]> 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 faces.
3923 For strings from wrap-prefix and line-prefix properties,
3924 use the default face, possibly remapped via
3925 Vface_remapping_alist. */
3926 base_face_id = it->string_from_prefix_prop_p
3927 ? (!NILP (Vface_remapping_alist)
3928 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3929 : DEFAULT_FACE_ID)
3930 : underlying_face_id (it);
3931 }
3932
3933 new_face_id = face_at_string_position (it->w,
3934 it->string,
3935 IT_STRING_CHARPOS (*it),
3936 bufpos,
3937 it->region_beg_charpos,
3938 it->region_end_charpos,
3939 &next_stop,
3940 base_face_id, 0);
3941
3942 /* Is this a start of a run of characters with box? Caveat:
3943 this can be called for a freshly allocated iterator; face_id
3944 is -1 is this case. We know that the new face will not
3945 change until the next check pos, i.e. if the new face has a
3946 box, all characters up to that position will have a
3947 box. But, as usual, we don't know whether that position
3948 is really the end. */
3949 if (new_face_id != it->face_id)
3950 {
3951 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3952 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3953
3954 /* If new face has a box but old face hasn't, this is the
3955 start of a run of characters with box, i.e. it has a
3956 shadow on the left side. */
3957 it->start_of_box_run_p
3958 = new_face->box && (old_face == NULL || !old_face->box);
3959 it->face_box_p = new_face->box != FACE_NO_BOX;
3960 }
3961 }
3962
3963 it->face_id = new_face_id;
3964 return HANDLED_NORMALLY;
3965 }
3966
3967
3968 /* Return the ID of the face ``underlying'' IT's current position,
3969 which is in a string. If the iterator is associated with a
3970 buffer, return the face at IT's current buffer position.
3971 Otherwise, use the iterator's base_face_id. */
3972
3973 static int
3974 underlying_face_id (struct it *it)
3975 {
3976 int face_id = it->base_face_id, i;
3977
3978 eassert (STRINGP (it->string));
3979
3980 for (i = it->sp - 1; i >= 0; --i)
3981 if (NILP (it->stack[i].string))
3982 face_id = it->stack[i].face_id;
3983
3984 return face_id;
3985 }
3986
3987
3988 /* Compute the face one character before or after the current position
3989 of IT, in the visual order. BEFORE_P non-zero means get the face
3990 in front (to the left in L2R paragraphs, to the right in R2L
3991 paragraphs) of IT's screen position. Value is the ID of the face. */
3992
3993 static int
3994 face_before_or_after_it_pos (struct it *it, int before_p)
3995 {
3996 int face_id, limit;
3997 ptrdiff_t next_check_charpos;
3998 struct it it_copy;
3999 void *it_copy_data = NULL;
4000
4001 eassert (it->s == NULL);
4002
4003 if (STRINGP (it->string))
4004 {
4005 ptrdiff_t bufpos, charpos;
4006 int base_face_id;
4007
4008 /* No face change past the end of the string (for the case
4009 we are padding with spaces). No face change before the
4010 string start. */
4011 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4012 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4013 return it->face_id;
4014
4015 if (!it->bidi_p)
4016 {
4017 /* Set charpos to the position before or after IT's current
4018 position, in the logical order, which in the non-bidi
4019 case is the same as the visual order. */
4020 if (before_p)
4021 charpos = IT_STRING_CHARPOS (*it) - 1;
4022 else if (it->what == IT_COMPOSITION)
4023 /* For composition, we must check the character after the
4024 composition. */
4025 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4026 else
4027 charpos = IT_STRING_CHARPOS (*it) + 1;
4028 }
4029 else
4030 {
4031 if (before_p)
4032 {
4033 /* With bidi iteration, the character before the current
4034 in the visual order cannot be found by simple
4035 iteration, because "reverse" reordering is not
4036 supported. Instead, we need to use the move_it_*
4037 family of functions. */
4038 /* Ignore face changes before the first visible
4039 character on this display line. */
4040 if (it->current_x <= it->first_visible_x)
4041 return it->face_id;
4042 SAVE_IT (it_copy, *it, it_copy_data);
4043 /* Implementation note: Since move_it_in_display_line
4044 works in the iterator geometry, and thinks the first
4045 character is always the leftmost, even in R2L lines,
4046 we don't need to distinguish between the R2L and L2R
4047 cases here. */
4048 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4049 it_copy.current_x - 1, MOVE_TO_X);
4050 charpos = IT_STRING_CHARPOS (it_copy);
4051 RESTORE_IT (it, it, it_copy_data);
4052 }
4053 else
4054 {
4055 /* Set charpos to the string position of the character
4056 that comes after IT's current position in the visual
4057 order. */
4058 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4059
4060 it_copy = *it;
4061 while (n--)
4062 bidi_move_to_visually_next (&it_copy.bidi_it);
4063
4064 charpos = it_copy.bidi_it.charpos;
4065 }
4066 }
4067 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4068
4069 if (it->current.overlay_string_index >= 0)
4070 bufpos = IT_CHARPOS (*it);
4071 else
4072 bufpos = 0;
4073
4074 base_face_id = underlying_face_id (it);
4075
4076 /* Get the face for ASCII, or unibyte. */
4077 face_id = face_at_string_position (it->w,
4078 it->string,
4079 charpos,
4080 bufpos,
4081 it->region_beg_charpos,
4082 it->region_end_charpos,
4083 &next_check_charpos,
4084 base_face_id, 0);
4085
4086 /* Correct the face for charsets different from ASCII. Do it
4087 for the multibyte case only. The face returned above is
4088 suitable for unibyte text if IT->string is unibyte. */
4089 if (STRING_MULTIBYTE (it->string))
4090 {
4091 struct text_pos pos1 = string_pos (charpos, it->string);
4092 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4093 int c, len;
4094 struct face *face = FACE_FROM_ID (it->f, face_id);
4095
4096 c = string_char_and_length (p, &len);
4097 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4098 }
4099 }
4100 else
4101 {
4102 struct text_pos pos;
4103
4104 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4105 || (IT_CHARPOS (*it) <= BEGV && before_p))
4106 return it->face_id;
4107
4108 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4109 pos = it->current.pos;
4110
4111 if (!it->bidi_p)
4112 {
4113 if (before_p)
4114 DEC_TEXT_POS (pos, it->multibyte_p);
4115 else
4116 {
4117 if (it->what == IT_COMPOSITION)
4118 {
4119 /* For composition, we must check the position after
4120 the composition. */
4121 pos.charpos += it->cmp_it.nchars;
4122 pos.bytepos += it->len;
4123 }
4124 else
4125 INC_TEXT_POS (pos, it->multibyte_p);
4126 }
4127 }
4128 else
4129 {
4130 if (before_p)
4131 {
4132 /* With bidi iteration, the character before the current
4133 in the visual order cannot be found by simple
4134 iteration, because "reverse" reordering is not
4135 supported. Instead, we need to use the move_it_*
4136 family of functions. */
4137 /* Ignore face changes before the first visible
4138 character on this display line. */
4139 if (it->current_x <= it->first_visible_x)
4140 return it->face_id;
4141 SAVE_IT (it_copy, *it, it_copy_data);
4142 /* Implementation note: Since move_it_in_display_line
4143 works in the iterator geometry, and thinks the first
4144 character is always the leftmost, even in R2L lines,
4145 we don't need to distinguish between the R2L and L2R
4146 cases here. */
4147 move_it_in_display_line (&it_copy, ZV,
4148 it_copy.current_x - 1, MOVE_TO_X);
4149 pos = it_copy.current.pos;
4150 RESTORE_IT (it, it, it_copy_data);
4151 }
4152 else
4153 {
4154 /* Set charpos to the buffer position of the character
4155 that comes after IT's current position in the visual
4156 order. */
4157 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4158
4159 it_copy = *it;
4160 while (n--)
4161 bidi_move_to_visually_next (&it_copy.bidi_it);
4162
4163 SET_TEXT_POS (pos,
4164 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4165 }
4166 }
4167 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4168
4169 /* Determine face for CHARSET_ASCII, or unibyte. */
4170 face_id = face_at_buffer_position (it->w,
4171 CHARPOS (pos),
4172 it->region_beg_charpos,
4173 it->region_end_charpos,
4174 &next_check_charpos,
4175 limit, 0, -1);
4176
4177 /* Correct the face for charsets different from ASCII. Do it
4178 for the multibyte case only. The face returned above is
4179 suitable for unibyte text if current_buffer is unibyte. */
4180 if (it->multibyte_p)
4181 {
4182 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4183 struct face *face = FACE_FROM_ID (it->f, face_id);
4184 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4185 }
4186 }
4187
4188 return face_id;
4189 }
4190
4191
4192 \f
4193 /***********************************************************************
4194 Invisible text
4195 ***********************************************************************/
4196
4197 /* Set up iterator IT from invisible properties at its current
4198 position. Called from handle_stop. */
4199
4200 static enum prop_handled
4201 handle_invisible_prop (struct it *it)
4202 {
4203 enum prop_handled handled = HANDLED_NORMALLY;
4204 int invis_p;
4205 Lisp_Object prop;
4206
4207 if (STRINGP (it->string))
4208 {
4209 Lisp_Object end_charpos, limit, charpos;
4210
4211 /* Get the value of the invisible text property at the
4212 current position. Value will be nil if there is no such
4213 property. */
4214 charpos = make_number (IT_STRING_CHARPOS (*it));
4215 prop = Fget_text_property (charpos, Qinvisible, it->string);
4216 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4217
4218 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4219 {
4220 /* Record whether we have to display an ellipsis for the
4221 invisible text. */
4222 int display_ellipsis_p = (invis_p == 2);
4223 ptrdiff_t len, endpos;
4224
4225 handled = HANDLED_RECOMPUTE_PROPS;
4226
4227 /* Get the position at which the next visible text can be
4228 found in IT->string, if any. */
4229 endpos = len = SCHARS (it->string);
4230 XSETINT (limit, len);
4231 do
4232 {
4233 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4234 it->string, limit);
4235 if (INTEGERP (end_charpos))
4236 {
4237 endpos = XFASTINT (end_charpos);
4238 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4239 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4240 if (invis_p == 2)
4241 display_ellipsis_p = 1;
4242 }
4243 }
4244 while (invis_p && endpos < len);
4245
4246 if (display_ellipsis_p)
4247 it->ellipsis_p = 1;
4248
4249 if (endpos < len)
4250 {
4251 /* Text at END_CHARPOS is visible. Move IT there. */
4252 struct text_pos old;
4253 ptrdiff_t oldpos;
4254
4255 old = it->current.string_pos;
4256 oldpos = CHARPOS (old);
4257 if (it->bidi_p)
4258 {
4259 if (it->bidi_it.first_elt
4260 && it->bidi_it.charpos < SCHARS (it->string))
4261 bidi_paragraph_init (it->paragraph_embedding,
4262 &it->bidi_it, 1);
4263 /* Bidi-iterate out of the invisible text. */
4264 do
4265 {
4266 bidi_move_to_visually_next (&it->bidi_it);
4267 }
4268 while (oldpos <= it->bidi_it.charpos
4269 && it->bidi_it.charpos < endpos);
4270
4271 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4272 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4273 if (IT_CHARPOS (*it) >= endpos)
4274 it->prev_stop = endpos;
4275 }
4276 else
4277 {
4278 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4279 compute_string_pos (&it->current.string_pos, old, it->string);
4280 }
4281 }
4282 else
4283 {
4284 /* The rest of the string is invisible. If this is an
4285 overlay string, proceed with the next overlay string
4286 or whatever comes and return a character from there. */
4287 if (it->current.overlay_string_index >= 0
4288 && !display_ellipsis_p)
4289 {
4290 next_overlay_string (it);
4291 /* Don't check for overlay strings when we just
4292 finished processing them. */
4293 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4294 }
4295 else
4296 {
4297 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4298 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4299 }
4300 }
4301 }
4302 }
4303 else
4304 {
4305 ptrdiff_t newpos, next_stop, start_charpos, tem;
4306 Lisp_Object pos, overlay;
4307
4308 /* First of all, is there invisible text at this position? */
4309 tem = start_charpos = IT_CHARPOS (*it);
4310 pos = make_number (tem);
4311 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4312 &overlay);
4313 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4314
4315 /* If we are on invisible text, skip over it. */
4316 if (invis_p && start_charpos < it->end_charpos)
4317 {
4318 /* Record whether we have to display an ellipsis for the
4319 invisible text. */
4320 int display_ellipsis_p = invis_p == 2;
4321
4322 handled = HANDLED_RECOMPUTE_PROPS;
4323
4324 /* Loop skipping over invisible text. The loop is left at
4325 ZV or with IT on the first char being visible again. */
4326 do
4327 {
4328 /* Try to skip some invisible text. Return value is the
4329 position reached which can be equal to where we start
4330 if there is nothing invisible there. This skips both
4331 over invisible text properties and overlays with
4332 invisible property. */
4333 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4334
4335 /* If we skipped nothing at all we weren't at invisible
4336 text in the first place. If everything to the end of
4337 the buffer was skipped, end the loop. */
4338 if (newpos == tem || newpos >= ZV)
4339 invis_p = 0;
4340 else
4341 {
4342 /* We skipped some characters but not necessarily
4343 all there are. Check if we ended up on visible
4344 text. Fget_char_property returns the property of
4345 the char before the given position, i.e. if we
4346 get invis_p = 0, this means that the char at
4347 newpos is visible. */
4348 pos = make_number (newpos);
4349 prop = Fget_char_property (pos, Qinvisible, it->window);
4350 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4351 }
4352
4353 /* If we ended up on invisible text, proceed to
4354 skip starting with next_stop. */
4355 if (invis_p)
4356 tem = next_stop;
4357
4358 /* If there are adjacent invisible texts, don't lose the
4359 second one's ellipsis. */
4360 if (invis_p == 2)
4361 display_ellipsis_p = 1;
4362 }
4363 while (invis_p);
4364
4365 /* The position newpos is now either ZV or on visible text. */
4366 if (it->bidi_p)
4367 {
4368 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4369 int on_newline =
4370 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4371 int after_newline =
4372 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4373
4374 /* If the invisible text ends on a newline or on a
4375 character after a newline, we can avoid the costly,
4376 character by character, bidi iteration to NEWPOS, and
4377 instead simply reseat the iterator there. That's
4378 because all bidi reordering information is tossed at
4379 the newline. This is a big win for modes that hide
4380 complete lines, like Outline, Org, etc. */
4381 if (on_newline || after_newline)
4382 {
4383 struct text_pos tpos;
4384 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4385
4386 SET_TEXT_POS (tpos, newpos, bpos);
4387 reseat_1 (it, tpos, 0);
4388 /* If we reseat on a newline/ZV, we need to prep the
4389 bidi iterator for advancing to the next character
4390 after the newline/EOB, keeping the current paragraph
4391 direction (so that PRODUCE_GLYPHS does TRT wrt
4392 prepending/appending glyphs to a glyph row). */
4393 if (on_newline)
4394 {
4395 it->bidi_it.first_elt = 0;
4396 it->bidi_it.paragraph_dir = pdir;
4397 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4398 it->bidi_it.nchars = 1;
4399 it->bidi_it.ch_len = 1;
4400 }
4401 }
4402 else /* Must use the slow method. */
4403 {
4404 /* With bidi iteration, the region of invisible text
4405 could start and/or end in the middle of a
4406 non-base embedding level. Therefore, we need to
4407 skip invisible text using the bidi iterator,
4408 starting at IT's current position, until we find
4409 ourselves outside of the invisible text.
4410 Skipping invisible text _after_ bidi iteration
4411 avoids affecting the visual order of the
4412 displayed text when invisible properties are
4413 added or removed. */
4414 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4415 {
4416 /* If we were `reseat'ed to a new paragraph,
4417 determine the paragraph base direction. We
4418 need to do it now because
4419 next_element_from_buffer may not have a
4420 chance to do it, if we are going to skip any
4421 text at the beginning, which resets the
4422 FIRST_ELT flag. */
4423 bidi_paragraph_init (it->paragraph_embedding,
4424 &it->bidi_it, 1);
4425 }
4426 do
4427 {
4428 bidi_move_to_visually_next (&it->bidi_it);
4429 }
4430 while (it->stop_charpos <= it->bidi_it.charpos
4431 && it->bidi_it.charpos < newpos);
4432 IT_CHARPOS (*it) = it->bidi_it.charpos;
4433 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4434 /* If we overstepped NEWPOS, record its position in
4435 the iterator, so that we skip invisible text if
4436 later the bidi iteration lands us in the
4437 invisible region again. */
4438 if (IT_CHARPOS (*it) >= newpos)
4439 it->prev_stop = newpos;
4440 }
4441 }
4442 else
4443 {
4444 IT_CHARPOS (*it) = newpos;
4445 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4446 }
4447
4448 /* If there are before-strings at the start of invisible
4449 text, and the text is invisible because of a text
4450 property, arrange to show before-strings because 20.x did
4451 it that way. (If the text is invisible because of an
4452 overlay property instead of a text property, this is
4453 already handled in the overlay code.) */
4454 if (NILP (overlay)
4455 && get_overlay_strings (it, it->stop_charpos))
4456 {
4457 handled = HANDLED_RECOMPUTE_PROPS;
4458 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4459 }
4460 else if (display_ellipsis_p)
4461 {
4462 /* Make sure that the glyphs of the ellipsis will get
4463 correct `charpos' values. If we would not update
4464 it->position here, the glyphs would belong to the
4465 last visible character _before_ the invisible
4466 text, which confuses `set_cursor_from_row'.
4467
4468 We use the last invisible position instead of the
4469 first because this way the cursor is always drawn on
4470 the first "." of the ellipsis, whenever PT is inside
4471 the invisible text. Otherwise the cursor would be
4472 placed _after_ the ellipsis when the point is after the
4473 first invisible character. */
4474 if (!STRINGP (it->object))
4475 {
4476 it->position.charpos = newpos - 1;
4477 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4478 }
4479 it->ellipsis_p = 1;
4480 /* Let the ellipsis display before
4481 considering any properties of the following char.
4482 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4483 handled = HANDLED_RETURN;
4484 }
4485 }
4486 }
4487
4488 return handled;
4489 }
4490
4491
4492 /* Make iterator IT return `...' next.
4493 Replaces LEN characters from buffer. */
4494
4495 static void
4496 setup_for_ellipsis (struct it *it, int len)
4497 {
4498 /* Use the display table definition for `...'. Invalid glyphs
4499 will be handled by the method returning elements from dpvec. */
4500 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4501 {
4502 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4503 it->dpvec = v->contents;
4504 it->dpend = v->contents + v->header.size;
4505 }
4506 else
4507 {
4508 /* Default `...'. */
4509 it->dpvec = default_invis_vector;
4510 it->dpend = default_invis_vector + 3;
4511 }
4512
4513 it->dpvec_char_len = len;
4514 it->current.dpvec_index = 0;
4515 it->dpvec_face_id = -1;
4516
4517 /* Remember the current face id in case glyphs specify faces.
4518 IT's face is restored in set_iterator_to_next.
4519 saved_face_id was set to preceding char's face in handle_stop. */
4520 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4521 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4522
4523 it->method = GET_FROM_DISPLAY_VECTOR;
4524 it->ellipsis_p = 1;
4525 }
4526
4527
4528 \f
4529 /***********************************************************************
4530 'display' property
4531 ***********************************************************************/
4532
4533 /* Set up iterator IT from `display' property at its current position.
4534 Called from handle_stop.
4535 We return HANDLED_RETURN if some part of the display property
4536 overrides the display of the buffer text itself.
4537 Otherwise we return HANDLED_NORMALLY. */
4538
4539 static enum prop_handled
4540 handle_display_prop (struct it *it)
4541 {
4542 Lisp_Object propval, object, overlay;
4543 struct text_pos *position;
4544 ptrdiff_t bufpos;
4545 /* Nonzero if some property replaces the display of the text itself. */
4546 int display_replaced_p = 0;
4547
4548 if (STRINGP (it->string))
4549 {
4550 object = it->string;
4551 position = &it->current.string_pos;
4552 bufpos = CHARPOS (it->current.pos);
4553 }
4554 else
4555 {
4556 XSETWINDOW (object, it->w);
4557 position = &it->current.pos;
4558 bufpos = CHARPOS (*position);
4559 }
4560
4561 /* Reset those iterator values set from display property values. */
4562 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4563 it->space_width = Qnil;
4564 it->font_height = Qnil;
4565 it->voffset = 0;
4566
4567 /* We don't support recursive `display' properties, i.e. string
4568 values that have a string `display' property, that have a string
4569 `display' property etc. */
4570 if (!it->string_from_display_prop_p)
4571 it->area = TEXT_AREA;
4572
4573 propval = get_char_property_and_overlay (make_number (position->charpos),
4574 Qdisplay, object, &overlay);
4575 if (NILP (propval))
4576 return HANDLED_NORMALLY;
4577 /* Now OVERLAY is the overlay that gave us this property, or nil
4578 if it was a text property. */
4579
4580 if (!STRINGP (it->string))
4581 object = it->w->contents;
4582
4583 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4584 position, bufpos,
4585 FRAME_WINDOW_P (it->f));
4586
4587 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4588 }
4589
4590 /* Subroutine of handle_display_prop. Returns non-zero if the display
4591 specification in SPEC is a replacing specification, i.e. it would
4592 replace the text covered by `display' property with something else,
4593 such as an image or a display string. If SPEC includes any kind or
4594 `(space ...) specification, the value is 2; this is used by
4595 compute_display_string_pos, which see.
4596
4597 See handle_single_display_spec for documentation of arguments.
4598 frame_window_p is non-zero if the window being redisplayed is on a
4599 GUI frame; this argument is used only if IT is NULL, see below.
4600
4601 IT can be NULL, if this is called by the bidi reordering code
4602 through compute_display_string_pos, which see. In that case, this
4603 function only examines SPEC, but does not otherwise "handle" it, in
4604 the sense that it doesn't set up members of IT from the display
4605 spec. */
4606 static int
4607 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4608 Lisp_Object overlay, struct text_pos *position,
4609 ptrdiff_t bufpos, int frame_window_p)
4610 {
4611 int replacing_p = 0;
4612 int rv;
4613
4614 if (CONSP (spec)
4615 /* Simple specifications. */
4616 && !EQ (XCAR (spec), Qimage)
4617 #ifdef HAVE_XWIDGETS
4618 && !EQ (XCAR (spec), Qxwidget)
4619 #endif
4620 && !EQ (XCAR (spec), Qspace)
4621 && !EQ (XCAR (spec), Qwhen)
4622 && !EQ (XCAR (spec), Qslice)
4623 && !EQ (XCAR (spec), Qspace_width)
4624 && !EQ (XCAR (spec), Qheight)
4625 && !EQ (XCAR (spec), Qraise)
4626 /* Marginal area specifications. */
4627 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4628 && !EQ (XCAR (spec), Qleft_fringe)
4629 && !EQ (XCAR (spec), Qright_fringe)
4630 && !NILP (XCAR (spec)))
4631 {
4632 for (; CONSP (spec); spec = XCDR (spec))
4633 {
4634 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4635 overlay, position, bufpos,
4636 replacing_p, frame_window_p)))
4637 {
4638 replacing_p = rv;
4639 /* If some text in a string is replaced, `position' no
4640 longer points to the position of `object'. */
4641 if (!it || STRINGP (object))
4642 break;
4643 }
4644 }
4645 }
4646 else if (VECTORP (spec))
4647 {
4648 ptrdiff_t i;
4649 for (i = 0; i < ASIZE (spec); ++i)
4650 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4651 overlay, position, bufpos,
4652 replacing_p, frame_window_p)))
4653 {
4654 replacing_p = rv;
4655 /* If some text in a string is replaced, `position' no
4656 longer points to the position of `object'. */
4657 if (!it || STRINGP (object))
4658 break;
4659 }
4660 }
4661 else
4662 {
4663 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4664 position, bufpos, 0,
4665 frame_window_p)))
4666 replacing_p = rv;
4667 }
4668
4669 return replacing_p;
4670 }
4671
4672 /* Value is the position of the end of the `display' property starting
4673 at START_POS in OBJECT. */
4674
4675 static struct text_pos
4676 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4677 {
4678 Lisp_Object end;
4679 struct text_pos end_pos;
4680
4681 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4682 Qdisplay, object, Qnil);
4683 CHARPOS (end_pos) = XFASTINT (end);
4684 if (STRINGP (object))
4685 compute_string_pos (&end_pos, start_pos, it->string);
4686 else
4687 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4688
4689 return end_pos;
4690 }
4691
4692
4693 /* Set up IT from a single `display' property specification SPEC. OBJECT
4694 is the object in which the `display' property was found. *POSITION
4695 is the position in OBJECT at which the `display' property was found.
4696 BUFPOS is the buffer position of OBJECT (different from POSITION if
4697 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4698 previously saw a display specification which already replaced text
4699 display with something else, for example an image; we ignore such
4700 properties after the first one has been processed.
4701
4702 OVERLAY is the overlay this `display' property came from,
4703 or nil if it was a text property.
4704
4705 If SPEC is a `space' or `image' specification, and in some other
4706 cases too, set *POSITION to the position where the `display'
4707 property ends.
4708
4709 If IT is NULL, only examine the property specification in SPEC, but
4710 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4711 is intended to be displayed in a window on a GUI frame.
4712
4713 Value is non-zero if something was found which replaces the display
4714 of buffer or string text. */
4715
4716 static int
4717 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4718 Lisp_Object overlay, struct text_pos *position,
4719 ptrdiff_t bufpos, int display_replaced_p,
4720 int frame_window_p)
4721 {
4722 Lisp_Object form;
4723 Lisp_Object location, value;
4724 struct text_pos start_pos = *position;
4725 int valid_p;
4726
4727 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4728 If the result is non-nil, use VALUE instead of SPEC. */
4729 form = Qt;
4730 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4731 {
4732 spec = XCDR (spec);
4733 if (!CONSP (spec))
4734 return 0;
4735 form = XCAR (spec);
4736 spec = XCDR (spec);
4737 }
4738
4739 if (!NILP (form) && !EQ (form, Qt))
4740 {
4741 ptrdiff_t count = SPECPDL_INDEX ();
4742 struct gcpro gcpro1;
4743
4744 /* Bind `object' to the object having the `display' property, a
4745 buffer or string. Bind `position' to the position in the
4746 object where the property was found, and `buffer-position'
4747 to the current position in the buffer. */
4748
4749 if (NILP (object))
4750 XSETBUFFER (object, current_buffer);
4751 specbind (Qobject, object);
4752 specbind (Qposition, make_number (CHARPOS (*position)));
4753 specbind (Qbuffer_position, make_number (bufpos));
4754 GCPRO1 (form);
4755 form = safe_eval (form);
4756 UNGCPRO;
4757 unbind_to (count, Qnil);
4758 }
4759
4760 if (NILP (form))
4761 return 0;
4762
4763 /* Handle `(height HEIGHT)' specifications. */
4764 if (CONSP (spec)
4765 && EQ (XCAR (spec), Qheight)
4766 && CONSP (XCDR (spec)))
4767 {
4768 if (it)
4769 {
4770 if (!FRAME_WINDOW_P (it->f))
4771 return 0;
4772
4773 it->font_height = XCAR (XCDR (spec));
4774 if (!NILP (it->font_height))
4775 {
4776 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4777 int new_height = -1;
4778
4779 if (CONSP (it->font_height)
4780 && (EQ (XCAR (it->font_height), Qplus)
4781 || EQ (XCAR (it->font_height), Qminus))
4782 && CONSP (XCDR (it->font_height))
4783 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4784 {
4785 /* `(+ N)' or `(- N)' where N is an integer. */
4786 int steps = XINT (XCAR (XCDR (it->font_height)));
4787 if (EQ (XCAR (it->font_height), Qplus))
4788 steps = - steps;
4789 it->face_id = smaller_face (it->f, it->face_id, steps);
4790 }
4791 else if (FUNCTIONP (it->font_height))
4792 {
4793 /* Call function with current height as argument.
4794 Value is the new height. */
4795 Lisp_Object height;
4796 height = safe_call1 (it->font_height,
4797 face->lface[LFACE_HEIGHT_INDEX]);
4798 if (NUMBERP (height))
4799 new_height = XFLOATINT (height);
4800 }
4801 else if (NUMBERP (it->font_height))
4802 {
4803 /* Value is a multiple of the canonical char height. */
4804 struct face *f;
4805
4806 f = FACE_FROM_ID (it->f,
4807 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4808 new_height = (XFLOATINT (it->font_height)
4809 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4810 }
4811 else
4812 {
4813 /* Evaluate IT->font_height with `height' bound to the
4814 current specified height to get the new height. */
4815 ptrdiff_t count = SPECPDL_INDEX ();
4816
4817 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4818 value = safe_eval (it->font_height);
4819 unbind_to (count, Qnil);
4820
4821 if (NUMBERP (value))
4822 new_height = XFLOATINT (value);
4823 }
4824
4825 if (new_height > 0)
4826 it->face_id = face_with_height (it->f, it->face_id, new_height);
4827 }
4828 }
4829
4830 return 0;
4831 }
4832
4833 /* Handle `(space-width WIDTH)'. */
4834 if (CONSP (spec)
4835 && EQ (XCAR (spec), Qspace_width)
4836 && CONSP (XCDR (spec)))
4837 {
4838 if (it)
4839 {
4840 if (!FRAME_WINDOW_P (it->f))
4841 return 0;
4842
4843 value = XCAR (XCDR (spec));
4844 if (NUMBERP (value) && XFLOATINT (value) > 0)
4845 it->space_width = value;
4846 }
4847
4848 return 0;
4849 }
4850
4851 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4852 if (CONSP (spec)
4853 && EQ (XCAR (spec), Qslice))
4854 {
4855 Lisp_Object tem;
4856
4857 if (it)
4858 {
4859 if (!FRAME_WINDOW_P (it->f))
4860 return 0;
4861
4862 if (tem = XCDR (spec), CONSP (tem))
4863 {
4864 it->slice.x = XCAR (tem);
4865 if (tem = XCDR (tem), CONSP (tem))
4866 {
4867 it->slice.y = XCAR (tem);
4868 if (tem = XCDR (tem), CONSP (tem))
4869 {
4870 it->slice.width = XCAR (tem);
4871 if (tem = XCDR (tem), CONSP (tem))
4872 it->slice.height = XCAR (tem);
4873 }
4874 }
4875 }
4876 }
4877
4878 return 0;
4879 }
4880
4881 /* Handle `(raise FACTOR)'. */
4882 if (CONSP (spec)
4883 && EQ (XCAR (spec), Qraise)
4884 && CONSP (XCDR (spec)))
4885 {
4886 if (it)
4887 {
4888 if (!FRAME_WINDOW_P (it->f))
4889 return 0;
4890
4891 #ifdef HAVE_WINDOW_SYSTEM
4892 value = XCAR (XCDR (spec));
4893 if (NUMBERP (value))
4894 {
4895 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4896 it->voffset = - (XFLOATINT (value)
4897 * (FONT_HEIGHT (face->font)));
4898 }
4899 #endif /* HAVE_WINDOW_SYSTEM */
4900 }
4901
4902 return 0;
4903 }
4904
4905 /* Don't handle the other kinds of display specifications
4906 inside a string that we got from a `display' property. */
4907 if (it && it->string_from_display_prop_p)
4908 return 0;
4909
4910 /* Characters having this form of property are not displayed, so
4911 we have to find the end of the property. */
4912 if (it)
4913 {
4914 start_pos = *position;
4915 *position = display_prop_end (it, object, start_pos);
4916 }
4917 value = Qnil;
4918
4919 /* Stop the scan at that end position--we assume that all
4920 text properties change there. */
4921 if (it)
4922 it->stop_charpos = position->charpos;
4923
4924 /* Handle `(left-fringe BITMAP [FACE])'
4925 and `(right-fringe BITMAP [FACE])'. */
4926 if (CONSP (spec)
4927 && (EQ (XCAR (spec), Qleft_fringe)
4928 || EQ (XCAR (spec), Qright_fringe))
4929 && CONSP (XCDR (spec)))
4930 {
4931 int fringe_bitmap;
4932
4933 if (it)
4934 {
4935 if (!FRAME_WINDOW_P (it->f))
4936 /* If we return here, POSITION has been advanced
4937 across the text with this property. */
4938 {
4939 /* Synchronize the bidi iterator with POSITION. This is
4940 needed because we are not going to push the iterator
4941 on behalf of this display property, so there will be
4942 no pop_it call to do this synchronization for us. */
4943 if (it->bidi_p)
4944 {
4945 it->position = *position;
4946 iterate_out_of_display_property (it);
4947 *position = it->position;
4948 }
4949 return 1;
4950 }
4951 }
4952 else if (!frame_window_p)
4953 return 1;
4954
4955 #ifdef HAVE_WINDOW_SYSTEM
4956 value = XCAR (XCDR (spec));
4957 if (!SYMBOLP (value)
4958 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4959 /* If we return here, POSITION has been advanced
4960 across the text with this property. */
4961 {
4962 if (it && it->bidi_p)
4963 {
4964 it->position = *position;
4965 iterate_out_of_display_property (it);
4966 *position = it->position;
4967 }
4968 return 1;
4969 }
4970
4971 if (it)
4972 {
4973 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4974
4975 if (CONSP (XCDR (XCDR (spec))))
4976 {
4977 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4978 int face_id2 = lookup_derived_face (it->f, face_name,
4979 FRINGE_FACE_ID, 0);
4980 if (face_id2 >= 0)
4981 face_id = face_id2;
4982 }
4983
4984 /* Save current settings of IT so that we can restore them
4985 when we are finished with the glyph property value. */
4986 push_it (it, position);
4987
4988 it->area = TEXT_AREA;
4989 it->what = IT_IMAGE;
4990 it->image_id = -1; /* no image */
4991 it->position = start_pos;
4992 it->object = NILP (object) ? it->w->contents : object;
4993 it->method = GET_FROM_IMAGE;
4994 it->from_overlay = Qnil;
4995 it->face_id = face_id;
4996 it->from_disp_prop_p = 1;
4997
4998 /* Say that we haven't consumed the characters with
4999 `display' property yet. The call to pop_it in
5000 set_iterator_to_next will clean this up. */
5001 *position = start_pos;
5002
5003 if (EQ (XCAR (spec), Qleft_fringe))
5004 {
5005 it->left_user_fringe_bitmap = fringe_bitmap;
5006 it->left_user_fringe_face_id = face_id;
5007 }
5008 else
5009 {
5010 it->right_user_fringe_bitmap = fringe_bitmap;
5011 it->right_user_fringe_face_id = face_id;
5012 }
5013 }
5014 #endif /* HAVE_WINDOW_SYSTEM */
5015 return 1;
5016 }
5017
5018 /* Prepare to handle `((margin left-margin) ...)',
5019 `((margin right-margin) ...)' and `((margin nil) ...)'
5020 prefixes for display specifications. */
5021 location = Qunbound;
5022 if (CONSP (spec) && CONSP (XCAR (spec)))
5023 {
5024 Lisp_Object tem;
5025
5026 value = XCDR (spec);
5027 if (CONSP (value))
5028 value = XCAR (value);
5029
5030 tem = XCAR (spec);
5031 if (EQ (XCAR (tem), Qmargin)
5032 && (tem = XCDR (tem),
5033 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5034 (NILP (tem)
5035 || EQ (tem, Qleft_margin)
5036 || EQ (tem, Qright_margin))))
5037 location = tem;
5038 }
5039
5040 if (EQ (location, Qunbound))
5041 {
5042 location = Qnil;
5043 value = spec;
5044 }
5045
5046 /* After this point, VALUE is the property after any
5047 margin prefix has been stripped. It must be a string,
5048 an image specification, or `(space ...)'.
5049
5050 LOCATION specifies where to display: `left-margin',
5051 `right-margin' or nil. */
5052
5053 valid_p = (STRINGP (value)
5054 #ifdef HAVE_WINDOW_SYSTEM
5055 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5056 && valid_image_p (value))
5057 #endif /* not HAVE_WINDOW_SYSTEM */
5058 || (CONSP (value) && EQ (XCAR (value), Qspace))
5059 #ifdef HAVE_XWIDGETS
5060 || valid_xwidget_spec_p(value)
5061 #endif
5062 );
5063
5064 if (valid_p && !display_replaced_p)
5065 {
5066 int retval = 1;
5067
5068 if (!it)
5069 {
5070 /* Callers need to know whether the display spec is any kind
5071 of `(space ...)' spec that is about to affect text-area
5072 display. */
5073 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5074 retval = 2;
5075 return retval;
5076 }
5077
5078 /* Save current settings of IT so that we can restore them
5079 when we are finished with the glyph property value. */
5080 push_it (it, position);
5081 it->from_overlay = overlay;
5082 it->from_disp_prop_p = 1;
5083
5084 if (NILP (location))
5085 it->area = TEXT_AREA;
5086 else if (EQ (location, Qleft_margin))
5087 it->area = LEFT_MARGIN_AREA;
5088 else
5089 it->area = RIGHT_MARGIN_AREA;
5090
5091 if (STRINGP (value))
5092 {
5093 it->string = value;
5094 it->multibyte_p = STRING_MULTIBYTE (it->string);
5095 it->current.overlay_string_index = -1;
5096 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5097 it->end_charpos = it->string_nchars = SCHARS (it->string);
5098 it->method = GET_FROM_STRING;
5099 it->stop_charpos = 0;
5100 it->prev_stop = 0;
5101 it->base_level_stop = 0;
5102 it->string_from_display_prop_p = 1;
5103 /* Say that we haven't consumed the characters with
5104 `display' property yet. The call to pop_it in
5105 set_iterator_to_next will clean this up. */
5106 if (BUFFERP (object))
5107 *position = start_pos;
5108
5109 /* Force paragraph direction to be that of the parent
5110 object. If the parent object's paragraph direction is
5111 not yet determined, default to L2R. */
5112 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5113 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5114 else
5115 it->paragraph_embedding = L2R;
5116
5117 /* Set up the bidi iterator for this display string. */
5118 if (it->bidi_p)
5119 {
5120 it->bidi_it.string.lstring = it->string;
5121 it->bidi_it.string.s = NULL;
5122 it->bidi_it.string.schars = it->end_charpos;
5123 it->bidi_it.string.bufpos = bufpos;
5124 it->bidi_it.string.from_disp_str = 1;
5125 it->bidi_it.string.unibyte = !it->multibyte_p;
5126 it->bidi_it.w = it->w;
5127 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5128 }
5129 }
5130 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5131 {
5132 it->method = GET_FROM_STRETCH;
5133 it->object = value;
5134 *position = it->position = start_pos;
5135 retval = 1 + (it->area == TEXT_AREA);
5136 }
5137 #ifdef HAVE_XWIDGETS
5138 else if (valid_xwidget_spec_p(value))
5139 {
5140 //printf("handle_single_display_spec: im an xwidget!!\n");
5141 it->what = IT_XWIDGET;
5142 it->method = GET_FROM_XWIDGET;
5143 it->position = start_pos;
5144 it->object = NILP (object) ? it->w->contents : object;
5145 *position = start_pos;
5146
5147 it->xwidget = lookup_xwidget(value);
5148 }
5149 #endif
5150 #ifdef HAVE_WINDOW_SYSTEM
5151 else
5152 {
5153 it->what = IT_IMAGE;
5154 it->image_id = lookup_image (it->f, value);
5155 it->position = start_pos;
5156 it->object = NILP (object) ? it->w->contents : object;
5157 it->method = GET_FROM_IMAGE;
5158
5159 /* Say that we haven't consumed the characters with
5160 `display' property yet. The call to pop_it in
5161 set_iterator_to_next will clean this up. */
5162 *position = start_pos;
5163 }
5164 #endif /* HAVE_WINDOW_SYSTEM */
5165
5166 return retval;
5167 }
5168
5169 /* Invalid property or property not supported. Restore
5170 POSITION to what it was before. */
5171 *position = start_pos;
5172 return 0;
5173 }
5174
5175 /* Check if PROP is a display property value whose text should be
5176 treated as intangible. OVERLAY is the overlay from which PROP
5177 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5178 specify the buffer position covered by PROP. */
5179
5180 int
5181 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5182 ptrdiff_t charpos, ptrdiff_t bytepos)
5183 {
5184 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5185 struct text_pos position;
5186
5187 SET_TEXT_POS (position, charpos, bytepos);
5188 return handle_display_spec (NULL, prop, Qnil, overlay,
5189 &position, charpos, frame_window_p);
5190 }
5191
5192
5193 /* Return 1 if PROP is a display sub-property value containing STRING.
5194
5195 Implementation note: this and the following function are really
5196 special cases of handle_display_spec and
5197 handle_single_display_spec, and should ideally use the same code.
5198 Until they do, these two pairs must be consistent and must be
5199 modified in sync. */
5200
5201 static int
5202 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5203 {
5204 if (EQ (string, prop))
5205 return 1;
5206
5207 /* Skip over `when FORM'. */
5208 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5209 {
5210 prop = XCDR (prop);
5211 if (!CONSP (prop))
5212 return 0;
5213 /* Actually, the condition following `when' should be eval'ed,
5214 like handle_single_display_spec does, and we should return
5215 zero if it evaluates to nil. However, this function is
5216 called only when the buffer was already displayed and some
5217 glyph in the glyph matrix was found to come from a display
5218 string. Therefore, the condition was already evaluated, and
5219 the result was non-nil, otherwise the display string wouldn't
5220 have been displayed and we would have never been called for
5221 this property. Thus, we can skip the evaluation and assume
5222 its result is non-nil. */
5223 prop = XCDR (prop);
5224 }
5225
5226 if (CONSP (prop))
5227 /* Skip over `margin LOCATION'. */
5228 if (EQ (XCAR (prop), Qmargin))
5229 {
5230 prop = XCDR (prop);
5231 if (!CONSP (prop))
5232 return 0;
5233
5234 prop = XCDR (prop);
5235 if (!CONSP (prop))
5236 return 0;
5237 }
5238
5239 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5240 }
5241
5242
5243 /* Return 1 if STRING appears in the `display' property PROP. */
5244
5245 static int
5246 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5247 {
5248 if (CONSP (prop)
5249 && !EQ (XCAR (prop), Qwhen)
5250 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5251 {
5252 /* A list of sub-properties. */
5253 while (CONSP (prop))
5254 {
5255 if (single_display_spec_string_p (XCAR (prop), string))
5256 return 1;
5257 prop = XCDR (prop);
5258 }
5259 }
5260 else if (VECTORP (prop))
5261 {
5262 /* A vector of sub-properties. */
5263 ptrdiff_t i;
5264 for (i = 0; i < ASIZE (prop); ++i)
5265 if (single_display_spec_string_p (AREF (prop, i), string))
5266 return 1;
5267 }
5268 else
5269 return single_display_spec_string_p (prop, string);
5270
5271 return 0;
5272 }
5273
5274 /* Look for STRING in overlays and text properties in the current
5275 buffer, between character positions FROM and TO (excluding TO).
5276 BACK_P non-zero means look back (in this case, TO is supposed to be
5277 less than FROM).
5278 Value is the first character position where STRING was found, or
5279 zero if it wasn't found before hitting TO.
5280
5281 This function may only use code that doesn't eval because it is
5282 called asynchronously from note_mouse_highlight. */
5283
5284 static ptrdiff_t
5285 string_buffer_position_lim (Lisp_Object string,
5286 ptrdiff_t from, ptrdiff_t to, int back_p)
5287 {
5288 Lisp_Object limit, prop, pos;
5289 int found = 0;
5290
5291 pos = make_number (max (from, BEGV));
5292
5293 if (!back_p) /* looking forward */
5294 {
5295 limit = make_number (min (to, ZV));
5296 while (!found && !EQ (pos, limit))
5297 {
5298 prop = Fget_char_property (pos, Qdisplay, Qnil);
5299 if (!NILP (prop) && display_prop_string_p (prop, string))
5300 found = 1;
5301 else
5302 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5303 limit);
5304 }
5305 }
5306 else /* looking back */
5307 {
5308 limit = make_number (max (to, BEGV));
5309 while (!found && !EQ (pos, limit))
5310 {
5311 prop = Fget_char_property (pos, Qdisplay, Qnil);
5312 if (!NILP (prop) && display_prop_string_p (prop, string))
5313 found = 1;
5314 else
5315 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5316 limit);
5317 }
5318 }
5319
5320 return found ? XINT (pos) : 0;
5321 }
5322
5323 /* Determine which buffer position in current buffer STRING comes from.
5324 AROUND_CHARPOS is an approximate position where it could come from.
5325 Value is the buffer position or 0 if it couldn't be determined.
5326
5327 This function is necessary because we don't record buffer positions
5328 in glyphs generated from strings (to keep struct glyph small).
5329 This function may only use code that doesn't eval because it is
5330 called asynchronously from note_mouse_highlight. */
5331
5332 static ptrdiff_t
5333 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5334 {
5335 const int MAX_DISTANCE = 1000;
5336 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5337 around_charpos + MAX_DISTANCE,
5338 0);
5339
5340 if (!found)
5341 found = string_buffer_position_lim (string, around_charpos,
5342 around_charpos - MAX_DISTANCE, 1);
5343 return found;
5344 }
5345
5346
5347 \f
5348 /***********************************************************************
5349 `composition' property
5350 ***********************************************************************/
5351
5352 /* Set up iterator IT from `composition' property at its current
5353 position. Called from handle_stop. */
5354
5355 static enum prop_handled
5356 handle_composition_prop (struct it *it)
5357 {
5358 Lisp_Object prop, string;
5359 ptrdiff_t pos, pos_byte, start, end;
5360
5361 if (STRINGP (it->string))
5362 {
5363 unsigned char *s;
5364
5365 pos = IT_STRING_CHARPOS (*it);
5366 pos_byte = IT_STRING_BYTEPOS (*it);
5367 string = it->string;
5368 s = SDATA (string) + pos_byte;
5369 it->c = STRING_CHAR (s);
5370 }
5371 else
5372 {
5373 pos = IT_CHARPOS (*it);
5374 pos_byte = IT_BYTEPOS (*it);
5375 string = Qnil;
5376 it->c = FETCH_CHAR (pos_byte);
5377 }
5378
5379 /* If there's a valid composition and point is not inside of the
5380 composition (in the case that the composition is from the current
5381 buffer), draw a glyph composed from the composition components. */
5382 if (find_composition (pos, -1, &start, &end, &prop, string)
5383 && composition_valid_p (start, end, prop)
5384 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5385 {
5386 if (start < pos)
5387 /* As we can't handle this situation (perhaps font-lock added
5388 a new composition), we just return here hoping that next
5389 redisplay will detect this composition much earlier. */
5390 return HANDLED_NORMALLY;
5391 if (start != pos)
5392 {
5393 if (STRINGP (it->string))
5394 pos_byte = string_char_to_byte (it->string, start);
5395 else
5396 pos_byte = CHAR_TO_BYTE (start);
5397 }
5398 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5399 prop, string);
5400
5401 if (it->cmp_it.id >= 0)
5402 {
5403 it->cmp_it.ch = -1;
5404 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5405 it->cmp_it.nglyphs = -1;
5406 }
5407 }
5408
5409 return HANDLED_NORMALLY;
5410 }
5411
5412
5413 \f
5414 /***********************************************************************
5415 Overlay strings
5416 ***********************************************************************/
5417
5418 /* The following structure is used to record overlay strings for
5419 later sorting in load_overlay_strings. */
5420
5421 struct overlay_entry
5422 {
5423 Lisp_Object overlay;
5424 Lisp_Object string;
5425 EMACS_INT priority;
5426 int after_string_p;
5427 };
5428
5429
5430 /* Set up iterator IT from overlay strings at its current position.
5431 Called from handle_stop. */
5432
5433 static enum prop_handled
5434 handle_overlay_change (struct it *it)
5435 {
5436 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5437 return HANDLED_RECOMPUTE_PROPS;
5438 else
5439 return HANDLED_NORMALLY;
5440 }
5441
5442
5443 /* Set up the next overlay string for delivery by IT, if there is an
5444 overlay string to deliver. Called by set_iterator_to_next when the
5445 end of the current overlay string is reached. If there are more
5446 overlay strings to display, IT->string and
5447 IT->current.overlay_string_index are set appropriately here.
5448 Otherwise IT->string is set to nil. */
5449
5450 static void
5451 next_overlay_string (struct it *it)
5452 {
5453 ++it->current.overlay_string_index;
5454 if (it->current.overlay_string_index == it->n_overlay_strings)
5455 {
5456 /* No more overlay strings. Restore IT's settings to what
5457 they were before overlay strings were processed, and
5458 continue to deliver from current_buffer. */
5459
5460 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5461 pop_it (it);
5462 eassert (it->sp > 0
5463 || (NILP (it->string)
5464 && it->method == GET_FROM_BUFFER
5465 && it->stop_charpos >= BEGV
5466 && it->stop_charpos <= it->end_charpos));
5467 it->current.overlay_string_index = -1;
5468 it->n_overlay_strings = 0;
5469 it->overlay_strings_charpos = -1;
5470 /* If there's an empty display string on the stack, pop the
5471 stack, to resync the bidi iterator with IT's position. Such
5472 empty strings are pushed onto the stack in
5473 get_overlay_strings_1. */
5474 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5475 pop_it (it);
5476
5477 /* If we're at the end of the buffer, record that we have
5478 processed the overlay strings there already, so that
5479 next_element_from_buffer doesn't try it again. */
5480 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5481 it->overlay_strings_at_end_processed_p = 1;
5482 }
5483 else
5484 {
5485 /* There are more overlay strings to process. If
5486 IT->current.overlay_string_index has advanced to a position
5487 where we must load IT->overlay_strings with more strings, do
5488 it. We must load at the IT->overlay_strings_charpos where
5489 IT->n_overlay_strings was originally computed; when invisible
5490 text is present, this might not be IT_CHARPOS (Bug#7016). */
5491 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5492
5493 if (it->current.overlay_string_index && i == 0)
5494 load_overlay_strings (it, it->overlay_strings_charpos);
5495
5496 /* Initialize IT to deliver display elements from the overlay
5497 string. */
5498 it->string = it->overlay_strings[i];
5499 it->multibyte_p = STRING_MULTIBYTE (it->string);
5500 SET_TEXT_POS (it->current.string_pos, 0, 0);
5501 it->method = GET_FROM_STRING;
5502 it->stop_charpos = 0;
5503 it->end_charpos = SCHARS (it->string);
5504 if (it->cmp_it.stop_pos >= 0)
5505 it->cmp_it.stop_pos = 0;
5506 it->prev_stop = 0;
5507 it->base_level_stop = 0;
5508
5509 /* Set up the bidi iterator for this overlay string. */
5510 if (it->bidi_p)
5511 {
5512 it->bidi_it.string.lstring = it->string;
5513 it->bidi_it.string.s = NULL;
5514 it->bidi_it.string.schars = SCHARS (it->string);
5515 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5516 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5517 it->bidi_it.string.unibyte = !it->multibyte_p;
5518 it->bidi_it.w = it->w;
5519 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5520 }
5521 }
5522
5523 CHECK_IT (it);
5524 }
5525
5526
5527 /* Compare two overlay_entry structures E1 and E2. Used as a
5528 comparison function for qsort in load_overlay_strings. Overlay
5529 strings for the same position are sorted so that
5530
5531 1. All after-strings come in front of before-strings, except
5532 when they come from the same overlay.
5533
5534 2. Within after-strings, strings are sorted so that overlay strings
5535 from overlays with higher priorities come first.
5536
5537 2. Within before-strings, strings are sorted so that overlay
5538 strings from overlays with higher priorities come last.
5539
5540 Value is analogous to strcmp. */
5541
5542
5543 static int
5544 compare_overlay_entries (const void *e1, const void *e2)
5545 {
5546 struct overlay_entry const *entry1 = e1;
5547 struct overlay_entry const *entry2 = e2;
5548 int result;
5549
5550 if (entry1->after_string_p != entry2->after_string_p)
5551 {
5552 /* Let after-strings appear in front of before-strings if
5553 they come from different overlays. */
5554 if (EQ (entry1->overlay, entry2->overlay))
5555 result = entry1->after_string_p ? 1 : -1;
5556 else
5557 result = entry1->after_string_p ? -1 : 1;
5558 }
5559 else if (entry1->priority != entry2->priority)
5560 {
5561 if (entry1->after_string_p)
5562 /* After-strings sorted in order of decreasing priority. */
5563 result = entry2->priority < entry1->priority ? -1 : 1;
5564 else
5565 /* Before-strings sorted in order of increasing priority. */
5566 result = entry1->priority < entry2->priority ? -1 : 1;
5567 }
5568 else
5569 result = 0;
5570
5571 return result;
5572 }
5573
5574
5575 /* Load the vector IT->overlay_strings with overlay strings from IT's
5576 current buffer position, or from CHARPOS if that is > 0. Set
5577 IT->n_overlays to the total number of overlay strings found.
5578
5579 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5580 a time. On entry into load_overlay_strings,
5581 IT->current.overlay_string_index gives the number of overlay
5582 strings that have already been loaded by previous calls to this
5583 function.
5584
5585 IT->add_overlay_start contains an additional overlay start
5586 position to consider for taking overlay strings from, if non-zero.
5587 This position comes into play when the overlay has an `invisible'
5588 property, and both before and after-strings. When we've skipped to
5589 the end of the overlay, because of its `invisible' property, we
5590 nevertheless want its before-string to appear.
5591 IT->add_overlay_start will contain the overlay start position
5592 in this case.
5593
5594 Overlay strings are sorted so that after-string strings come in
5595 front of before-string strings. Within before and after-strings,
5596 strings are sorted by overlay priority. See also function
5597 compare_overlay_entries. */
5598
5599 static void
5600 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5601 {
5602 Lisp_Object overlay, window, str, invisible;
5603 struct Lisp_Overlay *ov;
5604 ptrdiff_t start, end;
5605 ptrdiff_t size = 20;
5606 ptrdiff_t n = 0, i, j;
5607 int invis_p;
5608 struct overlay_entry *entries = alloca (size * sizeof *entries);
5609 USE_SAFE_ALLOCA;
5610
5611 if (charpos <= 0)
5612 charpos = IT_CHARPOS (*it);
5613
5614 /* Append the overlay string STRING of overlay OVERLAY to vector
5615 `entries' which has size `size' and currently contains `n'
5616 elements. AFTER_P non-zero means STRING is an after-string of
5617 OVERLAY. */
5618 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5619 do \
5620 { \
5621 Lisp_Object priority; \
5622 \
5623 if (n == size) \
5624 { \
5625 struct overlay_entry *old = entries; \
5626 SAFE_NALLOCA (entries, 2, size); \
5627 memcpy (entries, old, size * sizeof *entries); \
5628 size *= 2; \
5629 } \
5630 \
5631 entries[n].string = (STRING); \
5632 entries[n].overlay = (OVERLAY); \
5633 priority = Foverlay_get ((OVERLAY), Qpriority); \
5634 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5635 entries[n].after_string_p = (AFTER_P); \
5636 ++n; \
5637 } \
5638 while (0)
5639
5640 /* Process overlay before the overlay center. */
5641 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5642 {
5643 XSETMISC (overlay, ov);
5644 eassert (OVERLAYP (overlay));
5645 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5646 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5647
5648 if (end < charpos)
5649 break;
5650
5651 /* Skip this overlay if it doesn't start or end at IT's current
5652 position. */
5653 if (end != charpos && start != charpos)
5654 continue;
5655
5656 /* Skip this overlay if it doesn't apply to IT->w. */
5657 window = Foverlay_get (overlay, Qwindow);
5658 if (WINDOWP (window) && XWINDOW (window) != it->w)
5659 continue;
5660
5661 /* If the text ``under'' the overlay is invisible, both before-
5662 and after-strings from this overlay are visible; start and
5663 end position are indistinguishable. */
5664 invisible = Foverlay_get (overlay, Qinvisible);
5665 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5666
5667 /* If overlay has a non-empty before-string, record it. */
5668 if ((start == charpos || (end == charpos && invis_p))
5669 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5670 && SCHARS (str))
5671 RECORD_OVERLAY_STRING (overlay, str, 0);
5672
5673 /* If overlay has a non-empty after-string, record it. */
5674 if ((end == charpos || (start == charpos && invis_p))
5675 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5676 && SCHARS (str))
5677 RECORD_OVERLAY_STRING (overlay, str, 1);
5678 }
5679
5680 /* Process overlays after the overlay center. */
5681 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5682 {
5683 XSETMISC (overlay, ov);
5684 eassert (OVERLAYP (overlay));
5685 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5686 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5687
5688 if (start > charpos)
5689 break;
5690
5691 /* Skip this overlay if it doesn't start or end at IT's current
5692 position. */
5693 if (end != charpos && start != charpos)
5694 continue;
5695
5696 /* Skip this overlay if it doesn't apply to IT->w. */
5697 window = Foverlay_get (overlay, Qwindow);
5698 if (WINDOWP (window) && XWINDOW (window) != it->w)
5699 continue;
5700
5701 /* If the text ``under'' the overlay is invisible, it has a zero
5702 dimension, and both before- and after-strings apply. */
5703 invisible = Foverlay_get (overlay, Qinvisible);
5704 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5705
5706 /* If overlay has a non-empty before-string, record it. */
5707 if ((start == charpos || (end == charpos && invis_p))
5708 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5709 && SCHARS (str))
5710 RECORD_OVERLAY_STRING (overlay, str, 0);
5711
5712 /* If overlay has a non-empty after-string, record it. */
5713 if ((end == charpos || (start == charpos && invis_p))
5714 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5715 && SCHARS (str))
5716 RECORD_OVERLAY_STRING (overlay, str, 1);
5717 }
5718
5719 #undef RECORD_OVERLAY_STRING
5720
5721 /* Sort entries. */
5722 if (n > 1)
5723 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5724
5725 /* Record number of overlay strings, and where we computed it. */
5726 it->n_overlay_strings = n;
5727 it->overlay_strings_charpos = charpos;
5728
5729 /* IT->current.overlay_string_index is the number of overlay strings
5730 that have already been consumed by IT. Copy some of the
5731 remaining overlay strings to IT->overlay_strings. */
5732 i = 0;
5733 j = it->current.overlay_string_index;
5734 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5735 {
5736 it->overlay_strings[i] = entries[j].string;
5737 it->string_overlays[i++] = entries[j++].overlay;
5738 }
5739
5740 CHECK_IT (it);
5741 SAFE_FREE ();
5742 }
5743
5744
5745 /* Get the first chunk of overlay strings at IT's current buffer
5746 position, or at CHARPOS if that is > 0. Value is non-zero if at
5747 least one overlay string was found. */
5748
5749 static int
5750 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5751 {
5752 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5753 process. This fills IT->overlay_strings with strings, and sets
5754 IT->n_overlay_strings to the total number of strings to process.
5755 IT->pos.overlay_string_index has to be set temporarily to zero
5756 because load_overlay_strings needs this; it must be set to -1
5757 when no overlay strings are found because a zero value would
5758 indicate a position in the first overlay string. */
5759 it->current.overlay_string_index = 0;
5760 load_overlay_strings (it, charpos);
5761
5762 /* If we found overlay strings, set up IT to deliver display
5763 elements from the first one. Otherwise set up IT to deliver
5764 from current_buffer. */
5765 if (it->n_overlay_strings)
5766 {
5767 /* Make sure we know settings in current_buffer, so that we can
5768 restore meaningful values when we're done with the overlay
5769 strings. */
5770 if (compute_stop_p)
5771 compute_stop_pos (it);
5772 eassert (it->face_id >= 0);
5773
5774 /* Save IT's settings. They are restored after all overlay
5775 strings have been processed. */
5776 eassert (!compute_stop_p || it->sp == 0);
5777
5778 /* When called from handle_stop, there might be an empty display
5779 string loaded. In that case, don't bother saving it. But
5780 don't use this optimization with the bidi iterator, since we
5781 need the corresponding pop_it call to resync the bidi
5782 iterator's position with IT's position, after we are done
5783 with the overlay strings. (The corresponding call to pop_it
5784 in case of an empty display string is in
5785 next_overlay_string.) */
5786 if (!(!it->bidi_p
5787 && STRINGP (it->string) && !SCHARS (it->string)))
5788 push_it (it, NULL);
5789
5790 /* Set up IT to deliver display elements from the first overlay
5791 string. */
5792 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5793 it->string = it->overlay_strings[0];
5794 it->from_overlay = Qnil;
5795 it->stop_charpos = 0;
5796 eassert (STRINGP (it->string));
5797 it->end_charpos = SCHARS (it->string);
5798 it->prev_stop = 0;
5799 it->base_level_stop = 0;
5800 it->multibyte_p = STRING_MULTIBYTE (it->string);
5801 it->method = GET_FROM_STRING;
5802 it->from_disp_prop_p = 0;
5803
5804 /* Force paragraph direction to be that of the parent
5805 buffer. */
5806 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5807 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5808 else
5809 it->paragraph_embedding = L2R;
5810
5811 /* Set up the bidi iterator for this overlay string. */
5812 if (it->bidi_p)
5813 {
5814 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5815
5816 it->bidi_it.string.lstring = it->string;
5817 it->bidi_it.string.s = NULL;
5818 it->bidi_it.string.schars = SCHARS (it->string);
5819 it->bidi_it.string.bufpos = pos;
5820 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5821 it->bidi_it.string.unibyte = !it->multibyte_p;
5822 it->bidi_it.w = it->w;
5823 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5824 }
5825 return 1;
5826 }
5827
5828 it->current.overlay_string_index = -1;
5829 return 0;
5830 }
5831
5832 static int
5833 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5834 {
5835 it->string = Qnil;
5836 it->method = GET_FROM_BUFFER;
5837
5838 (void) get_overlay_strings_1 (it, charpos, 1);
5839
5840 CHECK_IT (it);
5841
5842 /* Value is non-zero if we found at least one overlay string. */
5843 return STRINGP (it->string);
5844 }
5845
5846
5847 \f
5848 /***********************************************************************
5849 Saving and restoring state
5850 ***********************************************************************/
5851
5852 /* Save current settings of IT on IT->stack. Called, for example,
5853 before setting up IT for an overlay string, to be able to restore
5854 IT's settings to what they were after the overlay string has been
5855 processed. If POSITION is non-NULL, it is the position to save on
5856 the stack instead of IT->position. */
5857
5858 static void
5859 push_it (struct it *it, struct text_pos *position)
5860 {
5861 struct iterator_stack_entry *p;
5862
5863 eassert (it->sp < IT_STACK_SIZE);
5864 p = it->stack + it->sp;
5865
5866 p->stop_charpos = it->stop_charpos;
5867 p->prev_stop = it->prev_stop;
5868 p->base_level_stop = it->base_level_stop;
5869 p->cmp_it = it->cmp_it;
5870 eassert (it->face_id >= 0);
5871 p->face_id = it->face_id;
5872 p->string = it->string;
5873 p->method = it->method;
5874 p->from_overlay = it->from_overlay;
5875 switch (p->method)
5876 {
5877 case GET_FROM_IMAGE:
5878 p->u.image.object = it->object;
5879 p->u.image.image_id = it->image_id;
5880 p->u.image.slice = it->slice;
5881 break;
5882 case GET_FROM_STRETCH:
5883 p->u.stretch.object = it->object;
5884 break;
5885 #ifdef HAVE_XWIDGETS
5886 case GET_FROM_XWIDGET:
5887 p->u.xwidget.object = it->object;
5888 break;
5889 #endif
5890 }
5891 p->position = position ? *position : it->position;
5892 p->current = it->current;
5893 p->end_charpos = it->end_charpos;
5894 p->string_nchars = it->string_nchars;
5895 p->area = it->area;
5896 p->multibyte_p = it->multibyte_p;
5897 p->avoid_cursor_p = it->avoid_cursor_p;
5898 p->space_width = it->space_width;
5899 p->font_height = it->font_height;
5900 p->voffset = it->voffset;
5901 p->string_from_display_prop_p = it->string_from_display_prop_p;
5902 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5903 p->display_ellipsis_p = 0;
5904 p->line_wrap = it->line_wrap;
5905 p->bidi_p = it->bidi_p;
5906 p->paragraph_embedding = it->paragraph_embedding;
5907 p->from_disp_prop_p = it->from_disp_prop_p;
5908 ++it->sp;
5909
5910 /* Save the state of the bidi iterator as well. */
5911 if (it->bidi_p)
5912 bidi_push_it (&it->bidi_it);
5913 }
5914
5915 static void
5916 iterate_out_of_display_property (struct it *it)
5917 {
5918 int buffer_p = !STRINGP (it->string);
5919 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5920 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5921
5922 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5923
5924 /* Maybe initialize paragraph direction. If we are at the beginning
5925 of a new paragraph, next_element_from_buffer may not have a
5926 chance to do that. */
5927 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5928 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5929 /* prev_stop can be zero, so check against BEGV as well. */
5930 while (it->bidi_it.charpos >= bob
5931 && it->prev_stop <= it->bidi_it.charpos
5932 && it->bidi_it.charpos < CHARPOS (it->position)
5933 && it->bidi_it.charpos < eob)
5934 bidi_move_to_visually_next (&it->bidi_it);
5935 /* Record the stop_pos we just crossed, for when we cross it
5936 back, maybe. */
5937 if (it->bidi_it.charpos > CHARPOS (it->position))
5938 it->prev_stop = CHARPOS (it->position);
5939 /* If we ended up not where pop_it put us, resync IT's
5940 positional members with the bidi iterator. */
5941 if (it->bidi_it.charpos != CHARPOS (it->position))
5942 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5943 if (buffer_p)
5944 it->current.pos = it->position;
5945 else
5946 it->current.string_pos = it->position;
5947 }
5948
5949 /* Restore IT's settings from IT->stack. Called, for example, when no
5950 more overlay strings must be processed, and we return to delivering
5951 display elements from a buffer, or when the end of a string from a
5952 `display' property is reached and we return to delivering display
5953 elements from an overlay string, or from a buffer. */
5954
5955 static void
5956 pop_it (struct it *it)
5957 {
5958 struct iterator_stack_entry *p;
5959 int from_display_prop = it->from_disp_prop_p;
5960
5961 eassert (it->sp > 0);
5962 --it->sp;
5963 p = it->stack + it->sp;
5964 it->stop_charpos = p->stop_charpos;
5965 it->prev_stop = p->prev_stop;
5966 it->base_level_stop = p->base_level_stop;
5967 it->cmp_it = p->cmp_it;
5968 it->face_id = p->face_id;
5969 it->current = p->current;
5970 it->position = p->position;
5971 it->string = p->string;
5972 it->from_overlay = p->from_overlay;
5973 if (NILP (it->string))
5974 SET_TEXT_POS (it->current.string_pos, -1, -1);
5975 it->method = p->method;
5976 switch (it->method)
5977 {
5978 case GET_FROM_IMAGE:
5979 it->image_id = p->u.image.image_id;
5980 it->object = p->u.image.object;
5981 it->slice = p->u.image.slice;
5982 break;
5983 #ifdef HAVE_XWIDGETS
5984 case GET_FROM_XWIDGET:
5985 it->object = p->u.xwidget.object;
5986 break;
5987 #endif
5988 case GET_FROM_STRETCH:
5989 it->object = p->u.stretch.object;
5990 break;
5991 case GET_FROM_BUFFER:
5992 it->object = it->w->contents;
5993 break;
5994 case GET_FROM_STRING:
5995 it->object = it->string;
5996 break;
5997 case GET_FROM_DISPLAY_VECTOR:
5998 if (it->s)
5999 it->method = GET_FROM_C_STRING;
6000 else if (STRINGP (it->string))
6001 it->method = GET_FROM_STRING;
6002 else
6003 {
6004 it->method = GET_FROM_BUFFER;
6005 it->object = it->w->contents;
6006 }
6007 }
6008 it->end_charpos = p->end_charpos;
6009 it->string_nchars = p->string_nchars;
6010 it->area = p->area;
6011 it->multibyte_p = p->multibyte_p;
6012 it->avoid_cursor_p = p->avoid_cursor_p;
6013 it->space_width = p->space_width;
6014 it->font_height = p->font_height;
6015 it->voffset = p->voffset;
6016 it->string_from_display_prop_p = p->string_from_display_prop_p;
6017 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6018 it->line_wrap = p->line_wrap;
6019 it->bidi_p = p->bidi_p;
6020 it->paragraph_embedding = p->paragraph_embedding;
6021 it->from_disp_prop_p = p->from_disp_prop_p;
6022 if (it->bidi_p)
6023 {
6024 bidi_pop_it (&it->bidi_it);
6025 /* Bidi-iterate until we get out of the portion of text, if any,
6026 covered by a `display' text property or by an overlay with
6027 `display' property. (We cannot just jump there, because the
6028 internal coherency of the bidi iterator state can not be
6029 preserved across such jumps.) We also must determine the
6030 paragraph base direction if the overlay we just processed is
6031 at the beginning of a new paragraph. */
6032 if (from_display_prop
6033 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6034 iterate_out_of_display_property (it);
6035
6036 eassert ((BUFFERP (it->object)
6037 && IT_CHARPOS (*it) == it->bidi_it.charpos
6038 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6039 || (STRINGP (it->object)
6040 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6041 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6042 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6043 }
6044 }
6045
6046
6047 \f
6048 /***********************************************************************
6049 Moving over lines
6050 ***********************************************************************/
6051
6052 /* Set IT's current position to the previous line start. */
6053
6054 static void
6055 back_to_previous_line_start (struct it *it)
6056 {
6057 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6058
6059 DEC_BOTH (cp, bp);
6060 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6061 }
6062
6063
6064 /* Move IT to the next line start.
6065
6066 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6067 we skipped over part of the text (as opposed to moving the iterator
6068 continuously over the text). Otherwise, don't change the value
6069 of *SKIPPED_P.
6070
6071 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6072 iterator on the newline, if it was found.
6073
6074 Newlines may come from buffer text, overlay strings, or strings
6075 displayed via the `display' property. That's the reason we can't
6076 simply use find_newline_no_quit.
6077
6078 Note that this function may not skip over invisible text that is so
6079 because of text properties and immediately follows a newline. If
6080 it would, function reseat_at_next_visible_line_start, when called
6081 from set_iterator_to_next, would effectively make invisible
6082 characters following a newline part of the wrong glyph row, which
6083 leads to wrong cursor motion. */
6084
6085 static int
6086 forward_to_next_line_start (struct it *it, int *skipped_p,
6087 struct bidi_it *bidi_it_prev)
6088 {
6089 ptrdiff_t old_selective;
6090 int newline_found_p, n;
6091 const int MAX_NEWLINE_DISTANCE = 500;
6092
6093 /* If already on a newline, just consume it to avoid unintended
6094 skipping over invisible text below. */
6095 if (it->what == IT_CHARACTER
6096 && it->c == '\n'
6097 && CHARPOS (it->position) == IT_CHARPOS (*it))
6098 {
6099 if (it->bidi_p && bidi_it_prev)
6100 *bidi_it_prev = it->bidi_it;
6101 set_iterator_to_next (it, 0);
6102 it->c = 0;
6103 return 1;
6104 }
6105
6106 /* Don't handle selective display in the following. It's (a)
6107 unnecessary because it's done by the caller, and (b) leads to an
6108 infinite recursion because next_element_from_ellipsis indirectly
6109 calls this function. */
6110 old_selective = it->selective;
6111 it->selective = 0;
6112
6113 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6114 from buffer text. */
6115 for (n = newline_found_p = 0;
6116 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6117 n += STRINGP (it->string) ? 0 : 1)
6118 {
6119 if (!get_next_display_element (it))
6120 return 0;
6121 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6122 if (newline_found_p && it->bidi_p && bidi_it_prev)
6123 *bidi_it_prev = it->bidi_it;
6124 set_iterator_to_next (it, 0);
6125 }
6126
6127 /* If we didn't find a newline near enough, see if we can use a
6128 short-cut. */
6129 if (!newline_found_p)
6130 {
6131 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6132 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6133 1, &bytepos);
6134 Lisp_Object pos;
6135
6136 eassert (!STRINGP (it->string));
6137
6138 /* If there isn't any `display' property in sight, and no
6139 overlays, we can just use the position of the newline in
6140 buffer text. */
6141 if (it->stop_charpos >= limit
6142 || ((pos = Fnext_single_property_change (make_number (start),
6143 Qdisplay, Qnil,
6144 make_number (limit)),
6145 NILP (pos))
6146 && next_overlay_change (start) == ZV))
6147 {
6148 if (!it->bidi_p)
6149 {
6150 IT_CHARPOS (*it) = limit;
6151 IT_BYTEPOS (*it) = bytepos;
6152 }
6153 else
6154 {
6155 struct bidi_it bprev;
6156
6157 /* Help bidi.c avoid expensive searches for display
6158 properties and overlays, by telling it that there are
6159 none up to `limit'. */
6160 if (it->bidi_it.disp_pos < limit)
6161 {
6162 it->bidi_it.disp_pos = limit;
6163 it->bidi_it.disp_prop = 0;
6164 }
6165 do {
6166 bprev = it->bidi_it;
6167 bidi_move_to_visually_next (&it->bidi_it);
6168 } while (it->bidi_it.charpos != limit);
6169 IT_CHARPOS (*it) = limit;
6170 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6171 if (bidi_it_prev)
6172 *bidi_it_prev = bprev;
6173 }
6174 *skipped_p = newline_found_p = 1;
6175 }
6176 else
6177 {
6178 while (get_next_display_element (it)
6179 && !newline_found_p)
6180 {
6181 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6182 if (newline_found_p && it->bidi_p && bidi_it_prev)
6183 *bidi_it_prev = it->bidi_it;
6184 set_iterator_to_next (it, 0);
6185 }
6186 }
6187 }
6188
6189 it->selective = old_selective;
6190 return newline_found_p;
6191 }
6192
6193
6194 /* Set IT's current position to the previous visible line start. Skip
6195 invisible text that is so either due to text properties or due to
6196 selective display. Caution: this does not change IT->current_x and
6197 IT->hpos. */
6198
6199 static void
6200 back_to_previous_visible_line_start (struct it *it)
6201 {
6202 while (IT_CHARPOS (*it) > BEGV)
6203 {
6204 back_to_previous_line_start (it);
6205
6206 if (IT_CHARPOS (*it) <= BEGV)
6207 break;
6208
6209 /* If selective > 0, then lines indented more than its value are
6210 invisible. */
6211 if (it->selective > 0
6212 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6213 it->selective))
6214 continue;
6215
6216 /* Check the newline before point for invisibility. */
6217 {
6218 Lisp_Object prop;
6219 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6220 Qinvisible, it->window);
6221 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6222 continue;
6223 }
6224
6225 if (IT_CHARPOS (*it) <= BEGV)
6226 break;
6227
6228 {
6229 struct it it2;
6230 void *it2data = NULL;
6231 ptrdiff_t pos;
6232 ptrdiff_t beg, end;
6233 Lisp_Object val, overlay;
6234
6235 SAVE_IT (it2, *it, it2data);
6236
6237 /* If newline is part of a composition, continue from start of composition */
6238 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6239 && beg < IT_CHARPOS (*it))
6240 goto replaced;
6241
6242 /* If newline is replaced by a display property, find start of overlay
6243 or interval and continue search from that point. */
6244 pos = --IT_CHARPOS (it2);
6245 --IT_BYTEPOS (it2);
6246 it2.sp = 0;
6247 bidi_unshelve_cache (NULL, 0);
6248 it2.string_from_display_prop_p = 0;
6249 it2.from_disp_prop_p = 0;
6250 if (handle_display_prop (&it2) == HANDLED_RETURN
6251 && !NILP (val = get_char_property_and_overlay
6252 (make_number (pos), Qdisplay, Qnil, &overlay))
6253 && (OVERLAYP (overlay)
6254 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6255 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6256 {
6257 RESTORE_IT (it, it, it2data);
6258 goto replaced;
6259 }
6260
6261 /* Newline is not replaced by anything -- so we are done. */
6262 RESTORE_IT (it, it, it2data);
6263 break;
6264
6265 replaced:
6266 if (beg < BEGV)
6267 beg = BEGV;
6268 IT_CHARPOS (*it) = beg;
6269 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6270 }
6271 }
6272
6273 it->continuation_lines_width = 0;
6274
6275 eassert (IT_CHARPOS (*it) >= BEGV);
6276 eassert (IT_CHARPOS (*it) == BEGV
6277 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6278 CHECK_IT (it);
6279 }
6280
6281
6282 /* Reseat iterator IT at the previous visible line start. Skip
6283 invisible text that is so either due to text properties or due to
6284 selective display. At the end, update IT's overlay information,
6285 face information etc. */
6286
6287 void
6288 reseat_at_previous_visible_line_start (struct it *it)
6289 {
6290 back_to_previous_visible_line_start (it);
6291 reseat (it, it->current.pos, 1);
6292 CHECK_IT (it);
6293 }
6294
6295
6296 /* Reseat iterator IT on the next visible line start in the current
6297 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6298 preceding the line start. Skip over invisible text that is so
6299 because of selective display. Compute faces, overlays etc at the
6300 new position. Note that this function does not skip over text that
6301 is invisible because of text properties. */
6302
6303 static void
6304 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6305 {
6306 int newline_found_p, skipped_p = 0;
6307 struct bidi_it bidi_it_prev;
6308
6309 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6310
6311 /* Skip over lines that are invisible because they are indented
6312 more than the value of IT->selective. */
6313 if (it->selective > 0)
6314 while (IT_CHARPOS (*it) < ZV
6315 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6316 it->selective))
6317 {
6318 eassert (IT_BYTEPOS (*it) == BEGV
6319 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6320 newline_found_p =
6321 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6322 }
6323
6324 /* Position on the newline if that's what's requested. */
6325 if (on_newline_p && newline_found_p)
6326 {
6327 if (STRINGP (it->string))
6328 {
6329 if (IT_STRING_CHARPOS (*it) > 0)
6330 {
6331 if (!it->bidi_p)
6332 {
6333 --IT_STRING_CHARPOS (*it);
6334 --IT_STRING_BYTEPOS (*it);
6335 }
6336 else
6337 {
6338 /* We need to restore the bidi iterator to the state
6339 it had on the newline, and resync the IT's
6340 position with that. */
6341 it->bidi_it = bidi_it_prev;
6342 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6343 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6344 }
6345 }
6346 }
6347 else if (IT_CHARPOS (*it) > BEGV)
6348 {
6349 if (!it->bidi_p)
6350 {
6351 --IT_CHARPOS (*it);
6352 --IT_BYTEPOS (*it);
6353 }
6354 else
6355 {
6356 /* We need to restore the bidi iterator to the state it
6357 had on the newline and resync IT with that. */
6358 it->bidi_it = bidi_it_prev;
6359 IT_CHARPOS (*it) = it->bidi_it.charpos;
6360 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6361 }
6362 reseat (it, it->current.pos, 0);
6363 }
6364 }
6365 else if (skipped_p)
6366 reseat (it, it->current.pos, 0);
6367
6368 CHECK_IT (it);
6369 }
6370
6371
6372 \f
6373 /***********************************************************************
6374 Changing an iterator's position
6375 ***********************************************************************/
6376
6377 /* Change IT's current position to POS in current_buffer. If FORCE_P
6378 is non-zero, always check for text properties at the new position.
6379 Otherwise, text properties are only looked up if POS >=
6380 IT->check_charpos of a property. */
6381
6382 static void
6383 reseat (struct it *it, struct text_pos pos, int force_p)
6384 {
6385 ptrdiff_t original_pos = IT_CHARPOS (*it);
6386
6387 reseat_1 (it, pos, 0);
6388
6389 /* Determine where to check text properties. Avoid doing it
6390 where possible because text property lookup is very expensive. */
6391 if (force_p
6392 || CHARPOS (pos) > it->stop_charpos
6393 || CHARPOS (pos) < original_pos)
6394 {
6395 if (it->bidi_p)
6396 {
6397 /* For bidi iteration, we need to prime prev_stop and
6398 base_level_stop with our best estimations. */
6399 /* Implementation note: Of course, POS is not necessarily a
6400 stop position, so assigning prev_pos to it is a lie; we
6401 should have called compute_stop_backwards. However, if
6402 the current buffer does not include any R2L characters,
6403 that call would be a waste of cycles, because the
6404 iterator will never move back, and thus never cross this
6405 "fake" stop position. So we delay that backward search
6406 until the time we really need it, in next_element_from_buffer. */
6407 if (CHARPOS (pos) != it->prev_stop)
6408 it->prev_stop = CHARPOS (pos);
6409 if (CHARPOS (pos) < it->base_level_stop)
6410 it->base_level_stop = 0; /* meaning it's unknown */
6411 handle_stop (it);
6412 }
6413 else
6414 {
6415 handle_stop (it);
6416 it->prev_stop = it->base_level_stop = 0;
6417 }
6418
6419 }
6420
6421 CHECK_IT (it);
6422 }
6423
6424
6425 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6426 IT->stop_pos to POS, also. */
6427
6428 static void
6429 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6430 {
6431 /* Don't call this function when scanning a C string. */
6432 eassert (it->s == NULL);
6433
6434 /* POS must be a reasonable value. */
6435 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6436
6437 it->current.pos = it->position = pos;
6438 it->end_charpos = ZV;
6439 it->dpvec = NULL;
6440 it->current.dpvec_index = -1;
6441 it->current.overlay_string_index = -1;
6442 IT_STRING_CHARPOS (*it) = -1;
6443 IT_STRING_BYTEPOS (*it) = -1;
6444 it->string = Qnil;
6445 it->method = GET_FROM_BUFFER;
6446 it->object = it->w->contents;
6447 it->area = TEXT_AREA;
6448 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6449 it->sp = 0;
6450 it->string_from_display_prop_p = 0;
6451 it->string_from_prefix_prop_p = 0;
6452
6453 it->from_disp_prop_p = 0;
6454 it->face_before_selective_p = 0;
6455 if (it->bidi_p)
6456 {
6457 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6458 &it->bidi_it);
6459 bidi_unshelve_cache (NULL, 0);
6460 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6461 it->bidi_it.string.s = NULL;
6462 it->bidi_it.string.lstring = Qnil;
6463 it->bidi_it.string.bufpos = 0;
6464 it->bidi_it.string.unibyte = 0;
6465 it->bidi_it.w = it->w;
6466 }
6467
6468 if (set_stop_p)
6469 {
6470 it->stop_charpos = CHARPOS (pos);
6471 it->base_level_stop = CHARPOS (pos);
6472 }
6473 /* This make the information stored in it->cmp_it invalidate. */
6474 it->cmp_it.id = -1;
6475 }
6476
6477
6478 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6479 If S is non-null, it is a C string to iterate over. Otherwise,
6480 STRING gives a Lisp string to iterate over.
6481
6482 If PRECISION > 0, don't return more then PRECISION number of
6483 characters from the string.
6484
6485 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6486 characters have been returned. FIELD_WIDTH < 0 means an infinite
6487 field width.
6488
6489 MULTIBYTE = 0 means disable processing of multibyte characters,
6490 MULTIBYTE > 0 means enable it,
6491 MULTIBYTE < 0 means use IT->multibyte_p.
6492
6493 IT must be initialized via a prior call to init_iterator before
6494 calling this function. */
6495
6496 static void
6497 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6498 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6499 int multibyte)
6500 {
6501 /* No region in strings. */
6502 it->region_beg_charpos = it->region_end_charpos = -1;
6503
6504 /* No text property checks performed by default, but see below. */
6505 it->stop_charpos = -1;
6506
6507 /* Set iterator position and end position. */
6508 memset (&it->current, 0, sizeof it->current);
6509 it->current.overlay_string_index = -1;
6510 it->current.dpvec_index = -1;
6511 eassert (charpos >= 0);
6512
6513 /* If STRING is specified, use its multibyteness, otherwise use the
6514 setting of MULTIBYTE, if specified. */
6515 if (multibyte >= 0)
6516 it->multibyte_p = multibyte > 0;
6517
6518 /* Bidirectional reordering of strings is controlled by the default
6519 value of bidi-display-reordering. Don't try to reorder while
6520 loading loadup.el, as the necessary character property tables are
6521 not yet available. */
6522 it->bidi_p =
6523 NILP (Vpurify_flag)
6524 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6525
6526 if (s == NULL)
6527 {
6528 eassert (STRINGP (string));
6529 it->string = string;
6530 it->s = NULL;
6531 it->end_charpos = it->string_nchars = SCHARS (string);
6532 it->method = GET_FROM_STRING;
6533 it->current.string_pos = string_pos (charpos, string);
6534
6535 if (it->bidi_p)
6536 {
6537 it->bidi_it.string.lstring = string;
6538 it->bidi_it.string.s = NULL;
6539 it->bidi_it.string.schars = it->end_charpos;
6540 it->bidi_it.string.bufpos = 0;
6541 it->bidi_it.string.from_disp_str = 0;
6542 it->bidi_it.string.unibyte = !it->multibyte_p;
6543 it->bidi_it.w = it->w;
6544 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6545 FRAME_WINDOW_P (it->f), &it->bidi_it);
6546 }
6547 }
6548 else
6549 {
6550 it->s = (const unsigned char *) s;
6551 it->string = Qnil;
6552
6553 /* Note that we use IT->current.pos, not it->current.string_pos,
6554 for displaying C strings. */
6555 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6556 if (it->multibyte_p)
6557 {
6558 it->current.pos = c_string_pos (charpos, s, 1);
6559 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6560 }
6561 else
6562 {
6563 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6564 it->end_charpos = it->string_nchars = strlen (s);
6565 }
6566
6567 if (it->bidi_p)
6568 {
6569 it->bidi_it.string.lstring = Qnil;
6570 it->bidi_it.string.s = (const unsigned char *) s;
6571 it->bidi_it.string.schars = it->end_charpos;
6572 it->bidi_it.string.bufpos = 0;
6573 it->bidi_it.string.from_disp_str = 0;
6574 it->bidi_it.string.unibyte = !it->multibyte_p;
6575 it->bidi_it.w = it->w;
6576 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6577 &it->bidi_it);
6578 }
6579 it->method = GET_FROM_C_STRING;
6580 }
6581
6582 /* PRECISION > 0 means don't return more than PRECISION characters
6583 from the string. */
6584 if (precision > 0 && it->end_charpos - charpos > precision)
6585 {
6586 it->end_charpos = it->string_nchars = charpos + precision;
6587 if (it->bidi_p)
6588 it->bidi_it.string.schars = it->end_charpos;
6589 }
6590
6591 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6592 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6593 FIELD_WIDTH < 0 means infinite field width. This is useful for
6594 padding with `-' at the end of a mode line. */
6595 if (field_width < 0)
6596 field_width = INFINITY;
6597 /* Implementation note: We deliberately don't enlarge
6598 it->bidi_it.string.schars here to fit it->end_charpos, because
6599 the bidi iterator cannot produce characters out of thin air. */
6600 if (field_width > it->end_charpos - charpos)
6601 it->end_charpos = charpos + field_width;
6602
6603 /* Use the standard display table for displaying strings. */
6604 if (DISP_TABLE_P (Vstandard_display_table))
6605 it->dp = XCHAR_TABLE (Vstandard_display_table);
6606
6607 it->stop_charpos = charpos;
6608 it->prev_stop = charpos;
6609 it->base_level_stop = 0;
6610 if (it->bidi_p)
6611 {
6612 it->bidi_it.first_elt = 1;
6613 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6614 it->bidi_it.disp_pos = -1;
6615 }
6616 if (s == NULL && it->multibyte_p)
6617 {
6618 ptrdiff_t endpos = SCHARS (it->string);
6619 if (endpos > it->end_charpos)
6620 endpos = it->end_charpos;
6621 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6622 it->string);
6623 }
6624 CHECK_IT (it);
6625 }
6626
6627
6628 \f
6629 /***********************************************************************
6630 Iteration
6631 ***********************************************************************/
6632
6633 /* Map enum it_method value to corresponding next_element_from_* function. */
6634
6635 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6636 {
6637 next_element_from_buffer,
6638 next_element_from_display_vector,
6639 next_element_from_string,
6640 next_element_from_c_string,
6641 next_element_from_image,
6642 next_element_from_stretch
6643 #ifdef HAVE_XWIDGETS
6644 ,next_element_from_xwidget
6645 #endif
6646 };
6647
6648 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6649
6650
6651 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6652 (possibly with the following characters). */
6653
6654 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6655 ((IT)->cmp_it.id >= 0 \
6656 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6657 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6658 END_CHARPOS, (IT)->w, \
6659 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6660 (IT)->string)))
6661
6662
6663 /* Lookup the char-table Vglyphless_char_display for character C (-1
6664 if we want information for no-font case), and return the display
6665 method symbol. By side-effect, update it->what and
6666 it->glyphless_method. This function is called from
6667 get_next_display_element for each character element, and from
6668 x_produce_glyphs when no suitable font was found. */
6669
6670 Lisp_Object
6671 lookup_glyphless_char_display (int c, struct it *it)
6672 {
6673 Lisp_Object glyphless_method = Qnil;
6674
6675 if (CHAR_TABLE_P (Vglyphless_char_display)
6676 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6677 {
6678 if (c >= 0)
6679 {
6680 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6681 if (CONSP (glyphless_method))
6682 glyphless_method = FRAME_WINDOW_P (it->f)
6683 ? XCAR (glyphless_method)
6684 : XCDR (glyphless_method);
6685 }
6686 else
6687 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6688 }
6689
6690 retry:
6691 if (NILP (glyphless_method))
6692 {
6693 if (c >= 0)
6694 /* The default is to display the character by a proper font. */
6695 return Qnil;
6696 /* The default for the no-font case is to display an empty box. */
6697 glyphless_method = Qempty_box;
6698 }
6699 if (EQ (glyphless_method, Qzero_width))
6700 {
6701 if (c >= 0)
6702 return glyphless_method;
6703 /* This method can't be used for the no-font case. */
6704 glyphless_method = Qempty_box;
6705 }
6706 if (EQ (glyphless_method, Qthin_space))
6707 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6708 else if (EQ (glyphless_method, Qempty_box))
6709 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6710 else if (EQ (glyphless_method, Qhex_code))
6711 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6712 else if (STRINGP (glyphless_method))
6713 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6714 else
6715 {
6716 /* Invalid value. We use the default method. */
6717 glyphless_method = Qnil;
6718 goto retry;
6719 }
6720 it->what = IT_GLYPHLESS;
6721 return glyphless_method;
6722 }
6723
6724 /* Load IT's display element fields with information about the next
6725 display element from the current position of IT. Value is zero if
6726 end of buffer (or C string) is reached. */
6727
6728 static struct frame *last_escape_glyph_frame = NULL;
6729 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6730 static int last_escape_glyph_merged_face_id = 0;
6731
6732 struct frame *last_glyphless_glyph_frame = NULL;
6733 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6734 int last_glyphless_glyph_merged_face_id = 0;
6735
6736 static int
6737 get_next_display_element (struct it *it)
6738 {
6739 /* Non-zero means that we found a display element. Zero means that
6740 we hit the end of what we iterate over. Performance note: the
6741 function pointer `method' used here turns out to be faster than
6742 using a sequence of if-statements. */
6743 int success_p;
6744
6745 get_next:
6746 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6747
6748 if (it->what == IT_CHARACTER)
6749 {
6750 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6751 and only if (a) the resolved directionality of that character
6752 is R..." */
6753 /* FIXME: Do we need an exception for characters from display
6754 tables? */
6755 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6756 it->c = bidi_mirror_char (it->c);
6757 /* Map via display table or translate control characters.
6758 IT->c, IT->len etc. have been set to the next character by
6759 the function call above. If we have a display table, and it
6760 contains an entry for IT->c, translate it. Don't do this if
6761 IT->c itself comes from a display table, otherwise we could
6762 end up in an infinite recursion. (An alternative could be to
6763 count the recursion depth of this function and signal an
6764 error when a certain maximum depth is reached.) Is it worth
6765 it? */
6766 if (success_p && it->dpvec == NULL)
6767 {
6768 Lisp_Object dv;
6769 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6770 int nonascii_space_p = 0;
6771 int nonascii_hyphen_p = 0;
6772 int c = it->c; /* This is the character to display. */
6773
6774 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6775 {
6776 eassert (SINGLE_BYTE_CHAR_P (c));
6777 if (unibyte_display_via_language_environment)
6778 {
6779 c = DECODE_CHAR (unibyte, c);
6780 if (c < 0)
6781 c = BYTE8_TO_CHAR (it->c);
6782 }
6783 else
6784 c = BYTE8_TO_CHAR (it->c);
6785 }
6786
6787 if (it->dp
6788 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6789 VECTORP (dv)))
6790 {
6791 struct Lisp_Vector *v = XVECTOR (dv);
6792
6793 /* Return the first character from the display table
6794 entry, if not empty. If empty, don't display the
6795 current character. */
6796 if (v->header.size)
6797 {
6798 it->dpvec_char_len = it->len;
6799 it->dpvec = v->contents;
6800 it->dpend = v->contents + v->header.size;
6801 it->current.dpvec_index = 0;
6802 it->dpvec_face_id = -1;
6803 it->saved_face_id = it->face_id;
6804 it->method = GET_FROM_DISPLAY_VECTOR;
6805 it->ellipsis_p = 0;
6806 }
6807 else
6808 {
6809 set_iterator_to_next (it, 0);
6810 }
6811 goto get_next;
6812 }
6813
6814 if (! NILP (lookup_glyphless_char_display (c, it)))
6815 {
6816 if (it->what == IT_GLYPHLESS)
6817 goto done;
6818 /* Don't display this character. */
6819 set_iterator_to_next (it, 0);
6820 goto get_next;
6821 }
6822
6823 /* If `nobreak-char-display' is non-nil, we display
6824 non-ASCII spaces and hyphens specially. */
6825 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6826 {
6827 if (c == 0xA0)
6828 nonascii_space_p = 1;
6829 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6830 nonascii_hyphen_p = 1;
6831 }
6832
6833 /* Translate control characters into `\003' or `^C' form.
6834 Control characters coming from a display table entry are
6835 currently not translated because we use IT->dpvec to hold
6836 the translation. This could easily be changed but I
6837 don't believe that it is worth doing.
6838
6839 The characters handled by `nobreak-char-display' must be
6840 translated too.
6841
6842 Non-printable characters and raw-byte characters are also
6843 translated to octal form. */
6844 if (((c < ' ' || c == 127) /* ASCII control chars */
6845 ? (it->area != TEXT_AREA
6846 /* In mode line, treat \n, \t like other crl chars. */
6847 || (c != '\t'
6848 && it->glyph_row
6849 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6850 || (c != '\n' && c != '\t'))
6851 : (nonascii_space_p
6852 || nonascii_hyphen_p
6853 || CHAR_BYTE8_P (c)
6854 || ! CHAR_PRINTABLE_P (c))))
6855 {
6856 /* C is a control character, non-ASCII space/hyphen,
6857 raw-byte, or a non-printable character which must be
6858 displayed either as '\003' or as `^C' where the '\\'
6859 and '^' can be defined in the display table. Fill
6860 IT->ctl_chars with glyphs for what we have to
6861 display. Then, set IT->dpvec to these glyphs. */
6862 Lisp_Object gc;
6863 int ctl_len;
6864 int face_id;
6865 int lface_id = 0;
6866 int escape_glyph;
6867
6868 /* Handle control characters with ^. */
6869
6870 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6871 {
6872 int g;
6873
6874 g = '^'; /* default glyph for Control */
6875 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6876 if (it->dp
6877 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6878 {
6879 g = GLYPH_CODE_CHAR (gc);
6880 lface_id = GLYPH_CODE_FACE (gc);
6881 }
6882 if (lface_id)
6883 {
6884 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6885 }
6886 else if (it->f == last_escape_glyph_frame
6887 && it->face_id == last_escape_glyph_face_id)
6888 {
6889 face_id = last_escape_glyph_merged_face_id;
6890 }
6891 else
6892 {
6893 /* Merge the escape-glyph face into the current face. */
6894 face_id = merge_faces (it->f, Qescape_glyph, 0,
6895 it->face_id);
6896 last_escape_glyph_frame = it->f;
6897 last_escape_glyph_face_id = it->face_id;
6898 last_escape_glyph_merged_face_id = face_id;
6899 }
6900
6901 XSETINT (it->ctl_chars[0], g);
6902 XSETINT (it->ctl_chars[1], c ^ 0100);
6903 ctl_len = 2;
6904 goto display_control;
6905 }
6906
6907 /* Handle non-ascii space in the mode where it only gets
6908 highlighting. */
6909
6910 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6911 {
6912 /* Merge `nobreak-space' into the current face. */
6913 face_id = merge_faces (it->f, Qnobreak_space, 0,
6914 it->face_id);
6915 XSETINT (it->ctl_chars[0], ' ');
6916 ctl_len = 1;
6917 goto display_control;
6918 }
6919
6920 /* Handle sequences that start with the "escape glyph". */
6921
6922 /* the default escape glyph is \. */
6923 escape_glyph = '\\';
6924
6925 if (it->dp
6926 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6927 {
6928 escape_glyph = GLYPH_CODE_CHAR (gc);
6929 lface_id = GLYPH_CODE_FACE (gc);
6930 }
6931 if (lface_id)
6932 {
6933 /* The display table specified a face.
6934 Merge it into face_id and also into escape_glyph. */
6935 face_id = merge_faces (it->f, Qt, lface_id,
6936 it->face_id);
6937 }
6938 else if (it->f == last_escape_glyph_frame
6939 && it->face_id == last_escape_glyph_face_id)
6940 {
6941 face_id = last_escape_glyph_merged_face_id;
6942 }
6943 else
6944 {
6945 /* Merge the escape-glyph face into the current face. */
6946 face_id = merge_faces (it->f, Qescape_glyph, 0,
6947 it->face_id);
6948 last_escape_glyph_frame = it->f;
6949 last_escape_glyph_face_id = it->face_id;
6950 last_escape_glyph_merged_face_id = face_id;
6951 }
6952
6953 /* Draw non-ASCII hyphen with just highlighting: */
6954
6955 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6956 {
6957 XSETINT (it->ctl_chars[0], '-');
6958 ctl_len = 1;
6959 goto display_control;
6960 }
6961
6962 /* Draw non-ASCII space/hyphen with escape glyph: */
6963
6964 if (nonascii_space_p || nonascii_hyphen_p)
6965 {
6966 XSETINT (it->ctl_chars[0], escape_glyph);
6967 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6968 ctl_len = 2;
6969 goto display_control;
6970 }
6971
6972 {
6973 char str[10];
6974 int len, i;
6975
6976 if (CHAR_BYTE8_P (c))
6977 /* Display \200 instead of \17777600. */
6978 c = CHAR_TO_BYTE8 (c);
6979 len = sprintf (str, "%03o", c);
6980
6981 XSETINT (it->ctl_chars[0], escape_glyph);
6982 for (i = 0; i < len; i++)
6983 XSETINT (it->ctl_chars[i + 1], str[i]);
6984 ctl_len = len + 1;
6985 }
6986
6987 display_control:
6988 /* Set up IT->dpvec and return first character from it. */
6989 it->dpvec_char_len = it->len;
6990 it->dpvec = it->ctl_chars;
6991 it->dpend = it->dpvec + ctl_len;
6992 it->current.dpvec_index = 0;
6993 it->dpvec_face_id = face_id;
6994 it->saved_face_id = it->face_id;
6995 it->method = GET_FROM_DISPLAY_VECTOR;
6996 it->ellipsis_p = 0;
6997 goto get_next;
6998 }
6999 it->char_to_display = c;
7000 }
7001 else if (success_p)
7002 {
7003 it->char_to_display = it->c;
7004 }
7005 }
7006
7007 /* Adjust face id for a multibyte character. There are no multibyte
7008 character in unibyte text. */
7009 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7010 && it->multibyte_p
7011 && success_p
7012 && FRAME_WINDOW_P (it->f))
7013 {
7014 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7015
7016 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7017 {
7018 /* Automatic composition with glyph-string. */
7019 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7020
7021 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7022 }
7023 else
7024 {
7025 ptrdiff_t pos = (it->s ? -1
7026 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7027 : IT_CHARPOS (*it));
7028 int c;
7029
7030 if (it->what == IT_CHARACTER)
7031 c = it->char_to_display;
7032 else
7033 {
7034 struct composition *cmp = composition_table[it->cmp_it.id];
7035 int i;
7036
7037 c = ' ';
7038 for (i = 0; i < cmp->glyph_len; i++)
7039 /* TAB in a composition means display glyphs with
7040 padding space on the left or right. */
7041 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7042 break;
7043 }
7044 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7045 }
7046 }
7047
7048 done:
7049 /* Is this character the last one of a run of characters with
7050 box? If yes, set IT->end_of_box_run_p to 1. */
7051 if (it->face_box_p
7052 && it->s == NULL)
7053 {
7054 if (it->method == GET_FROM_STRING && it->sp)
7055 {
7056 int face_id = underlying_face_id (it);
7057 struct face *face = FACE_FROM_ID (it->f, face_id);
7058
7059 if (face)
7060 {
7061 if (face->box == FACE_NO_BOX)
7062 {
7063 /* If the box comes from face properties in a
7064 display string, check faces in that string. */
7065 int string_face_id = face_after_it_pos (it);
7066 it->end_of_box_run_p
7067 = (FACE_FROM_ID (it->f, string_face_id)->box
7068 == FACE_NO_BOX);
7069 }
7070 /* Otherwise, the box comes from the underlying face.
7071 If this is the last string character displayed, check
7072 the next buffer location. */
7073 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7074 && (it->current.overlay_string_index
7075 == it->n_overlay_strings - 1))
7076 {
7077 ptrdiff_t ignore;
7078 int next_face_id;
7079 struct text_pos pos = it->current.pos;
7080 INC_TEXT_POS (pos, it->multibyte_p);
7081
7082 next_face_id = face_at_buffer_position
7083 (it->w, CHARPOS (pos), it->region_beg_charpos,
7084 it->region_end_charpos, &ignore,
7085 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7086 -1);
7087 it->end_of_box_run_p
7088 = (FACE_FROM_ID (it->f, next_face_id)->box
7089 == FACE_NO_BOX);
7090 }
7091 }
7092 }
7093 /* next_element_from_display_vector sets this flag according to
7094 faces of the display vector glyphs, see there. */
7095 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7096 {
7097 int face_id = face_after_it_pos (it);
7098 it->end_of_box_run_p
7099 = (face_id != it->face_id
7100 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7101 }
7102 }
7103 /* If we reached the end of the object we've been iterating (e.g., a
7104 display string or an overlay string), and there's something on
7105 IT->stack, proceed with what's on the stack. It doesn't make
7106 sense to return zero if there's unprocessed stuff on the stack,
7107 because otherwise that stuff will never be displayed. */
7108 if (!success_p && it->sp > 0)
7109 {
7110 set_iterator_to_next (it, 0);
7111 success_p = get_next_display_element (it);
7112 }
7113
7114 /* Value is 0 if end of buffer or string reached. */
7115 return success_p;
7116 }
7117
7118
7119 /* Move IT to the next display element.
7120
7121 RESEAT_P non-zero means if called on a newline in buffer text,
7122 skip to the next visible line start.
7123
7124 Functions get_next_display_element and set_iterator_to_next are
7125 separate because I find this arrangement easier to handle than a
7126 get_next_display_element function that also increments IT's
7127 position. The way it is we can first look at an iterator's current
7128 display element, decide whether it fits on a line, and if it does,
7129 increment the iterator position. The other way around we probably
7130 would either need a flag indicating whether the iterator has to be
7131 incremented the next time, or we would have to implement a
7132 decrement position function which would not be easy to write. */
7133
7134 void
7135 set_iterator_to_next (struct it *it, int reseat_p)
7136 {
7137 /* Reset flags indicating start and end of a sequence of characters
7138 with box. Reset them at the start of this function because
7139 moving the iterator to a new position might set them. */
7140 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7141
7142 switch (it->method)
7143 {
7144 case GET_FROM_BUFFER:
7145 /* The current display element of IT is a character from
7146 current_buffer. Advance in the buffer, and maybe skip over
7147 invisible lines that are so because of selective display. */
7148 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7149 reseat_at_next_visible_line_start (it, 0);
7150 else if (it->cmp_it.id >= 0)
7151 {
7152 /* We are currently getting glyphs from a composition. */
7153 int i;
7154
7155 if (! it->bidi_p)
7156 {
7157 IT_CHARPOS (*it) += it->cmp_it.nchars;
7158 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7159 if (it->cmp_it.to < it->cmp_it.nglyphs)
7160 {
7161 it->cmp_it.from = it->cmp_it.to;
7162 }
7163 else
7164 {
7165 it->cmp_it.id = -1;
7166 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7167 IT_BYTEPOS (*it),
7168 it->end_charpos, Qnil);
7169 }
7170 }
7171 else if (! it->cmp_it.reversed_p)
7172 {
7173 /* Composition created while scanning forward. */
7174 /* Update IT's char/byte positions to point to the first
7175 character of the next grapheme cluster, or to the
7176 character visually after the current composition. */
7177 for (i = 0; i < it->cmp_it.nchars; i++)
7178 bidi_move_to_visually_next (&it->bidi_it);
7179 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7180 IT_CHARPOS (*it) = it->bidi_it.charpos;
7181
7182 if (it->cmp_it.to < it->cmp_it.nglyphs)
7183 {
7184 /* Proceed to the next grapheme cluster. */
7185 it->cmp_it.from = it->cmp_it.to;
7186 }
7187 else
7188 {
7189 /* No more grapheme clusters in this composition.
7190 Find the next stop position. */
7191 ptrdiff_t stop = it->end_charpos;
7192 if (it->bidi_it.scan_dir < 0)
7193 /* Now we are scanning backward and don't know
7194 where to stop. */
7195 stop = -1;
7196 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7197 IT_BYTEPOS (*it), stop, Qnil);
7198 }
7199 }
7200 else
7201 {
7202 /* Composition created while scanning backward. */
7203 /* Update IT's char/byte positions to point to the last
7204 character of the previous grapheme cluster, or the
7205 character visually after the current composition. */
7206 for (i = 0; i < it->cmp_it.nchars; i++)
7207 bidi_move_to_visually_next (&it->bidi_it);
7208 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7209 IT_CHARPOS (*it) = it->bidi_it.charpos;
7210 if (it->cmp_it.from > 0)
7211 {
7212 /* Proceed to the previous grapheme cluster. */
7213 it->cmp_it.to = it->cmp_it.from;
7214 }
7215 else
7216 {
7217 /* No more grapheme clusters in this composition.
7218 Find the next stop position. */
7219 ptrdiff_t stop = it->end_charpos;
7220 if (it->bidi_it.scan_dir < 0)
7221 /* Now we are scanning backward and don't know
7222 where to stop. */
7223 stop = -1;
7224 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7225 IT_BYTEPOS (*it), stop, Qnil);
7226 }
7227 }
7228 }
7229 else
7230 {
7231 eassert (it->len != 0);
7232
7233 if (!it->bidi_p)
7234 {
7235 IT_BYTEPOS (*it) += it->len;
7236 IT_CHARPOS (*it) += 1;
7237 }
7238 else
7239 {
7240 int prev_scan_dir = it->bidi_it.scan_dir;
7241 /* If this is a new paragraph, determine its base
7242 direction (a.k.a. its base embedding level). */
7243 if (it->bidi_it.new_paragraph)
7244 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7245 bidi_move_to_visually_next (&it->bidi_it);
7246 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7247 IT_CHARPOS (*it) = it->bidi_it.charpos;
7248 if (prev_scan_dir != it->bidi_it.scan_dir)
7249 {
7250 /* As the scan direction was changed, we must
7251 re-compute the stop position for composition. */
7252 ptrdiff_t stop = it->end_charpos;
7253 if (it->bidi_it.scan_dir < 0)
7254 stop = -1;
7255 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7256 IT_BYTEPOS (*it), stop, Qnil);
7257 }
7258 }
7259 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7260 }
7261 break;
7262
7263 case GET_FROM_C_STRING:
7264 /* Current display element of IT is from a C string. */
7265 if (!it->bidi_p
7266 /* If the string position is beyond string's end, it means
7267 next_element_from_c_string is padding the string with
7268 blanks, in which case we bypass the bidi iterator,
7269 because it cannot deal with such virtual characters. */
7270 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7271 {
7272 IT_BYTEPOS (*it) += it->len;
7273 IT_CHARPOS (*it) += 1;
7274 }
7275 else
7276 {
7277 bidi_move_to_visually_next (&it->bidi_it);
7278 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7279 IT_CHARPOS (*it) = it->bidi_it.charpos;
7280 }
7281 break;
7282
7283 case GET_FROM_DISPLAY_VECTOR:
7284 /* Current display element of IT is from a display table entry.
7285 Advance in the display table definition. Reset it to null if
7286 end reached, and continue with characters from buffers/
7287 strings. */
7288 ++it->current.dpvec_index;
7289
7290 /* Restore face of the iterator to what they were before the
7291 display vector entry (these entries may contain faces). */
7292 it->face_id = it->saved_face_id;
7293
7294 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7295 {
7296 int recheck_faces = it->ellipsis_p;
7297
7298 if (it->s)
7299 it->method = GET_FROM_C_STRING;
7300 else if (STRINGP (it->string))
7301 it->method = GET_FROM_STRING;
7302 else
7303 {
7304 it->method = GET_FROM_BUFFER;
7305 it->object = it->w->contents;
7306 }
7307
7308 it->dpvec = NULL;
7309 it->current.dpvec_index = -1;
7310
7311 /* Skip over characters which were displayed via IT->dpvec. */
7312 if (it->dpvec_char_len < 0)
7313 reseat_at_next_visible_line_start (it, 1);
7314 else if (it->dpvec_char_len > 0)
7315 {
7316 if (it->method == GET_FROM_STRING
7317 && it->current.overlay_string_index >= 0
7318 && it->n_overlay_strings > 0)
7319 it->ignore_overlay_strings_at_pos_p = 1;
7320 it->len = it->dpvec_char_len;
7321 set_iterator_to_next (it, reseat_p);
7322 }
7323
7324 /* Maybe recheck faces after display vector */
7325 if (recheck_faces)
7326 it->stop_charpos = IT_CHARPOS (*it);
7327 }
7328 break;
7329
7330 case GET_FROM_STRING:
7331 /* Current display element is a character from a Lisp string. */
7332 eassert (it->s == NULL && STRINGP (it->string));
7333 /* Don't advance past string end. These conditions are true
7334 when set_iterator_to_next is called at the end of
7335 get_next_display_element, in which case the Lisp string is
7336 already exhausted, and all we want is pop the iterator
7337 stack. */
7338 if (it->current.overlay_string_index >= 0)
7339 {
7340 /* This is an overlay string, so there's no padding with
7341 spaces, and the number of characters in the string is
7342 where the string ends. */
7343 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7344 goto consider_string_end;
7345 }
7346 else
7347 {
7348 /* Not an overlay string. There could be padding, so test
7349 against it->end_charpos . */
7350 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7351 goto consider_string_end;
7352 }
7353 if (it->cmp_it.id >= 0)
7354 {
7355 int i;
7356
7357 if (! it->bidi_p)
7358 {
7359 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7360 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7361 if (it->cmp_it.to < it->cmp_it.nglyphs)
7362 it->cmp_it.from = it->cmp_it.to;
7363 else
7364 {
7365 it->cmp_it.id = -1;
7366 composition_compute_stop_pos (&it->cmp_it,
7367 IT_STRING_CHARPOS (*it),
7368 IT_STRING_BYTEPOS (*it),
7369 it->end_charpos, it->string);
7370 }
7371 }
7372 else if (! it->cmp_it.reversed_p)
7373 {
7374 for (i = 0; i < it->cmp_it.nchars; i++)
7375 bidi_move_to_visually_next (&it->bidi_it);
7376 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7377 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7378
7379 if (it->cmp_it.to < it->cmp_it.nglyphs)
7380 it->cmp_it.from = it->cmp_it.to;
7381 else
7382 {
7383 ptrdiff_t stop = it->end_charpos;
7384 if (it->bidi_it.scan_dir < 0)
7385 stop = -1;
7386 composition_compute_stop_pos (&it->cmp_it,
7387 IT_STRING_CHARPOS (*it),
7388 IT_STRING_BYTEPOS (*it), stop,
7389 it->string);
7390 }
7391 }
7392 else
7393 {
7394 for (i = 0; i < it->cmp_it.nchars; i++)
7395 bidi_move_to_visually_next (&it->bidi_it);
7396 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7397 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7398 if (it->cmp_it.from > 0)
7399 it->cmp_it.to = it->cmp_it.from;
7400 else
7401 {
7402 ptrdiff_t stop = it->end_charpos;
7403 if (it->bidi_it.scan_dir < 0)
7404 stop = -1;
7405 composition_compute_stop_pos (&it->cmp_it,
7406 IT_STRING_CHARPOS (*it),
7407 IT_STRING_BYTEPOS (*it), stop,
7408 it->string);
7409 }
7410 }
7411 }
7412 else
7413 {
7414 if (!it->bidi_p
7415 /* If the string position is beyond string's end, it
7416 means next_element_from_string is padding the string
7417 with blanks, in which case we bypass the bidi
7418 iterator, because it cannot deal with such virtual
7419 characters. */
7420 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7421 {
7422 IT_STRING_BYTEPOS (*it) += it->len;
7423 IT_STRING_CHARPOS (*it) += 1;
7424 }
7425 else
7426 {
7427 int prev_scan_dir = it->bidi_it.scan_dir;
7428
7429 bidi_move_to_visually_next (&it->bidi_it);
7430 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7431 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7432 if (prev_scan_dir != it->bidi_it.scan_dir)
7433 {
7434 ptrdiff_t stop = it->end_charpos;
7435
7436 if (it->bidi_it.scan_dir < 0)
7437 stop = -1;
7438 composition_compute_stop_pos (&it->cmp_it,
7439 IT_STRING_CHARPOS (*it),
7440 IT_STRING_BYTEPOS (*it), stop,
7441 it->string);
7442 }
7443 }
7444 }
7445
7446 consider_string_end:
7447
7448 if (it->current.overlay_string_index >= 0)
7449 {
7450 /* IT->string is an overlay string. Advance to the
7451 next, if there is one. */
7452 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7453 {
7454 it->ellipsis_p = 0;
7455 next_overlay_string (it);
7456 if (it->ellipsis_p)
7457 setup_for_ellipsis (it, 0);
7458 }
7459 }
7460 else
7461 {
7462 /* IT->string is not an overlay string. If we reached
7463 its end, and there is something on IT->stack, proceed
7464 with what is on the stack. This can be either another
7465 string, this time an overlay string, or a buffer. */
7466 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7467 && it->sp > 0)
7468 {
7469 pop_it (it);
7470 if (it->method == GET_FROM_STRING)
7471 goto consider_string_end;
7472 }
7473 }
7474 break;
7475
7476 case GET_FROM_IMAGE:
7477 case GET_FROM_STRETCH:
7478 #ifdef HAVE_XWIDGETS
7479 case GET_FROM_XWIDGET:
7480
7481 /* The position etc with which we have to proceed are on
7482 the stack. The position may be at the end of a string,
7483 if the `display' property takes up the whole string. */
7484 eassert (it->sp > 0);
7485 pop_it (it);
7486 if (it->method == GET_FROM_STRING)
7487 goto consider_string_end;
7488 break;
7489 #endif
7490 default:
7491 /* There are no other methods defined, so this should be a bug. */
7492 emacs_abort ();
7493 }
7494
7495 eassert (it->method != GET_FROM_STRING
7496 || (STRINGP (it->string)
7497 && IT_STRING_CHARPOS (*it) >= 0));
7498 }
7499
7500 /* Load IT's display element fields with information about the next
7501 display element which comes from a display table entry or from the
7502 result of translating a control character to one of the forms `^C'
7503 or `\003'.
7504
7505 IT->dpvec holds the glyphs to return as characters.
7506 IT->saved_face_id holds the face id before the display vector--it
7507 is restored into IT->face_id in set_iterator_to_next. */
7508
7509 static int
7510 next_element_from_display_vector (struct it *it)
7511 {
7512 Lisp_Object gc;
7513 int prev_face_id = it->face_id;
7514 int next_face_id;
7515
7516 /* Precondition. */
7517 eassert (it->dpvec && it->current.dpvec_index >= 0);
7518
7519 it->face_id = it->saved_face_id;
7520
7521 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7522 That seemed totally bogus - so I changed it... */
7523 gc = it->dpvec[it->current.dpvec_index];
7524
7525 if (GLYPH_CODE_P (gc))
7526 {
7527 struct face *this_face, *prev_face, *next_face;
7528
7529 it->c = GLYPH_CODE_CHAR (gc);
7530 it->len = CHAR_BYTES (it->c);
7531
7532 /* The entry may contain a face id to use. Such a face id is
7533 the id of a Lisp face, not a realized face. A face id of
7534 zero means no face is specified. */
7535 if (it->dpvec_face_id >= 0)
7536 it->face_id = it->dpvec_face_id;
7537 else
7538 {
7539 int lface_id = GLYPH_CODE_FACE (gc);
7540 if (lface_id > 0)
7541 it->face_id = merge_faces (it->f, Qt, lface_id,
7542 it->saved_face_id);
7543 }
7544
7545 /* Glyphs in the display vector could have the box face, so we
7546 need to set the related flags in the iterator, as
7547 appropriate. */
7548 this_face = FACE_FROM_ID (it->f, it->face_id);
7549 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7550
7551 /* Is this character the first character of a box-face run? */
7552 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7553 && (!prev_face
7554 || prev_face->box == FACE_NO_BOX));
7555
7556 /* For the last character of the box-face run, we need to look
7557 either at the next glyph from the display vector, or at the
7558 face we saw before the display vector. */
7559 next_face_id = it->saved_face_id;
7560 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7561 {
7562 if (it->dpvec_face_id >= 0)
7563 next_face_id = it->dpvec_face_id;
7564 else
7565 {
7566 int lface_id =
7567 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7568
7569 if (lface_id > 0)
7570 next_face_id = merge_faces (it->f, Qt, lface_id,
7571 it->saved_face_id);
7572 }
7573 }
7574 next_face = FACE_FROM_ID (it->f, next_face_id);
7575 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7576 && (!next_face
7577 || next_face->box == FACE_NO_BOX));
7578 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7579 }
7580 else
7581 /* Display table entry is invalid. Return a space. */
7582 it->c = ' ', it->len = 1;
7583
7584 /* Don't change position and object of the iterator here. They are
7585 still the values of the character that had this display table
7586 entry or was translated, and that's what we want. */
7587 it->what = IT_CHARACTER;
7588 return 1;
7589 }
7590
7591 /* Get the first element of string/buffer in the visual order, after
7592 being reseated to a new position in a string or a buffer. */
7593 static void
7594 get_visually_first_element (struct it *it)
7595 {
7596 int string_p = STRINGP (it->string) || it->s;
7597 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7598 ptrdiff_t bob = (string_p ? 0 : BEGV);
7599
7600 if (STRINGP (it->string))
7601 {
7602 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7603 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7604 }
7605 else
7606 {
7607 it->bidi_it.charpos = IT_CHARPOS (*it);
7608 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7609 }
7610
7611 if (it->bidi_it.charpos == eob)
7612 {
7613 /* Nothing to do, but reset the FIRST_ELT flag, like
7614 bidi_paragraph_init does, because we are not going to
7615 call it. */
7616 it->bidi_it.first_elt = 0;
7617 }
7618 else if (it->bidi_it.charpos == bob
7619 || (!string_p
7620 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7621 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7622 {
7623 /* If we are at the beginning of a line/string, we can produce
7624 the next element right away. */
7625 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7626 bidi_move_to_visually_next (&it->bidi_it);
7627 }
7628 else
7629 {
7630 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7631
7632 /* We need to prime the bidi iterator starting at the line's or
7633 string's beginning, before we will be able to produce the
7634 next element. */
7635 if (string_p)
7636 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7637 else
7638 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7639 IT_BYTEPOS (*it), -1,
7640 &it->bidi_it.bytepos);
7641 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7642 do
7643 {
7644 /* Now return to buffer/string position where we were asked
7645 to get the next display element, and produce that. */
7646 bidi_move_to_visually_next (&it->bidi_it);
7647 }
7648 while (it->bidi_it.bytepos != orig_bytepos
7649 && it->bidi_it.charpos < eob);
7650 }
7651
7652 /* Adjust IT's position information to where we ended up. */
7653 if (STRINGP (it->string))
7654 {
7655 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7656 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7657 }
7658 else
7659 {
7660 IT_CHARPOS (*it) = it->bidi_it.charpos;
7661 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7662 }
7663
7664 if (STRINGP (it->string) || !it->s)
7665 {
7666 ptrdiff_t stop, charpos, bytepos;
7667
7668 if (STRINGP (it->string))
7669 {
7670 eassert (!it->s);
7671 stop = SCHARS (it->string);
7672 if (stop > it->end_charpos)
7673 stop = it->end_charpos;
7674 charpos = IT_STRING_CHARPOS (*it);
7675 bytepos = IT_STRING_BYTEPOS (*it);
7676 }
7677 else
7678 {
7679 stop = it->end_charpos;
7680 charpos = IT_CHARPOS (*it);
7681 bytepos = IT_BYTEPOS (*it);
7682 }
7683 if (it->bidi_it.scan_dir < 0)
7684 stop = -1;
7685 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7686 it->string);
7687 }
7688 }
7689
7690 /* Load IT with the next display element from Lisp string IT->string.
7691 IT->current.string_pos is the current position within the string.
7692 If IT->current.overlay_string_index >= 0, the Lisp string is an
7693 overlay string. */
7694
7695 static int
7696 next_element_from_string (struct it *it)
7697 {
7698 struct text_pos position;
7699
7700 eassert (STRINGP (it->string));
7701 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7702 eassert (IT_STRING_CHARPOS (*it) >= 0);
7703 position = it->current.string_pos;
7704
7705 /* With bidi reordering, the character to display might not be the
7706 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7707 that we were reseat()ed to a new string, whose paragraph
7708 direction is not known. */
7709 if (it->bidi_p && it->bidi_it.first_elt)
7710 {
7711 get_visually_first_element (it);
7712 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7713 }
7714
7715 /* Time to check for invisible text? */
7716 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7717 {
7718 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7719 {
7720 if (!(!it->bidi_p
7721 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7722 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7723 {
7724 /* With bidi non-linear iteration, we could find
7725 ourselves far beyond the last computed stop_charpos,
7726 with several other stop positions in between that we
7727 missed. Scan them all now, in buffer's logical
7728 order, until we find and handle the last stop_charpos
7729 that precedes our current position. */
7730 handle_stop_backwards (it, it->stop_charpos);
7731 return GET_NEXT_DISPLAY_ELEMENT (it);
7732 }
7733 else
7734 {
7735 if (it->bidi_p)
7736 {
7737 /* Take note of the stop position we just moved
7738 across, for when we will move back across it. */
7739 it->prev_stop = it->stop_charpos;
7740 /* If we are at base paragraph embedding level, take
7741 note of the last stop position seen at this
7742 level. */
7743 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7744 it->base_level_stop = it->stop_charpos;
7745 }
7746 handle_stop (it);
7747
7748 /* Since a handler may have changed IT->method, we must
7749 recurse here. */
7750 return GET_NEXT_DISPLAY_ELEMENT (it);
7751 }
7752 }
7753 else if (it->bidi_p
7754 /* If we are before prev_stop, we may have overstepped
7755 on our way backwards a stop_pos, and if so, we need
7756 to handle that stop_pos. */
7757 && IT_STRING_CHARPOS (*it) < it->prev_stop
7758 /* We can sometimes back up for reasons that have nothing
7759 to do with bidi reordering. E.g., compositions. The
7760 code below is only needed when we are above the base
7761 embedding level, so test for that explicitly. */
7762 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7763 {
7764 /* If we lost track of base_level_stop, we have no better
7765 place for handle_stop_backwards to start from than string
7766 beginning. This happens, e.g., when we were reseated to
7767 the previous screenful of text by vertical-motion. */
7768 if (it->base_level_stop <= 0
7769 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7770 it->base_level_stop = 0;
7771 handle_stop_backwards (it, it->base_level_stop);
7772 return GET_NEXT_DISPLAY_ELEMENT (it);
7773 }
7774 }
7775
7776 if (it->current.overlay_string_index >= 0)
7777 {
7778 /* Get the next character from an overlay string. In overlay
7779 strings, there is no field width or padding with spaces to
7780 do. */
7781 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7782 {
7783 it->what = IT_EOB;
7784 return 0;
7785 }
7786 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7787 IT_STRING_BYTEPOS (*it),
7788 it->bidi_it.scan_dir < 0
7789 ? -1
7790 : SCHARS (it->string))
7791 && next_element_from_composition (it))
7792 {
7793 return 1;
7794 }
7795 else if (STRING_MULTIBYTE (it->string))
7796 {
7797 const unsigned char *s = (SDATA (it->string)
7798 + IT_STRING_BYTEPOS (*it));
7799 it->c = string_char_and_length (s, &it->len);
7800 }
7801 else
7802 {
7803 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7804 it->len = 1;
7805 }
7806 }
7807 else
7808 {
7809 /* Get the next character from a Lisp string that is not an
7810 overlay string. Such strings come from the mode line, for
7811 example. We may have to pad with spaces, or truncate the
7812 string. See also next_element_from_c_string. */
7813 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7814 {
7815 it->what = IT_EOB;
7816 return 0;
7817 }
7818 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7819 {
7820 /* Pad with spaces. */
7821 it->c = ' ', it->len = 1;
7822 CHARPOS (position) = BYTEPOS (position) = -1;
7823 }
7824 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7825 IT_STRING_BYTEPOS (*it),
7826 it->bidi_it.scan_dir < 0
7827 ? -1
7828 : it->string_nchars)
7829 && next_element_from_composition (it))
7830 {
7831 return 1;
7832 }
7833 else if (STRING_MULTIBYTE (it->string))
7834 {
7835 const unsigned char *s = (SDATA (it->string)
7836 + IT_STRING_BYTEPOS (*it));
7837 it->c = string_char_and_length (s, &it->len);
7838 }
7839 else
7840 {
7841 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7842 it->len = 1;
7843 }
7844 }
7845
7846 /* Record what we have and where it came from. */
7847 it->what = IT_CHARACTER;
7848 it->object = it->string;
7849 it->position = position;
7850 return 1;
7851 }
7852
7853
7854 /* Load IT with next display element from C string IT->s.
7855 IT->string_nchars is the maximum number of characters to return
7856 from the string. IT->end_charpos may be greater than
7857 IT->string_nchars when this function is called, in which case we
7858 may have to return padding spaces. Value is zero if end of string
7859 reached, including padding spaces. */
7860
7861 static int
7862 next_element_from_c_string (struct it *it)
7863 {
7864 int success_p = 1;
7865
7866 eassert (it->s);
7867 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7868 it->what = IT_CHARACTER;
7869 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7870 it->object = Qnil;
7871
7872 /* With bidi reordering, the character to display might not be the
7873 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7874 we were reseated to a new string, whose paragraph direction is
7875 not known. */
7876 if (it->bidi_p && it->bidi_it.first_elt)
7877 get_visually_first_element (it);
7878
7879 /* IT's position can be greater than IT->string_nchars in case a
7880 field width or precision has been specified when the iterator was
7881 initialized. */
7882 if (IT_CHARPOS (*it) >= it->end_charpos)
7883 {
7884 /* End of the game. */
7885 it->what = IT_EOB;
7886 success_p = 0;
7887 }
7888 else if (IT_CHARPOS (*it) >= it->string_nchars)
7889 {
7890 /* Pad with spaces. */
7891 it->c = ' ', it->len = 1;
7892 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7893 }
7894 else if (it->multibyte_p)
7895 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7896 else
7897 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7898
7899 return success_p;
7900 }
7901
7902
7903 /* Set up IT to return characters from an ellipsis, if appropriate.
7904 The definition of the ellipsis glyphs may come from a display table
7905 entry. This function fills IT with the first glyph from the
7906 ellipsis if an ellipsis is to be displayed. */
7907
7908 static int
7909 next_element_from_ellipsis (struct it *it)
7910 {
7911 if (it->selective_display_ellipsis_p)
7912 setup_for_ellipsis (it, it->len);
7913 else
7914 {
7915 /* The face at the current position may be different from the
7916 face we find after the invisible text. Remember what it
7917 was in IT->saved_face_id, and signal that it's there by
7918 setting face_before_selective_p. */
7919 it->saved_face_id = it->face_id;
7920 it->method = GET_FROM_BUFFER;
7921 it->object = it->w->contents;
7922 reseat_at_next_visible_line_start (it, 1);
7923 it->face_before_selective_p = 1;
7924 }
7925
7926 return GET_NEXT_DISPLAY_ELEMENT (it);
7927 }
7928
7929
7930 /* Deliver an image display element. The iterator IT is already
7931 filled with image information (done in handle_display_prop). Value
7932 is always 1. */
7933
7934
7935 static int
7936 next_element_from_image (struct it *it)
7937 {
7938 it->what = IT_IMAGE;
7939 it->ignore_overlay_strings_at_pos_p = 0;
7940 return 1;
7941 }
7942
7943 #ifdef HAVE_XWIDGETS
7944 /* im not sure about this FIXME JAVE*/
7945 static int
7946 next_element_from_xwidget (struct it *it)
7947 {
7948 it->what = IT_XWIDGET;
7949 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
7950 //this is shaky because why do we set "what" if we dont set the other parts??
7951 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
7952 return 1;
7953 }
7954 #endif
7955
7956
7957 /* Fill iterator IT with next display element from a stretch glyph
7958 property. IT->object is the value of the text property. Value is
7959 always 1. */
7960
7961 static int
7962 next_element_from_stretch (struct it *it)
7963 {
7964 it->what = IT_STRETCH;
7965 return 1;
7966 }
7967
7968 /* Scan backwards from IT's current position until we find a stop
7969 position, or until BEGV. This is called when we find ourself
7970 before both the last known prev_stop and base_level_stop while
7971 reordering bidirectional text. */
7972
7973 static void
7974 compute_stop_pos_backwards (struct it *it)
7975 {
7976 const int SCAN_BACK_LIMIT = 1000;
7977 struct text_pos pos;
7978 struct display_pos save_current = it->current;
7979 struct text_pos save_position = it->position;
7980 ptrdiff_t charpos = IT_CHARPOS (*it);
7981 ptrdiff_t where_we_are = charpos;
7982 ptrdiff_t save_stop_pos = it->stop_charpos;
7983 ptrdiff_t save_end_pos = it->end_charpos;
7984
7985 eassert (NILP (it->string) && !it->s);
7986 eassert (it->bidi_p);
7987 it->bidi_p = 0;
7988 do
7989 {
7990 it->end_charpos = min (charpos + 1, ZV);
7991 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7992 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7993 reseat_1 (it, pos, 0);
7994 compute_stop_pos (it);
7995 /* We must advance forward, right? */
7996 if (it->stop_charpos <= charpos)
7997 emacs_abort ();
7998 }
7999 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8000
8001 if (it->stop_charpos <= where_we_are)
8002 it->prev_stop = it->stop_charpos;
8003 else
8004 it->prev_stop = BEGV;
8005 it->bidi_p = 1;
8006 it->current = save_current;
8007 it->position = save_position;
8008 it->stop_charpos = save_stop_pos;
8009 it->end_charpos = save_end_pos;
8010 }
8011
8012 /* Scan forward from CHARPOS in the current buffer/string, until we
8013 find a stop position > current IT's position. Then handle the stop
8014 position before that. This is called when we bump into a stop
8015 position while reordering bidirectional text. CHARPOS should be
8016 the last previously processed stop_pos (or BEGV/0, if none were
8017 processed yet) whose position is less that IT's current
8018 position. */
8019
8020 static void
8021 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8022 {
8023 int bufp = !STRINGP (it->string);
8024 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8025 struct display_pos save_current = it->current;
8026 struct text_pos save_position = it->position;
8027 struct text_pos pos1;
8028 ptrdiff_t next_stop;
8029
8030 /* Scan in strict logical order. */
8031 eassert (it->bidi_p);
8032 it->bidi_p = 0;
8033 do
8034 {
8035 it->prev_stop = charpos;
8036 if (bufp)
8037 {
8038 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8039 reseat_1 (it, pos1, 0);
8040 }
8041 else
8042 it->current.string_pos = string_pos (charpos, it->string);
8043 compute_stop_pos (it);
8044 /* We must advance forward, right? */
8045 if (it->stop_charpos <= it->prev_stop)
8046 emacs_abort ();
8047 charpos = it->stop_charpos;
8048 }
8049 while (charpos <= where_we_are);
8050
8051 it->bidi_p = 1;
8052 it->current = save_current;
8053 it->position = save_position;
8054 next_stop = it->stop_charpos;
8055 it->stop_charpos = it->prev_stop;
8056 handle_stop (it);
8057 it->stop_charpos = next_stop;
8058 }
8059
8060 /* Load IT with the next display element from current_buffer. Value
8061 is zero if end of buffer reached. IT->stop_charpos is the next
8062 position at which to stop and check for text properties or buffer
8063 end. */
8064
8065 static int
8066 next_element_from_buffer (struct it *it)
8067 {
8068 int success_p = 1;
8069
8070 eassert (IT_CHARPOS (*it) >= BEGV);
8071 eassert (NILP (it->string) && !it->s);
8072 eassert (!it->bidi_p
8073 || (EQ (it->bidi_it.string.lstring, Qnil)
8074 && it->bidi_it.string.s == NULL));
8075
8076 /* With bidi reordering, the character to display might not be the
8077 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8078 we were reseat()ed to a new buffer position, which is potentially
8079 a different paragraph. */
8080 if (it->bidi_p && it->bidi_it.first_elt)
8081 {
8082 get_visually_first_element (it);
8083 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8084 }
8085
8086 if (IT_CHARPOS (*it) >= it->stop_charpos)
8087 {
8088 if (IT_CHARPOS (*it) >= it->end_charpos)
8089 {
8090 int overlay_strings_follow_p;
8091
8092 /* End of the game, except when overlay strings follow that
8093 haven't been returned yet. */
8094 if (it->overlay_strings_at_end_processed_p)
8095 overlay_strings_follow_p = 0;
8096 else
8097 {
8098 it->overlay_strings_at_end_processed_p = 1;
8099 overlay_strings_follow_p = get_overlay_strings (it, 0);
8100 }
8101
8102 if (overlay_strings_follow_p)
8103 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8104 else
8105 {
8106 it->what = IT_EOB;
8107 it->position = it->current.pos;
8108 success_p = 0;
8109 }
8110 }
8111 else if (!(!it->bidi_p
8112 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8113 || IT_CHARPOS (*it) == it->stop_charpos))
8114 {
8115 /* With bidi non-linear iteration, we could find ourselves
8116 far beyond the last computed stop_charpos, with several
8117 other stop positions in between that we missed. Scan
8118 them all now, in buffer's logical order, until we find
8119 and handle the last stop_charpos that precedes our
8120 current position. */
8121 handle_stop_backwards (it, it->stop_charpos);
8122 return GET_NEXT_DISPLAY_ELEMENT (it);
8123 }
8124 else
8125 {
8126 if (it->bidi_p)
8127 {
8128 /* Take note of the stop position we just moved across,
8129 for when we will move back across it. */
8130 it->prev_stop = it->stop_charpos;
8131 /* If we are at base paragraph embedding level, take
8132 note of the last stop position seen at this
8133 level. */
8134 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8135 it->base_level_stop = it->stop_charpos;
8136 }
8137 handle_stop (it);
8138 return GET_NEXT_DISPLAY_ELEMENT (it);
8139 }
8140 }
8141 else if (it->bidi_p
8142 /* If we are before prev_stop, we may have overstepped on
8143 our way backwards a stop_pos, and if so, we need to
8144 handle that stop_pos. */
8145 && IT_CHARPOS (*it) < it->prev_stop
8146 /* We can sometimes back up for reasons that have nothing
8147 to do with bidi reordering. E.g., compositions. The
8148 code below is only needed when we are above the base
8149 embedding level, so test for that explicitly. */
8150 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8151 {
8152 if (it->base_level_stop <= 0
8153 || IT_CHARPOS (*it) < it->base_level_stop)
8154 {
8155 /* If we lost track of base_level_stop, we need to find
8156 prev_stop by looking backwards. This happens, e.g., when
8157 we were reseated to the previous screenful of text by
8158 vertical-motion. */
8159 it->base_level_stop = BEGV;
8160 compute_stop_pos_backwards (it);
8161 handle_stop_backwards (it, it->prev_stop);
8162 }
8163 else
8164 handle_stop_backwards (it, it->base_level_stop);
8165 return GET_NEXT_DISPLAY_ELEMENT (it);
8166 }
8167 else
8168 {
8169 /* No face changes, overlays etc. in sight, so just return a
8170 character from current_buffer. */
8171 unsigned char *p;
8172 ptrdiff_t stop;
8173
8174 /* Maybe run the redisplay end trigger hook. Performance note:
8175 This doesn't seem to cost measurable time. */
8176 if (it->redisplay_end_trigger_charpos
8177 && it->glyph_row
8178 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8179 run_redisplay_end_trigger_hook (it);
8180
8181 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8182 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8183 stop)
8184 && next_element_from_composition (it))
8185 {
8186 return 1;
8187 }
8188
8189 /* Get the next character, maybe multibyte. */
8190 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8191 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8192 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8193 else
8194 it->c = *p, it->len = 1;
8195
8196 /* Record what we have and where it came from. */
8197 it->what = IT_CHARACTER;
8198 it->object = it->w->contents;
8199 it->position = it->current.pos;
8200
8201 /* Normally we return the character found above, except when we
8202 really want to return an ellipsis for selective display. */
8203 if (it->selective)
8204 {
8205 if (it->c == '\n')
8206 {
8207 /* A value of selective > 0 means hide lines indented more
8208 than that number of columns. */
8209 if (it->selective > 0
8210 && IT_CHARPOS (*it) + 1 < ZV
8211 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8212 IT_BYTEPOS (*it) + 1,
8213 it->selective))
8214 {
8215 success_p = next_element_from_ellipsis (it);
8216 it->dpvec_char_len = -1;
8217 }
8218 }
8219 else if (it->c == '\r' && it->selective == -1)
8220 {
8221 /* A value of selective == -1 means that everything from the
8222 CR to the end of the line is invisible, with maybe an
8223 ellipsis displayed for it. */
8224 success_p = next_element_from_ellipsis (it);
8225 it->dpvec_char_len = -1;
8226 }
8227 }
8228 }
8229
8230 /* Value is zero if end of buffer reached. */
8231 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8232 return success_p;
8233 }
8234
8235
8236 /* Run the redisplay end trigger hook for IT. */
8237
8238 static void
8239 run_redisplay_end_trigger_hook (struct it *it)
8240 {
8241 Lisp_Object args[3];
8242
8243 /* IT->glyph_row should be non-null, i.e. we should be actually
8244 displaying something, or otherwise we should not run the hook. */
8245 eassert (it->glyph_row);
8246
8247 /* Set up hook arguments. */
8248 args[0] = Qredisplay_end_trigger_functions;
8249 args[1] = it->window;
8250 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8251 it->redisplay_end_trigger_charpos = 0;
8252
8253 /* Since we are *trying* to run these functions, don't try to run
8254 them again, even if they get an error. */
8255 wset_redisplay_end_trigger (it->w, Qnil);
8256 Frun_hook_with_args (3, args);
8257
8258 /* Notice if it changed the face of the character we are on. */
8259 handle_face_prop (it);
8260 }
8261
8262
8263 /* Deliver a composition display element. Unlike the other
8264 next_element_from_XXX, this function is not registered in the array
8265 get_next_element[]. It is called from next_element_from_buffer and
8266 next_element_from_string when necessary. */
8267
8268 static int
8269 next_element_from_composition (struct it *it)
8270 {
8271 it->what = IT_COMPOSITION;
8272 it->len = it->cmp_it.nbytes;
8273 if (STRINGP (it->string))
8274 {
8275 if (it->c < 0)
8276 {
8277 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8278 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8279 return 0;
8280 }
8281 it->position = it->current.string_pos;
8282 it->object = it->string;
8283 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8284 IT_STRING_BYTEPOS (*it), it->string);
8285 }
8286 else
8287 {
8288 if (it->c < 0)
8289 {
8290 IT_CHARPOS (*it) += it->cmp_it.nchars;
8291 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8292 if (it->bidi_p)
8293 {
8294 if (it->bidi_it.new_paragraph)
8295 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8296 /* Resync the bidi iterator with IT's new position.
8297 FIXME: this doesn't support bidirectional text. */
8298 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8299 bidi_move_to_visually_next (&it->bidi_it);
8300 }
8301 return 0;
8302 }
8303 it->position = it->current.pos;
8304 it->object = it->w->contents;
8305 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8306 IT_BYTEPOS (*it), Qnil);
8307 }
8308 return 1;
8309 }
8310
8311
8312 \f
8313 /***********************************************************************
8314 Moving an iterator without producing glyphs
8315 ***********************************************************************/
8316
8317 /* Check if iterator is at a position corresponding to a valid buffer
8318 position after some move_it_ call. */
8319
8320 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8321 ((it)->method == GET_FROM_STRING \
8322 ? IT_STRING_CHARPOS (*it) == 0 \
8323 : 1)
8324
8325
8326 /* Move iterator IT to a specified buffer or X position within one
8327 line on the display without producing glyphs.
8328
8329 OP should be a bit mask including some or all of these bits:
8330 MOVE_TO_X: Stop upon reaching x-position TO_X.
8331 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8332 Regardless of OP's value, stop upon reaching the end of the display line.
8333
8334 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8335 This means, in particular, that TO_X includes window's horizontal
8336 scroll amount.
8337
8338 The return value has several possible values that
8339 say what condition caused the scan to stop:
8340
8341 MOVE_POS_MATCH_OR_ZV
8342 - when TO_POS or ZV was reached.
8343
8344 MOVE_X_REACHED
8345 -when TO_X was reached before TO_POS or ZV were reached.
8346
8347 MOVE_LINE_CONTINUED
8348 - when we reached the end of the display area and the line must
8349 be continued.
8350
8351 MOVE_LINE_TRUNCATED
8352 - when we reached the end of the display area and the line is
8353 truncated.
8354
8355 MOVE_NEWLINE_OR_CR
8356 - when we stopped at a line end, i.e. a newline or a CR and selective
8357 display is on. */
8358
8359 static enum move_it_result
8360 move_it_in_display_line_to (struct it *it,
8361 ptrdiff_t to_charpos, int to_x,
8362 enum move_operation_enum op)
8363 {
8364 enum move_it_result result = MOVE_UNDEFINED;
8365 struct glyph_row *saved_glyph_row;
8366 struct it wrap_it, atpos_it, atx_it, ppos_it;
8367 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8368 void *ppos_data = NULL;
8369 int may_wrap = 0;
8370 enum it_method prev_method = it->method;
8371 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8372 int saw_smaller_pos = prev_pos < to_charpos;
8373
8374 /* Don't produce glyphs in produce_glyphs. */
8375 saved_glyph_row = it->glyph_row;
8376 it->glyph_row = NULL;
8377
8378 /* Use wrap_it to save a copy of IT wherever a word wrap could
8379 occur. Use atpos_it to save a copy of IT at the desired buffer
8380 position, if found, so that we can scan ahead and check if the
8381 word later overshoots the window edge. Use atx_it similarly, for
8382 pixel positions. */
8383 wrap_it.sp = -1;
8384 atpos_it.sp = -1;
8385 atx_it.sp = -1;
8386
8387 /* Use ppos_it under bidi reordering to save a copy of IT for the
8388 position > CHARPOS that is the closest to CHARPOS. We restore
8389 that position in IT when we have scanned the entire display line
8390 without finding a match for CHARPOS and all the character
8391 positions are greater than CHARPOS. */
8392 if (it->bidi_p)
8393 {
8394 SAVE_IT (ppos_it, *it, ppos_data);
8395 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8396 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8397 SAVE_IT (ppos_it, *it, ppos_data);
8398 }
8399
8400 #define BUFFER_POS_REACHED_P() \
8401 ((op & MOVE_TO_POS) != 0 \
8402 && BUFFERP (it->object) \
8403 && (IT_CHARPOS (*it) == to_charpos \
8404 || ((!it->bidi_p \
8405 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8406 && IT_CHARPOS (*it) > to_charpos) \
8407 || (it->what == IT_COMPOSITION \
8408 && ((IT_CHARPOS (*it) > to_charpos \
8409 && to_charpos >= it->cmp_it.charpos) \
8410 || (IT_CHARPOS (*it) < to_charpos \
8411 && to_charpos <= it->cmp_it.charpos)))) \
8412 && (it->method == GET_FROM_BUFFER \
8413 || (it->method == GET_FROM_DISPLAY_VECTOR \
8414 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8415
8416 /* If there's a line-/wrap-prefix, handle it. */
8417 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8418 && it->current_y < it->last_visible_y)
8419 handle_line_prefix (it);
8420
8421 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8422 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8423
8424 while (1)
8425 {
8426 int x, i, ascent = 0, descent = 0;
8427
8428 /* Utility macro to reset an iterator with x, ascent, and descent. */
8429 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8430 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8431 (IT)->max_descent = descent)
8432
8433 /* Stop if we move beyond TO_CHARPOS (after an image or a
8434 display string or stretch glyph). */
8435 if ((op & MOVE_TO_POS) != 0
8436 && BUFFERP (it->object)
8437 && it->method == GET_FROM_BUFFER
8438 && (((!it->bidi_p
8439 /* When the iterator is at base embedding level, we
8440 are guaranteed that characters are delivered for
8441 display in strictly increasing order of their
8442 buffer positions. */
8443 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8444 && IT_CHARPOS (*it) > to_charpos)
8445 || (it->bidi_p
8446 && (prev_method == GET_FROM_IMAGE
8447 || prev_method == GET_FROM_STRETCH
8448 || prev_method == GET_FROM_STRING)
8449 /* Passed TO_CHARPOS from left to right. */
8450 && ((prev_pos < to_charpos
8451 && IT_CHARPOS (*it) > to_charpos)
8452 /* Passed TO_CHARPOS from right to left. */
8453 || (prev_pos > to_charpos
8454 && IT_CHARPOS (*it) < to_charpos)))))
8455 {
8456 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8457 {
8458 result = MOVE_POS_MATCH_OR_ZV;
8459 break;
8460 }
8461 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8462 /* If wrap_it is valid, the current position might be in a
8463 word that is wrapped. So, save the iterator in
8464 atpos_it and continue to see if wrapping happens. */
8465 SAVE_IT (atpos_it, *it, atpos_data);
8466 }
8467
8468 /* Stop when ZV reached.
8469 We used to stop here when TO_CHARPOS reached as well, but that is
8470 too soon if this glyph does not fit on this line. So we handle it
8471 explicitly below. */
8472 if (!get_next_display_element (it))
8473 {
8474 result = MOVE_POS_MATCH_OR_ZV;
8475 break;
8476 }
8477
8478 if (it->line_wrap == TRUNCATE)
8479 {
8480 if (BUFFER_POS_REACHED_P ())
8481 {
8482 result = MOVE_POS_MATCH_OR_ZV;
8483 break;
8484 }
8485 }
8486 else
8487 {
8488 if (it->line_wrap == WORD_WRAP)
8489 {
8490 if (IT_DISPLAYING_WHITESPACE (it))
8491 may_wrap = 1;
8492 else if (may_wrap)
8493 {
8494 /* We have reached a glyph that follows one or more
8495 whitespace characters. If the position is
8496 already found, we are done. */
8497 if (atpos_it.sp >= 0)
8498 {
8499 RESTORE_IT (it, &atpos_it, atpos_data);
8500 result = MOVE_POS_MATCH_OR_ZV;
8501 goto done;
8502 }
8503 if (atx_it.sp >= 0)
8504 {
8505 RESTORE_IT (it, &atx_it, atx_data);
8506 result = MOVE_X_REACHED;
8507 goto done;
8508 }
8509 /* Otherwise, we can wrap here. */
8510 SAVE_IT (wrap_it, *it, wrap_data);
8511 may_wrap = 0;
8512 }
8513 }
8514 }
8515
8516 /* Remember the line height for the current line, in case
8517 the next element doesn't fit on the line. */
8518 ascent = it->max_ascent;
8519 descent = it->max_descent;
8520
8521 /* The call to produce_glyphs will get the metrics of the
8522 display element IT is loaded with. Record the x-position
8523 before this display element, in case it doesn't fit on the
8524 line. */
8525 x = it->current_x;
8526
8527 PRODUCE_GLYPHS (it);
8528
8529 if (it->area != TEXT_AREA)
8530 {
8531 prev_method = it->method;
8532 if (it->method == GET_FROM_BUFFER)
8533 prev_pos = IT_CHARPOS (*it);
8534 set_iterator_to_next (it, 1);
8535 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8536 SET_TEXT_POS (this_line_min_pos,
8537 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8538 if (it->bidi_p
8539 && (op & MOVE_TO_POS)
8540 && IT_CHARPOS (*it) > to_charpos
8541 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8542 SAVE_IT (ppos_it, *it, ppos_data);
8543 continue;
8544 }
8545
8546 /* The number of glyphs we get back in IT->nglyphs will normally
8547 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8548 character on a terminal frame, or (iii) a line end. For the
8549 second case, IT->nglyphs - 1 padding glyphs will be present.
8550 (On X frames, there is only one glyph produced for a
8551 composite character.)
8552
8553 The behavior implemented below means, for continuation lines,
8554 that as many spaces of a TAB as fit on the current line are
8555 displayed there. For terminal frames, as many glyphs of a
8556 multi-glyph character are displayed in the current line, too.
8557 This is what the old redisplay code did, and we keep it that
8558 way. Under X, the whole shape of a complex character must
8559 fit on the line or it will be completely displayed in the
8560 next line.
8561
8562 Note that both for tabs and padding glyphs, all glyphs have
8563 the same width. */
8564 if (it->nglyphs)
8565 {
8566 /* More than one glyph or glyph doesn't fit on line. All
8567 glyphs have the same width. */
8568 int single_glyph_width = it->pixel_width / it->nglyphs;
8569 int new_x;
8570 int x_before_this_char = x;
8571 int hpos_before_this_char = it->hpos;
8572
8573 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8574 {
8575 new_x = x + single_glyph_width;
8576
8577 /* We want to leave anything reaching TO_X to the caller. */
8578 if ((op & MOVE_TO_X) && new_x > to_x)
8579 {
8580 if (BUFFER_POS_REACHED_P ())
8581 {
8582 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8583 goto buffer_pos_reached;
8584 if (atpos_it.sp < 0)
8585 {
8586 SAVE_IT (atpos_it, *it, atpos_data);
8587 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8588 }
8589 }
8590 else
8591 {
8592 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8593 {
8594 it->current_x = x;
8595 result = MOVE_X_REACHED;
8596 break;
8597 }
8598 if (atx_it.sp < 0)
8599 {
8600 SAVE_IT (atx_it, *it, atx_data);
8601 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8602 }
8603 }
8604 }
8605
8606 if (/* Lines are continued. */
8607 it->line_wrap != TRUNCATE
8608 && (/* And glyph doesn't fit on the line. */
8609 new_x > it->last_visible_x
8610 /* Or it fits exactly and we're on a window
8611 system frame. */
8612 || (new_x == it->last_visible_x
8613 && FRAME_WINDOW_P (it->f)
8614 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8615 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8616 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8617 {
8618 if (/* IT->hpos == 0 means the very first glyph
8619 doesn't fit on the line, e.g. a wide image. */
8620 it->hpos == 0
8621 || (new_x == it->last_visible_x
8622 && FRAME_WINDOW_P (it->f)))
8623 {
8624 ++it->hpos;
8625 it->current_x = new_x;
8626
8627 /* The character's last glyph just barely fits
8628 in this row. */
8629 if (i == it->nglyphs - 1)
8630 {
8631 /* If this is the destination position,
8632 return a position *before* it in this row,
8633 now that we know it fits in this row. */
8634 if (BUFFER_POS_REACHED_P ())
8635 {
8636 if (it->line_wrap != WORD_WRAP
8637 || wrap_it.sp < 0)
8638 {
8639 it->hpos = hpos_before_this_char;
8640 it->current_x = x_before_this_char;
8641 result = MOVE_POS_MATCH_OR_ZV;
8642 break;
8643 }
8644 if (it->line_wrap == WORD_WRAP
8645 && atpos_it.sp < 0)
8646 {
8647 SAVE_IT (atpos_it, *it, atpos_data);
8648 atpos_it.current_x = x_before_this_char;
8649 atpos_it.hpos = hpos_before_this_char;
8650 }
8651 }
8652
8653 prev_method = it->method;
8654 if (it->method == GET_FROM_BUFFER)
8655 prev_pos = IT_CHARPOS (*it);
8656 set_iterator_to_next (it, 1);
8657 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8658 SET_TEXT_POS (this_line_min_pos,
8659 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8660 /* On graphical terminals, newlines may
8661 "overflow" into the fringe if
8662 overflow-newline-into-fringe is non-nil.
8663 On text terminals, and on graphical
8664 terminals with no right margin, newlines
8665 may overflow into the last glyph on the
8666 display line.*/
8667 if (!FRAME_WINDOW_P (it->f)
8668 || ((it->bidi_p
8669 && it->bidi_it.paragraph_dir == R2L)
8670 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8671 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8672 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8673 {
8674 if (!get_next_display_element (it))
8675 {
8676 result = MOVE_POS_MATCH_OR_ZV;
8677 break;
8678 }
8679 if (BUFFER_POS_REACHED_P ())
8680 {
8681 if (ITERATOR_AT_END_OF_LINE_P (it))
8682 result = MOVE_POS_MATCH_OR_ZV;
8683 else
8684 result = MOVE_LINE_CONTINUED;
8685 break;
8686 }
8687 if (ITERATOR_AT_END_OF_LINE_P (it)
8688 && (it->line_wrap != WORD_WRAP
8689 || wrap_it.sp < 0))
8690 {
8691 result = MOVE_NEWLINE_OR_CR;
8692 break;
8693 }
8694 }
8695 }
8696 }
8697 else
8698 IT_RESET_X_ASCENT_DESCENT (it);
8699
8700 if (wrap_it.sp >= 0)
8701 {
8702 RESTORE_IT (it, &wrap_it, wrap_data);
8703 atpos_it.sp = -1;
8704 atx_it.sp = -1;
8705 }
8706
8707 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8708 IT_CHARPOS (*it)));
8709 result = MOVE_LINE_CONTINUED;
8710 break;
8711 }
8712
8713 if (BUFFER_POS_REACHED_P ())
8714 {
8715 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8716 goto buffer_pos_reached;
8717 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8718 {
8719 SAVE_IT (atpos_it, *it, atpos_data);
8720 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8721 }
8722 }
8723
8724 if (new_x > it->first_visible_x)
8725 {
8726 /* Glyph is visible. Increment number of glyphs that
8727 would be displayed. */
8728 ++it->hpos;
8729 }
8730 }
8731
8732 if (result != MOVE_UNDEFINED)
8733 break;
8734 }
8735 else if (BUFFER_POS_REACHED_P ())
8736 {
8737 buffer_pos_reached:
8738 IT_RESET_X_ASCENT_DESCENT (it);
8739 result = MOVE_POS_MATCH_OR_ZV;
8740 break;
8741 }
8742 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8743 {
8744 /* Stop when TO_X specified and reached. This check is
8745 necessary here because of lines consisting of a line end,
8746 only. The line end will not produce any glyphs and we
8747 would never get MOVE_X_REACHED. */
8748 eassert (it->nglyphs == 0);
8749 result = MOVE_X_REACHED;
8750 break;
8751 }
8752
8753 /* Is this a line end? If yes, we're done. */
8754 if (ITERATOR_AT_END_OF_LINE_P (it))
8755 {
8756 /* If we are past TO_CHARPOS, but never saw any character
8757 positions smaller than TO_CHARPOS, return
8758 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8759 did. */
8760 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8761 {
8762 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8763 {
8764 if (IT_CHARPOS (ppos_it) < ZV)
8765 {
8766 RESTORE_IT (it, &ppos_it, ppos_data);
8767 result = MOVE_POS_MATCH_OR_ZV;
8768 }
8769 else
8770 goto buffer_pos_reached;
8771 }
8772 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8773 && IT_CHARPOS (*it) > to_charpos)
8774 goto buffer_pos_reached;
8775 else
8776 result = MOVE_NEWLINE_OR_CR;
8777 }
8778 else
8779 result = MOVE_NEWLINE_OR_CR;
8780 break;
8781 }
8782
8783 prev_method = it->method;
8784 if (it->method == GET_FROM_BUFFER)
8785 prev_pos = IT_CHARPOS (*it);
8786 /* The current display element has been consumed. Advance
8787 to the next. */
8788 set_iterator_to_next (it, 1);
8789 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8790 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8791 if (IT_CHARPOS (*it) < to_charpos)
8792 saw_smaller_pos = 1;
8793 if (it->bidi_p
8794 && (op & MOVE_TO_POS)
8795 && IT_CHARPOS (*it) >= to_charpos
8796 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8797 SAVE_IT (ppos_it, *it, ppos_data);
8798
8799 /* Stop if lines are truncated and IT's current x-position is
8800 past the right edge of the window now. */
8801 if (it->line_wrap == TRUNCATE
8802 && it->current_x >= it->last_visible_x)
8803 {
8804 if (!FRAME_WINDOW_P (it->f)
8805 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8806 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8807 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8808 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8809 {
8810 int at_eob_p = 0;
8811
8812 if ((at_eob_p = !get_next_display_element (it))
8813 || BUFFER_POS_REACHED_P ()
8814 /* If we are past TO_CHARPOS, but never saw any
8815 character positions smaller than TO_CHARPOS,
8816 return MOVE_POS_MATCH_OR_ZV, like the
8817 unidirectional display did. */
8818 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8819 && !saw_smaller_pos
8820 && IT_CHARPOS (*it) > to_charpos))
8821 {
8822 if (it->bidi_p
8823 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8824 RESTORE_IT (it, &ppos_it, ppos_data);
8825 result = MOVE_POS_MATCH_OR_ZV;
8826 break;
8827 }
8828 if (ITERATOR_AT_END_OF_LINE_P (it))
8829 {
8830 result = MOVE_NEWLINE_OR_CR;
8831 break;
8832 }
8833 }
8834 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8835 && !saw_smaller_pos
8836 && IT_CHARPOS (*it) > to_charpos)
8837 {
8838 if (IT_CHARPOS (ppos_it) < ZV)
8839 RESTORE_IT (it, &ppos_it, ppos_data);
8840 result = MOVE_POS_MATCH_OR_ZV;
8841 break;
8842 }
8843 result = MOVE_LINE_TRUNCATED;
8844 break;
8845 }
8846 #undef IT_RESET_X_ASCENT_DESCENT
8847 }
8848
8849 #undef BUFFER_POS_REACHED_P
8850
8851 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8852 restore the saved iterator. */
8853 if (atpos_it.sp >= 0)
8854 RESTORE_IT (it, &atpos_it, atpos_data);
8855 else if (atx_it.sp >= 0)
8856 RESTORE_IT (it, &atx_it, atx_data);
8857
8858 done:
8859
8860 if (atpos_data)
8861 bidi_unshelve_cache (atpos_data, 1);
8862 if (atx_data)
8863 bidi_unshelve_cache (atx_data, 1);
8864 if (wrap_data)
8865 bidi_unshelve_cache (wrap_data, 1);
8866 if (ppos_data)
8867 bidi_unshelve_cache (ppos_data, 1);
8868
8869 /* Restore the iterator settings altered at the beginning of this
8870 function. */
8871 it->glyph_row = saved_glyph_row;
8872 return result;
8873 }
8874
8875 /* For external use. */
8876 void
8877 move_it_in_display_line (struct it *it,
8878 ptrdiff_t to_charpos, int to_x,
8879 enum move_operation_enum op)
8880 {
8881 if (it->line_wrap == WORD_WRAP
8882 && (op & MOVE_TO_X))
8883 {
8884 struct it save_it;
8885 void *save_data = NULL;
8886 int skip;
8887
8888 SAVE_IT (save_it, *it, save_data);
8889 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8890 /* When word-wrap is on, TO_X may lie past the end
8891 of a wrapped line. Then it->current is the
8892 character on the next line, so backtrack to the
8893 space before the wrap point. */
8894 if (skip == MOVE_LINE_CONTINUED)
8895 {
8896 int prev_x = max (it->current_x - 1, 0);
8897 RESTORE_IT (it, &save_it, save_data);
8898 move_it_in_display_line_to
8899 (it, -1, prev_x, MOVE_TO_X);
8900 }
8901 else
8902 bidi_unshelve_cache (save_data, 1);
8903 }
8904 else
8905 move_it_in_display_line_to (it, to_charpos, to_x, op);
8906 }
8907
8908
8909 /* Move IT forward until it satisfies one or more of the criteria in
8910 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8911
8912 OP is a bit-mask that specifies where to stop, and in particular,
8913 which of those four position arguments makes a difference. See the
8914 description of enum move_operation_enum.
8915
8916 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8917 screen line, this function will set IT to the next position that is
8918 displayed to the right of TO_CHARPOS on the screen. */
8919
8920 void
8921 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8922 {
8923 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8924 int line_height, line_start_x = 0, reached = 0;
8925 void *backup_data = NULL;
8926
8927 for (;;)
8928 {
8929 if (op & MOVE_TO_VPOS)
8930 {
8931 /* If no TO_CHARPOS and no TO_X specified, stop at the
8932 start of the line TO_VPOS. */
8933 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8934 {
8935 if (it->vpos == to_vpos)
8936 {
8937 reached = 1;
8938 break;
8939 }
8940 else
8941 skip = move_it_in_display_line_to (it, -1, -1, 0);
8942 }
8943 else
8944 {
8945 /* TO_VPOS >= 0 means stop at TO_X in the line at
8946 TO_VPOS, or at TO_POS, whichever comes first. */
8947 if (it->vpos == to_vpos)
8948 {
8949 reached = 2;
8950 break;
8951 }
8952
8953 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8954
8955 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8956 {
8957 reached = 3;
8958 break;
8959 }
8960 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8961 {
8962 /* We have reached TO_X but not in the line we want. */
8963 skip = move_it_in_display_line_to (it, to_charpos,
8964 -1, MOVE_TO_POS);
8965 if (skip == MOVE_POS_MATCH_OR_ZV)
8966 {
8967 reached = 4;
8968 break;
8969 }
8970 }
8971 }
8972 }
8973 else if (op & MOVE_TO_Y)
8974 {
8975 struct it it_backup;
8976
8977 if (it->line_wrap == WORD_WRAP)
8978 SAVE_IT (it_backup, *it, backup_data);
8979
8980 /* TO_Y specified means stop at TO_X in the line containing
8981 TO_Y---or at TO_CHARPOS if this is reached first. The
8982 problem is that we can't really tell whether the line
8983 contains TO_Y before we have completely scanned it, and
8984 this may skip past TO_X. What we do is to first scan to
8985 TO_X.
8986
8987 If TO_X is not specified, use a TO_X of zero. The reason
8988 is to make the outcome of this function more predictable.
8989 If we didn't use TO_X == 0, we would stop at the end of
8990 the line which is probably not what a caller would expect
8991 to happen. */
8992 skip = move_it_in_display_line_to
8993 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8994 (MOVE_TO_X | (op & MOVE_TO_POS)));
8995
8996 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8997 if (skip == MOVE_POS_MATCH_OR_ZV)
8998 reached = 5;
8999 else if (skip == MOVE_X_REACHED)
9000 {
9001 /* If TO_X was reached, we want to know whether TO_Y is
9002 in the line. We know this is the case if the already
9003 scanned glyphs make the line tall enough. Otherwise,
9004 we must check by scanning the rest of the line. */
9005 line_height = it->max_ascent + it->max_descent;
9006 if (to_y >= it->current_y
9007 && to_y < it->current_y + line_height)
9008 {
9009 reached = 6;
9010 break;
9011 }
9012 SAVE_IT (it_backup, *it, backup_data);
9013 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9014 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9015 op & MOVE_TO_POS);
9016 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9017 line_height = it->max_ascent + it->max_descent;
9018 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9019
9020 if (to_y >= it->current_y
9021 && to_y < it->current_y + line_height)
9022 {
9023 /* If TO_Y is in this line and TO_X was reached
9024 above, we scanned too far. We have to restore
9025 IT's settings to the ones before skipping. But
9026 keep the more accurate values of max_ascent and
9027 max_descent we've found while skipping the rest
9028 of the line, for the sake of callers, such as
9029 pos_visible_p, that need to know the line
9030 height. */
9031 int max_ascent = it->max_ascent;
9032 int max_descent = it->max_descent;
9033
9034 RESTORE_IT (it, &it_backup, backup_data);
9035 it->max_ascent = max_ascent;
9036 it->max_descent = max_descent;
9037 reached = 6;
9038 }
9039 else
9040 {
9041 skip = skip2;
9042 if (skip == MOVE_POS_MATCH_OR_ZV)
9043 reached = 7;
9044 }
9045 }
9046 else
9047 {
9048 /* Check whether TO_Y is in this line. */
9049 line_height = it->max_ascent + it->max_descent;
9050 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9051
9052 if (to_y >= it->current_y
9053 && to_y < it->current_y + line_height)
9054 {
9055 /* When word-wrap is on, TO_X may lie past the end
9056 of a wrapped line. Then it->current is the
9057 character on the next line, so backtrack to the
9058 space before the wrap point. */
9059 if (skip == MOVE_LINE_CONTINUED
9060 && it->line_wrap == WORD_WRAP)
9061 {
9062 int prev_x = max (it->current_x - 1, 0);
9063 RESTORE_IT (it, &it_backup, backup_data);
9064 skip = move_it_in_display_line_to
9065 (it, -1, prev_x, MOVE_TO_X);
9066 }
9067 reached = 6;
9068 }
9069 }
9070
9071 if (reached)
9072 break;
9073 }
9074 else if (BUFFERP (it->object)
9075 && (it->method == GET_FROM_BUFFER
9076 || it->method == GET_FROM_STRETCH)
9077 && IT_CHARPOS (*it) >= to_charpos
9078 /* Under bidi iteration, a call to set_iterator_to_next
9079 can scan far beyond to_charpos if the initial
9080 portion of the next line needs to be reordered. In
9081 that case, give move_it_in_display_line_to another
9082 chance below. */
9083 && !(it->bidi_p
9084 && it->bidi_it.scan_dir == -1))
9085 skip = MOVE_POS_MATCH_OR_ZV;
9086 else
9087 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9088
9089 switch (skip)
9090 {
9091 case MOVE_POS_MATCH_OR_ZV:
9092 reached = 8;
9093 goto out;
9094
9095 case MOVE_NEWLINE_OR_CR:
9096 set_iterator_to_next (it, 1);
9097 it->continuation_lines_width = 0;
9098 break;
9099
9100 case MOVE_LINE_TRUNCATED:
9101 it->continuation_lines_width = 0;
9102 reseat_at_next_visible_line_start (it, 0);
9103 if ((op & MOVE_TO_POS) != 0
9104 && IT_CHARPOS (*it) > to_charpos)
9105 {
9106 reached = 9;
9107 goto out;
9108 }
9109 break;
9110
9111 case MOVE_LINE_CONTINUED:
9112 /* For continued lines ending in a tab, some of the glyphs
9113 associated with the tab are displayed on the current
9114 line. Since it->current_x does not include these glyphs,
9115 we use it->last_visible_x instead. */
9116 if (it->c == '\t')
9117 {
9118 it->continuation_lines_width += it->last_visible_x;
9119 /* When moving by vpos, ensure that the iterator really
9120 advances to the next line (bug#847, bug#969). Fixme:
9121 do we need to do this in other circumstances? */
9122 if (it->current_x != it->last_visible_x
9123 && (op & MOVE_TO_VPOS)
9124 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9125 {
9126 line_start_x = it->current_x + it->pixel_width
9127 - it->last_visible_x;
9128 set_iterator_to_next (it, 0);
9129 }
9130 }
9131 else
9132 it->continuation_lines_width += it->current_x;
9133 break;
9134
9135 default:
9136 emacs_abort ();
9137 }
9138
9139 /* Reset/increment for the next run. */
9140 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9141 it->current_x = line_start_x;
9142 line_start_x = 0;
9143 it->hpos = 0;
9144 it->current_y += it->max_ascent + it->max_descent;
9145 ++it->vpos;
9146 last_height = it->max_ascent + it->max_descent;
9147 it->max_ascent = it->max_descent = 0;
9148 }
9149
9150 out:
9151
9152 /* On text terminals, we may stop at the end of a line in the middle
9153 of a multi-character glyph. If the glyph itself is continued,
9154 i.e. it is actually displayed on the next line, don't treat this
9155 stopping point as valid; move to the next line instead (unless
9156 that brings us offscreen). */
9157 if (!FRAME_WINDOW_P (it->f)
9158 && op & MOVE_TO_POS
9159 && IT_CHARPOS (*it) == to_charpos
9160 && it->what == IT_CHARACTER
9161 && it->nglyphs > 1
9162 && it->line_wrap == WINDOW_WRAP
9163 && it->current_x == it->last_visible_x - 1
9164 && it->c != '\n'
9165 && it->c != '\t'
9166 && it->vpos < it->w->window_end_vpos)
9167 {
9168 it->continuation_lines_width += it->current_x;
9169 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9170 it->current_y += it->max_ascent + it->max_descent;
9171 ++it->vpos;
9172 last_height = it->max_ascent + it->max_descent;
9173 }
9174
9175 if (backup_data)
9176 bidi_unshelve_cache (backup_data, 1);
9177
9178 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9179 }
9180
9181
9182 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9183
9184 If DY > 0, move IT backward at least that many pixels. DY = 0
9185 means move IT backward to the preceding line start or BEGV. This
9186 function may move over more than DY pixels if IT->current_y - DY
9187 ends up in the middle of a line; in this case IT->current_y will be
9188 set to the top of the line moved to. */
9189
9190 void
9191 move_it_vertically_backward (struct it *it, int dy)
9192 {
9193 int nlines, h;
9194 struct it it2, it3;
9195 void *it2data = NULL, *it3data = NULL;
9196 ptrdiff_t start_pos;
9197 int nchars_per_row
9198 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9199 ptrdiff_t pos_limit;
9200
9201 move_further_back:
9202 eassert (dy >= 0);
9203
9204 start_pos = IT_CHARPOS (*it);
9205
9206 /* Estimate how many newlines we must move back. */
9207 nlines = max (1, dy / default_line_pixel_height (it->w));
9208 if (it->line_wrap == TRUNCATE)
9209 pos_limit = BEGV;
9210 else
9211 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9212
9213 /* Set the iterator's position that many lines back. But don't go
9214 back more than NLINES full screen lines -- this wins a day with
9215 buffers which have very long lines. */
9216 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9217 back_to_previous_visible_line_start (it);
9218
9219 /* Reseat the iterator here. When moving backward, we don't want
9220 reseat to skip forward over invisible text, set up the iterator
9221 to deliver from overlay strings at the new position etc. So,
9222 use reseat_1 here. */
9223 reseat_1 (it, it->current.pos, 1);
9224
9225 /* We are now surely at a line start. */
9226 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9227 reordering is in effect. */
9228 it->continuation_lines_width = 0;
9229
9230 /* Move forward and see what y-distance we moved. First move to the
9231 start of the next line so that we get its height. We need this
9232 height to be able to tell whether we reached the specified
9233 y-distance. */
9234 SAVE_IT (it2, *it, it2data);
9235 it2.max_ascent = it2.max_descent = 0;
9236 do
9237 {
9238 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9239 MOVE_TO_POS | MOVE_TO_VPOS);
9240 }
9241 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9242 /* If we are in a display string which starts at START_POS,
9243 and that display string includes a newline, and we are
9244 right after that newline (i.e. at the beginning of a
9245 display line), exit the loop, because otherwise we will
9246 infloop, since move_it_to will see that it is already at
9247 START_POS and will not move. */
9248 || (it2.method == GET_FROM_STRING
9249 && IT_CHARPOS (it2) == start_pos
9250 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9251 eassert (IT_CHARPOS (*it) >= BEGV);
9252 SAVE_IT (it3, it2, it3data);
9253
9254 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9255 eassert (IT_CHARPOS (*it) >= BEGV);
9256 /* H is the actual vertical distance from the position in *IT
9257 and the starting position. */
9258 h = it2.current_y - it->current_y;
9259 /* NLINES is the distance in number of lines. */
9260 nlines = it2.vpos - it->vpos;
9261
9262 /* Correct IT's y and vpos position
9263 so that they are relative to the starting point. */
9264 it->vpos -= nlines;
9265 it->current_y -= h;
9266
9267 if (dy == 0)
9268 {
9269 /* DY == 0 means move to the start of the screen line. The
9270 value of nlines is > 0 if continuation lines were involved,
9271 or if the original IT position was at start of a line. */
9272 RESTORE_IT (it, it, it2data);
9273 if (nlines > 0)
9274 move_it_by_lines (it, nlines);
9275 /* The above code moves us to some position NLINES down,
9276 usually to its first glyph (leftmost in an L2R line), but
9277 that's not necessarily the start of the line, under bidi
9278 reordering. We want to get to the character position
9279 that is immediately after the newline of the previous
9280 line. */
9281 if (it->bidi_p
9282 && !it->continuation_lines_width
9283 && !STRINGP (it->string)
9284 && IT_CHARPOS (*it) > BEGV
9285 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9286 {
9287 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9288
9289 DEC_BOTH (cp, bp);
9290 cp = find_newline_no_quit (cp, bp, -1, NULL);
9291 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9292 }
9293 bidi_unshelve_cache (it3data, 1);
9294 }
9295 else
9296 {
9297 /* The y-position we try to reach, relative to *IT.
9298 Note that H has been subtracted in front of the if-statement. */
9299 int target_y = it->current_y + h - dy;
9300 int y0 = it3.current_y;
9301 int y1;
9302 int line_height;
9303
9304 RESTORE_IT (&it3, &it3, it3data);
9305 y1 = line_bottom_y (&it3);
9306 line_height = y1 - y0;
9307 RESTORE_IT (it, it, it2data);
9308 /* If we did not reach target_y, try to move further backward if
9309 we can. If we moved too far backward, try to move forward. */
9310 if (target_y < it->current_y
9311 /* This is heuristic. In a window that's 3 lines high, with
9312 a line height of 13 pixels each, recentering with point
9313 on the bottom line will try to move -39/2 = 19 pixels
9314 backward. Try to avoid moving into the first line. */
9315 && (it->current_y - target_y
9316 > min (window_box_height (it->w), line_height * 2 / 3))
9317 && IT_CHARPOS (*it) > BEGV)
9318 {
9319 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9320 target_y - it->current_y));
9321 dy = it->current_y - target_y;
9322 goto move_further_back;
9323 }
9324 else if (target_y >= it->current_y + line_height
9325 && IT_CHARPOS (*it) < ZV)
9326 {
9327 /* Should move forward by at least one line, maybe more.
9328
9329 Note: Calling move_it_by_lines can be expensive on
9330 terminal frames, where compute_motion is used (via
9331 vmotion) to do the job, when there are very long lines
9332 and truncate-lines is nil. That's the reason for
9333 treating terminal frames specially here. */
9334
9335 if (!FRAME_WINDOW_P (it->f))
9336 move_it_vertically (it, target_y - (it->current_y + line_height));
9337 else
9338 {
9339 do
9340 {
9341 move_it_by_lines (it, 1);
9342 }
9343 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9344 }
9345 }
9346 }
9347 }
9348
9349
9350 /* Move IT by a specified amount of pixel lines DY. DY negative means
9351 move backwards. DY = 0 means move to start of screen line. At the
9352 end, IT will be on the start of a screen line. */
9353
9354 void
9355 move_it_vertically (struct it *it, int dy)
9356 {
9357 if (dy <= 0)
9358 move_it_vertically_backward (it, -dy);
9359 else
9360 {
9361 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9362 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9363 MOVE_TO_POS | MOVE_TO_Y);
9364 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9365
9366 /* If buffer ends in ZV without a newline, move to the start of
9367 the line to satisfy the post-condition. */
9368 if (IT_CHARPOS (*it) == ZV
9369 && ZV > BEGV
9370 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9371 move_it_by_lines (it, 0);
9372 }
9373 }
9374
9375
9376 /* Move iterator IT past the end of the text line it is in. */
9377
9378 void
9379 move_it_past_eol (struct it *it)
9380 {
9381 enum move_it_result rc;
9382
9383 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9384 if (rc == MOVE_NEWLINE_OR_CR)
9385 set_iterator_to_next (it, 0);
9386 }
9387
9388
9389 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9390 negative means move up. DVPOS == 0 means move to the start of the
9391 screen line.
9392
9393 Optimization idea: If we would know that IT->f doesn't use
9394 a face with proportional font, we could be faster for
9395 truncate-lines nil. */
9396
9397 void
9398 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9399 {
9400
9401 /* The commented-out optimization uses vmotion on terminals. This
9402 gives bad results, because elements like it->what, on which
9403 callers such as pos_visible_p rely, aren't updated. */
9404 /* struct position pos;
9405 if (!FRAME_WINDOW_P (it->f))
9406 {
9407 struct text_pos textpos;
9408
9409 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9410 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9411 reseat (it, textpos, 1);
9412 it->vpos += pos.vpos;
9413 it->current_y += pos.vpos;
9414 }
9415 else */
9416
9417 if (dvpos == 0)
9418 {
9419 /* DVPOS == 0 means move to the start of the screen line. */
9420 move_it_vertically_backward (it, 0);
9421 /* Let next call to line_bottom_y calculate real line height */
9422 last_height = 0;
9423 }
9424 else if (dvpos > 0)
9425 {
9426 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9427 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9428 {
9429 /* Only move to the next buffer position if we ended up in a
9430 string from display property, not in an overlay string
9431 (before-string or after-string). That is because the
9432 latter don't conceal the underlying buffer position, so
9433 we can ask to move the iterator to the exact position we
9434 are interested in. Note that, even if we are already at
9435 IT_CHARPOS (*it), the call below is not a no-op, as it
9436 will detect that we are at the end of the string, pop the
9437 iterator, and compute it->current_x and it->hpos
9438 correctly. */
9439 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9440 -1, -1, -1, MOVE_TO_POS);
9441 }
9442 }
9443 else
9444 {
9445 struct it it2;
9446 void *it2data = NULL;
9447 ptrdiff_t start_charpos, i;
9448 int nchars_per_row
9449 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9450 ptrdiff_t pos_limit;
9451
9452 /* Start at the beginning of the screen line containing IT's
9453 position. This may actually move vertically backwards,
9454 in case of overlays, so adjust dvpos accordingly. */
9455 dvpos += it->vpos;
9456 move_it_vertically_backward (it, 0);
9457 dvpos -= it->vpos;
9458
9459 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9460 screen lines, and reseat the iterator there. */
9461 start_charpos = IT_CHARPOS (*it);
9462 if (it->line_wrap == TRUNCATE)
9463 pos_limit = BEGV;
9464 else
9465 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9466 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9467 back_to_previous_visible_line_start (it);
9468 reseat (it, it->current.pos, 1);
9469
9470 /* Move further back if we end up in a string or an image. */
9471 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9472 {
9473 /* First try to move to start of display line. */
9474 dvpos += it->vpos;
9475 move_it_vertically_backward (it, 0);
9476 dvpos -= it->vpos;
9477 if (IT_POS_VALID_AFTER_MOVE_P (it))
9478 break;
9479 /* If start of line is still in string or image,
9480 move further back. */
9481 back_to_previous_visible_line_start (it);
9482 reseat (it, it->current.pos, 1);
9483 dvpos--;
9484 }
9485
9486 it->current_x = it->hpos = 0;
9487
9488 /* Above call may have moved too far if continuation lines
9489 are involved. Scan forward and see if it did. */
9490 SAVE_IT (it2, *it, it2data);
9491 it2.vpos = it2.current_y = 0;
9492 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9493 it->vpos -= it2.vpos;
9494 it->current_y -= it2.current_y;
9495 it->current_x = it->hpos = 0;
9496
9497 /* If we moved too far back, move IT some lines forward. */
9498 if (it2.vpos > -dvpos)
9499 {
9500 int delta = it2.vpos + dvpos;
9501
9502 RESTORE_IT (&it2, &it2, it2data);
9503 SAVE_IT (it2, *it, it2data);
9504 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9505 /* Move back again if we got too far ahead. */
9506 if (IT_CHARPOS (*it) >= start_charpos)
9507 RESTORE_IT (it, &it2, it2data);
9508 else
9509 bidi_unshelve_cache (it2data, 1);
9510 }
9511 else
9512 RESTORE_IT (it, it, it2data);
9513 }
9514 }
9515
9516 /* Return 1 if IT points into the middle of a display vector. */
9517
9518 int
9519 in_display_vector_p (struct it *it)
9520 {
9521 return (it->method == GET_FROM_DISPLAY_VECTOR
9522 && it->current.dpvec_index > 0
9523 && it->dpvec + it->current.dpvec_index != it->dpend);
9524 }
9525
9526 \f
9527 /***********************************************************************
9528 Messages
9529 ***********************************************************************/
9530
9531
9532 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9533 to *Messages*. */
9534
9535 void
9536 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9537 {
9538 Lisp_Object args[3];
9539 Lisp_Object msg, fmt;
9540 char *buffer;
9541 ptrdiff_t len;
9542 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9543 USE_SAFE_ALLOCA;
9544
9545 fmt = msg = Qnil;
9546 GCPRO4 (fmt, msg, arg1, arg2);
9547
9548 args[0] = fmt = build_string (format);
9549 args[1] = arg1;
9550 args[2] = arg2;
9551 msg = Fformat (3, args);
9552
9553 len = SBYTES (msg) + 1;
9554 buffer = SAFE_ALLOCA (len);
9555 memcpy (buffer, SDATA (msg), len);
9556
9557 message_dolog (buffer, len - 1, 1, 0);
9558 SAFE_FREE ();
9559
9560 UNGCPRO;
9561 }
9562
9563
9564 /* Output a newline in the *Messages* buffer if "needs" one. */
9565
9566 void
9567 message_log_maybe_newline (void)
9568 {
9569 if (message_log_need_newline)
9570 message_dolog ("", 0, 1, 0);
9571 }
9572
9573
9574 /* Add a string M of length NBYTES to the message log, optionally
9575 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9576 true, means interpret the contents of M as multibyte. This
9577 function calls low-level routines in order to bypass text property
9578 hooks, etc. which might not be safe to run.
9579
9580 This may GC (insert may run before/after change hooks),
9581 so the buffer M must NOT point to a Lisp string. */
9582
9583 void
9584 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9585 {
9586 const unsigned char *msg = (const unsigned char *) m;
9587
9588 if (!NILP (Vmemory_full))
9589 return;
9590
9591 if (!NILP (Vmessage_log_max))
9592 {
9593 struct buffer *oldbuf;
9594 Lisp_Object oldpoint, oldbegv, oldzv;
9595 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9596 ptrdiff_t point_at_end = 0;
9597 ptrdiff_t zv_at_end = 0;
9598 Lisp_Object old_deactivate_mark;
9599 bool shown;
9600 struct gcpro gcpro1;
9601
9602 old_deactivate_mark = Vdeactivate_mark;
9603 oldbuf = current_buffer;
9604 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9605 bset_undo_list (current_buffer, Qt);
9606
9607 oldpoint = message_dolog_marker1;
9608 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9609 oldbegv = message_dolog_marker2;
9610 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9611 oldzv = message_dolog_marker3;
9612 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9613 GCPRO1 (old_deactivate_mark);
9614
9615 if (PT == Z)
9616 point_at_end = 1;
9617 if (ZV == Z)
9618 zv_at_end = 1;
9619
9620 BEGV = BEG;
9621 BEGV_BYTE = BEG_BYTE;
9622 ZV = Z;
9623 ZV_BYTE = Z_BYTE;
9624 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9625
9626 /* Insert the string--maybe converting multibyte to single byte
9627 or vice versa, so that all the text fits the buffer. */
9628 if (multibyte
9629 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9630 {
9631 ptrdiff_t i;
9632 int c, char_bytes;
9633 char work[1];
9634
9635 /* Convert a multibyte string to single-byte
9636 for the *Message* buffer. */
9637 for (i = 0; i < nbytes; i += char_bytes)
9638 {
9639 c = string_char_and_length (msg + i, &char_bytes);
9640 work[0] = (ASCII_CHAR_P (c)
9641 ? c
9642 : multibyte_char_to_unibyte (c));
9643 insert_1_both (work, 1, 1, 1, 0, 0);
9644 }
9645 }
9646 else if (! multibyte
9647 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9648 {
9649 ptrdiff_t i;
9650 int c, char_bytes;
9651 unsigned char str[MAX_MULTIBYTE_LENGTH];
9652 /* Convert a single-byte string to multibyte
9653 for the *Message* buffer. */
9654 for (i = 0; i < nbytes; i++)
9655 {
9656 c = msg[i];
9657 MAKE_CHAR_MULTIBYTE (c);
9658 char_bytes = CHAR_STRING (c, str);
9659 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9660 }
9661 }
9662 else if (nbytes)
9663 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9664
9665 if (nlflag)
9666 {
9667 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9668 printmax_t dups;
9669
9670 insert_1_both ("\n", 1, 1, 1, 0, 0);
9671
9672 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9673 this_bol = PT;
9674 this_bol_byte = PT_BYTE;
9675
9676 /* See if this line duplicates the previous one.
9677 If so, combine duplicates. */
9678 if (this_bol > BEG)
9679 {
9680 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9681 prev_bol = PT;
9682 prev_bol_byte = PT_BYTE;
9683
9684 dups = message_log_check_duplicate (prev_bol_byte,
9685 this_bol_byte);
9686 if (dups)
9687 {
9688 del_range_both (prev_bol, prev_bol_byte,
9689 this_bol, this_bol_byte, 0);
9690 if (dups > 1)
9691 {
9692 char dupstr[sizeof " [ times]"
9693 + INT_STRLEN_BOUND (printmax_t)];
9694
9695 /* If you change this format, don't forget to also
9696 change message_log_check_duplicate. */
9697 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9698 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9699 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9700 }
9701 }
9702 }
9703
9704 /* If we have more than the desired maximum number of lines
9705 in the *Messages* buffer now, delete the oldest ones.
9706 This is safe because we don't have undo in this buffer. */
9707
9708 if (NATNUMP (Vmessage_log_max))
9709 {
9710 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9711 -XFASTINT (Vmessage_log_max) - 1, 0);
9712 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9713 }
9714 }
9715 BEGV = marker_position (oldbegv);
9716 BEGV_BYTE = marker_byte_position (oldbegv);
9717
9718 if (zv_at_end)
9719 {
9720 ZV = Z;
9721 ZV_BYTE = Z_BYTE;
9722 }
9723 else
9724 {
9725 ZV = marker_position (oldzv);
9726 ZV_BYTE = marker_byte_position (oldzv);
9727 }
9728
9729 if (point_at_end)
9730 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9731 else
9732 /* We can't do Fgoto_char (oldpoint) because it will run some
9733 Lisp code. */
9734 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9735 marker_byte_position (oldpoint));
9736
9737 UNGCPRO;
9738 unchain_marker (XMARKER (oldpoint));
9739 unchain_marker (XMARKER (oldbegv));
9740 unchain_marker (XMARKER (oldzv));
9741
9742 shown = buffer_window_count (current_buffer) > 0;
9743 set_buffer_internal (oldbuf);
9744 /* We called insert_1_both above with its 5th argument (PREPARE)
9745 zero, which prevents insert_1_both from calling
9746 prepare_to_modify_buffer, which in turns prevents us from
9747 incrementing windows_or_buffers_changed even if *Messages* is
9748 shown in some window. So we must manually incrementing
9749 windows_or_buffers_changed here to make up for that. */
9750 if (shown)
9751 windows_or_buffers_changed++;
9752 else
9753 windows_or_buffers_changed = old_windows_or_buffers_changed;
9754 message_log_need_newline = !nlflag;
9755 Vdeactivate_mark = old_deactivate_mark;
9756 }
9757 }
9758
9759
9760 /* We are at the end of the buffer after just having inserted a newline.
9761 (Note: We depend on the fact we won't be crossing the gap.)
9762 Check to see if the most recent message looks a lot like the previous one.
9763 Return 0 if different, 1 if the new one should just replace it, or a
9764 value N > 1 if we should also append " [N times]". */
9765
9766 static intmax_t
9767 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9768 {
9769 ptrdiff_t i;
9770 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9771 int seen_dots = 0;
9772 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9773 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9774
9775 for (i = 0; i < len; i++)
9776 {
9777 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9778 seen_dots = 1;
9779 if (p1[i] != p2[i])
9780 return seen_dots;
9781 }
9782 p1 += len;
9783 if (*p1 == '\n')
9784 return 2;
9785 if (*p1++ == ' ' && *p1++ == '[')
9786 {
9787 char *pend;
9788 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9789 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9790 return n + 1;
9791 }
9792 return 0;
9793 }
9794 \f
9795
9796 /* Display an echo area message M with a specified length of NBYTES
9797 bytes. The string may include null characters. If M is not a
9798 string, clear out any existing message, and let the mini-buffer
9799 text show through.
9800
9801 This function cancels echoing. */
9802
9803 void
9804 message3 (Lisp_Object m)
9805 {
9806 struct gcpro gcpro1;
9807
9808 GCPRO1 (m);
9809 clear_message (1,1);
9810 cancel_echoing ();
9811
9812 /* First flush out any partial line written with print. */
9813 message_log_maybe_newline ();
9814 if (STRINGP (m))
9815 {
9816 ptrdiff_t nbytes = SBYTES (m);
9817 bool multibyte = STRING_MULTIBYTE (m);
9818 USE_SAFE_ALLOCA;
9819 char *buffer = SAFE_ALLOCA (nbytes);
9820 memcpy (buffer, SDATA (m), nbytes);
9821 message_dolog (buffer, nbytes, 1, multibyte);
9822 SAFE_FREE ();
9823 }
9824 message3_nolog (m);
9825
9826 UNGCPRO;
9827 }
9828
9829
9830 /* The non-logging version of message3.
9831 This does not cancel echoing, because it is used for echoing.
9832 Perhaps we need to make a separate function for echoing
9833 and make this cancel echoing. */
9834
9835 void
9836 message3_nolog (Lisp_Object m)
9837 {
9838 struct frame *sf = SELECTED_FRAME ();
9839
9840 if (FRAME_INITIAL_P (sf))
9841 {
9842 if (noninteractive_need_newline)
9843 putc ('\n', stderr);
9844 noninteractive_need_newline = 0;
9845 if (STRINGP (m))
9846 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9847 if (cursor_in_echo_area == 0)
9848 fprintf (stderr, "\n");
9849 fflush (stderr);
9850 }
9851 /* Error messages get reported properly by cmd_error, so this must be just an
9852 informative message; if the frame hasn't really been initialized yet, just
9853 toss it. */
9854 else if (INTERACTIVE && sf->glyphs_initialized_p)
9855 {
9856 /* Get the frame containing the mini-buffer
9857 that the selected frame is using. */
9858 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9859 Lisp_Object frame = XWINDOW (mini_window)->frame;
9860 struct frame *f = XFRAME (frame);
9861
9862 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9863 Fmake_frame_visible (frame);
9864
9865 if (STRINGP (m) && SCHARS (m) > 0)
9866 {
9867 set_message (m);
9868 if (minibuffer_auto_raise)
9869 Fraise_frame (frame);
9870 /* Assume we are not echoing.
9871 (If we are, echo_now will override this.) */
9872 echo_message_buffer = Qnil;
9873 }
9874 else
9875 clear_message (1, 1);
9876
9877 do_pending_window_change (0);
9878 echo_area_display (1);
9879 do_pending_window_change (0);
9880 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9881 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9882 }
9883 }
9884
9885
9886 /* Display a null-terminated echo area message M. If M is 0, clear
9887 out any existing message, and let the mini-buffer text show through.
9888
9889 The buffer M must continue to exist until after the echo area gets
9890 cleared or some other message gets displayed there. Do not pass
9891 text that is stored in a Lisp string. Do not pass text in a buffer
9892 that was alloca'd. */
9893
9894 void
9895 message1 (const char *m)
9896 {
9897 message3 (m ? build_unibyte_string (m) : Qnil);
9898 }
9899
9900
9901 /* The non-logging counterpart of message1. */
9902
9903 void
9904 message1_nolog (const char *m)
9905 {
9906 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9907 }
9908
9909 /* Display a message M which contains a single %s
9910 which gets replaced with STRING. */
9911
9912 void
9913 message_with_string (const char *m, Lisp_Object string, int log)
9914 {
9915 CHECK_STRING (string);
9916
9917 if (noninteractive)
9918 {
9919 if (m)
9920 {
9921 if (noninteractive_need_newline)
9922 putc ('\n', stderr);
9923 noninteractive_need_newline = 0;
9924 fprintf (stderr, m, SDATA (string));
9925 if (!cursor_in_echo_area)
9926 fprintf (stderr, "\n");
9927 fflush (stderr);
9928 }
9929 }
9930 else if (INTERACTIVE)
9931 {
9932 /* The frame whose minibuffer we're going to display the message on.
9933 It may be larger than the selected frame, so we need
9934 to use its buffer, not the selected frame's buffer. */
9935 Lisp_Object mini_window;
9936 struct frame *f, *sf = SELECTED_FRAME ();
9937
9938 /* Get the frame containing the minibuffer
9939 that the selected frame is using. */
9940 mini_window = FRAME_MINIBUF_WINDOW (sf);
9941 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9942
9943 /* Error messages get reported properly by cmd_error, so this must be
9944 just an informative message; if the frame hasn't really been
9945 initialized yet, just toss it. */
9946 if (f->glyphs_initialized_p)
9947 {
9948 Lisp_Object args[2], msg;
9949 struct gcpro gcpro1, gcpro2;
9950
9951 args[0] = build_string (m);
9952 args[1] = msg = string;
9953 GCPRO2 (args[0], msg);
9954 gcpro1.nvars = 2;
9955
9956 msg = Fformat (2, args);
9957
9958 if (log)
9959 message3 (msg);
9960 else
9961 message3_nolog (msg);
9962
9963 UNGCPRO;
9964
9965 /* Print should start at the beginning of the message
9966 buffer next time. */
9967 message_buf_print = 0;
9968 }
9969 }
9970 }
9971
9972
9973 /* Dump an informative message to the minibuf. If M is 0, clear out
9974 any existing message, and let the mini-buffer text show through. */
9975
9976 static void
9977 vmessage (const char *m, va_list ap)
9978 {
9979 if (noninteractive)
9980 {
9981 if (m)
9982 {
9983 if (noninteractive_need_newline)
9984 putc ('\n', stderr);
9985 noninteractive_need_newline = 0;
9986 vfprintf (stderr, m, ap);
9987 if (cursor_in_echo_area == 0)
9988 fprintf (stderr, "\n");
9989 fflush (stderr);
9990 }
9991 }
9992 else if (INTERACTIVE)
9993 {
9994 /* The frame whose mini-buffer we're going to display the message
9995 on. It may be larger than the selected frame, so we need to
9996 use its buffer, not the selected frame's buffer. */
9997 Lisp_Object mini_window;
9998 struct frame *f, *sf = SELECTED_FRAME ();
9999
10000 /* Get the frame containing the mini-buffer
10001 that the selected frame is using. */
10002 mini_window = FRAME_MINIBUF_WINDOW (sf);
10003 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10004
10005 /* Error messages get reported properly by cmd_error, so this must be
10006 just an informative message; if the frame hasn't really been
10007 initialized yet, just toss it. */
10008 if (f->glyphs_initialized_p)
10009 {
10010 if (m)
10011 {
10012 ptrdiff_t len;
10013 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10014 char *message_buf = alloca (maxsize + 1);
10015
10016 len = doprnt (message_buf, maxsize, m, 0, ap);
10017
10018 message3 (make_string (message_buf, len));
10019 }
10020 else
10021 message1 (0);
10022
10023 /* Print should start at the beginning of the message
10024 buffer next time. */
10025 message_buf_print = 0;
10026 }
10027 }
10028 }
10029
10030 void
10031 message (const char *m, ...)
10032 {
10033 va_list ap;
10034 va_start (ap, m);
10035 vmessage (m, ap);
10036 va_end (ap);
10037 }
10038
10039
10040 #if 0
10041 /* The non-logging version of message. */
10042
10043 void
10044 message_nolog (const char *m, ...)
10045 {
10046 Lisp_Object old_log_max;
10047 va_list ap;
10048 va_start (ap, m);
10049 old_log_max = Vmessage_log_max;
10050 Vmessage_log_max = Qnil;
10051 vmessage (m, ap);
10052 Vmessage_log_max = old_log_max;
10053 va_end (ap);
10054 }
10055 #endif
10056
10057
10058 /* Display the current message in the current mini-buffer. This is
10059 only called from error handlers in process.c, and is not time
10060 critical. */
10061
10062 void
10063 update_echo_area (void)
10064 {
10065 if (!NILP (echo_area_buffer[0]))
10066 {
10067 Lisp_Object string;
10068 string = Fcurrent_message ();
10069 message3 (string);
10070 }
10071 }
10072
10073
10074 /* Make sure echo area buffers in `echo_buffers' are live.
10075 If they aren't, make new ones. */
10076
10077 static void
10078 ensure_echo_area_buffers (void)
10079 {
10080 int i;
10081
10082 for (i = 0; i < 2; ++i)
10083 if (!BUFFERP (echo_buffer[i])
10084 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10085 {
10086 char name[30];
10087 Lisp_Object old_buffer;
10088 int j;
10089
10090 old_buffer = echo_buffer[i];
10091 echo_buffer[i] = Fget_buffer_create
10092 (make_formatted_string (name, " *Echo Area %d*", i));
10093 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10094 /* to force word wrap in echo area -
10095 it was decided to postpone this*/
10096 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10097
10098 for (j = 0; j < 2; ++j)
10099 if (EQ (old_buffer, echo_area_buffer[j]))
10100 echo_area_buffer[j] = echo_buffer[i];
10101 }
10102 }
10103
10104
10105 /* Call FN with args A1..A2 with either the current or last displayed
10106 echo_area_buffer as current buffer.
10107
10108 WHICH zero means use the current message buffer
10109 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10110 from echo_buffer[] and clear it.
10111
10112 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10113 suitable buffer from echo_buffer[] and clear it.
10114
10115 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10116 that the current message becomes the last displayed one, make
10117 choose a suitable buffer for echo_area_buffer[0], and clear it.
10118
10119 Value is what FN returns. */
10120
10121 static int
10122 with_echo_area_buffer (struct window *w, int which,
10123 int (*fn) (ptrdiff_t, Lisp_Object),
10124 ptrdiff_t a1, Lisp_Object a2)
10125 {
10126 Lisp_Object buffer;
10127 int this_one, the_other, clear_buffer_p, rc;
10128 ptrdiff_t count = SPECPDL_INDEX ();
10129
10130 /* If buffers aren't live, make new ones. */
10131 ensure_echo_area_buffers ();
10132
10133 clear_buffer_p = 0;
10134
10135 if (which == 0)
10136 this_one = 0, the_other = 1;
10137 else if (which > 0)
10138 this_one = 1, the_other = 0;
10139 else
10140 {
10141 this_one = 0, the_other = 1;
10142 clear_buffer_p = 1;
10143
10144 /* We need a fresh one in case the current echo buffer equals
10145 the one containing the last displayed echo area message. */
10146 if (!NILP (echo_area_buffer[this_one])
10147 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10148 echo_area_buffer[this_one] = Qnil;
10149 }
10150
10151 /* Choose a suitable buffer from echo_buffer[] is we don't
10152 have one. */
10153 if (NILP (echo_area_buffer[this_one]))
10154 {
10155 echo_area_buffer[this_one]
10156 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10157 ? echo_buffer[the_other]
10158 : echo_buffer[this_one]);
10159 clear_buffer_p = 1;
10160 }
10161
10162 buffer = echo_area_buffer[this_one];
10163
10164 /* Don't get confused by reusing the buffer used for echoing
10165 for a different purpose. */
10166 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10167 cancel_echoing ();
10168
10169 record_unwind_protect (unwind_with_echo_area_buffer,
10170 with_echo_area_buffer_unwind_data (w));
10171
10172 /* Make the echo area buffer current. Note that for display
10173 purposes, it is not necessary that the displayed window's buffer
10174 == current_buffer, except for text property lookup. So, let's
10175 only set that buffer temporarily here without doing a full
10176 Fset_window_buffer. We must also change w->pointm, though,
10177 because otherwise an assertions in unshow_buffer fails, and Emacs
10178 aborts. */
10179 set_buffer_internal_1 (XBUFFER (buffer));
10180 if (w)
10181 {
10182 wset_buffer (w, buffer);
10183 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10184 }
10185
10186 bset_undo_list (current_buffer, Qt);
10187 bset_read_only (current_buffer, Qnil);
10188 specbind (Qinhibit_read_only, Qt);
10189 specbind (Qinhibit_modification_hooks, Qt);
10190
10191 if (clear_buffer_p && Z > BEG)
10192 del_range (BEG, Z);
10193
10194 eassert (BEGV >= BEG);
10195 eassert (ZV <= Z && ZV >= BEGV);
10196
10197 rc = fn (a1, a2);
10198
10199 eassert (BEGV >= BEG);
10200 eassert (ZV <= Z && ZV >= BEGV);
10201
10202 unbind_to (count, Qnil);
10203 return rc;
10204 }
10205
10206
10207 /* Save state that should be preserved around the call to the function
10208 FN called in with_echo_area_buffer. */
10209
10210 static Lisp_Object
10211 with_echo_area_buffer_unwind_data (struct window *w)
10212 {
10213 int i = 0;
10214 Lisp_Object vector, tmp;
10215
10216 /* Reduce consing by keeping one vector in
10217 Vwith_echo_area_save_vector. */
10218 vector = Vwith_echo_area_save_vector;
10219 Vwith_echo_area_save_vector = Qnil;
10220
10221 if (NILP (vector))
10222 vector = Fmake_vector (make_number (9), Qnil);
10223
10224 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10225 ASET (vector, i, Vdeactivate_mark); ++i;
10226 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10227
10228 if (w)
10229 {
10230 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10231 ASET (vector, i, w->contents); ++i;
10232 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10233 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10234 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10235 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10236 }
10237 else
10238 {
10239 int end = i + 6;
10240 for (; i < end; ++i)
10241 ASET (vector, i, Qnil);
10242 }
10243
10244 eassert (i == ASIZE (vector));
10245 return vector;
10246 }
10247
10248
10249 /* Restore global state from VECTOR which was created by
10250 with_echo_area_buffer_unwind_data. */
10251
10252 static void
10253 unwind_with_echo_area_buffer (Lisp_Object vector)
10254 {
10255 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10256 Vdeactivate_mark = AREF (vector, 1);
10257 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10258
10259 if (WINDOWP (AREF (vector, 3)))
10260 {
10261 struct window *w;
10262 Lisp_Object buffer;
10263
10264 w = XWINDOW (AREF (vector, 3));
10265 buffer = AREF (vector, 4);
10266
10267 wset_buffer (w, buffer);
10268 set_marker_both (w->pointm, buffer,
10269 XFASTINT (AREF (vector, 5)),
10270 XFASTINT (AREF (vector, 6)));
10271 set_marker_both (w->start, buffer,
10272 XFASTINT (AREF (vector, 7)),
10273 XFASTINT (AREF (vector, 8)));
10274 }
10275
10276 Vwith_echo_area_save_vector = vector;
10277 }
10278
10279
10280 /* Set up the echo area for use by print functions. MULTIBYTE_P
10281 non-zero means we will print multibyte. */
10282
10283 void
10284 setup_echo_area_for_printing (int multibyte_p)
10285 {
10286 /* If we can't find an echo area any more, exit. */
10287 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10288 Fkill_emacs (Qnil);
10289
10290 ensure_echo_area_buffers ();
10291
10292 if (!message_buf_print)
10293 {
10294 /* A message has been output since the last time we printed.
10295 Choose a fresh echo area buffer. */
10296 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10297 echo_area_buffer[0] = echo_buffer[1];
10298 else
10299 echo_area_buffer[0] = echo_buffer[0];
10300
10301 /* Switch to that buffer and clear it. */
10302 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10303 bset_truncate_lines (current_buffer, Qnil);
10304
10305 if (Z > BEG)
10306 {
10307 ptrdiff_t count = SPECPDL_INDEX ();
10308 specbind (Qinhibit_read_only, Qt);
10309 /* Note that undo recording is always disabled. */
10310 del_range (BEG, Z);
10311 unbind_to (count, Qnil);
10312 }
10313 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10314
10315 /* Set up the buffer for the multibyteness we need. */
10316 if (multibyte_p
10317 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10318 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10319
10320 /* Raise the frame containing the echo area. */
10321 if (minibuffer_auto_raise)
10322 {
10323 struct frame *sf = SELECTED_FRAME ();
10324 Lisp_Object mini_window;
10325 mini_window = FRAME_MINIBUF_WINDOW (sf);
10326 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10327 }
10328
10329 message_log_maybe_newline ();
10330 message_buf_print = 1;
10331 }
10332 else
10333 {
10334 if (NILP (echo_area_buffer[0]))
10335 {
10336 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10337 echo_area_buffer[0] = echo_buffer[1];
10338 else
10339 echo_area_buffer[0] = echo_buffer[0];
10340 }
10341
10342 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10343 {
10344 /* Someone switched buffers between print requests. */
10345 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10346 bset_truncate_lines (current_buffer, Qnil);
10347 }
10348 }
10349 }
10350
10351
10352 /* Display an echo area message in window W. Value is non-zero if W's
10353 height is changed. If display_last_displayed_message_p is
10354 non-zero, display the message that was last displayed, otherwise
10355 display the current message. */
10356
10357 static int
10358 display_echo_area (struct window *w)
10359 {
10360 int i, no_message_p, window_height_changed_p;
10361
10362 /* Temporarily disable garbage collections while displaying the echo
10363 area. This is done because a GC can print a message itself.
10364 That message would modify the echo area buffer's contents while a
10365 redisplay of the buffer is going on, and seriously confuse
10366 redisplay. */
10367 ptrdiff_t count = inhibit_garbage_collection ();
10368
10369 /* If there is no message, we must call display_echo_area_1
10370 nevertheless because it resizes the window. But we will have to
10371 reset the echo_area_buffer in question to nil at the end because
10372 with_echo_area_buffer will sets it to an empty buffer. */
10373 i = display_last_displayed_message_p ? 1 : 0;
10374 no_message_p = NILP (echo_area_buffer[i]);
10375
10376 window_height_changed_p
10377 = with_echo_area_buffer (w, display_last_displayed_message_p,
10378 display_echo_area_1,
10379 (intptr_t) w, Qnil);
10380
10381 if (no_message_p)
10382 echo_area_buffer[i] = Qnil;
10383
10384 unbind_to (count, Qnil);
10385 return window_height_changed_p;
10386 }
10387
10388
10389 /* Helper for display_echo_area. Display the current buffer which
10390 contains the current echo area message in window W, a mini-window,
10391 a pointer to which is passed in A1. A2..A4 are currently not used.
10392 Change the height of W so that all of the message is displayed.
10393 Value is non-zero if height of W was changed. */
10394
10395 static int
10396 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10397 {
10398 intptr_t i1 = a1;
10399 struct window *w = (struct window *) i1;
10400 Lisp_Object window;
10401 struct text_pos start;
10402 int window_height_changed_p = 0;
10403
10404 /* Do this before displaying, so that we have a large enough glyph
10405 matrix for the display. If we can't get enough space for the
10406 whole text, display the last N lines. That works by setting w->start. */
10407 window_height_changed_p = resize_mini_window (w, 0);
10408
10409 /* Use the starting position chosen by resize_mini_window. */
10410 SET_TEXT_POS_FROM_MARKER (start, w->start);
10411
10412 /* Display. */
10413 clear_glyph_matrix (w->desired_matrix);
10414 XSETWINDOW (window, w);
10415 try_window (window, start, 0);
10416
10417 return window_height_changed_p;
10418 }
10419
10420
10421 /* Resize the echo area window to exactly the size needed for the
10422 currently displayed message, if there is one. If a mini-buffer
10423 is active, don't shrink it. */
10424
10425 void
10426 resize_echo_area_exactly (void)
10427 {
10428 if (BUFFERP (echo_area_buffer[0])
10429 && WINDOWP (echo_area_window))
10430 {
10431 struct window *w = XWINDOW (echo_area_window);
10432 int resized_p;
10433 Lisp_Object resize_exactly;
10434
10435 if (minibuf_level == 0)
10436 resize_exactly = Qt;
10437 else
10438 resize_exactly = Qnil;
10439
10440 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10441 (intptr_t) w, resize_exactly);
10442 if (resized_p)
10443 {
10444 ++windows_or_buffers_changed;
10445 ++update_mode_lines;
10446 redisplay_internal ();
10447 }
10448 }
10449 }
10450
10451
10452 /* Callback function for with_echo_area_buffer, when used from
10453 resize_echo_area_exactly. A1 contains a pointer to the window to
10454 resize, EXACTLY non-nil means resize the mini-window exactly to the
10455 size of the text displayed. A3 and A4 are not used. Value is what
10456 resize_mini_window returns. */
10457
10458 static int
10459 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10460 {
10461 intptr_t i1 = a1;
10462 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10463 }
10464
10465
10466 /* Resize mini-window W to fit the size of its contents. EXACT_P
10467 means size the window exactly to the size needed. Otherwise, it's
10468 only enlarged until W's buffer is empty.
10469
10470 Set W->start to the right place to begin display. If the whole
10471 contents fit, start at the beginning. Otherwise, start so as
10472 to make the end of the contents appear. This is particularly
10473 important for y-or-n-p, but seems desirable generally.
10474
10475 Value is non-zero if the window height has been changed. */
10476
10477 int
10478 resize_mini_window (struct window *w, int exact_p)
10479 {
10480 struct frame *f = XFRAME (w->frame);
10481 int window_height_changed_p = 0;
10482
10483 eassert (MINI_WINDOW_P (w));
10484
10485 /* By default, start display at the beginning. */
10486 set_marker_both (w->start, w->contents,
10487 BUF_BEGV (XBUFFER (w->contents)),
10488 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10489
10490 /* Don't resize windows while redisplaying a window; it would
10491 confuse redisplay functions when the size of the window they are
10492 displaying changes from under them. Such a resizing can happen,
10493 for instance, when which-func prints a long message while
10494 we are running fontification-functions. We're running these
10495 functions with safe_call which binds inhibit-redisplay to t. */
10496 if (!NILP (Vinhibit_redisplay))
10497 return 0;
10498
10499 /* Nil means don't try to resize. */
10500 if (NILP (Vresize_mini_windows)
10501 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10502 return 0;
10503
10504 if (!FRAME_MINIBUF_ONLY_P (f))
10505 {
10506 struct it it;
10507 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10508 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10509 int height;
10510 EMACS_INT max_height;
10511 int unit = FRAME_LINE_HEIGHT (f);
10512 struct text_pos start;
10513 struct buffer *old_current_buffer = NULL;
10514
10515 if (current_buffer != XBUFFER (w->contents))
10516 {
10517 old_current_buffer = current_buffer;
10518 set_buffer_internal (XBUFFER (w->contents));
10519 }
10520
10521 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10522
10523 /* Compute the max. number of lines specified by the user. */
10524 if (FLOATP (Vmax_mini_window_height))
10525 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10526 else if (INTEGERP (Vmax_mini_window_height))
10527 max_height = XINT (Vmax_mini_window_height);
10528 else
10529 max_height = total_height / 4;
10530
10531 /* Correct that max. height if it's bogus. */
10532 max_height = clip_to_bounds (1, max_height, total_height);
10533
10534 /* Find out the height of the text in the window. */
10535 if (it.line_wrap == TRUNCATE)
10536 height = 1;
10537 else
10538 {
10539 last_height = 0;
10540 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10541 if (it.max_ascent == 0 && it.max_descent == 0)
10542 height = it.current_y + last_height;
10543 else
10544 height = it.current_y + it.max_ascent + it.max_descent;
10545 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10546 height = (height + unit - 1) / unit;
10547 }
10548
10549 /* Compute a suitable window start. */
10550 if (height > max_height)
10551 {
10552 height = max_height;
10553 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10554 move_it_vertically_backward (&it, (height - 1) * unit);
10555 start = it.current.pos;
10556 }
10557 else
10558 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10559 SET_MARKER_FROM_TEXT_POS (w->start, start);
10560
10561 if (EQ (Vresize_mini_windows, Qgrow_only))
10562 {
10563 /* Let it grow only, until we display an empty message, in which
10564 case the window shrinks again. */
10565 if (height > WINDOW_TOTAL_LINES (w))
10566 {
10567 int old_height = WINDOW_TOTAL_LINES (w);
10568
10569 FRAME_WINDOWS_FROZEN (f) = 1;
10570 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10571 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10572 }
10573 else if (height < WINDOW_TOTAL_LINES (w)
10574 && (exact_p || BEGV == ZV))
10575 {
10576 int old_height = WINDOW_TOTAL_LINES (w);
10577
10578 FRAME_WINDOWS_FROZEN (f) = 0;
10579 shrink_mini_window (w);
10580 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10581 }
10582 }
10583 else
10584 {
10585 /* Always resize to exact size needed. */
10586 if (height > WINDOW_TOTAL_LINES (w))
10587 {
10588 int old_height = WINDOW_TOTAL_LINES (w);
10589
10590 FRAME_WINDOWS_FROZEN (f) = 1;
10591 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10592 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10593 }
10594 else if (height < WINDOW_TOTAL_LINES (w))
10595 {
10596 int old_height = WINDOW_TOTAL_LINES (w);
10597
10598 FRAME_WINDOWS_FROZEN (f) = 0;
10599 shrink_mini_window (w);
10600
10601 if (height)
10602 {
10603 FRAME_WINDOWS_FROZEN (f) = 1;
10604 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10605 }
10606
10607 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10608 }
10609 }
10610
10611 if (old_current_buffer)
10612 set_buffer_internal (old_current_buffer);
10613 }
10614
10615 return window_height_changed_p;
10616 }
10617
10618
10619 /* Value is the current message, a string, or nil if there is no
10620 current message. */
10621
10622 Lisp_Object
10623 current_message (void)
10624 {
10625 Lisp_Object msg;
10626
10627 if (!BUFFERP (echo_area_buffer[0]))
10628 msg = Qnil;
10629 else
10630 {
10631 with_echo_area_buffer (0, 0, current_message_1,
10632 (intptr_t) &msg, Qnil);
10633 if (NILP (msg))
10634 echo_area_buffer[0] = Qnil;
10635 }
10636
10637 return msg;
10638 }
10639
10640
10641 static int
10642 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10643 {
10644 intptr_t i1 = a1;
10645 Lisp_Object *msg = (Lisp_Object *) i1;
10646
10647 if (Z > BEG)
10648 *msg = make_buffer_string (BEG, Z, 1);
10649 else
10650 *msg = Qnil;
10651 return 0;
10652 }
10653
10654
10655 /* Push the current message on Vmessage_stack for later restoration
10656 by restore_message. Value is non-zero if the current message isn't
10657 empty. This is a relatively infrequent operation, so it's not
10658 worth optimizing. */
10659
10660 bool
10661 push_message (void)
10662 {
10663 Lisp_Object msg = current_message ();
10664 Vmessage_stack = Fcons (msg, Vmessage_stack);
10665 return STRINGP (msg);
10666 }
10667
10668
10669 /* Restore message display from the top of Vmessage_stack. */
10670
10671 void
10672 restore_message (void)
10673 {
10674 eassert (CONSP (Vmessage_stack));
10675 message3_nolog (XCAR (Vmessage_stack));
10676 }
10677
10678
10679 /* Handler for unwind-protect calling pop_message. */
10680
10681 void
10682 pop_message_unwind (void)
10683 {
10684 /* Pop the top-most entry off Vmessage_stack. */
10685 eassert (CONSP (Vmessage_stack));
10686 Vmessage_stack = XCDR (Vmessage_stack);
10687 }
10688
10689
10690 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10691 exits. If the stack is not empty, we have a missing pop_message
10692 somewhere. */
10693
10694 void
10695 check_message_stack (void)
10696 {
10697 if (!NILP (Vmessage_stack))
10698 emacs_abort ();
10699 }
10700
10701
10702 /* Truncate to NCHARS what will be displayed in the echo area the next
10703 time we display it---but don't redisplay it now. */
10704
10705 void
10706 truncate_echo_area (ptrdiff_t nchars)
10707 {
10708 if (nchars == 0)
10709 echo_area_buffer[0] = Qnil;
10710 else if (!noninteractive
10711 && INTERACTIVE
10712 && !NILP (echo_area_buffer[0]))
10713 {
10714 struct frame *sf = SELECTED_FRAME ();
10715 /* Error messages get reported properly by cmd_error, so this must be
10716 just an informative message; if the frame hasn't really been
10717 initialized yet, just toss it. */
10718 if (sf->glyphs_initialized_p)
10719 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10720 }
10721 }
10722
10723
10724 /* Helper function for truncate_echo_area. Truncate the current
10725 message to at most NCHARS characters. */
10726
10727 static int
10728 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10729 {
10730 if (BEG + nchars < Z)
10731 del_range (BEG + nchars, Z);
10732 if (Z == BEG)
10733 echo_area_buffer[0] = Qnil;
10734 return 0;
10735 }
10736
10737 /* Set the current message to STRING. */
10738
10739 static void
10740 set_message (Lisp_Object string)
10741 {
10742 eassert (STRINGP (string));
10743
10744 message_enable_multibyte = STRING_MULTIBYTE (string);
10745
10746 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10747 message_buf_print = 0;
10748 help_echo_showing_p = 0;
10749
10750 if (STRINGP (Vdebug_on_message)
10751 && STRINGP (string)
10752 && fast_string_match (Vdebug_on_message, string) >= 0)
10753 call_debugger (list2 (Qerror, string));
10754 }
10755
10756
10757 /* Helper function for set_message. First argument is ignored and second
10758 argument has the same meaning as for set_message.
10759 This function is called with the echo area buffer being current. */
10760
10761 static int
10762 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10763 {
10764 eassert (STRINGP (string));
10765
10766 /* Change multibyteness of the echo buffer appropriately. */
10767 if (message_enable_multibyte
10768 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10769 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10770
10771 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10772 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10773 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10774
10775 /* Insert new message at BEG. */
10776 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10777
10778 /* This function takes care of single/multibyte conversion.
10779 We just have to ensure that the echo area buffer has the right
10780 setting of enable_multibyte_characters. */
10781 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10782
10783 return 0;
10784 }
10785
10786
10787 /* Clear messages. CURRENT_P non-zero means clear the current
10788 message. LAST_DISPLAYED_P non-zero means clear the message
10789 last displayed. */
10790
10791 void
10792 clear_message (int current_p, int last_displayed_p)
10793 {
10794 if (current_p)
10795 {
10796 echo_area_buffer[0] = Qnil;
10797 message_cleared_p = 1;
10798 }
10799
10800 if (last_displayed_p)
10801 echo_area_buffer[1] = Qnil;
10802
10803 message_buf_print = 0;
10804 }
10805
10806 /* Clear garbaged frames.
10807
10808 This function is used where the old redisplay called
10809 redraw_garbaged_frames which in turn called redraw_frame which in
10810 turn called clear_frame. The call to clear_frame was a source of
10811 flickering. I believe a clear_frame is not necessary. It should
10812 suffice in the new redisplay to invalidate all current matrices,
10813 and ensure a complete redisplay of all windows. */
10814
10815 static void
10816 clear_garbaged_frames (void)
10817 {
10818 if (frame_garbaged)
10819 {
10820 Lisp_Object tail, frame;
10821 int changed_count = 0;
10822
10823 FOR_EACH_FRAME (tail, frame)
10824 {
10825 struct frame *f = XFRAME (frame);
10826
10827 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10828 {
10829 if (f->resized_p)
10830 {
10831 redraw_frame (f);
10832 f->force_flush_display_p = 1;
10833 }
10834 clear_current_matrices (f);
10835 changed_count++;
10836 f->garbaged = 0;
10837 f->resized_p = 0;
10838 }
10839 }
10840
10841 frame_garbaged = 0;
10842 if (changed_count)
10843 ++windows_or_buffers_changed;
10844 }
10845 }
10846
10847
10848 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10849 is non-zero update selected_frame. Value is non-zero if the
10850 mini-windows height has been changed. */
10851
10852 static int
10853 echo_area_display (int update_frame_p)
10854 {
10855 Lisp_Object mini_window;
10856 struct window *w;
10857 struct frame *f;
10858 int window_height_changed_p = 0;
10859 struct frame *sf = SELECTED_FRAME ();
10860
10861 mini_window = FRAME_MINIBUF_WINDOW (sf);
10862 w = XWINDOW (mini_window);
10863 f = XFRAME (WINDOW_FRAME (w));
10864
10865 /* Don't display if frame is invisible or not yet initialized. */
10866 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10867 return 0;
10868
10869 #ifdef HAVE_WINDOW_SYSTEM
10870 /* When Emacs starts, selected_frame may be the initial terminal
10871 frame. If we let this through, a message would be displayed on
10872 the terminal. */
10873 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10874 return 0;
10875 #endif /* HAVE_WINDOW_SYSTEM */
10876
10877 /* Redraw garbaged frames. */
10878 clear_garbaged_frames ();
10879
10880 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10881 {
10882 echo_area_window = mini_window;
10883 window_height_changed_p = display_echo_area (w);
10884 w->must_be_updated_p = 1;
10885
10886 /* Update the display, unless called from redisplay_internal.
10887 Also don't update the screen during redisplay itself. The
10888 update will happen at the end of redisplay, and an update
10889 here could cause confusion. */
10890 if (update_frame_p && !redisplaying_p)
10891 {
10892 int n = 0;
10893
10894 /* If the display update has been interrupted by pending
10895 input, update mode lines in the frame. Due to the
10896 pending input, it might have been that redisplay hasn't
10897 been called, so that mode lines above the echo area are
10898 garbaged. This looks odd, so we prevent it here. */
10899 if (!display_completed)
10900 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10901
10902 if (window_height_changed_p
10903 /* Don't do this if Emacs is shutting down. Redisplay
10904 needs to run hooks. */
10905 && !NILP (Vrun_hooks))
10906 {
10907 /* Must update other windows. Likewise as in other
10908 cases, don't let this update be interrupted by
10909 pending input. */
10910 ptrdiff_t count = SPECPDL_INDEX ();
10911 specbind (Qredisplay_dont_pause, Qt);
10912 windows_or_buffers_changed = 1;
10913 redisplay_internal ();
10914 unbind_to (count, Qnil);
10915 }
10916 else if (FRAME_WINDOW_P (f) && n == 0)
10917 {
10918 /* Window configuration is the same as before.
10919 Can do with a display update of the echo area,
10920 unless we displayed some mode lines. */
10921 update_single_window (w, 1);
10922 FRAME_RIF (f)->flush_display (f);
10923 }
10924 else
10925 update_frame (f, 1, 1);
10926
10927 /* If cursor is in the echo area, make sure that the next
10928 redisplay displays the minibuffer, so that the cursor will
10929 be replaced with what the minibuffer wants. */
10930 if (cursor_in_echo_area)
10931 ++windows_or_buffers_changed;
10932 }
10933 }
10934 else if (!EQ (mini_window, selected_window))
10935 windows_or_buffers_changed++;
10936
10937 /* Last displayed message is now the current message. */
10938 echo_area_buffer[1] = echo_area_buffer[0];
10939 /* Inform read_char that we're not echoing. */
10940 echo_message_buffer = Qnil;
10941
10942 /* Prevent redisplay optimization in redisplay_internal by resetting
10943 this_line_start_pos. This is done because the mini-buffer now
10944 displays the message instead of its buffer text. */
10945 if (EQ (mini_window, selected_window))
10946 CHARPOS (this_line_start_pos) = 0;
10947
10948 return window_height_changed_p;
10949 }
10950
10951 /* Nonzero if the current window's buffer is shown in more than one
10952 window and was modified since last redisplay. */
10953
10954 static int
10955 buffer_shared_and_changed (void)
10956 {
10957 return (buffer_window_count (current_buffer) > 1
10958 && UNCHANGED_MODIFIED < MODIFF);
10959 }
10960
10961 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10962 is enabled and mark of W's buffer was changed since last W's update. */
10963
10964 static int
10965 window_buffer_changed (struct window *w)
10966 {
10967 struct buffer *b = XBUFFER (w->contents);
10968
10969 eassert (BUFFER_LIVE_P (b));
10970
10971 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10972 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10973 != (w->region_showing != 0)));
10974 }
10975
10976 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10977
10978 static int
10979 mode_line_update_needed (struct window *w)
10980 {
10981 return (w->column_number_displayed != -1
10982 && !(PT == w->last_point && !window_outdated (w))
10983 && (w->column_number_displayed != current_column ()));
10984 }
10985
10986 /* Nonzero if window start of W is frozen and may not be changed during
10987 redisplay. */
10988
10989 static bool
10990 window_frozen_p (struct window *w)
10991 {
10992 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10993 {
10994 Lisp_Object window;
10995
10996 XSETWINDOW (window, w);
10997 if (MINI_WINDOW_P (w))
10998 return 0;
10999 else if (EQ (window, selected_window))
11000 return 0;
11001 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11002 && EQ (window, Vminibuf_scroll_window))
11003 /* This special window can't be frozen too. */
11004 return 0;
11005 else
11006 return 1;
11007 }
11008 return 0;
11009 }
11010
11011 /***********************************************************************
11012 Mode Lines and Frame Titles
11013 ***********************************************************************/
11014
11015 /* A buffer for constructing non-propertized mode-line strings and
11016 frame titles in it; allocated from the heap in init_xdisp and
11017 resized as needed in store_mode_line_noprop_char. */
11018
11019 static char *mode_line_noprop_buf;
11020
11021 /* The buffer's end, and a current output position in it. */
11022
11023 static char *mode_line_noprop_buf_end;
11024 static char *mode_line_noprop_ptr;
11025
11026 #define MODE_LINE_NOPROP_LEN(start) \
11027 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11028
11029 static enum {
11030 MODE_LINE_DISPLAY = 0,
11031 MODE_LINE_TITLE,
11032 MODE_LINE_NOPROP,
11033 MODE_LINE_STRING
11034 } mode_line_target;
11035
11036 /* Alist that caches the results of :propertize.
11037 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11038 static Lisp_Object mode_line_proptrans_alist;
11039
11040 /* List of strings making up the mode-line. */
11041 static Lisp_Object mode_line_string_list;
11042
11043 /* Base face property when building propertized mode line string. */
11044 static Lisp_Object mode_line_string_face;
11045 static Lisp_Object mode_line_string_face_prop;
11046
11047
11048 /* Unwind data for mode line strings */
11049
11050 static Lisp_Object Vmode_line_unwind_vector;
11051
11052 static Lisp_Object
11053 format_mode_line_unwind_data (struct frame *target_frame,
11054 struct buffer *obuf,
11055 Lisp_Object owin,
11056 int save_proptrans)
11057 {
11058 Lisp_Object vector, tmp;
11059
11060 /* Reduce consing by keeping one vector in
11061 Vwith_echo_area_save_vector. */
11062 vector = Vmode_line_unwind_vector;
11063 Vmode_line_unwind_vector = Qnil;
11064
11065 if (NILP (vector))
11066 vector = Fmake_vector (make_number (10), Qnil);
11067
11068 ASET (vector, 0, make_number (mode_line_target));
11069 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11070 ASET (vector, 2, mode_line_string_list);
11071 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11072 ASET (vector, 4, mode_line_string_face);
11073 ASET (vector, 5, mode_line_string_face_prop);
11074
11075 if (obuf)
11076 XSETBUFFER (tmp, obuf);
11077 else
11078 tmp = Qnil;
11079 ASET (vector, 6, tmp);
11080 ASET (vector, 7, owin);
11081 if (target_frame)
11082 {
11083 /* Similarly to `with-selected-window', if the operation selects
11084 a window on another frame, we must restore that frame's
11085 selected window, and (for a tty) the top-frame. */
11086 ASET (vector, 8, target_frame->selected_window);
11087 if (FRAME_TERMCAP_P (target_frame))
11088 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11089 }
11090
11091 return vector;
11092 }
11093
11094 static void
11095 unwind_format_mode_line (Lisp_Object vector)
11096 {
11097 Lisp_Object old_window = AREF (vector, 7);
11098 Lisp_Object target_frame_window = AREF (vector, 8);
11099 Lisp_Object old_top_frame = AREF (vector, 9);
11100
11101 mode_line_target = XINT (AREF (vector, 0));
11102 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11103 mode_line_string_list = AREF (vector, 2);
11104 if (! EQ (AREF (vector, 3), Qt))
11105 mode_line_proptrans_alist = AREF (vector, 3);
11106 mode_line_string_face = AREF (vector, 4);
11107 mode_line_string_face_prop = AREF (vector, 5);
11108
11109 /* Select window before buffer, since it may change the buffer. */
11110 if (!NILP (old_window))
11111 {
11112 /* If the operation that we are unwinding had selected a window
11113 on a different frame, reset its frame-selected-window. For a
11114 text terminal, reset its top-frame if necessary. */
11115 if (!NILP (target_frame_window))
11116 {
11117 Lisp_Object frame
11118 = WINDOW_FRAME (XWINDOW (target_frame_window));
11119
11120 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11121 Fselect_window (target_frame_window, Qt);
11122
11123 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11124 Fselect_frame (old_top_frame, Qt);
11125 }
11126
11127 Fselect_window (old_window, Qt);
11128 }
11129
11130 if (!NILP (AREF (vector, 6)))
11131 {
11132 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11133 ASET (vector, 6, Qnil);
11134 }
11135
11136 Vmode_line_unwind_vector = vector;
11137 }
11138
11139
11140 /* Store a single character C for the frame title in mode_line_noprop_buf.
11141 Re-allocate mode_line_noprop_buf if necessary. */
11142
11143 static void
11144 store_mode_line_noprop_char (char c)
11145 {
11146 /* If output position has reached the end of the allocated buffer,
11147 increase the buffer's size. */
11148 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11149 {
11150 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11151 ptrdiff_t size = len;
11152 mode_line_noprop_buf =
11153 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11154 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11155 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11156 }
11157
11158 *mode_line_noprop_ptr++ = c;
11159 }
11160
11161
11162 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11163 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11164 characters that yield more columns than PRECISION; PRECISION <= 0
11165 means copy the whole string. Pad with spaces until FIELD_WIDTH
11166 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11167 pad. Called from display_mode_element when it is used to build a
11168 frame title. */
11169
11170 static int
11171 store_mode_line_noprop (const char *string, int field_width, int precision)
11172 {
11173 const unsigned char *str = (const unsigned char *) string;
11174 int n = 0;
11175 ptrdiff_t dummy, nbytes;
11176
11177 /* Copy at most PRECISION chars from STR. */
11178 nbytes = strlen (string);
11179 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11180 while (nbytes--)
11181 store_mode_line_noprop_char (*str++);
11182
11183 /* Fill up with spaces until FIELD_WIDTH reached. */
11184 while (field_width > 0
11185 && n < field_width)
11186 {
11187 store_mode_line_noprop_char (' ');
11188 ++n;
11189 }
11190
11191 return n;
11192 }
11193
11194 /***********************************************************************
11195 Frame Titles
11196 ***********************************************************************/
11197
11198 #ifdef HAVE_WINDOW_SYSTEM
11199
11200 /* Set the title of FRAME, if it has changed. The title format is
11201 Vicon_title_format if FRAME is iconified, otherwise it is
11202 frame_title_format. */
11203
11204 static void
11205 x_consider_frame_title (Lisp_Object frame)
11206 {
11207 struct frame *f = XFRAME (frame);
11208
11209 if (FRAME_WINDOW_P (f)
11210 || FRAME_MINIBUF_ONLY_P (f)
11211 || f->explicit_name)
11212 {
11213 /* Do we have more than one visible frame on this X display? */
11214 Lisp_Object tail, other_frame, fmt;
11215 ptrdiff_t title_start;
11216 char *title;
11217 ptrdiff_t len;
11218 struct it it;
11219 ptrdiff_t count = SPECPDL_INDEX ();
11220
11221 FOR_EACH_FRAME (tail, other_frame)
11222 {
11223 struct frame *tf = XFRAME (other_frame);
11224
11225 if (tf != f
11226 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11227 && !FRAME_MINIBUF_ONLY_P (tf)
11228 && !EQ (other_frame, tip_frame)
11229 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11230 break;
11231 }
11232
11233 /* Set global variable indicating that multiple frames exist. */
11234 multiple_frames = CONSP (tail);
11235
11236 /* Switch to the buffer of selected window of the frame. Set up
11237 mode_line_target so that display_mode_element will output into
11238 mode_line_noprop_buf; then display the title. */
11239 record_unwind_protect (unwind_format_mode_line,
11240 format_mode_line_unwind_data
11241 (f, current_buffer, selected_window, 0));
11242
11243 Fselect_window (f->selected_window, Qt);
11244 set_buffer_internal_1
11245 (XBUFFER (XWINDOW (f->selected_window)->contents));
11246 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11247
11248 mode_line_target = MODE_LINE_TITLE;
11249 title_start = MODE_LINE_NOPROP_LEN (0);
11250 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11251 NULL, DEFAULT_FACE_ID);
11252 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11253 len = MODE_LINE_NOPROP_LEN (title_start);
11254 title = mode_line_noprop_buf + title_start;
11255 unbind_to (count, Qnil);
11256
11257 /* Set the title only if it's changed. This avoids consing in
11258 the common case where it hasn't. (If it turns out that we've
11259 already wasted too much time by walking through the list with
11260 display_mode_element, then we might need to optimize at a
11261 higher level than this.) */
11262 if (! STRINGP (f->name)
11263 || SBYTES (f->name) != len
11264 || memcmp (title, SDATA (f->name), len) != 0)
11265 x_implicitly_set_name (f, make_string (title, len), Qnil);
11266 }
11267 }
11268
11269 #endif /* not HAVE_WINDOW_SYSTEM */
11270
11271 \f
11272 /***********************************************************************
11273 Menu Bars
11274 ***********************************************************************/
11275
11276
11277 /* Prepare for redisplay by updating menu-bar item lists when
11278 appropriate. This can call eval. */
11279
11280 void
11281 prepare_menu_bars (void)
11282 {
11283 int all_windows;
11284 struct gcpro gcpro1, gcpro2;
11285 struct frame *f;
11286 Lisp_Object tooltip_frame;
11287
11288 #ifdef HAVE_WINDOW_SYSTEM
11289 tooltip_frame = tip_frame;
11290 #else
11291 tooltip_frame = Qnil;
11292 #endif
11293
11294 /* Update all frame titles based on their buffer names, etc. We do
11295 this before the menu bars so that the buffer-menu will show the
11296 up-to-date frame titles. */
11297 #ifdef HAVE_WINDOW_SYSTEM
11298 if (windows_or_buffers_changed || update_mode_lines)
11299 {
11300 Lisp_Object tail, frame;
11301
11302 FOR_EACH_FRAME (tail, frame)
11303 {
11304 f = XFRAME (frame);
11305 if (!EQ (frame, tooltip_frame)
11306 && (FRAME_ICONIFIED_P (f)
11307 || FRAME_VISIBLE_P (f) == 1
11308 /* Exclude TTY frames that are obscured because they
11309 are not the top frame on their console. This is
11310 because x_consider_frame_title actually switches
11311 to the frame, which for TTY frames means it is
11312 marked as garbaged, and will be completely
11313 redrawn on the next redisplay cycle. This causes
11314 TTY frames to be completely redrawn, when there
11315 are more than one of them, even though nothing
11316 should be changed on display. */
11317 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11318 x_consider_frame_title (frame);
11319 }
11320 }
11321 #endif /* HAVE_WINDOW_SYSTEM */
11322
11323 /* Update the menu bar item lists, if appropriate. This has to be
11324 done before any actual redisplay or generation of display lines. */
11325 all_windows = (update_mode_lines
11326 || buffer_shared_and_changed ()
11327 || windows_or_buffers_changed);
11328 if (all_windows)
11329 {
11330 Lisp_Object tail, frame;
11331 ptrdiff_t count = SPECPDL_INDEX ();
11332 /* 1 means that update_menu_bar has run its hooks
11333 so any further calls to update_menu_bar shouldn't do so again. */
11334 int menu_bar_hooks_run = 0;
11335
11336 record_unwind_save_match_data ();
11337
11338 FOR_EACH_FRAME (tail, frame)
11339 {
11340 f = XFRAME (frame);
11341
11342 /* Ignore tooltip frame. */
11343 if (EQ (frame, tooltip_frame))
11344 continue;
11345
11346 /* If a window on this frame changed size, report that to
11347 the user and clear the size-change flag. */
11348 if (FRAME_WINDOW_SIZES_CHANGED (f))
11349 {
11350 Lisp_Object functions;
11351
11352 /* Clear flag first in case we get an error below. */
11353 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11354 functions = Vwindow_size_change_functions;
11355 GCPRO2 (tail, functions);
11356
11357 while (CONSP (functions))
11358 {
11359 if (!EQ (XCAR (functions), Qt))
11360 call1 (XCAR (functions), frame);
11361 functions = XCDR (functions);
11362 }
11363 UNGCPRO;
11364 }
11365
11366 GCPRO1 (tail);
11367 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11368 #ifdef HAVE_WINDOW_SYSTEM
11369 update_tool_bar (f, 0);
11370 #endif
11371 #ifdef HAVE_NS
11372 if (windows_or_buffers_changed
11373 && FRAME_NS_P (f))
11374 ns_set_doc_edited
11375 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11376 #endif
11377 UNGCPRO;
11378 }
11379
11380 unbind_to (count, Qnil);
11381 }
11382 else
11383 {
11384 struct frame *sf = SELECTED_FRAME ();
11385 update_menu_bar (sf, 1, 0);
11386 #ifdef HAVE_WINDOW_SYSTEM
11387 update_tool_bar (sf, 1);
11388 #endif
11389 }
11390 }
11391
11392
11393 /* Update the menu bar item list for frame F. This has to be done
11394 before we start to fill in any display lines, because it can call
11395 eval.
11396
11397 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11398
11399 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11400 already ran the menu bar hooks for this redisplay, so there
11401 is no need to run them again. The return value is the
11402 updated value of this flag, to pass to the next call. */
11403
11404 static int
11405 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11406 {
11407 Lisp_Object window;
11408 register struct window *w;
11409
11410 /* If called recursively during a menu update, do nothing. This can
11411 happen when, for instance, an activate-menubar-hook causes a
11412 redisplay. */
11413 if (inhibit_menubar_update)
11414 return hooks_run;
11415
11416 window = FRAME_SELECTED_WINDOW (f);
11417 w = XWINDOW (window);
11418
11419 if (FRAME_WINDOW_P (f)
11420 ?
11421 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11422 || defined (HAVE_NS) || defined (USE_GTK)
11423 FRAME_EXTERNAL_MENU_BAR (f)
11424 #else
11425 FRAME_MENU_BAR_LINES (f) > 0
11426 #endif
11427 : FRAME_MENU_BAR_LINES (f) > 0)
11428 {
11429 /* If the user has switched buffers or windows, we need to
11430 recompute to reflect the new bindings. But we'll
11431 recompute when update_mode_lines is set too; that means
11432 that people can use force-mode-line-update to request
11433 that the menu bar be recomputed. The adverse effect on
11434 the rest of the redisplay algorithm is about the same as
11435 windows_or_buffers_changed anyway. */
11436 if (windows_or_buffers_changed
11437 /* This used to test w->update_mode_line, but we believe
11438 there is no need to recompute the menu in that case. */
11439 || update_mode_lines
11440 || window_buffer_changed (w))
11441 {
11442 struct buffer *prev = current_buffer;
11443 ptrdiff_t count = SPECPDL_INDEX ();
11444
11445 specbind (Qinhibit_menubar_update, Qt);
11446
11447 set_buffer_internal_1 (XBUFFER (w->contents));
11448 if (save_match_data)
11449 record_unwind_save_match_data ();
11450 if (NILP (Voverriding_local_map_menu_flag))
11451 {
11452 specbind (Qoverriding_terminal_local_map, Qnil);
11453 specbind (Qoverriding_local_map, Qnil);
11454 }
11455
11456 if (!hooks_run)
11457 {
11458 /* Run the Lucid hook. */
11459 safe_run_hooks (Qactivate_menubar_hook);
11460
11461 /* If it has changed current-menubar from previous value,
11462 really recompute the menu-bar from the value. */
11463 if (! NILP (Vlucid_menu_bar_dirty_flag))
11464 call0 (Qrecompute_lucid_menubar);
11465
11466 safe_run_hooks (Qmenu_bar_update_hook);
11467
11468 hooks_run = 1;
11469 }
11470
11471 XSETFRAME (Vmenu_updating_frame, f);
11472 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11473
11474 /* Redisplay the menu bar in case we changed it. */
11475 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11476 || defined (HAVE_NS) || defined (USE_GTK)
11477 if (FRAME_WINDOW_P (f))
11478 {
11479 #if defined (HAVE_NS)
11480 /* All frames on Mac OS share the same menubar. So only
11481 the selected frame should be allowed to set it. */
11482 if (f == SELECTED_FRAME ())
11483 #endif
11484 set_frame_menubar (f, 0, 0);
11485 }
11486 else
11487 /* On a terminal screen, the menu bar is an ordinary screen
11488 line, and this makes it get updated. */
11489 w->update_mode_line = 1;
11490 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11491 /* In the non-toolkit version, the menu bar is an ordinary screen
11492 line, and this makes it get updated. */
11493 w->update_mode_line = 1;
11494 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11495
11496 unbind_to (count, Qnil);
11497 set_buffer_internal_1 (prev);
11498 }
11499 }
11500
11501 return hooks_run;
11502 }
11503
11504
11505 \f
11506 /***********************************************************************
11507 Output Cursor
11508 ***********************************************************************/
11509
11510 #ifdef HAVE_WINDOW_SYSTEM
11511
11512 /* EXPORT:
11513 Nominal cursor position -- where to draw output.
11514 HPOS and VPOS are window relative glyph matrix coordinates.
11515 X and Y are window relative pixel coordinates. */
11516
11517 struct cursor_pos output_cursor;
11518
11519
11520 /* EXPORT:
11521 Set the global variable output_cursor to CURSOR. All cursor
11522 positions are relative to currently updated window. */
11523
11524 void
11525 set_output_cursor (struct cursor_pos *cursor)
11526 {
11527 output_cursor.hpos = cursor->hpos;
11528 output_cursor.vpos = cursor->vpos;
11529 output_cursor.x = cursor->x;
11530 output_cursor.y = cursor->y;
11531 }
11532
11533
11534 /* EXPORT for RIF:
11535 Set a nominal cursor position.
11536
11537 HPOS and VPOS are column/row positions in a window glyph matrix.
11538 X and Y are window text area relative pixel positions.
11539
11540 This is always done during window update, so the position is the
11541 future output cursor position for currently updated window W.
11542 NOTE: W is used only to check whether this function is called
11543 in a consistent manner via the redisplay interface. */
11544
11545 void
11546 x_cursor_to (struct window *w, int vpos, int hpos, int y, int x)
11547 {
11548 eassert (w);
11549
11550 /* Set the output cursor. */
11551 output_cursor.hpos = hpos;
11552 output_cursor.vpos = vpos;
11553 output_cursor.x = x;
11554 output_cursor.y = y;
11555 }
11556
11557 #endif /* HAVE_WINDOW_SYSTEM */
11558
11559 \f
11560 /***********************************************************************
11561 Tool-bars
11562 ***********************************************************************/
11563
11564 #ifdef HAVE_WINDOW_SYSTEM
11565
11566 /* Where the mouse was last time we reported a mouse event. */
11567
11568 struct frame *last_mouse_frame;
11569
11570 /* Tool-bar item index of the item on which a mouse button was pressed
11571 or -1. */
11572
11573 int last_tool_bar_item;
11574
11575 /* Select `frame' temporarily without running all the code in
11576 do_switch_frame.
11577 FIXME: Maybe do_switch_frame should be trimmed down similarly
11578 when `norecord' is set. */
11579 static void
11580 fast_set_selected_frame (Lisp_Object frame)
11581 {
11582 if (!EQ (selected_frame, frame))
11583 {
11584 selected_frame = frame;
11585 selected_window = XFRAME (frame)->selected_window;
11586 }
11587 }
11588
11589 /* Update the tool-bar item list for frame F. This has to be done
11590 before we start to fill in any display lines. Called from
11591 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11592 and restore it here. */
11593
11594 static void
11595 update_tool_bar (struct frame *f, int save_match_data)
11596 {
11597 #if defined (USE_GTK) || defined (HAVE_NS)
11598 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11599 #else
11600 int do_update = WINDOWP (f->tool_bar_window)
11601 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11602 #endif
11603
11604 if (do_update)
11605 {
11606 Lisp_Object window;
11607 struct window *w;
11608
11609 window = FRAME_SELECTED_WINDOW (f);
11610 w = XWINDOW (window);
11611
11612 /* If the user has switched buffers or windows, we need to
11613 recompute to reflect the new bindings. But we'll
11614 recompute when update_mode_lines is set too; that means
11615 that people can use force-mode-line-update to request
11616 that the menu bar be recomputed. The adverse effect on
11617 the rest of the redisplay algorithm is about the same as
11618 windows_or_buffers_changed anyway. */
11619 if (windows_or_buffers_changed
11620 || w->update_mode_line
11621 || update_mode_lines
11622 || window_buffer_changed (w))
11623 {
11624 struct buffer *prev = current_buffer;
11625 ptrdiff_t count = SPECPDL_INDEX ();
11626 Lisp_Object frame, new_tool_bar;
11627 int new_n_tool_bar;
11628 struct gcpro gcpro1;
11629
11630 /* Set current_buffer to the buffer of the selected
11631 window of the frame, so that we get the right local
11632 keymaps. */
11633 set_buffer_internal_1 (XBUFFER (w->contents));
11634
11635 /* Save match data, if we must. */
11636 if (save_match_data)
11637 record_unwind_save_match_data ();
11638
11639 /* Make sure that we don't accidentally use bogus keymaps. */
11640 if (NILP (Voverriding_local_map_menu_flag))
11641 {
11642 specbind (Qoverriding_terminal_local_map, Qnil);
11643 specbind (Qoverriding_local_map, Qnil);
11644 }
11645
11646 GCPRO1 (new_tool_bar);
11647
11648 /* We must temporarily set the selected frame to this frame
11649 before calling tool_bar_items, because the calculation of
11650 the tool-bar keymap uses the selected frame (see
11651 `tool-bar-make-keymap' in tool-bar.el). */
11652 eassert (EQ (selected_window,
11653 /* Since we only explicitly preserve selected_frame,
11654 check that selected_window would be redundant. */
11655 XFRAME (selected_frame)->selected_window));
11656 record_unwind_protect (fast_set_selected_frame, selected_frame);
11657 XSETFRAME (frame, f);
11658 fast_set_selected_frame (frame);
11659
11660 /* Build desired tool-bar items from keymaps. */
11661 new_tool_bar
11662 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11663 &new_n_tool_bar);
11664
11665 /* Redisplay the tool-bar if we changed it. */
11666 if (new_n_tool_bar != f->n_tool_bar_items
11667 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11668 {
11669 /* Redisplay that happens asynchronously due to an expose event
11670 may access f->tool_bar_items. Make sure we update both
11671 variables within BLOCK_INPUT so no such event interrupts. */
11672 block_input ();
11673 fset_tool_bar_items (f, new_tool_bar);
11674 f->n_tool_bar_items = new_n_tool_bar;
11675 w->update_mode_line = 1;
11676 unblock_input ();
11677 }
11678
11679 UNGCPRO;
11680
11681 unbind_to (count, Qnil);
11682 set_buffer_internal_1 (prev);
11683 }
11684 }
11685 }
11686
11687
11688 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11689 F's desired tool-bar contents. F->tool_bar_items must have
11690 been set up previously by calling prepare_menu_bars. */
11691
11692 static void
11693 build_desired_tool_bar_string (struct frame *f)
11694 {
11695 int i, size, size_needed;
11696 struct gcpro gcpro1, gcpro2, gcpro3;
11697 Lisp_Object image, plist, props;
11698
11699 image = plist = props = Qnil;
11700 GCPRO3 (image, plist, props);
11701
11702 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11703 Otherwise, make a new string. */
11704
11705 /* The size of the string we might be able to reuse. */
11706 size = (STRINGP (f->desired_tool_bar_string)
11707 ? SCHARS (f->desired_tool_bar_string)
11708 : 0);
11709
11710 /* We need one space in the string for each image. */
11711 size_needed = f->n_tool_bar_items;
11712
11713 /* Reuse f->desired_tool_bar_string, if possible. */
11714 if (size < size_needed || NILP (f->desired_tool_bar_string))
11715 fset_desired_tool_bar_string
11716 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11717 else
11718 {
11719 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11720 Fremove_text_properties (make_number (0), make_number (size),
11721 props, f->desired_tool_bar_string);
11722 }
11723
11724 /* Put a `display' property on the string for the images to display,
11725 put a `menu_item' property on tool-bar items with a value that
11726 is the index of the item in F's tool-bar item vector. */
11727 for (i = 0; i < f->n_tool_bar_items; ++i)
11728 {
11729 #define PROP(IDX) \
11730 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11731
11732 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11733 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11734 int hmargin, vmargin, relief, idx, end;
11735
11736 /* If image is a vector, choose the image according to the
11737 button state. */
11738 image = PROP (TOOL_BAR_ITEM_IMAGES);
11739 if (VECTORP (image))
11740 {
11741 if (enabled_p)
11742 idx = (selected_p
11743 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11744 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11745 else
11746 idx = (selected_p
11747 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11748 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11749
11750 eassert (ASIZE (image) >= idx);
11751 image = AREF (image, idx);
11752 }
11753 else
11754 idx = -1;
11755
11756 /* Ignore invalid image specifications. */
11757 if (!valid_image_p (image))
11758 continue;
11759
11760 /* Display the tool-bar button pressed, or depressed. */
11761 plist = Fcopy_sequence (XCDR (image));
11762
11763 /* Compute margin and relief to draw. */
11764 relief = (tool_bar_button_relief >= 0
11765 ? tool_bar_button_relief
11766 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11767 hmargin = vmargin = relief;
11768
11769 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11770 INT_MAX - max (hmargin, vmargin)))
11771 {
11772 hmargin += XFASTINT (Vtool_bar_button_margin);
11773 vmargin += XFASTINT (Vtool_bar_button_margin);
11774 }
11775 else if (CONSP (Vtool_bar_button_margin))
11776 {
11777 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11778 INT_MAX - hmargin))
11779 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11780
11781 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11782 INT_MAX - vmargin))
11783 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11784 }
11785
11786 if (auto_raise_tool_bar_buttons_p)
11787 {
11788 /* Add a `:relief' property to the image spec if the item is
11789 selected. */
11790 if (selected_p)
11791 {
11792 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11793 hmargin -= relief;
11794 vmargin -= relief;
11795 }
11796 }
11797 else
11798 {
11799 /* If image is selected, display it pressed, i.e. with a
11800 negative relief. If it's not selected, display it with a
11801 raised relief. */
11802 plist = Fplist_put (plist, QCrelief,
11803 (selected_p
11804 ? make_number (-relief)
11805 : make_number (relief)));
11806 hmargin -= relief;
11807 vmargin -= relief;
11808 }
11809
11810 /* Put a margin around the image. */
11811 if (hmargin || vmargin)
11812 {
11813 if (hmargin == vmargin)
11814 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11815 else
11816 plist = Fplist_put (plist, QCmargin,
11817 Fcons (make_number (hmargin),
11818 make_number (vmargin)));
11819 }
11820
11821 /* If button is not enabled, and we don't have special images
11822 for the disabled state, make the image appear disabled by
11823 applying an appropriate algorithm to it. */
11824 if (!enabled_p && idx < 0)
11825 plist = Fplist_put (plist, QCconversion, Qdisabled);
11826
11827 /* Put a `display' text property on the string for the image to
11828 display. Put a `menu-item' property on the string that gives
11829 the start of this item's properties in the tool-bar items
11830 vector. */
11831 image = Fcons (Qimage, plist);
11832 props = list4 (Qdisplay, image,
11833 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11834
11835 /* Let the last image hide all remaining spaces in the tool bar
11836 string. The string can be longer than needed when we reuse a
11837 previous string. */
11838 if (i + 1 == f->n_tool_bar_items)
11839 end = SCHARS (f->desired_tool_bar_string);
11840 else
11841 end = i + 1;
11842 Fadd_text_properties (make_number (i), make_number (end),
11843 props, f->desired_tool_bar_string);
11844 #undef PROP
11845 }
11846
11847 UNGCPRO;
11848 }
11849
11850
11851 /* Display one line of the tool-bar of frame IT->f.
11852
11853 HEIGHT specifies the desired height of the tool-bar line.
11854 If the actual height of the glyph row is less than HEIGHT, the
11855 row's height is increased to HEIGHT, and the icons are centered
11856 vertically in the new height.
11857
11858 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11859 count a final empty row in case the tool-bar width exactly matches
11860 the window width.
11861 */
11862
11863 static void
11864 display_tool_bar_line (struct it *it, int height)
11865 {
11866 struct glyph_row *row = it->glyph_row;
11867 int max_x = it->last_visible_x;
11868 struct glyph *last;
11869
11870 prepare_desired_row (row);
11871 row->y = it->current_y;
11872
11873 /* Note that this isn't made use of if the face hasn't a box,
11874 so there's no need to check the face here. */
11875 it->start_of_box_run_p = 1;
11876
11877 while (it->current_x < max_x)
11878 {
11879 int x, n_glyphs_before, i, nglyphs;
11880 struct it it_before;
11881
11882 /* Get the next display element. */
11883 if (!get_next_display_element (it))
11884 {
11885 /* Don't count empty row if we are counting needed tool-bar lines. */
11886 if (height < 0 && !it->hpos)
11887 return;
11888 break;
11889 }
11890
11891 /* Produce glyphs. */
11892 n_glyphs_before = row->used[TEXT_AREA];
11893 it_before = *it;
11894
11895 PRODUCE_GLYPHS (it);
11896
11897 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11898 i = 0;
11899 x = it_before.current_x;
11900 while (i < nglyphs)
11901 {
11902 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11903
11904 if (x + glyph->pixel_width > max_x)
11905 {
11906 /* Glyph doesn't fit on line. Backtrack. */
11907 row->used[TEXT_AREA] = n_glyphs_before;
11908 *it = it_before;
11909 /* If this is the only glyph on this line, it will never fit on the
11910 tool-bar, so skip it. But ensure there is at least one glyph,
11911 so we don't accidentally disable the tool-bar. */
11912 if (n_glyphs_before == 0
11913 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11914 break;
11915 goto out;
11916 }
11917
11918 ++it->hpos;
11919 x += glyph->pixel_width;
11920 ++i;
11921 }
11922
11923 /* Stop at line end. */
11924 if (ITERATOR_AT_END_OF_LINE_P (it))
11925 break;
11926
11927 set_iterator_to_next (it, 1);
11928 }
11929
11930 out:;
11931
11932 row->displays_text_p = row->used[TEXT_AREA] != 0;
11933
11934 /* Use default face for the border below the tool bar.
11935
11936 FIXME: When auto-resize-tool-bars is grow-only, there is
11937 no additional border below the possibly empty tool-bar lines.
11938 So to make the extra empty lines look "normal", we have to
11939 use the tool-bar face for the border too. */
11940 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11941 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11942 it->face_id = DEFAULT_FACE_ID;
11943
11944 extend_face_to_end_of_line (it);
11945 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11946 last->right_box_line_p = 1;
11947 if (last == row->glyphs[TEXT_AREA])
11948 last->left_box_line_p = 1;
11949
11950 /* Make line the desired height and center it vertically. */
11951 if ((height -= it->max_ascent + it->max_descent) > 0)
11952 {
11953 /* Don't add more than one line height. */
11954 height %= FRAME_LINE_HEIGHT (it->f);
11955 it->max_ascent += height / 2;
11956 it->max_descent += (height + 1) / 2;
11957 }
11958
11959 compute_line_metrics (it);
11960
11961 /* If line is empty, make it occupy the rest of the tool-bar. */
11962 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11963 {
11964 row->height = row->phys_height = it->last_visible_y - row->y;
11965 row->visible_height = row->height;
11966 row->ascent = row->phys_ascent = 0;
11967 row->extra_line_spacing = 0;
11968 }
11969
11970 row->full_width_p = 1;
11971 row->continued_p = 0;
11972 row->truncated_on_left_p = 0;
11973 row->truncated_on_right_p = 0;
11974
11975 it->current_x = it->hpos = 0;
11976 it->current_y += row->height;
11977 ++it->vpos;
11978 ++it->glyph_row;
11979 }
11980
11981
11982 /* Max tool-bar height. */
11983
11984 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11985 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11986
11987 /* Value is the number of screen lines needed to make all tool-bar
11988 items of frame F visible. The number of actual rows needed is
11989 returned in *N_ROWS if non-NULL. */
11990
11991 static int
11992 tool_bar_lines_needed (struct frame *f, int *n_rows)
11993 {
11994 struct window *w = XWINDOW (f->tool_bar_window);
11995 struct it it;
11996 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11997 the desired matrix, so use (unused) mode-line row as temporary row to
11998 avoid destroying the first tool-bar row. */
11999 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12000
12001 /* Initialize an iterator for iteration over
12002 F->desired_tool_bar_string in the tool-bar window of frame F. */
12003 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12004 it.first_visible_x = 0;
12005 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12006 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12007 it.paragraph_embedding = L2R;
12008
12009 while (!ITERATOR_AT_END_P (&it))
12010 {
12011 clear_glyph_row (temp_row);
12012 it.glyph_row = temp_row;
12013 display_tool_bar_line (&it, -1);
12014 }
12015 clear_glyph_row (temp_row);
12016
12017 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12018 if (n_rows)
12019 *n_rows = it.vpos > 0 ? it.vpos : -1;
12020
12021 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12022 }
12023
12024
12025 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
12026 0, 1, 0,
12027 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12028 If FRAME is nil or omitted, use the selected frame. */)
12029 (Lisp_Object frame)
12030 {
12031 struct frame *f = decode_any_frame (frame);
12032 struct window *w;
12033 int nlines = 0;
12034
12035 if (WINDOWP (f->tool_bar_window)
12036 && (w = XWINDOW (f->tool_bar_window),
12037 WINDOW_TOTAL_LINES (w) > 0))
12038 {
12039 update_tool_bar (f, 1);
12040 if (f->n_tool_bar_items)
12041 {
12042 build_desired_tool_bar_string (f);
12043 nlines = tool_bar_lines_needed (f, NULL);
12044 }
12045 }
12046
12047 return make_number (nlines);
12048 }
12049
12050
12051 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12052 height should be changed. */
12053
12054 static int
12055 redisplay_tool_bar (struct frame *f)
12056 {
12057 struct window *w;
12058 struct it it;
12059 struct glyph_row *row;
12060
12061 #if defined (USE_GTK) || defined (HAVE_NS)
12062 if (FRAME_EXTERNAL_TOOL_BAR (f))
12063 update_frame_tool_bar (f);
12064 return 0;
12065 #endif
12066
12067 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12068 do anything. This means you must start with tool-bar-lines
12069 non-zero to get the auto-sizing effect. Or in other words, you
12070 can turn off tool-bars by specifying tool-bar-lines zero. */
12071 if (!WINDOWP (f->tool_bar_window)
12072 || (w = XWINDOW (f->tool_bar_window),
12073 WINDOW_TOTAL_LINES (w) == 0))
12074 return 0;
12075
12076 /* Set up an iterator for the tool-bar window. */
12077 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12078 it.first_visible_x = 0;
12079 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12080 row = it.glyph_row;
12081
12082 /* Build a string that represents the contents of the tool-bar. */
12083 build_desired_tool_bar_string (f);
12084 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12085 /* FIXME: This should be controlled by a user option. But it
12086 doesn't make sense to have an R2L tool bar if the menu bar cannot
12087 be drawn also R2L, and making the menu bar R2L is tricky due
12088 toolkit-specific code that implements it. If an R2L tool bar is
12089 ever supported, display_tool_bar_line should also be augmented to
12090 call unproduce_glyphs like display_line and display_string
12091 do. */
12092 it.paragraph_embedding = L2R;
12093
12094 if (f->n_tool_bar_rows == 0)
12095 {
12096 int nlines;
12097
12098 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12099 nlines != WINDOW_TOTAL_LINES (w)))
12100 {
12101 Lisp_Object frame;
12102 int old_height = WINDOW_TOTAL_LINES (w);
12103
12104 XSETFRAME (frame, f);
12105 Fmodify_frame_parameters (frame,
12106 list1 (Fcons (Qtool_bar_lines,
12107 make_number (nlines))));
12108 if (WINDOW_TOTAL_LINES (w) != old_height)
12109 {
12110 clear_glyph_matrix (w->desired_matrix);
12111 fonts_changed_p = 1;
12112 return 1;
12113 }
12114 }
12115 }
12116
12117 /* Display as many lines as needed to display all tool-bar items. */
12118
12119 if (f->n_tool_bar_rows > 0)
12120 {
12121 int border, rows, height, extra;
12122
12123 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12124 border = XINT (Vtool_bar_border);
12125 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12126 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12127 else if (EQ (Vtool_bar_border, Qborder_width))
12128 border = f->border_width;
12129 else
12130 border = 0;
12131 if (border < 0)
12132 border = 0;
12133
12134 rows = f->n_tool_bar_rows;
12135 height = max (1, (it.last_visible_y - border) / rows);
12136 extra = it.last_visible_y - border - height * rows;
12137
12138 while (it.current_y < it.last_visible_y)
12139 {
12140 int h = 0;
12141 if (extra > 0 && rows-- > 0)
12142 {
12143 h = (extra + rows - 1) / rows;
12144 extra -= h;
12145 }
12146 display_tool_bar_line (&it, height + h);
12147 }
12148 }
12149 else
12150 {
12151 while (it.current_y < it.last_visible_y)
12152 display_tool_bar_line (&it, 0);
12153 }
12154
12155 /* It doesn't make much sense to try scrolling in the tool-bar
12156 window, so don't do it. */
12157 w->desired_matrix->no_scrolling_p = 1;
12158 w->must_be_updated_p = 1;
12159
12160 if (!NILP (Vauto_resize_tool_bars))
12161 {
12162 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12163 int change_height_p = 0;
12164
12165 /* If we couldn't display everything, change the tool-bar's
12166 height if there is room for more. */
12167 if (IT_STRING_CHARPOS (it) < it.end_charpos
12168 && it.current_y < max_tool_bar_height)
12169 change_height_p = 1;
12170
12171 row = it.glyph_row - 1;
12172
12173 /* If there are blank lines at the end, except for a partially
12174 visible blank line at the end that is smaller than
12175 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12176 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12177 && row->height >= FRAME_LINE_HEIGHT (f))
12178 change_height_p = 1;
12179
12180 /* If row displays tool-bar items, but is partially visible,
12181 change the tool-bar's height. */
12182 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12183 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12184 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12185 change_height_p = 1;
12186
12187 /* Resize windows as needed by changing the `tool-bar-lines'
12188 frame parameter. */
12189 if (change_height_p)
12190 {
12191 Lisp_Object frame;
12192 int old_height = WINDOW_TOTAL_LINES (w);
12193 int nrows;
12194 int nlines = tool_bar_lines_needed (f, &nrows);
12195
12196 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12197 && !f->minimize_tool_bar_window_p)
12198 ? (nlines > old_height)
12199 : (nlines != old_height));
12200 f->minimize_tool_bar_window_p = 0;
12201
12202 if (change_height_p)
12203 {
12204 XSETFRAME (frame, f);
12205 Fmodify_frame_parameters (frame,
12206 list1 (Fcons (Qtool_bar_lines,
12207 make_number (nlines))));
12208 if (WINDOW_TOTAL_LINES (w) != old_height)
12209 {
12210 clear_glyph_matrix (w->desired_matrix);
12211 f->n_tool_bar_rows = nrows;
12212 fonts_changed_p = 1;
12213 return 1;
12214 }
12215 }
12216 }
12217 }
12218
12219 f->minimize_tool_bar_window_p = 0;
12220 return 0;
12221 }
12222
12223
12224 /* Get information about the tool-bar item which is displayed in GLYPH
12225 on frame F. Return in *PROP_IDX the index where tool-bar item
12226 properties start in F->tool_bar_items. Value is zero if
12227 GLYPH doesn't display a tool-bar item. */
12228
12229 static int
12230 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12231 {
12232 Lisp_Object prop;
12233 int success_p;
12234 int charpos;
12235
12236 /* This function can be called asynchronously, which means we must
12237 exclude any possibility that Fget_text_property signals an
12238 error. */
12239 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12240 charpos = max (0, charpos);
12241
12242 /* Get the text property `menu-item' at pos. The value of that
12243 property is the start index of this item's properties in
12244 F->tool_bar_items. */
12245 prop = Fget_text_property (make_number (charpos),
12246 Qmenu_item, f->current_tool_bar_string);
12247 if (INTEGERP (prop))
12248 {
12249 *prop_idx = XINT (prop);
12250 success_p = 1;
12251 }
12252 else
12253 success_p = 0;
12254
12255 return success_p;
12256 }
12257
12258 \f
12259 /* Get information about the tool-bar item at position X/Y on frame F.
12260 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12261 the current matrix of the tool-bar window of F, or NULL if not
12262 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12263 item in F->tool_bar_items. Value is
12264
12265 -1 if X/Y is not on a tool-bar item
12266 0 if X/Y is on the same item that was highlighted before.
12267 1 otherwise. */
12268
12269 static int
12270 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12271 int *hpos, int *vpos, int *prop_idx)
12272 {
12273 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12274 struct window *w = XWINDOW (f->tool_bar_window);
12275 int area;
12276
12277 /* Find the glyph under X/Y. */
12278 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12279 if (*glyph == NULL)
12280 return -1;
12281
12282 /* Get the start of this tool-bar item's properties in
12283 f->tool_bar_items. */
12284 if (!tool_bar_item_info (f, *glyph, prop_idx))
12285 return -1;
12286
12287 /* Is mouse on the highlighted item? */
12288 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12289 && *vpos >= hlinfo->mouse_face_beg_row
12290 && *vpos <= hlinfo->mouse_face_end_row
12291 && (*vpos > hlinfo->mouse_face_beg_row
12292 || *hpos >= hlinfo->mouse_face_beg_col)
12293 && (*vpos < hlinfo->mouse_face_end_row
12294 || *hpos < hlinfo->mouse_face_end_col
12295 || hlinfo->mouse_face_past_end))
12296 return 0;
12297
12298 return 1;
12299 }
12300
12301
12302 /* EXPORT:
12303 Handle mouse button event on the tool-bar of frame F, at
12304 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12305 0 for button release. MODIFIERS is event modifiers for button
12306 release. */
12307
12308 void
12309 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12310 int modifiers)
12311 {
12312 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12313 struct window *w = XWINDOW (f->tool_bar_window);
12314 int hpos, vpos, prop_idx;
12315 struct glyph *glyph;
12316 Lisp_Object enabled_p;
12317 int ts;
12318
12319 /* If not on the highlighted tool-bar item, and mouse-highlight is
12320 non-nil, return. This is so we generate the tool-bar button
12321 click only when the mouse button is released on the same item as
12322 where it was pressed. However, when mouse-highlight is disabled,
12323 generate the click when the button is released regardless of the
12324 highlight, since tool-bar items are not highlighted in that
12325 case. */
12326 frame_to_window_pixel_xy (w, &x, &y);
12327 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12328 if (ts == -1
12329 || (ts != 0 && !NILP (Vmouse_highlight)))
12330 return;
12331
12332 /* When mouse-highlight is off, generate the click for the item
12333 where the button was pressed, disregarding where it was
12334 released. */
12335 if (NILP (Vmouse_highlight) && !down_p)
12336 prop_idx = last_tool_bar_item;
12337
12338 /* If item is disabled, do nothing. */
12339 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12340 if (NILP (enabled_p))
12341 return;
12342
12343 if (down_p)
12344 {
12345 /* Show item in pressed state. */
12346 if (!NILP (Vmouse_highlight))
12347 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12348 last_tool_bar_item = prop_idx;
12349 }
12350 else
12351 {
12352 Lisp_Object key, frame;
12353 struct input_event event;
12354 EVENT_INIT (event);
12355
12356 /* Show item in released state. */
12357 if (!NILP (Vmouse_highlight))
12358 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12359
12360 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12361
12362 XSETFRAME (frame, f);
12363 event.kind = TOOL_BAR_EVENT;
12364 event.frame_or_window = frame;
12365 event.arg = frame;
12366 kbd_buffer_store_event (&event);
12367
12368 event.kind = TOOL_BAR_EVENT;
12369 event.frame_or_window = frame;
12370 event.arg = key;
12371 event.modifiers = modifiers;
12372 kbd_buffer_store_event (&event);
12373 last_tool_bar_item = -1;
12374 }
12375 }
12376
12377
12378 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12379 tool-bar window-relative coordinates X/Y. Called from
12380 note_mouse_highlight. */
12381
12382 static void
12383 note_tool_bar_highlight (struct frame *f, int x, int y)
12384 {
12385 Lisp_Object window = f->tool_bar_window;
12386 struct window *w = XWINDOW (window);
12387 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12388 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12389 int hpos, vpos;
12390 struct glyph *glyph;
12391 struct glyph_row *row;
12392 int i;
12393 Lisp_Object enabled_p;
12394 int prop_idx;
12395 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12396 int mouse_down_p, rc;
12397
12398 /* Function note_mouse_highlight is called with negative X/Y
12399 values when mouse moves outside of the frame. */
12400 if (x <= 0 || y <= 0)
12401 {
12402 clear_mouse_face (hlinfo);
12403 return;
12404 }
12405
12406 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12407 if (rc < 0)
12408 {
12409 /* Not on tool-bar item. */
12410 clear_mouse_face (hlinfo);
12411 return;
12412 }
12413 else if (rc == 0)
12414 /* On same tool-bar item as before. */
12415 goto set_help_echo;
12416
12417 clear_mouse_face (hlinfo);
12418
12419 /* Mouse is down, but on different tool-bar item? */
12420 mouse_down_p = (dpyinfo->grabbed
12421 && f == last_mouse_frame
12422 && FRAME_LIVE_P (f));
12423 if (mouse_down_p
12424 && last_tool_bar_item != prop_idx)
12425 return;
12426
12427 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12428
12429 /* If tool-bar item is not enabled, don't highlight it. */
12430 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12431 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12432 {
12433 /* Compute the x-position of the glyph. In front and past the
12434 image is a space. We include this in the highlighted area. */
12435 row = MATRIX_ROW (w->current_matrix, vpos);
12436 for (i = x = 0; i < hpos; ++i)
12437 x += row->glyphs[TEXT_AREA][i].pixel_width;
12438
12439 /* Record this as the current active region. */
12440 hlinfo->mouse_face_beg_col = hpos;
12441 hlinfo->mouse_face_beg_row = vpos;
12442 hlinfo->mouse_face_beg_x = x;
12443 hlinfo->mouse_face_past_end = 0;
12444
12445 hlinfo->mouse_face_end_col = hpos + 1;
12446 hlinfo->mouse_face_end_row = vpos;
12447 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12448 hlinfo->mouse_face_window = window;
12449 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12450
12451 /* Display it as active. */
12452 show_mouse_face (hlinfo, draw);
12453 }
12454
12455 set_help_echo:
12456
12457 /* Set help_echo_string to a help string to display for this tool-bar item.
12458 XTread_socket does the rest. */
12459 help_echo_object = help_echo_window = Qnil;
12460 help_echo_pos = -1;
12461 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12462 if (NILP (help_echo_string))
12463 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12464 }
12465
12466 #endif /* HAVE_WINDOW_SYSTEM */
12467
12468
12469 \f
12470 /************************************************************************
12471 Horizontal scrolling
12472 ************************************************************************/
12473
12474 static int hscroll_window_tree (Lisp_Object);
12475 static int hscroll_windows (Lisp_Object);
12476
12477 /* For all leaf windows in the window tree rooted at WINDOW, set their
12478 hscroll value so that PT is (i) visible in the window, and (ii) so
12479 that it is not within a certain margin at the window's left and
12480 right border. Value is non-zero if any window's hscroll has been
12481 changed. */
12482
12483 static int
12484 hscroll_window_tree (Lisp_Object window)
12485 {
12486 int hscrolled_p = 0;
12487 int hscroll_relative_p = FLOATP (Vhscroll_step);
12488 int hscroll_step_abs = 0;
12489 double hscroll_step_rel = 0;
12490
12491 if (hscroll_relative_p)
12492 {
12493 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12494 if (hscroll_step_rel < 0)
12495 {
12496 hscroll_relative_p = 0;
12497 hscroll_step_abs = 0;
12498 }
12499 }
12500 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12501 {
12502 hscroll_step_abs = XINT (Vhscroll_step);
12503 if (hscroll_step_abs < 0)
12504 hscroll_step_abs = 0;
12505 }
12506 else
12507 hscroll_step_abs = 0;
12508
12509 while (WINDOWP (window))
12510 {
12511 struct window *w = XWINDOW (window);
12512
12513 if (WINDOWP (w->contents))
12514 hscrolled_p |= hscroll_window_tree (w->contents);
12515 else if (w->cursor.vpos >= 0)
12516 {
12517 int h_margin;
12518 int text_area_width;
12519 struct glyph_row *current_cursor_row
12520 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12521 struct glyph_row *desired_cursor_row
12522 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12523 struct glyph_row *cursor_row
12524 = (desired_cursor_row->enabled_p
12525 ? desired_cursor_row
12526 : current_cursor_row);
12527 int row_r2l_p = cursor_row->reversed_p;
12528
12529 text_area_width = window_box_width (w, TEXT_AREA);
12530
12531 /* Scroll when cursor is inside this scroll margin. */
12532 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12533
12534 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12535 /* For left-to-right rows, hscroll when cursor is either
12536 (i) inside the right hscroll margin, or (ii) if it is
12537 inside the left margin and the window is already
12538 hscrolled. */
12539 && ((!row_r2l_p
12540 && ((w->hscroll
12541 && w->cursor.x <= h_margin)
12542 || (cursor_row->enabled_p
12543 && cursor_row->truncated_on_right_p
12544 && (w->cursor.x >= text_area_width - h_margin))))
12545 /* For right-to-left rows, the logic is similar,
12546 except that rules for scrolling to left and right
12547 are reversed. E.g., if cursor.x <= h_margin, we
12548 need to hscroll "to the right" unconditionally,
12549 and that will scroll the screen to the left so as
12550 to reveal the next portion of the row. */
12551 || (row_r2l_p
12552 && ((cursor_row->enabled_p
12553 /* FIXME: It is confusing to set the
12554 truncated_on_right_p flag when R2L rows
12555 are actually truncated on the left. */
12556 && cursor_row->truncated_on_right_p
12557 && w->cursor.x <= h_margin)
12558 || (w->hscroll
12559 && (w->cursor.x >= text_area_width - h_margin))))))
12560 {
12561 struct it it;
12562 ptrdiff_t hscroll;
12563 struct buffer *saved_current_buffer;
12564 ptrdiff_t pt;
12565 int wanted_x;
12566
12567 /* Find point in a display of infinite width. */
12568 saved_current_buffer = current_buffer;
12569 current_buffer = XBUFFER (w->contents);
12570
12571 if (w == XWINDOW (selected_window))
12572 pt = PT;
12573 else
12574 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12575
12576 /* Move iterator to pt starting at cursor_row->start in
12577 a line with infinite width. */
12578 init_to_row_start (&it, w, cursor_row);
12579 it.last_visible_x = INFINITY;
12580 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12581 current_buffer = saved_current_buffer;
12582
12583 /* Position cursor in window. */
12584 if (!hscroll_relative_p && hscroll_step_abs == 0)
12585 hscroll = max (0, (it.current_x
12586 - (ITERATOR_AT_END_OF_LINE_P (&it)
12587 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12588 : (text_area_width / 2))))
12589 / FRAME_COLUMN_WIDTH (it.f);
12590 else if ((!row_r2l_p
12591 && w->cursor.x >= text_area_width - h_margin)
12592 || (row_r2l_p && w->cursor.x <= h_margin))
12593 {
12594 if (hscroll_relative_p)
12595 wanted_x = text_area_width * (1 - hscroll_step_rel)
12596 - h_margin;
12597 else
12598 wanted_x = text_area_width
12599 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12600 - h_margin;
12601 hscroll
12602 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12603 }
12604 else
12605 {
12606 if (hscroll_relative_p)
12607 wanted_x = text_area_width * hscroll_step_rel
12608 + h_margin;
12609 else
12610 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12611 + h_margin;
12612 hscroll
12613 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12614 }
12615 hscroll = max (hscroll, w->min_hscroll);
12616
12617 /* Don't prevent redisplay optimizations if hscroll
12618 hasn't changed, as it will unnecessarily slow down
12619 redisplay. */
12620 if (w->hscroll != hscroll)
12621 {
12622 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12623 w->hscroll = hscroll;
12624 hscrolled_p = 1;
12625 }
12626 }
12627 }
12628
12629 window = w->next;
12630 }
12631
12632 /* Value is non-zero if hscroll of any leaf window has been changed. */
12633 return hscrolled_p;
12634 }
12635
12636
12637 /* Set hscroll so that cursor is visible and not inside horizontal
12638 scroll margins for all windows in the tree rooted at WINDOW. See
12639 also hscroll_window_tree above. Value is non-zero if any window's
12640 hscroll has been changed. If it has, desired matrices on the frame
12641 of WINDOW are cleared. */
12642
12643 static int
12644 hscroll_windows (Lisp_Object window)
12645 {
12646 int hscrolled_p = hscroll_window_tree (window);
12647 if (hscrolled_p)
12648 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12649 return hscrolled_p;
12650 }
12651
12652
12653 \f
12654 /************************************************************************
12655 Redisplay
12656 ************************************************************************/
12657
12658 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12659 to a non-zero value. This is sometimes handy to have in a debugger
12660 session. */
12661
12662 #ifdef GLYPH_DEBUG
12663
12664 /* First and last unchanged row for try_window_id. */
12665
12666 static int debug_first_unchanged_at_end_vpos;
12667 static int debug_last_unchanged_at_beg_vpos;
12668
12669 /* Delta vpos and y. */
12670
12671 static int debug_dvpos, debug_dy;
12672
12673 /* Delta in characters and bytes for try_window_id. */
12674
12675 static ptrdiff_t debug_delta, debug_delta_bytes;
12676
12677 /* Values of window_end_pos and window_end_vpos at the end of
12678 try_window_id. */
12679
12680 static ptrdiff_t debug_end_vpos;
12681
12682 /* Append a string to W->desired_matrix->method. FMT is a printf
12683 format string. If trace_redisplay_p is non-zero also printf the
12684 resulting string to stderr. */
12685
12686 static void debug_method_add (struct window *, char const *, ...)
12687 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12688
12689 static void
12690 debug_method_add (struct window *w, char const *fmt, ...)
12691 {
12692 void *ptr = w;
12693 char *method = w->desired_matrix->method;
12694 int len = strlen (method);
12695 int size = sizeof w->desired_matrix->method;
12696 int remaining = size - len - 1;
12697 va_list ap;
12698
12699 if (len && remaining)
12700 {
12701 method[len] = '|';
12702 --remaining, ++len;
12703 }
12704
12705 va_start (ap, fmt);
12706 vsnprintf (method + len, remaining + 1, fmt, ap);
12707 va_end (ap);
12708
12709 if (trace_redisplay_p)
12710 fprintf (stderr, "%p (%s): %s\n",
12711 ptr,
12712 ((BUFFERP (w->contents)
12713 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12714 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12715 : "no buffer"),
12716 method + len);
12717 }
12718
12719 #endif /* GLYPH_DEBUG */
12720
12721
12722 /* Value is non-zero if all changes in window W, which displays
12723 current_buffer, are in the text between START and END. START is a
12724 buffer position, END is given as a distance from Z. Used in
12725 redisplay_internal for display optimization. */
12726
12727 static int
12728 text_outside_line_unchanged_p (struct window *w,
12729 ptrdiff_t start, ptrdiff_t end)
12730 {
12731 int unchanged_p = 1;
12732
12733 /* If text or overlays have changed, see where. */
12734 if (window_outdated (w))
12735 {
12736 /* Gap in the line? */
12737 if (GPT < start || Z - GPT < end)
12738 unchanged_p = 0;
12739
12740 /* Changes start in front of the line, or end after it? */
12741 if (unchanged_p
12742 && (BEG_UNCHANGED < start - 1
12743 || END_UNCHANGED < end))
12744 unchanged_p = 0;
12745
12746 /* If selective display, can't optimize if changes start at the
12747 beginning of the line. */
12748 if (unchanged_p
12749 && INTEGERP (BVAR (current_buffer, selective_display))
12750 && XINT (BVAR (current_buffer, selective_display)) > 0
12751 && (BEG_UNCHANGED < start || GPT <= start))
12752 unchanged_p = 0;
12753
12754 /* If there are overlays at the start or end of the line, these
12755 may have overlay strings with newlines in them. A change at
12756 START, for instance, may actually concern the display of such
12757 overlay strings as well, and they are displayed on different
12758 lines. So, quickly rule out this case. (For the future, it
12759 might be desirable to implement something more telling than
12760 just BEG/END_UNCHANGED.) */
12761 if (unchanged_p)
12762 {
12763 if (BEG + BEG_UNCHANGED == start
12764 && overlay_touches_p (start))
12765 unchanged_p = 0;
12766 if (END_UNCHANGED == end
12767 && overlay_touches_p (Z - end))
12768 unchanged_p = 0;
12769 }
12770
12771 /* Under bidi reordering, adding or deleting a character in the
12772 beginning of a paragraph, before the first strong directional
12773 character, can change the base direction of the paragraph (unless
12774 the buffer specifies a fixed paragraph direction), which will
12775 require to redisplay the whole paragraph. It might be worthwhile
12776 to find the paragraph limits and widen the range of redisplayed
12777 lines to that, but for now just give up this optimization. */
12778 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12779 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12780 unchanged_p = 0;
12781 }
12782
12783 return unchanged_p;
12784 }
12785
12786
12787 /* Do a frame update, taking possible shortcuts into account. This is
12788 the main external entry point for redisplay.
12789
12790 If the last redisplay displayed an echo area message and that message
12791 is no longer requested, we clear the echo area or bring back the
12792 mini-buffer if that is in use. */
12793
12794 void
12795 redisplay (void)
12796 {
12797 redisplay_internal ();
12798 }
12799
12800
12801 static Lisp_Object
12802 overlay_arrow_string_or_property (Lisp_Object var)
12803 {
12804 Lisp_Object val;
12805
12806 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12807 return val;
12808
12809 return Voverlay_arrow_string;
12810 }
12811
12812 /* Return 1 if there are any overlay-arrows in current_buffer. */
12813 static int
12814 overlay_arrow_in_current_buffer_p (void)
12815 {
12816 Lisp_Object vlist;
12817
12818 for (vlist = Voverlay_arrow_variable_list;
12819 CONSP (vlist);
12820 vlist = XCDR (vlist))
12821 {
12822 Lisp_Object var = XCAR (vlist);
12823 Lisp_Object val;
12824
12825 if (!SYMBOLP (var))
12826 continue;
12827 val = find_symbol_value (var);
12828 if (MARKERP (val)
12829 && current_buffer == XMARKER (val)->buffer)
12830 return 1;
12831 }
12832 return 0;
12833 }
12834
12835
12836 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12837 has changed. */
12838
12839 static int
12840 overlay_arrows_changed_p (void)
12841 {
12842 Lisp_Object vlist;
12843
12844 for (vlist = Voverlay_arrow_variable_list;
12845 CONSP (vlist);
12846 vlist = XCDR (vlist))
12847 {
12848 Lisp_Object var = XCAR (vlist);
12849 Lisp_Object val, pstr;
12850
12851 if (!SYMBOLP (var))
12852 continue;
12853 val = find_symbol_value (var);
12854 if (!MARKERP (val))
12855 continue;
12856 if (! EQ (COERCE_MARKER (val),
12857 Fget (var, Qlast_arrow_position))
12858 || ! (pstr = overlay_arrow_string_or_property (var),
12859 EQ (pstr, Fget (var, Qlast_arrow_string))))
12860 return 1;
12861 }
12862 return 0;
12863 }
12864
12865 /* Mark overlay arrows to be updated on next redisplay. */
12866
12867 static void
12868 update_overlay_arrows (int up_to_date)
12869 {
12870 Lisp_Object vlist;
12871
12872 for (vlist = Voverlay_arrow_variable_list;
12873 CONSP (vlist);
12874 vlist = XCDR (vlist))
12875 {
12876 Lisp_Object var = XCAR (vlist);
12877
12878 if (!SYMBOLP (var))
12879 continue;
12880
12881 if (up_to_date > 0)
12882 {
12883 Lisp_Object val = find_symbol_value (var);
12884 Fput (var, Qlast_arrow_position,
12885 COERCE_MARKER (val));
12886 Fput (var, Qlast_arrow_string,
12887 overlay_arrow_string_or_property (var));
12888 }
12889 else if (up_to_date < 0
12890 || !NILP (Fget (var, Qlast_arrow_position)))
12891 {
12892 Fput (var, Qlast_arrow_position, Qt);
12893 Fput (var, Qlast_arrow_string, Qt);
12894 }
12895 }
12896 }
12897
12898
12899 /* Return overlay arrow string to display at row.
12900 Return integer (bitmap number) for arrow bitmap in left fringe.
12901 Return nil if no overlay arrow. */
12902
12903 static Lisp_Object
12904 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12905 {
12906 Lisp_Object vlist;
12907
12908 for (vlist = Voverlay_arrow_variable_list;
12909 CONSP (vlist);
12910 vlist = XCDR (vlist))
12911 {
12912 Lisp_Object var = XCAR (vlist);
12913 Lisp_Object val;
12914
12915 if (!SYMBOLP (var))
12916 continue;
12917
12918 val = find_symbol_value (var);
12919
12920 if (MARKERP (val)
12921 && current_buffer == XMARKER (val)->buffer
12922 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12923 {
12924 if (FRAME_WINDOW_P (it->f)
12925 /* FIXME: if ROW->reversed_p is set, this should test
12926 the right fringe, not the left one. */
12927 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12928 {
12929 #ifdef HAVE_WINDOW_SYSTEM
12930 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12931 {
12932 int fringe_bitmap;
12933 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12934 return make_number (fringe_bitmap);
12935 }
12936 #endif
12937 return make_number (-1); /* Use default arrow bitmap. */
12938 }
12939 return overlay_arrow_string_or_property (var);
12940 }
12941 }
12942
12943 return Qnil;
12944 }
12945
12946 /* Return 1 if point moved out of or into a composition. Otherwise
12947 return 0. PREV_BUF and PREV_PT are the last point buffer and
12948 position. BUF and PT are the current point buffer and position. */
12949
12950 static int
12951 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12952 struct buffer *buf, ptrdiff_t pt)
12953 {
12954 ptrdiff_t start, end;
12955 Lisp_Object prop;
12956 Lisp_Object buffer;
12957
12958 XSETBUFFER (buffer, buf);
12959 /* Check a composition at the last point if point moved within the
12960 same buffer. */
12961 if (prev_buf == buf)
12962 {
12963 if (prev_pt == pt)
12964 /* Point didn't move. */
12965 return 0;
12966
12967 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12968 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12969 && composition_valid_p (start, end, prop)
12970 && start < prev_pt && end > prev_pt)
12971 /* The last point was within the composition. Return 1 iff
12972 point moved out of the composition. */
12973 return (pt <= start || pt >= end);
12974 }
12975
12976 /* Check a composition at the current point. */
12977 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12978 && find_composition (pt, -1, &start, &end, &prop, buffer)
12979 && composition_valid_p (start, end, prop)
12980 && start < pt && end > pt);
12981 }
12982
12983 /* Reconsider the clip changes of buffer which is displayed in W. */
12984
12985 static void
12986 reconsider_clip_changes (struct window *w)
12987 {
12988 struct buffer *b = XBUFFER (w->contents);
12989
12990 if (b->clip_changed
12991 && w->window_end_valid
12992 && w->current_matrix->buffer == b
12993 && w->current_matrix->zv == BUF_ZV (b)
12994 && w->current_matrix->begv == BUF_BEGV (b))
12995 b->clip_changed = 0;
12996
12997 /* If display wasn't paused, and W is not a tool bar window, see if
12998 point has been moved into or out of a composition. In that case,
12999 we set b->clip_changed to 1 to force updating the screen. If
13000 b->clip_changed has already been set to 1, we can skip this
13001 check. */
13002 if (!b->clip_changed && w->window_end_valid)
13003 {
13004 ptrdiff_t pt = (w == XWINDOW (selected_window)
13005 ? PT : marker_position (w->pointm));
13006
13007 if ((w->current_matrix->buffer != b || pt != w->last_point)
13008 && check_point_in_composition (w->current_matrix->buffer,
13009 w->last_point, b, pt))
13010 b->clip_changed = 1;
13011 }
13012 }
13013
13014 #define STOP_POLLING \
13015 do { if (! polling_stopped_here) stop_polling (); \
13016 polling_stopped_here = 1; } while (0)
13017
13018 #define RESUME_POLLING \
13019 do { if (polling_stopped_here) start_polling (); \
13020 polling_stopped_here = 0; } while (0)
13021
13022
13023 /* Perhaps in the future avoid recentering windows if it
13024 is not necessary; currently that causes some problems. */
13025
13026 static void
13027 redisplay_internal (void)
13028 {
13029 struct window *w = XWINDOW (selected_window);
13030 struct window *sw;
13031 struct frame *fr;
13032 int pending;
13033 bool must_finish = 0, match_p;
13034 struct text_pos tlbufpos, tlendpos;
13035 int number_of_visible_frames;
13036 ptrdiff_t count;
13037 struct frame *sf;
13038 int polling_stopped_here = 0;
13039 Lisp_Object tail, frame;
13040
13041 /* Non-zero means redisplay has to consider all windows on all
13042 frames. Zero means, only selected_window is considered. */
13043 int consider_all_windows_p;
13044
13045 /* Non-zero means redisplay has to redisplay the miniwindow. */
13046 int update_miniwindow_p = 0;
13047
13048 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13049
13050 /* No redisplay if running in batch mode or frame is not yet fully
13051 initialized, or redisplay is explicitly turned off by setting
13052 Vinhibit_redisplay. */
13053 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13054 || !NILP (Vinhibit_redisplay))
13055 return;
13056
13057 /* Don't examine these until after testing Vinhibit_redisplay.
13058 When Emacs is shutting down, perhaps because its connection to
13059 X has dropped, we should not look at them at all. */
13060 fr = XFRAME (w->frame);
13061 sf = SELECTED_FRAME ();
13062
13063 if (!fr->glyphs_initialized_p)
13064 return;
13065
13066 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13067 if (popup_activated ())
13068 return;
13069 #endif
13070
13071 /* I don't think this happens but let's be paranoid. */
13072 if (redisplaying_p)
13073 return;
13074
13075 /* Record a function that clears redisplaying_p
13076 when we leave this function. */
13077 count = SPECPDL_INDEX ();
13078 record_unwind_protect_void (unwind_redisplay);
13079 redisplaying_p = 1;
13080 specbind (Qinhibit_free_realized_faces, Qnil);
13081
13082 /* Record this function, so it appears on the profiler's backtraces. */
13083 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13084
13085 FOR_EACH_FRAME (tail, frame)
13086 XFRAME (frame)->already_hscrolled_p = 0;
13087
13088 retry:
13089 /* Remember the currently selected window. */
13090 sw = w;
13091
13092 pending = 0;
13093 last_escape_glyph_frame = NULL;
13094 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13095 last_glyphless_glyph_frame = NULL;
13096 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13097
13098 /* If new fonts have been loaded that make a glyph matrix adjustment
13099 necessary, do it. */
13100 if (fonts_changed_p)
13101 {
13102 adjust_glyphs (NULL);
13103 ++windows_or_buffers_changed;
13104 fonts_changed_p = 0;
13105 }
13106
13107 /* If face_change_count is non-zero, init_iterator will free all
13108 realized faces, which includes the faces referenced from current
13109 matrices. So, we can't reuse current matrices in this case. */
13110 if (face_change_count)
13111 ++windows_or_buffers_changed;
13112
13113 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13114 && FRAME_TTY (sf)->previous_frame != sf)
13115 {
13116 /* Since frames on a single ASCII terminal share the same
13117 display area, displaying a different frame means redisplay
13118 the whole thing. */
13119 windows_or_buffers_changed++;
13120 SET_FRAME_GARBAGED (sf);
13121 #ifndef DOS_NT
13122 set_tty_color_mode (FRAME_TTY (sf), sf);
13123 #endif
13124 FRAME_TTY (sf)->previous_frame = sf;
13125 }
13126
13127 /* Set the visible flags for all frames. Do this before checking for
13128 resized or garbaged frames; they want to know if their frames are
13129 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13130 number_of_visible_frames = 0;
13131
13132 FOR_EACH_FRAME (tail, frame)
13133 {
13134 struct frame *f = XFRAME (frame);
13135
13136 if (FRAME_VISIBLE_P (f))
13137 ++number_of_visible_frames;
13138 clear_desired_matrices (f);
13139 }
13140
13141 /* Notice any pending interrupt request to change frame size. */
13142 do_pending_window_change (1);
13143
13144 /* do_pending_window_change could change the selected_window due to
13145 frame resizing which makes the selected window too small. */
13146 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13147 sw = w;
13148
13149 /* Clear frames marked as garbaged. */
13150 clear_garbaged_frames ();
13151
13152 /* Build menubar and tool-bar items. */
13153 if (NILP (Vmemory_full))
13154 prepare_menu_bars ();
13155
13156 if (windows_or_buffers_changed)
13157 update_mode_lines++;
13158
13159 reconsider_clip_changes (w);
13160
13161 /* In most cases selected window displays current buffer. */
13162 match_p = XBUFFER (w->contents) == current_buffer;
13163 if (match_p)
13164 {
13165 ptrdiff_t count1;
13166
13167 /* Detect case that we need to write or remove a star in the mode line. */
13168 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13169 {
13170 w->update_mode_line = 1;
13171 if (buffer_shared_and_changed ())
13172 update_mode_lines++;
13173 }
13174
13175 /* Avoid invocation of point motion hooks by `current_column' below. */
13176 count1 = SPECPDL_INDEX ();
13177 specbind (Qinhibit_point_motion_hooks, Qt);
13178
13179 if (mode_line_update_needed (w))
13180 w->update_mode_line = 1;
13181
13182 unbind_to (count1, Qnil);
13183 }
13184
13185 consider_all_windows_p = (update_mode_lines
13186 || buffer_shared_and_changed ()
13187 || cursor_type_changed);
13188
13189 /* If specs for an arrow have changed, do thorough redisplay
13190 to ensure we remove any arrow that should no longer exist. */
13191 if (overlay_arrows_changed_p ())
13192 consider_all_windows_p = windows_or_buffers_changed = 1;
13193
13194 /* Normally the message* functions will have already displayed and
13195 updated the echo area, but the frame may have been trashed, or
13196 the update may have been preempted, so display the echo area
13197 again here. Checking message_cleared_p captures the case that
13198 the echo area should be cleared. */
13199 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13200 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13201 || (message_cleared_p
13202 && minibuf_level == 0
13203 /* If the mini-window is currently selected, this means the
13204 echo-area doesn't show through. */
13205 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13206 {
13207 int window_height_changed_p = echo_area_display (0);
13208
13209 if (message_cleared_p)
13210 update_miniwindow_p = 1;
13211
13212 must_finish = 1;
13213
13214 /* If we don't display the current message, don't clear the
13215 message_cleared_p flag, because, if we did, we wouldn't clear
13216 the echo area in the next redisplay which doesn't preserve
13217 the echo area. */
13218 if (!display_last_displayed_message_p)
13219 message_cleared_p = 0;
13220
13221 if (fonts_changed_p)
13222 goto retry;
13223 else if (window_height_changed_p)
13224 {
13225 consider_all_windows_p = 1;
13226 ++update_mode_lines;
13227 ++windows_or_buffers_changed;
13228
13229 /* If window configuration was changed, frames may have been
13230 marked garbaged. Clear them or we will experience
13231 surprises wrt scrolling. */
13232 clear_garbaged_frames ();
13233 }
13234 }
13235 else if (EQ (selected_window, minibuf_window)
13236 && (current_buffer->clip_changed || window_outdated (w))
13237 && resize_mini_window (w, 0))
13238 {
13239 /* Resized active mini-window to fit the size of what it is
13240 showing if its contents might have changed. */
13241 must_finish = 1;
13242 /* FIXME: this causes all frames to be updated, which seems unnecessary
13243 since only the current frame needs to be considered. This function
13244 needs to be rewritten with two variables, consider_all_windows and
13245 consider_all_frames. */
13246 consider_all_windows_p = 1;
13247 ++windows_or_buffers_changed;
13248 ++update_mode_lines;
13249
13250 /* If window configuration was changed, frames may have been
13251 marked garbaged. Clear them or we will experience
13252 surprises wrt scrolling. */
13253 clear_garbaged_frames ();
13254 }
13255
13256 /* If showing the region, and mark has changed, we must redisplay
13257 the whole window. The assignment to this_line_start_pos prevents
13258 the optimization directly below this if-statement. */
13259 if (((!NILP (Vtransient_mark_mode)
13260 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13261 != (w->region_showing > 0))
13262 || (w->region_showing
13263 && w->region_showing
13264 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13265 CHARPOS (this_line_start_pos) = 0;
13266
13267 /* Optimize the case that only the line containing the cursor in the
13268 selected window has changed. Variables starting with this_ are
13269 set in display_line and record information about the line
13270 containing the cursor. */
13271 tlbufpos = this_line_start_pos;
13272 tlendpos = this_line_end_pos;
13273 if (!consider_all_windows_p
13274 && CHARPOS (tlbufpos) > 0
13275 && !w->update_mode_line
13276 && !current_buffer->clip_changed
13277 && !current_buffer->prevent_redisplay_optimizations_p
13278 && FRAME_VISIBLE_P (XFRAME (w->frame))
13279 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13280 /* Make sure recorded data applies to current buffer, etc. */
13281 && this_line_buffer == current_buffer
13282 && match_p
13283 && !w->force_start
13284 && !w->optional_new_start
13285 /* Point must be on the line that we have info recorded about. */
13286 && PT >= CHARPOS (tlbufpos)
13287 && PT <= Z - CHARPOS (tlendpos)
13288 /* All text outside that line, including its final newline,
13289 must be unchanged. */
13290 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13291 CHARPOS (tlendpos)))
13292 {
13293 if (CHARPOS (tlbufpos) > BEGV
13294 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13295 && (CHARPOS (tlbufpos) == ZV
13296 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13297 /* Former continuation line has disappeared by becoming empty. */
13298 goto cancel;
13299 else if (window_outdated (w) || MINI_WINDOW_P (w))
13300 {
13301 /* We have to handle the case of continuation around a
13302 wide-column character (see the comment in indent.c around
13303 line 1340).
13304
13305 For instance, in the following case:
13306
13307 -------- Insert --------
13308 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13309 J_I_ ==> J_I_ `^^' are cursors.
13310 ^^ ^^
13311 -------- --------
13312
13313 As we have to redraw the line above, we cannot use this
13314 optimization. */
13315
13316 struct it it;
13317 int line_height_before = this_line_pixel_height;
13318
13319 /* Note that start_display will handle the case that the
13320 line starting at tlbufpos is a continuation line. */
13321 start_display (&it, w, tlbufpos);
13322
13323 /* Implementation note: It this still necessary? */
13324 if (it.current_x != this_line_start_x)
13325 goto cancel;
13326
13327 TRACE ((stderr, "trying display optimization 1\n"));
13328 w->cursor.vpos = -1;
13329 overlay_arrow_seen = 0;
13330 it.vpos = this_line_vpos;
13331 it.current_y = this_line_y;
13332 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13333 display_line (&it);
13334
13335 /* If line contains point, is not continued,
13336 and ends at same distance from eob as before, we win. */
13337 if (w->cursor.vpos >= 0
13338 /* Line is not continued, otherwise this_line_start_pos
13339 would have been set to 0 in display_line. */
13340 && CHARPOS (this_line_start_pos)
13341 /* Line ends as before. */
13342 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13343 /* Line has same height as before. Otherwise other lines
13344 would have to be shifted up or down. */
13345 && this_line_pixel_height == line_height_before)
13346 {
13347 /* If this is not the window's last line, we must adjust
13348 the charstarts of the lines below. */
13349 if (it.current_y < it.last_visible_y)
13350 {
13351 struct glyph_row *row
13352 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13353 ptrdiff_t delta, delta_bytes;
13354
13355 /* We used to distinguish between two cases here,
13356 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13357 when the line ends in a newline or the end of the
13358 buffer's accessible portion. But both cases did
13359 the same, so they were collapsed. */
13360 delta = (Z
13361 - CHARPOS (tlendpos)
13362 - MATRIX_ROW_START_CHARPOS (row));
13363 delta_bytes = (Z_BYTE
13364 - BYTEPOS (tlendpos)
13365 - MATRIX_ROW_START_BYTEPOS (row));
13366
13367 increment_matrix_positions (w->current_matrix,
13368 this_line_vpos + 1,
13369 w->current_matrix->nrows,
13370 delta, delta_bytes);
13371 }
13372
13373 /* If this row displays text now but previously didn't,
13374 or vice versa, w->window_end_vpos may have to be
13375 adjusted. */
13376 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13377 {
13378 if (w->window_end_vpos < this_line_vpos)
13379 w->window_end_vpos = this_line_vpos;
13380 }
13381 else if (w->window_end_vpos == this_line_vpos
13382 && this_line_vpos > 0)
13383 w->window_end_vpos = this_line_vpos - 1;
13384 w->window_end_valid = 0;
13385
13386 /* Update hint: No need to try to scroll in update_window. */
13387 w->desired_matrix->no_scrolling_p = 1;
13388
13389 #ifdef GLYPH_DEBUG
13390 *w->desired_matrix->method = 0;
13391 debug_method_add (w, "optimization 1");
13392 #endif
13393 #if HAVE_XWIDGETS
13394 //debug optimization movement issue
13395 //w->desired_matrix->no_scrolling_p = 1;
13396 //*w->desired_matrix->method = 0;
13397 //debug_method_add (w, "optimization 1");
13398 #endif
13399
13400 #ifdef HAVE_WINDOW_SYSTEM
13401 update_window_fringes (w, 0);
13402 #endif
13403 goto update;
13404 }
13405 else
13406 goto cancel;
13407 }
13408 else if (/* Cursor position hasn't changed. */
13409 PT == w->last_point
13410 /* Make sure the cursor was last displayed
13411 in this window. Otherwise we have to reposition it. */
13412 && 0 <= w->cursor.vpos
13413 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13414 {
13415 if (!must_finish)
13416 {
13417 do_pending_window_change (1);
13418 /* If selected_window changed, redisplay again. */
13419 if (WINDOWP (selected_window)
13420 && (w = XWINDOW (selected_window)) != sw)
13421 goto retry;
13422
13423 /* We used to always goto end_of_redisplay here, but this
13424 isn't enough if we have a blinking cursor. */
13425 if (w->cursor_off_p == w->last_cursor_off_p)
13426 goto end_of_redisplay;
13427 }
13428 goto update;
13429 }
13430 /* If highlighting the region, or if the cursor is in the echo area,
13431 then we can't just move the cursor. */
13432 else if (! (!NILP (Vtransient_mark_mode)
13433 && !NILP (BVAR (current_buffer, mark_active)))
13434 && (EQ (selected_window,
13435 BVAR (current_buffer, last_selected_window))
13436 || highlight_nonselected_windows)
13437 && !w->region_showing
13438 && NILP (Vshow_trailing_whitespace)
13439 && !cursor_in_echo_area)
13440 {
13441 struct it it;
13442 struct glyph_row *row;
13443
13444 /* Skip from tlbufpos to PT and see where it is. Note that
13445 PT may be in invisible text. If so, we will end at the
13446 next visible position. */
13447 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13448 NULL, DEFAULT_FACE_ID);
13449 it.current_x = this_line_start_x;
13450 it.current_y = this_line_y;
13451 it.vpos = this_line_vpos;
13452
13453 /* The call to move_it_to stops in front of PT, but
13454 moves over before-strings. */
13455 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13456
13457 if (it.vpos == this_line_vpos
13458 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13459 row->enabled_p))
13460 {
13461 eassert (this_line_vpos == it.vpos);
13462 eassert (this_line_y == it.current_y);
13463 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13464 #ifdef GLYPH_DEBUG
13465 *w->desired_matrix->method = 0;
13466 debug_method_add (w, "optimization 3");
13467 #endif
13468 goto update;
13469 }
13470 else
13471 goto cancel;
13472 }
13473
13474 cancel:
13475 /* Text changed drastically or point moved off of line. */
13476 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13477 }
13478
13479 CHARPOS (this_line_start_pos) = 0;
13480 consider_all_windows_p |= buffer_shared_and_changed ();
13481 ++clear_face_cache_count;
13482 #ifdef HAVE_WINDOW_SYSTEM
13483 ++clear_image_cache_count;
13484 #endif
13485
13486 /* Build desired matrices, and update the display. If
13487 consider_all_windows_p is non-zero, do it for all windows on all
13488 frames. Otherwise do it for selected_window, only. */
13489
13490 if (consider_all_windows_p)
13491 {
13492 FOR_EACH_FRAME (tail, frame)
13493 XFRAME (frame)->updated_p = 0;
13494
13495 FOR_EACH_FRAME (tail, frame)
13496 {
13497 struct frame *f = XFRAME (frame);
13498
13499 /* We don't have to do anything for unselected terminal
13500 frames. */
13501 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13502 && !EQ (FRAME_TTY (f)->top_frame, frame))
13503 continue;
13504
13505 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13506 {
13507 /* Mark all the scroll bars to be removed; we'll redeem
13508 the ones we want when we redisplay their windows. */
13509 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13510 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13511
13512 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13513 redisplay_windows (FRAME_ROOT_WINDOW (f));
13514
13515 /* The X error handler may have deleted that frame. */
13516 if (!FRAME_LIVE_P (f))
13517 continue;
13518
13519 /* Any scroll bars which redisplay_windows should have
13520 nuked should now go away. */
13521 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13522 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13523
13524 /* If fonts changed, display again. */
13525 /* ??? rms: I suspect it is a mistake to jump all the way
13526 back to retry here. It should just retry this frame. */
13527 if (fonts_changed_p)
13528 goto retry;
13529
13530 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13531 {
13532 /* See if we have to hscroll. */
13533 if (!f->already_hscrolled_p)
13534 {
13535 f->already_hscrolled_p = 1;
13536 if (hscroll_windows (f->root_window))
13537 goto retry;
13538 }
13539
13540 /* Prevent various kinds of signals during display
13541 update. stdio is not robust about handling
13542 signals, which can cause an apparent I/O
13543 error. */
13544 if (interrupt_input)
13545 unrequest_sigio ();
13546 STOP_POLLING;
13547
13548 /* Update the display. */
13549 set_window_update_flags (XWINDOW (f->root_window), 1);
13550 pending |= update_frame (f, 0, 0);
13551 f->updated_p = 1;
13552 }
13553 }
13554 }
13555
13556 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13557
13558 if (!pending)
13559 {
13560 /* Do the mark_window_display_accurate after all windows have
13561 been redisplayed because this call resets flags in buffers
13562 which are needed for proper redisplay. */
13563 FOR_EACH_FRAME (tail, frame)
13564 {
13565 struct frame *f = XFRAME (frame);
13566 if (f->updated_p)
13567 {
13568 mark_window_display_accurate (f->root_window, 1);
13569 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13570 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13571 }
13572 }
13573 }
13574 }
13575 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13576 {
13577 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13578 struct frame *mini_frame;
13579
13580 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13581 /* Use list_of_error, not Qerror, so that
13582 we catch only errors and don't run the debugger. */
13583 internal_condition_case_1 (redisplay_window_1, selected_window,
13584 list_of_error,
13585 redisplay_window_error);
13586 if (update_miniwindow_p)
13587 internal_condition_case_1 (redisplay_window_1, mini_window,
13588 list_of_error,
13589 redisplay_window_error);
13590
13591 /* Compare desired and current matrices, perform output. */
13592
13593 update:
13594 /* If fonts changed, display again. */
13595 if (fonts_changed_p)
13596 goto retry;
13597
13598 /* Prevent various kinds of signals during display update.
13599 stdio is not robust about handling signals,
13600 which can cause an apparent I/O error. */
13601 if (interrupt_input)
13602 unrequest_sigio ();
13603 STOP_POLLING;
13604
13605 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13606 {
13607 if (hscroll_windows (selected_window))
13608 goto retry;
13609
13610 XWINDOW (selected_window)->must_be_updated_p = 1;
13611 pending = update_frame (sf, 0, 0);
13612 }
13613
13614 /* We may have called echo_area_display at the top of this
13615 function. If the echo area is on another frame, that may
13616 have put text on a frame other than the selected one, so the
13617 above call to update_frame would not have caught it. Catch
13618 it here. */
13619 mini_window = FRAME_MINIBUF_WINDOW (sf);
13620 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13621
13622 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13623 {
13624 XWINDOW (mini_window)->must_be_updated_p = 1;
13625 pending |= update_frame (mini_frame, 0, 0);
13626 if (!pending && hscroll_windows (mini_window))
13627 goto retry;
13628 }
13629 }
13630
13631 /* If display was paused because of pending input, make sure we do a
13632 thorough update the next time. */
13633 if (pending)
13634 {
13635 /* Prevent the optimization at the beginning of
13636 redisplay_internal that tries a single-line update of the
13637 line containing the cursor in the selected window. */
13638 CHARPOS (this_line_start_pos) = 0;
13639
13640 /* Let the overlay arrow be updated the next time. */
13641 update_overlay_arrows (0);
13642
13643 /* If we pause after scrolling, some rows in the current
13644 matrices of some windows are not valid. */
13645 if (!WINDOW_FULL_WIDTH_P (w)
13646 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13647 update_mode_lines = 1;
13648 }
13649 else
13650 {
13651 if (!consider_all_windows_p)
13652 {
13653 /* This has already been done above if
13654 consider_all_windows_p is set. */
13655 mark_window_display_accurate_1 (w, 1);
13656
13657 /* Say overlay arrows are up to date. */
13658 update_overlay_arrows (1);
13659
13660 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13661 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13662 }
13663
13664 update_mode_lines = 0;
13665 windows_or_buffers_changed = 0;
13666 cursor_type_changed = 0;
13667 }
13668
13669 /* Start SIGIO interrupts coming again. Having them off during the
13670 code above makes it less likely one will discard output, but not
13671 impossible, since there might be stuff in the system buffer here.
13672 But it is much hairier to try to do anything about that. */
13673 if (interrupt_input)
13674 request_sigio ();
13675 RESUME_POLLING;
13676
13677 /* If a frame has become visible which was not before, redisplay
13678 again, so that we display it. Expose events for such a frame
13679 (which it gets when becoming visible) don't call the parts of
13680 redisplay constructing glyphs, so simply exposing a frame won't
13681 display anything in this case. So, we have to display these
13682 frames here explicitly. */
13683 if (!pending)
13684 {
13685 int new_count = 0;
13686
13687 FOR_EACH_FRAME (tail, frame)
13688 {
13689 int this_is_visible = 0;
13690
13691 if (XFRAME (frame)->visible)
13692 this_is_visible = 1;
13693
13694 if (this_is_visible)
13695 new_count++;
13696 }
13697
13698 if (new_count != number_of_visible_frames)
13699 windows_or_buffers_changed++;
13700 }
13701
13702 /* Change frame size now if a change is pending. */
13703 do_pending_window_change (1);
13704
13705 /* If we just did a pending size change, or have additional
13706 visible frames, or selected_window changed, redisplay again. */
13707 if ((windows_or_buffers_changed && !pending)
13708 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13709 goto retry;
13710
13711 /* Clear the face and image caches.
13712
13713 We used to do this only if consider_all_windows_p. But the cache
13714 needs to be cleared if a timer creates images in the current
13715 buffer (e.g. the test case in Bug#6230). */
13716
13717 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13718 {
13719 clear_face_cache (0);
13720 clear_face_cache_count = 0;
13721 }
13722
13723 #ifdef HAVE_WINDOW_SYSTEM
13724 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13725 {
13726 clear_image_caches (Qnil);
13727 clear_image_cache_count = 0;
13728 }
13729 #endif /* HAVE_WINDOW_SYSTEM */
13730
13731 end_of_redisplay:
13732 unbind_to (count, Qnil);
13733 RESUME_POLLING;
13734 }
13735
13736
13737 /* Redisplay, but leave alone any recent echo area message unless
13738 another message has been requested in its place.
13739
13740 This is useful in situations where you need to redisplay but no
13741 user action has occurred, making it inappropriate for the message
13742 area to be cleared. See tracking_off and
13743 wait_reading_process_output for examples of these situations.
13744
13745 FROM_WHERE is an integer saying from where this function was
13746 called. This is useful for debugging. */
13747
13748 void
13749 redisplay_preserve_echo_area (int from_where)
13750 {
13751 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13752
13753 if (!NILP (echo_area_buffer[1]))
13754 {
13755 /* We have a previously displayed message, but no current
13756 message. Redisplay the previous message. */
13757 display_last_displayed_message_p = 1;
13758 redisplay_internal ();
13759 display_last_displayed_message_p = 0;
13760 }
13761 else
13762 redisplay_internal ();
13763
13764 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13765 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13766 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13767 }
13768
13769
13770 /* Function registered with record_unwind_protect in redisplay_internal. */
13771
13772 static void
13773 unwind_redisplay (void)
13774 {
13775 redisplaying_p = 0;
13776 }
13777
13778
13779 /* Mark the display of leaf window W as accurate or inaccurate.
13780 If ACCURATE_P is non-zero mark display of W as accurate. If
13781 ACCURATE_P is zero, arrange for W to be redisplayed the next
13782 time redisplay_internal is called. */
13783
13784 static void
13785 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13786 {
13787 struct buffer *b = XBUFFER (w->contents);
13788
13789 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13790 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13791 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13792
13793 if (accurate_p)
13794 {
13795 b->clip_changed = 0;
13796 b->prevent_redisplay_optimizations_p = 0;
13797
13798 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13799 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13800 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13801 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13802
13803 w->current_matrix->buffer = b;
13804 w->current_matrix->begv = BUF_BEGV (b);
13805 w->current_matrix->zv = BUF_ZV (b);
13806
13807 w->last_cursor_vpos = w->cursor.vpos;
13808 w->last_cursor_off_p = w->cursor_off_p;
13809
13810 if (w == XWINDOW (selected_window))
13811 w->last_point = BUF_PT (b);
13812 else
13813 w->last_point = marker_position (w->pointm);
13814
13815 w->window_end_valid = 1;
13816 w->update_mode_line = 0;
13817 }
13818 }
13819
13820
13821 /* Mark the display of windows in the window tree rooted at WINDOW as
13822 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13823 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13824 be redisplayed the next time redisplay_internal is called. */
13825
13826 void
13827 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13828 {
13829 struct window *w;
13830
13831 for (; !NILP (window); window = w->next)
13832 {
13833 w = XWINDOW (window);
13834 if (WINDOWP (w->contents))
13835 mark_window_display_accurate (w->contents, accurate_p);
13836 else
13837 mark_window_display_accurate_1 (w, accurate_p);
13838 }
13839
13840 if (accurate_p)
13841 update_overlay_arrows (1);
13842 else
13843 /* Force a thorough redisplay the next time by setting
13844 last_arrow_position and last_arrow_string to t, which is
13845 unequal to any useful value of Voverlay_arrow_... */
13846 update_overlay_arrows (-1);
13847 }
13848
13849
13850 /* Return value in display table DP (Lisp_Char_Table *) for character
13851 C. Since a display table doesn't have any parent, we don't have to
13852 follow parent. Do not call this function directly but use the
13853 macro DISP_CHAR_VECTOR. */
13854
13855 Lisp_Object
13856 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13857 {
13858 Lisp_Object val;
13859
13860 if (ASCII_CHAR_P (c))
13861 {
13862 val = dp->ascii;
13863 if (SUB_CHAR_TABLE_P (val))
13864 val = XSUB_CHAR_TABLE (val)->contents[c];
13865 }
13866 else
13867 {
13868 Lisp_Object table;
13869
13870 XSETCHAR_TABLE (table, dp);
13871 val = char_table_ref (table, c);
13872 }
13873 if (NILP (val))
13874 val = dp->defalt;
13875 return val;
13876 }
13877
13878
13879 \f
13880 /***********************************************************************
13881 Window Redisplay
13882 ***********************************************************************/
13883
13884 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13885
13886 static void
13887 redisplay_windows (Lisp_Object window)
13888 {
13889 while (!NILP (window))
13890 {
13891 struct window *w = XWINDOW (window);
13892
13893 if (WINDOWP (w->contents))
13894 redisplay_windows (w->contents);
13895 else if (BUFFERP (w->contents))
13896 {
13897 displayed_buffer = XBUFFER (w->contents);
13898 /* Use list_of_error, not Qerror, so that
13899 we catch only errors and don't run the debugger. */
13900 internal_condition_case_1 (redisplay_window_0, window,
13901 list_of_error,
13902 redisplay_window_error);
13903 }
13904
13905 window = w->next;
13906 }
13907 }
13908
13909 static Lisp_Object
13910 redisplay_window_error (Lisp_Object ignore)
13911 {
13912 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13913 return Qnil;
13914 }
13915
13916 static Lisp_Object
13917 redisplay_window_0 (Lisp_Object window)
13918 {
13919 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13920 redisplay_window (window, 0);
13921 return Qnil;
13922 }
13923
13924 static Lisp_Object
13925 redisplay_window_1 (Lisp_Object window)
13926 {
13927 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13928 redisplay_window (window, 1);
13929 return Qnil;
13930 }
13931 \f
13932
13933 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13934 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13935 which positions recorded in ROW differ from current buffer
13936 positions.
13937
13938 Return 0 if cursor is not on this row, 1 otherwise. */
13939
13940 static int
13941 set_cursor_from_row (struct window *w, struct glyph_row *row,
13942 struct glyph_matrix *matrix,
13943 ptrdiff_t delta, ptrdiff_t delta_bytes,
13944 int dy, int dvpos)
13945 {
13946 struct glyph *glyph = row->glyphs[TEXT_AREA];
13947 struct glyph *end = glyph + row->used[TEXT_AREA];
13948 struct glyph *cursor = NULL;
13949 /* The last known character position in row. */
13950 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13951 int x = row->x;
13952 ptrdiff_t pt_old = PT - delta;
13953 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13954 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13955 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13956 /* A glyph beyond the edge of TEXT_AREA which we should never
13957 touch. */
13958 struct glyph *glyphs_end = end;
13959 /* Non-zero means we've found a match for cursor position, but that
13960 glyph has the avoid_cursor_p flag set. */
13961 int match_with_avoid_cursor = 0;
13962 /* Non-zero means we've seen at least one glyph that came from a
13963 display string. */
13964 int string_seen = 0;
13965 /* Largest and smallest buffer positions seen so far during scan of
13966 glyph row. */
13967 ptrdiff_t bpos_max = pos_before;
13968 ptrdiff_t bpos_min = pos_after;
13969 /* Last buffer position covered by an overlay string with an integer
13970 `cursor' property. */
13971 ptrdiff_t bpos_covered = 0;
13972 /* Non-zero means the display string on which to display the cursor
13973 comes from a text property, not from an overlay. */
13974 int string_from_text_prop = 0;
13975
13976 /* Don't even try doing anything if called for a mode-line or
13977 header-line row, since the rest of the code isn't prepared to
13978 deal with such calamities. */
13979 eassert (!row->mode_line_p);
13980 if (row->mode_line_p)
13981 return 0;
13982
13983 /* Skip over glyphs not having an object at the start and the end of
13984 the row. These are special glyphs like truncation marks on
13985 terminal frames. */
13986 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13987 {
13988 if (!row->reversed_p)
13989 {
13990 while (glyph < end
13991 && INTEGERP (glyph->object)
13992 && glyph->charpos < 0)
13993 {
13994 x += glyph->pixel_width;
13995 ++glyph;
13996 }
13997 while (end > glyph
13998 && INTEGERP ((end - 1)->object)
13999 /* CHARPOS is zero for blanks and stretch glyphs
14000 inserted by extend_face_to_end_of_line. */
14001 && (end - 1)->charpos <= 0)
14002 --end;
14003 glyph_before = glyph - 1;
14004 glyph_after = end;
14005 }
14006 else
14007 {
14008 struct glyph *g;
14009
14010 /* If the glyph row is reversed, we need to process it from back
14011 to front, so swap the edge pointers. */
14012 glyphs_end = end = glyph - 1;
14013 glyph += row->used[TEXT_AREA] - 1;
14014
14015 while (glyph > end + 1
14016 && INTEGERP (glyph->object)
14017 && glyph->charpos < 0)
14018 {
14019 --glyph;
14020 x -= glyph->pixel_width;
14021 }
14022 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14023 --glyph;
14024 /* By default, in reversed rows we put the cursor on the
14025 rightmost (first in the reading order) glyph. */
14026 for (g = end + 1; g < glyph; g++)
14027 x += g->pixel_width;
14028 while (end < glyph
14029 && INTEGERP ((end + 1)->object)
14030 && (end + 1)->charpos <= 0)
14031 ++end;
14032 glyph_before = glyph + 1;
14033 glyph_after = end;
14034 }
14035 }
14036 else if (row->reversed_p)
14037 {
14038 /* In R2L rows that don't display text, put the cursor on the
14039 rightmost glyph. Case in point: an empty last line that is
14040 part of an R2L paragraph. */
14041 cursor = end - 1;
14042 /* Avoid placing the cursor on the last glyph of the row, where
14043 on terminal frames we hold the vertical border between
14044 adjacent windows. */
14045 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14046 && !WINDOW_RIGHTMOST_P (w)
14047 && cursor == row->glyphs[LAST_AREA] - 1)
14048 cursor--;
14049 x = -1; /* will be computed below, at label compute_x */
14050 }
14051
14052 /* Step 1: Try to find the glyph whose character position
14053 corresponds to point. If that's not possible, find 2 glyphs
14054 whose character positions are the closest to point, one before
14055 point, the other after it. */
14056 if (!row->reversed_p)
14057 while (/* not marched to end of glyph row */
14058 glyph < end
14059 /* glyph was not inserted by redisplay for internal purposes */
14060 && !INTEGERP (glyph->object))
14061 {
14062 if (BUFFERP (glyph->object))
14063 {
14064 ptrdiff_t dpos = glyph->charpos - pt_old;
14065
14066 if (glyph->charpos > bpos_max)
14067 bpos_max = glyph->charpos;
14068 if (glyph->charpos < bpos_min)
14069 bpos_min = glyph->charpos;
14070 if (!glyph->avoid_cursor_p)
14071 {
14072 /* If we hit point, we've found the glyph on which to
14073 display the cursor. */
14074 if (dpos == 0)
14075 {
14076 match_with_avoid_cursor = 0;
14077 break;
14078 }
14079 /* See if we've found a better approximation to
14080 POS_BEFORE or to POS_AFTER. */
14081 if (0 > dpos && dpos > pos_before - pt_old)
14082 {
14083 pos_before = glyph->charpos;
14084 glyph_before = glyph;
14085 }
14086 else if (0 < dpos && dpos < pos_after - pt_old)
14087 {
14088 pos_after = glyph->charpos;
14089 glyph_after = glyph;
14090 }
14091 }
14092 else if (dpos == 0)
14093 match_with_avoid_cursor = 1;
14094 }
14095 else if (STRINGP (glyph->object))
14096 {
14097 Lisp_Object chprop;
14098 ptrdiff_t glyph_pos = glyph->charpos;
14099
14100 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14101 glyph->object);
14102 if (!NILP (chprop))
14103 {
14104 /* If the string came from a `display' text property,
14105 look up the buffer position of that property and
14106 use that position to update bpos_max, as if we
14107 actually saw such a position in one of the row's
14108 glyphs. This helps with supporting integer values
14109 of `cursor' property on the display string in
14110 situations where most or all of the row's buffer
14111 text is completely covered by display properties,
14112 so that no glyph with valid buffer positions is
14113 ever seen in the row. */
14114 ptrdiff_t prop_pos =
14115 string_buffer_position_lim (glyph->object, pos_before,
14116 pos_after, 0);
14117
14118 if (prop_pos >= pos_before)
14119 bpos_max = prop_pos - 1;
14120 }
14121 if (INTEGERP (chprop))
14122 {
14123 bpos_covered = bpos_max + XINT (chprop);
14124 /* If the `cursor' property covers buffer positions up
14125 to and including point, we should display cursor on
14126 this glyph. Note that, if a `cursor' property on one
14127 of the string's characters has an integer value, we
14128 will break out of the loop below _before_ we get to
14129 the position match above. IOW, integer values of
14130 the `cursor' property override the "exact match for
14131 point" strategy of positioning the cursor. */
14132 /* Implementation note: bpos_max == pt_old when, e.g.,
14133 we are in an empty line, where bpos_max is set to
14134 MATRIX_ROW_START_CHARPOS, see above. */
14135 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14136 {
14137 cursor = glyph;
14138 break;
14139 }
14140 }
14141
14142 string_seen = 1;
14143 }
14144 x += glyph->pixel_width;
14145 ++glyph;
14146 }
14147 else if (glyph > end) /* row is reversed */
14148 while (!INTEGERP (glyph->object))
14149 {
14150 if (BUFFERP (glyph->object))
14151 {
14152 ptrdiff_t dpos = glyph->charpos - pt_old;
14153
14154 if (glyph->charpos > bpos_max)
14155 bpos_max = glyph->charpos;
14156 if (glyph->charpos < bpos_min)
14157 bpos_min = glyph->charpos;
14158 if (!glyph->avoid_cursor_p)
14159 {
14160 if (dpos == 0)
14161 {
14162 match_with_avoid_cursor = 0;
14163 break;
14164 }
14165 if (0 > dpos && dpos > pos_before - pt_old)
14166 {
14167 pos_before = glyph->charpos;
14168 glyph_before = glyph;
14169 }
14170 else if (0 < dpos && dpos < pos_after - pt_old)
14171 {
14172 pos_after = glyph->charpos;
14173 glyph_after = glyph;
14174 }
14175 }
14176 else if (dpos == 0)
14177 match_with_avoid_cursor = 1;
14178 }
14179 else if (STRINGP (glyph->object))
14180 {
14181 Lisp_Object chprop;
14182 ptrdiff_t glyph_pos = glyph->charpos;
14183
14184 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14185 glyph->object);
14186 if (!NILP (chprop))
14187 {
14188 ptrdiff_t prop_pos =
14189 string_buffer_position_lim (glyph->object, pos_before,
14190 pos_after, 0);
14191
14192 if (prop_pos >= pos_before)
14193 bpos_max = prop_pos - 1;
14194 }
14195 if (INTEGERP (chprop))
14196 {
14197 bpos_covered = bpos_max + XINT (chprop);
14198 /* If the `cursor' property covers buffer positions up
14199 to and including point, we should display cursor on
14200 this glyph. */
14201 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14202 {
14203 cursor = glyph;
14204 break;
14205 }
14206 }
14207 string_seen = 1;
14208 }
14209 --glyph;
14210 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14211 {
14212 x--; /* can't use any pixel_width */
14213 break;
14214 }
14215 x -= glyph->pixel_width;
14216 }
14217
14218 /* Step 2: If we didn't find an exact match for point, we need to
14219 look for a proper place to put the cursor among glyphs between
14220 GLYPH_BEFORE and GLYPH_AFTER. */
14221 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14222 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14223 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14224 {
14225 /* An empty line has a single glyph whose OBJECT is zero and
14226 whose CHARPOS is the position of a newline on that line.
14227 Note that on a TTY, there are more glyphs after that, which
14228 were produced by extend_face_to_end_of_line, but their
14229 CHARPOS is zero or negative. */
14230 int empty_line_p =
14231 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14232 && INTEGERP (glyph->object) && glyph->charpos > 0
14233 /* On a TTY, continued and truncated rows also have a glyph at
14234 their end whose OBJECT is zero and whose CHARPOS is
14235 positive (the continuation and truncation glyphs), but such
14236 rows are obviously not "empty". */
14237 && !(row->continued_p || row->truncated_on_right_p);
14238
14239 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14240 {
14241 ptrdiff_t ellipsis_pos;
14242
14243 /* Scan back over the ellipsis glyphs. */
14244 if (!row->reversed_p)
14245 {
14246 ellipsis_pos = (glyph - 1)->charpos;
14247 while (glyph > row->glyphs[TEXT_AREA]
14248 && (glyph - 1)->charpos == ellipsis_pos)
14249 glyph--, x -= glyph->pixel_width;
14250 /* That loop always goes one position too far, including
14251 the glyph before the ellipsis. So scan forward over
14252 that one. */
14253 x += glyph->pixel_width;
14254 glyph++;
14255 }
14256 else /* row is reversed */
14257 {
14258 ellipsis_pos = (glyph + 1)->charpos;
14259 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14260 && (glyph + 1)->charpos == ellipsis_pos)
14261 glyph++, x += glyph->pixel_width;
14262 x -= glyph->pixel_width;
14263 glyph--;
14264 }
14265 }
14266 else if (match_with_avoid_cursor)
14267 {
14268 cursor = glyph_after;
14269 x = -1;
14270 }
14271 else if (string_seen)
14272 {
14273 int incr = row->reversed_p ? -1 : +1;
14274
14275 /* Need to find the glyph that came out of a string which is
14276 present at point. That glyph is somewhere between
14277 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14278 positioned between POS_BEFORE and POS_AFTER in the
14279 buffer. */
14280 struct glyph *start, *stop;
14281 ptrdiff_t pos = pos_before;
14282
14283 x = -1;
14284
14285 /* If the row ends in a newline from a display string,
14286 reordering could have moved the glyphs belonging to the
14287 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14288 in this case we extend the search to the last glyph in
14289 the row that was not inserted by redisplay. */
14290 if (row->ends_in_newline_from_string_p)
14291 {
14292 glyph_after = end;
14293 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14294 }
14295
14296 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14297 correspond to POS_BEFORE and POS_AFTER, respectively. We
14298 need START and STOP in the order that corresponds to the
14299 row's direction as given by its reversed_p flag. If the
14300 directionality of characters between POS_BEFORE and
14301 POS_AFTER is the opposite of the row's base direction,
14302 these characters will have been reordered for display,
14303 and we need to reverse START and STOP. */
14304 if (!row->reversed_p)
14305 {
14306 start = min (glyph_before, glyph_after);
14307 stop = max (glyph_before, glyph_after);
14308 }
14309 else
14310 {
14311 start = max (glyph_before, glyph_after);
14312 stop = min (glyph_before, glyph_after);
14313 }
14314 for (glyph = start + incr;
14315 row->reversed_p ? glyph > stop : glyph < stop; )
14316 {
14317
14318 /* Any glyphs that come from the buffer are here because
14319 of bidi reordering. Skip them, and only pay
14320 attention to glyphs that came from some string. */
14321 if (STRINGP (glyph->object))
14322 {
14323 Lisp_Object str;
14324 ptrdiff_t tem;
14325 /* If the display property covers the newline, we
14326 need to search for it one position farther. */
14327 ptrdiff_t lim = pos_after
14328 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14329
14330 string_from_text_prop = 0;
14331 str = glyph->object;
14332 tem = string_buffer_position_lim (str, pos, lim, 0);
14333 if (tem == 0 /* from overlay */
14334 || pos <= tem)
14335 {
14336 /* If the string from which this glyph came is
14337 found in the buffer at point, or at position
14338 that is closer to point than pos_after, then
14339 we've found the glyph we've been looking for.
14340 If it comes from an overlay (tem == 0), and
14341 it has the `cursor' property on one of its
14342 glyphs, record that glyph as a candidate for
14343 displaying the cursor. (As in the
14344 unidirectional version, we will display the
14345 cursor on the last candidate we find.) */
14346 if (tem == 0
14347 || tem == pt_old
14348 || (tem - pt_old > 0 && tem < pos_after))
14349 {
14350 /* The glyphs from this string could have
14351 been reordered. Find the one with the
14352 smallest string position. Or there could
14353 be a character in the string with the
14354 `cursor' property, which means display
14355 cursor on that character's glyph. */
14356 ptrdiff_t strpos = glyph->charpos;
14357
14358 if (tem)
14359 {
14360 cursor = glyph;
14361 string_from_text_prop = 1;
14362 }
14363 for ( ;
14364 (row->reversed_p ? glyph > stop : glyph < stop)
14365 && EQ (glyph->object, str);
14366 glyph += incr)
14367 {
14368 Lisp_Object cprop;
14369 ptrdiff_t gpos = glyph->charpos;
14370
14371 cprop = Fget_char_property (make_number (gpos),
14372 Qcursor,
14373 glyph->object);
14374 if (!NILP (cprop))
14375 {
14376 cursor = glyph;
14377 break;
14378 }
14379 if (tem && glyph->charpos < strpos)
14380 {
14381 strpos = glyph->charpos;
14382 cursor = glyph;
14383 }
14384 }
14385
14386 if (tem == pt_old
14387 || (tem - pt_old > 0 && tem < pos_after))
14388 goto compute_x;
14389 }
14390 if (tem)
14391 pos = tem + 1; /* don't find previous instances */
14392 }
14393 /* This string is not what we want; skip all of the
14394 glyphs that came from it. */
14395 while ((row->reversed_p ? glyph > stop : glyph < stop)
14396 && EQ (glyph->object, str))
14397 glyph += incr;
14398 }
14399 else
14400 glyph += incr;
14401 }
14402
14403 /* If we reached the end of the line, and END was from a string,
14404 the cursor is not on this line. */
14405 if (cursor == NULL
14406 && (row->reversed_p ? glyph <= end : glyph >= end)
14407 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14408 && STRINGP (end->object)
14409 && row->continued_p)
14410 return 0;
14411 }
14412 /* A truncated row may not include PT among its character positions.
14413 Setting the cursor inside the scroll margin will trigger
14414 recalculation of hscroll in hscroll_window_tree. But if a
14415 display string covers point, defer to the string-handling
14416 code below to figure this out. */
14417 else if (row->truncated_on_left_p && pt_old < bpos_min)
14418 {
14419 cursor = glyph_before;
14420 x = -1;
14421 }
14422 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14423 /* Zero-width characters produce no glyphs. */
14424 || (!empty_line_p
14425 && (row->reversed_p
14426 ? glyph_after > glyphs_end
14427 : glyph_after < glyphs_end)))
14428 {
14429 cursor = glyph_after;
14430 x = -1;
14431 }
14432 }
14433
14434 compute_x:
14435 if (cursor != NULL)
14436 glyph = cursor;
14437 else if (glyph == glyphs_end
14438 && pos_before == pos_after
14439 && STRINGP ((row->reversed_p
14440 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14441 : row->glyphs[TEXT_AREA])->object))
14442 {
14443 /* If all the glyphs of this row came from strings, put the
14444 cursor on the first glyph of the row. This avoids having the
14445 cursor outside of the text area in this very rare and hard
14446 use case. */
14447 glyph =
14448 row->reversed_p
14449 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14450 : row->glyphs[TEXT_AREA];
14451 }
14452 if (x < 0)
14453 {
14454 struct glyph *g;
14455
14456 /* Need to compute x that corresponds to GLYPH. */
14457 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14458 {
14459 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14460 emacs_abort ();
14461 x += g->pixel_width;
14462 }
14463 }
14464
14465 /* ROW could be part of a continued line, which, under bidi
14466 reordering, might have other rows whose start and end charpos
14467 occlude point. Only set w->cursor if we found a better
14468 approximation to the cursor position than we have from previously
14469 examined candidate rows belonging to the same continued line. */
14470 if (/* we already have a candidate row */
14471 w->cursor.vpos >= 0
14472 /* that candidate is not the row we are processing */
14473 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14474 /* Make sure cursor.vpos specifies a row whose start and end
14475 charpos occlude point, and it is valid candidate for being a
14476 cursor-row. This is because some callers of this function
14477 leave cursor.vpos at the row where the cursor was displayed
14478 during the last redisplay cycle. */
14479 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14480 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14481 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14482 {
14483 struct glyph *g1 =
14484 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14485
14486 /* Don't consider glyphs that are outside TEXT_AREA. */
14487 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14488 return 0;
14489 /* Keep the candidate whose buffer position is the closest to
14490 point or has the `cursor' property. */
14491 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14492 w->cursor.hpos >= 0
14493 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14494 && ((BUFFERP (g1->object)
14495 && (g1->charpos == pt_old /* an exact match always wins */
14496 || (BUFFERP (glyph->object)
14497 && eabs (g1->charpos - pt_old)
14498 < eabs (glyph->charpos - pt_old))))
14499 /* previous candidate is a glyph from a string that has
14500 a non-nil `cursor' property */
14501 || (STRINGP (g1->object)
14502 && (!NILP (Fget_char_property (make_number (g1->charpos),
14503 Qcursor, g1->object))
14504 /* previous candidate is from the same display
14505 string as this one, and the display string
14506 came from a text property */
14507 || (EQ (g1->object, glyph->object)
14508 && string_from_text_prop)
14509 /* this candidate is from newline and its
14510 position is not an exact match */
14511 || (INTEGERP (glyph->object)
14512 && glyph->charpos != pt_old)))))
14513 return 0;
14514 /* If this candidate gives an exact match, use that. */
14515 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14516 /* If this candidate is a glyph created for the
14517 terminating newline of a line, and point is on that
14518 newline, it wins because it's an exact match. */
14519 || (!row->continued_p
14520 && INTEGERP (glyph->object)
14521 && glyph->charpos == 0
14522 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14523 /* Otherwise, keep the candidate that comes from a row
14524 spanning less buffer positions. This may win when one or
14525 both candidate positions are on glyphs that came from
14526 display strings, for which we cannot compare buffer
14527 positions. */
14528 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14529 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14530 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14531 return 0;
14532 }
14533 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14534 w->cursor.x = x;
14535 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14536 w->cursor.y = row->y + dy;
14537
14538 if (w == XWINDOW (selected_window))
14539 {
14540 if (!row->continued_p
14541 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14542 && row->x == 0)
14543 {
14544 this_line_buffer = XBUFFER (w->contents);
14545
14546 CHARPOS (this_line_start_pos)
14547 = MATRIX_ROW_START_CHARPOS (row) + delta;
14548 BYTEPOS (this_line_start_pos)
14549 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14550
14551 CHARPOS (this_line_end_pos)
14552 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14553 BYTEPOS (this_line_end_pos)
14554 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14555
14556 this_line_y = w->cursor.y;
14557 this_line_pixel_height = row->height;
14558 this_line_vpos = w->cursor.vpos;
14559 this_line_start_x = row->x;
14560 }
14561 else
14562 CHARPOS (this_line_start_pos) = 0;
14563 }
14564
14565 return 1;
14566 }
14567
14568
14569 /* Run window scroll functions, if any, for WINDOW with new window
14570 start STARTP. Sets the window start of WINDOW to that position.
14571
14572 We assume that the window's buffer is really current. */
14573
14574 static struct text_pos
14575 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14576 {
14577 struct window *w = XWINDOW (window);
14578 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14579
14580 eassert (current_buffer == XBUFFER (w->contents));
14581
14582 if (!NILP (Vwindow_scroll_functions))
14583 {
14584 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14585 make_number (CHARPOS (startp)));
14586 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14587 /* In case the hook functions switch buffers. */
14588 set_buffer_internal (XBUFFER (w->contents));
14589 }
14590
14591 return startp;
14592 }
14593
14594
14595 /* Make sure the line containing the cursor is fully visible.
14596 A value of 1 means there is nothing to be done.
14597 (Either the line is fully visible, or it cannot be made so,
14598 or we cannot tell.)
14599
14600 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14601 is higher than window.
14602
14603 A value of 0 means the caller should do scrolling
14604 as if point had gone off the screen. */
14605
14606 static int
14607 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14608 {
14609 struct glyph_matrix *matrix;
14610 struct glyph_row *row;
14611 int window_height;
14612
14613 if (!make_cursor_line_fully_visible_p)
14614 return 1;
14615
14616 /* It's not always possible to find the cursor, e.g, when a window
14617 is full of overlay strings. Don't do anything in that case. */
14618 if (w->cursor.vpos < 0)
14619 return 1;
14620
14621 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14622 row = MATRIX_ROW (matrix, w->cursor.vpos);
14623
14624 /* If the cursor row is not partially visible, there's nothing to do. */
14625 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14626 return 1;
14627
14628 /* If the row the cursor is in is taller than the window's height,
14629 it's not clear what to do, so do nothing. */
14630 window_height = window_box_height (w);
14631 if (row->height >= window_height)
14632 {
14633 if (!force_p || MINI_WINDOW_P (w)
14634 || w->vscroll || w->cursor.vpos == 0)
14635 return 1;
14636 }
14637 return 0;
14638 }
14639
14640
14641 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14642 non-zero means only WINDOW is redisplayed in redisplay_internal.
14643 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14644 in redisplay_window to bring a partially visible line into view in
14645 the case that only the cursor has moved.
14646
14647 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14648 last screen line's vertical height extends past the end of the screen.
14649
14650 Value is
14651
14652 1 if scrolling succeeded
14653
14654 0 if scrolling didn't find point.
14655
14656 -1 if new fonts have been loaded so that we must interrupt
14657 redisplay, adjust glyph matrices, and try again. */
14658
14659 enum
14660 {
14661 SCROLLING_SUCCESS,
14662 SCROLLING_FAILED,
14663 SCROLLING_NEED_LARGER_MATRICES
14664 };
14665
14666 /* If scroll-conservatively is more than this, never recenter.
14667
14668 If you change this, don't forget to update the doc string of
14669 `scroll-conservatively' and the Emacs manual. */
14670 #define SCROLL_LIMIT 100
14671
14672 static int
14673 try_scrolling (Lisp_Object window, int just_this_one_p,
14674 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14675 int temp_scroll_step, int last_line_misfit)
14676 {
14677 struct window *w = XWINDOW (window);
14678 struct frame *f = XFRAME (w->frame);
14679 struct text_pos pos, startp;
14680 struct it it;
14681 int this_scroll_margin, scroll_max, rc, height;
14682 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14683 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14684 Lisp_Object aggressive;
14685 /* We will never try scrolling more than this number of lines. */
14686 int scroll_limit = SCROLL_LIMIT;
14687 int frame_line_height = default_line_pixel_height (w);
14688 int window_total_lines
14689 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14690
14691 #ifdef GLYPH_DEBUG
14692 debug_method_add (w, "try_scrolling");
14693 #endif
14694
14695 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14696
14697 /* Compute scroll margin height in pixels. We scroll when point is
14698 within this distance from the top or bottom of the window. */
14699 if (scroll_margin > 0)
14700 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14701 * frame_line_height;
14702 else
14703 this_scroll_margin = 0;
14704
14705 /* Force arg_scroll_conservatively to have a reasonable value, to
14706 avoid scrolling too far away with slow move_it_* functions. Note
14707 that the user can supply scroll-conservatively equal to
14708 `most-positive-fixnum', which can be larger than INT_MAX. */
14709 if (arg_scroll_conservatively > scroll_limit)
14710 {
14711 arg_scroll_conservatively = scroll_limit + 1;
14712 scroll_max = scroll_limit * frame_line_height;
14713 }
14714 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14715 /* Compute how much we should try to scroll maximally to bring
14716 point into view. */
14717 scroll_max = (max (scroll_step,
14718 max (arg_scroll_conservatively, temp_scroll_step))
14719 * frame_line_height);
14720 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14721 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14722 /* We're trying to scroll because of aggressive scrolling but no
14723 scroll_step is set. Choose an arbitrary one. */
14724 scroll_max = 10 * frame_line_height;
14725 else
14726 scroll_max = 0;
14727
14728 too_near_end:
14729
14730 /* Decide whether to scroll down. */
14731 if (PT > CHARPOS (startp))
14732 {
14733 int scroll_margin_y;
14734
14735 /* Compute the pixel ypos of the scroll margin, then move IT to
14736 either that ypos or PT, whichever comes first. */
14737 start_display (&it, w, startp);
14738 scroll_margin_y = it.last_visible_y - this_scroll_margin
14739 - frame_line_height * extra_scroll_margin_lines;
14740 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14741 (MOVE_TO_POS | MOVE_TO_Y));
14742
14743 if (PT > CHARPOS (it.current.pos))
14744 {
14745 int y0 = line_bottom_y (&it);
14746 /* Compute how many pixels below window bottom to stop searching
14747 for PT. This avoids costly search for PT that is far away if
14748 the user limited scrolling by a small number of lines, but
14749 always finds PT if scroll_conservatively is set to a large
14750 number, such as most-positive-fixnum. */
14751 int slack = max (scroll_max, 10 * frame_line_height);
14752 int y_to_move = it.last_visible_y + slack;
14753
14754 /* Compute the distance from the scroll margin to PT or to
14755 the scroll limit, whichever comes first. This should
14756 include the height of the cursor line, to make that line
14757 fully visible. */
14758 move_it_to (&it, PT, -1, y_to_move,
14759 -1, MOVE_TO_POS | MOVE_TO_Y);
14760 dy = line_bottom_y (&it) - y0;
14761
14762 if (dy > scroll_max)
14763 return SCROLLING_FAILED;
14764
14765 if (dy > 0)
14766 scroll_down_p = 1;
14767 }
14768 }
14769
14770 if (scroll_down_p)
14771 {
14772 /* Point is in or below the bottom scroll margin, so move the
14773 window start down. If scrolling conservatively, move it just
14774 enough down to make point visible. If scroll_step is set,
14775 move it down by scroll_step. */
14776 if (arg_scroll_conservatively)
14777 amount_to_scroll
14778 = min (max (dy, frame_line_height),
14779 frame_line_height * arg_scroll_conservatively);
14780 else if (scroll_step || temp_scroll_step)
14781 amount_to_scroll = scroll_max;
14782 else
14783 {
14784 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14785 height = WINDOW_BOX_TEXT_HEIGHT (w);
14786 if (NUMBERP (aggressive))
14787 {
14788 double float_amount = XFLOATINT (aggressive) * height;
14789 int aggressive_scroll = float_amount;
14790 if (aggressive_scroll == 0 && float_amount > 0)
14791 aggressive_scroll = 1;
14792 /* Don't let point enter the scroll margin near top of
14793 the window. This could happen if the value of
14794 scroll_up_aggressively is too large and there are
14795 non-zero margins, because scroll_up_aggressively
14796 means put point that fraction of window height
14797 _from_the_bottom_margin_. */
14798 if (aggressive_scroll + 2*this_scroll_margin > height)
14799 aggressive_scroll = height - 2*this_scroll_margin;
14800 amount_to_scroll = dy + aggressive_scroll;
14801 }
14802 }
14803
14804 if (amount_to_scroll <= 0)
14805 return SCROLLING_FAILED;
14806
14807 start_display (&it, w, startp);
14808 if (arg_scroll_conservatively <= scroll_limit)
14809 move_it_vertically (&it, amount_to_scroll);
14810 else
14811 {
14812 /* Extra precision for users who set scroll-conservatively
14813 to a large number: make sure the amount we scroll
14814 the window start is never less than amount_to_scroll,
14815 which was computed as distance from window bottom to
14816 point. This matters when lines at window top and lines
14817 below window bottom have different height. */
14818 struct it it1;
14819 void *it1data = NULL;
14820 /* We use a temporary it1 because line_bottom_y can modify
14821 its argument, if it moves one line down; see there. */
14822 int start_y;
14823
14824 SAVE_IT (it1, it, it1data);
14825 start_y = line_bottom_y (&it1);
14826 do {
14827 RESTORE_IT (&it, &it, it1data);
14828 move_it_by_lines (&it, 1);
14829 SAVE_IT (it1, it, it1data);
14830 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14831 }
14832
14833 /* If STARTP is unchanged, move it down another screen line. */
14834 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14835 move_it_by_lines (&it, 1);
14836 startp = it.current.pos;
14837 }
14838 else
14839 {
14840 struct text_pos scroll_margin_pos = startp;
14841 int y_offset = 0;
14842
14843 /* See if point is inside the scroll margin at the top of the
14844 window. */
14845 if (this_scroll_margin)
14846 {
14847 int y_start;
14848
14849 start_display (&it, w, startp);
14850 y_start = it.current_y;
14851 move_it_vertically (&it, this_scroll_margin);
14852 scroll_margin_pos = it.current.pos;
14853 /* If we didn't move enough before hitting ZV, request
14854 additional amount of scroll, to move point out of the
14855 scroll margin. */
14856 if (IT_CHARPOS (it) == ZV
14857 && it.current_y - y_start < this_scroll_margin)
14858 y_offset = this_scroll_margin - (it.current_y - y_start);
14859 }
14860
14861 if (PT < CHARPOS (scroll_margin_pos))
14862 {
14863 /* Point is in the scroll margin at the top of the window or
14864 above what is displayed in the window. */
14865 int y0, y_to_move;
14866
14867 /* Compute the vertical distance from PT to the scroll
14868 margin position. Move as far as scroll_max allows, or
14869 one screenful, or 10 screen lines, whichever is largest.
14870 Give up if distance is greater than scroll_max or if we
14871 didn't reach the scroll margin position. */
14872 SET_TEXT_POS (pos, PT, PT_BYTE);
14873 start_display (&it, w, pos);
14874 y0 = it.current_y;
14875 y_to_move = max (it.last_visible_y,
14876 max (scroll_max, 10 * frame_line_height));
14877 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14878 y_to_move, -1,
14879 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14880 dy = it.current_y - y0;
14881 if (dy > scroll_max
14882 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14883 return SCROLLING_FAILED;
14884
14885 /* Additional scroll for when ZV was too close to point. */
14886 dy += y_offset;
14887
14888 /* Compute new window start. */
14889 start_display (&it, w, startp);
14890
14891 if (arg_scroll_conservatively)
14892 amount_to_scroll = max (dy, frame_line_height *
14893 max (scroll_step, temp_scroll_step));
14894 else if (scroll_step || temp_scroll_step)
14895 amount_to_scroll = scroll_max;
14896 else
14897 {
14898 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14899 height = WINDOW_BOX_TEXT_HEIGHT (w);
14900 if (NUMBERP (aggressive))
14901 {
14902 double float_amount = XFLOATINT (aggressive) * height;
14903 int aggressive_scroll = float_amount;
14904 if (aggressive_scroll == 0 && float_amount > 0)
14905 aggressive_scroll = 1;
14906 /* Don't let point enter the scroll margin near
14907 bottom of the window, if the value of
14908 scroll_down_aggressively happens to be too
14909 large. */
14910 if (aggressive_scroll + 2*this_scroll_margin > height)
14911 aggressive_scroll = height - 2*this_scroll_margin;
14912 amount_to_scroll = dy + aggressive_scroll;
14913 }
14914 }
14915
14916 if (amount_to_scroll <= 0)
14917 return SCROLLING_FAILED;
14918
14919 move_it_vertically_backward (&it, amount_to_scroll);
14920 startp = it.current.pos;
14921 }
14922 }
14923
14924 /* Run window scroll functions. */
14925 startp = run_window_scroll_functions (window, startp);
14926
14927 /* Display the window. Give up if new fonts are loaded, or if point
14928 doesn't appear. */
14929 if (!try_window (window, startp, 0))
14930 rc = SCROLLING_NEED_LARGER_MATRICES;
14931 else if (w->cursor.vpos < 0)
14932 {
14933 clear_glyph_matrix (w->desired_matrix);
14934 rc = SCROLLING_FAILED;
14935 }
14936 else
14937 {
14938 /* Maybe forget recorded base line for line number display. */
14939 if (!just_this_one_p
14940 || current_buffer->clip_changed
14941 || BEG_UNCHANGED < CHARPOS (startp))
14942 w->base_line_number = 0;
14943
14944 /* If cursor ends up on a partially visible line,
14945 treat that as being off the bottom of the screen. */
14946 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14947 /* It's possible that the cursor is on the first line of the
14948 buffer, which is partially obscured due to a vscroll
14949 (Bug#7537). In that case, avoid looping forever . */
14950 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14951 {
14952 clear_glyph_matrix (w->desired_matrix);
14953 ++extra_scroll_margin_lines;
14954 goto too_near_end;
14955 }
14956 rc = SCROLLING_SUCCESS;
14957 }
14958
14959 return rc;
14960 }
14961
14962
14963 /* Compute a suitable window start for window W if display of W starts
14964 on a continuation line. Value is non-zero if a new window start
14965 was computed.
14966
14967 The new window start will be computed, based on W's width, starting
14968 from the start of the continued line. It is the start of the
14969 screen line with the minimum distance from the old start W->start. */
14970
14971 static int
14972 compute_window_start_on_continuation_line (struct window *w)
14973 {
14974 struct text_pos pos, start_pos;
14975 int window_start_changed_p = 0;
14976
14977 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14978
14979 /* If window start is on a continuation line... Window start may be
14980 < BEGV in case there's invisible text at the start of the
14981 buffer (M-x rmail, for example). */
14982 if (CHARPOS (start_pos) > BEGV
14983 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14984 {
14985 struct it it;
14986 struct glyph_row *row;
14987
14988 /* Handle the case that the window start is out of range. */
14989 if (CHARPOS (start_pos) < BEGV)
14990 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14991 else if (CHARPOS (start_pos) > ZV)
14992 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14993
14994 /* Find the start of the continued line. This should be fast
14995 because find_newline is fast (newline cache). */
14996 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14997 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14998 row, DEFAULT_FACE_ID);
14999 reseat_at_previous_visible_line_start (&it);
15000
15001 /* If the line start is "too far" away from the window start,
15002 say it takes too much time to compute a new window start. */
15003 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15004 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15005 {
15006 int min_distance, distance;
15007
15008 /* Move forward by display lines to find the new window
15009 start. If window width was enlarged, the new start can
15010 be expected to be > the old start. If window width was
15011 decreased, the new window start will be < the old start.
15012 So, we're looking for the display line start with the
15013 minimum distance from the old window start. */
15014 pos = it.current.pos;
15015 min_distance = INFINITY;
15016 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15017 distance < min_distance)
15018 {
15019 min_distance = distance;
15020 pos = it.current.pos;
15021 if (it.line_wrap == WORD_WRAP)
15022 {
15023 /* Under WORD_WRAP, move_it_by_lines is likely to
15024 overshoot and stop not at the first, but the
15025 second character from the left margin. So in
15026 that case, we need a more tight control on the X
15027 coordinate of the iterator than move_it_by_lines
15028 promises in its contract. The method is to first
15029 go to the last (rightmost) visible character of a
15030 line, then move to the leftmost character on the
15031 next line in a separate call. */
15032 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15033 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15034 move_it_to (&it, ZV, 0,
15035 it.current_y + it.max_ascent + it.max_descent, -1,
15036 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15037 }
15038 else
15039 move_it_by_lines (&it, 1);
15040 }
15041
15042 /* Set the window start there. */
15043 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15044 window_start_changed_p = 1;
15045 }
15046 }
15047
15048 return window_start_changed_p;
15049 }
15050
15051
15052 /* Try cursor movement in case text has not changed in window WINDOW,
15053 with window start STARTP. Value is
15054
15055 CURSOR_MOVEMENT_SUCCESS if successful
15056
15057 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15058
15059 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15060 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15061 we want to scroll as if scroll-step were set to 1. See the code.
15062
15063 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15064 which case we have to abort this redisplay, and adjust matrices
15065 first. */
15066
15067 enum
15068 {
15069 CURSOR_MOVEMENT_SUCCESS,
15070 CURSOR_MOVEMENT_CANNOT_BE_USED,
15071 CURSOR_MOVEMENT_MUST_SCROLL,
15072 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15073 };
15074
15075 static int
15076 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15077 {
15078 struct window *w = XWINDOW (window);
15079 struct frame *f = XFRAME (w->frame);
15080 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15081
15082 #ifdef GLYPH_DEBUG
15083 if (inhibit_try_cursor_movement)
15084 return rc;
15085 #endif
15086
15087 /* Previously, there was a check for Lisp integer in the
15088 if-statement below. Now, this field is converted to
15089 ptrdiff_t, thus zero means invalid position in a buffer. */
15090 eassert (w->last_point > 0);
15091 /* Likewise there was a check whether window_end_vpos is nil or larger
15092 than the window. Now window_end_vpos is int and so never nil, but
15093 let's leave eassert to check whether it fits in the window. */
15094 eassert (w->window_end_vpos < w->current_matrix->nrows);
15095
15096 /* Handle case where text has not changed, only point, and it has
15097 not moved off the frame. */
15098 if (/* Point may be in this window. */
15099 PT >= CHARPOS (startp)
15100 /* Selective display hasn't changed. */
15101 && !current_buffer->clip_changed
15102 /* Function force-mode-line-update is used to force a thorough
15103 redisplay. It sets either windows_or_buffers_changed or
15104 update_mode_lines. So don't take a shortcut here for these
15105 cases. */
15106 && !update_mode_lines
15107 && !windows_or_buffers_changed
15108 && !cursor_type_changed
15109 /* Can't use this case if highlighting a region. When a
15110 region exists, cursor movement has to do more than just
15111 set the cursor. */
15112 && markpos_of_region () < 0
15113 && !w->region_showing
15114 && NILP (Vshow_trailing_whitespace)
15115 /* This code is not used for mini-buffer for the sake of the case
15116 of redisplaying to replace an echo area message; since in
15117 that case the mini-buffer contents per se are usually
15118 unchanged. This code is of no real use in the mini-buffer
15119 since the handling of this_line_start_pos, etc., in redisplay
15120 handles the same cases. */
15121 && !EQ (window, minibuf_window)
15122 && (FRAME_WINDOW_P (f)
15123 || !overlay_arrow_in_current_buffer_p ()))
15124 {
15125 int this_scroll_margin, top_scroll_margin;
15126 struct glyph_row *row = NULL;
15127 int frame_line_height = default_line_pixel_height (w);
15128 int window_total_lines
15129 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15130
15131 #ifdef GLYPH_DEBUG
15132 debug_method_add (w, "cursor movement");
15133 #endif
15134
15135 /* Scroll if point within this distance from the top or bottom
15136 of the window. This is a pixel value. */
15137 if (scroll_margin > 0)
15138 {
15139 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15140 this_scroll_margin *= frame_line_height;
15141 }
15142 else
15143 this_scroll_margin = 0;
15144
15145 top_scroll_margin = this_scroll_margin;
15146 if (WINDOW_WANTS_HEADER_LINE_P (w))
15147 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15148
15149 /* Start with the row the cursor was displayed during the last
15150 not paused redisplay. Give up if that row is not valid. */
15151 if (w->last_cursor_vpos < 0
15152 || w->last_cursor_vpos >= w->current_matrix->nrows)
15153 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15154 else
15155 {
15156 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15157 if (row->mode_line_p)
15158 ++row;
15159 if (!row->enabled_p)
15160 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15161 }
15162
15163 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15164 {
15165 int scroll_p = 0, must_scroll = 0;
15166 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15167
15168 if (PT > w->last_point)
15169 {
15170 /* Point has moved forward. */
15171 while (MATRIX_ROW_END_CHARPOS (row) < PT
15172 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15173 {
15174 eassert (row->enabled_p);
15175 ++row;
15176 }
15177
15178 /* If the end position of a row equals the start
15179 position of the next row, and PT is at that position,
15180 we would rather display cursor in the next line. */
15181 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15182 && MATRIX_ROW_END_CHARPOS (row) == PT
15183 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15184 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15185 && !cursor_row_p (row))
15186 ++row;
15187
15188 /* If within the scroll margin, scroll. Note that
15189 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15190 the next line would be drawn, and that
15191 this_scroll_margin can be zero. */
15192 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15193 || PT > MATRIX_ROW_END_CHARPOS (row)
15194 /* Line is completely visible last line in window
15195 and PT is to be set in the next line. */
15196 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15197 && PT == MATRIX_ROW_END_CHARPOS (row)
15198 && !row->ends_at_zv_p
15199 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15200 scroll_p = 1;
15201 }
15202 else if (PT < w->last_point)
15203 {
15204 /* Cursor has to be moved backward. Note that PT >=
15205 CHARPOS (startp) because of the outer if-statement. */
15206 while (!row->mode_line_p
15207 && (MATRIX_ROW_START_CHARPOS (row) > PT
15208 || (MATRIX_ROW_START_CHARPOS (row) == PT
15209 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15210 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15211 row > w->current_matrix->rows
15212 && (row-1)->ends_in_newline_from_string_p))))
15213 && (row->y > top_scroll_margin
15214 || CHARPOS (startp) == BEGV))
15215 {
15216 eassert (row->enabled_p);
15217 --row;
15218 }
15219
15220 /* Consider the following case: Window starts at BEGV,
15221 there is invisible, intangible text at BEGV, so that
15222 display starts at some point START > BEGV. It can
15223 happen that we are called with PT somewhere between
15224 BEGV and START. Try to handle that case. */
15225 if (row < w->current_matrix->rows
15226 || row->mode_line_p)
15227 {
15228 row = w->current_matrix->rows;
15229 if (row->mode_line_p)
15230 ++row;
15231 }
15232
15233 /* Due to newlines in overlay strings, we may have to
15234 skip forward over overlay strings. */
15235 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15236 && MATRIX_ROW_END_CHARPOS (row) == PT
15237 && !cursor_row_p (row))
15238 ++row;
15239
15240 /* If within the scroll margin, scroll. */
15241 if (row->y < top_scroll_margin
15242 && CHARPOS (startp) != BEGV)
15243 scroll_p = 1;
15244 }
15245 else
15246 {
15247 /* Cursor did not move. So don't scroll even if cursor line
15248 is partially visible, as it was so before. */
15249 rc = CURSOR_MOVEMENT_SUCCESS;
15250 }
15251
15252 if (PT < MATRIX_ROW_START_CHARPOS (row)
15253 || PT > MATRIX_ROW_END_CHARPOS (row))
15254 {
15255 /* if PT is not in the glyph row, give up. */
15256 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15257 must_scroll = 1;
15258 }
15259 else if (rc != CURSOR_MOVEMENT_SUCCESS
15260 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15261 {
15262 struct glyph_row *row1;
15263
15264 /* If rows are bidi-reordered and point moved, back up
15265 until we find a row that does not belong to a
15266 continuation line. This is because we must consider
15267 all rows of a continued line as candidates for the
15268 new cursor positioning, since row start and end
15269 positions change non-linearly with vertical position
15270 in such rows. */
15271 /* FIXME: Revisit this when glyph ``spilling'' in
15272 continuation lines' rows is implemented for
15273 bidi-reordered rows. */
15274 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15275 MATRIX_ROW_CONTINUATION_LINE_P (row);
15276 --row)
15277 {
15278 /* If we hit the beginning of the displayed portion
15279 without finding the first row of a continued
15280 line, give up. */
15281 if (row <= row1)
15282 {
15283 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15284 break;
15285 }
15286 eassert (row->enabled_p);
15287 }
15288 }
15289 if (must_scroll)
15290 ;
15291 else if (rc != CURSOR_MOVEMENT_SUCCESS
15292 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15293 /* Make sure this isn't a header line by any chance, since
15294 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15295 && !row->mode_line_p
15296 && make_cursor_line_fully_visible_p)
15297 {
15298 if (PT == MATRIX_ROW_END_CHARPOS (row)
15299 && !row->ends_at_zv_p
15300 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15301 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15302 else if (row->height > window_box_height (w))
15303 {
15304 /* If we end up in a partially visible line, let's
15305 make it fully visible, except when it's taller
15306 than the window, in which case we can't do much
15307 about it. */
15308 *scroll_step = 1;
15309 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15310 }
15311 else
15312 {
15313 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15314 if (!cursor_row_fully_visible_p (w, 0, 1))
15315 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15316 else
15317 rc = CURSOR_MOVEMENT_SUCCESS;
15318 }
15319 }
15320 else if (scroll_p)
15321 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15322 else if (rc != CURSOR_MOVEMENT_SUCCESS
15323 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15324 {
15325 /* With bidi-reordered rows, there could be more than
15326 one candidate row whose start and end positions
15327 occlude point. We need to let set_cursor_from_row
15328 find the best candidate. */
15329 /* FIXME: Revisit this when glyph ``spilling'' in
15330 continuation lines' rows is implemented for
15331 bidi-reordered rows. */
15332 int rv = 0;
15333
15334 do
15335 {
15336 int at_zv_p = 0, exact_match_p = 0;
15337
15338 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15339 && PT <= MATRIX_ROW_END_CHARPOS (row)
15340 && cursor_row_p (row))
15341 rv |= set_cursor_from_row (w, row, w->current_matrix,
15342 0, 0, 0, 0);
15343 /* As soon as we've found the exact match for point,
15344 or the first suitable row whose ends_at_zv_p flag
15345 is set, we are done. */
15346 at_zv_p =
15347 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15348 if (rv && !at_zv_p
15349 && w->cursor.hpos >= 0
15350 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15351 w->cursor.vpos))
15352 {
15353 struct glyph_row *candidate =
15354 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15355 struct glyph *g =
15356 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15357 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15358
15359 exact_match_p =
15360 (BUFFERP (g->object) && g->charpos == PT)
15361 || (INTEGERP (g->object)
15362 && (g->charpos == PT
15363 || (g->charpos == 0 && endpos - 1 == PT)));
15364 }
15365 if (rv && (at_zv_p || exact_match_p))
15366 {
15367 rc = CURSOR_MOVEMENT_SUCCESS;
15368 break;
15369 }
15370 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15371 break;
15372 ++row;
15373 }
15374 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15375 || row->continued_p)
15376 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15377 || (MATRIX_ROW_START_CHARPOS (row) == PT
15378 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15379 /* If we didn't find any candidate rows, or exited the
15380 loop before all the candidates were examined, signal
15381 to the caller that this method failed. */
15382 if (rc != CURSOR_MOVEMENT_SUCCESS
15383 && !(rv
15384 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15385 && !row->continued_p))
15386 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15387 else if (rv)
15388 rc = CURSOR_MOVEMENT_SUCCESS;
15389 }
15390 else
15391 {
15392 do
15393 {
15394 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15395 {
15396 rc = CURSOR_MOVEMENT_SUCCESS;
15397 break;
15398 }
15399 ++row;
15400 }
15401 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15402 && MATRIX_ROW_START_CHARPOS (row) == PT
15403 && cursor_row_p (row));
15404 }
15405 }
15406 }
15407
15408 return rc;
15409 }
15410
15411 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15412 static
15413 #endif
15414 void
15415 set_vertical_scroll_bar (struct window *w)
15416 {
15417 ptrdiff_t start, end, whole;
15418
15419 /* Calculate the start and end positions for the current window.
15420 At some point, it would be nice to choose between scrollbars
15421 which reflect the whole buffer size, with special markers
15422 indicating narrowing, and scrollbars which reflect only the
15423 visible region.
15424
15425 Note that mini-buffers sometimes aren't displaying any text. */
15426 if (!MINI_WINDOW_P (w)
15427 || (w == XWINDOW (minibuf_window)
15428 && NILP (echo_area_buffer[0])))
15429 {
15430 struct buffer *buf = XBUFFER (w->contents);
15431 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15432 start = marker_position (w->start) - BUF_BEGV (buf);
15433 /* I don't think this is guaranteed to be right. For the
15434 moment, we'll pretend it is. */
15435 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15436
15437 if (end < start)
15438 end = start;
15439 if (whole < (end - start))
15440 whole = end - start;
15441 }
15442 else
15443 start = end = whole = 0;
15444
15445 /* Indicate what this scroll bar ought to be displaying now. */
15446 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15447 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15448 (w, end - start, whole, start);
15449 }
15450
15451
15452 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15453 selected_window is redisplayed.
15454
15455 We can return without actually redisplaying the window if
15456 fonts_changed_p. In that case, redisplay_internal will
15457 retry. */
15458
15459 static void
15460 redisplay_window (Lisp_Object window, int just_this_one_p)
15461 {
15462 struct window *w = XWINDOW (window);
15463 struct frame *f = XFRAME (w->frame);
15464 struct buffer *buffer = XBUFFER (w->contents);
15465 struct buffer *old = current_buffer;
15466 struct text_pos lpoint, opoint, startp;
15467 int update_mode_line;
15468 int tem;
15469 struct it it;
15470 /* Record it now because it's overwritten. */
15471 int current_matrix_up_to_date_p = 0;
15472 int used_current_matrix_p = 0;
15473 /* This is less strict than current_matrix_up_to_date_p.
15474 It indicates that the buffer contents and narrowing are unchanged. */
15475 int buffer_unchanged_p = 0;
15476 int temp_scroll_step = 0;
15477 ptrdiff_t count = SPECPDL_INDEX ();
15478 int rc;
15479 int centering_position = -1;
15480 int last_line_misfit = 0;
15481 ptrdiff_t beg_unchanged, end_unchanged;
15482 int frame_line_height;
15483
15484 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15485 opoint = lpoint;
15486
15487 #ifdef GLYPH_DEBUG
15488 *w->desired_matrix->method = 0;
15489 #endif
15490
15491 /* Make sure that both W's markers are valid. */
15492 eassert (XMARKER (w->start)->buffer == buffer);
15493 eassert (XMARKER (w->pointm)->buffer == buffer);
15494
15495 restart:
15496 reconsider_clip_changes (w);
15497 frame_line_height = default_line_pixel_height (w);
15498
15499 /* Has the mode line to be updated? */
15500 update_mode_line = (w->update_mode_line
15501 || update_mode_lines
15502 || buffer->clip_changed
15503 || buffer->prevent_redisplay_optimizations_p);
15504
15505 if (MINI_WINDOW_P (w))
15506 {
15507 if (w == XWINDOW (echo_area_window)
15508 && !NILP (echo_area_buffer[0]))
15509 {
15510 if (update_mode_line)
15511 /* We may have to update a tty frame's menu bar or a
15512 tool-bar. Example `M-x C-h C-h C-g'. */
15513 goto finish_menu_bars;
15514 else
15515 /* We've already displayed the echo area glyphs in this window. */
15516 goto finish_scroll_bars;
15517 }
15518 else if ((w != XWINDOW (minibuf_window)
15519 || minibuf_level == 0)
15520 /* When buffer is nonempty, redisplay window normally. */
15521 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15522 /* Quail displays non-mini buffers in minibuffer window.
15523 In that case, redisplay the window normally. */
15524 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15525 {
15526 /* W is a mini-buffer window, but it's not active, so clear
15527 it. */
15528 int yb = window_text_bottom_y (w);
15529 struct glyph_row *row;
15530 int y;
15531
15532 for (y = 0, row = w->desired_matrix->rows;
15533 y < yb;
15534 y += row->height, ++row)
15535 blank_row (w, row, y);
15536 goto finish_scroll_bars;
15537 }
15538
15539 clear_glyph_matrix (w->desired_matrix);
15540 }
15541
15542 /* Otherwise set up data on this window; select its buffer and point
15543 value. */
15544 /* Really select the buffer, for the sake of buffer-local
15545 variables. */
15546 set_buffer_internal_1 (XBUFFER (w->contents));
15547
15548 current_matrix_up_to_date_p
15549 = (w->window_end_valid
15550 && !current_buffer->clip_changed
15551 && !current_buffer->prevent_redisplay_optimizations_p
15552 && !window_outdated (w));
15553
15554 /* Run the window-bottom-change-functions
15555 if it is possible that the text on the screen has changed
15556 (either due to modification of the text, or any other reason). */
15557 if (!current_matrix_up_to_date_p
15558 && !NILP (Vwindow_text_change_functions))
15559 {
15560 safe_run_hooks (Qwindow_text_change_functions);
15561 goto restart;
15562 }
15563
15564 beg_unchanged = BEG_UNCHANGED;
15565 end_unchanged = END_UNCHANGED;
15566
15567 SET_TEXT_POS (opoint, PT, PT_BYTE);
15568
15569 specbind (Qinhibit_point_motion_hooks, Qt);
15570
15571 buffer_unchanged_p
15572 = (w->window_end_valid
15573 && !current_buffer->clip_changed
15574 && !window_outdated (w));
15575
15576 /* When windows_or_buffers_changed is non-zero, we can't rely
15577 on the window end being valid, so set it to zero there. */
15578 if (windows_or_buffers_changed)
15579 {
15580 /* If window starts on a continuation line, maybe adjust the
15581 window start in case the window's width changed. */
15582 if (XMARKER (w->start)->buffer == current_buffer)
15583 compute_window_start_on_continuation_line (w);
15584
15585 w->window_end_valid = 0;
15586 /* If so, we also can't rely on current matrix
15587 and should not fool try_cursor_movement below. */
15588 current_matrix_up_to_date_p = 0;
15589 }
15590
15591 /* Some sanity checks. */
15592 CHECK_WINDOW_END (w);
15593 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15594 emacs_abort ();
15595 if (BYTEPOS (opoint) < CHARPOS (opoint))
15596 emacs_abort ();
15597
15598 if (mode_line_update_needed (w))
15599 update_mode_line = 1;
15600
15601 /* Point refers normally to the selected window. For any other
15602 window, set up appropriate value. */
15603 if (!EQ (window, selected_window))
15604 {
15605 ptrdiff_t new_pt = marker_position (w->pointm);
15606 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15607 if (new_pt < BEGV)
15608 {
15609 new_pt = BEGV;
15610 new_pt_byte = BEGV_BYTE;
15611 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15612 }
15613 else if (new_pt > (ZV - 1))
15614 {
15615 new_pt = ZV;
15616 new_pt_byte = ZV_BYTE;
15617 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15618 }
15619
15620 /* We don't use SET_PT so that the point-motion hooks don't run. */
15621 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15622 }
15623
15624 /* If any of the character widths specified in the display table
15625 have changed, invalidate the width run cache. It's true that
15626 this may be a bit late to catch such changes, but the rest of
15627 redisplay goes (non-fatally) haywire when the display table is
15628 changed, so why should we worry about doing any better? */
15629 if (current_buffer->width_run_cache)
15630 {
15631 struct Lisp_Char_Table *disptab = buffer_display_table ();
15632
15633 if (! disptab_matches_widthtab
15634 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15635 {
15636 invalidate_region_cache (current_buffer,
15637 current_buffer->width_run_cache,
15638 BEG, Z);
15639 recompute_width_table (current_buffer, disptab);
15640 }
15641 }
15642
15643 /* If window-start is screwed up, choose a new one. */
15644 if (XMARKER (w->start)->buffer != current_buffer)
15645 goto recenter;
15646
15647 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15648
15649 /* If someone specified a new starting point but did not insist,
15650 check whether it can be used. */
15651 if (w->optional_new_start
15652 && CHARPOS (startp) >= BEGV
15653 && CHARPOS (startp) <= ZV)
15654 {
15655 w->optional_new_start = 0;
15656 start_display (&it, w, startp);
15657 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15658 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15659 if (IT_CHARPOS (it) == PT)
15660 w->force_start = 1;
15661 /* IT may overshoot PT if text at PT is invisible. */
15662 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15663 w->force_start = 1;
15664 }
15665
15666 force_start:
15667
15668 /* Handle case where place to start displaying has been specified,
15669 unless the specified location is outside the accessible range. */
15670 if (w->force_start || window_frozen_p (w))
15671 {
15672 /* We set this later on if we have to adjust point. */
15673 int new_vpos = -1;
15674
15675 w->force_start = 0;
15676 w->vscroll = 0;
15677 w->window_end_valid = 0;
15678
15679 /* Forget any recorded base line for line number display. */
15680 if (!buffer_unchanged_p)
15681 w->base_line_number = 0;
15682
15683 /* Redisplay the mode line. Select the buffer properly for that.
15684 Also, run the hook window-scroll-functions
15685 because we have scrolled. */
15686 /* Note, we do this after clearing force_start because
15687 if there's an error, it is better to forget about force_start
15688 than to get into an infinite loop calling the hook functions
15689 and having them get more errors. */
15690 if (!update_mode_line
15691 || ! NILP (Vwindow_scroll_functions))
15692 {
15693 update_mode_line = 1;
15694 w->update_mode_line = 1;
15695 startp = run_window_scroll_functions (window, startp);
15696 }
15697
15698 if (CHARPOS (startp) < BEGV)
15699 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15700 else if (CHARPOS (startp) > ZV)
15701 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15702
15703 /* Redisplay, then check if cursor has been set during the
15704 redisplay. Give up if new fonts were loaded. */
15705 /* We used to issue a CHECK_MARGINS argument to try_window here,
15706 but this causes scrolling to fail when point begins inside
15707 the scroll margin (bug#148) -- cyd */
15708 if (!try_window (window, startp, 0))
15709 {
15710 w->force_start = 1;
15711 clear_glyph_matrix (w->desired_matrix);
15712 goto need_larger_matrices;
15713 }
15714
15715 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15716 {
15717 /* If point does not appear, try to move point so it does
15718 appear. The desired matrix has been built above, so we
15719 can use it here. */
15720 new_vpos = window_box_height (w) / 2;
15721 }
15722
15723 if (!cursor_row_fully_visible_p (w, 0, 0))
15724 {
15725 /* Point does appear, but on a line partly visible at end of window.
15726 Move it back to a fully-visible line. */
15727 new_vpos = window_box_height (w);
15728 }
15729 else if (w->cursor.vpos >=0)
15730 {
15731 /* Some people insist on not letting point enter the scroll
15732 margin, even though this part handles windows that didn't
15733 scroll at all. */
15734 int window_total_lines
15735 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15736 int margin = min (scroll_margin, window_total_lines / 4);
15737 int pixel_margin = margin * frame_line_height;
15738 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15739
15740 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15741 below, which finds the row to move point to, advances by
15742 the Y coordinate of the _next_ row, see the definition of
15743 MATRIX_ROW_BOTTOM_Y. */
15744 if (w->cursor.vpos < margin + header_line)
15745 {
15746 w->cursor.vpos = -1;
15747 clear_glyph_matrix (w->desired_matrix);
15748 goto try_to_scroll;
15749 }
15750 else
15751 {
15752 int window_height = window_box_height (w);
15753
15754 if (header_line)
15755 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15756 if (w->cursor.y >= window_height - pixel_margin)
15757 {
15758 w->cursor.vpos = -1;
15759 clear_glyph_matrix (w->desired_matrix);
15760 goto try_to_scroll;
15761 }
15762 }
15763 }
15764
15765 /* If we need to move point for either of the above reasons,
15766 now actually do it. */
15767 if (new_vpos >= 0)
15768 {
15769 struct glyph_row *row;
15770
15771 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15772 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15773 ++row;
15774
15775 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15776 MATRIX_ROW_START_BYTEPOS (row));
15777
15778 if (w != XWINDOW (selected_window))
15779 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15780 else if (current_buffer == old)
15781 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15782
15783 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15784
15785 /* If we are highlighting the region, then we just changed
15786 the region, so redisplay to show it. */
15787 if (markpos_of_region () >= 0)
15788 {
15789 clear_glyph_matrix (w->desired_matrix);
15790 if (!try_window (window, startp, 0))
15791 goto need_larger_matrices;
15792 }
15793 }
15794
15795 #ifdef GLYPH_DEBUG
15796 debug_method_add (w, "forced window start");
15797 #endif
15798 goto done;
15799 }
15800
15801 /* Handle case where text has not changed, only point, and it has
15802 not moved off the frame, and we are not retrying after hscroll.
15803 (current_matrix_up_to_date_p is nonzero when retrying.) */
15804 if (current_matrix_up_to_date_p
15805 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15806 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15807 {
15808 switch (rc)
15809 {
15810 case CURSOR_MOVEMENT_SUCCESS:
15811 used_current_matrix_p = 1;
15812 goto done;
15813
15814 case CURSOR_MOVEMENT_MUST_SCROLL:
15815 goto try_to_scroll;
15816
15817 default:
15818 emacs_abort ();
15819 }
15820 }
15821 /* If current starting point was originally the beginning of a line
15822 but no longer is, find a new starting point. */
15823 else if (w->start_at_line_beg
15824 && !(CHARPOS (startp) <= BEGV
15825 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15826 {
15827 #ifdef GLYPH_DEBUG
15828 debug_method_add (w, "recenter 1");
15829 #endif
15830 goto recenter;
15831 }
15832
15833 /* Try scrolling with try_window_id. Value is > 0 if update has
15834 been done, it is -1 if we know that the same window start will
15835 not work. It is 0 if unsuccessful for some other reason. */
15836 else if ((tem = try_window_id (w)) != 0)
15837 {
15838 #ifdef GLYPH_DEBUG
15839 debug_method_add (w, "try_window_id %d", tem);
15840 #endif
15841
15842 if (fonts_changed_p)
15843 goto need_larger_matrices;
15844 if (tem > 0)
15845 goto done;
15846
15847 /* Otherwise try_window_id has returned -1 which means that we
15848 don't want the alternative below this comment to execute. */
15849 }
15850 else if (CHARPOS (startp) >= BEGV
15851 && CHARPOS (startp) <= ZV
15852 && PT >= CHARPOS (startp)
15853 && (CHARPOS (startp) < ZV
15854 /* Avoid starting at end of buffer. */
15855 || CHARPOS (startp) == BEGV
15856 || !window_outdated (w)))
15857 {
15858 int d1, d2, d3, d4, d5, d6;
15859
15860 /* If first window line is a continuation line, and window start
15861 is inside the modified region, but the first change is before
15862 current window start, we must select a new window start.
15863
15864 However, if this is the result of a down-mouse event (e.g. by
15865 extending the mouse-drag-overlay), we don't want to select a
15866 new window start, since that would change the position under
15867 the mouse, resulting in an unwanted mouse-movement rather
15868 than a simple mouse-click. */
15869 if (!w->start_at_line_beg
15870 && NILP (do_mouse_tracking)
15871 && CHARPOS (startp) > BEGV
15872 && CHARPOS (startp) > BEG + beg_unchanged
15873 && CHARPOS (startp) <= Z - end_unchanged
15874 /* Even if w->start_at_line_beg is nil, a new window may
15875 start at a line_beg, since that's how set_buffer_window
15876 sets it. So, we need to check the return value of
15877 compute_window_start_on_continuation_line. (See also
15878 bug#197). */
15879 && XMARKER (w->start)->buffer == current_buffer
15880 && compute_window_start_on_continuation_line (w)
15881 /* It doesn't make sense to force the window start like we
15882 do at label force_start if it is already known that point
15883 will not be visible in the resulting window, because
15884 doing so will move point from its correct position
15885 instead of scrolling the window to bring point into view.
15886 See bug#9324. */
15887 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15888 {
15889 w->force_start = 1;
15890 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15891 goto force_start;
15892 }
15893
15894 #ifdef GLYPH_DEBUG
15895 debug_method_add (w, "same window start");
15896 #endif
15897
15898 /* Try to redisplay starting at same place as before.
15899 If point has not moved off frame, accept the results. */
15900 if (!current_matrix_up_to_date_p
15901 /* Don't use try_window_reusing_current_matrix in this case
15902 because a window scroll function can have changed the
15903 buffer. */
15904 || !NILP (Vwindow_scroll_functions)
15905 || MINI_WINDOW_P (w)
15906 || !(used_current_matrix_p
15907 = try_window_reusing_current_matrix (w)))
15908 {
15909 IF_DEBUG (debug_method_add (w, "1"));
15910 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15911 /* -1 means we need to scroll.
15912 0 means we need new matrices, but fonts_changed_p
15913 is set in that case, so we will detect it below. */
15914 goto try_to_scroll;
15915 }
15916
15917 if (fonts_changed_p)
15918 goto need_larger_matrices;
15919
15920 if (w->cursor.vpos >= 0)
15921 {
15922 if (!just_this_one_p
15923 || current_buffer->clip_changed
15924 || BEG_UNCHANGED < CHARPOS (startp))
15925 /* Forget any recorded base line for line number display. */
15926 w->base_line_number = 0;
15927
15928 if (!cursor_row_fully_visible_p (w, 1, 0))
15929 {
15930 clear_glyph_matrix (w->desired_matrix);
15931 last_line_misfit = 1;
15932 }
15933 /* Drop through and scroll. */
15934 else
15935 goto done;
15936 }
15937 else
15938 clear_glyph_matrix (w->desired_matrix);
15939 }
15940
15941 try_to_scroll:
15942
15943 /* Redisplay the mode line. Select the buffer properly for that. */
15944 if (!update_mode_line)
15945 {
15946 update_mode_line = 1;
15947 w->update_mode_line = 1;
15948 }
15949
15950 /* Try to scroll by specified few lines. */
15951 if ((scroll_conservatively
15952 || emacs_scroll_step
15953 || temp_scroll_step
15954 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15955 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15956 && CHARPOS (startp) >= BEGV
15957 && CHARPOS (startp) <= ZV)
15958 {
15959 /* The function returns -1 if new fonts were loaded, 1 if
15960 successful, 0 if not successful. */
15961 int ss = try_scrolling (window, just_this_one_p,
15962 scroll_conservatively,
15963 emacs_scroll_step,
15964 temp_scroll_step, last_line_misfit);
15965 switch (ss)
15966 {
15967 case SCROLLING_SUCCESS:
15968 goto done;
15969
15970 case SCROLLING_NEED_LARGER_MATRICES:
15971 goto need_larger_matrices;
15972
15973 case SCROLLING_FAILED:
15974 break;
15975
15976 default:
15977 emacs_abort ();
15978 }
15979 }
15980
15981 /* Finally, just choose a place to start which positions point
15982 according to user preferences. */
15983
15984 recenter:
15985
15986 #ifdef GLYPH_DEBUG
15987 debug_method_add (w, "recenter");
15988 #endif
15989
15990 /* Forget any previously recorded base line for line number display. */
15991 if (!buffer_unchanged_p)
15992 w->base_line_number = 0;
15993
15994 /* Determine the window start relative to point. */
15995 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15996 it.current_y = it.last_visible_y;
15997 if (centering_position < 0)
15998 {
15999 int window_total_lines
16000 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16001 int margin =
16002 scroll_margin > 0
16003 ? min (scroll_margin, window_total_lines / 4)
16004 : 0;
16005 ptrdiff_t margin_pos = CHARPOS (startp);
16006 Lisp_Object aggressive;
16007 int scrolling_up;
16008
16009 /* If there is a scroll margin at the top of the window, find
16010 its character position. */
16011 if (margin
16012 /* Cannot call start_display if startp is not in the
16013 accessible region of the buffer. This can happen when we
16014 have just switched to a different buffer and/or changed
16015 its restriction. In that case, startp is initialized to
16016 the character position 1 (BEGV) because we did not yet
16017 have chance to display the buffer even once. */
16018 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16019 {
16020 struct it it1;
16021 void *it1data = NULL;
16022
16023 SAVE_IT (it1, it, it1data);
16024 start_display (&it1, w, startp);
16025 move_it_vertically (&it1, margin * frame_line_height);
16026 margin_pos = IT_CHARPOS (it1);
16027 RESTORE_IT (&it, &it, it1data);
16028 }
16029 scrolling_up = PT > margin_pos;
16030 aggressive =
16031 scrolling_up
16032 ? BVAR (current_buffer, scroll_up_aggressively)
16033 : BVAR (current_buffer, scroll_down_aggressively);
16034
16035 if (!MINI_WINDOW_P (w)
16036 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16037 {
16038 int pt_offset = 0;
16039
16040 /* Setting scroll-conservatively overrides
16041 scroll-*-aggressively. */
16042 if (!scroll_conservatively && NUMBERP (aggressive))
16043 {
16044 double float_amount = XFLOATINT (aggressive);
16045
16046 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16047 if (pt_offset == 0 && float_amount > 0)
16048 pt_offset = 1;
16049 if (pt_offset && margin > 0)
16050 margin -= 1;
16051 }
16052 /* Compute how much to move the window start backward from
16053 point so that point will be displayed where the user
16054 wants it. */
16055 if (scrolling_up)
16056 {
16057 centering_position = it.last_visible_y;
16058 if (pt_offset)
16059 centering_position -= pt_offset;
16060 centering_position -=
16061 frame_line_height * (1 + margin + (last_line_misfit != 0))
16062 + WINDOW_HEADER_LINE_HEIGHT (w);
16063 /* Don't let point enter the scroll margin near top of
16064 the window. */
16065 if (centering_position < margin * frame_line_height)
16066 centering_position = margin * frame_line_height;
16067 }
16068 else
16069 centering_position = margin * frame_line_height + pt_offset;
16070 }
16071 else
16072 /* Set the window start half the height of the window backward
16073 from point. */
16074 centering_position = window_box_height (w) / 2;
16075 }
16076 move_it_vertically_backward (&it, centering_position);
16077
16078 eassert (IT_CHARPOS (it) >= BEGV);
16079
16080 /* The function move_it_vertically_backward may move over more
16081 than the specified y-distance. If it->w is small, e.g. a
16082 mini-buffer window, we may end up in front of the window's
16083 display area. Start displaying at the start of the line
16084 containing PT in this case. */
16085 if (it.current_y <= 0)
16086 {
16087 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16088 move_it_vertically_backward (&it, 0);
16089 it.current_y = 0;
16090 }
16091
16092 it.current_x = it.hpos = 0;
16093
16094 /* Set the window start position here explicitly, to avoid an
16095 infinite loop in case the functions in window-scroll-functions
16096 get errors. */
16097 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16098
16099 /* Run scroll hooks. */
16100 startp = run_window_scroll_functions (window, it.current.pos);
16101
16102 /* Redisplay the window. */
16103 if (!current_matrix_up_to_date_p
16104 || windows_or_buffers_changed
16105 || cursor_type_changed
16106 /* Don't use try_window_reusing_current_matrix in this case
16107 because it can have changed the buffer. */
16108 || !NILP (Vwindow_scroll_functions)
16109 || !just_this_one_p
16110 || MINI_WINDOW_P (w)
16111 || !(used_current_matrix_p
16112 = try_window_reusing_current_matrix (w)))
16113 try_window (window, startp, 0);
16114
16115 /* If new fonts have been loaded (due to fontsets), give up. We
16116 have to start a new redisplay since we need to re-adjust glyph
16117 matrices. */
16118 if (fonts_changed_p)
16119 goto need_larger_matrices;
16120
16121 /* If cursor did not appear assume that the middle of the window is
16122 in the first line of the window. Do it again with the next line.
16123 (Imagine a window of height 100, displaying two lines of height
16124 60. Moving back 50 from it->last_visible_y will end in the first
16125 line.) */
16126 if (w->cursor.vpos < 0)
16127 {
16128 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16129 {
16130 clear_glyph_matrix (w->desired_matrix);
16131 move_it_by_lines (&it, 1);
16132 try_window (window, it.current.pos, 0);
16133 }
16134 else if (PT < IT_CHARPOS (it))
16135 {
16136 clear_glyph_matrix (w->desired_matrix);
16137 move_it_by_lines (&it, -1);
16138 try_window (window, it.current.pos, 0);
16139 }
16140 else
16141 {
16142 /* Not much we can do about it. */
16143 }
16144 }
16145
16146 /* Consider the following case: Window starts at BEGV, there is
16147 invisible, intangible text at BEGV, so that display starts at
16148 some point START > BEGV. It can happen that we are called with
16149 PT somewhere between BEGV and START. Try to handle that case. */
16150 if (w->cursor.vpos < 0)
16151 {
16152 struct glyph_row *row = w->current_matrix->rows;
16153 if (row->mode_line_p)
16154 ++row;
16155 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16156 }
16157
16158 if (!cursor_row_fully_visible_p (w, 0, 0))
16159 {
16160 /* If vscroll is enabled, disable it and try again. */
16161 if (w->vscroll)
16162 {
16163 w->vscroll = 0;
16164 clear_glyph_matrix (w->desired_matrix);
16165 goto recenter;
16166 }
16167
16168 /* Users who set scroll-conservatively to a large number want
16169 point just above/below the scroll margin. If we ended up
16170 with point's row partially visible, move the window start to
16171 make that row fully visible and out of the margin. */
16172 if (scroll_conservatively > SCROLL_LIMIT)
16173 {
16174 int window_total_lines
16175 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16176 int margin =
16177 scroll_margin > 0
16178 ? min (scroll_margin, window_total_lines / 4)
16179 : 0;
16180 int move_down = w->cursor.vpos >= window_total_lines / 2;
16181
16182 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16183 clear_glyph_matrix (w->desired_matrix);
16184 if (1 == try_window (window, it.current.pos,
16185 TRY_WINDOW_CHECK_MARGINS))
16186 goto done;
16187 }
16188
16189 /* If centering point failed to make the whole line visible,
16190 put point at the top instead. That has to make the whole line
16191 visible, if it can be done. */
16192 if (centering_position == 0)
16193 goto done;
16194
16195 clear_glyph_matrix (w->desired_matrix);
16196 centering_position = 0;
16197 goto recenter;
16198 }
16199
16200 done:
16201
16202 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16203 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16204 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16205
16206 /* Display the mode line, if we must. */
16207 if ((update_mode_line
16208 /* If window not full width, must redo its mode line
16209 if (a) the window to its side is being redone and
16210 (b) we do a frame-based redisplay. This is a consequence
16211 of how inverted lines are drawn in frame-based redisplay. */
16212 || (!just_this_one_p
16213 && !FRAME_WINDOW_P (f)
16214 && !WINDOW_FULL_WIDTH_P (w))
16215 /* Line number to display. */
16216 || w->base_line_pos > 0
16217 /* Column number is displayed and different from the one displayed. */
16218 || (w->column_number_displayed != -1
16219 && (w->column_number_displayed != current_column ())))
16220 /* This means that the window has a mode line. */
16221 && (WINDOW_WANTS_MODELINE_P (w)
16222 || WINDOW_WANTS_HEADER_LINE_P (w)))
16223 {
16224 display_mode_lines (w);
16225
16226 /* If mode line height has changed, arrange for a thorough
16227 immediate redisplay using the correct mode line height. */
16228 if (WINDOW_WANTS_MODELINE_P (w)
16229 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16230 {
16231 fonts_changed_p = 1;
16232 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16233 = DESIRED_MODE_LINE_HEIGHT (w);
16234 }
16235
16236 /* If header line height has changed, arrange for a thorough
16237 immediate redisplay using the correct header line height. */
16238 if (WINDOW_WANTS_HEADER_LINE_P (w)
16239 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16240 {
16241 fonts_changed_p = 1;
16242 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16243 = DESIRED_HEADER_LINE_HEIGHT (w);
16244 }
16245
16246 if (fonts_changed_p)
16247 goto need_larger_matrices;
16248 }
16249
16250 if (!line_number_displayed && w->base_line_pos != -1)
16251 {
16252 w->base_line_pos = 0;
16253 w->base_line_number = 0;
16254 }
16255
16256 finish_menu_bars:
16257
16258 /* When we reach a frame's selected window, redo the frame's menu bar. */
16259 if (update_mode_line
16260 && EQ (FRAME_SELECTED_WINDOW (f), window))
16261 {
16262 int redisplay_menu_p = 0;
16263
16264 if (FRAME_WINDOW_P (f))
16265 {
16266 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16267 || defined (HAVE_NS) || defined (USE_GTK)
16268 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16269 #else
16270 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16271 #endif
16272 }
16273 else
16274 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16275
16276 if (redisplay_menu_p)
16277 display_menu_bar (w);
16278
16279 #ifdef HAVE_WINDOW_SYSTEM
16280 if (FRAME_WINDOW_P (f))
16281 {
16282 #if defined (USE_GTK) || defined (HAVE_NS)
16283 if (FRAME_EXTERNAL_TOOL_BAR (f))
16284 redisplay_tool_bar (f);
16285 #else
16286 if (WINDOWP (f->tool_bar_window)
16287 && (FRAME_TOOL_BAR_LINES (f) > 0
16288 || !NILP (Vauto_resize_tool_bars))
16289 && redisplay_tool_bar (f))
16290 ignore_mouse_drag_p = 1;
16291 #endif
16292 }
16293 #endif
16294 }
16295
16296 #ifdef HAVE_WINDOW_SYSTEM
16297 if (FRAME_WINDOW_P (f)
16298 && update_window_fringes (w, (just_this_one_p
16299 || (!used_current_matrix_p && !overlay_arrow_seen)
16300 || w->pseudo_window_p)))
16301 {
16302 update_begin (f);
16303 block_input ();
16304 if (draw_window_fringes (w, 1))
16305 x_draw_vertical_border (w);
16306 unblock_input ();
16307 update_end (f);
16308 }
16309 #endif /* HAVE_WINDOW_SYSTEM */
16310
16311 /* We go to this label, with fonts_changed_p set,
16312 if it is necessary to try again using larger glyph matrices.
16313 We have to redeem the scroll bar even in this case,
16314 because the loop in redisplay_internal expects that. */
16315 need_larger_matrices:
16316 ;
16317 finish_scroll_bars:
16318
16319 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16320 {
16321 /* Set the thumb's position and size. */
16322 set_vertical_scroll_bar (w);
16323
16324 /* Note that we actually used the scroll bar attached to this
16325 window, so it shouldn't be deleted at the end of redisplay. */
16326 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16327 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16328 }
16329
16330 /* Restore current_buffer and value of point in it. The window
16331 update may have changed the buffer, so first make sure `opoint'
16332 is still valid (Bug#6177). */
16333 if (CHARPOS (opoint) < BEGV)
16334 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16335 else if (CHARPOS (opoint) > ZV)
16336 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16337 else
16338 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16339
16340 set_buffer_internal_1 (old);
16341 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16342 shorter. This can be caused by log truncation in *Messages*. */
16343 if (CHARPOS (lpoint) <= ZV)
16344 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16345
16346 unbind_to (count, Qnil);
16347 }
16348
16349
16350 /* Build the complete desired matrix of WINDOW with a window start
16351 buffer position POS.
16352
16353 Value is 1 if successful. It is zero if fonts were loaded during
16354 redisplay which makes re-adjusting glyph matrices necessary, and -1
16355 if point would appear in the scroll margins.
16356 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16357 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16358 set in FLAGS.) */
16359
16360 int
16361 try_window (Lisp_Object window, struct text_pos pos, int flags)
16362 {
16363 struct window *w = XWINDOW (window);
16364 struct it it;
16365 struct glyph_row *last_text_row = NULL;
16366 struct frame *f = XFRAME (w->frame);
16367 int frame_line_height = default_line_pixel_height (w);
16368
16369 /* Make POS the new window start. */
16370 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16371
16372 /* Mark cursor position as unknown. No overlay arrow seen. */
16373 w->cursor.vpos = -1;
16374 overlay_arrow_seen = 0;
16375
16376 /* Initialize iterator and info to start at POS. */
16377 start_display (&it, w, pos);
16378
16379
16380
16381 /* Display all lines of W. */
16382 while (it.current_y < it.last_visible_y)
16383 {
16384 if (display_line (&it))
16385 last_text_row = it.glyph_row - 1;
16386 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16387 return 0;
16388 }
16389 #ifdef HAVE_XWIDGETS_xxx
16390 //currently this is needed to detect xwidget movement reliably. or probably not.
16391 printf("try_window\n");
16392 return 0;
16393 #endif
16394
16395 /* Don't let the cursor end in the scroll margins. */
16396 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16397 && !MINI_WINDOW_P (w))
16398 {
16399 int this_scroll_margin;
16400 int window_total_lines
16401 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16402
16403 if (scroll_margin > 0)
16404 {
16405 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16406 this_scroll_margin *= frame_line_height;
16407 }
16408 else
16409 this_scroll_margin = 0;
16410
16411 if ((w->cursor.y >= 0 /* not vscrolled */
16412 && w->cursor.y < this_scroll_margin
16413 && CHARPOS (pos) > BEGV
16414 && IT_CHARPOS (it) < ZV)
16415 /* rms: considering make_cursor_line_fully_visible_p here
16416 seems to give wrong results. We don't want to recenter
16417 when the last line is partly visible, we want to allow
16418 that case to be handled in the usual way. */
16419 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16420 {
16421 w->cursor.vpos = -1;
16422 clear_glyph_matrix (w->desired_matrix);
16423 return -1;
16424 }
16425 }
16426
16427 /* If bottom moved off end of frame, change mode line percentage. */
16428 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16429 w->update_mode_line = 1;
16430
16431 /* Set window_end_pos to the offset of the last character displayed
16432 on the window from the end of current_buffer. Set
16433 window_end_vpos to its row number. */
16434 if (last_text_row)
16435 {
16436 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16437 adjust_window_ends (w, last_text_row, 0);
16438 eassert
16439 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16440 w->window_end_vpos)));
16441 }
16442 else
16443 {
16444 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16445 w->window_end_pos = Z - ZV;
16446 w->window_end_vpos = 0;
16447 }
16448
16449 /* But that is not valid info until redisplay finishes. */
16450 w->window_end_valid = 0;
16451 return 1;
16452 }
16453
16454
16455 \f
16456 /************************************************************************
16457 Window redisplay reusing current matrix when buffer has not changed
16458 ************************************************************************/
16459
16460 /* Try redisplay of window W showing an unchanged buffer with a
16461 different window start than the last time it was displayed by
16462 reusing its current matrix. Value is non-zero if successful.
16463 W->start is the new window start. */
16464
16465 static int
16466 try_window_reusing_current_matrix (struct window *w)
16467 {
16468 struct frame *f = XFRAME (w->frame);
16469 struct glyph_row *bottom_row;
16470 struct it it;
16471 struct run run;
16472 struct text_pos start, new_start;
16473 int nrows_scrolled, i;
16474 struct glyph_row *last_text_row;
16475 struct glyph_row *last_reused_text_row;
16476 struct glyph_row *start_row;
16477 int start_vpos, min_y, max_y;
16478
16479 #ifdef GLYPH_DEBUG
16480 if (inhibit_try_window_reusing)
16481 return 0;
16482 #endif
16483
16484 #ifdef HAVE_XWIDGETS_xxx
16485 //currently this is needed to detect xwidget movement reliably. or probably not.
16486 printf("try_window_reusing_current_matrix\n");
16487 return 0;
16488 #endif
16489
16490
16491 if (/* This function doesn't handle terminal frames. */
16492 !FRAME_WINDOW_P (f)
16493 /* Don't try to reuse the display if windows have been split
16494 or such. */
16495 || windows_or_buffers_changed
16496 || cursor_type_changed)
16497 return 0;
16498
16499 /* Can't do this if region may have changed. */
16500 if (markpos_of_region () >= 0
16501 || w->region_showing
16502 || !NILP (Vshow_trailing_whitespace))
16503 return 0;
16504
16505 /* If top-line visibility has changed, give up. */
16506 if (WINDOW_WANTS_HEADER_LINE_P (w)
16507 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16508 return 0;
16509
16510 /* Give up if old or new display is scrolled vertically. We could
16511 make this function handle this, but right now it doesn't. */
16512 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16513 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16514 return 0;
16515
16516 /* The variable new_start now holds the new window start. The old
16517 start `start' can be determined from the current matrix. */
16518 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16519 start = start_row->minpos;
16520 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16521
16522 /* Clear the desired matrix for the display below. */
16523 clear_glyph_matrix (w->desired_matrix);
16524
16525 if (CHARPOS (new_start) <= CHARPOS (start))
16526 {
16527 /* Don't use this method if the display starts with an ellipsis
16528 displayed for invisible text. It's not easy to handle that case
16529 below, and it's certainly not worth the effort since this is
16530 not a frequent case. */
16531 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16532 return 0;
16533
16534 IF_DEBUG (debug_method_add (w, "twu1"));
16535
16536 /* Display up to a row that can be reused. The variable
16537 last_text_row is set to the last row displayed that displays
16538 text. Note that it.vpos == 0 if or if not there is a
16539 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16540 start_display (&it, w, new_start);
16541 w->cursor.vpos = -1;
16542 last_text_row = last_reused_text_row = NULL;
16543
16544 while (it.current_y < it.last_visible_y
16545 && !fonts_changed_p)
16546 {
16547 /* If we have reached into the characters in the START row,
16548 that means the line boundaries have changed. So we
16549 can't start copying with the row START. Maybe it will
16550 work to start copying with the following row. */
16551 while (IT_CHARPOS (it) > CHARPOS (start))
16552 {
16553 /* Advance to the next row as the "start". */
16554 start_row++;
16555 start = start_row->minpos;
16556 /* If there are no more rows to try, or just one, give up. */
16557 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16558 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16559 || CHARPOS (start) == ZV)
16560 {
16561 clear_glyph_matrix (w->desired_matrix);
16562 return 0;
16563 }
16564
16565 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16566 }
16567 /* If we have reached alignment, we can copy the rest of the
16568 rows. */
16569 if (IT_CHARPOS (it) == CHARPOS (start)
16570 /* Don't accept "alignment" inside a display vector,
16571 since start_row could have started in the middle of
16572 that same display vector (thus their character
16573 positions match), and we have no way of telling if
16574 that is the case. */
16575 && it.current.dpvec_index < 0)
16576 break;
16577
16578 if (display_line (&it))
16579 last_text_row = it.glyph_row - 1;
16580
16581 }
16582
16583 /* A value of current_y < last_visible_y means that we stopped
16584 at the previous window start, which in turn means that we
16585 have at least one reusable row. */
16586 if (it.current_y < it.last_visible_y)
16587 {
16588 struct glyph_row *row;
16589
16590 /* IT.vpos always starts from 0; it counts text lines. */
16591 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16592
16593 /* Find PT if not already found in the lines displayed. */
16594 if (w->cursor.vpos < 0)
16595 {
16596 int dy = it.current_y - start_row->y;
16597
16598 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16599 row = row_containing_pos (w, PT, row, NULL, dy);
16600 if (row)
16601 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16602 dy, nrows_scrolled);
16603 else
16604 {
16605 clear_glyph_matrix (w->desired_matrix);
16606 return 0;
16607 }
16608 }
16609
16610 /* Scroll the display. Do it before the current matrix is
16611 changed. The problem here is that update has not yet
16612 run, i.e. part of the current matrix is not up to date.
16613 scroll_run_hook will clear the cursor, and use the
16614 current matrix to get the height of the row the cursor is
16615 in. */
16616 run.current_y = start_row->y;
16617 run.desired_y = it.current_y;
16618 run.height = it.last_visible_y - it.current_y;
16619
16620 if (run.height > 0 && run.current_y != run.desired_y)
16621 {
16622 update_begin (f);
16623 FRAME_RIF (f)->update_window_begin_hook (w);
16624 FRAME_RIF (f)->clear_window_mouse_face (w);
16625 FRAME_RIF (f)->scroll_run_hook (w, &run);
16626 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16627 update_end (f);
16628 }
16629
16630 /* Shift current matrix down by nrows_scrolled lines. */
16631 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16632 rotate_matrix (w->current_matrix,
16633 start_vpos,
16634 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16635 nrows_scrolled);
16636
16637 /* Disable lines that must be updated. */
16638 for (i = 0; i < nrows_scrolled; ++i)
16639 (start_row + i)->enabled_p = 0;
16640
16641 /* Re-compute Y positions. */
16642 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16643 max_y = it.last_visible_y;
16644 for (row = start_row + nrows_scrolled;
16645 row < bottom_row;
16646 ++row)
16647 {
16648 row->y = it.current_y;
16649 row->visible_height = row->height;
16650
16651 if (row->y < min_y)
16652 row->visible_height -= min_y - row->y;
16653 if (row->y + row->height > max_y)
16654 row->visible_height -= row->y + row->height - max_y;
16655 if (row->fringe_bitmap_periodic_p)
16656 row->redraw_fringe_bitmaps_p = 1;
16657
16658 it.current_y += row->height;
16659
16660 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16661 last_reused_text_row = row;
16662 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16663 break;
16664 }
16665
16666 /* Disable lines in the current matrix which are now
16667 below the window. */
16668 for (++row; row < bottom_row; ++row)
16669 row->enabled_p = row->mode_line_p = 0;
16670 }
16671
16672 /* Update window_end_pos etc.; last_reused_text_row is the last
16673 reused row from the current matrix containing text, if any.
16674 The value of last_text_row is the last displayed line
16675 containing text. */
16676 if (last_reused_text_row)
16677 adjust_window_ends (w, last_reused_text_row, 1);
16678 else if (last_text_row)
16679 adjust_window_ends (w, last_text_row, 0);
16680 else
16681 {
16682 /* This window must be completely empty. */
16683 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16684 w->window_end_pos = Z - ZV;
16685 w->window_end_vpos = 0;
16686 }
16687 w->window_end_valid = 0;
16688
16689 /* Update hint: don't try scrolling again in update_window. */
16690 w->desired_matrix->no_scrolling_p = 1;
16691
16692 #ifdef GLYPH_DEBUG
16693 debug_method_add (w, "try_window_reusing_current_matrix 1");
16694 #endif
16695 return 1;
16696 }
16697 else if (CHARPOS (new_start) > CHARPOS (start))
16698 {
16699 struct glyph_row *pt_row, *row;
16700 struct glyph_row *first_reusable_row;
16701 struct glyph_row *first_row_to_display;
16702 int dy;
16703 int yb = window_text_bottom_y (w);
16704
16705 /* Find the row starting at new_start, if there is one. Don't
16706 reuse a partially visible line at the end. */
16707 first_reusable_row = start_row;
16708 while (first_reusable_row->enabled_p
16709 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16710 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16711 < CHARPOS (new_start)))
16712 ++first_reusable_row;
16713
16714 /* Give up if there is no row to reuse. */
16715 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16716 || !first_reusable_row->enabled_p
16717 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16718 != CHARPOS (new_start)))
16719 return 0;
16720
16721 /* We can reuse fully visible rows beginning with
16722 first_reusable_row to the end of the window. Set
16723 first_row_to_display to the first row that cannot be reused.
16724 Set pt_row to the row containing point, if there is any. */
16725 pt_row = NULL;
16726 for (first_row_to_display = first_reusable_row;
16727 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16728 ++first_row_to_display)
16729 {
16730 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16731 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16732 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16733 && first_row_to_display->ends_at_zv_p
16734 && pt_row == NULL)))
16735 pt_row = first_row_to_display;
16736 }
16737
16738 /* Start displaying at the start of first_row_to_display. */
16739 eassert (first_row_to_display->y < yb);
16740 init_to_row_start (&it, w, first_row_to_display);
16741
16742 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16743 - start_vpos);
16744 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16745 - nrows_scrolled);
16746 it.current_y = (first_row_to_display->y - first_reusable_row->y
16747 + WINDOW_HEADER_LINE_HEIGHT (w));
16748
16749 /* Display lines beginning with first_row_to_display in the
16750 desired matrix. Set last_text_row to the last row displayed
16751 that displays text. */
16752 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16753 if (pt_row == NULL)
16754 w->cursor.vpos = -1;
16755 last_text_row = NULL;
16756 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16757 if (display_line (&it))
16758 last_text_row = it.glyph_row - 1;
16759
16760 /* If point is in a reused row, adjust y and vpos of the cursor
16761 position. */
16762 if (pt_row)
16763 {
16764 w->cursor.vpos -= nrows_scrolled;
16765 w->cursor.y -= first_reusable_row->y - start_row->y;
16766 }
16767
16768 /* Give up if point isn't in a row displayed or reused. (This
16769 also handles the case where w->cursor.vpos < nrows_scrolled
16770 after the calls to display_line, which can happen with scroll
16771 margins. See bug#1295.) */
16772 if (w->cursor.vpos < 0)
16773 {
16774 clear_glyph_matrix (w->desired_matrix);
16775 return 0;
16776 }
16777
16778 /* Scroll the display. */
16779 run.current_y = first_reusable_row->y;
16780 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16781 run.height = it.last_visible_y - run.current_y;
16782 dy = run.current_y - run.desired_y;
16783
16784 if (run.height)
16785 {
16786 update_begin (f);
16787 FRAME_RIF (f)->update_window_begin_hook (w);
16788 FRAME_RIF (f)->clear_window_mouse_face (w);
16789 FRAME_RIF (f)->scroll_run_hook (w, &run);
16790 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16791 update_end (f);
16792 }
16793
16794 /* Adjust Y positions of reused rows. */
16795 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16796 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16797 max_y = it.last_visible_y;
16798 for (row = first_reusable_row; row < first_row_to_display; ++row)
16799 {
16800 row->y -= dy;
16801 row->visible_height = row->height;
16802 if (row->y < min_y)
16803 row->visible_height -= min_y - row->y;
16804 if (row->y + row->height > max_y)
16805 row->visible_height -= row->y + row->height - max_y;
16806 if (row->fringe_bitmap_periodic_p)
16807 row->redraw_fringe_bitmaps_p = 1;
16808 }
16809
16810 /* Scroll the current matrix. */
16811 eassert (nrows_scrolled > 0);
16812 rotate_matrix (w->current_matrix,
16813 start_vpos,
16814 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16815 -nrows_scrolled);
16816
16817 /* Disable rows not reused. */
16818 for (row -= nrows_scrolled; row < bottom_row; ++row)
16819 row->enabled_p = 0;
16820
16821 /* Point may have moved to a different line, so we cannot assume that
16822 the previous cursor position is valid; locate the correct row. */
16823 if (pt_row)
16824 {
16825 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16826 row < bottom_row
16827 && PT >= MATRIX_ROW_END_CHARPOS (row)
16828 && !row->ends_at_zv_p;
16829 row++)
16830 {
16831 w->cursor.vpos++;
16832 w->cursor.y = row->y;
16833 }
16834 if (row < bottom_row)
16835 {
16836 /* Can't simply scan the row for point with
16837 bidi-reordered glyph rows. Let set_cursor_from_row
16838 figure out where to put the cursor, and if it fails,
16839 give up. */
16840 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16841 {
16842 if (!set_cursor_from_row (w, row, w->current_matrix,
16843 0, 0, 0, 0))
16844 {
16845 clear_glyph_matrix (w->desired_matrix);
16846 return 0;
16847 }
16848 }
16849 else
16850 {
16851 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16852 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16853
16854 for (; glyph < end
16855 && (!BUFFERP (glyph->object)
16856 || glyph->charpos < PT);
16857 glyph++)
16858 {
16859 w->cursor.hpos++;
16860 w->cursor.x += glyph->pixel_width;
16861 }
16862 }
16863 }
16864 }
16865
16866 /* Adjust window end. A null value of last_text_row means that
16867 the window end is in reused rows which in turn means that
16868 only its vpos can have changed. */
16869 if (last_text_row)
16870 adjust_window_ends (w, last_text_row, 0);
16871 else
16872 w->window_end_vpos -= nrows_scrolled;
16873
16874 w->window_end_valid = 0;
16875 w->desired_matrix->no_scrolling_p = 1;
16876
16877 #ifdef GLYPH_DEBUG
16878 debug_method_add (w, "try_window_reusing_current_matrix 2");
16879 #endif
16880 return 1;
16881 }
16882
16883 return 0;
16884 }
16885
16886
16887 \f
16888 /************************************************************************
16889 Window redisplay reusing current matrix when buffer has changed
16890 ************************************************************************/
16891
16892 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16893 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16894 ptrdiff_t *, ptrdiff_t *);
16895 static struct glyph_row *
16896 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16897 struct glyph_row *);
16898
16899
16900 /* Return the last row in MATRIX displaying text. If row START is
16901 non-null, start searching with that row. IT gives the dimensions
16902 of the display. Value is null if matrix is empty; otherwise it is
16903 a pointer to the row found. */
16904
16905 static struct glyph_row *
16906 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16907 struct glyph_row *start)
16908 {
16909 struct glyph_row *row, *row_found;
16910
16911 /* Set row_found to the last row in IT->w's current matrix
16912 displaying text. The loop looks funny but think of partially
16913 visible lines. */
16914 row_found = NULL;
16915 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16916 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16917 {
16918 eassert (row->enabled_p);
16919 row_found = row;
16920 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16921 break;
16922 ++row;
16923 }
16924
16925 return row_found;
16926 }
16927
16928
16929 /* Return the last row in the current matrix of W that is not affected
16930 by changes at the start of current_buffer that occurred since W's
16931 current matrix was built. Value is null if no such row exists.
16932
16933 BEG_UNCHANGED us the number of characters unchanged at the start of
16934 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16935 first changed character in current_buffer. Characters at positions <
16936 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16937 when the current matrix was built. */
16938
16939 static struct glyph_row *
16940 find_last_unchanged_at_beg_row (struct window *w)
16941 {
16942 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16943 struct glyph_row *row;
16944 struct glyph_row *row_found = NULL;
16945 int yb = window_text_bottom_y (w);
16946
16947 /* Find the last row displaying unchanged text. */
16948 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16949 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16950 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16951 ++row)
16952 {
16953 if (/* If row ends before first_changed_pos, it is unchanged,
16954 except in some case. */
16955 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16956 /* When row ends in ZV and we write at ZV it is not
16957 unchanged. */
16958 && !row->ends_at_zv_p
16959 /* When first_changed_pos is the end of a continued line,
16960 row is not unchanged because it may be no longer
16961 continued. */
16962 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16963 && (row->continued_p
16964 || row->exact_window_width_line_p))
16965 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16966 needs to be recomputed, so don't consider this row as
16967 unchanged. This happens when the last line was
16968 bidi-reordered and was killed immediately before this
16969 redisplay cycle. In that case, ROW->end stores the
16970 buffer position of the first visual-order character of
16971 the killed text, which is now beyond ZV. */
16972 && CHARPOS (row->end.pos) <= ZV)
16973 row_found = row;
16974
16975 /* Stop if last visible row. */
16976 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16977 break;
16978 }
16979
16980 return row_found;
16981 }
16982
16983
16984 /* Find the first glyph row in the current matrix of W that is not
16985 affected by changes at the end of current_buffer since the
16986 time W's current matrix was built.
16987
16988 Return in *DELTA the number of chars by which buffer positions in
16989 unchanged text at the end of current_buffer must be adjusted.
16990
16991 Return in *DELTA_BYTES the corresponding number of bytes.
16992
16993 Value is null if no such row exists, i.e. all rows are affected by
16994 changes. */
16995
16996 static struct glyph_row *
16997 find_first_unchanged_at_end_row (struct window *w,
16998 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16999 {
17000 struct glyph_row *row;
17001 struct glyph_row *row_found = NULL;
17002
17003 *delta = *delta_bytes = 0;
17004
17005 /* Display must not have been paused, otherwise the current matrix
17006 is not up to date. */
17007 eassert (w->window_end_valid);
17008
17009 /* A value of window_end_pos >= END_UNCHANGED means that the window
17010 end is in the range of changed text. If so, there is no
17011 unchanged row at the end of W's current matrix. */
17012 if (w->window_end_pos >= END_UNCHANGED)
17013 return NULL;
17014
17015 /* Set row to the last row in W's current matrix displaying text. */
17016 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17017
17018 /* If matrix is entirely empty, no unchanged row exists. */
17019 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17020 {
17021 /* The value of row is the last glyph row in the matrix having a
17022 meaningful buffer position in it. The end position of row
17023 corresponds to window_end_pos. This allows us to translate
17024 buffer positions in the current matrix to current buffer
17025 positions for characters not in changed text. */
17026 ptrdiff_t Z_old =
17027 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17028 ptrdiff_t Z_BYTE_old =
17029 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17030 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17031 struct glyph_row *first_text_row
17032 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17033
17034 *delta = Z - Z_old;
17035 *delta_bytes = Z_BYTE - Z_BYTE_old;
17036
17037 /* Set last_unchanged_pos to the buffer position of the last
17038 character in the buffer that has not been changed. Z is the
17039 index + 1 of the last character in current_buffer, i.e. by
17040 subtracting END_UNCHANGED we get the index of the last
17041 unchanged character, and we have to add BEG to get its buffer
17042 position. */
17043 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17044 last_unchanged_pos_old = last_unchanged_pos - *delta;
17045
17046 /* Search backward from ROW for a row displaying a line that
17047 starts at a minimum position >= last_unchanged_pos_old. */
17048 for (; row > first_text_row; --row)
17049 {
17050 /* This used to abort, but it can happen.
17051 It is ok to just stop the search instead here. KFS. */
17052 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17053 break;
17054
17055 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17056 row_found = row;
17057 }
17058 }
17059
17060 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17061
17062 return row_found;
17063 }
17064
17065
17066 /* Make sure that glyph rows in the current matrix of window W
17067 reference the same glyph memory as corresponding rows in the
17068 frame's frame matrix. This function is called after scrolling W's
17069 current matrix on a terminal frame in try_window_id and
17070 try_window_reusing_current_matrix. */
17071
17072 static void
17073 sync_frame_with_window_matrix_rows (struct window *w)
17074 {
17075 struct frame *f = XFRAME (w->frame);
17076 struct glyph_row *window_row, *window_row_end, *frame_row;
17077
17078 /* Preconditions: W must be a leaf window and full-width. Its frame
17079 must have a frame matrix. */
17080 eassert (BUFFERP (w->contents));
17081 eassert (WINDOW_FULL_WIDTH_P (w));
17082 eassert (!FRAME_WINDOW_P (f));
17083
17084 /* If W is a full-width window, glyph pointers in W's current matrix
17085 have, by definition, to be the same as glyph pointers in the
17086 corresponding frame matrix. Note that frame matrices have no
17087 marginal areas (see build_frame_matrix). */
17088 window_row = w->current_matrix->rows;
17089 window_row_end = window_row + w->current_matrix->nrows;
17090 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17091 while (window_row < window_row_end)
17092 {
17093 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17094 struct glyph *end = window_row->glyphs[LAST_AREA];
17095
17096 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17097 frame_row->glyphs[TEXT_AREA] = start;
17098 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17099 frame_row->glyphs[LAST_AREA] = end;
17100
17101 /* Disable frame rows whose corresponding window rows have
17102 been disabled in try_window_id. */
17103 if (!window_row->enabled_p)
17104 frame_row->enabled_p = 0;
17105
17106 ++window_row, ++frame_row;
17107 }
17108 }
17109
17110
17111 /* Find the glyph row in window W containing CHARPOS. Consider all
17112 rows between START and END (not inclusive). END null means search
17113 all rows to the end of the display area of W. Value is the row
17114 containing CHARPOS or null. */
17115
17116 struct glyph_row *
17117 row_containing_pos (struct window *w, ptrdiff_t charpos,
17118 struct glyph_row *start, struct glyph_row *end, int dy)
17119 {
17120 struct glyph_row *row = start;
17121 struct glyph_row *best_row = NULL;
17122 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17123 int last_y;
17124
17125 /* If we happen to start on a header-line, skip that. */
17126 if (row->mode_line_p)
17127 ++row;
17128
17129 if ((end && row >= end) || !row->enabled_p)
17130 return NULL;
17131
17132 last_y = window_text_bottom_y (w) - dy;
17133
17134 while (1)
17135 {
17136 /* Give up if we have gone too far. */
17137 if (end && row >= end)
17138 return NULL;
17139 /* This formerly returned if they were equal.
17140 I think that both quantities are of a "last plus one" type;
17141 if so, when they are equal, the row is within the screen. -- rms. */
17142 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17143 return NULL;
17144
17145 /* If it is in this row, return this row. */
17146 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17147 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17148 /* The end position of a row equals the start
17149 position of the next row. If CHARPOS is there, we
17150 would rather consider it displayed in the next
17151 line, except when this line ends in ZV. */
17152 && !row_for_charpos_p (row, charpos)))
17153 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17154 {
17155 struct glyph *g;
17156
17157 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17158 || (!best_row && !row->continued_p))
17159 return row;
17160 /* In bidi-reordered rows, there could be several rows whose
17161 edges surround CHARPOS, all of these rows belonging to
17162 the same continued line. We need to find the row which
17163 fits CHARPOS the best. */
17164 for (g = row->glyphs[TEXT_AREA];
17165 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17166 g++)
17167 {
17168 if (!STRINGP (g->object))
17169 {
17170 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17171 {
17172 mindif = eabs (g->charpos - charpos);
17173 best_row = row;
17174 /* Exact match always wins. */
17175 if (mindif == 0)
17176 return best_row;
17177 }
17178 }
17179 }
17180 }
17181 else if (best_row && !row->continued_p)
17182 return best_row;
17183 ++row;
17184 }
17185 }
17186
17187
17188 /* Try to redisplay window W by reusing its existing display. W's
17189 current matrix must be up to date when this function is called,
17190 i.e. window_end_valid must be nonzero.
17191
17192 Value is
17193
17194 1 if display has been updated
17195 0 if otherwise unsuccessful
17196 -1 if redisplay with same window start is known not to succeed
17197
17198 The following steps are performed:
17199
17200 1. Find the last row in the current matrix of W that is not
17201 affected by changes at the start of current_buffer. If no such row
17202 is found, give up.
17203
17204 2. Find the first row in W's current matrix that is not affected by
17205 changes at the end of current_buffer. Maybe there is no such row.
17206
17207 3. Display lines beginning with the row + 1 found in step 1 to the
17208 row found in step 2 or, if step 2 didn't find a row, to the end of
17209 the window.
17210
17211 4. If cursor is not known to appear on the window, give up.
17212
17213 5. If display stopped at the row found in step 2, scroll the
17214 display and current matrix as needed.
17215
17216 6. Maybe display some lines at the end of W, if we must. This can
17217 happen under various circumstances, like a partially visible line
17218 becoming fully visible, or because newly displayed lines are displayed
17219 in smaller font sizes.
17220
17221 7. Update W's window end information. */
17222
17223 static int
17224 try_window_id (struct window *w)
17225 {
17226 struct frame *f = XFRAME (w->frame);
17227 struct glyph_matrix *current_matrix = w->current_matrix;
17228 struct glyph_matrix *desired_matrix = w->desired_matrix;
17229 struct glyph_row *last_unchanged_at_beg_row;
17230 struct glyph_row *first_unchanged_at_end_row;
17231 struct glyph_row *row;
17232 struct glyph_row *bottom_row;
17233 int bottom_vpos;
17234 struct it it;
17235 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17236 int dvpos, dy;
17237 struct text_pos start_pos;
17238 struct run run;
17239 int first_unchanged_at_end_vpos = 0;
17240 struct glyph_row *last_text_row, *last_text_row_at_end;
17241 struct text_pos start;
17242 ptrdiff_t first_changed_charpos, last_changed_charpos;
17243
17244 #ifdef GLYPH_DEBUG
17245 if (inhibit_try_window_id)
17246 return 0;
17247 #endif
17248
17249 #ifdef HAVE_XWIDGETS_xxx
17250 //maybe needed for proper xwidget movement
17251 printf("try_window_id\n");
17252 return -1;
17253 #endif
17254
17255
17256 /* This is handy for debugging. */
17257 #if 0
17258 #define GIVE_UP(X) \
17259 do { \
17260 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17261 return 0; \
17262 } while (0)
17263 #else
17264 #define GIVE_UP(X) return 0
17265 #endif
17266
17267 SET_TEXT_POS_FROM_MARKER (start, w->start);
17268
17269 /* Don't use this for mini-windows because these can show
17270 messages and mini-buffers, and we don't handle that here. */
17271 if (MINI_WINDOW_P (w))
17272 GIVE_UP (1);
17273
17274 /* This flag is used to prevent redisplay optimizations. */
17275 if (windows_or_buffers_changed || cursor_type_changed)
17276 GIVE_UP (2);
17277
17278 /* Verify that narrowing has not changed.
17279 Also verify that we were not told to prevent redisplay optimizations.
17280 It would be nice to further
17281 reduce the number of cases where this prevents try_window_id. */
17282 if (current_buffer->clip_changed
17283 || current_buffer->prevent_redisplay_optimizations_p)
17284 GIVE_UP (3);
17285
17286 /* Window must either use window-based redisplay or be full width. */
17287 if (!FRAME_WINDOW_P (f)
17288 && (!FRAME_LINE_INS_DEL_OK (f)
17289 || !WINDOW_FULL_WIDTH_P (w)))
17290 GIVE_UP (4);
17291
17292 /* Give up if point is known NOT to appear in W. */
17293 if (PT < CHARPOS (start))
17294 GIVE_UP (5);
17295
17296 /* Another way to prevent redisplay optimizations. */
17297 if (w->last_modified == 0)
17298 GIVE_UP (6);
17299
17300 /* Verify that window is not hscrolled. */
17301 if (w->hscroll != 0)
17302 GIVE_UP (7);
17303
17304 /* Verify that display wasn't paused. */
17305 if (!w->window_end_valid)
17306 GIVE_UP (8);
17307
17308 /* Can't use this if highlighting a region because a cursor movement
17309 will do more than just set the cursor. */
17310 if (markpos_of_region () >= 0)
17311 GIVE_UP (9);
17312
17313 /* Likewise if highlighting trailing whitespace. */
17314 if (!NILP (Vshow_trailing_whitespace))
17315 GIVE_UP (11);
17316
17317 /* Likewise if showing a region. */
17318 if (w->region_showing)
17319 GIVE_UP (10);
17320
17321 /* Can't use this if overlay arrow position and/or string have
17322 changed. */
17323 if (overlay_arrows_changed_p ())
17324 GIVE_UP (12);
17325
17326 /* When word-wrap is on, adding a space to the first word of a
17327 wrapped line can change the wrap position, altering the line
17328 above it. It might be worthwhile to handle this more
17329 intelligently, but for now just redisplay from scratch. */
17330 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17331 GIVE_UP (21);
17332
17333 /* Under bidi reordering, adding or deleting a character in the
17334 beginning of a paragraph, before the first strong directional
17335 character, can change the base direction of the paragraph (unless
17336 the buffer specifies a fixed paragraph direction), which will
17337 require to redisplay the whole paragraph. It might be worthwhile
17338 to find the paragraph limits and widen the range of redisplayed
17339 lines to that, but for now just give up this optimization and
17340 redisplay from scratch. */
17341 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17342 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17343 GIVE_UP (22);
17344
17345 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17346 only if buffer has really changed. The reason is that the gap is
17347 initially at Z for freshly visited files. The code below would
17348 set end_unchanged to 0 in that case. */
17349 if (MODIFF > SAVE_MODIFF
17350 /* This seems to happen sometimes after saving a buffer. */
17351 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17352 {
17353 if (GPT - BEG < BEG_UNCHANGED)
17354 BEG_UNCHANGED = GPT - BEG;
17355 if (Z - GPT < END_UNCHANGED)
17356 END_UNCHANGED = Z - GPT;
17357 }
17358
17359 /* The position of the first and last character that has been changed. */
17360 first_changed_charpos = BEG + BEG_UNCHANGED;
17361 last_changed_charpos = Z - END_UNCHANGED;
17362
17363 /* If window starts after a line end, and the last change is in
17364 front of that newline, then changes don't affect the display.
17365 This case happens with stealth-fontification. Note that although
17366 the display is unchanged, glyph positions in the matrix have to
17367 be adjusted, of course. */
17368 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17369 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17370 && ((last_changed_charpos < CHARPOS (start)
17371 && CHARPOS (start) == BEGV)
17372 || (last_changed_charpos < CHARPOS (start) - 1
17373 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17374 {
17375 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17376 struct glyph_row *r0;
17377
17378 /* Compute how many chars/bytes have been added to or removed
17379 from the buffer. */
17380 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17381 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17382 Z_delta = Z - Z_old;
17383 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17384
17385 /* Give up if PT is not in the window. Note that it already has
17386 been checked at the start of try_window_id that PT is not in
17387 front of the window start. */
17388 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17389 GIVE_UP (13);
17390
17391 /* If window start is unchanged, we can reuse the whole matrix
17392 as is, after adjusting glyph positions. No need to compute
17393 the window end again, since its offset from Z hasn't changed. */
17394 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17395 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17396 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17397 /* PT must not be in a partially visible line. */
17398 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17399 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17400 {
17401 /* Adjust positions in the glyph matrix. */
17402 if (Z_delta || Z_delta_bytes)
17403 {
17404 struct glyph_row *r1
17405 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17406 increment_matrix_positions (w->current_matrix,
17407 MATRIX_ROW_VPOS (r0, current_matrix),
17408 MATRIX_ROW_VPOS (r1, current_matrix),
17409 Z_delta, Z_delta_bytes);
17410 }
17411
17412 /* Set the cursor. */
17413 row = row_containing_pos (w, PT, r0, NULL, 0);
17414 if (row)
17415 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17416 else
17417 emacs_abort ();
17418 return 1;
17419 }
17420 }
17421
17422 /* Handle the case that changes are all below what is displayed in
17423 the window, and that PT is in the window. This shortcut cannot
17424 be taken if ZV is visible in the window, and text has been added
17425 there that is visible in the window. */
17426 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17427 /* ZV is not visible in the window, or there are no
17428 changes at ZV, actually. */
17429 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17430 || first_changed_charpos == last_changed_charpos))
17431 {
17432 struct glyph_row *r0;
17433
17434 /* Give up if PT is not in the window. Note that it already has
17435 been checked at the start of try_window_id that PT is not in
17436 front of the window start. */
17437 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17438 GIVE_UP (14);
17439
17440 /* If window start is unchanged, we can reuse the whole matrix
17441 as is, without changing glyph positions since no text has
17442 been added/removed in front of the window end. */
17443 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17444 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17445 /* PT must not be in a partially visible line. */
17446 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17447 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17448 {
17449 /* We have to compute the window end anew since text
17450 could have been added/removed after it. */
17451 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17452 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17453
17454 /* Set the cursor. */
17455 row = row_containing_pos (w, PT, r0, NULL, 0);
17456 if (row)
17457 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17458 else
17459 emacs_abort ();
17460 return 2;
17461 }
17462 }
17463
17464 /* Give up if window start is in the changed area.
17465
17466 The condition used to read
17467
17468 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17469
17470 but why that was tested escapes me at the moment. */
17471 if (CHARPOS (start) >= first_changed_charpos
17472 && CHARPOS (start) <= last_changed_charpos)
17473 GIVE_UP (15);
17474
17475 /* Check that window start agrees with the start of the first glyph
17476 row in its current matrix. Check this after we know the window
17477 start is not in changed text, otherwise positions would not be
17478 comparable. */
17479 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17480 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17481 GIVE_UP (16);
17482
17483 /* Give up if the window ends in strings. Overlay strings
17484 at the end are difficult to handle, so don't try. */
17485 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17486 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17487 GIVE_UP (20);
17488
17489 /* Compute the position at which we have to start displaying new
17490 lines. Some of the lines at the top of the window might be
17491 reusable because they are not displaying changed text. Find the
17492 last row in W's current matrix not affected by changes at the
17493 start of current_buffer. Value is null if changes start in the
17494 first line of window. */
17495 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17496 if (last_unchanged_at_beg_row)
17497 {
17498 /* Avoid starting to display in the middle of a character, a TAB
17499 for instance. This is easier than to set up the iterator
17500 exactly, and it's not a frequent case, so the additional
17501 effort wouldn't really pay off. */
17502 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17503 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17504 && last_unchanged_at_beg_row > w->current_matrix->rows)
17505 --last_unchanged_at_beg_row;
17506
17507 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17508 GIVE_UP (17);
17509
17510 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17511 GIVE_UP (18);
17512 start_pos = it.current.pos;
17513
17514 /* Start displaying new lines in the desired matrix at the same
17515 vpos we would use in the current matrix, i.e. below
17516 last_unchanged_at_beg_row. */
17517 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17518 current_matrix);
17519 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17520 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17521
17522 eassert (it.hpos == 0 && it.current_x == 0);
17523 }
17524 else
17525 {
17526 /* There are no reusable lines at the start of the window.
17527 Start displaying in the first text line. */
17528 start_display (&it, w, start);
17529 it.vpos = it.first_vpos;
17530 start_pos = it.current.pos;
17531 }
17532
17533 /* Find the first row that is not affected by changes at the end of
17534 the buffer. Value will be null if there is no unchanged row, in
17535 which case we must redisplay to the end of the window. delta
17536 will be set to the value by which buffer positions beginning with
17537 first_unchanged_at_end_row have to be adjusted due to text
17538 changes. */
17539 first_unchanged_at_end_row
17540 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17541 IF_DEBUG (debug_delta = delta);
17542 IF_DEBUG (debug_delta_bytes = delta_bytes);
17543
17544 /* Set stop_pos to the buffer position up to which we will have to
17545 display new lines. If first_unchanged_at_end_row != NULL, this
17546 is the buffer position of the start of the line displayed in that
17547 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17548 that we don't stop at a buffer position. */
17549 stop_pos = 0;
17550 if (first_unchanged_at_end_row)
17551 {
17552 eassert (last_unchanged_at_beg_row == NULL
17553 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17554
17555 /* If this is a continuation line, move forward to the next one
17556 that isn't. Changes in lines above affect this line.
17557 Caution: this may move first_unchanged_at_end_row to a row
17558 not displaying text. */
17559 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17560 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17561 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17562 < it.last_visible_y))
17563 ++first_unchanged_at_end_row;
17564
17565 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17566 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17567 >= it.last_visible_y))
17568 first_unchanged_at_end_row = NULL;
17569 else
17570 {
17571 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17572 + delta);
17573 first_unchanged_at_end_vpos
17574 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17575 eassert (stop_pos >= Z - END_UNCHANGED);
17576 }
17577 }
17578 else if (last_unchanged_at_beg_row == NULL)
17579 GIVE_UP (19);
17580
17581
17582 #ifdef GLYPH_DEBUG
17583
17584 /* Either there is no unchanged row at the end, or the one we have
17585 now displays text. This is a necessary condition for the window
17586 end pos calculation at the end of this function. */
17587 eassert (first_unchanged_at_end_row == NULL
17588 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17589
17590 debug_last_unchanged_at_beg_vpos
17591 = (last_unchanged_at_beg_row
17592 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17593 : -1);
17594 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17595
17596 #endif /* GLYPH_DEBUG */
17597
17598
17599 /* Display new lines. Set last_text_row to the last new line
17600 displayed which has text on it, i.e. might end up as being the
17601 line where the window_end_vpos is. */
17602 w->cursor.vpos = -1;
17603 last_text_row = NULL;
17604 overlay_arrow_seen = 0;
17605 while (it.current_y < it.last_visible_y
17606 && !fonts_changed_p
17607 && (first_unchanged_at_end_row == NULL
17608 || IT_CHARPOS (it) < stop_pos))
17609 {
17610 if (display_line (&it))
17611 last_text_row = it.glyph_row - 1;
17612 }
17613
17614 if (fonts_changed_p)
17615 return -1;
17616
17617
17618 /* Compute differences in buffer positions, y-positions etc. for
17619 lines reused at the bottom of the window. Compute what we can
17620 scroll. */
17621 if (first_unchanged_at_end_row
17622 /* No lines reused because we displayed everything up to the
17623 bottom of the window. */
17624 && it.current_y < it.last_visible_y)
17625 {
17626 dvpos = (it.vpos
17627 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17628 current_matrix));
17629 dy = it.current_y - first_unchanged_at_end_row->y;
17630 run.current_y = first_unchanged_at_end_row->y;
17631 run.desired_y = run.current_y + dy;
17632 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17633 }
17634 else
17635 {
17636 delta = delta_bytes = dvpos = dy
17637 = run.current_y = run.desired_y = run.height = 0;
17638 first_unchanged_at_end_row = NULL;
17639 }
17640 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17641
17642
17643 /* Find the cursor if not already found. We have to decide whether
17644 PT will appear on this window (it sometimes doesn't, but this is
17645 not a very frequent case.) This decision has to be made before
17646 the current matrix is altered. A value of cursor.vpos < 0 means
17647 that PT is either in one of the lines beginning at
17648 first_unchanged_at_end_row or below the window. Don't care for
17649 lines that might be displayed later at the window end; as
17650 mentioned, this is not a frequent case. */
17651 if (w->cursor.vpos < 0)
17652 {
17653 /* Cursor in unchanged rows at the top? */
17654 if (PT < CHARPOS (start_pos)
17655 && last_unchanged_at_beg_row)
17656 {
17657 row = row_containing_pos (w, PT,
17658 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17659 last_unchanged_at_beg_row + 1, 0);
17660 if (row)
17661 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17662 }
17663
17664 /* Start from first_unchanged_at_end_row looking for PT. */
17665 else if (first_unchanged_at_end_row)
17666 {
17667 row = row_containing_pos (w, PT - delta,
17668 first_unchanged_at_end_row, NULL, 0);
17669 if (row)
17670 set_cursor_from_row (w, row, w->current_matrix, delta,
17671 delta_bytes, dy, dvpos);
17672 }
17673
17674 /* Give up if cursor was not found. */
17675 if (w->cursor.vpos < 0)
17676 {
17677 clear_glyph_matrix (w->desired_matrix);
17678 return -1;
17679 }
17680 }
17681
17682 /* Don't let the cursor end in the scroll margins. */
17683 {
17684 int this_scroll_margin, cursor_height;
17685 int frame_line_height = default_line_pixel_height (w);
17686 int window_total_lines
17687 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17688
17689 this_scroll_margin =
17690 max (0, min (scroll_margin, window_total_lines / 4));
17691 this_scroll_margin *= frame_line_height;
17692 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17693
17694 if ((w->cursor.y < this_scroll_margin
17695 && CHARPOS (start) > BEGV)
17696 /* Old redisplay didn't take scroll margin into account at the bottom,
17697 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17698 || (w->cursor.y + (make_cursor_line_fully_visible_p
17699 ? cursor_height + this_scroll_margin
17700 : 1)) > it.last_visible_y)
17701 {
17702 w->cursor.vpos = -1;
17703 clear_glyph_matrix (w->desired_matrix);
17704 return -1;
17705 }
17706 }
17707
17708 /* Scroll the display. Do it before changing the current matrix so
17709 that xterm.c doesn't get confused about where the cursor glyph is
17710 found. */
17711 if (dy && run.height)
17712 {
17713 update_begin (f);
17714
17715 if (FRAME_WINDOW_P (f))
17716 {
17717 FRAME_RIF (f)->update_window_begin_hook (w);
17718 FRAME_RIF (f)->clear_window_mouse_face (w);
17719 FRAME_RIF (f)->scroll_run_hook (w, &run);
17720 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17721 }
17722 else
17723 {
17724 /* Terminal frame. In this case, dvpos gives the number of
17725 lines to scroll by; dvpos < 0 means scroll up. */
17726 int from_vpos
17727 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17728 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17729 int end = (WINDOW_TOP_EDGE_LINE (w)
17730 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17731 + window_internal_height (w));
17732
17733 #if defined (HAVE_GPM) || defined (MSDOS)
17734 x_clear_window_mouse_face (w);
17735 #endif
17736 /* Perform the operation on the screen. */
17737 if (dvpos > 0)
17738 {
17739 /* Scroll last_unchanged_at_beg_row to the end of the
17740 window down dvpos lines. */
17741 set_terminal_window (f, end);
17742
17743 /* On dumb terminals delete dvpos lines at the end
17744 before inserting dvpos empty lines. */
17745 if (!FRAME_SCROLL_REGION_OK (f))
17746 ins_del_lines (f, end - dvpos, -dvpos);
17747
17748 /* Insert dvpos empty lines in front of
17749 last_unchanged_at_beg_row. */
17750 ins_del_lines (f, from, dvpos);
17751 }
17752 else if (dvpos < 0)
17753 {
17754 /* Scroll up last_unchanged_at_beg_vpos to the end of
17755 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17756 set_terminal_window (f, end);
17757
17758 /* Delete dvpos lines in front of
17759 last_unchanged_at_beg_vpos. ins_del_lines will set
17760 the cursor to the given vpos and emit |dvpos| delete
17761 line sequences. */
17762 ins_del_lines (f, from + dvpos, dvpos);
17763
17764 /* On a dumb terminal insert dvpos empty lines at the
17765 end. */
17766 if (!FRAME_SCROLL_REGION_OK (f))
17767 ins_del_lines (f, end + dvpos, -dvpos);
17768 }
17769
17770 set_terminal_window (f, 0);
17771 }
17772
17773 update_end (f);
17774 }
17775
17776 /* Shift reused rows of the current matrix to the right position.
17777 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17778 text. */
17779 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17780 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17781 if (dvpos < 0)
17782 {
17783 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17784 bottom_vpos, dvpos);
17785 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17786 bottom_vpos);
17787 }
17788 else if (dvpos > 0)
17789 {
17790 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17791 bottom_vpos, dvpos);
17792 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17793 first_unchanged_at_end_vpos + dvpos);
17794 }
17795
17796 /* For frame-based redisplay, make sure that current frame and window
17797 matrix are in sync with respect to glyph memory. */
17798 if (!FRAME_WINDOW_P (f))
17799 sync_frame_with_window_matrix_rows (w);
17800
17801 /* Adjust buffer positions in reused rows. */
17802 if (delta || delta_bytes)
17803 increment_matrix_positions (current_matrix,
17804 first_unchanged_at_end_vpos + dvpos,
17805 bottom_vpos, delta, delta_bytes);
17806
17807 /* Adjust Y positions. */
17808 if (dy)
17809 shift_glyph_matrix (w, current_matrix,
17810 first_unchanged_at_end_vpos + dvpos,
17811 bottom_vpos, dy);
17812
17813 if (first_unchanged_at_end_row)
17814 {
17815 first_unchanged_at_end_row += dvpos;
17816 if (first_unchanged_at_end_row->y >= it.last_visible_y
17817 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17818 first_unchanged_at_end_row = NULL;
17819 }
17820
17821 /* If scrolling up, there may be some lines to display at the end of
17822 the window. */
17823 last_text_row_at_end = NULL;
17824 if (dy < 0)
17825 {
17826 /* Scrolling up can leave for example a partially visible line
17827 at the end of the window to be redisplayed. */
17828 /* Set last_row to the glyph row in the current matrix where the
17829 window end line is found. It has been moved up or down in
17830 the matrix by dvpos. */
17831 int last_vpos = w->window_end_vpos + dvpos;
17832 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17833
17834 /* If last_row is the window end line, it should display text. */
17835 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17836
17837 /* If window end line was partially visible before, begin
17838 displaying at that line. Otherwise begin displaying with the
17839 line following it. */
17840 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17841 {
17842 init_to_row_start (&it, w, last_row);
17843 it.vpos = last_vpos;
17844 it.current_y = last_row->y;
17845 }
17846 else
17847 {
17848 init_to_row_end (&it, w, last_row);
17849 it.vpos = 1 + last_vpos;
17850 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17851 ++last_row;
17852 }
17853
17854 /* We may start in a continuation line. If so, we have to
17855 get the right continuation_lines_width and current_x. */
17856 it.continuation_lines_width = last_row->continuation_lines_width;
17857 it.hpos = it.current_x = 0;
17858
17859 /* Display the rest of the lines at the window end. */
17860 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17861 while (it.current_y < it.last_visible_y
17862 && !fonts_changed_p)
17863 {
17864 /* Is it always sure that the display agrees with lines in
17865 the current matrix? I don't think so, so we mark rows
17866 displayed invalid in the current matrix by setting their
17867 enabled_p flag to zero. */
17868 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17869 if (display_line (&it))
17870 last_text_row_at_end = it.glyph_row - 1;
17871 }
17872 }
17873
17874 /* Update window_end_pos and window_end_vpos. */
17875 if (first_unchanged_at_end_row && !last_text_row_at_end)
17876 {
17877 /* Window end line if one of the preserved rows from the current
17878 matrix. Set row to the last row displaying text in current
17879 matrix starting at first_unchanged_at_end_row, after
17880 scrolling. */
17881 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17882 row = find_last_row_displaying_text (w->current_matrix, &it,
17883 first_unchanged_at_end_row);
17884 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17885 adjust_window_ends (w, row, 1);
17886 eassert (w->window_end_bytepos >= 0);
17887 IF_DEBUG (debug_method_add (w, "A"));
17888 }
17889 else if (last_text_row_at_end)
17890 {
17891 adjust_window_ends (w, last_text_row_at_end, 0);
17892 eassert (w->window_end_bytepos >= 0);
17893 IF_DEBUG (debug_method_add (w, "B"));
17894 }
17895 else if (last_text_row)
17896 {
17897 /* We have displayed either to the end of the window or at the
17898 end of the window, i.e. the last row with text is to be found
17899 in the desired matrix. */
17900 adjust_window_ends (w, last_text_row, 0);
17901 eassert (w->window_end_bytepos >= 0);
17902 }
17903 else if (first_unchanged_at_end_row == NULL
17904 && last_text_row == NULL
17905 && last_text_row_at_end == NULL)
17906 {
17907 /* Displayed to end of window, but no line containing text was
17908 displayed. Lines were deleted at the end of the window. */
17909 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17910 int vpos = w->window_end_vpos;
17911 struct glyph_row *current_row = current_matrix->rows + vpos;
17912 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17913
17914 for (row = NULL;
17915 row == NULL && vpos >= first_vpos;
17916 --vpos, --current_row, --desired_row)
17917 {
17918 if (desired_row->enabled_p)
17919 {
17920 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17921 row = desired_row;
17922 }
17923 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17924 row = current_row;
17925 }
17926
17927 eassert (row != NULL);
17928 w->window_end_vpos = vpos + 1;
17929 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17930 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17931 eassert (w->window_end_bytepos >= 0);
17932 IF_DEBUG (debug_method_add (w, "C"));
17933 }
17934 else
17935 emacs_abort ();
17936
17937 IF_DEBUG (debug_end_pos = w->window_end_pos;
17938 debug_end_vpos = w->window_end_vpos);
17939
17940 /* Record that display has not been completed. */
17941 w->window_end_valid = 0;
17942 w->desired_matrix->no_scrolling_p = 1;
17943 return 3;
17944
17945 #undef GIVE_UP
17946 }
17947
17948
17949 \f
17950 /***********************************************************************
17951 More debugging support
17952 ***********************************************************************/
17953
17954 #ifdef GLYPH_DEBUG
17955
17956 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17957 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17958 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17959
17960
17961 /* Dump the contents of glyph matrix MATRIX on stderr.
17962
17963 GLYPHS 0 means don't show glyph contents.
17964 GLYPHS 1 means show glyphs in short form
17965 GLYPHS > 1 means show glyphs in long form. */
17966
17967 void
17968 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17969 {
17970 int i;
17971 for (i = 0; i < matrix->nrows; ++i)
17972 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17973 }
17974
17975
17976 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17977 the glyph row and area where the glyph comes from. */
17978
17979 void
17980 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17981 {
17982 if (glyph->type == CHAR_GLYPH
17983 || glyph->type == GLYPHLESS_GLYPH)
17984 {
17985 fprintf (stderr,
17986 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17987 glyph - row->glyphs[TEXT_AREA],
17988 (glyph->type == CHAR_GLYPH
17989 ? 'C'
17990 : 'G'),
17991 glyph->charpos,
17992 (BUFFERP (glyph->object)
17993 ? 'B'
17994 : (STRINGP (glyph->object)
17995 ? 'S'
17996 : (INTEGERP (glyph->object)
17997 ? '0'
17998 : '-'))),
17999 glyph->pixel_width,
18000 glyph->u.ch,
18001 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18002 ? glyph->u.ch
18003 : '.'),
18004 glyph->face_id,
18005 glyph->left_box_line_p,
18006 glyph->right_box_line_p);
18007 }
18008 else if (glyph->type == STRETCH_GLYPH)
18009 {
18010 fprintf (stderr,
18011 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18012 glyph - row->glyphs[TEXT_AREA],
18013 'S',
18014 glyph->charpos,
18015 (BUFFERP (glyph->object)
18016 ? 'B'
18017 : (STRINGP (glyph->object)
18018 ? 'S'
18019 : (INTEGERP (glyph->object)
18020 ? '0'
18021 : '-'))),
18022 glyph->pixel_width,
18023 0,
18024 ' ',
18025 glyph->face_id,
18026 glyph->left_box_line_p,
18027 glyph->right_box_line_p);
18028 }
18029 else if (glyph->type == IMAGE_GLYPH)
18030 {
18031 fprintf (stderr,
18032 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18033 glyph - row->glyphs[TEXT_AREA],
18034 'I',
18035 glyph->charpos,
18036 (BUFFERP (glyph->object)
18037 ? 'B'
18038 : (STRINGP (glyph->object)
18039 ? 'S'
18040 : (INTEGERP (glyph->object)
18041 ? '0'
18042 : '-'))),
18043 glyph->pixel_width,
18044 glyph->u.img_id,
18045 '.',
18046 glyph->face_id,
18047 glyph->left_box_line_p,
18048 glyph->right_box_line_p);
18049 }
18050 else if (glyph->type == COMPOSITE_GLYPH)
18051 {
18052 fprintf (stderr,
18053 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18054 glyph - row->glyphs[TEXT_AREA],
18055 '+',
18056 glyph->charpos,
18057 (BUFFERP (glyph->object)
18058 ? 'B'
18059 : (STRINGP (glyph->object)
18060 ? 'S'
18061 : (INTEGERP (glyph->object)
18062 ? '0'
18063 : '-'))),
18064 glyph->pixel_width,
18065 glyph->u.cmp.id);
18066 if (glyph->u.cmp.automatic)
18067 fprintf (stderr,
18068 "[%d-%d]",
18069 glyph->slice.cmp.from, glyph->slice.cmp.to);
18070 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18071 glyph->face_id,
18072 glyph->left_box_line_p,
18073 glyph->right_box_line_p);
18074 }
18075 #ifdef HAVE_XWIDGETS
18076 else if (glyph->type == XWIDGET_GLYPH)
18077 {
18078 fprintf (stderr,
18079 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18080 glyph - row->glyphs[TEXT_AREA],
18081 'X',
18082 glyph->charpos,
18083 (BUFFERP (glyph->object)
18084 ? 'B'
18085 : (STRINGP (glyph->object)
18086 ? 'S'
18087 : '-')),
18088 glyph->pixel_width,
18089 glyph->u.xwidget,
18090 '.',
18091 glyph->face_id,
18092 glyph->left_box_line_p,
18093 glyph->right_box_line_p);
18094
18095 // printf("dump xwidget glyph\n");
18096 }
18097 #endif
18098 }
18099
18100
18101 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18102 GLYPHS 0 means don't show glyph contents.
18103 GLYPHS 1 means show glyphs in short form
18104 GLYPHS > 1 means show glyphs in long form. */
18105
18106 void
18107 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18108 {
18109 if (glyphs != 1)
18110 {
18111 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18112 fprintf (stderr, "==============================================================================\n");
18113
18114 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18115 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18116 vpos,
18117 MATRIX_ROW_START_CHARPOS (row),
18118 MATRIX_ROW_END_CHARPOS (row),
18119 row->used[TEXT_AREA],
18120 row->contains_overlapping_glyphs_p,
18121 row->enabled_p,
18122 row->truncated_on_left_p,
18123 row->truncated_on_right_p,
18124 row->continued_p,
18125 MATRIX_ROW_CONTINUATION_LINE_P (row),
18126 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18127 row->ends_at_zv_p,
18128 row->fill_line_p,
18129 row->ends_in_middle_of_char_p,
18130 row->starts_in_middle_of_char_p,
18131 row->mouse_face_p,
18132 row->x,
18133 row->y,
18134 row->pixel_width,
18135 row->height,
18136 row->visible_height,
18137 row->ascent,
18138 row->phys_ascent);
18139 /* The next 3 lines should align to "Start" in the header. */
18140 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18141 row->end.overlay_string_index,
18142 row->continuation_lines_width);
18143 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18144 CHARPOS (row->start.string_pos),
18145 CHARPOS (row->end.string_pos));
18146 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18147 row->end.dpvec_index);
18148 }
18149
18150 if (glyphs > 1)
18151 {
18152 int area;
18153
18154 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18155 {
18156 struct glyph *glyph = row->glyphs[area];
18157 struct glyph *glyph_end = glyph + row->used[area];
18158
18159 /* Glyph for a line end in text. */
18160 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18161 ++glyph_end;
18162
18163 if (glyph < glyph_end)
18164 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18165
18166 for (; glyph < glyph_end; ++glyph)
18167 dump_glyph (row, glyph, area);
18168 }
18169 }
18170 else if (glyphs == 1)
18171 {
18172 int area;
18173
18174 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18175 {
18176 char *s = alloca (row->used[area] + 4);
18177 int i;
18178
18179 for (i = 0; i < row->used[area]; ++i)
18180 {
18181 struct glyph *glyph = row->glyphs[area] + i;
18182 if (i == row->used[area] - 1
18183 && area == TEXT_AREA
18184 && INTEGERP (glyph->object)
18185 && glyph->type == CHAR_GLYPH
18186 && glyph->u.ch == ' ')
18187 {
18188 strcpy (&s[i], "[\\n]");
18189 i += 4;
18190 }
18191 else if (glyph->type == CHAR_GLYPH
18192 && glyph->u.ch < 0x80
18193 && glyph->u.ch >= ' ')
18194 s[i] = glyph->u.ch;
18195 else
18196 s[i] = '.';
18197 }
18198
18199 s[i] = '\0';
18200 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18201 }
18202 }
18203 }
18204
18205
18206 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18207 Sdump_glyph_matrix, 0, 1, "p",
18208 doc: /* Dump the current matrix of the selected window to stderr.
18209 Shows contents of glyph row structures. With non-nil
18210 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18211 glyphs in short form, otherwise show glyphs in long form. */)
18212 (Lisp_Object glyphs)
18213 {
18214 struct window *w = XWINDOW (selected_window);
18215 struct buffer *buffer = XBUFFER (w->contents);
18216
18217 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18218 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18219 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18220 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18221 fprintf (stderr, "=============================================\n");
18222 dump_glyph_matrix (w->current_matrix,
18223 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18224 return Qnil;
18225 }
18226
18227
18228 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18229 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18230 (void)
18231 {
18232 struct frame *f = XFRAME (selected_frame);
18233 dump_glyph_matrix (f->current_matrix, 1);
18234 return Qnil;
18235 }
18236
18237
18238 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18239 doc: /* Dump glyph row ROW to stderr.
18240 GLYPH 0 means don't dump glyphs.
18241 GLYPH 1 means dump glyphs in short form.
18242 GLYPH > 1 or omitted means dump glyphs in long form. */)
18243 (Lisp_Object row, Lisp_Object glyphs)
18244 {
18245 struct glyph_matrix *matrix;
18246 EMACS_INT vpos;
18247
18248 CHECK_NUMBER (row);
18249 matrix = XWINDOW (selected_window)->current_matrix;
18250 vpos = XINT (row);
18251 if (vpos >= 0 && vpos < matrix->nrows)
18252 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18253 vpos,
18254 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18255 return Qnil;
18256 }
18257
18258
18259 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18260 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18261 GLYPH 0 means don't dump glyphs.
18262 GLYPH 1 means dump glyphs in short form.
18263 GLYPH > 1 or omitted means dump glyphs in long form. */)
18264 (Lisp_Object row, Lisp_Object glyphs)
18265 {
18266 struct frame *sf = SELECTED_FRAME ();
18267 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18268 EMACS_INT vpos;
18269
18270 CHECK_NUMBER (row);
18271 vpos = XINT (row);
18272 if (vpos >= 0 && vpos < m->nrows)
18273 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18274 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18275 return Qnil;
18276 }
18277
18278
18279 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18280 doc: /* Toggle tracing of redisplay.
18281 With ARG, turn tracing on if and only if ARG is positive. */)
18282 (Lisp_Object arg)
18283 {
18284 if (NILP (arg))
18285 trace_redisplay_p = !trace_redisplay_p;
18286 else
18287 {
18288 arg = Fprefix_numeric_value (arg);
18289 trace_redisplay_p = XINT (arg) > 0;
18290 }
18291
18292 return Qnil;
18293 }
18294
18295
18296 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18297 doc: /* Like `format', but print result to stderr.
18298 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18299 (ptrdiff_t nargs, Lisp_Object *args)
18300 {
18301 Lisp_Object s = Fformat (nargs, args);
18302 fprintf (stderr, "%s", SDATA (s));
18303 return Qnil;
18304 }
18305
18306 #endif /* GLYPH_DEBUG */
18307
18308
18309 \f
18310 /***********************************************************************
18311 Building Desired Matrix Rows
18312 ***********************************************************************/
18313
18314 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18315 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18316
18317 static struct glyph_row *
18318 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18319 {
18320 struct frame *f = XFRAME (WINDOW_FRAME (w));
18321 struct buffer *buffer = XBUFFER (w->contents);
18322 struct buffer *old = current_buffer;
18323 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18324 int arrow_len = SCHARS (overlay_arrow_string);
18325 const unsigned char *arrow_end = arrow_string + arrow_len;
18326 const unsigned char *p;
18327 struct it it;
18328 bool multibyte_p;
18329 int n_glyphs_before;
18330
18331 set_buffer_temp (buffer);
18332 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18333 it.glyph_row->used[TEXT_AREA] = 0;
18334 SET_TEXT_POS (it.position, 0, 0);
18335
18336 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18337 p = arrow_string;
18338 while (p < arrow_end)
18339 {
18340 Lisp_Object face, ilisp;
18341
18342 /* Get the next character. */
18343 if (multibyte_p)
18344 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18345 else
18346 {
18347 it.c = it.char_to_display = *p, it.len = 1;
18348 if (! ASCII_CHAR_P (it.c))
18349 it.char_to_display = BYTE8_TO_CHAR (it.c);
18350 }
18351 p += it.len;
18352
18353 /* Get its face. */
18354 ilisp = make_number (p - arrow_string);
18355 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18356 it.face_id = compute_char_face (f, it.char_to_display, face);
18357
18358 /* Compute its width, get its glyphs. */
18359 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18360 SET_TEXT_POS (it.position, -1, -1);
18361 PRODUCE_GLYPHS (&it);
18362
18363 /* If this character doesn't fit any more in the line, we have
18364 to remove some glyphs. */
18365 if (it.current_x > it.last_visible_x)
18366 {
18367 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18368 break;
18369 }
18370 }
18371
18372 set_buffer_temp (old);
18373 return it.glyph_row;
18374 }
18375
18376
18377 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18378 glyphs to insert is determined by produce_special_glyphs. */
18379
18380 static void
18381 insert_left_trunc_glyphs (struct it *it)
18382 {
18383 struct it truncate_it;
18384 struct glyph *from, *end, *to, *toend;
18385
18386 eassert (!FRAME_WINDOW_P (it->f)
18387 || (!it->glyph_row->reversed_p
18388 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18389 || (it->glyph_row->reversed_p
18390 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18391
18392 /* Get the truncation glyphs. */
18393 truncate_it = *it;
18394 truncate_it.current_x = 0;
18395 truncate_it.face_id = DEFAULT_FACE_ID;
18396 truncate_it.glyph_row = &scratch_glyph_row;
18397 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18398 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18399 truncate_it.object = make_number (0);
18400 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18401
18402 /* Overwrite glyphs from IT with truncation glyphs. */
18403 if (!it->glyph_row->reversed_p)
18404 {
18405 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18406
18407 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18408 end = from + tused;
18409 to = it->glyph_row->glyphs[TEXT_AREA];
18410 toend = to + it->glyph_row->used[TEXT_AREA];
18411 if (FRAME_WINDOW_P (it->f))
18412 {
18413 /* On GUI frames, when variable-size fonts are displayed,
18414 the truncation glyphs may need more pixels than the row's
18415 glyphs they overwrite. We overwrite more glyphs to free
18416 enough screen real estate, and enlarge the stretch glyph
18417 on the right (see display_line), if there is one, to
18418 preserve the screen position of the truncation glyphs on
18419 the right. */
18420 int w = 0;
18421 struct glyph *g = to;
18422 short used;
18423
18424 /* The first glyph could be partially visible, in which case
18425 it->glyph_row->x will be negative. But we want the left
18426 truncation glyphs to be aligned at the left margin of the
18427 window, so we override the x coordinate at which the row
18428 will begin. */
18429 it->glyph_row->x = 0;
18430 while (g < toend && w < it->truncation_pixel_width)
18431 {
18432 w += g->pixel_width;
18433 ++g;
18434 }
18435 if (g - to - tused > 0)
18436 {
18437 memmove (to + tused, g, (toend - g) * sizeof(*g));
18438 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18439 }
18440 used = it->glyph_row->used[TEXT_AREA];
18441 if (it->glyph_row->truncated_on_right_p
18442 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18443 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18444 == STRETCH_GLYPH)
18445 {
18446 int extra = w - it->truncation_pixel_width;
18447
18448 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18449 }
18450 }
18451
18452 while (from < end)
18453 *to++ = *from++;
18454
18455 /* There may be padding glyphs left over. Overwrite them too. */
18456 if (!FRAME_WINDOW_P (it->f))
18457 {
18458 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18459 {
18460 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18461 while (from < end)
18462 *to++ = *from++;
18463 }
18464 }
18465
18466 if (to > toend)
18467 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18468 }
18469 else
18470 {
18471 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18472
18473 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18474 that back to front. */
18475 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18476 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18477 toend = it->glyph_row->glyphs[TEXT_AREA];
18478 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18479 if (FRAME_WINDOW_P (it->f))
18480 {
18481 int w = 0;
18482 struct glyph *g = to;
18483
18484 while (g >= toend && w < it->truncation_pixel_width)
18485 {
18486 w += g->pixel_width;
18487 --g;
18488 }
18489 if (to - g - tused > 0)
18490 to = g + tused;
18491 if (it->glyph_row->truncated_on_right_p
18492 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18493 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18494 {
18495 int extra = w - it->truncation_pixel_width;
18496
18497 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18498 }
18499 }
18500
18501 while (from >= end && to >= toend)
18502 *to-- = *from--;
18503 if (!FRAME_WINDOW_P (it->f))
18504 {
18505 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18506 {
18507 from =
18508 truncate_it.glyph_row->glyphs[TEXT_AREA]
18509 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18510 while (from >= end && to >= toend)
18511 *to-- = *from--;
18512 }
18513 }
18514 if (from >= end)
18515 {
18516 /* Need to free some room before prepending additional
18517 glyphs. */
18518 int move_by = from - end + 1;
18519 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18520 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18521
18522 for ( ; g >= g0; g--)
18523 g[move_by] = *g;
18524 while (from >= end)
18525 *to-- = *from--;
18526 it->glyph_row->used[TEXT_AREA] += move_by;
18527 }
18528 }
18529 }
18530
18531 /* Compute the hash code for ROW. */
18532 unsigned
18533 row_hash (struct glyph_row *row)
18534 {
18535 int area, k;
18536 unsigned hashval = 0;
18537
18538 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18539 for (k = 0; k < row->used[area]; ++k)
18540 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18541 + row->glyphs[area][k].u.val
18542 + row->glyphs[area][k].face_id
18543 + row->glyphs[area][k].padding_p
18544 + (row->glyphs[area][k].type << 2));
18545
18546 return hashval;
18547 }
18548
18549 /* Compute the pixel height and width of IT->glyph_row.
18550
18551 Most of the time, ascent and height of a display line will be equal
18552 to the max_ascent and max_height values of the display iterator
18553 structure. This is not the case if
18554
18555 1. We hit ZV without displaying anything. In this case, max_ascent
18556 and max_height will be zero.
18557
18558 2. We have some glyphs that don't contribute to the line height.
18559 (The glyph row flag contributes_to_line_height_p is for future
18560 pixmap extensions).
18561
18562 The first case is easily covered by using default values because in
18563 these cases, the line height does not really matter, except that it
18564 must not be zero. */
18565
18566 static void
18567 compute_line_metrics (struct it *it)
18568 {
18569 struct glyph_row *row = it->glyph_row;
18570
18571 if (FRAME_WINDOW_P (it->f))
18572 {
18573 int i, min_y, max_y;
18574
18575 /* The line may consist of one space only, that was added to
18576 place the cursor on it. If so, the row's height hasn't been
18577 computed yet. */
18578 if (row->height == 0)
18579 {
18580 if (it->max_ascent + it->max_descent == 0)
18581 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18582 row->ascent = it->max_ascent;
18583 row->height = it->max_ascent + it->max_descent;
18584 row->phys_ascent = it->max_phys_ascent;
18585 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18586 row->extra_line_spacing = it->max_extra_line_spacing;
18587 }
18588
18589 /* Compute the width of this line. */
18590 row->pixel_width = row->x;
18591 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18592 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18593
18594 eassert (row->pixel_width >= 0);
18595 eassert (row->ascent >= 0 && row->height > 0);
18596
18597 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18598 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18599
18600 /* If first line's physical ascent is larger than its logical
18601 ascent, use the physical ascent, and make the row taller.
18602 This makes accented characters fully visible. */
18603 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18604 && row->phys_ascent > row->ascent)
18605 {
18606 row->height += row->phys_ascent - row->ascent;
18607 row->ascent = row->phys_ascent;
18608 }
18609
18610 /* Compute how much of the line is visible. */
18611 row->visible_height = row->height;
18612
18613 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18614 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18615
18616 if (row->y < min_y)
18617 row->visible_height -= min_y - row->y;
18618 if (row->y + row->height > max_y)
18619 row->visible_height -= row->y + row->height - max_y;
18620 }
18621 else
18622 {
18623 row->pixel_width = row->used[TEXT_AREA];
18624 if (row->continued_p)
18625 row->pixel_width -= it->continuation_pixel_width;
18626 else if (row->truncated_on_right_p)
18627 row->pixel_width -= it->truncation_pixel_width;
18628 row->ascent = row->phys_ascent = 0;
18629 row->height = row->phys_height = row->visible_height = 1;
18630 row->extra_line_spacing = 0;
18631 }
18632
18633 /* Compute a hash code for this row. */
18634 row->hash = row_hash (row);
18635
18636 it->max_ascent = it->max_descent = 0;
18637 it->max_phys_ascent = it->max_phys_descent = 0;
18638 }
18639
18640
18641 /* Append one space to the glyph row of iterator IT if doing a
18642 window-based redisplay. The space has the same face as
18643 IT->face_id. Value is non-zero if a space was added.
18644
18645 This function is called to make sure that there is always one glyph
18646 at the end of a glyph row that the cursor can be set on under
18647 window-systems. (If there weren't such a glyph we would not know
18648 how wide and tall a box cursor should be displayed).
18649
18650 At the same time this space let's a nicely handle clearing to the
18651 end of the line if the row ends in italic text. */
18652
18653 static int
18654 append_space_for_newline (struct it *it, int default_face_p)
18655 {
18656 if (FRAME_WINDOW_P (it->f))
18657 {
18658 int n = it->glyph_row->used[TEXT_AREA];
18659
18660 if (it->glyph_row->glyphs[TEXT_AREA] + n
18661 < it->glyph_row->glyphs[1 + TEXT_AREA])
18662 {
18663 /* Save some values that must not be changed.
18664 Must save IT->c and IT->len because otherwise
18665 ITERATOR_AT_END_P wouldn't work anymore after
18666 append_space_for_newline has been called. */
18667 enum display_element_type saved_what = it->what;
18668 int saved_c = it->c, saved_len = it->len;
18669 int saved_char_to_display = it->char_to_display;
18670 int saved_x = it->current_x;
18671 int saved_face_id = it->face_id;
18672 int saved_box_end = it->end_of_box_run_p;
18673 struct text_pos saved_pos;
18674 Lisp_Object saved_object;
18675 struct face *face;
18676
18677 saved_object = it->object;
18678 saved_pos = it->position;
18679
18680 it->what = IT_CHARACTER;
18681 memset (&it->position, 0, sizeof it->position);
18682 it->object = make_number (0);
18683 it->c = it->char_to_display = ' ';
18684 it->len = 1;
18685
18686 /* If the default face was remapped, be sure to use the
18687 remapped face for the appended newline. */
18688 if (default_face_p)
18689 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18690 else if (it->face_before_selective_p)
18691 it->face_id = it->saved_face_id;
18692 face = FACE_FROM_ID (it->f, it->face_id);
18693 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18694 /* In R2L rows, we will prepend a stretch glyph that will
18695 have the end_of_box_run_p flag set for it, so there's no
18696 need for the appended newline glyph to have that flag
18697 set. */
18698 if (it->glyph_row->reversed_p
18699 /* But if the appended newline glyph goes all the way to
18700 the end of the row, there will be no stretch glyph,
18701 so leave the box flag set. */
18702 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18703 it->end_of_box_run_p = 0;
18704
18705 PRODUCE_GLYPHS (it);
18706
18707 it->override_ascent = -1;
18708 it->constrain_row_ascent_descent_p = 0;
18709 it->current_x = saved_x;
18710 it->object = saved_object;
18711 it->position = saved_pos;
18712 it->what = saved_what;
18713 it->face_id = saved_face_id;
18714 it->len = saved_len;
18715 it->c = saved_c;
18716 it->char_to_display = saved_char_to_display;
18717 it->end_of_box_run_p = saved_box_end;
18718 return 1;
18719 }
18720 }
18721
18722 return 0;
18723 }
18724
18725
18726 /* Extend the face of the last glyph in the text area of IT->glyph_row
18727 to the end of the display line. Called from display_line. If the
18728 glyph row is empty, add a space glyph to it so that we know the
18729 face to draw. Set the glyph row flag fill_line_p. If the glyph
18730 row is R2L, prepend a stretch glyph to cover the empty space to the
18731 left of the leftmost glyph. */
18732
18733 static void
18734 extend_face_to_end_of_line (struct it *it)
18735 {
18736 struct face *face, *default_face;
18737 struct frame *f = it->f;
18738
18739 /* If line is already filled, do nothing. Non window-system frames
18740 get a grace of one more ``pixel'' because their characters are
18741 1-``pixel'' wide, so they hit the equality too early. This grace
18742 is needed only for R2L rows that are not continued, to produce
18743 one extra blank where we could display the cursor. */
18744 if (it->current_x >= it->last_visible_x
18745 + (!FRAME_WINDOW_P (f)
18746 && it->glyph_row->reversed_p
18747 && !it->glyph_row->continued_p))
18748 return;
18749
18750 /* The default face, possibly remapped. */
18751 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18752
18753 /* Face extension extends the background and box of IT->face_id
18754 to the end of the line. If the background equals the background
18755 of the frame, we don't have to do anything. */
18756 if (it->face_before_selective_p)
18757 face = FACE_FROM_ID (f, it->saved_face_id);
18758 else
18759 face = FACE_FROM_ID (f, it->face_id);
18760
18761 if (FRAME_WINDOW_P (f)
18762 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18763 && face->box == FACE_NO_BOX
18764 && face->background == FRAME_BACKGROUND_PIXEL (f)
18765 && !face->stipple
18766 && !it->glyph_row->reversed_p)
18767 return;
18768
18769 /* Set the glyph row flag indicating that the face of the last glyph
18770 in the text area has to be drawn to the end of the text area. */
18771 it->glyph_row->fill_line_p = 1;
18772
18773 /* If current character of IT is not ASCII, make sure we have the
18774 ASCII face. This will be automatically undone the next time
18775 get_next_display_element returns a multibyte character. Note
18776 that the character will always be single byte in unibyte
18777 text. */
18778 if (!ASCII_CHAR_P (it->c))
18779 {
18780 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18781 }
18782
18783 if (FRAME_WINDOW_P (f))
18784 {
18785 /* If the row is empty, add a space with the current face of IT,
18786 so that we know which face to draw. */
18787 if (it->glyph_row->used[TEXT_AREA] == 0)
18788 {
18789 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18790 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18791 it->glyph_row->used[TEXT_AREA] = 1;
18792 }
18793 #ifdef HAVE_WINDOW_SYSTEM
18794 if (it->glyph_row->reversed_p)
18795 {
18796 /* Prepend a stretch glyph to the row, such that the
18797 rightmost glyph will be drawn flushed all the way to the
18798 right margin of the window. The stretch glyph that will
18799 occupy the empty space, if any, to the left of the
18800 glyphs. */
18801 struct font *font = face->font ? face->font : FRAME_FONT (f);
18802 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18803 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18804 struct glyph *g;
18805 int row_width, stretch_ascent, stretch_width;
18806 struct text_pos saved_pos;
18807 int saved_face_id, saved_avoid_cursor, saved_box_start;
18808
18809 for (row_width = 0, g = row_start; g < row_end; g++)
18810 row_width += g->pixel_width;
18811 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18812 if (stretch_width > 0)
18813 {
18814 stretch_ascent =
18815 (((it->ascent + it->descent)
18816 * FONT_BASE (font)) / FONT_HEIGHT (font));
18817 saved_pos = it->position;
18818 memset (&it->position, 0, sizeof it->position);
18819 saved_avoid_cursor = it->avoid_cursor_p;
18820 it->avoid_cursor_p = 1;
18821 saved_face_id = it->face_id;
18822 saved_box_start = it->start_of_box_run_p;
18823 /* The last row's stretch glyph should get the default
18824 face, to avoid painting the rest of the window with
18825 the region face, if the region ends at ZV. */
18826 if (it->glyph_row->ends_at_zv_p)
18827 it->face_id = default_face->id;
18828 else
18829 it->face_id = face->id;
18830 it->start_of_box_run_p = 0;
18831 append_stretch_glyph (it, make_number (0), stretch_width,
18832 it->ascent + it->descent, stretch_ascent);
18833 it->position = saved_pos;
18834 it->avoid_cursor_p = saved_avoid_cursor;
18835 it->face_id = saved_face_id;
18836 it->start_of_box_run_p = saved_box_start;
18837 }
18838 }
18839 #endif /* HAVE_WINDOW_SYSTEM */
18840 }
18841 else
18842 {
18843 /* Save some values that must not be changed. */
18844 int saved_x = it->current_x;
18845 struct text_pos saved_pos;
18846 Lisp_Object saved_object;
18847 enum display_element_type saved_what = it->what;
18848 int saved_face_id = it->face_id;
18849
18850 saved_object = it->object;
18851 saved_pos = it->position;
18852
18853 it->what = IT_CHARACTER;
18854 memset (&it->position, 0, sizeof it->position);
18855 it->object = make_number (0);
18856 it->c = it->char_to_display = ' ';
18857 it->len = 1;
18858 /* The last row's blank glyphs should get the default face, to
18859 avoid painting the rest of the window with the region face,
18860 if the region ends at ZV. */
18861 if (it->glyph_row->ends_at_zv_p)
18862 it->face_id = default_face->id;
18863 else
18864 it->face_id = face->id;
18865
18866 PRODUCE_GLYPHS (it);
18867
18868 while (it->current_x <= it->last_visible_x)
18869 PRODUCE_GLYPHS (it);
18870
18871 /* Don't count these blanks really. It would let us insert a left
18872 truncation glyph below and make us set the cursor on them, maybe. */
18873 it->current_x = saved_x;
18874 it->object = saved_object;
18875 it->position = saved_pos;
18876 it->what = saved_what;
18877 it->face_id = saved_face_id;
18878 }
18879 }
18880
18881
18882 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18883 trailing whitespace. */
18884
18885 static int
18886 trailing_whitespace_p (ptrdiff_t charpos)
18887 {
18888 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18889 int c = 0;
18890
18891 while (bytepos < ZV_BYTE
18892 && (c = FETCH_CHAR (bytepos),
18893 c == ' ' || c == '\t'))
18894 ++bytepos;
18895
18896 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18897 {
18898 if (bytepos != PT_BYTE)
18899 return 1;
18900 }
18901 return 0;
18902 }
18903
18904
18905 /* Highlight trailing whitespace, if any, in ROW. */
18906
18907 static void
18908 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18909 {
18910 int used = row->used[TEXT_AREA];
18911
18912 if (used)
18913 {
18914 struct glyph *start = row->glyphs[TEXT_AREA];
18915 struct glyph *glyph = start + used - 1;
18916
18917 if (row->reversed_p)
18918 {
18919 /* Right-to-left rows need to be processed in the opposite
18920 direction, so swap the edge pointers. */
18921 glyph = start;
18922 start = row->glyphs[TEXT_AREA] + used - 1;
18923 }
18924
18925 /* Skip over glyphs inserted to display the cursor at the
18926 end of a line, for extending the face of the last glyph
18927 to the end of the line on terminals, and for truncation
18928 and continuation glyphs. */
18929 if (!row->reversed_p)
18930 {
18931 while (glyph >= start
18932 && glyph->type == CHAR_GLYPH
18933 && INTEGERP (glyph->object))
18934 --glyph;
18935 }
18936 else
18937 {
18938 while (glyph <= start
18939 && glyph->type == CHAR_GLYPH
18940 && INTEGERP (glyph->object))
18941 ++glyph;
18942 }
18943
18944 /* If last glyph is a space or stretch, and it's trailing
18945 whitespace, set the face of all trailing whitespace glyphs in
18946 IT->glyph_row to `trailing-whitespace'. */
18947 if ((row->reversed_p ? glyph <= start : glyph >= start)
18948 && BUFFERP (glyph->object)
18949 && (glyph->type == STRETCH_GLYPH
18950 || (glyph->type == CHAR_GLYPH
18951 && glyph->u.ch == ' '))
18952 && trailing_whitespace_p (glyph->charpos))
18953 {
18954 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18955 if (face_id < 0)
18956 return;
18957
18958 if (!row->reversed_p)
18959 {
18960 while (glyph >= start
18961 && BUFFERP (glyph->object)
18962 && (glyph->type == STRETCH_GLYPH
18963 || (glyph->type == CHAR_GLYPH
18964 && glyph->u.ch == ' ')))
18965 (glyph--)->face_id = face_id;
18966 }
18967 else
18968 {
18969 while (glyph <= start
18970 && BUFFERP (glyph->object)
18971 && (glyph->type == STRETCH_GLYPH
18972 || (glyph->type == CHAR_GLYPH
18973 && glyph->u.ch == ' ')))
18974 (glyph++)->face_id = face_id;
18975 }
18976 }
18977 }
18978 }
18979
18980
18981 /* Value is non-zero if glyph row ROW should be
18982 considered to hold the buffer position CHARPOS. */
18983
18984 static int
18985 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18986 {
18987 int result = 1;
18988
18989 if (charpos == CHARPOS (row->end.pos)
18990 || charpos == MATRIX_ROW_END_CHARPOS (row))
18991 {
18992 /* Suppose the row ends on a string.
18993 Unless the row is continued, that means it ends on a newline
18994 in the string. If it's anything other than a display string
18995 (e.g., a before-string from an overlay), we don't want the
18996 cursor there. (This heuristic seems to give the optimal
18997 behavior for the various types of multi-line strings.)
18998 One exception: if the string has `cursor' property on one of
18999 its characters, we _do_ want the cursor there. */
19000 if (CHARPOS (row->end.string_pos) >= 0)
19001 {
19002 if (row->continued_p)
19003 result = 1;
19004 else
19005 {
19006 /* Check for `display' property. */
19007 struct glyph *beg = row->glyphs[TEXT_AREA];
19008 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19009 struct glyph *glyph;
19010
19011 result = 0;
19012 for (glyph = end; glyph >= beg; --glyph)
19013 if (STRINGP (glyph->object))
19014 {
19015 Lisp_Object prop
19016 = Fget_char_property (make_number (charpos),
19017 Qdisplay, Qnil);
19018 result =
19019 (!NILP (prop)
19020 && display_prop_string_p (prop, glyph->object));
19021 /* If there's a `cursor' property on one of the
19022 string's characters, this row is a cursor row,
19023 even though this is not a display string. */
19024 if (!result)
19025 {
19026 Lisp_Object s = glyph->object;
19027
19028 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19029 {
19030 ptrdiff_t gpos = glyph->charpos;
19031
19032 if (!NILP (Fget_char_property (make_number (gpos),
19033 Qcursor, s)))
19034 {
19035 result = 1;
19036 break;
19037 }
19038 }
19039 }
19040 break;
19041 }
19042 }
19043 }
19044 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19045 {
19046 /* If the row ends in middle of a real character,
19047 and the line is continued, we want the cursor here.
19048 That's because CHARPOS (ROW->end.pos) would equal
19049 PT if PT is before the character. */
19050 if (!row->ends_in_ellipsis_p)
19051 result = row->continued_p;
19052 else
19053 /* If the row ends in an ellipsis, then
19054 CHARPOS (ROW->end.pos) will equal point after the
19055 invisible text. We want that position to be displayed
19056 after the ellipsis. */
19057 result = 0;
19058 }
19059 /* If the row ends at ZV, display the cursor at the end of that
19060 row instead of at the start of the row below. */
19061 else if (row->ends_at_zv_p)
19062 result = 1;
19063 else
19064 result = 0;
19065 }
19066
19067 return result;
19068 }
19069
19070 /* Value is non-zero if glyph row ROW should be
19071 used to hold the cursor. */
19072
19073 static int
19074 cursor_row_p (struct glyph_row *row)
19075 {
19076 return row_for_charpos_p (row, PT);
19077 }
19078
19079 \f
19080
19081 /* Push the property PROP so that it will be rendered at the current
19082 position in IT. Return 1 if PROP was successfully pushed, 0
19083 otherwise. Called from handle_line_prefix to handle the
19084 `line-prefix' and `wrap-prefix' properties. */
19085
19086 static int
19087 push_prefix_prop (struct it *it, Lisp_Object prop)
19088 {
19089 struct text_pos pos =
19090 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19091
19092 eassert (it->method == GET_FROM_BUFFER
19093 || it->method == GET_FROM_DISPLAY_VECTOR
19094 || it->method == GET_FROM_STRING);
19095
19096 /* We need to save the current buffer/string position, so it will be
19097 restored by pop_it, because iterate_out_of_display_property
19098 depends on that being set correctly, but some situations leave
19099 it->position not yet set when this function is called. */
19100 push_it (it, &pos);
19101
19102 if (STRINGP (prop))
19103 {
19104 if (SCHARS (prop) == 0)
19105 {
19106 pop_it (it);
19107 return 0;
19108 }
19109
19110 it->string = prop;
19111 it->string_from_prefix_prop_p = 1;
19112 it->multibyte_p = STRING_MULTIBYTE (it->string);
19113 it->current.overlay_string_index = -1;
19114 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19115 it->end_charpos = it->string_nchars = SCHARS (it->string);
19116 it->method = GET_FROM_STRING;
19117 it->stop_charpos = 0;
19118 it->prev_stop = 0;
19119 it->base_level_stop = 0;
19120
19121 /* Force paragraph direction to be that of the parent
19122 buffer/string. */
19123 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19124 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19125 else
19126 it->paragraph_embedding = L2R;
19127
19128 /* Set up the bidi iterator for this display string. */
19129 if (it->bidi_p)
19130 {
19131 it->bidi_it.string.lstring = it->string;
19132 it->bidi_it.string.s = NULL;
19133 it->bidi_it.string.schars = it->end_charpos;
19134 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19135 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19136 it->bidi_it.string.unibyte = !it->multibyte_p;
19137 it->bidi_it.w = it->w;
19138 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19139 }
19140 }
19141 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19142 {
19143 it->method = GET_FROM_STRETCH;
19144 it->object = prop;
19145 }
19146 #ifdef HAVE_WINDOW_SYSTEM
19147 else if (IMAGEP (prop))
19148 {
19149 it->what = IT_IMAGE;
19150 it->image_id = lookup_image (it->f, prop);
19151 it->method = GET_FROM_IMAGE;
19152 }
19153 #endif /* HAVE_WINDOW_SYSTEM */
19154 else
19155 {
19156 pop_it (it); /* bogus display property, give up */
19157 return 0;
19158 }
19159
19160 return 1;
19161 }
19162
19163 /* Return the character-property PROP at the current position in IT. */
19164
19165 static Lisp_Object
19166 get_it_property (struct it *it, Lisp_Object prop)
19167 {
19168 Lisp_Object position, object = it->object;
19169
19170 if (STRINGP (object))
19171 position = make_number (IT_STRING_CHARPOS (*it));
19172 else if (BUFFERP (object))
19173 {
19174 position = make_number (IT_CHARPOS (*it));
19175 object = it->window;
19176 }
19177 else
19178 return Qnil;
19179
19180 return Fget_char_property (position, prop, object);
19181 }
19182
19183 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19184
19185 static void
19186 handle_line_prefix (struct it *it)
19187 {
19188 Lisp_Object prefix;
19189
19190 if (it->continuation_lines_width > 0)
19191 {
19192 prefix = get_it_property (it, Qwrap_prefix);
19193 if (NILP (prefix))
19194 prefix = Vwrap_prefix;
19195 }
19196 else
19197 {
19198 prefix = get_it_property (it, Qline_prefix);
19199 if (NILP (prefix))
19200 prefix = Vline_prefix;
19201 }
19202 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19203 {
19204 /* If the prefix is wider than the window, and we try to wrap
19205 it, it would acquire its own wrap prefix, and so on till the
19206 iterator stack overflows. So, don't wrap the prefix. */
19207 it->line_wrap = TRUNCATE;
19208 it->avoid_cursor_p = 1;
19209 }
19210 }
19211
19212 \f
19213
19214 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19215 only for R2L lines from display_line and display_string, when they
19216 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19217 the line/string needs to be continued on the next glyph row. */
19218 static void
19219 unproduce_glyphs (struct it *it, int n)
19220 {
19221 struct glyph *glyph, *end;
19222
19223 eassert (it->glyph_row);
19224 eassert (it->glyph_row->reversed_p);
19225 eassert (it->area == TEXT_AREA);
19226 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19227
19228 if (n > it->glyph_row->used[TEXT_AREA])
19229 n = it->glyph_row->used[TEXT_AREA];
19230 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19231 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19232 for ( ; glyph < end; glyph++)
19233 glyph[-n] = *glyph;
19234 }
19235
19236 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19237 and ROW->maxpos. */
19238 static void
19239 find_row_edges (struct it *it, struct glyph_row *row,
19240 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19241 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19242 {
19243 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19244 lines' rows is implemented for bidi-reordered rows. */
19245
19246 /* ROW->minpos is the value of min_pos, the minimal buffer position
19247 we have in ROW, or ROW->start.pos if that is smaller. */
19248 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19249 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19250 else
19251 /* We didn't find buffer positions smaller than ROW->start, or
19252 didn't find _any_ valid buffer positions in any of the glyphs,
19253 so we must trust the iterator's computed positions. */
19254 row->minpos = row->start.pos;
19255 if (max_pos <= 0)
19256 {
19257 max_pos = CHARPOS (it->current.pos);
19258 max_bpos = BYTEPOS (it->current.pos);
19259 }
19260
19261 /* Here are the various use-cases for ending the row, and the
19262 corresponding values for ROW->maxpos:
19263
19264 Line ends in a newline from buffer eol_pos + 1
19265 Line is continued from buffer max_pos + 1
19266 Line is truncated on right it->current.pos
19267 Line ends in a newline from string max_pos + 1(*)
19268 (*) + 1 only when line ends in a forward scan
19269 Line is continued from string max_pos
19270 Line is continued from display vector max_pos
19271 Line is entirely from a string min_pos == max_pos
19272 Line is entirely from a display vector min_pos == max_pos
19273 Line that ends at ZV ZV
19274
19275 If you discover other use-cases, please add them here as
19276 appropriate. */
19277 if (row->ends_at_zv_p)
19278 row->maxpos = it->current.pos;
19279 else if (row->used[TEXT_AREA])
19280 {
19281 int seen_this_string = 0;
19282 struct glyph_row *r1 = row - 1;
19283
19284 /* Did we see the same display string on the previous row? */
19285 if (STRINGP (it->object)
19286 /* this is not the first row */
19287 && row > it->w->desired_matrix->rows
19288 /* previous row is not the header line */
19289 && !r1->mode_line_p
19290 /* previous row also ends in a newline from a string */
19291 && r1->ends_in_newline_from_string_p)
19292 {
19293 struct glyph *start, *end;
19294
19295 /* Search for the last glyph of the previous row that came
19296 from buffer or string. Depending on whether the row is
19297 L2R or R2L, we need to process it front to back or the
19298 other way round. */
19299 if (!r1->reversed_p)
19300 {
19301 start = r1->glyphs[TEXT_AREA];
19302 end = start + r1->used[TEXT_AREA];
19303 /* Glyphs inserted by redisplay have an integer (zero)
19304 as their object. */
19305 while (end > start
19306 && INTEGERP ((end - 1)->object)
19307 && (end - 1)->charpos <= 0)
19308 --end;
19309 if (end > start)
19310 {
19311 if (EQ ((end - 1)->object, it->object))
19312 seen_this_string = 1;
19313 }
19314 else
19315 /* If all the glyphs of the previous row were inserted
19316 by redisplay, it means the previous row was
19317 produced from a single newline, which is only
19318 possible if that newline came from the same string
19319 as the one which produced this ROW. */
19320 seen_this_string = 1;
19321 }
19322 else
19323 {
19324 end = r1->glyphs[TEXT_AREA] - 1;
19325 start = end + r1->used[TEXT_AREA];
19326 while (end < start
19327 && INTEGERP ((end + 1)->object)
19328 && (end + 1)->charpos <= 0)
19329 ++end;
19330 if (end < start)
19331 {
19332 if (EQ ((end + 1)->object, it->object))
19333 seen_this_string = 1;
19334 }
19335 else
19336 seen_this_string = 1;
19337 }
19338 }
19339 /* Take note of each display string that covers a newline only
19340 once, the first time we see it. This is for when a display
19341 string includes more than one newline in it. */
19342 if (row->ends_in_newline_from_string_p && !seen_this_string)
19343 {
19344 /* If we were scanning the buffer forward when we displayed
19345 the string, we want to account for at least one buffer
19346 position that belongs to this row (position covered by
19347 the display string), so that cursor positioning will
19348 consider this row as a candidate when point is at the end
19349 of the visual line represented by this row. This is not
19350 required when scanning back, because max_pos will already
19351 have a much larger value. */
19352 if (CHARPOS (row->end.pos) > max_pos)
19353 INC_BOTH (max_pos, max_bpos);
19354 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19355 }
19356 else if (CHARPOS (it->eol_pos) > 0)
19357 SET_TEXT_POS (row->maxpos,
19358 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19359 else if (row->continued_p)
19360 {
19361 /* If max_pos is different from IT's current position, it
19362 means IT->method does not belong to the display element
19363 at max_pos. However, it also means that the display
19364 element at max_pos was displayed in its entirety on this
19365 line, which is equivalent to saying that the next line
19366 starts at the next buffer position. */
19367 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19368 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19369 else
19370 {
19371 INC_BOTH (max_pos, max_bpos);
19372 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19373 }
19374 }
19375 else if (row->truncated_on_right_p)
19376 /* display_line already called reseat_at_next_visible_line_start,
19377 which puts the iterator at the beginning of the next line, in
19378 the logical order. */
19379 row->maxpos = it->current.pos;
19380 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19381 /* A line that is entirely from a string/image/stretch... */
19382 row->maxpos = row->minpos;
19383 else
19384 emacs_abort ();
19385 }
19386 else
19387 row->maxpos = it->current.pos;
19388 }
19389
19390 /* Construct the glyph row IT->glyph_row in the desired matrix of
19391 IT->w from text at the current position of IT. See dispextern.h
19392 for an overview of struct it. Value is non-zero if
19393 IT->glyph_row displays text, as opposed to a line displaying ZV
19394 only. */
19395
19396 static int
19397 display_line (struct it *it)
19398 {
19399 struct glyph_row *row = it->glyph_row;
19400 Lisp_Object overlay_arrow_string;
19401 struct it wrap_it;
19402 void *wrap_data = NULL;
19403 int may_wrap = 0, wrap_x IF_LINT (= 0);
19404 int wrap_row_used = -1;
19405 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19406 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19407 int wrap_row_extra_line_spacing IF_LINT (= 0);
19408 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19409 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19410 int cvpos;
19411 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19412 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19413
19414 /* We always start displaying at hpos zero even if hscrolled. */
19415 eassert (it->hpos == 0 && it->current_x == 0);
19416
19417 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19418 >= it->w->desired_matrix->nrows)
19419 {
19420 it->w->nrows_scale_factor++;
19421 fonts_changed_p = 1;
19422 return 0;
19423 }
19424
19425 /* Is IT->w showing the region? */
19426 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19427
19428 /* Clear the result glyph row and enable it. */
19429 prepare_desired_row (row);
19430
19431 row->y = it->current_y;
19432 row->start = it->start;
19433 row->continuation_lines_width = it->continuation_lines_width;
19434 row->displays_text_p = 1;
19435 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19436 it->starts_in_middle_of_char_p = 0;
19437
19438 /* Arrange the overlays nicely for our purposes. Usually, we call
19439 display_line on only one line at a time, in which case this
19440 can't really hurt too much, or we call it on lines which appear
19441 one after another in the buffer, in which case all calls to
19442 recenter_overlay_lists but the first will be pretty cheap. */
19443 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19444
19445 /* Move over display elements that are not visible because we are
19446 hscrolled. This may stop at an x-position < IT->first_visible_x
19447 if the first glyph is partially visible or if we hit a line end. */
19448 if (it->current_x < it->first_visible_x)
19449 {
19450 enum move_it_result move_result;
19451
19452 this_line_min_pos = row->start.pos;
19453 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19454 MOVE_TO_POS | MOVE_TO_X);
19455 /* If we are under a large hscroll, move_it_in_display_line_to
19456 could hit the end of the line without reaching
19457 it->first_visible_x. Pretend that we did reach it. This is
19458 especially important on a TTY, where we will call
19459 extend_face_to_end_of_line, which needs to know how many
19460 blank glyphs to produce. */
19461 if (it->current_x < it->first_visible_x
19462 && (move_result == MOVE_NEWLINE_OR_CR
19463 || move_result == MOVE_POS_MATCH_OR_ZV))
19464 it->current_x = it->first_visible_x;
19465
19466 /* Record the smallest positions seen while we moved over
19467 display elements that are not visible. This is needed by
19468 redisplay_internal for optimizing the case where the cursor
19469 stays inside the same line. The rest of this function only
19470 considers positions that are actually displayed, so
19471 RECORD_MAX_MIN_POS will not otherwise record positions that
19472 are hscrolled to the left of the left edge of the window. */
19473 min_pos = CHARPOS (this_line_min_pos);
19474 min_bpos = BYTEPOS (this_line_min_pos);
19475 }
19476 else
19477 {
19478 /* We only do this when not calling `move_it_in_display_line_to'
19479 above, because move_it_in_display_line_to calls
19480 handle_line_prefix itself. */
19481 handle_line_prefix (it);
19482 }
19483
19484 /* Get the initial row height. This is either the height of the
19485 text hscrolled, if there is any, or zero. */
19486 row->ascent = it->max_ascent;
19487 row->height = it->max_ascent + it->max_descent;
19488 row->phys_ascent = it->max_phys_ascent;
19489 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19490 row->extra_line_spacing = it->max_extra_line_spacing;
19491
19492 /* Utility macro to record max and min buffer positions seen until now. */
19493 #define RECORD_MAX_MIN_POS(IT) \
19494 do \
19495 { \
19496 int composition_p = !STRINGP ((IT)->string) \
19497 && ((IT)->what == IT_COMPOSITION); \
19498 ptrdiff_t current_pos = \
19499 composition_p ? (IT)->cmp_it.charpos \
19500 : IT_CHARPOS (*(IT)); \
19501 ptrdiff_t current_bpos = \
19502 composition_p ? CHAR_TO_BYTE (current_pos) \
19503 : IT_BYTEPOS (*(IT)); \
19504 if (current_pos < min_pos) \
19505 { \
19506 min_pos = current_pos; \
19507 min_bpos = current_bpos; \
19508 } \
19509 if (IT_CHARPOS (*it) > max_pos) \
19510 { \
19511 max_pos = IT_CHARPOS (*it); \
19512 max_bpos = IT_BYTEPOS (*it); \
19513 } \
19514 } \
19515 while (0)
19516
19517 /* Loop generating characters. The loop is left with IT on the next
19518 character to display. */
19519 while (1)
19520 {
19521 int n_glyphs_before, hpos_before, x_before;
19522 int x, nglyphs;
19523 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19524
19525 /* Retrieve the next thing to display. Value is zero if end of
19526 buffer reached. */
19527 if (!get_next_display_element (it))
19528 {
19529 /* Maybe add a space at the end of this line that is used to
19530 display the cursor there under X. Set the charpos of the
19531 first glyph of blank lines not corresponding to any text
19532 to -1. */
19533 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19534 row->exact_window_width_line_p = 1;
19535 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19536 || row->used[TEXT_AREA] == 0)
19537 {
19538 row->glyphs[TEXT_AREA]->charpos = -1;
19539 row->displays_text_p = 0;
19540
19541 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19542 && (!MINI_WINDOW_P (it->w)
19543 || (minibuf_level && EQ (it->window, minibuf_window))))
19544 row->indicate_empty_line_p = 1;
19545 }
19546
19547 it->continuation_lines_width = 0;
19548 row->ends_at_zv_p = 1;
19549 /* A row that displays right-to-left text must always have
19550 its last face extended all the way to the end of line,
19551 even if this row ends in ZV, because we still write to
19552 the screen left to right. We also need to extend the
19553 last face if the default face is remapped to some
19554 different face, otherwise the functions that clear
19555 portions of the screen will clear with the default face's
19556 background color. */
19557 if (row->reversed_p
19558 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19559 extend_face_to_end_of_line (it);
19560 break;
19561 }
19562
19563 /* Now, get the metrics of what we want to display. This also
19564 generates glyphs in `row' (which is IT->glyph_row). */
19565 n_glyphs_before = row->used[TEXT_AREA];
19566 x = it->current_x;
19567
19568 /* Remember the line height so far in case the next element doesn't
19569 fit on the line. */
19570 if (it->line_wrap != TRUNCATE)
19571 {
19572 ascent = it->max_ascent;
19573 descent = it->max_descent;
19574 phys_ascent = it->max_phys_ascent;
19575 phys_descent = it->max_phys_descent;
19576
19577 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19578 {
19579 if (IT_DISPLAYING_WHITESPACE (it))
19580 may_wrap = 1;
19581 else if (may_wrap)
19582 {
19583 SAVE_IT (wrap_it, *it, wrap_data);
19584 wrap_x = x;
19585 wrap_row_used = row->used[TEXT_AREA];
19586 wrap_row_ascent = row->ascent;
19587 wrap_row_height = row->height;
19588 wrap_row_phys_ascent = row->phys_ascent;
19589 wrap_row_phys_height = row->phys_height;
19590 wrap_row_extra_line_spacing = row->extra_line_spacing;
19591 wrap_row_min_pos = min_pos;
19592 wrap_row_min_bpos = min_bpos;
19593 wrap_row_max_pos = max_pos;
19594 wrap_row_max_bpos = max_bpos;
19595 may_wrap = 0;
19596 }
19597 }
19598 }
19599
19600 PRODUCE_GLYPHS (it);
19601
19602 /* If this display element was in marginal areas, continue with
19603 the next one. */
19604 if (it->area != TEXT_AREA)
19605 {
19606 row->ascent = max (row->ascent, it->max_ascent);
19607 row->height = max (row->height, it->max_ascent + it->max_descent);
19608 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19609 row->phys_height = max (row->phys_height,
19610 it->max_phys_ascent + it->max_phys_descent);
19611 row->extra_line_spacing = max (row->extra_line_spacing,
19612 it->max_extra_line_spacing);
19613 set_iterator_to_next (it, 1);
19614 continue;
19615 }
19616
19617 /* Does the display element fit on the line? If we truncate
19618 lines, we should draw past the right edge of the window. If
19619 we don't truncate, we want to stop so that we can display the
19620 continuation glyph before the right margin. If lines are
19621 continued, there are two possible strategies for characters
19622 resulting in more than 1 glyph (e.g. tabs): Display as many
19623 glyphs as possible in this line and leave the rest for the
19624 continuation line, or display the whole element in the next
19625 line. Original redisplay did the former, so we do it also. */
19626 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19627 hpos_before = it->hpos;
19628 x_before = x;
19629
19630 if (/* Not a newline. */
19631 nglyphs > 0
19632 /* Glyphs produced fit entirely in the line. */
19633 && it->current_x < it->last_visible_x)
19634 {
19635 it->hpos += nglyphs;
19636 row->ascent = max (row->ascent, it->max_ascent);
19637 row->height = max (row->height, it->max_ascent + it->max_descent);
19638 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19639 row->phys_height = max (row->phys_height,
19640 it->max_phys_ascent + it->max_phys_descent);
19641 row->extra_line_spacing = max (row->extra_line_spacing,
19642 it->max_extra_line_spacing);
19643 if (it->current_x - it->pixel_width < it->first_visible_x)
19644 row->x = x - it->first_visible_x;
19645 /* Record the maximum and minimum buffer positions seen so
19646 far in glyphs that will be displayed by this row. */
19647 if (it->bidi_p)
19648 RECORD_MAX_MIN_POS (it);
19649 }
19650 else
19651 {
19652 int i, new_x;
19653 struct glyph *glyph;
19654
19655 for (i = 0; i < nglyphs; ++i, x = new_x)
19656 {
19657 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19658 new_x = x + glyph->pixel_width;
19659
19660 if (/* Lines are continued. */
19661 it->line_wrap != TRUNCATE
19662 && (/* Glyph doesn't fit on the line. */
19663 new_x > it->last_visible_x
19664 /* Or it fits exactly on a window system frame. */
19665 || (new_x == it->last_visible_x
19666 && FRAME_WINDOW_P (it->f)
19667 && (row->reversed_p
19668 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19669 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19670 {
19671 /* End of a continued line. */
19672
19673 if (it->hpos == 0
19674 || (new_x == it->last_visible_x
19675 && FRAME_WINDOW_P (it->f)
19676 && (row->reversed_p
19677 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19678 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19679 {
19680 /* Current glyph is the only one on the line or
19681 fits exactly on the line. We must continue
19682 the line because we can't draw the cursor
19683 after the glyph. */
19684 row->continued_p = 1;
19685 it->current_x = new_x;
19686 it->continuation_lines_width += new_x;
19687 ++it->hpos;
19688 if (i == nglyphs - 1)
19689 {
19690 /* If line-wrap is on, check if a previous
19691 wrap point was found. */
19692 if (wrap_row_used > 0
19693 /* Even if there is a previous wrap
19694 point, continue the line here as
19695 usual, if (i) the previous character
19696 was a space or tab AND (ii) the
19697 current character is not. */
19698 && (!may_wrap
19699 || IT_DISPLAYING_WHITESPACE (it)))
19700 goto back_to_wrap;
19701
19702 /* Record the maximum and minimum buffer
19703 positions seen so far in glyphs that will be
19704 displayed by this row. */
19705 if (it->bidi_p)
19706 RECORD_MAX_MIN_POS (it);
19707 set_iterator_to_next (it, 1);
19708 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19709 {
19710 if (!get_next_display_element (it))
19711 {
19712 row->exact_window_width_line_p = 1;
19713 it->continuation_lines_width = 0;
19714 row->continued_p = 0;
19715 row->ends_at_zv_p = 1;
19716 }
19717 else if (ITERATOR_AT_END_OF_LINE_P (it))
19718 {
19719 row->continued_p = 0;
19720 row->exact_window_width_line_p = 1;
19721 }
19722 }
19723 }
19724 else if (it->bidi_p)
19725 RECORD_MAX_MIN_POS (it);
19726 }
19727 else if (CHAR_GLYPH_PADDING_P (*glyph)
19728 && !FRAME_WINDOW_P (it->f))
19729 {
19730 /* A padding glyph that doesn't fit on this line.
19731 This means the whole character doesn't fit
19732 on the line. */
19733 if (row->reversed_p)
19734 unproduce_glyphs (it, row->used[TEXT_AREA]
19735 - n_glyphs_before);
19736 row->used[TEXT_AREA] = n_glyphs_before;
19737
19738 /* Fill the rest of the row with continuation
19739 glyphs like in 20.x. */
19740 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19741 < row->glyphs[1 + TEXT_AREA])
19742 produce_special_glyphs (it, IT_CONTINUATION);
19743
19744 row->continued_p = 1;
19745 it->current_x = x_before;
19746 it->continuation_lines_width += x_before;
19747
19748 /* Restore the height to what it was before the
19749 element not fitting on the line. */
19750 it->max_ascent = ascent;
19751 it->max_descent = descent;
19752 it->max_phys_ascent = phys_ascent;
19753 it->max_phys_descent = phys_descent;
19754 }
19755 else if (wrap_row_used > 0)
19756 {
19757 back_to_wrap:
19758 if (row->reversed_p)
19759 unproduce_glyphs (it,
19760 row->used[TEXT_AREA] - wrap_row_used);
19761 RESTORE_IT (it, &wrap_it, wrap_data);
19762 it->continuation_lines_width += wrap_x;
19763 row->used[TEXT_AREA] = wrap_row_used;
19764 row->ascent = wrap_row_ascent;
19765 row->height = wrap_row_height;
19766 row->phys_ascent = wrap_row_phys_ascent;
19767 row->phys_height = wrap_row_phys_height;
19768 row->extra_line_spacing = wrap_row_extra_line_spacing;
19769 min_pos = wrap_row_min_pos;
19770 min_bpos = wrap_row_min_bpos;
19771 max_pos = wrap_row_max_pos;
19772 max_bpos = wrap_row_max_bpos;
19773 row->continued_p = 1;
19774 row->ends_at_zv_p = 0;
19775 row->exact_window_width_line_p = 0;
19776 it->continuation_lines_width += x;
19777
19778 /* Make sure that a non-default face is extended
19779 up to the right margin of the window. */
19780 extend_face_to_end_of_line (it);
19781 }
19782 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19783 {
19784 /* A TAB that extends past the right edge of the
19785 window. This produces a single glyph on
19786 window system frames. We leave the glyph in
19787 this row and let it fill the row, but don't
19788 consume the TAB. */
19789 if ((row->reversed_p
19790 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19791 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19792 produce_special_glyphs (it, IT_CONTINUATION);
19793 it->continuation_lines_width += it->last_visible_x;
19794 row->ends_in_middle_of_char_p = 1;
19795 row->continued_p = 1;
19796 glyph->pixel_width = it->last_visible_x - x;
19797 it->starts_in_middle_of_char_p = 1;
19798 }
19799 else
19800 {
19801 /* Something other than a TAB that draws past
19802 the right edge of the window. Restore
19803 positions to values before the element. */
19804 if (row->reversed_p)
19805 unproduce_glyphs (it, row->used[TEXT_AREA]
19806 - (n_glyphs_before + i));
19807 row->used[TEXT_AREA] = n_glyphs_before + i;
19808
19809 /* Display continuation glyphs. */
19810 it->current_x = x_before;
19811 it->continuation_lines_width += x;
19812 if (!FRAME_WINDOW_P (it->f)
19813 || (row->reversed_p
19814 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19815 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19816 produce_special_glyphs (it, IT_CONTINUATION);
19817 row->continued_p = 1;
19818
19819 extend_face_to_end_of_line (it);
19820
19821 if (nglyphs > 1 && i > 0)
19822 {
19823 row->ends_in_middle_of_char_p = 1;
19824 it->starts_in_middle_of_char_p = 1;
19825 }
19826
19827 /* Restore the height to what it was before the
19828 element not fitting on the line. */
19829 it->max_ascent = ascent;
19830 it->max_descent = descent;
19831 it->max_phys_ascent = phys_ascent;
19832 it->max_phys_descent = phys_descent;
19833 }
19834
19835 break;
19836 }
19837 else if (new_x > it->first_visible_x)
19838 {
19839 /* Increment number of glyphs actually displayed. */
19840 ++it->hpos;
19841
19842 /* Record the maximum and minimum buffer positions
19843 seen so far in glyphs that will be displayed by
19844 this row. */
19845 if (it->bidi_p)
19846 RECORD_MAX_MIN_POS (it);
19847
19848 if (x < it->first_visible_x)
19849 /* Glyph is partially visible, i.e. row starts at
19850 negative X position. */
19851 row->x = x - it->first_visible_x;
19852 }
19853 else
19854 {
19855 /* Glyph is completely off the left margin of the
19856 window. This should not happen because of the
19857 move_it_in_display_line at the start of this
19858 function, unless the text display area of the
19859 window is empty. */
19860 eassert (it->first_visible_x <= it->last_visible_x);
19861 }
19862 }
19863 /* Even if this display element produced no glyphs at all,
19864 we want to record its position. */
19865 if (it->bidi_p && nglyphs == 0)
19866 RECORD_MAX_MIN_POS (it);
19867
19868 row->ascent = max (row->ascent, it->max_ascent);
19869 row->height = max (row->height, it->max_ascent + it->max_descent);
19870 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19871 row->phys_height = max (row->phys_height,
19872 it->max_phys_ascent + it->max_phys_descent);
19873 row->extra_line_spacing = max (row->extra_line_spacing,
19874 it->max_extra_line_spacing);
19875
19876 /* End of this display line if row is continued. */
19877 if (row->continued_p || row->ends_at_zv_p)
19878 break;
19879 }
19880
19881 at_end_of_line:
19882 /* Is this a line end? If yes, we're also done, after making
19883 sure that a non-default face is extended up to the right
19884 margin of the window. */
19885 if (ITERATOR_AT_END_OF_LINE_P (it))
19886 {
19887 int used_before = row->used[TEXT_AREA];
19888
19889 row->ends_in_newline_from_string_p = STRINGP (it->object);
19890
19891 /* Add a space at the end of the line that is used to
19892 display the cursor there. */
19893 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19894 append_space_for_newline (it, 0);
19895
19896 /* Extend the face to the end of the line. */
19897 extend_face_to_end_of_line (it);
19898
19899 /* Make sure we have the position. */
19900 if (used_before == 0)
19901 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19902
19903 /* Record the position of the newline, for use in
19904 find_row_edges. */
19905 it->eol_pos = it->current.pos;
19906
19907 /* Consume the line end. This skips over invisible lines. */
19908 set_iterator_to_next (it, 1);
19909 it->continuation_lines_width = 0;
19910 break;
19911 }
19912
19913 /* Proceed with next display element. Note that this skips
19914 over lines invisible because of selective display. */
19915 set_iterator_to_next (it, 1);
19916
19917 /* If we truncate lines, we are done when the last displayed
19918 glyphs reach past the right margin of the window. */
19919 if (it->line_wrap == TRUNCATE
19920 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19921 ? (it->current_x >= it->last_visible_x)
19922 : (it->current_x > it->last_visible_x)))
19923 {
19924 /* Maybe add truncation glyphs. */
19925 if (!FRAME_WINDOW_P (it->f)
19926 || (row->reversed_p
19927 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19928 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19929 {
19930 int i, n;
19931
19932 if (!row->reversed_p)
19933 {
19934 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19935 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19936 break;
19937 }
19938 else
19939 {
19940 for (i = 0; i < row->used[TEXT_AREA]; i++)
19941 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19942 break;
19943 /* Remove any padding glyphs at the front of ROW, to
19944 make room for the truncation glyphs we will be
19945 adding below. The loop below always inserts at
19946 least one truncation glyph, so also remove the
19947 last glyph added to ROW. */
19948 unproduce_glyphs (it, i + 1);
19949 /* Adjust i for the loop below. */
19950 i = row->used[TEXT_AREA] - (i + 1);
19951 }
19952
19953 it->current_x = x_before;
19954 if (!FRAME_WINDOW_P (it->f))
19955 {
19956 for (n = row->used[TEXT_AREA]; i < n; ++i)
19957 {
19958 row->used[TEXT_AREA] = i;
19959 produce_special_glyphs (it, IT_TRUNCATION);
19960 }
19961 }
19962 else
19963 {
19964 row->used[TEXT_AREA] = i;
19965 produce_special_glyphs (it, IT_TRUNCATION);
19966 }
19967 }
19968 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19969 {
19970 /* Don't truncate if we can overflow newline into fringe. */
19971 if (!get_next_display_element (it))
19972 {
19973 it->continuation_lines_width = 0;
19974 row->ends_at_zv_p = 1;
19975 row->exact_window_width_line_p = 1;
19976 break;
19977 }
19978 if (ITERATOR_AT_END_OF_LINE_P (it))
19979 {
19980 row->exact_window_width_line_p = 1;
19981 goto at_end_of_line;
19982 }
19983 it->current_x = x_before;
19984 }
19985
19986 row->truncated_on_right_p = 1;
19987 it->continuation_lines_width = 0;
19988 reseat_at_next_visible_line_start (it, 0);
19989 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19990 it->hpos = hpos_before;
19991 break;
19992 }
19993 }
19994
19995 if (wrap_data)
19996 bidi_unshelve_cache (wrap_data, 1);
19997
19998 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19999 at the left window margin. */
20000 if (it->first_visible_x
20001 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20002 {
20003 if (!FRAME_WINDOW_P (it->f)
20004 || (row->reversed_p
20005 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20006 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20007 insert_left_trunc_glyphs (it);
20008 row->truncated_on_left_p = 1;
20009 }
20010
20011 /* Remember the position at which this line ends.
20012
20013 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20014 cannot be before the call to find_row_edges below, since that is
20015 where these positions are determined. */
20016 row->end = it->current;
20017 if (!it->bidi_p)
20018 {
20019 row->minpos = row->start.pos;
20020 row->maxpos = row->end.pos;
20021 }
20022 else
20023 {
20024 /* ROW->minpos and ROW->maxpos must be the smallest and
20025 `1 + the largest' buffer positions in ROW. But if ROW was
20026 bidi-reordered, these two positions can be anywhere in the
20027 row, so we must determine them now. */
20028 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20029 }
20030
20031 /* If the start of this line is the overlay arrow-position, then
20032 mark this glyph row as the one containing the overlay arrow.
20033 This is clearly a mess with variable size fonts. It would be
20034 better to let it be displayed like cursors under X. */
20035 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20036 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20037 !NILP (overlay_arrow_string)))
20038 {
20039 /* Overlay arrow in window redisplay is a fringe bitmap. */
20040 if (STRINGP (overlay_arrow_string))
20041 {
20042 struct glyph_row *arrow_row
20043 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20044 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20045 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20046 struct glyph *p = row->glyphs[TEXT_AREA];
20047 struct glyph *p2, *end;
20048
20049 /* Copy the arrow glyphs. */
20050 while (glyph < arrow_end)
20051 *p++ = *glyph++;
20052
20053 /* Throw away padding glyphs. */
20054 p2 = p;
20055 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20056 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20057 ++p2;
20058 if (p2 > p)
20059 {
20060 while (p2 < end)
20061 *p++ = *p2++;
20062 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20063 }
20064 }
20065 else
20066 {
20067 eassert (INTEGERP (overlay_arrow_string));
20068 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20069 }
20070 overlay_arrow_seen = 1;
20071 }
20072
20073 /* Highlight trailing whitespace. */
20074 if (!NILP (Vshow_trailing_whitespace))
20075 highlight_trailing_whitespace (it->f, it->glyph_row);
20076
20077 /* Compute pixel dimensions of this line. */
20078 compute_line_metrics (it);
20079
20080 /* Implementation note: No changes in the glyphs of ROW or in their
20081 faces can be done past this point, because compute_line_metrics
20082 computes ROW's hash value and stores it within the glyph_row
20083 structure. */
20084
20085 /* Record whether this row ends inside an ellipsis. */
20086 row->ends_in_ellipsis_p
20087 = (it->method == GET_FROM_DISPLAY_VECTOR
20088 && it->ellipsis_p);
20089
20090 /* Save fringe bitmaps in this row. */
20091 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20092 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20093 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20094 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20095
20096 it->left_user_fringe_bitmap = 0;
20097 it->left_user_fringe_face_id = 0;
20098 it->right_user_fringe_bitmap = 0;
20099 it->right_user_fringe_face_id = 0;
20100
20101 /* Maybe set the cursor. */
20102 cvpos = it->w->cursor.vpos;
20103 if ((cvpos < 0
20104 /* In bidi-reordered rows, keep checking for proper cursor
20105 position even if one has been found already, because buffer
20106 positions in such rows change non-linearly with ROW->VPOS,
20107 when a line is continued. One exception: when we are at ZV,
20108 display cursor on the first suitable glyph row, since all
20109 the empty rows after that also have their position set to ZV. */
20110 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20111 lines' rows is implemented for bidi-reordered rows. */
20112 || (it->bidi_p
20113 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20114 && PT >= MATRIX_ROW_START_CHARPOS (row)
20115 && PT <= MATRIX_ROW_END_CHARPOS (row)
20116 && cursor_row_p (row))
20117 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20118
20119 /* Prepare for the next line. This line starts horizontally at (X
20120 HPOS) = (0 0). Vertical positions are incremented. As a
20121 convenience for the caller, IT->glyph_row is set to the next
20122 row to be used. */
20123 it->current_x = it->hpos = 0;
20124 it->current_y += row->height;
20125 SET_TEXT_POS (it->eol_pos, 0, 0);
20126 ++it->vpos;
20127 ++it->glyph_row;
20128 /* The next row should by default use the same value of the
20129 reversed_p flag as this one. set_iterator_to_next decides when
20130 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20131 the flag accordingly. */
20132 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20133 it->glyph_row->reversed_p = row->reversed_p;
20134 it->start = row->end;
20135 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20136
20137 #undef RECORD_MAX_MIN_POS
20138 }
20139
20140 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20141 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20142 doc: /* Return paragraph direction at point in BUFFER.
20143 Value is either `left-to-right' or `right-to-left'.
20144 If BUFFER is omitted or nil, it defaults to the current buffer.
20145
20146 Paragraph direction determines how the text in the paragraph is displayed.
20147 In left-to-right paragraphs, text begins at the left margin of the window
20148 and the reading direction is generally left to right. In right-to-left
20149 paragraphs, text begins at the right margin and is read from right to left.
20150
20151 See also `bidi-paragraph-direction'. */)
20152 (Lisp_Object buffer)
20153 {
20154 struct buffer *buf = current_buffer;
20155 struct buffer *old = buf;
20156
20157 if (! NILP (buffer))
20158 {
20159 CHECK_BUFFER (buffer);
20160 buf = XBUFFER (buffer);
20161 }
20162
20163 if (NILP (BVAR (buf, bidi_display_reordering))
20164 || NILP (BVAR (buf, enable_multibyte_characters))
20165 /* When we are loading loadup.el, the character property tables
20166 needed for bidi iteration are not yet available. */
20167 || !NILP (Vpurify_flag))
20168 return Qleft_to_right;
20169 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20170 return BVAR (buf, bidi_paragraph_direction);
20171 else
20172 {
20173 /* Determine the direction from buffer text. We could try to
20174 use current_matrix if it is up to date, but this seems fast
20175 enough as it is. */
20176 struct bidi_it itb;
20177 ptrdiff_t pos = BUF_PT (buf);
20178 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20179 int c;
20180 void *itb_data = bidi_shelve_cache ();
20181
20182 set_buffer_temp (buf);
20183 /* bidi_paragraph_init finds the base direction of the paragraph
20184 by searching forward from paragraph start. We need the base
20185 direction of the current or _previous_ paragraph, so we need
20186 to make sure we are within that paragraph. To that end, find
20187 the previous non-empty line. */
20188 if (pos >= ZV && pos > BEGV)
20189 DEC_BOTH (pos, bytepos);
20190 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20191 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20192 {
20193 while ((c = FETCH_BYTE (bytepos)) == '\n'
20194 || c == ' ' || c == '\t' || c == '\f')
20195 {
20196 if (bytepos <= BEGV_BYTE)
20197 break;
20198 bytepos--;
20199 pos--;
20200 }
20201 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20202 bytepos--;
20203 }
20204 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20205 itb.paragraph_dir = NEUTRAL_DIR;
20206 itb.string.s = NULL;
20207 itb.string.lstring = Qnil;
20208 itb.string.bufpos = 0;
20209 itb.string.unibyte = 0;
20210 /* We have no window to use here for ignoring window-specific
20211 overlays. Using NULL for window pointer will cause
20212 compute_display_string_pos to use the current buffer. */
20213 itb.w = NULL;
20214 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20215 bidi_unshelve_cache (itb_data, 0);
20216 set_buffer_temp (old);
20217 switch (itb.paragraph_dir)
20218 {
20219 case L2R:
20220 return Qleft_to_right;
20221 break;
20222 case R2L:
20223 return Qright_to_left;
20224 break;
20225 default:
20226 emacs_abort ();
20227 }
20228 }
20229 }
20230
20231 DEFUN ("move-point-visually", Fmove_point_visually,
20232 Smove_point_visually, 1, 1, 0,
20233 doc: /* Move point in the visual order in the specified DIRECTION.
20234 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20235 left.
20236
20237 Value is the new character position of point. */)
20238 (Lisp_Object direction)
20239 {
20240 struct window *w = XWINDOW (selected_window);
20241 struct buffer *b = XBUFFER (w->contents);
20242 struct glyph_row *row;
20243 int dir;
20244 Lisp_Object paragraph_dir;
20245
20246 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20247 (!(ROW)->continued_p \
20248 && INTEGERP ((GLYPH)->object) \
20249 && (GLYPH)->type == CHAR_GLYPH \
20250 && (GLYPH)->u.ch == ' ' \
20251 && (GLYPH)->charpos >= 0 \
20252 && !(GLYPH)->avoid_cursor_p)
20253
20254 CHECK_NUMBER (direction);
20255 dir = XINT (direction);
20256 if (dir > 0)
20257 dir = 1;
20258 else
20259 dir = -1;
20260
20261 /* If current matrix is up-to-date, we can use the information
20262 recorded in the glyphs, at least as long as the goal is on the
20263 screen. */
20264 if (w->window_end_valid
20265 && !windows_or_buffers_changed
20266 && b
20267 && !b->clip_changed
20268 && !b->prevent_redisplay_optimizations_p
20269 && !window_outdated (w)
20270 && w->cursor.vpos >= 0
20271 && w->cursor.vpos < w->current_matrix->nrows
20272 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20273 {
20274 struct glyph *g = row->glyphs[TEXT_AREA];
20275 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20276 struct glyph *gpt = g + w->cursor.hpos;
20277
20278 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20279 {
20280 if (BUFFERP (g->object) && g->charpos != PT)
20281 {
20282 SET_PT (g->charpos);
20283 w->cursor.vpos = -1;
20284 return make_number (PT);
20285 }
20286 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20287 {
20288 ptrdiff_t new_pos;
20289
20290 if (BUFFERP (gpt->object))
20291 {
20292 new_pos = PT;
20293 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20294 new_pos += (row->reversed_p ? -dir : dir);
20295 else
20296 new_pos -= (row->reversed_p ? -dir : dir);;
20297 }
20298 else if (BUFFERP (g->object))
20299 new_pos = g->charpos;
20300 else
20301 break;
20302 SET_PT (new_pos);
20303 w->cursor.vpos = -1;
20304 return make_number (PT);
20305 }
20306 else if (ROW_GLYPH_NEWLINE_P (row, g))
20307 {
20308 /* Glyphs inserted at the end of a non-empty line for
20309 positioning the cursor have zero charpos, so we must
20310 deduce the value of point by other means. */
20311 if (g->charpos > 0)
20312 SET_PT (g->charpos);
20313 else if (row->ends_at_zv_p && PT != ZV)
20314 SET_PT (ZV);
20315 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20316 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20317 else
20318 break;
20319 w->cursor.vpos = -1;
20320 return make_number (PT);
20321 }
20322 }
20323 if (g == e || INTEGERP (g->object))
20324 {
20325 if (row->truncated_on_left_p || row->truncated_on_right_p)
20326 goto simulate_display;
20327 if (!row->reversed_p)
20328 row += dir;
20329 else
20330 row -= dir;
20331 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20332 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20333 goto simulate_display;
20334
20335 if (dir > 0)
20336 {
20337 if (row->reversed_p && !row->continued_p)
20338 {
20339 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20340 w->cursor.vpos = -1;
20341 return make_number (PT);
20342 }
20343 g = row->glyphs[TEXT_AREA];
20344 e = g + row->used[TEXT_AREA];
20345 for ( ; g < e; g++)
20346 {
20347 if (BUFFERP (g->object)
20348 /* Empty lines have only one glyph, which stands
20349 for the newline, and whose charpos is the
20350 buffer position of the newline. */
20351 || ROW_GLYPH_NEWLINE_P (row, g)
20352 /* When the buffer ends in a newline, the line at
20353 EOB also has one glyph, but its charpos is -1. */
20354 || (row->ends_at_zv_p
20355 && !row->reversed_p
20356 && INTEGERP (g->object)
20357 && g->type == CHAR_GLYPH
20358 && g->u.ch == ' '))
20359 {
20360 if (g->charpos > 0)
20361 SET_PT (g->charpos);
20362 else if (!row->reversed_p
20363 && row->ends_at_zv_p
20364 && PT != ZV)
20365 SET_PT (ZV);
20366 else
20367 continue;
20368 w->cursor.vpos = -1;
20369 return make_number (PT);
20370 }
20371 }
20372 }
20373 else
20374 {
20375 if (!row->reversed_p && !row->continued_p)
20376 {
20377 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20378 w->cursor.vpos = -1;
20379 return make_number (PT);
20380 }
20381 e = row->glyphs[TEXT_AREA];
20382 g = e + row->used[TEXT_AREA] - 1;
20383 for ( ; g >= e; g--)
20384 {
20385 if (BUFFERP (g->object)
20386 || (ROW_GLYPH_NEWLINE_P (row, g)
20387 && g->charpos > 0)
20388 /* Empty R2L lines on GUI frames have the buffer
20389 position of the newline stored in the stretch
20390 glyph. */
20391 || g->type == STRETCH_GLYPH
20392 || (row->ends_at_zv_p
20393 && row->reversed_p
20394 && INTEGERP (g->object)
20395 && g->type == CHAR_GLYPH
20396 && g->u.ch == ' '))
20397 {
20398 if (g->charpos > 0)
20399 SET_PT (g->charpos);
20400 else if (row->reversed_p
20401 && row->ends_at_zv_p
20402 && PT != ZV)
20403 SET_PT (ZV);
20404 else
20405 continue;
20406 w->cursor.vpos = -1;
20407 return make_number (PT);
20408 }
20409 }
20410 }
20411 }
20412 }
20413
20414 simulate_display:
20415
20416 /* If we wind up here, we failed to move by using the glyphs, so we
20417 need to simulate display instead. */
20418
20419 if (b)
20420 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20421 else
20422 paragraph_dir = Qleft_to_right;
20423 if (EQ (paragraph_dir, Qright_to_left))
20424 dir = -dir;
20425 if (PT <= BEGV && dir < 0)
20426 xsignal0 (Qbeginning_of_buffer);
20427 else if (PT >= ZV && dir > 0)
20428 xsignal0 (Qend_of_buffer);
20429 else
20430 {
20431 struct text_pos pt;
20432 struct it it;
20433 int pt_x, target_x, pixel_width, pt_vpos;
20434 bool at_eol_p;
20435 bool overshoot_expected = false;
20436 bool target_is_eol_p = false;
20437
20438 /* Setup the arena. */
20439 SET_TEXT_POS (pt, PT, PT_BYTE);
20440 start_display (&it, w, pt);
20441
20442 if (it.cmp_it.id < 0
20443 && it.method == GET_FROM_STRING
20444 && it.area == TEXT_AREA
20445 && it.string_from_display_prop_p
20446 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20447 overshoot_expected = true;
20448
20449 /* Find the X coordinate of point. We start from the beginning
20450 of this or previous line to make sure we are before point in
20451 the logical order (since the move_it_* functions can only
20452 move forward). */
20453 reseat_at_previous_visible_line_start (&it);
20454 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20455 if (IT_CHARPOS (it) != PT)
20456 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20457 -1, -1, -1, MOVE_TO_POS);
20458 pt_x = it.current_x;
20459 pt_vpos = it.vpos;
20460 if (dir > 0 || overshoot_expected)
20461 {
20462 struct glyph_row *row = it.glyph_row;
20463
20464 /* When point is at beginning of line, we don't have
20465 information about the glyph there loaded into struct
20466 it. Calling get_next_display_element fixes that. */
20467 if (pt_x == 0)
20468 get_next_display_element (&it);
20469 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20470 it.glyph_row = NULL;
20471 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20472 it.glyph_row = row;
20473 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20474 it, lest it will become out of sync with it's buffer
20475 position. */
20476 it.current_x = pt_x;
20477 }
20478 else
20479 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20480 pixel_width = it.pixel_width;
20481 if (overshoot_expected && at_eol_p)
20482 pixel_width = 0;
20483 else if (pixel_width <= 0)
20484 pixel_width = 1;
20485
20486 /* If there's a display string at point, we are actually at the
20487 glyph to the left of point, so we need to correct the X
20488 coordinate. */
20489 if (overshoot_expected)
20490 pt_x += pixel_width;
20491
20492 /* Compute target X coordinate, either to the left or to the
20493 right of point. On TTY frames, all characters have the same
20494 pixel width of 1, so we can use that. On GUI frames we don't
20495 have an easy way of getting at the pixel width of the
20496 character to the left of point, so we use a different method
20497 of getting to that place. */
20498 if (dir > 0)
20499 target_x = pt_x + pixel_width;
20500 else
20501 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20502
20503 /* Target X coordinate could be one line above or below the line
20504 of point, in which case we need to adjust the target X
20505 coordinate. Also, if moving to the left, we need to begin at
20506 the left edge of the point's screen line. */
20507 if (dir < 0)
20508 {
20509 if (pt_x > 0)
20510 {
20511 start_display (&it, w, pt);
20512 reseat_at_previous_visible_line_start (&it);
20513 it.current_x = it.current_y = it.hpos = 0;
20514 if (pt_vpos != 0)
20515 move_it_by_lines (&it, pt_vpos);
20516 }
20517 else
20518 {
20519 move_it_by_lines (&it, -1);
20520 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20521 target_is_eol_p = true;
20522 }
20523 }
20524 else
20525 {
20526 if (at_eol_p
20527 || (target_x >= it.last_visible_x
20528 && it.line_wrap != TRUNCATE))
20529 {
20530 if (pt_x > 0)
20531 move_it_by_lines (&it, 0);
20532 move_it_by_lines (&it, 1);
20533 target_x = 0;
20534 }
20535 }
20536
20537 /* Move to the target X coordinate. */
20538 #ifdef HAVE_WINDOW_SYSTEM
20539 /* On GUI frames, as we don't know the X coordinate of the
20540 character to the left of point, moving point to the left
20541 requires walking, one grapheme cluster at a time, until we
20542 find ourself at a place immediately to the left of the
20543 character at point. */
20544 if (FRAME_WINDOW_P (it.f) && dir < 0)
20545 {
20546 struct text_pos new_pos = it.current.pos;
20547 enum move_it_result rc = MOVE_X_REACHED;
20548
20549 while (it.current_x + it.pixel_width <= target_x
20550 && rc == MOVE_X_REACHED)
20551 {
20552 int new_x = it.current_x + it.pixel_width;
20553
20554 new_pos = it.current.pos;
20555 if (new_x == it.current_x)
20556 new_x++;
20557 rc = move_it_in_display_line_to (&it, ZV, new_x,
20558 MOVE_TO_POS | MOVE_TO_X);
20559 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20560 break;
20561 }
20562 /* If we ended up on a composed character inside
20563 bidi-reordered text (e.g., Hebrew text with diacritics),
20564 the iterator gives us the buffer position of the last (in
20565 logical order) character of the composed grapheme cluster,
20566 which is not what we want. So we cheat: we compute the
20567 character position of the character that follows (in the
20568 logical order) the one where the above loop stopped. That
20569 character will appear on display to the left of point. */
20570 if (it.bidi_p
20571 && it.bidi_it.scan_dir == -1
20572 && new_pos.charpos - IT_CHARPOS (it) > 1)
20573 {
20574 new_pos.charpos = IT_CHARPOS (it) + 1;
20575 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20576 }
20577 it.current.pos = new_pos;
20578 }
20579 else
20580 #endif
20581 if (it.current_x != target_x)
20582 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20583
20584 /* When lines are truncated, the above loop will stop at the
20585 window edge. But we want to get to the end of line, even if
20586 it is beyond the window edge; automatic hscroll will then
20587 scroll the window to show point as appropriate. */
20588 if (target_is_eol_p && it.line_wrap == TRUNCATE
20589 && get_next_display_element (&it))
20590 {
20591 struct text_pos new_pos = it.current.pos;
20592
20593 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20594 {
20595 set_iterator_to_next (&it, 0);
20596 if (it.method == GET_FROM_BUFFER)
20597 new_pos = it.current.pos;
20598 if (!get_next_display_element (&it))
20599 break;
20600 }
20601
20602 it.current.pos = new_pos;
20603 }
20604
20605 /* If we ended up in a display string that covers point, move to
20606 buffer position to the right in the visual order. */
20607 if (dir > 0)
20608 {
20609 while (IT_CHARPOS (it) == PT)
20610 {
20611 set_iterator_to_next (&it, 0);
20612 if (!get_next_display_element (&it))
20613 break;
20614 }
20615 }
20616
20617 /* Move point to that position. */
20618 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20619 }
20620
20621 return make_number (PT);
20622
20623 #undef ROW_GLYPH_NEWLINE_P
20624 }
20625
20626 \f
20627 /***********************************************************************
20628 Menu Bar
20629 ***********************************************************************/
20630
20631 /* Redisplay the menu bar in the frame for window W.
20632
20633 The menu bar of X frames that don't have X toolkit support is
20634 displayed in a special window W->frame->menu_bar_window.
20635
20636 The menu bar of terminal frames is treated specially as far as
20637 glyph matrices are concerned. Menu bar lines are not part of
20638 windows, so the update is done directly on the frame matrix rows
20639 for the menu bar. */
20640
20641 static void
20642 display_menu_bar (struct window *w)
20643 {
20644 struct frame *f = XFRAME (WINDOW_FRAME (w));
20645 struct it it;
20646 Lisp_Object items;
20647 int i;
20648
20649 /* Don't do all this for graphical frames. */
20650 #ifdef HAVE_NTGUI
20651 if (FRAME_W32_P (f))
20652 return;
20653 #endif
20654 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20655 if (FRAME_X_P (f))
20656 return;
20657 #endif
20658
20659 #ifdef HAVE_NS
20660 if (FRAME_NS_P (f))
20661 return;
20662 #endif /* HAVE_NS */
20663
20664 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20665 eassert (!FRAME_WINDOW_P (f));
20666 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20667 it.first_visible_x = 0;
20668 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20669 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20670 if (FRAME_WINDOW_P (f))
20671 {
20672 /* Menu bar lines are displayed in the desired matrix of the
20673 dummy window menu_bar_window. */
20674 struct window *menu_w;
20675 menu_w = XWINDOW (f->menu_bar_window);
20676 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20677 MENU_FACE_ID);
20678 it.first_visible_x = 0;
20679 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20680 }
20681 else
20682 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20683 {
20684 /* This is a TTY frame, i.e. character hpos/vpos are used as
20685 pixel x/y. */
20686 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20687 MENU_FACE_ID);
20688 it.first_visible_x = 0;
20689 it.last_visible_x = FRAME_COLS (f);
20690 }
20691
20692 /* FIXME: This should be controlled by a user option. See the
20693 comments in redisplay_tool_bar and display_mode_line about
20694 this. */
20695 it.paragraph_embedding = L2R;
20696
20697 /* Clear all rows of the menu bar. */
20698 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20699 {
20700 struct glyph_row *row = it.glyph_row + i;
20701 clear_glyph_row (row);
20702 row->enabled_p = 1;
20703 row->full_width_p = 1;
20704 }
20705
20706 /* Display all items of the menu bar. */
20707 items = FRAME_MENU_BAR_ITEMS (it.f);
20708 for (i = 0; i < ASIZE (items); i += 4)
20709 {
20710 Lisp_Object string;
20711
20712 /* Stop at nil string. */
20713 string = AREF (items, i + 1);
20714 if (NILP (string))
20715 break;
20716
20717 /* Remember where item was displayed. */
20718 ASET (items, i + 3, make_number (it.hpos));
20719
20720 /* Display the item, pad with one space. */
20721 if (it.current_x < it.last_visible_x)
20722 display_string (NULL, string, Qnil, 0, 0, &it,
20723 SCHARS (string) + 1, 0, 0, -1);
20724 }
20725
20726 /* Fill out the line with spaces. */
20727 if (it.current_x < it.last_visible_x)
20728 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20729
20730 /* Compute the total height of the lines. */
20731 compute_line_metrics (&it);
20732 }
20733
20734
20735 \f
20736 /***********************************************************************
20737 Mode Line
20738 ***********************************************************************/
20739
20740 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20741 FORCE is non-zero, redisplay mode lines unconditionally.
20742 Otherwise, redisplay only mode lines that are garbaged. Value is
20743 the number of windows whose mode lines were redisplayed. */
20744
20745 static int
20746 redisplay_mode_lines (Lisp_Object window, int force)
20747 {
20748 int nwindows = 0;
20749
20750 while (!NILP (window))
20751 {
20752 struct window *w = XWINDOW (window);
20753
20754 if (WINDOWP (w->contents))
20755 nwindows += redisplay_mode_lines (w->contents, force);
20756 else if (force
20757 || FRAME_GARBAGED_P (XFRAME (w->frame))
20758 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20759 {
20760 struct text_pos lpoint;
20761 struct buffer *old = current_buffer;
20762
20763 /* Set the window's buffer for the mode line display. */
20764 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20765 set_buffer_internal_1 (XBUFFER (w->contents));
20766
20767 /* Point refers normally to the selected window. For any
20768 other window, set up appropriate value. */
20769 if (!EQ (window, selected_window))
20770 {
20771 struct text_pos pt;
20772
20773 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20774 if (CHARPOS (pt) < BEGV)
20775 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20776 else if (CHARPOS (pt) > (ZV - 1))
20777 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20778 else
20779 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20780 }
20781
20782 /* Display mode lines. */
20783 clear_glyph_matrix (w->desired_matrix);
20784 if (display_mode_lines (w))
20785 {
20786 ++nwindows;
20787 w->must_be_updated_p = 1;
20788 }
20789
20790 /* Restore old settings. */
20791 set_buffer_internal_1 (old);
20792 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20793 }
20794
20795 window = w->next;
20796 }
20797
20798 return nwindows;
20799 }
20800
20801
20802 /* Display the mode and/or header line of window W. Value is the
20803 sum number of mode lines and header lines displayed. */
20804
20805 static int
20806 display_mode_lines (struct window *w)
20807 {
20808 Lisp_Object old_selected_window = selected_window;
20809 Lisp_Object old_selected_frame = selected_frame;
20810 Lisp_Object new_frame = w->frame;
20811 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20812 int n = 0;
20813
20814 selected_frame = new_frame;
20815 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20816 or window's point, then we'd need select_window_1 here as well. */
20817 XSETWINDOW (selected_window, w);
20818 XFRAME (new_frame)->selected_window = selected_window;
20819
20820 /* These will be set while the mode line specs are processed. */
20821 line_number_displayed = 0;
20822 w->column_number_displayed = -1;
20823
20824 if (WINDOW_WANTS_MODELINE_P (w))
20825 {
20826 struct window *sel_w = XWINDOW (old_selected_window);
20827
20828 /* Select mode line face based on the real selected window. */
20829 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20830 BVAR (current_buffer, mode_line_format));
20831 ++n;
20832 }
20833
20834 if (WINDOW_WANTS_HEADER_LINE_P (w))
20835 {
20836 display_mode_line (w, HEADER_LINE_FACE_ID,
20837 BVAR (current_buffer, header_line_format));
20838 ++n;
20839 }
20840
20841 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20842 selected_frame = old_selected_frame;
20843 selected_window = old_selected_window;
20844 return n;
20845 }
20846
20847
20848 /* Display mode or header line of window W. FACE_ID specifies which
20849 line to display; it is either MODE_LINE_FACE_ID or
20850 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20851 display. Value is the pixel height of the mode/header line
20852 displayed. */
20853
20854 static int
20855 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20856 {
20857 struct it it;
20858 struct face *face;
20859 ptrdiff_t count = SPECPDL_INDEX ();
20860
20861 init_iterator (&it, w, -1, -1, NULL, face_id);
20862 /* Don't extend on a previously drawn mode-line.
20863 This may happen if called from pos_visible_p. */
20864 it.glyph_row->enabled_p = 0;
20865 prepare_desired_row (it.glyph_row);
20866
20867 it.glyph_row->mode_line_p = 1;
20868
20869 /* FIXME: This should be controlled by a user option. But
20870 supporting such an option is not trivial, since the mode line is
20871 made up of many separate strings. */
20872 it.paragraph_embedding = L2R;
20873
20874 record_unwind_protect (unwind_format_mode_line,
20875 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20876
20877 mode_line_target = MODE_LINE_DISPLAY;
20878
20879 /* Temporarily make frame's keyboard the current kboard so that
20880 kboard-local variables in the mode_line_format will get the right
20881 values. */
20882 push_kboard (FRAME_KBOARD (it.f));
20883 record_unwind_save_match_data ();
20884 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20885 pop_kboard ();
20886
20887 unbind_to (count, Qnil);
20888
20889 /* Fill up with spaces. */
20890 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20891
20892 compute_line_metrics (&it);
20893 it.glyph_row->full_width_p = 1;
20894 it.glyph_row->continued_p = 0;
20895 it.glyph_row->truncated_on_left_p = 0;
20896 it.glyph_row->truncated_on_right_p = 0;
20897
20898 /* Make a 3D mode-line have a shadow at its right end. */
20899 face = FACE_FROM_ID (it.f, face_id);
20900 extend_face_to_end_of_line (&it);
20901 if (face->box != FACE_NO_BOX)
20902 {
20903 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20904 + it.glyph_row->used[TEXT_AREA] - 1);
20905 last->right_box_line_p = 1;
20906 }
20907
20908 return it.glyph_row->height;
20909 }
20910
20911 /* Move element ELT in LIST to the front of LIST.
20912 Return the updated list. */
20913
20914 static Lisp_Object
20915 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20916 {
20917 register Lisp_Object tail, prev;
20918 register Lisp_Object tem;
20919
20920 tail = list;
20921 prev = Qnil;
20922 while (CONSP (tail))
20923 {
20924 tem = XCAR (tail);
20925
20926 if (EQ (elt, tem))
20927 {
20928 /* Splice out the link TAIL. */
20929 if (NILP (prev))
20930 list = XCDR (tail);
20931 else
20932 Fsetcdr (prev, XCDR (tail));
20933
20934 /* Now make it the first. */
20935 Fsetcdr (tail, list);
20936 return tail;
20937 }
20938 else
20939 prev = tail;
20940 tail = XCDR (tail);
20941 QUIT;
20942 }
20943
20944 /* Not found--return unchanged LIST. */
20945 return list;
20946 }
20947
20948 /* Contribute ELT to the mode line for window IT->w. How it
20949 translates into text depends on its data type.
20950
20951 IT describes the display environment in which we display, as usual.
20952
20953 DEPTH is the depth in recursion. It is used to prevent
20954 infinite recursion here.
20955
20956 FIELD_WIDTH is the number of characters the display of ELT should
20957 occupy in the mode line, and PRECISION is the maximum number of
20958 characters to display from ELT's representation. See
20959 display_string for details.
20960
20961 Returns the hpos of the end of the text generated by ELT.
20962
20963 PROPS is a property list to add to any string we encounter.
20964
20965 If RISKY is nonzero, remove (disregard) any properties in any string
20966 we encounter, and ignore :eval and :propertize.
20967
20968 The global variable `mode_line_target' determines whether the
20969 output is passed to `store_mode_line_noprop',
20970 `store_mode_line_string', or `display_string'. */
20971
20972 static int
20973 display_mode_element (struct it *it, int depth, int field_width, int precision,
20974 Lisp_Object elt, Lisp_Object props, int risky)
20975 {
20976 int n = 0, field, prec;
20977 int literal = 0;
20978
20979 tail_recurse:
20980 if (depth > 100)
20981 elt = build_string ("*too-deep*");
20982
20983 depth++;
20984
20985 switch (XTYPE (elt))
20986 {
20987 case Lisp_String:
20988 {
20989 /* A string: output it and check for %-constructs within it. */
20990 unsigned char c;
20991 ptrdiff_t offset = 0;
20992
20993 if (SCHARS (elt) > 0
20994 && (!NILP (props) || risky))
20995 {
20996 Lisp_Object oprops, aelt;
20997 oprops = Ftext_properties_at (make_number (0), elt);
20998
20999 /* If the starting string's properties are not what
21000 we want, translate the string. Also, if the string
21001 is risky, do that anyway. */
21002
21003 if (NILP (Fequal (props, oprops)) || risky)
21004 {
21005 /* If the starting string has properties,
21006 merge the specified ones onto the existing ones. */
21007 if (! NILP (oprops) && !risky)
21008 {
21009 Lisp_Object tem;
21010
21011 oprops = Fcopy_sequence (oprops);
21012 tem = props;
21013 while (CONSP (tem))
21014 {
21015 oprops = Fplist_put (oprops, XCAR (tem),
21016 XCAR (XCDR (tem)));
21017 tem = XCDR (XCDR (tem));
21018 }
21019 props = oprops;
21020 }
21021
21022 aelt = Fassoc (elt, mode_line_proptrans_alist);
21023 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21024 {
21025 /* AELT is what we want. Move it to the front
21026 without consing. */
21027 elt = XCAR (aelt);
21028 mode_line_proptrans_alist
21029 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21030 }
21031 else
21032 {
21033 Lisp_Object tem;
21034
21035 /* If AELT has the wrong props, it is useless.
21036 so get rid of it. */
21037 if (! NILP (aelt))
21038 mode_line_proptrans_alist
21039 = Fdelq (aelt, mode_line_proptrans_alist);
21040
21041 elt = Fcopy_sequence (elt);
21042 Fset_text_properties (make_number (0), Flength (elt),
21043 props, elt);
21044 /* Add this item to mode_line_proptrans_alist. */
21045 mode_line_proptrans_alist
21046 = Fcons (Fcons (elt, props),
21047 mode_line_proptrans_alist);
21048 /* Truncate mode_line_proptrans_alist
21049 to at most 50 elements. */
21050 tem = Fnthcdr (make_number (50),
21051 mode_line_proptrans_alist);
21052 if (! NILP (tem))
21053 XSETCDR (tem, Qnil);
21054 }
21055 }
21056 }
21057
21058 offset = 0;
21059
21060 if (literal)
21061 {
21062 prec = precision - n;
21063 switch (mode_line_target)
21064 {
21065 case MODE_LINE_NOPROP:
21066 case MODE_LINE_TITLE:
21067 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21068 break;
21069 case MODE_LINE_STRING:
21070 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21071 break;
21072 case MODE_LINE_DISPLAY:
21073 n += display_string (NULL, elt, Qnil, 0, 0, it,
21074 0, prec, 0, STRING_MULTIBYTE (elt));
21075 break;
21076 }
21077
21078 break;
21079 }
21080
21081 /* Handle the non-literal case. */
21082
21083 while ((precision <= 0 || n < precision)
21084 && SREF (elt, offset) != 0
21085 && (mode_line_target != MODE_LINE_DISPLAY
21086 || it->current_x < it->last_visible_x))
21087 {
21088 ptrdiff_t last_offset = offset;
21089
21090 /* Advance to end of string or next format specifier. */
21091 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21092 ;
21093
21094 if (offset - 1 != last_offset)
21095 {
21096 ptrdiff_t nchars, nbytes;
21097
21098 /* Output to end of string or up to '%'. Field width
21099 is length of string. Don't output more than
21100 PRECISION allows us. */
21101 offset--;
21102
21103 prec = c_string_width (SDATA (elt) + last_offset,
21104 offset - last_offset, precision - n,
21105 &nchars, &nbytes);
21106
21107 switch (mode_line_target)
21108 {
21109 case MODE_LINE_NOPROP:
21110 case MODE_LINE_TITLE:
21111 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21112 break;
21113 case MODE_LINE_STRING:
21114 {
21115 ptrdiff_t bytepos = last_offset;
21116 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21117 ptrdiff_t endpos = (precision <= 0
21118 ? string_byte_to_char (elt, offset)
21119 : charpos + nchars);
21120
21121 n += store_mode_line_string (NULL,
21122 Fsubstring (elt, make_number (charpos),
21123 make_number (endpos)),
21124 0, 0, 0, Qnil);
21125 }
21126 break;
21127 case MODE_LINE_DISPLAY:
21128 {
21129 ptrdiff_t bytepos = last_offset;
21130 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21131
21132 if (precision <= 0)
21133 nchars = string_byte_to_char (elt, offset) - charpos;
21134 n += display_string (NULL, elt, Qnil, 0, charpos,
21135 it, 0, nchars, 0,
21136 STRING_MULTIBYTE (elt));
21137 }
21138 break;
21139 }
21140 }
21141 else /* c == '%' */
21142 {
21143 ptrdiff_t percent_position = offset;
21144
21145 /* Get the specified minimum width. Zero means
21146 don't pad. */
21147 field = 0;
21148 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21149 field = field * 10 + c - '0';
21150
21151 /* Don't pad beyond the total padding allowed. */
21152 if (field_width - n > 0 && field > field_width - n)
21153 field = field_width - n;
21154
21155 /* Note that either PRECISION <= 0 or N < PRECISION. */
21156 prec = precision - n;
21157
21158 if (c == 'M')
21159 n += display_mode_element (it, depth, field, prec,
21160 Vglobal_mode_string, props,
21161 risky);
21162 else if (c != 0)
21163 {
21164 bool multibyte;
21165 ptrdiff_t bytepos, charpos;
21166 const char *spec;
21167 Lisp_Object string;
21168
21169 bytepos = percent_position;
21170 charpos = (STRING_MULTIBYTE (elt)
21171 ? string_byte_to_char (elt, bytepos)
21172 : bytepos);
21173 spec = decode_mode_spec (it->w, c, field, &string);
21174 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21175
21176 switch (mode_line_target)
21177 {
21178 case MODE_LINE_NOPROP:
21179 case MODE_LINE_TITLE:
21180 n += store_mode_line_noprop (spec, field, prec);
21181 break;
21182 case MODE_LINE_STRING:
21183 {
21184 Lisp_Object tem = build_string (spec);
21185 props = Ftext_properties_at (make_number (charpos), elt);
21186 /* Should only keep face property in props */
21187 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21188 }
21189 break;
21190 case MODE_LINE_DISPLAY:
21191 {
21192 int nglyphs_before, nwritten;
21193
21194 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21195 nwritten = display_string (spec, string, elt,
21196 charpos, 0, it,
21197 field, prec, 0,
21198 multibyte);
21199
21200 /* Assign to the glyphs written above the
21201 string where the `%x' came from, position
21202 of the `%'. */
21203 if (nwritten > 0)
21204 {
21205 struct glyph *glyph
21206 = (it->glyph_row->glyphs[TEXT_AREA]
21207 + nglyphs_before);
21208 int i;
21209
21210 for (i = 0; i < nwritten; ++i)
21211 {
21212 glyph[i].object = elt;
21213 glyph[i].charpos = charpos;
21214 }
21215
21216 n += nwritten;
21217 }
21218 }
21219 break;
21220 }
21221 }
21222 else /* c == 0 */
21223 break;
21224 }
21225 }
21226 }
21227 break;
21228
21229 case Lisp_Symbol:
21230 /* A symbol: process the value of the symbol recursively
21231 as if it appeared here directly. Avoid error if symbol void.
21232 Special case: if value of symbol is a string, output the string
21233 literally. */
21234 {
21235 register Lisp_Object tem;
21236
21237 /* If the variable is not marked as risky to set
21238 then its contents are risky to use. */
21239 if (NILP (Fget (elt, Qrisky_local_variable)))
21240 risky = 1;
21241
21242 tem = Fboundp (elt);
21243 if (!NILP (tem))
21244 {
21245 tem = Fsymbol_value (elt);
21246 /* If value is a string, output that string literally:
21247 don't check for % within it. */
21248 if (STRINGP (tem))
21249 literal = 1;
21250
21251 if (!EQ (tem, elt))
21252 {
21253 /* Give up right away for nil or t. */
21254 elt = tem;
21255 goto tail_recurse;
21256 }
21257 }
21258 }
21259 break;
21260
21261 case Lisp_Cons:
21262 {
21263 register Lisp_Object car, tem;
21264
21265 /* A cons cell: five distinct cases.
21266 If first element is :eval or :propertize, do something special.
21267 If first element is a string or a cons, process all the elements
21268 and effectively concatenate them.
21269 If first element is a negative number, truncate displaying cdr to
21270 at most that many characters. If positive, pad (with spaces)
21271 to at least that many characters.
21272 If first element is a symbol, process the cadr or caddr recursively
21273 according to whether the symbol's value is non-nil or nil. */
21274 car = XCAR (elt);
21275 if (EQ (car, QCeval))
21276 {
21277 /* An element of the form (:eval FORM) means evaluate FORM
21278 and use the result as mode line elements. */
21279
21280 if (risky)
21281 break;
21282
21283 if (CONSP (XCDR (elt)))
21284 {
21285 Lisp_Object spec;
21286 spec = safe_eval (XCAR (XCDR (elt)));
21287 n += display_mode_element (it, depth, field_width - n,
21288 precision - n, spec, props,
21289 risky);
21290 }
21291 }
21292 else if (EQ (car, QCpropertize))
21293 {
21294 /* An element of the form (:propertize ELT PROPS...)
21295 means display ELT but applying properties PROPS. */
21296
21297 if (risky)
21298 break;
21299
21300 if (CONSP (XCDR (elt)))
21301 n += display_mode_element (it, depth, field_width - n,
21302 precision - n, XCAR (XCDR (elt)),
21303 XCDR (XCDR (elt)), risky);
21304 }
21305 else if (SYMBOLP (car))
21306 {
21307 tem = Fboundp (car);
21308 elt = XCDR (elt);
21309 if (!CONSP (elt))
21310 goto invalid;
21311 /* elt is now the cdr, and we know it is a cons cell.
21312 Use its car if CAR has a non-nil value. */
21313 if (!NILP (tem))
21314 {
21315 tem = Fsymbol_value (car);
21316 if (!NILP (tem))
21317 {
21318 elt = XCAR (elt);
21319 goto tail_recurse;
21320 }
21321 }
21322 /* Symbol's value is nil (or symbol is unbound)
21323 Get the cddr of the original list
21324 and if possible find the caddr and use that. */
21325 elt = XCDR (elt);
21326 if (NILP (elt))
21327 break;
21328 else if (!CONSP (elt))
21329 goto invalid;
21330 elt = XCAR (elt);
21331 goto tail_recurse;
21332 }
21333 else if (INTEGERP (car))
21334 {
21335 register int lim = XINT (car);
21336 elt = XCDR (elt);
21337 if (lim < 0)
21338 {
21339 /* Negative int means reduce maximum width. */
21340 if (precision <= 0)
21341 precision = -lim;
21342 else
21343 precision = min (precision, -lim);
21344 }
21345 else if (lim > 0)
21346 {
21347 /* Padding specified. Don't let it be more than
21348 current maximum. */
21349 if (precision > 0)
21350 lim = min (precision, lim);
21351
21352 /* If that's more padding than already wanted, queue it.
21353 But don't reduce padding already specified even if
21354 that is beyond the current truncation point. */
21355 field_width = max (lim, field_width);
21356 }
21357 goto tail_recurse;
21358 }
21359 else if (STRINGP (car) || CONSP (car))
21360 {
21361 Lisp_Object halftail = elt;
21362 int len = 0;
21363
21364 while (CONSP (elt)
21365 && (precision <= 0 || n < precision))
21366 {
21367 n += display_mode_element (it, depth,
21368 /* Do padding only after the last
21369 element in the list. */
21370 (! CONSP (XCDR (elt))
21371 ? field_width - n
21372 : 0),
21373 precision - n, XCAR (elt),
21374 props, risky);
21375 elt = XCDR (elt);
21376 len++;
21377 if ((len & 1) == 0)
21378 halftail = XCDR (halftail);
21379 /* Check for cycle. */
21380 if (EQ (halftail, elt))
21381 break;
21382 }
21383 }
21384 }
21385 break;
21386
21387 default:
21388 invalid:
21389 elt = build_string ("*invalid*");
21390 goto tail_recurse;
21391 }
21392
21393 /* Pad to FIELD_WIDTH. */
21394 if (field_width > 0 && n < field_width)
21395 {
21396 switch (mode_line_target)
21397 {
21398 case MODE_LINE_NOPROP:
21399 case MODE_LINE_TITLE:
21400 n += store_mode_line_noprop ("", field_width - n, 0);
21401 break;
21402 case MODE_LINE_STRING:
21403 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21404 break;
21405 case MODE_LINE_DISPLAY:
21406 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21407 0, 0, 0);
21408 break;
21409 }
21410 }
21411
21412 return n;
21413 }
21414
21415 /* Store a mode-line string element in mode_line_string_list.
21416
21417 If STRING is non-null, display that C string. Otherwise, the Lisp
21418 string LISP_STRING is displayed.
21419
21420 FIELD_WIDTH is the minimum number of output glyphs to produce.
21421 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21422 with spaces. FIELD_WIDTH <= 0 means don't pad.
21423
21424 PRECISION is the maximum number of characters to output from
21425 STRING. PRECISION <= 0 means don't truncate the string.
21426
21427 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21428 properties to the string.
21429
21430 PROPS are the properties to add to the string.
21431 The mode_line_string_face face property is always added to the string.
21432 */
21433
21434 static int
21435 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21436 int field_width, int precision, Lisp_Object props)
21437 {
21438 ptrdiff_t len;
21439 int n = 0;
21440
21441 if (string != NULL)
21442 {
21443 len = strlen (string);
21444 if (precision > 0 && len > precision)
21445 len = precision;
21446 lisp_string = make_string (string, len);
21447 if (NILP (props))
21448 props = mode_line_string_face_prop;
21449 else if (!NILP (mode_line_string_face))
21450 {
21451 Lisp_Object face = Fplist_get (props, Qface);
21452 props = Fcopy_sequence (props);
21453 if (NILP (face))
21454 face = mode_line_string_face;
21455 else
21456 face = list2 (face, mode_line_string_face);
21457 props = Fplist_put (props, Qface, face);
21458 }
21459 Fadd_text_properties (make_number (0), make_number (len),
21460 props, lisp_string);
21461 }
21462 else
21463 {
21464 len = XFASTINT (Flength (lisp_string));
21465 if (precision > 0 && len > precision)
21466 {
21467 len = precision;
21468 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21469 precision = -1;
21470 }
21471 if (!NILP (mode_line_string_face))
21472 {
21473 Lisp_Object face;
21474 if (NILP (props))
21475 props = Ftext_properties_at (make_number (0), lisp_string);
21476 face = Fplist_get (props, Qface);
21477 if (NILP (face))
21478 face = mode_line_string_face;
21479 else
21480 face = list2 (face, mode_line_string_face);
21481 props = list2 (Qface, face);
21482 if (copy_string)
21483 lisp_string = Fcopy_sequence (lisp_string);
21484 }
21485 if (!NILP (props))
21486 Fadd_text_properties (make_number (0), make_number (len),
21487 props, lisp_string);
21488 }
21489
21490 if (len > 0)
21491 {
21492 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21493 n += len;
21494 }
21495
21496 if (field_width > len)
21497 {
21498 field_width -= len;
21499 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21500 if (!NILP (props))
21501 Fadd_text_properties (make_number (0), make_number (field_width),
21502 props, lisp_string);
21503 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21504 n += field_width;
21505 }
21506
21507 return n;
21508 }
21509
21510
21511 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21512 1, 4, 0,
21513 doc: /* Format a string out of a mode line format specification.
21514 First arg FORMAT specifies the mode line format (see `mode-line-format'
21515 for details) to use.
21516
21517 By default, the format is evaluated for the currently selected window.
21518
21519 Optional second arg FACE specifies the face property to put on all
21520 characters for which no face is specified. The value nil means the
21521 default face. The value t means whatever face the window's mode line
21522 currently uses (either `mode-line' or `mode-line-inactive',
21523 depending on whether the window is the selected window or not).
21524 An integer value means the value string has no text
21525 properties.
21526
21527 Optional third and fourth args WINDOW and BUFFER specify the window
21528 and buffer to use as the context for the formatting (defaults
21529 are the selected window and the WINDOW's buffer). */)
21530 (Lisp_Object format, Lisp_Object face,
21531 Lisp_Object window, Lisp_Object buffer)
21532 {
21533 struct it it;
21534 int len;
21535 struct window *w;
21536 struct buffer *old_buffer = NULL;
21537 int face_id;
21538 int no_props = INTEGERP (face);
21539 ptrdiff_t count = SPECPDL_INDEX ();
21540 Lisp_Object str;
21541 int string_start = 0;
21542
21543 w = decode_any_window (window);
21544 XSETWINDOW (window, w);
21545
21546 if (NILP (buffer))
21547 buffer = w->contents;
21548 CHECK_BUFFER (buffer);
21549
21550 /* Make formatting the modeline a non-op when noninteractive, otherwise
21551 there will be problems later caused by a partially initialized frame. */
21552 if (NILP (format) || noninteractive)
21553 return empty_unibyte_string;
21554
21555 if (no_props)
21556 face = Qnil;
21557
21558 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21559 : EQ (face, Qt) ? (EQ (window, selected_window)
21560 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21561 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21562 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21563 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21564 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21565 : DEFAULT_FACE_ID;
21566
21567 old_buffer = current_buffer;
21568
21569 /* Save things including mode_line_proptrans_alist,
21570 and set that to nil so that we don't alter the outer value. */
21571 record_unwind_protect (unwind_format_mode_line,
21572 format_mode_line_unwind_data
21573 (XFRAME (WINDOW_FRAME (w)),
21574 old_buffer, selected_window, 1));
21575 mode_line_proptrans_alist = Qnil;
21576
21577 Fselect_window (window, Qt);
21578 set_buffer_internal_1 (XBUFFER (buffer));
21579
21580 init_iterator (&it, w, -1, -1, NULL, face_id);
21581
21582 if (no_props)
21583 {
21584 mode_line_target = MODE_LINE_NOPROP;
21585 mode_line_string_face_prop = Qnil;
21586 mode_line_string_list = Qnil;
21587 string_start = MODE_LINE_NOPROP_LEN (0);
21588 }
21589 else
21590 {
21591 mode_line_target = MODE_LINE_STRING;
21592 mode_line_string_list = Qnil;
21593 mode_line_string_face = face;
21594 mode_line_string_face_prop
21595 = NILP (face) ? Qnil : list2 (Qface, face);
21596 }
21597
21598 push_kboard (FRAME_KBOARD (it.f));
21599 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21600 pop_kboard ();
21601
21602 if (no_props)
21603 {
21604 len = MODE_LINE_NOPROP_LEN (string_start);
21605 str = make_string (mode_line_noprop_buf + string_start, len);
21606 }
21607 else
21608 {
21609 mode_line_string_list = Fnreverse (mode_line_string_list);
21610 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21611 empty_unibyte_string);
21612 }
21613
21614 unbind_to (count, Qnil);
21615 return str;
21616 }
21617
21618 /* Write a null-terminated, right justified decimal representation of
21619 the positive integer D to BUF using a minimal field width WIDTH. */
21620
21621 static void
21622 pint2str (register char *buf, register int width, register ptrdiff_t d)
21623 {
21624 register char *p = buf;
21625
21626 if (d <= 0)
21627 *p++ = '0';
21628 else
21629 {
21630 while (d > 0)
21631 {
21632 *p++ = d % 10 + '0';
21633 d /= 10;
21634 }
21635 }
21636
21637 for (width -= (int) (p - buf); width > 0; --width)
21638 *p++ = ' ';
21639 *p-- = '\0';
21640 while (p > buf)
21641 {
21642 d = *buf;
21643 *buf++ = *p;
21644 *p-- = d;
21645 }
21646 }
21647
21648 /* Write a null-terminated, right justified decimal and "human
21649 readable" representation of the nonnegative integer D to BUF using
21650 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21651
21652 static const char power_letter[] =
21653 {
21654 0, /* no letter */
21655 'k', /* kilo */
21656 'M', /* mega */
21657 'G', /* giga */
21658 'T', /* tera */
21659 'P', /* peta */
21660 'E', /* exa */
21661 'Z', /* zetta */
21662 'Y' /* yotta */
21663 };
21664
21665 static void
21666 pint2hrstr (char *buf, int width, ptrdiff_t d)
21667 {
21668 /* We aim to represent the nonnegative integer D as
21669 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21670 ptrdiff_t quotient = d;
21671 int remainder = 0;
21672 /* -1 means: do not use TENTHS. */
21673 int tenths = -1;
21674 int exponent = 0;
21675
21676 /* Length of QUOTIENT.TENTHS as a string. */
21677 int length;
21678
21679 char * psuffix;
21680 char * p;
21681
21682 if (quotient >= 1000)
21683 {
21684 /* Scale to the appropriate EXPONENT. */
21685 do
21686 {
21687 remainder = quotient % 1000;
21688 quotient /= 1000;
21689 exponent++;
21690 }
21691 while (quotient >= 1000);
21692
21693 /* Round to nearest and decide whether to use TENTHS or not. */
21694 if (quotient <= 9)
21695 {
21696 tenths = remainder / 100;
21697 if (remainder % 100 >= 50)
21698 {
21699 if (tenths < 9)
21700 tenths++;
21701 else
21702 {
21703 quotient++;
21704 if (quotient == 10)
21705 tenths = -1;
21706 else
21707 tenths = 0;
21708 }
21709 }
21710 }
21711 else
21712 if (remainder >= 500)
21713 {
21714 if (quotient < 999)
21715 quotient++;
21716 else
21717 {
21718 quotient = 1;
21719 exponent++;
21720 tenths = 0;
21721 }
21722 }
21723 }
21724
21725 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21726 if (tenths == -1 && quotient <= 99)
21727 if (quotient <= 9)
21728 length = 1;
21729 else
21730 length = 2;
21731 else
21732 length = 3;
21733 p = psuffix = buf + max (width, length);
21734
21735 /* Print EXPONENT. */
21736 *psuffix++ = power_letter[exponent];
21737 *psuffix = '\0';
21738
21739 /* Print TENTHS. */
21740 if (tenths >= 0)
21741 {
21742 *--p = '0' + tenths;
21743 *--p = '.';
21744 }
21745
21746 /* Print QUOTIENT. */
21747 do
21748 {
21749 int digit = quotient % 10;
21750 *--p = '0' + digit;
21751 }
21752 while ((quotient /= 10) != 0);
21753
21754 /* Print leading spaces. */
21755 while (buf < p)
21756 *--p = ' ';
21757 }
21758
21759 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21760 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21761 type of CODING_SYSTEM. Return updated pointer into BUF. */
21762
21763 static unsigned char invalid_eol_type[] = "(*invalid*)";
21764
21765 static char *
21766 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21767 {
21768 Lisp_Object val;
21769 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21770 const unsigned char *eol_str;
21771 int eol_str_len;
21772 /* The EOL conversion we are using. */
21773 Lisp_Object eoltype;
21774
21775 val = CODING_SYSTEM_SPEC (coding_system);
21776 eoltype = Qnil;
21777
21778 if (!VECTORP (val)) /* Not yet decided. */
21779 {
21780 *buf++ = multibyte ? '-' : ' ';
21781 if (eol_flag)
21782 eoltype = eol_mnemonic_undecided;
21783 /* Don't mention EOL conversion if it isn't decided. */
21784 }
21785 else
21786 {
21787 Lisp_Object attrs;
21788 Lisp_Object eolvalue;
21789
21790 attrs = AREF (val, 0);
21791 eolvalue = AREF (val, 2);
21792
21793 *buf++ = multibyte
21794 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21795 : ' ';
21796
21797 if (eol_flag)
21798 {
21799 /* The EOL conversion that is normal on this system. */
21800
21801 if (NILP (eolvalue)) /* Not yet decided. */
21802 eoltype = eol_mnemonic_undecided;
21803 else if (VECTORP (eolvalue)) /* Not yet decided. */
21804 eoltype = eol_mnemonic_undecided;
21805 else /* eolvalue is Qunix, Qdos, or Qmac. */
21806 eoltype = (EQ (eolvalue, Qunix)
21807 ? eol_mnemonic_unix
21808 : (EQ (eolvalue, Qdos) == 1
21809 ? eol_mnemonic_dos : eol_mnemonic_mac));
21810 }
21811 }
21812
21813 if (eol_flag)
21814 {
21815 /* Mention the EOL conversion if it is not the usual one. */
21816 if (STRINGP (eoltype))
21817 {
21818 eol_str = SDATA (eoltype);
21819 eol_str_len = SBYTES (eoltype);
21820 }
21821 else if (CHARACTERP (eoltype))
21822 {
21823 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21824 int c = XFASTINT (eoltype);
21825 eol_str_len = CHAR_STRING (c, tmp);
21826 eol_str = tmp;
21827 }
21828 else
21829 {
21830 eol_str = invalid_eol_type;
21831 eol_str_len = sizeof (invalid_eol_type) - 1;
21832 }
21833 memcpy (buf, eol_str, eol_str_len);
21834 buf += eol_str_len;
21835 }
21836
21837 return buf;
21838 }
21839
21840 /* Return a string for the output of a mode line %-spec for window W,
21841 generated by character C. FIELD_WIDTH > 0 means pad the string
21842 returned with spaces to that value. Return a Lisp string in
21843 *STRING if the resulting string is taken from that Lisp string.
21844
21845 Note we operate on the current buffer for most purposes. */
21846
21847 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21848
21849 static const char *
21850 decode_mode_spec (struct window *w, register int c, int field_width,
21851 Lisp_Object *string)
21852 {
21853 Lisp_Object obj;
21854 struct frame *f = XFRAME (WINDOW_FRAME (w));
21855 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21856 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21857 produce strings from numerical values, so limit preposterously
21858 large values of FIELD_WIDTH to avoid overrunning the buffer's
21859 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21860 bytes plus the terminating null. */
21861 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21862 struct buffer *b = current_buffer;
21863
21864 obj = Qnil;
21865 *string = Qnil;
21866
21867 switch (c)
21868 {
21869 case '*':
21870 if (!NILP (BVAR (b, read_only)))
21871 return "%";
21872 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21873 return "*";
21874 return "-";
21875
21876 case '+':
21877 /* This differs from %* only for a modified read-only buffer. */
21878 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21879 return "*";
21880 if (!NILP (BVAR (b, read_only)))
21881 return "%";
21882 return "-";
21883
21884 case '&':
21885 /* This differs from %* in ignoring read-only-ness. */
21886 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21887 return "*";
21888 return "-";
21889
21890 case '%':
21891 return "%";
21892
21893 case '[':
21894 {
21895 int i;
21896 char *p;
21897
21898 if (command_loop_level > 5)
21899 return "[[[... ";
21900 p = decode_mode_spec_buf;
21901 for (i = 0; i < command_loop_level; i++)
21902 *p++ = '[';
21903 *p = 0;
21904 return decode_mode_spec_buf;
21905 }
21906
21907 case ']':
21908 {
21909 int i;
21910 char *p;
21911
21912 if (command_loop_level > 5)
21913 return " ...]]]";
21914 p = decode_mode_spec_buf;
21915 for (i = 0; i < command_loop_level; i++)
21916 *p++ = ']';
21917 *p = 0;
21918 return decode_mode_spec_buf;
21919 }
21920
21921 case '-':
21922 {
21923 register int i;
21924
21925 /* Let lots_of_dashes be a string of infinite length. */
21926 if (mode_line_target == MODE_LINE_NOPROP
21927 || mode_line_target == MODE_LINE_STRING)
21928 return "--";
21929 if (field_width <= 0
21930 || field_width > sizeof (lots_of_dashes))
21931 {
21932 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21933 decode_mode_spec_buf[i] = '-';
21934 decode_mode_spec_buf[i] = '\0';
21935 return decode_mode_spec_buf;
21936 }
21937 else
21938 return lots_of_dashes;
21939 }
21940
21941 case 'b':
21942 obj = BVAR (b, name);
21943 break;
21944
21945 case 'c':
21946 /* %c and %l are ignored in `frame-title-format'.
21947 (In redisplay_internal, the frame title is drawn _before_ the
21948 windows are updated, so the stuff which depends on actual
21949 window contents (such as %l) may fail to render properly, or
21950 even crash emacs.) */
21951 if (mode_line_target == MODE_LINE_TITLE)
21952 return "";
21953 else
21954 {
21955 ptrdiff_t col = current_column ();
21956 w->column_number_displayed = col;
21957 pint2str (decode_mode_spec_buf, width, col);
21958 return decode_mode_spec_buf;
21959 }
21960
21961 case 'e':
21962 #ifndef SYSTEM_MALLOC
21963 {
21964 if (NILP (Vmemory_full))
21965 return "";
21966 else
21967 return "!MEM FULL! ";
21968 }
21969 #else
21970 return "";
21971 #endif
21972
21973 case 'F':
21974 /* %F displays the frame name. */
21975 if (!NILP (f->title))
21976 return SSDATA (f->title);
21977 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21978 return SSDATA (f->name);
21979 return "Emacs";
21980
21981 case 'f':
21982 obj = BVAR (b, filename);
21983 break;
21984
21985 case 'i':
21986 {
21987 ptrdiff_t size = ZV - BEGV;
21988 pint2str (decode_mode_spec_buf, width, size);
21989 return decode_mode_spec_buf;
21990 }
21991
21992 case 'I':
21993 {
21994 ptrdiff_t size = ZV - BEGV;
21995 pint2hrstr (decode_mode_spec_buf, width, size);
21996 return decode_mode_spec_buf;
21997 }
21998
21999 case 'l':
22000 {
22001 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22002 ptrdiff_t topline, nlines, height;
22003 ptrdiff_t junk;
22004
22005 /* %c and %l are ignored in `frame-title-format'. */
22006 if (mode_line_target == MODE_LINE_TITLE)
22007 return "";
22008
22009 startpos = marker_position (w->start);
22010 startpos_byte = marker_byte_position (w->start);
22011 height = WINDOW_TOTAL_LINES (w);
22012
22013 /* If we decided that this buffer isn't suitable for line numbers,
22014 don't forget that too fast. */
22015 if (w->base_line_pos == -1)
22016 goto no_value;
22017
22018 /* If the buffer is very big, don't waste time. */
22019 if (INTEGERP (Vline_number_display_limit)
22020 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22021 {
22022 w->base_line_pos = 0;
22023 w->base_line_number = 0;
22024 goto no_value;
22025 }
22026
22027 if (w->base_line_number > 0
22028 && w->base_line_pos > 0
22029 && w->base_line_pos <= startpos)
22030 {
22031 line = w->base_line_number;
22032 linepos = w->base_line_pos;
22033 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22034 }
22035 else
22036 {
22037 line = 1;
22038 linepos = BUF_BEGV (b);
22039 linepos_byte = BUF_BEGV_BYTE (b);
22040 }
22041
22042 /* Count lines from base line to window start position. */
22043 nlines = display_count_lines (linepos_byte,
22044 startpos_byte,
22045 startpos, &junk);
22046
22047 topline = nlines + line;
22048
22049 /* Determine a new base line, if the old one is too close
22050 or too far away, or if we did not have one.
22051 "Too close" means it's plausible a scroll-down would
22052 go back past it. */
22053 if (startpos == BUF_BEGV (b))
22054 {
22055 w->base_line_number = topline;
22056 w->base_line_pos = BUF_BEGV (b);
22057 }
22058 else if (nlines < height + 25 || nlines > height * 3 + 50
22059 || linepos == BUF_BEGV (b))
22060 {
22061 ptrdiff_t limit = BUF_BEGV (b);
22062 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22063 ptrdiff_t position;
22064 ptrdiff_t distance =
22065 (height * 2 + 30) * line_number_display_limit_width;
22066
22067 if (startpos - distance > limit)
22068 {
22069 limit = startpos - distance;
22070 limit_byte = CHAR_TO_BYTE (limit);
22071 }
22072
22073 nlines = display_count_lines (startpos_byte,
22074 limit_byte,
22075 - (height * 2 + 30),
22076 &position);
22077 /* If we couldn't find the lines we wanted within
22078 line_number_display_limit_width chars per line,
22079 give up on line numbers for this window. */
22080 if (position == limit_byte && limit == startpos - distance)
22081 {
22082 w->base_line_pos = -1;
22083 w->base_line_number = 0;
22084 goto no_value;
22085 }
22086
22087 w->base_line_number = topline - nlines;
22088 w->base_line_pos = BYTE_TO_CHAR (position);
22089 }
22090
22091 /* Now count lines from the start pos to point. */
22092 nlines = display_count_lines (startpos_byte,
22093 PT_BYTE, PT, &junk);
22094
22095 /* Record that we did display the line number. */
22096 line_number_displayed = 1;
22097
22098 /* Make the string to show. */
22099 pint2str (decode_mode_spec_buf, width, topline + nlines);
22100 return decode_mode_spec_buf;
22101 no_value:
22102 {
22103 char* p = decode_mode_spec_buf;
22104 int pad = width - 2;
22105 while (pad-- > 0)
22106 *p++ = ' ';
22107 *p++ = '?';
22108 *p++ = '?';
22109 *p = '\0';
22110 return decode_mode_spec_buf;
22111 }
22112 }
22113 break;
22114
22115 case 'm':
22116 obj = BVAR (b, mode_name);
22117 break;
22118
22119 case 'n':
22120 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22121 return " Narrow";
22122 break;
22123
22124 case 'p':
22125 {
22126 ptrdiff_t pos = marker_position (w->start);
22127 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22128
22129 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22130 {
22131 if (pos <= BUF_BEGV (b))
22132 return "All";
22133 else
22134 return "Bottom";
22135 }
22136 else if (pos <= BUF_BEGV (b))
22137 return "Top";
22138 else
22139 {
22140 if (total > 1000000)
22141 /* Do it differently for a large value, to avoid overflow. */
22142 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22143 else
22144 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22145 /* We can't normally display a 3-digit number,
22146 so get us a 2-digit number that is close. */
22147 if (total == 100)
22148 total = 99;
22149 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22150 return decode_mode_spec_buf;
22151 }
22152 }
22153
22154 /* Display percentage of size above the bottom of the screen. */
22155 case 'P':
22156 {
22157 ptrdiff_t toppos = marker_position (w->start);
22158 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22159 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22160
22161 if (botpos >= BUF_ZV (b))
22162 {
22163 if (toppos <= BUF_BEGV (b))
22164 return "All";
22165 else
22166 return "Bottom";
22167 }
22168 else
22169 {
22170 if (total > 1000000)
22171 /* Do it differently for a large value, to avoid overflow. */
22172 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22173 else
22174 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22175 /* We can't normally display a 3-digit number,
22176 so get us a 2-digit number that is close. */
22177 if (total == 100)
22178 total = 99;
22179 if (toppos <= BUF_BEGV (b))
22180 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22181 else
22182 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22183 return decode_mode_spec_buf;
22184 }
22185 }
22186
22187 case 's':
22188 /* status of process */
22189 obj = Fget_buffer_process (Fcurrent_buffer ());
22190 if (NILP (obj))
22191 return "no process";
22192 #ifndef MSDOS
22193 obj = Fsymbol_name (Fprocess_status (obj));
22194 #endif
22195 break;
22196
22197 case '@':
22198 {
22199 ptrdiff_t count = inhibit_garbage_collection ();
22200 Lisp_Object val = call1 (intern ("file-remote-p"),
22201 BVAR (current_buffer, directory));
22202 unbind_to (count, Qnil);
22203
22204 if (NILP (val))
22205 return "-";
22206 else
22207 return "@";
22208 }
22209
22210 case 'z':
22211 /* coding-system (not including end-of-line format) */
22212 case 'Z':
22213 /* coding-system (including end-of-line type) */
22214 {
22215 int eol_flag = (c == 'Z');
22216 char *p = decode_mode_spec_buf;
22217
22218 if (! FRAME_WINDOW_P (f))
22219 {
22220 /* No need to mention EOL here--the terminal never needs
22221 to do EOL conversion. */
22222 p = decode_mode_spec_coding (CODING_ID_NAME
22223 (FRAME_KEYBOARD_CODING (f)->id),
22224 p, 0);
22225 p = decode_mode_spec_coding (CODING_ID_NAME
22226 (FRAME_TERMINAL_CODING (f)->id),
22227 p, 0);
22228 }
22229 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22230 p, eol_flag);
22231
22232 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22233 #ifdef subprocesses
22234 obj = Fget_buffer_process (Fcurrent_buffer ());
22235 if (PROCESSP (obj))
22236 {
22237 p = decode_mode_spec_coding
22238 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22239 p = decode_mode_spec_coding
22240 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22241 }
22242 #endif /* subprocesses */
22243 #endif /* 0 */
22244 *p = 0;
22245 return decode_mode_spec_buf;
22246 }
22247 }
22248
22249 if (STRINGP (obj))
22250 {
22251 *string = obj;
22252 return SSDATA (obj);
22253 }
22254 else
22255 return "";
22256 }
22257
22258
22259 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22260 means count lines back from START_BYTE. But don't go beyond
22261 LIMIT_BYTE. Return the number of lines thus found (always
22262 nonnegative).
22263
22264 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22265 either the position COUNT lines after/before START_BYTE, if we
22266 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22267 COUNT lines. */
22268
22269 static ptrdiff_t
22270 display_count_lines (ptrdiff_t start_byte,
22271 ptrdiff_t limit_byte, ptrdiff_t count,
22272 ptrdiff_t *byte_pos_ptr)
22273 {
22274 register unsigned char *cursor;
22275 unsigned char *base;
22276
22277 register ptrdiff_t ceiling;
22278 register unsigned char *ceiling_addr;
22279 ptrdiff_t orig_count = count;
22280
22281 /* If we are not in selective display mode,
22282 check only for newlines. */
22283 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22284 && !INTEGERP (BVAR (current_buffer, selective_display)));
22285
22286 if (count > 0)
22287 {
22288 while (start_byte < limit_byte)
22289 {
22290 ceiling = BUFFER_CEILING_OF (start_byte);
22291 ceiling = min (limit_byte - 1, ceiling);
22292 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22293 base = (cursor = BYTE_POS_ADDR (start_byte));
22294
22295 do
22296 {
22297 if (selective_display)
22298 {
22299 while (*cursor != '\n' && *cursor != 015
22300 && ++cursor != ceiling_addr)
22301 continue;
22302 if (cursor == ceiling_addr)
22303 break;
22304 }
22305 else
22306 {
22307 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22308 if (! cursor)
22309 break;
22310 }
22311
22312 cursor++;
22313
22314 if (--count == 0)
22315 {
22316 start_byte += cursor - base;
22317 *byte_pos_ptr = start_byte;
22318 return orig_count;
22319 }
22320 }
22321 while (cursor < ceiling_addr);
22322
22323 start_byte += ceiling_addr - base;
22324 }
22325 }
22326 else
22327 {
22328 while (start_byte > limit_byte)
22329 {
22330 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22331 ceiling = max (limit_byte, ceiling);
22332 ceiling_addr = BYTE_POS_ADDR (ceiling);
22333 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22334 while (1)
22335 {
22336 if (selective_display)
22337 {
22338 while (--cursor >= ceiling_addr
22339 && *cursor != '\n' && *cursor != 015)
22340 continue;
22341 if (cursor < ceiling_addr)
22342 break;
22343 }
22344 else
22345 {
22346 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22347 if (! cursor)
22348 break;
22349 }
22350
22351 if (++count == 0)
22352 {
22353 start_byte += cursor - base + 1;
22354 *byte_pos_ptr = start_byte;
22355 /* When scanning backwards, we should
22356 not count the newline posterior to which we stop. */
22357 return - orig_count - 1;
22358 }
22359 }
22360 start_byte += ceiling_addr - base;
22361 }
22362 }
22363
22364 *byte_pos_ptr = limit_byte;
22365
22366 if (count < 0)
22367 return - orig_count + count;
22368 return orig_count - count;
22369
22370 }
22371
22372
22373 \f
22374 /***********************************************************************
22375 Displaying strings
22376 ***********************************************************************/
22377
22378 /* Display a NUL-terminated string, starting with index START.
22379
22380 If STRING is non-null, display that C string. Otherwise, the Lisp
22381 string LISP_STRING is displayed. There's a case that STRING is
22382 non-null and LISP_STRING is not nil. It means STRING is a string
22383 data of LISP_STRING. In that case, we display LISP_STRING while
22384 ignoring its text properties.
22385
22386 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22387 FACE_STRING. Display STRING or LISP_STRING with the face at
22388 FACE_STRING_POS in FACE_STRING:
22389
22390 Display the string in the environment given by IT, but use the
22391 standard display table, temporarily.
22392
22393 FIELD_WIDTH is the minimum number of output glyphs to produce.
22394 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22395 with spaces. If STRING has more characters, more than FIELD_WIDTH
22396 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22397
22398 PRECISION is the maximum number of characters to output from
22399 STRING. PRECISION < 0 means don't truncate the string.
22400
22401 This is roughly equivalent to printf format specifiers:
22402
22403 FIELD_WIDTH PRECISION PRINTF
22404 ----------------------------------------
22405 -1 -1 %s
22406 -1 10 %.10s
22407 10 -1 %10s
22408 20 10 %20.10s
22409
22410 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22411 display them, and < 0 means obey the current buffer's value of
22412 enable_multibyte_characters.
22413
22414 Value is the number of columns displayed. */
22415
22416 static int
22417 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22418 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22419 int field_width, int precision, int max_x, int multibyte)
22420 {
22421 int hpos_at_start = it->hpos;
22422 int saved_face_id = it->face_id;
22423 struct glyph_row *row = it->glyph_row;
22424 ptrdiff_t it_charpos;
22425
22426 /* Initialize the iterator IT for iteration over STRING beginning
22427 with index START. */
22428 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22429 precision, field_width, multibyte);
22430 if (string && STRINGP (lisp_string))
22431 /* LISP_STRING is the one returned by decode_mode_spec. We should
22432 ignore its text properties. */
22433 it->stop_charpos = it->end_charpos;
22434
22435 /* If displaying STRING, set up the face of the iterator from
22436 FACE_STRING, if that's given. */
22437 if (STRINGP (face_string))
22438 {
22439 ptrdiff_t endptr;
22440 struct face *face;
22441
22442 it->face_id
22443 = face_at_string_position (it->w, face_string, face_string_pos,
22444 0, it->region_beg_charpos,
22445 it->region_end_charpos,
22446 &endptr, it->base_face_id, 0);
22447 face = FACE_FROM_ID (it->f, it->face_id);
22448 it->face_box_p = face->box != FACE_NO_BOX;
22449 }
22450
22451 /* Set max_x to the maximum allowed X position. Don't let it go
22452 beyond the right edge of the window. */
22453 if (max_x <= 0)
22454 max_x = it->last_visible_x;
22455 else
22456 max_x = min (max_x, it->last_visible_x);
22457
22458 /* Skip over display elements that are not visible. because IT->w is
22459 hscrolled. */
22460 if (it->current_x < it->first_visible_x)
22461 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22462 MOVE_TO_POS | MOVE_TO_X);
22463
22464 row->ascent = it->max_ascent;
22465 row->height = it->max_ascent + it->max_descent;
22466 row->phys_ascent = it->max_phys_ascent;
22467 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22468 row->extra_line_spacing = it->max_extra_line_spacing;
22469
22470 if (STRINGP (it->string))
22471 it_charpos = IT_STRING_CHARPOS (*it);
22472 else
22473 it_charpos = IT_CHARPOS (*it);
22474
22475 /* This condition is for the case that we are called with current_x
22476 past last_visible_x. */
22477 while (it->current_x < max_x)
22478 {
22479 int x_before, x, n_glyphs_before, i, nglyphs;
22480
22481 /* Get the next display element. */
22482 if (!get_next_display_element (it))
22483 break;
22484
22485 /* Produce glyphs. */
22486 x_before = it->current_x;
22487 n_glyphs_before = row->used[TEXT_AREA];
22488 PRODUCE_GLYPHS (it);
22489
22490 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22491 i = 0;
22492 x = x_before;
22493 while (i < nglyphs)
22494 {
22495 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22496
22497 if (it->line_wrap != TRUNCATE
22498 && x + glyph->pixel_width > max_x)
22499 {
22500 /* End of continued line or max_x reached. */
22501 if (CHAR_GLYPH_PADDING_P (*glyph))
22502 {
22503 /* A wide character is unbreakable. */
22504 if (row->reversed_p)
22505 unproduce_glyphs (it, row->used[TEXT_AREA]
22506 - n_glyphs_before);
22507 row->used[TEXT_AREA] = n_glyphs_before;
22508 it->current_x = x_before;
22509 }
22510 else
22511 {
22512 if (row->reversed_p)
22513 unproduce_glyphs (it, row->used[TEXT_AREA]
22514 - (n_glyphs_before + i));
22515 row->used[TEXT_AREA] = n_glyphs_before + i;
22516 it->current_x = x;
22517 }
22518 break;
22519 }
22520 else if (x + glyph->pixel_width >= it->first_visible_x)
22521 {
22522 /* Glyph is at least partially visible. */
22523 ++it->hpos;
22524 if (x < it->first_visible_x)
22525 row->x = x - it->first_visible_x;
22526 }
22527 else
22528 {
22529 /* Glyph is off the left margin of the display area.
22530 Should not happen. */
22531 emacs_abort ();
22532 }
22533
22534 row->ascent = max (row->ascent, it->max_ascent);
22535 row->height = max (row->height, it->max_ascent + it->max_descent);
22536 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22537 row->phys_height = max (row->phys_height,
22538 it->max_phys_ascent + it->max_phys_descent);
22539 row->extra_line_spacing = max (row->extra_line_spacing,
22540 it->max_extra_line_spacing);
22541 x += glyph->pixel_width;
22542 ++i;
22543 }
22544
22545 /* Stop if max_x reached. */
22546 if (i < nglyphs)
22547 break;
22548
22549 /* Stop at line ends. */
22550 if (ITERATOR_AT_END_OF_LINE_P (it))
22551 {
22552 it->continuation_lines_width = 0;
22553 break;
22554 }
22555
22556 set_iterator_to_next (it, 1);
22557 if (STRINGP (it->string))
22558 it_charpos = IT_STRING_CHARPOS (*it);
22559 else
22560 it_charpos = IT_CHARPOS (*it);
22561
22562 /* Stop if truncating at the right edge. */
22563 if (it->line_wrap == TRUNCATE
22564 && it->current_x >= it->last_visible_x)
22565 {
22566 /* Add truncation mark, but don't do it if the line is
22567 truncated at a padding space. */
22568 if (it_charpos < it->string_nchars)
22569 {
22570 if (!FRAME_WINDOW_P (it->f))
22571 {
22572 int ii, n;
22573
22574 if (it->current_x > it->last_visible_x)
22575 {
22576 if (!row->reversed_p)
22577 {
22578 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22579 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22580 break;
22581 }
22582 else
22583 {
22584 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22585 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22586 break;
22587 unproduce_glyphs (it, ii + 1);
22588 ii = row->used[TEXT_AREA] - (ii + 1);
22589 }
22590 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22591 {
22592 row->used[TEXT_AREA] = ii;
22593 produce_special_glyphs (it, IT_TRUNCATION);
22594 }
22595 }
22596 produce_special_glyphs (it, IT_TRUNCATION);
22597 }
22598 row->truncated_on_right_p = 1;
22599 }
22600 break;
22601 }
22602 }
22603
22604 /* Maybe insert a truncation at the left. */
22605 if (it->first_visible_x
22606 && it_charpos > 0)
22607 {
22608 if (!FRAME_WINDOW_P (it->f)
22609 || (row->reversed_p
22610 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22611 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22612 insert_left_trunc_glyphs (it);
22613 row->truncated_on_left_p = 1;
22614 }
22615
22616 it->face_id = saved_face_id;
22617
22618 /* Value is number of columns displayed. */
22619 return it->hpos - hpos_at_start;
22620 }
22621
22622
22623 \f
22624 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22625 appears as an element of LIST or as the car of an element of LIST.
22626 If PROPVAL is a list, compare each element against LIST in that
22627 way, and return 1/2 if any element of PROPVAL is found in LIST.
22628 Otherwise return 0. This function cannot quit.
22629 The return value is 2 if the text is invisible but with an ellipsis
22630 and 1 if it's invisible and without an ellipsis. */
22631
22632 int
22633 invisible_p (register Lisp_Object propval, Lisp_Object list)
22634 {
22635 register Lisp_Object tail, proptail;
22636
22637 for (tail = list; CONSP (tail); tail = XCDR (tail))
22638 {
22639 register Lisp_Object tem;
22640 tem = XCAR (tail);
22641 if (EQ (propval, tem))
22642 return 1;
22643 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22644 return NILP (XCDR (tem)) ? 1 : 2;
22645 }
22646
22647 if (CONSP (propval))
22648 {
22649 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22650 {
22651 Lisp_Object propelt;
22652 propelt = XCAR (proptail);
22653 for (tail = list; CONSP (tail); tail = XCDR (tail))
22654 {
22655 register Lisp_Object tem;
22656 tem = XCAR (tail);
22657 if (EQ (propelt, tem))
22658 return 1;
22659 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22660 return NILP (XCDR (tem)) ? 1 : 2;
22661 }
22662 }
22663 }
22664
22665 return 0;
22666 }
22667
22668 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22669 doc: /* Non-nil if the property makes the text invisible.
22670 POS-OR-PROP can be a marker or number, in which case it is taken to be
22671 a position in the current buffer and the value of the `invisible' property
22672 is checked; or it can be some other value, which is then presumed to be the
22673 value of the `invisible' property of the text of interest.
22674 The non-nil value returned can be t for truly invisible text or something
22675 else if the text is replaced by an ellipsis. */)
22676 (Lisp_Object pos_or_prop)
22677 {
22678 Lisp_Object prop
22679 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22680 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22681 : pos_or_prop);
22682 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22683 return (invis == 0 ? Qnil
22684 : invis == 1 ? Qt
22685 : make_number (invis));
22686 }
22687
22688 /* Calculate a width or height in pixels from a specification using
22689 the following elements:
22690
22691 SPEC ::=
22692 NUM - a (fractional) multiple of the default font width/height
22693 (NUM) - specifies exactly NUM pixels
22694 UNIT - a fixed number of pixels, see below.
22695 ELEMENT - size of a display element in pixels, see below.
22696 (NUM . SPEC) - equals NUM * SPEC
22697 (+ SPEC SPEC ...) - add pixel values
22698 (- SPEC SPEC ...) - subtract pixel values
22699 (- SPEC) - negate pixel value
22700
22701 NUM ::=
22702 INT or FLOAT - a number constant
22703 SYMBOL - use symbol's (buffer local) variable binding.
22704
22705 UNIT ::=
22706 in - pixels per inch *)
22707 mm - pixels per 1/1000 meter *)
22708 cm - pixels per 1/100 meter *)
22709 width - width of current font in pixels.
22710 height - height of current font in pixels.
22711
22712 *) using the ratio(s) defined in display-pixels-per-inch.
22713
22714 ELEMENT ::=
22715
22716 left-fringe - left fringe width in pixels
22717 right-fringe - right fringe width in pixels
22718
22719 left-margin - left margin width in pixels
22720 right-margin - right margin width in pixels
22721
22722 scroll-bar - scroll-bar area width in pixels
22723
22724 Examples:
22725
22726 Pixels corresponding to 5 inches:
22727 (5 . in)
22728
22729 Total width of non-text areas on left side of window (if scroll-bar is on left):
22730 '(space :width (+ left-fringe left-margin scroll-bar))
22731
22732 Align to first text column (in header line):
22733 '(space :align-to 0)
22734
22735 Align to middle of text area minus half the width of variable `my-image'
22736 containing a loaded image:
22737 '(space :align-to (0.5 . (- text my-image)))
22738
22739 Width of left margin minus width of 1 character in the default font:
22740 '(space :width (- left-margin 1))
22741
22742 Width of left margin minus width of 2 characters in the current font:
22743 '(space :width (- left-margin (2 . width)))
22744
22745 Center 1 character over left-margin (in header line):
22746 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22747
22748 Different ways to express width of left fringe plus left margin minus one pixel:
22749 '(space :width (- (+ left-fringe left-margin) (1)))
22750 '(space :width (+ left-fringe left-margin (- (1))))
22751 '(space :width (+ left-fringe left-margin (-1)))
22752
22753 */
22754
22755 static int
22756 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22757 struct font *font, int width_p, int *align_to)
22758 {
22759 double pixels;
22760
22761 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22762 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22763
22764 if (NILP (prop))
22765 return OK_PIXELS (0);
22766
22767 eassert (FRAME_LIVE_P (it->f));
22768
22769 if (SYMBOLP (prop))
22770 {
22771 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22772 {
22773 char *unit = SSDATA (SYMBOL_NAME (prop));
22774
22775 if (unit[0] == 'i' && unit[1] == 'n')
22776 pixels = 1.0;
22777 else if (unit[0] == 'm' && unit[1] == 'm')
22778 pixels = 25.4;
22779 else if (unit[0] == 'c' && unit[1] == 'm')
22780 pixels = 2.54;
22781 else
22782 pixels = 0;
22783 if (pixels > 0)
22784 {
22785 double ppi = (width_p ? FRAME_RES_X (it->f)
22786 : FRAME_RES_Y (it->f));
22787
22788 if (ppi > 0)
22789 return OK_PIXELS (ppi / pixels);
22790 return 0;
22791 }
22792 }
22793
22794 #ifdef HAVE_WINDOW_SYSTEM
22795 if (EQ (prop, Qheight))
22796 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22797 if (EQ (prop, Qwidth))
22798 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22799 #else
22800 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22801 return OK_PIXELS (1);
22802 #endif
22803
22804 if (EQ (prop, Qtext))
22805 return OK_PIXELS (width_p
22806 ? window_box_width (it->w, TEXT_AREA)
22807 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22808
22809 if (align_to && *align_to < 0)
22810 {
22811 *res = 0;
22812 if (EQ (prop, Qleft))
22813 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22814 if (EQ (prop, Qright))
22815 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22816 if (EQ (prop, Qcenter))
22817 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22818 + window_box_width (it->w, TEXT_AREA) / 2);
22819 if (EQ (prop, Qleft_fringe))
22820 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22821 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22822 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22823 if (EQ (prop, Qright_fringe))
22824 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22825 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22826 : window_box_right_offset (it->w, TEXT_AREA));
22827 if (EQ (prop, Qleft_margin))
22828 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22829 if (EQ (prop, Qright_margin))
22830 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22831 if (EQ (prop, Qscroll_bar))
22832 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22833 ? 0
22834 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22835 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22836 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22837 : 0)));
22838 }
22839 else
22840 {
22841 if (EQ (prop, Qleft_fringe))
22842 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22843 if (EQ (prop, Qright_fringe))
22844 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22845 if (EQ (prop, Qleft_margin))
22846 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22847 if (EQ (prop, Qright_margin))
22848 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22849 if (EQ (prop, Qscroll_bar))
22850 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22851 }
22852
22853 prop = buffer_local_value_1 (prop, it->w->contents);
22854 if (EQ (prop, Qunbound))
22855 prop = Qnil;
22856 }
22857
22858 if (INTEGERP (prop) || FLOATP (prop))
22859 {
22860 int base_unit = (width_p
22861 ? FRAME_COLUMN_WIDTH (it->f)
22862 : FRAME_LINE_HEIGHT (it->f));
22863 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22864 }
22865
22866 if (CONSP (prop))
22867 {
22868 Lisp_Object car = XCAR (prop);
22869 Lisp_Object cdr = XCDR (prop);
22870
22871 if (SYMBOLP (car))
22872 {
22873 #ifdef HAVE_WINDOW_SYSTEM
22874 if (FRAME_WINDOW_P (it->f)
22875 && valid_image_p (prop))
22876 {
22877 ptrdiff_t id = lookup_image (it->f, prop);
22878 struct image *img = IMAGE_FROM_ID (it->f, id);
22879
22880 return OK_PIXELS (width_p ? img->width : img->height);
22881 }
22882 #ifdef HAVE_XWIDGETS
22883 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
22884 {
22885 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
22886 return OK_PIXELS (width_p ? 100 : 100);
22887 }
22888 #endif
22889 #endif
22890 if (EQ (car, Qplus) || EQ (car, Qminus))
22891 {
22892 int first = 1;
22893 double px;
22894
22895 pixels = 0;
22896 while (CONSP (cdr))
22897 {
22898 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22899 font, width_p, align_to))
22900 return 0;
22901 if (first)
22902 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22903 else
22904 pixels += px;
22905 cdr = XCDR (cdr);
22906 }
22907 if (EQ (car, Qminus))
22908 pixels = -pixels;
22909 return OK_PIXELS (pixels);
22910 }
22911
22912 car = buffer_local_value_1 (car, it->w->contents);
22913 if (EQ (car, Qunbound))
22914 car = Qnil;
22915 }
22916
22917 if (INTEGERP (car) || FLOATP (car))
22918 {
22919 double fact;
22920 pixels = XFLOATINT (car);
22921 if (NILP (cdr))
22922 return OK_PIXELS (pixels);
22923 if (calc_pixel_width_or_height (&fact, it, cdr,
22924 font, width_p, align_to))
22925 return OK_PIXELS (pixels * fact);
22926 return 0;
22927 }
22928
22929 return 0;
22930 }
22931
22932 return 0;
22933 }
22934
22935 \f
22936 /***********************************************************************
22937 Glyph Display
22938 ***********************************************************************/
22939
22940 #ifdef HAVE_WINDOW_SYSTEM
22941
22942 #ifdef GLYPH_DEBUG
22943
22944 void
22945 dump_glyph_string (struct glyph_string *s)
22946 {
22947 fprintf (stderr, "glyph string\n");
22948 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22949 s->x, s->y, s->width, s->height);
22950 fprintf (stderr, " ybase = %d\n", s->ybase);
22951 fprintf (stderr, " hl = %d\n", s->hl);
22952 fprintf (stderr, " left overhang = %d, right = %d\n",
22953 s->left_overhang, s->right_overhang);
22954 fprintf (stderr, " nchars = %d\n", s->nchars);
22955 fprintf (stderr, " extends to end of line = %d\n",
22956 s->extends_to_end_of_line_p);
22957 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22958 fprintf (stderr, " bg width = %d\n", s->background_width);
22959 }
22960
22961 #endif /* GLYPH_DEBUG */
22962
22963 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22964 of XChar2b structures for S; it can't be allocated in
22965 init_glyph_string because it must be allocated via `alloca'. W
22966 is the window on which S is drawn. ROW and AREA are the glyph row
22967 and area within the row from which S is constructed. START is the
22968 index of the first glyph structure covered by S. HL is a
22969 face-override for drawing S. */
22970
22971 #ifdef HAVE_NTGUI
22972 #define OPTIONAL_HDC(hdc) HDC hdc,
22973 #define DECLARE_HDC(hdc) HDC hdc;
22974 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22975 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22976 #endif
22977
22978 #ifndef OPTIONAL_HDC
22979 #define OPTIONAL_HDC(hdc)
22980 #define DECLARE_HDC(hdc)
22981 #define ALLOCATE_HDC(hdc, f)
22982 #define RELEASE_HDC(hdc, f)
22983 #endif
22984
22985 static void
22986 init_glyph_string (struct glyph_string *s,
22987 OPTIONAL_HDC (hdc)
22988 XChar2b *char2b, struct window *w, struct glyph_row *row,
22989 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22990 {
22991 memset (s, 0, sizeof *s);
22992 s->w = w;
22993 s->f = XFRAME (w->frame);
22994 #ifdef HAVE_NTGUI
22995 s->hdc = hdc;
22996 #endif
22997 s->display = FRAME_X_DISPLAY (s->f);
22998 s->window = FRAME_X_WINDOW (s->f);
22999 s->char2b = char2b;
23000 s->hl = hl;
23001 s->row = row;
23002 s->area = area;
23003 s->first_glyph = row->glyphs[area] + start;
23004 s->height = row->height;
23005 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23006 s->ybase = s->y + row->ascent;
23007 }
23008
23009
23010 /* Append the list of glyph strings with head H and tail T to the list
23011 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23012
23013 static void
23014 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23015 struct glyph_string *h, struct glyph_string *t)
23016 {
23017 if (h)
23018 {
23019 if (*head)
23020 (*tail)->next = h;
23021 else
23022 *head = h;
23023 h->prev = *tail;
23024 *tail = t;
23025 }
23026 }
23027
23028
23029 /* Prepend the list of glyph strings with head H and tail T to the
23030 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23031 result. */
23032
23033 static void
23034 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23035 struct glyph_string *h, struct glyph_string *t)
23036 {
23037 if (h)
23038 {
23039 if (*head)
23040 (*head)->prev = t;
23041 else
23042 *tail = t;
23043 t->next = *head;
23044 *head = h;
23045 }
23046 }
23047
23048
23049 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23050 Set *HEAD and *TAIL to the resulting list. */
23051
23052 static void
23053 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23054 struct glyph_string *s)
23055 {
23056 s->next = s->prev = NULL;
23057 append_glyph_string_lists (head, tail, s, s);
23058 }
23059
23060
23061 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23062 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23063 make sure that X resources for the face returned are allocated.
23064 Value is a pointer to a realized face that is ready for display if
23065 DISPLAY_P is non-zero. */
23066
23067 static struct face *
23068 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23069 XChar2b *char2b, int display_p)
23070 {
23071 struct face *face = FACE_FROM_ID (f, face_id);
23072 unsigned code = 0;
23073
23074 if (face->font)
23075 {
23076 code = face->font->driver->encode_char (face->font, c);
23077
23078 if (code == FONT_INVALID_CODE)
23079 code = 0;
23080 }
23081 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23082
23083 /* Make sure X resources of the face are allocated. */
23084 #ifdef HAVE_X_WINDOWS
23085 if (display_p)
23086 #endif
23087 {
23088 eassert (face != NULL);
23089 PREPARE_FACE_FOR_DISPLAY (f, face);
23090 }
23091
23092 return face;
23093 }
23094
23095
23096 /* Get face and two-byte form of character glyph GLYPH on frame F.
23097 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23098 a pointer to a realized face that is ready for display. */
23099
23100 static struct face *
23101 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23102 XChar2b *char2b, int *two_byte_p)
23103 {
23104 struct face *face;
23105 unsigned code = 0;
23106
23107 eassert (glyph->type == CHAR_GLYPH);
23108 face = FACE_FROM_ID (f, glyph->face_id);
23109
23110 /* Make sure X resources of the face are allocated. */
23111 eassert (face != NULL);
23112 PREPARE_FACE_FOR_DISPLAY (f, face);
23113
23114 if (two_byte_p)
23115 *two_byte_p = 0;
23116
23117 if (face->font)
23118 {
23119 if (CHAR_BYTE8_P (glyph->u.ch))
23120 code = CHAR_TO_BYTE8 (glyph->u.ch);
23121 else
23122 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23123
23124 if (code == FONT_INVALID_CODE)
23125 code = 0;
23126 }
23127
23128 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23129 return face;
23130 }
23131
23132
23133 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23134 Return 1 if FONT has a glyph for C, otherwise return 0. */
23135
23136 static int
23137 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23138 {
23139 unsigned code;
23140
23141 if (CHAR_BYTE8_P (c))
23142 code = CHAR_TO_BYTE8 (c);
23143 else
23144 code = font->driver->encode_char (font, c);
23145
23146 if (code == FONT_INVALID_CODE)
23147 return 0;
23148 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23149 return 1;
23150 }
23151
23152
23153 /* Fill glyph string S with composition components specified by S->cmp.
23154
23155 BASE_FACE is the base face of the composition.
23156 S->cmp_from is the index of the first component for S.
23157
23158 OVERLAPS non-zero means S should draw the foreground only, and use
23159 its physical height for clipping. See also draw_glyphs.
23160
23161 Value is the index of a component not in S. */
23162
23163 static int
23164 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23165 int overlaps)
23166 {
23167 int i;
23168 /* For all glyphs of this composition, starting at the offset
23169 S->cmp_from, until we reach the end of the definition or encounter a
23170 glyph that requires the different face, add it to S. */
23171 struct face *face;
23172
23173 eassert (s);
23174
23175 s->for_overlaps = overlaps;
23176 s->face = NULL;
23177 s->font = NULL;
23178 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23179 {
23180 int c = COMPOSITION_GLYPH (s->cmp, i);
23181
23182 /* TAB in a composition means display glyphs with padding space
23183 on the left or right. */
23184 if (c != '\t')
23185 {
23186 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23187 -1, Qnil);
23188
23189 face = get_char_face_and_encoding (s->f, c, face_id,
23190 s->char2b + i, 1);
23191 if (face)
23192 {
23193 if (! s->face)
23194 {
23195 s->face = face;
23196 s->font = s->face->font;
23197 }
23198 else if (s->face != face)
23199 break;
23200 }
23201 }
23202 ++s->nchars;
23203 }
23204 s->cmp_to = i;
23205
23206 if (s->face == NULL)
23207 {
23208 s->face = base_face->ascii_face;
23209 s->font = s->face->font;
23210 }
23211
23212 /* All glyph strings for the same composition has the same width,
23213 i.e. the width set for the first component of the composition. */
23214 s->width = s->first_glyph->pixel_width;
23215
23216 /* If the specified font could not be loaded, use the frame's
23217 default font, but record the fact that we couldn't load it in
23218 the glyph string so that we can draw rectangles for the
23219 characters of the glyph string. */
23220 if (s->font == NULL)
23221 {
23222 s->font_not_found_p = 1;
23223 s->font = FRAME_FONT (s->f);
23224 }
23225
23226 /* Adjust base line for subscript/superscript text. */
23227 s->ybase += s->first_glyph->voffset;
23228
23229 /* This glyph string must always be drawn with 16-bit functions. */
23230 s->two_byte_p = 1;
23231
23232 return s->cmp_to;
23233 }
23234
23235 static int
23236 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23237 int start, int end, int overlaps)
23238 {
23239 struct glyph *glyph, *last;
23240 Lisp_Object lgstring;
23241 int i;
23242
23243 s->for_overlaps = overlaps;
23244 glyph = s->row->glyphs[s->area] + start;
23245 last = s->row->glyphs[s->area] + end;
23246 s->cmp_id = glyph->u.cmp.id;
23247 s->cmp_from = glyph->slice.cmp.from;
23248 s->cmp_to = glyph->slice.cmp.to + 1;
23249 s->face = FACE_FROM_ID (s->f, face_id);
23250 lgstring = composition_gstring_from_id (s->cmp_id);
23251 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23252 glyph++;
23253 while (glyph < last
23254 && glyph->u.cmp.automatic
23255 && glyph->u.cmp.id == s->cmp_id
23256 && s->cmp_to == glyph->slice.cmp.from)
23257 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23258
23259 for (i = s->cmp_from; i < s->cmp_to; i++)
23260 {
23261 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23262 unsigned code = LGLYPH_CODE (lglyph);
23263
23264 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23265 }
23266 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23267 return glyph - s->row->glyphs[s->area];
23268 }
23269
23270
23271 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23272 See the comment of fill_glyph_string for arguments.
23273 Value is the index of the first glyph not in S. */
23274
23275
23276 static int
23277 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23278 int start, int end, int overlaps)
23279 {
23280 struct glyph *glyph, *last;
23281 int voffset;
23282
23283 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23284 s->for_overlaps = overlaps;
23285 glyph = s->row->glyphs[s->area] + start;
23286 last = s->row->glyphs[s->area] + end;
23287 voffset = glyph->voffset;
23288 s->face = FACE_FROM_ID (s->f, face_id);
23289 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23290 s->nchars = 1;
23291 s->width = glyph->pixel_width;
23292 glyph++;
23293 while (glyph < last
23294 && glyph->type == GLYPHLESS_GLYPH
23295 && glyph->voffset == voffset
23296 && glyph->face_id == face_id)
23297 {
23298 s->nchars++;
23299 s->width += glyph->pixel_width;
23300 glyph++;
23301 }
23302 s->ybase += voffset;
23303 return glyph - s->row->glyphs[s->area];
23304 }
23305
23306
23307 /* Fill glyph string S from a sequence of character glyphs.
23308
23309 FACE_ID is the face id of the string. START is the index of the
23310 first glyph to consider, END is the index of the last + 1.
23311 OVERLAPS non-zero means S should draw the foreground only, and use
23312 its physical height for clipping. See also draw_glyphs.
23313
23314 Value is the index of the first glyph not in S. */
23315
23316 static int
23317 fill_glyph_string (struct glyph_string *s, int face_id,
23318 int start, int end, int overlaps)
23319 {
23320 struct glyph *glyph, *last;
23321 int voffset;
23322 int glyph_not_available_p;
23323
23324 eassert (s->f == XFRAME (s->w->frame));
23325 eassert (s->nchars == 0);
23326 eassert (start >= 0 && end > start);
23327
23328 s->for_overlaps = overlaps;
23329 glyph = s->row->glyphs[s->area] + start;
23330 last = s->row->glyphs[s->area] + end;
23331 voffset = glyph->voffset;
23332 s->padding_p = glyph->padding_p;
23333 glyph_not_available_p = glyph->glyph_not_available_p;
23334
23335 while (glyph < last
23336 && glyph->type == CHAR_GLYPH
23337 && glyph->voffset == voffset
23338 /* Same face id implies same font, nowadays. */
23339 && glyph->face_id == face_id
23340 && glyph->glyph_not_available_p == glyph_not_available_p)
23341 {
23342 int two_byte_p;
23343
23344 s->face = get_glyph_face_and_encoding (s->f, glyph,
23345 s->char2b + s->nchars,
23346 &two_byte_p);
23347 s->two_byte_p = two_byte_p;
23348 ++s->nchars;
23349 eassert (s->nchars <= end - start);
23350 s->width += glyph->pixel_width;
23351 if (glyph++->padding_p != s->padding_p)
23352 break;
23353 }
23354
23355 s->font = s->face->font;
23356
23357 /* If the specified font could not be loaded, use the frame's font,
23358 but record the fact that we couldn't load it in
23359 S->font_not_found_p so that we can draw rectangles for the
23360 characters of the glyph string. */
23361 if (s->font == NULL || glyph_not_available_p)
23362 {
23363 s->font_not_found_p = 1;
23364 s->font = FRAME_FONT (s->f);
23365 }
23366
23367 /* Adjust base line for subscript/superscript text. */
23368 s->ybase += voffset;
23369
23370 eassert (s->face && s->face->gc);
23371 return glyph - s->row->glyphs[s->area];
23372 }
23373
23374
23375 /* Fill glyph string S from image glyph S->first_glyph. */
23376
23377 static void
23378 fill_image_glyph_string (struct glyph_string *s)
23379 {
23380 eassert (s->first_glyph->type == IMAGE_GLYPH);
23381 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23382 eassert (s->img);
23383 s->slice = s->first_glyph->slice.img;
23384 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23385 s->font = s->face->font;
23386 s->width = s->first_glyph->pixel_width;
23387
23388 /* Adjust base line for subscript/superscript text. */
23389 s->ybase += s->first_glyph->voffset;
23390 }
23391
23392 #ifdef HAVE_XWIDGETS
23393 static void
23394 fill_xwidget_glyph_string (struct glyph_string *s)
23395 {
23396 eassert (s->first_glyph->type == XWIDGET_GLYPH);
23397 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
23398 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23399 s->font = s->face->font;
23400 s->width = s->first_glyph->pixel_width;
23401 s->ybase += s->first_glyph->voffset;
23402 s->xwidget = s->first_glyph->u.xwidget;
23403 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
23404 }
23405 #endif
23406 /* Fill glyph string S from a sequence of stretch glyphs.
23407
23408 START is the index of the first glyph to consider,
23409 END is the index of the last + 1.
23410
23411 Value is the index of the first glyph not in S. */
23412
23413 static int
23414 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23415 {
23416 struct glyph *glyph, *last;
23417 int voffset, face_id;
23418
23419 eassert (s->first_glyph->type == STRETCH_GLYPH);
23420
23421 glyph = s->row->glyphs[s->area] + start;
23422 last = s->row->glyphs[s->area] + end;
23423 face_id = glyph->face_id;
23424 s->face = FACE_FROM_ID (s->f, face_id);
23425 s->font = s->face->font;
23426 s->width = glyph->pixel_width;
23427 s->nchars = 1;
23428 voffset = glyph->voffset;
23429
23430 for (++glyph;
23431 (glyph < last
23432 && glyph->type == STRETCH_GLYPH
23433 && glyph->voffset == voffset
23434 && glyph->face_id == face_id);
23435 ++glyph)
23436 s->width += glyph->pixel_width;
23437
23438 /* Adjust base line for subscript/superscript text. */
23439 s->ybase += voffset;
23440
23441 /* The case that face->gc == 0 is handled when drawing the glyph
23442 string by calling PREPARE_FACE_FOR_DISPLAY. */
23443 eassert (s->face);
23444 return glyph - s->row->glyphs[s->area];
23445 }
23446
23447 static struct font_metrics *
23448 get_per_char_metric (struct font *font, XChar2b *char2b)
23449 {
23450 static struct font_metrics metrics;
23451 unsigned code;
23452
23453 if (! font)
23454 return NULL;
23455 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23456 if (code == FONT_INVALID_CODE)
23457 return NULL;
23458 font->driver->text_extents (font, &code, 1, &metrics);
23459 return &metrics;
23460 }
23461
23462 /* EXPORT for RIF:
23463 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23464 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23465 assumed to be zero. */
23466
23467 void
23468 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23469 {
23470 *left = *right = 0;
23471
23472 if (glyph->type == CHAR_GLYPH)
23473 {
23474 struct face *face;
23475 XChar2b char2b;
23476 struct font_metrics *pcm;
23477
23478 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23479 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23480 {
23481 if (pcm->rbearing > pcm->width)
23482 *right = pcm->rbearing - pcm->width;
23483 if (pcm->lbearing < 0)
23484 *left = -pcm->lbearing;
23485 }
23486 }
23487 else if (glyph->type == COMPOSITE_GLYPH)
23488 {
23489 if (! glyph->u.cmp.automatic)
23490 {
23491 struct composition *cmp = composition_table[glyph->u.cmp.id];
23492
23493 if (cmp->rbearing > cmp->pixel_width)
23494 *right = cmp->rbearing - cmp->pixel_width;
23495 if (cmp->lbearing < 0)
23496 *left = - cmp->lbearing;
23497 }
23498 else
23499 {
23500 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23501 struct font_metrics metrics;
23502
23503 composition_gstring_width (gstring, glyph->slice.cmp.from,
23504 glyph->slice.cmp.to + 1, &metrics);
23505 if (metrics.rbearing > metrics.width)
23506 *right = metrics.rbearing - metrics.width;
23507 if (metrics.lbearing < 0)
23508 *left = - metrics.lbearing;
23509 }
23510 }
23511 }
23512
23513
23514 /* Return the index of the first glyph preceding glyph string S that
23515 is overwritten by S because of S's left overhang. Value is -1
23516 if no glyphs are overwritten. */
23517
23518 static int
23519 left_overwritten (struct glyph_string *s)
23520 {
23521 int k;
23522
23523 if (s->left_overhang)
23524 {
23525 int x = 0, i;
23526 struct glyph *glyphs = s->row->glyphs[s->area];
23527 int first = s->first_glyph - glyphs;
23528
23529 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23530 x -= glyphs[i].pixel_width;
23531
23532 k = i + 1;
23533 }
23534 else
23535 k = -1;
23536
23537 return k;
23538 }
23539
23540
23541 /* Return the index of the first glyph preceding glyph string S that
23542 is overwriting S because of its right overhang. Value is -1 if no
23543 glyph in front of S overwrites S. */
23544
23545 static int
23546 left_overwriting (struct glyph_string *s)
23547 {
23548 int i, k, x;
23549 struct glyph *glyphs = s->row->glyphs[s->area];
23550 int first = s->first_glyph - glyphs;
23551
23552 k = -1;
23553 x = 0;
23554 for (i = first - 1; i >= 0; --i)
23555 {
23556 int left, right;
23557 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23558 if (x + right > 0)
23559 k = i;
23560 x -= glyphs[i].pixel_width;
23561 }
23562
23563 return k;
23564 }
23565
23566
23567 /* Return the index of the last glyph following glyph string S that is
23568 overwritten by S because of S's right overhang. Value is -1 if
23569 no such glyph is found. */
23570
23571 static int
23572 right_overwritten (struct glyph_string *s)
23573 {
23574 int k = -1;
23575
23576 if (s->right_overhang)
23577 {
23578 int x = 0, i;
23579 struct glyph *glyphs = s->row->glyphs[s->area];
23580 int first = (s->first_glyph - glyphs
23581 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23582 int end = s->row->used[s->area];
23583
23584 for (i = first; i < end && s->right_overhang > x; ++i)
23585 x += glyphs[i].pixel_width;
23586
23587 k = i;
23588 }
23589
23590 return k;
23591 }
23592
23593
23594 /* Return the index of the last glyph following glyph string S that
23595 overwrites S because of its left overhang. Value is negative
23596 if no such glyph is found. */
23597
23598 static int
23599 right_overwriting (struct glyph_string *s)
23600 {
23601 int i, k, x;
23602 int end = s->row->used[s->area];
23603 struct glyph *glyphs = s->row->glyphs[s->area];
23604 int first = (s->first_glyph - glyphs
23605 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23606
23607 k = -1;
23608 x = 0;
23609 for (i = first; i < end; ++i)
23610 {
23611 int left, right;
23612 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23613 if (x - left < 0)
23614 k = i;
23615 x += glyphs[i].pixel_width;
23616 }
23617
23618 return k;
23619 }
23620
23621
23622 /* Set background width of glyph string S. START is the index of the
23623 first glyph following S. LAST_X is the right-most x-position + 1
23624 in the drawing area. */
23625
23626 static void
23627 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23628 {
23629 /* If the face of this glyph string has to be drawn to the end of
23630 the drawing area, set S->extends_to_end_of_line_p. */
23631
23632 if (start == s->row->used[s->area]
23633 && s->area == TEXT_AREA
23634 && ((s->row->fill_line_p
23635 && (s->hl == DRAW_NORMAL_TEXT
23636 || s->hl == DRAW_IMAGE_RAISED
23637 || s->hl == DRAW_IMAGE_SUNKEN))
23638 || s->hl == DRAW_MOUSE_FACE))
23639 s->extends_to_end_of_line_p = 1;
23640
23641 /* If S extends its face to the end of the line, set its
23642 background_width to the distance to the right edge of the drawing
23643 area. */
23644 if (s->extends_to_end_of_line_p)
23645 s->background_width = last_x - s->x + 1;
23646 else
23647 s->background_width = s->width;
23648 }
23649
23650
23651 /* Compute overhangs and x-positions for glyph string S and its
23652 predecessors, or successors. X is the starting x-position for S.
23653 BACKWARD_P non-zero means process predecessors. */
23654
23655 static void
23656 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23657 {
23658 if (backward_p)
23659 {
23660 while (s)
23661 {
23662 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23663 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23664 x -= s->width;
23665 s->x = x;
23666 s = s->prev;
23667 }
23668 }
23669 else
23670 {
23671 while (s)
23672 {
23673 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23674 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23675 s->x = x;
23676 x += s->width;
23677 s = s->next;
23678 }
23679 }
23680 }
23681
23682
23683
23684 /* The following macros are only called from draw_glyphs below.
23685 They reference the following parameters of that function directly:
23686 `w', `row', `area', and `overlap_p'
23687 as well as the following local variables:
23688 `s', `f', and `hdc' (in W32) */
23689
23690 #ifdef HAVE_NTGUI
23691 /* On W32, silently add local `hdc' variable to argument list of
23692 init_glyph_string. */
23693 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23694 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23695 #else
23696 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23697 init_glyph_string (s, char2b, w, row, area, start, hl)
23698 #endif
23699
23700 /* Add a glyph string for a stretch glyph to the list of strings
23701 between HEAD and TAIL. START is the index of the stretch glyph in
23702 row area AREA of glyph row ROW. END is the index of the last glyph
23703 in that glyph row area. X is the current output position assigned
23704 to the new glyph string constructed. HL overrides that face of the
23705 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23706 is the right-most x-position of the drawing area. */
23707
23708 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23709 and below -- keep them on one line. */
23710 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23711 do \
23712 { \
23713 s = alloca (sizeof *s); \
23714 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23715 START = fill_stretch_glyph_string (s, START, END); \
23716 append_glyph_string (&HEAD, &TAIL, s); \
23717 s->x = (X); \
23718 } \
23719 while (0)
23720
23721
23722 /* Add a glyph string for an image glyph to the list of strings
23723 between HEAD and TAIL. START is the index of the image glyph in
23724 row area AREA of glyph row ROW. END is the index of the last glyph
23725 in that glyph row area. X is the current output position assigned
23726 to the new glyph string constructed. HL overrides that face of the
23727 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23728 is the right-most x-position of the drawing area. */
23729
23730 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23731 do \
23732 { \
23733 s = alloca (sizeof *s); \
23734 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23735 fill_image_glyph_string (s); \
23736 append_glyph_string (&HEAD, &TAIL, s); \
23737 ++START; \
23738 s->x = (X); \
23739 } \
23740 while (0)
23741
23742 #ifdef HAVE_XWIDGETS
23743 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23744 do \
23745 { \
23746 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
23747 s = (struct glyph_string *) alloca (sizeof *s); \
23748 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23749 fill_xwidget_glyph_string (s); \
23750 append_glyph_string (&HEAD, &TAIL, s); \
23751 ++START; \
23752 s->x = (X); \
23753 } \
23754 while (0)
23755 #endif
23756
23757
23758 /* Add a glyph string for a sequence of character glyphs to the list
23759 of strings between HEAD and TAIL. START is the index of the first
23760 glyph in row area AREA of glyph row ROW that is part of the new
23761 glyph string. END is the index of the last glyph in that glyph row
23762 area. X is the current output position assigned to the new glyph
23763 string constructed. HL overrides that face of the glyph; e.g. it
23764 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23765 right-most x-position of the drawing area. */
23766
23767 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23768 do \
23769 { \
23770 int face_id; \
23771 XChar2b *char2b; \
23772 \
23773 face_id = (row)->glyphs[area][START].face_id; \
23774 \
23775 s = alloca (sizeof *s); \
23776 char2b = alloca ((END - START) * sizeof *char2b); \
23777 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23778 append_glyph_string (&HEAD, &TAIL, s); \
23779 s->x = (X); \
23780 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23781 } \
23782 while (0)
23783
23784
23785 /* Add a glyph string for a composite sequence to the list of strings
23786 between HEAD and TAIL. START is the index of the first glyph in
23787 row area AREA of glyph row ROW that is part of the new glyph
23788 string. END is the index of the last glyph in that glyph row area.
23789 X is the current output position assigned to the new glyph string
23790 constructed. HL overrides that face of the glyph; e.g. it is
23791 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23792 x-position of the drawing area. */
23793
23794 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23795 do { \
23796 int face_id = (row)->glyphs[area][START].face_id; \
23797 struct face *base_face = FACE_FROM_ID (f, face_id); \
23798 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23799 struct composition *cmp = composition_table[cmp_id]; \
23800 XChar2b *char2b; \
23801 struct glyph_string *first_s = NULL; \
23802 int n; \
23803 \
23804 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23805 \
23806 /* Make glyph_strings for each glyph sequence that is drawable by \
23807 the same face, and append them to HEAD/TAIL. */ \
23808 for (n = 0; n < cmp->glyph_len;) \
23809 { \
23810 s = alloca (sizeof *s); \
23811 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23812 append_glyph_string (&(HEAD), &(TAIL), s); \
23813 s->cmp = cmp; \
23814 s->cmp_from = n; \
23815 s->x = (X); \
23816 if (n == 0) \
23817 first_s = s; \
23818 n = fill_composite_glyph_string (s, base_face, overlaps); \
23819 } \
23820 \
23821 ++START; \
23822 s = first_s; \
23823 } while (0)
23824
23825
23826 /* Add a glyph string for a glyph-string sequence to the list of strings
23827 between HEAD and TAIL. */
23828
23829 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23830 do { \
23831 int face_id; \
23832 XChar2b *char2b; \
23833 Lisp_Object gstring; \
23834 \
23835 face_id = (row)->glyphs[area][START].face_id; \
23836 gstring = (composition_gstring_from_id \
23837 ((row)->glyphs[area][START].u.cmp.id)); \
23838 s = alloca (sizeof *s); \
23839 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23840 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23841 append_glyph_string (&(HEAD), &(TAIL), s); \
23842 s->x = (X); \
23843 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23844 } while (0)
23845
23846
23847 /* Add a glyph string for a sequence of glyphless character's glyphs
23848 to the list of strings between HEAD and TAIL. The meanings of
23849 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23850
23851 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23852 do \
23853 { \
23854 int face_id; \
23855 \
23856 face_id = (row)->glyphs[area][START].face_id; \
23857 \
23858 s = alloca (sizeof *s); \
23859 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23860 append_glyph_string (&HEAD, &TAIL, s); \
23861 s->x = (X); \
23862 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23863 overlaps); \
23864 } \
23865 while (0)
23866
23867
23868 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23869 of AREA of glyph row ROW on window W between indices START and END.
23870 HL overrides the face for drawing glyph strings, e.g. it is
23871 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23872 x-positions of the drawing area.
23873
23874 This is an ugly monster macro construct because we must use alloca
23875 to allocate glyph strings (because draw_glyphs can be called
23876 asynchronously). */
23877
23878 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23879 do \
23880 { \
23881 HEAD = TAIL = NULL; \
23882 while (START < END) \
23883 { \
23884 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23885 switch (first_glyph->type) \
23886 { \
23887 case CHAR_GLYPH: \
23888 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23889 HL, X, LAST_X); \
23890 break; \
23891 \
23892 case COMPOSITE_GLYPH: \
23893 if (first_glyph->u.cmp.automatic) \
23894 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23895 HL, X, LAST_X); \
23896 else \
23897 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23898 HL, X, LAST_X); \
23899 break; \
23900 \
23901 case STRETCH_GLYPH: \
23902 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23903 HL, X, LAST_X); \
23904 break; \
23905 \
23906 case IMAGE_GLYPH: \
23907 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23908 HL, X, LAST_X); \
23909 break;
23910
23911 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23912 case XWIDGET_GLYPH: \
23913 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
23914 HL, X, LAST_X); \
23915 break;
23916
23917 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
23918 case GLYPHLESS_GLYPH: \
23919 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23920 HL, X, LAST_X); \
23921 break; \
23922 \
23923 default: \
23924 emacs_abort (); \
23925 } \
23926 \
23927 if (s) \
23928 { \
23929 set_glyph_string_background_width (s, START, LAST_X); \
23930 (X) += s->width; \
23931 } \
23932 } \
23933 } while (0)
23934
23935
23936 #ifdef HAVE_XWIDGETS
23937 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23938 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23939 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23940 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23941 #else
23942 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23943 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23944 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23945 #endif
23946
23947
23948 /* Draw glyphs between START and END in AREA of ROW on window W,
23949 starting at x-position X. X is relative to AREA in W. HL is a
23950 face-override with the following meaning:
23951
23952 DRAW_NORMAL_TEXT draw normally
23953 DRAW_CURSOR draw in cursor face
23954 DRAW_MOUSE_FACE draw in mouse face.
23955 DRAW_INVERSE_VIDEO draw in mode line face
23956 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23957 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23958
23959 If OVERLAPS is non-zero, draw only the foreground of characters and
23960 clip to the physical height of ROW. Non-zero value also defines
23961 the overlapping part to be drawn:
23962
23963 OVERLAPS_PRED overlap with preceding rows
23964 OVERLAPS_SUCC overlap with succeeding rows
23965 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23966 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23967
23968 Value is the x-position reached, relative to AREA of W. */
23969
23970 static int
23971 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23972 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23973 enum draw_glyphs_face hl, int overlaps)
23974 {
23975 struct glyph_string *head, *tail;
23976 struct glyph_string *s;
23977 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23978 int i, j, x_reached, last_x, area_left = 0;
23979 struct frame *f = XFRAME (WINDOW_FRAME (w));
23980 DECLARE_HDC (hdc);
23981
23982 ALLOCATE_HDC (hdc, f);
23983
23984 /* Let's rather be paranoid than getting a SEGV. */
23985 end = min (end, row->used[area]);
23986 start = clip_to_bounds (0, start, end);
23987
23988 /* Translate X to frame coordinates. Set last_x to the right
23989 end of the drawing area. */
23990 if (row->full_width_p)
23991 {
23992 /* X is relative to the left edge of W, without scroll bars
23993 or fringes. */
23994 area_left = WINDOW_LEFT_EDGE_X (w);
23995 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23996 }
23997 else
23998 {
23999 area_left = window_box_left (w, area);
24000 last_x = area_left + window_box_width (w, area);
24001 }
24002 x += area_left;
24003
24004 /* Build a doubly-linked list of glyph_string structures between
24005 head and tail from what we have to draw. Note that the macro
24006 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24007 the reason we use a separate variable `i'. */
24008 i = start;
24009 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24010 if (tail)
24011 x_reached = tail->x + tail->background_width;
24012 else
24013 x_reached = x;
24014
24015 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24016 the row, redraw some glyphs in front or following the glyph
24017 strings built above. */
24018 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24019 {
24020 struct glyph_string *h, *t;
24021 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24022 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24023 int check_mouse_face = 0;
24024 int dummy_x = 0;
24025
24026 /* If mouse highlighting is on, we may need to draw adjacent
24027 glyphs using mouse-face highlighting. */
24028 if (area == TEXT_AREA && row->mouse_face_p
24029 && hlinfo->mouse_face_beg_row >= 0
24030 && hlinfo->mouse_face_end_row >= 0)
24031 {
24032 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24033
24034 if (row_vpos >= hlinfo->mouse_face_beg_row
24035 && row_vpos <= hlinfo->mouse_face_end_row)
24036 {
24037 check_mouse_face = 1;
24038 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24039 ? hlinfo->mouse_face_beg_col : 0;
24040 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24041 ? hlinfo->mouse_face_end_col
24042 : row->used[TEXT_AREA];
24043 }
24044 }
24045
24046 /* Compute overhangs for all glyph strings. */
24047 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24048 for (s = head; s; s = s->next)
24049 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24050
24051 /* Prepend glyph strings for glyphs in front of the first glyph
24052 string that are overwritten because of the first glyph
24053 string's left overhang. The background of all strings
24054 prepended must be drawn because the first glyph string
24055 draws over it. */
24056 i = left_overwritten (head);
24057 if (i >= 0)
24058 {
24059 enum draw_glyphs_face overlap_hl;
24060
24061 /* If this row contains mouse highlighting, attempt to draw
24062 the overlapped glyphs with the correct highlight. This
24063 code fails if the overlap encompasses more than one glyph
24064 and mouse-highlight spans only some of these glyphs.
24065 However, making it work perfectly involves a lot more
24066 code, and I don't know if the pathological case occurs in
24067 practice, so we'll stick to this for now. --- cyd */
24068 if (check_mouse_face
24069 && mouse_beg_col < start && mouse_end_col > i)
24070 overlap_hl = DRAW_MOUSE_FACE;
24071 else
24072 overlap_hl = DRAW_NORMAL_TEXT;
24073
24074 j = i;
24075 BUILD_GLYPH_STRINGS (j, start, h, t,
24076 overlap_hl, dummy_x, last_x);
24077 start = i;
24078 compute_overhangs_and_x (t, head->x, 1);
24079 prepend_glyph_string_lists (&head, &tail, h, t);
24080 clip_head = head;
24081 }
24082
24083 /* Prepend glyph strings for glyphs in front of the first glyph
24084 string that overwrite that glyph string because of their
24085 right overhang. For these strings, only the foreground must
24086 be drawn, because it draws over the glyph string at `head'.
24087 The background must not be drawn because this would overwrite
24088 right overhangs of preceding glyphs for which no glyph
24089 strings exist. */
24090 i = left_overwriting (head);
24091 if (i >= 0)
24092 {
24093 enum draw_glyphs_face overlap_hl;
24094
24095 if (check_mouse_face
24096 && mouse_beg_col < start && mouse_end_col > i)
24097 overlap_hl = DRAW_MOUSE_FACE;
24098 else
24099 overlap_hl = DRAW_NORMAL_TEXT;
24100
24101 clip_head = head;
24102 BUILD_GLYPH_STRINGS (i, start, h, t,
24103 overlap_hl, dummy_x, last_x);
24104 for (s = h; s; s = s->next)
24105 s->background_filled_p = 1;
24106 compute_overhangs_and_x (t, head->x, 1);
24107 prepend_glyph_string_lists (&head, &tail, h, t);
24108 }
24109
24110 /* Append glyphs strings for glyphs following the last glyph
24111 string tail that are overwritten by tail. The background of
24112 these strings has to be drawn because tail's foreground draws
24113 over it. */
24114 i = right_overwritten (tail);
24115 if (i >= 0)
24116 {
24117 enum draw_glyphs_face overlap_hl;
24118
24119 if (check_mouse_face
24120 && mouse_beg_col < i && mouse_end_col > end)
24121 overlap_hl = DRAW_MOUSE_FACE;
24122 else
24123 overlap_hl = DRAW_NORMAL_TEXT;
24124
24125 BUILD_GLYPH_STRINGS (end, i, h, t,
24126 overlap_hl, x, last_x);
24127 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24128 we don't have `end = i;' here. */
24129 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24130 append_glyph_string_lists (&head, &tail, h, t);
24131 clip_tail = tail;
24132 }
24133
24134 /* Append glyph strings for glyphs following the last glyph
24135 string tail that overwrite tail. The foreground of such
24136 glyphs has to be drawn because it writes into the background
24137 of tail. The background must not be drawn because it could
24138 paint over the foreground of following glyphs. */
24139 i = right_overwriting (tail);
24140 if (i >= 0)
24141 {
24142 enum draw_glyphs_face overlap_hl;
24143 if (check_mouse_face
24144 && mouse_beg_col < i && mouse_end_col > end)
24145 overlap_hl = DRAW_MOUSE_FACE;
24146 else
24147 overlap_hl = DRAW_NORMAL_TEXT;
24148
24149 clip_tail = tail;
24150 i++; /* We must include the Ith glyph. */
24151 BUILD_GLYPH_STRINGS (end, i, h, t,
24152 overlap_hl, x, last_x);
24153 for (s = h; s; s = s->next)
24154 s->background_filled_p = 1;
24155 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24156 append_glyph_string_lists (&head, &tail, h, t);
24157 }
24158 if (clip_head || clip_tail)
24159 for (s = head; s; s = s->next)
24160 {
24161 s->clip_head = clip_head;
24162 s->clip_tail = clip_tail;
24163 }
24164 }
24165
24166 /* Draw all strings. */
24167 for (s = head; s; s = s->next)
24168 FRAME_RIF (f)->draw_glyph_string (s);
24169
24170 #ifndef HAVE_NS
24171 /* When focus a sole frame and move horizontally, this sets on_p to 0
24172 causing a failure to erase prev cursor position. */
24173 if (area == TEXT_AREA
24174 && !row->full_width_p
24175 /* When drawing overlapping rows, only the glyph strings'
24176 foreground is drawn, which doesn't erase a cursor
24177 completely. */
24178 && !overlaps)
24179 {
24180 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24181 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24182 : (tail ? tail->x + tail->background_width : x));
24183 x0 -= area_left;
24184 x1 -= area_left;
24185
24186 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24187 row->y, MATRIX_ROW_BOTTOM_Y (row));
24188 }
24189 #endif
24190
24191 /* Value is the x-position up to which drawn, relative to AREA of W.
24192 This doesn't include parts drawn because of overhangs. */
24193 if (row->full_width_p)
24194 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24195 else
24196 x_reached -= area_left;
24197
24198 RELEASE_HDC (hdc, f);
24199
24200 return x_reached;
24201 }
24202
24203 /* Expand row matrix if too narrow. Don't expand if area
24204 is not present. */
24205
24206 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24207 { \
24208 if (!fonts_changed_p \
24209 && (it->glyph_row->glyphs[area] \
24210 < it->glyph_row->glyphs[area + 1])) \
24211 { \
24212 it->w->ncols_scale_factor++; \
24213 fonts_changed_p = 1; \
24214 } \
24215 }
24216
24217 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24218 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24219
24220 static void
24221 append_glyph (struct it *it)
24222 {
24223 struct glyph *glyph;
24224 enum glyph_row_area area = it->area;
24225
24226 eassert (it->glyph_row);
24227 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24228
24229 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24230 if (glyph < it->glyph_row->glyphs[area + 1])
24231 {
24232 /* If the glyph row is reversed, we need to prepend the glyph
24233 rather than append it. */
24234 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24235 {
24236 struct glyph *g;
24237
24238 /* Make room for the additional glyph. */
24239 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24240 g[1] = *g;
24241 glyph = it->glyph_row->glyphs[area];
24242 }
24243 glyph->charpos = CHARPOS (it->position);
24244 glyph->object = it->object;
24245 if (it->pixel_width > 0)
24246 {
24247 glyph->pixel_width = it->pixel_width;
24248 glyph->padding_p = 0;
24249 }
24250 else
24251 {
24252 /* Assure at least 1-pixel width. Otherwise, cursor can't
24253 be displayed correctly. */
24254 glyph->pixel_width = 1;
24255 glyph->padding_p = 1;
24256 }
24257 glyph->ascent = it->ascent;
24258 glyph->descent = it->descent;
24259 glyph->voffset = it->voffset;
24260 glyph->type = CHAR_GLYPH;
24261 glyph->avoid_cursor_p = it->avoid_cursor_p;
24262 glyph->multibyte_p = it->multibyte_p;
24263 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24264 {
24265 /* In R2L rows, the left and the right box edges need to be
24266 drawn in reverse direction. */
24267 glyph->right_box_line_p = it->start_of_box_run_p;
24268 glyph->left_box_line_p = it->end_of_box_run_p;
24269 }
24270 else
24271 {
24272 glyph->left_box_line_p = it->start_of_box_run_p;
24273 glyph->right_box_line_p = it->end_of_box_run_p;
24274 }
24275 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24276 || it->phys_descent > it->descent);
24277 glyph->glyph_not_available_p = it->glyph_not_available_p;
24278 glyph->face_id = it->face_id;
24279 glyph->u.ch = it->char_to_display;
24280 glyph->slice.img = null_glyph_slice;
24281 glyph->font_type = FONT_TYPE_UNKNOWN;
24282 if (it->bidi_p)
24283 {
24284 glyph->resolved_level = it->bidi_it.resolved_level;
24285 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24286 emacs_abort ();
24287 glyph->bidi_type = it->bidi_it.type;
24288 }
24289 else
24290 {
24291 glyph->resolved_level = 0;
24292 glyph->bidi_type = UNKNOWN_BT;
24293 }
24294 ++it->glyph_row->used[area];
24295 }
24296 else
24297 IT_EXPAND_MATRIX_WIDTH (it, area);
24298 }
24299
24300 /* Store one glyph for the composition IT->cmp_it.id in
24301 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24302 non-null. */
24303
24304 static void
24305 append_composite_glyph (struct it *it)
24306 {
24307 struct glyph *glyph;
24308 enum glyph_row_area area = it->area;
24309
24310 eassert (it->glyph_row);
24311
24312 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24313 if (glyph < it->glyph_row->glyphs[area + 1])
24314 {
24315 /* If the glyph row is reversed, we need to prepend the glyph
24316 rather than append it. */
24317 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24318 {
24319 struct glyph *g;
24320
24321 /* Make room for the new glyph. */
24322 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24323 g[1] = *g;
24324 glyph = it->glyph_row->glyphs[it->area];
24325 }
24326 glyph->charpos = it->cmp_it.charpos;
24327 glyph->object = it->object;
24328 glyph->pixel_width = it->pixel_width;
24329 glyph->ascent = it->ascent;
24330 glyph->descent = it->descent;
24331 glyph->voffset = it->voffset;
24332 glyph->type = COMPOSITE_GLYPH;
24333 if (it->cmp_it.ch < 0)
24334 {
24335 glyph->u.cmp.automatic = 0;
24336 glyph->u.cmp.id = it->cmp_it.id;
24337 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24338 }
24339 else
24340 {
24341 glyph->u.cmp.automatic = 1;
24342 glyph->u.cmp.id = it->cmp_it.id;
24343 glyph->slice.cmp.from = it->cmp_it.from;
24344 glyph->slice.cmp.to = it->cmp_it.to - 1;
24345 }
24346 glyph->avoid_cursor_p = it->avoid_cursor_p;
24347 glyph->multibyte_p = it->multibyte_p;
24348 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24349 {
24350 /* In R2L rows, the left and the right box edges need to be
24351 drawn in reverse direction. */
24352 glyph->right_box_line_p = it->start_of_box_run_p;
24353 glyph->left_box_line_p = it->end_of_box_run_p;
24354 }
24355 else
24356 {
24357 glyph->left_box_line_p = it->start_of_box_run_p;
24358 glyph->right_box_line_p = it->end_of_box_run_p;
24359 }
24360 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24361 || it->phys_descent > it->descent);
24362 glyph->padding_p = 0;
24363 glyph->glyph_not_available_p = 0;
24364 glyph->face_id = it->face_id;
24365 glyph->font_type = FONT_TYPE_UNKNOWN;
24366 if (it->bidi_p)
24367 {
24368 glyph->resolved_level = it->bidi_it.resolved_level;
24369 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24370 emacs_abort ();
24371 glyph->bidi_type = it->bidi_it.type;
24372 }
24373 ++it->glyph_row->used[area];
24374 }
24375 else
24376 IT_EXPAND_MATRIX_WIDTH (it, area);
24377 }
24378
24379
24380 /* Change IT->ascent and IT->height according to the setting of
24381 IT->voffset. */
24382
24383 static void
24384 take_vertical_position_into_account (struct it *it)
24385 {
24386 if (it->voffset)
24387 {
24388 if (it->voffset < 0)
24389 /* Increase the ascent so that we can display the text higher
24390 in the line. */
24391 it->ascent -= it->voffset;
24392 else
24393 /* Increase the descent so that we can display the text lower
24394 in the line. */
24395 it->descent += it->voffset;
24396 }
24397 }
24398
24399
24400 /* Produce glyphs/get display metrics for the image IT is loaded with.
24401 See the description of struct display_iterator in dispextern.h for
24402 an overview of struct display_iterator. */
24403
24404 static void
24405 produce_image_glyph (struct it *it)
24406 {
24407 struct image *img;
24408 struct face *face;
24409 int glyph_ascent, crop;
24410 struct glyph_slice slice;
24411
24412 eassert (it->what == IT_IMAGE);
24413
24414 face = FACE_FROM_ID (it->f, it->face_id);
24415 eassert (face);
24416 /* Make sure X resources of the face is loaded. */
24417 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24418
24419 if (it->image_id < 0)
24420 {
24421 /* Fringe bitmap. */
24422 it->ascent = it->phys_ascent = 0;
24423 it->descent = it->phys_descent = 0;
24424 it->pixel_width = 0;
24425 it->nglyphs = 0;
24426 return;
24427 }
24428
24429 img = IMAGE_FROM_ID (it->f, it->image_id);
24430 eassert (img);
24431 /* Make sure X resources of the image is loaded. */
24432 prepare_image_for_display (it->f, img);
24433
24434 slice.x = slice.y = 0;
24435 slice.width = img->width;
24436 slice.height = img->height;
24437
24438 if (INTEGERP (it->slice.x))
24439 slice.x = XINT (it->slice.x);
24440 else if (FLOATP (it->slice.x))
24441 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24442
24443 if (INTEGERP (it->slice.y))
24444 slice.y = XINT (it->slice.y);
24445 else if (FLOATP (it->slice.y))
24446 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24447
24448 if (INTEGERP (it->slice.width))
24449 slice.width = XINT (it->slice.width);
24450 else if (FLOATP (it->slice.width))
24451 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24452
24453 if (INTEGERP (it->slice.height))
24454 slice.height = XINT (it->slice.height);
24455 else if (FLOATP (it->slice.height))
24456 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24457
24458 if (slice.x >= img->width)
24459 slice.x = img->width;
24460 if (slice.y >= img->height)
24461 slice.y = img->height;
24462 if (slice.x + slice.width >= img->width)
24463 slice.width = img->width - slice.x;
24464 if (slice.y + slice.height > img->height)
24465 slice.height = img->height - slice.y;
24466
24467 if (slice.width == 0 || slice.height == 0)
24468 return;
24469
24470 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24471
24472 it->descent = slice.height - glyph_ascent;
24473 if (slice.y == 0)
24474 it->descent += img->vmargin;
24475 if (slice.y + slice.height == img->height)
24476 it->descent += img->vmargin;
24477 it->phys_descent = it->descent;
24478
24479 it->pixel_width = slice.width;
24480 if (slice.x == 0)
24481 it->pixel_width += img->hmargin;
24482 if (slice.x + slice.width == img->width)
24483 it->pixel_width += img->hmargin;
24484
24485 /* It's quite possible for images to have an ascent greater than
24486 their height, so don't get confused in that case. */
24487 if (it->descent < 0)
24488 it->descent = 0;
24489
24490 it->nglyphs = 1;
24491
24492 if (face->box != FACE_NO_BOX)
24493 {
24494 if (face->box_line_width > 0)
24495 {
24496 if (slice.y == 0)
24497 it->ascent += face->box_line_width;
24498 if (slice.y + slice.height == img->height)
24499 it->descent += face->box_line_width;
24500 }
24501
24502 if (it->start_of_box_run_p && slice.x == 0)
24503 it->pixel_width += eabs (face->box_line_width);
24504 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24505 it->pixel_width += eabs (face->box_line_width);
24506 }
24507
24508 take_vertical_position_into_account (it);
24509
24510 /* Automatically crop wide image glyphs at right edge so we can
24511 draw the cursor on same display row. */
24512 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24513 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24514 {
24515 it->pixel_width -= crop;
24516 slice.width -= crop;
24517 }
24518
24519 if (it->glyph_row)
24520 {
24521 struct glyph *glyph;
24522 enum glyph_row_area area = it->area;
24523
24524 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24525 if (glyph < it->glyph_row->glyphs[area + 1])
24526 {
24527 glyph->charpos = CHARPOS (it->position);
24528 glyph->object = it->object;
24529 glyph->pixel_width = it->pixel_width;
24530 glyph->ascent = glyph_ascent;
24531 glyph->descent = it->descent;
24532 glyph->voffset = it->voffset;
24533 glyph->type = IMAGE_GLYPH;
24534 glyph->avoid_cursor_p = it->avoid_cursor_p;
24535 glyph->multibyte_p = it->multibyte_p;
24536 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24537 {
24538 /* In R2L rows, the left and the right box edges need to be
24539 drawn in reverse direction. */
24540 glyph->right_box_line_p = it->start_of_box_run_p;
24541 glyph->left_box_line_p = it->end_of_box_run_p;
24542 }
24543 else
24544 {
24545 glyph->left_box_line_p = it->start_of_box_run_p;
24546 glyph->right_box_line_p = it->end_of_box_run_p;
24547 }
24548 glyph->overlaps_vertically_p = 0;
24549 glyph->padding_p = 0;
24550 glyph->glyph_not_available_p = 0;
24551 glyph->face_id = it->face_id;
24552 glyph->u.img_id = img->id;
24553 glyph->slice.img = slice;
24554 glyph->font_type = FONT_TYPE_UNKNOWN;
24555 if (it->bidi_p)
24556 {
24557 glyph->resolved_level = it->bidi_it.resolved_level;
24558 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24559 emacs_abort ();
24560 glyph->bidi_type = it->bidi_it.type;
24561 }
24562 ++it->glyph_row->used[area];
24563 }
24564 else
24565 IT_EXPAND_MATRIX_WIDTH (it, area);
24566 }
24567 }
24568
24569 #ifdef HAVE_XWIDGETS
24570 static void
24571 produce_xwidget_glyph (struct it *it)
24572 {
24573 struct xwidget* xw;
24574 struct face *face;
24575 int glyph_ascent, crop;
24576 printf("produce_xwidget_glyph:\n");
24577 eassert (it->what == IT_XWIDGET);
24578
24579 face = FACE_FROM_ID (it->f, it->face_id);
24580 eassert (face);
24581 /* Make sure X resources of the face is loaded. */
24582 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24583
24584 xw = it->xwidget;
24585 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
24586 it->descent = xw->height/2;
24587 it->phys_descent = it->descent;
24588 it->pixel_width = xw->width;
24589 /* It's quite possible for images to have an ascent greater than
24590 their height, so don't get confused in that case. */
24591 if (it->descent < 0)
24592 it->descent = 0;
24593
24594 it->nglyphs = 1;
24595
24596 if (face->box != FACE_NO_BOX)
24597 {
24598 if (face->box_line_width > 0)
24599 {
24600 it->ascent += face->box_line_width;
24601 it->descent += face->box_line_width;
24602 }
24603
24604 if (it->start_of_box_run_p)
24605 it->pixel_width += eabs (face->box_line_width);
24606 it->pixel_width += eabs (face->box_line_width);
24607 }
24608
24609 take_vertical_position_into_account (it);
24610
24611 /* Automatically crop wide image glyphs at right edge so we can
24612 draw the cursor on same display row. */
24613 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24614 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24615 {
24616 it->pixel_width -= crop;
24617 }
24618
24619 if (it->glyph_row)
24620 {
24621 struct glyph *glyph;
24622 enum glyph_row_area area = it->area;
24623
24624 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24625 if (glyph < it->glyph_row->glyphs[area + 1])
24626 {
24627 glyph->charpos = CHARPOS (it->position);
24628 glyph->object = it->object;
24629 glyph->pixel_width = it->pixel_width;
24630 glyph->ascent = glyph_ascent;
24631 glyph->descent = it->descent;
24632 glyph->voffset = it->voffset;
24633 glyph->type = XWIDGET_GLYPH;
24634
24635 glyph->multibyte_p = it->multibyte_p;
24636 glyph->left_box_line_p = it->start_of_box_run_p;
24637 glyph->right_box_line_p = it->end_of_box_run_p;
24638 glyph->overlaps_vertically_p = 0;
24639 glyph->padding_p = 0;
24640 glyph->glyph_not_available_p = 0;
24641 glyph->face_id = it->face_id;
24642 glyph->u.xwidget = it->xwidget;
24643 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
24644 glyph->font_type = FONT_TYPE_UNKNOWN;
24645 ++it->glyph_row->used[area];
24646 }
24647 else
24648 IT_EXPAND_MATRIX_WIDTH (it, area);
24649 }
24650 }
24651 #endif
24652
24653 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24654 of the glyph, WIDTH and HEIGHT are the width and height of the
24655 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24656
24657 static void
24658 append_stretch_glyph (struct it *it, Lisp_Object object,
24659 int width, int height, int ascent)
24660 {
24661 struct glyph *glyph;
24662 enum glyph_row_area area = it->area;
24663
24664 eassert (ascent >= 0 && ascent <= height);
24665
24666 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24667 if (glyph < it->glyph_row->glyphs[area + 1])
24668 {
24669 /* If the glyph row is reversed, we need to prepend the glyph
24670 rather than append it. */
24671 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24672 {
24673 struct glyph *g;
24674
24675 /* Make room for the additional glyph. */
24676 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24677 g[1] = *g;
24678 glyph = it->glyph_row->glyphs[area];
24679 }
24680 glyph->charpos = CHARPOS (it->position);
24681 glyph->object = object;
24682 glyph->pixel_width = width;
24683 glyph->ascent = ascent;
24684 glyph->descent = height - ascent;
24685 glyph->voffset = it->voffset;
24686 glyph->type = STRETCH_GLYPH;
24687 glyph->avoid_cursor_p = it->avoid_cursor_p;
24688 glyph->multibyte_p = it->multibyte_p;
24689 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24690 {
24691 /* In R2L rows, the left and the right box edges need to be
24692 drawn in reverse direction. */
24693 glyph->right_box_line_p = it->start_of_box_run_p;
24694 glyph->left_box_line_p = it->end_of_box_run_p;
24695 }
24696 else
24697 {
24698 glyph->left_box_line_p = it->start_of_box_run_p;
24699 glyph->right_box_line_p = it->end_of_box_run_p;
24700 }
24701 glyph->overlaps_vertically_p = 0;
24702 glyph->padding_p = 0;
24703 glyph->glyph_not_available_p = 0;
24704 glyph->face_id = it->face_id;
24705 glyph->u.stretch.ascent = ascent;
24706 glyph->u.stretch.height = height;
24707 glyph->slice.img = null_glyph_slice;
24708 glyph->font_type = FONT_TYPE_UNKNOWN;
24709 if (it->bidi_p)
24710 {
24711 glyph->resolved_level = it->bidi_it.resolved_level;
24712 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24713 emacs_abort ();
24714 glyph->bidi_type = it->bidi_it.type;
24715 }
24716 else
24717 {
24718 glyph->resolved_level = 0;
24719 glyph->bidi_type = UNKNOWN_BT;
24720 }
24721 ++it->glyph_row->used[area];
24722 }
24723 else
24724 IT_EXPAND_MATRIX_WIDTH (it, area);
24725 }
24726
24727 #endif /* HAVE_WINDOW_SYSTEM */
24728
24729 /* Produce a stretch glyph for iterator IT. IT->object is the value
24730 of the glyph property displayed. The value must be a list
24731 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24732 being recognized:
24733
24734 1. `:width WIDTH' specifies that the space should be WIDTH *
24735 canonical char width wide. WIDTH may be an integer or floating
24736 point number.
24737
24738 2. `:relative-width FACTOR' specifies that the width of the stretch
24739 should be computed from the width of the first character having the
24740 `glyph' property, and should be FACTOR times that width.
24741
24742 3. `:align-to HPOS' specifies that the space should be wide enough
24743 to reach HPOS, a value in canonical character units.
24744
24745 Exactly one of the above pairs must be present.
24746
24747 4. `:height HEIGHT' specifies that the height of the stretch produced
24748 should be HEIGHT, measured in canonical character units.
24749
24750 5. `:relative-height FACTOR' specifies that the height of the
24751 stretch should be FACTOR times the height of the characters having
24752 the glyph property.
24753
24754 Either none or exactly one of 4 or 5 must be present.
24755
24756 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24757 of the stretch should be used for the ascent of the stretch.
24758 ASCENT must be in the range 0 <= ASCENT <= 100. */
24759
24760 void
24761 produce_stretch_glyph (struct it *it)
24762 {
24763 /* (space :width WIDTH :height HEIGHT ...) */
24764 Lisp_Object prop, plist;
24765 int width = 0, height = 0, align_to = -1;
24766 int zero_width_ok_p = 0;
24767 double tem;
24768 struct font *font = NULL;
24769
24770 #ifdef HAVE_WINDOW_SYSTEM
24771 int ascent = 0;
24772 int zero_height_ok_p = 0;
24773
24774 if (FRAME_WINDOW_P (it->f))
24775 {
24776 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24777 font = face->font ? face->font : FRAME_FONT (it->f);
24778 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24779 }
24780 #endif
24781
24782 /* List should start with `space'. */
24783 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24784 plist = XCDR (it->object);
24785
24786 /* Compute the width of the stretch. */
24787 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24788 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24789 {
24790 /* Absolute width `:width WIDTH' specified and valid. */
24791 zero_width_ok_p = 1;
24792 width = (int)tem;
24793 }
24794 #ifdef HAVE_WINDOW_SYSTEM
24795 else if (FRAME_WINDOW_P (it->f)
24796 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24797 {
24798 /* Relative width `:relative-width FACTOR' specified and valid.
24799 Compute the width of the characters having the `glyph'
24800 property. */
24801 struct it it2;
24802 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24803
24804 it2 = *it;
24805 if (it->multibyte_p)
24806 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24807 else
24808 {
24809 it2.c = it2.char_to_display = *p, it2.len = 1;
24810 if (! ASCII_CHAR_P (it2.c))
24811 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24812 }
24813
24814 it2.glyph_row = NULL;
24815 it2.what = IT_CHARACTER;
24816 x_produce_glyphs (&it2);
24817 width = NUMVAL (prop) * it2.pixel_width;
24818 }
24819 #endif /* HAVE_WINDOW_SYSTEM */
24820 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24821 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24822 {
24823 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24824 align_to = (align_to < 0
24825 ? 0
24826 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24827 else if (align_to < 0)
24828 align_to = window_box_left_offset (it->w, TEXT_AREA);
24829 width = max (0, (int)tem + align_to - it->current_x);
24830 zero_width_ok_p = 1;
24831 }
24832 else
24833 /* Nothing specified -> width defaults to canonical char width. */
24834 width = FRAME_COLUMN_WIDTH (it->f);
24835
24836 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24837 width = 1;
24838
24839 #ifdef HAVE_WINDOW_SYSTEM
24840 /* Compute height. */
24841 if (FRAME_WINDOW_P (it->f))
24842 {
24843 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24844 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24845 {
24846 height = (int)tem;
24847 zero_height_ok_p = 1;
24848 }
24849 else if (prop = Fplist_get (plist, QCrelative_height),
24850 NUMVAL (prop) > 0)
24851 height = FONT_HEIGHT (font) * NUMVAL (prop);
24852 else
24853 height = FONT_HEIGHT (font);
24854
24855 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24856 height = 1;
24857
24858 /* Compute percentage of height used for ascent. If
24859 `:ascent ASCENT' is present and valid, use that. Otherwise,
24860 derive the ascent from the font in use. */
24861 if (prop = Fplist_get (plist, QCascent),
24862 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24863 ascent = height * NUMVAL (prop) / 100.0;
24864 else if (!NILP (prop)
24865 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24866 ascent = min (max (0, (int)tem), height);
24867 else
24868 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24869 }
24870 else
24871 #endif /* HAVE_WINDOW_SYSTEM */
24872 height = 1;
24873
24874 if (width > 0 && it->line_wrap != TRUNCATE
24875 && it->current_x + width > it->last_visible_x)
24876 {
24877 width = it->last_visible_x - it->current_x;
24878 #ifdef HAVE_WINDOW_SYSTEM
24879 /* Subtract one more pixel from the stretch width, but only on
24880 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24881 width -= FRAME_WINDOW_P (it->f);
24882 #endif
24883 }
24884
24885 if (width > 0 && height > 0 && it->glyph_row)
24886 {
24887 Lisp_Object o_object = it->object;
24888 Lisp_Object object = it->stack[it->sp - 1].string;
24889 int n = width;
24890
24891 if (!STRINGP (object))
24892 object = it->w->contents;
24893 #ifdef HAVE_WINDOW_SYSTEM
24894 if (FRAME_WINDOW_P (it->f))
24895 append_stretch_glyph (it, object, width, height, ascent);
24896 else
24897 #endif
24898 {
24899 it->object = object;
24900 it->char_to_display = ' ';
24901 it->pixel_width = it->len = 1;
24902 while (n--)
24903 tty_append_glyph (it);
24904 it->object = o_object;
24905 }
24906 }
24907
24908 it->pixel_width = width;
24909 #ifdef HAVE_WINDOW_SYSTEM
24910 if (FRAME_WINDOW_P (it->f))
24911 {
24912 it->ascent = it->phys_ascent = ascent;
24913 it->descent = it->phys_descent = height - it->ascent;
24914 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24915 take_vertical_position_into_account (it);
24916 }
24917 else
24918 #endif
24919 it->nglyphs = width;
24920 }
24921
24922 /* Get information about special display element WHAT in an
24923 environment described by IT. WHAT is one of IT_TRUNCATION or
24924 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24925 non-null glyph_row member. This function ensures that fields like
24926 face_id, c, len of IT are left untouched. */
24927
24928 static void
24929 produce_special_glyphs (struct it *it, enum display_element_type what)
24930 {
24931 struct it temp_it;
24932 Lisp_Object gc;
24933 GLYPH glyph;
24934
24935 temp_it = *it;
24936 temp_it.object = make_number (0);
24937 memset (&temp_it.current, 0, sizeof temp_it.current);
24938
24939 if (what == IT_CONTINUATION)
24940 {
24941 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24942 if (it->bidi_it.paragraph_dir == R2L)
24943 SET_GLYPH_FROM_CHAR (glyph, '/');
24944 else
24945 SET_GLYPH_FROM_CHAR (glyph, '\\');
24946 if (it->dp
24947 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24948 {
24949 /* FIXME: Should we mirror GC for R2L lines? */
24950 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24951 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24952 }
24953 }
24954 else if (what == IT_TRUNCATION)
24955 {
24956 /* Truncation glyph. */
24957 SET_GLYPH_FROM_CHAR (glyph, '$');
24958 if (it->dp
24959 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24960 {
24961 /* FIXME: Should we mirror GC for R2L lines? */
24962 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24963 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24964 }
24965 }
24966 else
24967 emacs_abort ();
24968
24969 #ifdef HAVE_WINDOW_SYSTEM
24970 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24971 is turned off, we precede the truncation/continuation glyphs by a
24972 stretch glyph whose width is computed such that these special
24973 glyphs are aligned at the window margin, even when very different
24974 fonts are used in different glyph rows. */
24975 if (FRAME_WINDOW_P (temp_it.f)
24976 /* init_iterator calls this with it->glyph_row == NULL, and it
24977 wants only the pixel width of the truncation/continuation
24978 glyphs. */
24979 && temp_it.glyph_row
24980 /* insert_left_trunc_glyphs calls us at the beginning of the
24981 row, and it has its own calculation of the stretch glyph
24982 width. */
24983 && temp_it.glyph_row->used[TEXT_AREA] > 0
24984 && (temp_it.glyph_row->reversed_p
24985 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24986 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24987 {
24988 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24989
24990 if (stretch_width > 0)
24991 {
24992 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24993 struct font *font =
24994 face->font ? face->font : FRAME_FONT (temp_it.f);
24995 int stretch_ascent =
24996 (((temp_it.ascent + temp_it.descent)
24997 * FONT_BASE (font)) / FONT_HEIGHT (font));
24998
24999 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25000 temp_it.ascent + temp_it.descent,
25001 stretch_ascent);
25002 }
25003 }
25004 #endif
25005
25006 temp_it.dp = NULL;
25007 temp_it.what = IT_CHARACTER;
25008 temp_it.len = 1;
25009 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25010 temp_it.face_id = GLYPH_FACE (glyph);
25011 temp_it.len = CHAR_BYTES (temp_it.c);
25012
25013 PRODUCE_GLYPHS (&temp_it);
25014 it->pixel_width = temp_it.pixel_width;
25015 it->nglyphs = temp_it.pixel_width;
25016 }
25017
25018 #ifdef HAVE_WINDOW_SYSTEM
25019
25020 /* Calculate line-height and line-spacing properties.
25021 An integer value specifies explicit pixel value.
25022 A float value specifies relative value to current face height.
25023 A cons (float . face-name) specifies relative value to
25024 height of specified face font.
25025
25026 Returns height in pixels, or nil. */
25027
25028
25029 static Lisp_Object
25030 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25031 int boff, int override)
25032 {
25033 Lisp_Object face_name = Qnil;
25034 int ascent, descent, height;
25035
25036 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25037 return val;
25038
25039 if (CONSP (val))
25040 {
25041 face_name = XCAR (val);
25042 val = XCDR (val);
25043 if (!NUMBERP (val))
25044 val = make_number (1);
25045 if (NILP (face_name))
25046 {
25047 height = it->ascent + it->descent;
25048 goto scale;
25049 }
25050 }
25051
25052 if (NILP (face_name))
25053 {
25054 font = FRAME_FONT (it->f);
25055 boff = FRAME_BASELINE_OFFSET (it->f);
25056 }
25057 else if (EQ (face_name, Qt))
25058 {
25059 override = 0;
25060 }
25061 else
25062 {
25063 int face_id;
25064 struct face *face;
25065
25066 face_id = lookup_named_face (it->f, face_name, 0);
25067 if (face_id < 0)
25068 return make_number (-1);
25069
25070 face = FACE_FROM_ID (it->f, face_id);
25071 font = face->font;
25072 if (font == NULL)
25073 return make_number (-1);
25074 boff = font->baseline_offset;
25075 if (font->vertical_centering)
25076 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25077 }
25078
25079 ascent = FONT_BASE (font) + boff;
25080 descent = FONT_DESCENT (font) - boff;
25081
25082 if (override)
25083 {
25084 it->override_ascent = ascent;
25085 it->override_descent = descent;
25086 it->override_boff = boff;
25087 }
25088
25089 height = ascent + descent;
25090
25091 scale:
25092 if (FLOATP (val))
25093 height = (int)(XFLOAT_DATA (val) * height);
25094 else if (INTEGERP (val))
25095 height *= XINT (val);
25096
25097 return make_number (height);
25098 }
25099
25100
25101 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25102 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25103 and only if this is for a character for which no font was found.
25104
25105 If the display method (it->glyphless_method) is
25106 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25107 length of the acronym or the hexadecimal string, UPPER_XOFF and
25108 UPPER_YOFF are pixel offsets for the upper part of the string,
25109 LOWER_XOFF and LOWER_YOFF are for the lower part.
25110
25111 For the other display methods, LEN through LOWER_YOFF are zero. */
25112
25113 static void
25114 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25115 short upper_xoff, short upper_yoff,
25116 short lower_xoff, short lower_yoff)
25117 {
25118 struct glyph *glyph;
25119 enum glyph_row_area area = it->area;
25120
25121 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25122 if (glyph < it->glyph_row->glyphs[area + 1])
25123 {
25124 /* If the glyph row is reversed, we need to prepend the glyph
25125 rather than append it. */
25126 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25127 {
25128 struct glyph *g;
25129
25130 /* Make room for the additional glyph. */
25131 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25132 g[1] = *g;
25133 glyph = it->glyph_row->glyphs[area];
25134 }
25135 glyph->charpos = CHARPOS (it->position);
25136 glyph->object = it->object;
25137 glyph->pixel_width = it->pixel_width;
25138 glyph->ascent = it->ascent;
25139 glyph->descent = it->descent;
25140 glyph->voffset = it->voffset;
25141 glyph->type = GLYPHLESS_GLYPH;
25142 glyph->u.glyphless.method = it->glyphless_method;
25143 glyph->u.glyphless.for_no_font = for_no_font;
25144 glyph->u.glyphless.len = len;
25145 glyph->u.glyphless.ch = it->c;
25146 glyph->slice.glyphless.upper_xoff = upper_xoff;
25147 glyph->slice.glyphless.upper_yoff = upper_yoff;
25148 glyph->slice.glyphless.lower_xoff = lower_xoff;
25149 glyph->slice.glyphless.lower_yoff = lower_yoff;
25150 glyph->avoid_cursor_p = it->avoid_cursor_p;
25151 glyph->multibyte_p = it->multibyte_p;
25152 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25153 {
25154 /* In R2L rows, the left and the right box edges need to be
25155 drawn in reverse direction. */
25156 glyph->right_box_line_p = it->start_of_box_run_p;
25157 glyph->left_box_line_p = it->end_of_box_run_p;
25158 }
25159 else
25160 {
25161 glyph->left_box_line_p = it->start_of_box_run_p;
25162 glyph->right_box_line_p = it->end_of_box_run_p;
25163 }
25164 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25165 || it->phys_descent > it->descent);
25166 glyph->padding_p = 0;
25167 glyph->glyph_not_available_p = 0;
25168 glyph->face_id = face_id;
25169 glyph->font_type = FONT_TYPE_UNKNOWN;
25170 if (it->bidi_p)
25171 {
25172 glyph->resolved_level = it->bidi_it.resolved_level;
25173 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25174 emacs_abort ();
25175 glyph->bidi_type = it->bidi_it.type;
25176 }
25177 ++it->glyph_row->used[area];
25178 }
25179 else
25180 IT_EXPAND_MATRIX_WIDTH (it, area);
25181 }
25182
25183
25184 /* Produce a glyph for a glyphless character for iterator IT.
25185 IT->glyphless_method specifies which method to use for displaying
25186 the character. See the description of enum
25187 glyphless_display_method in dispextern.h for the detail.
25188
25189 FOR_NO_FONT is nonzero if and only if this is for a character for
25190 which no font was found. ACRONYM, if non-nil, is an acronym string
25191 for the character. */
25192
25193 static void
25194 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25195 {
25196 int face_id;
25197 struct face *face;
25198 struct font *font;
25199 int base_width, base_height, width, height;
25200 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25201 int len;
25202
25203 /* Get the metrics of the base font. We always refer to the current
25204 ASCII face. */
25205 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25206 font = face->font ? face->font : FRAME_FONT (it->f);
25207 it->ascent = FONT_BASE (font) + font->baseline_offset;
25208 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25209 base_height = it->ascent + it->descent;
25210 base_width = font->average_width;
25211
25212 /* Get a face ID for the glyph by utilizing a cache (the same way as
25213 done for `escape-glyph' in get_next_display_element). */
25214 if (it->f == last_glyphless_glyph_frame
25215 && it->face_id == last_glyphless_glyph_face_id)
25216 {
25217 face_id = last_glyphless_glyph_merged_face_id;
25218 }
25219 else
25220 {
25221 /* Merge the `glyphless-char' face into the current face. */
25222 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
25223 last_glyphless_glyph_frame = it->f;
25224 last_glyphless_glyph_face_id = it->face_id;
25225 last_glyphless_glyph_merged_face_id = face_id;
25226 }
25227
25228 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25229 {
25230 it->pixel_width = THIN_SPACE_WIDTH;
25231 len = 0;
25232 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25233 }
25234 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25235 {
25236 width = CHAR_WIDTH (it->c);
25237 if (width == 0)
25238 width = 1;
25239 else if (width > 4)
25240 width = 4;
25241 it->pixel_width = base_width * width;
25242 len = 0;
25243 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25244 }
25245 else
25246 {
25247 char buf[7];
25248 const char *str;
25249 unsigned int code[6];
25250 int upper_len;
25251 int ascent, descent;
25252 struct font_metrics metrics_upper, metrics_lower;
25253
25254 face = FACE_FROM_ID (it->f, face_id);
25255 font = face->font ? face->font : FRAME_FONT (it->f);
25256 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25257
25258 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25259 {
25260 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25261 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25262 if (CONSP (acronym))
25263 acronym = XCAR (acronym);
25264 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25265 }
25266 else
25267 {
25268 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25269 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25270 str = buf;
25271 }
25272 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25273 code[len] = font->driver->encode_char (font, str[len]);
25274 upper_len = (len + 1) / 2;
25275 font->driver->text_extents (font, code, upper_len,
25276 &metrics_upper);
25277 font->driver->text_extents (font, code + upper_len, len - upper_len,
25278 &metrics_lower);
25279
25280
25281
25282 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25283 width = max (metrics_upper.width, metrics_lower.width) + 4;
25284 upper_xoff = upper_yoff = 2; /* the typical case */
25285 if (base_width >= width)
25286 {
25287 /* Align the upper to the left, the lower to the right. */
25288 it->pixel_width = base_width;
25289 lower_xoff = base_width - 2 - metrics_lower.width;
25290 }
25291 else
25292 {
25293 /* Center the shorter one. */
25294 it->pixel_width = width;
25295 if (metrics_upper.width >= metrics_lower.width)
25296 lower_xoff = (width - metrics_lower.width) / 2;
25297 else
25298 {
25299 /* FIXME: This code doesn't look right. It formerly was
25300 missing the "lower_xoff = 0;", which couldn't have
25301 been right since it left lower_xoff uninitialized. */
25302 lower_xoff = 0;
25303 upper_xoff = (width - metrics_upper.width) / 2;
25304 }
25305 }
25306
25307 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25308 top, bottom, and between upper and lower strings. */
25309 height = (metrics_upper.ascent + metrics_upper.descent
25310 + metrics_lower.ascent + metrics_lower.descent) + 5;
25311 /* Center vertically.
25312 H:base_height, D:base_descent
25313 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25314
25315 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25316 descent = D - H/2 + h/2;
25317 lower_yoff = descent - 2 - ld;
25318 upper_yoff = lower_yoff - la - 1 - ud; */
25319 ascent = - (it->descent - (base_height + height + 1) / 2);
25320 descent = it->descent - (base_height - height) / 2;
25321 lower_yoff = descent - 2 - metrics_lower.descent;
25322 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25323 - metrics_upper.descent);
25324 /* Don't make the height shorter than the base height. */
25325 if (height > base_height)
25326 {
25327 it->ascent = ascent;
25328 it->descent = descent;
25329 }
25330 }
25331
25332 it->phys_ascent = it->ascent;
25333 it->phys_descent = it->descent;
25334 if (it->glyph_row)
25335 append_glyphless_glyph (it, face_id, for_no_font, len,
25336 upper_xoff, upper_yoff,
25337 lower_xoff, lower_yoff);
25338 it->nglyphs = 1;
25339 take_vertical_position_into_account (it);
25340 }
25341
25342
25343 /* RIF:
25344 Produce glyphs/get display metrics for the display element IT is
25345 loaded with. See the description of struct it in dispextern.h
25346 for an overview of struct it. */
25347
25348 void
25349 x_produce_glyphs (struct it *it)
25350 {
25351 int extra_line_spacing = it->extra_line_spacing;
25352
25353 it->glyph_not_available_p = 0;
25354
25355 if (it->what == IT_CHARACTER)
25356 {
25357 XChar2b char2b;
25358 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25359 struct font *font = face->font;
25360 struct font_metrics *pcm = NULL;
25361 int boff; /* baseline offset */
25362
25363 if (font == NULL)
25364 {
25365 /* When no suitable font is found, display this character by
25366 the method specified in the first extra slot of
25367 Vglyphless_char_display. */
25368 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25369
25370 eassert (it->what == IT_GLYPHLESS);
25371 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25372 goto done;
25373 }
25374
25375 boff = font->baseline_offset;
25376 if (font->vertical_centering)
25377 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25378
25379 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25380 {
25381 int stretched_p;
25382
25383 it->nglyphs = 1;
25384
25385 if (it->override_ascent >= 0)
25386 {
25387 it->ascent = it->override_ascent;
25388 it->descent = it->override_descent;
25389 boff = it->override_boff;
25390 }
25391 else
25392 {
25393 it->ascent = FONT_BASE (font) + boff;
25394 it->descent = FONT_DESCENT (font) - boff;
25395 }
25396
25397 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25398 {
25399 pcm = get_per_char_metric (font, &char2b);
25400 if (pcm->width == 0
25401 && pcm->rbearing == 0 && pcm->lbearing == 0)
25402 pcm = NULL;
25403 }
25404
25405 if (pcm)
25406 {
25407 it->phys_ascent = pcm->ascent + boff;
25408 it->phys_descent = pcm->descent - boff;
25409 it->pixel_width = pcm->width;
25410 }
25411 else
25412 {
25413 it->glyph_not_available_p = 1;
25414 it->phys_ascent = it->ascent;
25415 it->phys_descent = it->descent;
25416 it->pixel_width = font->space_width;
25417 }
25418
25419 if (it->constrain_row_ascent_descent_p)
25420 {
25421 if (it->descent > it->max_descent)
25422 {
25423 it->ascent += it->descent - it->max_descent;
25424 it->descent = it->max_descent;
25425 }
25426 if (it->ascent > it->max_ascent)
25427 {
25428 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25429 it->ascent = it->max_ascent;
25430 }
25431 it->phys_ascent = min (it->phys_ascent, it->ascent);
25432 it->phys_descent = min (it->phys_descent, it->descent);
25433 extra_line_spacing = 0;
25434 }
25435
25436 /* If this is a space inside a region of text with
25437 `space-width' property, change its width. */
25438 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25439 if (stretched_p)
25440 it->pixel_width *= XFLOATINT (it->space_width);
25441
25442 /* If face has a box, add the box thickness to the character
25443 height. If character has a box line to the left and/or
25444 right, add the box line width to the character's width. */
25445 if (face->box != FACE_NO_BOX)
25446 {
25447 int thick = face->box_line_width;
25448
25449 if (thick > 0)
25450 {
25451 it->ascent += thick;
25452 it->descent += thick;
25453 }
25454 else
25455 thick = -thick;
25456
25457 if (it->start_of_box_run_p)
25458 it->pixel_width += thick;
25459 if (it->end_of_box_run_p)
25460 it->pixel_width += thick;
25461 }
25462
25463 /* If face has an overline, add the height of the overline
25464 (1 pixel) and a 1 pixel margin to the character height. */
25465 if (face->overline_p)
25466 it->ascent += overline_margin;
25467
25468 if (it->constrain_row_ascent_descent_p)
25469 {
25470 if (it->ascent > it->max_ascent)
25471 it->ascent = it->max_ascent;
25472 if (it->descent > it->max_descent)
25473 it->descent = it->max_descent;
25474 }
25475
25476 take_vertical_position_into_account (it);
25477
25478 /* If we have to actually produce glyphs, do it. */
25479 if (it->glyph_row)
25480 {
25481 if (stretched_p)
25482 {
25483 /* Translate a space with a `space-width' property
25484 into a stretch glyph. */
25485 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25486 / FONT_HEIGHT (font));
25487 append_stretch_glyph (it, it->object, it->pixel_width,
25488 it->ascent + it->descent, ascent);
25489 }
25490 else
25491 append_glyph (it);
25492
25493 /* If characters with lbearing or rbearing are displayed
25494 in this line, record that fact in a flag of the
25495 glyph row. This is used to optimize X output code. */
25496 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25497 it->glyph_row->contains_overlapping_glyphs_p = 1;
25498 }
25499 if (! stretched_p && it->pixel_width == 0)
25500 /* We assure that all visible glyphs have at least 1-pixel
25501 width. */
25502 it->pixel_width = 1;
25503 }
25504 else if (it->char_to_display == '\n')
25505 {
25506 /* A newline has no width, but we need the height of the
25507 line. But if previous part of the line sets a height,
25508 don't increase that height */
25509
25510 Lisp_Object height;
25511 Lisp_Object total_height = Qnil;
25512
25513 it->override_ascent = -1;
25514 it->pixel_width = 0;
25515 it->nglyphs = 0;
25516
25517 height = get_it_property (it, Qline_height);
25518 /* Split (line-height total-height) list */
25519 if (CONSP (height)
25520 && CONSP (XCDR (height))
25521 && NILP (XCDR (XCDR (height))))
25522 {
25523 total_height = XCAR (XCDR (height));
25524 height = XCAR (height);
25525 }
25526 height = calc_line_height_property (it, height, font, boff, 1);
25527
25528 if (it->override_ascent >= 0)
25529 {
25530 it->ascent = it->override_ascent;
25531 it->descent = it->override_descent;
25532 boff = it->override_boff;
25533 }
25534 else
25535 {
25536 it->ascent = FONT_BASE (font) + boff;
25537 it->descent = FONT_DESCENT (font) - boff;
25538 }
25539
25540 if (EQ (height, Qt))
25541 {
25542 if (it->descent > it->max_descent)
25543 {
25544 it->ascent += it->descent - it->max_descent;
25545 it->descent = it->max_descent;
25546 }
25547 if (it->ascent > it->max_ascent)
25548 {
25549 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25550 it->ascent = it->max_ascent;
25551 }
25552 it->phys_ascent = min (it->phys_ascent, it->ascent);
25553 it->phys_descent = min (it->phys_descent, it->descent);
25554 it->constrain_row_ascent_descent_p = 1;
25555 extra_line_spacing = 0;
25556 }
25557 else
25558 {
25559 Lisp_Object spacing;
25560
25561 it->phys_ascent = it->ascent;
25562 it->phys_descent = it->descent;
25563
25564 if ((it->max_ascent > 0 || it->max_descent > 0)
25565 && face->box != FACE_NO_BOX
25566 && face->box_line_width > 0)
25567 {
25568 it->ascent += face->box_line_width;
25569 it->descent += face->box_line_width;
25570 }
25571 if (!NILP (height)
25572 && XINT (height) > it->ascent + it->descent)
25573 it->ascent = XINT (height) - it->descent;
25574
25575 if (!NILP (total_height))
25576 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25577 else
25578 {
25579 spacing = get_it_property (it, Qline_spacing);
25580 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25581 }
25582 if (INTEGERP (spacing))
25583 {
25584 extra_line_spacing = XINT (spacing);
25585 if (!NILP (total_height))
25586 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25587 }
25588 }
25589 }
25590 else /* i.e. (it->char_to_display == '\t') */
25591 {
25592 if (font->space_width > 0)
25593 {
25594 int tab_width = it->tab_width * font->space_width;
25595 int x = it->current_x + it->continuation_lines_width;
25596 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25597
25598 /* If the distance from the current position to the next tab
25599 stop is less than a space character width, use the
25600 tab stop after that. */
25601 if (next_tab_x - x < font->space_width)
25602 next_tab_x += tab_width;
25603
25604 it->pixel_width = next_tab_x - x;
25605 it->nglyphs = 1;
25606 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25607 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25608
25609 if (it->glyph_row)
25610 {
25611 append_stretch_glyph (it, it->object, it->pixel_width,
25612 it->ascent + it->descent, it->ascent);
25613 }
25614 }
25615 else
25616 {
25617 it->pixel_width = 0;
25618 it->nglyphs = 1;
25619 }
25620 }
25621 }
25622 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25623 {
25624 /* A static composition.
25625
25626 Note: A composition is represented as one glyph in the
25627 glyph matrix. There are no padding glyphs.
25628
25629 Important note: pixel_width, ascent, and descent are the
25630 values of what is drawn by draw_glyphs (i.e. the values of
25631 the overall glyphs composed). */
25632 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25633 int boff; /* baseline offset */
25634 struct composition *cmp = composition_table[it->cmp_it.id];
25635 int glyph_len = cmp->glyph_len;
25636 struct font *font = face->font;
25637
25638 it->nglyphs = 1;
25639
25640 /* If we have not yet calculated pixel size data of glyphs of
25641 the composition for the current face font, calculate them
25642 now. Theoretically, we have to check all fonts for the
25643 glyphs, but that requires much time and memory space. So,
25644 here we check only the font of the first glyph. This may
25645 lead to incorrect display, but it's very rare, and C-l
25646 (recenter-top-bottom) can correct the display anyway. */
25647 if (! cmp->font || cmp->font != font)
25648 {
25649 /* Ascent and descent of the font of the first character
25650 of this composition (adjusted by baseline offset).
25651 Ascent and descent of overall glyphs should not be less
25652 than these, respectively. */
25653 int font_ascent, font_descent, font_height;
25654 /* Bounding box of the overall glyphs. */
25655 int leftmost, rightmost, lowest, highest;
25656 int lbearing, rbearing;
25657 int i, width, ascent, descent;
25658 int left_padded = 0, right_padded = 0;
25659 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25660 XChar2b char2b;
25661 struct font_metrics *pcm;
25662 int font_not_found_p;
25663 ptrdiff_t pos;
25664
25665 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25666 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25667 break;
25668 if (glyph_len < cmp->glyph_len)
25669 right_padded = 1;
25670 for (i = 0; i < glyph_len; i++)
25671 {
25672 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25673 break;
25674 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25675 }
25676 if (i > 0)
25677 left_padded = 1;
25678
25679 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25680 : IT_CHARPOS (*it));
25681 /* If no suitable font is found, use the default font. */
25682 font_not_found_p = font == NULL;
25683 if (font_not_found_p)
25684 {
25685 face = face->ascii_face;
25686 font = face->font;
25687 }
25688 boff = font->baseline_offset;
25689 if (font->vertical_centering)
25690 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25691 font_ascent = FONT_BASE (font) + boff;
25692 font_descent = FONT_DESCENT (font) - boff;
25693 font_height = FONT_HEIGHT (font);
25694
25695 cmp->font = font;
25696
25697 pcm = NULL;
25698 if (! font_not_found_p)
25699 {
25700 get_char_face_and_encoding (it->f, c, it->face_id,
25701 &char2b, 0);
25702 pcm = get_per_char_metric (font, &char2b);
25703 }
25704
25705 /* Initialize the bounding box. */
25706 if (pcm)
25707 {
25708 width = cmp->glyph_len > 0 ? pcm->width : 0;
25709 ascent = pcm->ascent;
25710 descent = pcm->descent;
25711 lbearing = pcm->lbearing;
25712 rbearing = pcm->rbearing;
25713 }
25714 else
25715 {
25716 width = cmp->glyph_len > 0 ? font->space_width : 0;
25717 ascent = FONT_BASE (font);
25718 descent = FONT_DESCENT (font);
25719 lbearing = 0;
25720 rbearing = width;
25721 }
25722
25723 rightmost = width;
25724 leftmost = 0;
25725 lowest = - descent + boff;
25726 highest = ascent + boff;
25727
25728 if (! font_not_found_p
25729 && font->default_ascent
25730 && CHAR_TABLE_P (Vuse_default_ascent)
25731 && !NILP (Faref (Vuse_default_ascent,
25732 make_number (it->char_to_display))))
25733 highest = font->default_ascent + boff;
25734
25735 /* Draw the first glyph at the normal position. It may be
25736 shifted to right later if some other glyphs are drawn
25737 at the left. */
25738 cmp->offsets[i * 2] = 0;
25739 cmp->offsets[i * 2 + 1] = boff;
25740 cmp->lbearing = lbearing;
25741 cmp->rbearing = rbearing;
25742
25743 /* Set cmp->offsets for the remaining glyphs. */
25744 for (i++; i < glyph_len; i++)
25745 {
25746 int left, right, btm, top;
25747 int ch = COMPOSITION_GLYPH (cmp, i);
25748 int face_id;
25749 struct face *this_face;
25750
25751 if (ch == '\t')
25752 ch = ' ';
25753 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25754 this_face = FACE_FROM_ID (it->f, face_id);
25755 font = this_face->font;
25756
25757 if (font == NULL)
25758 pcm = NULL;
25759 else
25760 {
25761 get_char_face_and_encoding (it->f, ch, face_id,
25762 &char2b, 0);
25763 pcm = get_per_char_metric (font, &char2b);
25764 }
25765 if (! pcm)
25766 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25767 else
25768 {
25769 width = pcm->width;
25770 ascent = pcm->ascent;
25771 descent = pcm->descent;
25772 lbearing = pcm->lbearing;
25773 rbearing = pcm->rbearing;
25774 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25775 {
25776 /* Relative composition with or without
25777 alternate chars. */
25778 left = (leftmost + rightmost - width) / 2;
25779 btm = - descent + boff;
25780 if (font->relative_compose
25781 && (! CHAR_TABLE_P (Vignore_relative_composition)
25782 || NILP (Faref (Vignore_relative_composition,
25783 make_number (ch)))))
25784 {
25785
25786 if (- descent >= font->relative_compose)
25787 /* One extra pixel between two glyphs. */
25788 btm = highest + 1;
25789 else if (ascent <= 0)
25790 /* One extra pixel between two glyphs. */
25791 btm = lowest - 1 - ascent - descent;
25792 }
25793 }
25794 else
25795 {
25796 /* A composition rule is specified by an integer
25797 value that encodes global and new reference
25798 points (GREF and NREF). GREF and NREF are
25799 specified by numbers as below:
25800
25801 0---1---2 -- ascent
25802 | |
25803 | |
25804 | |
25805 9--10--11 -- center
25806 | |
25807 ---3---4---5--- baseline
25808 | |
25809 6---7---8 -- descent
25810 */
25811 int rule = COMPOSITION_RULE (cmp, i);
25812 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25813
25814 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25815 grefx = gref % 3, nrefx = nref % 3;
25816 grefy = gref / 3, nrefy = nref / 3;
25817 if (xoff)
25818 xoff = font_height * (xoff - 128) / 256;
25819 if (yoff)
25820 yoff = font_height * (yoff - 128) / 256;
25821
25822 left = (leftmost
25823 + grefx * (rightmost - leftmost) / 2
25824 - nrefx * width / 2
25825 + xoff);
25826
25827 btm = ((grefy == 0 ? highest
25828 : grefy == 1 ? 0
25829 : grefy == 2 ? lowest
25830 : (highest + lowest) / 2)
25831 - (nrefy == 0 ? ascent + descent
25832 : nrefy == 1 ? descent - boff
25833 : nrefy == 2 ? 0
25834 : (ascent + descent) / 2)
25835 + yoff);
25836 }
25837
25838 cmp->offsets[i * 2] = left;
25839 cmp->offsets[i * 2 + 1] = btm + descent;
25840
25841 /* Update the bounding box of the overall glyphs. */
25842 if (width > 0)
25843 {
25844 right = left + width;
25845 if (left < leftmost)
25846 leftmost = left;
25847 if (right > rightmost)
25848 rightmost = right;
25849 }
25850 top = btm + descent + ascent;
25851 if (top > highest)
25852 highest = top;
25853 if (btm < lowest)
25854 lowest = btm;
25855
25856 if (cmp->lbearing > left + lbearing)
25857 cmp->lbearing = left + lbearing;
25858 if (cmp->rbearing < left + rbearing)
25859 cmp->rbearing = left + rbearing;
25860 }
25861 }
25862
25863 /* If there are glyphs whose x-offsets are negative,
25864 shift all glyphs to the right and make all x-offsets
25865 non-negative. */
25866 if (leftmost < 0)
25867 {
25868 for (i = 0; i < cmp->glyph_len; i++)
25869 cmp->offsets[i * 2] -= leftmost;
25870 rightmost -= leftmost;
25871 cmp->lbearing -= leftmost;
25872 cmp->rbearing -= leftmost;
25873 }
25874
25875 if (left_padded && cmp->lbearing < 0)
25876 {
25877 for (i = 0; i < cmp->glyph_len; i++)
25878 cmp->offsets[i * 2] -= cmp->lbearing;
25879 rightmost -= cmp->lbearing;
25880 cmp->rbearing -= cmp->lbearing;
25881 cmp->lbearing = 0;
25882 }
25883 if (right_padded && rightmost < cmp->rbearing)
25884 {
25885 rightmost = cmp->rbearing;
25886 }
25887
25888 cmp->pixel_width = rightmost;
25889 cmp->ascent = highest;
25890 cmp->descent = - lowest;
25891 if (cmp->ascent < font_ascent)
25892 cmp->ascent = font_ascent;
25893 if (cmp->descent < font_descent)
25894 cmp->descent = font_descent;
25895 }
25896
25897 if (it->glyph_row
25898 && (cmp->lbearing < 0
25899 || cmp->rbearing > cmp->pixel_width))
25900 it->glyph_row->contains_overlapping_glyphs_p = 1;
25901
25902 it->pixel_width = cmp->pixel_width;
25903 it->ascent = it->phys_ascent = cmp->ascent;
25904 it->descent = it->phys_descent = cmp->descent;
25905 if (face->box != FACE_NO_BOX)
25906 {
25907 int thick = face->box_line_width;
25908
25909 if (thick > 0)
25910 {
25911 it->ascent += thick;
25912 it->descent += thick;
25913 }
25914 else
25915 thick = - thick;
25916
25917 if (it->start_of_box_run_p)
25918 it->pixel_width += thick;
25919 if (it->end_of_box_run_p)
25920 it->pixel_width += thick;
25921 }
25922
25923 /* If face has an overline, add the height of the overline
25924 (1 pixel) and a 1 pixel margin to the character height. */
25925 if (face->overline_p)
25926 it->ascent += overline_margin;
25927
25928 take_vertical_position_into_account (it);
25929 if (it->ascent < 0)
25930 it->ascent = 0;
25931 if (it->descent < 0)
25932 it->descent = 0;
25933
25934 if (it->glyph_row && cmp->glyph_len > 0)
25935 append_composite_glyph (it);
25936 }
25937 else if (it->what == IT_COMPOSITION)
25938 {
25939 /* A dynamic (automatic) composition. */
25940 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25941 Lisp_Object gstring;
25942 struct font_metrics metrics;
25943
25944 it->nglyphs = 1;
25945
25946 gstring = composition_gstring_from_id (it->cmp_it.id);
25947 it->pixel_width
25948 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25949 &metrics);
25950 if (it->glyph_row
25951 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25952 it->glyph_row->contains_overlapping_glyphs_p = 1;
25953 it->ascent = it->phys_ascent = metrics.ascent;
25954 it->descent = it->phys_descent = metrics.descent;
25955 if (face->box != FACE_NO_BOX)
25956 {
25957 int thick = face->box_line_width;
25958
25959 if (thick > 0)
25960 {
25961 it->ascent += thick;
25962 it->descent += thick;
25963 }
25964 else
25965 thick = - thick;
25966
25967 if (it->start_of_box_run_p)
25968 it->pixel_width += thick;
25969 if (it->end_of_box_run_p)
25970 it->pixel_width += thick;
25971 }
25972 /* If face has an overline, add the height of the overline
25973 (1 pixel) and a 1 pixel margin to the character height. */
25974 if (face->overline_p)
25975 it->ascent += overline_margin;
25976 take_vertical_position_into_account (it);
25977 if (it->ascent < 0)
25978 it->ascent = 0;
25979 if (it->descent < 0)
25980 it->descent = 0;
25981
25982 if (it->glyph_row)
25983 append_composite_glyph (it);
25984 }
25985 else if (it->what == IT_GLYPHLESS)
25986 produce_glyphless_glyph (it, 0, Qnil);
25987 else if (it->what == IT_IMAGE)
25988 produce_image_glyph (it);
25989 else if (it->what == IT_STRETCH)
25990 produce_stretch_glyph (it);
25991 #ifdef HAVE_XWIDGETS
25992 else if (it->what == IT_XWIDGET)
25993 produce_xwidget_glyph (it);
25994 #endif
25995 done:
25996 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25997 because this isn't true for images with `:ascent 100'. */
25998 eassert (it->ascent >= 0 && it->descent >= 0);
25999 if (it->area == TEXT_AREA)
26000 it->current_x += it->pixel_width;
26001
26002 if (extra_line_spacing > 0)
26003 {
26004 it->descent += extra_line_spacing;
26005 if (extra_line_spacing > it->max_extra_line_spacing)
26006 it->max_extra_line_spacing = extra_line_spacing;
26007 }
26008
26009 it->max_ascent = max (it->max_ascent, it->ascent);
26010 it->max_descent = max (it->max_descent, it->descent);
26011 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26012 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26013 }
26014
26015 /* EXPORT for RIF:
26016 Output LEN glyphs starting at START at the nominal cursor position.
26017 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26018 being updated, and UPDATED_AREA is the area of that row being updated. */
26019
26020 void
26021 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26022 struct glyph *start, enum glyph_row_area updated_area, int len)
26023 {
26024 int x, hpos, chpos = w->phys_cursor.hpos;
26025
26026 eassert (updated_row);
26027 /* When the window is hscrolled, cursor hpos can legitimately be out
26028 of bounds, but we draw the cursor at the corresponding window
26029 margin in that case. */
26030 if (!updated_row->reversed_p && chpos < 0)
26031 chpos = 0;
26032 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26033 chpos = updated_row->used[TEXT_AREA] - 1;
26034
26035 block_input ();
26036
26037 /* Write glyphs. */
26038
26039 hpos = start - updated_row->glyphs[updated_area];
26040 x = draw_glyphs (w, output_cursor.x,
26041 updated_row, updated_area,
26042 hpos, hpos + len,
26043 DRAW_NORMAL_TEXT, 0);
26044
26045 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26046 if (updated_area == TEXT_AREA
26047 && w->phys_cursor_on_p
26048 && w->phys_cursor.vpos == output_cursor.vpos
26049 && chpos >= hpos
26050 && chpos < hpos + len)
26051 w->phys_cursor_on_p = 0;
26052
26053 unblock_input ();
26054
26055 /* Advance the output cursor. */
26056 output_cursor.hpos += len;
26057 output_cursor.x = x;
26058 }
26059
26060
26061 /* EXPORT for RIF:
26062 Insert LEN glyphs from START at the nominal cursor position. */
26063
26064 void
26065 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26066 struct glyph *start, enum glyph_row_area updated_area, int len)
26067 {
26068 struct frame *f;
26069 int line_height, shift_by_width, shifted_region_width;
26070 struct glyph_row *row;
26071 struct glyph *glyph;
26072 int frame_x, frame_y;
26073 ptrdiff_t hpos;
26074
26075 eassert (updated_row);
26076 block_input ();
26077 f = XFRAME (WINDOW_FRAME (w));
26078
26079 /* Get the height of the line we are in. */
26080 row = updated_row;
26081 line_height = row->height;
26082
26083 /* Get the width of the glyphs to insert. */
26084 shift_by_width = 0;
26085 for (glyph = start; glyph < start + len; ++glyph)
26086 shift_by_width += glyph->pixel_width;
26087
26088 /* Get the width of the region to shift right. */
26089 shifted_region_width = (window_box_width (w, updated_area)
26090 - output_cursor.x
26091 - shift_by_width);
26092
26093 /* Shift right. */
26094 frame_x = window_box_left (w, updated_area) + output_cursor.x;
26095 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
26096
26097 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26098 line_height, shift_by_width);
26099
26100 /* Write the glyphs. */
26101 hpos = start - row->glyphs[updated_area];
26102 draw_glyphs (w, output_cursor.x, row, updated_area,
26103 hpos, hpos + len,
26104 DRAW_NORMAL_TEXT, 0);
26105
26106 /* Advance the output cursor. */
26107 output_cursor.hpos += len;
26108 output_cursor.x += shift_by_width;
26109 unblock_input ();
26110 }
26111
26112
26113 /* EXPORT for RIF:
26114 Erase the current text line from the nominal cursor position
26115 (inclusive) to pixel column TO_X (exclusive). The idea is that
26116 everything from TO_X onward is already erased.
26117
26118 TO_X is a pixel position relative to UPDATED_AREA of currently
26119 updated window W. TO_X == -1 means clear to the end of this area. */
26120
26121 void
26122 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26123 enum glyph_row_area updated_area, int to_x)
26124 {
26125 struct frame *f;
26126 int max_x, min_y, max_y;
26127 int from_x, from_y, to_y;
26128
26129 eassert (updated_row);
26130 f = XFRAME (w->frame);
26131
26132 if (updated_row->full_width_p)
26133 max_x = WINDOW_TOTAL_WIDTH (w);
26134 else
26135 max_x = window_box_width (w, updated_area);
26136 max_y = window_text_bottom_y (w);
26137
26138 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26139 of window. For TO_X > 0, truncate to end of drawing area. */
26140 if (to_x == 0)
26141 return;
26142 else if (to_x < 0)
26143 to_x = max_x;
26144 else
26145 to_x = min (to_x, max_x);
26146
26147 to_y = min (max_y, output_cursor.y + updated_row->height);
26148
26149 /* Notice if the cursor will be cleared by this operation. */
26150 if (!updated_row->full_width_p)
26151 notice_overwritten_cursor (w, updated_area,
26152 output_cursor.x, -1,
26153 updated_row->y,
26154 MATRIX_ROW_BOTTOM_Y (updated_row));
26155
26156 from_x = output_cursor.x;
26157
26158 /* Translate to frame coordinates. */
26159 if (updated_row->full_width_p)
26160 {
26161 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26162 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26163 }
26164 else
26165 {
26166 int area_left = window_box_left (w, updated_area);
26167 from_x += area_left;
26168 to_x += area_left;
26169 }
26170
26171 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26172 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
26173 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26174
26175 /* Prevent inadvertently clearing to end of the X window. */
26176 if (to_x > from_x && to_y > from_y)
26177 {
26178 block_input ();
26179 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26180 to_x - from_x, to_y - from_y);
26181 unblock_input ();
26182 }
26183 }
26184
26185 #endif /* HAVE_WINDOW_SYSTEM */
26186
26187
26188 \f
26189 /***********************************************************************
26190 Cursor types
26191 ***********************************************************************/
26192
26193 /* Value is the internal representation of the specified cursor type
26194 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26195 of the bar cursor. */
26196
26197 static enum text_cursor_kinds
26198 get_specified_cursor_type (Lisp_Object arg, int *width)
26199 {
26200 enum text_cursor_kinds type;
26201
26202 if (NILP (arg))
26203 return NO_CURSOR;
26204
26205 if (EQ (arg, Qbox))
26206 return FILLED_BOX_CURSOR;
26207
26208 if (EQ (arg, Qhollow))
26209 return HOLLOW_BOX_CURSOR;
26210
26211 if (EQ (arg, Qbar))
26212 {
26213 *width = 2;
26214 return BAR_CURSOR;
26215 }
26216
26217 if (CONSP (arg)
26218 && EQ (XCAR (arg), Qbar)
26219 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26220 {
26221 *width = XINT (XCDR (arg));
26222 return BAR_CURSOR;
26223 }
26224
26225 if (EQ (arg, Qhbar))
26226 {
26227 *width = 2;
26228 return HBAR_CURSOR;
26229 }
26230
26231 if (CONSP (arg)
26232 && EQ (XCAR (arg), Qhbar)
26233 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26234 {
26235 *width = XINT (XCDR (arg));
26236 return HBAR_CURSOR;
26237 }
26238
26239 /* Treat anything unknown as "hollow box cursor".
26240 It was bad to signal an error; people have trouble fixing
26241 .Xdefaults with Emacs, when it has something bad in it. */
26242 type = HOLLOW_BOX_CURSOR;
26243
26244 return type;
26245 }
26246
26247 /* Set the default cursor types for specified frame. */
26248 void
26249 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26250 {
26251 int width = 1;
26252 Lisp_Object tem;
26253
26254 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26255 FRAME_CURSOR_WIDTH (f) = width;
26256
26257 /* By default, set up the blink-off state depending on the on-state. */
26258
26259 tem = Fassoc (arg, Vblink_cursor_alist);
26260 if (!NILP (tem))
26261 {
26262 FRAME_BLINK_OFF_CURSOR (f)
26263 = get_specified_cursor_type (XCDR (tem), &width);
26264 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26265 }
26266 else
26267 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26268
26269 /* Make sure the cursor gets redrawn. */
26270 cursor_type_changed = 1;
26271 }
26272
26273
26274 #ifdef HAVE_WINDOW_SYSTEM
26275
26276 /* Return the cursor we want to be displayed in window W. Return
26277 width of bar/hbar cursor through WIDTH arg. Return with
26278 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26279 (i.e. if the `system caret' should track this cursor).
26280
26281 In a mini-buffer window, we want the cursor only to appear if we
26282 are reading input from this window. For the selected window, we
26283 want the cursor type given by the frame parameter or buffer local
26284 setting of cursor-type. If explicitly marked off, draw no cursor.
26285 In all other cases, we want a hollow box cursor. */
26286
26287 static enum text_cursor_kinds
26288 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26289 int *active_cursor)
26290 {
26291 struct frame *f = XFRAME (w->frame);
26292 struct buffer *b = XBUFFER (w->contents);
26293 int cursor_type = DEFAULT_CURSOR;
26294 Lisp_Object alt_cursor;
26295 int non_selected = 0;
26296
26297 *active_cursor = 1;
26298
26299 /* Echo area */
26300 if (cursor_in_echo_area
26301 && FRAME_HAS_MINIBUF_P (f)
26302 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26303 {
26304 if (w == XWINDOW (echo_area_window))
26305 {
26306 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26307 {
26308 *width = FRAME_CURSOR_WIDTH (f);
26309 return FRAME_DESIRED_CURSOR (f);
26310 }
26311 else
26312 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26313 }
26314
26315 *active_cursor = 0;
26316 non_selected = 1;
26317 }
26318
26319 /* Detect a nonselected window or nonselected frame. */
26320 else if (w != XWINDOW (f->selected_window)
26321 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26322 {
26323 *active_cursor = 0;
26324
26325 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26326 return NO_CURSOR;
26327
26328 non_selected = 1;
26329 }
26330
26331 /* Never display a cursor in a window in which cursor-type is nil. */
26332 if (NILP (BVAR (b, cursor_type)))
26333 return NO_CURSOR;
26334
26335 /* Get the normal cursor type for this window. */
26336 if (EQ (BVAR (b, cursor_type), Qt))
26337 {
26338 cursor_type = FRAME_DESIRED_CURSOR (f);
26339 *width = FRAME_CURSOR_WIDTH (f);
26340 }
26341 else
26342 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26343
26344 /* Use cursor-in-non-selected-windows instead
26345 for non-selected window or frame. */
26346 if (non_selected)
26347 {
26348 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26349 if (!EQ (Qt, alt_cursor))
26350 return get_specified_cursor_type (alt_cursor, width);
26351 /* t means modify the normal cursor type. */
26352 if (cursor_type == FILLED_BOX_CURSOR)
26353 cursor_type = HOLLOW_BOX_CURSOR;
26354 else if (cursor_type == BAR_CURSOR && *width > 1)
26355 --*width;
26356 return cursor_type;
26357 }
26358
26359 /* Use normal cursor if not blinked off. */
26360 if (!w->cursor_off_p)
26361 {
26362
26363 #ifdef HAVE_XWIDGETS
26364 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
26365 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
26366 return NO_CURSOR;
26367 }
26368 #endif
26369 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26370 {
26371 if (cursor_type == FILLED_BOX_CURSOR)
26372 {
26373 /* Using a block cursor on large images can be very annoying.
26374 So use a hollow cursor for "large" images.
26375 If image is not transparent (no mask), also use hollow cursor. */
26376 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26377 if (img != NULL && IMAGEP (img->spec))
26378 {
26379 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26380 where N = size of default frame font size.
26381 This should cover most of the "tiny" icons people may use. */
26382 if (!img->mask
26383 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26384 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26385 cursor_type = HOLLOW_BOX_CURSOR;
26386 }
26387 }
26388 else if (cursor_type != NO_CURSOR)
26389 {
26390 /* Display current only supports BOX and HOLLOW cursors for images.
26391 So for now, unconditionally use a HOLLOW cursor when cursor is
26392 not a solid box cursor. */
26393 cursor_type = HOLLOW_BOX_CURSOR;
26394 }
26395 }
26396 return cursor_type;
26397 }
26398
26399 /* Cursor is blinked off, so determine how to "toggle" it. */
26400
26401 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26402 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26403 return get_specified_cursor_type (XCDR (alt_cursor), width);
26404
26405 /* Then see if frame has specified a specific blink off cursor type. */
26406 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26407 {
26408 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26409 return FRAME_BLINK_OFF_CURSOR (f);
26410 }
26411
26412 #if 0
26413 /* Some people liked having a permanently visible blinking cursor,
26414 while others had very strong opinions against it. So it was
26415 decided to remove it. KFS 2003-09-03 */
26416
26417 /* Finally perform built-in cursor blinking:
26418 filled box <-> hollow box
26419 wide [h]bar <-> narrow [h]bar
26420 narrow [h]bar <-> no cursor
26421 other type <-> no cursor */
26422
26423 if (cursor_type == FILLED_BOX_CURSOR)
26424 return HOLLOW_BOX_CURSOR;
26425
26426 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26427 {
26428 *width = 1;
26429 return cursor_type;
26430 }
26431 #endif
26432
26433 return NO_CURSOR;
26434 }
26435
26436
26437 /* Notice when the text cursor of window W has been completely
26438 overwritten by a drawing operation that outputs glyphs in AREA
26439 starting at X0 and ending at X1 in the line starting at Y0 and
26440 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26441 the rest of the line after X0 has been written. Y coordinates
26442 are window-relative. */
26443
26444 static void
26445 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26446 int x0, int x1, int y0, int y1)
26447 {
26448 int cx0, cx1, cy0, cy1;
26449 struct glyph_row *row;
26450
26451 if (!w->phys_cursor_on_p)
26452 return;
26453 if (area != TEXT_AREA)
26454 return;
26455
26456 if (w->phys_cursor.vpos < 0
26457 || w->phys_cursor.vpos >= w->current_matrix->nrows
26458 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26459 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26460 return;
26461
26462 if (row->cursor_in_fringe_p)
26463 {
26464 row->cursor_in_fringe_p = 0;
26465 draw_fringe_bitmap (w, row, row->reversed_p);
26466 w->phys_cursor_on_p = 0;
26467 return;
26468 }
26469
26470 cx0 = w->phys_cursor.x;
26471 cx1 = cx0 + w->phys_cursor_width;
26472 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26473 return;
26474
26475 /* The cursor image will be completely removed from the
26476 screen if the output area intersects the cursor area in
26477 y-direction. When we draw in [y0 y1[, and some part of
26478 the cursor is at y < y0, that part must have been drawn
26479 before. When scrolling, the cursor is erased before
26480 actually scrolling, so we don't come here. When not
26481 scrolling, the rows above the old cursor row must have
26482 changed, and in this case these rows must have written
26483 over the cursor image.
26484
26485 Likewise if part of the cursor is below y1, with the
26486 exception of the cursor being in the first blank row at
26487 the buffer and window end because update_text_area
26488 doesn't draw that row. (Except when it does, but
26489 that's handled in update_text_area.) */
26490
26491 cy0 = w->phys_cursor.y;
26492 cy1 = cy0 + w->phys_cursor_height;
26493 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26494 return;
26495
26496 w->phys_cursor_on_p = 0;
26497 }
26498
26499 #endif /* HAVE_WINDOW_SYSTEM */
26500
26501 \f
26502 /************************************************************************
26503 Mouse Face
26504 ************************************************************************/
26505
26506 #ifdef HAVE_WINDOW_SYSTEM
26507
26508 /* EXPORT for RIF:
26509 Fix the display of area AREA of overlapping row ROW in window W
26510 with respect to the overlapping part OVERLAPS. */
26511
26512 void
26513 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26514 enum glyph_row_area area, int overlaps)
26515 {
26516 int i, x;
26517
26518 block_input ();
26519
26520 x = 0;
26521 for (i = 0; i < row->used[area];)
26522 {
26523 if (row->glyphs[area][i].overlaps_vertically_p)
26524 {
26525 int start = i, start_x = x;
26526
26527 do
26528 {
26529 x += row->glyphs[area][i].pixel_width;
26530 ++i;
26531 }
26532 while (i < row->used[area]
26533 && row->glyphs[area][i].overlaps_vertically_p);
26534
26535 draw_glyphs (w, start_x, row, area,
26536 start, i,
26537 DRAW_NORMAL_TEXT, overlaps);
26538 }
26539 else
26540 {
26541 x += row->glyphs[area][i].pixel_width;
26542 ++i;
26543 }
26544 }
26545
26546 unblock_input ();
26547 }
26548
26549
26550 /* EXPORT:
26551 Draw the cursor glyph of window W in glyph row ROW. See the
26552 comment of draw_glyphs for the meaning of HL. */
26553
26554 void
26555 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26556 enum draw_glyphs_face hl)
26557 {
26558 /* If cursor hpos is out of bounds, don't draw garbage. This can
26559 happen in mini-buffer windows when switching between echo area
26560 glyphs and mini-buffer. */
26561 if ((row->reversed_p
26562 ? (w->phys_cursor.hpos >= 0)
26563 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26564 {
26565 int on_p = w->phys_cursor_on_p;
26566 int x1;
26567 int hpos = w->phys_cursor.hpos;
26568
26569 /* When the window is hscrolled, cursor hpos can legitimately be
26570 out of bounds, but we draw the cursor at the corresponding
26571 window margin in that case. */
26572 if (!row->reversed_p && hpos < 0)
26573 hpos = 0;
26574 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26575 hpos = row->used[TEXT_AREA] - 1;
26576
26577 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26578 hl, 0);
26579 w->phys_cursor_on_p = on_p;
26580
26581 if (hl == DRAW_CURSOR)
26582 w->phys_cursor_width = x1 - w->phys_cursor.x;
26583 /* When we erase the cursor, and ROW is overlapped by other
26584 rows, make sure that these overlapping parts of other rows
26585 are redrawn. */
26586 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26587 {
26588 w->phys_cursor_width = x1 - w->phys_cursor.x;
26589
26590 if (row > w->current_matrix->rows
26591 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26592 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26593 OVERLAPS_ERASED_CURSOR);
26594
26595 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26596 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26597 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26598 OVERLAPS_ERASED_CURSOR);
26599 }
26600 }
26601 }
26602
26603
26604 /* EXPORT:
26605 Erase the image of a cursor of window W from the screen. */
26606
26607 void
26608 erase_phys_cursor (struct window *w)
26609 {
26610 struct frame *f = XFRAME (w->frame);
26611 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26612 int hpos = w->phys_cursor.hpos;
26613 int vpos = w->phys_cursor.vpos;
26614 int mouse_face_here_p = 0;
26615 struct glyph_matrix *active_glyphs = w->current_matrix;
26616 struct glyph_row *cursor_row;
26617 struct glyph *cursor_glyph;
26618 enum draw_glyphs_face hl;
26619
26620 /* No cursor displayed or row invalidated => nothing to do on the
26621 screen. */
26622 if (w->phys_cursor_type == NO_CURSOR)
26623 goto mark_cursor_off;
26624
26625 /* VPOS >= active_glyphs->nrows means that window has been resized.
26626 Don't bother to erase the cursor. */
26627 if (vpos >= active_glyphs->nrows)
26628 goto mark_cursor_off;
26629
26630 /* If row containing cursor is marked invalid, there is nothing we
26631 can do. */
26632 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26633 if (!cursor_row->enabled_p)
26634 goto mark_cursor_off;
26635
26636 /* If line spacing is > 0, old cursor may only be partially visible in
26637 window after split-window. So adjust visible height. */
26638 cursor_row->visible_height = min (cursor_row->visible_height,
26639 window_text_bottom_y (w) - cursor_row->y);
26640
26641 /* If row is completely invisible, don't attempt to delete a cursor which
26642 isn't there. This can happen if cursor is at top of a window, and
26643 we switch to a buffer with a header line in that window. */
26644 if (cursor_row->visible_height <= 0)
26645 goto mark_cursor_off;
26646
26647 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26648 if (cursor_row->cursor_in_fringe_p)
26649 {
26650 cursor_row->cursor_in_fringe_p = 0;
26651 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26652 goto mark_cursor_off;
26653 }
26654
26655 /* This can happen when the new row is shorter than the old one.
26656 In this case, either draw_glyphs or clear_end_of_line
26657 should have cleared the cursor. Note that we wouldn't be
26658 able to erase the cursor in this case because we don't have a
26659 cursor glyph at hand. */
26660 if ((cursor_row->reversed_p
26661 ? (w->phys_cursor.hpos < 0)
26662 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26663 goto mark_cursor_off;
26664
26665 /* When the window is hscrolled, cursor hpos can legitimately be out
26666 of bounds, but we draw the cursor at the corresponding window
26667 margin in that case. */
26668 if (!cursor_row->reversed_p && hpos < 0)
26669 hpos = 0;
26670 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26671 hpos = cursor_row->used[TEXT_AREA] - 1;
26672
26673 /* If the cursor is in the mouse face area, redisplay that when
26674 we clear the cursor. */
26675 if (! NILP (hlinfo->mouse_face_window)
26676 && coords_in_mouse_face_p (w, hpos, vpos)
26677 /* Don't redraw the cursor's spot in mouse face if it is at the
26678 end of a line (on a newline). The cursor appears there, but
26679 mouse highlighting does not. */
26680 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26681 mouse_face_here_p = 1;
26682
26683 /* Maybe clear the display under the cursor. */
26684 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26685 {
26686 int x, y, left_x;
26687 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26688 int width;
26689
26690 cursor_glyph = get_phys_cursor_glyph (w);
26691 if (cursor_glyph == NULL)
26692 goto mark_cursor_off;
26693
26694 width = cursor_glyph->pixel_width;
26695 left_x = window_box_left_offset (w, TEXT_AREA);
26696 x = w->phys_cursor.x;
26697 if (x < left_x)
26698 width -= left_x - x;
26699 width = min (width, window_box_width (w, TEXT_AREA) - x);
26700 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26701 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26702
26703 if (width > 0)
26704 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26705 }
26706
26707 /* Erase the cursor by redrawing the character underneath it. */
26708 if (mouse_face_here_p)
26709 hl = DRAW_MOUSE_FACE;
26710 else
26711 hl = DRAW_NORMAL_TEXT;
26712 draw_phys_cursor_glyph (w, cursor_row, hl);
26713
26714 mark_cursor_off:
26715 w->phys_cursor_on_p = 0;
26716 w->phys_cursor_type = NO_CURSOR;
26717 }
26718
26719
26720 /* EXPORT:
26721 Display or clear cursor of window W. If ON is zero, clear the
26722 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26723 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26724
26725 void
26726 display_and_set_cursor (struct window *w, bool on,
26727 int hpos, int vpos, int x, int y)
26728 {
26729 struct frame *f = XFRAME (w->frame);
26730 int new_cursor_type;
26731 int new_cursor_width;
26732 int active_cursor;
26733 struct glyph_row *glyph_row;
26734 struct glyph *glyph;
26735
26736 /* This is pointless on invisible frames, and dangerous on garbaged
26737 windows and frames; in the latter case, the frame or window may
26738 be in the midst of changing its size, and x and y may be off the
26739 window. */
26740 if (! FRAME_VISIBLE_P (f)
26741 || FRAME_GARBAGED_P (f)
26742 || vpos >= w->current_matrix->nrows
26743 || hpos >= w->current_matrix->matrix_w)
26744 return;
26745
26746 /* If cursor is off and we want it off, return quickly. */
26747 if (!on && !w->phys_cursor_on_p)
26748 return;
26749
26750 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26751 /* If cursor row is not enabled, we don't really know where to
26752 display the cursor. */
26753 if (!glyph_row->enabled_p)
26754 {
26755 w->phys_cursor_on_p = 0;
26756 return;
26757 }
26758
26759 glyph = NULL;
26760 if (!glyph_row->exact_window_width_line_p
26761 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26762 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26763
26764 eassert (input_blocked_p ());
26765
26766 /* Set new_cursor_type to the cursor we want to be displayed. */
26767 new_cursor_type = get_window_cursor_type (w, glyph,
26768 &new_cursor_width, &active_cursor);
26769
26770 /* If cursor is currently being shown and we don't want it to be or
26771 it is in the wrong place, or the cursor type is not what we want,
26772 erase it. */
26773 if (w->phys_cursor_on_p
26774 && (!on
26775 || w->phys_cursor.x != x
26776 || w->phys_cursor.y != y
26777 || new_cursor_type != w->phys_cursor_type
26778 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26779 && new_cursor_width != w->phys_cursor_width)))
26780 erase_phys_cursor (w);
26781
26782 /* Don't check phys_cursor_on_p here because that flag is only set
26783 to zero in some cases where we know that the cursor has been
26784 completely erased, to avoid the extra work of erasing the cursor
26785 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26786 still not be visible, or it has only been partly erased. */
26787 if (on)
26788 {
26789 w->phys_cursor_ascent = glyph_row->ascent;
26790 w->phys_cursor_height = glyph_row->height;
26791
26792 /* Set phys_cursor_.* before x_draw_.* is called because some
26793 of them may need the information. */
26794 w->phys_cursor.x = x;
26795 w->phys_cursor.y = glyph_row->y;
26796 w->phys_cursor.hpos = hpos;
26797 w->phys_cursor.vpos = vpos;
26798 }
26799
26800 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26801 new_cursor_type, new_cursor_width,
26802 on, active_cursor);
26803 }
26804
26805
26806 /* Switch the display of W's cursor on or off, according to the value
26807 of ON. */
26808
26809 static void
26810 update_window_cursor (struct window *w, bool on)
26811 {
26812 /* Don't update cursor in windows whose frame is in the process
26813 of being deleted. */
26814 if (w->current_matrix)
26815 {
26816 int hpos = w->phys_cursor.hpos;
26817 int vpos = w->phys_cursor.vpos;
26818 struct glyph_row *row;
26819
26820 if (vpos >= w->current_matrix->nrows
26821 || hpos >= w->current_matrix->matrix_w)
26822 return;
26823
26824 row = MATRIX_ROW (w->current_matrix, vpos);
26825
26826 /* When the window is hscrolled, cursor hpos can legitimately be
26827 out of bounds, but we draw the cursor at the corresponding
26828 window margin in that case. */
26829 if (!row->reversed_p && hpos < 0)
26830 hpos = 0;
26831 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26832 hpos = row->used[TEXT_AREA] - 1;
26833
26834 block_input ();
26835 display_and_set_cursor (w, on, hpos, vpos,
26836 w->phys_cursor.x, w->phys_cursor.y);
26837 unblock_input ();
26838 }
26839 }
26840
26841
26842 /* Call update_window_cursor with parameter ON_P on all leaf windows
26843 in the window tree rooted at W. */
26844
26845 static void
26846 update_cursor_in_window_tree (struct window *w, bool on_p)
26847 {
26848 while (w)
26849 {
26850 if (WINDOWP (w->contents))
26851 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26852 else
26853 update_window_cursor (w, on_p);
26854
26855 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26856 }
26857 }
26858
26859
26860 /* EXPORT:
26861 Display the cursor on window W, or clear it, according to ON_P.
26862 Don't change the cursor's position. */
26863
26864 void
26865 x_update_cursor (struct frame *f, bool on_p)
26866 {
26867 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26868 }
26869
26870
26871 /* EXPORT:
26872 Clear the cursor of window W to background color, and mark the
26873 cursor as not shown. This is used when the text where the cursor
26874 is about to be rewritten. */
26875
26876 void
26877 x_clear_cursor (struct window *w)
26878 {
26879 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26880 update_window_cursor (w, 0);
26881 }
26882
26883 #endif /* HAVE_WINDOW_SYSTEM */
26884
26885 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26886 and MSDOS. */
26887 static void
26888 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26889 int start_hpos, int end_hpos,
26890 enum draw_glyphs_face draw)
26891 {
26892 #ifdef HAVE_WINDOW_SYSTEM
26893 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26894 {
26895 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26896 return;
26897 }
26898 #endif
26899 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26900 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26901 #endif
26902 }
26903
26904 /* Display the active region described by mouse_face_* according to DRAW. */
26905
26906 static void
26907 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26908 {
26909 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26910 struct frame *f = XFRAME (WINDOW_FRAME (w));
26911
26912 if (/* If window is in the process of being destroyed, don't bother
26913 to do anything. */
26914 w->current_matrix != NULL
26915 /* Don't update mouse highlight if hidden */
26916 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26917 /* Recognize when we are called to operate on rows that don't exist
26918 anymore. This can happen when a window is split. */
26919 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26920 {
26921 int phys_cursor_on_p = w->phys_cursor_on_p;
26922 struct glyph_row *row, *first, *last;
26923
26924 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26925 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26926
26927 for (row = first; row <= last && row->enabled_p; ++row)
26928 {
26929 int start_hpos, end_hpos, start_x;
26930
26931 /* For all but the first row, the highlight starts at column 0. */
26932 if (row == first)
26933 {
26934 /* R2L rows have BEG and END in reversed order, but the
26935 screen drawing geometry is always left to right. So
26936 we need to mirror the beginning and end of the
26937 highlighted area in R2L rows. */
26938 if (!row->reversed_p)
26939 {
26940 start_hpos = hlinfo->mouse_face_beg_col;
26941 start_x = hlinfo->mouse_face_beg_x;
26942 }
26943 else if (row == last)
26944 {
26945 start_hpos = hlinfo->mouse_face_end_col;
26946 start_x = hlinfo->mouse_face_end_x;
26947 }
26948 else
26949 {
26950 start_hpos = 0;
26951 start_x = 0;
26952 }
26953 }
26954 else if (row->reversed_p && row == last)
26955 {
26956 start_hpos = hlinfo->mouse_face_end_col;
26957 start_x = hlinfo->mouse_face_end_x;
26958 }
26959 else
26960 {
26961 start_hpos = 0;
26962 start_x = 0;
26963 }
26964
26965 if (row == last)
26966 {
26967 if (!row->reversed_p)
26968 end_hpos = hlinfo->mouse_face_end_col;
26969 else if (row == first)
26970 end_hpos = hlinfo->mouse_face_beg_col;
26971 else
26972 {
26973 end_hpos = row->used[TEXT_AREA];
26974 if (draw == DRAW_NORMAL_TEXT)
26975 row->fill_line_p = 1; /* Clear to end of line */
26976 }
26977 }
26978 else if (row->reversed_p && row == first)
26979 end_hpos = hlinfo->mouse_face_beg_col;
26980 else
26981 {
26982 end_hpos = row->used[TEXT_AREA];
26983 if (draw == DRAW_NORMAL_TEXT)
26984 row->fill_line_p = 1; /* Clear to end of line */
26985 }
26986
26987 if (end_hpos > start_hpos)
26988 {
26989 draw_row_with_mouse_face (w, start_x, row,
26990 start_hpos, end_hpos, draw);
26991
26992 row->mouse_face_p
26993 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26994 }
26995 }
26996
26997 #ifdef HAVE_WINDOW_SYSTEM
26998 /* When we've written over the cursor, arrange for it to
26999 be displayed again. */
27000 if (FRAME_WINDOW_P (f)
27001 && phys_cursor_on_p && !w->phys_cursor_on_p)
27002 {
27003 int hpos = w->phys_cursor.hpos;
27004
27005 /* When the window is hscrolled, cursor hpos can legitimately be
27006 out of bounds, but we draw the cursor at the corresponding
27007 window margin in that case. */
27008 if (!row->reversed_p && hpos < 0)
27009 hpos = 0;
27010 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27011 hpos = row->used[TEXT_AREA] - 1;
27012
27013 block_input ();
27014 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27015 w->phys_cursor.x, w->phys_cursor.y);
27016 unblock_input ();
27017 }
27018 #endif /* HAVE_WINDOW_SYSTEM */
27019 }
27020
27021 #ifdef HAVE_WINDOW_SYSTEM
27022 /* Change the mouse cursor. */
27023 if (FRAME_WINDOW_P (f))
27024 {
27025 if (draw == DRAW_NORMAL_TEXT
27026 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27027 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27028 else if (draw == DRAW_MOUSE_FACE)
27029 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27030 else
27031 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27032 }
27033 #endif /* HAVE_WINDOW_SYSTEM */
27034 }
27035
27036 /* EXPORT:
27037 Clear out the mouse-highlighted active region.
27038 Redraw it un-highlighted first. Value is non-zero if mouse
27039 face was actually drawn unhighlighted. */
27040
27041 int
27042 clear_mouse_face (Mouse_HLInfo *hlinfo)
27043 {
27044 int cleared = 0;
27045
27046 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27047 {
27048 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27049 cleared = 1;
27050 }
27051
27052 reset_mouse_highlight (hlinfo);
27053 return cleared;
27054 }
27055
27056 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
27057 within the mouse face on that window. */
27058 static int
27059 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27060 {
27061 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27062
27063 /* Quickly resolve the easy cases. */
27064 if (!(WINDOWP (hlinfo->mouse_face_window)
27065 && XWINDOW (hlinfo->mouse_face_window) == w))
27066 return 0;
27067 if (vpos < hlinfo->mouse_face_beg_row
27068 || vpos > hlinfo->mouse_face_end_row)
27069 return 0;
27070 if (vpos > hlinfo->mouse_face_beg_row
27071 && vpos < hlinfo->mouse_face_end_row)
27072 return 1;
27073
27074 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27075 {
27076 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27077 {
27078 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27079 return 1;
27080 }
27081 else if ((vpos == hlinfo->mouse_face_beg_row
27082 && hpos >= hlinfo->mouse_face_beg_col)
27083 || (vpos == hlinfo->mouse_face_end_row
27084 && hpos < hlinfo->mouse_face_end_col))
27085 return 1;
27086 }
27087 else
27088 {
27089 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27090 {
27091 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27092 return 1;
27093 }
27094 else if ((vpos == hlinfo->mouse_face_beg_row
27095 && hpos <= hlinfo->mouse_face_beg_col)
27096 || (vpos == hlinfo->mouse_face_end_row
27097 && hpos > hlinfo->mouse_face_end_col))
27098 return 1;
27099 }
27100 return 0;
27101 }
27102
27103
27104 /* EXPORT:
27105 Non-zero if physical cursor of window W is within mouse face. */
27106
27107 int
27108 cursor_in_mouse_face_p (struct window *w)
27109 {
27110 int hpos = w->phys_cursor.hpos;
27111 int vpos = w->phys_cursor.vpos;
27112 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27113
27114 /* When the window is hscrolled, cursor hpos can legitimately be out
27115 of bounds, but we draw the cursor at the corresponding window
27116 margin in that case. */
27117 if (!row->reversed_p && hpos < 0)
27118 hpos = 0;
27119 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27120 hpos = row->used[TEXT_AREA] - 1;
27121
27122 return coords_in_mouse_face_p (w, hpos, vpos);
27123 }
27124
27125
27126 \f
27127 /* Find the glyph rows START_ROW and END_ROW of window W that display
27128 characters between buffer positions START_CHARPOS and END_CHARPOS
27129 (excluding END_CHARPOS). DISP_STRING is a display string that
27130 covers these buffer positions. This is similar to
27131 row_containing_pos, but is more accurate when bidi reordering makes
27132 buffer positions change non-linearly with glyph rows. */
27133 static void
27134 rows_from_pos_range (struct window *w,
27135 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27136 Lisp_Object disp_string,
27137 struct glyph_row **start, struct glyph_row **end)
27138 {
27139 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27140 int last_y = window_text_bottom_y (w);
27141 struct glyph_row *row;
27142
27143 *start = NULL;
27144 *end = NULL;
27145
27146 while (!first->enabled_p
27147 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27148 first++;
27149
27150 /* Find the START row. */
27151 for (row = first;
27152 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27153 row++)
27154 {
27155 /* A row can potentially be the START row if the range of the
27156 characters it displays intersects the range
27157 [START_CHARPOS..END_CHARPOS). */
27158 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27159 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27160 /* See the commentary in row_containing_pos, for the
27161 explanation of the complicated way to check whether
27162 some position is beyond the end of the characters
27163 displayed by a row. */
27164 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27165 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27166 && !row->ends_at_zv_p
27167 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27168 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27169 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27170 && !row->ends_at_zv_p
27171 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27172 {
27173 /* Found a candidate row. Now make sure at least one of the
27174 glyphs it displays has a charpos from the range
27175 [START_CHARPOS..END_CHARPOS).
27176
27177 This is not obvious because bidi reordering could make
27178 buffer positions of a row be 1,2,3,102,101,100, and if we
27179 want to highlight characters in [50..60), we don't want
27180 this row, even though [50..60) does intersect [1..103),
27181 the range of character positions given by the row's start
27182 and end positions. */
27183 struct glyph *g = row->glyphs[TEXT_AREA];
27184 struct glyph *e = g + row->used[TEXT_AREA];
27185
27186 while (g < e)
27187 {
27188 if (((BUFFERP (g->object) || INTEGERP (g->object))
27189 && start_charpos <= g->charpos && g->charpos < end_charpos)
27190 /* A glyph that comes from DISP_STRING is by
27191 definition to be highlighted. */
27192 || EQ (g->object, disp_string))
27193 *start = row;
27194 g++;
27195 }
27196 if (*start)
27197 break;
27198 }
27199 }
27200
27201 /* Find the END row. */
27202 if (!*start
27203 /* If the last row is partially visible, start looking for END
27204 from that row, instead of starting from FIRST. */
27205 && !(row->enabled_p
27206 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27207 row = first;
27208 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27209 {
27210 struct glyph_row *next = row + 1;
27211 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27212
27213 if (!next->enabled_p
27214 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27215 /* The first row >= START whose range of displayed characters
27216 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27217 is the row END + 1. */
27218 || (start_charpos < next_start
27219 && end_charpos < next_start)
27220 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27221 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27222 && !next->ends_at_zv_p
27223 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27224 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27225 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27226 && !next->ends_at_zv_p
27227 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27228 {
27229 *end = row;
27230 break;
27231 }
27232 else
27233 {
27234 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27235 but none of the characters it displays are in the range, it is
27236 also END + 1. */
27237 struct glyph *g = next->glyphs[TEXT_AREA];
27238 struct glyph *s = g;
27239 struct glyph *e = g + next->used[TEXT_AREA];
27240
27241 while (g < e)
27242 {
27243 if (((BUFFERP (g->object) || INTEGERP (g->object))
27244 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27245 /* If the buffer position of the first glyph in
27246 the row is equal to END_CHARPOS, it means
27247 the last character to be highlighted is the
27248 newline of ROW, and we must consider NEXT as
27249 END, not END+1. */
27250 || (((!next->reversed_p && g == s)
27251 || (next->reversed_p && g == e - 1))
27252 && (g->charpos == end_charpos
27253 /* Special case for when NEXT is an
27254 empty line at ZV. */
27255 || (g->charpos == -1
27256 && !row->ends_at_zv_p
27257 && next_start == end_charpos)))))
27258 /* A glyph that comes from DISP_STRING is by
27259 definition to be highlighted. */
27260 || EQ (g->object, disp_string))
27261 break;
27262 g++;
27263 }
27264 if (g == e)
27265 {
27266 *end = row;
27267 break;
27268 }
27269 /* The first row that ends at ZV must be the last to be
27270 highlighted. */
27271 else if (next->ends_at_zv_p)
27272 {
27273 *end = next;
27274 break;
27275 }
27276 }
27277 }
27278 }
27279
27280 /* This function sets the mouse_face_* elements of HLINFO, assuming
27281 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27282 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27283 for the overlay or run of text properties specifying the mouse
27284 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27285 before-string and after-string that must also be highlighted.
27286 DISP_STRING, if non-nil, is a display string that may cover some
27287 or all of the highlighted text. */
27288
27289 static void
27290 mouse_face_from_buffer_pos (Lisp_Object window,
27291 Mouse_HLInfo *hlinfo,
27292 ptrdiff_t mouse_charpos,
27293 ptrdiff_t start_charpos,
27294 ptrdiff_t end_charpos,
27295 Lisp_Object before_string,
27296 Lisp_Object after_string,
27297 Lisp_Object disp_string)
27298 {
27299 struct window *w = XWINDOW (window);
27300 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27301 struct glyph_row *r1, *r2;
27302 struct glyph *glyph, *end;
27303 ptrdiff_t ignore, pos;
27304 int x;
27305
27306 eassert (NILP (disp_string) || STRINGP (disp_string));
27307 eassert (NILP (before_string) || STRINGP (before_string));
27308 eassert (NILP (after_string) || STRINGP (after_string));
27309
27310 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27311 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27312 if (r1 == NULL)
27313 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27314 /* If the before-string or display-string contains newlines,
27315 rows_from_pos_range skips to its last row. Move back. */
27316 if (!NILP (before_string) || !NILP (disp_string))
27317 {
27318 struct glyph_row *prev;
27319 while ((prev = r1 - 1, prev >= first)
27320 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27321 && prev->used[TEXT_AREA] > 0)
27322 {
27323 struct glyph *beg = prev->glyphs[TEXT_AREA];
27324 glyph = beg + prev->used[TEXT_AREA];
27325 while (--glyph >= beg && INTEGERP (glyph->object));
27326 if (glyph < beg
27327 || !(EQ (glyph->object, before_string)
27328 || EQ (glyph->object, disp_string)))
27329 break;
27330 r1 = prev;
27331 }
27332 }
27333 if (r2 == NULL)
27334 {
27335 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27336 hlinfo->mouse_face_past_end = 1;
27337 }
27338 else if (!NILP (after_string))
27339 {
27340 /* If the after-string has newlines, advance to its last row. */
27341 struct glyph_row *next;
27342 struct glyph_row *last
27343 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27344
27345 for (next = r2 + 1;
27346 next <= last
27347 && next->used[TEXT_AREA] > 0
27348 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27349 ++next)
27350 r2 = next;
27351 }
27352 /* The rest of the display engine assumes that mouse_face_beg_row is
27353 either above mouse_face_end_row or identical to it. But with
27354 bidi-reordered continued lines, the row for START_CHARPOS could
27355 be below the row for END_CHARPOS. If so, swap the rows and store
27356 them in correct order. */
27357 if (r1->y > r2->y)
27358 {
27359 struct glyph_row *tem = r2;
27360
27361 r2 = r1;
27362 r1 = tem;
27363 }
27364
27365 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27366 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27367
27368 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27369 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27370 could be anywhere in the row and in any order. The strategy
27371 below is to find the leftmost and the rightmost glyph that
27372 belongs to either of these 3 strings, or whose position is
27373 between START_CHARPOS and END_CHARPOS, and highlight all the
27374 glyphs between those two. This may cover more than just the text
27375 between START_CHARPOS and END_CHARPOS if the range of characters
27376 strides the bidi level boundary, e.g. if the beginning is in R2L
27377 text while the end is in L2R text or vice versa. */
27378 if (!r1->reversed_p)
27379 {
27380 /* This row is in a left to right paragraph. Scan it left to
27381 right. */
27382 glyph = r1->glyphs[TEXT_AREA];
27383 end = glyph + r1->used[TEXT_AREA];
27384 x = r1->x;
27385
27386 /* Skip truncation glyphs at the start of the glyph row. */
27387 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27388 for (; glyph < end
27389 && INTEGERP (glyph->object)
27390 && glyph->charpos < 0;
27391 ++glyph)
27392 x += glyph->pixel_width;
27393
27394 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27395 or DISP_STRING, and the first glyph from buffer whose
27396 position is between START_CHARPOS and END_CHARPOS. */
27397 for (; glyph < end
27398 && !INTEGERP (glyph->object)
27399 && !EQ (glyph->object, disp_string)
27400 && !(BUFFERP (glyph->object)
27401 && (glyph->charpos >= start_charpos
27402 && glyph->charpos < end_charpos));
27403 ++glyph)
27404 {
27405 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27406 are present at buffer positions between START_CHARPOS and
27407 END_CHARPOS, or if they come from an overlay. */
27408 if (EQ (glyph->object, before_string))
27409 {
27410 pos = string_buffer_position (before_string,
27411 start_charpos);
27412 /* If pos == 0, it means before_string came from an
27413 overlay, not from a buffer position. */
27414 if (!pos || (pos >= start_charpos && pos < end_charpos))
27415 break;
27416 }
27417 else if (EQ (glyph->object, after_string))
27418 {
27419 pos = string_buffer_position (after_string, end_charpos);
27420 if (!pos || (pos >= start_charpos && pos < end_charpos))
27421 break;
27422 }
27423 x += glyph->pixel_width;
27424 }
27425 hlinfo->mouse_face_beg_x = x;
27426 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27427 }
27428 else
27429 {
27430 /* This row is in a right to left paragraph. Scan it right to
27431 left. */
27432 struct glyph *g;
27433
27434 end = r1->glyphs[TEXT_AREA] - 1;
27435 glyph = end + r1->used[TEXT_AREA];
27436
27437 /* Skip truncation glyphs at the start of the glyph row. */
27438 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27439 for (; glyph > end
27440 && INTEGERP (glyph->object)
27441 && glyph->charpos < 0;
27442 --glyph)
27443 ;
27444
27445 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27446 or DISP_STRING, and the first glyph from buffer whose
27447 position is between START_CHARPOS and END_CHARPOS. */
27448 for (; glyph > end
27449 && !INTEGERP (glyph->object)
27450 && !EQ (glyph->object, disp_string)
27451 && !(BUFFERP (glyph->object)
27452 && (glyph->charpos >= start_charpos
27453 && glyph->charpos < end_charpos));
27454 --glyph)
27455 {
27456 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27457 are present at buffer positions between START_CHARPOS and
27458 END_CHARPOS, or if they come from an overlay. */
27459 if (EQ (glyph->object, before_string))
27460 {
27461 pos = string_buffer_position (before_string, start_charpos);
27462 /* If pos == 0, it means before_string came from an
27463 overlay, not from a buffer position. */
27464 if (!pos || (pos >= start_charpos && pos < end_charpos))
27465 break;
27466 }
27467 else if (EQ (glyph->object, after_string))
27468 {
27469 pos = string_buffer_position (after_string, end_charpos);
27470 if (!pos || (pos >= start_charpos && pos < end_charpos))
27471 break;
27472 }
27473 }
27474
27475 glyph++; /* first glyph to the right of the highlighted area */
27476 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27477 x += g->pixel_width;
27478 hlinfo->mouse_face_beg_x = x;
27479 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27480 }
27481
27482 /* If the highlight ends in a different row, compute GLYPH and END
27483 for the end row. Otherwise, reuse the values computed above for
27484 the row where the highlight begins. */
27485 if (r2 != r1)
27486 {
27487 if (!r2->reversed_p)
27488 {
27489 glyph = r2->glyphs[TEXT_AREA];
27490 end = glyph + r2->used[TEXT_AREA];
27491 x = r2->x;
27492 }
27493 else
27494 {
27495 end = r2->glyphs[TEXT_AREA] - 1;
27496 glyph = end + r2->used[TEXT_AREA];
27497 }
27498 }
27499
27500 if (!r2->reversed_p)
27501 {
27502 /* Skip truncation and continuation glyphs near the end of the
27503 row, and also blanks and stretch glyphs inserted by
27504 extend_face_to_end_of_line. */
27505 while (end > glyph
27506 && INTEGERP ((end - 1)->object))
27507 --end;
27508 /* Scan the rest of the glyph row from the end, looking for the
27509 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27510 DISP_STRING, or whose position is between START_CHARPOS
27511 and END_CHARPOS */
27512 for (--end;
27513 end > glyph
27514 && !INTEGERP (end->object)
27515 && !EQ (end->object, disp_string)
27516 && !(BUFFERP (end->object)
27517 && (end->charpos >= start_charpos
27518 && end->charpos < end_charpos));
27519 --end)
27520 {
27521 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27522 are present at buffer positions between START_CHARPOS and
27523 END_CHARPOS, or if they come from an overlay. */
27524 if (EQ (end->object, before_string))
27525 {
27526 pos = string_buffer_position (before_string, start_charpos);
27527 if (!pos || (pos >= start_charpos && pos < end_charpos))
27528 break;
27529 }
27530 else if (EQ (end->object, after_string))
27531 {
27532 pos = string_buffer_position (after_string, end_charpos);
27533 if (!pos || (pos >= start_charpos && pos < end_charpos))
27534 break;
27535 }
27536 }
27537 /* Find the X coordinate of the last glyph to be highlighted. */
27538 for (; glyph <= end; ++glyph)
27539 x += glyph->pixel_width;
27540
27541 hlinfo->mouse_face_end_x = x;
27542 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27543 }
27544 else
27545 {
27546 /* Skip truncation and continuation glyphs near the end of the
27547 row, and also blanks and stretch glyphs inserted by
27548 extend_face_to_end_of_line. */
27549 x = r2->x;
27550 end++;
27551 while (end < glyph
27552 && INTEGERP (end->object))
27553 {
27554 x += end->pixel_width;
27555 ++end;
27556 }
27557 /* Scan the rest of the glyph row from the end, looking for the
27558 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27559 DISP_STRING, or whose position is between START_CHARPOS
27560 and END_CHARPOS */
27561 for ( ;
27562 end < glyph
27563 && !INTEGERP (end->object)
27564 && !EQ (end->object, disp_string)
27565 && !(BUFFERP (end->object)
27566 && (end->charpos >= start_charpos
27567 && end->charpos < end_charpos));
27568 ++end)
27569 {
27570 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27571 are present at buffer positions between START_CHARPOS and
27572 END_CHARPOS, or if they come from an overlay. */
27573 if (EQ (end->object, before_string))
27574 {
27575 pos = string_buffer_position (before_string, start_charpos);
27576 if (!pos || (pos >= start_charpos && pos < end_charpos))
27577 break;
27578 }
27579 else if (EQ (end->object, after_string))
27580 {
27581 pos = string_buffer_position (after_string, end_charpos);
27582 if (!pos || (pos >= start_charpos && pos < end_charpos))
27583 break;
27584 }
27585 x += end->pixel_width;
27586 }
27587 /* If we exited the above loop because we arrived at the last
27588 glyph of the row, and its buffer position is still not in
27589 range, it means the last character in range is the preceding
27590 newline. Bump the end column and x values to get past the
27591 last glyph. */
27592 if (end == glyph
27593 && BUFFERP (end->object)
27594 && (end->charpos < start_charpos
27595 || end->charpos >= end_charpos))
27596 {
27597 x += end->pixel_width;
27598 ++end;
27599 }
27600 hlinfo->mouse_face_end_x = x;
27601 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27602 }
27603
27604 hlinfo->mouse_face_window = window;
27605 hlinfo->mouse_face_face_id
27606 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27607 mouse_charpos + 1,
27608 !hlinfo->mouse_face_hidden, -1);
27609 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27610 }
27611
27612 /* The following function is not used anymore (replaced with
27613 mouse_face_from_string_pos), but I leave it here for the time
27614 being, in case someone would. */
27615
27616 #if 0 /* not used */
27617
27618 /* Find the position of the glyph for position POS in OBJECT in
27619 window W's current matrix, and return in *X, *Y the pixel
27620 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27621
27622 RIGHT_P non-zero means return the position of the right edge of the
27623 glyph, RIGHT_P zero means return the left edge position.
27624
27625 If no glyph for POS exists in the matrix, return the position of
27626 the glyph with the next smaller position that is in the matrix, if
27627 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27628 exists in the matrix, return the position of the glyph with the
27629 next larger position in OBJECT.
27630
27631 Value is non-zero if a glyph was found. */
27632
27633 static int
27634 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27635 int *hpos, int *vpos, int *x, int *y, int right_p)
27636 {
27637 int yb = window_text_bottom_y (w);
27638 struct glyph_row *r;
27639 struct glyph *best_glyph = NULL;
27640 struct glyph_row *best_row = NULL;
27641 int best_x = 0;
27642
27643 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27644 r->enabled_p && r->y < yb;
27645 ++r)
27646 {
27647 struct glyph *g = r->glyphs[TEXT_AREA];
27648 struct glyph *e = g + r->used[TEXT_AREA];
27649 int gx;
27650
27651 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27652 if (EQ (g->object, object))
27653 {
27654 if (g->charpos == pos)
27655 {
27656 best_glyph = g;
27657 best_x = gx;
27658 best_row = r;
27659 goto found;
27660 }
27661 else if (best_glyph == NULL
27662 || ((eabs (g->charpos - pos)
27663 < eabs (best_glyph->charpos - pos))
27664 && (right_p
27665 ? g->charpos < pos
27666 : g->charpos > pos)))
27667 {
27668 best_glyph = g;
27669 best_x = gx;
27670 best_row = r;
27671 }
27672 }
27673 }
27674
27675 found:
27676
27677 if (best_glyph)
27678 {
27679 *x = best_x;
27680 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27681
27682 if (right_p)
27683 {
27684 *x += best_glyph->pixel_width;
27685 ++*hpos;
27686 }
27687
27688 *y = best_row->y;
27689 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27690 }
27691
27692 return best_glyph != NULL;
27693 }
27694 #endif /* not used */
27695
27696 /* Find the positions of the first and the last glyphs in window W's
27697 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27698 (assumed to be a string), and return in HLINFO's mouse_face_*
27699 members the pixel and column/row coordinates of those glyphs. */
27700
27701 static void
27702 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27703 Lisp_Object object,
27704 ptrdiff_t startpos, ptrdiff_t endpos)
27705 {
27706 int yb = window_text_bottom_y (w);
27707 struct glyph_row *r;
27708 struct glyph *g, *e;
27709 int gx;
27710 int found = 0;
27711
27712 /* Find the glyph row with at least one position in the range
27713 [STARTPOS..ENDPOS], and the first glyph in that row whose
27714 position belongs to that range. */
27715 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27716 r->enabled_p && r->y < yb;
27717 ++r)
27718 {
27719 if (!r->reversed_p)
27720 {
27721 g = r->glyphs[TEXT_AREA];
27722 e = g + r->used[TEXT_AREA];
27723 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27724 if (EQ (g->object, object)
27725 && startpos <= g->charpos && g->charpos <= endpos)
27726 {
27727 hlinfo->mouse_face_beg_row
27728 = MATRIX_ROW_VPOS (r, w->current_matrix);
27729 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27730 hlinfo->mouse_face_beg_x = gx;
27731 found = 1;
27732 break;
27733 }
27734 }
27735 else
27736 {
27737 struct glyph *g1;
27738
27739 e = r->glyphs[TEXT_AREA];
27740 g = e + r->used[TEXT_AREA];
27741 for ( ; g > e; --g)
27742 if (EQ ((g-1)->object, object)
27743 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27744 {
27745 hlinfo->mouse_face_beg_row
27746 = MATRIX_ROW_VPOS (r, w->current_matrix);
27747 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27748 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27749 gx += g1->pixel_width;
27750 hlinfo->mouse_face_beg_x = gx;
27751 found = 1;
27752 break;
27753 }
27754 }
27755 if (found)
27756 break;
27757 }
27758
27759 if (!found)
27760 return;
27761
27762 /* Starting with the next row, look for the first row which does NOT
27763 include any glyphs whose positions are in the range. */
27764 for (++r; r->enabled_p && r->y < yb; ++r)
27765 {
27766 g = r->glyphs[TEXT_AREA];
27767 e = g + r->used[TEXT_AREA];
27768 found = 0;
27769 for ( ; g < e; ++g)
27770 if (EQ (g->object, object)
27771 && startpos <= g->charpos && g->charpos <= endpos)
27772 {
27773 found = 1;
27774 break;
27775 }
27776 if (!found)
27777 break;
27778 }
27779
27780 /* The highlighted region ends on the previous row. */
27781 r--;
27782
27783 /* Set the end row. */
27784 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27785
27786 /* Compute and set the end column and the end column's horizontal
27787 pixel coordinate. */
27788 if (!r->reversed_p)
27789 {
27790 g = r->glyphs[TEXT_AREA];
27791 e = g + r->used[TEXT_AREA];
27792 for ( ; e > g; --e)
27793 if (EQ ((e-1)->object, object)
27794 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27795 break;
27796 hlinfo->mouse_face_end_col = e - g;
27797
27798 for (gx = r->x; g < e; ++g)
27799 gx += g->pixel_width;
27800 hlinfo->mouse_face_end_x = gx;
27801 }
27802 else
27803 {
27804 e = r->glyphs[TEXT_AREA];
27805 g = e + r->used[TEXT_AREA];
27806 for (gx = r->x ; e < g; ++e)
27807 {
27808 if (EQ (e->object, object)
27809 && startpos <= e->charpos && e->charpos <= endpos)
27810 break;
27811 gx += e->pixel_width;
27812 }
27813 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27814 hlinfo->mouse_face_end_x = gx;
27815 }
27816 }
27817
27818 #ifdef HAVE_WINDOW_SYSTEM
27819
27820 /* See if position X, Y is within a hot-spot of an image. */
27821
27822 static int
27823 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27824 {
27825 if (!CONSP (hot_spot))
27826 return 0;
27827
27828 if (EQ (XCAR (hot_spot), Qrect))
27829 {
27830 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27831 Lisp_Object rect = XCDR (hot_spot);
27832 Lisp_Object tem;
27833 if (!CONSP (rect))
27834 return 0;
27835 if (!CONSP (XCAR (rect)))
27836 return 0;
27837 if (!CONSP (XCDR (rect)))
27838 return 0;
27839 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27840 return 0;
27841 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27842 return 0;
27843 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27844 return 0;
27845 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27846 return 0;
27847 return 1;
27848 }
27849 else if (EQ (XCAR (hot_spot), Qcircle))
27850 {
27851 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27852 Lisp_Object circ = XCDR (hot_spot);
27853 Lisp_Object lr, lx0, ly0;
27854 if (CONSP (circ)
27855 && CONSP (XCAR (circ))
27856 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27857 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27858 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27859 {
27860 double r = XFLOATINT (lr);
27861 double dx = XINT (lx0) - x;
27862 double dy = XINT (ly0) - y;
27863 return (dx * dx + dy * dy <= r * r);
27864 }
27865 }
27866 else if (EQ (XCAR (hot_spot), Qpoly))
27867 {
27868 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27869 if (VECTORP (XCDR (hot_spot)))
27870 {
27871 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27872 Lisp_Object *poly = v->contents;
27873 ptrdiff_t n = v->header.size;
27874 ptrdiff_t i;
27875 int inside = 0;
27876 Lisp_Object lx, ly;
27877 int x0, y0;
27878
27879 /* Need an even number of coordinates, and at least 3 edges. */
27880 if (n < 6 || n & 1)
27881 return 0;
27882
27883 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27884 If count is odd, we are inside polygon. Pixels on edges
27885 may or may not be included depending on actual geometry of the
27886 polygon. */
27887 if ((lx = poly[n-2], !INTEGERP (lx))
27888 || (ly = poly[n-1], !INTEGERP (lx)))
27889 return 0;
27890 x0 = XINT (lx), y0 = XINT (ly);
27891 for (i = 0; i < n; i += 2)
27892 {
27893 int x1 = x0, y1 = y0;
27894 if ((lx = poly[i], !INTEGERP (lx))
27895 || (ly = poly[i+1], !INTEGERP (ly)))
27896 return 0;
27897 x0 = XINT (lx), y0 = XINT (ly);
27898
27899 /* Does this segment cross the X line? */
27900 if (x0 >= x)
27901 {
27902 if (x1 >= x)
27903 continue;
27904 }
27905 else if (x1 < x)
27906 continue;
27907 if (y > y0 && y > y1)
27908 continue;
27909 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27910 inside = !inside;
27911 }
27912 return inside;
27913 }
27914 }
27915 return 0;
27916 }
27917
27918 Lisp_Object
27919 find_hot_spot (Lisp_Object map, int x, int y)
27920 {
27921 while (CONSP (map))
27922 {
27923 if (CONSP (XCAR (map))
27924 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27925 return XCAR (map);
27926 map = XCDR (map);
27927 }
27928
27929 return Qnil;
27930 }
27931
27932 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27933 3, 3, 0,
27934 doc: /* Lookup in image map MAP coordinates X and Y.
27935 An image map is an alist where each element has the format (AREA ID PLIST).
27936 An AREA is specified as either a rectangle, a circle, or a polygon:
27937 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27938 pixel coordinates of the upper left and bottom right corners.
27939 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27940 and the radius of the circle; r may be a float or integer.
27941 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27942 vector describes one corner in the polygon.
27943 Returns the alist element for the first matching AREA in MAP. */)
27944 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27945 {
27946 if (NILP (map))
27947 return Qnil;
27948
27949 CHECK_NUMBER (x);
27950 CHECK_NUMBER (y);
27951
27952 return find_hot_spot (map,
27953 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27954 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27955 }
27956
27957
27958 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27959 static void
27960 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27961 {
27962 /* Do not change cursor shape while dragging mouse. */
27963 if (!NILP (do_mouse_tracking))
27964 return;
27965
27966 if (!NILP (pointer))
27967 {
27968 if (EQ (pointer, Qarrow))
27969 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27970 else if (EQ (pointer, Qhand))
27971 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27972 else if (EQ (pointer, Qtext))
27973 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27974 else if (EQ (pointer, intern ("hdrag")))
27975 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27976 #ifdef HAVE_X_WINDOWS
27977 else if (EQ (pointer, intern ("vdrag")))
27978 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27979 #endif
27980 else if (EQ (pointer, intern ("hourglass")))
27981 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27982 else if (EQ (pointer, Qmodeline))
27983 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27984 else
27985 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27986 }
27987
27988 if (cursor != No_Cursor)
27989 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27990 }
27991
27992 #endif /* HAVE_WINDOW_SYSTEM */
27993
27994 /* Take proper action when mouse has moved to the mode or header line
27995 or marginal area AREA of window W, x-position X and y-position Y.
27996 X is relative to the start of the text display area of W, so the
27997 width of bitmap areas and scroll bars must be subtracted to get a
27998 position relative to the start of the mode line. */
27999
28000 static void
28001 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28002 enum window_part area)
28003 {
28004 struct window *w = XWINDOW (window);
28005 struct frame *f = XFRAME (w->frame);
28006 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28007 #ifdef HAVE_WINDOW_SYSTEM
28008 Display_Info *dpyinfo;
28009 #endif
28010 Cursor cursor = No_Cursor;
28011 Lisp_Object pointer = Qnil;
28012 int dx, dy, width, height;
28013 ptrdiff_t charpos;
28014 Lisp_Object string, object = Qnil;
28015 Lisp_Object pos IF_LINT (= Qnil), help;
28016
28017 Lisp_Object mouse_face;
28018 int original_x_pixel = x;
28019 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28020 struct glyph_row *row IF_LINT (= 0);
28021
28022 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28023 {
28024 int x0;
28025 struct glyph *end;
28026
28027 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28028 returns them in row/column units! */
28029 string = mode_line_string (w, area, &x, &y, &charpos,
28030 &object, &dx, &dy, &width, &height);
28031
28032 row = (area == ON_MODE_LINE
28033 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28034 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28035
28036 /* Find the glyph under the mouse pointer. */
28037 if (row->mode_line_p && row->enabled_p)
28038 {
28039 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28040 end = glyph + row->used[TEXT_AREA];
28041
28042 for (x0 = original_x_pixel;
28043 glyph < end && x0 >= glyph->pixel_width;
28044 ++glyph)
28045 x0 -= glyph->pixel_width;
28046
28047 if (glyph >= end)
28048 glyph = NULL;
28049 }
28050 }
28051 else
28052 {
28053 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28054 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28055 returns them in row/column units! */
28056 string = marginal_area_string (w, area, &x, &y, &charpos,
28057 &object, &dx, &dy, &width, &height);
28058 }
28059
28060 help = Qnil;
28061
28062 #ifdef HAVE_WINDOW_SYSTEM
28063 if (IMAGEP (object))
28064 {
28065 Lisp_Object image_map, hotspot;
28066 if ((image_map = Fplist_get (XCDR (object), QCmap),
28067 !NILP (image_map))
28068 && (hotspot = find_hot_spot (image_map, dx, dy),
28069 CONSP (hotspot))
28070 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28071 {
28072 Lisp_Object plist;
28073
28074 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28075 If so, we could look for mouse-enter, mouse-leave
28076 properties in PLIST (and do something...). */
28077 hotspot = XCDR (hotspot);
28078 if (CONSP (hotspot)
28079 && (plist = XCAR (hotspot), CONSP (plist)))
28080 {
28081 pointer = Fplist_get (plist, Qpointer);
28082 if (NILP (pointer))
28083 pointer = Qhand;
28084 help = Fplist_get (plist, Qhelp_echo);
28085 if (!NILP (help))
28086 {
28087 help_echo_string = help;
28088 XSETWINDOW (help_echo_window, w);
28089 help_echo_object = w->contents;
28090 help_echo_pos = charpos;
28091 }
28092 }
28093 }
28094 if (NILP (pointer))
28095 pointer = Fplist_get (XCDR (object), QCpointer);
28096 }
28097 #endif /* HAVE_WINDOW_SYSTEM */
28098
28099 if (STRINGP (string))
28100 pos = make_number (charpos);
28101
28102 /* Set the help text and mouse pointer. If the mouse is on a part
28103 of the mode line without any text (e.g. past the right edge of
28104 the mode line text), use the default help text and pointer. */
28105 if (STRINGP (string) || area == ON_MODE_LINE)
28106 {
28107 /* Arrange to display the help by setting the global variables
28108 help_echo_string, help_echo_object, and help_echo_pos. */
28109 if (NILP (help))
28110 {
28111 if (STRINGP (string))
28112 help = Fget_text_property (pos, Qhelp_echo, string);
28113
28114 if (!NILP (help))
28115 {
28116 help_echo_string = help;
28117 XSETWINDOW (help_echo_window, w);
28118 help_echo_object = string;
28119 help_echo_pos = charpos;
28120 }
28121 else if (area == ON_MODE_LINE)
28122 {
28123 Lisp_Object default_help
28124 = buffer_local_value_1 (Qmode_line_default_help_echo,
28125 w->contents);
28126
28127 if (STRINGP (default_help))
28128 {
28129 help_echo_string = default_help;
28130 XSETWINDOW (help_echo_window, w);
28131 help_echo_object = Qnil;
28132 help_echo_pos = -1;
28133 }
28134 }
28135 }
28136
28137 #ifdef HAVE_WINDOW_SYSTEM
28138 /* Change the mouse pointer according to what is under it. */
28139 if (FRAME_WINDOW_P (f))
28140 {
28141 dpyinfo = FRAME_X_DISPLAY_INFO (f);
28142 if (STRINGP (string))
28143 {
28144 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28145
28146 if (NILP (pointer))
28147 pointer = Fget_text_property (pos, Qpointer, string);
28148
28149 /* Change the mouse pointer according to what is under X/Y. */
28150 if (NILP (pointer)
28151 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28152 {
28153 Lisp_Object map;
28154 map = Fget_text_property (pos, Qlocal_map, string);
28155 if (!KEYMAPP (map))
28156 map = Fget_text_property (pos, Qkeymap, string);
28157 if (!KEYMAPP (map))
28158 cursor = dpyinfo->vertical_scroll_bar_cursor;
28159 }
28160 }
28161 else
28162 /* Default mode-line pointer. */
28163 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28164 }
28165 #endif
28166 }
28167
28168 /* Change the mouse face according to what is under X/Y. */
28169 if (STRINGP (string))
28170 {
28171 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28172 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28173 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28174 && glyph)
28175 {
28176 Lisp_Object b, e;
28177
28178 struct glyph * tmp_glyph;
28179
28180 int gpos;
28181 int gseq_length;
28182 int total_pixel_width;
28183 ptrdiff_t begpos, endpos, ignore;
28184
28185 int vpos, hpos;
28186
28187 b = Fprevious_single_property_change (make_number (charpos + 1),
28188 Qmouse_face, string, Qnil);
28189 if (NILP (b))
28190 begpos = 0;
28191 else
28192 begpos = XINT (b);
28193
28194 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28195 if (NILP (e))
28196 endpos = SCHARS (string);
28197 else
28198 endpos = XINT (e);
28199
28200 /* Calculate the glyph position GPOS of GLYPH in the
28201 displayed string, relative to the beginning of the
28202 highlighted part of the string.
28203
28204 Note: GPOS is different from CHARPOS. CHARPOS is the
28205 position of GLYPH in the internal string object. A mode
28206 line string format has structures which are converted to
28207 a flattened string by the Emacs Lisp interpreter. The
28208 internal string is an element of those structures. The
28209 displayed string is the flattened string. */
28210 tmp_glyph = row_start_glyph;
28211 while (tmp_glyph < glyph
28212 && (!(EQ (tmp_glyph->object, glyph->object)
28213 && begpos <= tmp_glyph->charpos
28214 && tmp_glyph->charpos < endpos)))
28215 tmp_glyph++;
28216 gpos = glyph - tmp_glyph;
28217
28218 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28219 the highlighted part of the displayed string to which
28220 GLYPH belongs. Note: GSEQ_LENGTH is different from
28221 SCHARS (STRING), because the latter returns the length of
28222 the internal string. */
28223 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28224 tmp_glyph > glyph
28225 && (!(EQ (tmp_glyph->object, glyph->object)
28226 && begpos <= tmp_glyph->charpos
28227 && tmp_glyph->charpos < endpos));
28228 tmp_glyph--)
28229 ;
28230 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28231
28232 /* Calculate the total pixel width of all the glyphs between
28233 the beginning of the highlighted area and GLYPH. */
28234 total_pixel_width = 0;
28235 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28236 total_pixel_width += tmp_glyph->pixel_width;
28237
28238 /* Pre calculation of re-rendering position. Note: X is in
28239 column units here, after the call to mode_line_string or
28240 marginal_area_string. */
28241 hpos = x - gpos;
28242 vpos = (area == ON_MODE_LINE
28243 ? (w->current_matrix)->nrows - 1
28244 : 0);
28245
28246 /* If GLYPH's position is included in the region that is
28247 already drawn in mouse face, we have nothing to do. */
28248 if ( EQ (window, hlinfo->mouse_face_window)
28249 && (!row->reversed_p
28250 ? (hlinfo->mouse_face_beg_col <= hpos
28251 && hpos < hlinfo->mouse_face_end_col)
28252 /* In R2L rows we swap BEG and END, see below. */
28253 : (hlinfo->mouse_face_end_col <= hpos
28254 && hpos < hlinfo->mouse_face_beg_col))
28255 && hlinfo->mouse_face_beg_row == vpos )
28256 return;
28257
28258 if (clear_mouse_face (hlinfo))
28259 cursor = No_Cursor;
28260
28261 if (!row->reversed_p)
28262 {
28263 hlinfo->mouse_face_beg_col = hpos;
28264 hlinfo->mouse_face_beg_x = original_x_pixel
28265 - (total_pixel_width + dx);
28266 hlinfo->mouse_face_end_col = hpos + gseq_length;
28267 hlinfo->mouse_face_end_x = 0;
28268 }
28269 else
28270 {
28271 /* In R2L rows, show_mouse_face expects BEG and END
28272 coordinates to be swapped. */
28273 hlinfo->mouse_face_end_col = hpos;
28274 hlinfo->mouse_face_end_x = original_x_pixel
28275 - (total_pixel_width + dx);
28276 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28277 hlinfo->mouse_face_beg_x = 0;
28278 }
28279
28280 hlinfo->mouse_face_beg_row = vpos;
28281 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28282 hlinfo->mouse_face_past_end = 0;
28283 hlinfo->mouse_face_window = window;
28284
28285 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28286 charpos,
28287 0, 0, 0,
28288 &ignore,
28289 glyph->face_id,
28290 1);
28291 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28292
28293 if (NILP (pointer))
28294 pointer = Qhand;
28295 }
28296 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28297 clear_mouse_face (hlinfo);
28298 }
28299 #ifdef HAVE_WINDOW_SYSTEM
28300 if (FRAME_WINDOW_P (f))
28301 define_frame_cursor1 (f, cursor, pointer);
28302 #endif
28303 }
28304
28305
28306 /* EXPORT:
28307 Take proper action when the mouse has moved to position X, Y on
28308 frame F with regards to highlighting portions of display that have
28309 mouse-face properties. Also de-highlight portions of display where
28310 the mouse was before, set the mouse pointer shape as appropriate
28311 for the mouse coordinates, and activate help echo (tooltips).
28312 X and Y can be negative or out of range. */
28313
28314 void
28315 note_mouse_highlight (struct frame *f, int x, int y)
28316 {
28317 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28318 enum window_part part = ON_NOTHING;
28319 Lisp_Object window;
28320 struct window *w;
28321 Cursor cursor = No_Cursor;
28322 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28323 struct buffer *b;
28324
28325 /* When a menu is active, don't highlight because this looks odd. */
28326 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28327 if (popup_activated ())
28328 return;
28329 #endif
28330
28331 if (!f->glyphs_initialized_p
28332 || f->pointer_invisible)
28333 return;
28334
28335 hlinfo->mouse_face_mouse_x = x;
28336 hlinfo->mouse_face_mouse_y = y;
28337 hlinfo->mouse_face_mouse_frame = f;
28338
28339 if (hlinfo->mouse_face_defer)
28340 return;
28341
28342 /* Which window is that in? */
28343 window = window_from_coordinates (f, x, y, &part, 1);
28344
28345 /* If displaying active text in another window, clear that. */
28346 if (! EQ (window, hlinfo->mouse_face_window)
28347 /* Also clear if we move out of text area in same window. */
28348 || (!NILP (hlinfo->mouse_face_window)
28349 && !NILP (window)
28350 && part != ON_TEXT
28351 && part != ON_MODE_LINE
28352 && part != ON_HEADER_LINE))
28353 clear_mouse_face (hlinfo);
28354
28355 /* Not on a window -> return. */
28356 if (!WINDOWP (window))
28357 return;
28358
28359 /* Reset help_echo_string. It will get recomputed below. */
28360 help_echo_string = Qnil;
28361
28362 /* Convert to window-relative pixel coordinates. */
28363 w = XWINDOW (window);
28364 frame_to_window_pixel_xy (w, &x, &y);
28365
28366 #ifdef HAVE_WINDOW_SYSTEM
28367 /* Handle tool-bar window differently since it doesn't display a
28368 buffer. */
28369 if (EQ (window, f->tool_bar_window))
28370 {
28371 note_tool_bar_highlight (f, x, y);
28372 return;
28373 }
28374 #endif
28375
28376 /* Mouse is on the mode, header line or margin? */
28377 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28378 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28379 {
28380 note_mode_line_or_margin_highlight (window, x, y, part);
28381 return;
28382 }
28383
28384 #ifdef HAVE_WINDOW_SYSTEM
28385 if (part == ON_VERTICAL_BORDER)
28386 {
28387 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28388 help_echo_string = build_string ("drag-mouse-1: resize");
28389 }
28390 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28391 || part == ON_SCROLL_BAR)
28392 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28393 else
28394 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28395 #endif
28396
28397 /* Are we in a window whose display is up to date?
28398 And verify the buffer's text has not changed. */
28399 b = XBUFFER (w->contents);
28400 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28401 {
28402 int hpos, vpos, dx, dy, area = LAST_AREA;
28403 ptrdiff_t pos;
28404 struct glyph *glyph;
28405 Lisp_Object object;
28406 Lisp_Object mouse_face = Qnil, position;
28407 Lisp_Object *overlay_vec = NULL;
28408 ptrdiff_t i, noverlays;
28409 struct buffer *obuf;
28410 ptrdiff_t obegv, ozv;
28411 int same_region;
28412
28413 /* Find the glyph under X/Y. */
28414 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28415
28416 #ifdef HAVE_WINDOW_SYSTEM
28417 /* Look for :pointer property on image. */
28418 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28419 {
28420 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28421 if (img != NULL && IMAGEP (img->spec))
28422 {
28423 Lisp_Object image_map, hotspot;
28424 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28425 !NILP (image_map))
28426 && (hotspot = find_hot_spot (image_map,
28427 glyph->slice.img.x + dx,
28428 glyph->slice.img.y + dy),
28429 CONSP (hotspot))
28430 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28431 {
28432 Lisp_Object plist;
28433
28434 /* Could check XCAR (hotspot) to see if we enter/leave
28435 this hot-spot.
28436 If so, we could look for mouse-enter, mouse-leave
28437 properties in PLIST (and do something...). */
28438 hotspot = XCDR (hotspot);
28439 if (CONSP (hotspot)
28440 && (plist = XCAR (hotspot), CONSP (plist)))
28441 {
28442 pointer = Fplist_get (plist, Qpointer);
28443 if (NILP (pointer))
28444 pointer = Qhand;
28445 help_echo_string = Fplist_get (plist, Qhelp_echo);
28446 if (!NILP (help_echo_string))
28447 {
28448 help_echo_window = window;
28449 help_echo_object = glyph->object;
28450 help_echo_pos = glyph->charpos;
28451 }
28452 }
28453 }
28454 if (NILP (pointer))
28455 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28456 }
28457 }
28458 #endif /* HAVE_WINDOW_SYSTEM */
28459
28460 /* Clear mouse face if X/Y not over text. */
28461 if (glyph == NULL
28462 || area != TEXT_AREA
28463 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28464 /* Glyph's OBJECT is an integer for glyphs inserted by the
28465 display engine for its internal purposes, like truncation
28466 and continuation glyphs and blanks beyond the end of
28467 line's text on text terminals. If we are over such a
28468 glyph, we are not over any text. */
28469 || INTEGERP (glyph->object)
28470 /* R2L rows have a stretch glyph at their front, which
28471 stands for no text, whereas L2R rows have no glyphs at
28472 all beyond the end of text. Treat such stretch glyphs
28473 like we do with NULL glyphs in L2R rows. */
28474 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28475 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28476 && glyph->type == STRETCH_GLYPH
28477 && glyph->avoid_cursor_p))
28478 {
28479 if (clear_mouse_face (hlinfo))
28480 cursor = No_Cursor;
28481 #ifdef HAVE_WINDOW_SYSTEM
28482 if (FRAME_WINDOW_P (f) && NILP (pointer))
28483 {
28484 if (area != TEXT_AREA)
28485 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28486 else
28487 pointer = Vvoid_text_area_pointer;
28488 }
28489 #endif
28490 goto set_cursor;
28491 }
28492
28493 pos = glyph->charpos;
28494 object = glyph->object;
28495 if (!STRINGP (object) && !BUFFERP (object))
28496 goto set_cursor;
28497
28498 /* If we get an out-of-range value, return now; avoid an error. */
28499 if (BUFFERP (object) && pos > BUF_Z (b))
28500 goto set_cursor;
28501
28502 /* Make the window's buffer temporarily current for
28503 overlays_at and compute_char_face. */
28504 obuf = current_buffer;
28505 current_buffer = b;
28506 obegv = BEGV;
28507 ozv = ZV;
28508 BEGV = BEG;
28509 ZV = Z;
28510
28511 /* Is this char mouse-active or does it have help-echo? */
28512 position = make_number (pos);
28513
28514 if (BUFFERP (object))
28515 {
28516 /* Put all the overlays we want in a vector in overlay_vec. */
28517 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28518 /* Sort overlays into increasing priority order. */
28519 noverlays = sort_overlays (overlay_vec, noverlays, w);
28520 }
28521 else
28522 noverlays = 0;
28523
28524 if (NILP (Vmouse_highlight))
28525 {
28526 clear_mouse_face (hlinfo);
28527 goto check_help_echo;
28528 }
28529
28530 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28531
28532 if (same_region)
28533 cursor = No_Cursor;
28534
28535 /* Check mouse-face highlighting. */
28536 if (! same_region
28537 /* If there exists an overlay with mouse-face overlapping
28538 the one we are currently highlighting, we have to
28539 check if we enter the overlapping overlay, and then
28540 highlight only that. */
28541 || (OVERLAYP (hlinfo->mouse_face_overlay)
28542 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28543 {
28544 /* Find the highest priority overlay with a mouse-face. */
28545 Lisp_Object overlay = Qnil;
28546 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28547 {
28548 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28549 if (!NILP (mouse_face))
28550 overlay = overlay_vec[i];
28551 }
28552
28553 /* If we're highlighting the same overlay as before, there's
28554 no need to do that again. */
28555 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28556 goto check_help_echo;
28557 hlinfo->mouse_face_overlay = overlay;
28558
28559 /* Clear the display of the old active region, if any. */
28560 if (clear_mouse_face (hlinfo))
28561 cursor = No_Cursor;
28562
28563 /* If no overlay applies, get a text property. */
28564 if (NILP (overlay))
28565 mouse_face = Fget_text_property (position, Qmouse_face, object);
28566
28567 /* Next, compute the bounds of the mouse highlighting and
28568 display it. */
28569 if (!NILP (mouse_face) && STRINGP (object))
28570 {
28571 /* The mouse-highlighting comes from a display string
28572 with a mouse-face. */
28573 Lisp_Object s, e;
28574 ptrdiff_t ignore;
28575
28576 s = Fprevious_single_property_change
28577 (make_number (pos + 1), Qmouse_face, object, Qnil);
28578 e = Fnext_single_property_change
28579 (position, Qmouse_face, object, Qnil);
28580 if (NILP (s))
28581 s = make_number (0);
28582 if (NILP (e))
28583 e = make_number (SCHARS (object) - 1);
28584 mouse_face_from_string_pos (w, hlinfo, object,
28585 XINT (s), XINT (e));
28586 hlinfo->mouse_face_past_end = 0;
28587 hlinfo->mouse_face_window = window;
28588 hlinfo->mouse_face_face_id
28589 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28590 glyph->face_id, 1);
28591 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28592 cursor = No_Cursor;
28593 }
28594 else
28595 {
28596 /* The mouse-highlighting, if any, comes from an overlay
28597 or text property in the buffer. */
28598 Lisp_Object buffer IF_LINT (= Qnil);
28599 Lisp_Object disp_string IF_LINT (= Qnil);
28600
28601 if (STRINGP (object))
28602 {
28603 /* If we are on a display string with no mouse-face,
28604 check if the text under it has one. */
28605 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28606 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28607 pos = string_buffer_position (object, start);
28608 if (pos > 0)
28609 {
28610 mouse_face = get_char_property_and_overlay
28611 (make_number (pos), Qmouse_face, w->contents, &overlay);
28612 buffer = w->contents;
28613 disp_string = object;
28614 }
28615 }
28616 else
28617 {
28618 buffer = object;
28619 disp_string = Qnil;
28620 }
28621
28622 if (!NILP (mouse_face))
28623 {
28624 Lisp_Object before, after;
28625 Lisp_Object before_string, after_string;
28626 /* To correctly find the limits of mouse highlight
28627 in a bidi-reordered buffer, we must not use the
28628 optimization of limiting the search in
28629 previous-single-property-change and
28630 next-single-property-change, because
28631 rows_from_pos_range needs the real start and end
28632 positions to DTRT in this case. That's because
28633 the first row visible in a window does not
28634 necessarily display the character whose position
28635 is the smallest. */
28636 Lisp_Object lim1 =
28637 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28638 ? Fmarker_position (w->start)
28639 : Qnil;
28640 Lisp_Object lim2 =
28641 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28642 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28643 : Qnil;
28644
28645 if (NILP (overlay))
28646 {
28647 /* Handle the text property case. */
28648 before = Fprevious_single_property_change
28649 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28650 after = Fnext_single_property_change
28651 (make_number (pos), Qmouse_face, buffer, lim2);
28652 before_string = after_string = Qnil;
28653 }
28654 else
28655 {
28656 /* Handle the overlay case. */
28657 before = Foverlay_start (overlay);
28658 after = Foverlay_end (overlay);
28659 before_string = Foverlay_get (overlay, Qbefore_string);
28660 after_string = Foverlay_get (overlay, Qafter_string);
28661
28662 if (!STRINGP (before_string)) before_string = Qnil;
28663 if (!STRINGP (after_string)) after_string = Qnil;
28664 }
28665
28666 mouse_face_from_buffer_pos (window, hlinfo, pos,
28667 NILP (before)
28668 ? 1
28669 : XFASTINT (before),
28670 NILP (after)
28671 ? BUF_Z (XBUFFER (buffer))
28672 : XFASTINT (after),
28673 before_string, after_string,
28674 disp_string);
28675 cursor = No_Cursor;
28676 }
28677 }
28678 }
28679
28680 check_help_echo:
28681
28682 /* Look for a `help-echo' property. */
28683 if (NILP (help_echo_string)) {
28684 Lisp_Object help, overlay;
28685
28686 /* Check overlays first. */
28687 help = overlay = Qnil;
28688 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28689 {
28690 overlay = overlay_vec[i];
28691 help = Foverlay_get (overlay, Qhelp_echo);
28692 }
28693
28694 if (!NILP (help))
28695 {
28696 help_echo_string = help;
28697 help_echo_window = window;
28698 help_echo_object = overlay;
28699 help_echo_pos = pos;
28700 }
28701 else
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 help = Fget_text_property (make_number (charpos),
28712 Qhelp_echo, obj);
28713 if (NILP (help))
28714 {
28715 /* If the string itself doesn't specify a help-echo,
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 {
28723 help = Fget_char_property (make_number (p),
28724 Qhelp_echo, w->contents);
28725 if (!NILP (help))
28726 {
28727 charpos = p;
28728 obj = w->contents;
28729 }
28730 }
28731 }
28732 }
28733 else if (BUFFERP (obj)
28734 && charpos >= BEGV
28735 && charpos < ZV)
28736 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28737 obj);
28738
28739 if (!NILP (help))
28740 {
28741 help_echo_string = help;
28742 help_echo_window = window;
28743 help_echo_object = obj;
28744 help_echo_pos = charpos;
28745 }
28746 }
28747 }
28748
28749 #ifdef HAVE_WINDOW_SYSTEM
28750 /* Look for a `pointer' property. */
28751 if (FRAME_WINDOW_P (f) && NILP (pointer))
28752 {
28753 /* Check overlays first. */
28754 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28755 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28756
28757 if (NILP (pointer))
28758 {
28759 Lisp_Object obj = glyph->object;
28760 ptrdiff_t charpos = glyph->charpos;
28761
28762 /* Try text properties. */
28763 if (STRINGP (obj)
28764 && charpos >= 0
28765 && charpos < SCHARS (obj))
28766 {
28767 pointer = Fget_text_property (make_number (charpos),
28768 Qpointer, obj);
28769 if (NILP (pointer))
28770 {
28771 /* If the string itself doesn't specify a pointer,
28772 see if the buffer text ``under'' it does. */
28773 struct glyph_row *r
28774 = MATRIX_ROW (w->current_matrix, vpos);
28775 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28776 ptrdiff_t p = string_buffer_position (obj, start);
28777 if (p > 0)
28778 pointer = Fget_char_property (make_number (p),
28779 Qpointer, w->contents);
28780 }
28781 }
28782 else if (BUFFERP (obj)
28783 && charpos >= BEGV
28784 && charpos < ZV)
28785 pointer = Fget_text_property (make_number (charpos),
28786 Qpointer, obj);
28787 }
28788 }
28789 #endif /* HAVE_WINDOW_SYSTEM */
28790
28791 BEGV = obegv;
28792 ZV = ozv;
28793 current_buffer = obuf;
28794 }
28795
28796 set_cursor:
28797
28798 #ifdef HAVE_WINDOW_SYSTEM
28799 if (FRAME_WINDOW_P (f))
28800 define_frame_cursor1 (f, cursor, pointer);
28801 #else
28802 /* This is here to prevent a compiler error, about "label at end of
28803 compound statement". */
28804 return;
28805 #endif
28806 }
28807
28808
28809 /* EXPORT for RIF:
28810 Clear any mouse-face on window W. This function is part of the
28811 redisplay interface, and is called from try_window_id and similar
28812 functions to ensure the mouse-highlight is off. */
28813
28814 void
28815 x_clear_window_mouse_face (struct window *w)
28816 {
28817 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28818 Lisp_Object window;
28819
28820 block_input ();
28821 XSETWINDOW (window, w);
28822 if (EQ (window, hlinfo->mouse_face_window))
28823 clear_mouse_face (hlinfo);
28824 unblock_input ();
28825 }
28826
28827
28828 /* EXPORT:
28829 Just discard the mouse face information for frame F, if any.
28830 This is used when the size of F is changed. */
28831
28832 void
28833 cancel_mouse_face (struct frame *f)
28834 {
28835 Lisp_Object window;
28836 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28837
28838 window = hlinfo->mouse_face_window;
28839 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28840 reset_mouse_highlight (hlinfo);
28841 }
28842
28843
28844 \f
28845 /***********************************************************************
28846 Exposure Events
28847 ***********************************************************************/
28848
28849 #ifdef HAVE_WINDOW_SYSTEM
28850
28851 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28852 which intersects rectangle R. R is in window-relative coordinates. */
28853
28854 static void
28855 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28856 enum glyph_row_area area)
28857 {
28858 struct glyph *first = row->glyphs[area];
28859 struct glyph *end = row->glyphs[area] + row->used[area];
28860 struct glyph *last;
28861 int first_x, start_x, x;
28862
28863 if (area == TEXT_AREA && row->fill_line_p)
28864 /* If row extends face to end of line write the whole line. */
28865 draw_glyphs (w, 0, row, area,
28866 0, row->used[area],
28867 DRAW_NORMAL_TEXT, 0);
28868 else
28869 {
28870 /* Set START_X to the window-relative start position for drawing glyphs of
28871 AREA. The first glyph of the text area can be partially visible.
28872 The first glyphs of other areas cannot. */
28873 start_x = window_box_left_offset (w, area);
28874 x = start_x;
28875 if (area == TEXT_AREA)
28876 x += row->x;
28877
28878 /* Find the first glyph that must be redrawn. */
28879 while (first < end
28880 && x + first->pixel_width < r->x)
28881 {
28882 x += first->pixel_width;
28883 ++first;
28884 }
28885
28886 /* Find the last one. */
28887 last = first;
28888 first_x = x;
28889 while (last < end
28890 && x < r->x + r->width)
28891 {
28892 x += last->pixel_width;
28893 ++last;
28894 }
28895
28896 /* Repaint. */
28897 if (last > first)
28898 draw_glyphs (w, first_x - start_x, row, area,
28899 first - row->glyphs[area], last - row->glyphs[area],
28900 DRAW_NORMAL_TEXT, 0);
28901 }
28902 }
28903
28904
28905 /* Redraw the parts of the glyph row ROW on window W intersecting
28906 rectangle R. R is in window-relative coordinates. Value is
28907 non-zero if mouse-face was overwritten. */
28908
28909 static int
28910 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28911 {
28912 eassert (row->enabled_p);
28913
28914 if (row->mode_line_p || w->pseudo_window_p)
28915 draw_glyphs (w, 0, row, TEXT_AREA,
28916 0, row->used[TEXT_AREA],
28917 DRAW_NORMAL_TEXT, 0);
28918 else
28919 {
28920 if (row->used[LEFT_MARGIN_AREA])
28921 expose_area (w, row, r, LEFT_MARGIN_AREA);
28922 if (row->used[TEXT_AREA])
28923 expose_area (w, row, r, TEXT_AREA);
28924 if (row->used[RIGHT_MARGIN_AREA])
28925 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28926 draw_row_fringe_bitmaps (w, row);
28927 }
28928
28929 return row->mouse_face_p;
28930 }
28931
28932
28933 /* Redraw those parts of glyphs rows during expose event handling that
28934 overlap other rows. Redrawing of an exposed line writes over parts
28935 of lines overlapping that exposed line; this function fixes that.
28936
28937 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28938 row in W's current matrix that is exposed and overlaps other rows.
28939 LAST_OVERLAPPING_ROW is the last such row. */
28940
28941 static void
28942 expose_overlaps (struct window *w,
28943 struct glyph_row *first_overlapping_row,
28944 struct glyph_row *last_overlapping_row,
28945 XRectangle *r)
28946 {
28947 struct glyph_row *row;
28948
28949 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28950 if (row->overlapping_p)
28951 {
28952 eassert (row->enabled_p && !row->mode_line_p);
28953
28954 row->clip = r;
28955 if (row->used[LEFT_MARGIN_AREA])
28956 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28957
28958 if (row->used[TEXT_AREA])
28959 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28960
28961 if (row->used[RIGHT_MARGIN_AREA])
28962 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28963 row->clip = NULL;
28964 }
28965 }
28966
28967
28968 /* Return non-zero if W's cursor intersects rectangle R. */
28969
28970 static int
28971 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28972 {
28973 XRectangle cr, result;
28974 struct glyph *cursor_glyph;
28975 struct glyph_row *row;
28976
28977 if (w->phys_cursor.vpos >= 0
28978 && w->phys_cursor.vpos < w->current_matrix->nrows
28979 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28980 row->enabled_p)
28981 && row->cursor_in_fringe_p)
28982 {
28983 /* Cursor is in the fringe. */
28984 cr.x = window_box_right_offset (w,
28985 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28986 ? RIGHT_MARGIN_AREA
28987 : TEXT_AREA));
28988 cr.y = row->y;
28989 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28990 cr.height = row->height;
28991 return x_intersect_rectangles (&cr, r, &result);
28992 }
28993
28994 cursor_glyph = get_phys_cursor_glyph (w);
28995 if (cursor_glyph)
28996 {
28997 /* r is relative to W's box, but w->phys_cursor.x is relative
28998 to left edge of W's TEXT area. Adjust it. */
28999 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29000 cr.y = w->phys_cursor.y;
29001 cr.width = cursor_glyph->pixel_width;
29002 cr.height = w->phys_cursor_height;
29003 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29004 I assume the effect is the same -- and this is portable. */
29005 return x_intersect_rectangles (&cr, r, &result);
29006 }
29007 /* If we don't understand the format, pretend we're not in the hot-spot. */
29008 return 0;
29009 }
29010
29011
29012 /* EXPORT:
29013 Draw a vertical window border to the right of window W if W doesn't
29014 have vertical scroll bars. */
29015
29016 void
29017 x_draw_vertical_border (struct window *w)
29018 {
29019 struct frame *f = XFRAME (WINDOW_FRAME (w));
29020
29021 /* We could do better, if we knew what type of scroll-bar the adjacent
29022 windows (on either side) have... But we don't :-(
29023 However, I think this works ok. ++KFS 2003-04-25 */
29024
29025 /* Redraw borders between horizontally adjacent windows. Don't
29026 do it for frames with vertical scroll bars because either the
29027 right scroll bar of a window, or the left scroll bar of its
29028 neighbor will suffice as a border. */
29029 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
29030 return;
29031
29032 /* Note: It is necessary to redraw both the left and the right
29033 borders, for when only this single window W is being
29034 redisplayed. */
29035 if (!WINDOW_RIGHTMOST_P (w)
29036 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29037 {
29038 int x0, x1, y0, y1;
29039
29040 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
29041 y1 -= 1;
29042
29043 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29044 x1 -= 1;
29045
29046 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29047 }
29048 if (!WINDOW_LEFTMOST_P (w)
29049 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29050 {
29051 int x0, x1, y0, y1;
29052
29053 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
29054 y1 -= 1;
29055
29056 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29057 x0 -= 1;
29058
29059 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29060 }
29061 }
29062
29063
29064 /* Redraw the part of window W intersection rectangle FR. Pixel
29065 coordinates in FR are frame-relative. Call this function with
29066 input blocked. Value is non-zero if the exposure overwrites
29067 mouse-face. */
29068
29069 static int
29070 expose_window (struct window *w, XRectangle *fr)
29071 {
29072 struct frame *f = XFRAME (w->frame);
29073 XRectangle wr, r;
29074 int mouse_face_overwritten_p = 0;
29075
29076 /* If window is not yet fully initialized, do nothing. This can
29077 happen when toolkit scroll bars are used and a window is split.
29078 Reconfiguring the scroll bar will generate an expose for a newly
29079 created window. */
29080 if (w->current_matrix == NULL)
29081 return 0;
29082
29083 /* When we're currently updating the window, display and current
29084 matrix usually don't agree. Arrange for a thorough display
29085 later. */
29086 if (w->must_be_updated_p)
29087 {
29088 SET_FRAME_GARBAGED (f);
29089 return 0;
29090 }
29091
29092 /* Frame-relative pixel rectangle of W. */
29093 wr.x = WINDOW_LEFT_EDGE_X (w);
29094 wr.y = WINDOW_TOP_EDGE_Y (w);
29095 wr.width = WINDOW_TOTAL_WIDTH (w);
29096 wr.height = WINDOW_TOTAL_HEIGHT (w);
29097
29098 if (x_intersect_rectangles (fr, &wr, &r))
29099 {
29100 int yb = window_text_bottom_y (w);
29101 struct glyph_row *row;
29102 int cursor_cleared_p, phys_cursor_on_p;
29103 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29104
29105 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29106 r.x, r.y, r.width, r.height));
29107
29108 /* Convert to window coordinates. */
29109 r.x -= WINDOW_LEFT_EDGE_X (w);
29110 r.y -= WINDOW_TOP_EDGE_Y (w);
29111
29112 /* Turn off the cursor. */
29113 if (!w->pseudo_window_p
29114 && phys_cursor_in_rect_p (w, &r))
29115 {
29116 x_clear_cursor (w);
29117 cursor_cleared_p = 1;
29118 }
29119 else
29120 cursor_cleared_p = 0;
29121
29122 /* If the row containing the cursor extends face to end of line,
29123 then expose_area might overwrite the cursor outside the
29124 rectangle and thus notice_overwritten_cursor might clear
29125 w->phys_cursor_on_p. We remember the original value and
29126 check later if it is changed. */
29127 phys_cursor_on_p = w->phys_cursor_on_p;
29128
29129 /* Update lines intersecting rectangle R. */
29130 first_overlapping_row = last_overlapping_row = NULL;
29131 for (row = w->current_matrix->rows;
29132 row->enabled_p;
29133 ++row)
29134 {
29135 int y0 = row->y;
29136 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29137
29138 if ((y0 >= r.y && y0 < r.y + r.height)
29139 || (y1 > r.y && y1 < r.y + r.height)
29140 || (r.y >= y0 && r.y < y1)
29141 || (r.y + r.height > y0 && r.y + r.height < y1))
29142 {
29143 /* A header line may be overlapping, but there is no need
29144 to fix overlapping areas for them. KFS 2005-02-12 */
29145 if (row->overlapping_p && !row->mode_line_p)
29146 {
29147 if (first_overlapping_row == NULL)
29148 first_overlapping_row = row;
29149 last_overlapping_row = row;
29150 }
29151
29152 row->clip = fr;
29153 if (expose_line (w, row, &r))
29154 mouse_face_overwritten_p = 1;
29155 row->clip = NULL;
29156 }
29157 else if (row->overlapping_p)
29158 {
29159 /* We must redraw a row overlapping the exposed area. */
29160 if (y0 < r.y
29161 ? y0 + row->phys_height > r.y
29162 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29163 {
29164 if (first_overlapping_row == NULL)
29165 first_overlapping_row = row;
29166 last_overlapping_row = row;
29167 }
29168 }
29169
29170 if (y1 >= yb)
29171 break;
29172 }
29173
29174 /* Display the mode line if there is one. */
29175 if (WINDOW_WANTS_MODELINE_P (w)
29176 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29177 row->enabled_p)
29178 && row->y < r.y + r.height)
29179 {
29180 if (expose_line (w, row, &r))
29181 mouse_face_overwritten_p = 1;
29182 }
29183
29184 if (!w->pseudo_window_p)
29185 {
29186 /* Fix the display of overlapping rows. */
29187 if (first_overlapping_row)
29188 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29189 fr);
29190
29191 /* Draw border between windows. */
29192 x_draw_vertical_border (w);
29193
29194 /* Turn the cursor on again. */
29195 if (cursor_cleared_p
29196 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29197 update_window_cursor (w, 1);
29198 }
29199 }
29200
29201 return mouse_face_overwritten_p;
29202 }
29203
29204
29205
29206 /* Redraw (parts) of all windows in the window tree rooted at W that
29207 intersect R. R contains frame pixel coordinates. Value is
29208 non-zero if the exposure overwrites mouse-face. */
29209
29210 static int
29211 expose_window_tree (struct window *w, XRectangle *r)
29212 {
29213 struct frame *f = XFRAME (w->frame);
29214 int mouse_face_overwritten_p = 0;
29215
29216 while (w && !FRAME_GARBAGED_P (f))
29217 {
29218 if (WINDOWP (w->contents))
29219 mouse_face_overwritten_p
29220 |= expose_window_tree (XWINDOW (w->contents), r);
29221 else
29222 mouse_face_overwritten_p |= expose_window (w, r);
29223
29224 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29225 }
29226
29227 return mouse_face_overwritten_p;
29228 }
29229
29230
29231 /* EXPORT:
29232 Redisplay an exposed area of frame F. X and Y are the upper-left
29233 corner of the exposed rectangle. W and H are width and height of
29234 the exposed area. All are pixel values. W or H zero means redraw
29235 the entire frame. */
29236
29237 void
29238 expose_frame (struct frame *f, int x, int y, int w, int h)
29239 {
29240 XRectangle r;
29241 int mouse_face_overwritten_p = 0;
29242
29243 TRACE ((stderr, "expose_frame "));
29244
29245 /* No need to redraw if frame will be redrawn soon. */
29246 if (FRAME_GARBAGED_P (f))
29247 {
29248 TRACE ((stderr, " garbaged\n"));
29249 return;
29250 }
29251
29252 /* If basic faces haven't been realized yet, there is no point in
29253 trying to redraw anything. This can happen when we get an expose
29254 event while Emacs is starting, e.g. by moving another window. */
29255 if (FRAME_FACE_CACHE (f) == NULL
29256 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29257 {
29258 TRACE ((stderr, " no faces\n"));
29259 return;
29260 }
29261
29262 if (w == 0 || h == 0)
29263 {
29264 r.x = r.y = 0;
29265 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29266 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29267 }
29268 else
29269 {
29270 r.x = x;
29271 r.y = y;
29272 r.width = w;
29273 r.height = h;
29274 }
29275
29276 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29277 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29278
29279 if (WINDOWP (f->tool_bar_window))
29280 mouse_face_overwritten_p
29281 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29282
29283 #ifdef HAVE_X_WINDOWS
29284 #ifndef MSDOS
29285 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29286 if (WINDOWP (f->menu_bar_window))
29287 mouse_face_overwritten_p
29288 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29289 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29290 #endif
29291 #endif
29292
29293 /* Some window managers support a focus-follows-mouse style with
29294 delayed raising of frames. Imagine a partially obscured frame,
29295 and moving the mouse into partially obscured mouse-face on that
29296 frame. The visible part of the mouse-face will be highlighted,
29297 then the WM raises the obscured frame. With at least one WM, KDE
29298 2.1, Emacs is not getting any event for the raising of the frame
29299 (even tried with SubstructureRedirectMask), only Expose events.
29300 These expose events will draw text normally, i.e. not
29301 highlighted. Which means we must redo the highlight here.
29302 Subsume it under ``we love X''. --gerd 2001-08-15 */
29303 /* Included in Windows version because Windows most likely does not
29304 do the right thing if any third party tool offers
29305 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29306 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29307 {
29308 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29309 if (f == hlinfo->mouse_face_mouse_frame)
29310 {
29311 int mouse_x = hlinfo->mouse_face_mouse_x;
29312 int mouse_y = hlinfo->mouse_face_mouse_y;
29313 clear_mouse_face (hlinfo);
29314 note_mouse_highlight (f, mouse_x, mouse_y);
29315 }
29316 }
29317 }
29318
29319
29320 /* EXPORT:
29321 Determine the intersection of two rectangles R1 and R2. Return
29322 the intersection in *RESULT. Value is non-zero if RESULT is not
29323 empty. */
29324
29325 int
29326 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29327 {
29328 XRectangle *left, *right;
29329 XRectangle *upper, *lower;
29330 int intersection_p = 0;
29331
29332 /* Rearrange so that R1 is the left-most rectangle. */
29333 if (r1->x < r2->x)
29334 left = r1, right = r2;
29335 else
29336 left = r2, right = r1;
29337
29338 /* X0 of the intersection is right.x0, if this is inside R1,
29339 otherwise there is no intersection. */
29340 if (right->x <= left->x + left->width)
29341 {
29342 result->x = right->x;
29343
29344 /* The right end of the intersection is the minimum of
29345 the right ends of left and right. */
29346 result->width = (min (left->x + left->width, right->x + right->width)
29347 - result->x);
29348
29349 /* Same game for Y. */
29350 if (r1->y < r2->y)
29351 upper = r1, lower = r2;
29352 else
29353 upper = r2, lower = r1;
29354
29355 /* The upper end of the intersection is lower.y0, if this is inside
29356 of upper. Otherwise, there is no intersection. */
29357 if (lower->y <= upper->y + upper->height)
29358 {
29359 result->y = lower->y;
29360
29361 /* The lower end of the intersection is the minimum of the lower
29362 ends of upper and lower. */
29363 result->height = (min (lower->y + lower->height,
29364 upper->y + upper->height)
29365 - result->y);
29366 intersection_p = 1;
29367 }
29368 }
29369
29370 return intersection_p;
29371 }
29372
29373 #endif /* HAVE_WINDOW_SYSTEM */
29374
29375 \f
29376 /***********************************************************************
29377 Initialization
29378 ***********************************************************************/
29379
29380 void
29381 syms_of_xdisp (void)
29382 {
29383 Vwith_echo_area_save_vector = Qnil;
29384 staticpro (&Vwith_echo_area_save_vector);
29385
29386 Vmessage_stack = Qnil;
29387 staticpro (&Vmessage_stack);
29388
29389 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29390 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29391
29392 message_dolog_marker1 = Fmake_marker ();
29393 staticpro (&message_dolog_marker1);
29394 message_dolog_marker2 = Fmake_marker ();
29395 staticpro (&message_dolog_marker2);
29396 message_dolog_marker3 = Fmake_marker ();
29397 staticpro (&message_dolog_marker3);
29398
29399 #ifdef GLYPH_DEBUG
29400 defsubr (&Sdump_frame_glyph_matrix);
29401 defsubr (&Sdump_glyph_matrix);
29402 defsubr (&Sdump_glyph_row);
29403 defsubr (&Sdump_tool_bar_row);
29404 defsubr (&Strace_redisplay);
29405 defsubr (&Strace_to_stderr);
29406 #endif
29407 #ifdef HAVE_WINDOW_SYSTEM
29408 defsubr (&Stool_bar_lines_needed);
29409 defsubr (&Slookup_image_map);
29410 #endif
29411 defsubr (&Sline_pixel_height);
29412 defsubr (&Sformat_mode_line);
29413 defsubr (&Sinvisible_p);
29414 defsubr (&Scurrent_bidi_paragraph_direction);
29415 defsubr (&Smove_point_visually);
29416
29417 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29418 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29419 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29420 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29421 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29422 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29423 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29424 DEFSYM (Qeval, "eval");
29425 DEFSYM (QCdata, ":data");
29426 DEFSYM (Qdisplay, "display");
29427 DEFSYM (Qspace_width, "space-width");
29428 DEFSYM (Qraise, "raise");
29429 DEFSYM (Qslice, "slice");
29430 DEFSYM (Qspace, "space");
29431 DEFSYM (Qmargin, "margin");
29432 DEFSYM (Qpointer, "pointer");
29433 DEFSYM (Qleft_margin, "left-margin");
29434 DEFSYM (Qright_margin, "right-margin");
29435 DEFSYM (Qcenter, "center");
29436 DEFSYM (Qline_height, "line-height");
29437 DEFSYM (QCalign_to, ":align-to");
29438 DEFSYM (QCrelative_width, ":relative-width");
29439 DEFSYM (QCrelative_height, ":relative-height");
29440 DEFSYM (QCeval, ":eval");
29441 DEFSYM (QCpropertize, ":propertize");
29442 DEFSYM (QCfile, ":file");
29443 DEFSYM (Qfontified, "fontified");
29444 DEFSYM (Qfontification_functions, "fontification-functions");
29445 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29446 DEFSYM (Qescape_glyph, "escape-glyph");
29447 DEFSYM (Qnobreak_space, "nobreak-space");
29448 DEFSYM (Qimage, "image");
29449 DEFSYM (Qtext, "text");
29450 DEFSYM (Qboth, "both");
29451 DEFSYM (Qboth_horiz, "both-horiz");
29452 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29453 DEFSYM (QCmap, ":map");
29454 DEFSYM (QCpointer, ":pointer");
29455 DEFSYM (Qrect, "rect");
29456 DEFSYM (Qcircle, "circle");
29457 DEFSYM (Qpoly, "poly");
29458 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29459 DEFSYM (Qgrow_only, "grow-only");
29460 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29461 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29462 DEFSYM (Qposition, "position");
29463 DEFSYM (Qbuffer_position, "buffer-position");
29464 DEFSYM (Qobject, "object");
29465 DEFSYM (Qbar, "bar");
29466 DEFSYM (Qhbar, "hbar");
29467 DEFSYM (Qbox, "box");
29468 DEFSYM (Qhollow, "hollow");
29469 DEFSYM (Qhand, "hand");
29470 DEFSYM (Qarrow, "arrow");
29471 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29472
29473 list_of_error = list1 (list2 (intern_c_string ("error"),
29474 intern_c_string ("void-variable")));
29475 staticpro (&list_of_error);
29476
29477 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29478 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29479 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29480 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29481
29482 echo_buffer[0] = echo_buffer[1] = Qnil;
29483 staticpro (&echo_buffer[0]);
29484 staticpro (&echo_buffer[1]);
29485
29486 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29487 staticpro (&echo_area_buffer[0]);
29488 staticpro (&echo_area_buffer[1]);
29489
29490 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29491 staticpro (&Vmessages_buffer_name);
29492
29493 mode_line_proptrans_alist = Qnil;
29494 staticpro (&mode_line_proptrans_alist);
29495 mode_line_string_list = Qnil;
29496 staticpro (&mode_line_string_list);
29497 mode_line_string_face = Qnil;
29498 staticpro (&mode_line_string_face);
29499 mode_line_string_face_prop = Qnil;
29500 staticpro (&mode_line_string_face_prop);
29501 Vmode_line_unwind_vector = Qnil;
29502 staticpro (&Vmode_line_unwind_vector);
29503
29504 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29505
29506 help_echo_string = Qnil;
29507 staticpro (&help_echo_string);
29508 help_echo_object = Qnil;
29509 staticpro (&help_echo_object);
29510 help_echo_window = Qnil;
29511 staticpro (&help_echo_window);
29512 previous_help_echo_string = Qnil;
29513 staticpro (&previous_help_echo_string);
29514 help_echo_pos = -1;
29515
29516 DEFSYM (Qright_to_left, "right-to-left");
29517 DEFSYM (Qleft_to_right, "left-to-right");
29518
29519 #ifdef HAVE_WINDOW_SYSTEM
29520 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29521 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29522 For example, if a block cursor is over a tab, it will be drawn as
29523 wide as that tab on the display. */);
29524 x_stretch_cursor_p = 0;
29525 #endif
29526
29527 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29528 doc: /* Non-nil means highlight trailing whitespace.
29529 The face used for trailing whitespace is `trailing-whitespace'. */);
29530 Vshow_trailing_whitespace = Qnil;
29531
29532 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29533 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29534 If the value is t, Emacs highlights non-ASCII chars which have the
29535 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29536 or `escape-glyph' face respectively.
29537
29538 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29539 U+2011 (non-breaking hyphen) are affected.
29540
29541 Any other non-nil value means to display these characters as a escape
29542 glyph followed by an ordinary space or hyphen.
29543
29544 A value of nil means no special handling of these characters. */);
29545 Vnobreak_char_display = Qt;
29546
29547 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29548 doc: /* The pointer shape to show in void text areas.
29549 A value of nil means to show the text pointer. Other options are `arrow',
29550 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29551 Vvoid_text_area_pointer = Qarrow;
29552
29553 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29554 doc: /* Non-nil means don't actually do any redisplay.
29555 This is used for internal purposes. */);
29556 Vinhibit_redisplay = Qnil;
29557
29558 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29559 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29560 Vglobal_mode_string = Qnil;
29561
29562 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29563 doc: /* Marker for where to display an arrow on top of the buffer text.
29564 This must be the beginning of a line in order to work.
29565 See also `overlay-arrow-string'. */);
29566 Voverlay_arrow_position = Qnil;
29567
29568 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29569 doc: /* String to display as an arrow in non-window frames.
29570 See also `overlay-arrow-position'. */);
29571 Voverlay_arrow_string = build_pure_c_string ("=>");
29572
29573 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29574 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29575 The symbols on this list are examined during redisplay to determine
29576 where to display overlay arrows. */);
29577 Voverlay_arrow_variable_list
29578 = list1 (intern_c_string ("overlay-arrow-position"));
29579
29580 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29581 doc: /* The number of lines to try scrolling a window by when point moves out.
29582 If that fails to bring point back on frame, point is centered instead.
29583 If this is zero, point is always centered after it moves off frame.
29584 If you want scrolling to always be a line at a time, you should set
29585 `scroll-conservatively' to a large value rather than set this to 1. */);
29586
29587 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29588 doc: /* Scroll up to this many lines, to bring point back on screen.
29589 If point moves off-screen, redisplay will scroll by up to
29590 `scroll-conservatively' lines in order to bring point just barely
29591 onto the screen again. If that cannot be done, then redisplay
29592 recenters point as usual.
29593
29594 If the value is greater than 100, redisplay will never recenter point,
29595 but will always scroll just enough text to bring point into view, even
29596 if you move far away.
29597
29598 A value of zero means always recenter point if it moves off screen. */);
29599 scroll_conservatively = 0;
29600
29601 DEFVAR_INT ("scroll-margin", scroll_margin,
29602 doc: /* Number of lines of margin at the top and bottom of a window.
29603 Recenter the window whenever point gets within this many lines
29604 of the top or bottom of the window. */);
29605 scroll_margin = 0;
29606
29607 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29608 doc: /* Pixels per inch value for non-window system displays.
29609 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29610 Vdisplay_pixels_per_inch = make_float (72.0);
29611
29612 #ifdef GLYPH_DEBUG
29613 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29614 #endif
29615
29616 DEFVAR_LISP ("truncate-partial-width-windows",
29617 Vtruncate_partial_width_windows,
29618 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29619 For an integer value, truncate lines in each window narrower than the
29620 full frame width, provided the window width is less than that integer;
29621 otherwise, respect the value of `truncate-lines'.
29622
29623 For any other non-nil value, truncate lines in all windows that do
29624 not span the full frame width.
29625
29626 A value of nil means to respect the value of `truncate-lines'.
29627
29628 If `word-wrap' is enabled, you might want to reduce this. */);
29629 Vtruncate_partial_width_windows = make_number (50);
29630
29631 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29632 doc: /* Maximum buffer size for which line number should be displayed.
29633 If the buffer is bigger than this, the line number does not appear
29634 in the mode line. A value of nil means no limit. */);
29635 Vline_number_display_limit = Qnil;
29636
29637 DEFVAR_INT ("line-number-display-limit-width",
29638 line_number_display_limit_width,
29639 doc: /* Maximum line width (in characters) for line number display.
29640 If the average length of the lines near point is bigger than this, then the
29641 line number may be omitted from the mode line. */);
29642 line_number_display_limit_width = 200;
29643
29644 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29645 doc: /* Non-nil means highlight region even in nonselected windows. */);
29646 highlight_nonselected_windows = 0;
29647
29648 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29649 doc: /* Non-nil if more than one frame is visible on this display.
29650 Minibuffer-only frames don't count, but iconified frames do.
29651 This variable is not guaranteed to be accurate except while processing
29652 `frame-title-format' and `icon-title-format'. */);
29653
29654 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29655 doc: /* Template for displaying the title bar of visible frames.
29656 \(Assuming the window manager supports this feature.)
29657
29658 This variable has the same structure as `mode-line-format', except that
29659 the %c and %l constructs are ignored. It is used only on frames for
29660 which no explicit name has been set \(see `modify-frame-parameters'). */);
29661
29662 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29663 doc: /* Template for displaying the title bar of an iconified frame.
29664 \(Assuming the window manager supports this feature.)
29665 This variable has the same structure as `mode-line-format' (which see),
29666 and is used only on frames for which no explicit name has been set
29667 \(see `modify-frame-parameters'). */);
29668 Vicon_title_format
29669 = Vframe_title_format
29670 = listn (CONSTYPE_PURE, 3,
29671 intern_c_string ("multiple-frames"),
29672 build_pure_c_string ("%b"),
29673 listn (CONSTYPE_PURE, 4,
29674 empty_unibyte_string,
29675 intern_c_string ("invocation-name"),
29676 build_pure_c_string ("@"),
29677 intern_c_string ("system-name")));
29678
29679 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29680 doc: /* Maximum number of lines to keep in the message log buffer.
29681 If nil, disable message logging. If t, log messages but don't truncate
29682 the buffer when it becomes large. */);
29683 Vmessage_log_max = make_number (1000);
29684
29685 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29686 doc: /* Functions called before redisplay, if window sizes have changed.
29687 The value should be a list of functions that take one argument.
29688 Just before redisplay, for each frame, if any of its windows have changed
29689 size since the last redisplay, or have been split or deleted,
29690 all the functions in the list are called, with the frame as argument. */);
29691 Vwindow_size_change_functions = Qnil;
29692
29693 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29694 doc: /* List of functions to call before redisplaying a window with scrolling.
29695 Each function is called with two arguments, the window and its new
29696 display-start position. Note that these functions are also called by
29697 `set-window-buffer'. Also note that the value of `window-end' is not
29698 valid when these functions are called.
29699
29700 Warning: Do not use this feature to alter the way the window
29701 is scrolled. It is not designed for that, and such use probably won't
29702 work. */);
29703 Vwindow_scroll_functions = Qnil;
29704
29705 DEFVAR_LISP ("window-text-change-functions",
29706 Vwindow_text_change_functions,
29707 doc: /* Functions to call in redisplay when text in the window might change. */);
29708 Vwindow_text_change_functions = Qnil;
29709
29710 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29711 doc: /* Functions called when redisplay of a window reaches the end trigger.
29712 Each function is called with two arguments, the window and the end trigger value.
29713 See `set-window-redisplay-end-trigger'. */);
29714 Vredisplay_end_trigger_functions = Qnil;
29715
29716 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29717 doc: /* Non-nil means autoselect window with mouse pointer.
29718 If nil, do not autoselect windows.
29719 A positive number means delay autoselection by that many seconds: a
29720 window is autoselected only after the mouse has remained in that
29721 window for the duration of the delay.
29722 A negative number has a similar effect, but causes windows to be
29723 autoselected only after the mouse has stopped moving. \(Because of
29724 the way Emacs compares mouse events, you will occasionally wait twice
29725 that time before the window gets selected.\)
29726 Any other value means to autoselect window instantaneously when the
29727 mouse pointer enters it.
29728
29729 Autoselection selects the minibuffer only if it is active, and never
29730 unselects the minibuffer if it is active.
29731
29732 When customizing this variable make sure that the actual value of
29733 `focus-follows-mouse' matches the behavior of your window manager. */);
29734 Vmouse_autoselect_window = Qnil;
29735
29736 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29737 doc: /* Non-nil means automatically resize tool-bars.
29738 This dynamically changes the tool-bar's height to the minimum height
29739 that is needed to make all tool-bar items visible.
29740 If value is `grow-only', the tool-bar's height is only increased
29741 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29742 Vauto_resize_tool_bars = Qt;
29743
29744 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29745 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29746 auto_raise_tool_bar_buttons_p = 1;
29747
29748 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29749 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29750 make_cursor_line_fully_visible_p = 1;
29751
29752 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29753 doc: /* Border below tool-bar in pixels.
29754 If an integer, use it as the height of the border.
29755 If it is one of `internal-border-width' or `border-width', use the
29756 value of the corresponding frame parameter.
29757 Otherwise, no border is added below the tool-bar. */);
29758 Vtool_bar_border = Qinternal_border_width;
29759
29760 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29761 doc: /* Margin around tool-bar buttons in pixels.
29762 If an integer, use that for both horizontal and vertical margins.
29763 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29764 HORZ specifying the horizontal margin, and VERT specifying the
29765 vertical margin. */);
29766 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29767
29768 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29769 doc: /* Relief thickness of tool-bar buttons. */);
29770 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29771
29772 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29773 doc: /* Tool bar style to use.
29774 It can be one of
29775 image - show images only
29776 text - show text only
29777 both - show both, text below image
29778 both-horiz - show text to the right of the image
29779 text-image-horiz - show text to the left of the image
29780 any other - use system default or image if no system default.
29781
29782 This variable only affects the GTK+ toolkit version of Emacs. */);
29783 Vtool_bar_style = Qnil;
29784
29785 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29786 doc: /* Maximum number of characters a label can have to be shown.
29787 The tool bar style must also show labels for this to have any effect, see
29788 `tool-bar-style'. */);
29789 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29790
29791 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29792 doc: /* List of functions to call to fontify regions of text.
29793 Each function is called with one argument POS. Functions must
29794 fontify a region starting at POS in the current buffer, and give
29795 fontified regions the property `fontified'. */);
29796 Vfontification_functions = Qnil;
29797 Fmake_variable_buffer_local (Qfontification_functions);
29798
29799 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29800 unibyte_display_via_language_environment,
29801 doc: /* Non-nil means display unibyte text according to language environment.
29802 Specifically, this means that raw bytes in the range 160-255 decimal
29803 are displayed by converting them to the equivalent multibyte characters
29804 according to the current language environment. As a result, they are
29805 displayed according to the current fontset.
29806
29807 Note that this variable affects only how these bytes are displayed,
29808 but does not change the fact they are interpreted as raw bytes. */);
29809 unibyte_display_via_language_environment = 0;
29810
29811 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29812 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29813 If a float, it specifies a fraction of the mini-window frame's height.
29814 If an integer, it specifies a number of lines. */);
29815 Vmax_mini_window_height = make_float (0.25);
29816
29817 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29818 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29819 A value of nil means don't automatically resize mini-windows.
29820 A value of t means resize them to fit the text displayed in them.
29821 A value of `grow-only', the default, means let mini-windows grow only;
29822 they return to their normal size when the minibuffer is closed, or the
29823 echo area becomes empty. */);
29824 Vresize_mini_windows = Qgrow_only;
29825
29826 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29827 doc: /* Alist specifying how to blink the cursor off.
29828 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29829 `cursor-type' frame-parameter or variable equals ON-STATE,
29830 comparing using `equal', Emacs uses OFF-STATE to specify
29831 how to blink it off. ON-STATE and OFF-STATE are values for
29832 the `cursor-type' frame parameter.
29833
29834 If a frame's ON-STATE has no entry in this list,
29835 the frame's other specifications determine how to blink the cursor off. */);
29836 Vblink_cursor_alist = Qnil;
29837
29838 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29839 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29840 If non-nil, windows are automatically scrolled horizontally to make
29841 point visible. */);
29842 automatic_hscrolling_p = 1;
29843 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29844
29845 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29846 doc: /* How many columns away from the window edge point is allowed to get
29847 before automatic hscrolling will horizontally scroll the window. */);
29848 hscroll_margin = 5;
29849
29850 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29851 doc: /* How many columns to scroll the window when point gets too close to the edge.
29852 When point is less than `hscroll-margin' columns from the window
29853 edge, automatic hscrolling will scroll the window by the amount of columns
29854 determined by this variable. If its value is a positive integer, scroll that
29855 many columns. If it's a positive floating-point number, it specifies the
29856 fraction of the window's width to scroll. If it's nil or zero, point will be
29857 centered horizontally after the scroll. Any other value, including negative
29858 numbers, are treated as if the value were zero.
29859
29860 Automatic hscrolling always moves point outside the scroll margin, so if
29861 point was more than scroll step columns inside the margin, the window will
29862 scroll more than the value given by the scroll step.
29863
29864 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29865 and `scroll-right' overrides this variable's effect. */);
29866 Vhscroll_step = make_number (0);
29867
29868 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29869 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29870 Bind this around calls to `message' to let it take effect. */);
29871 message_truncate_lines = 0;
29872
29873 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29874 doc: /* Normal hook run to update the menu bar definitions.
29875 Redisplay runs this hook before it redisplays the menu bar.
29876 This is used to update submenus such as Buffers,
29877 whose contents depend on various data. */);
29878 Vmenu_bar_update_hook = Qnil;
29879
29880 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29881 doc: /* Frame for which we are updating a menu.
29882 The enable predicate for a menu binding should check this variable. */);
29883 Vmenu_updating_frame = Qnil;
29884
29885 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29886 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29887 inhibit_menubar_update = 0;
29888
29889 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29890 doc: /* Prefix prepended to all continuation lines at display time.
29891 The value may be a string, an image, or a stretch-glyph; it is
29892 interpreted in the same way as the value of a `display' text property.
29893
29894 This variable is overridden by any `wrap-prefix' text or overlay
29895 property.
29896
29897 To add a prefix to non-continuation lines, use `line-prefix'. */);
29898 Vwrap_prefix = Qnil;
29899 DEFSYM (Qwrap_prefix, "wrap-prefix");
29900 Fmake_variable_buffer_local (Qwrap_prefix);
29901
29902 DEFVAR_LISP ("line-prefix", Vline_prefix,
29903 doc: /* Prefix prepended to all non-continuation lines at display time.
29904 The value may be a string, an image, or a stretch-glyph; it is
29905 interpreted in the same way as the value of a `display' text property.
29906
29907 This variable is overridden by any `line-prefix' text or overlay
29908 property.
29909
29910 To add a prefix to continuation lines, use `wrap-prefix'. */);
29911 Vline_prefix = Qnil;
29912 DEFSYM (Qline_prefix, "line-prefix");
29913 Fmake_variable_buffer_local (Qline_prefix);
29914
29915 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29916 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29917 inhibit_eval_during_redisplay = 0;
29918
29919 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29920 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29921 inhibit_free_realized_faces = 0;
29922
29923 #ifdef GLYPH_DEBUG
29924 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29925 doc: /* Inhibit try_window_id display optimization. */);
29926 inhibit_try_window_id = 0;
29927
29928 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29929 doc: /* Inhibit try_window_reusing display optimization. */);
29930 inhibit_try_window_reusing = 0;
29931
29932 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29933 doc: /* Inhibit try_cursor_movement display optimization. */);
29934 inhibit_try_cursor_movement = 0;
29935 #endif /* GLYPH_DEBUG */
29936
29937 DEFVAR_INT ("overline-margin", overline_margin,
29938 doc: /* Space between overline and text, in pixels.
29939 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29940 margin to the character height. */);
29941 overline_margin = 2;
29942
29943 DEFVAR_INT ("underline-minimum-offset",
29944 underline_minimum_offset,
29945 doc: /* Minimum distance between baseline and underline.
29946 This can improve legibility of underlined text at small font sizes,
29947 particularly when using variable `x-use-underline-position-properties'
29948 with fonts that specify an UNDERLINE_POSITION relatively close to the
29949 baseline. The default value is 1. */);
29950 underline_minimum_offset = 1;
29951
29952 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29953 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29954 This feature only works when on a window system that can change
29955 cursor shapes. */);
29956 display_hourglass_p = 1;
29957
29958 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29959 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29960 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29961
29962 hourglass_atimer = NULL;
29963 hourglass_shown_p = 0;
29964
29965 DEFSYM (Qglyphless_char, "glyphless-char");
29966 DEFSYM (Qhex_code, "hex-code");
29967 DEFSYM (Qempty_box, "empty-box");
29968 DEFSYM (Qthin_space, "thin-space");
29969 DEFSYM (Qzero_width, "zero-width");
29970
29971 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29972 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29973
29974 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29975 doc: /* Char-table defining glyphless characters.
29976 Each element, if non-nil, should be one of the following:
29977 an ASCII acronym string: display this string in a box
29978 `hex-code': display the hexadecimal code of a character in a box
29979 `empty-box': display as an empty box
29980 `thin-space': display as 1-pixel width space
29981 `zero-width': don't display
29982 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29983 display method for graphical terminals and text terminals respectively.
29984 GRAPHICAL and TEXT should each have one of the values listed above.
29985
29986 The char-table has one extra slot to control the display of a character for
29987 which no font is found. This slot only takes effect on graphical terminals.
29988 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29989 `thin-space'. The default is `empty-box'. */);
29990 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29991 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29992 Qempty_box);
29993
29994 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29995 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29996 Vdebug_on_message = Qnil;
29997 }
29998
29999
30000 /* Initialize this module when Emacs starts. */
30001
30002 void
30003 init_xdisp (void)
30004 {
30005 current_header_line_height = current_mode_line_height = -1;
30006
30007 CHARPOS (this_line_start_pos) = 0;
30008
30009 if (!noninteractive)
30010 {
30011 struct window *m = XWINDOW (minibuf_window);
30012 Lisp_Object frame = m->frame;
30013 struct frame *f = XFRAME (frame);
30014 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30015 struct window *r = XWINDOW (root);
30016 int i;
30017
30018 echo_area_window = minibuf_window;
30019
30020 r->top_line = FRAME_TOP_MARGIN (f);
30021 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30022 r->total_cols = FRAME_COLS (f);
30023
30024 m->top_line = FRAME_LINES (f) - 1;
30025 m->total_lines = 1;
30026 m->total_cols = FRAME_COLS (f);
30027
30028 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30029 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30030 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30031
30032 /* The default ellipsis glyphs `...'. */
30033 for (i = 0; i < 3; ++i)
30034 default_invis_vector[i] = make_number ('.');
30035 }
30036
30037 {
30038 /* Allocate the buffer for frame titles.
30039 Also used for `format-mode-line'. */
30040 int size = 100;
30041 mode_line_noprop_buf = xmalloc (size);
30042 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30043 mode_line_noprop_ptr = mode_line_noprop_buf;
30044 mode_line_target = MODE_LINE_DISPLAY;
30045 }
30046
30047 help_echo_showing_p = 0;
30048 }
30049
30050 /* Platform-independent portion of hourglass implementation. */
30051
30052 /* Cancel a currently active hourglass timer, and start a new one. */
30053 void
30054 start_hourglass (void)
30055 {
30056 #if defined (HAVE_WINDOW_SYSTEM)
30057 EMACS_TIME delay;
30058
30059 cancel_hourglass ();
30060
30061 if (INTEGERP (Vhourglass_delay)
30062 && XINT (Vhourglass_delay) > 0)
30063 delay = make_emacs_time (min (XINT (Vhourglass_delay),
30064 TYPE_MAXIMUM (time_t)),
30065 0);
30066 else if (FLOATP (Vhourglass_delay)
30067 && XFLOAT_DATA (Vhourglass_delay) > 0)
30068 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
30069 else
30070 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
30071
30072 #ifdef HAVE_NTGUI
30073 {
30074 extern void w32_note_current_window (void);
30075 w32_note_current_window ();
30076 }
30077 #endif /* HAVE_NTGUI */
30078
30079 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30080 show_hourglass, NULL);
30081 #endif
30082 }
30083
30084
30085 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30086 shown. */
30087 void
30088 cancel_hourglass (void)
30089 {
30090 #if defined (HAVE_WINDOW_SYSTEM)
30091 if (hourglass_atimer)
30092 {
30093 cancel_atimer (hourglass_atimer);
30094 hourglass_atimer = NULL;
30095 }
30096
30097 if (hourglass_shown_p)
30098 hide_hourglass ();
30099 #endif
30100 }