]> code.delx.au - gnu-emacs/blob - src/xdisp.c
xdisp fix
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
315
316 #include "font.h"
317 #ifdef HAVE_XWIDGETS
318 #include "xwidget.h"
319 #endif
320 #ifndef FRAME_X_OUTPUT
321 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
322 #endif
323
324 #define INFINITY 10000000
325
326 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
327 Lisp_Object Qwindow_scroll_functions;
328 static Lisp_Object Qwindow_text_change_functions;
329 static Lisp_Object Qredisplay_end_trigger_functions;
330 Lisp_Object Qinhibit_point_motion_hooks;
331 static Lisp_Object QCeval, QCpropertize;
332 Lisp_Object QCfile, QCdata;
333 static Lisp_Object Qfontified;
334 static Lisp_Object Qgrow_only;
335 static Lisp_Object Qinhibit_eval_during_redisplay;
336 static Lisp_Object Qbuffer_position, Qposition, Qobject;
337 static Lisp_Object Qright_to_left, Qleft_to_right;
338
339 /* Cursor shapes. */
340 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
341
342 /* Pointer shapes. */
343 static Lisp_Object Qarrow, Qhand;
344 Lisp_Object Qtext;
345
346 /* Holds the list (error). */
347 static Lisp_Object list_of_error;
348
349 static Lisp_Object Qfontification_functions;
350
351 static Lisp_Object Qwrap_prefix;
352 static Lisp_Object Qline_prefix;
353 static Lisp_Object Qredisplay_internal;
354
355 /* Non-nil means don't actually do any redisplay. */
356
357 Lisp_Object Qinhibit_redisplay;
358
359 /* Names of text properties relevant for redisplay. */
360
361 Lisp_Object Qdisplay;
362
363 Lisp_Object Qspace, QCalign_to;
364 static Lisp_Object QCrelative_width, QCrelative_height;
365 Lisp_Object Qleft_margin, Qright_margin;
366 static Lisp_Object Qspace_width, Qraise;
367 static Lisp_Object Qslice;
368 Lisp_Object Qcenter;
369 static Lisp_Object Qmargin, Qpointer;
370 static Lisp_Object Qline_height;
371
372 #ifdef HAVE_WINDOW_SYSTEM
373
374 /* Test if overflow newline into fringe. Called with iterator IT
375 at or past right window margin, and with IT->current_x set. */
376
377 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
378 (!NILP (Voverflow_newline_into_fringe) \
379 && FRAME_WINDOW_P ((IT)->f) \
380 && ((IT)->bidi_it.paragraph_dir == R2L \
381 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
382 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
383 && (IT)->current_x == (IT)->last_visible_x \
384 && (IT)->line_wrap != WORD_WRAP)
385
386 #else /* !HAVE_WINDOW_SYSTEM */
387 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
388 #endif /* HAVE_WINDOW_SYSTEM */
389
390 /* Test if the display element loaded in IT, or the underlying buffer
391 or string character, is a space or a TAB character. This is used
392 to determine where word wrapping can occur. */
393
394 #define IT_DISPLAYING_WHITESPACE(it) \
395 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
396 || ((STRINGP (it->string) \
397 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
398 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
399 || (it->s \
400 && (it->s[IT_BYTEPOS (*it)] == ' ' \
401 || it->s[IT_BYTEPOS (*it)] == '\t')) \
402 || (IT_BYTEPOS (*it) < ZV_BYTE \
403 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
404 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
405
406 /* Name of the face used to highlight trailing whitespace. */
407
408 static Lisp_Object Qtrailing_whitespace;
409
410 /* Name and number of the face used to highlight escape glyphs. */
411
412 static Lisp_Object Qescape_glyph;
413
414 /* Name and number of the face used to highlight non-breaking spaces. */
415
416 static Lisp_Object Qnobreak_space;
417
418 /* The symbol `image' which is the car of the lists used to represent
419 images in Lisp. Also a tool bar style. */
420
421 Lisp_Object Qimage;
422
423 /* The image map types. */
424 Lisp_Object QCmap;
425 static Lisp_Object QCpointer;
426 static Lisp_Object Qrect, Qcircle, Qpoly;
427
428 /* Tool bar styles */
429 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
430
431 /* Non-zero means print newline to stdout before next mini-buffer
432 message. */
433
434 int noninteractive_need_newline;
435
436 /* Non-zero means print newline to message log before next message. */
437
438 static int message_log_need_newline;
439
440 /* Three markers that message_dolog uses.
441 It could allocate them itself, but that causes trouble
442 in handling memory-full errors. */
443 static Lisp_Object message_dolog_marker1;
444 static Lisp_Object message_dolog_marker2;
445 static Lisp_Object message_dolog_marker3;
446 \f
447 /* The buffer position of the first character appearing entirely or
448 partially on the line of the selected window which contains the
449 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
450 redisplay optimization in redisplay_internal. */
451
452 static struct text_pos this_line_start_pos;
453
454 /* Number of characters past the end of the line above, including the
455 terminating newline. */
456
457 static struct text_pos this_line_end_pos;
458
459 /* The vertical positions and the height of this line. */
460
461 static int this_line_vpos;
462 static int this_line_y;
463 static int this_line_pixel_height;
464
465 /* X position at which this display line starts. Usually zero;
466 negative if first character is partially visible. */
467
468 static int this_line_start_x;
469
470 /* The smallest character position seen by move_it_* functions as they
471 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
472 hscrolled lines, see display_line. */
473
474 static struct text_pos this_line_min_pos;
475
476 /* Buffer that this_line_.* variables are referring to. */
477
478 static struct buffer *this_line_buffer;
479
480
481 /* Values of those variables at last redisplay are stored as
482 properties on `overlay-arrow-position' symbol. However, if
483 Voverlay_arrow_position is a marker, last-arrow-position is its
484 numerical position. */
485
486 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
487
488 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
489 properties on a symbol in overlay-arrow-variable-list. */
490
491 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
492
493 Lisp_Object Qmenu_bar_update_hook;
494
495 /* Nonzero if an overlay arrow has been displayed in this window. */
496
497 static int overlay_arrow_seen;
498
499 /* Vector containing glyphs for an ellipsis `...'. */
500
501 static Lisp_Object default_invis_vector[3];
502
503 /* This is the window where the echo area message was displayed. It
504 is always a mini-buffer window, but it may not be the same window
505 currently active as a mini-buffer. */
506
507 Lisp_Object echo_area_window;
508
509 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
510 pushes the current message and the value of
511 message_enable_multibyte on the stack, the function restore_message
512 pops the stack and displays MESSAGE again. */
513
514 static Lisp_Object Vmessage_stack;
515
516 /* Nonzero means multibyte characters were enabled when the echo area
517 message was specified. */
518
519 static int message_enable_multibyte;
520
521 /* Nonzero if we should redraw the mode lines on the next redisplay. */
522
523 int update_mode_lines;
524
525 /* Nonzero if window sizes or contents have changed since last
526 redisplay that finished. */
527
528 int windows_or_buffers_changed;
529
530 /* Nonzero means a frame's cursor type has been changed. */
531
532 int cursor_type_changed;
533
534 /* Nonzero after display_mode_line if %l was used and it displayed a
535 line number. */
536
537 static int line_number_displayed;
538
539 /* The name of the *Messages* buffer, a string. */
540
541 static Lisp_Object Vmessages_buffer_name;
542
543 /* Current, index 0, and last displayed echo area message. Either
544 buffers from echo_buffers, or nil to indicate no message. */
545
546 Lisp_Object echo_area_buffer[2];
547
548 /* The buffers referenced from echo_area_buffer. */
549
550 static Lisp_Object echo_buffer[2];
551
552 /* A vector saved used in with_area_buffer to reduce consing. */
553
554 static Lisp_Object Vwith_echo_area_save_vector;
555
556 /* Non-zero means display_echo_area should display the last echo area
557 message again. Set by redisplay_preserve_echo_area. */
558
559 static int display_last_displayed_message_p;
560
561 /* Nonzero if echo area is being used by print; zero if being used by
562 message. */
563
564 static int message_buf_print;
565
566 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
567
568 static Lisp_Object Qinhibit_menubar_update;
569 static Lisp_Object Qmessage_truncate_lines;
570
571 /* Set to 1 in clear_message to make redisplay_internal aware
572 of an emptied echo area. */
573
574 static int message_cleared_p;
575
576 /* A scratch glyph row with contents used for generating truncation
577 glyphs. Also used in direct_output_for_insert. */
578
579 #define MAX_SCRATCH_GLYPHS 100
580 static struct glyph_row scratch_glyph_row;
581 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
582
583 /* Ascent and height of the last line processed by move_it_to. */
584
585 static int last_height;
586
587 /* Non-zero if there's a help-echo in the echo area. */
588
589 int help_echo_showing_p;
590
591 /* If >= 0, computed, exact values of mode-line and header-line height
592 to use in the macros CURRENT_MODE_LINE_HEIGHT and
593 CURRENT_HEADER_LINE_HEIGHT. */
594
595 int current_mode_line_height, current_header_line_height;
596
597 /* The maximum distance to look ahead for text properties. Values
598 that are too small let us call compute_char_face and similar
599 functions too often which is expensive. Values that are too large
600 let us call compute_char_face and alike too often because we
601 might not be interested in text properties that far away. */
602
603 #define TEXT_PROP_DISTANCE_LIMIT 100
604
605 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
606 iterator state and later restore it. This is needed because the
607 bidi iterator on bidi.c keeps a stacked cache of its states, which
608 is really a singleton. When we use scratch iterator objects to
609 move around the buffer, we can cause the bidi cache to be pushed or
610 popped, and therefore we need to restore the cache state when we
611 return to the original iterator. */
612 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
613 do { \
614 if (CACHE) \
615 bidi_unshelve_cache (CACHE, 1); \
616 ITCOPY = ITORIG; \
617 CACHE = bidi_shelve_cache (); \
618 } while (0)
619
620 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
621 do { \
622 if (pITORIG != pITCOPY) \
623 *(pITORIG) = *(pITCOPY); \
624 bidi_unshelve_cache (CACHE, 0); \
625 CACHE = NULL; \
626 } while (0)
627
628 #ifdef GLYPH_DEBUG
629
630 /* Non-zero means print traces of redisplay if compiled with
631 GLYPH_DEBUG defined. */
632
633 int trace_redisplay_p;
634
635 #endif /* GLYPH_DEBUG */
636
637 #ifdef DEBUG_TRACE_MOVE
638 /* Non-zero means trace with TRACE_MOVE to stderr. */
639 int trace_move;
640
641 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
642 #else
643 #define TRACE_MOVE(x) (void) 0
644 #endif
645
646 static Lisp_Object Qauto_hscroll_mode;
647
648 /* Buffer being redisplayed -- for redisplay_window_error. */
649
650 static struct buffer *displayed_buffer;
651
652 /* Value returned from text property handlers (see below). */
653
654 enum prop_handled
655 {
656 HANDLED_NORMALLY,
657 HANDLED_RECOMPUTE_PROPS,
658 HANDLED_OVERLAY_STRING_CONSUMED,
659 HANDLED_RETURN
660 };
661
662 /* A description of text properties that redisplay is interested
663 in. */
664
665 struct props
666 {
667 /* The name of the property. */
668 Lisp_Object *name;
669
670 /* A unique index for the property. */
671 enum prop_idx idx;
672
673 /* A handler function called to set up iterator IT from the property
674 at IT's current position. Value is used to steer handle_stop. */
675 enum prop_handled (*handler) (struct it *it);
676 };
677
678 static enum prop_handled handle_face_prop (struct it *);
679 static enum prop_handled handle_invisible_prop (struct it *);
680 static enum prop_handled handle_display_prop (struct it *);
681 static enum prop_handled handle_composition_prop (struct it *);
682 static enum prop_handled handle_overlay_change (struct it *);
683 static enum prop_handled handle_fontified_prop (struct it *);
684
685 /* Properties handled by iterators. */
686
687 static struct props it_props[] =
688 {
689 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
690 /* Handle `face' before `display' because some sub-properties of
691 `display' need to know the face. */
692 {&Qface, FACE_PROP_IDX, handle_face_prop},
693 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
694 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
695 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
696 {NULL, 0, NULL}
697 };
698
699 /* Value is the position described by X. If X is a marker, value is
700 the marker_position of X. Otherwise, value is X. */
701
702 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
703
704 /* Enumeration returned by some move_it_.* functions internally. */
705
706 enum move_it_result
707 {
708 /* Not used. Undefined value. */
709 MOVE_UNDEFINED,
710
711 /* Move ended at the requested buffer position or ZV. */
712 MOVE_POS_MATCH_OR_ZV,
713
714 /* Move ended at the requested X pixel position. */
715 MOVE_X_REACHED,
716
717 /* Move within a line ended at the end of a line that must be
718 continued. */
719 MOVE_LINE_CONTINUED,
720
721 /* Move within a line ended at the end of a line that would
722 be displayed truncated. */
723 MOVE_LINE_TRUNCATED,
724
725 /* Move within a line ended at a line end. */
726 MOVE_NEWLINE_OR_CR
727 };
728
729 /* This counter is used to clear the face cache every once in a while
730 in redisplay_internal. It is incremented for each redisplay.
731 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
732 cleared. */
733
734 #define CLEAR_FACE_CACHE_COUNT 500
735 static int clear_face_cache_count;
736
737 /* Similarly for the image cache. */
738
739 #ifdef HAVE_WINDOW_SYSTEM
740 #define CLEAR_IMAGE_CACHE_COUNT 101
741 static int clear_image_cache_count;
742
743 /* Null glyph slice */
744 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
745 #endif
746
747 /* True while redisplay_internal is in progress. */
748
749 bool redisplaying_p;
750
751 static Lisp_Object Qinhibit_free_realized_faces;
752 static Lisp_Object Qmode_line_default_help_echo;
753
754 /* If a string, XTread_socket generates an event to display that string.
755 (The display is done in read_char.) */
756
757 Lisp_Object help_echo_string;
758 Lisp_Object help_echo_window;
759 Lisp_Object help_echo_object;
760 ptrdiff_t help_echo_pos;
761
762 /* Temporary variable for XTread_socket. */
763
764 Lisp_Object previous_help_echo_string;
765
766 /* Platform-independent portion of hourglass implementation. */
767
768 /* Non-zero means an hourglass cursor is currently shown. */
769 int hourglass_shown_p;
770
771 /* If non-null, an asynchronous timer that, when it expires, displays
772 an hourglass cursor on all frames. */
773 struct atimer *hourglass_atimer;
774
775 /* Name of the face used to display glyphless characters. */
776 Lisp_Object Qglyphless_char;
777
778 /* Symbol for the purpose of Vglyphless_char_display. */
779 static Lisp_Object Qglyphless_char_display;
780
781 /* Method symbols for Vglyphless_char_display. */
782 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
783
784 /* Default pixel width of `thin-space' display method. */
785 #define THIN_SPACE_WIDTH 1
786
787 /* Default number of seconds to wait before displaying an hourglass
788 cursor. */
789 #define DEFAULT_HOURGLASS_DELAY 1
790
791 \f
792 /* Function prototypes. */
793
794 static void setup_for_ellipsis (struct it *, int);
795 static void set_iterator_to_next (struct it *, int);
796 static void mark_window_display_accurate_1 (struct window *, int);
797 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
798 static int display_prop_string_p (Lisp_Object, Lisp_Object);
799 static int cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, int);
801 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
802
803 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
804
805 static void handle_line_prefix (struct it *);
806
807 static void pint2str (char *, int, ptrdiff_t);
808 static void pint2hrstr (char *, int, ptrdiff_t);
809 static struct text_pos run_window_scroll_functions (Lisp_Object,
810 struct text_pos);
811 static void reconsider_clip_changes (struct window *, struct buffer *);
812 static int text_outside_line_unchanged_p (struct window *,
813 ptrdiff_t, ptrdiff_t);
814 static void store_mode_line_noprop_char (char);
815 static int store_mode_line_noprop (const char *, int, int);
816 static void handle_stop (struct it *);
817 static void handle_stop_backwards (struct it *, ptrdiff_t);
818 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
819 static void ensure_echo_area_buffers (void);
820 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
821 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
822 static int with_echo_area_buffer (struct window *, int,
823 int (*) (ptrdiff_t, Lisp_Object),
824 ptrdiff_t, Lisp_Object);
825 static void clear_garbaged_frames (void);
826 static int current_message_1 (ptrdiff_t, Lisp_Object);
827 static void pop_message (void);
828 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
829 static void set_message (Lisp_Object);
830 static int set_message_1 (ptrdiff_t, Lisp_Object);
831 static int display_echo_area (struct window *);
832 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
833 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
834 static Lisp_Object unwind_redisplay (Lisp_Object);
835 static int string_char_and_length (const unsigned char *, int *);
836 static struct text_pos display_prop_end (struct it *, Lisp_Object,
837 struct text_pos);
838 static int compute_window_start_on_continuation_line (struct window *);
839 static void insert_left_trunc_glyphs (struct it *);
840 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
841 Lisp_Object);
842 static void extend_face_to_end_of_line (struct it *);
843 static int append_space_for_newline (struct it *, int);
844 static int cursor_row_fully_visible_p (struct window *, int, int);
845 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
846 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
847 static int trailing_whitespace_p (ptrdiff_t);
848 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
849 static void push_it (struct it *, struct text_pos *);
850 static void iterate_out_of_display_property (struct it *);
851 static void pop_it (struct it *);
852 static void sync_frame_with_window_matrix_rows (struct window *);
853 static void redisplay_internal (void);
854 static int echo_area_display (int);
855 static void redisplay_windows (Lisp_Object);
856 static void redisplay_window (Lisp_Object, int);
857 static Lisp_Object redisplay_window_error (Lisp_Object);
858 static Lisp_Object redisplay_window_0 (Lisp_Object);
859 static Lisp_Object redisplay_window_1 (Lisp_Object);
860 static int set_cursor_from_row (struct window *, struct glyph_row *,
861 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
862 int, int);
863 static int update_menu_bar (struct frame *, int, int);
864 static int try_window_reusing_current_matrix (struct window *);
865 static int try_window_id (struct window *);
866 static int display_line (struct it *);
867 static int display_mode_lines (struct window *);
868 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
869 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
870 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
871 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
872 static void display_menu_bar (struct window *);
873 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
874 ptrdiff_t *);
875 static int display_string (const char *, Lisp_Object, Lisp_Object,
876 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
877 static void compute_line_metrics (struct it *);
878 static void run_redisplay_end_trigger_hook (struct it *);
879 static int get_overlay_strings (struct it *, ptrdiff_t);
880 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
881 static void next_overlay_string (struct it *);
882 static void reseat (struct it *, struct text_pos, int);
883 static void reseat_1 (struct it *, struct text_pos, int);
884 static void back_to_previous_visible_line_start (struct it *);
885 void reseat_at_previous_visible_line_start (struct it *);
886 static void reseat_at_next_visible_line_start (struct it *, int);
887 static int next_element_from_ellipsis (struct it *);
888 static int next_element_from_display_vector (struct it *);
889 static int next_element_from_string (struct it *);
890 static int next_element_from_c_string (struct it *);
891 static int next_element_from_buffer (struct it *);
892 static int next_element_from_composition (struct it *);
893 static int next_element_from_image (struct it *);
894 #ifdef HAVE_XWIDGETS
895 static int next_element_from_xwidget(struct it *);
896 #endif
897 static int next_element_from_stretch (struct it *);
898 static void load_overlay_strings (struct it *, ptrdiff_t);
899 static int init_from_display_pos (struct it *, struct window *,
900 struct display_pos *);
901 static void reseat_to_string (struct it *, const char *,
902 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
903 static int get_next_display_element (struct it *);
904 static enum move_it_result
905 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
906 enum move_operation_enum);
907 void move_it_vertically_backward (struct it *, int);
908 static void get_visually_first_element (struct it *);
909 static void init_to_row_start (struct it *, struct window *,
910 struct glyph_row *);
911 static int init_to_row_end (struct it *, struct window *,
912 struct glyph_row *);
913 static void back_to_previous_line_start (struct it *);
914 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
915 static struct text_pos string_pos_nchars_ahead (struct text_pos,
916 Lisp_Object, ptrdiff_t);
917 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
918 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
919 static ptrdiff_t number_of_chars (const char *, bool);
920 static void compute_stop_pos (struct it *);
921 static void compute_string_pos (struct text_pos *, struct text_pos,
922 Lisp_Object);
923 static int face_before_or_after_it_pos (struct it *, int);
924 static ptrdiff_t next_overlay_change (ptrdiff_t);
925 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
926 Lisp_Object, struct text_pos *, ptrdiff_t, int);
927 static int handle_single_display_spec (struct it *, Lisp_Object,
928 Lisp_Object, Lisp_Object,
929 struct text_pos *, ptrdiff_t, int, int);
930 static int underlying_face_id (struct it *);
931 static int in_ellipses_for_invisible_text_p (struct display_pos *,
932 struct window *);
933
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
936
937 #ifdef HAVE_WINDOW_SYSTEM
938
939 static void x_consider_frame_title (Lisp_Object);
940 static int tool_bar_lines_needed (struct frame *, int *);
941 static void update_tool_bar (struct frame *, int);
942 static void build_desired_tool_bar_string (struct frame *f);
943 static int redisplay_tool_bar (struct frame *);
944 static void display_tool_bar_line (struct it *, int);
945 static void notice_overwritten_cursor (struct window *,
946 enum glyph_row_area,
947 int, int, int, int);
948 static void append_stretch_glyph (struct it *, Lisp_Object,
949 int, int, int);
950
951
952 #endif /* HAVE_WINDOW_SYSTEM */
953
954 static void produce_special_glyphs (struct it *, enum display_element_type);
955 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
956 static int coords_in_mouse_face_p (struct window *, int, int);
957
958
959 \f
960 /***********************************************************************
961 Window display dimensions
962 ***********************************************************************/
963
964 /* Return the bottom boundary y-position for text lines in window W.
965 This is the first y position at which a line cannot start.
966 It is relative to the top of the window.
967
968 This is the height of W minus the height of a mode line, if any. */
969
970 int
971 window_text_bottom_y (struct window *w)
972 {
973 int height = WINDOW_TOTAL_HEIGHT (w);
974
975 if (WINDOW_WANTS_MODELINE_P (w))
976 height -= CURRENT_MODE_LINE_HEIGHT (w);
977 return height;
978 }
979
980 /* Return the pixel width of display area AREA of window W. AREA < 0
981 means return the total width of W, not including fringes to
982 the left and right of the window. */
983
984 int
985 window_box_width (struct window *w, int area)
986 {
987 int cols = w->total_cols;
988 int pixels = 0;
989
990 if (!w->pseudo_window_p)
991 {
992 cols -= WINDOW_SCROLL_BAR_COLS (w);
993
994 if (area == TEXT_AREA)
995 {
996 if (INTEGERP (w->left_margin_cols))
997 cols -= XFASTINT (w->left_margin_cols);
998 if (INTEGERP (w->right_margin_cols))
999 cols -= XFASTINT (w->right_margin_cols);
1000 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1001 }
1002 else if (area == LEFT_MARGIN_AREA)
1003 {
1004 cols = (INTEGERP (w->left_margin_cols)
1005 ? XFASTINT (w->left_margin_cols) : 0);
1006 pixels = 0;
1007 }
1008 else if (area == RIGHT_MARGIN_AREA)
1009 {
1010 cols = (INTEGERP (w->right_margin_cols)
1011 ? XFASTINT (w->right_margin_cols) : 0);
1012 pixels = 0;
1013 }
1014 }
1015
1016 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1017 }
1018
1019
1020 /* Return the pixel height of the display area of window W, not
1021 including mode lines of W, if any. */
1022
1023 int
1024 window_box_height (struct window *w)
1025 {
1026 struct frame *f = XFRAME (w->frame);
1027 int height = WINDOW_TOTAL_HEIGHT (w);
1028
1029 eassert (height >= 0);
1030
1031 /* Note: the code below that determines the mode-line/header-line
1032 height is essentially the same as that contained in the macro
1033 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1034 the appropriate glyph row has its `mode_line_p' flag set,
1035 and if it doesn't, uses estimate_mode_line_height instead. */
1036
1037 if (WINDOW_WANTS_MODELINE_P (w))
1038 {
1039 struct glyph_row *ml_row
1040 = (w->current_matrix && w->current_matrix->rows
1041 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1042 : 0);
1043 if (ml_row && ml_row->mode_line_p)
1044 height -= ml_row->height;
1045 else
1046 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1047 }
1048
1049 if (WINDOW_WANTS_HEADER_LINE_P (w))
1050 {
1051 struct glyph_row *hl_row
1052 = (w->current_matrix && w->current_matrix->rows
1053 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1054 : 0);
1055 if (hl_row && hl_row->mode_line_p)
1056 height -= hl_row->height;
1057 else
1058 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1059 }
1060
1061 /* With a very small font and a mode-line that's taller than
1062 default, we might end up with a negative height. */
1063 return max (0, height);
1064 }
1065
1066 /* Return the window-relative coordinate of the left edge of display
1067 area AREA of window W. AREA < 0 means return the left edge of the
1068 whole window, to the right of the left fringe of W. */
1069
1070 int
1071 window_box_left_offset (struct window *w, int area)
1072 {
1073 int x;
1074
1075 if (w->pseudo_window_p)
1076 return 0;
1077
1078 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079
1080 if (area == TEXT_AREA)
1081 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1082 + window_box_width (w, LEFT_MARGIN_AREA));
1083 else if (area == RIGHT_MARGIN_AREA)
1084 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1085 + window_box_width (w, LEFT_MARGIN_AREA)
1086 + window_box_width (w, TEXT_AREA)
1087 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 ? 0
1089 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1090 else if (area == LEFT_MARGIN_AREA
1091 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1092 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093
1094 return x;
1095 }
1096
1097
1098 /* Return the window-relative coordinate of the right edge of display
1099 area AREA of window W. AREA < 0 means return the right edge of the
1100 whole window, to the left of the right fringe of W. */
1101
1102 int
1103 window_box_right_offset (struct window *w, int area)
1104 {
1105 return window_box_left_offset (w, area) + window_box_width (w, area);
1106 }
1107
1108 /* Return the frame-relative coordinate of the left edge of display
1109 area AREA of window W. AREA < 0 means return the left edge of the
1110 whole window, to the right of the left fringe of W. */
1111
1112 int
1113 window_box_left (struct window *w, int area)
1114 {
1115 struct frame *f = XFRAME (w->frame);
1116 int x;
1117
1118 if (w->pseudo_window_p)
1119 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120
1121 x = (WINDOW_LEFT_EDGE_X (w)
1122 + window_box_left_offset (w, area));
1123
1124 return x;
1125 }
1126
1127
1128 /* Return the frame-relative coordinate of the right edge of display
1129 area AREA of window W. AREA < 0 means return the right edge of the
1130 whole window, to the left of the right fringe of W. */
1131
1132 int
1133 window_box_right (struct window *w, int area)
1134 {
1135 return window_box_left (w, area) + window_box_width (w, area);
1136 }
1137
1138 /* Get the bounding box of the display area AREA of window W, without
1139 mode lines, in frame-relative coordinates. AREA < 0 means the
1140 whole window, not including the left and right fringes of
1141 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1142 coordinates of the upper-left corner of the box. Return in
1143 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1144
1145 void
1146 window_box (struct window *w, int area, int *box_x, int *box_y,
1147 int *box_width, int *box_height)
1148 {
1149 if (box_width)
1150 *box_width = window_box_width (w, area);
1151 if (box_height)
1152 *box_height = window_box_height (w);
1153 if (box_x)
1154 *box_x = window_box_left (w, area);
1155 if (box_y)
1156 {
1157 *box_y = WINDOW_TOP_EDGE_Y (w);
1158 if (WINDOW_WANTS_HEADER_LINE_P (w))
1159 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1160 }
1161 }
1162
1163
1164 /* Get the bounding box of the display area AREA of window W, without
1165 mode lines. AREA < 0 means the whole window, not including the
1166 left and right fringe of the window. Return in *TOP_LEFT_X
1167 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1168 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1169 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1170 box. */
1171
1172 static void
1173 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1174 int *bottom_right_x, int *bottom_right_y)
1175 {
1176 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1177 bottom_right_y);
1178 *bottom_right_x += *top_left_x;
1179 *bottom_right_y += *top_left_y;
1180 }
1181
1182
1183 \f
1184 /***********************************************************************
1185 Utilities
1186 ***********************************************************************/
1187
1188 /* Return the bottom y-position of the line the iterator IT is in.
1189 This can modify IT's settings. */
1190
1191 int
1192 line_bottom_y (struct it *it)
1193 {
1194 int line_height = it->max_ascent + it->max_descent;
1195 int line_top_y = it->current_y;
1196
1197 if (line_height == 0)
1198 {
1199 if (last_height)
1200 line_height = last_height;
1201 else if (IT_CHARPOS (*it) < ZV)
1202 {
1203 move_it_by_lines (it, 1);
1204 line_height = (it->max_ascent || it->max_descent
1205 ? it->max_ascent + it->max_descent
1206 : last_height);
1207 }
1208 else
1209 {
1210 struct glyph_row *row = it->glyph_row;
1211
1212 /* Use the default character height. */
1213 it->glyph_row = NULL;
1214 it->what = IT_CHARACTER;
1215 it->c = ' ';
1216 it->len = 1;
1217 PRODUCE_GLYPHS (it);
1218 line_height = it->ascent + it->descent;
1219 it->glyph_row = row;
1220 }
1221 }
1222
1223 return line_top_y + line_height;
1224 }
1225
1226 /* Subroutine of pos_visible_p below. Extracts a display string, if
1227 any, from the display spec given as its argument. */
1228 static Lisp_Object
1229 string_from_display_spec (Lisp_Object spec)
1230 {
1231 if (CONSP (spec))
1232 {
1233 while (CONSP (spec))
1234 {
1235 if (STRINGP (XCAR (spec)))
1236 return XCAR (spec);
1237 spec = XCDR (spec);
1238 }
1239 }
1240 else if (VECTORP (spec))
1241 {
1242 ptrdiff_t i;
1243
1244 for (i = 0; i < ASIZE (spec); i++)
1245 {
1246 if (STRINGP (AREF (spec, i)))
1247 return AREF (spec, i);
1248 }
1249 return Qnil;
1250 }
1251
1252 return spec;
1253 }
1254
1255
1256 /* Limit insanely large values of W->hscroll on frame F to the largest
1257 value that will still prevent first_visible_x and last_visible_x of
1258 'struct it' from overflowing an int. */
1259 static int
1260 window_hscroll_limited (struct window *w, struct frame *f)
1261 {
1262 ptrdiff_t window_hscroll = w->hscroll;
1263 int window_text_width = window_box_width (w, TEXT_AREA);
1264 int colwidth = FRAME_COLUMN_WIDTH (f);
1265
1266 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1267 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1268
1269 return window_hscroll;
1270 }
1271
1272 /* Return 1 if position CHARPOS is visible in window W.
1273 CHARPOS < 0 means return info about WINDOW_END position.
1274 If visible, set *X and *Y to pixel coordinates of top left corner.
1275 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1276 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1277
1278 int
1279 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1280 int *rtop, int *rbot, int *rowh, int *vpos)
1281 {
1282 struct it it;
1283 void *itdata = bidi_shelve_cache ();
1284 struct text_pos top;
1285 int visible_p = 0;
1286 struct buffer *old_buffer = NULL;
1287
1288 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1289 return visible_p;
1290
1291 if (XBUFFER (w->contents) != current_buffer)
1292 {
1293 old_buffer = current_buffer;
1294 set_buffer_internal_1 (XBUFFER (w->contents));
1295 }
1296
1297 SET_TEXT_POS_FROM_MARKER (top, w->start);
1298 /* Scrolling a minibuffer window via scroll bar when the echo area
1299 shows long text sometimes resets the minibuffer contents behind
1300 our backs. */
1301 if (CHARPOS (top) > ZV)
1302 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1303
1304 /* Compute exact mode line heights. */
1305 if (WINDOW_WANTS_MODELINE_P (w))
1306 current_mode_line_height
1307 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1308 BVAR (current_buffer, mode_line_format));
1309
1310 if (WINDOW_WANTS_HEADER_LINE_P (w))
1311 current_header_line_height
1312 = display_mode_line (w, HEADER_LINE_FACE_ID,
1313 BVAR (current_buffer, header_line_format));
1314
1315 start_display (&it, w, top);
1316 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1317 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1318
1319 if (charpos >= 0
1320 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1321 && IT_CHARPOS (it) >= charpos)
1322 /* When scanning backwards under bidi iteration, move_it_to
1323 stops at or _before_ CHARPOS, because it stops at or to
1324 the _right_ of the character at CHARPOS. */
1325 || (it.bidi_p && it.bidi_it.scan_dir == -1
1326 && IT_CHARPOS (it) <= charpos)))
1327 {
1328 /* We have reached CHARPOS, or passed it. How the call to
1329 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1330 or covered by a display property, move_it_to stops at the end
1331 of the invisible text, to the right of CHARPOS. (ii) If
1332 CHARPOS is in a display vector, move_it_to stops on its last
1333 glyph. */
1334 int top_x = it.current_x;
1335 int top_y = it.current_y;
1336 /* Calling line_bottom_y may change it.method, it.position, etc. */
1337 enum it_method it_method = it.method;
1338 int bottom_y = (last_height = 0, line_bottom_y (&it));
1339 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1340
1341 if (top_y < window_top_y)
1342 visible_p = bottom_y > window_top_y;
1343 else if (top_y < it.last_visible_y)
1344 visible_p = 1;
1345 if (bottom_y >= it.last_visible_y
1346 && it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) < charpos)
1348 {
1349 /* When the last line of the window is scanned backwards
1350 under bidi iteration, we could be duped into thinking
1351 that we have passed CHARPOS, when in fact move_it_to
1352 simply stopped short of CHARPOS because it reached
1353 last_visible_y. To see if that's what happened, we call
1354 move_it_to again with a slightly larger vertical limit,
1355 and see if it actually moved vertically; if it did, we
1356 didn't really reach CHARPOS, which is beyond window end. */
1357 struct it save_it = it;
1358 /* Why 10? because we don't know how many canonical lines
1359 will the height of the next line(s) be. So we guess. */
1360 int ten_more_lines =
1361 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1362
1363 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1364 MOVE_TO_POS | MOVE_TO_Y);
1365 if (it.current_y > top_y)
1366 visible_p = 0;
1367
1368 it = save_it;
1369 }
1370 if (visible_p)
1371 {
1372 if (it_method == GET_FROM_DISPLAY_VECTOR)
1373 {
1374 /* We stopped on the last glyph of a display vector.
1375 Try and recompute. Hack alert! */
1376 if (charpos < 2 || top.charpos >= charpos)
1377 top_x = it.glyph_row->x;
1378 else
1379 {
1380 struct it it2;
1381 start_display (&it2, w, top);
1382 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1383 get_next_display_element (&it2);
1384 PRODUCE_GLYPHS (&it2);
1385 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1386 || it2.current_x > it2.last_visible_x)
1387 top_x = it.glyph_row->x;
1388 else
1389 {
1390 top_x = it2.current_x;
1391 top_y = it2.current_y;
1392 }
1393 }
1394 }
1395 else if (IT_CHARPOS (it) != charpos)
1396 {
1397 Lisp_Object cpos = make_number (charpos);
1398 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1399 Lisp_Object string = string_from_display_spec (spec);
1400 bool newline_in_string
1401 = (STRINGP (string)
1402 && memchr (SDATA (string), '\n', SBYTES (string)));
1403 /* The tricky code below is needed because there's a
1404 discrepancy between move_it_to and how we set cursor
1405 when the display line ends in a newline from a
1406 display string. move_it_to will stop _after_ such
1407 display strings, whereas set_cursor_from_row
1408 conspires with cursor_row_p to place the cursor on
1409 the first glyph produced from the display string. */
1410
1411 /* We have overshoot PT because it is covered by a
1412 display property whose value is a string. If the
1413 string includes embedded newlines, we are also in the
1414 wrong display line. Backtrack to the correct line,
1415 where the display string begins. */
1416 if (newline_in_string)
1417 {
1418 Lisp_Object startpos, endpos;
1419 EMACS_INT start, end;
1420 struct it it3;
1421 int it3_moved;
1422
1423 /* Find the first and the last buffer positions
1424 covered by the display string. */
1425 endpos =
1426 Fnext_single_char_property_change (cpos, Qdisplay,
1427 Qnil, Qnil);
1428 startpos =
1429 Fprevious_single_char_property_change (endpos, Qdisplay,
1430 Qnil, Qnil);
1431 start = XFASTINT (startpos);
1432 end = XFASTINT (endpos);
1433 /* Move to the last buffer position before the
1434 display property. */
1435 start_display (&it3, w, top);
1436 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1437 /* Move forward one more line if the position before
1438 the display string is a newline or if it is the
1439 rightmost character on a line that is
1440 continued or word-wrapped. */
1441 if (it3.method == GET_FROM_BUFFER
1442 && it3.c == '\n')
1443 move_it_by_lines (&it3, 1);
1444 else if (move_it_in_display_line_to (&it3, -1,
1445 it3.current_x
1446 + it3.pixel_width,
1447 MOVE_TO_X)
1448 == MOVE_LINE_CONTINUED)
1449 {
1450 move_it_by_lines (&it3, 1);
1451 /* When we are under word-wrap, the #$@%!
1452 move_it_by_lines moves 2 lines, so we need to
1453 fix that up. */
1454 if (it3.line_wrap == WORD_WRAP)
1455 move_it_by_lines (&it3, -1);
1456 }
1457
1458 /* Record the vertical coordinate of the display
1459 line where we wound up. */
1460 top_y = it3.current_y;
1461 if (it3.bidi_p)
1462 {
1463 /* When characters are reordered for display,
1464 the character displayed to the left of the
1465 display string could be _after_ the display
1466 property in the logical order. Use the
1467 smallest vertical position of these two. */
1468 start_display (&it3, w, top);
1469 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1470 if (it3.current_y < top_y)
1471 top_y = it3.current_y;
1472 }
1473 /* Move from the top of the window to the beginning
1474 of the display line where the display string
1475 begins. */
1476 start_display (&it3, w, top);
1477 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1478 /* If it3_moved stays zero after the 'while' loop
1479 below, that means we already were at a newline
1480 before the loop (e.g., the display string begins
1481 with a newline), so we don't need to (and cannot)
1482 inspect the glyphs of it3.glyph_row, because
1483 PRODUCE_GLYPHS will not produce anything for a
1484 newline, and thus it3.glyph_row stays at its
1485 stale content it got at top of the window. */
1486 it3_moved = 0;
1487 /* Finally, advance the iterator until we hit the
1488 first display element whose character position is
1489 CHARPOS, or until the first newline from the
1490 display string, which signals the end of the
1491 display line. */
1492 while (get_next_display_element (&it3))
1493 {
1494 PRODUCE_GLYPHS (&it3);
1495 if (IT_CHARPOS (it3) == charpos
1496 || ITERATOR_AT_END_OF_LINE_P (&it3))
1497 break;
1498 it3_moved = 1;
1499 set_iterator_to_next (&it3, 0);
1500 }
1501 top_x = it3.current_x - it3.pixel_width;
1502 /* Normally, we would exit the above loop because we
1503 found the display element whose character
1504 position is CHARPOS. For the contingency that we
1505 didn't, and stopped at the first newline from the
1506 display string, move back over the glyphs
1507 produced from the string, until we find the
1508 rightmost glyph not from the string. */
1509 if (it3_moved
1510 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1511 {
1512 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1513 + it3.glyph_row->used[TEXT_AREA];
1514
1515 while (EQ ((g - 1)->object, string))
1516 {
1517 --g;
1518 top_x -= g->pixel_width;
1519 }
1520 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1521 + it3.glyph_row->used[TEXT_AREA]);
1522 }
1523 }
1524 }
1525
1526 *x = top_x;
1527 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1528 *rtop = max (0, window_top_y - top_y);
1529 *rbot = max (0, bottom_y - it.last_visible_y);
1530 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1531 - max (top_y, window_top_y)));
1532 *vpos = it.vpos;
1533 }
1534 }
1535 else
1536 {
1537 /* We were asked to provide info about WINDOW_END. */
1538 struct it it2;
1539 void *it2data = NULL;
1540
1541 SAVE_IT (it2, it, it2data);
1542 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1543 move_it_by_lines (&it, 1);
1544 if (charpos < IT_CHARPOS (it)
1545 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1546 {
1547 visible_p = 1;
1548 RESTORE_IT (&it2, &it2, it2data);
1549 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1550 *x = it2.current_x;
1551 *y = it2.current_y + it2.max_ascent - it2.ascent;
1552 *rtop = max (0, -it2.current_y);
1553 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1554 - it.last_visible_y));
1555 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1556 it.last_visible_y)
1557 - max (it2.current_y,
1558 WINDOW_HEADER_LINE_HEIGHT (w))));
1559 *vpos = it2.vpos;
1560 }
1561 else
1562 bidi_unshelve_cache (it2data, 1);
1563 }
1564 bidi_unshelve_cache (itdata, 0);
1565
1566 if (old_buffer)
1567 set_buffer_internal_1 (old_buffer);
1568
1569 current_header_line_height = current_mode_line_height = -1;
1570
1571 if (visible_p && w->hscroll > 0)
1572 *x -=
1573 window_hscroll_limited (w, WINDOW_XFRAME (w))
1574 * WINDOW_FRAME_COLUMN_WIDTH (w);
1575
1576 #if 0
1577 /* Debugging code. */
1578 if (visible_p)
1579 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1580 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1581 else
1582 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1583 #endif
1584
1585 return visible_p;
1586 }
1587
1588
1589 /* Return the next character from STR. Return in *LEN the length of
1590 the character. This is like STRING_CHAR_AND_LENGTH but never
1591 returns an invalid character. If we find one, we return a `?', but
1592 with the length of the invalid character. */
1593
1594 static int
1595 string_char_and_length (const unsigned char *str, int *len)
1596 {
1597 int c;
1598
1599 c = STRING_CHAR_AND_LENGTH (str, *len);
1600 if (!CHAR_VALID_P (c))
1601 /* We may not change the length here because other places in Emacs
1602 don't use this function, i.e. they silently accept invalid
1603 characters. */
1604 c = '?';
1605
1606 return c;
1607 }
1608
1609
1610
1611 /* Given a position POS containing a valid character and byte position
1612 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1613
1614 static struct text_pos
1615 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1616 {
1617 eassert (STRINGP (string) && nchars >= 0);
1618
1619 if (STRING_MULTIBYTE (string))
1620 {
1621 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1622 int len;
1623
1624 while (nchars--)
1625 {
1626 string_char_and_length (p, &len);
1627 p += len;
1628 CHARPOS (pos) += 1;
1629 BYTEPOS (pos) += len;
1630 }
1631 }
1632 else
1633 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1634
1635 return pos;
1636 }
1637
1638
1639 /* Value is the text position, i.e. character and byte position,
1640 for character position CHARPOS in STRING. */
1641
1642 static struct text_pos
1643 string_pos (ptrdiff_t charpos, Lisp_Object string)
1644 {
1645 struct text_pos pos;
1646 eassert (STRINGP (string));
1647 eassert (charpos >= 0);
1648 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1649 return pos;
1650 }
1651
1652
1653 /* Value is a text position, i.e. character and byte position, for
1654 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1655 means recognize multibyte characters. */
1656
1657 static struct text_pos
1658 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1659 {
1660 struct text_pos pos;
1661
1662 eassert (s != NULL);
1663 eassert (charpos >= 0);
1664
1665 if (multibyte_p)
1666 {
1667 int len;
1668
1669 SET_TEXT_POS (pos, 0, 0);
1670 while (charpos--)
1671 {
1672 string_char_and_length ((const unsigned char *) s, &len);
1673 s += len;
1674 CHARPOS (pos) += 1;
1675 BYTEPOS (pos) += len;
1676 }
1677 }
1678 else
1679 SET_TEXT_POS (pos, charpos, charpos);
1680
1681 return pos;
1682 }
1683
1684
1685 /* Value is the number of characters in C string S. MULTIBYTE_P
1686 non-zero means recognize multibyte characters. */
1687
1688 static ptrdiff_t
1689 number_of_chars (const char *s, bool multibyte_p)
1690 {
1691 ptrdiff_t nchars;
1692
1693 if (multibyte_p)
1694 {
1695 ptrdiff_t rest = strlen (s);
1696 int len;
1697 const unsigned char *p = (const unsigned char *) s;
1698
1699 for (nchars = 0; rest > 0; ++nchars)
1700 {
1701 string_char_and_length (p, &len);
1702 rest -= len, p += len;
1703 }
1704 }
1705 else
1706 nchars = strlen (s);
1707
1708 return nchars;
1709 }
1710
1711
1712 /* Compute byte position NEWPOS->bytepos corresponding to
1713 NEWPOS->charpos. POS is a known position in string STRING.
1714 NEWPOS->charpos must be >= POS.charpos. */
1715
1716 static void
1717 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1718 {
1719 eassert (STRINGP (string));
1720 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1721
1722 if (STRING_MULTIBYTE (string))
1723 *newpos = string_pos_nchars_ahead (pos, string,
1724 CHARPOS (*newpos) - CHARPOS (pos));
1725 else
1726 BYTEPOS (*newpos) = CHARPOS (*newpos);
1727 }
1728
1729 /* EXPORT:
1730 Return an estimation of the pixel height of mode or header lines on
1731 frame F. FACE_ID specifies what line's height to estimate. */
1732
1733 int
1734 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1735 {
1736 #ifdef HAVE_WINDOW_SYSTEM
1737 if (FRAME_WINDOW_P (f))
1738 {
1739 int height = FONT_HEIGHT (FRAME_FONT (f));
1740
1741 /* This function is called so early when Emacs starts that the face
1742 cache and mode line face are not yet initialized. */
1743 if (FRAME_FACE_CACHE (f))
1744 {
1745 struct face *face = FACE_FROM_ID (f, face_id);
1746 if (face)
1747 {
1748 if (face->font)
1749 height = FONT_HEIGHT (face->font);
1750 if (face->box_line_width > 0)
1751 height += 2 * face->box_line_width;
1752 }
1753 }
1754
1755 return height;
1756 }
1757 #endif
1758
1759 return 1;
1760 }
1761
1762 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1763 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1764 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1765 not force the value into range. */
1766
1767 void
1768 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1769 int *x, int *y, NativeRectangle *bounds, int noclip)
1770 {
1771
1772 #ifdef HAVE_WINDOW_SYSTEM
1773 if (FRAME_WINDOW_P (f))
1774 {
1775 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1776 even for negative values. */
1777 if (pix_x < 0)
1778 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1779 if (pix_y < 0)
1780 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1781
1782 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1783 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1784
1785 if (bounds)
1786 STORE_NATIVE_RECT (*bounds,
1787 FRAME_COL_TO_PIXEL_X (f, pix_x),
1788 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1789 FRAME_COLUMN_WIDTH (f) - 1,
1790 FRAME_LINE_HEIGHT (f) - 1);
1791
1792 if (!noclip)
1793 {
1794 if (pix_x < 0)
1795 pix_x = 0;
1796 else if (pix_x > FRAME_TOTAL_COLS (f))
1797 pix_x = FRAME_TOTAL_COLS (f);
1798
1799 if (pix_y < 0)
1800 pix_y = 0;
1801 else if (pix_y > FRAME_LINES (f))
1802 pix_y = FRAME_LINES (f);
1803 }
1804 }
1805 #endif
1806
1807 *x = pix_x;
1808 *y = pix_y;
1809 }
1810
1811
1812 /* Find the glyph under window-relative coordinates X/Y in window W.
1813 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1814 strings. Return in *HPOS and *VPOS the row and column number of
1815 the glyph found. Return in *AREA the glyph area containing X.
1816 Value is a pointer to the glyph found or null if X/Y is not on
1817 text, or we can't tell because W's current matrix is not up to
1818 date. */
1819
1820 static
1821 struct glyph *
1822 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1823 int *dx, int *dy, int *area)
1824 {
1825 struct glyph *glyph, *end;
1826 struct glyph_row *row = NULL;
1827 int x0, i;
1828
1829 /* Find row containing Y. Give up if some row is not enabled. */
1830 for (i = 0; i < w->current_matrix->nrows; ++i)
1831 {
1832 row = MATRIX_ROW (w->current_matrix, i);
1833 if (!row->enabled_p)
1834 return NULL;
1835 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1836 break;
1837 }
1838
1839 *vpos = i;
1840 *hpos = 0;
1841
1842 /* Give up if Y is not in the window. */
1843 if (i == w->current_matrix->nrows)
1844 return NULL;
1845
1846 /* Get the glyph area containing X. */
1847 if (w->pseudo_window_p)
1848 {
1849 *area = TEXT_AREA;
1850 x0 = 0;
1851 }
1852 else
1853 {
1854 if (x < window_box_left_offset (w, TEXT_AREA))
1855 {
1856 *area = LEFT_MARGIN_AREA;
1857 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1858 }
1859 else if (x < window_box_right_offset (w, TEXT_AREA))
1860 {
1861 *area = TEXT_AREA;
1862 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1863 }
1864 else
1865 {
1866 *area = RIGHT_MARGIN_AREA;
1867 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1868 }
1869 }
1870
1871 /* Find glyph containing X. */
1872 glyph = row->glyphs[*area];
1873 end = glyph + row->used[*area];
1874 x -= x0;
1875 while (glyph < end && x >= glyph->pixel_width)
1876 {
1877 x -= glyph->pixel_width;
1878 ++glyph;
1879 }
1880
1881 if (glyph == end)
1882 return NULL;
1883
1884 if (dx)
1885 {
1886 *dx = x;
1887 *dy = y - (row->y + row->ascent - glyph->ascent);
1888 }
1889
1890 *hpos = glyph - row->glyphs[*area];
1891 return glyph;
1892 }
1893
1894 /* Convert frame-relative x/y to coordinates relative to window W.
1895 Takes pseudo-windows into account. */
1896
1897 static void
1898 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1899 {
1900 if (w->pseudo_window_p)
1901 {
1902 /* A pseudo-window is always full-width, and starts at the
1903 left edge of the frame, plus a frame border. */
1904 struct frame *f = XFRAME (w->frame);
1905 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1906 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1907 }
1908 else
1909 {
1910 *x -= WINDOW_LEFT_EDGE_X (w);
1911 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1912 }
1913 }
1914
1915 #ifdef HAVE_WINDOW_SYSTEM
1916
1917 /* EXPORT:
1918 Return in RECTS[] at most N clipping rectangles for glyph string S.
1919 Return the number of stored rectangles. */
1920
1921 int
1922 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1923 {
1924 XRectangle r;
1925
1926 if (n <= 0)
1927 return 0;
1928
1929 if (s->row->full_width_p)
1930 {
1931 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1932 r.x = WINDOW_LEFT_EDGE_X (s->w);
1933 r.width = WINDOW_TOTAL_WIDTH (s->w);
1934
1935 /* Unless displaying a mode or menu bar line, which are always
1936 fully visible, clip to the visible part of the row. */
1937 if (s->w->pseudo_window_p)
1938 r.height = s->row->visible_height;
1939 else
1940 r.height = s->height;
1941 }
1942 else
1943 {
1944 /* This is a text line that may be partially visible. */
1945 r.x = window_box_left (s->w, s->area);
1946 r.width = window_box_width (s->w, s->area);
1947 r.height = s->row->visible_height;
1948 }
1949
1950 if (s->clip_head)
1951 if (r.x < s->clip_head->x)
1952 {
1953 if (r.width >= s->clip_head->x - r.x)
1954 r.width -= s->clip_head->x - r.x;
1955 else
1956 r.width = 0;
1957 r.x = s->clip_head->x;
1958 }
1959 if (s->clip_tail)
1960 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1961 {
1962 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1963 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1964 else
1965 r.width = 0;
1966 }
1967
1968 /* If S draws overlapping rows, it's sufficient to use the top and
1969 bottom of the window for clipping because this glyph string
1970 intentionally draws over other lines. */
1971 if (s->for_overlaps)
1972 {
1973 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1974 r.height = window_text_bottom_y (s->w) - r.y;
1975
1976 /* Alas, the above simple strategy does not work for the
1977 environments with anti-aliased text: if the same text is
1978 drawn onto the same place multiple times, it gets thicker.
1979 If the overlap we are processing is for the erased cursor, we
1980 take the intersection with the rectangle of the cursor. */
1981 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1982 {
1983 XRectangle rc, r_save = r;
1984
1985 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1986 rc.y = s->w->phys_cursor.y;
1987 rc.width = s->w->phys_cursor_width;
1988 rc.height = s->w->phys_cursor_height;
1989
1990 x_intersect_rectangles (&r_save, &rc, &r);
1991 }
1992 }
1993 else
1994 {
1995 /* Don't use S->y for clipping because it doesn't take partially
1996 visible lines into account. For example, it can be negative for
1997 partially visible lines at the top of a window. */
1998 if (!s->row->full_width_p
1999 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2000 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2001 else
2002 r.y = max (0, s->row->y);
2003 }
2004
2005 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2006
2007 /* If drawing the cursor, don't let glyph draw outside its
2008 advertised boundaries. Cleartype does this under some circumstances. */
2009 if (s->hl == DRAW_CURSOR)
2010 {
2011 struct glyph *glyph = s->first_glyph;
2012 int height, max_y;
2013
2014 if (s->x > r.x)
2015 {
2016 r.width -= s->x - r.x;
2017 r.x = s->x;
2018 }
2019 r.width = min (r.width, glyph->pixel_width);
2020
2021 /* If r.y is below window bottom, ensure that we still see a cursor. */
2022 height = min (glyph->ascent + glyph->descent,
2023 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2024 max_y = window_text_bottom_y (s->w) - height;
2025 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2026 if (s->ybase - glyph->ascent > max_y)
2027 {
2028 r.y = max_y;
2029 r.height = height;
2030 }
2031 else
2032 {
2033 /* Don't draw cursor glyph taller than our actual glyph. */
2034 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2035 if (height < r.height)
2036 {
2037 max_y = r.y + r.height;
2038 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2039 r.height = min (max_y - r.y, height);
2040 }
2041 }
2042 }
2043
2044 if (s->row->clip)
2045 {
2046 XRectangle r_save = r;
2047
2048 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2049 r.width = 0;
2050 }
2051
2052 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2053 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2054 {
2055 #ifdef CONVERT_FROM_XRECT
2056 CONVERT_FROM_XRECT (r, *rects);
2057 #else
2058 *rects = r;
2059 #endif
2060 return 1;
2061 }
2062 else
2063 {
2064 /* If we are processing overlapping and allowed to return
2065 multiple clipping rectangles, we exclude the row of the glyph
2066 string from the clipping rectangle. This is to avoid drawing
2067 the same text on the environment with anti-aliasing. */
2068 #ifdef CONVERT_FROM_XRECT
2069 XRectangle rs[2];
2070 #else
2071 XRectangle *rs = rects;
2072 #endif
2073 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2074
2075 if (s->for_overlaps & OVERLAPS_PRED)
2076 {
2077 rs[i] = r;
2078 if (r.y + r.height > row_y)
2079 {
2080 if (r.y < row_y)
2081 rs[i].height = row_y - r.y;
2082 else
2083 rs[i].height = 0;
2084 }
2085 i++;
2086 }
2087 if (s->for_overlaps & OVERLAPS_SUCC)
2088 {
2089 rs[i] = r;
2090 if (r.y < row_y + s->row->visible_height)
2091 {
2092 if (r.y + r.height > row_y + s->row->visible_height)
2093 {
2094 rs[i].y = row_y + s->row->visible_height;
2095 rs[i].height = r.y + r.height - rs[i].y;
2096 }
2097 else
2098 rs[i].height = 0;
2099 }
2100 i++;
2101 }
2102
2103 n = i;
2104 #ifdef CONVERT_FROM_XRECT
2105 for (i = 0; i < n; i++)
2106 CONVERT_FROM_XRECT (rs[i], rects[i]);
2107 #endif
2108 return n;
2109 }
2110 }
2111
2112 /* EXPORT:
2113 Return in *NR the clipping rectangle for glyph string S. */
2114
2115 void
2116 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2117 {
2118 get_glyph_string_clip_rects (s, nr, 1);
2119 }
2120
2121
2122 /* EXPORT:
2123 Return the position and height of the phys cursor in window W.
2124 Set w->phys_cursor_width to width of phys cursor.
2125 */
2126
2127 void
2128 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2129 struct glyph *glyph, int *xp, int *yp, int *heightp)
2130 {
2131 struct frame *f = XFRAME (WINDOW_FRAME (w));
2132 int x, y, wd, h, h0, y0;
2133
2134 /* Compute the width of the rectangle to draw. If on a stretch
2135 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2136 rectangle as wide as the glyph, but use a canonical character
2137 width instead. */
2138 wd = glyph->pixel_width - 1;
2139 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2140 wd++; /* Why? */
2141 #endif
2142
2143 x = w->phys_cursor.x;
2144 if (x < 0)
2145 {
2146 wd += x;
2147 x = 0;
2148 }
2149
2150 if (glyph->type == STRETCH_GLYPH
2151 && !x_stretch_cursor_p)
2152 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2153 w->phys_cursor_width = wd;
2154
2155 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2156
2157 /* If y is below window bottom, ensure that we still see a cursor. */
2158 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2159
2160 h = max (h0, glyph->ascent + glyph->descent);
2161 h0 = min (h0, glyph->ascent + glyph->descent);
2162
2163 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2164 if (y < y0)
2165 {
2166 h = max (h - (y0 - y) + 1, h0);
2167 y = y0 - 1;
2168 }
2169 else
2170 {
2171 y0 = window_text_bottom_y (w) - h0;
2172 if (y > y0)
2173 {
2174 h += y - y0;
2175 y = y0;
2176 }
2177 }
2178
2179 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2180 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2181 *heightp = h;
2182 }
2183
2184 /*
2185 * Remember which glyph the mouse is over.
2186 */
2187
2188 void
2189 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2190 {
2191 Lisp_Object window;
2192 struct window *w;
2193 struct glyph_row *r, *gr, *end_row;
2194 enum window_part part;
2195 enum glyph_row_area area;
2196 int x, y, width, height;
2197
2198 /* Try to determine frame pixel position and size of the glyph under
2199 frame pixel coordinates X/Y on frame F. */
2200
2201 if (!f->glyphs_initialized_p
2202 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2203 NILP (window)))
2204 {
2205 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2206 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2207 goto virtual_glyph;
2208 }
2209
2210 w = XWINDOW (window);
2211 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2212 height = WINDOW_FRAME_LINE_HEIGHT (w);
2213
2214 x = window_relative_x_coord (w, part, gx);
2215 y = gy - WINDOW_TOP_EDGE_Y (w);
2216
2217 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2218 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2219
2220 if (w->pseudo_window_p)
2221 {
2222 area = TEXT_AREA;
2223 part = ON_MODE_LINE; /* Don't adjust margin. */
2224 goto text_glyph;
2225 }
2226
2227 switch (part)
2228 {
2229 case ON_LEFT_MARGIN:
2230 area = LEFT_MARGIN_AREA;
2231 goto text_glyph;
2232
2233 case ON_RIGHT_MARGIN:
2234 area = RIGHT_MARGIN_AREA;
2235 goto text_glyph;
2236
2237 case ON_HEADER_LINE:
2238 case ON_MODE_LINE:
2239 gr = (part == ON_HEADER_LINE
2240 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2241 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2242 gy = gr->y;
2243 area = TEXT_AREA;
2244 goto text_glyph_row_found;
2245
2246 case ON_TEXT:
2247 area = TEXT_AREA;
2248
2249 text_glyph:
2250 gr = 0; gy = 0;
2251 for (; r <= end_row && r->enabled_p; ++r)
2252 if (r->y + r->height > y)
2253 {
2254 gr = r; gy = r->y;
2255 break;
2256 }
2257
2258 text_glyph_row_found:
2259 if (gr && gy <= y)
2260 {
2261 struct glyph *g = gr->glyphs[area];
2262 struct glyph *end = g + gr->used[area];
2263
2264 height = gr->height;
2265 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2266 if (gx + g->pixel_width > x)
2267 break;
2268
2269 if (g < end)
2270 {
2271 if (g->type == IMAGE_GLYPH)
2272 {
2273 /* Don't remember when mouse is over image, as
2274 image may have hot-spots. */
2275 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2276 return;
2277 }
2278 width = g->pixel_width;
2279 }
2280 else
2281 {
2282 /* Use nominal char spacing at end of line. */
2283 x -= gx;
2284 gx += (x / width) * width;
2285 }
2286
2287 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2288 gx += window_box_left_offset (w, area);
2289 }
2290 else
2291 {
2292 /* Use nominal line height at end of window. */
2293 gx = (x / width) * width;
2294 y -= gy;
2295 gy += (y / height) * height;
2296 }
2297 break;
2298
2299 case ON_LEFT_FRINGE:
2300 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2301 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2302 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2303 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2304 goto row_glyph;
2305
2306 case ON_RIGHT_FRINGE:
2307 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2308 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2309 : window_box_right_offset (w, TEXT_AREA));
2310 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2311 goto row_glyph;
2312
2313 case ON_SCROLL_BAR:
2314 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2315 ? 0
2316 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2317 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2318 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2319 : 0)));
2320 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2321
2322 row_glyph:
2323 gr = 0, gy = 0;
2324 for (; r <= end_row && r->enabled_p; ++r)
2325 if (r->y + r->height > y)
2326 {
2327 gr = r; gy = r->y;
2328 break;
2329 }
2330
2331 if (gr && gy <= y)
2332 height = gr->height;
2333 else
2334 {
2335 /* Use nominal line height at end of window. */
2336 y -= gy;
2337 gy += (y / height) * height;
2338 }
2339 break;
2340
2341 default:
2342 ;
2343 virtual_glyph:
2344 /* If there is no glyph under the mouse, then we divide the screen
2345 into a grid of the smallest glyph in the frame, and use that
2346 as our "glyph". */
2347
2348 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2349 round down even for negative values. */
2350 if (gx < 0)
2351 gx -= width - 1;
2352 if (gy < 0)
2353 gy -= height - 1;
2354
2355 gx = (gx / width) * width;
2356 gy = (gy / height) * height;
2357
2358 goto store_rect;
2359 }
2360
2361 gx += WINDOW_LEFT_EDGE_X (w);
2362 gy += WINDOW_TOP_EDGE_Y (w);
2363
2364 store_rect:
2365 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2366
2367 /* Visible feedback for debugging. */
2368 #if 0
2369 #if HAVE_X_WINDOWS
2370 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2371 f->output_data.x->normal_gc,
2372 gx, gy, width, height);
2373 #endif
2374 #endif
2375 }
2376
2377
2378 #endif /* HAVE_WINDOW_SYSTEM */
2379
2380 \f
2381 /***********************************************************************
2382 Lisp form evaluation
2383 ***********************************************************************/
2384
2385 /* Error handler for safe_eval and safe_call. */
2386
2387 static Lisp_Object
2388 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2389 {
2390 add_to_log ("Error during redisplay: %S signaled %S",
2391 Flist (nargs, args), arg);
2392 return Qnil;
2393 }
2394
2395 /* Call function FUNC with the rest of NARGS - 1 arguments
2396 following. Return the result, or nil if something went
2397 wrong. Prevent redisplay during the evaluation. */
2398
2399 Lisp_Object
2400 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2401 {
2402 Lisp_Object val;
2403
2404 if (inhibit_eval_during_redisplay)
2405 val = Qnil;
2406 else
2407 {
2408 va_list ap;
2409 ptrdiff_t i;
2410 ptrdiff_t count = SPECPDL_INDEX ();
2411 struct gcpro gcpro1;
2412 Lisp_Object *args = alloca (nargs * word_size);
2413
2414 args[0] = func;
2415 va_start (ap, func);
2416 for (i = 1; i < nargs; i++)
2417 args[i] = va_arg (ap, Lisp_Object);
2418 va_end (ap);
2419
2420 GCPRO1 (args[0]);
2421 gcpro1.nvars = nargs;
2422 specbind (Qinhibit_redisplay, Qt);
2423 /* Use Qt to ensure debugger does not run,
2424 so there is no possibility of wanting to redisplay. */
2425 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2426 safe_eval_handler);
2427 UNGCPRO;
2428 val = unbind_to (count, val);
2429 }
2430
2431 return val;
2432 }
2433
2434
2435 /* Call function FN with one argument ARG.
2436 Return the result, or nil if something went wrong. */
2437
2438 Lisp_Object
2439 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2440 {
2441 return safe_call (2, fn, arg);
2442 }
2443
2444 static Lisp_Object Qeval;
2445
2446 Lisp_Object
2447 safe_eval (Lisp_Object sexpr)
2448 {
2449 return safe_call1 (Qeval, sexpr);
2450 }
2451
2452 /* Call function FN with two arguments ARG1 and ARG2.
2453 Return the result, or nil if something went wrong. */
2454
2455 Lisp_Object
2456 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2457 {
2458 return safe_call (3, fn, arg1, arg2);
2459 }
2460
2461
2462 \f
2463 /***********************************************************************
2464 Debugging
2465 ***********************************************************************/
2466
2467 #if 0
2468
2469 /* Define CHECK_IT to perform sanity checks on iterators.
2470 This is for debugging. It is too slow to do unconditionally. */
2471
2472 static void
2473 check_it (struct it *it)
2474 {
2475 if (it->method == GET_FROM_STRING)
2476 {
2477 eassert (STRINGP (it->string));
2478 eassert (IT_STRING_CHARPOS (*it) >= 0);
2479 }
2480 else
2481 {
2482 eassert (IT_STRING_CHARPOS (*it) < 0);
2483 if (it->method == GET_FROM_BUFFER)
2484 {
2485 /* Check that character and byte positions agree. */
2486 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2487 }
2488 }
2489
2490 if (it->dpvec)
2491 eassert (it->current.dpvec_index >= 0);
2492 else
2493 eassert (it->current.dpvec_index < 0);
2494 }
2495
2496 #define CHECK_IT(IT) check_it ((IT))
2497
2498 #else /* not 0 */
2499
2500 #define CHECK_IT(IT) (void) 0
2501
2502 #endif /* not 0 */
2503
2504
2505 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2506
2507 /* Check that the window end of window W is what we expect it
2508 to be---the last row in the current matrix displaying text. */
2509
2510 static void
2511 check_window_end (struct window *w)
2512 {
2513 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2514 {
2515 struct glyph_row *row;
2516 eassert ((row = MATRIX_ROW (w->current_matrix,
2517 XFASTINT (w->window_end_vpos)),
2518 !row->enabled_p
2519 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2520 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2521 }
2522 }
2523
2524 #define CHECK_WINDOW_END(W) check_window_end ((W))
2525
2526 #else
2527
2528 #define CHECK_WINDOW_END(W) (void) 0
2529
2530 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2531
2532 /* Return mark position if current buffer has the region of non-zero length,
2533 or -1 otherwise. */
2534
2535 static ptrdiff_t
2536 markpos_of_region (void)
2537 {
2538 if (!NILP (Vtransient_mark_mode)
2539 && !NILP (BVAR (current_buffer, mark_active))
2540 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2541 {
2542 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2543
2544 if (markpos != PT)
2545 return markpos;
2546 }
2547 return -1;
2548 }
2549
2550 /***********************************************************************
2551 Iterator initialization
2552 ***********************************************************************/
2553
2554 /* Initialize IT for displaying current_buffer in window W, starting
2555 at character position CHARPOS. CHARPOS < 0 means that no buffer
2556 position is specified which is useful when the iterator is assigned
2557 a position later. BYTEPOS is the byte position corresponding to
2558 CHARPOS.
2559
2560 If ROW is not null, calls to produce_glyphs with IT as parameter
2561 will produce glyphs in that row.
2562
2563 BASE_FACE_ID is the id of a base face to use. It must be one of
2564 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2565 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2566 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2567
2568 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2569 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2570 will be initialized to use the corresponding mode line glyph row of
2571 the desired matrix of W. */
2572
2573 void
2574 init_iterator (struct it *it, struct window *w,
2575 ptrdiff_t charpos, ptrdiff_t bytepos,
2576 struct glyph_row *row, enum face_id base_face_id)
2577 {
2578 ptrdiff_t markpos;
2579 enum face_id remapped_base_face_id = base_face_id;
2580
2581 /* Some precondition checks. */
2582 eassert (w != NULL && it != NULL);
2583 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2584 && charpos <= ZV));
2585
2586 /* If face attributes have been changed since the last redisplay,
2587 free realized faces now because they depend on face definitions
2588 that might have changed. Don't free faces while there might be
2589 desired matrices pending which reference these faces. */
2590 if (face_change_count && !inhibit_free_realized_faces)
2591 {
2592 face_change_count = 0;
2593 free_all_realized_faces (Qnil);
2594 }
2595
2596 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2597 if (! NILP (Vface_remapping_alist))
2598 remapped_base_face_id
2599 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2600
2601 /* Use one of the mode line rows of W's desired matrix if
2602 appropriate. */
2603 if (row == NULL)
2604 {
2605 if (base_face_id == MODE_LINE_FACE_ID
2606 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2607 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2608 else if (base_face_id == HEADER_LINE_FACE_ID)
2609 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2610 }
2611
2612 /* Clear IT. */
2613 memset (it, 0, sizeof *it);
2614 it->current.overlay_string_index = -1;
2615 it->current.dpvec_index = -1;
2616 it->base_face_id = remapped_base_face_id;
2617 it->string = Qnil;
2618 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2619 it->paragraph_embedding = L2R;
2620 it->bidi_it.string.lstring = Qnil;
2621 it->bidi_it.string.s = NULL;
2622 it->bidi_it.string.bufpos = 0;
2623
2624 /* The window in which we iterate over current_buffer: */
2625 XSETWINDOW (it->window, w);
2626 it->w = w;
2627 it->f = XFRAME (w->frame);
2628
2629 it->cmp_it.id = -1;
2630
2631 /* Extra space between lines (on window systems only). */
2632 if (base_face_id == DEFAULT_FACE_ID
2633 && FRAME_WINDOW_P (it->f))
2634 {
2635 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2636 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2637 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2638 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2639 * FRAME_LINE_HEIGHT (it->f));
2640 else if (it->f->extra_line_spacing > 0)
2641 it->extra_line_spacing = it->f->extra_line_spacing;
2642 it->max_extra_line_spacing = 0;
2643 }
2644
2645 /* If realized faces have been removed, e.g. because of face
2646 attribute changes of named faces, recompute them. When running
2647 in batch mode, the face cache of the initial frame is null. If
2648 we happen to get called, make a dummy face cache. */
2649 if (FRAME_FACE_CACHE (it->f) == NULL)
2650 init_frame_faces (it->f);
2651 if (FRAME_FACE_CACHE (it->f)->used == 0)
2652 recompute_basic_faces (it->f);
2653
2654 /* Current value of the `slice', `space-width', and 'height' properties. */
2655 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2656 it->space_width = Qnil;
2657 it->font_height = Qnil;
2658 it->override_ascent = -1;
2659
2660 /* Are control characters displayed as `^C'? */
2661 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2662
2663 /* -1 means everything between a CR and the following line end
2664 is invisible. >0 means lines indented more than this value are
2665 invisible. */
2666 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2667 ? (clip_to_bounds
2668 (-1, XINT (BVAR (current_buffer, selective_display)),
2669 PTRDIFF_MAX))
2670 : (!NILP (BVAR (current_buffer, selective_display))
2671 ? -1 : 0));
2672 it->selective_display_ellipsis_p
2673 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2674
2675 /* Display table to use. */
2676 it->dp = window_display_table (w);
2677
2678 /* Are multibyte characters enabled in current_buffer? */
2679 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2680
2681 /* If visible region is of non-zero length, set IT->region_beg_charpos
2682 and IT->region_end_charpos to the start and end of a visible region
2683 in window IT->w. Set both to -1 to indicate no region. */
2684 markpos = markpos_of_region ();
2685 if (markpos >= 0
2686 /* Maybe highlight only in selected window. */
2687 && (/* Either show region everywhere. */
2688 highlight_nonselected_windows
2689 /* Or show region in the selected window. */
2690 || w == XWINDOW (selected_window)
2691 /* Or show the region if we are in the mini-buffer and W is
2692 the window the mini-buffer refers to. */
2693 || (MINI_WINDOW_P (XWINDOW (selected_window))
2694 && WINDOWP (minibuf_selected_window)
2695 && w == XWINDOW (minibuf_selected_window))))
2696 {
2697 it->region_beg_charpos = min (PT, markpos);
2698 it->region_end_charpos = max (PT, markpos);
2699 }
2700 else
2701 it->region_beg_charpos = it->region_end_charpos = -1;
2702
2703 /* Get the position at which the redisplay_end_trigger hook should
2704 be run, if it is to be run at all. */
2705 if (MARKERP (w->redisplay_end_trigger)
2706 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2707 it->redisplay_end_trigger_charpos
2708 = marker_position (w->redisplay_end_trigger);
2709 else if (INTEGERP (w->redisplay_end_trigger))
2710 it->redisplay_end_trigger_charpos =
2711 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2712
2713 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2714
2715 /* Are lines in the display truncated? */
2716 if (base_face_id != DEFAULT_FACE_ID
2717 || it->w->hscroll
2718 || (! WINDOW_FULL_WIDTH_P (it->w)
2719 && ((!NILP (Vtruncate_partial_width_windows)
2720 && !INTEGERP (Vtruncate_partial_width_windows))
2721 || (INTEGERP (Vtruncate_partial_width_windows)
2722 && (WINDOW_TOTAL_COLS (it->w)
2723 < XINT (Vtruncate_partial_width_windows))))))
2724 it->line_wrap = TRUNCATE;
2725 else if (NILP (BVAR (current_buffer, truncate_lines)))
2726 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2727 ? WINDOW_WRAP : WORD_WRAP;
2728 else
2729 it->line_wrap = TRUNCATE;
2730
2731 /* Get dimensions of truncation and continuation glyphs. These are
2732 displayed as fringe bitmaps under X, but we need them for such
2733 frames when the fringes are turned off. But leave the dimensions
2734 zero for tooltip frames, as these glyphs look ugly there and also
2735 sabotage calculations of tooltip dimensions in x-show-tip. */
2736 #ifdef HAVE_WINDOW_SYSTEM
2737 if (!(FRAME_WINDOW_P (it->f)
2738 && FRAMEP (tip_frame)
2739 && it->f == XFRAME (tip_frame)))
2740 #endif
2741 {
2742 if (it->line_wrap == TRUNCATE)
2743 {
2744 /* We will need the truncation glyph. */
2745 eassert (it->glyph_row == NULL);
2746 produce_special_glyphs (it, IT_TRUNCATION);
2747 it->truncation_pixel_width = it->pixel_width;
2748 }
2749 else
2750 {
2751 /* We will need the continuation glyph. */
2752 eassert (it->glyph_row == NULL);
2753 produce_special_glyphs (it, IT_CONTINUATION);
2754 it->continuation_pixel_width = it->pixel_width;
2755 }
2756 }
2757
2758 /* Reset these values to zero because the produce_special_glyphs
2759 above has changed them. */
2760 it->pixel_width = it->ascent = it->descent = 0;
2761 it->phys_ascent = it->phys_descent = 0;
2762
2763 /* Set this after getting the dimensions of truncation and
2764 continuation glyphs, so that we don't produce glyphs when calling
2765 produce_special_glyphs, above. */
2766 it->glyph_row = row;
2767 it->area = TEXT_AREA;
2768
2769 /* Forget any previous info about this row being reversed. */
2770 if (it->glyph_row)
2771 it->glyph_row->reversed_p = 0;
2772
2773 /* Get the dimensions of the display area. The display area
2774 consists of the visible window area plus a horizontally scrolled
2775 part to the left of the window. All x-values are relative to the
2776 start of this total display area. */
2777 if (base_face_id != DEFAULT_FACE_ID)
2778 {
2779 /* Mode lines, menu bar in terminal frames. */
2780 it->first_visible_x = 0;
2781 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2782 }
2783 else
2784 {
2785 it->first_visible_x =
2786 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2787 it->last_visible_x = (it->first_visible_x
2788 + window_box_width (w, TEXT_AREA));
2789
2790 /* If we truncate lines, leave room for the truncation glyph(s) at
2791 the right margin. Otherwise, leave room for the continuation
2792 glyph(s). Done only if the window has no fringes. Since we
2793 don't know at this point whether there will be any R2L lines in
2794 the window, we reserve space for truncation/continuation glyphs
2795 even if only one of the fringes is absent. */
2796 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2797 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2798 {
2799 if (it->line_wrap == TRUNCATE)
2800 it->last_visible_x -= it->truncation_pixel_width;
2801 else
2802 it->last_visible_x -= it->continuation_pixel_width;
2803 }
2804
2805 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2806 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2807 }
2808
2809 /* Leave room for a border glyph. */
2810 if (!FRAME_WINDOW_P (it->f)
2811 && !WINDOW_RIGHTMOST_P (it->w))
2812 it->last_visible_x -= 1;
2813
2814 it->last_visible_y = window_text_bottom_y (w);
2815
2816 /* For mode lines and alike, arrange for the first glyph having a
2817 left box line if the face specifies a box. */
2818 if (base_face_id != DEFAULT_FACE_ID)
2819 {
2820 struct face *face;
2821
2822 it->face_id = remapped_base_face_id;
2823
2824 /* If we have a boxed mode line, make the first character appear
2825 with a left box line. */
2826 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2827 if (face->box != FACE_NO_BOX)
2828 it->start_of_box_run_p = 1;
2829 }
2830
2831 /* If a buffer position was specified, set the iterator there,
2832 getting overlays and face properties from that position. */
2833 if (charpos >= BUF_BEG (current_buffer))
2834 {
2835 it->end_charpos = ZV;
2836 eassert (charpos == BYTE_TO_CHAR (bytepos));
2837 IT_CHARPOS (*it) = charpos;
2838 IT_BYTEPOS (*it) = bytepos;
2839
2840 /* We will rely on `reseat' to set this up properly, via
2841 handle_face_prop. */
2842 it->face_id = it->base_face_id;
2843
2844 it->start = it->current;
2845 /* Do we need to reorder bidirectional text? Not if this is a
2846 unibyte buffer: by definition, none of the single-byte
2847 characters are strong R2L, so no reordering is needed. And
2848 bidi.c doesn't support unibyte buffers anyway. Also, don't
2849 reorder while we are loading loadup.el, since the tables of
2850 character properties needed for reordering are not yet
2851 available. */
2852 it->bidi_p =
2853 NILP (Vpurify_flag)
2854 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2855 && it->multibyte_p;
2856
2857 /* If we are to reorder bidirectional text, init the bidi
2858 iterator. */
2859 if (it->bidi_p)
2860 {
2861 /* Note the paragraph direction that this buffer wants to
2862 use. */
2863 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2864 Qleft_to_right))
2865 it->paragraph_embedding = L2R;
2866 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2867 Qright_to_left))
2868 it->paragraph_embedding = R2L;
2869 else
2870 it->paragraph_embedding = NEUTRAL_DIR;
2871 bidi_unshelve_cache (NULL, 0);
2872 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2873 &it->bidi_it);
2874 }
2875
2876 /* Compute faces etc. */
2877 reseat (it, it->current.pos, 1);
2878 }
2879
2880 CHECK_IT (it);
2881 }
2882
2883
2884 /* Initialize IT for the display of window W with window start POS. */
2885
2886 void
2887 start_display (struct it *it, struct window *w, struct text_pos pos)
2888 {
2889 struct glyph_row *row;
2890 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2891
2892 row = w->desired_matrix->rows + first_vpos;
2893 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2894 it->first_vpos = first_vpos;
2895
2896 /* Don't reseat to previous visible line start if current start
2897 position is in a string or image. */
2898 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2899 {
2900 int start_at_line_beg_p;
2901 int first_y = it->current_y;
2902
2903 /* If window start is not at a line start, skip forward to POS to
2904 get the correct continuation lines width. */
2905 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2906 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2907 if (!start_at_line_beg_p)
2908 {
2909 int new_x;
2910
2911 reseat_at_previous_visible_line_start (it);
2912 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2913
2914 new_x = it->current_x + it->pixel_width;
2915
2916 /* If lines are continued, this line may end in the middle
2917 of a multi-glyph character (e.g. a control character
2918 displayed as \003, or in the middle of an overlay
2919 string). In this case move_it_to above will not have
2920 taken us to the start of the continuation line but to the
2921 end of the continued line. */
2922 if (it->current_x > 0
2923 && it->line_wrap != TRUNCATE /* Lines are continued. */
2924 && (/* And glyph doesn't fit on the line. */
2925 new_x > it->last_visible_x
2926 /* Or it fits exactly and we're on a window
2927 system frame. */
2928 || (new_x == it->last_visible_x
2929 && FRAME_WINDOW_P (it->f)
2930 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2931 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2932 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2933 {
2934 if ((it->current.dpvec_index >= 0
2935 || it->current.overlay_string_index >= 0)
2936 /* If we are on a newline from a display vector or
2937 overlay string, then we are already at the end of
2938 a screen line; no need to go to the next line in
2939 that case, as this line is not really continued.
2940 (If we do go to the next line, C-e will not DTRT.) */
2941 && it->c != '\n')
2942 {
2943 set_iterator_to_next (it, 1);
2944 move_it_in_display_line_to (it, -1, -1, 0);
2945 }
2946
2947 it->continuation_lines_width += it->current_x;
2948 }
2949 /* If the character at POS is displayed via a display
2950 vector, move_it_to above stops at the final glyph of
2951 IT->dpvec. To make the caller redisplay that character
2952 again (a.k.a. start at POS), we need to reset the
2953 dpvec_index to the beginning of IT->dpvec. */
2954 else if (it->current.dpvec_index >= 0)
2955 it->current.dpvec_index = 0;
2956
2957 /* We're starting a new display line, not affected by the
2958 height of the continued line, so clear the appropriate
2959 fields in the iterator structure. */
2960 it->max_ascent = it->max_descent = 0;
2961 it->max_phys_ascent = it->max_phys_descent = 0;
2962
2963 it->current_y = first_y;
2964 it->vpos = 0;
2965 it->current_x = it->hpos = 0;
2966 }
2967 }
2968 }
2969
2970
2971 /* Return 1 if POS is a position in ellipses displayed for invisible
2972 text. W is the window we display, for text property lookup. */
2973
2974 static int
2975 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2976 {
2977 Lisp_Object prop, window;
2978 int ellipses_p = 0;
2979 ptrdiff_t charpos = CHARPOS (pos->pos);
2980
2981 /* If POS specifies a position in a display vector, this might
2982 be for an ellipsis displayed for invisible text. We won't
2983 get the iterator set up for delivering that ellipsis unless
2984 we make sure that it gets aware of the invisible text. */
2985 if (pos->dpvec_index >= 0
2986 && pos->overlay_string_index < 0
2987 && CHARPOS (pos->string_pos) < 0
2988 && charpos > BEGV
2989 && (XSETWINDOW (window, w),
2990 prop = Fget_char_property (make_number (charpos),
2991 Qinvisible, window),
2992 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2993 {
2994 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2995 window);
2996 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2997 }
2998
2999 return ellipses_p;
3000 }
3001
3002
3003 /* Initialize IT for stepping through current_buffer in window W,
3004 starting at position POS that includes overlay string and display
3005 vector/ control character translation position information. Value
3006 is zero if there are overlay strings with newlines at POS. */
3007
3008 static int
3009 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3010 {
3011 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3012 int i, overlay_strings_with_newlines = 0;
3013
3014 /* If POS specifies a position in a display vector, this might
3015 be for an ellipsis displayed for invisible text. We won't
3016 get the iterator set up for delivering that ellipsis unless
3017 we make sure that it gets aware of the invisible text. */
3018 if (in_ellipses_for_invisible_text_p (pos, w))
3019 {
3020 --charpos;
3021 bytepos = 0;
3022 }
3023
3024 /* Keep in mind: the call to reseat in init_iterator skips invisible
3025 text, so we might end up at a position different from POS. This
3026 is only a problem when POS is a row start after a newline and an
3027 overlay starts there with an after-string, and the overlay has an
3028 invisible property. Since we don't skip invisible text in
3029 display_line and elsewhere immediately after consuming the
3030 newline before the row start, such a POS will not be in a string,
3031 but the call to init_iterator below will move us to the
3032 after-string. */
3033 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3034
3035 /* This only scans the current chunk -- it should scan all chunks.
3036 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3037 to 16 in 22.1 to make this a lesser problem. */
3038 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3039 {
3040 const char *s = SSDATA (it->overlay_strings[i]);
3041 const char *e = s + SBYTES (it->overlay_strings[i]);
3042
3043 while (s < e && *s != '\n')
3044 ++s;
3045
3046 if (s < e)
3047 {
3048 overlay_strings_with_newlines = 1;
3049 break;
3050 }
3051 }
3052
3053 /* If position is within an overlay string, set up IT to the right
3054 overlay string. */
3055 if (pos->overlay_string_index >= 0)
3056 {
3057 int relative_index;
3058
3059 /* If the first overlay string happens to have a `display'
3060 property for an image, the iterator will be set up for that
3061 image, and we have to undo that setup first before we can
3062 correct the overlay string index. */
3063 if (it->method == GET_FROM_IMAGE)
3064 pop_it (it);
3065
3066 /* We already have the first chunk of overlay strings in
3067 IT->overlay_strings. Load more until the one for
3068 pos->overlay_string_index is in IT->overlay_strings. */
3069 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3070 {
3071 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3072 it->current.overlay_string_index = 0;
3073 while (n--)
3074 {
3075 load_overlay_strings (it, 0);
3076 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3077 }
3078 }
3079
3080 it->current.overlay_string_index = pos->overlay_string_index;
3081 relative_index = (it->current.overlay_string_index
3082 % OVERLAY_STRING_CHUNK_SIZE);
3083 it->string = it->overlay_strings[relative_index];
3084 eassert (STRINGP (it->string));
3085 it->current.string_pos = pos->string_pos;
3086 it->method = GET_FROM_STRING;
3087 it->end_charpos = SCHARS (it->string);
3088 /* Set up the bidi iterator for this overlay string. */
3089 if (it->bidi_p)
3090 {
3091 it->bidi_it.string.lstring = it->string;
3092 it->bidi_it.string.s = NULL;
3093 it->bidi_it.string.schars = SCHARS (it->string);
3094 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3095 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3096 it->bidi_it.string.unibyte = !it->multibyte_p;
3097 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3098 FRAME_WINDOW_P (it->f), &it->bidi_it);
3099
3100 /* Synchronize the state of the bidi iterator with
3101 pos->string_pos. For any string position other than
3102 zero, this will be done automagically when we resume
3103 iteration over the string and get_visually_first_element
3104 is called. But if string_pos is zero, and the string is
3105 to be reordered for display, we need to resync manually,
3106 since it could be that the iteration state recorded in
3107 pos ended at string_pos of 0 moving backwards in string. */
3108 if (CHARPOS (pos->string_pos) == 0)
3109 {
3110 get_visually_first_element (it);
3111 if (IT_STRING_CHARPOS (*it) != 0)
3112 do {
3113 /* Paranoia. */
3114 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3115 bidi_move_to_visually_next (&it->bidi_it);
3116 } while (it->bidi_it.charpos != 0);
3117 }
3118 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3119 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3120 }
3121 }
3122
3123 if (CHARPOS (pos->string_pos) >= 0)
3124 {
3125 /* Recorded position is not in an overlay string, but in another
3126 string. This can only be a string from a `display' property.
3127 IT should already be filled with that string. */
3128 it->current.string_pos = pos->string_pos;
3129 eassert (STRINGP (it->string));
3130 if (it->bidi_p)
3131 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3132 FRAME_WINDOW_P (it->f), &it->bidi_it);
3133 }
3134
3135 /* Restore position in display vector translations, control
3136 character translations or ellipses. */
3137 if (pos->dpvec_index >= 0)
3138 {
3139 if (it->dpvec == NULL)
3140 get_next_display_element (it);
3141 eassert (it->dpvec && it->current.dpvec_index == 0);
3142 it->current.dpvec_index = pos->dpvec_index;
3143 }
3144
3145 CHECK_IT (it);
3146 return !overlay_strings_with_newlines;
3147 }
3148
3149
3150 /* Initialize IT for stepping through current_buffer in window W
3151 starting at ROW->start. */
3152
3153 static void
3154 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3155 {
3156 init_from_display_pos (it, w, &row->start);
3157 it->start = row->start;
3158 it->continuation_lines_width = row->continuation_lines_width;
3159 CHECK_IT (it);
3160 }
3161
3162
3163 /* Initialize IT for stepping through current_buffer in window W
3164 starting in the line following ROW, i.e. starting at ROW->end.
3165 Value is zero if there are overlay strings with newlines at ROW's
3166 end position. */
3167
3168 static int
3169 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3170 {
3171 int success = 0;
3172
3173 if (init_from_display_pos (it, w, &row->end))
3174 {
3175 if (row->continued_p)
3176 it->continuation_lines_width
3177 = row->continuation_lines_width + row->pixel_width;
3178 CHECK_IT (it);
3179 success = 1;
3180 }
3181
3182 return success;
3183 }
3184
3185
3186
3187 \f
3188 /***********************************************************************
3189 Text properties
3190 ***********************************************************************/
3191
3192 /* Called when IT reaches IT->stop_charpos. Handle text property and
3193 overlay changes. Set IT->stop_charpos to the next position where
3194 to stop. */
3195
3196 static void
3197 handle_stop (struct it *it)
3198 {
3199 enum prop_handled handled;
3200 int handle_overlay_change_p;
3201 struct props *p;
3202
3203 it->dpvec = NULL;
3204 it->current.dpvec_index = -1;
3205 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3206 it->ignore_overlay_strings_at_pos_p = 0;
3207 it->ellipsis_p = 0;
3208
3209 /* Use face of preceding text for ellipsis (if invisible) */
3210 if (it->selective_display_ellipsis_p)
3211 it->saved_face_id = it->face_id;
3212
3213 do
3214 {
3215 handled = HANDLED_NORMALLY;
3216
3217 /* Call text property handlers. */
3218 for (p = it_props; p->handler; ++p)
3219 {
3220 handled = p->handler (it);
3221
3222 if (handled == HANDLED_RECOMPUTE_PROPS)
3223 break;
3224 else if (handled == HANDLED_RETURN)
3225 {
3226 /* We still want to show before and after strings from
3227 overlays even if the actual buffer text is replaced. */
3228 if (!handle_overlay_change_p
3229 || it->sp > 1
3230 /* Don't call get_overlay_strings_1 if we already
3231 have overlay strings loaded, because doing so
3232 will load them again and push the iterator state
3233 onto the stack one more time, which is not
3234 expected by the rest of the code that processes
3235 overlay strings. */
3236 || (it->current.overlay_string_index < 0
3237 ? !get_overlay_strings_1 (it, 0, 0)
3238 : 0))
3239 {
3240 if (it->ellipsis_p)
3241 setup_for_ellipsis (it, 0);
3242 /* When handling a display spec, we might load an
3243 empty string. In that case, discard it here. We
3244 used to discard it in handle_single_display_spec,
3245 but that causes get_overlay_strings_1, above, to
3246 ignore overlay strings that we must check. */
3247 if (STRINGP (it->string) && !SCHARS (it->string))
3248 pop_it (it);
3249 return;
3250 }
3251 else if (STRINGP (it->string) && !SCHARS (it->string))
3252 pop_it (it);
3253 else
3254 {
3255 it->ignore_overlay_strings_at_pos_p = 1;
3256 it->string_from_display_prop_p = 0;
3257 it->from_disp_prop_p = 0;
3258 handle_overlay_change_p = 0;
3259 }
3260 handled = HANDLED_RECOMPUTE_PROPS;
3261 break;
3262 }
3263 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3264 handle_overlay_change_p = 0;
3265 }
3266
3267 if (handled != HANDLED_RECOMPUTE_PROPS)
3268 {
3269 /* Don't check for overlay strings below when set to deliver
3270 characters from a display vector. */
3271 if (it->method == GET_FROM_DISPLAY_VECTOR)
3272 handle_overlay_change_p = 0;
3273
3274 /* Handle overlay changes.
3275 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3276 if it finds overlays. */
3277 if (handle_overlay_change_p)
3278 handled = handle_overlay_change (it);
3279 }
3280
3281 if (it->ellipsis_p)
3282 {
3283 setup_for_ellipsis (it, 0);
3284 break;
3285 }
3286 }
3287 while (handled == HANDLED_RECOMPUTE_PROPS);
3288
3289 /* Determine where to stop next. */
3290 if (handled == HANDLED_NORMALLY)
3291 compute_stop_pos (it);
3292 }
3293
3294
3295 /* Compute IT->stop_charpos from text property and overlay change
3296 information for IT's current position. */
3297
3298 static void
3299 compute_stop_pos (struct it *it)
3300 {
3301 register INTERVAL iv, next_iv;
3302 Lisp_Object object, limit, position;
3303 ptrdiff_t charpos, bytepos;
3304
3305 if (STRINGP (it->string))
3306 {
3307 /* Strings are usually short, so don't limit the search for
3308 properties. */
3309 it->stop_charpos = it->end_charpos;
3310 object = it->string;
3311 limit = Qnil;
3312 charpos = IT_STRING_CHARPOS (*it);
3313 bytepos = IT_STRING_BYTEPOS (*it);
3314 }
3315 else
3316 {
3317 ptrdiff_t pos;
3318
3319 /* If end_charpos is out of range for some reason, such as a
3320 misbehaving display function, rationalize it (Bug#5984). */
3321 if (it->end_charpos > ZV)
3322 it->end_charpos = ZV;
3323 it->stop_charpos = it->end_charpos;
3324
3325 /* If next overlay change is in front of the current stop pos
3326 (which is IT->end_charpos), stop there. Note: value of
3327 next_overlay_change is point-max if no overlay change
3328 follows. */
3329 charpos = IT_CHARPOS (*it);
3330 bytepos = IT_BYTEPOS (*it);
3331 pos = next_overlay_change (charpos);
3332 if (pos < it->stop_charpos)
3333 it->stop_charpos = pos;
3334
3335 /* If showing the region, we have to stop at the region
3336 start or end because the face might change there. */
3337 if (it->region_beg_charpos > 0)
3338 {
3339 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3340 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3341 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3342 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3343 }
3344
3345 /* Set up variables for computing the stop position from text
3346 property changes. */
3347 XSETBUFFER (object, current_buffer);
3348 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3349 }
3350
3351 /* Get the interval containing IT's position. Value is a null
3352 interval if there isn't such an interval. */
3353 position = make_number (charpos);
3354 iv = validate_interval_range (object, &position, &position, 0);
3355 if (iv)
3356 {
3357 Lisp_Object values_here[LAST_PROP_IDX];
3358 struct props *p;
3359
3360 /* Get properties here. */
3361 for (p = it_props; p->handler; ++p)
3362 values_here[p->idx] = textget (iv->plist, *p->name);
3363
3364 /* Look for an interval following iv that has different
3365 properties. */
3366 for (next_iv = next_interval (iv);
3367 (next_iv
3368 && (NILP (limit)
3369 || XFASTINT (limit) > next_iv->position));
3370 next_iv = next_interval (next_iv))
3371 {
3372 for (p = it_props; p->handler; ++p)
3373 {
3374 Lisp_Object new_value;
3375
3376 new_value = textget (next_iv->plist, *p->name);
3377 if (!EQ (values_here[p->idx], new_value))
3378 break;
3379 }
3380
3381 if (p->handler)
3382 break;
3383 }
3384
3385 if (next_iv)
3386 {
3387 if (INTEGERP (limit)
3388 && next_iv->position >= XFASTINT (limit))
3389 /* No text property change up to limit. */
3390 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3391 else
3392 /* Text properties change in next_iv. */
3393 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3394 }
3395 }
3396
3397 if (it->cmp_it.id < 0)
3398 {
3399 ptrdiff_t stoppos = it->end_charpos;
3400
3401 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3402 stoppos = -1;
3403 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3404 stoppos, it->string);
3405 }
3406
3407 eassert (STRINGP (it->string)
3408 || (it->stop_charpos >= BEGV
3409 && it->stop_charpos >= IT_CHARPOS (*it)));
3410 }
3411
3412
3413 /* Return the position of the next overlay change after POS in
3414 current_buffer. Value is point-max if no overlay change
3415 follows. This is like `next-overlay-change' but doesn't use
3416 xmalloc. */
3417
3418 static ptrdiff_t
3419 next_overlay_change (ptrdiff_t pos)
3420 {
3421 ptrdiff_t i, noverlays;
3422 ptrdiff_t endpos;
3423 Lisp_Object *overlays;
3424
3425 /* Get all overlays at the given position. */
3426 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3427
3428 /* If any of these overlays ends before endpos,
3429 use its ending point instead. */
3430 for (i = 0; i < noverlays; ++i)
3431 {
3432 Lisp_Object oend;
3433 ptrdiff_t oendpos;
3434
3435 oend = OVERLAY_END (overlays[i]);
3436 oendpos = OVERLAY_POSITION (oend);
3437 endpos = min (endpos, oendpos);
3438 }
3439
3440 return endpos;
3441 }
3442
3443 /* How many characters forward to search for a display property or
3444 display string. Searching too far forward makes the bidi display
3445 sluggish, especially in small windows. */
3446 #define MAX_DISP_SCAN 250
3447
3448 /* Return the character position of a display string at or after
3449 position specified by POSITION. If no display string exists at or
3450 after POSITION, return ZV. A display string is either an overlay
3451 with `display' property whose value is a string, or a `display'
3452 text property whose value is a string. STRING is data about the
3453 string to iterate; if STRING->lstring is nil, we are iterating a
3454 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3455 on a GUI frame. DISP_PROP is set to zero if we searched
3456 MAX_DISP_SCAN characters forward without finding any display
3457 strings, non-zero otherwise. It is set to 2 if the display string
3458 uses any kind of `(space ...)' spec that will produce a stretch of
3459 white space in the text area. */
3460 ptrdiff_t
3461 compute_display_string_pos (struct text_pos *position,
3462 struct bidi_string_data *string,
3463 int frame_window_p, int *disp_prop)
3464 {
3465 /* OBJECT = nil means current buffer. */
3466 Lisp_Object object =
3467 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3468 Lisp_Object pos, spec, limpos;
3469 int string_p = (string && (STRINGP (string->lstring) || string->s));
3470 ptrdiff_t eob = string_p ? string->schars : ZV;
3471 ptrdiff_t begb = string_p ? 0 : BEGV;
3472 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3473 ptrdiff_t lim =
3474 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3475 struct text_pos tpos;
3476 int rv = 0;
3477
3478 *disp_prop = 1;
3479
3480 if (charpos >= eob
3481 /* We don't support display properties whose values are strings
3482 that have display string properties. */
3483 || string->from_disp_str
3484 /* C strings cannot have display properties. */
3485 || (string->s && !STRINGP (object)))
3486 {
3487 *disp_prop = 0;
3488 return eob;
3489 }
3490
3491 /* If the character at CHARPOS is where the display string begins,
3492 return CHARPOS. */
3493 pos = make_number (charpos);
3494 if (STRINGP (object))
3495 bufpos = string->bufpos;
3496 else
3497 bufpos = charpos;
3498 tpos = *position;
3499 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3500 && (charpos <= begb
3501 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3502 object),
3503 spec))
3504 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3505 frame_window_p)))
3506 {
3507 if (rv == 2)
3508 *disp_prop = 2;
3509 return charpos;
3510 }
3511
3512 /* Look forward for the first character with a `display' property
3513 that will replace the underlying text when displayed. */
3514 limpos = make_number (lim);
3515 do {
3516 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3517 CHARPOS (tpos) = XFASTINT (pos);
3518 if (CHARPOS (tpos) >= lim)
3519 {
3520 *disp_prop = 0;
3521 break;
3522 }
3523 if (STRINGP (object))
3524 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3525 else
3526 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3527 spec = Fget_char_property (pos, Qdisplay, object);
3528 if (!STRINGP (object))
3529 bufpos = CHARPOS (tpos);
3530 } while (NILP (spec)
3531 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3532 bufpos, frame_window_p)));
3533 if (rv == 2)
3534 *disp_prop = 2;
3535
3536 return CHARPOS (tpos);
3537 }
3538
3539 /* Return the character position of the end of the display string that
3540 started at CHARPOS. If there's no display string at CHARPOS,
3541 return -1. A display string is either an overlay with `display'
3542 property whose value is a string or a `display' text property whose
3543 value is a string. */
3544 ptrdiff_t
3545 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3546 {
3547 /* OBJECT = nil means current buffer. */
3548 Lisp_Object object =
3549 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3550 Lisp_Object pos = make_number (charpos);
3551 ptrdiff_t eob =
3552 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3553
3554 if (charpos >= eob || (string->s && !STRINGP (object)))
3555 return eob;
3556
3557 /* It could happen that the display property or overlay was removed
3558 since we found it in compute_display_string_pos above. One way
3559 this can happen is if JIT font-lock was called (through
3560 handle_fontified_prop), and jit-lock-functions remove text
3561 properties or overlays from the portion of buffer that includes
3562 CHARPOS. Muse mode is known to do that, for example. In this
3563 case, we return -1 to the caller, to signal that no display
3564 string is actually present at CHARPOS. See bidi_fetch_char for
3565 how this is handled.
3566
3567 An alternative would be to never look for display properties past
3568 it->stop_charpos. But neither compute_display_string_pos nor
3569 bidi_fetch_char that calls it know or care where the next
3570 stop_charpos is. */
3571 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3572 return -1;
3573
3574 /* Look forward for the first character where the `display' property
3575 changes. */
3576 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3577
3578 return XFASTINT (pos);
3579 }
3580
3581
3582 \f
3583 /***********************************************************************
3584 Fontification
3585 ***********************************************************************/
3586
3587 /* Handle changes in the `fontified' property of the current buffer by
3588 calling hook functions from Qfontification_functions to fontify
3589 regions of text. */
3590
3591 static enum prop_handled
3592 handle_fontified_prop (struct it *it)
3593 {
3594 Lisp_Object prop, pos;
3595 enum prop_handled handled = HANDLED_NORMALLY;
3596
3597 if (!NILP (Vmemory_full))
3598 return handled;
3599
3600 /* Get the value of the `fontified' property at IT's current buffer
3601 position. (The `fontified' property doesn't have a special
3602 meaning in strings.) If the value is nil, call functions from
3603 Qfontification_functions. */
3604 if (!STRINGP (it->string)
3605 && it->s == NULL
3606 && !NILP (Vfontification_functions)
3607 && !NILP (Vrun_hooks)
3608 && (pos = make_number (IT_CHARPOS (*it)),
3609 prop = Fget_char_property (pos, Qfontified, Qnil),
3610 /* Ignore the special cased nil value always present at EOB since
3611 no amount of fontifying will be able to change it. */
3612 NILP (prop) && IT_CHARPOS (*it) < Z))
3613 {
3614 ptrdiff_t count = SPECPDL_INDEX ();
3615 Lisp_Object val;
3616 struct buffer *obuf = current_buffer;
3617 int begv = BEGV, zv = ZV;
3618 int old_clip_changed = current_buffer->clip_changed;
3619
3620 val = Vfontification_functions;
3621 specbind (Qfontification_functions, Qnil);
3622
3623 eassert (it->end_charpos == ZV);
3624
3625 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3626 safe_call1 (val, pos);
3627 else
3628 {
3629 Lisp_Object fns, fn;
3630 struct gcpro gcpro1, gcpro2;
3631
3632 fns = Qnil;
3633 GCPRO2 (val, fns);
3634
3635 for (; CONSP (val); val = XCDR (val))
3636 {
3637 fn = XCAR (val);
3638
3639 if (EQ (fn, Qt))
3640 {
3641 /* A value of t indicates this hook has a local
3642 binding; it means to run the global binding too.
3643 In a global value, t should not occur. If it
3644 does, we must ignore it to avoid an endless
3645 loop. */
3646 for (fns = Fdefault_value (Qfontification_functions);
3647 CONSP (fns);
3648 fns = XCDR (fns))
3649 {
3650 fn = XCAR (fns);
3651 if (!EQ (fn, Qt))
3652 safe_call1 (fn, pos);
3653 }
3654 }
3655 else
3656 safe_call1 (fn, pos);
3657 }
3658
3659 UNGCPRO;
3660 }
3661
3662 unbind_to (count, Qnil);
3663
3664 /* Fontification functions routinely call `save-restriction'.
3665 Normally, this tags clip_changed, which can confuse redisplay
3666 (see discussion in Bug#6671). Since we don't perform any
3667 special handling of fontification changes in the case where
3668 `save-restriction' isn't called, there's no point doing so in
3669 this case either. So, if the buffer's restrictions are
3670 actually left unchanged, reset clip_changed. */
3671 if (obuf == current_buffer)
3672 {
3673 if (begv == BEGV && zv == ZV)
3674 current_buffer->clip_changed = old_clip_changed;
3675 }
3676 /* There isn't much we can reasonably do to protect against
3677 misbehaving fontification, but here's a fig leaf. */
3678 else if (BUFFER_LIVE_P (obuf))
3679 set_buffer_internal_1 (obuf);
3680
3681 /* The fontification code may have added/removed text.
3682 It could do even a lot worse, but let's at least protect against
3683 the most obvious case where only the text past `pos' gets changed',
3684 as is/was done in grep.el where some escapes sequences are turned
3685 into face properties (bug#7876). */
3686 it->end_charpos = ZV;
3687
3688 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3689 something. This avoids an endless loop if they failed to
3690 fontify the text for which reason ever. */
3691 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3692 handled = HANDLED_RECOMPUTE_PROPS;
3693 }
3694
3695 return handled;
3696 }
3697
3698
3699 \f
3700 /***********************************************************************
3701 Faces
3702 ***********************************************************************/
3703
3704 /* Set up iterator IT from face properties at its current position.
3705 Called from handle_stop. */
3706
3707 static enum prop_handled
3708 handle_face_prop (struct it *it)
3709 {
3710 int new_face_id;
3711 ptrdiff_t next_stop;
3712
3713 if (!STRINGP (it->string))
3714 {
3715 new_face_id
3716 = face_at_buffer_position (it->w,
3717 IT_CHARPOS (*it),
3718 it->region_beg_charpos,
3719 it->region_end_charpos,
3720 &next_stop,
3721 (IT_CHARPOS (*it)
3722 + TEXT_PROP_DISTANCE_LIMIT),
3723 0, it->base_face_id);
3724
3725 /* Is this a start of a run of characters with box face?
3726 Caveat: this can be called for a freshly initialized
3727 iterator; face_id is -1 in this case. We know that the new
3728 face will not change until limit, i.e. if the new face has a
3729 box, all characters up to limit will have one. But, as
3730 usual, we don't know whether limit is really the end. */
3731 if (new_face_id != it->face_id)
3732 {
3733 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3734 /* If it->face_id is -1, old_face below will be NULL, see
3735 the definition of FACE_FROM_ID. This will happen if this
3736 is the initial call that gets the face. */
3737 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3738
3739 /* If the value of face_id of the iterator is -1, we have to
3740 look in front of IT's position and see whether there is a
3741 face there that's different from new_face_id. */
3742 if (!old_face && IT_CHARPOS (*it) > BEG)
3743 {
3744 int prev_face_id = face_before_it_pos (it);
3745
3746 old_face = FACE_FROM_ID (it->f, prev_face_id);
3747 }
3748
3749 /* If the new face has a box, but the old face does not,
3750 this is the start of a run of characters with box face,
3751 i.e. this character has a shadow on the left side. */
3752 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3753 && (old_face == NULL || !old_face->box));
3754 it->face_box_p = new_face->box != FACE_NO_BOX;
3755 }
3756 }
3757 else
3758 {
3759 int base_face_id;
3760 ptrdiff_t bufpos;
3761 int i;
3762 Lisp_Object from_overlay
3763 = (it->current.overlay_string_index >= 0
3764 ? it->string_overlays[it->current.overlay_string_index
3765 % OVERLAY_STRING_CHUNK_SIZE]
3766 : Qnil);
3767
3768 /* See if we got to this string directly or indirectly from
3769 an overlay property. That includes the before-string or
3770 after-string of an overlay, strings in display properties
3771 provided by an overlay, their text properties, etc.
3772
3773 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3774 if (! NILP (from_overlay))
3775 for (i = it->sp - 1; i >= 0; i--)
3776 {
3777 if (it->stack[i].current.overlay_string_index >= 0)
3778 from_overlay
3779 = it->string_overlays[it->stack[i].current.overlay_string_index
3780 % OVERLAY_STRING_CHUNK_SIZE];
3781 else if (! NILP (it->stack[i].from_overlay))
3782 from_overlay = it->stack[i].from_overlay;
3783
3784 if (!NILP (from_overlay))
3785 break;
3786 }
3787
3788 if (! NILP (from_overlay))
3789 {
3790 bufpos = IT_CHARPOS (*it);
3791 /* For a string from an overlay, the base face depends
3792 only on text properties and ignores overlays. */
3793 base_face_id
3794 = face_for_overlay_string (it->w,
3795 IT_CHARPOS (*it),
3796 it->region_beg_charpos,
3797 it->region_end_charpos,
3798 &next_stop,
3799 (IT_CHARPOS (*it)
3800 + TEXT_PROP_DISTANCE_LIMIT),
3801 0,
3802 from_overlay);
3803 }
3804 else
3805 {
3806 bufpos = 0;
3807
3808 /* For strings from a `display' property, use the face at
3809 IT's current buffer position as the base face to merge
3810 with, so that overlay strings appear in the same face as
3811 surrounding text, unless they specify their own
3812 faces. */
3813 base_face_id = it->string_from_prefix_prop_p
3814 ? DEFAULT_FACE_ID
3815 : underlying_face_id (it);
3816 }
3817
3818 new_face_id = face_at_string_position (it->w,
3819 it->string,
3820 IT_STRING_CHARPOS (*it),
3821 bufpos,
3822 it->region_beg_charpos,
3823 it->region_end_charpos,
3824 &next_stop,
3825 base_face_id, 0);
3826
3827 /* Is this a start of a run of characters with box? Caveat:
3828 this can be called for a freshly allocated iterator; face_id
3829 is -1 is this case. We know that the new face will not
3830 change until the next check pos, i.e. if the new face has a
3831 box, all characters up to that position will have a
3832 box. But, as usual, we don't know whether that position
3833 is really the end. */
3834 if (new_face_id != it->face_id)
3835 {
3836 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3837 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3838
3839 /* If new face has a box but old face hasn't, this is the
3840 start of a run of characters with box, i.e. it has a
3841 shadow on the left side. */
3842 it->start_of_box_run_p
3843 = new_face->box && (old_face == NULL || !old_face->box);
3844 it->face_box_p = new_face->box != FACE_NO_BOX;
3845 }
3846 }
3847
3848 it->face_id = new_face_id;
3849 return HANDLED_NORMALLY;
3850 }
3851
3852
3853 /* Return the ID of the face ``underlying'' IT's current position,
3854 which is in a string. If the iterator is associated with a
3855 buffer, return the face at IT's current buffer position.
3856 Otherwise, use the iterator's base_face_id. */
3857
3858 static int
3859 underlying_face_id (struct it *it)
3860 {
3861 int face_id = it->base_face_id, i;
3862
3863 eassert (STRINGP (it->string));
3864
3865 for (i = it->sp - 1; i >= 0; --i)
3866 if (NILP (it->stack[i].string))
3867 face_id = it->stack[i].face_id;
3868
3869 return face_id;
3870 }
3871
3872
3873 /* Compute the face one character before or after the current position
3874 of IT, in the visual order. BEFORE_P non-zero means get the face
3875 in front (to the left in L2R paragraphs, to the right in R2L
3876 paragraphs) of IT's screen position. Value is the ID of the face. */
3877
3878 static int
3879 face_before_or_after_it_pos (struct it *it, int before_p)
3880 {
3881 int face_id, limit;
3882 ptrdiff_t next_check_charpos;
3883 struct it it_copy;
3884 void *it_copy_data = NULL;
3885
3886 eassert (it->s == NULL);
3887
3888 if (STRINGP (it->string))
3889 {
3890 ptrdiff_t bufpos, charpos;
3891 int base_face_id;
3892
3893 /* No face change past the end of the string (for the case
3894 we are padding with spaces). No face change before the
3895 string start. */
3896 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3897 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3898 return it->face_id;
3899
3900 if (!it->bidi_p)
3901 {
3902 /* Set charpos to the position before or after IT's current
3903 position, in the logical order, which in the non-bidi
3904 case is the same as the visual order. */
3905 if (before_p)
3906 charpos = IT_STRING_CHARPOS (*it) - 1;
3907 else if (it->what == IT_COMPOSITION)
3908 /* For composition, we must check the character after the
3909 composition. */
3910 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3911 else
3912 charpos = IT_STRING_CHARPOS (*it) + 1;
3913 }
3914 else
3915 {
3916 if (before_p)
3917 {
3918 /* With bidi iteration, the character before the current
3919 in the visual order cannot be found by simple
3920 iteration, because "reverse" reordering is not
3921 supported. Instead, we need to use the move_it_*
3922 family of functions. */
3923 /* Ignore face changes before the first visible
3924 character on this display line. */
3925 if (it->current_x <= it->first_visible_x)
3926 return it->face_id;
3927 SAVE_IT (it_copy, *it, it_copy_data);
3928 /* Implementation note: Since move_it_in_display_line
3929 works in the iterator geometry, and thinks the first
3930 character is always the leftmost, even in R2L lines,
3931 we don't need to distinguish between the R2L and L2R
3932 cases here. */
3933 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3934 it_copy.current_x - 1, MOVE_TO_X);
3935 charpos = IT_STRING_CHARPOS (it_copy);
3936 RESTORE_IT (it, it, it_copy_data);
3937 }
3938 else
3939 {
3940 /* Set charpos to the string position of the character
3941 that comes after IT's current position in the visual
3942 order. */
3943 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3944
3945 it_copy = *it;
3946 while (n--)
3947 bidi_move_to_visually_next (&it_copy.bidi_it);
3948
3949 charpos = it_copy.bidi_it.charpos;
3950 }
3951 }
3952 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3953
3954 if (it->current.overlay_string_index >= 0)
3955 bufpos = IT_CHARPOS (*it);
3956 else
3957 bufpos = 0;
3958
3959 base_face_id = underlying_face_id (it);
3960
3961 /* Get the face for ASCII, or unibyte. */
3962 face_id = face_at_string_position (it->w,
3963 it->string,
3964 charpos,
3965 bufpos,
3966 it->region_beg_charpos,
3967 it->region_end_charpos,
3968 &next_check_charpos,
3969 base_face_id, 0);
3970
3971 /* Correct the face for charsets different from ASCII. Do it
3972 for the multibyte case only. The face returned above is
3973 suitable for unibyte text if IT->string is unibyte. */
3974 if (STRING_MULTIBYTE (it->string))
3975 {
3976 struct text_pos pos1 = string_pos (charpos, it->string);
3977 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3978 int c, len;
3979 struct face *face = FACE_FROM_ID (it->f, face_id);
3980
3981 c = string_char_and_length (p, &len);
3982 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3983 }
3984 }
3985 else
3986 {
3987 struct text_pos pos;
3988
3989 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3990 || (IT_CHARPOS (*it) <= BEGV && before_p))
3991 return it->face_id;
3992
3993 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3994 pos = it->current.pos;
3995
3996 if (!it->bidi_p)
3997 {
3998 if (before_p)
3999 DEC_TEXT_POS (pos, it->multibyte_p);
4000 else
4001 {
4002 if (it->what == IT_COMPOSITION)
4003 {
4004 /* For composition, we must check the position after
4005 the composition. */
4006 pos.charpos += it->cmp_it.nchars;
4007 pos.bytepos += it->len;
4008 }
4009 else
4010 INC_TEXT_POS (pos, it->multibyte_p);
4011 }
4012 }
4013 else
4014 {
4015 if (before_p)
4016 {
4017 /* With bidi iteration, the character before the current
4018 in the visual order cannot be found by simple
4019 iteration, because "reverse" reordering is not
4020 supported. Instead, we need to use the move_it_*
4021 family of functions. */
4022 /* Ignore face changes before the first visible
4023 character on this display line. */
4024 if (it->current_x <= it->first_visible_x)
4025 return it->face_id;
4026 SAVE_IT (it_copy, *it, it_copy_data);
4027 /* Implementation note: Since move_it_in_display_line
4028 works in the iterator geometry, and thinks the first
4029 character is always the leftmost, even in R2L lines,
4030 we don't need to distinguish between the R2L and L2R
4031 cases here. */
4032 move_it_in_display_line (&it_copy, ZV,
4033 it_copy.current_x - 1, MOVE_TO_X);
4034 pos = it_copy.current.pos;
4035 RESTORE_IT (it, it, it_copy_data);
4036 }
4037 else
4038 {
4039 /* Set charpos to the buffer position of the character
4040 that comes after IT's current position in the visual
4041 order. */
4042 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4043
4044 it_copy = *it;
4045 while (n--)
4046 bidi_move_to_visually_next (&it_copy.bidi_it);
4047
4048 SET_TEXT_POS (pos,
4049 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4050 }
4051 }
4052 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4053
4054 /* Determine face for CHARSET_ASCII, or unibyte. */
4055 face_id = face_at_buffer_position (it->w,
4056 CHARPOS (pos),
4057 it->region_beg_charpos,
4058 it->region_end_charpos,
4059 &next_check_charpos,
4060 limit, 0, -1);
4061
4062 /* Correct the face for charsets different from ASCII. Do it
4063 for the multibyte case only. The face returned above is
4064 suitable for unibyte text if current_buffer is unibyte. */
4065 if (it->multibyte_p)
4066 {
4067 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4068 struct face *face = FACE_FROM_ID (it->f, face_id);
4069 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4070 }
4071 }
4072
4073 return face_id;
4074 }
4075
4076
4077 \f
4078 /***********************************************************************
4079 Invisible text
4080 ***********************************************************************/
4081
4082 /* Set up iterator IT from invisible properties at its current
4083 position. Called from handle_stop. */
4084
4085 static enum prop_handled
4086 handle_invisible_prop (struct it *it)
4087 {
4088 enum prop_handled handled = HANDLED_NORMALLY;
4089 int invis_p;
4090 Lisp_Object prop;
4091
4092 if (STRINGP (it->string))
4093 {
4094 Lisp_Object end_charpos, limit, charpos;
4095
4096 /* Get the value of the invisible text property at the
4097 current position. Value will be nil if there is no such
4098 property. */
4099 charpos = make_number (IT_STRING_CHARPOS (*it));
4100 prop = Fget_text_property (charpos, Qinvisible, it->string);
4101 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4102
4103 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4104 {
4105 /* Record whether we have to display an ellipsis for the
4106 invisible text. */
4107 int display_ellipsis_p = (invis_p == 2);
4108 ptrdiff_t len, endpos;
4109
4110 handled = HANDLED_RECOMPUTE_PROPS;
4111
4112 /* Get the position at which the next visible text can be
4113 found in IT->string, if any. */
4114 endpos = len = SCHARS (it->string);
4115 XSETINT (limit, len);
4116 do
4117 {
4118 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4119 it->string, limit);
4120 if (INTEGERP (end_charpos))
4121 {
4122 endpos = XFASTINT (end_charpos);
4123 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4124 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4125 if (invis_p == 2)
4126 display_ellipsis_p = 1;
4127 }
4128 }
4129 while (invis_p && endpos < len);
4130
4131 if (display_ellipsis_p)
4132 it->ellipsis_p = 1;
4133
4134 if (endpos < len)
4135 {
4136 /* Text at END_CHARPOS is visible. Move IT there. */
4137 struct text_pos old;
4138 ptrdiff_t oldpos;
4139
4140 old = it->current.string_pos;
4141 oldpos = CHARPOS (old);
4142 if (it->bidi_p)
4143 {
4144 if (it->bidi_it.first_elt
4145 && it->bidi_it.charpos < SCHARS (it->string))
4146 bidi_paragraph_init (it->paragraph_embedding,
4147 &it->bidi_it, 1);
4148 /* Bidi-iterate out of the invisible text. */
4149 do
4150 {
4151 bidi_move_to_visually_next (&it->bidi_it);
4152 }
4153 while (oldpos <= it->bidi_it.charpos
4154 && it->bidi_it.charpos < endpos);
4155
4156 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4157 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4158 if (IT_CHARPOS (*it) >= endpos)
4159 it->prev_stop = endpos;
4160 }
4161 else
4162 {
4163 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4164 compute_string_pos (&it->current.string_pos, old, it->string);
4165 }
4166 }
4167 else
4168 {
4169 /* The rest of the string is invisible. If this is an
4170 overlay string, proceed with the next overlay string
4171 or whatever comes and return a character from there. */
4172 if (it->current.overlay_string_index >= 0
4173 && !display_ellipsis_p)
4174 {
4175 next_overlay_string (it);
4176 /* Don't check for overlay strings when we just
4177 finished processing them. */
4178 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4179 }
4180 else
4181 {
4182 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4183 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4184 }
4185 }
4186 }
4187 }
4188 else
4189 {
4190 ptrdiff_t newpos, next_stop, start_charpos, tem;
4191 Lisp_Object pos, overlay;
4192
4193 /* First of all, is there invisible text at this position? */
4194 tem = start_charpos = IT_CHARPOS (*it);
4195 pos = make_number (tem);
4196 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4197 &overlay);
4198 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4199
4200 /* If we are on invisible text, skip over it. */
4201 if (invis_p && start_charpos < it->end_charpos)
4202 {
4203 /* Record whether we have to display an ellipsis for the
4204 invisible text. */
4205 int display_ellipsis_p = invis_p == 2;
4206
4207 handled = HANDLED_RECOMPUTE_PROPS;
4208
4209 /* Loop skipping over invisible text. The loop is left at
4210 ZV or with IT on the first char being visible again. */
4211 do
4212 {
4213 /* Try to skip some invisible text. Return value is the
4214 position reached which can be equal to where we start
4215 if there is nothing invisible there. This skips both
4216 over invisible text properties and overlays with
4217 invisible property. */
4218 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4219
4220 /* If we skipped nothing at all we weren't at invisible
4221 text in the first place. If everything to the end of
4222 the buffer was skipped, end the loop. */
4223 if (newpos == tem || newpos >= ZV)
4224 invis_p = 0;
4225 else
4226 {
4227 /* We skipped some characters but not necessarily
4228 all there are. Check if we ended up on visible
4229 text. Fget_char_property returns the property of
4230 the char before the given position, i.e. if we
4231 get invis_p = 0, this means that the char at
4232 newpos is visible. */
4233 pos = make_number (newpos);
4234 prop = Fget_char_property (pos, Qinvisible, it->window);
4235 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4236 }
4237
4238 /* If we ended up on invisible text, proceed to
4239 skip starting with next_stop. */
4240 if (invis_p)
4241 tem = next_stop;
4242
4243 /* If there are adjacent invisible texts, don't lose the
4244 second one's ellipsis. */
4245 if (invis_p == 2)
4246 display_ellipsis_p = 1;
4247 }
4248 while (invis_p);
4249
4250 /* The position newpos is now either ZV or on visible text. */
4251 if (it->bidi_p)
4252 {
4253 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4254 int on_newline =
4255 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4256 int after_newline =
4257 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4258
4259 /* If the invisible text ends on a newline or on a
4260 character after a newline, we can avoid the costly,
4261 character by character, bidi iteration to NEWPOS, and
4262 instead simply reseat the iterator there. That's
4263 because all bidi reordering information is tossed at
4264 the newline. This is a big win for modes that hide
4265 complete lines, like Outline, Org, etc. */
4266 if (on_newline || after_newline)
4267 {
4268 struct text_pos tpos;
4269 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4270
4271 SET_TEXT_POS (tpos, newpos, bpos);
4272 reseat_1 (it, tpos, 0);
4273 /* If we reseat on a newline/ZV, we need to prep the
4274 bidi iterator for advancing to the next character
4275 after the newline/EOB, keeping the current paragraph
4276 direction (so that PRODUCE_GLYPHS does TRT wrt
4277 prepending/appending glyphs to a glyph row). */
4278 if (on_newline)
4279 {
4280 it->bidi_it.first_elt = 0;
4281 it->bidi_it.paragraph_dir = pdir;
4282 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4283 it->bidi_it.nchars = 1;
4284 it->bidi_it.ch_len = 1;
4285 }
4286 }
4287 else /* Must use the slow method. */
4288 {
4289 /* With bidi iteration, the region of invisible text
4290 could start and/or end in the middle of a
4291 non-base embedding level. Therefore, we need to
4292 skip invisible text using the bidi iterator,
4293 starting at IT's current position, until we find
4294 ourselves outside of the invisible text.
4295 Skipping invisible text _after_ bidi iteration
4296 avoids affecting the visual order of the
4297 displayed text when invisible properties are
4298 added or removed. */
4299 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4300 {
4301 /* If we were `reseat'ed to a new paragraph,
4302 determine the paragraph base direction. We
4303 need to do it now because
4304 next_element_from_buffer may not have a
4305 chance to do it, if we are going to skip any
4306 text at the beginning, which resets the
4307 FIRST_ELT flag. */
4308 bidi_paragraph_init (it->paragraph_embedding,
4309 &it->bidi_it, 1);
4310 }
4311 do
4312 {
4313 bidi_move_to_visually_next (&it->bidi_it);
4314 }
4315 while (it->stop_charpos <= it->bidi_it.charpos
4316 && it->bidi_it.charpos < newpos);
4317 IT_CHARPOS (*it) = it->bidi_it.charpos;
4318 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4319 /* If we overstepped NEWPOS, record its position in
4320 the iterator, so that we skip invisible text if
4321 later the bidi iteration lands us in the
4322 invisible region again. */
4323 if (IT_CHARPOS (*it) >= newpos)
4324 it->prev_stop = newpos;
4325 }
4326 }
4327 else
4328 {
4329 IT_CHARPOS (*it) = newpos;
4330 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4331 }
4332
4333 /* If there are before-strings at the start of invisible
4334 text, and the text is invisible because of a text
4335 property, arrange to show before-strings because 20.x did
4336 it that way. (If the text is invisible because of an
4337 overlay property instead of a text property, this is
4338 already handled in the overlay code.) */
4339 if (NILP (overlay)
4340 && get_overlay_strings (it, it->stop_charpos))
4341 {
4342 handled = HANDLED_RECOMPUTE_PROPS;
4343 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4344 }
4345 else if (display_ellipsis_p)
4346 {
4347 /* Make sure that the glyphs of the ellipsis will get
4348 correct `charpos' values. If we would not update
4349 it->position here, the glyphs would belong to the
4350 last visible character _before_ the invisible
4351 text, which confuses `set_cursor_from_row'.
4352
4353 We use the last invisible position instead of the
4354 first because this way the cursor is always drawn on
4355 the first "." of the ellipsis, whenever PT is inside
4356 the invisible text. Otherwise the cursor would be
4357 placed _after_ the ellipsis when the point is after the
4358 first invisible character. */
4359 if (!STRINGP (it->object))
4360 {
4361 it->position.charpos = newpos - 1;
4362 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4363 }
4364 it->ellipsis_p = 1;
4365 /* Let the ellipsis display before
4366 considering any properties of the following char.
4367 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4368 handled = HANDLED_RETURN;
4369 }
4370 }
4371 }
4372
4373 return handled;
4374 }
4375
4376
4377 /* Make iterator IT return `...' next.
4378 Replaces LEN characters from buffer. */
4379
4380 static void
4381 setup_for_ellipsis (struct it *it, int len)
4382 {
4383 /* Use the display table definition for `...'. Invalid glyphs
4384 will be handled by the method returning elements from dpvec. */
4385 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4386 {
4387 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4388 it->dpvec = v->contents;
4389 it->dpend = v->contents + v->header.size;
4390 }
4391 else
4392 {
4393 /* Default `...'. */
4394 it->dpvec = default_invis_vector;
4395 it->dpend = default_invis_vector + 3;
4396 }
4397
4398 it->dpvec_char_len = len;
4399 it->current.dpvec_index = 0;
4400 it->dpvec_face_id = -1;
4401
4402 /* Remember the current face id in case glyphs specify faces.
4403 IT's face is restored in set_iterator_to_next.
4404 saved_face_id was set to preceding char's face in handle_stop. */
4405 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4406 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4407
4408 it->method = GET_FROM_DISPLAY_VECTOR;
4409 it->ellipsis_p = 1;
4410 }
4411
4412
4413 \f
4414 /***********************************************************************
4415 'display' property
4416 ***********************************************************************/
4417
4418 /* Set up iterator IT from `display' property at its current position.
4419 Called from handle_stop.
4420 We return HANDLED_RETURN if some part of the display property
4421 overrides the display of the buffer text itself.
4422 Otherwise we return HANDLED_NORMALLY. */
4423
4424 static enum prop_handled
4425 handle_display_prop (struct it *it)
4426 {
4427 Lisp_Object propval, object, overlay;
4428 struct text_pos *position;
4429 ptrdiff_t bufpos;
4430 /* Nonzero if some property replaces the display of the text itself. */
4431 int display_replaced_p = 0;
4432
4433 if (STRINGP (it->string))
4434 {
4435 object = it->string;
4436 position = &it->current.string_pos;
4437 bufpos = CHARPOS (it->current.pos);
4438 }
4439 else
4440 {
4441 XSETWINDOW (object, it->w);
4442 position = &it->current.pos;
4443 bufpos = CHARPOS (*position);
4444 }
4445
4446 /* Reset those iterator values set from display property values. */
4447 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4448 it->space_width = Qnil;
4449 it->font_height = Qnil;
4450 it->voffset = 0;
4451
4452 /* We don't support recursive `display' properties, i.e. string
4453 values that have a string `display' property, that have a string
4454 `display' property etc. */
4455 if (!it->string_from_display_prop_p)
4456 it->area = TEXT_AREA;
4457
4458 propval = get_char_property_and_overlay (make_number (position->charpos),
4459 Qdisplay, object, &overlay);
4460 if (NILP (propval))
4461 return HANDLED_NORMALLY;
4462 /* Now OVERLAY is the overlay that gave us this property, or nil
4463 if it was a text property. */
4464
4465 if (!STRINGP (it->string))
4466 object = it->w->contents;
4467
4468 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4469 position, bufpos,
4470 FRAME_WINDOW_P (it->f));
4471
4472 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4473 }
4474
4475 /* Subroutine of handle_display_prop. Returns non-zero if the display
4476 specification in SPEC is a replacing specification, i.e. it would
4477 replace the text covered by `display' property with something else,
4478 such as an image or a display string. If SPEC includes any kind or
4479 `(space ...) specification, the value is 2; this is used by
4480 compute_display_string_pos, which see.
4481
4482 See handle_single_display_spec for documentation of arguments.
4483 frame_window_p is non-zero if the window being redisplayed is on a
4484 GUI frame; this argument is used only if IT is NULL, see below.
4485
4486 IT can be NULL, if this is called by the bidi reordering code
4487 through compute_display_string_pos, which see. In that case, this
4488 function only examines SPEC, but does not otherwise "handle" it, in
4489 the sense that it doesn't set up members of IT from the display
4490 spec. */
4491 static int
4492 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4493 Lisp_Object overlay, struct text_pos *position,
4494 ptrdiff_t bufpos, int frame_window_p)
4495 {
4496 int replacing_p = 0;
4497 int rv;
4498
4499 if (CONSP (spec)
4500 /* Simple specifications. */
4501 && !EQ (XCAR (spec), Qimage)
4502 #ifdef HAVE_XWIDGETS
4503 && !EQ (XCAR (spec), Qxwidget)
4504 #endif
4505 && !EQ (XCAR (spec), Qspace)
4506 && !EQ (XCAR (spec), Qwhen)
4507 && !EQ (XCAR (spec), Qslice)
4508 && !EQ (XCAR (spec), Qspace_width)
4509 && !EQ (XCAR (spec), Qheight)
4510 && !EQ (XCAR (spec), Qraise)
4511 /* Marginal area specifications. */
4512 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4513 && !EQ (XCAR (spec), Qleft_fringe)
4514 && !EQ (XCAR (spec), Qright_fringe)
4515 && !NILP (XCAR (spec)))
4516 {
4517 for (; CONSP (spec); spec = XCDR (spec))
4518 {
4519 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4520 overlay, position, bufpos,
4521 replacing_p, frame_window_p)))
4522 {
4523 replacing_p = rv;
4524 /* If some text in a string is replaced, `position' no
4525 longer points to the position of `object'. */
4526 if (!it || STRINGP (object))
4527 break;
4528 }
4529 }
4530 }
4531 else if (VECTORP (spec))
4532 {
4533 ptrdiff_t i;
4534 for (i = 0; i < ASIZE (spec); ++i)
4535 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4536 overlay, position, bufpos,
4537 replacing_p, frame_window_p)))
4538 {
4539 replacing_p = rv;
4540 /* If some text in a string is replaced, `position' no
4541 longer points to the position of `object'. */
4542 if (!it || STRINGP (object))
4543 break;
4544 }
4545 }
4546 else
4547 {
4548 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4549 position, bufpos, 0,
4550 frame_window_p)))
4551 replacing_p = rv;
4552 }
4553
4554 return replacing_p;
4555 }
4556
4557 /* Value is the position of the end of the `display' property starting
4558 at START_POS in OBJECT. */
4559
4560 static struct text_pos
4561 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4562 {
4563 Lisp_Object end;
4564 struct text_pos end_pos;
4565
4566 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4567 Qdisplay, object, Qnil);
4568 CHARPOS (end_pos) = XFASTINT (end);
4569 if (STRINGP (object))
4570 compute_string_pos (&end_pos, start_pos, it->string);
4571 else
4572 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4573
4574 return end_pos;
4575 }
4576
4577
4578 /* Set up IT from a single `display' property specification SPEC. OBJECT
4579 is the object in which the `display' property was found. *POSITION
4580 is the position in OBJECT at which the `display' property was found.
4581 BUFPOS is the buffer position of OBJECT (different from POSITION if
4582 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4583 previously saw a display specification which already replaced text
4584 display with something else, for example an image; we ignore such
4585 properties after the first one has been processed.
4586
4587 OVERLAY is the overlay this `display' property came from,
4588 or nil if it was a text property.
4589
4590 If SPEC is a `space' or `image' specification, and in some other
4591 cases too, set *POSITION to the position where the `display'
4592 property ends.
4593
4594 If IT is NULL, only examine the property specification in SPEC, but
4595 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4596 is intended to be displayed in a window on a GUI frame.
4597
4598 Value is non-zero if something was found which replaces the display
4599 of buffer or string text. */
4600
4601 static int
4602 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4603 Lisp_Object overlay, struct text_pos *position,
4604 ptrdiff_t bufpos, int display_replaced_p,
4605 int frame_window_p)
4606 {
4607 Lisp_Object form;
4608 Lisp_Object location, value;
4609 struct text_pos start_pos = *position;
4610 int valid_p;
4611
4612 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4613 If the result is non-nil, use VALUE instead of SPEC. */
4614 form = Qt;
4615 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4616 {
4617 spec = XCDR (spec);
4618 if (!CONSP (spec))
4619 return 0;
4620 form = XCAR (spec);
4621 spec = XCDR (spec);
4622 }
4623
4624 if (!NILP (form) && !EQ (form, Qt))
4625 {
4626 ptrdiff_t count = SPECPDL_INDEX ();
4627 struct gcpro gcpro1;
4628
4629 /* Bind `object' to the object having the `display' property, a
4630 buffer or string. Bind `position' to the position in the
4631 object where the property was found, and `buffer-position'
4632 to the current position in the buffer. */
4633
4634 if (NILP (object))
4635 XSETBUFFER (object, current_buffer);
4636 specbind (Qobject, object);
4637 specbind (Qposition, make_number (CHARPOS (*position)));
4638 specbind (Qbuffer_position, make_number (bufpos));
4639 GCPRO1 (form);
4640 form = safe_eval (form);
4641 UNGCPRO;
4642 unbind_to (count, Qnil);
4643 }
4644
4645 if (NILP (form))
4646 return 0;
4647
4648 /* Handle `(height HEIGHT)' specifications. */
4649 if (CONSP (spec)
4650 && EQ (XCAR (spec), Qheight)
4651 && CONSP (XCDR (spec)))
4652 {
4653 if (it)
4654 {
4655 if (!FRAME_WINDOW_P (it->f))
4656 return 0;
4657
4658 it->font_height = XCAR (XCDR (spec));
4659 if (!NILP (it->font_height))
4660 {
4661 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4662 int new_height = -1;
4663
4664 if (CONSP (it->font_height)
4665 && (EQ (XCAR (it->font_height), Qplus)
4666 || EQ (XCAR (it->font_height), Qminus))
4667 && CONSP (XCDR (it->font_height))
4668 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4669 {
4670 /* `(+ N)' or `(- N)' where N is an integer. */
4671 int steps = XINT (XCAR (XCDR (it->font_height)));
4672 if (EQ (XCAR (it->font_height), Qplus))
4673 steps = - steps;
4674 it->face_id = smaller_face (it->f, it->face_id, steps);
4675 }
4676 else if (FUNCTIONP (it->font_height))
4677 {
4678 /* Call function with current height as argument.
4679 Value is the new height. */
4680 Lisp_Object height;
4681 height = safe_call1 (it->font_height,
4682 face->lface[LFACE_HEIGHT_INDEX]);
4683 if (NUMBERP (height))
4684 new_height = XFLOATINT (height);
4685 }
4686 else if (NUMBERP (it->font_height))
4687 {
4688 /* Value is a multiple of the canonical char height. */
4689 struct face *f;
4690
4691 f = FACE_FROM_ID (it->f,
4692 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4693 new_height = (XFLOATINT (it->font_height)
4694 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4695 }
4696 else
4697 {
4698 /* Evaluate IT->font_height with `height' bound to the
4699 current specified height to get the new height. */
4700 ptrdiff_t count = SPECPDL_INDEX ();
4701
4702 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4703 value = safe_eval (it->font_height);
4704 unbind_to (count, Qnil);
4705
4706 if (NUMBERP (value))
4707 new_height = XFLOATINT (value);
4708 }
4709
4710 if (new_height > 0)
4711 it->face_id = face_with_height (it->f, it->face_id, new_height);
4712 }
4713 }
4714
4715 return 0;
4716 }
4717
4718 /* Handle `(space-width WIDTH)'. */
4719 if (CONSP (spec)
4720 && EQ (XCAR (spec), Qspace_width)
4721 && CONSP (XCDR (spec)))
4722 {
4723 if (it)
4724 {
4725 if (!FRAME_WINDOW_P (it->f))
4726 return 0;
4727
4728 value = XCAR (XCDR (spec));
4729 if (NUMBERP (value) && XFLOATINT (value) > 0)
4730 it->space_width = value;
4731 }
4732
4733 return 0;
4734 }
4735
4736 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4737 if (CONSP (spec)
4738 && EQ (XCAR (spec), Qslice))
4739 {
4740 Lisp_Object tem;
4741
4742 if (it)
4743 {
4744 if (!FRAME_WINDOW_P (it->f))
4745 return 0;
4746
4747 if (tem = XCDR (spec), CONSP (tem))
4748 {
4749 it->slice.x = XCAR (tem);
4750 if (tem = XCDR (tem), CONSP (tem))
4751 {
4752 it->slice.y = XCAR (tem);
4753 if (tem = XCDR (tem), CONSP (tem))
4754 {
4755 it->slice.width = XCAR (tem);
4756 if (tem = XCDR (tem), CONSP (tem))
4757 it->slice.height = XCAR (tem);
4758 }
4759 }
4760 }
4761 }
4762
4763 return 0;
4764 }
4765
4766 /* Handle `(raise FACTOR)'. */
4767 if (CONSP (spec)
4768 && EQ (XCAR (spec), Qraise)
4769 && CONSP (XCDR (spec)))
4770 {
4771 if (it)
4772 {
4773 if (!FRAME_WINDOW_P (it->f))
4774 return 0;
4775
4776 #ifdef HAVE_WINDOW_SYSTEM
4777 value = XCAR (XCDR (spec));
4778 if (NUMBERP (value))
4779 {
4780 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4781 it->voffset = - (XFLOATINT (value)
4782 * (FONT_HEIGHT (face->font)));
4783 }
4784 #endif /* HAVE_WINDOW_SYSTEM */
4785 }
4786
4787 return 0;
4788 }
4789
4790 /* Don't handle the other kinds of display specifications
4791 inside a string that we got from a `display' property. */
4792 if (it && it->string_from_display_prop_p)
4793 return 0;
4794
4795 /* Characters having this form of property are not displayed, so
4796 we have to find the end of the property. */
4797 if (it)
4798 {
4799 start_pos = *position;
4800 *position = display_prop_end (it, object, start_pos);
4801 }
4802 value = Qnil;
4803
4804 /* Stop the scan at that end position--we assume that all
4805 text properties change there. */
4806 if (it)
4807 it->stop_charpos = position->charpos;
4808
4809 /* Handle `(left-fringe BITMAP [FACE])'
4810 and `(right-fringe BITMAP [FACE])'. */
4811 if (CONSP (spec)
4812 && (EQ (XCAR (spec), Qleft_fringe)
4813 || EQ (XCAR (spec), Qright_fringe))
4814 && CONSP (XCDR (spec)))
4815 {
4816 int fringe_bitmap;
4817
4818 if (it)
4819 {
4820 if (!FRAME_WINDOW_P (it->f))
4821 /* If we return here, POSITION has been advanced
4822 across the text with this property. */
4823 {
4824 /* Synchronize the bidi iterator with POSITION. This is
4825 needed because we are not going to push the iterator
4826 on behalf of this display property, so there will be
4827 no pop_it call to do this synchronization for us. */
4828 if (it->bidi_p)
4829 {
4830 it->position = *position;
4831 iterate_out_of_display_property (it);
4832 *position = it->position;
4833 }
4834 return 1;
4835 }
4836 }
4837 else if (!frame_window_p)
4838 return 1;
4839
4840 #ifdef HAVE_WINDOW_SYSTEM
4841 value = XCAR (XCDR (spec));
4842 if (!SYMBOLP (value)
4843 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4844 /* If we return here, POSITION has been advanced
4845 across the text with this property. */
4846 {
4847 if (it && it->bidi_p)
4848 {
4849 it->position = *position;
4850 iterate_out_of_display_property (it);
4851 *position = it->position;
4852 }
4853 return 1;
4854 }
4855
4856 if (it)
4857 {
4858 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4859
4860 if (CONSP (XCDR (XCDR (spec))))
4861 {
4862 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4863 int face_id2 = lookup_derived_face (it->f, face_name,
4864 FRINGE_FACE_ID, 0);
4865 if (face_id2 >= 0)
4866 face_id = face_id2;
4867 }
4868
4869 /* Save current settings of IT so that we can restore them
4870 when we are finished with the glyph property value. */
4871 push_it (it, position);
4872
4873 it->area = TEXT_AREA;
4874 it->what = IT_IMAGE;
4875 it->image_id = -1; /* no image */
4876 it->position = start_pos;
4877 it->object = NILP (object) ? it->w->contents : object;
4878 it->method = GET_FROM_IMAGE;
4879 it->from_overlay = Qnil;
4880 it->face_id = face_id;
4881 it->from_disp_prop_p = 1;
4882
4883 /* Say that we haven't consumed the characters with
4884 `display' property yet. The call to pop_it in
4885 set_iterator_to_next will clean this up. */
4886 *position = start_pos;
4887
4888 if (EQ (XCAR (spec), Qleft_fringe))
4889 {
4890 it->left_user_fringe_bitmap = fringe_bitmap;
4891 it->left_user_fringe_face_id = face_id;
4892 }
4893 else
4894 {
4895 it->right_user_fringe_bitmap = fringe_bitmap;
4896 it->right_user_fringe_face_id = face_id;
4897 }
4898 }
4899 #endif /* HAVE_WINDOW_SYSTEM */
4900 return 1;
4901 }
4902
4903 /* Prepare to handle `((margin left-margin) ...)',
4904 `((margin right-margin) ...)' and `((margin nil) ...)'
4905 prefixes for display specifications. */
4906 location = Qunbound;
4907 if (CONSP (spec) && CONSP (XCAR (spec)))
4908 {
4909 Lisp_Object tem;
4910
4911 value = XCDR (spec);
4912 if (CONSP (value))
4913 value = XCAR (value);
4914
4915 tem = XCAR (spec);
4916 if (EQ (XCAR (tem), Qmargin)
4917 && (tem = XCDR (tem),
4918 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4919 (NILP (tem)
4920 || EQ (tem, Qleft_margin)
4921 || EQ (tem, Qright_margin))))
4922 location = tem;
4923 }
4924
4925 if (EQ (location, Qunbound))
4926 {
4927 location = Qnil;
4928 value = spec;
4929 }
4930
4931 /* After this point, VALUE is the property after any
4932 margin prefix has been stripped. It must be a string,
4933 an image specification, or `(space ...)'.
4934
4935 LOCATION specifies where to display: `left-margin',
4936 `right-margin' or nil. */
4937
4938 valid_p = (STRINGP (value)
4939 #ifdef HAVE_WINDOW_SYSTEM
4940 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4941 && valid_image_p (value))
4942 #endif /* not HAVE_WINDOW_SYSTEM */
4943 || (CONSP (value) && EQ (XCAR (value), Qspace))
4944 #ifdef HAVE_XWIDGETS
4945 || XWIDGETP(value)
4946 #endif
4947 );
4948
4949 if (valid_p && !display_replaced_p)
4950 {
4951 int retval = 1;
4952
4953 if (!it)
4954 {
4955 /* Callers need to know whether the display spec is any kind
4956 of `(space ...)' spec that is about to affect text-area
4957 display. */
4958 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4959 retval = 2;
4960 return retval;
4961 }
4962
4963 /* Save current settings of IT so that we can restore them
4964 when we are finished with the glyph property value. */
4965 push_it (it, position);
4966 it->from_overlay = overlay;
4967 it->from_disp_prop_p = 1;
4968
4969 if (NILP (location))
4970 it->area = TEXT_AREA;
4971 else if (EQ (location, Qleft_margin))
4972 it->area = LEFT_MARGIN_AREA;
4973 else
4974 it->area = RIGHT_MARGIN_AREA;
4975
4976 if (STRINGP (value))
4977 {
4978 it->string = value;
4979 it->multibyte_p = STRING_MULTIBYTE (it->string);
4980 it->current.overlay_string_index = -1;
4981 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4982 it->end_charpos = it->string_nchars = SCHARS (it->string);
4983 it->method = GET_FROM_STRING;
4984 it->stop_charpos = 0;
4985 it->prev_stop = 0;
4986 it->base_level_stop = 0;
4987 it->string_from_display_prop_p = 1;
4988 /* Say that we haven't consumed the characters with
4989 `display' property yet. The call to pop_it in
4990 set_iterator_to_next will clean this up. */
4991 if (BUFFERP (object))
4992 *position = start_pos;
4993
4994 /* Force paragraph direction to be that of the parent
4995 object. If the parent object's paragraph direction is
4996 not yet determined, default to L2R. */
4997 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4998 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4999 else
5000 it->paragraph_embedding = L2R;
5001
5002 /* Set up the bidi iterator for this display string. */
5003 if (it->bidi_p)
5004 {
5005 it->bidi_it.string.lstring = it->string;
5006 it->bidi_it.string.s = NULL;
5007 it->bidi_it.string.schars = it->end_charpos;
5008 it->bidi_it.string.bufpos = bufpos;
5009 it->bidi_it.string.from_disp_str = 1;
5010 it->bidi_it.string.unibyte = !it->multibyte_p;
5011 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5012 }
5013 }
5014 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5015 {
5016 it->method = GET_FROM_STRETCH;
5017 it->object = value;
5018 *position = it->position = start_pos;
5019 retval = 1 + (it->area == TEXT_AREA);
5020 }
5021 #ifdef HAVE_XWIDGETS
5022 else if (XWIDGETP(value))
5023 {
5024 //printf("handle_single_display_spec: im an xwidget!!\n");
5025 it->what = IT_XWIDGET;
5026 it->method = GET_FROM_XWIDGET;
5027 it->position = start_pos;
5028 it->object = NILP (object) ? it->w->contents : object;
5029 *position = start_pos;
5030
5031 it->xwidget = lookup_xwidget(value);
5032 }
5033 #endif
5034 #ifdef HAVE_WINDOW_SYSTEM
5035 else
5036 {
5037 it->what = IT_IMAGE;
5038 it->image_id = lookup_image (it->f, value);
5039 it->position = start_pos;
5040 it->object = NILP (object) ? it->w->contents : object;
5041 it->method = GET_FROM_IMAGE;
5042
5043 /* Say that we haven't consumed the characters with
5044 `display' property yet. The call to pop_it in
5045 set_iterator_to_next will clean this up. */
5046 *position = start_pos;
5047 }
5048 #endif /* HAVE_WINDOW_SYSTEM */
5049
5050 return retval;
5051 }
5052
5053 /* Invalid property or property not supported. Restore
5054 POSITION to what it was before. */
5055 *position = start_pos;
5056 return 0;
5057 }
5058
5059 /* Check if PROP is a display property value whose text should be
5060 treated as intangible. OVERLAY is the overlay from which PROP
5061 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5062 specify the buffer position covered by PROP. */
5063
5064 int
5065 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5066 ptrdiff_t charpos, ptrdiff_t bytepos)
5067 {
5068 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5069 struct text_pos position;
5070
5071 SET_TEXT_POS (position, charpos, bytepos);
5072 return handle_display_spec (NULL, prop, Qnil, overlay,
5073 &position, charpos, frame_window_p);
5074 }
5075
5076
5077 /* Return 1 if PROP is a display sub-property value containing STRING.
5078
5079 Implementation note: this and the following function are really
5080 special cases of handle_display_spec and
5081 handle_single_display_spec, and should ideally use the same code.
5082 Until they do, these two pairs must be consistent and must be
5083 modified in sync. */
5084
5085 static int
5086 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5087 {
5088 if (EQ (string, prop))
5089 return 1;
5090
5091 /* Skip over `when FORM'. */
5092 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5093 {
5094 prop = XCDR (prop);
5095 if (!CONSP (prop))
5096 return 0;
5097 /* Actually, the condition following `when' should be eval'ed,
5098 like handle_single_display_spec does, and we should return
5099 zero if it evaluates to nil. However, this function is
5100 called only when the buffer was already displayed and some
5101 glyph in the glyph matrix was found to come from a display
5102 string. Therefore, the condition was already evaluated, and
5103 the result was non-nil, otherwise the display string wouldn't
5104 have been displayed and we would have never been called for
5105 this property. Thus, we can skip the evaluation and assume
5106 its result is non-nil. */
5107 prop = XCDR (prop);
5108 }
5109
5110 if (CONSP (prop))
5111 /* Skip over `margin LOCATION'. */
5112 if (EQ (XCAR (prop), Qmargin))
5113 {
5114 prop = XCDR (prop);
5115 if (!CONSP (prop))
5116 return 0;
5117
5118 prop = XCDR (prop);
5119 if (!CONSP (prop))
5120 return 0;
5121 }
5122
5123 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5124 }
5125
5126
5127 /* Return 1 if STRING appears in the `display' property PROP. */
5128
5129 static int
5130 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5131 {
5132 if (CONSP (prop)
5133 && !EQ (XCAR (prop), Qwhen)
5134 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5135 {
5136 /* A list of sub-properties. */
5137 while (CONSP (prop))
5138 {
5139 if (single_display_spec_string_p (XCAR (prop), string))
5140 return 1;
5141 prop = XCDR (prop);
5142 }
5143 }
5144 else if (VECTORP (prop))
5145 {
5146 /* A vector of sub-properties. */
5147 ptrdiff_t i;
5148 for (i = 0; i < ASIZE (prop); ++i)
5149 if (single_display_spec_string_p (AREF (prop, i), string))
5150 return 1;
5151 }
5152 else
5153 return single_display_spec_string_p (prop, string);
5154
5155 return 0;
5156 }
5157
5158 /* Look for STRING in overlays and text properties in the current
5159 buffer, between character positions FROM and TO (excluding TO).
5160 BACK_P non-zero means look back (in this case, TO is supposed to be
5161 less than FROM).
5162 Value is the first character position where STRING was found, or
5163 zero if it wasn't found before hitting TO.
5164
5165 This function may only use code that doesn't eval because it is
5166 called asynchronously from note_mouse_highlight. */
5167
5168 static ptrdiff_t
5169 string_buffer_position_lim (Lisp_Object string,
5170 ptrdiff_t from, ptrdiff_t to, int back_p)
5171 {
5172 Lisp_Object limit, prop, pos;
5173 int found = 0;
5174
5175 pos = make_number (max (from, BEGV));
5176
5177 if (!back_p) /* looking forward */
5178 {
5179 limit = make_number (min (to, ZV));
5180 while (!found && !EQ (pos, limit))
5181 {
5182 prop = Fget_char_property (pos, Qdisplay, Qnil);
5183 if (!NILP (prop) && display_prop_string_p (prop, string))
5184 found = 1;
5185 else
5186 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5187 limit);
5188 }
5189 }
5190 else /* looking back */
5191 {
5192 limit = make_number (max (to, BEGV));
5193 while (!found && !EQ (pos, limit))
5194 {
5195 prop = Fget_char_property (pos, Qdisplay, Qnil);
5196 if (!NILP (prop) && display_prop_string_p (prop, string))
5197 found = 1;
5198 else
5199 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5200 limit);
5201 }
5202 }
5203
5204 return found ? XINT (pos) : 0;
5205 }
5206
5207 /* Determine which buffer position in current buffer STRING comes from.
5208 AROUND_CHARPOS is an approximate position where it could come from.
5209 Value is the buffer position or 0 if it couldn't be determined.
5210
5211 This function is necessary because we don't record buffer positions
5212 in glyphs generated from strings (to keep struct glyph small).
5213 This function may only use code that doesn't eval because it is
5214 called asynchronously from note_mouse_highlight. */
5215
5216 static ptrdiff_t
5217 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5218 {
5219 const int MAX_DISTANCE = 1000;
5220 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5221 around_charpos + MAX_DISTANCE,
5222 0);
5223
5224 if (!found)
5225 found = string_buffer_position_lim (string, around_charpos,
5226 around_charpos - MAX_DISTANCE, 1);
5227 return found;
5228 }
5229
5230
5231 \f
5232 /***********************************************************************
5233 `composition' property
5234 ***********************************************************************/
5235
5236 /* Set up iterator IT from `composition' property at its current
5237 position. Called from handle_stop. */
5238
5239 static enum prop_handled
5240 handle_composition_prop (struct it *it)
5241 {
5242 Lisp_Object prop, string;
5243 ptrdiff_t pos, pos_byte, start, end;
5244
5245 if (STRINGP (it->string))
5246 {
5247 unsigned char *s;
5248
5249 pos = IT_STRING_CHARPOS (*it);
5250 pos_byte = IT_STRING_BYTEPOS (*it);
5251 string = it->string;
5252 s = SDATA (string) + pos_byte;
5253 it->c = STRING_CHAR (s);
5254 }
5255 else
5256 {
5257 pos = IT_CHARPOS (*it);
5258 pos_byte = IT_BYTEPOS (*it);
5259 string = Qnil;
5260 it->c = FETCH_CHAR (pos_byte);
5261 }
5262
5263 /* If there's a valid composition and point is not inside of the
5264 composition (in the case that the composition is from the current
5265 buffer), draw a glyph composed from the composition components. */
5266 if (find_composition (pos, -1, &start, &end, &prop, string)
5267 && COMPOSITION_VALID_P (start, end, prop)
5268 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5269 {
5270 if (start < pos)
5271 /* As we can't handle this situation (perhaps font-lock added
5272 a new composition), we just return here hoping that next
5273 redisplay will detect this composition much earlier. */
5274 return HANDLED_NORMALLY;
5275 if (start != pos)
5276 {
5277 if (STRINGP (it->string))
5278 pos_byte = string_char_to_byte (it->string, start);
5279 else
5280 pos_byte = CHAR_TO_BYTE (start);
5281 }
5282 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5283 prop, string);
5284
5285 if (it->cmp_it.id >= 0)
5286 {
5287 it->cmp_it.ch = -1;
5288 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5289 it->cmp_it.nglyphs = -1;
5290 }
5291 }
5292
5293 return HANDLED_NORMALLY;
5294 }
5295
5296
5297 \f
5298 /***********************************************************************
5299 Overlay strings
5300 ***********************************************************************/
5301
5302 /* The following structure is used to record overlay strings for
5303 later sorting in load_overlay_strings. */
5304
5305 struct overlay_entry
5306 {
5307 Lisp_Object overlay;
5308 Lisp_Object string;
5309 EMACS_INT priority;
5310 int after_string_p;
5311 };
5312
5313
5314 /* Set up iterator IT from overlay strings at its current position.
5315 Called from handle_stop. */
5316
5317 static enum prop_handled
5318 handle_overlay_change (struct it *it)
5319 {
5320 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5321 return HANDLED_RECOMPUTE_PROPS;
5322 else
5323 return HANDLED_NORMALLY;
5324 }
5325
5326
5327 /* Set up the next overlay string for delivery by IT, if there is an
5328 overlay string to deliver. Called by set_iterator_to_next when the
5329 end of the current overlay string is reached. If there are more
5330 overlay strings to display, IT->string and
5331 IT->current.overlay_string_index are set appropriately here.
5332 Otherwise IT->string is set to nil. */
5333
5334 static void
5335 next_overlay_string (struct it *it)
5336 {
5337 ++it->current.overlay_string_index;
5338 if (it->current.overlay_string_index == it->n_overlay_strings)
5339 {
5340 /* No more overlay strings. Restore IT's settings to what
5341 they were before overlay strings were processed, and
5342 continue to deliver from current_buffer. */
5343
5344 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5345 pop_it (it);
5346 eassert (it->sp > 0
5347 || (NILP (it->string)
5348 && it->method == GET_FROM_BUFFER
5349 && it->stop_charpos >= BEGV
5350 && it->stop_charpos <= it->end_charpos));
5351 it->current.overlay_string_index = -1;
5352 it->n_overlay_strings = 0;
5353 it->overlay_strings_charpos = -1;
5354 /* If there's an empty display string on the stack, pop the
5355 stack, to resync the bidi iterator with IT's position. Such
5356 empty strings are pushed onto the stack in
5357 get_overlay_strings_1. */
5358 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5359 pop_it (it);
5360
5361 /* If we're at the end of the buffer, record that we have
5362 processed the overlay strings there already, so that
5363 next_element_from_buffer doesn't try it again. */
5364 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5365 it->overlay_strings_at_end_processed_p = 1;
5366 }
5367 else
5368 {
5369 /* There are more overlay strings to process. If
5370 IT->current.overlay_string_index has advanced to a position
5371 where we must load IT->overlay_strings with more strings, do
5372 it. We must load at the IT->overlay_strings_charpos where
5373 IT->n_overlay_strings was originally computed; when invisible
5374 text is present, this might not be IT_CHARPOS (Bug#7016). */
5375 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5376
5377 if (it->current.overlay_string_index && i == 0)
5378 load_overlay_strings (it, it->overlay_strings_charpos);
5379
5380 /* Initialize IT to deliver display elements from the overlay
5381 string. */
5382 it->string = it->overlay_strings[i];
5383 it->multibyte_p = STRING_MULTIBYTE (it->string);
5384 SET_TEXT_POS (it->current.string_pos, 0, 0);
5385 it->method = GET_FROM_STRING;
5386 it->stop_charpos = 0;
5387 it->end_charpos = SCHARS (it->string);
5388 if (it->cmp_it.stop_pos >= 0)
5389 it->cmp_it.stop_pos = 0;
5390 it->prev_stop = 0;
5391 it->base_level_stop = 0;
5392
5393 /* Set up the bidi iterator for this overlay string. */
5394 if (it->bidi_p)
5395 {
5396 it->bidi_it.string.lstring = it->string;
5397 it->bidi_it.string.s = NULL;
5398 it->bidi_it.string.schars = SCHARS (it->string);
5399 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5400 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5401 it->bidi_it.string.unibyte = !it->multibyte_p;
5402 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5403 }
5404 }
5405
5406 CHECK_IT (it);
5407 }
5408
5409
5410 /* Compare two overlay_entry structures E1 and E2. Used as a
5411 comparison function for qsort in load_overlay_strings. Overlay
5412 strings for the same position are sorted so that
5413
5414 1. All after-strings come in front of before-strings, except
5415 when they come from the same overlay.
5416
5417 2. Within after-strings, strings are sorted so that overlay strings
5418 from overlays with higher priorities come first.
5419
5420 2. Within before-strings, strings are sorted so that overlay
5421 strings from overlays with higher priorities come last.
5422
5423 Value is analogous to strcmp. */
5424
5425
5426 static int
5427 compare_overlay_entries (const void *e1, const void *e2)
5428 {
5429 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5430 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5431 int result;
5432
5433 if (entry1->after_string_p != entry2->after_string_p)
5434 {
5435 /* Let after-strings appear in front of before-strings if
5436 they come from different overlays. */
5437 if (EQ (entry1->overlay, entry2->overlay))
5438 result = entry1->after_string_p ? 1 : -1;
5439 else
5440 result = entry1->after_string_p ? -1 : 1;
5441 }
5442 else if (entry1->priority != entry2->priority)
5443 {
5444 if (entry1->after_string_p)
5445 /* After-strings sorted in order of decreasing priority. */
5446 result = entry2->priority < entry1->priority ? -1 : 1;
5447 else
5448 /* Before-strings sorted in order of increasing priority. */
5449 result = entry1->priority < entry2->priority ? -1 : 1;
5450 }
5451 else
5452 result = 0;
5453
5454 return result;
5455 }
5456
5457
5458 /* Load the vector IT->overlay_strings with overlay strings from IT's
5459 current buffer position, or from CHARPOS if that is > 0. Set
5460 IT->n_overlays to the total number of overlay strings found.
5461
5462 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5463 a time. On entry into load_overlay_strings,
5464 IT->current.overlay_string_index gives the number of overlay
5465 strings that have already been loaded by previous calls to this
5466 function.
5467
5468 IT->add_overlay_start contains an additional overlay start
5469 position to consider for taking overlay strings from, if non-zero.
5470 This position comes into play when the overlay has an `invisible'
5471 property, and both before and after-strings. When we've skipped to
5472 the end of the overlay, because of its `invisible' property, we
5473 nevertheless want its before-string to appear.
5474 IT->add_overlay_start will contain the overlay start position
5475 in this case.
5476
5477 Overlay strings are sorted so that after-string strings come in
5478 front of before-string strings. Within before and after-strings,
5479 strings are sorted by overlay priority. See also function
5480 compare_overlay_entries. */
5481
5482 static void
5483 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5484 {
5485 Lisp_Object overlay, window, str, invisible;
5486 struct Lisp_Overlay *ov;
5487 ptrdiff_t start, end;
5488 ptrdiff_t size = 20;
5489 ptrdiff_t n = 0, i, j;
5490 int invis_p;
5491 struct overlay_entry *entries = alloca (size * sizeof *entries);
5492 USE_SAFE_ALLOCA;
5493
5494 if (charpos <= 0)
5495 charpos = IT_CHARPOS (*it);
5496
5497 /* Append the overlay string STRING of overlay OVERLAY to vector
5498 `entries' which has size `size' and currently contains `n'
5499 elements. AFTER_P non-zero means STRING is an after-string of
5500 OVERLAY. */
5501 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5502 do \
5503 { \
5504 Lisp_Object priority; \
5505 \
5506 if (n == size) \
5507 { \
5508 struct overlay_entry *old = entries; \
5509 SAFE_NALLOCA (entries, 2, size); \
5510 memcpy (entries, old, size * sizeof *entries); \
5511 size *= 2; \
5512 } \
5513 \
5514 entries[n].string = (STRING); \
5515 entries[n].overlay = (OVERLAY); \
5516 priority = Foverlay_get ((OVERLAY), Qpriority); \
5517 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5518 entries[n].after_string_p = (AFTER_P); \
5519 ++n; \
5520 } \
5521 while (0)
5522
5523 /* Process overlay before the overlay center. */
5524 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5525 {
5526 XSETMISC (overlay, ov);
5527 eassert (OVERLAYP (overlay));
5528 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5529 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5530
5531 if (end < charpos)
5532 break;
5533
5534 /* Skip this overlay if it doesn't start or end at IT's current
5535 position. */
5536 if (end != charpos && start != charpos)
5537 continue;
5538
5539 /* Skip this overlay if it doesn't apply to IT->w. */
5540 window = Foverlay_get (overlay, Qwindow);
5541 if (WINDOWP (window) && XWINDOW (window) != it->w)
5542 continue;
5543
5544 /* If the text ``under'' the overlay is invisible, both before-
5545 and after-strings from this overlay are visible; start and
5546 end position are indistinguishable. */
5547 invisible = Foverlay_get (overlay, Qinvisible);
5548 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5549
5550 /* If overlay has a non-empty before-string, record it. */
5551 if ((start == charpos || (end == charpos && invis_p))
5552 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5553 && SCHARS (str))
5554 RECORD_OVERLAY_STRING (overlay, str, 0);
5555
5556 /* If overlay has a non-empty after-string, record it. */
5557 if ((end == charpos || (start == charpos && invis_p))
5558 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5559 && SCHARS (str))
5560 RECORD_OVERLAY_STRING (overlay, str, 1);
5561 }
5562
5563 /* Process overlays after the overlay center. */
5564 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5565 {
5566 XSETMISC (overlay, ov);
5567 eassert (OVERLAYP (overlay));
5568 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5569 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5570
5571 if (start > charpos)
5572 break;
5573
5574 /* Skip this overlay if it doesn't start or end at IT's current
5575 position. */
5576 if (end != charpos && start != charpos)
5577 continue;
5578
5579 /* Skip this overlay if it doesn't apply to IT->w. */
5580 window = Foverlay_get (overlay, Qwindow);
5581 if (WINDOWP (window) && XWINDOW (window) != it->w)
5582 continue;
5583
5584 /* If the text ``under'' the overlay is invisible, it has a zero
5585 dimension, and both before- and after-strings apply. */
5586 invisible = Foverlay_get (overlay, Qinvisible);
5587 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5588
5589 /* If overlay has a non-empty before-string, record it. */
5590 if ((start == charpos || (end == charpos && invis_p))
5591 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5592 && SCHARS (str))
5593 RECORD_OVERLAY_STRING (overlay, str, 0);
5594
5595 /* If overlay has a non-empty after-string, record it. */
5596 if ((end == charpos || (start == charpos && invis_p))
5597 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5598 && SCHARS (str))
5599 RECORD_OVERLAY_STRING (overlay, str, 1);
5600 }
5601
5602 #undef RECORD_OVERLAY_STRING
5603
5604 /* Sort entries. */
5605 if (n > 1)
5606 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5607
5608 /* Record number of overlay strings, and where we computed it. */
5609 it->n_overlay_strings = n;
5610 it->overlay_strings_charpos = charpos;
5611
5612 /* IT->current.overlay_string_index is the number of overlay strings
5613 that have already been consumed by IT. Copy some of the
5614 remaining overlay strings to IT->overlay_strings. */
5615 i = 0;
5616 j = it->current.overlay_string_index;
5617 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5618 {
5619 it->overlay_strings[i] = entries[j].string;
5620 it->string_overlays[i++] = entries[j++].overlay;
5621 }
5622
5623 CHECK_IT (it);
5624 SAFE_FREE ();
5625 }
5626
5627
5628 /* Get the first chunk of overlay strings at IT's current buffer
5629 position, or at CHARPOS if that is > 0. Value is non-zero if at
5630 least one overlay string was found. */
5631
5632 static int
5633 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5634 {
5635 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5636 process. This fills IT->overlay_strings with strings, and sets
5637 IT->n_overlay_strings to the total number of strings to process.
5638 IT->pos.overlay_string_index has to be set temporarily to zero
5639 because load_overlay_strings needs this; it must be set to -1
5640 when no overlay strings are found because a zero value would
5641 indicate a position in the first overlay string. */
5642 it->current.overlay_string_index = 0;
5643 load_overlay_strings (it, charpos);
5644
5645 /* If we found overlay strings, set up IT to deliver display
5646 elements from the first one. Otherwise set up IT to deliver
5647 from current_buffer. */
5648 if (it->n_overlay_strings)
5649 {
5650 /* Make sure we know settings in current_buffer, so that we can
5651 restore meaningful values when we're done with the overlay
5652 strings. */
5653 if (compute_stop_p)
5654 compute_stop_pos (it);
5655 eassert (it->face_id >= 0);
5656
5657 /* Save IT's settings. They are restored after all overlay
5658 strings have been processed. */
5659 eassert (!compute_stop_p || it->sp == 0);
5660
5661 /* When called from handle_stop, there might be an empty display
5662 string loaded. In that case, don't bother saving it. But
5663 don't use this optimization with the bidi iterator, since we
5664 need the corresponding pop_it call to resync the bidi
5665 iterator's position with IT's position, after we are done
5666 with the overlay strings. (The corresponding call to pop_it
5667 in case of an empty display string is in
5668 next_overlay_string.) */
5669 if (!(!it->bidi_p
5670 && STRINGP (it->string) && !SCHARS (it->string)))
5671 push_it (it, NULL);
5672
5673 /* Set up IT to deliver display elements from the first overlay
5674 string. */
5675 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5676 it->string = it->overlay_strings[0];
5677 it->from_overlay = Qnil;
5678 it->stop_charpos = 0;
5679 eassert (STRINGP (it->string));
5680 it->end_charpos = SCHARS (it->string);
5681 it->prev_stop = 0;
5682 it->base_level_stop = 0;
5683 it->multibyte_p = STRING_MULTIBYTE (it->string);
5684 it->method = GET_FROM_STRING;
5685 it->from_disp_prop_p = 0;
5686
5687 /* Force paragraph direction to be that of the parent
5688 buffer. */
5689 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5690 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5691 else
5692 it->paragraph_embedding = L2R;
5693
5694 /* Set up the bidi iterator for this overlay string. */
5695 if (it->bidi_p)
5696 {
5697 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5698
5699 it->bidi_it.string.lstring = it->string;
5700 it->bidi_it.string.s = NULL;
5701 it->bidi_it.string.schars = SCHARS (it->string);
5702 it->bidi_it.string.bufpos = pos;
5703 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5704 it->bidi_it.string.unibyte = !it->multibyte_p;
5705 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5706 }
5707 return 1;
5708 }
5709
5710 it->current.overlay_string_index = -1;
5711 return 0;
5712 }
5713
5714 static int
5715 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5716 {
5717 it->string = Qnil;
5718 it->method = GET_FROM_BUFFER;
5719
5720 (void) get_overlay_strings_1 (it, charpos, 1);
5721
5722 CHECK_IT (it);
5723
5724 /* Value is non-zero if we found at least one overlay string. */
5725 return STRINGP (it->string);
5726 }
5727
5728
5729 \f
5730 /***********************************************************************
5731 Saving and restoring state
5732 ***********************************************************************/
5733
5734 /* Save current settings of IT on IT->stack. Called, for example,
5735 before setting up IT for an overlay string, to be able to restore
5736 IT's settings to what they were after the overlay string has been
5737 processed. If POSITION is non-NULL, it is the position to save on
5738 the stack instead of IT->position. */
5739
5740 static void
5741 push_it (struct it *it, struct text_pos *position)
5742 {
5743 struct iterator_stack_entry *p;
5744
5745 eassert (it->sp < IT_STACK_SIZE);
5746 p = it->stack + it->sp;
5747
5748 p->stop_charpos = it->stop_charpos;
5749 p->prev_stop = it->prev_stop;
5750 p->base_level_stop = it->base_level_stop;
5751 p->cmp_it = it->cmp_it;
5752 eassert (it->face_id >= 0);
5753 p->face_id = it->face_id;
5754 p->string = it->string;
5755 p->method = it->method;
5756 p->from_overlay = it->from_overlay;
5757 switch (p->method)
5758 {
5759 case GET_FROM_IMAGE:
5760 p->u.image.object = it->object;
5761 p->u.image.image_id = it->image_id;
5762 p->u.image.slice = it->slice;
5763 break;
5764 case GET_FROM_STRETCH:
5765 p->u.stretch.object = it->object;
5766 break;
5767 #ifdef HAVE_XWIDGETS
5768 case GET_FROM_XWIDGET:
5769 p->u.xwidget.object = it->object;
5770 break;
5771 #endif
5772 }
5773 p->position = position ? *position : it->position;
5774 p->current = it->current;
5775 p->end_charpos = it->end_charpos;
5776 p->string_nchars = it->string_nchars;
5777 p->area = it->area;
5778 p->multibyte_p = it->multibyte_p;
5779 p->avoid_cursor_p = it->avoid_cursor_p;
5780 p->space_width = it->space_width;
5781 p->font_height = it->font_height;
5782 p->voffset = it->voffset;
5783 p->string_from_display_prop_p = it->string_from_display_prop_p;
5784 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5785 p->display_ellipsis_p = 0;
5786 p->line_wrap = it->line_wrap;
5787 p->bidi_p = it->bidi_p;
5788 p->paragraph_embedding = it->paragraph_embedding;
5789 p->from_disp_prop_p = it->from_disp_prop_p;
5790 ++it->sp;
5791
5792 /* Save the state of the bidi iterator as well. */
5793 if (it->bidi_p)
5794 bidi_push_it (&it->bidi_it);
5795 }
5796
5797 static void
5798 iterate_out_of_display_property (struct it *it)
5799 {
5800 int buffer_p = !STRINGP (it->string);
5801 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5802 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5803
5804 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5805
5806 /* Maybe initialize paragraph direction. If we are at the beginning
5807 of a new paragraph, next_element_from_buffer may not have a
5808 chance to do that. */
5809 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5810 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5811 /* prev_stop can be zero, so check against BEGV as well. */
5812 while (it->bidi_it.charpos >= bob
5813 && it->prev_stop <= it->bidi_it.charpos
5814 && it->bidi_it.charpos < CHARPOS (it->position)
5815 && it->bidi_it.charpos < eob)
5816 bidi_move_to_visually_next (&it->bidi_it);
5817 /* Record the stop_pos we just crossed, for when we cross it
5818 back, maybe. */
5819 if (it->bidi_it.charpos > CHARPOS (it->position))
5820 it->prev_stop = CHARPOS (it->position);
5821 /* If we ended up not where pop_it put us, resync IT's
5822 positional members with the bidi iterator. */
5823 if (it->bidi_it.charpos != CHARPOS (it->position))
5824 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5825 if (buffer_p)
5826 it->current.pos = it->position;
5827 else
5828 it->current.string_pos = it->position;
5829 }
5830
5831 /* Restore IT's settings from IT->stack. Called, for example, when no
5832 more overlay strings must be processed, and we return to delivering
5833 display elements from a buffer, or when the end of a string from a
5834 `display' property is reached and we return to delivering display
5835 elements from an overlay string, or from a buffer. */
5836
5837 static void
5838 pop_it (struct it *it)
5839 {
5840 struct iterator_stack_entry *p;
5841 int from_display_prop = it->from_disp_prop_p;
5842
5843 eassert (it->sp > 0);
5844 --it->sp;
5845 p = it->stack + it->sp;
5846 it->stop_charpos = p->stop_charpos;
5847 it->prev_stop = p->prev_stop;
5848 it->base_level_stop = p->base_level_stop;
5849 it->cmp_it = p->cmp_it;
5850 it->face_id = p->face_id;
5851 it->current = p->current;
5852 it->position = p->position;
5853 it->string = p->string;
5854 it->from_overlay = p->from_overlay;
5855 if (NILP (it->string))
5856 SET_TEXT_POS (it->current.string_pos, -1, -1);
5857 it->method = p->method;
5858 switch (it->method)
5859 {
5860 case GET_FROM_IMAGE:
5861 it->image_id = p->u.image.image_id;
5862 it->object = p->u.image.object;
5863 it->slice = p->u.image.slice;
5864 break;
5865 #ifdef HAVE_XWIDGETS
5866 case GET_FROM_XWIDGET:
5867 it->object = p->u.xwidget.object;
5868 break;
5869 #endif
5870 case GET_FROM_STRETCH:
5871 it->object = p->u.stretch.object;
5872 break;
5873 case GET_FROM_BUFFER:
5874 it->object = it->w->contents;
5875 break;
5876 case GET_FROM_STRING:
5877 it->object = it->string;
5878 break;
5879 case GET_FROM_DISPLAY_VECTOR:
5880 if (it->s)
5881 it->method = GET_FROM_C_STRING;
5882 else if (STRINGP (it->string))
5883 it->method = GET_FROM_STRING;
5884 else
5885 {
5886 it->method = GET_FROM_BUFFER;
5887 it->object = it->w->contents;
5888 }
5889 }
5890 it->end_charpos = p->end_charpos;
5891 it->string_nchars = p->string_nchars;
5892 it->area = p->area;
5893 it->multibyte_p = p->multibyte_p;
5894 it->avoid_cursor_p = p->avoid_cursor_p;
5895 it->space_width = p->space_width;
5896 it->font_height = p->font_height;
5897 it->voffset = p->voffset;
5898 it->string_from_display_prop_p = p->string_from_display_prop_p;
5899 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5900 it->line_wrap = p->line_wrap;
5901 it->bidi_p = p->bidi_p;
5902 it->paragraph_embedding = p->paragraph_embedding;
5903 it->from_disp_prop_p = p->from_disp_prop_p;
5904 if (it->bidi_p)
5905 {
5906 bidi_pop_it (&it->bidi_it);
5907 /* Bidi-iterate until we get out of the portion of text, if any,
5908 covered by a `display' text property or by an overlay with
5909 `display' property. (We cannot just jump there, because the
5910 internal coherency of the bidi iterator state can not be
5911 preserved across such jumps.) We also must determine the
5912 paragraph base direction if the overlay we just processed is
5913 at the beginning of a new paragraph. */
5914 if (from_display_prop
5915 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5916 iterate_out_of_display_property (it);
5917
5918 eassert ((BUFFERP (it->object)
5919 && IT_CHARPOS (*it) == it->bidi_it.charpos
5920 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5921 || (STRINGP (it->object)
5922 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5923 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5924 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5925 }
5926 }
5927
5928
5929 \f
5930 /***********************************************************************
5931 Moving over lines
5932 ***********************************************************************/
5933
5934 /* Set IT's current position to the previous line start. */
5935
5936 static void
5937 back_to_previous_line_start (struct it *it)
5938 {
5939 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5940
5941 DEC_BOTH (cp, bp);
5942 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5943 }
5944
5945
5946 /* Move IT to the next line start.
5947
5948 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5949 we skipped over part of the text (as opposed to moving the iterator
5950 continuously over the text). Otherwise, don't change the value
5951 of *SKIPPED_P.
5952
5953 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5954 iterator on the newline, if it was found.
5955
5956 Newlines may come from buffer text, overlay strings, or strings
5957 displayed via the `display' property. That's the reason we can't
5958 simply use find_newline_no_quit.
5959
5960 Note that this function may not skip over invisible text that is so
5961 because of text properties and immediately follows a newline. If
5962 it would, function reseat_at_next_visible_line_start, when called
5963 from set_iterator_to_next, would effectively make invisible
5964 characters following a newline part of the wrong glyph row, which
5965 leads to wrong cursor motion. */
5966
5967 static int
5968 forward_to_next_line_start (struct it *it, int *skipped_p,
5969 struct bidi_it *bidi_it_prev)
5970 {
5971 ptrdiff_t old_selective;
5972 int newline_found_p, n;
5973 const int MAX_NEWLINE_DISTANCE = 500;
5974
5975 /* If already on a newline, just consume it to avoid unintended
5976 skipping over invisible text below. */
5977 if (it->what == IT_CHARACTER
5978 && it->c == '\n'
5979 && CHARPOS (it->position) == IT_CHARPOS (*it))
5980 {
5981 if (it->bidi_p && bidi_it_prev)
5982 *bidi_it_prev = it->bidi_it;
5983 set_iterator_to_next (it, 0);
5984 it->c = 0;
5985 return 1;
5986 }
5987
5988 /* Don't handle selective display in the following. It's (a)
5989 unnecessary because it's done by the caller, and (b) leads to an
5990 infinite recursion because next_element_from_ellipsis indirectly
5991 calls this function. */
5992 old_selective = it->selective;
5993 it->selective = 0;
5994
5995 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5996 from buffer text. */
5997 for (n = newline_found_p = 0;
5998 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5999 n += STRINGP (it->string) ? 0 : 1)
6000 {
6001 if (!get_next_display_element (it))
6002 return 0;
6003 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6004 if (newline_found_p && it->bidi_p && bidi_it_prev)
6005 *bidi_it_prev = it->bidi_it;
6006 set_iterator_to_next (it, 0);
6007 }
6008
6009 /* If we didn't find a newline near enough, see if we can use a
6010 short-cut. */
6011 if (!newline_found_p)
6012 {
6013 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6014 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6015 1, &bytepos);
6016 Lisp_Object pos;
6017
6018 eassert (!STRINGP (it->string));
6019
6020 /* If there isn't any `display' property in sight, and no
6021 overlays, we can just use the position of the newline in
6022 buffer text. */
6023 if (it->stop_charpos >= limit
6024 || ((pos = Fnext_single_property_change (make_number (start),
6025 Qdisplay, Qnil,
6026 make_number (limit)),
6027 NILP (pos))
6028 && next_overlay_change (start) == ZV))
6029 {
6030 if (!it->bidi_p)
6031 {
6032 IT_CHARPOS (*it) = limit;
6033 IT_BYTEPOS (*it) = bytepos;
6034 }
6035 else
6036 {
6037 struct bidi_it bprev;
6038
6039 /* Help bidi.c avoid expensive searches for display
6040 properties and overlays, by telling it that there are
6041 none up to `limit'. */
6042 if (it->bidi_it.disp_pos < limit)
6043 {
6044 it->bidi_it.disp_pos = limit;
6045 it->bidi_it.disp_prop = 0;
6046 }
6047 do {
6048 bprev = it->bidi_it;
6049 bidi_move_to_visually_next (&it->bidi_it);
6050 } while (it->bidi_it.charpos != limit);
6051 IT_CHARPOS (*it) = limit;
6052 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6053 if (bidi_it_prev)
6054 *bidi_it_prev = bprev;
6055 }
6056 *skipped_p = newline_found_p = 1;
6057 }
6058 else
6059 {
6060 while (get_next_display_element (it)
6061 && !newline_found_p)
6062 {
6063 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6064 if (newline_found_p && it->bidi_p && bidi_it_prev)
6065 *bidi_it_prev = it->bidi_it;
6066 set_iterator_to_next (it, 0);
6067 }
6068 }
6069 }
6070
6071 it->selective = old_selective;
6072 return newline_found_p;
6073 }
6074
6075
6076 /* Set IT's current position to the previous visible line start. Skip
6077 invisible text that is so either due to text properties or due to
6078 selective display. Caution: this does not change IT->current_x and
6079 IT->hpos. */
6080
6081 static void
6082 back_to_previous_visible_line_start (struct it *it)
6083 {
6084 while (IT_CHARPOS (*it) > BEGV)
6085 {
6086 back_to_previous_line_start (it);
6087
6088 if (IT_CHARPOS (*it) <= BEGV)
6089 break;
6090
6091 /* If selective > 0, then lines indented more than its value are
6092 invisible. */
6093 if (it->selective > 0
6094 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6095 it->selective))
6096 continue;
6097
6098 /* Check the newline before point for invisibility. */
6099 {
6100 Lisp_Object prop;
6101 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6102 Qinvisible, it->window);
6103 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6104 continue;
6105 }
6106
6107 if (IT_CHARPOS (*it) <= BEGV)
6108 break;
6109
6110 {
6111 struct it it2;
6112 void *it2data = NULL;
6113 ptrdiff_t pos;
6114 ptrdiff_t beg, end;
6115 Lisp_Object val, overlay;
6116
6117 SAVE_IT (it2, *it, it2data);
6118
6119 /* If newline is part of a composition, continue from start of composition */
6120 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6121 && beg < IT_CHARPOS (*it))
6122 goto replaced;
6123
6124 /* If newline is replaced by a display property, find start of overlay
6125 or interval and continue search from that point. */
6126 pos = --IT_CHARPOS (it2);
6127 --IT_BYTEPOS (it2);
6128 it2.sp = 0;
6129 bidi_unshelve_cache (NULL, 0);
6130 it2.string_from_display_prop_p = 0;
6131 it2.from_disp_prop_p = 0;
6132 if (handle_display_prop (&it2) == HANDLED_RETURN
6133 && !NILP (val = get_char_property_and_overlay
6134 (make_number (pos), Qdisplay, Qnil, &overlay))
6135 && (OVERLAYP (overlay)
6136 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6137 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6138 {
6139 RESTORE_IT (it, it, it2data);
6140 goto replaced;
6141 }
6142
6143 /* Newline is not replaced by anything -- so we are done. */
6144 RESTORE_IT (it, it, it2data);
6145 break;
6146
6147 replaced:
6148 if (beg < BEGV)
6149 beg = BEGV;
6150 IT_CHARPOS (*it) = beg;
6151 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6152 }
6153 }
6154
6155 it->continuation_lines_width = 0;
6156
6157 eassert (IT_CHARPOS (*it) >= BEGV);
6158 eassert (IT_CHARPOS (*it) == BEGV
6159 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6160 CHECK_IT (it);
6161 }
6162
6163
6164 /* Reseat iterator IT at the previous visible line start. Skip
6165 invisible text that is so either due to text properties or due to
6166 selective display. At the end, update IT's overlay information,
6167 face information etc. */
6168
6169 void
6170 reseat_at_previous_visible_line_start (struct it *it)
6171 {
6172 back_to_previous_visible_line_start (it);
6173 reseat (it, it->current.pos, 1);
6174 CHECK_IT (it);
6175 }
6176
6177
6178 /* Reseat iterator IT on the next visible line start in the current
6179 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6180 preceding the line start. Skip over invisible text that is so
6181 because of selective display. Compute faces, overlays etc at the
6182 new position. Note that this function does not skip over text that
6183 is invisible because of text properties. */
6184
6185 static void
6186 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6187 {
6188 int newline_found_p, skipped_p = 0;
6189 struct bidi_it bidi_it_prev;
6190
6191 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6192
6193 /* Skip over lines that are invisible because they are indented
6194 more than the value of IT->selective. */
6195 if (it->selective > 0)
6196 while (IT_CHARPOS (*it) < ZV
6197 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6198 it->selective))
6199 {
6200 eassert (IT_BYTEPOS (*it) == BEGV
6201 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6202 newline_found_p =
6203 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6204 }
6205
6206 /* Position on the newline if that's what's requested. */
6207 if (on_newline_p && newline_found_p)
6208 {
6209 if (STRINGP (it->string))
6210 {
6211 if (IT_STRING_CHARPOS (*it) > 0)
6212 {
6213 if (!it->bidi_p)
6214 {
6215 --IT_STRING_CHARPOS (*it);
6216 --IT_STRING_BYTEPOS (*it);
6217 }
6218 else
6219 {
6220 /* We need to restore the bidi iterator to the state
6221 it had on the newline, and resync the IT's
6222 position with that. */
6223 it->bidi_it = bidi_it_prev;
6224 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6225 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6226 }
6227 }
6228 }
6229 else if (IT_CHARPOS (*it) > BEGV)
6230 {
6231 if (!it->bidi_p)
6232 {
6233 --IT_CHARPOS (*it);
6234 --IT_BYTEPOS (*it);
6235 }
6236 else
6237 {
6238 /* We need to restore the bidi iterator to the state it
6239 had on the newline and resync IT with that. */
6240 it->bidi_it = bidi_it_prev;
6241 IT_CHARPOS (*it) = it->bidi_it.charpos;
6242 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6243 }
6244 reseat (it, it->current.pos, 0);
6245 }
6246 }
6247 else if (skipped_p)
6248 reseat (it, it->current.pos, 0);
6249
6250 CHECK_IT (it);
6251 }
6252
6253
6254 \f
6255 /***********************************************************************
6256 Changing an iterator's position
6257 ***********************************************************************/
6258
6259 /* Change IT's current position to POS in current_buffer. If FORCE_P
6260 is non-zero, always check for text properties at the new position.
6261 Otherwise, text properties are only looked up if POS >=
6262 IT->check_charpos of a property. */
6263
6264 static void
6265 reseat (struct it *it, struct text_pos pos, int force_p)
6266 {
6267 ptrdiff_t original_pos = IT_CHARPOS (*it);
6268
6269 reseat_1 (it, pos, 0);
6270
6271 /* Determine where to check text properties. Avoid doing it
6272 where possible because text property lookup is very expensive. */
6273 if (force_p
6274 || CHARPOS (pos) > it->stop_charpos
6275 || CHARPOS (pos) < original_pos)
6276 {
6277 if (it->bidi_p)
6278 {
6279 /* For bidi iteration, we need to prime prev_stop and
6280 base_level_stop with our best estimations. */
6281 /* Implementation note: Of course, POS is not necessarily a
6282 stop position, so assigning prev_pos to it is a lie; we
6283 should have called compute_stop_backwards. However, if
6284 the current buffer does not include any R2L characters,
6285 that call would be a waste of cycles, because the
6286 iterator will never move back, and thus never cross this
6287 "fake" stop position. So we delay that backward search
6288 until the time we really need it, in next_element_from_buffer. */
6289 if (CHARPOS (pos) != it->prev_stop)
6290 it->prev_stop = CHARPOS (pos);
6291 if (CHARPOS (pos) < it->base_level_stop)
6292 it->base_level_stop = 0; /* meaning it's unknown */
6293 handle_stop (it);
6294 }
6295 else
6296 {
6297 handle_stop (it);
6298 it->prev_stop = it->base_level_stop = 0;
6299 }
6300
6301 }
6302
6303 CHECK_IT (it);
6304 }
6305
6306
6307 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6308 IT->stop_pos to POS, also. */
6309
6310 static void
6311 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6312 {
6313 /* Don't call this function when scanning a C string. */
6314 eassert (it->s == NULL);
6315
6316 /* POS must be a reasonable value. */
6317 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6318
6319 it->current.pos = it->position = pos;
6320 it->end_charpos = ZV;
6321 it->dpvec = NULL;
6322 it->current.dpvec_index = -1;
6323 it->current.overlay_string_index = -1;
6324 IT_STRING_CHARPOS (*it) = -1;
6325 IT_STRING_BYTEPOS (*it) = -1;
6326 it->string = Qnil;
6327 it->method = GET_FROM_BUFFER;
6328 it->object = it->w->contents;
6329 it->area = TEXT_AREA;
6330 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6331 it->sp = 0;
6332 it->string_from_display_prop_p = 0;
6333 it->string_from_prefix_prop_p = 0;
6334
6335 it->from_disp_prop_p = 0;
6336 it->face_before_selective_p = 0;
6337 if (it->bidi_p)
6338 {
6339 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6340 &it->bidi_it);
6341 bidi_unshelve_cache (NULL, 0);
6342 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6343 it->bidi_it.string.s = NULL;
6344 it->bidi_it.string.lstring = Qnil;
6345 it->bidi_it.string.bufpos = 0;
6346 it->bidi_it.string.unibyte = 0;
6347 }
6348
6349 if (set_stop_p)
6350 {
6351 it->stop_charpos = CHARPOS (pos);
6352 it->base_level_stop = CHARPOS (pos);
6353 }
6354 /* This make the information stored in it->cmp_it invalidate. */
6355 it->cmp_it.id = -1;
6356 }
6357
6358
6359 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6360 If S is non-null, it is a C string to iterate over. Otherwise,
6361 STRING gives a Lisp string to iterate over.
6362
6363 If PRECISION > 0, don't return more then PRECISION number of
6364 characters from the string.
6365
6366 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6367 characters have been returned. FIELD_WIDTH < 0 means an infinite
6368 field width.
6369
6370 MULTIBYTE = 0 means disable processing of multibyte characters,
6371 MULTIBYTE > 0 means enable it,
6372 MULTIBYTE < 0 means use IT->multibyte_p.
6373
6374 IT must be initialized via a prior call to init_iterator before
6375 calling this function. */
6376
6377 static void
6378 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6379 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6380 int multibyte)
6381 {
6382 /* No region in strings. */
6383 it->region_beg_charpos = it->region_end_charpos = -1;
6384
6385 /* No text property checks performed by default, but see below. */
6386 it->stop_charpos = -1;
6387
6388 /* Set iterator position and end position. */
6389 memset (&it->current, 0, sizeof it->current);
6390 it->current.overlay_string_index = -1;
6391 it->current.dpvec_index = -1;
6392 eassert (charpos >= 0);
6393
6394 /* If STRING is specified, use its multibyteness, otherwise use the
6395 setting of MULTIBYTE, if specified. */
6396 if (multibyte >= 0)
6397 it->multibyte_p = multibyte > 0;
6398
6399 /* Bidirectional reordering of strings is controlled by the default
6400 value of bidi-display-reordering. Don't try to reorder while
6401 loading loadup.el, as the necessary character property tables are
6402 not yet available. */
6403 it->bidi_p =
6404 NILP (Vpurify_flag)
6405 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6406
6407 if (s == NULL)
6408 {
6409 eassert (STRINGP (string));
6410 it->string = string;
6411 it->s = NULL;
6412 it->end_charpos = it->string_nchars = SCHARS (string);
6413 it->method = GET_FROM_STRING;
6414 it->current.string_pos = string_pos (charpos, string);
6415
6416 if (it->bidi_p)
6417 {
6418 it->bidi_it.string.lstring = string;
6419 it->bidi_it.string.s = NULL;
6420 it->bidi_it.string.schars = it->end_charpos;
6421 it->bidi_it.string.bufpos = 0;
6422 it->bidi_it.string.from_disp_str = 0;
6423 it->bidi_it.string.unibyte = !it->multibyte_p;
6424 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6425 FRAME_WINDOW_P (it->f), &it->bidi_it);
6426 }
6427 }
6428 else
6429 {
6430 it->s = (const unsigned char *) s;
6431 it->string = Qnil;
6432
6433 /* Note that we use IT->current.pos, not it->current.string_pos,
6434 for displaying C strings. */
6435 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6436 if (it->multibyte_p)
6437 {
6438 it->current.pos = c_string_pos (charpos, s, 1);
6439 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6440 }
6441 else
6442 {
6443 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6444 it->end_charpos = it->string_nchars = strlen (s);
6445 }
6446
6447 if (it->bidi_p)
6448 {
6449 it->bidi_it.string.lstring = Qnil;
6450 it->bidi_it.string.s = (const unsigned char *) s;
6451 it->bidi_it.string.schars = it->end_charpos;
6452 it->bidi_it.string.bufpos = 0;
6453 it->bidi_it.string.from_disp_str = 0;
6454 it->bidi_it.string.unibyte = !it->multibyte_p;
6455 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6456 &it->bidi_it);
6457 }
6458 it->method = GET_FROM_C_STRING;
6459 }
6460
6461 /* PRECISION > 0 means don't return more than PRECISION characters
6462 from the string. */
6463 if (precision > 0 && it->end_charpos - charpos > precision)
6464 {
6465 it->end_charpos = it->string_nchars = charpos + precision;
6466 if (it->bidi_p)
6467 it->bidi_it.string.schars = it->end_charpos;
6468 }
6469
6470 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6471 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6472 FIELD_WIDTH < 0 means infinite field width. This is useful for
6473 padding with `-' at the end of a mode line. */
6474 if (field_width < 0)
6475 field_width = INFINITY;
6476 /* Implementation note: We deliberately don't enlarge
6477 it->bidi_it.string.schars here to fit it->end_charpos, because
6478 the bidi iterator cannot produce characters out of thin air. */
6479 if (field_width > it->end_charpos - charpos)
6480 it->end_charpos = charpos + field_width;
6481
6482 /* Use the standard display table for displaying strings. */
6483 if (DISP_TABLE_P (Vstandard_display_table))
6484 it->dp = XCHAR_TABLE (Vstandard_display_table);
6485
6486 it->stop_charpos = charpos;
6487 it->prev_stop = charpos;
6488 it->base_level_stop = 0;
6489 if (it->bidi_p)
6490 {
6491 it->bidi_it.first_elt = 1;
6492 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6493 it->bidi_it.disp_pos = -1;
6494 }
6495 if (s == NULL && it->multibyte_p)
6496 {
6497 ptrdiff_t endpos = SCHARS (it->string);
6498 if (endpos > it->end_charpos)
6499 endpos = it->end_charpos;
6500 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6501 it->string);
6502 }
6503 CHECK_IT (it);
6504 }
6505
6506
6507 \f
6508 /***********************************************************************
6509 Iteration
6510 ***********************************************************************/
6511
6512 /* Map enum it_method value to corresponding next_element_from_* function. */
6513
6514 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6515 {
6516 next_element_from_buffer,
6517 next_element_from_display_vector,
6518 next_element_from_string,
6519 next_element_from_c_string,
6520 next_element_from_image,
6521 next_element_from_stretch
6522 #ifdef HAVE_XWIDGETS
6523 ,next_element_from_xwidget
6524 #endif
6525 };
6526
6527 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6528
6529
6530 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6531 (possibly with the following characters). */
6532
6533 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6534 ((IT)->cmp_it.id >= 0 \
6535 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6536 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6537 END_CHARPOS, (IT)->w, \
6538 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6539 (IT)->string)))
6540
6541
6542 /* Lookup the char-table Vglyphless_char_display for character C (-1
6543 if we want information for no-font case), and return the display
6544 method symbol. By side-effect, update it->what and
6545 it->glyphless_method. This function is called from
6546 get_next_display_element for each character element, and from
6547 x_produce_glyphs when no suitable font was found. */
6548
6549 Lisp_Object
6550 lookup_glyphless_char_display (int c, struct it *it)
6551 {
6552 Lisp_Object glyphless_method = Qnil;
6553
6554 if (CHAR_TABLE_P (Vglyphless_char_display)
6555 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6556 {
6557 if (c >= 0)
6558 {
6559 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6560 if (CONSP (glyphless_method))
6561 glyphless_method = FRAME_WINDOW_P (it->f)
6562 ? XCAR (glyphless_method)
6563 : XCDR (glyphless_method);
6564 }
6565 else
6566 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6567 }
6568
6569 retry:
6570 if (NILP (glyphless_method))
6571 {
6572 if (c >= 0)
6573 /* The default is to display the character by a proper font. */
6574 return Qnil;
6575 /* The default for the no-font case is to display an empty box. */
6576 glyphless_method = Qempty_box;
6577 }
6578 if (EQ (glyphless_method, Qzero_width))
6579 {
6580 if (c >= 0)
6581 return glyphless_method;
6582 /* This method can't be used for the no-font case. */
6583 glyphless_method = Qempty_box;
6584 }
6585 if (EQ (glyphless_method, Qthin_space))
6586 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6587 else if (EQ (glyphless_method, Qempty_box))
6588 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6589 else if (EQ (glyphless_method, Qhex_code))
6590 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6591 else if (STRINGP (glyphless_method))
6592 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6593 else
6594 {
6595 /* Invalid value. We use the default method. */
6596 glyphless_method = Qnil;
6597 goto retry;
6598 }
6599 it->what = IT_GLYPHLESS;
6600 return glyphless_method;
6601 }
6602
6603 /* Load IT's display element fields with information about the next
6604 display element from the current position of IT. Value is zero if
6605 end of buffer (or C string) is reached. */
6606
6607 static struct frame *last_escape_glyph_frame = NULL;
6608 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6609 static int last_escape_glyph_merged_face_id = 0;
6610
6611 struct frame *last_glyphless_glyph_frame = NULL;
6612 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6613 int last_glyphless_glyph_merged_face_id = 0;
6614
6615 static int
6616 get_next_display_element (struct it *it)
6617 {
6618 /* Non-zero means that we found a display element. Zero means that
6619 we hit the end of what we iterate over. Performance note: the
6620 function pointer `method' used here turns out to be faster than
6621 using a sequence of if-statements. */
6622 int success_p;
6623
6624 get_next:
6625 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6626
6627 if (it->what == IT_CHARACTER)
6628 {
6629 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6630 and only if (a) the resolved directionality of that character
6631 is R..." */
6632 /* FIXME: Do we need an exception for characters from display
6633 tables? */
6634 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6635 it->c = bidi_mirror_char (it->c);
6636 /* Map via display table or translate control characters.
6637 IT->c, IT->len etc. have been set to the next character by
6638 the function call above. If we have a display table, and it
6639 contains an entry for IT->c, translate it. Don't do this if
6640 IT->c itself comes from a display table, otherwise we could
6641 end up in an infinite recursion. (An alternative could be to
6642 count the recursion depth of this function and signal an
6643 error when a certain maximum depth is reached.) Is it worth
6644 it? */
6645 if (success_p && it->dpvec == NULL)
6646 {
6647 Lisp_Object dv;
6648 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6649 int nonascii_space_p = 0;
6650 int nonascii_hyphen_p = 0;
6651 int c = it->c; /* This is the character to display. */
6652
6653 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6654 {
6655 eassert (SINGLE_BYTE_CHAR_P (c));
6656 if (unibyte_display_via_language_environment)
6657 {
6658 c = DECODE_CHAR (unibyte, c);
6659 if (c < 0)
6660 c = BYTE8_TO_CHAR (it->c);
6661 }
6662 else
6663 c = BYTE8_TO_CHAR (it->c);
6664 }
6665
6666 if (it->dp
6667 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6668 VECTORP (dv)))
6669 {
6670 struct Lisp_Vector *v = XVECTOR (dv);
6671
6672 /* Return the first character from the display table
6673 entry, if not empty. If empty, don't display the
6674 current character. */
6675 if (v->header.size)
6676 {
6677 it->dpvec_char_len = it->len;
6678 it->dpvec = v->contents;
6679 it->dpend = v->contents + v->header.size;
6680 it->current.dpvec_index = 0;
6681 it->dpvec_face_id = -1;
6682 it->saved_face_id = it->face_id;
6683 it->method = GET_FROM_DISPLAY_VECTOR;
6684 it->ellipsis_p = 0;
6685 }
6686 else
6687 {
6688 set_iterator_to_next (it, 0);
6689 }
6690 goto get_next;
6691 }
6692
6693 if (! NILP (lookup_glyphless_char_display (c, it)))
6694 {
6695 if (it->what == IT_GLYPHLESS)
6696 goto done;
6697 /* Don't display this character. */
6698 set_iterator_to_next (it, 0);
6699 goto get_next;
6700 }
6701
6702 /* If `nobreak-char-display' is non-nil, we display
6703 non-ASCII spaces and hyphens specially. */
6704 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6705 {
6706 if (c == 0xA0)
6707 nonascii_space_p = 1;
6708 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6709 nonascii_hyphen_p = 1;
6710 }
6711
6712 /* Translate control characters into `\003' or `^C' form.
6713 Control characters coming from a display table entry are
6714 currently not translated because we use IT->dpvec to hold
6715 the translation. This could easily be changed but I
6716 don't believe that it is worth doing.
6717
6718 The characters handled by `nobreak-char-display' must be
6719 translated too.
6720
6721 Non-printable characters and raw-byte characters are also
6722 translated to octal form. */
6723 if (((c < ' ' || c == 127) /* ASCII control chars */
6724 ? (it->area != TEXT_AREA
6725 /* In mode line, treat \n, \t like other crl chars. */
6726 || (c != '\t'
6727 && it->glyph_row
6728 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6729 || (c != '\n' && c != '\t'))
6730 : (nonascii_space_p
6731 || nonascii_hyphen_p
6732 || CHAR_BYTE8_P (c)
6733 || ! CHAR_PRINTABLE_P (c))))
6734 {
6735 /* C is a control character, non-ASCII space/hyphen,
6736 raw-byte, or a non-printable character which must be
6737 displayed either as '\003' or as `^C' where the '\\'
6738 and '^' can be defined in the display table. Fill
6739 IT->ctl_chars with glyphs for what we have to
6740 display. Then, set IT->dpvec to these glyphs. */
6741 Lisp_Object gc;
6742 int ctl_len;
6743 int face_id;
6744 int lface_id = 0;
6745 int escape_glyph;
6746
6747 /* Handle control characters with ^. */
6748
6749 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6750 {
6751 int g;
6752
6753 g = '^'; /* default glyph for Control */
6754 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6755 if (it->dp
6756 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6757 {
6758 g = GLYPH_CODE_CHAR (gc);
6759 lface_id = GLYPH_CODE_FACE (gc);
6760 }
6761 if (lface_id)
6762 {
6763 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6764 }
6765 else if (it->f == last_escape_glyph_frame
6766 && it->face_id == last_escape_glyph_face_id)
6767 {
6768 face_id = last_escape_glyph_merged_face_id;
6769 }
6770 else
6771 {
6772 /* Merge the escape-glyph face into the current face. */
6773 face_id = merge_faces (it->f, Qescape_glyph, 0,
6774 it->face_id);
6775 last_escape_glyph_frame = it->f;
6776 last_escape_glyph_face_id = it->face_id;
6777 last_escape_glyph_merged_face_id = face_id;
6778 }
6779
6780 XSETINT (it->ctl_chars[0], g);
6781 XSETINT (it->ctl_chars[1], c ^ 0100);
6782 ctl_len = 2;
6783 goto display_control;
6784 }
6785
6786 /* Handle non-ascii space in the mode where it only gets
6787 highlighting. */
6788
6789 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6790 {
6791 /* Merge `nobreak-space' into the current face. */
6792 face_id = merge_faces (it->f, Qnobreak_space, 0,
6793 it->face_id);
6794 XSETINT (it->ctl_chars[0], ' ');
6795 ctl_len = 1;
6796 goto display_control;
6797 }
6798
6799 /* Handle sequences that start with the "escape glyph". */
6800
6801 /* the default escape glyph is \. */
6802 escape_glyph = '\\';
6803
6804 if (it->dp
6805 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6806 {
6807 escape_glyph = GLYPH_CODE_CHAR (gc);
6808 lface_id = GLYPH_CODE_FACE (gc);
6809 }
6810 if (lface_id)
6811 {
6812 /* The display table specified a face.
6813 Merge it into face_id and also into escape_glyph. */
6814 face_id = merge_faces (it->f, Qt, lface_id,
6815 it->face_id);
6816 }
6817 else if (it->f == last_escape_glyph_frame
6818 && it->face_id == last_escape_glyph_face_id)
6819 {
6820 face_id = last_escape_glyph_merged_face_id;
6821 }
6822 else
6823 {
6824 /* Merge the escape-glyph face into the current face. */
6825 face_id = merge_faces (it->f, Qescape_glyph, 0,
6826 it->face_id);
6827 last_escape_glyph_frame = it->f;
6828 last_escape_glyph_face_id = it->face_id;
6829 last_escape_glyph_merged_face_id = face_id;
6830 }
6831
6832 /* Draw non-ASCII hyphen with just highlighting: */
6833
6834 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6835 {
6836 XSETINT (it->ctl_chars[0], '-');
6837 ctl_len = 1;
6838 goto display_control;
6839 }
6840
6841 /* Draw non-ASCII space/hyphen with escape glyph: */
6842
6843 if (nonascii_space_p || nonascii_hyphen_p)
6844 {
6845 XSETINT (it->ctl_chars[0], escape_glyph);
6846 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6847 ctl_len = 2;
6848 goto display_control;
6849 }
6850
6851 {
6852 char str[10];
6853 int len, i;
6854
6855 if (CHAR_BYTE8_P (c))
6856 /* Display \200 instead of \17777600. */
6857 c = CHAR_TO_BYTE8 (c);
6858 len = sprintf (str, "%03o", c);
6859
6860 XSETINT (it->ctl_chars[0], escape_glyph);
6861 for (i = 0; i < len; i++)
6862 XSETINT (it->ctl_chars[i + 1], str[i]);
6863 ctl_len = len + 1;
6864 }
6865
6866 display_control:
6867 /* Set up IT->dpvec and return first character from it. */
6868 it->dpvec_char_len = it->len;
6869 it->dpvec = it->ctl_chars;
6870 it->dpend = it->dpvec + ctl_len;
6871 it->current.dpvec_index = 0;
6872 it->dpvec_face_id = face_id;
6873 it->saved_face_id = it->face_id;
6874 it->method = GET_FROM_DISPLAY_VECTOR;
6875 it->ellipsis_p = 0;
6876 goto get_next;
6877 }
6878 it->char_to_display = c;
6879 }
6880 else if (success_p)
6881 {
6882 it->char_to_display = it->c;
6883 }
6884 }
6885
6886 /* Adjust face id for a multibyte character. There are no multibyte
6887 character in unibyte text. */
6888 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6889 && it->multibyte_p
6890 && success_p
6891 && FRAME_WINDOW_P (it->f))
6892 {
6893 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6894
6895 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6896 {
6897 /* Automatic composition with glyph-string. */
6898 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6899
6900 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6901 }
6902 else
6903 {
6904 ptrdiff_t pos = (it->s ? -1
6905 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6906 : IT_CHARPOS (*it));
6907 int c;
6908
6909 if (it->what == IT_CHARACTER)
6910 c = it->char_to_display;
6911 else
6912 {
6913 struct composition *cmp = composition_table[it->cmp_it.id];
6914 int i;
6915
6916 c = ' ';
6917 for (i = 0; i < cmp->glyph_len; i++)
6918 /* TAB in a composition means display glyphs with
6919 padding space on the left or right. */
6920 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6921 break;
6922 }
6923 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6924 }
6925 }
6926
6927 done:
6928 /* Is this character the last one of a run of characters with
6929 box? If yes, set IT->end_of_box_run_p to 1. */
6930 if (it->face_box_p
6931 && it->s == NULL)
6932 {
6933 if (it->method == GET_FROM_STRING && it->sp)
6934 {
6935 int face_id = underlying_face_id (it);
6936 struct face *face = FACE_FROM_ID (it->f, face_id);
6937
6938 if (face)
6939 {
6940 if (face->box == FACE_NO_BOX)
6941 {
6942 /* If the box comes from face properties in a
6943 display string, check faces in that string. */
6944 int string_face_id = face_after_it_pos (it);
6945 it->end_of_box_run_p
6946 = (FACE_FROM_ID (it->f, string_face_id)->box
6947 == FACE_NO_BOX);
6948 }
6949 /* Otherwise, the box comes from the underlying face.
6950 If this is the last string character displayed, check
6951 the next buffer location. */
6952 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6953 && (it->current.overlay_string_index
6954 == it->n_overlay_strings - 1))
6955 {
6956 ptrdiff_t ignore;
6957 int next_face_id;
6958 struct text_pos pos = it->current.pos;
6959 INC_TEXT_POS (pos, it->multibyte_p);
6960
6961 next_face_id = face_at_buffer_position
6962 (it->w, CHARPOS (pos), it->region_beg_charpos,
6963 it->region_end_charpos, &ignore,
6964 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6965 -1);
6966 it->end_of_box_run_p
6967 = (FACE_FROM_ID (it->f, next_face_id)->box
6968 == FACE_NO_BOX);
6969 }
6970 }
6971 }
6972 else
6973 {
6974 int face_id = face_after_it_pos (it);
6975 it->end_of_box_run_p
6976 = (face_id != it->face_id
6977 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6978 }
6979 }
6980 /* If we reached the end of the object we've been iterating (e.g., a
6981 display string or an overlay string), and there's something on
6982 IT->stack, proceed with what's on the stack. It doesn't make
6983 sense to return zero if there's unprocessed stuff on the stack,
6984 because otherwise that stuff will never be displayed. */
6985 if (!success_p && it->sp > 0)
6986 {
6987 set_iterator_to_next (it, 0);
6988 success_p = get_next_display_element (it);
6989 }
6990
6991 /* Value is 0 if end of buffer or string reached. */
6992 return success_p;
6993 }
6994
6995
6996 /* Move IT to the next display element.
6997
6998 RESEAT_P non-zero means if called on a newline in buffer text,
6999 skip to the next visible line start.
7000
7001 Functions get_next_display_element and set_iterator_to_next are
7002 separate because I find this arrangement easier to handle than a
7003 get_next_display_element function that also increments IT's
7004 position. The way it is we can first look at an iterator's current
7005 display element, decide whether it fits on a line, and if it does,
7006 increment the iterator position. The other way around we probably
7007 would either need a flag indicating whether the iterator has to be
7008 incremented the next time, or we would have to implement a
7009 decrement position function which would not be easy to write. */
7010
7011 void
7012 set_iterator_to_next (struct it *it, int reseat_p)
7013 {
7014 /* Reset flags indicating start and end of a sequence of characters
7015 with box. Reset them at the start of this function because
7016 moving the iterator to a new position might set them. */
7017 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7018
7019 switch (it->method)
7020 {
7021 case GET_FROM_BUFFER:
7022 /* The current display element of IT is a character from
7023 current_buffer. Advance in the buffer, and maybe skip over
7024 invisible lines that are so because of selective display. */
7025 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7026 reseat_at_next_visible_line_start (it, 0);
7027 else if (it->cmp_it.id >= 0)
7028 {
7029 /* We are currently getting glyphs from a composition. */
7030 int i;
7031
7032 if (! it->bidi_p)
7033 {
7034 IT_CHARPOS (*it) += it->cmp_it.nchars;
7035 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7036 if (it->cmp_it.to < it->cmp_it.nglyphs)
7037 {
7038 it->cmp_it.from = it->cmp_it.to;
7039 }
7040 else
7041 {
7042 it->cmp_it.id = -1;
7043 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7044 IT_BYTEPOS (*it),
7045 it->end_charpos, Qnil);
7046 }
7047 }
7048 else if (! it->cmp_it.reversed_p)
7049 {
7050 /* Composition created while scanning forward. */
7051 /* Update IT's char/byte positions to point to the first
7052 character of the next grapheme cluster, or to the
7053 character visually after the current composition. */
7054 for (i = 0; i < it->cmp_it.nchars; i++)
7055 bidi_move_to_visually_next (&it->bidi_it);
7056 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7057 IT_CHARPOS (*it) = it->bidi_it.charpos;
7058
7059 if (it->cmp_it.to < it->cmp_it.nglyphs)
7060 {
7061 /* Proceed to the next grapheme cluster. */
7062 it->cmp_it.from = it->cmp_it.to;
7063 }
7064 else
7065 {
7066 /* No more grapheme clusters in this composition.
7067 Find the next stop position. */
7068 ptrdiff_t stop = it->end_charpos;
7069 if (it->bidi_it.scan_dir < 0)
7070 /* Now we are scanning backward and don't know
7071 where to stop. */
7072 stop = -1;
7073 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7074 IT_BYTEPOS (*it), stop, Qnil);
7075 }
7076 }
7077 else
7078 {
7079 /* Composition created while scanning backward. */
7080 /* Update IT's char/byte positions to point to the last
7081 character of the previous grapheme cluster, or the
7082 character visually after the current composition. */
7083 for (i = 0; i < it->cmp_it.nchars; i++)
7084 bidi_move_to_visually_next (&it->bidi_it);
7085 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7086 IT_CHARPOS (*it) = it->bidi_it.charpos;
7087 if (it->cmp_it.from > 0)
7088 {
7089 /* Proceed to the previous grapheme cluster. */
7090 it->cmp_it.to = it->cmp_it.from;
7091 }
7092 else
7093 {
7094 /* No more grapheme clusters in this composition.
7095 Find the next stop position. */
7096 ptrdiff_t stop = it->end_charpos;
7097 if (it->bidi_it.scan_dir < 0)
7098 /* Now we are scanning backward and don't know
7099 where to stop. */
7100 stop = -1;
7101 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7102 IT_BYTEPOS (*it), stop, Qnil);
7103 }
7104 }
7105 }
7106 else
7107 {
7108 eassert (it->len != 0);
7109
7110 if (!it->bidi_p)
7111 {
7112 IT_BYTEPOS (*it) += it->len;
7113 IT_CHARPOS (*it) += 1;
7114 }
7115 else
7116 {
7117 int prev_scan_dir = it->bidi_it.scan_dir;
7118 /* If this is a new paragraph, determine its base
7119 direction (a.k.a. its base embedding level). */
7120 if (it->bidi_it.new_paragraph)
7121 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7122 bidi_move_to_visually_next (&it->bidi_it);
7123 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7124 IT_CHARPOS (*it) = it->bidi_it.charpos;
7125 if (prev_scan_dir != it->bidi_it.scan_dir)
7126 {
7127 /* As the scan direction was changed, we must
7128 re-compute the stop position for composition. */
7129 ptrdiff_t stop = it->end_charpos;
7130 if (it->bidi_it.scan_dir < 0)
7131 stop = -1;
7132 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7133 IT_BYTEPOS (*it), stop, Qnil);
7134 }
7135 }
7136 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7137 }
7138 break;
7139
7140 case GET_FROM_C_STRING:
7141 /* Current display element of IT is from a C string. */
7142 if (!it->bidi_p
7143 /* If the string position is beyond string's end, it means
7144 next_element_from_c_string is padding the string with
7145 blanks, in which case we bypass the bidi iterator,
7146 because it cannot deal with such virtual characters. */
7147 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7148 {
7149 IT_BYTEPOS (*it) += it->len;
7150 IT_CHARPOS (*it) += 1;
7151 }
7152 else
7153 {
7154 bidi_move_to_visually_next (&it->bidi_it);
7155 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7156 IT_CHARPOS (*it) = it->bidi_it.charpos;
7157 }
7158 break;
7159
7160 case GET_FROM_DISPLAY_VECTOR:
7161 /* Current display element of IT is from a display table entry.
7162 Advance in the display table definition. Reset it to null if
7163 end reached, and continue with characters from buffers/
7164 strings. */
7165 ++it->current.dpvec_index;
7166
7167 /* Restore face of the iterator to what they were before the
7168 display vector entry (these entries may contain faces). */
7169 it->face_id = it->saved_face_id;
7170
7171 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7172 {
7173 int recheck_faces = it->ellipsis_p;
7174
7175 if (it->s)
7176 it->method = GET_FROM_C_STRING;
7177 else if (STRINGP (it->string))
7178 it->method = GET_FROM_STRING;
7179 else
7180 {
7181 it->method = GET_FROM_BUFFER;
7182 it->object = it->w->contents;
7183 }
7184
7185 it->dpvec = NULL;
7186 it->current.dpvec_index = -1;
7187
7188 /* Skip over characters which were displayed via IT->dpvec. */
7189 if (it->dpvec_char_len < 0)
7190 reseat_at_next_visible_line_start (it, 1);
7191 else if (it->dpvec_char_len > 0)
7192 {
7193 if (it->method == GET_FROM_STRING
7194 && it->n_overlay_strings > 0)
7195 it->ignore_overlay_strings_at_pos_p = 1;
7196 it->len = it->dpvec_char_len;
7197 set_iterator_to_next (it, reseat_p);
7198 }
7199
7200 /* Maybe recheck faces after display vector */
7201 if (recheck_faces)
7202 it->stop_charpos = IT_CHARPOS (*it);
7203 }
7204 break;
7205
7206 case GET_FROM_STRING:
7207 /* Current display element is a character from a Lisp string. */
7208 eassert (it->s == NULL && STRINGP (it->string));
7209 /* Don't advance past string end. These conditions are true
7210 when set_iterator_to_next is called at the end of
7211 get_next_display_element, in which case the Lisp string is
7212 already exhausted, and all we want is pop the iterator
7213 stack. */
7214 if (it->current.overlay_string_index >= 0)
7215 {
7216 /* This is an overlay string, so there's no padding with
7217 spaces, and the number of characters in the string is
7218 where the string ends. */
7219 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7220 goto consider_string_end;
7221 }
7222 else
7223 {
7224 /* Not an overlay string. There could be padding, so test
7225 against it->end_charpos . */
7226 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7227 goto consider_string_end;
7228 }
7229 if (it->cmp_it.id >= 0)
7230 {
7231 int i;
7232
7233 if (! it->bidi_p)
7234 {
7235 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7236 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7237 if (it->cmp_it.to < it->cmp_it.nglyphs)
7238 it->cmp_it.from = it->cmp_it.to;
7239 else
7240 {
7241 it->cmp_it.id = -1;
7242 composition_compute_stop_pos (&it->cmp_it,
7243 IT_STRING_CHARPOS (*it),
7244 IT_STRING_BYTEPOS (*it),
7245 it->end_charpos, it->string);
7246 }
7247 }
7248 else if (! it->cmp_it.reversed_p)
7249 {
7250 for (i = 0; i < it->cmp_it.nchars; i++)
7251 bidi_move_to_visually_next (&it->bidi_it);
7252 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7253 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7254
7255 if (it->cmp_it.to < it->cmp_it.nglyphs)
7256 it->cmp_it.from = it->cmp_it.to;
7257 else
7258 {
7259 ptrdiff_t stop = it->end_charpos;
7260 if (it->bidi_it.scan_dir < 0)
7261 stop = -1;
7262 composition_compute_stop_pos (&it->cmp_it,
7263 IT_STRING_CHARPOS (*it),
7264 IT_STRING_BYTEPOS (*it), stop,
7265 it->string);
7266 }
7267 }
7268 else
7269 {
7270 for (i = 0; i < it->cmp_it.nchars; i++)
7271 bidi_move_to_visually_next (&it->bidi_it);
7272 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7273 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7274 if (it->cmp_it.from > 0)
7275 it->cmp_it.to = it->cmp_it.from;
7276 else
7277 {
7278 ptrdiff_t stop = it->end_charpos;
7279 if (it->bidi_it.scan_dir < 0)
7280 stop = -1;
7281 composition_compute_stop_pos (&it->cmp_it,
7282 IT_STRING_CHARPOS (*it),
7283 IT_STRING_BYTEPOS (*it), stop,
7284 it->string);
7285 }
7286 }
7287 }
7288 else
7289 {
7290 if (!it->bidi_p
7291 /* If the string position is beyond string's end, it
7292 means next_element_from_string is padding the string
7293 with blanks, in which case we bypass the bidi
7294 iterator, because it cannot deal with such virtual
7295 characters. */
7296 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7297 {
7298 IT_STRING_BYTEPOS (*it) += it->len;
7299 IT_STRING_CHARPOS (*it) += 1;
7300 }
7301 else
7302 {
7303 int prev_scan_dir = it->bidi_it.scan_dir;
7304
7305 bidi_move_to_visually_next (&it->bidi_it);
7306 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7307 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7308 if (prev_scan_dir != it->bidi_it.scan_dir)
7309 {
7310 ptrdiff_t stop = it->end_charpos;
7311
7312 if (it->bidi_it.scan_dir < 0)
7313 stop = -1;
7314 composition_compute_stop_pos (&it->cmp_it,
7315 IT_STRING_CHARPOS (*it),
7316 IT_STRING_BYTEPOS (*it), stop,
7317 it->string);
7318 }
7319 }
7320 }
7321
7322 consider_string_end:
7323
7324 if (it->current.overlay_string_index >= 0)
7325 {
7326 /* IT->string is an overlay string. Advance to the
7327 next, if there is one. */
7328 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7329 {
7330 it->ellipsis_p = 0;
7331 next_overlay_string (it);
7332 if (it->ellipsis_p)
7333 setup_for_ellipsis (it, 0);
7334 }
7335 }
7336 else
7337 {
7338 /* IT->string is not an overlay string. If we reached
7339 its end, and there is something on IT->stack, proceed
7340 with what is on the stack. This can be either another
7341 string, this time an overlay string, or a buffer. */
7342 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7343 && it->sp > 0)
7344 {
7345 pop_it (it);
7346 if (it->method == GET_FROM_STRING)
7347 goto consider_string_end;
7348 }
7349 }
7350 break;
7351
7352 case GET_FROM_IMAGE:
7353 case GET_FROM_STRETCH:
7354 #ifdef HAVE_XWIDGETS
7355 case GET_FROM_XWIDGET:
7356
7357 /* The position etc with which we have to proceed are on
7358 the stack. The position may be at the end of a string,
7359 if the `display' property takes up the whole string. */
7360 eassert (it->sp > 0);
7361 pop_it (it);
7362 if (it->method == GET_FROM_STRING)
7363 goto consider_string_end;
7364 break;
7365 #endif
7366 default:
7367 /* There are no other methods defined, so this should be a bug. */
7368 emacs_abort ();
7369 }
7370
7371 eassert (it->method != GET_FROM_STRING
7372 || (STRINGP (it->string)
7373 && IT_STRING_CHARPOS (*it) >= 0));
7374 }
7375
7376 /* Load IT's display element fields with information about the next
7377 display element which comes from a display table entry or from the
7378 result of translating a control character to one of the forms `^C'
7379 or `\003'.
7380
7381 IT->dpvec holds the glyphs to return as characters.
7382 IT->saved_face_id holds the face id before the display vector--it
7383 is restored into IT->face_id in set_iterator_to_next. */
7384
7385 static int
7386 next_element_from_display_vector (struct it *it)
7387 {
7388 Lisp_Object gc;
7389
7390 /* Precondition. */
7391 eassert (it->dpvec && it->current.dpvec_index >= 0);
7392
7393 it->face_id = it->saved_face_id;
7394
7395 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7396 That seemed totally bogus - so I changed it... */
7397 gc = it->dpvec[it->current.dpvec_index];
7398
7399 if (GLYPH_CODE_P (gc))
7400 {
7401 it->c = GLYPH_CODE_CHAR (gc);
7402 it->len = CHAR_BYTES (it->c);
7403
7404 /* The entry may contain a face id to use. Such a face id is
7405 the id of a Lisp face, not a realized face. A face id of
7406 zero means no face is specified. */
7407 if (it->dpvec_face_id >= 0)
7408 it->face_id = it->dpvec_face_id;
7409 else
7410 {
7411 int lface_id = GLYPH_CODE_FACE (gc);
7412 if (lface_id > 0)
7413 it->face_id = merge_faces (it->f, Qt, lface_id,
7414 it->saved_face_id);
7415 }
7416 }
7417 else
7418 /* Display table entry is invalid. Return a space. */
7419 it->c = ' ', it->len = 1;
7420
7421 /* Don't change position and object of the iterator here. They are
7422 still the values of the character that had this display table
7423 entry or was translated, and that's what we want. */
7424 it->what = IT_CHARACTER;
7425 return 1;
7426 }
7427
7428 /* Get the first element of string/buffer in the visual order, after
7429 being reseated to a new position in a string or a buffer. */
7430 static void
7431 get_visually_first_element (struct it *it)
7432 {
7433 int string_p = STRINGP (it->string) || it->s;
7434 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7435 ptrdiff_t bob = (string_p ? 0 : BEGV);
7436
7437 if (STRINGP (it->string))
7438 {
7439 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7440 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7441 }
7442 else
7443 {
7444 it->bidi_it.charpos = IT_CHARPOS (*it);
7445 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7446 }
7447
7448 if (it->bidi_it.charpos == eob)
7449 {
7450 /* Nothing to do, but reset the FIRST_ELT flag, like
7451 bidi_paragraph_init does, because we are not going to
7452 call it. */
7453 it->bidi_it.first_elt = 0;
7454 }
7455 else if (it->bidi_it.charpos == bob
7456 || (!string_p
7457 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7458 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7459 {
7460 /* If we are at the beginning of a line/string, we can produce
7461 the next element right away. */
7462 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7463 bidi_move_to_visually_next (&it->bidi_it);
7464 }
7465 else
7466 {
7467 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7468
7469 /* We need to prime the bidi iterator starting at the line's or
7470 string's beginning, before we will be able to produce the
7471 next element. */
7472 if (string_p)
7473 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7474 else
7475 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7476 IT_BYTEPOS (*it), -1,
7477 &it->bidi_it.bytepos);
7478 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7479 do
7480 {
7481 /* Now return to buffer/string position where we were asked
7482 to get the next display element, and produce that. */
7483 bidi_move_to_visually_next (&it->bidi_it);
7484 }
7485 while (it->bidi_it.bytepos != orig_bytepos
7486 && it->bidi_it.charpos < eob);
7487 }
7488
7489 /* Adjust IT's position information to where we ended up. */
7490 if (STRINGP (it->string))
7491 {
7492 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7493 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7494 }
7495 else
7496 {
7497 IT_CHARPOS (*it) = it->bidi_it.charpos;
7498 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7499 }
7500
7501 if (STRINGP (it->string) || !it->s)
7502 {
7503 ptrdiff_t stop, charpos, bytepos;
7504
7505 if (STRINGP (it->string))
7506 {
7507 eassert (!it->s);
7508 stop = SCHARS (it->string);
7509 if (stop > it->end_charpos)
7510 stop = it->end_charpos;
7511 charpos = IT_STRING_CHARPOS (*it);
7512 bytepos = IT_STRING_BYTEPOS (*it);
7513 }
7514 else
7515 {
7516 stop = it->end_charpos;
7517 charpos = IT_CHARPOS (*it);
7518 bytepos = IT_BYTEPOS (*it);
7519 }
7520 if (it->bidi_it.scan_dir < 0)
7521 stop = -1;
7522 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7523 it->string);
7524 }
7525 }
7526
7527 /* Load IT with the next display element from Lisp string IT->string.
7528 IT->current.string_pos is the current position within the string.
7529 If IT->current.overlay_string_index >= 0, the Lisp string is an
7530 overlay string. */
7531
7532 static int
7533 next_element_from_string (struct it *it)
7534 {
7535 struct text_pos position;
7536
7537 eassert (STRINGP (it->string));
7538 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7539 eassert (IT_STRING_CHARPOS (*it) >= 0);
7540 position = it->current.string_pos;
7541
7542 /* With bidi reordering, the character to display might not be the
7543 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7544 that we were reseat()ed to a new string, whose paragraph
7545 direction is not known. */
7546 if (it->bidi_p && it->bidi_it.first_elt)
7547 {
7548 get_visually_first_element (it);
7549 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7550 }
7551
7552 /* Time to check for invisible text? */
7553 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7554 {
7555 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7556 {
7557 if (!(!it->bidi_p
7558 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7559 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7560 {
7561 /* With bidi non-linear iteration, we could find
7562 ourselves far beyond the last computed stop_charpos,
7563 with several other stop positions in between that we
7564 missed. Scan them all now, in buffer's logical
7565 order, until we find and handle the last stop_charpos
7566 that precedes our current position. */
7567 handle_stop_backwards (it, it->stop_charpos);
7568 return GET_NEXT_DISPLAY_ELEMENT (it);
7569 }
7570 else
7571 {
7572 if (it->bidi_p)
7573 {
7574 /* Take note of the stop position we just moved
7575 across, for when we will move back across it. */
7576 it->prev_stop = it->stop_charpos;
7577 /* If we are at base paragraph embedding level, take
7578 note of the last stop position seen at this
7579 level. */
7580 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7581 it->base_level_stop = it->stop_charpos;
7582 }
7583 handle_stop (it);
7584
7585 /* Since a handler may have changed IT->method, we must
7586 recurse here. */
7587 return GET_NEXT_DISPLAY_ELEMENT (it);
7588 }
7589 }
7590 else if (it->bidi_p
7591 /* If we are before prev_stop, we may have overstepped
7592 on our way backwards a stop_pos, and if so, we need
7593 to handle that stop_pos. */
7594 && IT_STRING_CHARPOS (*it) < it->prev_stop
7595 /* We can sometimes back up for reasons that have nothing
7596 to do with bidi reordering. E.g., compositions. The
7597 code below is only needed when we are above the base
7598 embedding level, so test for that explicitly. */
7599 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7600 {
7601 /* If we lost track of base_level_stop, we have no better
7602 place for handle_stop_backwards to start from than string
7603 beginning. This happens, e.g., when we were reseated to
7604 the previous screenful of text by vertical-motion. */
7605 if (it->base_level_stop <= 0
7606 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7607 it->base_level_stop = 0;
7608 handle_stop_backwards (it, it->base_level_stop);
7609 return GET_NEXT_DISPLAY_ELEMENT (it);
7610 }
7611 }
7612
7613 if (it->current.overlay_string_index >= 0)
7614 {
7615 /* Get the next character from an overlay string. In overlay
7616 strings, there is no field width or padding with spaces to
7617 do. */
7618 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7619 {
7620 it->what = IT_EOB;
7621 return 0;
7622 }
7623 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7624 IT_STRING_BYTEPOS (*it),
7625 it->bidi_it.scan_dir < 0
7626 ? -1
7627 : SCHARS (it->string))
7628 && next_element_from_composition (it))
7629 {
7630 return 1;
7631 }
7632 else if (STRING_MULTIBYTE (it->string))
7633 {
7634 const unsigned char *s = (SDATA (it->string)
7635 + IT_STRING_BYTEPOS (*it));
7636 it->c = string_char_and_length (s, &it->len);
7637 }
7638 else
7639 {
7640 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7641 it->len = 1;
7642 }
7643 }
7644 else
7645 {
7646 /* Get the next character from a Lisp string that is not an
7647 overlay string. Such strings come from the mode line, for
7648 example. We may have to pad with spaces, or truncate the
7649 string. See also next_element_from_c_string. */
7650 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7651 {
7652 it->what = IT_EOB;
7653 return 0;
7654 }
7655 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7656 {
7657 /* Pad with spaces. */
7658 it->c = ' ', it->len = 1;
7659 CHARPOS (position) = BYTEPOS (position) = -1;
7660 }
7661 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7662 IT_STRING_BYTEPOS (*it),
7663 it->bidi_it.scan_dir < 0
7664 ? -1
7665 : it->string_nchars)
7666 && next_element_from_composition (it))
7667 {
7668 return 1;
7669 }
7670 else if (STRING_MULTIBYTE (it->string))
7671 {
7672 const unsigned char *s = (SDATA (it->string)
7673 + IT_STRING_BYTEPOS (*it));
7674 it->c = string_char_and_length (s, &it->len);
7675 }
7676 else
7677 {
7678 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7679 it->len = 1;
7680 }
7681 }
7682
7683 /* Record what we have and where it came from. */
7684 it->what = IT_CHARACTER;
7685 it->object = it->string;
7686 it->position = position;
7687 return 1;
7688 }
7689
7690
7691 /* Load IT with next display element from C string IT->s.
7692 IT->string_nchars is the maximum number of characters to return
7693 from the string. IT->end_charpos may be greater than
7694 IT->string_nchars when this function is called, in which case we
7695 may have to return padding spaces. Value is zero if end of string
7696 reached, including padding spaces. */
7697
7698 static int
7699 next_element_from_c_string (struct it *it)
7700 {
7701 int success_p = 1;
7702
7703 eassert (it->s);
7704 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7705 it->what = IT_CHARACTER;
7706 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7707 it->object = Qnil;
7708
7709 /* With bidi reordering, the character to display might not be the
7710 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7711 we were reseated to a new string, whose paragraph direction is
7712 not known. */
7713 if (it->bidi_p && it->bidi_it.first_elt)
7714 get_visually_first_element (it);
7715
7716 /* IT's position can be greater than IT->string_nchars in case a
7717 field width or precision has been specified when the iterator was
7718 initialized. */
7719 if (IT_CHARPOS (*it) >= it->end_charpos)
7720 {
7721 /* End of the game. */
7722 it->what = IT_EOB;
7723 success_p = 0;
7724 }
7725 else if (IT_CHARPOS (*it) >= it->string_nchars)
7726 {
7727 /* Pad with spaces. */
7728 it->c = ' ', it->len = 1;
7729 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7730 }
7731 else if (it->multibyte_p)
7732 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7733 else
7734 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7735
7736 return success_p;
7737 }
7738
7739
7740 /* Set up IT to return characters from an ellipsis, if appropriate.
7741 The definition of the ellipsis glyphs may come from a display table
7742 entry. This function fills IT with the first glyph from the
7743 ellipsis if an ellipsis is to be displayed. */
7744
7745 static int
7746 next_element_from_ellipsis (struct it *it)
7747 {
7748 if (it->selective_display_ellipsis_p)
7749 setup_for_ellipsis (it, it->len);
7750 else
7751 {
7752 /* The face at the current position may be different from the
7753 face we find after the invisible text. Remember what it
7754 was in IT->saved_face_id, and signal that it's there by
7755 setting face_before_selective_p. */
7756 it->saved_face_id = it->face_id;
7757 it->method = GET_FROM_BUFFER;
7758 it->object = it->w->contents;
7759 reseat_at_next_visible_line_start (it, 1);
7760 it->face_before_selective_p = 1;
7761 }
7762
7763 return GET_NEXT_DISPLAY_ELEMENT (it);
7764 }
7765
7766
7767 /* Deliver an image display element. The iterator IT is already
7768 filled with image information (done in handle_display_prop). Value
7769 is always 1. */
7770
7771
7772 static int
7773 next_element_from_image (struct it *it)
7774 {
7775 it->what = IT_IMAGE;
7776 it->ignore_overlay_strings_at_pos_p = 0;
7777 return 1;
7778 }
7779
7780 #ifdef HAVE_XWIDGETS
7781 /* im not sure about this FIXME JAVE*/
7782 static int
7783 next_element_from_xwidget (struct it *it)
7784 {
7785 it->what = IT_XWIDGET;
7786 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
7787 //this is shaky because why do we set "what" if we dont set the other parts??
7788 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
7789 return 1;
7790 }
7791 #endif
7792
7793
7794 /* Fill iterator IT with next display element from a stretch glyph
7795 property. IT->object is the value of the text property. Value is
7796 always 1. */
7797
7798 static int
7799 next_element_from_stretch (struct it *it)
7800 {
7801 it->what = IT_STRETCH;
7802 return 1;
7803 }
7804
7805 /* Scan backwards from IT's current position until we find a stop
7806 position, or until BEGV. This is called when we find ourself
7807 before both the last known prev_stop and base_level_stop while
7808 reordering bidirectional text. */
7809
7810 static void
7811 compute_stop_pos_backwards (struct it *it)
7812 {
7813 const int SCAN_BACK_LIMIT = 1000;
7814 struct text_pos pos;
7815 struct display_pos save_current = it->current;
7816 struct text_pos save_position = it->position;
7817 ptrdiff_t charpos = IT_CHARPOS (*it);
7818 ptrdiff_t where_we_are = charpos;
7819 ptrdiff_t save_stop_pos = it->stop_charpos;
7820 ptrdiff_t save_end_pos = it->end_charpos;
7821
7822 eassert (NILP (it->string) && !it->s);
7823 eassert (it->bidi_p);
7824 it->bidi_p = 0;
7825 do
7826 {
7827 it->end_charpos = min (charpos + 1, ZV);
7828 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7829 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7830 reseat_1 (it, pos, 0);
7831 compute_stop_pos (it);
7832 /* We must advance forward, right? */
7833 if (it->stop_charpos <= charpos)
7834 emacs_abort ();
7835 }
7836 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7837
7838 if (it->stop_charpos <= where_we_are)
7839 it->prev_stop = it->stop_charpos;
7840 else
7841 it->prev_stop = BEGV;
7842 it->bidi_p = 1;
7843 it->current = save_current;
7844 it->position = save_position;
7845 it->stop_charpos = save_stop_pos;
7846 it->end_charpos = save_end_pos;
7847 }
7848
7849 /* Scan forward from CHARPOS in the current buffer/string, until we
7850 find a stop position > current IT's position. Then handle the stop
7851 position before that. This is called when we bump into a stop
7852 position while reordering bidirectional text. CHARPOS should be
7853 the last previously processed stop_pos (or BEGV/0, if none were
7854 processed yet) whose position is less that IT's current
7855 position. */
7856
7857 static void
7858 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7859 {
7860 int bufp = !STRINGP (it->string);
7861 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7862 struct display_pos save_current = it->current;
7863 struct text_pos save_position = it->position;
7864 struct text_pos pos1;
7865 ptrdiff_t next_stop;
7866
7867 /* Scan in strict logical order. */
7868 eassert (it->bidi_p);
7869 it->bidi_p = 0;
7870 do
7871 {
7872 it->prev_stop = charpos;
7873 if (bufp)
7874 {
7875 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7876 reseat_1 (it, pos1, 0);
7877 }
7878 else
7879 it->current.string_pos = string_pos (charpos, it->string);
7880 compute_stop_pos (it);
7881 /* We must advance forward, right? */
7882 if (it->stop_charpos <= it->prev_stop)
7883 emacs_abort ();
7884 charpos = it->stop_charpos;
7885 }
7886 while (charpos <= where_we_are);
7887
7888 it->bidi_p = 1;
7889 it->current = save_current;
7890 it->position = save_position;
7891 next_stop = it->stop_charpos;
7892 it->stop_charpos = it->prev_stop;
7893 handle_stop (it);
7894 it->stop_charpos = next_stop;
7895 }
7896
7897 /* Load IT with the next display element from current_buffer. Value
7898 is zero if end of buffer reached. IT->stop_charpos is the next
7899 position at which to stop and check for text properties or buffer
7900 end. */
7901
7902 static int
7903 next_element_from_buffer (struct it *it)
7904 {
7905 int success_p = 1;
7906
7907 eassert (IT_CHARPOS (*it) >= BEGV);
7908 eassert (NILP (it->string) && !it->s);
7909 eassert (!it->bidi_p
7910 || (EQ (it->bidi_it.string.lstring, Qnil)
7911 && it->bidi_it.string.s == NULL));
7912
7913 /* With bidi reordering, the character to display might not be the
7914 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7915 we were reseat()ed to a new buffer position, which is potentially
7916 a different paragraph. */
7917 if (it->bidi_p && it->bidi_it.first_elt)
7918 {
7919 get_visually_first_element (it);
7920 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7921 }
7922
7923 if (IT_CHARPOS (*it) >= it->stop_charpos)
7924 {
7925 if (IT_CHARPOS (*it) >= it->end_charpos)
7926 {
7927 int overlay_strings_follow_p;
7928
7929 /* End of the game, except when overlay strings follow that
7930 haven't been returned yet. */
7931 if (it->overlay_strings_at_end_processed_p)
7932 overlay_strings_follow_p = 0;
7933 else
7934 {
7935 it->overlay_strings_at_end_processed_p = 1;
7936 overlay_strings_follow_p = get_overlay_strings (it, 0);
7937 }
7938
7939 if (overlay_strings_follow_p)
7940 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7941 else
7942 {
7943 it->what = IT_EOB;
7944 it->position = it->current.pos;
7945 success_p = 0;
7946 }
7947 }
7948 else if (!(!it->bidi_p
7949 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7950 || IT_CHARPOS (*it) == it->stop_charpos))
7951 {
7952 /* With bidi non-linear iteration, we could find ourselves
7953 far beyond the last computed stop_charpos, with several
7954 other stop positions in between that we missed. Scan
7955 them all now, in buffer's logical order, until we find
7956 and handle the last stop_charpos that precedes our
7957 current position. */
7958 handle_stop_backwards (it, it->stop_charpos);
7959 return GET_NEXT_DISPLAY_ELEMENT (it);
7960 }
7961 else
7962 {
7963 if (it->bidi_p)
7964 {
7965 /* Take note of the stop position we just moved across,
7966 for when we will move back across it. */
7967 it->prev_stop = it->stop_charpos;
7968 /* If we are at base paragraph embedding level, take
7969 note of the last stop position seen at this
7970 level. */
7971 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7972 it->base_level_stop = it->stop_charpos;
7973 }
7974 handle_stop (it);
7975 return GET_NEXT_DISPLAY_ELEMENT (it);
7976 }
7977 }
7978 else if (it->bidi_p
7979 /* If we are before prev_stop, we may have overstepped on
7980 our way backwards a stop_pos, and if so, we need to
7981 handle that stop_pos. */
7982 && IT_CHARPOS (*it) < it->prev_stop
7983 /* We can sometimes back up for reasons that have nothing
7984 to do with bidi reordering. E.g., compositions. The
7985 code below is only needed when we are above the base
7986 embedding level, so test for that explicitly. */
7987 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7988 {
7989 if (it->base_level_stop <= 0
7990 || IT_CHARPOS (*it) < it->base_level_stop)
7991 {
7992 /* If we lost track of base_level_stop, we need to find
7993 prev_stop by looking backwards. This happens, e.g., when
7994 we were reseated to the previous screenful of text by
7995 vertical-motion. */
7996 it->base_level_stop = BEGV;
7997 compute_stop_pos_backwards (it);
7998 handle_stop_backwards (it, it->prev_stop);
7999 }
8000 else
8001 handle_stop_backwards (it, it->base_level_stop);
8002 return GET_NEXT_DISPLAY_ELEMENT (it);
8003 }
8004 else
8005 {
8006 /* No face changes, overlays etc. in sight, so just return a
8007 character from current_buffer. */
8008 unsigned char *p;
8009 ptrdiff_t stop;
8010
8011 /* Maybe run the redisplay end trigger hook. Performance note:
8012 This doesn't seem to cost measurable time. */
8013 if (it->redisplay_end_trigger_charpos
8014 && it->glyph_row
8015 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8016 run_redisplay_end_trigger_hook (it);
8017
8018 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8019 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8020 stop)
8021 && next_element_from_composition (it))
8022 {
8023 return 1;
8024 }
8025
8026 /* Get the next character, maybe multibyte. */
8027 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8028 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8029 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8030 else
8031 it->c = *p, it->len = 1;
8032
8033 /* Record what we have and where it came from. */
8034 it->what = IT_CHARACTER;
8035 it->object = it->w->contents;
8036 it->position = it->current.pos;
8037
8038 /* Normally we return the character found above, except when we
8039 really want to return an ellipsis for selective display. */
8040 if (it->selective)
8041 {
8042 if (it->c == '\n')
8043 {
8044 /* A value of selective > 0 means hide lines indented more
8045 than that number of columns. */
8046 if (it->selective > 0
8047 && IT_CHARPOS (*it) + 1 < ZV
8048 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8049 IT_BYTEPOS (*it) + 1,
8050 it->selective))
8051 {
8052 success_p = next_element_from_ellipsis (it);
8053 it->dpvec_char_len = -1;
8054 }
8055 }
8056 else if (it->c == '\r' && it->selective == -1)
8057 {
8058 /* A value of selective == -1 means that everything from the
8059 CR to the end of the line is invisible, with maybe an
8060 ellipsis displayed for it. */
8061 success_p = next_element_from_ellipsis (it);
8062 it->dpvec_char_len = -1;
8063 }
8064 }
8065 }
8066
8067 /* Value is zero if end of buffer reached. */
8068 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8069 return success_p;
8070 }
8071
8072
8073 /* Run the redisplay end trigger hook for IT. */
8074
8075 static void
8076 run_redisplay_end_trigger_hook (struct it *it)
8077 {
8078 Lisp_Object args[3];
8079
8080 /* IT->glyph_row should be non-null, i.e. we should be actually
8081 displaying something, or otherwise we should not run the hook. */
8082 eassert (it->glyph_row);
8083
8084 /* Set up hook arguments. */
8085 args[0] = Qredisplay_end_trigger_functions;
8086 args[1] = it->window;
8087 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8088 it->redisplay_end_trigger_charpos = 0;
8089
8090 /* Since we are *trying* to run these functions, don't try to run
8091 them again, even if they get an error. */
8092 wset_redisplay_end_trigger (it->w, Qnil);
8093 Frun_hook_with_args (3, args);
8094
8095 /* Notice if it changed the face of the character we are on. */
8096 handle_face_prop (it);
8097 }
8098
8099
8100 /* Deliver a composition display element. Unlike the other
8101 next_element_from_XXX, this function is not registered in the array
8102 get_next_element[]. It is called from next_element_from_buffer and
8103 next_element_from_string when necessary. */
8104
8105 static int
8106 next_element_from_composition (struct it *it)
8107 {
8108 it->what = IT_COMPOSITION;
8109 it->len = it->cmp_it.nbytes;
8110 if (STRINGP (it->string))
8111 {
8112 if (it->c < 0)
8113 {
8114 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8115 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8116 return 0;
8117 }
8118 it->position = it->current.string_pos;
8119 it->object = it->string;
8120 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8121 IT_STRING_BYTEPOS (*it), it->string);
8122 }
8123 else
8124 {
8125 if (it->c < 0)
8126 {
8127 IT_CHARPOS (*it) += it->cmp_it.nchars;
8128 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8129 if (it->bidi_p)
8130 {
8131 if (it->bidi_it.new_paragraph)
8132 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8133 /* Resync the bidi iterator with IT's new position.
8134 FIXME: this doesn't support bidirectional text. */
8135 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8136 bidi_move_to_visually_next (&it->bidi_it);
8137 }
8138 return 0;
8139 }
8140 it->position = it->current.pos;
8141 it->object = it->w->contents;
8142 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8143 IT_BYTEPOS (*it), Qnil);
8144 }
8145 return 1;
8146 }
8147
8148
8149 \f
8150 /***********************************************************************
8151 Moving an iterator without producing glyphs
8152 ***********************************************************************/
8153
8154 /* Check if iterator is at a position corresponding to a valid buffer
8155 position after some move_it_ call. */
8156
8157 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8158 ((it)->method == GET_FROM_STRING \
8159 ? IT_STRING_CHARPOS (*it) == 0 \
8160 : 1)
8161
8162
8163 /* Move iterator IT to a specified buffer or X position within one
8164 line on the display without producing glyphs.
8165
8166 OP should be a bit mask including some or all of these bits:
8167 MOVE_TO_X: Stop upon reaching x-position TO_X.
8168 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8169 Regardless of OP's value, stop upon reaching the end of the display line.
8170
8171 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8172 This means, in particular, that TO_X includes window's horizontal
8173 scroll amount.
8174
8175 The return value has several possible values that
8176 say what condition caused the scan to stop:
8177
8178 MOVE_POS_MATCH_OR_ZV
8179 - when TO_POS or ZV was reached.
8180
8181 MOVE_X_REACHED
8182 -when TO_X was reached before TO_POS or ZV were reached.
8183
8184 MOVE_LINE_CONTINUED
8185 - when we reached the end of the display area and the line must
8186 be continued.
8187
8188 MOVE_LINE_TRUNCATED
8189 - when we reached the end of the display area and the line is
8190 truncated.
8191
8192 MOVE_NEWLINE_OR_CR
8193 - when we stopped at a line end, i.e. a newline or a CR and selective
8194 display is on. */
8195
8196 static enum move_it_result
8197 move_it_in_display_line_to (struct it *it,
8198 ptrdiff_t to_charpos, int to_x,
8199 enum move_operation_enum op)
8200 {
8201 enum move_it_result result = MOVE_UNDEFINED;
8202 struct glyph_row *saved_glyph_row;
8203 struct it wrap_it, atpos_it, atx_it, ppos_it;
8204 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8205 void *ppos_data = NULL;
8206 int may_wrap = 0;
8207 enum it_method prev_method = it->method;
8208 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8209 int saw_smaller_pos = prev_pos < to_charpos;
8210
8211 /* Don't produce glyphs in produce_glyphs. */
8212 saved_glyph_row = it->glyph_row;
8213 it->glyph_row = NULL;
8214
8215 /* Use wrap_it to save a copy of IT wherever a word wrap could
8216 occur. Use atpos_it to save a copy of IT at the desired buffer
8217 position, if found, so that we can scan ahead and check if the
8218 word later overshoots the window edge. Use atx_it similarly, for
8219 pixel positions. */
8220 wrap_it.sp = -1;
8221 atpos_it.sp = -1;
8222 atx_it.sp = -1;
8223
8224 /* Use ppos_it under bidi reordering to save a copy of IT for the
8225 position > CHARPOS that is the closest to CHARPOS. We restore
8226 that position in IT when we have scanned the entire display line
8227 without finding a match for CHARPOS and all the character
8228 positions are greater than CHARPOS. */
8229 if (it->bidi_p)
8230 {
8231 SAVE_IT (ppos_it, *it, ppos_data);
8232 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8233 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8234 SAVE_IT (ppos_it, *it, ppos_data);
8235 }
8236
8237 #define BUFFER_POS_REACHED_P() \
8238 ((op & MOVE_TO_POS) != 0 \
8239 && BUFFERP (it->object) \
8240 && (IT_CHARPOS (*it) == to_charpos \
8241 || ((!it->bidi_p \
8242 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8243 && IT_CHARPOS (*it) > to_charpos) \
8244 || (it->what == IT_COMPOSITION \
8245 && ((IT_CHARPOS (*it) > to_charpos \
8246 && to_charpos >= it->cmp_it.charpos) \
8247 || (IT_CHARPOS (*it) < to_charpos \
8248 && to_charpos <= it->cmp_it.charpos)))) \
8249 && (it->method == GET_FROM_BUFFER \
8250 || (it->method == GET_FROM_DISPLAY_VECTOR \
8251 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8252
8253 /* If there's a line-/wrap-prefix, handle it. */
8254 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8255 && it->current_y < it->last_visible_y)
8256 handle_line_prefix (it);
8257
8258 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8259 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8260
8261 while (1)
8262 {
8263 int x, i, ascent = 0, descent = 0;
8264
8265 /* Utility macro to reset an iterator with x, ascent, and descent. */
8266 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8267 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8268 (IT)->max_descent = descent)
8269
8270 /* Stop if we move beyond TO_CHARPOS (after an image or a
8271 display string or stretch glyph). */
8272 if ((op & MOVE_TO_POS) != 0
8273 && BUFFERP (it->object)
8274 && it->method == GET_FROM_BUFFER
8275 && (((!it->bidi_p
8276 /* When the iterator is at base embedding level, we
8277 are guaranteed that characters are delivered for
8278 display in strictly increasing order of their
8279 buffer positions. */
8280 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8281 && IT_CHARPOS (*it) > to_charpos)
8282 || (it->bidi_p
8283 && (prev_method == GET_FROM_IMAGE
8284 || prev_method == GET_FROM_STRETCH
8285 || prev_method == GET_FROM_STRING)
8286 /* Passed TO_CHARPOS from left to right. */
8287 && ((prev_pos < to_charpos
8288 && IT_CHARPOS (*it) > to_charpos)
8289 /* Passed TO_CHARPOS from right to left. */
8290 || (prev_pos > to_charpos
8291 && IT_CHARPOS (*it) < to_charpos)))))
8292 {
8293 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8294 {
8295 result = MOVE_POS_MATCH_OR_ZV;
8296 break;
8297 }
8298 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8299 /* If wrap_it is valid, the current position might be in a
8300 word that is wrapped. So, save the iterator in
8301 atpos_it and continue to see if wrapping happens. */
8302 SAVE_IT (atpos_it, *it, atpos_data);
8303 }
8304
8305 /* Stop when ZV reached.
8306 We used to stop here when TO_CHARPOS reached as well, but that is
8307 too soon if this glyph does not fit on this line. So we handle it
8308 explicitly below. */
8309 if (!get_next_display_element (it))
8310 {
8311 result = MOVE_POS_MATCH_OR_ZV;
8312 break;
8313 }
8314
8315 if (it->line_wrap == TRUNCATE)
8316 {
8317 if (BUFFER_POS_REACHED_P ())
8318 {
8319 result = MOVE_POS_MATCH_OR_ZV;
8320 break;
8321 }
8322 }
8323 else
8324 {
8325 if (it->line_wrap == WORD_WRAP)
8326 {
8327 if (IT_DISPLAYING_WHITESPACE (it))
8328 may_wrap = 1;
8329 else if (may_wrap)
8330 {
8331 /* We have reached a glyph that follows one or more
8332 whitespace characters. If the position is
8333 already found, we are done. */
8334 if (atpos_it.sp >= 0)
8335 {
8336 RESTORE_IT (it, &atpos_it, atpos_data);
8337 result = MOVE_POS_MATCH_OR_ZV;
8338 goto done;
8339 }
8340 if (atx_it.sp >= 0)
8341 {
8342 RESTORE_IT (it, &atx_it, atx_data);
8343 result = MOVE_X_REACHED;
8344 goto done;
8345 }
8346 /* Otherwise, we can wrap here. */
8347 SAVE_IT (wrap_it, *it, wrap_data);
8348 may_wrap = 0;
8349 }
8350 }
8351 }
8352
8353 /* Remember the line height for the current line, in case
8354 the next element doesn't fit on the line. */
8355 ascent = it->max_ascent;
8356 descent = it->max_descent;
8357
8358 /* The call to produce_glyphs will get the metrics of the
8359 display element IT is loaded with. Record the x-position
8360 before this display element, in case it doesn't fit on the
8361 line. */
8362 x = it->current_x;
8363
8364 PRODUCE_GLYPHS (it);
8365
8366 if (it->area != TEXT_AREA)
8367 {
8368 prev_method = it->method;
8369 if (it->method == GET_FROM_BUFFER)
8370 prev_pos = IT_CHARPOS (*it);
8371 set_iterator_to_next (it, 1);
8372 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8373 SET_TEXT_POS (this_line_min_pos,
8374 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8375 if (it->bidi_p
8376 && (op & MOVE_TO_POS)
8377 && IT_CHARPOS (*it) > to_charpos
8378 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8379 SAVE_IT (ppos_it, *it, ppos_data);
8380 continue;
8381 }
8382
8383 /* The number of glyphs we get back in IT->nglyphs will normally
8384 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8385 character on a terminal frame, or (iii) a line end. For the
8386 second case, IT->nglyphs - 1 padding glyphs will be present.
8387 (On X frames, there is only one glyph produced for a
8388 composite character.)
8389
8390 The behavior implemented below means, for continuation lines,
8391 that as many spaces of a TAB as fit on the current line are
8392 displayed there. For terminal frames, as many glyphs of a
8393 multi-glyph character are displayed in the current line, too.
8394 This is what the old redisplay code did, and we keep it that
8395 way. Under X, the whole shape of a complex character must
8396 fit on the line or it will be completely displayed in the
8397 next line.
8398
8399 Note that both for tabs and padding glyphs, all glyphs have
8400 the same width. */
8401 if (it->nglyphs)
8402 {
8403 /* More than one glyph or glyph doesn't fit on line. All
8404 glyphs have the same width. */
8405 int single_glyph_width = it->pixel_width / it->nglyphs;
8406 int new_x;
8407 int x_before_this_char = x;
8408 int hpos_before_this_char = it->hpos;
8409
8410 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8411 {
8412 new_x = x + single_glyph_width;
8413
8414 /* We want to leave anything reaching TO_X to the caller. */
8415 if ((op & MOVE_TO_X) && new_x > to_x)
8416 {
8417 if (BUFFER_POS_REACHED_P ())
8418 {
8419 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8420 goto buffer_pos_reached;
8421 if (atpos_it.sp < 0)
8422 {
8423 SAVE_IT (atpos_it, *it, atpos_data);
8424 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8425 }
8426 }
8427 else
8428 {
8429 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8430 {
8431 it->current_x = x;
8432 result = MOVE_X_REACHED;
8433 break;
8434 }
8435 if (atx_it.sp < 0)
8436 {
8437 SAVE_IT (atx_it, *it, atx_data);
8438 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8439 }
8440 }
8441 }
8442
8443 if (/* Lines are continued. */
8444 it->line_wrap != TRUNCATE
8445 && (/* And glyph doesn't fit on the line. */
8446 new_x > it->last_visible_x
8447 /* Or it fits exactly and we're on a window
8448 system frame. */
8449 || (new_x == it->last_visible_x
8450 && FRAME_WINDOW_P (it->f)
8451 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8452 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8453 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8454 {
8455 if (/* IT->hpos == 0 means the very first glyph
8456 doesn't fit on the line, e.g. a wide image. */
8457 it->hpos == 0
8458 || (new_x == it->last_visible_x
8459 && FRAME_WINDOW_P (it->f)))
8460 {
8461 ++it->hpos;
8462 it->current_x = new_x;
8463
8464 /* The character's last glyph just barely fits
8465 in this row. */
8466 if (i == it->nglyphs - 1)
8467 {
8468 /* If this is the destination position,
8469 return a position *before* it in this row,
8470 now that we know it fits in this row. */
8471 if (BUFFER_POS_REACHED_P ())
8472 {
8473 if (it->line_wrap != WORD_WRAP
8474 || wrap_it.sp < 0)
8475 {
8476 it->hpos = hpos_before_this_char;
8477 it->current_x = x_before_this_char;
8478 result = MOVE_POS_MATCH_OR_ZV;
8479 break;
8480 }
8481 if (it->line_wrap == WORD_WRAP
8482 && atpos_it.sp < 0)
8483 {
8484 SAVE_IT (atpos_it, *it, atpos_data);
8485 atpos_it.current_x = x_before_this_char;
8486 atpos_it.hpos = hpos_before_this_char;
8487 }
8488 }
8489
8490 prev_method = it->method;
8491 if (it->method == GET_FROM_BUFFER)
8492 prev_pos = IT_CHARPOS (*it);
8493 set_iterator_to_next (it, 1);
8494 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8495 SET_TEXT_POS (this_line_min_pos,
8496 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8497 /* On graphical terminals, newlines may
8498 "overflow" into the fringe if
8499 overflow-newline-into-fringe is non-nil.
8500 On text terminals, and on graphical
8501 terminals with no right margin, newlines
8502 may overflow into the last glyph on the
8503 display line.*/
8504 if (!FRAME_WINDOW_P (it->f)
8505 || ((it->bidi_p
8506 && it->bidi_it.paragraph_dir == R2L)
8507 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8508 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8509 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8510 {
8511 if (!get_next_display_element (it))
8512 {
8513 result = MOVE_POS_MATCH_OR_ZV;
8514 break;
8515 }
8516 if (BUFFER_POS_REACHED_P ())
8517 {
8518 if (ITERATOR_AT_END_OF_LINE_P (it))
8519 result = MOVE_POS_MATCH_OR_ZV;
8520 else
8521 result = MOVE_LINE_CONTINUED;
8522 break;
8523 }
8524 if (ITERATOR_AT_END_OF_LINE_P (it))
8525 {
8526 result = MOVE_NEWLINE_OR_CR;
8527 break;
8528 }
8529 }
8530 }
8531 }
8532 else
8533 IT_RESET_X_ASCENT_DESCENT (it);
8534
8535 if (wrap_it.sp >= 0)
8536 {
8537 RESTORE_IT (it, &wrap_it, wrap_data);
8538 atpos_it.sp = -1;
8539 atx_it.sp = -1;
8540 }
8541
8542 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8543 IT_CHARPOS (*it)));
8544 result = MOVE_LINE_CONTINUED;
8545 break;
8546 }
8547
8548 if (BUFFER_POS_REACHED_P ())
8549 {
8550 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8551 goto buffer_pos_reached;
8552 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8553 {
8554 SAVE_IT (atpos_it, *it, atpos_data);
8555 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8556 }
8557 }
8558
8559 if (new_x > it->first_visible_x)
8560 {
8561 /* Glyph is visible. Increment number of glyphs that
8562 would be displayed. */
8563 ++it->hpos;
8564 }
8565 }
8566
8567 if (result != MOVE_UNDEFINED)
8568 break;
8569 }
8570 else if (BUFFER_POS_REACHED_P ())
8571 {
8572 buffer_pos_reached:
8573 IT_RESET_X_ASCENT_DESCENT (it);
8574 result = MOVE_POS_MATCH_OR_ZV;
8575 break;
8576 }
8577 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8578 {
8579 /* Stop when TO_X specified and reached. This check is
8580 necessary here because of lines consisting of a line end,
8581 only. The line end will not produce any glyphs and we
8582 would never get MOVE_X_REACHED. */
8583 eassert (it->nglyphs == 0);
8584 result = MOVE_X_REACHED;
8585 break;
8586 }
8587
8588 /* Is this a line end? If yes, we're done. */
8589 if (ITERATOR_AT_END_OF_LINE_P (it))
8590 {
8591 /* If we are past TO_CHARPOS, but never saw any character
8592 positions smaller than TO_CHARPOS, return
8593 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8594 did. */
8595 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8596 {
8597 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8598 {
8599 if (IT_CHARPOS (ppos_it) < ZV)
8600 {
8601 RESTORE_IT (it, &ppos_it, ppos_data);
8602 result = MOVE_POS_MATCH_OR_ZV;
8603 }
8604 else
8605 goto buffer_pos_reached;
8606 }
8607 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8608 && IT_CHARPOS (*it) > to_charpos)
8609 goto buffer_pos_reached;
8610 else
8611 result = MOVE_NEWLINE_OR_CR;
8612 }
8613 else
8614 result = MOVE_NEWLINE_OR_CR;
8615 break;
8616 }
8617
8618 prev_method = it->method;
8619 if (it->method == GET_FROM_BUFFER)
8620 prev_pos = IT_CHARPOS (*it);
8621 /* The current display element has been consumed. Advance
8622 to the next. */
8623 set_iterator_to_next (it, 1);
8624 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8625 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8626 if (IT_CHARPOS (*it) < to_charpos)
8627 saw_smaller_pos = 1;
8628 if (it->bidi_p
8629 && (op & MOVE_TO_POS)
8630 && IT_CHARPOS (*it) >= to_charpos
8631 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8632 SAVE_IT (ppos_it, *it, ppos_data);
8633
8634 /* Stop if lines are truncated and IT's current x-position is
8635 past the right edge of the window now. */
8636 if (it->line_wrap == TRUNCATE
8637 && it->current_x >= it->last_visible_x)
8638 {
8639 if (!FRAME_WINDOW_P (it->f)
8640 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8641 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8642 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8643 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8644 {
8645 int at_eob_p = 0;
8646
8647 if ((at_eob_p = !get_next_display_element (it))
8648 || BUFFER_POS_REACHED_P ()
8649 /* If we are past TO_CHARPOS, but never saw any
8650 character positions smaller than TO_CHARPOS,
8651 return MOVE_POS_MATCH_OR_ZV, like the
8652 unidirectional display did. */
8653 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8654 && !saw_smaller_pos
8655 && IT_CHARPOS (*it) > to_charpos))
8656 {
8657 if (it->bidi_p
8658 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8659 RESTORE_IT (it, &ppos_it, ppos_data);
8660 result = MOVE_POS_MATCH_OR_ZV;
8661 break;
8662 }
8663 if (ITERATOR_AT_END_OF_LINE_P (it))
8664 {
8665 result = MOVE_NEWLINE_OR_CR;
8666 break;
8667 }
8668 }
8669 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8670 && !saw_smaller_pos
8671 && IT_CHARPOS (*it) > to_charpos)
8672 {
8673 if (IT_CHARPOS (ppos_it) < ZV)
8674 RESTORE_IT (it, &ppos_it, ppos_data);
8675 result = MOVE_POS_MATCH_OR_ZV;
8676 break;
8677 }
8678 result = MOVE_LINE_TRUNCATED;
8679 break;
8680 }
8681 #undef IT_RESET_X_ASCENT_DESCENT
8682 }
8683
8684 #undef BUFFER_POS_REACHED_P
8685
8686 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8687 restore the saved iterator. */
8688 if (atpos_it.sp >= 0)
8689 RESTORE_IT (it, &atpos_it, atpos_data);
8690 else if (atx_it.sp >= 0)
8691 RESTORE_IT (it, &atx_it, atx_data);
8692
8693 done:
8694
8695 if (atpos_data)
8696 bidi_unshelve_cache (atpos_data, 1);
8697 if (atx_data)
8698 bidi_unshelve_cache (atx_data, 1);
8699 if (wrap_data)
8700 bidi_unshelve_cache (wrap_data, 1);
8701 if (ppos_data)
8702 bidi_unshelve_cache (ppos_data, 1);
8703
8704 /* Restore the iterator settings altered at the beginning of this
8705 function. */
8706 it->glyph_row = saved_glyph_row;
8707 return result;
8708 }
8709
8710 /* For external use. */
8711 void
8712 move_it_in_display_line (struct it *it,
8713 ptrdiff_t to_charpos, int to_x,
8714 enum move_operation_enum op)
8715 {
8716 if (it->line_wrap == WORD_WRAP
8717 && (op & MOVE_TO_X))
8718 {
8719 struct it save_it;
8720 void *save_data = NULL;
8721 int skip;
8722
8723 SAVE_IT (save_it, *it, save_data);
8724 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8725 /* When word-wrap is on, TO_X may lie past the end
8726 of a wrapped line. Then it->current is the
8727 character on the next line, so backtrack to the
8728 space before the wrap point. */
8729 if (skip == MOVE_LINE_CONTINUED)
8730 {
8731 int prev_x = max (it->current_x - 1, 0);
8732 RESTORE_IT (it, &save_it, save_data);
8733 move_it_in_display_line_to
8734 (it, -1, prev_x, MOVE_TO_X);
8735 }
8736 else
8737 bidi_unshelve_cache (save_data, 1);
8738 }
8739 else
8740 move_it_in_display_line_to (it, to_charpos, to_x, op);
8741 }
8742
8743
8744 /* Move IT forward until it satisfies one or more of the criteria in
8745 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8746
8747 OP is a bit-mask that specifies where to stop, and in particular,
8748 which of those four position arguments makes a difference. See the
8749 description of enum move_operation_enum.
8750
8751 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8752 screen line, this function will set IT to the next position that is
8753 displayed to the right of TO_CHARPOS on the screen. */
8754
8755 void
8756 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8757 {
8758 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8759 int line_height, line_start_x = 0, reached = 0;
8760 void *backup_data = NULL;
8761
8762 for (;;)
8763 {
8764 if (op & MOVE_TO_VPOS)
8765 {
8766 /* If no TO_CHARPOS and no TO_X specified, stop at the
8767 start of the line TO_VPOS. */
8768 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8769 {
8770 if (it->vpos == to_vpos)
8771 {
8772 reached = 1;
8773 break;
8774 }
8775 else
8776 skip = move_it_in_display_line_to (it, -1, -1, 0);
8777 }
8778 else
8779 {
8780 /* TO_VPOS >= 0 means stop at TO_X in the line at
8781 TO_VPOS, or at TO_POS, whichever comes first. */
8782 if (it->vpos == to_vpos)
8783 {
8784 reached = 2;
8785 break;
8786 }
8787
8788 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8789
8790 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8791 {
8792 reached = 3;
8793 break;
8794 }
8795 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8796 {
8797 /* We have reached TO_X but not in the line we want. */
8798 skip = move_it_in_display_line_to (it, to_charpos,
8799 -1, MOVE_TO_POS);
8800 if (skip == MOVE_POS_MATCH_OR_ZV)
8801 {
8802 reached = 4;
8803 break;
8804 }
8805 }
8806 }
8807 }
8808 else if (op & MOVE_TO_Y)
8809 {
8810 struct it it_backup;
8811
8812 if (it->line_wrap == WORD_WRAP)
8813 SAVE_IT (it_backup, *it, backup_data);
8814
8815 /* TO_Y specified means stop at TO_X in the line containing
8816 TO_Y---or at TO_CHARPOS if this is reached first. The
8817 problem is that we can't really tell whether the line
8818 contains TO_Y before we have completely scanned it, and
8819 this may skip past TO_X. What we do is to first scan to
8820 TO_X.
8821
8822 If TO_X is not specified, use a TO_X of zero. The reason
8823 is to make the outcome of this function more predictable.
8824 If we didn't use TO_X == 0, we would stop at the end of
8825 the line which is probably not what a caller would expect
8826 to happen. */
8827 skip = move_it_in_display_line_to
8828 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8829 (MOVE_TO_X | (op & MOVE_TO_POS)));
8830
8831 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8832 if (skip == MOVE_POS_MATCH_OR_ZV)
8833 reached = 5;
8834 else if (skip == MOVE_X_REACHED)
8835 {
8836 /* If TO_X was reached, we want to know whether TO_Y is
8837 in the line. We know this is the case if the already
8838 scanned glyphs make the line tall enough. Otherwise,
8839 we must check by scanning the rest of the line. */
8840 line_height = it->max_ascent + it->max_descent;
8841 if (to_y >= it->current_y
8842 && to_y < it->current_y + line_height)
8843 {
8844 reached = 6;
8845 break;
8846 }
8847 SAVE_IT (it_backup, *it, backup_data);
8848 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8849 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8850 op & MOVE_TO_POS);
8851 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8852 line_height = it->max_ascent + it->max_descent;
8853 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8854
8855 if (to_y >= it->current_y
8856 && to_y < it->current_y + line_height)
8857 {
8858 /* If TO_Y is in this line and TO_X was reached
8859 above, we scanned too far. We have to restore
8860 IT's settings to the ones before skipping. But
8861 keep the more accurate values of max_ascent and
8862 max_descent we've found while skipping the rest
8863 of the line, for the sake of callers, such as
8864 pos_visible_p, that need to know the line
8865 height. */
8866 int max_ascent = it->max_ascent;
8867 int max_descent = it->max_descent;
8868
8869 RESTORE_IT (it, &it_backup, backup_data);
8870 it->max_ascent = max_ascent;
8871 it->max_descent = max_descent;
8872 reached = 6;
8873 }
8874 else
8875 {
8876 skip = skip2;
8877 if (skip == MOVE_POS_MATCH_OR_ZV)
8878 reached = 7;
8879 }
8880 }
8881 else
8882 {
8883 /* Check whether TO_Y is in this line. */
8884 line_height = it->max_ascent + it->max_descent;
8885 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8886
8887 if (to_y >= it->current_y
8888 && to_y < it->current_y + line_height)
8889 {
8890 /* When word-wrap is on, TO_X may lie past the end
8891 of a wrapped line. Then it->current is the
8892 character on the next line, so backtrack to the
8893 space before the wrap point. */
8894 if (skip == MOVE_LINE_CONTINUED
8895 && it->line_wrap == WORD_WRAP)
8896 {
8897 int prev_x = max (it->current_x - 1, 0);
8898 RESTORE_IT (it, &it_backup, backup_data);
8899 skip = move_it_in_display_line_to
8900 (it, -1, prev_x, MOVE_TO_X);
8901 }
8902 reached = 6;
8903 }
8904 }
8905
8906 if (reached)
8907 break;
8908 }
8909 else if (BUFFERP (it->object)
8910 && (it->method == GET_FROM_BUFFER
8911 || it->method == GET_FROM_STRETCH)
8912 && IT_CHARPOS (*it) >= to_charpos
8913 /* Under bidi iteration, a call to set_iterator_to_next
8914 can scan far beyond to_charpos if the initial
8915 portion of the next line needs to be reordered. In
8916 that case, give move_it_in_display_line_to another
8917 chance below. */
8918 && !(it->bidi_p
8919 && it->bidi_it.scan_dir == -1))
8920 skip = MOVE_POS_MATCH_OR_ZV;
8921 else
8922 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8923
8924 switch (skip)
8925 {
8926 case MOVE_POS_MATCH_OR_ZV:
8927 reached = 8;
8928 goto out;
8929
8930 case MOVE_NEWLINE_OR_CR:
8931 set_iterator_to_next (it, 1);
8932 it->continuation_lines_width = 0;
8933 break;
8934
8935 case MOVE_LINE_TRUNCATED:
8936 it->continuation_lines_width = 0;
8937 reseat_at_next_visible_line_start (it, 0);
8938 if ((op & MOVE_TO_POS) != 0
8939 && IT_CHARPOS (*it) > to_charpos)
8940 {
8941 reached = 9;
8942 goto out;
8943 }
8944 break;
8945
8946 case MOVE_LINE_CONTINUED:
8947 /* For continued lines ending in a tab, some of the glyphs
8948 associated with the tab are displayed on the current
8949 line. Since it->current_x does not include these glyphs,
8950 we use it->last_visible_x instead. */
8951 if (it->c == '\t')
8952 {
8953 it->continuation_lines_width += it->last_visible_x;
8954 /* When moving by vpos, ensure that the iterator really
8955 advances to the next line (bug#847, bug#969). Fixme:
8956 do we need to do this in other circumstances? */
8957 if (it->current_x != it->last_visible_x
8958 && (op & MOVE_TO_VPOS)
8959 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8960 {
8961 line_start_x = it->current_x + it->pixel_width
8962 - it->last_visible_x;
8963 set_iterator_to_next (it, 0);
8964 }
8965 }
8966 else
8967 it->continuation_lines_width += it->current_x;
8968 break;
8969
8970 default:
8971 emacs_abort ();
8972 }
8973
8974 /* Reset/increment for the next run. */
8975 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8976 it->current_x = line_start_x;
8977 line_start_x = 0;
8978 it->hpos = 0;
8979 it->current_y += it->max_ascent + it->max_descent;
8980 ++it->vpos;
8981 last_height = it->max_ascent + it->max_descent;
8982 it->max_ascent = it->max_descent = 0;
8983 }
8984
8985 out:
8986
8987 /* On text terminals, we may stop at the end of a line in the middle
8988 of a multi-character glyph. If the glyph itself is continued,
8989 i.e. it is actually displayed on the next line, don't treat this
8990 stopping point as valid; move to the next line instead (unless
8991 that brings us offscreen). */
8992 if (!FRAME_WINDOW_P (it->f)
8993 && op & MOVE_TO_POS
8994 && IT_CHARPOS (*it) == to_charpos
8995 && it->what == IT_CHARACTER
8996 && it->nglyphs > 1
8997 && it->line_wrap == WINDOW_WRAP
8998 && it->current_x == it->last_visible_x - 1
8999 && it->c != '\n'
9000 && it->c != '\t'
9001 && it->vpos < XFASTINT (it->w->window_end_vpos))
9002 {
9003 it->continuation_lines_width += it->current_x;
9004 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9005 it->current_y += it->max_ascent + it->max_descent;
9006 ++it->vpos;
9007 last_height = it->max_ascent + it->max_descent;
9008 }
9009
9010 if (backup_data)
9011 bidi_unshelve_cache (backup_data, 1);
9012
9013 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9014 }
9015
9016
9017 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9018
9019 If DY > 0, move IT backward at least that many pixels. DY = 0
9020 means move IT backward to the preceding line start or BEGV. This
9021 function may move over more than DY pixels if IT->current_y - DY
9022 ends up in the middle of a line; in this case IT->current_y will be
9023 set to the top of the line moved to. */
9024
9025 void
9026 move_it_vertically_backward (struct it *it, int dy)
9027 {
9028 int nlines, h;
9029 struct it it2, it3;
9030 void *it2data = NULL, *it3data = NULL;
9031 ptrdiff_t start_pos;
9032 int nchars_per_row
9033 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9034 ptrdiff_t pos_limit;
9035
9036 move_further_back:
9037 eassert (dy >= 0);
9038
9039 start_pos = IT_CHARPOS (*it);
9040
9041 /* Estimate how many newlines we must move back. */
9042 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9043 if (it->line_wrap == TRUNCATE)
9044 pos_limit = BEGV;
9045 else
9046 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9047
9048 /* Set the iterator's position that many lines back. But don't go
9049 back more than NLINES full screen lines -- this wins a day with
9050 buffers which have very long lines. */
9051 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9052 back_to_previous_visible_line_start (it);
9053
9054 /* Reseat the iterator here. When moving backward, we don't want
9055 reseat to skip forward over invisible text, set up the iterator
9056 to deliver from overlay strings at the new position etc. So,
9057 use reseat_1 here. */
9058 reseat_1 (it, it->current.pos, 1);
9059
9060 /* We are now surely at a line start. */
9061 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9062 reordering is in effect. */
9063 it->continuation_lines_width = 0;
9064
9065 /* Move forward and see what y-distance we moved. First move to the
9066 start of the next line so that we get its height. We need this
9067 height to be able to tell whether we reached the specified
9068 y-distance. */
9069 SAVE_IT (it2, *it, it2data);
9070 it2.max_ascent = it2.max_descent = 0;
9071 do
9072 {
9073 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9074 MOVE_TO_POS | MOVE_TO_VPOS);
9075 }
9076 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9077 /* If we are in a display string which starts at START_POS,
9078 and that display string includes a newline, and we are
9079 right after that newline (i.e. at the beginning of a
9080 display line), exit the loop, because otherwise we will
9081 infloop, since move_it_to will see that it is already at
9082 START_POS and will not move. */
9083 || (it2.method == GET_FROM_STRING
9084 && IT_CHARPOS (it2) == start_pos
9085 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9086 eassert (IT_CHARPOS (*it) >= BEGV);
9087 SAVE_IT (it3, it2, it3data);
9088
9089 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9090 eassert (IT_CHARPOS (*it) >= BEGV);
9091 /* H is the actual vertical distance from the position in *IT
9092 and the starting position. */
9093 h = it2.current_y - it->current_y;
9094 /* NLINES is the distance in number of lines. */
9095 nlines = it2.vpos - it->vpos;
9096
9097 /* Correct IT's y and vpos position
9098 so that they are relative to the starting point. */
9099 it->vpos -= nlines;
9100 it->current_y -= h;
9101
9102 if (dy == 0)
9103 {
9104 /* DY == 0 means move to the start of the screen line. The
9105 value of nlines is > 0 if continuation lines were involved,
9106 or if the original IT position was at start of a line. */
9107 RESTORE_IT (it, it, it2data);
9108 if (nlines > 0)
9109 move_it_by_lines (it, nlines);
9110 /* The above code moves us to some position NLINES down,
9111 usually to its first glyph (leftmost in an L2R line), but
9112 that's not necessarily the start of the line, under bidi
9113 reordering. We want to get to the character position
9114 that is immediately after the newline of the previous
9115 line. */
9116 if (it->bidi_p
9117 && !it->continuation_lines_width
9118 && !STRINGP (it->string)
9119 && IT_CHARPOS (*it) > BEGV
9120 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9121 {
9122 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9123
9124 DEC_BOTH (cp, bp);
9125 cp = find_newline_no_quit (cp, bp, -1, NULL);
9126 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9127 }
9128 bidi_unshelve_cache (it3data, 1);
9129 }
9130 else
9131 {
9132 /* The y-position we try to reach, relative to *IT.
9133 Note that H has been subtracted in front of the if-statement. */
9134 int target_y = it->current_y + h - dy;
9135 int y0 = it3.current_y;
9136 int y1;
9137 int line_height;
9138
9139 RESTORE_IT (&it3, &it3, it3data);
9140 y1 = line_bottom_y (&it3);
9141 line_height = y1 - y0;
9142 RESTORE_IT (it, it, it2data);
9143 /* If we did not reach target_y, try to move further backward if
9144 we can. If we moved too far backward, try to move forward. */
9145 if (target_y < it->current_y
9146 /* This is heuristic. In a window that's 3 lines high, with
9147 a line height of 13 pixels each, recentering with point
9148 on the bottom line will try to move -39/2 = 19 pixels
9149 backward. Try to avoid moving into the first line. */
9150 && (it->current_y - target_y
9151 > min (window_box_height (it->w), line_height * 2 / 3))
9152 && IT_CHARPOS (*it) > BEGV)
9153 {
9154 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9155 target_y - it->current_y));
9156 dy = it->current_y - target_y;
9157 goto move_further_back;
9158 }
9159 else if (target_y >= it->current_y + line_height
9160 && IT_CHARPOS (*it) < ZV)
9161 {
9162 /* Should move forward by at least one line, maybe more.
9163
9164 Note: Calling move_it_by_lines can be expensive on
9165 terminal frames, where compute_motion is used (via
9166 vmotion) to do the job, when there are very long lines
9167 and truncate-lines is nil. That's the reason for
9168 treating terminal frames specially here. */
9169
9170 if (!FRAME_WINDOW_P (it->f))
9171 move_it_vertically (it, target_y - (it->current_y + line_height));
9172 else
9173 {
9174 do
9175 {
9176 move_it_by_lines (it, 1);
9177 }
9178 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9179 }
9180 }
9181 }
9182 }
9183
9184
9185 /* Move IT by a specified amount of pixel lines DY. DY negative means
9186 move backwards. DY = 0 means move to start of screen line. At the
9187 end, IT will be on the start of a screen line. */
9188
9189 void
9190 move_it_vertically (struct it *it, int dy)
9191 {
9192 if (dy <= 0)
9193 move_it_vertically_backward (it, -dy);
9194 else
9195 {
9196 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9197 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9198 MOVE_TO_POS | MOVE_TO_Y);
9199 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9200
9201 /* If buffer ends in ZV without a newline, move to the start of
9202 the line to satisfy the post-condition. */
9203 if (IT_CHARPOS (*it) == ZV
9204 && ZV > BEGV
9205 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9206 move_it_by_lines (it, 0);
9207 }
9208 }
9209
9210
9211 /* Move iterator IT past the end of the text line it is in. */
9212
9213 void
9214 move_it_past_eol (struct it *it)
9215 {
9216 enum move_it_result rc;
9217
9218 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9219 if (rc == MOVE_NEWLINE_OR_CR)
9220 set_iterator_to_next (it, 0);
9221 }
9222
9223
9224 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9225 negative means move up. DVPOS == 0 means move to the start of the
9226 screen line.
9227
9228 Optimization idea: If we would know that IT->f doesn't use
9229 a face with proportional font, we could be faster for
9230 truncate-lines nil. */
9231
9232 void
9233 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9234 {
9235
9236 /* The commented-out optimization uses vmotion on terminals. This
9237 gives bad results, because elements like it->what, on which
9238 callers such as pos_visible_p rely, aren't updated. */
9239 /* struct position pos;
9240 if (!FRAME_WINDOW_P (it->f))
9241 {
9242 struct text_pos textpos;
9243
9244 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9245 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9246 reseat (it, textpos, 1);
9247 it->vpos += pos.vpos;
9248 it->current_y += pos.vpos;
9249 }
9250 else */
9251
9252 if (dvpos == 0)
9253 {
9254 /* DVPOS == 0 means move to the start of the screen line. */
9255 move_it_vertically_backward (it, 0);
9256 /* Let next call to line_bottom_y calculate real line height */
9257 last_height = 0;
9258 }
9259 else if (dvpos > 0)
9260 {
9261 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9262 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9263 {
9264 /* Only move to the next buffer position if we ended up in a
9265 string from display property, not in an overlay string
9266 (before-string or after-string). That is because the
9267 latter don't conceal the underlying buffer position, so
9268 we can ask to move the iterator to the exact position we
9269 are interested in. Note that, even if we are already at
9270 IT_CHARPOS (*it), the call below is not a no-op, as it
9271 will detect that we are at the end of the string, pop the
9272 iterator, and compute it->current_x and it->hpos
9273 correctly. */
9274 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9275 -1, -1, -1, MOVE_TO_POS);
9276 }
9277 }
9278 else
9279 {
9280 struct it it2;
9281 void *it2data = NULL;
9282 ptrdiff_t start_charpos, i;
9283 int nchars_per_row
9284 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9285 ptrdiff_t pos_limit;
9286
9287 /* Start at the beginning of the screen line containing IT's
9288 position. This may actually move vertically backwards,
9289 in case of overlays, so adjust dvpos accordingly. */
9290 dvpos += it->vpos;
9291 move_it_vertically_backward (it, 0);
9292 dvpos -= it->vpos;
9293
9294 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9295 screen lines, and reseat the iterator there. */
9296 start_charpos = IT_CHARPOS (*it);
9297 if (it->line_wrap == TRUNCATE)
9298 pos_limit = BEGV;
9299 else
9300 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9301 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9302 back_to_previous_visible_line_start (it);
9303 reseat (it, it->current.pos, 1);
9304
9305 /* Move further back if we end up in a string or an image. */
9306 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9307 {
9308 /* First try to move to start of display line. */
9309 dvpos += it->vpos;
9310 move_it_vertically_backward (it, 0);
9311 dvpos -= it->vpos;
9312 if (IT_POS_VALID_AFTER_MOVE_P (it))
9313 break;
9314 /* If start of line is still in string or image,
9315 move further back. */
9316 back_to_previous_visible_line_start (it);
9317 reseat (it, it->current.pos, 1);
9318 dvpos--;
9319 }
9320
9321 it->current_x = it->hpos = 0;
9322
9323 /* Above call may have moved too far if continuation lines
9324 are involved. Scan forward and see if it did. */
9325 SAVE_IT (it2, *it, it2data);
9326 it2.vpos = it2.current_y = 0;
9327 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9328 it->vpos -= it2.vpos;
9329 it->current_y -= it2.current_y;
9330 it->current_x = it->hpos = 0;
9331
9332 /* If we moved too far back, move IT some lines forward. */
9333 if (it2.vpos > -dvpos)
9334 {
9335 int delta = it2.vpos + dvpos;
9336
9337 RESTORE_IT (&it2, &it2, it2data);
9338 SAVE_IT (it2, *it, it2data);
9339 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9340 /* Move back again if we got too far ahead. */
9341 if (IT_CHARPOS (*it) >= start_charpos)
9342 RESTORE_IT (it, &it2, it2data);
9343 else
9344 bidi_unshelve_cache (it2data, 1);
9345 }
9346 else
9347 RESTORE_IT (it, it, it2data);
9348 }
9349 }
9350
9351 /* Return 1 if IT points into the middle of a display vector. */
9352
9353 int
9354 in_display_vector_p (struct it *it)
9355 {
9356 return (it->method == GET_FROM_DISPLAY_VECTOR
9357 && it->current.dpvec_index > 0
9358 && it->dpvec + it->current.dpvec_index != it->dpend);
9359 }
9360
9361 \f
9362 /***********************************************************************
9363 Messages
9364 ***********************************************************************/
9365
9366
9367 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9368 to *Messages*. */
9369
9370 void
9371 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9372 {
9373 Lisp_Object args[3];
9374 Lisp_Object msg, fmt;
9375 char *buffer;
9376 ptrdiff_t len;
9377 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9378 USE_SAFE_ALLOCA;
9379
9380 fmt = msg = Qnil;
9381 GCPRO4 (fmt, msg, arg1, arg2);
9382
9383 args[0] = fmt = build_string (format);
9384 args[1] = arg1;
9385 args[2] = arg2;
9386 msg = Fformat (3, args);
9387
9388 len = SBYTES (msg) + 1;
9389 buffer = SAFE_ALLOCA (len);
9390 memcpy (buffer, SDATA (msg), len);
9391
9392 message_dolog (buffer, len - 1, 1, 0);
9393 SAFE_FREE ();
9394
9395 UNGCPRO;
9396 }
9397
9398
9399 /* Output a newline in the *Messages* buffer if "needs" one. */
9400
9401 void
9402 message_log_maybe_newline (void)
9403 {
9404 if (message_log_need_newline)
9405 message_dolog ("", 0, 1, 0);
9406 }
9407
9408
9409 /* Add a string M of length NBYTES to the message log, optionally
9410 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9411 true, means interpret the contents of M as multibyte. This
9412 function calls low-level routines in order to bypass text property
9413 hooks, etc. which might not be safe to run.
9414
9415 This may GC (insert may run before/after change hooks),
9416 so the buffer M must NOT point to a Lisp string. */
9417
9418 void
9419 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9420 {
9421 const unsigned char *msg = (const unsigned char *) m;
9422
9423 if (!NILP (Vmemory_full))
9424 return;
9425
9426 if (!NILP (Vmessage_log_max))
9427 {
9428 struct buffer *oldbuf;
9429 Lisp_Object oldpoint, oldbegv, oldzv;
9430 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9431 ptrdiff_t point_at_end = 0;
9432 ptrdiff_t zv_at_end = 0;
9433 Lisp_Object old_deactivate_mark;
9434 bool shown;
9435 struct gcpro gcpro1;
9436
9437 old_deactivate_mark = Vdeactivate_mark;
9438 oldbuf = current_buffer;
9439 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9440 bset_undo_list (current_buffer, Qt);
9441
9442 oldpoint = message_dolog_marker1;
9443 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9444 oldbegv = message_dolog_marker2;
9445 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9446 oldzv = message_dolog_marker3;
9447 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9448 GCPRO1 (old_deactivate_mark);
9449
9450 if (PT == Z)
9451 point_at_end = 1;
9452 if (ZV == Z)
9453 zv_at_end = 1;
9454
9455 BEGV = BEG;
9456 BEGV_BYTE = BEG_BYTE;
9457 ZV = Z;
9458 ZV_BYTE = Z_BYTE;
9459 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9460
9461 /* Insert the string--maybe converting multibyte to single byte
9462 or vice versa, so that all the text fits the buffer. */
9463 if (multibyte
9464 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9465 {
9466 ptrdiff_t i;
9467 int c, char_bytes;
9468 char work[1];
9469
9470 /* Convert a multibyte string to single-byte
9471 for the *Message* buffer. */
9472 for (i = 0; i < nbytes; i += char_bytes)
9473 {
9474 c = string_char_and_length (msg + i, &char_bytes);
9475 work[0] = (ASCII_CHAR_P (c)
9476 ? c
9477 : multibyte_char_to_unibyte (c));
9478 insert_1_both (work, 1, 1, 1, 0, 0);
9479 }
9480 }
9481 else if (! multibyte
9482 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9483 {
9484 ptrdiff_t i;
9485 int c, char_bytes;
9486 unsigned char str[MAX_MULTIBYTE_LENGTH];
9487 /* Convert a single-byte string to multibyte
9488 for the *Message* buffer. */
9489 for (i = 0; i < nbytes; i++)
9490 {
9491 c = msg[i];
9492 MAKE_CHAR_MULTIBYTE (c);
9493 char_bytes = CHAR_STRING (c, str);
9494 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9495 }
9496 }
9497 else if (nbytes)
9498 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9499
9500 if (nlflag)
9501 {
9502 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9503 printmax_t dups;
9504
9505 insert_1_both ("\n", 1, 1, 1, 0, 0);
9506
9507 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9508 this_bol = PT;
9509 this_bol_byte = PT_BYTE;
9510
9511 /* See if this line duplicates the previous one.
9512 If so, combine duplicates. */
9513 if (this_bol > BEG)
9514 {
9515 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9516 prev_bol = PT;
9517 prev_bol_byte = PT_BYTE;
9518
9519 dups = message_log_check_duplicate (prev_bol_byte,
9520 this_bol_byte);
9521 if (dups)
9522 {
9523 del_range_both (prev_bol, prev_bol_byte,
9524 this_bol, this_bol_byte, 0);
9525 if (dups > 1)
9526 {
9527 char dupstr[sizeof " [ times]"
9528 + INT_STRLEN_BOUND (printmax_t)];
9529
9530 /* If you change this format, don't forget to also
9531 change message_log_check_duplicate. */
9532 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9533 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9534 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9535 }
9536 }
9537 }
9538
9539 /* If we have more than the desired maximum number of lines
9540 in the *Messages* buffer now, delete the oldest ones.
9541 This is safe because we don't have undo in this buffer. */
9542
9543 if (NATNUMP (Vmessage_log_max))
9544 {
9545 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9546 -XFASTINT (Vmessage_log_max) - 1, 0);
9547 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9548 }
9549 }
9550 BEGV = marker_position (oldbegv);
9551 BEGV_BYTE = marker_byte_position (oldbegv);
9552
9553 if (zv_at_end)
9554 {
9555 ZV = Z;
9556 ZV_BYTE = Z_BYTE;
9557 }
9558 else
9559 {
9560 ZV = marker_position (oldzv);
9561 ZV_BYTE = marker_byte_position (oldzv);
9562 }
9563
9564 if (point_at_end)
9565 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9566 else
9567 /* We can't do Fgoto_char (oldpoint) because it will run some
9568 Lisp code. */
9569 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9570 marker_byte_position (oldpoint));
9571
9572 UNGCPRO;
9573 unchain_marker (XMARKER (oldpoint));
9574 unchain_marker (XMARKER (oldbegv));
9575 unchain_marker (XMARKER (oldzv));
9576
9577 shown = buffer_window_count (current_buffer) > 0;
9578 set_buffer_internal (oldbuf);
9579 if (!shown)
9580 windows_or_buffers_changed = old_windows_or_buffers_changed;
9581 message_log_need_newline = !nlflag;
9582 Vdeactivate_mark = old_deactivate_mark;
9583 }
9584 }
9585
9586
9587 /* We are at the end of the buffer after just having inserted a newline.
9588 (Note: We depend on the fact we won't be crossing the gap.)
9589 Check to see if the most recent message looks a lot like the previous one.
9590 Return 0 if different, 1 if the new one should just replace it, or a
9591 value N > 1 if we should also append " [N times]". */
9592
9593 static intmax_t
9594 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9595 {
9596 ptrdiff_t i;
9597 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9598 int seen_dots = 0;
9599 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9600 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9601
9602 for (i = 0; i < len; i++)
9603 {
9604 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9605 seen_dots = 1;
9606 if (p1[i] != p2[i])
9607 return seen_dots;
9608 }
9609 p1 += len;
9610 if (*p1 == '\n')
9611 return 2;
9612 if (*p1++ == ' ' && *p1++ == '[')
9613 {
9614 char *pend;
9615 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9616 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9617 return n + 1;
9618 }
9619 return 0;
9620 }
9621 \f
9622
9623 /* Display an echo area message M with a specified length of NBYTES
9624 bytes. The string may include null characters. If M is not a
9625 string, clear out any existing message, and let the mini-buffer
9626 text show through.
9627
9628 This function cancels echoing. */
9629
9630 void
9631 message3 (Lisp_Object m)
9632 {
9633 struct gcpro gcpro1;
9634
9635 GCPRO1 (m);
9636 clear_message (1,1);
9637 cancel_echoing ();
9638
9639 /* First flush out any partial line written with print. */
9640 message_log_maybe_newline ();
9641 if (STRINGP (m))
9642 {
9643 ptrdiff_t nbytes = SBYTES (m);
9644 bool multibyte = STRING_MULTIBYTE (m);
9645 USE_SAFE_ALLOCA;
9646 char *buffer = SAFE_ALLOCA (nbytes);
9647 memcpy (buffer, SDATA (m), nbytes);
9648 message_dolog (buffer, nbytes, 1, multibyte);
9649 SAFE_FREE ();
9650 }
9651 message3_nolog (m);
9652
9653 UNGCPRO;
9654 }
9655
9656
9657 /* The non-logging version of message3.
9658 This does not cancel echoing, because it is used for echoing.
9659 Perhaps we need to make a separate function for echoing
9660 and make this cancel echoing. */
9661
9662 void
9663 message3_nolog (Lisp_Object m)
9664 {
9665 struct frame *sf = SELECTED_FRAME ();
9666
9667 if (FRAME_INITIAL_P (sf))
9668 {
9669 if (noninteractive_need_newline)
9670 putc ('\n', stderr);
9671 noninteractive_need_newline = 0;
9672 if (STRINGP (m))
9673 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9674 if (cursor_in_echo_area == 0)
9675 fprintf (stderr, "\n");
9676 fflush (stderr);
9677 }
9678 /* Error messages get reported properly by cmd_error, so this must be just an
9679 informative message; if the frame hasn't really been initialized yet, just
9680 toss it. */
9681 else if (INTERACTIVE && sf->glyphs_initialized_p)
9682 {
9683 /* Get the frame containing the mini-buffer
9684 that the selected frame is using. */
9685 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9686 Lisp_Object frame = XWINDOW (mini_window)->frame;
9687 struct frame *f = XFRAME (frame);
9688
9689 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9690 Fmake_frame_visible (frame);
9691
9692 if (STRINGP (m) && SCHARS (m) > 0)
9693 {
9694 set_message (m);
9695 if (minibuffer_auto_raise)
9696 Fraise_frame (frame);
9697 /* Assume we are not echoing.
9698 (If we are, echo_now will override this.) */
9699 echo_message_buffer = Qnil;
9700 }
9701 else
9702 clear_message (1, 1);
9703
9704 do_pending_window_change (0);
9705 echo_area_display (1);
9706 do_pending_window_change (0);
9707 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9708 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9709 }
9710 }
9711
9712
9713 /* Display a null-terminated echo area message M. If M is 0, clear
9714 out any existing message, and let the mini-buffer text show through.
9715
9716 The buffer M must continue to exist until after the echo area gets
9717 cleared or some other message gets displayed there. Do not pass
9718 text that is stored in a Lisp string. Do not pass text in a buffer
9719 that was alloca'd. */
9720
9721 void
9722 message1 (const char *m)
9723 {
9724 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9725 }
9726
9727
9728 /* The non-logging counterpart of message1. */
9729
9730 void
9731 message1_nolog (const char *m)
9732 {
9733 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9734 }
9735
9736 /* Display a message M which contains a single %s
9737 which gets replaced with STRING. */
9738
9739 void
9740 message_with_string (const char *m, Lisp_Object string, int log)
9741 {
9742 CHECK_STRING (string);
9743
9744 if (noninteractive)
9745 {
9746 if (m)
9747 {
9748 if (noninteractive_need_newline)
9749 putc ('\n', stderr);
9750 noninteractive_need_newline = 0;
9751 fprintf (stderr, m, SDATA (string));
9752 if (!cursor_in_echo_area)
9753 fprintf (stderr, "\n");
9754 fflush (stderr);
9755 }
9756 }
9757 else if (INTERACTIVE)
9758 {
9759 /* The frame whose minibuffer we're going to display the message on.
9760 It may be larger than the selected frame, so we need
9761 to use its buffer, not the selected frame's buffer. */
9762 Lisp_Object mini_window;
9763 struct frame *f, *sf = SELECTED_FRAME ();
9764
9765 /* Get the frame containing the minibuffer
9766 that the selected frame is using. */
9767 mini_window = FRAME_MINIBUF_WINDOW (sf);
9768 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9769
9770 /* Error messages get reported properly by cmd_error, so this must be
9771 just an informative message; if the frame hasn't really been
9772 initialized yet, just toss it. */
9773 if (f->glyphs_initialized_p)
9774 {
9775 Lisp_Object args[2], msg;
9776 struct gcpro gcpro1, gcpro2;
9777
9778 args[0] = build_string (m);
9779 args[1] = msg = string;
9780 GCPRO2 (args[0], msg);
9781 gcpro1.nvars = 2;
9782
9783 msg = Fformat (2, args);
9784
9785 if (log)
9786 message3 (msg);
9787 else
9788 message3_nolog (msg);
9789
9790 UNGCPRO;
9791
9792 /* Print should start at the beginning of the message
9793 buffer next time. */
9794 message_buf_print = 0;
9795 }
9796 }
9797 }
9798
9799
9800 /* Dump an informative message to the minibuf. If M is 0, clear out
9801 any existing message, and let the mini-buffer text show through. */
9802
9803 static void
9804 vmessage (const char *m, va_list ap)
9805 {
9806 if (noninteractive)
9807 {
9808 if (m)
9809 {
9810 if (noninteractive_need_newline)
9811 putc ('\n', stderr);
9812 noninteractive_need_newline = 0;
9813 vfprintf (stderr, m, ap);
9814 if (cursor_in_echo_area == 0)
9815 fprintf (stderr, "\n");
9816 fflush (stderr);
9817 }
9818 }
9819 else if (INTERACTIVE)
9820 {
9821 /* The frame whose mini-buffer we're going to display the message
9822 on. It may be larger than the selected frame, so we need to
9823 use its buffer, not the selected frame's buffer. */
9824 Lisp_Object mini_window;
9825 struct frame *f, *sf = SELECTED_FRAME ();
9826
9827 /* Get the frame containing the mini-buffer
9828 that the selected frame is using. */
9829 mini_window = FRAME_MINIBUF_WINDOW (sf);
9830 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9831
9832 /* Error messages get reported properly by cmd_error, so this must be
9833 just an informative message; if the frame hasn't really been
9834 initialized yet, just toss it. */
9835 if (f->glyphs_initialized_p)
9836 {
9837 if (m)
9838 {
9839 ptrdiff_t len;
9840 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9841 char *message_buf = alloca (maxsize + 1);
9842
9843 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9844
9845 message3 (make_string (message_buf, len));
9846 }
9847 else
9848 message1 (0);
9849
9850 /* Print should start at the beginning of the message
9851 buffer next time. */
9852 message_buf_print = 0;
9853 }
9854 }
9855 }
9856
9857 void
9858 message (const char *m, ...)
9859 {
9860 va_list ap;
9861 va_start (ap, m);
9862 vmessage (m, ap);
9863 va_end (ap);
9864 }
9865
9866
9867 #if 0
9868 /* The non-logging version of message. */
9869
9870 void
9871 message_nolog (const char *m, ...)
9872 {
9873 Lisp_Object old_log_max;
9874 va_list ap;
9875 va_start (ap, m);
9876 old_log_max = Vmessage_log_max;
9877 Vmessage_log_max = Qnil;
9878 vmessage (m, ap);
9879 Vmessage_log_max = old_log_max;
9880 va_end (ap);
9881 }
9882 #endif
9883
9884
9885 /* Display the current message in the current mini-buffer. This is
9886 only called from error handlers in process.c, and is not time
9887 critical. */
9888
9889 void
9890 update_echo_area (void)
9891 {
9892 if (!NILP (echo_area_buffer[0]))
9893 {
9894 Lisp_Object string;
9895 string = Fcurrent_message ();
9896 message3 (string);
9897 }
9898 }
9899
9900
9901 /* Make sure echo area buffers in `echo_buffers' are live.
9902 If they aren't, make new ones. */
9903
9904 static void
9905 ensure_echo_area_buffers (void)
9906 {
9907 int i;
9908
9909 for (i = 0; i < 2; ++i)
9910 if (!BUFFERP (echo_buffer[i])
9911 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9912 {
9913 char name[30];
9914 Lisp_Object old_buffer;
9915 int j;
9916
9917 old_buffer = echo_buffer[i];
9918 echo_buffer[i] = Fget_buffer_create
9919 (make_formatted_string (name, " *Echo Area %d*", i));
9920 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9921 /* to force word wrap in echo area -
9922 it was decided to postpone this*/
9923 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9924
9925 for (j = 0; j < 2; ++j)
9926 if (EQ (old_buffer, echo_area_buffer[j]))
9927 echo_area_buffer[j] = echo_buffer[i];
9928 }
9929 }
9930
9931
9932 /* Call FN with args A1..A2 with either the current or last displayed
9933 echo_area_buffer as current buffer.
9934
9935 WHICH zero means use the current message buffer
9936 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9937 from echo_buffer[] and clear it.
9938
9939 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9940 suitable buffer from echo_buffer[] and clear it.
9941
9942 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9943 that the current message becomes the last displayed one, make
9944 choose a suitable buffer for echo_area_buffer[0], and clear it.
9945
9946 Value is what FN returns. */
9947
9948 static int
9949 with_echo_area_buffer (struct window *w, int which,
9950 int (*fn) (ptrdiff_t, Lisp_Object),
9951 ptrdiff_t a1, Lisp_Object a2)
9952 {
9953 Lisp_Object buffer;
9954 int this_one, the_other, clear_buffer_p, rc;
9955 ptrdiff_t count = SPECPDL_INDEX ();
9956
9957 /* If buffers aren't live, make new ones. */
9958 ensure_echo_area_buffers ();
9959
9960 clear_buffer_p = 0;
9961
9962 if (which == 0)
9963 this_one = 0, the_other = 1;
9964 else if (which > 0)
9965 this_one = 1, the_other = 0;
9966 else
9967 {
9968 this_one = 0, the_other = 1;
9969 clear_buffer_p = 1;
9970
9971 /* We need a fresh one in case the current echo buffer equals
9972 the one containing the last displayed echo area message. */
9973 if (!NILP (echo_area_buffer[this_one])
9974 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9975 echo_area_buffer[this_one] = Qnil;
9976 }
9977
9978 /* Choose a suitable buffer from echo_buffer[] is we don't
9979 have one. */
9980 if (NILP (echo_area_buffer[this_one]))
9981 {
9982 echo_area_buffer[this_one]
9983 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9984 ? echo_buffer[the_other]
9985 : echo_buffer[this_one]);
9986 clear_buffer_p = 1;
9987 }
9988
9989 buffer = echo_area_buffer[this_one];
9990
9991 /* Don't get confused by reusing the buffer used for echoing
9992 for a different purpose. */
9993 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9994 cancel_echoing ();
9995
9996 record_unwind_protect (unwind_with_echo_area_buffer,
9997 with_echo_area_buffer_unwind_data (w));
9998
9999 /* Make the echo area buffer current. Note that for display
10000 purposes, it is not necessary that the displayed window's buffer
10001 == current_buffer, except for text property lookup. So, let's
10002 only set that buffer temporarily here without doing a full
10003 Fset_window_buffer. We must also change w->pointm, though,
10004 because otherwise an assertions in unshow_buffer fails, and Emacs
10005 aborts. */
10006 set_buffer_internal_1 (XBUFFER (buffer));
10007 if (w)
10008 {
10009 wset_buffer (w, buffer);
10010 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10011 }
10012
10013 bset_undo_list (current_buffer, Qt);
10014 bset_read_only (current_buffer, Qnil);
10015 specbind (Qinhibit_read_only, Qt);
10016 specbind (Qinhibit_modification_hooks, Qt);
10017
10018 if (clear_buffer_p && Z > BEG)
10019 del_range (BEG, Z);
10020
10021 eassert (BEGV >= BEG);
10022 eassert (ZV <= Z && ZV >= BEGV);
10023
10024 rc = fn (a1, a2);
10025
10026 eassert (BEGV >= BEG);
10027 eassert (ZV <= Z && ZV >= BEGV);
10028
10029 unbind_to (count, Qnil);
10030 return rc;
10031 }
10032
10033
10034 /* Save state that should be preserved around the call to the function
10035 FN called in with_echo_area_buffer. */
10036
10037 static Lisp_Object
10038 with_echo_area_buffer_unwind_data (struct window *w)
10039 {
10040 int i = 0;
10041 Lisp_Object vector, tmp;
10042
10043 /* Reduce consing by keeping one vector in
10044 Vwith_echo_area_save_vector. */
10045 vector = Vwith_echo_area_save_vector;
10046 Vwith_echo_area_save_vector = Qnil;
10047
10048 if (NILP (vector))
10049 vector = Fmake_vector (make_number (9), Qnil);
10050
10051 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10052 ASET (vector, i, Vdeactivate_mark); ++i;
10053 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10054
10055 if (w)
10056 {
10057 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10058 ASET (vector, i, w->contents); ++i;
10059 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10060 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10061 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10062 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10063 }
10064 else
10065 {
10066 int end = i + 6;
10067 for (; i < end; ++i)
10068 ASET (vector, i, Qnil);
10069 }
10070
10071 eassert (i == ASIZE (vector));
10072 return vector;
10073 }
10074
10075
10076 /* Restore global state from VECTOR which was created by
10077 with_echo_area_buffer_unwind_data. */
10078
10079 static Lisp_Object
10080 unwind_with_echo_area_buffer (Lisp_Object vector)
10081 {
10082 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10083 Vdeactivate_mark = AREF (vector, 1);
10084 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10085
10086 if (WINDOWP (AREF (vector, 3)))
10087 {
10088 struct window *w;
10089 Lisp_Object buffer;
10090
10091 w = XWINDOW (AREF (vector, 3));
10092 buffer = AREF (vector, 4);
10093
10094 wset_buffer (w, buffer);
10095 set_marker_both (w->pointm, buffer,
10096 XFASTINT (AREF (vector, 5)),
10097 XFASTINT (AREF (vector, 6)));
10098 set_marker_both (w->start, buffer,
10099 XFASTINT (AREF (vector, 7)),
10100 XFASTINT (AREF (vector, 8)));
10101 }
10102
10103 Vwith_echo_area_save_vector = vector;
10104 return Qnil;
10105 }
10106
10107
10108 /* Set up the echo area for use by print functions. MULTIBYTE_P
10109 non-zero means we will print multibyte. */
10110
10111 void
10112 setup_echo_area_for_printing (int multibyte_p)
10113 {
10114 /* If we can't find an echo area any more, exit. */
10115 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10116 Fkill_emacs (Qnil);
10117
10118 ensure_echo_area_buffers ();
10119
10120 if (!message_buf_print)
10121 {
10122 /* A message has been output since the last time we printed.
10123 Choose a fresh echo area buffer. */
10124 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10125 echo_area_buffer[0] = echo_buffer[1];
10126 else
10127 echo_area_buffer[0] = echo_buffer[0];
10128
10129 /* Switch to that buffer and clear it. */
10130 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10131 bset_truncate_lines (current_buffer, Qnil);
10132
10133 if (Z > BEG)
10134 {
10135 ptrdiff_t count = SPECPDL_INDEX ();
10136 specbind (Qinhibit_read_only, Qt);
10137 /* Note that undo recording is always disabled. */
10138 del_range (BEG, Z);
10139 unbind_to (count, Qnil);
10140 }
10141 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10142
10143 /* Set up the buffer for the multibyteness we need. */
10144 if (multibyte_p
10145 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10146 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10147
10148 /* Raise the frame containing the echo area. */
10149 if (minibuffer_auto_raise)
10150 {
10151 struct frame *sf = SELECTED_FRAME ();
10152 Lisp_Object mini_window;
10153 mini_window = FRAME_MINIBUF_WINDOW (sf);
10154 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10155 }
10156
10157 message_log_maybe_newline ();
10158 message_buf_print = 1;
10159 }
10160 else
10161 {
10162 if (NILP (echo_area_buffer[0]))
10163 {
10164 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10165 echo_area_buffer[0] = echo_buffer[1];
10166 else
10167 echo_area_buffer[0] = echo_buffer[0];
10168 }
10169
10170 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10171 {
10172 /* Someone switched buffers between print requests. */
10173 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10174 bset_truncate_lines (current_buffer, Qnil);
10175 }
10176 }
10177 }
10178
10179
10180 /* Display an echo area message in window W. Value is non-zero if W's
10181 height is changed. If display_last_displayed_message_p is
10182 non-zero, display the message that was last displayed, otherwise
10183 display the current message. */
10184
10185 static int
10186 display_echo_area (struct window *w)
10187 {
10188 int i, no_message_p, window_height_changed_p;
10189
10190 /* Temporarily disable garbage collections while displaying the echo
10191 area. This is done because a GC can print a message itself.
10192 That message would modify the echo area buffer's contents while a
10193 redisplay of the buffer is going on, and seriously confuse
10194 redisplay. */
10195 ptrdiff_t count = inhibit_garbage_collection ();
10196
10197 /* If there is no message, we must call display_echo_area_1
10198 nevertheless because it resizes the window. But we will have to
10199 reset the echo_area_buffer in question to nil at the end because
10200 with_echo_area_buffer will sets it to an empty buffer. */
10201 i = display_last_displayed_message_p ? 1 : 0;
10202 no_message_p = NILP (echo_area_buffer[i]);
10203
10204 window_height_changed_p
10205 = with_echo_area_buffer (w, display_last_displayed_message_p,
10206 display_echo_area_1,
10207 (intptr_t) w, Qnil);
10208
10209 if (no_message_p)
10210 echo_area_buffer[i] = Qnil;
10211
10212 unbind_to (count, Qnil);
10213 return window_height_changed_p;
10214 }
10215
10216
10217 /* Helper for display_echo_area. Display the current buffer which
10218 contains the current echo area message in window W, a mini-window,
10219 a pointer to which is passed in A1. A2..A4 are currently not used.
10220 Change the height of W so that all of the message is displayed.
10221 Value is non-zero if height of W was changed. */
10222
10223 static int
10224 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10225 {
10226 intptr_t i1 = a1;
10227 struct window *w = (struct window *) i1;
10228 Lisp_Object window;
10229 struct text_pos start;
10230 int window_height_changed_p = 0;
10231
10232 /* Do this before displaying, so that we have a large enough glyph
10233 matrix for the display. If we can't get enough space for the
10234 whole text, display the last N lines. That works by setting w->start. */
10235 window_height_changed_p = resize_mini_window (w, 0);
10236
10237 /* Use the starting position chosen by resize_mini_window. */
10238 SET_TEXT_POS_FROM_MARKER (start, w->start);
10239
10240 /* Display. */
10241 clear_glyph_matrix (w->desired_matrix);
10242 XSETWINDOW (window, w);
10243 try_window (window, start, 0);
10244
10245 return window_height_changed_p;
10246 }
10247
10248
10249 /* Resize the echo area window to exactly the size needed for the
10250 currently displayed message, if there is one. If a mini-buffer
10251 is active, don't shrink it. */
10252
10253 void
10254 resize_echo_area_exactly (void)
10255 {
10256 if (BUFFERP (echo_area_buffer[0])
10257 && WINDOWP (echo_area_window))
10258 {
10259 struct window *w = XWINDOW (echo_area_window);
10260 int resized_p;
10261 Lisp_Object resize_exactly;
10262
10263 if (minibuf_level == 0)
10264 resize_exactly = Qt;
10265 else
10266 resize_exactly = Qnil;
10267
10268 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10269 (intptr_t) w, resize_exactly);
10270 if (resized_p)
10271 {
10272 ++windows_or_buffers_changed;
10273 ++update_mode_lines;
10274 redisplay_internal ();
10275 }
10276 }
10277 }
10278
10279
10280 /* Callback function for with_echo_area_buffer, when used from
10281 resize_echo_area_exactly. A1 contains a pointer to the window to
10282 resize, EXACTLY non-nil means resize the mini-window exactly to the
10283 size of the text displayed. A3 and A4 are not used. Value is what
10284 resize_mini_window returns. */
10285
10286 static int
10287 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10288 {
10289 intptr_t i1 = a1;
10290 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10291 }
10292
10293
10294 /* Resize mini-window W to fit the size of its contents. EXACT_P
10295 means size the window exactly to the size needed. Otherwise, it's
10296 only enlarged until W's buffer is empty.
10297
10298 Set W->start to the right place to begin display. If the whole
10299 contents fit, start at the beginning. Otherwise, start so as
10300 to make the end of the contents appear. This is particularly
10301 important for y-or-n-p, but seems desirable generally.
10302
10303 Value is non-zero if the window height has been changed. */
10304
10305 int
10306 resize_mini_window (struct window *w, int exact_p)
10307 {
10308 struct frame *f = XFRAME (w->frame);
10309 int window_height_changed_p = 0;
10310
10311 eassert (MINI_WINDOW_P (w));
10312
10313 /* By default, start display at the beginning. */
10314 set_marker_both (w->start, w->contents,
10315 BUF_BEGV (XBUFFER (w->contents)),
10316 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10317
10318 /* Don't resize windows while redisplaying a window; it would
10319 confuse redisplay functions when the size of the window they are
10320 displaying changes from under them. Such a resizing can happen,
10321 for instance, when which-func prints a long message while
10322 we are running fontification-functions. We're running these
10323 functions with safe_call which binds inhibit-redisplay to t. */
10324 if (!NILP (Vinhibit_redisplay))
10325 return 0;
10326
10327 /* Nil means don't try to resize. */
10328 if (NILP (Vresize_mini_windows)
10329 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10330 return 0;
10331
10332 if (!FRAME_MINIBUF_ONLY_P (f))
10333 {
10334 struct it it;
10335 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10336 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10337 int height;
10338 EMACS_INT max_height;
10339 int unit = FRAME_LINE_HEIGHT (f);
10340 struct text_pos start;
10341 struct buffer *old_current_buffer = NULL;
10342
10343 if (current_buffer != XBUFFER (w->contents))
10344 {
10345 old_current_buffer = current_buffer;
10346 set_buffer_internal (XBUFFER (w->contents));
10347 }
10348
10349 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10350
10351 /* Compute the max. number of lines specified by the user. */
10352 if (FLOATP (Vmax_mini_window_height))
10353 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10354 else if (INTEGERP (Vmax_mini_window_height))
10355 max_height = XINT (Vmax_mini_window_height);
10356 else
10357 max_height = total_height / 4;
10358
10359 /* Correct that max. height if it's bogus. */
10360 max_height = clip_to_bounds (1, max_height, total_height);
10361
10362 /* Find out the height of the text in the window. */
10363 if (it.line_wrap == TRUNCATE)
10364 height = 1;
10365 else
10366 {
10367 last_height = 0;
10368 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10369 if (it.max_ascent == 0 && it.max_descent == 0)
10370 height = it.current_y + last_height;
10371 else
10372 height = it.current_y + it.max_ascent + it.max_descent;
10373 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10374 height = (height + unit - 1) / unit;
10375 }
10376
10377 /* Compute a suitable window start. */
10378 if (height > max_height)
10379 {
10380 height = max_height;
10381 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10382 move_it_vertically_backward (&it, (height - 1) * unit);
10383 start = it.current.pos;
10384 }
10385 else
10386 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10387 SET_MARKER_FROM_TEXT_POS (w->start, start);
10388
10389 if (EQ (Vresize_mini_windows, Qgrow_only))
10390 {
10391 /* Let it grow only, until we display an empty message, in which
10392 case the window shrinks again. */
10393 if (height > WINDOW_TOTAL_LINES (w))
10394 {
10395 int old_height = WINDOW_TOTAL_LINES (w);
10396 freeze_window_starts (f, 1);
10397 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10398 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10399 }
10400 else if (height < WINDOW_TOTAL_LINES (w)
10401 && (exact_p || BEGV == ZV))
10402 {
10403 int old_height = WINDOW_TOTAL_LINES (w);
10404 freeze_window_starts (f, 0);
10405 shrink_mini_window (w);
10406 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10407 }
10408 }
10409 else
10410 {
10411 /* Always resize to exact size needed. */
10412 if (height > WINDOW_TOTAL_LINES (w))
10413 {
10414 int old_height = WINDOW_TOTAL_LINES (w);
10415 freeze_window_starts (f, 1);
10416 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10417 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10418 }
10419 else if (height < WINDOW_TOTAL_LINES (w))
10420 {
10421 int old_height = WINDOW_TOTAL_LINES (w);
10422 freeze_window_starts (f, 0);
10423 shrink_mini_window (w);
10424
10425 if (height)
10426 {
10427 freeze_window_starts (f, 1);
10428 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10429 }
10430
10431 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10432 }
10433 }
10434
10435 if (old_current_buffer)
10436 set_buffer_internal (old_current_buffer);
10437 }
10438
10439 return window_height_changed_p;
10440 }
10441
10442
10443 /* Value is the current message, a string, or nil if there is no
10444 current message. */
10445
10446 Lisp_Object
10447 current_message (void)
10448 {
10449 Lisp_Object msg;
10450
10451 if (!BUFFERP (echo_area_buffer[0]))
10452 msg = Qnil;
10453 else
10454 {
10455 with_echo_area_buffer (0, 0, current_message_1,
10456 (intptr_t) &msg, Qnil);
10457 if (NILP (msg))
10458 echo_area_buffer[0] = Qnil;
10459 }
10460
10461 return msg;
10462 }
10463
10464
10465 static int
10466 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10467 {
10468 intptr_t i1 = a1;
10469 Lisp_Object *msg = (Lisp_Object *) i1;
10470
10471 if (Z > BEG)
10472 *msg = make_buffer_string (BEG, Z, 1);
10473 else
10474 *msg = Qnil;
10475 return 0;
10476 }
10477
10478
10479 /* Push the current message on Vmessage_stack for later restoration
10480 by restore_message. Value is non-zero if the current message isn't
10481 empty. This is a relatively infrequent operation, so it's not
10482 worth optimizing. */
10483
10484 bool
10485 push_message (void)
10486 {
10487 Lisp_Object msg = current_message ();
10488 Vmessage_stack = Fcons (msg, Vmessage_stack);
10489 return STRINGP (msg);
10490 }
10491
10492
10493 /* Restore message display from the top of Vmessage_stack. */
10494
10495 void
10496 restore_message (void)
10497 {
10498 eassert (CONSP (Vmessage_stack));
10499 message3_nolog (XCAR (Vmessage_stack));
10500 }
10501
10502
10503 /* Handler for record_unwind_protect calling pop_message. */
10504
10505 Lisp_Object
10506 pop_message_unwind (Lisp_Object dummy)
10507 {
10508 pop_message ();
10509 return Qnil;
10510 }
10511
10512 /* Pop the top-most entry off Vmessage_stack. */
10513
10514 static void
10515 pop_message (void)
10516 {
10517 eassert (CONSP (Vmessage_stack));
10518 Vmessage_stack = XCDR (Vmessage_stack);
10519 }
10520
10521
10522 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10523 exits. If the stack is not empty, we have a missing pop_message
10524 somewhere. */
10525
10526 void
10527 check_message_stack (void)
10528 {
10529 if (!NILP (Vmessage_stack))
10530 emacs_abort ();
10531 }
10532
10533
10534 /* Truncate to NCHARS what will be displayed in the echo area the next
10535 time we display it---but don't redisplay it now. */
10536
10537 void
10538 truncate_echo_area (ptrdiff_t nchars)
10539 {
10540 if (nchars == 0)
10541 echo_area_buffer[0] = Qnil;
10542 else if (!noninteractive
10543 && INTERACTIVE
10544 && !NILP (echo_area_buffer[0]))
10545 {
10546 struct frame *sf = SELECTED_FRAME ();
10547 /* Error messages get reported properly by cmd_error, so this must be
10548 just an informative message; if the frame hasn't really been
10549 initialized yet, just toss it. */
10550 if (sf->glyphs_initialized_p)
10551 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10552 }
10553 }
10554
10555
10556 /* Helper function for truncate_echo_area. Truncate the current
10557 message to at most NCHARS characters. */
10558
10559 static int
10560 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10561 {
10562 if (BEG + nchars < Z)
10563 del_range (BEG + nchars, Z);
10564 if (Z == BEG)
10565 echo_area_buffer[0] = Qnil;
10566 return 0;
10567 }
10568
10569 /* Set the current message to STRING. */
10570
10571 static void
10572 set_message (Lisp_Object string)
10573 {
10574 eassert (STRINGP (string));
10575
10576 message_enable_multibyte = STRING_MULTIBYTE (string);
10577
10578 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10579 message_buf_print = 0;
10580 help_echo_showing_p = 0;
10581
10582 if (STRINGP (Vdebug_on_message)
10583 && STRINGP (string)
10584 && fast_string_match (Vdebug_on_message, string) >= 0)
10585 call_debugger (list2 (Qerror, string));
10586 }
10587
10588
10589 /* Helper function for set_message. First argument is ignored and second
10590 argument has the same meaning as for set_message.
10591 This function is called with the echo area buffer being current. */
10592
10593 static int
10594 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10595 {
10596 eassert (STRINGP (string));
10597
10598 /* Change multibyteness of the echo buffer appropriately. */
10599 if (message_enable_multibyte
10600 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10601 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10602
10603 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10604 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10605 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10606
10607 /* Insert new message at BEG. */
10608 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10609
10610 /* This function takes care of single/multibyte conversion.
10611 We just have to ensure that the echo area buffer has the right
10612 setting of enable_multibyte_characters. */
10613 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10614
10615 return 0;
10616 }
10617
10618
10619 /* Clear messages. CURRENT_P non-zero means clear the current
10620 message. LAST_DISPLAYED_P non-zero means clear the message
10621 last displayed. */
10622
10623 void
10624 clear_message (int current_p, int last_displayed_p)
10625 {
10626 if (current_p)
10627 {
10628 echo_area_buffer[0] = Qnil;
10629 message_cleared_p = 1;
10630 }
10631
10632 if (last_displayed_p)
10633 echo_area_buffer[1] = Qnil;
10634
10635 message_buf_print = 0;
10636 }
10637
10638 /* Clear garbaged frames.
10639
10640 This function is used where the old redisplay called
10641 redraw_garbaged_frames which in turn called redraw_frame which in
10642 turn called clear_frame. The call to clear_frame was a source of
10643 flickering. I believe a clear_frame is not necessary. It should
10644 suffice in the new redisplay to invalidate all current matrices,
10645 and ensure a complete redisplay of all windows. */
10646
10647 static void
10648 clear_garbaged_frames (void)
10649 {
10650 if (frame_garbaged)
10651 {
10652 Lisp_Object tail, frame;
10653 int changed_count = 0;
10654
10655 FOR_EACH_FRAME (tail, frame)
10656 {
10657 struct frame *f = XFRAME (frame);
10658
10659 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10660 {
10661 if (f->resized_p)
10662 {
10663 redraw_frame (f);
10664 f->force_flush_display_p = 1;
10665 }
10666 clear_current_matrices (f);
10667 changed_count++;
10668 f->garbaged = 0;
10669 f->resized_p = 0;
10670 }
10671 }
10672
10673 frame_garbaged = 0;
10674 if (changed_count)
10675 ++windows_or_buffers_changed;
10676 }
10677 }
10678
10679
10680 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10681 is non-zero update selected_frame. Value is non-zero if the
10682 mini-windows height has been changed. */
10683
10684 static int
10685 echo_area_display (int update_frame_p)
10686 {
10687 Lisp_Object mini_window;
10688 struct window *w;
10689 struct frame *f;
10690 int window_height_changed_p = 0;
10691 struct frame *sf = SELECTED_FRAME ();
10692
10693 mini_window = FRAME_MINIBUF_WINDOW (sf);
10694 w = XWINDOW (mini_window);
10695 f = XFRAME (WINDOW_FRAME (w));
10696
10697 /* Don't display if frame is invisible or not yet initialized. */
10698 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10699 return 0;
10700
10701 #ifdef HAVE_WINDOW_SYSTEM
10702 /* When Emacs starts, selected_frame may be the initial terminal
10703 frame. If we let this through, a message would be displayed on
10704 the terminal. */
10705 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10706 return 0;
10707 #endif /* HAVE_WINDOW_SYSTEM */
10708
10709 /* Redraw garbaged frames. */
10710 clear_garbaged_frames ();
10711
10712 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10713 {
10714 echo_area_window = mini_window;
10715 window_height_changed_p = display_echo_area (w);
10716 w->must_be_updated_p = 1;
10717
10718 /* Update the display, unless called from redisplay_internal.
10719 Also don't update the screen during redisplay itself. The
10720 update will happen at the end of redisplay, and an update
10721 here could cause confusion. */
10722 if (update_frame_p && !redisplaying_p)
10723 {
10724 int n = 0;
10725
10726 /* If the display update has been interrupted by pending
10727 input, update mode lines in the frame. Due to the
10728 pending input, it might have been that redisplay hasn't
10729 been called, so that mode lines above the echo area are
10730 garbaged. This looks odd, so we prevent it here. */
10731 if (!display_completed)
10732 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10733
10734 if (window_height_changed_p
10735 /* Don't do this if Emacs is shutting down. Redisplay
10736 needs to run hooks. */
10737 && !NILP (Vrun_hooks))
10738 {
10739 /* Must update other windows. Likewise as in other
10740 cases, don't let this update be interrupted by
10741 pending input. */
10742 ptrdiff_t count = SPECPDL_INDEX ();
10743 specbind (Qredisplay_dont_pause, Qt);
10744 windows_or_buffers_changed = 1;
10745 redisplay_internal ();
10746 unbind_to (count, Qnil);
10747 }
10748 else if (FRAME_WINDOW_P (f) && n == 0)
10749 {
10750 /* Window configuration is the same as before.
10751 Can do with a display update of the echo area,
10752 unless we displayed some mode lines. */
10753 update_single_window (w, 1);
10754 FRAME_RIF (f)->flush_display (f);
10755 }
10756 else
10757 update_frame (f, 1, 1);
10758
10759 /* If cursor is in the echo area, make sure that the next
10760 redisplay displays the minibuffer, so that the cursor will
10761 be replaced with what the minibuffer wants. */
10762 if (cursor_in_echo_area)
10763 ++windows_or_buffers_changed;
10764 }
10765 }
10766 else if (!EQ (mini_window, selected_window))
10767 windows_or_buffers_changed++;
10768
10769 /* Last displayed message is now the current message. */
10770 echo_area_buffer[1] = echo_area_buffer[0];
10771 /* Inform read_char that we're not echoing. */
10772 echo_message_buffer = Qnil;
10773
10774 /* Prevent redisplay optimization in redisplay_internal by resetting
10775 this_line_start_pos. This is done because the mini-buffer now
10776 displays the message instead of its buffer text. */
10777 if (EQ (mini_window, selected_window))
10778 CHARPOS (this_line_start_pos) = 0;
10779
10780 return window_height_changed_p;
10781 }
10782
10783 /* Nonzero if the current window's buffer is shown in more than one
10784 window and was modified since last redisplay. */
10785
10786 static int
10787 buffer_shared_and_changed (void)
10788 {
10789 return (buffer_window_count (current_buffer) > 1
10790 && UNCHANGED_MODIFIED < MODIFF);
10791 }
10792
10793 /* Nonzero if W doesn't reflect the actual state of current buffer due
10794 to its text or overlays change. FIXME: this may be called when
10795 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10796
10797 static int
10798 window_outdated (struct window *w)
10799 {
10800 return (w->last_modified < MODIFF
10801 || w->last_overlay_modified < OVERLAY_MODIFF);
10802 }
10803
10804 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10805 is enabled and mark of W's buffer was changed since last W's update. */
10806
10807 static int
10808 window_buffer_changed (struct window *w)
10809 {
10810 struct buffer *b = XBUFFER (w->contents);
10811
10812 eassert (BUFFER_LIVE_P (b));
10813
10814 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10815 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10816 != (w->region_showing != 0)));
10817 }
10818
10819 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10820
10821 static int
10822 mode_line_update_needed (struct window *w)
10823 {
10824 return (w->column_number_displayed != -1
10825 && !(PT == w->last_point && !window_outdated (w))
10826 && (w->column_number_displayed != current_column ()));
10827 }
10828
10829 /***********************************************************************
10830 Mode Lines and Frame Titles
10831 ***********************************************************************/
10832
10833 /* A buffer for constructing non-propertized mode-line strings and
10834 frame titles in it; allocated from the heap in init_xdisp and
10835 resized as needed in store_mode_line_noprop_char. */
10836
10837 static char *mode_line_noprop_buf;
10838
10839 /* The buffer's end, and a current output position in it. */
10840
10841 static char *mode_line_noprop_buf_end;
10842 static char *mode_line_noprop_ptr;
10843
10844 #define MODE_LINE_NOPROP_LEN(start) \
10845 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10846
10847 static enum {
10848 MODE_LINE_DISPLAY = 0,
10849 MODE_LINE_TITLE,
10850 MODE_LINE_NOPROP,
10851 MODE_LINE_STRING
10852 } mode_line_target;
10853
10854 /* Alist that caches the results of :propertize.
10855 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10856 static Lisp_Object mode_line_proptrans_alist;
10857
10858 /* List of strings making up the mode-line. */
10859 static Lisp_Object mode_line_string_list;
10860
10861 /* Base face property when building propertized mode line string. */
10862 static Lisp_Object mode_line_string_face;
10863 static Lisp_Object mode_line_string_face_prop;
10864
10865
10866 /* Unwind data for mode line strings */
10867
10868 static Lisp_Object Vmode_line_unwind_vector;
10869
10870 static Lisp_Object
10871 format_mode_line_unwind_data (struct frame *target_frame,
10872 struct buffer *obuf,
10873 Lisp_Object owin,
10874 int save_proptrans)
10875 {
10876 Lisp_Object vector, tmp;
10877
10878 /* Reduce consing by keeping one vector in
10879 Vwith_echo_area_save_vector. */
10880 vector = Vmode_line_unwind_vector;
10881 Vmode_line_unwind_vector = Qnil;
10882
10883 if (NILP (vector))
10884 vector = Fmake_vector (make_number (10), Qnil);
10885
10886 ASET (vector, 0, make_number (mode_line_target));
10887 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10888 ASET (vector, 2, mode_line_string_list);
10889 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10890 ASET (vector, 4, mode_line_string_face);
10891 ASET (vector, 5, mode_line_string_face_prop);
10892
10893 if (obuf)
10894 XSETBUFFER (tmp, obuf);
10895 else
10896 tmp = Qnil;
10897 ASET (vector, 6, tmp);
10898 ASET (vector, 7, owin);
10899 if (target_frame)
10900 {
10901 /* Similarly to `with-selected-window', if the operation selects
10902 a window on another frame, we must restore that frame's
10903 selected window, and (for a tty) the top-frame. */
10904 ASET (vector, 8, target_frame->selected_window);
10905 if (FRAME_TERMCAP_P (target_frame))
10906 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10907 }
10908
10909 return vector;
10910 }
10911
10912 static Lisp_Object
10913 unwind_format_mode_line (Lisp_Object vector)
10914 {
10915 Lisp_Object old_window = AREF (vector, 7);
10916 Lisp_Object target_frame_window = AREF (vector, 8);
10917 Lisp_Object old_top_frame = AREF (vector, 9);
10918
10919 mode_line_target = XINT (AREF (vector, 0));
10920 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10921 mode_line_string_list = AREF (vector, 2);
10922 if (! EQ (AREF (vector, 3), Qt))
10923 mode_line_proptrans_alist = AREF (vector, 3);
10924 mode_line_string_face = AREF (vector, 4);
10925 mode_line_string_face_prop = AREF (vector, 5);
10926
10927 /* Select window before buffer, since it may change the buffer. */
10928 if (!NILP (old_window))
10929 {
10930 /* If the operation that we are unwinding had selected a window
10931 on a different frame, reset its frame-selected-window. For a
10932 text terminal, reset its top-frame if necessary. */
10933 if (!NILP (target_frame_window))
10934 {
10935 Lisp_Object frame
10936 = WINDOW_FRAME (XWINDOW (target_frame_window));
10937
10938 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10939 Fselect_window (target_frame_window, Qt);
10940
10941 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10942 Fselect_frame (old_top_frame, Qt);
10943 }
10944
10945 Fselect_window (old_window, Qt);
10946 }
10947
10948 if (!NILP (AREF (vector, 6)))
10949 {
10950 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10951 ASET (vector, 6, Qnil);
10952 }
10953
10954 Vmode_line_unwind_vector = vector;
10955 return Qnil;
10956 }
10957
10958
10959 /* Store a single character C for the frame title in mode_line_noprop_buf.
10960 Re-allocate mode_line_noprop_buf if necessary. */
10961
10962 static void
10963 store_mode_line_noprop_char (char c)
10964 {
10965 /* If output position has reached the end of the allocated buffer,
10966 increase the buffer's size. */
10967 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10968 {
10969 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10970 ptrdiff_t size = len;
10971 mode_line_noprop_buf =
10972 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10973 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10974 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10975 }
10976
10977 *mode_line_noprop_ptr++ = c;
10978 }
10979
10980
10981 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10982 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10983 characters that yield more columns than PRECISION; PRECISION <= 0
10984 means copy the whole string. Pad with spaces until FIELD_WIDTH
10985 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10986 pad. Called from display_mode_element when it is used to build a
10987 frame title. */
10988
10989 static int
10990 store_mode_line_noprop (const char *string, int field_width, int precision)
10991 {
10992 const unsigned char *str = (const unsigned char *) string;
10993 int n = 0;
10994 ptrdiff_t dummy, nbytes;
10995
10996 /* Copy at most PRECISION chars from STR. */
10997 nbytes = strlen (string);
10998 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10999 while (nbytes--)
11000 store_mode_line_noprop_char (*str++);
11001
11002 /* Fill up with spaces until FIELD_WIDTH reached. */
11003 while (field_width > 0
11004 && n < field_width)
11005 {
11006 store_mode_line_noprop_char (' ');
11007 ++n;
11008 }
11009
11010 return n;
11011 }
11012
11013 /***********************************************************************
11014 Frame Titles
11015 ***********************************************************************/
11016
11017 #ifdef HAVE_WINDOW_SYSTEM
11018
11019 /* Set the title of FRAME, if it has changed. The title format is
11020 Vicon_title_format if FRAME is iconified, otherwise it is
11021 frame_title_format. */
11022
11023 static void
11024 x_consider_frame_title (Lisp_Object frame)
11025 {
11026 struct frame *f = XFRAME (frame);
11027
11028 if (FRAME_WINDOW_P (f)
11029 || FRAME_MINIBUF_ONLY_P (f)
11030 || f->explicit_name)
11031 {
11032 /* Do we have more than one visible frame on this X display? */
11033 Lisp_Object tail, other_frame, fmt;
11034 ptrdiff_t title_start;
11035 char *title;
11036 ptrdiff_t len;
11037 struct it it;
11038 ptrdiff_t count = SPECPDL_INDEX ();
11039
11040 FOR_EACH_FRAME (tail, other_frame)
11041 {
11042 struct frame *tf = XFRAME (other_frame);
11043
11044 if (tf != f
11045 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11046 && !FRAME_MINIBUF_ONLY_P (tf)
11047 && !EQ (other_frame, tip_frame)
11048 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11049 break;
11050 }
11051
11052 /* Set global variable indicating that multiple frames exist. */
11053 multiple_frames = CONSP (tail);
11054
11055 /* Switch to the buffer of selected window of the frame. Set up
11056 mode_line_target so that display_mode_element will output into
11057 mode_line_noprop_buf; then display the title. */
11058 record_unwind_protect (unwind_format_mode_line,
11059 format_mode_line_unwind_data
11060 (f, current_buffer, selected_window, 0));
11061
11062 Fselect_window (f->selected_window, Qt);
11063 set_buffer_internal_1
11064 (XBUFFER (XWINDOW (f->selected_window)->contents));
11065 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11066
11067 mode_line_target = MODE_LINE_TITLE;
11068 title_start = MODE_LINE_NOPROP_LEN (0);
11069 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11070 NULL, DEFAULT_FACE_ID);
11071 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11072 len = MODE_LINE_NOPROP_LEN (title_start);
11073 title = mode_line_noprop_buf + title_start;
11074 unbind_to (count, Qnil);
11075
11076 /* Set the title only if it's changed. This avoids consing in
11077 the common case where it hasn't. (If it turns out that we've
11078 already wasted too much time by walking through the list with
11079 display_mode_element, then we might need to optimize at a
11080 higher level than this.) */
11081 if (! STRINGP (f->name)
11082 || SBYTES (f->name) != len
11083 || memcmp (title, SDATA (f->name), len) != 0)
11084 x_implicitly_set_name (f, make_string (title, len), Qnil);
11085 }
11086 }
11087
11088 #endif /* not HAVE_WINDOW_SYSTEM */
11089
11090 \f
11091 /***********************************************************************
11092 Menu Bars
11093 ***********************************************************************/
11094
11095
11096 /* Prepare for redisplay by updating menu-bar item lists when
11097 appropriate. This can call eval. */
11098
11099 void
11100 prepare_menu_bars (void)
11101 {
11102 int all_windows;
11103 struct gcpro gcpro1, gcpro2;
11104 struct frame *f;
11105 Lisp_Object tooltip_frame;
11106
11107 #ifdef HAVE_WINDOW_SYSTEM
11108 tooltip_frame = tip_frame;
11109 #else
11110 tooltip_frame = Qnil;
11111 #endif
11112
11113 /* Update all frame titles based on their buffer names, etc. We do
11114 this before the menu bars so that the buffer-menu will show the
11115 up-to-date frame titles. */
11116 #ifdef HAVE_WINDOW_SYSTEM
11117 if (windows_or_buffers_changed || update_mode_lines)
11118 {
11119 Lisp_Object tail, frame;
11120
11121 FOR_EACH_FRAME (tail, frame)
11122 {
11123 f = XFRAME (frame);
11124 if (!EQ (frame, tooltip_frame)
11125 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11126 x_consider_frame_title (frame);
11127 }
11128 }
11129 #endif /* HAVE_WINDOW_SYSTEM */
11130
11131 /* Update the menu bar item lists, if appropriate. This has to be
11132 done before any actual redisplay or generation of display lines. */
11133 all_windows = (update_mode_lines
11134 || buffer_shared_and_changed ()
11135 || windows_or_buffers_changed);
11136 if (all_windows)
11137 {
11138 Lisp_Object tail, frame;
11139 ptrdiff_t count = SPECPDL_INDEX ();
11140 /* 1 means that update_menu_bar has run its hooks
11141 so any further calls to update_menu_bar shouldn't do so again. */
11142 int menu_bar_hooks_run = 0;
11143
11144 record_unwind_save_match_data ();
11145
11146 FOR_EACH_FRAME (tail, frame)
11147 {
11148 f = XFRAME (frame);
11149
11150 /* Ignore tooltip frame. */
11151 if (EQ (frame, tooltip_frame))
11152 continue;
11153
11154 /* If a window on this frame changed size, report that to
11155 the user and clear the size-change flag. */
11156 if (FRAME_WINDOW_SIZES_CHANGED (f))
11157 {
11158 Lisp_Object functions;
11159
11160 /* Clear flag first in case we get an error below. */
11161 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11162 functions = Vwindow_size_change_functions;
11163 GCPRO2 (tail, functions);
11164
11165 while (CONSP (functions))
11166 {
11167 if (!EQ (XCAR (functions), Qt))
11168 call1 (XCAR (functions), frame);
11169 functions = XCDR (functions);
11170 }
11171 UNGCPRO;
11172 }
11173
11174 GCPRO1 (tail);
11175 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11176 #ifdef HAVE_WINDOW_SYSTEM
11177 update_tool_bar (f, 0);
11178 #endif
11179 #ifdef HAVE_NS
11180 if (windows_or_buffers_changed
11181 && FRAME_NS_P (f))
11182 ns_set_doc_edited
11183 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11184 #endif
11185 UNGCPRO;
11186 }
11187
11188 unbind_to (count, Qnil);
11189 }
11190 else
11191 {
11192 struct frame *sf = SELECTED_FRAME ();
11193 update_menu_bar (sf, 1, 0);
11194 #ifdef HAVE_WINDOW_SYSTEM
11195 update_tool_bar (sf, 1);
11196 #endif
11197 }
11198 }
11199
11200
11201 /* Update the menu bar item list for frame F. This has to be done
11202 before we start to fill in any display lines, because it can call
11203 eval.
11204
11205 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11206
11207 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11208 already ran the menu bar hooks for this redisplay, so there
11209 is no need to run them again. The return value is the
11210 updated value of this flag, to pass to the next call. */
11211
11212 static int
11213 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11214 {
11215 Lisp_Object window;
11216 register struct window *w;
11217
11218 /* If called recursively during a menu update, do nothing. This can
11219 happen when, for instance, an activate-menubar-hook causes a
11220 redisplay. */
11221 if (inhibit_menubar_update)
11222 return hooks_run;
11223
11224 window = FRAME_SELECTED_WINDOW (f);
11225 w = XWINDOW (window);
11226
11227 if (FRAME_WINDOW_P (f)
11228 ?
11229 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11230 || defined (HAVE_NS) || defined (USE_GTK)
11231 FRAME_EXTERNAL_MENU_BAR (f)
11232 #else
11233 FRAME_MENU_BAR_LINES (f) > 0
11234 #endif
11235 : FRAME_MENU_BAR_LINES (f) > 0)
11236 {
11237 /* If the user has switched buffers or windows, we need to
11238 recompute to reflect the new bindings. But we'll
11239 recompute when update_mode_lines is set too; that means
11240 that people can use force-mode-line-update to request
11241 that the menu bar be recomputed. The adverse effect on
11242 the rest of the redisplay algorithm is about the same as
11243 windows_or_buffers_changed anyway. */
11244 if (windows_or_buffers_changed
11245 /* This used to test w->update_mode_line, but we believe
11246 there is no need to recompute the menu in that case. */
11247 || update_mode_lines
11248 || window_buffer_changed (w))
11249 {
11250 struct buffer *prev = current_buffer;
11251 ptrdiff_t count = SPECPDL_INDEX ();
11252
11253 specbind (Qinhibit_menubar_update, Qt);
11254
11255 set_buffer_internal_1 (XBUFFER (w->contents));
11256 if (save_match_data)
11257 record_unwind_save_match_data ();
11258 if (NILP (Voverriding_local_map_menu_flag))
11259 {
11260 specbind (Qoverriding_terminal_local_map, Qnil);
11261 specbind (Qoverriding_local_map, Qnil);
11262 }
11263
11264 if (!hooks_run)
11265 {
11266 /* Run the Lucid hook. */
11267 safe_run_hooks (Qactivate_menubar_hook);
11268
11269 /* If it has changed current-menubar from previous value,
11270 really recompute the menu-bar from the value. */
11271 if (! NILP (Vlucid_menu_bar_dirty_flag))
11272 call0 (Qrecompute_lucid_menubar);
11273
11274 safe_run_hooks (Qmenu_bar_update_hook);
11275
11276 hooks_run = 1;
11277 }
11278
11279 XSETFRAME (Vmenu_updating_frame, f);
11280 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11281
11282 /* Redisplay the menu bar in case we changed it. */
11283 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11284 || defined (HAVE_NS) || defined (USE_GTK)
11285 if (FRAME_WINDOW_P (f))
11286 {
11287 #if defined (HAVE_NS)
11288 /* All frames on Mac OS share the same menubar. So only
11289 the selected frame should be allowed to set it. */
11290 if (f == SELECTED_FRAME ())
11291 #endif
11292 set_frame_menubar (f, 0, 0);
11293 }
11294 else
11295 /* On a terminal screen, the menu bar is an ordinary screen
11296 line, and this makes it get updated. */
11297 w->update_mode_line = 1;
11298 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11299 /* In the non-toolkit version, the menu bar is an ordinary screen
11300 line, and this makes it get updated. */
11301 w->update_mode_line = 1;
11302 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11303
11304 unbind_to (count, Qnil);
11305 set_buffer_internal_1 (prev);
11306 }
11307 }
11308
11309 return hooks_run;
11310 }
11311
11312
11313 \f
11314 /***********************************************************************
11315 Output Cursor
11316 ***********************************************************************/
11317
11318 #ifdef HAVE_WINDOW_SYSTEM
11319
11320 /* EXPORT:
11321 Nominal cursor position -- where to draw output.
11322 HPOS and VPOS are window relative glyph matrix coordinates.
11323 X and Y are window relative pixel coordinates. */
11324
11325 struct cursor_pos output_cursor;
11326
11327
11328 /* EXPORT:
11329 Set the global variable output_cursor to CURSOR. All cursor
11330 positions are relative to updated_window. */
11331
11332 void
11333 set_output_cursor (struct cursor_pos *cursor)
11334 {
11335 output_cursor.hpos = cursor->hpos;
11336 output_cursor.vpos = cursor->vpos;
11337 output_cursor.x = cursor->x;
11338 output_cursor.y = cursor->y;
11339 }
11340
11341
11342 /* EXPORT for RIF:
11343 Set a nominal cursor position.
11344
11345 HPOS and VPOS are column/row positions in a window glyph matrix. X
11346 and Y are window text area relative pixel positions.
11347
11348 If this is done during an update, updated_window will contain the
11349 window that is being updated and the position is the future output
11350 cursor position for that window. If updated_window is null, use
11351 selected_window and display the cursor at the given position. */
11352
11353 void
11354 x_cursor_to (int vpos, int hpos, int y, int x)
11355 {
11356 struct window *w;
11357
11358 /* If updated_window is not set, work on selected_window. */
11359 if (updated_window)
11360 w = updated_window;
11361 else
11362 w = XWINDOW (selected_window);
11363
11364 /* Set the output cursor. */
11365 output_cursor.hpos = hpos;
11366 output_cursor.vpos = vpos;
11367 output_cursor.x = x;
11368 output_cursor.y = y;
11369
11370 /* If not called as part of an update, really display the cursor.
11371 This will also set the cursor position of W. */
11372 if (updated_window == NULL)
11373 {
11374 block_input ();
11375 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11376 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11377 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11378 unblock_input ();
11379 }
11380 }
11381
11382 #endif /* HAVE_WINDOW_SYSTEM */
11383
11384 \f
11385 /***********************************************************************
11386 Tool-bars
11387 ***********************************************************************/
11388
11389 #ifdef HAVE_WINDOW_SYSTEM
11390
11391 /* Where the mouse was last time we reported a mouse event. */
11392
11393 FRAME_PTR last_mouse_frame;
11394
11395 /* Tool-bar item index of the item on which a mouse button was pressed
11396 or -1. */
11397
11398 int last_tool_bar_item;
11399
11400 /* Select `frame' temporarily without running all the code in
11401 do_switch_frame.
11402 FIXME: Maybe do_switch_frame should be trimmed down similarly
11403 when `norecord' is set. */
11404 static Lisp_Object
11405 fast_set_selected_frame (Lisp_Object frame)
11406 {
11407 if (!EQ (selected_frame, frame))
11408 {
11409 selected_frame = frame;
11410 selected_window = XFRAME (frame)->selected_window;
11411 }
11412 return Qnil;
11413 }
11414
11415 /* Update the tool-bar item list for frame F. This has to be done
11416 before we start to fill in any display lines. Called from
11417 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11418 and restore it here. */
11419
11420 static void
11421 update_tool_bar (struct frame *f, int save_match_data)
11422 {
11423 #if defined (USE_GTK) || defined (HAVE_NS)
11424 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11425 #else
11426 int do_update = WINDOWP (f->tool_bar_window)
11427 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11428 #endif
11429
11430 if (do_update)
11431 {
11432 Lisp_Object window;
11433 struct window *w;
11434
11435 window = FRAME_SELECTED_WINDOW (f);
11436 w = XWINDOW (window);
11437
11438 /* If the user has switched buffers or windows, we need to
11439 recompute to reflect the new bindings. But we'll
11440 recompute when update_mode_lines is set too; that means
11441 that people can use force-mode-line-update to request
11442 that the menu bar be recomputed. The adverse effect on
11443 the rest of the redisplay algorithm is about the same as
11444 windows_or_buffers_changed anyway. */
11445 if (windows_or_buffers_changed
11446 || w->update_mode_line
11447 || update_mode_lines
11448 || window_buffer_changed (w))
11449 {
11450 struct buffer *prev = current_buffer;
11451 ptrdiff_t count = SPECPDL_INDEX ();
11452 Lisp_Object frame, new_tool_bar;
11453 int new_n_tool_bar;
11454 struct gcpro gcpro1;
11455
11456 /* Set current_buffer to the buffer of the selected
11457 window of the frame, so that we get the right local
11458 keymaps. */
11459 set_buffer_internal_1 (XBUFFER (w->contents));
11460
11461 /* Save match data, if we must. */
11462 if (save_match_data)
11463 record_unwind_save_match_data ();
11464
11465 /* Make sure that we don't accidentally use bogus keymaps. */
11466 if (NILP (Voverriding_local_map_menu_flag))
11467 {
11468 specbind (Qoverriding_terminal_local_map, Qnil);
11469 specbind (Qoverriding_local_map, Qnil);
11470 }
11471
11472 GCPRO1 (new_tool_bar);
11473
11474 /* We must temporarily set the selected frame to this frame
11475 before calling tool_bar_items, because the calculation of
11476 the tool-bar keymap uses the selected frame (see
11477 `tool-bar-make-keymap' in tool-bar.el). */
11478 eassert (EQ (selected_window,
11479 /* Since we only explicitly preserve selected_frame,
11480 check that selected_window would be redundant. */
11481 XFRAME (selected_frame)->selected_window));
11482 record_unwind_protect (fast_set_selected_frame, selected_frame);
11483 XSETFRAME (frame, f);
11484 fast_set_selected_frame (frame);
11485
11486 /* Build desired tool-bar items from keymaps. */
11487 new_tool_bar
11488 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11489 &new_n_tool_bar);
11490
11491 /* Redisplay the tool-bar if we changed it. */
11492 if (new_n_tool_bar != f->n_tool_bar_items
11493 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11494 {
11495 /* Redisplay that happens asynchronously due to an expose event
11496 may access f->tool_bar_items. Make sure we update both
11497 variables within BLOCK_INPUT so no such event interrupts. */
11498 block_input ();
11499 fset_tool_bar_items (f, new_tool_bar);
11500 f->n_tool_bar_items = new_n_tool_bar;
11501 w->update_mode_line = 1;
11502 unblock_input ();
11503 }
11504
11505 UNGCPRO;
11506
11507 unbind_to (count, Qnil);
11508 set_buffer_internal_1 (prev);
11509 }
11510 }
11511 }
11512
11513
11514 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11515 F's desired tool-bar contents. F->tool_bar_items must have
11516 been set up previously by calling prepare_menu_bars. */
11517
11518 static void
11519 build_desired_tool_bar_string (struct frame *f)
11520 {
11521 int i, size, size_needed;
11522 struct gcpro gcpro1, gcpro2, gcpro3;
11523 Lisp_Object image, plist, props;
11524
11525 image = plist = props = Qnil;
11526 GCPRO3 (image, plist, props);
11527
11528 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11529 Otherwise, make a new string. */
11530
11531 /* The size of the string we might be able to reuse. */
11532 size = (STRINGP (f->desired_tool_bar_string)
11533 ? SCHARS (f->desired_tool_bar_string)
11534 : 0);
11535
11536 /* We need one space in the string for each image. */
11537 size_needed = f->n_tool_bar_items;
11538
11539 /* Reuse f->desired_tool_bar_string, if possible. */
11540 if (size < size_needed || NILP (f->desired_tool_bar_string))
11541 fset_desired_tool_bar_string
11542 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11543 else
11544 {
11545 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11546 Fremove_text_properties (make_number (0), make_number (size),
11547 props, f->desired_tool_bar_string);
11548 }
11549
11550 /* Put a `display' property on the string for the images to display,
11551 put a `menu_item' property on tool-bar items with a value that
11552 is the index of the item in F's tool-bar item vector. */
11553 for (i = 0; i < f->n_tool_bar_items; ++i)
11554 {
11555 #define PROP(IDX) \
11556 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11557
11558 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11559 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11560 int hmargin, vmargin, relief, idx, end;
11561
11562 /* If image is a vector, choose the image according to the
11563 button state. */
11564 image = PROP (TOOL_BAR_ITEM_IMAGES);
11565 if (VECTORP (image))
11566 {
11567 if (enabled_p)
11568 idx = (selected_p
11569 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11570 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11571 else
11572 idx = (selected_p
11573 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11574 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11575
11576 eassert (ASIZE (image) >= idx);
11577 image = AREF (image, idx);
11578 }
11579 else
11580 idx = -1;
11581
11582 /* Ignore invalid image specifications. */
11583 if (!valid_image_p (image))
11584 continue;
11585
11586 /* Display the tool-bar button pressed, or depressed. */
11587 plist = Fcopy_sequence (XCDR (image));
11588
11589 /* Compute margin and relief to draw. */
11590 relief = (tool_bar_button_relief >= 0
11591 ? tool_bar_button_relief
11592 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11593 hmargin = vmargin = relief;
11594
11595 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11596 INT_MAX - max (hmargin, vmargin)))
11597 {
11598 hmargin += XFASTINT (Vtool_bar_button_margin);
11599 vmargin += XFASTINT (Vtool_bar_button_margin);
11600 }
11601 else if (CONSP (Vtool_bar_button_margin))
11602 {
11603 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11604 INT_MAX - hmargin))
11605 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11606
11607 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11608 INT_MAX - vmargin))
11609 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11610 }
11611
11612 if (auto_raise_tool_bar_buttons_p)
11613 {
11614 /* Add a `:relief' property to the image spec if the item is
11615 selected. */
11616 if (selected_p)
11617 {
11618 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11619 hmargin -= relief;
11620 vmargin -= relief;
11621 }
11622 }
11623 else
11624 {
11625 /* If image is selected, display it pressed, i.e. with a
11626 negative relief. If it's not selected, display it with a
11627 raised relief. */
11628 plist = Fplist_put (plist, QCrelief,
11629 (selected_p
11630 ? make_number (-relief)
11631 : make_number (relief)));
11632 hmargin -= relief;
11633 vmargin -= relief;
11634 }
11635
11636 /* Put a margin around the image. */
11637 if (hmargin || vmargin)
11638 {
11639 if (hmargin == vmargin)
11640 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11641 else
11642 plist = Fplist_put (plist, QCmargin,
11643 Fcons (make_number (hmargin),
11644 make_number (vmargin)));
11645 }
11646
11647 /* If button is not enabled, and we don't have special images
11648 for the disabled state, make the image appear disabled by
11649 applying an appropriate algorithm to it. */
11650 if (!enabled_p && idx < 0)
11651 plist = Fplist_put (plist, QCconversion, Qdisabled);
11652
11653 /* Put a `display' text property on the string for the image to
11654 display. Put a `menu-item' property on the string that gives
11655 the start of this item's properties in the tool-bar items
11656 vector. */
11657 image = Fcons (Qimage, plist);
11658 props = list4 (Qdisplay, image,
11659 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11660
11661 /* Let the last image hide all remaining spaces in the tool bar
11662 string. The string can be longer than needed when we reuse a
11663 previous string. */
11664 if (i + 1 == f->n_tool_bar_items)
11665 end = SCHARS (f->desired_tool_bar_string);
11666 else
11667 end = i + 1;
11668 Fadd_text_properties (make_number (i), make_number (end),
11669 props, f->desired_tool_bar_string);
11670 #undef PROP
11671 }
11672
11673 UNGCPRO;
11674 }
11675
11676
11677 /* Display one line of the tool-bar of frame IT->f.
11678
11679 HEIGHT specifies the desired height of the tool-bar line.
11680 If the actual height of the glyph row is less than HEIGHT, the
11681 row's height is increased to HEIGHT, and the icons are centered
11682 vertically in the new height.
11683
11684 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11685 count a final empty row in case the tool-bar width exactly matches
11686 the window width.
11687 */
11688
11689 static void
11690 display_tool_bar_line (struct it *it, int height)
11691 {
11692 struct glyph_row *row = it->glyph_row;
11693 int max_x = it->last_visible_x;
11694 struct glyph *last;
11695
11696 prepare_desired_row (row);
11697 row->y = it->current_y;
11698
11699 /* Note that this isn't made use of if the face hasn't a box,
11700 so there's no need to check the face here. */
11701 it->start_of_box_run_p = 1;
11702
11703 while (it->current_x < max_x)
11704 {
11705 int x, n_glyphs_before, i, nglyphs;
11706 struct it it_before;
11707
11708 /* Get the next display element. */
11709 if (!get_next_display_element (it))
11710 {
11711 /* Don't count empty row if we are counting needed tool-bar lines. */
11712 if (height < 0 && !it->hpos)
11713 return;
11714 break;
11715 }
11716
11717 /* Produce glyphs. */
11718 n_glyphs_before = row->used[TEXT_AREA];
11719 it_before = *it;
11720
11721 PRODUCE_GLYPHS (it);
11722
11723 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11724 i = 0;
11725 x = it_before.current_x;
11726 while (i < nglyphs)
11727 {
11728 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11729
11730 if (x + glyph->pixel_width > max_x)
11731 {
11732 /* Glyph doesn't fit on line. Backtrack. */
11733 row->used[TEXT_AREA] = n_glyphs_before;
11734 *it = it_before;
11735 /* If this is the only glyph on this line, it will never fit on the
11736 tool-bar, so skip it. But ensure there is at least one glyph,
11737 so we don't accidentally disable the tool-bar. */
11738 if (n_glyphs_before == 0
11739 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11740 break;
11741 goto out;
11742 }
11743
11744 ++it->hpos;
11745 x += glyph->pixel_width;
11746 ++i;
11747 }
11748
11749 /* Stop at line end. */
11750 if (ITERATOR_AT_END_OF_LINE_P (it))
11751 break;
11752
11753 set_iterator_to_next (it, 1);
11754 }
11755
11756 out:;
11757
11758 row->displays_text_p = row->used[TEXT_AREA] != 0;
11759
11760 /* Use default face for the border below the tool bar.
11761
11762 FIXME: When auto-resize-tool-bars is grow-only, there is
11763 no additional border below the possibly empty tool-bar lines.
11764 So to make the extra empty lines look "normal", we have to
11765 use the tool-bar face for the border too. */
11766 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11767 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11768 it->face_id = DEFAULT_FACE_ID;
11769
11770 extend_face_to_end_of_line (it);
11771 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11772 last->right_box_line_p = 1;
11773 if (last == row->glyphs[TEXT_AREA])
11774 last->left_box_line_p = 1;
11775
11776 /* Make line the desired height and center it vertically. */
11777 if ((height -= it->max_ascent + it->max_descent) > 0)
11778 {
11779 /* Don't add more than one line height. */
11780 height %= FRAME_LINE_HEIGHT (it->f);
11781 it->max_ascent += height / 2;
11782 it->max_descent += (height + 1) / 2;
11783 }
11784
11785 compute_line_metrics (it);
11786
11787 /* If line is empty, make it occupy the rest of the tool-bar. */
11788 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11789 {
11790 row->height = row->phys_height = it->last_visible_y - row->y;
11791 row->visible_height = row->height;
11792 row->ascent = row->phys_ascent = 0;
11793 row->extra_line_spacing = 0;
11794 }
11795
11796 row->full_width_p = 1;
11797 row->continued_p = 0;
11798 row->truncated_on_left_p = 0;
11799 row->truncated_on_right_p = 0;
11800
11801 it->current_x = it->hpos = 0;
11802 it->current_y += row->height;
11803 ++it->vpos;
11804 ++it->glyph_row;
11805 }
11806
11807
11808 /* Max tool-bar height. */
11809
11810 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11811 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11812
11813 /* Value is the number of screen lines needed to make all tool-bar
11814 items of frame F visible. The number of actual rows needed is
11815 returned in *N_ROWS if non-NULL. */
11816
11817 static int
11818 tool_bar_lines_needed (struct frame *f, int *n_rows)
11819 {
11820 struct window *w = XWINDOW (f->tool_bar_window);
11821 struct it it;
11822 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11823 the desired matrix, so use (unused) mode-line row as temporary row to
11824 avoid destroying the first tool-bar row. */
11825 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11826
11827 /* Initialize an iterator for iteration over
11828 F->desired_tool_bar_string in the tool-bar window of frame F. */
11829 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11830 it.first_visible_x = 0;
11831 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11832 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11833 it.paragraph_embedding = L2R;
11834
11835 while (!ITERATOR_AT_END_P (&it))
11836 {
11837 clear_glyph_row (temp_row);
11838 it.glyph_row = temp_row;
11839 display_tool_bar_line (&it, -1);
11840 }
11841 clear_glyph_row (temp_row);
11842
11843 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11844 if (n_rows)
11845 *n_rows = it.vpos > 0 ? it.vpos : -1;
11846
11847 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11848 }
11849
11850
11851 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11852 0, 1, 0,
11853 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11854 If FRAME is nil or omitted, use the selected frame. */)
11855 (Lisp_Object frame)
11856 {
11857 struct frame *f = decode_any_frame (frame);
11858 struct window *w;
11859 int nlines = 0;
11860
11861 if (WINDOWP (f->tool_bar_window)
11862 && (w = XWINDOW (f->tool_bar_window),
11863 WINDOW_TOTAL_LINES (w) > 0))
11864 {
11865 update_tool_bar (f, 1);
11866 if (f->n_tool_bar_items)
11867 {
11868 build_desired_tool_bar_string (f);
11869 nlines = tool_bar_lines_needed (f, NULL);
11870 }
11871 }
11872
11873 return make_number (nlines);
11874 }
11875
11876
11877 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11878 height should be changed. */
11879
11880 static int
11881 redisplay_tool_bar (struct frame *f)
11882 {
11883 struct window *w;
11884 struct it it;
11885 struct glyph_row *row;
11886
11887 #if defined (USE_GTK) || defined (HAVE_NS)
11888 if (FRAME_EXTERNAL_TOOL_BAR (f))
11889 update_frame_tool_bar (f);
11890 return 0;
11891 #endif
11892
11893 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11894 do anything. This means you must start with tool-bar-lines
11895 non-zero to get the auto-sizing effect. Or in other words, you
11896 can turn off tool-bars by specifying tool-bar-lines zero. */
11897 if (!WINDOWP (f->tool_bar_window)
11898 || (w = XWINDOW (f->tool_bar_window),
11899 WINDOW_TOTAL_LINES (w) == 0))
11900 return 0;
11901
11902 /* Set up an iterator for the tool-bar window. */
11903 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11904 it.first_visible_x = 0;
11905 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11906 row = it.glyph_row;
11907
11908 /* Build a string that represents the contents of the tool-bar. */
11909 build_desired_tool_bar_string (f);
11910 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11911 /* FIXME: This should be controlled by a user option. But it
11912 doesn't make sense to have an R2L tool bar if the menu bar cannot
11913 be drawn also R2L, and making the menu bar R2L is tricky due
11914 toolkit-specific code that implements it. If an R2L tool bar is
11915 ever supported, display_tool_bar_line should also be augmented to
11916 call unproduce_glyphs like display_line and display_string
11917 do. */
11918 it.paragraph_embedding = L2R;
11919
11920 if (f->n_tool_bar_rows == 0)
11921 {
11922 int nlines;
11923
11924 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11925 nlines != WINDOW_TOTAL_LINES (w)))
11926 {
11927 Lisp_Object frame;
11928 int old_height = WINDOW_TOTAL_LINES (w);
11929
11930 XSETFRAME (frame, f);
11931 Fmodify_frame_parameters (frame,
11932 Fcons (Fcons (Qtool_bar_lines,
11933 make_number (nlines)),
11934 Qnil));
11935 if (WINDOW_TOTAL_LINES (w) != old_height)
11936 {
11937 clear_glyph_matrix (w->desired_matrix);
11938 fonts_changed_p = 1;
11939 return 1;
11940 }
11941 }
11942 }
11943
11944 /* Display as many lines as needed to display all tool-bar items. */
11945
11946 if (f->n_tool_bar_rows > 0)
11947 {
11948 int border, rows, height, extra;
11949
11950 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11951 border = XINT (Vtool_bar_border);
11952 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11953 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11954 else if (EQ (Vtool_bar_border, Qborder_width))
11955 border = f->border_width;
11956 else
11957 border = 0;
11958 if (border < 0)
11959 border = 0;
11960
11961 rows = f->n_tool_bar_rows;
11962 height = max (1, (it.last_visible_y - border) / rows);
11963 extra = it.last_visible_y - border - height * rows;
11964
11965 while (it.current_y < it.last_visible_y)
11966 {
11967 int h = 0;
11968 if (extra > 0 && rows-- > 0)
11969 {
11970 h = (extra + rows - 1) / rows;
11971 extra -= h;
11972 }
11973 display_tool_bar_line (&it, height + h);
11974 }
11975 }
11976 else
11977 {
11978 while (it.current_y < it.last_visible_y)
11979 display_tool_bar_line (&it, 0);
11980 }
11981
11982 /* It doesn't make much sense to try scrolling in the tool-bar
11983 window, so don't do it. */
11984 w->desired_matrix->no_scrolling_p = 1;
11985 w->must_be_updated_p = 1;
11986
11987 if (!NILP (Vauto_resize_tool_bars))
11988 {
11989 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11990 int change_height_p = 0;
11991
11992 /* If we couldn't display everything, change the tool-bar's
11993 height if there is room for more. */
11994 if (IT_STRING_CHARPOS (it) < it.end_charpos
11995 && it.current_y < max_tool_bar_height)
11996 change_height_p = 1;
11997
11998 row = it.glyph_row - 1;
11999
12000 /* If there are blank lines at the end, except for a partially
12001 visible blank line at the end that is smaller than
12002 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12003 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12004 && row->height >= FRAME_LINE_HEIGHT (f))
12005 change_height_p = 1;
12006
12007 /* If row displays tool-bar items, but is partially visible,
12008 change the tool-bar's height. */
12009 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12010 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12011 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12012 change_height_p = 1;
12013
12014 /* Resize windows as needed by changing the `tool-bar-lines'
12015 frame parameter. */
12016 if (change_height_p)
12017 {
12018 Lisp_Object frame;
12019 int old_height = WINDOW_TOTAL_LINES (w);
12020 int nrows;
12021 int nlines = tool_bar_lines_needed (f, &nrows);
12022
12023 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12024 && !f->minimize_tool_bar_window_p)
12025 ? (nlines > old_height)
12026 : (nlines != old_height));
12027 f->minimize_tool_bar_window_p = 0;
12028
12029 if (change_height_p)
12030 {
12031 XSETFRAME (frame, f);
12032 Fmodify_frame_parameters (frame,
12033 Fcons (Fcons (Qtool_bar_lines,
12034 make_number (nlines)),
12035 Qnil));
12036 if (WINDOW_TOTAL_LINES (w) != old_height)
12037 {
12038 clear_glyph_matrix (w->desired_matrix);
12039 f->n_tool_bar_rows = nrows;
12040 fonts_changed_p = 1;
12041 return 1;
12042 }
12043 }
12044 }
12045 }
12046
12047 f->minimize_tool_bar_window_p = 0;
12048 return 0;
12049 }
12050
12051
12052 /* Get information about the tool-bar item which is displayed in GLYPH
12053 on frame F. Return in *PROP_IDX the index where tool-bar item
12054 properties start in F->tool_bar_items. Value is zero if
12055 GLYPH doesn't display a tool-bar item. */
12056
12057 static int
12058 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12059 {
12060 Lisp_Object prop;
12061 int success_p;
12062 int charpos;
12063
12064 /* This function can be called asynchronously, which means we must
12065 exclude any possibility that Fget_text_property signals an
12066 error. */
12067 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12068 charpos = max (0, charpos);
12069
12070 /* Get the text property `menu-item' at pos. The value of that
12071 property is the start index of this item's properties in
12072 F->tool_bar_items. */
12073 prop = Fget_text_property (make_number (charpos),
12074 Qmenu_item, f->current_tool_bar_string);
12075 if (INTEGERP (prop))
12076 {
12077 *prop_idx = XINT (prop);
12078 success_p = 1;
12079 }
12080 else
12081 success_p = 0;
12082
12083 return success_p;
12084 }
12085
12086 \f
12087 /* Get information about the tool-bar item at position X/Y on frame F.
12088 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12089 the current matrix of the tool-bar window of F, or NULL if not
12090 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12091 item in F->tool_bar_items. Value is
12092
12093 -1 if X/Y is not on a tool-bar item
12094 0 if X/Y is on the same item that was highlighted before.
12095 1 otherwise. */
12096
12097 static int
12098 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12099 int *hpos, int *vpos, int *prop_idx)
12100 {
12101 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12102 struct window *w = XWINDOW (f->tool_bar_window);
12103 int area;
12104
12105 /* Find the glyph under X/Y. */
12106 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12107 if (*glyph == NULL)
12108 return -1;
12109
12110 /* Get the start of this tool-bar item's properties in
12111 f->tool_bar_items. */
12112 if (!tool_bar_item_info (f, *glyph, prop_idx))
12113 return -1;
12114
12115 /* Is mouse on the highlighted item? */
12116 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12117 && *vpos >= hlinfo->mouse_face_beg_row
12118 && *vpos <= hlinfo->mouse_face_end_row
12119 && (*vpos > hlinfo->mouse_face_beg_row
12120 || *hpos >= hlinfo->mouse_face_beg_col)
12121 && (*vpos < hlinfo->mouse_face_end_row
12122 || *hpos < hlinfo->mouse_face_end_col
12123 || hlinfo->mouse_face_past_end))
12124 return 0;
12125
12126 return 1;
12127 }
12128
12129
12130 /* EXPORT:
12131 Handle mouse button event on the tool-bar of frame F, at
12132 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12133 0 for button release. MODIFIERS is event modifiers for button
12134 release. */
12135
12136 void
12137 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12138 int modifiers)
12139 {
12140 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12141 struct window *w = XWINDOW (f->tool_bar_window);
12142 int hpos, vpos, prop_idx;
12143 struct glyph *glyph;
12144 Lisp_Object enabled_p;
12145
12146 /* If not on the highlighted tool-bar item, return. */
12147 frame_to_window_pixel_xy (w, &x, &y);
12148 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12149 return;
12150
12151 /* If item is disabled, do nothing. */
12152 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12153 if (NILP (enabled_p))
12154 return;
12155
12156 if (down_p)
12157 {
12158 /* Show item in pressed state. */
12159 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12160 last_tool_bar_item = prop_idx;
12161 }
12162 else
12163 {
12164 Lisp_Object key, frame;
12165 struct input_event event;
12166 EVENT_INIT (event);
12167
12168 /* Show item in released state. */
12169 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12170
12171 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12172
12173 XSETFRAME (frame, f);
12174 event.kind = TOOL_BAR_EVENT;
12175 event.frame_or_window = frame;
12176 event.arg = frame;
12177 kbd_buffer_store_event (&event);
12178
12179 event.kind = TOOL_BAR_EVENT;
12180 event.frame_or_window = frame;
12181 event.arg = key;
12182 event.modifiers = modifiers;
12183 kbd_buffer_store_event (&event);
12184 last_tool_bar_item = -1;
12185 }
12186 }
12187
12188
12189 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12190 tool-bar window-relative coordinates X/Y. Called from
12191 note_mouse_highlight. */
12192
12193 static void
12194 note_tool_bar_highlight (struct frame *f, int x, int y)
12195 {
12196 Lisp_Object window = f->tool_bar_window;
12197 struct window *w = XWINDOW (window);
12198 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12199 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12200 int hpos, vpos;
12201 struct glyph *glyph;
12202 struct glyph_row *row;
12203 int i;
12204 Lisp_Object enabled_p;
12205 int prop_idx;
12206 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12207 int mouse_down_p, rc;
12208
12209 /* Function note_mouse_highlight is called with negative X/Y
12210 values when mouse moves outside of the frame. */
12211 if (x <= 0 || y <= 0)
12212 {
12213 clear_mouse_face (hlinfo);
12214 return;
12215 }
12216
12217 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12218 if (rc < 0)
12219 {
12220 /* Not on tool-bar item. */
12221 clear_mouse_face (hlinfo);
12222 return;
12223 }
12224 else if (rc == 0)
12225 /* On same tool-bar item as before. */
12226 goto set_help_echo;
12227
12228 clear_mouse_face (hlinfo);
12229
12230 /* Mouse is down, but on different tool-bar item? */
12231 mouse_down_p = (dpyinfo->grabbed
12232 && f == last_mouse_frame
12233 && FRAME_LIVE_P (f));
12234 if (mouse_down_p
12235 && last_tool_bar_item != prop_idx)
12236 return;
12237
12238 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12239
12240 /* If tool-bar item is not enabled, don't highlight it. */
12241 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12242 if (!NILP (enabled_p))
12243 {
12244 /* Compute the x-position of the glyph. In front and past the
12245 image is a space. We include this in the highlighted area. */
12246 row = MATRIX_ROW (w->current_matrix, vpos);
12247 for (i = x = 0; i < hpos; ++i)
12248 x += row->glyphs[TEXT_AREA][i].pixel_width;
12249
12250 /* Record this as the current active region. */
12251 hlinfo->mouse_face_beg_col = hpos;
12252 hlinfo->mouse_face_beg_row = vpos;
12253 hlinfo->mouse_face_beg_x = x;
12254 hlinfo->mouse_face_beg_y = row->y;
12255 hlinfo->mouse_face_past_end = 0;
12256
12257 hlinfo->mouse_face_end_col = hpos + 1;
12258 hlinfo->mouse_face_end_row = vpos;
12259 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12260 hlinfo->mouse_face_end_y = row->y;
12261 hlinfo->mouse_face_window = window;
12262 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12263
12264 /* Display it as active. */
12265 show_mouse_face (hlinfo, draw);
12266 }
12267
12268 set_help_echo:
12269
12270 /* Set help_echo_string to a help string to display for this tool-bar item.
12271 XTread_socket does the rest. */
12272 help_echo_object = help_echo_window = Qnil;
12273 help_echo_pos = -1;
12274 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12275 if (NILP (help_echo_string))
12276 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12277 }
12278
12279 #endif /* HAVE_WINDOW_SYSTEM */
12280
12281
12282 \f
12283 /************************************************************************
12284 Horizontal scrolling
12285 ************************************************************************/
12286
12287 static int hscroll_window_tree (Lisp_Object);
12288 static int hscroll_windows (Lisp_Object);
12289
12290 /* For all leaf windows in the window tree rooted at WINDOW, set their
12291 hscroll value so that PT is (i) visible in the window, and (ii) so
12292 that it is not within a certain margin at the window's left and
12293 right border. Value is non-zero if any window's hscroll has been
12294 changed. */
12295
12296 static int
12297 hscroll_window_tree (Lisp_Object window)
12298 {
12299 int hscrolled_p = 0;
12300 int hscroll_relative_p = FLOATP (Vhscroll_step);
12301 int hscroll_step_abs = 0;
12302 double hscroll_step_rel = 0;
12303
12304 if (hscroll_relative_p)
12305 {
12306 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12307 if (hscroll_step_rel < 0)
12308 {
12309 hscroll_relative_p = 0;
12310 hscroll_step_abs = 0;
12311 }
12312 }
12313 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12314 {
12315 hscroll_step_abs = XINT (Vhscroll_step);
12316 if (hscroll_step_abs < 0)
12317 hscroll_step_abs = 0;
12318 }
12319 else
12320 hscroll_step_abs = 0;
12321
12322 while (WINDOWP (window))
12323 {
12324 struct window *w = XWINDOW (window);
12325
12326 if (WINDOWP (w->contents))
12327 hscrolled_p |= hscroll_window_tree (w->contents);
12328 else if (w->cursor.vpos >= 0)
12329 {
12330 int h_margin;
12331 int text_area_width;
12332 struct glyph_row *current_cursor_row
12333 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12334 struct glyph_row *desired_cursor_row
12335 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12336 struct glyph_row *cursor_row
12337 = (desired_cursor_row->enabled_p
12338 ? desired_cursor_row
12339 : current_cursor_row);
12340 int row_r2l_p = cursor_row->reversed_p;
12341
12342 text_area_width = window_box_width (w, TEXT_AREA);
12343
12344 /* Scroll when cursor is inside this scroll margin. */
12345 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12346
12347 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12348 /* For left-to-right rows, hscroll when cursor is either
12349 (i) inside the right hscroll margin, or (ii) if it is
12350 inside the left margin and the window is already
12351 hscrolled. */
12352 && ((!row_r2l_p
12353 && ((w->hscroll
12354 && w->cursor.x <= h_margin)
12355 || (cursor_row->enabled_p
12356 && cursor_row->truncated_on_right_p
12357 && (w->cursor.x >= text_area_width - h_margin))))
12358 /* For right-to-left rows, the logic is similar,
12359 except that rules for scrolling to left and right
12360 are reversed. E.g., if cursor.x <= h_margin, we
12361 need to hscroll "to the right" unconditionally,
12362 and that will scroll the screen to the left so as
12363 to reveal the next portion of the row. */
12364 || (row_r2l_p
12365 && ((cursor_row->enabled_p
12366 /* FIXME: It is confusing to set the
12367 truncated_on_right_p flag when R2L rows
12368 are actually truncated on the left. */
12369 && cursor_row->truncated_on_right_p
12370 && w->cursor.x <= h_margin)
12371 || (w->hscroll
12372 && (w->cursor.x >= text_area_width - h_margin))))))
12373 {
12374 struct it it;
12375 ptrdiff_t hscroll;
12376 struct buffer *saved_current_buffer;
12377 ptrdiff_t pt;
12378 int wanted_x;
12379
12380 /* Find point in a display of infinite width. */
12381 saved_current_buffer = current_buffer;
12382 current_buffer = XBUFFER (w->contents);
12383
12384 if (w == XWINDOW (selected_window))
12385 pt = PT;
12386 else
12387 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12388
12389 /* Move iterator to pt starting at cursor_row->start in
12390 a line with infinite width. */
12391 init_to_row_start (&it, w, cursor_row);
12392 it.last_visible_x = INFINITY;
12393 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12394 current_buffer = saved_current_buffer;
12395
12396 /* Position cursor in window. */
12397 if (!hscroll_relative_p && hscroll_step_abs == 0)
12398 hscroll = max (0, (it.current_x
12399 - (ITERATOR_AT_END_OF_LINE_P (&it)
12400 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12401 : (text_area_width / 2))))
12402 / FRAME_COLUMN_WIDTH (it.f);
12403 else if ((!row_r2l_p
12404 && w->cursor.x >= text_area_width - h_margin)
12405 || (row_r2l_p && w->cursor.x <= h_margin))
12406 {
12407 if (hscroll_relative_p)
12408 wanted_x = text_area_width * (1 - hscroll_step_rel)
12409 - h_margin;
12410 else
12411 wanted_x = text_area_width
12412 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12413 - h_margin;
12414 hscroll
12415 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12416 }
12417 else
12418 {
12419 if (hscroll_relative_p)
12420 wanted_x = text_area_width * hscroll_step_rel
12421 + h_margin;
12422 else
12423 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12424 + h_margin;
12425 hscroll
12426 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12427 }
12428 hscroll = max (hscroll, w->min_hscroll);
12429
12430 /* Don't prevent redisplay optimizations if hscroll
12431 hasn't changed, as it will unnecessarily slow down
12432 redisplay. */
12433 if (w->hscroll != hscroll)
12434 {
12435 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12436 w->hscroll = hscroll;
12437 hscrolled_p = 1;
12438 }
12439 }
12440 }
12441
12442 window = w->next;
12443 }
12444
12445 /* Value is non-zero if hscroll of any leaf window has been changed. */
12446 return hscrolled_p;
12447 }
12448
12449
12450 /* Set hscroll so that cursor is visible and not inside horizontal
12451 scroll margins for all windows in the tree rooted at WINDOW. See
12452 also hscroll_window_tree above. Value is non-zero if any window's
12453 hscroll has been changed. If it has, desired matrices on the frame
12454 of WINDOW are cleared. */
12455
12456 static int
12457 hscroll_windows (Lisp_Object window)
12458 {
12459 int hscrolled_p = hscroll_window_tree (window);
12460 if (hscrolled_p)
12461 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12462 return hscrolled_p;
12463 }
12464
12465
12466 \f
12467 /************************************************************************
12468 Redisplay
12469 ************************************************************************/
12470
12471 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12472 to a non-zero value. This is sometimes handy to have in a debugger
12473 session. */
12474
12475 #ifdef GLYPH_DEBUG
12476
12477 /* First and last unchanged row for try_window_id. */
12478
12479 static int debug_first_unchanged_at_end_vpos;
12480 static int debug_last_unchanged_at_beg_vpos;
12481
12482 /* Delta vpos and y. */
12483
12484 static int debug_dvpos, debug_dy;
12485
12486 /* Delta in characters and bytes for try_window_id. */
12487
12488 static ptrdiff_t debug_delta, debug_delta_bytes;
12489
12490 /* Values of window_end_pos and window_end_vpos at the end of
12491 try_window_id. */
12492
12493 static ptrdiff_t debug_end_vpos;
12494
12495 /* Append a string to W->desired_matrix->method. FMT is a printf
12496 format string. If trace_redisplay_p is non-zero also printf the
12497 resulting string to stderr. */
12498
12499 static void debug_method_add (struct window *, char const *, ...)
12500 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12501
12502 static void
12503 debug_method_add (struct window *w, char const *fmt, ...)
12504 {
12505 char *method = w->desired_matrix->method;
12506 int len = strlen (method);
12507 int size = sizeof w->desired_matrix->method;
12508 int remaining = size - len - 1;
12509 va_list ap;
12510
12511 if (len && remaining)
12512 {
12513 method[len] = '|';
12514 --remaining, ++len;
12515 }
12516
12517 va_start (ap, fmt);
12518 vsnprintf (method + len, remaining + 1, fmt, ap);
12519 va_end (ap);
12520
12521 if (trace_redisplay_p)
12522 fprintf (stderr, "%p (%s): %s\n",
12523 w,
12524 ((BUFFERP (w->contents)
12525 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12526 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12527 : "no buffer"),
12528 method + len);
12529 }
12530
12531 #endif /* GLYPH_DEBUG */
12532
12533
12534 /* Value is non-zero if all changes in window W, which displays
12535 current_buffer, are in the text between START and END. START is a
12536 buffer position, END is given as a distance from Z. Used in
12537 redisplay_internal for display optimization. */
12538
12539 static int
12540 text_outside_line_unchanged_p (struct window *w,
12541 ptrdiff_t start, ptrdiff_t end)
12542 {
12543 int unchanged_p = 1;
12544
12545 /* If text or overlays have changed, see where. */
12546 if (window_outdated (w))
12547 {
12548 /* Gap in the line? */
12549 if (GPT < start || Z - GPT < end)
12550 unchanged_p = 0;
12551
12552 /* Changes start in front of the line, or end after it? */
12553 if (unchanged_p
12554 && (BEG_UNCHANGED < start - 1
12555 || END_UNCHANGED < end))
12556 unchanged_p = 0;
12557
12558 /* If selective display, can't optimize if changes start at the
12559 beginning of the line. */
12560 if (unchanged_p
12561 && INTEGERP (BVAR (current_buffer, selective_display))
12562 && XINT (BVAR (current_buffer, selective_display)) > 0
12563 && (BEG_UNCHANGED < start || GPT <= start))
12564 unchanged_p = 0;
12565
12566 /* If there are overlays at the start or end of the line, these
12567 may have overlay strings with newlines in them. A change at
12568 START, for instance, may actually concern the display of such
12569 overlay strings as well, and they are displayed on different
12570 lines. So, quickly rule out this case. (For the future, it
12571 might be desirable to implement something more telling than
12572 just BEG/END_UNCHANGED.) */
12573 if (unchanged_p)
12574 {
12575 if (BEG + BEG_UNCHANGED == start
12576 && overlay_touches_p (start))
12577 unchanged_p = 0;
12578 if (END_UNCHANGED == end
12579 && overlay_touches_p (Z - end))
12580 unchanged_p = 0;
12581 }
12582
12583 /* Under bidi reordering, adding or deleting a character in the
12584 beginning of a paragraph, before the first strong directional
12585 character, can change the base direction of the paragraph (unless
12586 the buffer specifies a fixed paragraph direction), which will
12587 require to redisplay the whole paragraph. It might be worthwhile
12588 to find the paragraph limits and widen the range of redisplayed
12589 lines to that, but for now just give up this optimization. */
12590 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12591 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12592 unchanged_p = 0;
12593 }
12594
12595 return unchanged_p;
12596 }
12597
12598
12599 /* Do a frame update, taking possible shortcuts into account. This is
12600 the main external entry point for redisplay.
12601
12602 If the last redisplay displayed an echo area message and that message
12603 is no longer requested, we clear the echo area or bring back the
12604 mini-buffer if that is in use. */
12605
12606 void
12607 redisplay (void)
12608 {
12609 redisplay_internal ();
12610 }
12611
12612
12613 static Lisp_Object
12614 overlay_arrow_string_or_property (Lisp_Object var)
12615 {
12616 Lisp_Object val;
12617
12618 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12619 return val;
12620
12621 return Voverlay_arrow_string;
12622 }
12623
12624 /* Return 1 if there are any overlay-arrows in current_buffer. */
12625 static int
12626 overlay_arrow_in_current_buffer_p (void)
12627 {
12628 Lisp_Object vlist;
12629
12630 for (vlist = Voverlay_arrow_variable_list;
12631 CONSP (vlist);
12632 vlist = XCDR (vlist))
12633 {
12634 Lisp_Object var = XCAR (vlist);
12635 Lisp_Object val;
12636
12637 if (!SYMBOLP (var))
12638 continue;
12639 val = find_symbol_value (var);
12640 if (MARKERP (val)
12641 && current_buffer == XMARKER (val)->buffer)
12642 return 1;
12643 }
12644 return 0;
12645 }
12646
12647
12648 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12649 has changed. */
12650
12651 static int
12652 overlay_arrows_changed_p (void)
12653 {
12654 Lisp_Object vlist;
12655
12656 for (vlist = Voverlay_arrow_variable_list;
12657 CONSP (vlist);
12658 vlist = XCDR (vlist))
12659 {
12660 Lisp_Object var = XCAR (vlist);
12661 Lisp_Object val, pstr;
12662
12663 if (!SYMBOLP (var))
12664 continue;
12665 val = find_symbol_value (var);
12666 if (!MARKERP (val))
12667 continue;
12668 if (! EQ (COERCE_MARKER (val),
12669 Fget (var, Qlast_arrow_position))
12670 || ! (pstr = overlay_arrow_string_or_property (var),
12671 EQ (pstr, Fget (var, Qlast_arrow_string))))
12672 return 1;
12673 }
12674 return 0;
12675 }
12676
12677 /* Mark overlay arrows to be updated on next redisplay. */
12678
12679 static void
12680 update_overlay_arrows (int up_to_date)
12681 {
12682 Lisp_Object vlist;
12683
12684 for (vlist = Voverlay_arrow_variable_list;
12685 CONSP (vlist);
12686 vlist = XCDR (vlist))
12687 {
12688 Lisp_Object var = XCAR (vlist);
12689
12690 if (!SYMBOLP (var))
12691 continue;
12692
12693 if (up_to_date > 0)
12694 {
12695 Lisp_Object val = find_symbol_value (var);
12696 Fput (var, Qlast_arrow_position,
12697 COERCE_MARKER (val));
12698 Fput (var, Qlast_arrow_string,
12699 overlay_arrow_string_or_property (var));
12700 }
12701 else if (up_to_date < 0
12702 || !NILP (Fget (var, Qlast_arrow_position)))
12703 {
12704 Fput (var, Qlast_arrow_position, Qt);
12705 Fput (var, Qlast_arrow_string, Qt);
12706 }
12707 }
12708 }
12709
12710
12711 /* Return overlay arrow string to display at row.
12712 Return integer (bitmap number) for arrow bitmap in left fringe.
12713 Return nil if no overlay arrow. */
12714
12715 static Lisp_Object
12716 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12717 {
12718 Lisp_Object vlist;
12719
12720 for (vlist = Voverlay_arrow_variable_list;
12721 CONSP (vlist);
12722 vlist = XCDR (vlist))
12723 {
12724 Lisp_Object var = XCAR (vlist);
12725 Lisp_Object val;
12726
12727 if (!SYMBOLP (var))
12728 continue;
12729
12730 val = find_symbol_value (var);
12731
12732 if (MARKERP (val)
12733 && current_buffer == XMARKER (val)->buffer
12734 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12735 {
12736 if (FRAME_WINDOW_P (it->f)
12737 /* FIXME: if ROW->reversed_p is set, this should test
12738 the right fringe, not the left one. */
12739 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12740 {
12741 #ifdef HAVE_WINDOW_SYSTEM
12742 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12743 {
12744 int fringe_bitmap;
12745 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12746 return make_number (fringe_bitmap);
12747 }
12748 #endif
12749 return make_number (-1); /* Use default arrow bitmap. */
12750 }
12751 return overlay_arrow_string_or_property (var);
12752 }
12753 }
12754
12755 return Qnil;
12756 }
12757
12758 /* Return 1 if point moved out of or into a composition. Otherwise
12759 return 0. PREV_BUF and PREV_PT are the last point buffer and
12760 position. BUF and PT are the current point buffer and position. */
12761
12762 static int
12763 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12764 struct buffer *buf, ptrdiff_t pt)
12765 {
12766 ptrdiff_t start, end;
12767 Lisp_Object prop;
12768 Lisp_Object buffer;
12769
12770 XSETBUFFER (buffer, buf);
12771 /* Check a composition at the last point if point moved within the
12772 same buffer. */
12773 if (prev_buf == buf)
12774 {
12775 if (prev_pt == pt)
12776 /* Point didn't move. */
12777 return 0;
12778
12779 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12780 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12781 && COMPOSITION_VALID_P (start, end, prop)
12782 && start < prev_pt && end > prev_pt)
12783 /* The last point was within the composition. Return 1 iff
12784 point moved out of the composition. */
12785 return (pt <= start || pt >= end);
12786 }
12787
12788 /* Check a composition at the current point. */
12789 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12790 && find_composition (pt, -1, &start, &end, &prop, buffer)
12791 && COMPOSITION_VALID_P (start, end, prop)
12792 && start < pt && end > pt);
12793 }
12794
12795
12796 /* Reconsider the setting of B->clip_changed which is displayed
12797 in window W. */
12798
12799 static void
12800 reconsider_clip_changes (struct window *w, struct buffer *b)
12801 {
12802 if (b->clip_changed
12803 && w->window_end_valid
12804 && w->current_matrix->buffer == b
12805 && w->current_matrix->zv == BUF_ZV (b)
12806 && w->current_matrix->begv == BUF_BEGV (b))
12807 b->clip_changed = 0;
12808
12809 /* If display wasn't paused, and W is not a tool bar window, see if
12810 point has been moved into or out of a composition. In that case,
12811 we set b->clip_changed to 1 to force updating the screen. If
12812 b->clip_changed has already been set to 1, we can skip this
12813 check. */
12814 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12815 {
12816 ptrdiff_t pt;
12817
12818 if (w == XWINDOW (selected_window))
12819 pt = PT;
12820 else
12821 pt = marker_position (w->pointm);
12822
12823 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12824 || pt != w->last_point)
12825 && check_point_in_composition (w->current_matrix->buffer,
12826 w->last_point,
12827 XBUFFER (w->contents), pt))
12828 b->clip_changed = 1;
12829 }
12830 }
12831 \f
12832
12833 #define STOP_POLLING \
12834 do { if (! polling_stopped_here) stop_polling (); \
12835 polling_stopped_here = 1; } while (0)
12836
12837 #define RESUME_POLLING \
12838 do { if (polling_stopped_here) start_polling (); \
12839 polling_stopped_here = 0; } while (0)
12840
12841
12842 /* Perhaps in the future avoid recentering windows if it
12843 is not necessary; currently that causes some problems. */
12844
12845 static void
12846 redisplay_internal (void)
12847 {
12848 struct window *w = XWINDOW (selected_window);
12849 struct window *sw;
12850 struct frame *fr;
12851 int pending;
12852 int must_finish = 0;
12853 struct text_pos tlbufpos, tlendpos;
12854 int number_of_visible_frames;
12855 ptrdiff_t count, count1;
12856 struct frame *sf;
12857 int polling_stopped_here = 0;
12858 Lisp_Object tail, frame;
12859 struct backtrace backtrace;
12860
12861 /* Non-zero means redisplay has to consider all windows on all
12862 frames. Zero means, only selected_window is considered. */
12863 int consider_all_windows_p;
12864
12865 /* Non-zero means redisplay has to redisplay the miniwindow. */
12866 int update_miniwindow_p = 0;
12867
12868 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12869
12870 /* No redisplay if running in batch mode or frame is not yet fully
12871 initialized, or redisplay is explicitly turned off by setting
12872 Vinhibit_redisplay. */
12873 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12874 || !NILP (Vinhibit_redisplay))
12875 return;
12876
12877 /* Don't examine these until after testing Vinhibit_redisplay.
12878 When Emacs is shutting down, perhaps because its connection to
12879 X has dropped, we should not look at them at all. */
12880 fr = XFRAME (w->frame);
12881 sf = SELECTED_FRAME ();
12882
12883 if (!fr->glyphs_initialized_p)
12884 return;
12885
12886 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12887 if (popup_activated ())
12888 return;
12889 #endif
12890
12891 /* I don't think this happens but let's be paranoid. */
12892 if (redisplaying_p)
12893 return;
12894
12895 /* Record a function that clears redisplaying_p
12896 when we leave this function. */
12897 count = SPECPDL_INDEX ();
12898 record_unwind_protect (unwind_redisplay, selected_frame);
12899 redisplaying_p = 1;
12900 specbind (Qinhibit_free_realized_faces, Qnil);
12901
12902 /* Record this function, so it appears on the profiler's backtraces. */
12903 backtrace.next = backtrace_list;
12904 backtrace.function = Qredisplay_internal;
12905 backtrace.args = &Qnil;
12906 backtrace.nargs = 0;
12907 backtrace.debug_on_exit = 0;
12908 backtrace_list = &backtrace;
12909
12910 FOR_EACH_FRAME (tail, frame)
12911 XFRAME (frame)->already_hscrolled_p = 0;
12912
12913 retry:
12914 /* Remember the currently selected window. */
12915 sw = w;
12916
12917 pending = 0;
12918 reconsider_clip_changes (w, current_buffer);
12919 last_escape_glyph_frame = NULL;
12920 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12921 last_glyphless_glyph_frame = NULL;
12922 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12923
12924 /* If new fonts have been loaded that make a glyph matrix adjustment
12925 necessary, do it. */
12926 if (fonts_changed_p)
12927 {
12928 adjust_glyphs (NULL);
12929 ++windows_or_buffers_changed;
12930 fonts_changed_p = 0;
12931 }
12932
12933 /* If face_change_count is non-zero, init_iterator will free all
12934 realized faces, which includes the faces referenced from current
12935 matrices. So, we can't reuse current matrices in this case. */
12936 if (face_change_count)
12937 ++windows_or_buffers_changed;
12938
12939 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12940 && FRAME_TTY (sf)->previous_frame != sf)
12941 {
12942 /* Since frames on a single ASCII terminal share the same
12943 display area, displaying a different frame means redisplay
12944 the whole thing. */
12945 windows_or_buffers_changed++;
12946 SET_FRAME_GARBAGED (sf);
12947 #ifndef DOS_NT
12948 set_tty_color_mode (FRAME_TTY (sf), sf);
12949 #endif
12950 FRAME_TTY (sf)->previous_frame = sf;
12951 }
12952
12953 /* Set the visible flags for all frames. Do this before checking for
12954 resized or garbaged frames; they want to know if their frames are
12955 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12956 number_of_visible_frames = 0;
12957
12958 FOR_EACH_FRAME (tail, frame)
12959 {
12960 struct frame *f = XFRAME (frame);
12961
12962 if (FRAME_VISIBLE_P (f))
12963 ++number_of_visible_frames;
12964 clear_desired_matrices (f);
12965 }
12966
12967 /* Notice any pending interrupt request to change frame size. */
12968 do_pending_window_change (1);
12969
12970 /* do_pending_window_change could change the selected_window due to
12971 frame resizing which makes the selected window too small. */
12972 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12973 {
12974 sw = w;
12975 reconsider_clip_changes (w, current_buffer);
12976 }
12977
12978 /* Clear frames marked as garbaged. */
12979 clear_garbaged_frames ();
12980
12981 /* Build menubar and tool-bar items. */
12982 if (NILP (Vmemory_full))
12983 prepare_menu_bars ();
12984
12985 if (windows_or_buffers_changed)
12986 update_mode_lines++;
12987
12988 /* Detect case that we need to write or remove a star in the mode line. */
12989 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12990 {
12991 w->update_mode_line = 1;
12992 if (buffer_shared_and_changed ())
12993 update_mode_lines++;
12994 }
12995
12996 /* Avoid invocation of point motion hooks by `current_column' below. */
12997 count1 = SPECPDL_INDEX ();
12998 specbind (Qinhibit_point_motion_hooks, Qt);
12999
13000 if (mode_line_update_needed (w))
13001 w->update_mode_line = 1;
13002
13003 unbind_to (count1, Qnil);
13004
13005 consider_all_windows_p = (update_mode_lines
13006 || buffer_shared_and_changed ()
13007 || cursor_type_changed);
13008
13009 /* If specs for an arrow have changed, do thorough redisplay
13010 to ensure we remove any arrow that should no longer exist. */
13011 if (overlay_arrows_changed_p ())
13012 consider_all_windows_p = windows_or_buffers_changed = 1;
13013
13014 /* Normally the message* functions will have already displayed and
13015 updated the echo area, but the frame may have been trashed, or
13016 the update may have been preempted, so display the echo area
13017 again here. Checking message_cleared_p captures the case that
13018 the echo area should be cleared. */
13019 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13020 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13021 || (message_cleared_p
13022 && minibuf_level == 0
13023 /* If the mini-window is currently selected, this means the
13024 echo-area doesn't show through. */
13025 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13026 {
13027 int window_height_changed_p = echo_area_display (0);
13028
13029 if (message_cleared_p)
13030 update_miniwindow_p = 1;
13031
13032 must_finish = 1;
13033
13034 /* If we don't display the current message, don't clear the
13035 message_cleared_p flag, because, if we did, we wouldn't clear
13036 the echo area in the next redisplay which doesn't preserve
13037 the echo area. */
13038 if (!display_last_displayed_message_p)
13039 message_cleared_p = 0;
13040
13041 if (fonts_changed_p)
13042 goto retry;
13043 else if (window_height_changed_p)
13044 {
13045 consider_all_windows_p = 1;
13046 ++update_mode_lines;
13047 ++windows_or_buffers_changed;
13048
13049 /* If window configuration was changed, frames may have been
13050 marked garbaged. Clear them or we will experience
13051 surprises wrt scrolling. */
13052 clear_garbaged_frames ();
13053 }
13054 }
13055 else if (EQ (selected_window, minibuf_window)
13056 && (current_buffer->clip_changed || window_outdated (w))
13057 && resize_mini_window (w, 0))
13058 {
13059 /* Resized active mini-window to fit the size of what it is
13060 showing if its contents might have changed. */
13061 must_finish = 1;
13062 /* FIXME: this causes all frames to be updated, which seems unnecessary
13063 since only the current frame needs to be considered. This function
13064 needs to be rewritten with two variables, consider_all_windows and
13065 consider_all_frames. */
13066 consider_all_windows_p = 1;
13067 ++windows_or_buffers_changed;
13068 ++update_mode_lines;
13069
13070 /* If window configuration was changed, frames may have been
13071 marked garbaged. Clear them or we will experience
13072 surprises wrt scrolling. */
13073 clear_garbaged_frames ();
13074 }
13075
13076 /* If showing the region, and mark has changed, we must redisplay
13077 the whole window. The assignment to this_line_start_pos prevents
13078 the optimization directly below this if-statement. */
13079 if (((!NILP (Vtransient_mark_mode)
13080 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13081 != (w->region_showing > 0))
13082 || (w->region_showing
13083 && w->region_showing
13084 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13085 CHARPOS (this_line_start_pos) = 0;
13086
13087 /* Optimize the case that only the line containing the cursor in the
13088 selected window has changed. Variables starting with this_ are
13089 set in display_line and record information about the line
13090 containing the cursor. */
13091 tlbufpos = this_line_start_pos;
13092 tlendpos = this_line_end_pos;
13093 if (!consider_all_windows_p
13094 && CHARPOS (tlbufpos) > 0
13095 && !w->update_mode_line
13096 && !current_buffer->clip_changed
13097 && !current_buffer->prevent_redisplay_optimizations_p
13098 && FRAME_VISIBLE_P (XFRAME (w->frame))
13099 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13100 /* Make sure recorded data applies to current buffer, etc. */
13101 && this_line_buffer == current_buffer
13102 && current_buffer == XBUFFER (w->contents)
13103 && !w->force_start
13104 && !w->optional_new_start
13105 /* Point must be on the line that we have info recorded about. */
13106 && PT >= CHARPOS (tlbufpos)
13107 && PT <= Z - CHARPOS (tlendpos)
13108 /* All text outside that line, including its final newline,
13109 must be unchanged. */
13110 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13111 CHARPOS (tlendpos)))
13112 {
13113 if (CHARPOS (tlbufpos) > BEGV
13114 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13115 && (CHARPOS (tlbufpos) == ZV
13116 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13117 /* Former continuation line has disappeared by becoming empty. */
13118 goto cancel;
13119 else if (window_outdated (w) || MINI_WINDOW_P (w))
13120 {
13121 /* We have to handle the case of continuation around a
13122 wide-column character (see the comment in indent.c around
13123 line 1340).
13124
13125 For instance, in the following case:
13126
13127 -------- Insert --------
13128 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13129 J_I_ ==> J_I_ `^^' are cursors.
13130 ^^ ^^
13131 -------- --------
13132
13133 As we have to redraw the line above, we cannot use this
13134 optimization. */
13135
13136 struct it it;
13137 int line_height_before = this_line_pixel_height;
13138
13139 /* Note that start_display will handle the case that the
13140 line starting at tlbufpos is a continuation line. */
13141 start_display (&it, w, tlbufpos);
13142
13143 /* Implementation note: It this still necessary? */
13144 if (it.current_x != this_line_start_x)
13145 goto cancel;
13146
13147 TRACE ((stderr, "trying display optimization 1\n"));
13148 w->cursor.vpos = -1;
13149 overlay_arrow_seen = 0;
13150 it.vpos = this_line_vpos;
13151 it.current_y = this_line_y;
13152 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13153 display_line (&it);
13154
13155 /* If line contains point, is not continued,
13156 and ends at same distance from eob as before, we win. */
13157 if (w->cursor.vpos >= 0
13158 /* Line is not continued, otherwise this_line_start_pos
13159 would have been set to 0 in display_line. */
13160 && CHARPOS (this_line_start_pos)
13161 /* Line ends as before. */
13162 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13163 /* Line has same height as before. Otherwise other lines
13164 would have to be shifted up or down. */
13165 && this_line_pixel_height == line_height_before)
13166 {
13167 /* If this is not the window's last line, we must adjust
13168 the charstarts of the lines below. */
13169 if (it.current_y < it.last_visible_y)
13170 {
13171 struct glyph_row *row
13172 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13173 ptrdiff_t delta, delta_bytes;
13174
13175 /* We used to distinguish between two cases here,
13176 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13177 when the line ends in a newline or the end of the
13178 buffer's accessible portion. But both cases did
13179 the same, so they were collapsed. */
13180 delta = (Z
13181 - CHARPOS (tlendpos)
13182 - MATRIX_ROW_START_CHARPOS (row));
13183 delta_bytes = (Z_BYTE
13184 - BYTEPOS (tlendpos)
13185 - MATRIX_ROW_START_BYTEPOS (row));
13186
13187 increment_matrix_positions (w->current_matrix,
13188 this_line_vpos + 1,
13189 w->current_matrix->nrows,
13190 delta, delta_bytes);
13191 }
13192
13193 /* If this row displays text now but previously didn't,
13194 or vice versa, w->window_end_vpos may have to be
13195 adjusted. */
13196 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13197 {
13198 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13199 wset_window_end_vpos (w, make_number (this_line_vpos));
13200 }
13201 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13202 && this_line_vpos > 0)
13203 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13204 w->window_end_valid = 0;
13205
13206 /* Update hint: No need to try to scroll in update_window. */
13207 w->desired_matrix->no_scrolling_p = 1;
13208
13209 #ifdef GLYPH_DEBUG
13210 *w->desired_matrix->method = 0;
13211 debug_method_add (w, "optimization 1");
13212 #endif
13213 #if HAVE_XWIDGETS
13214 //debug optimization movement issue
13215 //w->desired_matrix->no_scrolling_p = 1;
13216 //*w->desired_matrix->method = 0;
13217 //debug_method_add (w, "optimization 1");
13218 #endif
13219
13220 #ifdef HAVE_WINDOW_SYSTEM
13221 update_window_fringes (w, 0);
13222 #endif
13223 goto update;
13224 }
13225 else
13226 goto cancel;
13227 }
13228 else if (/* Cursor position hasn't changed. */
13229 PT == w->last_point
13230 /* Make sure the cursor was last displayed
13231 in this window. Otherwise we have to reposition it. */
13232 && 0 <= w->cursor.vpos
13233 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13234 {
13235 if (!must_finish)
13236 {
13237 do_pending_window_change (1);
13238 /* If selected_window changed, redisplay again. */
13239 if (WINDOWP (selected_window)
13240 && (w = XWINDOW (selected_window)) != sw)
13241 goto retry;
13242
13243 /* We used to always goto end_of_redisplay here, but this
13244 isn't enough if we have a blinking cursor. */
13245 if (w->cursor_off_p == w->last_cursor_off_p)
13246 goto end_of_redisplay;
13247 }
13248 goto update;
13249 }
13250 /* If highlighting the region, or if the cursor is in the echo area,
13251 then we can't just move the cursor. */
13252 else if (! (!NILP (Vtransient_mark_mode)
13253 && !NILP (BVAR (current_buffer, mark_active)))
13254 && (EQ (selected_window,
13255 BVAR (current_buffer, last_selected_window))
13256 || highlight_nonselected_windows)
13257 && !w->region_showing
13258 && NILP (Vshow_trailing_whitespace)
13259 && !cursor_in_echo_area)
13260 {
13261 struct it it;
13262 struct glyph_row *row;
13263
13264 /* Skip from tlbufpos to PT and see where it is. Note that
13265 PT may be in invisible text. If so, we will end at the
13266 next visible position. */
13267 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13268 NULL, DEFAULT_FACE_ID);
13269 it.current_x = this_line_start_x;
13270 it.current_y = this_line_y;
13271 it.vpos = this_line_vpos;
13272
13273 /* The call to move_it_to stops in front of PT, but
13274 moves over before-strings. */
13275 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13276
13277 if (it.vpos == this_line_vpos
13278 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13279 row->enabled_p))
13280 {
13281 eassert (this_line_vpos == it.vpos);
13282 eassert (this_line_y == it.current_y);
13283 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13284 #ifdef GLYPH_DEBUG
13285 *w->desired_matrix->method = 0;
13286 debug_method_add (w, "optimization 3");
13287 #endif
13288 goto update;
13289 }
13290 else
13291 goto cancel;
13292 }
13293
13294 cancel:
13295 /* Text changed drastically or point moved off of line. */
13296 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13297 }
13298
13299 CHARPOS (this_line_start_pos) = 0;
13300 consider_all_windows_p |= buffer_shared_and_changed ();
13301 ++clear_face_cache_count;
13302 #ifdef HAVE_WINDOW_SYSTEM
13303 ++clear_image_cache_count;
13304 #endif
13305
13306 /* Build desired matrices, and update the display. If
13307 consider_all_windows_p is non-zero, do it for all windows on all
13308 frames. Otherwise do it for selected_window, only. */
13309
13310 if (consider_all_windows_p)
13311 {
13312 FOR_EACH_FRAME (tail, frame)
13313 XFRAME (frame)->updated_p = 0;
13314
13315 FOR_EACH_FRAME (tail, frame)
13316 {
13317 struct frame *f = XFRAME (frame);
13318
13319 /* We don't have to do anything for unselected terminal
13320 frames. */
13321 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13322 && !EQ (FRAME_TTY (f)->top_frame, frame))
13323 continue;
13324
13325 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13326 {
13327 /* Mark all the scroll bars to be removed; we'll redeem
13328 the ones we want when we redisplay their windows. */
13329 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13330 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13331
13332 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13333 redisplay_windows (FRAME_ROOT_WINDOW (f));
13334
13335 /* The X error handler may have deleted that frame. */
13336 if (!FRAME_LIVE_P (f))
13337 continue;
13338
13339 /* Any scroll bars which redisplay_windows should have
13340 nuked should now go away. */
13341 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13342 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13343
13344 /* If fonts changed, display again. */
13345 /* ??? rms: I suspect it is a mistake to jump all the way
13346 back to retry here. It should just retry this frame. */
13347 if (fonts_changed_p)
13348 goto retry;
13349
13350 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13351 {
13352 /* See if we have to hscroll. */
13353 if (!f->already_hscrolled_p)
13354 {
13355 f->already_hscrolled_p = 1;
13356 if (hscroll_windows (f->root_window))
13357 goto retry;
13358 }
13359
13360 /* Prevent various kinds of signals during display
13361 update. stdio is not robust about handling
13362 signals, which can cause an apparent I/O
13363 error. */
13364 if (interrupt_input)
13365 unrequest_sigio ();
13366 STOP_POLLING;
13367
13368 /* Update the display. */
13369 set_window_update_flags (XWINDOW (f->root_window), 1);
13370 pending |= update_frame (f, 0, 0);
13371 f->updated_p = 1;
13372 }
13373 }
13374 }
13375
13376 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13377
13378 if (!pending)
13379 {
13380 /* Do the mark_window_display_accurate after all windows have
13381 been redisplayed because this call resets flags in buffers
13382 which are needed for proper redisplay. */
13383 FOR_EACH_FRAME (tail, frame)
13384 {
13385 struct frame *f = XFRAME (frame);
13386 if (f->updated_p)
13387 {
13388 mark_window_display_accurate (f->root_window, 1);
13389 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13390 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13391 }
13392 }
13393 }
13394 }
13395 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13396 {
13397 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13398 struct frame *mini_frame;
13399
13400 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13401 /* Use list_of_error, not Qerror, so that
13402 we catch only errors and don't run the debugger. */
13403 internal_condition_case_1 (redisplay_window_1, selected_window,
13404 list_of_error,
13405 redisplay_window_error);
13406 if (update_miniwindow_p)
13407 internal_condition_case_1 (redisplay_window_1, mini_window,
13408 list_of_error,
13409 redisplay_window_error);
13410
13411 /* Compare desired and current matrices, perform output. */
13412
13413 update:
13414 /* If fonts changed, display again. */
13415 if (fonts_changed_p)
13416 goto retry;
13417
13418 /* Prevent various kinds of signals during display update.
13419 stdio is not robust about handling signals,
13420 which can cause an apparent I/O error. */
13421 if (interrupt_input)
13422 unrequest_sigio ();
13423 STOP_POLLING;
13424
13425 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13426 {
13427 if (hscroll_windows (selected_window))
13428 goto retry;
13429
13430 XWINDOW (selected_window)->must_be_updated_p = 1;
13431 pending = update_frame (sf, 0, 0);
13432 }
13433
13434 /* We may have called echo_area_display at the top of this
13435 function. If the echo area is on another frame, that may
13436 have put text on a frame other than the selected one, so the
13437 above call to update_frame would not have caught it. Catch
13438 it here. */
13439 mini_window = FRAME_MINIBUF_WINDOW (sf);
13440 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13441
13442 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13443 {
13444 XWINDOW (mini_window)->must_be_updated_p = 1;
13445 pending |= update_frame (mini_frame, 0, 0);
13446 if (!pending && hscroll_windows (mini_window))
13447 goto retry;
13448 }
13449 }
13450
13451 /* If display was paused because of pending input, make sure we do a
13452 thorough update the next time. */
13453 if (pending)
13454 {
13455 /* Prevent the optimization at the beginning of
13456 redisplay_internal that tries a single-line update of the
13457 line containing the cursor in the selected window. */
13458 CHARPOS (this_line_start_pos) = 0;
13459
13460 /* Let the overlay arrow be updated the next time. */
13461 update_overlay_arrows (0);
13462
13463 /* If we pause after scrolling, some rows in the current
13464 matrices of some windows are not valid. */
13465 if (!WINDOW_FULL_WIDTH_P (w)
13466 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13467 update_mode_lines = 1;
13468 }
13469 else
13470 {
13471 if (!consider_all_windows_p)
13472 {
13473 /* This has already been done above if
13474 consider_all_windows_p is set. */
13475 mark_window_display_accurate_1 (w, 1);
13476
13477 /* Say overlay arrows are up to date. */
13478 update_overlay_arrows (1);
13479
13480 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13481 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13482 }
13483
13484 update_mode_lines = 0;
13485 windows_or_buffers_changed = 0;
13486 cursor_type_changed = 0;
13487 }
13488
13489 /* Start SIGIO interrupts coming again. Having them off during the
13490 code above makes it less likely one will discard output, but not
13491 impossible, since there might be stuff in the system buffer here.
13492 But it is much hairier to try to do anything about that. */
13493 if (interrupt_input)
13494 request_sigio ();
13495 RESUME_POLLING;
13496
13497 /* If a frame has become visible which was not before, redisplay
13498 again, so that we display it. Expose events for such a frame
13499 (which it gets when becoming visible) don't call the parts of
13500 redisplay constructing glyphs, so simply exposing a frame won't
13501 display anything in this case. So, we have to display these
13502 frames here explicitly. */
13503 if (!pending)
13504 {
13505 int new_count = 0;
13506
13507 FOR_EACH_FRAME (tail, frame)
13508 {
13509 int this_is_visible = 0;
13510
13511 if (XFRAME (frame)->visible)
13512 this_is_visible = 1;
13513
13514 if (this_is_visible)
13515 new_count++;
13516 }
13517
13518 if (new_count != number_of_visible_frames)
13519 windows_or_buffers_changed++;
13520 }
13521
13522 /* Change frame size now if a change is pending. */
13523 do_pending_window_change (1);
13524
13525 /* If we just did a pending size change, or have additional
13526 visible frames, or selected_window changed, redisplay again. */
13527 if ((windows_or_buffers_changed && !pending)
13528 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13529 goto retry;
13530
13531 /* Clear the face and image caches.
13532
13533 We used to do this only if consider_all_windows_p. But the cache
13534 needs to be cleared if a timer creates images in the current
13535 buffer (e.g. the test case in Bug#6230). */
13536
13537 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13538 {
13539 clear_face_cache (0);
13540 clear_face_cache_count = 0;
13541 }
13542
13543 #ifdef HAVE_WINDOW_SYSTEM
13544 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13545 {
13546 clear_image_caches (Qnil);
13547 clear_image_cache_count = 0;
13548 }
13549 #endif /* HAVE_WINDOW_SYSTEM */
13550
13551 end_of_redisplay:
13552 backtrace_list = backtrace.next;
13553 unbind_to (count, Qnil);
13554 RESUME_POLLING;
13555 }
13556
13557
13558 /* Redisplay, but leave alone any recent echo area message unless
13559 another message has been requested in its place.
13560
13561 This is useful in situations where you need to redisplay but no
13562 user action has occurred, making it inappropriate for the message
13563 area to be cleared. See tracking_off and
13564 wait_reading_process_output for examples of these situations.
13565
13566 FROM_WHERE is an integer saying from where this function was
13567 called. This is useful for debugging. */
13568
13569 void
13570 redisplay_preserve_echo_area (int from_where)
13571 {
13572 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13573
13574 if (!NILP (echo_area_buffer[1]))
13575 {
13576 /* We have a previously displayed message, but no current
13577 message. Redisplay the previous message. */
13578 display_last_displayed_message_p = 1;
13579 redisplay_internal ();
13580 display_last_displayed_message_p = 0;
13581 }
13582 else
13583 redisplay_internal ();
13584
13585 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13586 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13587 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13588 }
13589
13590
13591 /* Function registered with record_unwind_protect in redisplay_internal.
13592 Clear redisplaying_p. Also select the previously selected frame. */
13593
13594 static Lisp_Object
13595 unwind_redisplay (Lisp_Object old_frame)
13596 {
13597 redisplaying_p = 0;
13598 return Qnil;
13599 }
13600
13601
13602 /* Mark the display of leaf window W as accurate or inaccurate.
13603 If ACCURATE_P is non-zero mark display of W as accurate. If
13604 ACCURATE_P is zero, arrange for W to be redisplayed the next
13605 time redisplay_internal is called. */
13606
13607 static void
13608 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13609 {
13610 struct buffer *b = XBUFFER (w->contents);
13611
13612 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13613 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13614 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13615
13616 if (accurate_p)
13617 {
13618 b->clip_changed = 0;
13619 b->prevent_redisplay_optimizations_p = 0;
13620
13621 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13622 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13623 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13624 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13625
13626 w->current_matrix->buffer = b;
13627 w->current_matrix->begv = BUF_BEGV (b);
13628 w->current_matrix->zv = BUF_ZV (b);
13629
13630 w->last_cursor = w->cursor;
13631 w->last_cursor_off_p = w->cursor_off_p;
13632
13633 if (w == XWINDOW (selected_window))
13634 w->last_point = BUF_PT (b);
13635 else
13636 w->last_point = marker_position (w->pointm);
13637
13638 w->window_end_valid = 1;
13639 w->update_mode_line = 0;
13640 }
13641 }
13642
13643
13644 /* Mark the display of windows in the window tree rooted at WINDOW as
13645 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13646 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13647 be redisplayed the next time redisplay_internal is called. */
13648
13649 void
13650 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13651 {
13652 struct window *w;
13653
13654 for (; !NILP (window); window = w->next)
13655 {
13656 w = XWINDOW (window);
13657 if (WINDOWP (w->contents))
13658 mark_window_display_accurate (w->contents, accurate_p);
13659 else
13660 mark_window_display_accurate_1 (w, accurate_p);
13661 }
13662
13663 if (accurate_p)
13664 update_overlay_arrows (1);
13665 else
13666 /* Force a thorough redisplay the next time by setting
13667 last_arrow_position and last_arrow_string to t, which is
13668 unequal to any useful value of Voverlay_arrow_... */
13669 update_overlay_arrows (-1);
13670 }
13671
13672
13673 /* Return value in display table DP (Lisp_Char_Table *) for character
13674 C. Since a display table doesn't have any parent, we don't have to
13675 follow parent. Do not call this function directly but use the
13676 macro DISP_CHAR_VECTOR. */
13677
13678 Lisp_Object
13679 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13680 {
13681 Lisp_Object val;
13682
13683 if (ASCII_CHAR_P (c))
13684 {
13685 val = dp->ascii;
13686 if (SUB_CHAR_TABLE_P (val))
13687 val = XSUB_CHAR_TABLE (val)->contents[c];
13688 }
13689 else
13690 {
13691 Lisp_Object table;
13692
13693 XSETCHAR_TABLE (table, dp);
13694 val = char_table_ref (table, c);
13695 }
13696 if (NILP (val))
13697 val = dp->defalt;
13698 return val;
13699 }
13700
13701
13702 \f
13703 /***********************************************************************
13704 Window Redisplay
13705 ***********************************************************************/
13706
13707 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13708
13709 static void
13710 redisplay_windows (Lisp_Object window)
13711 {
13712 while (!NILP (window))
13713 {
13714 struct window *w = XWINDOW (window);
13715
13716 if (WINDOWP (w->contents))
13717 redisplay_windows (w->contents);
13718 else if (BUFFERP (w->contents))
13719 {
13720 displayed_buffer = XBUFFER (w->contents);
13721 /* Use list_of_error, not Qerror, so that
13722 we catch only errors and don't run the debugger. */
13723 internal_condition_case_1 (redisplay_window_0, window,
13724 list_of_error,
13725 redisplay_window_error);
13726 }
13727
13728 window = w->next;
13729 }
13730 }
13731
13732 static Lisp_Object
13733 redisplay_window_error (Lisp_Object ignore)
13734 {
13735 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13736 return Qnil;
13737 }
13738
13739 static Lisp_Object
13740 redisplay_window_0 (Lisp_Object window)
13741 {
13742 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13743 redisplay_window (window, 0);
13744 return Qnil;
13745 }
13746
13747 static Lisp_Object
13748 redisplay_window_1 (Lisp_Object window)
13749 {
13750 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13751 redisplay_window (window, 1);
13752 return Qnil;
13753 }
13754 \f
13755
13756 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13757 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13758 which positions recorded in ROW differ from current buffer
13759 positions.
13760
13761 Return 0 if cursor is not on this row, 1 otherwise. */
13762
13763 static int
13764 set_cursor_from_row (struct window *w, struct glyph_row *row,
13765 struct glyph_matrix *matrix,
13766 ptrdiff_t delta, ptrdiff_t delta_bytes,
13767 int dy, int dvpos)
13768 {
13769 struct glyph *glyph = row->glyphs[TEXT_AREA];
13770 struct glyph *end = glyph + row->used[TEXT_AREA];
13771 struct glyph *cursor = NULL;
13772 /* The last known character position in row. */
13773 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13774 int x = row->x;
13775 ptrdiff_t pt_old = PT - delta;
13776 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13777 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13778 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13779 /* A glyph beyond the edge of TEXT_AREA which we should never
13780 touch. */
13781 struct glyph *glyphs_end = end;
13782 /* Non-zero means we've found a match for cursor position, but that
13783 glyph has the avoid_cursor_p flag set. */
13784 int match_with_avoid_cursor = 0;
13785 /* Non-zero means we've seen at least one glyph that came from a
13786 display string. */
13787 int string_seen = 0;
13788 /* Largest and smallest buffer positions seen so far during scan of
13789 glyph row. */
13790 ptrdiff_t bpos_max = pos_before;
13791 ptrdiff_t bpos_min = pos_after;
13792 /* Last buffer position covered by an overlay string with an integer
13793 `cursor' property. */
13794 ptrdiff_t bpos_covered = 0;
13795 /* Non-zero means the display string on which to display the cursor
13796 comes from a text property, not from an overlay. */
13797 int string_from_text_prop = 0;
13798
13799 /* Don't even try doing anything if called for a mode-line or
13800 header-line row, since the rest of the code isn't prepared to
13801 deal with such calamities. */
13802 eassert (!row->mode_line_p);
13803 if (row->mode_line_p)
13804 return 0;
13805
13806 /* Skip over glyphs not having an object at the start and the end of
13807 the row. These are special glyphs like truncation marks on
13808 terminal frames. */
13809 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13810 {
13811 if (!row->reversed_p)
13812 {
13813 while (glyph < end
13814 && INTEGERP (glyph->object)
13815 && glyph->charpos < 0)
13816 {
13817 x += glyph->pixel_width;
13818 ++glyph;
13819 }
13820 while (end > glyph
13821 && INTEGERP ((end - 1)->object)
13822 /* CHARPOS is zero for blanks and stretch glyphs
13823 inserted by extend_face_to_end_of_line. */
13824 && (end - 1)->charpos <= 0)
13825 --end;
13826 glyph_before = glyph - 1;
13827 glyph_after = end;
13828 }
13829 else
13830 {
13831 struct glyph *g;
13832
13833 /* If the glyph row is reversed, we need to process it from back
13834 to front, so swap the edge pointers. */
13835 glyphs_end = end = glyph - 1;
13836 glyph += row->used[TEXT_AREA] - 1;
13837
13838 while (glyph > end + 1
13839 && INTEGERP (glyph->object)
13840 && glyph->charpos < 0)
13841 {
13842 --glyph;
13843 x -= glyph->pixel_width;
13844 }
13845 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13846 --glyph;
13847 /* By default, in reversed rows we put the cursor on the
13848 rightmost (first in the reading order) glyph. */
13849 for (g = end + 1; g < glyph; g++)
13850 x += g->pixel_width;
13851 while (end < glyph
13852 && INTEGERP ((end + 1)->object)
13853 && (end + 1)->charpos <= 0)
13854 ++end;
13855 glyph_before = glyph + 1;
13856 glyph_after = end;
13857 }
13858 }
13859 else if (row->reversed_p)
13860 {
13861 /* In R2L rows that don't display text, put the cursor on the
13862 rightmost glyph. Case in point: an empty last line that is
13863 part of an R2L paragraph. */
13864 cursor = end - 1;
13865 /* Avoid placing the cursor on the last glyph of the row, where
13866 on terminal frames we hold the vertical border between
13867 adjacent windows. */
13868 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13869 && !WINDOW_RIGHTMOST_P (w)
13870 && cursor == row->glyphs[LAST_AREA] - 1)
13871 cursor--;
13872 x = -1; /* will be computed below, at label compute_x */
13873 }
13874
13875 /* Step 1: Try to find the glyph whose character position
13876 corresponds to point. If that's not possible, find 2 glyphs
13877 whose character positions are the closest to point, one before
13878 point, the other after it. */
13879 if (!row->reversed_p)
13880 while (/* not marched to end of glyph row */
13881 glyph < end
13882 /* glyph was not inserted by redisplay for internal purposes */
13883 && !INTEGERP (glyph->object))
13884 {
13885 if (BUFFERP (glyph->object))
13886 {
13887 ptrdiff_t dpos = glyph->charpos - pt_old;
13888
13889 if (glyph->charpos > bpos_max)
13890 bpos_max = glyph->charpos;
13891 if (glyph->charpos < bpos_min)
13892 bpos_min = glyph->charpos;
13893 if (!glyph->avoid_cursor_p)
13894 {
13895 /* If we hit point, we've found the glyph on which to
13896 display the cursor. */
13897 if (dpos == 0)
13898 {
13899 match_with_avoid_cursor = 0;
13900 break;
13901 }
13902 /* See if we've found a better approximation to
13903 POS_BEFORE or to POS_AFTER. */
13904 if (0 > dpos && dpos > pos_before - pt_old)
13905 {
13906 pos_before = glyph->charpos;
13907 glyph_before = glyph;
13908 }
13909 else if (0 < dpos && dpos < pos_after - pt_old)
13910 {
13911 pos_after = glyph->charpos;
13912 glyph_after = glyph;
13913 }
13914 }
13915 else if (dpos == 0)
13916 match_with_avoid_cursor = 1;
13917 }
13918 else if (STRINGP (glyph->object))
13919 {
13920 Lisp_Object chprop;
13921 ptrdiff_t glyph_pos = glyph->charpos;
13922
13923 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13924 glyph->object);
13925 if (!NILP (chprop))
13926 {
13927 /* If the string came from a `display' text property,
13928 look up the buffer position of that property and
13929 use that position to update bpos_max, as if we
13930 actually saw such a position in one of the row's
13931 glyphs. This helps with supporting integer values
13932 of `cursor' property on the display string in
13933 situations where most or all of the row's buffer
13934 text is completely covered by display properties,
13935 so that no glyph with valid buffer positions is
13936 ever seen in the row. */
13937 ptrdiff_t prop_pos =
13938 string_buffer_position_lim (glyph->object, pos_before,
13939 pos_after, 0);
13940
13941 if (prop_pos >= pos_before)
13942 bpos_max = prop_pos - 1;
13943 }
13944 if (INTEGERP (chprop))
13945 {
13946 bpos_covered = bpos_max + XINT (chprop);
13947 /* If the `cursor' property covers buffer positions up
13948 to and including point, we should display cursor on
13949 this glyph. Note that, if a `cursor' property on one
13950 of the string's characters has an integer value, we
13951 will break out of the loop below _before_ we get to
13952 the position match above. IOW, integer values of
13953 the `cursor' property override the "exact match for
13954 point" strategy of positioning the cursor. */
13955 /* Implementation note: bpos_max == pt_old when, e.g.,
13956 we are in an empty line, where bpos_max is set to
13957 MATRIX_ROW_START_CHARPOS, see above. */
13958 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13959 {
13960 cursor = glyph;
13961 break;
13962 }
13963 }
13964
13965 string_seen = 1;
13966 }
13967 x += glyph->pixel_width;
13968 ++glyph;
13969 }
13970 else if (glyph > end) /* row is reversed */
13971 while (!INTEGERP (glyph->object))
13972 {
13973 if (BUFFERP (glyph->object))
13974 {
13975 ptrdiff_t dpos = glyph->charpos - pt_old;
13976
13977 if (glyph->charpos > bpos_max)
13978 bpos_max = glyph->charpos;
13979 if (glyph->charpos < bpos_min)
13980 bpos_min = glyph->charpos;
13981 if (!glyph->avoid_cursor_p)
13982 {
13983 if (dpos == 0)
13984 {
13985 match_with_avoid_cursor = 0;
13986 break;
13987 }
13988 if (0 > dpos && dpos > pos_before - pt_old)
13989 {
13990 pos_before = glyph->charpos;
13991 glyph_before = glyph;
13992 }
13993 else if (0 < dpos && dpos < pos_after - pt_old)
13994 {
13995 pos_after = glyph->charpos;
13996 glyph_after = glyph;
13997 }
13998 }
13999 else if (dpos == 0)
14000 match_with_avoid_cursor = 1;
14001 }
14002 else if (STRINGP (glyph->object))
14003 {
14004 Lisp_Object chprop;
14005 ptrdiff_t glyph_pos = glyph->charpos;
14006
14007 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14008 glyph->object);
14009 if (!NILP (chprop))
14010 {
14011 ptrdiff_t prop_pos =
14012 string_buffer_position_lim (glyph->object, pos_before,
14013 pos_after, 0);
14014
14015 if (prop_pos >= pos_before)
14016 bpos_max = prop_pos - 1;
14017 }
14018 if (INTEGERP (chprop))
14019 {
14020 bpos_covered = bpos_max + XINT (chprop);
14021 /* If the `cursor' property covers buffer positions up
14022 to and including point, we should display cursor on
14023 this glyph. */
14024 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14025 {
14026 cursor = glyph;
14027 break;
14028 }
14029 }
14030 string_seen = 1;
14031 }
14032 --glyph;
14033 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14034 {
14035 x--; /* can't use any pixel_width */
14036 break;
14037 }
14038 x -= glyph->pixel_width;
14039 }
14040
14041 /* Step 2: If we didn't find an exact match for point, we need to
14042 look for a proper place to put the cursor among glyphs between
14043 GLYPH_BEFORE and GLYPH_AFTER. */
14044 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14045 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14046 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14047 {
14048 /* An empty line has a single glyph whose OBJECT is zero and
14049 whose CHARPOS is the position of a newline on that line.
14050 Note that on a TTY, there are more glyphs after that, which
14051 were produced by extend_face_to_end_of_line, but their
14052 CHARPOS is zero or negative. */
14053 int empty_line_p =
14054 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14055 && INTEGERP (glyph->object) && glyph->charpos > 0
14056 /* On a TTY, continued and truncated rows also have a glyph at
14057 their end whose OBJECT is zero and whose CHARPOS is
14058 positive (the continuation and truncation glyphs), but such
14059 rows are obviously not "empty". */
14060 && !(row->continued_p || row->truncated_on_right_p);
14061
14062 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14063 {
14064 ptrdiff_t ellipsis_pos;
14065
14066 /* Scan back over the ellipsis glyphs. */
14067 if (!row->reversed_p)
14068 {
14069 ellipsis_pos = (glyph - 1)->charpos;
14070 while (glyph > row->glyphs[TEXT_AREA]
14071 && (glyph - 1)->charpos == ellipsis_pos)
14072 glyph--, x -= glyph->pixel_width;
14073 /* That loop always goes one position too far, including
14074 the glyph before the ellipsis. So scan forward over
14075 that one. */
14076 x += glyph->pixel_width;
14077 glyph++;
14078 }
14079 else /* row is reversed */
14080 {
14081 ellipsis_pos = (glyph + 1)->charpos;
14082 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14083 && (glyph + 1)->charpos == ellipsis_pos)
14084 glyph++, x += glyph->pixel_width;
14085 x -= glyph->pixel_width;
14086 glyph--;
14087 }
14088 }
14089 else if (match_with_avoid_cursor)
14090 {
14091 cursor = glyph_after;
14092 x = -1;
14093 }
14094 else if (string_seen)
14095 {
14096 int incr = row->reversed_p ? -1 : +1;
14097
14098 /* Need to find the glyph that came out of a string which is
14099 present at point. That glyph is somewhere between
14100 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14101 positioned between POS_BEFORE and POS_AFTER in the
14102 buffer. */
14103 struct glyph *start, *stop;
14104 ptrdiff_t pos = pos_before;
14105
14106 x = -1;
14107
14108 /* If the row ends in a newline from a display string,
14109 reordering could have moved the glyphs belonging to the
14110 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14111 in this case we extend the search to the last glyph in
14112 the row that was not inserted by redisplay. */
14113 if (row->ends_in_newline_from_string_p)
14114 {
14115 glyph_after = end;
14116 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14117 }
14118
14119 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14120 correspond to POS_BEFORE and POS_AFTER, respectively. We
14121 need START and STOP in the order that corresponds to the
14122 row's direction as given by its reversed_p flag. If the
14123 directionality of characters between POS_BEFORE and
14124 POS_AFTER is the opposite of the row's base direction,
14125 these characters will have been reordered for display,
14126 and we need to reverse START and STOP. */
14127 if (!row->reversed_p)
14128 {
14129 start = min (glyph_before, glyph_after);
14130 stop = max (glyph_before, glyph_after);
14131 }
14132 else
14133 {
14134 start = max (glyph_before, glyph_after);
14135 stop = min (glyph_before, glyph_after);
14136 }
14137 for (glyph = start + incr;
14138 row->reversed_p ? glyph > stop : glyph < stop; )
14139 {
14140
14141 /* Any glyphs that come from the buffer are here because
14142 of bidi reordering. Skip them, and only pay
14143 attention to glyphs that came from some string. */
14144 if (STRINGP (glyph->object))
14145 {
14146 Lisp_Object str;
14147 ptrdiff_t tem;
14148 /* If the display property covers the newline, we
14149 need to search for it one position farther. */
14150 ptrdiff_t lim = pos_after
14151 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14152
14153 string_from_text_prop = 0;
14154 str = glyph->object;
14155 tem = string_buffer_position_lim (str, pos, lim, 0);
14156 if (tem == 0 /* from overlay */
14157 || pos <= tem)
14158 {
14159 /* If the string from which this glyph came is
14160 found in the buffer at point, or at position
14161 that is closer to point than pos_after, then
14162 we've found the glyph we've been looking for.
14163 If it comes from an overlay (tem == 0), and
14164 it has the `cursor' property on one of its
14165 glyphs, record that glyph as a candidate for
14166 displaying the cursor. (As in the
14167 unidirectional version, we will display the
14168 cursor on the last candidate we find.) */
14169 if (tem == 0
14170 || tem == pt_old
14171 || (tem - pt_old > 0 && tem < pos_after))
14172 {
14173 /* The glyphs from this string could have
14174 been reordered. Find the one with the
14175 smallest string position. Or there could
14176 be a character in the string with the
14177 `cursor' property, which means display
14178 cursor on that character's glyph. */
14179 ptrdiff_t strpos = glyph->charpos;
14180
14181 if (tem)
14182 {
14183 cursor = glyph;
14184 string_from_text_prop = 1;
14185 }
14186 for ( ;
14187 (row->reversed_p ? glyph > stop : glyph < stop)
14188 && EQ (glyph->object, str);
14189 glyph += incr)
14190 {
14191 Lisp_Object cprop;
14192 ptrdiff_t gpos = glyph->charpos;
14193
14194 cprop = Fget_char_property (make_number (gpos),
14195 Qcursor,
14196 glyph->object);
14197 if (!NILP (cprop))
14198 {
14199 cursor = glyph;
14200 break;
14201 }
14202 if (tem && glyph->charpos < strpos)
14203 {
14204 strpos = glyph->charpos;
14205 cursor = glyph;
14206 }
14207 }
14208
14209 if (tem == pt_old
14210 || (tem - pt_old > 0 && tem < pos_after))
14211 goto compute_x;
14212 }
14213 if (tem)
14214 pos = tem + 1; /* don't find previous instances */
14215 }
14216 /* This string is not what we want; skip all of the
14217 glyphs that came from it. */
14218 while ((row->reversed_p ? glyph > stop : glyph < stop)
14219 && EQ (glyph->object, str))
14220 glyph += incr;
14221 }
14222 else
14223 glyph += incr;
14224 }
14225
14226 /* If we reached the end of the line, and END was from a string,
14227 the cursor is not on this line. */
14228 if (cursor == NULL
14229 && (row->reversed_p ? glyph <= end : glyph >= end)
14230 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14231 && STRINGP (end->object)
14232 && row->continued_p)
14233 return 0;
14234 }
14235 /* A truncated row may not include PT among its character positions.
14236 Setting the cursor inside the scroll margin will trigger
14237 recalculation of hscroll in hscroll_window_tree. But if a
14238 display string covers point, defer to the string-handling
14239 code below to figure this out. */
14240 else if (row->truncated_on_left_p && pt_old < bpos_min)
14241 {
14242 cursor = glyph_before;
14243 x = -1;
14244 }
14245 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14246 /* Zero-width characters produce no glyphs. */
14247 || (!empty_line_p
14248 && (row->reversed_p
14249 ? glyph_after > glyphs_end
14250 : glyph_after < glyphs_end)))
14251 {
14252 cursor = glyph_after;
14253 x = -1;
14254 }
14255 }
14256
14257 compute_x:
14258 if (cursor != NULL)
14259 glyph = cursor;
14260 else if (glyph == glyphs_end
14261 && pos_before == pos_after
14262 && STRINGP ((row->reversed_p
14263 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14264 : row->glyphs[TEXT_AREA])->object))
14265 {
14266 /* If all the glyphs of this row came from strings, put the
14267 cursor on the first glyph of the row. This avoids having the
14268 cursor outside of the text area in this very rare and hard
14269 use case. */
14270 glyph =
14271 row->reversed_p
14272 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14273 : row->glyphs[TEXT_AREA];
14274 }
14275 if (x < 0)
14276 {
14277 struct glyph *g;
14278
14279 /* Need to compute x that corresponds to GLYPH. */
14280 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14281 {
14282 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14283 emacs_abort ();
14284 x += g->pixel_width;
14285 }
14286 }
14287
14288 /* ROW could be part of a continued line, which, under bidi
14289 reordering, might have other rows whose start and end charpos
14290 occlude point. Only set w->cursor if we found a better
14291 approximation to the cursor position than we have from previously
14292 examined candidate rows belonging to the same continued line. */
14293 if (/* we already have a candidate row */
14294 w->cursor.vpos >= 0
14295 /* that candidate is not the row we are processing */
14296 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14297 /* Make sure cursor.vpos specifies a row whose start and end
14298 charpos occlude point, and it is valid candidate for being a
14299 cursor-row. This is because some callers of this function
14300 leave cursor.vpos at the row where the cursor was displayed
14301 during the last redisplay cycle. */
14302 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14303 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14304 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14305 {
14306 struct glyph *g1 =
14307 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14308
14309 /* Don't consider glyphs that are outside TEXT_AREA. */
14310 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14311 return 0;
14312 /* Keep the candidate whose buffer position is the closest to
14313 point or has the `cursor' property. */
14314 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14315 w->cursor.hpos >= 0
14316 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14317 && ((BUFFERP (g1->object)
14318 && (g1->charpos == pt_old /* an exact match always wins */
14319 || (BUFFERP (glyph->object)
14320 && eabs (g1->charpos - pt_old)
14321 < eabs (glyph->charpos - pt_old))))
14322 /* previous candidate is a glyph from a string that has
14323 a non-nil `cursor' property */
14324 || (STRINGP (g1->object)
14325 && (!NILP (Fget_char_property (make_number (g1->charpos),
14326 Qcursor, g1->object))
14327 /* previous candidate is from the same display
14328 string as this one, and the display string
14329 came from a text property */
14330 || (EQ (g1->object, glyph->object)
14331 && string_from_text_prop)
14332 /* this candidate is from newline and its
14333 position is not an exact match */
14334 || (INTEGERP (glyph->object)
14335 && glyph->charpos != pt_old)))))
14336 return 0;
14337 /* If this candidate gives an exact match, use that. */
14338 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14339 /* If this candidate is a glyph created for the
14340 terminating newline of a line, and point is on that
14341 newline, it wins because it's an exact match. */
14342 || (!row->continued_p
14343 && INTEGERP (glyph->object)
14344 && glyph->charpos == 0
14345 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14346 /* Otherwise, keep the candidate that comes from a row
14347 spanning less buffer positions. This may win when one or
14348 both candidate positions are on glyphs that came from
14349 display strings, for which we cannot compare buffer
14350 positions. */
14351 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14352 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14353 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14354 return 0;
14355 }
14356 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14357 w->cursor.x = x;
14358 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14359 w->cursor.y = row->y + dy;
14360
14361 if (w == XWINDOW (selected_window))
14362 {
14363 if (!row->continued_p
14364 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14365 && row->x == 0)
14366 {
14367 this_line_buffer = XBUFFER (w->contents);
14368
14369 CHARPOS (this_line_start_pos)
14370 = MATRIX_ROW_START_CHARPOS (row) + delta;
14371 BYTEPOS (this_line_start_pos)
14372 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14373
14374 CHARPOS (this_line_end_pos)
14375 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14376 BYTEPOS (this_line_end_pos)
14377 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14378
14379 this_line_y = w->cursor.y;
14380 this_line_pixel_height = row->height;
14381 this_line_vpos = w->cursor.vpos;
14382 this_line_start_x = row->x;
14383 }
14384 else
14385 CHARPOS (this_line_start_pos) = 0;
14386 }
14387
14388 return 1;
14389 }
14390
14391
14392 /* Run window scroll functions, if any, for WINDOW with new window
14393 start STARTP. Sets the window start of WINDOW to that position.
14394
14395 We assume that the window's buffer is really current. */
14396
14397 static struct text_pos
14398 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14399 {
14400 struct window *w = XWINDOW (window);
14401 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14402
14403 if (current_buffer != XBUFFER (w->contents))
14404 emacs_abort ();
14405
14406 if (!NILP (Vwindow_scroll_functions))
14407 {
14408 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14409 make_number (CHARPOS (startp)));
14410 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14411 /* In case the hook functions switch buffers. */
14412 set_buffer_internal (XBUFFER (w->contents));
14413 }
14414
14415 return startp;
14416 }
14417
14418
14419 /* Make sure the line containing the cursor is fully visible.
14420 A value of 1 means there is nothing to be done.
14421 (Either the line is fully visible, or it cannot be made so,
14422 or we cannot tell.)
14423
14424 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14425 is higher than window.
14426
14427 A value of 0 means the caller should do scrolling
14428 as if point had gone off the screen. */
14429
14430 static int
14431 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14432 {
14433 struct glyph_matrix *matrix;
14434 struct glyph_row *row;
14435 int window_height;
14436
14437 if (!make_cursor_line_fully_visible_p)
14438 return 1;
14439
14440 /* It's not always possible to find the cursor, e.g, when a window
14441 is full of overlay strings. Don't do anything in that case. */
14442 if (w->cursor.vpos < 0)
14443 return 1;
14444
14445 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14446 row = MATRIX_ROW (matrix, w->cursor.vpos);
14447
14448 /* If the cursor row is not partially visible, there's nothing to do. */
14449 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14450 return 1;
14451
14452 /* If the row the cursor is in is taller than the window's height,
14453 it's not clear what to do, so do nothing. */
14454 window_height = window_box_height (w);
14455 if (row->height >= window_height)
14456 {
14457 if (!force_p || MINI_WINDOW_P (w)
14458 || w->vscroll || w->cursor.vpos == 0)
14459 return 1;
14460 }
14461 return 0;
14462 }
14463
14464
14465 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14466 non-zero means only WINDOW is redisplayed in redisplay_internal.
14467 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14468 in redisplay_window to bring a partially visible line into view in
14469 the case that only the cursor has moved.
14470
14471 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14472 last screen line's vertical height extends past the end of the screen.
14473
14474 Value is
14475
14476 1 if scrolling succeeded
14477
14478 0 if scrolling didn't find point.
14479
14480 -1 if new fonts have been loaded so that we must interrupt
14481 redisplay, adjust glyph matrices, and try again. */
14482
14483 enum
14484 {
14485 SCROLLING_SUCCESS,
14486 SCROLLING_FAILED,
14487 SCROLLING_NEED_LARGER_MATRICES
14488 };
14489
14490 /* If scroll-conservatively is more than this, never recenter.
14491
14492 If you change this, don't forget to update the doc string of
14493 `scroll-conservatively' and the Emacs manual. */
14494 #define SCROLL_LIMIT 100
14495
14496 static int
14497 try_scrolling (Lisp_Object window, int just_this_one_p,
14498 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14499 int temp_scroll_step, int last_line_misfit)
14500 {
14501 struct window *w = XWINDOW (window);
14502 struct frame *f = XFRAME (w->frame);
14503 struct text_pos pos, startp;
14504 struct it it;
14505 int this_scroll_margin, scroll_max, rc, height;
14506 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14507 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14508 Lisp_Object aggressive;
14509 /* We will never try scrolling more than this number of lines. */
14510 int scroll_limit = SCROLL_LIMIT;
14511
14512 #ifdef GLYPH_DEBUG
14513 debug_method_add (w, "try_scrolling");
14514 #endif
14515
14516 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14517
14518 /* Compute scroll margin height in pixels. We scroll when point is
14519 within this distance from the top or bottom of the window. */
14520 if (scroll_margin > 0)
14521 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14522 * FRAME_LINE_HEIGHT (f);
14523 else
14524 this_scroll_margin = 0;
14525
14526 /* Force arg_scroll_conservatively to have a reasonable value, to
14527 avoid scrolling too far away with slow move_it_* functions. Note
14528 that the user can supply scroll-conservatively equal to
14529 `most-positive-fixnum', which can be larger than INT_MAX. */
14530 if (arg_scroll_conservatively > scroll_limit)
14531 {
14532 arg_scroll_conservatively = scroll_limit + 1;
14533 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14534 }
14535 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14536 /* Compute how much we should try to scroll maximally to bring
14537 point into view. */
14538 scroll_max = (max (scroll_step,
14539 max (arg_scroll_conservatively, temp_scroll_step))
14540 * FRAME_LINE_HEIGHT (f));
14541 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14542 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14543 /* We're trying to scroll because of aggressive scrolling but no
14544 scroll_step is set. Choose an arbitrary one. */
14545 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14546 else
14547 scroll_max = 0;
14548
14549 too_near_end:
14550
14551 /* Decide whether to scroll down. */
14552 if (PT > CHARPOS (startp))
14553 {
14554 int scroll_margin_y;
14555
14556 /* Compute the pixel ypos of the scroll margin, then move IT to
14557 either that ypos or PT, whichever comes first. */
14558 start_display (&it, w, startp);
14559 scroll_margin_y = it.last_visible_y - this_scroll_margin
14560 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14561 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14562 (MOVE_TO_POS | MOVE_TO_Y));
14563
14564 if (PT > CHARPOS (it.current.pos))
14565 {
14566 int y0 = line_bottom_y (&it);
14567 /* Compute how many pixels below window bottom to stop searching
14568 for PT. This avoids costly search for PT that is far away if
14569 the user limited scrolling by a small number of lines, but
14570 always finds PT if scroll_conservatively is set to a large
14571 number, such as most-positive-fixnum. */
14572 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14573 int y_to_move = it.last_visible_y + slack;
14574
14575 /* Compute the distance from the scroll margin to PT or to
14576 the scroll limit, whichever comes first. This should
14577 include the height of the cursor line, to make that line
14578 fully visible. */
14579 move_it_to (&it, PT, -1, y_to_move,
14580 -1, MOVE_TO_POS | MOVE_TO_Y);
14581 dy = line_bottom_y (&it) - y0;
14582
14583 if (dy > scroll_max)
14584 return SCROLLING_FAILED;
14585
14586 if (dy > 0)
14587 scroll_down_p = 1;
14588 }
14589 }
14590
14591 if (scroll_down_p)
14592 {
14593 /* Point is in or below the bottom scroll margin, so move the
14594 window start down. If scrolling conservatively, move it just
14595 enough down to make point visible. If scroll_step is set,
14596 move it down by scroll_step. */
14597 if (arg_scroll_conservatively)
14598 amount_to_scroll
14599 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14600 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14601 else if (scroll_step || temp_scroll_step)
14602 amount_to_scroll = scroll_max;
14603 else
14604 {
14605 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14606 height = WINDOW_BOX_TEXT_HEIGHT (w);
14607 if (NUMBERP (aggressive))
14608 {
14609 double float_amount = XFLOATINT (aggressive) * height;
14610 int aggressive_scroll = float_amount;
14611 if (aggressive_scroll == 0 && float_amount > 0)
14612 aggressive_scroll = 1;
14613 /* Don't let point enter the scroll margin near top of
14614 the window. This could happen if the value of
14615 scroll_up_aggressively is too large and there are
14616 non-zero margins, because scroll_up_aggressively
14617 means put point that fraction of window height
14618 _from_the_bottom_margin_. */
14619 if (aggressive_scroll + 2*this_scroll_margin > height)
14620 aggressive_scroll = height - 2*this_scroll_margin;
14621 amount_to_scroll = dy + aggressive_scroll;
14622 }
14623 }
14624
14625 if (amount_to_scroll <= 0)
14626 return SCROLLING_FAILED;
14627
14628 start_display (&it, w, startp);
14629 if (arg_scroll_conservatively <= scroll_limit)
14630 move_it_vertically (&it, amount_to_scroll);
14631 else
14632 {
14633 /* Extra precision for users who set scroll-conservatively
14634 to a large number: make sure the amount we scroll
14635 the window start is never less than amount_to_scroll,
14636 which was computed as distance from window bottom to
14637 point. This matters when lines at window top and lines
14638 below window bottom have different height. */
14639 struct it it1;
14640 void *it1data = NULL;
14641 /* We use a temporary it1 because line_bottom_y can modify
14642 its argument, if it moves one line down; see there. */
14643 int start_y;
14644
14645 SAVE_IT (it1, it, it1data);
14646 start_y = line_bottom_y (&it1);
14647 do {
14648 RESTORE_IT (&it, &it, it1data);
14649 move_it_by_lines (&it, 1);
14650 SAVE_IT (it1, it, it1data);
14651 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14652 }
14653
14654 /* If STARTP is unchanged, move it down another screen line. */
14655 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14656 move_it_by_lines (&it, 1);
14657 startp = it.current.pos;
14658 }
14659 else
14660 {
14661 struct text_pos scroll_margin_pos = startp;
14662 int y_offset = 0;
14663
14664 /* See if point is inside the scroll margin at the top of the
14665 window. */
14666 if (this_scroll_margin)
14667 {
14668 int y_start;
14669
14670 start_display (&it, w, startp);
14671 y_start = it.current_y;
14672 move_it_vertically (&it, this_scroll_margin);
14673 scroll_margin_pos = it.current.pos;
14674 /* If we didn't move enough before hitting ZV, request
14675 additional amount of scroll, to move point out of the
14676 scroll margin. */
14677 if (IT_CHARPOS (it) == ZV
14678 && it.current_y - y_start < this_scroll_margin)
14679 y_offset = this_scroll_margin - (it.current_y - y_start);
14680 }
14681
14682 if (PT < CHARPOS (scroll_margin_pos))
14683 {
14684 /* Point is in the scroll margin at the top of the window or
14685 above what is displayed in the window. */
14686 int y0, y_to_move;
14687
14688 /* Compute the vertical distance from PT to the scroll
14689 margin position. Move as far as scroll_max allows, or
14690 one screenful, or 10 screen lines, whichever is largest.
14691 Give up if distance is greater than scroll_max or if we
14692 didn't reach the scroll margin position. */
14693 SET_TEXT_POS (pos, PT, PT_BYTE);
14694 start_display (&it, w, pos);
14695 y0 = it.current_y;
14696 y_to_move = max (it.last_visible_y,
14697 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14698 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14699 y_to_move, -1,
14700 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14701 dy = it.current_y - y0;
14702 if (dy > scroll_max
14703 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14704 return SCROLLING_FAILED;
14705
14706 /* Additional scroll for when ZV was too close to point. */
14707 dy += y_offset;
14708
14709 /* Compute new window start. */
14710 start_display (&it, w, startp);
14711
14712 if (arg_scroll_conservatively)
14713 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14714 max (scroll_step, temp_scroll_step));
14715 else if (scroll_step || temp_scroll_step)
14716 amount_to_scroll = scroll_max;
14717 else
14718 {
14719 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14720 height = WINDOW_BOX_TEXT_HEIGHT (w);
14721 if (NUMBERP (aggressive))
14722 {
14723 double float_amount = XFLOATINT (aggressive) * height;
14724 int aggressive_scroll = float_amount;
14725 if (aggressive_scroll == 0 && float_amount > 0)
14726 aggressive_scroll = 1;
14727 /* Don't let point enter the scroll margin near
14728 bottom of the window, if the value of
14729 scroll_down_aggressively happens to be too
14730 large. */
14731 if (aggressive_scroll + 2*this_scroll_margin > height)
14732 aggressive_scroll = height - 2*this_scroll_margin;
14733 amount_to_scroll = dy + aggressive_scroll;
14734 }
14735 }
14736
14737 if (amount_to_scroll <= 0)
14738 return SCROLLING_FAILED;
14739
14740 move_it_vertically_backward (&it, amount_to_scroll);
14741 startp = it.current.pos;
14742 }
14743 }
14744
14745 /* Run window scroll functions. */
14746 startp = run_window_scroll_functions (window, startp);
14747
14748 /* Display the window. Give up if new fonts are loaded, or if point
14749 doesn't appear. */
14750 if (!try_window (window, startp, 0))
14751 rc = SCROLLING_NEED_LARGER_MATRICES;
14752 else if (w->cursor.vpos < 0)
14753 {
14754 clear_glyph_matrix (w->desired_matrix);
14755 rc = SCROLLING_FAILED;
14756 }
14757 else
14758 {
14759 /* Maybe forget recorded base line for line number display. */
14760 if (!just_this_one_p
14761 || current_buffer->clip_changed
14762 || BEG_UNCHANGED < CHARPOS (startp))
14763 w->base_line_number = 0;
14764
14765 /* If cursor ends up on a partially visible line,
14766 treat that as being off the bottom of the screen. */
14767 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14768 /* It's possible that the cursor is on the first line of the
14769 buffer, which is partially obscured due to a vscroll
14770 (Bug#7537). In that case, avoid looping forever . */
14771 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14772 {
14773 clear_glyph_matrix (w->desired_matrix);
14774 ++extra_scroll_margin_lines;
14775 goto too_near_end;
14776 }
14777 rc = SCROLLING_SUCCESS;
14778 }
14779
14780 return rc;
14781 }
14782
14783
14784 /* Compute a suitable window start for window W if display of W starts
14785 on a continuation line. Value is non-zero if a new window start
14786 was computed.
14787
14788 The new window start will be computed, based on W's width, starting
14789 from the start of the continued line. It is the start of the
14790 screen line with the minimum distance from the old start W->start. */
14791
14792 static int
14793 compute_window_start_on_continuation_line (struct window *w)
14794 {
14795 struct text_pos pos, start_pos;
14796 int window_start_changed_p = 0;
14797
14798 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14799
14800 /* If window start is on a continuation line... Window start may be
14801 < BEGV in case there's invisible text at the start of the
14802 buffer (M-x rmail, for example). */
14803 if (CHARPOS (start_pos) > BEGV
14804 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14805 {
14806 struct it it;
14807 struct glyph_row *row;
14808
14809 /* Handle the case that the window start is out of range. */
14810 if (CHARPOS (start_pos) < BEGV)
14811 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14812 else if (CHARPOS (start_pos) > ZV)
14813 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14814
14815 /* Find the start of the continued line. This should be fast
14816 because find_newline is fast (newline cache). */
14817 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14818 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14819 row, DEFAULT_FACE_ID);
14820 reseat_at_previous_visible_line_start (&it);
14821
14822 /* If the line start is "too far" away from the window start,
14823 say it takes too much time to compute a new window start. */
14824 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14825 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14826 {
14827 int min_distance, distance;
14828
14829 /* Move forward by display lines to find the new window
14830 start. If window width was enlarged, the new start can
14831 be expected to be > the old start. If window width was
14832 decreased, the new window start will be < the old start.
14833 So, we're looking for the display line start with the
14834 minimum distance from the old window start. */
14835 pos = it.current.pos;
14836 min_distance = INFINITY;
14837 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14838 distance < min_distance)
14839 {
14840 min_distance = distance;
14841 pos = it.current.pos;
14842 move_it_by_lines (&it, 1);
14843 }
14844
14845 /* Set the window start there. */
14846 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14847 window_start_changed_p = 1;
14848 }
14849 }
14850
14851 return window_start_changed_p;
14852 }
14853
14854
14855 /* Try cursor movement in case text has not changed in window WINDOW,
14856 with window start STARTP. Value is
14857
14858 CURSOR_MOVEMENT_SUCCESS if successful
14859
14860 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14861
14862 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14863 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14864 we want to scroll as if scroll-step were set to 1. See the code.
14865
14866 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14867 which case we have to abort this redisplay, and adjust matrices
14868 first. */
14869
14870 enum
14871 {
14872 CURSOR_MOVEMENT_SUCCESS,
14873 CURSOR_MOVEMENT_CANNOT_BE_USED,
14874 CURSOR_MOVEMENT_MUST_SCROLL,
14875 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14876 };
14877
14878 static int
14879 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14880 {
14881 struct window *w = XWINDOW (window);
14882 struct frame *f = XFRAME (w->frame);
14883 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14884
14885 #ifdef GLYPH_DEBUG
14886 if (inhibit_try_cursor_movement)
14887 return rc;
14888 #endif
14889
14890 /* Previously, there was a check for Lisp integer in the
14891 if-statement below. Now, this field is converted to
14892 ptrdiff_t, thus zero means invalid position in a buffer. */
14893 eassert (w->last_point > 0);
14894
14895 /* Handle case where text has not changed, only point, and it has
14896 not moved off the frame. */
14897 if (/* Point may be in this window. */
14898 PT >= CHARPOS (startp)
14899 /* Selective display hasn't changed. */
14900 && !current_buffer->clip_changed
14901 /* Function force-mode-line-update is used to force a thorough
14902 redisplay. It sets either windows_or_buffers_changed or
14903 update_mode_lines. So don't take a shortcut here for these
14904 cases. */
14905 && !update_mode_lines
14906 && !windows_or_buffers_changed
14907 && !cursor_type_changed
14908 /* Can't use this case if highlighting a region. When a
14909 region exists, cursor movement has to do more than just
14910 set the cursor. */
14911 && markpos_of_region () < 0
14912 && !w->region_showing
14913 && NILP (Vshow_trailing_whitespace)
14914 /* This code is not used for mini-buffer for the sake of the case
14915 of redisplaying to replace an echo area message; since in
14916 that case the mini-buffer contents per se are usually
14917 unchanged. This code is of no real use in the mini-buffer
14918 since the handling of this_line_start_pos, etc., in redisplay
14919 handles the same cases. */
14920 && !EQ (window, minibuf_window)
14921 /* When splitting windows or for new windows, it happens that
14922 redisplay is called with a nil window_end_vpos or one being
14923 larger than the window. This should really be fixed in
14924 window.c. I don't have this on my list, now, so we do
14925 approximately the same as the old redisplay code. --gerd. */
14926 && INTEGERP (w->window_end_vpos)
14927 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14928 && (FRAME_WINDOW_P (f)
14929 || !overlay_arrow_in_current_buffer_p ()))
14930 {
14931 int this_scroll_margin, top_scroll_margin;
14932 struct glyph_row *row = NULL;
14933
14934 #ifdef GLYPH_DEBUG
14935 debug_method_add (w, "cursor movement");
14936 #endif
14937
14938 /* Scroll if point within this distance from the top or bottom
14939 of the window. This is a pixel value. */
14940 if (scroll_margin > 0)
14941 {
14942 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14943 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14944 }
14945 else
14946 this_scroll_margin = 0;
14947
14948 top_scroll_margin = this_scroll_margin;
14949 if (WINDOW_WANTS_HEADER_LINE_P (w))
14950 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14951
14952 /* Start with the row the cursor was displayed during the last
14953 not paused redisplay. Give up if that row is not valid. */
14954 if (w->last_cursor.vpos < 0
14955 || w->last_cursor.vpos >= w->current_matrix->nrows)
14956 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14957 else
14958 {
14959 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14960 if (row->mode_line_p)
14961 ++row;
14962 if (!row->enabled_p)
14963 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14964 }
14965
14966 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14967 {
14968 int scroll_p = 0, must_scroll = 0;
14969 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14970
14971 if (PT > w->last_point)
14972 {
14973 /* Point has moved forward. */
14974 while (MATRIX_ROW_END_CHARPOS (row) < PT
14975 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14976 {
14977 eassert (row->enabled_p);
14978 ++row;
14979 }
14980
14981 /* If the end position of a row equals the start
14982 position of the next row, and PT is at that position,
14983 we would rather display cursor in the next line. */
14984 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14985 && MATRIX_ROW_END_CHARPOS (row) == PT
14986 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
14987 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14988 && !cursor_row_p (row))
14989 ++row;
14990
14991 /* If within the scroll margin, scroll. Note that
14992 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14993 the next line would be drawn, and that
14994 this_scroll_margin can be zero. */
14995 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14996 || PT > MATRIX_ROW_END_CHARPOS (row)
14997 /* Line is completely visible last line in window
14998 and PT is to be set in the next line. */
14999 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15000 && PT == MATRIX_ROW_END_CHARPOS (row)
15001 && !row->ends_at_zv_p
15002 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15003 scroll_p = 1;
15004 }
15005 else if (PT < w->last_point)
15006 {
15007 /* Cursor has to be moved backward. Note that PT >=
15008 CHARPOS (startp) because of the outer if-statement. */
15009 while (!row->mode_line_p
15010 && (MATRIX_ROW_START_CHARPOS (row) > PT
15011 || (MATRIX_ROW_START_CHARPOS (row) == PT
15012 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15013 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15014 row > w->current_matrix->rows
15015 && (row-1)->ends_in_newline_from_string_p))))
15016 && (row->y > top_scroll_margin
15017 || CHARPOS (startp) == BEGV))
15018 {
15019 eassert (row->enabled_p);
15020 --row;
15021 }
15022
15023 /* Consider the following case: Window starts at BEGV,
15024 there is invisible, intangible text at BEGV, so that
15025 display starts at some point START > BEGV. It can
15026 happen that we are called with PT somewhere between
15027 BEGV and START. Try to handle that case. */
15028 if (row < w->current_matrix->rows
15029 || row->mode_line_p)
15030 {
15031 row = w->current_matrix->rows;
15032 if (row->mode_line_p)
15033 ++row;
15034 }
15035
15036 /* Due to newlines in overlay strings, we may have to
15037 skip forward over overlay strings. */
15038 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15039 && MATRIX_ROW_END_CHARPOS (row) == PT
15040 && !cursor_row_p (row))
15041 ++row;
15042
15043 /* If within the scroll margin, scroll. */
15044 if (row->y < top_scroll_margin
15045 && CHARPOS (startp) != BEGV)
15046 scroll_p = 1;
15047 }
15048 else
15049 {
15050 /* Cursor did not move. So don't scroll even if cursor line
15051 is partially visible, as it was so before. */
15052 rc = CURSOR_MOVEMENT_SUCCESS;
15053 }
15054
15055 if (PT < MATRIX_ROW_START_CHARPOS (row)
15056 || PT > MATRIX_ROW_END_CHARPOS (row))
15057 {
15058 /* if PT is not in the glyph row, give up. */
15059 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15060 must_scroll = 1;
15061 }
15062 else if (rc != CURSOR_MOVEMENT_SUCCESS
15063 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15064 {
15065 struct glyph_row *row1;
15066
15067 /* If rows are bidi-reordered and point moved, back up
15068 until we find a row that does not belong to a
15069 continuation line. This is because we must consider
15070 all rows of a continued line as candidates for the
15071 new cursor positioning, since row start and end
15072 positions change non-linearly with vertical position
15073 in such rows. */
15074 /* FIXME: Revisit this when glyph ``spilling'' in
15075 continuation lines' rows is implemented for
15076 bidi-reordered rows. */
15077 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15078 MATRIX_ROW_CONTINUATION_LINE_P (row);
15079 --row)
15080 {
15081 /* If we hit the beginning of the displayed portion
15082 without finding the first row of a continued
15083 line, give up. */
15084 if (row <= row1)
15085 {
15086 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15087 break;
15088 }
15089 eassert (row->enabled_p);
15090 }
15091 }
15092 if (must_scroll)
15093 ;
15094 else if (rc != CURSOR_MOVEMENT_SUCCESS
15095 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15096 /* Make sure this isn't a header line by any chance, since
15097 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15098 && !row->mode_line_p
15099 && make_cursor_line_fully_visible_p)
15100 {
15101 if (PT == MATRIX_ROW_END_CHARPOS (row)
15102 && !row->ends_at_zv_p
15103 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15104 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15105 else if (row->height > window_box_height (w))
15106 {
15107 /* If we end up in a partially visible line, let's
15108 make it fully visible, except when it's taller
15109 than the window, in which case we can't do much
15110 about it. */
15111 *scroll_step = 1;
15112 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15113 }
15114 else
15115 {
15116 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15117 if (!cursor_row_fully_visible_p (w, 0, 1))
15118 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15119 else
15120 rc = CURSOR_MOVEMENT_SUCCESS;
15121 }
15122 }
15123 else if (scroll_p)
15124 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15125 else if (rc != CURSOR_MOVEMENT_SUCCESS
15126 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15127 {
15128 /* With bidi-reordered rows, there could be more than
15129 one candidate row whose start and end positions
15130 occlude point. We need to let set_cursor_from_row
15131 find the best candidate. */
15132 /* FIXME: Revisit this when glyph ``spilling'' in
15133 continuation lines' rows is implemented for
15134 bidi-reordered rows. */
15135 int rv = 0;
15136
15137 do
15138 {
15139 int at_zv_p = 0, exact_match_p = 0;
15140
15141 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15142 && PT <= MATRIX_ROW_END_CHARPOS (row)
15143 && cursor_row_p (row))
15144 rv |= set_cursor_from_row (w, row, w->current_matrix,
15145 0, 0, 0, 0);
15146 /* As soon as we've found the exact match for point,
15147 or the first suitable row whose ends_at_zv_p flag
15148 is set, we are done. */
15149 at_zv_p =
15150 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15151 if (rv && !at_zv_p
15152 && w->cursor.hpos >= 0
15153 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15154 w->cursor.vpos))
15155 {
15156 struct glyph_row *candidate =
15157 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15158 struct glyph *g =
15159 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15160 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15161
15162 exact_match_p =
15163 (BUFFERP (g->object) && g->charpos == PT)
15164 || (INTEGERP (g->object)
15165 && (g->charpos == PT
15166 || (g->charpos == 0 && endpos - 1 == PT)));
15167 }
15168 if (rv && (at_zv_p || exact_match_p))
15169 {
15170 rc = CURSOR_MOVEMENT_SUCCESS;
15171 break;
15172 }
15173 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15174 break;
15175 ++row;
15176 }
15177 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15178 || row->continued_p)
15179 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15180 || (MATRIX_ROW_START_CHARPOS (row) == PT
15181 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15182 /* If we didn't find any candidate rows, or exited the
15183 loop before all the candidates were examined, signal
15184 to the caller that this method failed. */
15185 if (rc != CURSOR_MOVEMENT_SUCCESS
15186 && !(rv
15187 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15188 && !row->continued_p))
15189 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15190 else if (rv)
15191 rc = CURSOR_MOVEMENT_SUCCESS;
15192 }
15193 else
15194 {
15195 do
15196 {
15197 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15198 {
15199 rc = CURSOR_MOVEMENT_SUCCESS;
15200 break;
15201 }
15202 ++row;
15203 }
15204 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15205 && MATRIX_ROW_START_CHARPOS (row) == PT
15206 && cursor_row_p (row));
15207 }
15208 }
15209 }
15210
15211 return rc;
15212 }
15213
15214 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15215 static
15216 #endif
15217 void
15218 set_vertical_scroll_bar (struct window *w)
15219 {
15220 ptrdiff_t start, end, whole;
15221
15222 /* Calculate the start and end positions for the current window.
15223 At some point, it would be nice to choose between scrollbars
15224 which reflect the whole buffer size, with special markers
15225 indicating narrowing, and scrollbars which reflect only the
15226 visible region.
15227
15228 Note that mini-buffers sometimes aren't displaying any text. */
15229 if (!MINI_WINDOW_P (w)
15230 || (w == XWINDOW (minibuf_window)
15231 && NILP (echo_area_buffer[0])))
15232 {
15233 struct buffer *buf = XBUFFER (w->contents);
15234 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15235 start = marker_position (w->start) - BUF_BEGV (buf);
15236 /* I don't think this is guaranteed to be right. For the
15237 moment, we'll pretend it is. */
15238 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15239
15240 if (end < start)
15241 end = start;
15242 if (whole < (end - start))
15243 whole = end - start;
15244 }
15245 else
15246 start = end = whole = 0;
15247
15248 /* Indicate what this scroll bar ought to be displaying now. */
15249 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15250 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15251 (w, end - start, whole, start);
15252 }
15253
15254
15255 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15256 selected_window is redisplayed.
15257
15258 We can return without actually redisplaying the window if
15259 fonts_changed_p. In that case, redisplay_internal will
15260 retry. */
15261
15262 static void
15263 redisplay_window (Lisp_Object window, int just_this_one_p)
15264 {
15265 struct window *w = XWINDOW (window);
15266 struct frame *f = XFRAME (w->frame);
15267 struct buffer *buffer = XBUFFER (w->contents);
15268 struct buffer *old = current_buffer;
15269 struct text_pos lpoint, opoint, startp;
15270 int update_mode_line;
15271 int tem;
15272 struct it it;
15273 /* Record it now because it's overwritten. */
15274 int current_matrix_up_to_date_p = 0;
15275 int used_current_matrix_p = 0;
15276 /* This is less strict than current_matrix_up_to_date_p.
15277 It indicates that the buffer contents and narrowing are unchanged. */
15278 int buffer_unchanged_p = 0;
15279 int temp_scroll_step = 0;
15280 ptrdiff_t count = SPECPDL_INDEX ();
15281 int rc;
15282 int centering_position = -1;
15283 int last_line_misfit = 0;
15284 ptrdiff_t beg_unchanged, end_unchanged;
15285
15286 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15287 opoint = lpoint;
15288
15289 #ifdef GLYPH_DEBUG
15290 *w->desired_matrix->method = 0;
15291 #endif
15292
15293 /* Make sure that both W's markers are valid. */
15294 eassert (XMARKER (w->start)->buffer == buffer);
15295 eassert (XMARKER (w->pointm)->buffer == buffer);
15296
15297 restart:
15298 reconsider_clip_changes (w, buffer);
15299
15300 /* Has the mode line to be updated? */
15301 update_mode_line = (w->update_mode_line
15302 || update_mode_lines
15303 || buffer->clip_changed
15304 || buffer->prevent_redisplay_optimizations_p);
15305
15306 if (MINI_WINDOW_P (w))
15307 {
15308 if (w == XWINDOW (echo_area_window)
15309 && !NILP (echo_area_buffer[0]))
15310 {
15311 if (update_mode_line)
15312 /* We may have to update a tty frame's menu bar or a
15313 tool-bar. Example `M-x C-h C-h C-g'. */
15314 goto finish_menu_bars;
15315 else
15316 /* We've already displayed the echo area glyphs in this window. */
15317 goto finish_scroll_bars;
15318 }
15319 else if ((w != XWINDOW (minibuf_window)
15320 || minibuf_level == 0)
15321 /* When buffer is nonempty, redisplay window normally. */
15322 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15323 /* Quail displays non-mini buffers in minibuffer window.
15324 In that case, redisplay the window normally. */
15325 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15326 {
15327 /* W is a mini-buffer window, but it's not active, so clear
15328 it. */
15329 int yb = window_text_bottom_y (w);
15330 struct glyph_row *row;
15331 int y;
15332
15333 for (y = 0, row = w->desired_matrix->rows;
15334 y < yb;
15335 y += row->height, ++row)
15336 blank_row (w, row, y);
15337 goto finish_scroll_bars;
15338 }
15339
15340 clear_glyph_matrix (w->desired_matrix);
15341 }
15342
15343 /* Otherwise set up data on this window; select its buffer and point
15344 value. */
15345 /* Really select the buffer, for the sake of buffer-local
15346 variables. */
15347 set_buffer_internal_1 (XBUFFER (w->contents));
15348
15349 current_matrix_up_to_date_p
15350 = (w->window_end_valid
15351 && !current_buffer->clip_changed
15352 && !current_buffer->prevent_redisplay_optimizations_p
15353 && !window_outdated (w));
15354
15355 /* Run the window-bottom-change-functions
15356 if it is possible that the text on the screen has changed
15357 (either due to modification of the text, or any other reason). */
15358 if (!current_matrix_up_to_date_p
15359 && !NILP (Vwindow_text_change_functions))
15360 {
15361 safe_run_hooks (Qwindow_text_change_functions);
15362 goto restart;
15363 }
15364
15365 beg_unchanged = BEG_UNCHANGED;
15366 end_unchanged = END_UNCHANGED;
15367
15368 SET_TEXT_POS (opoint, PT, PT_BYTE);
15369
15370 specbind (Qinhibit_point_motion_hooks, Qt);
15371
15372 buffer_unchanged_p
15373 = (w->window_end_valid
15374 && !current_buffer->clip_changed
15375 && !window_outdated (w));
15376
15377 /* When windows_or_buffers_changed is non-zero, we can't rely on
15378 the window end being valid, so set it to nil there. */
15379 if (windows_or_buffers_changed)
15380 {
15381 /* If window starts on a continuation line, maybe adjust the
15382 window start in case the window's width changed. */
15383 if (XMARKER (w->start)->buffer == current_buffer)
15384 compute_window_start_on_continuation_line (w);
15385
15386 w->window_end_valid = 0;
15387 }
15388
15389 /* Some sanity checks. */
15390 CHECK_WINDOW_END (w);
15391 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15392 emacs_abort ();
15393 if (BYTEPOS (opoint) < CHARPOS (opoint))
15394 emacs_abort ();
15395
15396 if (mode_line_update_needed (w))
15397 update_mode_line = 1;
15398
15399 /* Point refers normally to the selected window. For any other
15400 window, set up appropriate value. */
15401 if (!EQ (window, selected_window))
15402 {
15403 ptrdiff_t new_pt = marker_position (w->pointm);
15404 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15405 if (new_pt < BEGV)
15406 {
15407 new_pt = BEGV;
15408 new_pt_byte = BEGV_BYTE;
15409 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15410 }
15411 else if (new_pt > (ZV - 1))
15412 {
15413 new_pt = ZV;
15414 new_pt_byte = ZV_BYTE;
15415 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15416 }
15417
15418 /* We don't use SET_PT so that the point-motion hooks don't run. */
15419 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15420 }
15421
15422 /* If any of the character widths specified in the display table
15423 have changed, invalidate the width run cache. It's true that
15424 this may be a bit late to catch such changes, but the rest of
15425 redisplay goes (non-fatally) haywire when the display table is
15426 changed, so why should we worry about doing any better? */
15427 if (current_buffer->width_run_cache)
15428 {
15429 struct Lisp_Char_Table *disptab = buffer_display_table ();
15430
15431 if (! disptab_matches_widthtab
15432 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15433 {
15434 invalidate_region_cache (current_buffer,
15435 current_buffer->width_run_cache,
15436 BEG, Z);
15437 recompute_width_table (current_buffer, disptab);
15438 }
15439 }
15440
15441 /* If window-start is screwed up, choose a new one. */
15442 if (XMARKER (w->start)->buffer != current_buffer)
15443 goto recenter;
15444
15445 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15446
15447 /* If someone specified a new starting point but did not insist,
15448 check whether it can be used. */
15449 if (w->optional_new_start
15450 && CHARPOS (startp) >= BEGV
15451 && CHARPOS (startp) <= ZV)
15452 {
15453 w->optional_new_start = 0;
15454 start_display (&it, w, startp);
15455 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15456 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15457 if (IT_CHARPOS (it) == PT)
15458 w->force_start = 1;
15459 /* IT may overshoot PT if text at PT is invisible. */
15460 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15461 w->force_start = 1;
15462 }
15463
15464 force_start:
15465
15466 /* Handle case where place to start displaying has been specified,
15467 unless the specified location is outside the accessible range. */
15468 if (w->force_start || w->frozen_window_start_p)
15469 {
15470 /* We set this later on if we have to adjust point. */
15471 int new_vpos = -1;
15472
15473 w->force_start = 0;
15474 w->vscroll = 0;
15475 w->window_end_valid = 0;
15476
15477 /* Forget any recorded base line for line number display. */
15478 if (!buffer_unchanged_p)
15479 w->base_line_number = 0;
15480
15481 /* Redisplay the mode line. Select the buffer properly for that.
15482 Also, run the hook window-scroll-functions
15483 because we have scrolled. */
15484 /* Note, we do this after clearing force_start because
15485 if there's an error, it is better to forget about force_start
15486 than to get into an infinite loop calling the hook functions
15487 and having them get more errors. */
15488 if (!update_mode_line
15489 || ! NILP (Vwindow_scroll_functions))
15490 {
15491 update_mode_line = 1;
15492 w->update_mode_line = 1;
15493 startp = run_window_scroll_functions (window, startp);
15494 }
15495
15496 w->last_modified = 0;
15497 w->last_overlay_modified = 0;
15498 if (CHARPOS (startp) < BEGV)
15499 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15500 else if (CHARPOS (startp) > ZV)
15501 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15502
15503 /* Redisplay, then check if cursor has been set during the
15504 redisplay. Give up if new fonts were loaded. */
15505 /* We used to issue a CHECK_MARGINS argument to try_window here,
15506 but this causes scrolling to fail when point begins inside
15507 the scroll margin (bug#148) -- cyd */
15508 if (!try_window (window, startp, 0))
15509 {
15510 w->force_start = 1;
15511 clear_glyph_matrix (w->desired_matrix);
15512 goto need_larger_matrices;
15513 }
15514
15515 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15516 {
15517 /* If point does not appear, try to move point so it does
15518 appear. The desired matrix has been built above, so we
15519 can use it here. */
15520 new_vpos = window_box_height (w) / 2;
15521 }
15522
15523 if (!cursor_row_fully_visible_p (w, 0, 0))
15524 {
15525 /* Point does appear, but on a line partly visible at end of window.
15526 Move it back to a fully-visible line. */
15527 new_vpos = window_box_height (w);
15528 }
15529 else if (w->cursor.vpos >=0)
15530 {
15531 /* Some people insist on not letting point enter the scroll
15532 margin, even though this part handles windows that didn't
15533 scroll at all. */
15534 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15535 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15536 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15537
15538 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15539 below, which finds the row to move point to, advances by
15540 the Y coordinate of the _next_ row, see the definition of
15541 MATRIX_ROW_BOTTOM_Y. */
15542 if (w->cursor.vpos < margin + header_line)
15543 new_vpos
15544 = pixel_margin + (header_line
15545 ? CURRENT_HEADER_LINE_HEIGHT (w)
15546 : 0) + FRAME_LINE_HEIGHT (f);
15547 else
15548 {
15549 int window_height = window_box_height (w);
15550
15551 if (header_line)
15552 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15553 if (w->cursor.y >= window_height - pixel_margin)
15554 new_vpos = window_height - pixel_margin;
15555 }
15556 }
15557
15558 /* If we need to move point for either of the above reasons,
15559 now actually do it. */
15560 if (new_vpos >= 0)
15561 {
15562 struct glyph_row *row;
15563
15564 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15565 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15566 ++row;
15567
15568 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15569 MATRIX_ROW_START_BYTEPOS (row));
15570
15571 if (w != XWINDOW (selected_window))
15572 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15573 else if (current_buffer == old)
15574 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15575
15576 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15577
15578 /* If we are highlighting the region, then we just changed
15579 the region, so redisplay to show it. */
15580 if (markpos_of_region () >= 0)
15581 {
15582 clear_glyph_matrix (w->desired_matrix);
15583 if (!try_window (window, startp, 0))
15584 goto need_larger_matrices;
15585 }
15586 }
15587
15588 #ifdef GLYPH_DEBUG
15589 debug_method_add (w, "forced window start");
15590 #endif
15591 goto done;
15592 }
15593
15594 /* Handle case where text has not changed, only point, and it has
15595 not moved off the frame, and we are not retrying after hscroll.
15596 (current_matrix_up_to_date_p is nonzero when retrying.) */
15597 if (current_matrix_up_to_date_p
15598 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15599 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15600 {
15601 switch (rc)
15602 {
15603 case CURSOR_MOVEMENT_SUCCESS:
15604 used_current_matrix_p = 1;
15605 goto done;
15606
15607 case CURSOR_MOVEMENT_MUST_SCROLL:
15608 goto try_to_scroll;
15609
15610 default:
15611 emacs_abort ();
15612 }
15613 }
15614 /* If current starting point was originally the beginning of a line
15615 but no longer is, find a new starting point. */
15616 else if (w->start_at_line_beg
15617 && !(CHARPOS (startp) <= BEGV
15618 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15619 {
15620 #ifdef GLYPH_DEBUG
15621 debug_method_add (w, "recenter 1");
15622 #endif
15623 goto recenter;
15624 }
15625
15626 /* Try scrolling with try_window_id. Value is > 0 if update has
15627 been done, it is -1 if we know that the same window start will
15628 not work. It is 0 if unsuccessful for some other reason. */
15629 else if ((tem = try_window_id (w)) != 0)
15630 {
15631 #ifdef GLYPH_DEBUG
15632 debug_method_add (w, "try_window_id %d", tem);
15633 #endif
15634
15635 if (fonts_changed_p)
15636 goto need_larger_matrices;
15637 if (tem > 0)
15638 goto done;
15639
15640 /* Otherwise try_window_id has returned -1 which means that we
15641 don't want the alternative below this comment to execute. */
15642 }
15643 else if (CHARPOS (startp) >= BEGV
15644 && CHARPOS (startp) <= ZV
15645 && PT >= CHARPOS (startp)
15646 && (CHARPOS (startp) < ZV
15647 /* Avoid starting at end of buffer. */
15648 || CHARPOS (startp) == BEGV
15649 || !window_outdated (w)))
15650 {
15651 int d1, d2, d3, d4, d5, d6;
15652
15653 /* If first window line is a continuation line, and window start
15654 is inside the modified region, but the first change is before
15655 current window start, we must select a new window start.
15656
15657 However, if this is the result of a down-mouse event (e.g. by
15658 extending the mouse-drag-overlay), we don't want to select a
15659 new window start, since that would change the position under
15660 the mouse, resulting in an unwanted mouse-movement rather
15661 than a simple mouse-click. */
15662 if (!w->start_at_line_beg
15663 && NILP (do_mouse_tracking)
15664 && CHARPOS (startp) > BEGV
15665 && CHARPOS (startp) > BEG + beg_unchanged
15666 && CHARPOS (startp) <= Z - end_unchanged
15667 /* Even if w->start_at_line_beg is nil, a new window may
15668 start at a line_beg, since that's how set_buffer_window
15669 sets it. So, we need to check the return value of
15670 compute_window_start_on_continuation_line. (See also
15671 bug#197). */
15672 && XMARKER (w->start)->buffer == current_buffer
15673 && compute_window_start_on_continuation_line (w)
15674 /* It doesn't make sense to force the window start like we
15675 do at label force_start if it is already known that point
15676 will not be visible in the resulting window, because
15677 doing so will move point from its correct position
15678 instead of scrolling the window to bring point into view.
15679 See bug#9324. */
15680 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15681 {
15682 w->force_start = 1;
15683 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15684 goto force_start;
15685 }
15686
15687 #ifdef GLYPH_DEBUG
15688 debug_method_add (w, "same window start");
15689 #endif
15690
15691 /* Try to redisplay starting at same place as before.
15692 If point has not moved off frame, accept the results. */
15693 if (!current_matrix_up_to_date_p
15694 /* Don't use try_window_reusing_current_matrix in this case
15695 because a window scroll function can have changed the
15696 buffer. */
15697 || !NILP (Vwindow_scroll_functions)
15698 || MINI_WINDOW_P (w)
15699 || !(used_current_matrix_p
15700 = try_window_reusing_current_matrix (w)))
15701 {
15702 IF_DEBUG (debug_method_add (w, "1"));
15703 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15704 /* -1 means we need to scroll.
15705 0 means we need new matrices, but fonts_changed_p
15706 is set in that case, so we will detect it below. */
15707 goto try_to_scroll;
15708 }
15709
15710 if (fonts_changed_p)
15711 goto need_larger_matrices;
15712
15713 if (w->cursor.vpos >= 0)
15714 {
15715 if (!just_this_one_p
15716 || current_buffer->clip_changed
15717 || BEG_UNCHANGED < CHARPOS (startp))
15718 /* Forget any recorded base line for line number display. */
15719 w->base_line_number = 0;
15720
15721 if (!cursor_row_fully_visible_p (w, 1, 0))
15722 {
15723 clear_glyph_matrix (w->desired_matrix);
15724 last_line_misfit = 1;
15725 }
15726 /* Drop through and scroll. */
15727 else
15728 goto done;
15729 }
15730 else
15731 clear_glyph_matrix (w->desired_matrix);
15732 }
15733
15734 try_to_scroll:
15735
15736 w->last_modified = 0;
15737 w->last_overlay_modified = 0;
15738
15739 /* Redisplay the mode line. Select the buffer properly for that. */
15740 if (!update_mode_line)
15741 {
15742 update_mode_line = 1;
15743 w->update_mode_line = 1;
15744 }
15745
15746 /* Try to scroll by specified few lines. */
15747 if ((scroll_conservatively
15748 || emacs_scroll_step
15749 || temp_scroll_step
15750 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15751 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15752 && CHARPOS (startp) >= BEGV
15753 && CHARPOS (startp) <= ZV)
15754 {
15755 /* The function returns -1 if new fonts were loaded, 1 if
15756 successful, 0 if not successful. */
15757 int ss = try_scrolling (window, just_this_one_p,
15758 scroll_conservatively,
15759 emacs_scroll_step,
15760 temp_scroll_step, last_line_misfit);
15761 switch (ss)
15762 {
15763 case SCROLLING_SUCCESS:
15764 goto done;
15765
15766 case SCROLLING_NEED_LARGER_MATRICES:
15767 goto need_larger_matrices;
15768
15769 case SCROLLING_FAILED:
15770 break;
15771
15772 default:
15773 emacs_abort ();
15774 }
15775 }
15776
15777 /* Finally, just choose a place to start which positions point
15778 according to user preferences. */
15779
15780 recenter:
15781
15782 #ifdef GLYPH_DEBUG
15783 debug_method_add (w, "recenter");
15784 #endif
15785
15786 /* Forget any previously recorded base line for line number display. */
15787 if (!buffer_unchanged_p)
15788 w->base_line_number = 0;
15789
15790 /* Determine the window start relative to point. */
15791 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15792 it.current_y = it.last_visible_y;
15793 if (centering_position < 0)
15794 {
15795 int margin =
15796 scroll_margin > 0
15797 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15798 : 0;
15799 ptrdiff_t margin_pos = CHARPOS (startp);
15800 Lisp_Object aggressive;
15801 int scrolling_up;
15802
15803 /* If there is a scroll margin at the top of the window, find
15804 its character position. */
15805 if (margin
15806 /* Cannot call start_display if startp is not in the
15807 accessible region of the buffer. This can happen when we
15808 have just switched to a different buffer and/or changed
15809 its restriction. In that case, startp is initialized to
15810 the character position 1 (BEGV) because we did not yet
15811 have chance to display the buffer even once. */
15812 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15813 {
15814 struct it it1;
15815 void *it1data = NULL;
15816
15817 SAVE_IT (it1, it, it1data);
15818 start_display (&it1, w, startp);
15819 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15820 margin_pos = IT_CHARPOS (it1);
15821 RESTORE_IT (&it, &it, it1data);
15822 }
15823 scrolling_up = PT > margin_pos;
15824 aggressive =
15825 scrolling_up
15826 ? BVAR (current_buffer, scroll_up_aggressively)
15827 : BVAR (current_buffer, scroll_down_aggressively);
15828
15829 if (!MINI_WINDOW_P (w)
15830 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15831 {
15832 int pt_offset = 0;
15833
15834 /* Setting scroll-conservatively overrides
15835 scroll-*-aggressively. */
15836 if (!scroll_conservatively && NUMBERP (aggressive))
15837 {
15838 double float_amount = XFLOATINT (aggressive);
15839
15840 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15841 if (pt_offset == 0 && float_amount > 0)
15842 pt_offset = 1;
15843 if (pt_offset && margin > 0)
15844 margin -= 1;
15845 }
15846 /* Compute how much to move the window start backward from
15847 point so that point will be displayed where the user
15848 wants it. */
15849 if (scrolling_up)
15850 {
15851 centering_position = it.last_visible_y;
15852 if (pt_offset)
15853 centering_position -= pt_offset;
15854 centering_position -=
15855 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15856 + WINDOW_HEADER_LINE_HEIGHT (w);
15857 /* Don't let point enter the scroll margin near top of
15858 the window. */
15859 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15860 centering_position = margin * FRAME_LINE_HEIGHT (f);
15861 }
15862 else
15863 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15864 }
15865 else
15866 /* Set the window start half the height of the window backward
15867 from point. */
15868 centering_position = window_box_height (w) / 2;
15869 }
15870 move_it_vertically_backward (&it, centering_position);
15871
15872 eassert (IT_CHARPOS (it) >= BEGV);
15873
15874 /* The function move_it_vertically_backward may move over more
15875 than the specified y-distance. If it->w is small, e.g. a
15876 mini-buffer window, we may end up in front of the window's
15877 display area. Start displaying at the start of the line
15878 containing PT in this case. */
15879 if (it.current_y <= 0)
15880 {
15881 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15882 move_it_vertically_backward (&it, 0);
15883 it.current_y = 0;
15884 }
15885
15886 it.current_x = it.hpos = 0;
15887
15888 /* Set the window start position here explicitly, to avoid an
15889 infinite loop in case the functions in window-scroll-functions
15890 get errors. */
15891 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15892
15893 /* Run scroll hooks. */
15894 startp = run_window_scroll_functions (window, it.current.pos);
15895
15896 /* Redisplay the window. */
15897 if (!current_matrix_up_to_date_p
15898 || windows_or_buffers_changed
15899 || cursor_type_changed
15900 /* Don't use try_window_reusing_current_matrix in this case
15901 because it can have changed the buffer. */
15902 || !NILP (Vwindow_scroll_functions)
15903 || !just_this_one_p
15904 || MINI_WINDOW_P (w)
15905 || !(used_current_matrix_p
15906 = try_window_reusing_current_matrix (w)))
15907 try_window (window, startp, 0);
15908
15909 /* If new fonts have been loaded (due to fontsets), give up. We
15910 have to start a new redisplay since we need to re-adjust glyph
15911 matrices. */
15912 if (fonts_changed_p)
15913 goto need_larger_matrices;
15914
15915 /* If cursor did not appear assume that the middle of the window is
15916 in the first line of the window. Do it again with the next line.
15917 (Imagine a window of height 100, displaying two lines of height
15918 60. Moving back 50 from it->last_visible_y will end in the first
15919 line.) */
15920 if (w->cursor.vpos < 0)
15921 {
15922 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15923 {
15924 clear_glyph_matrix (w->desired_matrix);
15925 move_it_by_lines (&it, 1);
15926 try_window (window, it.current.pos, 0);
15927 }
15928 else if (PT < IT_CHARPOS (it))
15929 {
15930 clear_glyph_matrix (w->desired_matrix);
15931 move_it_by_lines (&it, -1);
15932 try_window (window, it.current.pos, 0);
15933 }
15934 else
15935 {
15936 /* Not much we can do about it. */
15937 }
15938 }
15939
15940 /* Consider the following case: Window starts at BEGV, there is
15941 invisible, intangible text at BEGV, so that display starts at
15942 some point START > BEGV. It can happen that we are called with
15943 PT somewhere between BEGV and START. Try to handle that case. */
15944 if (w->cursor.vpos < 0)
15945 {
15946 struct glyph_row *row = w->current_matrix->rows;
15947 if (row->mode_line_p)
15948 ++row;
15949 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15950 }
15951
15952 if (!cursor_row_fully_visible_p (w, 0, 0))
15953 {
15954 /* If vscroll is enabled, disable it and try again. */
15955 if (w->vscroll)
15956 {
15957 w->vscroll = 0;
15958 clear_glyph_matrix (w->desired_matrix);
15959 goto recenter;
15960 }
15961
15962 /* Users who set scroll-conservatively to a large number want
15963 point just above/below the scroll margin. If we ended up
15964 with point's row partially visible, move the window start to
15965 make that row fully visible and out of the margin. */
15966 if (scroll_conservatively > SCROLL_LIMIT)
15967 {
15968 int margin =
15969 scroll_margin > 0
15970 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15971 : 0;
15972 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15973
15974 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15975 clear_glyph_matrix (w->desired_matrix);
15976 if (1 == try_window (window, it.current.pos,
15977 TRY_WINDOW_CHECK_MARGINS))
15978 goto done;
15979 }
15980
15981 /* If centering point failed to make the whole line visible,
15982 put point at the top instead. That has to make the whole line
15983 visible, if it can be done. */
15984 if (centering_position == 0)
15985 goto done;
15986
15987 clear_glyph_matrix (w->desired_matrix);
15988 centering_position = 0;
15989 goto recenter;
15990 }
15991
15992 done:
15993
15994 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15995 w->start_at_line_beg = (CHARPOS (startp) == BEGV
15996 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
15997
15998 /* Display the mode line, if we must. */
15999 if ((update_mode_line
16000 /* If window not full width, must redo its mode line
16001 if (a) the window to its side is being redone and
16002 (b) we do a frame-based redisplay. This is a consequence
16003 of how inverted lines are drawn in frame-based redisplay. */
16004 || (!just_this_one_p
16005 && !FRAME_WINDOW_P (f)
16006 && !WINDOW_FULL_WIDTH_P (w))
16007 /* Line number to display. */
16008 || w->base_line_pos > 0
16009 /* Column number is displayed and different from the one displayed. */
16010 || (w->column_number_displayed != -1
16011 && (w->column_number_displayed != current_column ())))
16012 /* This means that the window has a mode line. */
16013 && (WINDOW_WANTS_MODELINE_P (w)
16014 || WINDOW_WANTS_HEADER_LINE_P (w)))
16015 {
16016 display_mode_lines (w);
16017
16018 /* If mode line height has changed, arrange for a thorough
16019 immediate redisplay using the correct mode line height. */
16020 if (WINDOW_WANTS_MODELINE_P (w)
16021 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16022 {
16023 fonts_changed_p = 1;
16024 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16025 = DESIRED_MODE_LINE_HEIGHT (w);
16026 }
16027
16028 /* If header line height has changed, arrange for a thorough
16029 immediate redisplay using the correct header line height. */
16030 if (WINDOW_WANTS_HEADER_LINE_P (w)
16031 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16032 {
16033 fonts_changed_p = 1;
16034 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16035 = DESIRED_HEADER_LINE_HEIGHT (w);
16036 }
16037
16038 if (fonts_changed_p)
16039 goto need_larger_matrices;
16040 }
16041
16042 if (!line_number_displayed && w->base_line_pos != -1)
16043 {
16044 w->base_line_pos = 0;
16045 w->base_line_number = 0;
16046 }
16047
16048 finish_menu_bars:
16049
16050 /* When we reach a frame's selected window, redo the frame's menu bar. */
16051 if (update_mode_line
16052 && EQ (FRAME_SELECTED_WINDOW (f), window))
16053 {
16054 int redisplay_menu_p = 0;
16055
16056 if (FRAME_WINDOW_P (f))
16057 {
16058 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16059 || defined (HAVE_NS) || defined (USE_GTK)
16060 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16061 #else
16062 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16063 #endif
16064 }
16065 else
16066 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16067
16068 if (redisplay_menu_p)
16069 display_menu_bar (w);
16070
16071 #ifdef HAVE_WINDOW_SYSTEM
16072 if (FRAME_WINDOW_P (f))
16073 {
16074 #if defined (USE_GTK) || defined (HAVE_NS)
16075 if (FRAME_EXTERNAL_TOOL_BAR (f))
16076 redisplay_tool_bar (f);
16077 #else
16078 if (WINDOWP (f->tool_bar_window)
16079 && (FRAME_TOOL_BAR_LINES (f) > 0
16080 || !NILP (Vauto_resize_tool_bars))
16081 && redisplay_tool_bar (f))
16082 ignore_mouse_drag_p = 1;
16083 #endif
16084 }
16085 #endif
16086 }
16087
16088 #ifdef HAVE_WINDOW_SYSTEM
16089 if (FRAME_WINDOW_P (f)
16090 && update_window_fringes (w, (just_this_one_p
16091 || (!used_current_matrix_p && !overlay_arrow_seen)
16092 || w->pseudo_window_p)))
16093 {
16094 update_begin (f);
16095 block_input ();
16096 if (draw_window_fringes (w, 1))
16097 x_draw_vertical_border (w);
16098 unblock_input ();
16099 update_end (f);
16100 }
16101 #endif /* HAVE_WINDOW_SYSTEM */
16102
16103 /* We go to this label, with fonts_changed_p set,
16104 if it is necessary to try again using larger glyph matrices.
16105 We have to redeem the scroll bar even in this case,
16106 because the loop in redisplay_internal expects that. */
16107 need_larger_matrices:
16108 ;
16109 finish_scroll_bars:
16110
16111 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16112 {
16113 /* Set the thumb's position and size. */
16114 set_vertical_scroll_bar (w);
16115
16116 /* Note that we actually used the scroll bar attached to this
16117 window, so it shouldn't be deleted at the end of redisplay. */
16118 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16119 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16120 }
16121
16122 /* Restore current_buffer and value of point in it. The window
16123 update may have changed the buffer, so first make sure `opoint'
16124 is still valid (Bug#6177). */
16125 if (CHARPOS (opoint) < BEGV)
16126 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16127 else if (CHARPOS (opoint) > ZV)
16128 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16129 else
16130 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16131
16132 set_buffer_internal_1 (old);
16133 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16134 shorter. This can be caused by log truncation in *Messages*. */
16135 if (CHARPOS (lpoint) <= ZV)
16136 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16137
16138 unbind_to (count, Qnil);
16139 }
16140
16141
16142 /* Build the complete desired matrix of WINDOW with a window start
16143 buffer position POS.
16144
16145 Value is 1 if successful. It is zero if fonts were loaded during
16146 redisplay which makes re-adjusting glyph matrices necessary, and -1
16147 if point would appear in the scroll margins.
16148 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16149 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16150 set in FLAGS.) */
16151
16152 int
16153 try_window (Lisp_Object window, struct text_pos pos, int flags)
16154 {
16155 struct window *w = XWINDOW (window);
16156 struct it it;
16157 struct glyph_row *last_text_row = NULL;
16158 struct frame *f = XFRAME (w->frame);
16159
16160 /* Make POS the new window start. */
16161 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16162
16163 /* Mark cursor position as unknown. No overlay arrow seen. */
16164 w->cursor.vpos = -1;
16165 overlay_arrow_seen = 0;
16166
16167 /* Initialize iterator and info to start at POS. */
16168 start_display (&it, w, pos);
16169
16170
16171
16172 /* Display all lines of W. */
16173 while (it.current_y < it.last_visible_y)
16174 {
16175 if (display_line (&it))
16176 last_text_row = it.glyph_row - 1;
16177 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16178 return 0;
16179 }
16180 #ifdef HAVE_XWIDGETS_xxx
16181 //currently this is needed to detect xwidget movement reliably. or probably not.
16182 printf("try_window\n");
16183 return 0;
16184 #endif
16185
16186 /* Don't let the cursor end in the scroll margins. */
16187 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16188 && !MINI_WINDOW_P (w))
16189 {
16190 int this_scroll_margin;
16191
16192 if (scroll_margin > 0)
16193 {
16194 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16195 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16196 }
16197 else
16198 this_scroll_margin = 0;
16199
16200 if ((w->cursor.y >= 0 /* not vscrolled */
16201 && w->cursor.y < this_scroll_margin
16202 && CHARPOS (pos) > BEGV
16203 && IT_CHARPOS (it) < ZV)
16204 /* rms: considering make_cursor_line_fully_visible_p here
16205 seems to give wrong results. We don't want to recenter
16206 when the last line is partly visible, we want to allow
16207 that case to be handled in the usual way. */
16208 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16209 {
16210 w->cursor.vpos = -1;
16211 clear_glyph_matrix (w->desired_matrix);
16212 return -1;
16213 }
16214 }
16215
16216 /* If bottom moved off end of frame, change mode line percentage. */
16217 if (XFASTINT (w->window_end_pos) <= 0
16218 && Z != IT_CHARPOS (it))
16219 w->update_mode_line = 1;
16220
16221 /* Set window_end_pos to the offset of the last character displayed
16222 on the window from the end of current_buffer. Set
16223 window_end_vpos to its row number. */
16224 if (last_text_row)
16225 {
16226 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16227 w->window_end_bytepos
16228 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16229 wset_window_end_pos
16230 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16231 wset_window_end_vpos
16232 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16233 eassert
16234 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16235 XFASTINT (w->window_end_vpos))));
16236 }
16237 else
16238 {
16239 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16240 wset_window_end_pos (w, make_number (Z - ZV));
16241 wset_window_end_vpos (w, make_number (0));
16242 }
16243
16244 /* But that is not valid info until redisplay finishes. */
16245 w->window_end_valid = 0;
16246 return 1;
16247 }
16248
16249
16250 \f
16251 /************************************************************************
16252 Window redisplay reusing current matrix when buffer has not changed
16253 ************************************************************************/
16254
16255 /* Try redisplay of window W showing an unchanged buffer with a
16256 different window start than the last time it was displayed by
16257 reusing its current matrix. Value is non-zero if successful.
16258 W->start is the new window start. */
16259
16260 static int
16261 try_window_reusing_current_matrix (struct window *w)
16262 {
16263 struct frame *f = XFRAME (w->frame);
16264 struct glyph_row *bottom_row;
16265 struct it it;
16266 struct run run;
16267 struct text_pos start, new_start;
16268 int nrows_scrolled, i;
16269 struct glyph_row *last_text_row;
16270 struct glyph_row *last_reused_text_row;
16271 struct glyph_row *start_row;
16272 int start_vpos, min_y, max_y;
16273
16274 #ifdef GLYPH_DEBUG
16275 if (inhibit_try_window_reusing)
16276 return 0;
16277 #endif
16278
16279 #ifdef HAVE_XWIDGETS_xxx
16280 //currently this is needed to detect xwidget movement reliably. or probably not.
16281 printf("try_window_reusing_current_matrix\n");
16282 return 0;
16283 #endif
16284
16285
16286 if (/* This function doesn't handle terminal frames. */
16287 !FRAME_WINDOW_P (f)
16288 /* Don't try to reuse the display if windows have been split
16289 or such. */
16290 || windows_or_buffers_changed
16291 || cursor_type_changed)
16292 return 0;
16293
16294 /* Can't do this if region may have changed. */
16295 if (markpos_of_region () >= 0
16296 || w->region_showing
16297 || !NILP (Vshow_trailing_whitespace))
16298 return 0;
16299
16300 /* If top-line visibility has changed, give up. */
16301 if (WINDOW_WANTS_HEADER_LINE_P (w)
16302 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16303 return 0;
16304
16305 /* Give up if old or new display is scrolled vertically. We could
16306 make this function handle this, but right now it doesn't. */
16307 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16308 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16309 return 0;
16310
16311 /* The variable new_start now holds the new window start. The old
16312 start `start' can be determined from the current matrix. */
16313 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16314 start = start_row->minpos;
16315 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16316
16317 /* Clear the desired matrix for the display below. */
16318 clear_glyph_matrix (w->desired_matrix);
16319
16320 if (CHARPOS (new_start) <= CHARPOS (start))
16321 {
16322 /* Don't use this method if the display starts with an ellipsis
16323 displayed for invisible text. It's not easy to handle that case
16324 below, and it's certainly not worth the effort since this is
16325 not a frequent case. */
16326 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16327 return 0;
16328
16329 IF_DEBUG (debug_method_add (w, "twu1"));
16330
16331 /* Display up to a row that can be reused. The variable
16332 last_text_row is set to the last row displayed that displays
16333 text. Note that it.vpos == 0 if or if not there is a
16334 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16335 start_display (&it, w, new_start);
16336 w->cursor.vpos = -1;
16337 last_text_row = last_reused_text_row = NULL;
16338
16339 while (it.current_y < it.last_visible_y
16340 && !fonts_changed_p)
16341 {
16342 /* If we have reached into the characters in the START row,
16343 that means the line boundaries have changed. So we
16344 can't start copying with the row START. Maybe it will
16345 work to start copying with the following row. */
16346 while (IT_CHARPOS (it) > CHARPOS (start))
16347 {
16348 /* Advance to the next row as the "start". */
16349 start_row++;
16350 start = start_row->minpos;
16351 /* If there are no more rows to try, or just one, give up. */
16352 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16353 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16354 || CHARPOS (start) == ZV)
16355 {
16356 clear_glyph_matrix (w->desired_matrix);
16357 return 0;
16358 }
16359
16360 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16361 }
16362 /* If we have reached alignment, we can copy the rest of the
16363 rows. */
16364 if (IT_CHARPOS (it) == CHARPOS (start)
16365 /* Don't accept "alignment" inside a display vector,
16366 since start_row could have started in the middle of
16367 that same display vector (thus their character
16368 positions match), and we have no way of telling if
16369 that is the case. */
16370 && it.current.dpvec_index < 0)
16371 break;
16372
16373 if (display_line (&it))
16374 last_text_row = it.glyph_row - 1;
16375
16376 }
16377
16378 /* A value of current_y < last_visible_y means that we stopped
16379 at the previous window start, which in turn means that we
16380 have at least one reusable row. */
16381 if (it.current_y < it.last_visible_y)
16382 {
16383 struct glyph_row *row;
16384
16385 /* IT.vpos always starts from 0; it counts text lines. */
16386 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16387
16388 /* Find PT if not already found in the lines displayed. */
16389 if (w->cursor.vpos < 0)
16390 {
16391 int dy = it.current_y - start_row->y;
16392
16393 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16394 row = row_containing_pos (w, PT, row, NULL, dy);
16395 if (row)
16396 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16397 dy, nrows_scrolled);
16398 else
16399 {
16400 clear_glyph_matrix (w->desired_matrix);
16401 return 0;
16402 }
16403 }
16404
16405 /* Scroll the display. Do it before the current matrix is
16406 changed. The problem here is that update has not yet
16407 run, i.e. part of the current matrix is not up to date.
16408 scroll_run_hook will clear the cursor, and use the
16409 current matrix to get the height of the row the cursor is
16410 in. */
16411 run.current_y = start_row->y;
16412 run.desired_y = it.current_y;
16413 run.height = it.last_visible_y - it.current_y;
16414
16415 if (run.height > 0 && run.current_y != run.desired_y)
16416 {
16417 update_begin (f);
16418 FRAME_RIF (f)->update_window_begin_hook (w);
16419 FRAME_RIF (f)->clear_window_mouse_face (w);
16420 FRAME_RIF (f)->scroll_run_hook (w, &run);
16421 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16422 update_end (f);
16423 }
16424
16425 /* Shift current matrix down by nrows_scrolled lines. */
16426 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16427 rotate_matrix (w->current_matrix,
16428 start_vpos,
16429 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16430 nrows_scrolled);
16431
16432 /* Disable lines that must be updated. */
16433 for (i = 0; i < nrows_scrolled; ++i)
16434 (start_row + i)->enabled_p = 0;
16435
16436 /* Re-compute Y positions. */
16437 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16438 max_y = it.last_visible_y;
16439 for (row = start_row + nrows_scrolled;
16440 row < bottom_row;
16441 ++row)
16442 {
16443 row->y = it.current_y;
16444 row->visible_height = row->height;
16445
16446 if (row->y < min_y)
16447 row->visible_height -= min_y - row->y;
16448 if (row->y + row->height > max_y)
16449 row->visible_height -= row->y + row->height - max_y;
16450 if (row->fringe_bitmap_periodic_p)
16451 row->redraw_fringe_bitmaps_p = 1;
16452
16453 it.current_y += row->height;
16454
16455 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16456 last_reused_text_row = row;
16457 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16458 break;
16459 }
16460
16461 /* Disable lines in the current matrix which are now
16462 below the window. */
16463 for (++row; row < bottom_row; ++row)
16464 row->enabled_p = row->mode_line_p = 0;
16465 }
16466
16467 /* Update window_end_pos etc.; last_reused_text_row is the last
16468 reused row from the current matrix containing text, if any.
16469 The value of last_text_row is the last displayed line
16470 containing text. */
16471 if (last_reused_text_row)
16472 {
16473 w->window_end_bytepos
16474 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16475 wset_window_end_pos
16476 (w, make_number (Z
16477 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16478 wset_window_end_vpos
16479 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16480 w->current_matrix)));
16481 }
16482 else if (last_text_row)
16483 {
16484 w->window_end_bytepos
16485 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16486 wset_window_end_pos
16487 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16488 wset_window_end_vpos
16489 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16490 w->desired_matrix)));
16491 }
16492 else
16493 {
16494 /* This window must be completely empty. */
16495 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16496 wset_window_end_pos (w, make_number (Z - ZV));
16497 wset_window_end_vpos (w, make_number (0));
16498 }
16499 w->window_end_valid = 0;
16500
16501 /* Update hint: don't try scrolling again in update_window. */
16502 w->desired_matrix->no_scrolling_p = 1;
16503
16504 #ifdef GLYPH_DEBUG
16505 debug_method_add (w, "try_window_reusing_current_matrix 1");
16506 #endif
16507 return 1;
16508 }
16509 else if (CHARPOS (new_start) > CHARPOS (start))
16510 {
16511 struct glyph_row *pt_row, *row;
16512 struct glyph_row *first_reusable_row;
16513 struct glyph_row *first_row_to_display;
16514 int dy;
16515 int yb = window_text_bottom_y (w);
16516
16517 /* Find the row starting at new_start, if there is one. Don't
16518 reuse a partially visible line at the end. */
16519 first_reusable_row = start_row;
16520 while (first_reusable_row->enabled_p
16521 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16522 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16523 < CHARPOS (new_start)))
16524 ++first_reusable_row;
16525
16526 /* Give up if there is no row to reuse. */
16527 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16528 || !first_reusable_row->enabled_p
16529 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16530 != CHARPOS (new_start)))
16531 return 0;
16532
16533 /* We can reuse fully visible rows beginning with
16534 first_reusable_row to the end of the window. Set
16535 first_row_to_display to the first row that cannot be reused.
16536 Set pt_row to the row containing point, if there is any. */
16537 pt_row = NULL;
16538 for (first_row_to_display = first_reusable_row;
16539 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16540 ++first_row_to_display)
16541 {
16542 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16543 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16544 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16545 && first_row_to_display->ends_at_zv_p
16546 && pt_row == NULL)))
16547 pt_row = first_row_to_display;
16548 }
16549
16550 /* Start displaying at the start of first_row_to_display. */
16551 eassert (first_row_to_display->y < yb);
16552 init_to_row_start (&it, w, first_row_to_display);
16553
16554 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16555 - start_vpos);
16556 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16557 - nrows_scrolled);
16558 it.current_y = (first_row_to_display->y - first_reusable_row->y
16559 + WINDOW_HEADER_LINE_HEIGHT (w));
16560
16561 /* Display lines beginning with first_row_to_display in the
16562 desired matrix. Set last_text_row to the last row displayed
16563 that displays text. */
16564 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16565 if (pt_row == NULL)
16566 w->cursor.vpos = -1;
16567 last_text_row = NULL;
16568 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16569 if (display_line (&it))
16570 last_text_row = it.glyph_row - 1;
16571
16572 /* If point is in a reused row, adjust y and vpos of the cursor
16573 position. */
16574 if (pt_row)
16575 {
16576 w->cursor.vpos -= nrows_scrolled;
16577 w->cursor.y -= first_reusable_row->y - start_row->y;
16578 }
16579
16580 /* Give up if point isn't in a row displayed or reused. (This
16581 also handles the case where w->cursor.vpos < nrows_scrolled
16582 after the calls to display_line, which can happen with scroll
16583 margins. See bug#1295.) */
16584 if (w->cursor.vpos < 0)
16585 {
16586 clear_glyph_matrix (w->desired_matrix);
16587 return 0;
16588 }
16589
16590 /* Scroll the display. */
16591 run.current_y = first_reusable_row->y;
16592 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16593 run.height = it.last_visible_y - run.current_y;
16594 dy = run.current_y - run.desired_y;
16595
16596 if (run.height)
16597 {
16598 update_begin (f);
16599 FRAME_RIF (f)->update_window_begin_hook (w);
16600 FRAME_RIF (f)->clear_window_mouse_face (w);
16601 FRAME_RIF (f)->scroll_run_hook (w, &run);
16602 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16603 update_end (f);
16604 }
16605
16606 /* Adjust Y positions of reused rows. */
16607 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16608 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16609 max_y = it.last_visible_y;
16610 for (row = first_reusable_row; row < first_row_to_display; ++row)
16611 {
16612 row->y -= dy;
16613 row->visible_height = row->height;
16614 if (row->y < min_y)
16615 row->visible_height -= min_y - row->y;
16616 if (row->y + row->height > max_y)
16617 row->visible_height -= row->y + row->height - max_y;
16618 if (row->fringe_bitmap_periodic_p)
16619 row->redraw_fringe_bitmaps_p = 1;
16620 }
16621
16622 /* Scroll the current matrix. */
16623 eassert (nrows_scrolled > 0);
16624 rotate_matrix (w->current_matrix,
16625 start_vpos,
16626 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16627 -nrows_scrolled);
16628
16629 /* Disable rows not reused. */
16630 for (row -= nrows_scrolled; row < bottom_row; ++row)
16631 row->enabled_p = 0;
16632
16633 /* Point may have moved to a different line, so we cannot assume that
16634 the previous cursor position is valid; locate the correct row. */
16635 if (pt_row)
16636 {
16637 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16638 row < bottom_row
16639 && PT >= MATRIX_ROW_END_CHARPOS (row)
16640 && !row->ends_at_zv_p;
16641 row++)
16642 {
16643 w->cursor.vpos++;
16644 w->cursor.y = row->y;
16645 }
16646 if (row < bottom_row)
16647 {
16648 /* Can't simply scan the row for point with
16649 bidi-reordered glyph rows. Let set_cursor_from_row
16650 figure out where to put the cursor, and if it fails,
16651 give up. */
16652 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16653 {
16654 if (!set_cursor_from_row (w, row, w->current_matrix,
16655 0, 0, 0, 0))
16656 {
16657 clear_glyph_matrix (w->desired_matrix);
16658 return 0;
16659 }
16660 }
16661 else
16662 {
16663 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16664 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16665
16666 for (; glyph < end
16667 && (!BUFFERP (glyph->object)
16668 || glyph->charpos < PT);
16669 glyph++)
16670 {
16671 w->cursor.hpos++;
16672 w->cursor.x += glyph->pixel_width;
16673 }
16674 }
16675 }
16676 }
16677
16678 /* Adjust window end. A null value of last_text_row means that
16679 the window end is in reused rows which in turn means that
16680 only its vpos can have changed. */
16681 if (last_text_row)
16682 {
16683 w->window_end_bytepos
16684 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16685 wset_window_end_pos
16686 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16687 wset_window_end_vpos
16688 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16689 w->desired_matrix)));
16690 }
16691 else
16692 {
16693 wset_window_end_vpos
16694 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16695 }
16696
16697 w->window_end_valid = 0;
16698 w->desired_matrix->no_scrolling_p = 1;
16699
16700 #ifdef GLYPH_DEBUG
16701 debug_method_add (w, "try_window_reusing_current_matrix 2");
16702 #endif
16703 return 1;
16704 }
16705
16706 return 0;
16707 }
16708
16709
16710 \f
16711 /************************************************************************
16712 Window redisplay reusing current matrix when buffer has changed
16713 ************************************************************************/
16714
16715 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16716 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16717 ptrdiff_t *, ptrdiff_t *);
16718 static struct glyph_row *
16719 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16720 struct glyph_row *);
16721
16722
16723 /* Return the last row in MATRIX displaying text. If row START is
16724 non-null, start searching with that row. IT gives the dimensions
16725 of the display. Value is null if matrix is empty; otherwise it is
16726 a pointer to the row found. */
16727
16728 static struct glyph_row *
16729 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16730 struct glyph_row *start)
16731 {
16732 struct glyph_row *row, *row_found;
16733
16734 /* Set row_found to the last row in IT->w's current matrix
16735 displaying text. The loop looks funny but think of partially
16736 visible lines. */
16737 row_found = NULL;
16738 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16739 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16740 {
16741 eassert (row->enabled_p);
16742 row_found = row;
16743 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16744 break;
16745 ++row;
16746 }
16747
16748 return row_found;
16749 }
16750
16751
16752 /* Return the last row in the current matrix of W that is not affected
16753 by changes at the start of current_buffer that occurred since W's
16754 current matrix was built. Value is null if no such row exists.
16755
16756 BEG_UNCHANGED us the number of characters unchanged at the start of
16757 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16758 first changed character in current_buffer. Characters at positions <
16759 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16760 when the current matrix was built. */
16761
16762 static struct glyph_row *
16763 find_last_unchanged_at_beg_row (struct window *w)
16764 {
16765 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16766 struct glyph_row *row;
16767 struct glyph_row *row_found = NULL;
16768 int yb = window_text_bottom_y (w);
16769
16770 /* Find the last row displaying unchanged text. */
16771 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16772 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16773 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16774 ++row)
16775 {
16776 if (/* If row ends before first_changed_pos, it is unchanged,
16777 except in some case. */
16778 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16779 /* When row ends in ZV and we write at ZV it is not
16780 unchanged. */
16781 && !row->ends_at_zv_p
16782 /* When first_changed_pos is the end of a continued line,
16783 row is not unchanged because it may be no longer
16784 continued. */
16785 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16786 && (row->continued_p
16787 || row->exact_window_width_line_p))
16788 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16789 needs to be recomputed, so don't consider this row as
16790 unchanged. This happens when the last line was
16791 bidi-reordered and was killed immediately before this
16792 redisplay cycle. In that case, ROW->end stores the
16793 buffer position of the first visual-order character of
16794 the killed text, which is now beyond ZV. */
16795 && CHARPOS (row->end.pos) <= ZV)
16796 row_found = row;
16797
16798 /* Stop if last visible row. */
16799 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16800 break;
16801 }
16802
16803 return row_found;
16804 }
16805
16806
16807 /* Find the first glyph row in the current matrix of W that is not
16808 affected by changes at the end of current_buffer since the
16809 time W's current matrix was built.
16810
16811 Return in *DELTA the number of chars by which buffer positions in
16812 unchanged text at the end of current_buffer must be adjusted.
16813
16814 Return in *DELTA_BYTES the corresponding number of bytes.
16815
16816 Value is null if no such row exists, i.e. all rows are affected by
16817 changes. */
16818
16819 static struct glyph_row *
16820 find_first_unchanged_at_end_row (struct window *w,
16821 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16822 {
16823 struct glyph_row *row;
16824 struct glyph_row *row_found = NULL;
16825
16826 *delta = *delta_bytes = 0;
16827
16828 /* Display must not have been paused, otherwise the current matrix
16829 is not up to date. */
16830 eassert (w->window_end_valid);
16831
16832 /* A value of window_end_pos >= END_UNCHANGED means that the window
16833 end is in the range of changed text. If so, there is no
16834 unchanged row at the end of W's current matrix. */
16835 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16836 return NULL;
16837
16838 /* Set row to the last row in W's current matrix displaying text. */
16839 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16840
16841 /* If matrix is entirely empty, no unchanged row exists. */
16842 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16843 {
16844 /* The value of row is the last glyph row in the matrix having a
16845 meaningful buffer position in it. The end position of row
16846 corresponds to window_end_pos. This allows us to translate
16847 buffer positions in the current matrix to current buffer
16848 positions for characters not in changed text. */
16849 ptrdiff_t Z_old =
16850 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16851 ptrdiff_t Z_BYTE_old =
16852 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16853 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16854 struct glyph_row *first_text_row
16855 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16856
16857 *delta = Z - Z_old;
16858 *delta_bytes = Z_BYTE - Z_BYTE_old;
16859
16860 /* Set last_unchanged_pos to the buffer position of the last
16861 character in the buffer that has not been changed. Z is the
16862 index + 1 of the last character in current_buffer, i.e. by
16863 subtracting END_UNCHANGED we get the index of the last
16864 unchanged character, and we have to add BEG to get its buffer
16865 position. */
16866 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16867 last_unchanged_pos_old = last_unchanged_pos - *delta;
16868
16869 /* Search backward from ROW for a row displaying a line that
16870 starts at a minimum position >= last_unchanged_pos_old. */
16871 for (; row > first_text_row; --row)
16872 {
16873 /* This used to abort, but it can happen.
16874 It is ok to just stop the search instead here. KFS. */
16875 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16876 break;
16877
16878 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16879 row_found = row;
16880 }
16881 }
16882
16883 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16884
16885 return row_found;
16886 }
16887
16888
16889 /* Make sure that glyph rows in the current matrix of window W
16890 reference the same glyph memory as corresponding rows in the
16891 frame's frame matrix. This function is called after scrolling W's
16892 current matrix on a terminal frame in try_window_id and
16893 try_window_reusing_current_matrix. */
16894
16895 static void
16896 sync_frame_with_window_matrix_rows (struct window *w)
16897 {
16898 struct frame *f = XFRAME (w->frame);
16899 struct glyph_row *window_row, *window_row_end, *frame_row;
16900
16901 /* Preconditions: W must be a leaf window and full-width. Its frame
16902 must have a frame matrix. */
16903 eassert (BUFFERP (w->contents));
16904 eassert (WINDOW_FULL_WIDTH_P (w));
16905 eassert (!FRAME_WINDOW_P (f));
16906
16907 /* If W is a full-width window, glyph pointers in W's current matrix
16908 have, by definition, to be the same as glyph pointers in the
16909 corresponding frame matrix. Note that frame matrices have no
16910 marginal areas (see build_frame_matrix). */
16911 window_row = w->current_matrix->rows;
16912 window_row_end = window_row + w->current_matrix->nrows;
16913 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16914 while (window_row < window_row_end)
16915 {
16916 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16917 struct glyph *end = window_row->glyphs[LAST_AREA];
16918
16919 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16920 frame_row->glyphs[TEXT_AREA] = start;
16921 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16922 frame_row->glyphs[LAST_AREA] = end;
16923
16924 /* Disable frame rows whose corresponding window rows have
16925 been disabled in try_window_id. */
16926 if (!window_row->enabled_p)
16927 frame_row->enabled_p = 0;
16928
16929 ++window_row, ++frame_row;
16930 }
16931 }
16932
16933
16934 /* Find the glyph row in window W containing CHARPOS. Consider all
16935 rows between START and END (not inclusive). END null means search
16936 all rows to the end of the display area of W. Value is the row
16937 containing CHARPOS or null. */
16938
16939 struct glyph_row *
16940 row_containing_pos (struct window *w, ptrdiff_t charpos,
16941 struct glyph_row *start, struct glyph_row *end, int dy)
16942 {
16943 struct glyph_row *row = start;
16944 struct glyph_row *best_row = NULL;
16945 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16946 int last_y;
16947
16948 /* If we happen to start on a header-line, skip that. */
16949 if (row->mode_line_p)
16950 ++row;
16951
16952 if ((end && row >= end) || !row->enabled_p)
16953 return NULL;
16954
16955 last_y = window_text_bottom_y (w) - dy;
16956
16957 while (1)
16958 {
16959 /* Give up if we have gone too far. */
16960 if (end && row >= end)
16961 return NULL;
16962 /* This formerly returned if they were equal.
16963 I think that both quantities are of a "last plus one" type;
16964 if so, when they are equal, the row is within the screen. -- rms. */
16965 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16966 return NULL;
16967
16968 /* If it is in this row, return this row. */
16969 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16970 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16971 /* The end position of a row equals the start
16972 position of the next row. If CHARPOS is there, we
16973 would rather display it in the next line, except
16974 when this line ends in ZV. */
16975 && !row->ends_at_zv_p
16976 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16977 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16978 {
16979 struct glyph *g;
16980
16981 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16982 || (!best_row && !row->continued_p))
16983 return row;
16984 /* In bidi-reordered rows, there could be several rows
16985 occluding point, all of them belonging to the same
16986 continued line. We need to find the row which fits
16987 CHARPOS the best. */
16988 for (g = row->glyphs[TEXT_AREA];
16989 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16990 g++)
16991 {
16992 if (!STRINGP (g->object))
16993 {
16994 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16995 {
16996 mindif = eabs (g->charpos - charpos);
16997 best_row = row;
16998 /* Exact match always wins. */
16999 if (mindif == 0)
17000 return best_row;
17001 }
17002 }
17003 }
17004 }
17005 else if (best_row && !row->continued_p)
17006 return best_row;
17007 ++row;
17008 }
17009 }
17010
17011
17012 /* Try to redisplay window W by reusing its existing display. W's
17013 current matrix must be up to date when this function is called,
17014 i.e. window_end_valid must be nonzero.
17015
17016 Value is
17017
17018 1 if display has been updated
17019 0 if otherwise unsuccessful
17020 -1 if redisplay with same window start is known not to succeed
17021
17022 The following steps are performed:
17023
17024 1. Find the last row in the current matrix of W that is not
17025 affected by changes at the start of current_buffer. If no such row
17026 is found, give up.
17027
17028 2. Find the first row in W's current matrix that is not affected by
17029 changes at the end of current_buffer. Maybe there is no such row.
17030
17031 3. Display lines beginning with the row + 1 found in step 1 to the
17032 row found in step 2 or, if step 2 didn't find a row, to the end of
17033 the window.
17034
17035 4. If cursor is not known to appear on the window, give up.
17036
17037 5. If display stopped at the row found in step 2, scroll the
17038 display and current matrix as needed.
17039
17040 6. Maybe display some lines at the end of W, if we must. This can
17041 happen under various circumstances, like a partially visible line
17042 becoming fully visible, or because newly displayed lines are displayed
17043 in smaller font sizes.
17044
17045 7. Update W's window end information. */
17046
17047 static int
17048 try_window_id (struct window *w)
17049 {
17050 struct frame *f = XFRAME (w->frame);
17051 struct glyph_matrix *current_matrix = w->current_matrix;
17052 struct glyph_matrix *desired_matrix = w->desired_matrix;
17053 struct glyph_row *last_unchanged_at_beg_row;
17054 struct glyph_row *first_unchanged_at_end_row;
17055 struct glyph_row *row;
17056 struct glyph_row *bottom_row;
17057 int bottom_vpos;
17058 struct it it;
17059 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17060 int dvpos, dy;
17061 struct text_pos start_pos;
17062 struct run run;
17063 int first_unchanged_at_end_vpos = 0;
17064 struct glyph_row *last_text_row, *last_text_row_at_end;
17065 struct text_pos start;
17066 ptrdiff_t first_changed_charpos, last_changed_charpos;
17067
17068 #ifdef GLYPH_DEBUG
17069 if (inhibit_try_window_id)
17070 return 0;
17071 #endif
17072
17073 #ifdef HAVE_XWIDGETS_xxx
17074 //maybe needed for proper xwidget movement
17075 printf("try_window_id\n");
17076 return -1;
17077 #endif
17078
17079
17080 /* This is handy for debugging. */
17081 #if 0
17082 #define GIVE_UP(X) \
17083 do { \
17084 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17085 return 0; \
17086 } while (0)
17087 #else
17088 #define GIVE_UP(X) return 0
17089 #endif
17090
17091 SET_TEXT_POS_FROM_MARKER (start, w->start);
17092
17093 /* Don't use this for mini-windows because these can show
17094 messages and mini-buffers, and we don't handle that here. */
17095 if (MINI_WINDOW_P (w))
17096 GIVE_UP (1);
17097
17098 /* This flag is used to prevent redisplay optimizations. */
17099 if (windows_or_buffers_changed || cursor_type_changed)
17100 GIVE_UP (2);
17101
17102 /* Verify that narrowing has not changed.
17103 Also verify that we were not told to prevent redisplay optimizations.
17104 It would be nice to further
17105 reduce the number of cases where this prevents try_window_id. */
17106 if (current_buffer->clip_changed
17107 || current_buffer->prevent_redisplay_optimizations_p)
17108 GIVE_UP (3);
17109
17110 /* Window must either use window-based redisplay or be full width. */
17111 if (!FRAME_WINDOW_P (f)
17112 && (!FRAME_LINE_INS_DEL_OK (f)
17113 || !WINDOW_FULL_WIDTH_P (w)))
17114 GIVE_UP (4);
17115
17116 /* Give up if point is known NOT to appear in W. */
17117 if (PT < CHARPOS (start))
17118 GIVE_UP (5);
17119
17120 /* Another way to prevent redisplay optimizations. */
17121 if (w->last_modified == 0)
17122 GIVE_UP (6);
17123
17124 /* Verify that window is not hscrolled. */
17125 if (w->hscroll != 0)
17126 GIVE_UP (7);
17127
17128 /* Verify that display wasn't paused. */
17129 if (!w->window_end_valid)
17130 GIVE_UP (8);
17131
17132 /* Can't use this if highlighting a region because a cursor movement
17133 will do more than just set the cursor. */
17134 if (markpos_of_region () >= 0)
17135 GIVE_UP (9);
17136
17137 /* Likewise if highlighting trailing whitespace. */
17138 if (!NILP (Vshow_trailing_whitespace))
17139 GIVE_UP (11);
17140
17141 /* Likewise if showing a region. */
17142 if (w->region_showing)
17143 GIVE_UP (10);
17144
17145 /* Can't use this if overlay arrow position and/or string have
17146 changed. */
17147 if (overlay_arrows_changed_p ())
17148 GIVE_UP (12);
17149
17150 /* When word-wrap is on, adding a space to the first word of a
17151 wrapped line can change the wrap position, altering the line
17152 above it. It might be worthwhile to handle this more
17153 intelligently, but for now just redisplay from scratch. */
17154 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17155 GIVE_UP (21);
17156
17157 /* Under bidi reordering, adding or deleting a character in the
17158 beginning of a paragraph, before the first strong directional
17159 character, can change the base direction of the paragraph (unless
17160 the buffer specifies a fixed paragraph direction), which will
17161 require to redisplay the whole paragraph. It might be worthwhile
17162 to find the paragraph limits and widen the range of redisplayed
17163 lines to that, but for now just give up this optimization and
17164 redisplay from scratch. */
17165 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17166 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17167 GIVE_UP (22);
17168
17169 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17170 only if buffer has really changed. The reason is that the gap is
17171 initially at Z for freshly visited files. The code below would
17172 set end_unchanged to 0 in that case. */
17173 if (MODIFF > SAVE_MODIFF
17174 /* This seems to happen sometimes after saving a buffer. */
17175 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17176 {
17177 if (GPT - BEG < BEG_UNCHANGED)
17178 BEG_UNCHANGED = GPT - BEG;
17179 if (Z - GPT < END_UNCHANGED)
17180 END_UNCHANGED = Z - GPT;
17181 }
17182
17183 /* The position of the first and last character that has been changed. */
17184 first_changed_charpos = BEG + BEG_UNCHANGED;
17185 last_changed_charpos = Z - END_UNCHANGED;
17186
17187 /* If window starts after a line end, and the last change is in
17188 front of that newline, then changes don't affect the display.
17189 This case happens with stealth-fontification. Note that although
17190 the display is unchanged, glyph positions in the matrix have to
17191 be adjusted, of course. */
17192 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17193 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17194 && ((last_changed_charpos < CHARPOS (start)
17195 && CHARPOS (start) == BEGV)
17196 || (last_changed_charpos < CHARPOS (start) - 1
17197 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17198 {
17199 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17200 struct glyph_row *r0;
17201
17202 /* Compute how many chars/bytes have been added to or removed
17203 from the buffer. */
17204 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17205 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17206 Z_delta = Z - Z_old;
17207 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17208
17209 /* Give up if PT is not in the window. Note that it already has
17210 been checked at the start of try_window_id that PT is not in
17211 front of the window start. */
17212 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17213 GIVE_UP (13);
17214
17215 /* If window start is unchanged, we can reuse the whole matrix
17216 as is, after adjusting glyph positions. No need to compute
17217 the window end again, since its offset from Z hasn't changed. */
17218 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17219 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17220 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17221 /* PT must not be in a partially visible line. */
17222 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17223 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17224 {
17225 /* Adjust positions in the glyph matrix. */
17226 if (Z_delta || Z_delta_bytes)
17227 {
17228 struct glyph_row *r1
17229 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17230 increment_matrix_positions (w->current_matrix,
17231 MATRIX_ROW_VPOS (r0, current_matrix),
17232 MATRIX_ROW_VPOS (r1, current_matrix),
17233 Z_delta, Z_delta_bytes);
17234 }
17235
17236 /* Set the cursor. */
17237 row = row_containing_pos (w, PT, r0, NULL, 0);
17238 if (row)
17239 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17240 else
17241 emacs_abort ();
17242 return 1;
17243 }
17244 }
17245
17246 /* Handle the case that changes are all below what is displayed in
17247 the window, and that PT is in the window. This shortcut cannot
17248 be taken if ZV is visible in the window, and text has been added
17249 there that is visible in the window. */
17250 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17251 /* ZV is not visible in the window, or there are no
17252 changes at ZV, actually. */
17253 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17254 || first_changed_charpos == last_changed_charpos))
17255 {
17256 struct glyph_row *r0;
17257
17258 /* Give up if PT is not in the window. Note that it already has
17259 been checked at the start of try_window_id that PT is not in
17260 front of the window start. */
17261 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17262 GIVE_UP (14);
17263
17264 /* If window start is unchanged, we can reuse the whole matrix
17265 as is, without changing glyph positions since no text has
17266 been added/removed in front of the window end. */
17267 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17268 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17269 /* PT must not be in a partially visible line. */
17270 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17271 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17272 {
17273 /* We have to compute the window end anew since text
17274 could have been added/removed after it. */
17275 wset_window_end_pos
17276 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17277 w->window_end_bytepos
17278 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17279
17280 /* Set the cursor. */
17281 row = row_containing_pos (w, PT, r0, NULL, 0);
17282 if (row)
17283 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17284 else
17285 emacs_abort ();
17286 return 2;
17287 }
17288 }
17289
17290 /* Give up if window start is in the changed area.
17291
17292 The condition used to read
17293
17294 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17295
17296 but why that was tested escapes me at the moment. */
17297 if (CHARPOS (start) >= first_changed_charpos
17298 && CHARPOS (start) <= last_changed_charpos)
17299 GIVE_UP (15);
17300
17301 /* Check that window start agrees with the start of the first glyph
17302 row in its current matrix. Check this after we know the window
17303 start is not in changed text, otherwise positions would not be
17304 comparable. */
17305 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17306 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17307 GIVE_UP (16);
17308
17309 /* Give up if the window ends in strings. Overlay strings
17310 at the end are difficult to handle, so don't try. */
17311 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17312 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17313 GIVE_UP (20);
17314
17315 /* Compute the position at which we have to start displaying new
17316 lines. Some of the lines at the top of the window might be
17317 reusable because they are not displaying changed text. Find the
17318 last row in W's current matrix not affected by changes at the
17319 start of current_buffer. Value is null if changes start in the
17320 first line of window. */
17321 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17322 if (last_unchanged_at_beg_row)
17323 {
17324 /* Avoid starting to display in the middle of a character, a TAB
17325 for instance. This is easier than to set up the iterator
17326 exactly, and it's not a frequent case, so the additional
17327 effort wouldn't really pay off. */
17328 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17329 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17330 && last_unchanged_at_beg_row > w->current_matrix->rows)
17331 --last_unchanged_at_beg_row;
17332
17333 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17334 GIVE_UP (17);
17335
17336 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17337 GIVE_UP (18);
17338 start_pos = it.current.pos;
17339
17340 /* Start displaying new lines in the desired matrix at the same
17341 vpos we would use in the current matrix, i.e. below
17342 last_unchanged_at_beg_row. */
17343 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17344 current_matrix);
17345 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17346 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17347
17348 eassert (it.hpos == 0 && it.current_x == 0);
17349 }
17350 else
17351 {
17352 /* There are no reusable lines at the start of the window.
17353 Start displaying in the first text line. */
17354 start_display (&it, w, start);
17355 it.vpos = it.first_vpos;
17356 start_pos = it.current.pos;
17357 }
17358
17359 /* Find the first row that is not affected by changes at the end of
17360 the buffer. Value will be null if there is no unchanged row, in
17361 which case we must redisplay to the end of the window. delta
17362 will be set to the value by which buffer positions beginning with
17363 first_unchanged_at_end_row have to be adjusted due to text
17364 changes. */
17365 first_unchanged_at_end_row
17366 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17367 IF_DEBUG (debug_delta = delta);
17368 IF_DEBUG (debug_delta_bytes = delta_bytes);
17369
17370 /* Set stop_pos to the buffer position up to which we will have to
17371 display new lines. If first_unchanged_at_end_row != NULL, this
17372 is the buffer position of the start of the line displayed in that
17373 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17374 that we don't stop at a buffer position. */
17375 stop_pos = 0;
17376 if (first_unchanged_at_end_row)
17377 {
17378 eassert (last_unchanged_at_beg_row == NULL
17379 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17380
17381 /* If this is a continuation line, move forward to the next one
17382 that isn't. Changes in lines above affect this line.
17383 Caution: this may move first_unchanged_at_end_row to a row
17384 not displaying text. */
17385 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17386 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17387 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17388 < it.last_visible_y))
17389 ++first_unchanged_at_end_row;
17390
17391 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17392 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17393 >= it.last_visible_y))
17394 first_unchanged_at_end_row = NULL;
17395 else
17396 {
17397 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17398 + delta);
17399 first_unchanged_at_end_vpos
17400 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17401 eassert (stop_pos >= Z - END_UNCHANGED);
17402 }
17403 }
17404 else if (last_unchanged_at_beg_row == NULL)
17405 GIVE_UP (19);
17406
17407
17408 #ifdef GLYPH_DEBUG
17409
17410 /* Either there is no unchanged row at the end, or the one we have
17411 now displays text. This is a necessary condition for the window
17412 end pos calculation at the end of this function. */
17413 eassert (first_unchanged_at_end_row == NULL
17414 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17415
17416 debug_last_unchanged_at_beg_vpos
17417 = (last_unchanged_at_beg_row
17418 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17419 : -1);
17420 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17421
17422 #endif /* GLYPH_DEBUG */
17423
17424
17425 /* Display new lines. Set last_text_row to the last new line
17426 displayed which has text on it, i.e. might end up as being the
17427 line where the window_end_vpos is. */
17428 w->cursor.vpos = -1;
17429 last_text_row = NULL;
17430 overlay_arrow_seen = 0;
17431 while (it.current_y < it.last_visible_y
17432 && !fonts_changed_p
17433 && (first_unchanged_at_end_row == NULL
17434 || IT_CHARPOS (it) < stop_pos))
17435 {
17436 if (display_line (&it))
17437 last_text_row = it.glyph_row - 1;
17438 }
17439
17440 if (fonts_changed_p)
17441 return -1;
17442
17443
17444 /* Compute differences in buffer positions, y-positions etc. for
17445 lines reused at the bottom of the window. Compute what we can
17446 scroll. */
17447 if (first_unchanged_at_end_row
17448 /* No lines reused because we displayed everything up to the
17449 bottom of the window. */
17450 && it.current_y < it.last_visible_y)
17451 {
17452 dvpos = (it.vpos
17453 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17454 current_matrix));
17455 dy = it.current_y - first_unchanged_at_end_row->y;
17456 run.current_y = first_unchanged_at_end_row->y;
17457 run.desired_y = run.current_y + dy;
17458 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17459 }
17460 else
17461 {
17462 delta = delta_bytes = dvpos = dy
17463 = run.current_y = run.desired_y = run.height = 0;
17464 first_unchanged_at_end_row = NULL;
17465 }
17466 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17467
17468
17469 /* Find the cursor if not already found. We have to decide whether
17470 PT will appear on this window (it sometimes doesn't, but this is
17471 not a very frequent case.) This decision has to be made before
17472 the current matrix is altered. A value of cursor.vpos < 0 means
17473 that PT is either in one of the lines beginning at
17474 first_unchanged_at_end_row or below the window. Don't care for
17475 lines that might be displayed later at the window end; as
17476 mentioned, this is not a frequent case. */
17477 if (w->cursor.vpos < 0)
17478 {
17479 /* Cursor in unchanged rows at the top? */
17480 if (PT < CHARPOS (start_pos)
17481 && last_unchanged_at_beg_row)
17482 {
17483 row = row_containing_pos (w, PT,
17484 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17485 last_unchanged_at_beg_row + 1, 0);
17486 if (row)
17487 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17488 }
17489
17490 /* Start from first_unchanged_at_end_row looking for PT. */
17491 else if (first_unchanged_at_end_row)
17492 {
17493 row = row_containing_pos (w, PT - delta,
17494 first_unchanged_at_end_row, NULL, 0);
17495 if (row)
17496 set_cursor_from_row (w, row, w->current_matrix, delta,
17497 delta_bytes, dy, dvpos);
17498 }
17499
17500 /* Give up if cursor was not found. */
17501 if (w->cursor.vpos < 0)
17502 {
17503 clear_glyph_matrix (w->desired_matrix);
17504 return -1;
17505 }
17506 }
17507
17508 /* Don't let the cursor end in the scroll margins. */
17509 {
17510 int this_scroll_margin, cursor_height;
17511
17512 this_scroll_margin =
17513 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17514 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17515 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17516
17517 if ((w->cursor.y < this_scroll_margin
17518 && CHARPOS (start) > BEGV)
17519 /* Old redisplay didn't take scroll margin into account at the bottom,
17520 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17521 || (w->cursor.y + (make_cursor_line_fully_visible_p
17522 ? cursor_height + this_scroll_margin
17523 : 1)) > it.last_visible_y)
17524 {
17525 w->cursor.vpos = -1;
17526 clear_glyph_matrix (w->desired_matrix);
17527 return -1;
17528 }
17529 }
17530
17531 /* Scroll the display. Do it before changing the current matrix so
17532 that xterm.c doesn't get confused about where the cursor glyph is
17533 found. */
17534 if (dy && run.height)
17535 {
17536 update_begin (f);
17537
17538 if (FRAME_WINDOW_P (f))
17539 {
17540 FRAME_RIF (f)->update_window_begin_hook (w);
17541 FRAME_RIF (f)->clear_window_mouse_face (w);
17542 FRAME_RIF (f)->scroll_run_hook (w, &run);
17543 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17544 }
17545 else
17546 {
17547 /* Terminal frame. In this case, dvpos gives the number of
17548 lines to scroll by; dvpos < 0 means scroll up. */
17549 int from_vpos
17550 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17551 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17552 int end = (WINDOW_TOP_EDGE_LINE (w)
17553 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17554 + window_internal_height (w));
17555
17556 #if defined (HAVE_GPM) || defined (MSDOS)
17557 x_clear_window_mouse_face (w);
17558 #endif
17559 /* Perform the operation on the screen. */
17560 if (dvpos > 0)
17561 {
17562 /* Scroll last_unchanged_at_beg_row to the end of the
17563 window down dvpos lines. */
17564 set_terminal_window (f, end);
17565
17566 /* On dumb terminals delete dvpos lines at the end
17567 before inserting dvpos empty lines. */
17568 if (!FRAME_SCROLL_REGION_OK (f))
17569 ins_del_lines (f, end - dvpos, -dvpos);
17570
17571 /* Insert dvpos empty lines in front of
17572 last_unchanged_at_beg_row. */
17573 ins_del_lines (f, from, dvpos);
17574 }
17575 else if (dvpos < 0)
17576 {
17577 /* Scroll up last_unchanged_at_beg_vpos to the end of
17578 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17579 set_terminal_window (f, end);
17580
17581 /* Delete dvpos lines in front of
17582 last_unchanged_at_beg_vpos. ins_del_lines will set
17583 the cursor to the given vpos and emit |dvpos| delete
17584 line sequences. */
17585 ins_del_lines (f, from + dvpos, dvpos);
17586
17587 /* On a dumb terminal insert dvpos empty lines at the
17588 end. */
17589 if (!FRAME_SCROLL_REGION_OK (f))
17590 ins_del_lines (f, end + dvpos, -dvpos);
17591 }
17592
17593 set_terminal_window (f, 0);
17594 }
17595
17596 update_end (f);
17597 }
17598
17599 /* Shift reused rows of the current matrix to the right position.
17600 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17601 text. */
17602 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17603 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17604 if (dvpos < 0)
17605 {
17606 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17607 bottom_vpos, dvpos);
17608 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17609 bottom_vpos);
17610 }
17611 else if (dvpos > 0)
17612 {
17613 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17614 bottom_vpos, dvpos);
17615 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17616 first_unchanged_at_end_vpos + dvpos);
17617 }
17618
17619 /* For frame-based redisplay, make sure that current frame and window
17620 matrix are in sync with respect to glyph memory. */
17621 if (!FRAME_WINDOW_P (f))
17622 sync_frame_with_window_matrix_rows (w);
17623
17624 /* Adjust buffer positions in reused rows. */
17625 if (delta || delta_bytes)
17626 increment_matrix_positions (current_matrix,
17627 first_unchanged_at_end_vpos + dvpos,
17628 bottom_vpos, delta, delta_bytes);
17629
17630 /* Adjust Y positions. */
17631 if (dy)
17632 shift_glyph_matrix (w, current_matrix,
17633 first_unchanged_at_end_vpos + dvpos,
17634 bottom_vpos, dy);
17635
17636 if (first_unchanged_at_end_row)
17637 {
17638 first_unchanged_at_end_row += dvpos;
17639 if (first_unchanged_at_end_row->y >= it.last_visible_y
17640 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17641 first_unchanged_at_end_row = NULL;
17642 }
17643
17644 /* If scrolling up, there may be some lines to display at the end of
17645 the window. */
17646 last_text_row_at_end = NULL;
17647 if (dy < 0)
17648 {
17649 /* Scrolling up can leave for example a partially visible line
17650 at the end of the window to be redisplayed. */
17651 /* Set last_row to the glyph row in the current matrix where the
17652 window end line is found. It has been moved up or down in
17653 the matrix by dvpos. */
17654 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17655 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17656
17657 /* If last_row is the window end line, it should display text. */
17658 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17659
17660 /* If window end line was partially visible before, begin
17661 displaying at that line. Otherwise begin displaying with the
17662 line following it. */
17663 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17664 {
17665 init_to_row_start (&it, w, last_row);
17666 it.vpos = last_vpos;
17667 it.current_y = last_row->y;
17668 }
17669 else
17670 {
17671 init_to_row_end (&it, w, last_row);
17672 it.vpos = 1 + last_vpos;
17673 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17674 ++last_row;
17675 }
17676
17677 /* We may start in a continuation line. If so, we have to
17678 get the right continuation_lines_width and current_x. */
17679 it.continuation_lines_width = last_row->continuation_lines_width;
17680 it.hpos = it.current_x = 0;
17681
17682 /* Display the rest of the lines at the window end. */
17683 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17684 while (it.current_y < it.last_visible_y
17685 && !fonts_changed_p)
17686 {
17687 /* Is it always sure that the display agrees with lines in
17688 the current matrix? I don't think so, so we mark rows
17689 displayed invalid in the current matrix by setting their
17690 enabled_p flag to zero. */
17691 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17692 if (display_line (&it))
17693 last_text_row_at_end = it.glyph_row - 1;
17694 }
17695 }
17696
17697 /* Update window_end_pos and window_end_vpos. */
17698 if (first_unchanged_at_end_row
17699 && !last_text_row_at_end)
17700 {
17701 /* Window end line if one of the preserved rows from the current
17702 matrix. Set row to the last row displaying text in current
17703 matrix starting at first_unchanged_at_end_row, after
17704 scrolling. */
17705 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17706 row = find_last_row_displaying_text (w->current_matrix, &it,
17707 first_unchanged_at_end_row);
17708 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17709
17710 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17711 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17712 wset_window_end_vpos
17713 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17714 eassert (w->window_end_bytepos >= 0);
17715 IF_DEBUG (debug_method_add (w, "A"));
17716 }
17717 else if (last_text_row_at_end)
17718 {
17719 wset_window_end_pos
17720 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17721 w->window_end_bytepos
17722 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17723 wset_window_end_vpos
17724 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17725 desired_matrix)));
17726 eassert (w->window_end_bytepos >= 0);
17727 IF_DEBUG (debug_method_add (w, "B"));
17728 }
17729 else if (last_text_row)
17730 {
17731 /* We have displayed either to the end of the window or at the
17732 end of the window, i.e. the last row with text is to be found
17733 in the desired matrix. */
17734 wset_window_end_pos
17735 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17736 w->window_end_bytepos
17737 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17738 wset_window_end_vpos
17739 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17740 eassert (w->window_end_bytepos >= 0);
17741 }
17742 else if (first_unchanged_at_end_row == NULL
17743 && last_text_row == NULL
17744 && last_text_row_at_end == NULL)
17745 {
17746 /* Displayed to end of window, but no line containing text was
17747 displayed. Lines were deleted at the end of the window. */
17748 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17749 int vpos = XFASTINT (w->window_end_vpos);
17750 struct glyph_row *current_row = current_matrix->rows + vpos;
17751 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17752
17753 for (row = NULL;
17754 row == NULL && vpos >= first_vpos;
17755 --vpos, --current_row, --desired_row)
17756 {
17757 if (desired_row->enabled_p)
17758 {
17759 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17760 row = desired_row;
17761 }
17762 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17763 row = current_row;
17764 }
17765
17766 eassert (row != NULL);
17767 wset_window_end_vpos (w, make_number (vpos + 1));
17768 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17769 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17770 eassert (w->window_end_bytepos >= 0);
17771 IF_DEBUG (debug_method_add (w, "C"));
17772 }
17773 else
17774 emacs_abort ();
17775
17776 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17777 debug_end_vpos = XFASTINT (w->window_end_vpos));
17778
17779 /* Record that display has not been completed. */
17780 w->window_end_valid = 0;
17781 w->desired_matrix->no_scrolling_p = 1;
17782 return 3;
17783
17784 #undef GIVE_UP
17785 }
17786
17787
17788 \f
17789 /***********************************************************************
17790 More debugging support
17791 ***********************************************************************/
17792
17793 #ifdef GLYPH_DEBUG
17794
17795 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17796 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17797 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17798
17799
17800 /* Dump the contents of glyph matrix MATRIX on stderr.
17801
17802 GLYPHS 0 means don't show glyph contents.
17803 GLYPHS 1 means show glyphs in short form
17804 GLYPHS > 1 means show glyphs in long form. */
17805
17806 void
17807 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17808 {
17809 int i;
17810 for (i = 0; i < matrix->nrows; ++i)
17811 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17812 }
17813
17814
17815 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17816 the glyph row and area where the glyph comes from. */
17817
17818 void
17819 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17820 {
17821 if (glyph->type == CHAR_GLYPH
17822 || glyph->type == GLYPHLESS_GLYPH)
17823 {
17824 fprintf (stderr,
17825 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17826 glyph - row->glyphs[TEXT_AREA],
17827 (glyph->type == CHAR_GLYPH
17828 ? 'C'
17829 : 'G'),
17830 glyph->charpos,
17831 (BUFFERP (glyph->object)
17832 ? 'B'
17833 : (STRINGP (glyph->object)
17834 ? 'S'
17835 : (INTEGERP (glyph->object)
17836 ? '0'
17837 : '-'))),
17838 glyph->pixel_width,
17839 glyph->u.ch,
17840 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17841 ? glyph->u.ch
17842 : '.'),
17843 glyph->face_id,
17844 glyph->left_box_line_p,
17845 glyph->right_box_line_p);
17846 }
17847 else if (glyph->type == STRETCH_GLYPH)
17848 {
17849 fprintf (stderr,
17850 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17851 glyph - row->glyphs[TEXT_AREA],
17852 'S',
17853 glyph->charpos,
17854 (BUFFERP (glyph->object)
17855 ? 'B'
17856 : (STRINGP (glyph->object)
17857 ? 'S'
17858 : (INTEGERP (glyph->object)
17859 ? '0'
17860 : '-'))),
17861 glyph->pixel_width,
17862 0,
17863 ' ',
17864 glyph->face_id,
17865 glyph->left_box_line_p,
17866 glyph->right_box_line_p);
17867 }
17868 else if (glyph->type == IMAGE_GLYPH)
17869 {
17870 fprintf (stderr,
17871 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17872 glyph - row->glyphs[TEXT_AREA],
17873 'I',
17874 glyph->charpos,
17875 (BUFFERP (glyph->object)
17876 ? 'B'
17877 : (STRINGP (glyph->object)
17878 ? 'S'
17879 : (INTEGERP (glyph->object)
17880 ? '0'
17881 : '-'))),
17882 glyph->pixel_width,
17883 glyph->u.img_id,
17884 '.',
17885 glyph->face_id,
17886 glyph->left_box_line_p,
17887 glyph->right_box_line_p);
17888 }
17889 else if (glyph->type == COMPOSITE_GLYPH)
17890 {
17891 fprintf (stderr,
17892 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17893 glyph - row->glyphs[TEXT_AREA],
17894 '+',
17895 glyph->charpos,
17896 (BUFFERP (glyph->object)
17897 ? 'B'
17898 : (STRINGP (glyph->object)
17899 ? 'S'
17900 : (INTEGERP (glyph->object)
17901 ? '0'
17902 : '-'))),
17903 glyph->pixel_width,
17904 glyph->u.cmp.id);
17905 if (glyph->u.cmp.automatic)
17906 fprintf (stderr,
17907 "[%d-%d]",
17908 glyph->slice.cmp.from, glyph->slice.cmp.to);
17909 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17910 glyph->face_id,
17911 glyph->left_box_line_p,
17912 glyph->right_box_line_p);
17913 }
17914 #ifdef HAVE_XWIDGETS
17915 else if (glyph->type == XWIDGET_GLYPH)
17916 {
17917 fprintf (stderr,
17918 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17919 glyph - row->glyphs[TEXT_AREA],
17920 'X',
17921 glyph->charpos,
17922 (BUFFERP (glyph->object)
17923 ? 'B'
17924 : (STRINGP (glyph->object)
17925 ? 'S'
17926 : '-')),
17927 glyph->pixel_width,
17928 glyph->u.xwidget,
17929 '.',
17930 glyph->face_id,
17931 glyph->left_box_line_p,
17932 glyph->right_box_line_p);
17933
17934 // printf("dump xwidget glyph\n");
17935 }
17936 #endif
17937 }
17938
17939
17940 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17941 GLYPHS 0 means don't show glyph contents.
17942 GLYPHS 1 means show glyphs in short form
17943 GLYPHS > 1 means show glyphs in long form. */
17944
17945 void
17946 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17947 {
17948 if (glyphs != 1)
17949 {
17950 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17951 fprintf (stderr, "==============================================================================\n");
17952
17953 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17954 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17955 vpos,
17956 MATRIX_ROW_START_CHARPOS (row),
17957 MATRIX_ROW_END_CHARPOS (row),
17958 row->used[TEXT_AREA],
17959 row->contains_overlapping_glyphs_p,
17960 row->enabled_p,
17961 row->truncated_on_left_p,
17962 row->truncated_on_right_p,
17963 row->continued_p,
17964 MATRIX_ROW_CONTINUATION_LINE_P (row),
17965 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17966 row->ends_at_zv_p,
17967 row->fill_line_p,
17968 row->ends_in_middle_of_char_p,
17969 row->starts_in_middle_of_char_p,
17970 row->mouse_face_p,
17971 row->x,
17972 row->y,
17973 row->pixel_width,
17974 row->height,
17975 row->visible_height,
17976 row->ascent,
17977 row->phys_ascent);
17978 /* The next 3 lines should align to "Start" in the header. */
17979 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17980 row->end.overlay_string_index,
17981 row->continuation_lines_width);
17982 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17983 CHARPOS (row->start.string_pos),
17984 CHARPOS (row->end.string_pos));
17985 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17986 row->end.dpvec_index);
17987 }
17988
17989 if (glyphs > 1)
17990 {
17991 int area;
17992
17993 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17994 {
17995 struct glyph *glyph = row->glyphs[area];
17996 struct glyph *glyph_end = glyph + row->used[area];
17997
17998 /* Glyph for a line end in text. */
17999 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18000 ++glyph_end;
18001
18002 if (glyph < glyph_end)
18003 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18004
18005 for (; glyph < glyph_end; ++glyph)
18006 dump_glyph (row, glyph, area);
18007 }
18008 }
18009 else if (glyphs == 1)
18010 {
18011 int area;
18012
18013 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18014 {
18015 char *s = alloca (row->used[area] + 4);
18016 int i;
18017
18018 for (i = 0; i < row->used[area]; ++i)
18019 {
18020 struct glyph *glyph = row->glyphs[area] + i;
18021 if (i == row->used[area] - 1
18022 && area == TEXT_AREA
18023 && INTEGERP (glyph->object)
18024 && glyph->type == CHAR_GLYPH
18025 && glyph->u.ch == ' ')
18026 {
18027 strcpy (&s[i], "[\\n]");
18028 i += 4;
18029 }
18030 else if (glyph->type == CHAR_GLYPH
18031 && glyph->u.ch < 0x80
18032 && glyph->u.ch >= ' ')
18033 s[i] = glyph->u.ch;
18034 else
18035 s[i] = '.';
18036 }
18037
18038 s[i] = '\0';
18039 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18040 }
18041 }
18042 }
18043
18044
18045 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18046 Sdump_glyph_matrix, 0, 1, "p",
18047 doc: /* Dump the current matrix of the selected window to stderr.
18048 Shows contents of glyph row structures. With non-nil
18049 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18050 glyphs in short form, otherwise show glyphs in long form. */)
18051 (Lisp_Object glyphs)
18052 {
18053 struct window *w = XWINDOW (selected_window);
18054 struct buffer *buffer = XBUFFER (w->contents);
18055
18056 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18057 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18058 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18059 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18060 fprintf (stderr, "=============================================\n");
18061 dump_glyph_matrix (w->current_matrix,
18062 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18063 return Qnil;
18064 }
18065
18066
18067 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18068 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18069 (void)
18070 {
18071 struct frame *f = XFRAME (selected_frame);
18072 dump_glyph_matrix (f->current_matrix, 1);
18073 return Qnil;
18074 }
18075
18076
18077 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18078 doc: /* Dump glyph row ROW to stderr.
18079 GLYPH 0 means don't dump glyphs.
18080 GLYPH 1 means dump glyphs in short form.
18081 GLYPH > 1 or omitted means dump glyphs in long form. */)
18082 (Lisp_Object row, Lisp_Object glyphs)
18083 {
18084 struct glyph_matrix *matrix;
18085 EMACS_INT vpos;
18086
18087 CHECK_NUMBER (row);
18088 matrix = XWINDOW (selected_window)->current_matrix;
18089 vpos = XINT (row);
18090 if (vpos >= 0 && vpos < matrix->nrows)
18091 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18092 vpos,
18093 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18094 return Qnil;
18095 }
18096
18097
18098 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18099 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18100 GLYPH 0 means don't dump glyphs.
18101 GLYPH 1 means dump glyphs in short form.
18102 GLYPH > 1 or omitted means dump glyphs in long form. */)
18103 (Lisp_Object row, Lisp_Object glyphs)
18104 {
18105 struct frame *sf = SELECTED_FRAME ();
18106 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18107 EMACS_INT vpos;
18108
18109 CHECK_NUMBER (row);
18110 vpos = XINT (row);
18111 if (vpos >= 0 && vpos < m->nrows)
18112 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18113 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18114 return Qnil;
18115 }
18116
18117
18118 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18119 doc: /* Toggle tracing of redisplay.
18120 With ARG, turn tracing on if and only if ARG is positive. */)
18121 (Lisp_Object arg)
18122 {
18123 if (NILP (arg))
18124 trace_redisplay_p = !trace_redisplay_p;
18125 else
18126 {
18127 arg = Fprefix_numeric_value (arg);
18128 trace_redisplay_p = XINT (arg) > 0;
18129 }
18130
18131 return Qnil;
18132 }
18133
18134
18135 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18136 doc: /* Like `format', but print result to stderr.
18137 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18138 (ptrdiff_t nargs, Lisp_Object *args)
18139 {
18140 Lisp_Object s = Fformat (nargs, args);
18141 fprintf (stderr, "%s", SDATA (s));
18142 return Qnil;
18143 }
18144
18145 #endif /* GLYPH_DEBUG */
18146
18147
18148 \f
18149 /***********************************************************************
18150 Building Desired Matrix Rows
18151 ***********************************************************************/
18152
18153 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18154 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18155
18156 static struct glyph_row *
18157 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18158 {
18159 struct frame *f = XFRAME (WINDOW_FRAME (w));
18160 struct buffer *buffer = XBUFFER (w->contents);
18161 struct buffer *old = current_buffer;
18162 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18163 int arrow_len = SCHARS (overlay_arrow_string);
18164 const unsigned char *arrow_end = arrow_string + arrow_len;
18165 const unsigned char *p;
18166 struct it it;
18167 bool multibyte_p;
18168 int n_glyphs_before;
18169
18170 set_buffer_temp (buffer);
18171 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18172 it.glyph_row->used[TEXT_AREA] = 0;
18173 SET_TEXT_POS (it.position, 0, 0);
18174
18175 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18176 p = arrow_string;
18177 while (p < arrow_end)
18178 {
18179 Lisp_Object face, ilisp;
18180
18181 /* Get the next character. */
18182 if (multibyte_p)
18183 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18184 else
18185 {
18186 it.c = it.char_to_display = *p, it.len = 1;
18187 if (! ASCII_CHAR_P (it.c))
18188 it.char_to_display = BYTE8_TO_CHAR (it.c);
18189 }
18190 p += it.len;
18191
18192 /* Get its face. */
18193 ilisp = make_number (p - arrow_string);
18194 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18195 it.face_id = compute_char_face (f, it.char_to_display, face);
18196
18197 /* Compute its width, get its glyphs. */
18198 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18199 SET_TEXT_POS (it.position, -1, -1);
18200 PRODUCE_GLYPHS (&it);
18201
18202 /* If this character doesn't fit any more in the line, we have
18203 to remove some glyphs. */
18204 if (it.current_x > it.last_visible_x)
18205 {
18206 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18207 break;
18208 }
18209 }
18210
18211 set_buffer_temp (old);
18212 return it.glyph_row;
18213 }
18214
18215
18216 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18217 glyphs to insert is determined by produce_special_glyphs. */
18218
18219 static void
18220 insert_left_trunc_glyphs (struct it *it)
18221 {
18222 struct it truncate_it;
18223 struct glyph *from, *end, *to, *toend;
18224
18225 eassert (!FRAME_WINDOW_P (it->f)
18226 || (!it->glyph_row->reversed_p
18227 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18228 || (it->glyph_row->reversed_p
18229 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18230
18231 /* Get the truncation glyphs. */
18232 truncate_it = *it;
18233 truncate_it.current_x = 0;
18234 truncate_it.face_id = DEFAULT_FACE_ID;
18235 truncate_it.glyph_row = &scratch_glyph_row;
18236 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18237 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18238 truncate_it.object = make_number (0);
18239 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18240
18241 /* Overwrite glyphs from IT with truncation glyphs. */
18242 if (!it->glyph_row->reversed_p)
18243 {
18244 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18245
18246 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18247 end = from + tused;
18248 to = it->glyph_row->glyphs[TEXT_AREA];
18249 toend = to + it->glyph_row->used[TEXT_AREA];
18250 if (FRAME_WINDOW_P (it->f))
18251 {
18252 /* On GUI frames, when variable-size fonts are displayed,
18253 the truncation glyphs may need more pixels than the row's
18254 glyphs they overwrite. We overwrite more glyphs to free
18255 enough screen real estate, and enlarge the stretch glyph
18256 on the right (see display_line), if there is one, to
18257 preserve the screen position of the truncation glyphs on
18258 the right. */
18259 int w = 0;
18260 struct glyph *g = to;
18261 short used;
18262
18263 /* The first glyph could be partially visible, in which case
18264 it->glyph_row->x will be negative. But we want the left
18265 truncation glyphs to be aligned at the left margin of the
18266 window, so we override the x coordinate at which the row
18267 will begin. */
18268 it->glyph_row->x = 0;
18269 while (g < toend && w < it->truncation_pixel_width)
18270 {
18271 w += g->pixel_width;
18272 ++g;
18273 }
18274 if (g - to - tused > 0)
18275 {
18276 memmove (to + tused, g, (toend - g) * sizeof(*g));
18277 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18278 }
18279 used = it->glyph_row->used[TEXT_AREA];
18280 if (it->glyph_row->truncated_on_right_p
18281 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18282 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18283 == STRETCH_GLYPH)
18284 {
18285 int extra = w - it->truncation_pixel_width;
18286
18287 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18288 }
18289 }
18290
18291 while (from < end)
18292 *to++ = *from++;
18293
18294 /* There may be padding glyphs left over. Overwrite them too. */
18295 if (!FRAME_WINDOW_P (it->f))
18296 {
18297 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18298 {
18299 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18300 while (from < end)
18301 *to++ = *from++;
18302 }
18303 }
18304
18305 if (to > toend)
18306 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18307 }
18308 else
18309 {
18310 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18311
18312 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18313 that back to front. */
18314 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18315 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18316 toend = it->glyph_row->glyphs[TEXT_AREA];
18317 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18318 if (FRAME_WINDOW_P (it->f))
18319 {
18320 int w = 0;
18321 struct glyph *g = to;
18322
18323 while (g >= toend && w < it->truncation_pixel_width)
18324 {
18325 w += g->pixel_width;
18326 --g;
18327 }
18328 if (to - g - tused > 0)
18329 to = g + tused;
18330 if (it->glyph_row->truncated_on_right_p
18331 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18332 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18333 {
18334 int extra = w - it->truncation_pixel_width;
18335
18336 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18337 }
18338 }
18339
18340 while (from >= end && to >= toend)
18341 *to-- = *from--;
18342 if (!FRAME_WINDOW_P (it->f))
18343 {
18344 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18345 {
18346 from =
18347 truncate_it.glyph_row->glyphs[TEXT_AREA]
18348 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18349 while (from >= end && to >= toend)
18350 *to-- = *from--;
18351 }
18352 }
18353 if (from >= end)
18354 {
18355 /* Need to free some room before prepending additional
18356 glyphs. */
18357 int move_by = from - end + 1;
18358 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18359 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18360
18361 for ( ; g >= g0; g--)
18362 g[move_by] = *g;
18363 while (from >= end)
18364 *to-- = *from--;
18365 it->glyph_row->used[TEXT_AREA] += move_by;
18366 }
18367 }
18368 }
18369
18370 /* Compute the hash code for ROW. */
18371 unsigned
18372 row_hash (struct glyph_row *row)
18373 {
18374 int area, k;
18375 unsigned hashval = 0;
18376
18377 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18378 for (k = 0; k < row->used[area]; ++k)
18379 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18380 + row->glyphs[area][k].u.val
18381 + row->glyphs[area][k].face_id
18382 + row->glyphs[area][k].padding_p
18383 + (row->glyphs[area][k].type << 2));
18384
18385 return hashval;
18386 }
18387
18388 /* Compute the pixel height and width of IT->glyph_row.
18389
18390 Most of the time, ascent and height of a display line will be equal
18391 to the max_ascent and max_height values of the display iterator
18392 structure. This is not the case if
18393
18394 1. We hit ZV without displaying anything. In this case, max_ascent
18395 and max_height will be zero.
18396
18397 2. We have some glyphs that don't contribute to the line height.
18398 (The glyph row flag contributes_to_line_height_p is for future
18399 pixmap extensions).
18400
18401 The first case is easily covered by using default values because in
18402 these cases, the line height does not really matter, except that it
18403 must not be zero. */
18404
18405 static void
18406 compute_line_metrics (struct it *it)
18407 {
18408 struct glyph_row *row = it->glyph_row;
18409
18410 if (FRAME_WINDOW_P (it->f))
18411 {
18412 int i, min_y, max_y;
18413
18414 /* The line may consist of one space only, that was added to
18415 place the cursor on it. If so, the row's height hasn't been
18416 computed yet. */
18417 if (row->height == 0)
18418 {
18419 if (it->max_ascent + it->max_descent == 0)
18420 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18421 row->ascent = it->max_ascent;
18422 row->height = it->max_ascent + it->max_descent;
18423 row->phys_ascent = it->max_phys_ascent;
18424 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18425 row->extra_line_spacing = it->max_extra_line_spacing;
18426 }
18427
18428 /* Compute the width of this line. */
18429 row->pixel_width = row->x;
18430 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18431 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18432
18433 eassert (row->pixel_width >= 0);
18434 eassert (row->ascent >= 0 && row->height > 0);
18435
18436 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18437 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18438
18439 /* If first line's physical ascent is larger than its logical
18440 ascent, use the physical ascent, and make the row taller.
18441 This makes accented characters fully visible. */
18442 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18443 && row->phys_ascent > row->ascent)
18444 {
18445 row->height += row->phys_ascent - row->ascent;
18446 row->ascent = row->phys_ascent;
18447 }
18448
18449 /* Compute how much of the line is visible. */
18450 row->visible_height = row->height;
18451
18452 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18453 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18454
18455 if (row->y < min_y)
18456 row->visible_height -= min_y - row->y;
18457 if (row->y + row->height > max_y)
18458 row->visible_height -= row->y + row->height - max_y;
18459 }
18460 else
18461 {
18462 row->pixel_width = row->used[TEXT_AREA];
18463 if (row->continued_p)
18464 row->pixel_width -= it->continuation_pixel_width;
18465 else if (row->truncated_on_right_p)
18466 row->pixel_width -= it->truncation_pixel_width;
18467 row->ascent = row->phys_ascent = 0;
18468 row->height = row->phys_height = row->visible_height = 1;
18469 row->extra_line_spacing = 0;
18470 }
18471
18472 /* Compute a hash code for this row. */
18473 row->hash = row_hash (row);
18474
18475 it->max_ascent = it->max_descent = 0;
18476 it->max_phys_ascent = it->max_phys_descent = 0;
18477 }
18478
18479
18480 /* Append one space to the glyph row of iterator IT if doing a
18481 window-based redisplay. The space has the same face as
18482 IT->face_id. Value is non-zero if a space was added.
18483
18484 This function is called to make sure that there is always one glyph
18485 at the end of a glyph row that the cursor can be set on under
18486 window-systems. (If there weren't such a glyph we would not know
18487 how wide and tall a box cursor should be displayed).
18488
18489 At the same time this space let's a nicely handle clearing to the
18490 end of the line if the row ends in italic text. */
18491
18492 static int
18493 append_space_for_newline (struct it *it, int default_face_p)
18494 {
18495 if (FRAME_WINDOW_P (it->f))
18496 {
18497 int n = it->glyph_row->used[TEXT_AREA];
18498
18499 if (it->glyph_row->glyphs[TEXT_AREA] + n
18500 < it->glyph_row->glyphs[1 + TEXT_AREA])
18501 {
18502 /* Save some values that must not be changed.
18503 Must save IT->c and IT->len because otherwise
18504 ITERATOR_AT_END_P wouldn't work anymore after
18505 append_space_for_newline has been called. */
18506 enum display_element_type saved_what = it->what;
18507 int saved_c = it->c, saved_len = it->len;
18508 int saved_char_to_display = it->char_to_display;
18509 int saved_x = it->current_x;
18510 int saved_face_id = it->face_id;
18511 int saved_box_end = it->end_of_box_run_p;
18512 struct text_pos saved_pos;
18513 Lisp_Object saved_object;
18514 struct face *face;
18515
18516 saved_object = it->object;
18517 saved_pos = it->position;
18518
18519 it->what = IT_CHARACTER;
18520 memset (&it->position, 0, sizeof it->position);
18521 it->object = make_number (0);
18522 it->c = it->char_to_display = ' ';
18523 it->len = 1;
18524
18525 /* If the default face was remapped, be sure to use the
18526 remapped face for the appended newline. */
18527 if (default_face_p)
18528 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18529 else if (it->face_before_selective_p)
18530 it->face_id = it->saved_face_id;
18531 face = FACE_FROM_ID (it->f, it->face_id);
18532 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18533 /* In R2L rows, we will prepend a stretch glyph that will
18534 have the end_of_box_run_p flag set for it, so there's no
18535 need for the appended newline glyph to have that flag
18536 set. */
18537 if (it->glyph_row->reversed_p
18538 /* But if the appended newline glyph goes all the way to
18539 the end of the row, there will be no stretch glyph,
18540 so leave the box flag set. */
18541 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18542 it->end_of_box_run_p = 0;
18543
18544 PRODUCE_GLYPHS (it);
18545
18546 it->override_ascent = -1;
18547 it->constrain_row_ascent_descent_p = 0;
18548 it->current_x = saved_x;
18549 it->object = saved_object;
18550 it->position = saved_pos;
18551 it->what = saved_what;
18552 it->face_id = saved_face_id;
18553 it->len = saved_len;
18554 it->c = saved_c;
18555 it->char_to_display = saved_char_to_display;
18556 it->end_of_box_run_p = saved_box_end;
18557 return 1;
18558 }
18559 }
18560
18561 return 0;
18562 }
18563
18564
18565 /* Extend the face of the last glyph in the text area of IT->glyph_row
18566 to the end of the display line. Called from display_line. If the
18567 glyph row is empty, add a space glyph to it so that we know the
18568 face to draw. Set the glyph row flag fill_line_p. If the glyph
18569 row is R2L, prepend a stretch glyph to cover the empty space to the
18570 left of the leftmost glyph. */
18571
18572 static void
18573 extend_face_to_end_of_line (struct it *it)
18574 {
18575 struct face *face, *default_face;
18576 struct frame *f = it->f;
18577
18578 /* If line is already filled, do nothing. Non window-system frames
18579 get a grace of one more ``pixel'' because their characters are
18580 1-``pixel'' wide, so they hit the equality too early. This grace
18581 is needed only for R2L rows that are not continued, to produce
18582 one extra blank where we could display the cursor. */
18583 if (it->current_x >= it->last_visible_x
18584 + (!FRAME_WINDOW_P (f)
18585 && it->glyph_row->reversed_p
18586 && !it->glyph_row->continued_p))
18587 return;
18588
18589 /* The default face, possibly remapped. */
18590 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18591
18592 /* Face extension extends the background and box of IT->face_id
18593 to the end of the line. If the background equals the background
18594 of the frame, we don't have to do anything. */
18595 if (it->face_before_selective_p)
18596 face = FACE_FROM_ID (f, it->saved_face_id);
18597 else
18598 face = FACE_FROM_ID (f, it->face_id);
18599
18600 if (FRAME_WINDOW_P (f)
18601 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18602 && face->box == FACE_NO_BOX
18603 && face->background == FRAME_BACKGROUND_PIXEL (f)
18604 && !face->stipple
18605 && !it->glyph_row->reversed_p)
18606 return;
18607
18608 /* Set the glyph row flag indicating that the face of the last glyph
18609 in the text area has to be drawn to the end of the text area. */
18610 it->glyph_row->fill_line_p = 1;
18611
18612 /* If current character of IT is not ASCII, make sure we have the
18613 ASCII face. This will be automatically undone the next time
18614 get_next_display_element returns a multibyte character. Note
18615 that the character will always be single byte in unibyte
18616 text. */
18617 if (!ASCII_CHAR_P (it->c))
18618 {
18619 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18620 }
18621
18622 if (FRAME_WINDOW_P (f))
18623 {
18624 /* If the row is empty, add a space with the current face of IT,
18625 so that we know which face to draw. */
18626 if (it->glyph_row->used[TEXT_AREA] == 0)
18627 {
18628 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18629 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18630 it->glyph_row->used[TEXT_AREA] = 1;
18631 }
18632 #ifdef HAVE_WINDOW_SYSTEM
18633 if (it->glyph_row->reversed_p)
18634 {
18635 /* Prepend a stretch glyph to the row, such that the
18636 rightmost glyph will be drawn flushed all the way to the
18637 right margin of the window. The stretch glyph that will
18638 occupy the empty space, if any, to the left of the
18639 glyphs. */
18640 struct font *font = face->font ? face->font : FRAME_FONT (f);
18641 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18642 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18643 struct glyph *g;
18644 int row_width, stretch_ascent, stretch_width;
18645 struct text_pos saved_pos;
18646 int saved_face_id, saved_avoid_cursor, saved_box_start;
18647
18648 for (row_width = 0, g = row_start; g < row_end; g++)
18649 row_width += g->pixel_width;
18650 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18651 if (stretch_width > 0)
18652 {
18653 stretch_ascent =
18654 (((it->ascent + it->descent)
18655 * FONT_BASE (font)) / FONT_HEIGHT (font));
18656 saved_pos = it->position;
18657 memset (&it->position, 0, sizeof it->position);
18658 saved_avoid_cursor = it->avoid_cursor_p;
18659 it->avoid_cursor_p = 1;
18660 saved_face_id = it->face_id;
18661 saved_box_start = it->start_of_box_run_p;
18662 /* The last row's stretch glyph should get the default
18663 face, to avoid painting the rest of the window with
18664 the region face, if the region ends at ZV. */
18665 if (it->glyph_row->ends_at_zv_p)
18666 it->face_id = default_face->id;
18667 else
18668 it->face_id = face->id;
18669 it->start_of_box_run_p = 0;
18670 append_stretch_glyph (it, make_number (0), stretch_width,
18671 it->ascent + it->descent, stretch_ascent);
18672 it->position = saved_pos;
18673 it->avoid_cursor_p = saved_avoid_cursor;
18674 it->face_id = saved_face_id;
18675 it->start_of_box_run_p = saved_box_start;
18676 }
18677 }
18678 #endif /* HAVE_WINDOW_SYSTEM */
18679 }
18680 else
18681 {
18682 /* Save some values that must not be changed. */
18683 int saved_x = it->current_x;
18684 struct text_pos saved_pos;
18685 Lisp_Object saved_object;
18686 enum display_element_type saved_what = it->what;
18687 int saved_face_id = it->face_id;
18688
18689 saved_object = it->object;
18690 saved_pos = it->position;
18691
18692 it->what = IT_CHARACTER;
18693 memset (&it->position, 0, sizeof it->position);
18694 it->object = make_number (0);
18695 it->c = it->char_to_display = ' ';
18696 it->len = 1;
18697 /* The last row's blank glyphs should get the default face, to
18698 avoid painting the rest of the window with the region face,
18699 if the region ends at ZV. */
18700 if (it->glyph_row->ends_at_zv_p)
18701 it->face_id = default_face->id;
18702 else
18703 it->face_id = face->id;
18704
18705 PRODUCE_GLYPHS (it);
18706
18707 while (it->current_x <= it->last_visible_x)
18708 PRODUCE_GLYPHS (it);
18709
18710 /* Don't count these blanks really. It would let us insert a left
18711 truncation glyph below and make us set the cursor on them, maybe. */
18712 it->current_x = saved_x;
18713 it->object = saved_object;
18714 it->position = saved_pos;
18715 it->what = saved_what;
18716 it->face_id = saved_face_id;
18717 }
18718 }
18719
18720
18721 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18722 trailing whitespace. */
18723
18724 static int
18725 trailing_whitespace_p (ptrdiff_t charpos)
18726 {
18727 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18728 int c = 0;
18729
18730 while (bytepos < ZV_BYTE
18731 && (c = FETCH_CHAR (bytepos),
18732 c == ' ' || c == '\t'))
18733 ++bytepos;
18734
18735 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18736 {
18737 if (bytepos != PT_BYTE)
18738 return 1;
18739 }
18740 return 0;
18741 }
18742
18743
18744 /* Highlight trailing whitespace, if any, in ROW. */
18745
18746 static void
18747 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18748 {
18749 int used = row->used[TEXT_AREA];
18750
18751 if (used)
18752 {
18753 struct glyph *start = row->glyphs[TEXT_AREA];
18754 struct glyph *glyph = start + used - 1;
18755
18756 if (row->reversed_p)
18757 {
18758 /* Right-to-left rows need to be processed in the opposite
18759 direction, so swap the edge pointers. */
18760 glyph = start;
18761 start = row->glyphs[TEXT_AREA] + used - 1;
18762 }
18763
18764 /* Skip over glyphs inserted to display the cursor at the
18765 end of a line, for extending the face of the last glyph
18766 to the end of the line on terminals, and for truncation
18767 and continuation glyphs. */
18768 if (!row->reversed_p)
18769 {
18770 while (glyph >= start
18771 && glyph->type == CHAR_GLYPH
18772 && INTEGERP (glyph->object))
18773 --glyph;
18774 }
18775 else
18776 {
18777 while (glyph <= start
18778 && glyph->type == CHAR_GLYPH
18779 && INTEGERP (glyph->object))
18780 ++glyph;
18781 }
18782
18783 /* If last glyph is a space or stretch, and it's trailing
18784 whitespace, set the face of all trailing whitespace glyphs in
18785 IT->glyph_row to `trailing-whitespace'. */
18786 if ((row->reversed_p ? glyph <= start : glyph >= start)
18787 && BUFFERP (glyph->object)
18788 && (glyph->type == STRETCH_GLYPH
18789 || (glyph->type == CHAR_GLYPH
18790 && glyph->u.ch == ' '))
18791 && trailing_whitespace_p (glyph->charpos))
18792 {
18793 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18794 if (face_id < 0)
18795 return;
18796
18797 if (!row->reversed_p)
18798 {
18799 while (glyph >= start
18800 && BUFFERP (glyph->object)
18801 && (glyph->type == STRETCH_GLYPH
18802 || (glyph->type == CHAR_GLYPH
18803 && glyph->u.ch == ' ')))
18804 (glyph--)->face_id = face_id;
18805 }
18806 else
18807 {
18808 while (glyph <= start
18809 && BUFFERP (glyph->object)
18810 && (glyph->type == STRETCH_GLYPH
18811 || (glyph->type == CHAR_GLYPH
18812 && glyph->u.ch == ' ')))
18813 (glyph++)->face_id = face_id;
18814 }
18815 }
18816 }
18817 }
18818
18819
18820 /* Value is non-zero if glyph row ROW should be
18821 used to hold the cursor. */
18822
18823 static int
18824 cursor_row_p (struct glyph_row *row)
18825 {
18826 int result = 1;
18827
18828 if (PT == CHARPOS (row->end.pos)
18829 || PT == MATRIX_ROW_END_CHARPOS (row))
18830 {
18831 /* Suppose the row ends on a string.
18832 Unless the row is continued, that means it ends on a newline
18833 in the string. If it's anything other than a display string
18834 (e.g., a before-string from an overlay), we don't want the
18835 cursor there. (This heuristic seems to give the optimal
18836 behavior for the various types of multi-line strings.)
18837 One exception: if the string has `cursor' property on one of
18838 its characters, we _do_ want the cursor there. */
18839 if (CHARPOS (row->end.string_pos) >= 0)
18840 {
18841 if (row->continued_p)
18842 result = 1;
18843 else
18844 {
18845 /* Check for `display' property. */
18846 struct glyph *beg = row->glyphs[TEXT_AREA];
18847 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18848 struct glyph *glyph;
18849
18850 result = 0;
18851 for (glyph = end; glyph >= beg; --glyph)
18852 if (STRINGP (glyph->object))
18853 {
18854 Lisp_Object prop
18855 = Fget_char_property (make_number (PT),
18856 Qdisplay, Qnil);
18857 result =
18858 (!NILP (prop)
18859 && display_prop_string_p (prop, glyph->object));
18860 /* If there's a `cursor' property on one of the
18861 string's characters, this row is a cursor row,
18862 even though this is not a display string. */
18863 if (!result)
18864 {
18865 Lisp_Object s = glyph->object;
18866
18867 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18868 {
18869 ptrdiff_t gpos = glyph->charpos;
18870
18871 if (!NILP (Fget_char_property (make_number (gpos),
18872 Qcursor, s)))
18873 {
18874 result = 1;
18875 break;
18876 }
18877 }
18878 }
18879 break;
18880 }
18881 }
18882 }
18883 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18884 {
18885 /* If the row ends in middle of a real character,
18886 and the line is continued, we want the cursor here.
18887 That's because CHARPOS (ROW->end.pos) would equal
18888 PT if PT is before the character. */
18889 if (!row->ends_in_ellipsis_p)
18890 result = row->continued_p;
18891 else
18892 /* If the row ends in an ellipsis, then
18893 CHARPOS (ROW->end.pos) will equal point after the
18894 invisible text. We want that position to be displayed
18895 after the ellipsis. */
18896 result = 0;
18897 }
18898 /* If the row ends at ZV, display the cursor at the end of that
18899 row instead of at the start of the row below. */
18900 else if (row->ends_at_zv_p)
18901 result = 1;
18902 else
18903 result = 0;
18904 }
18905
18906 return result;
18907 }
18908
18909 \f
18910
18911 /* Push the property PROP so that it will be rendered at the current
18912 position in IT. Return 1 if PROP was successfully pushed, 0
18913 otherwise. Called from handle_line_prefix to handle the
18914 `line-prefix' and `wrap-prefix' properties. */
18915
18916 static int
18917 push_prefix_prop (struct it *it, Lisp_Object prop)
18918 {
18919 struct text_pos pos =
18920 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18921
18922 eassert (it->method == GET_FROM_BUFFER
18923 || it->method == GET_FROM_DISPLAY_VECTOR
18924 || it->method == GET_FROM_STRING);
18925
18926 /* We need to save the current buffer/string position, so it will be
18927 restored by pop_it, because iterate_out_of_display_property
18928 depends on that being set correctly, but some situations leave
18929 it->position not yet set when this function is called. */
18930 push_it (it, &pos);
18931
18932 if (STRINGP (prop))
18933 {
18934 if (SCHARS (prop) == 0)
18935 {
18936 pop_it (it);
18937 return 0;
18938 }
18939
18940 it->string = prop;
18941 it->string_from_prefix_prop_p = 1;
18942 it->multibyte_p = STRING_MULTIBYTE (it->string);
18943 it->current.overlay_string_index = -1;
18944 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18945 it->end_charpos = it->string_nchars = SCHARS (it->string);
18946 it->method = GET_FROM_STRING;
18947 it->stop_charpos = 0;
18948 it->prev_stop = 0;
18949 it->base_level_stop = 0;
18950
18951 /* Force paragraph direction to be that of the parent
18952 buffer/string. */
18953 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18954 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18955 else
18956 it->paragraph_embedding = L2R;
18957
18958 /* Set up the bidi iterator for this display string. */
18959 if (it->bidi_p)
18960 {
18961 it->bidi_it.string.lstring = it->string;
18962 it->bidi_it.string.s = NULL;
18963 it->bidi_it.string.schars = it->end_charpos;
18964 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18965 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18966 it->bidi_it.string.unibyte = !it->multibyte_p;
18967 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18968 }
18969 }
18970 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18971 {
18972 it->method = GET_FROM_STRETCH;
18973 it->object = prop;
18974 }
18975 #ifdef HAVE_WINDOW_SYSTEM
18976 else if (IMAGEP (prop))
18977 {
18978 it->what = IT_IMAGE;
18979 it->image_id = lookup_image (it->f, prop);
18980 it->method = GET_FROM_IMAGE;
18981 }
18982 #endif /* HAVE_WINDOW_SYSTEM */
18983 else
18984 {
18985 pop_it (it); /* bogus display property, give up */
18986 return 0;
18987 }
18988
18989 return 1;
18990 }
18991
18992 /* Return the character-property PROP at the current position in IT. */
18993
18994 static Lisp_Object
18995 get_it_property (struct it *it, Lisp_Object prop)
18996 {
18997 Lisp_Object position;
18998
18999 if (STRINGP (it->object))
19000 position = make_number (IT_STRING_CHARPOS (*it));
19001 else if (BUFFERP (it->object))
19002 position = make_number (IT_CHARPOS (*it));
19003 else
19004 return Qnil;
19005
19006 return Fget_char_property (position, prop, it->object);
19007 }
19008
19009 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19010
19011 static void
19012 handle_line_prefix (struct it *it)
19013 {
19014 Lisp_Object prefix;
19015
19016 if (it->continuation_lines_width > 0)
19017 {
19018 prefix = get_it_property (it, Qwrap_prefix);
19019 if (NILP (prefix))
19020 prefix = Vwrap_prefix;
19021 }
19022 else
19023 {
19024 prefix = get_it_property (it, Qline_prefix);
19025 if (NILP (prefix))
19026 prefix = Vline_prefix;
19027 }
19028 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19029 {
19030 /* If the prefix is wider than the window, and we try to wrap
19031 it, it would acquire its own wrap prefix, and so on till the
19032 iterator stack overflows. So, don't wrap the prefix. */
19033 it->line_wrap = TRUNCATE;
19034 it->avoid_cursor_p = 1;
19035 }
19036 }
19037
19038 \f
19039
19040 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19041 only for R2L lines from display_line and display_string, when they
19042 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19043 the line/string needs to be continued on the next glyph row. */
19044 static void
19045 unproduce_glyphs (struct it *it, int n)
19046 {
19047 struct glyph *glyph, *end;
19048
19049 eassert (it->glyph_row);
19050 eassert (it->glyph_row->reversed_p);
19051 eassert (it->area == TEXT_AREA);
19052 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19053
19054 if (n > it->glyph_row->used[TEXT_AREA])
19055 n = it->glyph_row->used[TEXT_AREA];
19056 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19057 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19058 for ( ; glyph < end; glyph++)
19059 glyph[-n] = *glyph;
19060 }
19061
19062 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19063 and ROW->maxpos. */
19064 static void
19065 find_row_edges (struct it *it, struct glyph_row *row,
19066 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19067 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19068 {
19069 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19070 lines' rows is implemented for bidi-reordered rows. */
19071
19072 /* ROW->minpos is the value of min_pos, the minimal buffer position
19073 we have in ROW, or ROW->start.pos if that is smaller. */
19074 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19075 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19076 else
19077 /* We didn't find buffer positions smaller than ROW->start, or
19078 didn't find _any_ valid buffer positions in any of the glyphs,
19079 so we must trust the iterator's computed positions. */
19080 row->minpos = row->start.pos;
19081 if (max_pos <= 0)
19082 {
19083 max_pos = CHARPOS (it->current.pos);
19084 max_bpos = BYTEPOS (it->current.pos);
19085 }
19086
19087 /* Here are the various use-cases for ending the row, and the
19088 corresponding values for ROW->maxpos:
19089
19090 Line ends in a newline from buffer eol_pos + 1
19091 Line is continued from buffer max_pos + 1
19092 Line is truncated on right it->current.pos
19093 Line ends in a newline from string max_pos + 1(*)
19094 (*) + 1 only when line ends in a forward scan
19095 Line is continued from string max_pos
19096 Line is continued from display vector max_pos
19097 Line is entirely from a string min_pos == max_pos
19098 Line is entirely from a display vector min_pos == max_pos
19099 Line that ends at ZV ZV
19100
19101 If you discover other use-cases, please add them here as
19102 appropriate. */
19103 if (row->ends_at_zv_p)
19104 row->maxpos = it->current.pos;
19105 else if (row->used[TEXT_AREA])
19106 {
19107 int seen_this_string = 0;
19108 struct glyph_row *r1 = row - 1;
19109
19110 /* Did we see the same display string on the previous row? */
19111 if (STRINGP (it->object)
19112 /* this is not the first row */
19113 && row > it->w->desired_matrix->rows
19114 /* previous row is not the header line */
19115 && !r1->mode_line_p
19116 /* previous row also ends in a newline from a string */
19117 && r1->ends_in_newline_from_string_p)
19118 {
19119 struct glyph *start, *end;
19120
19121 /* Search for the last glyph of the previous row that came
19122 from buffer or string. Depending on whether the row is
19123 L2R or R2L, we need to process it front to back or the
19124 other way round. */
19125 if (!r1->reversed_p)
19126 {
19127 start = r1->glyphs[TEXT_AREA];
19128 end = start + r1->used[TEXT_AREA];
19129 /* Glyphs inserted by redisplay have an integer (zero)
19130 as their object. */
19131 while (end > start
19132 && INTEGERP ((end - 1)->object)
19133 && (end - 1)->charpos <= 0)
19134 --end;
19135 if (end > start)
19136 {
19137 if (EQ ((end - 1)->object, it->object))
19138 seen_this_string = 1;
19139 }
19140 else
19141 /* If all the glyphs of the previous row were inserted
19142 by redisplay, it means the previous row was
19143 produced from a single newline, which is only
19144 possible if that newline came from the same string
19145 as the one which produced this ROW. */
19146 seen_this_string = 1;
19147 }
19148 else
19149 {
19150 end = r1->glyphs[TEXT_AREA] - 1;
19151 start = end + r1->used[TEXT_AREA];
19152 while (end < start
19153 && INTEGERP ((end + 1)->object)
19154 && (end + 1)->charpos <= 0)
19155 ++end;
19156 if (end < start)
19157 {
19158 if (EQ ((end + 1)->object, it->object))
19159 seen_this_string = 1;
19160 }
19161 else
19162 seen_this_string = 1;
19163 }
19164 }
19165 /* Take note of each display string that covers a newline only
19166 once, the first time we see it. This is for when a display
19167 string includes more than one newline in it. */
19168 if (row->ends_in_newline_from_string_p && !seen_this_string)
19169 {
19170 /* If we were scanning the buffer forward when we displayed
19171 the string, we want to account for at least one buffer
19172 position that belongs to this row (position covered by
19173 the display string), so that cursor positioning will
19174 consider this row as a candidate when point is at the end
19175 of the visual line represented by this row. This is not
19176 required when scanning back, because max_pos will already
19177 have a much larger value. */
19178 if (CHARPOS (row->end.pos) > max_pos)
19179 INC_BOTH (max_pos, max_bpos);
19180 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19181 }
19182 else if (CHARPOS (it->eol_pos) > 0)
19183 SET_TEXT_POS (row->maxpos,
19184 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19185 else if (row->continued_p)
19186 {
19187 /* If max_pos is different from IT's current position, it
19188 means IT->method does not belong to the display element
19189 at max_pos. However, it also means that the display
19190 element at max_pos was displayed in its entirety on this
19191 line, which is equivalent to saying that the next line
19192 starts at the next buffer position. */
19193 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19194 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19195 else
19196 {
19197 INC_BOTH (max_pos, max_bpos);
19198 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19199 }
19200 }
19201 else if (row->truncated_on_right_p)
19202 /* display_line already called reseat_at_next_visible_line_start,
19203 which puts the iterator at the beginning of the next line, in
19204 the logical order. */
19205 row->maxpos = it->current.pos;
19206 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19207 /* A line that is entirely from a string/image/stretch... */
19208 row->maxpos = row->minpos;
19209 else
19210 emacs_abort ();
19211 }
19212 else
19213 row->maxpos = it->current.pos;
19214 }
19215
19216 /* Construct the glyph row IT->glyph_row in the desired matrix of
19217 IT->w from text at the current position of IT. See dispextern.h
19218 for an overview of struct it. Value is non-zero if
19219 IT->glyph_row displays text, as opposed to a line displaying ZV
19220 only. */
19221
19222 static int
19223 display_line (struct it *it)
19224 {
19225 struct glyph_row *row = it->glyph_row;
19226 Lisp_Object overlay_arrow_string;
19227 struct it wrap_it;
19228 void *wrap_data = NULL;
19229 int may_wrap = 0, wrap_x IF_LINT (= 0);
19230 int wrap_row_used = -1;
19231 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19232 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19233 int wrap_row_extra_line_spacing IF_LINT (= 0);
19234 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19235 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19236 int cvpos;
19237 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19238 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19239
19240 /* We always start displaying at hpos zero even if hscrolled. */
19241 eassert (it->hpos == 0 && it->current_x == 0);
19242
19243 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19244 >= it->w->desired_matrix->nrows)
19245 {
19246 it->w->nrows_scale_factor++;
19247 fonts_changed_p = 1;
19248 return 0;
19249 }
19250
19251 /* Is IT->w showing the region? */
19252 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19253
19254 /* Clear the result glyph row and enable it. */
19255 prepare_desired_row (row);
19256
19257 row->y = it->current_y;
19258 row->start = it->start;
19259 row->continuation_lines_width = it->continuation_lines_width;
19260 row->displays_text_p = 1;
19261 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19262 it->starts_in_middle_of_char_p = 0;
19263
19264 /* Arrange the overlays nicely for our purposes. Usually, we call
19265 display_line on only one line at a time, in which case this
19266 can't really hurt too much, or we call it on lines which appear
19267 one after another in the buffer, in which case all calls to
19268 recenter_overlay_lists but the first will be pretty cheap. */
19269 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19270
19271 /* Move over display elements that are not visible because we are
19272 hscrolled. This may stop at an x-position < IT->first_visible_x
19273 if the first glyph is partially visible or if we hit a line end. */
19274 if (it->current_x < it->first_visible_x)
19275 {
19276 enum move_it_result move_result;
19277
19278 this_line_min_pos = row->start.pos;
19279 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19280 MOVE_TO_POS | MOVE_TO_X);
19281 /* If we are under a large hscroll, move_it_in_display_line_to
19282 could hit the end of the line without reaching
19283 it->first_visible_x. Pretend that we did reach it. This is
19284 especially important on a TTY, where we will call
19285 extend_face_to_end_of_line, which needs to know how many
19286 blank glyphs to produce. */
19287 if (it->current_x < it->first_visible_x
19288 && (move_result == MOVE_NEWLINE_OR_CR
19289 || move_result == MOVE_POS_MATCH_OR_ZV))
19290 it->current_x = it->first_visible_x;
19291
19292 /* Record the smallest positions seen while we moved over
19293 display elements that are not visible. This is needed by
19294 redisplay_internal for optimizing the case where the cursor
19295 stays inside the same line. The rest of this function only
19296 considers positions that are actually displayed, so
19297 RECORD_MAX_MIN_POS will not otherwise record positions that
19298 are hscrolled to the left of the left edge of the window. */
19299 min_pos = CHARPOS (this_line_min_pos);
19300 min_bpos = BYTEPOS (this_line_min_pos);
19301 }
19302 else
19303 {
19304 /* We only do this when not calling `move_it_in_display_line_to'
19305 above, because move_it_in_display_line_to calls
19306 handle_line_prefix itself. */
19307 handle_line_prefix (it);
19308 }
19309
19310 /* Get the initial row height. This is either the height of the
19311 text hscrolled, if there is any, or zero. */
19312 row->ascent = it->max_ascent;
19313 row->height = it->max_ascent + it->max_descent;
19314 row->phys_ascent = it->max_phys_ascent;
19315 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19316 row->extra_line_spacing = it->max_extra_line_spacing;
19317
19318 /* Utility macro to record max and min buffer positions seen until now. */
19319 #define RECORD_MAX_MIN_POS(IT) \
19320 do \
19321 { \
19322 int composition_p = !STRINGP ((IT)->string) \
19323 && ((IT)->what == IT_COMPOSITION); \
19324 ptrdiff_t current_pos = \
19325 composition_p ? (IT)->cmp_it.charpos \
19326 : IT_CHARPOS (*(IT)); \
19327 ptrdiff_t current_bpos = \
19328 composition_p ? CHAR_TO_BYTE (current_pos) \
19329 : IT_BYTEPOS (*(IT)); \
19330 if (current_pos < min_pos) \
19331 { \
19332 min_pos = current_pos; \
19333 min_bpos = current_bpos; \
19334 } \
19335 if (IT_CHARPOS (*it) > max_pos) \
19336 { \
19337 max_pos = IT_CHARPOS (*it); \
19338 max_bpos = IT_BYTEPOS (*it); \
19339 } \
19340 } \
19341 while (0)
19342
19343 /* Loop generating characters. The loop is left with IT on the next
19344 character to display. */
19345 while (1)
19346 {
19347 int n_glyphs_before, hpos_before, x_before;
19348 int x, nglyphs;
19349 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19350
19351 /* Retrieve the next thing to display. Value is zero if end of
19352 buffer reached. */
19353 if (!get_next_display_element (it))
19354 {
19355 /* Maybe add a space at the end of this line that is used to
19356 display the cursor there under X. Set the charpos of the
19357 first glyph of blank lines not corresponding to any text
19358 to -1. */
19359 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19360 row->exact_window_width_line_p = 1;
19361 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19362 || row->used[TEXT_AREA] == 0)
19363 {
19364 row->glyphs[TEXT_AREA]->charpos = -1;
19365 row->displays_text_p = 0;
19366
19367 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19368 && (!MINI_WINDOW_P (it->w)
19369 || (minibuf_level && EQ (it->window, minibuf_window))))
19370 row->indicate_empty_line_p = 1;
19371 }
19372
19373 it->continuation_lines_width = 0;
19374 row->ends_at_zv_p = 1;
19375 /* A row that displays right-to-left text must always have
19376 its last face extended all the way to the end of line,
19377 even if this row ends in ZV, because we still write to
19378 the screen left to right. We also need to extend the
19379 last face if the default face is remapped to some
19380 different face, otherwise the functions that clear
19381 portions of the screen will clear with the default face's
19382 background color. */
19383 if (row->reversed_p
19384 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19385 extend_face_to_end_of_line (it);
19386 break;
19387 }
19388
19389 /* Now, get the metrics of what we want to display. This also
19390 generates glyphs in `row' (which is IT->glyph_row). */
19391 n_glyphs_before = row->used[TEXT_AREA];
19392 x = it->current_x;
19393
19394 /* Remember the line height so far in case the next element doesn't
19395 fit on the line. */
19396 if (it->line_wrap != TRUNCATE)
19397 {
19398 ascent = it->max_ascent;
19399 descent = it->max_descent;
19400 phys_ascent = it->max_phys_ascent;
19401 phys_descent = it->max_phys_descent;
19402
19403 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19404 {
19405 if (IT_DISPLAYING_WHITESPACE (it))
19406 may_wrap = 1;
19407 else if (may_wrap)
19408 {
19409 SAVE_IT (wrap_it, *it, wrap_data);
19410 wrap_x = x;
19411 wrap_row_used = row->used[TEXT_AREA];
19412 wrap_row_ascent = row->ascent;
19413 wrap_row_height = row->height;
19414 wrap_row_phys_ascent = row->phys_ascent;
19415 wrap_row_phys_height = row->phys_height;
19416 wrap_row_extra_line_spacing = row->extra_line_spacing;
19417 wrap_row_min_pos = min_pos;
19418 wrap_row_min_bpos = min_bpos;
19419 wrap_row_max_pos = max_pos;
19420 wrap_row_max_bpos = max_bpos;
19421 may_wrap = 0;
19422 }
19423 }
19424 }
19425
19426 PRODUCE_GLYPHS (it);
19427
19428 /* If this display element was in marginal areas, continue with
19429 the next one. */
19430 if (it->area != TEXT_AREA)
19431 {
19432 row->ascent = max (row->ascent, it->max_ascent);
19433 row->height = max (row->height, it->max_ascent + it->max_descent);
19434 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19435 row->phys_height = max (row->phys_height,
19436 it->max_phys_ascent + it->max_phys_descent);
19437 row->extra_line_spacing = max (row->extra_line_spacing,
19438 it->max_extra_line_spacing);
19439 set_iterator_to_next (it, 1);
19440 continue;
19441 }
19442
19443 /* Does the display element fit on the line? If we truncate
19444 lines, we should draw past the right edge of the window. If
19445 we don't truncate, we want to stop so that we can display the
19446 continuation glyph before the right margin. If lines are
19447 continued, there are two possible strategies for characters
19448 resulting in more than 1 glyph (e.g. tabs): Display as many
19449 glyphs as possible in this line and leave the rest for the
19450 continuation line, or display the whole element in the next
19451 line. Original redisplay did the former, so we do it also. */
19452 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19453 hpos_before = it->hpos;
19454 x_before = x;
19455
19456 if (/* Not a newline. */
19457 nglyphs > 0
19458 /* Glyphs produced fit entirely in the line. */
19459 && it->current_x < it->last_visible_x)
19460 {
19461 it->hpos += nglyphs;
19462 row->ascent = max (row->ascent, it->max_ascent);
19463 row->height = max (row->height, it->max_ascent + it->max_descent);
19464 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19465 row->phys_height = max (row->phys_height,
19466 it->max_phys_ascent + it->max_phys_descent);
19467 row->extra_line_spacing = max (row->extra_line_spacing,
19468 it->max_extra_line_spacing);
19469 if (it->current_x - it->pixel_width < it->first_visible_x)
19470 row->x = x - it->first_visible_x;
19471 /* Record the maximum and minimum buffer positions seen so
19472 far in glyphs that will be displayed by this row. */
19473 if (it->bidi_p)
19474 RECORD_MAX_MIN_POS (it);
19475 }
19476 else
19477 {
19478 int i, new_x;
19479 struct glyph *glyph;
19480
19481 for (i = 0; i < nglyphs; ++i, x = new_x)
19482 {
19483 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19484 new_x = x + glyph->pixel_width;
19485
19486 if (/* Lines are continued. */
19487 it->line_wrap != TRUNCATE
19488 && (/* Glyph doesn't fit on the line. */
19489 new_x > it->last_visible_x
19490 /* Or it fits exactly on a window system frame. */
19491 || (new_x == it->last_visible_x
19492 && FRAME_WINDOW_P (it->f)
19493 && (row->reversed_p
19494 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19495 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19496 {
19497 /* End of a continued line. */
19498
19499 if (it->hpos == 0
19500 || (new_x == it->last_visible_x
19501 && FRAME_WINDOW_P (it->f)
19502 && (row->reversed_p
19503 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19504 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19505 {
19506 /* Current glyph is the only one on the line or
19507 fits exactly on the line. We must continue
19508 the line because we can't draw the cursor
19509 after the glyph. */
19510 row->continued_p = 1;
19511 it->current_x = new_x;
19512 it->continuation_lines_width += new_x;
19513 ++it->hpos;
19514 if (i == nglyphs - 1)
19515 {
19516 /* If line-wrap is on, check if a previous
19517 wrap point was found. */
19518 if (wrap_row_used > 0
19519 /* Even if there is a previous wrap
19520 point, continue the line here as
19521 usual, if (i) the previous character
19522 was a space or tab AND (ii) the
19523 current character is not. */
19524 && (!may_wrap
19525 || IT_DISPLAYING_WHITESPACE (it)))
19526 goto back_to_wrap;
19527
19528 /* Record the maximum and minimum buffer
19529 positions seen so far in glyphs that will be
19530 displayed by this row. */
19531 if (it->bidi_p)
19532 RECORD_MAX_MIN_POS (it);
19533 set_iterator_to_next (it, 1);
19534 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19535 {
19536 if (!get_next_display_element (it))
19537 {
19538 row->exact_window_width_line_p = 1;
19539 it->continuation_lines_width = 0;
19540 row->continued_p = 0;
19541 row->ends_at_zv_p = 1;
19542 }
19543 else if (ITERATOR_AT_END_OF_LINE_P (it))
19544 {
19545 row->continued_p = 0;
19546 row->exact_window_width_line_p = 1;
19547 }
19548 }
19549 }
19550 else if (it->bidi_p)
19551 RECORD_MAX_MIN_POS (it);
19552 }
19553 else if (CHAR_GLYPH_PADDING_P (*glyph)
19554 && !FRAME_WINDOW_P (it->f))
19555 {
19556 /* A padding glyph that doesn't fit on this line.
19557 This means the whole character doesn't fit
19558 on the line. */
19559 if (row->reversed_p)
19560 unproduce_glyphs (it, row->used[TEXT_AREA]
19561 - n_glyphs_before);
19562 row->used[TEXT_AREA] = n_glyphs_before;
19563
19564 /* Fill the rest of the row with continuation
19565 glyphs like in 20.x. */
19566 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19567 < row->glyphs[1 + TEXT_AREA])
19568 produce_special_glyphs (it, IT_CONTINUATION);
19569
19570 row->continued_p = 1;
19571 it->current_x = x_before;
19572 it->continuation_lines_width += x_before;
19573
19574 /* Restore the height to what it was before the
19575 element not fitting on the line. */
19576 it->max_ascent = ascent;
19577 it->max_descent = descent;
19578 it->max_phys_ascent = phys_ascent;
19579 it->max_phys_descent = phys_descent;
19580 }
19581 else if (wrap_row_used > 0)
19582 {
19583 back_to_wrap:
19584 if (row->reversed_p)
19585 unproduce_glyphs (it,
19586 row->used[TEXT_AREA] - wrap_row_used);
19587 RESTORE_IT (it, &wrap_it, wrap_data);
19588 it->continuation_lines_width += wrap_x;
19589 row->used[TEXT_AREA] = wrap_row_used;
19590 row->ascent = wrap_row_ascent;
19591 row->height = wrap_row_height;
19592 row->phys_ascent = wrap_row_phys_ascent;
19593 row->phys_height = wrap_row_phys_height;
19594 row->extra_line_spacing = wrap_row_extra_line_spacing;
19595 min_pos = wrap_row_min_pos;
19596 min_bpos = wrap_row_min_bpos;
19597 max_pos = wrap_row_max_pos;
19598 max_bpos = wrap_row_max_bpos;
19599 row->continued_p = 1;
19600 row->ends_at_zv_p = 0;
19601 row->exact_window_width_line_p = 0;
19602 it->continuation_lines_width += x;
19603
19604 /* Make sure that a non-default face is extended
19605 up to the right margin of the window. */
19606 extend_face_to_end_of_line (it);
19607 }
19608 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19609 {
19610 /* A TAB that extends past the right edge of the
19611 window. This produces a single glyph on
19612 window system frames. We leave the glyph in
19613 this row and let it fill the row, but don't
19614 consume the TAB. */
19615 if ((row->reversed_p
19616 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19617 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19618 produce_special_glyphs (it, IT_CONTINUATION);
19619 it->continuation_lines_width += it->last_visible_x;
19620 row->ends_in_middle_of_char_p = 1;
19621 row->continued_p = 1;
19622 glyph->pixel_width = it->last_visible_x - x;
19623 it->starts_in_middle_of_char_p = 1;
19624 }
19625 else
19626 {
19627 /* Something other than a TAB that draws past
19628 the right edge of the window. Restore
19629 positions to values before the element. */
19630 if (row->reversed_p)
19631 unproduce_glyphs (it, row->used[TEXT_AREA]
19632 - (n_glyphs_before + i));
19633 row->used[TEXT_AREA] = n_glyphs_before + i;
19634
19635 /* Display continuation glyphs. */
19636 it->current_x = x_before;
19637 it->continuation_lines_width += x;
19638 if (!FRAME_WINDOW_P (it->f)
19639 || (row->reversed_p
19640 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19641 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19642 produce_special_glyphs (it, IT_CONTINUATION);
19643 row->continued_p = 1;
19644
19645 extend_face_to_end_of_line (it);
19646
19647 if (nglyphs > 1 && i > 0)
19648 {
19649 row->ends_in_middle_of_char_p = 1;
19650 it->starts_in_middle_of_char_p = 1;
19651 }
19652
19653 /* Restore the height to what it was before the
19654 element not fitting on the line. */
19655 it->max_ascent = ascent;
19656 it->max_descent = descent;
19657 it->max_phys_ascent = phys_ascent;
19658 it->max_phys_descent = phys_descent;
19659 }
19660
19661 break;
19662 }
19663 else if (new_x > it->first_visible_x)
19664 {
19665 /* Increment number of glyphs actually displayed. */
19666 ++it->hpos;
19667
19668 /* Record the maximum and minimum buffer positions
19669 seen so far in glyphs that will be displayed by
19670 this row. */
19671 if (it->bidi_p)
19672 RECORD_MAX_MIN_POS (it);
19673
19674 if (x < it->first_visible_x)
19675 /* Glyph is partially visible, i.e. row starts at
19676 negative X position. */
19677 row->x = x - it->first_visible_x;
19678 }
19679 else
19680 {
19681 /* Glyph is completely off the left margin of the
19682 window. This should not happen because of the
19683 move_it_in_display_line at the start of this
19684 function, unless the text display area of the
19685 window is empty. */
19686 eassert (it->first_visible_x <= it->last_visible_x);
19687 }
19688 }
19689 /* Even if this display element produced no glyphs at all,
19690 we want to record its position. */
19691 if (it->bidi_p && nglyphs == 0)
19692 RECORD_MAX_MIN_POS (it);
19693
19694 row->ascent = max (row->ascent, it->max_ascent);
19695 row->height = max (row->height, it->max_ascent + it->max_descent);
19696 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19697 row->phys_height = max (row->phys_height,
19698 it->max_phys_ascent + it->max_phys_descent);
19699 row->extra_line_spacing = max (row->extra_line_spacing,
19700 it->max_extra_line_spacing);
19701
19702 /* End of this display line if row is continued. */
19703 if (row->continued_p || row->ends_at_zv_p)
19704 break;
19705 }
19706
19707 at_end_of_line:
19708 /* Is this a line end? If yes, we're also done, after making
19709 sure that a non-default face is extended up to the right
19710 margin of the window. */
19711 if (ITERATOR_AT_END_OF_LINE_P (it))
19712 {
19713 int used_before = row->used[TEXT_AREA];
19714
19715 row->ends_in_newline_from_string_p = STRINGP (it->object);
19716
19717 /* Add a space at the end of the line that is used to
19718 display the cursor there. */
19719 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19720 append_space_for_newline (it, 0);
19721
19722 /* Extend the face to the end of the line. */
19723 extend_face_to_end_of_line (it);
19724
19725 /* Make sure we have the position. */
19726 if (used_before == 0)
19727 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19728
19729 /* Record the position of the newline, for use in
19730 find_row_edges. */
19731 it->eol_pos = it->current.pos;
19732
19733 /* Consume the line end. This skips over invisible lines. */
19734 set_iterator_to_next (it, 1);
19735 it->continuation_lines_width = 0;
19736 break;
19737 }
19738
19739 /* Proceed with next display element. Note that this skips
19740 over lines invisible because of selective display. */
19741 set_iterator_to_next (it, 1);
19742
19743 /* If we truncate lines, we are done when the last displayed
19744 glyphs reach past the right margin of the window. */
19745 if (it->line_wrap == TRUNCATE
19746 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19747 ? (it->current_x >= it->last_visible_x)
19748 : (it->current_x > it->last_visible_x)))
19749 {
19750 /* Maybe add truncation glyphs. */
19751 if (!FRAME_WINDOW_P (it->f)
19752 || (row->reversed_p
19753 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19754 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19755 {
19756 int i, n;
19757
19758 if (!row->reversed_p)
19759 {
19760 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19761 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19762 break;
19763 }
19764 else
19765 {
19766 for (i = 0; i < row->used[TEXT_AREA]; i++)
19767 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19768 break;
19769 /* Remove any padding glyphs at the front of ROW, to
19770 make room for the truncation glyphs we will be
19771 adding below. The loop below always inserts at
19772 least one truncation glyph, so also remove the
19773 last glyph added to ROW. */
19774 unproduce_glyphs (it, i + 1);
19775 /* Adjust i for the loop below. */
19776 i = row->used[TEXT_AREA] - (i + 1);
19777 }
19778
19779 it->current_x = x_before;
19780 if (!FRAME_WINDOW_P (it->f))
19781 {
19782 for (n = row->used[TEXT_AREA]; i < n; ++i)
19783 {
19784 row->used[TEXT_AREA] = i;
19785 produce_special_glyphs (it, IT_TRUNCATION);
19786 }
19787 }
19788 else
19789 {
19790 row->used[TEXT_AREA] = i;
19791 produce_special_glyphs (it, IT_TRUNCATION);
19792 }
19793 }
19794 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19795 {
19796 /* Don't truncate if we can overflow newline into fringe. */
19797 if (!get_next_display_element (it))
19798 {
19799 it->continuation_lines_width = 0;
19800 row->ends_at_zv_p = 1;
19801 row->exact_window_width_line_p = 1;
19802 break;
19803 }
19804 if (ITERATOR_AT_END_OF_LINE_P (it))
19805 {
19806 row->exact_window_width_line_p = 1;
19807 goto at_end_of_line;
19808 }
19809 it->current_x = x_before;
19810 }
19811
19812 row->truncated_on_right_p = 1;
19813 it->continuation_lines_width = 0;
19814 reseat_at_next_visible_line_start (it, 0);
19815 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19816 it->hpos = hpos_before;
19817 break;
19818 }
19819 }
19820
19821 if (wrap_data)
19822 bidi_unshelve_cache (wrap_data, 1);
19823
19824 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19825 at the left window margin. */
19826 if (it->first_visible_x
19827 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19828 {
19829 if (!FRAME_WINDOW_P (it->f)
19830 || (row->reversed_p
19831 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19832 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19833 insert_left_trunc_glyphs (it);
19834 row->truncated_on_left_p = 1;
19835 }
19836
19837 /* Remember the position at which this line ends.
19838
19839 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19840 cannot be before the call to find_row_edges below, since that is
19841 where these positions are determined. */
19842 row->end = it->current;
19843 if (!it->bidi_p)
19844 {
19845 row->minpos = row->start.pos;
19846 row->maxpos = row->end.pos;
19847 }
19848 else
19849 {
19850 /* ROW->minpos and ROW->maxpos must be the smallest and
19851 `1 + the largest' buffer positions in ROW. But if ROW was
19852 bidi-reordered, these two positions can be anywhere in the
19853 row, so we must determine them now. */
19854 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19855 }
19856
19857 /* If the start of this line is the overlay arrow-position, then
19858 mark this glyph row as the one containing the overlay arrow.
19859 This is clearly a mess with variable size fonts. It would be
19860 better to let it be displayed like cursors under X. */
19861 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19862 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19863 !NILP (overlay_arrow_string)))
19864 {
19865 /* Overlay arrow in window redisplay is a fringe bitmap. */
19866 if (STRINGP (overlay_arrow_string))
19867 {
19868 struct glyph_row *arrow_row
19869 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19870 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19871 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19872 struct glyph *p = row->glyphs[TEXT_AREA];
19873 struct glyph *p2, *end;
19874
19875 /* Copy the arrow glyphs. */
19876 while (glyph < arrow_end)
19877 *p++ = *glyph++;
19878
19879 /* Throw away padding glyphs. */
19880 p2 = p;
19881 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19882 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19883 ++p2;
19884 if (p2 > p)
19885 {
19886 while (p2 < end)
19887 *p++ = *p2++;
19888 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19889 }
19890 }
19891 else
19892 {
19893 eassert (INTEGERP (overlay_arrow_string));
19894 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19895 }
19896 overlay_arrow_seen = 1;
19897 }
19898
19899 /* Highlight trailing whitespace. */
19900 if (!NILP (Vshow_trailing_whitespace))
19901 highlight_trailing_whitespace (it->f, it->glyph_row);
19902
19903 /* Compute pixel dimensions of this line. */
19904 compute_line_metrics (it);
19905
19906 /* Implementation note: No changes in the glyphs of ROW or in their
19907 faces can be done past this point, because compute_line_metrics
19908 computes ROW's hash value and stores it within the glyph_row
19909 structure. */
19910
19911 /* Record whether this row ends inside an ellipsis. */
19912 row->ends_in_ellipsis_p
19913 = (it->method == GET_FROM_DISPLAY_VECTOR
19914 && it->ellipsis_p);
19915
19916 /* Save fringe bitmaps in this row. */
19917 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19918 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19919 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19920 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19921
19922 it->left_user_fringe_bitmap = 0;
19923 it->left_user_fringe_face_id = 0;
19924 it->right_user_fringe_bitmap = 0;
19925 it->right_user_fringe_face_id = 0;
19926
19927 /* Maybe set the cursor. */
19928 cvpos = it->w->cursor.vpos;
19929 if ((cvpos < 0
19930 /* In bidi-reordered rows, keep checking for proper cursor
19931 position even if one has been found already, because buffer
19932 positions in such rows change non-linearly with ROW->VPOS,
19933 when a line is continued. One exception: when we are at ZV,
19934 display cursor on the first suitable glyph row, since all
19935 the empty rows after that also have their position set to ZV. */
19936 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19937 lines' rows is implemented for bidi-reordered rows. */
19938 || (it->bidi_p
19939 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19940 && PT >= MATRIX_ROW_START_CHARPOS (row)
19941 && PT <= MATRIX_ROW_END_CHARPOS (row)
19942 && cursor_row_p (row))
19943 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19944
19945 /* Prepare for the next line. This line starts horizontally at (X
19946 HPOS) = (0 0). Vertical positions are incremented. As a
19947 convenience for the caller, IT->glyph_row is set to the next
19948 row to be used. */
19949 it->current_x = it->hpos = 0;
19950 it->current_y += row->height;
19951 SET_TEXT_POS (it->eol_pos, 0, 0);
19952 ++it->vpos;
19953 ++it->glyph_row;
19954 /* The next row should by default use the same value of the
19955 reversed_p flag as this one. set_iterator_to_next decides when
19956 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19957 the flag accordingly. */
19958 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19959 it->glyph_row->reversed_p = row->reversed_p;
19960 it->start = row->end;
19961 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19962
19963 #undef RECORD_MAX_MIN_POS
19964 }
19965
19966 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19967 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19968 doc: /* Return paragraph direction at point in BUFFER.
19969 Value is either `left-to-right' or `right-to-left'.
19970 If BUFFER is omitted or nil, it defaults to the current buffer.
19971
19972 Paragraph direction determines how the text in the paragraph is displayed.
19973 In left-to-right paragraphs, text begins at the left margin of the window
19974 and the reading direction is generally left to right. In right-to-left
19975 paragraphs, text begins at the right margin and is read from right to left.
19976
19977 See also `bidi-paragraph-direction'. */)
19978 (Lisp_Object buffer)
19979 {
19980 struct buffer *buf = current_buffer;
19981 struct buffer *old = buf;
19982
19983 if (! NILP (buffer))
19984 {
19985 CHECK_BUFFER (buffer);
19986 buf = XBUFFER (buffer);
19987 }
19988
19989 if (NILP (BVAR (buf, bidi_display_reordering))
19990 || NILP (BVAR (buf, enable_multibyte_characters))
19991 /* When we are loading loadup.el, the character property tables
19992 needed for bidi iteration are not yet available. */
19993 || !NILP (Vpurify_flag))
19994 return Qleft_to_right;
19995 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19996 return BVAR (buf, bidi_paragraph_direction);
19997 else
19998 {
19999 /* Determine the direction from buffer text. We could try to
20000 use current_matrix if it is up to date, but this seems fast
20001 enough as it is. */
20002 struct bidi_it itb;
20003 ptrdiff_t pos = BUF_PT (buf);
20004 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20005 int c;
20006 void *itb_data = bidi_shelve_cache ();
20007
20008 set_buffer_temp (buf);
20009 /* bidi_paragraph_init finds the base direction of the paragraph
20010 by searching forward from paragraph start. We need the base
20011 direction of the current or _previous_ paragraph, so we need
20012 to make sure we are within that paragraph. To that end, find
20013 the previous non-empty line. */
20014 if (pos >= ZV && pos > BEGV)
20015 DEC_BOTH (pos, bytepos);
20016 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20017 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20018 {
20019 while ((c = FETCH_BYTE (bytepos)) == '\n'
20020 || c == ' ' || c == '\t' || c == '\f')
20021 {
20022 if (bytepos <= BEGV_BYTE)
20023 break;
20024 bytepos--;
20025 pos--;
20026 }
20027 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20028 bytepos--;
20029 }
20030 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20031 itb.paragraph_dir = NEUTRAL_DIR;
20032 itb.string.s = NULL;
20033 itb.string.lstring = Qnil;
20034 itb.string.bufpos = 0;
20035 itb.string.unibyte = 0;
20036 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20037 bidi_unshelve_cache (itb_data, 0);
20038 set_buffer_temp (old);
20039 switch (itb.paragraph_dir)
20040 {
20041 case L2R:
20042 return Qleft_to_right;
20043 break;
20044 case R2L:
20045 return Qright_to_left;
20046 break;
20047 default:
20048 emacs_abort ();
20049 }
20050 }
20051 }
20052
20053
20054 \f
20055 /***********************************************************************
20056 Menu Bar
20057 ***********************************************************************/
20058
20059 /* Redisplay the menu bar in the frame for window W.
20060
20061 The menu bar of X frames that don't have X toolkit support is
20062 displayed in a special window W->frame->menu_bar_window.
20063
20064 The menu bar of terminal frames is treated specially as far as
20065 glyph matrices are concerned. Menu bar lines are not part of
20066 windows, so the update is done directly on the frame matrix rows
20067 for the menu bar. */
20068
20069 static void
20070 display_menu_bar (struct window *w)
20071 {
20072 struct frame *f = XFRAME (WINDOW_FRAME (w));
20073 struct it it;
20074 Lisp_Object items;
20075 int i;
20076
20077 /* Don't do all this for graphical frames. */
20078 #ifdef HAVE_NTGUI
20079 if (FRAME_W32_P (f))
20080 return;
20081 #endif
20082 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20083 if (FRAME_X_P (f))
20084 return;
20085 #endif
20086
20087 #ifdef HAVE_NS
20088 if (FRAME_NS_P (f))
20089 return;
20090 #endif /* HAVE_NS */
20091
20092 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20093 eassert (!FRAME_WINDOW_P (f));
20094 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20095 it.first_visible_x = 0;
20096 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20097 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20098 if (FRAME_WINDOW_P (f))
20099 {
20100 /* Menu bar lines are displayed in the desired matrix of the
20101 dummy window menu_bar_window. */
20102 struct window *menu_w;
20103 menu_w = XWINDOW (f->menu_bar_window);
20104 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20105 MENU_FACE_ID);
20106 it.first_visible_x = 0;
20107 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20108 }
20109 else
20110 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20111 {
20112 /* This is a TTY frame, i.e. character hpos/vpos are used as
20113 pixel x/y. */
20114 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20115 MENU_FACE_ID);
20116 it.first_visible_x = 0;
20117 it.last_visible_x = FRAME_COLS (f);
20118 }
20119
20120 /* FIXME: This should be controlled by a user option. See the
20121 comments in redisplay_tool_bar and display_mode_line about
20122 this. */
20123 it.paragraph_embedding = L2R;
20124
20125 /* Clear all rows of the menu bar. */
20126 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20127 {
20128 struct glyph_row *row = it.glyph_row + i;
20129 clear_glyph_row (row);
20130 row->enabled_p = 1;
20131 row->full_width_p = 1;
20132 }
20133
20134 /* Display all items of the menu bar. */
20135 items = FRAME_MENU_BAR_ITEMS (it.f);
20136 for (i = 0; i < ASIZE (items); i += 4)
20137 {
20138 Lisp_Object string;
20139
20140 /* Stop at nil string. */
20141 string = AREF (items, i + 1);
20142 if (NILP (string))
20143 break;
20144
20145 /* Remember where item was displayed. */
20146 ASET (items, i + 3, make_number (it.hpos));
20147
20148 /* Display the item, pad with one space. */
20149 if (it.current_x < it.last_visible_x)
20150 display_string (NULL, string, Qnil, 0, 0, &it,
20151 SCHARS (string) + 1, 0, 0, -1);
20152 }
20153
20154 /* Fill out the line with spaces. */
20155 if (it.current_x < it.last_visible_x)
20156 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20157
20158 /* Compute the total height of the lines. */
20159 compute_line_metrics (&it);
20160 }
20161
20162
20163 \f
20164 /***********************************************************************
20165 Mode Line
20166 ***********************************************************************/
20167
20168 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20169 FORCE is non-zero, redisplay mode lines unconditionally.
20170 Otherwise, redisplay only mode lines that are garbaged. Value is
20171 the number of windows whose mode lines were redisplayed. */
20172
20173 static int
20174 redisplay_mode_lines (Lisp_Object window, int force)
20175 {
20176 int nwindows = 0;
20177
20178 while (!NILP (window))
20179 {
20180 struct window *w = XWINDOW (window);
20181
20182 if (WINDOWP (w->contents))
20183 nwindows += redisplay_mode_lines (w->contents, force);
20184 else if (force
20185 || FRAME_GARBAGED_P (XFRAME (w->frame))
20186 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20187 {
20188 struct text_pos lpoint;
20189 struct buffer *old = current_buffer;
20190
20191 /* Set the window's buffer for the mode line display. */
20192 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20193 set_buffer_internal_1 (XBUFFER (w->contents));
20194
20195 /* Point refers normally to the selected window. For any
20196 other window, set up appropriate value. */
20197 if (!EQ (window, selected_window))
20198 {
20199 struct text_pos pt;
20200
20201 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20202 if (CHARPOS (pt) < BEGV)
20203 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20204 else if (CHARPOS (pt) > (ZV - 1))
20205 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20206 else
20207 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20208 }
20209
20210 /* Display mode lines. */
20211 clear_glyph_matrix (w->desired_matrix);
20212 if (display_mode_lines (w))
20213 {
20214 ++nwindows;
20215 w->must_be_updated_p = 1;
20216 }
20217
20218 /* Restore old settings. */
20219 set_buffer_internal_1 (old);
20220 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20221 }
20222
20223 window = w->next;
20224 }
20225
20226 return nwindows;
20227 }
20228
20229
20230 /* Display the mode and/or header line of window W. Value is the
20231 sum number of mode lines and header lines displayed. */
20232
20233 static int
20234 display_mode_lines (struct window *w)
20235 {
20236 Lisp_Object old_selected_window = selected_window;
20237 Lisp_Object old_selected_frame = selected_frame;
20238 Lisp_Object new_frame = w->frame;
20239 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20240 int n = 0;
20241
20242 selected_frame = new_frame;
20243 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20244 or window's point, then we'd need select_window_1 here as well. */
20245 XSETWINDOW (selected_window, w);
20246 XFRAME (new_frame)->selected_window = selected_window;
20247
20248 /* These will be set while the mode line specs are processed. */
20249 line_number_displayed = 0;
20250 w->column_number_displayed = -1;
20251
20252 if (WINDOW_WANTS_MODELINE_P (w))
20253 {
20254 struct window *sel_w = XWINDOW (old_selected_window);
20255
20256 /* Select mode line face based on the real selected window. */
20257 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20258 BVAR (current_buffer, mode_line_format));
20259 ++n;
20260 }
20261
20262 if (WINDOW_WANTS_HEADER_LINE_P (w))
20263 {
20264 display_mode_line (w, HEADER_LINE_FACE_ID,
20265 BVAR (current_buffer, header_line_format));
20266 ++n;
20267 }
20268
20269 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20270 selected_frame = old_selected_frame;
20271 selected_window = old_selected_window;
20272 return n;
20273 }
20274
20275
20276 /* Display mode or header line of window W. FACE_ID specifies which
20277 line to display; it is either MODE_LINE_FACE_ID or
20278 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20279 display. Value is the pixel height of the mode/header line
20280 displayed. */
20281
20282 static int
20283 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20284 {
20285 struct it it;
20286 struct face *face;
20287 ptrdiff_t count = SPECPDL_INDEX ();
20288
20289 init_iterator (&it, w, -1, -1, NULL, face_id);
20290 /* Don't extend on a previously drawn mode-line.
20291 This may happen if called from pos_visible_p. */
20292 it.glyph_row->enabled_p = 0;
20293 prepare_desired_row (it.glyph_row);
20294
20295 it.glyph_row->mode_line_p = 1;
20296
20297 /* FIXME: This should be controlled by a user option. But
20298 supporting such an option is not trivial, since the mode line is
20299 made up of many separate strings. */
20300 it.paragraph_embedding = L2R;
20301
20302 record_unwind_protect (unwind_format_mode_line,
20303 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20304
20305 mode_line_target = MODE_LINE_DISPLAY;
20306
20307 /* Temporarily make frame's keyboard the current kboard so that
20308 kboard-local variables in the mode_line_format will get the right
20309 values. */
20310 push_kboard (FRAME_KBOARD (it.f));
20311 record_unwind_save_match_data ();
20312 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20313 pop_kboard ();
20314
20315 unbind_to (count, Qnil);
20316
20317 /* Fill up with spaces. */
20318 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20319
20320 compute_line_metrics (&it);
20321 it.glyph_row->full_width_p = 1;
20322 it.glyph_row->continued_p = 0;
20323 it.glyph_row->truncated_on_left_p = 0;
20324 it.glyph_row->truncated_on_right_p = 0;
20325
20326 /* Make a 3D mode-line have a shadow at its right end. */
20327 face = FACE_FROM_ID (it.f, face_id);
20328 extend_face_to_end_of_line (&it);
20329 if (face->box != FACE_NO_BOX)
20330 {
20331 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20332 + it.glyph_row->used[TEXT_AREA] - 1);
20333 last->right_box_line_p = 1;
20334 }
20335
20336 return it.glyph_row->height;
20337 }
20338
20339 /* Move element ELT in LIST to the front of LIST.
20340 Return the updated list. */
20341
20342 static Lisp_Object
20343 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20344 {
20345 register Lisp_Object tail, prev;
20346 register Lisp_Object tem;
20347
20348 tail = list;
20349 prev = Qnil;
20350 while (CONSP (tail))
20351 {
20352 tem = XCAR (tail);
20353
20354 if (EQ (elt, tem))
20355 {
20356 /* Splice out the link TAIL. */
20357 if (NILP (prev))
20358 list = XCDR (tail);
20359 else
20360 Fsetcdr (prev, XCDR (tail));
20361
20362 /* Now make it the first. */
20363 Fsetcdr (tail, list);
20364 return tail;
20365 }
20366 else
20367 prev = tail;
20368 tail = XCDR (tail);
20369 QUIT;
20370 }
20371
20372 /* Not found--return unchanged LIST. */
20373 return list;
20374 }
20375
20376 /* Contribute ELT to the mode line for window IT->w. How it
20377 translates into text depends on its data type.
20378
20379 IT describes the display environment in which we display, as usual.
20380
20381 DEPTH is the depth in recursion. It is used to prevent
20382 infinite recursion here.
20383
20384 FIELD_WIDTH is the number of characters the display of ELT should
20385 occupy in the mode line, and PRECISION is the maximum number of
20386 characters to display from ELT's representation. See
20387 display_string for details.
20388
20389 Returns the hpos of the end of the text generated by ELT.
20390
20391 PROPS is a property list to add to any string we encounter.
20392
20393 If RISKY is nonzero, remove (disregard) any properties in any string
20394 we encounter, and ignore :eval and :propertize.
20395
20396 The global variable `mode_line_target' determines whether the
20397 output is passed to `store_mode_line_noprop',
20398 `store_mode_line_string', or `display_string'. */
20399
20400 static int
20401 display_mode_element (struct it *it, int depth, int field_width, int precision,
20402 Lisp_Object elt, Lisp_Object props, int risky)
20403 {
20404 int n = 0, field, prec;
20405 int literal = 0;
20406
20407 tail_recurse:
20408 if (depth > 100)
20409 elt = build_string ("*too-deep*");
20410
20411 depth++;
20412
20413 switch (XTYPE (elt))
20414 {
20415 case Lisp_String:
20416 {
20417 /* A string: output it and check for %-constructs within it. */
20418 unsigned char c;
20419 ptrdiff_t offset = 0;
20420
20421 if (SCHARS (elt) > 0
20422 && (!NILP (props) || risky))
20423 {
20424 Lisp_Object oprops, aelt;
20425 oprops = Ftext_properties_at (make_number (0), elt);
20426
20427 /* If the starting string's properties are not what
20428 we want, translate the string. Also, if the string
20429 is risky, do that anyway. */
20430
20431 if (NILP (Fequal (props, oprops)) || risky)
20432 {
20433 /* If the starting string has properties,
20434 merge the specified ones onto the existing ones. */
20435 if (! NILP (oprops) && !risky)
20436 {
20437 Lisp_Object tem;
20438
20439 oprops = Fcopy_sequence (oprops);
20440 tem = props;
20441 while (CONSP (tem))
20442 {
20443 oprops = Fplist_put (oprops, XCAR (tem),
20444 XCAR (XCDR (tem)));
20445 tem = XCDR (XCDR (tem));
20446 }
20447 props = oprops;
20448 }
20449
20450 aelt = Fassoc (elt, mode_line_proptrans_alist);
20451 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20452 {
20453 /* AELT is what we want. Move it to the front
20454 without consing. */
20455 elt = XCAR (aelt);
20456 mode_line_proptrans_alist
20457 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20458 }
20459 else
20460 {
20461 Lisp_Object tem;
20462
20463 /* If AELT has the wrong props, it is useless.
20464 so get rid of it. */
20465 if (! NILP (aelt))
20466 mode_line_proptrans_alist
20467 = Fdelq (aelt, mode_line_proptrans_alist);
20468
20469 elt = Fcopy_sequence (elt);
20470 Fset_text_properties (make_number (0), Flength (elt),
20471 props, elt);
20472 /* Add this item to mode_line_proptrans_alist. */
20473 mode_line_proptrans_alist
20474 = Fcons (Fcons (elt, props),
20475 mode_line_proptrans_alist);
20476 /* Truncate mode_line_proptrans_alist
20477 to at most 50 elements. */
20478 tem = Fnthcdr (make_number (50),
20479 mode_line_proptrans_alist);
20480 if (! NILP (tem))
20481 XSETCDR (tem, Qnil);
20482 }
20483 }
20484 }
20485
20486 offset = 0;
20487
20488 if (literal)
20489 {
20490 prec = precision - n;
20491 switch (mode_line_target)
20492 {
20493 case MODE_LINE_NOPROP:
20494 case MODE_LINE_TITLE:
20495 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20496 break;
20497 case MODE_LINE_STRING:
20498 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20499 break;
20500 case MODE_LINE_DISPLAY:
20501 n += display_string (NULL, elt, Qnil, 0, 0, it,
20502 0, prec, 0, STRING_MULTIBYTE (elt));
20503 break;
20504 }
20505
20506 break;
20507 }
20508
20509 /* Handle the non-literal case. */
20510
20511 while ((precision <= 0 || n < precision)
20512 && SREF (elt, offset) != 0
20513 && (mode_line_target != MODE_LINE_DISPLAY
20514 || it->current_x < it->last_visible_x))
20515 {
20516 ptrdiff_t last_offset = offset;
20517
20518 /* Advance to end of string or next format specifier. */
20519 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20520 ;
20521
20522 if (offset - 1 != last_offset)
20523 {
20524 ptrdiff_t nchars, nbytes;
20525
20526 /* Output to end of string or up to '%'. Field width
20527 is length of string. Don't output more than
20528 PRECISION allows us. */
20529 offset--;
20530
20531 prec = c_string_width (SDATA (elt) + last_offset,
20532 offset - last_offset, precision - n,
20533 &nchars, &nbytes);
20534
20535 switch (mode_line_target)
20536 {
20537 case MODE_LINE_NOPROP:
20538 case MODE_LINE_TITLE:
20539 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20540 break;
20541 case MODE_LINE_STRING:
20542 {
20543 ptrdiff_t bytepos = last_offset;
20544 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20545 ptrdiff_t endpos = (precision <= 0
20546 ? string_byte_to_char (elt, offset)
20547 : charpos + nchars);
20548
20549 n += store_mode_line_string (NULL,
20550 Fsubstring (elt, make_number (charpos),
20551 make_number (endpos)),
20552 0, 0, 0, Qnil);
20553 }
20554 break;
20555 case MODE_LINE_DISPLAY:
20556 {
20557 ptrdiff_t bytepos = last_offset;
20558 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20559
20560 if (precision <= 0)
20561 nchars = string_byte_to_char (elt, offset) - charpos;
20562 n += display_string (NULL, elt, Qnil, 0, charpos,
20563 it, 0, nchars, 0,
20564 STRING_MULTIBYTE (elt));
20565 }
20566 break;
20567 }
20568 }
20569 else /* c == '%' */
20570 {
20571 ptrdiff_t percent_position = offset;
20572
20573 /* Get the specified minimum width. Zero means
20574 don't pad. */
20575 field = 0;
20576 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20577 field = field * 10 + c - '0';
20578
20579 /* Don't pad beyond the total padding allowed. */
20580 if (field_width - n > 0 && field > field_width - n)
20581 field = field_width - n;
20582
20583 /* Note that either PRECISION <= 0 or N < PRECISION. */
20584 prec = precision - n;
20585
20586 if (c == 'M')
20587 n += display_mode_element (it, depth, field, prec,
20588 Vglobal_mode_string, props,
20589 risky);
20590 else if (c != 0)
20591 {
20592 bool multibyte;
20593 ptrdiff_t bytepos, charpos;
20594 const char *spec;
20595 Lisp_Object string;
20596
20597 bytepos = percent_position;
20598 charpos = (STRING_MULTIBYTE (elt)
20599 ? string_byte_to_char (elt, bytepos)
20600 : bytepos);
20601 spec = decode_mode_spec (it->w, c, field, &string);
20602 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20603
20604 switch (mode_line_target)
20605 {
20606 case MODE_LINE_NOPROP:
20607 case MODE_LINE_TITLE:
20608 n += store_mode_line_noprop (spec, field, prec);
20609 break;
20610 case MODE_LINE_STRING:
20611 {
20612 Lisp_Object tem = build_string (spec);
20613 props = Ftext_properties_at (make_number (charpos), elt);
20614 /* Should only keep face property in props */
20615 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20616 }
20617 break;
20618 case MODE_LINE_DISPLAY:
20619 {
20620 int nglyphs_before, nwritten;
20621
20622 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20623 nwritten = display_string (spec, string, elt,
20624 charpos, 0, it,
20625 field, prec, 0,
20626 multibyte);
20627
20628 /* Assign to the glyphs written above the
20629 string where the `%x' came from, position
20630 of the `%'. */
20631 if (nwritten > 0)
20632 {
20633 struct glyph *glyph
20634 = (it->glyph_row->glyphs[TEXT_AREA]
20635 + nglyphs_before);
20636 int i;
20637
20638 for (i = 0; i < nwritten; ++i)
20639 {
20640 glyph[i].object = elt;
20641 glyph[i].charpos = charpos;
20642 }
20643
20644 n += nwritten;
20645 }
20646 }
20647 break;
20648 }
20649 }
20650 else /* c == 0 */
20651 break;
20652 }
20653 }
20654 }
20655 break;
20656
20657 case Lisp_Symbol:
20658 /* A symbol: process the value of the symbol recursively
20659 as if it appeared here directly. Avoid error if symbol void.
20660 Special case: if value of symbol is a string, output the string
20661 literally. */
20662 {
20663 register Lisp_Object tem;
20664
20665 /* If the variable is not marked as risky to set
20666 then its contents are risky to use. */
20667 if (NILP (Fget (elt, Qrisky_local_variable)))
20668 risky = 1;
20669
20670 tem = Fboundp (elt);
20671 if (!NILP (tem))
20672 {
20673 tem = Fsymbol_value (elt);
20674 /* If value is a string, output that string literally:
20675 don't check for % within it. */
20676 if (STRINGP (tem))
20677 literal = 1;
20678
20679 if (!EQ (tem, elt))
20680 {
20681 /* Give up right away for nil or t. */
20682 elt = tem;
20683 goto tail_recurse;
20684 }
20685 }
20686 }
20687 break;
20688
20689 case Lisp_Cons:
20690 {
20691 register Lisp_Object car, tem;
20692
20693 /* A cons cell: five distinct cases.
20694 If first element is :eval or :propertize, do something special.
20695 If first element is a string or a cons, process all the elements
20696 and effectively concatenate them.
20697 If first element is a negative number, truncate displaying cdr to
20698 at most that many characters. If positive, pad (with spaces)
20699 to at least that many characters.
20700 If first element is a symbol, process the cadr or caddr recursively
20701 according to whether the symbol's value is non-nil or nil. */
20702 car = XCAR (elt);
20703 if (EQ (car, QCeval))
20704 {
20705 /* An element of the form (:eval FORM) means evaluate FORM
20706 and use the result as mode line elements. */
20707
20708 if (risky)
20709 break;
20710
20711 if (CONSP (XCDR (elt)))
20712 {
20713 Lisp_Object spec;
20714 spec = safe_eval (XCAR (XCDR (elt)));
20715 n += display_mode_element (it, depth, field_width - n,
20716 precision - n, spec, props,
20717 risky);
20718 }
20719 }
20720 else if (EQ (car, QCpropertize))
20721 {
20722 /* An element of the form (:propertize ELT PROPS...)
20723 means display ELT but applying properties PROPS. */
20724
20725 if (risky)
20726 break;
20727
20728 if (CONSP (XCDR (elt)))
20729 n += display_mode_element (it, depth, field_width - n,
20730 precision - n, XCAR (XCDR (elt)),
20731 XCDR (XCDR (elt)), risky);
20732 }
20733 else if (SYMBOLP (car))
20734 {
20735 tem = Fboundp (car);
20736 elt = XCDR (elt);
20737 if (!CONSP (elt))
20738 goto invalid;
20739 /* elt is now the cdr, and we know it is a cons cell.
20740 Use its car if CAR has a non-nil value. */
20741 if (!NILP (tem))
20742 {
20743 tem = Fsymbol_value (car);
20744 if (!NILP (tem))
20745 {
20746 elt = XCAR (elt);
20747 goto tail_recurse;
20748 }
20749 }
20750 /* Symbol's value is nil (or symbol is unbound)
20751 Get the cddr of the original list
20752 and if possible find the caddr and use that. */
20753 elt = XCDR (elt);
20754 if (NILP (elt))
20755 break;
20756 else if (!CONSP (elt))
20757 goto invalid;
20758 elt = XCAR (elt);
20759 goto tail_recurse;
20760 }
20761 else if (INTEGERP (car))
20762 {
20763 register int lim = XINT (car);
20764 elt = XCDR (elt);
20765 if (lim < 0)
20766 {
20767 /* Negative int means reduce maximum width. */
20768 if (precision <= 0)
20769 precision = -lim;
20770 else
20771 precision = min (precision, -lim);
20772 }
20773 else if (lim > 0)
20774 {
20775 /* Padding specified. Don't let it be more than
20776 current maximum. */
20777 if (precision > 0)
20778 lim = min (precision, lim);
20779
20780 /* If that's more padding than already wanted, queue it.
20781 But don't reduce padding already specified even if
20782 that is beyond the current truncation point. */
20783 field_width = max (lim, field_width);
20784 }
20785 goto tail_recurse;
20786 }
20787 else if (STRINGP (car) || CONSP (car))
20788 {
20789 Lisp_Object halftail = elt;
20790 int len = 0;
20791
20792 while (CONSP (elt)
20793 && (precision <= 0 || n < precision))
20794 {
20795 n += display_mode_element (it, depth,
20796 /* Do padding only after the last
20797 element in the list. */
20798 (! CONSP (XCDR (elt))
20799 ? field_width - n
20800 : 0),
20801 precision - n, XCAR (elt),
20802 props, risky);
20803 elt = XCDR (elt);
20804 len++;
20805 if ((len & 1) == 0)
20806 halftail = XCDR (halftail);
20807 /* Check for cycle. */
20808 if (EQ (halftail, elt))
20809 break;
20810 }
20811 }
20812 }
20813 break;
20814
20815 default:
20816 invalid:
20817 elt = build_string ("*invalid*");
20818 goto tail_recurse;
20819 }
20820
20821 /* Pad to FIELD_WIDTH. */
20822 if (field_width > 0 && n < field_width)
20823 {
20824 switch (mode_line_target)
20825 {
20826 case MODE_LINE_NOPROP:
20827 case MODE_LINE_TITLE:
20828 n += store_mode_line_noprop ("", field_width - n, 0);
20829 break;
20830 case MODE_LINE_STRING:
20831 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20832 break;
20833 case MODE_LINE_DISPLAY:
20834 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20835 0, 0, 0);
20836 break;
20837 }
20838 }
20839
20840 return n;
20841 }
20842
20843 /* Store a mode-line string element in mode_line_string_list.
20844
20845 If STRING is non-null, display that C string. Otherwise, the Lisp
20846 string LISP_STRING is displayed.
20847
20848 FIELD_WIDTH is the minimum number of output glyphs to produce.
20849 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20850 with spaces. FIELD_WIDTH <= 0 means don't pad.
20851
20852 PRECISION is the maximum number of characters to output from
20853 STRING. PRECISION <= 0 means don't truncate the string.
20854
20855 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20856 properties to the string.
20857
20858 PROPS are the properties to add to the string.
20859 The mode_line_string_face face property is always added to the string.
20860 */
20861
20862 static int
20863 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20864 int field_width, int precision, Lisp_Object props)
20865 {
20866 ptrdiff_t len;
20867 int n = 0;
20868
20869 if (string != NULL)
20870 {
20871 len = strlen (string);
20872 if (precision > 0 && len > precision)
20873 len = precision;
20874 lisp_string = make_string (string, len);
20875 if (NILP (props))
20876 props = mode_line_string_face_prop;
20877 else if (!NILP (mode_line_string_face))
20878 {
20879 Lisp_Object face = Fplist_get (props, Qface);
20880 props = Fcopy_sequence (props);
20881 if (NILP (face))
20882 face = mode_line_string_face;
20883 else
20884 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20885 props = Fplist_put (props, Qface, face);
20886 }
20887 Fadd_text_properties (make_number (0), make_number (len),
20888 props, lisp_string);
20889 }
20890 else
20891 {
20892 len = XFASTINT (Flength (lisp_string));
20893 if (precision > 0 && len > precision)
20894 {
20895 len = precision;
20896 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20897 precision = -1;
20898 }
20899 if (!NILP (mode_line_string_face))
20900 {
20901 Lisp_Object face;
20902 if (NILP (props))
20903 props = Ftext_properties_at (make_number (0), lisp_string);
20904 face = Fplist_get (props, Qface);
20905 if (NILP (face))
20906 face = mode_line_string_face;
20907 else
20908 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20909 props = Fcons (Qface, Fcons (face, Qnil));
20910 if (copy_string)
20911 lisp_string = Fcopy_sequence (lisp_string);
20912 }
20913 if (!NILP (props))
20914 Fadd_text_properties (make_number (0), make_number (len),
20915 props, lisp_string);
20916 }
20917
20918 if (len > 0)
20919 {
20920 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20921 n += len;
20922 }
20923
20924 if (field_width > len)
20925 {
20926 field_width -= len;
20927 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20928 if (!NILP (props))
20929 Fadd_text_properties (make_number (0), make_number (field_width),
20930 props, lisp_string);
20931 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20932 n += field_width;
20933 }
20934
20935 return n;
20936 }
20937
20938
20939 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20940 1, 4, 0,
20941 doc: /* Format a string out of a mode line format specification.
20942 First arg FORMAT specifies the mode line format (see `mode-line-format'
20943 for details) to use.
20944
20945 By default, the format is evaluated for the currently selected window.
20946
20947 Optional second arg FACE specifies the face property to put on all
20948 characters for which no face is specified. The value nil means the
20949 default face. The value t means whatever face the window's mode line
20950 currently uses (either `mode-line' or `mode-line-inactive',
20951 depending on whether the window is the selected window or not).
20952 An integer value means the value string has no text
20953 properties.
20954
20955 Optional third and fourth args WINDOW and BUFFER specify the window
20956 and buffer to use as the context for the formatting (defaults
20957 are the selected window and the WINDOW's buffer). */)
20958 (Lisp_Object format, Lisp_Object face,
20959 Lisp_Object window, Lisp_Object buffer)
20960 {
20961 struct it it;
20962 int len;
20963 struct window *w;
20964 struct buffer *old_buffer = NULL;
20965 int face_id;
20966 int no_props = INTEGERP (face);
20967 ptrdiff_t count = SPECPDL_INDEX ();
20968 Lisp_Object str;
20969 int string_start = 0;
20970
20971 w = decode_any_window (window);
20972 XSETWINDOW (window, w);
20973
20974 if (NILP (buffer))
20975 buffer = w->contents;
20976 CHECK_BUFFER (buffer);
20977
20978 /* Make formatting the modeline a non-op when noninteractive, otherwise
20979 there will be problems later caused by a partially initialized frame. */
20980 if (NILP (format) || noninteractive)
20981 return empty_unibyte_string;
20982
20983 if (no_props)
20984 face = Qnil;
20985
20986 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20987 : EQ (face, Qt) ? (EQ (window, selected_window)
20988 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20989 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20990 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20991 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20992 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20993 : DEFAULT_FACE_ID;
20994
20995 old_buffer = current_buffer;
20996
20997 /* Save things including mode_line_proptrans_alist,
20998 and set that to nil so that we don't alter the outer value. */
20999 record_unwind_protect (unwind_format_mode_line,
21000 format_mode_line_unwind_data
21001 (XFRAME (WINDOW_FRAME (w)),
21002 old_buffer, selected_window, 1));
21003 mode_line_proptrans_alist = Qnil;
21004
21005 Fselect_window (window, Qt);
21006 set_buffer_internal_1 (XBUFFER (buffer));
21007
21008 init_iterator (&it, w, -1, -1, NULL, face_id);
21009
21010 if (no_props)
21011 {
21012 mode_line_target = MODE_LINE_NOPROP;
21013 mode_line_string_face_prop = Qnil;
21014 mode_line_string_list = Qnil;
21015 string_start = MODE_LINE_NOPROP_LEN (0);
21016 }
21017 else
21018 {
21019 mode_line_target = MODE_LINE_STRING;
21020 mode_line_string_list = Qnil;
21021 mode_line_string_face = face;
21022 mode_line_string_face_prop
21023 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21024 }
21025
21026 push_kboard (FRAME_KBOARD (it.f));
21027 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21028 pop_kboard ();
21029
21030 if (no_props)
21031 {
21032 len = MODE_LINE_NOPROP_LEN (string_start);
21033 str = make_string (mode_line_noprop_buf + string_start, len);
21034 }
21035 else
21036 {
21037 mode_line_string_list = Fnreverse (mode_line_string_list);
21038 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21039 empty_unibyte_string);
21040 }
21041
21042 unbind_to (count, Qnil);
21043 return str;
21044 }
21045
21046 /* Write a null-terminated, right justified decimal representation of
21047 the positive integer D to BUF using a minimal field width WIDTH. */
21048
21049 static void
21050 pint2str (register char *buf, register int width, register ptrdiff_t d)
21051 {
21052 register char *p = buf;
21053
21054 if (d <= 0)
21055 *p++ = '0';
21056 else
21057 {
21058 while (d > 0)
21059 {
21060 *p++ = d % 10 + '0';
21061 d /= 10;
21062 }
21063 }
21064
21065 for (width -= (int) (p - buf); width > 0; --width)
21066 *p++ = ' ';
21067 *p-- = '\0';
21068 while (p > buf)
21069 {
21070 d = *buf;
21071 *buf++ = *p;
21072 *p-- = d;
21073 }
21074 }
21075
21076 /* Write a null-terminated, right justified decimal and "human
21077 readable" representation of the nonnegative integer D to BUF using
21078 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21079
21080 static const char power_letter[] =
21081 {
21082 0, /* no letter */
21083 'k', /* kilo */
21084 'M', /* mega */
21085 'G', /* giga */
21086 'T', /* tera */
21087 'P', /* peta */
21088 'E', /* exa */
21089 'Z', /* zetta */
21090 'Y' /* yotta */
21091 };
21092
21093 static void
21094 pint2hrstr (char *buf, int width, ptrdiff_t d)
21095 {
21096 /* We aim to represent the nonnegative integer D as
21097 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21098 ptrdiff_t quotient = d;
21099 int remainder = 0;
21100 /* -1 means: do not use TENTHS. */
21101 int tenths = -1;
21102 int exponent = 0;
21103
21104 /* Length of QUOTIENT.TENTHS as a string. */
21105 int length;
21106
21107 char * psuffix;
21108 char * p;
21109
21110 if (quotient >= 1000)
21111 {
21112 /* Scale to the appropriate EXPONENT. */
21113 do
21114 {
21115 remainder = quotient % 1000;
21116 quotient /= 1000;
21117 exponent++;
21118 }
21119 while (quotient >= 1000);
21120
21121 /* Round to nearest and decide whether to use TENTHS or not. */
21122 if (quotient <= 9)
21123 {
21124 tenths = remainder / 100;
21125 if (remainder % 100 >= 50)
21126 {
21127 if (tenths < 9)
21128 tenths++;
21129 else
21130 {
21131 quotient++;
21132 if (quotient == 10)
21133 tenths = -1;
21134 else
21135 tenths = 0;
21136 }
21137 }
21138 }
21139 else
21140 if (remainder >= 500)
21141 {
21142 if (quotient < 999)
21143 quotient++;
21144 else
21145 {
21146 quotient = 1;
21147 exponent++;
21148 tenths = 0;
21149 }
21150 }
21151 }
21152
21153 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21154 if (tenths == -1 && quotient <= 99)
21155 if (quotient <= 9)
21156 length = 1;
21157 else
21158 length = 2;
21159 else
21160 length = 3;
21161 p = psuffix = buf + max (width, length);
21162
21163 /* Print EXPONENT. */
21164 *psuffix++ = power_letter[exponent];
21165 *psuffix = '\0';
21166
21167 /* Print TENTHS. */
21168 if (tenths >= 0)
21169 {
21170 *--p = '0' + tenths;
21171 *--p = '.';
21172 }
21173
21174 /* Print QUOTIENT. */
21175 do
21176 {
21177 int digit = quotient % 10;
21178 *--p = '0' + digit;
21179 }
21180 while ((quotient /= 10) != 0);
21181
21182 /* Print leading spaces. */
21183 while (buf < p)
21184 *--p = ' ';
21185 }
21186
21187 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21188 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21189 type of CODING_SYSTEM. Return updated pointer into BUF. */
21190
21191 static unsigned char invalid_eol_type[] = "(*invalid*)";
21192
21193 static char *
21194 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21195 {
21196 Lisp_Object val;
21197 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21198 const unsigned char *eol_str;
21199 int eol_str_len;
21200 /* The EOL conversion we are using. */
21201 Lisp_Object eoltype;
21202
21203 val = CODING_SYSTEM_SPEC (coding_system);
21204 eoltype = Qnil;
21205
21206 if (!VECTORP (val)) /* Not yet decided. */
21207 {
21208 *buf++ = multibyte ? '-' : ' ';
21209 if (eol_flag)
21210 eoltype = eol_mnemonic_undecided;
21211 /* Don't mention EOL conversion if it isn't decided. */
21212 }
21213 else
21214 {
21215 Lisp_Object attrs;
21216 Lisp_Object eolvalue;
21217
21218 attrs = AREF (val, 0);
21219 eolvalue = AREF (val, 2);
21220
21221 *buf++ = multibyte
21222 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21223 : ' ';
21224
21225 if (eol_flag)
21226 {
21227 /* The EOL conversion that is normal on this system. */
21228
21229 if (NILP (eolvalue)) /* Not yet decided. */
21230 eoltype = eol_mnemonic_undecided;
21231 else if (VECTORP (eolvalue)) /* Not yet decided. */
21232 eoltype = eol_mnemonic_undecided;
21233 else /* eolvalue is Qunix, Qdos, or Qmac. */
21234 eoltype = (EQ (eolvalue, Qunix)
21235 ? eol_mnemonic_unix
21236 : (EQ (eolvalue, Qdos) == 1
21237 ? eol_mnemonic_dos : eol_mnemonic_mac));
21238 }
21239 }
21240
21241 if (eol_flag)
21242 {
21243 /* Mention the EOL conversion if it is not the usual one. */
21244 if (STRINGP (eoltype))
21245 {
21246 eol_str = SDATA (eoltype);
21247 eol_str_len = SBYTES (eoltype);
21248 }
21249 else if (CHARACTERP (eoltype))
21250 {
21251 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21252 int c = XFASTINT (eoltype);
21253 eol_str_len = CHAR_STRING (c, tmp);
21254 eol_str = tmp;
21255 }
21256 else
21257 {
21258 eol_str = invalid_eol_type;
21259 eol_str_len = sizeof (invalid_eol_type) - 1;
21260 }
21261 memcpy (buf, eol_str, eol_str_len);
21262 buf += eol_str_len;
21263 }
21264
21265 return buf;
21266 }
21267
21268 /* Return a string for the output of a mode line %-spec for window W,
21269 generated by character C. FIELD_WIDTH > 0 means pad the string
21270 returned with spaces to that value. Return a Lisp string in
21271 *STRING if the resulting string is taken from that Lisp string.
21272
21273 Note we operate on the current buffer for most purposes. */
21274
21275 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21276
21277 static const char *
21278 decode_mode_spec (struct window *w, register int c, int field_width,
21279 Lisp_Object *string)
21280 {
21281 Lisp_Object obj;
21282 struct frame *f = XFRAME (WINDOW_FRAME (w));
21283 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21284 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21285 produce strings from numerical values, so limit preposterously
21286 large values of FIELD_WIDTH to avoid overrunning the buffer's
21287 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21288 bytes plus the terminating null. */
21289 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21290 struct buffer *b = current_buffer;
21291
21292 obj = Qnil;
21293 *string = Qnil;
21294
21295 switch (c)
21296 {
21297 case '*':
21298 if (!NILP (BVAR (b, read_only)))
21299 return "%";
21300 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21301 return "*";
21302 return "-";
21303
21304 case '+':
21305 /* This differs from %* only for a modified read-only buffer. */
21306 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21307 return "*";
21308 if (!NILP (BVAR (b, read_only)))
21309 return "%";
21310 return "-";
21311
21312 case '&':
21313 /* This differs from %* in ignoring read-only-ness. */
21314 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21315 return "*";
21316 return "-";
21317
21318 case '%':
21319 return "%";
21320
21321 case '[':
21322 {
21323 int i;
21324 char *p;
21325
21326 if (command_loop_level > 5)
21327 return "[[[... ";
21328 p = decode_mode_spec_buf;
21329 for (i = 0; i < command_loop_level; i++)
21330 *p++ = '[';
21331 *p = 0;
21332 return decode_mode_spec_buf;
21333 }
21334
21335 case ']':
21336 {
21337 int i;
21338 char *p;
21339
21340 if (command_loop_level > 5)
21341 return " ...]]]";
21342 p = decode_mode_spec_buf;
21343 for (i = 0; i < command_loop_level; i++)
21344 *p++ = ']';
21345 *p = 0;
21346 return decode_mode_spec_buf;
21347 }
21348
21349 case '-':
21350 {
21351 register int i;
21352
21353 /* Let lots_of_dashes be a string of infinite length. */
21354 if (mode_line_target == MODE_LINE_NOPROP
21355 || mode_line_target == MODE_LINE_STRING)
21356 return "--";
21357 if (field_width <= 0
21358 || field_width > sizeof (lots_of_dashes))
21359 {
21360 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21361 decode_mode_spec_buf[i] = '-';
21362 decode_mode_spec_buf[i] = '\0';
21363 return decode_mode_spec_buf;
21364 }
21365 else
21366 return lots_of_dashes;
21367 }
21368
21369 case 'b':
21370 obj = BVAR (b, name);
21371 break;
21372
21373 case 'c':
21374 /* %c and %l are ignored in `frame-title-format'.
21375 (In redisplay_internal, the frame title is drawn _before_ the
21376 windows are updated, so the stuff which depends on actual
21377 window contents (such as %l) may fail to render properly, or
21378 even crash emacs.) */
21379 if (mode_line_target == MODE_LINE_TITLE)
21380 return "";
21381 else
21382 {
21383 ptrdiff_t col = current_column ();
21384 w->column_number_displayed = col;
21385 pint2str (decode_mode_spec_buf, width, col);
21386 return decode_mode_spec_buf;
21387 }
21388
21389 case 'e':
21390 #ifndef SYSTEM_MALLOC
21391 {
21392 if (NILP (Vmemory_full))
21393 return "";
21394 else
21395 return "!MEM FULL! ";
21396 }
21397 #else
21398 return "";
21399 #endif
21400
21401 case 'F':
21402 /* %F displays the frame name. */
21403 if (!NILP (f->title))
21404 return SSDATA (f->title);
21405 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21406 return SSDATA (f->name);
21407 return "Emacs";
21408
21409 case 'f':
21410 obj = BVAR (b, filename);
21411 break;
21412
21413 case 'i':
21414 {
21415 ptrdiff_t size = ZV - BEGV;
21416 pint2str (decode_mode_spec_buf, width, size);
21417 return decode_mode_spec_buf;
21418 }
21419
21420 case 'I':
21421 {
21422 ptrdiff_t size = ZV - BEGV;
21423 pint2hrstr (decode_mode_spec_buf, width, size);
21424 return decode_mode_spec_buf;
21425 }
21426
21427 case 'l':
21428 {
21429 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21430 ptrdiff_t topline, nlines, height;
21431 ptrdiff_t junk;
21432
21433 /* %c and %l are ignored in `frame-title-format'. */
21434 if (mode_line_target == MODE_LINE_TITLE)
21435 return "";
21436
21437 startpos = marker_position (w->start);
21438 startpos_byte = marker_byte_position (w->start);
21439 height = WINDOW_TOTAL_LINES (w);
21440
21441 /* If we decided that this buffer isn't suitable for line numbers,
21442 don't forget that too fast. */
21443 if (w->base_line_pos == -1)
21444 goto no_value;
21445
21446 /* If the buffer is very big, don't waste time. */
21447 if (INTEGERP (Vline_number_display_limit)
21448 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21449 {
21450 w->base_line_pos = 0;
21451 w->base_line_number = 0;
21452 goto no_value;
21453 }
21454
21455 if (w->base_line_number > 0
21456 && w->base_line_pos > 0
21457 && w->base_line_pos <= startpos)
21458 {
21459 line = w->base_line_number;
21460 linepos = w->base_line_pos;
21461 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21462 }
21463 else
21464 {
21465 line = 1;
21466 linepos = BUF_BEGV (b);
21467 linepos_byte = BUF_BEGV_BYTE (b);
21468 }
21469
21470 /* Count lines from base line to window start position. */
21471 nlines = display_count_lines (linepos_byte,
21472 startpos_byte,
21473 startpos, &junk);
21474
21475 topline = nlines + line;
21476
21477 /* Determine a new base line, if the old one is too close
21478 or too far away, or if we did not have one.
21479 "Too close" means it's plausible a scroll-down would
21480 go back past it. */
21481 if (startpos == BUF_BEGV (b))
21482 {
21483 w->base_line_number = topline;
21484 w->base_line_pos = BUF_BEGV (b);
21485 }
21486 else if (nlines < height + 25 || nlines > height * 3 + 50
21487 || linepos == BUF_BEGV (b))
21488 {
21489 ptrdiff_t limit = BUF_BEGV (b);
21490 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21491 ptrdiff_t position;
21492 ptrdiff_t distance =
21493 (height * 2 + 30) * line_number_display_limit_width;
21494
21495 if (startpos - distance > limit)
21496 {
21497 limit = startpos - distance;
21498 limit_byte = CHAR_TO_BYTE (limit);
21499 }
21500
21501 nlines = display_count_lines (startpos_byte,
21502 limit_byte,
21503 - (height * 2 + 30),
21504 &position);
21505 /* If we couldn't find the lines we wanted within
21506 line_number_display_limit_width chars per line,
21507 give up on line numbers for this window. */
21508 if (position == limit_byte && limit == startpos - distance)
21509 {
21510 w->base_line_pos = -1;
21511 w->base_line_number = 0;
21512 goto no_value;
21513 }
21514
21515 w->base_line_number = topline - nlines;
21516 w->base_line_pos = BYTE_TO_CHAR (position);
21517 }
21518
21519 /* Now count lines from the start pos to point. */
21520 nlines = display_count_lines (startpos_byte,
21521 PT_BYTE, PT, &junk);
21522
21523 /* Record that we did display the line number. */
21524 line_number_displayed = 1;
21525
21526 /* Make the string to show. */
21527 pint2str (decode_mode_spec_buf, width, topline + nlines);
21528 return decode_mode_spec_buf;
21529 no_value:
21530 {
21531 char* p = decode_mode_spec_buf;
21532 int pad = width - 2;
21533 while (pad-- > 0)
21534 *p++ = ' ';
21535 *p++ = '?';
21536 *p++ = '?';
21537 *p = '\0';
21538 return decode_mode_spec_buf;
21539 }
21540 }
21541 break;
21542
21543 case 'm':
21544 obj = BVAR (b, mode_name);
21545 break;
21546
21547 case 'n':
21548 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21549 return " Narrow";
21550 break;
21551
21552 case 'p':
21553 {
21554 ptrdiff_t pos = marker_position (w->start);
21555 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21556
21557 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21558 {
21559 if (pos <= BUF_BEGV (b))
21560 return "All";
21561 else
21562 return "Bottom";
21563 }
21564 else if (pos <= BUF_BEGV (b))
21565 return "Top";
21566 else
21567 {
21568 if (total > 1000000)
21569 /* Do it differently for a large value, to avoid overflow. */
21570 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21571 else
21572 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21573 /* We can't normally display a 3-digit number,
21574 so get us a 2-digit number that is close. */
21575 if (total == 100)
21576 total = 99;
21577 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21578 return decode_mode_spec_buf;
21579 }
21580 }
21581
21582 /* Display percentage of size above the bottom of the screen. */
21583 case 'P':
21584 {
21585 ptrdiff_t toppos = marker_position (w->start);
21586 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21587 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21588
21589 if (botpos >= BUF_ZV (b))
21590 {
21591 if (toppos <= BUF_BEGV (b))
21592 return "All";
21593 else
21594 return "Bottom";
21595 }
21596 else
21597 {
21598 if (total > 1000000)
21599 /* Do it differently for a large value, to avoid overflow. */
21600 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21601 else
21602 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21603 /* We can't normally display a 3-digit number,
21604 so get us a 2-digit number that is close. */
21605 if (total == 100)
21606 total = 99;
21607 if (toppos <= BUF_BEGV (b))
21608 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21609 else
21610 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21611 return decode_mode_spec_buf;
21612 }
21613 }
21614
21615 case 's':
21616 /* status of process */
21617 obj = Fget_buffer_process (Fcurrent_buffer ());
21618 if (NILP (obj))
21619 return "no process";
21620 #ifndef MSDOS
21621 obj = Fsymbol_name (Fprocess_status (obj));
21622 #endif
21623 break;
21624
21625 case '@':
21626 {
21627 ptrdiff_t count = inhibit_garbage_collection ();
21628 Lisp_Object val = call1 (intern ("file-remote-p"),
21629 BVAR (current_buffer, directory));
21630 unbind_to (count, Qnil);
21631
21632 if (NILP (val))
21633 return "-";
21634 else
21635 return "@";
21636 }
21637
21638 case 'z':
21639 /* coding-system (not including end-of-line format) */
21640 case 'Z':
21641 /* coding-system (including end-of-line type) */
21642 {
21643 int eol_flag = (c == 'Z');
21644 char *p = decode_mode_spec_buf;
21645
21646 if (! FRAME_WINDOW_P (f))
21647 {
21648 /* No need to mention EOL here--the terminal never needs
21649 to do EOL conversion. */
21650 p = decode_mode_spec_coding (CODING_ID_NAME
21651 (FRAME_KEYBOARD_CODING (f)->id),
21652 p, 0);
21653 p = decode_mode_spec_coding (CODING_ID_NAME
21654 (FRAME_TERMINAL_CODING (f)->id),
21655 p, 0);
21656 }
21657 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21658 p, eol_flag);
21659
21660 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21661 #ifdef subprocesses
21662 obj = Fget_buffer_process (Fcurrent_buffer ());
21663 if (PROCESSP (obj))
21664 {
21665 p = decode_mode_spec_coding
21666 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21667 p = decode_mode_spec_coding
21668 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21669 }
21670 #endif /* subprocesses */
21671 #endif /* 0 */
21672 *p = 0;
21673 return decode_mode_spec_buf;
21674 }
21675 }
21676
21677 if (STRINGP (obj))
21678 {
21679 *string = obj;
21680 return SSDATA (obj);
21681 }
21682 else
21683 return "";
21684 }
21685
21686
21687 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21688 means count lines back from START_BYTE. But don't go beyond
21689 LIMIT_BYTE. Return the number of lines thus found (always
21690 nonnegative).
21691
21692 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21693 either the position COUNT lines after/before START_BYTE, if we
21694 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21695 COUNT lines. */
21696
21697 static ptrdiff_t
21698 display_count_lines (ptrdiff_t start_byte,
21699 ptrdiff_t limit_byte, ptrdiff_t count,
21700 ptrdiff_t *byte_pos_ptr)
21701 {
21702 register unsigned char *cursor;
21703 unsigned char *base;
21704
21705 register ptrdiff_t ceiling;
21706 register unsigned char *ceiling_addr;
21707 ptrdiff_t orig_count = count;
21708
21709 /* If we are not in selective display mode,
21710 check only for newlines. */
21711 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21712 && !INTEGERP (BVAR (current_buffer, selective_display)));
21713
21714 if (count > 0)
21715 {
21716 while (start_byte < limit_byte)
21717 {
21718 ceiling = BUFFER_CEILING_OF (start_byte);
21719 ceiling = min (limit_byte - 1, ceiling);
21720 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21721 base = (cursor = BYTE_POS_ADDR (start_byte));
21722
21723 do
21724 {
21725 if (selective_display)
21726 {
21727 while (*cursor != '\n' && *cursor != 015
21728 && ++cursor != ceiling_addr)
21729 continue;
21730 if (cursor == ceiling_addr)
21731 break;
21732 }
21733 else
21734 {
21735 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
21736 if (! cursor)
21737 break;
21738 }
21739
21740 cursor++;
21741
21742 if (--count == 0)
21743 {
21744 start_byte += cursor - base;
21745 *byte_pos_ptr = start_byte;
21746 return orig_count;
21747 }
21748 }
21749 while (cursor < ceiling_addr);
21750
21751 start_byte += ceiling_addr - base;
21752 }
21753 }
21754 else
21755 {
21756 while (start_byte > limit_byte)
21757 {
21758 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21759 ceiling = max (limit_byte, ceiling);
21760 ceiling_addr = BYTE_POS_ADDR (ceiling);
21761 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21762 while (1)
21763 {
21764 if (selective_display)
21765 {
21766 while (--cursor >= ceiling_addr
21767 && *cursor != '\n' && *cursor != 015)
21768 continue;
21769 if (cursor < ceiling_addr)
21770 break;
21771 }
21772 else
21773 {
21774 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
21775 if (! cursor)
21776 break;
21777 }
21778
21779 if (++count == 0)
21780 {
21781 start_byte += cursor - base + 1;
21782 *byte_pos_ptr = start_byte;
21783 /* When scanning backwards, we should
21784 not count the newline posterior to which we stop. */
21785 return - orig_count - 1;
21786 }
21787 }
21788 start_byte += ceiling_addr - base;
21789 }
21790 }
21791
21792 *byte_pos_ptr = limit_byte;
21793
21794 if (count < 0)
21795 return - orig_count + count;
21796 return orig_count - count;
21797
21798 }
21799
21800
21801 \f
21802 /***********************************************************************
21803 Displaying strings
21804 ***********************************************************************/
21805
21806 /* Display a NUL-terminated string, starting with index START.
21807
21808 If STRING is non-null, display that C string. Otherwise, the Lisp
21809 string LISP_STRING is displayed. There's a case that STRING is
21810 non-null and LISP_STRING is not nil. It means STRING is a string
21811 data of LISP_STRING. In that case, we display LISP_STRING while
21812 ignoring its text properties.
21813
21814 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21815 FACE_STRING. Display STRING or LISP_STRING with the face at
21816 FACE_STRING_POS in FACE_STRING:
21817
21818 Display the string in the environment given by IT, but use the
21819 standard display table, temporarily.
21820
21821 FIELD_WIDTH is the minimum number of output glyphs to produce.
21822 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21823 with spaces. If STRING has more characters, more than FIELD_WIDTH
21824 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21825
21826 PRECISION is the maximum number of characters to output from
21827 STRING. PRECISION < 0 means don't truncate the string.
21828
21829 This is roughly equivalent to printf format specifiers:
21830
21831 FIELD_WIDTH PRECISION PRINTF
21832 ----------------------------------------
21833 -1 -1 %s
21834 -1 10 %.10s
21835 10 -1 %10s
21836 20 10 %20.10s
21837
21838 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21839 display them, and < 0 means obey the current buffer's value of
21840 enable_multibyte_characters.
21841
21842 Value is the number of columns displayed. */
21843
21844 static int
21845 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21846 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21847 int field_width, int precision, int max_x, int multibyte)
21848 {
21849 int hpos_at_start = it->hpos;
21850 int saved_face_id = it->face_id;
21851 struct glyph_row *row = it->glyph_row;
21852 ptrdiff_t it_charpos;
21853
21854 /* Initialize the iterator IT for iteration over STRING beginning
21855 with index START. */
21856 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21857 precision, field_width, multibyte);
21858 if (string && STRINGP (lisp_string))
21859 /* LISP_STRING is the one returned by decode_mode_spec. We should
21860 ignore its text properties. */
21861 it->stop_charpos = it->end_charpos;
21862
21863 /* If displaying STRING, set up the face of the iterator from
21864 FACE_STRING, if that's given. */
21865 if (STRINGP (face_string))
21866 {
21867 ptrdiff_t endptr;
21868 struct face *face;
21869
21870 it->face_id
21871 = face_at_string_position (it->w, face_string, face_string_pos,
21872 0, it->region_beg_charpos,
21873 it->region_end_charpos,
21874 &endptr, it->base_face_id, 0);
21875 face = FACE_FROM_ID (it->f, it->face_id);
21876 it->face_box_p = face->box != FACE_NO_BOX;
21877 }
21878
21879 /* Set max_x to the maximum allowed X position. Don't let it go
21880 beyond the right edge of the window. */
21881 if (max_x <= 0)
21882 max_x = it->last_visible_x;
21883 else
21884 max_x = min (max_x, it->last_visible_x);
21885
21886 /* Skip over display elements that are not visible. because IT->w is
21887 hscrolled. */
21888 if (it->current_x < it->first_visible_x)
21889 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21890 MOVE_TO_POS | MOVE_TO_X);
21891
21892 row->ascent = it->max_ascent;
21893 row->height = it->max_ascent + it->max_descent;
21894 row->phys_ascent = it->max_phys_ascent;
21895 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21896 row->extra_line_spacing = it->max_extra_line_spacing;
21897
21898 if (STRINGP (it->string))
21899 it_charpos = IT_STRING_CHARPOS (*it);
21900 else
21901 it_charpos = IT_CHARPOS (*it);
21902
21903 /* This condition is for the case that we are called with current_x
21904 past last_visible_x. */
21905 while (it->current_x < max_x)
21906 {
21907 int x_before, x, n_glyphs_before, i, nglyphs;
21908
21909 /* Get the next display element. */
21910 if (!get_next_display_element (it))
21911 break;
21912
21913 /* Produce glyphs. */
21914 x_before = it->current_x;
21915 n_glyphs_before = row->used[TEXT_AREA];
21916 PRODUCE_GLYPHS (it);
21917
21918 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21919 i = 0;
21920 x = x_before;
21921 while (i < nglyphs)
21922 {
21923 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21924
21925 if (it->line_wrap != TRUNCATE
21926 && x + glyph->pixel_width > max_x)
21927 {
21928 /* End of continued line or max_x reached. */
21929 if (CHAR_GLYPH_PADDING_P (*glyph))
21930 {
21931 /* A wide character is unbreakable. */
21932 if (row->reversed_p)
21933 unproduce_glyphs (it, row->used[TEXT_AREA]
21934 - n_glyphs_before);
21935 row->used[TEXT_AREA] = n_glyphs_before;
21936 it->current_x = x_before;
21937 }
21938 else
21939 {
21940 if (row->reversed_p)
21941 unproduce_glyphs (it, row->used[TEXT_AREA]
21942 - (n_glyphs_before + i));
21943 row->used[TEXT_AREA] = n_glyphs_before + i;
21944 it->current_x = x;
21945 }
21946 break;
21947 }
21948 else if (x + glyph->pixel_width >= it->first_visible_x)
21949 {
21950 /* Glyph is at least partially visible. */
21951 ++it->hpos;
21952 if (x < it->first_visible_x)
21953 row->x = x - it->first_visible_x;
21954 }
21955 else
21956 {
21957 /* Glyph is off the left margin of the display area.
21958 Should not happen. */
21959 emacs_abort ();
21960 }
21961
21962 row->ascent = max (row->ascent, it->max_ascent);
21963 row->height = max (row->height, it->max_ascent + it->max_descent);
21964 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21965 row->phys_height = max (row->phys_height,
21966 it->max_phys_ascent + it->max_phys_descent);
21967 row->extra_line_spacing = max (row->extra_line_spacing,
21968 it->max_extra_line_spacing);
21969 x += glyph->pixel_width;
21970 ++i;
21971 }
21972
21973 /* Stop if max_x reached. */
21974 if (i < nglyphs)
21975 break;
21976
21977 /* Stop at line ends. */
21978 if (ITERATOR_AT_END_OF_LINE_P (it))
21979 {
21980 it->continuation_lines_width = 0;
21981 break;
21982 }
21983
21984 set_iterator_to_next (it, 1);
21985 if (STRINGP (it->string))
21986 it_charpos = IT_STRING_CHARPOS (*it);
21987 else
21988 it_charpos = IT_CHARPOS (*it);
21989
21990 /* Stop if truncating at the right edge. */
21991 if (it->line_wrap == TRUNCATE
21992 && it->current_x >= it->last_visible_x)
21993 {
21994 /* Add truncation mark, but don't do it if the line is
21995 truncated at a padding space. */
21996 if (it_charpos < it->string_nchars)
21997 {
21998 if (!FRAME_WINDOW_P (it->f))
21999 {
22000 int ii, n;
22001
22002 if (it->current_x > it->last_visible_x)
22003 {
22004 if (!row->reversed_p)
22005 {
22006 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22007 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22008 break;
22009 }
22010 else
22011 {
22012 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22013 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22014 break;
22015 unproduce_glyphs (it, ii + 1);
22016 ii = row->used[TEXT_AREA] - (ii + 1);
22017 }
22018 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22019 {
22020 row->used[TEXT_AREA] = ii;
22021 produce_special_glyphs (it, IT_TRUNCATION);
22022 }
22023 }
22024 produce_special_glyphs (it, IT_TRUNCATION);
22025 }
22026 row->truncated_on_right_p = 1;
22027 }
22028 break;
22029 }
22030 }
22031
22032 /* Maybe insert a truncation at the left. */
22033 if (it->first_visible_x
22034 && it_charpos > 0)
22035 {
22036 if (!FRAME_WINDOW_P (it->f)
22037 || (row->reversed_p
22038 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22039 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22040 insert_left_trunc_glyphs (it);
22041 row->truncated_on_left_p = 1;
22042 }
22043
22044 it->face_id = saved_face_id;
22045
22046 /* Value is number of columns displayed. */
22047 return it->hpos - hpos_at_start;
22048 }
22049
22050
22051 \f
22052 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22053 appears as an element of LIST or as the car of an element of LIST.
22054 If PROPVAL is a list, compare each element against LIST in that
22055 way, and return 1/2 if any element of PROPVAL is found in LIST.
22056 Otherwise return 0. This function cannot quit.
22057 The return value is 2 if the text is invisible but with an ellipsis
22058 and 1 if it's invisible and without an ellipsis. */
22059
22060 int
22061 invisible_p (register Lisp_Object propval, Lisp_Object list)
22062 {
22063 register Lisp_Object tail, proptail;
22064
22065 for (tail = list; CONSP (tail); tail = XCDR (tail))
22066 {
22067 register Lisp_Object tem;
22068 tem = XCAR (tail);
22069 if (EQ (propval, tem))
22070 return 1;
22071 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22072 return NILP (XCDR (tem)) ? 1 : 2;
22073 }
22074
22075 if (CONSP (propval))
22076 {
22077 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22078 {
22079 Lisp_Object propelt;
22080 propelt = XCAR (proptail);
22081 for (tail = list; CONSP (tail); tail = XCDR (tail))
22082 {
22083 register Lisp_Object tem;
22084 tem = XCAR (tail);
22085 if (EQ (propelt, tem))
22086 return 1;
22087 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22088 return NILP (XCDR (tem)) ? 1 : 2;
22089 }
22090 }
22091 }
22092
22093 return 0;
22094 }
22095
22096 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22097 doc: /* Non-nil if the property makes the text invisible.
22098 POS-OR-PROP can be a marker or number, in which case it is taken to be
22099 a position in the current buffer and the value of the `invisible' property
22100 is checked; or it can be some other value, which is then presumed to be the
22101 value of the `invisible' property of the text of interest.
22102 The non-nil value returned can be t for truly invisible text or something
22103 else if the text is replaced by an ellipsis. */)
22104 (Lisp_Object pos_or_prop)
22105 {
22106 Lisp_Object prop
22107 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22108 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22109 : pos_or_prop);
22110 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22111 return (invis == 0 ? Qnil
22112 : invis == 1 ? Qt
22113 : make_number (invis));
22114 }
22115
22116 /* Calculate a width or height in pixels from a specification using
22117 the following elements:
22118
22119 SPEC ::=
22120 NUM - a (fractional) multiple of the default font width/height
22121 (NUM) - specifies exactly NUM pixels
22122 UNIT - a fixed number of pixels, see below.
22123 ELEMENT - size of a display element in pixels, see below.
22124 (NUM . SPEC) - equals NUM * SPEC
22125 (+ SPEC SPEC ...) - add pixel values
22126 (- SPEC SPEC ...) - subtract pixel values
22127 (- SPEC) - negate pixel value
22128
22129 NUM ::=
22130 INT or FLOAT - a number constant
22131 SYMBOL - use symbol's (buffer local) variable binding.
22132
22133 UNIT ::=
22134 in - pixels per inch *)
22135 mm - pixels per 1/1000 meter *)
22136 cm - pixels per 1/100 meter *)
22137 width - width of current font in pixels.
22138 height - height of current font in pixels.
22139
22140 *) using the ratio(s) defined in display-pixels-per-inch.
22141
22142 ELEMENT ::=
22143
22144 left-fringe - left fringe width in pixels
22145 right-fringe - right fringe width in pixels
22146
22147 left-margin - left margin width in pixels
22148 right-margin - right margin width in pixels
22149
22150 scroll-bar - scroll-bar area width in pixels
22151
22152 Examples:
22153
22154 Pixels corresponding to 5 inches:
22155 (5 . in)
22156
22157 Total width of non-text areas on left side of window (if scroll-bar is on left):
22158 '(space :width (+ left-fringe left-margin scroll-bar))
22159
22160 Align to first text column (in header line):
22161 '(space :align-to 0)
22162
22163 Align to middle of text area minus half the width of variable `my-image'
22164 containing a loaded image:
22165 '(space :align-to (0.5 . (- text my-image)))
22166
22167 Width of left margin minus width of 1 character in the default font:
22168 '(space :width (- left-margin 1))
22169
22170 Width of left margin minus width of 2 characters in the current font:
22171 '(space :width (- left-margin (2 . width)))
22172
22173 Center 1 character over left-margin (in header line):
22174 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22175
22176 Different ways to express width of left fringe plus left margin minus one pixel:
22177 '(space :width (- (+ left-fringe left-margin) (1)))
22178 '(space :width (+ left-fringe left-margin (- (1))))
22179 '(space :width (+ left-fringe left-margin (-1)))
22180
22181 */
22182
22183 static int
22184 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22185 struct font *font, int width_p, int *align_to)
22186 {
22187 double pixels;
22188
22189 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22190 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22191
22192 if (NILP (prop))
22193 return OK_PIXELS (0);
22194
22195 eassert (FRAME_LIVE_P (it->f));
22196
22197 if (SYMBOLP (prop))
22198 {
22199 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22200 {
22201 char *unit = SSDATA (SYMBOL_NAME (prop));
22202
22203 if (unit[0] == 'i' && unit[1] == 'n')
22204 pixels = 1.0;
22205 else if (unit[0] == 'm' && unit[1] == 'm')
22206 pixels = 25.4;
22207 else if (unit[0] == 'c' && unit[1] == 'm')
22208 pixels = 2.54;
22209 else
22210 pixels = 0;
22211 if (pixels > 0)
22212 {
22213 double ppi = (width_p ? FRAME_RES_X (it->f)
22214 : FRAME_RES_Y (it->f));
22215
22216 if (ppi > 0)
22217 return OK_PIXELS (ppi / pixels);
22218 return 0;
22219 }
22220 }
22221
22222 #ifdef HAVE_WINDOW_SYSTEM
22223 if (EQ (prop, Qheight))
22224 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22225 if (EQ (prop, Qwidth))
22226 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22227 #else
22228 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22229 return OK_PIXELS (1);
22230 #endif
22231
22232 if (EQ (prop, Qtext))
22233 return OK_PIXELS (width_p
22234 ? window_box_width (it->w, TEXT_AREA)
22235 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22236
22237 if (align_to && *align_to < 0)
22238 {
22239 *res = 0;
22240 if (EQ (prop, Qleft))
22241 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22242 if (EQ (prop, Qright))
22243 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22244 if (EQ (prop, Qcenter))
22245 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22246 + window_box_width (it->w, TEXT_AREA) / 2);
22247 if (EQ (prop, Qleft_fringe))
22248 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22249 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22250 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22251 if (EQ (prop, Qright_fringe))
22252 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22253 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22254 : window_box_right_offset (it->w, TEXT_AREA));
22255 if (EQ (prop, Qleft_margin))
22256 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22257 if (EQ (prop, Qright_margin))
22258 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22259 if (EQ (prop, Qscroll_bar))
22260 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22261 ? 0
22262 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22263 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22264 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22265 : 0)));
22266 }
22267 else
22268 {
22269 if (EQ (prop, Qleft_fringe))
22270 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22271 if (EQ (prop, Qright_fringe))
22272 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22273 if (EQ (prop, Qleft_margin))
22274 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22275 if (EQ (prop, Qright_margin))
22276 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22277 if (EQ (prop, Qscroll_bar))
22278 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22279 }
22280
22281 prop = buffer_local_value_1 (prop, it->w->contents);
22282 if (EQ (prop, Qunbound))
22283 prop = Qnil;
22284 }
22285
22286 if (INTEGERP (prop) || FLOATP (prop))
22287 {
22288 int base_unit = (width_p
22289 ? FRAME_COLUMN_WIDTH (it->f)
22290 : FRAME_LINE_HEIGHT (it->f));
22291 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22292 }
22293
22294 if (CONSP (prop))
22295 {
22296 Lisp_Object car = XCAR (prop);
22297 Lisp_Object cdr = XCDR (prop);
22298
22299 if (SYMBOLP (car))
22300 {
22301 #ifdef HAVE_WINDOW_SYSTEM
22302 if (FRAME_WINDOW_P (it->f)
22303 && valid_image_p (prop))
22304 {
22305 ptrdiff_t id = lookup_image (it->f, prop);
22306 struct image *img = IMAGE_FROM_ID (it->f, id);
22307
22308 return OK_PIXELS (width_p ? img->width : img->height);
22309 }
22310 #ifdef HAVE_XWIDGETS
22311 if (FRAME_WINDOW_P (it->f) && valid_xwidget_p (prop))
22312 {
22313 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
22314 return OK_PIXELS (width_p ? 100 : 100);
22315 }
22316 #endif
22317 #endif
22318 if (EQ (car, Qplus) || EQ (car, Qminus))
22319 {
22320 int first = 1;
22321 double px;
22322
22323 pixels = 0;
22324 while (CONSP (cdr))
22325 {
22326 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22327 font, width_p, align_to))
22328 return 0;
22329 if (first)
22330 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22331 else
22332 pixels += px;
22333 cdr = XCDR (cdr);
22334 }
22335 if (EQ (car, Qminus))
22336 pixels = -pixels;
22337 return OK_PIXELS (pixels);
22338 }
22339
22340 car = buffer_local_value_1 (car, it->w->contents);
22341 if (EQ (car, Qunbound))
22342 car = Qnil;
22343 }
22344
22345 if (INTEGERP (car) || FLOATP (car))
22346 {
22347 double fact;
22348 pixels = XFLOATINT (car);
22349 if (NILP (cdr))
22350 return OK_PIXELS (pixels);
22351 if (calc_pixel_width_or_height (&fact, it, cdr,
22352 font, width_p, align_to))
22353 return OK_PIXELS (pixels * fact);
22354 return 0;
22355 }
22356
22357 return 0;
22358 }
22359
22360 return 0;
22361 }
22362
22363 \f
22364 /***********************************************************************
22365 Glyph Display
22366 ***********************************************************************/
22367
22368 #ifdef HAVE_WINDOW_SYSTEM
22369
22370 #ifdef GLYPH_DEBUG
22371
22372 void
22373 dump_glyph_string (struct glyph_string *s)
22374 {
22375 fprintf (stderr, "glyph string\n");
22376 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22377 s->x, s->y, s->width, s->height);
22378 fprintf (stderr, " ybase = %d\n", s->ybase);
22379 fprintf (stderr, " hl = %d\n", s->hl);
22380 fprintf (stderr, " left overhang = %d, right = %d\n",
22381 s->left_overhang, s->right_overhang);
22382 fprintf (stderr, " nchars = %d\n", s->nchars);
22383 fprintf (stderr, " extends to end of line = %d\n",
22384 s->extends_to_end_of_line_p);
22385 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22386 fprintf (stderr, " bg width = %d\n", s->background_width);
22387 }
22388
22389 #endif /* GLYPH_DEBUG */
22390
22391 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22392 of XChar2b structures for S; it can't be allocated in
22393 init_glyph_string because it must be allocated via `alloca'. W
22394 is the window on which S is drawn. ROW and AREA are the glyph row
22395 and area within the row from which S is constructed. START is the
22396 index of the first glyph structure covered by S. HL is a
22397 face-override for drawing S. */
22398
22399 #ifdef HAVE_NTGUI
22400 #define OPTIONAL_HDC(hdc) HDC hdc,
22401 #define DECLARE_HDC(hdc) HDC hdc;
22402 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22403 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22404 #endif
22405
22406 #ifndef OPTIONAL_HDC
22407 #define OPTIONAL_HDC(hdc)
22408 #define DECLARE_HDC(hdc)
22409 #define ALLOCATE_HDC(hdc, f)
22410 #define RELEASE_HDC(hdc, f)
22411 #endif
22412
22413 static void
22414 init_glyph_string (struct glyph_string *s,
22415 OPTIONAL_HDC (hdc)
22416 XChar2b *char2b, struct window *w, struct glyph_row *row,
22417 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22418 {
22419 memset (s, 0, sizeof *s);
22420 s->w = w;
22421 s->f = XFRAME (w->frame);
22422 #ifdef HAVE_NTGUI
22423 s->hdc = hdc;
22424 #endif
22425 s->display = FRAME_X_DISPLAY (s->f);
22426 s->window = FRAME_X_WINDOW (s->f);
22427 s->char2b = char2b;
22428 s->hl = hl;
22429 s->row = row;
22430 s->area = area;
22431 s->first_glyph = row->glyphs[area] + start;
22432 s->height = row->height;
22433 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22434 s->ybase = s->y + row->ascent;
22435 }
22436
22437
22438 /* Append the list of glyph strings with head H and tail T to the list
22439 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22440
22441 static void
22442 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22443 struct glyph_string *h, struct glyph_string *t)
22444 {
22445 if (h)
22446 {
22447 if (*head)
22448 (*tail)->next = h;
22449 else
22450 *head = h;
22451 h->prev = *tail;
22452 *tail = t;
22453 }
22454 }
22455
22456
22457 /* Prepend the list of glyph strings with head H and tail T to the
22458 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22459 result. */
22460
22461 static void
22462 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22463 struct glyph_string *h, struct glyph_string *t)
22464 {
22465 if (h)
22466 {
22467 if (*head)
22468 (*head)->prev = t;
22469 else
22470 *tail = t;
22471 t->next = *head;
22472 *head = h;
22473 }
22474 }
22475
22476
22477 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22478 Set *HEAD and *TAIL to the resulting list. */
22479
22480 static void
22481 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22482 struct glyph_string *s)
22483 {
22484 s->next = s->prev = NULL;
22485 append_glyph_string_lists (head, tail, s, s);
22486 }
22487
22488
22489 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22490 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22491 make sure that X resources for the face returned are allocated.
22492 Value is a pointer to a realized face that is ready for display if
22493 DISPLAY_P is non-zero. */
22494
22495 static struct face *
22496 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22497 XChar2b *char2b, int display_p)
22498 {
22499 struct face *face = FACE_FROM_ID (f, face_id);
22500 unsigned code = 0;
22501
22502 if (face->font)
22503 {
22504 code = face->font->driver->encode_char (face->font, c);
22505
22506 if (code == FONT_INVALID_CODE)
22507 code = 0;
22508 }
22509 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22510
22511 /* Make sure X resources of the face are allocated. */
22512 #ifdef HAVE_X_WINDOWS
22513 if (display_p)
22514 #endif
22515 {
22516 eassert (face != NULL);
22517 PREPARE_FACE_FOR_DISPLAY (f, face);
22518 }
22519
22520 return face;
22521 }
22522
22523
22524 /* Get face and two-byte form of character glyph GLYPH on frame F.
22525 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22526 a pointer to a realized face that is ready for display. */
22527
22528 static struct face *
22529 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22530 XChar2b *char2b, int *two_byte_p)
22531 {
22532 struct face *face;
22533 unsigned code = 0;
22534
22535 eassert (glyph->type == CHAR_GLYPH);
22536 face = FACE_FROM_ID (f, glyph->face_id);
22537
22538 /* Make sure X resources of the face are allocated. */
22539 eassert (face != NULL);
22540 PREPARE_FACE_FOR_DISPLAY (f, face);
22541
22542 if (two_byte_p)
22543 *two_byte_p = 0;
22544
22545 if (face->font)
22546 {
22547 if (CHAR_BYTE8_P (glyph->u.ch))
22548 code = CHAR_TO_BYTE8 (glyph->u.ch);
22549 else
22550 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22551
22552 if (code == FONT_INVALID_CODE)
22553 code = 0;
22554 }
22555
22556 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22557 return face;
22558 }
22559
22560
22561 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22562 Return 1 if FONT has a glyph for C, otherwise return 0. */
22563
22564 static int
22565 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22566 {
22567 unsigned code;
22568
22569 if (CHAR_BYTE8_P (c))
22570 code = CHAR_TO_BYTE8 (c);
22571 else
22572 code = font->driver->encode_char (font, c);
22573
22574 if (code == FONT_INVALID_CODE)
22575 return 0;
22576 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22577 return 1;
22578 }
22579
22580
22581 /* Fill glyph string S with composition components specified by S->cmp.
22582
22583 BASE_FACE is the base face of the composition.
22584 S->cmp_from is the index of the first component for S.
22585
22586 OVERLAPS non-zero means S should draw the foreground only, and use
22587 its physical height for clipping. See also draw_glyphs.
22588
22589 Value is the index of a component not in S. */
22590
22591 static int
22592 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22593 int overlaps)
22594 {
22595 int i;
22596 /* For all glyphs of this composition, starting at the offset
22597 S->cmp_from, until we reach the end of the definition or encounter a
22598 glyph that requires the different face, add it to S. */
22599 struct face *face;
22600
22601 eassert (s);
22602
22603 s->for_overlaps = overlaps;
22604 s->face = NULL;
22605 s->font = NULL;
22606 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22607 {
22608 int c = COMPOSITION_GLYPH (s->cmp, i);
22609
22610 /* TAB in a composition means display glyphs with padding space
22611 on the left or right. */
22612 if (c != '\t')
22613 {
22614 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22615 -1, Qnil);
22616
22617 face = get_char_face_and_encoding (s->f, c, face_id,
22618 s->char2b + i, 1);
22619 if (face)
22620 {
22621 if (! s->face)
22622 {
22623 s->face = face;
22624 s->font = s->face->font;
22625 }
22626 else if (s->face != face)
22627 break;
22628 }
22629 }
22630 ++s->nchars;
22631 }
22632 s->cmp_to = i;
22633
22634 if (s->face == NULL)
22635 {
22636 s->face = base_face->ascii_face;
22637 s->font = s->face->font;
22638 }
22639
22640 /* All glyph strings for the same composition has the same width,
22641 i.e. the width set for the first component of the composition. */
22642 s->width = s->first_glyph->pixel_width;
22643
22644 /* If the specified font could not be loaded, use the frame's
22645 default font, but record the fact that we couldn't load it in
22646 the glyph string so that we can draw rectangles for the
22647 characters of the glyph string. */
22648 if (s->font == NULL)
22649 {
22650 s->font_not_found_p = 1;
22651 s->font = FRAME_FONT (s->f);
22652 }
22653
22654 /* Adjust base line for subscript/superscript text. */
22655 s->ybase += s->first_glyph->voffset;
22656
22657 /* This glyph string must always be drawn with 16-bit functions. */
22658 s->two_byte_p = 1;
22659
22660 return s->cmp_to;
22661 }
22662
22663 static int
22664 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22665 int start, int end, int overlaps)
22666 {
22667 struct glyph *glyph, *last;
22668 Lisp_Object lgstring;
22669 int i;
22670
22671 s->for_overlaps = overlaps;
22672 glyph = s->row->glyphs[s->area] + start;
22673 last = s->row->glyphs[s->area] + end;
22674 s->cmp_id = glyph->u.cmp.id;
22675 s->cmp_from = glyph->slice.cmp.from;
22676 s->cmp_to = glyph->slice.cmp.to + 1;
22677 s->face = FACE_FROM_ID (s->f, face_id);
22678 lgstring = composition_gstring_from_id (s->cmp_id);
22679 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22680 glyph++;
22681 while (glyph < last
22682 && glyph->u.cmp.automatic
22683 && glyph->u.cmp.id == s->cmp_id
22684 && s->cmp_to == glyph->slice.cmp.from)
22685 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22686
22687 for (i = s->cmp_from; i < s->cmp_to; i++)
22688 {
22689 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22690 unsigned code = LGLYPH_CODE (lglyph);
22691
22692 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22693 }
22694 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22695 return glyph - s->row->glyphs[s->area];
22696 }
22697
22698
22699 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22700 See the comment of fill_glyph_string for arguments.
22701 Value is the index of the first glyph not in S. */
22702
22703
22704 static int
22705 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22706 int start, int end, int overlaps)
22707 {
22708 struct glyph *glyph, *last;
22709 int voffset;
22710
22711 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22712 s->for_overlaps = overlaps;
22713 glyph = s->row->glyphs[s->area] + start;
22714 last = s->row->glyphs[s->area] + end;
22715 voffset = glyph->voffset;
22716 s->face = FACE_FROM_ID (s->f, face_id);
22717 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22718 s->nchars = 1;
22719 s->width = glyph->pixel_width;
22720 glyph++;
22721 while (glyph < last
22722 && glyph->type == GLYPHLESS_GLYPH
22723 && glyph->voffset == voffset
22724 && glyph->face_id == face_id)
22725 {
22726 s->nchars++;
22727 s->width += glyph->pixel_width;
22728 glyph++;
22729 }
22730 s->ybase += voffset;
22731 return glyph - s->row->glyphs[s->area];
22732 }
22733
22734
22735 /* Fill glyph string S from a sequence of character glyphs.
22736
22737 FACE_ID is the face id of the string. START is the index of the
22738 first glyph to consider, END is the index of the last + 1.
22739 OVERLAPS non-zero means S should draw the foreground only, and use
22740 its physical height for clipping. See also draw_glyphs.
22741
22742 Value is the index of the first glyph not in S. */
22743
22744 static int
22745 fill_glyph_string (struct glyph_string *s, int face_id,
22746 int start, int end, int overlaps)
22747 {
22748 struct glyph *glyph, *last;
22749 int voffset;
22750 int glyph_not_available_p;
22751
22752 eassert (s->f == XFRAME (s->w->frame));
22753 eassert (s->nchars == 0);
22754 eassert (start >= 0 && end > start);
22755
22756 s->for_overlaps = overlaps;
22757 glyph = s->row->glyphs[s->area] + start;
22758 last = s->row->glyphs[s->area] + end;
22759 voffset = glyph->voffset;
22760 s->padding_p = glyph->padding_p;
22761 glyph_not_available_p = glyph->glyph_not_available_p;
22762
22763 while (glyph < last
22764 && glyph->type == CHAR_GLYPH
22765 && glyph->voffset == voffset
22766 /* Same face id implies same font, nowadays. */
22767 && glyph->face_id == face_id
22768 && glyph->glyph_not_available_p == glyph_not_available_p)
22769 {
22770 int two_byte_p;
22771
22772 s->face = get_glyph_face_and_encoding (s->f, glyph,
22773 s->char2b + s->nchars,
22774 &two_byte_p);
22775 s->two_byte_p = two_byte_p;
22776 ++s->nchars;
22777 eassert (s->nchars <= end - start);
22778 s->width += glyph->pixel_width;
22779 if (glyph++->padding_p != s->padding_p)
22780 break;
22781 }
22782
22783 s->font = s->face->font;
22784
22785 /* If the specified font could not be loaded, use the frame's font,
22786 but record the fact that we couldn't load it in
22787 S->font_not_found_p so that we can draw rectangles for the
22788 characters of the glyph string. */
22789 if (s->font == NULL || glyph_not_available_p)
22790 {
22791 s->font_not_found_p = 1;
22792 s->font = FRAME_FONT (s->f);
22793 }
22794
22795 /* Adjust base line for subscript/superscript text. */
22796 s->ybase += voffset;
22797
22798 eassert (s->face && s->face->gc);
22799 return glyph - s->row->glyphs[s->area];
22800 }
22801
22802
22803 /* Fill glyph string S from image glyph S->first_glyph. */
22804
22805 static void
22806 fill_image_glyph_string (struct glyph_string *s)
22807 {
22808 eassert (s->first_glyph->type == IMAGE_GLYPH);
22809 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22810 eassert (s->img);
22811 s->slice = s->first_glyph->slice.img;
22812 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22813 s->font = s->face->font;
22814 s->width = s->first_glyph->pixel_width;
22815
22816 /* Adjust base line for subscript/superscript text. */
22817 s->ybase += s->first_glyph->voffset;
22818 }
22819
22820 #ifdef HAVE_XWIDGETS
22821 static void
22822 fill_xwidget_glyph_string (struct glyph_string *s)
22823 {
22824 eassert (s->first_glyph->type == XWIDGET_GLYPH);
22825 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
22826 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22827 s->font = s->face->font;
22828 s->width = s->first_glyph->pixel_width;
22829 s->ybase += s->first_glyph->voffset;
22830 s->xwidget = s->first_glyph->u.xwidget;
22831 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
22832 }
22833 #endif
22834 /* Fill glyph string S from a sequence of stretch glyphs.
22835
22836 START is the index of the first glyph to consider,
22837 END is the index of the last + 1.
22838
22839 Value is the index of the first glyph not in S. */
22840
22841 static int
22842 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22843 {
22844 struct glyph *glyph, *last;
22845 int voffset, face_id;
22846
22847 eassert (s->first_glyph->type == STRETCH_GLYPH);
22848
22849 glyph = s->row->glyphs[s->area] + start;
22850 last = s->row->glyphs[s->area] + end;
22851 face_id = glyph->face_id;
22852 s->face = FACE_FROM_ID (s->f, face_id);
22853 s->font = s->face->font;
22854 s->width = glyph->pixel_width;
22855 s->nchars = 1;
22856 voffset = glyph->voffset;
22857
22858 for (++glyph;
22859 (glyph < last
22860 && glyph->type == STRETCH_GLYPH
22861 && glyph->voffset == voffset
22862 && glyph->face_id == face_id);
22863 ++glyph)
22864 s->width += glyph->pixel_width;
22865
22866 /* Adjust base line for subscript/superscript text. */
22867 s->ybase += voffset;
22868
22869 /* The case that face->gc == 0 is handled when drawing the glyph
22870 string by calling PREPARE_FACE_FOR_DISPLAY. */
22871 eassert (s->face);
22872 return glyph - s->row->glyphs[s->area];
22873 }
22874
22875 static struct font_metrics *
22876 get_per_char_metric (struct font *font, XChar2b *char2b)
22877 {
22878 static struct font_metrics metrics;
22879 unsigned code;
22880
22881 if (! font)
22882 return NULL;
22883 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22884 if (code == FONT_INVALID_CODE)
22885 return NULL;
22886 font->driver->text_extents (font, &code, 1, &metrics);
22887 return &metrics;
22888 }
22889
22890 /* EXPORT for RIF:
22891 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22892 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22893 assumed to be zero. */
22894
22895 void
22896 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22897 {
22898 *left = *right = 0;
22899
22900 if (glyph->type == CHAR_GLYPH)
22901 {
22902 struct face *face;
22903 XChar2b char2b;
22904 struct font_metrics *pcm;
22905
22906 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22907 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22908 {
22909 if (pcm->rbearing > pcm->width)
22910 *right = pcm->rbearing - pcm->width;
22911 if (pcm->lbearing < 0)
22912 *left = -pcm->lbearing;
22913 }
22914 }
22915 else if (glyph->type == COMPOSITE_GLYPH)
22916 {
22917 if (! glyph->u.cmp.automatic)
22918 {
22919 struct composition *cmp = composition_table[glyph->u.cmp.id];
22920
22921 if (cmp->rbearing > cmp->pixel_width)
22922 *right = cmp->rbearing - cmp->pixel_width;
22923 if (cmp->lbearing < 0)
22924 *left = - cmp->lbearing;
22925 }
22926 else
22927 {
22928 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22929 struct font_metrics metrics;
22930
22931 composition_gstring_width (gstring, glyph->slice.cmp.from,
22932 glyph->slice.cmp.to + 1, &metrics);
22933 if (metrics.rbearing > metrics.width)
22934 *right = metrics.rbearing - metrics.width;
22935 if (metrics.lbearing < 0)
22936 *left = - metrics.lbearing;
22937 }
22938 }
22939 }
22940
22941
22942 /* Return the index of the first glyph preceding glyph string S that
22943 is overwritten by S because of S's left overhang. Value is -1
22944 if no glyphs are overwritten. */
22945
22946 static int
22947 left_overwritten (struct glyph_string *s)
22948 {
22949 int k;
22950
22951 if (s->left_overhang)
22952 {
22953 int x = 0, i;
22954 struct glyph *glyphs = s->row->glyphs[s->area];
22955 int first = s->first_glyph - glyphs;
22956
22957 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22958 x -= glyphs[i].pixel_width;
22959
22960 k = i + 1;
22961 }
22962 else
22963 k = -1;
22964
22965 return k;
22966 }
22967
22968
22969 /* Return the index of the first glyph preceding glyph string S that
22970 is overwriting S because of its right overhang. Value is -1 if no
22971 glyph in front of S overwrites S. */
22972
22973 static int
22974 left_overwriting (struct glyph_string *s)
22975 {
22976 int i, k, x;
22977 struct glyph *glyphs = s->row->glyphs[s->area];
22978 int first = s->first_glyph - glyphs;
22979
22980 k = -1;
22981 x = 0;
22982 for (i = first - 1; i >= 0; --i)
22983 {
22984 int left, right;
22985 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22986 if (x + right > 0)
22987 k = i;
22988 x -= glyphs[i].pixel_width;
22989 }
22990
22991 return k;
22992 }
22993
22994
22995 /* Return the index of the last glyph following glyph string S that is
22996 overwritten by S because of S's right overhang. Value is -1 if
22997 no such glyph is found. */
22998
22999 static int
23000 right_overwritten (struct glyph_string *s)
23001 {
23002 int k = -1;
23003
23004 if (s->right_overhang)
23005 {
23006 int x = 0, i;
23007 struct glyph *glyphs = s->row->glyphs[s->area];
23008 int first = (s->first_glyph - glyphs
23009 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23010 int end = s->row->used[s->area];
23011
23012 for (i = first; i < end && s->right_overhang > x; ++i)
23013 x += glyphs[i].pixel_width;
23014
23015 k = i;
23016 }
23017
23018 return k;
23019 }
23020
23021
23022 /* Return the index of the last glyph following glyph string S that
23023 overwrites S because of its left overhang. Value is negative
23024 if no such glyph is found. */
23025
23026 static int
23027 right_overwriting (struct glyph_string *s)
23028 {
23029 int i, k, x;
23030 int end = s->row->used[s->area];
23031 struct glyph *glyphs = s->row->glyphs[s->area];
23032 int first = (s->first_glyph - glyphs
23033 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23034
23035 k = -1;
23036 x = 0;
23037 for (i = first; i < end; ++i)
23038 {
23039 int left, right;
23040 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23041 if (x - left < 0)
23042 k = i;
23043 x += glyphs[i].pixel_width;
23044 }
23045
23046 return k;
23047 }
23048
23049
23050 /* Set background width of glyph string S. START is the index of the
23051 first glyph following S. LAST_X is the right-most x-position + 1
23052 in the drawing area. */
23053
23054 static void
23055 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23056 {
23057 /* If the face of this glyph string has to be drawn to the end of
23058 the drawing area, set S->extends_to_end_of_line_p. */
23059
23060 if (start == s->row->used[s->area]
23061 && s->area == TEXT_AREA
23062 && ((s->row->fill_line_p
23063 && (s->hl == DRAW_NORMAL_TEXT
23064 || s->hl == DRAW_IMAGE_RAISED
23065 || s->hl == DRAW_IMAGE_SUNKEN))
23066 || s->hl == DRAW_MOUSE_FACE))
23067 s->extends_to_end_of_line_p = 1;
23068
23069 /* If S extends its face to the end of the line, set its
23070 background_width to the distance to the right edge of the drawing
23071 area. */
23072 if (s->extends_to_end_of_line_p)
23073 s->background_width = last_x - s->x + 1;
23074 else
23075 s->background_width = s->width;
23076 }
23077
23078
23079 /* Compute overhangs and x-positions for glyph string S and its
23080 predecessors, or successors. X is the starting x-position for S.
23081 BACKWARD_P non-zero means process predecessors. */
23082
23083 static void
23084 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23085 {
23086 if (backward_p)
23087 {
23088 while (s)
23089 {
23090 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23091 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23092 x -= s->width;
23093 s->x = x;
23094 s = s->prev;
23095 }
23096 }
23097 else
23098 {
23099 while (s)
23100 {
23101 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23102 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23103 s->x = x;
23104 x += s->width;
23105 s = s->next;
23106 }
23107 }
23108 }
23109
23110
23111
23112 /* The following macros are only called from draw_glyphs below.
23113 They reference the following parameters of that function directly:
23114 `w', `row', `area', and `overlap_p'
23115 as well as the following local variables:
23116 `s', `f', and `hdc' (in W32) */
23117
23118 #ifdef HAVE_NTGUI
23119 /* On W32, silently add local `hdc' variable to argument list of
23120 init_glyph_string. */
23121 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23122 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23123 #else
23124 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23125 init_glyph_string (s, char2b, w, row, area, start, hl)
23126 #endif
23127
23128 /* Add a glyph string for a stretch glyph to the list of strings
23129 between HEAD and TAIL. START is the index of the stretch glyph in
23130 row area AREA of glyph row ROW. END is the index of the last glyph
23131 in that glyph row area. X is the current output position assigned
23132 to the new glyph string constructed. HL overrides that face of the
23133 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23134 is the right-most x-position of the drawing area. */
23135
23136 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23137 and below -- keep them on one line. */
23138 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23139 do \
23140 { \
23141 s = alloca (sizeof *s); \
23142 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23143 START = fill_stretch_glyph_string (s, START, END); \
23144 append_glyph_string (&HEAD, &TAIL, s); \
23145 s->x = (X); \
23146 } \
23147 while (0)
23148
23149
23150 /* Add a glyph string for an image glyph to the list of strings
23151 between HEAD and TAIL. START is the index of the image glyph in
23152 row area AREA of glyph row ROW. END is the index of the last glyph
23153 in that glyph row area. X is the current output position assigned
23154 to the new glyph string constructed. HL overrides that face of the
23155 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23156 is the right-most x-position of the drawing area. */
23157
23158 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23159 do \
23160 { \
23161 s = alloca (sizeof *s); \
23162 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23163 fill_image_glyph_string (s); \
23164 append_glyph_string (&HEAD, &TAIL, s); \
23165 ++START; \
23166 s->x = (X); \
23167 } \
23168 while (0)
23169
23170 #ifdef HAVE_XWIDGETS
23171 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23172 do \
23173 { \
23174 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
23175 s = (struct glyph_string *) alloca (sizeof *s); \
23176 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23177 fill_xwidget_glyph_string (s); \
23178 append_glyph_string (&HEAD, &TAIL, s); \
23179 ++START; \
23180 s->x = (X); \
23181 } \
23182 while (0)
23183 #endif
23184
23185
23186 /* Add a glyph string for a sequence of character glyphs to the list
23187 of strings between HEAD and TAIL. START is the index of the first
23188 glyph in row area AREA of glyph row ROW that is part of the new
23189 glyph string. END is the index of the last glyph in that glyph row
23190 area. X is the current output position assigned to the new glyph
23191 string constructed. HL overrides that face of the glyph; e.g. it
23192 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23193 right-most x-position of the drawing area. */
23194
23195 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23196 do \
23197 { \
23198 int face_id; \
23199 XChar2b *char2b; \
23200 \
23201 face_id = (row)->glyphs[area][START].face_id; \
23202 \
23203 s = alloca (sizeof *s); \
23204 char2b = alloca ((END - START) * sizeof *char2b); \
23205 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23206 append_glyph_string (&HEAD, &TAIL, s); \
23207 s->x = (X); \
23208 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23209 } \
23210 while (0)
23211
23212
23213 /* Add a glyph string for a composite sequence to the list of strings
23214 between HEAD and TAIL. START is the index of the first glyph in
23215 row area AREA of glyph row ROW that is part of the new glyph
23216 string. END is the index of the last glyph in that glyph row area.
23217 X is the current output position assigned to the new glyph string
23218 constructed. HL overrides that face of the glyph; e.g. it is
23219 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23220 x-position of the drawing area. */
23221
23222 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23223 do { \
23224 int face_id = (row)->glyphs[area][START].face_id; \
23225 struct face *base_face = FACE_FROM_ID (f, face_id); \
23226 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23227 struct composition *cmp = composition_table[cmp_id]; \
23228 XChar2b *char2b; \
23229 struct glyph_string *first_s = NULL; \
23230 int n; \
23231 \
23232 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23233 \
23234 /* Make glyph_strings for each glyph sequence that is drawable by \
23235 the same face, and append them to HEAD/TAIL. */ \
23236 for (n = 0; n < cmp->glyph_len;) \
23237 { \
23238 s = alloca (sizeof *s); \
23239 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23240 append_glyph_string (&(HEAD), &(TAIL), s); \
23241 s->cmp = cmp; \
23242 s->cmp_from = n; \
23243 s->x = (X); \
23244 if (n == 0) \
23245 first_s = s; \
23246 n = fill_composite_glyph_string (s, base_face, overlaps); \
23247 } \
23248 \
23249 ++START; \
23250 s = first_s; \
23251 } while (0)
23252
23253
23254 /* Add a glyph string for a glyph-string sequence to the list of strings
23255 between HEAD and TAIL. */
23256
23257 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23258 do { \
23259 int face_id; \
23260 XChar2b *char2b; \
23261 Lisp_Object gstring; \
23262 \
23263 face_id = (row)->glyphs[area][START].face_id; \
23264 gstring = (composition_gstring_from_id \
23265 ((row)->glyphs[area][START].u.cmp.id)); \
23266 s = alloca (sizeof *s); \
23267 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23268 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23269 append_glyph_string (&(HEAD), &(TAIL), s); \
23270 s->x = (X); \
23271 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23272 } while (0)
23273
23274
23275 /* Add a glyph string for a sequence of glyphless character's glyphs
23276 to the list of strings between HEAD and TAIL. The meanings of
23277 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23278
23279 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23280 do \
23281 { \
23282 int face_id; \
23283 \
23284 face_id = (row)->glyphs[area][START].face_id; \
23285 \
23286 s = alloca (sizeof *s); \
23287 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23288 append_glyph_string (&HEAD, &TAIL, s); \
23289 s->x = (X); \
23290 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23291 overlaps); \
23292 } \
23293 while (0)
23294
23295
23296 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23297 of AREA of glyph row ROW on window W between indices START and END.
23298 HL overrides the face for drawing glyph strings, e.g. it is
23299 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23300 x-positions of the drawing area.
23301
23302 This is an ugly monster macro construct because we must use alloca
23303 to allocate glyph strings (because draw_glyphs can be called
23304 asynchronously). */
23305
23306 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23307 do \
23308 { \
23309 HEAD = TAIL = NULL; \
23310 while (START < END) \
23311 { \
23312 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23313 switch (first_glyph->type) \
23314 { \
23315 case CHAR_GLYPH: \
23316 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23317 HL, X, LAST_X); \
23318 break; \
23319 \
23320 case COMPOSITE_GLYPH: \
23321 if (first_glyph->u.cmp.automatic) \
23322 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23323 HL, X, LAST_X); \
23324 else \
23325 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23326 HL, X, LAST_X); \
23327 break; \
23328 \
23329 case STRETCH_GLYPH: \
23330 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23331 HL, X, LAST_X); \
23332 break; \
23333 \
23334 case IMAGE_GLYPH: \
23335 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23336 HL, X, LAST_X); \
23337 break;
23338
23339 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23340 case XWIDGET_GLYPH: \
23341 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
23342 HL, X, LAST_X); \
23343 break;
23344
23345 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
23346 case GLYPHLESS_GLYPH: \
23347 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23348 HL, X, LAST_X); \
23349 break; \
23350 \
23351 default: \
23352 emacs_abort (); \
23353 } \
23354 \
23355 if (s) \
23356 { \
23357 set_glyph_string_background_width (s, START, LAST_X); \
23358 (X) += s->width; \
23359 } \
23360 } \
23361 } while (0)
23362
23363
23364 #ifdef HAVE_XWIDGETS
23365 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23366 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23367 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23368 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23369 #else
23370 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23371 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23372 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23373 #endif
23374
23375
23376 /* Draw glyphs between START and END in AREA of ROW on window W,
23377 starting at x-position X. X is relative to AREA in W. HL is a
23378 face-override with the following meaning:
23379
23380 DRAW_NORMAL_TEXT draw normally
23381 DRAW_CURSOR draw in cursor face
23382 DRAW_MOUSE_FACE draw in mouse face.
23383 DRAW_INVERSE_VIDEO draw in mode line face
23384 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23385 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23386
23387 If OVERLAPS is non-zero, draw only the foreground of characters and
23388 clip to the physical height of ROW. Non-zero value also defines
23389 the overlapping part to be drawn:
23390
23391 OVERLAPS_PRED overlap with preceding rows
23392 OVERLAPS_SUCC overlap with succeeding rows
23393 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23394 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23395
23396 Value is the x-position reached, relative to AREA of W. */
23397
23398 static int
23399 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23400 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23401 enum draw_glyphs_face hl, int overlaps)
23402 {
23403 struct glyph_string *head, *tail;
23404 struct glyph_string *s;
23405 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23406 int i, j, x_reached, last_x, area_left = 0;
23407 struct frame *f = XFRAME (WINDOW_FRAME (w));
23408 DECLARE_HDC (hdc);
23409
23410 ALLOCATE_HDC (hdc, f);
23411
23412 /* Let's rather be paranoid than getting a SEGV. */
23413 end = min (end, row->used[area]);
23414 start = clip_to_bounds (0, start, end);
23415
23416 /* Translate X to frame coordinates. Set last_x to the right
23417 end of the drawing area. */
23418 if (row->full_width_p)
23419 {
23420 /* X is relative to the left edge of W, without scroll bars
23421 or fringes. */
23422 area_left = WINDOW_LEFT_EDGE_X (w);
23423 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23424 }
23425 else
23426 {
23427 area_left = window_box_left (w, area);
23428 last_x = area_left + window_box_width (w, area);
23429 }
23430 x += area_left;
23431
23432 /* Build a doubly-linked list of glyph_string structures between
23433 head and tail from what we have to draw. Note that the macro
23434 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23435 the reason we use a separate variable `i'. */
23436 i = start;
23437 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23438 if (tail)
23439 x_reached = tail->x + tail->background_width;
23440 else
23441 x_reached = x;
23442
23443 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23444 the row, redraw some glyphs in front or following the glyph
23445 strings built above. */
23446 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23447 {
23448 struct glyph_string *h, *t;
23449 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23450 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23451 int check_mouse_face = 0;
23452 int dummy_x = 0;
23453
23454 /* If mouse highlighting is on, we may need to draw adjacent
23455 glyphs using mouse-face highlighting. */
23456 if (area == TEXT_AREA && row->mouse_face_p
23457 && hlinfo->mouse_face_beg_row >= 0
23458 && hlinfo->mouse_face_end_row >= 0)
23459 {
23460 struct glyph_row *mouse_beg_row, *mouse_end_row;
23461
23462 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23463 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23464
23465 if (row >= mouse_beg_row && row <= mouse_end_row)
23466 {
23467 check_mouse_face = 1;
23468 mouse_beg_col = (row == mouse_beg_row)
23469 ? hlinfo->mouse_face_beg_col : 0;
23470 mouse_end_col = (row == mouse_end_row)
23471 ? hlinfo->mouse_face_end_col
23472 : row->used[TEXT_AREA];
23473 }
23474 }
23475
23476 /* Compute overhangs for all glyph strings. */
23477 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23478 for (s = head; s; s = s->next)
23479 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23480
23481 /* Prepend glyph strings for glyphs in front of the first glyph
23482 string that are overwritten because of the first glyph
23483 string's left overhang. The background of all strings
23484 prepended must be drawn because the first glyph string
23485 draws over it. */
23486 i = left_overwritten (head);
23487 if (i >= 0)
23488 {
23489 enum draw_glyphs_face overlap_hl;
23490
23491 /* If this row contains mouse highlighting, attempt to draw
23492 the overlapped glyphs with the correct highlight. This
23493 code fails if the overlap encompasses more than one glyph
23494 and mouse-highlight spans only some of these glyphs.
23495 However, making it work perfectly involves a lot more
23496 code, and I don't know if the pathological case occurs in
23497 practice, so we'll stick to this for now. --- cyd */
23498 if (check_mouse_face
23499 && mouse_beg_col < start && mouse_end_col > i)
23500 overlap_hl = DRAW_MOUSE_FACE;
23501 else
23502 overlap_hl = DRAW_NORMAL_TEXT;
23503
23504 j = i;
23505 BUILD_GLYPH_STRINGS (j, start, h, t,
23506 overlap_hl, dummy_x, last_x);
23507 start = i;
23508 compute_overhangs_and_x (t, head->x, 1);
23509 prepend_glyph_string_lists (&head, &tail, h, t);
23510 clip_head = head;
23511 }
23512
23513 /* Prepend glyph strings for glyphs in front of the first glyph
23514 string that overwrite that glyph string because of their
23515 right overhang. For these strings, only the foreground must
23516 be drawn, because it draws over the glyph string at `head'.
23517 The background must not be drawn because this would overwrite
23518 right overhangs of preceding glyphs for which no glyph
23519 strings exist. */
23520 i = left_overwriting (head);
23521 if (i >= 0)
23522 {
23523 enum draw_glyphs_face overlap_hl;
23524
23525 if (check_mouse_face
23526 && mouse_beg_col < start && mouse_end_col > i)
23527 overlap_hl = DRAW_MOUSE_FACE;
23528 else
23529 overlap_hl = DRAW_NORMAL_TEXT;
23530
23531 clip_head = head;
23532 BUILD_GLYPH_STRINGS (i, start, h, t,
23533 overlap_hl, dummy_x, last_x);
23534 for (s = h; s; s = s->next)
23535 s->background_filled_p = 1;
23536 compute_overhangs_and_x (t, head->x, 1);
23537 prepend_glyph_string_lists (&head, &tail, h, t);
23538 }
23539
23540 /* Append glyphs strings for glyphs following the last glyph
23541 string tail that are overwritten by tail. The background of
23542 these strings has to be drawn because tail's foreground draws
23543 over it. */
23544 i = right_overwritten (tail);
23545 if (i >= 0)
23546 {
23547 enum draw_glyphs_face overlap_hl;
23548
23549 if (check_mouse_face
23550 && mouse_beg_col < i && mouse_end_col > end)
23551 overlap_hl = DRAW_MOUSE_FACE;
23552 else
23553 overlap_hl = DRAW_NORMAL_TEXT;
23554
23555 BUILD_GLYPH_STRINGS (end, i, h, t,
23556 overlap_hl, x, last_x);
23557 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23558 we don't have `end = i;' here. */
23559 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23560 append_glyph_string_lists (&head, &tail, h, t);
23561 clip_tail = tail;
23562 }
23563
23564 /* Append glyph strings for glyphs following the last glyph
23565 string tail that overwrite tail. The foreground of such
23566 glyphs has to be drawn because it writes into the background
23567 of tail. The background must not be drawn because it could
23568 paint over the foreground of following glyphs. */
23569 i = right_overwriting (tail);
23570 if (i >= 0)
23571 {
23572 enum draw_glyphs_face overlap_hl;
23573 if (check_mouse_face
23574 && mouse_beg_col < i && mouse_end_col > end)
23575 overlap_hl = DRAW_MOUSE_FACE;
23576 else
23577 overlap_hl = DRAW_NORMAL_TEXT;
23578
23579 clip_tail = tail;
23580 i++; /* We must include the Ith glyph. */
23581 BUILD_GLYPH_STRINGS (end, i, h, t,
23582 overlap_hl, x, last_x);
23583 for (s = h; s; s = s->next)
23584 s->background_filled_p = 1;
23585 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23586 append_glyph_string_lists (&head, &tail, h, t);
23587 }
23588 if (clip_head || clip_tail)
23589 for (s = head; s; s = s->next)
23590 {
23591 s->clip_head = clip_head;
23592 s->clip_tail = clip_tail;
23593 }
23594 }
23595
23596 /* Draw all strings. */
23597 for (s = head; s; s = s->next)
23598 FRAME_RIF (f)->draw_glyph_string (s);
23599
23600 #ifndef HAVE_NS
23601 /* When focus a sole frame and move horizontally, this sets on_p to 0
23602 causing a failure to erase prev cursor position. */
23603 if (area == TEXT_AREA
23604 && !row->full_width_p
23605 /* When drawing overlapping rows, only the glyph strings'
23606 foreground is drawn, which doesn't erase a cursor
23607 completely. */
23608 && !overlaps)
23609 {
23610 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23611 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23612 : (tail ? tail->x + tail->background_width : x));
23613 x0 -= area_left;
23614 x1 -= area_left;
23615
23616 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23617 row->y, MATRIX_ROW_BOTTOM_Y (row));
23618 }
23619 #endif
23620
23621 /* Value is the x-position up to which drawn, relative to AREA of W.
23622 This doesn't include parts drawn because of overhangs. */
23623 if (row->full_width_p)
23624 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23625 else
23626 x_reached -= area_left;
23627
23628 RELEASE_HDC (hdc, f);
23629
23630 return x_reached;
23631 }
23632
23633 /* Expand row matrix if too narrow. Don't expand if area
23634 is not present. */
23635
23636 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23637 { \
23638 if (!fonts_changed_p \
23639 && (it->glyph_row->glyphs[area] \
23640 < it->glyph_row->glyphs[area + 1])) \
23641 { \
23642 it->w->ncols_scale_factor++; \
23643 fonts_changed_p = 1; \
23644 } \
23645 }
23646
23647 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23648 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23649
23650 static void
23651 append_glyph (struct it *it)
23652 {
23653 struct glyph *glyph;
23654 enum glyph_row_area area = it->area;
23655
23656 eassert (it->glyph_row);
23657 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23658
23659 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23660 if (glyph < it->glyph_row->glyphs[area + 1])
23661 {
23662 /* If the glyph row is reversed, we need to prepend the glyph
23663 rather than append it. */
23664 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23665 {
23666 struct glyph *g;
23667
23668 /* Make room for the additional glyph. */
23669 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23670 g[1] = *g;
23671 glyph = it->glyph_row->glyphs[area];
23672 }
23673 glyph->charpos = CHARPOS (it->position);
23674 glyph->object = it->object;
23675 if (it->pixel_width > 0)
23676 {
23677 glyph->pixel_width = it->pixel_width;
23678 glyph->padding_p = 0;
23679 }
23680 else
23681 {
23682 /* Assure at least 1-pixel width. Otherwise, cursor can't
23683 be displayed correctly. */
23684 glyph->pixel_width = 1;
23685 glyph->padding_p = 1;
23686 }
23687 glyph->ascent = it->ascent;
23688 glyph->descent = it->descent;
23689 glyph->voffset = it->voffset;
23690 glyph->type = CHAR_GLYPH;
23691 glyph->avoid_cursor_p = it->avoid_cursor_p;
23692 glyph->multibyte_p = it->multibyte_p;
23693 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23694 {
23695 /* In R2L rows, the left and the right box edges need to be
23696 drawn in reverse direction. */
23697 glyph->right_box_line_p = it->start_of_box_run_p;
23698 glyph->left_box_line_p = it->end_of_box_run_p;
23699 }
23700 else
23701 {
23702 glyph->left_box_line_p = it->start_of_box_run_p;
23703 glyph->right_box_line_p = it->end_of_box_run_p;
23704 }
23705 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23706 || it->phys_descent > it->descent);
23707 glyph->glyph_not_available_p = it->glyph_not_available_p;
23708 glyph->face_id = it->face_id;
23709 glyph->u.ch = it->char_to_display;
23710 glyph->slice.img = null_glyph_slice;
23711 glyph->font_type = FONT_TYPE_UNKNOWN;
23712 if (it->bidi_p)
23713 {
23714 glyph->resolved_level = it->bidi_it.resolved_level;
23715 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23716 emacs_abort ();
23717 glyph->bidi_type = it->bidi_it.type;
23718 }
23719 else
23720 {
23721 glyph->resolved_level = 0;
23722 glyph->bidi_type = UNKNOWN_BT;
23723 }
23724 ++it->glyph_row->used[area];
23725 }
23726 else
23727 IT_EXPAND_MATRIX_WIDTH (it, area);
23728 }
23729
23730 /* Store one glyph for the composition IT->cmp_it.id in
23731 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23732 non-null. */
23733
23734 static void
23735 append_composite_glyph (struct it *it)
23736 {
23737 struct glyph *glyph;
23738 enum glyph_row_area area = it->area;
23739
23740 eassert (it->glyph_row);
23741
23742 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23743 if (glyph < it->glyph_row->glyphs[area + 1])
23744 {
23745 /* If the glyph row is reversed, we need to prepend the glyph
23746 rather than append it. */
23747 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23748 {
23749 struct glyph *g;
23750
23751 /* Make room for the new glyph. */
23752 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23753 g[1] = *g;
23754 glyph = it->glyph_row->glyphs[it->area];
23755 }
23756 glyph->charpos = it->cmp_it.charpos;
23757 glyph->object = it->object;
23758 glyph->pixel_width = it->pixel_width;
23759 glyph->ascent = it->ascent;
23760 glyph->descent = it->descent;
23761 glyph->voffset = it->voffset;
23762 glyph->type = COMPOSITE_GLYPH;
23763 if (it->cmp_it.ch < 0)
23764 {
23765 glyph->u.cmp.automatic = 0;
23766 glyph->u.cmp.id = it->cmp_it.id;
23767 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23768 }
23769 else
23770 {
23771 glyph->u.cmp.automatic = 1;
23772 glyph->u.cmp.id = it->cmp_it.id;
23773 glyph->slice.cmp.from = it->cmp_it.from;
23774 glyph->slice.cmp.to = it->cmp_it.to - 1;
23775 }
23776 glyph->avoid_cursor_p = it->avoid_cursor_p;
23777 glyph->multibyte_p = it->multibyte_p;
23778 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23779 {
23780 /* In R2L rows, the left and the right box edges need to be
23781 drawn in reverse direction. */
23782 glyph->right_box_line_p = it->start_of_box_run_p;
23783 glyph->left_box_line_p = it->end_of_box_run_p;
23784 }
23785 else
23786 {
23787 glyph->left_box_line_p = it->start_of_box_run_p;
23788 glyph->right_box_line_p = it->end_of_box_run_p;
23789 }
23790 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23791 || it->phys_descent > it->descent);
23792 glyph->padding_p = 0;
23793 glyph->glyph_not_available_p = 0;
23794 glyph->face_id = it->face_id;
23795 glyph->font_type = FONT_TYPE_UNKNOWN;
23796 if (it->bidi_p)
23797 {
23798 glyph->resolved_level = it->bidi_it.resolved_level;
23799 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23800 emacs_abort ();
23801 glyph->bidi_type = it->bidi_it.type;
23802 }
23803 ++it->glyph_row->used[area];
23804 }
23805 else
23806 IT_EXPAND_MATRIX_WIDTH (it, area);
23807 }
23808
23809
23810 /* Change IT->ascent and IT->height according to the setting of
23811 IT->voffset. */
23812
23813 static void
23814 take_vertical_position_into_account (struct it *it)
23815 {
23816 if (it->voffset)
23817 {
23818 if (it->voffset < 0)
23819 /* Increase the ascent so that we can display the text higher
23820 in the line. */
23821 it->ascent -= it->voffset;
23822 else
23823 /* Increase the descent so that we can display the text lower
23824 in the line. */
23825 it->descent += it->voffset;
23826 }
23827 }
23828
23829
23830 /* Produce glyphs/get display metrics for the image IT is loaded with.
23831 See the description of struct display_iterator in dispextern.h for
23832 an overview of struct display_iterator. */
23833
23834 static void
23835 produce_image_glyph (struct it *it)
23836 {
23837 struct image *img;
23838 struct face *face;
23839 int glyph_ascent, crop;
23840 struct glyph_slice slice;
23841
23842 eassert (it->what == IT_IMAGE);
23843
23844 face = FACE_FROM_ID (it->f, it->face_id);
23845 eassert (face);
23846 /* Make sure X resources of the face is loaded. */
23847 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23848
23849 if (it->image_id < 0)
23850 {
23851 /* Fringe bitmap. */
23852 it->ascent = it->phys_ascent = 0;
23853 it->descent = it->phys_descent = 0;
23854 it->pixel_width = 0;
23855 it->nglyphs = 0;
23856 return;
23857 }
23858
23859 img = IMAGE_FROM_ID (it->f, it->image_id);
23860 eassert (img);
23861 /* Make sure X resources of the image is loaded. */
23862 prepare_image_for_display (it->f, img);
23863
23864 slice.x = slice.y = 0;
23865 slice.width = img->width;
23866 slice.height = img->height;
23867
23868 if (INTEGERP (it->slice.x))
23869 slice.x = XINT (it->slice.x);
23870 else if (FLOATP (it->slice.x))
23871 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23872
23873 if (INTEGERP (it->slice.y))
23874 slice.y = XINT (it->slice.y);
23875 else if (FLOATP (it->slice.y))
23876 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23877
23878 if (INTEGERP (it->slice.width))
23879 slice.width = XINT (it->slice.width);
23880 else if (FLOATP (it->slice.width))
23881 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23882
23883 if (INTEGERP (it->slice.height))
23884 slice.height = XINT (it->slice.height);
23885 else if (FLOATP (it->slice.height))
23886 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23887
23888 if (slice.x >= img->width)
23889 slice.x = img->width;
23890 if (slice.y >= img->height)
23891 slice.y = img->height;
23892 if (slice.x + slice.width >= img->width)
23893 slice.width = img->width - slice.x;
23894 if (slice.y + slice.height > img->height)
23895 slice.height = img->height - slice.y;
23896
23897 if (slice.width == 0 || slice.height == 0)
23898 return;
23899
23900 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23901
23902 it->descent = slice.height - glyph_ascent;
23903 if (slice.y == 0)
23904 it->descent += img->vmargin;
23905 if (slice.y + slice.height == img->height)
23906 it->descent += img->vmargin;
23907 it->phys_descent = it->descent;
23908
23909 it->pixel_width = slice.width;
23910 if (slice.x == 0)
23911 it->pixel_width += img->hmargin;
23912 if (slice.x + slice.width == img->width)
23913 it->pixel_width += img->hmargin;
23914
23915 /* It's quite possible for images to have an ascent greater than
23916 their height, so don't get confused in that case. */
23917 if (it->descent < 0)
23918 it->descent = 0;
23919
23920 it->nglyphs = 1;
23921
23922 if (face->box != FACE_NO_BOX)
23923 {
23924 if (face->box_line_width > 0)
23925 {
23926 if (slice.y == 0)
23927 it->ascent += face->box_line_width;
23928 if (slice.y + slice.height == img->height)
23929 it->descent += face->box_line_width;
23930 }
23931
23932 if (it->start_of_box_run_p && slice.x == 0)
23933 it->pixel_width += eabs (face->box_line_width);
23934 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23935 it->pixel_width += eabs (face->box_line_width);
23936 }
23937
23938 take_vertical_position_into_account (it);
23939
23940 /* Automatically crop wide image glyphs at right edge so we can
23941 draw the cursor on same display row. */
23942 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23943 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23944 {
23945 it->pixel_width -= crop;
23946 slice.width -= crop;
23947 }
23948
23949 if (it->glyph_row)
23950 {
23951 struct glyph *glyph;
23952 enum glyph_row_area area = it->area;
23953
23954 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23955 if (glyph < it->glyph_row->glyphs[area + 1])
23956 {
23957 glyph->charpos = CHARPOS (it->position);
23958 glyph->object = it->object;
23959 glyph->pixel_width = it->pixel_width;
23960 glyph->ascent = glyph_ascent;
23961 glyph->descent = it->descent;
23962 glyph->voffset = it->voffset;
23963 glyph->type = IMAGE_GLYPH;
23964 glyph->avoid_cursor_p = it->avoid_cursor_p;
23965 glyph->multibyte_p = it->multibyte_p;
23966 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23967 {
23968 /* In R2L rows, the left and the right box edges need to be
23969 drawn in reverse direction. */
23970 glyph->right_box_line_p = it->start_of_box_run_p;
23971 glyph->left_box_line_p = it->end_of_box_run_p;
23972 }
23973 else
23974 {
23975 glyph->left_box_line_p = it->start_of_box_run_p;
23976 glyph->right_box_line_p = it->end_of_box_run_p;
23977 }
23978 glyph->overlaps_vertically_p = 0;
23979 glyph->padding_p = 0;
23980 glyph->glyph_not_available_p = 0;
23981 glyph->face_id = it->face_id;
23982 glyph->u.img_id = img->id;
23983 glyph->slice.img = slice;
23984 glyph->font_type = FONT_TYPE_UNKNOWN;
23985 if (it->bidi_p)
23986 {
23987 glyph->resolved_level = it->bidi_it.resolved_level;
23988 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23989 emacs_abort ();
23990 glyph->bidi_type = it->bidi_it.type;
23991 }
23992 ++it->glyph_row->used[area];
23993 }
23994 else
23995 IT_EXPAND_MATRIX_WIDTH (it, area);
23996 }
23997 }
23998
23999 #ifdef HAVE_XWIDGETS
24000 static void
24001 produce_xwidget_glyph (struct it *it)
24002 {
24003 struct xwidget* xw;
24004 struct face *face;
24005 int glyph_ascent, crop;
24006 printf("produce_xwidget_glyph:\n");
24007 eassert (it->what == IT_XWIDGET);
24008
24009 face = FACE_FROM_ID (it->f, it->face_id);
24010 eassert (face);
24011 /* Make sure X resources of the face is loaded. */
24012 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24013
24014 xw = it->xwidget;
24015 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
24016 it->descent = xw->height/2;
24017 it->phys_descent = it->descent;
24018 it->pixel_width = xw->width;
24019 /* It's quite possible for images to have an ascent greater than
24020 their height, so don't get confused in that case. */
24021 if (it->descent < 0)
24022 it->descent = 0;
24023
24024 it->nglyphs = 1;
24025
24026 if (face->box != FACE_NO_BOX)
24027 {
24028 if (face->box_line_width > 0)
24029 {
24030 it->ascent += face->box_line_width;
24031 it->descent += face->box_line_width;
24032 }
24033
24034 if (it->start_of_box_run_p)
24035 it->pixel_width += eabs (face->box_line_width);
24036 it->pixel_width += eabs (face->box_line_width);
24037 }
24038
24039 take_vertical_position_into_account (it);
24040
24041 /* Automatically crop wide image glyphs at right edge so we can
24042 draw the cursor on same display row. */
24043 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24044 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24045 {
24046 it->pixel_width -= crop;
24047 }
24048
24049 if (it->glyph_row)
24050 {
24051 struct glyph *glyph;
24052 enum glyph_row_area area = it->area;
24053
24054 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24055 if (glyph < it->glyph_row->glyphs[area + 1])
24056 {
24057 glyph->charpos = CHARPOS (it->position);
24058 glyph->object = it->object;
24059 glyph->pixel_width = it->pixel_width;
24060 glyph->ascent = glyph_ascent;
24061 glyph->descent = it->descent;
24062 glyph->voffset = it->voffset;
24063 glyph->type = XWIDGET_GLYPH;
24064
24065 glyph->multibyte_p = it->multibyte_p;
24066 glyph->left_box_line_p = it->start_of_box_run_p;
24067 glyph->right_box_line_p = it->end_of_box_run_p;
24068 glyph->overlaps_vertically_p = 0;
24069 glyph->padding_p = 0;
24070 glyph->glyph_not_available_p = 0;
24071 glyph->face_id = it->face_id;
24072 glyph->u.xwidget = it->xwidget;
24073 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
24074 glyph->font_type = FONT_TYPE_UNKNOWN;
24075 ++it->glyph_row->used[area];
24076 }
24077 else
24078 IT_EXPAND_MATRIX_WIDTH (it, area);
24079 }
24080 }
24081 #endif
24082
24083 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24084 of the glyph, WIDTH and HEIGHT are the width and height of the
24085 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24086
24087 static void
24088 append_stretch_glyph (struct it *it, Lisp_Object object,
24089 int width, int height, int ascent)
24090 {
24091 struct glyph *glyph;
24092 enum glyph_row_area area = it->area;
24093
24094 eassert (ascent >= 0 && ascent <= height);
24095
24096 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24097 if (glyph < it->glyph_row->glyphs[area + 1])
24098 {
24099 /* If the glyph row is reversed, we need to prepend the glyph
24100 rather than append it. */
24101 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24102 {
24103 struct glyph *g;
24104
24105 /* Make room for the additional glyph. */
24106 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24107 g[1] = *g;
24108 glyph = it->glyph_row->glyphs[area];
24109 }
24110 glyph->charpos = CHARPOS (it->position);
24111 glyph->object = object;
24112 glyph->pixel_width = width;
24113 glyph->ascent = ascent;
24114 glyph->descent = height - ascent;
24115 glyph->voffset = it->voffset;
24116 glyph->type = STRETCH_GLYPH;
24117 glyph->avoid_cursor_p = it->avoid_cursor_p;
24118 glyph->multibyte_p = it->multibyte_p;
24119 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24120 {
24121 /* In R2L rows, the left and the right box edges need to be
24122 drawn in reverse direction. */
24123 glyph->right_box_line_p = it->start_of_box_run_p;
24124 glyph->left_box_line_p = it->end_of_box_run_p;
24125 }
24126 else
24127 {
24128 glyph->left_box_line_p = it->start_of_box_run_p;
24129 glyph->right_box_line_p = it->end_of_box_run_p;
24130 }
24131 glyph->overlaps_vertically_p = 0;
24132 glyph->padding_p = 0;
24133 glyph->glyph_not_available_p = 0;
24134 glyph->face_id = it->face_id;
24135 glyph->u.stretch.ascent = ascent;
24136 glyph->u.stretch.height = height;
24137 glyph->slice.img = null_glyph_slice;
24138 glyph->font_type = FONT_TYPE_UNKNOWN;
24139 if (it->bidi_p)
24140 {
24141 glyph->resolved_level = it->bidi_it.resolved_level;
24142 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24143 emacs_abort ();
24144 glyph->bidi_type = it->bidi_it.type;
24145 }
24146 else
24147 {
24148 glyph->resolved_level = 0;
24149 glyph->bidi_type = UNKNOWN_BT;
24150 }
24151 ++it->glyph_row->used[area];
24152 }
24153 else
24154 IT_EXPAND_MATRIX_WIDTH (it, area);
24155 }
24156
24157 #endif /* HAVE_WINDOW_SYSTEM */
24158
24159 /* Produce a stretch glyph for iterator IT. IT->object is the value
24160 of the glyph property displayed. The value must be a list
24161 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24162 being recognized:
24163
24164 1. `:width WIDTH' specifies that the space should be WIDTH *
24165 canonical char width wide. WIDTH may be an integer or floating
24166 point number.
24167
24168 2. `:relative-width FACTOR' specifies that the width of the stretch
24169 should be computed from the width of the first character having the
24170 `glyph' property, and should be FACTOR times that width.
24171
24172 3. `:align-to HPOS' specifies that the space should be wide enough
24173 to reach HPOS, a value in canonical character units.
24174
24175 Exactly one of the above pairs must be present.
24176
24177 4. `:height HEIGHT' specifies that the height of the stretch produced
24178 should be HEIGHT, measured in canonical character units.
24179
24180 5. `:relative-height FACTOR' specifies that the height of the
24181 stretch should be FACTOR times the height of the characters having
24182 the glyph property.
24183
24184 Either none or exactly one of 4 or 5 must be present.
24185
24186 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24187 of the stretch should be used for the ascent of the stretch.
24188 ASCENT must be in the range 0 <= ASCENT <= 100. */
24189
24190 void
24191 produce_stretch_glyph (struct it *it)
24192 {
24193 /* (space :width WIDTH :height HEIGHT ...) */
24194 Lisp_Object prop, plist;
24195 int width = 0, height = 0, align_to = -1;
24196 int zero_width_ok_p = 0;
24197 double tem;
24198 struct font *font = NULL;
24199
24200 #ifdef HAVE_WINDOW_SYSTEM
24201 int ascent = 0;
24202 int zero_height_ok_p = 0;
24203
24204 if (FRAME_WINDOW_P (it->f))
24205 {
24206 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24207 font = face->font ? face->font : FRAME_FONT (it->f);
24208 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24209 }
24210 #endif
24211
24212 /* List should start with `space'. */
24213 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24214 plist = XCDR (it->object);
24215
24216 /* Compute the width of the stretch. */
24217 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24218 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24219 {
24220 /* Absolute width `:width WIDTH' specified and valid. */
24221 zero_width_ok_p = 1;
24222 width = (int)tem;
24223 }
24224 #ifdef HAVE_WINDOW_SYSTEM
24225 else if (FRAME_WINDOW_P (it->f)
24226 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24227 {
24228 /* Relative width `:relative-width FACTOR' specified and valid.
24229 Compute the width of the characters having the `glyph'
24230 property. */
24231 struct it it2;
24232 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24233
24234 it2 = *it;
24235 if (it->multibyte_p)
24236 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24237 else
24238 {
24239 it2.c = it2.char_to_display = *p, it2.len = 1;
24240 if (! ASCII_CHAR_P (it2.c))
24241 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24242 }
24243
24244 it2.glyph_row = NULL;
24245 it2.what = IT_CHARACTER;
24246 x_produce_glyphs (&it2);
24247 width = NUMVAL (prop) * it2.pixel_width;
24248 }
24249 #endif /* HAVE_WINDOW_SYSTEM */
24250 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24251 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24252 {
24253 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24254 align_to = (align_to < 0
24255 ? 0
24256 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24257 else if (align_to < 0)
24258 align_to = window_box_left_offset (it->w, TEXT_AREA);
24259 width = max (0, (int)tem + align_to - it->current_x);
24260 zero_width_ok_p = 1;
24261 }
24262 else
24263 /* Nothing specified -> width defaults to canonical char width. */
24264 width = FRAME_COLUMN_WIDTH (it->f);
24265
24266 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24267 width = 1;
24268
24269 #ifdef HAVE_WINDOW_SYSTEM
24270 /* Compute height. */
24271 if (FRAME_WINDOW_P (it->f))
24272 {
24273 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24274 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24275 {
24276 height = (int)tem;
24277 zero_height_ok_p = 1;
24278 }
24279 else if (prop = Fplist_get (plist, QCrelative_height),
24280 NUMVAL (prop) > 0)
24281 height = FONT_HEIGHT (font) * NUMVAL (prop);
24282 else
24283 height = FONT_HEIGHT (font);
24284
24285 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24286 height = 1;
24287
24288 /* Compute percentage of height used for ascent. If
24289 `:ascent ASCENT' is present and valid, use that. Otherwise,
24290 derive the ascent from the font in use. */
24291 if (prop = Fplist_get (plist, QCascent),
24292 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24293 ascent = height * NUMVAL (prop) / 100.0;
24294 else if (!NILP (prop)
24295 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24296 ascent = min (max (0, (int)tem), height);
24297 else
24298 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24299 }
24300 else
24301 #endif /* HAVE_WINDOW_SYSTEM */
24302 height = 1;
24303
24304 if (width > 0 && it->line_wrap != TRUNCATE
24305 && it->current_x + width > it->last_visible_x)
24306 {
24307 width = it->last_visible_x - it->current_x;
24308 #ifdef HAVE_WINDOW_SYSTEM
24309 /* Subtract one more pixel from the stretch width, but only on
24310 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24311 width -= FRAME_WINDOW_P (it->f);
24312 #endif
24313 }
24314
24315 if (width > 0 && height > 0 && it->glyph_row)
24316 {
24317 Lisp_Object o_object = it->object;
24318 Lisp_Object object = it->stack[it->sp - 1].string;
24319 int n = width;
24320
24321 if (!STRINGP (object))
24322 object = it->w->contents;
24323 #ifdef HAVE_WINDOW_SYSTEM
24324 if (FRAME_WINDOW_P (it->f))
24325 append_stretch_glyph (it, object, width, height, ascent);
24326 else
24327 #endif
24328 {
24329 it->object = object;
24330 it->char_to_display = ' ';
24331 it->pixel_width = it->len = 1;
24332 while (n--)
24333 tty_append_glyph (it);
24334 it->object = o_object;
24335 }
24336 }
24337
24338 it->pixel_width = width;
24339 #ifdef HAVE_WINDOW_SYSTEM
24340 if (FRAME_WINDOW_P (it->f))
24341 {
24342 it->ascent = it->phys_ascent = ascent;
24343 it->descent = it->phys_descent = height - it->ascent;
24344 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24345 take_vertical_position_into_account (it);
24346 }
24347 else
24348 #endif
24349 it->nglyphs = width;
24350 }
24351
24352 /* Get information about special display element WHAT in an
24353 environment described by IT. WHAT is one of IT_TRUNCATION or
24354 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24355 non-null glyph_row member. This function ensures that fields like
24356 face_id, c, len of IT are left untouched. */
24357
24358 static void
24359 produce_special_glyphs (struct it *it, enum display_element_type what)
24360 {
24361 struct it temp_it;
24362 Lisp_Object gc;
24363 GLYPH glyph;
24364
24365 temp_it = *it;
24366 temp_it.object = make_number (0);
24367 memset (&temp_it.current, 0, sizeof temp_it.current);
24368
24369 if (what == IT_CONTINUATION)
24370 {
24371 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24372 if (it->bidi_it.paragraph_dir == R2L)
24373 SET_GLYPH_FROM_CHAR (glyph, '/');
24374 else
24375 SET_GLYPH_FROM_CHAR (glyph, '\\');
24376 if (it->dp
24377 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24378 {
24379 /* FIXME: Should we mirror GC for R2L lines? */
24380 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24381 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24382 }
24383 }
24384 else if (what == IT_TRUNCATION)
24385 {
24386 /* Truncation glyph. */
24387 SET_GLYPH_FROM_CHAR (glyph, '$');
24388 if (it->dp
24389 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24390 {
24391 /* FIXME: Should we mirror GC for R2L lines? */
24392 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24393 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24394 }
24395 }
24396 else
24397 emacs_abort ();
24398
24399 #ifdef HAVE_WINDOW_SYSTEM
24400 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24401 is turned off, we precede the truncation/continuation glyphs by a
24402 stretch glyph whose width is computed such that these special
24403 glyphs are aligned at the window margin, even when very different
24404 fonts are used in different glyph rows. */
24405 if (FRAME_WINDOW_P (temp_it.f)
24406 /* init_iterator calls this with it->glyph_row == NULL, and it
24407 wants only the pixel width of the truncation/continuation
24408 glyphs. */
24409 && temp_it.glyph_row
24410 /* insert_left_trunc_glyphs calls us at the beginning of the
24411 row, and it has its own calculation of the stretch glyph
24412 width. */
24413 && temp_it.glyph_row->used[TEXT_AREA] > 0
24414 && (temp_it.glyph_row->reversed_p
24415 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24416 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24417 {
24418 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24419
24420 if (stretch_width > 0)
24421 {
24422 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24423 struct font *font =
24424 face->font ? face->font : FRAME_FONT (temp_it.f);
24425 int stretch_ascent =
24426 (((temp_it.ascent + temp_it.descent)
24427 * FONT_BASE (font)) / FONT_HEIGHT (font));
24428
24429 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24430 temp_it.ascent + temp_it.descent,
24431 stretch_ascent);
24432 }
24433 }
24434 #endif
24435
24436 temp_it.dp = NULL;
24437 temp_it.what = IT_CHARACTER;
24438 temp_it.len = 1;
24439 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24440 temp_it.face_id = GLYPH_FACE (glyph);
24441 temp_it.len = CHAR_BYTES (temp_it.c);
24442
24443 PRODUCE_GLYPHS (&temp_it);
24444 it->pixel_width = temp_it.pixel_width;
24445 it->nglyphs = temp_it.pixel_width;
24446 }
24447
24448 #ifdef HAVE_WINDOW_SYSTEM
24449
24450 /* Calculate line-height and line-spacing properties.
24451 An integer value specifies explicit pixel value.
24452 A float value specifies relative value to current face height.
24453 A cons (float . face-name) specifies relative value to
24454 height of specified face font.
24455
24456 Returns height in pixels, or nil. */
24457
24458
24459 static Lisp_Object
24460 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24461 int boff, int override)
24462 {
24463 Lisp_Object face_name = Qnil;
24464 int ascent, descent, height;
24465
24466 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24467 return val;
24468
24469 if (CONSP (val))
24470 {
24471 face_name = XCAR (val);
24472 val = XCDR (val);
24473 if (!NUMBERP (val))
24474 val = make_number (1);
24475 if (NILP (face_name))
24476 {
24477 height = it->ascent + it->descent;
24478 goto scale;
24479 }
24480 }
24481
24482 if (NILP (face_name))
24483 {
24484 font = FRAME_FONT (it->f);
24485 boff = FRAME_BASELINE_OFFSET (it->f);
24486 }
24487 else if (EQ (face_name, Qt))
24488 {
24489 override = 0;
24490 }
24491 else
24492 {
24493 int face_id;
24494 struct face *face;
24495
24496 face_id = lookup_named_face (it->f, face_name, 0);
24497 if (face_id < 0)
24498 return make_number (-1);
24499
24500 face = FACE_FROM_ID (it->f, face_id);
24501 font = face->font;
24502 if (font == NULL)
24503 return make_number (-1);
24504 boff = font->baseline_offset;
24505 if (font->vertical_centering)
24506 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24507 }
24508
24509 ascent = FONT_BASE (font) + boff;
24510 descent = FONT_DESCENT (font) - boff;
24511
24512 if (override)
24513 {
24514 it->override_ascent = ascent;
24515 it->override_descent = descent;
24516 it->override_boff = boff;
24517 }
24518
24519 height = ascent + descent;
24520
24521 scale:
24522 if (FLOATP (val))
24523 height = (int)(XFLOAT_DATA (val) * height);
24524 else if (INTEGERP (val))
24525 height *= XINT (val);
24526
24527 return make_number (height);
24528 }
24529
24530
24531 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24532 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24533 and only if this is for a character for which no font was found.
24534
24535 If the display method (it->glyphless_method) is
24536 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24537 length of the acronym or the hexadecimal string, UPPER_XOFF and
24538 UPPER_YOFF are pixel offsets for the upper part of the string,
24539 LOWER_XOFF and LOWER_YOFF are for the lower part.
24540
24541 For the other display methods, LEN through LOWER_YOFF are zero. */
24542
24543 static void
24544 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24545 short upper_xoff, short upper_yoff,
24546 short lower_xoff, short lower_yoff)
24547 {
24548 struct glyph *glyph;
24549 enum glyph_row_area area = it->area;
24550
24551 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24552 if (glyph < it->glyph_row->glyphs[area + 1])
24553 {
24554 /* If the glyph row is reversed, we need to prepend the glyph
24555 rather than append it. */
24556 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24557 {
24558 struct glyph *g;
24559
24560 /* Make room for the additional glyph. */
24561 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24562 g[1] = *g;
24563 glyph = it->glyph_row->glyphs[area];
24564 }
24565 glyph->charpos = CHARPOS (it->position);
24566 glyph->object = it->object;
24567 glyph->pixel_width = it->pixel_width;
24568 glyph->ascent = it->ascent;
24569 glyph->descent = it->descent;
24570 glyph->voffset = it->voffset;
24571 glyph->type = GLYPHLESS_GLYPH;
24572 glyph->u.glyphless.method = it->glyphless_method;
24573 glyph->u.glyphless.for_no_font = for_no_font;
24574 glyph->u.glyphless.len = len;
24575 glyph->u.glyphless.ch = it->c;
24576 glyph->slice.glyphless.upper_xoff = upper_xoff;
24577 glyph->slice.glyphless.upper_yoff = upper_yoff;
24578 glyph->slice.glyphless.lower_xoff = lower_xoff;
24579 glyph->slice.glyphless.lower_yoff = lower_yoff;
24580 glyph->avoid_cursor_p = it->avoid_cursor_p;
24581 glyph->multibyte_p = it->multibyte_p;
24582 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24583 {
24584 /* In R2L rows, the left and the right box edges need to be
24585 drawn in reverse direction. */
24586 glyph->right_box_line_p = it->start_of_box_run_p;
24587 glyph->left_box_line_p = it->end_of_box_run_p;
24588 }
24589 else
24590 {
24591 glyph->left_box_line_p = it->start_of_box_run_p;
24592 glyph->right_box_line_p = it->end_of_box_run_p;
24593 }
24594 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24595 || it->phys_descent > it->descent);
24596 glyph->padding_p = 0;
24597 glyph->glyph_not_available_p = 0;
24598 glyph->face_id = face_id;
24599 glyph->font_type = FONT_TYPE_UNKNOWN;
24600 if (it->bidi_p)
24601 {
24602 glyph->resolved_level = it->bidi_it.resolved_level;
24603 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24604 emacs_abort ();
24605 glyph->bidi_type = it->bidi_it.type;
24606 }
24607 ++it->glyph_row->used[area];
24608 }
24609 else
24610 IT_EXPAND_MATRIX_WIDTH (it, area);
24611 }
24612
24613
24614 /* Produce a glyph for a glyphless character for iterator IT.
24615 IT->glyphless_method specifies which method to use for displaying
24616 the character. See the description of enum
24617 glyphless_display_method in dispextern.h for the detail.
24618
24619 FOR_NO_FONT is nonzero if and only if this is for a character for
24620 which no font was found. ACRONYM, if non-nil, is an acronym string
24621 for the character. */
24622
24623 static void
24624 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24625 {
24626 int face_id;
24627 struct face *face;
24628 struct font *font;
24629 int base_width, base_height, width, height;
24630 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24631 int len;
24632
24633 /* Get the metrics of the base font. We always refer to the current
24634 ASCII face. */
24635 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24636 font = face->font ? face->font : FRAME_FONT (it->f);
24637 it->ascent = FONT_BASE (font) + font->baseline_offset;
24638 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24639 base_height = it->ascent + it->descent;
24640 base_width = font->average_width;
24641
24642 /* Get a face ID for the glyph by utilizing a cache (the same way as
24643 done for `escape-glyph' in get_next_display_element). */
24644 if (it->f == last_glyphless_glyph_frame
24645 && it->face_id == last_glyphless_glyph_face_id)
24646 {
24647 face_id = last_glyphless_glyph_merged_face_id;
24648 }
24649 else
24650 {
24651 /* Merge the `glyphless-char' face into the current face. */
24652 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24653 last_glyphless_glyph_frame = it->f;
24654 last_glyphless_glyph_face_id = it->face_id;
24655 last_glyphless_glyph_merged_face_id = face_id;
24656 }
24657
24658 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24659 {
24660 it->pixel_width = THIN_SPACE_WIDTH;
24661 len = 0;
24662 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24663 }
24664 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24665 {
24666 width = CHAR_WIDTH (it->c);
24667 if (width == 0)
24668 width = 1;
24669 else if (width > 4)
24670 width = 4;
24671 it->pixel_width = base_width * width;
24672 len = 0;
24673 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24674 }
24675 else
24676 {
24677 char buf[7];
24678 const char *str;
24679 unsigned int code[6];
24680 int upper_len;
24681 int ascent, descent;
24682 struct font_metrics metrics_upper, metrics_lower;
24683
24684 face = FACE_FROM_ID (it->f, face_id);
24685 font = face->font ? face->font : FRAME_FONT (it->f);
24686 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24687
24688 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24689 {
24690 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24691 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24692 if (CONSP (acronym))
24693 acronym = XCAR (acronym);
24694 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24695 }
24696 else
24697 {
24698 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24699 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24700 str = buf;
24701 }
24702 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24703 code[len] = font->driver->encode_char (font, str[len]);
24704 upper_len = (len + 1) / 2;
24705 font->driver->text_extents (font, code, upper_len,
24706 &metrics_upper);
24707 font->driver->text_extents (font, code + upper_len, len - upper_len,
24708 &metrics_lower);
24709
24710
24711
24712 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24713 width = max (metrics_upper.width, metrics_lower.width) + 4;
24714 upper_xoff = upper_yoff = 2; /* the typical case */
24715 if (base_width >= width)
24716 {
24717 /* Align the upper to the left, the lower to the right. */
24718 it->pixel_width = base_width;
24719 lower_xoff = base_width - 2 - metrics_lower.width;
24720 }
24721 else
24722 {
24723 /* Center the shorter one. */
24724 it->pixel_width = width;
24725 if (metrics_upper.width >= metrics_lower.width)
24726 lower_xoff = (width - metrics_lower.width) / 2;
24727 else
24728 {
24729 /* FIXME: This code doesn't look right. It formerly was
24730 missing the "lower_xoff = 0;", which couldn't have
24731 been right since it left lower_xoff uninitialized. */
24732 lower_xoff = 0;
24733 upper_xoff = (width - metrics_upper.width) / 2;
24734 }
24735 }
24736
24737 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24738 top, bottom, and between upper and lower strings. */
24739 height = (metrics_upper.ascent + metrics_upper.descent
24740 + metrics_lower.ascent + metrics_lower.descent) + 5;
24741 /* Center vertically.
24742 H:base_height, D:base_descent
24743 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24744
24745 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24746 descent = D - H/2 + h/2;
24747 lower_yoff = descent - 2 - ld;
24748 upper_yoff = lower_yoff - la - 1 - ud; */
24749 ascent = - (it->descent - (base_height + height + 1) / 2);
24750 descent = it->descent - (base_height - height) / 2;
24751 lower_yoff = descent - 2 - metrics_lower.descent;
24752 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24753 - metrics_upper.descent);
24754 /* Don't make the height shorter than the base height. */
24755 if (height > base_height)
24756 {
24757 it->ascent = ascent;
24758 it->descent = descent;
24759 }
24760 }
24761
24762 it->phys_ascent = it->ascent;
24763 it->phys_descent = it->descent;
24764 if (it->glyph_row)
24765 append_glyphless_glyph (it, face_id, for_no_font, len,
24766 upper_xoff, upper_yoff,
24767 lower_xoff, lower_yoff);
24768 it->nglyphs = 1;
24769 take_vertical_position_into_account (it);
24770 }
24771
24772
24773 /* RIF:
24774 Produce glyphs/get display metrics for the display element IT is
24775 loaded with. See the description of struct it in dispextern.h
24776 for an overview of struct it. */
24777
24778 void
24779 x_produce_glyphs (struct it *it)
24780 {
24781 int extra_line_spacing = it->extra_line_spacing;
24782
24783 it->glyph_not_available_p = 0;
24784
24785 if (it->what == IT_CHARACTER)
24786 {
24787 XChar2b char2b;
24788 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24789 struct font *font = face->font;
24790 struct font_metrics *pcm = NULL;
24791 int boff; /* baseline offset */
24792
24793 if (font == NULL)
24794 {
24795 /* When no suitable font is found, display this character by
24796 the method specified in the first extra slot of
24797 Vglyphless_char_display. */
24798 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24799
24800 eassert (it->what == IT_GLYPHLESS);
24801 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24802 goto done;
24803 }
24804
24805 boff = font->baseline_offset;
24806 if (font->vertical_centering)
24807 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24808
24809 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24810 {
24811 int stretched_p;
24812
24813 it->nglyphs = 1;
24814
24815 if (it->override_ascent >= 0)
24816 {
24817 it->ascent = it->override_ascent;
24818 it->descent = it->override_descent;
24819 boff = it->override_boff;
24820 }
24821 else
24822 {
24823 it->ascent = FONT_BASE (font) + boff;
24824 it->descent = FONT_DESCENT (font) - boff;
24825 }
24826
24827 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24828 {
24829 pcm = get_per_char_metric (font, &char2b);
24830 if (pcm->width == 0
24831 && pcm->rbearing == 0 && pcm->lbearing == 0)
24832 pcm = NULL;
24833 }
24834
24835 if (pcm)
24836 {
24837 it->phys_ascent = pcm->ascent + boff;
24838 it->phys_descent = pcm->descent - boff;
24839 it->pixel_width = pcm->width;
24840 }
24841 else
24842 {
24843 it->glyph_not_available_p = 1;
24844 it->phys_ascent = it->ascent;
24845 it->phys_descent = it->descent;
24846 it->pixel_width = font->space_width;
24847 }
24848
24849 if (it->constrain_row_ascent_descent_p)
24850 {
24851 if (it->descent > it->max_descent)
24852 {
24853 it->ascent += it->descent - it->max_descent;
24854 it->descent = it->max_descent;
24855 }
24856 if (it->ascent > it->max_ascent)
24857 {
24858 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24859 it->ascent = it->max_ascent;
24860 }
24861 it->phys_ascent = min (it->phys_ascent, it->ascent);
24862 it->phys_descent = min (it->phys_descent, it->descent);
24863 extra_line_spacing = 0;
24864 }
24865
24866 /* If this is a space inside a region of text with
24867 `space-width' property, change its width. */
24868 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24869 if (stretched_p)
24870 it->pixel_width *= XFLOATINT (it->space_width);
24871
24872 /* If face has a box, add the box thickness to the character
24873 height. If character has a box line to the left and/or
24874 right, add the box line width to the character's width. */
24875 if (face->box != FACE_NO_BOX)
24876 {
24877 int thick = face->box_line_width;
24878
24879 if (thick > 0)
24880 {
24881 it->ascent += thick;
24882 it->descent += thick;
24883 }
24884 else
24885 thick = -thick;
24886
24887 if (it->start_of_box_run_p)
24888 it->pixel_width += thick;
24889 if (it->end_of_box_run_p)
24890 it->pixel_width += thick;
24891 }
24892
24893 /* If face has an overline, add the height of the overline
24894 (1 pixel) and a 1 pixel margin to the character height. */
24895 if (face->overline_p)
24896 it->ascent += overline_margin;
24897
24898 if (it->constrain_row_ascent_descent_p)
24899 {
24900 if (it->ascent > it->max_ascent)
24901 it->ascent = it->max_ascent;
24902 if (it->descent > it->max_descent)
24903 it->descent = it->max_descent;
24904 }
24905
24906 take_vertical_position_into_account (it);
24907
24908 /* If we have to actually produce glyphs, do it. */
24909 if (it->glyph_row)
24910 {
24911 if (stretched_p)
24912 {
24913 /* Translate a space with a `space-width' property
24914 into a stretch glyph. */
24915 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24916 / FONT_HEIGHT (font));
24917 append_stretch_glyph (it, it->object, it->pixel_width,
24918 it->ascent + it->descent, ascent);
24919 }
24920 else
24921 append_glyph (it);
24922
24923 /* If characters with lbearing or rbearing are displayed
24924 in this line, record that fact in a flag of the
24925 glyph row. This is used to optimize X output code. */
24926 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24927 it->glyph_row->contains_overlapping_glyphs_p = 1;
24928 }
24929 if (! stretched_p && it->pixel_width == 0)
24930 /* We assure that all visible glyphs have at least 1-pixel
24931 width. */
24932 it->pixel_width = 1;
24933 }
24934 else if (it->char_to_display == '\n')
24935 {
24936 /* A newline has no width, but we need the height of the
24937 line. But if previous part of the line sets a height,
24938 don't increase that height */
24939
24940 Lisp_Object height;
24941 Lisp_Object total_height = Qnil;
24942
24943 it->override_ascent = -1;
24944 it->pixel_width = 0;
24945 it->nglyphs = 0;
24946
24947 height = get_it_property (it, Qline_height);
24948 /* Split (line-height total-height) list */
24949 if (CONSP (height)
24950 && CONSP (XCDR (height))
24951 && NILP (XCDR (XCDR (height))))
24952 {
24953 total_height = XCAR (XCDR (height));
24954 height = XCAR (height);
24955 }
24956 height = calc_line_height_property (it, height, font, boff, 1);
24957
24958 if (it->override_ascent >= 0)
24959 {
24960 it->ascent = it->override_ascent;
24961 it->descent = it->override_descent;
24962 boff = it->override_boff;
24963 }
24964 else
24965 {
24966 it->ascent = FONT_BASE (font) + boff;
24967 it->descent = FONT_DESCENT (font) - boff;
24968 }
24969
24970 if (EQ (height, Qt))
24971 {
24972 if (it->descent > it->max_descent)
24973 {
24974 it->ascent += it->descent - it->max_descent;
24975 it->descent = it->max_descent;
24976 }
24977 if (it->ascent > it->max_ascent)
24978 {
24979 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24980 it->ascent = it->max_ascent;
24981 }
24982 it->phys_ascent = min (it->phys_ascent, it->ascent);
24983 it->phys_descent = min (it->phys_descent, it->descent);
24984 it->constrain_row_ascent_descent_p = 1;
24985 extra_line_spacing = 0;
24986 }
24987 else
24988 {
24989 Lisp_Object spacing;
24990
24991 it->phys_ascent = it->ascent;
24992 it->phys_descent = it->descent;
24993
24994 if ((it->max_ascent > 0 || it->max_descent > 0)
24995 && face->box != FACE_NO_BOX
24996 && face->box_line_width > 0)
24997 {
24998 it->ascent += face->box_line_width;
24999 it->descent += face->box_line_width;
25000 }
25001 if (!NILP (height)
25002 && XINT (height) > it->ascent + it->descent)
25003 it->ascent = XINT (height) - it->descent;
25004
25005 if (!NILP (total_height))
25006 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25007 else
25008 {
25009 spacing = get_it_property (it, Qline_spacing);
25010 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25011 }
25012 if (INTEGERP (spacing))
25013 {
25014 extra_line_spacing = XINT (spacing);
25015 if (!NILP (total_height))
25016 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25017 }
25018 }
25019 }
25020 else /* i.e. (it->char_to_display == '\t') */
25021 {
25022 if (font->space_width > 0)
25023 {
25024 int tab_width = it->tab_width * font->space_width;
25025 int x = it->current_x + it->continuation_lines_width;
25026 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25027
25028 /* If the distance from the current position to the next tab
25029 stop is less than a space character width, use the
25030 tab stop after that. */
25031 if (next_tab_x - x < font->space_width)
25032 next_tab_x += tab_width;
25033
25034 it->pixel_width = next_tab_x - x;
25035 it->nglyphs = 1;
25036 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25037 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25038
25039 if (it->glyph_row)
25040 {
25041 append_stretch_glyph (it, it->object, it->pixel_width,
25042 it->ascent + it->descent, it->ascent);
25043 }
25044 }
25045 else
25046 {
25047 it->pixel_width = 0;
25048 it->nglyphs = 1;
25049 }
25050 }
25051 }
25052 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25053 {
25054 /* A static composition.
25055
25056 Note: A composition is represented as one glyph in the
25057 glyph matrix. There are no padding glyphs.
25058
25059 Important note: pixel_width, ascent, and descent are the
25060 values of what is drawn by draw_glyphs (i.e. the values of
25061 the overall glyphs composed). */
25062 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25063 int boff; /* baseline offset */
25064 struct composition *cmp = composition_table[it->cmp_it.id];
25065 int glyph_len = cmp->glyph_len;
25066 struct font *font = face->font;
25067
25068 it->nglyphs = 1;
25069
25070 /* If we have not yet calculated pixel size data of glyphs of
25071 the composition for the current face font, calculate them
25072 now. Theoretically, we have to check all fonts for the
25073 glyphs, but that requires much time and memory space. So,
25074 here we check only the font of the first glyph. This may
25075 lead to incorrect display, but it's very rare, and C-l
25076 (recenter-top-bottom) can correct the display anyway. */
25077 if (! cmp->font || cmp->font != font)
25078 {
25079 /* Ascent and descent of the font of the first character
25080 of this composition (adjusted by baseline offset).
25081 Ascent and descent of overall glyphs should not be less
25082 than these, respectively. */
25083 int font_ascent, font_descent, font_height;
25084 /* Bounding box of the overall glyphs. */
25085 int leftmost, rightmost, lowest, highest;
25086 int lbearing, rbearing;
25087 int i, width, ascent, descent;
25088 int left_padded = 0, right_padded = 0;
25089 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25090 XChar2b char2b;
25091 struct font_metrics *pcm;
25092 int font_not_found_p;
25093 ptrdiff_t pos;
25094
25095 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25096 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25097 break;
25098 if (glyph_len < cmp->glyph_len)
25099 right_padded = 1;
25100 for (i = 0; i < glyph_len; i++)
25101 {
25102 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25103 break;
25104 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25105 }
25106 if (i > 0)
25107 left_padded = 1;
25108
25109 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25110 : IT_CHARPOS (*it));
25111 /* If no suitable font is found, use the default font. */
25112 font_not_found_p = font == NULL;
25113 if (font_not_found_p)
25114 {
25115 face = face->ascii_face;
25116 font = face->font;
25117 }
25118 boff = font->baseline_offset;
25119 if (font->vertical_centering)
25120 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25121 font_ascent = FONT_BASE (font) + boff;
25122 font_descent = FONT_DESCENT (font) - boff;
25123 font_height = FONT_HEIGHT (font);
25124
25125 cmp->font = font;
25126
25127 pcm = NULL;
25128 if (! font_not_found_p)
25129 {
25130 get_char_face_and_encoding (it->f, c, it->face_id,
25131 &char2b, 0);
25132 pcm = get_per_char_metric (font, &char2b);
25133 }
25134
25135 /* Initialize the bounding box. */
25136 if (pcm)
25137 {
25138 width = cmp->glyph_len > 0 ? pcm->width : 0;
25139 ascent = pcm->ascent;
25140 descent = pcm->descent;
25141 lbearing = pcm->lbearing;
25142 rbearing = pcm->rbearing;
25143 }
25144 else
25145 {
25146 width = cmp->glyph_len > 0 ? font->space_width : 0;
25147 ascent = FONT_BASE (font);
25148 descent = FONT_DESCENT (font);
25149 lbearing = 0;
25150 rbearing = width;
25151 }
25152
25153 rightmost = width;
25154 leftmost = 0;
25155 lowest = - descent + boff;
25156 highest = ascent + boff;
25157
25158 if (! font_not_found_p
25159 && font->default_ascent
25160 && CHAR_TABLE_P (Vuse_default_ascent)
25161 && !NILP (Faref (Vuse_default_ascent,
25162 make_number (it->char_to_display))))
25163 highest = font->default_ascent + boff;
25164
25165 /* Draw the first glyph at the normal position. It may be
25166 shifted to right later if some other glyphs are drawn
25167 at the left. */
25168 cmp->offsets[i * 2] = 0;
25169 cmp->offsets[i * 2 + 1] = boff;
25170 cmp->lbearing = lbearing;
25171 cmp->rbearing = rbearing;
25172
25173 /* Set cmp->offsets for the remaining glyphs. */
25174 for (i++; i < glyph_len; i++)
25175 {
25176 int left, right, btm, top;
25177 int ch = COMPOSITION_GLYPH (cmp, i);
25178 int face_id;
25179 struct face *this_face;
25180
25181 if (ch == '\t')
25182 ch = ' ';
25183 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25184 this_face = FACE_FROM_ID (it->f, face_id);
25185 font = this_face->font;
25186
25187 if (font == NULL)
25188 pcm = NULL;
25189 else
25190 {
25191 get_char_face_and_encoding (it->f, ch, face_id,
25192 &char2b, 0);
25193 pcm = get_per_char_metric (font, &char2b);
25194 }
25195 if (! pcm)
25196 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25197 else
25198 {
25199 width = pcm->width;
25200 ascent = pcm->ascent;
25201 descent = pcm->descent;
25202 lbearing = pcm->lbearing;
25203 rbearing = pcm->rbearing;
25204 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25205 {
25206 /* Relative composition with or without
25207 alternate chars. */
25208 left = (leftmost + rightmost - width) / 2;
25209 btm = - descent + boff;
25210 if (font->relative_compose
25211 && (! CHAR_TABLE_P (Vignore_relative_composition)
25212 || NILP (Faref (Vignore_relative_composition,
25213 make_number (ch)))))
25214 {
25215
25216 if (- descent >= font->relative_compose)
25217 /* One extra pixel between two glyphs. */
25218 btm = highest + 1;
25219 else if (ascent <= 0)
25220 /* One extra pixel between two glyphs. */
25221 btm = lowest - 1 - ascent - descent;
25222 }
25223 }
25224 else
25225 {
25226 /* A composition rule is specified by an integer
25227 value that encodes global and new reference
25228 points (GREF and NREF). GREF and NREF are
25229 specified by numbers as below:
25230
25231 0---1---2 -- ascent
25232 | |
25233 | |
25234 | |
25235 9--10--11 -- center
25236 | |
25237 ---3---4---5--- baseline
25238 | |
25239 6---7---8 -- descent
25240 */
25241 int rule = COMPOSITION_RULE (cmp, i);
25242 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25243
25244 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25245 grefx = gref % 3, nrefx = nref % 3;
25246 grefy = gref / 3, nrefy = nref / 3;
25247 if (xoff)
25248 xoff = font_height * (xoff - 128) / 256;
25249 if (yoff)
25250 yoff = font_height * (yoff - 128) / 256;
25251
25252 left = (leftmost
25253 + grefx * (rightmost - leftmost) / 2
25254 - nrefx * width / 2
25255 + xoff);
25256
25257 btm = ((grefy == 0 ? highest
25258 : grefy == 1 ? 0
25259 : grefy == 2 ? lowest
25260 : (highest + lowest) / 2)
25261 - (nrefy == 0 ? ascent + descent
25262 : nrefy == 1 ? descent - boff
25263 : nrefy == 2 ? 0
25264 : (ascent + descent) / 2)
25265 + yoff);
25266 }
25267
25268 cmp->offsets[i * 2] = left;
25269 cmp->offsets[i * 2 + 1] = btm + descent;
25270
25271 /* Update the bounding box of the overall glyphs. */
25272 if (width > 0)
25273 {
25274 right = left + width;
25275 if (left < leftmost)
25276 leftmost = left;
25277 if (right > rightmost)
25278 rightmost = right;
25279 }
25280 top = btm + descent + ascent;
25281 if (top > highest)
25282 highest = top;
25283 if (btm < lowest)
25284 lowest = btm;
25285
25286 if (cmp->lbearing > left + lbearing)
25287 cmp->lbearing = left + lbearing;
25288 if (cmp->rbearing < left + rbearing)
25289 cmp->rbearing = left + rbearing;
25290 }
25291 }
25292
25293 /* If there are glyphs whose x-offsets are negative,
25294 shift all glyphs to the right and make all x-offsets
25295 non-negative. */
25296 if (leftmost < 0)
25297 {
25298 for (i = 0; i < cmp->glyph_len; i++)
25299 cmp->offsets[i * 2] -= leftmost;
25300 rightmost -= leftmost;
25301 cmp->lbearing -= leftmost;
25302 cmp->rbearing -= leftmost;
25303 }
25304
25305 if (left_padded && cmp->lbearing < 0)
25306 {
25307 for (i = 0; i < cmp->glyph_len; i++)
25308 cmp->offsets[i * 2] -= cmp->lbearing;
25309 rightmost -= cmp->lbearing;
25310 cmp->rbearing -= cmp->lbearing;
25311 cmp->lbearing = 0;
25312 }
25313 if (right_padded && rightmost < cmp->rbearing)
25314 {
25315 rightmost = cmp->rbearing;
25316 }
25317
25318 cmp->pixel_width = rightmost;
25319 cmp->ascent = highest;
25320 cmp->descent = - lowest;
25321 if (cmp->ascent < font_ascent)
25322 cmp->ascent = font_ascent;
25323 if (cmp->descent < font_descent)
25324 cmp->descent = font_descent;
25325 }
25326
25327 if (it->glyph_row
25328 && (cmp->lbearing < 0
25329 || cmp->rbearing > cmp->pixel_width))
25330 it->glyph_row->contains_overlapping_glyphs_p = 1;
25331
25332 it->pixel_width = cmp->pixel_width;
25333 it->ascent = it->phys_ascent = cmp->ascent;
25334 it->descent = it->phys_descent = cmp->descent;
25335 if (face->box != FACE_NO_BOX)
25336 {
25337 int thick = face->box_line_width;
25338
25339 if (thick > 0)
25340 {
25341 it->ascent += thick;
25342 it->descent += thick;
25343 }
25344 else
25345 thick = - thick;
25346
25347 if (it->start_of_box_run_p)
25348 it->pixel_width += thick;
25349 if (it->end_of_box_run_p)
25350 it->pixel_width += thick;
25351 }
25352
25353 /* If face has an overline, add the height of the overline
25354 (1 pixel) and a 1 pixel margin to the character height. */
25355 if (face->overline_p)
25356 it->ascent += overline_margin;
25357
25358 take_vertical_position_into_account (it);
25359 if (it->ascent < 0)
25360 it->ascent = 0;
25361 if (it->descent < 0)
25362 it->descent = 0;
25363
25364 if (it->glyph_row && cmp->glyph_len > 0)
25365 append_composite_glyph (it);
25366 }
25367 else if (it->what == IT_COMPOSITION)
25368 {
25369 /* A dynamic (automatic) composition. */
25370 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25371 Lisp_Object gstring;
25372 struct font_metrics metrics;
25373
25374 it->nglyphs = 1;
25375
25376 gstring = composition_gstring_from_id (it->cmp_it.id);
25377 it->pixel_width
25378 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25379 &metrics);
25380 if (it->glyph_row
25381 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25382 it->glyph_row->contains_overlapping_glyphs_p = 1;
25383 it->ascent = it->phys_ascent = metrics.ascent;
25384 it->descent = it->phys_descent = metrics.descent;
25385 if (face->box != FACE_NO_BOX)
25386 {
25387 int thick = face->box_line_width;
25388
25389 if (thick > 0)
25390 {
25391 it->ascent += thick;
25392 it->descent += thick;
25393 }
25394 else
25395 thick = - thick;
25396
25397 if (it->start_of_box_run_p)
25398 it->pixel_width += thick;
25399 if (it->end_of_box_run_p)
25400 it->pixel_width += thick;
25401 }
25402 /* If face has an overline, add the height of the overline
25403 (1 pixel) and a 1 pixel margin to the character height. */
25404 if (face->overline_p)
25405 it->ascent += overline_margin;
25406 take_vertical_position_into_account (it);
25407 if (it->ascent < 0)
25408 it->ascent = 0;
25409 if (it->descent < 0)
25410 it->descent = 0;
25411
25412 if (it->glyph_row)
25413 append_composite_glyph (it);
25414 }
25415 else if (it->what == IT_GLYPHLESS)
25416 produce_glyphless_glyph (it, 0, Qnil);
25417 else if (it->what == IT_IMAGE)
25418 produce_image_glyph (it);
25419 else if (it->what == IT_STRETCH)
25420 produce_stretch_glyph (it);
25421 #ifdef HAVE_XWIDGETS
25422 else if (it->what == IT_XWIDGET)
25423 produce_xwidget_glyph (it);
25424 #endif
25425 done:
25426 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25427 because this isn't true for images with `:ascent 100'. */
25428 eassert (it->ascent >= 0 && it->descent >= 0);
25429 if (it->area == TEXT_AREA)
25430 it->current_x += it->pixel_width;
25431
25432 if (extra_line_spacing > 0)
25433 {
25434 it->descent += extra_line_spacing;
25435 if (extra_line_spacing > it->max_extra_line_spacing)
25436 it->max_extra_line_spacing = extra_line_spacing;
25437 }
25438
25439 it->max_ascent = max (it->max_ascent, it->ascent);
25440 it->max_descent = max (it->max_descent, it->descent);
25441 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25442 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25443 }
25444
25445 /* EXPORT for RIF:
25446 Output LEN glyphs starting at START at the nominal cursor position.
25447 Advance the nominal cursor over the text. The global variable
25448 updated_window contains the window being updated, updated_row is
25449 the glyph row being updated, and updated_area is the area of that
25450 row being updated. */
25451
25452 void
25453 x_write_glyphs (struct glyph *start, int len)
25454 {
25455 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25456
25457 eassert (updated_window && updated_row);
25458 /* When the window is hscrolled, cursor hpos can legitimately be out
25459 of bounds, but we draw the cursor at the corresponding window
25460 margin in that case. */
25461 if (!updated_row->reversed_p && chpos < 0)
25462 chpos = 0;
25463 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25464 chpos = updated_row->used[TEXT_AREA] - 1;
25465
25466 block_input ();
25467
25468 /* Write glyphs. */
25469
25470 hpos = start - updated_row->glyphs[updated_area];
25471 x = draw_glyphs (updated_window, output_cursor.x,
25472 updated_row, updated_area,
25473 hpos, hpos + len,
25474 DRAW_NORMAL_TEXT, 0);
25475
25476 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25477 if (updated_area == TEXT_AREA
25478 && updated_window->phys_cursor_on_p
25479 && updated_window->phys_cursor.vpos == output_cursor.vpos
25480 && chpos >= hpos
25481 && chpos < hpos + len)
25482 updated_window->phys_cursor_on_p = 0;
25483
25484 unblock_input ();
25485
25486 /* Advance the output cursor. */
25487 output_cursor.hpos += len;
25488 output_cursor.x = x;
25489 }
25490
25491
25492 /* EXPORT for RIF:
25493 Insert LEN glyphs from START at the nominal cursor position. */
25494
25495 void
25496 x_insert_glyphs (struct glyph *start, int len)
25497 {
25498 struct frame *f;
25499 struct window *w;
25500 int line_height, shift_by_width, shifted_region_width;
25501 struct glyph_row *row;
25502 struct glyph *glyph;
25503 int frame_x, frame_y;
25504 ptrdiff_t hpos;
25505
25506 eassert (updated_window && updated_row);
25507 block_input ();
25508 w = updated_window;
25509 f = XFRAME (WINDOW_FRAME (w));
25510
25511 /* Get the height of the line we are in. */
25512 row = updated_row;
25513 line_height = row->height;
25514
25515 /* Get the width of the glyphs to insert. */
25516 shift_by_width = 0;
25517 for (glyph = start; glyph < start + len; ++glyph)
25518 shift_by_width += glyph->pixel_width;
25519
25520 /* Get the width of the region to shift right. */
25521 shifted_region_width = (window_box_width (w, updated_area)
25522 - output_cursor.x
25523 - shift_by_width);
25524
25525 /* Shift right. */
25526 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25527 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25528
25529 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25530 line_height, shift_by_width);
25531
25532 /* Write the glyphs. */
25533 hpos = start - row->glyphs[updated_area];
25534 draw_glyphs (w, output_cursor.x, row, updated_area,
25535 hpos, hpos + len,
25536 DRAW_NORMAL_TEXT, 0);
25537
25538 /* Advance the output cursor. */
25539 output_cursor.hpos += len;
25540 output_cursor.x += shift_by_width;
25541 unblock_input ();
25542 }
25543
25544
25545 /* EXPORT for RIF:
25546 Erase the current text line from the nominal cursor position
25547 (inclusive) to pixel column TO_X (exclusive). The idea is that
25548 everything from TO_X onward is already erased.
25549
25550 TO_X is a pixel position relative to updated_area of
25551 updated_window. TO_X == -1 means clear to the end of this area. */
25552
25553 void
25554 x_clear_end_of_line (int to_x)
25555 {
25556 struct frame *f;
25557 struct window *w = updated_window;
25558 int max_x, min_y, max_y;
25559 int from_x, from_y, to_y;
25560
25561 eassert (updated_window && updated_row);
25562 f = XFRAME (w->frame);
25563
25564 if (updated_row->full_width_p)
25565 max_x = WINDOW_TOTAL_WIDTH (w);
25566 else
25567 max_x = window_box_width (w, updated_area);
25568 max_y = window_text_bottom_y (w);
25569
25570 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25571 of window. For TO_X > 0, truncate to end of drawing area. */
25572 if (to_x == 0)
25573 return;
25574 else if (to_x < 0)
25575 to_x = max_x;
25576 else
25577 to_x = min (to_x, max_x);
25578
25579 to_y = min (max_y, output_cursor.y + updated_row->height);
25580
25581 /* Notice if the cursor will be cleared by this operation. */
25582 if (!updated_row->full_width_p)
25583 notice_overwritten_cursor (w, updated_area,
25584 output_cursor.x, -1,
25585 updated_row->y,
25586 MATRIX_ROW_BOTTOM_Y (updated_row));
25587
25588 from_x = output_cursor.x;
25589
25590 /* Translate to frame coordinates. */
25591 if (updated_row->full_width_p)
25592 {
25593 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25594 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25595 }
25596 else
25597 {
25598 int area_left = window_box_left (w, updated_area);
25599 from_x += area_left;
25600 to_x += area_left;
25601 }
25602
25603 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25604 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25605 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25606
25607 /* Prevent inadvertently clearing to end of the X window. */
25608 if (to_x > from_x && to_y > from_y)
25609 {
25610 block_input ();
25611 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25612 to_x - from_x, to_y - from_y);
25613 unblock_input ();
25614 }
25615 }
25616
25617 #endif /* HAVE_WINDOW_SYSTEM */
25618
25619
25620 \f
25621 /***********************************************************************
25622 Cursor types
25623 ***********************************************************************/
25624
25625 /* Value is the internal representation of the specified cursor type
25626 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25627 of the bar cursor. */
25628
25629 static enum text_cursor_kinds
25630 get_specified_cursor_type (Lisp_Object arg, int *width)
25631 {
25632 enum text_cursor_kinds type;
25633
25634 if (NILP (arg))
25635 return NO_CURSOR;
25636
25637 if (EQ (arg, Qbox))
25638 return FILLED_BOX_CURSOR;
25639
25640 if (EQ (arg, Qhollow))
25641 return HOLLOW_BOX_CURSOR;
25642
25643 if (EQ (arg, Qbar))
25644 {
25645 *width = 2;
25646 return BAR_CURSOR;
25647 }
25648
25649 if (CONSP (arg)
25650 && EQ (XCAR (arg), Qbar)
25651 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25652 {
25653 *width = XINT (XCDR (arg));
25654 return BAR_CURSOR;
25655 }
25656
25657 if (EQ (arg, Qhbar))
25658 {
25659 *width = 2;
25660 return HBAR_CURSOR;
25661 }
25662
25663 if (CONSP (arg)
25664 && EQ (XCAR (arg), Qhbar)
25665 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25666 {
25667 *width = XINT (XCDR (arg));
25668 return HBAR_CURSOR;
25669 }
25670
25671 /* Treat anything unknown as "hollow box cursor".
25672 It was bad to signal an error; people have trouble fixing
25673 .Xdefaults with Emacs, when it has something bad in it. */
25674 type = HOLLOW_BOX_CURSOR;
25675
25676 return type;
25677 }
25678
25679 /* Set the default cursor types for specified frame. */
25680 void
25681 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25682 {
25683 int width = 1;
25684 Lisp_Object tem;
25685
25686 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25687 FRAME_CURSOR_WIDTH (f) = width;
25688
25689 /* By default, set up the blink-off state depending on the on-state. */
25690
25691 tem = Fassoc (arg, Vblink_cursor_alist);
25692 if (!NILP (tem))
25693 {
25694 FRAME_BLINK_OFF_CURSOR (f)
25695 = get_specified_cursor_type (XCDR (tem), &width);
25696 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25697 }
25698 else
25699 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25700 }
25701
25702
25703 #ifdef HAVE_WINDOW_SYSTEM
25704
25705 /* Return the cursor we want to be displayed in window W. Return
25706 width of bar/hbar cursor through WIDTH arg. Return with
25707 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25708 (i.e. if the `system caret' should track this cursor).
25709
25710 In a mini-buffer window, we want the cursor only to appear if we
25711 are reading input from this window. For the selected window, we
25712 want the cursor type given by the frame parameter or buffer local
25713 setting of cursor-type. If explicitly marked off, draw no cursor.
25714 In all other cases, we want a hollow box cursor. */
25715
25716 static enum text_cursor_kinds
25717 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25718 int *active_cursor)
25719 {
25720 struct frame *f = XFRAME (w->frame);
25721 struct buffer *b = XBUFFER (w->contents);
25722 int cursor_type = DEFAULT_CURSOR;
25723 Lisp_Object alt_cursor;
25724 int non_selected = 0;
25725
25726 *active_cursor = 1;
25727
25728 /* Echo area */
25729 if (cursor_in_echo_area
25730 && FRAME_HAS_MINIBUF_P (f)
25731 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25732 {
25733 if (w == XWINDOW (echo_area_window))
25734 {
25735 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25736 {
25737 *width = FRAME_CURSOR_WIDTH (f);
25738 return FRAME_DESIRED_CURSOR (f);
25739 }
25740 else
25741 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25742 }
25743
25744 *active_cursor = 0;
25745 non_selected = 1;
25746 }
25747
25748 /* Detect a nonselected window or nonselected frame. */
25749 else if (w != XWINDOW (f->selected_window)
25750 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25751 {
25752 *active_cursor = 0;
25753
25754 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25755 return NO_CURSOR;
25756
25757 non_selected = 1;
25758 }
25759
25760 /* Never display a cursor in a window in which cursor-type is nil. */
25761 if (NILP (BVAR (b, cursor_type)))
25762 return NO_CURSOR;
25763
25764 /* Get the normal cursor type for this window. */
25765 if (EQ (BVAR (b, cursor_type), Qt))
25766 {
25767 cursor_type = FRAME_DESIRED_CURSOR (f);
25768 *width = FRAME_CURSOR_WIDTH (f);
25769 }
25770 else
25771 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25772
25773 /* Use cursor-in-non-selected-windows instead
25774 for non-selected window or frame. */
25775 if (non_selected)
25776 {
25777 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25778 if (!EQ (Qt, alt_cursor))
25779 return get_specified_cursor_type (alt_cursor, width);
25780 /* t means modify the normal cursor type. */
25781 if (cursor_type == FILLED_BOX_CURSOR)
25782 cursor_type = HOLLOW_BOX_CURSOR;
25783 else if (cursor_type == BAR_CURSOR && *width > 1)
25784 --*width;
25785 return cursor_type;
25786 }
25787
25788 /* Use normal cursor if not blinked off. */
25789 if (!w->cursor_off_p)
25790 {
25791
25792 #ifdef HAVE_XWIDGETS
25793 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
25794 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
25795 return NO_CURSOR;
25796 }
25797 #endif
25798 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25799 {
25800 if (cursor_type == FILLED_BOX_CURSOR)
25801 {
25802 /* Using a block cursor on large images can be very annoying.
25803 So use a hollow cursor for "large" images.
25804 If image is not transparent (no mask), also use hollow cursor. */
25805 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25806 if (img != NULL && IMAGEP (img->spec))
25807 {
25808 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25809 where N = size of default frame font size.
25810 This should cover most of the "tiny" icons people may use. */
25811 if (!img->mask
25812 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25813 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25814 cursor_type = HOLLOW_BOX_CURSOR;
25815 }
25816 }
25817 else if (cursor_type != NO_CURSOR)
25818 {
25819 /* Display current only supports BOX and HOLLOW cursors for images.
25820 So for now, unconditionally use a HOLLOW cursor when cursor is
25821 not a solid box cursor. */
25822 cursor_type = HOLLOW_BOX_CURSOR;
25823 }
25824 }
25825 return cursor_type;
25826 }
25827
25828 /* Cursor is blinked off, so determine how to "toggle" it. */
25829
25830 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25831 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25832 return get_specified_cursor_type (XCDR (alt_cursor), width);
25833
25834 /* Then see if frame has specified a specific blink off cursor type. */
25835 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25836 {
25837 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25838 return FRAME_BLINK_OFF_CURSOR (f);
25839 }
25840
25841 #if 0
25842 /* Some people liked having a permanently visible blinking cursor,
25843 while others had very strong opinions against it. So it was
25844 decided to remove it. KFS 2003-09-03 */
25845
25846 /* Finally perform built-in cursor blinking:
25847 filled box <-> hollow box
25848 wide [h]bar <-> narrow [h]bar
25849 narrow [h]bar <-> no cursor
25850 other type <-> no cursor */
25851
25852 if (cursor_type == FILLED_BOX_CURSOR)
25853 return HOLLOW_BOX_CURSOR;
25854
25855 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25856 {
25857 *width = 1;
25858 return cursor_type;
25859 }
25860 #endif
25861
25862 return NO_CURSOR;
25863 }
25864
25865
25866 /* Notice when the text cursor of window W has been completely
25867 overwritten by a drawing operation that outputs glyphs in AREA
25868 starting at X0 and ending at X1 in the line starting at Y0 and
25869 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25870 the rest of the line after X0 has been written. Y coordinates
25871 are window-relative. */
25872
25873 static void
25874 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25875 int x0, int x1, int y0, int y1)
25876 {
25877 int cx0, cx1, cy0, cy1;
25878 struct glyph_row *row;
25879
25880 if (!w->phys_cursor_on_p)
25881 return;
25882 if (area != TEXT_AREA)
25883 return;
25884
25885 if (w->phys_cursor.vpos < 0
25886 || w->phys_cursor.vpos >= w->current_matrix->nrows
25887 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25888 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
25889 return;
25890
25891 if (row->cursor_in_fringe_p)
25892 {
25893 row->cursor_in_fringe_p = 0;
25894 draw_fringe_bitmap (w, row, row->reversed_p);
25895 w->phys_cursor_on_p = 0;
25896 return;
25897 }
25898
25899 cx0 = w->phys_cursor.x;
25900 cx1 = cx0 + w->phys_cursor_width;
25901 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25902 return;
25903
25904 /* The cursor image will be completely removed from the
25905 screen if the output area intersects the cursor area in
25906 y-direction. When we draw in [y0 y1[, and some part of
25907 the cursor is at y < y0, that part must have been drawn
25908 before. When scrolling, the cursor is erased before
25909 actually scrolling, so we don't come here. When not
25910 scrolling, the rows above the old cursor row must have
25911 changed, and in this case these rows must have written
25912 over the cursor image.
25913
25914 Likewise if part of the cursor is below y1, with the
25915 exception of the cursor being in the first blank row at
25916 the buffer and window end because update_text_area
25917 doesn't draw that row. (Except when it does, but
25918 that's handled in update_text_area.) */
25919
25920 cy0 = w->phys_cursor.y;
25921 cy1 = cy0 + w->phys_cursor_height;
25922 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25923 return;
25924
25925 w->phys_cursor_on_p = 0;
25926 }
25927
25928 #endif /* HAVE_WINDOW_SYSTEM */
25929
25930 \f
25931 /************************************************************************
25932 Mouse Face
25933 ************************************************************************/
25934
25935 #ifdef HAVE_WINDOW_SYSTEM
25936
25937 /* EXPORT for RIF:
25938 Fix the display of area AREA of overlapping row ROW in window W
25939 with respect to the overlapping part OVERLAPS. */
25940
25941 void
25942 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25943 enum glyph_row_area area, int overlaps)
25944 {
25945 int i, x;
25946
25947 block_input ();
25948
25949 x = 0;
25950 for (i = 0; i < row->used[area];)
25951 {
25952 if (row->glyphs[area][i].overlaps_vertically_p)
25953 {
25954 int start = i, start_x = x;
25955
25956 do
25957 {
25958 x += row->glyphs[area][i].pixel_width;
25959 ++i;
25960 }
25961 while (i < row->used[area]
25962 && row->glyphs[area][i].overlaps_vertically_p);
25963
25964 draw_glyphs (w, start_x, row, area,
25965 start, i,
25966 DRAW_NORMAL_TEXT, overlaps);
25967 }
25968 else
25969 {
25970 x += row->glyphs[area][i].pixel_width;
25971 ++i;
25972 }
25973 }
25974
25975 unblock_input ();
25976 }
25977
25978
25979 /* EXPORT:
25980 Draw the cursor glyph of window W in glyph row ROW. See the
25981 comment of draw_glyphs for the meaning of HL. */
25982
25983 void
25984 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25985 enum draw_glyphs_face hl)
25986 {
25987 /* If cursor hpos is out of bounds, don't draw garbage. This can
25988 happen in mini-buffer windows when switching between echo area
25989 glyphs and mini-buffer. */
25990 if ((row->reversed_p
25991 ? (w->phys_cursor.hpos >= 0)
25992 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25993 {
25994 int on_p = w->phys_cursor_on_p;
25995 int x1;
25996 int hpos = w->phys_cursor.hpos;
25997
25998 /* When the window is hscrolled, cursor hpos can legitimately be
25999 out of bounds, but we draw the cursor at the corresponding
26000 window margin in that case. */
26001 if (!row->reversed_p && hpos < 0)
26002 hpos = 0;
26003 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26004 hpos = row->used[TEXT_AREA] - 1;
26005
26006 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26007 hl, 0);
26008 w->phys_cursor_on_p = on_p;
26009
26010 if (hl == DRAW_CURSOR)
26011 w->phys_cursor_width = x1 - w->phys_cursor.x;
26012 /* When we erase the cursor, and ROW is overlapped by other
26013 rows, make sure that these overlapping parts of other rows
26014 are redrawn. */
26015 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26016 {
26017 w->phys_cursor_width = x1 - w->phys_cursor.x;
26018
26019 if (row > w->current_matrix->rows
26020 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26021 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26022 OVERLAPS_ERASED_CURSOR);
26023
26024 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26025 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26026 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26027 OVERLAPS_ERASED_CURSOR);
26028 }
26029 }
26030 }
26031
26032
26033 /* EXPORT:
26034 Erase the image of a cursor of window W from the screen. */
26035
26036 void
26037 erase_phys_cursor (struct window *w)
26038 {
26039 struct frame *f = XFRAME (w->frame);
26040 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26041 int hpos = w->phys_cursor.hpos;
26042 int vpos = w->phys_cursor.vpos;
26043 int mouse_face_here_p = 0;
26044 struct glyph_matrix *active_glyphs = w->current_matrix;
26045 struct glyph_row *cursor_row;
26046 struct glyph *cursor_glyph;
26047 enum draw_glyphs_face hl;
26048
26049 /* No cursor displayed or row invalidated => nothing to do on the
26050 screen. */
26051 if (w->phys_cursor_type == NO_CURSOR)
26052 goto mark_cursor_off;
26053
26054 /* VPOS >= active_glyphs->nrows means that window has been resized.
26055 Don't bother to erase the cursor. */
26056 if (vpos >= active_glyphs->nrows)
26057 goto mark_cursor_off;
26058
26059 /* If row containing cursor is marked invalid, there is nothing we
26060 can do. */
26061 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26062 if (!cursor_row->enabled_p)
26063 goto mark_cursor_off;
26064
26065 /* If line spacing is > 0, old cursor may only be partially visible in
26066 window after split-window. So adjust visible height. */
26067 cursor_row->visible_height = min (cursor_row->visible_height,
26068 window_text_bottom_y (w) - cursor_row->y);
26069
26070 /* If row is completely invisible, don't attempt to delete a cursor which
26071 isn't there. This can happen if cursor is at top of a window, and
26072 we switch to a buffer with a header line in that window. */
26073 if (cursor_row->visible_height <= 0)
26074 goto mark_cursor_off;
26075
26076 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26077 if (cursor_row->cursor_in_fringe_p)
26078 {
26079 cursor_row->cursor_in_fringe_p = 0;
26080 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26081 goto mark_cursor_off;
26082 }
26083
26084 /* This can happen when the new row is shorter than the old one.
26085 In this case, either draw_glyphs or clear_end_of_line
26086 should have cleared the cursor. Note that we wouldn't be
26087 able to erase the cursor in this case because we don't have a
26088 cursor glyph at hand. */
26089 if ((cursor_row->reversed_p
26090 ? (w->phys_cursor.hpos < 0)
26091 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26092 goto mark_cursor_off;
26093
26094 /* When the window is hscrolled, cursor hpos can legitimately be out
26095 of bounds, but we draw the cursor at the corresponding window
26096 margin in that case. */
26097 if (!cursor_row->reversed_p && hpos < 0)
26098 hpos = 0;
26099 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26100 hpos = cursor_row->used[TEXT_AREA] - 1;
26101
26102 /* If the cursor is in the mouse face area, redisplay that when
26103 we clear the cursor. */
26104 if (! NILP (hlinfo->mouse_face_window)
26105 && coords_in_mouse_face_p (w, hpos, vpos)
26106 /* Don't redraw the cursor's spot in mouse face if it is at the
26107 end of a line (on a newline). The cursor appears there, but
26108 mouse highlighting does not. */
26109 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26110 mouse_face_here_p = 1;
26111
26112 /* Maybe clear the display under the cursor. */
26113 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26114 {
26115 int x, y, left_x;
26116 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26117 int width;
26118
26119 cursor_glyph = get_phys_cursor_glyph (w);
26120 if (cursor_glyph == NULL)
26121 goto mark_cursor_off;
26122
26123 width = cursor_glyph->pixel_width;
26124 left_x = window_box_left_offset (w, TEXT_AREA);
26125 x = w->phys_cursor.x;
26126 if (x < left_x)
26127 width -= left_x - x;
26128 width = min (width, window_box_width (w, TEXT_AREA) - x);
26129 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26130 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26131
26132 if (width > 0)
26133 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26134 }
26135
26136 /* Erase the cursor by redrawing the character underneath it. */
26137 if (mouse_face_here_p)
26138 hl = DRAW_MOUSE_FACE;
26139 else
26140 hl = DRAW_NORMAL_TEXT;
26141 draw_phys_cursor_glyph (w, cursor_row, hl);
26142
26143 mark_cursor_off:
26144 w->phys_cursor_on_p = 0;
26145 w->phys_cursor_type = NO_CURSOR;
26146 }
26147
26148
26149 /* EXPORT:
26150 Display or clear cursor of window W. If ON is zero, clear the
26151 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26152 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26153
26154 void
26155 display_and_set_cursor (struct window *w, int on,
26156 int hpos, int vpos, int x, int y)
26157 {
26158 struct frame *f = XFRAME (w->frame);
26159 int new_cursor_type;
26160 int new_cursor_width;
26161 int active_cursor;
26162 struct glyph_row *glyph_row;
26163 struct glyph *glyph;
26164
26165 /* This is pointless on invisible frames, and dangerous on garbaged
26166 windows and frames; in the latter case, the frame or window may
26167 be in the midst of changing its size, and x and y may be off the
26168 window. */
26169 if (! FRAME_VISIBLE_P (f)
26170 || FRAME_GARBAGED_P (f)
26171 || vpos >= w->current_matrix->nrows
26172 || hpos >= w->current_matrix->matrix_w)
26173 return;
26174
26175 /* If cursor is off and we want it off, return quickly. */
26176 if (!on && !w->phys_cursor_on_p)
26177 return;
26178
26179 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26180 /* If cursor row is not enabled, we don't really know where to
26181 display the cursor. */
26182 if (!glyph_row->enabled_p)
26183 {
26184 w->phys_cursor_on_p = 0;
26185 return;
26186 }
26187
26188 glyph = NULL;
26189 if (!glyph_row->exact_window_width_line_p
26190 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26191 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26192
26193 eassert (input_blocked_p ());
26194
26195 /* Set new_cursor_type to the cursor we want to be displayed. */
26196 new_cursor_type = get_window_cursor_type (w, glyph,
26197 &new_cursor_width, &active_cursor);
26198
26199 /* If cursor is currently being shown and we don't want it to be or
26200 it is in the wrong place, or the cursor type is not what we want,
26201 erase it. */
26202 if (w->phys_cursor_on_p
26203 && (!on
26204 || w->phys_cursor.x != x
26205 || w->phys_cursor.y != y
26206 || new_cursor_type != w->phys_cursor_type
26207 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26208 && new_cursor_width != w->phys_cursor_width)))
26209 erase_phys_cursor (w);
26210
26211 /* Don't check phys_cursor_on_p here because that flag is only set
26212 to zero in some cases where we know that the cursor has been
26213 completely erased, to avoid the extra work of erasing the cursor
26214 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26215 still not be visible, or it has only been partly erased. */
26216 if (on)
26217 {
26218 w->phys_cursor_ascent = glyph_row->ascent;
26219 w->phys_cursor_height = glyph_row->height;
26220
26221 /* Set phys_cursor_.* before x_draw_.* is called because some
26222 of them may need the information. */
26223 w->phys_cursor.x = x;
26224 w->phys_cursor.y = glyph_row->y;
26225 w->phys_cursor.hpos = hpos;
26226 w->phys_cursor.vpos = vpos;
26227 }
26228
26229 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26230 new_cursor_type, new_cursor_width,
26231 on, active_cursor);
26232 }
26233
26234
26235 /* Switch the display of W's cursor on or off, according to the value
26236 of ON. */
26237
26238 static void
26239 update_window_cursor (struct window *w, int on)
26240 {
26241 /* Don't update cursor in windows whose frame is in the process
26242 of being deleted. */
26243 if (w->current_matrix)
26244 {
26245 int hpos = w->phys_cursor.hpos;
26246 int vpos = w->phys_cursor.vpos;
26247 struct glyph_row *row;
26248
26249 if (vpos >= w->current_matrix->nrows
26250 || hpos >= w->current_matrix->matrix_w)
26251 return;
26252
26253 row = MATRIX_ROW (w->current_matrix, vpos);
26254
26255 /* When the window is hscrolled, cursor hpos can legitimately be
26256 out of bounds, but we draw the cursor at the corresponding
26257 window margin in that case. */
26258 if (!row->reversed_p && hpos < 0)
26259 hpos = 0;
26260 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26261 hpos = row->used[TEXT_AREA] - 1;
26262
26263 block_input ();
26264 display_and_set_cursor (w, on, hpos, vpos,
26265 w->phys_cursor.x, w->phys_cursor.y);
26266 unblock_input ();
26267 }
26268 }
26269
26270
26271 /* Call update_window_cursor with parameter ON_P on all leaf windows
26272 in the window tree rooted at W. */
26273
26274 static void
26275 update_cursor_in_window_tree (struct window *w, int on_p)
26276 {
26277 while (w)
26278 {
26279 if (WINDOWP (w->contents))
26280 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26281 else
26282 update_window_cursor (w, on_p);
26283
26284 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26285 }
26286 }
26287
26288
26289 /* EXPORT:
26290 Display the cursor on window W, or clear it, according to ON_P.
26291 Don't change the cursor's position. */
26292
26293 void
26294 x_update_cursor (struct frame *f, int on_p)
26295 {
26296 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26297 }
26298
26299
26300 /* EXPORT:
26301 Clear the cursor of window W to background color, and mark the
26302 cursor as not shown. This is used when the text where the cursor
26303 is about to be rewritten. */
26304
26305 void
26306 x_clear_cursor (struct window *w)
26307 {
26308 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26309 update_window_cursor (w, 0);
26310 }
26311
26312 #endif /* HAVE_WINDOW_SYSTEM */
26313
26314 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26315 and MSDOS. */
26316 static void
26317 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26318 int start_hpos, int end_hpos,
26319 enum draw_glyphs_face draw)
26320 {
26321 #ifdef HAVE_WINDOW_SYSTEM
26322 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26323 {
26324 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26325 return;
26326 }
26327 #endif
26328 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26329 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26330 #endif
26331 }
26332
26333 /* Display the active region described by mouse_face_* according to DRAW. */
26334
26335 static void
26336 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26337 {
26338 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26339 struct frame *f = XFRAME (WINDOW_FRAME (w));
26340
26341 if (/* If window is in the process of being destroyed, don't bother
26342 to do anything. */
26343 w->current_matrix != NULL
26344 /* Don't update mouse highlight if hidden */
26345 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26346 /* Recognize when we are called to operate on rows that don't exist
26347 anymore. This can happen when a window is split. */
26348 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26349 {
26350 int phys_cursor_on_p = w->phys_cursor_on_p;
26351 struct glyph_row *row, *first, *last;
26352
26353 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26354 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26355
26356 for (row = first; row <= last && row->enabled_p; ++row)
26357 {
26358 int start_hpos, end_hpos, start_x;
26359
26360 /* For all but the first row, the highlight starts at column 0. */
26361 if (row == first)
26362 {
26363 /* R2L rows have BEG and END in reversed order, but the
26364 screen drawing geometry is always left to right. So
26365 we need to mirror the beginning and end of the
26366 highlighted area in R2L rows. */
26367 if (!row->reversed_p)
26368 {
26369 start_hpos = hlinfo->mouse_face_beg_col;
26370 start_x = hlinfo->mouse_face_beg_x;
26371 }
26372 else if (row == last)
26373 {
26374 start_hpos = hlinfo->mouse_face_end_col;
26375 start_x = hlinfo->mouse_face_end_x;
26376 }
26377 else
26378 {
26379 start_hpos = 0;
26380 start_x = 0;
26381 }
26382 }
26383 else if (row->reversed_p && row == last)
26384 {
26385 start_hpos = hlinfo->mouse_face_end_col;
26386 start_x = hlinfo->mouse_face_end_x;
26387 }
26388 else
26389 {
26390 start_hpos = 0;
26391 start_x = 0;
26392 }
26393
26394 if (row == last)
26395 {
26396 if (!row->reversed_p)
26397 end_hpos = hlinfo->mouse_face_end_col;
26398 else if (row == first)
26399 end_hpos = hlinfo->mouse_face_beg_col;
26400 else
26401 {
26402 end_hpos = row->used[TEXT_AREA];
26403 if (draw == DRAW_NORMAL_TEXT)
26404 row->fill_line_p = 1; /* Clear to end of line */
26405 }
26406 }
26407 else if (row->reversed_p && row == first)
26408 end_hpos = hlinfo->mouse_face_beg_col;
26409 else
26410 {
26411 end_hpos = row->used[TEXT_AREA];
26412 if (draw == DRAW_NORMAL_TEXT)
26413 row->fill_line_p = 1; /* Clear to end of line */
26414 }
26415
26416 if (end_hpos > start_hpos)
26417 {
26418 draw_row_with_mouse_face (w, start_x, row,
26419 start_hpos, end_hpos, draw);
26420
26421 row->mouse_face_p
26422 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26423 }
26424 }
26425
26426 #ifdef HAVE_WINDOW_SYSTEM
26427 /* When we've written over the cursor, arrange for it to
26428 be displayed again. */
26429 if (FRAME_WINDOW_P (f)
26430 && phys_cursor_on_p && !w->phys_cursor_on_p)
26431 {
26432 int hpos = w->phys_cursor.hpos;
26433
26434 /* When the window is hscrolled, cursor hpos can legitimately be
26435 out of bounds, but we draw the cursor at the corresponding
26436 window margin in that case. */
26437 if (!row->reversed_p && hpos < 0)
26438 hpos = 0;
26439 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26440 hpos = row->used[TEXT_AREA] - 1;
26441
26442 block_input ();
26443 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26444 w->phys_cursor.x, w->phys_cursor.y);
26445 unblock_input ();
26446 }
26447 #endif /* HAVE_WINDOW_SYSTEM */
26448 }
26449
26450 #ifdef HAVE_WINDOW_SYSTEM
26451 /* Change the mouse cursor. */
26452 if (FRAME_WINDOW_P (f))
26453 {
26454 if (draw == DRAW_NORMAL_TEXT
26455 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26456 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26457 else if (draw == DRAW_MOUSE_FACE)
26458 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26459 else
26460 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26461 }
26462 #endif /* HAVE_WINDOW_SYSTEM */
26463 }
26464
26465 /* EXPORT:
26466 Clear out the mouse-highlighted active region.
26467 Redraw it un-highlighted first. Value is non-zero if mouse
26468 face was actually drawn unhighlighted. */
26469
26470 int
26471 clear_mouse_face (Mouse_HLInfo *hlinfo)
26472 {
26473 int cleared = 0;
26474
26475 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26476 {
26477 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26478 cleared = 1;
26479 }
26480
26481 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26482 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26483 hlinfo->mouse_face_window = Qnil;
26484 hlinfo->mouse_face_overlay = Qnil;
26485 return cleared;
26486 }
26487
26488 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26489 within the mouse face on that window. */
26490 static int
26491 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26492 {
26493 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26494
26495 /* Quickly resolve the easy cases. */
26496 if (!(WINDOWP (hlinfo->mouse_face_window)
26497 && XWINDOW (hlinfo->mouse_face_window) == w))
26498 return 0;
26499 if (vpos < hlinfo->mouse_face_beg_row
26500 || vpos > hlinfo->mouse_face_end_row)
26501 return 0;
26502 if (vpos > hlinfo->mouse_face_beg_row
26503 && vpos < hlinfo->mouse_face_end_row)
26504 return 1;
26505
26506 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26507 {
26508 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26509 {
26510 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26511 return 1;
26512 }
26513 else if ((vpos == hlinfo->mouse_face_beg_row
26514 && hpos >= hlinfo->mouse_face_beg_col)
26515 || (vpos == hlinfo->mouse_face_end_row
26516 && hpos < hlinfo->mouse_face_end_col))
26517 return 1;
26518 }
26519 else
26520 {
26521 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26522 {
26523 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26524 return 1;
26525 }
26526 else if ((vpos == hlinfo->mouse_face_beg_row
26527 && hpos <= hlinfo->mouse_face_beg_col)
26528 || (vpos == hlinfo->mouse_face_end_row
26529 && hpos > hlinfo->mouse_face_end_col))
26530 return 1;
26531 }
26532 return 0;
26533 }
26534
26535
26536 /* EXPORT:
26537 Non-zero if physical cursor of window W is within mouse face. */
26538
26539 int
26540 cursor_in_mouse_face_p (struct window *w)
26541 {
26542 int hpos = w->phys_cursor.hpos;
26543 int vpos = w->phys_cursor.vpos;
26544 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26545
26546 /* When the window is hscrolled, cursor hpos can legitimately be out
26547 of bounds, but we draw the cursor at the corresponding window
26548 margin in that case. */
26549 if (!row->reversed_p && hpos < 0)
26550 hpos = 0;
26551 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26552 hpos = row->used[TEXT_AREA] - 1;
26553
26554 return coords_in_mouse_face_p (w, hpos, vpos);
26555 }
26556
26557
26558 \f
26559 /* Find the glyph rows START_ROW and END_ROW of window W that display
26560 characters between buffer positions START_CHARPOS and END_CHARPOS
26561 (excluding END_CHARPOS). DISP_STRING is a display string that
26562 covers these buffer positions. This is similar to
26563 row_containing_pos, but is more accurate when bidi reordering makes
26564 buffer positions change non-linearly with glyph rows. */
26565 static void
26566 rows_from_pos_range (struct window *w,
26567 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26568 Lisp_Object disp_string,
26569 struct glyph_row **start, struct glyph_row **end)
26570 {
26571 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26572 int last_y = window_text_bottom_y (w);
26573 struct glyph_row *row;
26574
26575 *start = NULL;
26576 *end = NULL;
26577
26578 while (!first->enabled_p
26579 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26580 first++;
26581
26582 /* Find the START row. */
26583 for (row = first;
26584 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26585 row++)
26586 {
26587 /* A row can potentially be the START row if the range of the
26588 characters it displays intersects the range
26589 [START_CHARPOS..END_CHARPOS). */
26590 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26591 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26592 /* See the commentary in row_containing_pos, for the
26593 explanation of the complicated way to check whether
26594 some position is beyond the end of the characters
26595 displayed by a row. */
26596 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26597 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26598 && !row->ends_at_zv_p
26599 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26600 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26601 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26602 && !row->ends_at_zv_p
26603 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26604 {
26605 /* Found a candidate row. Now make sure at least one of the
26606 glyphs it displays has a charpos from the range
26607 [START_CHARPOS..END_CHARPOS).
26608
26609 This is not obvious because bidi reordering could make
26610 buffer positions of a row be 1,2,3,102,101,100, and if we
26611 want to highlight characters in [50..60), we don't want
26612 this row, even though [50..60) does intersect [1..103),
26613 the range of character positions given by the row's start
26614 and end positions. */
26615 struct glyph *g = row->glyphs[TEXT_AREA];
26616 struct glyph *e = g + row->used[TEXT_AREA];
26617
26618 while (g < e)
26619 {
26620 if (((BUFFERP (g->object) || INTEGERP (g->object))
26621 && start_charpos <= g->charpos && g->charpos < end_charpos)
26622 /* A glyph that comes from DISP_STRING is by
26623 definition to be highlighted. */
26624 || EQ (g->object, disp_string))
26625 *start = row;
26626 g++;
26627 }
26628 if (*start)
26629 break;
26630 }
26631 }
26632
26633 /* Find the END row. */
26634 if (!*start
26635 /* If the last row is partially visible, start looking for END
26636 from that row, instead of starting from FIRST. */
26637 && !(row->enabled_p
26638 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26639 row = first;
26640 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26641 {
26642 struct glyph_row *next = row + 1;
26643 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26644
26645 if (!next->enabled_p
26646 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26647 /* The first row >= START whose range of displayed characters
26648 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26649 is the row END + 1. */
26650 || (start_charpos < next_start
26651 && end_charpos < next_start)
26652 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26653 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26654 && !next->ends_at_zv_p
26655 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26656 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26657 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26658 && !next->ends_at_zv_p
26659 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26660 {
26661 *end = row;
26662 break;
26663 }
26664 else
26665 {
26666 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26667 but none of the characters it displays are in the range, it is
26668 also END + 1. */
26669 struct glyph *g = next->glyphs[TEXT_AREA];
26670 struct glyph *s = g;
26671 struct glyph *e = g + next->used[TEXT_AREA];
26672
26673 while (g < e)
26674 {
26675 if (((BUFFERP (g->object) || INTEGERP (g->object))
26676 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26677 /* If the buffer position of the first glyph in
26678 the row is equal to END_CHARPOS, it means
26679 the last character to be highlighted is the
26680 newline of ROW, and we must consider NEXT as
26681 END, not END+1. */
26682 || (((!next->reversed_p && g == s)
26683 || (next->reversed_p && g == e - 1))
26684 && (g->charpos == end_charpos
26685 /* Special case for when NEXT is an
26686 empty line at ZV. */
26687 || (g->charpos == -1
26688 && !row->ends_at_zv_p
26689 && next_start == end_charpos)))))
26690 /* A glyph that comes from DISP_STRING is by
26691 definition to be highlighted. */
26692 || EQ (g->object, disp_string))
26693 break;
26694 g++;
26695 }
26696 if (g == e)
26697 {
26698 *end = row;
26699 break;
26700 }
26701 /* The first row that ends at ZV must be the last to be
26702 highlighted. */
26703 else if (next->ends_at_zv_p)
26704 {
26705 *end = next;
26706 break;
26707 }
26708 }
26709 }
26710 }
26711
26712 /* This function sets the mouse_face_* elements of HLINFO, assuming
26713 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26714 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26715 for the overlay or run of text properties specifying the mouse
26716 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26717 before-string and after-string that must also be highlighted.
26718 DISP_STRING, if non-nil, is a display string that may cover some
26719 or all of the highlighted text. */
26720
26721 static void
26722 mouse_face_from_buffer_pos (Lisp_Object window,
26723 Mouse_HLInfo *hlinfo,
26724 ptrdiff_t mouse_charpos,
26725 ptrdiff_t start_charpos,
26726 ptrdiff_t end_charpos,
26727 Lisp_Object before_string,
26728 Lisp_Object after_string,
26729 Lisp_Object disp_string)
26730 {
26731 struct window *w = XWINDOW (window);
26732 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26733 struct glyph_row *r1, *r2;
26734 struct glyph *glyph, *end;
26735 ptrdiff_t ignore, pos;
26736 int x;
26737
26738 eassert (NILP (disp_string) || STRINGP (disp_string));
26739 eassert (NILP (before_string) || STRINGP (before_string));
26740 eassert (NILP (after_string) || STRINGP (after_string));
26741
26742 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26743 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26744 if (r1 == NULL)
26745 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26746 /* If the before-string or display-string contains newlines,
26747 rows_from_pos_range skips to its last row. Move back. */
26748 if (!NILP (before_string) || !NILP (disp_string))
26749 {
26750 struct glyph_row *prev;
26751 while ((prev = r1 - 1, prev >= first)
26752 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26753 && prev->used[TEXT_AREA] > 0)
26754 {
26755 struct glyph *beg = prev->glyphs[TEXT_AREA];
26756 glyph = beg + prev->used[TEXT_AREA];
26757 while (--glyph >= beg && INTEGERP (glyph->object));
26758 if (glyph < beg
26759 || !(EQ (glyph->object, before_string)
26760 || EQ (glyph->object, disp_string)))
26761 break;
26762 r1 = prev;
26763 }
26764 }
26765 if (r2 == NULL)
26766 {
26767 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26768 hlinfo->mouse_face_past_end = 1;
26769 }
26770 else if (!NILP (after_string))
26771 {
26772 /* If the after-string has newlines, advance to its last row. */
26773 struct glyph_row *next;
26774 struct glyph_row *last
26775 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26776
26777 for (next = r2 + 1;
26778 next <= last
26779 && next->used[TEXT_AREA] > 0
26780 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26781 ++next)
26782 r2 = next;
26783 }
26784 /* The rest of the display engine assumes that mouse_face_beg_row is
26785 either above mouse_face_end_row or identical to it. But with
26786 bidi-reordered continued lines, the row for START_CHARPOS could
26787 be below the row for END_CHARPOS. If so, swap the rows and store
26788 them in correct order. */
26789 if (r1->y > r2->y)
26790 {
26791 struct glyph_row *tem = r2;
26792
26793 r2 = r1;
26794 r1 = tem;
26795 }
26796
26797 hlinfo->mouse_face_beg_y = r1->y;
26798 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26799 hlinfo->mouse_face_end_y = r2->y;
26800 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26801
26802 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26803 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26804 could be anywhere in the row and in any order. The strategy
26805 below is to find the leftmost and the rightmost glyph that
26806 belongs to either of these 3 strings, or whose position is
26807 between START_CHARPOS and END_CHARPOS, and highlight all the
26808 glyphs between those two. This may cover more than just the text
26809 between START_CHARPOS and END_CHARPOS if the range of characters
26810 strides the bidi level boundary, e.g. if the beginning is in R2L
26811 text while the end is in L2R text or vice versa. */
26812 if (!r1->reversed_p)
26813 {
26814 /* This row is in a left to right paragraph. Scan it left to
26815 right. */
26816 glyph = r1->glyphs[TEXT_AREA];
26817 end = glyph + r1->used[TEXT_AREA];
26818 x = r1->x;
26819
26820 /* Skip truncation glyphs at the start of the glyph row. */
26821 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26822 for (; glyph < end
26823 && INTEGERP (glyph->object)
26824 && glyph->charpos < 0;
26825 ++glyph)
26826 x += glyph->pixel_width;
26827
26828 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26829 or DISP_STRING, and the first glyph from buffer whose
26830 position is between START_CHARPOS and END_CHARPOS. */
26831 for (; glyph < end
26832 && !INTEGERP (glyph->object)
26833 && !EQ (glyph->object, disp_string)
26834 && !(BUFFERP (glyph->object)
26835 && (glyph->charpos >= start_charpos
26836 && glyph->charpos < end_charpos));
26837 ++glyph)
26838 {
26839 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26840 are present at buffer positions between START_CHARPOS and
26841 END_CHARPOS, or if they come from an overlay. */
26842 if (EQ (glyph->object, before_string))
26843 {
26844 pos = string_buffer_position (before_string,
26845 start_charpos);
26846 /* If pos == 0, it means before_string came from an
26847 overlay, not from a buffer position. */
26848 if (!pos || (pos >= start_charpos && pos < end_charpos))
26849 break;
26850 }
26851 else if (EQ (glyph->object, after_string))
26852 {
26853 pos = string_buffer_position (after_string, end_charpos);
26854 if (!pos || (pos >= start_charpos && pos < end_charpos))
26855 break;
26856 }
26857 x += glyph->pixel_width;
26858 }
26859 hlinfo->mouse_face_beg_x = x;
26860 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26861 }
26862 else
26863 {
26864 /* This row is in a right to left paragraph. Scan it right to
26865 left. */
26866 struct glyph *g;
26867
26868 end = r1->glyphs[TEXT_AREA] - 1;
26869 glyph = end + r1->used[TEXT_AREA];
26870
26871 /* Skip truncation glyphs at the start of the glyph row. */
26872 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26873 for (; glyph > end
26874 && INTEGERP (glyph->object)
26875 && glyph->charpos < 0;
26876 --glyph)
26877 ;
26878
26879 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26880 or DISP_STRING, and the first glyph from buffer whose
26881 position is between START_CHARPOS and END_CHARPOS. */
26882 for (; glyph > end
26883 && !INTEGERP (glyph->object)
26884 && !EQ (glyph->object, disp_string)
26885 && !(BUFFERP (glyph->object)
26886 && (glyph->charpos >= start_charpos
26887 && glyph->charpos < end_charpos));
26888 --glyph)
26889 {
26890 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26891 are present at buffer positions between START_CHARPOS and
26892 END_CHARPOS, or if they come from an overlay. */
26893 if (EQ (glyph->object, before_string))
26894 {
26895 pos = string_buffer_position (before_string, start_charpos);
26896 /* If pos == 0, it means before_string came from an
26897 overlay, not from a buffer position. */
26898 if (!pos || (pos >= start_charpos && pos < end_charpos))
26899 break;
26900 }
26901 else if (EQ (glyph->object, after_string))
26902 {
26903 pos = string_buffer_position (after_string, end_charpos);
26904 if (!pos || (pos >= start_charpos && pos < end_charpos))
26905 break;
26906 }
26907 }
26908
26909 glyph++; /* first glyph to the right of the highlighted area */
26910 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26911 x += g->pixel_width;
26912 hlinfo->mouse_face_beg_x = x;
26913 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26914 }
26915
26916 /* If the highlight ends in a different row, compute GLYPH and END
26917 for the end row. Otherwise, reuse the values computed above for
26918 the row where the highlight begins. */
26919 if (r2 != r1)
26920 {
26921 if (!r2->reversed_p)
26922 {
26923 glyph = r2->glyphs[TEXT_AREA];
26924 end = glyph + r2->used[TEXT_AREA];
26925 x = r2->x;
26926 }
26927 else
26928 {
26929 end = r2->glyphs[TEXT_AREA] - 1;
26930 glyph = end + r2->used[TEXT_AREA];
26931 }
26932 }
26933
26934 if (!r2->reversed_p)
26935 {
26936 /* Skip truncation and continuation glyphs near the end of the
26937 row, and also blanks and stretch glyphs inserted by
26938 extend_face_to_end_of_line. */
26939 while (end > glyph
26940 && INTEGERP ((end - 1)->object))
26941 --end;
26942 /* Scan the rest of the glyph row from the end, looking for the
26943 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26944 DISP_STRING, or whose position is between START_CHARPOS
26945 and END_CHARPOS */
26946 for (--end;
26947 end > glyph
26948 && !INTEGERP (end->object)
26949 && !EQ (end->object, disp_string)
26950 && !(BUFFERP (end->object)
26951 && (end->charpos >= start_charpos
26952 && end->charpos < end_charpos));
26953 --end)
26954 {
26955 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26956 are present at buffer positions between START_CHARPOS and
26957 END_CHARPOS, or if they come from an overlay. */
26958 if (EQ (end->object, before_string))
26959 {
26960 pos = string_buffer_position (before_string, start_charpos);
26961 if (!pos || (pos >= start_charpos && pos < end_charpos))
26962 break;
26963 }
26964 else if (EQ (end->object, after_string))
26965 {
26966 pos = string_buffer_position (after_string, end_charpos);
26967 if (!pos || (pos >= start_charpos && pos < end_charpos))
26968 break;
26969 }
26970 }
26971 /* Find the X coordinate of the last glyph to be highlighted. */
26972 for (; glyph <= end; ++glyph)
26973 x += glyph->pixel_width;
26974
26975 hlinfo->mouse_face_end_x = x;
26976 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26977 }
26978 else
26979 {
26980 /* Skip truncation and continuation glyphs near the end of the
26981 row, and also blanks and stretch glyphs inserted by
26982 extend_face_to_end_of_line. */
26983 x = r2->x;
26984 end++;
26985 while (end < glyph
26986 && INTEGERP (end->object))
26987 {
26988 x += end->pixel_width;
26989 ++end;
26990 }
26991 /* Scan the rest of the glyph row from the end, looking for the
26992 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26993 DISP_STRING, or whose position is between START_CHARPOS
26994 and END_CHARPOS */
26995 for ( ;
26996 end < glyph
26997 && !INTEGERP (end->object)
26998 && !EQ (end->object, disp_string)
26999 && !(BUFFERP (end->object)
27000 && (end->charpos >= start_charpos
27001 && end->charpos < end_charpos));
27002 ++end)
27003 {
27004 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27005 are present at buffer positions between START_CHARPOS and
27006 END_CHARPOS, or if they come from an overlay. */
27007 if (EQ (end->object, before_string))
27008 {
27009 pos = string_buffer_position (before_string, start_charpos);
27010 if (!pos || (pos >= start_charpos && pos < end_charpos))
27011 break;
27012 }
27013 else if (EQ (end->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 += end->pixel_width;
27020 }
27021 /* If we exited the above loop because we arrived at the last
27022 glyph of the row, and its buffer position is still not in
27023 range, it means the last character in range is the preceding
27024 newline. Bump the end column and x values to get past the
27025 last glyph. */
27026 if (end == glyph
27027 && BUFFERP (end->object)
27028 && (end->charpos < start_charpos
27029 || end->charpos >= end_charpos))
27030 {
27031 x += end->pixel_width;
27032 ++end;
27033 }
27034 hlinfo->mouse_face_end_x = x;
27035 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27036 }
27037
27038 hlinfo->mouse_face_window = window;
27039 hlinfo->mouse_face_face_id
27040 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27041 mouse_charpos + 1,
27042 !hlinfo->mouse_face_hidden, -1);
27043 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27044 }
27045
27046 /* The following function is not used anymore (replaced with
27047 mouse_face_from_string_pos), but I leave it here for the time
27048 being, in case someone would. */
27049
27050 #if 0 /* not used */
27051
27052 /* Find the position of the glyph for position POS in OBJECT in
27053 window W's current matrix, and return in *X, *Y the pixel
27054 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27055
27056 RIGHT_P non-zero means return the position of the right edge of the
27057 glyph, RIGHT_P zero means return the left edge position.
27058
27059 If no glyph for POS exists in the matrix, return the position of
27060 the glyph with the next smaller position that is in the matrix, if
27061 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27062 exists in the matrix, return the position of the glyph with the
27063 next larger position in OBJECT.
27064
27065 Value is non-zero if a glyph was found. */
27066
27067 static int
27068 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27069 int *hpos, int *vpos, int *x, int *y, int right_p)
27070 {
27071 int yb = window_text_bottom_y (w);
27072 struct glyph_row *r;
27073 struct glyph *best_glyph = NULL;
27074 struct glyph_row *best_row = NULL;
27075 int best_x = 0;
27076
27077 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27078 r->enabled_p && r->y < yb;
27079 ++r)
27080 {
27081 struct glyph *g = r->glyphs[TEXT_AREA];
27082 struct glyph *e = g + r->used[TEXT_AREA];
27083 int gx;
27084
27085 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27086 if (EQ (g->object, object))
27087 {
27088 if (g->charpos == pos)
27089 {
27090 best_glyph = g;
27091 best_x = gx;
27092 best_row = r;
27093 goto found;
27094 }
27095 else if (best_glyph == NULL
27096 || ((eabs (g->charpos - pos)
27097 < eabs (best_glyph->charpos - pos))
27098 && (right_p
27099 ? g->charpos < pos
27100 : g->charpos > pos)))
27101 {
27102 best_glyph = g;
27103 best_x = gx;
27104 best_row = r;
27105 }
27106 }
27107 }
27108
27109 found:
27110
27111 if (best_glyph)
27112 {
27113 *x = best_x;
27114 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27115
27116 if (right_p)
27117 {
27118 *x += best_glyph->pixel_width;
27119 ++*hpos;
27120 }
27121
27122 *y = best_row->y;
27123 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27124 }
27125
27126 return best_glyph != NULL;
27127 }
27128 #endif /* not used */
27129
27130 /* Find the positions of the first and the last glyphs in window W's
27131 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27132 (assumed to be a string), and return in HLINFO's mouse_face_*
27133 members the pixel and column/row coordinates of those glyphs. */
27134
27135 static void
27136 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27137 Lisp_Object object,
27138 ptrdiff_t startpos, ptrdiff_t endpos)
27139 {
27140 int yb = window_text_bottom_y (w);
27141 struct glyph_row *r;
27142 struct glyph *g, *e;
27143 int gx;
27144 int found = 0;
27145
27146 /* Find the glyph row with at least one position in the range
27147 [STARTPOS..ENDPOS], and the first glyph in that row whose
27148 position belongs to that range. */
27149 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27150 r->enabled_p && r->y < yb;
27151 ++r)
27152 {
27153 if (!r->reversed_p)
27154 {
27155 g = r->glyphs[TEXT_AREA];
27156 e = g + r->used[TEXT_AREA];
27157 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27158 if (EQ (g->object, object)
27159 && startpos <= g->charpos && g->charpos <= endpos)
27160 {
27161 hlinfo->mouse_face_beg_row
27162 = MATRIX_ROW_VPOS (r, w->current_matrix);
27163 hlinfo->mouse_face_beg_y = r->y;
27164 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27165 hlinfo->mouse_face_beg_x = gx;
27166 found = 1;
27167 break;
27168 }
27169 }
27170 else
27171 {
27172 struct glyph *g1;
27173
27174 e = r->glyphs[TEXT_AREA];
27175 g = e + r->used[TEXT_AREA];
27176 for ( ; g > e; --g)
27177 if (EQ ((g-1)->object, object)
27178 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27179 {
27180 hlinfo->mouse_face_beg_row
27181 = MATRIX_ROW_VPOS (r, w->current_matrix);
27182 hlinfo->mouse_face_beg_y = r->y;
27183 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27184 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27185 gx += g1->pixel_width;
27186 hlinfo->mouse_face_beg_x = gx;
27187 found = 1;
27188 break;
27189 }
27190 }
27191 if (found)
27192 break;
27193 }
27194
27195 if (!found)
27196 return;
27197
27198 /* Starting with the next row, look for the first row which does NOT
27199 include any glyphs whose positions are in the range. */
27200 for (++r; r->enabled_p && r->y < yb; ++r)
27201 {
27202 g = r->glyphs[TEXT_AREA];
27203 e = g + r->used[TEXT_AREA];
27204 found = 0;
27205 for ( ; g < e; ++g)
27206 if (EQ (g->object, object)
27207 && startpos <= g->charpos && g->charpos <= endpos)
27208 {
27209 found = 1;
27210 break;
27211 }
27212 if (!found)
27213 break;
27214 }
27215
27216 /* The highlighted region ends on the previous row. */
27217 r--;
27218
27219 /* Set the end row and its vertical pixel coordinate. */
27220 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27221 hlinfo->mouse_face_end_y = r->y;
27222
27223 /* Compute and set the end column and the end column's horizontal
27224 pixel coordinate. */
27225 if (!r->reversed_p)
27226 {
27227 g = r->glyphs[TEXT_AREA];
27228 e = g + r->used[TEXT_AREA];
27229 for ( ; e > g; --e)
27230 if (EQ ((e-1)->object, object)
27231 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27232 break;
27233 hlinfo->mouse_face_end_col = e - g;
27234
27235 for (gx = r->x; g < e; ++g)
27236 gx += g->pixel_width;
27237 hlinfo->mouse_face_end_x = gx;
27238 }
27239 else
27240 {
27241 e = r->glyphs[TEXT_AREA];
27242 g = e + r->used[TEXT_AREA];
27243 for (gx = r->x ; e < g; ++e)
27244 {
27245 if (EQ (e->object, object)
27246 && startpos <= e->charpos && e->charpos <= endpos)
27247 break;
27248 gx += e->pixel_width;
27249 }
27250 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27251 hlinfo->mouse_face_end_x = gx;
27252 }
27253 }
27254
27255 #ifdef HAVE_WINDOW_SYSTEM
27256
27257 /* See if position X, Y is within a hot-spot of an image. */
27258
27259 static int
27260 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27261 {
27262 if (!CONSP (hot_spot))
27263 return 0;
27264
27265 if (EQ (XCAR (hot_spot), Qrect))
27266 {
27267 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27268 Lisp_Object rect = XCDR (hot_spot);
27269 Lisp_Object tem;
27270 if (!CONSP (rect))
27271 return 0;
27272 if (!CONSP (XCAR (rect)))
27273 return 0;
27274 if (!CONSP (XCDR (rect)))
27275 return 0;
27276 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27277 return 0;
27278 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27279 return 0;
27280 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27281 return 0;
27282 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27283 return 0;
27284 return 1;
27285 }
27286 else if (EQ (XCAR (hot_spot), Qcircle))
27287 {
27288 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27289 Lisp_Object circ = XCDR (hot_spot);
27290 Lisp_Object lr, lx0, ly0;
27291 if (CONSP (circ)
27292 && CONSP (XCAR (circ))
27293 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27294 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27295 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27296 {
27297 double r = XFLOATINT (lr);
27298 double dx = XINT (lx0) - x;
27299 double dy = XINT (ly0) - y;
27300 return (dx * dx + dy * dy <= r * r);
27301 }
27302 }
27303 else if (EQ (XCAR (hot_spot), Qpoly))
27304 {
27305 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27306 if (VECTORP (XCDR (hot_spot)))
27307 {
27308 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27309 Lisp_Object *poly = v->contents;
27310 ptrdiff_t n = v->header.size;
27311 ptrdiff_t i;
27312 int inside = 0;
27313 Lisp_Object lx, ly;
27314 int x0, y0;
27315
27316 /* Need an even number of coordinates, and at least 3 edges. */
27317 if (n < 6 || n & 1)
27318 return 0;
27319
27320 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27321 If count is odd, we are inside polygon. Pixels on edges
27322 may or may not be included depending on actual geometry of the
27323 polygon. */
27324 if ((lx = poly[n-2], !INTEGERP (lx))
27325 || (ly = poly[n-1], !INTEGERP (lx)))
27326 return 0;
27327 x0 = XINT (lx), y0 = XINT (ly);
27328 for (i = 0; i < n; i += 2)
27329 {
27330 int x1 = x0, y1 = y0;
27331 if ((lx = poly[i], !INTEGERP (lx))
27332 || (ly = poly[i+1], !INTEGERP (ly)))
27333 return 0;
27334 x0 = XINT (lx), y0 = XINT (ly);
27335
27336 /* Does this segment cross the X line? */
27337 if (x0 >= x)
27338 {
27339 if (x1 >= x)
27340 continue;
27341 }
27342 else if (x1 < x)
27343 continue;
27344 if (y > y0 && y > y1)
27345 continue;
27346 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27347 inside = !inside;
27348 }
27349 return inside;
27350 }
27351 }
27352 return 0;
27353 }
27354
27355 Lisp_Object
27356 find_hot_spot (Lisp_Object map, int x, int y)
27357 {
27358 while (CONSP (map))
27359 {
27360 if (CONSP (XCAR (map))
27361 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27362 return XCAR (map);
27363 map = XCDR (map);
27364 }
27365
27366 return Qnil;
27367 }
27368
27369 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27370 3, 3, 0,
27371 doc: /* Lookup in image map MAP coordinates X and Y.
27372 An image map is an alist where each element has the format (AREA ID PLIST).
27373 An AREA is specified as either a rectangle, a circle, or a polygon:
27374 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27375 pixel coordinates of the upper left and bottom right corners.
27376 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27377 and the radius of the circle; r may be a float or integer.
27378 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27379 vector describes one corner in the polygon.
27380 Returns the alist element for the first matching AREA in MAP. */)
27381 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27382 {
27383 if (NILP (map))
27384 return Qnil;
27385
27386 CHECK_NUMBER (x);
27387 CHECK_NUMBER (y);
27388
27389 return find_hot_spot (map,
27390 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27391 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27392 }
27393
27394
27395 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27396 static void
27397 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27398 {
27399 /* Do not change cursor shape while dragging mouse. */
27400 if (!NILP (do_mouse_tracking))
27401 return;
27402
27403 if (!NILP (pointer))
27404 {
27405 if (EQ (pointer, Qarrow))
27406 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27407 else if (EQ (pointer, Qhand))
27408 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27409 else if (EQ (pointer, Qtext))
27410 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27411 else if (EQ (pointer, intern ("hdrag")))
27412 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27413 #ifdef HAVE_X_WINDOWS
27414 else if (EQ (pointer, intern ("vdrag")))
27415 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27416 #endif
27417 else if (EQ (pointer, intern ("hourglass")))
27418 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27419 else if (EQ (pointer, Qmodeline))
27420 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27421 else
27422 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27423 }
27424
27425 if (cursor != No_Cursor)
27426 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27427 }
27428
27429 #endif /* HAVE_WINDOW_SYSTEM */
27430
27431 /* Take proper action when mouse has moved to the mode or header line
27432 or marginal area AREA of window W, x-position X and y-position Y.
27433 X is relative to the start of the text display area of W, so the
27434 width of bitmap areas and scroll bars must be subtracted to get a
27435 position relative to the start of the mode line. */
27436
27437 static void
27438 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27439 enum window_part area)
27440 {
27441 struct window *w = XWINDOW (window);
27442 struct frame *f = XFRAME (w->frame);
27443 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27444 #ifdef HAVE_WINDOW_SYSTEM
27445 Display_Info *dpyinfo;
27446 #endif
27447 Cursor cursor = No_Cursor;
27448 Lisp_Object pointer = Qnil;
27449 int dx, dy, width, height;
27450 ptrdiff_t charpos;
27451 Lisp_Object string, object = Qnil;
27452 Lisp_Object pos IF_LINT (= Qnil), help;
27453
27454 Lisp_Object mouse_face;
27455 int original_x_pixel = x;
27456 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27457 struct glyph_row *row IF_LINT (= 0);
27458
27459 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27460 {
27461 int x0;
27462 struct glyph *end;
27463
27464 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27465 returns them in row/column units! */
27466 string = mode_line_string (w, area, &x, &y, &charpos,
27467 &object, &dx, &dy, &width, &height);
27468
27469 row = (area == ON_MODE_LINE
27470 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27471 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27472
27473 /* Find the glyph under the mouse pointer. */
27474 if (row->mode_line_p && row->enabled_p)
27475 {
27476 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27477 end = glyph + row->used[TEXT_AREA];
27478
27479 for (x0 = original_x_pixel;
27480 glyph < end && x0 >= glyph->pixel_width;
27481 ++glyph)
27482 x0 -= glyph->pixel_width;
27483
27484 if (glyph >= end)
27485 glyph = NULL;
27486 }
27487 }
27488 else
27489 {
27490 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27491 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27492 returns them in row/column units! */
27493 string = marginal_area_string (w, area, &x, &y, &charpos,
27494 &object, &dx, &dy, &width, &height);
27495 }
27496
27497 help = Qnil;
27498
27499 #ifdef HAVE_WINDOW_SYSTEM
27500 if (IMAGEP (object))
27501 {
27502 Lisp_Object image_map, hotspot;
27503 if ((image_map = Fplist_get (XCDR (object), QCmap),
27504 !NILP (image_map))
27505 && (hotspot = find_hot_spot (image_map, dx, dy),
27506 CONSP (hotspot))
27507 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27508 {
27509 Lisp_Object plist;
27510
27511 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27512 If so, we could look for mouse-enter, mouse-leave
27513 properties in PLIST (and do something...). */
27514 hotspot = XCDR (hotspot);
27515 if (CONSP (hotspot)
27516 && (plist = XCAR (hotspot), CONSP (plist)))
27517 {
27518 pointer = Fplist_get (plist, Qpointer);
27519 if (NILP (pointer))
27520 pointer = Qhand;
27521 help = Fplist_get (plist, Qhelp_echo);
27522 if (!NILP (help))
27523 {
27524 help_echo_string = help;
27525 XSETWINDOW (help_echo_window, w);
27526 help_echo_object = w->contents;
27527 help_echo_pos = charpos;
27528 }
27529 }
27530 }
27531 if (NILP (pointer))
27532 pointer = Fplist_get (XCDR (object), QCpointer);
27533 }
27534 #endif /* HAVE_WINDOW_SYSTEM */
27535
27536 if (STRINGP (string))
27537 pos = make_number (charpos);
27538
27539 /* Set the help text and mouse pointer. If the mouse is on a part
27540 of the mode line without any text (e.g. past the right edge of
27541 the mode line text), use the default help text and pointer. */
27542 if (STRINGP (string) || area == ON_MODE_LINE)
27543 {
27544 /* Arrange to display the help by setting the global variables
27545 help_echo_string, help_echo_object, and help_echo_pos. */
27546 if (NILP (help))
27547 {
27548 if (STRINGP (string))
27549 help = Fget_text_property (pos, Qhelp_echo, string);
27550
27551 if (!NILP (help))
27552 {
27553 help_echo_string = help;
27554 XSETWINDOW (help_echo_window, w);
27555 help_echo_object = string;
27556 help_echo_pos = charpos;
27557 }
27558 else if (area == ON_MODE_LINE)
27559 {
27560 Lisp_Object default_help
27561 = buffer_local_value_1 (Qmode_line_default_help_echo,
27562 w->contents);
27563
27564 if (STRINGP (default_help))
27565 {
27566 help_echo_string = default_help;
27567 XSETWINDOW (help_echo_window, w);
27568 help_echo_object = Qnil;
27569 help_echo_pos = -1;
27570 }
27571 }
27572 }
27573
27574 #ifdef HAVE_WINDOW_SYSTEM
27575 /* Change the mouse pointer according to what is under it. */
27576 if (FRAME_WINDOW_P (f))
27577 {
27578 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27579 if (STRINGP (string))
27580 {
27581 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27582
27583 if (NILP (pointer))
27584 pointer = Fget_text_property (pos, Qpointer, string);
27585
27586 /* Change the mouse pointer according to what is under X/Y. */
27587 if (NILP (pointer)
27588 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27589 {
27590 Lisp_Object map;
27591 map = Fget_text_property (pos, Qlocal_map, string);
27592 if (!KEYMAPP (map))
27593 map = Fget_text_property (pos, Qkeymap, string);
27594 if (!KEYMAPP (map))
27595 cursor = dpyinfo->vertical_scroll_bar_cursor;
27596 }
27597 }
27598 else
27599 /* Default mode-line pointer. */
27600 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27601 }
27602 #endif
27603 }
27604
27605 /* Change the mouse face according to what is under X/Y. */
27606 if (STRINGP (string))
27607 {
27608 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27609 if (!NILP (mouse_face)
27610 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27611 && glyph)
27612 {
27613 Lisp_Object b, e;
27614
27615 struct glyph * tmp_glyph;
27616
27617 int gpos;
27618 int gseq_length;
27619 int total_pixel_width;
27620 ptrdiff_t begpos, endpos, ignore;
27621
27622 int vpos, hpos;
27623
27624 b = Fprevious_single_property_change (make_number (charpos + 1),
27625 Qmouse_face, string, Qnil);
27626 if (NILP (b))
27627 begpos = 0;
27628 else
27629 begpos = XINT (b);
27630
27631 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27632 if (NILP (e))
27633 endpos = SCHARS (string);
27634 else
27635 endpos = XINT (e);
27636
27637 /* Calculate the glyph position GPOS of GLYPH in the
27638 displayed string, relative to the beginning of the
27639 highlighted part of the string.
27640
27641 Note: GPOS is different from CHARPOS. CHARPOS is the
27642 position of GLYPH in the internal string object. A mode
27643 line string format has structures which are converted to
27644 a flattened string by the Emacs Lisp interpreter. The
27645 internal string is an element of those structures. The
27646 displayed string is the flattened string. */
27647 tmp_glyph = row_start_glyph;
27648 while (tmp_glyph < glyph
27649 && (!(EQ (tmp_glyph->object, glyph->object)
27650 && begpos <= tmp_glyph->charpos
27651 && tmp_glyph->charpos < endpos)))
27652 tmp_glyph++;
27653 gpos = glyph - tmp_glyph;
27654
27655 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27656 the highlighted part of the displayed string to which
27657 GLYPH belongs. Note: GSEQ_LENGTH is different from
27658 SCHARS (STRING), because the latter returns the length of
27659 the internal string. */
27660 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27661 tmp_glyph > glyph
27662 && (!(EQ (tmp_glyph->object, glyph->object)
27663 && begpos <= tmp_glyph->charpos
27664 && tmp_glyph->charpos < endpos));
27665 tmp_glyph--)
27666 ;
27667 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27668
27669 /* Calculate the total pixel width of all the glyphs between
27670 the beginning of the highlighted area and GLYPH. */
27671 total_pixel_width = 0;
27672 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27673 total_pixel_width += tmp_glyph->pixel_width;
27674
27675 /* Pre calculation of re-rendering position. Note: X is in
27676 column units here, after the call to mode_line_string or
27677 marginal_area_string. */
27678 hpos = x - gpos;
27679 vpos = (area == ON_MODE_LINE
27680 ? (w->current_matrix)->nrows - 1
27681 : 0);
27682
27683 /* If GLYPH's position is included in the region that is
27684 already drawn in mouse face, we have nothing to do. */
27685 if ( EQ (window, hlinfo->mouse_face_window)
27686 && (!row->reversed_p
27687 ? (hlinfo->mouse_face_beg_col <= hpos
27688 && hpos < hlinfo->mouse_face_end_col)
27689 /* In R2L rows we swap BEG and END, see below. */
27690 : (hlinfo->mouse_face_end_col <= hpos
27691 && hpos < hlinfo->mouse_face_beg_col))
27692 && hlinfo->mouse_face_beg_row == vpos )
27693 return;
27694
27695 if (clear_mouse_face (hlinfo))
27696 cursor = No_Cursor;
27697
27698 if (!row->reversed_p)
27699 {
27700 hlinfo->mouse_face_beg_col = hpos;
27701 hlinfo->mouse_face_beg_x = original_x_pixel
27702 - (total_pixel_width + dx);
27703 hlinfo->mouse_face_end_col = hpos + gseq_length;
27704 hlinfo->mouse_face_end_x = 0;
27705 }
27706 else
27707 {
27708 /* In R2L rows, show_mouse_face expects BEG and END
27709 coordinates to be swapped. */
27710 hlinfo->mouse_face_end_col = hpos;
27711 hlinfo->mouse_face_end_x = original_x_pixel
27712 - (total_pixel_width + dx);
27713 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27714 hlinfo->mouse_face_beg_x = 0;
27715 }
27716
27717 hlinfo->mouse_face_beg_row = vpos;
27718 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27719 hlinfo->mouse_face_beg_y = 0;
27720 hlinfo->mouse_face_end_y = 0;
27721 hlinfo->mouse_face_past_end = 0;
27722 hlinfo->mouse_face_window = window;
27723
27724 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27725 charpos,
27726 0, 0, 0,
27727 &ignore,
27728 glyph->face_id,
27729 1);
27730 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27731
27732 if (NILP (pointer))
27733 pointer = Qhand;
27734 }
27735 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27736 clear_mouse_face (hlinfo);
27737 }
27738 #ifdef HAVE_WINDOW_SYSTEM
27739 if (FRAME_WINDOW_P (f))
27740 define_frame_cursor1 (f, cursor, pointer);
27741 #endif
27742 }
27743
27744
27745 /* EXPORT:
27746 Take proper action when the mouse has moved to position X, Y on
27747 frame F as regards highlighting characters that have mouse-face
27748 properties. Also de-highlighting chars where the mouse was before.
27749 X and Y can be negative or out of range. */
27750
27751 void
27752 note_mouse_highlight (struct frame *f, int x, int y)
27753 {
27754 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27755 enum window_part part = ON_NOTHING;
27756 Lisp_Object window;
27757 struct window *w;
27758 Cursor cursor = No_Cursor;
27759 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27760 struct buffer *b;
27761
27762 /* When a menu is active, don't highlight because this looks odd. */
27763 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27764 if (popup_activated ())
27765 return;
27766 #endif
27767
27768 if (NILP (Vmouse_highlight)
27769 || !f->glyphs_initialized_p
27770 || f->pointer_invisible)
27771 return;
27772
27773 hlinfo->mouse_face_mouse_x = x;
27774 hlinfo->mouse_face_mouse_y = y;
27775 hlinfo->mouse_face_mouse_frame = f;
27776
27777 if (hlinfo->mouse_face_defer)
27778 return;
27779
27780 /* Which window is that in? */
27781 window = window_from_coordinates (f, x, y, &part, 1);
27782
27783 /* If displaying active text in another window, clear that. */
27784 if (! EQ (window, hlinfo->mouse_face_window)
27785 /* Also clear if we move out of text area in same window. */
27786 || (!NILP (hlinfo->mouse_face_window)
27787 && !NILP (window)
27788 && part != ON_TEXT
27789 && part != ON_MODE_LINE
27790 && part != ON_HEADER_LINE))
27791 clear_mouse_face (hlinfo);
27792
27793 /* Not on a window -> return. */
27794 if (!WINDOWP (window))
27795 return;
27796
27797 /* Reset help_echo_string. It will get recomputed below. */
27798 help_echo_string = Qnil;
27799
27800 /* Convert to window-relative pixel coordinates. */
27801 w = XWINDOW (window);
27802 frame_to_window_pixel_xy (w, &x, &y);
27803
27804 #ifdef HAVE_WINDOW_SYSTEM
27805 /* Handle tool-bar window differently since it doesn't display a
27806 buffer. */
27807 if (EQ (window, f->tool_bar_window))
27808 {
27809 note_tool_bar_highlight (f, x, y);
27810 return;
27811 }
27812 #endif
27813
27814 /* Mouse is on the mode, header line or margin? */
27815 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27816 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27817 {
27818 note_mode_line_or_margin_highlight (window, x, y, part);
27819 return;
27820 }
27821
27822 #ifdef HAVE_WINDOW_SYSTEM
27823 if (part == ON_VERTICAL_BORDER)
27824 {
27825 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27826 help_echo_string = build_string ("drag-mouse-1: resize");
27827 }
27828 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27829 || part == ON_SCROLL_BAR)
27830 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27831 else
27832 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27833 #endif
27834
27835 /* Are we in a window whose display is up to date?
27836 And verify the buffer's text has not changed. */
27837 b = XBUFFER (w->contents);
27838 if (part == ON_TEXT
27839 && w->window_end_valid
27840 && w->last_modified == BUF_MODIFF (b)
27841 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27842 {
27843 int hpos, vpos, dx, dy, area = LAST_AREA;
27844 ptrdiff_t pos;
27845 struct glyph *glyph;
27846 Lisp_Object object;
27847 Lisp_Object mouse_face = Qnil, position;
27848 Lisp_Object *overlay_vec = NULL;
27849 ptrdiff_t i, noverlays;
27850 struct buffer *obuf;
27851 ptrdiff_t obegv, ozv;
27852 int same_region;
27853
27854 /* Find the glyph under X/Y. */
27855 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27856
27857 #ifdef HAVE_WINDOW_SYSTEM
27858 /* Look for :pointer property on image. */
27859 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27860 {
27861 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27862 if (img != NULL && IMAGEP (img->spec))
27863 {
27864 Lisp_Object image_map, hotspot;
27865 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27866 !NILP (image_map))
27867 && (hotspot = find_hot_spot (image_map,
27868 glyph->slice.img.x + dx,
27869 glyph->slice.img.y + dy),
27870 CONSP (hotspot))
27871 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27872 {
27873 Lisp_Object plist;
27874
27875 /* Could check XCAR (hotspot) to see if we enter/leave
27876 this hot-spot.
27877 If so, we could look for mouse-enter, mouse-leave
27878 properties in PLIST (and do something...). */
27879 hotspot = XCDR (hotspot);
27880 if (CONSP (hotspot)
27881 && (plist = XCAR (hotspot), CONSP (plist)))
27882 {
27883 pointer = Fplist_get (plist, Qpointer);
27884 if (NILP (pointer))
27885 pointer = Qhand;
27886 help_echo_string = Fplist_get (plist, Qhelp_echo);
27887 if (!NILP (help_echo_string))
27888 {
27889 help_echo_window = window;
27890 help_echo_object = glyph->object;
27891 help_echo_pos = glyph->charpos;
27892 }
27893 }
27894 }
27895 if (NILP (pointer))
27896 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27897 }
27898 }
27899 #endif /* HAVE_WINDOW_SYSTEM */
27900
27901 /* Clear mouse face if X/Y not over text. */
27902 if (glyph == NULL
27903 || area != TEXT_AREA
27904 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
27905 /* Glyph's OBJECT is an integer for glyphs inserted by the
27906 display engine for its internal purposes, like truncation
27907 and continuation glyphs and blanks beyond the end of
27908 line's text on text terminals. If we are over such a
27909 glyph, we are not over any text. */
27910 || INTEGERP (glyph->object)
27911 /* R2L rows have a stretch glyph at their front, which
27912 stands for no text, whereas L2R rows have no glyphs at
27913 all beyond the end of text. Treat such stretch glyphs
27914 like we do with NULL glyphs in L2R rows. */
27915 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27916 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
27917 && glyph->type == STRETCH_GLYPH
27918 && glyph->avoid_cursor_p))
27919 {
27920 if (clear_mouse_face (hlinfo))
27921 cursor = No_Cursor;
27922 #ifdef HAVE_WINDOW_SYSTEM
27923 if (FRAME_WINDOW_P (f) && NILP (pointer))
27924 {
27925 if (area != TEXT_AREA)
27926 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27927 else
27928 pointer = Vvoid_text_area_pointer;
27929 }
27930 #endif
27931 goto set_cursor;
27932 }
27933
27934 pos = glyph->charpos;
27935 object = glyph->object;
27936 if (!STRINGP (object) && !BUFFERP (object))
27937 goto set_cursor;
27938
27939 /* If we get an out-of-range value, return now; avoid an error. */
27940 if (BUFFERP (object) && pos > BUF_Z (b))
27941 goto set_cursor;
27942
27943 /* Make the window's buffer temporarily current for
27944 overlays_at and compute_char_face. */
27945 obuf = current_buffer;
27946 current_buffer = b;
27947 obegv = BEGV;
27948 ozv = ZV;
27949 BEGV = BEG;
27950 ZV = Z;
27951
27952 /* Is this char mouse-active or does it have help-echo? */
27953 position = make_number (pos);
27954
27955 if (BUFFERP (object))
27956 {
27957 /* Put all the overlays we want in a vector in overlay_vec. */
27958 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27959 /* Sort overlays into increasing priority order. */
27960 noverlays = sort_overlays (overlay_vec, noverlays, w);
27961 }
27962 else
27963 noverlays = 0;
27964
27965 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27966
27967 if (same_region)
27968 cursor = No_Cursor;
27969
27970 /* Check mouse-face highlighting. */
27971 if (! same_region
27972 /* If there exists an overlay with mouse-face overlapping
27973 the one we are currently highlighting, we have to
27974 check if we enter the overlapping overlay, and then
27975 highlight only that. */
27976 || (OVERLAYP (hlinfo->mouse_face_overlay)
27977 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27978 {
27979 /* Find the highest priority overlay with a mouse-face. */
27980 Lisp_Object overlay = Qnil;
27981 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27982 {
27983 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27984 if (!NILP (mouse_face))
27985 overlay = overlay_vec[i];
27986 }
27987
27988 /* If we're highlighting the same overlay as before, there's
27989 no need to do that again. */
27990 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27991 goto check_help_echo;
27992 hlinfo->mouse_face_overlay = overlay;
27993
27994 /* Clear the display of the old active region, if any. */
27995 if (clear_mouse_face (hlinfo))
27996 cursor = No_Cursor;
27997
27998 /* If no overlay applies, get a text property. */
27999 if (NILP (overlay))
28000 mouse_face = Fget_text_property (position, Qmouse_face, object);
28001
28002 /* Next, compute the bounds of the mouse highlighting and
28003 display it. */
28004 if (!NILP (mouse_face) && STRINGP (object))
28005 {
28006 /* The mouse-highlighting comes from a display string
28007 with a mouse-face. */
28008 Lisp_Object s, e;
28009 ptrdiff_t ignore;
28010
28011 s = Fprevious_single_property_change
28012 (make_number (pos + 1), Qmouse_face, object, Qnil);
28013 e = Fnext_single_property_change
28014 (position, Qmouse_face, object, Qnil);
28015 if (NILP (s))
28016 s = make_number (0);
28017 if (NILP (e))
28018 e = make_number (SCHARS (object) - 1);
28019 mouse_face_from_string_pos (w, hlinfo, object,
28020 XINT (s), XINT (e));
28021 hlinfo->mouse_face_past_end = 0;
28022 hlinfo->mouse_face_window = window;
28023 hlinfo->mouse_face_face_id
28024 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28025 glyph->face_id, 1);
28026 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28027 cursor = No_Cursor;
28028 }
28029 else
28030 {
28031 /* The mouse-highlighting, if any, comes from an overlay
28032 or text property in the buffer. */
28033 Lisp_Object buffer IF_LINT (= Qnil);
28034 Lisp_Object disp_string IF_LINT (= Qnil);
28035
28036 if (STRINGP (object))
28037 {
28038 /* If we are on a display string with no mouse-face,
28039 check if the text under it has one. */
28040 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28041 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28042 pos = string_buffer_position (object, start);
28043 if (pos > 0)
28044 {
28045 mouse_face = get_char_property_and_overlay
28046 (make_number (pos), Qmouse_face, w->contents, &overlay);
28047 buffer = w->contents;
28048 disp_string = object;
28049 }
28050 }
28051 else
28052 {
28053 buffer = object;
28054 disp_string = Qnil;
28055 }
28056
28057 if (!NILP (mouse_face))
28058 {
28059 Lisp_Object before, after;
28060 Lisp_Object before_string, after_string;
28061 /* To correctly find the limits of mouse highlight
28062 in a bidi-reordered buffer, we must not use the
28063 optimization of limiting the search in
28064 previous-single-property-change and
28065 next-single-property-change, because
28066 rows_from_pos_range needs the real start and end
28067 positions to DTRT in this case. That's because
28068 the first row visible in a window does not
28069 necessarily display the character whose position
28070 is the smallest. */
28071 Lisp_Object lim1 =
28072 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28073 ? Fmarker_position (w->start)
28074 : Qnil;
28075 Lisp_Object lim2 =
28076 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28077 ? make_number (BUF_Z (XBUFFER (buffer))
28078 - XFASTINT (w->window_end_pos))
28079 : Qnil;
28080
28081 if (NILP (overlay))
28082 {
28083 /* Handle the text property case. */
28084 before = Fprevious_single_property_change
28085 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28086 after = Fnext_single_property_change
28087 (make_number (pos), Qmouse_face, buffer, lim2);
28088 before_string = after_string = Qnil;
28089 }
28090 else
28091 {
28092 /* Handle the overlay case. */
28093 before = Foverlay_start (overlay);
28094 after = Foverlay_end (overlay);
28095 before_string = Foverlay_get (overlay, Qbefore_string);
28096 after_string = Foverlay_get (overlay, Qafter_string);
28097
28098 if (!STRINGP (before_string)) before_string = Qnil;
28099 if (!STRINGP (after_string)) after_string = Qnil;
28100 }
28101
28102 mouse_face_from_buffer_pos (window, hlinfo, pos,
28103 NILP (before)
28104 ? 1
28105 : XFASTINT (before),
28106 NILP (after)
28107 ? BUF_Z (XBUFFER (buffer))
28108 : XFASTINT (after),
28109 before_string, after_string,
28110 disp_string);
28111 cursor = No_Cursor;
28112 }
28113 }
28114 }
28115
28116 check_help_echo:
28117
28118 /* Look for a `help-echo' property. */
28119 if (NILP (help_echo_string)) {
28120 Lisp_Object help, overlay;
28121
28122 /* Check overlays first. */
28123 help = overlay = Qnil;
28124 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28125 {
28126 overlay = overlay_vec[i];
28127 help = Foverlay_get (overlay, Qhelp_echo);
28128 }
28129
28130 if (!NILP (help))
28131 {
28132 help_echo_string = help;
28133 help_echo_window = window;
28134 help_echo_object = overlay;
28135 help_echo_pos = pos;
28136 }
28137 else
28138 {
28139 Lisp_Object obj = glyph->object;
28140 ptrdiff_t charpos = glyph->charpos;
28141
28142 /* Try text properties. */
28143 if (STRINGP (obj)
28144 && charpos >= 0
28145 && charpos < SCHARS (obj))
28146 {
28147 help = Fget_text_property (make_number (charpos),
28148 Qhelp_echo, obj);
28149 if (NILP (help))
28150 {
28151 /* If the string itself doesn't specify a help-echo,
28152 see if the buffer text ``under'' it does. */
28153 struct glyph_row *r
28154 = MATRIX_ROW (w->current_matrix, vpos);
28155 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28156 ptrdiff_t p = string_buffer_position (obj, start);
28157 if (p > 0)
28158 {
28159 help = Fget_char_property (make_number (p),
28160 Qhelp_echo, w->contents);
28161 if (!NILP (help))
28162 {
28163 charpos = p;
28164 obj = w->contents;
28165 }
28166 }
28167 }
28168 }
28169 else if (BUFFERP (obj)
28170 && charpos >= BEGV
28171 && charpos < ZV)
28172 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28173 obj);
28174
28175 if (!NILP (help))
28176 {
28177 help_echo_string = help;
28178 help_echo_window = window;
28179 help_echo_object = obj;
28180 help_echo_pos = charpos;
28181 }
28182 }
28183 }
28184
28185 #ifdef HAVE_WINDOW_SYSTEM
28186 /* Look for a `pointer' property. */
28187 if (FRAME_WINDOW_P (f) && NILP (pointer))
28188 {
28189 /* Check overlays first. */
28190 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28191 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28192
28193 if (NILP (pointer))
28194 {
28195 Lisp_Object obj = glyph->object;
28196 ptrdiff_t charpos = glyph->charpos;
28197
28198 /* Try text properties. */
28199 if (STRINGP (obj)
28200 && charpos >= 0
28201 && charpos < SCHARS (obj))
28202 {
28203 pointer = Fget_text_property (make_number (charpos),
28204 Qpointer, obj);
28205 if (NILP (pointer))
28206 {
28207 /* If the string itself doesn't specify a pointer,
28208 see if the buffer text ``under'' it does. */
28209 struct glyph_row *r
28210 = MATRIX_ROW (w->current_matrix, vpos);
28211 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28212 ptrdiff_t p = string_buffer_position (obj, start);
28213 if (p > 0)
28214 pointer = Fget_char_property (make_number (p),
28215 Qpointer, w->contents);
28216 }
28217 }
28218 else if (BUFFERP (obj)
28219 && charpos >= BEGV
28220 && charpos < ZV)
28221 pointer = Fget_text_property (make_number (charpos),
28222 Qpointer, obj);
28223 }
28224 }
28225 #endif /* HAVE_WINDOW_SYSTEM */
28226
28227 BEGV = obegv;
28228 ZV = ozv;
28229 current_buffer = obuf;
28230 }
28231
28232 set_cursor:
28233
28234 #ifdef HAVE_WINDOW_SYSTEM
28235 if (FRAME_WINDOW_P (f))
28236 define_frame_cursor1 (f, cursor, pointer);
28237 #else
28238 /* This is here to prevent a compiler error, about "label at end of
28239 compound statement". */
28240 return;
28241 #endif
28242 }
28243
28244
28245 /* EXPORT for RIF:
28246 Clear any mouse-face on window W. This function is part of the
28247 redisplay interface, and is called from try_window_id and similar
28248 functions to ensure the mouse-highlight is off. */
28249
28250 void
28251 x_clear_window_mouse_face (struct window *w)
28252 {
28253 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28254 Lisp_Object window;
28255
28256 block_input ();
28257 XSETWINDOW (window, w);
28258 if (EQ (window, hlinfo->mouse_face_window))
28259 clear_mouse_face (hlinfo);
28260 unblock_input ();
28261 }
28262
28263
28264 /* EXPORT:
28265 Just discard the mouse face information for frame F, if any.
28266 This is used when the size of F is changed. */
28267
28268 void
28269 cancel_mouse_face (struct frame *f)
28270 {
28271 Lisp_Object window;
28272 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28273
28274 window = hlinfo->mouse_face_window;
28275 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28276 {
28277 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28278 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28279 hlinfo->mouse_face_window = Qnil;
28280 }
28281 }
28282
28283
28284 \f
28285 /***********************************************************************
28286 Exposure Events
28287 ***********************************************************************/
28288
28289 #ifdef HAVE_WINDOW_SYSTEM
28290
28291 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28292 which intersects rectangle R. R is in window-relative coordinates. */
28293
28294 static void
28295 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28296 enum glyph_row_area area)
28297 {
28298 struct glyph *first = row->glyphs[area];
28299 struct glyph *end = row->glyphs[area] + row->used[area];
28300 struct glyph *last;
28301 int first_x, start_x, x;
28302
28303 if (area == TEXT_AREA && row->fill_line_p)
28304 /* If row extends face to end of line write the whole line. */
28305 draw_glyphs (w, 0, row, area,
28306 0, row->used[area],
28307 DRAW_NORMAL_TEXT, 0);
28308 else
28309 {
28310 /* Set START_X to the window-relative start position for drawing glyphs of
28311 AREA. The first glyph of the text area can be partially visible.
28312 The first glyphs of other areas cannot. */
28313 start_x = window_box_left_offset (w, area);
28314 x = start_x;
28315 if (area == TEXT_AREA)
28316 x += row->x;
28317
28318 /* Find the first glyph that must be redrawn. */
28319 while (first < end
28320 && x + first->pixel_width < r->x)
28321 {
28322 x += first->pixel_width;
28323 ++first;
28324 }
28325
28326 /* Find the last one. */
28327 last = first;
28328 first_x = x;
28329 while (last < end
28330 && x < r->x + r->width)
28331 {
28332 x += last->pixel_width;
28333 ++last;
28334 }
28335
28336 /* Repaint. */
28337 if (last > first)
28338 draw_glyphs (w, first_x - start_x, row, area,
28339 first - row->glyphs[area], last - row->glyphs[area],
28340 DRAW_NORMAL_TEXT, 0);
28341 }
28342 }
28343
28344
28345 /* Redraw the parts of the glyph row ROW on window W intersecting
28346 rectangle R. R is in window-relative coordinates. Value is
28347 non-zero if mouse-face was overwritten. */
28348
28349 static int
28350 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28351 {
28352 eassert (row->enabled_p);
28353
28354 if (row->mode_line_p || w->pseudo_window_p)
28355 draw_glyphs (w, 0, row, TEXT_AREA,
28356 0, row->used[TEXT_AREA],
28357 DRAW_NORMAL_TEXT, 0);
28358 else
28359 {
28360 if (row->used[LEFT_MARGIN_AREA])
28361 expose_area (w, row, r, LEFT_MARGIN_AREA);
28362 if (row->used[TEXT_AREA])
28363 expose_area (w, row, r, TEXT_AREA);
28364 if (row->used[RIGHT_MARGIN_AREA])
28365 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28366 draw_row_fringe_bitmaps (w, row);
28367 }
28368
28369 return row->mouse_face_p;
28370 }
28371
28372
28373 /* Redraw those parts of glyphs rows during expose event handling that
28374 overlap other rows. Redrawing of an exposed line writes over parts
28375 of lines overlapping that exposed line; this function fixes that.
28376
28377 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28378 row in W's current matrix that is exposed and overlaps other rows.
28379 LAST_OVERLAPPING_ROW is the last such row. */
28380
28381 static void
28382 expose_overlaps (struct window *w,
28383 struct glyph_row *first_overlapping_row,
28384 struct glyph_row *last_overlapping_row,
28385 XRectangle *r)
28386 {
28387 struct glyph_row *row;
28388
28389 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28390 if (row->overlapping_p)
28391 {
28392 eassert (row->enabled_p && !row->mode_line_p);
28393
28394 row->clip = r;
28395 if (row->used[LEFT_MARGIN_AREA])
28396 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28397
28398 if (row->used[TEXT_AREA])
28399 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28400
28401 if (row->used[RIGHT_MARGIN_AREA])
28402 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28403 row->clip = NULL;
28404 }
28405 }
28406
28407
28408 /* Return non-zero if W's cursor intersects rectangle R. */
28409
28410 static int
28411 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28412 {
28413 XRectangle cr, result;
28414 struct glyph *cursor_glyph;
28415 struct glyph_row *row;
28416
28417 if (w->phys_cursor.vpos >= 0
28418 && w->phys_cursor.vpos < w->current_matrix->nrows
28419 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28420 row->enabled_p)
28421 && row->cursor_in_fringe_p)
28422 {
28423 /* Cursor is in the fringe. */
28424 cr.x = window_box_right_offset (w,
28425 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28426 ? RIGHT_MARGIN_AREA
28427 : TEXT_AREA));
28428 cr.y = row->y;
28429 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28430 cr.height = row->height;
28431 return x_intersect_rectangles (&cr, r, &result);
28432 }
28433
28434 cursor_glyph = get_phys_cursor_glyph (w);
28435 if (cursor_glyph)
28436 {
28437 /* r is relative to W's box, but w->phys_cursor.x is relative
28438 to left edge of W's TEXT area. Adjust it. */
28439 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28440 cr.y = w->phys_cursor.y;
28441 cr.width = cursor_glyph->pixel_width;
28442 cr.height = w->phys_cursor_height;
28443 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28444 I assume the effect is the same -- and this is portable. */
28445 return x_intersect_rectangles (&cr, r, &result);
28446 }
28447 /* If we don't understand the format, pretend we're not in the hot-spot. */
28448 return 0;
28449 }
28450
28451
28452 /* EXPORT:
28453 Draw a vertical window border to the right of window W if W doesn't
28454 have vertical scroll bars. */
28455
28456 void
28457 x_draw_vertical_border (struct window *w)
28458 {
28459 struct frame *f = XFRAME (WINDOW_FRAME (w));
28460
28461 /* We could do better, if we knew what type of scroll-bar the adjacent
28462 windows (on either side) have... But we don't :-(
28463 However, I think this works ok. ++KFS 2003-04-25 */
28464
28465 /* Redraw borders between horizontally adjacent windows. Don't
28466 do it for frames with vertical scroll bars because either the
28467 right scroll bar of a window, or the left scroll bar of its
28468 neighbor will suffice as a border. */
28469 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28470 return;
28471
28472 /* Note: It is necessary to redraw both the left and the right
28473 borders, for when only this single window W is being
28474 redisplayed. */
28475 if (!WINDOW_RIGHTMOST_P (w)
28476 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28477 {
28478 int x0, x1, y0, y1;
28479
28480 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28481 y1 -= 1;
28482
28483 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28484 x1 -= 1;
28485
28486 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28487 }
28488 if (!WINDOW_LEFTMOST_P (w)
28489 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28490 {
28491 int x0, x1, y0, y1;
28492
28493 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28494 y1 -= 1;
28495
28496 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28497 x0 -= 1;
28498
28499 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28500 }
28501 }
28502
28503
28504 /* Redraw the part of window W intersection rectangle FR. Pixel
28505 coordinates in FR are frame-relative. Call this function with
28506 input blocked. Value is non-zero if the exposure overwrites
28507 mouse-face. */
28508
28509 static int
28510 expose_window (struct window *w, XRectangle *fr)
28511 {
28512 struct frame *f = XFRAME (w->frame);
28513 XRectangle wr, r;
28514 int mouse_face_overwritten_p = 0;
28515
28516 /* If window is not yet fully initialized, do nothing. This can
28517 happen when toolkit scroll bars are used and a window is split.
28518 Reconfiguring the scroll bar will generate an expose for a newly
28519 created window. */
28520 if (w->current_matrix == NULL)
28521 return 0;
28522
28523 /* When we're currently updating the window, display and current
28524 matrix usually don't agree. Arrange for a thorough display
28525 later. */
28526 if (w == updated_window)
28527 {
28528 SET_FRAME_GARBAGED (f);
28529 return 0;
28530 }
28531
28532 /* Frame-relative pixel rectangle of W. */
28533 wr.x = WINDOW_LEFT_EDGE_X (w);
28534 wr.y = WINDOW_TOP_EDGE_Y (w);
28535 wr.width = WINDOW_TOTAL_WIDTH (w);
28536 wr.height = WINDOW_TOTAL_HEIGHT (w);
28537
28538 if (x_intersect_rectangles (fr, &wr, &r))
28539 {
28540 int yb = window_text_bottom_y (w);
28541 struct glyph_row *row;
28542 int cursor_cleared_p, phys_cursor_on_p;
28543 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28544
28545 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28546 r.x, r.y, r.width, r.height));
28547
28548 /* Convert to window coordinates. */
28549 r.x -= WINDOW_LEFT_EDGE_X (w);
28550 r.y -= WINDOW_TOP_EDGE_Y (w);
28551
28552 /* Turn off the cursor. */
28553 if (!w->pseudo_window_p
28554 && phys_cursor_in_rect_p (w, &r))
28555 {
28556 x_clear_cursor (w);
28557 cursor_cleared_p = 1;
28558 }
28559 else
28560 cursor_cleared_p = 0;
28561
28562 /* If the row containing the cursor extends face to end of line,
28563 then expose_area might overwrite the cursor outside the
28564 rectangle and thus notice_overwritten_cursor might clear
28565 w->phys_cursor_on_p. We remember the original value and
28566 check later if it is changed. */
28567 phys_cursor_on_p = w->phys_cursor_on_p;
28568
28569 /* Update lines intersecting rectangle R. */
28570 first_overlapping_row = last_overlapping_row = NULL;
28571 for (row = w->current_matrix->rows;
28572 row->enabled_p;
28573 ++row)
28574 {
28575 int y0 = row->y;
28576 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28577
28578 if ((y0 >= r.y && y0 < r.y + r.height)
28579 || (y1 > r.y && y1 < r.y + r.height)
28580 || (r.y >= y0 && r.y < y1)
28581 || (r.y + r.height > y0 && r.y + r.height < y1))
28582 {
28583 /* A header line may be overlapping, but there is no need
28584 to fix overlapping areas for them. KFS 2005-02-12 */
28585 if (row->overlapping_p && !row->mode_line_p)
28586 {
28587 if (first_overlapping_row == NULL)
28588 first_overlapping_row = row;
28589 last_overlapping_row = row;
28590 }
28591
28592 row->clip = fr;
28593 if (expose_line (w, row, &r))
28594 mouse_face_overwritten_p = 1;
28595 row->clip = NULL;
28596 }
28597 else if (row->overlapping_p)
28598 {
28599 /* We must redraw a row overlapping the exposed area. */
28600 if (y0 < r.y
28601 ? y0 + row->phys_height > r.y
28602 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28603 {
28604 if (first_overlapping_row == NULL)
28605 first_overlapping_row = row;
28606 last_overlapping_row = row;
28607 }
28608 }
28609
28610 if (y1 >= yb)
28611 break;
28612 }
28613
28614 /* Display the mode line if there is one. */
28615 if (WINDOW_WANTS_MODELINE_P (w)
28616 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28617 row->enabled_p)
28618 && row->y < r.y + r.height)
28619 {
28620 if (expose_line (w, row, &r))
28621 mouse_face_overwritten_p = 1;
28622 }
28623
28624 if (!w->pseudo_window_p)
28625 {
28626 /* Fix the display of overlapping rows. */
28627 if (first_overlapping_row)
28628 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28629 fr);
28630
28631 /* Draw border between windows. */
28632 x_draw_vertical_border (w);
28633
28634 /* Turn the cursor on again. */
28635 if (cursor_cleared_p
28636 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28637 update_window_cursor (w, 1);
28638 }
28639 }
28640
28641 return mouse_face_overwritten_p;
28642 }
28643
28644
28645
28646 /* Redraw (parts) of all windows in the window tree rooted at W that
28647 intersect R. R contains frame pixel coordinates. Value is
28648 non-zero if the exposure overwrites mouse-face. */
28649
28650 static int
28651 expose_window_tree (struct window *w, XRectangle *r)
28652 {
28653 struct frame *f = XFRAME (w->frame);
28654 int mouse_face_overwritten_p = 0;
28655
28656 while (w && !FRAME_GARBAGED_P (f))
28657 {
28658 if (WINDOWP (w->contents))
28659 mouse_face_overwritten_p
28660 |= expose_window_tree (XWINDOW (w->contents), r);
28661 else
28662 mouse_face_overwritten_p |= expose_window (w, r);
28663
28664 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28665 }
28666
28667 return mouse_face_overwritten_p;
28668 }
28669
28670
28671 /* EXPORT:
28672 Redisplay an exposed area of frame F. X and Y are the upper-left
28673 corner of the exposed rectangle. W and H are width and height of
28674 the exposed area. All are pixel values. W or H zero means redraw
28675 the entire frame. */
28676
28677 void
28678 expose_frame (struct frame *f, int x, int y, int w, int h)
28679 {
28680 XRectangle r;
28681 int mouse_face_overwritten_p = 0;
28682
28683 TRACE ((stderr, "expose_frame "));
28684
28685 /* No need to redraw if frame will be redrawn soon. */
28686 if (FRAME_GARBAGED_P (f))
28687 {
28688 TRACE ((stderr, " garbaged\n"));
28689 return;
28690 }
28691
28692 /* If basic faces haven't been realized yet, there is no point in
28693 trying to redraw anything. This can happen when we get an expose
28694 event while Emacs is starting, e.g. by moving another window. */
28695 if (FRAME_FACE_CACHE (f) == NULL
28696 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28697 {
28698 TRACE ((stderr, " no faces\n"));
28699 return;
28700 }
28701
28702 if (w == 0 || h == 0)
28703 {
28704 r.x = r.y = 0;
28705 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28706 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28707 }
28708 else
28709 {
28710 r.x = x;
28711 r.y = y;
28712 r.width = w;
28713 r.height = h;
28714 }
28715
28716 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28717 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28718
28719 if (WINDOWP (f->tool_bar_window))
28720 mouse_face_overwritten_p
28721 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28722
28723 #ifdef HAVE_X_WINDOWS
28724 #ifndef MSDOS
28725 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28726 if (WINDOWP (f->menu_bar_window))
28727 mouse_face_overwritten_p
28728 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28729 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28730 #endif
28731 #endif
28732
28733 /* Some window managers support a focus-follows-mouse style with
28734 delayed raising of frames. Imagine a partially obscured frame,
28735 and moving the mouse into partially obscured mouse-face on that
28736 frame. The visible part of the mouse-face will be highlighted,
28737 then the WM raises the obscured frame. With at least one WM, KDE
28738 2.1, Emacs is not getting any event for the raising of the frame
28739 (even tried with SubstructureRedirectMask), only Expose events.
28740 These expose events will draw text normally, i.e. not
28741 highlighted. Which means we must redo the highlight here.
28742 Subsume it under ``we love X''. --gerd 2001-08-15 */
28743 /* Included in Windows version because Windows most likely does not
28744 do the right thing if any third party tool offers
28745 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28746 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28747 {
28748 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28749 if (f == hlinfo->mouse_face_mouse_frame)
28750 {
28751 int mouse_x = hlinfo->mouse_face_mouse_x;
28752 int mouse_y = hlinfo->mouse_face_mouse_y;
28753 clear_mouse_face (hlinfo);
28754 note_mouse_highlight (f, mouse_x, mouse_y);
28755 }
28756 }
28757 }
28758
28759
28760 /* EXPORT:
28761 Determine the intersection of two rectangles R1 and R2. Return
28762 the intersection in *RESULT. Value is non-zero if RESULT is not
28763 empty. */
28764
28765 int
28766 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28767 {
28768 XRectangle *left, *right;
28769 XRectangle *upper, *lower;
28770 int intersection_p = 0;
28771
28772 /* Rearrange so that R1 is the left-most rectangle. */
28773 if (r1->x < r2->x)
28774 left = r1, right = r2;
28775 else
28776 left = r2, right = r1;
28777
28778 /* X0 of the intersection is right.x0, if this is inside R1,
28779 otherwise there is no intersection. */
28780 if (right->x <= left->x + left->width)
28781 {
28782 result->x = right->x;
28783
28784 /* The right end of the intersection is the minimum of
28785 the right ends of left and right. */
28786 result->width = (min (left->x + left->width, right->x + right->width)
28787 - result->x);
28788
28789 /* Same game for Y. */
28790 if (r1->y < r2->y)
28791 upper = r1, lower = r2;
28792 else
28793 upper = r2, lower = r1;
28794
28795 /* The upper end of the intersection is lower.y0, if this is inside
28796 of upper. Otherwise, there is no intersection. */
28797 if (lower->y <= upper->y + upper->height)
28798 {
28799 result->y = lower->y;
28800
28801 /* The lower end of the intersection is the minimum of the lower
28802 ends of upper and lower. */
28803 result->height = (min (lower->y + lower->height,
28804 upper->y + upper->height)
28805 - result->y);
28806 intersection_p = 1;
28807 }
28808 }
28809
28810 return intersection_p;
28811 }
28812
28813 #endif /* HAVE_WINDOW_SYSTEM */
28814
28815 \f
28816 /***********************************************************************
28817 Initialization
28818 ***********************************************************************/
28819
28820 void
28821 syms_of_xdisp (void)
28822 {
28823 Vwith_echo_area_save_vector = Qnil;
28824 staticpro (&Vwith_echo_area_save_vector);
28825
28826 Vmessage_stack = Qnil;
28827 staticpro (&Vmessage_stack);
28828
28829 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28830 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28831
28832 message_dolog_marker1 = Fmake_marker ();
28833 staticpro (&message_dolog_marker1);
28834 message_dolog_marker2 = Fmake_marker ();
28835 staticpro (&message_dolog_marker2);
28836 message_dolog_marker3 = Fmake_marker ();
28837 staticpro (&message_dolog_marker3);
28838
28839 #ifdef GLYPH_DEBUG
28840 defsubr (&Sdump_frame_glyph_matrix);
28841 defsubr (&Sdump_glyph_matrix);
28842 defsubr (&Sdump_glyph_row);
28843 defsubr (&Sdump_tool_bar_row);
28844 defsubr (&Strace_redisplay);
28845 defsubr (&Strace_to_stderr);
28846 #endif
28847 #ifdef HAVE_WINDOW_SYSTEM
28848 defsubr (&Stool_bar_lines_needed);
28849 defsubr (&Slookup_image_map);
28850 #endif
28851 defsubr (&Sformat_mode_line);
28852 defsubr (&Sinvisible_p);
28853 defsubr (&Scurrent_bidi_paragraph_direction);
28854
28855 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28856 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28857 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28858 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28859 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28860 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28861 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28862 DEFSYM (Qeval, "eval");
28863 DEFSYM (QCdata, ":data");
28864 DEFSYM (Qdisplay, "display");
28865 DEFSYM (Qspace_width, "space-width");
28866 DEFSYM (Qraise, "raise");
28867 DEFSYM (Qslice, "slice");
28868 DEFSYM (Qspace, "space");
28869 DEFSYM (Qmargin, "margin");
28870 DEFSYM (Qpointer, "pointer");
28871 DEFSYM (Qleft_margin, "left-margin");
28872 DEFSYM (Qright_margin, "right-margin");
28873 DEFSYM (Qcenter, "center");
28874 DEFSYM (Qline_height, "line-height");
28875 DEFSYM (QCalign_to, ":align-to");
28876 DEFSYM (QCrelative_width, ":relative-width");
28877 DEFSYM (QCrelative_height, ":relative-height");
28878 DEFSYM (QCeval, ":eval");
28879 DEFSYM (QCpropertize, ":propertize");
28880 DEFSYM (QCfile, ":file");
28881 DEFSYM (Qfontified, "fontified");
28882 DEFSYM (Qfontification_functions, "fontification-functions");
28883 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28884 DEFSYM (Qescape_glyph, "escape-glyph");
28885 DEFSYM (Qnobreak_space, "nobreak-space");
28886 DEFSYM (Qimage, "image");
28887 DEFSYM (Qtext, "text");
28888 DEFSYM (Qboth, "both");
28889 DEFSYM (Qboth_horiz, "both-horiz");
28890 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28891 DEFSYM (QCmap, ":map");
28892 DEFSYM (QCpointer, ":pointer");
28893 DEFSYM (Qrect, "rect");
28894 DEFSYM (Qcircle, "circle");
28895 DEFSYM (Qpoly, "poly");
28896 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28897 DEFSYM (Qgrow_only, "grow-only");
28898 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28899 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28900 DEFSYM (Qposition, "position");
28901 DEFSYM (Qbuffer_position, "buffer-position");
28902 DEFSYM (Qobject, "object");
28903 DEFSYM (Qbar, "bar");
28904 DEFSYM (Qhbar, "hbar");
28905 DEFSYM (Qbox, "box");
28906 DEFSYM (Qhollow, "hollow");
28907 DEFSYM (Qhand, "hand");
28908 DEFSYM (Qarrow, "arrow");
28909 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28910
28911 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28912 Fcons (intern_c_string ("void-variable"), Qnil)),
28913 Qnil);
28914 staticpro (&list_of_error);
28915
28916 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28917 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28918 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28919 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28920
28921 echo_buffer[0] = echo_buffer[1] = Qnil;
28922 staticpro (&echo_buffer[0]);
28923 staticpro (&echo_buffer[1]);
28924
28925 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28926 staticpro (&echo_area_buffer[0]);
28927 staticpro (&echo_area_buffer[1]);
28928
28929 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28930 staticpro (&Vmessages_buffer_name);
28931
28932 mode_line_proptrans_alist = Qnil;
28933 staticpro (&mode_line_proptrans_alist);
28934 mode_line_string_list = Qnil;
28935 staticpro (&mode_line_string_list);
28936 mode_line_string_face = Qnil;
28937 staticpro (&mode_line_string_face);
28938 mode_line_string_face_prop = Qnil;
28939 staticpro (&mode_line_string_face_prop);
28940 Vmode_line_unwind_vector = Qnil;
28941 staticpro (&Vmode_line_unwind_vector);
28942
28943 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28944
28945 help_echo_string = Qnil;
28946 staticpro (&help_echo_string);
28947 help_echo_object = Qnil;
28948 staticpro (&help_echo_object);
28949 help_echo_window = Qnil;
28950 staticpro (&help_echo_window);
28951 previous_help_echo_string = Qnil;
28952 staticpro (&previous_help_echo_string);
28953 help_echo_pos = -1;
28954
28955 DEFSYM (Qright_to_left, "right-to-left");
28956 DEFSYM (Qleft_to_right, "left-to-right");
28957
28958 #ifdef HAVE_WINDOW_SYSTEM
28959 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28960 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28961 For example, if a block cursor is over a tab, it will be drawn as
28962 wide as that tab on the display. */);
28963 x_stretch_cursor_p = 0;
28964 #endif
28965
28966 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28967 doc: /* Non-nil means highlight trailing whitespace.
28968 The face used for trailing whitespace is `trailing-whitespace'. */);
28969 Vshow_trailing_whitespace = Qnil;
28970
28971 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28972 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28973 If the value is t, Emacs highlights non-ASCII chars which have the
28974 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28975 or `escape-glyph' face respectively.
28976
28977 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28978 U+2011 (non-breaking hyphen) are affected.
28979
28980 Any other non-nil value means to display these characters as a escape
28981 glyph followed by an ordinary space or hyphen.
28982
28983 A value of nil means no special handling of these characters. */);
28984 Vnobreak_char_display = Qt;
28985
28986 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28987 doc: /* The pointer shape to show in void text areas.
28988 A value of nil means to show the text pointer. Other options are `arrow',
28989 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28990 Vvoid_text_area_pointer = Qarrow;
28991
28992 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28993 doc: /* Non-nil means don't actually do any redisplay.
28994 This is used for internal purposes. */);
28995 Vinhibit_redisplay = Qnil;
28996
28997 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28998 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28999 Vglobal_mode_string = Qnil;
29000
29001 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29002 doc: /* Marker for where to display an arrow on top of the buffer text.
29003 This must be the beginning of a line in order to work.
29004 See also `overlay-arrow-string'. */);
29005 Voverlay_arrow_position = Qnil;
29006
29007 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29008 doc: /* String to display as an arrow in non-window frames.
29009 See also `overlay-arrow-position'. */);
29010 Voverlay_arrow_string = build_pure_c_string ("=>");
29011
29012 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29013 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29014 The symbols on this list are examined during redisplay to determine
29015 where to display overlay arrows. */);
29016 Voverlay_arrow_variable_list
29017 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
29018
29019 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29020 doc: /* The number of lines to try scrolling a window by when point moves out.
29021 If that fails to bring point back on frame, point is centered instead.
29022 If this is zero, point is always centered after it moves off frame.
29023 If you want scrolling to always be a line at a time, you should set
29024 `scroll-conservatively' to a large value rather than set this to 1. */);
29025
29026 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29027 doc: /* Scroll up to this many lines, to bring point back on screen.
29028 If point moves off-screen, redisplay will scroll by up to
29029 `scroll-conservatively' lines in order to bring point just barely
29030 onto the screen again. If that cannot be done, then redisplay
29031 recenters point as usual.
29032
29033 If the value is greater than 100, redisplay will never recenter point,
29034 but will always scroll just enough text to bring point into view, even
29035 if you move far away.
29036
29037 A value of zero means always recenter point if it moves off screen. */);
29038 scroll_conservatively = 0;
29039
29040 DEFVAR_INT ("scroll-margin", scroll_margin,
29041 doc: /* Number of lines of margin at the top and bottom of a window.
29042 Recenter the window whenever point gets within this many lines
29043 of the top or bottom of the window. */);
29044 scroll_margin = 0;
29045
29046 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29047 doc: /* Pixels per inch value for non-window system displays.
29048 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29049 Vdisplay_pixels_per_inch = make_float (72.0);
29050
29051 #ifdef GLYPH_DEBUG
29052 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29053 #endif
29054
29055 DEFVAR_LISP ("truncate-partial-width-windows",
29056 Vtruncate_partial_width_windows,
29057 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29058 For an integer value, truncate lines in each window narrower than the
29059 full frame width, provided the window width is less than that integer;
29060 otherwise, respect the value of `truncate-lines'.
29061
29062 For any other non-nil value, truncate lines in all windows that do
29063 not span the full frame width.
29064
29065 A value of nil means to respect the value of `truncate-lines'.
29066
29067 If `word-wrap' is enabled, you might want to reduce this. */);
29068 Vtruncate_partial_width_windows = make_number (50);
29069
29070 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29071 doc: /* Maximum buffer size for which line number should be displayed.
29072 If the buffer is bigger than this, the line number does not appear
29073 in the mode line. A value of nil means no limit. */);
29074 Vline_number_display_limit = Qnil;
29075
29076 DEFVAR_INT ("line-number-display-limit-width",
29077 line_number_display_limit_width,
29078 doc: /* Maximum line width (in characters) for line number display.
29079 If the average length of the lines near point is bigger than this, then the
29080 line number may be omitted from the mode line. */);
29081 line_number_display_limit_width = 200;
29082
29083 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29084 doc: /* Non-nil means highlight region even in nonselected windows. */);
29085 highlight_nonselected_windows = 0;
29086
29087 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29088 doc: /* Non-nil if more than one frame is visible on this display.
29089 Minibuffer-only frames don't count, but iconified frames do.
29090 This variable is not guaranteed to be accurate except while processing
29091 `frame-title-format' and `icon-title-format'. */);
29092
29093 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29094 doc: /* Template for displaying the title bar of visible frames.
29095 \(Assuming the window manager supports this feature.)
29096
29097 This variable has the same structure as `mode-line-format', except that
29098 the %c and %l constructs are ignored. It is used only on frames for
29099 which no explicit name has been set \(see `modify-frame-parameters'). */);
29100
29101 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29102 doc: /* Template for displaying the title bar of an iconified frame.
29103 \(Assuming the window manager supports this feature.)
29104 This variable has the same structure as `mode-line-format' (which see),
29105 and is used only on frames for which no explicit name has been set
29106 \(see `modify-frame-parameters'). */);
29107 Vicon_title_format
29108 = Vframe_title_format
29109 = listn (CONSTYPE_PURE, 3,
29110 intern_c_string ("multiple-frames"),
29111 build_pure_c_string ("%b"),
29112 listn (CONSTYPE_PURE, 4,
29113 empty_unibyte_string,
29114 intern_c_string ("invocation-name"),
29115 build_pure_c_string ("@"),
29116 intern_c_string ("system-name")));
29117
29118 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29119 doc: /* Maximum number of lines to keep in the message log buffer.
29120 If nil, disable message logging. If t, log messages but don't truncate
29121 the buffer when it becomes large. */);
29122 Vmessage_log_max = make_number (1000);
29123
29124 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29125 doc: /* Functions called before redisplay, if window sizes have changed.
29126 The value should be a list of functions that take one argument.
29127 Just before redisplay, for each frame, if any of its windows have changed
29128 size since the last redisplay, or have been split or deleted,
29129 all the functions in the list are called, with the frame as argument. */);
29130 Vwindow_size_change_functions = Qnil;
29131
29132 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29133 doc: /* List of functions to call before redisplaying a window with scrolling.
29134 Each function is called with two arguments, the window and its new
29135 display-start position. Note that these functions are also called by
29136 `set-window-buffer'. Also note that the value of `window-end' is not
29137 valid when these functions are called.
29138
29139 Warning: Do not use this feature to alter the way the window
29140 is scrolled. It is not designed for that, and such use probably won't
29141 work. */);
29142 Vwindow_scroll_functions = Qnil;
29143
29144 DEFVAR_LISP ("window-text-change-functions",
29145 Vwindow_text_change_functions,
29146 doc: /* Functions to call in redisplay when text in the window might change. */);
29147 Vwindow_text_change_functions = Qnil;
29148
29149 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29150 doc: /* Functions called when redisplay of a window reaches the end trigger.
29151 Each function is called with two arguments, the window and the end trigger value.
29152 See `set-window-redisplay-end-trigger'. */);
29153 Vredisplay_end_trigger_functions = Qnil;
29154
29155 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29156 doc: /* Non-nil means autoselect window with mouse pointer.
29157 If nil, do not autoselect windows.
29158 A positive number means delay autoselection by that many seconds: a
29159 window is autoselected only after the mouse has remained in that
29160 window for the duration of the delay.
29161 A negative number has a similar effect, but causes windows to be
29162 autoselected only after the mouse has stopped moving. \(Because of
29163 the way Emacs compares mouse events, you will occasionally wait twice
29164 that time before the window gets selected.\)
29165 Any other value means to autoselect window instantaneously when the
29166 mouse pointer enters it.
29167
29168 Autoselection selects the minibuffer only if it is active, and never
29169 unselects the minibuffer if it is active.
29170
29171 When customizing this variable make sure that the actual value of
29172 `focus-follows-mouse' matches the behavior of your window manager. */);
29173 Vmouse_autoselect_window = Qnil;
29174
29175 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29176 doc: /* Non-nil means automatically resize tool-bars.
29177 This dynamically changes the tool-bar's height to the minimum height
29178 that is needed to make all tool-bar items visible.
29179 If value is `grow-only', the tool-bar's height is only increased
29180 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29181 Vauto_resize_tool_bars = Qt;
29182
29183 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29184 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29185 auto_raise_tool_bar_buttons_p = 1;
29186
29187 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29188 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29189 make_cursor_line_fully_visible_p = 1;
29190
29191 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29192 doc: /* Border below tool-bar in pixels.
29193 If an integer, use it as the height of the border.
29194 If it is one of `internal-border-width' or `border-width', use the
29195 value of the corresponding frame parameter.
29196 Otherwise, no border is added below the tool-bar. */);
29197 Vtool_bar_border = Qinternal_border_width;
29198
29199 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29200 doc: /* Margin around tool-bar buttons in pixels.
29201 If an integer, use that for both horizontal and vertical margins.
29202 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29203 HORZ specifying the horizontal margin, and VERT specifying the
29204 vertical margin. */);
29205 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29206
29207 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29208 doc: /* Relief thickness of tool-bar buttons. */);
29209 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29210
29211 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29212 doc: /* Tool bar style to use.
29213 It can be one of
29214 image - show images only
29215 text - show text only
29216 both - show both, text below image
29217 both-horiz - show text to the right of the image
29218 text-image-horiz - show text to the left of the image
29219 any other - use system default or image if no system default.
29220
29221 This variable only affects the GTK+ toolkit version of Emacs. */);
29222 Vtool_bar_style = Qnil;
29223
29224 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29225 doc: /* Maximum number of characters a label can have to be shown.
29226 The tool bar style must also show labels for this to have any effect, see
29227 `tool-bar-style'. */);
29228 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29229
29230 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29231 doc: /* List of functions to call to fontify regions of text.
29232 Each function is called with one argument POS. Functions must
29233 fontify a region starting at POS in the current buffer, and give
29234 fontified regions the property `fontified'. */);
29235 Vfontification_functions = Qnil;
29236 Fmake_variable_buffer_local (Qfontification_functions);
29237
29238 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29239 unibyte_display_via_language_environment,
29240 doc: /* Non-nil means display unibyte text according to language environment.
29241 Specifically, this means that raw bytes in the range 160-255 decimal
29242 are displayed by converting them to the equivalent multibyte characters
29243 according to the current language environment. As a result, they are
29244 displayed according to the current fontset.
29245
29246 Note that this variable affects only how these bytes are displayed,
29247 but does not change the fact they are interpreted as raw bytes. */);
29248 unibyte_display_via_language_environment = 0;
29249
29250 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29251 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29252 If a float, it specifies a fraction of the mini-window frame's height.
29253 If an integer, it specifies a number of lines. */);
29254 Vmax_mini_window_height = make_float (0.25);
29255
29256 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29257 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29258 A value of nil means don't automatically resize mini-windows.
29259 A value of t means resize them to fit the text displayed in them.
29260 A value of `grow-only', the default, means let mini-windows grow only;
29261 they return to their normal size when the minibuffer is closed, or the
29262 echo area becomes empty. */);
29263 Vresize_mini_windows = Qgrow_only;
29264
29265 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29266 doc: /* Alist specifying how to blink the cursor off.
29267 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29268 `cursor-type' frame-parameter or variable equals ON-STATE,
29269 comparing using `equal', Emacs uses OFF-STATE to specify
29270 how to blink it off. ON-STATE and OFF-STATE are values for
29271 the `cursor-type' frame parameter.
29272
29273 If a frame's ON-STATE has no entry in this list,
29274 the frame's other specifications determine how to blink the cursor off. */);
29275 Vblink_cursor_alist = Qnil;
29276
29277 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29278 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29279 If non-nil, windows are automatically scrolled horizontally to make
29280 point visible. */);
29281 automatic_hscrolling_p = 1;
29282 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29283
29284 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29285 doc: /* How many columns away from the window edge point is allowed to get
29286 before automatic hscrolling will horizontally scroll the window. */);
29287 hscroll_margin = 5;
29288
29289 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29290 doc: /* How many columns to scroll the window when point gets too close to the edge.
29291 When point is less than `hscroll-margin' columns from the window
29292 edge, automatic hscrolling will scroll the window by the amount of columns
29293 determined by this variable. If its value is a positive integer, scroll that
29294 many columns. If it's a positive floating-point number, it specifies the
29295 fraction of the window's width to scroll. If it's nil or zero, point will be
29296 centered horizontally after the scroll. Any other value, including negative
29297 numbers, are treated as if the value were zero.
29298
29299 Automatic hscrolling always moves point outside the scroll margin, so if
29300 point was more than scroll step columns inside the margin, the window will
29301 scroll more than the value given by the scroll step.
29302
29303 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29304 and `scroll-right' overrides this variable's effect. */);
29305 Vhscroll_step = make_number (0);
29306
29307 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29308 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29309 Bind this around calls to `message' to let it take effect. */);
29310 message_truncate_lines = 0;
29311
29312 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29313 doc: /* Normal hook run to update the menu bar definitions.
29314 Redisplay runs this hook before it redisplays the menu bar.
29315 This is used to update submenus such as Buffers,
29316 whose contents depend on various data. */);
29317 Vmenu_bar_update_hook = Qnil;
29318
29319 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29320 doc: /* Frame for which we are updating a menu.
29321 The enable predicate for a menu binding should check this variable. */);
29322 Vmenu_updating_frame = Qnil;
29323
29324 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29325 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29326 inhibit_menubar_update = 0;
29327
29328 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29329 doc: /* Prefix prepended to all continuation lines at display time.
29330 The value may be a string, an image, or a stretch-glyph; it is
29331 interpreted in the same way as the value of a `display' text property.
29332
29333 This variable is overridden by any `wrap-prefix' text or overlay
29334 property.
29335
29336 To add a prefix to non-continuation lines, use `line-prefix'. */);
29337 Vwrap_prefix = Qnil;
29338 DEFSYM (Qwrap_prefix, "wrap-prefix");
29339 Fmake_variable_buffer_local (Qwrap_prefix);
29340
29341 DEFVAR_LISP ("line-prefix", Vline_prefix,
29342 doc: /* Prefix prepended to all non-continuation lines at display time.
29343 The value may be a string, an image, or a stretch-glyph; it is
29344 interpreted in the same way as the value of a `display' text property.
29345
29346 This variable is overridden by any `line-prefix' text or overlay
29347 property.
29348
29349 To add a prefix to continuation lines, use `wrap-prefix'. */);
29350 Vline_prefix = Qnil;
29351 DEFSYM (Qline_prefix, "line-prefix");
29352 Fmake_variable_buffer_local (Qline_prefix);
29353
29354 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29355 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29356 inhibit_eval_during_redisplay = 0;
29357
29358 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29359 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29360 inhibit_free_realized_faces = 0;
29361
29362 #ifdef GLYPH_DEBUG
29363 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29364 doc: /* Inhibit try_window_id display optimization. */);
29365 inhibit_try_window_id = 0;
29366
29367 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29368 doc: /* Inhibit try_window_reusing display optimization. */);
29369 inhibit_try_window_reusing = 0;
29370
29371 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29372 doc: /* Inhibit try_cursor_movement display optimization. */);
29373 inhibit_try_cursor_movement = 0;
29374 #endif /* GLYPH_DEBUG */
29375
29376 DEFVAR_INT ("overline-margin", overline_margin,
29377 doc: /* Space between overline and text, in pixels.
29378 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29379 margin to the character height. */);
29380 overline_margin = 2;
29381
29382 DEFVAR_INT ("underline-minimum-offset",
29383 underline_minimum_offset,
29384 doc: /* Minimum distance between baseline and underline.
29385 This can improve legibility of underlined text at small font sizes,
29386 particularly when using variable `x-use-underline-position-properties'
29387 with fonts that specify an UNDERLINE_POSITION relatively close to the
29388 baseline. The default value is 1. */);
29389 underline_minimum_offset = 1;
29390
29391 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29392 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29393 This feature only works when on a window system that can change
29394 cursor shapes. */);
29395 display_hourglass_p = 1;
29396
29397 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29398 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29399 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29400
29401 hourglass_atimer = NULL;
29402 hourglass_shown_p = 0;
29403
29404 DEFSYM (Qglyphless_char, "glyphless-char");
29405 DEFSYM (Qhex_code, "hex-code");
29406 DEFSYM (Qempty_box, "empty-box");
29407 DEFSYM (Qthin_space, "thin-space");
29408 DEFSYM (Qzero_width, "zero-width");
29409
29410 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29411 /* Intern this now in case it isn't already done.
29412 Setting this variable twice is harmless.
29413 But don't staticpro it here--that is done in alloc.c. */
29414 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29415 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29416
29417 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29418 doc: /* Char-table defining glyphless characters.
29419 Each element, if non-nil, should be one of the following:
29420 an ASCII acronym string: display this string in a box
29421 `hex-code': display the hexadecimal code of a character in a box
29422 `empty-box': display as an empty box
29423 `thin-space': display as 1-pixel width space
29424 `zero-width': don't display
29425 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29426 display method for graphical terminals and text terminals respectively.
29427 GRAPHICAL and TEXT should each have one of the values listed above.
29428
29429 The char-table has one extra slot to control the display of a character for
29430 which no font is found. This slot only takes effect on graphical terminals.
29431 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29432 `thin-space'. The default is `empty-box'. */);
29433 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29434 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29435 Qempty_box);
29436
29437 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29438 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29439 Vdebug_on_message = Qnil;
29440 }
29441
29442
29443 /* Initialize this module when Emacs starts. */
29444
29445 void
29446 init_xdisp (void)
29447 {
29448 current_header_line_height = current_mode_line_height = -1;
29449
29450 CHARPOS (this_line_start_pos) = 0;
29451
29452 if (!noninteractive)
29453 {
29454 struct window *m = XWINDOW (minibuf_window);
29455 Lisp_Object frame = m->frame;
29456 struct frame *f = XFRAME (frame);
29457 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29458 struct window *r = XWINDOW (root);
29459 int i;
29460
29461 echo_area_window = minibuf_window;
29462
29463 r->top_line = FRAME_TOP_MARGIN (f);
29464 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29465 r->total_cols = FRAME_COLS (f);
29466
29467 m->top_line = FRAME_LINES (f) - 1;
29468 m->total_lines = 1;
29469 m->total_cols = FRAME_COLS (f);
29470
29471 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29472 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29473 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29474
29475 /* The default ellipsis glyphs `...'. */
29476 for (i = 0; i < 3; ++i)
29477 default_invis_vector[i] = make_number ('.');
29478 }
29479
29480 {
29481 /* Allocate the buffer for frame titles.
29482 Also used for `format-mode-line'. */
29483 int size = 100;
29484 mode_line_noprop_buf = xmalloc (size);
29485 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29486 mode_line_noprop_ptr = mode_line_noprop_buf;
29487 mode_line_target = MODE_LINE_DISPLAY;
29488 }
29489
29490 help_echo_showing_p = 0;
29491 }
29492
29493 /* Platform-independent portion of hourglass implementation. */
29494
29495 /* Cancel a currently active hourglass timer, and start a new one. */
29496 void
29497 start_hourglass (void)
29498 {
29499 #if defined (HAVE_WINDOW_SYSTEM)
29500 EMACS_TIME delay;
29501
29502 cancel_hourglass ();
29503
29504 if (INTEGERP (Vhourglass_delay)
29505 && XINT (Vhourglass_delay) > 0)
29506 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29507 TYPE_MAXIMUM (time_t)),
29508 0);
29509 else if (FLOATP (Vhourglass_delay)
29510 && XFLOAT_DATA (Vhourglass_delay) > 0)
29511 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29512 else
29513 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29514
29515 #ifdef HAVE_NTGUI
29516 {
29517 extern void w32_note_current_window (void);
29518 w32_note_current_window ();
29519 }
29520 #endif /* HAVE_NTGUI */
29521
29522 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29523 show_hourglass, NULL);
29524 #endif
29525 }
29526
29527
29528 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29529 shown. */
29530 void
29531 cancel_hourglass (void)
29532 {
29533 #if defined (HAVE_WINDOW_SYSTEM)
29534 if (hourglass_atimer)
29535 {
29536 cancel_atimer (hourglass_atimer);
29537 hourglass_atimer = NULL;
29538 }
29539
29540 if (hourglass_shown_p)
29541 hide_hourglass ();
29542 #endif
29543 }