]> code.delx.au - gnu-emacs/blob - src/xdisp.c
merge from trunk
[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
385 #else /* !HAVE_WINDOW_SYSTEM */
386 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
387 #endif /* HAVE_WINDOW_SYSTEM */
388
389 /* Test if the display element loaded in IT, or the underlying buffer
390 or string character, is a space or a TAB character. This is used
391 to determine where word wrapping can occur. */
392
393 #define IT_DISPLAYING_WHITESPACE(it) \
394 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
395 || ((STRINGP (it->string) \
396 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
397 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
398 || (it->s \
399 && (it->s[IT_BYTEPOS (*it)] == ' ' \
400 || it->s[IT_BYTEPOS (*it)] == '\t')) \
401 || (IT_BYTEPOS (*it) < ZV_BYTE \
402 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
403 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
404
405 /* Name of the face used to highlight trailing whitespace. */
406
407 static Lisp_Object Qtrailing_whitespace;
408
409 /* Name and number of the face used to highlight escape glyphs. */
410
411 static Lisp_Object Qescape_glyph;
412
413 /* Name and number of the face used to highlight non-breaking spaces. */
414
415 static Lisp_Object Qnobreak_space;
416
417 /* The symbol `image' which is the car of the lists used to represent
418 images in Lisp. Also a tool bar style. */
419
420 Lisp_Object Qimage;
421
422 /* The image map types. */
423 Lisp_Object QCmap;
424 static Lisp_Object QCpointer;
425 static Lisp_Object Qrect, Qcircle, Qpoly;
426
427 /* Tool bar styles */
428 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
429
430 /* Non-zero means print newline to stdout before next mini-buffer
431 message. */
432
433 int noninteractive_need_newline;
434
435 /* Non-zero means print newline to message log before next message. */
436
437 static int message_log_need_newline;
438
439 /* Three markers that message_dolog uses.
440 It could allocate them itself, but that causes trouble
441 in handling memory-full errors. */
442 static Lisp_Object message_dolog_marker1;
443 static Lisp_Object message_dolog_marker2;
444 static Lisp_Object message_dolog_marker3;
445 \f
446 /* The buffer position of the first character appearing entirely or
447 partially on the line of the selected window which contains the
448 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
449 redisplay optimization in redisplay_internal. */
450
451 static struct text_pos this_line_start_pos;
452
453 /* Number of characters past the end of the line above, including the
454 terminating newline. */
455
456 static struct text_pos this_line_end_pos;
457
458 /* The vertical positions and the height of this line. */
459
460 static int this_line_vpos;
461 static int this_line_y;
462 static int this_line_pixel_height;
463
464 /* X position at which this display line starts. Usually zero;
465 negative if first character is partially visible. */
466
467 static int this_line_start_x;
468
469 /* The smallest character position seen by move_it_* functions as they
470 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
471 hscrolled lines, see display_line. */
472
473 static struct text_pos this_line_min_pos;
474
475 /* Buffer that this_line_.* variables are referring to. */
476
477 static struct buffer *this_line_buffer;
478
479
480 /* Values of those variables at last redisplay are stored as
481 properties on `overlay-arrow-position' symbol. However, if
482 Voverlay_arrow_position is a marker, last-arrow-position is its
483 numerical position. */
484
485 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
486
487 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
488 properties on a symbol in overlay-arrow-variable-list. */
489
490 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
491
492 Lisp_Object Qmenu_bar_update_hook;
493
494 /* Nonzero if an overlay arrow has been displayed in this window. */
495
496 static int overlay_arrow_seen;
497
498 /* Vector containing glyphs for an ellipsis `...'. */
499
500 static Lisp_Object default_invis_vector[3];
501
502 /* This is the window where the echo area message was displayed. It
503 is always a mini-buffer window, but it may not be the same window
504 currently active as a mini-buffer. */
505
506 Lisp_Object echo_area_window;
507
508 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
509 pushes the current message and the value of
510 message_enable_multibyte on the stack, the function restore_message
511 pops the stack and displays MESSAGE again. */
512
513 static Lisp_Object Vmessage_stack;
514
515 /* Nonzero means multibyte characters were enabled when the echo area
516 message was specified. */
517
518 static int message_enable_multibyte;
519
520 /* Nonzero if we should redraw the mode lines on the next redisplay. */
521
522 int update_mode_lines;
523
524 /* Nonzero if window sizes or contents have changed since last
525 redisplay that finished. */
526
527 int windows_or_buffers_changed;
528
529 /* Nonzero means a frame's cursor type has been changed. */
530
531 static int cursor_type_changed;
532
533 /* Nonzero after display_mode_line if %l was used and it displayed a
534 line number. */
535
536 static int line_number_displayed;
537
538 /* The name of the *Messages* buffer, a string. */
539
540 static Lisp_Object Vmessages_buffer_name;
541
542 /* Current, index 0, and last displayed echo area message. Either
543 buffers from echo_buffers, or nil to indicate no message. */
544
545 Lisp_Object echo_area_buffer[2];
546
547 /* The buffers referenced from echo_area_buffer. */
548
549 static Lisp_Object echo_buffer[2];
550
551 /* A vector saved used in with_area_buffer to reduce consing. */
552
553 static Lisp_Object Vwith_echo_area_save_vector;
554
555 /* Non-zero means display_echo_area should display the last echo area
556 message again. Set by redisplay_preserve_echo_area. */
557
558 static int display_last_displayed_message_p;
559
560 /* Nonzero if echo area is being used by print; zero if being used by
561 message. */
562
563 static int message_buf_print;
564
565 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
566
567 static Lisp_Object Qinhibit_menubar_update;
568 static Lisp_Object Qmessage_truncate_lines;
569
570 /* Set to 1 in clear_message to make redisplay_internal aware
571 of an emptied echo area. */
572
573 static int message_cleared_p;
574
575 /* A scratch glyph row with contents used for generating truncation
576 glyphs. Also used in direct_output_for_insert. */
577
578 #define MAX_SCRATCH_GLYPHS 100
579 static struct glyph_row scratch_glyph_row;
580 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
581
582 /* Ascent and height of the last line processed by move_it_to. */
583
584 static int last_height;
585
586 /* Non-zero if there's a help-echo in the echo area. */
587
588 int help_echo_showing_p;
589
590 /* If >= 0, computed, exact values of mode-line and header-line height
591 to use in the macros CURRENT_MODE_LINE_HEIGHT and
592 CURRENT_HEADER_LINE_HEIGHT. */
593
594 int current_mode_line_height, current_header_line_height;
595
596 /* The maximum distance to look ahead for text properties. Values
597 that are too small let us call compute_char_face and similar
598 functions too often which is expensive. Values that are too large
599 let us call compute_char_face and alike too often because we
600 might not be interested in text properties that far away. */
601
602 #define TEXT_PROP_DISTANCE_LIMIT 100
603
604 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
605 iterator state and later restore it. This is needed because the
606 bidi iterator on bidi.c keeps a stacked cache of its states, which
607 is really a singleton. When we use scratch iterator objects to
608 move around the buffer, we can cause the bidi cache to be pushed or
609 popped, and therefore we need to restore the cache state when we
610 return to the original iterator. */
611 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
612 do { \
613 if (CACHE) \
614 bidi_unshelve_cache (CACHE, 1); \
615 ITCOPY = ITORIG; \
616 CACHE = bidi_shelve_cache (); \
617 } while (0)
618
619 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
620 do { \
621 if (pITORIG != pITCOPY) \
622 *(pITORIG) = *(pITCOPY); \
623 bidi_unshelve_cache (CACHE, 0); \
624 CACHE = NULL; \
625 } while (0)
626
627 #ifdef GLYPH_DEBUG
628
629 /* Non-zero means print traces of redisplay if compiled with
630 GLYPH_DEBUG defined. */
631
632 int trace_redisplay_p;
633
634 #endif /* GLYPH_DEBUG */
635
636 #ifdef DEBUG_TRACE_MOVE
637 /* Non-zero means trace with TRACE_MOVE to stderr. */
638 int trace_move;
639
640 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
641 #else
642 #define TRACE_MOVE(x) (void) 0
643 #endif
644
645 static Lisp_Object Qauto_hscroll_mode;
646
647 /* Buffer being redisplayed -- for redisplay_window_error. */
648
649 static struct buffer *displayed_buffer;
650
651 /* Value returned from text property handlers (see below). */
652
653 enum prop_handled
654 {
655 HANDLED_NORMALLY,
656 HANDLED_RECOMPUTE_PROPS,
657 HANDLED_OVERLAY_STRING_CONSUMED,
658 HANDLED_RETURN
659 };
660
661 /* A description of text properties that redisplay is interested
662 in. */
663
664 struct props
665 {
666 /* The name of the property. */
667 Lisp_Object *name;
668
669 /* A unique index for the property. */
670 enum prop_idx idx;
671
672 /* A handler function called to set up iterator IT from the property
673 at IT's current position. Value is used to steer handle_stop. */
674 enum prop_handled (*handler) (struct it *it);
675 };
676
677 static enum prop_handled handle_face_prop (struct it *);
678 static enum prop_handled handle_invisible_prop (struct it *);
679 static enum prop_handled handle_display_prop (struct it *);
680 static enum prop_handled handle_composition_prop (struct it *);
681 static enum prop_handled handle_overlay_change (struct it *);
682 static enum prop_handled handle_fontified_prop (struct it *);
683
684 /* Properties handled by iterators. */
685
686 static struct props it_props[] =
687 {
688 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
689 /* Handle `face' before `display' because some sub-properties of
690 `display' need to know the face. */
691 {&Qface, FACE_PROP_IDX, handle_face_prop},
692 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
693 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
694 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
695 {NULL, 0, NULL}
696 };
697
698 /* Value is the position described by X. If X is a marker, value is
699 the marker_position of X. Otherwise, value is X. */
700
701 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
702
703 /* Enumeration returned by some move_it_.* functions internally. */
704
705 enum move_it_result
706 {
707 /* Not used. Undefined value. */
708 MOVE_UNDEFINED,
709
710 /* Move ended at the requested buffer position or ZV. */
711 MOVE_POS_MATCH_OR_ZV,
712
713 /* Move ended at the requested X pixel position. */
714 MOVE_X_REACHED,
715
716 /* Move within a line ended at the end of a line that must be
717 continued. */
718 MOVE_LINE_CONTINUED,
719
720 /* Move within a line ended at the end of a line that would
721 be displayed truncated. */
722 MOVE_LINE_TRUNCATED,
723
724 /* Move within a line ended at a line end. */
725 MOVE_NEWLINE_OR_CR
726 };
727
728 /* This counter is used to clear the face cache every once in a while
729 in redisplay_internal. It is incremented for each redisplay.
730 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
731 cleared. */
732
733 #define CLEAR_FACE_CACHE_COUNT 500
734 static int clear_face_cache_count;
735
736 /* Similarly for the image cache. */
737
738 #ifdef HAVE_WINDOW_SYSTEM
739 #define CLEAR_IMAGE_CACHE_COUNT 101
740 static int clear_image_cache_count;
741
742 /* Null glyph slice */
743 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
744 #endif
745
746 /* True while redisplay_internal is in progress. */
747
748 bool redisplaying_p;
749
750 static Lisp_Object Qinhibit_free_realized_faces;
751 static Lisp_Object Qmode_line_default_help_echo;
752
753 /* If a string, XTread_socket generates an event to display that string.
754 (The display is done in read_char.) */
755
756 Lisp_Object help_echo_string;
757 Lisp_Object help_echo_window;
758 Lisp_Object help_echo_object;
759 ptrdiff_t help_echo_pos;
760
761 /* Temporary variable for XTread_socket. */
762
763 Lisp_Object previous_help_echo_string;
764
765 /* Platform-independent portion of hourglass implementation. */
766
767 /* Non-zero means an hourglass cursor is currently shown. */
768 int hourglass_shown_p;
769
770 /* If non-null, an asynchronous timer that, when it expires, displays
771 an hourglass cursor on all frames. */
772 struct atimer *hourglass_atimer;
773
774 /* Name of the face used to display glyphless characters. */
775 Lisp_Object Qglyphless_char;
776
777 /* Symbol for the purpose of Vglyphless_char_display. */
778 static Lisp_Object Qglyphless_char_display;
779
780 /* Method symbols for Vglyphless_char_display. */
781 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
782
783 /* Default pixel width of `thin-space' display method. */
784 #define THIN_SPACE_WIDTH 1
785
786 /* Default number of seconds to wait before displaying an hourglass
787 cursor. */
788 #define DEFAULT_HOURGLASS_DELAY 1
789
790 \f
791 /* Function prototypes. */
792
793 static void setup_for_ellipsis (struct it *, int);
794 static void set_iterator_to_next (struct it *, int);
795 static void mark_window_display_accurate_1 (struct window *, int);
796 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
797 static int display_prop_string_p (Lisp_Object, Lisp_Object);
798 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
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 int text_outside_line_unchanged_p (struct window *,
812 ptrdiff_t, ptrdiff_t);
813 static void store_mode_line_noprop_char (char);
814 static int store_mode_line_noprop (const char *, int, int);
815 static void handle_stop (struct it *);
816 static void handle_stop_backwards (struct it *, ptrdiff_t);
817 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
818 static void ensure_echo_area_buffers (void);
819 static void unwind_with_echo_area_buffer (Lisp_Object);
820 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
821 static int with_echo_area_buffer (struct window *, int,
822 int (*) (ptrdiff_t, Lisp_Object),
823 ptrdiff_t, Lisp_Object);
824 static void clear_garbaged_frames (void);
825 static int current_message_1 (ptrdiff_t, Lisp_Object);
826 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
827 static void set_message (Lisp_Object);
828 static int set_message_1 (ptrdiff_t, Lisp_Object);
829 static int display_echo_area (struct window *);
830 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
831 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
832 static void unwind_redisplay (void);
833 static int string_char_and_length (const unsigned char *, int *);
834 static struct text_pos display_prop_end (struct it *, Lisp_Object,
835 struct text_pos);
836 static int compute_window_start_on_continuation_line (struct window *);
837 static void insert_left_trunc_glyphs (struct it *);
838 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
839 Lisp_Object);
840 static void extend_face_to_end_of_line (struct it *);
841 static int append_space_for_newline (struct it *, int);
842 static int cursor_row_fully_visible_p (struct window *, int, int);
843 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
844 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
845 static int trailing_whitespace_p (ptrdiff_t);
846 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
847 static void push_it (struct it *, struct text_pos *);
848 static void iterate_out_of_display_property (struct it *);
849 static void pop_it (struct it *);
850 static void sync_frame_with_window_matrix_rows (struct window *);
851 static void redisplay_internal (void);
852 static int echo_area_display (int);
853 static void redisplay_windows (Lisp_Object);
854 static void redisplay_window (Lisp_Object, int);
855 static Lisp_Object redisplay_window_error (Lisp_Object);
856 static Lisp_Object redisplay_window_0 (Lisp_Object);
857 static Lisp_Object redisplay_window_1 (Lisp_Object);
858 static int set_cursor_from_row (struct window *, struct glyph_row *,
859 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
860 int, int);
861 static int update_menu_bar (struct frame *, int, int);
862 static int try_window_reusing_current_matrix (struct window *);
863 static int try_window_id (struct window *);
864 static int display_line (struct it *);
865 static int display_mode_lines (struct window *);
866 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
867 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
868 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
869 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
870 static void display_menu_bar (struct window *);
871 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
872 ptrdiff_t *);
873 static int display_string (const char *, Lisp_Object, Lisp_Object,
874 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
875 static void compute_line_metrics (struct it *);
876 static void run_redisplay_end_trigger_hook (struct it *);
877 static int get_overlay_strings (struct it *, ptrdiff_t);
878 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
879 static void next_overlay_string (struct it *);
880 static void reseat (struct it *, struct text_pos, int);
881 static void reseat_1 (struct it *, struct text_pos, int);
882 static void back_to_previous_visible_line_start (struct it *);
883 static void reseat_at_next_visible_line_start (struct it *, int);
884 static int next_element_from_ellipsis (struct it *);
885 static int next_element_from_display_vector (struct it *);
886 static int next_element_from_string (struct it *);
887 static int next_element_from_c_string (struct it *);
888 static int next_element_from_buffer (struct it *);
889 static int next_element_from_composition (struct it *);
890 static int next_element_from_image (struct it *);
891 #ifdef HAVE_XWIDGETS
892 static int next_element_from_xwidget(struct it *);
893 #endif
894 static int next_element_from_stretch (struct it *);
895 static void load_overlay_strings (struct it *, ptrdiff_t);
896 static int init_from_display_pos (struct it *, struct window *,
897 struct display_pos *);
898 static void reseat_to_string (struct it *, const char *,
899 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
900 static int get_next_display_element (struct it *);
901 static enum move_it_result
902 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
903 enum move_operation_enum);
904 static void get_visually_first_element (struct it *);
905 static void init_to_row_start (struct it *, struct window *,
906 struct glyph_row *);
907 static int init_to_row_end (struct it *, struct window *,
908 struct glyph_row *);
909 static void back_to_previous_line_start (struct it *);
910 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
911 static struct text_pos string_pos_nchars_ahead (struct text_pos,
912 Lisp_Object, ptrdiff_t);
913 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
914 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
915 static ptrdiff_t number_of_chars (const char *, bool);
916 static void compute_stop_pos (struct it *);
917 static void compute_string_pos (struct text_pos *, struct text_pos,
918 Lisp_Object);
919 static int face_before_or_after_it_pos (struct it *, int);
920 static ptrdiff_t next_overlay_change (ptrdiff_t);
921 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
922 Lisp_Object, struct text_pos *, ptrdiff_t, int);
923 static int handle_single_display_spec (struct it *, Lisp_Object,
924 Lisp_Object, Lisp_Object,
925 struct text_pos *, ptrdiff_t, int, int);
926 static int underlying_face_id (struct it *);
927 static int in_ellipses_for_invisible_text_p (struct display_pos *,
928 struct window *);
929
930 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
931 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
932
933 #ifdef HAVE_WINDOW_SYSTEM
934
935 static void x_consider_frame_title (Lisp_Object);
936 static int tool_bar_lines_needed (struct frame *, int *);
937 static void update_tool_bar (struct frame *, int);
938 static void build_desired_tool_bar_string (struct frame *f);
939 static int redisplay_tool_bar (struct frame *);
940 static void display_tool_bar_line (struct it *, int);
941 static void notice_overwritten_cursor (struct window *,
942 enum glyph_row_area,
943 int, int, int, int);
944 static void append_stretch_glyph (struct it *, Lisp_Object,
945 int, int, int);
946
947
948 #endif /* HAVE_WINDOW_SYSTEM */
949
950 static void produce_special_glyphs (struct it *, enum display_element_type);
951 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
952 static int coords_in_mouse_face_p (struct window *, int, int);
953
954
955 \f
956 /***********************************************************************
957 Window display dimensions
958 ***********************************************************************/
959
960 /* Return the bottom boundary y-position for text lines in window W.
961 This is the first y position at which a line cannot start.
962 It is relative to the top of the window.
963
964 This is the height of W minus the height of a mode line, if any. */
965
966 int
967 window_text_bottom_y (struct window *w)
968 {
969 int height = WINDOW_TOTAL_HEIGHT (w);
970
971 if (WINDOW_WANTS_MODELINE_P (w))
972 height -= CURRENT_MODE_LINE_HEIGHT (w);
973 return height;
974 }
975
976 /* Return the pixel width of display area AREA of window W. AREA < 0
977 means return the total width of W, not including fringes to
978 the left and right of the window. */
979
980 int
981 window_box_width (struct window *w, int area)
982 {
983 int cols = w->total_cols;
984 int pixels = 0;
985
986 if (!w->pseudo_window_p)
987 {
988 cols -= WINDOW_SCROLL_BAR_COLS (w);
989
990 if (area == TEXT_AREA)
991 {
992 cols -= max (0, w->left_margin_cols);
993 cols -= max (0, w->right_margin_cols);
994 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
995 }
996 else if (area == LEFT_MARGIN_AREA)
997 {
998 cols = max (0, w->left_margin_cols);
999 pixels = 0;
1000 }
1001 else if (area == RIGHT_MARGIN_AREA)
1002 {
1003 cols = max (0, w->right_margin_cols);
1004 pixels = 0;
1005 }
1006 }
1007
1008 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1009 }
1010
1011
1012 /* Return the pixel height of the display area of window W, not
1013 including mode lines of W, if any. */
1014
1015 int
1016 window_box_height (struct window *w)
1017 {
1018 struct frame *f = XFRAME (w->frame);
1019 int height = WINDOW_TOTAL_HEIGHT (w);
1020
1021 eassert (height >= 0);
1022
1023 /* Note: the code below that determines the mode-line/header-line
1024 height is essentially the same as that contained in the macro
1025 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1026 the appropriate glyph row has its `mode_line_p' flag set,
1027 and if it doesn't, uses estimate_mode_line_height instead. */
1028
1029 if (WINDOW_WANTS_MODELINE_P (w))
1030 {
1031 struct glyph_row *ml_row
1032 = (w->current_matrix && w->current_matrix->rows
1033 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1034 : 0);
1035 if (ml_row && ml_row->mode_line_p)
1036 height -= ml_row->height;
1037 else
1038 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1039 }
1040
1041 if (WINDOW_WANTS_HEADER_LINE_P (w))
1042 {
1043 struct glyph_row *hl_row
1044 = (w->current_matrix && w->current_matrix->rows
1045 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1046 : 0);
1047 if (hl_row && hl_row->mode_line_p)
1048 height -= hl_row->height;
1049 else
1050 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1051 }
1052
1053 /* With a very small font and a mode-line that's taller than
1054 default, we might end up with a negative height. */
1055 return max (0, height);
1056 }
1057
1058 /* Return the window-relative coordinate of the left edge of display
1059 area AREA of window W. AREA < 0 means return the left edge of the
1060 whole window, to the right of the left fringe of W. */
1061
1062 int
1063 window_box_left_offset (struct window *w, int area)
1064 {
1065 int x;
1066
1067 if (w->pseudo_window_p)
1068 return 0;
1069
1070 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1071
1072 if (area == TEXT_AREA)
1073 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1074 + window_box_width (w, LEFT_MARGIN_AREA));
1075 else if (area == RIGHT_MARGIN_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA)
1078 + window_box_width (w, TEXT_AREA)
1079 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1080 ? 0
1081 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1082 else if (area == LEFT_MARGIN_AREA
1083 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1084 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1085
1086 return x;
1087 }
1088
1089
1090 /* Return the window-relative coordinate of the right edge of display
1091 area AREA of window W. AREA < 0 means return the right edge of the
1092 whole window, to the left of the right fringe of W. */
1093
1094 int
1095 window_box_right_offset (struct window *w, int area)
1096 {
1097 return window_box_left_offset (w, area) + window_box_width (w, area);
1098 }
1099
1100 /* Return the frame-relative coordinate of the left edge of display
1101 area AREA of window W. AREA < 0 means return the left edge of the
1102 whole window, to the right of the left fringe of W. */
1103
1104 int
1105 window_box_left (struct window *w, int area)
1106 {
1107 struct frame *f = XFRAME (w->frame);
1108 int x;
1109
1110 if (w->pseudo_window_p)
1111 return FRAME_INTERNAL_BORDER_WIDTH (f);
1112
1113 x = (WINDOW_LEFT_EDGE_X (w)
1114 + window_box_left_offset (w, area));
1115
1116 return x;
1117 }
1118
1119
1120 /* Return the frame-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the right edge of the
1122 whole window, to the left of the right fringe of W. */
1123
1124 int
1125 window_box_right (struct window *w, int area)
1126 {
1127 return window_box_left (w, area) + window_box_width (w, area);
1128 }
1129
1130 /* Get the bounding box of the display area AREA of window W, without
1131 mode lines, in frame-relative coordinates. AREA < 0 means the
1132 whole window, not including the left and right fringes of
1133 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1134 coordinates of the upper-left corner of the box. Return in
1135 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1136
1137 void
1138 window_box (struct window *w, int area, int *box_x, int *box_y,
1139 int *box_width, int *box_height)
1140 {
1141 if (box_width)
1142 *box_width = window_box_width (w, area);
1143 if (box_height)
1144 *box_height = window_box_height (w);
1145 if (box_x)
1146 *box_x = window_box_left (w, area);
1147 if (box_y)
1148 {
1149 *box_y = WINDOW_TOP_EDGE_Y (w);
1150 if (WINDOW_WANTS_HEADER_LINE_P (w))
1151 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1152 }
1153 }
1154
1155
1156 /* Get the bounding box of the display area AREA of window W, without
1157 mode lines. AREA < 0 means the whole window, not including the
1158 left and right fringe of the window. Return in *TOP_LEFT_X
1159 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1160 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1161 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1162 box. */
1163
1164 static void
1165 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1166 int *bottom_right_x, int *bottom_right_y)
1167 {
1168 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1169 bottom_right_y);
1170 *bottom_right_x += *top_left_x;
1171 *bottom_right_y += *top_left_y;
1172 }
1173
1174
1175 \f
1176 /***********************************************************************
1177 Utilities
1178 ***********************************************************************/
1179
1180 /* Return the bottom y-position of the line the iterator IT is in.
1181 This can modify IT's settings. */
1182
1183 int
1184 line_bottom_y (struct it *it)
1185 {
1186 int line_height = it->max_ascent + it->max_descent;
1187 int line_top_y = it->current_y;
1188
1189 if (line_height == 0)
1190 {
1191 if (last_height)
1192 line_height = last_height;
1193 else if (IT_CHARPOS (*it) < ZV)
1194 {
1195 move_it_by_lines (it, 1);
1196 line_height = (it->max_ascent || it->max_descent
1197 ? it->max_ascent + it->max_descent
1198 : last_height);
1199 }
1200 else
1201 {
1202 struct glyph_row *row = it->glyph_row;
1203
1204 /* Use the default character height. */
1205 it->glyph_row = NULL;
1206 it->what = IT_CHARACTER;
1207 it->c = ' ';
1208 it->len = 1;
1209 PRODUCE_GLYPHS (it);
1210 line_height = it->ascent + it->descent;
1211 it->glyph_row = row;
1212 }
1213 }
1214
1215 return line_top_y + line_height;
1216 }
1217
1218 DEFUN ("line-pixel-height", Fline_pixel_height,
1219 Sline_pixel_height, 0, 0, 0,
1220 doc: /* Return height in pixels of text line in the selected window.
1221
1222 Value is the height in pixels of the line at point. */)
1223 (void)
1224 {
1225 struct it it;
1226 struct text_pos pt;
1227 struct window *w = XWINDOW (selected_window);
1228
1229 SET_TEXT_POS (pt, PT, PT_BYTE);
1230 start_display (&it, w, pt);
1231 it.vpos = it.current_y = 0;
1232 last_height = 0;
1233 return make_number (line_bottom_y (&it));
1234 }
1235
1236 /* Return the default pixel height of text lines in window W. The
1237 value is the canonical height of the W frame's default font, plus
1238 any extra space required by the line-spacing variable or frame
1239 parameter.
1240
1241 Implementation note: this ignores any line-spacing text properties
1242 put on the newline characters. This is because those properties
1243 only affect the _screen_ line ending in the newline (i.e., in a
1244 continued line, only the last screen line will be affected), which
1245 means only a small number of lines in a buffer can ever use this
1246 feature. Since this function is used to compute the default pixel
1247 equivalent of text lines in a window, we can safely ignore those
1248 few lines. For the same reasons, we ignore the line-height
1249 properties. */
1250 int
1251 default_line_pixel_height (struct window *w)
1252 {
1253 struct frame *f = WINDOW_XFRAME (w);
1254 int height = FRAME_LINE_HEIGHT (f);
1255
1256 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1257 {
1258 struct buffer *b = XBUFFER (w->contents);
1259 Lisp_Object val = BVAR (b, extra_line_spacing);
1260
1261 if (NILP (val))
1262 val = BVAR (&buffer_defaults, extra_line_spacing);
1263 if (!NILP (val))
1264 {
1265 if (RANGED_INTEGERP (0, val, INT_MAX))
1266 height += XFASTINT (val);
1267 else if (FLOATP (val))
1268 {
1269 int addon = XFLOAT_DATA (val) * height + 0.5;
1270
1271 if (addon >= 0)
1272 height += addon;
1273 }
1274 }
1275 else
1276 height += f->extra_line_spacing;
1277 }
1278
1279 return height;
1280 }
1281
1282 /* Subroutine of pos_visible_p below. Extracts a display string, if
1283 any, from the display spec given as its argument. */
1284 static Lisp_Object
1285 string_from_display_spec (Lisp_Object spec)
1286 {
1287 if (CONSP (spec))
1288 {
1289 while (CONSP (spec))
1290 {
1291 if (STRINGP (XCAR (spec)))
1292 return XCAR (spec);
1293 spec = XCDR (spec);
1294 }
1295 }
1296 else if (VECTORP (spec))
1297 {
1298 ptrdiff_t i;
1299
1300 for (i = 0; i < ASIZE (spec); i++)
1301 {
1302 if (STRINGP (AREF (spec, i)))
1303 return AREF (spec, i);
1304 }
1305 return Qnil;
1306 }
1307
1308 return spec;
1309 }
1310
1311
1312 /* Limit insanely large values of W->hscroll on frame F to the largest
1313 value that will still prevent first_visible_x and last_visible_x of
1314 'struct it' from overflowing an int. */
1315 static int
1316 window_hscroll_limited (struct window *w, struct frame *f)
1317 {
1318 ptrdiff_t window_hscroll = w->hscroll;
1319 int window_text_width = window_box_width (w, TEXT_AREA);
1320 int colwidth = FRAME_COLUMN_WIDTH (f);
1321
1322 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1323 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1324
1325 return window_hscroll;
1326 }
1327
1328 /* Return 1 if position CHARPOS is visible in window W.
1329 CHARPOS < 0 means return info about WINDOW_END position.
1330 If visible, set *X and *Y to pixel coordinates of top left corner.
1331 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1332 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1333
1334 int
1335 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1336 int *rtop, int *rbot, int *rowh, int *vpos)
1337 {
1338 struct it it;
1339 void *itdata = bidi_shelve_cache ();
1340 struct text_pos top;
1341 int visible_p = 0;
1342 struct buffer *old_buffer = NULL;
1343
1344 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1345 return visible_p;
1346
1347 if (XBUFFER (w->contents) != current_buffer)
1348 {
1349 old_buffer = current_buffer;
1350 set_buffer_internal_1 (XBUFFER (w->contents));
1351 }
1352
1353 SET_TEXT_POS_FROM_MARKER (top, w->start);
1354 /* Scrolling a minibuffer window via scroll bar when the echo area
1355 shows long text sometimes resets the minibuffer contents behind
1356 our backs. */
1357 if (CHARPOS (top) > ZV)
1358 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1359
1360 /* Compute exact mode line heights. */
1361 if (WINDOW_WANTS_MODELINE_P (w))
1362 current_mode_line_height
1363 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1364 BVAR (current_buffer, mode_line_format));
1365
1366 if (WINDOW_WANTS_HEADER_LINE_P (w))
1367 current_header_line_height
1368 = display_mode_line (w, HEADER_LINE_FACE_ID,
1369 BVAR (current_buffer, header_line_format));
1370
1371 start_display (&it, w, top);
1372 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1373 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1374
1375 if (charpos >= 0
1376 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1377 && IT_CHARPOS (it) >= charpos)
1378 /* When scanning backwards under bidi iteration, move_it_to
1379 stops at or _before_ CHARPOS, because it stops at or to
1380 the _right_ of the character at CHARPOS. */
1381 || (it.bidi_p && it.bidi_it.scan_dir == -1
1382 && IT_CHARPOS (it) <= charpos)))
1383 {
1384 /* We have reached CHARPOS, or passed it. How the call to
1385 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1386 or covered by a display property, move_it_to stops at the end
1387 of the invisible text, to the right of CHARPOS. (ii) If
1388 CHARPOS is in a display vector, move_it_to stops on its last
1389 glyph. */
1390 int top_x = it.current_x;
1391 int top_y = it.current_y;
1392 /* Calling line_bottom_y may change it.method, it.position, etc. */
1393 enum it_method it_method = it.method;
1394 int bottom_y = (last_height = 0, line_bottom_y (&it));
1395 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1396
1397 if (top_y < window_top_y)
1398 visible_p = bottom_y > window_top_y;
1399 else if (top_y < it.last_visible_y)
1400 visible_p = 1;
1401 if (bottom_y >= it.last_visible_y
1402 && it.bidi_p && it.bidi_it.scan_dir == -1
1403 && IT_CHARPOS (it) < charpos)
1404 {
1405 /* When the last line of the window is scanned backwards
1406 under bidi iteration, we could be duped into thinking
1407 that we have passed CHARPOS, when in fact move_it_to
1408 simply stopped short of CHARPOS because it reached
1409 last_visible_y. To see if that's what happened, we call
1410 move_it_to again with a slightly larger vertical limit,
1411 and see if it actually moved vertically; if it did, we
1412 didn't really reach CHARPOS, which is beyond window end. */
1413 struct it save_it = it;
1414 /* Why 10? because we don't know how many canonical lines
1415 will the height of the next line(s) be. So we guess. */
1416 int ten_more_lines = 10 * default_line_pixel_height (w);
1417
1418 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1419 MOVE_TO_POS | MOVE_TO_Y);
1420 if (it.current_y > top_y)
1421 visible_p = 0;
1422
1423 it = save_it;
1424 }
1425 if (visible_p)
1426 {
1427 if (it_method == GET_FROM_DISPLAY_VECTOR)
1428 {
1429 /* We stopped on the last glyph of a display vector.
1430 Try and recompute. Hack alert! */
1431 if (charpos < 2 || top.charpos >= charpos)
1432 top_x = it.glyph_row->x;
1433 else
1434 {
1435 struct it it2, it2_prev;
1436 /* The idea is to get to the previous buffer
1437 position, consume the character there, and use
1438 the pixel coordinates we get after that. But if
1439 the previous buffer position is also displayed
1440 from a display vector, we need to consume all of
1441 the glyphs from that display vector. */
1442 start_display (&it2, w, top);
1443 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1444 /* If we didn't get to CHARPOS - 1, there's some
1445 replacing display property at that position, and
1446 we stopped after it. That is exactly the place
1447 whose coordinates we want. */
1448 if (IT_CHARPOS (it2) != charpos - 1)
1449 it2_prev = it2;
1450 else
1451 {
1452 /* Iterate until we get out of the display
1453 vector that displays the character at
1454 CHARPOS - 1. */
1455 do {
1456 get_next_display_element (&it2);
1457 PRODUCE_GLYPHS (&it2);
1458 it2_prev = it2;
1459 set_iterator_to_next (&it2, 1);
1460 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1461 && IT_CHARPOS (it2) < charpos);
1462 }
1463 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1464 || it2_prev.current_x > it2_prev.last_visible_x)
1465 top_x = it.glyph_row->x;
1466 else
1467 {
1468 top_x = it2_prev.current_x;
1469 top_y = it2_prev.current_y;
1470 }
1471 }
1472 }
1473 else if (IT_CHARPOS (it) != charpos)
1474 {
1475 Lisp_Object cpos = make_number (charpos);
1476 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1477 Lisp_Object string = string_from_display_spec (spec);
1478 struct text_pos tpos;
1479 int replacing_spec_p;
1480 bool newline_in_string
1481 = (STRINGP (string)
1482 && memchr (SDATA (string), '\n', SBYTES (string)));
1483
1484 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1485 replacing_spec_p
1486 = (!NILP (spec)
1487 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1488 charpos, FRAME_WINDOW_P (it.f)));
1489 /* The tricky code below is needed because there's a
1490 discrepancy between move_it_to and how we set cursor
1491 when PT is at the beginning of a portion of text
1492 covered by a display property or an overlay with a
1493 display property, or the display line ends in a
1494 newline from a display string. move_it_to will stop
1495 _after_ such display strings, whereas
1496 set_cursor_from_row conspires with cursor_row_p to
1497 place the cursor on the first glyph produced from the
1498 display string. */
1499
1500 /* We have overshoot PT because it is covered by a
1501 display property that replaces the text it covers.
1502 If the string includes embedded newlines, we are also
1503 in the wrong display line. Backtrack to the correct
1504 line, where the display property begins. */
1505 if (replacing_spec_p)
1506 {
1507 Lisp_Object startpos, endpos;
1508 EMACS_INT start, end;
1509 struct it it3;
1510 int it3_moved;
1511
1512 /* Find the first and the last buffer positions
1513 covered by the display string. */
1514 endpos =
1515 Fnext_single_char_property_change (cpos, Qdisplay,
1516 Qnil, Qnil);
1517 startpos =
1518 Fprevious_single_char_property_change (endpos, Qdisplay,
1519 Qnil, Qnil);
1520 start = XFASTINT (startpos);
1521 end = XFASTINT (endpos);
1522 /* Move to the last buffer position before the
1523 display property. */
1524 start_display (&it3, w, top);
1525 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1526 /* Move forward one more line if the position before
1527 the display string is a newline or if it is the
1528 rightmost character on a line that is
1529 continued or word-wrapped. */
1530 if (it3.method == GET_FROM_BUFFER
1531 && (it3.c == '\n'
1532 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1533 move_it_by_lines (&it3, 1);
1534 else if (move_it_in_display_line_to (&it3, -1,
1535 it3.current_x
1536 + it3.pixel_width,
1537 MOVE_TO_X)
1538 == MOVE_LINE_CONTINUED)
1539 {
1540 move_it_by_lines (&it3, 1);
1541 /* When we are under word-wrap, the #$@%!
1542 move_it_by_lines moves 2 lines, so we need to
1543 fix that up. */
1544 if (it3.line_wrap == WORD_WRAP)
1545 move_it_by_lines (&it3, -1);
1546 }
1547
1548 /* Record the vertical coordinate of the display
1549 line where we wound up. */
1550 top_y = it3.current_y;
1551 if (it3.bidi_p)
1552 {
1553 /* When characters are reordered for display,
1554 the character displayed to the left of the
1555 display string could be _after_ the display
1556 property in the logical order. Use the
1557 smallest vertical position of these two. */
1558 start_display (&it3, w, top);
1559 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1560 if (it3.current_y < top_y)
1561 top_y = it3.current_y;
1562 }
1563 /* Move from the top of the window to the beginning
1564 of the display line where the display string
1565 begins. */
1566 start_display (&it3, w, top);
1567 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1568 /* If it3_moved stays zero after the 'while' loop
1569 below, that means we already were at a newline
1570 before the loop (e.g., the display string begins
1571 with a newline), so we don't need to (and cannot)
1572 inspect the glyphs of it3.glyph_row, because
1573 PRODUCE_GLYPHS will not produce anything for a
1574 newline, and thus it3.glyph_row stays at its
1575 stale content it got at top of the window. */
1576 it3_moved = 0;
1577 /* Finally, advance the iterator until we hit the
1578 first display element whose character position is
1579 CHARPOS, or until the first newline from the
1580 display string, which signals the end of the
1581 display line. */
1582 while (get_next_display_element (&it3))
1583 {
1584 PRODUCE_GLYPHS (&it3);
1585 if (IT_CHARPOS (it3) == charpos
1586 || ITERATOR_AT_END_OF_LINE_P (&it3))
1587 break;
1588 it3_moved = 1;
1589 set_iterator_to_next (&it3, 0);
1590 }
1591 top_x = it3.current_x - it3.pixel_width;
1592 /* Normally, we would exit the above loop because we
1593 found the display element whose character
1594 position is CHARPOS. For the contingency that we
1595 didn't, and stopped at the first newline from the
1596 display string, move back over the glyphs
1597 produced from the string, until we find the
1598 rightmost glyph not from the string. */
1599 if (it3_moved
1600 && newline_in_string
1601 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1602 {
1603 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1604 + it3.glyph_row->used[TEXT_AREA];
1605
1606 while (EQ ((g - 1)->object, string))
1607 {
1608 --g;
1609 top_x -= g->pixel_width;
1610 }
1611 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1612 + it3.glyph_row->used[TEXT_AREA]);
1613 }
1614 }
1615 }
1616
1617 *x = top_x;
1618 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1619 *rtop = max (0, window_top_y - top_y);
1620 *rbot = max (0, bottom_y - it.last_visible_y);
1621 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1622 - max (top_y, window_top_y)));
1623 *vpos = it.vpos;
1624 }
1625 }
1626 else
1627 {
1628 /* We were asked to provide info about WINDOW_END. */
1629 struct it it2;
1630 void *it2data = NULL;
1631
1632 SAVE_IT (it2, it, it2data);
1633 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1634 move_it_by_lines (&it, 1);
1635 if (charpos < IT_CHARPOS (it)
1636 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1637 {
1638 visible_p = 1;
1639 RESTORE_IT (&it2, &it2, it2data);
1640 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1641 *x = it2.current_x;
1642 *y = it2.current_y + it2.max_ascent - it2.ascent;
1643 *rtop = max (0, -it2.current_y);
1644 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1645 - it.last_visible_y));
1646 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1647 it.last_visible_y)
1648 - max (it2.current_y,
1649 WINDOW_HEADER_LINE_HEIGHT (w))));
1650 *vpos = it2.vpos;
1651 }
1652 else
1653 bidi_unshelve_cache (it2data, 1);
1654 }
1655 bidi_unshelve_cache (itdata, 0);
1656
1657 if (old_buffer)
1658 set_buffer_internal_1 (old_buffer);
1659
1660 current_header_line_height = current_mode_line_height = -1;
1661
1662 if (visible_p && w->hscroll > 0)
1663 *x -=
1664 window_hscroll_limited (w, WINDOW_XFRAME (w))
1665 * WINDOW_FRAME_COLUMN_WIDTH (w);
1666
1667 #if 0
1668 /* Debugging code. */
1669 if (visible_p)
1670 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1671 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1672 else
1673 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1674 #endif
1675
1676 return visible_p;
1677 }
1678
1679
1680 /* Return the next character from STR. Return in *LEN the length of
1681 the character. This is like STRING_CHAR_AND_LENGTH but never
1682 returns an invalid character. If we find one, we return a `?', but
1683 with the length of the invalid character. */
1684
1685 static int
1686 string_char_and_length (const unsigned char *str, int *len)
1687 {
1688 int c;
1689
1690 c = STRING_CHAR_AND_LENGTH (str, *len);
1691 if (!CHAR_VALID_P (c))
1692 /* We may not change the length here because other places in Emacs
1693 don't use this function, i.e. they silently accept invalid
1694 characters. */
1695 c = '?';
1696
1697 return c;
1698 }
1699
1700
1701
1702 /* Given a position POS containing a valid character and byte position
1703 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1704
1705 static struct text_pos
1706 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1707 {
1708 eassert (STRINGP (string) && nchars >= 0);
1709
1710 if (STRING_MULTIBYTE (string))
1711 {
1712 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1713 int len;
1714
1715 while (nchars--)
1716 {
1717 string_char_and_length (p, &len);
1718 p += len;
1719 CHARPOS (pos) += 1;
1720 BYTEPOS (pos) += len;
1721 }
1722 }
1723 else
1724 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1725
1726 return pos;
1727 }
1728
1729
1730 /* Value is the text position, i.e. character and byte position,
1731 for character position CHARPOS in STRING. */
1732
1733 static struct text_pos
1734 string_pos (ptrdiff_t charpos, Lisp_Object string)
1735 {
1736 struct text_pos pos;
1737 eassert (STRINGP (string));
1738 eassert (charpos >= 0);
1739 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1740 return pos;
1741 }
1742
1743
1744 /* Value is a text position, i.e. character and byte position, for
1745 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1746 means recognize multibyte characters. */
1747
1748 static struct text_pos
1749 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1750 {
1751 struct text_pos pos;
1752
1753 eassert (s != NULL);
1754 eassert (charpos >= 0);
1755
1756 if (multibyte_p)
1757 {
1758 int len;
1759
1760 SET_TEXT_POS (pos, 0, 0);
1761 while (charpos--)
1762 {
1763 string_char_and_length ((const unsigned char *) s, &len);
1764 s += len;
1765 CHARPOS (pos) += 1;
1766 BYTEPOS (pos) += len;
1767 }
1768 }
1769 else
1770 SET_TEXT_POS (pos, charpos, charpos);
1771
1772 return pos;
1773 }
1774
1775
1776 /* Value is the number of characters in C string S. MULTIBYTE_P
1777 non-zero means recognize multibyte characters. */
1778
1779 static ptrdiff_t
1780 number_of_chars (const char *s, bool multibyte_p)
1781 {
1782 ptrdiff_t nchars;
1783
1784 if (multibyte_p)
1785 {
1786 ptrdiff_t rest = strlen (s);
1787 int len;
1788 const unsigned char *p = (const unsigned char *) s;
1789
1790 for (nchars = 0; rest > 0; ++nchars)
1791 {
1792 string_char_and_length (p, &len);
1793 rest -= len, p += len;
1794 }
1795 }
1796 else
1797 nchars = strlen (s);
1798
1799 return nchars;
1800 }
1801
1802
1803 /* Compute byte position NEWPOS->bytepos corresponding to
1804 NEWPOS->charpos. POS is a known position in string STRING.
1805 NEWPOS->charpos must be >= POS.charpos. */
1806
1807 static void
1808 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1809 {
1810 eassert (STRINGP (string));
1811 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1812
1813 if (STRING_MULTIBYTE (string))
1814 *newpos = string_pos_nchars_ahead (pos, string,
1815 CHARPOS (*newpos) - CHARPOS (pos));
1816 else
1817 BYTEPOS (*newpos) = CHARPOS (*newpos);
1818 }
1819
1820 /* EXPORT:
1821 Return an estimation of the pixel height of mode or header lines on
1822 frame F. FACE_ID specifies what line's height to estimate. */
1823
1824 int
1825 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1826 {
1827 #ifdef HAVE_WINDOW_SYSTEM
1828 if (FRAME_WINDOW_P (f))
1829 {
1830 int height = FONT_HEIGHT (FRAME_FONT (f));
1831
1832 /* This function is called so early when Emacs starts that the face
1833 cache and mode line face are not yet initialized. */
1834 if (FRAME_FACE_CACHE (f))
1835 {
1836 struct face *face = FACE_FROM_ID (f, face_id);
1837 if (face)
1838 {
1839 if (face->font)
1840 height = FONT_HEIGHT (face->font);
1841 if (face->box_line_width > 0)
1842 height += 2 * face->box_line_width;
1843 }
1844 }
1845
1846 return height;
1847 }
1848 #endif
1849
1850 return 1;
1851 }
1852
1853 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1854 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1855 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1856 not force the value into range. */
1857
1858 void
1859 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1860 int *x, int *y, NativeRectangle *bounds, int noclip)
1861 {
1862
1863 #ifdef HAVE_WINDOW_SYSTEM
1864 if (FRAME_WINDOW_P (f))
1865 {
1866 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1867 even for negative values. */
1868 if (pix_x < 0)
1869 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1870 if (pix_y < 0)
1871 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1872
1873 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1874 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1875
1876 if (bounds)
1877 STORE_NATIVE_RECT (*bounds,
1878 FRAME_COL_TO_PIXEL_X (f, pix_x),
1879 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1880 FRAME_COLUMN_WIDTH (f) - 1,
1881 FRAME_LINE_HEIGHT (f) - 1);
1882
1883 if (!noclip)
1884 {
1885 if (pix_x < 0)
1886 pix_x = 0;
1887 else if (pix_x > FRAME_TOTAL_COLS (f))
1888 pix_x = FRAME_TOTAL_COLS (f);
1889
1890 if (pix_y < 0)
1891 pix_y = 0;
1892 else if (pix_y > FRAME_LINES (f))
1893 pix_y = FRAME_LINES (f);
1894 }
1895 }
1896 #endif
1897
1898 *x = pix_x;
1899 *y = pix_y;
1900 }
1901
1902
1903 /* Find the glyph under window-relative coordinates X/Y in window W.
1904 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1905 strings. Return in *HPOS and *VPOS the row and column number of
1906 the glyph found. Return in *AREA the glyph area containing X.
1907 Value is a pointer to the glyph found or null if X/Y is not on
1908 text, or we can't tell because W's current matrix is not up to
1909 date. */
1910
1911 static
1912 struct glyph *
1913 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1914 int *dx, int *dy, int *area)
1915 {
1916 struct glyph *glyph, *end;
1917 struct glyph_row *row = NULL;
1918 int x0, i;
1919
1920 /* Find row containing Y. Give up if some row is not enabled. */
1921 for (i = 0; i < w->current_matrix->nrows; ++i)
1922 {
1923 row = MATRIX_ROW (w->current_matrix, i);
1924 if (!row->enabled_p)
1925 return NULL;
1926 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1927 break;
1928 }
1929
1930 *vpos = i;
1931 *hpos = 0;
1932
1933 /* Give up if Y is not in the window. */
1934 if (i == w->current_matrix->nrows)
1935 return NULL;
1936
1937 /* Get the glyph area containing X. */
1938 if (w->pseudo_window_p)
1939 {
1940 *area = TEXT_AREA;
1941 x0 = 0;
1942 }
1943 else
1944 {
1945 if (x < window_box_left_offset (w, TEXT_AREA))
1946 {
1947 *area = LEFT_MARGIN_AREA;
1948 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1949 }
1950 else if (x < window_box_right_offset (w, TEXT_AREA))
1951 {
1952 *area = TEXT_AREA;
1953 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1954 }
1955 else
1956 {
1957 *area = RIGHT_MARGIN_AREA;
1958 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1959 }
1960 }
1961
1962 /* Find glyph containing X. */
1963 glyph = row->glyphs[*area];
1964 end = glyph + row->used[*area];
1965 x -= x0;
1966 while (glyph < end && x >= glyph->pixel_width)
1967 {
1968 x -= glyph->pixel_width;
1969 ++glyph;
1970 }
1971
1972 if (glyph == end)
1973 return NULL;
1974
1975 if (dx)
1976 {
1977 *dx = x;
1978 *dy = y - (row->y + row->ascent - glyph->ascent);
1979 }
1980
1981 *hpos = glyph - row->glyphs[*area];
1982 return glyph;
1983 }
1984
1985 /* Convert frame-relative x/y to coordinates relative to window W.
1986 Takes pseudo-windows into account. */
1987
1988 static void
1989 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1990 {
1991 if (w->pseudo_window_p)
1992 {
1993 /* A pseudo-window is always full-width, and starts at the
1994 left edge of the frame, plus a frame border. */
1995 struct frame *f = XFRAME (w->frame);
1996 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1997 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1998 }
1999 else
2000 {
2001 *x -= WINDOW_LEFT_EDGE_X (w);
2002 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2003 }
2004 }
2005
2006 #ifdef HAVE_WINDOW_SYSTEM
2007
2008 /* EXPORT:
2009 Return in RECTS[] at most N clipping rectangles for glyph string S.
2010 Return the number of stored rectangles. */
2011
2012 int
2013 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2014 {
2015 XRectangle r;
2016
2017 if (n <= 0)
2018 return 0;
2019
2020 if (s->row->full_width_p)
2021 {
2022 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2023 r.x = WINDOW_LEFT_EDGE_X (s->w);
2024 r.width = WINDOW_TOTAL_WIDTH (s->w);
2025
2026 /* Unless displaying a mode or menu bar line, which are always
2027 fully visible, clip to the visible part of the row. */
2028 if (s->w->pseudo_window_p)
2029 r.height = s->row->visible_height;
2030 else
2031 r.height = s->height;
2032 }
2033 else
2034 {
2035 /* This is a text line that may be partially visible. */
2036 r.x = window_box_left (s->w, s->area);
2037 r.width = window_box_width (s->w, s->area);
2038 r.height = s->row->visible_height;
2039 }
2040
2041 if (s->clip_head)
2042 if (r.x < s->clip_head->x)
2043 {
2044 if (r.width >= s->clip_head->x - r.x)
2045 r.width -= s->clip_head->x - r.x;
2046 else
2047 r.width = 0;
2048 r.x = s->clip_head->x;
2049 }
2050 if (s->clip_tail)
2051 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2052 {
2053 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2054 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2055 else
2056 r.width = 0;
2057 }
2058
2059 /* If S draws overlapping rows, it's sufficient to use the top and
2060 bottom of the window for clipping because this glyph string
2061 intentionally draws over other lines. */
2062 if (s->for_overlaps)
2063 {
2064 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2065 r.height = window_text_bottom_y (s->w) - r.y;
2066
2067 /* Alas, the above simple strategy does not work for the
2068 environments with anti-aliased text: if the same text is
2069 drawn onto the same place multiple times, it gets thicker.
2070 If the overlap we are processing is for the erased cursor, we
2071 take the intersection with the rectangle of the cursor. */
2072 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2073 {
2074 XRectangle rc, r_save = r;
2075
2076 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2077 rc.y = s->w->phys_cursor.y;
2078 rc.width = s->w->phys_cursor_width;
2079 rc.height = s->w->phys_cursor_height;
2080
2081 x_intersect_rectangles (&r_save, &rc, &r);
2082 }
2083 }
2084 else
2085 {
2086 /* Don't use S->y for clipping because it doesn't take partially
2087 visible lines into account. For example, it can be negative for
2088 partially visible lines at the top of a window. */
2089 if (!s->row->full_width_p
2090 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2091 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2092 else
2093 r.y = max (0, s->row->y);
2094 }
2095
2096 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2097
2098 /* If drawing the cursor, don't let glyph draw outside its
2099 advertised boundaries. Cleartype does this under some circumstances. */
2100 if (s->hl == DRAW_CURSOR)
2101 {
2102 struct glyph *glyph = s->first_glyph;
2103 int height, max_y;
2104
2105 if (s->x > r.x)
2106 {
2107 r.width -= s->x - r.x;
2108 r.x = s->x;
2109 }
2110 r.width = min (r.width, glyph->pixel_width);
2111
2112 /* If r.y is below window bottom, ensure that we still see a cursor. */
2113 height = min (glyph->ascent + glyph->descent,
2114 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2115 max_y = window_text_bottom_y (s->w) - height;
2116 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2117 if (s->ybase - glyph->ascent > max_y)
2118 {
2119 r.y = max_y;
2120 r.height = height;
2121 }
2122 else
2123 {
2124 /* Don't draw cursor glyph taller than our actual glyph. */
2125 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2126 if (height < r.height)
2127 {
2128 max_y = r.y + r.height;
2129 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2130 r.height = min (max_y - r.y, height);
2131 }
2132 }
2133 }
2134
2135 if (s->row->clip)
2136 {
2137 XRectangle r_save = r;
2138
2139 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2140 r.width = 0;
2141 }
2142
2143 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2144 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2145 {
2146 #ifdef CONVERT_FROM_XRECT
2147 CONVERT_FROM_XRECT (r, *rects);
2148 #else
2149 *rects = r;
2150 #endif
2151 return 1;
2152 }
2153 else
2154 {
2155 /* If we are processing overlapping and allowed to return
2156 multiple clipping rectangles, we exclude the row of the glyph
2157 string from the clipping rectangle. This is to avoid drawing
2158 the same text on the environment with anti-aliasing. */
2159 #ifdef CONVERT_FROM_XRECT
2160 XRectangle rs[2];
2161 #else
2162 XRectangle *rs = rects;
2163 #endif
2164 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2165
2166 if (s->for_overlaps & OVERLAPS_PRED)
2167 {
2168 rs[i] = r;
2169 if (r.y + r.height > row_y)
2170 {
2171 if (r.y < row_y)
2172 rs[i].height = row_y - r.y;
2173 else
2174 rs[i].height = 0;
2175 }
2176 i++;
2177 }
2178 if (s->for_overlaps & OVERLAPS_SUCC)
2179 {
2180 rs[i] = r;
2181 if (r.y < row_y + s->row->visible_height)
2182 {
2183 if (r.y + r.height > row_y + s->row->visible_height)
2184 {
2185 rs[i].y = row_y + s->row->visible_height;
2186 rs[i].height = r.y + r.height - rs[i].y;
2187 }
2188 else
2189 rs[i].height = 0;
2190 }
2191 i++;
2192 }
2193
2194 n = i;
2195 #ifdef CONVERT_FROM_XRECT
2196 for (i = 0; i < n; i++)
2197 CONVERT_FROM_XRECT (rs[i], rects[i]);
2198 #endif
2199 return n;
2200 }
2201 }
2202
2203 /* EXPORT:
2204 Return in *NR the clipping rectangle for glyph string S. */
2205
2206 void
2207 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2208 {
2209 get_glyph_string_clip_rects (s, nr, 1);
2210 }
2211
2212
2213 /* EXPORT:
2214 Return the position and height of the phys cursor in window W.
2215 Set w->phys_cursor_width to width of phys cursor.
2216 */
2217
2218 void
2219 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2220 struct glyph *glyph, int *xp, int *yp, int *heightp)
2221 {
2222 struct frame *f = XFRAME (WINDOW_FRAME (w));
2223 int x, y, wd, h, h0, y0;
2224
2225 /* Compute the width of the rectangle to draw. If on a stretch
2226 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2227 rectangle as wide as the glyph, but use a canonical character
2228 width instead. */
2229 wd = glyph->pixel_width - 1;
2230 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2231 wd++; /* Why? */
2232 #endif
2233
2234 x = w->phys_cursor.x;
2235 if (x < 0)
2236 {
2237 wd += x;
2238 x = 0;
2239 }
2240
2241 if (glyph->type == STRETCH_GLYPH
2242 && !x_stretch_cursor_p)
2243 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2244 w->phys_cursor_width = wd;
2245
2246 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2247
2248 /* If y is below window bottom, ensure that we still see a cursor. */
2249 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2250
2251 h = max (h0, glyph->ascent + glyph->descent);
2252 h0 = min (h0, glyph->ascent + glyph->descent);
2253
2254 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2255 if (y < y0)
2256 {
2257 h = max (h - (y0 - y) + 1, h0);
2258 y = y0 - 1;
2259 }
2260 else
2261 {
2262 y0 = window_text_bottom_y (w) - h0;
2263 if (y > y0)
2264 {
2265 h += y - y0;
2266 y = y0;
2267 }
2268 }
2269
2270 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2271 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2272 *heightp = h;
2273 }
2274
2275 /*
2276 * Remember which glyph the mouse is over.
2277 */
2278
2279 void
2280 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2281 {
2282 Lisp_Object window;
2283 struct window *w;
2284 struct glyph_row *r, *gr, *end_row;
2285 enum window_part part;
2286 enum glyph_row_area area;
2287 int x, y, width, height;
2288
2289 /* Try to determine frame pixel position and size of the glyph under
2290 frame pixel coordinates X/Y on frame F. */
2291
2292 if (!f->glyphs_initialized_p
2293 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2294 NILP (window)))
2295 {
2296 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2297 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2298 goto virtual_glyph;
2299 }
2300
2301 w = XWINDOW (window);
2302 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2303 height = WINDOW_FRAME_LINE_HEIGHT (w);
2304
2305 x = window_relative_x_coord (w, part, gx);
2306 y = gy - WINDOW_TOP_EDGE_Y (w);
2307
2308 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2309 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2310
2311 if (w->pseudo_window_p)
2312 {
2313 area = TEXT_AREA;
2314 part = ON_MODE_LINE; /* Don't adjust margin. */
2315 goto text_glyph;
2316 }
2317
2318 switch (part)
2319 {
2320 case ON_LEFT_MARGIN:
2321 area = LEFT_MARGIN_AREA;
2322 goto text_glyph;
2323
2324 case ON_RIGHT_MARGIN:
2325 area = RIGHT_MARGIN_AREA;
2326 goto text_glyph;
2327
2328 case ON_HEADER_LINE:
2329 case ON_MODE_LINE:
2330 gr = (part == ON_HEADER_LINE
2331 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2332 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2333 gy = gr->y;
2334 area = TEXT_AREA;
2335 goto text_glyph_row_found;
2336
2337 case ON_TEXT:
2338 area = TEXT_AREA;
2339
2340 text_glyph:
2341 gr = 0; gy = 0;
2342 for (; r <= end_row && r->enabled_p; ++r)
2343 if (r->y + r->height > y)
2344 {
2345 gr = r; gy = r->y;
2346 break;
2347 }
2348
2349 text_glyph_row_found:
2350 if (gr && gy <= y)
2351 {
2352 struct glyph *g = gr->glyphs[area];
2353 struct glyph *end = g + gr->used[area];
2354
2355 height = gr->height;
2356 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2357 if (gx + g->pixel_width > x)
2358 break;
2359
2360 if (g < end)
2361 {
2362 if (g->type == IMAGE_GLYPH)
2363 {
2364 /* Don't remember when mouse is over image, as
2365 image may have hot-spots. */
2366 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2367 return;
2368 }
2369 width = g->pixel_width;
2370 }
2371 else
2372 {
2373 /* Use nominal char spacing at end of line. */
2374 x -= gx;
2375 gx += (x / width) * width;
2376 }
2377
2378 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2379 gx += window_box_left_offset (w, area);
2380 }
2381 else
2382 {
2383 /* Use nominal line height at end of window. */
2384 gx = (x / width) * width;
2385 y -= gy;
2386 gy += (y / height) * height;
2387 }
2388 break;
2389
2390 case ON_LEFT_FRINGE:
2391 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2392 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2393 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2394 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2395 goto row_glyph;
2396
2397 case ON_RIGHT_FRINGE:
2398 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2399 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2400 : window_box_right_offset (w, TEXT_AREA));
2401 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2402 goto row_glyph;
2403
2404 case ON_SCROLL_BAR:
2405 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2406 ? 0
2407 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2408 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2409 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2410 : 0)));
2411 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2412
2413 row_glyph:
2414 gr = 0, gy = 0;
2415 for (; r <= end_row && r->enabled_p; ++r)
2416 if (r->y + r->height > y)
2417 {
2418 gr = r; gy = r->y;
2419 break;
2420 }
2421
2422 if (gr && gy <= y)
2423 height = gr->height;
2424 else
2425 {
2426 /* Use nominal line height at end of window. */
2427 y -= gy;
2428 gy += (y / height) * height;
2429 }
2430 break;
2431
2432 default:
2433 ;
2434 virtual_glyph:
2435 /* If there is no glyph under the mouse, then we divide the screen
2436 into a grid of the smallest glyph in the frame, and use that
2437 as our "glyph". */
2438
2439 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2440 round down even for negative values. */
2441 if (gx < 0)
2442 gx -= width - 1;
2443 if (gy < 0)
2444 gy -= height - 1;
2445
2446 gx = (gx / width) * width;
2447 gy = (gy / height) * height;
2448
2449 goto store_rect;
2450 }
2451
2452 gx += WINDOW_LEFT_EDGE_X (w);
2453 gy += WINDOW_TOP_EDGE_Y (w);
2454
2455 store_rect:
2456 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2457
2458 /* Visible feedback for debugging. */
2459 #if 0
2460 #if HAVE_X_WINDOWS
2461 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2462 f->output_data.x->normal_gc,
2463 gx, gy, width, height);
2464 #endif
2465 #endif
2466 }
2467
2468
2469 #endif /* HAVE_WINDOW_SYSTEM */
2470
2471 static void
2472 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2473 {
2474 eassert (w);
2475 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2476 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2477 w->window_end_vpos
2478 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2479 }
2480
2481 /***********************************************************************
2482 Lisp form evaluation
2483 ***********************************************************************/
2484
2485 /* Error handler for safe_eval and safe_call. */
2486
2487 static Lisp_Object
2488 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2489 {
2490 add_to_log ("Error during redisplay: %S signaled %S",
2491 Flist (nargs, args), arg);
2492 return Qnil;
2493 }
2494
2495 /* Call function FUNC with the rest of NARGS - 1 arguments
2496 following. Return the result, or nil if something went
2497 wrong. Prevent redisplay during the evaluation. */
2498
2499 Lisp_Object
2500 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2501 {
2502 Lisp_Object val;
2503
2504 if (inhibit_eval_during_redisplay)
2505 val = Qnil;
2506 else
2507 {
2508 va_list ap;
2509 ptrdiff_t i;
2510 ptrdiff_t count = SPECPDL_INDEX ();
2511 struct gcpro gcpro1;
2512 Lisp_Object *args = alloca (nargs * word_size);
2513
2514 args[0] = func;
2515 va_start (ap, func);
2516 for (i = 1; i < nargs; i++)
2517 args[i] = va_arg (ap, Lisp_Object);
2518 va_end (ap);
2519
2520 GCPRO1 (args[0]);
2521 gcpro1.nvars = nargs;
2522 specbind (Qinhibit_redisplay, Qt);
2523 /* Use Qt to ensure debugger does not run,
2524 so there is no possibility of wanting to redisplay. */
2525 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2526 safe_eval_handler);
2527 UNGCPRO;
2528 val = unbind_to (count, val);
2529 }
2530
2531 return val;
2532 }
2533
2534
2535 /* Call function FN with one argument ARG.
2536 Return the result, or nil if something went wrong. */
2537
2538 Lisp_Object
2539 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2540 {
2541 return safe_call (2, fn, arg);
2542 }
2543
2544 static Lisp_Object Qeval;
2545
2546 Lisp_Object
2547 safe_eval (Lisp_Object sexpr)
2548 {
2549 return safe_call1 (Qeval, sexpr);
2550 }
2551
2552 /* Call function FN with two arguments ARG1 and ARG2.
2553 Return the result, or nil if something went wrong. */
2554
2555 Lisp_Object
2556 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2557 {
2558 return safe_call (3, fn, arg1, arg2);
2559 }
2560
2561
2562 \f
2563 /***********************************************************************
2564 Debugging
2565 ***********************************************************************/
2566
2567 #if 0
2568
2569 /* Define CHECK_IT to perform sanity checks on iterators.
2570 This is for debugging. It is too slow to do unconditionally. */
2571
2572 static void
2573 check_it (struct it *it)
2574 {
2575 if (it->method == GET_FROM_STRING)
2576 {
2577 eassert (STRINGP (it->string));
2578 eassert (IT_STRING_CHARPOS (*it) >= 0);
2579 }
2580 else
2581 {
2582 eassert (IT_STRING_CHARPOS (*it) < 0);
2583 if (it->method == GET_FROM_BUFFER)
2584 {
2585 /* Check that character and byte positions agree. */
2586 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2587 }
2588 }
2589
2590 if (it->dpvec)
2591 eassert (it->current.dpvec_index >= 0);
2592 else
2593 eassert (it->current.dpvec_index < 0);
2594 }
2595
2596 #define CHECK_IT(IT) check_it ((IT))
2597
2598 #else /* not 0 */
2599
2600 #define CHECK_IT(IT) (void) 0
2601
2602 #endif /* not 0 */
2603
2604
2605 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2606
2607 /* Check that the window end of window W is what we expect it
2608 to be---the last row in the current matrix displaying text. */
2609
2610 static void
2611 check_window_end (struct window *w)
2612 {
2613 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2614 {
2615 struct glyph_row *row;
2616 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2617 !row->enabled_p
2618 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2619 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2620 }
2621 }
2622
2623 #define CHECK_WINDOW_END(W) check_window_end ((W))
2624
2625 #else
2626
2627 #define CHECK_WINDOW_END(W) (void) 0
2628
2629 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2630
2631 /* Return mark position if current buffer has the region of non-zero length,
2632 or -1 otherwise. */
2633
2634 static ptrdiff_t
2635 markpos_of_region (void)
2636 {
2637 if (!NILP (Vtransient_mark_mode)
2638 && !NILP (BVAR (current_buffer, mark_active))
2639 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2640 {
2641 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2642
2643 if (markpos != PT)
2644 return markpos;
2645 }
2646 return -1;
2647 }
2648
2649 /***********************************************************************
2650 Iterator initialization
2651 ***********************************************************************/
2652
2653 /* Initialize IT for displaying current_buffer in window W, starting
2654 at character position CHARPOS. CHARPOS < 0 means that no buffer
2655 position is specified which is useful when the iterator is assigned
2656 a position later. BYTEPOS is the byte position corresponding to
2657 CHARPOS.
2658
2659 If ROW is not null, calls to produce_glyphs with IT as parameter
2660 will produce glyphs in that row.
2661
2662 BASE_FACE_ID is the id of a base face to use. It must be one of
2663 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2664 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2665 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2666
2667 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2668 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2669 will be initialized to use the corresponding mode line glyph row of
2670 the desired matrix of W. */
2671
2672 void
2673 init_iterator (struct it *it, struct window *w,
2674 ptrdiff_t charpos, ptrdiff_t bytepos,
2675 struct glyph_row *row, enum face_id base_face_id)
2676 {
2677 ptrdiff_t markpos;
2678 enum face_id remapped_base_face_id = base_face_id;
2679
2680 /* Some precondition checks. */
2681 eassert (w != NULL && it != NULL);
2682 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2683 && charpos <= ZV));
2684
2685 /* If face attributes have been changed since the last redisplay,
2686 free realized faces now because they depend on face definitions
2687 that might have changed. Don't free faces while there might be
2688 desired matrices pending which reference these faces. */
2689 if (face_change_count && !inhibit_free_realized_faces)
2690 {
2691 face_change_count = 0;
2692 free_all_realized_faces (Qnil);
2693 }
2694
2695 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2696 if (! NILP (Vface_remapping_alist))
2697 remapped_base_face_id
2698 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2699
2700 /* Use one of the mode line rows of W's desired matrix if
2701 appropriate. */
2702 if (row == NULL)
2703 {
2704 if (base_face_id == MODE_LINE_FACE_ID
2705 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2706 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2707 else if (base_face_id == HEADER_LINE_FACE_ID)
2708 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2709 }
2710
2711 /* Clear IT. */
2712 memset (it, 0, sizeof *it);
2713 it->current.overlay_string_index = -1;
2714 it->current.dpvec_index = -1;
2715 it->base_face_id = remapped_base_face_id;
2716 it->string = Qnil;
2717 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2718 it->paragraph_embedding = L2R;
2719 it->bidi_it.string.lstring = Qnil;
2720 it->bidi_it.string.s = NULL;
2721 it->bidi_it.string.bufpos = 0;
2722 it->bidi_it.w = w;
2723
2724 /* The window in which we iterate over current_buffer: */
2725 XSETWINDOW (it->window, w);
2726 it->w = w;
2727 it->f = XFRAME (w->frame);
2728
2729 it->cmp_it.id = -1;
2730
2731 /* Extra space between lines (on window systems only). */
2732 if (base_face_id == DEFAULT_FACE_ID
2733 && FRAME_WINDOW_P (it->f))
2734 {
2735 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2736 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2737 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2738 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2739 * FRAME_LINE_HEIGHT (it->f));
2740 else if (it->f->extra_line_spacing > 0)
2741 it->extra_line_spacing = it->f->extra_line_spacing;
2742 it->max_extra_line_spacing = 0;
2743 }
2744
2745 /* If realized faces have been removed, e.g. because of face
2746 attribute changes of named faces, recompute them. When running
2747 in batch mode, the face cache of the initial frame is null. If
2748 we happen to get called, make a dummy face cache. */
2749 if (FRAME_FACE_CACHE (it->f) == NULL)
2750 init_frame_faces (it->f);
2751 if (FRAME_FACE_CACHE (it->f)->used == 0)
2752 recompute_basic_faces (it->f);
2753
2754 /* Current value of the `slice', `space-width', and 'height' properties. */
2755 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2756 it->space_width = Qnil;
2757 it->font_height = Qnil;
2758 it->override_ascent = -1;
2759
2760 /* Are control characters displayed as `^C'? */
2761 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2762
2763 /* -1 means everything between a CR and the following line end
2764 is invisible. >0 means lines indented more than this value are
2765 invisible. */
2766 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2767 ? (clip_to_bounds
2768 (-1, XINT (BVAR (current_buffer, selective_display)),
2769 PTRDIFF_MAX))
2770 : (!NILP (BVAR (current_buffer, selective_display))
2771 ? -1 : 0));
2772 it->selective_display_ellipsis_p
2773 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2774
2775 /* Display table to use. */
2776 it->dp = window_display_table (w);
2777
2778 /* Are multibyte characters enabled in current_buffer? */
2779 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2780
2781 /* If visible region is of non-zero length, set IT->region_beg_charpos
2782 and IT->region_end_charpos to the start and end of a visible region
2783 in window IT->w. Set both to -1 to indicate no region. */
2784 markpos = markpos_of_region ();
2785 if (markpos >= 0
2786 /* Maybe highlight only in selected window. */
2787 && (/* Either show region everywhere. */
2788 highlight_nonselected_windows
2789 /* Or show region in the selected window. */
2790 || w == XWINDOW (selected_window)
2791 /* Or show the region if we are in the mini-buffer and W is
2792 the window the mini-buffer refers to. */
2793 || (MINI_WINDOW_P (XWINDOW (selected_window))
2794 && WINDOWP (minibuf_selected_window)
2795 && w == XWINDOW (minibuf_selected_window))))
2796 {
2797 it->region_beg_charpos = min (PT, markpos);
2798 it->region_end_charpos = max (PT, markpos);
2799 }
2800 else
2801 it->region_beg_charpos = it->region_end_charpos = -1;
2802
2803 /* Get the position at which the redisplay_end_trigger hook should
2804 be run, if it is to be run at all. */
2805 if (MARKERP (w->redisplay_end_trigger)
2806 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2807 it->redisplay_end_trigger_charpos
2808 = marker_position (w->redisplay_end_trigger);
2809 else if (INTEGERP (w->redisplay_end_trigger))
2810 it->redisplay_end_trigger_charpos =
2811 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2812
2813 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2814
2815 /* Are lines in the display truncated? */
2816 if (base_face_id != DEFAULT_FACE_ID
2817 || it->w->hscroll
2818 || (! WINDOW_FULL_WIDTH_P (it->w)
2819 && ((!NILP (Vtruncate_partial_width_windows)
2820 && !INTEGERP (Vtruncate_partial_width_windows))
2821 || (INTEGERP (Vtruncate_partial_width_windows)
2822 && (WINDOW_TOTAL_COLS (it->w)
2823 < XINT (Vtruncate_partial_width_windows))))))
2824 it->line_wrap = TRUNCATE;
2825 else if (NILP (BVAR (current_buffer, truncate_lines)))
2826 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2827 ? WINDOW_WRAP : WORD_WRAP;
2828 else
2829 it->line_wrap = TRUNCATE;
2830
2831 /* Get dimensions of truncation and continuation glyphs. These are
2832 displayed as fringe bitmaps under X, but we need them for such
2833 frames when the fringes are turned off. But leave the dimensions
2834 zero for tooltip frames, as these glyphs look ugly there and also
2835 sabotage calculations of tooltip dimensions in x-show-tip. */
2836 #ifdef HAVE_WINDOW_SYSTEM
2837 if (!(FRAME_WINDOW_P (it->f)
2838 && FRAMEP (tip_frame)
2839 && it->f == XFRAME (tip_frame)))
2840 #endif
2841 {
2842 if (it->line_wrap == TRUNCATE)
2843 {
2844 /* We will need the truncation glyph. */
2845 eassert (it->glyph_row == NULL);
2846 produce_special_glyphs (it, IT_TRUNCATION);
2847 it->truncation_pixel_width = it->pixel_width;
2848 }
2849 else
2850 {
2851 /* We will need the continuation glyph. */
2852 eassert (it->glyph_row == NULL);
2853 produce_special_glyphs (it, IT_CONTINUATION);
2854 it->continuation_pixel_width = it->pixel_width;
2855 }
2856 }
2857
2858 /* Reset these values to zero because the produce_special_glyphs
2859 above has changed them. */
2860 it->pixel_width = it->ascent = it->descent = 0;
2861 it->phys_ascent = it->phys_descent = 0;
2862
2863 /* Set this after getting the dimensions of truncation and
2864 continuation glyphs, so that we don't produce glyphs when calling
2865 produce_special_glyphs, above. */
2866 it->glyph_row = row;
2867 it->area = TEXT_AREA;
2868
2869 /* Forget any previous info about this row being reversed. */
2870 if (it->glyph_row)
2871 it->glyph_row->reversed_p = 0;
2872
2873 /* Get the dimensions of the display area. The display area
2874 consists of the visible window area plus a horizontally scrolled
2875 part to the left of the window. All x-values are relative to the
2876 start of this total display area. */
2877 if (base_face_id != DEFAULT_FACE_ID)
2878 {
2879 /* Mode lines, menu bar in terminal frames. */
2880 it->first_visible_x = 0;
2881 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2882 }
2883 else
2884 {
2885 it->first_visible_x =
2886 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2887 it->last_visible_x = (it->first_visible_x
2888 + window_box_width (w, TEXT_AREA));
2889
2890 /* If we truncate lines, leave room for the truncation glyph(s) at
2891 the right margin. Otherwise, leave room for the continuation
2892 glyph(s). Done only if the window has no fringes. Since we
2893 don't know at this point whether there will be any R2L lines in
2894 the window, we reserve space for truncation/continuation glyphs
2895 even if only one of the fringes is absent. */
2896 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2897 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2898 {
2899 if (it->line_wrap == TRUNCATE)
2900 it->last_visible_x -= it->truncation_pixel_width;
2901 else
2902 it->last_visible_x -= it->continuation_pixel_width;
2903 }
2904
2905 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2906 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2907 }
2908
2909 /* Leave room for a border glyph. */
2910 if (!FRAME_WINDOW_P (it->f)
2911 && !WINDOW_RIGHTMOST_P (it->w))
2912 it->last_visible_x -= 1;
2913
2914 it->last_visible_y = window_text_bottom_y (w);
2915
2916 /* For mode lines and alike, arrange for the first glyph having a
2917 left box line if the face specifies a box. */
2918 if (base_face_id != DEFAULT_FACE_ID)
2919 {
2920 struct face *face;
2921
2922 it->face_id = remapped_base_face_id;
2923
2924 /* If we have a boxed mode line, make the first character appear
2925 with a left box line. */
2926 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2927 if (face->box != FACE_NO_BOX)
2928 it->start_of_box_run_p = 1;
2929 }
2930
2931 /* If a buffer position was specified, set the iterator there,
2932 getting overlays and face properties from that position. */
2933 if (charpos >= BUF_BEG (current_buffer))
2934 {
2935 it->end_charpos = ZV;
2936 eassert (charpos == BYTE_TO_CHAR (bytepos));
2937 IT_CHARPOS (*it) = charpos;
2938 IT_BYTEPOS (*it) = bytepos;
2939
2940 /* We will rely on `reseat' to set this up properly, via
2941 handle_face_prop. */
2942 it->face_id = it->base_face_id;
2943
2944 it->start = it->current;
2945 /* Do we need to reorder bidirectional text? Not if this is a
2946 unibyte buffer: by definition, none of the single-byte
2947 characters are strong R2L, so no reordering is needed. And
2948 bidi.c doesn't support unibyte buffers anyway. Also, don't
2949 reorder while we are loading loadup.el, since the tables of
2950 character properties needed for reordering are not yet
2951 available. */
2952 it->bidi_p =
2953 NILP (Vpurify_flag)
2954 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2955 && it->multibyte_p;
2956
2957 /* If we are to reorder bidirectional text, init the bidi
2958 iterator. */
2959 if (it->bidi_p)
2960 {
2961 /* Note the paragraph direction that this buffer wants to
2962 use. */
2963 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2964 Qleft_to_right))
2965 it->paragraph_embedding = L2R;
2966 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2967 Qright_to_left))
2968 it->paragraph_embedding = R2L;
2969 else
2970 it->paragraph_embedding = NEUTRAL_DIR;
2971 bidi_unshelve_cache (NULL, 0);
2972 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2973 &it->bidi_it);
2974 }
2975
2976 /* Compute faces etc. */
2977 reseat (it, it->current.pos, 1);
2978 }
2979
2980 CHECK_IT (it);
2981 }
2982
2983
2984 /* Initialize IT for the display of window W with window start POS. */
2985
2986 void
2987 start_display (struct it *it, struct window *w, struct text_pos pos)
2988 {
2989 struct glyph_row *row;
2990 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2991
2992 row = w->desired_matrix->rows + first_vpos;
2993 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2994 it->first_vpos = first_vpos;
2995
2996 /* Don't reseat to previous visible line start if current start
2997 position is in a string or image. */
2998 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2999 {
3000 int start_at_line_beg_p;
3001 int first_y = it->current_y;
3002
3003 /* If window start is not at a line start, skip forward to POS to
3004 get the correct continuation lines width. */
3005 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3006 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3007 if (!start_at_line_beg_p)
3008 {
3009 int new_x;
3010
3011 reseat_at_previous_visible_line_start (it);
3012 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3013
3014 new_x = it->current_x + it->pixel_width;
3015
3016 /* If lines are continued, this line may end in the middle
3017 of a multi-glyph character (e.g. a control character
3018 displayed as \003, or in the middle of an overlay
3019 string). In this case move_it_to above will not have
3020 taken us to the start of the continuation line but to the
3021 end of the continued line. */
3022 if (it->current_x > 0
3023 && it->line_wrap != TRUNCATE /* Lines are continued. */
3024 && (/* And glyph doesn't fit on the line. */
3025 new_x > it->last_visible_x
3026 /* Or it fits exactly and we're on a window
3027 system frame. */
3028 || (new_x == it->last_visible_x
3029 && FRAME_WINDOW_P (it->f)
3030 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3031 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3032 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3033 {
3034 if ((it->current.dpvec_index >= 0
3035 || it->current.overlay_string_index >= 0)
3036 /* If we are on a newline from a display vector or
3037 overlay string, then we are already at the end of
3038 a screen line; no need to go to the next line in
3039 that case, as this line is not really continued.
3040 (If we do go to the next line, C-e will not DTRT.) */
3041 && it->c != '\n')
3042 {
3043 set_iterator_to_next (it, 1);
3044 move_it_in_display_line_to (it, -1, -1, 0);
3045 }
3046
3047 it->continuation_lines_width += it->current_x;
3048 }
3049 /* If the character at POS is displayed via a display
3050 vector, move_it_to above stops at the final glyph of
3051 IT->dpvec. To make the caller redisplay that character
3052 again (a.k.a. start at POS), we need to reset the
3053 dpvec_index to the beginning of IT->dpvec. */
3054 else if (it->current.dpvec_index >= 0)
3055 it->current.dpvec_index = 0;
3056
3057 /* We're starting a new display line, not affected by the
3058 height of the continued line, so clear the appropriate
3059 fields in the iterator structure. */
3060 it->max_ascent = it->max_descent = 0;
3061 it->max_phys_ascent = it->max_phys_descent = 0;
3062
3063 it->current_y = first_y;
3064 it->vpos = 0;
3065 it->current_x = it->hpos = 0;
3066 }
3067 }
3068 }
3069
3070
3071 /* Return 1 if POS is a position in ellipses displayed for invisible
3072 text. W is the window we display, for text property lookup. */
3073
3074 static int
3075 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3076 {
3077 Lisp_Object prop, window;
3078 int ellipses_p = 0;
3079 ptrdiff_t charpos = CHARPOS (pos->pos);
3080
3081 /* If POS specifies a position in a display vector, this might
3082 be for an ellipsis displayed for invisible text. We won't
3083 get the iterator set up for delivering that ellipsis unless
3084 we make sure that it gets aware of the invisible text. */
3085 if (pos->dpvec_index >= 0
3086 && pos->overlay_string_index < 0
3087 && CHARPOS (pos->string_pos) < 0
3088 && charpos > BEGV
3089 && (XSETWINDOW (window, w),
3090 prop = Fget_char_property (make_number (charpos),
3091 Qinvisible, window),
3092 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3093 {
3094 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3095 window);
3096 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3097 }
3098
3099 return ellipses_p;
3100 }
3101
3102
3103 /* Initialize IT for stepping through current_buffer in window W,
3104 starting at position POS that includes overlay string and display
3105 vector/ control character translation position information. Value
3106 is zero if there are overlay strings with newlines at POS. */
3107
3108 static int
3109 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3110 {
3111 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3112 int i, overlay_strings_with_newlines = 0;
3113
3114 /* If POS specifies a position in a display vector, this might
3115 be for an ellipsis displayed for invisible text. We won't
3116 get the iterator set up for delivering that ellipsis unless
3117 we make sure that it gets aware of the invisible text. */
3118 if (in_ellipses_for_invisible_text_p (pos, w))
3119 {
3120 --charpos;
3121 bytepos = 0;
3122 }
3123
3124 /* Keep in mind: the call to reseat in init_iterator skips invisible
3125 text, so we might end up at a position different from POS. This
3126 is only a problem when POS is a row start after a newline and an
3127 overlay starts there with an after-string, and the overlay has an
3128 invisible property. Since we don't skip invisible text in
3129 display_line and elsewhere immediately after consuming the
3130 newline before the row start, such a POS will not be in a string,
3131 but the call to init_iterator below will move us to the
3132 after-string. */
3133 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3134
3135 /* This only scans the current chunk -- it should scan all chunks.
3136 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3137 to 16 in 22.1 to make this a lesser problem. */
3138 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3139 {
3140 const char *s = SSDATA (it->overlay_strings[i]);
3141 const char *e = s + SBYTES (it->overlay_strings[i]);
3142
3143 while (s < e && *s != '\n')
3144 ++s;
3145
3146 if (s < e)
3147 {
3148 overlay_strings_with_newlines = 1;
3149 break;
3150 }
3151 }
3152
3153 /* If position is within an overlay string, set up IT to the right
3154 overlay string. */
3155 if (pos->overlay_string_index >= 0)
3156 {
3157 int relative_index;
3158
3159 /* If the first overlay string happens to have a `display'
3160 property for an image, the iterator will be set up for that
3161 image, and we have to undo that setup first before we can
3162 correct the overlay string index. */
3163 if (it->method == GET_FROM_IMAGE)
3164 pop_it (it);
3165
3166 /* We already have the first chunk of overlay strings in
3167 IT->overlay_strings. Load more until the one for
3168 pos->overlay_string_index is in IT->overlay_strings. */
3169 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3170 {
3171 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3172 it->current.overlay_string_index = 0;
3173 while (n--)
3174 {
3175 load_overlay_strings (it, 0);
3176 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3177 }
3178 }
3179
3180 it->current.overlay_string_index = pos->overlay_string_index;
3181 relative_index = (it->current.overlay_string_index
3182 % OVERLAY_STRING_CHUNK_SIZE);
3183 it->string = it->overlay_strings[relative_index];
3184 eassert (STRINGP (it->string));
3185 it->current.string_pos = pos->string_pos;
3186 it->method = GET_FROM_STRING;
3187 it->end_charpos = SCHARS (it->string);
3188 /* Set up the bidi iterator for this overlay string. */
3189 if (it->bidi_p)
3190 {
3191 it->bidi_it.string.lstring = it->string;
3192 it->bidi_it.string.s = NULL;
3193 it->bidi_it.string.schars = SCHARS (it->string);
3194 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3195 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3196 it->bidi_it.string.unibyte = !it->multibyte_p;
3197 it->bidi_it.w = it->w;
3198 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3199 FRAME_WINDOW_P (it->f), &it->bidi_it);
3200
3201 /* Synchronize the state of the bidi iterator with
3202 pos->string_pos. For any string position other than
3203 zero, this will be done automagically when we resume
3204 iteration over the string and get_visually_first_element
3205 is called. But if string_pos is zero, and the string is
3206 to be reordered for display, we need to resync manually,
3207 since it could be that the iteration state recorded in
3208 pos ended at string_pos of 0 moving backwards in string. */
3209 if (CHARPOS (pos->string_pos) == 0)
3210 {
3211 get_visually_first_element (it);
3212 if (IT_STRING_CHARPOS (*it) != 0)
3213 do {
3214 /* Paranoia. */
3215 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3216 bidi_move_to_visually_next (&it->bidi_it);
3217 } while (it->bidi_it.charpos != 0);
3218 }
3219 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3220 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3221 }
3222 }
3223
3224 if (CHARPOS (pos->string_pos) >= 0)
3225 {
3226 /* Recorded position is not in an overlay string, but in another
3227 string. This can only be a string from a `display' property.
3228 IT should already be filled with that string. */
3229 it->current.string_pos = pos->string_pos;
3230 eassert (STRINGP (it->string));
3231 if (it->bidi_p)
3232 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3233 FRAME_WINDOW_P (it->f), &it->bidi_it);
3234 }
3235
3236 /* Restore position in display vector translations, control
3237 character translations or ellipses. */
3238 if (pos->dpvec_index >= 0)
3239 {
3240 if (it->dpvec == NULL)
3241 get_next_display_element (it);
3242 eassert (it->dpvec && it->current.dpvec_index == 0);
3243 it->current.dpvec_index = pos->dpvec_index;
3244 }
3245
3246 CHECK_IT (it);
3247 return !overlay_strings_with_newlines;
3248 }
3249
3250
3251 /* Initialize IT for stepping through current_buffer in window W
3252 starting at ROW->start. */
3253
3254 static void
3255 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3256 {
3257 init_from_display_pos (it, w, &row->start);
3258 it->start = row->start;
3259 it->continuation_lines_width = row->continuation_lines_width;
3260 CHECK_IT (it);
3261 }
3262
3263
3264 /* Initialize IT for stepping through current_buffer in window W
3265 starting in the line following ROW, i.e. starting at ROW->end.
3266 Value is zero if there are overlay strings with newlines at ROW's
3267 end position. */
3268
3269 static int
3270 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3271 {
3272 int success = 0;
3273
3274 if (init_from_display_pos (it, w, &row->end))
3275 {
3276 if (row->continued_p)
3277 it->continuation_lines_width
3278 = row->continuation_lines_width + row->pixel_width;
3279 CHECK_IT (it);
3280 success = 1;
3281 }
3282
3283 return success;
3284 }
3285
3286
3287
3288 \f
3289 /***********************************************************************
3290 Text properties
3291 ***********************************************************************/
3292
3293 /* Called when IT reaches IT->stop_charpos. Handle text property and
3294 overlay changes. Set IT->stop_charpos to the next position where
3295 to stop. */
3296
3297 static void
3298 handle_stop (struct it *it)
3299 {
3300 enum prop_handled handled;
3301 int handle_overlay_change_p;
3302 struct props *p;
3303
3304 it->dpvec = NULL;
3305 it->current.dpvec_index = -1;
3306 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3307 it->ignore_overlay_strings_at_pos_p = 0;
3308 it->ellipsis_p = 0;
3309
3310 /* Use face of preceding text for ellipsis (if invisible) */
3311 if (it->selective_display_ellipsis_p)
3312 it->saved_face_id = it->face_id;
3313
3314 do
3315 {
3316 handled = HANDLED_NORMALLY;
3317
3318 /* Call text property handlers. */
3319 for (p = it_props; p->handler; ++p)
3320 {
3321 handled = p->handler (it);
3322
3323 if (handled == HANDLED_RECOMPUTE_PROPS)
3324 break;
3325 else if (handled == HANDLED_RETURN)
3326 {
3327 /* We still want to show before and after strings from
3328 overlays even if the actual buffer text is replaced. */
3329 if (!handle_overlay_change_p
3330 || it->sp > 1
3331 /* Don't call get_overlay_strings_1 if we already
3332 have overlay strings loaded, because doing so
3333 will load them again and push the iterator state
3334 onto the stack one more time, which is not
3335 expected by the rest of the code that processes
3336 overlay strings. */
3337 || (it->current.overlay_string_index < 0
3338 ? !get_overlay_strings_1 (it, 0, 0)
3339 : 0))
3340 {
3341 if (it->ellipsis_p)
3342 setup_for_ellipsis (it, 0);
3343 /* When handling a display spec, we might load an
3344 empty string. In that case, discard it here. We
3345 used to discard it in handle_single_display_spec,
3346 but that causes get_overlay_strings_1, above, to
3347 ignore overlay strings that we must check. */
3348 if (STRINGP (it->string) && !SCHARS (it->string))
3349 pop_it (it);
3350 return;
3351 }
3352 else if (STRINGP (it->string) && !SCHARS (it->string))
3353 pop_it (it);
3354 else
3355 {
3356 it->ignore_overlay_strings_at_pos_p = 1;
3357 it->string_from_display_prop_p = 0;
3358 it->from_disp_prop_p = 0;
3359 handle_overlay_change_p = 0;
3360 }
3361 handled = HANDLED_RECOMPUTE_PROPS;
3362 break;
3363 }
3364 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3365 handle_overlay_change_p = 0;
3366 }
3367
3368 if (handled != HANDLED_RECOMPUTE_PROPS)
3369 {
3370 /* Don't check for overlay strings below when set to deliver
3371 characters from a display vector. */
3372 if (it->method == GET_FROM_DISPLAY_VECTOR)
3373 handle_overlay_change_p = 0;
3374
3375 /* Handle overlay changes.
3376 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3377 if it finds overlays. */
3378 if (handle_overlay_change_p)
3379 handled = handle_overlay_change (it);
3380 }
3381
3382 if (it->ellipsis_p)
3383 {
3384 setup_for_ellipsis (it, 0);
3385 break;
3386 }
3387 }
3388 while (handled == HANDLED_RECOMPUTE_PROPS);
3389
3390 /* Determine where to stop next. */
3391 if (handled == HANDLED_NORMALLY)
3392 compute_stop_pos (it);
3393 }
3394
3395
3396 /* Compute IT->stop_charpos from text property and overlay change
3397 information for IT's current position. */
3398
3399 static void
3400 compute_stop_pos (struct it *it)
3401 {
3402 register INTERVAL iv, next_iv;
3403 Lisp_Object object, limit, position;
3404 ptrdiff_t charpos, bytepos;
3405
3406 if (STRINGP (it->string))
3407 {
3408 /* Strings are usually short, so don't limit the search for
3409 properties. */
3410 it->stop_charpos = it->end_charpos;
3411 object = it->string;
3412 limit = Qnil;
3413 charpos = IT_STRING_CHARPOS (*it);
3414 bytepos = IT_STRING_BYTEPOS (*it);
3415 }
3416 else
3417 {
3418 ptrdiff_t pos;
3419
3420 /* If end_charpos is out of range for some reason, such as a
3421 misbehaving display function, rationalize it (Bug#5984). */
3422 if (it->end_charpos > ZV)
3423 it->end_charpos = ZV;
3424 it->stop_charpos = it->end_charpos;
3425
3426 /* If next overlay change is in front of the current stop pos
3427 (which is IT->end_charpos), stop there. Note: value of
3428 next_overlay_change is point-max if no overlay change
3429 follows. */
3430 charpos = IT_CHARPOS (*it);
3431 bytepos = IT_BYTEPOS (*it);
3432 pos = next_overlay_change (charpos);
3433 if (pos < it->stop_charpos)
3434 it->stop_charpos = pos;
3435
3436 /* If showing the region, we have to stop at the region
3437 start or end because the face might change there. */
3438 if (it->region_beg_charpos > 0)
3439 {
3440 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3441 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3442 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3443 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3444 }
3445
3446 /* Set up variables for computing the stop position from text
3447 property changes. */
3448 XSETBUFFER (object, current_buffer);
3449 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3450 }
3451
3452 /* Get the interval containing IT's position. Value is a null
3453 interval if there isn't such an interval. */
3454 position = make_number (charpos);
3455 iv = validate_interval_range (object, &position, &position, 0);
3456 if (iv)
3457 {
3458 Lisp_Object values_here[LAST_PROP_IDX];
3459 struct props *p;
3460
3461 /* Get properties here. */
3462 for (p = it_props; p->handler; ++p)
3463 values_here[p->idx] = textget (iv->plist, *p->name);
3464
3465 /* Look for an interval following iv that has different
3466 properties. */
3467 for (next_iv = next_interval (iv);
3468 (next_iv
3469 && (NILP (limit)
3470 || XFASTINT (limit) > next_iv->position));
3471 next_iv = next_interval (next_iv))
3472 {
3473 for (p = it_props; p->handler; ++p)
3474 {
3475 Lisp_Object new_value;
3476
3477 new_value = textget (next_iv->plist, *p->name);
3478 if (!EQ (values_here[p->idx], new_value))
3479 break;
3480 }
3481
3482 if (p->handler)
3483 break;
3484 }
3485
3486 if (next_iv)
3487 {
3488 if (INTEGERP (limit)
3489 && next_iv->position >= XFASTINT (limit))
3490 /* No text property change up to limit. */
3491 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3492 else
3493 /* Text properties change in next_iv. */
3494 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3495 }
3496 }
3497
3498 if (it->cmp_it.id < 0)
3499 {
3500 ptrdiff_t stoppos = it->end_charpos;
3501
3502 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3503 stoppos = -1;
3504 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3505 stoppos, it->string);
3506 }
3507
3508 eassert (STRINGP (it->string)
3509 || (it->stop_charpos >= BEGV
3510 && it->stop_charpos >= IT_CHARPOS (*it)));
3511 }
3512
3513
3514 /* Return the position of the next overlay change after POS in
3515 current_buffer. Value is point-max if no overlay change
3516 follows. This is like `next-overlay-change' but doesn't use
3517 xmalloc. */
3518
3519 static ptrdiff_t
3520 next_overlay_change (ptrdiff_t pos)
3521 {
3522 ptrdiff_t i, noverlays;
3523 ptrdiff_t endpos;
3524 Lisp_Object *overlays;
3525
3526 /* Get all overlays at the given position. */
3527 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3528
3529 /* If any of these overlays ends before endpos,
3530 use its ending point instead. */
3531 for (i = 0; i < noverlays; ++i)
3532 {
3533 Lisp_Object oend;
3534 ptrdiff_t oendpos;
3535
3536 oend = OVERLAY_END (overlays[i]);
3537 oendpos = OVERLAY_POSITION (oend);
3538 endpos = min (endpos, oendpos);
3539 }
3540
3541 return endpos;
3542 }
3543
3544 /* How many characters forward to search for a display property or
3545 display string. Searching too far forward makes the bidi display
3546 sluggish, especially in small windows. */
3547 #define MAX_DISP_SCAN 250
3548
3549 /* Return the character position of a display string at or after
3550 position specified by POSITION. If no display string exists at or
3551 after POSITION, return ZV. A display string is either an overlay
3552 with `display' property whose value is a string, or a `display'
3553 text property whose value is a string. STRING is data about the
3554 string to iterate; if STRING->lstring is nil, we are iterating a
3555 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3556 on a GUI frame. DISP_PROP is set to zero if we searched
3557 MAX_DISP_SCAN characters forward without finding any display
3558 strings, non-zero otherwise. It is set to 2 if the display string
3559 uses any kind of `(space ...)' spec that will produce a stretch of
3560 white space in the text area. */
3561 ptrdiff_t
3562 compute_display_string_pos (struct text_pos *position,
3563 struct bidi_string_data *string,
3564 struct window *w,
3565 int frame_window_p, int *disp_prop)
3566 {
3567 /* OBJECT = nil means current buffer. */
3568 Lisp_Object object, object1;
3569 Lisp_Object pos, spec, limpos;
3570 int string_p = (string && (STRINGP (string->lstring) || string->s));
3571 ptrdiff_t eob = string_p ? string->schars : ZV;
3572 ptrdiff_t begb = string_p ? 0 : BEGV;
3573 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3574 ptrdiff_t lim =
3575 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3576 struct text_pos tpos;
3577 int rv = 0;
3578
3579 if (string && STRINGP (string->lstring))
3580 object1 = object = string->lstring;
3581 else if (w && !string_p)
3582 {
3583 XSETWINDOW (object, w);
3584 object1 = Qnil;
3585 }
3586 else
3587 object1 = object = Qnil;
3588
3589 *disp_prop = 1;
3590
3591 if (charpos >= eob
3592 /* We don't support display properties whose values are strings
3593 that have display string properties. */
3594 || string->from_disp_str
3595 /* C strings cannot have display properties. */
3596 || (string->s && !STRINGP (object)))
3597 {
3598 *disp_prop = 0;
3599 return eob;
3600 }
3601
3602 /* If the character at CHARPOS is where the display string begins,
3603 return CHARPOS. */
3604 pos = make_number (charpos);
3605 if (STRINGP (object))
3606 bufpos = string->bufpos;
3607 else
3608 bufpos = charpos;
3609 tpos = *position;
3610 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3611 && (charpos <= begb
3612 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3613 object),
3614 spec))
3615 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3616 frame_window_p)))
3617 {
3618 if (rv == 2)
3619 *disp_prop = 2;
3620 return charpos;
3621 }
3622
3623 /* Look forward for the first character with a `display' property
3624 that will replace the underlying text when displayed. */
3625 limpos = make_number (lim);
3626 do {
3627 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3628 CHARPOS (tpos) = XFASTINT (pos);
3629 if (CHARPOS (tpos) >= lim)
3630 {
3631 *disp_prop = 0;
3632 break;
3633 }
3634 if (STRINGP (object))
3635 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3636 else
3637 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3638 spec = Fget_char_property (pos, Qdisplay, object);
3639 if (!STRINGP (object))
3640 bufpos = CHARPOS (tpos);
3641 } while (NILP (spec)
3642 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3643 bufpos, frame_window_p)));
3644 if (rv == 2)
3645 *disp_prop = 2;
3646
3647 return CHARPOS (tpos);
3648 }
3649
3650 /* Return the character position of the end of the display string that
3651 started at CHARPOS. If there's no display string at CHARPOS,
3652 return -1. A display string is either an overlay with `display'
3653 property whose value is a string or a `display' text property whose
3654 value is a string. */
3655 ptrdiff_t
3656 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3657 {
3658 /* OBJECT = nil means current buffer. */
3659 Lisp_Object object =
3660 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3661 Lisp_Object pos = make_number (charpos);
3662 ptrdiff_t eob =
3663 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3664
3665 if (charpos >= eob || (string->s && !STRINGP (object)))
3666 return eob;
3667
3668 /* It could happen that the display property or overlay was removed
3669 since we found it in compute_display_string_pos above. One way
3670 this can happen is if JIT font-lock was called (through
3671 handle_fontified_prop), and jit-lock-functions remove text
3672 properties or overlays from the portion of buffer that includes
3673 CHARPOS. Muse mode is known to do that, for example. In this
3674 case, we return -1 to the caller, to signal that no display
3675 string is actually present at CHARPOS. See bidi_fetch_char for
3676 how this is handled.
3677
3678 An alternative would be to never look for display properties past
3679 it->stop_charpos. But neither compute_display_string_pos nor
3680 bidi_fetch_char that calls it know or care where the next
3681 stop_charpos is. */
3682 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3683 return -1;
3684
3685 /* Look forward for the first character where the `display' property
3686 changes. */
3687 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3688
3689 return XFASTINT (pos);
3690 }
3691
3692
3693 \f
3694 /***********************************************************************
3695 Fontification
3696 ***********************************************************************/
3697
3698 /* Handle changes in the `fontified' property of the current buffer by
3699 calling hook functions from Qfontification_functions to fontify
3700 regions of text. */
3701
3702 static enum prop_handled
3703 handle_fontified_prop (struct it *it)
3704 {
3705 Lisp_Object prop, pos;
3706 enum prop_handled handled = HANDLED_NORMALLY;
3707
3708 if (!NILP (Vmemory_full))
3709 return handled;
3710
3711 /* Get the value of the `fontified' property at IT's current buffer
3712 position. (The `fontified' property doesn't have a special
3713 meaning in strings.) If the value is nil, call functions from
3714 Qfontification_functions. */
3715 if (!STRINGP (it->string)
3716 && it->s == NULL
3717 && !NILP (Vfontification_functions)
3718 && !NILP (Vrun_hooks)
3719 && (pos = make_number (IT_CHARPOS (*it)),
3720 prop = Fget_char_property (pos, Qfontified, Qnil),
3721 /* Ignore the special cased nil value always present at EOB since
3722 no amount of fontifying will be able to change it. */
3723 NILP (prop) && IT_CHARPOS (*it) < Z))
3724 {
3725 ptrdiff_t count = SPECPDL_INDEX ();
3726 Lisp_Object val;
3727 struct buffer *obuf = current_buffer;
3728 int begv = BEGV, zv = ZV;
3729 int old_clip_changed = current_buffer->clip_changed;
3730
3731 val = Vfontification_functions;
3732 specbind (Qfontification_functions, Qnil);
3733
3734 eassert (it->end_charpos == ZV);
3735
3736 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3737 safe_call1 (val, pos);
3738 else
3739 {
3740 Lisp_Object fns, fn;
3741 struct gcpro gcpro1, gcpro2;
3742
3743 fns = Qnil;
3744 GCPRO2 (val, fns);
3745
3746 for (; CONSP (val); val = XCDR (val))
3747 {
3748 fn = XCAR (val);
3749
3750 if (EQ (fn, Qt))
3751 {
3752 /* A value of t indicates this hook has a local
3753 binding; it means to run the global binding too.
3754 In a global value, t should not occur. If it
3755 does, we must ignore it to avoid an endless
3756 loop. */
3757 for (fns = Fdefault_value (Qfontification_functions);
3758 CONSP (fns);
3759 fns = XCDR (fns))
3760 {
3761 fn = XCAR (fns);
3762 if (!EQ (fn, Qt))
3763 safe_call1 (fn, pos);
3764 }
3765 }
3766 else
3767 safe_call1 (fn, pos);
3768 }
3769
3770 UNGCPRO;
3771 }
3772
3773 unbind_to (count, Qnil);
3774
3775 /* Fontification functions routinely call `save-restriction'.
3776 Normally, this tags clip_changed, which can confuse redisplay
3777 (see discussion in Bug#6671). Since we don't perform any
3778 special handling of fontification changes in the case where
3779 `save-restriction' isn't called, there's no point doing so in
3780 this case either. So, if the buffer's restrictions are
3781 actually left unchanged, reset clip_changed. */
3782 if (obuf == current_buffer)
3783 {
3784 if (begv == BEGV && zv == ZV)
3785 current_buffer->clip_changed = old_clip_changed;
3786 }
3787 /* There isn't much we can reasonably do to protect against
3788 misbehaving fontification, but here's a fig leaf. */
3789 else if (BUFFER_LIVE_P (obuf))
3790 set_buffer_internal_1 (obuf);
3791
3792 /* The fontification code may have added/removed text.
3793 It could do even a lot worse, but let's at least protect against
3794 the most obvious case where only the text past `pos' gets changed',
3795 as is/was done in grep.el where some escapes sequences are turned
3796 into face properties (bug#7876). */
3797 it->end_charpos = ZV;
3798
3799 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3800 something. This avoids an endless loop if they failed to
3801 fontify the text for which reason ever. */
3802 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3803 handled = HANDLED_RECOMPUTE_PROPS;
3804 }
3805
3806 return handled;
3807 }
3808
3809
3810 \f
3811 /***********************************************************************
3812 Faces
3813 ***********************************************************************/
3814
3815 /* Set up iterator IT from face properties at its current position.
3816 Called from handle_stop. */
3817
3818 static enum prop_handled
3819 handle_face_prop (struct it *it)
3820 {
3821 int new_face_id;
3822 ptrdiff_t next_stop;
3823
3824 if (!STRINGP (it->string))
3825 {
3826 new_face_id
3827 = face_at_buffer_position (it->w,
3828 IT_CHARPOS (*it),
3829 it->region_beg_charpos,
3830 it->region_end_charpos,
3831 &next_stop,
3832 (IT_CHARPOS (*it)
3833 + TEXT_PROP_DISTANCE_LIMIT),
3834 0, it->base_face_id);
3835
3836 /* Is this a start of a run of characters with box face?
3837 Caveat: this can be called for a freshly initialized
3838 iterator; face_id is -1 in this case. We know that the new
3839 face will not change until limit, i.e. if the new face has a
3840 box, all characters up to limit will have one. But, as
3841 usual, we don't know whether limit is really the end. */
3842 if (new_face_id != it->face_id)
3843 {
3844 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3845 /* If it->face_id is -1, old_face below will be NULL, see
3846 the definition of FACE_FROM_ID. This will happen if this
3847 is the initial call that gets the face. */
3848 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3849
3850 /* If the value of face_id of the iterator is -1, we have to
3851 look in front of IT's position and see whether there is a
3852 face there that's different from new_face_id. */
3853 if (!old_face && IT_CHARPOS (*it) > BEG)
3854 {
3855 int prev_face_id = face_before_it_pos (it);
3856
3857 old_face = FACE_FROM_ID (it->f, prev_face_id);
3858 }
3859
3860 /* If the new face has a box, but the old face does not,
3861 this is the start of a run of characters with box face,
3862 i.e. this character has a shadow on the left side. */
3863 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3864 && (old_face == NULL || !old_face->box));
3865 it->face_box_p = new_face->box != FACE_NO_BOX;
3866 }
3867 }
3868 else
3869 {
3870 int base_face_id;
3871 ptrdiff_t bufpos;
3872 int i;
3873 Lisp_Object from_overlay
3874 = (it->current.overlay_string_index >= 0
3875 ? it->string_overlays[it->current.overlay_string_index
3876 % OVERLAY_STRING_CHUNK_SIZE]
3877 : Qnil);
3878
3879 /* See if we got to this string directly or indirectly from
3880 an overlay property. That includes the before-string or
3881 after-string of an overlay, strings in display properties
3882 provided by an overlay, their text properties, etc.
3883
3884 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3885 if (! NILP (from_overlay))
3886 for (i = it->sp - 1; i >= 0; i--)
3887 {
3888 if (it->stack[i].current.overlay_string_index >= 0)
3889 from_overlay
3890 = it->string_overlays[it->stack[i].current.overlay_string_index
3891 % OVERLAY_STRING_CHUNK_SIZE];
3892 else if (! NILP (it->stack[i].from_overlay))
3893 from_overlay = it->stack[i].from_overlay;
3894
3895 if (!NILP (from_overlay))
3896 break;
3897 }
3898
3899 if (! NILP (from_overlay))
3900 {
3901 bufpos = IT_CHARPOS (*it);
3902 /* For a string from an overlay, the base face depends
3903 only on text properties and ignores overlays. */
3904 base_face_id
3905 = face_for_overlay_string (it->w,
3906 IT_CHARPOS (*it),
3907 it->region_beg_charpos,
3908 it->region_end_charpos,
3909 &next_stop,
3910 (IT_CHARPOS (*it)
3911 + TEXT_PROP_DISTANCE_LIMIT),
3912 0,
3913 from_overlay);
3914 }
3915 else
3916 {
3917 bufpos = 0;
3918
3919 /* For strings from a `display' property, use the face at
3920 IT's current buffer position as the base face to merge
3921 with, so that overlay strings appear in the same face as
3922 surrounding text, unless they specify their own
3923 faces. */
3924 base_face_id = it->string_from_prefix_prop_p
3925 ? DEFAULT_FACE_ID
3926 : underlying_face_id (it);
3927 }
3928
3929 new_face_id = face_at_string_position (it->w,
3930 it->string,
3931 IT_STRING_CHARPOS (*it),
3932 bufpos,
3933 it->region_beg_charpos,
3934 it->region_end_charpos,
3935 &next_stop,
3936 base_face_id, 0);
3937
3938 /* Is this a start of a run of characters with box? Caveat:
3939 this can be called for a freshly allocated iterator; face_id
3940 is -1 is this case. We know that the new face will not
3941 change until the next check pos, i.e. if the new face has a
3942 box, all characters up to that position will have a
3943 box. But, as usual, we don't know whether that position
3944 is really the end. */
3945 if (new_face_id != it->face_id)
3946 {
3947 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3948 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3949
3950 /* If new face has a box but old face hasn't, this is the
3951 start of a run of characters with box, i.e. it has a
3952 shadow on the left side. */
3953 it->start_of_box_run_p
3954 = new_face->box && (old_face == NULL || !old_face->box);
3955 it->face_box_p = new_face->box != FACE_NO_BOX;
3956 }
3957 }
3958
3959 it->face_id = new_face_id;
3960 return HANDLED_NORMALLY;
3961 }
3962
3963
3964 /* Return the ID of the face ``underlying'' IT's current position,
3965 which is in a string. If the iterator is associated with a
3966 buffer, return the face at IT's current buffer position.
3967 Otherwise, use the iterator's base_face_id. */
3968
3969 static int
3970 underlying_face_id (struct it *it)
3971 {
3972 int face_id = it->base_face_id, i;
3973
3974 eassert (STRINGP (it->string));
3975
3976 for (i = it->sp - 1; i >= 0; --i)
3977 if (NILP (it->stack[i].string))
3978 face_id = it->stack[i].face_id;
3979
3980 return face_id;
3981 }
3982
3983
3984 /* Compute the face one character before or after the current position
3985 of IT, in the visual order. BEFORE_P non-zero means get the face
3986 in front (to the left in L2R paragraphs, to the right in R2L
3987 paragraphs) of IT's screen position. Value is the ID of the face. */
3988
3989 static int
3990 face_before_or_after_it_pos (struct it *it, int before_p)
3991 {
3992 int face_id, limit;
3993 ptrdiff_t next_check_charpos;
3994 struct it it_copy;
3995 void *it_copy_data = NULL;
3996
3997 eassert (it->s == NULL);
3998
3999 if (STRINGP (it->string))
4000 {
4001 ptrdiff_t bufpos, charpos;
4002 int base_face_id;
4003
4004 /* No face change past the end of the string (for the case
4005 we are padding with spaces). No face change before the
4006 string start. */
4007 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4008 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4009 return it->face_id;
4010
4011 if (!it->bidi_p)
4012 {
4013 /* Set charpos to the position before or after IT's current
4014 position, in the logical order, which in the non-bidi
4015 case is the same as the visual order. */
4016 if (before_p)
4017 charpos = IT_STRING_CHARPOS (*it) - 1;
4018 else if (it->what == IT_COMPOSITION)
4019 /* For composition, we must check the character after the
4020 composition. */
4021 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4022 else
4023 charpos = IT_STRING_CHARPOS (*it) + 1;
4024 }
4025 else
4026 {
4027 if (before_p)
4028 {
4029 /* With bidi iteration, the character before the current
4030 in the visual order cannot be found by simple
4031 iteration, because "reverse" reordering is not
4032 supported. Instead, we need to use the move_it_*
4033 family of functions. */
4034 /* Ignore face changes before the first visible
4035 character on this display line. */
4036 if (it->current_x <= it->first_visible_x)
4037 return it->face_id;
4038 SAVE_IT (it_copy, *it, it_copy_data);
4039 /* Implementation note: Since move_it_in_display_line
4040 works in the iterator geometry, and thinks the first
4041 character is always the leftmost, even in R2L lines,
4042 we don't need to distinguish between the R2L and L2R
4043 cases here. */
4044 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4045 it_copy.current_x - 1, MOVE_TO_X);
4046 charpos = IT_STRING_CHARPOS (it_copy);
4047 RESTORE_IT (it, it, it_copy_data);
4048 }
4049 else
4050 {
4051 /* Set charpos to the string position of the character
4052 that comes after IT's current position in the visual
4053 order. */
4054 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4055
4056 it_copy = *it;
4057 while (n--)
4058 bidi_move_to_visually_next (&it_copy.bidi_it);
4059
4060 charpos = it_copy.bidi_it.charpos;
4061 }
4062 }
4063 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4064
4065 if (it->current.overlay_string_index >= 0)
4066 bufpos = IT_CHARPOS (*it);
4067 else
4068 bufpos = 0;
4069
4070 base_face_id = underlying_face_id (it);
4071
4072 /* Get the face for ASCII, or unibyte. */
4073 face_id = face_at_string_position (it->w,
4074 it->string,
4075 charpos,
4076 bufpos,
4077 it->region_beg_charpos,
4078 it->region_end_charpos,
4079 &next_check_charpos,
4080 base_face_id, 0);
4081
4082 /* Correct the face for charsets different from ASCII. Do it
4083 for the multibyte case only. The face returned above is
4084 suitable for unibyte text if IT->string is unibyte. */
4085 if (STRING_MULTIBYTE (it->string))
4086 {
4087 struct text_pos pos1 = string_pos (charpos, it->string);
4088 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4089 int c, len;
4090 struct face *face = FACE_FROM_ID (it->f, face_id);
4091
4092 c = string_char_and_length (p, &len);
4093 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4094 }
4095 }
4096 else
4097 {
4098 struct text_pos pos;
4099
4100 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4101 || (IT_CHARPOS (*it) <= BEGV && before_p))
4102 return it->face_id;
4103
4104 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4105 pos = it->current.pos;
4106
4107 if (!it->bidi_p)
4108 {
4109 if (before_p)
4110 DEC_TEXT_POS (pos, it->multibyte_p);
4111 else
4112 {
4113 if (it->what == IT_COMPOSITION)
4114 {
4115 /* For composition, we must check the position after
4116 the composition. */
4117 pos.charpos += it->cmp_it.nchars;
4118 pos.bytepos += it->len;
4119 }
4120 else
4121 INC_TEXT_POS (pos, it->multibyte_p);
4122 }
4123 }
4124 else
4125 {
4126 if (before_p)
4127 {
4128 /* With bidi iteration, the character before the current
4129 in the visual order cannot be found by simple
4130 iteration, because "reverse" reordering is not
4131 supported. Instead, we need to use the move_it_*
4132 family of functions. */
4133 /* Ignore face changes before the first visible
4134 character on this display line. */
4135 if (it->current_x <= it->first_visible_x)
4136 return it->face_id;
4137 SAVE_IT (it_copy, *it, it_copy_data);
4138 /* Implementation note: Since move_it_in_display_line
4139 works in the iterator geometry, and thinks the first
4140 character is always the leftmost, even in R2L lines,
4141 we don't need to distinguish between the R2L and L2R
4142 cases here. */
4143 move_it_in_display_line (&it_copy, ZV,
4144 it_copy.current_x - 1, MOVE_TO_X);
4145 pos = it_copy.current.pos;
4146 RESTORE_IT (it, it, it_copy_data);
4147 }
4148 else
4149 {
4150 /* Set charpos to the buffer position of the character
4151 that comes after IT's current position in the visual
4152 order. */
4153 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4154
4155 it_copy = *it;
4156 while (n--)
4157 bidi_move_to_visually_next (&it_copy.bidi_it);
4158
4159 SET_TEXT_POS (pos,
4160 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4161 }
4162 }
4163 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4164
4165 /* Determine face for CHARSET_ASCII, or unibyte. */
4166 face_id = face_at_buffer_position (it->w,
4167 CHARPOS (pos),
4168 it->region_beg_charpos,
4169 it->region_end_charpos,
4170 &next_check_charpos,
4171 limit, 0, -1);
4172
4173 /* Correct the face for charsets different from ASCII. Do it
4174 for the multibyte case only. The face returned above is
4175 suitable for unibyte text if current_buffer is unibyte. */
4176 if (it->multibyte_p)
4177 {
4178 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4179 struct face *face = FACE_FROM_ID (it->f, face_id);
4180 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4181 }
4182 }
4183
4184 return face_id;
4185 }
4186
4187
4188 \f
4189 /***********************************************************************
4190 Invisible text
4191 ***********************************************************************/
4192
4193 /* Set up iterator IT from invisible properties at its current
4194 position. Called from handle_stop. */
4195
4196 static enum prop_handled
4197 handle_invisible_prop (struct it *it)
4198 {
4199 enum prop_handled handled = HANDLED_NORMALLY;
4200 int invis_p;
4201 Lisp_Object prop;
4202
4203 if (STRINGP (it->string))
4204 {
4205 Lisp_Object end_charpos, limit, charpos;
4206
4207 /* Get the value of the invisible text property at the
4208 current position. Value will be nil if there is no such
4209 property. */
4210 charpos = make_number (IT_STRING_CHARPOS (*it));
4211 prop = Fget_text_property (charpos, Qinvisible, it->string);
4212 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4213
4214 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4215 {
4216 /* Record whether we have to display an ellipsis for the
4217 invisible text. */
4218 int display_ellipsis_p = (invis_p == 2);
4219 ptrdiff_t len, endpos;
4220
4221 handled = HANDLED_RECOMPUTE_PROPS;
4222
4223 /* Get the position at which the next visible text can be
4224 found in IT->string, if any. */
4225 endpos = len = SCHARS (it->string);
4226 XSETINT (limit, len);
4227 do
4228 {
4229 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4230 it->string, limit);
4231 if (INTEGERP (end_charpos))
4232 {
4233 endpos = XFASTINT (end_charpos);
4234 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4235 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4236 if (invis_p == 2)
4237 display_ellipsis_p = 1;
4238 }
4239 }
4240 while (invis_p && endpos < len);
4241
4242 if (display_ellipsis_p)
4243 it->ellipsis_p = 1;
4244
4245 if (endpos < len)
4246 {
4247 /* Text at END_CHARPOS is visible. Move IT there. */
4248 struct text_pos old;
4249 ptrdiff_t oldpos;
4250
4251 old = it->current.string_pos;
4252 oldpos = CHARPOS (old);
4253 if (it->bidi_p)
4254 {
4255 if (it->bidi_it.first_elt
4256 && it->bidi_it.charpos < SCHARS (it->string))
4257 bidi_paragraph_init (it->paragraph_embedding,
4258 &it->bidi_it, 1);
4259 /* Bidi-iterate out of the invisible text. */
4260 do
4261 {
4262 bidi_move_to_visually_next (&it->bidi_it);
4263 }
4264 while (oldpos <= it->bidi_it.charpos
4265 && it->bidi_it.charpos < endpos);
4266
4267 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4268 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4269 if (IT_CHARPOS (*it) >= endpos)
4270 it->prev_stop = endpos;
4271 }
4272 else
4273 {
4274 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4275 compute_string_pos (&it->current.string_pos, old, it->string);
4276 }
4277 }
4278 else
4279 {
4280 /* The rest of the string is invisible. If this is an
4281 overlay string, proceed with the next overlay string
4282 or whatever comes and return a character from there. */
4283 if (it->current.overlay_string_index >= 0
4284 && !display_ellipsis_p)
4285 {
4286 next_overlay_string (it);
4287 /* Don't check for overlay strings when we just
4288 finished processing them. */
4289 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4290 }
4291 else
4292 {
4293 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4294 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4295 }
4296 }
4297 }
4298 }
4299 else
4300 {
4301 ptrdiff_t newpos, next_stop, start_charpos, tem;
4302 Lisp_Object pos, overlay;
4303
4304 /* First of all, is there invisible text at this position? */
4305 tem = start_charpos = IT_CHARPOS (*it);
4306 pos = make_number (tem);
4307 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4308 &overlay);
4309 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4310
4311 /* If we are on invisible text, skip over it. */
4312 if (invis_p && start_charpos < it->end_charpos)
4313 {
4314 /* Record whether we have to display an ellipsis for the
4315 invisible text. */
4316 int display_ellipsis_p = invis_p == 2;
4317
4318 handled = HANDLED_RECOMPUTE_PROPS;
4319
4320 /* Loop skipping over invisible text. The loop is left at
4321 ZV or with IT on the first char being visible again. */
4322 do
4323 {
4324 /* Try to skip some invisible text. Return value is the
4325 position reached which can be equal to where we start
4326 if there is nothing invisible there. This skips both
4327 over invisible text properties and overlays with
4328 invisible property. */
4329 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4330
4331 /* If we skipped nothing at all we weren't at invisible
4332 text in the first place. If everything to the end of
4333 the buffer was skipped, end the loop. */
4334 if (newpos == tem || newpos >= ZV)
4335 invis_p = 0;
4336 else
4337 {
4338 /* We skipped some characters but not necessarily
4339 all there are. Check if we ended up on visible
4340 text. Fget_char_property returns the property of
4341 the char before the given position, i.e. if we
4342 get invis_p = 0, this means that the char at
4343 newpos is visible. */
4344 pos = make_number (newpos);
4345 prop = Fget_char_property (pos, Qinvisible, it->window);
4346 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4347 }
4348
4349 /* If we ended up on invisible text, proceed to
4350 skip starting with next_stop. */
4351 if (invis_p)
4352 tem = next_stop;
4353
4354 /* If there are adjacent invisible texts, don't lose the
4355 second one's ellipsis. */
4356 if (invis_p == 2)
4357 display_ellipsis_p = 1;
4358 }
4359 while (invis_p);
4360
4361 /* The position newpos is now either ZV or on visible text. */
4362 if (it->bidi_p)
4363 {
4364 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4365 int on_newline =
4366 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4367 int after_newline =
4368 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4369
4370 /* If the invisible text ends on a newline or on a
4371 character after a newline, we can avoid the costly,
4372 character by character, bidi iteration to NEWPOS, and
4373 instead simply reseat the iterator there. That's
4374 because all bidi reordering information is tossed at
4375 the newline. This is a big win for modes that hide
4376 complete lines, like Outline, Org, etc. */
4377 if (on_newline || after_newline)
4378 {
4379 struct text_pos tpos;
4380 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4381
4382 SET_TEXT_POS (tpos, newpos, bpos);
4383 reseat_1 (it, tpos, 0);
4384 /* If we reseat on a newline/ZV, we need to prep the
4385 bidi iterator for advancing to the next character
4386 after the newline/EOB, keeping the current paragraph
4387 direction (so that PRODUCE_GLYPHS does TRT wrt
4388 prepending/appending glyphs to a glyph row). */
4389 if (on_newline)
4390 {
4391 it->bidi_it.first_elt = 0;
4392 it->bidi_it.paragraph_dir = pdir;
4393 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4394 it->bidi_it.nchars = 1;
4395 it->bidi_it.ch_len = 1;
4396 }
4397 }
4398 else /* Must use the slow method. */
4399 {
4400 /* With bidi iteration, the region of invisible text
4401 could start and/or end in the middle of a
4402 non-base embedding level. Therefore, we need to
4403 skip invisible text using the bidi iterator,
4404 starting at IT's current position, until we find
4405 ourselves outside of the invisible text.
4406 Skipping invisible text _after_ bidi iteration
4407 avoids affecting the visual order of the
4408 displayed text when invisible properties are
4409 added or removed. */
4410 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4411 {
4412 /* If we were `reseat'ed to a new paragraph,
4413 determine the paragraph base direction. We
4414 need to do it now because
4415 next_element_from_buffer may not have a
4416 chance to do it, if we are going to skip any
4417 text at the beginning, which resets the
4418 FIRST_ELT flag. */
4419 bidi_paragraph_init (it->paragraph_embedding,
4420 &it->bidi_it, 1);
4421 }
4422 do
4423 {
4424 bidi_move_to_visually_next (&it->bidi_it);
4425 }
4426 while (it->stop_charpos <= it->bidi_it.charpos
4427 && it->bidi_it.charpos < newpos);
4428 IT_CHARPOS (*it) = it->bidi_it.charpos;
4429 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4430 /* If we overstepped NEWPOS, record its position in
4431 the iterator, so that we skip invisible text if
4432 later the bidi iteration lands us in the
4433 invisible region again. */
4434 if (IT_CHARPOS (*it) >= newpos)
4435 it->prev_stop = newpos;
4436 }
4437 }
4438 else
4439 {
4440 IT_CHARPOS (*it) = newpos;
4441 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4442 }
4443
4444 /* If there are before-strings at the start of invisible
4445 text, and the text is invisible because of a text
4446 property, arrange to show before-strings because 20.x did
4447 it that way. (If the text is invisible because of an
4448 overlay property instead of a text property, this is
4449 already handled in the overlay code.) */
4450 if (NILP (overlay)
4451 && get_overlay_strings (it, it->stop_charpos))
4452 {
4453 handled = HANDLED_RECOMPUTE_PROPS;
4454 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4455 }
4456 else if (display_ellipsis_p)
4457 {
4458 /* Make sure that the glyphs of the ellipsis will get
4459 correct `charpos' values. If we would not update
4460 it->position here, the glyphs would belong to the
4461 last visible character _before_ the invisible
4462 text, which confuses `set_cursor_from_row'.
4463
4464 We use the last invisible position instead of the
4465 first because this way the cursor is always drawn on
4466 the first "." of the ellipsis, whenever PT is inside
4467 the invisible text. Otherwise the cursor would be
4468 placed _after_ the ellipsis when the point is after the
4469 first invisible character. */
4470 if (!STRINGP (it->object))
4471 {
4472 it->position.charpos = newpos - 1;
4473 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4474 }
4475 it->ellipsis_p = 1;
4476 /* Let the ellipsis display before
4477 considering any properties of the following char.
4478 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4479 handled = HANDLED_RETURN;
4480 }
4481 }
4482 }
4483
4484 return handled;
4485 }
4486
4487
4488 /* Make iterator IT return `...' next.
4489 Replaces LEN characters from buffer. */
4490
4491 static void
4492 setup_for_ellipsis (struct it *it, int len)
4493 {
4494 /* Use the display table definition for `...'. Invalid glyphs
4495 will be handled by the method returning elements from dpvec. */
4496 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4497 {
4498 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4499 it->dpvec = v->contents;
4500 it->dpend = v->contents + v->header.size;
4501 }
4502 else
4503 {
4504 /* Default `...'. */
4505 it->dpvec = default_invis_vector;
4506 it->dpend = default_invis_vector + 3;
4507 }
4508
4509 it->dpvec_char_len = len;
4510 it->current.dpvec_index = 0;
4511 it->dpvec_face_id = -1;
4512
4513 /* Remember the current face id in case glyphs specify faces.
4514 IT's face is restored in set_iterator_to_next.
4515 saved_face_id was set to preceding char's face in handle_stop. */
4516 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4517 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4518
4519 it->method = GET_FROM_DISPLAY_VECTOR;
4520 it->ellipsis_p = 1;
4521 }
4522
4523
4524 \f
4525 /***********************************************************************
4526 'display' property
4527 ***********************************************************************/
4528
4529 /* Set up iterator IT from `display' property at its current position.
4530 Called from handle_stop.
4531 We return HANDLED_RETURN if some part of the display property
4532 overrides the display of the buffer text itself.
4533 Otherwise we return HANDLED_NORMALLY. */
4534
4535 static enum prop_handled
4536 handle_display_prop (struct it *it)
4537 {
4538 Lisp_Object propval, object, overlay;
4539 struct text_pos *position;
4540 ptrdiff_t bufpos;
4541 /* Nonzero if some property replaces the display of the text itself. */
4542 int display_replaced_p = 0;
4543
4544 if (STRINGP (it->string))
4545 {
4546 object = it->string;
4547 position = &it->current.string_pos;
4548 bufpos = CHARPOS (it->current.pos);
4549 }
4550 else
4551 {
4552 XSETWINDOW (object, it->w);
4553 position = &it->current.pos;
4554 bufpos = CHARPOS (*position);
4555 }
4556
4557 /* Reset those iterator values set from display property values. */
4558 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4559 it->space_width = Qnil;
4560 it->font_height = Qnil;
4561 it->voffset = 0;
4562
4563 /* We don't support recursive `display' properties, i.e. string
4564 values that have a string `display' property, that have a string
4565 `display' property etc. */
4566 if (!it->string_from_display_prop_p)
4567 it->area = TEXT_AREA;
4568
4569 propval = get_char_property_and_overlay (make_number (position->charpos),
4570 Qdisplay, object, &overlay);
4571 if (NILP (propval))
4572 return HANDLED_NORMALLY;
4573 /* Now OVERLAY is the overlay that gave us this property, or nil
4574 if it was a text property. */
4575
4576 if (!STRINGP (it->string))
4577 object = it->w->contents;
4578
4579 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4580 position, bufpos,
4581 FRAME_WINDOW_P (it->f));
4582
4583 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4584 }
4585
4586 /* Subroutine of handle_display_prop. Returns non-zero if the display
4587 specification in SPEC is a replacing specification, i.e. it would
4588 replace the text covered by `display' property with something else,
4589 such as an image or a display string. If SPEC includes any kind or
4590 `(space ...) specification, the value is 2; this is used by
4591 compute_display_string_pos, which see.
4592
4593 See handle_single_display_spec for documentation of arguments.
4594 frame_window_p is non-zero if the window being redisplayed is on a
4595 GUI frame; this argument is used only if IT is NULL, see below.
4596
4597 IT can be NULL, if this is called by the bidi reordering code
4598 through compute_display_string_pos, which see. In that case, this
4599 function only examines SPEC, but does not otherwise "handle" it, in
4600 the sense that it doesn't set up members of IT from the display
4601 spec. */
4602 static int
4603 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4604 Lisp_Object overlay, struct text_pos *position,
4605 ptrdiff_t bufpos, int frame_window_p)
4606 {
4607 int replacing_p = 0;
4608 int rv;
4609
4610 if (CONSP (spec)
4611 /* Simple specifications. */
4612 && !EQ (XCAR (spec), Qimage)
4613 #ifdef HAVE_XWIDGETS
4614 && !EQ (XCAR (spec), Qxwidget)
4615 #endif
4616 && !EQ (XCAR (spec), Qspace)
4617 && !EQ (XCAR (spec), Qwhen)
4618 && !EQ (XCAR (spec), Qslice)
4619 && !EQ (XCAR (spec), Qspace_width)
4620 && !EQ (XCAR (spec), Qheight)
4621 && !EQ (XCAR (spec), Qraise)
4622 /* Marginal area specifications. */
4623 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4624 && !EQ (XCAR (spec), Qleft_fringe)
4625 && !EQ (XCAR (spec), Qright_fringe)
4626 && !NILP (XCAR (spec)))
4627 {
4628 for (; CONSP (spec); spec = XCDR (spec))
4629 {
4630 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4631 overlay, position, bufpos,
4632 replacing_p, frame_window_p)))
4633 {
4634 replacing_p = rv;
4635 /* If some text in a string is replaced, `position' no
4636 longer points to the position of `object'. */
4637 if (!it || STRINGP (object))
4638 break;
4639 }
4640 }
4641 }
4642 else if (VECTORP (spec))
4643 {
4644 ptrdiff_t i;
4645 for (i = 0; i < ASIZE (spec); ++i)
4646 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4647 overlay, position, bufpos,
4648 replacing_p, frame_window_p)))
4649 {
4650 replacing_p = rv;
4651 /* If some text in a string is replaced, `position' no
4652 longer points to the position of `object'. */
4653 if (!it || STRINGP (object))
4654 break;
4655 }
4656 }
4657 else
4658 {
4659 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4660 position, bufpos, 0,
4661 frame_window_p)))
4662 replacing_p = rv;
4663 }
4664
4665 return replacing_p;
4666 }
4667
4668 /* Value is the position of the end of the `display' property starting
4669 at START_POS in OBJECT. */
4670
4671 static struct text_pos
4672 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4673 {
4674 Lisp_Object end;
4675 struct text_pos end_pos;
4676
4677 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4678 Qdisplay, object, Qnil);
4679 CHARPOS (end_pos) = XFASTINT (end);
4680 if (STRINGP (object))
4681 compute_string_pos (&end_pos, start_pos, it->string);
4682 else
4683 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4684
4685 return end_pos;
4686 }
4687
4688
4689 /* Set up IT from a single `display' property specification SPEC. OBJECT
4690 is the object in which the `display' property was found. *POSITION
4691 is the position in OBJECT at which the `display' property was found.
4692 BUFPOS is the buffer position of OBJECT (different from POSITION if
4693 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4694 previously saw a display specification which already replaced text
4695 display with something else, for example an image; we ignore such
4696 properties after the first one has been processed.
4697
4698 OVERLAY is the overlay this `display' property came from,
4699 or nil if it was a text property.
4700
4701 If SPEC is a `space' or `image' specification, and in some other
4702 cases too, set *POSITION to the position where the `display'
4703 property ends.
4704
4705 If IT is NULL, only examine the property specification in SPEC, but
4706 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4707 is intended to be displayed in a window on a GUI frame.
4708
4709 Value is non-zero if something was found which replaces the display
4710 of buffer or string text. */
4711
4712 static int
4713 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4714 Lisp_Object overlay, struct text_pos *position,
4715 ptrdiff_t bufpos, int display_replaced_p,
4716 int frame_window_p)
4717 {
4718 Lisp_Object form;
4719 Lisp_Object location, value;
4720 struct text_pos start_pos = *position;
4721 int valid_p;
4722
4723 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4724 If the result is non-nil, use VALUE instead of SPEC. */
4725 form = Qt;
4726 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4727 {
4728 spec = XCDR (spec);
4729 if (!CONSP (spec))
4730 return 0;
4731 form = XCAR (spec);
4732 spec = XCDR (spec);
4733 }
4734
4735 if (!NILP (form) && !EQ (form, Qt))
4736 {
4737 ptrdiff_t count = SPECPDL_INDEX ();
4738 struct gcpro gcpro1;
4739
4740 /* Bind `object' to the object having the `display' property, a
4741 buffer or string. Bind `position' to the position in the
4742 object where the property was found, and `buffer-position'
4743 to the current position in the buffer. */
4744
4745 if (NILP (object))
4746 XSETBUFFER (object, current_buffer);
4747 specbind (Qobject, object);
4748 specbind (Qposition, make_number (CHARPOS (*position)));
4749 specbind (Qbuffer_position, make_number (bufpos));
4750 GCPRO1 (form);
4751 form = safe_eval (form);
4752 UNGCPRO;
4753 unbind_to (count, Qnil);
4754 }
4755
4756 if (NILP (form))
4757 return 0;
4758
4759 /* Handle `(height HEIGHT)' specifications. */
4760 if (CONSP (spec)
4761 && EQ (XCAR (spec), Qheight)
4762 && CONSP (XCDR (spec)))
4763 {
4764 if (it)
4765 {
4766 if (!FRAME_WINDOW_P (it->f))
4767 return 0;
4768
4769 it->font_height = XCAR (XCDR (spec));
4770 if (!NILP (it->font_height))
4771 {
4772 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4773 int new_height = -1;
4774
4775 if (CONSP (it->font_height)
4776 && (EQ (XCAR (it->font_height), Qplus)
4777 || EQ (XCAR (it->font_height), Qminus))
4778 && CONSP (XCDR (it->font_height))
4779 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4780 {
4781 /* `(+ N)' or `(- N)' where N is an integer. */
4782 int steps = XINT (XCAR (XCDR (it->font_height)));
4783 if (EQ (XCAR (it->font_height), Qplus))
4784 steps = - steps;
4785 it->face_id = smaller_face (it->f, it->face_id, steps);
4786 }
4787 else if (FUNCTIONP (it->font_height))
4788 {
4789 /* Call function with current height as argument.
4790 Value is the new height. */
4791 Lisp_Object height;
4792 height = safe_call1 (it->font_height,
4793 face->lface[LFACE_HEIGHT_INDEX]);
4794 if (NUMBERP (height))
4795 new_height = XFLOATINT (height);
4796 }
4797 else if (NUMBERP (it->font_height))
4798 {
4799 /* Value is a multiple of the canonical char height. */
4800 struct face *f;
4801
4802 f = FACE_FROM_ID (it->f,
4803 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4804 new_height = (XFLOATINT (it->font_height)
4805 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4806 }
4807 else
4808 {
4809 /* Evaluate IT->font_height with `height' bound to the
4810 current specified height to get the new height. */
4811 ptrdiff_t count = SPECPDL_INDEX ();
4812
4813 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4814 value = safe_eval (it->font_height);
4815 unbind_to (count, Qnil);
4816
4817 if (NUMBERP (value))
4818 new_height = XFLOATINT (value);
4819 }
4820
4821 if (new_height > 0)
4822 it->face_id = face_with_height (it->f, it->face_id, new_height);
4823 }
4824 }
4825
4826 return 0;
4827 }
4828
4829 /* Handle `(space-width WIDTH)'. */
4830 if (CONSP (spec)
4831 && EQ (XCAR (spec), Qspace_width)
4832 && CONSP (XCDR (spec)))
4833 {
4834 if (it)
4835 {
4836 if (!FRAME_WINDOW_P (it->f))
4837 return 0;
4838
4839 value = XCAR (XCDR (spec));
4840 if (NUMBERP (value) && XFLOATINT (value) > 0)
4841 it->space_width = value;
4842 }
4843
4844 return 0;
4845 }
4846
4847 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4848 if (CONSP (spec)
4849 && EQ (XCAR (spec), Qslice))
4850 {
4851 Lisp_Object tem;
4852
4853 if (it)
4854 {
4855 if (!FRAME_WINDOW_P (it->f))
4856 return 0;
4857
4858 if (tem = XCDR (spec), CONSP (tem))
4859 {
4860 it->slice.x = XCAR (tem);
4861 if (tem = XCDR (tem), CONSP (tem))
4862 {
4863 it->slice.y = XCAR (tem);
4864 if (tem = XCDR (tem), CONSP (tem))
4865 {
4866 it->slice.width = XCAR (tem);
4867 if (tem = XCDR (tem), CONSP (tem))
4868 it->slice.height = XCAR (tem);
4869 }
4870 }
4871 }
4872 }
4873
4874 return 0;
4875 }
4876
4877 /* Handle `(raise FACTOR)'. */
4878 if (CONSP (spec)
4879 && EQ (XCAR (spec), Qraise)
4880 && CONSP (XCDR (spec)))
4881 {
4882 if (it)
4883 {
4884 if (!FRAME_WINDOW_P (it->f))
4885 return 0;
4886
4887 #ifdef HAVE_WINDOW_SYSTEM
4888 value = XCAR (XCDR (spec));
4889 if (NUMBERP (value))
4890 {
4891 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4892 it->voffset = - (XFLOATINT (value)
4893 * (FONT_HEIGHT (face->font)));
4894 }
4895 #endif /* HAVE_WINDOW_SYSTEM */
4896 }
4897
4898 return 0;
4899 }
4900
4901 /* Don't handle the other kinds of display specifications
4902 inside a string that we got from a `display' property. */
4903 if (it && it->string_from_display_prop_p)
4904 return 0;
4905
4906 /* Characters having this form of property are not displayed, so
4907 we have to find the end of the property. */
4908 if (it)
4909 {
4910 start_pos = *position;
4911 *position = display_prop_end (it, object, start_pos);
4912 }
4913 value = Qnil;
4914
4915 /* Stop the scan at that end position--we assume that all
4916 text properties change there. */
4917 if (it)
4918 it->stop_charpos = position->charpos;
4919
4920 /* Handle `(left-fringe BITMAP [FACE])'
4921 and `(right-fringe BITMAP [FACE])'. */
4922 if (CONSP (spec)
4923 && (EQ (XCAR (spec), Qleft_fringe)
4924 || EQ (XCAR (spec), Qright_fringe))
4925 && CONSP (XCDR (spec)))
4926 {
4927 int fringe_bitmap;
4928
4929 if (it)
4930 {
4931 if (!FRAME_WINDOW_P (it->f))
4932 /* If we return here, POSITION has been advanced
4933 across the text with this property. */
4934 {
4935 /* Synchronize the bidi iterator with POSITION. This is
4936 needed because we are not going to push the iterator
4937 on behalf of this display property, so there will be
4938 no pop_it call to do this synchronization for us. */
4939 if (it->bidi_p)
4940 {
4941 it->position = *position;
4942 iterate_out_of_display_property (it);
4943 *position = it->position;
4944 }
4945 return 1;
4946 }
4947 }
4948 else if (!frame_window_p)
4949 return 1;
4950
4951 #ifdef HAVE_WINDOW_SYSTEM
4952 value = XCAR (XCDR (spec));
4953 if (!SYMBOLP (value)
4954 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4955 /* If we return here, POSITION has been advanced
4956 across the text with this property. */
4957 {
4958 if (it && it->bidi_p)
4959 {
4960 it->position = *position;
4961 iterate_out_of_display_property (it);
4962 *position = it->position;
4963 }
4964 return 1;
4965 }
4966
4967 if (it)
4968 {
4969 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4970
4971 if (CONSP (XCDR (XCDR (spec))))
4972 {
4973 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4974 int face_id2 = lookup_derived_face (it->f, face_name,
4975 FRINGE_FACE_ID, 0);
4976 if (face_id2 >= 0)
4977 face_id = face_id2;
4978 }
4979
4980 /* Save current settings of IT so that we can restore them
4981 when we are finished with the glyph property value. */
4982 push_it (it, position);
4983
4984 it->area = TEXT_AREA;
4985 it->what = IT_IMAGE;
4986 it->image_id = -1; /* no image */
4987 it->position = start_pos;
4988 it->object = NILP (object) ? it->w->contents : object;
4989 it->method = GET_FROM_IMAGE;
4990 it->from_overlay = Qnil;
4991 it->face_id = face_id;
4992 it->from_disp_prop_p = 1;
4993
4994 /* Say that we haven't consumed the characters with
4995 `display' property yet. The call to pop_it in
4996 set_iterator_to_next will clean this up. */
4997 *position = start_pos;
4998
4999 if (EQ (XCAR (spec), Qleft_fringe))
5000 {
5001 it->left_user_fringe_bitmap = fringe_bitmap;
5002 it->left_user_fringe_face_id = face_id;
5003 }
5004 else
5005 {
5006 it->right_user_fringe_bitmap = fringe_bitmap;
5007 it->right_user_fringe_face_id = face_id;
5008 }
5009 }
5010 #endif /* HAVE_WINDOW_SYSTEM */
5011 return 1;
5012 }
5013
5014 /* Prepare to handle `((margin left-margin) ...)',
5015 `((margin right-margin) ...)' and `((margin nil) ...)'
5016 prefixes for display specifications. */
5017 location = Qunbound;
5018 if (CONSP (spec) && CONSP (XCAR (spec)))
5019 {
5020 Lisp_Object tem;
5021
5022 value = XCDR (spec);
5023 if (CONSP (value))
5024 value = XCAR (value);
5025
5026 tem = XCAR (spec);
5027 if (EQ (XCAR (tem), Qmargin)
5028 && (tem = XCDR (tem),
5029 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5030 (NILP (tem)
5031 || EQ (tem, Qleft_margin)
5032 || EQ (tem, Qright_margin))))
5033 location = tem;
5034 }
5035
5036 if (EQ (location, Qunbound))
5037 {
5038 location = Qnil;
5039 value = spec;
5040 }
5041
5042 /* After this point, VALUE is the property after any
5043 margin prefix has been stripped. It must be a string,
5044 an image specification, or `(space ...)'.
5045
5046 LOCATION specifies where to display: `left-margin',
5047 `right-margin' or nil. */
5048
5049 valid_p = (STRINGP (value)
5050 #ifdef HAVE_WINDOW_SYSTEM
5051 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5052 && valid_image_p (value))
5053 #endif /* not HAVE_WINDOW_SYSTEM */
5054 || (CONSP (value) && EQ (XCAR (value), Qspace))
5055 #ifdef HAVE_XWIDGETS
5056 || valid_xwidget_spec_p(value)
5057 #endif
5058 );
5059
5060 if (valid_p && !display_replaced_p)
5061 {
5062 int retval = 1;
5063
5064 if (!it)
5065 {
5066 /* Callers need to know whether the display spec is any kind
5067 of `(space ...)' spec that is about to affect text-area
5068 display. */
5069 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5070 retval = 2;
5071 return retval;
5072 }
5073
5074 /* Save current settings of IT so that we can restore them
5075 when we are finished with the glyph property value. */
5076 push_it (it, position);
5077 it->from_overlay = overlay;
5078 it->from_disp_prop_p = 1;
5079
5080 if (NILP (location))
5081 it->area = TEXT_AREA;
5082 else if (EQ (location, Qleft_margin))
5083 it->area = LEFT_MARGIN_AREA;
5084 else
5085 it->area = RIGHT_MARGIN_AREA;
5086
5087 if (STRINGP (value))
5088 {
5089 it->string = value;
5090 it->multibyte_p = STRING_MULTIBYTE (it->string);
5091 it->current.overlay_string_index = -1;
5092 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5093 it->end_charpos = it->string_nchars = SCHARS (it->string);
5094 it->method = GET_FROM_STRING;
5095 it->stop_charpos = 0;
5096 it->prev_stop = 0;
5097 it->base_level_stop = 0;
5098 it->string_from_display_prop_p = 1;
5099 /* Say that we haven't consumed the characters with
5100 `display' property yet. The call to pop_it in
5101 set_iterator_to_next will clean this up. */
5102 if (BUFFERP (object))
5103 *position = start_pos;
5104
5105 /* Force paragraph direction to be that of the parent
5106 object. If the parent object's paragraph direction is
5107 not yet determined, default to L2R. */
5108 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5109 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5110 else
5111 it->paragraph_embedding = L2R;
5112
5113 /* Set up the bidi iterator for this display string. */
5114 if (it->bidi_p)
5115 {
5116 it->bidi_it.string.lstring = it->string;
5117 it->bidi_it.string.s = NULL;
5118 it->bidi_it.string.schars = it->end_charpos;
5119 it->bidi_it.string.bufpos = bufpos;
5120 it->bidi_it.string.from_disp_str = 1;
5121 it->bidi_it.string.unibyte = !it->multibyte_p;
5122 it->bidi_it.w = it->w;
5123 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5124 }
5125 }
5126 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5127 {
5128 it->method = GET_FROM_STRETCH;
5129 it->object = value;
5130 *position = it->position = start_pos;
5131 retval = 1 + (it->area == TEXT_AREA);
5132 }
5133 #ifdef HAVE_XWIDGETS
5134 else if (valid_xwidget_spec_p(value))
5135 {
5136 //printf("handle_single_display_spec: im an xwidget!!\n");
5137 it->what = IT_XWIDGET;
5138 it->method = GET_FROM_XWIDGET;
5139 it->position = start_pos;
5140 it->object = NILP (object) ? it->w->contents : object;
5141 *position = start_pos;
5142
5143 it->xwidget = lookup_xwidget(value);
5144 }
5145 #endif
5146 #ifdef HAVE_WINDOW_SYSTEM
5147 else
5148 {
5149 it->what = IT_IMAGE;
5150 it->image_id = lookup_image (it->f, value);
5151 it->position = start_pos;
5152 it->object = NILP (object) ? it->w->contents : object;
5153 it->method = GET_FROM_IMAGE;
5154
5155 /* Say that we haven't consumed the characters with
5156 `display' property yet. The call to pop_it in
5157 set_iterator_to_next will clean this up. */
5158 *position = start_pos;
5159 }
5160 #endif /* HAVE_WINDOW_SYSTEM */
5161
5162 return retval;
5163 }
5164
5165 /* Invalid property or property not supported. Restore
5166 POSITION to what it was before. */
5167 *position = start_pos;
5168 return 0;
5169 }
5170
5171 /* Check if PROP is a display property value whose text should be
5172 treated as intangible. OVERLAY is the overlay from which PROP
5173 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5174 specify the buffer position covered by PROP. */
5175
5176 int
5177 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5178 ptrdiff_t charpos, ptrdiff_t bytepos)
5179 {
5180 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5181 struct text_pos position;
5182
5183 SET_TEXT_POS (position, charpos, bytepos);
5184 return handle_display_spec (NULL, prop, Qnil, overlay,
5185 &position, charpos, frame_window_p);
5186 }
5187
5188
5189 /* Return 1 if PROP is a display sub-property value containing STRING.
5190
5191 Implementation note: this and the following function are really
5192 special cases of handle_display_spec and
5193 handle_single_display_spec, and should ideally use the same code.
5194 Until they do, these two pairs must be consistent and must be
5195 modified in sync. */
5196
5197 static int
5198 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5199 {
5200 if (EQ (string, prop))
5201 return 1;
5202
5203 /* Skip over `when FORM'. */
5204 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5205 {
5206 prop = XCDR (prop);
5207 if (!CONSP (prop))
5208 return 0;
5209 /* Actually, the condition following `when' should be eval'ed,
5210 like handle_single_display_spec does, and we should return
5211 zero if it evaluates to nil. However, this function is
5212 called only when the buffer was already displayed and some
5213 glyph in the glyph matrix was found to come from a display
5214 string. Therefore, the condition was already evaluated, and
5215 the result was non-nil, otherwise the display string wouldn't
5216 have been displayed and we would have never been called for
5217 this property. Thus, we can skip the evaluation and assume
5218 its result is non-nil. */
5219 prop = XCDR (prop);
5220 }
5221
5222 if (CONSP (prop))
5223 /* Skip over `margin LOCATION'. */
5224 if (EQ (XCAR (prop), Qmargin))
5225 {
5226 prop = XCDR (prop);
5227 if (!CONSP (prop))
5228 return 0;
5229
5230 prop = XCDR (prop);
5231 if (!CONSP (prop))
5232 return 0;
5233 }
5234
5235 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5236 }
5237
5238
5239 /* Return 1 if STRING appears in the `display' property PROP. */
5240
5241 static int
5242 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5243 {
5244 if (CONSP (prop)
5245 && !EQ (XCAR (prop), Qwhen)
5246 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5247 {
5248 /* A list of sub-properties. */
5249 while (CONSP (prop))
5250 {
5251 if (single_display_spec_string_p (XCAR (prop), string))
5252 return 1;
5253 prop = XCDR (prop);
5254 }
5255 }
5256 else if (VECTORP (prop))
5257 {
5258 /* A vector of sub-properties. */
5259 ptrdiff_t i;
5260 for (i = 0; i < ASIZE (prop); ++i)
5261 if (single_display_spec_string_p (AREF (prop, i), string))
5262 return 1;
5263 }
5264 else
5265 return single_display_spec_string_p (prop, string);
5266
5267 return 0;
5268 }
5269
5270 /* Look for STRING in overlays and text properties in the current
5271 buffer, between character positions FROM and TO (excluding TO).
5272 BACK_P non-zero means look back (in this case, TO is supposed to be
5273 less than FROM).
5274 Value is the first character position where STRING was found, or
5275 zero if it wasn't found before hitting TO.
5276
5277 This function may only use code that doesn't eval because it is
5278 called asynchronously from note_mouse_highlight. */
5279
5280 static ptrdiff_t
5281 string_buffer_position_lim (Lisp_Object string,
5282 ptrdiff_t from, ptrdiff_t to, int back_p)
5283 {
5284 Lisp_Object limit, prop, pos;
5285 int found = 0;
5286
5287 pos = make_number (max (from, BEGV));
5288
5289 if (!back_p) /* looking forward */
5290 {
5291 limit = make_number (min (to, ZV));
5292 while (!found && !EQ (pos, limit))
5293 {
5294 prop = Fget_char_property (pos, Qdisplay, Qnil);
5295 if (!NILP (prop) && display_prop_string_p (prop, string))
5296 found = 1;
5297 else
5298 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5299 limit);
5300 }
5301 }
5302 else /* looking back */
5303 {
5304 limit = make_number (max (to, BEGV));
5305 while (!found && !EQ (pos, limit))
5306 {
5307 prop = Fget_char_property (pos, Qdisplay, Qnil);
5308 if (!NILP (prop) && display_prop_string_p (prop, string))
5309 found = 1;
5310 else
5311 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5312 limit);
5313 }
5314 }
5315
5316 return found ? XINT (pos) : 0;
5317 }
5318
5319 /* Determine which buffer position in current buffer STRING comes from.
5320 AROUND_CHARPOS is an approximate position where it could come from.
5321 Value is the buffer position or 0 if it couldn't be determined.
5322
5323 This function is necessary because we don't record buffer positions
5324 in glyphs generated from strings (to keep struct glyph small).
5325 This function may only use code that doesn't eval because it is
5326 called asynchronously from note_mouse_highlight. */
5327
5328 static ptrdiff_t
5329 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5330 {
5331 const int MAX_DISTANCE = 1000;
5332 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5333 around_charpos + MAX_DISTANCE,
5334 0);
5335
5336 if (!found)
5337 found = string_buffer_position_lim (string, around_charpos,
5338 around_charpos - MAX_DISTANCE, 1);
5339 return found;
5340 }
5341
5342
5343 \f
5344 /***********************************************************************
5345 `composition' property
5346 ***********************************************************************/
5347
5348 /* Set up iterator IT from `composition' property at its current
5349 position. Called from handle_stop. */
5350
5351 static enum prop_handled
5352 handle_composition_prop (struct it *it)
5353 {
5354 Lisp_Object prop, string;
5355 ptrdiff_t pos, pos_byte, start, end;
5356
5357 if (STRINGP (it->string))
5358 {
5359 unsigned char *s;
5360
5361 pos = IT_STRING_CHARPOS (*it);
5362 pos_byte = IT_STRING_BYTEPOS (*it);
5363 string = it->string;
5364 s = SDATA (string) + pos_byte;
5365 it->c = STRING_CHAR (s);
5366 }
5367 else
5368 {
5369 pos = IT_CHARPOS (*it);
5370 pos_byte = IT_BYTEPOS (*it);
5371 string = Qnil;
5372 it->c = FETCH_CHAR (pos_byte);
5373 }
5374
5375 /* If there's a valid composition and point is not inside of the
5376 composition (in the case that the composition is from the current
5377 buffer), draw a glyph composed from the composition components. */
5378 if (find_composition (pos, -1, &start, &end, &prop, string)
5379 && composition_valid_p (start, end, prop)
5380 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5381 {
5382 if (start < pos)
5383 /* As we can't handle this situation (perhaps font-lock added
5384 a new composition), we just return here hoping that next
5385 redisplay will detect this composition much earlier. */
5386 return HANDLED_NORMALLY;
5387 if (start != pos)
5388 {
5389 if (STRINGP (it->string))
5390 pos_byte = string_char_to_byte (it->string, start);
5391 else
5392 pos_byte = CHAR_TO_BYTE (start);
5393 }
5394 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5395 prop, string);
5396
5397 if (it->cmp_it.id >= 0)
5398 {
5399 it->cmp_it.ch = -1;
5400 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5401 it->cmp_it.nglyphs = -1;
5402 }
5403 }
5404
5405 return HANDLED_NORMALLY;
5406 }
5407
5408
5409 \f
5410 /***********************************************************************
5411 Overlay strings
5412 ***********************************************************************/
5413
5414 /* The following structure is used to record overlay strings for
5415 later sorting in load_overlay_strings. */
5416
5417 struct overlay_entry
5418 {
5419 Lisp_Object overlay;
5420 Lisp_Object string;
5421 EMACS_INT priority;
5422 int after_string_p;
5423 };
5424
5425
5426 /* Set up iterator IT from overlay strings at its current position.
5427 Called from handle_stop. */
5428
5429 static enum prop_handled
5430 handle_overlay_change (struct it *it)
5431 {
5432 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5433 return HANDLED_RECOMPUTE_PROPS;
5434 else
5435 return HANDLED_NORMALLY;
5436 }
5437
5438
5439 /* Set up the next overlay string for delivery by IT, if there is an
5440 overlay string to deliver. Called by set_iterator_to_next when the
5441 end of the current overlay string is reached. If there are more
5442 overlay strings to display, IT->string and
5443 IT->current.overlay_string_index are set appropriately here.
5444 Otherwise IT->string is set to nil. */
5445
5446 static void
5447 next_overlay_string (struct it *it)
5448 {
5449 ++it->current.overlay_string_index;
5450 if (it->current.overlay_string_index == it->n_overlay_strings)
5451 {
5452 /* No more overlay strings. Restore IT's settings to what
5453 they were before overlay strings were processed, and
5454 continue to deliver from current_buffer. */
5455
5456 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5457 pop_it (it);
5458 eassert (it->sp > 0
5459 || (NILP (it->string)
5460 && it->method == GET_FROM_BUFFER
5461 && it->stop_charpos >= BEGV
5462 && it->stop_charpos <= it->end_charpos));
5463 it->current.overlay_string_index = -1;
5464 it->n_overlay_strings = 0;
5465 it->overlay_strings_charpos = -1;
5466 /* If there's an empty display string on the stack, pop the
5467 stack, to resync the bidi iterator with IT's position. Such
5468 empty strings are pushed onto the stack in
5469 get_overlay_strings_1. */
5470 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5471 pop_it (it);
5472
5473 /* If we're at the end of the buffer, record that we have
5474 processed the overlay strings there already, so that
5475 next_element_from_buffer doesn't try it again. */
5476 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5477 it->overlay_strings_at_end_processed_p = 1;
5478 }
5479 else
5480 {
5481 /* There are more overlay strings to process. If
5482 IT->current.overlay_string_index has advanced to a position
5483 where we must load IT->overlay_strings with more strings, do
5484 it. We must load at the IT->overlay_strings_charpos where
5485 IT->n_overlay_strings was originally computed; when invisible
5486 text is present, this might not be IT_CHARPOS (Bug#7016). */
5487 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5488
5489 if (it->current.overlay_string_index && i == 0)
5490 load_overlay_strings (it, it->overlay_strings_charpos);
5491
5492 /* Initialize IT to deliver display elements from the overlay
5493 string. */
5494 it->string = it->overlay_strings[i];
5495 it->multibyte_p = STRING_MULTIBYTE (it->string);
5496 SET_TEXT_POS (it->current.string_pos, 0, 0);
5497 it->method = GET_FROM_STRING;
5498 it->stop_charpos = 0;
5499 it->end_charpos = SCHARS (it->string);
5500 if (it->cmp_it.stop_pos >= 0)
5501 it->cmp_it.stop_pos = 0;
5502 it->prev_stop = 0;
5503 it->base_level_stop = 0;
5504
5505 /* Set up the bidi iterator for this overlay string. */
5506 if (it->bidi_p)
5507 {
5508 it->bidi_it.string.lstring = it->string;
5509 it->bidi_it.string.s = NULL;
5510 it->bidi_it.string.schars = SCHARS (it->string);
5511 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5512 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5513 it->bidi_it.string.unibyte = !it->multibyte_p;
5514 it->bidi_it.w = it->w;
5515 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5516 }
5517 }
5518
5519 CHECK_IT (it);
5520 }
5521
5522
5523 /* Compare two overlay_entry structures E1 and E2. Used as a
5524 comparison function for qsort in load_overlay_strings. Overlay
5525 strings for the same position are sorted so that
5526
5527 1. All after-strings come in front of before-strings, except
5528 when they come from the same overlay.
5529
5530 2. Within after-strings, strings are sorted so that overlay strings
5531 from overlays with higher priorities come first.
5532
5533 2. Within before-strings, strings are sorted so that overlay
5534 strings from overlays with higher priorities come last.
5535
5536 Value is analogous to strcmp. */
5537
5538
5539 static int
5540 compare_overlay_entries (const void *e1, const void *e2)
5541 {
5542 struct overlay_entry const *entry1 = e1;
5543 struct overlay_entry const *entry2 = e2;
5544 int result;
5545
5546 if (entry1->after_string_p != entry2->after_string_p)
5547 {
5548 /* Let after-strings appear in front of before-strings if
5549 they come from different overlays. */
5550 if (EQ (entry1->overlay, entry2->overlay))
5551 result = entry1->after_string_p ? 1 : -1;
5552 else
5553 result = entry1->after_string_p ? -1 : 1;
5554 }
5555 else if (entry1->priority != entry2->priority)
5556 {
5557 if (entry1->after_string_p)
5558 /* After-strings sorted in order of decreasing priority. */
5559 result = entry2->priority < entry1->priority ? -1 : 1;
5560 else
5561 /* Before-strings sorted in order of increasing priority. */
5562 result = entry1->priority < entry2->priority ? -1 : 1;
5563 }
5564 else
5565 result = 0;
5566
5567 return result;
5568 }
5569
5570
5571 /* Load the vector IT->overlay_strings with overlay strings from IT's
5572 current buffer position, or from CHARPOS if that is > 0. Set
5573 IT->n_overlays to the total number of overlay strings found.
5574
5575 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5576 a time. On entry into load_overlay_strings,
5577 IT->current.overlay_string_index gives the number of overlay
5578 strings that have already been loaded by previous calls to this
5579 function.
5580
5581 IT->add_overlay_start contains an additional overlay start
5582 position to consider for taking overlay strings from, if non-zero.
5583 This position comes into play when the overlay has an `invisible'
5584 property, and both before and after-strings. When we've skipped to
5585 the end of the overlay, because of its `invisible' property, we
5586 nevertheless want its before-string to appear.
5587 IT->add_overlay_start will contain the overlay start position
5588 in this case.
5589
5590 Overlay strings are sorted so that after-string strings come in
5591 front of before-string strings. Within before and after-strings,
5592 strings are sorted by overlay priority. See also function
5593 compare_overlay_entries. */
5594
5595 static void
5596 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5597 {
5598 Lisp_Object overlay, window, str, invisible;
5599 struct Lisp_Overlay *ov;
5600 ptrdiff_t start, end;
5601 ptrdiff_t size = 20;
5602 ptrdiff_t n = 0, i, j;
5603 int invis_p;
5604 struct overlay_entry *entries = alloca (size * sizeof *entries);
5605 USE_SAFE_ALLOCA;
5606
5607 if (charpos <= 0)
5608 charpos = IT_CHARPOS (*it);
5609
5610 /* Append the overlay string STRING of overlay OVERLAY to vector
5611 `entries' which has size `size' and currently contains `n'
5612 elements. AFTER_P non-zero means STRING is an after-string of
5613 OVERLAY. */
5614 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5615 do \
5616 { \
5617 Lisp_Object priority; \
5618 \
5619 if (n == size) \
5620 { \
5621 struct overlay_entry *old = entries; \
5622 SAFE_NALLOCA (entries, 2, size); \
5623 memcpy (entries, old, size * sizeof *entries); \
5624 size *= 2; \
5625 } \
5626 \
5627 entries[n].string = (STRING); \
5628 entries[n].overlay = (OVERLAY); \
5629 priority = Foverlay_get ((OVERLAY), Qpriority); \
5630 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5631 entries[n].after_string_p = (AFTER_P); \
5632 ++n; \
5633 } \
5634 while (0)
5635
5636 /* Process overlay before the overlay center. */
5637 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5638 {
5639 XSETMISC (overlay, ov);
5640 eassert (OVERLAYP (overlay));
5641 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5642 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5643
5644 if (end < charpos)
5645 break;
5646
5647 /* Skip this overlay if it doesn't start or end at IT's current
5648 position. */
5649 if (end != charpos && start != charpos)
5650 continue;
5651
5652 /* Skip this overlay if it doesn't apply to IT->w. */
5653 window = Foverlay_get (overlay, Qwindow);
5654 if (WINDOWP (window) && XWINDOW (window) != it->w)
5655 continue;
5656
5657 /* If the text ``under'' the overlay is invisible, both before-
5658 and after-strings from this overlay are visible; start and
5659 end position are indistinguishable. */
5660 invisible = Foverlay_get (overlay, Qinvisible);
5661 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5662
5663 /* If overlay has a non-empty before-string, record it. */
5664 if ((start == charpos || (end == charpos && invis_p))
5665 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5666 && SCHARS (str))
5667 RECORD_OVERLAY_STRING (overlay, str, 0);
5668
5669 /* If overlay has a non-empty after-string, record it. */
5670 if ((end == charpos || (start == charpos && invis_p))
5671 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5672 && SCHARS (str))
5673 RECORD_OVERLAY_STRING (overlay, str, 1);
5674 }
5675
5676 /* Process overlays after the overlay center. */
5677 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5678 {
5679 XSETMISC (overlay, ov);
5680 eassert (OVERLAYP (overlay));
5681 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5682 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5683
5684 if (start > charpos)
5685 break;
5686
5687 /* Skip this overlay if it doesn't start or end at IT's current
5688 position. */
5689 if (end != charpos && start != charpos)
5690 continue;
5691
5692 /* Skip this overlay if it doesn't apply to IT->w. */
5693 window = Foverlay_get (overlay, Qwindow);
5694 if (WINDOWP (window) && XWINDOW (window) != it->w)
5695 continue;
5696
5697 /* If the text ``under'' the overlay is invisible, it has a zero
5698 dimension, and both before- and after-strings apply. */
5699 invisible = Foverlay_get (overlay, Qinvisible);
5700 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5701
5702 /* If overlay has a non-empty before-string, record it. */
5703 if ((start == charpos || (end == charpos && invis_p))
5704 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5705 && SCHARS (str))
5706 RECORD_OVERLAY_STRING (overlay, str, 0);
5707
5708 /* If overlay has a non-empty after-string, record it. */
5709 if ((end == charpos || (start == charpos && invis_p))
5710 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5711 && SCHARS (str))
5712 RECORD_OVERLAY_STRING (overlay, str, 1);
5713 }
5714
5715 #undef RECORD_OVERLAY_STRING
5716
5717 /* Sort entries. */
5718 if (n > 1)
5719 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5720
5721 /* Record number of overlay strings, and where we computed it. */
5722 it->n_overlay_strings = n;
5723 it->overlay_strings_charpos = charpos;
5724
5725 /* IT->current.overlay_string_index is the number of overlay strings
5726 that have already been consumed by IT. Copy some of the
5727 remaining overlay strings to IT->overlay_strings. */
5728 i = 0;
5729 j = it->current.overlay_string_index;
5730 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5731 {
5732 it->overlay_strings[i] = entries[j].string;
5733 it->string_overlays[i++] = entries[j++].overlay;
5734 }
5735
5736 CHECK_IT (it);
5737 SAFE_FREE ();
5738 }
5739
5740
5741 /* Get the first chunk of overlay strings at IT's current buffer
5742 position, or at CHARPOS if that is > 0. Value is non-zero if at
5743 least one overlay string was found. */
5744
5745 static int
5746 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5747 {
5748 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5749 process. This fills IT->overlay_strings with strings, and sets
5750 IT->n_overlay_strings to the total number of strings to process.
5751 IT->pos.overlay_string_index has to be set temporarily to zero
5752 because load_overlay_strings needs this; it must be set to -1
5753 when no overlay strings are found because a zero value would
5754 indicate a position in the first overlay string. */
5755 it->current.overlay_string_index = 0;
5756 load_overlay_strings (it, charpos);
5757
5758 /* If we found overlay strings, set up IT to deliver display
5759 elements from the first one. Otherwise set up IT to deliver
5760 from current_buffer. */
5761 if (it->n_overlay_strings)
5762 {
5763 /* Make sure we know settings in current_buffer, so that we can
5764 restore meaningful values when we're done with the overlay
5765 strings. */
5766 if (compute_stop_p)
5767 compute_stop_pos (it);
5768 eassert (it->face_id >= 0);
5769
5770 /* Save IT's settings. They are restored after all overlay
5771 strings have been processed. */
5772 eassert (!compute_stop_p || it->sp == 0);
5773
5774 /* When called from handle_stop, there might be an empty display
5775 string loaded. In that case, don't bother saving it. But
5776 don't use this optimization with the bidi iterator, since we
5777 need the corresponding pop_it call to resync the bidi
5778 iterator's position with IT's position, after we are done
5779 with the overlay strings. (The corresponding call to pop_it
5780 in case of an empty display string is in
5781 next_overlay_string.) */
5782 if (!(!it->bidi_p
5783 && STRINGP (it->string) && !SCHARS (it->string)))
5784 push_it (it, NULL);
5785
5786 /* Set up IT to deliver display elements from the first overlay
5787 string. */
5788 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5789 it->string = it->overlay_strings[0];
5790 it->from_overlay = Qnil;
5791 it->stop_charpos = 0;
5792 eassert (STRINGP (it->string));
5793 it->end_charpos = SCHARS (it->string);
5794 it->prev_stop = 0;
5795 it->base_level_stop = 0;
5796 it->multibyte_p = STRING_MULTIBYTE (it->string);
5797 it->method = GET_FROM_STRING;
5798 it->from_disp_prop_p = 0;
5799
5800 /* Force paragraph direction to be that of the parent
5801 buffer. */
5802 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5803 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5804 else
5805 it->paragraph_embedding = L2R;
5806
5807 /* Set up the bidi iterator for this overlay string. */
5808 if (it->bidi_p)
5809 {
5810 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5811
5812 it->bidi_it.string.lstring = it->string;
5813 it->bidi_it.string.s = NULL;
5814 it->bidi_it.string.schars = SCHARS (it->string);
5815 it->bidi_it.string.bufpos = pos;
5816 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5817 it->bidi_it.string.unibyte = !it->multibyte_p;
5818 it->bidi_it.w = it->w;
5819 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5820 }
5821 return 1;
5822 }
5823
5824 it->current.overlay_string_index = -1;
5825 return 0;
5826 }
5827
5828 static int
5829 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5830 {
5831 it->string = Qnil;
5832 it->method = GET_FROM_BUFFER;
5833
5834 (void) get_overlay_strings_1 (it, charpos, 1);
5835
5836 CHECK_IT (it);
5837
5838 /* Value is non-zero if we found at least one overlay string. */
5839 return STRINGP (it->string);
5840 }
5841
5842
5843 \f
5844 /***********************************************************************
5845 Saving and restoring state
5846 ***********************************************************************/
5847
5848 /* Save current settings of IT on IT->stack. Called, for example,
5849 before setting up IT for an overlay string, to be able to restore
5850 IT's settings to what they were after the overlay string has been
5851 processed. If POSITION is non-NULL, it is the position to save on
5852 the stack instead of IT->position. */
5853
5854 static void
5855 push_it (struct it *it, struct text_pos *position)
5856 {
5857 struct iterator_stack_entry *p;
5858
5859 eassert (it->sp < IT_STACK_SIZE);
5860 p = it->stack + it->sp;
5861
5862 p->stop_charpos = it->stop_charpos;
5863 p->prev_stop = it->prev_stop;
5864 p->base_level_stop = it->base_level_stop;
5865 p->cmp_it = it->cmp_it;
5866 eassert (it->face_id >= 0);
5867 p->face_id = it->face_id;
5868 p->string = it->string;
5869 p->method = it->method;
5870 p->from_overlay = it->from_overlay;
5871 switch (p->method)
5872 {
5873 case GET_FROM_IMAGE:
5874 p->u.image.object = it->object;
5875 p->u.image.image_id = it->image_id;
5876 p->u.image.slice = it->slice;
5877 break;
5878 case GET_FROM_STRETCH:
5879 p->u.stretch.object = it->object;
5880 break;
5881 #ifdef HAVE_XWIDGETS
5882 case GET_FROM_XWIDGET:
5883 p->u.xwidget.object = it->object;
5884 break;
5885 #endif
5886 }
5887 p->position = position ? *position : it->position;
5888 p->current = it->current;
5889 p->end_charpos = it->end_charpos;
5890 p->string_nchars = it->string_nchars;
5891 p->area = it->area;
5892 p->multibyte_p = it->multibyte_p;
5893 p->avoid_cursor_p = it->avoid_cursor_p;
5894 p->space_width = it->space_width;
5895 p->font_height = it->font_height;
5896 p->voffset = it->voffset;
5897 p->string_from_display_prop_p = it->string_from_display_prop_p;
5898 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5899 p->display_ellipsis_p = 0;
5900 p->line_wrap = it->line_wrap;
5901 p->bidi_p = it->bidi_p;
5902 p->paragraph_embedding = it->paragraph_embedding;
5903 p->from_disp_prop_p = it->from_disp_prop_p;
5904 ++it->sp;
5905
5906 /* Save the state of the bidi iterator as well. */
5907 if (it->bidi_p)
5908 bidi_push_it (&it->bidi_it);
5909 }
5910
5911 static void
5912 iterate_out_of_display_property (struct it *it)
5913 {
5914 int buffer_p = !STRINGP (it->string);
5915 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5916 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5917
5918 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5919
5920 /* Maybe initialize paragraph direction. If we are at the beginning
5921 of a new paragraph, next_element_from_buffer may not have a
5922 chance to do that. */
5923 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5924 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5925 /* prev_stop can be zero, so check against BEGV as well. */
5926 while (it->bidi_it.charpos >= bob
5927 && it->prev_stop <= it->bidi_it.charpos
5928 && it->bidi_it.charpos < CHARPOS (it->position)
5929 && it->bidi_it.charpos < eob)
5930 bidi_move_to_visually_next (&it->bidi_it);
5931 /* Record the stop_pos we just crossed, for when we cross it
5932 back, maybe. */
5933 if (it->bidi_it.charpos > CHARPOS (it->position))
5934 it->prev_stop = CHARPOS (it->position);
5935 /* If we ended up not where pop_it put us, resync IT's
5936 positional members with the bidi iterator. */
5937 if (it->bidi_it.charpos != CHARPOS (it->position))
5938 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5939 if (buffer_p)
5940 it->current.pos = it->position;
5941 else
5942 it->current.string_pos = it->position;
5943 }
5944
5945 /* Restore IT's settings from IT->stack. Called, for example, when no
5946 more overlay strings must be processed, and we return to delivering
5947 display elements from a buffer, or when the end of a string from a
5948 `display' property is reached and we return to delivering display
5949 elements from an overlay string, or from a buffer. */
5950
5951 static void
5952 pop_it (struct it *it)
5953 {
5954 struct iterator_stack_entry *p;
5955 int from_display_prop = it->from_disp_prop_p;
5956
5957 eassert (it->sp > 0);
5958 --it->sp;
5959 p = it->stack + it->sp;
5960 it->stop_charpos = p->stop_charpos;
5961 it->prev_stop = p->prev_stop;
5962 it->base_level_stop = p->base_level_stop;
5963 it->cmp_it = p->cmp_it;
5964 it->face_id = p->face_id;
5965 it->current = p->current;
5966 it->position = p->position;
5967 it->string = p->string;
5968 it->from_overlay = p->from_overlay;
5969 if (NILP (it->string))
5970 SET_TEXT_POS (it->current.string_pos, -1, -1);
5971 it->method = p->method;
5972 switch (it->method)
5973 {
5974 case GET_FROM_IMAGE:
5975 it->image_id = p->u.image.image_id;
5976 it->object = p->u.image.object;
5977 it->slice = p->u.image.slice;
5978 break;
5979 #ifdef HAVE_XWIDGETS
5980 case GET_FROM_XWIDGET:
5981 it->object = p->u.xwidget.object;
5982 break;
5983 #endif
5984 case GET_FROM_STRETCH:
5985 it->object = p->u.stretch.object;
5986 break;
5987 case GET_FROM_BUFFER:
5988 it->object = it->w->contents;
5989 break;
5990 case GET_FROM_STRING:
5991 it->object = it->string;
5992 break;
5993 case GET_FROM_DISPLAY_VECTOR:
5994 if (it->s)
5995 it->method = GET_FROM_C_STRING;
5996 else if (STRINGP (it->string))
5997 it->method = GET_FROM_STRING;
5998 else
5999 {
6000 it->method = GET_FROM_BUFFER;
6001 it->object = it->w->contents;
6002 }
6003 }
6004 it->end_charpos = p->end_charpos;
6005 it->string_nchars = p->string_nchars;
6006 it->area = p->area;
6007 it->multibyte_p = p->multibyte_p;
6008 it->avoid_cursor_p = p->avoid_cursor_p;
6009 it->space_width = p->space_width;
6010 it->font_height = p->font_height;
6011 it->voffset = p->voffset;
6012 it->string_from_display_prop_p = p->string_from_display_prop_p;
6013 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6014 it->line_wrap = p->line_wrap;
6015 it->bidi_p = p->bidi_p;
6016 it->paragraph_embedding = p->paragraph_embedding;
6017 it->from_disp_prop_p = p->from_disp_prop_p;
6018 if (it->bidi_p)
6019 {
6020 bidi_pop_it (&it->bidi_it);
6021 /* Bidi-iterate until we get out of the portion of text, if any,
6022 covered by a `display' text property or by an overlay with
6023 `display' property. (We cannot just jump there, because the
6024 internal coherency of the bidi iterator state can not be
6025 preserved across such jumps.) We also must determine the
6026 paragraph base direction if the overlay we just processed is
6027 at the beginning of a new paragraph. */
6028 if (from_display_prop
6029 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6030 iterate_out_of_display_property (it);
6031
6032 eassert ((BUFFERP (it->object)
6033 && IT_CHARPOS (*it) == it->bidi_it.charpos
6034 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6035 || (STRINGP (it->object)
6036 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6037 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6038 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6039 }
6040 }
6041
6042
6043 \f
6044 /***********************************************************************
6045 Moving over lines
6046 ***********************************************************************/
6047
6048 /* Set IT's current position to the previous line start. */
6049
6050 static void
6051 back_to_previous_line_start (struct it *it)
6052 {
6053 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6054
6055 DEC_BOTH (cp, bp);
6056 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6057 }
6058
6059
6060 /* Move IT to the next line start.
6061
6062 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6063 we skipped over part of the text (as opposed to moving the iterator
6064 continuously over the text). Otherwise, don't change the value
6065 of *SKIPPED_P.
6066
6067 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6068 iterator on the newline, if it was found.
6069
6070 Newlines may come from buffer text, overlay strings, or strings
6071 displayed via the `display' property. That's the reason we can't
6072 simply use find_newline_no_quit.
6073
6074 Note that this function may not skip over invisible text that is so
6075 because of text properties and immediately follows a newline. If
6076 it would, function reseat_at_next_visible_line_start, when called
6077 from set_iterator_to_next, would effectively make invisible
6078 characters following a newline part of the wrong glyph row, which
6079 leads to wrong cursor motion. */
6080
6081 static int
6082 forward_to_next_line_start (struct it *it, int *skipped_p,
6083 struct bidi_it *bidi_it_prev)
6084 {
6085 ptrdiff_t old_selective;
6086 int newline_found_p, n;
6087 const int MAX_NEWLINE_DISTANCE = 500;
6088
6089 /* If already on a newline, just consume it to avoid unintended
6090 skipping over invisible text below. */
6091 if (it->what == IT_CHARACTER
6092 && it->c == '\n'
6093 && CHARPOS (it->position) == IT_CHARPOS (*it))
6094 {
6095 if (it->bidi_p && bidi_it_prev)
6096 *bidi_it_prev = it->bidi_it;
6097 set_iterator_to_next (it, 0);
6098 it->c = 0;
6099 return 1;
6100 }
6101
6102 /* Don't handle selective display in the following. It's (a)
6103 unnecessary because it's done by the caller, and (b) leads to an
6104 infinite recursion because next_element_from_ellipsis indirectly
6105 calls this function. */
6106 old_selective = it->selective;
6107 it->selective = 0;
6108
6109 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6110 from buffer text. */
6111 for (n = newline_found_p = 0;
6112 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6113 n += STRINGP (it->string) ? 0 : 1)
6114 {
6115 if (!get_next_display_element (it))
6116 return 0;
6117 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6118 if (newline_found_p && it->bidi_p && bidi_it_prev)
6119 *bidi_it_prev = it->bidi_it;
6120 set_iterator_to_next (it, 0);
6121 }
6122
6123 /* If we didn't find a newline near enough, see if we can use a
6124 short-cut. */
6125 if (!newline_found_p)
6126 {
6127 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6128 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6129 1, &bytepos);
6130 Lisp_Object pos;
6131
6132 eassert (!STRINGP (it->string));
6133
6134 /* If there isn't any `display' property in sight, and no
6135 overlays, we can just use the position of the newline in
6136 buffer text. */
6137 if (it->stop_charpos >= limit
6138 || ((pos = Fnext_single_property_change (make_number (start),
6139 Qdisplay, Qnil,
6140 make_number (limit)),
6141 NILP (pos))
6142 && next_overlay_change (start) == ZV))
6143 {
6144 if (!it->bidi_p)
6145 {
6146 IT_CHARPOS (*it) = limit;
6147 IT_BYTEPOS (*it) = bytepos;
6148 }
6149 else
6150 {
6151 struct bidi_it bprev;
6152
6153 /* Help bidi.c avoid expensive searches for display
6154 properties and overlays, by telling it that there are
6155 none up to `limit'. */
6156 if (it->bidi_it.disp_pos < limit)
6157 {
6158 it->bidi_it.disp_pos = limit;
6159 it->bidi_it.disp_prop = 0;
6160 }
6161 do {
6162 bprev = it->bidi_it;
6163 bidi_move_to_visually_next (&it->bidi_it);
6164 } while (it->bidi_it.charpos != limit);
6165 IT_CHARPOS (*it) = limit;
6166 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6167 if (bidi_it_prev)
6168 *bidi_it_prev = bprev;
6169 }
6170 *skipped_p = newline_found_p = 1;
6171 }
6172 else
6173 {
6174 while (get_next_display_element (it)
6175 && !newline_found_p)
6176 {
6177 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6178 if (newline_found_p && it->bidi_p && bidi_it_prev)
6179 *bidi_it_prev = it->bidi_it;
6180 set_iterator_to_next (it, 0);
6181 }
6182 }
6183 }
6184
6185 it->selective = old_selective;
6186 return newline_found_p;
6187 }
6188
6189
6190 /* Set IT's current position to the previous visible line start. Skip
6191 invisible text that is so either due to text properties or due to
6192 selective display. Caution: this does not change IT->current_x and
6193 IT->hpos. */
6194
6195 static void
6196 back_to_previous_visible_line_start (struct it *it)
6197 {
6198 while (IT_CHARPOS (*it) > BEGV)
6199 {
6200 back_to_previous_line_start (it);
6201
6202 if (IT_CHARPOS (*it) <= BEGV)
6203 break;
6204
6205 /* If selective > 0, then lines indented more than its value are
6206 invisible. */
6207 if (it->selective > 0
6208 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6209 it->selective))
6210 continue;
6211
6212 /* Check the newline before point for invisibility. */
6213 {
6214 Lisp_Object prop;
6215 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6216 Qinvisible, it->window);
6217 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6218 continue;
6219 }
6220
6221 if (IT_CHARPOS (*it) <= BEGV)
6222 break;
6223
6224 {
6225 struct it it2;
6226 void *it2data = NULL;
6227 ptrdiff_t pos;
6228 ptrdiff_t beg, end;
6229 Lisp_Object val, overlay;
6230
6231 SAVE_IT (it2, *it, it2data);
6232
6233 /* If newline is part of a composition, continue from start of composition */
6234 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6235 && beg < IT_CHARPOS (*it))
6236 goto replaced;
6237
6238 /* If newline is replaced by a display property, find start of overlay
6239 or interval and continue search from that point. */
6240 pos = --IT_CHARPOS (it2);
6241 --IT_BYTEPOS (it2);
6242 it2.sp = 0;
6243 bidi_unshelve_cache (NULL, 0);
6244 it2.string_from_display_prop_p = 0;
6245 it2.from_disp_prop_p = 0;
6246 if (handle_display_prop (&it2) == HANDLED_RETURN
6247 && !NILP (val = get_char_property_and_overlay
6248 (make_number (pos), Qdisplay, Qnil, &overlay))
6249 && (OVERLAYP (overlay)
6250 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6251 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6252 {
6253 RESTORE_IT (it, it, it2data);
6254 goto replaced;
6255 }
6256
6257 /* Newline is not replaced by anything -- so we are done. */
6258 RESTORE_IT (it, it, it2data);
6259 break;
6260
6261 replaced:
6262 if (beg < BEGV)
6263 beg = BEGV;
6264 IT_CHARPOS (*it) = beg;
6265 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6266 }
6267 }
6268
6269 it->continuation_lines_width = 0;
6270
6271 eassert (IT_CHARPOS (*it) >= BEGV);
6272 eassert (IT_CHARPOS (*it) == BEGV
6273 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6274 CHECK_IT (it);
6275 }
6276
6277
6278 /* Reseat iterator IT at the previous visible line start. Skip
6279 invisible text that is so either due to text properties or due to
6280 selective display. At the end, update IT's overlay information,
6281 face information etc. */
6282
6283 void
6284 reseat_at_previous_visible_line_start (struct it *it)
6285 {
6286 back_to_previous_visible_line_start (it);
6287 reseat (it, it->current.pos, 1);
6288 CHECK_IT (it);
6289 }
6290
6291
6292 /* Reseat iterator IT on the next visible line start in the current
6293 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6294 preceding the line start. Skip over invisible text that is so
6295 because of selective display. Compute faces, overlays etc at the
6296 new position. Note that this function does not skip over text that
6297 is invisible because of text properties. */
6298
6299 static void
6300 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6301 {
6302 int newline_found_p, skipped_p = 0;
6303 struct bidi_it bidi_it_prev;
6304
6305 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6306
6307 /* Skip over lines that are invisible because they are indented
6308 more than the value of IT->selective. */
6309 if (it->selective > 0)
6310 while (IT_CHARPOS (*it) < ZV
6311 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6312 it->selective))
6313 {
6314 eassert (IT_BYTEPOS (*it) == BEGV
6315 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6316 newline_found_p =
6317 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6318 }
6319
6320 /* Position on the newline if that's what's requested. */
6321 if (on_newline_p && newline_found_p)
6322 {
6323 if (STRINGP (it->string))
6324 {
6325 if (IT_STRING_CHARPOS (*it) > 0)
6326 {
6327 if (!it->bidi_p)
6328 {
6329 --IT_STRING_CHARPOS (*it);
6330 --IT_STRING_BYTEPOS (*it);
6331 }
6332 else
6333 {
6334 /* We need to restore the bidi iterator to the state
6335 it had on the newline, and resync the IT's
6336 position with that. */
6337 it->bidi_it = bidi_it_prev;
6338 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6339 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6340 }
6341 }
6342 }
6343 else if (IT_CHARPOS (*it) > BEGV)
6344 {
6345 if (!it->bidi_p)
6346 {
6347 --IT_CHARPOS (*it);
6348 --IT_BYTEPOS (*it);
6349 }
6350 else
6351 {
6352 /* We need to restore the bidi iterator to the state it
6353 had on the newline and resync IT with that. */
6354 it->bidi_it = bidi_it_prev;
6355 IT_CHARPOS (*it) = it->bidi_it.charpos;
6356 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6357 }
6358 reseat (it, it->current.pos, 0);
6359 }
6360 }
6361 else if (skipped_p)
6362 reseat (it, it->current.pos, 0);
6363
6364 CHECK_IT (it);
6365 }
6366
6367
6368 \f
6369 /***********************************************************************
6370 Changing an iterator's position
6371 ***********************************************************************/
6372
6373 /* Change IT's current position to POS in current_buffer. If FORCE_P
6374 is non-zero, always check for text properties at the new position.
6375 Otherwise, text properties are only looked up if POS >=
6376 IT->check_charpos of a property. */
6377
6378 static void
6379 reseat (struct it *it, struct text_pos pos, int force_p)
6380 {
6381 ptrdiff_t original_pos = IT_CHARPOS (*it);
6382
6383 reseat_1 (it, pos, 0);
6384
6385 /* Determine where to check text properties. Avoid doing it
6386 where possible because text property lookup is very expensive. */
6387 if (force_p
6388 || CHARPOS (pos) > it->stop_charpos
6389 || CHARPOS (pos) < original_pos)
6390 {
6391 if (it->bidi_p)
6392 {
6393 /* For bidi iteration, we need to prime prev_stop and
6394 base_level_stop with our best estimations. */
6395 /* Implementation note: Of course, POS is not necessarily a
6396 stop position, so assigning prev_pos to it is a lie; we
6397 should have called compute_stop_backwards. However, if
6398 the current buffer does not include any R2L characters,
6399 that call would be a waste of cycles, because the
6400 iterator will never move back, and thus never cross this
6401 "fake" stop position. So we delay that backward search
6402 until the time we really need it, in next_element_from_buffer. */
6403 if (CHARPOS (pos) != it->prev_stop)
6404 it->prev_stop = CHARPOS (pos);
6405 if (CHARPOS (pos) < it->base_level_stop)
6406 it->base_level_stop = 0; /* meaning it's unknown */
6407 handle_stop (it);
6408 }
6409 else
6410 {
6411 handle_stop (it);
6412 it->prev_stop = it->base_level_stop = 0;
6413 }
6414
6415 }
6416
6417 CHECK_IT (it);
6418 }
6419
6420
6421 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6422 IT->stop_pos to POS, also. */
6423
6424 static void
6425 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6426 {
6427 /* Don't call this function when scanning a C string. */
6428 eassert (it->s == NULL);
6429
6430 /* POS must be a reasonable value. */
6431 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6432
6433 it->current.pos = it->position = pos;
6434 it->end_charpos = ZV;
6435 it->dpvec = NULL;
6436 it->current.dpvec_index = -1;
6437 it->current.overlay_string_index = -1;
6438 IT_STRING_CHARPOS (*it) = -1;
6439 IT_STRING_BYTEPOS (*it) = -1;
6440 it->string = Qnil;
6441 it->method = GET_FROM_BUFFER;
6442 it->object = it->w->contents;
6443 it->area = TEXT_AREA;
6444 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6445 it->sp = 0;
6446 it->string_from_display_prop_p = 0;
6447 it->string_from_prefix_prop_p = 0;
6448
6449 it->from_disp_prop_p = 0;
6450 it->face_before_selective_p = 0;
6451 if (it->bidi_p)
6452 {
6453 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6454 &it->bidi_it);
6455 bidi_unshelve_cache (NULL, 0);
6456 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6457 it->bidi_it.string.s = NULL;
6458 it->bidi_it.string.lstring = Qnil;
6459 it->bidi_it.string.bufpos = 0;
6460 it->bidi_it.string.unibyte = 0;
6461 it->bidi_it.w = it->w;
6462 }
6463
6464 if (set_stop_p)
6465 {
6466 it->stop_charpos = CHARPOS (pos);
6467 it->base_level_stop = CHARPOS (pos);
6468 }
6469 /* This make the information stored in it->cmp_it invalidate. */
6470 it->cmp_it.id = -1;
6471 }
6472
6473
6474 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6475 If S is non-null, it is a C string to iterate over. Otherwise,
6476 STRING gives a Lisp string to iterate over.
6477
6478 If PRECISION > 0, don't return more then PRECISION number of
6479 characters from the string.
6480
6481 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6482 characters have been returned. FIELD_WIDTH < 0 means an infinite
6483 field width.
6484
6485 MULTIBYTE = 0 means disable processing of multibyte characters,
6486 MULTIBYTE > 0 means enable it,
6487 MULTIBYTE < 0 means use IT->multibyte_p.
6488
6489 IT must be initialized via a prior call to init_iterator before
6490 calling this function. */
6491
6492 static void
6493 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6494 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6495 int multibyte)
6496 {
6497 /* No region in strings. */
6498 it->region_beg_charpos = it->region_end_charpos = -1;
6499
6500 /* No text property checks performed by default, but see below. */
6501 it->stop_charpos = -1;
6502
6503 /* Set iterator position and end position. */
6504 memset (&it->current, 0, sizeof it->current);
6505 it->current.overlay_string_index = -1;
6506 it->current.dpvec_index = -1;
6507 eassert (charpos >= 0);
6508
6509 /* If STRING is specified, use its multibyteness, otherwise use the
6510 setting of MULTIBYTE, if specified. */
6511 if (multibyte >= 0)
6512 it->multibyte_p = multibyte > 0;
6513
6514 /* Bidirectional reordering of strings is controlled by the default
6515 value of bidi-display-reordering. Don't try to reorder while
6516 loading loadup.el, as the necessary character property tables are
6517 not yet available. */
6518 it->bidi_p =
6519 NILP (Vpurify_flag)
6520 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6521
6522 if (s == NULL)
6523 {
6524 eassert (STRINGP (string));
6525 it->string = string;
6526 it->s = NULL;
6527 it->end_charpos = it->string_nchars = SCHARS (string);
6528 it->method = GET_FROM_STRING;
6529 it->current.string_pos = string_pos (charpos, string);
6530
6531 if (it->bidi_p)
6532 {
6533 it->bidi_it.string.lstring = string;
6534 it->bidi_it.string.s = NULL;
6535 it->bidi_it.string.schars = it->end_charpos;
6536 it->bidi_it.string.bufpos = 0;
6537 it->bidi_it.string.from_disp_str = 0;
6538 it->bidi_it.string.unibyte = !it->multibyte_p;
6539 it->bidi_it.w = it->w;
6540 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6541 FRAME_WINDOW_P (it->f), &it->bidi_it);
6542 }
6543 }
6544 else
6545 {
6546 it->s = (const unsigned char *) s;
6547 it->string = Qnil;
6548
6549 /* Note that we use IT->current.pos, not it->current.string_pos,
6550 for displaying C strings. */
6551 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6552 if (it->multibyte_p)
6553 {
6554 it->current.pos = c_string_pos (charpos, s, 1);
6555 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6556 }
6557 else
6558 {
6559 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6560 it->end_charpos = it->string_nchars = strlen (s);
6561 }
6562
6563 if (it->bidi_p)
6564 {
6565 it->bidi_it.string.lstring = Qnil;
6566 it->bidi_it.string.s = (const unsigned char *) s;
6567 it->bidi_it.string.schars = it->end_charpos;
6568 it->bidi_it.string.bufpos = 0;
6569 it->bidi_it.string.from_disp_str = 0;
6570 it->bidi_it.string.unibyte = !it->multibyte_p;
6571 it->bidi_it.w = it->w;
6572 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6573 &it->bidi_it);
6574 }
6575 it->method = GET_FROM_C_STRING;
6576 }
6577
6578 /* PRECISION > 0 means don't return more than PRECISION characters
6579 from the string. */
6580 if (precision > 0 && it->end_charpos - charpos > precision)
6581 {
6582 it->end_charpos = it->string_nchars = charpos + precision;
6583 if (it->bidi_p)
6584 it->bidi_it.string.schars = it->end_charpos;
6585 }
6586
6587 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6588 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6589 FIELD_WIDTH < 0 means infinite field width. This is useful for
6590 padding with `-' at the end of a mode line. */
6591 if (field_width < 0)
6592 field_width = INFINITY;
6593 /* Implementation note: We deliberately don't enlarge
6594 it->bidi_it.string.schars here to fit it->end_charpos, because
6595 the bidi iterator cannot produce characters out of thin air. */
6596 if (field_width > it->end_charpos - charpos)
6597 it->end_charpos = charpos + field_width;
6598
6599 /* Use the standard display table for displaying strings. */
6600 if (DISP_TABLE_P (Vstandard_display_table))
6601 it->dp = XCHAR_TABLE (Vstandard_display_table);
6602
6603 it->stop_charpos = charpos;
6604 it->prev_stop = charpos;
6605 it->base_level_stop = 0;
6606 if (it->bidi_p)
6607 {
6608 it->bidi_it.first_elt = 1;
6609 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6610 it->bidi_it.disp_pos = -1;
6611 }
6612 if (s == NULL && it->multibyte_p)
6613 {
6614 ptrdiff_t endpos = SCHARS (it->string);
6615 if (endpos > it->end_charpos)
6616 endpos = it->end_charpos;
6617 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6618 it->string);
6619 }
6620 CHECK_IT (it);
6621 }
6622
6623
6624 \f
6625 /***********************************************************************
6626 Iteration
6627 ***********************************************************************/
6628
6629 /* Map enum it_method value to corresponding next_element_from_* function. */
6630
6631 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6632 {
6633 next_element_from_buffer,
6634 next_element_from_display_vector,
6635 next_element_from_string,
6636 next_element_from_c_string,
6637 next_element_from_image,
6638 next_element_from_stretch
6639 #ifdef HAVE_XWIDGETS
6640 ,next_element_from_xwidget
6641 #endif
6642 };
6643
6644 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6645
6646
6647 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6648 (possibly with the following characters). */
6649
6650 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6651 ((IT)->cmp_it.id >= 0 \
6652 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6653 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6654 END_CHARPOS, (IT)->w, \
6655 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6656 (IT)->string)))
6657
6658
6659 /* Lookup the char-table Vglyphless_char_display for character C (-1
6660 if we want information for no-font case), and return the display
6661 method symbol. By side-effect, update it->what and
6662 it->glyphless_method. This function is called from
6663 get_next_display_element for each character element, and from
6664 x_produce_glyphs when no suitable font was found. */
6665
6666 Lisp_Object
6667 lookup_glyphless_char_display (int c, struct it *it)
6668 {
6669 Lisp_Object glyphless_method = Qnil;
6670
6671 if (CHAR_TABLE_P (Vglyphless_char_display)
6672 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6673 {
6674 if (c >= 0)
6675 {
6676 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6677 if (CONSP (glyphless_method))
6678 glyphless_method = FRAME_WINDOW_P (it->f)
6679 ? XCAR (glyphless_method)
6680 : XCDR (glyphless_method);
6681 }
6682 else
6683 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6684 }
6685
6686 retry:
6687 if (NILP (glyphless_method))
6688 {
6689 if (c >= 0)
6690 /* The default is to display the character by a proper font. */
6691 return Qnil;
6692 /* The default for the no-font case is to display an empty box. */
6693 glyphless_method = Qempty_box;
6694 }
6695 if (EQ (glyphless_method, Qzero_width))
6696 {
6697 if (c >= 0)
6698 return glyphless_method;
6699 /* This method can't be used for the no-font case. */
6700 glyphless_method = Qempty_box;
6701 }
6702 if (EQ (glyphless_method, Qthin_space))
6703 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6704 else if (EQ (glyphless_method, Qempty_box))
6705 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6706 else if (EQ (glyphless_method, Qhex_code))
6707 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6708 else if (STRINGP (glyphless_method))
6709 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6710 else
6711 {
6712 /* Invalid value. We use the default method. */
6713 glyphless_method = Qnil;
6714 goto retry;
6715 }
6716 it->what = IT_GLYPHLESS;
6717 return glyphless_method;
6718 }
6719
6720 /* Load IT's display element fields with information about the next
6721 display element from the current position of IT. Value is zero if
6722 end of buffer (or C string) is reached. */
6723
6724 static struct frame *last_escape_glyph_frame = NULL;
6725 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6726 static int last_escape_glyph_merged_face_id = 0;
6727
6728 struct frame *last_glyphless_glyph_frame = NULL;
6729 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6730 int last_glyphless_glyph_merged_face_id = 0;
6731
6732 static int
6733 get_next_display_element (struct it *it)
6734 {
6735 /* Non-zero means that we found a display element. Zero means that
6736 we hit the end of what we iterate over. Performance note: the
6737 function pointer `method' used here turns out to be faster than
6738 using a sequence of if-statements. */
6739 int success_p;
6740
6741 get_next:
6742 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6743
6744 if (it->what == IT_CHARACTER)
6745 {
6746 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6747 and only if (a) the resolved directionality of that character
6748 is R..." */
6749 /* FIXME: Do we need an exception for characters from display
6750 tables? */
6751 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6752 it->c = bidi_mirror_char (it->c);
6753 /* Map via display table or translate control characters.
6754 IT->c, IT->len etc. have been set to the next character by
6755 the function call above. If we have a display table, and it
6756 contains an entry for IT->c, translate it. Don't do this if
6757 IT->c itself comes from a display table, otherwise we could
6758 end up in an infinite recursion. (An alternative could be to
6759 count the recursion depth of this function and signal an
6760 error when a certain maximum depth is reached.) Is it worth
6761 it? */
6762 if (success_p && it->dpvec == NULL)
6763 {
6764 Lisp_Object dv;
6765 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6766 int nonascii_space_p = 0;
6767 int nonascii_hyphen_p = 0;
6768 int c = it->c; /* This is the character to display. */
6769
6770 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6771 {
6772 eassert (SINGLE_BYTE_CHAR_P (c));
6773 if (unibyte_display_via_language_environment)
6774 {
6775 c = DECODE_CHAR (unibyte, c);
6776 if (c < 0)
6777 c = BYTE8_TO_CHAR (it->c);
6778 }
6779 else
6780 c = BYTE8_TO_CHAR (it->c);
6781 }
6782
6783 if (it->dp
6784 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6785 VECTORP (dv)))
6786 {
6787 struct Lisp_Vector *v = XVECTOR (dv);
6788
6789 /* Return the first character from the display table
6790 entry, if not empty. If empty, don't display the
6791 current character. */
6792 if (v->header.size)
6793 {
6794 it->dpvec_char_len = it->len;
6795 it->dpvec = v->contents;
6796 it->dpend = v->contents + v->header.size;
6797 it->current.dpvec_index = 0;
6798 it->dpvec_face_id = -1;
6799 it->saved_face_id = it->face_id;
6800 it->method = GET_FROM_DISPLAY_VECTOR;
6801 it->ellipsis_p = 0;
6802 }
6803 else
6804 {
6805 set_iterator_to_next (it, 0);
6806 }
6807 goto get_next;
6808 }
6809
6810 if (! NILP (lookup_glyphless_char_display (c, it)))
6811 {
6812 if (it->what == IT_GLYPHLESS)
6813 goto done;
6814 /* Don't display this character. */
6815 set_iterator_to_next (it, 0);
6816 goto get_next;
6817 }
6818
6819 /* If `nobreak-char-display' is non-nil, we display
6820 non-ASCII spaces and hyphens specially. */
6821 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6822 {
6823 if (c == 0xA0)
6824 nonascii_space_p = 1;
6825 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6826 nonascii_hyphen_p = 1;
6827 }
6828
6829 /* Translate control characters into `\003' or `^C' form.
6830 Control characters coming from a display table entry are
6831 currently not translated because we use IT->dpvec to hold
6832 the translation. This could easily be changed but I
6833 don't believe that it is worth doing.
6834
6835 The characters handled by `nobreak-char-display' must be
6836 translated too.
6837
6838 Non-printable characters and raw-byte characters are also
6839 translated to octal form. */
6840 if (((c < ' ' || c == 127) /* ASCII control chars */
6841 ? (it->area != TEXT_AREA
6842 /* In mode line, treat \n, \t like other crl chars. */
6843 || (c != '\t'
6844 && it->glyph_row
6845 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6846 || (c != '\n' && c != '\t'))
6847 : (nonascii_space_p
6848 || nonascii_hyphen_p
6849 || CHAR_BYTE8_P (c)
6850 || ! CHAR_PRINTABLE_P (c))))
6851 {
6852 /* C is a control character, non-ASCII space/hyphen,
6853 raw-byte, or a non-printable character which must be
6854 displayed either as '\003' or as `^C' where the '\\'
6855 and '^' can be defined in the display table. Fill
6856 IT->ctl_chars with glyphs for what we have to
6857 display. Then, set IT->dpvec to these glyphs. */
6858 Lisp_Object gc;
6859 int ctl_len;
6860 int face_id;
6861 int lface_id = 0;
6862 int escape_glyph;
6863
6864 /* Handle control characters with ^. */
6865
6866 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6867 {
6868 int g;
6869
6870 g = '^'; /* default glyph for Control */
6871 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6872 if (it->dp
6873 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6874 {
6875 g = GLYPH_CODE_CHAR (gc);
6876 lface_id = GLYPH_CODE_FACE (gc);
6877 }
6878 if (lface_id)
6879 {
6880 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6881 }
6882 else if (it->f == last_escape_glyph_frame
6883 && it->face_id == last_escape_glyph_face_id)
6884 {
6885 face_id = last_escape_glyph_merged_face_id;
6886 }
6887 else
6888 {
6889 /* Merge the escape-glyph face into the current face. */
6890 face_id = merge_faces (it->f, Qescape_glyph, 0,
6891 it->face_id);
6892 last_escape_glyph_frame = it->f;
6893 last_escape_glyph_face_id = it->face_id;
6894 last_escape_glyph_merged_face_id = face_id;
6895 }
6896
6897 XSETINT (it->ctl_chars[0], g);
6898 XSETINT (it->ctl_chars[1], c ^ 0100);
6899 ctl_len = 2;
6900 goto display_control;
6901 }
6902
6903 /* Handle non-ascii space in the mode where it only gets
6904 highlighting. */
6905
6906 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6907 {
6908 /* Merge `nobreak-space' into the current face. */
6909 face_id = merge_faces (it->f, Qnobreak_space, 0,
6910 it->face_id);
6911 XSETINT (it->ctl_chars[0], ' ');
6912 ctl_len = 1;
6913 goto display_control;
6914 }
6915
6916 /* Handle sequences that start with the "escape glyph". */
6917
6918 /* the default escape glyph is \. */
6919 escape_glyph = '\\';
6920
6921 if (it->dp
6922 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6923 {
6924 escape_glyph = GLYPH_CODE_CHAR (gc);
6925 lface_id = GLYPH_CODE_FACE (gc);
6926 }
6927 if (lface_id)
6928 {
6929 /* The display table specified a face.
6930 Merge it into face_id and also into escape_glyph. */
6931 face_id = merge_faces (it->f, Qt, lface_id,
6932 it->face_id);
6933 }
6934 else if (it->f == last_escape_glyph_frame
6935 && it->face_id == last_escape_glyph_face_id)
6936 {
6937 face_id = last_escape_glyph_merged_face_id;
6938 }
6939 else
6940 {
6941 /* Merge the escape-glyph face into the current face. */
6942 face_id = merge_faces (it->f, Qescape_glyph, 0,
6943 it->face_id);
6944 last_escape_glyph_frame = it->f;
6945 last_escape_glyph_face_id = it->face_id;
6946 last_escape_glyph_merged_face_id = face_id;
6947 }
6948
6949 /* Draw non-ASCII hyphen with just highlighting: */
6950
6951 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6952 {
6953 XSETINT (it->ctl_chars[0], '-');
6954 ctl_len = 1;
6955 goto display_control;
6956 }
6957
6958 /* Draw non-ASCII space/hyphen with escape glyph: */
6959
6960 if (nonascii_space_p || nonascii_hyphen_p)
6961 {
6962 XSETINT (it->ctl_chars[0], escape_glyph);
6963 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6964 ctl_len = 2;
6965 goto display_control;
6966 }
6967
6968 {
6969 char str[10];
6970 int len, i;
6971
6972 if (CHAR_BYTE8_P (c))
6973 /* Display \200 instead of \17777600. */
6974 c = CHAR_TO_BYTE8 (c);
6975 len = sprintf (str, "%03o", c);
6976
6977 XSETINT (it->ctl_chars[0], escape_glyph);
6978 for (i = 0; i < len; i++)
6979 XSETINT (it->ctl_chars[i + 1], str[i]);
6980 ctl_len = len + 1;
6981 }
6982
6983 display_control:
6984 /* Set up IT->dpvec and return first character from it. */
6985 it->dpvec_char_len = it->len;
6986 it->dpvec = it->ctl_chars;
6987 it->dpend = it->dpvec + ctl_len;
6988 it->current.dpvec_index = 0;
6989 it->dpvec_face_id = face_id;
6990 it->saved_face_id = it->face_id;
6991 it->method = GET_FROM_DISPLAY_VECTOR;
6992 it->ellipsis_p = 0;
6993 goto get_next;
6994 }
6995 it->char_to_display = c;
6996 }
6997 else if (success_p)
6998 {
6999 it->char_to_display = it->c;
7000 }
7001 }
7002
7003 /* Adjust face id for a multibyte character. There are no multibyte
7004 character in unibyte text. */
7005 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7006 && it->multibyte_p
7007 && success_p
7008 && FRAME_WINDOW_P (it->f))
7009 {
7010 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7011
7012 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7013 {
7014 /* Automatic composition with glyph-string. */
7015 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7016
7017 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7018 }
7019 else
7020 {
7021 ptrdiff_t pos = (it->s ? -1
7022 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7023 : IT_CHARPOS (*it));
7024 int c;
7025
7026 if (it->what == IT_CHARACTER)
7027 c = it->char_to_display;
7028 else
7029 {
7030 struct composition *cmp = composition_table[it->cmp_it.id];
7031 int i;
7032
7033 c = ' ';
7034 for (i = 0; i < cmp->glyph_len; i++)
7035 /* TAB in a composition means display glyphs with
7036 padding space on the left or right. */
7037 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7038 break;
7039 }
7040 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7041 }
7042 }
7043
7044 done:
7045 /* Is this character the last one of a run of characters with
7046 box? If yes, set IT->end_of_box_run_p to 1. */
7047 if (it->face_box_p
7048 && it->s == NULL)
7049 {
7050 if (it->method == GET_FROM_STRING && it->sp)
7051 {
7052 int face_id = underlying_face_id (it);
7053 struct face *face = FACE_FROM_ID (it->f, face_id);
7054
7055 if (face)
7056 {
7057 if (face->box == FACE_NO_BOX)
7058 {
7059 /* If the box comes from face properties in a
7060 display string, check faces in that string. */
7061 int string_face_id = face_after_it_pos (it);
7062 it->end_of_box_run_p
7063 = (FACE_FROM_ID (it->f, string_face_id)->box
7064 == FACE_NO_BOX);
7065 }
7066 /* Otherwise, the box comes from the underlying face.
7067 If this is the last string character displayed, check
7068 the next buffer location. */
7069 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7070 && (it->current.overlay_string_index
7071 == it->n_overlay_strings - 1))
7072 {
7073 ptrdiff_t ignore;
7074 int next_face_id;
7075 struct text_pos pos = it->current.pos;
7076 INC_TEXT_POS (pos, it->multibyte_p);
7077
7078 next_face_id = face_at_buffer_position
7079 (it->w, CHARPOS (pos), it->region_beg_charpos,
7080 it->region_end_charpos, &ignore,
7081 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7082 -1);
7083 it->end_of_box_run_p
7084 = (FACE_FROM_ID (it->f, next_face_id)->box
7085 == FACE_NO_BOX);
7086 }
7087 }
7088 }
7089 else
7090 {
7091 int face_id = face_after_it_pos (it);
7092 it->end_of_box_run_p
7093 = (face_id != it->face_id
7094 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7095 }
7096 }
7097 /* If we reached the end of the object we've been iterating (e.g., a
7098 display string or an overlay string), and there's something on
7099 IT->stack, proceed with what's on the stack. It doesn't make
7100 sense to return zero if there's unprocessed stuff on the stack,
7101 because otherwise that stuff will never be displayed. */
7102 if (!success_p && it->sp > 0)
7103 {
7104 set_iterator_to_next (it, 0);
7105 success_p = get_next_display_element (it);
7106 }
7107
7108 /* Value is 0 if end of buffer or string reached. */
7109 return success_p;
7110 }
7111
7112
7113 /* Move IT to the next display element.
7114
7115 RESEAT_P non-zero means if called on a newline in buffer text,
7116 skip to the next visible line start.
7117
7118 Functions get_next_display_element and set_iterator_to_next are
7119 separate because I find this arrangement easier to handle than a
7120 get_next_display_element function that also increments IT's
7121 position. The way it is we can first look at an iterator's current
7122 display element, decide whether it fits on a line, and if it does,
7123 increment the iterator position. The other way around we probably
7124 would either need a flag indicating whether the iterator has to be
7125 incremented the next time, or we would have to implement a
7126 decrement position function which would not be easy to write. */
7127
7128 void
7129 set_iterator_to_next (struct it *it, int reseat_p)
7130 {
7131 /* Reset flags indicating start and end of a sequence of characters
7132 with box. Reset them at the start of this function because
7133 moving the iterator to a new position might set them. */
7134 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7135
7136 switch (it->method)
7137 {
7138 case GET_FROM_BUFFER:
7139 /* The current display element of IT is a character from
7140 current_buffer. Advance in the buffer, and maybe skip over
7141 invisible lines that are so because of selective display. */
7142 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7143 reseat_at_next_visible_line_start (it, 0);
7144 else if (it->cmp_it.id >= 0)
7145 {
7146 /* We are currently getting glyphs from a composition. */
7147 int i;
7148
7149 if (! it->bidi_p)
7150 {
7151 IT_CHARPOS (*it) += it->cmp_it.nchars;
7152 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7153 if (it->cmp_it.to < it->cmp_it.nglyphs)
7154 {
7155 it->cmp_it.from = it->cmp_it.to;
7156 }
7157 else
7158 {
7159 it->cmp_it.id = -1;
7160 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7161 IT_BYTEPOS (*it),
7162 it->end_charpos, Qnil);
7163 }
7164 }
7165 else if (! it->cmp_it.reversed_p)
7166 {
7167 /* Composition created while scanning forward. */
7168 /* Update IT's char/byte positions to point to the first
7169 character of the next grapheme cluster, or to the
7170 character visually after the current composition. */
7171 for (i = 0; i < it->cmp_it.nchars; i++)
7172 bidi_move_to_visually_next (&it->bidi_it);
7173 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7174 IT_CHARPOS (*it) = it->bidi_it.charpos;
7175
7176 if (it->cmp_it.to < it->cmp_it.nglyphs)
7177 {
7178 /* Proceed to the next grapheme cluster. */
7179 it->cmp_it.from = it->cmp_it.to;
7180 }
7181 else
7182 {
7183 /* No more grapheme clusters in this composition.
7184 Find the next stop position. */
7185 ptrdiff_t stop = it->end_charpos;
7186 if (it->bidi_it.scan_dir < 0)
7187 /* Now we are scanning backward and don't know
7188 where to stop. */
7189 stop = -1;
7190 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7191 IT_BYTEPOS (*it), stop, Qnil);
7192 }
7193 }
7194 else
7195 {
7196 /* Composition created while scanning backward. */
7197 /* Update IT's char/byte positions to point to the last
7198 character of the previous grapheme cluster, or the
7199 character visually after the current composition. */
7200 for (i = 0; i < it->cmp_it.nchars; i++)
7201 bidi_move_to_visually_next (&it->bidi_it);
7202 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7203 IT_CHARPOS (*it) = it->bidi_it.charpos;
7204 if (it->cmp_it.from > 0)
7205 {
7206 /* Proceed to the previous grapheme cluster. */
7207 it->cmp_it.to = it->cmp_it.from;
7208 }
7209 else
7210 {
7211 /* No more grapheme clusters in this composition.
7212 Find the next stop position. */
7213 ptrdiff_t stop = it->end_charpos;
7214 if (it->bidi_it.scan_dir < 0)
7215 /* Now we are scanning backward and don't know
7216 where to stop. */
7217 stop = -1;
7218 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7219 IT_BYTEPOS (*it), stop, Qnil);
7220 }
7221 }
7222 }
7223 else
7224 {
7225 eassert (it->len != 0);
7226
7227 if (!it->bidi_p)
7228 {
7229 IT_BYTEPOS (*it) += it->len;
7230 IT_CHARPOS (*it) += 1;
7231 }
7232 else
7233 {
7234 int prev_scan_dir = it->bidi_it.scan_dir;
7235 /* If this is a new paragraph, determine its base
7236 direction (a.k.a. its base embedding level). */
7237 if (it->bidi_it.new_paragraph)
7238 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7239 bidi_move_to_visually_next (&it->bidi_it);
7240 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7241 IT_CHARPOS (*it) = it->bidi_it.charpos;
7242 if (prev_scan_dir != it->bidi_it.scan_dir)
7243 {
7244 /* As the scan direction was changed, we must
7245 re-compute the stop position for composition. */
7246 ptrdiff_t stop = it->end_charpos;
7247 if (it->bidi_it.scan_dir < 0)
7248 stop = -1;
7249 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7250 IT_BYTEPOS (*it), stop, Qnil);
7251 }
7252 }
7253 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7254 }
7255 break;
7256
7257 case GET_FROM_C_STRING:
7258 /* Current display element of IT is from a C string. */
7259 if (!it->bidi_p
7260 /* If the string position is beyond string's end, it means
7261 next_element_from_c_string is padding the string with
7262 blanks, in which case we bypass the bidi iterator,
7263 because it cannot deal with such virtual characters. */
7264 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7265 {
7266 IT_BYTEPOS (*it) += it->len;
7267 IT_CHARPOS (*it) += 1;
7268 }
7269 else
7270 {
7271 bidi_move_to_visually_next (&it->bidi_it);
7272 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7273 IT_CHARPOS (*it) = it->bidi_it.charpos;
7274 }
7275 break;
7276
7277 case GET_FROM_DISPLAY_VECTOR:
7278 /* Current display element of IT is from a display table entry.
7279 Advance in the display table definition. Reset it to null if
7280 end reached, and continue with characters from buffers/
7281 strings. */
7282 ++it->current.dpvec_index;
7283
7284 /* Restore face of the iterator to what they were before the
7285 display vector entry (these entries may contain faces). */
7286 it->face_id = it->saved_face_id;
7287
7288 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7289 {
7290 int recheck_faces = it->ellipsis_p;
7291
7292 if (it->s)
7293 it->method = GET_FROM_C_STRING;
7294 else if (STRINGP (it->string))
7295 it->method = GET_FROM_STRING;
7296 else
7297 {
7298 it->method = GET_FROM_BUFFER;
7299 it->object = it->w->contents;
7300 }
7301
7302 it->dpvec = NULL;
7303 it->current.dpvec_index = -1;
7304
7305 /* Skip over characters which were displayed via IT->dpvec. */
7306 if (it->dpvec_char_len < 0)
7307 reseat_at_next_visible_line_start (it, 1);
7308 else if (it->dpvec_char_len > 0)
7309 {
7310 if (it->method == GET_FROM_STRING
7311 && it->current.overlay_string_index >= 0
7312 && it->n_overlay_strings > 0)
7313 it->ignore_overlay_strings_at_pos_p = 1;
7314 it->len = it->dpvec_char_len;
7315 set_iterator_to_next (it, reseat_p);
7316 }
7317
7318 /* Maybe recheck faces after display vector */
7319 if (recheck_faces)
7320 it->stop_charpos = IT_CHARPOS (*it);
7321 }
7322 break;
7323
7324 case GET_FROM_STRING:
7325 /* Current display element is a character from a Lisp string. */
7326 eassert (it->s == NULL && STRINGP (it->string));
7327 /* Don't advance past string end. These conditions are true
7328 when set_iterator_to_next is called at the end of
7329 get_next_display_element, in which case the Lisp string is
7330 already exhausted, and all we want is pop the iterator
7331 stack. */
7332 if (it->current.overlay_string_index >= 0)
7333 {
7334 /* This is an overlay string, so there's no padding with
7335 spaces, and the number of characters in the string is
7336 where the string ends. */
7337 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7338 goto consider_string_end;
7339 }
7340 else
7341 {
7342 /* Not an overlay string. There could be padding, so test
7343 against it->end_charpos . */
7344 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7345 goto consider_string_end;
7346 }
7347 if (it->cmp_it.id >= 0)
7348 {
7349 int i;
7350
7351 if (! it->bidi_p)
7352 {
7353 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7354 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7355 if (it->cmp_it.to < it->cmp_it.nglyphs)
7356 it->cmp_it.from = it->cmp_it.to;
7357 else
7358 {
7359 it->cmp_it.id = -1;
7360 composition_compute_stop_pos (&it->cmp_it,
7361 IT_STRING_CHARPOS (*it),
7362 IT_STRING_BYTEPOS (*it),
7363 it->end_charpos, it->string);
7364 }
7365 }
7366 else if (! it->cmp_it.reversed_p)
7367 {
7368 for (i = 0; i < it->cmp_it.nchars; i++)
7369 bidi_move_to_visually_next (&it->bidi_it);
7370 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7371 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7372
7373 if (it->cmp_it.to < it->cmp_it.nglyphs)
7374 it->cmp_it.from = it->cmp_it.to;
7375 else
7376 {
7377 ptrdiff_t stop = it->end_charpos;
7378 if (it->bidi_it.scan_dir < 0)
7379 stop = -1;
7380 composition_compute_stop_pos (&it->cmp_it,
7381 IT_STRING_CHARPOS (*it),
7382 IT_STRING_BYTEPOS (*it), stop,
7383 it->string);
7384 }
7385 }
7386 else
7387 {
7388 for (i = 0; i < it->cmp_it.nchars; i++)
7389 bidi_move_to_visually_next (&it->bidi_it);
7390 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7391 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7392 if (it->cmp_it.from > 0)
7393 it->cmp_it.to = it->cmp_it.from;
7394 else
7395 {
7396 ptrdiff_t stop = it->end_charpos;
7397 if (it->bidi_it.scan_dir < 0)
7398 stop = -1;
7399 composition_compute_stop_pos (&it->cmp_it,
7400 IT_STRING_CHARPOS (*it),
7401 IT_STRING_BYTEPOS (*it), stop,
7402 it->string);
7403 }
7404 }
7405 }
7406 else
7407 {
7408 if (!it->bidi_p
7409 /* If the string position is beyond string's end, it
7410 means next_element_from_string is padding the string
7411 with blanks, in which case we bypass the bidi
7412 iterator, because it cannot deal with such virtual
7413 characters. */
7414 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7415 {
7416 IT_STRING_BYTEPOS (*it) += it->len;
7417 IT_STRING_CHARPOS (*it) += 1;
7418 }
7419 else
7420 {
7421 int prev_scan_dir = it->bidi_it.scan_dir;
7422
7423 bidi_move_to_visually_next (&it->bidi_it);
7424 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7425 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7426 if (prev_scan_dir != it->bidi_it.scan_dir)
7427 {
7428 ptrdiff_t stop = it->end_charpos;
7429
7430 if (it->bidi_it.scan_dir < 0)
7431 stop = -1;
7432 composition_compute_stop_pos (&it->cmp_it,
7433 IT_STRING_CHARPOS (*it),
7434 IT_STRING_BYTEPOS (*it), stop,
7435 it->string);
7436 }
7437 }
7438 }
7439
7440 consider_string_end:
7441
7442 if (it->current.overlay_string_index >= 0)
7443 {
7444 /* IT->string is an overlay string. Advance to the
7445 next, if there is one. */
7446 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7447 {
7448 it->ellipsis_p = 0;
7449 next_overlay_string (it);
7450 if (it->ellipsis_p)
7451 setup_for_ellipsis (it, 0);
7452 }
7453 }
7454 else
7455 {
7456 /* IT->string is not an overlay string. If we reached
7457 its end, and there is something on IT->stack, proceed
7458 with what is on the stack. This can be either another
7459 string, this time an overlay string, or a buffer. */
7460 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7461 && it->sp > 0)
7462 {
7463 pop_it (it);
7464 if (it->method == GET_FROM_STRING)
7465 goto consider_string_end;
7466 }
7467 }
7468 break;
7469
7470 case GET_FROM_IMAGE:
7471 case GET_FROM_STRETCH:
7472 #ifdef HAVE_XWIDGETS
7473 case GET_FROM_XWIDGET:
7474
7475 /* The position etc with which we have to proceed are on
7476 the stack. The position may be at the end of a string,
7477 if the `display' property takes up the whole string. */
7478 eassert (it->sp > 0);
7479 pop_it (it);
7480 if (it->method == GET_FROM_STRING)
7481 goto consider_string_end;
7482 break;
7483 #endif
7484 default:
7485 /* There are no other methods defined, so this should be a bug. */
7486 emacs_abort ();
7487 }
7488
7489 eassert (it->method != GET_FROM_STRING
7490 || (STRINGP (it->string)
7491 && IT_STRING_CHARPOS (*it) >= 0));
7492 }
7493
7494 /* Load IT's display element fields with information about the next
7495 display element which comes from a display table entry or from the
7496 result of translating a control character to one of the forms `^C'
7497 or `\003'.
7498
7499 IT->dpvec holds the glyphs to return as characters.
7500 IT->saved_face_id holds the face id before the display vector--it
7501 is restored into IT->face_id in set_iterator_to_next. */
7502
7503 static int
7504 next_element_from_display_vector (struct it *it)
7505 {
7506 Lisp_Object gc;
7507
7508 /* Precondition. */
7509 eassert (it->dpvec && it->current.dpvec_index >= 0);
7510
7511 it->face_id = it->saved_face_id;
7512
7513 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7514 That seemed totally bogus - so I changed it... */
7515 gc = it->dpvec[it->current.dpvec_index];
7516
7517 if (GLYPH_CODE_P (gc))
7518 {
7519 it->c = GLYPH_CODE_CHAR (gc);
7520 it->len = CHAR_BYTES (it->c);
7521
7522 /* The entry may contain a face id to use. Such a face id is
7523 the id of a Lisp face, not a realized face. A face id of
7524 zero means no face is specified. */
7525 if (it->dpvec_face_id >= 0)
7526 it->face_id = it->dpvec_face_id;
7527 else
7528 {
7529 int lface_id = GLYPH_CODE_FACE (gc);
7530 if (lface_id > 0)
7531 it->face_id = merge_faces (it->f, Qt, lface_id,
7532 it->saved_face_id);
7533 }
7534 }
7535 else
7536 /* Display table entry is invalid. Return a space. */
7537 it->c = ' ', it->len = 1;
7538
7539 /* Don't change position and object of the iterator here. They are
7540 still the values of the character that had this display table
7541 entry or was translated, and that's what we want. */
7542 it->what = IT_CHARACTER;
7543 return 1;
7544 }
7545
7546 /* Get the first element of string/buffer in the visual order, after
7547 being reseated to a new position in a string or a buffer. */
7548 static void
7549 get_visually_first_element (struct it *it)
7550 {
7551 int string_p = STRINGP (it->string) || it->s;
7552 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7553 ptrdiff_t bob = (string_p ? 0 : BEGV);
7554
7555 if (STRINGP (it->string))
7556 {
7557 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7558 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7559 }
7560 else
7561 {
7562 it->bidi_it.charpos = IT_CHARPOS (*it);
7563 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7564 }
7565
7566 if (it->bidi_it.charpos == eob)
7567 {
7568 /* Nothing to do, but reset the FIRST_ELT flag, like
7569 bidi_paragraph_init does, because we are not going to
7570 call it. */
7571 it->bidi_it.first_elt = 0;
7572 }
7573 else if (it->bidi_it.charpos == bob
7574 || (!string_p
7575 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7576 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7577 {
7578 /* If we are at the beginning of a line/string, we can produce
7579 the next element right away. */
7580 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7581 bidi_move_to_visually_next (&it->bidi_it);
7582 }
7583 else
7584 {
7585 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7586
7587 /* We need to prime the bidi iterator starting at the line's or
7588 string's beginning, before we will be able to produce the
7589 next element. */
7590 if (string_p)
7591 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7592 else
7593 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7594 IT_BYTEPOS (*it), -1,
7595 &it->bidi_it.bytepos);
7596 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7597 do
7598 {
7599 /* Now return to buffer/string position where we were asked
7600 to get the next display element, and produce that. */
7601 bidi_move_to_visually_next (&it->bidi_it);
7602 }
7603 while (it->bidi_it.bytepos != orig_bytepos
7604 && it->bidi_it.charpos < eob);
7605 }
7606
7607 /* Adjust IT's position information to where we ended up. */
7608 if (STRINGP (it->string))
7609 {
7610 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7611 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7612 }
7613 else
7614 {
7615 IT_CHARPOS (*it) = it->bidi_it.charpos;
7616 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7617 }
7618
7619 if (STRINGP (it->string) || !it->s)
7620 {
7621 ptrdiff_t stop, charpos, bytepos;
7622
7623 if (STRINGP (it->string))
7624 {
7625 eassert (!it->s);
7626 stop = SCHARS (it->string);
7627 if (stop > it->end_charpos)
7628 stop = it->end_charpos;
7629 charpos = IT_STRING_CHARPOS (*it);
7630 bytepos = IT_STRING_BYTEPOS (*it);
7631 }
7632 else
7633 {
7634 stop = it->end_charpos;
7635 charpos = IT_CHARPOS (*it);
7636 bytepos = IT_BYTEPOS (*it);
7637 }
7638 if (it->bidi_it.scan_dir < 0)
7639 stop = -1;
7640 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7641 it->string);
7642 }
7643 }
7644
7645 /* Load IT with the next display element from Lisp string IT->string.
7646 IT->current.string_pos is the current position within the string.
7647 If IT->current.overlay_string_index >= 0, the Lisp string is an
7648 overlay string. */
7649
7650 static int
7651 next_element_from_string (struct it *it)
7652 {
7653 struct text_pos position;
7654
7655 eassert (STRINGP (it->string));
7656 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7657 eassert (IT_STRING_CHARPOS (*it) >= 0);
7658 position = it->current.string_pos;
7659
7660 /* With bidi reordering, the character to display might not be the
7661 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7662 that we were reseat()ed to a new string, whose paragraph
7663 direction is not known. */
7664 if (it->bidi_p && it->bidi_it.first_elt)
7665 {
7666 get_visually_first_element (it);
7667 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7668 }
7669
7670 /* Time to check for invisible text? */
7671 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7672 {
7673 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7674 {
7675 if (!(!it->bidi_p
7676 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7677 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7678 {
7679 /* With bidi non-linear iteration, we could find
7680 ourselves far beyond the last computed stop_charpos,
7681 with several other stop positions in between that we
7682 missed. Scan them all now, in buffer's logical
7683 order, until we find and handle the last stop_charpos
7684 that precedes our current position. */
7685 handle_stop_backwards (it, it->stop_charpos);
7686 return GET_NEXT_DISPLAY_ELEMENT (it);
7687 }
7688 else
7689 {
7690 if (it->bidi_p)
7691 {
7692 /* Take note of the stop position we just moved
7693 across, for when we will move back across it. */
7694 it->prev_stop = it->stop_charpos;
7695 /* If we are at base paragraph embedding level, take
7696 note of the last stop position seen at this
7697 level. */
7698 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7699 it->base_level_stop = it->stop_charpos;
7700 }
7701 handle_stop (it);
7702
7703 /* Since a handler may have changed IT->method, we must
7704 recurse here. */
7705 return GET_NEXT_DISPLAY_ELEMENT (it);
7706 }
7707 }
7708 else if (it->bidi_p
7709 /* If we are before prev_stop, we may have overstepped
7710 on our way backwards a stop_pos, and if so, we need
7711 to handle that stop_pos. */
7712 && IT_STRING_CHARPOS (*it) < it->prev_stop
7713 /* We can sometimes back up for reasons that have nothing
7714 to do with bidi reordering. E.g., compositions. The
7715 code below is only needed when we are above the base
7716 embedding level, so test for that explicitly. */
7717 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7718 {
7719 /* If we lost track of base_level_stop, we have no better
7720 place for handle_stop_backwards to start from than string
7721 beginning. This happens, e.g., when we were reseated to
7722 the previous screenful of text by vertical-motion. */
7723 if (it->base_level_stop <= 0
7724 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7725 it->base_level_stop = 0;
7726 handle_stop_backwards (it, it->base_level_stop);
7727 return GET_NEXT_DISPLAY_ELEMENT (it);
7728 }
7729 }
7730
7731 if (it->current.overlay_string_index >= 0)
7732 {
7733 /* Get the next character from an overlay string. In overlay
7734 strings, there is no field width or padding with spaces to
7735 do. */
7736 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7737 {
7738 it->what = IT_EOB;
7739 return 0;
7740 }
7741 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7742 IT_STRING_BYTEPOS (*it),
7743 it->bidi_it.scan_dir < 0
7744 ? -1
7745 : SCHARS (it->string))
7746 && next_element_from_composition (it))
7747 {
7748 return 1;
7749 }
7750 else if (STRING_MULTIBYTE (it->string))
7751 {
7752 const unsigned char *s = (SDATA (it->string)
7753 + IT_STRING_BYTEPOS (*it));
7754 it->c = string_char_and_length (s, &it->len);
7755 }
7756 else
7757 {
7758 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7759 it->len = 1;
7760 }
7761 }
7762 else
7763 {
7764 /* Get the next character from a Lisp string that is not an
7765 overlay string. Such strings come from the mode line, for
7766 example. We may have to pad with spaces, or truncate the
7767 string. See also next_element_from_c_string. */
7768 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7769 {
7770 it->what = IT_EOB;
7771 return 0;
7772 }
7773 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7774 {
7775 /* Pad with spaces. */
7776 it->c = ' ', it->len = 1;
7777 CHARPOS (position) = BYTEPOS (position) = -1;
7778 }
7779 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7780 IT_STRING_BYTEPOS (*it),
7781 it->bidi_it.scan_dir < 0
7782 ? -1
7783 : it->string_nchars)
7784 && next_element_from_composition (it))
7785 {
7786 return 1;
7787 }
7788 else if (STRING_MULTIBYTE (it->string))
7789 {
7790 const unsigned char *s = (SDATA (it->string)
7791 + IT_STRING_BYTEPOS (*it));
7792 it->c = string_char_and_length (s, &it->len);
7793 }
7794 else
7795 {
7796 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7797 it->len = 1;
7798 }
7799 }
7800
7801 /* Record what we have and where it came from. */
7802 it->what = IT_CHARACTER;
7803 it->object = it->string;
7804 it->position = position;
7805 return 1;
7806 }
7807
7808
7809 /* Load IT with next display element from C string IT->s.
7810 IT->string_nchars is the maximum number of characters to return
7811 from the string. IT->end_charpos may be greater than
7812 IT->string_nchars when this function is called, in which case we
7813 may have to return padding spaces. Value is zero if end of string
7814 reached, including padding spaces. */
7815
7816 static int
7817 next_element_from_c_string (struct it *it)
7818 {
7819 int success_p = 1;
7820
7821 eassert (it->s);
7822 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7823 it->what = IT_CHARACTER;
7824 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7825 it->object = Qnil;
7826
7827 /* With bidi reordering, the character to display might not be the
7828 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7829 we were reseated to a new string, whose paragraph direction is
7830 not known. */
7831 if (it->bidi_p && it->bidi_it.first_elt)
7832 get_visually_first_element (it);
7833
7834 /* IT's position can be greater than IT->string_nchars in case a
7835 field width or precision has been specified when the iterator was
7836 initialized. */
7837 if (IT_CHARPOS (*it) >= it->end_charpos)
7838 {
7839 /* End of the game. */
7840 it->what = IT_EOB;
7841 success_p = 0;
7842 }
7843 else if (IT_CHARPOS (*it) >= it->string_nchars)
7844 {
7845 /* Pad with spaces. */
7846 it->c = ' ', it->len = 1;
7847 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7848 }
7849 else if (it->multibyte_p)
7850 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7851 else
7852 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7853
7854 return success_p;
7855 }
7856
7857
7858 /* Set up IT to return characters from an ellipsis, if appropriate.
7859 The definition of the ellipsis glyphs may come from a display table
7860 entry. This function fills IT with the first glyph from the
7861 ellipsis if an ellipsis is to be displayed. */
7862
7863 static int
7864 next_element_from_ellipsis (struct it *it)
7865 {
7866 if (it->selective_display_ellipsis_p)
7867 setup_for_ellipsis (it, it->len);
7868 else
7869 {
7870 /* The face at the current position may be different from the
7871 face we find after the invisible text. Remember what it
7872 was in IT->saved_face_id, and signal that it's there by
7873 setting face_before_selective_p. */
7874 it->saved_face_id = it->face_id;
7875 it->method = GET_FROM_BUFFER;
7876 it->object = it->w->contents;
7877 reseat_at_next_visible_line_start (it, 1);
7878 it->face_before_selective_p = 1;
7879 }
7880
7881 return GET_NEXT_DISPLAY_ELEMENT (it);
7882 }
7883
7884
7885 /* Deliver an image display element. The iterator IT is already
7886 filled with image information (done in handle_display_prop). Value
7887 is always 1. */
7888
7889
7890 static int
7891 next_element_from_image (struct it *it)
7892 {
7893 it->what = IT_IMAGE;
7894 it->ignore_overlay_strings_at_pos_p = 0;
7895 return 1;
7896 }
7897
7898 #ifdef HAVE_XWIDGETS
7899 /* im not sure about this FIXME JAVE*/
7900 static int
7901 next_element_from_xwidget (struct it *it)
7902 {
7903 it->what = IT_XWIDGET;
7904 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
7905 //this is shaky because why do we set "what" if we dont set the other parts??
7906 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
7907 return 1;
7908 }
7909 #endif
7910
7911
7912 /* Fill iterator IT with next display element from a stretch glyph
7913 property. IT->object is the value of the text property. Value is
7914 always 1. */
7915
7916 static int
7917 next_element_from_stretch (struct it *it)
7918 {
7919 it->what = IT_STRETCH;
7920 return 1;
7921 }
7922
7923 /* Scan backwards from IT's current position until we find a stop
7924 position, or until BEGV. This is called when we find ourself
7925 before both the last known prev_stop and base_level_stop while
7926 reordering bidirectional text. */
7927
7928 static void
7929 compute_stop_pos_backwards (struct it *it)
7930 {
7931 const int SCAN_BACK_LIMIT = 1000;
7932 struct text_pos pos;
7933 struct display_pos save_current = it->current;
7934 struct text_pos save_position = it->position;
7935 ptrdiff_t charpos = IT_CHARPOS (*it);
7936 ptrdiff_t where_we_are = charpos;
7937 ptrdiff_t save_stop_pos = it->stop_charpos;
7938 ptrdiff_t save_end_pos = it->end_charpos;
7939
7940 eassert (NILP (it->string) && !it->s);
7941 eassert (it->bidi_p);
7942 it->bidi_p = 0;
7943 do
7944 {
7945 it->end_charpos = min (charpos + 1, ZV);
7946 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7947 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7948 reseat_1 (it, pos, 0);
7949 compute_stop_pos (it);
7950 /* We must advance forward, right? */
7951 if (it->stop_charpos <= charpos)
7952 emacs_abort ();
7953 }
7954 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7955
7956 if (it->stop_charpos <= where_we_are)
7957 it->prev_stop = it->stop_charpos;
7958 else
7959 it->prev_stop = BEGV;
7960 it->bidi_p = 1;
7961 it->current = save_current;
7962 it->position = save_position;
7963 it->stop_charpos = save_stop_pos;
7964 it->end_charpos = save_end_pos;
7965 }
7966
7967 /* Scan forward from CHARPOS in the current buffer/string, until we
7968 find a stop position > current IT's position. Then handle the stop
7969 position before that. This is called when we bump into a stop
7970 position while reordering bidirectional text. CHARPOS should be
7971 the last previously processed stop_pos (or BEGV/0, if none were
7972 processed yet) whose position is less that IT's current
7973 position. */
7974
7975 static void
7976 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7977 {
7978 int bufp = !STRINGP (it->string);
7979 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7980 struct display_pos save_current = it->current;
7981 struct text_pos save_position = it->position;
7982 struct text_pos pos1;
7983 ptrdiff_t next_stop;
7984
7985 /* Scan in strict logical order. */
7986 eassert (it->bidi_p);
7987 it->bidi_p = 0;
7988 do
7989 {
7990 it->prev_stop = charpos;
7991 if (bufp)
7992 {
7993 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7994 reseat_1 (it, pos1, 0);
7995 }
7996 else
7997 it->current.string_pos = string_pos (charpos, it->string);
7998 compute_stop_pos (it);
7999 /* We must advance forward, right? */
8000 if (it->stop_charpos <= it->prev_stop)
8001 emacs_abort ();
8002 charpos = it->stop_charpos;
8003 }
8004 while (charpos <= where_we_are);
8005
8006 it->bidi_p = 1;
8007 it->current = save_current;
8008 it->position = save_position;
8009 next_stop = it->stop_charpos;
8010 it->stop_charpos = it->prev_stop;
8011 handle_stop (it);
8012 it->stop_charpos = next_stop;
8013 }
8014
8015 /* Load IT with the next display element from current_buffer. Value
8016 is zero if end of buffer reached. IT->stop_charpos is the next
8017 position at which to stop and check for text properties or buffer
8018 end. */
8019
8020 static int
8021 next_element_from_buffer (struct it *it)
8022 {
8023 int success_p = 1;
8024
8025 eassert (IT_CHARPOS (*it) >= BEGV);
8026 eassert (NILP (it->string) && !it->s);
8027 eassert (!it->bidi_p
8028 || (EQ (it->bidi_it.string.lstring, Qnil)
8029 && it->bidi_it.string.s == NULL));
8030
8031 /* With bidi reordering, the character to display might not be the
8032 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8033 we were reseat()ed to a new buffer position, which is potentially
8034 a different paragraph. */
8035 if (it->bidi_p && it->bidi_it.first_elt)
8036 {
8037 get_visually_first_element (it);
8038 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8039 }
8040
8041 if (IT_CHARPOS (*it) >= it->stop_charpos)
8042 {
8043 if (IT_CHARPOS (*it) >= it->end_charpos)
8044 {
8045 int overlay_strings_follow_p;
8046
8047 /* End of the game, except when overlay strings follow that
8048 haven't been returned yet. */
8049 if (it->overlay_strings_at_end_processed_p)
8050 overlay_strings_follow_p = 0;
8051 else
8052 {
8053 it->overlay_strings_at_end_processed_p = 1;
8054 overlay_strings_follow_p = get_overlay_strings (it, 0);
8055 }
8056
8057 if (overlay_strings_follow_p)
8058 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8059 else
8060 {
8061 it->what = IT_EOB;
8062 it->position = it->current.pos;
8063 success_p = 0;
8064 }
8065 }
8066 else if (!(!it->bidi_p
8067 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8068 || IT_CHARPOS (*it) == it->stop_charpos))
8069 {
8070 /* With bidi non-linear iteration, we could find ourselves
8071 far beyond the last computed stop_charpos, with several
8072 other stop positions in between that we missed. Scan
8073 them all now, in buffer's logical order, until we find
8074 and handle the last stop_charpos that precedes our
8075 current position. */
8076 handle_stop_backwards (it, it->stop_charpos);
8077 return GET_NEXT_DISPLAY_ELEMENT (it);
8078 }
8079 else
8080 {
8081 if (it->bidi_p)
8082 {
8083 /* Take note of the stop position we just moved across,
8084 for when we will move back across it. */
8085 it->prev_stop = it->stop_charpos;
8086 /* If we are at base paragraph embedding level, take
8087 note of the last stop position seen at this
8088 level. */
8089 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8090 it->base_level_stop = it->stop_charpos;
8091 }
8092 handle_stop (it);
8093 return GET_NEXT_DISPLAY_ELEMENT (it);
8094 }
8095 }
8096 else if (it->bidi_p
8097 /* If we are before prev_stop, we may have overstepped on
8098 our way backwards a stop_pos, and if so, we need to
8099 handle that stop_pos. */
8100 && IT_CHARPOS (*it) < it->prev_stop
8101 /* We can sometimes back up for reasons that have nothing
8102 to do with bidi reordering. E.g., compositions. The
8103 code below is only needed when we are above the base
8104 embedding level, so test for that explicitly. */
8105 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8106 {
8107 if (it->base_level_stop <= 0
8108 || IT_CHARPOS (*it) < it->base_level_stop)
8109 {
8110 /* If we lost track of base_level_stop, we need to find
8111 prev_stop by looking backwards. This happens, e.g., when
8112 we were reseated to the previous screenful of text by
8113 vertical-motion. */
8114 it->base_level_stop = BEGV;
8115 compute_stop_pos_backwards (it);
8116 handle_stop_backwards (it, it->prev_stop);
8117 }
8118 else
8119 handle_stop_backwards (it, it->base_level_stop);
8120 return GET_NEXT_DISPLAY_ELEMENT (it);
8121 }
8122 else
8123 {
8124 /* No face changes, overlays etc. in sight, so just return a
8125 character from current_buffer. */
8126 unsigned char *p;
8127 ptrdiff_t stop;
8128
8129 /* Maybe run the redisplay end trigger hook. Performance note:
8130 This doesn't seem to cost measurable time. */
8131 if (it->redisplay_end_trigger_charpos
8132 && it->glyph_row
8133 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8134 run_redisplay_end_trigger_hook (it);
8135
8136 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8137 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8138 stop)
8139 && next_element_from_composition (it))
8140 {
8141 return 1;
8142 }
8143
8144 /* Get the next character, maybe multibyte. */
8145 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8146 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8147 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8148 else
8149 it->c = *p, it->len = 1;
8150
8151 /* Record what we have and where it came from. */
8152 it->what = IT_CHARACTER;
8153 it->object = it->w->contents;
8154 it->position = it->current.pos;
8155
8156 /* Normally we return the character found above, except when we
8157 really want to return an ellipsis for selective display. */
8158 if (it->selective)
8159 {
8160 if (it->c == '\n')
8161 {
8162 /* A value of selective > 0 means hide lines indented more
8163 than that number of columns. */
8164 if (it->selective > 0
8165 && IT_CHARPOS (*it) + 1 < ZV
8166 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8167 IT_BYTEPOS (*it) + 1,
8168 it->selective))
8169 {
8170 success_p = next_element_from_ellipsis (it);
8171 it->dpvec_char_len = -1;
8172 }
8173 }
8174 else if (it->c == '\r' && it->selective == -1)
8175 {
8176 /* A value of selective == -1 means that everything from the
8177 CR to the end of the line is invisible, with maybe an
8178 ellipsis displayed for it. */
8179 success_p = next_element_from_ellipsis (it);
8180 it->dpvec_char_len = -1;
8181 }
8182 }
8183 }
8184
8185 /* Value is zero if end of buffer reached. */
8186 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8187 return success_p;
8188 }
8189
8190
8191 /* Run the redisplay end trigger hook for IT. */
8192
8193 static void
8194 run_redisplay_end_trigger_hook (struct it *it)
8195 {
8196 Lisp_Object args[3];
8197
8198 /* IT->glyph_row should be non-null, i.e. we should be actually
8199 displaying something, or otherwise we should not run the hook. */
8200 eassert (it->glyph_row);
8201
8202 /* Set up hook arguments. */
8203 args[0] = Qredisplay_end_trigger_functions;
8204 args[1] = it->window;
8205 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8206 it->redisplay_end_trigger_charpos = 0;
8207
8208 /* Since we are *trying* to run these functions, don't try to run
8209 them again, even if they get an error. */
8210 wset_redisplay_end_trigger (it->w, Qnil);
8211 Frun_hook_with_args (3, args);
8212
8213 /* Notice if it changed the face of the character we are on. */
8214 handle_face_prop (it);
8215 }
8216
8217
8218 /* Deliver a composition display element. Unlike the other
8219 next_element_from_XXX, this function is not registered in the array
8220 get_next_element[]. It is called from next_element_from_buffer and
8221 next_element_from_string when necessary. */
8222
8223 static int
8224 next_element_from_composition (struct it *it)
8225 {
8226 it->what = IT_COMPOSITION;
8227 it->len = it->cmp_it.nbytes;
8228 if (STRINGP (it->string))
8229 {
8230 if (it->c < 0)
8231 {
8232 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8233 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8234 return 0;
8235 }
8236 it->position = it->current.string_pos;
8237 it->object = it->string;
8238 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8239 IT_STRING_BYTEPOS (*it), it->string);
8240 }
8241 else
8242 {
8243 if (it->c < 0)
8244 {
8245 IT_CHARPOS (*it) += it->cmp_it.nchars;
8246 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8247 if (it->bidi_p)
8248 {
8249 if (it->bidi_it.new_paragraph)
8250 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8251 /* Resync the bidi iterator with IT's new position.
8252 FIXME: this doesn't support bidirectional text. */
8253 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8254 bidi_move_to_visually_next (&it->bidi_it);
8255 }
8256 return 0;
8257 }
8258 it->position = it->current.pos;
8259 it->object = it->w->contents;
8260 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8261 IT_BYTEPOS (*it), Qnil);
8262 }
8263 return 1;
8264 }
8265
8266
8267 \f
8268 /***********************************************************************
8269 Moving an iterator without producing glyphs
8270 ***********************************************************************/
8271
8272 /* Check if iterator is at a position corresponding to a valid buffer
8273 position after some move_it_ call. */
8274
8275 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8276 ((it)->method == GET_FROM_STRING \
8277 ? IT_STRING_CHARPOS (*it) == 0 \
8278 : 1)
8279
8280
8281 /* Move iterator IT to a specified buffer or X position within one
8282 line on the display without producing glyphs.
8283
8284 OP should be a bit mask including some or all of these bits:
8285 MOVE_TO_X: Stop upon reaching x-position TO_X.
8286 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8287 Regardless of OP's value, stop upon reaching the end of the display line.
8288
8289 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8290 This means, in particular, that TO_X includes window's horizontal
8291 scroll amount.
8292
8293 The return value has several possible values that
8294 say what condition caused the scan to stop:
8295
8296 MOVE_POS_MATCH_OR_ZV
8297 - when TO_POS or ZV was reached.
8298
8299 MOVE_X_REACHED
8300 -when TO_X was reached before TO_POS or ZV were reached.
8301
8302 MOVE_LINE_CONTINUED
8303 - when we reached the end of the display area and the line must
8304 be continued.
8305
8306 MOVE_LINE_TRUNCATED
8307 - when we reached the end of the display area and the line is
8308 truncated.
8309
8310 MOVE_NEWLINE_OR_CR
8311 - when we stopped at a line end, i.e. a newline or a CR and selective
8312 display is on. */
8313
8314 static enum move_it_result
8315 move_it_in_display_line_to (struct it *it,
8316 ptrdiff_t to_charpos, int to_x,
8317 enum move_operation_enum op)
8318 {
8319 enum move_it_result result = MOVE_UNDEFINED;
8320 struct glyph_row *saved_glyph_row;
8321 struct it wrap_it, atpos_it, atx_it, ppos_it;
8322 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8323 void *ppos_data = NULL;
8324 int may_wrap = 0;
8325 enum it_method prev_method = it->method;
8326 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8327 int saw_smaller_pos = prev_pos < to_charpos;
8328
8329 /* Don't produce glyphs in produce_glyphs. */
8330 saved_glyph_row = it->glyph_row;
8331 it->glyph_row = NULL;
8332
8333 /* Use wrap_it to save a copy of IT wherever a word wrap could
8334 occur. Use atpos_it to save a copy of IT at the desired buffer
8335 position, if found, so that we can scan ahead and check if the
8336 word later overshoots the window edge. Use atx_it similarly, for
8337 pixel positions. */
8338 wrap_it.sp = -1;
8339 atpos_it.sp = -1;
8340 atx_it.sp = -1;
8341
8342 /* Use ppos_it under bidi reordering to save a copy of IT for the
8343 position > CHARPOS that is the closest to CHARPOS. We restore
8344 that position in IT when we have scanned the entire display line
8345 without finding a match for CHARPOS and all the character
8346 positions are greater than CHARPOS. */
8347 if (it->bidi_p)
8348 {
8349 SAVE_IT (ppos_it, *it, ppos_data);
8350 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8351 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8352 SAVE_IT (ppos_it, *it, ppos_data);
8353 }
8354
8355 #define BUFFER_POS_REACHED_P() \
8356 ((op & MOVE_TO_POS) != 0 \
8357 && BUFFERP (it->object) \
8358 && (IT_CHARPOS (*it) == to_charpos \
8359 || ((!it->bidi_p \
8360 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8361 && IT_CHARPOS (*it) > to_charpos) \
8362 || (it->what == IT_COMPOSITION \
8363 && ((IT_CHARPOS (*it) > to_charpos \
8364 && to_charpos >= it->cmp_it.charpos) \
8365 || (IT_CHARPOS (*it) < to_charpos \
8366 && to_charpos <= it->cmp_it.charpos)))) \
8367 && (it->method == GET_FROM_BUFFER \
8368 || (it->method == GET_FROM_DISPLAY_VECTOR \
8369 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8370
8371 /* If there's a line-/wrap-prefix, handle it. */
8372 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8373 && it->current_y < it->last_visible_y)
8374 handle_line_prefix (it);
8375
8376 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8377 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8378
8379 while (1)
8380 {
8381 int x, i, ascent = 0, descent = 0;
8382
8383 /* Utility macro to reset an iterator with x, ascent, and descent. */
8384 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8385 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8386 (IT)->max_descent = descent)
8387
8388 /* Stop if we move beyond TO_CHARPOS (after an image or a
8389 display string or stretch glyph). */
8390 if ((op & MOVE_TO_POS) != 0
8391 && BUFFERP (it->object)
8392 && it->method == GET_FROM_BUFFER
8393 && (((!it->bidi_p
8394 /* When the iterator is at base embedding level, we
8395 are guaranteed that characters are delivered for
8396 display in strictly increasing order of their
8397 buffer positions. */
8398 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8399 && IT_CHARPOS (*it) > to_charpos)
8400 || (it->bidi_p
8401 && (prev_method == GET_FROM_IMAGE
8402 || prev_method == GET_FROM_STRETCH
8403 || prev_method == GET_FROM_STRING)
8404 /* Passed TO_CHARPOS from left to right. */
8405 && ((prev_pos < to_charpos
8406 && IT_CHARPOS (*it) > to_charpos)
8407 /* Passed TO_CHARPOS from right to left. */
8408 || (prev_pos > to_charpos
8409 && IT_CHARPOS (*it) < to_charpos)))))
8410 {
8411 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8412 {
8413 result = MOVE_POS_MATCH_OR_ZV;
8414 break;
8415 }
8416 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8417 /* If wrap_it is valid, the current position might be in a
8418 word that is wrapped. So, save the iterator in
8419 atpos_it and continue to see if wrapping happens. */
8420 SAVE_IT (atpos_it, *it, atpos_data);
8421 }
8422
8423 /* Stop when ZV reached.
8424 We used to stop here when TO_CHARPOS reached as well, but that is
8425 too soon if this glyph does not fit on this line. So we handle it
8426 explicitly below. */
8427 if (!get_next_display_element (it))
8428 {
8429 result = MOVE_POS_MATCH_OR_ZV;
8430 break;
8431 }
8432
8433 if (it->line_wrap == TRUNCATE)
8434 {
8435 if (BUFFER_POS_REACHED_P ())
8436 {
8437 result = MOVE_POS_MATCH_OR_ZV;
8438 break;
8439 }
8440 }
8441 else
8442 {
8443 if (it->line_wrap == WORD_WRAP)
8444 {
8445 if (IT_DISPLAYING_WHITESPACE (it))
8446 may_wrap = 1;
8447 else if (may_wrap)
8448 {
8449 /* We have reached a glyph that follows one or more
8450 whitespace characters. If the position is
8451 already found, we are done. */
8452 if (atpos_it.sp >= 0)
8453 {
8454 RESTORE_IT (it, &atpos_it, atpos_data);
8455 result = MOVE_POS_MATCH_OR_ZV;
8456 goto done;
8457 }
8458 if (atx_it.sp >= 0)
8459 {
8460 RESTORE_IT (it, &atx_it, atx_data);
8461 result = MOVE_X_REACHED;
8462 goto done;
8463 }
8464 /* Otherwise, we can wrap here. */
8465 SAVE_IT (wrap_it, *it, wrap_data);
8466 may_wrap = 0;
8467 }
8468 }
8469 }
8470
8471 /* Remember the line height for the current line, in case
8472 the next element doesn't fit on the line. */
8473 ascent = it->max_ascent;
8474 descent = it->max_descent;
8475
8476 /* The call to produce_glyphs will get the metrics of the
8477 display element IT is loaded with. Record the x-position
8478 before this display element, in case it doesn't fit on the
8479 line. */
8480 x = it->current_x;
8481
8482 PRODUCE_GLYPHS (it);
8483
8484 if (it->area != TEXT_AREA)
8485 {
8486 prev_method = it->method;
8487 if (it->method == GET_FROM_BUFFER)
8488 prev_pos = IT_CHARPOS (*it);
8489 set_iterator_to_next (it, 1);
8490 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8491 SET_TEXT_POS (this_line_min_pos,
8492 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8493 if (it->bidi_p
8494 && (op & MOVE_TO_POS)
8495 && IT_CHARPOS (*it) > to_charpos
8496 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8497 SAVE_IT (ppos_it, *it, ppos_data);
8498 continue;
8499 }
8500
8501 /* The number of glyphs we get back in IT->nglyphs will normally
8502 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8503 character on a terminal frame, or (iii) a line end. For the
8504 second case, IT->nglyphs - 1 padding glyphs will be present.
8505 (On X frames, there is only one glyph produced for a
8506 composite character.)
8507
8508 The behavior implemented below means, for continuation lines,
8509 that as many spaces of a TAB as fit on the current line are
8510 displayed there. For terminal frames, as many glyphs of a
8511 multi-glyph character are displayed in the current line, too.
8512 This is what the old redisplay code did, and we keep it that
8513 way. Under X, the whole shape of a complex character must
8514 fit on the line or it will be completely displayed in the
8515 next line.
8516
8517 Note that both for tabs and padding glyphs, all glyphs have
8518 the same width. */
8519 if (it->nglyphs)
8520 {
8521 /* More than one glyph or glyph doesn't fit on line. All
8522 glyphs have the same width. */
8523 int single_glyph_width = it->pixel_width / it->nglyphs;
8524 int new_x;
8525 int x_before_this_char = x;
8526 int hpos_before_this_char = it->hpos;
8527
8528 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8529 {
8530 new_x = x + single_glyph_width;
8531
8532 /* We want to leave anything reaching TO_X to the caller. */
8533 if ((op & MOVE_TO_X) && new_x > to_x)
8534 {
8535 if (BUFFER_POS_REACHED_P ())
8536 {
8537 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8538 goto buffer_pos_reached;
8539 if (atpos_it.sp < 0)
8540 {
8541 SAVE_IT (atpos_it, *it, atpos_data);
8542 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8543 }
8544 }
8545 else
8546 {
8547 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8548 {
8549 it->current_x = x;
8550 result = MOVE_X_REACHED;
8551 break;
8552 }
8553 if (atx_it.sp < 0)
8554 {
8555 SAVE_IT (atx_it, *it, atx_data);
8556 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8557 }
8558 }
8559 }
8560
8561 if (/* Lines are continued. */
8562 it->line_wrap != TRUNCATE
8563 && (/* And glyph doesn't fit on the line. */
8564 new_x > it->last_visible_x
8565 /* Or it fits exactly and we're on a window
8566 system frame. */
8567 || (new_x == it->last_visible_x
8568 && FRAME_WINDOW_P (it->f)
8569 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8570 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8571 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8572 {
8573 if (/* IT->hpos == 0 means the very first glyph
8574 doesn't fit on the line, e.g. a wide image. */
8575 it->hpos == 0
8576 || (new_x == it->last_visible_x
8577 && FRAME_WINDOW_P (it->f)))
8578 {
8579 ++it->hpos;
8580 it->current_x = new_x;
8581
8582 /* The character's last glyph just barely fits
8583 in this row. */
8584 if (i == it->nglyphs - 1)
8585 {
8586 /* If this is the destination position,
8587 return a position *before* it in this row,
8588 now that we know it fits in this row. */
8589 if (BUFFER_POS_REACHED_P ())
8590 {
8591 if (it->line_wrap != WORD_WRAP
8592 || wrap_it.sp < 0)
8593 {
8594 it->hpos = hpos_before_this_char;
8595 it->current_x = x_before_this_char;
8596 result = MOVE_POS_MATCH_OR_ZV;
8597 break;
8598 }
8599 if (it->line_wrap == WORD_WRAP
8600 && atpos_it.sp < 0)
8601 {
8602 SAVE_IT (atpos_it, *it, atpos_data);
8603 atpos_it.current_x = x_before_this_char;
8604 atpos_it.hpos = hpos_before_this_char;
8605 }
8606 }
8607
8608 prev_method = it->method;
8609 if (it->method == GET_FROM_BUFFER)
8610 prev_pos = IT_CHARPOS (*it);
8611 set_iterator_to_next (it, 1);
8612 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8613 SET_TEXT_POS (this_line_min_pos,
8614 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8615 /* On graphical terminals, newlines may
8616 "overflow" into the fringe if
8617 overflow-newline-into-fringe is non-nil.
8618 On text terminals, and on graphical
8619 terminals with no right margin, newlines
8620 may overflow into the last glyph on the
8621 display line.*/
8622 if (!FRAME_WINDOW_P (it->f)
8623 || ((it->bidi_p
8624 && it->bidi_it.paragraph_dir == R2L)
8625 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8626 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8627 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8628 {
8629 if (!get_next_display_element (it))
8630 {
8631 result = MOVE_POS_MATCH_OR_ZV;
8632 break;
8633 }
8634 if (BUFFER_POS_REACHED_P ())
8635 {
8636 if (ITERATOR_AT_END_OF_LINE_P (it))
8637 result = MOVE_POS_MATCH_OR_ZV;
8638 else
8639 result = MOVE_LINE_CONTINUED;
8640 break;
8641 }
8642 if (ITERATOR_AT_END_OF_LINE_P (it)
8643 && (it->line_wrap != WORD_WRAP
8644 || wrap_it.sp < 0))
8645 {
8646 result = MOVE_NEWLINE_OR_CR;
8647 break;
8648 }
8649 }
8650 }
8651 }
8652 else
8653 IT_RESET_X_ASCENT_DESCENT (it);
8654
8655 if (wrap_it.sp >= 0)
8656 {
8657 RESTORE_IT (it, &wrap_it, wrap_data);
8658 atpos_it.sp = -1;
8659 atx_it.sp = -1;
8660 }
8661
8662 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8663 IT_CHARPOS (*it)));
8664 result = MOVE_LINE_CONTINUED;
8665 break;
8666 }
8667
8668 if (BUFFER_POS_REACHED_P ())
8669 {
8670 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8671 goto buffer_pos_reached;
8672 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8673 {
8674 SAVE_IT (atpos_it, *it, atpos_data);
8675 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8676 }
8677 }
8678
8679 if (new_x > it->first_visible_x)
8680 {
8681 /* Glyph is visible. Increment number of glyphs that
8682 would be displayed. */
8683 ++it->hpos;
8684 }
8685 }
8686
8687 if (result != MOVE_UNDEFINED)
8688 break;
8689 }
8690 else if (BUFFER_POS_REACHED_P ())
8691 {
8692 buffer_pos_reached:
8693 IT_RESET_X_ASCENT_DESCENT (it);
8694 result = MOVE_POS_MATCH_OR_ZV;
8695 break;
8696 }
8697 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8698 {
8699 /* Stop when TO_X specified and reached. This check is
8700 necessary here because of lines consisting of a line end,
8701 only. The line end will not produce any glyphs and we
8702 would never get MOVE_X_REACHED. */
8703 eassert (it->nglyphs == 0);
8704 result = MOVE_X_REACHED;
8705 break;
8706 }
8707
8708 /* Is this a line end? If yes, we're done. */
8709 if (ITERATOR_AT_END_OF_LINE_P (it))
8710 {
8711 /* If we are past TO_CHARPOS, but never saw any character
8712 positions smaller than TO_CHARPOS, return
8713 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8714 did. */
8715 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8716 {
8717 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8718 {
8719 if (IT_CHARPOS (ppos_it) < ZV)
8720 {
8721 RESTORE_IT (it, &ppos_it, ppos_data);
8722 result = MOVE_POS_MATCH_OR_ZV;
8723 }
8724 else
8725 goto buffer_pos_reached;
8726 }
8727 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8728 && IT_CHARPOS (*it) > to_charpos)
8729 goto buffer_pos_reached;
8730 else
8731 result = MOVE_NEWLINE_OR_CR;
8732 }
8733 else
8734 result = MOVE_NEWLINE_OR_CR;
8735 break;
8736 }
8737
8738 prev_method = it->method;
8739 if (it->method == GET_FROM_BUFFER)
8740 prev_pos = IT_CHARPOS (*it);
8741 /* The current display element has been consumed. Advance
8742 to the next. */
8743 set_iterator_to_next (it, 1);
8744 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8745 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8746 if (IT_CHARPOS (*it) < to_charpos)
8747 saw_smaller_pos = 1;
8748 if (it->bidi_p
8749 && (op & MOVE_TO_POS)
8750 && IT_CHARPOS (*it) >= to_charpos
8751 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8752 SAVE_IT (ppos_it, *it, ppos_data);
8753
8754 /* Stop if lines are truncated and IT's current x-position is
8755 past the right edge of the window now. */
8756 if (it->line_wrap == TRUNCATE
8757 && it->current_x >= it->last_visible_x)
8758 {
8759 if (!FRAME_WINDOW_P (it->f)
8760 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8761 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8762 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8763 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8764 {
8765 int at_eob_p = 0;
8766
8767 if ((at_eob_p = !get_next_display_element (it))
8768 || BUFFER_POS_REACHED_P ()
8769 /* If we are past TO_CHARPOS, but never saw any
8770 character positions smaller than TO_CHARPOS,
8771 return MOVE_POS_MATCH_OR_ZV, like the
8772 unidirectional display did. */
8773 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8774 && !saw_smaller_pos
8775 && IT_CHARPOS (*it) > to_charpos))
8776 {
8777 if (it->bidi_p
8778 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8779 RESTORE_IT (it, &ppos_it, ppos_data);
8780 result = MOVE_POS_MATCH_OR_ZV;
8781 break;
8782 }
8783 if (ITERATOR_AT_END_OF_LINE_P (it))
8784 {
8785 result = MOVE_NEWLINE_OR_CR;
8786 break;
8787 }
8788 }
8789 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8790 && !saw_smaller_pos
8791 && IT_CHARPOS (*it) > to_charpos)
8792 {
8793 if (IT_CHARPOS (ppos_it) < ZV)
8794 RESTORE_IT (it, &ppos_it, ppos_data);
8795 result = MOVE_POS_MATCH_OR_ZV;
8796 break;
8797 }
8798 result = MOVE_LINE_TRUNCATED;
8799 break;
8800 }
8801 #undef IT_RESET_X_ASCENT_DESCENT
8802 }
8803
8804 #undef BUFFER_POS_REACHED_P
8805
8806 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8807 restore the saved iterator. */
8808 if (atpos_it.sp >= 0)
8809 RESTORE_IT (it, &atpos_it, atpos_data);
8810 else if (atx_it.sp >= 0)
8811 RESTORE_IT (it, &atx_it, atx_data);
8812
8813 done:
8814
8815 if (atpos_data)
8816 bidi_unshelve_cache (atpos_data, 1);
8817 if (atx_data)
8818 bidi_unshelve_cache (atx_data, 1);
8819 if (wrap_data)
8820 bidi_unshelve_cache (wrap_data, 1);
8821 if (ppos_data)
8822 bidi_unshelve_cache (ppos_data, 1);
8823
8824 /* Restore the iterator settings altered at the beginning of this
8825 function. */
8826 it->glyph_row = saved_glyph_row;
8827 return result;
8828 }
8829
8830 /* For external use. */
8831 void
8832 move_it_in_display_line (struct it *it,
8833 ptrdiff_t to_charpos, int to_x,
8834 enum move_operation_enum op)
8835 {
8836 if (it->line_wrap == WORD_WRAP
8837 && (op & MOVE_TO_X))
8838 {
8839 struct it save_it;
8840 void *save_data = NULL;
8841 int skip;
8842
8843 SAVE_IT (save_it, *it, save_data);
8844 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8845 /* When word-wrap is on, TO_X may lie past the end
8846 of a wrapped line. Then it->current is the
8847 character on the next line, so backtrack to the
8848 space before the wrap point. */
8849 if (skip == MOVE_LINE_CONTINUED)
8850 {
8851 int prev_x = max (it->current_x - 1, 0);
8852 RESTORE_IT (it, &save_it, save_data);
8853 move_it_in_display_line_to
8854 (it, -1, prev_x, MOVE_TO_X);
8855 }
8856 else
8857 bidi_unshelve_cache (save_data, 1);
8858 }
8859 else
8860 move_it_in_display_line_to (it, to_charpos, to_x, op);
8861 }
8862
8863
8864 /* Move IT forward until it satisfies one or more of the criteria in
8865 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8866
8867 OP is a bit-mask that specifies where to stop, and in particular,
8868 which of those four position arguments makes a difference. See the
8869 description of enum move_operation_enum.
8870
8871 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8872 screen line, this function will set IT to the next position that is
8873 displayed to the right of TO_CHARPOS on the screen. */
8874
8875 void
8876 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8877 {
8878 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8879 int line_height, line_start_x = 0, reached = 0;
8880 void *backup_data = NULL;
8881
8882 for (;;)
8883 {
8884 if (op & MOVE_TO_VPOS)
8885 {
8886 /* If no TO_CHARPOS and no TO_X specified, stop at the
8887 start of the line TO_VPOS. */
8888 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8889 {
8890 if (it->vpos == to_vpos)
8891 {
8892 reached = 1;
8893 break;
8894 }
8895 else
8896 skip = move_it_in_display_line_to (it, -1, -1, 0);
8897 }
8898 else
8899 {
8900 /* TO_VPOS >= 0 means stop at TO_X in the line at
8901 TO_VPOS, or at TO_POS, whichever comes first. */
8902 if (it->vpos == to_vpos)
8903 {
8904 reached = 2;
8905 break;
8906 }
8907
8908 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8909
8910 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8911 {
8912 reached = 3;
8913 break;
8914 }
8915 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8916 {
8917 /* We have reached TO_X but not in the line we want. */
8918 skip = move_it_in_display_line_to (it, to_charpos,
8919 -1, MOVE_TO_POS);
8920 if (skip == MOVE_POS_MATCH_OR_ZV)
8921 {
8922 reached = 4;
8923 break;
8924 }
8925 }
8926 }
8927 }
8928 else if (op & MOVE_TO_Y)
8929 {
8930 struct it it_backup;
8931
8932 if (it->line_wrap == WORD_WRAP)
8933 SAVE_IT (it_backup, *it, backup_data);
8934
8935 /* TO_Y specified means stop at TO_X in the line containing
8936 TO_Y---or at TO_CHARPOS if this is reached first. The
8937 problem is that we can't really tell whether the line
8938 contains TO_Y before we have completely scanned it, and
8939 this may skip past TO_X. What we do is to first scan to
8940 TO_X.
8941
8942 If TO_X is not specified, use a TO_X of zero. The reason
8943 is to make the outcome of this function more predictable.
8944 If we didn't use TO_X == 0, we would stop at the end of
8945 the line which is probably not what a caller would expect
8946 to happen. */
8947 skip = move_it_in_display_line_to
8948 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8949 (MOVE_TO_X | (op & MOVE_TO_POS)));
8950
8951 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8952 if (skip == MOVE_POS_MATCH_OR_ZV)
8953 reached = 5;
8954 else if (skip == MOVE_X_REACHED)
8955 {
8956 /* If TO_X was reached, we want to know whether TO_Y is
8957 in the line. We know this is the case if the already
8958 scanned glyphs make the line tall enough. Otherwise,
8959 we must check by scanning the rest of the line. */
8960 line_height = it->max_ascent + it->max_descent;
8961 if (to_y >= it->current_y
8962 && to_y < it->current_y + line_height)
8963 {
8964 reached = 6;
8965 break;
8966 }
8967 SAVE_IT (it_backup, *it, backup_data);
8968 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8969 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8970 op & MOVE_TO_POS);
8971 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8972 line_height = it->max_ascent + it->max_descent;
8973 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8974
8975 if (to_y >= it->current_y
8976 && to_y < it->current_y + line_height)
8977 {
8978 /* If TO_Y is in this line and TO_X was reached
8979 above, we scanned too far. We have to restore
8980 IT's settings to the ones before skipping. But
8981 keep the more accurate values of max_ascent and
8982 max_descent we've found while skipping the rest
8983 of the line, for the sake of callers, such as
8984 pos_visible_p, that need to know the line
8985 height. */
8986 int max_ascent = it->max_ascent;
8987 int max_descent = it->max_descent;
8988
8989 RESTORE_IT (it, &it_backup, backup_data);
8990 it->max_ascent = max_ascent;
8991 it->max_descent = max_descent;
8992 reached = 6;
8993 }
8994 else
8995 {
8996 skip = skip2;
8997 if (skip == MOVE_POS_MATCH_OR_ZV)
8998 reached = 7;
8999 }
9000 }
9001 else
9002 {
9003 /* Check whether TO_Y is in this line. */
9004 line_height = it->max_ascent + it->max_descent;
9005 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9006
9007 if (to_y >= it->current_y
9008 && to_y < it->current_y + line_height)
9009 {
9010 /* When word-wrap is on, TO_X may lie past the end
9011 of a wrapped line. Then it->current is the
9012 character on the next line, so backtrack to the
9013 space before the wrap point. */
9014 if (skip == MOVE_LINE_CONTINUED
9015 && it->line_wrap == WORD_WRAP)
9016 {
9017 int prev_x = max (it->current_x - 1, 0);
9018 RESTORE_IT (it, &it_backup, backup_data);
9019 skip = move_it_in_display_line_to
9020 (it, -1, prev_x, MOVE_TO_X);
9021 }
9022 reached = 6;
9023 }
9024 }
9025
9026 if (reached)
9027 break;
9028 }
9029 else if (BUFFERP (it->object)
9030 && (it->method == GET_FROM_BUFFER
9031 || it->method == GET_FROM_STRETCH)
9032 && IT_CHARPOS (*it) >= to_charpos
9033 /* Under bidi iteration, a call to set_iterator_to_next
9034 can scan far beyond to_charpos if the initial
9035 portion of the next line needs to be reordered. In
9036 that case, give move_it_in_display_line_to another
9037 chance below. */
9038 && !(it->bidi_p
9039 && it->bidi_it.scan_dir == -1))
9040 skip = MOVE_POS_MATCH_OR_ZV;
9041 else
9042 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9043
9044 switch (skip)
9045 {
9046 case MOVE_POS_MATCH_OR_ZV:
9047 reached = 8;
9048 goto out;
9049
9050 case MOVE_NEWLINE_OR_CR:
9051 set_iterator_to_next (it, 1);
9052 it->continuation_lines_width = 0;
9053 break;
9054
9055 case MOVE_LINE_TRUNCATED:
9056 it->continuation_lines_width = 0;
9057 reseat_at_next_visible_line_start (it, 0);
9058 if ((op & MOVE_TO_POS) != 0
9059 && IT_CHARPOS (*it) > to_charpos)
9060 {
9061 reached = 9;
9062 goto out;
9063 }
9064 break;
9065
9066 case MOVE_LINE_CONTINUED:
9067 /* For continued lines ending in a tab, some of the glyphs
9068 associated with the tab are displayed on the current
9069 line. Since it->current_x does not include these glyphs,
9070 we use it->last_visible_x instead. */
9071 if (it->c == '\t')
9072 {
9073 it->continuation_lines_width += it->last_visible_x;
9074 /* When moving by vpos, ensure that the iterator really
9075 advances to the next line (bug#847, bug#969). Fixme:
9076 do we need to do this in other circumstances? */
9077 if (it->current_x != it->last_visible_x
9078 && (op & MOVE_TO_VPOS)
9079 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9080 {
9081 line_start_x = it->current_x + it->pixel_width
9082 - it->last_visible_x;
9083 set_iterator_to_next (it, 0);
9084 }
9085 }
9086 else
9087 it->continuation_lines_width += it->current_x;
9088 break;
9089
9090 default:
9091 emacs_abort ();
9092 }
9093
9094 /* Reset/increment for the next run. */
9095 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9096 it->current_x = line_start_x;
9097 line_start_x = 0;
9098 it->hpos = 0;
9099 it->current_y += it->max_ascent + it->max_descent;
9100 ++it->vpos;
9101 last_height = it->max_ascent + it->max_descent;
9102 it->max_ascent = it->max_descent = 0;
9103 }
9104
9105 out:
9106
9107 /* On text terminals, we may stop at the end of a line in the middle
9108 of a multi-character glyph. If the glyph itself is continued,
9109 i.e. it is actually displayed on the next line, don't treat this
9110 stopping point as valid; move to the next line instead (unless
9111 that brings us offscreen). */
9112 if (!FRAME_WINDOW_P (it->f)
9113 && op & MOVE_TO_POS
9114 && IT_CHARPOS (*it) == to_charpos
9115 && it->what == IT_CHARACTER
9116 && it->nglyphs > 1
9117 && it->line_wrap == WINDOW_WRAP
9118 && it->current_x == it->last_visible_x - 1
9119 && it->c != '\n'
9120 && it->c != '\t'
9121 && it->vpos < it->w->window_end_vpos)
9122 {
9123 it->continuation_lines_width += it->current_x;
9124 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9125 it->current_y += it->max_ascent + it->max_descent;
9126 ++it->vpos;
9127 last_height = it->max_ascent + it->max_descent;
9128 }
9129
9130 if (backup_data)
9131 bidi_unshelve_cache (backup_data, 1);
9132
9133 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9134 }
9135
9136
9137 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9138
9139 If DY > 0, move IT backward at least that many pixels. DY = 0
9140 means move IT backward to the preceding line start or BEGV. This
9141 function may move over more than DY pixels if IT->current_y - DY
9142 ends up in the middle of a line; in this case IT->current_y will be
9143 set to the top of the line moved to. */
9144
9145 void
9146 move_it_vertically_backward (struct it *it, int dy)
9147 {
9148 int nlines, h;
9149 struct it it2, it3;
9150 void *it2data = NULL, *it3data = NULL;
9151 ptrdiff_t start_pos;
9152 int nchars_per_row
9153 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9154 ptrdiff_t pos_limit;
9155
9156 move_further_back:
9157 eassert (dy >= 0);
9158
9159 start_pos = IT_CHARPOS (*it);
9160
9161 /* Estimate how many newlines we must move back. */
9162 nlines = max (1, dy / default_line_pixel_height (it->w));
9163 if (it->line_wrap == TRUNCATE)
9164 pos_limit = BEGV;
9165 else
9166 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9167
9168 /* Set the iterator's position that many lines back. But don't go
9169 back more than NLINES full screen lines -- this wins a day with
9170 buffers which have very long lines. */
9171 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9172 back_to_previous_visible_line_start (it);
9173
9174 /* Reseat the iterator here. When moving backward, we don't want
9175 reseat to skip forward over invisible text, set up the iterator
9176 to deliver from overlay strings at the new position etc. So,
9177 use reseat_1 here. */
9178 reseat_1 (it, it->current.pos, 1);
9179
9180 /* We are now surely at a line start. */
9181 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9182 reordering is in effect. */
9183 it->continuation_lines_width = 0;
9184
9185 /* Move forward and see what y-distance we moved. First move to the
9186 start of the next line so that we get its height. We need this
9187 height to be able to tell whether we reached the specified
9188 y-distance. */
9189 SAVE_IT (it2, *it, it2data);
9190 it2.max_ascent = it2.max_descent = 0;
9191 do
9192 {
9193 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9194 MOVE_TO_POS | MOVE_TO_VPOS);
9195 }
9196 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9197 /* If we are in a display string which starts at START_POS,
9198 and that display string includes a newline, and we are
9199 right after that newline (i.e. at the beginning of a
9200 display line), exit the loop, because otherwise we will
9201 infloop, since move_it_to will see that it is already at
9202 START_POS and will not move. */
9203 || (it2.method == GET_FROM_STRING
9204 && IT_CHARPOS (it2) == start_pos
9205 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9206 eassert (IT_CHARPOS (*it) >= BEGV);
9207 SAVE_IT (it3, it2, it3data);
9208
9209 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9210 eassert (IT_CHARPOS (*it) >= BEGV);
9211 /* H is the actual vertical distance from the position in *IT
9212 and the starting position. */
9213 h = it2.current_y - it->current_y;
9214 /* NLINES is the distance in number of lines. */
9215 nlines = it2.vpos - it->vpos;
9216
9217 /* Correct IT's y and vpos position
9218 so that they are relative to the starting point. */
9219 it->vpos -= nlines;
9220 it->current_y -= h;
9221
9222 if (dy == 0)
9223 {
9224 /* DY == 0 means move to the start of the screen line. The
9225 value of nlines is > 0 if continuation lines were involved,
9226 or if the original IT position was at start of a line. */
9227 RESTORE_IT (it, it, it2data);
9228 if (nlines > 0)
9229 move_it_by_lines (it, nlines);
9230 /* The above code moves us to some position NLINES down,
9231 usually to its first glyph (leftmost in an L2R line), but
9232 that's not necessarily the start of the line, under bidi
9233 reordering. We want to get to the character position
9234 that is immediately after the newline of the previous
9235 line. */
9236 if (it->bidi_p
9237 && !it->continuation_lines_width
9238 && !STRINGP (it->string)
9239 && IT_CHARPOS (*it) > BEGV
9240 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9241 {
9242 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9243
9244 DEC_BOTH (cp, bp);
9245 cp = find_newline_no_quit (cp, bp, -1, NULL);
9246 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9247 }
9248 bidi_unshelve_cache (it3data, 1);
9249 }
9250 else
9251 {
9252 /* The y-position we try to reach, relative to *IT.
9253 Note that H has been subtracted in front of the if-statement. */
9254 int target_y = it->current_y + h - dy;
9255 int y0 = it3.current_y;
9256 int y1;
9257 int line_height;
9258
9259 RESTORE_IT (&it3, &it3, it3data);
9260 y1 = line_bottom_y (&it3);
9261 line_height = y1 - y0;
9262 RESTORE_IT (it, it, it2data);
9263 /* If we did not reach target_y, try to move further backward if
9264 we can. If we moved too far backward, try to move forward. */
9265 if (target_y < it->current_y
9266 /* This is heuristic. In a window that's 3 lines high, with
9267 a line height of 13 pixels each, recentering with point
9268 on the bottom line will try to move -39/2 = 19 pixels
9269 backward. Try to avoid moving into the first line. */
9270 && (it->current_y - target_y
9271 > min (window_box_height (it->w), line_height * 2 / 3))
9272 && IT_CHARPOS (*it) > BEGV)
9273 {
9274 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9275 target_y - it->current_y));
9276 dy = it->current_y - target_y;
9277 goto move_further_back;
9278 }
9279 else if (target_y >= it->current_y + line_height
9280 && IT_CHARPOS (*it) < ZV)
9281 {
9282 /* Should move forward by at least one line, maybe more.
9283
9284 Note: Calling move_it_by_lines can be expensive on
9285 terminal frames, where compute_motion is used (via
9286 vmotion) to do the job, when there are very long lines
9287 and truncate-lines is nil. That's the reason for
9288 treating terminal frames specially here. */
9289
9290 if (!FRAME_WINDOW_P (it->f))
9291 move_it_vertically (it, target_y - (it->current_y + line_height));
9292 else
9293 {
9294 do
9295 {
9296 move_it_by_lines (it, 1);
9297 }
9298 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9299 }
9300 }
9301 }
9302 }
9303
9304
9305 /* Move IT by a specified amount of pixel lines DY. DY negative means
9306 move backwards. DY = 0 means move to start of screen line. At the
9307 end, IT will be on the start of a screen line. */
9308
9309 void
9310 move_it_vertically (struct it *it, int dy)
9311 {
9312 if (dy <= 0)
9313 move_it_vertically_backward (it, -dy);
9314 else
9315 {
9316 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9317 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9318 MOVE_TO_POS | MOVE_TO_Y);
9319 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9320
9321 /* If buffer ends in ZV without a newline, move to the start of
9322 the line to satisfy the post-condition. */
9323 if (IT_CHARPOS (*it) == ZV
9324 && ZV > BEGV
9325 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9326 move_it_by_lines (it, 0);
9327 }
9328 }
9329
9330
9331 /* Move iterator IT past the end of the text line it is in. */
9332
9333 void
9334 move_it_past_eol (struct it *it)
9335 {
9336 enum move_it_result rc;
9337
9338 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9339 if (rc == MOVE_NEWLINE_OR_CR)
9340 set_iterator_to_next (it, 0);
9341 }
9342
9343
9344 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9345 negative means move up. DVPOS == 0 means move to the start of the
9346 screen line.
9347
9348 Optimization idea: If we would know that IT->f doesn't use
9349 a face with proportional font, we could be faster for
9350 truncate-lines nil. */
9351
9352 void
9353 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9354 {
9355
9356 /* The commented-out optimization uses vmotion on terminals. This
9357 gives bad results, because elements like it->what, on which
9358 callers such as pos_visible_p rely, aren't updated. */
9359 /* struct position pos;
9360 if (!FRAME_WINDOW_P (it->f))
9361 {
9362 struct text_pos textpos;
9363
9364 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9365 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9366 reseat (it, textpos, 1);
9367 it->vpos += pos.vpos;
9368 it->current_y += pos.vpos;
9369 }
9370 else */
9371
9372 if (dvpos == 0)
9373 {
9374 /* DVPOS == 0 means move to the start of the screen line. */
9375 move_it_vertically_backward (it, 0);
9376 /* Let next call to line_bottom_y calculate real line height */
9377 last_height = 0;
9378 }
9379 else if (dvpos > 0)
9380 {
9381 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9382 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9383 {
9384 /* Only move to the next buffer position if we ended up in a
9385 string from display property, not in an overlay string
9386 (before-string or after-string). That is because the
9387 latter don't conceal the underlying buffer position, so
9388 we can ask to move the iterator to the exact position we
9389 are interested in. Note that, even if we are already at
9390 IT_CHARPOS (*it), the call below is not a no-op, as it
9391 will detect that we are at the end of the string, pop the
9392 iterator, and compute it->current_x and it->hpos
9393 correctly. */
9394 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9395 -1, -1, -1, MOVE_TO_POS);
9396 }
9397 }
9398 else
9399 {
9400 struct it it2;
9401 void *it2data = NULL;
9402 ptrdiff_t start_charpos, i;
9403 int nchars_per_row
9404 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9405 ptrdiff_t pos_limit;
9406
9407 /* Start at the beginning of the screen line containing IT's
9408 position. This may actually move vertically backwards,
9409 in case of overlays, so adjust dvpos accordingly. */
9410 dvpos += it->vpos;
9411 move_it_vertically_backward (it, 0);
9412 dvpos -= it->vpos;
9413
9414 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9415 screen lines, and reseat the iterator there. */
9416 start_charpos = IT_CHARPOS (*it);
9417 if (it->line_wrap == TRUNCATE)
9418 pos_limit = BEGV;
9419 else
9420 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9421 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9422 back_to_previous_visible_line_start (it);
9423 reseat (it, it->current.pos, 1);
9424
9425 /* Move further back if we end up in a string or an image. */
9426 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9427 {
9428 /* First try to move to start of display line. */
9429 dvpos += it->vpos;
9430 move_it_vertically_backward (it, 0);
9431 dvpos -= it->vpos;
9432 if (IT_POS_VALID_AFTER_MOVE_P (it))
9433 break;
9434 /* If start of line is still in string or image,
9435 move further back. */
9436 back_to_previous_visible_line_start (it);
9437 reseat (it, it->current.pos, 1);
9438 dvpos--;
9439 }
9440
9441 it->current_x = it->hpos = 0;
9442
9443 /* Above call may have moved too far if continuation lines
9444 are involved. Scan forward and see if it did. */
9445 SAVE_IT (it2, *it, it2data);
9446 it2.vpos = it2.current_y = 0;
9447 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9448 it->vpos -= it2.vpos;
9449 it->current_y -= it2.current_y;
9450 it->current_x = it->hpos = 0;
9451
9452 /* If we moved too far back, move IT some lines forward. */
9453 if (it2.vpos > -dvpos)
9454 {
9455 int delta = it2.vpos + dvpos;
9456
9457 RESTORE_IT (&it2, &it2, it2data);
9458 SAVE_IT (it2, *it, it2data);
9459 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9460 /* Move back again if we got too far ahead. */
9461 if (IT_CHARPOS (*it) >= start_charpos)
9462 RESTORE_IT (it, &it2, it2data);
9463 else
9464 bidi_unshelve_cache (it2data, 1);
9465 }
9466 else
9467 RESTORE_IT (it, it, it2data);
9468 }
9469 }
9470
9471 /* Return 1 if IT points into the middle of a display vector. */
9472
9473 int
9474 in_display_vector_p (struct it *it)
9475 {
9476 return (it->method == GET_FROM_DISPLAY_VECTOR
9477 && it->current.dpvec_index > 0
9478 && it->dpvec + it->current.dpvec_index != it->dpend);
9479 }
9480
9481 \f
9482 /***********************************************************************
9483 Messages
9484 ***********************************************************************/
9485
9486
9487 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9488 to *Messages*. */
9489
9490 void
9491 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9492 {
9493 Lisp_Object args[3];
9494 Lisp_Object msg, fmt;
9495 char *buffer;
9496 ptrdiff_t len;
9497 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9498 USE_SAFE_ALLOCA;
9499
9500 fmt = msg = Qnil;
9501 GCPRO4 (fmt, msg, arg1, arg2);
9502
9503 args[0] = fmt = build_string (format);
9504 args[1] = arg1;
9505 args[2] = arg2;
9506 msg = Fformat (3, args);
9507
9508 len = SBYTES (msg) + 1;
9509 buffer = SAFE_ALLOCA (len);
9510 memcpy (buffer, SDATA (msg), len);
9511
9512 message_dolog (buffer, len - 1, 1, 0);
9513 SAFE_FREE ();
9514
9515 UNGCPRO;
9516 }
9517
9518
9519 /* Output a newline in the *Messages* buffer if "needs" one. */
9520
9521 void
9522 message_log_maybe_newline (void)
9523 {
9524 if (message_log_need_newline)
9525 message_dolog ("", 0, 1, 0);
9526 }
9527
9528
9529 /* Add a string M of length NBYTES to the message log, optionally
9530 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9531 true, means interpret the contents of M as multibyte. This
9532 function calls low-level routines in order to bypass text property
9533 hooks, etc. which might not be safe to run.
9534
9535 This may GC (insert may run before/after change hooks),
9536 so the buffer M must NOT point to a Lisp string. */
9537
9538 void
9539 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9540 {
9541 const unsigned char *msg = (const unsigned char *) m;
9542
9543 if (!NILP (Vmemory_full))
9544 return;
9545
9546 if (!NILP (Vmessage_log_max))
9547 {
9548 struct buffer *oldbuf;
9549 Lisp_Object oldpoint, oldbegv, oldzv;
9550 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9551 ptrdiff_t point_at_end = 0;
9552 ptrdiff_t zv_at_end = 0;
9553 Lisp_Object old_deactivate_mark;
9554 bool shown;
9555 struct gcpro gcpro1;
9556
9557 old_deactivate_mark = Vdeactivate_mark;
9558 oldbuf = current_buffer;
9559 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9560 bset_undo_list (current_buffer, Qt);
9561
9562 oldpoint = message_dolog_marker1;
9563 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9564 oldbegv = message_dolog_marker2;
9565 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9566 oldzv = message_dolog_marker3;
9567 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9568 GCPRO1 (old_deactivate_mark);
9569
9570 if (PT == Z)
9571 point_at_end = 1;
9572 if (ZV == Z)
9573 zv_at_end = 1;
9574
9575 BEGV = BEG;
9576 BEGV_BYTE = BEG_BYTE;
9577 ZV = Z;
9578 ZV_BYTE = Z_BYTE;
9579 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9580
9581 /* Insert the string--maybe converting multibyte to single byte
9582 or vice versa, so that all the text fits the buffer. */
9583 if (multibyte
9584 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9585 {
9586 ptrdiff_t i;
9587 int c, char_bytes;
9588 char work[1];
9589
9590 /* Convert a multibyte string to single-byte
9591 for the *Message* buffer. */
9592 for (i = 0; i < nbytes; i += char_bytes)
9593 {
9594 c = string_char_and_length (msg + i, &char_bytes);
9595 work[0] = (ASCII_CHAR_P (c)
9596 ? c
9597 : multibyte_char_to_unibyte (c));
9598 insert_1_both (work, 1, 1, 1, 0, 0);
9599 }
9600 }
9601 else if (! multibyte
9602 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9603 {
9604 ptrdiff_t i;
9605 int c, char_bytes;
9606 unsigned char str[MAX_MULTIBYTE_LENGTH];
9607 /* Convert a single-byte string to multibyte
9608 for the *Message* buffer. */
9609 for (i = 0; i < nbytes; i++)
9610 {
9611 c = msg[i];
9612 MAKE_CHAR_MULTIBYTE (c);
9613 char_bytes = CHAR_STRING (c, str);
9614 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9615 }
9616 }
9617 else if (nbytes)
9618 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9619
9620 if (nlflag)
9621 {
9622 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9623 printmax_t dups;
9624
9625 insert_1_both ("\n", 1, 1, 1, 0, 0);
9626
9627 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9628 this_bol = PT;
9629 this_bol_byte = PT_BYTE;
9630
9631 /* See if this line duplicates the previous one.
9632 If so, combine duplicates. */
9633 if (this_bol > BEG)
9634 {
9635 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9636 prev_bol = PT;
9637 prev_bol_byte = PT_BYTE;
9638
9639 dups = message_log_check_duplicate (prev_bol_byte,
9640 this_bol_byte);
9641 if (dups)
9642 {
9643 del_range_both (prev_bol, prev_bol_byte,
9644 this_bol, this_bol_byte, 0);
9645 if (dups > 1)
9646 {
9647 char dupstr[sizeof " [ times]"
9648 + INT_STRLEN_BOUND (printmax_t)];
9649
9650 /* If you change this format, don't forget to also
9651 change message_log_check_duplicate. */
9652 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9653 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9654 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9655 }
9656 }
9657 }
9658
9659 /* If we have more than the desired maximum number of lines
9660 in the *Messages* buffer now, delete the oldest ones.
9661 This is safe because we don't have undo in this buffer. */
9662
9663 if (NATNUMP (Vmessage_log_max))
9664 {
9665 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9666 -XFASTINT (Vmessage_log_max) - 1, 0);
9667 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9668 }
9669 }
9670 BEGV = marker_position (oldbegv);
9671 BEGV_BYTE = marker_byte_position (oldbegv);
9672
9673 if (zv_at_end)
9674 {
9675 ZV = Z;
9676 ZV_BYTE = Z_BYTE;
9677 }
9678 else
9679 {
9680 ZV = marker_position (oldzv);
9681 ZV_BYTE = marker_byte_position (oldzv);
9682 }
9683
9684 if (point_at_end)
9685 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9686 else
9687 /* We can't do Fgoto_char (oldpoint) because it will run some
9688 Lisp code. */
9689 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9690 marker_byte_position (oldpoint));
9691
9692 UNGCPRO;
9693 unchain_marker (XMARKER (oldpoint));
9694 unchain_marker (XMARKER (oldbegv));
9695 unchain_marker (XMARKER (oldzv));
9696
9697 shown = buffer_window_count (current_buffer) > 0;
9698 set_buffer_internal (oldbuf);
9699 /* We called insert_1_both above with its 5th argument (PREPARE)
9700 zero, which prevents insert_1_both from calling
9701 prepare_to_modify_buffer, which in turns prevents us from
9702 incrementing windows_or_buffers_changed even if *Messages* is
9703 shown in some window. So we must manually incrementing
9704 windows_or_buffers_changed here to make up for that. */
9705 if (shown)
9706 windows_or_buffers_changed++;
9707 else
9708 windows_or_buffers_changed = old_windows_or_buffers_changed;
9709 message_log_need_newline = !nlflag;
9710 Vdeactivate_mark = old_deactivate_mark;
9711 }
9712 }
9713
9714
9715 /* We are at the end of the buffer after just having inserted a newline.
9716 (Note: We depend on the fact we won't be crossing the gap.)
9717 Check to see if the most recent message looks a lot like the previous one.
9718 Return 0 if different, 1 if the new one should just replace it, or a
9719 value N > 1 if we should also append " [N times]". */
9720
9721 static intmax_t
9722 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9723 {
9724 ptrdiff_t i;
9725 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9726 int seen_dots = 0;
9727 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9728 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9729
9730 for (i = 0; i < len; i++)
9731 {
9732 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9733 seen_dots = 1;
9734 if (p1[i] != p2[i])
9735 return seen_dots;
9736 }
9737 p1 += len;
9738 if (*p1 == '\n')
9739 return 2;
9740 if (*p1++ == ' ' && *p1++ == '[')
9741 {
9742 char *pend;
9743 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9744 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9745 return n + 1;
9746 }
9747 return 0;
9748 }
9749 \f
9750
9751 /* Display an echo area message M with a specified length of NBYTES
9752 bytes. The string may include null characters. If M is not a
9753 string, clear out any existing message, and let the mini-buffer
9754 text show through.
9755
9756 This function cancels echoing. */
9757
9758 void
9759 message3 (Lisp_Object m)
9760 {
9761 struct gcpro gcpro1;
9762
9763 GCPRO1 (m);
9764 clear_message (1,1);
9765 cancel_echoing ();
9766
9767 /* First flush out any partial line written with print. */
9768 message_log_maybe_newline ();
9769 if (STRINGP (m))
9770 {
9771 ptrdiff_t nbytes = SBYTES (m);
9772 bool multibyte = STRING_MULTIBYTE (m);
9773 USE_SAFE_ALLOCA;
9774 char *buffer = SAFE_ALLOCA (nbytes);
9775 memcpy (buffer, SDATA (m), nbytes);
9776 message_dolog (buffer, nbytes, 1, multibyte);
9777 SAFE_FREE ();
9778 }
9779 message3_nolog (m);
9780
9781 UNGCPRO;
9782 }
9783
9784
9785 /* The non-logging version of message3.
9786 This does not cancel echoing, because it is used for echoing.
9787 Perhaps we need to make a separate function for echoing
9788 and make this cancel echoing. */
9789
9790 void
9791 message3_nolog (Lisp_Object m)
9792 {
9793 struct frame *sf = SELECTED_FRAME ();
9794
9795 if (FRAME_INITIAL_P (sf))
9796 {
9797 if (noninteractive_need_newline)
9798 putc ('\n', stderr);
9799 noninteractive_need_newline = 0;
9800 if (STRINGP (m))
9801 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9802 if (cursor_in_echo_area == 0)
9803 fprintf (stderr, "\n");
9804 fflush (stderr);
9805 }
9806 /* Error messages get reported properly by cmd_error, so this must be just an
9807 informative message; if the frame hasn't really been initialized yet, just
9808 toss it. */
9809 else if (INTERACTIVE && sf->glyphs_initialized_p)
9810 {
9811 /* Get the frame containing the mini-buffer
9812 that the selected frame is using. */
9813 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9814 Lisp_Object frame = XWINDOW (mini_window)->frame;
9815 struct frame *f = XFRAME (frame);
9816
9817 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9818 Fmake_frame_visible (frame);
9819
9820 if (STRINGP (m) && SCHARS (m) > 0)
9821 {
9822 set_message (m);
9823 if (minibuffer_auto_raise)
9824 Fraise_frame (frame);
9825 /* Assume we are not echoing.
9826 (If we are, echo_now will override this.) */
9827 echo_message_buffer = Qnil;
9828 }
9829 else
9830 clear_message (1, 1);
9831
9832 do_pending_window_change (0);
9833 echo_area_display (1);
9834 do_pending_window_change (0);
9835 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9836 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9837 }
9838 }
9839
9840
9841 /* Display a null-terminated echo area message M. If M is 0, clear
9842 out any existing message, and let the mini-buffer text show through.
9843
9844 The buffer M must continue to exist until after the echo area gets
9845 cleared or some other message gets displayed there. Do not pass
9846 text that is stored in a Lisp string. Do not pass text in a buffer
9847 that was alloca'd. */
9848
9849 void
9850 message1 (const char *m)
9851 {
9852 message3 (m ? build_unibyte_string (m) : Qnil);
9853 }
9854
9855
9856 /* The non-logging counterpart of message1. */
9857
9858 void
9859 message1_nolog (const char *m)
9860 {
9861 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9862 }
9863
9864 /* Display a message M which contains a single %s
9865 which gets replaced with STRING. */
9866
9867 void
9868 message_with_string (const char *m, Lisp_Object string, int log)
9869 {
9870 CHECK_STRING (string);
9871
9872 if (noninteractive)
9873 {
9874 if (m)
9875 {
9876 if (noninteractive_need_newline)
9877 putc ('\n', stderr);
9878 noninteractive_need_newline = 0;
9879 fprintf (stderr, m, SDATA (string));
9880 if (!cursor_in_echo_area)
9881 fprintf (stderr, "\n");
9882 fflush (stderr);
9883 }
9884 }
9885 else if (INTERACTIVE)
9886 {
9887 /* The frame whose minibuffer we're going to display the message on.
9888 It may be larger than the selected frame, so we need
9889 to use its buffer, not the selected frame's buffer. */
9890 Lisp_Object mini_window;
9891 struct frame *f, *sf = SELECTED_FRAME ();
9892
9893 /* Get the frame containing the minibuffer
9894 that the selected frame is using. */
9895 mini_window = FRAME_MINIBUF_WINDOW (sf);
9896 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9897
9898 /* Error messages get reported properly by cmd_error, so this must be
9899 just an informative message; if the frame hasn't really been
9900 initialized yet, just toss it. */
9901 if (f->glyphs_initialized_p)
9902 {
9903 Lisp_Object args[2], msg;
9904 struct gcpro gcpro1, gcpro2;
9905
9906 args[0] = build_string (m);
9907 args[1] = msg = string;
9908 GCPRO2 (args[0], msg);
9909 gcpro1.nvars = 2;
9910
9911 msg = Fformat (2, args);
9912
9913 if (log)
9914 message3 (msg);
9915 else
9916 message3_nolog (msg);
9917
9918 UNGCPRO;
9919
9920 /* Print should start at the beginning of the message
9921 buffer next time. */
9922 message_buf_print = 0;
9923 }
9924 }
9925 }
9926
9927
9928 /* Dump an informative message to the minibuf. If M is 0, clear out
9929 any existing message, and let the mini-buffer text show through. */
9930
9931 static void
9932 vmessage (const char *m, va_list ap)
9933 {
9934 if (noninteractive)
9935 {
9936 if (m)
9937 {
9938 if (noninteractive_need_newline)
9939 putc ('\n', stderr);
9940 noninteractive_need_newline = 0;
9941 vfprintf (stderr, m, ap);
9942 if (cursor_in_echo_area == 0)
9943 fprintf (stderr, "\n");
9944 fflush (stderr);
9945 }
9946 }
9947 else if (INTERACTIVE)
9948 {
9949 /* The frame whose mini-buffer we're going to display the message
9950 on. It may be larger than the selected frame, so we need to
9951 use its buffer, not the selected frame's buffer. */
9952 Lisp_Object mini_window;
9953 struct frame *f, *sf = SELECTED_FRAME ();
9954
9955 /* Get the frame containing the mini-buffer
9956 that the selected frame is using. */
9957 mini_window = FRAME_MINIBUF_WINDOW (sf);
9958 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9959
9960 /* Error messages get reported properly by cmd_error, so this must be
9961 just an informative message; if the frame hasn't really been
9962 initialized yet, just toss it. */
9963 if (f->glyphs_initialized_p)
9964 {
9965 if (m)
9966 {
9967 ptrdiff_t len;
9968 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9969 char *message_buf = alloca (maxsize + 1);
9970
9971 len = doprnt (message_buf, maxsize, m, 0, ap);
9972
9973 message3 (make_string (message_buf, len));
9974 }
9975 else
9976 message1 (0);
9977
9978 /* Print should start at the beginning of the message
9979 buffer next time. */
9980 message_buf_print = 0;
9981 }
9982 }
9983 }
9984
9985 void
9986 message (const char *m, ...)
9987 {
9988 va_list ap;
9989 va_start (ap, m);
9990 vmessage (m, ap);
9991 va_end (ap);
9992 }
9993
9994
9995 #if 0
9996 /* The non-logging version of message. */
9997
9998 void
9999 message_nolog (const char *m, ...)
10000 {
10001 Lisp_Object old_log_max;
10002 va_list ap;
10003 va_start (ap, m);
10004 old_log_max = Vmessage_log_max;
10005 Vmessage_log_max = Qnil;
10006 vmessage (m, ap);
10007 Vmessage_log_max = old_log_max;
10008 va_end (ap);
10009 }
10010 #endif
10011
10012
10013 /* Display the current message in the current mini-buffer. This is
10014 only called from error handlers in process.c, and is not time
10015 critical. */
10016
10017 void
10018 update_echo_area (void)
10019 {
10020 if (!NILP (echo_area_buffer[0]))
10021 {
10022 Lisp_Object string;
10023 string = Fcurrent_message ();
10024 message3 (string);
10025 }
10026 }
10027
10028
10029 /* Make sure echo area buffers in `echo_buffers' are live.
10030 If they aren't, make new ones. */
10031
10032 static void
10033 ensure_echo_area_buffers (void)
10034 {
10035 int i;
10036
10037 for (i = 0; i < 2; ++i)
10038 if (!BUFFERP (echo_buffer[i])
10039 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10040 {
10041 char name[30];
10042 Lisp_Object old_buffer;
10043 int j;
10044
10045 old_buffer = echo_buffer[i];
10046 echo_buffer[i] = Fget_buffer_create
10047 (make_formatted_string (name, " *Echo Area %d*", i));
10048 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10049 /* to force word wrap in echo area -
10050 it was decided to postpone this*/
10051 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10052
10053 for (j = 0; j < 2; ++j)
10054 if (EQ (old_buffer, echo_area_buffer[j]))
10055 echo_area_buffer[j] = echo_buffer[i];
10056 }
10057 }
10058
10059
10060 /* Call FN with args A1..A2 with either the current or last displayed
10061 echo_area_buffer as current buffer.
10062
10063 WHICH zero means use the current message buffer
10064 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10065 from echo_buffer[] and clear it.
10066
10067 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10068 suitable buffer from echo_buffer[] and clear it.
10069
10070 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10071 that the current message becomes the last displayed one, make
10072 choose a suitable buffer for echo_area_buffer[0], and clear it.
10073
10074 Value is what FN returns. */
10075
10076 static int
10077 with_echo_area_buffer (struct window *w, int which,
10078 int (*fn) (ptrdiff_t, Lisp_Object),
10079 ptrdiff_t a1, Lisp_Object a2)
10080 {
10081 Lisp_Object buffer;
10082 int this_one, the_other, clear_buffer_p, rc;
10083 ptrdiff_t count = SPECPDL_INDEX ();
10084
10085 /* If buffers aren't live, make new ones. */
10086 ensure_echo_area_buffers ();
10087
10088 clear_buffer_p = 0;
10089
10090 if (which == 0)
10091 this_one = 0, the_other = 1;
10092 else if (which > 0)
10093 this_one = 1, the_other = 0;
10094 else
10095 {
10096 this_one = 0, the_other = 1;
10097 clear_buffer_p = 1;
10098
10099 /* We need a fresh one in case the current echo buffer equals
10100 the one containing the last displayed echo area message. */
10101 if (!NILP (echo_area_buffer[this_one])
10102 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10103 echo_area_buffer[this_one] = Qnil;
10104 }
10105
10106 /* Choose a suitable buffer from echo_buffer[] is we don't
10107 have one. */
10108 if (NILP (echo_area_buffer[this_one]))
10109 {
10110 echo_area_buffer[this_one]
10111 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10112 ? echo_buffer[the_other]
10113 : echo_buffer[this_one]);
10114 clear_buffer_p = 1;
10115 }
10116
10117 buffer = echo_area_buffer[this_one];
10118
10119 /* Don't get confused by reusing the buffer used for echoing
10120 for a different purpose. */
10121 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10122 cancel_echoing ();
10123
10124 record_unwind_protect (unwind_with_echo_area_buffer,
10125 with_echo_area_buffer_unwind_data (w));
10126
10127 /* Make the echo area buffer current. Note that for display
10128 purposes, it is not necessary that the displayed window's buffer
10129 == current_buffer, except for text property lookup. So, let's
10130 only set that buffer temporarily here without doing a full
10131 Fset_window_buffer. We must also change w->pointm, though,
10132 because otherwise an assertions in unshow_buffer fails, and Emacs
10133 aborts. */
10134 set_buffer_internal_1 (XBUFFER (buffer));
10135 if (w)
10136 {
10137 wset_buffer (w, buffer);
10138 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10139 }
10140
10141 bset_undo_list (current_buffer, Qt);
10142 bset_read_only (current_buffer, Qnil);
10143 specbind (Qinhibit_read_only, Qt);
10144 specbind (Qinhibit_modification_hooks, Qt);
10145
10146 if (clear_buffer_p && Z > BEG)
10147 del_range (BEG, Z);
10148
10149 eassert (BEGV >= BEG);
10150 eassert (ZV <= Z && ZV >= BEGV);
10151
10152 rc = fn (a1, a2);
10153
10154 eassert (BEGV >= BEG);
10155 eassert (ZV <= Z && ZV >= BEGV);
10156
10157 unbind_to (count, Qnil);
10158 return rc;
10159 }
10160
10161
10162 /* Save state that should be preserved around the call to the function
10163 FN called in with_echo_area_buffer. */
10164
10165 static Lisp_Object
10166 with_echo_area_buffer_unwind_data (struct window *w)
10167 {
10168 int i = 0;
10169 Lisp_Object vector, tmp;
10170
10171 /* Reduce consing by keeping one vector in
10172 Vwith_echo_area_save_vector. */
10173 vector = Vwith_echo_area_save_vector;
10174 Vwith_echo_area_save_vector = Qnil;
10175
10176 if (NILP (vector))
10177 vector = Fmake_vector (make_number (9), Qnil);
10178
10179 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10180 ASET (vector, i, Vdeactivate_mark); ++i;
10181 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10182
10183 if (w)
10184 {
10185 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10186 ASET (vector, i, w->contents); ++i;
10187 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10188 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10189 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10190 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10191 }
10192 else
10193 {
10194 int end = i + 6;
10195 for (; i < end; ++i)
10196 ASET (vector, i, Qnil);
10197 }
10198
10199 eassert (i == ASIZE (vector));
10200 return vector;
10201 }
10202
10203
10204 /* Restore global state from VECTOR which was created by
10205 with_echo_area_buffer_unwind_data. */
10206
10207 static void
10208 unwind_with_echo_area_buffer (Lisp_Object vector)
10209 {
10210 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10211 Vdeactivate_mark = AREF (vector, 1);
10212 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10213
10214 if (WINDOWP (AREF (vector, 3)))
10215 {
10216 struct window *w;
10217 Lisp_Object buffer;
10218
10219 w = XWINDOW (AREF (vector, 3));
10220 buffer = AREF (vector, 4);
10221
10222 wset_buffer (w, buffer);
10223 set_marker_both (w->pointm, buffer,
10224 XFASTINT (AREF (vector, 5)),
10225 XFASTINT (AREF (vector, 6)));
10226 set_marker_both (w->start, buffer,
10227 XFASTINT (AREF (vector, 7)),
10228 XFASTINT (AREF (vector, 8)));
10229 }
10230
10231 Vwith_echo_area_save_vector = vector;
10232 }
10233
10234
10235 /* Set up the echo area for use by print functions. MULTIBYTE_P
10236 non-zero means we will print multibyte. */
10237
10238 void
10239 setup_echo_area_for_printing (int multibyte_p)
10240 {
10241 /* If we can't find an echo area any more, exit. */
10242 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10243 Fkill_emacs (Qnil);
10244
10245 ensure_echo_area_buffers ();
10246
10247 if (!message_buf_print)
10248 {
10249 /* A message has been output since the last time we printed.
10250 Choose a fresh echo area buffer. */
10251 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10252 echo_area_buffer[0] = echo_buffer[1];
10253 else
10254 echo_area_buffer[0] = echo_buffer[0];
10255
10256 /* Switch to that buffer and clear it. */
10257 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10258 bset_truncate_lines (current_buffer, Qnil);
10259
10260 if (Z > BEG)
10261 {
10262 ptrdiff_t count = SPECPDL_INDEX ();
10263 specbind (Qinhibit_read_only, Qt);
10264 /* Note that undo recording is always disabled. */
10265 del_range (BEG, Z);
10266 unbind_to (count, Qnil);
10267 }
10268 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10269
10270 /* Set up the buffer for the multibyteness we need. */
10271 if (multibyte_p
10272 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10273 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10274
10275 /* Raise the frame containing the echo area. */
10276 if (minibuffer_auto_raise)
10277 {
10278 struct frame *sf = SELECTED_FRAME ();
10279 Lisp_Object mini_window;
10280 mini_window = FRAME_MINIBUF_WINDOW (sf);
10281 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10282 }
10283
10284 message_log_maybe_newline ();
10285 message_buf_print = 1;
10286 }
10287 else
10288 {
10289 if (NILP (echo_area_buffer[0]))
10290 {
10291 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10292 echo_area_buffer[0] = echo_buffer[1];
10293 else
10294 echo_area_buffer[0] = echo_buffer[0];
10295 }
10296
10297 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10298 {
10299 /* Someone switched buffers between print requests. */
10300 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10301 bset_truncate_lines (current_buffer, Qnil);
10302 }
10303 }
10304 }
10305
10306
10307 /* Display an echo area message in window W. Value is non-zero if W's
10308 height is changed. If display_last_displayed_message_p is
10309 non-zero, display the message that was last displayed, otherwise
10310 display the current message. */
10311
10312 static int
10313 display_echo_area (struct window *w)
10314 {
10315 int i, no_message_p, window_height_changed_p;
10316
10317 /* Temporarily disable garbage collections while displaying the echo
10318 area. This is done because a GC can print a message itself.
10319 That message would modify the echo area buffer's contents while a
10320 redisplay of the buffer is going on, and seriously confuse
10321 redisplay. */
10322 ptrdiff_t count = inhibit_garbage_collection ();
10323
10324 /* If there is no message, we must call display_echo_area_1
10325 nevertheless because it resizes the window. But we will have to
10326 reset the echo_area_buffer in question to nil at the end because
10327 with_echo_area_buffer will sets it to an empty buffer. */
10328 i = display_last_displayed_message_p ? 1 : 0;
10329 no_message_p = NILP (echo_area_buffer[i]);
10330
10331 window_height_changed_p
10332 = with_echo_area_buffer (w, display_last_displayed_message_p,
10333 display_echo_area_1,
10334 (intptr_t) w, Qnil);
10335
10336 if (no_message_p)
10337 echo_area_buffer[i] = Qnil;
10338
10339 unbind_to (count, Qnil);
10340 return window_height_changed_p;
10341 }
10342
10343
10344 /* Helper for display_echo_area. Display the current buffer which
10345 contains the current echo area message in window W, a mini-window,
10346 a pointer to which is passed in A1. A2..A4 are currently not used.
10347 Change the height of W so that all of the message is displayed.
10348 Value is non-zero if height of W was changed. */
10349
10350 static int
10351 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10352 {
10353 intptr_t i1 = a1;
10354 struct window *w = (struct window *) i1;
10355 Lisp_Object window;
10356 struct text_pos start;
10357 int window_height_changed_p = 0;
10358
10359 /* Do this before displaying, so that we have a large enough glyph
10360 matrix for the display. If we can't get enough space for the
10361 whole text, display the last N lines. That works by setting w->start. */
10362 window_height_changed_p = resize_mini_window (w, 0);
10363
10364 /* Use the starting position chosen by resize_mini_window. */
10365 SET_TEXT_POS_FROM_MARKER (start, w->start);
10366
10367 /* Display. */
10368 clear_glyph_matrix (w->desired_matrix);
10369 XSETWINDOW (window, w);
10370 try_window (window, start, 0);
10371
10372 return window_height_changed_p;
10373 }
10374
10375
10376 /* Resize the echo area window to exactly the size needed for the
10377 currently displayed message, if there is one. If a mini-buffer
10378 is active, don't shrink it. */
10379
10380 void
10381 resize_echo_area_exactly (void)
10382 {
10383 if (BUFFERP (echo_area_buffer[0])
10384 && WINDOWP (echo_area_window))
10385 {
10386 struct window *w = XWINDOW (echo_area_window);
10387 int resized_p;
10388 Lisp_Object resize_exactly;
10389
10390 if (minibuf_level == 0)
10391 resize_exactly = Qt;
10392 else
10393 resize_exactly = Qnil;
10394
10395 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10396 (intptr_t) w, resize_exactly);
10397 if (resized_p)
10398 {
10399 ++windows_or_buffers_changed;
10400 ++update_mode_lines;
10401 redisplay_internal ();
10402 }
10403 }
10404 }
10405
10406
10407 /* Callback function for with_echo_area_buffer, when used from
10408 resize_echo_area_exactly. A1 contains a pointer to the window to
10409 resize, EXACTLY non-nil means resize the mini-window exactly to the
10410 size of the text displayed. A3 and A4 are not used. Value is what
10411 resize_mini_window returns. */
10412
10413 static int
10414 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10415 {
10416 intptr_t i1 = a1;
10417 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10418 }
10419
10420
10421 /* Resize mini-window W to fit the size of its contents. EXACT_P
10422 means size the window exactly to the size needed. Otherwise, it's
10423 only enlarged until W's buffer is empty.
10424
10425 Set W->start to the right place to begin display. If the whole
10426 contents fit, start at the beginning. Otherwise, start so as
10427 to make the end of the contents appear. This is particularly
10428 important for y-or-n-p, but seems desirable generally.
10429
10430 Value is non-zero if the window height has been changed. */
10431
10432 int
10433 resize_mini_window (struct window *w, int exact_p)
10434 {
10435 struct frame *f = XFRAME (w->frame);
10436 int window_height_changed_p = 0;
10437
10438 eassert (MINI_WINDOW_P (w));
10439
10440 /* By default, start display at the beginning. */
10441 set_marker_both (w->start, w->contents,
10442 BUF_BEGV (XBUFFER (w->contents)),
10443 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10444
10445 /* Don't resize windows while redisplaying a window; it would
10446 confuse redisplay functions when the size of the window they are
10447 displaying changes from under them. Such a resizing can happen,
10448 for instance, when which-func prints a long message while
10449 we are running fontification-functions. We're running these
10450 functions with safe_call which binds inhibit-redisplay to t. */
10451 if (!NILP (Vinhibit_redisplay))
10452 return 0;
10453
10454 /* Nil means don't try to resize. */
10455 if (NILP (Vresize_mini_windows)
10456 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10457 return 0;
10458
10459 if (!FRAME_MINIBUF_ONLY_P (f))
10460 {
10461 struct it it;
10462 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10463 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10464 int height;
10465 EMACS_INT max_height;
10466 int unit = FRAME_LINE_HEIGHT (f);
10467 struct text_pos start;
10468 struct buffer *old_current_buffer = NULL;
10469
10470 if (current_buffer != XBUFFER (w->contents))
10471 {
10472 old_current_buffer = current_buffer;
10473 set_buffer_internal (XBUFFER (w->contents));
10474 }
10475
10476 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10477
10478 /* Compute the max. number of lines specified by the user. */
10479 if (FLOATP (Vmax_mini_window_height))
10480 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10481 else if (INTEGERP (Vmax_mini_window_height))
10482 max_height = XINT (Vmax_mini_window_height);
10483 else
10484 max_height = total_height / 4;
10485
10486 /* Correct that max. height if it's bogus. */
10487 max_height = clip_to_bounds (1, max_height, total_height);
10488
10489 /* Find out the height of the text in the window. */
10490 if (it.line_wrap == TRUNCATE)
10491 height = 1;
10492 else
10493 {
10494 last_height = 0;
10495 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10496 if (it.max_ascent == 0 && it.max_descent == 0)
10497 height = it.current_y + last_height;
10498 else
10499 height = it.current_y + it.max_ascent + it.max_descent;
10500 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10501 height = (height + unit - 1) / unit;
10502 }
10503
10504 /* Compute a suitable window start. */
10505 if (height > max_height)
10506 {
10507 height = max_height;
10508 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10509 move_it_vertically_backward (&it, (height - 1) * unit);
10510 start = it.current.pos;
10511 }
10512 else
10513 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10514 SET_MARKER_FROM_TEXT_POS (w->start, start);
10515
10516 if (EQ (Vresize_mini_windows, Qgrow_only))
10517 {
10518 /* Let it grow only, until we display an empty message, in which
10519 case the window shrinks again. */
10520 if (height > WINDOW_TOTAL_LINES (w))
10521 {
10522 int old_height = WINDOW_TOTAL_LINES (w);
10523
10524 FRAME_WINDOWS_FROZEN (f) = 1;
10525 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10526 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10527 }
10528 else if (height < WINDOW_TOTAL_LINES (w)
10529 && (exact_p || BEGV == ZV))
10530 {
10531 int old_height = WINDOW_TOTAL_LINES (w);
10532
10533 FRAME_WINDOWS_FROZEN (f) = 0;
10534 shrink_mini_window (w);
10535 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10536 }
10537 }
10538 else
10539 {
10540 /* Always resize to exact size needed. */
10541 if (height > WINDOW_TOTAL_LINES (w))
10542 {
10543 int old_height = WINDOW_TOTAL_LINES (w);
10544
10545 FRAME_WINDOWS_FROZEN (f) = 1;
10546 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10547 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10548 }
10549 else if (height < WINDOW_TOTAL_LINES (w))
10550 {
10551 int old_height = WINDOW_TOTAL_LINES (w);
10552
10553 FRAME_WINDOWS_FROZEN (f) = 0;
10554 shrink_mini_window (w);
10555
10556 if (height)
10557 {
10558 FRAME_WINDOWS_FROZEN (f) = 1;
10559 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10560 }
10561
10562 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10563 }
10564 }
10565
10566 if (old_current_buffer)
10567 set_buffer_internal (old_current_buffer);
10568 }
10569
10570 return window_height_changed_p;
10571 }
10572
10573
10574 /* Value is the current message, a string, or nil if there is no
10575 current message. */
10576
10577 Lisp_Object
10578 current_message (void)
10579 {
10580 Lisp_Object msg;
10581
10582 if (!BUFFERP (echo_area_buffer[0]))
10583 msg = Qnil;
10584 else
10585 {
10586 with_echo_area_buffer (0, 0, current_message_1,
10587 (intptr_t) &msg, Qnil);
10588 if (NILP (msg))
10589 echo_area_buffer[0] = Qnil;
10590 }
10591
10592 return msg;
10593 }
10594
10595
10596 static int
10597 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10598 {
10599 intptr_t i1 = a1;
10600 Lisp_Object *msg = (Lisp_Object *) i1;
10601
10602 if (Z > BEG)
10603 *msg = make_buffer_string (BEG, Z, 1);
10604 else
10605 *msg = Qnil;
10606 return 0;
10607 }
10608
10609
10610 /* Push the current message on Vmessage_stack for later restoration
10611 by restore_message. Value is non-zero if the current message isn't
10612 empty. This is a relatively infrequent operation, so it's not
10613 worth optimizing. */
10614
10615 bool
10616 push_message (void)
10617 {
10618 Lisp_Object msg = current_message ();
10619 Vmessage_stack = Fcons (msg, Vmessage_stack);
10620 return STRINGP (msg);
10621 }
10622
10623
10624 /* Restore message display from the top of Vmessage_stack. */
10625
10626 void
10627 restore_message (void)
10628 {
10629 eassert (CONSP (Vmessage_stack));
10630 message3_nolog (XCAR (Vmessage_stack));
10631 }
10632
10633
10634 /* Handler for unwind-protect calling pop_message. */
10635
10636 void
10637 pop_message_unwind (void)
10638 {
10639 /* Pop the top-most entry off Vmessage_stack. */
10640 eassert (CONSP (Vmessage_stack));
10641 Vmessage_stack = XCDR (Vmessage_stack);
10642 }
10643
10644
10645 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10646 exits. If the stack is not empty, we have a missing pop_message
10647 somewhere. */
10648
10649 void
10650 check_message_stack (void)
10651 {
10652 if (!NILP (Vmessage_stack))
10653 emacs_abort ();
10654 }
10655
10656
10657 /* Truncate to NCHARS what will be displayed in the echo area the next
10658 time we display it---but don't redisplay it now. */
10659
10660 void
10661 truncate_echo_area (ptrdiff_t nchars)
10662 {
10663 if (nchars == 0)
10664 echo_area_buffer[0] = Qnil;
10665 else if (!noninteractive
10666 && INTERACTIVE
10667 && !NILP (echo_area_buffer[0]))
10668 {
10669 struct frame *sf = SELECTED_FRAME ();
10670 /* Error messages get reported properly by cmd_error, so this must be
10671 just an informative message; if the frame hasn't really been
10672 initialized yet, just toss it. */
10673 if (sf->glyphs_initialized_p)
10674 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10675 }
10676 }
10677
10678
10679 /* Helper function for truncate_echo_area. Truncate the current
10680 message to at most NCHARS characters. */
10681
10682 static int
10683 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10684 {
10685 if (BEG + nchars < Z)
10686 del_range (BEG + nchars, Z);
10687 if (Z == BEG)
10688 echo_area_buffer[0] = Qnil;
10689 return 0;
10690 }
10691
10692 /* Set the current message to STRING. */
10693
10694 static void
10695 set_message (Lisp_Object string)
10696 {
10697 eassert (STRINGP (string));
10698
10699 message_enable_multibyte = STRING_MULTIBYTE (string);
10700
10701 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10702 message_buf_print = 0;
10703 help_echo_showing_p = 0;
10704
10705 if (STRINGP (Vdebug_on_message)
10706 && STRINGP (string)
10707 && fast_string_match (Vdebug_on_message, string) >= 0)
10708 call_debugger (list2 (Qerror, string));
10709 }
10710
10711
10712 /* Helper function for set_message. First argument is ignored and second
10713 argument has the same meaning as for set_message.
10714 This function is called with the echo area buffer being current. */
10715
10716 static int
10717 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10718 {
10719 eassert (STRINGP (string));
10720
10721 /* Change multibyteness of the echo buffer appropriately. */
10722 if (message_enable_multibyte
10723 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10724 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10725
10726 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10727 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10728 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10729
10730 /* Insert new message at BEG. */
10731 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10732
10733 /* This function takes care of single/multibyte conversion.
10734 We just have to ensure that the echo area buffer has the right
10735 setting of enable_multibyte_characters. */
10736 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10737
10738 return 0;
10739 }
10740
10741
10742 /* Clear messages. CURRENT_P non-zero means clear the current
10743 message. LAST_DISPLAYED_P non-zero means clear the message
10744 last displayed. */
10745
10746 void
10747 clear_message (int current_p, int last_displayed_p)
10748 {
10749 if (current_p)
10750 {
10751 echo_area_buffer[0] = Qnil;
10752 message_cleared_p = 1;
10753 }
10754
10755 if (last_displayed_p)
10756 echo_area_buffer[1] = Qnil;
10757
10758 message_buf_print = 0;
10759 }
10760
10761 /* Clear garbaged frames.
10762
10763 This function is used where the old redisplay called
10764 redraw_garbaged_frames which in turn called redraw_frame which in
10765 turn called clear_frame. The call to clear_frame was a source of
10766 flickering. I believe a clear_frame is not necessary. It should
10767 suffice in the new redisplay to invalidate all current matrices,
10768 and ensure a complete redisplay of all windows. */
10769
10770 static void
10771 clear_garbaged_frames (void)
10772 {
10773 if (frame_garbaged)
10774 {
10775 Lisp_Object tail, frame;
10776 int changed_count = 0;
10777
10778 FOR_EACH_FRAME (tail, frame)
10779 {
10780 struct frame *f = XFRAME (frame);
10781
10782 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10783 {
10784 if (f->resized_p)
10785 {
10786 redraw_frame (f);
10787 f->force_flush_display_p = 1;
10788 }
10789 clear_current_matrices (f);
10790 changed_count++;
10791 f->garbaged = 0;
10792 f->resized_p = 0;
10793 }
10794 }
10795
10796 frame_garbaged = 0;
10797 if (changed_count)
10798 ++windows_or_buffers_changed;
10799 }
10800 }
10801
10802
10803 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10804 is non-zero update selected_frame. Value is non-zero if the
10805 mini-windows height has been changed. */
10806
10807 static int
10808 echo_area_display (int update_frame_p)
10809 {
10810 Lisp_Object mini_window;
10811 struct window *w;
10812 struct frame *f;
10813 int window_height_changed_p = 0;
10814 struct frame *sf = SELECTED_FRAME ();
10815
10816 mini_window = FRAME_MINIBUF_WINDOW (sf);
10817 w = XWINDOW (mini_window);
10818 f = XFRAME (WINDOW_FRAME (w));
10819
10820 /* Don't display if frame is invisible or not yet initialized. */
10821 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10822 return 0;
10823
10824 #ifdef HAVE_WINDOW_SYSTEM
10825 /* When Emacs starts, selected_frame may be the initial terminal
10826 frame. If we let this through, a message would be displayed on
10827 the terminal. */
10828 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10829 return 0;
10830 #endif /* HAVE_WINDOW_SYSTEM */
10831
10832 /* Redraw garbaged frames. */
10833 clear_garbaged_frames ();
10834
10835 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10836 {
10837 echo_area_window = mini_window;
10838 window_height_changed_p = display_echo_area (w);
10839 w->must_be_updated_p = 1;
10840
10841 /* Update the display, unless called from redisplay_internal.
10842 Also don't update the screen during redisplay itself. The
10843 update will happen at the end of redisplay, and an update
10844 here could cause confusion. */
10845 if (update_frame_p && !redisplaying_p)
10846 {
10847 int n = 0;
10848
10849 /* If the display update has been interrupted by pending
10850 input, update mode lines in the frame. Due to the
10851 pending input, it might have been that redisplay hasn't
10852 been called, so that mode lines above the echo area are
10853 garbaged. This looks odd, so we prevent it here. */
10854 if (!display_completed)
10855 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10856
10857 if (window_height_changed_p
10858 /* Don't do this if Emacs is shutting down. Redisplay
10859 needs to run hooks. */
10860 && !NILP (Vrun_hooks))
10861 {
10862 /* Must update other windows. Likewise as in other
10863 cases, don't let this update be interrupted by
10864 pending input. */
10865 ptrdiff_t count = SPECPDL_INDEX ();
10866 specbind (Qredisplay_dont_pause, Qt);
10867 windows_or_buffers_changed = 1;
10868 redisplay_internal ();
10869 unbind_to (count, Qnil);
10870 }
10871 else if (FRAME_WINDOW_P (f) && n == 0)
10872 {
10873 /* Window configuration is the same as before.
10874 Can do with a display update of the echo area,
10875 unless we displayed some mode lines. */
10876 update_single_window (w, 1);
10877 FRAME_RIF (f)->flush_display (f);
10878 }
10879 else
10880 update_frame (f, 1, 1);
10881
10882 /* If cursor is in the echo area, make sure that the next
10883 redisplay displays the minibuffer, so that the cursor will
10884 be replaced with what the minibuffer wants. */
10885 if (cursor_in_echo_area)
10886 ++windows_or_buffers_changed;
10887 }
10888 }
10889 else if (!EQ (mini_window, selected_window))
10890 windows_or_buffers_changed++;
10891
10892 /* Last displayed message is now the current message. */
10893 echo_area_buffer[1] = echo_area_buffer[0];
10894 /* Inform read_char that we're not echoing. */
10895 echo_message_buffer = Qnil;
10896
10897 /* Prevent redisplay optimization in redisplay_internal by resetting
10898 this_line_start_pos. This is done because the mini-buffer now
10899 displays the message instead of its buffer text. */
10900 if (EQ (mini_window, selected_window))
10901 CHARPOS (this_line_start_pos) = 0;
10902
10903 return window_height_changed_p;
10904 }
10905
10906 /* Nonzero if the current window's buffer is shown in more than one
10907 window and was modified since last redisplay. */
10908
10909 static int
10910 buffer_shared_and_changed (void)
10911 {
10912 return (buffer_window_count (current_buffer) > 1
10913 && UNCHANGED_MODIFIED < MODIFF);
10914 }
10915
10916 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10917 is enabled and mark of W's buffer was changed since last W's update. */
10918
10919 static int
10920 window_buffer_changed (struct window *w)
10921 {
10922 struct buffer *b = XBUFFER (w->contents);
10923
10924 eassert (BUFFER_LIVE_P (b));
10925
10926 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10927 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10928 != (w->region_showing != 0)));
10929 }
10930
10931 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10932
10933 static int
10934 mode_line_update_needed (struct window *w)
10935 {
10936 return (w->column_number_displayed != -1
10937 && !(PT == w->last_point && !window_outdated (w))
10938 && (w->column_number_displayed != current_column ()));
10939 }
10940
10941 /* Nonzero if window start of W is frozen and may not be changed during
10942 redisplay. */
10943
10944 static bool
10945 window_frozen_p (struct window *w)
10946 {
10947 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10948 {
10949 Lisp_Object window;
10950
10951 XSETWINDOW (window, w);
10952 if (MINI_WINDOW_P (w))
10953 return 0;
10954 else if (EQ (window, selected_window))
10955 return 0;
10956 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10957 && EQ (window, Vminibuf_scroll_window))
10958 /* This special window can't be frozen too. */
10959 return 0;
10960 else
10961 return 1;
10962 }
10963 return 0;
10964 }
10965
10966 /***********************************************************************
10967 Mode Lines and Frame Titles
10968 ***********************************************************************/
10969
10970 /* A buffer for constructing non-propertized mode-line strings and
10971 frame titles in it; allocated from the heap in init_xdisp and
10972 resized as needed in store_mode_line_noprop_char. */
10973
10974 static char *mode_line_noprop_buf;
10975
10976 /* The buffer's end, and a current output position in it. */
10977
10978 static char *mode_line_noprop_buf_end;
10979 static char *mode_line_noprop_ptr;
10980
10981 #define MODE_LINE_NOPROP_LEN(start) \
10982 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10983
10984 static enum {
10985 MODE_LINE_DISPLAY = 0,
10986 MODE_LINE_TITLE,
10987 MODE_LINE_NOPROP,
10988 MODE_LINE_STRING
10989 } mode_line_target;
10990
10991 /* Alist that caches the results of :propertize.
10992 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10993 static Lisp_Object mode_line_proptrans_alist;
10994
10995 /* List of strings making up the mode-line. */
10996 static Lisp_Object mode_line_string_list;
10997
10998 /* Base face property when building propertized mode line string. */
10999 static Lisp_Object mode_line_string_face;
11000 static Lisp_Object mode_line_string_face_prop;
11001
11002
11003 /* Unwind data for mode line strings */
11004
11005 static Lisp_Object Vmode_line_unwind_vector;
11006
11007 static Lisp_Object
11008 format_mode_line_unwind_data (struct frame *target_frame,
11009 struct buffer *obuf,
11010 Lisp_Object owin,
11011 int save_proptrans)
11012 {
11013 Lisp_Object vector, tmp;
11014
11015 /* Reduce consing by keeping one vector in
11016 Vwith_echo_area_save_vector. */
11017 vector = Vmode_line_unwind_vector;
11018 Vmode_line_unwind_vector = Qnil;
11019
11020 if (NILP (vector))
11021 vector = Fmake_vector (make_number (10), Qnil);
11022
11023 ASET (vector, 0, make_number (mode_line_target));
11024 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11025 ASET (vector, 2, mode_line_string_list);
11026 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11027 ASET (vector, 4, mode_line_string_face);
11028 ASET (vector, 5, mode_line_string_face_prop);
11029
11030 if (obuf)
11031 XSETBUFFER (tmp, obuf);
11032 else
11033 tmp = Qnil;
11034 ASET (vector, 6, tmp);
11035 ASET (vector, 7, owin);
11036 if (target_frame)
11037 {
11038 /* Similarly to `with-selected-window', if the operation selects
11039 a window on another frame, we must restore that frame's
11040 selected window, and (for a tty) the top-frame. */
11041 ASET (vector, 8, target_frame->selected_window);
11042 if (FRAME_TERMCAP_P (target_frame))
11043 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11044 }
11045
11046 return vector;
11047 }
11048
11049 static void
11050 unwind_format_mode_line (Lisp_Object vector)
11051 {
11052 Lisp_Object old_window = AREF (vector, 7);
11053 Lisp_Object target_frame_window = AREF (vector, 8);
11054 Lisp_Object old_top_frame = AREF (vector, 9);
11055
11056 mode_line_target = XINT (AREF (vector, 0));
11057 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11058 mode_line_string_list = AREF (vector, 2);
11059 if (! EQ (AREF (vector, 3), Qt))
11060 mode_line_proptrans_alist = AREF (vector, 3);
11061 mode_line_string_face = AREF (vector, 4);
11062 mode_line_string_face_prop = AREF (vector, 5);
11063
11064 /* Select window before buffer, since it may change the buffer. */
11065 if (!NILP (old_window))
11066 {
11067 /* If the operation that we are unwinding had selected a window
11068 on a different frame, reset its frame-selected-window. For a
11069 text terminal, reset its top-frame if necessary. */
11070 if (!NILP (target_frame_window))
11071 {
11072 Lisp_Object frame
11073 = WINDOW_FRAME (XWINDOW (target_frame_window));
11074
11075 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11076 Fselect_window (target_frame_window, Qt);
11077
11078 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11079 Fselect_frame (old_top_frame, Qt);
11080 }
11081
11082 Fselect_window (old_window, Qt);
11083 }
11084
11085 if (!NILP (AREF (vector, 6)))
11086 {
11087 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11088 ASET (vector, 6, Qnil);
11089 }
11090
11091 Vmode_line_unwind_vector = vector;
11092 }
11093
11094
11095 /* Store a single character C for the frame title in mode_line_noprop_buf.
11096 Re-allocate mode_line_noprop_buf if necessary. */
11097
11098 static void
11099 store_mode_line_noprop_char (char c)
11100 {
11101 /* If output position has reached the end of the allocated buffer,
11102 increase the buffer's size. */
11103 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11104 {
11105 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11106 ptrdiff_t size = len;
11107 mode_line_noprop_buf =
11108 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11109 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11110 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11111 }
11112
11113 *mode_line_noprop_ptr++ = c;
11114 }
11115
11116
11117 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11118 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11119 characters that yield more columns than PRECISION; PRECISION <= 0
11120 means copy the whole string. Pad with spaces until FIELD_WIDTH
11121 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11122 pad. Called from display_mode_element when it is used to build a
11123 frame title. */
11124
11125 static int
11126 store_mode_line_noprop (const char *string, int field_width, int precision)
11127 {
11128 const unsigned char *str = (const unsigned char *) string;
11129 int n = 0;
11130 ptrdiff_t dummy, nbytes;
11131
11132 /* Copy at most PRECISION chars from STR. */
11133 nbytes = strlen (string);
11134 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11135 while (nbytes--)
11136 store_mode_line_noprop_char (*str++);
11137
11138 /* Fill up with spaces until FIELD_WIDTH reached. */
11139 while (field_width > 0
11140 && n < field_width)
11141 {
11142 store_mode_line_noprop_char (' ');
11143 ++n;
11144 }
11145
11146 return n;
11147 }
11148
11149 /***********************************************************************
11150 Frame Titles
11151 ***********************************************************************/
11152
11153 #ifdef HAVE_WINDOW_SYSTEM
11154
11155 /* Set the title of FRAME, if it has changed. The title format is
11156 Vicon_title_format if FRAME is iconified, otherwise it is
11157 frame_title_format. */
11158
11159 static void
11160 x_consider_frame_title (Lisp_Object frame)
11161 {
11162 struct frame *f = XFRAME (frame);
11163
11164 if (FRAME_WINDOW_P (f)
11165 || FRAME_MINIBUF_ONLY_P (f)
11166 || f->explicit_name)
11167 {
11168 /* Do we have more than one visible frame on this X display? */
11169 Lisp_Object tail, other_frame, fmt;
11170 ptrdiff_t title_start;
11171 char *title;
11172 ptrdiff_t len;
11173 struct it it;
11174 ptrdiff_t count = SPECPDL_INDEX ();
11175
11176 FOR_EACH_FRAME (tail, other_frame)
11177 {
11178 struct frame *tf = XFRAME (other_frame);
11179
11180 if (tf != f
11181 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11182 && !FRAME_MINIBUF_ONLY_P (tf)
11183 && !EQ (other_frame, tip_frame)
11184 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11185 break;
11186 }
11187
11188 /* Set global variable indicating that multiple frames exist. */
11189 multiple_frames = CONSP (tail);
11190
11191 /* Switch to the buffer of selected window of the frame. Set up
11192 mode_line_target so that display_mode_element will output into
11193 mode_line_noprop_buf; then display the title. */
11194 record_unwind_protect (unwind_format_mode_line,
11195 format_mode_line_unwind_data
11196 (f, current_buffer, selected_window, 0));
11197
11198 Fselect_window (f->selected_window, Qt);
11199 set_buffer_internal_1
11200 (XBUFFER (XWINDOW (f->selected_window)->contents));
11201 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11202
11203 mode_line_target = MODE_LINE_TITLE;
11204 title_start = MODE_LINE_NOPROP_LEN (0);
11205 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11206 NULL, DEFAULT_FACE_ID);
11207 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11208 len = MODE_LINE_NOPROP_LEN (title_start);
11209 title = mode_line_noprop_buf + title_start;
11210 unbind_to (count, Qnil);
11211
11212 /* Set the title only if it's changed. This avoids consing in
11213 the common case where it hasn't. (If it turns out that we've
11214 already wasted too much time by walking through the list with
11215 display_mode_element, then we might need to optimize at a
11216 higher level than this.) */
11217 if (! STRINGP (f->name)
11218 || SBYTES (f->name) != len
11219 || memcmp (title, SDATA (f->name), len) != 0)
11220 x_implicitly_set_name (f, make_string (title, len), Qnil);
11221 }
11222 }
11223
11224 #endif /* not HAVE_WINDOW_SYSTEM */
11225
11226 \f
11227 /***********************************************************************
11228 Menu Bars
11229 ***********************************************************************/
11230
11231
11232 /* Prepare for redisplay by updating menu-bar item lists when
11233 appropriate. This can call eval. */
11234
11235 void
11236 prepare_menu_bars (void)
11237 {
11238 int all_windows;
11239 struct gcpro gcpro1, gcpro2;
11240 struct frame *f;
11241 Lisp_Object tooltip_frame;
11242
11243 #ifdef HAVE_WINDOW_SYSTEM
11244 tooltip_frame = tip_frame;
11245 #else
11246 tooltip_frame = Qnil;
11247 #endif
11248
11249 /* Update all frame titles based on their buffer names, etc. We do
11250 this before the menu bars so that the buffer-menu will show the
11251 up-to-date frame titles. */
11252 #ifdef HAVE_WINDOW_SYSTEM
11253 if (windows_or_buffers_changed || update_mode_lines)
11254 {
11255 Lisp_Object tail, frame;
11256
11257 FOR_EACH_FRAME (tail, frame)
11258 {
11259 f = XFRAME (frame);
11260 if (!EQ (frame, tooltip_frame)
11261 && (FRAME_ICONIFIED_P (f)
11262 || FRAME_VISIBLE_P (f) == 1
11263 /* Exclude TTY frames that are obscured because they
11264 are not the top frame on their console. This is
11265 because x_consider_frame_title actually switches
11266 to the frame, which for TTY frames means it is
11267 marked as garbaged, and will be completely
11268 redrawn on the next redisplay cycle. This causes
11269 TTY frames to be completely redrawn, when there
11270 are more than one of them, even though nothing
11271 should be changed on display. */
11272 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11273 x_consider_frame_title (frame);
11274 }
11275 }
11276 #endif /* HAVE_WINDOW_SYSTEM */
11277
11278 /* Update the menu bar item lists, if appropriate. This has to be
11279 done before any actual redisplay or generation of display lines. */
11280 all_windows = (update_mode_lines
11281 || buffer_shared_and_changed ()
11282 || windows_or_buffers_changed);
11283 if (all_windows)
11284 {
11285 Lisp_Object tail, frame;
11286 ptrdiff_t count = SPECPDL_INDEX ();
11287 /* 1 means that update_menu_bar has run its hooks
11288 so any further calls to update_menu_bar shouldn't do so again. */
11289 int menu_bar_hooks_run = 0;
11290
11291 record_unwind_save_match_data ();
11292
11293 FOR_EACH_FRAME (tail, frame)
11294 {
11295 f = XFRAME (frame);
11296
11297 /* Ignore tooltip frame. */
11298 if (EQ (frame, tooltip_frame))
11299 continue;
11300
11301 /* If a window on this frame changed size, report that to
11302 the user and clear the size-change flag. */
11303 if (FRAME_WINDOW_SIZES_CHANGED (f))
11304 {
11305 Lisp_Object functions;
11306
11307 /* Clear flag first in case we get an error below. */
11308 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11309 functions = Vwindow_size_change_functions;
11310 GCPRO2 (tail, functions);
11311
11312 while (CONSP (functions))
11313 {
11314 if (!EQ (XCAR (functions), Qt))
11315 call1 (XCAR (functions), frame);
11316 functions = XCDR (functions);
11317 }
11318 UNGCPRO;
11319 }
11320
11321 GCPRO1 (tail);
11322 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11323 #ifdef HAVE_WINDOW_SYSTEM
11324 update_tool_bar (f, 0);
11325 #endif
11326 #ifdef HAVE_NS
11327 if (windows_or_buffers_changed
11328 && FRAME_NS_P (f))
11329 ns_set_doc_edited
11330 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11331 #endif
11332 UNGCPRO;
11333 }
11334
11335 unbind_to (count, Qnil);
11336 }
11337 else
11338 {
11339 struct frame *sf = SELECTED_FRAME ();
11340 update_menu_bar (sf, 1, 0);
11341 #ifdef HAVE_WINDOW_SYSTEM
11342 update_tool_bar (sf, 1);
11343 #endif
11344 }
11345 }
11346
11347
11348 /* Update the menu bar item list for frame F. This has to be done
11349 before we start to fill in any display lines, because it can call
11350 eval.
11351
11352 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11353
11354 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11355 already ran the menu bar hooks for this redisplay, so there
11356 is no need to run them again. The return value is the
11357 updated value of this flag, to pass to the next call. */
11358
11359 static int
11360 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11361 {
11362 Lisp_Object window;
11363 register struct window *w;
11364
11365 /* If called recursively during a menu update, do nothing. This can
11366 happen when, for instance, an activate-menubar-hook causes a
11367 redisplay. */
11368 if (inhibit_menubar_update)
11369 return hooks_run;
11370
11371 window = FRAME_SELECTED_WINDOW (f);
11372 w = XWINDOW (window);
11373
11374 if (FRAME_WINDOW_P (f)
11375 ?
11376 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11377 || defined (HAVE_NS) || defined (USE_GTK)
11378 FRAME_EXTERNAL_MENU_BAR (f)
11379 #else
11380 FRAME_MENU_BAR_LINES (f) > 0
11381 #endif
11382 : FRAME_MENU_BAR_LINES (f) > 0)
11383 {
11384 /* If the user has switched buffers or windows, we need to
11385 recompute to reflect the new bindings. But we'll
11386 recompute when update_mode_lines is set too; that means
11387 that people can use force-mode-line-update to request
11388 that the menu bar be recomputed. The adverse effect on
11389 the rest of the redisplay algorithm is about the same as
11390 windows_or_buffers_changed anyway. */
11391 if (windows_or_buffers_changed
11392 /* This used to test w->update_mode_line, but we believe
11393 there is no need to recompute the menu in that case. */
11394 || update_mode_lines
11395 || window_buffer_changed (w))
11396 {
11397 struct buffer *prev = current_buffer;
11398 ptrdiff_t count = SPECPDL_INDEX ();
11399
11400 specbind (Qinhibit_menubar_update, Qt);
11401
11402 set_buffer_internal_1 (XBUFFER (w->contents));
11403 if (save_match_data)
11404 record_unwind_save_match_data ();
11405 if (NILP (Voverriding_local_map_menu_flag))
11406 {
11407 specbind (Qoverriding_terminal_local_map, Qnil);
11408 specbind (Qoverriding_local_map, Qnil);
11409 }
11410
11411 if (!hooks_run)
11412 {
11413 /* Run the Lucid hook. */
11414 safe_run_hooks (Qactivate_menubar_hook);
11415
11416 /* If it has changed current-menubar from previous value,
11417 really recompute the menu-bar from the value. */
11418 if (! NILP (Vlucid_menu_bar_dirty_flag))
11419 call0 (Qrecompute_lucid_menubar);
11420
11421 safe_run_hooks (Qmenu_bar_update_hook);
11422
11423 hooks_run = 1;
11424 }
11425
11426 XSETFRAME (Vmenu_updating_frame, f);
11427 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11428
11429 /* Redisplay the menu bar in case we changed it. */
11430 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11431 || defined (HAVE_NS) || defined (USE_GTK)
11432 if (FRAME_WINDOW_P (f))
11433 {
11434 #if defined (HAVE_NS)
11435 /* All frames on Mac OS share the same menubar. So only
11436 the selected frame should be allowed to set it. */
11437 if (f == SELECTED_FRAME ())
11438 #endif
11439 set_frame_menubar (f, 0, 0);
11440 }
11441 else
11442 /* On a terminal screen, the menu bar is an ordinary screen
11443 line, and this makes it get updated. */
11444 w->update_mode_line = 1;
11445 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11446 /* In the non-toolkit version, the menu bar is an ordinary screen
11447 line, and this makes it get updated. */
11448 w->update_mode_line = 1;
11449 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11450
11451 unbind_to (count, Qnil);
11452 set_buffer_internal_1 (prev);
11453 }
11454 }
11455
11456 return hooks_run;
11457 }
11458
11459
11460 \f
11461 /***********************************************************************
11462 Output Cursor
11463 ***********************************************************************/
11464
11465 #ifdef HAVE_WINDOW_SYSTEM
11466
11467 /* EXPORT:
11468 Nominal cursor position -- where to draw output.
11469 HPOS and VPOS are window relative glyph matrix coordinates.
11470 X and Y are window relative pixel coordinates. */
11471
11472 struct cursor_pos output_cursor;
11473
11474
11475 /* EXPORT:
11476 Set the global variable output_cursor to CURSOR. All cursor
11477 positions are relative to currently updated window. */
11478
11479 void
11480 set_output_cursor (struct cursor_pos *cursor)
11481 {
11482 output_cursor.hpos = cursor->hpos;
11483 output_cursor.vpos = cursor->vpos;
11484 output_cursor.x = cursor->x;
11485 output_cursor.y = cursor->y;
11486 }
11487
11488
11489 /* EXPORT for RIF:
11490 Set a nominal cursor position.
11491
11492 HPOS and VPOS are column/row positions in a window glyph matrix.
11493 X and Y are window text area relative pixel positions.
11494
11495 This is always done during window update, so the position is the
11496 future output cursor position for currently updated window W.
11497 NOTE: W is used only to check whether this function is called
11498 in a consistent manner via the redisplay interface. */
11499
11500 void
11501 x_cursor_to (struct window *w, int vpos, int hpos, int y, int x)
11502 {
11503 eassert (w);
11504
11505 /* Set the output cursor. */
11506 output_cursor.hpos = hpos;
11507 output_cursor.vpos = vpos;
11508 output_cursor.x = x;
11509 output_cursor.y = y;
11510 }
11511
11512 #endif /* HAVE_WINDOW_SYSTEM */
11513
11514 \f
11515 /***********************************************************************
11516 Tool-bars
11517 ***********************************************************************/
11518
11519 #ifdef HAVE_WINDOW_SYSTEM
11520
11521 /* Where the mouse was last time we reported a mouse event. */
11522
11523 struct frame *last_mouse_frame;
11524
11525 /* Tool-bar item index of the item on which a mouse button was pressed
11526 or -1. */
11527
11528 int last_tool_bar_item;
11529
11530 /* Select `frame' temporarily without running all the code in
11531 do_switch_frame.
11532 FIXME: Maybe do_switch_frame should be trimmed down similarly
11533 when `norecord' is set. */
11534 static void
11535 fast_set_selected_frame (Lisp_Object frame)
11536 {
11537 if (!EQ (selected_frame, frame))
11538 {
11539 selected_frame = frame;
11540 selected_window = XFRAME (frame)->selected_window;
11541 }
11542 }
11543
11544 /* Update the tool-bar item list for frame F. This has to be done
11545 before we start to fill in any display lines. Called from
11546 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11547 and restore it here. */
11548
11549 static void
11550 update_tool_bar (struct frame *f, int save_match_data)
11551 {
11552 #if defined (USE_GTK) || defined (HAVE_NS)
11553 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11554 #else
11555 int do_update = WINDOWP (f->tool_bar_window)
11556 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11557 #endif
11558
11559 if (do_update)
11560 {
11561 Lisp_Object window;
11562 struct window *w;
11563
11564 window = FRAME_SELECTED_WINDOW (f);
11565 w = XWINDOW (window);
11566
11567 /* If the user has switched buffers or windows, we need to
11568 recompute to reflect the new bindings. But we'll
11569 recompute when update_mode_lines is set too; that means
11570 that people can use force-mode-line-update to request
11571 that the menu bar be recomputed. The adverse effect on
11572 the rest of the redisplay algorithm is about the same as
11573 windows_or_buffers_changed anyway. */
11574 if (windows_or_buffers_changed
11575 || w->update_mode_line
11576 || update_mode_lines
11577 || window_buffer_changed (w))
11578 {
11579 struct buffer *prev = current_buffer;
11580 ptrdiff_t count = SPECPDL_INDEX ();
11581 Lisp_Object frame, new_tool_bar;
11582 int new_n_tool_bar;
11583 struct gcpro gcpro1;
11584
11585 /* Set current_buffer to the buffer of the selected
11586 window of the frame, so that we get the right local
11587 keymaps. */
11588 set_buffer_internal_1 (XBUFFER (w->contents));
11589
11590 /* Save match data, if we must. */
11591 if (save_match_data)
11592 record_unwind_save_match_data ();
11593
11594 /* Make sure that we don't accidentally use bogus keymaps. */
11595 if (NILP (Voverriding_local_map_menu_flag))
11596 {
11597 specbind (Qoverriding_terminal_local_map, Qnil);
11598 specbind (Qoverriding_local_map, Qnil);
11599 }
11600
11601 GCPRO1 (new_tool_bar);
11602
11603 /* We must temporarily set the selected frame to this frame
11604 before calling tool_bar_items, because the calculation of
11605 the tool-bar keymap uses the selected frame (see
11606 `tool-bar-make-keymap' in tool-bar.el). */
11607 eassert (EQ (selected_window,
11608 /* Since we only explicitly preserve selected_frame,
11609 check that selected_window would be redundant. */
11610 XFRAME (selected_frame)->selected_window));
11611 record_unwind_protect (fast_set_selected_frame, selected_frame);
11612 XSETFRAME (frame, f);
11613 fast_set_selected_frame (frame);
11614
11615 /* Build desired tool-bar items from keymaps. */
11616 new_tool_bar
11617 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11618 &new_n_tool_bar);
11619
11620 /* Redisplay the tool-bar if we changed it. */
11621 if (new_n_tool_bar != f->n_tool_bar_items
11622 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11623 {
11624 /* Redisplay that happens asynchronously due to an expose event
11625 may access f->tool_bar_items. Make sure we update both
11626 variables within BLOCK_INPUT so no such event interrupts. */
11627 block_input ();
11628 fset_tool_bar_items (f, new_tool_bar);
11629 f->n_tool_bar_items = new_n_tool_bar;
11630 w->update_mode_line = 1;
11631 unblock_input ();
11632 }
11633
11634 UNGCPRO;
11635
11636 unbind_to (count, Qnil);
11637 set_buffer_internal_1 (prev);
11638 }
11639 }
11640 }
11641
11642
11643 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11644 F's desired tool-bar contents. F->tool_bar_items must have
11645 been set up previously by calling prepare_menu_bars. */
11646
11647 static void
11648 build_desired_tool_bar_string (struct frame *f)
11649 {
11650 int i, size, size_needed;
11651 struct gcpro gcpro1, gcpro2, gcpro3;
11652 Lisp_Object image, plist, props;
11653
11654 image = plist = props = Qnil;
11655 GCPRO3 (image, plist, props);
11656
11657 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11658 Otherwise, make a new string. */
11659
11660 /* The size of the string we might be able to reuse. */
11661 size = (STRINGP (f->desired_tool_bar_string)
11662 ? SCHARS (f->desired_tool_bar_string)
11663 : 0);
11664
11665 /* We need one space in the string for each image. */
11666 size_needed = f->n_tool_bar_items;
11667
11668 /* Reuse f->desired_tool_bar_string, if possible. */
11669 if (size < size_needed || NILP (f->desired_tool_bar_string))
11670 fset_desired_tool_bar_string
11671 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11672 else
11673 {
11674 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11675 Fremove_text_properties (make_number (0), make_number (size),
11676 props, f->desired_tool_bar_string);
11677 }
11678
11679 /* Put a `display' property on the string for the images to display,
11680 put a `menu_item' property on tool-bar items with a value that
11681 is the index of the item in F's tool-bar item vector. */
11682 for (i = 0; i < f->n_tool_bar_items; ++i)
11683 {
11684 #define PROP(IDX) \
11685 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11686
11687 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11688 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11689 int hmargin, vmargin, relief, idx, end;
11690
11691 /* If image is a vector, choose the image according to the
11692 button state. */
11693 image = PROP (TOOL_BAR_ITEM_IMAGES);
11694 if (VECTORP (image))
11695 {
11696 if (enabled_p)
11697 idx = (selected_p
11698 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11699 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11700 else
11701 idx = (selected_p
11702 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11703 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11704
11705 eassert (ASIZE (image) >= idx);
11706 image = AREF (image, idx);
11707 }
11708 else
11709 idx = -1;
11710
11711 /* Ignore invalid image specifications. */
11712 if (!valid_image_p (image))
11713 continue;
11714
11715 /* Display the tool-bar button pressed, or depressed. */
11716 plist = Fcopy_sequence (XCDR (image));
11717
11718 /* Compute margin and relief to draw. */
11719 relief = (tool_bar_button_relief >= 0
11720 ? tool_bar_button_relief
11721 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11722 hmargin = vmargin = relief;
11723
11724 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11725 INT_MAX - max (hmargin, vmargin)))
11726 {
11727 hmargin += XFASTINT (Vtool_bar_button_margin);
11728 vmargin += XFASTINT (Vtool_bar_button_margin);
11729 }
11730 else if (CONSP (Vtool_bar_button_margin))
11731 {
11732 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11733 INT_MAX - hmargin))
11734 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11735
11736 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11737 INT_MAX - vmargin))
11738 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11739 }
11740
11741 if (auto_raise_tool_bar_buttons_p)
11742 {
11743 /* Add a `:relief' property to the image spec if the item is
11744 selected. */
11745 if (selected_p)
11746 {
11747 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11748 hmargin -= relief;
11749 vmargin -= relief;
11750 }
11751 }
11752 else
11753 {
11754 /* If image is selected, display it pressed, i.e. with a
11755 negative relief. If it's not selected, display it with a
11756 raised relief. */
11757 plist = Fplist_put (plist, QCrelief,
11758 (selected_p
11759 ? make_number (-relief)
11760 : make_number (relief)));
11761 hmargin -= relief;
11762 vmargin -= relief;
11763 }
11764
11765 /* Put a margin around the image. */
11766 if (hmargin || vmargin)
11767 {
11768 if (hmargin == vmargin)
11769 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11770 else
11771 plist = Fplist_put (plist, QCmargin,
11772 Fcons (make_number (hmargin),
11773 make_number (vmargin)));
11774 }
11775
11776 /* If button is not enabled, and we don't have special images
11777 for the disabled state, make the image appear disabled by
11778 applying an appropriate algorithm to it. */
11779 if (!enabled_p && idx < 0)
11780 plist = Fplist_put (plist, QCconversion, Qdisabled);
11781
11782 /* Put a `display' text property on the string for the image to
11783 display. Put a `menu-item' property on the string that gives
11784 the start of this item's properties in the tool-bar items
11785 vector. */
11786 image = Fcons (Qimage, plist);
11787 props = list4 (Qdisplay, image,
11788 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11789
11790 /* Let the last image hide all remaining spaces in the tool bar
11791 string. The string can be longer than needed when we reuse a
11792 previous string. */
11793 if (i + 1 == f->n_tool_bar_items)
11794 end = SCHARS (f->desired_tool_bar_string);
11795 else
11796 end = i + 1;
11797 Fadd_text_properties (make_number (i), make_number (end),
11798 props, f->desired_tool_bar_string);
11799 #undef PROP
11800 }
11801
11802 UNGCPRO;
11803 }
11804
11805
11806 /* Display one line of the tool-bar of frame IT->f.
11807
11808 HEIGHT specifies the desired height of the tool-bar line.
11809 If the actual height of the glyph row is less than HEIGHT, the
11810 row's height is increased to HEIGHT, and the icons are centered
11811 vertically in the new height.
11812
11813 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11814 count a final empty row in case the tool-bar width exactly matches
11815 the window width.
11816 */
11817
11818 static void
11819 display_tool_bar_line (struct it *it, int height)
11820 {
11821 struct glyph_row *row = it->glyph_row;
11822 int max_x = it->last_visible_x;
11823 struct glyph *last;
11824
11825 prepare_desired_row (row);
11826 row->y = it->current_y;
11827
11828 /* Note that this isn't made use of if the face hasn't a box,
11829 so there's no need to check the face here. */
11830 it->start_of_box_run_p = 1;
11831
11832 while (it->current_x < max_x)
11833 {
11834 int x, n_glyphs_before, i, nglyphs;
11835 struct it it_before;
11836
11837 /* Get the next display element. */
11838 if (!get_next_display_element (it))
11839 {
11840 /* Don't count empty row if we are counting needed tool-bar lines. */
11841 if (height < 0 && !it->hpos)
11842 return;
11843 break;
11844 }
11845
11846 /* Produce glyphs. */
11847 n_glyphs_before = row->used[TEXT_AREA];
11848 it_before = *it;
11849
11850 PRODUCE_GLYPHS (it);
11851
11852 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11853 i = 0;
11854 x = it_before.current_x;
11855 while (i < nglyphs)
11856 {
11857 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11858
11859 if (x + glyph->pixel_width > max_x)
11860 {
11861 /* Glyph doesn't fit on line. Backtrack. */
11862 row->used[TEXT_AREA] = n_glyphs_before;
11863 *it = it_before;
11864 /* If this is the only glyph on this line, it will never fit on the
11865 tool-bar, so skip it. But ensure there is at least one glyph,
11866 so we don't accidentally disable the tool-bar. */
11867 if (n_glyphs_before == 0
11868 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11869 break;
11870 goto out;
11871 }
11872
11873 ++it->hpos;
11874 x += glyph->pixel_width;
11875 ++i;
11876 }
11877
11878 /* Stop at line end. */
11879 if (ITERATOR_AT_END_OF_LINE_P (it))
11880 break;
11881
11882 set_iterator_to_next (it, 1);
11883 }
11884
11885 out:;
11886
11887 row->displays_text_p = row->used[TEXT_AREA] != 0;
11888
11889 /* Use default face for the border below the tool bar.
11890
11891 FIXME: When auto-resize-tool-bars is grow-only, there is
11892 no additional border below the possibly empty tool-bar lines.
11893 So to make the extra empty lines look "normal", we have to
11894 use the tool-bar face for the border too. */
11895 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11896 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11897 it->face_id = DEFAULT_FACE_ID;
11898
11899 extend_face_to_end_of_line (it);
11900 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11901 last->right_box_line_p = 1;
11902 if (last == row->glyphs[TEXT_AREA])
11903 last->left_box_line_p = 1;
11904
11905 /* Make line the desired height and center it vertically. */
11906 if ((height -= it->max_ascent + it->max_descent) > 0)
11907 {
11908 /* Don't add more than one line height. */
11909 height %= FRAME_LINE_HEIGHT (it->f);
11910 it->max_ascent += height / 2;
11911 it->max_descent += (height + 1) / 2;
11912 }
11913
11914 compute_line_metrics (it);
11915
11916 /* If line is empty, make it occupy the rest of the tool-bar. */
11917 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11918 {
11919 row->height = row->phys_height = it->last_visible_y - row->y;
11920 row->visible_height = row->height;
11921 row->ascent = row->phys_ascent = 0;
11922 row->extra_line_spacing = 0;
11923 }
11924
11925 row->full_width_p = 1;
11926 row->continued_p = 0;
11927 row->truncated_on_left_p = 0;
11928 row->truncated_on_right_p = 0;
11929
11930 it->current_x = it->hpos = 0;
11931 it->current_y += row->height;
11932 ++it->vpos;
11933 ++it->glyph_row;
11934 }
11935
11936
11937 /* Max tool-bar height. */
11938
11939 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11940 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11941
11942 /* Value is the number of screen lines needed to make all tool-bar
11943 items of frame F visible. The number of actual rows needed is
11944 returned in *N_ROWS if non-NULL. */
11945
11946 static int
11947 tool_bar_lines_needed (struct frame *f, int *n_rows)
11948 {
11949 struct window *w = XWINDOW (f->tool_bar_window);
11950 struct it it;
11951 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11952 the desired matrix, so use (unused) mode-line row as temporary row to
11953 avoid destroying the first tool-bar row. */
11954 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11955
11956 /* Initialize an iterator for iteration over
11957 F->desired_tool_bar_string in the tool-bar window of frame F. */
11958 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11959 it.first_visible_x = 0;
11960 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11961 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11962 it.paragraph_embedding = L2R;
11963
11964 while (!ITERATOR_AT_END_P (&it))
11965 {
11966 clear_glyph_row (temp_row);
11967 it.glyph_row = temp_row;
11968 display_tool_bar_line (&it, -1);
11969 }
11970 clear_glyph_row (temp_row);
11971
11972 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11973 if (n_rows)
11974 *n_rows = it.vpos > 0 ? it.vpos : -1;
11975
11976 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11977 }
11978
11979
11980 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11981 0, 1, 0,
11982 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11983 If FRAME is nil or omitted, use the selected frame. */)
11984 (Lisp_Object frame)
11985 {
11986 struct frame *f = decode_any_frame (frame);
11987 struct window *w;
11988 int nlines = 0;
11989
11990 if (WINDOWP (f->tool_bar_window)
11991 && (w = XWINDOW (f->tool_bar_window),
11992 WINDOW_TOTAL_LINES (w) > 0))
11993 {
11994 update_tool_bar (f, 1);
11995 if (f->n_tool_bar_items)
11996 {
11997 build_desired_tool_bar_string (f);
11998 nlines = tool_bar_lines_needed (f, NULL);
11999 }
12000 }
12001
12002 return make_number (nlines);
12003 }
12004
12005
12006 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12007 height should be changed. */
12008
12009 static int
12010 redisplay_tool_bar (struct frame *f)
12011 {
12012 struct window *w;
12013 struct it it;
12014 struct glyph_row *row;
12015
12016 #if defined (USE_GTK) || defined (HAVE_NS)
12017 if (FRAME_EXTERNAL_TOOL_BAR (f))
12018 update_frame_tool_bar (f);
12019 return 0;
12020 #endif
12021
12022 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12023 do anything. This means you must start with tool-bar-lines
12024 non-zero to get the auto-sizing effect. Or in other words, you
12025 can turn off tool-bars by specifying tool-bar-lines zero. */
12026 if (!WINDOWP (f->tool_bar_window)
12027 || (w = XWINDOW (f->tool_bar_window),
12028 WINDOW_TOTAL_LINES (w) == 0))
12029 return 0;
12030
12031 /* Set up an iterator for the tool-bar window. */
12032 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12033 it.first_visible_x = 0;
12034 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12035 row = it.glyph_row;
12036
12037 /* Build a string that represents the contents of the tool-bar. */
12038 build_desired_tool_bar_string (f);
12039 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12040 /* FIXME: This should be controlled by a user option. But it
12041 doesn't make sense to have an R2L tool bar if the menu bar cannot
12042 be drawn also R2L, and making the menu bar R2L is tricky due
12043 toolkit-specific code that implements it. If an R2L tool bar is
12044 ever supported, display_tool_bar_line should also be augmented to
12045 call unproduce_glyphs like display_line and display_string
12046 do. */
12047 it.paragraph_embedding = L2R;
12048
12049 if (f->n_tool_bar_rows == 0)
12050 {
12051 int nlines;
12052
12053 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12054 nlines != WINDOW_TOTAL_LINES (w)))
12055 {
12056 Lisp_Object frame;
12057 int old_height = WINDOW_TOTAL_LINES (w);
12058
12059 XSETFRAME (frame, f);
12060 Fmodify_frame_parameters (frame,
12061 list1 (Fcons (Qtool_bar_lines,
12062 make_number (nlines))));
12063 if (WINDOW_TOTAL_LINES (w) != old_height)
12064 {
12065 clear_glyph_matrix (w->desired_matrix);
12066 fonts_changed_p = 1;
12067 return 1;
12068 }
12069 }
12070 }
12071
12072 /* Display as many lines as needed to display all tool-bar items. */
12073
12074 if (f->n_tool_bar_rows > 0)
12075 {
12076 int border, rows, height, extra;
12077
12078 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12079 border = XINT (Vtool_bar_border);
12080 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12081 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12082 else if (EQ (Vtool_bar_border, Qborder_width))
12083 border = f->border_width;
12084 else
12085 border = 0;
12086 if (border < 0)
12087 border = 0;
12088
12089 rows = f->n_tool_bar_rows;
12090 height = max (1, (it.last_visible_y - border) / rows);
12091 extra = it.last_visible_y - border - height * rows;
12092
12093 while (it.current_y < it.last_visible_y)
12094 {
12095 int h = 0;
12096 if (extra > 0 && rows-- > 0)
12097 {
12098 h = (extra + rows - 1) / rows;
12099 extra -= h;
12100 }
12101 display_tool_bar_line (&it, height + h);
12102 }
12103 }
12104 else
12105 {
12106 while (it.current_y < it.last_visible_y)
12107 display_tool_bar_line (&it, 0);
12108 }
12109
12110 /* It doesn't make much sense to try scrolling in the tool-bar
12111 window, so don't do it. */
12112 w->desired_matrix->no_scrolling_p = 1;
12113 w->must_be_updated_p = 1;
12114
12115 if (!NILP (Vauto_resize_tool_bars))
12116 {
12117 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12118 int change_height_p = 0;
12119
12120 /* If we couldn't display everything, change the tool-bar's
12121 height if there is room for more. */
12122 if (IT_STRING_CHARPOS (it) < it.end_charpos
12123 && it.current_y < max_tool_bar_height)
12124 change_height_p = 1;
12125
12126 row = it.glyph_row - 1;
12127
12128 /* If there are blank lines at the end, except for a partially
12129 visible blank line at the end that is smaller than
12130 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12131 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12132 && row->height >= FRAME_LINE_HEIGHT (f))
12133 change_height_p = 1;
12134
12135 /* If row displays tool-bar items, but is partially visible,
12136 change the tool-bar's height. */
12137 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12138 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12139 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12140 change_height_p = 1;
12141
12142 /* Resize windows as needed by changing the `tool-bar-lines'
12143 frame parameter. */
12144 if (change_height_p)
12145 {
12146 Lisp_Object frame;
12147 int old_height = WINDOW_TOTAL_LINES (w);
12148 int nrows;
12149 int nlines = tool_bar_lines_needed (f, &nrows);
12150
12151 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12152 && !f->minimize_tool_bar_window_p)
12153 ? (nlines > old_height)
12154 : (nlines != old_height));
12155 f->minimize_tool_bar_window_p = 0;
12156
12157 if (change_height_p)
12158 {
12159 XSETFRAME (frame, f);
12160 Fmodify_frame_parameters (frame,
12161 list1 (Fcons (Qtool_bar_lines,
12162 make_number (nlines))));
12163 if (WINDOW_TOTAL_LINES (w) != old_height)
12164 {
12165 clear_glyph_matrix (w->desired_matrix);
12166 f->n_tool_bar_rows = nrows;
12167 fonts_changed_p = 1;
12168 return 1;
12169 }
12170 }
12171 }
12172 }
12173
12174 f->minimize_tool_bar_window_p = 0;
12175 return 0;
12176 }
12177
12178
12179 /* Get information about the tool-bar item which is displayed in GLYPH
12180 on frame F. Return in *PROP_IDX the index where tool-bar item
12181 properties start in F->tool_bar_items. Value is zero if
12182 GLYPH doesn't display a tool-bar item. */
12183
12184 static int
12185 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12186 {
12187 Lisp_Object prop;
12188 int success_p;
12189 int charpos;
12190
12191 /* This function can be called asynchronously, which means we must
12192 exclude any possibility that Fget_text_property signals an
12193 error. */
12194 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12195 charpos = max (0, charpos);
12196
12197 /* Get the text property `menu-item' at pos. The value of that
12198 property is the start index of this item's properties in
12199 F->tool_bar_items. */
12200 prop = Fget_text_property (make_number (charpos),
12201 Qmenu_item, f->current_tool_bar_string);
12202 if (INTEGERP (prop))
12203 {
12204 *prop_idx = XINT (prop);
12205 success_p = 1;
12206 }
12207 else
12208 success_p = 0;
12209
12210 return success_p;
12211 }
12212
12213 \f
12214 /* Get information about the tool-bar item at position X/Y on frame F.
12215 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12216 the current matrix of the tool-bar window of F, or NULL if not
12217 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12218 item in F->tool_bar_items. Value is
12219
12220 -1 if X/Y is not on a tool-bar item
12221 0 if X/Y is on the same item that was highlighted before.
12222 1 otherwise. */
12223
12224 static int
12225 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12226 int *hpos, int *vpos, int *prop_idx)
12227 {
12228 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12229 struct window *w = XWINDOW (f->tool_bar_window);
12230 int area;
12231
12232 /* Find the glyph under X/Y. */
12233 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12234 if (*glyph == NULL)
12235 return -1;
12236
12237 /* Get the start of this tool-bar item's properties in
12238 f->tool_bar_items. */
12239 if (!tool_bar_item_info (f, *glyph, prop_idx))
12240 return -1;
12241
12242 /* Is mouse on the highlighted item? */
12243 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12244 && *vpos >= hlinfo->mouse_face_beg_row
12245 && *vpos <= hlinfo->mouse_face_end_row
12246 && (*vpos > hlinfo->mouse_face_beg_row
12247 || *hpos >= hlinfo->mouse_face_beg_col)
12248 && (*vpos < hlinfo->mouse_face_end_row
12249 || *hpos < hlinfo->mouse_face_end_col
12250 || hlinfo->mouse_face_past_end))
12251 return 0;
12252
12253 return 1;
12254 }
12255
12256
12257 /* EXPORT:
12258 Handle mouse button event on the tool-bar of frame F, at
12259 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12260 0 for button release. MODIFIERS is event modifiers for button
12261 release. */
12262
12263 void
12264 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12265 int modifiers)
12266 {
12267 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12268 struct window *w = XWINDOW (f->tool_bar_window);
12269 int hpos, vpos, prop_idx;
12270 struct glyph *glyph;
12271 Lisp_Object enabled_p;
12272 int ts;
12273
12274 /* If not on the highlighted tool-bar item, and mouse-highlight is
12275 non-nil, return. This is so we generate the tool-bar button
12276 click only when the mouse button is released on the same item as
12277 where it was pressed. However, when mouse-highlight is disabled,
12278 generate the click when the button is released regardless of the
12279 highlight, since tool-bar items are not highlighted in that
12280 case. */
12281 frame_to_window_pixel_xy (w, &x, &y);
12282 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12283 if (ts == -1
12284 || (ts != 0 && !NILP (Vmouse_highlight)))
12285 return;
12286
12287 /* When mouse-highlight is off, generate the click for the item
12288 where the button was pressed, disregarding where it was
12289 released. */
12290 if (NILP (Vmouse_highlight) && !down_p)
12291 prop_idx = last_tool_bar_item;
12292
12293 /* If item is disabled, do nothing. */
12294 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12295 if (NILP (enabled_p))
12296 return;
12297
12298 if (down_p)
12299 {
12300 /* Show item in pressed state. */
12301 if (!NILP (Vmouse_highlight))
12302 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12303 last_tool_bar_item = prop_idx;
12304 }
12305 else
12306 {
12307 Lisp_Object key, frame;
12308 struct input_event event;
12309 EVENT_INIT (event);
12310
12311 /* Show item in released state. */
12312 if (!NILP (Vmouse_highlight))
12313 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12314
12315 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12316
12317 XSETFRAME (frame, f);
12318 event.kind = TOOL_BAR_EVENT;
12319 event.frame_or_window = frame;
12320 event.arg = frame;
12321 kbd_buffer_store_event (&event);
12322
12323 event.kind = TOOL_BAR_EVENT;
12324 event.frame_or_window = frame;
12325 event.arg = key;
12326 event.modifiers = modifiers;
12327 kbd_buffer_store_event (&event);
12328 last_tool_bar_item = -1;
12329 }
12330 }
12331
12332
12333 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12334 tool-bar window-relative coordinates X/Y. Called from
12335 note_mouse_highlight. */
12336
12337 static void
12338 note_tool_bar_highlight (struct frame *f, int x, int y)
12339 {
12340 Lisp_Object window = f->tool_bar_window;
12341 struct window *w = XWINDOW (window);
12342 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12343 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12344 int hpos, vpos;
12345 struct glyph *glyph;
12346 struct glyph_row *row;
12347 int i;
12348 Lisp_Object enabled_p;
12349 int prop_idx;
12350 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12351 int mouse_down_p, rc;
12352
12353 /* Function note_mouse_highlight is called with negative X/Y
12354 values when mouse moves outside of the frame. */
12355 if (x <= 0 || y <= 0)
12356 {
12357 clear_mouse_face (hlinfo);
12358 return;
12359 }
12360
12361 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12362 if (rc < 0)
12363 {
12364 /* Not on tool-bar item. */
12365 clear_mouse_face (hlinfo);
12366 return;
12367 }
12368 else if (rc == 0)
12369 /* On same tool-bar item as before. */
12370 goto set_help_echo;
12371
12372 clear_mouse_face (hlinfo);
12373
12374 /* Mouse is down, but on different tool-bar item? */
12375 mouse_down_p = (dpyinfo->grabbed
12376 && f == last_mouse_frame
12377 && FRAME_LIVE_P (f));
12378 if (mouse_down_p
12379 && last_tool_bar_item != prop_idx)
12380 return;
12381
12382 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12383
12384 /* If tool-bar item is not enabled, don't highlight it. */
12385 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12386 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12387 {
12388 /* Compute the x-position of the glyph. In front and past the
12389 image is a space. We include this in the highlighted area. */
12390 row = MATRIX_ROW (w->current_matrix, vpos);
12391 for (i = x = 0; i < hpos; ++i)
12392 x += row->glyphs[TEXT_AREA][i].pixel_width;
12393
12394 /* Record this as the current active region. */
12395 hlinfo->mouse_face_beg_col = hpos;
12396 hlinfo->mouse_face_beg_row = vpos;
12397 hlinfo->mouse_face_beg_x = x;
12398 hlinfo->mouse_face_beg_y = row->y;
12399 hlinfo->mouse_face_past_end = 0;
12400
12401 hlinfo->mouse_face_end_col = hpos + 1;
12402 hlinfo->mouse_face_end_row = vpos;
12403 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12404 hlinfo->mouse_face_end_y = row->y;
12405 hlinfo->mouse_face_window = window;
12406 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12407
12408 /* Display it as active. */
12409 show_mouse_face (hlinfo, draw);
12410 }
12411
12412 set_help_echo:
12413
12414 /* Set help_echo_string to a help string to display for this tool-bar item.
12415 XTread_socket does the rest. */
12416 help_echo_object = help_echo_window = Qnil;
12417 help_echo_pos = -1;
12418 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12419 if (NILP (help_echo_string))
12420 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12421 }
12422
12423 #endif /* HAVE_WINDOW_SYSTEM */
12424
12425
12426 \f
12427 /************************************************************************
12428 Horizontal scrolling
12429 ************************************************************************/
12430
12431 static int hscroll_window_tree (Lisp_Object);
12432 static int hscroll_windows (Lisp_Object);
12433
12434 /* For all leaf windows in the window tree rooted at WINDOW, set their
12435 hscroll value so that PT is (i) visible in the window, and (ii) so
12436 that it is not within a certain margin at the window's left and
12437 right border. Value is non-zero if any window's hscroll has been
12438 changed. */
12439
12440 static int
12441 hscroll_window_tree (Lisp_Object window)
12442 {
12443 int hscrolled_p = 0;
12444 int hscroll_relative_p = FLOATP (Vhscroll_step);
12445 int hscroll_step_abs = 0;
12446 double hscroll_step_rel = 0;
12447
12448 if (hscroll_relative_p)
12449 {
12450 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12451 if (hscroll_step_rel < 0)
12452 {
12453 hscroll_relative_p = 0;
12454 hscroll_step_abs = 0;
12455 }
12456 }
12457 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12458 {
12459 hscroll_step_abs = XINT (Vhscroll_step);
12460 if (hscroll_step_abs < 0)
12461 hscroll_step_abs = 0;
12462 }
12463 else
12464 hscroll_step_abs = 0;
12465
12466 while (WINDOWP (window))
12467 {
12468 struct window *w = XWINDOW (window);
12469
12470 if (WINDOWP (w->contents))
12471 hscrolled_p |= hscroll_window_tree (w->contents);
12472 else if (w->cursor.vpos >= 0)
12473 {
12474 int h_margin;
12475 int text_area_width;
12476 struct glyph_row *current_cursor_row
12477 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12478 struct glyph_row *desired_cursor_row
12479 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12480 struct glyph_row *cursor_row
12481 = (desired_cursor_row->enabled_p
12482 ? desired_cursor_row
12483 : current_cursor_row);
12484 int row_r2l_p = cursor_row->reversed_p;
12485
12486 text_area_width = window_box_width (w, TEXT_AREA);
12487
12488 /* Scroll when cursor is inside this scroll margin. */
12489 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12490
12491 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12492 /* For left-to-right rows, hscroll when cursor is either
12493 (i) inside the right hscroll margin, or (ii) if it is
12494 inside the left margin and the window is already
12495 hscrolled. */
12496 && ((!row_r2l_p
12497 && ((w->hscroll
12498 && w->cursor.x <= h_margin)
12499 || (cursor_row->enabled_p
12500 && cursor_row->truncated_on_right_p
12501 && (w->cursor.x >= text_area_width - h_margin))))
12502 /* For right-to-left rows, the logic is similar,
12503 except that rules for scrolling to left and right
12504 are reversed. E.g., if cursor.x <= h_margin, we
12505 need to hscroll "to the right" unconditionally,
12506 and that will scroll the screen to the left so as
12507 to reveal the next portion of the row. */
12508 || (row_r2l_p
12509 && ((cursor_row->enabled_p
12510 /* FIXME: It is confusing to set the
12511 truncated_on_right_p flag when R2L rows
12512 are actually truncated on the left. */
12513 && cursor_row->truncated_on_right_p
12514 && w->cursor.x <= h_margin)
12515 || (w->hscroll
12516 && (w->cursor.x >= text_area_width - h_margin))))))
12517 {
12518 struct it it;
12519 ptrdiff_t hscroll;
12520 struct buffer *saved_current_buffer;
12521 ptrdiff_t pt;
12522 int wanted_x;
12523
12524 /* Find point in a display of infinite width. */
12525 saved_current_buffer = current_buffer;
12526 current_buffer = XBUFFER (w->contents);
12527
12528 if (w == XWINDOW (selected_window))
12529 pt = PT;
12530 else
12531 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12532
12533 /* Move iterator to pt starting at cursor_row->start in
12534 a line with infinite width. */
12535 init_to_row_start (&it, w, cursor_row);
12536 it.last_visible_x = INFINITY;
12537 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12538 current_buffer = saved_current_buffer;
12539
12540 /* Position cursor in window. */
12541 if (!hscroll_relative_p && hscroll_step_abs == 0)
12542 hscroll = max (0, (it.current_x
12543 - (ITERATOR_AT_END_OF_LINE_P (&it)
12544 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12545 : (text_area_width / 2))))
12546 / FRAME_COLUMN_WIDTH (it.f);
12547 else if ((!row_r2l_p
12548 && w->cursor.x >= text_area_width - h_margin)
12549 || (row_r2l_p && w->cursor.x <= h_margin))
12550 {
12551 if (hscroll_relative_p)
12552 wanted_x = text_area_width * (1 - hscroll_step_rel)
12553 - h_margin;
12554 else
12555 wanted_x = text_area_width
12556 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12557 - h_margin;
12558 hscroll
12559 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12560 }
12561 else
12562 {
12563 if (hscroll_relative_p)
12564 wanted_x = text_area_width * hscroll_step_rel
12565 + h_margin;
12566 else
12567 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12568 + h_margin;
12569 hscroll
12570 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12571 }
12572 hscroll = max (hscroll, w->min_hscroll);
12573
12574 /* Don't prevent redisplay optimizations if hscroll
12575 hasn't changed, as it will unnecessarily slow down
12576 redisplay. */
12577 if (w->hscroll != hscroll)
12578 {
12579 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12580 w->hscroll = hscroll;
12581 hscrolled_p = 1;
12582 }
12583 }
12584 }
12585
12586 window = w->next;
12587 }
12588
12589 /* Value is non-zero if hscroll of any leaf window has been changed. */
12590 return hscrolled_p;
12591 }
12592
12593
12594 /* Set hscroll so that cursor is visible and not inside horizontal
12595 scroll margins for all windows in the tree rooted at WINDOW. See
12596 also hscroll_window_tree above. Value is non-zero if any window's
12597 hscroll has been changed. If it has, desired matrices on the frame
12598 of WINDOW are cleared. */
12599
12600 static int
12601 hscroll_windows (Lisp_Object window)
12602 {
12603 int hscrolled_p = hscroll_window_tree (window);
12604 if (hscrolled_p)
12605 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12606 return hscrolled_p;
12607 }
12608
12609
12610 \f
12611 /************************************************************************
12612 Redisplay
12613 ************************************************************************/
12614
12615 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12616 to a non-zero value. This is sometimes handy to have in a debugger
12617 session. */
12618
12619 #ifdef GLYPH_DEBUG
12620
12621 /* First and last unchanged row for try_window_id. */
12622
12623 static int debug_first_unchanged_at_end_vpos;
12624 static int debug_last_unchanged_at_beg_vpos;
12625
12626 /* Delta vpos and y. */
12627
12628 static int debug_dvpos, debug_dy;
12629
12630 /* Delta in characters and bytes for try_window_id. */
12631
12632 static ptrdiff_t debug_delta, debug_delta_bytes;
12633
12634 /* Values of window_end_pos and window_end_vpos at the end of
12635 try_window_id. */
12636
12637 static ptrdiff_t debug_end_vpos;
12638
12639 /* Append a string to W->desired_matrix->method. FMT is a printf
12640 format string. If trace_redisplay_p is non-zero also printf the
12641 resulting string to stderr. */
12642
12643 static void debug_method_add (struct window *, char const *, ...)
12644 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12645
12646 static void
12647 debug_method_add (struct window *w, char const *fmt, ...)
12648 {
12649 void *ptr = w;
12650 char *method = w->desired_matrix->method;
12651 int len = strlen (method);
12652 int size = sizeof w->desired_matrix->method;
12653 int remaining = size - len - 1;
12654 va_list ap;
12655
12656 if (len && remaining)
12657 {
12658 method[len] = '|';
12659 --remaining, ++len;
12660 }
12661
12662 va_start (ap, fmt);
12663 vsnprintf (method + len, remaining + 1, fmt, ap);
12664 va_end (ap);
12665
12666 if (trace_redisplay_p)
12667 fprintf (stderr, "%p (%s): %s\n",
12668 ptr,
12669 ((BUFFERP (w->contents)
12670 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12671 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12672 : "no buffer"),
12673 method + len);
12674 }
12675
12676 #endif /* GLYPH_DEBUG */
12677
12678
12679 /* Value is non-zero if all changes in window W, which displays
12680 current_buffer, are in the text between START and END. START is a
12681 buffer position, END is given as a distance from Z. Used in
12682 redisplay_internal for display optimization. */
12683
12684 static int
12685 text_outside_line_unchanged_p (struct window *w,
12686 ptrdiff_t start, ptrdiff_t end)
12687 {
12688 int unchanged_p = 1;
12689
12690 /* If text or overlays have changed, see where. */
12691 if (window_outdated (w))
12692 {
12693 /* Gap in the line? */
12694 if (GPT < start || Z - GPT < end)
12695 unchanged_p = 0;
12696
12697 /* Changes start in front of the line, or end after it? */
12698 if (unchanged_p
12699 && (BEG_UNCHANGED < start - 1
12700 || END_UNCHANGED < end))
12701 unchanged_p = 0;
12702
12703 /* If selective display, can't optimize if changes start at the
12704 beginning of the line. */
12705 if (unchanged_p
12706 && INTEGERP (BVAR (current_buffer, selective_display))
12707 && XINT (BVAR (current_buffer, selective_display)) > 0
12708 && (BEG_UNCHANGED < start || GPT <= start))
12709 unchanged_p = 0;
12710
12711 /* If there are overlays at the start or end of the line, these
12712 may have overlay strings with newlines in them. A change at
12713 START, for instance, may actually concern the display of such
12714 overlay strings as well, and they are displayed on different
12715 lines. So, quickly rule out this case. (For the future, it
12716 might be desirable to implement something more telling than
12717 just BEG/END_UNCHANGED.) */
12718 if (unchanged_p)
12719 {
12720 if (BEG + BEG_UNCHANGED == start
12721 && overlay_touches_p (start))
12722 unchanged_p = 0;
12723 if (END_UNCHANGED == end
12724 && overlay_touches_p (Z - end))
12725 unchanged_p = 0;
12726 }
12727
12728 /* Under bidi reordering, adding or deleting a character in the
12729 beginning of a paragraph, before the first strong directional
12730 character, can change the base direction of the paragraph (unless
12731 the buffer specifies a fixed paragraph direction), which will
12732 require to redisplay the whole paragraph. It might be worthwhile
12733 to find the paragraph limits and widen the range of redisplayed
12734 lines to that, but for now just give up this optimization. */
12735 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12736 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12737 unchanged_p = 0;
12738 }
12739
12740 return unchanged_p;
12741 }
12742
12743
12744 /* Do a frame update, taking possible shortcuts into account. This is
12745 the main external entry point for redisplay.
12746
12747 If the last redisplay displayed an echo area message and that message
12748 is no longer requested, we clear the echo area or bring back the
12749 mini-buffer if that is in use. */
12750
12751 void
12752 redisplay (void)
12753 {
12754 redisplay_internal ();
12755 }
12756
12757
12758 static Lisp_Object
12759 overlay_arrow_string_or_property (Lisp_Object var)
12760 {
12761 Lisp_Object val;
12762
12763 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12764 return val;
12765
12766 return Voverlay_arrow_string;
12767 }
12768
12769 /* Return 1 if there are any overlay-arrows in current_buffer. */
12770 static int
12771 overlay_arrow_in_current_buffer_p (void)
12772 {
12773 Lisp_Object vlist;
12774
12775 for (vlist = Voverlay_arrow_variable_list;
12776 CONSP (vlist);
12777 vlist = XCDR (vlist))
12778 {
12779 Lisp_Object var = XCAR (vlist);
12780 Lisp_Object val;
12781
12782 if (!SYMBOLP (var))
12783 continue;
12784 val = find_symbol_value (var);
12785 if (MARKERP (val)
12786 && current_buffer == XMARKER (val)->buffer)
12787 return 1;
12788 }
12789 return 0;
12790 }
12791
12792
12793 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12794 has changed. */
12795
12796 static int
12797 overlay_arrows_changed_p (void)
12798 {
12799 Lisp_Object vlist;
12800
12801 for (vlist = Voverlay_arrow_variable_list;
12802 CONSP (vlist);
12803 vlist = XCDR (vlist))
12804 {
12805 Lisp_Object var = XCAR (vlist);
12806 Lisp_Object val, pstr;
12807
12808 if (!SYMBOLP (var))
12809 continue;
12810 val = find_symbol_value (var);
12811 if (!MARKERP (val))
12812 continue;
12813 if (! EQ (COERCE_MARKER (val),
12814 Fget (var, Qlast_arrow_position))
12815 || ! (pstr = overlay_arrow_string_or_property (var),
12816 EQ (pstr, Fget (var, Qlast_arrow_string))))
12817 return 1;
12818 }
12819 return 0;
12820 }
12821
12822 /* Mark overlay arrows to be updated on next redisplay. */
12823
12824 static void
12825 update_overlay_arrows (int up_to_date)
12826 {
12827 Lisp_Object vlist;
12828
12829 for (vlist = Voverlay_arrow_variable_list;
12830 CONSP (vlist);
12831 vlist = XCDR (vlist))
12832 {
12833 Lisp_Object var = XCAR (vlist);
12834
12835 if (!SYMBOLP (var))
12836 continue;
12837
12838 if (up_to_date > 0)
12839 {
12840 Lisp_Object val = find_symbol_value (var);
12841 Fput (var, Qlast_arrow_position,
12842 COERCE_MARKER (val));
12843 Fput (var, Qlast_arrow_string,
12844 overlay_arrow_string_or_property (var));
12845 }
12846 else if (up_to_date < 0
12847 || !NILP (Fget (var, Qlast_arrow_position)))
12848 {
12849 Fput (var, Qlast_arrow_position, Qt);
12850 Fput (var, Qlast_arrow_string, Qt);
12851 }
12852 }
12853 }
12854
12855
12856 /* Return overlay arrow string to display at row.
12857 Return integer (bitmap number) for arrow bitmap in left fringe.
12858 Return nil if no overlay arrow. */
12859
12860 static Lisp_Object
12861 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12862 {
12863 Lisp_Object vlist;
12864
12865 for (vlist = Voverlay_arrow_variable_list;
12866 CONSP (vlist);
12867 vlist = XCDR (vlist))
12868 {
12869 Lisp_Object var = XCAR (vlist);
12870 Lisp_Object val;
12871
12872 if (!SYMBOLP (var))
12873 continue;
12874
12875 val = find_symbol_value (var);
12876
12877 if (MARKERP (val)
12878 && current_buffer == XMARKER (val)->buffer
12879 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12880 {
12881 if (FRAME_WINDOW_P (it->f)
12882 /* FIXME: if ROW->reversed_p is set, this should test
12883 the right fringe, not the left one. */
12884 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12885 {
12886 #ifdef HAVE_WINDOW_SYSTEM
12887 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12888 {
12889 int fringe_bitmap;
12890 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12891 return make_number (fringe_bitmap);
12892 }
12893 #endif
12894 return make_number (-1); /* Use default arrow bitmap. */
12895 }
12896 return overlay_arrow_string_or_property (var);
12897 }
12898 }
12899
12900 return Qnil;
12901 }
12902
12903 /* Return 1 if point moved out of or into a composition. Otherwise
12904 return 0. PREV_BUF and PREV_PT are the last point buffer and
12905 position. BUF and PT are the current point buffer and position. */
12906
12907 static int
12908 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12909 struct buffer *buf, ptrdiff_t pt)
12910 {
12911 ptrdiff_t start, end;
12912 Lisp_Object prop;
12913 Lisp_Object buffer;
12914
12915 XSETBUFFER (buffer, buf);
12916 /* Check a composition at the last point if point moved within the
12917 same buffer. */
12918 if (prev_buf == buf)
12919 {
12920 if (prev_pt == pt)
12921 /* Point didn't move. */
12922 return 0;
12923
12924 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12925 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12926 && composition_valid_p (start, end, prop)
12927 && start < prev_pt && end > prev_pt)
12928 /* The last point was within the composition. Return 1 iff
12929 point moved out of the composition. */
12930 return (pt <= start || pt >= end);
12931 }
12932
12933 /* Check a composition at the current point. */
12934 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12935 && find_composition (pt, -1, &start, &end, &prop, buffer)
12936 && composition_valid_p (start, end, prop)
12937 && start < pt && end > pt);
12938 }
12939
12940 /* Reconsider the clip changes of buffer which is displayed in W. */
12941
12942 static void
12943 reconsider_clip_changes (struct window *w)
12944 {
12945 struct buffer *b = XBUFFER (w->contents);
12946
12947 if (b->clip_changed
12948 && w->window_end_valid
12949 && w->current_matrix->buffer == b
12950 && w->current_matrix->zv == BUF_ZV (b)
12951 && w->current_matrix->begv == BUF_BEGV (b))
12952 b->clip_changed = 0;
12953
12954 /* If display wasn't paused, and W is not a tool bar window, see if
12955 point has been moved into or out of a composition. In that case,
12956 we set b->clip_changed to 1 to force updating the screen. If
12957 b->clip_changed has already been set to 1, we can skip this
12958 check. */
12959 if (!b->clip_changed && w->window_end_valid)
12960 {
12961 ptrdiff_t pt = (w == XWINDOW (selected_window)
12962 ? PT : marker_position (w->pointm));
12963
12964 if ((w->current_matrix->buffer != b || pt != w->last_point)
12965 && check_point_in_composition (w->current_matrix->buffer,
12966 w->last_point, b, pt))
12967 b->clip_changed = 1;
12968 }
12969 }
12970
12971 #define STOP_POLLING \
12972 do { if (! polling_stopped_here) stop_polling (); \
12973 polling_stopped_here = 1; } while (0)
12974
12975 #define RESUME_POLLING \
12976 do { if (polling_stopped_here) start_polling (); \
12977 polling_stopped_here = 0; } while (0)
12978
12979
12980 /* Perhaps in the future avoid recentering windows if it
12981 is not necessary; currently that causes some problems. */
12982
12983 static void
12984 redisplay_internal (void)
12985 {
12986 struct window *w = XWINDOW (selected_window);
12987 struct window *sw;
12988 struct frame *fr;
12989 int pending;
12990 bool must_finish = 0, match_p;
12991 struct text_pos tlbufpos, tlendpos;
12992 int number_of_visible_frames;
12993 ptrdiff_t count;
12994 struct frame *sf;
12995 int polling_stopped_here = 0;
12996 Lisp_Object tail, frame;
12997
12998 /* Non-zero means redisplay has to consider all windows on all
12999 frames. Zero means, only selected_window is considered. */
13000 int consider_all_windows_p;
13001
13002 /* Non-zero means redisplay has to redisplay the miniwindow. */
13003 int update_miniwindow_p = 0;
13004
13005 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13006
13007 /* No redisplay if running in batch mode or frame is not yet fully
13008 initialized, or redisplay is explicitly turned off by setting
13009 Vinhibit_redisplay. */
13010 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13011 || !NILP (Vinhibit_redisplay))
13012 return;
13013
13014 /* Don't examine these until after testing Vinhibit_redisplay.
13015 When Emacs is shutting down, perhaps because its connection to
13016 X has dropped, we should not look at them at all. */
13017 fr = XFRAME (w->frame);
13018 sf = SELECTED_FRAME ();
13019
13020 if (!fr->glyphs_initialized_p)
13021 return;
13022
13023 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13024 if (popup_activated ())
13025 return;
13026 #endif
13027
13028 /* I don't think this happens but let's be paranoid. */
13029 if (redisplaying_p)
13030 return;
13031
13032 /* Record a function that clears redisplaying_p
13033 when we leave this function. */
13034 count = SPECPDL_INDEX ();
13035 record_unwind_protect_void (unwind_redisplay);
13036 redisplaying_p = 1;
13037 specbind (Qinhibit_free_realized_faces, Qnil);
13038
13039 /* Record this function, so it appears on the profiler's backtraces. */
13040 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13041
13042 FOR_EACH_FRAME (tail, frame)
13043 XFRAME (frame)->already_hscrolled_p = 0;
13044
13045 retry:
13046 /* Remember the currently selected window. */
13047 sw = w;
13048
13049 pending = 0;
13050 last_escape_glyph_frame = NULL;
13051 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13052 last_glyphless_glyph_frame = NULL;
13053 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13054
13055 /* If new fonts have been loaded that make a glyph matrix adjustment
13056 necessary, do it. */
13057 if (fonts_changed_p)
13058 {
13059 adjust_glyphs (NULL);
13060 ++windows_or_buffers_changed;
13061 fonts_changed_p = 0;
13062 }
13063
13064 /* If face_change_count is non-zero, init_iterator will free all
13065 realized faces, which includes the faces referenced from current
13066 matrices. So, we can't reuse current matrices in this case. */
13067 if (face_change_count)
13068 ++windows_or_buffers_changed;
13069
13070 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13071 && FRAME_TTY (sf)->previous_frame != sf)
13072 {
13073 /* Since frames on a single ASCII terminal share the same
13074 display area, displaying a different frame means redisplay
13075 the whole thing. */
13076 windows_or_buffers_changed++;
13077 SET_FRAME_GARBAGED (sf);
13078 #ifndef DOS_NT
13079 set_tty_color_mode (FRAME_TTY (sf), sf);
13080 #endif
13081 FRAME_TTY (sf)->previous_frame = sf;
13082 }
13083
13084 /* Set the visible flags for all frames. Do this before checking for
13085 resized or garbaged frames; they want to know if their frames are
13086 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13087 number_of_visible_frames = 0;
13088
13089 FOR_EACH_FRAME (tail, frame)
13090 {
13091 struct frame *f = XFRAME (frame);
13092
13093 if (FRAME_VISIBLE_P (f))
13094 ++number_of_visible_frames;
13095 clear_desired_matrices (f);
13096 }
13097
13098 /* Notice any pending interrupt request to change frame size. */
13099 do_pending_window_change (1);
13100
13101 /* do_pending_window_change could change the selected_window due to
13102 frame resizing which makes the selected window too small. */
13103 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13104 sw = w;
13105
13106 /* Clear frames marked as garbaged. */
13107 clear_garbaged_frames ();
13108
13109 /* Build menubar and tool-bar items. */
13110 if (NILP (Vmemory_full))
13111 prepare_menu_bars ();
13112
13113 if (windows_or_buffers_changed)
13114 update_mode_lines++;
13115
13116 reconsider_clip_changes (w);
13117
13118 /* In most cases selected window displays current buffer. */
13119 match_p = XBUFFER (w->contents) == current_buffer;
13120 if (match_p)
13121 {
13122 ptrdiff_t count1;
13123
13124 /* Detect case that we need to write or remove a star in the mode line. */
13125 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13126 {
13127 w->update_mode_line = 1;
13128 if (buffer_shared_and_changed ())
13129 update_mode_lines++;
13130 }
13131
13132 /* Avoid invocation of point motion hooks by `current_column' below. */
13133 count1 = SPECPDL_INDEX ();
13134 specbind (Qinhibit_point_motion_hooks, Qt);
13135
13136 if (mode_line_update_needed (w))
13137 w->update_mode_line = 1;
13138
13139 unbind_to (count1, Qnil);
13140 }
13141
13142 consider_all_windows_p = (update_mode_lines
13143 || buffer_shared_and_changed ()
13144 || cursor_type_changed);
13145
13146 /* If specs for an arrow have changed, do thorough redisplay
13147 to ensure we remove any arrow that should no longer exist. */
13148 if (overlay_arrows_changed_p ())
13149 consider_all_windows_p = windows_or_buffers_changed = 1;
13150
13151 /* Normally the message* functions will have already displayed and
13152 updated the echo area, but the frame may have been trashed, or
13153 the update may have been preempted, so display the echo area
13154 again here. Checking message_cleared_p captures the case that
13155 the echo area should be cleared. */
13156 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13157 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13158 || (message_cleared_p
13159 && minibuf_level == 0
13160 /* If the mini-window is currently selected, this means the
13161 echo-area doesn't show through. */
13162 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13163 {
13164 int window_height_changed_p = echo_area_display (0);
13165
13166 if (message_cleared_p)
13167 update_miniwindow_p = 1;
13168
13169 must_finish = 1;
13170
13171 /* If we don't display the current message, don't clear the
13172 message_cleared_p flag, because, if we did, we wouldn't clear
13173 the echo area in the next redisplay which doesn't preserve
13174 the echo area. */
13175 if (!display_last_displayed_message_p)
13176 message_cleared_p = 0;
13177
13178 if (fonts_changed_p)
13179 goto retry;
13180 else if (window_height_changed_p)
13181 {
13182 consider_all_windows_p = 1;
13183 ++update_mode_lines;
13184 ++windows_or_buffers_changed;
13185
13186 /* If window configuration was changed, frames may have been
13187 marked garbaged. Clear them or we will experience
13188 surprises wrt scrolling. */
13189 clear_garbaged_frames ();
13190 }
13191 }
13192 else if (EQ (selected_window, minibuf_window)
13193 && (current_buffer->clip_changed || window_outdated (w))
13194 && resize_mini_window (w, 0))
13195 {
13196 /* Resized active mini-window to fit the size of what it is
13197 showing if its contents might have changed. */
13198 must_finish = 1;
13199 /* FIXME: this causes all frames to be updated, which seems unnecessary
13200 since only the current frame needs to be considered. This function
13201 needs to be rewritten with two variables, consider_all_windows and
13202 consider_all_frames. */
13203 consider_all_windows_p = 1;
13204 ++windows_or_buffers_changed;
13205 ++update_mode_lines;
13206
13207 /* If window configuration was changed, frames may have been
13208 marked garbaged. Clear them or we will experience
13209 surprises wrt scrolling. */
13210 clear_garbaged_frames ();
13211 }
13212
13213 /* If showing the region, and mark has changed, we must redisplay
13214 the whole window. The assignment to this_line_start_pos prevents
13215 the optimization directly below this if-statement. */
13216 if (((!NILP (Vtransient_mark_mode)
13217 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13218 != (w->region_showing > 0))
13219 || (w->region_showing
13220 && w->region_showing
13221 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13222 CHARPOS (this_line_start_pos) = 0;
13223
13224 /* Optimize the case that only the line containing the cursor in the
13225 selected window has changed. Variables starting with this_ are
13226 set in display_line and record information about the line
13227 containing the cursor. */
13228 tlbufpos = this_line_start_pos;
13229 tlendpos = this_line_end_pos;
13230 if (!consider_all_windows_p
13231 && CHARPOS (tlbufpos) > 0
13232 && !w->update_mode_line
13233 && !current_buffer->clip_changed
13234 && !current_buffer->prevent_redisplay_optimizations_p
13235 && FRAME_VISIBLE_P (XFRAME (w->frame))
13236 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13237 /* Make sure recorded data applies to current buffer, etc. */
13238 && this_line_buffer == current_buffer
13239 && match_p
13240 && !w->force_start
13241 && !w->optional_new_start
13242 /* Point must be on the line that we have info recorded about. */
13243 && PT >= CHARPOS (tlbufpos)
13244 && PT <= Z - CHARPOS (tlendpos)
13245 /* All text outside that line, including its final newline,
13246 must be unchanged. */
13247 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13248 CHARPOS (tlendpos)))
13249 {
13250 if (CHARPOS (tlbufpos) > BEGV
13251 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13252 && (CHARPOS (tlbufpos) == ZV
13253 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13254 /* Former continuation line has disappeared by becoming empty. */
13255 goto cancel;
13256 else if (window_outdated (w) || MINI_WINDOW_P (w))
13257 {
13258 /* We have to handle the case of continuation around a
13259 wide-column character (see the comment in indent.c around
13260 line 1340).
13261
13262 For instance, in the following case:
13263
13264 -------- Insert --------
13265 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13266 J_I_ ==> J_I_ `^^' are cursors.
13267 ^^ ^^
13268 -------- --------
13269
13270 As we have to redraw the line above, we cannot use this
13271 optimization. */
13272
13273 struct it it;
13274 int line_height_before = this_line_pixel_height;
13275
13276 /* Note that start_display will handle the case that the
13277 line starting at tlbufpos is a continuation line. */
13278 start_display (&it, w, tlbufpos);
13279
13280 /* Implementation note: It this still necessary? */
13281 if (it.current_x != this_line_start_x)
13282 goto cancel;
13283
13284 TRACE ((stderr, "trying display optimization 1\n"));
13285 w->cursor.vpos = -1;
13286 overlay_arrow_seen = 0;
13287 it.vpos = this_line_vpos;
13288 it.current_y = this_line_y;
13289 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13290 display_line (&it);
13291
13292 /* If line contains point, is not continued,
13293 and ends at same distance from eob as before, we win. */
13294 if (w->cursor.vpos >= 0
13295 /* Line is not continued, otherwise this_line_start_pos
13296 would have been set to 0 in display_line. */
13297 && CHARPOS (this_line_start_pos)
13298 /* Line ends as before. */
13299 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13300 /* Line has same height as before. Otherwise other lines
13301 would have to be shifted up or down. */
13302 && this_line_pixel_height == line_height_before)
13303 {
13304 /* If this is not the window's last line, we must adjust
13305 the charstarts of the lines below. */
13306 if (it.current_y < it.last_visible_y)
13307 {
13308 struct glyph_row *row
13309 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13310 ptrdiff_t delta, delta_bytes;
13311
13312 /* We used to distinguish between two cases here,
13313 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13314 when the line ends in a newline or the end of the
13315 buffer's accessible portion. But both cases did
13316 the same, so they were collapsed. */
13317 delta = (Z
13318 - CHARPOS (tlendpos)
13319 - MATRIX_ROW_START_CHARPOS (row));
13320 delta_bytes = (Z_BYTE
13321 - BYTEPOS (tlendpos)
13322 - MATRIX_ROW_START_BYTEPOS (row));
13323
13324 increment_matrix_positions (w->current_matrix,
13325 this_line_vpos + 1,
13326 w->current_matrix->nrows,
13327 delta, delta_bytes);
13328 }
13329
13330 /* If this row displays text now but previously didn't,
13331 or vice versa, w->window_end_vpos may have to be
13332 adjusted. */
13333 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13334 {
13335 if (w->window_end_vpos < this_line_vpos)
13336 w->window_end_vpos = this_line_vpos;
13337 }
13338 else if (w->window_end_vpos == this_line_vpos
13339 && this_line_vpos > 0)
13340 w->window_end_vpos = this_line_vpos - 1;
13341 w->window_end_valid = 0;
13342
13343 /* Update hint: No need to try to scroll in update_window. */
13344 w->desired_matrix->no_scrolling_p = 1;
13345
13346 #ifdef GLYPH_DEBUG
13347 *w->desired_matrix->method = 0;
13348 debug_method_add (w, "optimization 1");
13349 #endif
13350 #if HAVE_XWIDGETS
13351 //debug optimization movement issue
13352 //w->desired_matrix->no_scrolling_p = 1;
13353 //*w->desired_matrix->method = 0;
13354 //debug_method_add (w, "optimization 1");
13355 #endif
13356
13357 #ifdef HAVE_WINDOW_SYSTEM
13358 update_window_fringes (w, 0);
13359 #endif
13360 goto update;
13361 }
13362 else
13363 goto cancel;
13364 }
13365 else if (/* Cursor position hasn't changed. */
13366 PT == w->last_point
13367 /* Make sure the cursor was last displayed
13368 in this window. Otherwise we have to reposition it. */
13369 && 0 <= w->cursor.vpos
13370 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13371 {
13372 if (!must_finish)
13373 {
13374 do_pending_window_change (1);
13375 /* If selected_window changed, redisplay again. */
13376 if (WINDOWP (selected_window)
13377 && (w = XWINDOW (selected_window)) != sw)
13378 goto retry;
13379
13380 /* We used to always goto end_of_redisplay here, but this
13381 isn't enough if we have a blinking cursor. */
13382 if (w->cursor_off_p == w->last_cursor_off_p)
13383 goto end_of_redisplay;
13384 }
13385 goto update;
13386 }
13387 /* If highlighting the region, or if the cursor is in the echo area,
13388 then we can't just move the cursor. */
13389 else if (! (!NILP (Vtransient_mark_mode)
13390 && !NILP (BVAR (current_buffer, mark_active)))
13391 && (EQ (selected_window,
13392 BVAR (current_buffer, last_selected_window))
13393 || highlight_nonselected_windows)
13394 && !w->region_showing
13395 && NILP (Vshow_trailing_whitespace)
13396 && !cursor_in_echo_area)
13397 {
13398 struct it it;
13399 struct glyph_row *row;
13400
13401 /* Skip from tlbufpos to PT and see where it is. Note that
13402 PT may be in invisible text. If so, we will end at the
13403 next visible position. */
13404 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13405 NULL, DEFAULT_FACE_ID);
13406 it.current_x = this_line_start_x;
13407 it.current_y = this_line_y;
13408 it.vpos = this_line_vpos;
13409
13410 /* The call to move_it_to stops in front of PT, but
13411 moves over before-strings. */
13412 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13413
13414 if (it.vpos == this_line_vpos
13415 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13416 row->enabled_p))
13417 {
13418 eassert (this_line_vpos == it.vpos);
13419 eassert (this_line_y == it.current_y);
13420 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13421 #ifdef GLYPH_DEBUG
13422 *w->desired_matrix->method = 0;
13423 debug_method_add (w, "optimization 3");
13424 #endif
13425 goto update;
13426 }
13427 else
13428 goto cancel;
13429 }
13430
13431 cancel:
13432 /* Text changed drastically or point moved off of line. */
13433 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13434 }
13435
13436 CHARPOS (this_line_start_pos) = 0;
13437 consider_all_windows_p |= buffer_shared_and_changed ();
13438 ++clear_face_cache_count;
13439 #ifdef HAVE_WINDOW_SYSTEM
13440 ++clear_image_cache_count;
13441 #endif
13442
13443 /* Build desired matrices, and update the display. If
13444 consider_all_windows_p is non-zero, do it for all windows on all
13445 frames. Otherwise do it for selected_window, only. */
13446
13447 if (consider_all_windows_p)
13448 {
13449 FOR_EACH_FRAME (tail, frame)
13450 XFRAME (frame)->updated_p = 0;
13451
13452 FOR_EACH_FRAME (tail, frame)
13453 {
13454 struct frame *f = XFRAME (frame);
13455
13456 /* We don't have to do anything for unselected terminal
13457 frames. */
13458 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13459 && !EQ (FRAME_TTY (f)->top_frame, frame))
13460 continue;
13461
13462 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13463 {
13464 /* Mark all the scroll bars to be removed; we'll redeem
13465 the ones we want when we redisplay their windows. */
13466 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13467 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13468
13469 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13470 redisplay_windows (FRAME_ROOT_WINDOW (f));
13471
13472 /* The X error handler may have deleted that frame. */
13473 if (!FRAME_LIVE_P (f))
13474 continue;
13475
13476 /* Any scroll bars which redisplay_windows should have
13477 nuked should now go away. */
13478 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13479 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13480
13481 /* If fonts changed, display again. */
13482 /* ??? rms: I suspect it is a mistake to jump all the way
13483 back to retry here. It should just retry this frame. */
13484 if (fonts_changed_p)
13485 goto retry;
13486
13487 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13488 {
13489 /* See if we have to hscroll. */
13490 if (!f->already_hscrolled_p)
13491 {
13492 f->already_hscrolled_p = 1;
13493 if (hscroll_windows (f->root_window))
13494 goto retry;
13495 }
13496
13497 /* Prevent various kinds of signals during display
13498 update. stdio is not robust about handling
13499 signals, which can cause an apparent I/O
13500 error. */
13501 if (interrupt_input)
13502 unrequest_sigio ();
13503 STOP_POLLING;
13504
13505 /* Update the display. */
13506 set_window_update_flags (XWINDOW (f->root_window), 1);
13507 pending |= update_frame (f, 0, 0);
13508 f->updated_p = 1;
13509 }
13510 }
13511 }
13512
13513 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13514
13515 if (!pending)
13516 {
13517 /* Do the mark_window_display_accurate after all windows have
13518 been redisplayed because this call resets flags in buffers
13519 which are needed for proper redisplay. */
13520 FOR_EACH_FRAME (tail, frame)
13521 {
13522 struct frame *f = XFRAME (frame);
13523 if (f->updated_p)
13524 {
13525 mark_window_display_accurate (f->root_window, 1);
13526 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13527 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13528 }
13529 }
13530 }
13531 }
13532 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13533 {
13534 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13535 struct frame *mini_frame;
13536
13537 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13538 /* Use list_of_error, not Qerror, so that
13539 we catch only errors and don't run the debugger. */
13540 internal_condition_case_1 (redisplay_window_1, selected_window,
13541 list_of_error,
13542 redisplay_window_error);
13543 if (update_miniwindow_p)
13544 internal_condition_case_1 (redisplay_window_1, mini_window,
13545 list_of_error,
13546 redisplay_window_error);
13547
13548 /* Compare desired and current matrices, perform output. */
13549
13550 update:
13551 /* If fonts changed, display again. */
13552 if (fonts_changed_p)
13553 goto retry;
13554
13555 /* Prevent various kinds of signals during display update.
13556 stdio is not robust about handling signals,
13557 which can cause an apparent I/O error. */
13558 if (interrupt_input)
13559 unrequest_sigio ();
13560 STOP_POLLING;
13561
13562 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13563 {
13564 if (hscroll_windows (selected_window))
13565 goto retry;
13566
13567 XWINDOW (selected_window)->must_be_updated_p = 1;
13568 pending = update_frame (sf, 0, 0);
13569 }
13570
13571 /* We may have called echo_area_display at the top of this
13572 function. If the echo area is on another frame, that may
13573 have put text on a frame other than the selected one, so the
13574 above call to update_frame would not have caught it. Catch
13575 it here. */
13576 mini_window = FRAME_MINIBUF_WINDOW (sf);
13577 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13578
13579 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13580 {
13581 XWINDOW (mini_window)->must_be_updated_p = 1;
13582 pending |= update_frame (mini_frame, 0, 0);
13583 if (!pending && hscroll_windows (mini_window))
13584 goto retry;
13585 }
13586 }
13587
13588 /* If display was paused because of pending input, make sure we do a
13589 thorough update the next time. */
13590 if (pending)
13591 {
13592 /* Prevent the optimization at the beginning of
13593 redisplay_internal that tries a single-line update of the
13594 line containing the cursor in the selected window. */
13595 CHARPOS (this_line_start_pos) = 0;
13596
13597 /* Let the overlay arrow be updated the next time. */
13598 update_overlay_arrows (0);
13599
13600 /* If we pause after scrolling, some rows in the current
13601 matrices of some windows are not valid. */
13602 if (!WINDOW_FULL_WIDTH_P (w)
13603 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13604 update_mode_lines = 1;
13605 }
13606 else
13607 {
13608 if (!consider_all_windows_p)
13609 {
13610 /* This has already been done above if
13611 consider_all_windows_p is set. */
13612 mark_window_display_accurate_1 (w, 1);
13613
13614 /* Say overlay arrows are up to date. */
13615 update_overlay_arrows (1);
13616
13617 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13618 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13619 }
13620
13621 update_mode_lines = 0;
13622 windows_or_buffers_changed = 0;
13623 cursor_type_changed = 0;
13624 }
13625
13626 /* Start SIGIO interrupts coming again. Having them off during the
13627 code above makes it less likely one will discard output, but not
13628 impossible, since there might be stuff in the system buffer here.
13629 But it is much hairier to try to do anything about that. */
13630 if (interrupt_input)
13631 request_sigio ();
13632 RESUME_POLLING;
13633
13634 /* If a frame has become visible which was not before, redisplay
13635 again, so that we display it. Expose events for such a frame
13636 (which it gets when becoming visible) don't call the parts of
13637 redisplay constructing glyphs, so simply exposing a frame won't
13638 display anything in this case. So, we have to display these
13639 frames here explicitly. */
13640 if (!pending)
13641 {
13642 int new_count = 0;
13643
13644 FOR_EACH_FRAME (tail, frame)
13645 {
13646 int this_is_visible = 0;
13647
13648 if (XFRAME (frame)->visible)
13649 this_is_visible = 1;
13650
13651 if (this_is_visible)
13652 new_count++;
13653 }
13654
13655 if (new_count != number_of_visible_frames)
13656 windows_or_buffers_changed++;
13657 }
13658
13659 /* Change frame size now if a change is pending. */
13660 do_pending_window_change (1);
13661
13662 /* If we just did a pending size change, or have additional
13663 visible frames, or selected_window changed, redisplay again. */
13664 if ((windows_or_buffers_changed && !pending)
13665 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13666 goto retry;
13667
13668 /* Clear the face and image caches.
13669
13670 We used to do this only if consider_all_windows_p. But the cache
13671 needs to be cleared if a timer creates images in the current
13672 buffer (e.g. the test case in Bug#6230). */
13673
13674 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13675 {
13676 clear_face_cache (0);
13677 clear_face_cache_count = 0;
13678 }
13679
13680 #ifdef HAVE_WINDOW_SYSTEM
13681 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13682 {
13683 clear_image_caches (Qnil);
13684 clear_image_cache_count = 0;
13685 }
13686 #endif /* HAVE_WINDOW_SYSTEM */
13687
13688 end_of_redisplay:
13689 unbind_to (count, Qnil);
13690 RESUME_POLLING;
13691 }
13692
13693
13694 /* Redisplay, but leave alone any recent echo area message unless
13695 another message has been requested in its place.
13696
13697 This is useful in situations where you need to redisplay but no
13698 user action has occurred, making it inappropriate for the message
13699 area to be cleared. See tracking_off and
13700 wait_reading_process_output for examples of these situations.
13701
13702 FROM_WHERE is an integer saying from where this function was
13703 called. This is useful for debugging. */
13704
13705 void
13706 redisplay_preserve_echo_area (int from_where)
13707 {
13708 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13709
13710 if (!NILP (echo_area_buffer[1]))
13711 {
13712 /* We have a previously displayed message, but no current
13713 message. Redisplay the previous message. */
13714 display_last_displayed_message_p = 1;
13715 redisplay_internal ();
13716 display_last_displayed_message_p = 0;
13717 }
13718 else
13719 redisplay_internal ();
13720
13721 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13722 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13723 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13724 }
13725
13726
13727 /* Function registered with record_unwind_protect in redisplay_internal. */
13728
13729 static void
13730 unwind_redisplay (void)
13731 {
13732 redisplaying_p = 0;
13733 }
13734
13735
13736 /* Mark the display of leaf window W as accurate or inaccurate.
13737 If ACCURATE_P is non-zero mark display of W as accurate. If
13738 ACCURATE_P is zero, arrange for W to be redisplayed the next
13739 time redisplay_internal is called. */
13740
13741 static void
13742 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13743 {
13744 struct buffer *b = XBUFFER (w->contents);
13745
13746 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13747 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13748 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13749
13750 if (accurate_p)
13751 {
13752 b->clip_changed = 0;
13753 b->prevent_redisplay_optimizations_p = 0;
13754
13755 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13756 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13757 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13758 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13759
13760 w->current_matrix->buffer = b;
13761 w->current_matrix->begv = BUF_BEGV (b);
13762 w->current_matrix->zv = BUF_ZV (b);
13763
13764 w->last_cursor = w->cursor;
13765 w->last_cursor_off_p = w->cursor_off_p;
13766
13767 if (w == XWINDOW (selected_window))
13768 w->last_point = BUF_PT (b);
13769 else
13770 w->last_point = marker_position (w->pointm);
13771
13772 w->window_end_valid = 1;
13773 w->update_mode_line = 0;
13774 }
13775 }
13776
13777
13778 /* Mark the display of windows in the window tree rooted at WINDOW as
13779 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13780 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13781 be redisplayed the next time redisplay_internal is called. */
13782
13783 void
13784 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13785 {
13786 struct window *w;
13787
13788 for (; !NILP (window); window = w->next)
13789 {
13790 w = XWINDOW (window);
13791 if (WINDOWP (w->contents))
13792 mark_window_display_accurate (w->contents, accurate_p);
13793 else
13794 mark_window_display_accurate_1 (w, accurate_p);
13795 }
13796
13797 if (accurate_p)
13798 update_overlay_arrows (1);
13799 else
13800 /* Force a thorough redisplay the next time by setting
13801 last_arrow_position and last_arrow_string to t, which is
13802 unequal to any useful value of Voverlay_arrow_... */
13803 update_overlay_arrows (-1);
13804 }
13805
13806
13807 /* Return value in display table DP (Lisp_Char_Table *) for character
13808 C. Since a display table doesn't have any parent, we don't have to
13809 follow parent. Do not call this function directly but use the
13810 macro DISP_CHAR_VECTOR. */
13811
13812 Lisp_Object
13813 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13814 {
13815 Lisp_Object val;
13816
13817 if (ASCII_CHAR_P (c))
13818 {
13819 val = dp->ascii;
13820 if (SUB_CHAR_TABLE_P (val))
13821 val = XSUB_CHAR_TABLE (val)->contents[c];
13822 }
13823 else
13824 {
13825 Lisp_Object table;
13826
13827 XSETCHAR_TABLE (table, dp);
13828 val = char_table_ref (table, c);
13829 }
13830 if (NILP (val))
13831 val = dp->defalt;
13832 return val;
13833 }
13834
13835
13836 \f
13837 /***********************************************************************
13838 Window Redisplay
13839 ***********************************************************************/
13840
13841 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13842
13843 static void
13844 redisplay_windows (Lisp_Object window)
13845 {
13846 while (!NILP (window))
13847 {
13848 struct window *w = XWINDOW (window);
13849
13850 if (WINDOWP (w->contents))
13851 redisplay_windows (w->contents);
13852 else if (BUFFERP (w->contents))
13853 {
13854 displayed_buffer = XBUFFER (w->contents);
13855 /* Use list_of_error, not Qerror, so that
13856 we catch only errors and don't run the debugger. */
13857 internal_condition_case_1 (redisplay_window_0, window,
13858 list_of_error,
13859 redisplay_window_error);
13860 }
13861
13862 window = w->next;
13863 }
13864 }
13865
13866 static Lisp_Object
13867 redisplay_window_error (Lisp_Object ignore)
13868 {
13869 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13870 return Qnil;
13871 }
13872
13873 static Lisp_Object
13874 redisplay_window_0 (Lisp_Object window)
13875 {
13876 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13877 redisplay_window (window, 0);
13878 return Qnil;
13879 }
13880
13881 static Lisp_Object
13882 redisplay_window_1 (Lisp_Object window)
13883 {
13884 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13885 redisplay_window (window, 1);
13886 return Qnil;
13887 }
13888 \f
13889
13890 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13891 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13892 which positions recorded in ROW differ from current buffer
13893 positions.
13894
13895 Return 0 if cursor is not on this row, 1 otherwise. */
13896
13897 static int
13898 set_cursor_from_row (struct window *w, struct glyph_row *row,
13899 struct glyph_matrix *matrix,
13900 ptrdiff_t delta, ptrdiff_t delta_bytes,
13901 int dy, int dvpos)
13902 {
13903 struct glyph *glyph = row->glyphs[TEXT_AREA];
13904 struct glyph *end = glyph + row->used[TEXT_AREA];
13905 struct glyph *cursor = NULL;
13906 /* The last known character position in row. */
13907 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13908 int x = row->x;
13909 ptrdiff_t pt_old = PT - delta;
13910 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13911 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13912 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13913 /* A glyph beyond the edge of TEXT_AREA which we should never
13914 touch. */
13915 struct glyph *glyphs_end = end;
13916 /* Non-zero means we've found a match for cursor position, but that
13917 glyph has the avoid_cursor_p flag set. */
13918 int match_with_avoid_cursor = 0;
13919 /* Non-zero means we've seen at least one glyph that came from a
13920 display string. */
13921 int string_seen = 0;
13922 /* Largest and smallest buffer positions seen so far during scan of
13923 glyph row. */
13924 ptrdiff_t bpos_max = pos_before;
13925 ptrdiff_t bpos_min = pos_after;
13926 /* Last buffer position covered by an overlay string with an integer
13927 `cursor' property. */
13928 ptrdiff_t bpos_covered = 0;
13929 /* Non-zero means the display string on which to display the cursor
13930 comes from a text property, not from an overlay. */
13931 int string_from_text_prop = 0;
13932
13933 /* Don't even try doing anything if called for a mode-line or
13934 header-line row, since the rest of the code isn't prepared to
13935 deal with such calamities. */
13936 eassert (!row->mode_line_p);
13937 if (row->mode_line_p)
13938 return 0;
13939
13940 /* Skip over glyphs not having an object at the start and the end of
13941 the row. These are special glyphs like truncation marks on
13942 terminal frames. */
13943 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13944 {
13945 if (!row->reversed_p)
13946 {
13947 while (glyph < end
13948 && INTEGERP (glyph->object)
13949 && glyph->charpos < 0)
13950 {
13951 x += glyph->pixel_width;
13952 ++glyph;
13953 }
13954 while (end > glyph
13955 && INTEGERP ((end - 1)->object)
13956 /* CHARPOS is zero for blanks and stretch glyphs
13957 inserted by extend_face_to_end_of_line. */
13958 && (end - 1)->charpos <= 0)
13959 --end;
13960 glyph_before = glyph - 1;
13961 glyph_after = end;
13962 }
13963 else
13964 {
13965 struct glyph *g;
13966
13967 /* If the glyph row is reversed, we need to process it from back
13968 to front, so swap the edge pointers. */
13969 glyphs_end = end = glyph - 1;
13970 glyph += row->used[TEXT_AREA] - 1;
13971
13972 while (glyph > end + 1
13973 && INTEGERP (glyph->object)
13974 && glyph->charpos < 0)
13975 {
13976 --glyph;
13977 x -= glyph->pixel_width;
13978 }
13979 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13980 --glyph;
13981 /* By default, in reversed rows we put the cursor on the
13982 rightmost (first in the reading order) glyph. */
13983 for (g = end + 1; g < glyph; g++)
13984 x += g->pixel_width;
13985 while (end < glyph
13986 && INTEGERP ((end + 1)->object)
13987 && (end + 1)->charpos <= 0)
13988 ++end;
13989 glyph_before = glyph + 1;
13990 glyph_after = end;
13991 }
13992 }
13993 else if (row->reversed_p)
13994 {
13995 /* In R2L rows that don't display text, put the cursor on the
13996 rightmost glyph. Case in point: an empty last line that is
13997 part of an R2L paragraph. */
13998 cursor = end - 1;
13999 /* Avoid placing the cursor on the last glyph of the row, where
14000 on terminal frames we hold the vertical border between
14001 adjacent windows. */
14002 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14003 && !WINDOW_RIGHTMOST_P (w)
14004 && cursor == row->glyphs[LAST_AREA] - 1)
14005 cursor--;
14006 x = -1; /* will be computed below, at label compute_x */
14007 }
14008
14009 /* Step 1: Try to find the glyph whose character position
14010 corresponds to point. If that's not possible, find 2 glyphs
14011 whose character positions are the closest to point, one before
14012 point, the other after it. */
14013 if (!row->reversed_p)
14014 while (/* not marched to end of glyph row */
14015 glyph < end
14016 /* glyph was not inserted by redisplay for internal purposes */
14017 && !INTEGERP (glyph->object))
14018 {
14019 if (BUFFERP (glyph->object))
14020 {
14021 ptrdiff_t dpos = glyph->charpos - pt_old;
14022
14023 if (glyph->charpos > bpos_max)
14024 bpos_max = glyph->charpos;
14025 if (glyph->charpos < bpos_min)
14026 bpos_min = glyph->charpos;
14027 if (!glyph->avoid_cursor_p)
14028 {
14029 /* If we hit point, we've found the glyph on which to
14030 display the cursor. */
14031 if (dpos == 0)
14032 {
14033 match_with_avoid_cursor = 0;
14034 break;
14035 }
14036 /* See if we've found a better approximation to
14037 POS_BEFORE or to POS_AFTER. */
14038 if (0 > dpos && dpos > pos_before - pt_old)
14039 {
14040 pos_before = glyph->charpos;
14041 glyph_before = glyph;
14042 }
14043 else if (0 < dpos && dpos < pos_after - pt_old)
14044 {
14045 pos_after = glyph->charpos;
14046 glyph_after = glyph;
14047 }
14048 }
14049 else if (dpos == 0)
14050 match_with_avoid_cursor = 1;
14051 }
14052 else if (STRINGP (glyph->object))
14053 {
14054 Lisp_Object chprop;
14055 ptrdiff_t glyph_pos = glyph->charpos;
14056
14057 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14058 glyph->object);
14059 if (!NILP (chprop))
14060 {
14061 /* If the string came from a `display' text property,
14062 look up the buffer position of that property and
14063 use that position to update bpos_max, as if we
14064 actually saw such a position in one of the row's
14065 glyphs. This helps with supporting integer values
14066 of `cursor' property on the display string in
14067 situations where most or all of the row's buffer
14068 text is completely covered by display properties,
14069 so that no glyph with valid buffer positions is
14070 ever seen in the row. */
14071 ptrdiff_t prop_pos =
14072 string_buffer_position_lim (glyph->object, pos_before,
14073 pos_after, 0);
14074
14075 if (prop_pos >= pos_before)
14076 bpos_max = prop_pos - 1;
14077 }
14078 if (INTEGERP (chprop))
14079 {
14080 bpos_covered = bpos_max + XINT (chprop);
14081 /* If the `cursor' property covers buffer positions up
14082 to and including point, we should display cursor on
14083 this glyph. Note that, if a `cursor' property on one
14084 of the string's characters has an integer value, we
14085 will break out of the loop below _before_ we get to
14086 the position match above. IOW, integer values of
14087 the `cursor' property override the "exact match for
14088 point" strategy of positioning the cursor. */
14089 /* Implementation note: bpos_max == pt_old when, e.g.,
14090 we are in an empty line, where bpos_max is set to
14091 MATRIX_ROW_START_CHARPOS, see above. */
14092 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14093 {
14094 cursor = glyph;
14095 break;
14096 }
14097 }
14098
14099 string_seen = 1;
14100 }
14101 x += glyph->pixel_width;
14102 ++glyph;
14103 }
14104 else if (glyph > end) /* row is reversed */
14105 while (!INTEGERP (glyph->object))
14106 {
14107 if (BUFFERP (glyph->object))
14108 {
14109 ptrdiff_t dpos = glyph->charpos - pt_old;
14110
14111 if (glyph->charpos > bpos_max)
14112 bpos_max = glyph->charpos;
14113 if (glyph->charpos < bpos_min)
14114 bpos_min = glyph->charpos;
14115 if (!glyph->avoid_cursor_p)
14116 {
14117 if (dpos == 0)
14118 {
14119 match_with_avoid_cursor = 0;
14120 break;
14121 }
14122 if (0 > dpos && dpos > pos_before - pt_old)
14123 {
14124 pos_before = glyph->charpos;
14125 glyph_before = glyph;
14126 }
14127 else if (0 < dpos && dpos < pos_after - pt_old)
14128 {
14129 pos_after = glyph->charpos;
14130 glyph_after = glyph;
14131 }
14132 }
14133 else if (dpos == 0)
14134 match_with_avoid_cursor = 1;
14135 }
14136 else if (STRINGP (glyph->object))
14137 {
14138 Lisp_Object chprop;
14139 ptrdiff_t glyph_pos = glyph->charpos;
14140
14141 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14142 glyph->object);
14143 if (!NILP (chprop))
14144 {
14145 ptrdiff_t prop_pos =
14146 string_buffer_position_lim (glyph->object, pos_before,
14147 pos_after, 0);
14148
14149 if (prop_pos >= pos_before)
14150 bpos_max = prop_pos - 1;
14151 }
14152 if (INTEGERP (chprop))
14153 {
14154 bpos_covered = bpos_max + XINT (chprop);
14155 /* If the `cursor' property covers buffer positions up
14156 to and including point, we should display cursor on
14157 this glyph. */
14158 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14159 {
14160 cursor = glyph;
14161 break;
14162 }
14163 }
14164 string_seen = 1;
14165 }
14166 --glyph;
14167 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14168 {
14169 x--; /* can't use any pixel_width */
14170 break;
14171 }
14172 x -= glyph->pixel_width;
14173 }
14174
14175 /* Step 2: If we didn't find an exact match for point, we need to
14176 look for a proper place to put the cursor among glyphs between
14177 GLYPH_BEFORE and GLYPH_AFTER. */
14178 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14179 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14180 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14181 {
14182 /* An empty line has a single glyph whose OBJECT is zero and
14183 whose CHARPOS is the position of a newline on that line.
14184 Note that on a TTY, there are more glyphs after that, which
14185 were produced by extend_face_to_end_of_line, but their
14186 CHARPOS is zero or negative. */
14187 int empty_line_p =
14188 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14189 && INTEGERP (glyph->object) && glyph->charpos > 0
14190 /* On a TTY, continued and truncated rows also have a glyph at
14191 their end whose OBJECT is zero and whose CHARPOS is
14192 positive (the continuation and truncation glyphs), but such
14193 rows are obviously not "empty". */
14194 && !(row->continued_p || row->truncated_on_right_p);
14195
14196 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14197 {
14198 ptrdiff_t ellipsis_pos;
14199
14200 /* Scan back over the ellipsis glyphs. */
14201 if (!row->reversed_p)
14202 {
14203 ellipsis_pos = (glyph - 1)->charpos;
14204 while (glyph > row->glyphs[TEXT_AREA]
14205 && (glyph - 1)->charpos == ellipsis_pos)
14206 glyph--, x -= glyph->pixel_width;
14207 /* That loop always goes one position too far, including
14208 the glyph before the ellipsis. So scan forward over
14209 that one. */
14210 x += glyph->pixel_width;
14211 glyph++;
14212 }
14213 else /* row is reversed */
14214 {
14215 ellipsis_pos = (glyph + 1)->charpos;
14216 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14217 && (glyph + 1)->charpos == ellipsis_pos)
14218 glyph++, x += glyph->pixel_width;
14219 x -= glyph->pixel_width;
14220 glyph--;
14221 }
14222 }
14223 else if (match_with_avoid_cursor)
14224 {
14225 cursor = glyph_after;
14226 x = -1;
14227 }
14228 else if (string_seen)
14229 {
14230 int incr = row->reversed_p ? -1 : +1;
14231
14232 /* Need to find the glyph that came out of a string which is
14233 present at point. That glyph is somewhere between
14234 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14235 positioned between POS_BEFORE and POS_AFTER in the
14236 buffer. */
14237 struct glyph *start, *stop;
14238 ptrdiff_t pos = pos_before;
14239
14240 x = -1;
14241
14242 /* If the row ends in a newline from a display string,
14243 reordering could have moved the glyphs belonging to the
14244 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14245 in this case we extend the search to the last glyph in
14246 the row that was not inserted by redisplay. */
14247 if (row->ends_in_newline_from_string_p)
14248 {
14249 glyph_after = end;
14250 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14251 }
14252
14253 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14254 correspond to POS_BEFORE and POS_AFTER, respectively. We
14255 need START and STOP in the order that corresponds to the
14256 row's direction as given by its reversed_p flag. If the
14257 directionality of characters between POS_BEFORE and
14258 POS_AFTER is the opposite of the row's base direction,
14259 these characters will have been reordered for display,
14260 and we need to reverse START and STOP. */
14261 if (!row->reversed_p)
14262 {
14263 start = min (glyph_before, glyph_after);
14264 stop = max (glyph_before, glyph_after);
14265 }
14266 else
14267 {
14268 start = max (glyph_before, glyph_after);
14269 stop = min (glyph_before, glyph_after);
14270 }
14271 for (glyph = start + incr;
14272 row->reversed_p ? glyph > stop : glyph < stop; )
14273 {
14274
14275 /* Any glyphs that come from the buffer are here because
14276 of bidi reordering. Skip them, and only pay
14277 attention to glyphs that came from some string. */
14278 if (STRINGP (glyph->object))
14279 {
14280 Lisp_Object str;
14281 ptrdiff_t tem;
14282 /* If the display property covers the newline, we
14283 need to search for it one position farther. */
14284 ptrdiff_t lim = pos_after
14285 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14286
14287 string_from_text_prop = 0;
14288 str = glyph->object;
14289 tem = string_buffer_position_lim (str, pos, lim, 0);
14290 if (tem == 0 /* from overlay */
14291 || pos <= tem)
14292 {
14293 /* If the string from which this glyph came is
14294 found in the buffer at point, or at position
14295 that is closer to point than pos_after, then
14296 we've found the glyph we've been looking for.
14297 If it comes from an overlay (tem == 0), and
14298 it has the `cursor' property on one of its
14299 glyphs, record that glyph as a candidate for
14300 displaying the cursor. (As in the
14301 unidirectional version, we will display the
14302 cursor on the last candidate we find.) */
14303 if (tem == 0
14304 || tem == pt_old
14305 || (tem - pt_old > 0 && tem < pos_after))
14306 {
14307 /* The glyphs from this string could have
14308 been reordered. Find the one with the
14309 smallest string position. Or there could
14310 be a character in the string with the
14311 `cursor' property, which means display
14312 cursor on that character's glyph. */
14313 ptrdiff_t strpos = glyph->charpos;
14314
14315 if (tem)
14316 {
14317 cursor = glyph;
14318 string_from_text_prop = 1;
14319 }
14320 for ( ;
14321 (row->reversed_p ? glyph > stop : glyph < stop)
14322 && EQ (glyph->object, str);
14323 glyph += incr)
14324 {
14325 Lisp_Object cprop;
14326 ptrdiff_t gpos = glyph->charpos;
14327
14328 cprop = Fget_char_property (make_number (gpos),
14329 Qcursor,
14330 glyph->object);
14331 if (!NILP (cprop))
14332 {
14333 cursor = glyph;
14334 break;
14335 }
14336 if (tem && glyph->charpos < strpos)
14337 {
14338 strpos = glyph->charpos;
14339 cursor = glyph;
14340 }
14341 }
14342
14343 if (tem == pt_old
14344 || (tem - pt_old > 0 && tem < pos_after))
14345 goto compute_x;
14346 }
14347 if (tem)
14348 pos = tem + 1; /* don't find previous instances */
14349 }
14350 /* This string is not what we want; skip all of the
14351 glyphs that came from it. */
14352 while ((row->reversed_p ? glyph > stop : glyph < stop)
14353 && EQ (glyph->object, str))
14354 glyph += incr;
14355 }
14356 else
14357 glyph += incr;
14358 }
14359
14360 /* If we reached the end of the line, and END was from a string,
14361 the cursor is not on this line. */
14362 if (cursor == NULL
14363 && (row->reversed_p ? glyph <= end : glyph >= end)
14364 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14365 && STRINGP (end->object)
14366 && row->continued_p)
14367 return 0;
14368 }
14369 /* A truncated row may not include PT among its character positions.
14370 Setting the cursor inside the scroll margin will trigger
14371 recalculation of hscroll in hscroll_window_tree. But if a
14372 display string covers point, defer to the string-handling
14373 code below to figure this out. */
14374 else if (row->truncated_on_left_p && pt_old < bpos_min)
14375 {
14376 cursor = glyph_before;
14377 x = -1;
14378 }
14379 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14380 /* Zero-width characters produce no glyphs. */
14381 || (!empty_line_p
14382 && (row->reversed_p
14383 ? glyph_after > glyphs_end
14384 : glyph_after < glyphs_end)))
14385 {
14386 cursor = glyph_after;
14387 x = -1;
14388 }
14389 }
14390
14391 compute_x:
14392 if (cursor != NULL)
14393 glyph = cursor;
14394 else if (glyph == glyphs_end
14395 && pos_before == pos_after
14396 && STRINGP ((row->reversed_p
14397 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14398 : row->glyphs[TEXT_AREA])->object))
14399 {
14400 /* If all the glyphs of this row came from strings, put the
14401 cursor on the first glyph of the row. This avoids having the
14402 cursor outside of the text area in this very rare and hard
14403 use case. */
14404 glyph =
14405 row->reversed_p
14406 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14407 : row->glyphs[TEXT_AREA];
14408 }
14409 if (x < 0)
14410 {
14411 struct glyph *g;
14412
14413 /* Need to compute x that corresponds to GLYPH. */
14414 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14415 {
14416 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14417 emacs_abort ();
14418 x += g->pixel_width;
14419 }
14420 }
14421
14422 /* ROW could be part of a continued line, which, under bidi
14423 reordering, might have other rows whose start and end charpos
14424 occlude point. Only set w->cursor if we found a better
14425 approximation to the cursor position than we have from previously
14426 examined candidate rows belonging to the same continued line. */
14427 if (/* we already have a candidate row */
14428 w->cursor.vpos >= 0
14429 /* that candidate is not the row we are processing */
14430 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14431 /* Make sure cursor.vpos specifies a row whose start and end
14432 charpos occlude point, and it is valid candidate for being a
14433 cursor-row. This is because some callers of this function
14434 leave cursor.vpos at the row where the cursor was displayed
14435 during the last redisplay cycle. */
14436 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14437 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14438 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14439 {
14440 struct glyph *g1 =
14441 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14442
14443 /* Don't consider glyphs that are outside TEXT_AREA. */
14444 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14445 return 0;
14446 /* Keep the candidate whose buffer position is the closest to
14447 point or has the `cursor' property. */
14448 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14449 w->cursor.hpos >= 0
14450 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14451 && ((BUFFERP (g1->object)
14452 && (g1->charpos == pt_old /* an exact match always wins */
14453 || (BUFFERP (glyph->object)
14454 && eabs (g1->charpos - pt_old)
14455 < eabs (glyph->charpos - pt_old))))
14456 /* previous candidate is a glyph from a string that has
14457 a non-nil `cursor' property */
14458 || (STRINGP (g1->object)
14459 && (!NILP (Fget_char_property (make_number (g1->charpos),
14460 Qcursor, g1->object))
14461 /* previous candidate is from the same display
14462 string as this one, and the display string
14463 came from a text property */
14464 || (EQ (g1->object, glyph->object)
14465 && string_from_text_prop)
14466 /* this candidate is from newline and its
14467 position is not an exact match */
14468 || (INTEGERP (glyph->object)
14469 && glyph->charpos != pt_old)))))
14470 return 0;
14471 /* If this candidate gives an exact match, use that. */
14472 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14473 /* If this candidate is a glyph created for the
14474 terminating newline of a line, and point is on that
14475 newline, it wins because it's an exact match. */
14476 || (!row->continued_p
14477 && INTEGERP (glyph->object)
14478 && glyph->charpos == 0
14479 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14480 /* Otherwise, keep the candidate that comes from a row
14481 spanning less buffer positions. This may win when one or
14482 both candidate positions are on glyphs that came from
14483 display strings, for which we cannot compare buffer
14484 positions. */
14485 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14486 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14487 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14488 return 0;
14489 }
14490 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14491 w->cursor.x = x;
14492 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14493 w->cursor.y = row->y + dy;
14494
14495 if (w == XWINDOW (selected_window))
14496 {
14497 if (!row->continued_p
14498 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14499 && row->x == 0)
14500 {
14501 this_line_buffer = XBUFFER (w->contents);
14502
14503 CHARPOS (this_line_start_pos)
14504 = MATRIX_ROW_START_CHARPOS (row) + delta;
14505 BYTEPOS (this_line_start_pos)
14506 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14507
14508 CHARPOS (this_line_end_pos)
14509 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14510 BYTEPOS (this_line_end_pos)
14511 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14512
14513 this_line_y = w->cursor.y;
14514 this_line_pixel_height = row->height;
14515 this_line_vpos = w->cursor.vpos;
14516 this_line_start_x = row->x;
14517 }
14518 else
14519 CHARPOS (this_line_start_pos) = 0;
14520 }
14521
14522 return 1;
14523 }
14524
14525
14526 /* Run window scroll functions, if any, for WINDOW with new window
14527 start STARTP. Sets the window start of WINDOW to that position.
14528
14529 We assume that the window's buffer is really current. */
14530
14531 static struct text_pos
14532 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14533 {
14534 struct window *w = XWINDOW (window);
14535 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14536
14537 eassert (current_buffer == XBUFFER (w->contents));
14538
14539 if (!NILP (Vwindow_scroll_functions))
14540 {
14541 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14542 make_number (CHARPOS (startp)));
14543 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14544 /* In case the hook functions switch buffers. */
14545 set_buffer_internal (XBUFFER (w->contents));
14546 }
14547
14548 return startp;
14549 }
14550
14551
14552 /* Make sure the line containing the cursor is fully visible.
14553 A value of 1 means there is nothing to be done.
14554 (Either the line is fully visible, or it cannot be made so,
14555 or we cannot tell.)
14556
14557 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14558 is higher than window.
14559
14560 A value of 0 means the caller should do scrolling
14561 as if point had gone off the screen. */
14562
14563 static int
14564 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14565 {
14566 struct glyph_matrix *matrix;
14567 struct glyph_row *row;
14568 int window_height;
14569
14570 if (!make_cursor_line_fully_visible_p)
14571 return 1;
14572
14573 /* It's not always possible to find the cursor, e.g, when a window
14574 is full of overlay strings. Don't do anything in that case. */
14575 if (w->cursor.vpos < 0)
14576 return 1;
14577
14578 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14579 row = MATRIX_ROW (matrix, w->cursor.vpos);
14580
14581 /* If the cursor row is not partially visible, there's nothing to do. */
14582 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14583 return 1;
14584
14585 /* If the row the cursor is in is taller than the window's height,
14586 it's not clear what to do, so do nothing. */
14587 window_height = window_box_height (w);
14588 if (row->height >= window_height)
14589 {
14590 if (!force_p || MINI_WINDOW_P (w)
14591 || w->vscroll || w->cursor.vpos == 0)
14592 return 1;
14593 }
14594 return 0;
14595 }
14596
14597
14598 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14599 non-zero means only WINDOW is redisplayed in redisplay_internal.
14600 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14601 in redisplay_window to bring a partially visible line into view in
14602 the case that only the cursor has moved.
14603
14604 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14605 last screen line's vertical height extends past the end of the screen.
14606
14607 Value is
14608
14609 1 if scrolling succeeded
14610
14611 0 if scrolling didn't find point.
14612
14613 -1 if new fonts have been loaded so that we must interrupt
14614 redisplay, adjust glyph matrices, and try again. */
14615
14616 enum
14617 {
14618 SCROLLING_SUCCESS,
14619 SCROLLING_FAILED,
14620 SCROLLING_NEED_LARGER_MATRICES
14621 };
14622
14623 /* If scroll-conservatively is more than this, never recenter.
14624
14625 If you change this, don't forget to update the doc string of
14626 `scroll-conservatively' and the Emacs manual. */
14627 #define SCROLL_LIMIT 100
14628
14629 static int
14630 try_scrolling (Lisp_Object window, int just_this_one_p,
14631 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14632 int temp_scroll_step, int last_line_misfit)
14633 {
14634 struct window *w = XWINDOW (window);
14635 struct frame *f = XFRAME (w->frame);
14636 struct text_pos pos, startp;
14637 struct it it;
14638 int this_scroll_margin, scroll_max, rc, height;
14639 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14640 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14641 Lisp_Object aggressive;
14642 /* We will never try scrolling more than this number of lines. */
14643 int scroll_limit = SCROLL_LIMIT;
14644 int frame_line_height = default_line_pixel_height (w);
14645 int window_total_lines
14646 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14647
14648 #ifdef GLYPH_DEBUG
14649 debug_method_add (w, "try_scrolling");
14650 #endif
14651
14652 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14653
14654 /* Compute scroll margin height in pixels. We scroll when point is
14655 within this distance from the top or bottom of the window. */
14656 if (scroll_margin > 0)
14657 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14658 * frame_line_height;
14659 else
14660 this_scroll_margin = 0;
14661
14662 /* Force arg_scroll_conservatively to have a reasonable value, to
14663 avoid scrolling too far away with slow move_it_* functions. Note
14664 that the user can supply scroll-conservatively equal to
14665 `most-positive-fixnum', which can be larger than INT_MAX. */
14666 if (arg_scroll_conservatively > scroll_limit)
14667 {
14668 arg_scroll_conservatively = scroll_limit + 1;
14669 scroll_max = scroll_limit * frame_line_height;
14670 }
14671 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14672 /* Compute how much we should try to scroll maximally to bring
14673 point into view. */
14674 scroll_max = (max (scroll_step,
14675 max (arg_scroll_conservatively, temp_scroll_step))
14676 * frame_line_height);
14677 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14678 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14679 /* We're trying to scroll because of aggressive scrolling but no
14680 scroll_step is set. Choose an arbitrary one. */
14681 scroll_max = 10 * frame_line_height;
14682 else
14683 scroll_max = 0;
14684
14685 too_near_end:
14686
14687 /* Decide whether to scroll down. */
14688 if (PT > CHARPOS (startp))
14689 {
14690 int scroll_margin_y;
14691
14692 /* Compute the pixel ypos of the scroll margin, then move IT to
14693 either that ypos or PT, whichever comes first. */
14694 start_display (&it, w, startp);
14695 scroll_margin_y = it.last_visible_y - this_scroll_margin
14696 - frame_line_height * extra_scroll_margin_lines;
14697 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14698 (MOVE_TO_POS | MOVE_TO_Y));
14699
14700 if (PT > CHARPOS (it.current.pos))
14701 {
14702 int y0 = line_bottom_y (&it);
14703 /* Compute how many pixels below window bottom to stop searching
14704 for PT. This avoids costly search for PT that is far away if
14705 the user limited scrolling by a small number of lines, but
14706 always finds PT if scroll_conservatively is set to a large
14707 number, such as most-positive-fixnum. */
14708 int slack = max (scroll_max, 10 * frame_line_height);
14709 int y_to_move = it.last_visible_y + slack;
14710
14711 /* Compute the distance from the scroll margin to PT or to
14712 the scroll limit, whichever comes first. This should
14713 include the height of the cursor line, to make that line
14714 fully visible. */
14715 move_it_to (&it, PT, -1, y_to_move,
14716 -1, MOVE_TO_POS | MOVE_TO_Y);
14717 dy = line_bottom_y (&it) - y0;
14718
14719 if (dy > scroll_max)
14720 return SCROLLING_FAILED;
14721
14722 if (dy > 0)
14723 scroll_down_p = 1;
14724 }
14725 }
14726
14727 if (scroll_down_p)
14728 {
14729 /* Point is in or below the bottom scroll margin, so move the
14730 window start down. If scrolling conservatively, move it just
14731 enough down to make point visible. If scroll_step is set,
14732 move it down by scroll_step. */
14733 if (arg_scroll_conservatively)
14734 amount_to_scroll
14735 = min (max (dy, frame_line_height),
14736 frame_line_height * arg_scroll_conservatively);
14737 else if (scroll_step || temp_scroll_step)
14738 amount_to_scroll = scroll_max;
14739 else
14740 {
14741 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14742 height = WINDOW_BOX_TEXT_HEIGHT (w);
14743 if (NUMBERP (aggressive))
14744 {
14745 double float_amount = XFLOATINT (aggressive) * height;
14746 int aggressive_scroll = float_amount;
14747 if (aggressive_scroll == 0 && float_amount > 0)
14748 aggressive_scroll = 1;
14749 /* Don't let point enter the scroll margin near top of
14750 the window. This could happen if the value of
14751 scroll_up_aggressively is too large and there are
14752 non-zero margins, because scroll_up_aggressively
14753 means put point that fraction of window height
14754 _from_the_bottom_margin_. */
14755 if (aggressive_scroll + 2*this_scroll_margin > height)
14756 aggressive_scroll = height - 2*this_scroll_margin;
14757 amount_to_scroll = dy + aggressive_scroll;
14758 }
14759 }
14760
14761 if (amount_to_scroll <= 0)
14762 return SCROLLING_FAILED;
14763
14764 start_display (&it, w, startp);
14765 if (arg_scroll_conservatively <= scroll_limit)
14766 move_it_vertically (&it, amount_to_scroll);
14767 else
14768 {
14769 /* Extra precision for users who set scroll-conservatively
14770 to a large number: make sure the amount we scroll
14771 the window start is never less than amount_to_scroll,
14772 which was computed as distance from window bottom to
14773 point. This matters when lines at window top and lines
14774 below window bottom have different height. */
14775 struct it it1;
14776 void *it1data = NULL;
14777 /* We use a temporary it1 because line_bottom_y can modify
14778 its argument, if it moves one line down; see there. */
14779 int start_y;
14780
14781 SAVE_IT (it1, it, it1data);
14782 start_y = line_bottom_y (&it1);
14783 do {
14784 RESTORE_IT (&it, &it, it1data);
14785 move_it_by_lines (&it, 1);
14786 SAVE_IT (it1, it, it1data);
14787 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14788 }
14789
14790 /* If STARTP is unchanged, move it down another screen line. */
14791 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14792 move_it_by_lines (&it, 1);
14793 startp = it.current.pos;
14794 }
14795 else
14796 {
14797 struct text_pos scroll_margin_pos = startp;
14798 int y_offset = 0;
14799
14800 /* See if point is inside the scroll margin at the top of the
14801 window. */
14802 if (this_scroll_margin)
14803 {
14804 int y_start;
14805
14806 start_display (&it, w, startp);
14807 y_start = it.current_y;
14808 move_it_vertically (&it, this_scroll_margin);
14809 scroll_margin_pos = it.current.pos;
14810 /* If we didn't move enough before hitting ZV, request
14811 additional amount of scroll, to move point out of the
14812 scroll margin. */
14813 if (IT_CHARPOS (it) == ZV
14814 && it.current_y - y_start < this_scroll_margin)
14815 y_offset = this_scroll_margin - (it.current_y - y_start);
14816 }
14817
14818 if (PT < CHARPOS (scroll_margin_pos))
14819 {
14820 /* Point is in the scroll margin at the top of the window or
14821 above what is displayed in the window. */
14822 int y0, y_to_move;
14823
14824 /* Compute the vertical distance from PT to the scroll
14825 margin position. Move as far as scroll_max allows, or
14826 one screenful, or 10 screen lines, whichever is largest.
14827 Give up if distance is greater than scroll_max or if we
14828 didn't reach the scroll margin position. */
14829 SET_TEXT_POS (pos, PT, PT_BYTE);
14830 start_display (&it, w, pos);
14831 y0 = it.current_y;
14832 y_to_move = max (it.last_visible_y,
14833 max (scroll_max, 10 * frame_line_height));
14834 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14835 y_to_move, -1,
14836 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14837 dy = it.current_y - y0;
14838 if (dy > scroll_max
14839 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14840 return SCROLLING_FAILED;
14841
14842 /* Additional scroll for when ZV was too close to point. */
14843 dy += y_offset;
14844
14845 /* Compute new window start. */
14846 start_display (&it, w, startp);
14847
14848 if (arg_scroll_conservatively)
14849 amount_to_scroll = max (dy, frame_line_height *
14850 max (scroll_step, temp_scroll_step));
14851 else if (scroll_step || temp_scroll_step)
14852 amount_to_scroll = scroll_max;
14853 else
14854 {
14855 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14856 height = WINDOW_BOX_TEXT_HEIGHT (w);
14857 if (NUMBERP (aggressive))
14858 {
14859 double float_amount = XFLOATINT (aggressive) * height;
14860 int aggressive_scroll = float_amount;
14861 if (aggressive_scroll == 0 && float_amount > 0)
14862 aggressive_scroll = 1;
14863 /* Don't let point enter the scroll margin near
14864 bottom of the window, if the value of
14865 scroll_down_aggressively happens to be too
14866 large. */
14867 if (aggressive_scroll + 2*this_scroll_margin > height)
14868 aggressive_scroll = height - 2*this_scroll_margin;
14869 amount_to_scroll = dy + aggressive_scroll;
14870 }
14871 }
14872
14873 if (amount_to_scroll <= 0)
14874 return SCROLLING_FAILED;
14875
14876 move_it_vertically_backward (&it, amount_to_scroll);
14877 startp = it.current.pos;
14878 }
14879 }
14880
14881 /* Run window scroll functions. */
14882 startp = run_window_scroll_functions (window, startp);
14883
14884 /* Display the window. Give up if new fonts are loaded, or if point
14885 doesn't appear. */
14886 if (!try_window (window, startp, 0))
14887 rc = SCROLLING_NEED_LARGER_MATRICES;
14888 else if (w->cursor.vpos < 0)
14889 {
14890 clear_glyph_matrix (w->desired_matrix);
14891 rc = SCROLLING_FAILED;
14892 }
14893 else
14894 {
14895 /* Maybe forget recorded base line for line number display. */
14896 if (!just_this_one_p
14897 || current_buffer->clip_changed
14898 || BEG_UNCHANGED < CHARPOS (startp))
14899 w->base_line_number = 0;
14900
14901 /* If cursor ends up on a partially visible line,
14902 treat that as being off the bottom of the screen. */
14903 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14904 /* It's possible that the cursor is on the first line of the
14905 buffer, which is partially obscured due to a vscroll
14906 (Bug#7537). In that case, avoid looping forever . */
14907 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14908 {
14909 clear_glyph_matrix (w->desired_matrix);
14910 ++extra_scroll_margin_lines;
14911 goto too_near_end;
14912 }
14913 rc = SCROLLING_SUCCESS;
14914 }
14915
14916 return rc;
14917 }
14918
14919
14920 /* Compute a suitable window start for window W if display of W starts
14921 on a continuation line. Value is non-zero if a new window start
14922 was computed.
14923
14924 The new window start will be computed, based on W's width, starting
14925 from the start of the continued line. It is the start of the
14926 screen line with the minimum distance from the old start W->start. */
14927
14928 static int
14929 compute_window_start_on_continuation_line (struct window *w)
14930 {
14931 struct text_pos pos, start_pos;
14932 int window_start_changed_p = 0;
14933
14934 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14935
14936 /* If window start is on a continuation line... Window start may be
14937 < BEGV in case there's invisible text at the start of the
14938 buffer (M-x rmail, for example). */
14939 if (CHARPOS (start_pos) > BEGV
14940 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14941 {
14942 struct it it;
14943 struct glyph_row *row;
14944
14945 /* Handle the case that the window start is out of range. */
14946 if (CHARPOS (start_pos) < BEGV)
14947 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14948 else if (CHARPOS (start_pos) > ZV)
14949 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14950
14951 /* Find the start of the continued line. This should be fast
14952 because find_newline is fast (newline cache). */
14953 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14954 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14955 row, DEFAULT_FACE_ID);
14956 reseat_at_previous_visible_line_start (&it);
14957
14958 /* If the line start is "too far" away from the window start,
14959 say it takes too much time to compute a new window start. */
14960 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14961 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14962 {
14963 int min_distance, distance;
14964
14965 /* Move forward by display lines to find the new window
14966 start. If window width was enlarged, the new start can
14967 be expected to be > the old start. If window width was
14968 decreased, the new window start will be < the old start.
14969 So, we're looking for the display line start with the
14970 minimum distance from the old window start. */
14971 pos = it.current.pos;
14972 min_distance = INFINITY;
14973 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14974 distance < min_distance)
14975 {
14976 min_distance = distance;
14977 pos = it.current.pos;
14978 move_it_by_lines (&it, 1);
14979 }
14980
14981 /* Set the window start there. */
14982 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14983 window_start_changed_p = 1;
14984 }
14985 }
14986
14987 return window_start_changed_p;
14988 }
14989
14990
14991 /* Try cursor movement in case text has not changed in window WINDOW,
14992 with window start STARTP. Value is
14993
14994 CURSOR_MOVEMENT_SUCCESS if successful
14995
14996 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14997
14998 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14999 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15000 we want to scroll as if scroll-step were set to 1. See the code.
15001
15002 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15003 which case we have to abort this redisplay, and adjust matrices
15004 first. */
15005
15006 enum
15007 {
15008 CURSOR_MOVEMENT_SUCCESS,
15009 CURSOR_MOVEMENT_CANNOT_BE_USED,
15010 CURSOR_MOVEMENT_MUST_SCROLL,
15011 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15012 };
15013
15014 static int
15015 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15016 {
15017 struct window *w = XWINDOW (window);
15018 struct frame *f = XFRAME (w->frame);
15019 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15020
15021 #ifdef GLYPH_DEBUG
15022 if (inhibit_try_cursor_movement)
15023 return rc;
15024 #endif
15025
15026 /* Previously, there was a check for Lisp integer in the
15027 if-statement below. Now, this field is converted to
15028 ptrdiff_t, thus zero means invalid position in a buffer. */
15029 eassert (w->last_point > 0);
15030 /* Likewise there was a check whether window_end_vpos is nil or larger
15031 than the window. Now window_end_vpos is int and so never nil, but
15032 let's leave eassert to check whether it fits in the window. */
15033 eassert (w->window_end_vpos < w->current_matrix->nrows);
15034
15035 /* Handle case where text has not changed, only point, and it has
15036 not moved off the frame. */
15037 if (/* Point may be in this window. */
15038 PT >= CHARPOS (startp)
15039 /* Selective display hasn't changed. */
15040 && !current_buffer->clip_changed
15041 /* Function force-mode-line-update is used to force a thorough
15042 redisplay. It sets either windows_or_buffers_changed or
15043 update_mode_lines. So don't take a shortcut here for these
15044 cases. */
15045 && !update_mode_lines
15046 && !windows_or_buffers_changed
15047 && !cursor_type_changed
15048 /* Can't use this case if highlighting a region. When a
15049 region exists, cursor movement has to do more than just
15050 set the cursor. */
15051 && markpos_of_region () < 0
15052 && !w->region_showing
15053 && NILP (Vshow_trailing_whitespace)
15054 /* This code is not used for mini-buffer for the sake of the case
15055 of redisplaying to replace an echo area message; since in
15056 that case the mini-buffer contents per se are usually
15057 unchanged. This code is of no real use in the mini-buffer
15058 since the handling of this_line_start_pos, etc., in redisplay
15059 handles the same cases. */
15060 && !EQ (window, minibuf_window)
15061 && (FRAME_WINDOW_P (f)
15062 || !overlay_arrow_in_current_buffer_p ()))
15063 {
15064 int this_scroll_margin, top_scroll_margin;
15065 struct glyph_row *row = NULL;
15066 int frame_line_height = default_line_pixel_height (w);
15067 int window_total_lines
15068 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15069
15070 #ifdef GLYPH_DEBUG
15071 debug_method_add (w, "cursor movement");
15072 #endif
15073
15074 /* Scroll if point within this distance from the top or bottom
15075 of the window. This is a pixel value. */
15076 if (scroll_margin > 0)
15077 {
15078 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15079 this_scroll_margin *= frame_line_height;
15080 }
15081 else
15082 this_scroll_margin = 0;
15083
15084 top_scroll_margin = this_scroll_margin;
15085 if (WINDOW_WANTS_HEADER_LINE_P (w))
15086 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15087
15088 /* Start with the row the cursor was displayed during the last
15089 not paused redisplay. Give up if that row is not valid. */
15090 if (w->last_cursor.vpos < 0
15091 || w->last_cursor.vpos >= w->current_matrix->nrows)
15092 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15093 else
15094 {
15095 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15096 if (row->mode_line_p)
15097 ++row;
15098 if (!row->enabled_p)
15099 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15100 }
15101
15102 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15103 {
15104 int scroll_p = 0, must_scroll = 0;
15105 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15106
15107 if (PT > w->last_point)
15108 {
15109 /* Point has moved forward. */
15110 while (MATRIX_ROW_END_CHARPOS (row) < PT
15111 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15112 {
15113 eassert (row->enabled_p);
15114 ++row;
15115 }
15116
15117 /* If the end position of a row equals the start
15118 position of the next row, and PT is at that position,
15119 we would rather display cursor in the next line. */
15120 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15121 && MATRIX_ROW_END_CHARPOS (row) == PT
15122 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15123 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15124 && !cursor_row_p (row))
15125 ++row;
15126
15127 /* If within the scroll margin, scroll. Note that
15128 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15129 the next line would be drawn, and that
15130 this_scroll_margin can be zero. */
15131 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15132 || PT > MATRIX_ROW_END_CHARPOS (row)
15133 /* Line is completely visible last line in window
15134 and PT is to be set in the next line. */
15135 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15136 && PT == MATRIX_ROW_END_CHARPOS (row)
15137 && !row->ends_at_zv_p
15138 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15139 scroll_p = 1;
15140 }
15141 else if (PT < w->last_point)
15142 {
15143 /* Cursor has to be moved backward. Note that PT >=
15144 CHARPOS (startp) because of the outer if-statement. */
15145 while (!row->mode_line_p
15146 && (MATRIX_ROW_START_CHARPOS (row) > PT
15147 || (MATRIX_ROW_START_CHARPOS (row) == PT
15148 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15149 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15150 row > w->current_matrix->rows
15151 && (row-1)->ends_in_newline_from_string_p))))
15152 && (row->y > top_scroll_margin
15153 || CHARPOS (startp) == BEGV))
15154 {
15155 eassert (row->enabled_p);
15156 --row;
15157 }
15158
15159 /* Consider the following case: Window starts at BEGV,
15160 there is invisible, intangible text at BEGV, so that
15161 display starts at some point START > BEGV. It can
15162 happen that we are called with PT somewhere between
15163 BEGV and START. Try to handle that case. */
15164 if (row < w->current_matrix->rows
15165 || row->mode_line_p)
15166 {
15167 row = w->current_matrix->rows;
15168 if (row->mode_line_p)
15169 ++row;
15170 }
15171
15172 /* Due to newlines in overlay strings, we may have to
15173 skip forward over overlay strings. */
15174 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15175 && MATRIX_ROW_END_CHARPOS (row) == PT
15176 && !cursor_row_p (row))
15177 ++row;
15178
15179 /* If within the scroll margin, scroll. */
15180 if (row->y < top_scroll_margin
15181 && CHARPOS (startp) != BEGV)
15182 scroll_p = 1;
15183 }
15184 else
15185 {
15186 /* Cursor did not move. So don't scroll even if cursor line
15187 is partially visible, as it was so before. */
15188 rc = CURSOR_MOVEMENT_SUCCESS;
15189 }
15190
15191 if (PT < MATRIX_ROW_START_CHARPOS (row)
15192 || PT > MATRIX_ROW_END_CHARPOS (row))
15193 {
15194 /* if PT is not in the glyph row, give up. */
15195 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15196 must_scroll = 1;
15197 }
15198 else if (rc != CURSOR_MOVEMENT_SUCCESS
15199 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15200 {
15201 struct glyph_row *row1;
15202
15203 /* If rows are bidi-reordered and point moved, back up
15204 until we find a row that does not belong to a
15205 continuation line. This is because we must consider
15206 all rows of a continued line as candidates for the
15207 new cursor positioning, since row start and end
15208 positions change non-linearly with vertical position
15209 in such rows. */
15210 /* FIXME: Revisit this when glyph ``spilling'' in
15211 continuation lines' rows is implemented for
15212 bidi-reordered rows. */
15213 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15214 MATRIX_ROW_CONTINUATION_LINE_P (row);
15215 --row)
15216 {
15217 /* If we hit the beginning of the displayed portion
15218 without finding the first row of a continued
15219 line, give up. */
15220 if (row <= row1)
15221 {
15222 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15223 break;
15224 }
15225 eassert (row->enabled_p);
15226 }
15227 }
15228 if (must_scroll)
15229 ;
15230 else if (rc != CURSOR_MOVEMENT_SUCCESS
15231 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15232 /* Make sure this isn't a header line by any chance, since
15233 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15234 && !row->mode_line_p
15235 && make_cursor_line_fully_visible_p)
15236 {
15237 if (PT == MATRIX_ROW_END_CHARPOS (row)
15238 && !row->ends_at_zv_p
15239 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15240 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15241 else if (row->height > window_box_height (w))
15242 {
15243 /* If we end up in a partially visible line, let's
15244 make it fully visible, except when it's taller
15245 than the window, in which case we can't do much
15246 about it. */
15247 *scroll_step = 1;
15248 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15249 }
15250 else
15251 {
15252 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15253 if (!cursor_row_fully_visible_p (w, 0, 1))
15254 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15255 else
15256 rc = CURSOR_MOVEMENT_SUCCESS;
15257 }
15258 }
15259 else if (scroll_p)
15260 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15261 else if (rc != CURSOR_MOVEMENT_SUCCESS
15262 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15263 {
15264 /* With bidi-reordered rows, there could be more than
15265 one candidate row whose start and end positions
15266 occlude point. We need to let set_cursor_from_row
15267 find the best candidate. */
15268 /* FIXME: Revisit this when glyph ``spilling'' in
15269 continuation lines' rows is implemented for
15270 bidi-reordered rows. */
15271 int rv = 0;
15272
15273 do
15274 {
15275 int at_zv_p = 0, exact_match_p = 0;
15276
15277 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15278 && PT <= MATRIX_ROW_END_CHARPOS (row)
15279 && cursor_row_p (row))
15280 rv |= set_cursor_from_row (w, row, w->current_matrix,
15281 0, 0, 0, 0);
15282 /* As soon as we've found the exact match for point,
15283 or the first suitable row whose ends_at_zv_p flag
15284 is set, we are done. */
15285 at_zv_p =
15286 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15287 if (rv && !at_zv_p
15288 && w->cursor.hpos >= 0
15289 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15290 w->cursor.vpos))
15291 {
15292 struct glyph_row *candidate =
15293 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15294 struct glyph *g =
15295 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15296 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15297
15298 exact_match_p =
15299 (BUFFERP (g->object) && g->charpos == PT)
15300 || (INTEGERP (g->object)
15301 && (g->charpos == PT
15302 || (g->charpos == 0 && endpos - 1 == PT)));
15303 }
15304 if (rv && (at_zv_p || exact_match_p))
15305 {
15306 rc = CURSOR_MOVEMENT_SUCCESS;
15307 break;
15308 }
15309 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15310 break;
15311 ++row;
15312 }
15313 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15314 || row->continued_p)
15315 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15316 || (MATRIX_ROW_START_CHARPOS (row) == PT
15317 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15318 /* If we didn't find any candidate rows, or exited the
15319 loop before all the candidates were examined, signal
15320 to the caller that this method failed. */
15321 if (rc != CURSOR_MOVEMENT_SUCCESS
15322 && !(rv
15323 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15324 && !row->continued_p))
15325 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15326 else if (rv)
15327 rc = CURSOR_MOVEMENT_SUCCESS;
15328 }
15329 else
15330 {
15331 do
15332 {
15333 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15334 {
15335 rc = CURSOR_MOVEMENT_SUCCESS;
15336 break;
15337 }
15338 ++row;
15339 }
15340 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15341 && MATRIX_ROW_START_CHARPOS (row) == PT
15342 && cursor_row_p (row));
15343 }
15344 }
15345 }
15346
15347 return rc;
15348 }
15349
15350 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15351 static
15352 #endif
15353 void
15354 set_vertical_scroll_bar (struct window *w)
15355 {
15356 ptrdiff_t start, end, whole;
15357
15358 /* Calculate the start and end positions for the current window.
15359 At some point, it would be nice to choose between scrollbars
15360 which reflect the whole buffer size, with special markers
15361 indicating narrowing, and scrollbars which reflect only the
15362 visible region.
15363
15364 Note that mini-buffers sometimes aren't displaying any text. */
15365 if (!MINI_WINDOW_P (w)
15366 || (w == XWINDOW (minibuf_window)
15367 && NILP (echo_area_buffer[0])))
15368 {
15369 struct buffer *buf = XBUFFER (w->contents);
15370 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15371 start = marker_position (w->start) - BUF_BEGV (buf);
15372 /* I don't think this is guaranteed to be right. For the
15373 moment, we'll pretend it is. */
15374 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15375
15376 if (end < start)
15377 end = start;
15378 if (whole < (end - start))
15379 whole = end - start;
15380 }
15381 else
15382 start = end = whole = 0;
15383
15384 /* Indicate what this scroll bar ought to be displaying now. */
15385 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15386 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15387 (w, end - start, whole, start);
15388 }
15389
15390
15391 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15392 selected_window is redisplayed.
15393
15394 We can return without actually redisplaying the window if
15395 fonts_changed_p. In that case, redisplay_internal will
15396 retry. */
15397
15398 static void
15399 redisplay_window (Lisp_Object window, int just_this_one_p)
15400 {
15401 struct window *w = XWINDOW (window);
15402 struct frame *f = XFRAME (w->frame);
15403 struct buffer *buffer = XBUFFER (w->contents);
15404 struct buffer *old = current_buffer;
15405 struct text_pos lpoint, opoint, startp;
15406 int update_mode_line;
15407 int tem;
15408 struct it it;
15409 /* Record it now because it's overwritten. */
15410 int current_matrix_up_to_date_p = 0;
15411 int used_current_matrix_p = 0;
15412 /* This is less strict than current_matrix_up_to_date_p.
15413 It indicates that the buffer contents and narrowing are unchanged. */
15414 int buffer_unchanged_p = 0;
15415 int temp_scroll_step = 0;
15416 ptrdiff_t count = SPECPDL_INDEX ();
15417 int rc;
15418 int centering_position = -1;
15419 int last_line_misfit = 0;
15420 ptrdiff_t beg_unchanged, end_unchanged;
15421 int frame_line_height;
15422
15423 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15424 opoint = lpoint;
15425
15426 #ifdef GLYPH_DEBUG
15427 *w->desired_matrix->method = 0;
15428 #endif
15429
15430 /* Make sure that both W's markers are valid. */
15431 eassert (XMARKER (w->start)->buffer == buffer);
15432 eassert (XMARKER (w->pointm)->buffer == buffer);
15433
15434 restart:
15435 reconsider_clip_changes (w);
15436 frame_line_height = default_line_pixel_height (w);
15437
15438 /* Has the mode line to be updated? */
15439 update_mode_line = (w->update_mode_line
15440 || update_mode_lines
15441 || buffer->clip_changed
15442 || buffer->prevent_redisplay_optimizations_p);
15443
15444 if (MINI_WINDOW_P (w))
15445 {
15446 if (w == XWINDOW (echo_area_window)
15447 && !NILP (echo_area_buffer[0]))
15448 {
15449 if (update_mode_line)
15450 /* We may have to update a tty frame's menu bar or a
15451 tool-bar. Example `M-x C-h C-h C-g'. */
15452 goto finish_menu_bars;
15453 else
15454 /* We've already displayed the echo area glyphs in this window. */
15455 goto finish_scroll_bars;
15456 }
15457 else if ((w != XWINDOW (minibuf_window)
15458 || minibuf_level == 0)
15459 /* When buffer is nonempty, redisplay window normally. */
15460 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15461 /* Quail displays non-mini buffers in minibuffer window.
15462 In that case, redisplay the window normally. */
15463 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15464 {
15465 /* W is a mini-buffer window, but it's not active, so clear
15466 it. */
15467 int yb = window_text_bottom_y (w);
15468 struct glyph_row *row;
15469 int y;
15470
15471 for (y = 0, row = w->desired_matrix->rows;
15472 y < yb;
15473 y += row->height, ++row)
15474 blank_row (w, row, y);
15475 goto finish_scroll_bars;
15476 }
15477
15478 clear_glyph_matrix (w->desired_matrix);
15479 }
15480
15481 /* Otherwise set up data on this window; select its buffer and point
15482 value. */
15483 /* Really select the buffer, for the sake of buffer-local
15484 variables. */
15485 set_buffer_internal_1 (XBUFFER (w->contents));
15486
15487 current_matrix_up_to_date_p
15488 = (w->window_end_valid
15489 && !current_buffer->clip_changed
15490 && !current_buffer->prevent_redisplay_optimizations_p
15491 && !window_outdated (w));
15492
15493 /* Run the window-bottom-change-functions
15494 if it is possible that the text on the screen has changed
15495 (either due to modification of the text, or any other reason). */
15496 if (!current_matrix_up_to_date_p
15497 && !NILP (Vwindow_text_change_functions))
15498 {
15499 safe_run_hooks (Qwindow_text_change_functions);
15500 goto restart;
15501 }
15502
15503 beg_unchanged = BEG_UNCHANGED;
15504 end_unchanged = END_UNCHANGED;
15505
15506 SET_TEXT_POS (opoint, PT, PT_BYTE);
15507
15508 specbind (Qinhibit_point_motion_hooks, Qt);
15509
15510 buffer_unchanged_p
15511 = (w->window_end_valid
15512 && !current_buffer->clip_changed
15513 && !window_outdated (w));
15514
15515 /* When windows_or_buffers_changed is non-zero, we can't rely on
15516 the window end being valid, so set it to nil there. */
15517 if (windows_or_buffers_changed)
15518 {
15519 /* If window starts on a continuation line, maybe adjust the
15520 window start in case the window's width changed. */
15521 if (XMARKER (w->start)->buffer == current_buffer)
15522 compute_window_start_on_continuation_line (w);
15523
15524 w->window_end_valid = 0;
15525 }
15526
15527 /* Some sanity checks. */
15528 CHECK_WINDOW_END (w);
15529 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15530 emacs_abort ();
15531 if (BYTEPOS (opoint) < CHARPOS (opoint))
15532 emacs_abort ();
15533
15534 if (mode_line_update_needed (w))
15535 update_mode_line = 1;
15536
15537 /* Point refers normally to the selected window. For any other
15538 window, set up appropriate value. */
15539 if (!EQ (window, selected_window))
15540 {
15541 ptrdiff_t new_pt = marker_position (w->pointm);
15542 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15543 if (new_pt < BEGV)
15544 {
15545 new_pt = BEGV;
15546 new_pt_byte = BEGV_BYTE;
15547 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15548 }
15549 else if (new_pt > (ZV - 1))
15550 {
15551 new_pt = ZV;
15552 new_pt_byte = ZV_BYTE;
15553 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15554 }
15555
15556 /* We don't use SET_PT so that the point-motion hooks don't run. */
15557 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15558 }
15559
15560 /* If any of the character widths specified in the display table
15561 have changed, invalidate the width run cache. It's true that
15562 this may be a bit late to catch such changes, but the rest of
15563 redisplay goes (non-fatally) haywire when the display table is
15564 changed, so why should we worry about doing any better? */
15565 if (current_buffer->width_run_cache)
15566 {
15567 struct Lisp_Char_Table *disptab = buffer_display_table ();
15568
15569 if (! disptab_matches_widthtab
15570 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15571 {
15572 invalidate_region_cache (current_buffer,
15573 current_buffer->width_run_cache,
15574 BEG, Z);
15575 recompute_width_table (current_buffer, disptab);
15576 }
15577 }
15578
15579 /* If window-start is screwed up, choose a new one. */
15580 if (XMARKER (w->start)->buffer != current_buffer)
15581 goto recenter;
15582
15583 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15584
15585 /* If someone specified a new starting point but did not insist,
15586 check whether it can be used. */
15587 if (w->optional_new_start
15588 && CHARPOS (startp) >= BEGV
15589 && CHARPOS (startp) <= ZV)
15590 {
15591 w->optional_new_start = 0;
15592 start_display (&it, w, startp);
15593 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15594 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15595 if (IT_CHARPOS (it) == PT)
15596 w->force_start = 1;
15597 /* IT may overshoot PT if text at PT is invisible. */
15598 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15599 w->force_start = 1;
15600 }
15601
15602 force_start:
15603
15604 /* Handle case where place to start displaying has been specified,
15605 unless the specified location is outside the accessible range. */
15606 if (w->force_start || window_frozen_p (w))
15607 {
15608 /* We set this later on if we have to adjust point. */
15609 int new_vpos = -1;
15610
15611 w->force_start = 0;
15612 w->vscroll = 0;
15613 w->window_end_valid = 0;
15614
15615 /* Forget any recorded base line for line number display. */
15616 if (!buffer_unchanged_p)
15617 w->base_line_number = 0;
15618
15619 /* Redisplay the mode line. Select the buffer properly for that.
15620 Also, run the hook window-scroll-functions
15621 because we have scrolled. */
15622 /* Note, we do this after clearing force_start because
15623 if there's an error, it is better to forget about force_start
15624 than to get into an infinite loop calling the hook functions
15625 and having them get more errors. */
15626 if (!update_mode_line
15627 || ! NILP (Vwindow_scroll_functions))
15628 {
15629 update_mode_line = 1;
15630 w->update_mode_line = 1;
15631 startp = run_window_scroll_functions (window, startp);
15632 }
15633
15634 if (CHARPOS (startp) < BEGV)
15635 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15636 else if (CHARPOS (startp) > ZV)
15637 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15638
15639 /* Redisplay, then check if cursor has been set during the
15640 redisplay. Give up if new fonts were loaded. */
15641 /* We used to issue a CHECK_MARGINS argument to try_window here,
15642 but this causes scrolling to fail when point begins inside
15643 the scroll margin (bug#148) -- cyd */
15644 if (!try_window (window, startp, 0))
15645 {
15646 w->force_start = 1;
15647 clear_glyph_matrix (w->desired_matrix);
15648 goto need_larger_matrices;
15649 }
15650
15651 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15652 {
15653 /* If point does not appear, try to move point so it does
15654 appear. The desired matrix has been built above, so we
15655 can use it here. */
15656 new_vpos = window_box_height (w) / 2;
15657 }
15658
15659 if (!cursor_row_fully_visible_p (w, 0, 0))
15660 {
15661 /* Point does appear, but on a line partly visible at end of window.
15662 Move it back to a fully-visible line. */
15663 new_vpos = window_box_height (w);
15664 }
15665 else if (w->cursor.vpos >=0)
15666 {
15667 /* Some people insist on not letting point enter the scroll
15668 margin, even though this part handles windows that didn't
15669 scroll at all. */
15670 int window_total_lines
15671 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15672 int margin = min (scroll_margin, window_total_lines / 4);
15673 int pixel_margin = margin * frame_line_height;
15674 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15675
15676 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15677 below, which finds the row to move point to, advances by
15678 the Y coordinate of the _next_ row, see the definition of
15679 MATRIX_ROW_BOTTOM_Y. */
15680 if (w->cursor.vpos < margin + header_line)
15681 {
15682 w->cursor.vpos = -1;
15683 clear_glyph_matrix (w->desired_matrix);
15684 goto try_to_scroll;
15685 }
15686 else
15687 {
15688 int window_height = window_box_height (w);
15689
15690 if (header_line)
15691 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15692 if (w->cursor.y >= window_height - pixel_margin)
15693 {
15694 w->cursor.vpos = -1;
15695 clear_glyph_matrix (w->desired_matrix);
15696 goto try_to_scroll;
15697 }
15698 }
15699 }
15700
15701 /* If we need to move point for either of the above reasons,
15702 now actually do it. */
15703 if (new_vpos >= 0)
15704 {
15705 struct glyph_row *row;
15706
15707 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15708 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15709 ++row;
15710
15711 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15712 MATRIX_ROW_START_BYTEPOS (row));
15713
15714 if (w != XWINDOW (selected_window))
15715 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15716 else if (current_buffer == old)
15717 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15718
15719 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15720
15721 /* If we are highlighting the region, then we just changed
15722 the region, so redisplay to show it. */
15723 if (markpos_of_region () >= 0)
15724 {
15725 clear_glyph_matrix (w->desired_matrix);
15726 if (!try_window (window, startp, 0))
15727 goto need_larger_matrices;
15728 }
15729 }
15730
15731 #ifdef GLYPH_DEBUG
15732 debug_method_add (w, "forced window start");
15733 #endif
15734 goto done;
15735 }
15736
15737 /* Handle case where text has not changed, only point, and it has
15738 not moved off the frame, and we are not retrying after hscroll.
15739 (current_matrix_up_to_date_p is nonzero when retrying.) */
15740 if (current_matrix_up_to_date_p
15741 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15742 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15743 {
15744 switch (rc)
15745 {
15746 case CURSOR_MOVEMENT_SUCCESS:
15747 used_current_matrix_p = 1;
15748 goto done;
15749
15750 case CURSOR_MOVEMENT_MUST_SCROLL:
15751 goto try_to_scroll;
15752
15753 default:
15754 emacs_abort ();
15755 }
15756 }
15757 /* If current starting point was originally the beginning of a line
15758 but no longer is, find a new starting point. */
15759 else if (w->start_at_line_beg
15760 && !(CHARPOS (startp) <= BEGV
15761 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15762 {
15763 #ifdef GLYPH_DEBUG
15764 debug_method_add (w, "recenter 1");
15765 #endif
15766 goto recenter;
15767 }
15768
15769 /* Try scrolling with try_window_id. Value is > 0 if update has
15770 been done, it is -1 if we know that the same window start will
15771 not work. It is 0 if unsuccessful for some other reason. */
15772 else if ((tem = try_window_id (w)) != 0)
15773 {
15774 #ifdef GLYPH_DEBUG
15775 debug_method_add (w, "try_window_id %d", tem);
15776 #endif
15777
15778 if (fonts_changed_p)
15779 goto need_larger_matrices;
15780 if (tem > 0)
15781 goto done;
15782
15783 /* Otherwise try_window_id has returned -1 which means that we
15784 don't want the alternative below this comment to execute. */
15785 }
15786 else if (CHARPOS (startp) >= BEGV
15787 && CHARPOS (startp) <= ZV
15788 && PT >= CHARPOS (startp)
15789 && (CHARPOS (startp) < ZV
15790 /* Avoid starting at end of buffer. */
15791 || CHARPOS (startp) == BEGV
15792 || !window_outdated (w)))
15793 {
15794 int d1, d2, d3, d4, d5, d6;
15795
15796 /* If first window line is a continuation line, and window start
15797 is inside the modified region, but the first change is before
15798 current window start, we must select a new window start.
15799
15800 However, if this is the result of a down-mouse event (e.g. by
15801 extending the mouse-drag-overlay), we don't want to select a
15802 new window start, since that would change the position under
15803 the mouse, resulting in an unwanted mouse-movement rather
15804 than a simple mouse-click. */
15805 if (!w->start_at_line_beg
15806 && NILP (do_mouse_tracking)
15807 && CHARPOS (startp) > BEGV
15808 && CHARPOS (startp) > BEG + beg_unchanged
15809 && CHARPOS (startp) <= Z - end_unchanged
15810 /* Even if w->start_at_line_beg is nil, a new window may
15811 start at a line_beg, since that's how set_buffer_window
15812 sets it. So, we need to check the return value of
15813 compute_window_start_on_continuation_line. (See also
15814 bug#197). */
15815 && XMARKER (w->start)->buffer == current_buffer
15816 && compute_window_start_on_continuation_line (w)
15817 /* It doesn't make sense to force the window start like we
15818 do at label force_start if it is already known that point
15819 will not be visible in the resulting window, because
15820 doing so will move point from its correct position
15821 instead of scrolling the window to bring point into view.
15822 See bug#9324. */
15823 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15824 {
15825 w->force_start = 1;
15826 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15827 goto force_start;
15828 }
15829
15830 #ifdef GLYPH_DEBUG
15831 debug_method_add (w, "same window start");
15832 #endif
15833
15834 /* Try to redisplay starting at same place as before.
15835 If point has not moved off frame, accept the results. */
15836 if (!current_matrix_up_to_date_p
15837 /* Don't use try_window_reusing_current_matrix in this case
15838 because a window scroll function can have changed the
15839 buffer. */
15840 || !NILP (Vwindow_scroll_functions)
15841 || MINI_WINDOW_P (w)
15842 || !(used_current_matrix_p
15843 = try_window_reusing_current_matrix (w)))
15844 {
15845 IF_DEBUG (debug_method_add (w, "1"));
15846 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15847 /* -1 means we need to scroll.
15848 0 means we need new matrices, but fonts_changed_p
15849 is set in that case, so we will detect it below. */
15850 goto try_to_scroll;
15851 }
15852
15853 if (fonts_changed_p)
15854 goto need_larger_matrices;
15855
15856 if (w->cursor.vpos >= 0)
15857 {
15858 if (!just_this_one_p
15859 || current_buffer->clip_changed
15860 || BEG_UNCHANGED < CHARPOS (startp))
15861 /* Forget any recorded base line for line number display. */
15862 w->base_line_number = 0;
15863
15864 if (!cursor_row_fully_visible_p (w, 1, 0))
15865 {
15866 clear_glyph_matrix (w->desired_matrix);
15867 last_line_misfit = 1;
15868 }
15869 /* Drop through and scroll. */
15870 else
15871 goto done;
15872 }
15873 else
15874 clear_glyph_matrix (w->desired_matrix);
15875 }
15876
15877 try_to_scroll:
15878
15879 /* Redisplay the mode line. Select the buffer properly for that. */
15880 if (!update_mode_line)
15881 {
15882 update_mode_line = 1;
15883 w->update_mode_line = 1;
15884 }
15885
15886 /* Try to scroll by specified few lines. */
15887 if ((scroll_conservatively
15888 || emacs_scroll_step
15889 || temp_scroll_step
15890 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15891 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15892 && CHARPOS (startp) >= BEGV
15893 && CHARPOS (startp) <= ZV)
15894 {
15895 /* The function returns -1 if new fonts were loaded, 1 if
15896 successful, 0 if not successful. */
15897 int ss = try_scrolling (window, just_this_one_p,
15898 scroll_conservatively,
15899 emacs_scroll_step,
15900 temp_scroll_step, last_line_misfit);
15901 switch (ss)
15902 {
15903 case SCROLLING_SUCCESS:
15904 goto done;
15905
15906 case SCROLLING_NEED_LARGER_MATRICES:
15907 goto need_larger_matrices;
15908
15909 case SCROLLING_FAILED:
15910 break;
15911
15912 default:
15913 emacs_abort ();
15914 }
15915 }
15916
15917 /* Finally, just choose a place to start which positions point
15918 according to user preferences. */
15919
15920 recenter:
15921
15922 #ifdef GLYPH_DEBUG
15923 debug_method_add (w, "recenter");
15924 #endif
15925
15926 /* Forget any previously recorded base line for line number display. */
15927 if (!buffer_unchanged_p)
15928 w->base_line_number = 0;
15929
15930 /* Determine the window start relative to point. */
15931 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15932 it.current_y = it.last_visible_y;
15933 if (centering_position < 0)
15934 {
15935 int window_total_lines
15936 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15937 int margin =
15938 scroll_margin > 0
15939 ? min (scroll_margin, window_total_lines / 4)
15940 : 0;
15941 ptrdiff_t margin_pos = CHARPOS (startp);
15942 Lisp_Object aggressive;
15943 int scrolling_up;
15944
15945 /* If there is a scroll margin at the top of the window, find
15946 its character position. */
15947 if (margin
15948 /* Cannot call start_display if startp is not in the
15949 accessible region of the buffer. This can happen when we
15950 have just switched to a different buffer and/or changed
15951 its restriction. In that case, startp is initialized to
15952 the character position 1 (BEGV) because we did not yet
15953 have chance to display the buffer even once. */
15954 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15955 {
15956 struct it it1;
15957 void *it1data = NULL;
15958
15959 SAVE_IT (it1, it, it1data);
15960 start_display (&it1, w, startp);
15961 move_it_vertically (&it1, margin * frame_line_height);
15962 margin_pos = IT_CHARPOS (it1);
15963 RESTORE_IT (&it, &it, it1data);
15964 }
15965 scrolling_up = PT > margin_pos;
15966 aggressive =
15967 scrolling_up
15968 ? BVAR (current_buffer, scroll_up_aggressively)
15969 : BVAR (current_buffer, scroll_down_aggressively);
15970
15971 if (!MINI_WINDOW_P (w)
15972 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15973 {
15974 int pt_offset = 0;
15975
15976 /* Setting scroll-conservatively overrides
15977 scroll-*-aggressively. */
15978 if (!scroll_conservatively && NUMBERP (aggressive))
15979 {
15980 double float_amount = XFLOATINT (aggressive);
15981
15982 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15983 if (pt_offset == 0 && float_amount > 0)
15984 pt_offset = 1;
15985 if (pt_offset && margin > 0)
15986 margin -= 1;
15987 }
15988 /* Compute how much to move the window start backward from
15989 point so that point will be displayed where the user
15990 wants it. */
15991 if (scrolling_up)
15992 {
15993 centering_position = it.last_visible_y;
15994 if (pt_offset)
15995 centering_position -= pt_offset;
15996 centering_position -=
15997 frame_line_height * (1 + margin + (last_line_misfit != 0))
15998 + WINDOW_HEADER_LINE_HEIGHT (w);
15999 /* Don't let point enter the scroll margin near top of
16000 the window. */
16001 if (centering_position < margin * frame_line_height)
16002 centering_position = margin * frame_line_height;
16003 }
16004 else
16005 centering_position = margin * frame_line_height + pt_offset;
16006 }
16007 else
16008 /* Set the window start half the height of the window backward
16009 from point. */
16010 centering_position = window_box_height (w) / 2;
16011 }
16012 move_it_vertically_backward (&it, centering_position);
16013
16014 eassert (IT_CHARPOS (it) >= BEGV);
16015
16016 /* The function move_it_vertically_backward may move over more
16017 than the specified y-distance. If it->w is small, e.g. a
16018 mini-buffer window, we may end up in front of the window's
16019 display area. Start displaying at the start of the line
16020 containing PT in this case. */
16021 if (it.current_y <= 0)
16022 {
16023 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16024 move_it_vertically_backward (&it, 0);
16025 it.current_y = 0;
16026 }
16027
16028 it.current_x = it.hpos = 0;
16029
16030 /* Set the window start position here explicitly, to avoid an
16031 infinite loop in case the functions in window-scroll-functions
16032 get errors. */
16033 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16034
16035 /* Run scroll hooks. */
16036 startp = run_window_scroll_functions (window, it.current.pos);
16037
16038 /* Redisplay the window. */
16039 if (!current_matrix_up_to_date_p
16040 || windows_or_buffers_changed
16041 || cursor_type_changed
16042 /* Don't use try_window_reusing_current_matrix in this case
16043 because it can have changed the buffer. */
16044 || !NILP (Vwindow_scroll_functions)
16045 || !just_this_one_p
16046 || MINI_WINDOW_P (w)
16047 || !(used_current_matrix_p
16048 = try_window_reusing_current_matrix (w)))
16049 try_window (window, startp, 0);
16050
16051 /* If new fonts have been loaded (due to fontsets), give up. We
16052 have to start a new redisplay since we need to re-adjust glyph
16053 matrices. */
16054 if (fonts_changed_p)
16055 goto need_larger_matrices;
16056
16057 /* If cursor did not appear assume that the middle of the window is
16058 in the first line of the window. Do it again with the next line.
16059 (Imagine a window of height 100, displaying two lines of height
16060 60. Moving back 50 from it->last_visible_y will end in the first
16061 line.) */
16062 if (w->cursor.vpos < 0)
16063 {
16064 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16065 {
16066 clear_glyph_matrix (w->desired_matrix);
16067 move_it_by_lines (&it, 1);
16068 try_window (window, it.current.pos, 0);
16069 }
16070 else if (PT < IT_CHARPOS (it))
16071 {
16072 clear_glyph_matrix (w->desired_matrix);
16073 move_it_by_lines (&it, -1);
16074 try_window (window, it.current.pos, 0);
16075 }
16076 else
16077 {
16078 /* Not much we can do about it. */
16079 }
16080 }
16081
16082 /* Consider the following case: Window starts at BEGV, there is
16083 invisible, intangible text at BEGV, so that display starts at
16084 some point START > BEGV. It can happen that we are called with
16085 PT somewhere between BEGV and START. Try to handle that case. */
16086 if (w->cursor.vpos < 0)
16087 {
16088 struct glyph_row *row = w->current_matrix->rows;
16089 if (row->mode_line_p)
16090 ++row;
16091 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16092 }
16093
16094 if (!cursor_row_fully_visible_p (w, 0, 0))
16095 {
16096 /* If vscroll is enabled, disable it and try again. */
16097 if (w->vscroll)
16098 {
16099 w->vscroll = 0;
16100 clear_glyph_matrix (w->desired_matrix);
16101 goto recenter;
16102 }
16103
16104 /* Users who set scroll-conservatively to a large number want
16105 point just above/below the scroll margin. If we ended up
16106 with point's row partially visible, move the window start to
16107 make that row fully visible and out of the margin. */
16108 if (scroll_conservatively > SCROLL_LIMIT)
16109 {
16110 int window_total_lines
16111 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16112 int margin =
16113 scroll_margin > 0
16114 ? min (scroll_margin, window_total_lines / 4)
16115 : 0;
16116 int move_down = w->cursor.vpos >= window_total_lines / 2;
16117
16118 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16119 clear_glyph_matrix (w->desired_matrix);
16120 if (1 == try_window (window, it.current.pos,
16121 TRY_WINDOW_CHECK_MARGINS))
16122 goto done;
16123 }
16124
16125 /* If centering point failed to make the whole line visible,
16126 put point at the top instead. That has to make the whole line
16127 visible, if it can be done. */
16128 if (centering_position == 0)
16129 goto done;
16130
16131 clear_glyph_matrix (w->desired_matrix);
16132 centering_position = 0;
16133 goto recenter;
16134 }
16135
16136 done:
16137
16138 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16139 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16140 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16141
16142 /* Display the mode line, if we must. */
16143 if ((update_mode_line
16144 /* If window not full width, must redo its mode line
16145 if (a) the window to its side is being redone and
16146 (b) we do a frame-based redisplay. This is a consequence
16147 of how inverted lines are drawn in frame-based redisplay. */
16148 || (!just_this_one_p
16149 && !FRAME_WINDOW_P (f)
16150 && !WINDOW_FULL_WIDTH_P (w))
16151 /* Line number to display. */
16152 || w->base_line_pos > 0
16153 /* Column number is displayed and different from the one displayed. */
16154 || (w->column_number_displayed != -1
16155 && (w->column_number_displayed != current_column ())))
16156 /* This means that the window has a mode line. */
16157 && (WINDOW_WANTS_MODELINE_P (w)
16158 || WINDOW_WANTS_HEADER_LINE_P (w)))
16159 {
16160 display_mode_lines (w);
16161
16162 /* If mode line height has changed, arrange for a thorough
16163 immediate redisplay using the correct mode line height. */
16164 if (WINDOW_WANTS_MODELINE_P (w)
16165 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16166 {
16167 fonts_changed_p = 1;
16168 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16169 = DESIRED_MODE_LINE_HEIGHT (w);
16170 }
16171
16172 /* If header line height has changed, arrange for a thorough
16173 immediate redisplay using the correct header line height. */
16174 if (WINDOW_WANTS_HEADER_LINE_P (w)
16175 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16176 {
16177 fonts_changed_p = 1;
16178 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16179 = DESIRED_HEADER_LINE_HEIGHT (w);
16180 }
16181
16182 if (fonts_changed_p)
16183 goto need_larger_matrices;
16184 }
16185
16186 if (!line_number_displayed && w->base_line_pos != -1)
16187 {
16188 w->base_line_pos = 0;
16189 w->base_line_number = 0;
16190 }
16191
16192 finish_menu_bars:
16193
16194 /* When we reach a frame's selected window, redo the frame's menu bar. */
16195 if (update_mode_line
16196 && EQ (FRAME_SELECTED_WINDOW (f), window))
16197 {
16198 int redisplay_menu_p = 0;
16199
16200 if (FRAME_WINDOW_P (f))
16201 {
16202 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16203 || defined (HAVE_NS) || defined (USE_GTK)
16204 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16205 #else
16206 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16207 #endif
16208 }
16209 else
16210 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16211
16212 if (redisplay_menu_p)
16213 display_menu_bar (w);
16214
16215 #ifdef HAVE_WINDOW_SYSTEM
16216 if (FRAME_WINDOW_P (f))
16217 {
16218 #if defined (USE_GTK) || defined (HAVE_NS)
16219 if (FRAME_EXTERNAL_TOOL_BAR (f))
16220 redisplay_tool_bar (f);
16221 #else
16222 if (WINDOWP (f->tool_bar_window)
16223 && (FRAME_TOOL_BAR_LINES (f) > 0
16224 || !NILP (Vauto_resize_tool_bars))
16225 && redisplay_tool_bar (f))
16226 ignore_mouse_drag_p = 1;
16227 #endif
16228 }
16229 #endif
16230 }
16231
16232 #ifdef HAVE_WINDOW_SYSTEM
16233 if (FRAME_WINDOW_P (f)
16234 && update_window_fringes (w, (just_this_one_p
16235 || (!used_current_matrix_p && !overlay_arrow_seen)
16236 || w->pseudo_window_p)))
16237 {
16238 update_begin (f);
16239 block_input ();
16240 if (draw_window_fringes (w, 1))
16241 x_draw_vertical_border (w);
16242 unblock_input ();
16243 update_end (f);
16244 }
16245 #endif /* HAVE_WINDOW_SYSTEM */
16246
16247 /* We go to this label, with fonts_changed_p set,
16248 if it is necessary to try again using larger glyph matrices.
16249 We have to redeem the scroll bar even in this case,
16250 because the loop in redisplay_internal expects that. */
16251 need_larger_matrices:
16252 ;
16253 finish_scroll_bars:
16254
16255 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16256 {
16257 /* Set the thumb's position and size. */
16258 set_vertical_scroll_bar (w);
16259
16260 /* Note that we actually used the scroll bar attached to this
16261 window, so it shouldn't be deleted at the end of redisplay. */
16262 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16263 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16264 }
16265
16266 /* Restore current_buffer and value of point in it. The window
16267 update may have changed the buffer, so first make sure `opoint'
16268 is still valid (Bug#6177). */
16269 if (CHARPOS (opoint) < BEGV)
16270 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16271 else if (CHARPOS (opoint) > ZV)
16272 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16273 else
16274 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16275
16276 set_buffer_internal_1 (old);
16277 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16278 shorter. This can be caused by log truncation in *Messages*. */
16279 if (CHARPOS (lpoint) <= ZV)
16280 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16281
16282 unbind_to (count, Qnil);
16283 }
16284
16285
16286 /* Build the complete desired matrix of WINDOW with a window start
16287 buffer position POS.
16288
16289 Value is 1 if successful. It is zero if fonts were loaded during
16290 redisplay which makes re-adjusting glyph matrices necessary, and -1
16291 if point would appear in the scroll margins.
16292 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16293 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16294 set in FLAGS.) */
16295
16296 int
16297 try_window (Lisp_Object window, struct text_pos pos, int flags)
16298 {
16299 struct window *w = XWINDOW (window);
16300 struct it it;
16301 struct glyph_row *last_text_row = NULL;
16302 struct frame *f = XFRAME (w->frame);
16303 int frame_line_height = default_line_pixel_height (w);
16304
16305 /* Make POS the new window start. */
16306 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16307
16308 /* Mark cursor position as unknown. No overlay arrow seen. */
16309 w->cursor.vpos = -1;
16310 overlay_arrow_seen = 0;
16311
16312 /* Initialize iterator and info to start at POS. */
16313 start_display (&it, w, pos);
16314
16315
16316
16317 /* Display all lines of W. */
16318 while (it.current_y < it.last_visible_y)
16319 {
16320 if (display_line (&it))
16321 last_text_row = it.glyph_row - 1;
16322 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16323 return 0;
16324 }
16325 #ifdef HAVE_XWIDGETS_xxx
16326 //currently this is needed to detect xwidget movement reliably. or probably not.
16327 printf("try_window\n");
16328 return 0;
16329 #endif
16330
16331 /* Don't let the cursor end in the scroll margins. */
16332 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16333 && !MINI_WINDOW_P (w))
16334 {
16335 int this_scroll_margin;
16336 int window_total_lines
16337 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16338
16339 if (scroll_margin > 0)
16340 {
16341 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16342 this_scroll_margin *= frame_line_height;
16343 }
16344 else
16345 this_scroll_margin = 0;
16346
16347 if ((w->cursor.y >= 0 /* not vscrolled */
16348 && w->cursor.y < this_scroll_margin
16349 && CHARPOS (pos) > BEGV
16350 && IT_CHARPOS (it) < ZV)
16351 /* rms: considering make_cursor_line_fully_visible_p here
16352 seems to give wrong results. We don't want to recenter
16353 when the last line is partly visible, we want to allow
16354 that case to be handled in the usual way. */
16355 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16356 {
16357 w->cursor.vpos = -1;
16358 clear_glyph_matrix (w->desired_matrix);
16359 return -1;
16360 }
16361 }
16362
16363 /* If bottom moved off end of frame, change mode line percentage. */
16364 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16365 w->update_mode_line = 1;
16366
16367 /* Set window_end_pos to the offset of the last character displayed
16368 on the window from the end of current_buffer. Set
16369 window_end_vpos to its row number. */
16370 if (last_text_row)
16371 {
16372 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16373 adjust_window_ends (w, last_text_row, 0);
16374 eassert
16375 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16376 w->window_end_vpos)));
16377 }
16378 else
16379 {
16380 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16381 w->window_end_pos = Z - ZV;
16382 w->window_end_vpos = 0;
16383 }
16384
16385 /* But that is not valid info until redisplay finishes. */
16386 w->window_end_valid = 0;
16387 return 1;
16388 }
16389
16390
16391 \f
16392 /************************************************************************
16393 Window redisplay reusing current matrix when buffer has not changed
16394 ************************************************************************/
16395
16396 /* Try redisplay of window W showing an unchanged buffer with a
16397 different window start than the last time it was displayed by
16398 reusing its current matrix. Value is non-zero if successful.
16399 W->start is the new window start. */
16400
16401 static int
16402 try_window_reusing_current_matrix (struct window *w)
16403 {
16404 struct frame *f = XFRAME (w->frame);
16405 struct glyph_row *bottom_row;
16406 struct it it;
16407 struct run run;
16408 struct text_pos start, new_start;
16409 int nrows_scrolled, i;
16410 struct glyph_row *last_text_row;
16411 struct glyph_row *last_reused_text_row;
16412 struct glyph_row *start_row;
16413 int start_vpos, min_y, max_y;
16414
16415 #ifdef GLYPH_DEBUG
16416 if (inhibit_try_window_reusing)
16417 return 0;
16418 #endif
16419
16420 #ifdef HAVE_XWIDGETS_xxx
16421 //currently this is needed to detect xwidget movement reliably. or probably not.
16422 printf("try_window_reusing_current_matrix\n");
16423 return 0;
16424 #endif
16425
16426
16427 if (/* This function doesn't handle terminal frames. */
16428 !FRAME_WINDOW_P (f)
16429 /* Don't try to reuse the display if windows have been split
16430 or such. */
16431 || windows_or_buffers_changed
16432 || cursor_type_changed)
16433 return 0;
16434
16435 /* Can't do this if region may have changed. */
16436 if (markpos_of_region () >= 0
16437 || w->region_showing
16438 || !NILP (Vshow_trailing_whitespace))
16439 return 0;
16440
16441 /* If top-line visibility has changed, give up. */
16442 if (WINDOW_WANTS_HEADER_LINE_P (w)
16443 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16444 return 0;
16445
16446 /* Give up if old or new display is scrolled vertically. We could
16447 make this function handle this, but right now it doesn't. */
16448 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16449 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16450 return 0;
16451
16452 /* The variable new_start now holds the new window start. The old
16453 start `start' can be determined from the current matrix. */
16454 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16455 start = start_row->minpos;
16456 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16457
16458 /* Clear the desired matrix for the display below. */
16459 clear_glyph_matrix (w->desired_matrix);
16460
16461 if (CHARPOS (new_start) <= CHARPOS (start))
16462 {
16463 /* Don't use this method if the display starts with an ellipsis
16464 displayed for invisible text. It's not easy to handle that case
16465 below, and it's certainly not worth the effort since this is
16466 not a frequent case. */
16467 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16468 return 0;
16469
16470 IF_DEBUG (debug_method_add (w, "twu1"));
16471
16472 /* Display up to a row that can be reused. The variable
16473 last_text_row is set to the last row displayed that displays
16474 text. Note that it.vpos == 0 if or if not there is a
16475 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16476 start_display (&it, w, new_start);
16477 w->cursor.vpos = -1;
16478 last_text_row = last_reused_text_row = NULL;
16479
16480 while (it.current_y < it.last_visible_y
16481 && !fonts_changed_p)
16482 {
16483 /* If we have reached into the characters in the START row,
16484 that means the line boundaries have changed. So we
16485 can't start copying with the row START. Maybe it will
16486 work to start copying with the following row. */
16487 while (IT_CHARPOS (it) > CHARPOS (start))
16488 {
16489 /* Advance to the next row as the "start". */
16490 start_row++;
16491 start = start_row->minpos;
16492 /* If there are no more rows to try, or just one, give up. */
16493 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16494 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16495 || CHARPOS (start) == ZV)
16496 {
16497 clear_glyph_matrix (w->desired_matrix);
16498 return 0;
16499 }
16500
16501 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16502 }
16503 /* If we have reached alignment, we can copy the rest of the
16504 rows. */
16505 if (IT_CHARPOS (it) == CHARPOS (start)
16506 /* Don't accept "alignment" inside a display vector,
16507 since start_row could have started in the middle of
16508 that same display vector (thus their character
16509 positions match), and we have no way of telling if
16510 that is the case. */
16511 && it.current.dpvec_index < 0)
16512 break;
16513
16514 if (display_line (&it))
16515 last_text_row = it.glyph_row - 1;
16516
16517 }
16518
16519 /* A value of current_y < last_visible_y means that we stopped
16520 at the previous window start, which in turn means that we
16521 have at least one reusable row. */
16522 if (it.current_y < it.last_visible_y)
16523 {
16524 struct glyph_row *row;
16525
16526 /* IT.vpos always starts from 0; it counts text lines. */
16527 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16528
16529 /* Find PT if not already found in the lines displayed. */
16530 if (w->cursor.vpos < 0)
16531 {
16532 int dy = it.current_y - start_row->y;
16533
16534 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16535 row = row_containing_pos (w, PT, row, NULL, dy);
16536 if (row)
16537 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16538 dy, nrows_scrolled);
16539 else
16540 {
16541 clear_glyph_matrix (w->desired_matrix);
16542 return 0;
16543 }
16544 }
16545
16546 /* Scroll the display. Do it before the current matrix is
16547 changed. The problem here is that update has not yet
16548 run, i.e. part of the current matrix is not up to date.
16549 scroll_run_hook will clear the cursor, and use the
16550 current matrix to get the height of the row the cursor is
16551 in. */
16552 run.current_y = start_row->y;
16553 run.desired_y = it.current_y;
16554 run.height = it.last_visible_y - it.current_y;
16555
16556 if (run.height > 0 && run.current_y != run.desired_y)
16557 {
16558 update_begin (f);
16559 FRAME_RIF (f)->update_window_begin_hook (w);
16560 FRAME_RIF (f)->clear_window_mouse_face (w);
16561 FRAME_RIF (f)->scroll_run_hook (w, &run);
16562 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16563 update_end (f);
16564 }
16565
16566 /* Shift current matrix down by nrows_scrolled lines. */
16567 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16568 rotate_matrix (w->current_matrix,
16569 start_vpos,
16570 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16571 nrows_scrolled);
16572
16573 /* Disable lines that must be updated. */
16574 for (i = 0; i < nrows_scrolled; ++i)
16575 (start_row + i)->enabled_p = 0;
16576
16577 /* Re-compute Y positions. */
16578 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16579 max_y = it.last_visible_y;
16580 for (row = start_row + nrows_scrolled;
16581 row < bottom_row;
16582 ++row)
16583 {
16584 row->y = it.current_y;
16585 row->visible_height = row->height;
16586
16587 if (row->y < min_y)
16588 row->visible_height -= min_y - row->y;
16589 if (row->y + row->height > max_y)
16590 row->visible_height -= row->y + row->height - max_y;
16591 if (row->fringe_bitmap_periodic_p)
16592 row->redraw_fringe_bitmaps_p = 1;
16593
16594 it.current_y += row->height;
16595
16596 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16597 last_reused_text_row = row;
16598 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16599 break;
16600 }
16601
16602 /* Disable lines in the current matrix which are now
16603 below the window. */
16604 for (++row; row < bottom_row; ++row)
16605 row->enabled_p = row->mode_line_p = 0;
16606 }
16607
16608 /* Update window_end_pos etc.; last_reused_text_row is the last
16609 reused row from the current matrix containing text, if any.
16610 The value of last_text_row is the last displayed line
16611 containing text. */
16612 if (last_reused_text_row)
16613 adjust_window_ends (w, last_reused_text_row, 1);
16614 else if (last_text_row)
16615 adjust_window_ends (w, last_text_row, 0);
16616 else
16617 {
16618 /* This window must be completely empty. */
16619 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16620 w->window_end_pos = Z - ZV;
16621 w->window_end_vpos = 0;
16622 }
16623 w->window_end_valid = 0;
16624
16625 /* Update hint: don't try scrolling again in update_window. */
16626 w->desired_matrix->no_scrolling_p = 1;
16627
16628 #ifdef GLYPH_DEBUG
16629 debug_method_add (w, "try_window_reusing_current_matrix 1");
16630 #endif
16631 return 1;
16632 }
16633 else if (CHARPOS (new_start) > CHARPOS (start))
16634 {
16635 struct glyph_row *pt_row, *row;
16636 struct glyph_row *first_reusable_row;
16637 struct glyph_row *first_row_to_display;
16638 int dy;
16639 int yb = window_text_bottom_y (w);
16640
16641 /* Find the row starting at new_start, if there is one. Don't
16642 reuse a partially visible line at the end. */
16643 first_reusable_row = start_row;
16644 while (first_reusable_row->enabled_p
16645 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16646 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16647 < CHARPOS (new_start)))
16648 ++first_reusable_row;
16649
16650 /* Give up if there is no row to reuse. */
16651 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16652 || !first_reusable_row->enabled_p
16653 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16654 != CHARPOS (new_start)))
16655 return 0;
16656
16657 /* We can reuse fully visible rows beginning with
16658 first_reusable_row to the end of the window. Set
16659 first_row_to_display to the first row that cannot be reused.
16660 Set pt_row to the row containing point, if there is any. */
16661 pt_row = NULL;
16662 for (first_row_to_display = first_reusable_row;
16663 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16664 ++first_row_to_display)
16665 {
16666 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16667 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16668 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16669 && first_row_to_display->ends_at_zv_p
16670 && pt_row == NULL)))
16671 pt_row = first_row_to_display;
16672 }
16673
16674 /* Start displaying at the start of first_row_to_display. */
16675 eassert (first_row_to_display->y < yb);
16676 init_to_row_start (&it, w, first_row_to_display);
16677
16678 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16679 - start_vpos);
16680 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16681 - nrows_scrolled);
16682 it.current_y = (first_row_to_display->y - first_reusable_row->y
16683 + WINDOW_HEADER_LINE_HEIGHT (w));
16684
16685 /* Display lines beginning with first_row_to_display in the
16686 desired matrix. Set last_text_row to the last row displayed
16687 that displays text. */
16688 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16689 if (pt_row == NULL)
16690 w->cursor.vpos = -1;
16691 last_text_row = NULL;
16692 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16693 if (display_line (&it))
16694 last_text_row = it.glyph_row - 1;
16695
16696 /* If point is in a reused row, adjust y and vpos of the cursor
16697 position. */
16698 if (pt_row)
16699 {
16700 w->cursor.vpos -= nrows_scrolled;
16701 w->cursor.y -= first_reusable_row->y - start_row->y;
16702 }
16703
16704 /* Give up if point isn't in a row displayed or reused. (This
16705 also handles the case where w->cursor.vpos < nrows_scrolled
16706 after the calls to display_line, which can happen with scroll
16707 margins. See bug#1295.) */
16708 if (w->cursor.vpos < 0)
16709 {
16710 clear_glyph_matrix (w->desired_matrix);
16711 return 0;
16712 }
16713
16714 /* Scroll the display. */
16715 run.current_y = first_reusable_row->y;
16716 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16717 run.height = it.last_visible_y - run.current_y;
16718 dy = run.current_y - run.desired_y;
16719
16720 if (run.height)
16721 {
16722 update_begin (f);
16723 FRAME_RIF (f)->update_window_begin_hook (w);
16724 FRAME_RIF (f)->clear_window_mouse_face (w);
16725 FRAME_RIF (f)->scroll_run_hook (w, &run);
16726 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16727 update_end (f);
16728 }
16729
16730 /* Adjust Y positions of reused rows. */
16731 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16732 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16733 max_y = it.last_visible_y;
16734 for (row = first_reusable_row; row < first_row_to_display; ++row)
16735 {
16736 row->y -= dy;
16737 row->visible_height = row->height;
16738 if (row->y < min_y)
16739 row->visible_height -= min_y - row->y;
16740 if (row->y + row->height > max_y)
16741 row->visible_height -= row->y + row->height - max_y;
16742 if (row->fringe_bitmap_periodic_p)
16743 row->redraw_fringe_bitmaps_p = 1;
16744 }
16745
16746 /* Scroll the current matrix. */
16747 eassert (nrows_scrolled > 0);
16748 rotate_matrix (w->current_matrix,
16749 start_vpos,
16750 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16751 -nrows_scrolled);
16752
16753 /* Disable rows not reused. */
16754 for (row -= nrows_scrolled; row < bottom_row; ++row)
16755 row->enabled_p = 0;
16756
16757 /* Point may have moved to a different line, so we cannot assume that
16758 the previous cursor position is valid; locate the correct row. */
16759 if (pt_row)
16760 {
16761 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16762 row < bottom_row
16763 && PT >= MATRIX_ROW_END_CHARPOS (row)
16764 && !row->ends_at_zv_p;
16765 row++)
16766 {
16767 w->cursor.vpos++;
16768 w->cursor.y = row->y;
16769 }
16770 if (row < bottom_row)
16771 {
16772 /* Can't simply scan the row for point with
16773 bidi-reordered glyph rows. Let set_cursor_from_row
16774 figure out where to put the cursor, and if it fails,
16775 give up. */
16776 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16777 {
16778 if (!set_cursor_from_row (w, row, w->current_matrix,
16779 0, 0, 0, 0))
16780 {
16781 clear_glyph_matrix (w->desired_matrix);
16782 return 0;
16783 }
16784 }
16785 else
16786 {
16787 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16788 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16789
16790 for (; glyph < end
16791 && (!BUFFERP (glyph->object)
16792 || glyph->charpos < PT);
16793 glyph++)
16794 {
16795 w->cursor.hpos++;
16796 w->cursor.x += glyph->pixel_width;
16797 }
16798 }
16799 }
16800 }
16801
16802 /* Adjust window end. A null value of last_text_row means that
16803 the window end is in reused rows which in turn means that
16804 only its vpos can have changed. */
16805 if (last_text_row)
16806 adjust_window_ends (w, last_text_row, 0);
16807 else
16808 w->window_end_vpos -= nrows_scrolled;
16809
16810 w->window_end_valid = 0;
16811 w->desired_matrix->no_scrolling_p = 1;
16812
16813 #ifdef GLYPH_DEBUG
16814 debug_method_add (w, "try_window_reusing_current_matrix 2");
16815 #endif
16816 return 1;
16817 }
16818
16819 return 0;
16820 }
16821
16822
16823 \f
16824 /************************************************************************
16825 Window redisplay reusing current matrix when buffer has changed
16826 ************************************************************************/
16827
16828 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16829 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16830 ptrdiff_t *, ptrdiff_t *);
16831 static struct glyph_row *
16832 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16833 struct glyph_row *);
16834
16835
16836 /* Return the last row in MATRIX displaying text. If row START is
16837 non-null, start searching with that row. IT gives the dimensions
16838 of the display. Value is null if matrix is empty; otherwise it is
16839 a pointer to the row found. */
16840
16841 static struct glyph_row *
16842 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16843 struct glyph_row *start)
16844 {
16845 struct glyph_row *row, *row_found;
16846
16847 /* Set row_found to the last row in IT->w's current matrix
16848 displaying text. The loop looks funny but think of partially
16849 visible lines. */
16850 row_found = NULL;
16851 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16852 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16853 {
16854 eassert (row->enabled_p);
16855 row_found = row;
16856 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16857 break;
16858 ++row;
16859 }
16860
16861 return row_found;
16862 }
16863
16864
16865 /* Return the last row in the current matrix of W that is not affected
16866 by changes at the start of current_buffer that occurred since W's
16867 current matrix was built. Value is null if no such row exists.
16868
16869 BEG_UNCHANGED us the number of characters unchanged at the start of
16870 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16871 first changed character in current_buffer. Characters at positions <
16872 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16873 when the current matrix was built. */
16874
16875 static struct glyph_row *
16876 find_last_unchanged_at_beg_row (struct window *w)
16877 {
16878 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16879 struct glyph_row *row;
16880 struct glyph_row *row_found = NULL;
16881 int yb = window_text_bottom_y (w);
16882
16883 /* Find the last row displaying unchanged text. */
16884 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16885 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16886 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16887 ++row)
16888 {
16889 if (/* If row ends before first_changed_pos, it is unchanged,
16890 except in some case. */
16891 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16892 /* When row ends in ZV and we write at ZV it is not
16893 unchanged. */
16894 && !row->ends_at_zv_p
16895 /* When first_changed_pos is the end of a continued line,
16896 row is not unchanged because it may be no longer
16897 continued. */
16898 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16899 && (row->continued_p
16900 || row->exact_window_width_line_p))
16901 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16902 needs to be recomputed, so don't consider this row as
16903 unchanged. This happens when the last line was
16904 bidi-reordered and was killed immediately before this
16905 redisplay cycle. In that case, ROW->end stores the
16906 buffer position of the first visual-order character of
16907 the killed text, which is now beyond ZV. */
16908 && CHARPOS (row->end.pos) <= ZV)
16909 row_found = row;
16910
16911 /* Stop if last visible row. */
16912 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16913 break;
16914 }
16915
16916 return row_found;
16917 }
16918
16919
16920 /* Find the first glyph row in the current matrix of W that is not
16921 affected by changes at the end of current_buffer since the
16922 time W's current matrix was built.
16923
16924 Return in *DELTA the number of chars by which buffer positions in
16925 unchanged text at the end of current_buffer must be adjusted.
16926
16927 Return in *DELTA_BYTES the corresponding number of bytes.
16928
16929 Value is null if no such row exists, i.e. all rows are affected by
16930 changes. */
16931
16932 static struct glyph_row *
16933 find_first_unchanged_at_end_row (struct window *w,
16934 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16935 {
16936 struct glyph_row *row;
16937 struct glyph_row *row_found = NULL;
16938
16939 *delta = *delta_bytes = 0;
16940
16941 /* Display must not have been paused, otherwise the current matrix
16942 is not up to date. */
16943 eassert (w->window_end_valid);
16944
16945 /* A value of window_end_pos >= END_UNCHANGED means that the window
16946 end is in the range of changed text. If so, there is no
16947 unchanged row at the end of W's current matrix. */
16948 if (w->window_end_pos >= END_UNCHANGED)
16949 return NULL;
16950
16951 /* Set row to the last row in W's current matrix displaying text. */
16952 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16953
16954 /* If matrix is entirely empty, no unchanged row exists. */
16955 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16956 {
16957 /* The value of row is the last glyph row in the matrix having a
16958 meaningful buffer position in it. The end position of row
16959 corresponds to window_end_pos. This allows us to translate
16960 buffer positions in the current matrix to current buffer
16961 positions for characters not in changed text. */
16962 ptrdiff_t Z_old =
16963 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16964 ptrdiff_t Z_BYTE_old =
16965 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16966 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16967 struct glyph_row *first_text_row
16968 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16969
16970 *delta = Z - Z_old;
16971 *delta_bytes = Z_BYTE - Z_BYTE_old;
16972
16973 /* Set last_unchanged_pos to the buffer position of the last
16974 character in the buffer that has not been changed. Z is the
16975 index + 1 of the last character in current_buffer, i.e. by
16976 subtracting END_UNCHANGED we get the index of the last
16977 unchanged character, and we have to add BEG to get its buffer
16978 position. */
16979 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16980 last_unchanged_pos_old = last_unchanged_pos - *delta;
16981
16982 /* Search backward from ROW for a row displaying a line that
16983 starts at a minimum position >= last_unchanged_pos_old. */
16984 for (; row > first_text_row; --row)
16985 {
16986 /* This used to abort, but it can happen.
16987 It is ok to just stop the search instead here. KFS. */
16988 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16989 break;
16990
16991 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16992 row_found = row;
16993 }
16994 }
16995
16996 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16997
16998 return row_found;
16999 }
17000
17001
17002 /* Make sure that glyph rows in the current matrix of window W
17003 reference the same glyph memory as corresponding rows in the
17004 frame's frame matrix. This function is called after scrolling W's
17005 current matrix on a terminal frame in try_window_id and
17006 try_window_reusing_current_matrix. */
17007
17008 static void
17009 sync_frame_with_window_matrix_rows (struct window *w)
17010 {
17011 struct frame *f = XFRAME (w->frame);
17012 struct glyph_row *window_row, *window_row_end, *frame_row;
17013
17014 /* Preconditions: W must be a leaf window and full-width. Its frame
17015 must have a frame matrix. */
17016 eassert (BUFFERP (w->contents));
17017 eassert (WINDOW_FULL_WIDTH_P (w));
17018 eassert (!FRAME_WINDOW_P (f));
17019
17020 /* If W is a full-width window, glyph pointers in W's current matrix
17021 have, by definition, to be the same as glyph pointers in the
17022 corresponding frame matrix. Note that frame matrices have no
17023 marginal areas (see build_frame_matrix). */
17024 window_row = w->current_matrix->rows;
17025 window_row_end = window_row + w->current_matrix->nrows;
17026 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17027 while (window_row < window_row_end)
17028 {
17029 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17030 struct glyph *end = window_row->glyphs[LAST_AREA];
17031
17032 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17033 frame_row->glyphs[TEXT_AREA] = start;
17034 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17035 frame_row->glyphs[LAST_AREA] = end;
17036
17037 /* Disable frame rows whose corresponding window rows have
17038 been disabled in try_window_id. */
17039 if (!window_row->enabled_p)
17040 frame_row->enabled_p = 0;
17041
17042 ++window_row, ++frame_row;
17043 }
17044 }
17045
17046
17047 /* Find the glyph row in window W containing CHARPOS. Consider all
17048 rows between START and END (not inclusive). END null means search
17049 all rows to the end of the display area of W. Value is the row
17050 containing CHARPOS or null. */
17051
17052 struct glyph_row *
17053 row_containing_pos (struct window *w, ptrdiff_t charpos,
17054 struct glyph_row *start, struct glyph_row *end, int dy)
17055 {
17056 struct glyph_row *row = start;
17057 struct glyph_row *best_row = NULL;
17058 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17059 int last_y;
17060
17061 /* If we happen to start on a header-line, skip that. */
17062 if (row->mode_line_p)
17063 ++row;
17064
17065 if ((end && row >= end) || !row->enabled_p)
17066 return NULL;
17067
17068 last_y = window_text_bottom_y (w) - dy;
17069
17070 while (1)
17071 {
17072 /* Give up if we have gone too far. */
17073 if (end && row >= end)
17074 return NULL;
17075 /* This formerly returned if they were equal.
17076 I think that both quantities are of a "last plus one" type;
17077 if so, when they are equal, the row is within the screen. -- rms. */
17078 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17079 return NULL;
17080
17081 /* If it is in this row, return this row. */
17082 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17083 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17084 /* The end position of a row equals the start
17085 position of the next row. If CHARPOS is there, we
17086 would rather consider it displayed in the next
17087 line, except when this line ends in ZV. */
17088 && !row_for_charpos_p (row, charpos)))
17089 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17090 {
17091 struct glyph *g;
17092
17093 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17094 || (!best_row && !row->continued_p))
17095 return row;
17096 /* In bidi-reordered rows, there could be several rows whose
17097 edges surround CHARPOS, all of these rows belonging to
17098 the same continued line. We need to find the row which
17099 fits CHARPOS the best. */
17100 for (g = row->glyphs[TEXT_AREA];
17101 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17102 g++)
17103 {
17104 if (!STRINGP (g->object))
17105 {
17106 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17107 {
17108 mindif = eabs (g->charpos - charpos);
17109 best_row = row;
17110 /* Exact match always wins. */
17111 if (mindif == 0)
17112 return best_row;
17113 }
17114 }
17115 }
17116 }
17117 else if (best_row && !row->continued_p)
17118 return best_row;
17119 ++row;
17120 }
17121 }
17122
17123
17124 /* Try to redisplay window W by reusing its existing display. W's
17125 current matrix must be up to date when this function is called,
17126 i.e. window_end_valid must be nonzero.
17127
17128 Value is
17129
17130 1 if display has been updated
17131 0 if otherwise unsuccessful
17132 -1 if redisplay with same window start is known not to succeed
17133
17134 The following steps are performed:
17135
17136 1. Find the last row in the current matrix of W that is not
17137 affected by changes at the start of current_buffer. If no such row
17138 is found, give up.
17139
17140 2. Find the first row in W's current matrix that is not affected by
17141 changes at the end of current_buffer. Maybe there is no such row.
17142
17143 3. Display lines beginning with the row + 1 found in step 1 to the
17144 row found in step 2 or, if step 2 didn't find a row, to the end of
17145 the window.
17146
17147 4. If cursor is not known to appear on the window, give up.
17148
17149 5. If display stopped at the row found in step 2, scroll the
17150 display and current matrix as needed.
17151
17152 6. Maybe display some lines at the end of W, if we must. This can
17153 happen under various circumstances, like a partially visible line
17154 becoming fully visible, or because newly displayed lines are displayed
17155 in smaller font sizes.
17156
17157 7. Update W's window end information. */
17158
17159 static int
17160 try_window_id (struct window *w)
17161 {
17162 struct frame *f = XFRAME (w->frame);
17163 struct glyph_matrix *current_matrix = w->current_matrix;
17164 struct glyph_matrix *desired_matrix = w->desired_matrix;
17165 struct glyph_row *last_unchanged_at_beg_row;
17166 struct glyph_row *first_unchanged_at_end_row;
17167 struct glyph_row *row;
17168 struct glyph_row *bottom_row;
17169 int bottom_vpos;
17170 struct it it;
17171 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17172 int dvpos, dy;
17173 struct text_pos start_pos;
17174 struct run run;
17175 int first_unchanged_at_end_vpos = 0;
17176 struct glyph_row *last_text_row, *last_text_row_at_end;
17177 struct text_pos start;
17178 ptrdiff_t first_changed_charpos, last_changed_charpos;
17179
17180 #ifdef GLYPH_DEBUG
17181 if (inhibit_try_window_id)
17182 return 0;
17183 #endif
17184
17185 #ifdef HAVE_XWIDGETS_xxx
17186 //maybe needed for proper xwidget movement
17187 printf("try_window_id\n");
17188 return -1;
17189 #endif
17190
17191
17192 /* This is handy for debugging. */
17193 #if 0
17194 #define GIVE_UP(X) \
17195 do { \
17196 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17197 return 0; \
17198 } while (0)
17199 #else
17200 #define GIVE_UP(X) return 0
17201 #endif
17202
17203 SET_TEXT_POS_FROM_MARKER (start, w->start);
17204
17205 /* Don't use this for mini-windows because these can show
17206 messages and mini-buffers, and we don't handle that here. */
17207 if (MINI_WINDOW_P (w))
17208 GIVE_UP (1);
17209
17210 /* This flag is used to prevent redisplay optimizations. */
17211 if (windows_or_buffers_changed || cursor_type_changed)
17212 GIVE_UP (2);
17213
17214 /* Verify that narrowing has not changed.
17215 Also verify that we were not told to prevent redisplay optimizations.
17216 It would be nice to further
17217 reduce the number of cases where this prevents try_window_id. */
17218 if (current_buffer->clip_changed
17219 || current_buffer->prevent_redisplay_optimizations_p)
17220 GIVE_UP (3);
17221
17222 /* Window must either use window-based redisplay or be full width. */
17223 if (!FRAME_WINDOW_P (f)
17224 && (!FRAME_LINE_INS_DEL_OK (f)
17225 || !WINDOW_FULL_WIDTH_P (w)))
17226 GIVE_UP (4);
17227
17228 /* Give up if point is known NOT to appear in W. */
17229 if (PT < CHARPOS (start))
17230 GIVE_UP (5);
17231
17232 /* Another way to prevent redisplay optimizations. */
17233 if (w->last_modified == 0)
17234 GIVE_UP (6);
17235
17236 /* Verify that window is not hscrolled. */
17237 if (w->hscroll != 0)
17238 GIVE_UP (7);
17239
17240 /* Verify that display wasn't paused. */
17241 if (!w->window_end_valid)
17242 GIVE_UP (8);
17243
17244 /* Can't use this if highlighting a region because a cursor movement
17245 will do more than just set the cursor. */
17246 if (markpos_of_region () >= 0)
17247 GIVE_UP (9);
17248
17249 /* Likewise if highlighting trailing whitespace. */
17250 if (!NILP (Vshow_trailing_whitespace))
17251 GIVE_UP (11);
17252
17253 /* Likewise if showing a region. */
17254 if (w->region_showing)
17255 GIVE_UP (10);
17256
17257 /* Can't use this if overlay arrow position and/or string have
17258 changed. */
17259 if (overlay_arrows_changed_p ())
17260 GIVE_UP (12);
17261
17262 /* When word-wrap is on, adding a space to the first word of a
17263 wrapped line can change the wrap position, altering the line
17264 above it. It might be worthwhile to handle this more
17265 intelligently, but for now just redisplay from scratch. */
17266 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17267 GIVE_UP (21);
17268
17269 /* Under bidi reordering, adding or deleting a character in the
17270 beginning of a paragraph, before the first strong directional
17271 character, can change the base direction of the paragraph (unless
17272 the buffer specifies a fixed paragraph direction), which will
17273 require to redisplay the whole paragraph. It might be worthwhile
17274 to find the paragraph limits and widen the range of redisplayed
17275 lines to that, but for now just give up this optimization and
17276 redisplay from scratch. */
17277 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17278 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17279 GIVE_UP (22);
17280
17281 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17282 only if buffer has really changed. The reason is that the gap is
17283 initially at Z for freshly visited files. The code below would
17284 set end_unchanged to 0 in that case. */
17285 if (MODIFF > SAVE_MODIFF
17286 /* This seems to happen sometimes after saving a buffer. */
17287 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17288 {
17289 if (GPT - BEG < BEG_UNCHANGED)
17290 BEG_UNCHANGED = GPT - BEG;
17291 if (Z - GPT < END_UNCHANGED)
17292 END_UNCHANGED = Z - GPT;
17293 }
17294
17295 /* The position of the first and last character that has been changed. */
17296 first_changed_charpos = BEG + BEG_UNCHANGED;
17297 last_changed_charpos = Z - END_UNCHANGED;
17298
17299 /* If window starts after a line end, and the last change is in
17300 front of that newline, then changes don't affect the display.
17301 This case happens with stealth-fontification. Note that although
17302 the display is unchanged, glyph positions in the matrix have to
17303 be adjusted, of course. */
17304 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17305 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17306 && ((last_changed_charpos < CHARPOS (start)
17307 && CHARPOS (start) == BEGV)
17308 || (last_changed_charpos < CHARPOS (start) - 1
17309 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17310 {
17311 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17312 struct glyph_row *r0;
17313
17314 /* Compute how many chars/bytes have been added to or removed
17315 from the buffer. */
17316 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17317 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17318 Z_delta = Z - Z_old;
17319 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17320
17321 /* Give up if PT is not in the window. Note that it already has
17322 been checked at the start of try_window_id that PT is not in
17323 front of the window start. */
17324 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17325 GIVE_UP (13);
17326
17327 /* If window start is unchanged, we can reuse the whole matrix
17328 as is, after adjusting glyph positions. No need to compute
17329 the window end again, since its offset from Z hasn't changed. */
17330 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17331 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17332 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17333 /* PT must not be in a partially visible line. */
17334 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17335 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17336 {
17337 /* Adjust positions in the glyph matrix. */
17338 if (Z_delta || Z_delta_bytes)
17339 {
17340 struct glyph_row *r1
17341 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17342 increment_matrix_positions (w->current_matrix,
17343 MATRIX_ROW_VPOS (r0, current_matrix),
17344 MATRIX_ROW_VPOS (r1, current_matrix),
17345 Z_delta, Z_delta_bytes);
17346 }
17347
17348 /* Set the cursor. */
17349 row = row_containing_pos (w, PT, r0, NULL, 0);
17350 if (row)
17351 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17352 else
17353 emacs_abort ();
17354 return 1;
17355 }
17356 }
17357
17358 /* Handle the case that changes are all below what is displayed in
17359 the window, and that PT is in the window. This shortcut cannot
17360 be taken if ZV is visible in the window, and text has been added
17361 there that is visible in the window. */
17362 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17363 /* ZV is not visible in the window, or there are no
17364 changes at ZV, actually. */
17365 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17366 || first_changed_charpos == last_changed_charpos))
17367 {
17368 struct glyph_row *r0;
17369
17370 /* Give up if PT is not in the window. Note that it already has
17371 been checked at the start of try_window_id that PT is not in
17372 front of the window start. */
17373 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17374 GIVE_UP (14);
17375
17376 /* If window start is unchanged, we can reuse the whole matrix
17377 as is, without changing glyph positions since no text has
17378 been added/removed in front of the window end. */
17379 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17380 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17381 /* PT must not be in a partially visible line. */
17382 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17383 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17384 {
17385 /* We have to compute the window end anew since text
17386 could have been added/removed after it. */
17387 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17388 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17389
17390 /* Set the cursor. */
17391 row = row_containing_pos (w, PT, r0, NULL, 0);
17392 if (row)
17393 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17394 else
17395 emacs_abort ();
17396 return 2;
17397 }
17398 }
17399
17400 /* Give up if window start is in the changed area.
17401
17402 The condition used to read
17403
17404 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17405
17406 but why that was tested escapes me at the moment. */
17407 if (CHARPOS (start) >= first_changed_charpos
17408 && CHARPOS (start) <= last_changed_charpos)
17409 GIVE_UP (15);
17410
17411 /* Check that window start agrees with the start of the first glyph
17412 row in its current matrix. Check this after we know the window
17413 start is not in changed text, otherwise positions would not be
17414 comparable. */
17415 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17416 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17417 GIVE_UP (16);
17418
17419 /* Give up if the window ends in strings. Overlay strings
17420 at the end are difficult to handle, so don't try. */
17421 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17422 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17423 GIVE_UP (20);
17424
17425 /* Compute the position at which we have to start displaying new
17426 lines. Some of the lines at the top of the window might be
17427 reusable because they are not displaying changed text. Find the
17428 last row in W's current matrix not affected by changes at the
17429 start of current_buffer. Value is null if changes start in the
17430 first line of window. */
17431 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17432 if (last_unchanged_at_beg_row)
17433 {
17434 /* Avoid starting to display in the middle of a character, a TAB
17435 for instance. This is easier than to set up the iterator
17436 exactly, and it's not a frequent case, so the additional
17437 effort wouldn't really pay off. */
17438 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17439 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17440 && last_unchanged_at_beg_row > w->current_matrix->rows)
17441 --last_unchanged_at_beg_row;
17442
17443 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17444 GIVE_UP (17);
17445
17446 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17447 GIVE_UP (18);
17448 start_pos = it.current.pos;
17449
17450 /* Start displaying new lines in the desired matrix at the same
17451 vpos we would use in the current matrix, i.e. below
17452 last_unchanged_at_beg_row. */
17453 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17454 current_matrix);
17455 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17456 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17457
17458 eassert (it.hpos == 0 && it.current_x == 0);
17459 }
17460 else
17461 {
17462 /* There are no reusable lines at the start of the window.
17463 Start displaying in the first text line. */
17464 start_display (&it, w, start);
17465 it.vpos = it.first_vpos;
17466 start_pos = it.current.pos;
17467 }
17468
17469 /* Find the first row that is not affected by changes at the end of
17470 the buffer. Value will be null if there is no unchanged row, in
17471 which case we must redisplay to the end of the window. delta
17472 will be set to the value by which buffer positions beginning with
17473 first_unchanged_at_end_row have to be adjusted due to text
17474 changes. */
17475 first_unchanged_at_end_row
17476 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17477 IF_DEBUG (debug_delta = delta);
17478 IF_DEBUG (debug_delta_bytes = delta_bytes);
17479
17480 /* Set stop_pos to the buffer position up to which we will have to
17481 display new lines. If first_unchanged_at_end_row != NULL, this
17482 is the buffer position of the start of the line displayed in that
17483 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17484 that we don't stop at a buffer position. */
17485 stop_pos = 0;
17486 if (first_unchanged_at_end_row)
17487 {
17488 eassert (last_unchanged_at_beg_row == NULL
17489 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17490
17491 /* If this is a continuation line, move forward to the next one
17492 that isn't. Changes in lines above affect this line.
17493 Caution: this may move first_unchanged_at_end_row to a row
17494 not displaying text. */
17495 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17496 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17497 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17498 < it.last_visible_y))
17499 ++first_unchanged_at_end_row;
17500
17501 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17502 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17503 >= it.last_visible_y))
17504 first_unchanged_at_end_row = NULL;
17505 else
17506 {
17507 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17508 + delta);
17509 first_unchanged_at_end_vpos
17510 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17511 eassert (stop_pos >= Z - END_UNCHANGED);
17512 }
17513 }
17514 else if (last_unchanged_at_beg_row == NULL)
17515 GIVE_UP (19);
17516
17517
17518 #ifdef GLYPH_DEBUG
17519
17520 /* Either there is no unchanged row at the end, or the one we have
17521 now displays text. This is a necessary condition for the window
17522 end pos calculation at the end of this function. */
17523 eassert (first_unchanged_at_end_row == NULL
17524 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17525
17526 debug_last_unchanged_at_beg_vpos
17527 = (last_unchanged_at_beg_row
17528 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17529 : -1);
17530 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17531
17532 #endif /* GLYPH_DEBUG */
17533
17534
17535 /* Display new lines. Set last_text_row to the last new line
17536 displayed which has text on it, i.e. might end up as being the
17537 line where the window_end_vpos is. */
17538 w->cursor.vpos = -1;
17539 last_text_row = NULL;
17540 overlay_arrow_seen = 0;
17541 while (it.current_y < it.last_visible_y
17542 && !fonts_changed_p
17543 && (first_unchanged_at_end_row == NULL
17544 || IT_CHARPOS (it) < stop_pos))
17545 {
17546 if (display_line (&it))
17547 last_text_row = it.glyph_row - 1;
17548 }
17549
17550 if (fonts_changed_p)
17551 return -1;
17552
17553
17554 /* Compute differences in buffer positions, y-positions etc. for
17555 lines reused at the bottom of the window. Compute what we can
17556 scroll. */
17557 if (first_unchanged_at_end_row
17558 /* No lines reused because we displayed everything up to the
17559 bottom of the window. */
17560 && it.current_y < it.last_visible_y)
17561 {
17562 dvpos = (it.vpos
17563 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17564 current_matrix));
17565 dy = it.current_y - first_unchanged_at_end_row->y;
17566 run.current_y = first_unchanged_at_end_row->y;
17567 run.desired_y = run.current_y + dy;
17568 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17569 }
17570 else
17571 {
17572 delta = delta_bytes = dvpos = dy
17573 = run.current_y = run.desired_y = run.height = 0;
17574 first_unchanged_at_end_row = NULL;
17575 }
17576 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17577
17578
17579 /* Find the cursor if not already found. We have to decide whether
17580 PT will appear on this window (it sometimes doesn't, but this is
17581 not a very frequent case.) This decision has to be made before
17582 the current matrix is altered. A value of cursor.vpos < 0 means
17583 that PT is either in one of the lines beginning at
17584 first_unchanged_at_end_row or below the window. Don't care for
17585 lines that might be displayed later at the window end; as
17586 mentioned, this is not a frequent case. */
17587 if (w->cursor.vpos < 0)
17588 {
17589 /* Cursor in unchanged rows at the top? */
17590 if (PT < CHARPOS (start_pos)
17591 && last_unchanged_at_beg_row)
17592 {
17593 row = row_containing_pos (w, PT,
17594 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17595 last_unchanged_at_beg_row + 1, 0);
17596 if (row)
17597 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17598 }
17599
17600 /* Start from first_unchanged_at_end_row looking for PT. */
17601 else if (first_unchanged_at_end_row)
17602 {
17603 row = row_containing_pos (w, PT - delta,
17604 first_unchanged_at_end_row, NULL, 0);
17605 if (row)
17606 set_cursor_from_row (w, row, w->current_matrix, delta,
17607 delta_bytes, dy, dvpos);
17608 }
17609
17610 /* Give up if cursor was not found. */
17611 if (w->cursor.vpos < 0)
17612 {
17613 clear_glyph_matrix (w->desired_matrix);
17614 return -1;
17615 }
17616 }
17617
17618 /* Don't let the cursor end in the scroll margins. */
17619 {
17620 int this_scroll_margin, cursor_height;
17621 int frame_line_height = default_line_pixel_height (w);
17622 int window_total_lines
17623 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17624
17625 this_scroll_margin =
17626 max (0, min (scroll_margin, window_total_lines / 4));
17627 this_scroll_margin *= frame_line_height;
17628 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17629
17630 if ((w->cursor.y < this_scroll_margin
17631 && CHARPOS (start) > BEGV)
17632 /* Old redisplay didn't take scroll margin into account at the bottom,
17633 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17634 || (w->cursor.y + (make_cursor_line_fully_visible_p
17635 ? cursor_height + this_scroll_margin
17636 : 1)) > it.last_visible_y)
17637 {
17638 w->cursor.vpos = -1;
17639 clear_glyph_matrix (w->desired_matrix);
17640 return -1;
17641 }
17642 }
17643
17644 /* Scroll the display. Do it before changing the current matrix so
17645 that xterm.c doesn't get confused about where the cursor glyph is
17646 found. */
17647 if (dy && run.height)
17648 {
17649 update_begin (f);
17650
17651 if (FRAME_WINDOW_P (f))
17652 {
17653 FRAME_RIF (f)->update_window_begin_hook (w);
17654 FRAME_RIF (f)->clear_window_mouse_face (w);
17655 FRAME_RIF (f)->scroll_run_hook (w, &run);
17656 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17657 }
17658 else
17659 {
17660 /* Terminal frame. In this case, dvpos gives the number of
17661 lines to scroll by; dvpos < 0 means scroll up. */
17662 int from_vpos
17663 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17664 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17665 int end = (WINDOW_TOP_EDGE_LINE (w)
17666 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17667 + window_internal_height (w));
17668
17669 #if defined (HAVE_GPM) || defined (MSDOS)
17670 x_clear_window_mouse_face (w);
17671 #endif
17672 /* Perform the operation on the screen. */
17673 if (dvpos > 0)
17674 {
17675 /* Scroll last_unchanged_at_beg_row to the end of the
17676 window down dvpos lines. */
17677 set_terminal_window (f, end);
17678
17679 /* On dumb terminals delete dvpos lines at the end
17680 before inserting dvpos empty lines. */
17681 if (!FRAME_SCROLL_REGION_OK (f))
17682 ins_del_lines (f, end - dvpos, -dvpos);
17683
17684 /* Insert dvpos empty lines in front of
17685 last_unchanged_at_beg_row. */
17686 ins_del_lines (f, from, dvpos);
17687 }
17688 else if (dvpos < 0)
17689 {
17690 /* Scroll up last_unchanged_at_beg_vpos to the end of
17691 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17692 set_terminal_window (f, end);
17693
17694 /* Delete dvpos lines in front of
17695 last_unchanged_at_beg_vpos. ins_del_lines will set
17696 the cursor to the given vpos and emit |dvpos| delete
17697 line sequences. */
17698 ins_del_lines (f, from + dvpos, dvpos);
17699
17700 /* On a dumb terminal insert dvpos empty lines at the
17701 end. */
17702 if (!FRAME_SCROLL_REGION_OK (f))
17703 ins_del_lines (f, end + dvpos, -dvpos);
17704 }
17705
17706 set_terminal_window (f, 0);
17707 }
17708
17709 update_end (f);
17710 }
17711
17712 /* Shift reused rows of the current matrix to the right position.
17713 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17714 text. */
17715 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17716 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17717 if (dvpos < 0)
17718 {
17719 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17720 bottom_vpos, dvpos);
17721 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17722 bottom_vpos);
17723 }
17724 else if (dvpos > 0)
17725 {
17726 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17727 bottom_vpos, dvpos);
17728 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17729 first_unchanged_at_end_vpos + dvpos);
17730 }
17731
17732 /* For frame-based redisplay, make sure that current frame and window
17733 matrix are in sync with respect to glyph memory. */
17734 if (!FRAME_WINDOW_P (f))
17735 sync_frame_with_window_matrix_rows (w);
17736
17737 /* Adjust buffer positions in reused rows. */
17738 if (delta || delta_bytes)
17739 increment_matrix_positions (current_matrix,
17740 first_unchanged_at_end_vpos + dvpos,
17741 bottom_vpos, delta, delta_bytes);
17742
17743 /* Adjust Y positions. */
17744 if (dy)
17745 shift_glyph_matrix (w, current_matrix,
17746 first_unchanged_at_end_vpos + dvpos,
17747 bottom_vpos, dy);
17748
17749 if (first_unchanged_at_end_row)
17750 {
17751 first_unchanged_at_end_row += dvpos;
17752 if (first_unchanged_at_end_row->y >= it.last_visible_y
17753 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17754 first_unchanged_at_end_row = NULL;
17755 }
17756
17757 /* If scrolling up, there may be some lines to display at the end of
17758 the window. */
17759 last_text_row_at_end = NULL;
17760 if (dy < 0)
17761 {
17762 /* Scrolling up can leave for example a partially visible line
17763 at the end of the window to be redisplayed. */
17764 /* Set last_row to the glyph row in the current matrix where the
17765 window end line is found. It has been moved up or down in
17766 the matrix by dvpos. */
17767 int last_vpos = w->window_end_vpos + dvpos;
17768 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17769
17770 /* If last_row is the window end line, it should display text. */
17771 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17772
17773 /* If window end line was partially visible before, begin
17774 displaying at that line. Otherwise begin displaying with the
17775 line following it. */
17776 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17777 {
17778 init_to_row_start (&it, w, last_row);
17779 it.vpos = last_vpos;
17780 it.current_y = last_row->y;
17781 }
17782 else
17783 {
17784 init_to_row_end (&it, w, last_row);
17785 it.vpos = 1 + last_vpos;
17786 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17787 ++last_row;
17788 }
17789
17790 /* We may start in a continuation line. If so, we have to
17791 get the right continuation_lines_width and current_x. */
17792 it.continuation_lines_width = last_row->continuation_lines_width;
17793 it.hpos = it.current_x = 0;
17794
17795 /* Display the rest of the lines at the window end. */
17796 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17797 while (it.current_y < it.last_visible_y
17798 && !fonts_changed_p)
17799 {
17800 /* Is it always sure that the display agrees with lines in
17801 the current matrix? I don't think so, so we mark rows
17802 displayed invalid in the current matrix by setting their
17803 enabled_p flag to zero. */
17804 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17805 if (display_line (&it))
17806 last_text_row_at_end = it.glyph_row - 1;
17807 }
17808 }
17809
17810 /* Update window_end_pos and window_end_vpos. */
17811 if (first_unchanged_at_end_row && !last_text_row_at_end)
17812 {
17813 /* Window end line if one of the preserved rows from the current
17814 matrix. Set row to the last row displaying text in current
17815 matrix starting at first_unchanged_at_end_row, after
17816 scrolling. */
17817 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17818 row = find_last_row_displaying_text (w->current_matrix, &it,
17819 first_unchanged_at_end_row);
17820 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17821 adjust_window_ends (w, row, 1);
17822 eassert (w->window_end_bytepos >= 0);
17823 IF_DEBUG (debug_method_add (w, "A"));
17824 }
17825 else if (last_text_row_at_end)
17826 {
17827 adjust_window_ends (w, last_text_row_at_end, 0);
17828 eassert (w->window_end_bytepos >= 0);
17829 IF_DEBUG (debug_method_add (w, "B"));
17830 }
17831 else if (last_text_row)
17832 {
17833 /* We have displayed either to the end of the window or at the
17834 end of the window, i.e. the last row with text is to be found
17835 in the desired matrix. */
17836 adjust_window_ends (w, last_text_row, 0);
17837 eassert (w->window_end_bytepos >= 0);
17838 }
17839 else if (first_unchanged_at_end_row == NULL
17840 && last_text_row == NULL
17841 && last_text_row_at_end == NULL)
17842 {
17843 /* Displayed to end of window, but no line containing text was
17844 displayed. Lines were deleted at the end of the window. */
17845 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17846 int vpos = w->window_end_vpos;
17847 struct glyph_row *current_row = current_matrix->rows + vpos;
17848 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17849
17850 for (row = NULL;
17851 row == NULL && vpos >= first_vpos;
17852 --vpos, --current_row, --desired_row)
17853 {
17854 if (desired_row->enabled_p)
17855 {
17856 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17857 row = desired_row;
17858 }
17859 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17860 row = current_row;
17861 }
17862
17863 eassert (row != NULL);
17864 w->window_end_vpos = vpos + 1;
17865 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17866 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17867 eassert (w->window_end_bytepos >= 0);
17868 IF_DEBUG (debug_method_add (w, "C"));
17869 }
17870 else
17871 emacs_abort ();
17872
17873 IF_DEBUG (debug_end_pos = w->window_end_pos;
17874 debug_end_vpos = w->window_end_vpos);
17875
17876 /* Record that display has not been completed. */
17877 w->window_end_valid = 0;
17878 w->desired_matrix->no_scrolling_p = 1;
17879 return 3;
17880
17881 #undef GIVE_UP
17882 }
17883
17884
17885 \f
17886 /***********************************************************************
17887 More debugging support
17888 ***********************************************************************/
17889
17890 #ifdef GLYPH_DEBUG
17891
17892 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17893 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17894 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17895
17896
17897 /* Dump the contents of glyph matrix MATRIX on stderr.
17898
17899 GLYPHS 0 means don't show glyph contents.
17900 GLYPHS 1 means show glyphs in short form
17901 GLYPHS > 1 means show glyphs in long form. */
17902
17903 void
17904 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17905 {
17906 int i;
17907 for (i = 0; i < matrix->nrows; ++i)
17908 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17909 }
17910
17911
17912 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17913 the glyph row and area where the glyph comes from. */
17914
17915 void
17916 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17917 {
17918 if (glyph->type == CHAR_GLYPH
17919 || glyph->type == GLYPHLESS_GLYPH)
17920 {
17921 fprintf (stderr,
17922 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17923 glyph - row->glyphs[TEXT_AREA],
17924 (glyph->type == CHAR_GLYPH
17925 ? 'C'
17926 : 'G'),
17927 glyph->charpos,
17928 (BUFFERP (glyph->object)
17929 ? 'B'
17930 : (STRINGP (glyph->object)
17931 ? 'S'
17932 : (INTEGERP (glyph->object)
17933 ? '0'
17934 : '-'))),
17935 glyph->pixel_width,
17936 glyph->u.ch,
17937 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17938 ? glyph->u.ch
17939 : '.'),
17940 glyph->face_id,
17941 glyph->left_box_line_p,
17942 glyph->right_box_line_p);
17943 }
17944 else if (glyph->type == STRETCH_GLYPH)
17945 {
17946 fprintf (stderr,
17947 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17948 glyph - row->glyphs[TEXT_AREA],
17949 'S',
17950 glyph->charpos,
17951 (BUFFERP (glyph->object)
17952 ? 'B'
17953 : (STRINGP (glyph->object)
17954 ? 'S'
17955 : (INTEGERP (glyph->object)
17956 ? '0'
17957 : '-'))),
17958 glyph->pixel_width,
17959 0,
17960 ' ',
17961 glyph->face_id,
17962 glyph->left_box_line_p,
17963 glyph->right_box_line_p);
17964 }
17965 else if (glyph->type == IMAGE_GLYPH)
17966 {
17967 fprintf (stderr,
17968 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17969 glyph - row->glyphs[TEXT_AREA],
17970 'I',
17971 glyph->charpos,
17972 (BUFFERP (glyph->object)
17973 ? 'B'
17974 : (STRINGP (glyph->object)
17975 ? 'S'
17976 : (INTEGERP (glyph->object)
17977 ? '0'
17978 : '-'))),
17979 glyph->pixel_width,
17980 glyph->u.img_id,
17981 '.',
17982 glyph->face_id,
17983 glyph->left_box_line_p,
17984 glyph->right_box_line_p);
17985 }
17986 else if (glyph->type == COMPOSITE_GLYPH)
17987 {
17988 fprintf (stderr,
17989 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17990 glyph - row->glyphs[TEXT_AREA],
17991 '+',
17992 glyph->charpos,
17993 (BUFFERP (glyph->object)
17994 ? 'B'
17995 : (STRINGP (glyph->object)
17996 ? 'S'
17997 : (INTEGERP (glyph->object)
17998 ? '0'
17999 : '-'))),
18000 glyph->pixel_width,
18001 glyph->u.cmp.id);
18002 if (glyph->u.cmp.automatic)
18003 fprintf (stderr,
18004 "[%d-%d]",
18005 glyph->slice.cmp.from, glyph->slice.cmp.to);
18006 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18007 glyph->face_id,
18008 glyph->left_box_line_p,
18009 glyph->right_box_line_p);
18010 }
18011 #ifdef HAVE_XWIDGETS
18012 else if (glyph->type == XWIDGET_GLYPH)
18013 {
18014 fprintf (stderr,
18015 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18016 glyph - row->glyphs[TEXT_AREA],
18017 'X',
18018 glyph->charpos,
18019 (BUFFERP (glyph->object)
18020 ? 'B'
18021 : (STRINGP (glyph->object)
18022 ? 'S'
18023 : '-')),
18024 glyph->pixel_width,
18025 glyph->u.xwidget,
18026 '.',
18027 glyph->face_id,
18028 glyph->left_box_line_p,
18029 glyph->right_box_line_p);
18030
18031 // printf("dump xwidget glyph\n");
18032 }
18033 #endif
18034 }
18035
18036
18037 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18038 GLYPHS 0 means don't show glyph contents.
18039 GLYPHS 1 means show glyphs in short form
18040 GLYPHS > 1 means show glyphs in long form. */
18041
18042 void
18043 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18044 {
18045 if (glyphs != 1)
18046 {
18047 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18048 fprintf (stderr, "==============================================================================\n");
18049
18050 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18051 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18052 vpos,
18053 MATRIX_ROW_START_CHARPOS (row),
18054 MATRIX_ROW_END_CHARPOS (row),
18055 row->used[TEXT_AREA],
18056 row->contains_overlapping_glyphs_p,
18057 row->enabled_p,
18058 row->truncated_on_left_p,
18059 row->truncated_on_right_p,
18060 row->continued_p,
18061 MATRIX_ROW_CONTINUATION_LINE_P (row),
18062 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18063 row->ends_at_zv_p,
18064 row->fill_line_p,
18065 row->ends_in_middle_of_char_p,
18066 row->starts_in_middle_of_char_p,
18067 row->mouse_face_p,
18068 row->x,
18069 row->y,
18070 row->pixel_width,
18071 row->height,
18072 row->visible_height,
18073 row->ascent,
18074 row->phys_ascent);
18075 /* The next 3 lines should align to "Start" in the header. */
18076 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18077 row->end.overlay_string_index,
18078 row->continuation_lines_width);
18079 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18080 CHARPOS (row->start.string_pos),
18081 CHARPOS (row->end.string_pos));
18082 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18083 row->end.dpvec_index);
18084 }
18085
18086 if (glyphs > 1)
18087 {
18088 int area;
18089
18090 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18091 {
18092 struct glyph *glyph = row->glyphs[area];
18093 struct glyph *glyph_end = glyph + row->used[area];
18094
18095 /* Glyph for a line end in text. */
18096 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18097 ++glyph_end;
18098
18099 if (glyph < glyph_end)
18100 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18101
18102 for (; glyph < glyph_end; ++glyph)
18103 dump_glyph (row, glyph, area);
18104 }
18105 }
18106 else if (glyphs == 1)
18107 {
18108 int area;
18109
18110 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18111 {
18112 char *s = alloca (row->used[area] + 4);
18113 int i;
18114
18115 for (i = 0; i < row->used[area]; ++i)
18116 {
18117 struct glyph *glyph = row->glyphs[area] + i;
18118 if (i == row->used[area] - 1
18119 && area == TEXT_AREA
18120 && INTEGERP (glyph->object)
18121 && glyph->type == CHAR_GLYPH
18122 && glyph->u.ch == ' ')
18123 {
18124 strcpy (&s[i], "[\\n]");
18125 i += 4;
18126 }
18127 else if (glyph->type == CHAR_GLYPH
18128 && glyph->u.ch < 0x80
18129 && glyph->u.ch >= ' ')
18130 s[i] = glyph->u.ch;
18131 else
18132 s[i] = '.';
18133 }
18134
18135 s[i] = '\0';
18136 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18137 }
18138 }
18139 }
18140
18141
18142 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18143 Sdump_glyph_matrix, 0, 1, "p",
18144 doc: /* Dump the current matrix of the selected window to stderr.
18145 Shows contents of glyph row structures. With non-nil
18146 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18147 glyphs in short form, otherwise show glyphs in long form. */)
18148 (Lisp_Object glyphs)
18149 {
18150 struct window *w = XWINDOW (selected_window);
18151 struct buffer *buffer = XBUFFER (w->contents);
18152
18153 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18154 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18155 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18156 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18157 fprintf (stderr, "=============================================\n");
18158 dump_glyph_matrix (w->current_matrix,
18159 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18160 return Qnil;
18161 }
18162
18163
18164 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18165 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18166 (void)
18167 {
18168 struct frame *f = XFRAME (selected_frame);
18169 dump_glyph_matrix (f->current_matrix, 1);
18170 return Qnil;
18171 }
18172
18173
18174 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18175 doc: /* Dump glyph row ROW to stderr.
18176 GLYPH 0 means don't dump glyphs.
18177 GLYPH 1 means dump glyphs in short form.
18178 GLYPH > 1 or omitted means dump glyphs in long form. */)
18179 (Lisp_Object row, Lisp_Object glyphs)
18180 {
18181 struct glyph_matrix *matrix;
18182 EMACS_INT vpos;
18183
18184 CHECK_NUMBER (row);
18185 matrix = XWINDOW (selected_window)->current_matrix;
18186 vpos = XINT (row);
18187 if (vpos >= 0 && vpos < matrix->nrows)
18188 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18189 vpos,
18190 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18191 return Qnil;
18192 }
18193
18194
18195 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18196 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18197 GLYPH 0 means don't dump glyphs.
18198 GLYPH 1 means dump glyphs in short form.
18199 GLYPH > 1 or omitted means dump glyphs in long form. */)
18200 (Lisp_Object row, Lisp_Object glyphs)
18201 {
18202 struct frame *sf = SELECTED_FRAME ();
18203 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18204 EMACS_INT vpos;
18205
18206 CHECK_NUMBER (row);
18207 vpos = XINT (row);
18208 if (vpos >= 0 && vpos < m->nrows)
18209 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18210 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18211 return Qnil;
18212 }
18213
18214
18215 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18216 doc: /* Toggle tracing of redisplay.
18217 With ARG, turn tracing on if and only if ARG is positive. */)
18218 (Lisp_Object arg)
18219 {
18220 if (NILP (arg))
18221 trace_redisplay_p = !trace_redisplay_p;
18222 else
18223 {
18224 arg = Fprefix_numeric_value (arg);
18225 trace_redisplay_p = XINT (arg) > 0;
18226 }
18227
18228 return Qnil;
18229 }
18230
18231
18232 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18233 doc: /* Like `format', but print result to stderr.
18234 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18235 (ptrdiff_t nargs, Lisp_Object *args)
18236 {
18237 Lisp_Object s = Fformat (nargs, args);
18238 fprintf (stderr, "%s", SDATA (s));
18239 return Qnil;
18240 }
18241
18242 #endif /* GLYPH_DEBUG */
18243
18244
18245 \f
18246 /***********************************************************************
18247 Building Desired Matrix Rows
18248 ***********************************************************************/
18249
18250 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18251 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18252
18253 static struct glyph_row *
18254 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18255 {
18256 struct frame *f = XFRAME (WINDOW_FRAME (w));
18257 struct buffer *buffer = XBUFFER (w->contents);
18258 struct buffer *old = current_buffer;
18259 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18260 int arrow_len = SCHARS (overlay_arrow_string);
18261 const unsigned char *arrow_end = arrow_string + arrow_len;
18262 const unsigned char *p;
18263 struct it it;
18264 bool multibyte_p;
18265 int n_glyphs_before;
18266
18267 set_buffer_temp (buffer);
18268 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18269 it.glyph_row->used[TEXT_AREA] = 0;
18270 SET_TEXT_POS (it.position, 0, 0);
18271
18272 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18273 p = arrow_string;
18274 while (p < arrow_end)
18275 {
18276 Lisp_Object face, ilisp;
18277
18278 /* Get the next character. */
18279 if (multibyte_p)
18280 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18281 else
18282 {
18283 it.c = it.char_to_display = *p, it.len = 1;
18284 if (! ASCII_CHAR_P (it.c))
18285 it.char_to_display = BYTE8_TO_CHAR (it.c);
18286 }
18287 p += it.len;
18288
18289 /* Get its face. */
18290 ilisp = make_number (p - arrow_string);
18291 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18292 it.face_id = compute_char_face (f, it.char_to_display, face);
18293
18294 /* Compute its width, get its glyphs. */
18295 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18296 SET_TEXT_POS (it.position, -1, -1);
18297 PRODUCE_GLYPHS (&it);
18298
18299 /* If this character doesn't fit any more in the line, we have
18300 to remove some glyphs. */
18301 if (it.current_x > it.last_visible_x)
18302 {
18303 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18304 break;
18305 }
18306 }
18307
18308 set_buffer_temp (old);
18309 return it.glyph_row;
18310 }
18311
18312
18313 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18314 glyphs to insert is determined by produce_special_glyphs. */
18315
18316 static void
18317 insert_left_trunc_glyphs (struct it *it)
18318 {
18319 struct it truncate_it;
18320 struct glyph *from, *end, *to, *toend;
18321
18322 eassert (!FRAME_WINDOW_P (it->f)
18323 || (!it->glyph_row->reversed_p
18324 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18325 || (it->glyph_row->reversed_p
18326 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18327
18328 /* Get the truncation glyphs. */
18329 truncate_it = *it;
18330 truncate_it.current_x = 0;
18331 truncate_it.face_id = DEFAULT_FACE_ID;
18332 truncate_it.glyph_row = &scratch_glyph_row;
18333 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18334 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18335 truncate_it.object = make_number (0);
18336 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18337
18338 /* Overwrite glyphs from IT with truncation glyphs. */
18339 if (!it->glyph_row->reversed_p)
18340 {
18341 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18342
18343 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18344 end = from + tused;
18345 to = it->glyph_row->glyphs[TEXT_AREA];
18346 toend = to + it->glyph_row->used[TEXT_AREA];
18347 if (FRAME_WINDOW_P (it->f))
18348 {
18349 /* On GUI frames, when variable-size fonts are displayed,
18350 the truncation glyphs may need more pixels than the row's
18351 glyphs they overwrite. We overwrite more glyphs to free
18352 enough screen real estate, and enlarge the stretch glyph
18353 on the right (see display_line), if there is one, to
18354 preserve the screen position of the truncation glyphs on
18355 the right. */
18356 int w = 0;
18357 struct glyph *g = to;
18358 short used;
18359
18360 /* The first glyph could be partially visible, in which case
18361 it->glyph_row->x will be negative. But we want the left
18362 truncation glyphs to be aligned at the left margin of the
18363 window, so we override the x coordinate at which the row
18364 will begin. */
18365 it->glyph_row->x = 0;
18366 while (g < toend && w < it->truncation_pixel_width)
18367 {
18368 w += g->pixel_width;
18369 ++g;
18370 }
18371 if (g - to - tused > 0)
18372 {
18373 memmove (to + tused, g, (toend - g) * sizeof(*g));
18374 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18375 }
18376 used = it->glyph_row->used[TEXT_AREA];
18377 if (it->glyph_row->truncated_on_right_p
18378 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18379 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18380 == STRETCH_GLYPH)
18381 {
18382 int extra = w - it->truncation_pixel_width;
18383
18384 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18385 }
18386 }
18387
18388 while (from < end)
18389 *to++ = *from++;
18390
18391 /* There may be padding glyphs left over. Overwrite them too. */
18392 if (!FRAME_WINDOW_P (it->f))
18393 {
18394 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18395 {
18396 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18397 while (from < end)
18398 *to++ = *from++;
18399 }
18400 }
18401
18402 if (to > toend)
18403 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18404 }
18405 else
18406 {
18407 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18408
18409 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18410 that back to front. */
18411 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18412 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18413 toend = it->glyph_row->glyphs[TEXT_AREA];
18414 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18415 if (FRAME_WINDOW_P (it->f))
18416 {
18417 int w = 0;
18418 struct glyph *g = to;
18419
18420 while (g >= toend && w < it->truncation_pixel_width)
18421 {
18422 w += g->pixel_width;
18423 --g;
18424 }
18425 if (to - g - tused > 0)
18426 to = g + tused;
18427 if (it->glyph_row->truncated_on_right_p
18428 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18429 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18430 {
18431 int extra = w - it->truncation_pixel_width;
18432
18433 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18434 }
18435 }
18436
18437 while (from >= end && to >= toend)
18438 *to-- = *from--;
18439 if (!FRAME_WINDOW_P (it->f))
18440 {
18441 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18442 {
18443 from =
18444 truncate_it.glyph_row->glyphs[TEXT_AREA]
18445 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18446 while (from >= end && to >= toend)
18447 *to-- = *from--;
18448 }
18449 }
18450 if (from >= end)
18451 {
18452 /* Need to free some room before prepending additional
18453 glyphs. */
18454 int move_by = from - end + 1;
18455 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18456 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18457
18458 for ( ; g >= g0; g--)
18459 g[move_by] = *g;
18460 while (from >= end)
18461 *to-- = *from--;
18462 it->glyph_row->used[TEXT_AREA] += move_by;
18463 }
18464 }
18465 }
18466
18467 /* Compute the hash code for ROW. */
18468 unsigned
18469 row_hash (struct glyph_row *row)
18470 {
18471 int area, k;
18472 unsigned hashval = 0;
18473
18474 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18475 for (k = 0; k < row->used[area]; ++k)
18476 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18477 + row->glyphs[area][k].u.val
18478 + row->glyphs[area][k].face_id
18479 + row->glyphs[area][k].padding_p
18480 + (row->glyphs[area][k].type << 2));
18481
18482 return hashval;
18483 }
18484
18485 /* Compute the pixel height and width of IT->glyph_row.
18486
18487 Most of the time, ascent and height of a display line will be equal
18488 to the max_ascent and max_height values of the display iterator
18489 structure. This is not the case if
18490
18491 1. We hit ZV without displaying anything. In this case, max_ascent
18492 and max_height will be zero.
18493
18494 2. We have some glyphs that don't contribute to the line height.
18495 (The glyph row flag contributes_to_line_height_p is for future
18496 pixmap extensions).
18497
18498 The first case is easily covered by using default values because in
18499 these cases, the line height does not really matter, except that it
18500 must not be zero. */
18501
18502 static void
18503 compute_line_metrics (struct it *it)
18504 {
18505 struct glyph_row *row = it->glyph_row;
18506
18507 if (FRAME_WINDOW_P (it->f))
18508 {
18509 int i, min_y, max_y;
18510
18511 /* The line may consist of one space only, that was added to
18512 place the cursor on it. If so, the row's height hasn't been
18513 computed yet. */
18514 if (row->height == 0)
18515 {
18516 if (it->max_ascent + it->max_descent == 0)
18517 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18518 row->ascent = it->max_ascent;
18519 row->height = it->max_ascent + it->max_descent;
18520 row->phys_ascent = it->max_phys_ascent;
18521 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18522 row->extra_line_spacing = it->max_extra_line_spacing;
18523 }
18524
18525 /* Compute the width of this line. */
18526 row->pixel_width = row->x;
18527 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18528 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18529
18530 eassert (row->pixel_width >= 0);
18531 eassert (row->ascent >= 0 && row->height > 0);
18532
18533 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18534 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18535
18536 /* If first line's physical ascent is larger than its logical
18537 ascent, use the physical ascent, and make the row taller.
18538 This makes accented characters fully visible. */
18539 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18540 && row->phys_ascent > row->ascent)
18541 {
18542 row->height += row->phys_ascent - row->ascent;
18543 row->ascent = row->phys_ascent;
18544 }
18545
18546 /* Compute how much of the line is visible. */
18547 row->visible_height = row->height;
18548
18549 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18550 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18551
18552 if (row->y < min_y)
18553 row->visible_height -= min_y - row->y;
18554 if (row->y + row->height > max_y)
18555 row->visible_height -= row->y + row->height - max_y;
18556 }
18557 else
18558 {
18559 row->pixel_width = row->used[TEXT_AREA];
18560 if (row->continued_p)
18561 row->pixel_width -= it->continuation_pixel_width;
18562 else if (row->truncated_on_right_p)
18563 row->pixel_width -= it->truncation_pixel_width;
18564 row->ascent = row->phys_ascent = 0;
18565 row->height = row->phys_height = row->visible_height = 1;
18566 row->extra_line_spacing = 0;
18567 }
18568
18569 /* Compute a hash code for this row. */
18570 row->hash = row_hash (row);
18571
18572 it->max_ascent = it->max_descent = 0;
18573 it->max_phys_ascent = it->max_phys_descent = 0;
18574 }
18575
18576
18577 /* Append one space to the glyph row of iterator IT if doing a
18578 window-based redisplay. The space has the same face as
18579 IT->face_id. Value is non-zero if a space was added.
18580
18581 This function is called to make sure that there is always one glyph
18582 at the end of a glyph row that the cursor can be set on under
18583 window-systems. (If there weren't such a glyph we would not know
18584 how wide and tall a box cursor should be displayed).
18585
18586 At the same time this space let's a nicely handle clearing to the
18587 end of the line if the row ends in italic text. */
18588
18589 static int
18590 append_space_for_newline (struct it *it, int default_face_p)
18591 {
18592 if (FRAME_WINDOW_P (it->f))
18593 {
18594 int n = it->glyph_row->used[TEXT_AREA];
18595
18596 if (it->glyph_row->glyphs[TEXT_AREA] + n
18597 < it->glyph_row->glyphs[1 + TEXT_AREA])
18598 {
18599 /* Save some values that must not be changed.
18600 Must save IT->c and IT->len because otherwise
18601 ITERATOR_AT_END_P wouldn't work anymore after
18602 append_space_for_newline has been called. */
18603 enum display_element_type saved_what = it->what;
18604 int saved_c = it->c, saved_len = it->len;
18605 int saved_char_to_display = it->char_to_display;
18606 int saved_x = it->current_x;
18607 int saved_face_id = it->face_id;
18608 int saved_box_end = it->end_of_box_run_p;
18609 struct text_pos saved_pos;
18610 Lisp_Object saved_object;
18611 struct face *face;
18612
18613 saved_object = it->object;
18614 saved_pos = it->position;
18615
18616 it->what = IT_CHARACTER;
18617 memset (&it->position, 0, sizeof it->position);
18618 it->object = make_number (0);
18619 it->c = it->char_to_display = ' ';
18620 it->len = 1;
18621
18622 /* If the default face was remapped, be sure to use the
18623 remapped face for the appended newline. */
18624 if (default_face_p)
18625 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18626 else if (it->face_before_selective_p)
18627 it->face_id = it->saved_face_id;
18628 face = FACE_FROM_ID (it->f, it->face_id);
18629 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18630 /* In R2L rows, we will prepend a stretch glyph that will
18631 have the end_of_box_run_p flag set for it, so there's no
18632 need for the appended newline glyph to have that flag
18633 set. */
18634 if (it->glyph_row->reversed_p
18635 /* But if the appended newline glyph goes all the way to
18636 the end of the row, there will be no stretch glyph,
18637 so leave the box flag set. */
18638 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18639 it->end_of_box_run_p = 0;
18640
18641 PRODUCE_GLYPHS (it);
18642
18643 it->override_ascent = -1;
18644 it->constrain_row_ascent_descent_p = 0;
18645 it->current_x = saved_x;
18646 it->object = saved_object;
18647 it->position = saved_pos;
18648 it->what = saved_what;
18649 it->face_id = saved_face_id;
18650 it->len = saved_len;
18651 it->c = saved_c;
18652 it->char_to_display = saved_char_to_display;
18653 it->end_of_box_run_p = saved_box_end;
18654 return 1;
18655 }
18656 }
18657
18658 return 0;
18659 }
18660
18661
18662 /* Extend the face of the last glyph in the text area of IT->glyph_row
18663 to the end of the display line. Called from display_line. If the
18664 glyph row is empty, add a space glyph to it so that we know the
18665 face to draw. Set the glyph row flag fill_line_p. If the glyph
18666 row is R2L, prepend a stretch glyph to cover the empty space to the
18667 left of the leftmost glyph. */
18668
18669 static void
18670 extend_face_to_end_of_line (struct it *it)
18671 {
18672 struct face *face, *default_face;
18673 struct frame *f = it->f;
18674
18675 /* If line is already filled, do nothing. Non window-system frames
18676 get a grace of one more ``pixel'' because their characters are
18677 1-``pixel'' wide, so they hit the equality too early. This grace
18678 is needed only for R2L rows that are not continued, to produce
18679 one extra blank where we could display the cursor. */
18680 if (it->current_x >= it->last_visible_x
18681 + (!FRAME_WINDOW_P (f)
18682 && it->glyph_row->reversed_p
18683 && !it->glyph_row->continued_p))
18684 return;
18685
18686 /* The default face, possibly remapped. */
18687 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18688
18689 /* Face extension extends the background and box of IT->face_id
18690 to the end of the line. If the background equals the background
18691 of the frame, we don't have to do anything. */
18692 if (it->face_before_selective_p)
18693 face = FACE_FROM_ID (f, it->saved_face_id);
18694 else
18695 face = FACE_FROM_ID (f, it->face_id);
18696
18697 if (FRAME_WINDOW_P (f)
18698 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18699 && face->box == FACE_NO_BOX
18700 && face->background == FRAME_BACKGROUND_PIXEL (f)
18701 && !face->stipple
18702 && !it->glyph_row->reversed_p)
18703 return;
18704
18705 /* Set the glyph row flag indicating that the face of the last glyph
18706 in the text area has to be drawn to the end of the text area. */
18707 it->glyph_row->fill_line_p = 1;
18708
18709 /* If current character of IT is not ASCII, make sure we have the
18710 ASCII face. This will be automatically undone the next time
18711 get_next_display_element returns a multibyte character. Note
18712 that the character will always be single byte in unibyte
18713 text. */
18714 if (!ASCII_CHAR_P (it->c))
18715 {
18716 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18717 }
18718
18719 if (FRAME_WINDOW_P (f))
18720 {
18721 /* If the row is empty, add a space with the current face of IT,
18722 so that we know which face to draw. */
18723 if (it->glyph_row->used[TEXT_AREA] == 0)
18724 {
18725 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18726 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18727 it->glyph_row->used[TEXT_AREA] = 1;
18728 }
18729 #ifdef HAVE_WINDOW_SYSTEM
18730 if (it->glyph_row->reversed_p)
18731 {
18732 /* Prepend a stretch glyph to the row, such that the
18733 rightmost glyph will be drawn flushed all the way to the
18734 right margin of the window. The stretch glyph that will
18735 occupy the empty space, if any, to the left of the
18736 glyphs. */
18737 struct font *font = face->font ? face->font : FRAME_FONT (f);
18738 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18739 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18740 struct glyph *g;
18741 int row_width, stretch_ascent, stretch_width;
18742 struct text_pos saved_pos;
18743 int saved_face_id, saved_avoid_cursor, saved_box_start;
18744
18745 for (row_width = 0, g = row_start; g < row_end; g++)
18746 row_width += g->pixel_width;
18747 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18748 if (stretch_width > 0)
18749 {
18750 stretch_ascent =
18751 (((it->ascent + it->descent)
18752 * FONT_BASE (font)) / FONT_HEIGHT (font));
18753 saved_pos = it->position;
18754 memset (&it->position, 0, sizeof it->position);
18755 saved_avoid_cursor = it->avoid_cursor_p;
18756 it->avoid_cursor_p = 1;
18757 saved_face_id = it->face_id;
18758 saved_box_start = it->start_of_box_run_p;
18759 /* The last row's stretch glyph should get the default
18760 face, to avoid painting the rest of the window with
18761 the region face, if the region ends at ZV. */
18762 if (it->glyph_row->ends_at_zv_p)
18763 it->face_id = default_face->id;
18764 else
18765 it->face_id = face->id;
18766 it->start_of_box_run_p = 0;
18767 append_stretch_glyph (it, make_number (0), stretch_width,
18768 it->ascent + it->descent, stretch_ascent);
18769 it->position = saved_pos;
18770 it->avoid_cursor_p = saved_avoid_cursor;
18771 it->face_id = saved_face_id;
18772 it->start_of_box_run_p = saved_box_start;
18773 }
18774 }
18775 #endif /* HAVE_WINDOW_SYSTEM */
18776 }
18777 else
18778 {
18779 /* Save some values that must not be changed. */
18780 int saved_x = it->current_x;
18781 struct text_pos saved_pos;
18782 Lisp_Object saved_object;
18783 enum display_element_type saved_what = it->what;
18784 int saved_face_id = it->face_id;
18785
18786 saved_object = it->object;
18787 saved_pos = it->position;
18788
18789 it->what = IT_CHARACTER;
18790 memset (&it->position, 0, sizeof it->position);
18791 it->object = make_number (0);
18792 it->c = it->char_to_display = ' ';
18793 it->len = 1;
18794 /* The last row's blank glyphs should get the default face, to
18795 avoid painting the rest of the window with the region face,
18796 if the region ends at ZV. */
18797 if (it->glyph_row->ends_at_zv_p)
18798 it->face_id = default_face->id;
18799 else
18800 it->face_id = face->id;
18801
18802 PRODUCE_GLYPHS (it);
18803
18804 while (it->current_x <= it->last_visible_x)
18805 PRODUCE_GLYPHS (it);
18806
18807 /* Don't count these blanks really. It would let us insert a left
18808 truncation glyph below and make us set the cursor on them, maybe. */
18809 it->current_x = saved_x;
18810 it->object = saved_object;
18811 it->position = saved_pos;
18812 it->what = saved_what;
18813 it->face_id = saved_face_id;
18814 }
18815 }
18816
18817
18818 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18819 trailing whitespace. */
18820
18821 static int
18822 trailing_whitespace_p (ptrdiff_t charpos)
18823 {
18824 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18825 int c = 0;
18826
18827 while (bytepos < ZV_BYTE
18828 && (c = FETCH_CHAR (bytepos),
18829 c == ' ' || c == '\t'))
18830 ++bytepos;
18831
18832 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18833 {
18834 if (bytepos != PT_BYTE)
18835 return 1;
18836 }
18837 return 0;
18838 }
18839
18840
18841 /* Highlight trailing whitespace, if any, in ROW. */
18842
18843 static void
18844 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18845 {
18846 int used = row->used[TEXT_AREA];
18847
18848 if (used)
18849 {
18850 struct glyph *start = row->glyphs[TEXT_AREA];
18851 struct glyph *glyph = start + used - 1;
18852
18853 if (row->reversed_p)
18854 {
18855 /* Right-to-left rows need to be processed in the opposite
18856 direction, so swap the edge pointers. */
18857 glyph = start;
18858 start = row->glyphs[TEXT_AREA] + used - 1;
18859 }
18860
18861 /* Skip over glyphs inserted to display the cursor at the
18862 end of a line, for extending the face of the last glyph
18863 to the end of the line on terminals, and for truncation
18864 and continuation glyphs. */
18865 if (!row->reversed_p)
18866 {
18867 while (glyph >= start
18868 && glyph->type == CHAR_GLYPH
18869 && INTEGERP (glyph->object))
18870 --glyph;
18871 }
18872 else
18873 {
18874 while (glyph <= start
18875 && glyph->type == CHAR_GLYPH
18876 && INTEGERP (glyph->object))
18877 ++glyph;
18878 }
18879
18880 /* If last glyph is a space or stretch, and it's trailing
18881 whitespace, set the face of all trailing whitespace glyphs in
18882 IT->glyph_row to `trailing-whitespace'. */
18883 if ((row->reversed_p ? glyph <= start : glyph >= start)
18884 && BUFFERP (glyph->object)
18885 && (glyph->type == STRETCH_GLYPH
18886 || (glyph->type == CHAR_GLYPH
18887 && glyph->u.ch == ' '))
18888 && trailing_whitespace_p (glyph->charpos))
18889 {
18890 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18891 if (face_id < 0)
18892 return;
18893
18894 if (!row->reversed_p)
18895 {
18896 while (glyph >= start
18897 && BUFFERP (glyph->object)
18898 && (glyph->type == STRETCH_GLYPH
18899 || (glyph->type == CHAR_GLYPH
18900 && glyph->u.ch == ' ')))
18901 (glyph--)->face_id = face_id;
18902 }
18903 else
18904 {
18905 while (glyph <= start
18906 && BUFFERP (glyph->object)
18907 && (glyph->type == STRETCH_GLYPH
18908 || (glyph->type == CHAR_GLYPH
18909 && glyph->u.ch == ' ')))
18910 (glyph++)->face_id = face_id;
18911 }
18912 }
18913 }
18914 }
18915
18916
18917 /* Value is non-zero if glyph row ROW should be
18918 considered to hold the buffer position CHARPOS. */
18919
18920 static int
18921 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18922 {
18923 int result = 1;
18924
18925 if (charpos == CHARPOS (row->end.pos)
18926 || charpos == MATRIX_ROW_END_CHARPOS (row))
18927 {
18928 /* Suppose the row ends on a string.
18929 Unless the row is continued, that means it ends on a newline
18930 in the string. If it's anything other than a display string
18931 (e.g., a before-string from an overlay), we don't want the
18932 cursor there. (This heuristic seems to give the optimal
18933 behavior for the various types of multi-line strings.)
18934 One exception: if the string has `cursor' property on one of
18935 its characters, we _do_ want the cursor there. */
18936 if (CHARPOS (row->end.string_pos) >= 0)
18937 {
18938 if (row->continued_p)
18939 result = 1;
18940 else
18941 {
18942 /* Check for `display' property. */
18943 struct glyph *beg = row->glyphs[TEXT_AREA];
18944 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18945 struct glyph *glyph;
18946
18947 result = 0;
18948 for (glyph = end; glyph >= beg; --glyph)
18949 if (STRINGP (glyph->object))
18950 {
18951 Lisp_Object prop
18952 = Fget_char_property (make_number (charpos),
18953 Qdisplay, Qnil);
18954 result =
18955 (!NILP (prop)
18956 && display_prop_string_p (prop, glyph->object));
18957 /* If there's a `cursor' property on one of the
18958 string's characters, this row is a cursor row,
18959 even though this is not a display string. */
18960 if (!result)
18961 {
18962 Lisp_Object s = glyph->object;
18963
18964 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18965 {
18966 ptrdiff_t gpos = glyph->charpos;
18967
18968 if (!NILP (Fget_char_property (make_number (gpos),
18969 Qcursor, s)))
18970 {
18971 result = 1;
18972 break;
18973 }
18974 }
18975 }
18976 break;
18977 }
18978 }
18979 }
18980 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18981 {
18982 /* If the row ends in middle of a real character,
18983 and the line is continued, we want the cursor here.
18984 That's because CHARPOS (ROW->end.pos) would equal
18985 PT if PT is before the character. */
18986 if (!row->ends_in_ellipsis_p)
18987 result = row->continued_p;
18988 else
18989 /* If the row ends in an ellipsis, then
18990 CHARPOS (ROW->end.pos) will equal point after the
18991 invisible text. We want that position to be displayed
18992 after the ellipsis. */
18993 result = 0;
18994 }
18995 /* If the row ends at ZV, display the cursor at the end of that
18996 row instead of at the start of the row below. */
18997 else if (row->ends_at_zv_p)
18998 result = 1;
18999 else
19000 result = 0;
19001 }
19002
19003 return result;
19004 }
19005
19006 /* Value is non-zero if glyph row ROW should be
19007 used to hold the cursor. */
19008
19009 static int
19010 cursor_row_p (struct glyph_row *row)
19011 {
19012 return row_for_charpos_p (row, PT);
19013 }
19014
19015 \f
19016
19017 /* Push the property PROP so that it will be rendered at the current
19018 position in IT. Return 1 if PROP was successfully pushed, 0
19019 otherwise. Called from handle_line_prefix to handle the
19020 `line-prefix' and `wrap-prefix' properties. */
19021
19022 static int
19023 push_prefix_prop (struct it *it, Lisp_Object prop)
19024 {
19025 struct text_pos pos =
19026 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19027
19028 eassert (it->method == GET_FROM_BUFFER
19029 || it->method == GET_FROM_DISPLAY_VECTOR
19030 || it->method == GET_FROM_STRING);
19031
19032 /* We need to save the current buffer/string position, so it will be
19033 restored by pop_it, because iterate_out_of_display_property
19034 depends on that being set correctly, but some situations leave
19035 it->position not yet set when this function is called. */
19036 push_it (it, &pos);
19037
19038 if (STRINGP (prop))
19039 {
19040 if (SCHARS (prop) == 0)
19041 {
19042 pop_it (it);
19043 return 0;
19044 }
19045
19046 it->string = prop;
19047 it->string_from_prefix_prop_p = 1;
19048 it->multibyte_p = STRING_MULTIBYTE (it->string);
19049 it->current.overlay_string_index = -1;
19050 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19051 it->end_charpos = it->string_nchars = SCHARS (it->string);
19052 it->method = GET_FROM_STRING;
19053 it->stop_charpos = 0;
19054 it->prev_stop = 0;
19055 it->base_level_stop = 0;
19056
19057 /* Force paragraph direction to be that of the parent
19058 buffer/string. */
19059 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19060 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19061 else
19062 it->paragraph_embedding = L2R;
19063
19064 /* Set up the bidi iterator for this display string. */
19065 if (it->bidi_p)
19066 {
19067 it->bidi_it.string.lstring = it->string;
19068 it->bidi_it.string.s = NULL;
19069 it->bidi_it.string.schars = it->end_charpos;
19070 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19071 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19072 it->bidi_it.string.unibyte = !it->multibyte_p;
19073 it->bidi_it.w = it->w;
19074 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19075 }
19076 }
19077 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19078 {
19079 it->method = GET_FROM_STRETCH;
19080 it->object = prop;
19081 }
19082 #ifdef HAVE_WINDOW_SYSTEM
19083 else if (IMAGEP (prop))
19084 {
19085 it->what = IT_IMAGE;
19086 it->image_id = lookup_image (it->f, prop);
19087 it->method = GET_FROM_IMAGE;
19088 }
19089 #endif /* HAVE_WINDOW_SYSTEM */
19090 else
19091 {
19092 pop_it (it); /* bogus display property, give up */
19093 return 0;
19094 }
19095
19096 return 1;
19097 }
19098
19099 /* Return the character-property PROP at the current position in IT. */
19100
19101 static Lisp_Object
19102 get_it_property (struct it *it, Lisp_Object prop)
19103 {
19104 Lisp_Object position, object = it->object;
19105
19106 if (STRINGP (object))
19107 position = make_number (IT_STRING_CHARPOS (*it));
19108 else if (BUFFERP (object))
19109 {
19110 position = make_number (IT_CHARPOS (*it));
19111 object = it->window;
19112 }
19113 else
19114 return Qnil;
19115
19116 return Fget_char_property (position, prop, object);
19117 }
19118
19119 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19120
19121 static void
19122 handle_line_prefix (struct it *it)
19123 {
19124 Lisp_Object prefix;
19125
19126 if (it->continuation_lines_width > 0)
19127 {
19128 prefix = get_it_property (it, Qwrap_prefix);
19129 if (NILP (prefix))
19130 prefix = Vwrap_prefix;
19131 }
19132 else
19133 {
19134 prefix = get_it_property (it, Qline_prefix);
19135 if (NILP (prefix))
19136 prefix = Vline_prefix;
19137 }
19138 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19139 {
19140 /* If the prefix is wider than the window, and we try to wrap
19141 it, it would acquire its own wrap prefix, and so on till the
19142 iterator stack overflows. So, don't wrap the prefix. */
19143 it->line_wrap = TRUNCATE;
19144 it->avoid_cursor_p = 1;
19145 }
19146 }
19147
19148 \f
19149
19150 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19151 only for R2L lines from display_line and display_string, when they
19152 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19153 the line/string needs to be continued on the next glyph row. */
19154 static void
19155 unproduce_glyphs (struct it *it, int n)
19156 {
19157 struct glyph *glyph, *end;
19158
19159 eassert (it->glyph_row);
19160 eassert (it->glyph_row->reversed_p);
19161 eassert (it->area == TEXT_AREA);
19162 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19163
19164 if (n > it->glyph_row->used[TEXT_AREA])
19165 n = it->glyph_row->used[TEXT_AREA];
19166 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19167 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19168 for ( ; glyph < end; glyph++)
19169 glyph[-n] = *glyph;
19170 }
19171
19172 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19173 and ROW->maxpos. */
19174 static void
19175 find_row_edges (struct it *it, struct glyph_row *row,
19176 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19177 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19178 {
19179 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19180 lines' rows is implemented for bidi-reordered rows. */
19181
19182 /* ROW->minpos is the value of min_pos, the minimal buffer position
19183 we have in ROW, or ROW->start.pos if that is smaller. */
19184 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19185 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19186 else
19187 /* We didn't find buffer positions smaller than ROW->start, or
19188 didn't find _any_ valid buffer positions in any of the glyphs,
19189 so we must trust the iterator's computed positions. */
19190 row->minpos = row->start.pos;
19191 if (max_pos <= 0)
19192 {
19193 max_pos = CHARPOS (it->current.pos);
19194 max_bpos = BYTEPOS (it->current.pos);
19195 }
19196
19197 /* Here are the various use-cases for ending the row, and the
19198 corresponding values for ROW->maxpos:
19199
19200 Line ends in a newline from buffer eol_pos + 1
19201 Line is continued from buffer max_pos + 1
19202 Line is truncated on right it->current.pos
19203 Line ends in a newline from string max_pos + 1(*)
19204 (*) + 1 only when line ends in a forward scan
19205 Line is continued from string max_pos
19206 Line is continued from display vector max_pos
19207 Line is entirely from a string min_pos == max_pos
19208 Line is entirely from a display vector min_pos == max_pos
19209 Line that ends at ZV ZV
19210
19211 If you discover other use-cases, please add them here as
19212 appropriate. */
19213 if (row->ends_at_zv_p)
19214 row->maxpos = it->current.pos;
19215 else if (row->used[TEXT_AREA])
19216 {
19217 int seen_this_string = 0;
19218 struct glyph_row *r1 = row - 1;
19219
19220 /* Did we see the same display string on the previous row? */
19221 if (STRINGP (it->object)
19222 /* this is not the first row */
19223 && row > it->w->desired_matrix->rows
19224 /* previous row is not the header line */
19225 && !r1->mode_line_p
19226 /* previous row also ends in a newline from a string */
19227 && r1->ends_in_newline_from_string_p)
19228 {
19229 struct glyph *start, *end;
19230
19231 /* Search for the last glyph of the previous row that came
19232 from buffer or string. Depending on whether the row is
19233 L2R or R2L, we need to process it front to back or the
19234 other way round. */
19235 if (!r1->reversed_p)
19236 {
19237 start = r1->glyphs[TEXT_AREA];
19238 end = start + r1->used[TEXT_AREA];
19239 /* Glyphs inserted by redisplay have an integer (zero)
19240 as their object. */
19241 while (end > start
19242 && INTEGERP ((end - 1)->object)
19243 && (end - 1)->charpos <= 0)
19244 --end;
19245 if (end > start)
19246 {
19247 if (EQ ((end - 1)->object, it->object))
19248 seen_this_string = 1;
19249 }
19250 else
19251 /* If all the glyphs of the previous row were inserted
19252 by redisplay, it means the previous row was
19253 produced from a single newline, which is only
19254 possible if that newline came from the same string
19255 as the one which produced this ROW. */
19256 seen_this_string = 1;
19257 }
19258 else
19259 {
19260 end = r1->glyphs[TEXT_AREA] - 1;
19261 start = end + r1->used[TEXT_AREA];
19262 while (end < start
19263 && INTEGERP ((end + 1)->object)
19264 && (end + 1)->charpos <= 0)
19265 ++end;
19266 if (end < start)
19267 {
19268 if (EQ ((end + 1)->object, it->object))
19269 seen_this_string = 1;
19270 }
19271 else
19272 seen_this_string = 1;
19273 }
19274 }
19275 /* Take note of each display string that covers a newline only
19276 once, the first time we see it. This is for when a display
19277 string includes more than one newline in it. */
19278 if (row->ends_in_newline_from_string_p && !seen_this_string)
19279 {
19280 /* If we were scanning the buffer forward when we displayed
19281 the string, we want to account for at least one buffer
19282 position that belongs to this row (position covered by
19283 the display string), so that cursor positioning will
19284 consider this row as a candidate when point is at the end
19285 of the visual line represented by this row. This is not
19286 required when scanning back, because max_pos will already
19287 have a much larger value. */
19288 if (CHARPOS (row->end.pos) > max_pos)
19289 INC_BOTH (max_pos, max_bpos);
19290 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19291 }
19292 else if (CHARPOS (it->eol_pos) > 0)
19293 SET_TEXT_POS (row->maxpos,
19294 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19295 else if (row->continued_p)
19296 {
19297 /* If max_pos is different from IT's current position, it
19298 means IT->method does not belong to the display element
19299 at max_pos. However, it also means that the display
19300 element at max_pos was displayed in its entirety on this
19301 line, which is equivalent to saying that the next line
19302 starts at the next buffer position. */
19303 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19304 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19305 else
19306 {
19307 INC_BOTH (max_pos, max_bpos);
19308 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19309 }
19310 }
19311 else if (row->truncated_on_right_p)
19312 /* display_line already called reseat_at_next_visible_line_start,
19313 which puts the iterator at the beginning of the next line, in
19314 the logical order. */
19315 row->maxpos = it->current.pos;
19316 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19317 /* A line that is entirely from a string/image/stretch... */
19318 row->maxpos = row->minpos;
19319 else
19320 emacs_abort ();
19321 }
19322 else
19323 row->maxpos = it->current.pos;
19324 }
19325
19326 /* Construct the glyph row IT->glyph_row in the desired matrix of
19327 IT->w from text at the current position of IT. See dispextern.h
19328 for an overview of struct it. Value is non-zero if
19329 IT->glyph_row displays text, as opposed to a line displaying ZV
19330 only. */
19331
19332 static int
19333 display_line (struct it *it)
19334 {
19335 struct glyph_row *row = it->glyph_row;
19336 Lisp_Object overlay_arrow_string;
19337 struct it wrap_it;
19338 void *wrap_data = NULL;
19339 int may_wrap = 0, wrap_x IF_LINT (= 0);
19340 int wrap_row_used = -1;
19341 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19342 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19343 int wrap_row_extra_line_spacing IF_LINT (= 0);
19344 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19345 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19346 int cvpos;
19347 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19348 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19349
19350 /* We always start displaying at hpos zero even if hscrolled. */
19351 eassert (it->hpos == 0 && it->current_x == 0);
19352
19353 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19354 >= it->w->desired_matrix->nrows)
19355 {
19356 it->w->nrows_scale_factor++;
19357 fonts_changed_p = 1;
19358 return 0;
19359 }
19360
19361 /* Is IT->w showing the region? */
19362 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19363
19364 /* Clear the result glyph row and enable it. */
19365 prepare_desired_row (row);
19366
19367 row->y = it->current_y;
19368 row->start = it->start;
19369 row->continuation_lines_width = it->continuation_lines_width;
19370 row->displays_text_p = 1;
19371 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19372 it->starts_in_middle_of_char_p = 0;
19373
19374 /* Arrange the overlays nicely for our purposes. Usually, we call
19375 display_line on only one line at a time, in which case this
19376 can't really hurt too much, or we call it on lines which appear
19377 one after another in the buffer, in which case all calls to
19378 recenter_overlay_lists but the first will be pretty cheap. */
19379 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19380
19381 /* Move over display elements that are not visible because we are
19382 hscrolled. This may stop at an x-position < IT->first_visible_x
19383 if the first glyph is partially visible or if we hit a line end. */
19384 if (it->current_x < it->first_visible_x)
19385 {
19386 enum move_it_result move_result;
19387
19388 this_line_min_pos = row->start.pos;
19389 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19390 MOVE_TO_POS | MOVE_TO_X);
19391 /* If we are under a large hscroll, move_it_in_display_line_to
19392 could hit the end of the line without reaching
19393 it->first_visible_x. Pretend that we did reach it. This is
19394 especially important on a TTY, where we will call
19395 extend_face_to_end_of_line, which needs to know how many
19396 blank glyphs to produce. */
19397 if (it->current_x < it->first_visible_x
19398 && (move_result == MOVE_NEWLINE_OR_CR
19399 || move_result == MOVE_POS_MATCH_OR_ZV))
19400 it->current_x = it->first_visible_x;
19401
19402 /* Record the smallest positions seen while we moved over
19403 display elements that are not visible. This is needed by
19404 redisplay_internal for optimizing the case where the cursor
19405 stays inside the same line. The rest of this function only
19406 considers positions that are actually displayed, so
19407 RECORD_MAX_MIN_POS will not otherwise record positions that
19408 are hscrolled to the left of the left edge of the window. */
19409 min_pos = CHARPOS (this_line_min_pos);
19410 min_bpos = BYTEPOS (this_line_min_pos);
19411 }
19412 else
19413 {
19414 /* We only do this when not calling `move_it_in_display_line_to'
19415 above, because move_it_in_display_line_to calls
19416 handle_line_prefix itself. */
19417 handle_line_prefix (it);
19418 }
19419
19420 /* Get the initial row height. This is either the height of the
19421 text hscrolled, if there is any, or zero. */
19422 row->ascent = it->max_ascent;
19423 row->height = it->max_ascent + it->max_descent;
19424 row->phys_ascent = it->max_phys_ascent;
19425 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19426 row->extra_line_spacing = it->max_extra_line_spacing;
19427
19428 /* Utility macro to record max and min buffer positions seen until now. */
19429 #define RECORD_MAX_MIN_POS(IT) \
19430 do \
19431 { \
19432 int composition_p = !STRINGP ((IT)->string) \
19433 && ((IT)->what == IT_COMPOSITION); \
19434 ptrdiff_t current_pos = \
19435 composition_p ? (IT)->cmp_it.charpos \
19436 : IT_CHARPOS (*(IT)); \
19437 ptrdiff_t current_bpos = \
19438 composition_p ? CHAR_TO_BYTE (current_pos) \
19439 : IT_BYTEPOS (*(IT)); \
19440 if (current_pos < min_pos) \
19441 { \
19442 min_pos = current_pos; \
19443 min_bpos = current_bpos; \
19444 } \
19445 if (IT_CHARPOS (*it) > max_pos) \
19446 { \
19447 max_pos = IT_CHARPOS (*it); \
19448 max_bpos = IT_BYTEPOS (*it); \
19449 } \
19450 } \
19451 while (0)
19452
19453 /* Loop generating characters. The loop is left with IT on the next
19454 character to display. */
19455 while (1)
19456 {
19457 int n_glyphs_before, hpos_before, x_before;
19458 int x, nglyphs;
19459 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19460
19461 /* Retrieve the next thing to display. Value is zero if end of
19462 buffer reached. */
19463 if (!get_next_display_element (it))
19464 {
19465 /* Maybe add a space at the end of this line that is used to
19466 display the cursor there under X. Set the charpos of the
19467 first glyph of blank lines not corresponding to any text
19468 to -1. */
19469 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19470 row->exact_window_width_line_p = 1;
19471 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19472 || row->used[TEXT_AREA] == 0)
19473 {
19474 row->glyphs[TEXT_AREA]->charpos = -1;
19475 row->displays_text_p = 0;
19476
19477 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19478 && (!MINI_WINDOW_P (it->w)
19479 || (minibuf_level && EQ (it->window, minibuf_window))))
19480 row->indicate_empty_line_p = 1;
19481 }
19482
19483 it->continuation_lines_width = 0;
19484 row->ends_at_zv_p = 1;
19485 /* A row that displays right-to-left text must always have
19486 its last face extended all the way to the end of line,
19487 even if this row ends in ZV, because we still write to
19488 the screen left to right. We also need to extend the
19489 last face if the default face is remapped to some
19490 different face, otherwise the functions that clear
19491 portions of the screen will clear with the default face's
19492 background color. */
19493 if (row->reversed_p
19494 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19495 extend_face_to_end_of_line (it);
19496 break;
19497 }
19498
19499 /* Now, get the metrics of what we want to display. This also
19500 generates glyphs in `row' (which is IT->glyph_row). */
19501 n_glyphs_before = row->used[TEXT_AREA];
19502 x = it->current_x;
19503
19504 /* Remember the line height so far in case the next element doesn't
19505 fit on the line. */
19506 if (it->line_wrap != TRUNCATE)
19507 {
19508 ascent = it->max_ascent;
19509 descent = it->max_descent;
19510 phys_ascent = it->max_phys_ascent;
19511 phys_descent = it->max_phys_descent;
19512
19513 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19514 {
19515 if (IT_DISPLAYING_WHITESPACE (it))
19516 may_wrap = 1;
19517 else if (may_wrap)
19518 {
19519 SAVE_IT (wrap_it, *it, wrap_data);
19520 wrap_x = x;
19521 wrap_row_used = row->used[TEXT_AREA];
19522 wrap_row_ascent = row->ascent;
19523 wrap_row_height = row->height;
19524 wrap_row_phys_ascent = row->phys_ascent;
19525 wrap_row_phys_height = row->phys_height;
19526 wrap_row_extra_line_spacing = row->extra_line_spacing;
19527 wrap_row_min_pos = min_pos;
19528 wrap_row_min_bpos = min_bpos;
19529 wrap_row_max_pos = max_pos;
19530 wrap_row_max_bpos = max_bpos;
19531 may_wrap = 0;
19532 }
19533 }
19534 }
19535
19536 PRODUCE_GLYPHS (it);
19537
19538 /* If this display element was in marginal areas, continue with
19539 the next one. */
19540 if (it->area != TEXT_AREA)
19541 {
19542 row->ascent = max (row->ascent, it->max_ascent);
19543 row->height = max (row->height, it->max_ascent + it->max_descent);
19544 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19545 row->phys_height = max (row->phys_height,
19546 it->max_phys_ascent + it->max_phys_descent);
19547 row->extra_line_spacing = max (row->extra_line_spacing,
19548 it->max_extra_line_spacing);
19549 set_iterator_to_next (it, 1);
19550 continue;
19551 }
19552
19553 /* Does the display element fit on the line? If we truncate
19554 lines, we should draw past the right edge of the window. If
19555 we don't truncate, we want to stop so that we can display the
19556 continuation glyph before the right margin. If lines are
19557 continued, there are two possible strategies for characters
19558 resulting in more than 1 glyph (e.g. tabs): Display as many
19559 glyphs as possible in this line and leave the rest for the
19560 continuation line, or display the whole element in the next
19561 line. Original redisplay did the former, so we do it also. */
19562 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19563 hpos_before = it->hpos;
19564 x_before = x;
19565
19566 if (/* Not a newline. */
19567 nglyphs > 0
19568 /* Glyphs produced fit entirely in the line. */
19569 && it->current_x < it->last_visible_x)
19570 {
19571 it->hpos += nglyphs;
19572 row->ascent = max (row->ascent, it->max_ascent);
19573 row->height = max (row->height, it->max_ascent + it->max_descent);
19574 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19575 row->phys_height = max (row->phys_height,
19576 it->max_phys_ascent + it->max_phys_descent);
19577 row->extra_line_spacing = max (row->extra_line_spacing,
19578 it->max_extra_line_spacing);
19579 if (it->current_x - it->pixel_width < it->first_visible_x)
19580 row->x = x - it->first_visible_x;
19581 /* Record the maximum and minimum buffer positions seen so
19582 far in glyphs that will be displayed by this row. */
19583 if (it->bidi_p)
19584 RECORD_MAX_MIN_POS (it);
19585 }
19586 else
19587 {
19588 int i, new_x;
19589 struct glyph *glyph;
19590
19591 for (i = 0; i < nglyphs; ++i, x = new_x)
19592 {
19593 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19594 new_x = x + glyph->pixel_width;
19595
19596 if (/* Lines are continued. */
19597 it->line_wrap != TRUNCATE
19598 && (/* Glyph doesn't fit on the line. */
19599 new_x > it->last_visible_x
19600 /* Or it fits exactly on a window system frame. */
19601 || (new_x == it->last_visible_x
19602 && FRAME_WINDOW_P (it->f)
19603 && (row->reversed_p
19604 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19605 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19606 {
19607 /* End of a continued line. */
19608
19609 if (it->hpos == 0
19610 || (new_x == it->last_visible_x
19611 && FRAME_WINDOW_P (it->f)
19612 && (row->reversed_p
19613 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19614 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19615 {
19616 /* Current glyph is the only one on the line or
19617 fits exactly on the line. We must continue
19618 the line because we can't draw the cursor
19619 after the glyph. */
19620 row->continued_p = 1;
19621 it->current_x = new_x;
19622 it->continuation_lines_width += new_x;
19623 ++it->hpos;
19624 if (i == nglyphs - 1)
19625 {
19626 /* If line-wrap is on, check if a previous
19627 wrap point was found. */
19628 if (wrap_row_used > 0
19629 /* Even if there is a previous wrap
19630 point, continue the line here as
19631 usual, if (i) the previous character
19632 was a space or tab AND (ii) the
19633 current character is not. */
19634 && (!may_wrap
19635 || IT_DISPLAYING_WHITESPACE (it)))
19636 goto back_to_wrap;
19637
19638 /* Record the maximum and minimum buffer
19639 positions seen so far in glyphs that will be
19640 displayed by this row. */
19641 if (it->bidi_p)
19642 RECORD_MAX_MIN_POS (it);
19643 set_iterator_to_next (it, 1);
19644 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19645 {
19646 if (!get_next_display_element (it))
19647 {
19648 row->exact_window_width_line_p = 1;
19649 it->continuation_lines_width = 0;
19650 row->continued_p = 0;
19651 row->ends_at_zv_p = 1;
19652 }
19653 else if (ITERATOR_AT_END_OF_LINE_P (it))
19654 {
19655 row->continued_p = 0;
19656 row->exact_window_width_line_p = 1;
19657 }
19658 }
19659 }
19660 else if (it->bidi_p)
19661 RECORD_MAX_MIN_POS (it);
19662 }
19663 else if (CHAR_GLYPH_PADDING_P (*glyph)
19664 && !FRAME_WINDOW_P (it->f))
19665 {
19666 /* A padding glyph that doesn't fit on this line.
19667 This means the whole character doesn't fit
19668 on the line. */
19669 if (row->reversed_p)
19670 unproduce_glyphs (it, row->used[TEXT_AREA]
19671 - n_glyphs_before);
19672 row->used[TEXT_AREA] = n_glyphs_before;
19673
19674 /* Fill the rest of the row with continuation
19675 glyphs like in 20.x. */
19676 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19677 < row->glyphs[1 + TEXT_AREA])
19678 produce_special_glyphs (it, IT_CONTINUATION);
19679
19680 row->continued_p = 1;
19681 it->current_x = x_before;
19682 it->continuation_lines_width += x_before;
19683
19684 /* Restore the height to what it was before the
19685 element not fitting on the line. */
19686 it->max_ascent = ascent;
19687 it->max_descent = descent;
19688 it->max_phys_ascent = phys_ascent;
19689 it->max_phys_descent = phys_descent;
19690 }
19691 else if (wrap_row_used > 0)
19692 {
19693 back_to_wrap:
19694 if (row->reversed_p)
19695 unproduce_glyphs (it,
19696 row->used[TEXT_AREA] - wrap_row_used);
19697 RESTORE_IT (it, &wrap_it, wrap_data);
19698 it->continuation_lines_width += wrap_x;
19699 row->used[TEXT_AREA] = wrap_row_used;
19700 row->ascent = wrap_row_ascent;
19701 row->height = wrap_row_height;
19702 row->phys_ascent = wrap_row_phys_ascent;
19703 row->phys_height = wrap_row_phys_height;
19704 row->extra_line_spacing = wrap_row_extra_line_spacing;
19705 min_pos = wrap_row_min_pos;
19706 min_bpos = wrap_row_min_bpos;
19707 max_pos = wrap_row_max_pos;
19708 max_bpos = wrap_row_max_bpos;
19709 row->continued_p = 1;
19710 row->ends_at_zv_p = 0;
19711 row->exact_window_width_line_p = 0;
19712 it->continuation_lines_width += x;
19713
19714 /* Make sure that a non-default face is extended
19715 up to the right margin of the window. */
19716 extend_face_to_end_of_line (it);
19717 }
19718 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19719 {
19720 /* A TAB that extends past the right edge of the
19721 window. This produces a single glyph on
19722 window system frames. We leave the glyph in
19723 this row and let it fill the row, but don't
19724 consume the TAB. */
19725 if ((row->reversed_p
19726 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19727 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19728 produce_special_glyphs (it, IT_CONTINUATION);
19729 it->continuation_lines_width += it->last_visible_x;
19730 row->ends_in_middle_of_char_p = 1;
19731 row->continued_p = 1;
19732 glyph->pixel_width = it->last_visible_x - x;
19733 it->starts_in_middle_of_char_p = 1;
19734 }
19735 else
19736 {
19737 /* Something other than a TAB that draws past
19738 the right edge of the window. Restore
19739 positions to values before the element. */
19740 if (row->reversed_p)
19741 unproduce_glyphs (it, row->used[TEXT_AREA]
19742 - (n_glyphs_before + i));
19743 row->used[TEXT_AREA] = n_glyphs_before + i;
19744
19745 /* Display continuation glyphs. */
19746 it->current_x = x_before;
19747 it->continuation_lines_width += x;
19748 if (!FRAME_WINDOW_P (it->f)
19749 || (row->reversed_p
19750 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19751 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19752 produce_special_glyphs (it, IT_CONTINUATION);
19753 row->continued_p = 1;
19754
19755 extend_face_to_end_of_line (it);
19756
19757 if (nglyphs > 1 && i > 0)
19758 {
19759 row->ends_in_middle_of_char_p = 1;
19760 it->starts_in_middle_of_char_p = 1;
19761 }
19762
19763 /* Restore the height to what it was before the
19764 element not fitting on the line. */
19765 it->max_ascent = ascent;
19766 it->max_descent = descent;
19767 it->max_phys_ascent = phys_ascent;
19768 it->max_phys_descent = phys_descent;
19769 }
19770
19771 break;
19772 }
19773 else if (new_x > it->first_visible_x)
19774 {
19775 /* Increment number of glyphs actually displayed. */
19776 ++it->hpos;
19777
19778 /* Record the maximum and minimum buffer positions
19779 seen so far in glyphs that will be displayed by
19780 this row. */
19781 if (it->bidi_p)
19782 RECORD_MAX_MIN_POS (it);
19783
19784 if (x < it->first_visible_x)
19785 /* Glyph is partially visible, i.e. row starts at
19786 negative X position. */
19787 row->x = x - it->first_visible_x;
19788 }
19789 else
19790 {
19791 /* Glyph is completely off the left margin of the
19792 window. This should not happen because of the
19793 move_it_in_display_line at the start of this
19794 function, unless the text display area of the
19795 window is empty. */
19796 eassert (it->first_visible_x <= it->last_visible_x);
19797 }
19798 }
19799 /* Even if this display element produced no glyphs at all,
19800 we want to record its position. */
19801 if (it->bidi_p && nglyphs == 0)
19802 RECORD_MAX_MIN_POS (it);
19803
19804 row->ascent = max (row->ascent, it->max_ascent);
19805 row->height = max (row->height, it->max_ascent + it->max_descent);
19806 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19807 row->phys_height = max (row->phys_height,
19808 it->max_phys_ascent + it->max_phys_descent);
19809 row->extra_line_spacing = max (row->extra_line_spacing,
19810 it->max_extra_line_spacing);
19811
19812 /* End of this display line if row is continued. */
19813 if (row->continued_p || row->ends_at_zv_p)
19814 break;
19815 }
19816
19817 at_end_of_line:
19818 /* Is this a line end? If yes, we're also done, after making
19819 sure that a non-default face is extended up to the right
19820 margin of the window. */
19821 if (ITERATOR_AT_END_OF_LINE_P (it))
19822 {
19823 int used_before = row->used[TEXT_AREA];
19824
19825 row->ends_in_newline_from_string_p = STRINGP (it->object);
19826
19827 /* Add a space at the end of the line that is used to
19828 display the cursor there. */
19829 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19830 append_space_for_newline (it, 0);
19831
19832 /* Extend the face to the end of the line. */
19833 extend_face_to_end_of_line (it);
19834
19835 /* Make sure we have the position. */
19836 if (used_before == 0)
19837 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19838
19839 /* Record the position of the newline, for use in
19840 find_row_edges. */
19841 it->eol_pos = it->current.pos;
19842
19843 /* Consume the line end. This skips over invisible lines. */
19844 set_iterator_to_next (it, 1);
19845 it->continuation_lines_width = 0;
19846 break;
19847 }
19848
19849 /* Proceed with next display element. Note that this skips
19850 over lines invisible because of selective display. */
19851 set_iterator_to_next (it, 1);
19852
19853 /* If we truncate lines, we are done when the last displayed
19854 glyphs reach past the right margin of the window. */
19855 if (it->line_wrap == TRUNCATE
19856 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19857 ? (it->current_x >= it->last_visible_x)
19858 : (it->current_x > it->last_visible_x)))
19859 {
19860 /* Maybe add truncation glyphs. */
19861 if (!FRAME_WINDOW_P (it->f)
19862 || (row->reversed_p
19863 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19864 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19865 {
19866 int i, n;
19867
19868 if (!row->reversed_p)
19869 {
19870 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19871 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19872 break;
19873 }
19874 else
19875 {
19876 for (i = 0; i < row->used[TEXT_AREA]; i++)
19877 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19878 break;
19879 /* Remove any padding glyphs at the front of ROW, to
19880 make room for the truncation glyphs we will be
19881 adding below. The loop below always inserts at
19882 least one truncation glyph, so also remove the
19883 last glyph added to ROW. */
19884 unproduce_glyphs (it, i + 1);
19885 /* Adjust i for the loop below. */
19886 i = row->used[TEXT_AREA] - (i + 1);
19887 }
19888
19889 it->current_x = x_before;
19890 if (!FRAME_WINDOW_P (it->f))
19891 {
19892 for (n = row->used[TEXT_AREA]; i < n; ++i)
19893 {
19894 row->used[TEXT_AREA] = i;
19895 produce_special_glyphs (it, IT_TRUNCATION);
19896 }
19897 }
19898 else
19899 {
19900 row->used[TEXT_AREA] = i;
19901 produce_special_glyphs (it, IT_TRUNCATION);
19902 }
19903 }
19904 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19905 {
19906 /* Don't truncate if we can overflow newline into fringe. */
19907 if (!get_next_display_element (it))
19908 {
19909 it->continuation_lines_width = 0;
19910 row->ends_at_zv_p = 1;
19911 row->exact_window_width_line_p = 1;
19912 break;
19913 }
19914 if (ITERATOR_AT_END_OF_LINE_P (it))
19915 {
19916 row->exact_window_width_line_p = 1;
19917 goto at_end_of_line;
19918 }
19919 it->current_x = x_before;
19920 }
19921
19922 row->truncated_on_right_p = 1;
19923 it->continuation_lines_width = 0;
19924 reseat_at_next_visible_line_start (it, 0);
19925 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19926 it->hpos = hpos_before;
19927 break;
19928 }
19929 }
19930
19931 if (wrap_data)
19932 bidi_unshelve_cache (wrap_data, 1);
19933
19934 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19935 at the left window margin. */
19936 if (it->first_visible_x
19937 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19938 {
19939 if (!FRAME_WINDOW_P (it->f)
19940 || (row->reversed_p
19941 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19942 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19943 insert_left_trunc_glyphs (it);
19944 row->truncated_on_left_p = 1;
19945 }
19946
19947 /* Remember the position at which this line ends.
19948
19949 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19950 cannot be before the call to find_row_edges below, since that is
19951 where these positions are determined. */
19952 row->end = it->current;
19953 if (!it->bidi_p)
19954 {
19955 row->minpos = row->start.pos;
19956 row->maxpos = row->end.pos;
19957 }
19958 else
19959 {
19960 /* ROW->minpos and ROW->maxpos must be the smallest and
19961 `1 + the largest' buffer positions in ROW. But if ROW was
19962 bidi-reordered, these two positions can be anywhere in the
19963 row, so we must determine them now. */
19964 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19965 }
19966
19967 /* If the start of this line is the overlay arrow-position, then
19968 mark this glyph row as the one containing the overlay arrow.
19969 This is clearly a mess with variable size fonts. It would be
19970 better to let it be displayed like cursors under X. */
19971 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19972 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19973 !NILP (overlay_arrow_string)))
19974 {
19975 /* Overlay arrow in window redisplay is a fringe bitmap. */
19976 if (STRINGP (overlay_arrow_string))
19977 {
19978 struct glyph_row *arrow_row
19979 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19980 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19981 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19982 struct glyph *p = row->glyphs[TEXT_AREA];
19983 struct glyph *p2, *end;
19984
19985 /* Copy the arrow glyphs. */
19986 while (glyph < arrow_end)
19987 *p++ = *glyph++;
19988
19989 /* Throw away padding glyphs. */
19990 p2 = p;
19991 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19992 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19993 ++p2;
19994 if (p2 > p)
19995 {
19996 while (p2 < end)
19997 *p++ = *p2++;
19998 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19999 }
20000 }
20001 else
20002 {
20003 eassert (INTEGERP (overlay_arrow_string));
20004 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20005 }
20006 overlay_arrow_seen = 1;
20007 }
20008
20009 /* Highlight trailing whitespace. */
20010 if (!NILP (Vshow_trailing_whitespace))
20011 highlight_trailing_whitespace (it->f, it->glyph_row);
20012
20013 /* Compute pixel dimensions of this line. */
20014 compute_line_metrics (it);
20015
20016 /* Implementation note: No changes in the glyphs of ROW or in their
20017 faces can be done past this point, because compute_line_metrics
20018 computes ROW's hash value and stores it within the glyph_row
20019 structure. */
20020
20021 /* Record whether this row ends inside an ellipsis. */
20022 row->ends_in_ellipsis_p
20023 = (it->method == GET_FROM_DISPLAY_VECTOR
20024 && it->ellipsis_p);
20025
20026 /* Save fringe bitmaps in this row. */
20027 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20028 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20029 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20030 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20031
20032 it->left_user_fringe_bitmap = 0;
20033 it->left_user_fringe_face_id = 0;
20034 it->right_user_fringe_bitmap = 0;
20035 it->right_user_fringe_face_id = 0;
20036
20037 /* Maybe set the cursor. */
20038 cvpos = it->w->cursor.vpos;
20039 if ((cvpos < 0
20040 /* In bidi-reordered rows, keep checking for proper cursor
20041 position even if one has been found already, because buffer
20042 positions in such rows change non-linearly with ROW->VPOS,
20043 when a line is continued. One exception: when we are at ZV,
20044 display cursor on the first suitable glyph row, since all
20045 the empty rows after that also have their position set to ZV. */
20046 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20047 lines' rows is implemented for bidi-reordered rows. */
20048 || (it->bidi_p
20049 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20050 && PT >= MATRIX_ROW_START_CHARPOS (row)
20051 && PT <= MATRIX_ROW_END_CHARPOS (row)
20052 && cursor_row_p (row))
20053 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20054
20055 /* Prepare for the next line. This line starts horizontally at (X
20056 HPOS) = (0 0). Vertical positions are incremented. As a
20057 convenience for the caller, IT->glyph_row is set to the next
20058 row to be used. */
20059 it->current_x = it->hpos = 0;
20060 it->current_y += row->height;
20061 SET_TEXT_POS (it->eol_pos, 0, 0);
20062 ++it->vpos;
20063 ++it->glyph_row;
20064 /* The next row should by default use the same value of the
20065 reversed_p flag as this one. set_iterator_to_next decides when
20066 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20067 the flag accordingly. */
20068 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20069 it->glyph_row->reversed_p = row->reversed_p;
20070 it->start = row->end;
20071 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20072
20073 #undef RECORD_MAX_MIN_POS
20074 }
20075
20076 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20077 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20078 doc: /* Return paragraph direction at point in BUFFER.
20079 Value is either `left-to-right' or `right-to-left'.
20080 If BUFFER is omitted or nil, it defaults to the current buffer.
20081
20082 Paragraph direction determines how the text in the paragraph is displayed.
20083 In left-to-right paragraphs, text begins at the left margin of the window
20084 and the reading direction is generally left to right. In right-to-left
20085 paragraphs, text begins at the right margin and is read from right to left.
20086
20087 See also `bidi-paragraph-direction'. */)
20088 (Lisp_Object buffer)
20089 {
20090 struct buffer *buf = current_buffer;
20091 struct buffer *old = buf;
20092
20093 if (! NILP (buffer))
20094 {
20095 CHECK_BUFFER (buffer);
20096 buf = XBUFFER (buffer);
20097 }
20098
20099 if (NILP (BVAR (buf, bidi_display_reordering))
20100 || NILP (BVAR (buf, enable_multibyte_characters))
20101 /* When we are loading loadup.el, the character property tables
20102 needed for bidi iteration are not yet available. */
20103 || !NILP (Vpurify_flag))
20104 return Qleft_to_right;
20105 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20106 return BVAR (buf, bidi_paragraph_direction);
20107 else
20108 {
20109 /* Determine the direction from buffer text. We could try to
20110 use current_matrix if it is up to date, but this seems fast
20111 enough as it is. */
20112 struct bidi_it itb;
20113 ptrdiff_t pos = BUF_PT (buf);
20114 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20115 int c;
20116 void *itb_data = bidi_shelve_cache ();
20117
20118 set_buffer_temp (buf);
20119 /* bidi_paragraph_init finds the base direction of the paragraph
20120 by searching forward from paragraph start. We need the base
20121 direction of the current or _previous_ paragraph, so we need
20122 to make sure we are within that paragraph. To that end, find
20123 the previous non-empty line. */
20124 if (pos >= ZV && pos > BEGV)
20125 DEC_BOTH (pos, bytepos);
20126 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20127 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20128 {
20129 while ((c = FETCH_BYTE (bytepos)) == '\n'
20130 || c == ' ' || c == '\t' || c == '\f')
20131 {
20132 if (bytepos <= BEGV_BYTE)
20133 break;
20134 bytepos--;
20135 pos--;
20136 }
20137 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20138 bytepos--;
20139 }
20140 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20141 itb.paragraph_dir = NEUTRAL_DIR;
20142 itb.string.s = NULL;
20143 itb.string.lstring = Qnil;
20144 itb.string.bufpos = 0;
20145 itb.string.unibyte = 0;
20146 /* We have no window to use here for ignoring window-specific
20147 overlays. Using NULL for window pointer will cause
20148 compute_display_string_pos to use the current buffer. */
20149 itb.w = NULL;
20150 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20151 bidi_unshelve_cache (itb_data, 0);
20152 set_buffer_temp (old);
20153 switch (itb.paragraph_dir)
20154 {
20155 case L2R:
20156 return Qleft_to_right;
20157 break;
20158 case R2L:
20159 return Qright_to_left;
20160 break;
20161 default:
20162 emacs_abort ();
20163 }
20164 }
20165 }
20166
20167 DEFUN ("move-point-visually", Fmove_point_visually,
20168 Smove_point_visually, 1, 1, 0,
20169 doc: /* Move point in the visual order in the specified DIRECTION.
20170 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20171 left.
20172
20173 Value is the new character position of point. */)
20174 (Lisp_Object direction)
20175 {
20176 struct window *w = XWINDOW (selected_window);
20177 struct buffer *b = XBUFFER (w->contents);
20178 struct glyph_row *row;
20179 int dir;
20180 Lisp_Object paragraph_dir;
20181
20182 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20183 (!(ROW)->continued_p \
20184 && INTEGERP ((GLYPH)->object) \
20185 && (GLYPH)->type == CHAR_GLYPH \
20186 && (GLYPH)->u.ch == ' ' \
20187 && (GLYPH)->charpos >= 0 \
20188 && !(GLYPH)->avoid_cursor_p)
20189
20190 CHECK_NUMBER (direction);
20191 dir = XINT (direction);
20192 if (dir > 0)
20193 dir = 1;
20194 else
20195 dir = -1;
20196
20197 /* If current matrix is up-to-date, we can use the information
20198 recorded in the glyphs, at least as long as the goal is on the
20199 screen. */
20200 if (w->window_end_valid
20201 && !windows_or_buffers_changed
20202 && b
20203 && !b->clip_changed
20204 && !b->prevent_redisplay_optimizations_p
20205 && !window_outdated (w)
20206 && w->cursor.vpos >= 0
20207 && w->cursor.vpos < w->current_matrix->nrows
20208 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20209 {
20210 struct glyph *g = row->glyphs[TEXT_AREA];
20211 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20212 struct glyph *gpt = g + w->cursor.hpos;
20213
20214 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20215 {
20216 if (BUFFERP (g->object) && g->charpos != PT)
20217 {
20218 SET_PT (g->charpos);
20219 w->cursor.vpos = -1;
20220 return make_number (PT);
20221 }
20222 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20223 {
20224 ptrdiff_t new_pos;
20225
20226 if (BUFFERP (gpt->object))
20227 {
20228 new_pos = PT;
20229 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20230 new_pos += (row->reversed_p ? -dir : dir);
20231 else
20232 new_pos -= (row->reversed_p ? -dir : dir);;
20233 }
20234 else if (BUFFERP (g->object))
20235 new_pos = g->charpos;
20236 else
20237 break;
20238 SET_PT (new_pos);
20239 w->cursor.vpos = -1;
20240 return make_number (PT);
20241 }
20242 else if (ROW_GLYPH_NEWLINE_P (row, g))
20243 {
20244 /* Glyphs inserted at the end of a non-empty line for
20245 positioning the cursor have zero charpos, so we must
20246 deduce the value of point by other means. */
20247 if (g->charpos > 0)
20248 SET_PT (g->charpos);
20249 else if (row->ends_at_zv_p && PT != ZV)
20250 SET_PT (ZV);
20251 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20252 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20253 else
20254 break;
20255 w->cursor.vpos = -1;
20256 return make_number (PT);
20257 }
20258 }
20259 if (g == e || INTEGERP (g->object))
20260 {
20261 if (row->truncated_on_left_p || row->truncated_on_right_p)
20262 goto simulate_display;
20263 if (!row->reversed_p)
20264 row += dir;
20265 else
20266 row -= dir;
20267 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20268 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20269 goto simulate_display;
20270
20271 if (dir > 0)
20272 {
20273 if (row->reversed_p && !row->continued_p)
20274 {
20275 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20276 w->cursor.vpos = -1;
20277 return make_number (PT);
20278 }
20279 g = row->glyphs[TEXT_AREA];
20280 e = g + row->used[TEXT_AREA];
20281 for ( ; g < e; g++)
20282 {
20283 if (BUFFERP (g->object)
20284 /* Empty lines have only one glyph, which stands
20285 for the newline, and whose charpos is the
20286 buffer position of the newline. */
20287 || ROW_GLYPH_NEWLINE_P (row, g)
20288 /* When the buffer ends in a newline, the line at
20289 EOB also has one glyph, but its charpos is -1. */
20290 || (row->ends_at_zv_p
20291 && !row->reversed_p
20292 && INTEGERP (g->object)
20293 && g->type == CHAR_GLYPH
20294 && g->u.ch == ' '))
20295 {
20296 if (g->charpos > 0)
20297 SET_PT (g->charpos);
20298 else if (!row->reversed_p
20299 && row->ends_at_zv_p
20300 && PT != ZV)
20301 SET_PT (ZV);
20302 else
20303 continue;
20304 w->cursor.vpos = -1;
20305 return make_number (PT);
20306 }
20307 }
20308 }
20309 else
20310 {
20311 if (!row->reversed_p && !row->continued_p)
20312 {
20313 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20314 w->cursor.vpos = -1;
20315 return make_number (PT);
20316 }
20317 e = row->glyphs[TEXT_AREA];
20318 g = e + row->used[TEXT_AREA] - 1;
20319 for ( ; g >= e; g--)
20320 {
20321 if (BUFFERP (g->object)
20322 || (ROW_GLYPH_NEWLINE_P (row, g)
20323 && g->charpos > 0)
20324 /* Empty R2L lines on GUI frames have the buffer
20325 position of the newline stored in the stretch
20326 glyph. */
20327 || g->type == STRETCH_GLYPH
20328 || (row->ends_at_zv_p
20329 && row->reversed_p
20330 && INTEGERP (g->object)
20331 && g->type == CHAR_GLYPH
20332 && g->u.ch == ' '))
20333 {
20334 if (g->charpos > 0)
20335 SET_PT (g->charpos);
20336 else if (row->reversed_p
20337 && row->ends_at_zv_p
20338 && PT != ZV)
20339 SET_PT (ZV);
20340 else
20341 continue;
20342 w->cursor.vpos = -1;
20343 return make_number (PT);
20344 }
20345 }
20346 }
20347 }
20348 }
20349
20350 simulate_display:
20351
20352 /* If we wind up here, we failed to move by using the glyphs, so we
20353 need to simulate display instead. */
20354
20355 if (b)
20356 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20357 else
20358 paragraph_dir = Qleft_to_right;
20359 if (EQ (paragraph_dir, Qright_to_left))
20360 dir = -dir;
20361 if (PT <= BEGV && dir < 0)
20362 xsignal0 (Qbeginning_of_buffer);
20363 else if (PT >= ZV && dir > 0)
20364 xsignal0 (Qend_of_buffer);
20365 else
20366 {
20367 struct text_pos pt;
20368 struct it it;
20369 int pt_x, target_x, pixel_width, pt_vpos;
20370 bool at_eol_p;
20371 bool overshoot_expected = false;
20372 bool target_is_eol_p = false;
20373
20374 /* Setup the arena. */
20375 SET_TEXT_POS (pt, PT, PT_BYTE);
20376 start_display (&it, w, pt);
20377
20378 if (it.cmp_it.id < 0
20379 && it.method == GET_FROM_STRING
20380 && it.area == TEXT_AREA
20381 && it.string_from_display_prop_p
20382 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20383 overshoot_expected = true;
20384
20385 /* Find the X coordinate of point. We start from the beginning
20386 of this or previous line to make sure we are before point in
20387 the logical order (since the move_it_* functions can only
20388 move forward). */
20389 reseat_at_previous_visible_line_start (&it);
20390 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20391 if (IT_CHARPOS (it) != PT)
20392 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20393 -1, -1, -1, MOVE_TO_POS);
20394 pt_x = it.current_x;
20395 pt_vpos = it.vpos;
20396 if (dir > 0 || overshoot_expected)
20397 {
20398 struct glyph_row *row = it.glyph_row;
20399
20400 /* When point is at beginning of line, we don't have
20401 information about the glyph there loaded into struct
20402 it. Calling get_next_display_element fixes that. */
20403 if (pt_x == 0)
20404 get_next_display_element (&it);
20405 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20406 it.glyph_row = NULL;
20407 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20408 it.glyph_row = row;
20409 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20410 it, lest it will become out of sync with it's buffer
20411 position. */
20412 it.current_x = pt_x;
20413 }
20414 else
20415 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20416 pixel_width = it.pixel_width;
20417 if (overshoot_expected && at_eol_p)
20418 pixel_width = 0;
20419 else if (pixel_width <= 0)
20420 pixel_width = 1;
20421
20422 /* If there's a display string at point, we are actually at the
20423 glyph to the left of point, so we need to correct the X
20424 coordinate. */
20425 if (overshoot_expected)
20426 pt_x += pixel_width;
20427
20428 /* Compute target X coordinate, either to the left or to the
20429 right of point. On TTY frames, all characters have the same
20430 pixel width of 1, so we can use that. On GUI frames we don't
20431 have an easy way of getting at the pixel width of the
20432 character to the left of point, so we use a different method
20433 of getting to that place. */
20434 if (dir > 0)
20435 target_x = pt_x + pixel_width;
20436 else
20437 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20438
20439 /* Target X coordinate could be one line above or below the line
20440 of point, in which case we need to adjust the target X
20441 coordinate. Also, if moving to the left, we need to begin at
20442 the left edge of the point's screen line. */
20443 if (dir < 0)
20444 {
20445 if (pt_x > 0)
20446 {
20447 start_display (&it, w, pt);
20448 reseat_at_previous_visible_line_start (&it);
20449 it.current_x = it.current_y = it.hpos = 0;
20450 if (pt_vpos != 0)
20451 move_it_by_lines (&it, pt_vpos);
20452 }
20453 else
20454 {
20455 move_it_by_lines (&it, -1);
20456 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20457 target_is_eol_p = true;
20458 }
20459 }
20460 else
20461 {
20462 if (at_eol_p
20463 || (target_x >= it.last_visible_x
20464 && it.line_wrap != TRUNCATE))
20465 {
20466 if (pt_x > 0)
20467 move_it_by_lines (&it, 0);
20468 move_it_by_lines (&it, 1);
20469 target_x = 0;
20470 }
20471 }
20472
20473 /* Move to the target X coordinate. */
20474 #ifdef HAVE_WINDOW_SYSTEM
20475 /* On GUI frames, as we don't know the X coordinate of the
20476 character to the left of point, moving point to the left
20477 requires walking, one grapheme cluster at a time, until we
20478 find ourself at a place immediately to the left of the
20479 character at point. */
20480 if (FRAME_WINDOW_P (it.f) && dir < 0)
20481 {
20482 struct text_pos new_pos = it.current.pos;
20483 enum move_it_result rc = MOVE_X_REACHED;
20484
20485 while (it.current_x + it.pixel_width <= target_x
20486 && rc == MOVE_X_REACHED)
20487 {
20488 int new_x = it.current_x + it.pixel_width;
20489
20490 new_pos = it.current.pos;
20491 if (new_x == it.current_x)
20492 new_x++;
20493 rc = move_it_in_display_line_to (&it, ZV, new_x,
20494 MOVE_TO_POS | MOVE_TO_X);
20495 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20496 break;
20497 }
20498 /* If we ended up on a composed character inside
20499 bidi-reordered text (e.g., Hebrew text with diacritics),
20500 the iterator gives us the buffer position of the last (in
20501 logical order) character of the composed grapheme cluster,
20502 which is not what we want. So we cheat: we compute the
20503 character position of the character that follows (in the
20504 logical order) the one where the above loop stopped. That
20505 character will appear on display to the left of point. */
20506 if (it.bidi_p
20507 && it.bidi_it.scan_dir == -1
20508 && new_pos.charpos - IT_CHARPOS (it) > 1)
20509 {
20510 new_pos.charpos = IT_CHARPOS (it) + 1;
20511 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20512 }
20513 it.current.pos = new_pos;
20514 }
20515 else
20516 #endif
20517 if (it.current_x != target_x)
20518 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20519
20520 /* When lines are truncated, the above loop will stop at the
20521 window edge. But we want to get to the end of line, even if
20522 it is beyond the window edge; automatic hscroll will then
20523 scroll the window to show point as appropriate. */
20524 if (target_is_eol_p && it.line_wrap == TRUNCATE
20525 && get_next_display_element (&it))
20526 {
20527 struct text_pos new_pos = it.current.pos;
20528
20529 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20530 {
20531 set_iterator_to_next (&it, 0);
20532 if (it.method == GET_FROM_BUFFER)
20533 new_pos = it.current.pos;
20534 if (!get_next_display_element (&it))
20535 break;
20536 }
20537
20538 it.current.pos = new_pos;
20539 }
20540
20541 /* If we ended up in a display string that covers point, move to
20542 buffer position to the right in the visual order. */
20543 if (dir > 0)
20544 {
20545 while (IT_CHARPOS (it) == PT)
20546 {
20547 set_iterator_to_next (&it, 0);
20548 if (!get_next_display_element (&it))
20549 break;
20550 }
20551 }
20552
20553 /* Move point to that position. */
20554 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20555 }
20556
20557 return make_number (PT);
20558
20559 #undef ROW_GLYPH_NEWLINE_P
20560 }
20561
20562 \f
20563 /***********************************************************************
20564 Menu Bar
20565 ***********************************************************************/
20566
20567 /* Redisplay the menu bar in the frame for window W.
20568
20569 The menu bar of X frames that don't have X toolkit support is
20570 displayed in a special window W->frame->menu_bar_window.
20571
20572 The menu bar of terminal frames is treated specially as far as
20573 glyph matrices are concerned. Menu bar lines are not part of
20574 windows, so the update is done directly on the frame matrix rows
20575 for the menu bar. */
20576
20577 static void
20578 display_menu_bar (struct window *w)
20579 {
20580 struct frame *f = XFRAME (WINDOW_FRAME (w));
20581 struct it it;
20582 Lisp_Object items;
20583 int i;
20584
20585 /* Don't do all this for graphical frames. */
20586 #ifdef HAVE_NTGUI
20587 if (FRAME_W32_P (f))
20588 return;
20589 #endif
20590 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20591 if (FRAME_X_P (f))
20592 return;
20593 #endif
20594
20595 #ifdef HAVE_NS
20596 if (FRAME_NS_P (f))
20597 return;
20598 #endif /* HAVE_NS */
20599
20600 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20601 eassert (!FRAME_WINDOW_P (f));
20602 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20603 it.first_visible_x = 0;
20604 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20605 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20606 if (FRAME_WINDOW_P (f))
20607 {
20608 /* Menu bar lines are displayed in the desired matrix of the
20609 dummy window menu_bar_window. */
20610 struct window *menu_w;
20611 menu_w = XWINDOW (f->menu_bar_window);
20612 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20613 MENU_FACE_ID);
20614 it.first_visible_x = 0;
20615 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20616 }
20617 else
20618 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20619 {
20620 /* This is a TTY frame, i.e. character hpos/vpos are used as
20621 pixel x/y. */
20622 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20623 MENU_FACE_ID);
20624 it.first_visible_x = 0;
20625 it.last_visible_x = FRAME_COLS (f);
20626 }
20627
20628 /* FIXME: This should be controlled by a user option. See the
20629 comments in redisplay_tool_bar and display_mode_line about
20630 this. */
20631 it.paragraph_embedding = L2R;
20632
20633 /* Clear all rows of the menu bar. */
20634 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20635 {
20636 struct glyph_row *row = it.glyph_row + i;
20637 clear_glyph_row (row);
20638 row->enabled_p = 1;
20639 row->full_width_p = 1;
20640 }
20641
20642 /* Display all items of the menu bar. */
20643 items = FRAME_MENU_BAR_ITEMS (it.f);
20644 for (i = 0; i < ASIZE (items); i += 4)
20645 {
20646 Lisp_Object string;
20647
20648 /* Stop at nil string. */
20649 string = AREF (items, i + 1);
20650 if (NILP (string))
20651 break;
20652
20653 /* Remember where item was displayed. */
20654 ASET (items, i + 3, make_number (it.hpos));
20655
20656 /* Display the item, pad with one space. */
20657 if (it.current_x < it.last_visible_x)
20658 display_string (NULL, string, Qnil, 0, 0, &it,
20659 SCHARS (string) + 1, 0, 0, -1);
20660 }
20661
20662 /* Fill out the line with spaces. */
20663 if (it.current_x < it.last_visible_x)
20664 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20665
20666 /* Compute the total height of the lines. */
20667 compute_line_metrics (&it);
20668 }
20669
20670
20671 \f
20672 /***********************************************************************
20673 Mode Line
20674 ***********************************************************************/
20675
20676 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20677 FORCE is non-zero, redisplay mode lines unconditionally.
20678 Otherwise, redisplay only mode lines that are garbaged. Value is
20679 the number of windows whose mode lines were redisplayed. */
20680
20681 static int
20682 redisplay_mode_lines (Lisp_Object window, int force)
20683 {
20684 int nwindows = 0;
20685
20686 while (!NILP (window))
20687 {
20688 struct window *w = XWINDOW (window);
20689
20690 if (WINDOWP (w->contents))
20691 nwindows += redisplay_mode_lines (w->contents, force);
20692 else if (force
20693 || FRAME_GARBAGED_P (XFRAME (w->frame))
20694 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20695 {
20696 struct text_pos lpoint;
20697 struct buffer *old = current_buffer;
20698
20699 /* Set the window's buffer for the mode line display. */
20700 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20701 set_buffer_internal_1 (XBUFFER (w->contents));
20702
20703 /* Point refers normally to the selected window. For any
20704 other window, set up appropriate value. */
20705 if (!EQ (window, selected_window))
20706 {
20707 struct text_pos pt;
20708
20709 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20710 if (CHARPOS (pt) < BEGV)
20711 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20712 else if (CHARPOS (pt) > (ZV - 1))
20713 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20714 else
20715 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20716 }
20717
20718 /* Display mode lines. */
20719 clear_glyph_matrix (w->desired_matrix);
20720 if (display_mode_lines (w))
20721 {
20722 ++nwindows;
20723 w->must_be_updated_p = 1;
20724 }
20725
20726 /* Restore old settings. */
20727 set_buffer_internal_1 (old);
20728 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20729 }
20730
20731 window = w->next;
20732 }
20733
20734 return nwindows;
20735 }
20736
20737
20738 /* Display the mode and/or header line of window W. Value is the
20739 sum number of mode lines and header lines displayed. */
20740
20741 static int
20742 display_mode_lines (struct window *w)
20743 {
20744 Lisp_Object old_selected_window = selected_window;
20745 Lisp_Object old_selected_frame = selected_frame;
20746 Lisp_Object new_frame = w->frame;
20747 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20748 int n = 0;
20749
20750 selected_frame = new_frame;
20751 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20752 or window's point, then we'd need select_window_1 here as well. */
20753 XSETWINDOW (selected_window, w);
20754 XFRAME (new_frame)->selected_window = selected_window;
20755
20756 /* These will be set while the mode line specs are processed. */
20757 line_number_displayed = 0;
20758 w->column_number_displayed = -1;
20759
20760 if (WINDOW_WANTS_MODELINE_P (w))
20761 {
20762 struct window *sel_w = XWINDOW (old_selected_window);
20763
20764 /* Select mode line face based on the real selected window. */
20765 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20766 BVAR (current_buffer, mode_line_format));
20767 ++n;
20768 }
20769
20770 if (WINDOW_WANTS_HEADER_LINE_P (w))
20771 {
20772 display_mode_line (w, HEADER_LINE_FACE_ID,
20773 BVAR (current_buffer, header_line_format));
20774 ++n;
20775 }
20776
20777 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20778 selected_frame = old_selected_frame;
20779 selected_window = old_selected_window;
20780 return n;
20781 }
20782
20783
20784 /* Display mode or header line of window W. FACE_ID specifies which
20785 line to display; it is either MODE_LINE_FACE_ID or
20786 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20787 display. Value is the pixel height of the mode/header line
20788 displayed. */
20789
20790 static int
20791 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20792 {
20793 struct it it;
20794 struct face *face;
20795 ptrdiff_t count = SPECPDL_INDEX ();
20796
20797 init_iterator (&it, w, -1, -1, NULL, face_id);
20798 /* Don't extend on a previously drawn mode-line.
20799 This may happen if called from pos_visible_p. */
20800 it.glyph_row->enabled_p = 0;
20801 prepare_desired_row (it.glyph_row);
20802
20803 it.glyph_row->mode_line_p = 1;
20804
20805 /* FIXME: This should be controlled by a user option. But
20806 supporting such an option is not trivial, since the mode line is
20807 made up of many separate strings. */
20808 it.paragraph_embedding = L2R;
20809
20810 record_unwind_protect (unwind_format_mode_line,
20811 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20812
20813 mode_line_target = MODE_LINE_DISPLAY;
20814
20815 /* Temporarily make frame's keyboard the current kboard so that
20816 kboard-local variables in the mode_line_format will get the right
20817 values. */
20818 push_kboard (FRAME_KBOARD (it.f));
20819 record_unwind_save_match_data ();
20820 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20821 pop_kboard ();
20822
20823 unbind_to (count, Qnil);
20824
20825 /* Fill up with spaces. */
20826 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20827
20828 compute_line_metrics (&it);
20829 it.glyph_row->full_width_p = 1;
20830 it.glyph_row->continued_p = 0;
20831 it.glyph_row->truncated_on_left_p = 0;
20832 it.glyph_row->truncated_on_right_p = 0;
20833
20834 /* Make a 3D mode-line have a shadow at its right end. */
20835 face = FACE_FROM_ID (it.f, face_id);
20836 extend_face_to_end_of_line (&it);
20837 if (face->box != FACE_NO_BOX)
20838 {
20839 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20840 + it.glyph_row->used[TEXT_AREA] - 1);
20841 last->right_box_line_p = 1;
20842 }
20843
20844 return it.glyph_row->height;
20845 }
20846
20847 /* Move element ELT in LIST to the front of LIST.
20848 Return the updated list. */
20849
20850 static Lisp_Object
20851 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20852 {
20853 register Lisp_Object tail, prev;
20854 register Lisp_Object tem;
20855
20856 tail = list;
20857 prev = Qnil;
20858 while (CONSP (tail))
20859 {
20860 tem = XCAR (tail);
20861
20862 if (EQ (elt, tem))
20863 {
20864 /* Splice out the link TAIL. */
20865 if (NILP (prev))
20866 list = XCDR (tail);
20867 else
20868 Fsetcdr (prev, XCDR (tail));
20869
20870 /* Now make it the first. */
20871 Fsetcdr (tail, list);
20872 return tail;
20873 }
20874 else
20875 prev = tail;
20876 tail = XCDR (tail);
20877 QUIT;
20878 }
20879
20880 /* Not found--return unchanged LIST. */
20881 return list;
20882 }
20883
20884 /* Contribute ELT to the mode line for window IT->w. How it
20885 translates into text depends on its data type.
20886
20887 IT describes the display environment in which we display, as usual.
20888
20889 DEPTH is the depth in recursion. It is used to prevent
20890 infinite recursion here.
20891
20892 FIELD_WIDTH is the number of characters the display of ELT should
20893 occupy in the mode line, and PRECISION is the maximum number of
20894 characters to display from ELT's representation. See
20895 display_string for details.
20896
20897 Returns the hpos of the end of the text generated by ELT.
20898
20899 PROPS is a property list to add to any string we encounter.
20900
20901 If RISKY is nonzero, remove (disregard) any properties in any string
20902 we encounter, and ignore :eval and :propertize.
20903
20904 The global variable `mode_line_target' determines whether the
20905 output is passed to `store_mode_line_noprop',
20906 `store_mode_line_string', or `display_string'. */
20907
20908 static int
20909 display_mode_element (struct it *it, int depth, int field_width, int precision,
20910 Lisp_Object elt, Lisp_Object props, int risky)
20911 {
20912 int n = 0, field, prec;
20913 int literal = 0;
20914
20915 tail_recurse:
20916 if (depth > 100)
20917 elt = build_string ("*too-deep*");
20918
20919 depth++;
20920
20921 switch (XTYPE (elt))
20922 {
20923 case Lisp_String:
20924 {
20925 /* A string: output it and check for %-constructs within it. */
20926 unsigned char c;
20927 ptrdiff_t offset = 0;
20928
20929 if (SCHARS (elt) > 0
20930 && (!NILP (props) || risky))
20931 {
20932 Lisp_Object oprops, aelt;
20933 oprops = Ftext_properties_at (make_number (0), elt);
20934
20935 /* If the starting string's properties are not what
20936 we want, translate the string. Also, if the string
20937 is risky, do that anyway. */
20938
20939 if (NILP (Fequal (props, oprops)) || risky)
20940 {
20941 /* If the starting string has properties,
20942 merge the specified ones onto the existing ones. */
20943 if (! NILP (oprops) && !risky)
20944 {
20945 Lisp_Object tem;
20946
20947 oprops = Fcopy_sequence (oprops);
20948 tem = props;
20949 while (CONSP (tem))
20950 {
20951 oprops = Fplist_put (oprops, XCAR (tem),
20952 XCAR (XCDR (tem)));
20953 tem = XCDR (XCDR (tem));
20954 }
20955 props = oprops;
20956 }
20957
20958 aelt = Fassoc (elt, mode_line_proptrans_alist);
20959 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20960 {
20961 /* AELT is what we want. Move it to the front
20962 without consing. */
20963 elt = XCAR (aelt);
20964 mode_line_proptrans_alist
20965 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20966 }
20967 else
20968 {
20969 Lisp_Object tem;
20970
20971 /* If AELT has the wrong props, it is useless.
20972 so get rid of it. */
20973 if (! NILP (aelt))
20974 mode_line_proptrans_alist
20975 = Fdelq (aelt, mode_line_proptrans_alist);
20976
20977 elt = Fcopy_sequence (elt);
20978 Fset_text_properties (make_number (0), Flength (elt),
20979 props, elt);
20980 /* Add this item to mode_line_proptrans_alist. */
20981 mode_line_proptrans_alist
20982 = Fcons (Fcons (elt, props),
20983 mode_line_proptrans_alist);
20984 /* Truncate mode_line_proptrans_alist
20985 to at most 50 elements. */
20986 tem = Fnthcdr (make_number (50),
20987 mode_line_proptrans_alist);
20988 if (! NILP (tem))
20989 XSETCDR (tem, Qnil);
20990 }
20991 }
20992 }
20993
20994 offset = 0;
20995
20996 if (literal)
20997 {
20998 prec = precision - n;
20999 switch (mode_line_target)
21000 {
21001 case MODE_LINE_NOPROP:
21002 case MODE_LINE_TITLE:
21003 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21004 break;
21005 case MODE_LINE_STRING:
21006 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21007 break;
21008 case MODE_LINE_DISPLAY:
21009 n += display_string (NULL, elt, Qnil, 0, 0, it,
21010 0, prec, 0, STRING_MULTIBYTE (elt));
21011 break;
21012 }
21013
21014 break;
21015 }
21016
21017 /* Handle the non-literal case. */
21018
21019 while ((precision <= 0 || n < precision)
21020 && SREF (elt, offset) != 0
21021 && (mode_line_target != MODE_LINE_DISPLAY
21022 || it->current_x < it->last_visible_x))
21023 {
21024 ptrdiff_t last_offset = offset;
21025
21026 /* Advance to end of string or next format specifier. */
21027 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21028 ;
21029
21030 if (offset - 1 != last_offset)
21031 {
21032 ptrdiff_t nchars, nbytes;
21033
21034 /* Output to end of string or up to '%'. Field width
21035 is length of string. Don't output more than
21036 PRECISION allows us. */
21037 offset--;
21038
21039 prec = c_string_width (SDATA (elt) + last_offset,
21040 offset - last_offset, precision - n,
21041 &nchars, &nbytes);
21042
21043 switch (mode_line_target)
21044 {
21045 case MODE_LINE_NOPROP:
21046 case MODE_LINE_TITLE:
21047 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21048 break;
21049 case MODE_LINE_STRING:
21050 {
21051 ptrdiff_t bytepos = last_offset;
21052 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21053 ptrdiff_t endpos = (precision <= 0
21054 ? string_byte_to_char (elt, offset)
21055 : charpos + nchars);
21056
21057 n += store_mode_line_string (NULL,
21058 Fsubstring (elt, make_number (charpos),
21059 make_number (endpos)),
21060 0, 0, 0, Qnil);
21061 }
21062 break;
21063 case MODE_LINE_DISPLAY:
21064 {
21065 ptrdiff_t bytepos = last_offset;
21066 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21067
21068 if (precision <= 0)
21069 nchars = string_byte_to_char (elt, offset) - charpos;
21070 n += display_string (NULL, elt, Qnil, 0, charpos,
21071 it, 0, nchars, 0,
21072 STRING_MULTIBYTE (elt));
21073 }
21074 break;
21075 }
21076 }
21077 else /* c == '%' */
21078 {
21079 ptrdiff_t percent_position = offset;
21080
21081 /* Get the specified minimum width. Zero means
21082 don't pad. */
21083 field = 0;
21084 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21085 field = field * 10 + c - '0';
21086
21087 /* Don't pad beyond the total padding allowed. */
21088 if (field_width - n > 0 && field > field_width - n)
21089 field = field_width - n;
21090
21091 /* Note that either PRECISION <= 0 or N < PRECISION. */
21092 prec = precision - n;
21093
21094 if (c == 'M')
21095 n += display_mode_element (it, depth, field, prec,
21096 Vglobal_mode_string, props,
21097 risky);
21098 else if (c != 0)
21099 {
21100 bool multibyte;
21101 ptrdiff_t bytepos, charpos;
21102 const char *spec;
21103 Lisp_Object string;
21104
21105 bytepos = percent_position;
21106 charpos = (STRING_MULTIBYTE (elt)
21107 ? string_byte_to_char (elt, bytepos)
21108 : bytepos);
21109 spec = decode_mode_spec (it->w, c, field, &string);
21110 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21111
21112 switch (mode_line_target)
21113 {
21114 case MODE_LINE_NOPROP:
21115 case MODE_LINE_TITLE:
21116 n += store_mode_line_noprop (spec, field, prec);
21117 break;
21118 case MODE_LINE_STRING:
21119 {
21120 Lisp_Object tem = build_string (spec);
21121 props = Ftext_properties_at (make_number (charpos), elt);
21122 /* Should only keep face property in props */
21123 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21124 }
21125 break;
21126 case MODE_LINE_DISPLAY:
21127 {
21128 int nglyphs_before, nwritten;
21129
21130 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21131 nwritten = display_string (spec, string, elt,
21132 charpos, 0, it,
21133 field, prec, 0,
21134 multibyte);
21135
21136 /* Assign to the glyphs written above the
21137 string where the `%x' came from, position
21138 of the `%'. */
21139 if (nwritten > 0)
21140 {
21141 struct glyph *glyph
21142 = (it->glyph_row->glyphs[TEXT_AREA]
21143 + nglyphs_before);
21144 int i;
21145
21146 for (i = 0; i < nwritten; ++i)
21147 {
21148 glyph[i].object = elt;
21149 glyph[i].charpos = charpos;
21150 }
21151
21152 n += nwritten;
21153 }
21154 }
21155 break;
21156 }
21157 }
21158 else /* c == 0 */
21159 break;
21160 }
21161 }
21162 }
21163 break;
21164
21165 case Lisp_Symbol:
21166 /* A symbol: process the value of the symbol recursively
21167 as if it appeared here directly. Avoid error if symbol void.
21168 Special case: if value of symbol is a string, output the string
21169 literally. */
21170 {
21171 register Lisp_Object tem;
21172
21173 /* If the variable is not marked as risky to set
21174 then its contents are risky to use. */
21175 if (NILP (Fget (elt, Qrisky_local_variable)))
21176 risky = 1;
21177
21178 tem = Fboundp (elt);
21179 if (!NILP (tem))
21180 {
21181 tem = Fsymbol_value (elt);
21182 /* If value is a string, output that string literally:
21183 don't check for % within it. */
21184 if (STRINGP (tem))
21185 literal = 1;
21186
21187 if (!EQ (tem, elt))
21188 {
21189 /* Give up right away for nil or t. */
21190 elt = tem;
21191 goto tail_recurse;
21192 }
21193 }
21194 }
21195 break;
21196
21197 case Lisp_Cons:
21198 {
21199 register Lisp_Object car, tem;
21200
21201 /* A cons cell: five distinct cases.
21202 If first element is :eval or :propertize, do something special.
21203 If first element is a string or a cons, process all the elements
21204 and effectively concatenate them.
21205 If first element is a negative number, truncate displaying cdr to
21206 at most that many characters. If positive, pad (with spaces)
21207 to at least that many characters.
21208 If first element is a symbol, process the cadr or caddr recursively
21209 according to whether the symbol's value is non-nil or nil. */
21210 car = XCAR (elt);
21211 if (EQ (car, QCeval))
21212 {
21213 /* An element of the form (:eval FORM) means evaluate FORM
21214 and use the result as mode line elements. */
21215
21216 if (risky)
21217 break;
21218
21219 if (CONSP (XCDR (elt)))
21220 {
21221 Lisp_Object spec;
21222 spec = safe_eval (XCAR (XCDR (elt)));
21223 n += display_mode_element (it, depth, field_width - n,
21224 precision - n, spec, props,
21225 risky);
21226 }
21227 }
21228 else if (EQ (car, QCpropertize))
21229 {
21230 /* An element of the form (:propertize ELT PROPS...)
21231 means display ELT but applying properties PROPS. */
21232
21233 if (risky)
21234 break;
21235
21236 if (CONSP (XCDR (elt)))
21237 n += display_mode_element (it, depth, field_width - n,
21238 precision - n, XCAR (XCDR (elt)),
21239 XCDR (XCDR (elt)), risky);
21240 }
21241 else if (SYMBOLP (car))
21242 {
21243 tem = Fboundp (car);
21244 elt = XCDR (elt);
21245 if (!CONSP (elt))
21246 goto invalid;
21247 /* elt is now the cdr, and we know it is a cons cell.
21248 Use its car if CAR has a non-nil value. */
21249 if (!NILP (tem))
21250 {
21251 tem = Fsymbol_value (car);
21252 if (!NILP (tem))
21253 {
21254 elt = XCAR (elt);
21255 goto tail_recurse;
21256 }
21257 }
21258 /* Symbol's value is nil (or symbol is unbound)
21259 Get the cddr of the original list
21260 and if possible find the caddr and use that. */
21261 elt = XCDR (elt);
21262 if (NILP (elt))
21263 break;
21264 else if (!CONSP (elt))
21265 goto invalid;
21266 elt = XCAR (elt);
21267 goto tail_recurse;
21268 }
21269 else if (INTEGERP (car))
21270 {
21271 register int lim = XINT (car);
21272 elt = XCDR (elt);
21273 if (lim < 0)
21274 {
21275 /* Negative int means reduce maximum width. */
21276 if (precision <= 0)
21277 precision = -lim;
21278 else
21279 precision = min (precision, -lim);
21280 }
21281 else if (lim > 0)
21282 {
21283 /* Padding specified. Don't let it be more than
21284 current maximum. */
21285 if (precision > 0)
21286 lim = min (precision, lim);
21287
21288 /* If that's more padding than already wanted, queue it.
21289 But don't reduce padding already specified even if
21290 that is beyond the current truncation point. */
21291 field_width = max (lim, field_width);
21292 }
21293 goto tail_recurse;
21294 }
21295 else if (STRINGP (car) || CONSP (car))
21296 {
21297 Lisp_Object halftail = elt;
21298 int len = 0;
21299
21300 while (CONSP (elt)
21301 && (precision <= 0 || n < precision))
21302 {
21303 n += display_mode_element (it, depth,
21304 /* Do padding only after the last
21305 element in the list. */
21306 (! CONSP (XCDR (elt))
21307 ? field_width - n
21308 : 0),
21309 precision - n, XCAR (elt),
21310 props, risky);
21311 elt = XCDR (elt);
21312 len++;
21313 if ((len & 1) == 0)
21314 halftail = XCDR (halftail);
21315 /* Check for cycle. */
21316 if (EQ (halftail, elt))
21317 break;
21318 }
21319 }
21320 }
21321 break;
21322
21323 default:
21324 invalid:
21325 elt = build_string ("*invalid*");
21326 goto tail_recurse;
21327 }
21328
21329 /* Pad to FIELD_WIDTH. */
21330 if (field_width > 0 && n < field_width)
21331 {
21332 switch (mode_line_target)
21333 {
21334 case MODE_LINE_NOPROP:
21335 case MODE_LINE_TITLE:
21336 n += store_mode_line_noprop ("", field_width - n, 0);
21337 break;
21338 case MODE_LINE_STRING:
21339 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21340 break;
21341 case MODE_LINE_DISPLAY:
21342 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21343 0, 0, 0);
21344 break;
21345 }
21346 }
21347
21348 return n;
21349 }
21350
21351 /* Store a mode-line string element in mode_line_string_list.
21352
21353 If STRING is non-null, display that C string. Otherwise, the Lisp
21354 string LISP_STRING is displayed.
21355
21356 FIELD_WIDTH is the minimum number of output glyphs to produce.
21357 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21358 with spaces. FIELD_WIDTH <= 0 means don't pad.
21359
21360 PRECISION is the maximum number of characters to output from
21361 STRING. PRECISION <= 0 means don't truncate the string.
21362
21363 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21364 properties to the string.
21365
21366 PROPS are the properties to add to the string.
21367 The mode_line_string_face face property is always added to the string.
21368 */
21369
21370 static int
21371 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21372 int field_width, int precision, Lisp_Object props)
21373 {
21374 ptrdiff_t len;
21375 int n = 0;
21376
21377 if (string != NULL)
21378 {
21379 len = strlen (string);
21380 if (precision > 0 && len > precision)
21381 len = precision;
21382 lisp_string = make_string (string, len);
21383 if (NILP (props))
21384 props = mode_line_string_face_prop;
21385 else if (!NILP (mode_line_string_face))
21386 {
21387 Lisp_Object face = Fplist_get (props, Qface);
21388 props = Fcopy_sequence (props);
21389 if (NILP (face))
21390 face = mode_line_string_face;
21391 else
21392 face = list2 (face, mode_line_string_face);
21393 props = Fplist_put (props, Qface, face);
21394 }
21395 Fadd_text_properties (make_number (0), make_number (len),
21396 props, lisp_string);
21397 }
21398 else
21399 {
21400 len = XFASTINT (Flength (lisp_string));
21401 if (precision > 0 && len > precision)
21402 {
21403 len = precision;
21404 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21405 precision = -1;
21406 }
21407 if (!NILP (mode_line_string_face))
21408 {
21409 Lisp_Object face;
21410 if (NILP (props))
21411 props = Ftext_properties_at (make_number (0), lisp_string);
21412 face = Fplist_get (props, Qface);
21413 if (NILP (face))
21414 face = mode_line_string_face;
21415 else
21416 face = list2 (face, mode_line_string_face);
21417 props = list2 (Qface, face);
21418 if (copy_string)
21419 lisp_string = Fcopy_sequence (lisp_string);
21420 }
21421 if (!NILP (props))
21422 Fadd_text_properties (make_number (0), make_number (len),
21423 props, lisp_string);
21424 }
21425
21426 if (len > 0)
21427 {
21428 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21429 n += len;
21430 }
21431
21432 if (field_width > len)
21433 {
21434 field_width -= len;
21435 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21436 if (!NILP (props))
21437 Fadd_text_properties (make_number (0), make_number (field_width),
21438 props, lisp_string);
21439 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21440 n += field_width;
21441 }
21442
21443 return n;
21444 }
21445
21446
21447 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21448 1, 4, 0,
21449 doc: /* Format a string out of a mode line format specification.
21450 First arg FORMAT specifies the mode line format (see `mode-line-format'
21451 for details) to use.
21452
21453 By default, the format is evaluated for the currently selected window.
21454
21455 Optional second arg FACE specifies the face property to put on all
21456 characters for which no face is specified. The value nil means the
21457 default face. The value t means whatever face the window's mode line
21458 currently uses (either `mode-line' or `mode-line-inactive',
21459 depending on whether the window is the selected window or not).
21460 An integer value means the value string has no text
21461 properties.
21462
21463 Optional third and fourth args WINDOW and BUFFER specify the window
21464 and buffer to use as the context for the formatting (defaults
21465 are the selected window and the WINDOW's buffer). */)
21466 (Lisp_Object format, Lisp_Object face,
21467 Lisp_Object window, Lisp_Object buffer)
21468 {
21469 struct it it;
21470 int len;
21471 struct window *w;
21472 struct buffer *old_buffer = NULL;
21473 int face_id;
21474 int no_props = INTEGERP (face);
21475 ptrdiff_t count = SPECPDL_INDEX ();
21476 Lisp_Object str;
21477 int string_start = 0;
21478
21479 w = decode_any_window (window);
21480 XSETWINDOW (window, w);
21481
21482 if (NILP (buffer))
21483 buffer = w->contents;
21484 CHECK_BUFFER (buffer);
21485
21486 /* Make formatting the modeline a non-op when noninteractive, otherwise
21487 there will be problems later caused by a partially initialized frame. */
21488 if (NILP (format) || noninteractive)
21489 return empty_unibyte_string;
21490
21491 if (no_props)
21492 face = Qnil;
21493
21494 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21495 : EQ (face, Qt) ? (EQ (window, selected_window)
21496 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21497 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21498 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21499 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21500 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21501 : DEFAULT_FACE_ID;
21502
21503 old_buffer = current_buffer;
21504
21505 /* Save things including mode_line_proptrans_alist,
21506 and set that to nil so that we don't alter the outer value. */
21507 record_unwind_protect (unwind_format_mode_line,
21508 format_mode_line_unwind_data
21509 (XFRAME (WINDOW_FRAME (w)),
21510 old_buffer, selected_window, 1));
21511 mode_line_proptrans_alist = Qnil;
21512
21513 Fselect_window (window, Qt);
21514 set_buffer_internal_1 (XBUFFER (buffer));
21515
21516 init_iterator (&it, w, -1, -1, NULL, face_id);
21517
21518 if (no_props)
21519 {
21520 mode_line_target = MODE_LINE_NOPROP;
21521 mode_line_string_face_prop = Qnil;
21522 mode_line_string_list = Qnil;
21523 string_start = MODE_LINE_NOPROP_LEN (0);
21524 }
21525 else
21526 {
21527 mode_line_target = MODE_LINE_STRING;
21528 mode_line_string_list = Qnil;
21529 mode_line_string_face = face;
21530 mode_line_string_face_prop
21531 = NILP (face) ? Qnil : list2 (Qface, face);
21532 }
21533
21534 push_kboard (FRAME_KBOARD (it.f));
21535 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21536 pop_kboard ();
21537
21538 if (no_props)
21539 {
21540 len = MODE_LINE_NOPROP_LEN (string_start);
21541 str = make_string (mode_line_noprop_buf + string_start, len);
21542 }
21543 else
21544 {
21545 mode_line_string_list = Fnreverse (mode_line_string_list);
21546 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21547 empty_unibyte_string);
21548 }
21549
21550 unbind_to (count, Qnil);
21551 return str;
21552 }
21553
21554 /* Write a null-terminated, right justified decimal representation of
21555 the positive integer D to BUF using a minimal field width WIDTH. */
21556
21557 static void
21558 pint2str (register char *buf, register int width, register ptrdiff_t d)
21559 {
21560 register char *p = buf;
21561
21562 if (d <= 0)
21563 *p++ = '0';
21564 else
21565 {
21566 while (d > 0)
21567 {
21568 *p++ = d % 10 + '0';
21569 d /= 10;
21570 }
21571 }
21572
21573 for (width -= (int) (p - buf); width > 0; --width)
21574 *p++ = ' ';
21575 *p-- = '\0';
21576 while (p > buf)
21577 {
21578 d = *buf;
21579 *buf++ = *p;
21580 *p-- = d;
21581 }
21582 }
21583
21584 /* Write a null-terminated, right justified decimal and "human
21585 readable" representation of the nonnegative integer D to BUF using
21586 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21587
21588 static const char power_letter[] =
21589 {
21590 0, /* no letter */
21591 'k', /* kilo */
21592 'M', /* mega */
21593 'G', /* giga */
21594 'T', /* tera */
21595 'P', /* peta */
21596 'E', /* exa */
21597 'Z', /* zetta */
21598 'Y' /* yotta */
21599 };
21600
21601 static void
21602 pint2hrstr (char *buf, int width, ptrdiff_t d)
21603 {
21604 /* We aim to represent the nonnegative integer D as
21605 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21606 ptrdiff_t quotient = d;
21607 int remainder = 0;
21608 /* -1 means: do not use TENTHS. */
21609 int tenths = -1;
21610 int exponent = 0;
21611
21612 /* Length of QUOTIENT.TENTHS as a string. */
21613 int length;
21614
21615 char * psuffix;
21616 char * p;
21617
21618 if (quotient >= 1000)
21619 {
21620 /* Scale to the appropriate EXPONENT. */
21621 do
21622 {
21623 remainder = quotient % 1000;
21624 quotient /= 1000;
21625 exponent++;
21626 }
21627 while (quotient >= 1000);
21628
21629 /* Round to nearest and decide whether to use TENTHS or not. */
21630 if (quotient <= 9)
21631 {
21632 tenths = remainder / 100;
21633 if (remainder % 100 >= 50)
21634 {
21635 if (tenths < 9)
21636 tenths++;
21637 else
21638 {
21639 quotient++;
21640 if (quotient == 10)
21641 tenths = -1;
21642 else
21643 tenths = 0;
21644 }
21645 }
21646 }
21647 else
21648 if (remainder >= 500)
21649 {
21650 if (quotient < 999)
21651 quotient++;
21652 else
21653 {
21654 quotient = 1;
21655 exponent++;
21656 tenths = 0;
21657 }
21658 }
21659 }
21660
21661 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21662 if (tenths == -1 && quotient <= 99)
21663 if (quotient <= 9)
21664 length = 1;
21665 else
21666 length = 2;
21667 else
21668 length = 3;
21669 p = psuffix = buf + max (width, length);
21670
21671 /* Print EXPONENT. */
21672 *psuffix++ = power_letter[exponent];
21673 *psuffix = '\0';
21674
21675 /* Print TENTHS. */
21676 if (tenths >= 0)
21677 {
21678 *--p = '0' + tenths;
21679 *--p = '.';
21680 }
21681
21682 /* Print QUOTIENT. */
21683 do
21684 {
21685 int digit = quotient % 10;
21686 *--p = '0' + digit;
21687 }
21688 while ((quotient /= 10) != 0);
21689
21690 /* Print leading spaces. */
21691 while (buf < p)
21692 *--p = ' ';
21693 }
21694
21695 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21696 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21697 type of CODING_SYSTEM. Return updated pointer into BUF. */
21698
21699 static unsigned char invalid_eol_type[] = "(*invalid*)";
21700
21701 static char *
21702 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21703 {
21704 Lisp_Object val;
21705 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21706 const unsigned char *eol_str;
21707 int eol_str_len;
21708 /* The EOL conversion we are using. */
21709 Lisp_Object eoltype;
21710
21711 val = CODING_SYSTEM_SPEC (coding_system);
21712 eoltype = Qnil;
21713
21714 if (!VECTORP (val)) /* Not yet decided. */
21715 {
21716 *buf++ = multibyte ? '-' : ' ';
21717 if (eol_flag)
21718 eoltype = eol_mnemonic_undecided;
21719 /* Don't mention EOL conversion if it isn't decided. */
21720 }
21721 else
21722 {
21723 Lisp_Object attrs;
21724 Lisp_Object eolvalue;
21725
21726 attrs = AREF (val, 0);
21727 eolvalue = AREF (val, 2);
21728
21729 *buf++ = multibyte
21730 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21731 : ' ';
21732
21733 if (eol_flag)
21734 {
21735 /* The EOL conversion that is normal on this system. */
21736
21737 if (NILP (eolvalue)) /* Not yet decided. */
21738 eoltype = eol_mnemonic_undecided;
21739 else if (VECTORP (eolvalue)) /* Not yet decided. */
21740 eoltype = eol_mnemonic_undecided;
21741 else /* eolvalue is Qunix, Qdos, or Qmac. */
21742 eoltype = (EQ (eolvalue, Qunix)
21743 ? eol_mnemonic_unix
21744 : (EQ (eolvalue, Qdos) == 1
21745 ? eol_mnemonic_dos : eol_mnemonic_mac));
21746 }
21747 }
21748
21749 if (eol_flag)
21750 {
21751 /* Mention the EOL conversion if it is not the usual one. */
21752 if (STRINGP (eoltype))
21753 {
21754 eol_str = SDATA (eoltype);
21755 eol_str_len = SBYTES (eoltype);
21756 }
21757 else if (CHARACTERP (eoltype))
21758 {
21759 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21760 int c = XFASTINT (eoltype);
21761 eol_str_len = CHAR_STRING (c, tmp);
21762 eol_str = tmp;
21763 }
21764 else
21765 {
21766 eol_str = invalid_eol_type;
21767 eol_str_len = sizeof (invalid_eol_type) - 1;
21768 }
21769 memcpy (buf, eol_str, eol_str_len);
21770 buf += eol_str_len;
21771 }
21772
21773 return buf;
21774 }
21775
21776 /* Return a string for the output of a mode line %-spec for window W,
21777 generated by character C. FIELD_WIDTH > 0 means pad the string
21778 returned with spaces to that value. Return a Lisp string in
21779 *STRING if the resulting string is taken from that Lisp string.
21780
21781 Note we operate on the current buffer for most purposes. */
21782
21783 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21784
21785 static const char *
21786 decode_mode_spec (struct window *w, register int c, int field_width,
21787 Lisp_Object *string)
21788 {
21789 Lisp_Object obj;
21790 struct frame *f = XFRAME (WINDOW_FRAME (w));
21791 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21792 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21793 produce strings from numerical values, so limit preposterously
21794 large values of FIELD_WIDTH to avoid overrunning the buffer's
21795 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21796 bytes plus the terminating null. */
21797 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21798 struct buffer *b = current_buffer;
21799
21800 obj = Qnil;
21801 *string = Qnil;
21802
21803 switch (c)
21804 {
21805 case '*':
21806 if (!NILP (BVAR (b, read_only)))
21807 return "%";
21808 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21809 return "*";
21810 return "-";
21811
21812 case '+':
21813 /* This differs from %* only for a modified read-only buffer. */
21814 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21815 return "*";
21816 if (!NILP (BVAR (b, read_only)))
21817 return "%";
21818 return "-";
21819
21820 case '&':
21821 /* This differs from %* in ignoring read-only-ness. */
21822 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21823 return "*";
21824 return "-";
21825
21826 case '%':
21827 return "%";
21828
21829 case '[':
21830 {
21831 int i;
21832 char *p;
21833
21834 if (command_loop_level > 5)
21835 return "[[[... ";
21836 p = decode_mode_spec_buf;
21837 for (i = 0; i < command_loop_level; i++)
21838 *p++ = '[';
21839 *p = 0;
21840 return decode_mode_spec_buf;
21841 }
21842
21843 case ']':
21844 {
21845 int i;
21846 char *p;
21847
21848 if (command_loop_level > 5)
21849 return " ...]]]";
21850 p = decode_mode_spec_buf;
21851 for (i = 0; i < command_loop_level; i++)
21852 *p++ = ']';
21853 *p = 0;
21854 return decode_mode_spec_buf;
21855 }
21856
21857 case '-':
21858 {
21859 register int i;
21860
21861 /* Let lots_of_dashes be a string of infinite length. */
21862 if (mode_line_target == MODE_LINE_NOPROP
21863 || mode_line_target == MODE_LINE_STRING)
21864 return "--";
21865 if (field_width <= 0
21866 || field_width > sizeof (lots_of_dashes))
21867 {
21868 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21869 decode_mode_spec_buf[i] = '-';
21870 decode_mode_spec_buf[i] = '\0';
21871 return decode_mode_spec_buf;
21872 }
21873 else
21874 return lots_of_dashes;
21875 }
21876
21877 case 'b':
21878 obj = BVAR (b, name);
21879 break;
21880
21881 case 'c':
21882 /* %c and %l are ignored in `frame-title-format'.
21883 (In redisplay_internal, the frame title is drawn _before_ the
21884 windows are updated, so the stuff which depends on actual
21885 window contents (such as %l) may fail to render properly, or
21886 even crash emacs.) */
21887 if (mode_line_target == MODE_LINE_TITLE)
21888 return "";
21889 else
21890 {
21891 ptrdiff_t col = current_column ();
21892 w->column_number_displayed = col;
21893 pint2str (decode_mode_spec_buf, width, col);
21894 return decode_mode_spec_buf;
21895 }
21896
21897 case 'e':
21898 #ifndef SYSTEM_MALLOC
21899 {
21900 if (NILP (Vmemory_full))
21901 return "";
21902 else
21903 return "!MEM FULL! ";
21904 }
21905 #else
21906 return "";
21907 #endif
21908
21909 case 'F':
21910 /* %F displays the frame name. */
21911 if (!NILP (f->title))
21912 return SSDATA (f->title);
21913 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21914 return SSDATA (f->name);
21915 return "Emacs";
21916
21917 case 'f':
21918 obj = BVAR (b, filename);
21919 break;
21920
21921 case 'i':
21922 {
21923 ptrdiff_t size = ZV - BEGV;
21924 pint2str (decode_mode_spec_buf, width, size);
21925 return decode_mode_spec_buf;
21926 }
21927
21928 case 'I':
21929 {
21930 ptrdiff_t size = ZV - BEGV;
21931 pint2hrstr (decode_mode_spec_buf, width, size);
21932 return decode_mode_spec_buf;
21933 }
21934
21935 case 'l':
21936 {
21937 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21938 ptrdiff_t topline, nlines, height;
21939 ptrdiff_t junk;
21940
21941 /* %c and %l are ignored in `frame-title-format'. */
21942 if (mode_line_target == MODE_LINE_TITLE)
21943 return "";
21944
21945 startpos = marker_position (w->start);
21946 startpos_byte = marker_byte_position (w->start);
21947 height = WINDOW_TOTAL_LINES (w);
21948
21949 /* If we decided that this buffer isn't suitable for line numbers,
21950 don't forget that too fast. */
21951 if (w->base_line_pos == -1)
21952 goto no_value;
21953
21954 /* If the buffer is very big, don't waste time. */
21955 if (INTEGERP (Vline_number_display_limit)
21956 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21957 {
21958 w->base_line_pos = 0;
21959 w->base_line_number = 0;
21960 goto no_value;
21961 }
21962
21963 if (w->base_line_number > 0
21964 && w->base_line_pos > 0
21965 && w->base_line_pos <= startpos)
21966 {
21967 line = w->base_line_number;
21968 linepos = w->base_line_pos;
21969 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21970 }
21971 else
21972 {
21973 line = 1;
21974 linepos = BUF_BEGV (b);
21975 linepos_byte = BUF_BEGV_BYTE (b);
21976 }
21977
21978 /* Count lines from base line to window start position. */
21979 nlines = display_count_lines (linepos_byte,
21980 startpos_byte,
21981 startpos, &junk);
21982
21983 topline = nlines + line;
21984
21985 /* Determine a new base line, if the old one is too close
21986 or too far away, or if we did not have one.
21987 "Too close" means it's plausible a scroll-down would
21988 go back past it. */
21989 if (startpos == BUF_BEGV (b))
21990 {
21991 w->base_line_number = topline;
21992 w->base_line_pos = BUF_BEGV (b);
21993 }
21994 else if (nlines < height + 25 || nlines > height * 3 + 50
21995 || linepos == BUF_BEGV (b))
21996 {
21997 ptrdiff_t limit = BUF_BEGV (b);
21998 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21999 ptrdiff_t position;
22000 ptrdiff_t distance =
22001 (height * 2 + 30) * line_number_display_limit_width;
22002
22003 if (startpos - distance > limit)
22004 {
22005 limit = startpos - distance;
22006 limit_byte = CHAR_TO_BYTE (limit);
22007 }
22008
22009 nlines = display_count_lines (startpos_byte,
22010 limit_byte,
22011 - (height * 2 + 30),
22012 &position);
22013 /* If we couldn't find the lines we wanted within
22014 line_number_display_limit_width chars per line,
22015 give up on line numbers for this window. */
22016 if (position == limit_byte && limit == startpos - distance)
22017 {
22018 w->base_line_pos = -1;
22019 w->base_line_number = 0;
22020 goto no_value;
22021 }
22022
22023 w->base_line_number = topline - nlines;
22024 w->base_line_pos = BYTE_TO_CHAR (position);
22025 }
22026
22027 /* Now count lines from the start pos to point. */
22028 nlines = display_count_lines (startpos_byte,
22029 PT_BYTE, PT, &junk);
22030
22031 /* Record that we did display the line number. */
22032 line_number_displayed = 1;
22033
22034 /* Make the string to show. */
22035 pint2str (decode_mode_spec_buf, width, topline + nlines);
22036 return decode_mode_spec_buf;
22037 no_value:
22038 {
22039 char* p = decode_mode_spec_buf;
22040 int pad = width - 2;
22041 while (pad-- > 0)
22042 *p++ = ' ';
22043 *p++ = '?';
22044 *p++ = '?';
22045 *p = '\0';
22046 return decode_mode_spec_buf;
22047 }
22048 }
22049 break;
22050
22051 case 'm':
22052 obj = BVAR (b, mode_name);
22053 break;
22054
22055 case 'n':
22056 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22057 return " Narrow";
22058 break;
22059
22060 case 'p':
22061 {
22062 ptrdiff_t pos = marker_position (w->start);
22063 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22064
22065 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22066 {
22067 if (pos <= BUF_BEGV (b))
22068 return "All";
22069 else
22070 return "Bottom";
22071 }
22072 else if (pos <= BUF_BEGV (b))
22073 return "Top";
22074 else
22075 {
22076 if (total > 1000000)
22077 /* Do it differently for a large value, to avoid overflow. */
22078 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22079 else
22080 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22081 /* We can't normally display a 3-digit number,
22082 so get us a 2-digit number that is close. */
22083 if (total == 100)
22084 total = 99;
22085 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22086 return decode_mode_spec_buf;
22087 }
22088 }
22089
22090 /* Display percentage of size above the bottom of the screen. */
22091 case 'P':
22092 {
22093 ptrdiff_t toppos = marker_position (w->start);
22094 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22095 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22096
22097 if (botpos >= BUF_ZV (b))
22098 {
22099 if (toppos <= BUF_BEGV (b))
22100 return "All";
22101 else
22102 return "Bottom";
22103 }
22104 else
22105 {
22106 if (total > 1000000)
22107 /* Do it differently for a large value, to avoid overflow. */
22108 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22109 else
22110 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22111 /* We can't normally display a 3-digit number,
22112 so get us a 2-digit number that is close. */
22113 if (total == 100)
22114 total = 99;
22115 if (toppos <= BUF_BEGV (b))
22116 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22117 else
22118 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22119 return decode_mode_spec_buf;
22120 }
22121 }
22122
22123 case 's':
22124 /* status of process */
22125 obj = Fget_buffer_process (Fcurrent_buffer ());
22126 if (NILP (obj))
22127 return "no process";
22128 #ifndef MSDOS
22129 obj = Fsymbol_name (Fprocess_status (obj));
22130 #endif
22131 break;
22132
22133 case '@':
22134 {
22135 ptrdiff_t count = inhibit_garbage_collection ();
22136 Lisp_Object val = call1 (intern ("file-remote-p"),
22137 BVAR (current_buffer, directory));
22138 unbind_to (count, Qnil);
22139
22140 if (NILP (val))
22141 return "-";
22142 else
22143 return "@";
22144 }
22145
22146 case 'z':
22147 /* coding-system (not including end-of-line format) */
22148 case 'Z':
22149 /* coding-system (including end-of-line type) */
22150 {
22151 int eol_flag = (c == 'Z');
22152 char *p = decode_mode_spec_buf;
22153
22154 if (! FRAME_WINDOW_P (f))
22155 {
22156 /* No need to mention EOL here--the terminal never needs
22157 to do EOL conversion. */
22158 p = decode_mode_spec_coding (CODING_ID_NAME
22159 (FRAME_KEYBOARD_CODING (f)->id),
22160 p, 0);
22161 p = decode_mode_spec_coding (CODING_ID_NAME
22162 (FRAME_TERMINAL_CODING (f)->id),
22163 p, 0);
22164 }
22165 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22166 p, eol_flag);
22167
22168 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22169 #ifdef subprocesses
22170 obj = Fget_buffer_process (Fcurrent_buffer ());
22171 if (PROCESSP (obj))
22172 {
22173 p = decode_mode_spec_coding
22174 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22175 p = decode_mode_spec_coding
22176 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22177 }
22178 #endif /* subprocesses */
22179 #endif /* 0 */
22180 *p = 0;
22181 return decode_mode_spec_buf;
22182 }
22183 }
22184
22185 if (STRINGP (obj))
22186 {
22187 *string = obj;
22188 return SSDATA (obj);
22189 }
22190 else
22191 return "";
22192 }
22193
22194
22195 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22196 means count lines back from START_BYTE. But don't go beyond
22197 LIMIT_BYTE. Return the number of lines thus found (always
22198 nonnegative).
22199
22200 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22201 either the position COUNT lines after/before START_BYTE, if we
22202 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22203 COUNT lines. */
22204
22205 static ptrdiff_t
22206 display_count_lines (ptrdiff_t start_byte,
22207 ptrdiff_t limit_byte, ptrdiff_t count,
22208 ptrdiff_t *byte_pos_ptr)
22209 {
22210 register unsigned char *cursor;
22211 unsigned char *base;
22212
22213 register ptrdiff_t ceiling;
22214 register unsigned char *ceiling_addr;
22215 ptrdiff_t orig_count = count;
22216
22217 /* If we are not in selective display mode,
22218 check only for newlines. */
22219 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22220 && !INTEGERP (BVAR (current_buffer, selective_display)));
22221
22222 if (count > 0)
22223 {
22224 while (start_byte < limit_byte)
22225 {
22226 ceiling = BUFFER_CEILING_OF (start_byte);
22227 ceiling = min (limit_byte - 1, ceiling);
22228 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22229 base = (cursor = BYTE_POS_ADDR (start_byte));
22230
22231 do
22232 {
22233 if (selective_display)
22234 {
22235 while (*cursor != '\n' && *cursor != 015
22236 && ++cursor != ceiling_addr)
22237 continue;
22238 if (cursor == ceiling_addr)
22239 break;
22240 }
22241 else
22242 {
22243 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22244 if (! cursor)
22245 break;
22246 }
22247
22248 cursor++;
22249
22250 if (--count == 0)
22251 {
22252 start_byte += cursor - base;
22253 *byte_pos_ptr = start_byte;
22254 return orig_count;
22255 }
22256 }
22257 while (cursor < ceiling_addr);
22258
22259 start_byte += ceiling_addr - base;
22260 }
22261 }
22262 else
22263 {
22264 while (start_byte > limit_byte)
22265 {
22266 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22267 ceiling = max (limit_byte, ceiling);
22268 ceiling_addr = BYTE_POS_ADDR (ceiling);
22269 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22270 while (1)
22271 {
22272 if (selective_display)
22273 {
22274 while (--cursor >= ceiling_addr
22275 && *cursor != '\n' && *cursor != 015)
22276 continue;
22277 if (cursor < ceiling_addr)
22278 break;
22279 }
22280 else
22281 {
22282 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22283 if (! cursor)
22284 break;
22285 }
22286
22287 if (++count == 0)
22288 {
22289 start_byte += cursor - base + 1;
22290 *byte_pos_ptr = start_byte;
22291 /* When scanning backwards, we should
22292 not count the newline posterior to which we stop. */
22293 return - orig_count - 1;
22294 }
22295 }
22296 start_byte += ceiling_addr - base;
22297 }
22298 }
22299
22300 *byte_pos_ptr = limit_byte;
22301
22302 if (count < 0)
22303 return - orig_count + count;
22304 return orig_count - count;
22305
22306 }
22307
22308
22309 \f
22310 /***********************************************************************
22311 Displaying strings
22312 ***********************************************************************/
22313
22314 /* Display a NUL-terminated string, starting with index START.
22315
22316 If STRING is non-null, display that C string. Otherwise, the Lisp
22317 string LISP_STRING is displayed. There's a case that STRING is
22318 non-null and LISP_STRING is not nil. It means STRING is a string
22319 data of LISP_STRING. In that case, we display LISP_STRING while
22320 ignoring its text properties.
22321
22322 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22323 FACE_STRING. Display STRING or LISP_STRING with the face at
22324 FACE_STRING_POS in FACE_STRING:
22325
22326 Display the string in the environment given by IT, but use the
22327 standard display table, temporarily.
22328
22329 FIELD_WIDTH is the minimum number of output glyphs to produce.
22330 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22331 with spaces. If STRING has more characters, more than FIELD_WIDTH
22332 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22333
22334 PRECISION is the maximum number of characters to output from
22335 STRING. PRECISION < 0 means don't truncate the string.
22336
22337 This is roughly equivalent to printf format specifiers:
22338
22339 FIELD_WIDTH PRECISION PRINTF
22340 ----------------------------------------
22341 -1 -1 %s
22342 -1 10 %.10s
22343 10 -1 %10s
22344 20 10 %20.10s
22345
22346 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22347 display them, and < 0 means obey the current buffer's value of
22348 enable_multibyte_characters.
22349
22350 Value is the number of columns displayed. */
22351
22352 static int
22353 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22354 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22355 int field_width, int precision, int max_x, int multibyte)
22356 {
22357 int hpos_at_start = it->hpos;
22358 int saved_face_id = it->face_id;
22359 struct glyph_row *row = it->glyph_row;
22360 ptrdiff_t it_charpos;
22361
22362 /* Initialize the iterator IT for iteration over STRING beginning
22363 with index START. */
22364 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22365 precision, field_width, multibyte);
22366 if (string && STRINGP (lisp_string))
22367 /* LISP_STRING is the one returned by decode_mode_spec. We should
22368 ignore its text properties. */
22369 it->stop_charpos = it->end_charpos;
22370
22371 /* If displaying STRING, set up the face of the iterator from
22372 FACE_STRING, if that's given. */
22373 if (STRINGP (face_string))
22374 {
22375 ptrdiff_t endptr;
22376 struct face *face;
22377
22378 it->face_id
22379 = face_at_string_position (it->w, face_string, face_string_pos,
22380 0, it->region_beg_charpos,
22381 it->region_end_charpos,
22382 &endptr, it->base_face_id, 0);
22383 face = FACE_FROM_ID (it->f, it->face_id);
22384 it->face_box_p = face->box != FACE_NO_BOX;
22385 }
22386
22387 /* Set max_x to the maximum allowed X position. Don't let it go
22388 beyond the right edge of the window. */
22389 if (max_x <= 0)
22390 max_x = it->last_visible_x;
22391 else
22392 max_x = min (max_x, it->last_visible_x);
22393
22394 /* Skip over display elements that are not visible. because IT->w is
22395 hscrolled. */
22396 if (it->current_x < it->first_visible_x)
22397 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22398 MOVE_TO_POS | MOVE_TO_X);
22399
22400 row->ascent = it->max_ascent;
22401 row->height = it->max_ascent + it->max_descent;
22402 row->phys_ascent = it->max_phys_ascent;
22403 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22404 row->extra_line_spacing = it->max_extra_line_spacing;
22405
22406 if (STRINGP (it->string))
22407 it_charpos = IT_STRING_CHARPOS (*it);
22408 else
22409 it_charpos = IT_CHARPOS (*it);
22410
22411 /* This condition is for the case that we are called with current_x
22412 past last_visible_x. */
22413 while (it->current_x < max_x)
22414 {
22415 int x_before, x, n_glyphs_before, i, nglyphs;
22416
22417 /* Get the next display element. */
22418 if (!get_next_display_element (it))
22419 break;
22420
22421 /* Produce glyphs. */
22422 x_before = it->current_x;
22423 n_glyphs_before = row->used[TEXT_AREA];
22424 PRODUCE_GLYPHS (it);
22425
22426 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22427 i = 0;
22428 x = x_before;
22429 while (i < nglyphs)
22430 {
22431 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22432
22433 if (it->line_wrap != TRUNCATE
22434 && x + glyph->pixel_width > max_x)
22435 {
22436 /* End of continued line or max_x reached. */
22437 if (CHAR_GLYPH_PADDING_P (*glyph))
22438 {
22439 /* A wide character is unbreakable. */
22440 if (row->reversed_p)
22441 unproduce_glyphs (it, row->used[TEXT_AREA]
22442 - n_glyphs_before);
22443 row->used[TEXT_AREA] = n_glyphs_before;
22444 it->current_x = x_before;
22445 }
22446 else
22447 {
22448 if (row->reversed_p)
22449 unproduce_glyphs (it, row->used[TEXT_AREA]
22450 - (n_glyphs_before + i));
22451 row->used[TEXT_AREA] = n_glyphs_before + i;
22452 it->current_x = x;
22453 }
22454 break;
22455 }
22456 else if (x + glyph->pixel_width >= it->first_visible_x)
22457 {
22458 /* Glyph is at least partially visible. */
22459 ++it->hpos;
22460 if (x < it->first_visible_x)
22461 row->x = x - it->first_visible_x;
22462 }
22463 else
22464 {
22465 /* Glyph is off the left margin of the display area.
22466 Should not happen. */
22467 emacs_abort ();
22468 }
22469
22470 row->ascent = max (row->ascent, it->max_ascent);
22471 row->height = max (row->height, it->max_ascent + it->max_descent);
22472 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22473 row->phys_height = max (row->phys_height,
22474 it->max_phys_ascent + it->max_phys_descent);
22475 row->extra_line_spacing = max (row->extra_line_spacing,
22476 it->max_extra_line_spacing);
22477 x += glyph->pixel_width;
22478 ++i;
22479 }
22480
22481 /* Stop if max_x reached. */
22482 if (i < nglyphs)
22483 break;
22484
22485 /* Stop at line ends. */
22486 if (ITERATOR_AT_END_OF_LINE_P (it))
22487 {
22488 it->continuation_lines_width = 0;
22489 break;
22490 }
22491
22492 set_iterator_to_next (it, 1);
22493 if (STRINGP (it->string))
22494 it_charpos = IT_STRING_CHARPOS (*it);
22495 else
22496 it_charpos = IT_CHARPOS (*it);
22497
22498 /* Stop if truncating at the right edge. */
22499 if (it->line_wrap == TRUNCATE
22500 && it->current_x >= it->last_visible_x)
22501 {
22502 /* Add truncation mark, but don't do it if the line is
22503 truncated at a padding space. */
22504 if (it_charpos < it->string_nchars)
22505 {
22506 if (!FRAME_WINDOW_P (it->f))
22507 {
22508 int ii, n;
22509
22510 if (it->current_x > it->last_visible_x)
22511 {
22512 if (!row->reversed_p)
22513 {
22514 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22515 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22516 break;
22517 }
22518 else
22519 {
22520 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22521 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22522 break;
22523 unproduce_glyphs (it, ii + 1);
22524 ii = row->used[TEXT_AREA] - (ii + 1);
22525 }
22526 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22527 {
22528 row->used[TEXT_AREA] = ii;
22529 produce_special_glyphs (it, IT_TRUNCATION);
22530 }
22531 }
22532 produce_special_glyphs (it, IT_TRUNCATION);
22533 }
22534 row->truncated_on_right_p = 1;
22535 }
22536 break;
22537 }
22538 }
22539
22540 /* Maybe insert a truncation at the left. */
22541 if (it->first_visible_x
22542 && it_charpos > 0)
22543 {
22544 if (!FRAME_WINDOW_P (it->f)
22545 || (row->reversed_p
22546 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22547 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22548 insert_left_trunc_glyphs (it);
22549 row->truncated_on_left_p = 1;
22550 }
22551
22552 it->face_id = saved_face_id;
22553
22554 /* Value is number of columns displayed. */
22555 return it->hpos - hpos_at_start;
22556 }
22557
22558
22559 \f
22560 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22561 appears as an element of LIST or as the car of an element of LIST.
22562 If PROPVAL is a list, compare each element against LIST in that
22563 way, and return 1/2 if any element of PROPVAL is found in LIST.
22564 Otherwise return 0. This function cannot quit.
22565 The return value is 2 if the text is invisible but with an ellipsis
22566 and 1 if it's invisible and without an ellipsis. */
22567
22568 int
22569 invisible_p (register Lisp_Object propval, Lisp_Object list)
22570 {
22571 register Lisp_Object tail, proptail;
22572
22573 for (tail = list; CONSP (tail); tail = XCDR (tail))
22574 {
22575 register Lisp_Object tem;
22576 tem = XCAR (tail);
22577 if (EQ (propval, tem))
22578 return 1;
22579 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22580 return NILP (XCDR (tem)) ? 1 : 2;
22581 }
22582
22583 if (CONSP (propval))
22584 {
22585 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22586 {
22587 Lisp_Object propelt;
22588 propelt = XCAR (proptail);
22589 for (tail = list; CONSP (tail); tail = XCDR (tail))
22590 {
22591 register Lisp_Object tem;
22592 tem = XCAR (tail);
22593 if (EQ (propelt, tem))
22594 return 1;
22595 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22596 return NILP (XCDR (tem)) ? 1 : 2;
22597 }
22598 }
22599 }
22600
22601 return 0;
22602 }
22603
22604 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22605 doc: /* Non-nil if the property makes the text invisible.
22606 POS-OR-PROP can be a marker or number, in which case it is taken to be
22607 a position in the current buffer and the value of the `invisible' property
22608 is checked; or it can be some other value, which is then presumed to be the
22609 value of the `invisible' property of the text of interest.
22610 The non-nil value returned can be t for truly invisible text or something
22611 else if the text is replaced by an ellipsis. */)
22612 (Lisp_Object pos_or_prop)
22613 {
22614 Lisp_Object prop
22615 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22616 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22617 : pos_or_prop);
22618 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22619 return (invis == 0 ? Qnil
22620 : invis == 1 ? Qt
22621 : make_number (invis));
22622 }
22623
22624 /* Calculate a width or height in pixels from a specification using
22625 the following elements:
22626
22627 SPEC ::=
22628 NUM - a (fractional) multiple of the default font width/height
22629 (NUM) - specifies exactly NUM pixels
22630 UNIT - a fixed number of pixels, see below.
22631 ELEMENT - size of a display element in pixels, see below.
22632 (NUM . SPEC) - equals NUM * SPEC
22633 (+ SPEC SPEC ...) - add pixel values
22634 (- SPEC SPEC ...) - subtract pixel values
22635 (- SPEC) - negate pixel value
22636
22637 NUM ::=
22638 INT or FLOAT - a number constant
22639 SYMBOL - use symbol's (buffer local) variable binding.
22640
22641 UNIT ::=
22642 in - pixels per inch *)
22643 mm - pixels per 1/1000 meter *)
22644 cm - pixels per 1/100 meter *)
22645 width - width of current font in pixels.
22646 height - height of current font in pixels.
22647
22648 *) using the ratio(s) defined in display-pixels-per-inch.
22649
22650 ELEMENT ::=
22651
22652 left-fringe - left fringe width in pixels
22653 right-fringe - right fringe width in pixels
22654
22655 left-margin - left margin width in pixels
22656 right-margin - right margin width in pixels
22657
22658 scroll-bar - scroll-bar area width in pixels
22659
22660 Examples:
22661
22662 Pixels corresponding to 5 inches:
22663 (5 . in)
22664
22665 Total width of non-text areas on left side of window (if scroll-bar is on left):
22666 '(space :width (+ left-fringe left-margin scroll-bar))
22667
22668 Align to first text column (in header line):
22669 '(space :align-to 0)
22670
22671 Align to middle of text area minus half the width of variable `my-image'
22672 containing a loaded image:
22673 '(space :align-to (0.5 . (- text my-image)))
22674
22675 Width of left margin minus width of 1 character in the default font:
22676 '(space :width (- left-margin 1))
22677
22678 Width of left margin minus width of 2 characters in the current font:
22679 '(space :width (- left-margin (2 . width)))
22680
22681 Center 1 character over left-margin (in header line):
22682 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22683
22684 Different ways to express width of left fringe plus left margin minus one pixel:
22685 '(space :width (- (+ left-fringe left-margin) (1)))
22686 '(space :width (+ left-fringe left-margin (- (1))))
22687 '(space :width (+ left-fringe left-margin (-1)))
22688
22689 */
22690
22691 static int
22692 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22693 struct font *font, int width_p, int *align_to)
22694 {
22695 double pixels;
22696
22697 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22698 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22699
22700 if (NILP (prop))
22701 return OK_PIXELS (0);
22702
22703 eassert (FRAME_LIVE_P (it->f));
22704
22705 if (SYMBOLP (prop))
22706 {
22707 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22708 {
22709 char *unit = SSDATA (SYMBOL_NAME (prop));
22710
22711 if (unit[0] == 'i' && unit[1] == 'n')
22712 pixels = 1.0;
22713 else if (unit[0] == 'm' && unit[1] == 'm')
22714 pixels = 25.4;
22715 else if (unit[0] == 'c' && unit[1] == 'm')
22716 pixels = 2.54;
22717 else
22718 pixels = 0;
22719 if (pixels > 0)
22720 {
22721 double ppi = (width_p ? FRAME_RES_X (it->f)
22722 : FRAME_RES_Y (it->f));
22723
22724 if (ppi > 0)
22725 return OK_PIXELS (ppi / pixels);
22726 return 0;
22727 }
22728 }
22729
22730 #ifdef HAVE_WINDOW_SYSTEM
22731 if (EQ (prop, Qheight))
22732 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22733 if (EQ (prop, Qwidth))
22734 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22735 #else
22736 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22737 return OK_PIXELS (1);
22738 #endif
22739
22740 if (EQ (prop, Qtext))
22741 return OK_PIXELS (width_p
22742 ? window_box_width (it->w, TEXT_AREA)
22743 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22744
22745 if (align_to && *align_to < 0)
22746 {
22747 *res = 0;
22748 if (EQ (prop, Qleft))
22749 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22750 if (EQ (prop, Qright))
22751 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22752 if (EQ (prop, Qcenter))
22753 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22754 + window_box_width (it->w, TEXT_AREA) / 2);
22755 if (EQ (prop, Qleft_fringe))
22756 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22757 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22758 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22759 if (EQ (prop, Qright_fringe))
22760 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22761 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22762 : window_box_right_offset (it->w, TEXT_AREA));
22763 if (EQ (prop, Qleft_margin))
22764 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22765 if (EQ (prop, Qright_margin))
22766 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22767 if (EQ (prop, Qscroll_bar))
22768 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22769 ? 0
22770 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22771 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22772 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22773 : 0)));
22774 }
22775 else
22776 {
22777 if (EQ (prop, Qleft_fringe))
22778 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22779 if (EQ (prop, Qright_fringe))
22780 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22781 if (EQ (prop, Qleft_margin))
22782 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22783 if (EQ (prop, Qright_margin))
22784 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22785 if (EQ (prop, Qscroll_bar))
22786 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22787 }
22788
22789 prop = buffer_local_value_1 (prop, it->w->contents);
22790 if (EQ (prop, Qunbound))
22791 prop = Qnil;
22792 }
22793
22794 if (INTEGERP (prop) || FLOATP (prop))
22795 {
22796 int base_unit = (width_p
22797 ? FRAME_COLUMN_WIDTH (it->f)
22798 : FRAME_LINE_HEIGHT (it->f));
22799 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22800 }
22801
22802 if (CONSP (prop))
22803 {
22804 Lisp_Object car = XCAR (prop);
22805 Lisp_Object cdr = XCDR (prop);
22806
22807 if (SYMBOLP (car))
22808 {
22809 #ifdef HAVE_WINDOW_SYSTEM
22810 if (FRAME_WINDOW_P (it->f)
22811 && valid_image_p (prop))
22812 {
22813 ptrdiff_t id = lookup_image (it->f, prop);
22814 struct image *img = IMAGE_FROM_ID (it->f, id);
22815
22816 return OK_PIXELS (width_p ? img->width : img->height);
22817 }
22818 #ifdef HAVE_XWIDGETS
22819 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
22820 {
22821 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
22822 return OK_PIXELS (width_p ? 100 : 100);
22823 }
22824 #endif
22825 #endif
22826 if (EQ (car, Qplus) || EQ (car, Qminus))
22827 {
22828 int first = 1;
22829 double px;
22830
22831 pixels = 0;
22832 while (CONSP (cdr))
22833 {
22834 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22835 font, width_p, align_to))
22836 return 0;
22837 if (first)
22838 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22839 else
22840 pixels += px;
22841 cdr = XCDR (cdr);
22842 }
22843 if (EQ (car, Qminus))
22844 pixels = -pixels;
22845 return OK_PIXELS (pixels);
22846 }
22847
22848 car = buffer_local_value_1 (car, it->w->contents);
22849 if (EQ (car, Qunbound))
22850 car = Qnil;
22851 }
22852
22853 if (INTEGERP (car) || FLOATP (car))
22854 {
22855 double fact;
22856 pixels = XFLOATINT (car);
22857 if (NILP (cdr))
22858 return OK_PIXELS (pixels);
22859 if (calc_pixel_width_or_height (&fact, it, cdr,
22860 font, width_p, align_to))
22861 return OK_PIXELS (pixels * fact);
22862 return 0;
22863 }
22864
22865 return 0;
22866 }
22867
22868 return 0;
22869 }
22870
22871 \f
22872 /***********************************************************************
22873 Glyph Display
22874 ***********************************************************************/
22875
22876 #ifdef HAVE_WINDOW_SYSTEM
22877
22878 #ifdef GLYPH_DEBUG
22879
22880 void
22881 dump_glyph_string (struct glyph_string *s)
22882 {
22883 fprintf (stderr, "glyph string\n");
22884 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22885 s->x, s->y, s->width, s->height);
22886 fprintf (stderr, " ybase = %d\n", s->ybase);
22887 fprintf (stderr, " hl = %d\n", s->hl);
22888 fprintf (stderr, " left overhang = %d, right = %d\n",
22889 s->left_overhang, s->right_overhang);
22890 fprintf (stderr, " nchars = %d\n", s->nchars);
22891 fprintf (stderr, " extends to end of line = %d\n",
22892 s->extends_to_end_of_line_p);
22893 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22894 fprintf (stderr, " bg width = %d\n", s->background_width);
22895 }
22896
22897 #endif /* GLYPH_DEBUG */
22898
22899 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22900 of XChar2b structures for S; it can't be allocated in
22901 init_glyph_string because it must be allocated via `alloca'. W
22902 is the window on which S is drawn. ROW and AREA are the glyph row
22903 and area within the row from which S is constructed. START is the
22904 index of the first glyph structure covered by S. HL is a
22905 face-override for drawing S. */
22906
22907 #ifdef HAVE_NTGUI
22908 #define OPTIONAL_HDC(hdc) HDC hdc,
22909 #define DECLARE_HDC(hdc) HDC hdc;
22910 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22911 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22912 #endif
22913
22914 #ifndef OPTIONAL_HDC
22915 #define OPTIONAL_HDC(hdc)
22916 #define DECLARE_HDC(hdc)
22917 #define ALLOCATE_HDC(hdc, f)
22918 #define RELEASE_HDC(hdc, f)
22919 #endif
22920
22921 static void
22922 init_glyph_string (struct glyph_string *s,
22923 OPTIONAL_HDC (hdc)
22924 XChar2b *char2b, struct window *w, struct glyph_row *row,
22925 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22926 {
22927 memset (s, 0, sizeof *s);
22928 s->w = w;
22929 s->f = XFRAME (w->frame);
22930 #ifdef HAVE_NTGUI
22931 s->hdc = hdc;
22932 #endif
22933 s->display = FRAME_X_DISPLAY (s->f);
22934 s->window = FRAME_X_WINDOW (s->f);
22935 s->char2b = char2b;
22936 s->hl = hl;
22937 s->row = row;
22938 s->area = area;
22939 s->first_glyph = row->glyphs[area] + start;
22940 s->height = row->height;
22941 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22942 s->ybase = s->y + row->ascent;
22943 }
22944
22945
22946 /* Append the list of glyph strings with head H and tail T to the list
22947 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22948
22949 static void
22950 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22951 struct glyph_string *h, struct glyph_string *t)
22952 {
22953 if (h)
22954 {
22955 if (*head)
22956 (*tail)->next = h;
22957 else
22958 *head = h;
22959 h->prev = *tail;
22960 *tail = t;
22961 }
22962 }
22963
22964
22965 /* Prepend the list of glyph strings with head H and tail T to the
22966 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22967 result. */
22968
22969 static void
22970 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22971 struct glyph_string *h, struct glyph_string *t)
22972 {
22973 if (h)
22974 {
22975 if (*head)
22976 (*head)->prev = t;
22977 else
22978 *tail = t;
22979 t->next = *head;
22980 *head = h;
22981 }
22982 }
22983
22984
22985 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22986 Set *HEAD and *TAIL to the resulting list. */
22987
22988 static void
22989 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22990 struct glyph_string *s)
22991 {
22992 s->next = s->prev = NULL;
22993 append_glyph_string_lists (head, tail, s, s);
22994 }
22995
22996
22997 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22998 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22999 make sure that X resources for the face returned are allocated.
23000 Value is a pointer to a realized face that is ready for display if
23001 DISPLAY_P is non-zero. */
23002
23003 static struct face *
23004 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23005 XChar2b *char2b, int display_p)
23006 {
23007 struct face *face = FACE_FROM_ID (f, face_id);
23008 unsigned code = 0;
23009
23010 if (face->font)
23011 {
23012 code = face->font->driver->encode_char (face->font, c);
23013
23014 if (code == FONT_INVALID_CODE)
23015 code = 0;
23016 }
23017 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23018
23019 /* Make sure X resources of the face are allocated. */
23020 #ifdef HAVE_X_WINDOWS
23021 if (display_p)
23022 #endif
23023 {
23024 eassert (face != NULL);
23025 PREPARE_FACE_FOR_DISPLAY (f, face);
23026 }
23027
23028 return face;
23029 }
23030
23031
23032 /* Get face and two-byte form of character glyph GLYPH on frame F.
23033 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23034 a pointer to a realized face that is ready for display. */
23035
23036 static struct face *
23037 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23038 XChar2b *char2b, int *two_byte_p)
23039 {
23040 struct face *face;
23041 unsigned code = 0;
23042
23043 eassert (glyph->type == CHAR_GLYPH);
23044 face = FACE_FROM_ID (f, glyph->face_id);
23045
23046 /* Make sure X resources of the face are allocated. */
23047 eassert (face != NULL);
23048 PREPARE_FACE_FOR_DISPLAY (f, face);
23049
23050 if (two_byte_p)
23051 *two_byte_p = 0;
23052
23053 if (face->font)
23054 {
23055 if (CHAR_BYTE8_P (glyph->u.ch))
23056 code = CHAR_TO_BYTE8 (glyph->u.ch);
23057 else
23058 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23059
23060 if (code == FONT_INVALID_CODE)
23061 code = 0;
23062 }
23063
23064 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23065 return face;
23066 }
23067
23068
23069 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23070 Return 1 if FONT has a glyph for C, otherwise return 0. */
23071
23072 static int
23073 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23074 {
23075 unsigned code;
23076
23077 if (CHAR_BYTE8_P (c))
23078 code = CHAR_TO_BYTE8 (c);
23079 else
23080 code = font->driver->encode_char (font, c);
23081
23082 if (code == FONT_INVALID_CODE)
23083 return 0;
23084 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23085 return 1;
23086 }
23087
23088
23089 /* Fill glyph string S with composition components specified by S->cmp.
23090
23091 BASE_FACE is the base face of the composition.
23092 S->cmp_from is the index of the first component for S.
23093
23094 OVERLAPS non-zero means S should draw the foreground only, and use
23095 its physical height for clipping. See also draw_glyphs.
23096
23097 Value is the index of a component not in S. */
23098
23099 static int
23100 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23101 int overlaps)
23102 {
23103 int i;
23104 /* For all glyphs of this composition, starting at the offset
23105 S->cmp_from, until we reach the end of the definition or encounter a
23106 glyph that requires the different face, add it to S. */
23107 struct face *face;
23108
23109 eassert (s);
23110
23111 s->for_overlaps = overlaps;
23112 s->face = NULL;
23113 s->font = NULL;
23114 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23115 {
23116 int c = COMPOSITION_GLYPH (s->cmp, i);
23117
23118 /* TAB in a composition means display glyphs with padding space
23119 on the left or right. */
23120 if (c != '\t')
23121 {
23122 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23123 -1, Qnil);
23124
23125 face = get_char_face_and_encoding (s->f, c, face_id,
23126 s->char2b + i, 1);
23127 if (face)
23128 {
23129 if (! s->face)
23130 {
23131 s->face = face;
23132 s->font = s->face->font;
23133 }
23134 else if (s->face != face)
23135 break;
23136 }
23137 }
23138 ++s->nchars;
23139 }
23140 s->cmp_to = i;
23141
23142 if (s->face == NULL)
23143 {
23144 s->face = base_face->ascii_face;
23145 s->font = s->face->font;
23146 }
23147
23148 /* All glyph strings for the same composition has the same width,
23149 i.e. the width set for the first component of the composition. */
23150 s->width = s->first_glyph->pixel_width;
23151
23152 /* If the specified font could not be loaded, use the frame's
23153 default font, but record the fact that we couldn't load it in
23154 the glyph string so that we can draw rectangles for the
23155 characters of the glyph string. */
23156 if (s->font == NULL)
23157 {
23158 s->font_not_found_p = 1;
23159 s->font = FRAME_FONT (s->f);
23160 }
23161
23162 /* Adjust base line for subscript/superscript text. */
23163 s->ybase += s->first_glyph->voffset;
23164
23165 /* This glyph string must always be drawn with 16-bit functions. */
23166 s->two_byte_p = 1;
23167
23168 return s->cmp_to;
23169 }
23170
23171 static int
23172 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23173 int start, int end, int overlaps)
23174 {
23175 struct glyph *glyph, *last;
23176 Lisp_Object lgstring;
23177 int i;
23178
23179 s->for_overlaps = overlaps;
23180 glyph = s->row->glyphs[s->area] + start;
23181 last = s->row->glyphs[s->area] + end;
23182 s->cmp_id = glyph->u.cmp.id;
23183 s->cmp_from = glyph->slice.cmp.from;
23184 s->cmp_to = glyph->slice.cmp.to + 1;
23185 s->face = FACE_FROM_ID (s->f, face_id);
23186 lgstring = composition_gstring_from_id (s->cmp_id);
23187 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23188 glyph++;
23189 while (glyph < last
23190 && glyph->u.cmp.automatic
23191 && glyph->u.cmp.id == s->cmp_id
23192 && s->cmp_to == glyph->slice.cmp.from)
23193 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23194
23195 for (i = s->cmp_from; i < s->cmp_to; i++)
23196 {
23197 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23198 unsigned code = LGLYPH_CODE (lglyph);
23199
23200 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23201 }
23202 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23203 return glyph - s->row->glyphs[s->area];
23204 }
23205
23206
23207 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23208 See the comment of fill_glyph_string for arguments.
23209 Value is the index of the first glyph not in S. */
23210
23211
23212 static int
23213 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23214 int start, int end, int overlaps)
23215 {
23216 struct glyph *glyph, *last;
23217 int voffset;
23218
23219 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23220 s->for_overlaps = overlaps;
23221 glyph = s->row->glyphs[s->area] + start;
23222 last = s->row->glyphs[s->area] + end;
23223 voffset = glyph->voffset;
23224 s->face = FACE_FROM_ID (s->f, face_id);
23225 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23226 s->nchars = 1;
23227 s->width = glyph->pixel_width;
23228 glyph++;
23229 while (glyph < last
23230 && glyph->type == GLYPHLESS_GLYPH
23231 && glyph->voffset == voffset
23232 && glyph->face_id == face_id)
23233 {
23234 s->nchars++;
23235 s->width += glyph->pixel_width;
23236 glyph++;
23237 }
23238 s->ybase += voffset;
23239 return glyph - s->row->glyphs[s->area];
23240 }
23241
23242
23243 /* Fill glyph string S from a sequence of character glyphs.
23244
23245 FACE_ID is the face id of the string. START is the index of the
23246 first glyph to consider, END is the index of the last + 1.
23247 OVERLAPS non-zero means S should draw the foreground only, and use
23248 its physical height for clipping. See also draw_glyphs.
23249
23250 Value is the index of the first glyph not in S. */
23251
23252 static int
23253 fill_glyph_string (struct glyph_string *s, int face_id,
23254 int start, int end, int overlaps)
23255 {
23256 struct glyph *glyph, *last;
23257 int voffset;
23258 int glyph_not_available_p;
23259
23260 eassert (s->f == XFRAME (s->w->frame));
23261 eassert (s->nchars == 0);
23262 eassert (start >= 0 && end > start);
23263
23264 s->for_overlaps = overlaps;
23265 glyph = s->row->glyphs[s->area] + start;
23266 last = s->row->glyphs[s->area] + end;
23267 voffset = glyph->voffset;
23268 s->padding_p = glyph->padding_p;
23269 glyph_not_available_p = glyph->glyph_not_available_p;
23270
23271 while (glyph < last
23272 && glyph->type == CHAR_GLYPH
23273 && glyph->voffset == voffset
23274 /* Same face id implies same font, nowadays. */
23275 && glyph->face_id == face_id
23276 && glyph->glyph_not_available_p == glyph_not_available_p)
23277 {
23278 int two_byte_p;
23279
23280 s->face = get_glyph_face_and_encoding (s->f, glyph,
23281 s->char2b + s->nchars,
23282 &two_byte_p);
23283 s->two_byte_p = two_byte_p;
23284 ++s->nchars;
23285 eassert (s->nchars <= end - start);
23286 s->width += glyph->pixel_width;
23287 if (glyph++->padding_p != s->padding_p)
23288 break;
23289 }
23290
23291 s->font = s->face->font;
23292
23293 /* If the specified font could not be loaded, use the frame's font,
23294 but record the fact that we couldn't load it in
23295 S->font_not_found_p so that we can draw rectangles for the
23296 characters of the glyph string. */
23297 if (s->font == NULL || glyph_not_available_p)
23298 {
23299 s->font_not_found_p = 1;
23300 s->font = FRAME_FONT (s->f);
23301 }
23302
23303 /* Adjust base line for subscript/superscript text. */
23304 s->ybase += voffset;
23305
23306 eassert (s->face && s->face->gc);
23307 return glyph - s->row->glyphs[s->area];
23308 }
23309
23310
23311 /* Fill glyph string S from image glyph S->first_glyph. */
23312
23313 static void
23314 fill_image_glyph_string (struct glyph_string *s)
23315 {
23316 eassert (s->first_glyph->type == IMAGE_GLYPH);
23317 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23318 eassert (s->img);
23319 s->slice = s->first_glyph->slice.img;
23320 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23321 s->font = s->face->font;
23322 s->width = s->first_glyph->pixel_width;
23323
23324 /* Adjust base line for subscript/superscript text. */
23325 s->ybase += s->first_glyph->voffset;
23326 }
23327
23328 #ifdef HAVE_XWIDGETS
23329 static void
23330 fill_xwidget_glyph_string (struct glyph_string *s)
23331 {
23332 eassert (s->first_glyph->type == XWIDGET_GLYPH);
23333 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
23334 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23335 s->font = s->face->font;
23336 s->width = s->first_glyph->pixel_width;
23337 s->ybase += s->first_glyph->voffset;
23338 s->xwidget = s->first_glyph->u.xwidget;
23339 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
23340 }
23341 #endif
23342 /* Fill glyph string S from a sequence of stretch glyphs.
23343
23344 START is the index of the first glyph to consider,
23345 END is the index of the last + 1.
23346
23347 Value is the index of the first glyph not in S. */
23348
23349 static int
23350 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23351 {
23352 struct glyph *glyph, *last;
23353 int voffset, face_id;
23354
23355 eassert (s->first_glyph->type == STRETCH_GLYPH);
23356
23357 glyph = s->row->glyphs[s->area] + start;
23358 last = s->row->glyphs[s->area] + end;
23359 face_id = glyph->face_id;
23360 s->face = FACE_FROM_ID (s->f, face_id);
23361 s->font = s->face->font;
23362 s->width = glyph->pixel_width;
23363 s->nchars = 1;
23364 voffset = glyph->voffset;
23365
23366 for (++glyph;
23367 (glyph < last
23368 && glyph->type == STRETCH_GLYPH
23369 && glyph->voffset == voffset
23370 && glyph->face_id == face_id);
23371 ++glyph)
23372 s->width += glyph->pixel_width;
23373
23374 /* Adjust base line for subscript/superscript text. */
23375 s->ybase += voffset;
23376
23377 /* The case that face->gc == 0 is handled when drawing the glyph
23378 string by calling PREPARE_FACE_FOR_DISPLAY. */
23379 eassert (s->face);
23380 return glyph - s->row->glyphs[s->area];
23381 }
23382
23383 static struct font_metrics *
23384 get_per_char_metric (struct font *font, XChar2b *char2b)
23385 {
23386 static struct font_metrics metrics;
23387 unsigned code;
23388
23389 if (! font)
23390 return NULL;
23391 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23392 if (code == FONT_INVALID_CODE)
23393 return NULL;
23394 font->driver->text_extents (font, &code, 1, &metrics);
23395 return &metrics;
23396 }
23397
23398 /* EXPORT for RIF:
23399 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23400 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23401 assumed to be zero. */
23402
23403 void
23404 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23405 {
23406 *left = *right = 0;
23407
23408 if (glyph->type == CHAR_GLYPH)
23409 {
23410 struct face *face;
23411 XChar2b char2b;
23412 struct font_metrics *pcm;
23413
23414 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23415 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23416 {
23417 if (pcm->rbearing > pcm->width)
23418 *right = pcm->rbearing - pcm->width;
23419 if (pcm->lbearing < 0)
23420 *left = -pcm->lbearing;
23421 }
23422 }
23423 else if (glyph->type == COMPOSITE_GLYPH)
23424 {
23425 if (! glyph->u.cmp.automatic)
23426 {
23427 struct composition *cmp = composition_table[glyph->u.cmp.id];
23428
23429 if (cmp->rbearing > cmp->pixel_width)
23430 *right = cmp->rbearing - cmp->pixel_width;
23431 if (cmp->lbearing < 0)
23432 *left = - cmp->lbearing;
23433 }
23434 else
23435 {
23436 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23437 struct font_metrics metrics;
23438
23439 composition_gstring_width (gstring, glyph->slice.cmp.from,
23440 glyph->slice.cmp.to + 1, &metrics);
23441 if (metrics.rbearing > metrics.width)
23442 *right = metrics.rbearing - metrics.width;
23443 if (metrics.lbearing < 0)
23444 *left = - metrics.lbearing;
23445 }
23446 }
23447 }
23448
23449
23450 /* Return the index of the first glyph preceding glyph string S that
23451 is overwritten by S because of S's left overhang. Value is -1
23452 if no glyphs are overwritten. */
23453
23454 static int
23455 left_overwritten (struct glyph_string *s)
23456 {
23457 int k;
23458
23459 if (s->left_overhang)
23460 {
23461 int x = 0, i;
23462 struct glyph *glyphs = s->row->glyphs[s->area];
23463 int first = s->first_glyph - glyphs;
23464
23465 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23466 x -= glyphs[i].pixel_width;
23467
23468 k = i + 1;
23469 }
23470 else
23471 k = -1;
23472
23473 return k;
23474 }
23475
23476
23477 /* Return the index of the first glyph preceding glyph string S that
23478 is overwriting S because of its right overhang. Value is -1 if no
23479 glyph in front of S overwrites S. */
23480
23481 static int
23482 left_overwriting (struct glyph_string *s)
23483 {
23484 int i, k, x;
23485 struct glyph *glyphs = s->row->glyphs[s->area];
23486 int first = s->first_glyph - glyphs;
23487
23488 k = -1;
23489 x = 0;
23490 for (i = first - 1; i >= 0; --i)
23491 {
23492 int left, right;
23493 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23494 if (x + right > 0)
23495 k = i;
23496 x -= glyphs[i].pixel_width;
23497 }
23498
23499 return k;
23500 }
23501
23502
23503 /* Return the index of the last glyph following glyph string S that is
23504 overwritten by S because of S's right overhang. Value is -1 if
23505 no such glyph is found. */
23506
23507 static int
23508 right_overwritten (struct glyph_string *s)
23509 {
23510 int k = -1;
23511
23512 if (s->right_overhang)
23513 {
23514 int x = 0, i;
23515 struct glyph *glyphs = s->row->glyphs[s->area];
23516 int first = (s->first_glyph - glyphs
23517 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23518 int end = s->row->used[s->area];
23519
23520 for (i = first; i < end && s->right_overhang > x; ++i)
23521 x += glyphs[i].pixel_width;
23522
23523 k = i;
23524 }
23525
23526 return k;
23527 }
23528
23529
23530 /* Return the index of the last glyph following glyph string S that
23531 overwrites S because of its left overhang. Value is negative
23532 if no such glyph is found. */
23533
23534 static int
23535 right_overwriting (struct glyph_string *s)
23536 {
23537 int i, k, x;
23538 int end = s->row->used[s->area];
23539 struct glyph *glyphs = s->row->glyphs[s->area];
23540 int first = (s->first_glyph - glyphs
23541 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23542
23543 k = -1;
23544 x = 0;
23545 for (i = first; i < end; ++i)
23546 {
23547 int left, right;
23548 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23549 if (x - left < 0)
23550 k = i;
23551 x += glyphs[i].pixel_width;
23552 }
23553
23554 return k;
23555 }
23556
23557
23558 /* Set background width of glyph string S. START is the index of the
23559 first glyph following S. LAST_X is the right-most x-position + 1
23560 in the drawing area. */
23561
23562 static void
23563 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23564 {
23565 /* If the face of this glyph string has to be drawn to the end of
23566 the drawing area, set S->extends_to_end_of_line_p. */
23567
23568 if (start == s->row->used[s->area]
23569 && s->area == TEXT_AREA
23570 && ((s->row->fill_line_p
23571 && (s->hl == DRAW_NORMAL_TEXT
23572 || s->hl == DRAW_IMAGE_RAISED
23573 || s->hl == DRAW_IMAGE_SUNKEN))
23574 || s->hl == DRAW_MOUSE_FACE))
23575 s->extends_to_end_of_line_p = 1;
23576
23577 /* If S extends its face to the end of the line, set its
23578 background_width to the distance to the right edge of the drawing
23579 area. */
23580 if (s->extends_to_end_of_line_p)
23581 s->background_width = last_x - s->x + 1;
23582 else
23583 s->background_width = s->width;
23584 }
23585
23586
23587 /* Compute overhangs and x-positions for glyph string S and its
23588 predecessors, or successors. X is the starting x-position for S.
23589 BACKWARD_P non-zero means process predecessors. */
23590
23591 static void
23592 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23593 {
23594 if (backward_p)
23595 {
23596 while (s)
23597 {
23598 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23599 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23600 x -= s->width;
23601 s->x = x;
23602 s = s->prev;
23603 }
23604 }
23605 else
23606 {
23607 while (s)
23608 {
23609 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23610 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23611 s->x = x;
23612 x += s->width;
23613 s = s->next;
23614 }
23615 }
23616 }
23617
23618
23619
23620 /* The following macros are only called from draw_glyphs below.
23621 They reference the following parameters of that function directly:
23622 `w', `row', `area', and `overlap_p'
23623 as well as the following local variables:
23624 `s', `f', and `hdc' (in W32) */
23625
23626 #ifdef HAVE_NTGUI
23627 /* On W32, silently add local `hdc' variable to argument list of
23628 init_glyph_string. */
23629 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23630 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23631 #else
23632 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23633 init_glyph_string (s, char2b, w, row, area, start, hl)
23634 #endif
23635
23636 /* Add a glyph string for a stretch glyph to the list of strings
23637 between HEAD and TAIL. START is the index of the stretch glyph in
23638 row area AREA of glyph row ROW. END is the index of the last glyph
23639 in that glyph row area. X is the current output position assigned
23640 to the new glyph string constructed. HL overrides that face of the
23641 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23642 is the right-most x-position of the drawing area. */
23643
23644 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23645 and below -- keep them on one line. */
23646 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23647 do \
23648 { \
23649 s = alloca (sizeof *s); \
23650 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23651 START = fill_stretch_glyph_string (s, START, END); \
23652 append_glyph_string (&HEAD, &TAIL, s); \
23653 s->x = (X); \
23654 } \
23655 while (0)
23656
23657
23658 /* Add a glyph string for an image glyph to the list of strings
23659 between HEAD and TAIL. START is the index of the image glyph in
23660 row area AREA of glyph row ROW. END is the index of the last glyph
23661 in that glyph row area. X is the current output position assigned
23662 to the new glyph string constructed. HL overrides that face of the
23663 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23664 is the right-most x-position of the drawing area. */
23665
23666 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23667 do \
23668 { \
23669 s = alloca (sizeof *s); \
23670 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23671 fill_image_glyph_string (s); \
23672 append_glyph_string (&HEAD, &TAIL, s); \
23673 ++START; \
23674 s->x = (X); \
23675 } \
23676 while (0)
23677
23678 #ifdef HAVE_XWIDGETS
23679 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23680 do \
23681 { \
23682 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
23683 s = (struct glyph_string *) alloca (sizeof *s); \
23684 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23685 fill_xwidget_glyph_string (s); \
23686 append_glyph_string (&HEAD, &TAIL, s); \
23687 ++START; \
23688 s->x = (X); \
23689 } \
23690 while (0)
23691 #endif
23692
23693
23694 /* Add a glyph string for a sequence of character glyphs to the list
23695 of strings between HEAD and TAIL. START is the index of the first
23696 glyph in row area AREA of glyph row ROW that is part of the new
23697 glyph string. END is the index of the last glyph in that glyph row
23698 area. X is the current output position assigned to the new glyph
23699 string constructed. HL overrides that face of the glyph; e.g. it
23700 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23701 right-most x-position of the drawing area. */
23702
23703 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23704 do \
23705 { \
23706 int face_id; \
23707 XChar2b *char2b; \
23708 \
23709 face_id = (row)->glyphs[area][START].face_id; \
23710 \
23711 s = alloca (sizeof *s); \
23712 char2b = alloca ((END - START) * sizeof *char2b); \
23713 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23714 append_glyph_string (&HEAD, &TAIL, s); \
23715 s->x = (X); \
23716 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23717 } \
23718 while (0)
23719
23720
23721 /* Add a glyph string for a composite sequence to the list of strings
23722 between HEAD and TAIL. START is the index of the first glyph in
23723 row area AREA of glyph row ROW that is part of the new glyph
23724 string. END is the index of the last glyph in that glyph row area.
23725 X is the current output position assigned to the new glyph string
23726 constructed. HL overrides that face of the glyph; e.g. it is
23727 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23728 x-position of the drawing area. */
23729
23730 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23731 do { \
23732 int face_id = (row)->glyphs[area][START].face_id; \
23733 struct face *base_face = FACE_FROM_ID (f, face_id); \
23734 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23735 struct composition *cmp = composition_table[cmp_id]; \
23736 XChar2b *char2b; \
23737 struct glyph_string *first_s = NULL; \
23738 int n; \
23739 \
23740 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23741 \
23742 /* Make glyph_strings for each glyph sequence that is drawable by \
23743 the same face, and append them to HEAD/TAIL. */ \
23744 for (n = 0; n < cmp->glyph_len;) \
23745 { \
23746 s = alloca (sizeof *s); \
23747 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23748 append_glyph_string (&(HEAD), &(TAIL), s); \
23749 s->cmp = cmp; \
23750 s->cmp_from = n; \
23751 s->x = (X); \
23752 if (n == 0) \
23753 first_s = s; \
23754 n = fill_composite_glyph_string (s, base_face, overlaps); \
23755 } \
23756 \
23757 ++START; \
23758 s = first_s; \
23759 } while (0)
23760
23761
23762 /* Add a glyph string for a glyph-string sequence to the list of strings
23763 between HEAD and TAIL. */
23764
23765 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23766 do { \
23767 int face_id; \
23768 XChar2b *char2b; \
23769 Lisp_Object gstring; \
23770 \
23771 face_id = (row)->glyphs[area][START].face_id; \
23772 gstring = (composition_gstring_from_id \
23773 ((row)->glyphs[area][START].u.cmp.id)); \
23774 s = alloca (sizeof *s); \
23775 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23776 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23777 append_glyph_string (&(HEAD), &(TAIL), s); \
23778 s->x = (X); \
23779 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23780 } while (0)
23781
23782
23783 /* Add a glyph string for a sequence of glyphless character's glyphs
23784 to the list of strings between HEAD and TAIL. The meanings of
23785 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23786
23787 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23788 do \
23789 { \
23790 int face_id; \
23791 \
23792 face_id = (row)->glyphs[area][START].face_id; \
23793 \
23794 s = alloca (sizeof *s); \
23795 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23796 append_glyph_string (&HEAD, &TAIL, s); \
23797 s->x = (X); \
23798 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23799 overlaps); \
23800 } \
23801 while (0)
23802
23803
23804 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23805 of AREA of glyph row ROW on window W between indices START and END.
23806 HL overrides the face for drawing glyph strings, e.g. it is
23807 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23808 x-positions of the drawing area.
23809
23810 This is an ugly monster macro construct because we must use alloca
23811 to allocate glyph strings (because draw_glyphs can be called
23812 asynchronously). */
23813
23814 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23815 do \
23816 { \
23817 HEAD = TAIL = NULL; \
23818 while (START < END) \
23819 { \
23820 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23821 switch (first_glyph->type) \
23822 { \
23823 case CHAR_GLYPH: \
23824 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23825 HL, X, LAST_X); \
23826 break; \
23827 \
23828 case COMPOSITE_GLYPH: \
23829 if (first_glyph->u.cmp.automatic) \
23830 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23831 HL, X, LAST_X); \
23832 else \
23833 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23834 HL, X, LAST_X); \
23835 break; \
23836 \
23837 case STRETCH_GLYPH: \
23838 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23839 HL, X, LAST_X); \
23840 break; \
23841 \
23842 case IMAGE_GLYPH: \
23843 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23844 HL, X, LAST_X); \
23845 break;
23846
23847 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23848 case XWIDGET_GLYPH: \
23849 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
23850 HL, X, LAST_X); \
23851 break;
23852
23853 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
23854 case GLYPHLESS_GLYPH: \
23855 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23856 HL, X, LAST_X); \
23857 break; \
23858 \
23859 default: \
23860 emacs_abort (); \
23861 } \
23862 \
23863 if (s) \
23864 { \
23865 set_glyph_string_background_width (s, START, LAST_X); \
23866 (X) += s->width; \
23867 } \
23868 } \
23869 } while (0)
23870
23871
23872 #ifdef HAVE_XWIDGETS
23873 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23874 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23875 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23876 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23877 #else
23878 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23879 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23880 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23881 #endif
23882
23883
23884 /* Draw glyphs between START and END in AREA of ROW on window W,
23885 starting at x-position X. X is relative to AREA in W. HL is a
23886 face-override with the following meaning:
23887
23888 DRAW_NORMAL_TEXT draw normally
23889 DRAW_CURSOR draw in cursor face
23890 DRAW_MOUSE_FACE draw in mouse face.
23891 DRAW_INVERSE_VIDEO draw in mode line face
23892 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23893 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23894
23895 If OVERLAPS is non-zero, draw only the foreground of characters and
23896 clip to the physical height of ROW. Non-zero value also defines
23897 the overlapping part to be drawn:
23898
23899 OVERLAPS_PRED overlap with preceding rows
23900 OVERLAPS_SUCC overlap with succeeding rows
23901 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23902 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23903
23904 Value is the x-position reached, relative to AREA of W. */
23905
23906 static int
23907 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23908 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23909 enum draw_glyphs_face hl, int overlaps)
23910 {
23911 struct glyph_string *head, *tail;
23912 struct glyph_string *s;
23913 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23914 int i, j, x_reached, last_x, area_left = 0;
23915 struct frame *f = XFRAME (WINDOW_FRAME (w));
23916 DECLARE_HDC (hdc);
23917
23918 ALLOCATE_HDC (hdc, f);
23919
23920 /* Let's rather be paranoid than getting a SEGV. */
23921 end = min (end, row->used[area]);
23922 start = clip_to_bounds (0, start, end);
23923
23924 /* Translate X to frame coordinates. Set last_x to the right
23925 end of the drawing area. */
23926 if (row->full_width_p)
23927 {
23928 /* X is relative to the left edge of W, without scroll bars
23929 or fringes. */
23930 area_left = WINDOW_LEFT_EDGE_X (w);
23931 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23932 }
23933 else
23934 {
23935 area_left = window_box_left (w, area);
23936 last_x = area_left + window_box_width (w, area);
23937 }
23938 x += area_left;
23939
23940 /* Build a doubly-linked list of glyph_string structures between
23941 head and tail from what we have to draw. Note that the macro
23942 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23943 the reason we use a separate variable `i'. */
23944 i = start;
23945 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23946 if (tail)
23947 x_reached = tail->x + tail->background_width;
23948 else
23949 x_reached = x;
23950
23951 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23952 the row, redraw some glyphs in front or following the glyph
23953 strings built above. */
23954 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23955 {
23956 struct glyph_string *h, *t;
23957 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23958 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23959 int check_mouse_face = 0;
23960 int dummy_x = 0;
23961
23962 /* If mouse highlighting is on, we may need to draw adjacent
23963 glyphs using mouse-face highlighting. */
23964 if (area == TEXT_AREA && row->mouse_face_p
23965 && hlinfo->mouse_face_beg_row >= 0
23966 && hlinfo->mouse_face_end_row >= 0)
23967 {
23968 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23969
23970 if (row_vpos >= hlinfo->mouse_face_beg_row
23971 && row_vpos <= hlinfo->mouse_face_end_row)
23972 {
23973 check_mouse_face = 1;
23974 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23975 ? hlinfo->mouse_face_beg_col : 0;
23976 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23977 ? hlinfo->mouse_face_end_col
23978 : row->used[TEXT_AREA];
23979 }
23980 }
23981
23982 /* Compute overhangs for all glyph strings. */
23983 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23984 for (s = head; s; s = s->next)
23985 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23986
23987 /* Prepend glyph strings for glyphs in front of the first glyph
23988 string that are overwritten because of the first glyph
23989 string's left overhang. The background of all strings
23990 prepended must be drawn because the first glyph string
23991 draws over it. */
23992 i = left_overwritten (head);
23993 if (i >= 0)
23994 {
23995 enum draw_glyphs_face overlap_hl;
23996
23997 /* If this row contains mouse highlighting, attempt to draw
23998 the overlapped glyphs with the correct highlight. This
23999 code fails if the overlap encompasses more than one glyph
24000 and mouse-highlight spans only some of these glyphs.
24001 However, making it work perfectly involves a lot more
24002 code, and I don't know if the pathological case occurs in
24003 practice, so we'll stick to this for now. --- cyd */
24004 if (check_mouse_face
24005 && mouse_beg_col < start && mouse_end_col > i)
24006 overlap_hl = DRAW_MOUSE_FACE;
24007 else
24008 overlap_hl = DRAW_NORMAL_TEXT;
24009
24010 j = i;
24011 BUILD_GLYPH_STRINGS (j, start, h, t,
24012 overlap_hl, dummy_x, last_x);
24013 start = i;
24014 compute_overhangs_and_x (t, head->x, 1);
24015 prepend_glyph_string_lists (&head, &tail, h, t);
24016 clip_head = head;
24017 }
24018
24019 /* Prepend glyph strings for glyphs in front of the first glyph
24020 string that overwrite that glyph string because of their
24021 right overhang. For these strings, only the foreground must
24022 be drawn, because it draws over the glyph string at `head'.
24023 The background must not be drawn because this would overwrite
24024 right overhangs of preceding glyphs for which no glyph
24025 strings exist. */
24026 i = left_overwriting (head);
24027 if (i >= 0)
24028 {
24029 enum draw_glyphs_face overlap_hl;
24030
24031 if (check_mouse_face
24032 && mouse_beg_col < start && mouse_end_col > i)
24033 overlap_hl = DRAW_MOUSE_FACE;
24034 else
24035 overlap_hl = DRAW_NORMAL_TEXT;
24036
24037 clip_head = head;
24038 BUILD_GLYPH_STRINGS (i, start, h, t,
24039 overlap_hl, dummy_x, last_x);
24040 for (s = h; s; s = s->next)
24041 s->background_filled_p = 1;
24042 compute_overhangs_and_x (t, head->x, 1);
24043 prepend_glyph_string_lists (&head, &tail, h, t);
24044 }
24045
24046 /* Append glyphs strings for glyphs following the last glyph
24047 string tail that are overwritten by tail. The background of
24048 these strings has to be drawn because tail's foreground draws
24049 over it. */
24050 i = right_overwritten (tail);
24051 if (i >= 0)
24052 {
24053 enum draw_glyphs_face overlap_hl;
24054
24055 if (check_mouse_face
24056 && mouse_beg_col < i && mouse_end_col > end)
24057 overlap_hl = DRAW_MOUSE_FACE;
24058 else
24059 overlap_hl = DRAW_NORMAL_TEXT;
24060
24061 BUILD_GLYPH_STRINGS (end, i, h, t,
24062 overlap_hl, x, last_x);
24063 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24064 we don't have `end = i;' here. */
24065 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24066 append_glyph_string_lists (&head, &tail, h, t);
24067 clip_tail = tail;
24068 }
24069
24070 /* Append glyph strings for glyphs following the last glyph
24071 string tail that overwrite tail. The foreground of such
24072 glyphs has to be drawn because it writes into the background
24073 of tail. The background must not be drawn because it could
24074 paint over the foreground of following glyphs. */
24075 i = right_overwriting (tail);
24076 if (i >= 0)
24077 {
24078 enum draw_glyphs_face overlap_hl;
24079 if (check_mouse_face
24080 && mouse_beg_col < i && mouse_end_col > end)
24081 overlap_hl = DRAW_MOUSE_FACE;
24082 else
24083 overlap_hl = DRAW_NORMAL_TEXT;
24084
24085 clip_tail = tail;
24086 i++; /* We must include the Ith glyph. */
24087 BUILD_GLYPH_STRINGS (end, i, h, t,
24088 overlap_hl, x, last_x);
24089 for (s = h; s; s = s->next)
24090 s->background_filled_p = 1;
24091 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24092 append_glyph_string_lists (&head, &tail, h, t);
24093 }
24094 if (clip_head || clip_tail)
24095 for (s = head; s; s = s->next)
24096 {
24097 s->clip_head = clip_head;
24098 s->clip_tail = clip_tail;
24099 }
24100 }
24101
24102 /* Draw all strings. */
24103 for (s = head; s; s = s->next)
24104 FRAME_RIF (f)->draw_glyph_string (s);
24105
24106 #ifndef HAVE_NS
24107 /* When focus a sole frame and move horizontally, this sets on_p to 0
24108 causing a failure to erase prev cursor position. */
24109 if (area == TEXT_AREA
24110 && !row->full_width_p
24111 /* When drawing overlapping rows, only the glyph strings'
24112 foreground is drawn, which doesn't erase a cursor
24113 completely. */
24114 && !overlaps)
24115 {
24116 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24117 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24118 : (tail ? tail->x + tail->background_width : x));
24119 x0 -= area_left;
24120 x1 -= area_left;
24121
24122 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24123 row->y, MATRIX_ROW_BOTTOM_Y (row));
24124 }
24125 #endif
24126
24127 /* Value is the x-position up to which drawn, relative to AREA of W.
24128 This doesn't include parts drawn because of overhangs. */
24129 if (row->full_width_p)
24130 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24131 else
24132 x_reached -= area_left;
24133
24134 RELEASE_HDC (hdc, f);
24135
24136 return x_reached;
24137 }
24138
24139 /* Expand row matrix if too narrow. Don't expand if area
24140 is not present. */
24141
24142 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24143 { \
24144 if (!fonts_changed_p \
24145 && (it->glyph_row->glyphs[area] \
24146 < it->glyph_row->glyphs[area + 1])) \
24147 { \
24148 it->w->ncols_scale_factor++; \
24149 fonts_changed_p = 1; \
24150 } \
24151 }
24152
24153 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24154 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24155
24156 static void
24157 append_glyph (struct it *it)
24158 {
24159 struct glyph *glyph;
24160 enum glyph_row_area area = it->area;
24161
24162 eassert (it->glyph_row);
24163 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24164
24165 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24166 if (glyph < it->glyph_row->glyphs[area + 1])
24167 {
24168 /* If the glyph row is reversed, we need to prepend the glyph
24169 rather than append it. */
24170 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24171 {
24172 struct glyph *g;
24173
24174 /* Make room for the additional glyph. */
24175 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24176 g[1] = *g;
24177 glyph = it->glyph_row->glyphs[area];
24178 }
24179 glyph->charpos = CHARPOS (it->position);
24180 glyph->object = it->object;
24181 if (it->pixel_width > 0)
24182 {
24183 glyph->pixel_width = it->pixel_width;
24184 glyph->padding_p = 0;
24185 }
24186 else
24187 {
24188 /* Assure at least 1-pixel width. Otherwise, cursor can't
24189 be displayed correctly. */
24190 glyph->pixel_width = 1;
24191 glyph->padding_p = 1;
24192 }
24193 glyph->ascent = it->ascent;
24194 glyph->descent = it->descent;
24195 glyph->voffset = it->voffset;
24196 glyph->type = CHAR_GLYPH;
24197 glyph->avoid_cursor_p = it->avoid_cursor_p;
24198 glyph->multibyte_p = it->multibyte_p;
24199 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24200 {
24201 /* In R2L rows, the left and the right box edges need to be
24202 drawn in reverse direction. */
24203 glyph->right_box_line_p = it->start_of_box_run_p;
24204 glyph->left_box_line_p = it->end_of_box_run_p;
24205 }
24206 else
24207 {
24208 glyph->left_box_line_p = it->start_of_box_run_p;
24209 glyph->right_box_line_p = it->end_of_box_run_p;
24210 }
24211 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24212 || it->phys_descent > it->descent);
24213 glyph->glyph_not_available_p = it->glyph_not_available_p;
24214 glyph->face_id = it->face_id;
24215 glyph->u.ch = it->char_to_display;
24216 glyph->slice.img = null_glyph_slice;
24217 glyph->font_type = FONT_TYPE_UNKNOWN;
24218 if (it->bidi_p)
24219 {
24220 glyph->resolved_level = it->bidi_it.resolved_level;
24221 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24222 emacs_abort ();
24223 glyph->bidi_type = it->bidi_it.type;
24224 }
24225 else
24226 {
24227 glyph->resolved_level = 0;
24228 glyph->bidi_type = UNKNOWN_BT;
24229 }
24230 ++it->glyph_row->used[area];
24231 }
24232 else
24233 IT_EXPAND_MATRIX_WIDTH (it, area);
24234 }
24235
24236 /* Store one glyph for the composition IT->cmp_it.id in
24237 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24238 non-null. */
24239
24240 static void
24241 append_composite_glyph (struct it *it)
24242 {
24243 struct glyph *glyph;
24244 enum glyph_row_area area = it->area;
24245
24246 eassert (it->glyph_row);
24247
24248 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24249 if (glyph < it->glyph_row->glyphs[area + 1])
24250 {
24251 /* If the glyph row is reversed, we need to prepend the glyph
24252 rather than append it. */
24253 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24254 {
24255 struct glyph *g;
24256
24257 /* Make room for the new glyph. */
24258 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24259 g[1] = *g;
24260 glyph = it->glyph_row->glyphs[it->area];
24261 }
24262 glyph->charpos = it->cmp_it.charpos;
24263 glyph->object = it->object;
24264 glyph->pixel_width = it->pixel_width;
24265 glyph->ascent = it->ascent;
24266 glyph->descent = it->descent;
24267 glyph->voffset = it->voffset;
24268 glyph->type = COMPOSITE_GLYPH;
24269 if (it->cmp_it.ch < 0)
24270 {
24271 glyph->u.cmp.automatic = 0;
24272 glyph->u.cmp.id = it->cmp_it.id;
24273 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24274 }
24275 else
24276 {
24277 glyph->u.cmp.automatic = 1;
24278 glyph->u.cmp.id = it->cmp_it.id;
24279 glyph->slice.cmp.from = it->cmp_it.from;
24280 glyph->slice.cmp.to = it->cmp_it.to - 1;
24281 }
24282 glyph->avoid_cursor_p = it->avoid_cursor_p;
24283 glyph->multibyte_p = it->multibyte_p;
24284 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24285 {
24286 /* In R2L rows, the left and the right box edges need to be
24287 drawn in reverse direction. */
24288 glyph->right_box_line_p = it->start_of_box_run_p;
24289 glyph->left_box_line_p = it->end_of_box_run_p;
24290 }
24291 else
24292 {
24293 glyph->left_box_line_p = it->start_of_box_run_p;
24294 glyph->right_box_line_p = it->end_of_box_run_p;
24295 }
24296 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24297 || it->phys_descent > it->descent);
24298 glyph->padding_p = 0;
24299 glyph->glyph_not_available_p = 0;
24300 glyph->face_id = it->face_id;
24301 glyph->font_type = FONT_TYPE_UNKNOWN;
24302 if (it->bidi_p)
24303 {
24304 glyph->resolved_level = it->bidi_it.resolved_level;
24305 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24306 emacs_abort ();
24307 glyph->bidi_type = it->bidi_it.type;
24308 }
24309 ++it->glyph_row->used[area];
24310 }
24311 else
24312 IT_EXPAND_MATRIX_WIDTH (it, area);
24313 }
24314
24315
24316 /* Change IT->ascent and IT->height according to the setting of
24317 IT->voffset. */
24318
24319 static void
24320 take_vertical_position_into_account (struct it *it)
24321 {
24322 if (it->voffset)
24323 {
24324 if (it->voffset < 0)
24325 /* Increase the ascent so that we can display the text higher
24326 in the line. */
24327 it->ascent -= it->voffset;
24328 else
24329 /* Increase the descent so that we can display the text lower
24330 in the line. */
24331 it->descent += it->voffset;
24332 }
24333 }
24334
24335
24336 /* Produce glyphs/get display metrics for the image IT is loaded with.
24337 See the description of struct display_iterator in dispextern.h for
24338 an overview of struct display_iterator. */
24339
24340 static void
24341 produce_image_glyph (struct it *it)
24342 {
24343 struct image *img;
24344 struct face *face;
24345 int glyph_ascent, crop;
24346 struct glyph_slice slice;
24347
24348 eassert (it->what == IT_IMAGE);
24349
24350 face = FACE_FROM_ID (it->f, it->face_id);
24351 eassert (face);
24352 /* Make sure X resources of the face is loaded. */
24353 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24354
24355 if (it->image_id < 0)
24356 {
24357 /* Fringe bitmap. */
24358 it->ascent = it->phys_ascent = 0;
24359 it->descent = it->phys_descent = 0;
24360 it->pixel_width = 0;
24361 it->nglyphs = 0;
24362 return;
24363 }
24364
24365 img = IMAGE_FROM_ID (it->f, it->image_id);
24366 eassert (img);
24367 /* Make sure X resources of the image is loaded. */
24368 prepare_image_for_display (it->f, img);
24369
24370 slice.x = slice.y = 0;
24371 slice.width = img->width;
24372 slice.height = img->height;
24373
24374 if (INTEGERP (it->slice.x))
24375 slice.x = XINT (it->slice.x);
24376 else if (FLOATP (it->slice.x))
24377 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24378
24379 if (INTEGERP (it->slice.y))
24380 slice.y = XINT (it->slice.y);
24381 else if (FLOATP (it->slice.y))
24382 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24383
24384 if (INTEGERP (it->slice.width))
24385 slice.width = XINT (it->slice.width);
24386 else if (FLOATP (it->slice.width))
24387 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24388
24389 if (INTEGERP (it->slice.height))
24390 slice.height = XINT (it->slice.height);
24391 else if (FLOATP (it->slice.height))
24392 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24393
24394 if (slice.x >= img->width)
24395 slice.x = img->width;
24396 if (slice.y >= img->height)
24397 slice.y = img->height;
24398 if (slice.x + slice.width >= img->width)
24399 slice.width = img->width - slice.x;
24400 if (slice.y + slice.height > img->height)
24401 slice.height = img->height - slice.y;
24402
24403 if (slice.width == 0 || slice.height == 0)
24404 return;
24405
24406 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24407
24408 it->descent = slice.height - glyph_ascent;
24409 if (slice.y == 0)
24410 it->descent += img->vmargin;
24411 if (slice.y + slice.height == img->height)
24412 it->descent += img->vmargin;
24413 it->phys_descent = it->descent;
24414
24415 it->pixel_width = slice.width;
24416 if (slice.x == 0)
24417 it->pixel_width += img->hmargin;
24418 if (slice.x + slice.width == img->width)
24419 it->pixel_width += img->hmargin;
24420
24421 /* It's quite possible for images to have an ascent greater than
24422 their height, so don't get confused in that case. */
24423 if (it->descent < 0)
24424 it->descent = 0;
24425
24426 it->nglyphs = 1;
24427
24428 if (face->box != FACE_NO_BOX)
24429 {
24430 if (face->box_line_width > 0)
24431 {
24432 if (slice.y == 0)
24433 it->ascent += face->box_line_width;
24434 if (slice.y + slice.height == img->height)
24435 it->descent += face->box_line_width;
24436 }
24437
24438 if (it->start_of_box_run_p && slice.x == 0)
24439 it->pixel_width += eabs (face->box_line_width);
24440 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24441 it->pixel_width += eabs (face->box_line_width);
24442 }
24443
24444 take_vertical_position_into_account (it);
24445
24446 /* Automatically crop wide image glyphs at right edge so we can
24447 draw the cursor on same display row. */
24448 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24449 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24450 {
24451 it->pixel_width -= crop;
24452 slice.width -= crop;
24453 }
24454
24455 if (it->glyph_row)
24456 {
24457 struct glyph *glyph;
24458 enum glyph_row_area area = it->area;
24459
24460 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24461 if (glyph < it->glyph_row->glyphs[area + 1])
24462 {
24463 glyph->charpos = CHARPOS (it->position);
24464 glyph->object = it->object;
24465 glyph->pixel_width = it->pixel_width;
24466 glyph->ascent = glyph_ascent;
24467 glyph->descent = it->descent;
24468 glyph->voffset = it->voffset;
24469 glyph->type = IMAGE_GLYPH;
24470 glyph->avoid_cursor_p = it->avoid_cursor_p;
24471 glyph->multibyte_p = it->multibyte_p;
24472 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24473 {
24474 /* In R2L rows, the left and the right box edges need to be
24475 drawn in reverse direction. */
24476 glyph->right_box_line_p = it->start_of_box_run_p;
24477 glyph->left_box_line_p = it->end_of_box_run_p;
24478 }
24479 else
24480 {
24481 glyph->left_box_line_p = it->start_of_box_run_p;
24482 glyph->right_box_line_p = it->end_of_box_run_p;
24483 }
24484 glyph->overlaps_vertically_p = 0;
24485 glyph->padding_p = 0;
24486 glyph->glyph_not_available_p = 0;
24487 glyph->face_id = it->face_id;
24488 glyph->u.img_id = img->id;
24489 glyph->slice.img = slice;
24490 glyph->font_type = FONT_TYPE_UNKNOWN;
24491 if (it->bidi_p)
24492 {
24493 glyph->resolved_level = it->bidi_it.resolved_level;
24494 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24495 emacs_abort ();
24496 glyph->bidi_type = it->bidi_it.type;
24497 }
24498 ++it->glyph_row->used[area];
24499 }
24500 else
24501 IT_EXPAND_MATRIX_WIDTH (it, area);
24502 }
24503 }
24504
24505 #ifdef HAVE_XWIDGETS
24506 static void
24507 produce_xwidget_glyph (struct it *it)
24508 {
24509 struct xwidget* xw;
24510 struct face *face;
24511 int glyph_ascent, crop;
24512 printf("produce_xwidget_glyph:\n");
24513 eassert (it->what == IT_XWIDGET);
24514
24515 face = FACE_FROM_ID (it->f, it->face_id);
24516 eassert (face);
24517 /* Make sure X resources of the face is loaded. */
24518 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24519
24520 xw = it->xwidget;
24521 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
24522 it->descent = xw->height/2;
24523 it->phys_descent = it->descent;
24524 it->pixel_width = xw->width;
24525 /* It's quite possible for images to have an ascent greater than
24526 their height, so don't get confused in that case. */
24527 if (it->descent < 0)
24528 it->descent = 0;
24529
24530 it->nglyphs = 1;
24531
24532 if (face->box != FACE_NO_BOX)
24533 {
24534 if (face->box_line_width > 0)
24535 {
24536 it->ascent += face->box_line_width;
24537 it->descent += face->box_line_width;
24538 }
24539
24540 if (it->start_of_box_run_p)
24541 it->pixel_width += eabs (face->box_line_width);
24542 it->pixel_width += eabs (face->box_line_width);
24543 }
24544
24545 take_vertical_position_into_account (it);
24546
24547 /* Automatically crop wide image glyphs at right edge so we can
24548 draw the cursor on same display row. */
24549 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24550 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24551 {
24552 it->pixel_width -= crop;
24553 }
24554
24555 if (it->glyph_row)
24556 {
24557 struct glyph *glyph;
24558 enum glyph_row_area area = it->area;
24559
24560 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24561 if (glyph < it->glyph_row->glyphs[area + 1])
24562 {
24563 glyph->charpos = CHARPOS (it->position);
24564 glyph->object = it->object;
24565 glyph->pixel_width = it->pixel_width;
24566 glyph->ascent = glyph_ascent;
24567 glyph->descent = it->descent;
24568 glyph->voffset = it->voffset;
24569 glyph->type = XWIDGET_GLYPH;
24570
24571 glyph->multibyte_p = it->multibyte_p;
24572 glyph->left_box_line_p = it->start_of_box_run_p;
24573 glyph->right_box_line_p = it->end_of_box_run_p;
24574 glyph->overlaps_vertically_p = 0;
24575 glyph->padding_p = 0;
24576 glyph->glyph_not_available_p = 0;
24577 glyph->face_id = it->face_id;
24578 glyph->u.xwidget = it->xwidget;
24579 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
24580 glyph->font_type = FONT_TYPE_UNKNOWN;
24581 ++it->glyph_row->used[area];
24582 }
24583 else
24584 IT_EXPAND_MATRIX_WIDTH (it, area);
24585 }
24586 }
24587 #endif
24588
24589 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24590 of the glyph, WIDTH and HEIGHT are the width and height of the
24591 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24592
24593 static void
24594 append_stretch_glyph (struct it *it, Lisp_Object object,
24595 int width, int height, int ascent)
24596 {
24597 struct glyph *glyph;
24598 enum glyph_row_area area = it->area;
24599
24600 eassert (ascent >= 0 && ascent <= height);
24601
24602 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24603 if (glyph < it->glyph_row->glyphs[area + 1])
24604 {
24605 /* If the glyph row is reversed, we need to prepend the glyph
24606 rather than append it. */
24607 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24608 {
24609 struct glyph *g;
24610
24611 /* Make room for the additional glyph. */
24612 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24613 g[1] = *g;
24614 glyph = it->glyph_row->glyphs[area];
24615 }
24616 glyph->charpos = CHARPOS (it->position);
24617 glyph->object = object;
24618 glyph->pixel_width = width;
24619 glyph->ascent = ascent;
24620 glyph->descent = height - ascent;
24621 glyph->voffset = it->voffset;
24622 glyph->type = STRETCH_GLYPH;
24623 glyph->avoid_cursor_p = it->avoid_cursor_p;
24624 glyph->multibyte_p = it->multibyte_p;
24625 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24626 {
24627 /* In R2L rows, the left and the right box edges need to be
24628 drawn in reverse direction. */
24629 glyph->right_box_line_p = it->start_of_box_run_p;
24630 glyph->left_box_line_p = it->end_of_box_run_p;
24631 }
24632 else
24633 {
24634 glyph->left_box_line_p = it->start_of_box_run_p;
24635 glyph->right_box_line_p = it->end_of_box_run_p;
24636 }
24637 glyph->overlaps_vertically_p = 0;
24638 glyph->padding_p = 0;
24639 glyph->glyph_not_available_p = 0;
24640 glyph->face_id = it->face_id;
24641 glyph->u.stretch.ascent = ascent;
24642 glyph->u.stretch.height = height;
24643 glyph->slice.img = null_glyph_slice;
24644 glyph->font_type = FONT_TYPE_UNKNOWN;
24645 if (it->bidi_p)
24646 {
24647 glyph->resolved_level = it->bidi_it.resolved_level;
24648 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24649 emacs_abort ();
24650 glyph->bidi_type = it->bidi_it.type;
24651 }
24652 else
24653 {
24654 glyph->resolved_level = 0;
24655 glyph->bidi_type = UNKNOWN_BT;
24656 }
24657 ++it->glyph_row->used[area];
24658 }
24659 else
24660 IT_EXPAND_MATRIX_WIDTH (it, area);
24661 }
24662
24663 #endif /* HAVE_WINDOW_SYSTEM */
24664
24665 /* Produce a stretch glyph for iterator IT. IT->object is the value
24666 of the glyph property displayed. The value must be a list
24667 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24668 being recognized:
24669
24670 1. `:width WIDTH' specifies that the space should be WIDTH *
24671 canonical char width wide. WIDTH may be an integer or floating
24672 point number.
24673
24674 2. `:relative-width FACTOR' specifies that the width of the stretch
24675 should be computed from the width of the first character having the
24676 `glyph' property, and should be FACTOR times that width.
24677
24678 3. `:align-to HPOS' specifies that the space should be wide enough
24679 to reach HPOS, a value in canonical character units.
24680
24681 Exactly one of the above pairs must be present.
24682
24683 4. `:height HEIGHT' specifies that the height of the stretch produced
24684 should be HEIGHT, measured in canonical character units.
24685
24686 5. `:relative-height FACTOR' specifies that the height of the
24687 stretch should be FACTOR times the height of the characters having
24688 the glyph property.
24689
24690 Either none or exactly one of 4 or 5 must be present.
24691
24692 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24693 of the stretch should be used for the ascent of the stretch.
24694 ASCENT must be in the range 0 <= ASCENT <= 100. */
24695
24696 void
24697 produce_stretch_glyph (struct it *it)
24698 {
24699 /* (space :width WIDTH :height HEIGHT ...) */
24700 Lisp_Object prop, plist;
24701 int width = 0, height = 0, align_to = -1;
24702 int zero_width_ok_p = 0;
24703 double tem;
24704 struct font *font = NULL;
24705
24706 #ifdef HAVE_WINDOW_SYSTEM
24707 int ascent = 0;
24708 int zero_height_ok_p = 0;
24709
24710 if (FRAME_WINDOW_P (it->f))
24711 {
24712 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24713 font = face->font ? face->font : FRAME_FONT (it->f);
24714 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24715 }
24716 #endif
24717
24718 /* List should start with `space'. */
24719 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24720 plist = XCDR (it->object);
24721
24722 /* Compute the width of the stretch. */
24723 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24724 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24725 {
24726 /* Absolute width `:width WIDTH' specified and valid. */
24727 zero_width_ok_p = 1;
24728 width = (int)tem;
24729 }
24730 #ifdef HAVE_WINDOW_SYSTEM
24731 else if (FRAME_WINDOW_P (it->f)
24732 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24733 {
24734 /* Relative width `:relative-width FACTOR' specified and valid.
24735 Compute the width of the characters having the `glyph'
24736 property. */
24737 struct it it2;
24738 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24739
24740 it2 = *it;
24741 if (it->multibyte_p)
24742 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24743 else
24744 {
24745 it2.c = it2.char_to_display = *p, it2.len = 1;
24746 if (! ASCII_CHAR_P (it2.c))
24747 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24748 }
24749
24750 it2.glyph_row = NULL;
24751 it2.what = IT_CHARACTER;
24752 x_produce_glyphs (&it2);
24753 width = NUMVAL (prop) * it2.pixel_width;
24754 }
24755 #endif /* HAVE_WINDOW_SYSTEM */
24756 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24757 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24758 {
24759 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24760 align_to = (align_to < 0
24761 ? 0
24762 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24763 else if (align_to < 0)
24764 align_to = window_box_left_offset (it->w, TEXT_AREA);
24765 width = max (0, (int)tem + align_to - it->current_x);
24766 zero_width_ok_p = 1;
24767 }
24768 else
24769 /* Nothing specified -> width defaults to canonical char width. */
24770 width = FRAME_COLUMN_WIDTH (it->f);
24771
24772 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24773 width = 1;
24774
24775 #ifdef HAVE_WINDOW_SYSTEM
24776 /* Compute height. */
24777 if (FRAME_WINDOW_P (it->f))
24778 {
24779 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24780 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24781 {
24782 height = (int)tem;
24783 zero_height_ok_p = 1;
24784 }
24785 else if (prop = Fplist_get (plist, QCrelative_height),
24786 NUMVAL (prop) > 0)
24787 height = FONT_HEIGHT (font) * NUMVAL (prop);
24788 else
24789 height = FONT_HEIGHT (font);
24790
24791 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24792 height = 1;
24793
24794 /* Compute percentage of height used for ascent. If
24795 `:ascent ASCENT' is present and valid, use that. Otherwise,
24796 derive the ascent from the font in use. */
24797 if (prop = Fplist_get (plist, QCascent),
24798 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24799 ascent = height * NUMVAL (prop) / 100.0;
24800 else if (!NILP (prop)
24801 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24802 ascent = min (max (0, (int)tem), height);
24803 else
24804 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24805 }
24806 else
24807 #endif /* HAVE_WINDOW_SYSTEM */
24808 height = 1;
24809
24810 if (width > 0 && it->line_wrap != TRUNCATE
24811 && it->current_x + width > it->last_visible_x)
24812 {
24813 width = it->last_visible_x - it->current_x;
24814 #ifdef HAVE_WINDOW_SYSTEM
24815 /* Subtract one more pixel from the stretch width, but only on
24816 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24817 width -= FRAME_WINDOW_P (it->f);
24818 #endif
24819 }
24820
24821 if (width > 0 && height > 0 && it->glyph_row)
24822 {
24823 Lisp_Object o_object = it->object;
24824 Lisp_Object object = it->stack[it->sp - 1].string;
24825 int n = width;
24826
24827 if (!STRINGP (object))
24828 object = it->w->contents;
24829 #ifdef HAVE_WINDOW_SYSTEM
24830 if (FRAME_WINDOW_P (it->f))
24831 append_stretch_glyph (it, object, width, height, ascent);
24832 else
24833 #endif
24834 {
24835 it->object = object;
24836 it->char_to_display = ' ';
24837 it->pixel_width = it->len = 1;
24838 while (n--)
24839 tty_append_glyph (it);
24840 it->object = o_object;
24841 }
24842 }
24843
24844 it->pixel_width = width;
24845 #ifdef HAVE_WINDOW_SYSTEM
24846 if (FRAME_WINDOW_P (it->f))
24847 {
24848 it->ascent = it->phys_ascent = ascent;
24849 it->descent = it->phys_descent = height - it->ascent;
24850 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24851 take_vertical_position_into_account (it);
24852 }
24853 else
24854 #endif
24855 it->nglyphs = width;
24856 }
24857
24858 /* Get information about special display element WHAT in an
24859 environment described by IT. WHAT is one of IT_TRUNCATION or
24860 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24861 non-null glyph_row member. This function ensures that fields like
24862 face_id, c, len of IT are left untouched. */
24863
24864 static void
24865 produce_special_glyphs (struct it *it, enum display_element_type what)
24866 {
24867 struct it temp_it;
24868 Lisp_Object gc;
24869 GLYPH glyph;
24870
24871 temp_it = *it;
24872 temp_it.object = make_number (0);
24873 memset (&temp_it.current, 0, sizeof temp_it.current);
24874
24875 if (what == IT_CONTINUATION)
24876 {
24877 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24878 if (it->bidi_it.paragraph_dir == R2L)
24879 SET_GLYPH_FROM_CHAR (glyph, '/');
24880 else
24881 SET_GLYPH_FROM_CHAR (glyph, '\\');
24882 if (it->dp
24883 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24884 {
24885 /* FIXME: Should we mirror GC for R2L lines? */
24886 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24887 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24888 }
24889 }
24890 else if (what == IT_TRUNCATION)
24891 {
24892 /* Truncation glyph. */
24893 SET_GLYPH_FROM_CHAR (glyph, '$');
24894 if (it->dp
24895 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24896 {
24897 /* FIXME: Should we mirror GC for R2L lines? */
24898 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24899 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24900 }
24901 }
24902 else
24903 emacs_abort ();
24904
24905 #ifdef HAVE_WINDOW_SYSTEM
24906 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24907 is turned off, we precede the truncation/continuation glyphs by a
24908 stretch glyph whose width is computed such that these special
24909 glyphs are aligned at the window margin, even when very different
24910 fonts are used in different glyph rows. */
24911 if (FRAME_WINDOW_P (temp_it.f)
24912 /* init_iterator calls this with it->glyph_row == NULL, and it
24913 wants only the pixel width of the truncation/continuation
24914 glyphs. */
24915 && temp_it.glyph_row
24916 /* insert_left_trunc_glyphs calls us at the beginning of the
24917 row, and it has its own calculation of the stretch glyph
24918 width. */
24919 && temp_it.glyph_row->used[TEXT_AREA] > 0
24920 && (temp_it.glyph_row->reversed_p
24921 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24922 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24923 {
24924 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24925
24926 if (stretch_width > 0)
24927 {
24928 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24929 struct font *font =
24930 face->font ? face->font : FRAME_FONT (temp_it.f);
24931 int stretch_ascent =
24932 (((temp_it.ascent + temp_it.descent)
24933 * FONT_BASE (font)) / FONT_HEIGHT (font));
24934
24935 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24936 temp_it.ascent + temp_it.descent,
24937 stretch_ascent);
24938 }
24939 }
24940 #endif
24941
24942 temp_it.dp = NULL;
24943 temp_it.what = IT_CHARACTER;
24944 temp_it.len = 1;
24945 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24946 temp_it.face_id = GLYPH_FACE (glyph);
24947 temp_it.len = CHAR_BYTES (temp_it.c);
24948
24949 PRODUCE_GLYPHS (&temp_it);
24950 it->pixel_width = temp_it.pixel_width;
24951 it->nglyphs = temp_it.pixel_width;
24952 }
24953
24954 #ifdef HAVE_WINDOW_SYSTEM
24955
24956 /* Calculate line-height and line-spacing properties.
24957 An integer value specifies explicit pixel value.
24958 A float value specifies relative value to current face height.
24959 A cons (float . face-name) specifies relative value to
24960 height of specified face font.
24961
24962 Returns height in pixels, or nil. */
24963
24964
24965 static Lisp_Object
24966 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24967 int boff, int override)
24968 {
24969 Lisp_Object face_name = Qnil;
24970 int ascent, descent, height;
24971
24972 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24973 return val;
24974
24975 if (CONSP (val))
24976 {
24977 face_name = XCAR (val);
24978 val = XCDR (val);
24979 if (!NUMBERP (val))
24980 val = make_number (1);
24981 if (NILP (face_name))
24982 {
24983 height = it->ascent + it->descent;
24984 goto scale;
24985 }
24986 }
24987
24988 if (NILP (face_name))
24989 {
24990 font = FRAME_FONT (it->f);
24991 boff = FRAME_BASELINE_OFFSET (it->f);
24992 }
24993 else if (EQ (face_name, Qt))
24994 {
24995 override = 0;
24996 }
24997 else
24998 {
24999 int face_id;
25000 struct face *face;
25001
25002 face_id = lookup_named_face (it->f, face_name, 0);
25003 if (face_id < 0)
25004 return make_number (-1);
25005
25006 face = FACE_FROM_ID (it->f, face_id);
25007 font = face->font;
25008 if (font == NULL)
25009 return make_number (-1);
25010 boff = font->baseline_offset;
25011 if (font->vertical_centering)
25012 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25013 }
25014
25015 ascent = FONT_BASE (font) + boff;
25016 descent = FONT_DESCENT (font) - boff;
25017
25018 if (override)
25019 {
25020 it->override_ascent = ascent;
25021 it->override_descent = descent;
25022 it->override_boff = boff;
25023 }
25024
25025 height = ascent + descent;
25026
25027 scale:
25028 if (FLOATP (val))
25029 height = (int)(XFLOAT_DATA (val) * height);
25030 else if (INTEGERP (val))
25031 height *= XINT (val);
25032
25033 return make_number (height);
25034 }
25035
25036
25037 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25038 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25039 and only if this is for a character for which no font was found.
25040
25041 If the display method (it->glyphless_method) is
25042 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25043 length of the acronym or the hexadecimal string, UPPER_XOFF and
25044 UPPER_YOFF are pixel offsets for the upper part of the string,
25045 LOWER_XOFF and LOWER_YOFF are for the lower part.
25046
25047 For the other display methods, LEN through LOWER_YOFF are zero. */
25048
25049 static void
25050 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25051 short upper_xoff, short upper_yoff,
25052 short lower_xoff, short lower_yoff)
25053 {
25054 struct glyph *glyph;
25055 enum glyph_row_area area = it->area;
25056
25057 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25058 if (glyph < it->glyph_row->glyphs[area + 1])
25059 {
25060 /* If the glyph row is reversed, we need to prepend the glyph
25061 rather than append it. */
25062 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25063 {
25064 struct glyph *g;
25065
25066 /* Make room for the additional glyph. */
25067 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25068 g[1] = *g;
25069 glyph = it->glyph_row->glyphs[area];
25070 }
25071 glyph->charpos = CHARPOS (it->position);
25072 glyph->object = it->object;
25073 glyph->pixel_width = it->pixel_width;
25074 glyph->ascent = it->ascent;
25075 glyph->descent = it->descent;
25076 glyph->voffset = it->voffset;
25077 glyph->type = GLYPHLESS_GLYPH;
25078 glyph->u.glyphless.method = it->glyphless_method;
25079 glyph->u.glyphless.for_no_font = for_no_font;
25080 glyph->u.glyphless.len = len;
25081 glyph->u.glyphless.ch = it->c;
25082 glyph->slice.glyphless.upper_xoff = upper_xoff;
25083 glyph->slice.glyphless.upper_yoff = upper_yoff;
25084 glyph->slice.glyphless.lower_xoff = lower_xoff;
25085 glyph->slice.glyphless.lower_yoff = lower_yoff;
25086 glyph->avoid_cursor_p = it->avoid_cursor_p;
25087 glyph->multibyte_p = it->multibyte_p;
25088 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25089 {
25090 /* In R2L rows, the left and the right box edges need to be
25091 drawn in reverse direction. */
25092 glyph->right_box_line_p = it->start_of_box_run_p;
25093 glyph->left_box_line_p = it->end_of_box_run_p;
25094 }
25095 else
25096 {
25097 glyph->left_box_line_p = it->start_of_box_run_p;
25098 glyph->right_box_line_p = it->end_of_box_run_p;
25099 }
25100 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25101 || it->phys_descent > it->descent);
25102 glyph->padding_p = 0;
25103 glyph->glyph_not_available_p = 0;
25104 glyph->face_id = face_id;
25105 glyph->font_type = FONT_TYPE_UNKNOWN;
25106 if (it->bidi_p)
25107 {
25108 glyph->resolved_level = it->bidi_it.resolved_level;
25109 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25110 emacs_abort ();
25111 glyph->bidi_type = it->bidi_it.type;
25112 }
25113 ++it->glyph_row->used[area];
25114 }
25115 else
25116 IT_EXPAND_MATRIX_WIDTH (it, area);
25117 }
25118
25119
25120 /* Produce a glyph for a glyphless character for iterator IT.
25121 IT->glyphless_method specifies which method to use for displaying
25122 the character. See the description of enum
25123 glyphless_display_method in dispextern.h for the detail.
25124
25125 FOR_NO_FONT is nonzero if and only if this is for a character for
25126 which no font was found. ACRONYM, if non-nil, is an acronym string
25127 for the character. */
25128
25129 static void
25130 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25131 {
25132 int face_id;
25133 struct face *face;
25134 struct font *font;
25135 int base_width, base_height, width, height;
25136 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25137 int len;
25138
25139 /* Get the metrics of the base font. We always refer to the current
25140 ASCII face. */
25141 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25142 font = face->font ? face->font : FRAME_FONT (it->f);
25143 it->ascent = FONT_BASE (font) + font->baseline_offset;
25144 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25145 base_height = it->ascent + it->descent;
25146 base_width = font->average_width;
25147
25148 /* Get a face ID for the glyph by utilizing a cache (the same way as
25149 done for `escape-glyph' in get_next_display_element). */
25150 if (it->f == last_glyphless_glyph_frame
25151 && it->face_id == last_glyphless_glyph_face_id)
25152 {
25153 face_id = last_glyphless_glyph_merged_face_id;
25154 }
25155 else
25156 {
25157 /* Merge the `glyphless-char' face into the current face. */
25158 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
25159 last_glyphless_glyph_frame = it->f;
25160 last_glyphless_glyph_face_id = it->face_id;
25161 last_glyphless_glyph_merged_face_id = face_id;
25162 }
25163
25164 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25165 {
25166 it->pixel_width = THIN_SPACE_WIDTH;
25167 len = 0;
25168 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25169 }
25170 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25171 {
25172 width = CHAR_WIDTH (it->c);
25173 if (width == 0)
25174 width = 1;
25175 else if (width > 4)
25176 width = 4;
25177 it->pixel_width = base_width * width;
25178 len = 0;
25179 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25180 }
25181 else
25182 {
25183 char buf[7];
25184 const char *str;
25185 unsigned int code[6];
25186 int upper_len;
25187 int ascent, descent;
25188 struct font_metrics metrics_upper, metrics_lower;
25189
25190 face = FACE_FROM_ID (it->f, face_id);
25191 font = face->font ? face->font : FRAME_FONT (it->f);
25192 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25193
25194 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25195 {
25196 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25197 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25198 if (CONSP (acronym))
25199 acronym = XCAR (acronym);
25200 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25201 }
25202 else
25203 {
25204 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25205 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25206 str = buf;
25207 }
25208 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25209 code[len] = font->driver->encode_char (font, str[len]);
25210 upper_len = (len + 1) / 2;
25211 font->driver->text_extents (font, code, upper_len,
25212 &metrics_upper);
25213 font->driver->text_extents (font, code + upper_len, len - upper_len,
25214 &metrics_lower);
25215
25216
25217
25218 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25219 width = max (metrics_upper.width, metrics_lower.width) + 4;
25220 upper_xoff = upper_yoff = 2; /* the typical case */
25221 if (base_width >= width)
25222 {
25223 /* Align the upper to the left, the lower to the right. */
25224 it->pixel_width = base_width;
25225 lower_xoff = base_width - 2 - metrics_lower.width;
25226 }
25227 else
25228 {
25229 /* Center the shorter one. */
25230 it->pixel_width = width;
25231 if (metrics_upper.width >= metrics_lower.width)
25232 lower_xoff = (width - metrics_lower.width) / 2;
25233 else
25234 {
25235 /* FIXME: This code doesn't look right. It formerly was
25236 missing the "lower_xoff = 0;", which couldn't have
25237 been right since it left lower_xoff uninitialized. */
25238 lower_xoff = 0;
25239 upper_xoff = (width - metrics_upper.width) / 2;
25240 }
25241 }
25242
25243 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25244 top, bottom, and between upper and lower strings. */
25245 height = (metrics_upper.ascent + metrics_upper.descent
25246 + metrics_lower.ascent + metrics_lower.descent) + 5;
25247 /* Center vertically.
25248 H:base_height, D:base_descent
25249 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25250
25251 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25252 descent = D - H/2 + h/2;
25253 lower_yoff = descent - 2 - ld;
25254 upper_yoff = lower_yoff - la - 1 - ud; */
25255 ascent = - (it->descent - (base_height + height + 1) / 2);
25256 descent = it->descent - (base_height - height) / 2;
25257 lower_yoff = descent - 2 - metrics_lower.descent;
25258 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25259 - metrics_upper.descent);
25260 /* Don't make the height shorter than the base height. */
25261 if (height > base_height)
25262 {
25263 it->ascent = ascent;
25264 it->descent = descent;
25265 }
25266 }
25267
25268 it->phys_ascent = it->ascent;
25269 it->phys_descent = it->descent;
25270 if (it->glyph_row)
25271 append_glyphless_glyph (it, face_id, for_no_font, len,
25272 upper_xoff, upper_yoff,
25273 lower_xoff, lower_yoff);
25274 it->nglyphs = 1;
25275 take_vertical_position_into_account (it);
25276 }
25277
25278
25279 /* RIF:
25280 Produce glyphs/get display metrics for the display element IT is
25281 loaded with. See the description of struct it in dispextern.h
25282 for an overview of struct it. */
25283
25284 void
25285 x_produce_glyphs (struct it *it)
25286 {
25287 int extra_line_spacing = it->extra_line_spacing;
25288
25289 it->glyph_not_available_p = 0;
25290
25291 if (it->what == IT_CHARACTER)
25292 {
25293 XChar2b char2b;
25294 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25295 struct font *font = face->font;
25296 struct font_metrics *pcm = NULL;
25297 int boff; /* baseline offset */
25298
25299 if (font == NULL)
25300 {
25301 /* When no suitable font is found, display this character by
25302 the method specified in the first extra slot of
25303 Vglyphless_char_display. */
25304 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25305
25306 eassert (it->what == IT_GLYPHLESS);
25307 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25308 goto done;
25309 }
25310
25311 boff = font->baseline_offset;
25312 if (font->vertical_centering)
25313 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25314
25315 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25316 {
25317 int stretched_p;
25318
25319 it->nglyphs = 1;
25320
25321 if (it->override_ascent >= 0)
25322 {
25323 it->ascent = it->override_ascent;
25324 it->descent = it->override_descent;
25325 boff = it->override_boff;
25326 }
25327 else
25328 {
25329 it->ascent = FONT_BASE (font) + boff;
25330 it->descent = FONT_DESCENT (font) - boff;
25331 }
25332
25333 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25334 {
25335 pcm = get_per_char_metric (font, &char2b);
25336 if (pcm->width == 0
25337 && pcm->rbearing == 0 && pcm->lbearing == 0)
25338 pcm = NULL;
25339 }
25340
25341 if (pcm)
25342 {
25343 it->phys_ascent = pcm->ascent + boff;
25344 it->phys_descent = pcm->descent - boff;
25345 it->pixel_width = pcm->width;
25346 }
25347 else
25348 {
25349 it->glyph_not_available_p = 1;
25350 it->phys_ascent = it->ascent;
25351 it->phys_descent = it->descent;
25352 it->pixel_width = font->space_width;
25353 }
25354
25355 if (it->constrain_row_ascent_descent_p)
25356 {
25357 if (it->descent > it->max_descent)
25358 {
25359 it->ascent += it->descent - it->max_descent;
25360 it->descent = it->max_descent;
25361 }
25362 if (it->ascent > it->max_ascent)
25363 {
25364 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25365 it->ascent = it->max_ascent;
25366 }
25367 it->phys_ascent = min (it->phys_ascent, it->ascent);
25368 it->phys_descent = min (it->phys_descent, it->descent);
25369 extra_line_spacing = 0;
25370 }
25371
25372 /* If this is a space inside a region of text with
25373 `space-width' property, change its width. */
25374 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25375 if (stretched_p)
25376 it->pixel_width *= XFLOATINT (it->space_width);
25377
25378 /* If face has a box, add the box thickness to the character
25379 height. If character has a box line to the left and/or
25380 right, add the box line width to the character's width. */
25381 if (face->box != FACE_NO_BOX)
25382 {
25383 int thick = face->box_line_width;
25384
25385 if (thick > 0)
25386 {
25387 it->ascent += thick;
25388 it->descent += thick;
25389 }
25390 else
25391 thick = -thick;
25392
25393 if (it->start_of_box_run_p)
25394 it->pixel_width += thick;
25395 if (it->end_of_box_run_p)
25396 it->pixel_width += thick;
25397 }
25398
25399 /* If face has an overline, add the height of the overline
25400 (1 pixel) and a 1 pixel margin to the character height. */
25401 if (face->overline_p)
25402 it->ascent += overline_margin;
25403
25404 if (it->constrain_row_ascent_descent_p)
25405 {
25406 if (it->ascent > it->max_ascent)
25407 it->ascent = it->max_ascent;
25408 if (it->descent > it->max_descent)
25409 it->descent = it->max_descent;
25410 }
25411
25412 take_vertical_position_into_account (it);
25413
25414 /* If we have to actually produce glyphs, do it. */
25415 if (it->glyph_row)
25416 {
25417 if (stretched_p)
25418 {
25419 /* Translate a space with a `space-width' property
25420 into a stretch glyph. */
25421 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25422 / FONT_HEIGHT (font));
25423 append_stretch_glyph (it, it->object, it->pixel_width,
25424 it->ascent + it->descent, ascent);
25425 }
25426 else
25427 append_glyph (it);
25428
25429 /* If characters with lbearing or rbearing are displayed
25430 in this line, record that fact in a flag of the
25431 glyph row. This is used to optimize X output code. */
25432 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25433 it->glyph_row->contains_overlapping_glyphs_p = 1;
25434 }
25435 if (! stretched_p && it->pixel_width == 0)
25436 /* We assure that all visible glyphs have at least 1-pixel
25437 width. */
25438 it->pixel_width = 1;
25439 }
25440 else if (it->char_to_display == '\n')
25441 {
25442 /* A newline has no width, but we need the height of the
25443 line. But if previous part of the line sets a height,
25444 don't increase that height */
25445
25446 Lisp_Object height;
25447 Lisp_Object total_height = Qnil;
25448
25449 it->override_ascent = -1;
25450 it->pixel_width = 0;
25451 it->nglyphs = 0;
25452
25453 height = get_it_property (it, Qline_height);
25454 /* Split (line-height total-height) list */
25455 if (CONSP (height)
25456 && CONSP (XCDR (height))
25457 && NILP (XCDR (XCDR (height))))
25458 {
25459 total_height = XCAR (XCDR (height));
25460 height = XCAR (height);
25461 }
25462 height = calc_line_height_property (it, height, font, boff, 1);
25463
25464 if (it->override_ascent >= 0)
25465 {
25466 it->ascent = it->override_ascent;
25467 it->descent = it->override_descent;
25468 boff = it->override_boff;
25469 }
25470 else
25471 {
25472 it->ascent = FONT_BASE (font) + boff;
25473 it->descent = FONT_DESCENT (font) - boff;
25474 }
25475
25476 if (EQ (height, Qt))
25477 {
25478 if (it->descent > it->max_descent)
25479 {
25480 it->ascent += it->descent - it->max_descent;
25481 it->descent = it->max_descent;
25482 }
25483 if (it->ascent > it->max_ascent)
25484 {
25485 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25486 it->ascent = it->max_ascent;
25487 }
25488 it->phys_ascent = min (it->phys_ascent, it->ascent);
25489 it->phys_descent = min (it->phys_descent, it->descent);
25490 it->constrain_row_ascent_descent_p = 1;
25491 extra_line_spacing = 0;
25492 }
25493 else
25494 {
25495 Lisp_Object spacing;
25496
25497 it->phys_ascent = it->ascent;
25498 it->phys_descent = it->descent;
25499
25500 if ((it->max_ascent > 0 || it->max_descent > 0)
25501 && face->box != FACE_NO_BOX
25502 && face->box_line_width > 0)
25503 {
25504 it->ascent += face->box_line_width;
25505 it->descent += face->box_line_width;
25506 }
25507 if (!NILP (height)
25508 && XINT (height) > it->ascent + it->descent)
25509 it->ascent = XINT (height) - it->descent;
25510
25511 if (!NILP (total_height))
25512 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25513 else
25514 {
25515 spacing = get_it_property (it, Qline_spacing);
25516 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25517 }
25518 if (INTEGERP (spacing))
25519 {
25520 extra_line_spacing = XINT (spacing);
25521 if (!NILP (total_height))
25522 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25523 }
25524 }
25525 }
25526 else /* i.e. (it->char_to_display == '\t') */
25527 {
25528 if (font->space_width > 0)
25529 {
25530 int tab_width = it->tab_width * font->space_width;
25531 int x = it->current_x + it->continuation_lines_width;
25532 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25533
25534 /* If the distance from the current position to the next tab
25535 stop is less than a space character width, use the
25536 tab stop after that. */
25537 if (next_tab_x - x < font->space_width)
25538 next_tab_x += tab_width;
25539
25540 it->pixel_width = next_tab_x - x;
25541 it->nglyphs = 1;
25542 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25543 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25544
25545 if (it->glyph_row)
25546 {
25547 append_stretch_glyph (it, it->object, it->pixel_width,
25548 it->ascent + it->descent, it->ascent);
25549 }
25550 }
25551 else
25552 {
25553 it->pixel_width = 0;
25554 it->nglyphs = 1;
25555 }
25556 }
25557 }
25558 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25559 {
25560 /* A static composition.
25561
25562 Note: A composition is represented as one glyph in the
25563 glyph matrix. There are no padding glyphs.
25564
25565 Important note: pixel_width, ascent, and descent are the
25566 values of what is drawn by draw_glyphs (i.e. the values of
25567 the overall glyphs composed). */
25568 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25569 int boff; /* baseline offset */
25570 struct composition *cmp = composition_table[it->cmp_it.id];
25571 int glyph_len = cmp->glyph_len;
25572 struct font *font = face->font;
25573
25574 it->nglyphs = 1;
25575
25576 /* If we have not yet calculated pixel size data of glyphs of
25577 the composition for the current face font, calculate them
25578 now. Theoretically, we have to check all fonts for the
25579 glyphs, but that requires much time and memory space. So,
25580 here we check only the font of the first glyph. This may
25581 lead to incorrect display, but it's very rare, and C-l
25582 (recenter-top-bottom) can correct the display anyway. */
25583 if (! cmp->font || cmp->font != font)
25584 {
25585 /* Ascent and descent of the font of the first character
25586 of this composition (adjusted by baseline offset).
25587 Ascent and descent of overall glyphs should not be less
25588 than these, respectively. */
25589 int font_ascent, font_descent, font_height;
25590 /* Bounding box of the overall glyphs. */
25591 int leftmost, rightmost, lowest, highest;
25592 int lbearing, rbearing;
25593 int i, width, ascent, descent;
25594 int left_padded = 0, right_padded = 0;
25595 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25596 XChar2b char2b;
25597 struct font_metrics *pcm;
25598 int font_not_found_p;
25599 ptrdiff_t pos;
25600
25601 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25602 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25603 break;
25604 if (glyph_len < cmp->glyph_len)
25605 right_padded = 1;
25606 for (i = 0; i < glyph_len; i++)
25607 {
25608 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25609 break;
25610 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25611 }
25612 if (i > 0)
25613 left_padded = 1;
25614
25615 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25616 : IT_CHARPOS (*it));
25617 /* If no suitable font is found, use the default font. */
25618 font_not_found_p = font == NULL;
25619 if (font_not_found_p)
25620 {
25621 face = face->ascii_face;
25622 font = face->font;
25623 }
25624 boff = font->baseline_offset;
25625 if (font->vertical_centering)
25626 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25627 font_ascent = FONT_BASE (font) + boff;
25628 font_descent = FONT_DESCENT (font) - boff;
25629 font_height = FONT_HEIGHT (font);
25630
25631 cmp->font = font;
25632
25633 pcm = NULL;
25634 if (! font_not_found_p)
25635 {
25636 get_char_face_and_encoding (it->f, c, it->face_id,
25637 &char2b, 0);
25638 pcm = get_per_char_metric (font, &char2b);
25639 }
25640
25641 /* Initialize the bounding box. */
25642 if (pcm)
25643 {
25644 width = cmp->glyph_len > 0 ? pcm->width : 0;
25645 ascent = pcm->ascent;
25646 descent = pcm->descent;
25647 lbearing = pcm->lbearing;
25648 rbearing = pcm->rbearing;
25649 }
25650 else
25651 {
25652 width = cmp->glyph_len > 0 ? font->space_width : 0;
25653 ascent = FONT_BASE (font);
25654 descent = FONT_DESCENT (font);
25655 lbearing = 0;
25656 rbearing = width;
25657 }
25658
25659 rightmost = width;
25660 leftmost = 0;
25661 lowest = - descent + boff;
25662 highest = ascent + boff;
25663
25664 if (! font_not_found_p
25665 && font->default_ascent
25666 && CHAR_TABLE_P (Vuse_default_ascent)
25667 && !NILP (Faref (Vuse_default_ascent,
25668 make_number (it->char_to_display))))
25669 highest = font->default_ascent + boff;
25670
25671 /* Draw the first glyph at the normal position. It may be
25672 shifted to right later if some other glyphs are drawn
25673 at the left. */
25674 cmp->offsets[i * 2] = 0;
25675 cmp->offsets[i * 2 + 1] = boff;
25676 cmp->lbearing = lbearing;
25677 cmp->rbearing = rbearing;
25678
25679 /* Set cmp->offsets for the remaining glyphs. */
25680 for (i++; i < glyph_len; i++)
25681 {
25682 int left, right, btm, top;
25683 int ch = COMPOSITION_GLYPH (cmp, i);
25684 int face_id;
25685 struct face *this_face;
25686
25687 if (ch == '\t')
25688 ch = ' ';
25689 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25690 this_face = FACE_FROM_ID (it->f, face_id);
25691 font = this_face->font;
25692
25693 if (font == NULL)
25694 pcm = NULL;
25695 else
25696 {
25697 get_char_face_and_encoding (it->f, ch, face_id,
25698 &char2b, 0);
25699 pcm = get_per_char_metric (font, &char2b);
25700 }
25701 if (! pcm)
25702 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25703 else
25704 {
25705 width = pcm->width;
25706 ascent = pcm->ascent;
25707 descent = pcm->descent;
25708 lbearing = pcm->lbearing;
25709 rbearing = pcm->rbearing;
25710 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25711 {
25712 /* Relative composition with or without
25713 alternate chars. */
25714 left = (leftmost + rightmost - width) / 2;
25715 btm = - descent + boff;
25716 if (font->relative_compose
25717 && (! CHAR_TABLE_P (Vignore_relative_composition)
25718 || NILP (Faref (Vignore_relative_composition,
25719 make_number (ch)))))
25720 {
25721
25722 if (- descent >= font->relative_compose)
25723 /* One extra pixel between two glyphs. */
25724 btm = highest + 1;
25725 else if (ascent <= 0)
25726 /* One extra pixel between two glyphs. */
25727 btm = lowest - 1 - ascent - descent;
25728 }
25729 }
25730 else
25731 {
25732 /* A composition rule is specified by an integer
25733 value that encodes global and new reference
25734 points (GREF and NREF). GREF and NREF are
25735 specified by numbers as below:
25736
25737 0---1---2 -- ascent
25738 | |
25739 | |
25740 | |
25741 9--10--11 -- center
25742 | |
25743 ---3---4---5--- baseline
25744 | |
25745 6---7---8 -- descent
25746 */
25747 int rule = COMPOSITION_RULE (cmp, i);
25748 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25749
25750 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25751 grefx = gref % 3, nrefx = nref % 3;
25752 grefy = gref / 3, nrefy = nref / 3;
25753 if (xoff)
25754 xoff = font_height * (xoff - 128) / 256;
25755 if (yoff)
25756 yoff = font_height * (yoff - 128) / 256;
25757
25758 left = (leftmost
25759 + grefx * (rightmost - leftmost) / 2
25760 - nrefx * width / 2
25761 + xoff);
25762
25763 btm = ((grefy == 0 ? highest
25764 : grefy == 1 ? 0
25765 : grefy == 2 ? lowest
25766 : (highest + lowest) / 2)
25767 - (nrefy == 0 ? ascent + descent
25768 : nrefy == 1 ? descent - boff
25769 : nrefy == 2 ? 0
25770 : (ascent + descent) / 2)
25771 + yoff);
25772 }
25773
25774 cmp->offsets[i * 2] = left;
25775 cmp->offsets[i * 2 + 1] = btm + descent;
25776
25777 /* Update the bounding box of the overall glyphs. */
25778 if (width > 0)
25779 {
25780 right = left + width;
25781 if (left < leftmost)
25782 leftmost = left;
25783 if (right > rightmost)
25784 rightmost = right;
25785 }
25786 top = btm + descent + ascent;
25787 if (top > highest)
25788 highest = top;
25789 if (btm < lowest)
25790 lowest = btm;
25791
25792 if (cmp->lbearing > left + lbearing)
25793 cmp->lbearing = left + lbearing;
25794 if (cmp->rbearing < left + rbearing)
25795 cmp->rbearing = left + rbearing;
25796 }
25797 }
25798
25799 /* If there are glyphs whose x-offsets are negative,
25800 shift all glyphs to the right and make all x-offsets
25801 non-negative. */
25802 if (leftmost < 0)
25803 {
25804 for (i = 0; i < cmp->glyph_len; i++)
25805 cmp->offsets[i * 2] -= leftmost;
25806 rightmost -= leftmost;
25807 cmp->lbearing -= leftmost;
25808 cmp->rbearing -= leftmost;
25809 }
25810
25811 if (left_padded && cmp->lbearing < 0)
25812 {
25813 for (i = 0; i < cmp->glyph_len; i++)
25814 cmp->offsets[i * 2] -= cmp->lbearing;
25815 rightmost -= cmp->lbearing;
25816 cmp->rbearing -= cmp->lbearing;
25817 cmp->lbearing = 0;
25818 }
25819 if (right_padded && rightmost < cmp->rbearing)
25820 {
25821 rightmost = cmp->rbearing;
25822 }
25823
25824 cmp->pixel_width = rightmost;
25825 cmp->ascent = highest;
25826 cmp->descent = - lowest;
25827 if (cmp->ascent < font_ascent)
25828 cmp->ascent = font_ascent;
25829 if (cmp->descent < font_descent)
25830 cmp->descent = font_descent;
25831 }
25832
25833 if (it->glyph_row
25834 && (cmp->lbearing < 0
25835 || cmp->rbearing > cmp->pixel_width))
25836 it->glyph_row->contains_overlapping_glyphs_p = 1;
25837
25838 it->pixel_width = cmp->pixel_width;
25839 it->ascent = it->phys_ascent = cmp->ascent;
25840 it->descent = it->phys_descent = cmp->descent;
25841 if (face->box != FACE_NO_BOX)
25842 {
25843 int thick = face->box_line_width;
25844
25845 if (thick > 0)
25846 {
25847 it->ascent += thick;
25848 it->descent += thick;
25849 }
25850 else
25851 thick = - thick;
25852
25853 if (it->start_of_box_run_p)
25854 it->pixel_width += thick;
25855 if (it->end_of_box_run_p)
25856 it->pixel_width += thick;
25857 }
25858
25859 /* If face has an overline, add the height of the overline
25860 (1 pixel) and a 1 pixel margin to the character height. */
25861 if (face->overline_p)
25862 it->ascent += overline_margin;
25863
25864 take_vertical_position_into_account (it);
25865 if (it->ascent < 0)
25866 it->ascent = 0;
25867 if (it->descent < 0)
25868 it->descent = 0;
25869
25870 if (it->glyph_row && cmp->glyph_len > 0)
25871 append_composite_glyph (it);
25872 }
25873 else if (it->what == IT_COMPOSITION)
25874 {
25875 /* A dynamic (automatic) composition. */
25876 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25877 Lisp_Object gstring;
25878 struct font_metrics metrics;
25879
25880 it->nglyphs = 1;
25881
25882 gstring = composition_gstring_from_id (it->cmp_it.id);
25883 it->pixel_width
25884 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25885 &metrics);
25886 if (it->glyph_row
25887 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25888 it->glyph_row->contains_overlapping_glyphs_p = 1;
25889 it->ascent = it->phys_ascent = metrics.ascent;
25890 it->descent = it->phys_descent = metrics.descent;
25891 if (face->box != FACE_NO_BOX)
25892 {
25893 int thick = face->box_line_width;
25894
25895 if (thick > 0)
25896 {
25897 it->ascent += thick;
25898 it->descent += thick;
25899 }
25900 else
25901 thick = - thick;
25902
25903 if (it->start_of_box_run_p)
25904 it->pixel_width += thick;
25905 if (it->end_of_box_run_p)
25906 it->pixel_width += thick;
25907 }
25908 /* If face has an overline, add the height of the overline
25909 (1 pixel) and a 1 pixel margin to the character height. */
25910 if (face->overline_p)
25911 it->ascent += overline_margin;
25912 take_vertical_position_into_account (it);
25913 if (it->ascent < 0)
25914 it->ascent = 0;
25915 if (it->descent < 0)
25916 it->descent = 0;
25917
25918 if (it->glyph_row)
25919 append_composite_glyph (it);
25920 }
25921 else if (it->what == IT_GLYPHLESS)
25922 produce_glyphless_glyph (it, 0, Qnil);
25923 else if (it->what == IT_IMAGE)
25924 produce_image_glyph (it);
25925 else if (it->what == IT_STRETCH)
25926 produce_stretch_glyph (it);
25927 #ifdef HAVE_XWIDGETS
25928 else if (it->what == IT_XWIDGET)
25929 produce_xwidget_glyph (it);
25930 #endif
25931 done:
25932 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25933 because this isn't true for images with `:ascent 100'. */
25934 eassert (it->ascent >= 0 && it->descent >= 0);
25935 if (it->area == TEXT_AREA)
25936 it->current_x += it->pixel_width;
25937
25938 if (extra_line_spacing > 0)
25939 {
25940 it->descent += extra_line_spacing;
25941 if (extra_line_spacing > it->max_extra_line_spacing)
25942 it->max_extra_line_spacing = extra_line_spacing;
25943 }
25944
25945 it->max_ascent = max (it->max_ascent, it->ascent);
25946 it->max_descent = max (it->max_descent, it->descent);
25947 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25948 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25949 }
25950
25951 /* EXPORT for RIF:
25952 Output LEN glyphs starting at START at the nominal cursor position.
25953 Advance the nominal cursor over the text. The global variable
25954 updated_row is the glyph row being updated, and updated_area is the
25955 area of that row being updated. */
25956
25957 void
25958 x_write_glyphs (struct window *w, struct glyph *start, int len)
25959 {
25960 int x, hpos, chpos = w->phys_cursor.hpos;
25961
25962 eassert (updated_row);
25963 /* When the window is hscrolled, cursor hpos can legitimately be out
25964 of bounds, but we draw the cursor at the corresponding window
25965 margin in that case. */
25966 if (!updated_row->reversed_p && chpos < 0)
25967 chpos = 0;
25968 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25969 chpos = updated_row->used[TEXT_AREA] - 1;
25970
25971 block_input ();
25972
25973 /* Write glyphs. */
25974
25975 hpos = start - updated_row->glyphs[updated_area];
25976 x = draw_glyphs (w, output_cursor.x,
25977 updated_row, updated_area,
25978 hpos, hpos + len,
25979 DRAW_NORMAL_TEXT, 0);
25980
25981 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25982 if (updated_area == TEXT_AREA
25983 && w->phys_cursor_on_p
25984 && w->phys_cursor.vpos == output_cursor.vpos
25985 && chpos >= hpos
25986 && chpos < hpos + len)
25987 w->phys_cursor_on_p = 0;
25988
25989 unblock_input ();
25990
25991 /* Advance the output cursor. */
25992 output_cursor.hpos += len;
25993 output_cursor.x = x;
25994 }
25995
25996
25997 /* EXPORT for RIF:
25998 Insert LEN glyphs from START at the nominal cursor position. */
25999
26000 void
26001 x_insert_glyphs (struct window *w, struct glyph *start, int len)
26002 {
26003 struct frame *f;
26004 int line_height, shift_by_width, shifted_region_width;
26005 struct glyph_row *row;
26006 struct glyph *glyph;
26007 int frame_x, frame_y;
26008 ptrdiff_t hpos;
26009
26010 eassert (updated_row);
26011 block_input ();
26012 f = XFRAME (WINDOW_FRAME (w));
26013
26014 /* Get the height of the line we are in. */
26015 row = updated_row;
26016 line_height = row->height;
26017
26018 /* Get the width of the glyphs to insert. */
26019 shift_by_width = 0;
26020 for (glyph = start; glyph < start + len; ++glyph)
26021 shift_by_width += glyph->pixel_width;
26022
26023 /* Get the width of the region to shift right. */
26024 shifted_region_width = (window_box_width (w, updated_area)
26025 - output_cursor.x
26026 - shift_by_width);
26027
26028 /* Shift right. */
26029 frame_x = window_box_left (w, updated_area) + output_cursor.x;
26030 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
26031
26032 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26033 line_height, shift_by_width);
26034
26035 /* Write the glyphs. */
26036 hpos = start - row->glyphs[updated_area];
26037 draw_glyphs (w, output_cursor.x, row, updated_area,
26038 hpos, hpos + len,
26039 DRAW_NORMAL_TEXT, 0);
26040
26041 /* Advance the output cursor. */
26042 output_cursor.hpos += len;
26043 output_cursor.x += shift_by_width;
26044 unblock_input ();
26045 }
26046
26047
26048 /* EXPORT for RIF:
26049 Erase the current text line from the nominal cursor position
26050 (inclusive) to pixel column TO_X (exclusive). The idea is that
26051 everything from TO_X onward is already erased.
26052
26053 TO_X is a pixel position relative to updated_area of currently
26054 updated window W. TO_X == -1 means clear to the end of this area. */
26055
26056 void
26057 x_clear_end_of_line (struct window *w, int to_x)
26058 {
26059 struct frame *f;
26060 int max_x, min_y, max_y;
26061 int from_x, from_y, to_y;
26062
26063 eassert (updated_row);
26064 f = XFRAME (w->frame);
26065
26066 if (updated_row->full_width_p)
26067 max_x = WINDOW_TOTAL_WIDTH (w);
26068 else
26069 max_x = window_box_width (w, updated_area);
26070 max_y = window_text_bottom_y (w);
26071
26072 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26073 of window. For TO_X > 0, truncate to end of drawing area. */
26074 if (to_x == 0)
26075 return;
26076 else if (to_x < 0)
26077 to_x = max_x;
26078 else
26079 to_x = min (to_x, max_x);
26080
26081 to_y = min (max_y, output_cursor.y + updated_row->height);
26082
26083 /* Notice if the cursor will be cleared by this operation. */
26084 if (!updated_row->full_width_p)
26085 notice_overwritten_cursor (w, updated_area,
26086 output_cursor.x, -1,
26087 updated_row->y,
26088 MATRIX_ROW_BOTTOM_Y (updated_row));
26089
26090 from_x = output_cursor.x;
26091
26092 /* Translate to frame coordinates. */
26093 if (updated_row->full_width_p)
26094 {
26095 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26096 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26097 }
26098 else
26099 {
26100 int area_left = window_box_left (w, updated_area);
26101 from_x += area_left;
26102 to_x += area_left;
26103 }
26104
26105 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26106 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
26107 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26108
26109 /* Prevent inadvertently clearing to end of the X window. */
26110 if (to_x > from_x && to_y > from_y)
26111 {
26112 block_input ();
26113 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26114 to_x - from_x, to_y - from_y);
26115 unblock_input ();
26116 }
26117 }
26118
26119 #endif /* HAVE_WINDOW_SYSTEM */
26120
26121
26122 \f
26123 /***********************************************************************
26124 Cursor types
26125 ***********************************************************************/
26126
26127 /* Value is the internal representation of the specified cursor type
26128 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26129 of the bar cursor. */
26130
26131 static enum text_cursor_kinds
26132 get_specified_cursor_type (Lisp_Object arg, int *width)
26133 {
26134 enum text_cursor_kinds type;
26135
26136 if (NILP (arg))
26137 return NO_CURSOR;
26138
26139 if (EQ (arg, Qbox))
26140 return FILLED_BOX_CURSOR;
26141
26142 if (EQ (arg, Qhollow))
26143 return HOLLOW_BOX_CURSOR;
26144
26145 if (EQ (arg, Qbar))
26146 {
26147 *width = 2;
26148 return BAR_CURSOR;
26149 }
26150
26151 if (CONSP (arg)
26152 && EQ (XCAR (arg), Qbar)
26153 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26154 {
26155 *width = XINT (XCDR (arg));
26156 return BAR_CURSOR;
26157 }
26158
26159 if (EQ (arg, Qhbar))
26160 {
26161 *width = 2;
26162 return HBAR_CURSOR;
26163 }
26164
26165 if (CONSP (arg)
26166 && EQ (XCAR (arg), Qhbar)
26167 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26168 {
26169 *width = XINT (XCDR (arg));
26170 return HBAR_CURSOR;
26171 }
26172
26173 /* Treat anything unknown as "hollow box cursor".
26174 It was bad to signal an error; people have trouble fixing
26175 .Xdefaults with Emacs, when it has something bad in it. */
26176 type = HOLLOW_BOX_CURSOR;
26177
26178 return type;
26179 }
26180
26181 /* Set the default cursor types for specified frame. */
26182 void
26183 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26184 {
26185 int width = 1;
26186 Lisp_Object tem;
26187
26188 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26189 FRAME_CURSOR_WIDTH (f) = width;
26190
26191 /* By default, set up the blink-off state depending on the on-state. */
26192
26193 tem = Fassoc (arg, Vblink_cursor_alist);
26194 if (!NILP (tem))
26195 {
26196 FRAME_BLINK_OFF_CURSOR (f)
26197 = get_specified_cursor_type (XCDR (tem), &width);
26198 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26199 }
26200 else
26201 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26202
26203 /* Make sure the cursor gets redrawn. */
26204 cursor_type_changed = 1;
26205 }
26206
26207
26208 #ifdef HAVE_WINDOW_SYSTEM
26209
26210 /* Return the cursor we want to be displayed in window W. Return
26211 width of bar/hbar cursor through WIDTH arg. Return with
26212 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26213 (i.e. if the `system caret' should track this cursor).
26214
26215 In a mini-buffer window, we want the cursor only to appear if we
26216 are reading input from this window. For the selected window, we
26217 want the cursor type given by the frame parameter or buffer local
26218 setting of cursor-type. If explicitly marked off, draw no cursor.
26219 In all other cases, we want a hollow box cursor. */
26220
26221 static enum text_cursor_kinds
26222 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26223 int *active_cursor)
26224 {
26225 struct frame *f = XFRAME (w->frame);
26226 struct buffer *b = XBUFFER (w->contents);
26227 int cursor_type = DEFAULT_CURSOR;
26228 Lisp_Object alt_cursor;
26229 int non_selected = 0;
26230
26231 *active_cursor = 1;
26232
26233 /* Echo area */
26234 if (cursor_in_echo_area
26235 && FRAME_HAS_MINIBUF_P (f)
26236 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26237 {
26238 if (w == XWINDOW (echo_area_window))
26239 {
26240 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26241 {
26242 *width = FRAME_CURSOR_WIDTH (f);
26243 return FRAME_DESIRED_CURSOR (f);
26244 }
26245 else
26246 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26247 }
26248
26249 *active_cursor = 0;
26250 non_selected = 1;
26251 }
26252
26253 /* Detect a nonselected window or nonselected frame. */
26254 else if (w != XWINDOW (f->selected_window)
26255 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26256 {
26257 *active_cursor = 0;
26258
26259 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26260 return NO_CURSOR;
26261
26262 non_selected = 1;
26263 }
26264
26265 /* Never display a cursor in a window in which cursor-type is nil. */
26266 if (NILP (BVAR (b, cursor_type)))
26267 return NO_CURSOR;
26268
26269 /* Get the normal cursor type for this window. */
26270 if (EQ (BVAR (b, cursor_type), Qt))
26271 {
26272 cursor_type = FRAME_DESIRED_CURSOR (f);
26273 *width = FRAME_CURSOR_WIDTH (f);
26274 }
26275 else
26276 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26277
26278 /* Use cursor-in-non-selected-windows instead
26279 for non-selected window or frame. */
26280 if (non_selected)
26281 {
26282 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26283 if (!EQ (Qt, alt_cursor))
26284 return get_specified_cursor_type (alt_cursor, width);
26285 /* t means modify the normal cursor type. */
26286 if (cursor_type == FILLED_BOX_CURSOR)
26287 cursor_type = HOLLOW_BOX_CURSOR;
26288 else if (cursor_type == BAR_CURSOR && *width > 1)
26289 --*width;
26290 return cursor_type;
26291 }
26292
26293 /* Use normal cursor if not blinked off. */
26294 if (!w->cursor_off_p)
26295 {
26296
26297 #ifdef HAVE_XWIDGETS
26298 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
26299 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
26300 return NO_CURSOR;
26301 }
26302 #endif
26303 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26304 {
26305 if (cursor_type == FILLED_BOX_CURSOR)
26306 {
26307 /* Using a block cursor on large images can be very annoying.
26308 So use a hollow cursor for "large" images.
26309 If image is not transparent (no mask), also use hollow cursor. */
26310 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26311 if (img != NULL && IMAGEP (img->spec))
26312 {
26313 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26314 where N = size of default frame font size.
26315 This should cover most of the "tiny" icons people may use. */
26316 if (!img->mask
26317 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26318 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26319 cursor_type = HOLLOW_BOX_CURSOR;
26320 }
26321 }
26322 else if (cursor_type != NO_CURSOR)
26323 {
26324 /* Display current only supports BOX and HOLLOW cursors for images.
26325 So for now, unconditionally use a HOLLOW cursor when cursor is
26326 not a solid box cursor. */
26327 cursor_type = HOLLOW_BOX_CURSOR;
26328 }
26329 }
26330 return cursor_type;
26331 }
26332
26333 /* Cursor is blinked off, so determine how to "toggle" it. */
26334
26335 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26336 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26337 return get_specified_cursor_type (XCDR (alt_cursor), width);
26338
26339 /* Then see if frame has specified a specific blink off cursor type. */
26340 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26341 {
26342 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26343 return FRAME_BLINK_OFF_CURSOR (f);
26344 }
26345
26346 #if 0
26347 /* Some people liked having a permanently visible blinking cursor,
26348 while others had very strong opinions against it. So it was
26349 decided to remove it. KFS 2003-09-03 */
26350
26351 /* Finally perform built-in cursor blinking:
26352 filled box <-> hollow box
26353 wide [h]bar <-> narrow [h]bar
26354 narrow [h]bar <-> no cursor
26355 other type <-> no cursor */
26356
26357 if (cursor_type == FILLED_BOX_CURSOR)
26358 return HOLLOW_BOX_CURSOR;
26359
26360 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26361 {
26362 *width = 1;
26363 return cursor_type;
26364 }
26365 #endif
26366
26367 return NO_CURSOR;
26368 }
26369
26370
26371 /* Notice when the text cursor of window W has been completely
26372 overwritten by a drawing operation that outputs glyphs in AREA
26373 starting at X0 and ending at X1 in the line starting at Y0 and
26374 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26375 the rest of the line after X0 has been written. Y coordinates
26376 are window-relative. */
26377
26378 static void
26379 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26380 int x0, int x1, int y0, int y1)
26381 {
26382 int cx0, cx1, cy0, cy1;
26383 struct glyph_row *row;
26384
26385 if (!w->phys_cursor_on_p)
26386 return;
26387 if (area != TEXT_AREA)
26388 return;
26389
26390 if (w->phys_cursor.vpos < 0
26391 || w->phys_cursor.vpos >= w->current_matrix->nrows
26392 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26393 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26394 return;
26395
26396 if (row->cursor_in_fringe_p)
26397 {
26398 row->cursor_in_fringe_p = 0;
26399 draw_fringe_bitmap (w, row, row->reversed_p);
26400 w->phys_cursor_on_p = 0;
26401 return;
26402 }
26403
26404 cx0 = w->phys_cursor.x;
26405 cx1 = cx0 + w->phys_cursor_width;
26406 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26407 return;
26408
26409 /* The cursor image will be completely removed from the
26410 screen if the output area intersects the cursor area in
26411 y-direction. When we draw in [y0 y1[, and some part of
26412 the cursor is at y < y0, that part must have been drawn
26413 before. When scrolling, the cursor is erased before
26414 actually scrolling, so we don't come here. When not
26415 scrolling, the rows above the old cursor row must have
26416 changed, and in this case these rows must have written
26417 over the cursor image.
26418
26419 Likewise if part of the cursor is below y1, with the
26420 exception of the cursor being in the first blank row at
26421 the buffer and window end because update_text_area
26422 doesn't draw that row. (Except when it does, but
26423 that's handled in update_text_area.) */
26424
26425 cy0 = w->phys_cursor.y;
26426 cy1 = cy0 + w->phys_cursor_height;
26427 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26428 return;
26429
26430 w->phys_cursor_on_p = 0;
26431 }
26432
26433 #endif /* HAVE_WINDOW_SYSTEM */
26434
26435 \f
26436 /************************************************************************
26437 Mouse Face
26438 ************************************************************************/
26439
26440 #ifdef HAVE_WINDOW_SYSTEM
26441
26442 /* EXPORT for RIF:
26443 Fix the display of area AREA of overlapping row ROW in window W
26444 with respect to the overlapping part OVERLAPS. */
26445
26446 void
26447 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26448 enum glyph_row_area area, int overlaps)
26449 {
26450 int i, x;
26451
26452 block_input ();
26453
26454 x = 0;
26455 for (i = 0; i < row->used[area];)
26456 {
26457 if (row->glyphs[area][i].overlaps_vertically_p)
26458 {
26459 int start = i, start_x = x;
26460
26461 do
26462 {
26463 x += row->glyphs[area][i].pixel_width;
26464 ++i;
26465 }
26466 while (i < row->used[area]
26467 && row->glyphs[area][i].overlaps_vertically_p);
26468
26469 draw_glyphs (w, start_x, row, area,
26470 start, i,
26471 DRAW_NORMAL_TEXT, overlaps);
26472 }
26473 else
26474 {
26475 x += row->glyphs[area][i].pixel_width;
26476 ++i;
26477 }
26478 }
26479
26480 unblock_input ();
26481 }
26482
26483
26484 /* EXPORT:
26485 Draw the cursor glyph of window W in glyph row ROW. See the
26486 comment of draw_glyphs for the meaning of HL. */
26487
26488 void
26489 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26490 enum draw_glyphs_face hl)
26491 {
26492 /* If cursor hpos is out of bounds, don't draw garbage. This can
26493 happen in mini-buffer windows when switching between echo area
26494 glyphs and mini-buffer. */
26495 if ((row->reversed_p
26496 ? (w->phys_cursor.hpos >= 0)
26497 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26498 {
26499 int on_p = w->phys_cursor_on_p;
26500 int x1;
26501 int hpos = w->phys_cursor.hpos;
26502
26503 /* When the window is hscrolled, cursor hpos can legitimately be
26504 out of bounds, but we draw the cursor at the corresponding
26505 window margin in that case. */
26506 if (!row->reversed_p && hpos < 0)
26507 hpos = 0;
26508 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26509 hpos = row->used[TEXT_AREA] - 1;
26510
26511 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26512 hl, 0);
26513 w->phys_cursor_on_p = on_p;
26514
26515 if (hl == DRAW_CURSOR)
26516 w->phys_cursor_width = x1 - w->phys_cursor.x;
26517 /* When we erase the cursor, and ROW is overlapped by other
26518 rows, make sure that these overlapping parts of other rows
26519 are redrawn. */
26520 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26521 {
26522 w->phys_cursor_width = x1 - w->phys_cursor.x;
26523
26524 if (row > w->current_matrix->rows
26525 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26526 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26527 OVERLAPS_ERASED_CURSOR);
26528
26529 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26530 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26531 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26532 OVERLAPS_ERASED_CURSOR);
26533 }
26534 }
26535 }
26536
26537
26538 /* EXPORT:
26539 Erase the image of a cursor of window W from the screen. */
26540
26541 void
26542 erase_phys_cursor (struct window *w)
26543 {
26544 struct frame *f = XFRAME (w->frame);
26545 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26546 int hpos = w->phys_cursor.hpos;
26547 int vpos = w->phys_cursor.vpos;
26548 int mouse_face_here_p = 0;
26549 struct glyph_matrix *active_glyphs = w->current_matrix;
26550 struct glyph_row *cursor_row;
26551 struct glyph *cursor_glyph;
26552 enum draw_glyphs_face hl;
26553
26554 /* No cursor displayed or row invalidated => nothing to do on the
26555 screen. */
26556 if (w->phys_cursor_type == NO_CURSOR)
26557 goto mark_cursor_off;
26558
26559 /* VPOS >= active_glyphs->nrows means that window has been resized.
26560 Don't bother to erase the cursor. */
26561 if (vpos >= active_glyphs->nrows)
26562 goto mark_cursor_off;
26563
26564 /* If row containing cursor is marked invalid, there is nothing we
26565 can do. */
26566 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26567 if (!cursor_row->enabled_p)
26568 goto mark_cursor_off;
26569
26570 /* If line spacing is > 0, old cursor may only be partially visible in
26571 window after split-window. So adjust visible height. */
26572 cursor_row->visible_height = min (cursor_row->visible_height,
26573 window_text_bottom_y (w) - cursor_row->y);
26574
26575 /* If row is completely invisible, don't attempt to delete a cursor which
26576 isn't there. This can happen if cursor is at top of a window, and
26577 we switch to a buffer with a header line in that window. */
26578 if (cursor_row->visible_height <= 0)
26579 goto mark_cursor_off;
26580
26581 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26582 if (cursor_row->cursor_in_fringe_p)
26583 {
26584 cursor_row->cursor_in_fringe_p = 0;
26585 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26586 goto mark_cursor_off;
26587 }
26588
26589 /* This can happen when the new row is shorter than the old one.
26590 In this case, either draw_glyphs or clear_end_of_line
26591 should have cleared the cursor. Note that we wouldn't be
26592 able to erase the cursor in this case because we don't have a
26593 cursor glyph at hand. */
26594 if ((cursor_row->reversed_p
26595 ? (w->phys_cursor.hpos < 0)
26596 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26597 goto mark_cursor_off;
26598
26599 /* When the window is hscrolled, cursor hpos can legitimately be out
26600 of bounds, but we draw the cursor at the corresponding window
26601 margin in that case. */
26602 if (!cursor_row->reversed_p && hpos < 0)
26603 hpos = 0;
26604 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26605 hpos = cursor_row->used[TEXT_AREA] - 1;
26606
26607 /* If the cursor is in the mouse face area, redisplay that when
26608 we clear the cursor. */
26609 if (! NILP (hlinfo->mouse_face_window)
26610 && coords_in_mouse_face_p (w, hpos, vpos)
26611 /* Don't redraw the cursor's spot in mouse face if it is at the
26612 end of a line (on a newline). The cursor appears there, but
26613 mouse highlighting does not. */
26614 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26615 mouse_face_here_p = 1;
26616
26617 /* Maybe clear the display under the cursor. */
26618 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26619 {
26620 int x, y, left_x;
26621 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26622 int width;
26623
26624 cursor_glyph = get_phys_cursor_glyph (w);
26625 if (cursor_glyph == NULL)
26626 goto mark_cursor_off;
26627
26628 width = cursor_glyph->pixel_width;
26629 left_x = window_box_left_offset (w, TEXT_AREA);
26630 x = w->phys_cursor.x;
26631 if (x < left_x)
26632 width -= left_x - x;
26633 width = min (width, window_box_width (w, TEXT_AREA) - x);
26634 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26635 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26636
26637 if (width > 0)
26638 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26639 }
26640
26641 /* Erase the cursor by redrawing the character underneath it. */
26642 if (mouse_face_here_p)
26643 hl = DRAW_MOUSE_FACE;
26644 else
26645 hl = DRAW_NORMAL_TEXT;
26646 draw_phys_cursor_glyph (w, cursor_row, hl);
26647
26648 mark_cursor_off:
26649 w->phys_cursor_on_p = 0;
26650 w->phys_cursor_type = NO_CURSOR;
26651 }
26652
26653
26654 /* EXPORT:
26655 Display or clear cursor of window W. If ON is zero, clear the
26656 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26657 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26658
26659 void
26660 display_and_set_cursor (struct window *w, int on,
26661 int hpos, int vpos, int x, int y)
26662 {
26663 struct frame *f = XFRAME (w->frame);
26664 int new_cursor_type;
26665 int new_cursor_width;
26666 int active_cursor;
26667 struct glyph_row *glyph_row;
26668 struct glyph *glyph;
26669
26670 /* This is pointless on invisible frames, and dangerous on garbaged
26671 windows and frames; in the latter case, the frame or window may
26672 be in the midst of changing its size, and x and y may be off the
26673 window. */
26674 if (! FRAME_VISIBLE_P (f)
26675 || FRAME_GARBAGED_P (f)
26676 || vpos >= w->current_matrix->nrows
26677 || hpos >= w->current_matrix->matrix_w)
26678 return;
26679
26680 /* If cursor is off and we want it off, return quickly. */
26681 if (!on && !w->phys_cursor_on_p)
26682 return;
26683
26684 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26685 /* If cursor row is not enabled, we don't really know where to
26686 display the cursor. */
26687 if (!glyph_row->enabled_p)
26688 {
26689 w->phys_cursor_on_p = 0;
26690 return;
26691 }
26692
26693 glyph = NULL;
26694 if (!glyph_row->exact_window_width_line_p
26695 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26696 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26697
26698 eassert (input_blocked_p ());
26699
26700 /* Set new_cursor_type to the cursor we want to be displayed. */
26701 new_cursor_type = get_window_cursor_type (w, glyph,
26702 &new_cursor_width, &active_cursor);
26703
26704 /* If cursor is currently being shown and we don't want it to be or
26705 it is in the wrong place, or the cursor type is not what we want,
26706 erase it. */
26707 if (w->phys_cursor_on_p
26708 && (!on
26709 || w->phys_cursor.x != x
26710 || w->phys_cursor.y != y
26711 || new_cursor_type != w->phys_cursor_type
26712 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26713 && new_cursor_width != w->phys_cursor_width)))
26714 erase_phys_cursor (w);
26715
26716 /* Don't check phys_cursor_on_p here because that flag is only set
26717 to zero in some cases where we know that the cursor has been
26718 completely erased, to avoid the extra work of erasing the cursor
26719 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26720 still not be visible, or it has only been partly erased. */
26721 if (on)
26722 {
26723 w->phys_cursor_ascent = glyph_row->ascent;
26724 w->phys_cursor_height = glyph_row->height;
26725
26726 /* Set phys_cursor_.* before x_draw_.* is called because some
26727 of them may need the information. */
26728 w->phys_cursor.x = x;
26729 w->phys_cursor.y = glyph_row->y;
26730 w->phys_cursor.hpos = hpos;
26731 w->phys_cursor.vpos = vpos;
26732 }
26733
26734 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26735 new_cursor_type, new_cursor_width,
26736 on, active_cursor);
26737 }
26738
26739
26740 /* Switch the display of W's cursor on or off, according to the value
26741 of ON. */
26742
26743 static void
26744 update_window_cursor (struct window *w, int on)
26745 {
26746 /* Don't update cursor in windows whose frame is in the process
26747 of being deleted. */
26748 if (w->current_matrix)
26749 {
26750 int hpos = w->phys_cursor.hpos;
26751 int vpos = w->phys_cursor.vpos;
26752 struct glyph_row *row;
26753
26754 if (vpos >= w->current_matrix->nrows
26755 || hpos >= w->current_matrix->matrix_w)
26756 return;
26757
26758 row = MATRIX_ROW (w->current_matrix, vpos);
26759
26760 /* When the window is hscrolled, cursor hpos can legitimately be
26761 out of bounds, but we draw the cursor at the corresponding
26762 window margin in that case. */
26763 if (!row->reversed_p && hpos < 0)
26764 hpos = 0;
26765 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26766 hpos = row->used[TEXT_AREA] - 1;
26767
26768 block_input ();
26769 display_and_set_cursor (w, on, hpos, vpos,
26770 w->phys_cursor.x, w->phys_cursor.y);
26771 unblock_input ();
26772 }
26773 }
26774
26775
26776 /* Call update_window_cursor with parameter ON_P on all leaf windows
26777 in the window tree rooted at W. */
26778
26779 static void
26780 update_cursor_in_window_tree (struct window *w, int on_p)
26781 {
26782 while (w)
26783 {
26784 if (WINDOWP (w->contents))
26785 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26786 else
26787 update_window_cursor (w, on_p);
26788
26789 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26790 }
26791 }
26792
26793
26794 /* EXPORT:
26795 Display the cursor on window W, or clear it, according to ON_P.
26796 Don't change the cursor's position. */
26797
26798 void
26799 x_update_cursor (struct frame *f, int on_p)
26800 {
26801 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26802 }
26803
26804
26805 /* EXPORT:
26806 Clear the cursor of window W to background color, and mark the
26807 cursor as not shown. This is used when the text where the cursor
26808 is about to be rewritten. */
26809
26810 void
26811 x_clear_cursor (struct window *w)
26812 {
26813 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26814 update_window_cursor (w, 0);
26815 }
26816
26817 #endif /* HAVE_WINDOW_SYSTEM */
26818
26819 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26820 and MSDOS. */
26821 static void
26822 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26823 int start_hpos, int end_hpos,
26824 enum draw_glyphs_face draw)
26825 {
26826 #ifdef HAVE_WINDOW_SYSTEM
26827 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26828 {
26829 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26830 return;
26831 }
26832 #endif
26833 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26834 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26835 #endif
26836 }
26837
26838 /* Display the active region described by mouse_face_* according to DRAW. */
26839
26840 static void
26841 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26842 {
26843 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26844 struct frame *f = XFRAME (WINDOW_FRAME (w));
26845
26846 if (/* If window is in the process of being destroyed, don't bother
26847 to do anything. */
26848 w->current_matrix != NULL
26849 /* Don't update mouse highlight if hidden */
26850 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26851 /* Recognize when we are called to operate on rows that don't exist
26852 anymore. This can happen when a window is split. */
26853 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26854 {
26855 int phys_cursor_on_p = w->phys_cursor_on_p;
26856 struct glyph_row *row, *first, *last;
26857
26858 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26859 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26860
26861 for (row = first; row <= last && row->enabled_p; ++row)
26862 {
26863 int start_hpos, end_hpos, start_x;
26864
26865 /* For all but the first row, the highlight starts at column 0. */
26866 if (row == first)
26867 {
26868 /* R2L rows have BEG and END in reversed order, but the
26869 screen drawing geometry is always left to right. So
26870 we need to mirror the beginning and end of the
26871 highlighted area in R2L rows. */
26872 if (!row->reversed_p)
26873 {
26874 start_hpos = hlinfo->mouse_face_beg_col;
26875 start_x = hlinfo->mouse_face_beg_x;
26876 }
26877 else if (row == last)
26878 {
26879 start_hpos = hlinfo->mouse_face_end_col;
26880 start_x = hlinfo->mouse_face_end_x;
26881 }
26882 else
26883 {
26884 start_hpos = 0;
26885 start_x = 0;
26886 }
26887 }
26888 else if (row->reversed_p && row == last)
26889 {
26890 start_hpos = hlinfo->mouse_face_end_col;
26891 start_x = hlinfo->mouse_face_end_x;
26892 }
26893 else
26894 {
26895 start_hpos = 0;
26896 start_x = 0;
26897 }
26898
26899 if (row == last)
26900 {
26901 if (!row->reversed_p)
26902 end_hpos = hlinfo->mouse_face_end_col;
26903 else if (row == first)
26904 end_hpos = hlinfo->mouse_face_beg_col;
26905 else
26906 {
26907 end_hpos = row->used[TEXT_AREA];
26908 if (draw == DRAW_NORMAL_TEXT)
26909 row->fill_line_p = 1; /* Clear to end of line */
26910 }
26911 }
26912 else if (row->reversed_p && row == first)
26913 end_hpos = hlinfo->mouse_face_beg_col;
26914 else
26915 {
26916 end_hpos = row->used[TEXT_AREA];
26917 if (draw == DRAW_NORMAL_TEXT)
26918 row->fill_line_p = 1; /* Clear to end of line */
26919 }
26920
26921 if (end_hpos > start_hpos)
26922 {
26923 draw_row_with_mouse_face (w, start_x, row,
26924 start_hpos, end_hpos, draw);
26925
26926 row->mouse_face_p
26927 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26928 }
26929 }
26930
26931 #ifdef HAVE_WINDOW_SYSTEM
26932 /* When we've written over the cursor, arrange for it to
26933 be displayed again. */
26934 if (FRAME_WINDOW_P (f)
26935 && phys_cursor_on_p && !w->phys_cursor_on_p)
26936 {
26937 int hpos = w->phys_cursor.hpos;
26938
26939 /* When the window is hscrolled, cursor hpos can legitimately be
26940 out of bounds, but we draw the cursor at the corresponding
26941 window margin in that case. */
26942 if (!row->reversed_p && hpos < 0)
26943 hpos = 0;
26944 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26945 hpos = row->used[TEXT_AREA] - 1;
26946
26947 block_input ();
26948 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26949 w->phys_cursor.x, w->phys_cursor.y);
26950 unblock_input ();
26951 }
26952 #endif /* HAVE_WINDOW_SYSTEM */
26953 }
26954
26955 #ifdef HAVE_WINDOW_SYSTEM
26956 /* Change the mouse cursor. */
26957 if (FRAME_WINDOW_P (f))
26958 {
26959 if (draw == DRAW_NORMAL_TEXT
26960 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26961 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26962 else if (draw == DRAW_MOUSE_FACE)
26963 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26964 else
26965 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26966 }
26967 #endif /* HAVE_WINDOW_SYSTEM */
26968 }
26969
26970 /* EXPORT:
26971 Clear out the mouse-highlighted active region.
26972 Redraw it un-highlighted first. Value is non-zero if mouse
26973 face was actually drawn unhighlighted. */
26974
26975 int
26976 clear_mouse_face (Mouse_HLInfo *hlinfo)
26977 {
26978 int cleared = 0;
26979
26980 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26981 {
26982 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26983 cleared = 1;
26984 }
26985
26986 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26987 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26988 hlinfo->mouse_face_window = Qnil;
26989 hlinfo->mouse_face_overlay = Qnil;
26990 return cleared;
26991 }
26992
26993 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26994 within the mouse face on that window. */
26995 static int
26996 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26997 {
26998 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26999
27000 /* Quickly resolve the easy cases. */
27001 if (!(WINDOWP (hlinfo->mouse_face_window)
27002 && XWINDOW (hlinfo->mouse_face_window) == w))
27003 return 0;
27004 if (vpos < hlinfo->mouse_face_beg_row
27005 || vpos > hlinfo->mouse_face_end_row)
27006 return 0;
27007 if (vpos > hlinfo->mouse_face_beg_row
27008 && vpos < hlinfo->mouse_face_end_row)
27009 return 1;
27010
27011 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27012 {
27013 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27014 {
27015 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27016 return 1;
27017 }
27018 else if ((vpos == hlinfo->mouse_face_beg_row
27019 && hpos >= hlinfo->mouse_face_beg_col)
27020 || (vpos == hlinfo->mouse_face_end_row
27021 && hpos < hlinfo->mouse_face_end_col))
27022 return 1;
27023 }
27024 else
27025 {
27026 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27027 {
27028 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27029 return 1;
27030 }
27031 else if ((vpos == hlinfo->mouse_face_beg_row
27032 && hpos <= hlinfo->mouse_face_beg_col)
27033 || (vpos == hlinfo->mouse_face_end_row
27034 && hpos > hlinfo->mouse_face_end_col))
27035 return 1;
27036 }
27037 return 0;
27038 }
27039
27040
27041 /* EXPORT:
27042 Non-zero if physical cursor of window W is within mouse face. */
27043
27044 int
27045 cursor_in_mouse_face_p (struct window *w)
27046 {
27047 int hpos = w->phys_cursor.hpos;
27048 int vpos = w->phys_cursor.vpos;
27049 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27050
27051 /* When the window is hscrolled, cursor hpos can legitimately be out
27052 of bounds, but we draw the cursor at the corresponding window
27053 margin in that case. */
27054 if (!row->reversed_p && hpos < 0)
27055 hpos = 0;
27056 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27057 hpos = row->used[TEXT_AREA] - 1;
27058
27059 return coords_in_mouse_face_p (w, hpos, vpos);
27060 }
27061
27062
27063 \f
27064 /* Find the glyph rows START_ROW and END_ROW of window W that display
27065 characters between buffer positions START_CHARPOS and END_CHARPOS
27066 (excluding END_CHARPOS). DISP_STRING is a display string that
27067 covers these buffer positions. This is similar to
27068 row_containing_pos, but is more accurate when bidi reordering makes
27069 buffer positions change non-linearly with glyph rows. */
27070 static void
27071 rows_from_pos_range (struct window *w,
27072 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27073 Lisp_Object disp_string,
27074 struct glyph_row **start, struct glyph_row **end)
27075 {
27076 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27077 int last_y = window_text_bottom_y (w);
27078 struct glyph_row *row;
27079
27080 *start = NULL;
27081 *end = NULL;
27082
27083 while (!first->enabled_p
27084 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27085 first++;
27086
27087 /* Find the START row. */
27088 for (row = first;
27089 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27090 row++)
27091 {
27092 /* A row can potentially be the START row if the range of the
27093 characters it displays intersects the range
27094 [START_CHARPOS..END_CHARPOS). */
27095 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27096 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27097 /* See the commentary in row_containing_pos, for the
27098 explanation of the complicated way to check whether
27099 some position is beyond the end of the characters
27100 displayed by a row. */
27101 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27102 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27103 && !row->ends_at_zv_p
27104 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27105 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27106 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27107 && !row->ends_at_zv_p
27108 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27109 {
27110 /* Found a candidate row. Now make sure at least one of the
27111 glyphs it displays has a charpos from the range
27112 [START_CHARPOS..END_CHARPOS).
27113
27114 This is not obvious because bidi reordering could make
27115 buffer positions of a row be 1,2,3,102,101,100, and if we
27116 want to highlight characters in [50..60), we don't want
27117 this row, even though [50..60) does intersect [1..103),
27118 the range of character positions given by the row's start
27119 and end positions. */
27120 struct glyph *g = row->glyphs[TEXT_AREA];
27121 struct glyph *e = g + row->used[TEXT_AREA];
27122
27123 while (g < e)
27124 {
27125 if (((BUFFERP (g->object) || INTEGERP (g->object))
27126 && start_charpos <= g->charpos && g->charpos < end_charpos)
27127 /* A glyph that comes from DISP_STRING is by
27128 definition to be highlighted. */
27129 || EQ (g->object, disp_string))
27130 *start = row;
27131 g++;
27132 }
27133 if (*start)
27134 break;
27135 }
27136 }
27137
27138 /* Find the END row. */
27139 if (!*start
27140 /* If the last row is partially visible, start looking for END
27141 from that row, instead of starting from FIRST. */
27142 && !(row->enabled_p
27143 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27144 row = first;
27145 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27146 {
27147 struct glyph_row *next = row + 1;
27148 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27149
27150 if (!next->enabled_p
27151 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27152 /* The first row >= START whose range of displayed characters
27153 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27154 is the row END + 1. */
27155 || (start_charpos < next_start
27156 && end_charpos < next_start)
27157 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27158 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27159 && !next->ends_at_zv_p
27160 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27161 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27162 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27163 && !next->ends_at_zv_p
27164 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27165 {
27166 *end = row;
27167 break;
27168 }
27169 else
27170 {
27171 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27172 but none of the characters it displays are in the range, it is
27173 also END + 1. */
27174 struct glyph *g = next->glyphs[TEXT_AREA];
27175 struct glyph *s = g;
27176 struct glyph *e = g + next->used[TEXT_AREA];
27177
27178 while (g < e)
27179 {
27180 if (((BUFFERP (g->object) || INTEGERP (g->object))
27181 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27182 /* If the buffer position of the first glyph in
27183 the row is equal to END_CHARPOS, it means
27184 the last character to be highlighted is the
27185 newline of ROW, and we must consider NEXT as
27186 END, not END+1. */
27187 || (((!next->reversed_p && g == s)
27188 || (next->reversed_p && g == e - 1))
27189 && (g->charpos == end_charpos
27190 /* Special case for when NEXT is an
27191 empty line at ZV. */
27192 || (g->charpos == -1
27193 && !row->ends_at_zv_p
27194 && next_start == end_charpos)))))
27195 /* A glyph that comes from DISP_STRING is by
27196 definition to be highlighted. */
27197 || EQ (g->object, disp_string))
27198 break;
27199 g++;
27200 }
27201 if (g == e)
27202 {
27203 *end = row;
27204 break;
27205 }
27206 /* The first row that ends at ZV must be the last to be
27207 highlighted. */
27208 else if (next->ends_at_zv_p)
27209 {
27210 *end = next;
27211 break;
27212 }
27213 }
27214 }
27215 }
27216
27217 /* This function sets the mouse_face_* elements of HLINFO, assuming
27218 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27219 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27220 for the overlay or run of text properties specifying the mouse
27221 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27222 before-string and after-string that must also be highlighted.
27223 DISP_STRING, if non-nil, is a display string that may cover some
27224 or all of the highlighted text. */
27225
27226 static void
27227 mouse_face_from_buffer_pos (Lisp_Object window,
27228 Mouse_HLInfo *hlinfo,
27229 ptrdiff_t mouse_charpos,
27230 ptrdiff_t start_charpos,
27231 ptrdiff_t end_charpos,
27232 Lisp_Object before_string,
27233 Lisp_Object after_string,
27234 Lisp_Object disp_string)
27235 {
27236 struct window *w = XWINDOW (window);
27237 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27238 struct glyph_row *r1, *r2;
27239 struct glyph *glyph, *end;
27240 ptrdiff_t ignore, pos;
27241 int x;
27242
27243 eassert (NILP (disp_string) || STRINGP (disp_string));
27244 eassert (NILP (before_string) || STRINGP (before_string));
27245 eassert (NILP (after_string) || STRINGP (after_string));
27246
27247 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27248 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27249 if (r1 == NULL)
27250 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27251 /* If the before-string or display-string contains newlines,
27252 rows_from_pos_range skips to its last row. Move back. */
27253 if (!NILP (before_string) || !NILP (disp_string))
27254 {
27255 struct glyph_row *prev;
27256 while ((prev = r1 - 1, prev >= first)
27257 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27258 && prev->used[TEXT_AREA] > 0)
27259 {
27260 struct glyph *beg = prev->glyphs[TEXT_AREA];
27261 glyph = beg + prev->used[TEXT_AREA];
27262 while (--glyph >= beg && INTEGERP (glyph->object));
27263 if (glyph < beg
27264 || !(EQ (glyph->object, before_string)
27265 || EQ (glyph->object, disp_string)))
27266 break;
27267 r1 = prev;
27268 }
27269 }
27270 if (r2 == NULL)
27271 {
27272 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27273 hlinfo->mouse_face_past_end = 1;
27274 }
27275 else if (!NILP (after_string))
27276 {
27277 /* If the after-string has newlines, advance to its last row. */
27278 struct glyph_row *next;
27279 struct glyph_row *last
27280 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27281
27282 for (next = r2 + 1;
27283 next <= last
27284 && next->used[TEXT_AREA] > 0
27285 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27286 ++next)
27287 r2 = next;
27288 }
27289 /* The rest of the display engine assumes that mouse_face_beg_row is
27290 either above mouse_face_end_row or identical to it. But with
27291 bidi-reordered continued lines, the row for START_CHARPOS could
27292 be below the row for END_CHARPOS. If so, swap the rows and store
27293 them in correct order. */
27294 if (r1->y > r2->y)
27295 {
27296 struct glyph_row *tem = r2;
27297
27298 r2 = r1;
27299 r1 = tem;
27300 }
27301
27302 hlinfo->mouse_face_beg_y = r1->y;
27303 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27304 hlinfo->mouse_face_end_y = r2->y;
27305 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27306
27307 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27308 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27309 could be anywhere in the row and in any order. The strategy
27310 below is to find the leftmost and the rightmost glyph that
27311 belongs to either of these 3 strings, or whose position is
27312 between START_CHARPOS and END_CHARPOS, and highlight all the
27313 glyphs between those two. This may cover more than just the text
27314 between START_CHARPOS and END_CHARPOS if the range of characters
27315 strides the bidi level boundary, e.g. if the beginning is in R2L
27316 text while the end is in L2R text or vice versa. */
27317 if (!r1->reversed_p)
27318 {
27319 /* This row is in a left to right paragraph. Scan it left to
27320 right. */
27321 glyph = r1->glyphs[TEXT_AREA];
27322 end = glyph + r1->used[TEXT_AREA];
27323 x = r1->x;
27324
27325 /* Skip truncation glyphs at the start of the glyph row. */
27326 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27327 for (; glyph < end
27328 && INTEGERP (glyph->object)
27329 && glyph->charpos < 0;
27330 ++glyph)
27331 x += glyph->pixel_width;
27332
27333 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27334 or DISP_STRING, and the first glyph from buffer whose
27335 position is between START_CHARPOS and END_CHARPOS. */
27336 for (; glyph < end
27337 && !INTEGERP (glyph->object)
27338 && !EQ (glyph->object, disp_string)
27339 && !(BUFFERP (glyph->object)
27340 && (glyph->charpos >= start_charpos
27341 && glyph->charpos < end_charpos));
27342 ++glyph)
27343 {
27344 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27345 are present at buffer positions between START_CHARPOS and
27346 END_CHARPOS, or if they come from an overlay. */
27347 if (EQ (glyph->object, before_string))
27348 {
27349 pos = string_buffer_position (before_string,
27350 start_charpos);
27351 /* If pos == 0, it means before_string came from an
27352 overlay, not from a buffer position. */
27353 if (!pos || (pos >= start_charpos && pos < end_charpos))
27354 break;
27355 }
27356 else if (EQ (glyph->object, after_string))
27357 {
27358 pos = string_buffer_position (after_string, end_charpos);
27359 if (!pos || (pos >= start_charpos && pos < end_charpos))
27360 break;
27361 }
27362 x += glyph->pixel_width;
27363 }
27364 hlinfo->mouse_face_beg_x = x;
27365 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27366 }
27367 else
27368 {
27369 /* This row is in a right to left paragraph. Scan it right to
27370 left. */
27371 struct glyph *g;
27372
27373 end = r1->glyphs[TEXT_AREA] - 1;
27374 glyph = end + r1->used[TEXT_AREA];
27375
27376 /* Skip truncation glyphs at the start of the glyph row. */
27377 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27378 for (; glyph > end
27379 && INTEGERP (glyph->object)
27380 && glyph->charpos < 0;
27381 --glyph)
27382 ;
27383
27384 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27385 or DISP_STRING, and the first glyph from buffer whose
27386 position is between START_CHARPOS and END_CHARPOS. */
27387 for (; glyph > end
27388 && !INTEGERP (glyph->object)
27389 && !EQ (glyph->object, disp_string)
27390 && !(BUFFERP (glyph->object)
27391 && (glyph->charpos >= start_charpos
27392 && glyph->charpos < end_charpos));
27393 --glyph)
27394 {
27395 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27396 are present at buffer positions between START_CHARPOS and
27397 END_CHARPOS, or if they come from an overlay. */
27398 if (EQ (glyph->object, before_string))
27399 {
27400 pos = string_buffer_position (before_string, start_charpos);
27401 /* If pos == 0, it means before_string came from an
27402 overlay, not from a buffer position. */
27403 if (!pos || (pos >= start_charpos && pos < end_charpos))
27404 break;
27405 }
27406 else if (EQ (glyph->object, after_string))
27407 {
27408 pos = string_buffer_position (after_string, end_charpos);
27409 if (!pos || (pos >= start_charpos && pos < end_charpos))
27410 break;
27411 }
27412 }
27413
27414 glyph++; /* first glyph to the right of the highlighted area */
27415 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27416 x += g->pixel_width;
27417 hlinfo->mouse_face_beg_x = x;
27418 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27419 }
27420
27421 /* If the highlight ends in a different row, compute GLYPH and END
27422 for the end row. Otherwise, reuse the values computed above for
27423 the row where the highlight begins. */
27424 if (r2 != r1)
27425 {
27426 if (!r2->reversed_p)
27427 {
27428 glyph = r2->glyphs[TEXT_AREA];
27429 end = glyph + r2->used[TEXT_AREA];
27430 x = r2->x;
27431 }
27432 else
27433 {
27434 end = r2->glyphs[TEXT_AREA] - 1;
27435 glyph = end + r2->used[TEXT_AREA];
27436 }
27437 }
27438
27439 if (!r2->reversed_p)
27440 {
27441 /* Skip truncation and continuation glyphs near the end of the
27442 row, and also blanks and stretch glyphs inserted by
27443 extend_face_to_end_of_line. */
27444 while (end > glyph
27445 && INTEGERP ((end - 1)->object))
27446 --end;
27447 /* Scan the rest of the glyph row from the end, looking for the
27448 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27449 DISP_STRING, or whose position is between START_CHARPOS
27450 and END_CHARPOS */
27451 for (--end;
27452 end > glyph
27453 && !INTEGERP (end->object)
27454 && !EQ (end->object, disp_string)
27455 && !(BUFFERP (end->object)
27456 && (end->charpos >= start_charpos
27457 && end->charpos < end_charpos));
27458 --end)
27459 {
27460 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27461 are present at buffer positions between START_CHARPOS and
27462 END_CHARPOS, or if they come from an overlay. */
27463 if (EQ (end->object, before_string))
27464 {
27465 pos = string_buffer_position (before_string, start_charpos);
27466 if (!pos || (pos >= start_charpos && pos < end_charpos))
27467 break;
27468 }
27469 else if (EQ (end->object, after_string))
27470 {
27471 pos = string_buffer_position (after_string, end_charpos);
27472 if (!pos || (pos >= start_charpos && pos < end_charpos))
27473 break;
27474 }
27475 }
27476 /* Find the X coordinate of the last glyph to be highlighted. */
27477 for (; glyph <= end; ++glyph)
27478 x += glyph->pixel_width;
27479
27480 hlinfo->mouse_face_end_x = x;
27481 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27482 }
27483 else
27484 {
27485 /* Skip truncation and continuation glyphs near the end of the
27486 row, and also blanks and stretch glyphs inserted by
27487 extend_face_to_end_of_line. */
27488 x = r2->x;
27489 end++;
27490 while (end < glyph
27491 && INTEGERP (end->object))
27492 {
27493 x += end->pixel_width;
27494 ++end;
27495 }
27496 /* Scan the rest of the glyph row from the end, looking for the
27497 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27498 DISP_STRING, or whose position is between START_CHARPOS
27499 and END_CHARPOS */
27500 for ( ;
27501 end < glyph
27502 && !INTEGERP (end->object)
27503 && !EQ (end->object, disp_string)
27504 && !(BUFFERP (end->object)
27505 && (end->charpos >= start_charpos
27506 && end->charpos < end_charpos));
27507 ++end)
27508 {
27509 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27510 are present at buffer positions between START_CHARPOS and
27511 END_CHARPOS, or if they come from an overlay. */
27512 if (EQ (end->object, before_string))
27513 {
27514 pos = string_buffer_position (before_string, start_charpos);
27515 if (!pos || (pos >= start_charpos && pos < end_charpos))
27516 break;
27517 }
27518 else if (EQ (end->object, after_string))
27519 {
27520 pos = string_buffer_position (after_string, end_charpos);
27521 if (!pos || (pos >= start_charpos && pos < end_charpos))
27522 break;
27523 }
27524 x += end->pixel_width;
27525 }
27526 /* If we exited the above loop because we arrived at the last
27527 glyph of the row, and its buffer position is still not in
27528 range, it means the last character in range is the preceding
27529 newline. Bump the end column and x values to get past the
27530 last glyph. */
27531 if (end == glyph
27532 && BUFFERP (end->object)
27533 && (end->charpos < start_charpos
27534 || end->charpos >= end_charpos))
27535 {
27536 x += end->pixel_width;
27537 ++end;
27538 }
27539 hlinfo->mouse_face_end_x = x;
27540 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27541 }
27542
27543 hlinfo->mouse_face_window = window;
27544 hlinfo->mouse_face_face_id
27545 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27546 mouse_charpos + 1,
27547 !hlinfo->mouse_face_hidden, -1);
27548 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27549 }
27550
27551 /* The following function is not used anymore (replaced with
27552 mouse_face_from_string_pos), but I leave it here for the time
27553 being, in case someone would. */
27554
27555 #if 0 /* not used */
27556
27557 /* Find the position of the glyph for position POS in OBJECT in
27558 window W's current matrix, and return in *X, *Y the pixel
27559 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27560
27561 RIGHT_P non-zero means return the position of the right edge of the
27562 glyph, RIGHT_P zero means return the left edge position.
27563
27564 If no glyph for POS exists in the matrix, return the position of
27565 the glyph with the next smaller position that is in the matrix, if
27566 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27567 exists in the matrix, return the position of the glyph with the
27568 next larger position in OBJECT.
27569
27570 Value is non-zero if a glyph was found. */
27571
27572 static int
27573 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27574 int *hpos, int *vpos, int *x, int *y, int right_p)
27575 {
27576 int yb = window_text_bottom_y (w);
27577 struct glyph_row *r;
27578 struct glyph *best_glyph = NULL;
27579 struct glyph_row *best_row = NULL;
27580 int best_x = 0;
27581
27582 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27583 r->enabled_p && r->y < yb;
27584 ++r)
27585 {
27586 struct glyph *g = r->glyphs[TEXT_AREA];
27587 struct glyph *e = g + r->used[TEXT_AREA];
27588 int gx;
27589
27590 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27591 if (EQ (g->object, object))
27592 {
27593 if (g->charpos == pos)
27594 {
27595 best_glyph = g;
27596 best_x = gx;
27597 best_row = r;
27598 goto found;
27599 }
27600 else if (best_glyph == NULL
27601 || ((eabs (g->charpos - pos)
27602 < eabs (best_glyph->charpos - pos))
27603 && (right_p
27604 ? g->charpos < pos
27605 : g->charpos > pos)))
27606 {
27607 best_glyph = g;
27608 best_x = gx;
27609 best_row = r;
27610 }
27611 }
27612 }
27613
27614 found:
27615
27616 if (best_glyph)
27617 {
27618 *x = best_x;
27619 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27620
27621 if (right_p)
27622 {
27623 *x += best_glyph->pixel_width;
27624 ++*hpos;
27625 }
27626
27627 *y = best_row->y;
27628 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27629 }
27630
27631 return best_glyph != NULL;
27632 }
27633 #endif /* not used */
27634
27635 /* Find the positions of the first and the last glyphs in window W's
27636 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27637 (assumed to be a string), and return in HLINFO's mouse_face_*
27638 members the pixel and column/row coordinates of those glyphs. */
27639
27640 static void
27641 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27642 Lisp_Object object,
27643 ptrdiff_t startpos, ptrdiff_t endpos)
27644 {
27645 int yb = window_text_bottom_y (w);
27646 struct glyph_row *r;
27647 struct glyph *g, *e;
27648 int gx;
27649 int found = 0;
27650
27651 /* Find the glyph row with at least one position in the range
27652 [STARTPOS..ENDPOS], and the first glyph in that row whose
27653 position belongs to that range. */
27654 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27655 r->enabled_p && r->y < yb;
27656 ++r)
27657 {
27658 if (!r->reversed_p)
27659 {
27660 g = r->glyphs[TEXT_AREA];
27661 e = g + r->used[TEXT_AREA];
27662 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27663 if (EQ (g->object, object)
27664 && startpos <= g->charpos && g->charpos <= endpos)
27665 {
27666 hlinfo->mouse_face_beg_row
27667 = MATRIX_ROW_VPOS (r, w->current_matrix);
27668 hlinfo->mouse_face_beg_y = r->y;
27669 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27670 hlinfo->mouse_face_beg_x = gx;
27671 found = 1;
27672 break;
27673 }
27674 }
27675 else
27676 {
27677 struct glyph *g1;
27678
27679 e = r->glyphs[TEXT_AREA];
27680 g = e + r->used[TEXT_AREA];
27681 for ( ; g > e; --g)
27682 if (EQ ((g-1)->object, object)
27683 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27684 {
27685 hlinfo->mouse_face_beg_row
27686 = MATRIX_ROW_VPOS (r, w->current_matrix);
27687 hlinfo->mouse_face_beg_y = r->y;
27688 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27689 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27690 gx += g1->pixel_width;
27691 hlinfo->mouse_face_beg_x = gx;
27692 found = 1;
27693 break;
27694 }
27695 }
27696 if (found)
27697 break;
27698 }
27699
27700 if (!found)
27701 return;
27702
27703 /* Starting with the next row, look for the first row which does NOT
27704 include any glyphs whose positions are in the range. */
27705 for (++r; r->enabled_p && r->y < yb; ++r)
27706 {
27707 g = r->glyphs[TEXT_AREA];
27708 e = g + r->used[TEXT_AREA];
27709 found = 0;
27710 for ( ; g < e; ++g)
27711 if (EQ (g->object, object)
27712 && startpos <= g->charpos && g->charpos <= endpos)
27713 {
27714 found = 1;
27715 break;
27716 }
27717 if (!found)
27718 break;
27719 }
27720
27721 /* The highlighted region ends on the previous row. */
27722 r--;
27723
27724 /* Set the end row and its vertical pixel coordinate. */
27725 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27726 hlinfo->mouse_face_end_y = r->y;
27727
27728 /* Compute and set the end column and the end column's horizontal
27729 pixel coordinate. */
27730 if (!r->reversed_p)
27731 {
27732 g = r->glyphs[TEXT_AREA];
27733 e = g + r->used[TEXT_AREA];
27734 for ( ; e > g; --e)
27735 if (EQ ((e-1)->object, object)
27736 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27737 break;
27738 hlinfo->mouse_face_end_col = e - g;
27739
27740 for (gx = r->x; g < e; ++g)
27741 gx += g->pixel_width;
27742 hlinfo->mouse_face_end_x = gx;
27743 }
27744 else
27745 {
27746 e = r->glyphs[TEXT_AREA];
27747 g = e + r->used[TEXT_AREA];
27748 for (gx = r->x ; e < g; ++e)
27749 {
27750 if (EQ (e->object, object)
27751 && startpos <= e->charpos && e->charpos <= endpos)
27752 break;
27753 gx += e->pixel_width;
27754 }
27755 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27756 hlinfo->mouse_face_end_x = gx;
27757 }
27758 }
27759
27760 #ifdef HAVE_WINDOW_SYSTEM
27761
27762 /* See if position X, Y is within a hot-spot of an image. */
27763
27764 static int
27765 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27766 {
27767 if (!CONSP (hot_spot))
27768 return 0;
27769
27770 if (EQ (XCAR (hot_spot), Qrect))
27771 {
27772 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27773 Lisp_Object rect = XCDR (hot_spot);
27774 Lisp_Object tem;
27775 if (!CONSP (rect))
27776 return 0;
27777 if (!CONSP (XCAR (rect)))
27778 return 0;
27779 if (!CONSP (XCDR (rect)))
27780 return 0;
27781 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27782 return 0;
27783 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27784 return 0;
27785 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27786 return 0;
27787 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27788 return 0;
27789 return 1;
27790 }
27791 else if (EQ (XCAR (hot_spot), Qcircle))
27792 {
27793 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27794 Lisp_Object circ = XCDR (hot_spot);
27795 Lisp_Object lr, lx0, ly0;
27796 if (CONSP (circ)
27797 && CONSP (XCAR (circ))
27798 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27799 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27800 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27801 {
27802 double r = XFLOATINT (lr);
27803 double dx = XINT (lx0) - x;
27804 double dy = XINT (ly0) - y;
27805 return (dx * dx + dy * dy <= r * r);
27806 }
27807 }
27808 else if (EQ (XCAR (hot_spot), Qpoly))
27809 {
27810 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27811 if (VECTORP (XCDR (hot_spot)))
27812 {
27813 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27814 Lisp_Object *poly = v->contents;
27815 ptrdiff_t n = v->header.size;
27816 ptrdiff_t i;
27817 int inside = 0;
27818 Lisp_Object lx, ly;
27819 int x0, y0;
27820
27821 /* Need an even number of coordinates, and at least 3 edges. */
27822 if (n < 6 || n & 1)
27823 return 0;
27824
27825 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27826 If count is odd, we are inside polygon. Pixels on edges
27827 may or may not be included depending on actual geometry of the
27828 polygon. */
27829 if ((lx = poly[n-2], !INTEGERP (lx))
27830 || (ly = poly[n-1], !INTEGERP (lx)))
27831 return 0;
27832 x0 = XINT (lx), y0 = XINT (ly);
27833 for (i = 0; i < n; i += 2)
27834 {
27835 int x1 = x0, y1 = y0;
27836 if ((lx = poly[i], !INTEGERP (lx))
27837 || (ly = poly[i+1], !INTEGERP (ly)))
27838 return 0;
27839 x0 = XINT (lx), y0 = XINT (ly);
27840
27841 /* Does this segment cross the X line? */
27842 if (x0 >= x)
27843 {
27844 if (x1 >= x)
27845 continue;
27846 }
27847 else if (x1 < x)
27848 continue;
27849 if (y > y0 && y > y1)
27850 continue;
27851 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27852 inside = !inside;
27853 }
27854 return inside;
27855 }
27856 }
27857 return 0;
27858 }
27859
27860 Lisp_Object
27861 find_hot_spot (Lisp_Object map, int x, int y)
27862 {
27863 while (CONSP (map))
27864 {
27865 if (CONSP (XCAR (map))
27866 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27867 return XCAR (map);
27868 map = XCDR (map);
27869 }
27870
27871 return Qnil;
27872 }
27873
27874 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27875 3, 3, 0,
27876 doc: /* Lookup in image map MAP coordinates X and Y.
27877 An image map is an alist where each element has the format (AREA ID PLIST).
27878 An AREA is specified as either a rectangle, a circle, or a polygon:
27879 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27880 pixel coordinates of the upper left and bottom right corners.
27881 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27882 and the radius of the circle; r may be a float or integer.
27883 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27884 vector describes one corner in the polygon.
27885 Returns the alist element for the first matching AREA in MAP. */)
27886 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27887 {
27888 if (NILP (map))
27889 return Qnil;
27890
27891 CHECK_NUMBER (x);
27892 CHECK_NUMBER (y);
27893
27894 return find_hot_spot (map,
27895 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27896 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27897 }
27898
27899
27900 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27901 static void
27902 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27903 {
27904 /* Do not change cursor shape while dragging mouse. */
27905 if (!NILP (do_mouse_tracking))
27906 return;
27907
27908 if (!NILP (pointer))
27909 {
27910 if (EQ (pointer, Qarrow))
27911 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27912 else if (EQ (pointer, Qhand))
27913 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27914 else if (EQ (pointer, Qtext))
27915 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27916 else if (EQ (pointer, intern ("hdrag")))
27917 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27918 #ifdef HAVE_X_WINDOWS
27919 else if (EQ (pointer, intern ("vdrag")))
27920 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27921 #endif
27922 else if (EQ (pointer, intern ("hourglass")))
27923 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27924 else if (EQ (pointer, Qmodeline))
27925 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27926 else
27927 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27928 }
27929
27930 if (cursor != No_Cursor)
27931 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27932 }
27933
27934 #endif /* HAVE_WINDOW_SYSTEM */
27935
27936 /* Take proper action when mouse has moved to the mode or header line
27937 or marginal area AREA of window W, x-position X and y-position Y.
27938 X is relative to the start of the text display area of W, so the
27939 width of bitmap areas and scroll bars must be subtracted to get a
27940 position relative to the start of the mode line. */
27941
27942 static void
27943 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27944 enum window_part area)
27945 {
27946 struct window *w = XWINDOW (window);
27947 struct frame *f = XFRAME (w->frame);
27948 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27949 #ifdef HAVE_WINDOW_SYSTEM
27950 Display_Info *dpyinfo;
27951 #endif
27952 Cursor cursor = No_Cursor;
27953 Lisp_Object pointer = Qnil;
27954 int dx, dy, width, height;
27955 ptrdiff_t charpos;
27956 Lisp_Object string, object = Qnil;
27957 Lisp_Object pos IF_LINT (= Qnil), help;
27958
27959 Lisp_Object mouse_face;
27960 int original_x_pixel = x;
27961 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27962 struct glyph_row *row IF_LINT (= 0);
27963
27964 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27965 {
27966 int x0;
27967 struct glyph *end;
27968
27969 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27970 returns them in row/column units! */
27971 string = mode_line_string (w, area, &x, &y, &charpos,
27972 &object, &dx, &dy, &width, &height);
27973
27974 row = (area == ON_MODE_LINE
27975 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27976 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27977
27978 /* Find the glyph under the mouse pointer. */
27979 if (row->mode_line_p && row->enabled_p)
27980 {
27981 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27982 end = glyph + row->used[TEXT_AREA];
27983
27984 for (x0 = original_x_pixel;
27985 glyph < end && x0 >= glyph->pixel_width;
27986 ++glyph)
27987 x0 -= glyph->pixel_width;
27988
27989 if (glyph >= end)
27990 glyph = NULL;
27991 }
27992 }
27993 else
27994 {
27995 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27996 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27997 returns them in row/column units! */
27998 string = marginal_area_string (w, area, &x, &y, &charpos,
27999 &object, &dx, &dy, &width, &height);
28000 }
28001
28002 help = Qnil;
28003
28004 #ifdef HAVE_WINDOW_SYSTEM
28005 if (IMAGEP (object))
28006 {
28007 Lisp_Object image_map, hotspot;
28008 if ((image_map = Fplist_get (XCDR (object), QCmap),
28009 !NILP (image_map))
28010 && (hotspot = find_hot_spot (image_map, dx, dy),
28011 CONSP (hotspot))
28012 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28013 {
28014 Lisp_Object plist;
28015
28016 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28017 If so, we could look for mouse-enter, mouse-leave
28018 properties in PLIST (and do something...). */
28019 hotspot = XCDR (hotspot);
28020 if (CONSP (hotspot)
28021 && (plist = XCAR (hotspot), CONSP (plist)))
28022 {
28023 pointer = Fplist_get (plist, Qpointer);
28024 if (NILP (pointer))
28025 pointer = Qhand;
28026 help = Fplist_get (plist, Qhelp_echo);
28027 if (!NILP (help))
28028 {
28029 help_echo_string = help;
28030 XSETWINDOW (help_echo_window, w);
28031 help_echo_object = w->contents;
28032 help_echo_pos = charpos;
28033 }
28034 }
28035 }
28036 if (NILP (pointer))
28037 pointer = Fplist_get (XCDR (object), QCpointer);
28038 }
28039 #endif /* HAVE_WINDOW_SYSTEM */
28040
28041 if (STRINGP (string))
28042 pos = make_number (charpos);
28043
28044 /* Set the help text and mouse pointer. If the mouse is on a part
28045 of the mode line without any text (e.g. past the right edge of
28046 the mode line text), use the default help text and pointer. */
28047 if (STRINGP (string) || area == ON_MODE_LINE)
28048 {
28049 /* Arrange to display the help by setting the global variables
28050 help_echo_string, help_echo_object, and help_echo_pos. */
28051 if (NILP (help))
28052 {
28053 if (STRINGP (string))
28054 help = Fget_text_property (pos, Qhelp_echo, string);
28055
28056 if (!NILP (help))
28057 {
28058 help_echo_string = help;
28059 XSETWINDOW (help_echo_window, w);
28060 help_echo_object = string;
28061 help_echo_pos = charpos;
28062 }
28063 else if (area == ON_MODE_LINE)
28064 {
28065 Lisp_Object default_help
28066 = buffer_local_value_1 (Qmode_line_default_help_echo,
28067 w->contents);
28068
28069 if (STRINGP (default_help))
28070 {
28071 help_echo_string = default_help;
28072 XSETWINDOW (help_echo_window, w);
28073 help_echo_object = Qnil;
28074 help_echo_pos = -1;
28075 }
28076 }
28077 }
28078
28079 #ifdef HAVE_WINDOW_SYSTEM
28080 /* Change the mouse pointer according to what is under it. */
28081 if (FRAME_WINDOW_P (f))
28082 {
28083 dpyinfo = FRAME_X_DISPLAY_INFO (f);
28084 if (STRINGP (string))
28085 {
28086 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28087
28088 if (NILP (pointer))
28089 pointer = Fget_text_property (pos, Qpointer, string);
28090
28091 /* Change the mouse pointer according to what is under X/Y. */
28092 if (NILP (pointer)
28093 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28094 {
28095 Lisp_Object map;
28096 map = Fget_text_property (pos, Qlocal_map, string);
28097 if (!KEYMAPP (map))
28098 map = Fget_text_property (pos, Qkeymap, string);
28099 if (!KEYMAPP (map))
28100 cursor = dpyinfo->vertical_scroll_bar_cursor;
28101 }
28102 }
28103 else
28104 /* Default mode-line pointer. */
28105 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28106 }
28107 #endif
28108 }
28109
28110 /* Change the mouse face according to what is under X/Y. */
28111 if (STRINGP (string))
28112 {
28113 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28114 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28115 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28116 && glyph)
28117 {
28118 Lisp_Object b, e;
28119
28120 struct glyph * tmp_glyph;
28121
28122 int gpos;
28123 int gseq_length;
28124 int total_pixel_width;
28125 ptrdiff_t begpos, endpos, ignore;
28126
28127 int vpos, hpos;
28128
28129 b = Fprevious_single_property_change (make_number (charpos + 1),
28130 Qmouse_face, string, Qnil);
28131 if (NILP (b))
28132 begpos = 0;
28133 else
28134 begpos = XINT (b);
28135
28136 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28137 if (NILP (e))
28138 endpos = SCHARS (string);
28139 else
28140 endpos = XINT (e);
28141
28142 /* Calculate the glyph position GPOS of GLYPH in the
28143 displayed string, relative to the beginning of the
28144 highlighted part of the string.
28145
28146 Note: GPOS is different from CHARPOS. CHARPOS is the
28147 position of GLYPH in the internal string object. A mode
28148 line string format has structures which are converted to
28149 a flattened string by the Emacs Lisp interpreter. The
28150 internal string is an element of those structures. The
28151 displayed string is the flattened string. */
28152 tmp_glyph = row_start_glyph;
28153 while (tmp_glyph < glyph
28154 && (!(EQ (tmp_glyph->object, glyph->object)
28155 && begpos <= tmp_glyph->charpos
28156 && tmp_glyph->charpos < endpos)))
28157 tmp_glyph++;
28158 gpos = glyph - tmp_glyph;
28159
28160 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28161 the highlighted part of the displayed string to which
28162 GLYPH belongs. Note: GSEQ_LENGTH is different from
28163 SCHARS (STRING), because the latter returns the length of
28164 the internal string. */
28165 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28166 tmp_glyph > glyph
28167 && (!(EQ (tmp_glyph->object, glyph->object)
28168 && begpos <= tmp_glyph->charpos
28169 && tmp_glyph->charpos < endpos));
28170 tmp_glyph--)
28171 ;
28172 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28173
28174 /* Calculate the total pixel width of all the glyphs between
28175 the beginning of the highlighted area and GLYPH. */
28176 total_pixel_width = 0;
28177 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28178 total_pixel_width += tmp_glyph->pixel_width;
28179
28180 /* Pre calculation of re-rendering position. Note: X is in
28181 column units here, after the call to mode_line_string or
28182 marginal_area_string. */
28183 hpos = x - gpos;
28184 vpos = (area == ON_MODE_LINE
28185 ? (w->current_matrix)->nrows - 1
28186 : 0);
28187
28188 /* If GLYPH's position is included in the region that is
28189 already drawn in mouse face, we have nothing to do. */
28190 if ( EQ (window, hlinfo->mouse_face_window)
28191 && (!row->reversed_p
28192 ? (hlinfo->mouse_face_beg_col <= hpos
28193 && hpos < hlinfo->mouse_face_end_col)
28194 /* In R2L rows we swap BEG and END, see below. */
28195 : (hlinfo->mouse_face_end_col <= hpos
28196 && hpos < hlinfo->mouse_face_beg_col))
28197 && hlinfo->mouse_face_beg_row == vpos )
28198 return;
28199
28200 if (clear_mouse_face (hlinfo))
28201 cursor = No_Cursor;
28202
28203 if (!row->reversed_p)
28204 {
28205 hlinfo->mouse_face_beg_col = hpos;
28206 hlinfo->mouse_face_beg_x = original_x_pixel
28207 - (total_pixel_width + dx);
28208 hlinfo->mouse_face_end_col = hpos + gseq_length;
28209 hlinfo->mouse_face_end_x = 0;
28210 }
28211 else
28212 {
28213 /* In R2L rows, show_mouse_face expects BEG and END
28214 coordinates to be swapped. */
28215 hlinfo->mouse_face_end_col = hpos;
28216 hlinfo->mouse_face_end_x = original_x_pixel
28217 - (total_pixel_width + dx);
28218 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28219 hlinfo->mouse_face_beg_x = 0;
28220 }
28221
28222 hlinfo->mouse_face_beg_row = vpos;
28223 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28224 hlinfo->mouse_face_beg_y = 0;
28225 hlinfo->mouse_face_end_y = 0;
28226 hlinfo->mouse_face_past_end = 0;
28227 hlinfo->mouse_face_window = window;
28228
28229 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28230 charpos,
28231 0, 0, 0,
28232 &ignore,
28233 glyph->face_id,
28234 1);
28235 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28236
28237 if (NILP (pointer))
28238 pointer = Qhand;
28239 }
28240 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28241 clear_mouse_face (hlinfo);
28242 }
28243 #ifdef HAVE_WINDOW_SYSTEM
28244 if (FRAME_WINDOW_P (f))
28245 define_frame_cursor1 (f, cursor, pointer);
28246 #endif
28247 }
28248
28249
28250 /* EXPORT:
28251 Take proper action when the mouse has moved to position X, Y on
28252 frame F with regards to highlighting portions of display that have
28253 mouse-face properties. Also de-highlight portions of display where
28254 the mouse was before, set the mouse pointer shape as appropriate
28255 for the mouse coordinates, and activate help echo (tooltips).
28256 X and Y can be negative or out of range. */
28257
28258 void
28259 note_mouse_highlight (struct frame *f, int x, int y)
28260 {
28261 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28262 enum window_part part = ON_NOTHING;
28263 Lisp_Object window;
28264 struct window *w;
28265 Cursor cursor = No_Cursor;
28266 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28267 struct buffer *b;
28268
28269 /* When a menu is active, don't highlight because this looks odd. */
28270 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28271 if (popup_activated ())
28272 return;
28273 #endif
28274
28275 if (!f->glyphs_initialized_p
28276 || f->pointer_invisible)
28277 return;
28278
28279 hlinfo->mouse_face_mouse_x = x;
28280 hlinfo->mouse_face_mouse_y = y;
28281 hlinfo->mouse_face_mouse_frame = f;
28282
28283 if (hlinfo->mouse_face_defer)
28284 return;
28285
28286 /* Which window is that in? */
28287 window = window_from_coordinates (f, x, y, &part, 1);
28288
28289 /* If displaying active text in another window, clear that. */
28290 if (! EQ (window, hlinfo->mouse_face_window)
28291 /* Also clear if we move out of text area in same window. */
28292 || (!NILP (hlinfo->mouse_face_window)
28293 && !NILP (window)
28294 && part != ON_TEXT
28295 && part != ON_MODE_LINE
28296 && part != ON_HEADER_LINE))
28297 clear_mouse_face (hlinfo);
28298
28299 /* Not on a window -> return. */
28300 if (!WINDOWP (window))
28301 return;
28302
28303 /* Reset help_echo_string. It will get recomputed below. */
28304 help_echo_string = Qnil;
28305
28306 /* Convert to window-relative pixel coordinates. */
28307 w = XWINDOW (window);
28308 frame_to_window_pixel_xy (w, &x, &y);
28309
28310 #ifdef HAVE_WINDOW_SYSTEM
28311 /* Handle tool-bar window differently since it doesn't display a
28312 buffer. */
28313 if (EQ (window, f->tool_bar_window))
28314 {
28315 note_tool_bar_highlight (f, x, y);
28316 return;
28317 }
28318 #endif
28319
28320 /* Mouse is on the mode, header line or margin? */
28321 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28322 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28323 {
28324 note_mode_line_or_margin_highlight (window, x, y, part);
28325 return;
28326 }
28327
28328 #ifdef HAVE_WINDOW_SYSTEM
28329 if (part == ON_VERTICAL_BORDER)
28330 {
28331 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28332 help_echo_string = build_string ("drag-mouse-1: resize");
28333 }
28334 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28335 || part == ON_SCROLL_BAR)
28336 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28337 else
28338 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28339 #endif
28340
28341 /* Are we in a window whose display is up to date?
28342 And verify the buffer's text has not changed. */
28343 b = XBUFFER (w->contents);
28344 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28345 {
28346 int hpos, vpos, dx, dy, area = LAST_AREA;
28347 ptrdiff_t pos;
28348 struct glyph *glyph;
28349 Lisp_Object object;
28350 Lisp_Object mouse_face = Qnil, position;
28351 Lisp_Object *overlay_vec = NULL;
28352 ptrdiff_t i, noverlays;
28353 struct buffer *obuf;
28354 ptrdiff_t obegv, ozv;
28355 int same_region;
28356
28357 /* Find the glyph under X/Y. */
28358 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28359
28360 #ifdef HAVE_WINDOW_SYSTEM
28361 /* Look for :pointer property on image. */
28362 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28363 {
28364 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28365 if (img != NULL && IMAGEP (img->spec))
28366 {
28367 Lisp_Object image_map, hotspot;
28368 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28369 !NILP (image_map))
28370 && (hotspot = find_hot_spot (image_map,
28371 glyph->slice.img.x + dx,
28372 glyph->slice.img.y + dy),
28373 CONSP (hotspot))
28374 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28375 {
28376 Lisp_Object plist;
28377
28378 /* Could check XCAR (hotspot) to see if we enter/leave
28379 this hot-spot.
28380 If so, we could look for mouse-enter, mouse-leave
28381 properties in PLIST (and do something...). */
28382 hotspot = XCDR (hotspot);
28383 if (CONSP (hotspot)
28384 && (plist = XCAR (hotspot), CONSP (plist)))
28385 {
28386 pointer = Fplist_get (plist, Qpointer);
28387 if (NILP (pointer))
28388 pointer = Qhand;
28389 help_echo_string = Fplist_get (plist, Qhelp_echo);
28390 if (!NILP (help_echo_string))
28391 {
28392 help_echo_window = window;
28393 help_echo_object = glyph->object;
28394 help_echo_pos = glyph->charpos;
28395 }
28396 }
28397 }
28398 if (NILP (pointer))
28399 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28400 }
28401 }
28402 #endif /* HAVE_WINDOW_SYSTEM */
28403
28404 /* Clear mouse face if X/Y not over text. */
28405 if (glyph == NULL
28406 || area != TEXT_AREA
28407 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28408 /* Glyph's OBJECT is an integer for glyphs inserted by the
28409 display engine for its internal purposes, like truncation
28410 and continuation glyphs and blanks beyond the end of
28411 line's text on text terminals. If we are over such a
28412 glyph, we are not over any text. */
28413 || INTEGERP (glyph->object)
28414 /* R2L rows have a stretch glyph at their front, which
28415 stands for no text, whereas L2R rows have no glyphs at
28416 all beyond the end of text. Treat such stretch glyphs
28417 like we do with NULL glyphs in L2R rows. */
28418 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28419 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28420 && glyph->type == STRETCH_GLYPH
28421 && glyph->avoid_cursor_p))
28422 {
28423 if (clear_mouse_face (hlinfo))
28424 cursor = No_Cursor;
28425 #ifdef HAVE_WINDOW_SYSTEM
28426 if (FRAME_WINDOW_P (f) && NILP (pointer))
28427 {
28428 if (area != TEXT_AREA)
28429 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28430 else
28431 pointer = Vvoid_text_area_pointer;
28432 }
28433 #endif
28434 goto set_cursor;
28435 }
28436
28437 pos = glyph->charpos;
28438 object = glyph->object;
28439 if (!STRINGP (object) && !BUFFERP (object))
28440 goto set_cursor;
28441
28442 /* If we get an out-of-range value, return now; avoid an error. */
28443 if (BUFFERP (object) && pos > BUF_Z (b))
28444 goto set_cursor;
28445
28446 /* Make the window's buffer temporarily current for
28447 overlays_at and compute_char_face. */
28448 obuf = current_buffer;
28449 current_buffer = b;
28450 obegv = BEGV;
28451 ozv = ZV;
28452 BEGV = BEG;
28453 ZV = Z;
28454
28455 /* Is this char mouse-active or does it have help-echo? */
28456 position = make_number (pos);
28457
28458 if (BUFFERP (object))
28459 {
28460 /* Put all the overlays we want in a vector in overlay_vec. */
28461 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28462 /* Sort overlays into increasing priority order. */
28463 noverlays = sort_overlays (overlay_vec, noverlays, w);
28464 }
28465 else
28466 noverlays = 0;
28467
28468 if (NILP (Vmouse_highlight))
28469 {
28470 clear_mouse_face (hlinfo);
28471 goto check_help_echo;
28472 }
28473
28474 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28475
28476 if (same_region)
28477 cursor = No_Cursor;
28478
28479 /* Check mouse-face highlighting. */
28480 if (! same_region
28481 /* If there exists an overlay with mouse-face overlapping
28482 the one we are currently highlighting, we have to
28483 check if we enter the overlapping overlay, and then
28484 highlight only that. */
28485 || (OVERLAYP (hlinfo->mouse_face_overlay)
28486 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28487 {
28488 /* Find the highest priority overlay with a mouse-face. */
28489 Lisp_Object overlay = Qnil;
28490 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28491 {
28492 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28493 if (!NILP (mouse_face))
28494 overlay = overlay_vec[i];
28495 }
28496
28497 /* If we're highlighting the same overlay as before, there's
28498 no need to do that again. */
28499 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28500 goto check_help_echo;
28501 hlinfo->mouse_face_overlay = overlay;
28502
28503 /* Clear the display of the old active region, if any. */
28504 if (clear_mouse_face (hlinfo))
28505 cursor = No_Cursor;
28506
28507 /* If no overlay applies, get a text property. */
28508 if (NILP (overlay))
28509 mouse_face = Fget_text_property (position, Qmouse_face, object);
28510
28511 /* Next, compute the bounds of the mouse highlighting and
28512 display it. */
28513 if (!NILP (mouse_face) && STRINGP (object))
28514 {
28515 /* The mouse-highlighting comes from a display string
28516 with a mouse-face. */
28517 Lisp_Object s, e;
28518 ptrdiff_t ignore;
28519
28520 s = Fprevious_single_property_change
28521 (make_number (pos + 1), Qmouse_face, object, Qnil);
28522 e = Fnext_single_property_change
28523 (position, Qmouse_face, object, Qnil);
28524 if (NILP (s))
28525 s = make_number (0);
28526 if (NILP (e))
28527 e = make_number (SCHARS (object) - 1);
28528 mouse_face_from_string_pos (w, hlinfo, object,
28529 XINT (s), XINT (e));
28530 hlinfo->mouse_face_past_end = 0;
28531 hlinfo->mouse_face_window = window;
28532 hlinfo->mouse_face_face_id
28533 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28534 glyph->face_id, 1);
28535 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28536 cursor = No_Cursor;
28537 }
28538 else
28539 {
28540 /* The mouse-highlighting, if any, comes from an overlay
28541 or text property in the buffer. */
28542 Lisp_Object buffer IF_LINT (= Qnil);
28543 Lisp_Object disp_string IF_LINT (= Qnil);
28544
28545 if (STRINGP (object))
28546 {
28547 /* If we are on a display string with no mouse-face,
28548 check if the text under it has one. */
28549 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28550 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28551 pos = string_buffer_position (object, start);
28552 if (pos > 0)
28553 {
28554 mouse_face = get_char_property_and_overlay
28555 (make_number (pos), Qmouse_face, w->contents, &overlay);
28556 buffer = w->contents;
28557 disp_string = object;
28558 }
28559 }
28560 else
28561 {
28562 buffer = object;
28563 disp_string = Qnil;
28564 }
28565
28566 if (!NILP (mouse_face))
28567 {
28568 Lisp_Object before, after;
28569 Lisp_Object before_string, after_string;
28570 /* To correctly find the limits of mouse highlight
28571 in a bidi-reordered buffer, we must not use the
28572 optimization of limiting the search in
28573 previous-single-property-change and
28574 next-single-property-change, because
28575 rows_from_pos_range needs the real start and end
28576 positions to DTRT in this case. That's because
28577 the first row visible in a window does not
28578 necessarily display the character whose position
28579 is the smallest. */
28580 Lisp_Object lim1 =
28581 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28582 ? Fmarker_position (w->start)
28583 : Qnil;
28584 Lisp_Object lim2 =
28585 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28586 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28587 : Qnil;
28588
28589 if (NILP (overlay))
28590 {
28591 /* Handle the text property case. */
28592 before = Fprevious_single_property_change
28593 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28594 after = Fnext_single_property_change
28595 (make_number (pos), Qmouse_face, buffer, lim2);
28596 before_string = after_string = Qnil;
28597 }
28598 else
28599 {
28600 /* Handle the overlay case. */
28601 before = Foverlay_start (overlay);
28602 after = Foverlay_end (overlay);
28603 before_string = Foverlay_get (overlay, Qbefore_string);
28604 after_string = Foverlay_get (overlay, Qafter_string);
28605
28606 if (!STRINGP (before_string)) before_string = Qnil;
28607 if (!STRINGP (after_string)) after_string = Qnil;
28608 }
28609
28610 mouse_face_from_buffer_pos (window, hlinfo, pos,
28611 NILP (before)
28612 ? 1
28613 : XFASTINT (before),
28614 NILP (after)
28615 ? BUF_Z (XBUFFER (buffer))
28616 : XFASTINT (after),
28617 before_string, after_string,
28618 disp_string);
28619 cursor = No_Cursor;
28620 }
28621 }
28622 }
28623
28624 check_help_echo:
28625
28626 /* Look for a `help-echo' property. */
28627 if (NILP (help_echo_string)) {
28628 Lisp_Object help, overlay;
28629
28630 /* Check overlays first. */
28631 help = overlay = Qnil;
28632 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28633 {
28634 overlay = overlay_vec[i];
28635 help = Foverlay_get (overlay, Qhelp_echo);
28636 }
28637
28638 if (!NILP (help))
28639 {
28640 help_echo_string = help;
28641 help_echo_window = window;
28642 help_echo_object = overlay;
28643 help_echo_pos = pos;
28644 }
28645 else
28646 {
28647 Lisp_Object obj = glyph->object;
28648 ptrdiff_t charpos = glyph->charpos;
28649
28650 /* Try text properties. */
28651 if (STRINGP (obj)
28652 && charpos >= 0
28653 && charpos < SCHARS (obj))
28654 {
28655 help = Fget_text_property (make_number (charpos),
28656 Qhelp_echo, obj);
28657 if (NILP (help))
28658 {
28659 /* If the string itself doesn't specify a help-echo,
28660 see if the buffer text ``under'' it does. */
28661 struct glyph_row *r
28662 = MATRIX_ROW (w->current_matrix, vpos);
28663 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28664 ptrdiff_t p = string_buffer_position (obj, start);
28665 if (p > 0)
28666 {
28667 help = Fget_char_property (make_number (p),
28668 Qhelp_echo, w->contents);
28669 if (!NILP (help))
28670 {
28671 charpos = p;
28672 obj = w->contents;
28673 }
28674 }
28675 }
28676 }
28677 else if (BUFFERP (obj)
28678 && charpos >= BEGV
28679 && charpos < ZV)
28680 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28681 obj);
28682
28683 if (!NILP (help))
28684 {
28685 help_echo_string = help;
28686 help_echo_window = window;
28687 help_echo_object = obj;
28688 help_echo_pos = charpos;
28689 }
28690 }
28691 }
28692
28693 #ifdef HAVE_WINDOW_SYSTEM
28694 /* Look for a `pointer' property. */
28695 if (FRAME_WINDOW_P (f) && NILP (pointer))
28696 {
28697 /* Check overlays first. */
28698 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28699 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28700
28701 if (NILP (pointer))
28702 {
28703 Lisp_Object obj = glyph->object;
28704 ptrdiff_t charpos = glyph->charpos;
28705
28706 /* Try text properties. */
28707 if (STRINGP (obj)
28708 && charpos >= 0
28709 && charpos < SCHARS (obj))
28710 {
28711 pointer = Fget_text_property (make_number (charpos),
28712 Qpointer, obj);
28713 if (NILP (pointer))
28714 {
28715 /* If the string itself doesn't specify a pointer,
28716 see if the buffer text ``under'' it does. */
28717 struct glyph_row *r
28718 = MATRIX_ROW (w->current_matrix, vpos);
28719 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28720 ptrdiff_t p = string_buffer_position (obj, start);
28721 if (p > 0)
28722 pointer = Fget_char_property (make_number (p),
28723 Qpointer, w->contents);
28724 }
28725 }
28726 else if (BUFFERP (obj)
28727 && charpos >= BEGV
28728 && charpos < ZV)
28729 pointer = Fget_text_property (make_number (charpos),
28730 Qpointer, obj);
28731 }
28732 }
28733 #endif /* HAVE_WINDOW_SYSTEM */
28734
28735 BEGV = obegv;
28736 ZV = ozv;
28737 current_buffer = obuf;
28738 }
28739
28740 set_cursor:
28741
28742 #ifdef HAVE_WINDOW_SYSTEM
28743 if (FRAME_WINDOW_P (f))
28744 define_frame_cursor1 (f, cursor, pointer);
28745 #else
28746 /* This is here to prevent a compiler error, about "label at end of
28747 compound statement". */
28748 return;
28749 #endif
28750 }
28751
28752
28753 /* EXPORT for RIF:
28754 Clear any mouse-face on window W. This function is part of the
28755 redisplay interface, and is called from try_window_id and similar
28756 functions to ensure the mouse-highlight is off. */
28757
28758 void
28759 x_clear_window_mouse_face (struct window *w)
28760 {
28761 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28762 Lisp_Object window;
28763
28764 block_input ();
28765 XSETWINDOW (window, w);
28766 if (EQ (window, hlinfo->mouse_face_window))
28767 clear_mouse_face (hlinfo);
28768 unblock_input ();
28769 }
28770
28771
28772 /* EXPORT:
28773 Just discard the mouse face information for frame F, if any.
28774 This is used when the size of F is changed. */
28775
28776 void
28777 cancel_mouse_face (struct frame *f)
28778 {
28779 Lisp_Object window;
28780 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28781
28782 window = hlinfo->mouse_face_window;
28783 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28784 {
28785 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28786 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28787 hlinfo->mouse_face_window = Qnil;
28788 }
28789 }
28790
28791
28792 \f
28793 /***********************************************************************
28794 Exposure Events
28795 ***********************************************************************/
28796
28797 #ifdef HAVE_WINDOW_SYSTEM
28798
28799 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28800 which intersects rectangle R. R is in window-relative coordinates. */
28801
28802 static void
28803 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28804 enum glyph_row_area area)
28805 {
28806 struct glyph *first = row->glyphs[area];
28807 struct glyph *end = row->glyphs[area] + row->used[area];
28808 struct glyph *last;
28809 int first_x, start_x, x;
28810
28811 if (area == TEXT_AREA && row->fill_line_p)
28812 /* If row extends face to end of line write the whole line. */
28813 draw_glyphs (w, 0, row, area,
28814 0, row->used[area],
28815 DRAW_NORMAL_TEXT, 0);
28816 else
28817 {
28818 /* Set START_X to the window-relative start position for drawing glyphs of
28819 AREA. The first glyph of the text area can be partially visible.
28820 The first glyphs of other areas cannot. */
28821 start_x = window_box_left_offset (w, area);
28822 x = start_x;
28823 if (area == TEXT_AREA)
28824 x += row->x;
28825
28826 /* Find the first glyph that must be redrawn. */
28827 while (first < end
28828 && x + first->pixel_width < r->x)
28829 {
28830 x += first->pixel_width;
28831 ++first;
28832 }
28833
28834 /* Find the last one. */
28835 last = first;
28836 first_x = x;
28837 while (last < end
28838 && x < r->x + r->width)
28839 {
28840 x += last->pixel_width;
28841 ++last;
28842 }
28843
28844 /* Repaint. */
28845 if (last > first)
28846 draw_glyphs (w, first_x - start_x, row, area,
28847 first - row->glyphs[area], last - row->glyphs[area],
28848 DRAW_NORMAL_TEXT, 0);
28849 }
28850 }
28851
28852
28853 /* Redraw the parts of the glyph row ROW on window W intersecting
28854 rectangle R. R is in window-relative coordinates. Value is
28855 non-zero if mouse-face was overwritten. */
28856
28857 static int
28858 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28859 {
28860 eassert (row->enabled_p);
28861
28862 if (row->mode_line_p || w->pseudo_window_p)
28863 draw_glyphs (w, 0, row, TEXT_AREA,
28864 0, row->used[TEXT_AREA],
28865 DRAW_NORMAL_TEXT, 0);
28866 else
28867 {
28868 if (row->used[LEFT_MARGIN_AREA])
28869 expose_area (w, row, r, LEFT_MARGIN_AREA);
28870 if (row->used[TEXT_AREA])
28871 expose_area (w, row, r, TEXT_AREA);
28872 if (row->used[RIGHT_MARGIN_AREA])
28873 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28874 draw_row_fringe_bitmaps (w, row);
28875 }
28876
28877 return row->mouse_face_p;
28878 }
28879
28880
28881 /* Redraw those parts of glyphs rows during expose event handling that
28882 overlap other rows. Redrawing of an exposed line writes over parts
28883 of lines overlapping that exposed line; this function fixes that.
28884
28885 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28886 row in W's current matrix that is exposed and overlaps other rows.
28887 LAST_OVERLAPPING_ROW is the last such row. */
28888
28889 static void
28890 expose_overlaps (struct window *w,
28891 struct glyph_row *first_overlapping_row,
28892 struct glyph_row *last_overlapping_row,
28893 XRectangle *r)
28894 {
28895 struct glyph_row *row;
28896
28897 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28898 if (row->overlapping_p)
28899 {
28900 eassert (row->enabled_p && !row->mode_line_p);
28901
28902 row->clip = r;
28903 if (row->used[LEFT_MARGIN_AREA])
28904 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28905
28906 if (row->used[TEXT_AREA])
28907 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28908
28909 if (row->used[RIGHT_MARGIN_AREA])
28910 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28911 row->clip = NULL;
28912 }
28913 }
28914
28915
28916 /* Return non-zero if W's cursor intersects rectangle R. */
28917
28918 static int
28919 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28920 {
28921 XRectangle cr, result;
28922 struct glyph *cursor_glyph;
28923 struct glyph_row *row;
28924
28925 if (w->phys_cursor.vpos >= 0
28926 && w->phys_cursor.vpos < w->current_matrix->nrows
28927 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28928 row->enabled_p)
28929 && row->cursor_in_fringe_p)
28930 {
28931 /* Cursor is in the fringe. */
28932 cr.x = window_box_right_offset (w,
28933 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28934 ? RIGHT_MARGIN_AREA
28935 : TEXT_AREA));
28936 cr.y = row->y;
28937 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28938 cr.height = row->height;
28939 return x_intersect_rectangles (&cr, r, &result);
28940 }
28941
28942 cursor_glyph = get_phys_cursor_glyph (w);
28943 if (cursor_glyph)
28944 {
28945 /* r is relative to W's box, but w->phys_cursor.x is relative
28946 to left edge of W's TEXT area. Adjust it. */
28947 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28948 cr.y = w->phys_cursor.y;
28949 cr.width = cursor_glyph->pixel_width;
28950 cr.height = w->phys_cursor_height;
28951 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28952 I assume the effect is the same -- and this is portable. */
28953 return x_intersect_rectangles (&cr, r, &result);
28954 }
28955 /* If we don't understand the format, pretend we're not in the hot-spot. */
28956 return 0;
28957 }
28958
28959
28960 /* EXPORT:
28961 Draw a vertical window border to the right of window W if W doesn't
28962 have vertical scroll bars. */
28963
28964 void
28965 x_draw_vertical_border (struct window *w)
28966 {
28967 struct frame *f = XFRAME (WINDOW_FRAME (w));
28968
28969 /* We could do better, if we knew what type of scroll-bar the adjacent
28970 windows (on either side) have... But we don't :-(
28971 However, I think this works ok. ++KFS 2003-04-25 */
28972
28973 /* Redraw borders between horizontally adjacent windows. Don't
28974 do it for frames with vertical scroll bars because either the
28975 right scroll bar of a window, or the left scroll bar of its
28976 neighbor will suffice as a border. */
28977 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28978 return;
28979
28980 /* Note: It is necessary to redraw both the left and the right
28981 borders, for when only this single window W is being
28982 redisplayed. */
28983 if (!WINDOW_RIGHTMOST_P (w)
28984 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28985 {
28986 int x0, x1, y0, y1;
28987
28988 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28989 y1 -= 1;
28990
28991 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28992 x1 -= 1;
28993
28994 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28995 }
28996 if (!WINDOW_LEFTMOST_P (w)
28997 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28998 {
28999 int x0, x1, y0, y1;
29000
29001 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
29002 y1 -= 1;
29003
29004 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29005 x0 -= 1;
29006
29007 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29008 }
29009 }
29010
29011
29012 /* Redraw the part of window W intersection rectangle FR. Pixel
29013 coordinates in FR are frame-relative. Call this function with
29014 input blocked. Value is non-zero if the exposure overwrites
29015 mouse-face. */
29016
29017 static int
29018 expose_window (struct window *w, XRectangle *fr)
29019 {
29020 struct frame *f = XFRAME (w->frame);
29021 XRectangle wr, r;
29022 int mouse_face_overwritten_p = 0;
29023
29024 /* If window is not yet fully initialized, do nothing. This can
29025 happen when toolkit scroll bars are used and a window is split.
29026 Reconfiguring the scroll bar will generate an expose for a newly
29027 created window. */
29028 if (w->current_matrix == NULL)
29029 return 0;
29030
29031 /* When we're currently updating the window, display and current
29032 matrix usually don't agree. Arrange for a thorough display
29033 later. */
29034 if (w->must_be_updated_p)
29035 {
29036 SET_FRAME_GARBAGED (f);
29037 return 0;
29038 }
29039
29040 /* Frame-relative pixel rectangle of W. */
29041 wr.x = WINDOW_LEFT_EDGE_X (w);
29042 wr.y = WINDOW_TOP_EDGE_Y (w);
29043 wr.width = WINDOW_TOTAL_WIDTH (w);
29044 wr.height = WINDOW_TOTAL_HEIGHT (w);
29045
29046 if (x_intersect_rectangles (fr, &wr, &r))
29047 {
29048 int yb = window_text_bottom_y (w);
29049 struct glyph_row *row;
29050 int cursor_cleared_p, phys_cursor_on_p;
29051 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29052
29053 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29054 r.x, r.y, r.width, r.height));
29055
29056 /* Convert to window coordinates. */
29057 r.x -= WINDOW_LEFT_EDGE_X (w);
29058 r.y -= WINDOW_TOP_EDGE_Y (w);
29059
29060 /* Turn off the cursor. */
29061 if (!w->pseudo_window_p
29062 && phys_cursor_in_rect_p (w, &r))
29063 {
29064 x_clear_cursor (w);
29065 cursor_cleared_p = 1;
29066 }
29067 else
29068 cursor_cleared_p = 0;
29069
29070 /* If the row containing the cursor extends face to end of line,
29071 then expose_area might overwrite the cursor outside the
29072 rectangle and thus notice_overwritten_cursor might clear
29073 w->phys_cursor_on_p. We remember the original value and
29074 check later if it is changed. */
29075 phys_cursor_on_p = w->phys_cursor_on_p;
29076
29077 /* Update lines intersecting rectangle R. */
29078 first_overlapping_row = last_overlapping_row = NULL;
29079 for (row = w->current_matrix->rows;
29080 row->enabled_p;
29081 ++row)
29082 {
29083 int y0 = row->y;
29084 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29085
29086 if ((y0 >= r.y && y0 < r.y + r.height)
29087 || (y1 > r.y && y1 < r.y + r.height)
29088 || (r.y >= y0 && r.y < y1)
29089 || (r.y + r.height > y0 && r.y + r.height < y1))
29090 {
29091 /* A header line may be overlapping, but there is no need
29092 to fix overlapping areas for them. KFS 2005-02-12 */
29093 if (row->overlapping_p && !row->mode_line_p)
29094 {
29095 if (first_overlapping_row == NULL)
29096 first_overlapping_row = row;
29097 last_overlapping_row = row;
29098 }
29099
29100 row->clip = fr;
29101 if (expose_line (w, row, &r))
29102 mouse_face_overwritten_p = 1;
29103 row->clip = NULL;
29104 }
29105 else if (row->overlapping_p)
29106 {
29107 /* We must redraw a row overlapping the exposed area. */
29108 if (y0 < r.y
29109 ? y0 + row->phys_height > r.y
29110 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29111 {
29112 if (first_overlapping_row == NULL)
29113 first_overlapping_row = row;
29114 last_overlapping_row = row;
29115 }
29116 }
29117
29118 if (y1 >= yb)
29119 break;
29120 }
29121
29122 /* Display the mode line if there is one. */
29123 if (WINDOW_WANTS_MODELINE_P (w)
29124 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29125 row->enabled_p)
29126 && row->y < r.y + r.height)
29127 {
29128 if (expose_line (w, row, &r))
29129 mouse_face_overwritten_p = 1;
29130 }
29131
29132 if (!w->pseudo_window_p)
29133 {
29134 /* Fix the display of overlapping rows. */
29135 if (first_overlapping_row)
29136 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29137 fr);
29138
29139 /* Draw border between windows. */
29140 x_draw_vertical_border (w);
29141
29142 /* Turn the cursor on again. */
29143 if (cursor_cleared_p
29144 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29145 update_window_cursor (w, 1);
29146 }
29147 }
29148
29149 return mouse_face_overwritten_p;
29150 }
29151
29152
29153
29154 /* Redraw (parts) of all windows in the window tree rooted at W that
29155 intersect R. R contains frame pixel coordinates. Value is
29156 non-zero if the exposure overwrites mouse-face. */
29157
29158 static int
29159 expose_window_tree (struct window *w, XRectangle *r)
29160 {
29161 struct frame *f = XFRAME (w->frame);
29162 int mouse_face_overwritten_p = 0;
29163
29164 while (w && !FRAME_GARBAGED_P (f))
29165 {
29166 if (WINDOWP (w->contents))
29167 mouse_face_overwritten_p
29168 |= expose_window_tree (XWINDOW (w->contents), r);
29169 else
29170 mouse_face_overwritten_p |= expose_window (w, r);
29171
29172 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29173 }
29174
29175 return mouse_face_overwritten_p;
29176 }
29177
29178
29179 /* EXPORT:
29180 Redisplay an exposed area of frame F. X and Y are the upper-left
29181 corner of the exposed rectangle. W and H are width and height of
29182 the exposed area. All are pixel values. W or H zero means redraw
29183 the entire frame. */
29184
29185 void
29186 expose_frame (struct frame *f, int x, int y, int w, int h)
29187 {
29188 XRectangle r;
29189 int mouse_face_overwritten_p = 0;
29190
29191 TRACE ((stderr, "expose_frame "));
29192
29193 /* No need to redraw if frame will be redrawn soon. */
29194 if (FRAME_GARBAGED_P (f))
29195 {
29196 TRACE ((stderr, " garbaged\n"));
29197 return;
29198 }
29199
29200 /* If basic faces haven't been realized yet, there is no point in
29201 trying to redraw anything. This can happen when we get an expose
29202 event while Emacs is starting, e.g. by moving another window. */
29203 if (FRAME_FACE_CACHE (f) == NULL
29204 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29205 {
29206 TRACE ((stderr, " no faces\n"));
29207 return;
29208 }
29209
29210 if (w == 0 || h == 0)
29211 {
29212 r.x = r.y = 0;
29213 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29214 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29215 }
29216 else
29217 {
29218 r.x = x;
29219 r.y = y;
29220 r.width = w;
29221 r.height = h;
29222 }
29223
29224 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29225 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29226
29227 if (WINDOWP (f->tool_bar_window))
29228 mouse_face_overwritten_p
29229 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29230
29231 #ifdef HAVE_X_WINDOWS
29232 #ifndef MSDOS
29233 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29234 if (WINDOWP (f->menu_bar_window))
29235 mouse_face_overwritten_p
29236 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29237 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29238 #endif
29239 #endif
29240
29241 /* Some window managers support a focus-follows-mouse style with
29242 delayed raising of frames. Imagine a partially obscured frame,
29243 and moving the mouse into partially obscured mouse-face on that
29244 frame. The visible part of the mouse-face will be highlighted,
29245 then the WM raises the obscured frame. With at least one WM, KDE
29246 2.1, Emacs is not getting any event for the raising of the frame
29247 (even tried with SubstructureRedirectMask), only Expose events.
29248 These expose events will draw text normally, i.e. not
29249 highlighted. Which means we must redo the highlight here.
29250 Subsume it under ``we love X''. --gerd 2001-08-15 */
29251 /* Included in Windows version because Windows most likely does not
29252 do the right thing if any third party tool offers
29253 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29254 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29255 {
29256 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29257 if (f == hlinfo->mouse_face_mouse_frame)
29258 {
29259 int mouse_x = hlinfo->mouse_face_mouse_x;
29260 int mouse_y = hlinfo->mouse_face_mouse_y;
29261 clear_mouse_face (hlinfo);
29262 note_mouse_highlight (f, mouse_x, mouse_y);
29263 }
29264 }
29265 }
29266
29267
29268 /* EXPORT:
29269 Determine the intersection of two rectangles R1 and R2. Return
29270 the intersection in *RESULT. Value is non-zero if RESULT is not
29271 empty. */
29272
29273 int
29274 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29275 {
29276 XRectangle *left, *right;
29277 XRectangle *upper, *lower;
29278 int intersection_p = 0;
29279
29280 /* Rearrange so that R1 is the left-most rectangle. */
29281 if (r1->x < r2->x)
29282 left = r1, right = r2;
29283 else
29284 left = r2, right = r1;
29285
29286 /* X0 of the intersection is right.x0, if this is inside R1,
29287 otherwise there is no intersection. */
29288 if (right->x <= left->x + left->width)
29289 {
29290 result->x = right->x;
29291
29292 /* The right end of the intersection is the minimum of
29293 the right ends of left and right. */
29294 result->width = (min (left->x + left->width, right->x + right->width)
29295 - result->x);
29296
29297 /* Same game for Y. */
29298 if (r1->y < r2->y)
29299 upper = r1, lower = r2;
29300 else
29301 upper = r2, lower = r1;
29302
29303 /* The upper end of the intersection is lower.y0, if this is inside
29304 of upper. Otherwise, there is no intersection. */
29305 if (lower->y <= upper->y + upper->height)
29306 {
29307 result->y = lower->y;
29308
29309 /* The lower end of the intersection is the minimum of the lower
29310 ends of upper and lower. */
29311 result->height = (min (lower->y + lower->height,
29312 upper->y + upper->height)
29313 - result->y);
29314 intersection_p = 1;
29315 }
29316 }
29317
29318 return intersection_p;
29319 }
29320
29321 #endif /* HAVE_WINDOW_SYSTEM */
29322
29323 \f
29324 /***********************************************************************
29325 Initialization
29326 ***********************************************************************/
29327
29328 void
29329 syms_of_xdisp (void)
29330 {
29331 Vwith_echo_area_save_vector = Qnil;
29332 staticpro (&Vwith_echo_area_save_vector);
29333
29334 Vmessage_stack = Qnil;
29335 staticpro (&Vmessage_stack);
29336
29337 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29338 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29339
29340 message_dolog_marker1 = Fmake_marker ();
29341 staticpro (&message_dolog_marker1);
29342 message_dolog_marker2 = Fmake_marker ();
29343 staticpro (&message_dolog_marker2);
29344 message_dolog_marker3 = Fmake_marker ();
29345 staticpro (&message_dolog_marker3);
29346
29347 #ifdef GLYPH_DEBUG
29348 defsubr (&Sdump_frame_glyph_matrix);
29349 defsubr (&Sdump_glyph_matrix);
29350 defsubr (&Sdump_glyph_row);
29351 defsubr (&Sdump_tool_bar_row);
29352 defsubr (&Strace_redisplay);
29353 defsubr (&Strace_to_stderr);
29354 #endif
29355 #ifdef HAVE_WINDOW_SYSTEM
29356 defsubr (&Stool_bar_lines_needed);
29357 defsubr (&Slookup_image_map);
29358 #endif
29359 defsubr (&Sline_pixel_height);
29360 defsubr (&Sformat_mode_line);
29361 defsubr (&Sinvisible_p);
29362 defsubr (&Scurrent_bidi_paragraph_direction);
29363 defsubr (&Smove_point_visually);
29364
29365 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29366 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29367 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29368 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29369 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29370 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29371 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29372 DEFSYM (Qeval, "eval");
29373 DEFSYM (QCdata, ":data");
29374 DEFSYM (Qdisplay, "display");
29375 DEFSYM (Qspace_width, "space-width");
29376 DEFSYM (Qraise, "raise");
29377 DEFSYM (Qslice, "slice");
29378 DEFSYM (Qspace, "space");
29379 DEFSYM (Qmargin, "margin");
29380 DEFSYM (Qpointer, "pointer");
29381 DEFSYM (Qleft_margin, "left-margin");
29382 DEFSYM (Qright_margin, "right-margin");
29383 DEFSYM (Qcenter, "center");
29384 DEFSYM (Qline_height, "line-height");
29385 DEFSYM (QCalign_to, ":align-to");
29386 DEFSYM (QCrelative_width, ":relative-width");
29387 DEFSYM (QCrelative_height, ":relative-height");
29388 DEFSYM (QCeval, ":eval");
29389 DEFSYM (QCpropertize, ":propertize");
29390 DEFSYM (QCfile, ":file");
29391 DEFSYM (Qfontified, "fontified");
29392 DEFSYM (Qfontification_functions, "fontification-functions");
29393 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29394 DEFSYM (Qescape_glyph, "escape-glyph");
29395 DEFSYM (Qnobreak_space, "nobreak-space");
29396 DEFSYM (Qimage, "image");
29397 DEFSYM (Qtext, "text");
29398 DEFSYM (Qboth, "both");
29399 DEFSYM (Qboth_horiz, "both-horiz");
29400 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29401 DEFSYM (QCmap, ":map");
29402 DEFSYM (QCpointer, ":pointer");
29403 DEFSYM (Qrect, "rect");
29404 DEFSYM (Qcircle, "circle");
29405 DEFSYM (Qpoly, "poly");
29406 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29407 DEFSYM (Qgrow_only, "grow-only");
29408 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29409 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29410 DEFSYM (Qposition, "position");
29411 DEFSYM (Qbuffer_position, "buffer-position");
29412 DEFSYM (Qobject, "object");
29413 DEFSYM (Qbar, "bar");
29414 DEFSYM (Qhbar, "hbar");
29415 DEFSYM (Qbox, "box");
29416 DEFSYM (Qhollow, "hollow");
29417 DEFSYM (Qhand, "hand");
29418 DEFSYM (Qarrow, "arrow");
29419 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29420
29421 list_of_error = list1 (list2 (intern_c_string ("error"),
29422 intern_c_string ("void-variable")));
29423 staticpro (&list_of_error);
29424
29425 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29426 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29427 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29428 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29429
29430 echo_buffer[0] = echo_buffer[1] = Qnil;
29431 staticpro (&echo_buffer[0]);
29432 staticpro (&echo_buffer[1]);
29433
29434 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29435 staticpro (&echo_area_buffer[0]);
29436 staticpro (&echo_area_buffer[1]);
29437
29438 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29439 staticpro (&Vmessages_buffer_name);
29440
29441 mode_line_proptrans_alist = Qnil;
29442 staticpro (&mode_line_proptrans_alist);
29443 mode_line_string_list = Qnil;
29444 staticpro (&mode_line_string_list);
29445 mode_line_string_face = Qnil;
29446 staticpro (&mode_line_string_face);
29447 mode_line_string_face_prop = Qnil;
29448 staticpro (&mode_line_string_face_prop);
29449 Vmode_line_unwind_vector = Qnil;
29450 staticpro (&Vmode_line_unwind_vector);
29451
29452 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29453
29454 help_echo_string = Qnil;
29455 staticpro (&help_echo_string);
29456 help_echo_object = Qnil;
29457 staticpro (&help_echo_object);
29458 help_echo_window = Qnil;
29459 staticpro (&help_echo_window);
29460 previous_help_echo_string = Qnil;
29461 staticpro (&previous_help_echo_string);
29462 help_echo_pos = -1;
29463
29464 DEFSYM (Qright_to_left, "right-to-left");
29465 DEFSYM (Qleft_to_right, "left-to-right");
29466
29467 #ifdef HAVE_WINDOW_SYSTEM
29468 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29469 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29470 For example, if a block cursor is over a tab, it will be drawn as
29471 wide as that tab on the display. */);
29472 x_stretch_cursor_p = 0;
29473 #endif
29474
29475 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29476 doc: /* Non-nil means highlight trailing whitespace.
29477 The face used for trailing whitespace is `trailing-whitespace'. */);
29478 Vshow_trailing_whitespace = Qnil;
29479
29480 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29481 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29482 If the value is t, Emacs highlights non-ASCII chars which have the
29483 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29484 or `escape-glyph' face respectively.
29485
29486 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29487 U+2011 (non-breaking hyphen) are affected.
29488
29489 Any other non-nil value means to display these characters as a escape
29490 glyph followed by an ordinary space or hyphen.
29491
29492 A value of nil means no special handling of these characters. */);
29493 Vnobreak_char_display = Qt;
29494
29495 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29496 doc: /* The pointer shape to show in void text areas.
29497 A value of nil means to show the text pointer. Other options are `arrow',
29498 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29499 Vvoid_text_area_pointer = Qarrow;
29500
29501 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29502 doc: /* Non-nil means don't actually do any redisplay.
29503 This is used for internal purposes. */);
29504 Vinhibit_redisplay = Qnil;
29505
29506 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29507 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29508 Vglobal_mode_string = Qnil;
29509
29510 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29511 doc: /* Marker for where to display an arrow on top of the buffer text.
29512 This must be the beginning of a line in order to work.
29513 See also `overlay-arrow-string'. */);
29514 Voverlay_arrow_position = Qnil;
29515
29516 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29517 doc: /* String to display as an arrow in non-window frames.
29518 See also `overlay-arrow-position'. */);
29519 Voverlay_arrow_string = build_pure_c_string ("=>");
29520
29521 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29522 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29523 The symbols on this list are examined during redisplay to determine
29524 where to display overlay arrows. */);
29525 Voverlay_arrow_variable_list
29526 = list1 (intern_c_string ("overlay-arrow-position"));
29527
29528 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29529 doc: /* The number of lines to try scrolling a window by when point moves out.
29530 If that fails to bring point back on frame, point is centered instead.
29531 If this is zero, point is always centered after it moves off frame.
29532 If you want scrolling to always be a line at a time, you should set
29533 `scroll-conservatively' to a large value rather than set this to 1. */);
29534
29535 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29536 doc: /* Scroll up to this many lines, to bring point back on screen.
29537 If point moves off-screen, redisplay will scroll by up to
29538 `scroll-conservatively' lines in order to bring point just barely
29539 onto the screen again. If that cannot be done, then redisplay
29540 recenters point as usual.
29541
29542 If the value is greater than 100, redisplay will never recenter point,
29543 but will always scroll just enough text to bring point into view, even
29544 if you move far away.
29545
29546 A value of zero means always recenter point if it moves off screen. */);
29547 scroll_conservatively = 0;
29548
29549 DEFVAR_INT ("scroll-margin", scroll_margin,
29550 doc: /* Number of lines of margin at the top and bottom of a window.
29551 Recenter the window whenever point gets within this many lines
29552 of the top or bottom of the window. */);
29553 scroll_margin = 0;
29554
29555 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29556 doc: /* Pixels per inch value for non-window system displays.
29557 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29558 Vdisplay_pixels_per_inch = make_float (72.0);
29559
29560 #ifdef GLYPH_DEBUG
29561 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29562 #endif
29563
29564 DEFVAR_LISP ("truncate-partial-width-windows",
29565 Vtruncate_partial_width_windows,
29566 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29567 For an integer value, truncate lines in each window narrower than the
29568 full frame width, provided the window width is less than that integer;
29569 otherwise, respect the value of `truncate-lines'.
29570
29571 For any other non-nil value, truncate lines in all windows that do
29572 not span the full frame width.
29573
29574 A value of nil means to respect the value of `truncate-lines'.
29575
29576 If `word-wrap' is enabled, you might want to reduce this. */);
29577 Vtruncate_partial_width_windows = make_number (50);
29578
29579 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29580 doc: /* Maximum buffer size for which line number should be displayed.
29581 If the buffer is bigger than this, the line number does not appear
29582 in the mode line. A value of nil means no limit. */);
29583 Vline_number_display_limit = Qnil;
29584
29585 DEFVAR_INT ("line-number-display-limit-width",
29586 line_number_display_limit_width,
29587 doc: /* Maximum line width (in characters) for line number display.
29588 If the average length of the lines near point is bigger than this, then the
29589 line number may be omitted from the mode line. */);
29590 line_number_display_limit_width = 200;
29591
29592 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29593 doc: /* Non-nil means highlight region even in nonselected windows. */);
29594 highlight_nonselected_windows = 0;
29595
29596 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29597 doc: /* Non-nil if more than one frame is visible on this display.
29598 Minibuffer-only frames don't count, but iconified frames do.
29599 This variable is not guaranteed to be accurate except while processing
29600 `frame-title-format' and `icon-title-format'. */);
29601
29602 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29603 doc: /* Template for displaying the title bar of visible frames.
29604 \(Assuming the window manager supports this feature.)
29605
29606 This variable has the same structure as `mode-line-format', except that
29607 the %c and %l constructs are ignored. It is used only on frames for
29608 which no explicit name has been set \(see `modify-frame-parameters'). */);
29609
29610 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29611 doc: /* Template for displaying the title bar of an iconified frame.
29612 \(Assuming the window manager supports this feature.)
29613 This variable has the same structure as `mode-line-format' (which see),
29614 and is used only on frames for which no explicit name has been set
29615 \(see `modify-frame-parameters'). */);
29616 Vicon_title_format
29617 = Vframe_title_format
29618 = listn (CONSTYPE_PURE, 3,
29619 intern_c_string ("multiple-frames"),
29620 build_pure_c_string ("%b"),
29621 listn (CONSTYPE_PURE, 4,
29622 empty_unibyte_string,
29623 intern_c_string ("invocation-name"),
29624 build_pure_c_string ("@"),
29625 intern_c_string ("system-name")));
29626
29627 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29628 doc: /* Maximum number of lines to keep in the message log buffer.
29629 If nil, disable message logging. If t, log messages but don't truncate
29630 the buffer when it becomes large. */);
29631 Vmessage_log_max = make_number (1000);
29632
29633 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29634 doc: /* Functions called before redisplay, if window sizes have changed.
29635 The value should be a list of functions that take one argument.
29636 Just before redisplay, for each frame, if any of its windows have changed
29637 size since the last redisplay, or have been split or deleted,
29638 all the functions in the list are called, with the frame as argument. */);
29639 Vwindow_size_change_functions = Qnil;
29640
29641 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29642 doc: /* List of functions to call before redisplaying a window with scrolling.
29643 Each function is called with two arguments, the window and its new
29644 display-start position. Note that these functions are also called by
29645 `set-window-buffer'. Also note that the value of `window-end' is not
29646 valid when these functions are called.
29647
29648 Warning: Do not use this feature to alter the way the window
29649 is scrolled. It is not designed for that, and such use probably won't
29650 work. */);
29651 Vwindow_scroll_functions = Qnil;
29652
29653 DEFVAR_LISP ("window-text-change-functions",
29654 Vwindow_text_change_functions,
29655 doc: /* Functions to call in redisplay when text in the window might change. */);
29656 Vwindow_text_change_functions = Qnil;
29657
29658 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29659 doc: /* Functions called when redisplay of a window reaches the end trigger.
29660 Each function is called with two arguments, the window and the end trigger value.
29661 See `set-window-redisplay-end-trigger'. */);
29662 Vredisplay_end_trigger_functions = Qnil;
29663
29664 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29665 doc: /* Non-nil means autoselect window with mouse pointer.
29666 If nil, do not autoselect windows.
29667 A positive number means delay autoselection by that many seconds: a
29668 window is autoselected only after the mouse has remained in that
29669 window for the duration of the delay.
29670 A negative number has a similar effect, but causes windows to be
29671 autoselected only after the mouse has stopped moving. \(Because of
29672 the way Emacs compares mouse events, you will occasionally wait twice
29673 that time before the window gets selected.\)
29674 Any other value means to autoselect window instantaneously when the
29675 mouse pointer enters it.
29676
29677 Autoselection selects the minibuffer only if it is active, and never
29678 unselects the minibuffer if it is active.
29679
29680 When customizing this variable make sure that the actual value of
29681 `focus-follows-mouse' matches the behavior of your window manager. */);
29682 Vmouse_autoselect_window = Qnil;
29683
29684 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29685 doc: /* Non-nil means automatically resize tool-bars.
29686 This dynamically changes the tool-bar's height to the minimum height
29687 that is needed to make all tool-bar items visible.
29688 If value is `grow-only', the tool-bar's height is only increased
29689 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29690 Vauto_resize_tool_bars = Qt;
29691
29692 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29693 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29694 auto_raise_tool_bar_buttons_p = 1;
29695
29696 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29697 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29698 make_cursor_line_fully_visible_p = 1;
29699
29700 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29701 doc: /* Border below tool-bar in pixels.
29702 If an integer, use it as the height of the border.
29703 If it is one of `internal-border-width' or `border-width', use the
29704 value of the corresponding frame parameter.
29705 Otherwise, no border is added below the tool-bar. */);
29706 Vtool_bar_border = Qinternal_border_width;
29707
29708 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29709 doc: /* Margin around tool-bar buttons in pixels.
29710 If an integer, use that for both horizontal and vertical margins.
29711 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29712 HORZ specifying the horizontal margin, and VERT specifying the
29713 vertical margin. */);
29714 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29715
29716 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29717 doc: /* Relief thickness of tool-bar buttons. */);
29718 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29719
29720 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29721 doc: /* Tool bar style to use.
29722 It can be one of
29723 image - show images only
29724 text - show text only
29725 both - show both, text below image
29726 both-horiz - show text to the right of the image
29727 text-image-horiz - show text to the left of the image
29728 any other - use system default or image if no system default.
29729
29730 This variable only affects the GTK+ toolkit version of Emacs. */);
29731 Vtool_bar_style = Qnil;
29732
29733 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29734 doc: /* Maximum number of characters a label can have to be shown.
29735 The tool bar style must also show labels for this to have any effect, see
29736 `tool-bar-style'. */);
29737 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29738
29739 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29740 doc: /* List of functions to call to fontify regions of text.
29741 Each function is called with one argument POS. Functions must
29742 fontify a region starting at POS in the current buffer, and give
29743 fontified regions the property `fontified'. */);
29744 Vfontification_functions = Qnil;
29745 Fmake_variable_buffer_local (Qfontification_functions);
29746
29747 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29748 unibyte_display_via_language_environment,
29749 doc: /* Non-nil means display unibyte text according to language environment.
29750 Specifically, this means that raw bytes in the range 160-255 decimal
29751 are displayed by converting them to the equivalent multibyte characters
29752 according to the current language environment. As a result, they are
29753 displayed according to the current fontset.
29754
29755 Note that this variable affects only how these bytes are displayed,
29756 but does not change the fact they are interpreted as raw bytes. */);
29757 unibyte_display_via_language_environment = 0;
29758
29759 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29760 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29761 If a float, it specifies a fraction of the mini-window frame's height.
29762 If an integer, it specifies a number of lines. */);
29763 Vmax_mini_window_height = make_float (0.25);
29764
29765 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29766 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29767 A value of nil means don't automatically resize mini-windows.
29768 A value of t means resize them to fit the text displayed in them.
29769 A value of `grow-only', the default, means let mini-windows grow only;
29770 they return to their normal size when the minibuffer is closed, or the
29771 echo area becomes empty. */);
29772 Vresize_mini_windows = Qgrow_only;
29773
29774 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29775 doc: /* Alist specifying how to blink the cursor off.
29776 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29777 `cursor-type' frame-parameter or variable equals ON-STATE,
29778 comparing using `equal', Emacs uses OFF-STATE to specify
29779 how to blink it off. ON-STATE and OFF-STATE are values for
29780 the `cursor-type' frame parameter.
29781
29782 If a frame's ON-STATE has no entry in this list,
29783 the frame's other specifications determine how to blink the cursor off. */);
29784 Vblink_cursor_alist = Qnil;
29785
29786 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29787 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29788 If non-nil, windows are automatically scrolled horizontally to make
29789 point visible. */);
29790 automatic_hscrolling_p = 1;
29791 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29792
29793 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29794 doc: /* How many columns away from the window edge point is allowed to get
29795 before automatic hscrolling will horizontally scroll the window. */);
29796 hscroll_margin = 5;
29797
29798 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29799 doc: /* How many columns to scroll the window when point gets too close to the edge.
29800 When point is less than `hscroll-margin' columns from the window
29801 edge, automatic hscrolling will scroll the window by the amount of columns
29802 determined by this variable. If its value is a positive integer, scroll that
29803 many columns. If it's a positive floating-point number, it specifies the
29804 fraction of the window's width to scroll. If it's nil or zero, point will be
29805 centered horizontally after the scroll. Any other value, including negative
29806 numbers, are treated as if the value were zero.
29807
29808 Automatic hscrolling always moves point outside the scroll margin, so if
29809 point was more than scroll step columns inside the margin, the window will
29810 scroll more than the value given by the scroll step.
29811
29812 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29813 and `scroll-right' overrides this variable's effect. */);
29814 Vhscroll_step = make_number (0);
29815
29816 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29817 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29818 Bind this around calls to `message' to let it take effect. */);
29819 message_truncate_lines = 0;
29820
29821 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29822 doc: /* Normal hook run to update the menu bar definitions.
29823 Redisplay runs this hook before it redisplays the menu bar.
29824 This is used to update submenus such as Buffers,
29825 whose contents depend on various data. */);
29826 Vmenu_bar_update_hook = Qnil;
29827
29828 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29829 doc: /* Frame for which we are updating a menu.
29830 The enable predicate for a menu binding should check this variable. */);
29831 Vmenu_updating_frame = Qnil;
29832
29833 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29834 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29835 inhibit_menubar_update = 0;
29836
29837 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29838 doc: /* Prefix prepended to all continuation lines at display time.
29839 The value may be a string, an image, or a stretch-glyph; it is
29840 interpreted in the same way as the value of a `display' text property.
29841
29842 This variable is overridden by any `wrap-prefix' text or overlay
29843 property.
29844
29845 To add a prefix to non-continuation lines, use `line-prefix'. */);
29846 Vwrap_prefix = Qnil;
29847 DEFSYM (Qwrap_prefix, "wrap-prefix");
29848 Fmake_variable_buffer_local (Qwrap_prefix);
29849
29850 DEFVAR_LISP ("line-prefix", Vline_prefix,
29851 doc: /* Prefix prepended to all non-continuation lines at display time.
29852 The value may be a string, an image, or a stretch-glyph; it is
29853 interpreted in the same way as the value of a `display' text property.
29854
29855 This variable is overridden by any `line-prefix' text or overlay
29856 property.
29857
29858 To add a prefix to continuation lines, use `wrap-prefix'. */);
29859 Vline_prefix = Qnil;
29860 DEFSYM (Qline_prefix, "line-prefix");
29861 Fmake_variable_buffer_local (Qline_prefix);
29862
29863 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29864 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29865 inhibit_eval_during_redisplay = 0;
29866
29867 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29868 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29869 inhibit_free_realized_faces = 0;
29870
29871 #ifdef GLYPH_DEBUG
29872 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29873 doc: /* Inhibit try_window_id display optimization. */);
29874 inhibit_try_window_id = 0;
29875
29876 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29877 doc: /* Inhibit try_window_reusing display optimization. */);
29878 inhibit_try_window_reusing = 0;
29879
29880 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29881 doc: /* Inhibit try_cursor_movement display optimization. */);
29882 inhibit_try_cursor_movement = 0;
29883 #endif /* GLYPH_DEBUG */
29884
29885 DEFVAR_INT ("overline-margin", overline_margin,
29886 doc: /* Space between overline and text, in pixels.
29887 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29888 margin to the character height. */);
29889 overline_margin = 2;
29890
29891 DEFVAR_INT ("underline-minimum-offset",
29892 underline_minimum_offset,
29893 doc: /* Minimum distance between baseline and underline.
29894 This can improve legibility of underlined text at small font sizes,
29895 particularly when using variable `x-use-underline-position-properties'
29896 with fonts that specify an UNDERLINE_POSITION relatively close to the
29897 baseline. The default value is 1. */);
29898 underline_minimum_offset = 1;
29899
29900 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29901 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29902 This feature only works when on a window system that can change
29903 cursor shapes. */);
29904 display_hourglass_p = 1;
29905
29906 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29907 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29908 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29909
29910 hourglass_atimer = NULL;
29911 hourglass_shown_p = 0;
29912
29913 DEFSYM (Qglyphless_char, "glyphless-char");
29914 DEFSYM (Qhex_code, "hex-code");
29915 DEFSYM (Qempty_box, "empty-box");
29916 DEFSYM (Qthin_space, "thin-space");
29917 DEFSYM (Qzero_width, "zero-width");
29918
29919 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29920 /* Intern this now in case it isn't already done.
29921 Setting this variable twice is harmless.
29922 But don't staticpro it here--that is done in alloc.c. */
29923 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29924 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29925
29926 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29927 doc: /* Char-table defining glyphless characters.
29928 Each element, if non-nil, should be one of the following:
29929 an ASCII acronym string: display this string in a box
29930 `hex-code': display the hexadecimal code of a character in a box
29931 `empty-box': display as an empty box
29932 `thin-space': display as 1-pixel width space
29933 `zero-width': don't display
29934 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29935 display method for graphical terminals and text terminals respectively.
29936 GRAPHICAL and TEXT should each have one of the values listed above.
29937
29938 The char-table has one extra slot to control the display of a character for
29939 which no font is found. This slot only takes effect on graphical terminals.
29940 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29941 `thin-space'. The default is `empty-box'. */);
29942 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29943 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29944 Qempty_box);
29945
29946 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29947 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29948 Vdebug_on_message = Qnil;
29949 }
29950
29951
29952 /* Initialize this module when Emacs starts. */
29953
29954 void
29955 init_xdisp (void)
29956 {
29957 current_header_line_height = current_mode_line_height = -1;
29958
29959 CHARPOS (this_line_start_pos) = 0;
29960
29961 if (!noninteractive)
29962 {
29963 struct window *m = XWINDOW (minibuf_window);
29964 Lisp_Object frame = m->frame;
29965 struct frame *f = XFRAME (frame);
29966 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29967 struct window *r = XWINDOW (root);
29968 int i;
29969
29970 echo_area_window = minibuf_window;
29971
29972 r->top_line = FRAME_TOP_MARGIN (f);
29973 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29974 r->total_cols = FRAME_COLS (f);
29975
29976 m->top_line = FRAME_LINES (f) - 1;
29977 m->total_lines = 1;
29978 m->total_cols = FRAME_COLS (f);
29979
29980 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29981 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29982 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29983
29984 /* The default ellipsis glyphs `...'. */
29985 for (i = 0; i < 3; ++i)
29986 default_invis_vector[i] = make_number ('.');
29987 }
29988
29989 {
29990 /* Allocate the buffer for frame titles.
29991 Also used for `format-mode-line'. */
29992 int size = 100;
29993 mode_line_noprop_buf = xmalloc (size);
29994 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29995 mode_line_noprop_ptr = mode_line_noprop_buf;
29996 mode_line_target = MODE_LINE_DISPLAY;
29997 }
29998
29999 help_echo_showing_p = 0;
30000 }
30001
30002 /* Platform-independent portion of hourglass implementation. */
30003
30004 /* Cancel a currently active hourglass timer, and start a new one. */
30005 void
30006 start_hourglass (void)
30007 {
30008 #if defined (HAVE_WINDOW_SYSTEM)
30009 EMACS_TIME delay;
30010
30011 cancel_hourglass ();
30012
30013 if (INTEGERP (Vhourglass_delay)
30014 && XINT (Vhourglass_delay) > 0)
30015 delay = make_emacs_time (min (XINT (Vhourglass_delay),
30016 TYPE_MAXIMUM (time_t)),
30017 0);
30018 else if (FLOATP (Vhourglass_delay)
30019 && XFLOAT_DATA (Vhourglass_delay) > 0)
30020 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
30021 else
30022 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
30023
30024 #ifdef HAVE_NTGUI
30025 {
30026 extern void w32_note_current_window (void);
30027 w32_note_current_window ();
30028 }
30029 #endif /* HAVE_NTGUI */
30030
30031 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30032 show_hourglass, NULL);
30033 #endif
30034 }
30035
30036
30037 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30038 shown. */
30039 void
30040 cancel_hourglass (void)
30041 {
30042 #if defined (HAVE_WINDOW_SYSTEM)
30043 if (hourglass_atimer)
30044 {
30045 cancel_atimer (hourglass_atimer);
30046 hourglass_atimer = NULL;
30047 }
30048
30049 if (hourglass_shown_p)
30050 hide_hourglass ();
30051 #endif
30052 }