]> code.delx.au - gnu-emacs/blob - src/xdisp.c
merge from trunk
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
315
316 #include "font.h"
317 #ifdef HAVE_XWIDGETS
318 #include "xwidget.h"
319 #endif
320 #ifndef FRAME_X_OUTPUT
321 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
322 #endif
323
324 #define INFINITY 10000000
325
326 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
327 Lisp_Object Qwindow_scroll_functions;
328 static Lisp_Object Qwindow_text_change_functions;
329 static Lisp_Object Qredisplay_end_trigger_functions;
330 Lisp_Object Qinhibit_point_motion_hooks;
331 static Lisp_Object QCeval, QCpropertize;
332 Lisp_Object QCfile, QCdata;
333 static Lisp_Object Qfontified;
334 static Lisp_Object Qgrow_only;
335 static Lisp_Object Qinhibit_eval_during_redisplay;
336 static Lisp_Object Qbuffer_position, Qposition, Qobject;
337 static Lisp_Object Qright_to_left, Qleft_to_right;
338
339 /* Cursor shapes. */
340 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
341
342 /* Pointer shapes. */
343 static Lisp_Object Qarrow, Qhand;
344 Lisp_Object Qtext;
345
346 /* Holds the list (error). */
347 static Lisp_Object list_of_error;
348
349 static Lisp_Object Qfontification_functions;
350
351 static Lisp_Object Qwrap_prefix;
352 static Lisp_Object Qline_prefix;
353 static Lisp_Object Qredisplay_internal;
354
355 /* Non-nil means don't actually do any redisplay. */
356
357 Lisp_Object Qinhibit_redisplay;
358
359 /* Names of text properties relevant for redisplay. */
360
361 Lisp_Object Qdisplay;
362
363 Lisp_Object Qspace, QCalign_to;
364 static Lisp_Object QCrelative_width, QCrelative_height;
365 Lisp_Object Qleft_margin, Qright_margin;
366 static Lisp_Object Qspace_width, Qraise;
367 static Lisp_Object Qslice;
368 Lisp_Object Qcenter;
369 static Lisp_Object Qmargin, Qpointer;
370 static Lisp_Object Qline_height;
371
372 #ifdef HAVE_WINDOW_SYSTEM
373
374 /* Test if overflow newline into fringe. Called with iterator IT
375 at or past right window margin, and with IT->current_x set. */
376
377 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
378 (!NILP (Voverflow_newline_into_fringe) \
379 && FRAME_WINDOW_P ((IT)->f) \
380 && ((IT)->bidi_it.paragraph_dir == R2L \
381 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
382 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
383 && (IT)->current_x == (IT)->last_visible_x)
384
385 #else /* !HAVE_WINDOW_SYSTEM */
386 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
387 #endif /* HAVE_WINDOW_SYSTEM */
388
389 /* Test if the display element loaded in IT, or the underlying buffer
390 or string character, is a space or a TAB character. This is used
391 to determine where word wrapping can occur. */
392
393 #define IT_DISPLAYING_WHITESPACE(it) \
394 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
395 || ((STRINGP (it->string) \
396 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
397 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
398 || (it->s \
399 && (it->s[IT_BYTEPOS (*it)] == ' ' \
400 || it->s[IT_BYTEPOS (*it)] == '\t')) \
401 || (IT_BYTEPOS (*it) < ZV_BYTE \
402 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
403 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
404
405 /* Name of the face used to highlight trailing whitespace. */
406
407 static Lisp_Object Qtrailing_whitespace;
408
409 /* Name and number of the face used to highlight escape glyphs. */
410
411 static Lisp_Object Qescape_glyph;
412
413 /* Name and number of the face used to highlight non-breaking spaces. */
414
415 static Lisp_Object Qnobreak_space;
416
417 /* The symbol `image' which is the car of the lists used to represent
418 images in Lisp. Also a tool bar style. */
419
420 Lisp_Object Qimage;
421
422 /* The image map types. */
423 Lisp_Object QCmap;
424 static Lisp_Object QCpointer;
425 static Lisp_Object Qrect, Qcircle, Qpoly;
426
427 /* Tool bar styles */
428 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
429
430 /* Non-zero means print newline to stdout before next mini-buffer
431 message. */
432
433 int noninteractive_need_newline;
434
435 /* Non-zero means print newline to message log before next message. */
436
437 static int message_log_need_newline;
438
439 /* Three markers that message_dolog uses.
440 It could allocate them itself, but that causes trouble
441 in handling memory-full errors. */
442 static Lisp_Object message_dolog_marker1;
443 static Lisp_Object message_dolog_marker2;
444 static Lisp_Object message_dolog_marker3;
445 \f
446 /* The buffer position of the first character appearing entirely or
447 partially on the line of the selected window which contains the
448 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
449 redisplay optimization in redisplay_internal. */
450
451 static struct text_pos this_line_start_pos;
452
453 /* Number of characters past the end of the line above, including the
454 terminating newline. */
455
456 static struct text_pos this_line_end_pos;
457
458 /* The vertical positions and the height of this line. */
459
460 static int this_line_vpos;
461 static int this_line_y;
462 static int this_line_pixel_height;
463
464 /* X position at which this display line starts. Usually zero;
465 negative if first character is partially visible. */
466
467 static int this_line_start_x;
468
469 /* The smallest character position seen by move_it_* functions as they
470 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
471 hscrolled lines, see display_line. */
472
473 static struct text_pos this_line_min_pos;
474
475 /* Buffer that this_line_.* variables are referring to. */
476
477 static struct buffer *this_line_buffer;
478
479
480 /* Values of those variables at last redisplay are stored as
481 properties on `overlay-arrow-position' symbol. However, if
482 Voverlay_arrow_position is a marker, last-arrow-position is its
483 numerical position. */
484
485 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
486
487 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
488 properties on a symbol in overlay-arrow-variable-list. */
489
490 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
491
492 Lisp_Object Qmenu_bar_update_hook;
493
494 /* Nonzero if an overlay arrow has been displayed in this window. */
495
496 static int overlay_arrow_seen;
497
498 /* Vector containing glyphs for an ellipsis `...'. */
499
500 static Lisp_Object default_invis_vector[3];
501
502 /* This is the window where the echo area message was displayed. It
503 is always a mini-buffer window, but it may not be the same window
504 currently active as a mini-buffer. */
505
506 Lisp_Object echo_area_window;
507
508 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
509 pushes the current message and the value of
510 message_enable_multibyte on the stack, the function restore_message
511 pops the stack and displays MESSAGE again. */
512
513 static Lisp_Object Vmessage_stack;
514
515 /* Nonzero means multibyte characters were enabled when the echo area
516 message was specified. */
517
518 static int message_enable_multibyte;
519
520 /* Nonzero if we should redraw the mode lines on the next redisplay. */
521
522 int update_mode_lines;
523
524 /* Nonzero if window sizes or contents have changed since last
525 redisplay that finished. */
526
527 int windows_or_buffers_changed;
528
529 /* Nonzero means a frame's cursor type has been changed. */
530
531 static int cursor_type_changed;
532
533 /* Nonzero after display_mode_line if %l was used and it displayed a
534 line number. */
535
536 static int line_number_displayed;
537
538 /* The name of the *Messages* buffer, a string. */
539
540 static Lisp_Object Vmessages_buffer_name;
541
542 /* Current, index 0, and last displayed echo area message. Either
543 buffers from echo_buffers, or nil to indicate no message. */
544
545 Lisp_Object echo_area_buffer[2];
546
547 /* The buffers referenced from echo_area_buffer. */
548
549 static Lisp_Object echo_buffer[2];
550
551 /* A vector saved used in with_area_buffer to reduce consing. */
552
553 static Lisp_Object Vwith_echo_area_save_vector;
554
555 /* Non-zero means display_echo_area should display the last echo area
556 message again. Set by redisplay_preserve_echo_area. */
557
558 static int display_last_displayed_message_p;
559
560 /* Nonzero if echo area is being used by print; zero if being used by
561 message. */
562
563 static int message_buf_print;
564
565 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
566
567 static Lisp_Object Qinhibit_menubar_update;
568 static Lisp_Object Qmessage_truncate_lines;
569
570 /* Set to 1 in clear_message to make redisplay_internal aware
571 of an emptied echo area. */
572
573 static int message_cleared_p;
574
575 /* A scratch glyph row with contents used for generating truncation
576 glyphs. Also used in direct_output_for_insert. */
577
578 #define MAX_SCRATCH_GLYPHS 100
579 static struct glyph_row scratch_glyph_row;
580 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
581
582 /* Ascent and height of the last line processed by move_it_to. */
583
584 static int last_height;
585
586 /* Non-zero if there's a help-echo in the echo area. */
587
588 int help_echo_showing_p;
589
590 /* If >= 0, computed, exact values of mode-line and header-line height
591 to use in the macros CURRENT_MODE_LINE_HEIGHT and
592 CURRENT_HEADER_LINE_HEIGHT. */
593
594 int current_mode_line_height, current_header_line_height;
595
596 /* The maximum distance to look ahead for text properties. Values
597 that are too small let us call compute_char_face and similar
598 functions too often which is expensive. Values that are too large
599 let us call compute_char_face and alike too often because we
600 might not be interested in text properties that far away. */
601
602 #define TEXT_PROP_DISTANCE_LIMIT 100
603
604 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
605 iterator state and later restore it. This is needed because the
606 bidi iterator on bidi.c keeps a stacked cache of its states, which
607 is really a singleton. When we use scratch iterator objects to
608 move around the buffer, we can cause the bidi cache to be pushed or
609 popped, and therefore we need to restore the cache state when we
610 return to the original iterator. */
611 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
612 do { \
613 if (CACHE) \
614 bidi_unshelve_cache (CACHE, 1); \
615 ITCOPY = ITORIG; \
616 CACHE = bidi_shelve_cache (); \
617 } while (0)
618
619 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
620 do { \
621 if (pITORIG != pITCOPY) \
622 *(pITORIG) = *(pITCOPY); \
623 bidi_unshelve_cache (CACHE, 0); \
624 CACHE = NULL; \
625 } while (0)
626
627 #ifdef GLYPH_DEBUG
628
629 /* Non-zero means print traces of redisplay if compiled with
630 GLYPH_DEBUG defined. */
631
632 int trace_redisplay_p;
633
634 #endif /* GLYPH_DEBUG */
635
636 #ifdef DEBUG_TRACE_MOVE
637 /* Non-zero means trace with TRACE_MOVE to stderr. */
638 int trace_move;
639
640 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
641 #else
642 #define TRACE_MOVE(x) (void) 0
643 #endif
644
645 static Lisp_Object Qauto_hscroll_mode;
646
647 /* Buffer being redisplayed -- for redisplay_window_error. */
648
649 static struct buffer *displayed_buffer;
650
651 /* Value returned from text property handlers (see below). */
652
653 enum prop_handled
654 {
655 HANDLED_NORMALLY,
656 HANDLED_RECOMPUTE_PROPS,
657 HANDLED_OVERLAY_STRING_CONSUMED,
658 HANDLED_RETURN
659 };
660
661 /* A description of text properties that redisplay is interested
662 in. */
663
664 struct props
665 {
666 /* The name of the property. */
667 Lisp_Object *name;
668
669 /* A unique index for the property. */
670 enum prop_idx idx;
671
672 /* A handler function called to set up iterator IT from the property
673 at IT's current position. Value is used to steer handle_stop. */
674 enum prop_handled (*handler) (struct it *it);
675 };
676
677 static enum prop_handled handle_face_prop (struct it *);
678 static enum prop_handled handle_invisible_prop (struct it *);
679 static enum prop_handled handle_display_prop (struct it *);
680 static enum prop_handled handle_composition_prop (struct it *);
681 static enum prop_handled handle_overlay_change (struct it *);
682 static enum prop_handled handle_fontified_prop (struct it *);
683
684 /* Properties handled by iterators. */
685
686 static struct props it_props[] =
687 {
688 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
689 /* Handle `face' before `display' because some sub-properties of
690 `display' need to know the face. */
691 {&Qface, FACE_PROP_IDX, handle_face_prop},
692 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
693 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
694 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
695 {NULL, 0, NULL}
696 };
697
698 /* Value is the position described by X. If X is a marker, value is
699 the marker_position of X. Otherwise, value is X. */
700
701 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
702
703 /* Enumeration returned by some move_it_.* functions internally. */
704
705 enum move_it_result
706 {
707 /* Not used. Undefined value. */
708 MOVE_UNDEFINED,
709
710 /* Move ended at the requested buffer position or ZV. */
711 MOVE_POS_MATCH_OR_ZV,
712
713 /* Move ended at the requested X pixel position. */
714 MOVE_X_REACHED,
715
716 /* Move within a line ended at the end of a line that must be
717 continued. */
718 MOVE_LINE_CONTINUED,
719
720 /* Move within a line ended at the end of a line that would
721 be displayed truncated. */
722 MOVE_LINE_TRUNCATED,
723
724 /* Move within a line ended at a line end. */
725 MOVE_NEWLINE_OR_CR
726 };
727
728 /* This counter is used to clear the face cache every once in a while
729 in redisplay_internal. It is incremented for each redisplay.
730 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
731 cleared. */
732
733 #define CLEAR_FACE_CACHE_COUNT 500
734 static int clear_face_cache_count;
735
736 /* Similarly for the image cache. */
737
738 #ifdef HAVE_WINDOW_SYSTEM
739 #define CLEAR_IMAGE_CACHE_COUNT 101
740 static int clear_image_cache_count;
741
742 /* Null glyph slice */
743 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
744 #endif
745
746 /* True while redisplay_internal is in progress. */
747
748 bool redisplaying_p;
749
750 static Lisp_Object Qinhibit_free_realized_faces;
751 static Lisp_Object Qmode_line_default_help_echo;
752
753 /* If a string, XTread_socket generates an event to display that string.
754 (The display is done in read_char.) */
755
756 Lisp_Object help_echo_string;
757 Lisp_Object help_echo_window;
758 Lisp_Object help_echo_object;
759 ptrdiff_t help_echo_pos;
760
761 /* Temporary variable for XTread_socket. */
762
763 Lisp_Object previous_help_echo_string;
764
765 /* Platform-independent portion of hourglass implementation. */
766
767 /* Non-zero means an hourglass cursor is currently shown. */
768 int hourglass_shown_p;
769
770 /* If non-null, an asynchronous timer that, when it expires, displays
771 an hourglass cursor on all frames. */
772 struct atimer *hourglass_atimer;
773
774 /* Name of the face used to display glyphless characters. */
775 Lisp_Object Qglyphless_char;
776
777 /* Symbol for the purpose of Vglyphless_char_display. */
778 static Lisp_Object Qglyphless_char_display;
779
780 /* Method symbols for Vglyphless_char_display. */
781 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
782
783 /* Default pixel width of `thin-space' display method. */
784 #define THIN_SPACE_WIDTH 1
785
786 /* Default number of seconds to wait before displaying an hourglass
787 cursor. */
788 #define DEFAULT_HOURGLASS_DELAY 1
789
790 \f
791 /* Function prototypes. */
792
793 static void setup_for_ellipsis (struct it *, int);
794 static void set_iterator_to_next (struct it *, int);
795 static void mark_window_display_accurate_1 (struct window *, int);
796 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
797 static int display_prop_string_p (Lisp_Object, Lisp_Object);
798 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
799 static int cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, int);
801 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
802
803 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
804
805 static void handle_line_prefix (struct it *);
806
807 static void pint2str (char *, int, ptrdiff_t);
808 static void pint2hrstr (char *, int, ptrdiff_t);
809 static struct text_pos run_window_scroll_functions (Lisp_Object,
810 struct text_pos);
811 static int text_outside_line_unchanged_p (struct window *,
812 ptrdiff_t, ptrdiff_t);
813 static void store_mode_line_noprop_char (char);
814 static int store_mode_line_noprop (const char *, int, int);
815 static void handle_stop (struct it *);
816 static void handle_stop_backwards (struct it *, ptrdiff_t);
817 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
818 static void ensure_echo_area_buffers (void);
819 static void unwind_with_echo_area_buffer (Lisp_Object);
820 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
821 static int with_echo_area_buffer (struct window *, int,
822 int (*) (ptrdiff_t, Lisp_Object),
823 ptrdiff_t, Lisp_Object);
824 static void clear_garbaged_frames (void);
825 static int current_message_1 (ptrdiff_t, Lisp_Object);
826 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
827 static void set_message (Lisp_Object);
828 static int set_message_1 (ptrdiff_t, Lisp_Object);
829 static int display_echo_area (struct window *);
830 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
831 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
832 static void unwind_redisplay (void);
833 static int string_char_and_length (const unsigned char *, int *);
834 static struct text_pos display_prop_end (struct it *, Lisp_Object,
835 struct text_pos);
836 static int compute_window_start_on_continuation_line (struct window *);
837 static void insert_left_trunc_glyphs (struct it *);
838 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
839 Lisp_Object);
840 static void extend_face_to_end_of_line (struct it *);
841 static int append_space_for_newline (struct it *, int);
842 static int cursor_row_fully_visible_p (struct window *, int, int);
843 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
844 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
845 static int trailing_whitespace_p (ptrdiff_t);
846 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
847 static void push_it (struct it *, struct text_pos *);
848 static void iterate_out_of_display_property (struct it *);
849 static void pop_it (struct it *);
850 static void sync_frame_with_window_matrix_rows (struct window *);
851 static void redisplay_internal (void);
852 static int echo_area_display (int);
853 static void redisplay_windows (Lisp_Object);
854 static void redisplay_window (Lisp_Object, int);
855 static Lisp_Object redisplay_window_error (Lisp_Object);
856 static Lisp_Object redisplay_window_0 (Lisp_Object);
857 static Lisp_Object redisplay_window_1 (Lisp_Object);
858 static int set_cursor_from_row (struct window *, struct glyph_row *,
859 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
860 int, int);
861 static int update_menu_bar (struct frame *, int, int);
862 static int try_window_reusing_current_matrix (struct window *);
863 static int try_window_id (struct window *);
864 static int display_line (struct it *);
865 static int display_mode_lines (struct window *);
866 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
867 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
868 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
869 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
870 static void display_menu_bar (struct window *);
871 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
872 ptrdiff_t *);
873 static int display_string (const char *, Lisp_Object, Lisp_Object,
874 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
875 static void compute_line_metrics (struct it *);
876 static void run_redisplay_end_trigger_hook (struct it *);
877 static int get_overlay_strings (struct it *, ptrdiff_t);
878 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
879 static void next_overlay_string (struct it *);
880 static void reseat (struct it *, struct text_pos, int);
881 static void reseat_1 (struct it *, struct text_pos, int);
882 static void back_to_previous_visible_line_start (struct it *);
883 static void reseat_at_next_visible_line_start (struct it *, int);
884 static int next_element_from_ellipsis (struct it *);
885 static int next_element_from_display_vector (struct it *);
886 static int next_element_from_string (struct it *);
887 static int next_element_from_c_string (struct it *);
888 static int next_element_from_buffer (struct it *);
889 static int next_element_from_composition (struct it *);
890 static int next_element_from_image (struct it *);
891 #ifdef HAVE_XWIDGETS
892 static int next_element_from_xwidget(struct it *);
893 #endif
894 static int next_element_from_stretch (struct it *);
895 static void load_overlay_strings (struct it *, ptrdiff_t);
896 static int init_from_display_pos (struct it *, struct window *,
897 struct display_pos *);
898 static void reseat_to_string (struct it *, const char *,
899 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
900 static int get_next_display_element (struct it *);
901 static enum move_it_result
902 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
903 enum move_operation_enum);
904 static void get_visually_first_element (struct it *);
905 static void init_to_row_start (struct it *, struct window *,
906 struct glyph_row *);
907 static int init_to_row_end (struct it *, struct window *,
908 struct glyph_row *);
909 static void back_to_previous_line_start (struct it *);
910 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
911 static struct text_pos string_pos_nchars_ahead (struct text_pos,
912 Lisp_Object, ptrdiff_t);
913 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
914 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
915 static ptrdiff_t number_of_chars (const char *, bool);
916 static void compute_stop_pos (struct it *);
917 static void compute_string_pos (struct text_pos *, struct text_pos,
918 Lisp_Object);
919 static int face_before_or_after_it_pos (struct it *, int);
920 static ptrdiff_t next_overlay_change (ptrdiff_t);
921 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
922 Lisp_Object, struct text_pos *, ptrdiff_t, int);
923 static int handle_single_display_spec (struct it *, Lisp_Object,
924 Lisp_Object, Lisp_Object,
925 struct text_pos *, ptrdiff_t, int, int);
926 static int underlying_face_id (struct it *);
927 static int in_ellipses_for_invisible_text_p (struct display_pos *,
928 struct window *);
929
930 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
931 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
932
933 #ifdef HAVE_WINDOW_SYSTEM
934
935 static void x_consider_frame_title (Lisp_Object);
936 static int tool_bar_lines_needed (struct frame *, int *);
937 static void update_tool_bar (struct frame *, int);
938 static void build_desired_tool_bar_string (struct frame *f);
939 static int redisplay_tool_bar (struct frame *);
940 static void display_tool_bar_line (struct it *, int);
941 static void notice_overwritten_cursor (struct window *,
942 enum glyph_row_area,
943 int, int, int, int);
944 static void append_stretch_glyph (struct it *, Lisp_Object,
945 int, int, int);
946
947
948 #endif /* HAVE_WINDOW_SYSTEM */
949
950 static void produce_special_glyphs (struct it *, enum display_element_type);
951 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
952 static int coords_in_mouse_face_p (struct window *, int, int);
953
954
955 \f
956 /***********************************************************************
957 Window display dimensions
958 ***********************************************************************/
959
960 /* Return the bottom boundary y-position for text lines in window W.
961 This is the first y position at which a line cannot start.
962 It is relative to the top of the window.
963
964 This is the height of W minus the height of a mode line, if any. */
965
966 int
967 window_text_bottom_y (struct window *w)
968 {
969 int height = WINDOW_TOTAL_HEIGHT (w);
970
971 if (WINDOW_WANTS_MODELINE_P (w))
972 height -= CURRENT_MODE_LINE_HEIGHT (w);
973 return height;
974 }
975
976 /* Return the pixel width of display area AREA of window W. AREA < 0
977 means return the total width of W, not including fringes to
978 the left and right of the window. */
979
980 int
981 window_box_width (struct window *w, int area)
982 {
983 int cols = w->total_cols;
984 int pixels = 0;
985
986 if (!w->pseudo_window_p)
987 {
988 cols -= WINDOW_SCROLL_BAR_COLS (w);
989
990 if (area == TEXT_AREA)
991 {
992 cols -= max (0, w->left_margin_cols);
993 cols -= max (0, w->right_margin_cols);
994 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
995 }
996 else if (area == LEFT_MARGIN_AREA)
997 {
998 cols = max (0, w->left_margin_cols);
999 pixels = 0;
1000 }
1001 else if (area == RIGHT_MARGIN_AREA)
1002 {
1003 cols = max (0, w->right_margin_cols);
1004 pixels = 0;
1005 }
1006 }
1007
1008 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1009 }
1010
1011
1012 /* Return the pixel height of the display area of window W, not
1013 including mode lines of W, if any. */
1014
1015 int
1016 window_box_height (struct window *w)
1017 {
1018 struct frame *f = XFRAME (w->frame);
1019 int height = WINDOW_TOTAL_HEIGHT (w);
1020
1021 eassert (height >= 0);
1022
1023 /* Note: the code below that determines the mode-line/header-line
1024 height is essentially the same as that contained in the macro
1025 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1026 the appropriate glyph row has its `mode_line_p' flag set,
1027 and if it doesn't, uses estimate_mode_line_height instead. */
1028
1029 if (WINDOW_WANTS_MODELINE_P (w))
1030 {
1031 struct glyph_row *ml_row
1032 = (w->current_matrix && w->current_matrix->rows
1033 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1034 : 0);
1035 if (ml_row && ml_row->mode_line_p)
1036 height -= ml_row->height;
1037 else
1038 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1039 }
1040
1041 if (WINDOW_WANTS_HEADER_LINE_P (w))
1042 {
1043 struct glyph_row *hl_row
1044 = (w->current_matrix && w->current_matrix->rows
1045 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1046 : 0);
1047 if (hl_row && hl_row->mode_line_p)
1048 height -= hl_row->height;
1049 else
1050 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1051 }
1052
1053 /* With a very small font and a mode-line that's taller than
1054 default, we might end up with a negative height. */
1055 return max (0, height);
1056 }
1057
1058 /* Return the window-relative coordinate of the left edge of display
1059 area AREA of window W. AREA < 0 means return the left edge of the
1060 whole window, to the right of the left fringe of W. */
1061
1062 int
1063 window_box_left_offset (struct window *w, int area)
1064 {
1065 int x;
1066
1067 if (w->pseudo_window_p)
1068 return 0;
1069
1070 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1071
1072 if (area == TEXT_AREA)
1073 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1074 + window_box_width (w, LEFT_MARGIN_AREA));
1075 else if (area == RIGHT_MARGIN_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA)
1078 + window_box_width (w, TEXT_AREA)
1079 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1080 ? 0
1081 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1082 else if (area == LEFT_MARGIN_AREA
1083 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1084 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1085
1086 return x;
1087 }
1088
1089
1090 /* Return the window-relative coordinate of the right edge of display
1091 area AREA of window W. AREA < 0 means return the right edge of the
1092 whole window, to the left of the right fringe of W. */
1093
1094 int
1095 window_box_right_offset (struct window *w, int area)
1096 {
1097 return window_box_left_offset (w, area) + window_box_width (w, area);
1098 }
1099
1100 /* Return the frame-relative coordinate of the left edge of display
1101 area AREA of window W. AREA < 0 means return the left edge of the
1102 whole window, to the right of the left fringe of W. */
1103
1104 int
1105 window_box_left (struct window *w, int area)
1106 {
1107 struct frame *f = XFRAME (w->frame);
1108 int x;
1109
1110 if (w->pseudo_window_p)
1111 return FRAME_INTERNAL_BORDER_WIDTH (f);
1112
1113 x = (WINDOW_LEFT_EDGE_X (w)
1114 + window_box_left_offset (w, area));
1115
1116 return x;
1117 }
1118
1119
1120 /* Return the frame-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the right edge of the
1122 whole window, to the left of the right fringe of W. */
1123
1124 int
1125 window_box_right (struct window *w, int area)
1126 {
1127 return window_box_left (w, area) + window_box_width (w, area);
1128 }
1129
1130 /* Get the bounding box of the display area AREA of window W, without
1131 mode lines, in frame-relative coordinates. AREA < 0 means the
1132 whole window, not including the left and right fringes of
1133 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1134 coordinates of the upper-left corner of the box. Return in
1135 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1136
1137 void
1138 window_box (struct window *w, int area, int *box_x, int *box_y,
1139 int *box_width, int *box_height)
1140 {
1141 if (box_width)
1142 *box_width = window_box_width (w, area);
1143 if (box_height)
1144 *box_height = window_box_height (w);
1145 if (box_x)
1146 *box_x = window_box_left (w, area);
1147 if (box_y)
1148 {
1149 *box_y = WINDOW_TOP_EDGE_Y (w);
1150 if (WINDOW_WANTS_HEADER_LINE_P (w))
1151 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1152 }
1153 }
1154
1155
1156 /* Get the bounding box of the display area AREA of window W, without
1157 mode lines. AREA < 0 means the whole window, not including the
1158 left and right fringe of the window. Return in *TOP_LEFT_X
1159 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1160 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1161 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1162 box. */
1163
1164 static void
1165 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1166 int *bottom_right_x, int *bottom_right_y)
1167 {
1168 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1169 bottom_right_y);
1170 *bottom_right_x += *top_left_x;
1171 *bottom_right_y += *top_left_y;
1172 }
1173
1174
1175 \f
1176 /***********************************************************************
1177 Utilities
1178 ***********************************************************************/
1179
1180 /* Return the bottom y-position of the line the iterator IT is in.
1181 This can modify IT's settings. */
1182
1183 int
1184 line_bottom_y (struct it *it)
1185 {
1186 int line_height = it->max_ascent + it->max_descent;
1187 int line_top_y = it->current_y;
1188
1189 if (line_height == 0)
1190 {
1191 if (last_height)
1192 line_height = last_height;
1193 else if (IT_CHARPOS (*it) < ZV)
1194 {
1195 move_it_by_lines (it, 1);
1196 line_height = (it->max_ascent || it->max_descent
1197 ? it->max_ascent + it->max_descent
1198 : last_height);
1199 }
1200 else
1201 {
1202 struct glyph_row *row = it->glyph_row;
1203
1204 /* Use the default character height. */
1205 it->glyph_row = NULL;
1206 it->what = IT_CHARACTER;
1207 it->c = ' ';
1208 it->len = 1;
1209 PRODUCE_GLYPHS (it);
1210 line_height = it->ascent + it->descent;
1211 it->glyph_row = row;
1212 }
1213 }
1214
1215 return line_top_y + line_height;
1216 }
1217
1218 DEFUN ("line-pixel-height", Fline_pixel_height,
1219 Sline_pixel_height, 0, 0, 0,
1220 doc: /* Return height in pixels of text line in the selected window.
1221
1222 Value is the height in pixels of the line at point. */)
1223 (void)
1224 {
1225 struct it it;
1226 struct text_pos pt;
1227 struct window *w = XWINDOW (selected_window);
1228
1229 SET_TEXT_POS (pt, PT, PT_BYTE);
1230 start_display (&it, w, pt);
1231 it.vpos = it.current_y = 0;
1232 last_height = 0;
1233 return make_number (line_bottom_y (&it));
1234 }
1235
1236 /* Return the default pixel height of text lines in window W. The
1237 value is the canonical height of the W frame's default font, plus
1238 any extra space required by the line-spacing variable or frame
1239 parameter.
1240
1241 Implementation note: this ignores any line-spacing text properties
1242 put on the newline characters. This is because those properties
1243 only affect the _screen_ line ending in the newline (i.e., in a
1244 continued line, only the last screen line will be affected), which
1245 means only a small number of lines in a buffer can ever use this
1246 feature. Since this function is used to compute the default pixel
1247 equivalent of text lines in a window, we can safely ignore those
1248 few lines. For the same reasons, we ignore the line-height
1249 properties. */
1250 int
1251 default_line_pixel_height (struct window *w)
1252 {
1253 struct frame *f = WINDOW_XFRAME (w);
1254 int height = FRAME_LINE_HEIGHT (f);
1255
1256 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1257 {
1258 struct buffer *b = XBUFFER (w->contents);
1259 Lisp_Object val = BVAR (b, extra_line_spacing);
1260
1261 if (NILP (val))
1262 val = BVAR (&buffer_defaults, extra_line_spacing);
1263 if (!NILP (val))
1264 {
1265 if (RANGED_INTEGERP (0, val, INT_MAX))
1266 height += XFASTINT (val);
1267 else if (FLOATP (val))
1268 {
1269 int addon = XFLOAT_DATA (val) * height + 0.5;
1270
1271 if (addon >= 0)
1272 height += addon;
1273 }
1274 }
1275 else
1276 height += f->extra_line_spacing;
1277 }
1278
1279 return height;
1280 }
1281
1282 /* Subroutine of pos_visible_p below. Extracts a display string, if
1283 any, from the display spec given as its argument. */
1284 static Lisp_Object
1285 string_from_display_spec (Lisp_Object spec)
1286 {
1287 if (CONSP (spec))
1288 {
1289 while (CONSP (spec))
1290 {
1291 if (STRINGP (XCAR (spec)))
1292 return XCAR (spec);
1293 spec = XCDR (spec);
1294 }
1295 }
1296 else if (VECTORP (spec))
1297 {
1298 ptrdiff_t i;
1299
1300 for (i = 0; i < ASIZE (spec); i++)
1301 {
1302 if (STRINGP (AREF (spec, i)))
1303 return AREF (spec, i);
1304 }
1305 return Qnil;
1306 }
1307
1308 return spec;
1309 }
1310
1311
1312 /* Limit insanely large values of W->hscroll on frame F to the largest
1313 value that will still prevent first_visible_x and last_visible_x of
1314 'struct it' from overflowing an int. */
1315 static int
1316 window_hscroll_limited (struct window *w, struct frame *f)
1317 {
1318 ptrdiff_t window_hscroll = w->hscroll;
1319 int window_text_width = window_box_width (w, TEXT_AREA);
1320 int colwidth = FRAME_COLUMN_WIDTH (f);
1321
1322 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1323 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1324
1325 return window_hscroll;
1326 }
1327
1328 /* Return 1 if position CHARPOS is visible in window W.
1329 CHARPOS < 0 means return info about WINDOW_END position.
1330 If visible, set *X and *Y to pixel coordinates of top left corner.
1331 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1332 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1333
1334 int
1335 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1336 int *rtop, int *rbot, int *rowh, int *vpos)
1337 {
1338 struct it it;
1339 void *itdata = bidi_shelve_cache ();
1340 struct text_pos top;
1341 int visible_p = 0;
1342 struct buffer *old_buffer = NULL;
1343
1344 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1345 return visible_p;
1346
1347 if (XBUFFER (w->contents) != current_buffer)
1348 {
1349 old_buffer = current_buffer;
1350 set_buffer_internal_1 (XBUFFER (w->contents));
1351 }
1352
1353 SET_TEXT_POS_FROM_MARKER (top, w->start);
1354 /* Scrolling a minibuffer window via scroll bar when the echo area
1355 shows long text sometimes resets the minibuffer contents behind
1356 our backs. */
1357 if (CHARPOS (top) > ZV)
1358 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1359
1360 /* Compute exact mode line heights. */
1361 if (WINDOW_WANTS_MODELINE_P (w))
1362 current_mode_line_height
1363 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1364 BVAR (current_buffer, mode_line_format));
1365
1366 if (WINDOW_WANTS_HEADER_LINE_P (w))
1367 current_header_line_height
1368 = display_mode_line (w, HEADER_LINE_FACE_ID,
1369 BVAR (current_buffer, header_line_format));
1370
1371 start_display (&it, w, top);
1372 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1373 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1374
1375 if (charpos >= 0
1376 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1377 && IT_CHARPOS (it) >= charpos)
1378 /* When scanning backwards under bidi iteration, move_it_to
1379 stops at or _before_ CHARPOS, because it stops at or to
1380 the _right_ of the character at CHARPOS. */
1381 || (it.bidi_p && it.bidi_it.scan_dir == -1
1382 && IT_CHARPOS (it) <= charpos)))
1383 {
1384 /* We have reached CHARPOS, or passed it. How the call to
1385 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1386 or covered by a display property, move_it_to stops at the end
1387 of the invisible text, to the right of CHARPOS. (ii) If
1388 CHARPOS is in a display vector, move_it_to stops on its last
1389 glyph. */
1390 int top_x = it.current_x;
1391 int top_y = it.current_y;
1392 /* Calling line_bottom_y may change it.method, it.position, etc. */
1393 enum it_method it_method = it.method;
1394 int bottom_y = (last_height = 0, line_bottom_y (&it));
1395 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1396
1397 if (top_y < window_top_y)
1398 visible_p = bottom_y > window_top_y;
1399 else if (top_y < it.last_visible_y)
1400 visible_p = 1;
1401 if (bottom_y >= it.last_visible_y
1402 && it.bidi_p && it.bidi_it.scan_dir == -1
1403 && IT_CHARPOS (it) < charpos)
1404 {
1405 /* When the last line of the window is scanned backwards
1406 under bidi iteration, we could be duped into thinking
1407 that we have passed CHARPOS, when in fact move_it_to
1408 simply stopped short of CHARPOS because it reached
1409 last_visible_y. To see if that's what happened, we call
1410 move_it_to again with a slightly larger vertical limit,
1411 and see if it actually moved vertically; if it did, we
1412 didn't really reach CHARPOS, which is beyond window end. */
1413 struct it save_it = it;
1414 /* Why 10? because we don't know how many canonical lines
1415 will the height of the next line(s) be. So we guess. */
1416 int ten_more_lines = 10 * default_line_pixel_height (w);
1417
1418 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1419 MOVE_TO_POS | MOVE_TO_Y);
1420 if (it.current_y > top_y)
1421 visible_p = 0;
1422
1423 it = save_it;
1424 }
1425 if (visible_p)
1426 {
1427 if (it_method == GET_FROM_DISPLAY_VECTOR)
1428 {
1429 /* We stopped on the last glyph of a display vector.
1430 Try and recompute. Hack alert! */
1431 if (charpos < 2 || top.charpos >= charpos)
1432 top_x = it.glyph_row->x;
1433 else
1434 {
1435 struct it it2, it2_prev;
1436 /* The idea is to get to the previous buffer
1437 position, consume the character there, and use
1438 the pixel coordinates we get after that. But if
1439 the previous buffer position is also displayed
1440 from a display vector, we need to consume all of
1441 the glyphs from that display vector. */
1442 start_display (&it2, w, top);
1443 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1444 /* If we didn't get to CHARPOS - 1, there's some
1445 replacing display property at that position, and
1446 we stopped after it. That is exactly the place
1447 whose coordinates we want. */
1448 if (IT_CHARPOS (it2) != charpos - 1)
1449 it2_prev = it2;
1450 else
1451 {
1452 /* Iterate until we get out of the display
1453 vector that displays the character at
1454 CHARPOS - 1. */
1455 do {
1456 get_next_display_element (&it2);
1457 PRODUCE_GLYPHS (&it2);
1458 it2_prev = it2;
1459 set_iterator_to_next (&it2, 1);
1460 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1461 && IT_CHARPOS (it2) < charpos);
1462 }
1463 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1464 || it2_prev.current_x > it2_prev.last_visible_x)
1465 top_x = it.glyph_row->x;
1466 else
1467 {
1468 top_x = it2_prev.current_x;
1469 top_y = it2_prev.current_y;
1470 }
1471 }
1472 }
1473 else if (IT_CHARPOS (it) != charpos)
1474 {
1475 Lisp_Object cpos = make_number (charpos);
1476 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1477 Lisp_Object string = string_from_display_spec (spec);
1478 struct text_pos tpos;
1479 int replacing_spec_p;
1480 bool newline_in_string
1481 = (STRINGP (string)
1482 && memchr (SDATA (string), '\n', SBYTES (string)));
1483
1484 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1485 replacing_spec_p
1486 = (!NILP (spec)
1487 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1488 charpos, FRAME_WINDOW_P (it.f)));
1489 /* The tricky code below is needed because there's a
1490 discrepancy between move_it_to and how we set cursor
1491 when PT is at the beginning of a portion of text
1492 covered by a display property or an overlay with a
1493 display property, or the display line ends in a
1494 newline from a display string. move_it_to will stop
1495 _after_ such display strings, whereas
1496 set_cursor_from_row conspires with cursor_row_p to
1497 place the cursor on the first glyph produced from the
1498 display string. */
1499
1500 /* We have overshoot PT because it is covered by a
1501 display property that replaces the text it covers.
1502 If the string includes embedded newlines, we are also
1503 in the wrong display line. Backtrack to the correct
1504 line, where the display property begins. */
1505 if (replacing_spec_p)
1506 {
1507 Lisp_Object startpos, endpos;
1508 EMACS_INT start, end;
1509 struct it it3;
1510 int it3_moved;
1511
1512 /* Find the first and the last buffer positions
1513 covered by the display string. */
1514 endpos =
1515 Fnext_single_char_property_change (cpos, Qdisplay,
1516 Qnil, Qnil);
1517 startpos =
1518 Fprevious_single_char_property_change (endpos, Qdisplay,
1519 Qnil, Qnil);
1520 start = XFASTINT (startpos);
1521 end = XFASTINT (endpos);
1522 /* Move to the last buffer position before the
1523 display property. */
1524 start_display (&it3, w, top);
1525 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1526 /* Move forward one more line if the position before
1527 the display string is a newline or if it is the
1528 rightmost character on a line that is
1529 continued or word-wrapped. */
1530 if (it3.method == GET_FROM_BUFFER
1531 && (it3.c == '\n'
1532 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1533 move_it_by_lines (&it3, 1);
1534 else if (move_it_in_display_line_to (&it3, -1,
1535 it3.current_x
1536 + it3.pixel_width,
1537 MOVE_TO_X)
1538 == MOVE_LINE_CONTINUED)
1539 {
1540 move_it_by_lines (&it3, 1);
1541 /* When we are under word-wrap, the #$@%!
1542 move_it_by_lines moves 2 lines, so we need to
1543 fix that up. */
1544 if (it3.line_wrap == WORD_WRAP)
1545 move_it_by_lines (&it3, -1);
1546 }
1547
1548 /* Record the vertical coordinate of the display
1549 line where we wound up. */
1550 top_y = it3.current_y;
1551 if (it3.bidi_p)
1552 {
1553 /* When characters are reordered for display,
1554 the character displayed to the left of the
1555 display string could be _after_ the display
1556 property in the logical order. Use the
1557 smallest vertical position of these two. */
1558 start_display (&it3, w, top);
1559 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1560 if (it3.current_y < top_y)
1561 top_y = it3.current_y;
1562 }
1563 /* Move from the top of the window to the beginning
1564 of the display line where the display string
1565 begins. */
1566 start_display (&it3, w, top);
1567 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1568 /* If it3_moved stays zero after the 'while' loop
1569 below, that means we already were at a newline
1570 before the loop (e.g., the display string begins
1571 with a newline), so we don't need to (and cannot)
1572 inspect the glyphs of it3.glyph_row, because
1573 PRODUCE_GLYPHS will not produce anything for a
1574 newline, and thus it3.glyph_row stays at its
1575 stale content it got at top of the window. */
1576 it3_moved = 0;
1577 /* Finally, advance the iterator until we hit the
1578 first display element whose character position is
1579 CHARPOS, or until the first newline from the
1580 display string, which signals the end of the
1581 display line. */
1582 while (get_next_display_element (&it3))
1583 {
1584 PRODUCE_GLYPHS (&it3);
1585 if (IT_CHARPOS (it3) == charpos
1586 || ITERATOR_AT_END_OF_LINE_P (&it3))
1587 break;
1588 it3_moved = 1;
1589 set_iterator_to_next (&it3, 0);
1590 }
1591 top_x = it3.current_x - it3.pixel_width;
1592 /* Normally, we would exit the above loop because we
1593 found the display element whose character
1594 position is CHARPOS. For the contingency that we
1595 didn't, and stopped at the first newline from the
1596 display string, move back over the glyphs
1597 produced from the string, until we find the
1598 rightmost glyph not from the string. */
1599 if (it3_moved
1600 && newline_in_string
1601 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1602 {
1603 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1604 + it3.glyph_row->used[TEXT_AREA];
1605
1606 while (EQ ((g - 1)->object, string))
1607 {
1608 --g;
1609 top_x -= g->pixel_width;
1610 }
1611 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1612 + it3.glyph_row->used[TEXT_AREA]);
1613 }
1614 }
1615 }
1616
1617 *x = top_x;
1618 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1619 *rtop = max (0, window_top_y - top_y);
1620 *rbot = max (0, bottom_y - it.last_visible_y);
1621 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1622 - max (top_y, window_top_y)));
1623 *vpos = it.vpos;
1624 }
1625 }
1626 else
1627 {
1628 /* We were asked to provide info about WINDOW_END. */
1629 struct it it2;
1630 void *it2data = NULL;
1631
1632 SAVE_IT (it2, it, it2data);
1633 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1634 move_it_by_lines (&it, 1);
1635 if (charpos < IT_CHARPOS (it)
1636 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1637 {
1638 visible_p = 1;
1639 RESTORE_IT (&it2, &it2, it2data);
1640 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1641 *x = it2.current_x;
1642 *y = it2.current_y + it2.max_ascent - it2.ascent;
1643 *rtop = max (0, -it2.current_y);
1644 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1645 - it.last_visible_y));
1646 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1647 it.last_visible_y)
1648 - max (it2.current_y,
1649 WINDOW_HEADER_LINE_HEIGHT (w))));
1650 *vpos = it2.vpos;
1651 }
1652 else
1653 bidi_unshelve_cache (it2data, 1);
1654 }
1655 bidi_unshelve_cache (itdata, 0);
1656
1657 if (old_buffer)
1658 set_buffer_internal_1 (old_buffer);
1659
1660 current_header_line_height = current_mode_line_height = -1;
1661
1662 if (visible_p && w->hscroll > 0)
1663 *x -=
1664 window_hscroll_limited (w, WINDOW_XFRAME (w))
1665 * WINDOW_FRAME_COLUMN_WIDTH (w);
1666
1667 #if 0
1668 /* Debugging code. */
1669 if (visible_p)
1670 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1671 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1672 else
1673 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1674 #endif
1675
1676 return visible_p;
1677 }
1678
1679
1680 /* Return the next character from STR. Return in *LEN the length of
1681 the character. This is like STRING_CHAR_AND_LENGTH but never
1682 returns an invalid character. If we find one, we return a `?', but
1683 with the length of the invalid character. */
1684
1685 static int
1686 string_char_and_length (const unsigned char *str, int *len)
1687 {
1688 int c;
1689
1690 c = STRING_CHAR_AND_LENGTH (str, *len);
1691 if (!CHAR_VALID_P (c))
1692 /* We may not change the length here because other places in Emacs
1693 don't use this function, i.e. they silently accept invalid
1694 characters. */
1695 c = '?';
1696
1697 return c;
1698 }
1699
1700
1701
1702 /* Given a position POS containing a valid character and byte position
1703 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1704
1705 static struct text_pos
1706 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1707 {
1708 eassert (STRINGP (string) && nchars >= 0);
1709
1710 if (STRING_MULTIBYTE (string))
1711 {
1712 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1713 int len;
1714
1715 while (nchars--)
1716 {
1717 string_char_and_length (p, &len);
1718 p += len;
1719 CHARPOS (pos) += 1;
1720 BYTEPOS (pos) += len;
1721 }
1722 }
1723 else
1724 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1725
1726 return pos;
1727 }
1728
1729
1730 /* Value is the text position, i.e. character and byte position,
1731 for character position CHARPOS in STRING. */
1732
1733 static struct text_pos
1734 string_pos (ptrdiff_t charpos, Lisp_Object string)
1735 {
1736 struct text_pos pos;
1737 eassert (STRINGP (string));
1738 eassert (charpos >= 0);
1739 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1740 return pos;
1741 }
1742
1743
1744 /* Value is a text position, i.e. character and byte position, for
1745 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1746 means recognize multibyte characters. */
1747
1748 static struct text_pos
1749 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1750 {
1751 struct text_pos pos;
1752
1753 eassert (s != NULL);
1754 eassert (charpos >= 0);
1755
1756 if (multibyte_p)
1757 {
1758 int len;
1759
1760 SET_TEXT_POS (pos, 0, 0);
1761 while (charpos--)
1762 {
1763 string_char_and_length ((const unsigned char *) s, &len);
1764 s += len;
1765 CHARPOS (pos) += 1;
1766 BYTEPOS (pos) += len;
1767 }
1768 }
1769 else
1770 SET_TEXT_POS (pos, charpos, charpos);
1771
1772 return pos;
1773 }
1774
1775
1776 /* Value is the number of characters in C string S. MULTIBYTE_P
1777 non-zero means recognize multibyte characters. */
1778
1779 static ptrdiff_t
1780 number_of_chars (const char *s, bool multibyte_p)
1781 {
1782 ptrdiff_t nchars;
1783
1784 if (multibyte_p)
1785 {
1786 ptrdiff_t rest = strlen (s);
1787 int len;
1788 const unsigned char *p = (const unsigned char *) s;
1789
1790 for (nchars = 0; rest > 0; ++nchars)
1791 {
1792 string_char_and_length (p, &len);
1793 rest -= len, p += len;
1794 }
1795 }
1796 else
1797 nchars = strlen (s);
1798
1799 return nchars;
1800 }
1801
1802
1803 /* Compute byte position NEWPOS->bytepos corresponding to
1804 NEWPOS->charpos. POS is a known position in string STRING.
1805 NEWPOS->charpos must be >= POS.charpos. */
1806
1807 static void
1808 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1809 {
1810 eassert (STRINGP (string));
1811 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1812
1813 if (STRING_MULTIBYTE (string))
1814 *newpos = string_pos_nchars_ahead (pos, string,
1815 CHARPOS (*newpos) - CHARPOS (pos));
1816 else
1817 BYTEPOS (*newpos) = CHARPOS (*newpos);
1818 }
1819
1820 /* EXPORT:
1821 Return an estimation of the pixel height of mode or header lines on
1822 frame F. FACE_ID specifies what line's height to estimate. */
1823
1824 int
1825 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1826 {
1827 #ifdef HAVE_WINDOW_SYSTEM
1828 if (FRAME_WINDOW_P (f))
1829 {
1830 int height = FONT_HEIGHT (FRAME_FONT (f));
1831
1832 /* This function is called so early when Emacs starts that the face
1833 cache and mode line face are not yet initialized. */
1834 if (FRAME_FACE_CACHE (f))
1835 {
1836 struct face *face = FACE_FROM_ID (f, face_id);
1837 if (face)
1838 {
1839 if (face->font)
1840 height = FONT_HEIGHT (face->font);
1841 if (face->box_line_width > 0)
1842 height += 2 * face->box_line_width;
1843 }
1844 }
1845
1846 return height;
1847 }
1848 #endif
1849
1850 return 1;
1851 }
1852
1853 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1854 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1855 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1856 not force the value into range. */
1857
1858 void
1859 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1860 int *x, int *y, NativeRectangle *bounds, int noclip)
1861 {
1862
1863 #ifdef HAVE_WINDOW_SYSTEM
1864 if (FRAME_WINDOW_P (f))
1865 {
1866 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1867 even for negative values. */
1868 if (pix_x < 0)
1869 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1870 if (pix_y < 0)
1871 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1872
1873 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1874 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1875
1876 if (bounds)
1877 STORE_NATIVE_RECT (*bounds,
1878 FRAME_COL_TO_PIXEL_X (f, pix_x),
1879 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1880 FRAME_COLUMN_WIDTH (f) - 1,
1881 FRAME_LINE_HEIGHT (f) - 1);
1882
1883 if (!noclip)
1884 {
1885 if (pix_x < 0)
1886 pix_x = 0;
1887 else if (pix_x > FRAME_TOTAL_COLS (f))
1888 pix_x = FRAME_TOTAL_COLS (f);
1889
1890 if (pix_y < 0)
1891 pix_y = 0;
1892 else if (pix_y > FRAME_LINES (f))
1893 pix_y = FRAME_LINES (f);
1894 }
1895 }
1896 #endif
1897
1898 *x = pix_x;
1899 *y = pix_y;
1900 }
1901
1902
1903 /* Find the glyph under window-relative coordinates X/Y in window W.
1904 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1905 strings. Return in *HPOS and *VPOS the row and column number of
1906 the glyph found. Return in *AREA the glyph area containing X.
1907 Value is a pointer to the glyph found or null if X/Y is not on
1908 text, or we can't tell because W's current matrix is not up to
1909 date. */
1910
1911 static
1912 struct glyph *
1913 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1914 int *dx, int *dy, int *area)
1915 {
1916 struct glyph *glyph, *end;
1917 struct glyph_row *row = NULL;
1918 int x0, i;
1919
1920 /* Find row containing Y. Give up if some row is not enabled. */
1921 for (i = 0; i < w->current_matrix->nrows; ++i)
1922 {
1923 row = MATRIX_ROW (w->current_matrix, i);
1924 if (!row->enabled_p)
1925 return NULL;
1926 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1927 break;
1928 }
1929
1930 *vpos = i;
1931 *hpos = 0;
1932
1933 /* Give up if Y is not in the window. */
1934 if (i == w->current_matrix->nrows)
1935 return NULL;
1936
1937 /* Get the glyph area containing X. */
1938 if (w->pseudo_window_p)
1939 {
1940 *area = TEXT_AREA;
1941 x0 = 0;
1942 }
1943 else
1944 {
1945 if (x < window_box_left_offset (w, TEXT_AREA))
1946 {
1947 *area = LEFT_MARGIN_AREA;
1948 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1949 }
1950 else if (x < window_box_right_offset (w, TEXT_AREA))
1951 {
1952 *area = TEXT_AREA;
1953 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1954 }
1955 else
1956 {
1957 *area = RIGHT_MARGIN_AREA;
1958 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1959 }
1960 }
1961
1962 /* Find glyph containing X. */
1963 glyph = row->glyphs[*area];
1964 end = glyph + row->used[*area];
1965 x -= x0;
1966 while (glyph < end && x >= glyph->pixel_width)
1967 {
1968 x -= glyph->pixel_width;
1969 ++glyph;
1970 }
1971
1972 if (glyph == end)
1973 return NULL;
1974
1975 if (dx)
1976 {
1977 *dx = x;
1978 *dy = y - (row->y + row->ascent - glyph->ascent);
1979 }
1980
1981 *hpos = glyph - row->glyphs[*area];
1982 return glyph;
1983 }
1984
1985 /* Convert frame-relative x/y to coordinates relative to window W.
1986 Takes pseudo-windows into account. */
1987
1988 static void
1989 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1990 {
1991 if (w->pseudo_window_p)
1992 {
1993 /* A pseudo-window is always full-width, and starts at the
1994 left edge of the frame, plus a frame border. */
1995 struct frame *f = XFRAME (w->frame);
1996 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1997 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1998 }
1999 else
2000 {
2001 *x -= WINDOW_LEFT_EDGE_X (w);
2002 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2003 }
2004 }
2005
2006 #ifdef HAVE_WINDOW_SYSTEM
2007
2008 /* EXPORT:
2009 Return in RECTS[] at most N clipping rectangles for glyph string S.
2010 Return the number of stored rectangles. */
2011
2012 int
2013 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2014 {
2015 XRectangle r;
2016
2017 if (n <= 0)
2018 return 0;
2019
2020 if (s->row->full_width_p)
2021 {
2022 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2023 r.x = WINDOW_LEFT_EDGE_X (s->w);
2024 r.width = WINDOW_TOTAL_WIDTH (s->w);
2025
2026 /* Unless displaying a mode or menu bar line, which are always
2027 fully visible, clip to the visible part of the row. */
2028 if (s->w->pseudo_window_p)
2029 r.height = s->row->visible_height;
2030 else
2031 r.height = s->height;
2032 }
2033 else
2034 {
2035 /* This is a text line that may be partially visible. */
2036 r.x = window_box_left (s->w, s->area);
2037 r.width = window_box_width (s->w, s->area);
2038 r.height = s->row->visible_height;
2039 }
2040
2041 if (s->clip_head)
2042 if (r.x < s->clip_head->x)
2043 {
2044 if (r.width >= s->clip_head->x - r.x)
2045 r.width -= s->clip_head->x - r.x;
2046 else
2047 r.width = 0;
2048 r.x = s->clip_head->x;
2049 }
2050 if (s->clip_tail)
2051 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2052 {
2053 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2054 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2055 else
2056 r.width = 0;
2057 }
2058
2059 /* If S draws overlapping rows, it's sufficient to use the top and
2060 bottom of the window for clipping because this glyph string
2061 intentionally draws over other lines. */
2062 if (s->for_overlaps)
2063 {
2064 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2065 r.height = window_text_bottom_y (s->w) - r.y;
2066
2067 /* Alas, the above simple strategy does not work for the
2068 environments with anti-aliased text: if the same text is
2069 drawn onto the same place multiple times, it gets thicker.
2070 If the overlap we are processing is for the erased cursor, we
2071 take the intersection with the rectangle of the cursor. */
2072 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2073 {
2074 XRectangle rc, r_save = r;
2075
2076 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2077 rc.y = s->w->phys_cursor.y;
2078 rc.width = s->w->phys_cursor_width;
2079 rc.height = s->w->phys_cursor_height;
2080
2081 x_intersect_rectangles (&r_save, &rc, &r);
2082 }
2083 }
2084 else
2085 {
2086 /* Don't use S->y for clipping because it doesn't take partially
2087 visible lines into account. For example, it can be negative for
2088 partially visible lines at the top of a window. */
2089 if (!s->row->full_width_p
2090 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2091 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2092 else
2093 r.y = max (0, s->row->y);
2094 }
2095
2096 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2097
2098 /* If drawing the cursor, don't let glyph draw outside its
2099 advertised boundaries. Cleartype does this under some circumstances. */
2100 if (s->hl == DRAW_CURSOR)
2101 {
2102 struct glyph *glyph = s->first_glyph;
2103 int height, max_y;
2104
2105 if (s->x > r.x)
2106 {
2107 r.width -= s->x - r.x;
2108 r.x = s->x;
2109 }
2110 r.width = min (r.width, glyph->pixel_width);
2111
2112 /* If r.y is below window bottom, ensure that we still see a cursor. */
2113 height = min (glyph->ascent + glyph->descent,
2114 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2115 max_y = window_text_bottom_y (s->w) - height;
2116 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2117 if (s->ybase - glyph->ascent > max_y)
2118 {
2119 r.y = max_y;
2120 r.height = height;
2121 }
2122 else
2123 {
2124 /* Don't draw cursor glyph taller than our actual glyph. */
2125 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2126 if (height < r.height)
2127 {
2128 max_y = r.y + r.height;
2129 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2130 r.height = min (max_y - r.y, height);
2131 }
2132 }
2133 }
2134
2135 if (s->row->clip)
2136 {
2137 XRectangle r_save = r;
2138
2139 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2140 r.width = 0;
2141 }
2142
2143 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2144 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2145 {
2146 #ifdef CONVERT_FROM_XRECT
2147 CONVERT_FROM_XRECT (r, *rects);
2148 #else
2149 *rects = r;
2150 #endif
2151 return 1;
2152 }
2153 else
2154 {
2155 /* If we are processing overlapping and allowed to return
2156 multiple clipping rectangles, we exclude the row of the glyph
2157 string from the clipping rectangle. This is to avoid drawing
2158 the same text on the environment with anti-aliasing. */
2159 #ifdef CONVERT_FROM_XRECT
2160 XRectangle rs[2];
2161 #else
2162 XRectangle *rs = rects;
2163 #endif
2164 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2165
2166 if (s->for_overlaps & OVERLAPS_PRED)
2167 {
2168 rs[i] = r;
2169 if (r.y + r.height > row_y)
2170 {
2171 if (r.y < row_y)
2172 rs[i].height = row_y - r.y;
2173 else
2174 rs[i].height = 0;
2175 }
2176 i++;
2177 }
2178 if (s->for_overlaps & OVERLAPS_SUCC)
2179 {
2180 rs[i] = r;
2181 if (r.y < row_y + s->row->visible_height)
2182 {
2183 if (r.y + r.height > row_y + s->row->visible_height)
2184 {
2185 rs[i].y = row_y + s->row->visible_height;
2186 rs[i].height = r.y + r.height - rs[i].y;
2187 }
2188 else
2189 rs[i].height = 0;
2190 }
2191 i++;
2192 }
2193
2194 n = i;
2195 #ifdef CONVERT_FROM_XRECT
2196 for (i = 0; i < n; i++)
2197 CONVERT_FROM_XRECT (rs[i], rects[i]);
2198 #endif
2199 return n;
2200 }
2201 }
2202
2203 /* EXPORT:
2204 Return in *NR the clipping rectangle for glyph string S. */
2205
2206 void
2207 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2208 {
2209 get_glyph_string_clip_rects (s, nr, 1);
2210 }
2211
2212
2213 /* EXPORT:
2214 Return the position and height of the phys cursor in window W.
2215 Set w->phys_cursor_width to width of phys cursor.
2216 */
2217
2218 void
2219 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2220 struct glyph *glyph, int *xp, int *yp, int *heightp)
2221 {
2222 struct frame *f = XFRAME (WINDOW_FRAME (w));
2223 int x, y, wd, h, h0, y0;
2224
2225 /* Compute the width of the rectangle to draw. If on a stretch
2226 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2227 rectangle as wide as the glyph, but use a canonical character
2228 width instead. */
2229 wd = glyph->pixel_width - 1;
2230 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2231 wd++; /* Why? */
2232 #endif
2233
2234 x = w->phys_cursor.x;
2235 if (x < 0)
2236 {
2237 wd += x;
2238 x = 0;
2239 }
2240
2241 if (glyph->type == STRETCH_GLYPH
2242 && !x_stretch_cursor_p)
2243 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2244 w->phys_cursor_width = wd;
2245
2246 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2247
2248 /* If y is below window bottom, ensure that we still see a cursor. */
2249 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2250
2251 h = max (h0, glyph->ascent + glyph->descent);
2252 h0 = min (h0, glyph->ascent + glyph->descent);
2253
2254 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2255 if (y < y0)
2256 {
2257 h = max (h - (y0 - y) + 1, h0);
2258 y = y0 - 1;
2259 }
2260 else
2261 {
2262 y0 = window_text_bottom_y (w) - h0;
2263 if (y > y0)
2264 {
2265 h += y - y0;
2266 y = y0;
2267 }
2268 }
2269
2270 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2271 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2272 *heightp = h;
2273 }
2274
2275 /*
2276 * Remember which glyph the mouse is over.
2277 */
2278
2279 void
2280 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2281 {
2282 Lisp_Object window;
2283 struct window *w;
2284 struct glyph_row *r, *gr, *end_row;
2285 enum window_part part;
2286 enum glyph_row_area area;
2287 int x, y, width, height;
2288
2289 /* Try to determine frame pixel position and size of the glyph under
2290 frame pixel coordinates X/Y on frame F. */
2291
2292 if (!f->glyphs_initialized_p
2293 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2294 NILP (window)))
2295 {
2296 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2297 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2298 goto virtual_glyph;
2299 }
2300
2301 w = XWINDOW (window);
2302 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2303 height = WINDOW_FRAME_LINE_HEIGHT (w);
2304
2305 x = window_relative_x_coord (w, part, gx);
2306 y = gy - WINDOW_TOP_EDGE_Y (w);
2307
2308 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2309 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2310
2311 if (w->pseudo_window_p)
2312 {
2313 area = TEXT_AREA;
2314 part = ON_MODE_LINE; /* Don't adjust margin. */
2315 goto text_glyph;
2316 }
2317
2318 switch (part)
2319 {
2320 case ON_LEFT_MARGIN:
2321 area = LEFT_MARGIN_AREA;
2322 goto text_glyph;
2323
2324 case ON_RIGHT_MARGIN:
2325 area = RIGHT_MARGIN_AREA;
2326 goto text_glyph;
2327
2328 case ON_HEADER_LINE:
2329 case ON_MODE_LINE:
2330 gr = (part == ON_HEADER_LINE
2331 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2332 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2333 gy = gr->y;
2334 area = TEXT_AREA;
2335 goto text_glyph_row_found;
2336
2337 case ON_TEXT:
2338 area = TEXT_AREA;
2339
2340 text_glyph:
2341 gr = 0; gy = 0;
2342 for (; r <= end_row && r->enabled_p; ++r)
2343 if (r->y + r->height > y)
2344 {
2345 gr = r; gy = r->y;
2346 break;
2347 }
2348
2349 text_glyph_row_found:
2350 if (gr && gy <= y)
2351 {
2352 struct glyph *g = gr->glyphs[area];
2353 struct glyph *end = g + gr->used[area];
2354
2355 height = gr->height;
2356 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2357 if (gx + g->pixel_width > x)
2358 break;
2359
2360 if (g < end)
2361 {
2362 if (g->type == IMAGE_GLYPH)
2363 {
2364 /* Don't remember when mouse is over image, as
2365 image may have hot-spots. */
2366 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2367 return;
2368 }
2369 width = g->pixel_width;
2370 }
2371 else
2372 {
2373 /* Use nominal char spacing at end of line. */
2374 x -= gx;
2375 gx += (x / width) * width;
2376 }
2377
2378 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2379 gx += window_box_left_offset (w, area);
2380 }
2381 else
2382 {
2383 /* Use nominal line height at end of window. */
2384 gx = (x / width) * width;
2385 y -= gy;
2386 gy += (y / height) * height;
2387 }
2388 break;
2389
2390 case ON_LEFT_FRINGE:
2391 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2392 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2393 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2394 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2395 goto row_glyph;
2396
2397 case ON_RIGHT_FRINGE:
2398 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2399 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2400 : window_box_right_offset (w, TEXT_AREA));
2401 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2402 goto row_glyph;
2403
2404 case ON_SCROLL_BAR:
2405 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2406 ? 0
2407 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2408 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2409 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2410 : 0)));
2411 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2412
2413 row_glyph:
2414 gr = 0, gy = 0;
2415 for (; r <= end_row && r->enabled_p; ++r)
2416 if (r->y + r->height > y)
2417 {
2418 gr = r; gy = r->y;
2419 break;
2420 }
2421
2422 if (gr && gy <= y)
2423 height = gr->height;
2424 else
2425 {
2426 /* Use nominal line height at end of window. */
2427 y -= gy;
2428 gy += (y / height) * height;
2429 }
2430 break;
2431
2432 default:
2433 ;
2434 virtual_glyph:
2435 /* If there is no glyph under the mouse, then we divide the screen
2436 into a grid of the smallest glyph in the frame, and use that
2437 as our "glyph". */
2438
2439 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2440 round down even for negative values. */
2441 if (gx < 0)
2442 gx -= width - 1;
2443 if (gy < 0)
2444 gy -= height - 1;
2445
2446 gx = (gx / width) * width;
2447 gy = (gy / height) * height;
2448
2449 goto store_rect;
2450 }
2451
2452 gx += WINDOW_LEFT_EDGE_X (w);
2453 gy += WINDOW_TOP_EDGE_Y (w);
2454
2455 store_rect:
2456 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2457
2458 /* Visible feedback for debugging. */
2459 #if 0
2460 #if HAVE_X_WINDOWS
2461 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2462 f->output_data.x->normal_gc,
2463 gx, gy, width, height);
2464 #endif
2465 #endif
2466 }
2467
2468
2469 #endif /* HAVE_WINDOW_SYSTEM */
2470
2471 static void
2472 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2473 {
2474 eassert (w);
2475 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2476 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2477 w->window_end_vpos
2478 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2479 }
2480
2481 /***********************************************************************
2482 Lisp form evaluation
2483 ***********************************************************************/
2484
2485 /* Error handler for safe_eval and safe_call. */
2486
2487 static Lisp_Object
2488 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2489 {
2490 add_to_log ("Error during redisplay: %S signaled %S",
2491 Flist (nargs, args), arg);
2492 return Qnil;
2493 }
2494
2495 /* Call function FUNC with the rest of NARGS - 1 arguments
2496 following. Return the result, or nil if something went
2497 wrong. Prevent redisplay during the evaluation. */
2498
2499 Lisp_Object
2500 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2501 {
2502 Lisp_Object val;
2503
2504 if (inhibit_eval_during_redisplay)
2505 val = Qnil;
2506 else
2507 {
2508 va_list ap;
2509 ptrdiff_t i;
2510 ptrdiff_t count = SPECPDL_INDEX ();
2511 struct gcpro gcpro1;
2512 Lisp_Object *args = alloca (nargs * word_size);
2513
2514 args[0] = func;
2515 va_start (ap, func);
2516 for (i = 1; i < nargs; i++)
2517 args[i] = va_arg (ap, Lisp_Object);
2518 va_end (ap);
2519
2520 GCPRO1 (args[0]);
2521 gcpro1.nvars = nargs;
2522 specbind (Qinhibit_redisplay, Qt);
2523 /* Use Qt to ensure debugger does not run,
2524 so there is no possibility of wanting to redisplay. */
2525 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2526 safe_eval_handler);
2527 UNGCPRO;
2528 val = unbind_to (count, val);
2529 }
2530
2531 return val;
2532 }
2533
2534
2535 /* Call function FN with one argument ARG.
2536 Return the result, or nil if something went wrong. */
2537
2538 Lisp_Object
2539 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2540 {
2541 return safe_call (2, fn, arg);
2542 }
2543
2544 static Lisp_Object Qeval;
2545
2546 Lisp_Object
2547 safe_eval (Lisp_Object sexpr)
2548 {
2549 return safe_call1 (Qeval, sexpr);
2550 }
2551
2552 /* Call function FN with two arguments ARG1 and ARG2.
2553 Return the result, or nil if something went wrong. */
2554
2555 Lisp_Object
2556 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2557 {
2558 return safe_call (3, fn, arg1, arg2);
2559 }
2560
2561
2562 \f
2563 /***********************************************************************
2564 Debugging
2565 ***********************************************************************/
2566
2567 #if 0
2568
2569 /* Define CHECK_IT to perform sanity checks on iterators.
2570 This is for debugging. It is too slow to do unconditionally. */
2571
2572 static void
2573 check_it (struct it *it)
2574 {
2575 if (it->method == GET_FROM_STRING)
2576 {
2577 eassert (STRINGP (it->string));
2578 eassert (IT_STRING_CHARPOS (*it) >= 0);
2579 }
2580 else
2581 {
2582 eassert (IT_STRING_CHARPOS (*it) < 0);
2583 if (it->method == GET_FROM_BUFFER)
2584 {
2585 /* Check that character and byte positions agree. */
2586 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2587 }
2588 }
2589
2590 if (it->dpvec)
2591 eassert (it->current.dpvec_index >= 0);
2592 else
2593 eassert (it->current.dpvec_index < 0);
2594 }
2595
2596 #define CHECK_IT(IT) check_it ((IT))
2597
2598 #else /* not 0 */
2599
2600 #define CHECK_IT(IT) (void) 0
2601
2602 #endif /* not 0 */
2603
2604
2605 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2606
2607 /* Check that the window end of window W is what we expect it
2608 to be---the last row in the current matrix displaying text. */
2609
2610 static void
2611 check_window_end (struct window *w)
2612 {
2613 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2614 {
2615 struct glyph_row *row;
2616 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2617 !row->enabled_p
2618 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2619 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2620 }
2621 }
2622
2623 #define CHECK_WINDOW_END(W) check_window_end ((W))
2624
2625 #else
2626
2627 #define CHECK_WINDOW_END(W) (void) 0
2628
2629 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2630
2631 /* Return mark position if current buffer has the region of non-zero length,
2632 or -1 otherwise. */
2633
2634 static ptrdiff_t
2635 markpos_of_region (void)
2636 {
2637 if (!NILP (Vtransient_mark_mode)
2638 && !NILP (BVAR (current_buffer, mark_active))
2639 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2640 {
2641 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2642
2643 if (markpos != PT)
2644 return markpos;
2645 }
2646 return -1;
2647 }
2648
2649 /***********************************************************************
2650 Iterator initialization
2651 ***********************************************************************/
2652
2653 /* Initialize IT for displaying current_buffer in window W, starting
2654 at character position CHARPOS. CHARPOS < 0 means that no buffer
2655 position is specified which is useful when the iterator is assigned
2656 a position later. BYTEPOS is the byte position corresponding to
2657 CHARPOS.
2658
2659 If ROW is not null, calls to produce_glyphs with IT as parameter
2660 will produce glyphs in that row.
2661
2662 BASE_FACE_ID is the id of a base face to use. It must be one of
2663 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2664 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2665 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2666
2667 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2668 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2669 will be initialized to use the corresponding mode line glyph row of
2670 the desired matrix of W. */
2671
2672 void
2673 init_iterator (struct it *it, struct window *w,
2674 ptrdiff_t charpos, ptrdiff_t bytepos,
2675 struct glyph_row *row, enum face_id base_face_id)
2676 {
2677 ptrdiff_t markpos;
2678 enum face_id remapped_base_face_id = base_face_id;
2679
2680 /* Some precondition checks. */
2681 eassert (w != NULL && it != NULL);
2682 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2683 && charpos <= ZV));
2684
2685 /* If face attributes have been changed since the last redisplay,
2686 free realized faces now because they depend on face definitions
2687 that might have changed. Don't free faces while there might be
2688 desired matrices pending which reference these faces. */
2689 if (face_change_count && !inhibit_free_realized_faces)
2690 {
2691 face_change_count = 0;
2692 free_all_realized_faces (Qnil);
2693 }
2694
2695 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2696 if (! NILP (Vface_remapping_alist))
2697 remapped_base_face_id
2698 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2699
2700 /* Use one of the mode line rows of W's desired matrix if
2701 appropriate. */
2702 if (row == NULL)
2703 {
2704 if (base_face_id == MODE_LINE_FACE_ID
2705 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2706 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2707 else if (base_face_id == HEADER_LINE_FACE_ID)
2708 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2709 }
2710
2711 /* Clear IT. */
2712 memset (it, 0, sizeof *it);
2713 it->current.overlay_string_index = -1;
2714 it->current.dpvec_index = -1;
2715 it->base_face_id = remapped_base_face_id;
2716 it->string = Qnil;
2717 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2718 it->paragraph_embedding = L2R;
2719 it->bidi_it.string.lstring = Qnil;
2720 it->bidi_it.string.s = NULL;
2721 it->bidi_it.string.bufpos = 0;
2722 it->bidi_it.w = w;
2723
2724 /* The window in which we iterate over current_buffer: */
2725 XSETWINDOW (it->window, w);
2726 it->w = w;
2727 it->f = XFRAME (w->frame);
2728
2729 it->cmp_it.id = -1;
2730
2731 /* Extra space between lines (on window systems only). */
2732 if (base_face_id == DEFAULT_FACE_ID
2733 && FRAME_WINDOW_P (it->f))
2734 {
2735 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2736 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2737 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2738 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2739 * FRAME_LINE_HEIGHT (it->f));
2740 else if (it->f->extra_line_spacing > 0)
2741 it->extra_line_spacing = it->f->extra_line_spacing;
2742 it->max_extra_line_spacing = 0;
2743 }
2744
2745 /* If realized faces have been removed, e.g. because of face
2746 attribute changes of named faces, recompute them. When running
2747 in batch mode, the face cache of the initial frame is null. If
2748 we happen to get called, make a dummy face cache. */
2749 if (FRAME_FACE_CACHE (it->f) == NULL)
2750 init_frame_faces (it->f);
2751 if (FRAME_FACE_CACHE (it->f)->used == 0)
2752 recompute_basic_faces (it->f);
2753
2754 /* Current value of the `slice', `space-width', and 'height' properties. */
2755 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2756 it->space_width = Qnil;
2757 it->font_height = Qnil;
2758 it->override_ascent = -1;
2759
2760 /* Are control characters displayed as `^C'? */
2761 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2762
2763 /* -1 means everything between a CR and the following line end
2764 is invisible. >0 means lines indented more than this value are
2765 invisible. */
2766 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2767 ? (clip_to_bounds
2768 (-1, XINT (BVAR (current_buffer, selective_display)),
2769 PTRDIFF_MAX))
2770 : (!NILP (BVAR (current_buffer, selective_display))
2771 ? -1 : 0));
2772 it->selective_display_ellipsis_p
2773 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2774
2775 /* Display table to use. */
2776 it->dp = window_display_table (w);
2777
2778 /* Are multibyte characters enabled in current_buffer? */
2779 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2780
2781 /* If visible region is of non-zero length, set IT->region_beg_charpos
2782 and IT->region_end_charpos to the start and end of a visible region
2783 in window IT->w. Set both to -1 to indicate no region. */
2784 markpos = markpos_of_region ();
2785 if (markpos >= 0
2786 /* Maybe highlight only in selected window. */
2787 && (/* Either show region everywhere. */
2788 highlight_nonselected_windows
2789 /* Or show region in the selected window. */
2790 || w == XWINDOW (selected_window)
2791 /* Or show the region if we are in the mini-buffer and W is
2792 the window the mini-buffer refers to. */
2793 || (MINI_WINDOW_P (XWINDOW (selected_window))
2794 && WINDOWP (minibuf_selected_window)
2795 && w == XWINDOW (minibuf_selected_window))))
2796 {
2797 it->region_beg_charpos = min (PT, markpos);
2798 it->region_end_charpos = max (PT, markpos);
2799 }
2800 else
2801 it->region_beg_charpos = it->region_end_charpos = -1;
2802
2803 /* Get the position at which the redisplay_end_trigger hook should
2804 be run, if it is to be run at all. */
2805 if (MARKERP (w->redisplay_end_trigger)
2806 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2807 it->redisplay_end_trigger_charpos
2808 = marker_position (w->redisplay_end_trigger);
2809 else if (INTEGERP (w->redisplay_end_trigger))
2810 it->redisplay_end_trigger_charpos =
2811 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2812
2813 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2814
2815 /* Are lines in the display truncated? */
2816 if (base_face_id != DEFAULT_FACE_ID
2817 || it->w->hscroll
2818 || (! WINDOW_FULL_WIDTH_P (it->w)
2819 && ((!NILP (Vtruncate_partial_width_windows)
2820 && !INTEGERP (Vtruncate_partial_width_windows))
2821 || (INTEGERP (Vtruncate_partial_width_windows)
2822 && (WINDOW_TOTAL_COLS (it->w)
2823 < XINT (Vtruncate_partial_width_windows))))))
2824 it->line_wrap = TRUNCATE;
2825 else if (NILP (BVAR (current_buffer, truncate_lines)))
2826 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2827 ? WINDOW_WRAP : WORD_WRAP;
2828 else
2829 it->line_wrap = TRUNCATE;
2830
2831 /* Get dimensions of truncation and continuation glyphs. These are
2832 displayed as fringe bitmaps under X, but we need them for such
2833 frames when the fringes are turned off. But leave the dimensions
2834 zero for tooltip frames, as these glyphs look ugly there and also
2835 sabotage calculations of tooltip dimensions in x-show-tip. */
2836 #ifdef HAVE_WINDOW_SYSTEM
2837 if (!(FRAME_WINDOW_P (it->f)
2838 && FRAMEP (tip_frame)
2839 && it->f == XFRAME (tip_frame)))
2840 #endif
2841 {
2842 if (it->line_wrap == TRUNCATE)
2843 {
2844 /* We will need the truncation glyph. */
2845 eassert (it->glyph_row == NULL);
2846 produce_special_glyphs (it, IT_TRUNCATION);
2847 it->truncation_pixel_width = it->pixel_width;
2848 }
2849 else
2850 {
2851 /* We will need the continuation glyph. */
2852 eassert (it->glyph_row == NULL);
2853 produce_special_glyphs (it, IT_CONTINUATION);
2854 it->continuation_pixel_width = it->pixel_width;
2855 }
2856 }
2857
2858 /* Reset these values to zero because the produce_special_glyphs
2859 above has changed them. */
2860 it->pixel_width = it->ascent = it->descent = 0;
2861 it->phys_ascent = it->phys_descent = 0;
2862
2863 /* Set this after getting the dimensions of truncation and
2864 continuation glyphs, so that we don't produce glyphs when calling
2865 produce_special_glyphs, above. */
2866 it->glyph_row = row;
2867 it->area = TEXT_AREA;
2868
2869 /* Forget any previous info about this row being reversed. */
2870 if (it->glyph_row)
2871 it->glyph_row->reversed_p = 0;
2872
2873 /* Get the dimensions of the display area. The display area
2874 consists of the visible window area plus a horizontally scrolled
2875 part to the left of the window. All x-values are relative to the
2876 start of this total display area. */
2877 if (base_face_id != DEFAULT_FACE_ID)
2878 {
2879 /* Mode lines, menu bar in terminal frames. */
2880 it->first_visible_x = 0;
2881 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2882 }
2883 else
2884 {
2885 it->first_visible_x =
2886 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2887 it->last_visible_x = (it->first_visible_x
2888 + window_box_width (w, TEXT_AREA));
2889
2890 /* If we truncate lines, leave room for the truncation glyph(s) at
2891 the right margin. Otherwise, leave room for the continuation
2892 glyph(s). Done only if the window has no fringes. Since we
2893 don't know at this point whether there will be any R2L lines in
2894 the window, we reserve space for truncation/continuation glyphs
2895 even if only one of the fringes is absent. */
2896 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2897 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2898 {
2899 if (it->line_wrap == TRUNCATE)
2900 it->last_visible_x -= it->truncation_pixel_width;
2901 else
2902 it->last_visible_x -= it->continuation_pixel_width;
2903 }
2904
2905 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2906 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2907 }
2908
2909 /* Leave room for a border glyph. */
2910 if (!FRAME_WINDOW_P (it->f)
2911 && !WINDOW_RIGHTMOST_P (it->w))
2912 it->last_visible_x -= 1;
2913
2914 it->last_visible_y = window_text_bottom_y (w);
2915
2916 /* For mode lines and alike, arrange for the first glyph having a
2917 left box line if the face specifies a box. */
2918 if (base_face_id != DEFAULT_FACE_ID)
2919 {
2920 struct face *face;
2921
2922 it->face_id = remapped_base_face_id;
2923
2924 /* If we have a boxed mode line, make the first character appear
2925 with a left box line. */
2926 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2927 if (face->box != FACE_NO_BOX)
2928 it->start_of_box_run_p = 1;
2929 }
2930
2931 /* If a buffer position was specified, set the iterator there,
2932 getting overlays and face properties from that position. */
2933 if (charpos >= BUF_BEG (current_buffer))
2934 {
2935 it->end_charpos = ZV;
2936 eassert (charpos == BYTE_TO_CHAR (bytepos));
2937 IT_CHARPOS (*it) = charpos;
2938 IT_BYTEPOS (*it) = bytepos;
2939
2940 /* We will rely on `reseat' to set this up properly, via
2941 handle_face_prop. */
2942 it->face_id = it->base_face_id;
2943
2944 it->start = it->current;
2945 /* Do we need to reorder bidirectional text? Not if this is a
2946 unibyte buffer: by definition, none of the single-byte
2947 characters are strong R2L, so no reordering is needed. And
2948 bidi.c doesn't support unibyte buffers anyway. Also, don't
2949 reorder while we are loading loadup.el, since the tables of
2950 character properties needed for reordering are not yet
2951 available. */
2952 it->bidi_p =
2953 NILP (Vpurify_flag)
2954 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2955 && it->multibyte_p;
2956
2957 /* If we are to reorder bidirectional text, init the bidi
2958 iterator. */
2959 if (it->bidi_p)
2960 {
2961 /* Note the paragraph direction that this buffer wants to
2962 use. */
2963 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2964 Qleft_to_right))
2965 it->paragraph_embedding = L2R;
2966 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2967 Qright_to_left))
2968 it->paragraph_embedding = R2L;
2969 else
2970 it->paragraph_embedding = NEUTRAL_DIR;
2971 bidi_unshelve_cache (NULL, 0);
2972 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2973 &it->bidi_it);
2974 }
2975
2976 /* Compute faces etc. */
2977 reseat (it, it->current.pos, 1);
2978 }
2979
2980 CHECK_IT (it);
2981 }
2982
2983
2984 /* Initialize IT for the display of window W with window start POS. */
2985
2986 void
2987 start_display (struct it *it, struct window *w, struct text_pos pos)
2988 {
2989 struct glyph_row *row;
2990 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2991
2992 row = w->desired_matrix->rows + first_vpos;
2993 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2994 it->first_vpos = first_vpos;
2995
2996 /* Don't reseat to previous visible line start if current start
2997 position is in a string or image. */
2998 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2999 {
3000 int start_at_line_beg_p;
3001 int first_y = it->current_y;
3002
3003 /* If window start is not at a line start, skip forward to POS to
3004 get the correct continuation lines width. */
3005 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3006 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3007 if (!start_at_line_beg_p)
3008 {
3009 int new_x;
3010
3011 reseat_at_previous_visible_line_start (it);
3012 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3013
3014 new_x = it->current_x + it->pixel_width;
3015
3016 /* If lines are continued, this line may end in the middle
3017 of a multi-glyph character (e.g. a control character
3018 displayed as \003, or in the middle of an overlay
3019 string). In this case move_it_to above will not have
3020 taken us to the start of the continuation line but to the
3021 end of the continued line. */
3022 if (it->current_x > 0
3023 && it->line_wrap != TRUNCATE /* Lines are continued. */
3024 && (/* And glyph doesn't fit on the line. */
3025 new_x > it->last_visible_x
3026 /* Or it fits exactly and we're on a window
3027 system frame. */
3028 || (new_x == it->last_visible_x
3029 && FRAME_WINDOW_P (it->f)
3030 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3031 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3032 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3033 {
3034 if ((it->current.dpvec_index >= 0
3035 || it->current.overlay_string_index >= 0)
3036 /* If we are on a newline from a display vector or
3037 overlay string, then we are already at the end of
3038 a screen line; no need to go to the next line in
3039 that case, as this line is not really continued.
3040 (If we do go to the next line, C-e will not DTRT.) */
3041 && it->c != '\n')
3042 {
3043 set_iterator_to_next (it, 1);
3044 move_it_in_display_line_to (it, -1, -1, 0);
3045 }
3046
3047 it->continuation_lines_width += it->current_x;
3048 }
3049 /* If the character at POS is displayed via a display
3050 vector, move_it_to above stops at the final glyph of
3051 IT->dpvec. To make the caller redisplay that character
3052 again (a.k.a. start at POS), we need to reset the
3053 dpvec_index to the beginning of IT->dpvec. */
3054 else if (it->current.dpvec_index >= 0)
3055 it->current.dpvec_index = 0;
3056
3057 /* We're starting a new display line, not affected by the
3058 height of the continued line, so clear the appropriate
3059 fields in the iterator structure. */
3060 it->max_ascent = it->max_descent = 0;
3061 it->max_phys_ascent = it->max_phys_descent = 0;
3062
3063 it->current_y = first_y;
3064 it->vpos = 0;
3065 it->current_x = it->hpos = 0;
3066 }
3067 }
3068 }
3069
3070
3071 /* Return 1 if POS is a position in ellipses displayed for invisible
3072 text. W is the window we display, for text property lookup. */
3073
3074 static int
3075 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3076 {
3077 Lisp_Object prop, window;
3078 int ellipses_p = 0;
3079 ptrdiff_t charpos = CHARPOS (pos->pos);
3080
3081 /* If POS specifies a position in a display vector, this might
3082 be for an ellipsis displayed for invisible text. We won't
3083 get the iterator set up for delivering that ellipsis unless
3084 we make sure that it gets aware of the invisible text. */
3085 if (pos->dpvec_index >= 0
3086 && pos->overlay_string_index < 0
3087 && CHARPOS (pos->string_pos) < 0
3088 && charpos > BEGV
3089 && (XSETWINDOW (window, w),
3090 prop = Fget_char_property (make_number (charpos),
3091 Qinvisible, window),
3092 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3093 {
3094 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3095 window);
3096 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3097 }
3098
3099 return ellipses_p;
3100 }
3101
3102
3103 /* Initialize IT for stepping through current_buffer in window W,
3104 starting at position POS that includes overlay string and display
3105 vector/ control character translation position information. Value
3106 is zero if there are overlay strings with newlines at POS. */
3107
3108 static int
3109 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3110 {
3111 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3112 int i, overlay_strings_with_newlines = 0;
3113
3114 /* If POS specifies a position in a display vector, this might
3115 be for an ellipsis displayed for invisible text. We won't
3116 get the iterator set up for delivering that ellipsis unless
3117 we make sure that it gets aware of the invisible text. */
3118 if (in_ellipses_for_invisible_text_p (pos, w))
3119 {
3120 --charpos;
3121 bytepos = 0;
3122 }
3123
3124 /* Keep in mind: the call to reseat in init_iterator skips invisible
3125 text, so we might end up at a position different from POS. This
3126 is only a problem when POS is a row start after a newline and an
3127 overlay starts there with an after-string, and the overlay has an
3128 invisible property. Since we don't skip invisible text in
3129 display_line and elsewhere immediately after consuming the
3130 newline before the row start, such a POS will not be in a string,
3131 but the call to init_iterator below will move us to the
3132 after-string. */
3133 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3134
3135 /* This only scans the current chunk -- it should scan all chunks.
3136 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3137 to 16 in 22.1 to make this a lesser problem. */
3138 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3139 {
3140 const char *s = SSDATA (it->overlay_strings[i]);
3141 const char *e = s + SBYTES (it->overlay_strings[i]);
3142
3143 while (s < e && *s != '\n')
3144 ++s;
3145
3146 if (s < e)
3147 {
3148 overlay_strings_with_newlines = 1;
3149 break;
3150 }
3151 }
3152
3153 /* If position is within an overlay string, set up IT to the right
3154 overlay string. */
3155 if (pos->overlay_string_index >= 0)
3156 {
3157 int relative_index;
3158
3159 /* If the first overlay string happens to have a `display'
3160 property for an image, the iterator will be set up for that
3161 image, and we have to undo that setup first before we can
3162 correct the overlay string index. */
3163 if (it->method == GET_FROM_IMAGE)
3164 pop_it (it);
3165
3166 /* We already have the first chunk of overlay strings in
3167 IT->overlay_strings. Load more until the one for
3168 pos->overlay_string_index is in IT->overlay_strings. */
3169 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3170 {
3171 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3172 it->current.overlay_string_index = 0;
3173 while (n--)
3174 {
3175 load_overlay_strings (it, 0);
3176 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3177 }
3178 }
3179
3180 it->current.overlay_string_index = pos->overlay_string_index;
3181 relative_index = (it->current.overlay_string_index
3182 % OVERLAY_STRING_CHUNK_SIZE);
3183 it->string = it->overlay_strings[relative_index];
3184 eassert (STRINGP (it->string));
3185 it->current.string_pos = pos->string_pos;
3186 it->method = GET_FROM_STRING;
3187 it->end_charpos = SCHARS (it->string);
3188 /* Set up the bidi iterator for this overlay string. */
3189 if (it->bidi_p)
3190 {
3191 it->bidi_it.string.lstring = it->string;
3192 it->bidi_it.string.s = NULL;
3193 it->bidi_it.string.schars = SCHARS (it->string);
3194 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3195 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3196 it->bidi_it.string.unibyte = !it->multibyte_p;
3197 it->bidi_it.w = it->w;
3198 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3199 FRAME_WINDOW_P (it->f), &it->bidi_it);
3200
3201 /* Synchronize the state of the bidi iterator with
3202 pos->string_pos. For any string position other than
3203 zero, this will be done automagically when we resume
3204 iteration over the string and get_visually_first_element
3205 is called. But if string_pos is zero, and the string is
3206 to be reordered for display, we need to resync manually,
3207 since it could be that the iteration state recorded in
3208 pos ended at string_pos of 0 moving backwards in string. */
3209 if (CHARPOS (pos->string_pos) == 0)
3210 {
3211 get_visually_first_element (it);
3212 if (IT_STRING_CHARPOS (*it) != 0)
3213 do {
3214 /* Paranoia. */
3215 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3216 bidi_move_to_visually_next (&it->bidi_it);
3217 } while (it->bidi_it.charpos != 0);
3218 }
3219 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3220 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3221 }
3222 }
3223
3224 if (CHARPOS (pos->string_pos) >= 0)
3225 {
3226 /* Recorded position is not in an overlay string, but in another
3227 string. This can only be a string from a `display' property.
3228 IT should already be filled with that string. */
3229 it->current.string_pos = pos->string_pos;
3230 eassert (STRINGP (it->string));
3231 if (it->bidi_p)
3232 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3233 FRAME_WINDOW_P (it->f), &it->bidi_it);
3234 }
3235
3236 /* Restore position in display vector translations, control
3237 character translations or ellipses. */
3238 if (pos->dpvec_index >= 0)
3239 {
3240 if (it->dpvec == NULL)
3241 get_next_display_element (it);
3242 eassert (it->dpvec && it->current.dpvec_index == 0);
3243 it->current.dpvec_index = pos->dpvec_index;
3244 }
3245
3246 CHECK_IT (it);
3247 return !overlay_strings_with_newlines;
3248 }
3249
3250
3251 /* Initialize IT for stepping through current_buffer in window W
3252 starting at ROW->start. */
3253
3254 static void
3255 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3256 {
3257 init_from_display_pos (it, w, &row->start);
3258 it->start = row->start;
3259 it->continuation_lines_width = row->continuation_lines_width;
3260 CHECK_IT (it);
3261 }
3262
3263
3264 /* Initialize IT for stepping through current_buffer in window W
3265 starting in the line following ROW, i.e. starting at ROW->end.
3266 Value is zero if there are overlay strings with newlines at ROW's
3267 end position. */
3268
3269 static int
3270 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3271 {
3272 int success = 0;
3273
3274 if (init_from_display_pos (it, w, &row->end))
3275 {
3276 if (row->continued_p)
3277 it->continuation_lines_width
3278 = row->continuation_lines_width + row->pixel_width;
3279 CHECK_IT (it);
3280 success = 1;
3281 }
3282
3283 return success;
3284 }
3285
3286
3287
3288 \f
3289 /***********************************************************************
3290 Text properties
3291 ***********************************************************************/
3292
3293 /* Called when IT reaches IT->stop_charpos. Handle text property and
3294 overlay changes. Set IT->stop_charpos to the next position where
3295 to stop. */
3296
3297 static void
3298 handle_stop (struct it *it)
3299 {
3300 enum prop_handled handled;
3301 int handle_overlay_change_p;
3302 struct props *p;
3303
3304 it->dpvec = NULL;
3305 it->current.dpvec_index = -1;
3306 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3307 it->ignore_overlay_strings_at_pos_p = 0;
3308 it->ellipsis_p = 0;
3309
3310 /* Use face of preceding text for ellipsis (if invisible) */
3311 if (it->selective_display_ellipsis_p)
3312 it->saved_face_id = it->face_id;
3313
3314 do
3315 {
3316 handled = HANDLED_NORMALLY;
3317
3318 /* Call text property handlers. */
3319 for (p = it_props; p->handler; ++p)
3320 {
3321 handled = p->handler (it);
3322
3323 if (handled == HANDLED_RECOMPUTE_PROPS)
3324 break;
3325 else if (handled == HANDLED_RETURN)
3326 {
3327 /* We still want to show before and after strings from
3328 overlays even if the actual buffer text is replaced. */
3329 if (!handle_overlay_change_p
3330 || it->sp > 1
3331 /* Don't call get_overlay_strings_1 if we already
3332 have overlay strings loaded, because doing so
3333 will load them again and push the iterator state
3334 onto the stack one more time, which is not
3335 expected by the rest of the code that processes
3336 overlay strings. */
3337 || (it->current.overlay_string_index < 0
3338 ? !get_overlay_strings_1 (it, 0, 0)
3339 : 0))
3340 {
3341 if (it->ellipsis_p)
3342 setup_for_ellipsis (it, 0);
3343 /* When handling a display spec, we might load an
3344 empty string. In that case, discard it here. We
3345 used to discard it in handle_single_display_spec,
3346 but that causes get_overlay_strings_1, above, to
3347 ignore overlay strings that we must check. */
3348 if (STRINGP (it->string) && !SCHARS (it->string))
3349 pop_it (it);
3350 return;
3351 }
3352 else if (STRINGP (it->string) && !SCHARS (it->string))
3353 pop_it (it);
3354 else
3355 {
3356 it->ignore_overlay_strings_at_pos_p = 1;
3357 it->string_from_display_prop_p = 0;
3358 it->from_disp_prop_p = 0;
3359 handle_overlay_change_p = 0;
3360 }
3361 handled = HANDLED_RECOMPUTE_PROPS;
3362 break;
3363 }
3364 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3365 handle_overlay_change_p = 0;
3366 }
3367
3368 if (handled != HANDLED_RECOMPUTE_PROPS)
3369 {
3370 /* Don't check for overlay strings below when set to deliver
3371 characters from a display vector. */
3372 if (it->method == GET_FROM_DISPLAY_VECTOR)
3373 handle_overlay_change_p = 0;
3374
3375 /* Handle overlay changes.
3376 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3377 if it finds overlays. */
3378 if (handle_overlay_change_p)
3379 handled = handle_overlay_change (it);
3380 }
3381
3382 if (it->ellipsis_p)
3383 {
3384 setup_for_ellipsis (it, 0);
3385 break;
3386 }
3387 }
3388 while (handled == HANDLED_RECOMPUTE_PROPS);
3389
3390 /* Determine where to stop next. */
3391 if (handled == HANDLED_NORMALLY)
3392 compute_stop_pos (it);
3393 }
3394
3395
3396 /* Compute IT->stop_charpos from text property and overlay change
3397 information for IT's current position. */
3398
3399 static void
3400 compute_stop_pos (struct it *it)
3401 {
3402 register INTERVAL iv, next_iv;
3403 Lisp_Object object, limit, position;
3404 ptrdiff_t charpos, bytepos;
3405
3406 if (STRINGP (it->string))
3407 {
3408 /* Strings are usually short, so don't limit the search for
3409 properties. */
3410 it->stop_charpos = it->end_charpos;
3411 object = it->string;
3412 limit = Qnil;
3413 charpos = IT_STRING_CHARPOS (*it);
3414 bytepos = IT_STRING_BYTEPOS (*it);
3415 }
3416 else
3417 {
3418 ptrdiff_t pos;
3419
3420 /* If end_charpos is out of range for some reason, such as a
3421 misbehaving display function, rationalize it (Bug#5984). */
3422 if (it->end_charpos > ZV)
3423 it->end_charpos = ZV;
3424 it->stop_charpos = it->end_charpos;
3425
3426 /* If next overlay change is in front of the current stop pos
3427 (which is IT->end_charpos), stop there. Note: value of
3428 next_overlay_change is point-max if no overlay change
3429 follows. */
3430 charpos = IT_CHARPOS (*it);
3431 bytepos = IT_BYTEPOS (*it);
3432 pos = next_overlay_change (charpos);
3433 if (pos < it->stop_charpos)
3434 it->stop_charpos = pos;
3435
3436 /* If showing the region, we have to stop at the region
3437 start or end because the face might change there. */
3438 if (it->region_beg_charpos > 0)
3439 {
3440 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3441 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3442 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3443 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3444 }
3445
3446 /* Set up variables for computing the stop position from text
3447 property changes. */
3448 XSETBUFFER (object, current_buffer);
3449 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3450 }
3451
3452 /* Get the interval containing IT's position. Value is a null
3453 interval if there isn't such an interval. */
3454 position = make_number (charpos);
3455 iv = validate_interval_range (object, &position, &position, 0);
3456 if (iv)
3457 {
3458 Lisp_Object values_here[LAST_PROP_IDX];
3459 struct props *p;
3460
3461 /* Get properties here. */
3462 for (p = it_props; p->handler; ++p)
3463 values_here[p->idx] = textget (iv->plist, *p->name);
3464
3465 /* Look for an interval following iv that has different
3466 properties. */
3467 for (next_iv = next_interval (iv);
3468 (next_iv
3469 && (NILP (limit)
3470 || XFASTINT (limit) > next_iv->position));
3471 next_iv = next_interval (next_iv))
3472 {
3473 for (p = it_props; p->handler; ++p)
3474 {
3475 Lisp_Object new_value;
3476
3477 new_value = textget (next_iv->plist, *p->name);
3478 if (!EQ (values_here[p->idx], new_value))
3479 break;
3480 }
3481
3482 if (p->handler)
3483 break;
3484 }
3485
3486 if (next_iv)
3487 {
3488 if (INTEGERP (limit)
3489 && next_iv->position >= XFASTINT (limit))
3490 /* No text property change up to limit. */
3491 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3492 else
3493 /* Text properties change in next_iv. */
3494 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3495 }
3496 }
3497
3498 if (it->cmp_it.id < 0)
3499 {
3500 ptrdiff_t stoppos = it->end_charpos;
3501
3502 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3503 stoppos = -1;
3504 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3505 stoppos, it->string);
3506 }
3507
3508 eassert (STRINGP (it->string)
3509 || (it->stop_charpos >= BEGV
3510 && it->stop_charpos >= IT_CHARPOS (*it)));
3511 }
3512
3513
3514 /* Return the position of the next overlay change after POS in
3515 current_buffer. Value is point-max if no overlay change
3516 follows. This is like `next-overlay-change' but doesn't use
3517 xmalloc. */
3518
3519 static ptrdiff_t
3520 next_overlay_change (ptrdiff_t pos)
3521 {
3522 ptrdiff_t i, noverlays;
3523 ptrdiff_t endpos;
3524 Lisp_Object *overlays;
3525
3526 /* Get all overlays at the given position. */
3527 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3528
3529 /* If any of these overlays ends before endpos,
3530 use its ending point instead. */
3531 for (i = 0; i < noverlays; ++i)
3532 {
3533 Lisp_Object oend;
3534 ptrdiff_t oendpos;
3535
3536 oend = OVERLAY_END (overlays[i]);
3537 oendpos = OVERLAY_POSITION (oend);
3538 endpos = min (endpos, oendpos);
3539 }
3540
3541 return endpos;
3542 }
3543
3544 /* How many characters forward to search for a display property or
3545 display string. Searching too far forward makes the bidi display
3546 sluggish, especially in small windows. */
3547 #define MAX_DISP_SCAN 250
3548
3549 /* Return the character position of a display string at or after
3550 position specified by POSITION. If no display string exists at or
3551 after POSITION, return ZV. A display string is either an overlay
3552 with `display' property whose value is a string, or a `display'
3553 text property whose value is a string. STRING is data about the
3554 string to iterate; if STRING->lstring is nil, we are iterating a
3555 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3556 on a GUI frame. DISP_PROP is set to zero if we searched
3557 MAX_DISP_SCAN characters forward without finding any display
3558 strings, non-zero otherwise. It is set to 2 if the display string
3559 uses any kind of `(space ...)' spec that will produce a stretch of
3560 white space in the text area. */
3561 ptrdiff_t
3562 compute_display_string_pos (struct text_pos *position,
3563 struct bidi_string_data *string,
3564 struct window *w,
3565 int frame_window_p, int *disp_prop)
3566 {
3567 /* OBJECT = nil means current buffer. */
3568 Lisp_Object object, object1;
3569 Lisp_Object pos, spec, limpos;
3570 int string_p = (string && (STRINGP (string->lstring) || string->s));
3571 ptrdiff_t eob = string_p ? string->schars : ZV;
3572 ptrdiff_t begb = string_p ? 0 : BEGV;
3573 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3574 ptrdiff_t lim =
3575 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3576 struct text_pos tpos;
3577 int rv = 0;
3578
3579 if (string && STRINGP (string->lstring))
3580 object1 = object = string->lstring;
3581 else if (w && !string_p)
3582 {
3583 XSETWINDOW (object, w);
3584 object1 = Qnil;
3585 }
3586 else
3587 object1 = object = Qnil;
3588
3589 *disp_prop = 1;
3590
3591 if (charpos >= eob
3592 /* We don't support display properties whose values are strings
3593 that have display string properties. */
3594 || string->from_disp_str
3595 /* C strings cannot have display properties. */
3596 || (string->s && !STRINGP (object)))
3597 {
3598 *disp_prop = 0;
3599 return eob;
3600 }
3601
3602 /* If the character at CHARPOS is where the display string begins,
3603 return CHARPOS. */
3604 pos = make_number (charpos);
3605 if (STRINGP (object))
3606 bufpos = string->bufpos;
3607 else
3608 bufpos = charpos;
3609 tpos = *position;
3610 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3611 && (charpos <= begb
3612 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3613 object),
3614 spec))
3615 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3616 frame_window_p)))
3617 {
3618 if (rv == 2)
3619 *disp_prop = 2;
3620 return charpos;
3621 }
3622
3623 /* Look forward for the first character with a `display' property
3624 that will replace the underlying text when displayed. */
3625 limpos = make_number (lim);
3626 do {
3627 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3628 CHARPOS (tpos) = XFASTINT (pos);
3629 if (CHARPOS (tpos) >= lim)
3630 {
3631 *disp_prop = 0;
3632 break;
3633 }
3634 if (STRINGP (object))
3635 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3636 else
3637 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3638 spec = Fget_char_property (pos, Qdisplay, object);
3639 if (!STRINGP (object))
3640 bufpos = CHARPOS (tpos);
3641 } while (NILP (spec)
3642 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3643 bufpos, frame_window_p)));
3644 if (rv == 2)
3645 *disp_prop = 2;
3646
3647 return CHARPOS (tpos);
3648 }
3649
3650 /* Return the character position of the end of the display string that
3651 started at CHARPOS. If there's no display string at CHARPOS,
3652 return -1. A display string is either an overlay with `display'
3653 property whose value is a string or a `display' text property whose
3654 value is a string. */
3655 ptrdiff_t
3656 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3657 {
3658 /* OBJECT = nil means current buffer. */
3659 Lisp_Object object =
3660 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3661 Lisp_Object pos = make_number (charpos);
3662 ptrdiff_t eob =
3663 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3664
3665 if (charpos >= eob || (string->s && !STRINGP (object)))
3666 return eob;
3667
3668 /* It could happen that the display property or overlay was removed
3669 since we found it in compute_display_string_pos above. One way
3670 this can happen is if JIT font-lock was called (through
3671 handle_fontified_prop), and jit-lock-functions remove text
3672 properties or overlays from the portion of buffer that includes
3673 CHARPOS. Muse mode is known to do that, for example. In this
3674 case, we return -1 to the caller, to signal that no display
3675 string is actually present at CHARPOS. See bidi_fetch_char for
3676 how this is handled.
3677
3678 An alternative would be to never look for display properties past
3679 it->stop_charpos. But neither compute_display_string_pos nor
3680 bidi_fetch_char that calls it know or care where the next
3681 stop_charpos is. */
3682 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3683 return -1;
3684
3685 /* Look forward for the first character where the `display' property
3686 changes. */
3687 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3688
3689 return XFASTINT (pos);
3690 }
3691
3692
3693 \f
3694 /***********************************************************************
3695 Fontification
3696 ***********************************************************************/
3697
3698 /* Handle changes in the `fontified' property of the current buffer by
3699 calling hook functions from Qfontification_functions to fontify
3700 regions of text. */
3701
3702 static enum prop_handled
3703 handle_fontified_prop (struct it *it)
3704 {
3705 Lisp_Object prop, pos;
3706 enum prop_handled handled = HANDLED_NORMALLY;
3707
3708 if (!NILP (Vmemory_full))
3709 return handled;
3710
3711 /* Get the value of the `fontified' property at IT's current buffer
3712 position. (The `fontified' property doesn't have a special
3713 meaning in strings.) If the value is nil, call functions from
3714 Qfontification_functions. */
3715 if (!STRINGP (it->string)
3716 && it->s == NULL
3717 && !NILP (Vfontification_functions)
3718 && !NILP (Vrun_hooks)
3719 && (pos = make_number (IT_CHARPOS (*it)),
3720 prop = Fget_char_property (pos, Qfontified, Qnil),
3721 /* Ignore the special cased nil value always present at EOB since
3722 no amount of fontifying will be able to change it. */
3723 NILP (prop) && IT_CHARPOS (*it) < Z))
3724 {
3725 ptrdiff_t count = SPECPDL_INDEX ();
3726 Lisp_Object val;
3727 struct buffer *obuf = current_buffer;
3728 int begv = BEGV, zv = ZV;
3729 int old_clip_changed = current_buffer->clip_changed;
3730
3731 val = Vfontification_functions;
3732 specbind (Qfontification_functions, Qnil);
3733
3734 eassert (it->end_charpos == ZV);
3735
3736 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3737 safe_call1 (val, pos);
3738 else
3739 {
3740 Lisp_Object fns, fn;
3741 struct gcpro gcpro1, gcpro2;
3742
3743 fns = Qnil;
3744 GCPRO2 (val, fns);
3745
3746 for (; CONSP (val); val = XCDR (val))
3747 {
3748 fn = XCAR (val);
3749
3750 if (EQ (fn, Qt))
3751 {
3752 /* A value of t indicates this hook has a local
3753 binding; it means to run the global binding too.
3754 In a global value, t should not occur. If it
3755 does, we must ignore it to avoid an endless
3756 loop. */
3757 for (fns = Fdefault_value (Qfontification_functions);
3758 CONSP (fns);
3759 fns = XCDR (fns))
3760 {
3761 fn = XCAR (fns);
3762 if (!EQ (fn, Qt))
3763 safe_call1 (fn, pos);
3764 }
3765 }
3766 else
3767 safe_call1 (fn, pos);
3768 }
3769
3770 UNGCPRO;
3771 }
3772
3773 unbind_to (count, Qnil);
3774
3775 /* Fontification functions routinely call `save-restriction'.
3776 Normally, this tags clip_changed, which can confuse redisplay
3777 (see discussion in Bug#6671). Since we don't perform any
3778 special handling of fontification changes in the case where
3779 `save-restriction' isn't called, there's no point doing so in
3780 this case either. So, if the buffer's restrictions are
3781 actually left unchanged, reset clip_changed. */
3782 if (obuf == current_buffer)
3783 {
3784 if (begv == BEGV && zv == ZV)
3785 current_buffer->clip_changed = old_clip_changed;
3786 }
3787 /* There isn't much we can reasonably do to protect against
3788 misbehaving fontification, but here's a fig leaf. */
3789 else if (BUFFER_LIVE_P (obuf))
3790 set_buffer_internal_1 (obuf);
3791
3792 /* The fontification code may have added/removed text.
3793 It could do even a lot worse, but let's at least protect against
3794 the most obvious case where only the text past `pos' gets changed',
3795 as is/was done in grep.el where some escapes sequences are turned
3796 into face properties (bug#7876). */
3797 it->end_charpos = ZV;
3798
3799 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3800 something. This avoids an endless loop if they failed to
3801 fontify the text for which reason ever. */
3802 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3803 handled = HANDLED_RECOMPUTE_PROPS;
3804 }
3805
3806 return handled;
3807 }
3808
3809
3810 \f
3811 /***********************************************************************
3812 Faces
3813 ***********************************************************************/
3814
3815 /* Set up iterator IT from face properties at its current position.
3816 Called from handle_stop. */
3817
3818 static enum prop_handled
3819 handle_face_prop (struct it *it)
3820 {
3821 int new_face_id;
3822 ptrdiff_t next_stop;
3823
3824 if (!STRINGP (it->string))
3825 {
3826 new_face_id
3827 = face_at_buffer_position (it->w,
3828 IT_CHARPOS (*it),
3829 it->region_beg_charpos,
3830 it->region_end_charpos,
3831 &next_stop,
3832 (IT_CHARPOS (*it)
3833 + TEXT_PROP_DISTANCE_LIMIT),
3834 0, it->base_face_id);
3835
3836 /* Is this a start of a run of characters with box face?
3837 Caveat: this can be called for a freshly initialized
3838 iterator; face_id is -1 in this case. We know that the new
3839 face will not change until limit, i.e. if the new face has a
3840 box, all characters up to limit will have one. But, as
3841 usual, we don't know whether limit is really the end. */
3842 if (new_face_id != it->face_id)
3843 {
3844 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3845 /* If it->face_id is -1, old_face below will be NULL, see
3846 the definition of FACE_FROM_ID. This will happen if this
3847 is the initial call that gets the face. */
3848 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3849
3850 /* If the value of face_id of the iterator is -1, we have to
3851 look in front of IT's position and see whether there is a
3852 face there that's different from new_face_id. */
3853 if (!old_face && IT_CHARPOS (*it) > BEG)
3854 {
3855 int prev_face_id = face_before_it_pos (it);
3856
3857 old_face = FACE_FROM_ID (it->f, prev_face_id);
3858 }
3859
3860 /* If the new face has a box, but the old face does not,
3861 this is the start of a run of characters with box face,
3862 i.e. this character has a shadow on the left side. */
3863 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3864 && (old_face == NULL || !old_face->box));
3865 it->face_box_p = new_face->box != FACE_NO_BOX;
3866 }
3867 }
3868 else
3869 {
3870 int base_face_id;
3871 ptrdiff_t bufpos;
3872 int i;
3873 Lisp_Object from_overlay
3874 = (it->current.overlay_string_index >= 0
3875 ? it->string_overlays[it->current.overlay_string_index
3876 % OVERLAY_STRING_CHUNK_SIZE]
3877 : Qnil);
3878
3879 /* See if we got to this string directly or indirectly from
3880 an overlay property. That includes the before-string or
3881 after-string of an overlay, strings in display properties
3882 provided by an overlay, their text properties, etc.
3883
3884 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3885 if (! NILP (from_overlay))
3886 for (i = it->sp - 1; i >= 0; i--)
3887 {
3888 if (it->stack[i].current.overlay_string_index >= 0)
3889 from_overlay
3890 = it->string_overlays[it->stack[i].current.overlay_string_index
3891 % OVERLAY_STRING_CHUNK_SIZE];
3892 else if (! NILP (it->stack[i].from_overlay))
3893 from_overlay = it->stack[i].from_overlay;
3894
3895 if (!NILP (from_overlay))
3896 break;
3897 }
3898
3899 if (! NILP (from_overlay))
3900 {
3901 bufpos = IT_CHARPOS (*it);
3902 /* For a string from an overlay, the base face depends
3903 only on text properties and ignores overlays. */
3904 base_face_id
3905 = face_for_overlay_string (it->w,
3906 IT_CHARPOS (*it),
3907 it->region_beg_charpos,
3908 it->region_end_charpos,
3909 &next_stop,
3910 (IT_CHARPOS (*it)
3911 + TEXT_PROP_DISTANCE_LIMIT),
3912 0,
3913 from_overlay);
3914 }
3915 else
3916 {
3917 bufpos = 0;
3918
3919 /* For strings from a `display' property, use the face at
3920 IT's current buffer position as the base face to merge
3921 with, so that overlay strings appear in the same face as
3922 surrounding text, unless they specify their own
3923 faces. */
3924 base_face_id = it->string_from_prefix_prop_p
3925 ? DEFAULT_FACE_ID
3926 : underlying_face_id (it);
3927 }
3928
3929 new_face_id = face_at_string_position (it->w,
3930 it->string,
3931 IT_STRING_CHARPOS (*it),
3932 bufpos,
3933 it->region_beg_charpos,
3934 it->region_end_charpos,
3935 &next_stop,
3936 base_face_id, 0);
3937
3938 /* Is this a start of a run of characters with box? Caveat:
3939 this can be called for a freshly allocated iterator; face_id
3940 is -1 is this case. We know that the new face will not
3941 change until the next check pos, i.e. if the new face has a
3942 box, all characters up to that position will have a
3943 box. But, as usual, we don't know whether that position
3944 is really the end. */
3945 if (new_face_id != it->face_id)
3946 {
3947 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3948 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3949
3950 /* If new face has a box but old face hasn't, this is the
3951 start of a run of characters with box, i.e. it has a
3952 shadow on the left side. */
3953 it->start_of_box_run_p
3954 = new_face->box && (old_face == NULL || !old_face->box);
3955 it->face_box_p = new_face->box != FACE_NO_BOX;
3956 }
3957 }
3958
3959 it->face_id = new_face_id;
3960 return HANDLED_NORMALLY;
3961 }
3962
3963
3964 /* Return the ID of the face ``underlying'' IT's current position,
3965 which is in a string. If the iterator is associated with a
3966 buffer, return the face at IT's current buffer position.
3967 Otherwise, use the iterator's base_face_id. */
3968
3969 static int
3970 underlying_face_id (struct it *it)
3971 {
3972 int face_id = it->base_face_id, i;
3973
3974 eassert (STRINGP (it->string));
3975
3976 for (i = it->sp - 1; i >= 0; --i)
3977 if (NILP (it->stack[i].string))
3978 face_id = it->stack[i].face_id;
3979
3980 return face_id;
3981 }
3982
3983
3984 /* Compute the face one character before or after the current position
3985 of IT, in the visual order. BEFORE_P non-zero means get the face
3986 in front (to the left in L2R paragraphs, to the right in R2L
3987 paragraphs) of IT's screen position. Value is the ID of the face. */
3988
3989 static int
3990 face_before_or_after_it_pos (struct it *it, int before_p)
3991 {
3992 int face_id, limit;
3993 ptrdiff_t next_check_charpos;
3994 struct it it_copy;
3995 void *it_copy_data = NULL;
3996
3997 eassert (it->s == NULL);
3998
3999 if (STRINGP (it->string))
4000 {
4001 ptrdiff_t bufpos, charpos;
4002 int base_face_id;
4003
4004 /* No face change past the end of the string (for the case
4005 we are padding with spaces). No face change before the
4006 string start. */
4007 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4008 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4009 return it->face_id;
4010
4011 if (!it->bidi_p)
4012 {
4013 /* Set charpos to the position before or after IT's current
4014 position, in the logical order, which in the non-bidi
4015 case is the same as the visual order. */
4016 if (before_p)
4017 charpos = IT_STRING_CHARPOS (*it) - 1;
4018 else if (it->what == IT_COMPOSITION)
4019 /* For composition, we must check the character after the
4020 composition. */
4021 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4022 else
4023 charpos = IT_STRING_CHARPOS (*it) + 1;
4024 }
4025 else
4026 {
4027 if (before_p)
4028 {
4029 /* With bidi iteration, the character before the current
4030 in the visual order cannot be found by simple
4031 iteration, because "reverse" reordering is not
4032 supported. Instead, we need to use the move_it_*
4033 family of functions. */
4034 /* Ignore face changes before the first visible
4035 character on this display line. */
4036 if (it->current_x <= it->first_visible_x)
4037 return it->face_id;
4038 SAVE_IT (it_copy, *it, it_copy_data);
4039 /* Implementation note: Since move_it_in_display_line
4040 works in the iterator geometry, and thinks the first
4041 character is always the leftmost, even in R2L lines,
4042 we don't need to distinguish between the R2L and L2R
4043 cases here. */
4044 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4045 it_copy.current_x - 1, MOVE_TO_X);
4046 charpos = IT_STRING_CHARPOS (it_copy);
4047 RESTORE_IT (it, it, it_copy_data);
4048 }
4049 else
4050 {
4051 /* Set charpos to the string position of the character
4052 that comes after IT's current position in the visual
4053 order. */
4054 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4055
4056 it_copy = *it;
4057 while (n--)
4058 bidi_move_to_visually_next (&it_copy.bidi_it);
4059
4060 charpos = it_copy.bidi_it.charpos;
4061 }
4062 }
4063 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4064
4065 if (it->current.overlay_string_index >= 0)
4066 bufpos = IT_CHARPOS (*it);
4067 else
4068 bufpos = 0;
4069
4070 base_face_id = underlying_face_id (it);
4071
4072 /* Get the face for ASCII, or unibyte. */
4073 face_id = face_at_string_position (it->w,
4074 it->string,
4075 charpos,
4076 bufpos,
4077 it->region_beg_charpos,
4078 it->region_end_charpos,
4079 &next_check_charpos,
4080 base_face_id, 0);
4081
4082 /* Correct the face for charsets different from ASCII. Do it
4083 for the multibyte case only. The face returned above is
4084 suitable for unibyte text if IT->string is unibyte. */
4085 if (STRING_MULTIBYTE (it->string))
4086 {
4087 struct text_pos pos1 = string_pos (charpos, it->string);
4088 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4089 int c, len;
4090 struct face *face = FACE_FROM_ID (it->f, face_id);
4091
4092 c = string_char_and_length (p, &len);
4093 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4094 }
4095 }
4096 else
4097 {
4098 struct text_pos pos;
4099
4100 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4101 || (IT_CHARPOS (*it) <= BEGV && before_p))
4102 return it->face_id;
4103
4104 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4105 pos = it->current.pos;
4106
4107 if (!it->bidi_p)
4108 {
4109 if (before_p)
4110 DEC_TEXT_POS (pos, it->multibyte_p);
4111 else
4112 {
4113 if (it->what == IT_COMPOSITION)
4114 {
4115 /* For composition, we must check the position after
4116 the composition. */
4117 pos.charpos += it->cmp_it.nchars;
4118 pos.bytepos += it->len;
4119 }
4120 else
4121 INC_TEXT_POS (pos, it->multibyte_p);
4122 }
4123 }
4124 else
4125 {
4126 if (before_p)
4127 {
4128 /* With bidi iteration, the character before the current
4129 in the visual order cannot be found by simple
4130 iteration, because "reverse" reordering is not
4131 supported. Instead, we need to use the move_it_*
4132 family of functions. */
4133 /* Ignore face changes before the first visible
4134 character on this display line. */
4135 if (it->current_x <= it->first_visible_x)
4136 return it->face_id;
4137 SAVE_IT (it_copy, *it, it_copy_data);
4138 /* Implementation note: Since move_it_in_display_line
4139 works in the iterator geometry, and thinks the first
4140 character is always the leftmost, even in R2L lines,
4141 we don't need to distinguish between the R2L and L2R
4142 cases here. */
4143 move_it_in_display_line (&it_copy, ZV,
4144 it_copy.current_x - 1, MOVE_TO_X);
4145 pos = it_copy.current.pos;
4146 RESTORE_IT (it, it, it_copy_data);
4147 }
4148 else
4149 {
4150 /* Set charpos to the buffer position of the character
4151 that comes after IT's current position in the visual
4152 order. */
4153 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4154
4155 it_copy = *it;
4156 while (n--)
4157 bidi_move_to_visually_next (&it_copy.bidi_it);
4158
4159 SET_TEXT_POS (pos,
4160 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4161 }
4162 }
4163 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4164
4165 /* Determine face for CHARSET_ASCII, or unibyte. */
4166 face_id = face_at_buffer_position (it->w,
4167 CHARPOS (pos),
4168 it->region_beg_charpos,
4169 it->region_end_charpos,
4170 &next_check_charpos,
4171 limit, 0, -1);
4172
4173 /* Correct the face for charsets different from ASCII. Do it
4174 for the multibyte case only. The face returned above is
4175 suitable for unibyte text if current_buffer is unibyte. */
4176 if (it->multibyte_p)
4177 {
4178 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4179 struct face *face = FACE_FROM_ID (it->f, face_id);
4180 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4181 }
4182 }
4183
4184 return face_id;
4185 }
4186
4187
4188 \f
4189 /***********************************************************************
4190 Invisible text
4191 ***********************************************************************/
4192
4193 /* Set up iterator IT from invisible properties at its current
4194 position. Called from handle_stop. */
4195
4196 static enum prop_handled
4197 handle_invisible_prop (struct it *it)
4198 {
4199 enum prop_handled handled = HANDLED_NORMALLY;
4200 int invis_p;
4201 Lisp_Object prop;
4202
4203 if (STRINGP (it->string))
4204 {
4205 Lisp_Object end_charpos, limit, charpos;
4206
4207 /* Get the value of the invisible text property at the
4208 current position. Value will be nil if there is no such
4209 property. */
4210 charpos = make_number (IT_STRING_CHARPOS (*it));
4211 prop = Fget_text_property (charpos, Qinvisible, it->string);
4212 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4213
4214 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4215 {
4216 /* Record whether we have to display an ellipsis for the
4217 invisible text. */
4218 int display_ellipsis_p = (invis_p == 2);
4219 ptrdiff_t len, endpos;
4220
4221 handled = HANDLED_RECOMPUTE_PROPS;
4222
4223 /* Get the position at which the next visible text can be
4224 found in IT->string, if any. */
4225 endpos = len = SCHARS (it->string);
4226 XSETINT (limit, len);
4227 do
4228 {
4229 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4230 it->string, limit);
4231 if (INTEGERP (end_charpos))
4232 {
4233 endpos = XFASTINT (end_charpos);
4234 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4235 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4236 if (invis_p == 2)
4237 display_ellipsis_p = 1;
4238 }
4239 }
4240 while (invis_p && endpos < len);
4241
4242 if (display_ellipsis_p)
4243 it->ellipsis_p = 1;
4244
4245 if (endpos < len)
4246 {
4247 /* Text at END_CHARPOS is visible. Move IT there. */
4248 struct text_pos old;
4249 ptrdiff_t oldpos;
4250
4251 old = it->current.string_pos;
4252 oldpos = CHARPOS (old);
4253 if (it->bidi_p)
4254 {
4255 if (it->bidi_it.first_elt
4256 && it->bidi_it.charpos < SCHARS (it->string))
4257 bidi_paragraph_init (it->paragraph_embedding,
4258 &it->bidi_it, 1);
4259 /* Bidi-iterate out of the invisible text. */
4260 do
4261 {
4262 bidi_move_to_visually_next (&it->bidi_it);
4263 }
4264 while (oldpos <= it->bidi_it.charpos
4265 && it->bidi_it.charpos < endpos);
4266
4267 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4268 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4269 if (IT_CHARPOS (*it) >= endpos)
4270 it->prev_stop = endpos;
4271 }
4272 else
4273 {
4274 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4275 compute_string_pos (&it->current.string_pos, old, it->string);
4276 }
4277 }
4278 else
4279 {
4280 /* The rest of the string is invisible. If this is an
4281 overlay string, proceed with the next overlay string
4282 or whatever comes and return a character from there. */
4283 if (it->current.overlay_string_index >= 0
4284 && !display_ellipsis_p)
4285 {
4286 next_overlay_string (it);
4287 /* Don't check for overlay strings when we just
4288 finished processing them. */
4289 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4290 }
4291 else
4292 {
4293 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4294 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4295 }
4296 }
4297 }
4298 }
4299 else
4300 {
4301 ptrdiff_t newpos, next_stop, start_charpos, tem;
4302 Lisp_Object pos, overlay;
4303
4304 /* First of all, is there invisible text at this position? */
4305 tem = start_charpos = IT_CHARPOS (*it);
4306 pos = make_number (tem);
4307 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4308 &overlay);
4309 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4310
4311 /* If we are on invisible text, skip over it. */
4312 if (invis_p && start_charpos < it->end_charpos)
4313 {
4314 /* Record whether we have to display an ellipsis for the
4315 invisible text. */
4316 int display_ellipsis_p = invis_p == 2;
4317
4318 handled = HANDLED_RECOMPUTE_PROPS;
4319
4320 /* Loop skipping over invisible text. The loop is left at
4321 ZV or with IT on the first char being visible again. */
4322 do
4323 {
4324 /* Try to skip some invisible text. Return value is the
4325 position reached which can be equal to where we start
4326 if there is nothing invisible there. This skips both
4327 over invisible text properties and overlays with
4328 invisible property. */
4329 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4330
4331 /* If we skipped nothing at all we weren't at invisible
4332 text in the first place. If everything to the end of
4333 the buffer was skipped, end the loop. */
4334 if (newpos == tem || newpos >= ZV)
4335 invis_p = 0;
4336 else
4337 {
4338 /* We skipped some characters but not necessarily
4339 all there are. Check if we ended up on visible
4340 text. Fget_char_property returns the property of
4341 the char before the given position, i.e. if we
4342 get invis_p = 0, this means that the char at
4343 newpos is visible. */
4344 pos = make_number (newpos);
4345 prop = Fget_char_property (pos, Qinvisible, it->window);
4346 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4347 }
4348
4349 /* If we ended up on invisible text, proceed to
4350 skip starting with next_stop. */
4351 if (invis_p)
4352 tem = next_stop;
4353
4354 /* If there are adjacent invisible texts, don't lose the
4355 second one's ellipsis. */
4356 if (invis_p == 2)
4357 display_ellipsis_p = 1;
4358 }
4359 while (invis_p);
4360
4361 /* The position newpos is now either ZV or on visible text. */
4362 if (it->bidi_p)
4363 {
4364 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4365 int on_newline =
4366 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4367 int after_newline =
4368 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4369
4370 /* If the invisible text ends on a newline or on a
4371 character after a newline, we can avoid the costly,
4372 character by character, bidi iteration to NEWPOS, and
4373 instead simply reseat the iterator there. That's
4374 because all bidi reordering information is tossed at
4375 the newline. This is a big win for modes that hide
4376 complete lines, like Outline, Org, etc. */
4377 if (on_newline || after_newline)
4378 {
4379 struct text_pos tpos;
4380 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4381
4382 SET_TEXT_POS (tpos, newpos, bpos);
4383 reseat_1 (it, tpos, 0);
4384 /* If we reseat on a newline/ZV, we need to prep the
4385 bidi iterator for advancing to the next character
4386 after the newline/EOB, keeping the current paragraph
4387 direction (so that PRODUCE_GLYPHS does TRT wrt
4388 prepending/appending glyphs to a glyph row). */
4389 if (on_newline)
4390 {
4391 it->bidi_it.first_elt = 0;
4392 it->bidi_it.paragraph_dir = pdir;
4393 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4394 it->bidi_it.nchars = 1;
4395 it->bidi_it.ch_len = 1;
4396 }
4397 }
4398 else /* Must use the slow method. */
4399 {
4400 /* With bidi iteration, the region of invisible text
4401 could start and/or end in the middle of a
4402 non-base embedding level. Therefore, we need to
4403 skip invisible text using the bidi iterator,
4404 starting at IT's current position, until we find
4405 ourselves outside of the invisible text.
4406 Skipping invisible text _after_ bidi iteration
4407 avoids affecting the visual order of the
4408 displayed text when invisible properties are
4409 added or removed. */
4410 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4411 {
4412 /* If we were `reseat'ed to a new paragraph,
4413 determine the paragraph base direction. We
4414 need to do it now because
4415 next_element_from_buffer may not have a
4416 chance to do it, if we are going to skip any
4417 text at the beginning, which resets the
4418 FIRST_ELT flag. */
4419 bidi_paragraph_init (it->paragraph_embedding,
4420 &it->bidi_it, 1);
4421 }
4422 do
4423 {
4424 bidi_move_to_visually_next (&it->bidi_it);
4425 }
4426 while (it->stop_charpos <= it->bidi_it.charpos
4427 && it->bidi_it.charpos < newpos);
4428 IT_CHARPOS (*it) = it->bidi_it.charpos;
4429 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4430 /* If we overstepped NEWPOS, record its position in
4431 the iterator, so that we skip invisible text if
4432 later the bidi iteration lands us in the
4433 invisible region again. */
4434 if (IT_CHARPOS (*it) >= newpos)
4435 it->prev_stop = newpos;
4436 }
4437 }
4438 else
4439 {
4440 IT_CHARPOS (*it) = newpos;
4441 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4442 }
4443
4444 /* If there are before-strings at the start of invisible
4445 text, and the text is invisible because of a text
4446 property, arrange to show before-strings because 20.x did
4447 it that way. (If the text is invisible because of an
4448 overlay property instead of a text property, this is
4449 already handled in the overlay code.) */
4450 if (NILP (overlay)
4451 && get_overlay_strings (it, it->stop_charpos))
4452 {
4453 handled = HANDLED_RECOMPUTE_PROPS;
4454 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4455 }
4456 else if (display_ellipsis_p)
4457 {
4458 /* Make sure that the glyphs of the ellipsis will get
4459 correct `charpos' values. If we would not update
4460 it->position here, the glyphs would belong to the
4461 last visible character _before_ the invisible
4462 text, which confuses `set_cursor_from_row'.
4463
4464 We use the last invisible position instead of the
4465 first because this way the cursor is always drawn on
4466 the first "." of the ellipsis, whenever PT is inside
4467 the invisible text. Otherwise the cursor would be
4468 placed _after_ the ellipsis when the point is after the
4469 first invisible character. */
4470 if (!STRINGP (it->object))
4471 {
4472 it->position.charpos = newpos - 1;
4473 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4474 }
4475 it->ellipsis_p = 1;
4476 /* Let the ellipsis display before
4477 considering any properties of the following char.
4478 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4479 handled = HANDLED_RETURN;
4480 }
4481 }
4482 }
4483
4484 return handled;
4485 }
4486
4487
4488 /* Make iterator IT return `...' next.
4489 Replaces LEN characters from buffer. */
4490
4491 static void
4492 setup_for_ellipsis (struct it *it, int len)
4493 {
4494 /* Use the display table definition for `...'. Invalid glyphs
4495 will be handled by the method returning elements from dpvec. */
4496 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4497 {
4498 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4499 it->dpvec = v->contents;
4500 it->dpend = v->contents + v->header.size;
4501 }
4502 else
4503 {
4504 /* Default `...'. */
4505 it->dpvec = default_invis_vector;
4506 it->dpend = default_invis_vector + 3;
4507 }
4508
4509 it->dpvec_char_len = len;
4510 it->current.dpvec_index = 0;
4511 it->dpvec_face_id = -1;
4512
4513 /* Remember the current face id in case glyphs specify faces.
4514 IT's face is restored in set_iterator_to_next.
4515 saved_face_id was set to preceding char's face in handle_stop. */
4516 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4517 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4518
4519 it->method = GET_FROM_DISPLAY_VECTOR;
4520 it->ellipsis_p = 1;
4521 }
4522
4523
4524 \f
4525 /***********************************************************************
4526 'display' property
4527 ***********************************************************************/
4528
4529 /* Set up iterator IT from `display' property at its current position.
4530 Called from handle_stop.
4531 We return HANDLED_RETURN if some part of the display property
4532 overrides the display of the buffer text itself.
4533 Otherwise we return HANDLED_NORMALLY. */
4534
4535 static enum prop_handled
4536 handle_display_prop (struct it *it)
4537 {
4538 Lisp_Object propval, object, overlay;
4539 struct text_pos *position;
4540 ptrdiff_t bufpos;
4541 /* Nonzero if some property replaces the display of the text itself. */
4542 int display_replaced_p = 0;
4543
4544 if (STRINGP (it->string))
4545 {
4546 object = it->string;
4547 position = &it->current.string_pos;
4548 bufpos = CHARPOS (it->current.pos);
4549 }
4550 else
4551 {
4552 XSETWINDOW (object, it->w);
4553 position = &it->current.pos;
4554 bufpos = CHARPOS (*position);
4555 }
4556
4557 /* Reset those iterator values set from display property values. */
4558 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4559 it->space_width = Qnil;
4560 it->font_height = Qnil;
4561 it->voffset = 0;
4562
4563 /* We don't support recursive `display' properties, i.e. string
4564 values that have a string `display' property, that have a string
4565 `display' property etc. */
4566 if (!it->string_from_display_prop_p)
4567 it->area = TEXT_AREA;
4568
4569 propval = get_char_property_and_overlay (make_number (position->charpos),
4570 Qdisplay, object, &overlay);
4571 if (NILP (propval))
4572 return HANDLED_NORMALLY;
4573 /* Now OVERLAY is the overlay that gave us this property, or nil
4574 if it was a text property. */
4575
4576 if (!STRINGP (it->string))
4577 object = it->w->contents;
4578
4579 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4580 position, bufpos,
4581 FRAME_WINDOW_P (it->f));
4582
4583 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4584 }
4585
4586 /* Subroutine of handle_display_prop. Returns non-zero if the display
4587 specification in SPEC is a replacing specification, i.e. it would
4588 replace the text covered by `display' property with something else,
4589 such as an image or a display string. If SPEC includes any kind or
4590 `(space ...) specification, the value is 2; this is used by
4591 compute_display_string_pos, which see.
4592
4593 See handle_single_display_spec for documentation of arguments.
4594 frame_window_p is non-zero if the window being redisplayed is on a
4595 GUI frame; this argument is used only if IT is NULL, see below.
4596
4597 IT can be NULL, if this is called by the bidi reordering code
4598 through compute_display_string_pos, which see. In that case, this
4599 function only examines SPEC, but does not otherwise "handle" it, in
4600 the sense that it doesn't set up members of IT from the display
4601 spec. */
4602 static int
4603 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4604 Lisp_Object overlay, struct text_pos *position,
4605 ptrdiff_t bufpos, int frame_window_p)
4606 {
4607 int replacing_p = 0;
4608 int rv;
4609
4610 if (CONSP (spec)
4611 /* Simple specifications. */
4612 && !EQ (XCAR (spec), Qimage)
4613 #ifdef HAVE_XWIDGETS
4614 && !EQ (XCAR (spec), Qxwidget)
4615 #endif
4616 && !EQ (XCAR (spec), Qspace)
4617 && !EQ (XCAR (spec), Qwhen)
4618 && !EQ (XCAR (spec), Qslice)
4619 && !EQ (XCAR (spec), Qspace_width)
4620 && !EQ (XCAR (spec), Qheight)
4621 && !EQ (XCAR (spec), Qraise)
4622 /* Marginal area specifications. */
4623 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4624 && !EQ (XCAR (spec), Qleft_fringe)
4625 && !EQ (XCAR (spec), Qright_fringe)
4626 && !NILP (XCAR (spec)))
4627 {
4628 for (; CONSP (spec); spec = XCDR (spec))
4629 {
4630 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4631 overlay, position, bufpos,
4632 replacing_p, frame_window_p)))
4633 {
4634 replacing_p = rv;
4635 /* If some text in a string is replaced, `position' no
4636 longer points to the position of `object'. */
4637 if (!it || STRINGP (object))
4638 break;
4639 }
4640 }
4641 }
4642 else if (VECTORP (spec))
4643 {
4644 ptrdiff_t i;
4645 for (i = 0; i < ASIZE (spec); ++i)
4646 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4647 overlay, position, bufpos,
4648 replacing_p, frame_window_p)))
4649 {
4650 replacing_p = rv;
4651 /* If some text in a string is replaced, `position' no
4652 longer points to the position of `object'. */
4653 if (!it || STRINGP (object))
4654 break;
4655 }
4656 }
4657 else
4658 {
4659 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4660 position, bufpos, 0,
4661 frame_window_p)))
4662 replacing_p = rv;
4663 }
4664
4665 return replacing_p;
4666 }
4667
4668 /* Value is the position of the end of the `display' property starting
4669 at START_POS in OBJECT. */
4670
4671 static struct text_pos
4672 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4673 {
4674 Lisp_Object end;
4675 struct text_pos end_pos;
4676
4677 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4678 Qdisplay, object, Qnil);
4679 CHARPOS (end_pos) = XFASTINT (end);
4680 if (STRINGP (object))
4681 compute_string_pos (&end_pos, start_pos, it->string);
4682 else
4683 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4684
4685 return end_pos;
4686 }
4687
4688
4689 /* Set up IT from a single `display' property specification SPEC. OBJECT
4690 is the object in which the `display' property was found. *POSITION
4691 is the position in OBJECT at which the `display' property was found.
4692 BUFPOS is the buffer position of OBJECT (different from POSITION if
4693 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4694 previously saw a display specification which already replaced text
4695 display with something else, for example an image; we ignore such
4696 properties after the first one has been processed.
4697
4698 OVERLAY is the overlay this `display' property came from,
4699 or nil if it was a text property.
4700
4701 If SPEC is a `space' or `image' specification, and in some other
4702 cases too, set *POSITION to the position where the `display'
4703 property ends.
4704
4705 If IT is NULL, only examine the property specification in SPEC, but
4706 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4707 is intended to be displayed in a window on a GUI frame.
4708
4709 Value is non-zero if something was found which replaces the display
4710 of buffer or string text. */
4711
4712 static int
4713 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4714 Lisp_Object overlay, struct text_pos *position,
4715 ptrdiff_t bufpos, int display_replaced_p,
4716 int frame_window_p)
4717 {
4718 Lisp_Object form;
4719 Lisp_Object location, value;
4720 struct text_pos start_pos = *position;
4721 int valid_p;
4722
4723 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4724 If the result is non-nil, use VALUE instead of SPEC. */
4725 form = Qt;
4726 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4727 {
4728 spec = XCDR (spec);
4729 if (!CONSP (spec))
4730 return 0;
4731 form = XCAR (spec);
4732 spec = XCDR (spec);
4733 }
4734
4735 if (!NILP (form) && !EQ (form, Qt))
4736 {
4737 ptrdiff_t count = SPECPDL_INDEX ();
4738 struct gcpro gcpro1;
4739
4740 /* Bind `object' to the object having the `display' property, a
4741 buffer or string. Bind `position' to the position in the
4742 object where the property was found, and `buffer-position'
4743 to the current position in the buffer. */
4744
4745 if (NILP (object))
4746 XSETBUFFER (object, current_buffer);
4747 specbind (Qobject, object);
4748 specbind (Qposition, make_number (CHARPOS (*position)));
4749 specbind (Qbuffer_position, make_number (bufpos));
4750 GCPRO1 (form);
4751 form = safe_eval (form);
4752 UNGCPRO;
4753 unbind_to (count, Qnil);
4754 }
4755
4756 if (NILP (form))
4757 return 0;
4758
4759 /* Handle `(height HEIGHT)' specifications. */
4760 if (CONSP (spec)
4761 && EQ (XCAR (spec), Qheight)
4762 && CONSP (XCDR (spec)))
4763 {
4764 if (it)
4765 {
4766 if (!FRAME_WINDOW_P (it->f))
4767 return 0;
4768
4769 it->font_height = XCAR (XCDR (spec));
4770 if (!NILP (it->font_height))
4771 {
4772 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4773 int new_height = -1;
4774
4775 if (CONSP (it->font_height)
4776 && (EQ (XCAR (it->font_height), Qplus)
4777 || EQ (XCAR (it->font_height), Qminus))
4778 && CONSP (XCDR (it->font_height))
4779 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4780 {
4781 /* `(+ N)' or `(- N)' where N is an integer. */
4782 int steps = XINT (XCAR (XCDR (it->font_height)));
4783 if (EQ (XCAR (it->font_height), Qplus))
4784 steps = - steps;
4785 it->face_id = smaller_face (it->f, it->face_id, steps);
4786 }
4787 else if (FUNCTIONP (it->font_height))
4788 {
4789 /* Call function with current height as argument.
4790 Value is the new height. */
4791 Lisp_Object height;
4792 height = safe_call1 (it->font_height,
4793 face->lface[LFACE_HEIGHT_INDEX]);
4794 if (NUMBERP (height))
4795 new_height = XFLOATINT (height);
4796 }
4797 else if (NUMBERP (it->font_height))
4798 {
4799 /* Value is a multiple of the canonical char height. */
4800 struct face *f;
4801
4802 f = FACE_FROM_ID (it->f,
4803 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4804 new_height = (XFLOATINT (it->font_height)
4805 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4806 }
4807 else
4808 {
4809 /* Evaluate IT->font_height with `height' bound to the
4810 current specified height to get the new height. */
4811 ptrdiff_t count = SPECPDL_INDEX ();
4812
4813 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4814 value = safe_eval (it->font_height);
4815 unbind_to (count, Qnil);
4816
4817 if (NUMBERP (value))
4818 new_height = XFLOATINT (value);
4819 }
4820
4821 if (new_height > 0)
4822 it->face_id = face_with_height (it->f, it->face_id, new_height);
4823 }
4824 }
4825
4826 return 0;
4827 }
4828
4829 /* Handle `(space-width WIDTH)'. */
4830 if (CONSP (spec)
4831 && EQ (XCAR (spec), Qspace_width)
4832 && CONSP (XCDR (spec)))
4833 {
4834 if (it)
4835 {
4836 if (!FRAME_WINDOW_P (it->f))
4837 return 0;
4838
4839 value = XCAR (XCDR (spec));
4840 if (NUMBERP (value) && XFLOATINT (value) > 0)
4841 it->space_width = value;
4842 }
4843
4844 return 0;
4845 }
4846
4847 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4848 if (CONSP (spec)
4849 && EQ (XCAR (spec), Qslice))
4850 {
4851 Lisp_Object tem;
4852
4853 if (it)
4854 {
4855 if (!FRAME_WINDOW_P (it->f))
4856 return 0;
4857
4858 if (tem = XCDR (spec), CONSP (tem))
4859 {
4860 it->slice.x = XCAR (tem);
4861 if (tem = XCDR (tem), CONSP (tem))
4862 {
4863 it->slice.y = XCAR (tem);
4864 if (tem = XCDR (tem), CONSP (tem))
4865 {
4866 it->slice.width = XCAR (tem);
4867 if (tem = XCDR (tem), CONSP (tem))
4868 it->slice.height = XCAR (tem);
4869 }
4870 }
4871 }
4872 }
4873
4874 return 0;
4875 }
4876
4877 /* Handle `(raise FACTOR)'. */
4878 if (CONSP (spec)
4879 && EQ (XCAR (spec), Qraise)
4880 && CONSP (XCDR (spec)))
4881 {
4882 if (it)
4883 {
4884 if (!FRAME_WINDOW_P (it->f))
4885 return 0;
4886
4887 #ifdef HAVE_WINDOW_SYSTEM
4888 value = XCAR (XCDR (spec));
4889 if (NUMBERP (value))
4890 {
4891 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4892 it->voffset = - (XFLOATINT (value)
4893 * (FONT_HEIGHT (face->font)));
4894 }
4895 #endif /* HAVE_WINDOW_SYSTEM */
4896 }
4897
4898 return 0;
4899 }
4900
4901 /* Don't handle the other kinds of display specifications
4902 inside a string that we got from a `display' property. */
4903 if (it && it->string_from_display_prop_p)
4904 return 0;
4905
4906 /* Characters having this form of property are not displayed, so
4907 we have to find the end of the property. */
4908 if (it)
4909 {
4910 start_pos = *position;
4911 *position = display_prop_end (it, object, start_pos);
4912 }
4913 value = Qnil;
4914
4915 /* Stop the scan at that end position--we assume that all
4916 text properties change there. */
4917 if (it)
4918 it->stop_charpos = position->charpos;
4919
4920 /* Handle `(left-fringe BITMAP [FACE])'
4921 and `(right-fringe BITMAP [FACE])'. */
4922 if (CONSP (spec)
4923 && (EQ (XCAR (spec), Qleft_fringe)
4924 || EQ (XCAR (spec), Qright_fringe))
4925 && CONSP (XCDR (spec)))
4926 {
4927 int fringe_bitmap;
4928
4929 if (it)
4930 {
4931 if (!FRAME_WINDOW_P (it->f))
4932 /* If we return here, POSITION has been advanced
4933 across the text with this property. */
4934 {
4935 /* Synchronize the bidi iterator with POSITION. This is
4936 needed because we are not going to push the iterator
4937 on behalf of this display property, so there will be
4938 no pop_it call to do this synchronization for us. */
4939 if (it->bidi_p)
4940 {
4941 it->position = *position;
4942 iterate_out_of_display_property (it);
4943 *position = it->position;
4944 }
4945 return 1;
4946 }
4947 }
4948 else if (!frame_window_p)
4949 return 1;
4950
4951 #ifdef HAVE_WINDOW_SYSTEM
4952 value = XCAR (XCDR (spec));
4953 if (!SYMBOLP (value)
4954 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4955 /* If we return here, POSITION has been advanced
4956 across the text with this property. */
4957 {
4958 if (it && it->bidi_p)
4959 {
4960 it->position = *position;
4961 iterate_out_of_display_property (it);
4962 *position = it->position;
4963 }
4964 return 1;
4965 }
4966
4967 if (it)
4968 {
4969 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4970
4971 if (CONSP (XCDR (XCDR (spec))))
4972 {
4973 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4974 int face_id2 = lookup_derived_face (it->f, face_name,
4975 FRINGE_FACE_ID, 0);
4976 if (face_id2 >= 0)
4977 face_id = face_id2;
4978 }
4979
4980 /* Save current settings of IT so that we can restore them
4981 when we are finished with the glyph property value. */
4982 push_it (it, position);
4983
4984 it->area = TEXT_AREA;
4985 it->what = IT_IMAGE;
4986 it->image_id = -1; /* no image */
4987 it->position = start_pos;
4988 it->object = NILP (object) ? it->w->contents : object;
4989 it->method = GET_FROM_IMAGE;
4990 it->from_overlay = Qnil;
4991 it->face_id = face_id;
4992 it->from_disp_prop_p = 1;
4993
4994 /* Say that we haven't consumed the characters with
4995 `display' property yet. The call to pop_it in
4996 set_iterator_to_next will clean this up. */
4997 *position = start_pos;
4998
4999 if (EQ (XCAR (spec), Qleft_fringe))
5000 {
5001 it->left_user_fringe_bitmap = fringe_bitmap;
5002 it->left_user_fringe_face_id = face_id;
5003 }
5004 else
5005 {
5006 it->right_user_fringe_bitmap = fringe_bitmap;
5007 it->right_user_fringe_face_id = face_id;
5008 }
5009 }
5010 #endif /* HAVE_WINDOW_SYSTEM */
5011 return 1;
5012 }
5013
5014 /* Prepare to handle `((margin left-margin) ...)',
5015 `((margin right-margin) ...)' and `((margin nil) ...)'
5016 prefixes for display specifications. */
5017 location = Qunbound;
5018 if (CONSP (spec) && CONSP (XCAR (spec)))
5019 {
5020 Lisp_Object tem;
5021
5022 value = XCDR (spec);
5023 if (CONSP (value))
5024 value = XCAR (value);
5025
5026 tem = XCAR (spec);
5027 if (EQ (XCAR (tem), Qmargin)
5028 && (tem = XCDR (tem),
5029 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5030 (NILP (tem)
5031 || EQ (tem, Qleft_margin)
5032 || EQ (tem, Qright_margin))))
5033 location = tem;
5034 }
5035
5036 if (EQ (location, Qunbound))
5037 {
5038 location = Qnil;
5039 value = spec;
5040 }
5041
5042 /* After this point, VALUE is the property after any
5043 margin prefix has been stripped. It must be a string,
5044 an image specification, or `(space ...)'.
5045
5046 LOCATION specifies where to display: `left-margin',
5047 `right-margin' or nil. */
5048
5049 valid_p = (STRINGP (value)
5050 #ifdef HAVE_WINDOW_SYSTEM
5051 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5052 && valid_image_p (value))
5053 #endif /* not HAVE_WINDOW_SYSTEM */
5054 || (CONSP (value) && EQ (XCAR (value), Qspace))
5055 #ifdef HAVE_XWIDGETS
5056 || valid_xwidget_spec_p(value)
5057 #endif
5058 );
5059
5060 if (valid_p && !display_replaced_p)
5061 {
5062 int retval = 1;
5063
5064 if (!it)
5065 {
5066 /* Callers need to know whether the display spec is any kind
5067 of `(space ...)' spec that is about to affect text-area
5068 display. */
5069 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5070 retval = 2;
5071 return retval;
5072 }
5073
5074 /* Save current settings of IT so that we can restore them
5075 when we are finished with the glyph property value. */
5076 push_it (it, position);
5077 it->from_overlay = overlay;
5078 it->from_disp_prop_p = 1;
5079
5080 if (NILP (location))
5081 it->area = TEXT_AREA;
5082 else if (EQ (location, Qleft_margin))
5083 it->area = LEFT_MARGIN_AREA;
5084 else
5085 it->area = RIGHT_MARGIN_AREA;
5086
5087 if (STRINGP (value))
5088 {
5089 it->string = value;
5090 it->multibyte_p = STRING_MULTIBYTE (it->string);
5091 it->current.overlay_string_index = -1;
5092 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5093 it->end_charpos = it->string_nchars = SCHARS (it->string);
5094 it->method = GET_FROM_STRING;
5095 it->stop_charpos = 0;
5096 it->prev_stop = 0;
5097 it->base_level_stop = 0;
5098 it->string_from_display_prop_p = 1;
5099 /* Say that we haven't consumed the characters with
5100 `display' property yet. The call to pop_it in
5101 set_iterator_to_next will clean this up. */
5102 if (BUFFERP (object))
5103 *position = start_pos;
5104
5105 /* Force paragraph direction to be that of the parent
5106 object. If the parent object's paragraph direction is
5107 not yet determined, default to L2R. */
5108 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5109 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5110 else
5111 it->paragraph_embedding = L2R;
5112
5113 /* Set up the bidi iterator for this display string. */
5114 if (it->bidi_p)
5115 {
5116 it->bidi_it.string.lstring = it->string;
5117 it->bidi_it.string.s = NULL;
5118 it->bidi_it.string.schars = it->end_charpos;
5119 it->bidi_it.string.bufpos = bufpos;
5120 it->bidi_it.string.from_disp_str = 1;
5121 it->bidi_it.string.unibyte = !it->multibyte_p;
5122 it->bidi_it.w = it->w;
5123 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5124 }
5125 }
5126 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5127 {
5128 it->method = GET_FROM_STRETCH;
5129 it->object = value;
5130 *position = it->position = start_pos;
5131 retval = 1 + (it->area == TEXT_AREA);
5132 }
5133 #ifdef HAVE_XWIDGETS
5134 else if (valid_xwidget_spec_p(value))
5135 {
5136 //printf("handle_single_display_spec: im an xwidget!!\n");
5137 it->what = IT_XWIDGET;
5138 it->method = GET_FROM_XWIDGET;
5139 it->position = start_pos;
5140 it->object = NILP (object) ? it->w->contents : object;
5141 *position = start_pos;
5142
5143 it->xwidget = lookup_xwidget(value);
5144 }
5145 #endif
5146 #ifdef HAVE_WINDOW_SYSTEM
5147 else
5148 {
5149 it->what = IT_IMAGE;
5150 it->image_id = lookup_image (it->f, value);
5151 it->position = start_pos;
5152 it->object = NILP (object) ? it->w->contents : object;
5153 it->method = GET_FROM_IMAGE;
5154
5155 /* Say that we haven't consumed the characters with
5156 `display' property yet. The call to pop_it in
5157 set_iterator_to_next will clean this up. */
5158 *position = start_pos;
5159 }
5160 #endif /* HAVE_WINDOW_SYSTEM */
5161
5162 return retval;
5163 }
5164
5165 /* Invalid property or property not supported. Restore
5166 POSITION to what it was before. */
5167 *position = start_pos;
5168 return 0;
5169 }
5170
5171 /* Check if PROP is a display property value whose text should be
5172 treated as intangible. OVERLAY is the overlay from which PROP
5173 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5174 specify the buffer position covered by PROP. */
5175
5176 int
5177 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5178 ptrdiff_t charpos, ptrdiff_t bytepos)
5179 {
5180 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5181 struct text_pos position;
5182
5183 SET_TEXT_POS (position, charpos, bytepos);
5184 return handle_display_spec (NULL, prop, Qnil, overlay,
5185 &position, charpos, frame_window_p);
5186 }
5187
5188
5189 /* Return 1 if PROP is a display sub-property value containing STRING.
5190
5191 Implementation note: this and the following function are really
5192 special cases of handle_display_spec and
5193 handle_single_display_spec, and should ideally use the same code.
5194 Until they do, these two pairs must be consistent and must be
5195 modified in sync. */
5196
5197 static int
5198 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5199 {
5200 if (EQ (string, prop))
5201 return 1;
5202
5203 /* Skip over `when FORM'. */
5204 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5205 {
5206 prop = XCDR (prop);
5207 if (!CONSP (prop))
5208 return 0;
5209 /* Actually, the condition following `when' should be eval'ed,
5210 like handle_single_display_spec does, and we should return
5211 zero if it evaluates to nil. However, this function is
5212 called only when the buffer was already displayed and some
5213 glyph in the glyph matrix was found to come from a display
5214 string. Therefore, the condition was already evaluated, and
5215 the result was non-nil, otherwise the display string wouldn't
5216 have been displayed and we would have never been called for
5217 this property. Thus, we can skip the evaluation and assume
5218 its result is non-nil. */
5219 prop = XCDR (prop);
5220 }
5221
5222 if (CONSP (prop))
5223 /* Skip over `margin LOCATION'. */
5224 if (EQ (XCAR (prop), Qmargin))
5225 {
5226 prop = XCDR (prop);
5227 if (!CONSP (prop))
5228 return 0;
5229
5230 prop = XCDR (prop);
5231 if (!CONSP (prop))
5232 return 0;
5233 }
5234
5235 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5236 }
5237
5238
5239 /* Return 1 if STRING appears in the `display' property PROP. */
5240
5241 static int
5242 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5243 {
5244 if (CONSP (prop)
5245 && !EQ (XCAR (prop), Qwhen)
5246 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5247 {
5248 /* A list of sub-properties. */
5249 while (CONSP (prop))
5250 {
5251 if (single_display_spec_string_p (XCAR (prop), string))
5252 return 1;
5253 prop = XCDR (prop);
5254 }
5255 }
5256 else if (VECTORP (prop))
5257 {
5258 /* A vector of sub-properties. */
5259 ptrdiff_t i;
5260 for (i = 0; i < ASIZE (prop); ++i)
5261 if (single_display_spec_string_p (AREF (prop, i), string))
5262 return 1;
5263 }
5264 else
5265 return single_display_spec_string_p (prop, string);
5266
5267 return 0;
5268 }
5269
5270 /* Look for STRING in overlays and text properties in the current
5271 buffer, between character positions FROM and TO (excluding TO).
5272 BACK_P non-zero means look back (in this case, TO is supposed to be
5273 less than FROM).
5274 Value is the first character position where STRING was found, or
5275 zero if it wasn't found before hitting TO.
5276
5277 This function may only use code that doesn't eval because it is
5278 called asynchronously from note_mouse_highlight. */
5279
5280 static ptrdiff_t
5281 string_buffer_position_lim (Lisp_Object string,
5282 ptrdiff_t from, ptrdiff_t to, int back_p)
5283 {
5284 Lisp_Object limit, prop, pos;
5285 int found = 0;
5286
5287 pos = make_number (max (from, BEGV));
5288
5289 if (!back_p) /* looking forward */
5290 {
5291 limit = make_number (min (to, ZV));
5292 while (!found && !EQ (pos, limit))
5293 {
5294 prop = Fget_char_property (pos, Qdisplay, Qnil);
5295 if (!NILP (prop) && display_prop_string_p (prop, string))
5296 found = 1;
5297 else
5298 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5299 limit);
5300 }
5301 }
5302 else /* looking back */
5303 {
5304 limit = make_number (max (to, BEGV));
5305 while (!found && !EQ (pos, limit))
5306 {
5307 prop = Fget_char_property (pos, Qdisplay, Qnil);
5308 if (!NILP (prop) && display_prop_string_p (prop, string))
5309 found = 1;
5310 else
5311 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5312 limit);
5313 }
5314 }
5315
5316 return found ? XINT (pos) : 0;
5317 }
5318
5319 /* Determine which buffer position in current buffer STRING comes from.
5320 AROUND_CHARPOS is an approximate position where it could come from.
5321 Value is the buffer position or 0 if it couldn't be determined.
5322
5323 This function is necessary because we don't record buffer positions
5324 in glyphs generated from strings (to keep struct glyph small).
5325 This function may only use code that doesn't eval because it is
5326 called asynchronously from note_mouse_highlight. */
5327
5328 static ptrdiff_t
5329 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5330 {
5331 const int MAX_DISTANCE = 1000;
5332 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5333 around_charpos + MAX_DISTANCE,
5334 0);
5335
5336 if (!found)
5337 found = string_buffer_position_lim (string, around_charpos,
5338 around_charpos - MAX_DISTANCE, 1);
5339 return found;
5340 }
5341
5342
5343 \f
5344 /***********************************************************************
5345 `composition' property
5346 ***********************************************************************/
5347
5348 /* Set up iterator IT from `composition' property at its current
5349 position. Called from handle_stop. */
5350
5351 static enum prop_handled
5352 handle_composition_prop (struct it *it)
5353 {
5354 Lisp_Object prop, string;
5355 ptrdiff_t pos, pos_byte, start, end;
5356
5357 if (STRINGP (it->string))
5358 {
5359 unsigned char *s;
5360
5361 pos = IT_STRING_CHARPOS (*it);
5362 pos_byte = IT_STRING_BYTEPOS (*it);
5363 string = it->string;
5364 s = SDATA (string) + pos_byte;
5365 it->c = STRING_CHAR (s);
5366 }
5367 else
5368 {
5369 pos = IT_CHARPOS (*it);
5370 pos_byte = IT_BYTEPOS (*it);
5371 string = Qnil;
5372 it->c = FETCH_CHAR (pos_byte);
5373 }
5374
5375 /* If there's a valid composition and point is not inside of the
5376 composition (in the case that the composition is from the current
5377 buffer), draw a glyph composed from the composition components. */
5378 if (find_composition (pos, -1, &start, &end, &prop, string)
5379 && composition_valid_p (start, end, prop)
5380 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5381 {
5382 if (start < pos)
5383 /* As we can't handle this situation (perhaps font-lock added
5384 a new composition), we just return here hoping that next
5385 redisplay will detect this composition much earlier. */
5386 return HANDLED_NORMALLY;
5387 if (start != pos)
5388 {
5389 if (STRINGP (it->string))
5390 pos_byte = string_char_to_byte (it->string, start);
5391 else
5392 pos_byte = CHAR_TO_BYTE (start);
5393 }
5394 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5395 prop, string);
5396
5397 if (it->cmp_it.id >= 0)
5398 {
5399 it->cmp_it.ch = -1;
5400 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5401 it->cmp_it.nglyphs = -1;
5402 }
5403 }
5404
5405 return HANDLED_NORMALLY;
5406 }
5407
5408
5409 \f
5410 /***********************************************************************
5411 Overlay strings
5412 ***********************************************************************/
5413
5414 /* The following structure is used to record overlay strings for
5415 later sorting in load_overlay_strings. */
5416
5417 struct overlay_entry
5418 {
5419 Lisp_Object overlay;
5420 Lisp_Object string;
5421 EMACS_INT priority;
5422 int after_string_p;
5423 };
5424
5425
5426 /* Set up iterator IT from overlay strings at its current position.
5427 Called from handle_stop. */
5428
5429 static enum prop_handled
5430 handle_overlay_change (struct it *it)
5431 {
5432 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5433 return HANDLED_RECOMPUTE_PROPS;
5434 else
5435 return HANDLED_NORMALLY;
5436 }
5437
5438
5439 /* Set up the next overlay string for delivery by IT, if there is an
5440 overlay string to deliver. Called by set_iterator_to_next when the
5441 end of the current overlay string is reached. If there are more
5442 overlay strings to display, IT->string and
5443 IT->current.overlay_string_index are set appropriately here.
5444 Otherwise IT->string is set to nil. */
5445
5446 static void
5447 next_overlay_string (struct it *it)
5448 {
5449 ++it->current.overlay_string_index;
5450 if (it->current.overlay_string_index == it->n_overlay_strings)
5451 {
5452 /* No more overlay strings. Restore IT's settings to what
5453 they were before overlay strings were processed, and
5454 continue to deliver from current_buffer. */
5455
5456 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5457 pop_it (it);
5458 eassert (it->sp > 0
5459 || (NILP (it->string)
5460 && it->method == GET_FROM_BUFFER
5461 && it->stop_charpos >= BEGV
5462 && it->stop_charpos <= it->end_charpos));
5463 it->current.overlay_string_index = -1;
5464 it->n_overlay_strings = 0;
5465 it->overlay_strings_charpos = -1;
5466 /* If there's an empty display string on the stack, pop the
5467 stack, to resync the bidi iterator with IT's position. Such
5468 empty strings are pushed onto the stack in
5469 get_overlay_strings_1. */
5470 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5471 pop_it (it);
5472
5473 /* If we're at the end of the buffer, record that we have
5474 processed the overlay strings there already, so that
5475 next_element_from_buffer doesn't try it again. */
5476 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5477 it->overlay_strings_at_end_processed_p = 1;
5478 }
5479 else
5480 {
5481 /* There are more overlay strings to process. If
5482 IT->current.overlay_string_index has advanced to a position
5483 where we must load IT->overlay_strings with more strings, do
5484 it. We must load at the IT->overlay_strings_charpos where
5485 IT->n_overlay_strings was originally computed; when invisible
5486 text is present, this might not be IT_CHARPOS (Bug#7016). */
5487 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5488
5489 if (it->current.overlay_string_index && i == 0)
5490 load_overlay_strings (it, it->overlay_strings_charpos);
5491
5492 /* Initialize IT to deliver display elements from the overlay
5493 string. */
5494 it->string = it->overlay_strings[i];
5495 it->multibyte_p = STRING_MULTIBYTE (it->string);
5496 SET_TEXT_POS (it->current.string_pos, 0, 0);
5497 it->method = GET_FROM_STRING;
5498 it->stop_charpos = 0;
5499 it->end_charpos = SCHARS (it->string);
5500 if (it->cmp_it.stop_pos >= 0)
5501 it->cmp_it.stop_pos = 0;
5502 it->prev_stop = 0;
5503 it->base_level_stop = 0;
5504
5505 /* Set up the bidi iterator for this overlay string. */
5506 if (it->bidi_p)
5507 {
5508 it->bidi_it.string.lstring = it->string;
5509 it->bidi_it.string.s = NULL;
5510 it->bidi_it.string.schars = SCHARS (it->string);
5511 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5512 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5513 it->bidi_it.string.unibyte = !it->multibyte_p;
5514 it->bidi_it.w = it->w;
5515 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5516 }
5517 }
5518
5519 CHECK_IT (it);
5520 }
5521
5522
5523 /* Compare two overlay_entry structures E1 and E2. Used as a
5524 comparison function for qsort in load_overlay_strings. Overlay
5525 strings for the same position are sorted so that
5526
5527 1. All after-strings come in front of before-strings, except
5528 when they come from the same overlay.
5529
5530 2. Within after-strings, strings are sorted so that overlay strings
5531 from overlays with higher priorities come first.
5532
5533 2. Within before-strings, strings are sorted so that overlay
5534 strings from overlays with higher priorities come last.
5535
5536 Value is analogous to strcmp. */
5537
5538
5539 static int
5540 compare_overlay_entries (const void *e1, const void *e2)
5541 {
5542 struct overlay_entry const *entry1 = e1;
5543 struct overlay_entry const *entry2 = e2;
5544 int result;
5545
5546 if (entry1->after_string_p != entry2->after_string_p)
5547 {
5548 /* Let after-strings appear in front of before-strings if
5549 they come from different overlays. */
5550 if (EQ (entry1->overlay, entry2->overlay))
5551 result = entry1->after_string_p ? 1 : -1;
5552 else
5553 result = entry1->after_string_p ? -1 : 1;
5554 }
5555 else if (entry1->priority != entry2->priority)
5556 {
5557 if (entry1->after_string_p)
5558 /* After-strings sorted in order of decreasing priority. */
5559 result = entry2->priority < entry1->priority ? -1 : 1;
5560 else
5561 /* Before-strings sorted in order of increasing priority. */
5562 result = entry1->priority < entry2->priority ? -1 : 1;
5563 }
5564 else
5565 result = 0;
5566
5567 return result;
5568 }
5569
5570
5571 /* Load the vector IT->overlay_strings with overlay strings from IT's
5572 current buffer position, or from CHARPOS if that is > 0. Set
5573 IT->n_overlays to the total number of overlay strings found.
5574
5575 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5576 a time. On entry into load_overlay_strings,
5577 IT->current.overlay_string_index gives the number of overlay
5578 strings that have already been loaded by previous calls to this
5579 function.
5580
5581 IT->add_overlay_start contains an additional overlay start
5582 position to consider for taking overlay strings from, if non-zero.
5583 This position comes into play when the overlay has an `invisible'
5584 property, and both before and after-strings. When we've skipped to
5585 the end of the overlay, because of its `invisible' property, we
5586 nevertheless want its before-string to appear.
5587 IT->add_overlay_start will contain the overlay start position
5588 in this case.
5589
5590 Overlay strings are sorted so that after-string strings come in
5591 front of before-string strings. Within before and after-strings,
5592 strings are sorted by overlay priority. See also function
5593 compare_overlay_entries. */
5594
5595 static void
5596 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5597 {
5598 Lisp_Object overlay, window, str, invisible;
5599 struct Lisp_Overlay *ov;
5600 ptrdiff_t start, end;
5601 ptrdiff_t size = 20;
5602 ptrdiff_t n = 0, i, j;
5603 int invis_p;
5604 struct overlay_entry *entries = alloca (size * sizeof *entries);
5605 USE_SAFE_ALLOCA;
5606
5607 if (charpos <= 0)
5608 charpos = IT_CHARPOS (*it);
5609
5610 /* Append the overlay string STRING of overlay OVERLAY to vector
5611 `entries' which has size `size' and currently contains `n'
5612 elements. AFTER_P non-zero means STRING is an after-string of
5613 OVERLAY. */
5614 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5615 do \
5616 { \
5617 Lisp_Object priority; \
5618 \
5619 if (n == size) \
5620 { \
5621 struct overlay_entry *old = entries; \
5622 SAFE_NALLOCA (entries, 2, size); \
5623 memcpy (entries, old, size * sizeof *entries); \
5624 size *= 2; \
5625 } \
5626 \
5627 entries[n].string = (STRING); \
5628 entries[n].overlay = (OVERLAY); \
5629 priority = Foverlay_get ((OVERLAY), Qpriority); \
5630 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5631 entries[n].after_string_p = (AFTER_P); \
5632 ++n; \
5633 } \
5634 while (0)
5635
5636 /* Process overlay before the overlay center. */
5637 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5638 {
5639 XSETMISC (overlay, ov);
5640 eassert (OVERLAYP (overlay));
5641 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5642 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5643
5644 if (end < charpos)
5645 break;
5646
5647 /* Skip this overlay if it doesn't start or end at IT's current
5648 position. */
5649 if (end != charpos && start != charpos)
5650 continue;
5651
5652 /* Skip this overlay if it doesn't apply to IT->w. */
5653 window = Foverlay_get (overlay, Qwindow);
5654 if (WINDOWP (window) && XWINDOW (window) != it->w)
5655 continue;
5656
5657 /* If the text ``under'' the overlay is invisible, both before-
5658 and after-strings from this overlay are visible; start and
5659 end position are indistinguishable. */
5660 invisible = Foverlay_get (overlay, Qinvisible);
5661 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5662
5663 /* If overlay has a non-empty before-string, record it. */
5664 if ((start == charpos || (end == charpos && invis_p))
5665 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5666 && SCHARS (str))
5667 RECORD_OVERLAY_STRING (overlay, str, 0);
5668
5669 /* If overlay has a non-empty after-string, record it. */
5670 if ((end == charpos || (start == charpos && invis_p))
5671 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5672 && SCHARS (str))
5673 RECORD_OVERLAY_STRING (overlay, str, 1);
5674 }
5675
5676 /* Process overlays after the overlay center. */
5677 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5678 {
5679 XSETMISC (overlay, ov);
5680 eassert (OVERLAYP (overlay));
5681 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5682 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5683
5684 if (start > charpos)
5685 break;
5686
5687 /* Skip this overlay if it doesn't start or end at IT's current
5688 position. */
5689 if (end != charpos && start != charpos)
5690 continue;
5691
5692 /* Skip this overlay if it doesn't apply to IT->w. */
5693 window = Foverlay_get (overlay, Qwindow);
5694 if (WINDOWP (window) && XWINDOW (window) != it->w)
5695 continue;
5696
5697 /* If the text ``under'' the overlay is invisible, it has a zero
5698 dimension, and both before- and after-strings apply. */
5699 invisible = Foverlay_get (overlay, Qinvisible);
5700 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5701
5702 /* If overlay has a non-empty before-string, record it. */
5703 if ((start == charpos || (end == charpos && invis_p))
5704 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5705 && SCHARS (str))
5706 RECORD_OVERLAY_STRING (overlay, str, 0);
5707
5708 /* If overlay has a non-empty after-string, record it. */
5709 if ((end == charpos || (start == charpos && invis_p))
5710 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5711 && SCHARS (str))
5712 RECORD_OVERLAY_STRING (overlay, str, 1);
5713 }
5714
5715 #undef RECORD_OVERLAY_STRING
5716
5717 /* Sort entries. */
5718 if (n > 1)
5719 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5720
5721 /* Record number of overlay strings, and where we computed it. */
5722 it->n_overlay_strings = n;
5723 it->overlay_strings_charpos = charpos;
5724
5725 /* IT->current.overlay_string_index is the number of overlay strings
5726 that have already been consumed by IT. Copy some of the
5727 remaining overlay strings to IT->overlay_strings. */
5728 i = 0;
5729 j = it->current.overlay_string_index;
5730 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5731 {
5732 it->overlay_strings[i] = entries[j].string;
5733 it->string_overlays[i++] = entries[j++].overlay;
5734 }
5735
5736 CHECK_IT (it);
5737 SAFE_FREE ();
5738 }
5739
5740
5741 /* Get the first chunk of overlay strings at IT's current buffer
5742 position, or at CHARPOS if that is > 0. Value is non-zero if at
5743 least one overlay string was found. */
5744
5745 static int
5746 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5747 {
5748 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5749 process. This fills IT->overlay_strings with strings, and sets
5750 IT->n_overlay_strings to the total number of strings to process.
5751 IT->pos.overlay_string_index has to be set temporarily to zero
5752 because load_overlay_strings needs this; it must be set to -1
5753 when no overlay strings are found because a zero value would
5754 indicate a position in the first overlay string. */
5755 it->current.overlay_string_index = 0;
5756 load_overlay_strings (it, charpos);
5757
5758 /* If we found overlay strings, set up IT to deliver display
5759 elements from the first one. Otherwise set up IT to deliver
5760 from current_buffer. */
5761 if (it->n_overlay_strings)
5762 {
5763 /* Make sure we know settings in current_buffer, so that we can
5764 restore meaningful values when we're done with the overlay
5765 strings. */
5766 if (compute_stop_p)
5767 compute_stop_pos (it);
5768 eassert (it->face_id >= 0);
5769
5770 /* Save IT's settings. They are restored after all overlay
5771 strings have been processed. */
5772 eassert (!compute_stop_p || it->sp == 0);
5773
5774 /* When called from handle_stop, there might be an empty display
5775 string loaded. In that case, don't bother saving it. But
5776 don't use this optimization with the bidi iterator, since we
5777 need the corresponding pop_it call to resync the bidi
5778 iterator's position with IT's position, after we are done
5779 with the overlay strings. (The corresponding call to pop_it
5780 in case of an empty display string is in
5781 next_overlay_string.) */
5782 if (!(!it->bidi_p
5783 && STRINGP (it->string) && !SCHARS (it->string)))
5784 push_it (it, NULL);
5785
5786 /* Set up IT to deliver display elements from the first overlay
5787 string. */
5788 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5789 it->string = it->overlay_strings[0];
5790 it->from_overlay = Qnil;
5791 it->stop_charpos = 0;
5792 eassert (STRINGP (it->string));
5793 it->end_charpos = SCHARS (it->string);
5794 it->prev_stop = 0;
5795 it->base_level_stop = 0;
5796 it->multibyte_p = STRING_MULTIBYTE (it->string);
5797 it->method = GET_FROM_STRING;
5798 it->from_disp_prop_p = 0;
5799
5800 /* Force paragraph direction to be that of the parent
5801 buffer. */
5802 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5803 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5804 else
5805 it->paragraph_embedding = L2R;
5806
5807 /* Set up the bidi iterator for this overlay string. */
5808 if (it->bidi_p)
5809 {
5810 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5811
5812 it->bidi_it.string.lstring = it->string;
5813 it->bidi_it.string.s = NULL;
5814 it->bidi_it.string.schars = SCHARS (it->string);
5815 it->bidi_it.string.bufpos = pos;
5816 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5817 it->bidi_it.string.unibyte = !it->multibyte_p;
5818 it->bidi_it.w = it->w;
5819 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5820 }
5821 return 1;
5822 }
5823
5824 it->current.overlay_string_index = -1;
5825 return 0;
5826 }
5827
5828 static int
5829 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5830 {
5831 it->string = Qnil;
5832 it->method = GET_FROM_BUFFER;
5833
5834 (void) get_overlay_strings_1 (it, charpos, 1);
5835
5836 CHECK_IT (it);
5837
5838 /* Value is non-zero if we found at least one overlay string. */
5839 return STRINGP (it->string);
5840 }
5841
5842
5843 \f
5844 /***********************************************************************
5845 Saving and restoring state
5846 ***********************************************************************/
5847
5848 /* Save current settings of IT on IT->stack. Called, for example,
5849 before setting up IT for an overlay string, to be able to restore
5850 IT's settings to what they were after the overlay string has been
5851 processed. If POSITION is non-NULL, it is the position to save on
5852 the stack instead of IT->position. */
5853
5854 static void
5855 push_it (struct it *it, struct text_pos *position)
5856 {
5857 struct iterator_stack_entry *p;
5858
5859 eassert (it->sp < IT_STACK_SIZE);
5860 p = it->stack + it->sp;
5861
5862 p->stop_charpos = it->stop_charpos;
5863 p->prev_stop = it->prev_stop;
5864 p->base_level_stop = it->base_level_stop;
5865 p->cmp_it = it->cmp_it;
5866 eassert (it->face_id >= 0);
5867 p->face_id = it->face_id;
5868 p->string = it->string;
5869 p->method = it->method;
5870 p->from_overlay = it->from_overlay;
5871 switch (p->method)
5872 {
5873 case GET_FROM_IMAGE:
5874 p->u.image.object = it->object;
5875 p->u.image.image_id = it->image_id;
5876 p->u.image.slice = it->slice;
5877 break;
5878 case GET_FROM_STRETCH:
5879 p->u.stretch.object = it->object;
5880 break;
5881 #ifdef HAVE_XWIDGETS
5882 case GET_FROM_XWIDGET:
5883 p->u.xwidget.object = it->object;
5884 break;
5885 #endif
5886 }
5887 p->position = position ? *position : it->position;
5888 p->current = it->current;
5889 p->end_charpos = it->end_charpos;
5890 p->string_nchars = it->string_nchars;
5891 p->area = it->area;
5892 p->multibyte_p = it->multibyte_p;
5893 p->avoid_cursor_p = it->avoid_cursor_p;
5894 p->space_width = it->space_width;
5895 p->font_height = it->font_height;
5896 p->voffset = it->voffset;
5897 p->string_from_display_prop_p = it->string_from_display_prop_p;
5898 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5899 p->display_ellipsis_p = 0;
5900 p->line_wrap = it->line_wrap;
5901 p->bidi_p = it->bidi_p;
5902 p->paragraph_embedding = it->paragraph_embedding;
5903 p->from_disp_prop_p = it->from_disp_prop_p;
5904 ++it->sp;
5905
5906 /* Save the state of the bidi iterator as well. */
5907 if (it->bidi_p)
5908 bidi_push_it (&it->bidi_it);
5909 }
5910
5911 static void
5912 iterate_out_of_display_property (struct it *it)
5913 {
5914 int buffer_p = !STRINGP (it->string);
5915 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5916 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5917
5918 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5919
5920 /* Maybe initialize paragraph direction. If we are at the beginning
5921 of a new paragraph, next_element_from_buffer may not have a
5922 chance to do that. */
5923 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5924 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5925 /* prev_stop can be zero, so check against BEGV as well. */
5926 while (it->bidi_it.charpos >= bob
5927 && it->prev_stop <= it->bidi_it.charpos
5928 && it->bidi_it.charpos < CHARPOS (it->position)
5929 && it->bidi_it.charpos < eob)
5930 bidi_move_to_visually_next (&it->bidi_it);
5931 /* Record the stop_pos we just crossed, for when we cross it
5932 back, maybe. */
5933 if (it->bidi_it.charpos > CHARPOS (it->position))
5934 it->prev_stop = CHARPOS (it->position);
5935 /* If we ended up not where pop_it put us, resync IT's
5936 positional members with the bidi iterator. */
5937 if (it->bidi_it.charpos != CHARPOS (it->position))
5938 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5939 if (buffer_p)
5940 it->current.pos = it->position;
5941 else
5942 it->current.string_pos = it->position;
5943 }
5944
5945 /* Restore IT's settings from IT->stack. Called, for example, when no
5946 more overlay strings must be processed, and we return to delivering
5947 display elements from a buffer, or when the end of a string from a
5948 `display' property is reached and we return to delivering display
5949 elements from an overlay string, or from a buffer. */
5950
5951 static void
5952 pop_it (struct it *it)
5953 {
5954 struct iterator_stack_entry *p;
5955 int from_display_prop = it->from_disp_prop_p;
5956
5957 eassert (it->sp > 0);
5958 --it->sp;
5959 p = it->stack + it->sp;
5960 it->stop_charpos = p->stop_charpos;
5961 it->prev_stop = p->prev_stop;
5962 it->base_level_stop = p->base_level_stop;
5963 it->cmp_it = p->cmp_it;
5964 it->face_id = p->face_id;
5965 it->current = p->current;
5966 it->position = p->position;
5967 it->string = p->string;
5968 it->from_overlay = p->from_overlay;
5969 if (NILP (it->string))
5970 SET_TEXT_POS (it->current.string_pos, -1, -1);
5971 it->method = p->method;
5972 switch (it->method)
5973 {
5974 case GET_FROM_IMAGE:
5975 it->image_id = p->u.image.image_id;
5976 it->object = p->u.image.object;
5977 it->slice = p->u.image.slice;
5978 break;
5979 #ifdef HAVE_XWIDGETS
5980 case GET_FROM_XWIDGET:
5981 it->object = p->u.xwidget.object;
5982 break;
5983 #endif
5984 case GET_FROM_STRETCH:
5985 it->object = p->u.stretch.object;
5986 break;
5987 case GET_FROM_BUFFER:
5988 it->object = it->w->contents;
5989 break;
5990 case GET_FROM_STRING:
5991 it->object = it->string;
5992 break;
5993 case GET_FROM_DISPLAY_VECTOR:
5994 if (it->s)
5995 it->method = GET_FROM_C_STRING;
5996 else if (STRINGP (it->string))
5997 it->method = GET_FROM_STRING;
5998 else
5999 {
6000 it->method = GET_FROM_BUFFER;
6001 it->object = it->w->contents;
6002 }
6003 }
6004 it->end_charpos = p->end_charpos;
6005 it->string_nchars = p->string_nchars;
6006 it->area = p->area;
6007 it->multibyte_p = p->multibyte_p;
6008 it->avoid_cursor_p = p->avoid_cursor_p;
6009 it->space_width = p->space_width;
6010 it->font_height = p->font_height;
6011 it->voffset = p->voffset;
6012 it->string_from_display_prop_p = p->string_from_display_prop_p;
6013 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6014 it->line_wrap = p->line_wrap;
6015 it->bidi_p = p->bidi_p;
6016 it->paragraph_embedding = p->paragraph_embedding;
6017 it->from_disp_prop_p = p->from_disp_prop_p;
6018 if (it->bidi_p)
6019 {
6020 bidi_pop_it (&it->bidi_it);
6021 /* Bidi-iterate until we get out of the portion of text, if any,
6022 covered by a `display' text property or by an overlay with
6023 `display' property. (We cannot just jump there, because the
6024 internal coherency of the bidi iterator state can not be
6025 preserved across such jumps.) We also must determine the
6026 paragraph base direction if the overlay we just processed is
6027 at the beginning of a new paragraph. */
6028 if (from_display_prop
6029 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6030 iterate_out_of_display_property (it);
6031
6032 eassert ((BUFFERP (it->object)
6033 && IT_CHARPOS (*it) == it->bidi_it.charpos
6034 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6035 || (STRINGP (it->object)
6036 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6037 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6038 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6039 }
6040 }
6041
6042
6043 \f
6044 /***********************************************************************
6045 Moving over lines
6046 ***********************************************************************/
6047
6048 /* Set IT's current position to the previous line start. */
6049
6050 static void
6051 back_to_previous_line_start (struct it *it)
6052 {
6053 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6054
6055 DEC_BOTH (cp, bp);
6056 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6057 }
6058
6059
6060 /* Move IT to the next line start.
6061
6062 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6063 we skipped over part of the text (as opposed to moving the iterator
6064 continuously over the text). Otherwise, don't change the value
6065 of *SKIPPED_P.
6066
6067 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6068 iterator on the newline, if it was found.
6069
6070 Newlines may come from buffer text, overlay strings, or strings
6071 displayed via the `display' property. That's the reason we can't
6072 simply use find_newline_no_quit.
6073
6074 Note that this function may not skip over invisible text that is so
6075 because of text properties and immediately follows a newline. If
6076 it would, function reseat_at_next_visible_line_start, when called
6077 from set_iterator_to_next, would effectively make invisible
6078 characters following a newline part of the wrong glyph row, which
6079 leads to wrong cursor motion. */
6080
6081 static int
6082 forward_to_next_line_start (struct it *it, int *skipped_p,
6083 struct bidi_it *bidi_it_prev)
6084 {
6085 ptrdiff_t old_selective;
6086 int newline_found_p, n;
6087 const int MAX_NEWLINE_DISTANCE = 500;
6088
6089 /* If already on a newline, just consume it to avoid unintended
6090 skipping over invisible text below. */
6091 if (it->what == IT_CHARACTER
6092 && it->c == '\n'
6093 && CHARPOS (it->position) == IT_CHARPOS (*it))
6094 {
6095 if (it->bidi_p && bidi_it_prev)
6096 *bidi_it_prev = it->bidi_it;
6097 set_iterator_to_next (it, 0);
6098 it->c = 0;
6099 return 1;
6100 }
6101
6102 /* Don't handle selective display in the following. It's (a)
6103 unnecessary because it's done by the caller, and (b) leads to an
6104 infinite recursion because next_element_from_ellipsis indirectly
6105 calls this function. */
6106 old_selective = it->selective;
6107 it->selective = 0;
6108
6109 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6110 from buffer text. */
6111 for (n = newline_found_p = 0;
6112 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6113 n += STRINGP (it->string) ? 0 : 1)
6114 {
6115 if (!get_next_display_element (it))
6116 return 0;
6117 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6118 if (newline_found_p && it->bidi_p && bidi_it_prev)
6119 *bidi_it_prev = it->bidi_it;
6120 set_iterator_to_next (it, 0);
6121 }
6122
6123 /* If we didn't find a newline near enough, see if we can use a
6124 short-cut. */
6125 if (!newline_found_p)
6126 {
6127 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6128 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6129 1, &bytepos);
6130 Lisp_Object pos;
6131
6132 eassert (!STRINGP (it->string));
6133
6134 /* If there isn't any `display' property in sight, and no
6135 overlays, we can just use the position of the newline in
6136 buffer text. */
6137 if (it->stop_charpos >= limit
6138 || ((pos = Fnext_single_property_change (make_number (start),
6139 Qdisplay, Qnil,
6140 make_number (limit)),
6141 NILP (pos))
6142 && next_overlay_change (start) == ZV))
6143 {
6144 if (!it->bidi_p)
6145 {
6146 IT_CHARPOS (*it) = limit;
6147 IT_BYTEPOS (*it) = bytepos;
6148 }
6149 else
6150 {
6151 struct bidi_it bprev;
6152
6153 /* Help bidi.c avoid expensive searches for display
6154 properties and overlays, by telling it that there are
6155 none up to `limit'. */
6156 if (it->bidi_it.disp_pos < limit)
6157 {
6158 it->bidi_it.disp_pos = limit;
6159 it->bidi_it.disp_prop = 0;
6160 }
6161 do {
6162 bprev = it->bidi_it;
6163 bidi_move_to_visually_next (&it->bidi_it);
6164 } while (it->bidi_it.charpos != limit);
6165 IT_CHARPOS (*it) = limit;
6166 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6167 if (bidi_it_prev)
6168 *bidi_it_prev = bprev;
6169 }
6170 *skipped_p = newline_found_p = 1;
6171 }
6172 else
6173 {
6174 while (get_next_display_element (it)
6175 && !newline_found_p)
6176 {
6177 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6178 if (newline_found_p && it->bidi_p && bidi_it_prev)
6179 *bidi_it_prev = it->bidi_it;
6180 set_iterator_to_next (it, 0);
6181 }
6182 }
6183 }
6184
6185 it->selective = old_selective;
6186 return newline_found_p;
6187 }
6188
6189
6190 /* Set IT's current position to the previous visible line start. Skip
6191 invisible text that is so either due to text properties or due to
6192 selective display. Caution: this does not change IT->current_x and
6193 IT->hpos. */
6194
6195 static void
6196 back_to_previous_visible_line_start (struct it *it)
6197 {
6198 while (IT_CHARPOS (*it) > BEGV)
6199 {
6200 back_to_previous_line_start (it);
6201
6202 if (IT_CHARPOS (*it) <= BEGV)
6203 break;
6204
6205 /* If selective > 0, then lines indented more than its value are
6206 invisible. */
6207 if (it->selective > 0
6208 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6209 it->selective))
6210 continue;
6211
6212 /* Check the newline before point for invisibility. */
6213 {
6214 Lisp_Object prop;
6215 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6216 Qinvisible, it->window);
6217 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6218 continue;
6219 }
6220
6221 if (IT_CHARPOS (*it) <= BEGV)
6222 break;
6223
6224 {
6225 struct it it2;
6226 void *it2data = NULL;
6227 ptrdiff_t pos;
6228 ptrdiff_t beg, end;
6229 Lisp_Object val, overlay;
6230
6231 SAVE_IT (it2, *it, it2data);
6232
6233 /* If newline is part of a composition, continue from start of composition */
6234 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6235 && beg < IT_CHARPOS (*it))
6236 goto replaced;
6237
6238 /* If newline is replaced by a display property, find start of overlay
6239 or interval and continue search from that point. */
6240 pos = --IT_CHARPOS (it2);
6241 --IT_BYTEPOS (it2);
6242 it2.sp = 0;
6243 bidi_unshelve_cache (NULL, 0);
6244 it2.string_from_display_prop_p = 0;
6245 it2.from_disp_prop_p = 0;
6246 if (handle_display_prop (&it2) == HANDLED_RETURN
6247 && !NILP (val = get_char_property_and_overlay
6248 (make_number (pos), Qdisplay, Qnil, &overlay))
6249 && (OVERLAYP (overlay)
6250 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6251 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6252 {
6253 RESTORE_IT (it, it, it2data);
6254 goto replaced;
6255 }
6256
6257 /* Newline is not replaced by anything -- so we are done. */
6258 RESTORE_IT (it, it, it2data);
6259 break;
6260
6261 replaced:
6262 if (beg < BEGV)
6263 beg = BEGV;
6264 IT_CHARPOS (*it) = beg;
6265 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6266 }
6267 }
6268
6269 it->continuation_lines_width = 0;
6270
6271 eassert (IT_CHARPOS (*it) >= BEGV);
6272 eassert (IT_CHARPOS (*it) == BEGV
6273 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6274 CHECK_IT (it);
6275 }
6276
6277
6278 /* Reseat iterator IT at the previous visible line start. Skip
6279 invisible text that is so either due to text properties or due to
6280 selective display. At the end, update IT's overlay information,
6281 face information etc. */
6282
6283 void
6284 reseat_at_previous_visible_line_start (struct it *it)
6285 {
6286 back_to_previous_visible_line_start (it);
6287 reseat (it, it->current.pos, 1);
6288 CHECK_IT (it);
6289 }
6290
6291
6292 /* Reseat iterator IT on the next visible line start in the current
6293 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6294 preceding the line start. Skip over invisible text that is so
6295 because of selective display. Compute faces, overlays etc at the
6296 new position. Note that this function does not skip over text that
6297 is invisible because of text properties. */
6298
6299 static void
6300 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6301 {
6302 int newline_found_p, skipped_p = 0;
6303 struct bidi_it bidi_it_prev;
6304
6305 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6306
6307 /* Skip over lines that are invisible because they are indented
6308 more than the value of IT->selective. */
6309 if (it->selective > 0)
6310 while (IT_CHARPOS (*it) < ZV
6311 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6312 it->selective))
6313 {
6314 eassert (IT_BYTEPOS (*it) == BEGV
6315 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6316 newline_found_p =
6317 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6318 }
6319
6320 /* Position on the newline if that's what's requested. */
6321 if (on_newline_p && newline_found_p)
6322 {
6323 if (STRINGP (it->string))
6324 {
6325 if (IT_STRING_CHARPOS (*it) > 0)
6326 {
6327 if (!it->bidi_p)
6328 {
6329 --IT_STRING_CHARPOS (*it);
6330 --IT_STRING_BYTEPOS (*it);
6331 }
6332 else
6333 {
6334 /* We need to restore the bidi iterator to the state
6335 it had on the newline, and resync the IT's
6336 position with that. */
6337 it->bidi_it = bidi_it_prev;
6338 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6339 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6340 }
6341 }
6342 }
6343 else if (IT_CHARPOS (*it) > BEGV)
6344 {
6345 if (!it->bidi_p)
6346 {
6347 --IT_CHARPOS (*it);
6348 --IT_BYTEPOS (*it);
6349 }
6350 else
6351 {
6352 /* We need to restore the bidi iterator to the state it
6353 had on the newline and resync IT with that. */
6354 it->bidi_it = bidi_it_prev;
6355 IT_CHARPOS (*it) = it->bidi_it.charpos;
6356 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6357 }
6358 reseat (it, it->current.pos, 0);
6359 }
6360 }
6361 else if (skipped_p)
6362 reseat (it, it->current.pos, 0);
6363
6364 CHECK_IT (it);
6365 }
6366
6367
6368 \f
6369 /***********************************************************************
6370 Changing an iterator's position
6371 ***********************************************************************/
6372
6373 /* Change IT's current position to POS in current_buffer. If FORCE_P
6374 is non-zero, always check for text properties at the new position.
6375 Otherwise, text properties are only looked up if POS >=
6376 IT->check_charpos of a property. */
6377
6378 static void
6379 reseat (struct it *it, struct text_pos pos, int force_p)
6380 {
6381 ptrdiff_t original_pos = IT_CHARPOS (*it);
6382
6383 reseat_1 (it, pos, 0);
6384
6385 /* Determine where to check text properties. Avoid doing it
6386 where possible because text property lookup is very expensive. */
6387 if (force_p
6388 || CHARPOS (pos) > it->stop_charpos
6389 || CHARPOS (pos) < original_pos)
6390 {
6391 if (it->bidi_p)
6392 {
6393 /* For bidi iteration, we need to prime prev_stop and
6394 base_level_stop with our best estimations. */
6395 /* Implementation note: Of course, POS is not necessarily a
6396 stop position, so assigning prev_pos to it is a lie; we
6397 should have called compute_stop_backwards. However, if
6398 the current buffer does not include any R2L characters,
6399 that call would be a waste of cycles, because the
6400 iterator will never move back, and thus never cross this
6401 "fake" stop position. So we delay that backward search
6402 until the time we really need it, in next_element_from_buffer. */
6403 if (CHARPOS (pos) != it->prev_stop)
6404 it->prev_stop = CHARPOS (pos);
6405 if (CHARPOS (pos) < it->base_level_stop)
6406 it->base_level_stop = 0; /* meaning it's unknown */
6407 handle_stop (it);
6408 }
6409 else
6410 {
6411 handle_stop (it);
6412 it->prev_stop = it->base_level_stop = 0;
6413 }
6414
6415 }
6416
6417 CHECK_IT (it);
6418 }
6419
6420
6421 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6422 IT->stop_pos to POS, also. */
6423
6424 static void
6425 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6426 {
6427 /* Don't call this function when scanning a C string. */
6428 eassert (it->s == NULL);
6429
6430 /* POS must be a reasonable value. */
6431 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6432
6433 it->current.pos = it->position = pos;
6434 it->end_charpos = ZV;
6435 it->dpvec = NULL;
6436 it->current.dpvec_index = -1;
6437 it->current.overlay_string_index = -1;
6438 IT_STRING_CHARPOS (*it) = -1;
6439 IT_STRING_BYTEPOS (*it) = -1;
6440 it->string = Qnil;
6441 it->method = GET_FROM_BUFFER;
6442 it->object = it->w->contents;
6443 it->area = TEXT_AREA;
6444 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6445 it->sp = 0;
6446 it->string_from_display_prop_p = 0;
6447 it->string_from_prefix_prop_p = 0;
6448
6449 it->from_disp_prop_p = 0;
6450 it->face_before_selective_p = 0;
6451 if (it->bidi_p)
6452 {
6453 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6454 &it->bidi_it);
6455 bidi_unshelve_cache (NULL, 0);
6456 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6457 it->bidi_it.string.s = NULL;
6458 it->bidi_it.string.lstring = Qnil;
6459 it->bidi_it.string.bufpos = 0;
6460 it->bidi_it.string.unibyte = 0;
6461 it->bidi_it.w = it->w;
6462 }
6463
6464 if (set_stop_p)
6465 {
6466 it->stop_charpos = CHARPOS (pos);
6467 it->base_level_stop = CHARPOS (pos);
6468 }
6469 /* This make the information stored in it->cmp_it invalidate. */
6470 it->cmp_it.id = -1;
6471 }
6472
6473
6474 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6475 If S is non-null, it is a C string to iterate over. Otherwise,
6476 STRING gives a Lisp string to iterate over.
6477
6478 If PRECISION > 0, don't return more then PRECISION number of
6479 characters from the string.
6480
6481 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6482 characters have been returned. FIELD_WIDTH < 0 means an infinite
6483 field width.
6484
6485 MULTIBYTE = 0 means disable processing of multibyte characters,
6486 MULTIBYTE > 0 means enable it,
6487 MULTIBYTE < 0 means use IT->multibyte_p.
6488
6489 IT must be initialized via a prior call to init_iterator before
6490 calling this function. */
6491
6492 static void
6493 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6494 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6495 int multibyte)
6496 {
6497 /* No region in strings. */
6498 it->region_beg_charpos = it->region_end_charpos = -1;
6499
6500 /* No text property checks performed by default, but see below. */
6501 it->stop_charpos = -1;
6502
6503 /* Set iterator position and end position. */
6504 memset (&it->current, 0, sizeof it->current);
6505 it->current.overlay_string_index = -1;
6506 it->current.dpvec_index = -1;
6507 eassert (charpos >= 0);
6508
6509 /* If STRING is specified, use its multibyteness, otherwise use the
6510 setting of MULTIBYTE, if specified. */
6511 if (multibyte >= 0)
6512 it->multibyte_p = multibyte > 0;
6513
6514 /* Bidirectional reordering of strings is controlled by the default
6515 value of bidi-display-reordering. Don't try to reorder while
6516 loading loadup.el, as the necessary character property tables are
6517 not yet available. */
6518 it->bidi_p =
6519 NILP (Vpurify_flag)
6520 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6521
6522 if (s == NULL)
6523 {
6524 eassert (STRINGP (string));
6525 it->string = string;
6526 it->s = NULL;
6527 it->end_charpos = it->string_nchars = SCHARS (string);
6528 it->method = GET_FROM_STRING;
6529 it->current.string_pos = string_pos (charpos, string);
6530
6531 if (it->bidi_p)
6532 {
6533 it->bidi_it.string.lstring = string;
6534 it->bidi_it.string.s = NULL;
6535 it->bidi_it.string.schars = it->end_charpos;
6536 it->bidi_it.string.bufpos = 0;
6537 it->bidi_it.string.from_disp_str = 0;
6538 it->bidi_it.string.unibyte = !it->multibyte_p;
6539 it->bidi_it.w = it->w;
6540 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6541 FRAME_WINDOW_P (it->f), &it->bidi_it);
6542 }
6543 }
6544 else
6545 {
6546 it->s = (const unsigned char *) s;
6547 it->string = Qnil;
6548
6549 /* Note that we use IT->current.pos, not it->current.string_pos,
6550 for displaying C strings. */
6551 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6552 if (it->multibyte_p)
6553 {
6554 it->current.pos = c_string_pos (charpos, s, 1);
6555 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6556 }
6557 else
6558 {
6559 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6560 it->end_charpos = it->string_nchars = strlen (s);
6561 }
6562
6563 if (it->bidi_p)
6564 {
6565 it->bidi_it.string.lstring = Qnil;
6566 it->bidi_it.string.s = (const unsigned char *) s;
6567 it->bidi_it.string.schars = it->end_charpos;
6568 it->bidi_it.string.bufpos = 0;
6569 it->bidi_it.string.from_disp_str = 0;
6570 it->bidi_it.string.unibyte = !it->multibyte_p;
6571 it->bidi_it.w = it->w;
6572 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6573 &it->bidi_it);
6574 }
6575 it->method = GET_FROM_C_STRING;
6576 }
6577
6578 /* PRECISION > 0 means don't return more than PRECISION characters
6579 from the string. */
6580 if (precision > 0 && it->end_charpos - charpos > precision)
6581 {
6582 it->end_charpos = it->string_nchars = charpos + precision;
6583 if (it->bidi_p)
6584 it->bidi_it.string.schars = it->end_charpos;
6585 }
6586
6587 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6588 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6589 FIELD_WIDTH < 0 means infinite field width. This is useful for
6590 padding with `-' at the end of a mode line. */
6591 if (field_width < 0)
6592 field_width = INFINITY;
6593 /* Implementation note: We deliberately don't enlarge
6594 it->bidi_it.string.schars here to fit it->end_charpos, because
6595 the bidi iterator cannot produce characters out of thin air. */
6596 if (field_width > it->end_charpos - charpos)
6597 it->end_charpos = charpos + field_width;
6598
6599 /* Use the standard display table for displaying strings. */
6600 if (DISP_TABLE_P (Vstandard_display_table))
6601 it->dp = XCHAR_TABLE (Vstandard_display_table);
6602
6603 it->stop_charpos = charpos;
6604 it->prev_stop = charpos;
6605 it->base_level_stop = 0;
6606 if (it->bidi_p)
6607 {
6608 it->bidi_it.first_elt = 1;
6609 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6610 it->bidi_it.disp_pos = -1;
6611 }
6612 if (s == NULL && it->multibyte_p)
6613 {
6614 ptrdiff_t endpos = SCHARS (it->string);
6615 if (endpos > it->end_charpos)
6616 endpos = it->end_charpos;
6617 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6618 it->string);
6619 }
6620 CHECK_IT (it);
6621 }
6622
6623
6624 \f
6625 /***********************************************************************
6626 Iteration
6627 ***********************************************************************/
6628
6629 /* Map enum it_method value to corresponding next_element_from_* function. */
6630
6631 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6632 {
6633 next_element_from_buffer,
6634 next_element_from_display_vector,
6635 next_element_from_string,
6636 next_element_from_c_string,
6637 next_element_from_image,
6638 next_element_from_stretch
6639 #ifdef HAVE_XWIDGETS
6640 ,next_element_from_xwidget
6641 #endif
6642 };
6643
6644 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6645
6646
6647 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6648 (possibly with the following characters). */
6649
6650 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6651 ((IT)->cmp_it.id >= 0 \
6652 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6653 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6654 END_CHARPOS, (IT)->w, \
6655 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6656 (IT)->string)))
6657
6658
6659 /* Lookup the char-table Vglyphless_char_display for character C (-1
6660 if we want information for no-font case), and return the display
6661 method symbol. By side-effect, update it->what and
6662 it->glyphless_method. This function is called from
6663 get_next_display_element for each character element, and from
6664 x_produce_glyphs when no suitable font was found. */
6665
6666 Lisp_Object
6667 lookup_glyphless_char_display (int c, struct it *it)
6668 {
6669 Lisp_Object glyphless_method = Qnil;
6670
6671 if (CHAR_TABLE_P (Vglyphless_char_display)
6672 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6673 {
6674 if (c >= 0)
6675 {
6676 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6677 if (CONSP (glyphless_method))
6678 glyphless_method = FRAME_WINDOW_P (it->f)
6679 ? XCAR (glyphless_method)
6680 : XCDR (glyphless_method);
6681 }
6682 else
6683 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6684 }
6685
6686 retry:
6687 if (NILP (glyphless_method))
6688 {
6689 if (c >= 0)
6690 /* The default is to display the character by a proper font. */
6691 return Qnil;
6692 /* The default for the no-font case is to display an empty box. */
6693 glyphless_method = Qempty_box;
6694 }
6695 if (EQ (glyphless_method, Qzero_width))
6696 {
6697 if (c >= 0)
6698 return glyphless_method;
6699 /* This method can't be used for the no-font case. */
6700 glyphless_method = Qempty_box;
6701 }
6702 if (EQ (glyphless_method, Qthin_space))
6703 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6704 else if (EQ (glyphless_method, Qempty_box))
6705 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6706 else if (EQ (glyphless_method, Qhex_code))
6707 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6708 else if (STRINGP (glyphless_method))
6709 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6710 else
6711 {
6712 /* Invalid value. We use the default method. */
6713 glyphless_method = Qnil;
6714 goto retry;
6715 }
6716 it->what = IT_GLYPHLESS;
6717 return glyphless_method;
6718 }
6719
6720 /* Load IT's display element fields with information about the next
6721 display element from the current position of IT. Value is zero if
6722 end of buffer (or C string) is reached. */
6723
6724 static struct frame *last_escape_glyph_frame = NULL;
6725 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6726 static int last_escape_glyph_merged_face_id = 0;
6727
6728 struct frame *last_glyphless_glyph_frame = NULL;
6729 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6730 int last_glyphless_glyph_merged_face_id = 0;
6731
6732 static int
6733 get_next_display_element (struct it *it)
6734 {
6735 /* Non-zero means that we found a display element. Zero means that
6736 we hit the end of what we iterate over. Performance note: the
6737 function pointer `method' used here turns out to be faster than
6738 using a sequence of if-statements. */
6739 int success_p;
6740
6741 get_next:
6742 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6743
6744 if (it->what == IT_CHARACTER)
6745 {
6746 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6747 and only if (a) the resolved directionality of that character
6748 is R..." */
6749 /* FIXME: Do we need an exception for characters from display
6750 tables? */
6751 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6752 it->c = bidi_mirror_char (it->c);
6753 /* Map via display table or translate control characters.
6754 IT->c, IT->len etc. have been set to the next character by
6755 the function call above. If we have a display table, and it
6756 contains an entry for IT->c, translate it. Don't do this if
6757 IT->c itself comes from a display table, otherwise we could
6758 end up in an infinite recursion. (An alternative could be to
6759 count the recursion depth of this function and signal an
6760 error when a certain maximum depth is reached.) Is it worth
6761 it? */
6762 if (success_p && it->dpvec == NULL)
6763 {
6764 Lisp_Object dv;
6765 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6766 int nonascii_space_p = 0;
6767 int nonascii_hyphen_p = 0;
6768 int c = it->c; /* This is the character to display. */
6769
6770 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6771 {
6772 eassert (SINGLE_BYTE_CHAR_P (c));
6773 if (unibyte_display_via_language_environment)
6774 {
6775 c = DECODE_CHAR (unibyte, c);
6776 if (c < 0)
6777 c = BYTE8_TO_CHAR (it->c);
6778 }
6779 else
6780 c = BYTE8_TO_CHAR (it->c);
6781 }
6782
6783 if (it->dp
6784 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6785 VECTORP (dv)))
6786 {
6787 struct Lisp_Vector *v = XVECTOR (dv);
6788
6789 /* Return the first character from the display table
6790 entry, if not empty. If empty, don't display the
6791 current character. */
6792 if (v->header.size)
6793 {
6794 it->dpvec_char_len = it->len;
6795 it->dpvec = v->contents;
6796 it->dpend = v->contents + v->header.size;
6797 it->current.dpvec_index = 0;
6798 it->dpvec_face_id = -1;
6799 it->saved_face_id = it->face_id;
6800 it->method = GET_FROM_DISPLAY_VECTOR;
6801 it->ellipsis_p = 0;
6802 }
6803 else
6804 {
6805 set_iterator_to_next (it, 0);
6806 }
6807 goto get_next;
6808 }
6809
6810 if (! NILP (lookup_glyphless_char_display (c, it)))
6811 {
6812 if (it->what == IT_GLYPHLESS)
6813 goto done;
6814 /* Don't display this character. */
6815 set_iterator_to_next (it, 0);
6816 goto get_next;
6817 }
6818
6819 /* If `nobreak-char-display' is non-nil, we display
6820 non-ASCII spaces and hyphens specially. */
6821 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6822 {
6823 if (c == 0xA0)
6824 nonascii_space_p = 1;
6825 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6826 nonascii_hyphen_p = 1;
6827 }
6828
6829 /* Translate control characters into `\003' or `^C' form.
6830 Control characters coming from a display table entry are
6831 currently not translated because we use IT->dpvec to hold
6832 the translation. This could easily be changed but I
6833 don't believe that it is worth doing.
6834
6835 The characters handled by `nobreak-char-display' must be
6836 translated too.
6837
6838 Non-printable characters and raw-byte characters are also
6839 translated to octal form. */
6840 if (((c < ' ' || c == 127) /* ASCII control chars */
6841 ? (it->area != TEXT_AREA
6842 /* In mode line, treat \n, \t like other crl chars. */
6843 || (c != '\t'
6844 && it->glyph_row
6845 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6846 || (c != '\n' && c != '\t'))
6847 : (nonascii_space_p
6848 || nonascii_hyphen_p
6849 || CHAR_BYTE8_P (c)
6850 || ! CHAR_PRINTABLE_P (c))))
6851 {
6852 /* C is a control character, non-ASCII space/hyphen,
6853 raw-byte, or a non-printable character which must be
6854 displayed either as '\003' or as `^C' where the '\\'
6855 and '^' can be defined in the display table. Fill
6856 IT->ctl_chars with glyphs for what we have to
6857 display. Then, set IT->dpvec to these glyphs. */
6858 Lisp_Object gc;
6859 int ctl_len;
6860 int face_id;
6861 int lface_id = 0;
6862 int escape_glyph;
6863
6864 /* Handle control characters with ^. */
6865
6866 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6867 {
6868 int g;
6869
6870 g = '^'; /* default glyph for Control */
6871 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6872 if (it->dp
6873 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6874 {
6875 g = GLYPH_CODE_CHAR (gc);
6876 lface_id = GLYPH_CODE_FACE (gc);
6877 }
6878 if (lface_id)
6879 {
6880 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6881 }
6882 else if (it->f == last_escape_glyph_frame
6883 && it->face_id == last_escape_glyph_face_id)
6884 {
6885 face_id = last_escape_glyph_merged_face_id;
6886 }
6887 else
6888 {
6889 /* Merge the escape-glyph face into the current face. */
6890 face_id = merge_faces (it->f, Qescape_glyph, 0,
6891 it->face_id);
6892 last_escape_glyph_frame = it->f;
6893 last_escape_glyph_face_id = it->face_id;
6894 last_escape_glyph_merged_face_id = face_id;
6895 }
6896
6897 XSETINT (it->ctl_chars[0], g);
6898 XSETINT (it->ctl_chars[1], c ^ 0100);
6899 ctl_len = 2;
6900 goto display_control;
6901 }
6902
6903 /* Handle non-ascii space in the mode where it only gets
6904 highlighting. */
6905
6906 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6907 {
6908 /* Merge `nobreak-space' into the current face. */
6909 face_id = merge_faces (it->f, Qnobreak_space, 0,
6910 it->face_id);
6911 XSETINT (it->ctl_chars[0], ' ');
6912 ctl_len = 1;
6913 goto display_control;
6914 }
6915
6916 /* Handle sequences that start with the "escape glyph". */
6917
6918 /* the default escape glyph is \. */
6919 escape_glyph = '\\';
6920
6921 if (it->dp
6922 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6923 {
6924 escape_glyph = GLYPH_CODE_CHAR (gc);
6925 lface_id = GLYPH_CODE_FACE (gc);
6926 }
6927 if (lface_id)
6928 {
6929 /* The display table specified a face.
6930 Merge it into face_id and also into escape_glyph. */
6931 face_id = merge_faces (it->f, Qt, lface_id,
6932 it->face_id);
6933 }
6934 else if (it->f == last_escape_glyph_frame
6935 && it->face_id == last_escape_glyph_face_id)
6936 {
6937 face_id = last_escape_glyph_merged_face_id;
6938 }
6939 else
6940 {
6941 /* Merge the escape-glyph face into the current face. */
6942 face_id = merge_faces (it->f, Qescape_glyph, 0,
6943 it->face_id);
6944 last_escape_glyph_frame = it->f;
6945 last_escape_glyph_face_id = it->face_id;
6946 last_escape_glyph_merged_face_id = face_id;
6947 }
6948
6949 /* Draw non-ASCII hyphen with just highlighting: */
6950
6951 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6952 {
6953 XSETINT (it->ctl_chars[0], '-');
6954 ctl_len = 1;
6955 goto display_control;
6956 }
6957
6958 /* Draw non-ASCII space/hyphen with escape glyph: */
6959
6960 if (nonascii_space_p || nonascii_hyphen_p)
6961 {
6962 XSETINT (it->ctl_chars[0], escape_glyph);
6963 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6964 ctl_len = 2;
6965 goto display_control;
6966 }
6967
6968 {
6969 char str[10];
6970 int len, i;
6971
6972 if (CHAR_BYTE8_P (c))
6973 /* Display \200 instead of \17777600. */
6974 c = CHAR_TO_BYTE8 (c);
6975 len = sprintf (str, "%03o", c);
6976
6977 XSETINT (it->ctl_chars[0], escape_glyph);
6978 for (i = 0; i < len; i++)
6979 XSETINT (it->ctl_chars[i + 1], str[i]);
6980 ctl_len = len + 1;
6981 }
6982
6983 display_control:
6984 /* Set up IT->dpvec and return first character from it. */
6985 it->dpvec_char_len = it->len;
6986 it->dpvec = it->ctl_chars;
6987 it->dpend = it->dpvec + ctl_len;
6988 it->current.dpvec_index = 0;
6989 it->dpvec_face_id = face_id;
6990 it->saved_face_id = it->face_id;
6991 it->method = GET_FROM_DISPLAY_VECTOR;
6992 it->ellipsis_p = 0;
6993 goto get_next;
6994 }
6995 it->char_to_display = c;
6996 }
6997 else if (success_p)
6998 {
6999 it->char_to_display = it->c;
7000 }
7001 }
7002
7003 /* Adjust face id for a multibyte character. There are no multibyte
7004 character in unibyte text. */
7005 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7006 && it->multibyte_p
7007 && success_p
7008 && FRAME_WINDOW_P (it->f))
7009 {
7010 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7011
7012 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7013 {
7014 /* Automatic composition with glyph-string. */
7015 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7016
7017 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7018 }
7019 else
7020 {
7021 ptrdiff_t pos = (it->s ? -1
7022 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7023 : IT_CHARPOS (*it));
7024 int c;
7025
7026 if (it->what == IT_CHARACTER)
7027 c = it->char_to_display;
7028 else
7029 {
7030 struct composition *cmp = composition_table[it->cmp_it.id];
7031 int i;
7032
7033 c = ' ';
7034 for (i = 0; i < cmp->glyph_len; i++)
7035 /* TAB in a composition means display glyphs with
7036 padding space on the left or right. */
7037 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7038 break;
7039 }
7040 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7041 }
7042 }
7043
7044 done:
7045 /* Is this character the last one of a run of characters with
7046 box? If yes, set IT->end_of_box_run_p to 1. */
7047 if (it->face_box_p
7048 && it->s == NULL)
7049 {
7050 if (it->method == GET_FROM_STRING && it->sp)
7051 {
7052 int face_id = underlying_face_id (it);
7053 struct face *face = FACE_FROM_ID (it->f, face_id);
7054
7055 if (face)
7056 {
7057 if (face->box == FACE_NO_BOX)
7058 {
7059 /* If the box comes from face properties in a
7060 display string, check faces in that string. */
7061 int string_face_id = face_after_it_pos (it);
7062 it->end_of_box_run_p
7063 = (FACE_FROM_ID (it->f, string_face_id)->box
7064 == FACE_NO_BOX);
7065 }
7066 /* Otherwise, the box comes from the underlying face.
7067 If this is the last string character displayed, check
7068 the next buffer location. */
7069 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7070 && (it->current.overlay_string_index
7071 == it->n_overlay_strings - 1))
7072 {
7073 ptrdiff_t ignore;
7074 int next_face_id;
7075 struct text_pos pos = it->current.pos;
7076 INC_TEXT_POS (pos, it->multibyte_p);
7077
7078 next_face_id = face_at_buffer_position
7079 (it->w, CHARPOS (pos), it->region_beg_charpos,
7080 it->region_end_charpos, &ignore,
7081 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7082 -1);
7083 it->end_of_box_run_p
7084 = (FACE_FROM_ID (it->f, next_face_id)->box
7085 == FACE_NO_BOX);
7086 }
7087 }
7088 }
7089 else
7090 {
7091 int face_id = face_after_it_pos (it);
7092 it->end_of_box_run_p
7093 = (face_id != it->face_id
7094 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7095 }
7096 }
7097 /* If we reached the end of the object we've been iterating (e.g., a
7098 display string or an overlay string), and there's something on
7099 IT->stack, proceed with what's on the stack. It doesn't make
7100 sense to return zero if there's unprocessed stuff on the stack,
7101 because otherwise that stuff will never be displayed. */
7102 if (!success_p && it->sp > 0)
7103 {
7104 set_iterator_to_next (it, 0);
7105 success_p = get_next_display_element (it);
7106 }
7107
7108 /* Value is 0 if end of buffer or string reached. */
7109 return success_p;
7110 }
7111
7112
7113 /* Move IT to the next display element.
7114
7115 RESEAT_P non-zero means if called on a newline in buffer text,
7116 skip to the next visible line start.
7117
7118 Functions get_next_display_element and set_iterator_to_next are
7119 separate because I find this arrangement easier to handle than a
7120 get_next_display_element function that also increments IT's
7121 position. The way it is we can first look at an iterator's current
7122 display element, decide whether it fits on a line, and if it does,
7123 increment the iterator position. The other way around we probably
7124 would either need a flag indicating whether the iterator has to be
7125 incremented the next time, or we would have to implement a
7126 decrement position function which would not be easy to write. */
7127
7128 void
7129 set_iterator_to_next (struct it *it, int reseat_p)
7130 {
7131 /* Reset flags indicating start and end of a sequence of characters
7132 with box. Reset them at the start of this function because
7133 moving the iterator to a new position might set them. */
7134 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7135
7136 switch (it->method)
7137 {
7138 case GET_FROM_BUFFER:
7139 /* The current display element of IT is a character from
7140 current_buffer. Advance in the buffer, and maybe skip over
7141 invisible lines that are so because of selective display. */
7142 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7143 reseat_at_next_visible_line_start (it, 0);
7144 else if (it->cmp_it.id >= 0)
7145 {
7146 /* We are currently getting glyphs from a composition. */
7147 int i;
7148
7149 if (! it->bidi_p)
7150 {
7151 IT_CHARPOS (*it) += it->cmp_it.nchars;
7152 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7153 if (it->cmp_it.to < it->cmp_it.nglyphs)
7154 {
7155 it->cmp_it.from = it->cmp_it.to;
7156 }
7157 else
7158 {
7159 it->cmp_it.id = -1;
7160 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7161 IT_BYTEPOS (*it),
7162 it->end_charpos, Qnil);
7163 }
7164 }
7165 else if (! it->cmp_it.reversed_p)
7166 {
7167 /* Composition created while scanning forward. */
7168 /* Update IT's char/byte positions to point to the first
7169 character of the next grapheme cluster, or to the
7170 character visually after the current composition. */
7171 for (i = 0; i < it->cmp_it.nchars; i++)
7172 bidi_move_to_visually_next (&it->bidi_it);
7173 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7174 IT_CHARPOS (*it) = it->bidi_it.charpos;
7175
7176 if (it->cmp_it.to < it->cmp_it.nglyphs)
7177 {
7178 /* Proceed to the next grapheme cluster. */
7179 it->cmp_it.from = it->cmp_it.to;
7180 }
7181 else
7182 {
7183 /* No more grapheme clusters in this composition.
7184 Find the next stop position. */
7185 ptrdiff_t stop = it->end_charpos;
7186 if (it->bidi_it.scan_dir < 0)
7187 /* Now we are scanning backward and don't know
7188 where to stop. */
7189 stop = -1;
7190 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7191 IT_BYTEPOS (*it), stop, Qnil);
7192 }
7193 }
7194 else
7195 {
7196 /* Composition created while scanning backward. */
7197 /* Update IT's char/byte positions to point to the last
7198 character of the previous grapheme cluster, or the
7199 character visually after the current composition. */
7200 for (i = 0; i < it->cmp_it.nchars; i++)
7201 bidi_move_to_visually_next (&it->bidi_it);
7202 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7203 IT_CHARPOS (*it) = it->bidi_it.charpos;
7204 if (it->cmp_it.from > 0)
7205 {
7206 /* Proceed to the previous grapheme cluster. */
7207 it->cmp_it.to = it->cmp_it.from;
7208 }
7209 else
7210 {
7211 /* No more grapheme clusters in this composition.
7212 Find the next stop position. */
7213 ptrdiff_t stop = it->end_charpos;
7214 if (it->bidi_it.scan_dir < 0)
7215 /* Now we are scanning backward and don't know
7216 where to stop. */
7217 stop = -1;
7218 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7219 IT_BYTEPOS (*it), stop, Qnil);
7220 }
7221 }
7222 }
7223 else
7224 {
7225 eassert (it->len != 0);
7226
7227 if (!it->bidi_p)
7228 {
7229 IT_BYTEPOS (*it) += it->len;
7230 IT_CHARPOS (*it) += 1;
7231 }
7232 else
7233 {
7234 int prev_scan_dir = it->bidi_it.scan_dir;
7235 /* If this is a new paragraph, determine its base
7236 direction (a.k.a. its base embedding level). */
7237 if (it->bidi_it.new_paragraph)
7238 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7239 bidi_move_to_visually_next (&it->bidi_it);
7240 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7241 IT_CHARPOS (*it) = it->bidi_it.charpos;
7242 if (prev_scan_dir != it->bidi_it.scan_dir)
7243 {
7244 /* As the scan direction was changed, we must
7245 re-compute the stop position for composition. */
7246 ptrdiff_t stop = it->end_charpos;
7247 if (it->bidi_it.scan_dir < 0)
7248 stop = -1;
7249 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7250 IT_BYTEPOS (*it), stop, Qnil);
7251 }
7252 }
7253 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7254 }
7255 break;
7256
7257 case GET_FROM_C_STRING:
7258 /* Current display element of IT is from a C string. */
7259 if (!it->bidi_p
7260 /* If the string position is beyond string's end, it means
7261 next_element_from_c_string is padding the string with
7262 blanks, in which case we bypass the bidi iterator,
7263 because it cannot deal with such virtual characters. */
7264 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7265 {
7266 IT_BYTEPOS (*it) += it->len;
7267 IT_CHARPOS (*it) += 1;
7268 }
7269 else
7270 {
7271 bidi_move_to_visually_next (&it->bidi_it);
7272 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7273 IT_CHARPOS (*it) = it->bidi_it.charpos;
7274 }
7275 break;
7276
7277 case GET_FROM_DISPLAY_VECTOR:
7278 /* Current display element of IT is from a display table entry.
7279 Advance in the display table definition. Reset it to null if
7280 end reached, and continue with characters from buffers/
7281 strings. */
7282 ++it->current.dpvec_index;
7283
7284 /* Restore face of the iterator to what they were before the
7285 display vector entry (these entries may contain faces). */
7286 it->face_id = it->saved_face_id;
7287
7288 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7289 {
7290 int recheck_faces = it->ellipsis_p;
7291
7292 if (it->s)
7293 it->method = GET_FROM_C_STRING;
7294 else if (STRINGP (it->string))
7295 it->method = GET_FROM_STRING;
7296 else
7297 {
7298 it->method = GET_FROM_BUFFER;
7299 it->object = it->w->contents;
7300 }
7301
7302 it->dpvec = NULL;
7303 it->current.dpvec_index = -1;
7304
7305 /* Skip over characters which were displayed via IT->dpvec. */
7306 if (it->dpvec_char_len < 0)
7307 reseat_at_next_visible_line_start (it, 1);
7308 else if (it->dpvec_char_len > 0)
7309 {
7310 if (it->method == GET_FROM_STRING
7311 && it->current.overlay_string_index >= 0
7312 && it->n_overlay_strings > 0)
7313 it->ignore_overlay_strings_at_pos_p = 1;
7314 it->len = it->dpvec_char_len;
7315 set_iterator_to_next (it, reseat_p);
7316 }
7317
7318 /* Maybe recheck faces after display vector */
7319 if (recheck_faces)
7320 it->stop_charpos = IT_CHARPOS (*it);
7321 }
7322 break;
7323
7324 case GET_FROM_STRING:
7325 /* Current display element is a character from a Lisp string. */
7326 eassert (it->s == NULL && STRINGP (it->string));
7327 /* Don't advance past string end. These conditions are true
7328 when set_iterator_to_next is called at the end of
7329 get_next_display_element, in which case the Lisp string is
7330 already exhausted, and all we want is pop the iterator
7331 stack. */
7332 if (it->current.overlay_string_index >= 0)
7333 {
7334 /* This is an overlay string, so there's no padding with
7335 spaces, and the number of characters in the string is
7336 where the string ends. */
7337 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7338 goto consider_string_end;
7339 }
7340 else
7341 {
7342 /* Not an overlay string. There could be padding, so test
7343 against it->end_charpos . */
7344 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7345 goto consider_string_end;
7346 }
7347 if (it->cmp_it.id >= 0)
7348 {
7349 int i;
7350
7351 if (! it->bidi_p)
7352 {
7353 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7354 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7355 if (it->cmp_it.to < it->cmp_it.nglyphs)
7356 it->cmp_it.from = it->cmp_it.to;
7357 else
7358 {
7359 it->cmp_it.id = -1;
7360 composition_compute_stop_pos (&it->cmp_it,
7361 IT_STRING_CHARPOS (*it),
7362 IT_STRING_BYTEPOS (*it),
7363 it->end_charpos, it->string);
7364 }
7365 }
7366 else if (! it->cmp_it.reversed_p)
7367 {
7368 for (i = 0; i < it->cmp_it.nchars; i++)
7369 bidi_move_to_visually_next (&it->bidi_it);
7370 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7371 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7372
7373 if (it->cmp_it.to < it->cmp_it.nglyphs)
7374 it->cmp_it.from = it->cmp_it.to;
7375 else
7376 {
7377 ptrdiff_t stop = it->end_charpos;
7378 if (it->bidi_it.scan_dir < 0)
7379 stop = -1;
7380 composition_compute_stop_pos (&it->cmp_it,
7381 IT_STRING_CHARPOS (*it),
7382 IT_STRING_BYTEPOS (*it), stop,
7383 it->string);
7384 }
7385 }
7386 else
7387 {
7388 for (i = 0; i < it->cmp_it.nchars; i++)
7389 bidi_move_to_visually_next (&it->bidi_it);
7390 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7391 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7392 if (it->cmp_it.from > 0)
7393 it->cmp_it.to = it->cmp_it.from;
7394 else
7395 {
7396 ptrdiff_t stop = it->end_charpos;
7397 if (it->bidi_it.scan_dir < 0)
7398 stop = -1;
7399 composition_compute_stop_pos (&it->cmp_it,
7400 IT_STRING_CHARPOS (*it),
7401 IT_STRING_BYTEPOS (*it), stop,
7402 it->string);
7403 }
7404 }
7405 }
7406 else
7407 {
7408 if (!it->bidi_p
7409 /* If the string position is beyond string's end, it
7410 means next_element_from_string is padding the string
7411 with blanks, in which case we bypass the bidi
7412 iterator, because it cannot deal with such virtual
7413 characters. */
7414 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7415 {
7416 IT_STRING_BYTEPOS (*it) += it->len;
7417 IT_STRING_CHARPOS (*it) += 1;
7418 }
7419 else
7420 {
7421 int prev_scan_dir = it->bidi_it.scan_dir;
7422
7423 bidi_move_to_visually_next (&it->bidi_it);
7424 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7425 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7426 if (prev_scan_dir != it->bidi_it.scan_dir)
7427 {
7428 ptrdiff_t stop = it->end_charpos;
7429
7430 if (it->bidi_it.scan_dir < 0)
7431 stop = -1;
7432 composition_compute_stop_pos (&it->cmp_it,
7433 IT_STRING_CHARPOS (*it),
7434 IT_STRING_BYTEPOS (*it), stop,
7435 it->string);
7436 }
7437 }
7438 }
7439
7440 consider_string_end:
7441
7442 if (it->current.overlay_string_index >= 0)
7443 {
7444 /* IT->string is an overlay string. Advance to the
7445 next, if there is one. */
7446 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7447 {
7448 it->ellipsis_p = 0;
7449 next_overlay_string (it);
7450 if (it->ellipsis_p)
7451 setup_for_ellipsis (it, 0);
7452 }
7453 }
7454 else
7455 {
7456 /* IT->string is not an overlay string. If we reached
7457 its end, and there is something on IT->stack, proceed
7458 with what is on the stack. This can be either another
7459 string, this time an overlay string, or a buffer. */
7460 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7461 && it->sp > 0)
7462 {
7463 pop_it (it);
7464 if (it->method == GET_FROM_STRING)
7465 goto consider_string_end;
7466 }
7467 }
7468 break;
7469
7470 case GET_FROM_IMAGE:
7471 case GET_FROM_STRETCH:
7472 #ifdef HAVE_XWIDGETS
7473 case GET_FROM_XWIDGET:
7474
7475 /* The position etc with which we have to proceed are on
7476 the stack. The position may be at the end of a string,
7477 if the `display' property takes up the whole string. */
7478 eassert (it->sp > 0);
7479 pop_it (it);
7480 if (it->method == GET_FROM_STRING)
7481 goto consider_string_end;
7482 break;
7483 #endif
7484 default:
7485 /* There are no other methods defined, so this should be a bug. */
7486 emacs_abort ();
7487 }
7488
7489 eassert (it->method != GET_FROM_STRING
7490 || (STRINGP (it->string)
7491 && IT_STRING_CHARPOS (*it) >= 0));
7492 }
7493
7494 /* Load IT's display element fields with information about the next
7495 display element which comes from a display table entry or from the
7496 result of translating a control character to one of the forms `^C'
7497 or `\003'.
7498
7499 IT->dpvec holds the glyphs to return as characters.
7500 IT->saved_face_id holds the face id before the display vector--it
7501 is restored into IT->face_id in set_iterator_to_next. */
7502
7503 static int
7504 next_element_from_display_vector (struct it *it)
7505 {
7506 Lisp_Object gc;
7507 int prev_face_id = it->face_id;
7508 int next_face_id;
7509
7510 /* Precondition. */
7511 eassert (it->dpvec && it->current.dpvec_index >= 0);
7512
7513 it->face_id = it->saved_face_id;
7514
7515 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7516 That seemed totally bogus - so I changed it... */
7517 gc = it->dpvec[it->current.dpvec_index];
7518
7519 if (GLYPH_CODE_P (gc))
7520 {
7521 struct face *this_face, *prev_face, *next_face;
7522
7523 it->c = GLYPH_CODE_CHAR (gc);
7524 it->len = CHAR_BYTES (it->c);
7525
7526 /* The entry may contain a face id to use. Such a face id is
7527 the id of a Lisp face, not a realized face. A face id of
7528 zero means no face is specified. */
7529 if (it->dpvec_face_id >= 0)
7530 it->face_id = it->dpvec_face_id;
7531 else
7532 {
7533 int lface_id = GLYPH_CODE_FACE (gc);
7534 if (lface_id > 0)
7535 it->face_id = merge_faces (it->f, Qt, lface_id,
7536 it->saved_face_id);
7537 }
7538
7539 /* Glyphs in the display vector could have the box face, so we
7540 need to set the related flags in the iterator, as
7541 appropriate. */
7542 this_face = FACE_FROM_ID (it->f, it->face_id);
7543 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7544
7545 /* Is this character the first character of a box-face run? */
7546 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7547 && (!prev_face
7548 || prev_face->box == FACE_NO_BOX));
7549
7550 /* For the last character of the box-face run, we need to look
7551 either at the next glyph from the display vector, or at the
7552 face we saw before the display vector. */
7553 next_face_id = it->saved_face_id;
7554 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7555 {
7556 if (it->dpvec_face_id >= 0)
7557 next_face_id = it->dpvec_face_id;
7558 else
7559 {
7560 int lface_id =
7561 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7562
7563 if (lface_id > 0)
7564 next_face_id = merge_faces (it->f, Qt, lface_id,
7565 it->saved_face_id);
7566 }
7567 }
7568 next_face = FACE_FROM_ID (it->f, next_face_id);
7569 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7570 && (!next_face
7571 || next_face->box == FACE_NO_BOX));
7572 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7573 }
7574 else
7575 /* Display table entry is invalid. Return a space. */
7576 it->c = ' ', it->len = 1;
7577
7578 /* Don't change position and object of the iterator here. They are
7579 still the values of the character that had this display table
7580 entry or was translated, and that's what we want. */
7581 it->what = IT_CHARACTER;
7582 return 1;
7583 }
7584
7585 /* Get the first element of string/buffer in the visual order, after
7586 being reseated to a new position in a string or a buffer. */
7587 static void
7588 get_visually_first_element (struct it *it)
7589 {
7590 int string_p = STRINGP (it->string) || it->s;
7591 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7592 ptrdiff_t bob = (string_p ? 0 : BEGV);
7593
7594 if (STRINGP (it->string))
7595 {
7596 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7597 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7598 }
7599 else
7600 {
7601 it->bidi_it.charpos = IT_CHARPOS (*it);
7602 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7603 }
7604
7605 if (it->bidi_it.charpos == eob)
7606 {
7607 /* Nothing to do, but reset the FIRST_ELT flag, like
7608 bidi_paragraph_init does, because we are not going to
7609 call it. */
7610 it->bidi_it.first_elt = 0;
7611 }
7612 else if (it->bidi_it.charpos == bob
7613 || (!string_p
7614 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7615 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7616 {
7617 /* If we are at the beginning of a line/string, we can produce
7618 the next element right away. */
7619 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7620 bidi_move_to_visually_next (&it->bidi_it);
7621 }
7622 else
7623 {
7624 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7625
7626 /* We need to prime the bidi iterator starting at the line's or
7627 string's beginning, before we will be able to produce the
7628 next element. */
7629 if (string_p)
7630 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7631 else
7632 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7633 IT_BYTEPOS (*it), -1,
7634 &it->bidi_it.bytepos);
7635 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7636 do
7637 {
7638 /* Now return to buffer/string position where we were asked
7639 to get the next display element, and produce that. */
7640 bidi_move_to_visually_next (&it->bidi_it);
7641 }
7642 while (it->bidi_it.bytepos != orig_bytepos
7643 && it->bidi_it.charpos < eob);
7644 }
7645
7646 /* Adjust IT's position information to where we ended up. */
7647 if (STRINGP (it->string))
7648 {
7649 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7650 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7651 }
7652 else
7653 {
7654 IT_CHARPOS (*it) = it->bidi_it.charpos;
7655 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7656 }
7657
7658 if (STRINGP (it->string) || !it->s)
7659 {
7660 ptrdiff_t stop, charpos, bytepos;
7661
7662 if (STRINGP (it->string))
7663 {
7664 eassert (!it->s);
7665 stop = SCHARS (it->string);
7666 if (stop > it->end_charpos)
7667 stop = it->end_charpos;
7668 charpos = IT_STRING_CHARPOS (*it);
7669 bytepos = IT_STRING_BYTEPOS (*it);
7670 }
7671 else
7672 {
7673 stop = it->end_charpos;
7674 charpos = IT_CHARPOS (*it);
7675 bytepos = IT_BYTEPOS (*it);
7676 }
7677 if (it->bidi_it.scan_dir < 0)
7678 stop = -1;
7679 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7680 it->string);
7681 }
7682 }
7683
7684 /* Load IT with the next display element from Lisp string IT->string.
7685 IT->current.string_pos is the current position within the string.
7686 If IT->current.overlay_string_index >= 0, the Lisp string is an
7687 overlay string. */
7688
7689 static int
7690 next_element_from_string (struct it *it)
7691 {
7692 struct text_pos position;
7693
7694 eassert (STRINGP (it->string));
7695 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7696 eassert (IT_STRING_CHARPOS (*it) >= 0);
7697 position = it->current.string_pos;
7698
7699 /* With bidi reordering, the character to display might not be the
7700 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7701 that we were reseat()ed to a new string, whose paragraph
7702 direction is not known. */
7703 if (it->bidi_p && it->bidi_it.first_elt)
7704 {
7705 get_visually_first_element (it);
7706 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7707 }
7708
7709 /* Time to check for invisible text? */
7710 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7711 {
7712 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7713 {
7714 if (!(!it->bidi_p
7715 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7716 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7717 {
7718 /* With bidi non-linear iteration, we could find
7719 ourselves far beyond the last computed stop_charpos,
7720 with several other stop positions in between that we
7721 missed. Scan them all now, in buffer's logical
7722 order, until we find and handle the last stop_charpos
7723 that precedes our current position. */
7724 handle_stop_backwards (it, it->stop_charpos);
7725 return GET_NEXT_DISPLAY_ELEMENT (it);
7726 }
7727 else
7728 {
7729 if (it->bidi_p)
7730 {
7731 /* Take note of the stop position we just moved
7732 across, for when we will move back across it. */
7733 it->prev_stop = it->stop_charpos;
7734 /* If we are at base paragraph embedding level, take
7735 note of the last stop position seen at this
7736 level. */
7737 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7738 it->base_level_stop = it->stop_charpos;
7739 }
7740 handle_stop (it);
7741
7742 /* Since a handler may have changed IT->method, we must
7743 recurse here. */
7744 return GET_NEXT_DISPLAY_ELEMENT (it);
7745 }
7746 }
7747 else if (it->bidi_p
7748 /* If we are before prev_stop, we may have overstepped
7749 on our way backwards a stop_pos, and if so, we need
7750 to handle that stop_pos. */
7751 && IT_STRING_CHARPOS (*it) < it->prev_stop
7752 /* We can sometimes back up for reasons that have nothing
7753 to do with bidi reordering. E.g., compositions. The
7754 code below is only needed when we are above the base
7755 embedding level, so test for that explicitly. */
7756 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7757 {
7758 /* If we lost track of base_level_stop, we have no better
7759 place for handle_stop_backwards to start from than string
7760 beginning. This happens, e.g., when we were reseated to
7761 the previous screenful of text by vertical-motion. */
7762 if (it->base_level_stop <= 0
7763 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7764 it->base_level_stop = 0;
7765 handle_stop_backwards (it, it->base_level_stop);
7766 return GET_NEXT_DISPLAY_ELEMENT (it);
7767 }
7768 }
7769
7770 if (it->current.overlay_string_index >= 0)
7771 {
7772 /* Get the next character from an overlay string. In overlay
7773 strings, there is no field width or padding with spaces to
7774 do. */
7775 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7776 {
7777 it->what = IT_EOB;
7778 return 0;
7779 }
7780 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7781 IT_STRING_BYTEPOS (*it),
7782 it->bidi_it.scan_dir < 0
7783 ? -1
7784 : SCHARS (it->string))
7785 && next_element_from_composition (it))
7786 {
7787 return 1;
7788 }
7789 else if (STRING_MULTIBYTE (it->string))
7790 {
7791 const unsigned char *s = (SDATA (it->string)
7792 + IT_STRING_BYTEPOS (*it));
7793 it->c = string_char_and_length (s, &it->len);
7794 }
7795 else
7796 {
7797 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7798 it->len = 1;
7799 }
7800 }
7801 else
7802 {
7803 /* Get the next character from a Lisp string that is not an
7804 overlay string. Such strings come from the mode line, for
7805 example. We may have to pad with spaces, or truncate the
7806 string. See also next_element_from_c_string. */
7807 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7808 {
7809 it->what = IT_EOB;
7810 return 0;
7811 }
7812 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7813 {
7814 /* Pad with spaces. */
7815 it->c = ' ', it->len = 1;
7816 CHARPOS (position) = BYTEPOS (position) = -1;
7817 }
7818 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7819 IT_STRING_BYTEPOS (*it),
7820 it->bidi_it.scan_dir < 0
7821 ? -1
7822 : it->string_nchars)
7823 && next_element_from_composition (it))
7824 {
7825 return 1;
7826 }
7827 else if (STRING_MULTIBYTE (it->string))
7828 {
7829 const unsigned char *s = (SDATA (it->string)
7830 + IT_STRING_BYTEPOS (*it));
7831 it->c = string_char_and_length (s, &it->len);
7832 }
7833 else
7834 {
7835 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7836 it->len = 1;
7837 }
7838 }
7839
7840 /* Record what we have and where it came from. */
7841 it->what = IT_CHARACTER;
7842 it->object = it->string;
7843 it->position = position;
7844 return 1;
7845 }
7846
7847
7848 /* Load IT with next display element from C string IT->s.
7849 IT->string_nchars is the maximum number of characters to return
7850 from the string. IT->end_charpos may be greater than
7851 IT->string_nchars when this function is called, in which case we
7852 may have to return padding spaces. Value is zero if end of string
7853 reached, including padding spaces. */
7854
7855 static int
7856 next_element_from_c_string (struct it *it)
7857 {
7858 int success_p = 1;
7859
7860 eassert (it->s);
7861 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7862 it->what = IT_CHARACTER;
7863 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7864 it->object = Qnil;
7865
7866 /* With bidi reordering, the character to display might not be the
7867 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7868 we were reseated to a new string, whose paragraph direction is
7869 not known. */
7870 if (it->bidi_p && it->bidi_it.first_elt)
7871 get_visually_first_element (it);
7872
7873 /* IT's position can be greater than IT->string_nchars in case a
7874 field width or precision has been specified when the iterator was
7875 initialized. */
7876 if (IT_CHARPOS (*it) >= it->end_charpos)
7877 {
7878 /* End of the game. */
7879 it->what = IT_EOB;
7880 success_p = 0;
7881 }
7882 else if (IT_CHARPOS (*it) >= it->string_nchars)
7883 {
7884 /* Pad with spaces. */
7885 it->c = ' ', it->len = 1;
7886 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7887 }
7888 else if (it->multibyte_p)
7889 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7890 else
7891 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7892
7893 return success_p;
7894 }
7895
7896
7897 /* Set up IT to return characters from an ellipsis, if appropriate.
7898 The definition of the ellipsis glyphs may come from a display table
7899 entry. This function fills IT with the first glyph from the
7900 ellipsis if an ellipsis is to be displayed. */
7901
7902 static int
7903 next_element_from_ellipsis (struct it *it)
7904 {
7905 if (it->selective_display_ellipsis_p)
7906 setup_for_ellipsis (it, it->len);
7907 else
7908 {
7909 /* The face at the current position may be different from the
7910 face we find after the invisible text. Remember what it
7911 was in IT->saved_face_id, and signal that it's there by
7912 setting face_before_selective_p. */
7913 it->saved_face_id = it->face_id;
7914 it->method = GET_FROM_BUFFER;
7915 it->object = it->w->contents;
7916 reseat_at_next_visible_line_start (it, 1);
7917 it->face_before_selective_p = 1;
7918 }
7919
7920 return GET_NEXT_DISPLAY_ELEMENT (it);
7921 }
7922
7923
7924 /* Deliver an image display element. The iterator IT is already
7925 filled with image information (done in handle_display_prop). Value
7926 is always 1. */
7927
7928
7929 static int
7930 next_element_from_image (struct it *it)
7931 {
7932 it->what = IT_IMAGE;
7933 it->ignore_overlay_strings_at_pos_p = 0;
7934 return 1;
7935 }
7936
7937 #ifdef HAVE_XWIDGETS
7938 /* im not sure about this FIXME JAVE*/
7939 static int
7940 next_element_from_xwidget (struct it *it)
7941 {
7942 it->what = IT_XWIDGET;
7943 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
7944 //this is shaky because why do we set "what" if we dont set the other parts??
7945 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
7946 return 1;
7947 }
7948 #endif
7949
7950
7951 /* Fill iterator IT with next display element from a stretch glyph
7952 property. IT->object is the value of the text property. Value is
7953 always 1. */
7954
7955 static int
7956 next_element_from_stretch (struct it *it)
7957 {
7958 it->what = IT_STRETCH;
7959 return 1;
7960 }
7961
7962 /* Scan backwards from IT's current position until we find a stop
7963 position, or until BEGV. This is called when we find ourself
7964 before both the last known prev_stop and base_level_stop while
7965 reordering bidirectional text. */
7966
7967 static void
7968 compute_stop_pos_backwards (struct it *it)
7969 {
7970 const int SCAN_BACK_LIMIT = 1000;
7971 struct text_pos pos;
7972 struct display_pos save_current = it->current;
7973 struct text_pos save_position = it->position;
7974 ptrdiff_t charpos = IT_CHARPOS (*it);
7975 ptrdiff_t where_we_are = charpos;
7976 ptrdiff_t save_stop_pos = it->stop_charpos;
7977 ptrdiff_t save_end_pos = it->end_charpos;
7978
7979 eassert (NILP (it->string) && !it->s);
7980 eassert (it->bidi_p);
7981 it->bidi_p = 0;
7982 do
7983 {
7984 it->end_charpos = min (charpos + 1, ZV);
7985 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7986 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7987 reseat_1 (it, pos, 0);
7988 compute_stop_pos (it);
7989 /* We must advance forward, right? */
7990 if (it->stop_charpos <= charpos)
7991 emacs_abort ();
7992 }
7993 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7994
7995 if (it->stop_charpos <= where_we_are)
7996 it->prev_stop = it->stop_charpos;
7997 else
7998 it->prev_stop = BEGV;
7999 it->bidi_p = 1;
8000 it->current = save_current;
8001 it->position = save_position;
8002 it->stop_charpos = save_stop_pos;
8003 it->end_charpos = save_end_pos;
8004 }
8005
8006 /* Scan forward from CHARPOS in the current buffer/string, until we
8007 find a stop position > current IT's position. Then handle the stop
8008 position before that. This is called when we bump into a stop
8009 position while reordering bidirectional text. CHARPOS should be
8010 the last previously processed stop_pos (or BEGV/0, if none were
8011 processed yet) whose position is less that IT's current
8012 position. */
8013
8014 static void
8015 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8016 {
8017 int bufp = !STRINGP (it->string);
8018 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8019 struct display_pos save_current = it->current;
8020 struct text_pos save_position = it->position;
8021 struct text_pos pos1;
8022 ptrdiff_t next_stop;
8023
8024 /* Scan in strict logical order. */
8025 eassert (it->bidi_p);
8026 it->bidi_p = 0;
8027 do
8028 {
8029 it->prev_stop = charpos;
8030 if (bufp)
8031 {
8032 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8033 reseat_1 (it, pos1, 0);
8034 }
8035 else
8036 it->current.string_pos = string_pos (charpos, it->string);
8037 compute_stop_pos (it);
8038 /* We must advance forward, right? */
8039 if (it->stop_charpos <= it->prev_stop)
8040 emacs_abort ();
8041 charpos = it->stop_charpos;
8042 }
8043 while (charpos <= where_we_are);
8044
8045 it->bidi_p = 1;
8046 it->current = save_current;
8047 it->position = save_position;
8048 next_stop = it->stop_charpos;
8049 it->stop_charpos = it->prev_stop;
8050 handle_stop (it);
8051 it->stop_charpos = next_stop;
8052 }
8053
8054 /* Load IT with the next display element from current_buffer. Value
8055 is zero if end of buffer reached. IT->stop_charpos is the next
8056 position at which to stop and check for text properties or buffer
8057 end. */
8058
8059 static int
8060 next_element_from_buffer (struct it *it)
8061 {
8062 int success_p = 1;
8063
8064 eassert (IT_CHARPOS (*it) >= BEGV);
8065 eassert (NILP (it->string) && !it->s);
8066 eassert (!it->bidi_p
8067 || (EQ (it->bidi_it.string.lstring, Qnil)
8068 && it->bidi_it.string.s == NULL));
8069
8070 /* With bidi reordering, the character to display might not be the
8071 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8072 we were reseat()ed to a new buffer position, which is potentially
8073 a different paragraph. */
8074 if (it->bidi_p && it->bidi_it.first_elt)
8075 {
8076 get_visually_first_element (it);
8077 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8078 }
8079
8080 if (IT_CHARPOS (*it) >= it->stop_charpos)
8081 {
8082 if (IT_CHARPOS (*it) >= it->end_charpos)
8083 {
8084 int overlay_strings_follow_p;
8085
8086 /* End of the game, except when overlay strings follow that
8087 haven't been returned yet. */
8088 if (it->overlay_strings_at_end_processed_p)
8089 overlay_strings_follow_p = 0;
8090 else
8091 {
8092 it->overlay_strings_at_end_processed_p = 1;
8093 overlay_strings_follow_p = get_overlay_strings (it, 0);
8094 }
8095
8096 if (overlay_strings_follow_p)
8097 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8098 else
8099 {
8100 it->what = IT_EOB;
8101 it->position = it->current.pos;
8102 success_p = 0;
8103 }
8104 }
8105 else if (!(!it->bidi_p
8106 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8107 || IT_CHARPOS (*it) == it->stop_charpos))
8108 {
8109 /* With bidi non-linear iteration, we could find ourselves
8110 far beyond the last computed stop_charpos, with several
8111 other stop positions in between that we missed. Scan
8112 them all now, in buffer's logical order, until we find
8113 and handle the last stop_charpos that precedes our
8114 current position. */
8115 handle_stop_backwards (it, it->stop_charpos);
8116 return GET_NEXT_DISPLAY_ELEMENT (it);
8117 }
8118 else
8119 {
8120 if (it->bidi_p)
8121 {
8122 /* Take note of the stop position we just moved across,
8123 for when we will move back across it. */
8124 it->prev_stop = it->stop_charpos;
8125 /* If we are at base paragraph embedding level, take
8126 note of the last stop position seen at this
8127 level. */
8128 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8129 it->base_level_stop = it->stop_charpos;
8130 }
8131 handle_stop (it);
8132 return GET_NEXT_DISPLAY_ELEMENT (it);
8133 }
8134 }
8135 else if (it->bidi_p
8136 /* If we are before prev_stop, we may have overstepped on
8137 our way backwards a stop_pos, and if so, we need to
8138 handle that stop_pos. */
8139 && IT_CHARPOS (*it) < it->prev_stop
8140 /* We can sometimes back up for reasons that have nothing
8141 to do with bidi reordering. E.g., compositions. The
8142 code below is only needed when we are above the base
8143 embedding level, so test for that explicitly. */
8144 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8145 {
8146 if (it->base_level_stop <= 0
8147 || IT_CHARPOS (*it) < it->base_level_stop)
8148 {
8149 /* If we lost track of base_level_stop, we need to find
8150 prev_stop by looking backwards. This happens, e.g., when
8151 we were reseated to the previous screenful of text by
8152 vertical-motion. */
8153 it->base_level_stop = BEGV;
8154 compute_stop_pos_backwards (it);
8155 handle_stop_backwards (it, it->prev_stop);
8156 }
8157 else
8158 handle_stop_backwards (it, it->base_level_stop);
8159 return GET_NEXT_DISPLAY_ELEMENT (it);
8160 }
8161 else
8162 {
8163 /* No face changes, overlays etc. in sight, so just return a
8164 character from current_buffer. */
8165 unsigned char *p;
8166 ptrdiff_t stop;
8167
8168 /* Maybe run the redisplay end trigger hook. Performance note:
8169 This doesn't seem to cost measurable time. */
8170 if (it->redisplay_end_trigger_charpos
8171 && it->glyph_row
8172 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8173 run_redisplay_end_trigger_hook (it);
8174
8175 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8176 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8177 stop)
8178 && next_element_from_composition (it))
8179 {
8180 return 1;
8181 }
8182
8183 /* Get the next character, maybe multibyte. */
8184 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8185 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8186 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8187 else
8188 it->c = *p, it->len = 1;
8189
8190 /* Record what we have and where it came from. */
8191 it->what = IT_CHARACTER;
8192 it->object = it->w->contents;
8193 it->position = it->current.pos;
8194
8195 /* Normally we return the character found above, except when we
8196 really want to return an ellipsis for selective display. */
8197 if (it->selective)
8198 {
8199 if (it->c == '\n')
8200 {
8201 /* A value of selective > 0 means hide lines indented more
8202 than that number of columns. */
8203 if (it->selective > 0
8204 && IT_CHARPOS (*it) + 1 < ZV
8205 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8206 IT_BYTEPOS (*it) + 1,
8207 it->selective))
8208 {
8209 success_p = next_element_from_ellipsis (it);
8210 it->dpvec_char_len = -1;
8211 }
8212 }
8213 else if (it->c == '\r' && it->selective == -1)
8214 {
8215 /* A value of selective == -1 means that everything from the
8216 CR to the end of the line is invisible, with maybe an
8217 ellipsis displayed for it. */
8218 success_p = next_element_from_ellipsis (it);
8219 it->dpvec_char_len = -1;
8220 }
8221 }
8222 }
8223
8224 /* Value is zero if end of buffer reached. */
8225 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8226 return success_p;
8227 }
8228
8229
8230 /* Run the redisplay end trigger hook for IT. */
8231
8232 static void
8233 run_redisplay_end_trigger_hook (struct it *it)
8234 {
8235 Lisp_Object args[3];
8236
8237 /* IT->glyph_row should be non-null, i.e. we should be actually
8238 displaying something, or otherwise we should not run the hook. */
8239 eassert (it->glyph_row);
8240
8241 /* Set up hook arguments. */
8242 args[0] = Qredisplay_end_trigger_functions;
8243 args[1] = it->window;
8244 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8245 it->redisplay_end_trigger_charpos = 0;
8246
8247 /* Since we are *trying* to run these functions, don't try to run
8248 them again, even if they get an error. */
8249 wset_redisplay_end_trigger (it->w, Qnil);
8250 Frun_hook_with_args (3, args);
8251
8252 /* Notice if it changed the face of the character we are on. */
8253 handle_face_prop (it);
8254 }
8255
8256
8257 /* Deliver a composition display element. Unlike the other
8258 next_element_from_XXX, this function is not registered in the array
8259 get_next_element[]. It is called from next_element_from_buffer and
8260 next_element_from_string when necessary. */
8261
8262 static int
8263 next_element_from_composition (struct it *it)
8264 {
8265 it->what = IT_COMPOSITION;
8266 it->len = it->cmp_it.nbytes;
8267 if (STRINGP (it->string))
8268 {
8269 if (it->c < 0)
8270 {
8271 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8272 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8273 return 0;
8274 }
8275 it->position = it->current.string_pos;
8276 it->object = it->string;
8277 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8278 IT_STRING_BYTEPOS (*it), it->string);
8279 }
8280 else
8281 {
8282 if (it->c < 0)
8283 {
8284 IT_CHARPOS (*it) += it->cmp_it.nchars;
8285 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8286 if (it->bidi_p)
8287 {
8288 if (it->bidi_it.new_paragraph)
8289 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8290 /* Resync the bidi iterator with IT's new position.
8291 FIXME: this doesn't support bidirectional text. */
8292 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8293 bidi_move_to_visually_next (&it->bidi_it);
8294 }
8295 return 0;
8296 }
8297 it->position = it->current.pos;
8298 it->object = it->w->contents;
8299 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8300 IT_BYTEPOS (*it), Qnil);
8301 }
8302 return 1;
8303 }
8304
8305
8306 \f
8307 /***********************************************************************
8308 Moving an iterator without producing glyphs
8309 ***********************************************************************/
8310
8311 /* Check if iterator is at a position corresponding to a valid buffer
8312 position after some move_it_ call. */
8313
8314 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8315 ((it)->method == GET_FROM_STRING \
8316 ? IT_STRING_CHARPOS (*it) == 0 \
8317 : 1)
8318
8319
8320 /* Move iterator IT to a specified buffer or X position within one
8321 line on the display without producing glyphs.
8322
8323 OP should be a bit mask including some or all of these bits:
8324 MOVE_TO_X: Stop upon reaching x-position TO_X.
8325 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8326 Regardless of OP's value, stop upon reaching the end of the display line.
8327
8328 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8329 This means, in particular, that TO_X includes window's horizontal
8330 scroll amount.
8331
8332 The return value has several possible values that
8333 say what condition caused the scan to stop:
8334
8335 MOVE_POS_MATCH_OR_ZV
8336 - when TO_POS or ZV was reached.
8337
8338 MOVE_X_REACHED
8339 -when TO_X was reached before TO_POS or ZV were reached.
8340
8341 MOVE_LINE_CONTINUED
8342 - when we reached the end of the display area and the line must
8343 be continued.
8344
8345 MOVE_LINE_TRUNCATED
8346 - when we reached the end of the display area and the line is
8347 truncated.
8348
8349 MOVE_NEWLINE_OR_CR
8350 - when we stopped at a line end, i.e. a newline or a CR and selective
8351 display is on. */
8352
8353 static enum move_it_result
8354 move_it_in_display_line_to (struct it *it,
8355 ptrdiff_t to_charpos, int to_x,
8356 enum move_operation_enum op)
8357 {
8358 enum move_it_result result = MOVE_UNDEFINED;
8359 struct glyph_row *saved_glyph_row;
8360 struct it wrap_it, atpos_it, atx_it, ppos_it;
8361 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8362 void *ppos_data = NULL;
8363 int may_wrap = 0;
8364 enum it_method prev_method = it->method;
8365 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8366 int saw_smaller_pos = prev_pos < to_charpos;
8367
8368 /* Don't produce glyphs in produce_glyphs. */
8369 saved_glyph_row = it->glyph_row;
8370 it->glyph_row = NULL;
8371
8372 /* Use wrap_it to save a copy of IT wherever a word wrap could
8373 occur. Use atpos_it to save a copy of IT at the desired buffer
8374 position, if found, so that we can scan ahead and check if the
8375 word later overshoots the window edge. Use atx_it similarly, for
8376 pixel positions. */
8377 wrap_it.sp = -1;
8378 atpos_it.sp = -1;
8379 atx_it.sp = -1;
8380
8381 /* Use ppos_it under bidi reordering to save a copy of IT for the
8382 position > CHARPOS that is the closest to CHARPOS. We restore
8383 that position in IT when we have scanned the entire display line
8384 without finding a match for CHARPOS and all the character
8385 positions are greater than CHARPOS. */
8386 if (it->bidi_p)
8387 {
8388 SAVE_IT (ppos_it, *it, ppos_data);
8389 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8390 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8391 SAVE_IT (ppos_it, *it, ppos_data);
8392 }
8393
8394 #define BUFFER_POS_REACHED_P() \
8395 ((op & MOVE_TO_POS) != 0 \
8396 && BUFFERP (it->object) \
8397 && (IT_CHARPOS (*it) == to_charpos \
8398 || ((!it->bidi_p \
8399 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8400 && IT_CHARPOS (*it) > to_charpos) \
8401 || (it->what == IT_COMPOSITION \
8402 && ((IT_CHARPOS (*it) > to_charpos \
8403 && to_charpos >= it->cmp_it.charpos) \
8404 || (IT_CHARPOS (*it) < to_charpos \
8405 && to_charpos <= it->cmp_it.charpos)))) \
8406 && (it->method == GET_FROM_BUFFER \
8407 || (it->method == GET_FROM_DISPLAY_VECTOR \
8408 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8409
8410 /* If there's a line-/wrap-prefix, handle it. */
8411 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8412 && it->current_y < it->last_visible_y)
8413 handle_line_prefix (it);
8414
8415 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8416 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8417
8418 while (1)
8419 {
8420 int x, i, ascent = 0, descent = 0;
8421
8422 /* Utility macro to reset an iterator with x, ascent, and descent. */
8423 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8424 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8425 (IT)->max_descent = descent)
8426
8427 /* Stop if we move beyond TO_CHARPOS (after an image or a
8428 display string or stretch glyph). */
8429 if ((op & MOVE_TO_POS) != 0
8430 && BUFFERP (it->object)
8431 && it->method == GET_FROM_BUFFER
8432 && (((!it->bidi_p
8433 /* When the iterator is at base embedding level, we
8434 are guaranteed that characters are delivered for
8435 display in strictly increasing order of their
8436 buffer positions. */
8437 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8438 && IT_CHARPOS (*it) > to_charpos)
8439 || (it->bidi_p
8440 && (prev_method == GET_FROM_IMAGE
8441 || prev_method == GET_FROM_STRETCH
8442 || prev_method == GET_FROM_STRING)
8443 /* Passed TO_CHARPOS from left to right. */
8444 && ((prev_pos < to_charpos
8445 && IT_CHARPOS (*it) > to_charpos)
8446 /* Passed TO_CHARPOS from right to left. */
8447 || (prev_pos > to_charpos
8448 && IT_CHARPOS (*it) < to_charpos)))))
8449 {
8450 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8451 {
8452 result = MOVE_POS_MATCH_OR_ZV;
8453 break;
8454 }
8455 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8456 /* If wrap_it is valid, the current position might be in a
8457 word that is wrapped. So, save the iterator in
8458 atpos_it and continue to see if wrapping happens. */
8459 SAVE_IT (atpos_it, *it, atpos_data);
8460 }
8461
8462 /* Stop when ZV reached.
8463 We used to stop here when TO_CHARPOS reached as well, but that is
8464 too soon if this glyph does not fit on this line. So we handle it
8465 explicitly below. */
8466 if (!get_next_display_element (it))
8467 {
8468 result = MOVE_POS_MATCH_OR_ZV;
8469 break;
8470 }
8471
8472 if (it->line_wrap == TRUNCATE)
8473 {
8474 if (BUFFER_POS_REACHED_P ())
8475 {
8476 result = MOVE_POS_MATCH_OR_ZV;
8477 break;
8478 }
8479 }
8480 else
8481 {
8482 if (it->line_wrap == WORD_WRAP)
8483 {
8484 if (IT_DISPLAYING_WHITESPACE (it))
8485 may_wrap = 1;
8486 else if (may_wrap)
8487 {
8488 /* We have reached a glyph that follows one or more
8489 whitespace characters. If the position is
8490 already found, we are done. */
8491 if (atpos_it.sp >= 0)
8492 {
8493 RESTORE_IT (it, &atpos_it, atpos_data);
8494 result = MOVE_POS_MATCH_OR_ZV;
8495 goto done;
8496 }
8497 if (atx_it.sp >= 0)
8498 {
8499 RESTORE_IT (it, &atx_it, atx_data);
8500 result = MOVE_X_REACHED;
8501 goto done;
8502 }
8503 /* Otherwise, we can wrap here. */
8504 SAVE_IT (wrap_it, *it, wrap_data);
8505 may_wrap = 0;
8506 }
8507 }
8508 }
8509
8510 /* Remember the line height for the current line, in case
8511 the next element doesn't fit on the line. */
8512 ascent = it->max_ascent;
8513 descent = it->max_descent;
8514
8515 /* The call to produce_glyphs will get the metrics of the
8516 display element IT is loaded with. Record the x-position
8517 before this display element, in case it doesn't fit on the
8518 line. */
8519 x = it->current_x;
8520
8521 PRODUCE_GLYPHS (it);
8522
8523 if (it->area != TEXT_AREA)
8524 {
8525 prev_method = it->method;
8526 if (it->method == GET_FROM_BUFFER)
8527 prev_pos = IT_CHARPOS (*it);
8528 set_iterator_to_next (it, 1);
8529 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8530 SET_TEXT_POS (this_line_min_pos,
8531 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8532 if (it->bidi_p
8533 && (op & MOVE_TO_POS)
8534 && IT_CHARPOS (*it) > to_charpos
8535 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8536 SAVE_IT (ppos_it, *it, ppos_data);
8537 continue;
8538 }
8539
8540 /* The number of glyphs we get back in IT->nglyphs will normally
8541 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8542 character on a terminal frame, or (iii) a line end. For the
8543 second case, IT->nglyphs - 1 padding glyphs will be present.
8544 (On X frames, there is only one glyph produced for a
8545 composite character.)
8546
8547 The behavior implemented below means, for continuation lines,
8548 that as many spaces of a TAB as fit on the current line are
8549 displayed there. For terminal frames, as many glyphs of a
8550 multi-glyph character are displayed in the current line, too.
8551 This is what the old redisplay code did, and we keep it that
8552 way. Under X, the whole shape of a complex character must
8553 fit on the line or it will be completely displayed in the
8554 next line.
8555
8556 Note that both for tabs and padding glyphs, all glyphs have
8557 the same width. */
8558 if (it->nglyphs)
8559 {
8560 /* More than one glyph or glyph doesn't fit on line. All
8561 glyphs have the same width. */
8562 int single_glyph_width = it->pixel_width / it->nglyphs;
8563 int new_x;
8564 int x_before_this_char = x;
8565 int hpos_before_this_char = it->hpos;
8566
8567 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8568 {
8569 new_x = x + single_glyph_width;
8570
8571 /* We want to leave anything reaching TO_X to the caller. */
8572 if ((op & MOVE_TO_X) && new_x > to_x)
8573 {
8574 if (BUFFER_POS_REACHED_P ())
8575 {
8576 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8577 goto buffer_pos_reached;
8578 if (atpos_it.sp < 0)
8579 {
8580 SAVE_IT (atpos_it, *it, atpos_data);
8581 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8582 }
8583 }
8584 else
8585 {
8586 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8587 {
8588 it->current_x = x;
8589 result = MOVE_X_REACHED;
8590 break;
8591 }
8592 if (atx_it.sp < 0)
8593 {
8594 SAVE_IT (atx_it, *it, atx_data);
8595 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8596 }
8597 }
8598 }
8599
8600 if (/* Lines are continued. */
8601 it->line_wrap != TRUNCATE
8602 && (/* And glyph doesn't fit on the line. */
8603 new_x > it->last_visible_x
8604 /* Or it fits exactly and we're on a window
8605 system frame. */
8606 || (new_x == it->last_visible_x
8607 && FRAME_WINDOW_P (it->f)
8608 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8609 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8610 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8611 {
8612 if (/* IT->hpos == 0 means the very first glyph
8613 doesn't fit on the line, e.g. a wide image. */
8614 it->hpos == 0
8615 || (new_x == it->last_visible_x
8616 && FRAME_WINDOW_P (it->f)))
8617 {
8618 ++it->hpos;
8619 it->current_x = new_x;
8620
8621 /* The character's last glyph just barely fits
8622 in this row. */
8623 if (i == it->nglyphs - 1)
8624 {
8625 /* If this is the destination position,
8626 return a position *before* it in this row,
8627 now that we know it fits in this row. */
8628 if (BUFFER_POS_REACHED_P ())
8629 {
8630 if (it->line_wrap != WORD_WRAP
8631 || wrap_it.sp < 0)
8632 {
8633 it->hpos = hpos_before_this_char;
8634 it->current_x = x_before_this_char;
8635 result = MOVE_POS_MATCH_OR_ZV;
8636 break;
8637 }
8638 if (it->line_wrap == WORD_WRAP
8639 && atpos_it.sp < 0)
8640 {
8641 SAVE_IT (atpos_it, *it, atpos_data);
8642 atpos_it.current_x = x_before_this_char;
8643 atpos_it.hpos = hpos_before_this_char;
8644 }
8645 }
8646
8647 prev_method = it->method;
8648 if (it->method == GET_FROM_BUFFER)
8649 prev_pos = IT_CHARPOS (*it);
8650 set_iterator_to_next (it, 1);
8651 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8652 SET_TEXT_POS (this_line_min_pos,
8653 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8654 /* On graphical terminals, newlines may
8655 "overflow" into the fringe if
8656 overflow-newline-into-fringe is non-nil.
8657 On text terminals, and on graphical
8658 terminals with no right margin, newlines
8659 may overflow into the last glyph on the
8660 display line.*/
8661 if (!FRAME_WINDOW_P (it->f)
8662 || ((it->bidi_p
8663 && it->bidi_it.paragraph_dir == R2L)
8664 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8665 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8666 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8667 {
8668 if (!get_next_display_element (it))
8669 {
8670 result = MOVE_POS_MATCH_OR_ZV;
8671 break;
8672 }
8673 if (BUFFER_POS_REACHED_P ())
8674 {
8675 if (ITERATOR_AT_END_OF_LINE_P (it))
8676 result = MOVE_POS_MATCH_OR_ZV;
8677 else
8678 result = MOVE_LINE_CONTINUED;
8679 break;
8680 }
8681 if (ITERATOR_AT_END_OF_LINE_P (it)
8682 && (it->line_wrap != WORD_WRAP
8683 || wrap_it.sp < 0))
8684 {
8685 result = MOVE_NEWLINE_OR_CR;
8686 break;
8687 }
8688 }
8689 }
8690 }
8691 else
8692 IT_RESET_X_ASCENT_DESCENT (it);
8693
8694 if (wrap_it.sp >= 0)
8695 {
8696 RESTORE_IT (it, &wrap_it, wrap_data);
8697 atpos_it.sp = -1;
8698 atx_it.sp = -1;
8699 }
8700
8701 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8702 IT_CHARPOS (*it)));
8703 result = MOVE_LINE_CONTINUED;
8704 break;
8705 }
8706
8707 if (BUFFER_POS_REACHED_P ())
8708 {
8709 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8710 goto buffer_pos_reached;
8711 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8712 {
8713 SAVE_IT (atpos_it, *it, atpos_data);
8714 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8715 }
8716 }
8717
8718 if (new_x > it->first_visible_x)
8719 {
8720 /* Glyph is visible. Increment number of glyphs that
8721 would be displayed. */
8722 ++it->hpos;
8723 }
8724 }
8725
8726 if (result != MOVE_UNDEFINED)
8727 break;
8728 }
8729 else if (BUFFER_POS_REACHED_P ())
8730 {
8731 buffer_pos_reached:
8732 IT_RESET_X_ASCENT_DESCENT (it);
8733 result = MOVE_POS_MATCH_OR_ZV;
8734 break;
8735 }
8736 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8737 {
8738 /* Stop when TO_X specified and reached. This check is
8739 necessary here because of lines consisting of a line end,
8740 only. The line end will not produce any glyphs and we
8741 would never get MOVE_X_REACHED. */
8742 eassert (it->nglyphs == 0);
8743 result = MOVE_X_REACHED;
8744 break;
8745 }
8746
8747 /* Is this a line end? If yes, we're done. */
8748 if (ITERATOR_AT_END_OF_LINE_P (it))
8749 {
8750 /* If we are past TO_CHARPOS, but never saw any character
8751 positions smaller than TO_CHARPOS, return
8752 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8753 did. */
8754 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8755 {
8756 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8757 {
8758 if (IT_CHARPOS (ppos_it) < ZV)
8759 {
8760 RESTORE_IT (it, &ppos_it, ppos_data);
8761 result = MOVE_POS_MATCH_OR_ZV;
8762 }
8763 else
8764 goto buffer_pos_reached;
8765 }
8766 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8767 && IT_CHARPOS (*it) > to_charpos)
8768 goto buffer_pos_reached;
8769 else
8770 result = MOVE_NEWLINE_OR_CR;
8771 }
8772 else
8773 result = MOVE_NEWLINE_OR_CR;
8774 break;
8775 }
8776
8777 prev_method = it->method;
8778 if (it->method == GET_FROM_BUFFER)
8779 prev_pos = IT_CHARPOS (*it);
8780 /* The current display element has been consumed. Advance
8781 to the next. */
8782 set_iterator_to_next (it, 1);
8783 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8784 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8785 if (IT_CHARPOS (*it) < to_charpos)
8786 saw_smaller_pos = 1;
8787 if (it->bidi_p
8788 && (op & MOVE_TO_POS)
8789 && IT_CHARPOS (*it) >= to_charpos
8790 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8791 SAVE_IT (ppos_it, *it, ppos_data);
8792
8793 /* Stop if lines are truncated and IT's current x-position is
8794 past the right edge of the window now. */
8795 if (it->line_wrap == TRUNCATE
8796 && it->current_x >= it->last_visible_x)
8797 {
8798 if (!FRAME_WINDOW_P (it->f)
8799 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8800 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8801 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8802 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8803 {
8804 int at_eob_p = 0;
8805
8806 if ((at_eob_p = !get_next_display_element (it))
8807 || BUFFER_POS_REACHED_P ()
8808 /* If we are past TO_CHARPOS, but never saw any
8809 character positions smaller than TO_CHARPOS,
8810 return MOVE_POS_MATCH_OR_ZV, like the
8811 unidirectional display did. */
8812 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8813 && !saw_smaller_pos
8814 && IT_CHARPOS (*it) > to_charpos))
8815 {
8816 if (it->bidi_p
8817 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8818 RESTORE_IT (it, &ppos_it, ppos_data);
8819 result = MOVE_POS_MATCH_OR_ZV;
8820 break;
8821 }
8822 if (ITERATOR_AT_END_OF_LINE_P (it))
8823 {
8824 result = MOVE_NEWLINE_OR_CR;
8825 break;
8826 }
8827 }
8828 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8829 && !saw_smaller_pos
8830 && IT_CHARPOS (*it) > to_charpos)
8831 {
8832 if (IT_CHARPOS (ppos_it) < ZV)
8833 RESTORE_IT (it, &ppos_it, ppos_data);
8834 result = MOVE_POS_MATCH_OR_ZV;
8835 break;
8836 }
8837 result = MOVE_LINE_TRUNCATED;
8838 break;
8839 }
8840 #undef IT_RESET_X_ASCENT_DESCENT
8841 }
8842
8843 #undef BUFFER_POS_REACHED_P
8844
8845 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8846 restore the saved iterator. */
8847 if (atpos_it.sp >= 0)
8848 RESTORE_IT (it, &atpos_it, atpos_data);
8849 else if (atx_it.sp >= 0)
8850 RESTORE_IT (it, &atx_it, atx_data);
8851
8852 done:
8853
8854 if (atpos_data)
8855 bidi_unshelve_cache (atpos_data, 1);
8856 if (atx_data)
8857 bidi_unshelve_cache (atx_data, 1);
8858 if (wrap_data)
8859 bidi_unshelve_cache (wrap_data, 1);
8860 if (ppos_data)
8861 bidi_unshelve_cache (ppos_data, 1);
8862
8863 /* Restore the iterator settings altered at the beginning of this
8864 function. */
8865 it->glyph_row = saved_glyph_row;
8866 return result;
8867 }
8868
8869 /* For external use. */
8870 void
8871 move_it_in_display_line (struct it *it,
8872 ptrdiff_t to_charpos, int to_x,
8873 enum move_operation_enum op)
8874 {
8875 if (it->line_wrap == WORD_WRAP
8876 && (op & MOVE_TO_X))
8877 {
8878 struct it save_it;
8879 void *save_data = NULL;
8880 int skip;
8881
8882 SAVE_IT (save_it, *it, save_data);
8883 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8884 /* When word-wrap is on, TO_X may lie past the end
8885 of a wrapped line. Then it->current is the
8886 character on the next line, so backtrack to the
8887 space before the wrap point. */
8888 if (skip == MOVE_LINE_CONTINUED)
8889 {
8890 int prev_x = max (it->current_x - 1, 0);
8891 RESTORE_IT (it, &save_it, save_data);
8892 move_it_in_display_line_to
8893 (it, -1, prev_x, MOVE_TO_X);
8894 }
8895 else
8896 bidi_unshelve_cache (save_data, 1);
8897 }
8898 else
8899 move_it_in_display_line_to (it, to_charpos, to_x, op);
8900 }
8901
8902
8903 /* Move IT forward until it satisfies one or more of the criteria in
8904 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8905
8906 OP is a bit-mask that specifies where to stop, and in particular,
8907 which of those four position arguments makes a difference. See the
8908 description of enum move_operation_enum.
8909
8910 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8911 screen line, this function will set IT to the next position that is
8912 displayed to the right of TO_CHARPOS on the screen. */
8913
8914 void
8915 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8916 {
8917 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8918 int line_height, line_start_x = 0, reached = 0;
8919 void *backup_data = NULL;
8920
8921 for (;;)
8922 {
8923 if (op & MOVE_TO_VPOS)
8924 {
8925 /* If no TO_CHARPOS and no TO_X specified, stop at the
8926 start of the line TO_VPOS. */
8927 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8928 {
8929 if (it->vpos == to_vpos)
8930 {
8931 reached = 1;
8932 break;
8933 }
8934 else
8935 skip = move_it_in_display_line_to (it, -1, -1, 0);
8936 }
8937 else
8938 {
8939 /* TO_VPOS >= 0 means stop at TO_X in the line at
8940 TO_VPOS, or at TO_POS, whichever comes first. */
8941 if (it->vpos == to_vpos)
8942 {
8943 reached = 2;
8944 break;
8945 }
8946
8947 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8948
8949 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8950 {
8951 reached = 3;
8952 break;
8953 }
8954 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8955 {
8956 /* We have reached TO_X but not in the line we want. */
8957 skip = move_it_in_display_line_to (it, to_charpos,
8958 -1, MOVE_TO_POS);
8959 if (skip == MOVE_POS_MATCH_OR_ZV)
8960 {
8961 reached = 4;
8962 break;
8963 }
8964 }
8965 }
8966 }
8967 else if (op & MOVE_TO_Y)
8968 {
8969 struct it it_backup;
8970
8971 if (it->line_wrap == WORD_WRAP)
8972 SAVE_IT (it_backup, *it, backup_data);
8973
8974 /* TO_Y specified means stop at TO_X in the line containing
8975 TO_Y---or at TO_CHARPOS if this is reached first. The
8976 problem is that we can't really tell whether the line
8977 contains TO_Y before we have completely scanned it, and
8978 this may skip past TO_X. What we do is to first scan to
8979 TO_X.
8980
8981 If TO_X is not specified, use a TO_X of zero. The reason
8982 is to make the outcome of this function more predictable.
8983 If we didn't use TO_X == 0, we would stop at the end of
8984 the line which is probably not what a caller would expect
8985 to happen. */
8986 skip = move_it_in_display_line_to
8987 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8988 (MOVE_TO_X | (op & MOVE_TO_POS)));
8989
8990 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8991 if (skip == MOVE_POS_MATCH_OR_ZV)
8992 reached = 5;
8993 else if (skip == MOVE_X_REACHED)
8994 {
8995 /* If TO_X was reached, we want to know whether TO_Y is
8996 in the line. We know this is the case if the already
8997 scanned glyphs make the line tall enough. Otherwise,
8998 we must check by scanning the rest of the line. */
8999 line_height = it->max_ascent + it->max_descent;
9000 if (to_y >= it->current_y
9001 && to_y < it->current_y + line_height)
9002 {
9003 reached = 6;
9004 break;
9005 }
9006 SAVE_IT (it_backup, *it, backup_data);
9007 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9008 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9009 op & MOVE_TO_POS);
9010 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9011 line_height = it->max_ascent + it->max_descent;
9012 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9013
9014 if (to_y >= it->current_y
9015 && to_y < it->current_y + line_height)
9016 {
9017 /* If TO_Y is in this line and TO_X was reached
9018 above, we scanned too far. We have to restore
9019 IT's settings to the ones before skipping. But
9020 keep the more accurate values of max_ascent and
9021 max_descent we've found while skipping the rest
9022 of the line, for the sake of callers, such as
9023 pos_visible_p, that need to know the line
9024 height. */
9025 int max_ascent = it->max_ascent;
9026 int max_descent = it->max_descent;
9027
9028 RESTORE_IT (it, &it_backup, backup_data);
9029 it->max_ascent = max_ascent;
9030 it->max_descent = max_descent;
9031 reached = 6;
9032 }
9033 else
9034 {
9035 skip = skip2;
9036 if (skip == MOVE_POS_MATCH_OR_ZV)
9037 reached = 7;
9038 }
9039 }
9040 else
9041 {
9042 /* Check whether TO_Y is in this line. */
9043 line_height = it->max_ascent + it->max_descent;
9044 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9045
9046 if (to_y >= it->current_y
9047 && to_y < it->current_y + line_height)
9048 {
9049 /* When word-wrap is on, TO_X may lie past the end
9050 of a wrapped line. Then it->current is the
9051 character on the next line, so backtrack to the
9052 space before the wrap point. */
9053 if (skip == MOVE_LINE_CONTINUED
9054 && it->line_wrap == WORD_WRAP)
9055 {
9056 int prev_x = max (it->current_x - 1, 0);
9057 RESTORE_IT (it, &it_backup, backup_data);
9058 skip = move_it_in_display_line_to
9059 (it, -1, prev_x, MOVE_TO_X);
9060 }
9061 reached = 6;
9062 }
9063 }
9064
9065 if (reached)
9066 break;
9067 }
9068 else if (BUFFERP (it->object)
9069 && (it->method == GET_FROM_BUFFER
9070 || it->method == GET_FROM_STRETCH)
9071 && IT_CHARPOS (*it) >= to_charpos
9072 /* Under bidi iteration, a call to set_iterator_to_next
9073 can scan far beyond to_charpos if the initial
9074 portion of the next line needs to be reordered. In
9075 that case, give move_it_in_display_line_to another
9076 chance below. */
9077 && !(it->bidi_p
9078 && it->bidi_it.scan_dir == -1))
9079 skip = MOVE_POS_MATCH_OR_ZV;
9080 else
9081 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9082
9083 switch (skip)
9084 {
9085 case MOVE_POS_MATCH_OR_ZV:
9086 reached = 8;
9087 goto out;
9088
9089 case MOVE_NEWLINE_OR_CR:
9090 set_iterator_to_next (it, 1);
9091 it->continuation_lines_width = 0;
9092 break;
9093
9094 case MOVE_LINE_TRUNCATED:
9095 it->continuation_lines_width = 0;
9096 reseat_at_next_visible_line_start (it, 0);
9097 if ((op & MOVE_TO_POS) != 0
9098 && IT_CHARPOS (*it) > to_charpos)
9099 {
9100 reached = 9;
9101 goto out;
9102 }
9103 break;
9104
9105 case MOVE_LINE_CONTINUED:
9106 /* For continued lines ending in a tab, some of the glyphs
9107 associated with the tab are displayed on the current
9108 line. Since it->current_x does not include these glyphs,
9109 we use it->last_visible_x instead. */
9110 if (it->c == '\t')
9111 {
9112 it->continuation_lines_width += it->last_visible_x;
9113 /* When moving by vpos, ensure that the iterator really
9114 advances to the next line (bug#847, bug#969). Fixme:
9115 do we need to do this in other circumstances? */
9116 if (it->current_x != it->last_visible_x
9117 && (op & MOVE_TO_VPOS)
9118 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9119 {
9120 line_start_x = it->current_x + it->pixel_width
9121 - it->last_visible_x;
9122 set_iterator_to_next (it, 0);
9123 }
9124 }
9125 else
9126 it->continuation_lines_width += it->current_x;
9127 break;
9128
9129 default:
9130 emacs_abort ();
9131 }
9132
9133 /* Reset/increment for the next run. */
9134 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9135 it->current_x = line_start_x;
9136 line_start_x = 0;
9137 it->hpos = 0;
9138 it->current_y += it->max_ascent + it->max_descent;
9139 ++it->vpos;
9140 last_height = it->max_ascent + it->max_descent;
9141 it->max_ascent = it->max_descent = 0;
9142 }
9143
9144 out:
9145
9146 /* On text terminals, we may stop at the end of a line in the middle
9147 of a multi-character glyph. If the glyph itself is continued,
9148 i.e. it is actually displayed on the next line, don't treat this
9149 stopping point as valid; move to the next line instead (unless
9150 that brings us offscreen). */
9151 if (!FRAME_WINDOW_P (it->f)
9152 && op & MOVE_TO_POS
9153 && IT_CHARPOS (*it) == to_charpos
9154 && it->what == IT_CHARACTER
9155 && it->nglyphs > 1
9156 && it->line_wrap == WINDOW_WRAP
9157 && it->current_x == it->last_visible_x - 1
9158 && it->c != '\n'
9159 && it->c != '\t'
9160 && it->vpos < it->w->window_end_vpos)
9161 {
9162 it->continuation_lines_width += it->current_x;
9163 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9164 it->current_y += it->max_ascent + it->max_descent;
9165 ++it->vpos;
9166 last_height = it->max_ascent + it->max_descent;
9167 }
9168
9169 if (backup_data)
9170 bidi_unshelve_cache (backup_data, 1);
9171
9172 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9173 }
9174
9175
9176 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9177
9178 If DY > 0, move IT backward at least that many pixels. DY = 0
9179 means move IT backward to the preceding line start or BEGV. This
9180 function may move over more than DY pixels if IT->current_y - DY
9181 ends up in the middle of a line; in this case IT->current_y will be
9182 set to the top of the line moved to. */
9183
9184 void
9185 move_it_vertically_backward (struct it *it, int dy)
9186 {
9187 int nlines, h;
9188 struct it it2, it3;
9189 void *it2data = NULL, *it3data = NULL;
9190 ptrdiff_t start_pos;
9191 int nchars_per_row
9192 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9193 ptrdiff_t pos_limit;
9194
9195 move_further_back:
9196 eassert (dy >= 0);
9197
9198 start_pos = IT_CHARPOS (*it);
9199
9200 /* Estimate how many newlines we must move back. */
9201 nlines = max (1, dy / default_line_pixel_height (it->w));
9202 if (it->line_wrap == TRUNCATE)
9203 pos_limit = BEGV;
9204 else
9205 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9206
9207 /* Set the iterator's position that many lines back. But don't go
9208 back more than NLINES full screen lines -- this wins a day with
9209 buffers which have very long lines. */
9210 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9211 back_to_previous_visible_line_start (it);
9212
9213 /* Reseat the iterator here. When moving backward, we don't want
9214 reseat to skip forward over invisible text, set up the iterator
9215 to deliver from overlay strings at the new position etc. So,
9216 use reseat_1 here. */
9217 reseat_1 (it, it->current.pos, 1);
9218
9219 /* We are now surely at a line start. */
9220 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9221 reordering is in effect. */
9222 it->continuation_lines_width = 0;
9223
9224 /* Move forward and see what y-distance we moved. First move to the
9225 start of the next line so that we get its height. We need this
9226 height to be able to tell whether we reached the specified
9227 y-distance. */
9228 SAVE_IT (it2, *it, it2data);
9229 it2.max_ascent = it2.max_descent = 0;
9230 do
9231 {
9232 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9233 MOVE_TO_POS | MOVE_TO_VPOS);
9234 }
9235 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9236 /* If we are in a display string which starts at START_POS,
9237 and that display string includes a newline, and we are
9238 right after that newline (i.e. at the beginning of a
9239 display line), exit the loop, because otherwise we will
9240 infloop, since move_it_to will see that it is already at
9241 START_POS and will not move. */
9242 || (it2.method == GET_FROM_STRING
9243 && IT_CHARPOS (it2) == start_pos
9244 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9245 eassert (IT_CHARPOS (*it) >= BEGV);
9246 SAVE_IT (it3, it2, it3data);
9247
9248 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9249 eassert (IT_CHARPOS (*it) >= BEGV);
9250 /* H is the actual vertical distance from the position in *IT
9251 and the starting position. */
9252 h = it2.current_y - it->current_y;
9253 /* NLINES is the distance in number of lines. */
9254 nlines = it2.vpos - it->vpos;
9255
9256 /* Correct IT's y and vpos position
9257 so that they are relative to the starting point. */
9258 it->vpos -= nlines;
9259 it->current_y -= h;
9260
9261 if (dy == 0)
9262 {
9263 /* DY == 0 means move to the start of the screen line. The
9264 value of nlines is > 0 if continuation lines were involved,
9265 or if the original IT position was at start of a line. */
9266 RESTORE_IT (it, it, it2data);
9267 if (nlines > 0)
9268 move_it_by_lines (it, nlines);
9269 /* The above code moves us to some position NLINES down,
9270 usually to its first glyph (leftmost in an L2R line), but
9271 that's not necessarily the start of the line, under bidi
9272 reordering. We want to get to the character position
9273 that is immediately after the newline of the previous
9274 line. */
9275 if (it->bidi_p
9276 && !it->continuation_lines_width
9277 && !STRINGP (it->string)
9278 && IT_CHARPOS (*it) > BEGV
9279 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9280 {
9281 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9282
9283 DEC_BOTH (cp, bp);
9284 cp = find_newline_no_quit (cp, bp, -1, NULL);
9285 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9286 }
9287 bidi_unshelve_cache (it3data, 1);
9288 }
9289 else
9290 {
9291 /* The y-position we try to reach, relative to *IT.
9292 Note that H has been subtracted in front of the if-statement. */
9293 int target_y = it->current_y + h - dy;
9294 int y0 = it3.current_y;
9295 int y1;
9296 int line_height;
9297
9298 RESTORE_IT (&it3, &it3, it3data);
9299 y1 = line_bottom_y (&it3);
9300 line_height = y1 - y0;
9301 RESTORE_IT (it, it, it2data);
9302 /* If we did not reach target_y, try to move further backward if
9303 we can. If we moved too far backward, try to move forward. */
9304 if (target_y < it->current_y
9305 /* This is heuristic. In a window that's 3 lines high, with
9306 a line height of 13 pixels each, recentering with point
9307 on the bottom line will try to move -39/2 = 19 pixels
9308 backward. Try to avoid moving into the first line. */
9309 && (it->current_y - target_y
9310 > min (window_box_height (it->w), line_height * 2 / 3))
9311 && IT_CHARPOS (*it) > BEGV)
9312 {
9313 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9314 target_y - it->current_y));
9315 dy = it->current_y - target_y;
9316 goto move_further_back;
9317 }
9318 else if (target_y >= it->current_y + line_height
9319 && IT_CHARPOS (*it) < ZV)
9320 {
9321 /* Should move forward by at least one line, maybe more.
9322
9323 Note: Calling move_it_by_lines can be expensive on
9324 terminal frames, where compute_motion is used (via
9325 vmotion) to do the job, when there are very long lines
9326 and truncate-lines is nil. That's the reason for
9327 treating terminal frames specially here. */
9328
9329 if (!FRAME_WINDOW_P (it->f))
9330 move_it_vertically (it, target_y - (it->current_y + line_height));
9331 else
9332 {
9333 do
9334 {
9335 move_it_by_lines (it, 1);
9336 }
9337 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9338 }
9339 }
9340 }
9341 }
9342
9343
9344 /* Move IT by a specified amount of pixel lines DY. DY negative means
9345 move backwards. DY = 0 means move to start of screen line. At the
9346 end, IT will be on the start of a screen line. */
9347
9348 void
9349 move_it_vertically (struct it *it, int dy)
9350 {
9351 if (dy <= 0)
9352 move_it_vertically_backward (it, -dy);
9353 else
9354 {
9355 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9356 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9357 MOVE_TO_POS | MOVE_TO_Y);
9358 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9359
9360 /* If buffer ends in ZV without a newline, move to the start of
9361 the line to satisfy the post-condition. */
9362 if (IT_CHARPOS (*it) == ZV
9363 && ZV > BEGV
9364 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9365 move_it_by_lines (it, 0);
9366 }
9367 }
9368
9369
9370 /* Move iterator IT past the end of the text line it is in. */
9371
9372 void
9373 move_it_past_eol (struct it *it)
9374 {
9375 enum move_it_result rc;
9376
9377 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9378 if (rc == MOVE_NEWLINE_OR_CR)
9379 set_iterator_to_next (it, 0);
9380 }
9381
9382
9383 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9384 negative means move up. DVPOS == 0 means move to the start of the
9385 screen line.
9386
9387 Optimization idea: If we would know that IT->f doesn't use
9388 a face with proportional font, we could be faster for
9389 truncate-lines nil. */
9390
9391 void
9392 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9393 {
9394
9395 /* The commented-out optimization uses vmotion on terminals. This
9396 gives bad results, because elements like it->what, on which
9397 callers such as pos_visible_p rely, aren't updated. */
9398 /* struct position pos;
9399 if (!FRAME_WINDOW_P (it->f))
9400 {
9401 struct text_pos textpos;
9402
9403 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9404 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9405 reseat (it, textpos, 1);
9406 it->vpos += pos.vpos;
9407 it->current_y += pos.vpos;
9408 }
9409 else */
9410
9411 if (dvpos == 0)
9412 {
9413 /* DVPOS == 0 means move to the start of the screen line. */
9414 move_it_vertically_backward (it, 0);
9415 /* Let next call to line_bottom_y calculate real line height */
9416 last_height = 0;
9417 }
9418 else if (dvpos > 0)
9419 {
9420 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9421 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9422 {
9423 /* Only move to the next buffer position if we ended up in a
9424 string from display property, not in an overlay string
9425 (before-string or after-string). That is because the
9426 latter don't conceal the underlying buffer position, so
9427 we can ask to move the iterator to the exact position we
9428 are interested in. Note that, even if we are already at
9429 IT_CHARPOS (*it), the call below is not a no-op, as it
9430 will detect that we are at the end of the string, pop the
9431 iterator, and compute it->current_x and it->hpos
9432 correctly. */
9433 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9434 -1, -1, -1, MOVE_TO_POS);
9435 }
9436 }
9437 else
9438 {
9439 struct it it2;
9440 void *it2data = NULL;
9441 ptrdiff_t start_charpos, i;
9442 int nchars_per_row
9443 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9444 ptrdiff_t pos_limit;
9445
9446 /* Start at the beginning of the screen line containing IT's
9447 position. This may actually move vertically backwards,
9448 in case of overlays, so adjust dvpos accordingly. */
9449 dvpos += it->vpos;
9450 move_it_vertically_backward (it, 0);
9451 dvpos -= it->vpos;
9452
9453 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9454 screen lines, and reseat the iterator there. */
9455 start_charpos = IT_CHARPOS (*it);
9456 if (it->line_wrap == TRUNCATE)
9457 pos_limit = BEGV;
9458 else
9459 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9460 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9461 back_to_previous_visible_line_start (it);
9462 reseat (it, it->current.pos, 1);
9463
9464 /* Move further back if we end up in a string or an image. */
9465 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9466 {
9467 /* First try to move to start of display line. */
9468 dvpos += it->vpos;
9469 move_it_vertically_backward (it, 0);
9470 dvpos -= it->vpos;
9471 if (IT_POS_VALID_AFTER_MOVE_P (it))
9472 break;
9473 /* If start of line is still in string or image,
9474 move further back. */
9475 back_to_previous_visible_line_start (it);
9476 reseat (it, it->current.pos, 1);
9477 dvpos--;
9478 }
9479
9480 it->current_x = it->hpos = 0;
9481
9482 /* Above call may have moved too far if continuation lines
9483 are involved. Scan forward and see if it did. */
9484 SAVE_IT (it2, *it, it2data);
9485 it2.vpos = it2.current_y = 0;
9486 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9487 it->vpos -= it2.vpos;
9488 it->current_y -= it2.current_y;
9489 it->current_x = it->hpos = 0;
9490
9491 /* If we moved too far back, move IT some lines forward. */
9492 if (it2.vpos > -dvpos)
9493 {
9494 int delta = it2.vpos + dvpos;
9495
9496 RESTORE_IT (&it2, &it2, it2data);
9497 SAVE_IT (it2, *it, it2data);
9498 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9499 /* Move back again if we got too far ahead. */
9500 if (IT_CHARPOS (*it) >= start_charpos)
9501 RESTORE_IT (it, &it2, it2data);
9502 else
9503 bidi_unshelve_cache (it2data, 1);
9504 }
9505 else
9506 RESTORE_IT (it, it, it2data);
9507 }
9508 }
9509
9510 /* Return 1 if IT points into the middle of a display vector. */
9511
9512 int
9513 in_display_vector_p (struct it *it)
9514 {
9515 return (it->method == GET_FROM_DISPLAY_VECTOR
9516 && it->current.dpvec_index > 0
9517 && it->dpvec + it->current.dpvec_index != it->dpend);
9518 }
9519
9520 \f
9521 /***********************************************************************
9522 Messages
9523 ***********************************************************************/
9524
9525
9526 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9527 to *Messages*. */
9528
9529 void
9530 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9531 {
9532 Lisp_Object args[3];
9533 Lisp_Object msg, fmt;
9534 char *buffer;
9535 ptrdiff_t len;
9536 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9537 USE_SAFE_ALLOCA;
9538
9539 fmt = msg = Qnil;
9540 GCPRO4 (fmt, msg, arg1, arg2);
9541
9542 args[0] = fmt = build_string (format);
9543 args[1] = arg1;
9544 args[2] = arg2;
9545 msg = Fformat (3, args);
9546
9547 len = SBYTES (msg) + 1;
9548 buffer = SAFE_ALLOCA (len);
9549 memcpy (buffer, SDATA (msg), len);
9550
9551 message_dolog (buffer, len - 1, 1, 0);
9552 SAFE_FREE ();
9553
9554 UNGCPRO;
9555 }
9556
9557
9558 /* Output a newline in the *Messages* buffer if "needs" one. */
9559
9560 void
9561 message_log_maybe_newline (void)
9562 {
9563 if (message_log_need_newline)
9564 message_dolog ("", 0, 1, 0);
9565 }
9566
9567
9568 /* Add a string M of length NBYTES to the message log, optionally
9569 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9570 true, means interpret the contents of M as multibyte. This
9571 function calls low-level routines in order to bypass text property
9572 hooks, etc. which might not be safe to run.
9573
9574 This may GC (insert may run before/after change hooks),
9575 so the buffer M must NOT point to a Lisp string. */
9576
9577 void
9578 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9579 {
9580 const unsigned char *msg = (const unsigned char *) m;
9581
9582 if (!NILP (Vmemory_full))
9583 return;
9584
9585 if (!NILP (Vmessage_log_max))
9586 {
9587 struct buffer *oldbuf;
9588 Lisp_Object oldpoint, oldbegv, oldzv;
9589 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9590 ptrdiff_t point_at_end = 0;
9591 ptrdiff_t zv_at_end = 0;
9592 Lisp_Object old_deactivate_mark;
9593 bool shown;
9594 struct gcpro gcpro1;
9595
9596 old_deactivate_mark = Vdeactivate_mark;
9597 oldbuf = current_buffer;
9598 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9599 bset_undo_list (current_buffer, Qt);
9600
9601 oldpoint = message_dolog_marker1;
9602 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9603 oldbegv = message_dolog_marker2;
9604 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9605 oldzv = message_dolog_marker3;
9606 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9607 GCPRO1 (old_deactivate_mark);
9608
9609 if (PT == Z)
9610 point_at_end = 1;
9611 if (ZV == Z)
9612 zv_at_end = 1;
9613
9614 BEGV = BEG;
9615 BEGV_BYTE = BEG_BYTE;
9616 ZV = Z;
9617 ZV_BYTE = Z_BYTE;
9618 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9619
9620 /* Insert the string--maybe converting multibyte to single byte
9621 or vice versa, so that all the text fits the buffer. */
9622 if (multibyte
9623 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9624 {
9625 ptrdiff_t i;
9626 int c, char_bytes;
9627 char work[1];
9628
9629 /* Convert a multibyte string to single-byte
9630 for the *Message* buffer. */
9631 for (i = 0; i < nbytes; i += char_bytes)
9632 {
9633 c = string_char_and_length (msg + i, &char_bytes);
9634 work[0] = (ASCII_CHAR_P (c)
9635 ? c
9636 : multibyte_char_to_unibyte (c));
9637 insert_1_both (work, 1, 1, 1, 0, 0);
9638 }
9639 }
9640 else if (! multibyte
9641 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9642 {
9643 ptrdiff_t i;
9644 int c, char_bytes;
9645 unsigned char str[MAX_MULTIBYTE_LENGTH];
9646 /* Convert a single-byte string to multibyte
9647 for the *Message* buffer. */
9648 for (i = 0; i < nbytes; i++)
9649 {
9650 c = msg[i];
9651 MAKE_CHAR_MULTIBYTE (c);
9652 char_bytes = CHAR_STRING (c, str);
9653 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9654 }
9655 }
9656 else if (nbytes)
9657 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9658
9659 if (nlflag)
9660 {
9661 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9662 printmax_t dups;
9663
9664 insert_1_both ("\n", 1, 1, 1, 0, 0);
9665
9666 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9667 this_bol = PT;
9668 this_bol_byte = PT_BYTE;
9669
9670 /* See if this line duplicates the previous one.
9671 If so, combine duplicates. */
9672 if (this_bol > BEG)
9673 {
9674 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9675 prev_bol = PT;
9676 prev_bol_byte = PT_BYTE;
9677
9678 dups = message_log_check_duplicate (prev_bol_byte,
9679 this_bol_byte);
9680 if (dups)
9681 {
9682 del_range_both (prev_bol, prev_bol_byte,
9683 this_bol, this_bol_byte, 0);
9684 if (dups > 1)
9685 {
9686 char dupstr[sizeof " [ times]"
9687 + INT_STRLEN_BOUND (printmax_t)];
9688
9689 /* If you change this format, don't forget to also
9690 change message_log_check_duplicate. */
9691 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9692 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9693 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9694 }
9695 }
9696 }
9697
9698 /* If we have more than the desired maximum number of lines
9699 in the *Messages* buffer now, delete the oldest ones.
9700 This is safe because we don't have undo in this buffer. */
9701
9702 if (NATNUMP (Vmessage_log_max))
9703 {
9704 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9705 -XFASTINT (Vmessage_log_max) - 1, 0);
9706 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9707 }
9708 }
9709 BEGV = marker_position (oldbegv);
9710 BEGV_BYTE = marker_byte_position (oldbegv);
9711
9712 if (zv_at_end)
9713 {
9714 ZV = Z;
9715 ZV_BYTE = Z_BYTE;
9716 }
9717 else
9718 {
9719 ZV = marker_position (oldzv);
9720 ZV_BYTE = marker_byte_position (oldzv);
9721 }
9722
9723 if (point_at_end)
9724 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9725 else
9726 /* We can't do Fgoto_char (oldpoint) because it will run some
9727 Lisp code. */
9728 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9729 marker_byte_position (oldpoint));
9730
9731 UNGCPRO;
9732 unchain_marker (XMARKER (oldpoint));
9733 unchain_marker (XMARKER (oldbegv));
9734 unchain_marker (XMARKER (oldzv));
9735
9736 shown = buffer_window_count (current_buffer) > 0;
9737 set_buffer_internal (oldbuf);
9738 /* We called insert_1_both above with its 5th argument (PREPARE)
9739 zero, which prevents insert_1_both from calling
9740 prepare_to_modify_buffer, which in turns prevents us from
9741 incrementing windows_or_buffers_changed even if *Messages* is
9742 shown in some window. So we must manually incrementing
9743 windows_or_buffers_changed here to make up for that. */
9744 if (shown)
9745 windows_or_buffers_changed++;
9746 else
9747 windows_or_buffers_changed = old_windows_or_buffers_changed;
9748 message_log_need_newline = !nlflag;
9749 Vdeactivate_mark = old_deactivate_mark;
9750 }
9751 }
9752
9753
9754 /* We are at the end of the buffer after just having inserted a newline.
9755 (Note: We depend on the fact we won't be crossing the gap.)
9756 Check to see if the most recent message looks a lot like the previous one.
9757 Return 0 if different, 1 if the new one should just replace it, or a
9758 value N > 1 if we should also append " [N times]". */
9759
9760 static intmax_t
9761 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9762 {
9763 ptrdiff_t i;
9764 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9765 int seen_dots = 0;
9766 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9767 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9768
9769 for (i = 0; i < len; i++)
9770 {
9771 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9772 seen_dots = 1;
9773 if (p1[i] != p2[i])
9774 return seen_dots;
9775 }
9776 p1 += len;
9777 if (*p1 == '\n')
9778 return 2;
9779 if (*p1++ == ' ' && *p1++ == '[')
9780 {
9781 char *pend;
9782 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9783 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9784 return n + 1;
9785 }
9786 return 0;
9787 }
9788 \f
9789
9790 /* Display an echo area message M with a specified length of NBYTES
9791 bytes. The string may include null characters. If M is not a
9792 string, clear out any existing message, and let the mini-buffer
9793 text show through.
9794
9795 This function cancels echoing. */
9796
9797 void
9798 message3 (Lisp_Object m)
9799 {
9800 struct gcpro gcpro1;
9801
9802 GCPRO1 (m);
9803 clear_message (1,1);
9804 cancel_echoing ();
9805
9806 /* First flush out any partial line written with print. */
9807 message_log_maybe_newline ();
9808 if (STRINGP (m))
9809 {
9810 ptrdiff_t nbytes = SBYTES (m);
9811 bool multibyte = STRING_MULTIBYTE (m);
9812 USE_SAFE_ALLOCA;
9813 char *buffer = SAFE_ALLOCA (nbytes);
9814 memcpy (buffer, SDATA (m), nbytes);
9815 message_dolog (buffer, nbytes, 1, multibyte);
9816 SAFE_FREE ();
9817 }
9818 message3_nolog (m);
9819
9820 UNGCPRO;
9821 }
9822
9823
9824 /* The non-logging version of message3.
9825 This does not cancel echoing, because it is used for echoing.
9826 Perhaps we need to make a separate function for echoing
9827 and make this cancel echoing. */
9828
9829 void
9830 message3_nolog (Lisp_Object m)
9831 {
9832 struct frame *sf = SELECTED_FRAME ();
9833
9834 if (FRAME_INITIAL_P (sf))
9835 {
9836 if (noninteractive_need_newline)
9837 putc ('\n', stderr);
9838 noninteractive_need_newline = 0;
9839 if (STRINGP (m))
9840 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9841 if (cursor_in_echo_area == 0)
9842 fprintf (stderr, "\n");
9843 fflush (stderr);
9844 }
9845 /* Error messages get reported properly by cmd_error, so this must be just an
9846 informative message; if the frame hasn't really been initialized yet, just
9847 toss it. */
9848 else if (INTERACTIVE && sf->glyphs_initialized_p)
9849 {
9850 /* Get the frame containing the mini-buffer
9851 that the selected frame is using. */
9852 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9853 Lisp_Object frame = XWINDOW (mini_window)->frame;
9854 struct frame *f = XFRAME (frame);
9855
9856 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9857 Fmake_frame_visible (frame);
9858
9859 if (STRINGP (m) && SCHARS (m) > 0)
9860 {
9861 set_message (m);
9862 if (minibuffer_auto_raise)
9863 Fraise_frame (frame);
9864 /* Assume we are not echoing.
9865 (If we are, echo_now will override this.) */
9866 echo_message_buffer = Qnil;
9867 }
9868 else
9869 clear_message (1, 1);
9870
9871 do_pending_window_change (0);
9872 echo_area_display (1);
9873 do_pending_window_change (0);
9874 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9875 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9876 }
9877 }
9878
9879
9880 /* Display a null-terminated echo area message M. If M is 0, clear
9881 out any existing message, and let the mini-buffer text show through.
9882
9883 The buffer M must continue to exist until after the echo area gets
9884 cleared or some other message gets displayed there. Do not pass
9885 text that is stored in a Lisp string. Do not pass text in a buffer
9886 that was alloca'd. */
9887
9888 void
9889 message1 (const char *m)
9890 {
9891 message3 (m ? build_unibyte_string (m) : Qnil);
9892 }
9893
9894
9895 /* The non-logging counterpart of message1. */
9896
9897 void
9898 message1_nolog (const char *m)
9899 {
9900 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9901 }
9902
9903 /* Display a message M which contains a single %s
9904 which gets replaced with STRING. */
9905
9906 void
9907 message_with_string (const char *m, Lisp_Object string, int log)
9908 {
9909 CHECK_STRING (string);
9910
9911 if (noninteractive)
9912 {
9913 if (m)
9914 {
9915 if (noninteractive_need_newline)
9916 putc ('\n', stderr);
9917 noninteractive_need_newline = 0;
9918 fprintf (stderr, m, SDATA (string));
9919 if (!cursor_in_echo_area)
9920 fprintf (stderr, "\n");
9921 fflush (stderr);
9922 }
9923 }
9924 else if (INTERACTIVE)
9925 {
9926 /* The frame whose minibuffer we're going to display the message on.
9927 It may be larger than the selected frame, so we need
9928 to use its buffer, not the selected frame's buffer. */
9929 Lisp_Object mini_window;
9930 struct frame *f, *sf = SELECTED_FRAME ();
9931
9932 /* Get the frame containing the minibuffer
9933 that the selected frame is using. */
9934 mini_window = FRAME_MINIBUF_WINDOW (sf);
9935 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9936
9937 /* Error messages get reported properly by cmd_error, so this must be
9938 just an informative message; if the frame hasn't really been
9939 initialized yet, just toss it. */
9940 if (f->glyphs_initialized_p)
9941 {
9942 Lisp_Object args[2], msg;
9943 struct gcpro gcpro1, gcpro2;
9944
9945 args[0] = build_string (m);
9946 args[1] = msg = string;
9947 GCPRO2 (args[0], msg);
9948 gcpro1.nvars = 2;
9949
9950 msg = Fformat (2, args);
9951
9952 if (log)
9953 message3 (msg);
9954 else
9955 message3_nolog (msg);
9956
9957 UNGCPRO;
9958
9959 /* Print should start at the beginning of the message
9960 buffer next time. */
9961 message_buf_print = 0;
9962 }
9963 }
9964 }
9965
9966
9967 /* Dump an informative message to the minibuf. If M is 0, clear out
9968 any existing message, and let the mini-buffer text show through. */
9969
9970 static void
9971 vmessage (const char *m, va_list ap)
9972 {
9973 if (noninteractive)
9974 {
9975 if (m)
9976 {
9977 if (noninteractive_need_newline)
9978 putc ('\n', stderr);
9979 noninteractive_need_newline = 0;
9980 vfprintf (stderr, m, ap);
9981 if (cursor_in_echo_area == 0)
9982 fprintf (stderr, "\n");
9983 fflush (stderr);
9984 }
9985 }
9986 else if (INTERACTIVE)
9987 {
9988 /* The frame whose mini-buffer we're going to display the message
9989 on. It may be larger than the selected frame, so we need to
9990 use its buffer, not the selected frame's buffer. */
9991 Lisp_Object mini_window;
9992 struct frame *f, *sf = SELECTED_FRAME ();
9993
9994 /* Get the frame containing the mini-buffer
9995 that the selected frame is using. */
9996 mini_window = FRAME_MINIBUF_WINDOW (sf);
9997 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9998
9999 /* Error messages get reported properly by cmd_error, so this must be
10000 just an informative message; if the frame hasn't really been
10001 initialized yet, just toss it. */
10002 if (f->glyphs_initialized_p)
10003 {
10004 if (m)
10005 {
10006 ptrdiff_t len;
10007 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10008 char *message_buf = alloca (maxsize + 1);
10009
10010 len = doprnt (message_buf, maxsize, m, 0, ap);
10011
10012 message3 (make_string (message_buf, len));
10013 }
10014 else
10015 message1 (0);
10016
10017 /* Print should start at the beginning of the message
10018 buffer next time. */
10019 message_buf_print = 0;
10020 }
10021 }
10022 }
10023
10024 void
10025 message (const char *m, ...)
10026 {
10027 va_list ap;
10028 va_start (ap, m);
10029 vmessage (m, ap);
10030 va_end (ap);
10031 }
10032
10033
10034 #if 0
10035 /* The non-logging version of message. */
10036
10037 void
10038 message_nolog (const char *m, ...)
10039 {
10040 Lisp_Object old_log_max;
10041 va_list ap;
10042 va_start (ap, m);
10043 old_log_max = Vmessage_log_max;
10044 Vmessage_log_max = Qnil;
10045 vmessage (m, ap);
10046 Vmessage_log_max = old_log_max;
10047 va_end (ap);
10048 }
10049 #endif
10050
10051
10052 /* Display the current message in the current mini-buffer. This is
10053 only called from error handlers in process.c, and is not time
10054 critical. */
10055
10056 void
10057 update_echo_area (void)
10058 {
10059 if (!NILP (echo_area_buffer[0]))
10060 {
10061 Lisp_Object string;
10062 string = Fcurrent_message ();
10063 message3 (string);
10064 }
10065 }
10066
10067
10068 /* Make sure echo area buffers in `echo_buffers' are live.
10069 If they aren't, make new ones. */
10070
10071 static void
10072 ensure_echo_area_buffers (void)
10073 {
10074 int i;
10075
10076 for (i = 0; i < 2; ++i)
10077 if (!BUFFERP (echo_buffer[i])
10078 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10079 {
10080 char name[30];
10081 Lisp_Object old_buffer;
10082 int j;
10083
10084 old_buffer = echo_buffer[i];
10085 echo_buffer[i] = Fget_buffer_create
10086 (make_formatted_string (name, " *Echo Area %d*", i));
10087 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10088 /* to force word wrap in echo area -
10089 it was decided to postpone this*/
10090 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10091
10092 for (j = 0; j < 2; ++j)
10093 if (EQ (old_buffer, echo_area_buffer[j]))
10094 echo_area_buffer[j] = echo_buffer[i];
10095 }
10096 }
10097
10098
10099 /* Call FN with args A1..A2 with either the current or last displayed
10100 echo_area_buffer as current buffer.
10101
10102 WHICH zero means use the current message buffer
10103 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10104 from echo_buffer[] and clear it.
10105
10106 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10107 suitable buffer from echo_buffer[] and clear it.
10108
10109 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10110 that the current message becomes the last displayed one, make
10111 choose a suitable buffer for echo_area_buffer[0], and clear it.
10112
10113 Value is what FN returns. */
10114
10115 static int
10116 with_echo_area_buffer (struct window *w, int which,
10117 int (*fn) (ptrdiff_t, Lisp_Object),
10118 ptrdiff_t a1, Lisp_Object a2)
10119 {
10120 Lisp_Object buffer;
10121 int this_one, the_other, clear_buffer_p, rc;
10122 ptrdiff_t count = SPECPDL_INDEX ();
10123
10124 /* If buffers aren't live, make new ones. */
10125 ensure_echo_area_buffers ();
10126
10127 clear_buffer_p = 0;
10128
10129 if (which == 0)
10130 this_one = 0, the_other = 1;
10131 else if (which > 0)
10132 this_one = 1, the_other = 0;
10133 else
10134 {
10135 this_one = 0, the_other = 1;
10136 clear_buffer_p = 1;
10137
10138 /* We need a fresh one in case the current echo buffer equals
10139 the one containing the last displayed echo area message. */
10140 if (!NILP (echo_area_buffer[this_one])
10141 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10142 echo_area_buffer[this_one] = Qnil;
10143 }
10144
10145 /* Choose a suitable buffer from echo_buffer[] is we don't
10146 have one. */
10147 if (NILP (echo_area_buffer[this_one]))
10148 {
10149 echo_area_buffer[this_one]
10150 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10151 ? echo_buffer[the_other]
10152 : echo_buffer[this_one]);
10153 clear_buffer_p = 1;
10154 }
10155
10156 buffer = echo_area_buffer[this_one];
10157
10158 /* Don't get confused by reusing the buffer used for echoing
10159 for a different purpose. */
10160 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10161 cancel_echoing ();
10162
10163 record_unwind_protect (unwind_with_echo_area_buffer,
10164 with_echo_area_buffer_unwind_data (w));
10165
10166 /* Make the echo area buffer current. Note that for display
10167 purposes, it is not necessary that the displayed window's buffer
10168 == current_buffer, except for text property lookup. So, let's
10169 only set that buffer temporarily here without doing a full
10170 Fset_window_buffer. We must also change w->pointm, though,
10171 because otherwise an assertions in unshow_buffer fails, and Emacs
10172 aborts. */
10173 set_buffer_internal_1 (XBUFFER (buffer));
10174 if (w)
10175 {
10176 wset_buffer (w, buffer);
10177 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10178 }
10179
10180 bset_undo_list (current_buffer, Qt);
10181 bset_read_only (current_buffer, Qnil);
10182 specbind (Qinhibit_read_only, Qt);
10183 specbind (Qinhibit_modification_hooks, Qt);
10184
10185 if (clear_buffer_p && Z > BEG)
10186 del_range (BEG, Z);
10187
10188 eassert (BEGV >= BEG);
10189 eassert (ZV <= Z && ZV >= BEGV);
10190
10191 rc = fn (a1, a2);
10192
10193 eassert (BEGV >= BEG);
10194 eassert (ZV <= Z && ZV >= BEGV);
10195
10196 unbind_to (count, Qnil);
10197 return rc;
10198 }
10199
10200
10201 /* Save state that should be preserved around the call to the function
10202 FN called in with_echo_area_buffer. */
10203
10204 static Lisp_Object
10205 with_echo_area_buffer_unwind_data (struct window *w)
10206 {
10207 int i = 0;
10208 Lisp_Object vector, tmp;
10209
10210 /* Reduce consing by keeping one vector in
10211 Vwith_echo_area_save_vector. */
10212 vector = Vwith_echo_area_save_vector;
10213 Vwith_echo_area_save_vector = Qnil;
10214
10215 if (NILP (vector))
10216 vector = Fmake_vector (make_number (9), Qnil);
10217
10218 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10219 ASET (vector, i, Vdeactivate_mark); ++i;
10220 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10221
10222 if (w)
10223 {
10224 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10225 ASET (vector, i, w->contents); ++i;
10226 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10227 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10228 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10229 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10230 }
10231 else
10232 {
10233 int end = i + 6;
10234 for (; i < end; ++i)
10235 ASET (vector, i, Qnil);
10236 }
10237
10238 eassert (i == ASIZE (vector));
10239 return vector;
10240 }
10241
10242
10243 /* Restore global state from VECTOR which was created by
10244 with_echo_area_buffer_unwind_data. */
10245
10246 static void
10247 unwind_with_echo_area_buffer (Lisp_Object vector)
10248 {
10249 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10250 Vdeactivate_mark = AREF (vector, 1);
10251 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10252
10253 if (WINDOWP (AREF (vector, 3)))
10254 {
10255 struct window *w;
10256 Lisp_Object buffer;
10257
10258 w = XWINDOW (AREF (vector, 3));
10259 buffer = AREF (vector, 4);
10260
10261 wset_buffer (w, buffer);
10262 set_marker_both (w->pointm, buffer,
10263 XFASTINT (AREF (vector, 5)),
10264 XFASTINT (AREF (vector, 6)));
10265 set_marker_both (w->start, buffer,
10266 XFASTINT (AREF (vector, 7)),
10267 XFASTINT (AREF (vector, 8)));
10268 }
10269
10270 Vwith_echo_area_save_vector = vector;
10271 }
10272
10273
10274 /* Set up the echo area for use by print functions. MULTIBYTE_P
10275 non-zero means we will print multibyte. */
10276
10277 void
10278 setup_echo_area_for_printing (int multibyte_p)
10279 {
10280 /* If we can't find an echo area any more, exit. */
10281 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10282 Fkill_emacs (Qnil);
10283
10284 ensure_echo_area_buffers ();
10285
10286 if (!message_buf_print)
10287 {
10288 /* A message has been output since the last time we printed.
10289 Choose a fresh echo area buffer. */
10290 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10291 echo_area_buffer[0] = echo_buffer[1];
10292 else
10293 echo_area_buffer[0] = echo_buffer[0];
10294
10295 /* Switch to that buffer and clear it. */
10296 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10297 bset_truncate_lines (current_buffer, Qnil);
10298
10299 if (Z > BEG)
10300 {
10301 ptrdiff_t count = SPECPDL_INDEX ();
10302 specbind (Qinhibit_read_only, Qt);
10303 /* Note that undo recording is always disabled. */
10304 del_range (BEG, Z);
10305 unbind_to (count, Qnil);
10306 }
10307 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10308
10309 /* Set up the buffer for the multibyteness we need. */
10310 if (multibyte_p
10311 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10312 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10313
10314 /* Raise the frame containing the echo area. */
10315 if (minibuffer_auto_raise)
10316 {
10317 struct frame *sf = SELECTED_FRAME ();
10318 Lisp_Object mini_window;
10319 mini_window = FRAME_MINIBUF_WINDOW (sf);
10320 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10321 }
10322
10323 message_log_maybe_newline ();
10324 message_buf_print = 1;
10325 }
10326 else
10327 {
10328 if (NILP (echo_area_buffer[0]))
10329 {
10330 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10331 echo_area_buffer[0] = echo_buffer[1];
10332 else
10333 echo_area_buffer[0] = echo_buffer[0];
10334 }
10335
10336 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10337 {
10338 /* Someone switched buffers between print requests. */
10339 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10340 bset_truncate_lines (current_buffer, Qnil);
10341 }
10342 }
10343 }
10344
10345
10346 /* Display an echo area message in window W. Value is non-zero if W's
10347 height is changed. If display_last_displayed_message_p is
10348 non-zero, display the message that was last displayed, otherwise
10349 display the current message. */
10350
10351 static int
10352 display_echo_area (struct window *w)
10353 {
10354 int i, no_message_p, window_height_changed_p;
10355
10356 /* Temporarily disable garbage collections while displaying the echo
10357 area. This is done because a GC can print a message itself.
10358 That message would modify the echo area buffer's contents while a
10359 redisplay of the buffer is going on, and seriously confuse
10360 redisplay. */
10361 ptrdiff_t count = inhibit_garbage_collection ();
10362
10363 /* If there is no message, we must call display_echo_area_1
10364 nevertheless because it resizes the window. But we will have to
10365 reset the echo_area_buffer in question to nil at the end because
10366 with_echo_area_buffer will sets it to an empty buffer. */
10367 i = display_last_displayed_message_p ? 1 : 0;
10368 no_message_p = NILP (echo_area_buffer[i]);
10369
10370 window_height_changed_p
10371 = with_echo_area_buffer (w, display_last_displayed_message_p,
10372 display_echo_area_1,
10373 (intptr_t) w, Qnil);
10374
10375 if (no_message_p)
10376 echo_area_buffer[i] = Qnil;
10377
10378 unbind_to (count, Qnil);
10379 return window_height_changed_p;
10380 }
10381
10382
10383 /* Helper for display_echo_area. Display the current buffer which
10384 contains the current echo area message in window W, a mini-window,
10385 a pointer to which is passed in A1. A2..A4 are currently not used.
10386 Change the height of W so that all of the message is displayed.
10387 Value is non-zero if height of W was changed. */
10388
10389 static int
10390 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10391 {
10392 intptr_t i1 = a1;
10393 struct window *w = (struct window *) i1;
10394 Lisp_Object window;
10395 struct text_pos start;
10396 int window_height_changed_p = 0;
10397
10398 /* Do this before displaying, so that we have a large enough glyph
10399 matrix for the display. If we can't get enough space for the
10400 whole text, display the last N lines. That works by setting w->start. */
10401 window_height_changed_p = resize_mini_window (w, 0);
10402
10403 /* Use the starting position chosen by resize_mini_window. */
10404 SET_TEXT_POS_FROM_MARKER (start, w->start);
10405
10406 /* Display. */
10407 clear_glyph_matrix (w->desired_matrix);
10408 XSETWINDOW (window, w);
10409 try_window (window, start, 0);
10410
10411 return window_height_changed_p;
10412 }
10413
10414
10415 /* Resize the echo area window to exactly the size needed for the
10416 currently displayed message, if there is one. If a mini-buffer
10417 is active, don't shrink it. */
10418
10419 void
10420 resize_echo_area_exactly (void)
10421 {
10422 if (BUFFERP (echo_area_buffer[0])
10423 && WINDOWP (echo_area_window))
10424 {
10425 struct window *w = XWINDOW (echo_area_window);
10426 int resized_p;
10427 Lisp_Object resize_exactly;
10428
10429 if (minibuf_level == 0)
10430 resize_exactly = Qt;
10431 else
10432 resize_exactly = Qnil;
10433
10434 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10435 (intptr_t) w, resize_exactly);
10436 if (resized_p)
10437 {
10438 ++windows_or_buffers_changed;
10439 ++update_mode_lines;
10440 redisplay_internal ();
10441 }
10442 }
10443 }
10444
10445
10446 /* Callback function for with_echo_area_buffer, when used from
10447 resize_echo_area_exactly. A1 contains a pointer to the window to
10448 resize, EXACTLY non-nil means resize the mini-window exactly to the
10449 size of the text displayed. A3 and A4 are not used. Value is what
10450 resize_mini_window returns. */
10451
10452 static int
10453 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10454 {
10455 intptr_t i1 = a1;
10456 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10457 }
10458
10459
10460 /* Resize mini-window W to fit the size of its contents. EXACT_P
10461 means size the window exactly to the size needed. Otherwise, it's
10462 only enlarged until W's buffer is empty.
10463
10464 Set W->start to the right place to begin display. If the whole
10465 contents fit, start at the beginning. Otherwise, start so as
10466 to make the end of the contents appear. This is particularly
10467 important for y-or-n-p, but seems desirable generally.
10468
10469 Value is non-zero if the window height has been changed. */
10470
10471 int
10472 resize_mini_window (struct window *w, int exact_p)
10473 {
10474 struct frame *f = XFRAME (w->frame);
10475 int window_height_changed_p = 0;
10476
10477 eassert (MINI_WINDOW_P (w));
10478
10479 /* By default, start display at the beginning. */
10480 set_marker_both (w->start, w->contents,
10481 BUF_BEGV (XBUFFER (w->contents)),
10482 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10483
10484 /* Don't resize windows while redisplaying a window; it would
10485 confuse redisplay functions when the size of the window they are
10486 displaying changes from under them. Such a resizing can happen,
10487 for instance, when which-func prints a long message while
10488 we are running fontification-functions. We're running these
10489 functions with safe_call which binds inhibit-redisplay to t. */
10490 if (!NILP (Vinhibit_redisplay))
10491 return 0;
10492
10493 /* Nil means don't try to resize. */
10494 if (NILP (Vresize_mini_windows)
10495 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10496 return 0;
10497
10498 if (!FRAME_MINIBUF_ONLY_P (f))
10499 {
10500 struct it it;
10501 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10502 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10503 int height;
10504 EMACS_INT max_height;
10505 int unit = FRAME_LINE_HEIGHT (f);
10506 struct text_pos start;
10507 struct buffer *old_current_buffer = NULL;
10508
10509 if (current_buffer != XBUFFER (w->contents))
10510 {
10511 old_current_buffer = current_buffer;
10512 set_buffer_internal (XBUFFER (w->contents));
10513 }
10514
10515 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10516
10517 /* Compute the max. number of lines specified by the user. */
10518 if (FLOATP (Vmax_mini_window_height))
10519 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10520 else if (INTEGERP (Vmax_mini_window_height))
10521 max_height = XINT (Vmax_mini_window_height);
10522 else
10523 max_height = total_height / 4;
10524
10525 /* Correct that max. height if it's bogus. */
10526 max_height = clip_to_bounds (1, max_height, total_height);
10527
10528 /* Find out the height of the text in the window. */
10529 if (it.line_wrap == TRUNCATE)
10530 height = 1;
10531 else
10532 {
10533 last_height = 0;
10534 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10535 if (it.max_ascent == 0 && it.max_descent == 0)
10536 height = it.current_y + last_height;
10537 else
10538 height = it.current_y + it.max_ascent + it.max_descent;
10539 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10540 height = (height + unit - 1) / unit;
10541 }
10542
10543 /* Compute a suitable window start. */
10544 if (height > max_height)
10545 {
10546 height = max_height;
10547 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10548 move_it_vertically_backward (&it, (height - 1) * unit);
10549 start = it.current.pos;
10550 }
10551 else
10552 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10553 SET_MARKER_FROM_TEXT_POS (w->start, start);
10554
10555 if (EQ (Vresize_mini_windows, Qgrow_only))
10556 {
10557 /* Let it grow only, until we display an empty message, in which
10558 case the window shrinks again. */
10559 if (height > WINDOW_TOTAL_LINES (w))
10560 {
10561 int old_height = WINDOW_TOTAL_LINES (w);
10562
10563 FRAME_WINDOWS_FROZEN (f) = 1;
10564 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10565 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10566 }
10567 else if (height < WINDOW_TOTAL_LINES (w)
10568 && (exact_p || BEGV == ZV))
10569 {
10570 int old_height = WINDOW_TOTAL_LINES (w);
10571
10572 FRAME_WINDOWS_FROZEN (f) = 0;
10573 shrink_mini_window (w);
10574 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10575 }
10576 }
10577 else
10578 {
10579 /* Always resize to exact size needed. */
10580 if (height > WINDOW_TOTAL_LINES (w))
10581 {
10582 int old_height = WINDOW_TOTAL_LINES (w);
10583
10584 FRAME_WINDOWS_FROZEN (f) = 1;
10585 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10586 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10587 }
10588 else if (height < WINDOW_TOTAL_LINES (w))
10589 {
10590 int old_height = WINDOW_TOTAL_LINES (w);
10591
10592 FRAME_WINDOWS_FROZEN (f) = 0;
10593 shrink_mini_window (w);
10594
10595 if (height)
10596 {
10597 FRAME_WINDOWS_FROZEN (f) = 1;
10598 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10599 }
10600
10601 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10602 }
10603 }
10604
10605 if (old_current_buffer)
10606 set_buffer_internal (old_current_buffer);
10607 }
10608
10609 return window_height_changed_p;
10610 }
10611
10612
10613 /* Value is the current message, a string, or nil if there is no
10614 current message. */
10615
10616 Lisp_Object
10617 current_message (void)
10618 {
10619 Lisp_Object msg;
10620
10621 if (!BUFFERP (echo_area_buffer[0]))
10622 msg = Qnil;
10623 else
10624 {
10625 with_echo_area_buffer (0, 0, current_message_1,
10626 (intptr_t) &msg, Qnil);
10627 if (NILP (msg))
10628 echo_area_buffer[0] = Qnil;
10629 }
10630
10631 return msg;
10632 }
10633
10634
10635 static int
10636 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10637 {
10638 intptr_t i1 = a1;
10639 Lisp_Object *msg = (Lisp_Object *) i1;
10640
10641 if (Z > BEG)
10642 *msg = make_buffer_string (BEG, Z, 1);
10643 else
10644 *msg = Qnil;
10645 return 0;
10646 }
10647
10648
10649 /* Push the current message on Vmessage_stack for later restoration
10650 by restore_message. Value is non-zero if the current message isn't
10651 empty. This is a relatively infrequent operation, so it's not
10652 worth optimizing. */
10653
10654 bool
10655 push_message (void)
10656 {
10657 Lisp_Object msg = current_message ();
10658 Vmessage_stack = Fcons (msg, Vmessage_stack);
10659 return STRINGP (msg);
10660 }
10661
10662
10663 /* Restore message display from the top of Vmessage_stack. */
10664
10665 void
10666 restore_message (void)
10667 {
10668 eassert (CONSP (Vmessage_stack));
10669 message3_nolog (XCAR (Vmessage_stack));
10670 }
10671
10672
10673 /* Handler for unwind-protect calling pop_message. */
10674
10675 void
10676 pop_message_unwind (void)
10677 {
10678 /* Pop the top-most entry off Vmessage_stack. */
10679 eassert (CONSP (Vmessage_stack));
10680 Vmessage_stack = XCDR (Vmessage_stack);
10681 }
10682
10683
10684 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10685 exits. If the stack is not empty, we have a missing pop_message
10686 somewhere. */
10687
10688 void
10689 check_message_stack (void)
10690 {
10691 if (!NILP (Vmessage_stack))
10692 emacs_abort ();
10693 }
10694
10695
10696 /* Truncate to NCHARS what will be displayed in the echo area the next
10697 time we display it---but don't redisplay it now. */
10698
10699 void
10700 truncate_echo_area (ptrdiff_t nchars)
10701 {
10702 if (nchars == 0)
10703 echo_area_buffer[0] = Qnil;
10704 else if (!noninteractive
10705 && INTERACTIVE
10706 && !NILP (echo_area_buffer[0]))
10707 {
10708 struct frame *sf = SELECTED_FRAME ();
10709 /* Error messages get reported properly by cmd_error, so this must be
10710 just an informative message; if the frame hasn't really been
10711 initialized yet, just toss it. */
10712 if (sf->glyphs_initialized_p)
10713 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10714 }
10715 }
10716
10717
10718 /* Helper function for truncate_echo_area. Truncate the current
10719 message to at most NCHARS characters. */
10720
10721 static int
10722 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10723 {
10724 if (BEG + nchars < Z)
10725 del_range (BEG + nchars, Z);
10726 if (Z == BEG)
10727 echo_area_buffer[0] = Qnil;
10728 return 0;
10729 }
10730
10731 /* Set the current message to STRING. */
10732
10733 static void
10734 set_message (Lisp_Object string)
10735 {
10736 eassert (STRINGP (string));
10737
10738 message_enable_multibyte = STRING_MULTIBYTE (string);
10739
10740 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10741 message_buf_print = 0;
10742 help_echo_showing_p = 0;
10743
10744 if (STRINGP (Vdebug_on_message)
10745 && STRINGP (string)
10746 && fast_string_match (Vdebug_on_message, string) >= 0)
10747 call_debugger (list2 (Qerror, string));
10748 }
10749
10750
10751 /* Helper function for set_message. First argument is ignored and second
10752 argument has the same meaning as for set_message.
10753 This function is called with the echo area buffer being current. */
10754
10755 static int
10756 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10757 {
10758 eassert (STRINGP (string));
10759
10760 /* Change multibyteness of the echo buffer appropriately. */
10761 if (message_enable_multibyte
10762 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10763 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10764
10765 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10766 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10767 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10768
10769 /* Insert new message at BEG. */
10770 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10771
10772 /* This function takes care of single/multibyte conversion.
10773 We just have to ensure that the echo area buffer has the right
10774 setting of enable_multibyte_characters. */
10775 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10776
10777 return 0;
10778 }
10779
10780
10781 /* Clear messages. CURRENT_P non-zero means clear the current
10782 message. LAST_DISPLAYED_P non-zero means clear the message
10783 last displayed. */
10784
10785 void
10786 clear_message (int current_p, int last_displayed_p)
10787 {
10788 if (current_p)
10789 {
10790 echo_area_buffer[0] = Qnil;
10791 message_cleared_p = 1;
10792 }
10793
10794 if (last_displayed_p)
10795 echo_area_buffer[1] = Qnil;
10796
10797 message_buf_print = 0;
10798 }
10799
10800 /* Clear garbaged frames.
10801
10802 This function is used where the old redisplay called
10803 redraw_garbaged_frames which in turn called redraw_frame which in
10804 turn called clear_frame. The call to clear_frame was a source of
10805 flickering. I believe a clear_frame is not necessary. It should
10806 suffice in the new redisplay to invalidate all current matrices,
10807 and ensure a complete redisplay of all windows. */
10808
10809 static void
10810 clear_garbaged_frames (void)
10811 {
10812 if (frame_garbaged)
10813 {
10814 Lisp_Object tail, frame;
10815 int changed_count = 0;
10816
10817 FOR_EACH_FRAME (tail, frame)
10818 {
10819 struct frame *f = XFRAME (frame);
10820
10821 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10822 {
10823 if (f->resized_p)
10824 {
10825 redraw_frame (f);
10826 f->force_flush_display_p = 1;
10827 }
10828 clear_current_matrices (f);
10829 changed_count++;
10830 f->garbaged = 0;
10831 f->resized_p = 0;
10832 }
10833 }
10834
10835 frame_garbaged = 0;
10836 if (changed_count)
10837 ++windows_or_buffers_changed;
10838 }
10839 }
10840
10841
10842 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10843 is non-zero update selected_frame. Value is non-zero if the
10844 mini-windows height has been changed. */
10845
10846 static int
10847 echo_area_display (int update_frame_p)
10848 {
10849 Lisp_Object mini_window;
10850 struct window *w;
10851 struct frame *f;
10852 int window_height_changed_p = 0;
10853 struct frame *sf = SELECTED_FRAME ();
10854
10855 mini_window = FRAME_MINIBUF_WINDOW (sf);
10856 w = XWINDOW (mini_window);
10857 f = XFRAME (WINDOW_FRAME (w));
10858
10859 /* Don't display if frame is invisible or not yet initialized. */
10860 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10861 return 0;
10862
10863 #ifdef HAVE_WINDOW_SYSTEM
10864 /* When Emacs starts, selected_frame may be the initial terminal
10865 frame. If we let this through, a message would be displayed on
10866 the terminal. */
10867 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10868 return 0;
10869 #endif /* HAVE_WINDOW_SYSTEM */
10870
10871 /* Redraw garbaged frames. */
10872 clear_garbaged_frames ();
10873
10874 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10875 {
10876 echo_area_window = mini_window;
10877 window_height_changed_p = display_echo_area (w);
10878 w->must_be_updated_p = 1;
10879
10880 /* Update the display, unless called from redisplay_internal.
10881 Also don't update the screen during redisplay itself. The
10882 update will happen at the end of redisplay, and an update
10883 here could cause confusion. */
10884 if (update_frame_p && !redisplaying_p)
10885 {
10886 int n = 0;
10887
10888 /* If the display update has been interrupted by pending
10889 input, update mode lines in the frame. Due to the
10890 pending input, it might have been that redisplay hasn't
10891 been called, so that mode lines above the echo area are
10892 garbaged. This looks odd, so we prevent it here. */
10893 if (!display_completed)
10894 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10895
10896 if (window_height_changed_p
10897 /* Don't do this if Emacs is shutting down. Redisplay
10898 needs to run hooks. */
10899 && !NILP (Vrun_hooks))
10900 {
10901 /* Must update other windows. Likewise as in other
10902 cases, don't let this update be interrupted by
10903 pending input. */
10904 ptrdiff_t count = SPECPDL_INDEX ();
10905 specbind (Qredisplay_dont_pause, Qt);
10906 windows_or_buffers_changed = 1;
10907 redisplay_internal ();
10908 unbind_to (count, Qnil);
10909 }
10910 else if (FRAME_WINDOW_P (f) && n == 0)
10911 {
10912 /* Window configuration is the same as before.
10913 Can do with a display update of the echo area,
10914 unless we displayed some mode lines. */
10915 update_single_window (w, 1);
10916 FRAME_RIF (f)->flush_display (f);
10917 }
10918 else
10919 update_frame (f, 1, 1);
10920
10921 /* If cursor is in the echo area, make sure that the next
10922 redisplay displays the minibuffer, so that the cursor will
10923 be replaced with what the minibuffer wants. */
10924 if (cursor_in_echo_area)
10925 ++windows_or_buffers_changed;
10926 }
10927 }
10928 else if (!EQ (mini_window, selected_window))
10929 windows_or_buffers_changed++;
10930
10931 /* Last displayed message is now the current message. */
10932 echo_area_buffer[1] = echo_area_buffer[0];
10933 /* Inform read_char that we're not echoing. */
10934 echo_message_buffer = Qnil;
10935
10936 /* Prevent redisplay optimization in redisplay_internal by resetting
10937 this_line_start_pos. This is done because the mini-buffer now
10938 displays the message instead of its buffer text. */
10939 if (EQ (mini_window, selected_window))
10940 CHARPOS (this_line_start_pos) = 0;
10941
10942 return window_height_changed_p;
10943 }
10944
10945 /* Nonzero if the current window's buffer is shown in more than one
10946 window and was modified since last redisplay. */
10947
10948 static int
10949 buffer_shared_and_changed (void)
10950 {
10951 return (buffer_window_count (current_buffer) > 1
10952 && UNCHANGED_MODIFIED < MODIFF);
10953 }
10954
10955 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10956 is enabled and mark of W's buffer was changed since last W's update. */
10957
10958 static int
10959 window_buffer_changed (struct window *w)
10960 {
10961 struct buffer *b = XBUFFER (w->contents);
10962
10963 eassert (BUFFER_LIVE_P (b));
10964
10965 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10966 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10967 != (w->region_showing != 0)));
10968 }
10969
10970 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10971
10972 static int
10973 mode_line_update_needed (struct window *w)
10974 {
10975 return (w->column_number_displayed != -1
10976 && !(PT == w->last_point && !window_outdated (w))
10977 && (w->column_number_displayed != current_column ()));
10978 }
10979
10980 /* Nonzero if window start of W is frozen and may not be changed during
10981 redisplay. */
10982
10983 static bool
10984 window_frozen_p (struct window *w)
10985 {
10986 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10987 {
10988 Lisp_Object window;
10989
10990 XSETWINDOW (window, w);
10991 if (MINI_WINDOW_P (w))
10992 return 0;
10993 else if (EQ (window, selected_window))
10994 return 0;
10995 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10996 && EQ (window, Vminibuf_scroll_window))
10997 /* This special window can't be frozen too. */
10998 return 0;
10999 else
11000 return 1;
11001 }
11002 return 0;
11003 }
11004
11005 /***********************************************************************
11006 Mode Lines and Frame Titles
11007 ***********************************************************************/
11008
11009 /* A buffer for constructing non-propertized mode-line strings and
11010 frame titles in it; allocated from the heap in init_xdisp and
11011 resized as needed in store_mode_line_noprop_char. */
11012
11013 static char *mode_line_noprop_buf;
11014
11015 /* The buffer's end, and a current output position in it. */
11016
11017 static char *mode_line_noprop_buf_end;
11018 static char *mode_line_noprop_ptr;
11019
11020 #define MODE_LINE_NOPROP_LEN(start) \
11021 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11022
11023 static enum {
11024 MODE_LINE_DISPLAY = 0,
11025 MODE_LINE_TITLE,
11026 MODE_LINE_NOPROP,
11027 MODE_LINE_STRING
11028 } mode_line_target;
11029
11030 /* Alist that caches the results of :propertize.
11031 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11032 static Lisp_Object mode_line_proptrans_alist;
11033
11034 /* List of strings making up the mode-line. */
11035 static Lisp_Object mode_line_string_list;
11036
11037 /* Base face property when building propertized mode line string. */
11038 static Lisp_Object mode_line_string_face;
11039 static Lisp_Object mode_line_string_face_prop;
11040
11041
11042 /* Unwind data for mode line strings */
11043
11044 static Lisp_Object Vmode_line_unwind_vector;
11045
11046 static Lisp_Object
11047 format_mode_line_unwind_data (struct frame *target_frame,
11048 struct buffer *obuf,
11049 Lisp_Object owin,
11050 int save_proptrans)
11051 {
11052 Lisp_Object vector, tmp;
11053
11054 /* Reduce consing by keeping one vector in
11055 Vwith_echo_area_save_vector. */
11056 vector = Vmode_line_unwind_vector;
11057 Vmode_line_unwind_vector = Qnil;
11058
11059 if (NILP (vector))
11060 vector = Fmake_vector (make_number (10), Qnil);
11061
11062 ASET (vector, 0, make_number (mode_line_target));
11063 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11064 ASET (vector, 2, mode_line_string_list);
11065 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11066 ASET (vector, 4, mode_line_string_face);
11067 ASET (vector, 5, mode_line_string_face_prop);
11068
11069 if (obuf)
11070 XSETBUFFER (tmp, obuf);
11071 else
11072 tmp = Qnil;
11073 ASET (vector, 6, tmp);
11074 ASET (vector, 7, owin);
11075 if (target_frame)
11076 {
11077 /* Similarly to `with-selected-window', if the operation selects
11078 a window on another frame, we must restore that frame's
11079 selected window, and (for a tty) the top-frame. */
11080 ASET (vector, 8, target_frame->selected_window);
11081 if (FRAME_TERMCAP_P (target_frame))
11082 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11083 }
11084
11085 return vector;
11086 }
11087
11088 static void
11089 unwind_format_mode_line (Lisp_Object vector)
11090 {
11091 Lisp_Object old_window = AREF (vector, 7);
11092 Lisp_Object target_frame_window = AREF (vector, 8);
11093 Lisp_Object old_top_frame = AREF (vector, 9);
11094
11095 mode_line_target = XINT (AREF (vector, 0));
11096 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11097 mode_line_string_list = AREF (vector, 2);
11098 if (! EQ (AREF (vector, 3), Qt))
11099 mode_line_proptrans_alist = AREF (vector, 3);
11100 mode_line_string_face = AREF (vector, 4);
11101 mode_line_string_face_prop = AREF (vector, 5);
11102
11103 /* Select window before buffer, since it may change the buffer. */
11104 if (!NILP (old_window))
11105 {
11106 /* If the operation that we are unwinding had selected a window
11107 on a different frame, reset its frame-selected-window. For a
11108 text terminal, reset its top-frame if necessary. */
11109 if (!NILP (target_frame_window))
11110 {
11111 Lisp_Object frame
11112 = WINDOW_FRAME (XWINDOW (target_frame_window));
11113
11114 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11115 Fselect_window (target_frame_window, Qt);
11116
11117 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11118 Fselect_frame (old_top_frame, Qt);
11119 }
11120
11121 Fselect_window (old_window, Qt);
11122 }
11123
11124 if (!NILP (AREF (vector, 6)))
11125 {
11126 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11127 ASET (vector, 6, Qnil);
11128 }
11129
11130 Vmode_line_unwind_vector = vector;
11131 }
11132
11133
11134 /* Store a single character C for the frame title in mode_line_noprop_buf.
11135 Re-allocate mode_line_noprop_buf if necessary. */
11136
11137 static void
11138 store_mode_line_noprop_char (char c)
11139 {
11140 /* If output position has reached the end of the allocated buffer,
11141 increase the buffer's size. */
11142 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11143 {
11144 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11145 ptrdiff_t size = len;
11146 mode_line_noprop_buf =
11147 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11148 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11149 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11150 }
11151
11152 *mode_line_noprop_ptr++ = c;
11153 }
11154
11155
11156 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11157 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11158 characters that yield more columns than PRECISION; PRECISION <= 0
11159 means copy the whole string. Pad with spaces until FIELD_WIDTH
11160 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11161 pad. Called from display_mode_element when it is used to build a
11162 frame title. */
11163
11164 static int
11165 store_mode_line_noprop (const char *string, int field_width, int precision)
11166 {
11167 const unsigned char *str = (const unsigned char *) string;
11168 int n = 0;
11169 ptrdiff_t dummy, nbytes;
11170
11171 /* Copy at most PRECISION chars from STR. */
11172 nbytes = strlen (string);
11173 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11174 while (nbytes--)
11175 store_mode_line_noprop_char (*str++);
11176
11177 /* Fill up with spaces until FIELD_WIDTH reached. */
11178 while (field_width > 0
11179 && n < field_width)
11180 {
11181 store_mode_line_noprop_char (' ');
11182 ++n;
11183 }
11184
11185 return n;
11186 }
11187
11188 /***********************************************************************
11189 Frame Titles
11190 ***********************************************************************/
11191
11192 #ifdef HAVE_WINDOW_SYSTEM
11193
11194 /* Set the title of FRAME, if it has changed. The title format is
11195 Vicon_title_format if FRAME is iconified, otherwise it is
11196 frame_title_format. */
11197
11198 static void
11199 x_consider_frame_title (Lisp_Object frame)
11200 {
11201 struct frame *f = XFRAME (frame);
11202
11203 if (FRAME_WINDOW_P (f)
11204 || FRAME_MINIBUF_ONLY_P (f)
11205 || f->explicit_name)
11206 {
11207 /* Do we have more than one visible frame on this X display? */
11208 Lisp_Object tail, other_frame, fmt;
11209 ptrdiff_t title_start;
11210 char *title;
11211 ptrdiff_t len;
11212 struct it it;
11213 ptrdiff_t count = SPECPDL_INDEX ();
11214
11215 FOR_EACH_FRAME (tail, other_frame)
11216 {
11217 struct frame *tf = XFRAME (other_frame);
11218
11219 if (tf != f
11220 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11221 && !FRAME_MINIBUF_ONLY_P (tf)
11222 && !EQ (other_frame, tip_frame)
11223 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11224 break;
11225 }
11226
11227 /* Set global variable indicating that multiple frames exist. */
11228 multiple_frames = CONSP (tail);
11229
11230 /* Switch to the buffer of selected window of the frame. Set up
11231 mode_line_target so that display_mode_element will output into
11232 mode_line_noprop_buf; then display the title. */
11233 record_unwind_protect (unwind_format_mode_line,
11234 format_mode_line_unwind_data
11235 (f, current_buffer, selected_window, 0));
11236
11237 Fselect_window (f->selected_window, Qt);
11238 set_buffer_internal_1
11239 (XBUFFER (XWINDOW (f->selected_window)->contents));
11240 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11241
11242 mode_line_target = MODE_LINE_TITLE;
11243 title_start = MODE_LINE_NOPROP_LEN (0);
11244 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11245 NULL, DEFAULT_FACE_ID);
11246 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11247 len = MODE_LINE_NOPROP_LEN (title_start);
11248 title = mode_line_noprop_buf + title_start;
11249 unbind_to (count, Qnil);
11250
11251 /* Set the title only if it's changed. This avoids consing in
11252 the common case where it hasn't. (If it turns out that we've
11253 already wasted too much time by walking through the list with
11254 display_mode_element, then we might need to optimize at a
11255 higher level than this.) */
11256 if (! STRINGP (f->name)
11257 || SBYTES (f->name) != len
11258 || memcmp (title, SDATA (f->name), len) != 0)
11259 x_implicitly_set_name (f, make_string (title, len), Qnil);
11260 }
11261 }
11262
11263 #endif /* not HAVE_WINDOW_SYSTEM */
11264
11265 \f
11266 /***********************************************************************
11267 Menu Bars
11268 ***********************************************************************/
11269
11270
11271 /* Prepare for redisplay by updating menu-bar item lists when
11272 appropriate. This can call eval. */
11273
11274 void
11275 prepare_menu_bars (void)
11276 {
11277 int all_windows;
11278 struct gcpro gcpro1, gcpro2;
11279 struct frame *f;
11280 Lisp_Object tooltip_frame;
11281
11282 #ifdef HAVE_WINDOW_SYSTEM
11283 tooltip_frame = tip_frame;
11284 #else
11285 tooltip_frame = Qnil;
11286 #endif
11287
11288 /* Update all frame titles based on their buffer names, etc. We do
11289 this before the menu bars so that the buffer-menu will show the
11290 up-to-date frame titles. */
11291 #ifdef HAVE_WINDOW_SYSTEM
11292 if (windows_or_buffers_changed || update_mode_lines)
11293 {
11294 Lisp_Object tail, frame;
11295
11296 FOR_EACH_FRAME (tail, frame)
11297 {
11298 f = XFRAME (frame);
11299 if (!EQ (frame, tooltip_frame)
11300 && (FRAME_ICONIFIED_P (f)
11301 || FRAME_VISIBLE_P (f) == 1
11302 /* Exclude TTY frames that are obscured because they
11303 are not the top frame on their console. This is
11304 because x_consider_frame_title actually switches
11305 to the frame, which for TTY frames means it is
11306 marked as garbaged, and will be completely
11307 redrawn on the next redisplay cycle. This causes
11308 TTY frames to be completely redrawn, when there
11309 are more than one of them, even though nothing
11310 should be changed on display. */
11311 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11312 x_consider_frame_title (frame);
11313 }
11314 }
11315 #endif /* HAVE_WINDOW_SYSTEM */
11316
11317 /* Update the menu bar item lists, if appropriate. This has to be
11318 done before any actual redisplay or generation of display lines. */
11319 all_windows = (update_mode_lines
11320 || buffer_shared_and_changed ()
11321 || windows_or_buffers_changed);
11322 if (all_windows)
11323 {
11324 Lisp_Object tail, frame;
11325 ptrdiff_t count = SPECPDL_INDEX ();
11326 /* 1 means that update_menu_bar has run its hooks
11327 so any further calls to update_menu_bar shouldn't do so again. */
11328 int menu_bar_hooks_run = 0;
11329
11330 record_unwind_save_match_data ();
11331
11332 FOR_EACH_FRAME (tail, frame)
11333 {
11334 f = XFRAME (frame);
11335
11336 /* Ignore tooltip frame. */
11337 if (EQ (frame, tooltip_frame))
11338 continue;
11339
11340 /* If a window on this frame changed size, report that to
11341 the user and clear the size-change flag. */
11342 if (FRAME_WINDOW_SIZES_CHANGED (f))
11343 {
11344 Lisp_Object functions;
11345
11346 /* Clear flag first in case we get an error below. */
11347 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11348 functions = Vwindow_size_change_functions;
11349 GCPRO2 (tail, functions);
11350
11351 while (CONSP (functions))
11352 {
11353 if (!EQ (XCAR (functions), Qt))
11354 call1 (XCAR (functions), frame);
11355 functions = XCDR (functions);
11356 }
11357 UNGCPRO;
11358 }
11359
11360 GCPRO1 (tail);
11361 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11362 #ifdef HAVE_WINDOW_SYSTEM
11363 update_tool_bar (f, 0);
11364 #endif
11365 #ifdef HAVE_NS
11366 if (windows_or_buffers_changed
11367 && FRAME_NS_P (f))
11368 ns_set_doc_edited
11369 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11370 #endif
11371 UNGCPRO;
11372 }
11373
11374 unbind_to (count, Qnil);
11375 }
11376 else
11377 {
11378 struct frame *sf = SELECTED_FRAME ();
11379 update_menu_bar (sf, 1, 0);
11380 #ifdef HAVE_WINDOW_SYSTEM
11381 update_tool_bar (sf, 1);
11382 #endif
11383 }
11384 }
11385
11386
11387 /* Update the menu bar item list for frame F. This has to be done
11388 before we start to fill in any display lines, because it can call
11389 eval.
11390
11391 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11392
11393 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11394 already ran the menu bar hooks for this redisplay, so there
11395 is no need to run them again. The return value is the
11396 updated value of this flag, to pass to the next call. */
11397
11398 static int
11399 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11400 {
11401 Lisp_Object window;
11402 register struct window *w;
11403
11404 /* If called recursively during a menu update, do nothing. This can
11405 happen when, for instance, an activate-menubar-hook causes a
11406 redisplay. */
11407 if (inhibit_menubar_update)
11408 return hooks_run;
11409
11410 window = FRAME_SELECTED_WINDOW (f);
11411 w = XWINDOW (window);
11412
11413 if (FRAME_WINDOW_P (f)
11414 ?
11415 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11416 || defined (HAVE_NS) || defined (USE_GTK)
11417 FRAME_EXTERNAL_MENU_BAR (f)
11418 #else
11419 FRAME_MENU_BAR_LINES (f) > 0
11420 #endif
11421 : FRAME_MENU_BAR_LINES (f) > 0)
11422 {
11423 /* If the user has switched buffers or windows, we need to
11424 recompute to reflect the new bindings. But we'll
11425 recompute when update_mode_lines is set too; that means
11426 that people can use force-mode-line-update to request
11427 that the menu bar be recomputed. The adverse effect on
11428 the rest of the redisplay algorithm is about the same as
11429 windows_or_buffers_changed anyway. */
11430 if (windows_or_buffers_changed
11431 /* This used to test w->update_mode_line, but we believe
11432 there is no need to recompute the menu in that case. */
11433 || update_mode_lines
11434 || window_buffer_changed (w))
11435 {
11436 struct buffer *prev = current_buffer;
11437 ptrdiff_t count = SPECPDL_INDEX ();
11438
11439 specbind (Qinhibit_menubar_update, Qt);
11440
11441 set_buffer_internal_1 (XBUFFER (w->contents));
11442 if (save_match_data)
11443 record_unwind_save_match_data ();
11444 if (NILP (Voverriding_local_map_menu_flag))
11445 {
11446 specbind (Qoverriding_terminal_local_map, Qnil);
11447 specbind (Qoverriding_local_map, Qnil);
11448 }
11449
11450 if (!hooks_run)
11451 {
11452 /* Run the Lucid hook. */
11453 safe_run_hooks (Qactivate_menubar_hook);
11454
11455 /* If it has changed current-menubar from previous value,
11456 really recompute the menu-bar from the value. */
11457 if (! NILP (Vlucid_menu_bar_dirty_flag))
11458 call0 (Qrecompute_lucid_menubar);
11459
11460 safe_run_hooks (Qmenu_bar_update_hook);
11461
11462 hooks_run = 1;
11463 }
11464
11465 XSETFRAME (Vmenu_updating_frame, f);
11466 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11467
11468 /* Redisplay the menu bar in case we changed it. */
11469 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11470 || defined (HAVE_NS) || defined (USE_GTK)
11471 if (FRAME_WINDOW_P (f))
11472 {
11473 #if defined (HAVE_NS)
11474 /* All frames on Mac OS share the same menubar. So only
11475 the selected frame should be allowed to set it. */
11476 if (f == SELECTED_FRAME ())
11477 #endif
11478 set_frame_menubar (f, 0, 0);
11479 }
11480 else
11481 /* On a terminal screen, the menu bar is an ordinary screen
11482 line, and this makes it get updated. */
11483 w->update_mode_line = 1;
11484 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11485 /* In the non-toolkit version, the menu bar is an ordinary screen
11486 line, and this makes it get updated. */
11487 w->update_mode_line = 1;
11488 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11489
11490 unbind_to (count, Qnil);
11491 set_buffer_internal_1 (prev);
11492 }
11493 }
11494
11495 return hooks_run;
11496 }
11497
11498
11499 \f
11500 /***********************************************************************
11501 Output Cursor
11502 ***********************************************************************/
11503
11504 #ifdef HAVE_WINDOW_SYSTEM
11505
11506 /* EXPORT:
11507 Nominal cursor position -- where to draw output.
11508 HPOS and VPOS are window relative glyph matrix coordinates.
11509 X and Y are window relative pixel coordinates. */
11510
11511 struct cursor_pos output_cursor;
11512
11513
11514 /* EXPORT:
11515 Set the global variable output_cursor to CURSOR. All cursor
11516 positions are relative to currently updated window. */
11517
11518 void
11519 set_output_cursor (struct cursor_pos *cursor)
11520 {
11521 output_cursor.hpos = cursor->hpos;
11522 output_cursor.vpos = cursor->vpos;
11523 output_cursor.x = cursor->x;
11524 output_cursor.y = cursor->y;
11525 }
11526
11527
11528 /* EXPORT for RIF:
11529 Set a nominal cursor position.
11530
11531 HPOS and VPOS are column/row positions in a window glyph matrix.
11532 X and Y are window text area relative pixel positions.
11533
11534 This is always done during window update, so the position is the
11535 future output cursor position for currently updated window W.
11536 NOTE: W is used only to check whether this function is called
11537 in a consistent manner via the redisplay interface. */
11538
11539 void
11540 x_cursor_to (struct window *w, int vpos, int hpos, int y, int x)
11541 {
11542 eassert (w);
11543
11544 /* Set the output cursor. */
11545 output_cursor.hpos = hpos;
11546 output_cursor.vpos = vpos;
11547 output_cursor.x = x;
11548 output_cursor.y = y;
11549 }
11550
11551 #endif /* HAVE_WINDOW_SYSTEM */
11552
11553 \f
11554 /***********************************************************************
11555 Tool-bars
11556 ***********************************************************************/
11557
11558 #ifdef HAVE_WINDOW_SYSTEM
11559
11560 /* Where the mouse was last time we reported a mouse event. */
11561
11562 struct frame *last_mouse_frame;
11563
11564 /* Tool-bar item index of the item on which a mouse button was pressed
11565 or -1. */
11566
11567 int last_tool_bar_item;
11568
11569 /* Select `frame' temporarily without running all the code in
11570 do_switch_frame.
11571 FIXME: Maybe do_switch_frame should be trimmed down similarly
11572 when `norecord' is set. */
11573 static void
11574 fast_set_selected_frame (Lisp_Object frame)
11575 {
11576 if (!EQ (selected_frame, frame))
11577 {
11578 selected_frame = frame;
11579 selected_window = XFRAME (frame)->selected_window;
11580 }
11581 }
11582
11583 /* Update the tool-bar item list for frame F. This has to be done
11584 before we start to fill in any display lines. Called from
11585 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11586 and restore it here. */
11587
11588 static void
11589 update_tool_bar (struct frame *f, int save_match_data)
11590 {
11591 #if defined (USE_GTK) || defined (HAVE_NS)
11592 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11593 #else
11594 int do_update = WINDOWP (f->tool_bar_window)
11595 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11596 #endif
11597
11598 if (do_update)
11599 {
11600 Lisp_Object window;
11601 struct window *w;
11602
11603 window = FRAME_SELECTED_WINDOW (f);
11604 w = XWINDOW (window);
11605
11606 /* If the user has switched buffers or windows, we need to
11607 recompute to reflect the new bindings. But we'll
11608 recompute when update_mode_lines is set too; that means
11609 that people can use force-mode-line-update to request
11610 that the menu bar be recomputed. The adverse effect on
11611 the rest of the redisplay algorithm is about the same as
11612 windows_or_buffers_changed anyway. */
11613 if (windows_or_buffers_changed
11614 || w->update_mode_line
11615 || update_mode_lines
11616 || window_buffer_changed (w))
11617 {
11618 struct buffer *prev = current_buffer;
11619 ptrdiff_t count = SPECPDL_INDEX ();
11620 Lisp_Object frame, new_tool_bar;
11621 int new_n_tool_bar;
11622 struct gcpro gcpro1;
11623
11624 /* Set current_buffer to the buffer of the selected
11625 window of the frame, so that we get the right local
11626 keymaps. */
11627 set_buffer_internal_1 (XBUFFER (w->contents));
11628
11629 /* Save match data, if we must. */
11630 if (save_match_data)
11631 record_unwind_save_match_data ();
11632
11633 /* Make sure that we don't accidentally use bogus keymaps. */
11634 if (NILP (Voverriding_local_map_menu_flag))
11635 {
11636 specbind (Qoverriding_terminal_local_map, Qnil);
11637 specbind (Qoverriding_local_map, Qnil);
11638 }
11639
11640 GCPRO1 (new_tool_bar);
11641
11642 /* We must temporarily set the selected frame to this frame
11643 before calling tool_bar_items, because the calculation of
11644 the tool-bar keymap uses the selected frame (see
11645 `tool-bar-make-keymap' in tool-bar.el). */
11646 eassert (EQ (selected_window,
11647 /* Since we only explicitly preserve selected_frame,
11648 check that selected_window would be redundant. */
11649 XFRAME (selected_frame)->selected_window));
11650 record_unwind_protect (fast_set_selected_frame, selected_frame);
11651 XSETFRAME (frame, f);
11652 fast_set_selected_frame (frame);
11653
11654 /* Build desired tool-bar items from keymaps. */
11655 new_tool_bar
11656 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11657 &new_n_tool_bar);
11658
11659 /* Redisplay the tool-bar if we changed it. */
11660 if (new_n_tool_bar != f->n_tool_bar_items
11661 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11662 {
11663 /* Redisplay that happens asynchronously due to an expose event
11664 may access f->tool_bar_items. Make sure we update both
11665 variables within BLOCK_INPUT so no such event interrupts. */
11666 block_input ();
11667 fset_tool_bar_items (f, new_tool_bar);
11668 f->n_tool_bar_items = new_n_tool_bar;
11669 w->update_mode_line = 1;
11670 unblock_input ();
11671 }
11672
11673 UNGCPRO;
11674
11675 unbind_to (count, Qnil);
11676 set_buffer_internal_1 (prev);
11677 }
11678 }
11679 }
11680
11681
11682 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11683 F's desired tool-bar contents. F->tool_bar_items must have
11684 been set up previously by calling prepare_menu_bars. */
11685
11686 static void
11687 build_desired_tool_bar_string (struct frame *f)
11688 {
11689 int i, size, size_needed;
11690 struct gcpro gcpro1, gcpro2, gcpro3;
11691 Lisp_Object image, plist, props;
11692
11693 image = plist = props = Qnil;
11694 GCPRO3 (image, plist, props);
11695
11696 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11697 Otherwise, make a new string. */
11698
11699 /* The size of the string we might be able to reuse. */
11700 size = (STRINGP (f->desired_tool_bar_string)
11701 ? SCHARS (f->desired_tool_bar_string)
11702 : 0);
11703
11704 /* We need one space in the string for each image. */
11705 size_needed = f->n_tool_bar_items;
11706
11707 /* Reuse f->desired_tool_bar_string, if possible. */
11708 if (size < size_needed || NILP (f->desired_tool_bar_string))
11709 fset_desired_tool_bar_string
11710 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11711 else
11712 {
11713 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11714 Fremove_text_properties (make_number (0), make_number (size),
11715 props, f->desired_tool_bar_string);
11716 }
11717
11718 /* Put a `display' property on the string for the images to display,
11719 put a `menu_item' property on tool-bar items with a value that
11720 is the index of the item in F's tool-bar item vector. */
11721 for (i = 0; i < f->n_tool_bar_items; ++i)
11722 {
11723 #define PROP(IDX) \
11724 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11725
11726 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11727 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11728 int hmargin, vmargin, relief, idx, end;
11729
11730 /* If image is a vector, choose the image according to the
11731 button state. */
11732 image = PROP (TOOL_BAR_ITEM_IMAGES);
11733 if (VECTORP (image))
11734 {
11735 if (enabled_p)
11736 idx = (selected_p
11737 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11738 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11739 else
11740 idx = (selected_p
11741 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11742 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11743
11744 eassert (ASIZE (image) >= idx);
11745 image = AREF (image, idx);
11746 }
11747 else
11748 idx = -1;
11749
11750 /* Ignore invalid image specifications. */
11751 if (!valid_image_p (image))
11752 continue;
11753
11754 /* Display the tool-bar button pressed, or depressed. */
11755 plist = Fcopy_sequence (XCDR (image));
11756
11757 /* Compute margin and relief to draw. */
11758 relief = (tool_bar_button_relief >= 0
11759 ? tool_bar_button_relief
11760 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11761 hmargin = vmargin = relief;
11762
11763 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11764 INT_MAX - max (hmargin, vmargin)))
11765 {
11766 hmargin += XFASTINT (Vtool_bar_button_margin);
11767 vmargin += XFASTINT (Vtool_bar_button_margin);
11768 }
11769 else if (CONSP (Vtool_bar_button_margin))
11770 {
11771 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11772 INT_MAX - hmargin))
11773 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11774
11775 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11776 INT_MAX - vmargin))
11777 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11778 }
11779
11780 if (auto_raise_tool_bar_buttons_p)
11781 {
11782 /* Add a `:relief' property to the image spec if the item is
11783 selected. */
11784 if (selected_p)
11785 {
11786 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11787 hmargin -= relief;
11788 vmargin -= relief;
11789 }
11790 }
11791 else
11792 {
11793 /* If image is selected, display it pressed, i.e. with a
11794 negative relief. If it's not selected, display it with a
11795 raised relief. */
11796 plist = Fplist_put (plist, QCrelief,
11797 (selected_p
11798 ? make_number (-relief)
11799 : make_number (relief)));
11800 hmargin -= relief;
11801 vmargin -= relief;
11802 }
11803
11804 /* Put a margin around the image. */
11805 if (hmargin || vmargin)
11806 {
11807 if (hmargin == vmargin)
11808 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11809 else
11810 plist = Fplist_put (plist, QCmargin,
11811 Fcons (make_number (hmargin),
11812 make_number (vmargin)));
11813 }
11814
11815 /* If button is not enabled, and we don't have special images
11816 for the disabled state, make the image appear disabled by
11817 applying an appropriate algorithm to it. */
11818 if (!enabled_p && idx < 0)
11819 plist = Fplist_put (plist, QCconversion, Qdisabled);
11820
11821 /* Put a `display' text property on the string for the image to
11822 display. Put a `menu-item' property on the string that gives
11823 the start of this item's properties in the tool-bar items
11824 vector. */
11825 image = Fcons (Qimage, plist);
11826 props = list4 (Qdisplay, image,
11827 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11828
11829 /* Let the last image hide all remaining spaces in the tool bar
11830 string. The string can be longer than needed when we reuse a
11831 previous string. */
11832 if (i + 1 == f->n_tool_bar_items)
11833 end = SCHARS (f->desired_tool_bar_string);
11834 else
11835 end = i + 1;
11836 Fadd_text_properties (make_number (i), make_number (end),
11837 props, f->desired_tool_bar_string);
11838 #undef PROP
11839 }
11840
11841 UNGCPRO;
11842 }
11843
11844
11845 /* Display one line of the tool-bar of frame IT->f.
11846
11847 HEIGHT specifies the desired height of the tool-bar line.
11848 If the actual height of the glyph row is less than HEIGHT, the
11849 row's height is increased to HEIGHT, and the icons are centered
11850 vertically in the new height.
11851
11852 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11853 count a final empty row in case the tool-bar width exactly matches
11854 the window width.
11855 */
11856
11857 static void
11858 display_tool_bar_line (struct it *it, int height)
11859 {
11860 struct glyph_row *row = it->glyph_row;
11861 int max_x = it->last_visible_x;
11862 struct glyph *last;
11863
11864 prepare_desired_row (row);
11865 row->y = it->current_y;
11866
11867 /* Note that this isn't made use of if the face hasn't a box,
11868 so there's no need to check the face here. */
11869 it->start_of_box_run_p = 1;
11870
11871 while (it->current_x < max_x)
11872 {
11873 int x, n_glyphs_before, i, nglyphs;
11874 struct it it_before;
11875
11876 /* Get the next display element. */
11877 if (!get_next_display_element (it))
11878 {
11879 /* Don't count empty row if we are counting needed tool-bar lines. */
11880 if (height < 0 && !it->hpos)
11881 return;
11882 break;
11883 }
11884
11885 /* Produce glyphs. */
11886 n_glyphs_before = row->used[TEXT_AREA];
11887 it_before = *it;
11888
11889 PRODUCE_GLYPHS (it);
11890
11891 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11892 i = 0;
11893 x = it_before.current_x;
11894 while (i < nglyphs)
11895 {
11896 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11897
11898 if (x + glyph->pixel_width > max_x)
11899 {
11900 /* Glyph doesn't fit on line. Backtrack. */
11901 row->used[TEXT_AREA] = n_glyphs_before;
11902 *it = it_before;
11903 /* If this is the only glyph on this line, it will never fit on the
11904 tool-bar, so skip it. But ensure there is at least one glyph,
11905 so we don't accidentally disable the tool-bar. */
11906 if (n_glyphs_before == 0
11907 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11908 break;
11909 goto out;
11910 }
11911
11912 ++it->hpos;
11913 x += glyph->pixel_width;
11914 ++i;
11915 }
11916
11917 /* Stop at line end. */
11918 if (ITERATOR_AT_END_OF_LINE_P (it))
11919 break;
11920
11921 set_iterator_to_next (it, 1);
11922 }
11923
11924 out:;
11925
11926 row->displays_text_p = row->used[TEXT_AREA] != 0;
11927
11928 /* Use default face for the border below the tool bar.
11929
11930 FIXME: When auto-resize-tool-bars is grow-only, there is
11931 no additional border below the possibly empty tool-bar lines.
11932 So to make the extra empty lines look "normal", we have to
11933 use the tool-bar face for the border too. */
11934 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11935 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11936 it->face_id = DEFAULT_FACE_ID;
11937
11938 extend_face_to_end_of_line (it);
11939 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11940 last->right_box_line_p = 1;
11941 if (last == row->glyphs[TEXT_AREA])
11942 last->left_box_line_p = 1;
11943
11944 /* Make line the desired height and center it vertically. */
11945 if ((height -= it->max_ascent + it->max_descent) > 0)
11946 {
11947 /* Don't add more than one line height. */
11948 height %= FRAME_LINE_HEIGHT (it->f);
11949 it->max_ascent += height / 2;
11950 it->max_descent += (height + 1) / 2;
11951 }
11952
11953 compute_line_metrics (it);
11954
11955 /* If line is empty, make it occupy the rest of the tool-bar. */
11956 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11957 {
11958 row->height = row->phys_height = it->last_visible_y - row->y;
11959 row->visible_height = row->height;
11960 row->ascent = row->phys_ascent = 0;
11961 row->extra_line_spacing = 0;
11962 }
11963
11964 row->full_width_p = 1;
11965 row->continued_p = 0;
11966 row->truncated_on_left_p = 0;
11967 row->truncated_on_right_p = 0;
11968
11969 it->current_x = it->hpos = 0;
11970 it->current_y += row->height;
11971 ++it->vpos;
11972 ++it->glyph_row;
11973 }
11974
11975
11976 /* Max tool-bar height. */
11977
11978 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11979 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11980
11981 /* Value is the number of screen lines needed to make all tool-bar
11982 items of frame F visible. The number of actual rows needed is
11983 returned in *N_ROWS if non-NULL. */
11984
11985 static int
11986 tool_bar_lines_needed (struct frame *f, int *n_rows)
11987 {
11988 struct window *w = XWINDOW (f->tool_bar_window);
11989 struct it it;
11990 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11991 the desired matrix, so use (unused) mode-line row as temporary row to
11992 avoid destroying the first tool-bar row. */
11993 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11994
11995 /* Initialize an iterator for iteration over
11996 F->desired_tool_bar_string in the tool-bar window of frame F. */
11997 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11998 it.first_visible_x = 0;
11999 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12000 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12001 it.paragraph_embedding = L2R;
12002
12003 while (!ITERATOR_AT_END_P (&it))
12004 {
12005 clear_glyph_row (temp_row);
12006 it.glyph_row = temp_row;
12007 display_tool_bar_line (&it, -1);
12008 }
12009 clear_glyph_row (temp_row);
12010
12011 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12012 if (n_rows)
12013 *n_rows = it.vpos > 0 ? it.vpos : -1;
12014
12015 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12016 }
12017
12018
12019 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
12020 0, 1, 0,
12021 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12022 If FRAME is nil or omitted, use the selected frame. */)
12023 (Lisp_Object frame)
12024 {
12025 struct frame *f = decode_any_frame (frame);
12026 struct window *w;
12027 int nlines = 0;
12028
12029 if (WINDOWP (f->tool_bar_window)
12030 && (w = XWINDOW (f->tool_bar_window),
12031 WINDOW_TOTAL_LINES (w) > 0))
12032 {
12033 update_tool_bar (f, 1);
12034 if (f->n_tool_bar_items)
12035 {
12036 build_desired_tool_bar_string (f);
12037 nlines = tool_bar_lines_needed (f, NULL);
12038 }
12039 }
12040
12041 return make_number (nlines);
12042 }
12043
12044
12045 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12046 height should be changed. */
12047
12048 static int
12049 redisplay_tool_bar (struct frame *f)
12050 {
12051 struct window *w;
12052 struct it it;
12053 struct glyph_row *row;
12054
12055 #if defined (USE_GTK) || defined (HAVE_NS)
12056 if (FRAME_EXTERNAL_TOOL_BAR (f))
12057 update_frame_tool_bar (f);
12058 return 0;
12059 #endif
12060
12061 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12062 do anything. This means you must start with tool-bar-lines
12063 non-zero to get the auto-sizing effect. Or in other words, you
12064 can turn off tool-bars by specifying tool-bar-lines zero. */
12065 if (!WINDOWP (f->tool_bar_window)
12066 || (w = XWINDOW (f->tool_bar_window),
12067 WINDOW_TOTAL_LINES (w) == 0))
12068 return 0;
12069
12070 /* Set up an iterator for the tool-bar window. */
12071 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12072 it.first_visible_x = 0;
12073 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12074 row = it.glyph_row;
12075
12076 /* Build a string that represents the contents of the tool-bar. */
12077 build_desired_tool_bar_string (f);
12078 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12079 /* FIXME: This should be controlled by a user option. But it
12080 doesn't make sense to have an R2L tool bar if the menu bar cannot
12081 be drawn also R2L, and making the menu bar R2L is tricky due
12082 toolkit-specific code that implements it. If an R2L tool bar is
12083 ever supported, display_tool_bar_line should also be augmented to
12084 call unproduce_glyphs like display_line and display_string
12085 do. */
12086 it.paragraph_embedding = L2R;
12087
12088 if (f->n_tool_bar_rows == 0)
12089 {
12090 int nlines;
12091
12092 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12093 nlines != WINDOW_TOTAL_LINES (w)))
12094 {
12095 Lisp_Object frame;
12096 int old_height = WINDOW_TOTAL_LINES (w);
12097
12098 XSETFRAME (frame, f);
12099 Fmodify_frame_parameters (frame,
12100 list1 (Fcons (Qtool_bar_lines,
12101 make_number (nlines))));
12102 if (WINDOW_TOTAL_LINES (w) != old_height)
12103 {
12104 clear_glyph_matrix (w->desired_matrix);
12105 fonts_changed_p = 1;
12106 return 1;
12107 }
12108 }
12109 }
12110
12111 /* Display as many lines as needed to display all tool-bar items. */
12112
12113 if (f->n_tool_bar_rows > 0)
12114 {
12115 int border, rows, height, extra;
12116
12117 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12118 border = XINT (Vtool_bar_border);
12119 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12120 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12121 else if (EQ (Vtool_bar_border, Qborder_width))
12122 border = f->border_width;
12123 else
12124 border = 0;
12125 if (border < 0)
12126 border = 0;
12127
12128 rows = f->n_tool_bar_rows;
12129 height = max (1, (it.last_visible_y - border) / rows);
12130 extra = it.last_visible_y - border - height * rows;
12131
12132 while (it.current_y < it.last_visible_y)
12133 {
12134 int h = 0;
12135 if (extra > 0 && rows-- > 0)
12136 {
12137 h = (extra + rows - 1) / rows;
12138 extra -= h;
12139 }
12140 display_tool_bar_line (&it, height + h);
12141 }
12142 }
12143 else
12144 {
12145 while (it.current_y < it.last_visible_y)
12146 display_tool_bar_line (&it, 0);
12147 }
12148
12149 /* It doesn't make much sense to try scrolling in the tool-bar
12150 window, so don't do it. */
12151 w->desired_matrix->no_scrolling_p = 1;
12152 w->must_be_updated_p = 1;
12153
12154 if (!NILP (Vauto_resize_tool_bars))
12155 {
12156 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12157 int change_height_p = 0;
12158
12159 /* If we couldn't display everything, change the tool-bar's
12160 height if there is room for more. */
12161 if (IT_STRING_CHARPOS (it) < it.end_charpos
12162 && it.current_y < max_tool_bar_height)
12163 change_height_p = 1;
12164
12165 row = it.glyph_row - 1;
12166
12167 /* If there are blank lines at the end, except for a partially
12168 visible blank line at the end that is smaller than
12169 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12170 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12171 && row->height >= FRAME_LINE_HEIGHT (f))
12172 change_height_p = 1;
12173
12174 /* If row displays tool-bar items, but is partially visible,
12175 change the tool-bar's height. */
12176 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12177 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12178 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12179 change_height_p = 1;
12180
12181 /* Resize windows as needed by changing the `tool-bar-lines'
12182 frame parameter. */
12183 if (change_height_p)
12184 {
12185 Lisp_Object frame;
12186 int old_height = WINDOW_TOTAL_LINES (w);
12187 int nrows;
12188 int nlines = tool_bar_lines_needed (f, &nrows);
12189
12190 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12191 && !f->minimize_tool_bar_window_p)
12192 ? (nlines > old_height)
12193 : (nlines != old_height));
12194 f->minimize_tool_bar_window_p = 0;
12195
12196 if (change_height_p)
12197 {
12198 XSETFRAME (frame, f);
12199 Fmodify_frame_parameters (frame,
12200 list1 (Fcons (Qtool_bar_lines,
12201 make_number (nlines))));
12202 if (WINDOW_TOTAL_LINES (w) != old_height)
12203 {
12204 clear_glyph_matrix (w->desired_matrix);
12205 f->n_tool_bar_rows = nrows;
12206 fonts_changed_p = 1;
12207 return 1;
12208 }
12209 }
12210 }
12211 }
12212
12213 f->minimize_tool_bar_window_p = 0;
12214 return 0;
12215 }
12216
12217
12218 /* Get information about the tool-bar item which is displayed in GLYPH
12219 on frame F. Return in *PROP_IDX the index where tool-bar item
12220 properties start in F->tool_bar_items. Value is zero if
12221 GLYPH doesn't display a tool-bar item. */
12222
12223 static int
12224 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12225 {
12226 Lisp_Object prop;
12227 int success_p;
12228 int charpos;
12229
12230 /* This function can be called asynchronously, which means we must
12231 exclude any possibility that Fget_text_property signals an
12232 error. */
12233 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12234 charpos = max (0, charpos);
12235
12236 /* Get the text property `menu-item' at pos. The value of that
12237 property is the start index of this item's properties in
12238 F->tool_bar_items. */
12239 prop = Fget_text_property (make_number (charpos),
12240 Qmenu_item, f->current_tool_bar_string);
12241 if (INTEGERP (prop))
12242 {
12243 *prop_idx = XINT (prop);
12244 success_p = 1;
12245 }
12246 else
12247 success_p = 0;
12248
12249 return success_p;
12250 }
12251
12252 \f
12253 /* Get information about the tool-bar item at position X/Y on frame F.
12254 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12255 the current matrix of the tool-bar window of F, or NULL if not
12256 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12257 item in F->tool_bar_items. Value is
12258
12259 -1 if X/Y is not on a tool-bar item
12260 0 if X/Y is on the same item that was highlighted before.
12261 1 otherwise. */
12262
12263 static int
12264 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12265 int *hpos, int *vpos, int *prop_idx)
12266 {
12267 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12268 struct window *w = XWINDOW (f->tool_bar_window);
12269 int area;
12270
12271 /* Find the glyph under X/Y. */
12272 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12273 if (*glyph == NULL)
12274 return -1;
12275
12276 /* Get the start of this tool-bar item's properties in
12277 f->tool_bar_items. */
12278 if (!tool_bar_item_info (f, *glyph, prop_idx))
12279 return -1;
12280
12281 /* Is mouse on the highlighted item? */
12282 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12283 && *vpos >= hlinfo->mouse_face_beg_row
12284 && *vpos <= hlinfo->mouse_face_end_row
12285 && (*vpos > hlinfo->mouse_face_beg_row
12286 || *hpos >= hlinfo->mouse_face_beg_col)
12287 && (*vpos < hlinfo->mouse_face_end_row
12288 || *hpos < hlinfo->mouse_face_end_col
12289 || hlinfo->mouse_face_past_end))
12290 return 0;
12291
12292 return 1;
12293 }
12294
12295
12296 /* EXPORT:
12297 Handle mouse button event on the tool-bar of frame F, at
12298 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12299 0 for button release. MODIFIERS is event modifiers for button
12300 release. */
12301
12302 void
12303 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12304 int modifiers)
12305 {
12306 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12307 struct window *w = XWINDOW (f->tool_bar_window);
12308 int hpos, vpos, prop_idx;
12309 struct glyph *glyph;
12310 Lisp_Object enabled_p;
12311 int ts;
12312
12313 /* If not on the highlighted tool-bar item, and mouse-highlight is
12314 non-nil, return. This is so we generate the tool-bar button
12315 click only when the mouse button is released on the same item as
12316 where it was pressed. However, when mouse-highlight is disabled,
12317 generate the click when the button is released regardless of the
12318 highlight, since tool-bar items are not highlighted in that
12319 case. */
12320 frame_to_window_pixel_xy (w, &x, &y);
12321 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12322 if (ts == -1
12323 || (ts != 0 && !NILP (Vmouse_highlight)))
12324 return;
12325
12326 /* When mouse-highlight is off, generate the click for the item
12327 where the button was pressed, disregarding where it was
12328 released. */
12329 if (NILP (Vmouse_highlight) && !down_p)
12330 prop_idx = last_tool_bar_item;
12331
12332 /* If item is disabled, do nothing. */
12333 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12334 if (NILP (enabled_p))
12335 return;
12336
12337 if (down_p)
12338 {
12339 /* Show item in pressed state. */
12340 if (!NILP (Vmouse_highlight))
12341 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12342 last_tool_bar_item = prop_idx;
12343 }
12344 else
12345 {
12346 Lisp_Object key, frame;
12347 struct input_event event;
12348 EVENT_INIT (event);
12349
12350 /* Show item in released state. */
12351 if (!NILP (Vmouse_highlight))
12352 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12353
12354 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12355
12356 XSETFRAME (frame, f);
12357 event.kind = TOOL_BAR_EVENT;
12358 event.frame_or_window = frame;
12359 event.arg = frame;
12360 kbd_buffer_store_event (&event);
12361
12362 event.kind = TOOL_BAR_EVENT;
12363 event.frame_or_window = frame;
12364 event.arg = key;
12365 event.modifiers = modifiers;
12366 kbd_buffer_store_event (&event);
12367 last_tool_bar_item = -1;
12368 }
12369 }
12370
12371
12372 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12373 tool-bar window-relative coordinates X/Y. Called from
12374 note_mouse_highlight. */
12375
12376 static void
12377 note_tool_bar_highlight (struct frame *f, int x, int y)
12378 {
12379 Lisp_Object window = f->tool_bar_window;
12380 struct window *w = XWINDOW (window);
12381 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12382 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12383 int hpos, vpos;
12384 struct glyph *glyph;
12385 struct glyph_row *row;
12386 int i;
12387 Lisp_Object enabled_p;
12388 int prop_idx;
12389 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12390 int mouse_down_p, rc;
12391
12392 /* Function note_mouse_highlight is called with negative X/Y
12393 values when mouse moves outside of the frame. */
12394 if (x <= 0 || y <= 0)
12395 {
12396 clear_mouse_face (hlinfo);
12397 return;
12398 }
12399
12400 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12401 if (rc < 0)
12402 {
12403 /* Not on tool-bar item. */
12404 clear_mouse_face (hlinfo);
12405 return;
12406 }
12407 else if (rc == 0)
12408 /* On same tool-bar item as before. */
12409 goto set_help_echo;
12410
12411 clear_mouse_face (hlinfo);
12412
12413 /* Mouse is down, but on different tool-bar item? */
12414 mouse_down_p = (dpyinfo->grabbed
12415 && f == last_mouse_frame
12416 && FRAME_LIVE_P (f));
12417 if (mouse_down_p
12418 && last_tool_bar_item != prop_idx)
12419 return;
12420
12421 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12422
12423 /* If tool-bar item is not enabled, don't highlight it. */
12424 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12425 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12426 {
12427 /* Compute the x-position of the glyph. In front and past the
12428 image is a space. We include this in the highlighted area. */
12429 row = MATRIX_ROW (w->current_matrix, vpos);
12430 for (i = x = 0; i < hpos; ++i)
12431 x += row->glyphs[TEXT_AREA][i].pixel_width;
12432
12433 /* Record this as the current active region. */
12434 hlinfo->mouse_face_beg_col = hpos;
12435 hlinfo->mouse_face_beg_row = vpos;
12436 hlinfo->mouse_face_beg_x = x;
12437 hlinfo->mouse_face_beg_y = row->y;
12438 hlinfo->mouse_face_past_end = 0;
12439
12440 hlinfo->mouse_face_end_col = hpos + 1;
12441 hlinfo->mouse_face_end_row = vpos;
12442 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12443 hlinfo->mouse_face_end_y = row->y;
12444 hlinfo->mouse_face_window = window;
12445 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12446
12447 /* Display it as active. */
12448 show_mouse_face (hlinfo, draw);
12449 }
12450
12451 set_help_echo:
12452
12453 /* Set help_echo_string to a help string to display for this tool-bar item.
12454 XTread_socket does the rest. */
12455 help_echo_object = help_echo_window = Qnil;
12456 help_echo_pos = -1;
12457 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12458 if (NILP (help_echo_string))
12459 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12460 }
12461
12462 #endif /* HAVE_WINDOW_SYSTEM */
12463
12464
12465 \f
12466 /************************************************************************
12467 Horizontal scrolling
12468 ************************************************************************/
12469
12470 static int hscroll_window_tree (Lisp_Object);
12471 static int hscroll_windows (Lisp_Object);
12472
12473 /* For all leaf windows in the window tree rooted at WINDOW, set their
12474 hscroll value so that PT is (i) visible in the window, and (ii) so
12475 that it is not within a certain margin at the window's left and
12476 right border. Value is non-zero if any window's hscroll has been
12477 changed. */
12478
12479 static int
12480 hscroll_window_tree (Lisp_Object window)
12481 {
12482 int hscrolled_p = 0;
12483 int hscroll_relative_p = FLOATP (Vhscroll_step);
12484 int hscroll_step_abs = 0;
12485 double hscroll_step_rel = 0;
12486
12487 if (hscroll_relative_p)
12488 {
12489 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12490 if (hscroll_step_rel < 0)
12491 {
12492 hscroll_relative_p = 0;
12493 hscroll_step_abs = 0;
12494 }
12495 }
12496 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12497 {
12498 hscroll_step_abs = XINT (Vhscroll_step);
12499 if (hscroll_step_abs < 0)
12500 hscroll_step_abs = 0;
12501 }
12502 else
12503 hscroll_step_abs = 0;
12504
12505 while (WINDOWP (window))
12506 {
12507 struct window *w = XWINDOW (window);
12508
12509 if (WINDOWP (w->contents))
12510 hscrolled_p |= hscroll_window_tree (w->contents);
12511 else if (w->cursor.vpos >= 0)
12512 {
12513 int h_margin;
12514 int text_area_width;
12515 struct glyph_row *current_cursor_row
12516 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12517 struct glyph_row *desired_cursor_row
12518 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12519 struct glyph_row *cursor_row
12520 = (desired_cursor_row->enabled_p
12521 ? desired_cursor_row
12522 : current_cursor_row);
12523 int row_r2l_p = cursor_row->reversed_p;
12524
12525 text_area_width = window_box_width (w, TEXT_AREA);
12526
12527 /* Scroll when cursor is inside this scroll margin. */
12528 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12529
12530 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12531 /* For left-to-right rows, hscroll when cursor is either
12532 (i) inside the right hscroll margin, or (ii) if it is
12533 inside the left margin and the window is already
12534 hscrolled. */
12535 && ((!row_r2l_p
12536 && ((w->hscroll
12537 && w->cursor.x <= h_margin)
12538 || (cursor_row->enabled_p
12539 && cursor_row->truncated_on_right_p
12540 && (w->cursor.x >= text_area_width - h_margin))))
12541 /* For right-to-left rows, the logic is similar,
12542 except that rules for scrolling to left and right
12543 are reversed. E.g., if cursor.x <= h_margin, we
12544 need to hscroll "to the right" unconditionally,
12545 and that will scroll the screen to the left so as
12546 to reveal the next portion of the row. */
12547 || (row_r2l_p
12548 && ((cursor_row->enabled_p
12549 /* FIXME: It is confusing to set the
12550 truncated_on_right_p flag when R2L rows
12551 are actually truncated on the left. */
12552 && cursor_row->truncated_on_right_p
12553 && w->cursor.x <= h_margin)
12554 || (w->hscroll
12555 && (w->cursor.x >= text_area_width - h_margin))))))
12556 {
12557 struct it it;
12558 ptrdiff_t hscroll;
12559 struct buffer *saved_current_buffer;
12560 ptrdiff_t pt;
12561 int wanted_x;
12562
12563 /* Find point in a display of infinite width. */
12564 saved_current_buffer = current_buffer;
12565 current_buffer = XBUFFER (w->contents);
12566
12567 if (w == XWINDOW (selected_window))
12568 pt = PT;
12569 else
12570 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12571
12572 /* Move iterator to pt starting at cursor_row->start in
12573 a line with infinite width. */
12574 init_to_row_start (&it, w, cursor_row);
12575 it.last_visible_x = INFINITY;
12576 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12577 current_buffer = saved_current_buffer;
12578
12579 /* Position cursor in window. */
12580 if (!hscroll_relative_p && hscroll_step_abs == 0)
12581 hscroll = max (0, (it.current_x
12582 - (ITERATOR_AT_END_OF_LINE_P (&it)
12583 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12584 : (text_area_width / 2))))
12585 / FRAME_COLUMN_WIDTH (it.f);
12586 else if ((!row_r2l_p
12587 && w->cursor.x >= text_area_width - h_margin)
12588 || (row_r2l_p && w->cursor.x <= h_margin))
12589 {
12590 if (hscroll_relative_p)
12591 wanted_x = text_area_width * (1 - hscroll_step_rel)
12592 - h_margin;
12593 else
12594 wanted_x = text_area_width
12595 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12596 - h_margin;
12597 hscroll
12598 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12599 }
12600 else
12601 {
12602 if (hscroll_relative_p)
12603 wanted_x = text_area_width * hscroll_step_rel
12604 + h_margin;
12605 else
12606 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12607 + h_margin;
12608 hscroll
12609 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12610 }
12611 hscroll = max (hscroll, w->min_hscroll);
12612
12613 /* Don't prevent redisplay optimizations if hscroll
12614 hasn't changed, as it will unnecessarily slow down
12615 redisplay. */
12616 if (w->hscroll != hscroll)
12617 {
12618 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12619 w->hscroll = hscroll;
12620 hscrolled_p = 1;
12621 }
12622 }
12623 }
12624
12625 window = w->next;
12626 }
12627
12628 /* Value is non-zero if hscroll of any leaf window has been changed. */
12629 return hscrolled_p;
12630 }
12631
12632
12633 /* Set hscroll so that cursor is visible and not inside horizontal
12634 scroll margins for all windows in the tree rooted at WINDOW. See
12635 also hscroll_window_tree above. Value is non-zero if any window's
12636 hscroll has been changed. If it has, desired matrices on the frame
12637 of WINDOW are cleared. */
12638
12639 static int
12640 hscroll_windows (Lisp_Object window)
12641 {
12642 int hscrolled_p = hscroll_window_tree (window);
12643 if (hscrolled_p)
12644 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12645 return hscrolled_p;
12646 }
12647
12648
12649 \f
12650 /************************************************************************
12651 Redisplay
12652 ************************************************************************/
12653
12654 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12655 to a non-zero value. This is sometimes handy to have in a debugger
12656 session. */
12657
12658 #ifdef GLYPH_DEBUG
12659
12660 /* First and last unchanged row for try_window_id. */
12661
12662 static int debug_first_unchanged_at_end_vpos;
12663 static int debug_last_unchanged_at_beg_vpos;
12664
12665 /* Delta vpos and y. */
12666
12667 static int debug_dvpos, debug_dy;
12668
12669 /* Delta in characters and bytes for try_window_id. */
12670
12671 static ptrdiff_t debug_delta, debug_delta_bytes;
12672
12673 /* Values of window_end_pos and window_end_vpos at the end of
12674 try_window_id. */
12675
12676 static ptrdiff_t debug_end_vpos;
12677
12678 /* Append a string to W->desired_matrix->method. FMT is a printf
12679 format string. If trace_redisplay_p is non-zero also printf the
12680 resulting string to stderr. */
12681
12682 static void debug_method_add (struct window *, char const *, ...)
12683 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12684
12685 static void
12686 debug_method_add (struct window *w, char const *fmt, ...)
12687 {
12688 void *ptr = w;
12689 char *method = w->desired_matrix->method;
12690 int len = strlen (method);
12691 int size = sizeof w->desired_matrix->method;
12692 int remaining = size - len - 1;
12693 va_list ap;
12694
12695 if (len && remaining)
12696 {
12697 method[len] = '|';
12698 --remaining, ++len;
12699 }
12700
12701 va_start (ap, fmt);
12702 vsnprintf (method + len, remaining + 1, fmt, ap);
12703 va_end (ap);
12704
12705 if (trace_redisplay_p)
12706 fprintf (stderr, "%p (%s): %s\n",
12707 ptr,
12708 ((BUFFERP (w->contents)
12709 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12710 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12711 : "no buffer"),
12712 method + len);
12713 }
12714
12715 #endif /* GLYPH_DEBUG */
12716
12717
12718 /* Value is non-zero if all changes in window W, which displays
12719 current_buffer, are in the text between START and END. START is a
12720 buffer position, END is given as a distance from Z. Used in
12721 redisplay_internal for display optimization. */
12722
12723 static int
12724 text_outside_line_unchanged_p (struct window *w,
12725 ptrdiff_t start, ptrdiff_t end)
12726 {
12727 int unchanged_p = 1;
12728
12729 /* If text or overlays have changed, see where. */
12730 if (window_outdated (w))
12731 {
12732 /* Gap in the line? */
12733 if (GPT < start || Z - GPT < end)
12734 unchanged_p = 0;
12735
12736 /* Changes start in front of the line, or end after it? */
12737 if (unchanged_p
12738 && (BEG_UNCHANGED < start - 1
12739 || END_UNCHANGED < end))
12740 unchanged_p = 0;
12741
12742 /* If selective display, can't optimize if changes start at the
12743 beginning of the line. */
12744 if (unchanged_p
12745 && INTEGERP (BVAR (current_buffer, selective_display))
12746 && XINT (BVAR (current_buffer, selective_display)) > 0
12747 && (BEG_UNCHANGED < start || GPT <= start))
12748 unchanged_p = 0;
12749
12750 /* If there are overlays at the start or end of the line, these
12751 may have overlay strings with newlines in them. A change at
12752 START, for instance, may actually concern the display of such
12753 overlay strings as well, and they are displayed on different
12754 lines. So, quickly rule out this case. (For the future, it
12755 might be desirable to implement something more telling than
12756 just BEG/END_UNCHANGED.) */
12757 if (unchanged_p)
12758 {
12759 if (BEG + BEG_UNCHANGED == start
12760 && overlay_touches_p (start))
12761 unchanged_p = 0;
12762 if (END_UNCHANGED == end
12763 && overlay_touches_p (Z - end))
12764 unchanged_p = 0;
12765 }
12766
12767 /* Under bidi reordering, adding or deleting a character in the
12768 beginning of a paragraph, before the first strong directional
12769 character, can change the base direction of the paragraph (unless
12770 the buffer specifies a fixed paragraph direction), which will
12771 require to redisplay the whole paragraph. It might be worthwhile
12772 to find the paragraph limits and widen the range of redisplayed
12773 lines to that, but for now just give up this optimization. */
12774 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12775 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12776 unchanged_p = 0;
12777 }
12778
12779 return unchanged_p;
12780 }
12781
12782
12783 /* Do a frame update, taking possible shortcuts into account. This is
12784 the main external entry point for redisplay.
12785
12786 If the last redisplay displayed an echo area message and that message
12787 is no longer requested, we clear the echo area or bring back the
12788 mini-buffer if that is in use. */
12789
12790 void
12791 redisplay (void)
12792 {
12793 redisplay_internal ();
12794 }
12795
12796
12797 static Lisp_Object
12798 overlay_arrow_string_or_property (Lisp_Object var)
12799 {
12800 Lisp_Object val;
12801
12802 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12803 return val;
12804
12805 return Voverlay_arrow_string;
12806 }
12807
12808 /* Return 1 if there are any overlay-arrows in current_buffer. */
12809 static int
12810 overlay_arrow_in_current_buffer_p (void)
12811 {
12812 Lisp_Object vlist;
12813
12814 for (vlist = Voverlay_arrow_variable_list;
12815 CONSP (vlist);
12816 vlist = XCDR (vlist))
12817 {
12818 Lisp_Object var = XCAR (vlist);
12819 Lisp_Object val;
12820
12821 if (!SYMBOLP (var))
12822 continue;
12823 val = find_symbol_value (var);
12824 if (MARKERP (val)
12825 && current_buffer == XMARKER (val)->buffer)
12826 return 1;
12827 }
12828 return 0;
12829 }
12830
12831
12832 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12833 has changed. */
12834
12835 static int
12836 overlay_arrows_changed_p (void)
12837 {
12838 Lisp_Object vlist;
12839
12840 for (vlist = Voverlay_arrow_variable_list;
12841 CONSP (vlist);
12842 vlist = XCDR (vlist))
12843 {
12844 Lisp_Object var = XCAR (vlist);
12845 Lisp_Object val, pstr;
12846
12847 if (!SYMBOLP (var))
12848 continue;
12849 val = find_symbol_value (var);
12850 if (!MARKERP (val))
12851 continue;
12852 if (! EQ (COERCE_MARKER (val),
12853 Fget (var, Qlast_arrow_position))
12854 || ! (pstr = overlay_arrow_string_or_property (var),
12855 EQ (pstr, Fget (var, Qlast_arrow_string))))
12856 return 1;
12857 }
12858 return 0;
12859 }
12860
12861 /* Mark overlay arrows to be updated on next redisplay. */
12862
12863 static void
12864 update_overlay_arrows (int up_to_date)
12865 {
12866 Lisp_Object vlist;
12867
12868 for (vlist = Voverlay_arrow_variable_list;
12869 CONSP (vlist);
12870 vlist = XCDR (vlist))
12871 {
12872 Lisp_Object var = XCAR (vlist);
12873
12874 if (!SYMBOLP (var))
12875 continue;
12876
12877 if (up_to_date > 0)
12878 {
12879 Lisp_Object val = find_symbol_value (var);
12880 Fput (var, Qlast_arrow_position,
12881 COERCE_MARKER (val));
12882 Fput (var, Qlast_arrow_string,
12883 overlay_arrow_string_or_property (var));
12884 }
12885 else if (up_to_date < 0
12886 || !NILP (Fget (var, Qlast_arrow_position)))
12887 {
12888 Fput (var, Qlast_arrow_position, Qt);
12889 Fput (var, Qlast_arrow_string, Qt);
12890 }
12891 }
12892 }
12893
12894
12895 /* Return overlay arrow string to display at row.
12896 Return integer (bitmap number) for arrow bitmap in left fringe.
12897 Return nil if no overlay arrow. */
12898
12899 static Lisp_Object
12900 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12901 {
12902 Lisp_Object vlist;
12903
12904 for (vlist = Voverlay_arrow_variable_list;
12905 CONSP (vlist);
12906 vlist = XCDR (vlist))
12907 {
12908 Lisp_Object var = XCAR (vlist);
12909 Lisp_Object val;
12910
12911 if (!SYMBOLP (var))
12912 continue;
12913
12914 val = find_symbol_value (var);
12915
12916 if (MARKERP (val)
12917 && current_buffer == XMARKER (val)->buffer
12918 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12919 {
12920 if (FRAME_WINDOW_P (it->f)
12921 /* FIXME: if ROW->reversed_p is set, this should test
12922 the right fringe, not the left one. */
12923 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12924 {
12925 #ifdef HAVE_WINDOW_SYSTEM
12926 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12927 {
12928 int fringe_bitmap;
12929 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12930 return make_number (fringe_bitmap);
12931 }
12932 #endif
12933 return make_number (-1); /* Use default arrow bitmap. */
12934 }
12935 return overlay_arrow_string_or_property (var);
12936 }
12937 }
12938
12939 return Qnil;
12940 }
12941
12942 /* Return 1 if point moved out of or into a composition. Otherwise
12943 return 0. PREV_BUF and PREV_PT are the last point buffer and
12944 position. BUF and PT are the current point buffer and position. */
12945
12946 static int
12947 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12948 struct buffer *buf, ptrdiff_t pt)
12949 {
12950 ptrdiff_t start, end;
12951 Lisp_Object prop;
12952 Lisp_Object buffer;
12953
12954 XSETBUFFER (buffer, buf);
12955 /* Check a composition at the last point if point moved within the
12956 same buffer. */
12957 if (prev_buf == buf)
12958 {
12959 if (prev_pt == pt)
12960 /* Point didn't move. */
12961 return 0;
12962
12963 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12964 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12965 && composition_valid_p (start, end, prop)
12966 && start < prev_pt && end > prev_pt)
12967 /* The last point was within the composition. Return 1 iff
12968 point moved out of the composition. */
12969 return (pt <= start || pt >= end);
12970 }
12971
12972 /* Check a composition at the current point. */
12973 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12974 && find_composition (pt, -1, &start, &end, &prop, buffer)
12975 && composition_valid_p (start, end, prop)
12976 && start < pt && end > pt);
12977 }
12978
12979 /* Reconsider the clip changes of buffer which is displayed in W. */
12980
12981 static void
12982 reconsider_clip_changes (struct window *w)
12983 {
12984 struct buffer *b = XBUFFER (w->contents);
12985
12986 if (b->clip_changed
12987 && w->window_end_valid
12988 && w->current_matrix->buffer == b
12989 && w->current_matrix->zv == BUF_ZV (b)
12990 && w->current_matrix->begv == BUF_BEGV (b))
12991 b->clip_changed = 0;
12992
12993 /* If display wasn't paused, and W is not a tool bar window, see if
12994 point has been moved into or out of a composition. In that case,
12995 we set b->clip_changed to 1 to force updating the screen. If
12996 b->clip_changed has already been set to 1, we can skip this
12997 check. */
12998 if (!b->clip_changed && w->window_end_valid)
12999 {
13000 ptrdiff_t pt = (w == XWINDOW (selected_window)
13001 ? PT : marker_position (w->pointm));
13002
13003 if ((w->current_matrix->buffer != b || pt != w->last_point)
13004 && check_point_in_composition (w->current_matrix->buffer,
13005 w->last_point, b, pt))
13006 b->clip_changed = 1;
13007 }
13008 }
13009
13010 #define STOP_POLLING \
13011 do { if (! polling_stopped_here) stop_polling (); \
13012 polling_stopped_here = 1; } while (0)
13013
13014 #define RESUME_POLLING \
13015 do { if (polling_stopped_here) start_polling (); \
13016 polling_stopped_here = 0; } while (0)
13017
13018
13019 /* Perhaps in the future avoid recentering windows if it
13020 is not necessary; currently that causes some problems. */
13021
13022 static void
13023 redisplay_internal (void)
13024 {
13025 struct window *w = XWINDOW (selected_window);
13026 struct window *sw;
13027 struct frame *fr;
13028 int pending;
13029 bool must_finish = 0, match_p;
13030 struct text_pos tlbufpos, tlendpos;
13031 int number_of_visible_frames;
13032 ptrdiff_t count;
13033 struct frame *sf;
13034 int polling_stopped_here = 0;
13035 Lisp_Object tail, frame;
13036
13037 /* Non-zero means redisplay has to consider all windows on all
13038 frames. Zero means, only selected_window is considered. */
13039 int consider_all_windows_p;
13040
13041 /* Non-zero means redisplay has to redisplay the miniwindow. */
13042 int update_miniwindow_p = 0;
13043
13044 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13045
13046 /* No redisplay if running in batch mode or frame is not yet fully
13047 initialized, or redisplay is explicitly turned off by setting
13048 Vinhibit_redisplay. */
13049 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13050 || !NILP (Vinhibit_redisplay))
13051 return;
13052
13053 /* Don't examine these until after testing Vinhibit_redisplay.
13054 When Emacs is shutting down, perhaps because its connection to
13055 X has dropped, we should not look at them at all. */
13056 fr = XFRAME (w->frame);
13057 sf = SELECTED_FRAME ();
13058
13059 if (!fr->glyphs_initialized_p)
13060 return;
13061
13062 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13063 if (popup_activated ())
13064 return;
13065 #endif
13066
13067 /* I don't think this happens but let's be paranoid. */
13068 if (redisplaying_p)
13069 return;
13070
13071 /* Record a function that clears redisplaying_p
13072 when we leave this function. */
13073 count = SPECPDL_INDEX ();
13074 record_unwind_protect_void (unwind_redisplay);
13075 redisplaying_p = 1;
13076 specbind (Qinhibit_free_realized_faces, Qnil);
13077
13078 /* Record this function, so it appears on the profiler's backtraces. */
13079 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13080
13081 FOR_EACH_FRAME (tail, frame)
13082 XFRAME (frame)->already_hscrolled_p = 0;
13083
13084 retry:
13085 /* Remember the currently selected window. */
13086 sw = w;
13087
13088 pending = 0;
13089 last_escape_glyph_frame = NULL;
13090 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13091 last_glyphless_glyph_frame = NULL;
13092 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13093
13094 /* If new fonts have been loaded that make a glyph matrix adjustment
13095 necessary, do it. */
13096 if (fonts_changed_p)
13097 {
13098 adjust_glyphs (NULL);
13099 ++windows_or_buffers_changed;
13100 fonts_changed_p = 0;
13101 }
13102
13103 /* If face_change_count is non-zero, init_iterator will free all
13104 realized faces, which includes the faces referenced from current
13105 matrices. So, we can't reuse current matrices in this case. */
13106 if (face_change_count)
13107 ++windows_or_buffers_changed;
13108
13109 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13110 && FRAME_TTY (sf)->previous_frame != sf)
13111 {
13112 /* Since frames on a single ASCII terminal share the same
13113 display area, displaying a different frame means redisplay
13114 the whole thing. */
13115 windows_or_buffers_changed++;
13116 SET_FRAME_GARBAGED (sf);
13117 #ifndef DOS_NT
13118 set_tty_color_mode (FRAME_TTY (sf), sf);
13119 #endif
13120 FRAME_TTY (sf)->previous_frame = sf;
13121 }
13122
13123 /* Set the visible flags for all frames. Do this before checking for
13124 resized or garbaged frames; they want to know if their frames are
13125 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13126 number_of_visible_frames = 0;
13127
13128 FOR_EACH_FRAME (tail, frame)
13129 {
13130 struct frame *f = XFRAME (frame);
13131
13132 if (FRAME_VISIBLE_P (f))
13133 ++number_of_visible_frames;
13134 clear_desired_matrices (f);
13135 }
13136
13137 /* Notice any pending interrupt request to change frame size. */
13138 do_pending_window_change (1);
13139
13140 /* do_pending_window_change could change the selected_window due to
13141 frame resizing which makes the selected window too small. */
13142 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13143 sw = w;
13144
13145 /* Clear frames marked as garbaged. */
13146 clear_garbaged_frames ();
13147
13148 /* Build menubar and tool-bar items. */
13149 if (NILP (Vmemory_full))
13150 prepare_menu_bars ();
13151
13152 if (windows_or_buffers_changed)
13153 update_mode_lines++;
13154
13155 reconsider_clip_changes (w);
13156
13157 /* In most cases selected window displays current buffer. */
13158 match_p = XBUFFER (w->contents) == current_buffer;
13159 if (match_p)
13160 {
13161 ptrdiff_t count1;
13162
13163 /* Detect case that we need to write or remove a star in the mode line. */
13164 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13165 {
13166 w->update_mode_line = 1;
13167 if (buffer_shared_and_changed ())
13168 update_mode_lines++;
13169 }
13170
13171 /* Avoid invocation of point motion hooks by `current_column' below. */
13172 count1 = SPECPDL_INDEX ();
13173 specbind (Qinhibit_point_motion_hooks, Qt);
13174
13175 if (mode_line_update_needed (w))
13176 w->update_mode_line = 1;
13177
13178 unbind_to (count1, Qnil);
13179 }
13180
13181 consider_all_windows_p = (update_mode_lines
13182 || buffer_shared_and_changed ()
13183 || cursor_type_changed);
13184
13185 /* If specs for an arrow have changed, do thorough redisplay
13186 to ensure we remove any arrow that should no longer exist. */
13187 if (overlay_arrows_changed_p ())
13188 consider_all_windows_p = windows_or_buffers_changed = 1;
13189
13190 /* Normally the message* functions will have already displayed and
13191 updated the echo area, but the frame may have been trashed, or
13192 the update may have been preempted, so display the echo area
13193 again here. Checking message_cleared_p captures the case that
13194 the echo area should be cleared. */
13195 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13196 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13197 || (message_cleared_p
13198 && minibuf_level == 0
13199 /* If the mini-window is currently selected, this means the
13200 echo-area doesn't show through. */
13201 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13202 {
13203 int window_height_changed_p = echo_area_display (0);
13204
13205 if (message_cleared_p)
13206 update_miniwindow_p = 1;
13207
13208 must_finish = 1;
13209
13210 /* If we don't display the current message, don't clear the
13211 message_cleared_p flag, because, if we did, we wouldn't clear
13212 the echo area in the next redisplay which doesn't preserve
13213 the echo area. */
13214 if (!display_last_displayed_message_p)
13215 message_cleared_p = 0;
13216
13217 if (fonts_changed_p)
13218 goto retry;
13219 else if (window_height_changed_p)
13220 {
13221 consider_all_windows_p = 1;
13222 ++update_mode_lines;
13223 ++windows_or_buffers_changed;
13224
13225 /* If window configuration was changed, frames may have been
13226 marked garbaged. Clear them or we will experience
13227 surprises wrt scrolling. */
13228 clear_garbaged_frames ();
13229 }
13230 }
13231 else if (EQ (selected_window, minibuf_window)
13232 && (current_buffer->clip_changed || window_outdated (w))
13233 && resize_mini_window (w, 0))
13234 {
13235 /* Resized active mini-window to fit the size of what it is
13236 showing if its contents might have changed. */
13237 must_finish = 1;
13238 /* FIXME: this causes all frames to be updated, which seems unnecessary
13239 since only the current frame needs to be considered. This function
13240 needs to be rewritten with two variables, consider_all_windows and
13241 consider_all_frames. */
13242 consider_all_windows_p = 1;
13243 ++windows_or_buffers_changed;
13244 ++update_mode_lines;
13245
13246 /* If window configuration was changed, frames may have been
13247 marked garbaged. Clear them or we will experience
13248 surprises wrt scrolling. */
13249 clear_garbaged_frames ();
13250 }
13251
13252 /* If showing the region, and mark has changed, we must redisplay
13253 the whole window. The assignment to this_line_start_pos prevents
13254 the optimization directly below this if-statement. */
13255 if (((!NILP (Vtransient_mark_mode)
13256 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13257 != (w->region_showing > 0))
13258 || (w->region_showing
13259 && w->region_showing
13260 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13261 CHARPOS (this_line_start_pos) = 0;
13262
13263 /* Optimize the case that only the line containing the cursor in the
13264 selected window has changed. Variables starting with this_ are
13265 set in display_line and record information about the line
13266 containing the cursor. */
13267 tlbufpos = this_line_start_pos;
13268 tlendpos = this_line_end_pos;
13269 if (!consider_all_windows_p
13270 && CHARPOS (tlbufpos) > 0
13271 && !w->update_mode_line
13272 && !current_buffer->clip_changed
13273 && !current_buffer->prevent_redisplay_optimizations_p
13274 && FRAME_VISIBLE_P (XFRAME (w->frame))
13275 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13276 /* Make sure recorded data applies to current buffer, etc. */
13277 && this_line_buffer == current_buffer
13278 && match_p
13279 && !w->force_start
13280 && !w->optional_new_start
13281 /* Point must be on the line that we have info recorded about. */
13282 && PT >= CHARPOS (tlbufpos)
13283 && PT <= Z - CHARPOS (tlendpos)
13284 /* All text outside that line, including its final newline,
13285 must be unchanged. */
13286 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13287 CHARPOS (tlendpos)))
13288 {
13289 if (CHARPOS (tlbufpos) > BEGV
13290 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13291 && (CHARPOS (tlbufpos) == ZV
13292 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13293 /* Former continuation line has disappeared by becoming empty. */
13294 goto cancel;
13295 else if (window_outdated (w) || MINI_WINDOW_P (w))
13296 {
13297 /* We have to handle the case of continuation around a
13298 wide-column character (see the comment in indent.c around
13299 line 1340).
13300
13301 For instance, in the following case:
13302
13303 -------- Insert --------
13304 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13305 J_I_ ==> J_I_ `^^' are cursors.
13306 ^^ ^^
13307 -------- --------
13308
13309 As we have to redraw the line above, we cannot use this
13310 optimization. */
13311
13312 struct it it;
13313 int line_height_before = this_line_pixel_height;
13314
13315 /* Note that start_display will handle the case that the
13316 line starting at tlbufpos is a continuation line. */
13317 start_display (&it, w, tlbufpos);
13318
13319 /* Implementation note: It this still necessary? */
13320 if (it.current_x != this_line_start_x)
13321 goto cancel;
13322
13323 TRACE ((stderr, "trying display optimization 1\n"));
13324 w->cursor.vpos = -1;
13325 overlay_arrow_seen = 0;
13326 it.vpos = this_line_vpos;
13327 it.current_y = this_line_y;
13328 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13329 display_line (&it);
13330
13331 /* If line contains point, is not continued,
13332 and ends at same distance from eob as before, we win. */
13333 if (w->cursor.vpos >= 0
13334 /* Line is not continued, otherwise this_line_start_pos
13335 would have been set to 0 in display_line. */
13336 && CHARPOS (this_line_start_pos)
13337 /* Line ends as before. */
13338 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13339 /* Line has same height as before. Otherwise other lines
13340 would have to be shifted up or down. */
13341 && this_line_pixel_height == line_height_before)
13342 {
13343 /* If this is not the window's last line, we must adjust
13344 the charstarts of the lines below. */
13345 if (it.current_y < it.last_visible_y)
13346 {
13347 struct glyph_row *row
13348 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13349 ptrdiff_t delta, delta_bytes;
13350
13351 /* We used to distinguish between two cases here,
13352 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13353 when the line ends in a newline or the end of the
13354 buffer's accessible portion. But both cases did
13355 the same, so they were collapsed. */
13356 delta = (Z
13357 - CHARPOS (tlendpos)
13358 - MATRIX_ROW_START_CHARPOS (row));
13359 delta_bytes = (Z_BYTE
13360 - BYTEPOS (tlendpos)
13361 - MATRIX_ROW_START_BYTEPOS (row));
13362
13363 increment_matrix_positions (w->current_matrix,
13364 this_line_vpos + 1,
13365 w->current_matrix->nrows,
13366 delta, delta_bytes);
13367 }
13368
13369 /* If this row displays text now but previously didn't,
13370 or vice versa, w->window_end_vpos may have to be
13371 adjusted. */
13372 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13373 {
13374 if (w->window_end_vpos < this_line_vpos)
13375 w->window_end_vpos = this_line_vpos;
13376 }
13377 else if (w->window_end_vpos == this_line_vpos
13378 && this_line_vpos > 0)
13379 w->window_end_vpos = this_line_vpos - 1;
13380 w->window_end_valid = 0;
13381
13382 /* Update hint: No need to try to scroll in update_window. */
13383 w->desired_matrix->no_scrolling_p = 1;
13384
13385 #ifdef GLYPH_DEBUG
13386 *w->desired_matrix->method = 0;
13387 debug_method_add (w, "optimization 1");
13388 #endif
13389 #if HAVE_XWIDGETS
13390 //debug optimization movement issue
13391 //w->desired_matrix->no_scrolling_p = 1;
13392 //*w->desired_matrix->method = 0;
13393 //debug_method_add (w, "optimization 1");
13394 #endif
13395
13396 #ifdef HAVE_WINDOW_SYSTEM
13397 update_window_fringes (w, 0);
13398 #endif
13399 goto update;
13400 }
13401 else
13402 goto cancel;
13403 }
13404 else if (/* Cursor position hasn't changed. */
13405 PT == w->last_point
13406 /* Make sure the cursor was last displayed
13407 in this window. Otherwise we have to reposition it. */
13408 && 0 <= w->cursor.vpos
13409 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13410 {
13411 if (!must_finish)
13412 {
13413 do_pending_window_change (1);
13414 /* If selected_window changed, redisplay again. */
13415 if (WINDOWP (selected_window)
13416 && (w = XWINDOW (selected_window)) != sw)
13417 goto retry;
13418
13419 /* We used to always goto end_of_redisplay here, but this
13420 isn't enough if we have a blinking cursor. */
13421 if (w->cursor_off_p == w->last_cursor_off_p)
13422 goto end_of_redisplay;
13423 }
13424 goto update;
13425 }
13426 /* If highlighting the region, or if the cursor is in the echo area,
13427 then we can't just move the cursor. */
13428 else if (! (!NILP (Vtransient_mark_mode)
13429 && !NILP (BVAR (current_buffer, mark_active)))
13430 && (EQ (selected_window,
13431 BVAR (current_buffer, last_selected_window))
13432 || highlight_nonselected_windows)
13433 && !w->region_showing
13434 && NILP (Vshow_trailing_whitespace)
13435 && !cursor_in_echo_area)
13436 {
13437 struct it it;
13438 struct glyph_row *row;
13439
13440 /* Skip from tlbufpos to PT and see where it is. Note that
13441 PT may be in invisible text. If so, we will end at the
13442 next visible position. */
13443 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13444 NULL, DEFAULT_FACE_ID);
13445 it.current_x = this_line_start_x;
13446 it.current_y = this_line_y;
13447 it.vpos = this_line_vpos;
13448
13449 /* The call to move_it_to stops in front of PT, but
13450 moves over before-strings. */
13451 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13452
13453 if (it.vpos == this_line_vpos
13454 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13455 row->enabled_p))
13456 {
13457 eassert (this_line_vpos == it.vpos);
13458 eassert (this_line_y == it.current_y);
13459 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13460 #ifdef GLYPH_DEBUG
13461 *w->desired_matrix->method = 0;
13462 debug_method_add (w, "optimization 3");
13463 #endif
13464 goto update;
13465 }
13466 else
13467 goto cancel;
13468 }
13469
13470 cancel:
13471 /* Text changed drastically or point moved off of line. */
13472 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13473 }
13474
13475 CHARPOS (this_line_start_pos) = 0;
13476 consider_all_windows_p |= buffer_shared_and_changed ();
13477 ++clear_face_cache_count;
13478 #ifdef HAVE_WINDOW_SYSTEM
13479 ++clear_image_cache_count;
13480 #endif
13481
13482 /* Build desired matrices, and update the display. If
13483 consider_all_windows_p is non-zero, do it for all windows on all
13484 frames. Otherwise do it for selected_window, only. */
13485
13486 if (consider_all_windows_p)
13487 {
13488 FOR_EACH_FRAME (tail, frame)
13489 XFRAME (frame)->updated_p = 0;
13490
13491 FOR_EACH_FRAME (tail, frame)
13492 {
13493 struct frame *f = XFRAME (frame);
13494
13495 /* We don't have to do anything for unselected terminal
13496 frames. */
13497 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13498 && !EQ (FRAME_TTY (f)->top_frame, frame))
13499 continue;
13500
13501 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13502 {
13503 /* Mark all the scroll bars to be removed; we'll redeem
13504 the ones we want when we redisplay their windows. */
13505 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13506 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13507
13508 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13509 redisplay_windows (FRAME_ROOT_WINDOW (f));
13510
13511 /* The X error handler may have deleted that frame. */
13512 if (!FRAME_LIVE_P (f))
13513 continue;
13514
13515 /* Any scroll bars which redisplay_windows should have
13516 nuked should now go away. */
13517 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13518 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13519
13520 /* If fonts changed, display again. */
13521 /* ??? rms: I suspect it is a mistake to jump all the way
13522 back to retry here. It should just retry this frame. */
13523 if (fonts_changed_p)
13524 goto retry;
13525
13526 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13527 {
13528 /* See if we have to hscroll. */
13529 if (!f->already_hscrolled_p)
13530 {
13531 f->already_hscrolled_p = 1;
13532 if (hscroll_windows (f->root_window))
13533 goto retry;
13534 }
13535
13536 /* Prevent various kinds of signals during display
13537 update. stdio is not robust about handling
13538 signals, which can cause an apparent I/O
13539 error. */
13540 if (interrupt_input)
13541 unrequest_sigio ();
13542 STOP_POLLING;
13543
13544 /* Update the display. */
13545 set_window_update_flags (XWINDOW (f->root_window), 1);
13546 pending |= update_frame (f, 0, 0);
13547 f->updated_p = 1;
13548 }
13549 }
13550 }
13551
13552 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13553
13554 if (!pending)
13555 {
13556 /* Do the mark_window_display_accurate after all windows have
13557 been redisplayed because this call resets flags in buffers
13558 which are needed for proper redisplay. */
13559 FOR_EACH_FRAME (tail, frame)
13560 {
13561 struct frame *f = XFRAME (frame);
13562 if (f->updated_p)
13563 {
13564 mark_window_display_accurate (f->root_window, 1);
13565 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13566 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13567 }
13568 }
13569 }
13570 }
13571 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13572 {
13573 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13574 struct frame *mini_frame;
13575
13576 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13577 /* Use list_of_error, not Qerror, so that
13578 we catch only errors and don't run the debugger. */
13579 internal_condition_case_1 (redisplay_window_1, selected_window,
13580 list_of_error,
13581 redisplay_window_error);
13582 if (update_miniwindow_p)
13583 internal_condition_case_1 (redisplay_window_1, mini_window,
13584 list_of_error,
13585 redisplay_window_error);
13586
13587 /* Compare desired and current matrices, perform output. */
13588
13589 update:
13590 /* If fonts changed, display again. */
13591 if (fonts_changed_p)
13592 goto retry;
13593
13594 /* Prevent various kinds of signals during display update.
13595 stdio is not robust about handling signals,
13596 which can cause an apparent I/O error. */
13597 if (interrupt_input)
13598 unrequest_sigio ();
13599 STOP_POLLING;
13600
13601 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13602 {
13603 if (hscroll_windows (selected_window))
13604 goto retry;
13605
13606 XWINDOW (selected_window)->must_be_updated_p = 1;
13607 pending = update_frame (sf, 0, 0);
13608 }
13609
13610 /* We may have called echo_area_display at the top of this
13611 function. If the echo area is on another frame, that may
13612 have put text on a frame other than the selected one, so the
13613 above call to update_frame would not have caught it. Catch
13614 it here. */
13615 mini_window = FRAME_MINIBUF_WINDOW (sf);
13616 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13617
13618 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13619 {
13620 XWINDOW (mini_window)->must_be_updated_p = 1;
13621 pending |= update_frame (mini_frame, 0, 0);
13622 if (!pending && hscroll_windows (mini_window))
13623 goto retry;
13624 }
13625 }
13626
13627 /* If display was paused because of pending input, make sure we do a
13628 thorough update the next time. */
13629 if (pending)
13630 {
13631 /* Prevent the optimization at the beginning of
13632 redisplay_internal that tries a single-line update of the
13633 line containing the cursor in the selected window. */
13634 CHARPOS (this_line_start_pos) = 0;
13635
13636 /* Let the overlay arrow be updated the next time. */
13637 update_overlay_arrows (0);
13638
13639 /* If we pause after scrolling, some rows in the current
13640 matrices of some windows are not valid. */
13641 if (!WINDOW_FULL_WIDTH_P (w)
13642 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13643 update_mode_lines = 1;
13644 }
13645 else
13646 {
13647 if (!consider_all_windows_p)
13648 {
13649 /* This has already been done above if
13650 consider_all_windows_p is set. */
13651 mark_window_display_accurate_1 (w, 1);
13652
13653 /* Say overlay arrows are up to date. */
13654 update_overlay_arrows (1);
13655
13656 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13657 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13658 }
13659
13660 update_mode_lines = 0;
13661 windows_or_buffers_changed = 0;
13662 cursor_type_changed = 0;
13663 }
13664
13665 /* Start SIGIO interrupts coming again. Having them off during the
13666 code above makes it less likely one will discard output, but not
13667 impossible, since there might be stuff in the system buffer here.
13668 But it is much hairier to try to do anything about that. */
13669 if (interrupt_input)
13670 request_sigio ();
13671 RESUME_POLLING;
13672
13673 /* If a frame has become visible which was not before, redisplay
13674 again, so that we display it. Expose events for such a frame
13675 (which it gets when becoming visible) don't call the parts of
13676 redisplay constructing glyphs, so simply exposing a frame won't
13677 display anything in this case. So, we have to display these
13678 frames here explicitly. */
13679 if (!pending)
13680 {
13681 int new_count = 0;
13682
13683 FOR_EACH_FRAME (tail, frame)
13684 {
13685 int this_is_visible = 0;
13686
13687 if (XFRAME (frame)->visible)
13688 this_is_visible = 1;
13689
13690 if (this_is_visible)
13691 new_count++;
13692 }
13693
13694 if (new_count != number_of_visible_frames)
13695 windows_or_buffers_changed++;
13696 }
13697
13698 /* Change frame size now if a change is pending. */
13699 do_pending_window_change (1);
13700
13701 /* If we just did a pending size change, or have additional
13702 visible frames, or selected_window changed, redisplay again. */
13703 if ((windows_or_buffers_changed && !pending)
13704 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13705 goto retry;
13706
13707 /* Clear the face and image caches.
13708
13709 We used to do this only if consider_all_windows_p. But the cache
13710 needs to be cleared if a timer creates images in the current
13711 buffer (e.g. the test case in Bug#6230). */
13712
13713 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13714 {
13715 clear_face_cache (0);
13716 clear_face_cache_count = 0;
13717 }
13718
13719 #ifdef HAVE_WINDOW_SYSTEM
13720 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13721 {
13722 clear_image_caches (Qnil);
13723 clear_image_cache_count = 0;
13724 }
13725 #endif /* HAVE_WINDOW_SYSTEM */
13726
13727 end_of_redisplay:
13728 unbind_to (count, Qnil);
13729 RESUME_POLLING;
13730 }
13731
13732
13733 /* Redisplay, but leave alone any recent echo area message unless
13734 another message has been requested in its place.
13735
13736 This is useful in situations where you need to redisplay but no
13737 user action has occurred, making it inappropriate for the message
13738 area to be cleared. See tracking_off and
13739 wait_reading_process_output for examples of these situations.
13740
13741 FROM_WHERE is an integer saying from where this function was
13742 called. This is useful for debugging. */
13743
13744 void
13745 redisplay_preserve_echo_area (int from_where)
13746 {
13747 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13748
13749 if (!NILP (echo_area_buffer[1]))
13750 {
13751 /* We have a previously displayed message, but no current
13752 message. Redisplay the previous message. */
13753 display_last_displayed_message_p = 1;
13754 redisplay_internal ();
13755 display_last_displayed_message_p = 0;
13756 }
13757 else
13758 redisplay_internal ();
13759
13760 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13761 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13762 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13763 }
13764
13765
13766 /* Function registered with record_unwind_protect in redisplay_internal. */
13767
13768 static void
13769 unwind_redisplay (void)
13770 {
13771 redisplaying_p = 0;
13772 }
13773
13774
13775 /* Mark the display of leaf window W as accurate or inaccurate.
13776 If ACCURATE_P is non-zero mark display of W as accurate. If
13777 ACCURATE_P is zero, arrange for W to be redisplayed the next
13778 time redisplay_internal is called. */
13779
13780 static void
13781 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13782 {
13783 struct buffer *b = XBUFFER (w->contents);
13784
13785 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13786 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13787 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13788
13789 if (accurate_p)
13790 {
13791 b->clip_changed = 0;
13792 b->prevent_redisplay_optimizations_p = 0;
13793
13794 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13795 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13796 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13797 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13798
13799 w->current_matrix->buffer = b;
13800 w->current_matrix->begv = BUF_BEGV (b);
13801 w->current_matrix->zv = BUF_ZV (b);
13802
13803 w->last_cursor = w->cursor;
13804 w->last_cursor_off_p = w->cursor_off_p;
13805
13806 if (w == XWINDOW (selected_window))
13807 w->last_point = BUF_PT (b);
13808 else
13809 w->last_point = marker_position (w->pointm);
13810
13811 w->window_end_valid = 1;
13812 w->update_mode_line = 0;
13813 }
13814 }
13815
13816
13817 /* Mark the display of windows in the window tree rooted at WINDOW as
13818 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13819 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13820 be redisplayed the next time redisplay_internal is called. */
13821
13822 void
13823 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13824 {
13825 struct window *w;
13826
13827 for (; !NILP (window); window = w->next)
13828 {
13829 w = XWINDOW (window);
13830 if (WINDOWP (w->contents))
13831 mark_window_display_accurate (w->contents, accurate_p);
13832 else
13833 mark_window_display_accurate_1 (w, accurate_p);
13834 }
13835
13836 if (accurate_p)
13837 update_overlay_arrows (1);
13838 else
13839 /* Force a thorough redisplay the next time by setting
13840 last_arrow_position and last_arrow_string to t, which is
13841 unequal to any useful value of Voverlay_arrow_... */
13842 update_overlay_arrows (-1);
13843 }
13844
13845
13846 /* Return value in display table DP (Lisp_Char_Table *) for character
13847 C. Since a display table doesn't have any parent, we don't have to
13848 follow parent. Do not call this function directly but use the
13849 macro DISP_CHAR_VECTOR. */
13850
13851 Lisp_Object
13852 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13853 {
13854 Lisp_Object val;
13855
13856 if (ASCII_CHAR_P (c))
13857 {
13858 val = dp->ascii;
13859 if (SUB_CHAR_TABLE_P (val))
13860 val = XSUB_CHAR_TABLE (val)->contents[c];
13861 }
13862 else
13863 {
13864 Lisp_Object table;
13865
13866 XSETCHAR_TABLE (table, dp);
13867 val = char_table_ref (table, c);
13868 }
13869 if (NILP (val))
13870 val = dp->defalt;
13871 return val;
13872 }
13873
13874
13875 \f
13876 /***********************************************************************
13877 Window Redisplay
13878 ***********************************************************************/
13879
13880 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13881
13882 static void
13883 redisplay_windows (Lisp_Object window)
13884 {
13885 while (!NILP (window))
13886 {
13887 struct window *w = XWINDOW (window);
13888
13889 if (WINDOWP (w->contents))
13890 redisplay_windows (w->contents);
13891 else if (BUFFERP (w->contents))
13892 {
13893 displayed_buffer = XBUFFER (w->contents);
13894 /* Use list_of_error, not Qerror, so that
13895 we catch only errors and don't run the debugger. */
13896 internal_condition_case_1 (redisplay_window_0, window,
13897 list_of_error,
13898 redisplay_window_error);
13899 }
13900
13901 window = w->next;
13902 }
13903 }
13904
13905 static Lisp_Object
13906 redisplay_window_error (Lisp_Object ignore)
13907 {
13908 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13909 return Qnil;
13910 }
13911
13912 static Lisp_Object
13913 redisplay_window_0 (Lisp_Object window)
13914 {
13915 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13916 redisplay_window (window, 0);
13917 return Qnil;
13918 }
13919
13920 static Lisp_Object
13921 redisplay_window_1 (Lisp_Object window)
13922 {
13923 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13924 redisplay_window (window, 1);
13925 return Qnil;
13926 }
13927 \f
13928
13929 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13930 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13931 which positions recorded in ROW differ from current buffer
13932 positions.
13933
13934 Return 0 if cursor is not on this row, 1 otherwise. */
13935
13936 static int
13937 set_cursor_from_row (struct window *w, struct glyph_row *row,
13938 struct glyph_matrix *matrix,
13939 ptrdiff_t delta, ptrdiff_t delta_bytes,
13940 int dy, int dvpos)
13941 {
13942 struct glyph *glyph = row->glyphs[TEXT_AREA];
13943 struct glyph *end = glyph + row->used[TEXT_AREA];
13944 struct glyph *cursor = NULL;
13945 /* The last known character position in row. */
13946 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13947 int x = row->x;
13948 ptrdiff_t pt_old = PT - delta;
13949 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13950 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13951 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13952 /* A glyph beyond the edge of TEXT_AREA which we should never
13953 touch. */
13954 struct glyph *glyphs_end = end;
13955 /* Non-zero means we've found a match for cursor position, but that
13956 glyph has the avoid_cursor_p flag set. */
13957 int match_with_avoid_cursor = 0;
13958 /* Non-zero means we've seen at least one glyph that came from a
13959 display string. */
13960 int string_seen = 0;
13961 /* Largest and smallest buffer positions seen so far during scan of
13962 glyph row. */
13963 ptrdiff_t bpos_max = pos_before;
13964 ptrdiff_t bpos_min = pos_after;
13965 /* Last buffer position covered by an overlay string with an integer
13966 `cursor' property. */
13967 ptrdiff_t bpos_covered = 0;
13968 /* Non-zero means the display string on which to display the cursor
13969 comes from a text property, not from an overlay. */
13970 int string_from_text_prop = 0;
13971
13972 /* Don't even try doing anything if called for a mode-line or
13973 header-line row, since the rest of the code isn't prepared to
13974 deal with such calamities. */
13975 eassert (!row->mode_line_p);
13976 if (row->mode_line_p)
13977 return 0;
13978
13979 /* Skip over glyphs not having an object at the start and the end of
13980 the row. These are special glyphs like truncation marks on
13981 terminal frames. */
13982 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13983 {
13984 if (!row->reversed_p)
13985 {
13986 while (glyph < end
13987 && INTEGERP (glyph->object)
13988 && glyph->charpos < 0)
13989 {
13990 x += glyph->pixel_width;
13991 ++glyph;
13992 }
13993 while (end > glyph
13994 && INTEGERP ((end - 1)->object)
13995 /* CHARPOS is zero for blanks and stretch glyphs
13996 inserted by extend_face_to_end_of_line. */
13997 && (end - 1)->charpos <= 0)
13998 --end;
13999 glyph_before = glyph - 1;
14000 glyph_after = end;
14001 }
14002 else
14003 {
14004 struct glyph *g;
14005
14006 /* If the glyph row is reversed, we need to process it from back
14007 to front, so swap the edge pointers. */
14008 glyphs_end = end = glyph - 1;
14009 glyph += row->used[TEXT_AREA] - 1;
14010
14011 while (glyph > end + 1
14012 && INTEGERP (glyph->object)
14013 && glyph->charpos < 0)
14014 {
14015 --glyph;
14016 x -= glyph->pixel_width;
14017 }
14018 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14019 --glyph;
14020 /* By default, in reversed rows we put the cursor on the
14021 rightmost (first in the reading order) glyph. */
14022 for (g = end + 1; g < glyph; g++)
14023 x += g->pixel_width;
14024 while (end < glyph
14025 && INTEGERP ((end + 1)->object)
14026 && (end + 1)->charpos <= 0)
14027 ++end;
14028 glyph_before = glyph + 1;
14029 glyph_after = end;
14030 }
14031 }
14032 else if (row->reversed_p)
14033 {
14034 /* In R2L rows that don't display text, put the cursor on the
14035 rightmost glyph. Case in point: an empty last line that is
14036 part of an R2L paragraph. */
14037 cursor = end - 1;
14038 /* Avoid placing the cursor on the last glyph of the row, where
14039 on terminal frames we hold the vertical border between
14040 adjacent windows. */
14041 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14042 && !WINDOW_RIGHTMOST_P (w)
14043 && cursor == row->glyphs[LAST_AREA] - 1)
14044 cursor--;
14045 x = -1; /* will be computed below, at label compute_x */
14046 }
14047
14048 /* Step 1: Try to find the glyph whose character position
14049 corresponds to point. If that's not possible, find 2 glyphs
14050 whose character positions are the closest to point, one before
14051 point, the other after it. */
14052 if (!row->reversed_p)
14053 while (/* not marched to end of glyph row */
14054 glyph < end
14055 /* glyph was not inserted by redisplay for internal purposes */
14056 && !INTEGERP (glyph->object))
14057 {
14058 if (BUFFERP (glyph->object))
14059 {
14060 ptrdiff_t dpos = glyph->charpos - pt_old;
14061
14062 if (glyph->charpos > bpos_max)
14063 bpos_max = glyph->charpos;
14064 if (glyph->charpos < bpos_min)
14065 bpos_min = glyph->charpos;
14066 if (!glyph->avoid_cursor_p)
14067 {
14068 /* If we hit point, we've found the glyph on which to
14069 display the cursor. */
14070 if (dpos == 0)
14071 {
14072 match_with_avoid_cursor = 0;
14073 break;
14074 }
14075 /* See if we've found a better approximation to
14076 POS_BEFORE or to POS_AFTER. */
14077 if (0 > dpos && dpos > pos_before - pt_old)
14078 {
14079 pos_before = glyph->charpos;
14080 glyph_before = glyph;
14081 }
14082 else if (0 < dpos && dpos < pos_after - pt_old)
14083 {
14084 pos_after = glyph->charpos;
14085 glyph_after = glyph;
14086 }
14087 }
14088 else if (dpos == 0)
14089 match_with_avoid_cursor = 1;
14090 }
14091 else if (STRINGP (glyph->object))
14092 {
14093 Lisp_Object chprop;
14094 ptrdiff_t glyph_pos = glyph->charpos;
14095
14096 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14097 glyph->object);
14098 if (!NILP (chprop))
14099 {
14100 /* If the string came from a `display' text property,
14101 look up the buffer position of that property and
14102 use that position to update bpos_max, as if we
14103 actually saw such a position in one of the row's
14104 glyphs. This helps with supporting integer values
14105 of `cursor' property on the display string in
14106 situations where most or all of the row's buffer
14107 text is completely covered by display properties,
14108 so that no glyph with valid buffer positions is
14109 ever seen in the row. */
14110 ptrdiff_t prop_pos =
14111 string_buffer_position_lim (glyph->object, pos_before,
14112 pos_after, 0);
14113
14114 if (prop_pos >= pos_before)
14115 bpos_max = prop_pos - 1;
14116 }
14117 if (INTEGERP (chprop))
14118 {
14119 bpos_covered = bpos_max + XINT (chprop);
14120 /* If the `cursor' property covers buffer positions up
14121 to and including point, we should display cursor on
14122 this glyph. Note that, if a `cursor' property on one
14123 of the string's characters has an integer value, we
14124 will break out of the loop below _before_ we get to
14125 the position match above. IOW, integer values of
14126 the `cursor' property override the "exact match for
14127 point" strategy of positioning the cursor. */
14128 /* Implementation note: bpos_max == pt_old when, e.g.,
14129 we are in an empty line, where bpos_max is set to
14130 MATRIX_ROW_START_CHARPOS, see above. */
14131 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14132 {
14133 cursor = glyph;
14134 break;
14135 }
14136 }
14137
14138 string_seen = 1;
14139 }
14140 x += glyph->pixel_width;
14141 ++glyph;
14142 }
14143 else if (glyph > end) /* row is reversed */
14144 while (!INTEGERP (glyph->object))
14145 {
14146 if (BUFFERP (glyph->object))
14147 {
14148 ptrdiff_t dpos = glyph->charpos - pt_old;
14149
14150 if (glyph->charpos > bpos_max)
14151 bpos_max = glyph->charpos;
14152 if (glyph->charpos < bpos_min)
14153 bpos_min = glyph->charpos;
14154 if (!glyph->avoid_cursor_p)
14155 {
14156 if (dpos == 0)
14157 {
14158 match_with_avoid_cursor = 0;
14159 break;
14160 }
14161 if (0 > dpos && dpos > pos_before - pt_old)
14162 {
14163 pos_before = glyph->charpos;
14164 glyph_before = glyph;
14165 }
14166 else if (0 < dpos && dpos < pos_after - pt_old)
14167 {
14168 pos_after = glyph->charpos;
14169 glyph_after = glyph;
14170 }
14171 }
14172 else if (dpos == 0)
14173 match_with_avoid_cursor = 1;
14174 }
14175 else if (STRINGP (glyph->object))
14176 {
14177 Lisp_Object chprop;
14178 ptrdiff_t glyph_pos = glyph->charpos;
14179
14180 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14181 glyph->object);
14182 if (!NILP (chprop))
14183 {
14184 ptrdiff_t prop_pos =
14185 string_buffer_position_lim (glyph->object, pos_before,
14186 pos_after, 0);
14187
14188 if (prop_pos >= pos_before)
14189 bpos_max = prop_pos - 1;
14190 }
14191 if (INTEGERP (chprop))
14192 {
14193 bpos_covered = bpos_max + XINT (chprop);
14194 /* If the `cursor' property covers buffer positions up
14195 to and including point, we should display cursor on
14196 this glyph. */
14197 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14198 {
14199 cursor = glyph;
14200 break;
14201 }
14202 }
14203 string_seen = 1;
14204 }
14205 --glyph;
14206 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14207 {
14208 x--; /* can't use any pixel_width */
14209 break;
14210 }
14211 x -= glyph->pixel_width;
14212 }
14213
14214 /* Step 2: If we didn't find an exact match for point, we need to
14215 look for a proper place to put the cursor among glyphs between
14216 GLYPH_BEFORE and GLYPH_AFTER. */
14217 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14218 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14219 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14220 {
14221 /* An empty line has a single glyph whose OBJECT is zero and
14222 whose CHARPOS is the position of a newline on that line.
14223 Note that on a TTY, there are more glyphs after that, which
14224 were produced by extend_face_to_end_of_line, but their
14225 CHARPOS is zero or negative. */
14226 int empty_line_p =
14227 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14228 && INTEGERP (glyph->object) && glyph->charpos > 0
14229 /* On a TTY, continued and truncated rows also have a glyph at
14230 their end whose OBJECT is zero and whose CHARPOS is
14231 positive (the continuation and truncation glyphs), but such
14232 rows are obviously not "empty". */
14233 && !(row->continued_p || row->truncated_on_right_p);
14234
14235 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14236 {
14237 ptrdiff_t ellipsis_pos;
14238
14239 /* Scan back over the ellipsis glyphs. */
14240 if (!row->reversed_p)
14241 {
14242 ellipsis_pos = (glyph - 1)->charpos;
14243 while (glyph > row->glyphs[TEXT_AREA]
14244 && (glyph - 1)->charpos == ellipsis_pos)
14245 glyph--, x -= glyph->pixel_width;
14246 /* That loop always goes one position too far, including
14247 the glyph before the ellipsis. So scan forward over
14248 that one. */
14249 x += glyph->pixel_width;
14250 glyph++;
14251 }
14252 else /* row is reversed */
14253 {
14254 ellipsis_pos = (glyph + 1)->charpos;
14255 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14256 && (glyph + 1)->charpos == ellipsis_pos)
14257 glyph++, x += glyph->pixel_width;
14258 x -= glyph->pixel_width;
14259 glyph--;
14260 }
14261 }
14262 else if (match_with_avoid_cursor)
14263 {
14264 cursor = glyph_after;
14265 x = -1;
14266 }
14267 else if (string_seen)
14268 {
14269 int incr = row->reversed_p ? -1 : +1;
14270
14271 /* Need to find the glyph that came out of a string which is
14272 present at point. That glyph is somewhere between
14273 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14274 positioned between POS_BEFORE and POS_AFTER in the
14275 buffer. */
14276 struct glyph *start, *stop;
14277 ptrdiff_t pos = pos_before;
14278
14279 x = -1;
14280
14281 /* If the row ends in a newline from a display string,
14282 reordering could have moved the glyphs belonging to the
14283 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14284 in this case we extend the search to the last glyph in
14285 the row that was not inserted by redisplay. */
14286 if (row->ends_in_newline_from_string_p)
14287 {
14288 glyph_after = end;
14289 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14290 }
14291
14292 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14293 correspond to POS_BEFORE and POS_AFTER, respectively. We
14294 need START and STOP in the order that corresponds to the
14295 row's direction as given by its reversed_p flag. If the
14296 directionality of characters between POS_BEFORE and
14297 POS_AFTER is the opposite of the row's base direction,
14298 these characters will have been reordered for display,
14299 and we need to reverse START and STOP. */
14300 if (!row->reversed_p)
14301 {
14302 start = min (glyph_before, glyph_after);
14303 stop = max (glyph_before, glyph_after);
14304 }
14305 else
14306 {
14307 start = max (glyph_before, glyph_after);
14308 stop = min (glyph_before, glyph_after);
14309 }
14310 for (glyph = start + incr;
14311 row->reversed_p ? glyph > stop : glyph < stop; )
14312 {
14313
14314 /* Any glyphs that come from the buffer are here because
14315 of bidi reordering. Skip them, and only pay
14316 attention to glyphs that came from some string. */
14317 if (STRINGP (glyph->object))
14318 {
14319 Lisp_Object str;
14320 ptrdiff_t tem;
14321 /* If the display property covers the newline, we
14322 need to search for it one position farther. */
14323 ptrdiff_t lim = pos_after
14324 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14325
14326 string_from_text_prop = 0;
14327 str = glyph->object;
14328 tem = string_buffer_position_lim (str, pos, lim, 0);
14329 if (tem == 0 /* from overlay */
14330 || pos <= tem)
14331 {
14332 /* If the string from which this glyph came is
14333 found in the buffer at point, or at position
14334 that is closer to point than pos_after, then
14335 we've found the glyph we've been looking for.
14336 If it comes from an overlay (tem == 0), and
14337 it has the `cursor' property on one of its
14338 glyphs, record that glyph as a candidate for
14339 displaying the cursor. (As in the
14340 unidirectional version, we will display the
14341 cursor on the last candidate we find.) */
14342 if (tem == 0
14343 || tem == pt_old
14344 || (tem - pt_old > 0 && tem < pos_after))
14345 {
14346 /* The glyphs from this string could have
14347 been reordered. Find the one with the
14348 smallest string position. Or there could
14349 be a character in the string with the
14350 `cursor' property, which means display
14351 cursor on that character's glyph. */
14352 ptrdiff_t strpos = glyph->charpos;
14353
14354 if (tem)
14355 {
14356 cursor = glyph;
14357 string_from_text_prop = 1;
14358 }
14359 for ( ;
14360 (row->reversed_p ? glyph > stop : glyph < stop)
14361 && EQ (glyph->object, str);
14362 glyph += incr)
14363 {
14364 Lisp_Object cprop;
14365 ptrdiff_t gpos = glyph->charpos;
14366
14367 cprop = Fget_char_property (make_number (gpos),
14368 Qcursor,
14369 glyph->object);
14370 if (!NILP (cprop))
14371 {
14372 cursor = glyph;
14373 break;
14374 }
14375 if (tem && glyph->charpos < strpos)
14376 {
14377 strpos = glyph->charpos;
14378 cursor = glyph;
14379 }
14380 }
14381
14382 if (tem == pt_old
14383 || (tem - pt_old > 0 && tem < pos_after))
14384 goto compute_x;
14385 }
14386 if (tem)
14387 pos = tem + 1; /* don't find previous instances */
14388 }
14389 /* This string is not what we want; skip all of the
14390 glyphs that came from it. */
14391 while ((row->reversed_p ? glyph > stop : glyph < stop)
14392 && EQ (glyph->object, str))
14393 glyph += incr;
14394 }
14395 else
14396 glyph += incr;
14397 }
14398
14399 /* If we reached the end of the line, and END was from a string,
14400 the cursor is not on this line. */
14401 if (cursor == NULL
14402 && (row->reversed_p ? glyph <= end : glyph >= end)
14403 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14404 && STRINGP (end->object)
14405 && row->continued_p)
14406 return 0;
14407 }
14408 /* A truncated row may not include PT among its character positions.
14409 Setting the cursor inside the scroll margin will trigger
14410 recalculation of hscroll in hscroll_window_tree. But if a
14411 display string covers point, defer to the string-handling
14412 code below to figure this out. */
14413 else if (row->truncated_on_left_p && pt_old < bpos_min)
14414 {
14415 cursor = glyph_before;
14416 x = -1;
14417 }
14418 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14419 /* Zero-width characters produce no glyphs. */
14420 || (!empty_line_p
14421 && (row->reversed_p
14422 ? glyph_after > glyphs_end
14423 : glyph_after < glyphs_end)))
14424 {
14425 cursor = glyph_after;
14426 x = -1;
14427 }
14428 }
14429
14430 compute_x:
14431 if (cursor != NULL)
14432 glyph = cursor;
14433 else if (glyph == glyphs_end
14434 && pos_before == pos_after
14435 && STRINGP ((row->reversed_p
14436 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14437 : row->glyphs[TEXT_AREA])->object))
14438 {
14439 /* If all the glyphs of this row came from strings, put the
14440 cursor on the first glyph of the row. This avoids having the
14441 cursor outside of the text area in this very rare and hard
14442 use case. */
14443 glyph =
14444 row->reversed_p
14445 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14446 : row->glyphs[TEXT_AREA];
14447 }
14448 if (x < 0)
14449 {
14450 struct glyph *g;
14451
14452 /* Need to compute x that corresponds to GLYPH. */
14453 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14454 {
14455 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14456 emacs_abort ();
14457 x += g->pixel_width;
14458 }
14459 }
14460
14461 /* ROW could be part of a continued line, which, under bidi
14462 reordering, might have other rows whose start and end charpos
14463 occlude point. Only set w->cursor if we found a better
14464 approximation to the cursor position than we have from previously
14465 examined candidate rows belonging to the same continued line. */
14466 if (/* we already have a candidate row */
14467 w->cursor.vpos >= 0
14468 /* that candidate is not the row we are processing */
14469 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14470 /* Make sure cursor.vpos specifies a row whose start and end
14471 charpos occlude point, and it is valid candidate for being a
14472 cursor-row. This is because some callers of this function
14473 leave cursor.vpos at the row where the cursor was displayed
14474 during the last redisplay cycle. */
14475 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14476 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14477 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14478 {
14479 struct glyph *g1 =
14480 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14481
14482 /* Don't consider glyphs that are outside TEXT_AREA. */
14483 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14484 return 0;
14485 /* Keep the candidate whose buffer position is the closest to
14486 point or has the `cursor' property. */
14487 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14488 w->cursor.hpos >= 0
14489 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14490 && ((BUFFERP (g1->object)
14491 && (g1->charpos == pt_old /* an exact match always wins */
14492 || (BUFFERP (glyph->object)
14493 && eabs (g1->charpos - pt_old)
14494 < eabs (glyph->charpos - pt_old))))
14495 /* previous candidate is a glyph from a string that has
14496 a non-nil `cursor' property */
14497 || (STRINGP (g1->object)
14498 && (!NILP (Fget_char_property (make_number (g1->charpos),
14499 Qcursor, g1->object))
14500 /* previous candidate is from the same display
14501 string as this one, and the display string
14502 came from a text property */
14503 || (EQ (g1->object, glyph->object)
14504 && string_from_text_prop)
14505 /* this candidate is from newline and its
14506 position is not an exact match */
14507 || (INTEGERP (glyph->object)
14508 && glyph->charpos != pt_old)))))
14509 return 0;
14510 /* If this candidate gives an exact match, use that. */
14511 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14512 /* If this candidate is a glyph created for the
14513 terminating newline of a line, and point is on that
14514 newline, it wins because it's an exact match. */
14515 || (!row->continued_p
14516 && INTEGERP (glyph->object)
14517 && glyph->charpos == 0
14518 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14519 /* Otherwise, keep the candidate that comes from a row
14520 spanning less buffer positions. This may win when one or
14521 both candidate positions are on glyphs that came from
14522 display strings, for which we cannot compare buffer
14523 positions. */
14524 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14525 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14526 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14527 return 0;
14528 }
14529 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14530 w->cursor.x = x;
14531 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14532 w->cursor.y = row->y + dy;
14533
14534 if (w == XWINDOW (selected_window))
14535 {
14536 if (!row->continued_p
14537 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14538 && row->x == 0)
14539 {
14540 this_line_buffer = XBUFFER (w->contents);
14541
14542 CHARPOS (this_line_start_pos)
14543 = MATRIX_ROW_START_CHARPOS (row) + delta;
14544 BYTEPOS (this_line_start_pos)
14545 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14546
14547 CHARPOS (this_line_end_pos)
14548 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14549 BYTEPOS (this_line_end_pos)
14550 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14551
14552 this_line_y = w->cursor.y;
14553 this_line_pixel_height = row->height;
14554 this_line_vpos = w->cursor.vpos;
14555 this_line_start_x = row->x;
14556 }
14557 else
14558 CHARPOS (this_line_start_pos) = 0;
14559 }
14560
14561 return 1;
14562 }
14563
14564
14565 /* Run window scroll functions, if any, for WINDOW with new window
14566 start STARTP. Sets the window start of WINDOW to that position.
14567
14568 We assume that the window's buffer is really current. */
14569
14570 static struct text_pos
14571 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14572 {
14573 struct window *w = XWINDOW (window);
14574 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14575
14576 eassert (current_buffer == XBUFFER (w->contents));
14577
14578 if (!NILP (Vwindow_scroll_functions))
14579 {
14580 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14581 make_number (CHARPOS (startp)));
14582 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14583 /* In case the hook functions switch buffers. */
14584 set_buffer_internal (XBUFFER (w->contents));
14585 }
14586
14587 return startp;
14588 }
14589
14590
14591 /* Make sure the line containing the cursor is fully visible.
14592 A value of 1 means there is nothing to be done.
14593 (Either the line is fully visible, or it cannot be made so,
14594 or we cannot tell.)
14595
14596 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14597 is higher than window.
14598
14599 A value of 0 means the caller should do scrolling
14600 as if point had gone off the screen. */
14601
14602 static int
14603 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14604 {
14605 struct glyph_matrix *matrix;
14606 struct glyph_row *row;
14607 int window_height;
14608
14609 if (!make_cursor_line_fully_visible_p)
14610 return 1;
14611
14612 /* It's not always possible to find the cursor, e.g, when a window
14613 is full of overlay strings. Don't do anything in that case. */
14614 if (w->cursor.vpos < 0)
14615 return 1;
14616
14617 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14618 row = MATRIX_ROW (matrix, w->cursor.vpos);
14619
14620 /* If the cursor row is not partially visible, there's nothing to do. */
14621 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14622 return 1;
14623
14624 /* If the row the cursor is in is taller than the window's height,
14625 it's not clear what to do, so do nothing. */
14626 window_height = window_box_height (w);
14627 if (row->height >= window_height)
14628 {
14629 if (!force_p || MINI_WINDOW_P (w)
14630 || w->vscroll || w->cursor.vpos == 0)
14631 return 1;
14632 }
14633 return 0;
14634 }
14635
14636
14637 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14638 non-zero means only WINDOW is redisplayed in redisplay_internal.
14639 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14640 in redisplay_window to bring a partially visible line into view in
14641 the case that only the cursor has moved.
14642
14643 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14644 last screen line's vertical height extends past the end of the screen.
14645
14646 Value is
14647
14648 1 if scrolling succeeded
14649
14650 0 if scrolling didn't find point.
14651
14652 -1 if new fonts have been loaded so that we must interrupt
14653 redisplay, adjust glyph matrices, and try again. */
14654
14655 enum
14656 {
14657 SCROLLING_SUCCESS,
14658 SCROLLING_FAILED,
14659 SCROLLING_NEED_LARGER_MATRICES
14660 };
14661
14662 /* If scroll-conservatively is more than this, never recenter.
14663
14664 If you change this, don't forget to update the doc string of
14665 `scroll-conservatively' and the Emacs manual. */
14666 #define SCROLL_LIMIT 100
14667
14668 static int
14669 try_scrolling (Lisp_Object window, int just_this_one_p,
14670 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14671 int temp_scroll_step, int last_line_misfit)
14672 {
14673 struct window *w = XWINDOW (window);
14674 struct frame *f = XFRAME (w->frame);
14675 struct text_pos pos, startp;
14676 struct it it;
14677 int this_scroll_margin, scroll_max, rc, height;
14678 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14679 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14680 Lisp_Object aggressive;
14681 /* We will never try scrolling more than this number of lines. */
14682 int scroll_limit = SCROLL_LIMIT;
14683 int frame_line_height = default_line_pixel_height (w);
14684 int window_total_lines
14685 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14686
14687 #ifdef GLYPH_DEBUG
14688 debug_method_add (w, "try_scrolling");
14689 #endif
14690
14691 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14692
14693 /* Compute scroll margin height in pixels. We scroll when point is
14694 within this distance from the top or bottom of the window. */
14695 if (scroll_margin > 0)
14696 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14697 * frame_line_height;
14698 else
14699 this_scroll_margin = 0;
14700
14701 /* Force arg_scroll_conservatively to have a reasonable value, to
14702 avoid scrolling too far away with slow move_it_* functions. Note
14703 that the user can supply scroll-conservatively equal to
14704 `most-positive-fixnum', which can be larger than INT_MAX. */
14705 if (arg_scroll_conservatively > scroll_limit)
14706 {
14707 arg_scroll_conservatively = scroll_limit + 1;
14708 scroll_max = scroll_limit * frame_line_height;
14709 }
14710 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14711 /* Compute how much we should try to scroll maximally to bring
14712 point into view. */
14713 scroll_max = (max (scroll_step,
14714 max (arg_scroll_conservatively, temp_scroll_step))
14715 * frame_line_height);
14716 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14717 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14718 /* We're trying to scroll because of aggressive scrolling but no
14719 scroll_step is set. Choose an arbitrary one. */
14720 scroll_max = 10 * frame_line_height;
14721 else
14722 scroll_max = 0;
14723
14724 too_near_end:
14725
14726 /* Decide whether to scroll down. */
14727 if (PT > CHARPOS (startp))
14728 {
14729 int scroll_margin_y;
14730
14731 /* Compute the pixel ypos of the scroll margin, then move IT to
14732 either that ypos or PT, whichever comes first. */
14733 start_display (&it, w, startp);
14734 scroll_margin_y = it.last_visible_y - this_scroll_margin
14735 - frame_line_height * extra_scroll_margin_lines;
14736 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14737 (MOVE_TO_POS | MOVE_TO_Y));
14738
14739 if (PT > CHARPOS (it.current.pos))
14740 {
14741 int y0 = line_bottom_y (&it);
14742 /* Compute how many pixels below window bottom to stop searching
14743 for PT. This avoids costly search for PT that is far away if
14744 the user limited scrolling by a small number of lines, but
14745 always finds PT if scroll_conservatively is set to a large
14746 number, such as most-positive-fixnum. */
14747 int slack = max (scroll_max, 10 * frame_line_height);
14748 int y_to_move = it.last_visible_y + slack;
14749
14750 /* Compute the distance from the scroll margin to PT or to
14751 the scroll limit, whichever comes first. This should
14752 include the height of the cursor line, to make that line
14753 fully visible. */
14754 move_it_to (&it, PT, -1, y_to_move,
14755 -1, MOVE_TO_POS | MOVE_TO_Y);
14756 dy = line_bottom_y (&it) - y0;
14757
14758 if (dy > scroll_max)
14759 return SCROLLING_FAILED;
14760
14761 if (dy > 0)
14762 scroll_down_p = 1;
14763 }
14764 }
14765
14766 if (scroll_down_p)
14767 {
14768 /* Point is in or below the bottom scroll margin, so move the
14769 window start down. If scrolling conservatively, move it just
14770 enough down to make point visible. If scroll_step is set,
14771 move it down by scroll_step. */
14772 if (arg_scroll_conservatively)
14773 amount_to_scroll
14774 = min (max (dy, frame_line_height),
14775 frame_line_height * arg_scroll_conservatively);
14776 else if (scroll_step || temp_scroll_step)
14777 amount_to_scroll = scroll_max;
14778 else
14779 {
14780 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14781 height = WINDOW_BOX_TEXT_HEIGHT (w);
14782 if (NUMBERP (aggressive))
14783 {
14784 double float_amount = XFLOATINT (aggressive) * height;
14785 int aggressive_scroll = float_amount;
14786 if (aggressive_scroll == 0 && float_amount > 0)
14787 aggressive_scroll = 1;
14788 /* Don't let point enter the scroll margin near top of
14789 the window. This could happen if the value of
14790 scroll_up_aggressively is too large and there are
14791 non-zero margins, because scroll_up_aggressively
14792 means put point that fraction of window height
14793 _from_the_bottom_margin_. */
14794 if (aggressive_scroll + 2*this_scroll_margin > height)
14795 aggressive_scroll = height - 2*this_scroll_margin;
14796 amount_to_scroll = dy + aggressive_scroll;
14797 }
14798 }
14799
14800 if (amount_to_scroll <= 0)
14801 return SCROLLING_FAILED;
14802
14803 start_display (&it, w, startp);
14804 if (arg_scroll_conservatively <= scroll_limit)
14805 move_it_vertically (&it, amount_to_scroll);
14806 else
14807 {
14808 /* Extra precision for users who set scroll-conservatively
14809 to a large number: make sure the amount we scroll
14810 the window start is never less than amount_to_scroll,
14811 which was computed as distance from window bottom to
14812 point. This matters when lines at window top and lines
14813 below window bottom have different height. */
14814 struct it it1;
14815 void *it1data = NULL;
14816 /* We use a temporary it1 because line_bottom_y can modify
14817 its argument, if it moves one line down; see there. */
14818 int start_y;
14819
14820 SAVE_IT (it1, it, it1data);
14821 start_y = line_bottom_y (&it1);
14822 do {
14823 RESTORE_IT (&it, &it, it1data);
14824 move_it_by_lines (&it, 1);
14825 SAVE_IT (it1, it, it1data);
14826 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14827 }
14828
14829 /* If STARTP is unchanged, move it down another screen line. */
14830 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14831 move_it_by_lines (&it, 1);
14832 startp = it.current.pos;
14833 }
14834 else
14835 {
14836 struct text_pos scroll_margin_pos = startp;
14837 int y_offset = 0;
14838
14839 /* See if point is inside the scroll margin at the top of the
14840 window. */
14841 if (this_scroll_margin)
14842 {
14843 int y_start;
14844
14845 start_display (&it, w, startp);
14846 y_start = it.current_y;
14847 move_it_vertically (&it, this_scroll_margin);
14848 scroll_margin_pos = it.current.pos;
14849 /* If we didn't move enough before hitting ZV, request
14850 additional amount of scroll, to move point out of the
14851 scroll margin. */
14852 if (IT_CHARPOS (it) == ZV
14853 && it.current_y - y_start < this_scroll_margin)
14854 y_offset = this_scroll_margin - (it.current_y - y_start);
14855 }
14856
14857 if (PT < CHARPOS (scroll_margin_pos))
14858 {
14859 /* Point is in the scroll margin at the top of the window or
14860 above what is displayed in the window. */
14861 int y0, y_to_move;
14862
14863 /* Compute the vertical distance from PT to the scroll
14864 margin position. Move as far as scroll_max allows, or
14865 one screenful, or 10 screen lines, whichever is largest.
14866 Give up if distance is greater than scroll_max or if we
14867 didn't reach the scroll margin position. */
14868 SET_TEXT_POS (pos, PT, PT_BYTE);
14869 start_display (&it, w, pos);
14870 y0 = it.current_y;
14871 y_to_move = max (it.last_visible_y,
14872 max (scroll_max, 10 * frame_line_height));
14873 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14874 y_to_move, -1,
14875 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14876 dy = it.current_y - y0;
14877 if (dy > scroll_max
14878 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14879 return SCROLLING_FAILED;
14880
14881 /* Additional scroll for when ZV was too close to point. */
14882 dy += y_offset;
14883
14884 /* Compute new window start. */
14885 start_display (&it, w, startp);
14886
14887 if (arg_scroll_conservatively)
14888 amount_to_scroll = max (dy, frame_line_height *
14889 max (scroll_step, temp_scroll_step));
14890 else if (scroll_step || temp_scroll_step)
14891 amount_to_scroll = scroll_max;
14892 else
14893 {
14894 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14895 height = WINDOW_BOX_TEXT_HEIGHT (w);
14896 if (NUMBERP (aggressive))
14897 {
14898 double float_amount = XFLOATINT (aggressive) * height;
14899 int aggressive_scroll = float_amount;
14900 if (aggressive_scroll == 0 && float_amount > 0)
14901 aggressive_scroll = 1;
14902 /* Don't let point enter the scroll margin near
14903 bottom of the window, if the value of
14904 scroll_down_aggressively happens to be too
14905 large. */
14906 if (aggressive_scroll + 2*this_scroll_margin > height)
14907 aggressive_scroll = height - 2*this_scroll_margin;
14908 amount_to_scroll = dy + aggressive_scroll;
14909 }
14910 }
14911
14912 if (amount_to_scroll <= 0)
14913 return SCROLLING_FAILED;
14914
14915 move_it_vertically_backward (&it, amount_to_scroll);
14916 startp = it.current.pos;
14917 }
14918 }
14919
14920 /* Run window scroll functions. */
14921 startp = run_window_scroll_functions (window, startp);
14922
14923 /* Display the window. Give up if new fonts are loaded, or if point
14924 doesn't appear. */
14925 if (!try_window (window, startp, 0))
14926 rc = SCROLLING_NEED_LARGER_MATRICES;
14927 else if (w->cursor.vpos < 0)
14928 {
14929 clear_glyph_matrix (w->desired_matrix);
14930 rc = SCROLLING_FAILED;
14931 }
14932 else
14933 {
14934 /* Maybe forget recorded base line for line number display. */
14935 if (!just_this_one_p
14936 || current_buffer->clip_changed
14937 || BEG_UNCHANGED < CHARPOS (startp))
14938 w->base_line_number = 0;
14939
14940 /* If cursor ends up on a partially visible line,
14941 treat that as being off the bottom of the screen. */
14942 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14943 /* It's possible that the cursor is on the first line of the
14944 buffer, which is partially obscured due to a vscroll
14945 (Bug#7537). In that case, avoid looping forever . */
14946 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14947 {
14948 clear_glyph_matrix (w->desired_matrix);
14949 ++extra_scroll_margin_lines;
14950 goto too_near_end;
14951 }
14952 rc = SCROLLING_SUCCESS;
14953 }
14954
14955 return rc;
14956 }
14957
14958
14959 /* Compute a suitable window start for window W if display of W starts
14960 on a continuation line. Value is non-zero if a new window start
14961 was computed.
14962
14963 The new window start will be computed, based on W's width, starting
14964 from the start of the continued line. It is the start of the
14965 screen line with the minimum distance from the old start W->start. */
14966
14967 static int
14968 compute_window_start_on_continuation_line (struct window *w)
14969 {
14970 struct text_pos pos, start_pos;
14971 int window_start_changed_p = 0;
14972
14973 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14974
14975 /* If window start is on a continuation line... Window start may be
14976 < BEGV in case there's invisible text at the start of the
14977 buffer (M-x rmail, for example). */
14978 if (CHARPOS (start_pos) > BEGV
14979 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14980 {
14981 struct it it;
14982 struct glyph_row *row;
14983
14984 /* Handle the case that the window start is out of range. */
14985 if (CHARPOS (start_pos) < BEGV)
14986 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14987 else if (CHARPOS (start_pos) > ZV)
14988 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14989
14990 /* Find the start of the continued line. This should be fast
14991 because find_newline is fast (newline cache). */
14992 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14993 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14994 row, DEFAULT_FACE_ID);
14995 reseat_at_previous_visible_line_start (&it);
14996
14997 /* If the line start is "too far" away from the window start,
14998 say it takes too much time to compute a new window start. */
14999 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15000 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15001 {
15002 int min_distance, distance;
15003
15004 /* Move forward by display lines to find the new window
15005 start. If window width was enlarged, the new start can
15006 be expected to be > the old start. If window width was
15007 decreased, the new window start will be < the old start.
15008 So, we're looking for the display line start with the
15009 minimum distance from the old window start. */
15010 pos = it.current.pos;
15011 min_distance = INFINITY;
15012 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15013 distance < min_distance)
15014 {
15015 min_distance = distance;
15016 pos = it.current.pos;
15017 if (it.line_wrap == WORD_WRAP)
15018 {
15019 /* Under WORD_WRAP, move_it_by_lines is likely to
15020 overshoot and stop not at the first, but the
15021 second character from the left margin. So in
15022 that case, we need a more tight control on the X
15023 coordinate of the iterator than move_it_by_lines
15024 promises in its contract. The method is to first
15025 go to the last (rightmost) visible character of a
15026 line, then move to the leftmost character on the
15027 next line in a separate call. */
15028 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15029 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15030 move_it_to (&it, ZV, 0,
15031 it.current_y + it.max_ascent + it.max_descent, -1,
15032 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15033 }
15034 else
15035 move_it_by_lines (&it, 1);
15036 }
15037
15038 /* Set the window start there. */
15039 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15040 window_start_changed_p = 1;
15041 }
15042 }
15043
15044 return window_start_changed_p;
15045 }
15046
15047
15048 /* Try cursor movement in case text has not changed in window WINDOW,
15049 with window start STARTP. Value is
15050
15051 CURSOR_MOVEMENT_SUCCESS if successful
15052
15053 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15054
15055 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15056 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15057 we want to scroll as if scroll-step were set to 1. See the code.
15058
15059 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15060 which case we have to abort this redisplay, and adjust matrices
15061 first. */
15062
15063 enum
15064 {
15065 CURSOR_MOVEMENT_SUCCESS,
15066 CURSOR_MOVEMENT_CANNOT_BE_USED,
15067 CURSOR_MOVEMENT_MUST_SCROLL,
15068 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15069 };
15070
15071 static int
15072 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15073 {
15074 struct window *w = XWINDOW (window);
15075 struct frame *f = XFRAME (w->frame);
15076 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15077
15078 #ifdef GLYPH_DEBUG
15079 if (inhibit_try_cursor_movement)
15080 return rc;
15081 #endif
15082
15083 /* Previously, there was a check for Lisp integer in the
15084 if-statement below. Now, this field is converted to
15085 ptrdiff_t, thus zero means invalid position in a buffer. */
15086 eassert (w->last_point > 0);
15087 /* Likewise there was a check whether window_end_vpos is nil or larger
15088 than the window. Now window_end_vpos is int and so never nil, but
15089 let's leave eassert to check whether it fits in the window. */
15090 eassert (w->window_end_vpos < w->current_matrix->nrows);
15091
15092 /* Handle case where text has not changed, only point, and it has
15093 not moved off the frame. */
15094 if (/* Point may be in this window. */
15095 PT >= CHARPOS (startp)
15096 /* Selective display hasn't changed. */
15097 && !current_buffer->clip_changed
15098 /* Function force-mode-line-update is used to force a thorough
15099 redisplay. It sets either windows_or_buffers_changed or
15100 update_mode_lines. So don't take a shortcut here for these
15101 cases. */
15102 && !update_mode_lines
15103 && !windows_or_buffers_changed
15104 && !cursor_type_changed
15105 /* Can't use this case if highlighting a region. When a
15106 region exists, cursor movement has to do more than just
15107 set the cursor. */
15108 && markpos_of_region () < 0
15109 && !w->region_showing
15110 && NILP (Vshow_trailing_whitespace)
15111 /* This code is not used for mini-buffer for the sake of the case
15112 of redisplaying to replace an echo area message; since in
15113 that case the mini-buffer contents per se are usually
15114 unchanged. This code is of no real use in the mini-buffer
15115 since the handling of this_line_start_pos, etc., in redisplay
15116 handles the same cases. */
15117 && !EQ (window, minibuf_window)
15118 && (FRAME_WINDOW_P (f)
15119 || !overlay_arrow_in_current_buffer_p ()))
15120 {
15121 int this_scroll_margin, top_scroll_margin;
15122 struct glyph_row *row = NULL;
15123 int frame_line_height = default_line_pixel_height (w);
15124 int window_total_lines
15125 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15126
15127 #ifdef GLYPH_DEBUG
15128 debug_method_add (w, "cursor movement");
15129 #endif
15130
15131 /* Scroll if point within this distance from the top or bottom
15132 of the window. This is a pixel value. */
15133 if (scroll_margin > 0)
15134 {
15135 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15136 this_scroll_margin *= frame_line_height;
15137 }
15138 else
15139 this_scroll_margin = 0;
15140
15141 top_scroll_margin = this_scroll_margin;
15142 if (WINDOW_WANTS_HEADER_LINE_P (w))
15143 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15144
15145 /* Start with the row the cursor was displayed during the last
15146 not paused redisplay. Give up if that row is not valid. */
15147 if (w->last_cursor.vpos < 0
15148 || w->last_cursor.vpos >= w->current_matrix->nrows)
15149 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15150 else
15151 {
15152 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15153 if (row->mode_line_p)
15154 ++row;
15155 if (!row->enabled_p)
15156 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15157 }
15158
15159 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15160 {
15161 int scroll_p = 0, must_scroll = 0;
15162 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15163
15164 if (PT > w->last_point)
15165 {
15166 /* Point has moved forward. */
15167 while (MATRIX_ROW_END_CHARPOS (row) < PT
15168 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15169 {
15170 eassert (row->enabled_p);
15171 ++row;
15172 }
15173
15174 /* If the end position of a row equals the start
15175 position of the next row, and PT is at that position,
15176 we would rather display cursor in the next line. */
15177 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15178 && MATRIX_ROW_END_CHARPOS (row) == PT
15179 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15180 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15181 && !cursor_row_p (row))
15182 ++row;
15183
15184 /* If within the scroll margin, scroll. Note that
15185 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15186 the next line would be drawn, and that
15187 this_scroll_margin can be zero. */
15188 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15189 || PT > MATRIX_ROW_END_CHARPOS (row)
15190 /* Line is completely visible last line in window
15191 and PT is to be set in the next line. */
15192 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15193 && PT == MATRIX_ROW_END_CHARPOS (row)
15194 && !row->ends_at_zv_p
15195 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15196 scroll_p = 1;
15197 }
15198 else if (PT < w->last_point)
15199 {
15200 /* Cursor has to be moved backward. Note that PT >=
15201 CHARPOS (startp) because of the outer if-statement. */
15202 while (!row->mode_line_p
15203 && (MATRIX_ROW_START_CHARPOS (row) > PT
15204 || (MATRIX_ROW_START_CHARPOS (row) == PT
15205 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15206 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15207 row > w->current_matrix->rows
15208 && (row-1)->ends_in_newline_from_string_p))))
15209 && (row->y > top_scroll_margin
15210 || CHARPOS (startp) == BEGV))
15211 {
15212 eassert (row->enabled_p);
15213 --row;
15214 }
15215
15216 /* Consider the following case: Window starts at BEGV,
15217 there is invisible, intangible text at BEGV, so that
15218 display starts at some point START > BEGV. It can
15219 happen that we are called with PT somewhere between
15220 BEGV and START. Try to handle that case. */
15221 if (row < w->current_matrix->rows
15222 || row->mode_line_p)
15223 {
15224 row = w->current_matrix->rows;
15225 if (row->mode_line_p)
15226 ++row;
15227 }
15228
15229 /* Due to newlines in overlay strings, we may have to
15230 skip forward over overlay strings. */
15231 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15232 && MATRIX_ROW_END_CHARPOS (row) == PT
15233 && !cursor_row_p (row))
15234 ++row;
15235
15236 /* If within the scroll margin, scroll. */
15237 if (row->y < top_scroll_margin
15238 && CHARPOS (startp) != BEGV)
15239 scroll_p = 1;
15240 }
15241 else
15242 {
15243 /* Cursor did not move. So don't scroll even if cursor line
15244 is partially visible, as it was so before. */
15245 rc = CURSOR_MOVEMENT_SUCCESS;
15246 }
15247
15248 if (PT < MATRIX_ROW_START_CHARPOS (row)
15249 || PT > MATRIX_ROW_END_CHARPOS (row))
15250 {
15251 /* if PT is not in the glyph row, give up. */
15252 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15253 must_scroll = 1;
15254 }
15255 else if (rc != CURSOR_MOVEMENT_SUCCESS
15256 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15257 {
15258 struct glyph_row *row1;
15259
15260 /* If rows are bidi-reordered and point moved, back up
15261 until we find a row that does not belong to a
15262 continuation line. This is because we must consider
15263 all rows of a continued line as candidates for the
15264 new cursor positioning, since row start and end
15265 positions change non-linearly with vertical position
15266 in such rows. */
15267 /* FIXME: Revisit this when glyph ``spilling'' in
15268 continuation lines' rows is implemented for
15269 bidi-reordered rows. */
15270 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15271 MATRIX_ROW_CONTINUATION_LINE_P (row);
15272 --row)
15273 {
15274 /* If we hit the beginning of the displayed portion
15275 without finding the first row of a continued
15276 line, give up. */
15277 if (row <= row1)
15278 {
15279 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15280 break;
15281 }
15282 eassert (row->enabled_p);
15283 }
15284 }
15285 if (must_scroll)
15286 ;
15287 else if (rc != CURSOR_MOVEMENT_SUCCESS
15288 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15289 /* Make sure this isn't a header line by any chance, since
15290 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15291 && !row->mode_line_p
15292 && make_cursor_line_fully_visible_p)
15293 {
15294 if (PT == MATRIX_ROW_END_CHARPOS (row)
15295 && !row->ends_at_zv_p
15296 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15297 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15298 else if (row->height > window_box_height (w))
15299 {
15300 /* If we end up in a partially visible line, let's
15301 make it fully visible, except when it's taller
15302 than the window, in which case we can't do much
15303 about it. */
15304 *scroll_step = 1;
15305 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15306 }
15307 else
15308 {
15309 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15310 if (!cursor_row_fully_visible_p (w, 0, 1))
15311 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15312 else
15313 rc = CURSOR_MOVEMENT_SUCCESS;
15314 }
15315 }
15316 else if (scroll_p)
15317 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15318 else if (rc != CURSOR_MOVEMENT_SUCCESS
15319 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15320 {
15321 /* With bidi-reordered rows, there could be more than
15322 one candidate row whose start and end positions
15323 occlude point. We need to let set_cursor_from_row
15324 find the best candidate. */
15325 /* FIXME: Revisit this when glyph ``spilling'' in
15326 continuation lines' rows is implemented for
15327 bidi-reordered rows. */
15328 int rv = 0;
15329
15330 do
15331 {
15332 int at_zv_p = 0, exact_match_p = 0;
15333
15334 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15335 && PT <= MATRIX_ROW_END_CHARPOS (row)
15336 && cursor_row_p (row))
15337 rv |= set_cursor_from_row (w, row, w->current_matrix,
15338 0, 0, 0, 0);
15339 /* As soon as we've found the exact match for point,
15340 or the first suitable row whose ends_at_zv_p flag
15341 is set, we are done. */
15342 at_zv_p =
15343 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15344 if (rv && !at_zv_p
15345 && w->cursor.hpos >= 0
15346 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15347 w->cursor.vpos))
15348 {
15349 struct glyph_row *candidate =
15350 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15351 struct glyph *g =
15352 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15353 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15354
15355 exact_match_p =
15356 (BUFFERP (g->object) && g->charpos == PT)
15357 || (INTEGERP (g->object)
15358 && (g->charpos == PT
15359 || (g->charpos == 0 && endpos - 1 == PT)));
15360 }
15361 if (rv && (at_zv_p || exact_match_p))
15362 {
15363 rc = CURSOR_MOVEMENT_SUCCESS;
15364 break;
15365 }
15366 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15367 break;
15368 ++row;
15369 }
15370 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15371 || row->continued_p)
15372 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15373 || (MATRIX_ROW_START_CHARPOS (row) == PT
15374 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15375 /* If we didn't find any candidate rows, or exited the
15376 loop before all the candidates were examined, signal
15377 to the caller that this method failed. */
15378 if (rc != CURSOR_MOVEMENT_SUCCESS
15379 && !(rv
15380 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15381 && !row->continued_p))
15382 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15383 else if (rv)
15384 rc = CURSOR_MOVEMENT_SUCCESS;
15385 }
15386 else
15387 {
15388 do
15389 {
15390 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15391 {
15392 rc = CURSOR_MOVEMENT_SUCCESS;
15393 break;
15394 }
15395 ++row;
15396 }
15397 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15398 && MATRIX_ROW_START_CHARPOS (row) == PT
15399 && cursor_row_p (row));
15400 }
15401 }
15402 }
15403
15404 return rc;
15405 }
15406
15407 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15408 static
15409 #endif
15410 void
15411 set_vertical_scroll_bar (struct window *w)
15412 {
15413 ptrdiff_t start, end, whole;
15414
15415 /* Calculate the start and end positions for the current window.
15416 At some point, it would be nice to choose between scrollbars
15417 which reflect the whole buffer size, with special markers
15418 indicating narrowing, and scrollbars which reflect only the
15419 visible region.
15420
15421 Note that mini-buffers sometimes aren't displaying any text. */
15422 if (!MINI_WINDOW_P (w)
15423 || (w == XWINDOW (minibuf_window)
15424 && NILP (echo_area_buffer[0])))
15425 {
15426 struct buffer *buf = XBUFFER (w->contents);
15427 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15428 start = marker_position (w->start) - BUF_BEGV (buf);
15429 /* I don't think this is guaranteed to be right. For the
15430 moment, we'll pretend it is. */
15431 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15432
15433 if (end < start)
15434 end = start;
15435 if (whole < (end - start))
15436 whole = end - start;
15437 }
15438 else
15439 start = end = whole = 0;
15440
15441 /* Indicate what this scroll bar ought to be displaying now. */
15442 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15443 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15444 (w, end - start, whole, start);
15445 }
15446
15447
15448 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15449 selected_window is redisplayed.
15450
15451 We can return without actually redisplaying the window if
15452 fonts_changed_p. In that case, redisplay_internal will
15453 retry. */
15454
15455 static void
15456 redisplay_window (Lisp_Object window, int just_this_one_p)
15457 {
15458 struct window *w = XWINDOW (window);
15459 struct frame *f = XFRAME (w->frame);
15460 struct buffer *buffer = XBUFFER (w->contents);
15461 struct buffer *old = current_buffer;
15462 struct text_pos lpoint, opoint, startp;
15463 int update_mode_line;
15464 int tem;
15465 struct it it;
15466 /* Record it now because it's overwritten. */
15467 int current_matrix_up_to_date_p = 0;
15468 int used_current_matrix_p = 0;
15469 /* This is less strict than current_matrix_up_to_date_p.
15470 It indicates that the buffer contents and narrowing are unchanged. */
15471 int buffer_unchanged_p = 0;
15472 int temp_scroll_step = 0;
15473 ptrdiff_t count = SPECPDL_INDEX ();
15474 int rc;
15475 int centering_position = -1;
15476 int last_line_misfit = 0;
15477 ptrdiff_t beg_unchanged, end_unchanged;
15478 int frame_line_height;
15479
15480 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15481 opoint = lpoint;
15482
15483 #ifdef GLYPH_DEBUG
15484 *w->desired_matrix->method = 0;
15485 #endif
15486
15487 /* Make sure that both W's markers are valid. */
15488 eassert (XMARKER (w->start)->buffer == buffer);
15489 eassert (XMARKER (w->pointm)->buffer == buffer);
15490
15491 restart:
15492 reconsider_clip_changes (w);
15493 frame_line_height = default_line_pixel_height (w);
15494
15495 /* Has the mode line to be updated? */
15496 update_mode_line = (w->update_mode_line
15497 || update_mode_lines
15498 || buffer->clip_changed
15499 || buffer->prevent_redisplay_optimizations_p);
15500
15501 if (MINI_WINDOW_P (w))
15502 {
15503 if (w == XWINDOW (echo_area_window)
15504 && !NILP (echo_area_buffer[0]))
15505 {
15506 if (update_mode_line)
15507 /* We may have to update a tty frame's menu bar or a
15508 tool-bar. Example `M-x C-h C-h C-g'. */
15509 goto finish_menu_bars;
15510 else
15511 /* We've already displayed the echo area glyphs in this window. */
15512 goto finish_scroll_bars;
15513 }
15514 else if ((w != XWINDOW (minibuf_window)
15515 || minibuf_level == 0)
15516 /* When buffer is nonempty, redisplay window normally. */
15517 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15518 /* Quail displays non-mini buffers in minibuffer window.
15519 In that case, redisplay the window normally. */
15520 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15521 {
15522 /* W is a mini-buffer window, but it's not active, so clear
15523 it. */
15524 int yb = window_text_bottom_y (w);
15525 struct glyph_row *row;
15526 int y;
15527
15528 for (y = 0, row = w->desired_matrix->rows;
15529 y < yb;
15530 y += row->height, ++row)
15531 blank_row (w, row, y);
15532 goto finish_scroll_bars;
15533 }
15534
15535 clear_glyph_matrix (w->desired_matrix);
15536 }
15537
15538 /* Otherwise set up data on this window; select its buffer and point
15539 value. */
15540 /* Really select the buffer, for the sake of buffer-local
15541 variables. */
15542 set_buffer_internal_1 (XBUFFER (w->contents));
15543
15544 current_matrix_up_to_date_p
15545 = (w->window_end_valid
15546 && !current_buffer->clip_changed
15547 && !current_buffer->prevent_redisplay_optimizations_p
15548 && !window_outdated (w));
15549
15550 /* Run the window-bottom-change-functions
15551 if it is possible that the text on the screen has changed
15552 (either due to modification of the text, or any other reason). */
15553 if (!current_matrix_up_to_date_p
15554 && !NILP (Vwindow_text_change_functions))
15555 {
15556 safe_run_hooks (Qwindow_text_change_functions);
15557 goto restart;
15558 }
15559
15560 beg_unchanged = BEG_UNCHANGED;
15561 end_unchanged = END_UNCHANGED;
15562
15563 SET_TEXT_POS (opoint, PT, PT_BYTE);
15564
15565 specbind (Qinhibit_point_motion_hooks, Qt);
15566
15567 buffer_unchanged_p
15568 = (w->window_end_valid
15569 && !current_buffer->clip_changed
15570 && !window_outdated (w));
15571
15572 /* When windows_or_buffers_changed is non-zero, we can't rely
15573 on the window end being valid, so set it to zero there. */
15574 if (windows_or_buffers_changed)
15575 {
15576 /* If window starts on a continuation line, maybe adjust the
15577 window start in case the window's width changed. */
15578 if (XMARKER (w->start)->buffer == current_buffer)
15579 compute_window_start_on_continuation_line (w);
15580
15581 w->window_end_valid = 0;
15582 /* If so, we also can't rely on current matrix
15583 and should not fool try_cursor_movement below. */
15584 current_matrix_up_to_date_p = 0;
15585 }
15586
15587 /* Some sanity checks. */
15588 CHECK_WINDOW_END (w);
15589 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15590 emacs_abort ();
15591 if (BYTEPOS (opoint) < CHARPOS (opoint))
15592 emacs_abort ();
15593
15594 if (mode_line_update_needed (w))
15595 update_mode_line = 1;
15596
15597 /* Point refers normally to the selected window. For any other
15598 window, set up appropriate value. */
15599 if (!EQ (window, selected_window))
15600 {
15601 ptrdiff_t new_pt = marker_position (w->pointm);
15602 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15603 if (new_pt < BEGV)
15604 {
15605 new_pt = BEGV;
15606 new_pt_byte = BEGV_BYTE;
15607 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15608 }
15609 else if (new_pt > (ZV - 1))
15610 {
15611 new_pt = ZV;
15612 new_pt_byte = ZV_BYTE;
15613 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15614 }
15615
15616 /* We don't use SET_PT so that the point-motion hooks don't run. */
15617 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15618 }
15619
15620 /* If any of the character widths specified in the display table
15621 have changed, invalidate the width run cache. It's true that
15622 this may be a bit late to catch such changes, but the rest of
15623 redisplay goes (non-fatally) haywire when the display table is
15624 changed, so why should we worry about doing any better? */
15625 if (current_buffer->width_run_cache)
15626 {
15627 struct Lisp_Char_Table *disptab = buffer_display_table ();
15628
15629 if (! disptab_matches_widthtab
15630 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15631 {
15632 invalidate_region_cache (current_buffer,
15633 current_buffer->width_run_cache,
15634 BEG, Z);
15635 recompute_width_table (current_buffer, disptab);
15636 }
15637 }
15638
15639 /* If window-start is screwed up, choose a new one. */
15640 if (XMARKER (w->start)->buffer != current_buffer)
15641 goto recenter;
15642
15643 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15644
15645 /* If someone specified a new starting point but did not insist,
15646 check whether it can be used. */
15647 if (w->optional_new_start
15648 && CHARPOS (startp) >= BEGV
15649 && CHARPOS (startp) <= ZV)
15650 {
15651 w->optional_new_start = 0;
15652 start_display (&it, w, startp);
15653 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15654 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15655 if (IT_CHARPOS (it) == PT)
15656 w->force_start = 1;
15657 /* IT may overshoot PT if text at PT is invisible. */
15658 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15659 w->force_start = 1;
15660 }
15661
15662 force_start:
15663
15664 /* Handle case where place to start displaying has been specified,
15665 unless the specified location is outside the accessible range. */
15666 if (w->force_start || window_frozen_p (w))
15667 {
15668 /* We set this later on if we have to adjust point. */
15669 int new_vpos = -1;
15670
15671 w->force_start = 0;
15672 w->vscroll = 0;
15673 w->window_end_valid = 0;
15674
15675 /* Forget any recorded base line for line number display. */
15676 if (!buffer_unchanged_p)
15677 w->base_line_number = 0;
15678
15679 /* Redisplay the mode line. Select the buffer properly for that.
15680 Also, run the hook window-scroll-functions
15681 because we have scrolled. */
15682 /* Note, we do this after clearing force_start because
15683 if there's an error, it is better to forget about force_start
15684 than to get into an infinite loop calling the hook functions
15685 and having them get more errors. */
15686 if (!update_mode_line
15687 || ! NILP (Vwindow_scroll_functions))
15688 {
15689 update_mode_line = 1;
15690 w->update_mode_line = 1;
15691 startp = run_window_scroll_functions (window, startp);
15692 }
15693
15694 if (CHARPOS (startp) < BEGV)
15695 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15696 else if (CHARPOS (startp) > ZV)
15697 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15698
15699 /* Redisplay, then check if cursor has been set during the
15700 redisplay. Give up if new fonts were loaded. */
15701 /* We used to issue a CHECK_MARGINS argument to try_window here,
15702 but this causes scrolling to fail when point begins inside
15703 the scroll margin (bug#148) -- cyd */
15704 if (!try_window (window, startp, 0))
15705 {
15706 w->force_start = 1;
15707 clear_glyph_matrix (w->desired_matrix);
15708 goto need_larger_matrices;
15709 }
15710
15711 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15712 {
15713 /* If point does not appear, try to move point so it does
15714 appear. The desired matrix has been built above, so we
15715 can use it here. */
15716 new_vpos = window_box_height (w) / 2;
15717 }
15718
15719 if (!cursor_row_fully_visible_p (w, 0, 0))
15720 {
15721 /* Point does appear, but on a line partly visible at end of window.
15722 Move it back to a fully-visible line. */
15723 new_vpos = window_box_height (w);
15724 }
15725 else if (w->cursor.vpos >=0)
15726 {
15727 /* Some people insist on not letting point enter the scroll
15728 margin, even though this part handles windows that didn't
15729 scroll at all. */
15730 int window_total_lines
15731 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15732 int margin = min (scroll_margin, window_total_lines / 4);
15733 int pixel_margin = margin * frame_line_height;
15734 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15735
15736 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15737 below, which finds the row to move point to, advances by
15738 the Y coordinate of the _next_ row, see the definition of
15739 MATRIX_ROW_BOTTOM_Y. */
15740 if (w->cursor.vpos < margin + header_line)
15741 {
15742 w->cursor.vpos = -1;
15743 clear_glyph_matrix (w->desired_matrix);
15744 goto try_to_scroll;
15745 }
15746 else
15747 {
15748 int window_height = window_box_height (w);
15749
15750 if (header_line)
15751 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15752 if (w->cursor.y >= window_height - pixel_margin)
15753 {
15754 w->cursor.vpos = -1;
15755 clear_glyph_matrix (w->desired_matrix);
15756 goto try_to_scroll;
15757 }
15758 }
15759 }
15760
15761 /* If we need to move point for either of the above reasons,
15762 now actually do it. */
15763 if (new_vpos >= 0)
15764 {
15765 struct glyph_row *row;
15766
15767 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15768 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15769 ++row;
15770
15771 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15772 MATRIX_ROW_START_BYTEPOS (row));
15773
15774 if (w != XWINDOW (selected_window))
15775 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15776 else if (current_buffer == old)
15777 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15778
15779 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15780
15781 /* If we are highlighting the region, then we just changed
15782 the region, so redisplay to show it. */
15783 if (markpos_of_region () >= 0)
15784 {
15785 clear_glyph_matrix (w->desired_matrix);
15786 if (!try_window (window, startp, 0))
15787 goto need_larger_matrices;
15788 }
15789 }
15790
15791 #ifdef GLYPH_DEBUG
15792 debug_method_add (w, "forced window start");
15793 #endif
15794 goto done;
15795 }
15796
15797 /* Handle case where text has not changed, only point, and it has
15798 not moved off the frame, and we are not retrying after hscroll.
15799 (current_matrix_up_to_date_p is nonzero when retrying.) */
15800 if (current_matrix_up_to_date_p
15801 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15802 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15803 {
15804 switch (rc)
15805 {
15806 case CURSOR_MOVEMENT_SUCCESS:
15807 used_current_matrix_p = 1;
15808 goto done;
15809
15810 case CURSOR_MOVEMENT_MUST_SCROLL:
15811 goto try_to_scroll;
15812
15813 default:
15814 emacs_abort ();
15815 }
15816 }
15817 /* If current starting point was originally the beginning of a line
15818 but no longer is, find a new starting point. */
15819 else if (w->start_at_line_beg
15820 && !(CHARPOS (startp) <= BEGV
15821 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15822 {
15823 #ifdef GLYPH_DEBUG
15824 debug_method_add (w, "recenter 1");
15825 #endif
15826 goto recenter;
15827 }
15828
15829 /* Try scrolling with try_window_id. Value is > 0 if update has
15830 been done, it is -1 if we know that the same window start will
15831 not work. It is 0 if unsuccessful for some other reason. */
15832 else if ((tem = try_window_id (w)) != 0)
15833 {
15834 #ifdef GLYPH_DEBUG
15835 debug_method_add (w, "try_window_id %d", tem);
15836 #endif
15837
15838 if (fonts_changed_p)
15839 goto need_larger_matrices;
15840 if (tem > 0)
15841 goto done;
15842
15843 /* Otherwise try_window_id has returned -1 which means that we
15844 don't want the alternative below this comment to execute. */
15845 }
15846 else if (CHARPOS (startp) >= BEGV
15847 && CHARPOS (startp) <= ZV
15848 && PT >= CHARPOS (startp)
15849 && (CHARPOS (startp) < ZV
15850 /* Avoid starting at end of buffer. */
15851 || CHARPOS (startp) == BEGV
15852 || !window_outdated (w)))
15853 {
15854 int d1, d2, d3, d4, d5, d6;
15855
15856 /* If first window line is a continuation line, and window start
15857 is inside the modified region, but the first change is before
15858 current window start, we must select a new window start.
15859
15860 However, if this is the result of a down-mouse event (e.g. by
15861 extending the mouse-drag-overlay), we don't want to select a
15862 new window start, since that would change the position under
15863 the mouse, resulting in an unwanted mouse-movement rather
15864 than a simple mouse-click. */
15865 if (!w->start_at_line_beg
15866 && NILP (do_mouse_tracking)
15867 && CHARPOS (startp) > BEGV
15868 && CHARPOS (startp) > BEG + beg_unchanged
15869 && CHARPOS (startp) <= Z - end_unchanged
15870 /* Even if w->start_at_line_beg is nil, a new window may
15871 start at a line_beg, since that's how set_buffer_window
15872 sets it. So, we need to check the return value of
15873 compute_window_start_on_continuation_line. (See also
15874 bug#197). */
15875 && XMARKER (w->start)->buffer == current_buffer
15876 && compute_window_start_on_continuation_line (w)
15877 /* It doesn't make sense to force the window start like we
15878 do at label force_start if it is already known that point
15879 will not be visible in the resulting window, because
15880 doing so will move point from its correct position
15881 instead of scrolling the window to bring point into view.
15882 See bug#9324. */
15883 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15884 {
15885 w->force_start = 1;
15886 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15887 goto force_start;
15888 }
15889
15890 #ifdef GLYPH_DEBUG
15891 debug_method_add (w, "same window start");
15892 #endif
15893
15894 /* Try to redisplay starting at same place as before.
15895 If point has not moved off frame, accept the results. */
15896 if (!current_matrix_up_to_date_p
15897 /* Don't use try_window_reusing_current_matrix in this case
15898 because a window scroll function can have changed the
15899 buffer. */
15900 || !NILP (Vwindow_scroll_functions)
15901 || MINI_WINDOW_P (w)
15902 || !(used_current_matrix_p
15903 = try_window_reusing_current_matrix (w)))
15904 {
15905 IF_DEBUG (debug_method_add (w, "1"));
15906 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15907 /* -1 means we need to scroll.
15908 0 means we need new matrices, but fonts_changed_p
15909 is set in that case, so we will detect it below. */
15910 goto try_to_scroll;
15911 }
15912
15913 if (fonts_changed_p)
15914 goto need_larger_matrices;
15915
15916 if (w->cursor.vpos >= 0)
15917 {
15918 if (!just_this_one_p
15919 || current_buffer->clip_changed
15920 || BEG_UNCHANGED < CHARPOS (startp))
15921 /* Forget any recorded base line for line number display. */
15922 w->base_line_number = 0;
15923
15924 if (!cursor_row_fully_visible_p (w, 1, 0))
15925 {
15926 clear_glyph_matrix (w->desired_matrix);
15927 last_line_misfit = 1;
15928 }
15929 /* Drop through and scroll. */
15930 else
15931 goto done;
15932 }
15933 else
15934 clear_glyph_matrix (w->desired_matrix);
15935 }
15936
15937 try_to_scroll:
15938
15939 /* Redisplay the mode line. Select the buffer properly for that. */
15940 if (!update_mode_line)
15941 {
15942 update_mode_line = 1;
15943 w->update_mode_line = 1;
15944 }
15945
15946 /* Try to scroll by specified few lines. */
15947 if ((scroll_conservatively
15948 || emacs_scroll_step
15949 || temp_scroll_step
15950 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15951 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15952 && CHARPOS (startp) >= BEGV
15953 && CHARPOS (startp) <= ZV)
15954 {
15955 /* The function returns -1 if new fonts were loaded, 1 if
15956 successful, 0 if not successful. */
15957 int ss = try_scrolling (window, just_this_one_p,
15958 scroll_conservatively,
15959 emacs_scroll_step,
15960 temp_scroll_step, last_line_misfit);
15961 switch (ss)
15962 {
15963 case SCROLLING_SUCCESS:
15964 goto done;
15965
15966 case SCROLLING_NEED_LARGER_MATRICES:
15967 goto need_larger_matrices;
15968
15969 case SCROLLING_FAILED:
15970 break;
15971
15972 default:
15973 emacs_abort ();
15974 }
15975 }
15976
15977 /* Finally, just choose a place to start which positions point
15978 according to user preferences. */
15979
15980 recenter:
15981
15982 #ifdef GLYPH_DEBUG
15983 debug_method_add (w, "recenter");
15984 #endif
15985
15986 /* Forget any previously recorded base line for line number display. */
15987 if (!buffer_unchanged_p)
15988 w->base_line_number = 0;
15989
15990 /* Determine the window start relative to point. */
15991 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15992 it.current_y = it.last_visible_y;
15993 if (centering_position < 0)
15994 {
15995 int window_total_lines
15996 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15997 int margin =
15998 scroll_margin > 0
15999 ? min (scroll_margin, window_total_lines / 4)
16000 : 0;
16001 ptrdiff_t margin_pos = CHARPOS (startp);
16002 Lisp_Object aggressive;
16003 int scrolling_up;
16004
16005 /* If there is a scroll margin at the top of the window, find
16006 its character position. */
16007 if (margin
16008 /* Cannot call start_display if startp is not in the
16009 accessible region of the buffer. This can happen when we
16010 have just switched to a different buffer and/or changed
16011 its restriction. In that case, startp is initialized to
16012 the character position 1 (BEGV) because we did not yet
16013 have chance to display the buffer even once. */
16014 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16015 {
16016 struct it it1;
16017 void *it1data = NULL;
16018
16019 SAVE_IT (it1, it, it1data);
16020 start_display (&it1, w, startp);
16021 move_it_vertically (&it1, margin * frame_line_height);
16022 margin_pos = IT_CHARPOS (it1);
16023 RESTORE_IT (&it, &it, it1data);
16024 }
16025 scrolling_up = PT > margin_pos;
16026 aggressive =
16027 scrolling_up
16028 ? BVAR (current_buffer, scroll_up_aggressively)
16029 : BVAR (current_buffer, scroll_down_aggressively);
16030
16031 if (!MINI_WINDOW_P (w)
16032 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16033 {
16034 int pt_offset = 0;
16035
16036 /* Setting scroll-conservatively overrides
16037 scroll-*-aggressively. */
16038 if (!scroll_conservatively && NUMBERP (aggressive))
16039 {
16040 double float_amount = XFLOATINT (aggressive);
16041
16042 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16043 if (pt_offset == 0 && float_amount > 0)
16044 pt_offset = 1;
16045 if (pt_offset && margin > 0)
16046 margin -= 1;
16047 }
16048 /* Compute how much to move the window start backward from
16049 point so that point will be displayed where the user
16050 wants it. */
16051 if (scrolling_up)
16052 {
16053 centering_position = it.last_visible_y;
16054 if (pt_offset)
16055 centering_position -= pt_offset;
16056 centering_position -=
16057 frame_line_height * (1 + margin + (last_line_misfit != 0))
16058 + WINDOW_HEADER_LINE_HEIGHT (w);
16059 /* Don't let point enter the scroll margin near top of
16060 the window. */
16061 if (centering_position < margin * frame_line_height)
16062 centering_position = margin * frame_line_height;
16063 }
16064 else
16065 centering_position = margin * frame_line_height + pt_offset;
16066 }
16067 else
16068 /* Set the window start half the height of the window backward
16069 from point. */
16070 centering_position = window_box_height (w) / 2;
16071 }
16072 move_it_vertically_backward (&it, centering_position);
16073
16074 eassert (IT_CHARPOS (it) >= BEGV);
16075
16076 /* The function move_it_vertically_backward may move over more
16077 than the specified y-distance. If it->w is small, e.g. a
16078 mini-buffer window, we may end up in front of the window's
16079 display area. Start displaying at the start of the line
16080 containing PT in this case. */
16081 if (it.current_y <= 0)
16082 {
16083 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16084 move_it_vertically_backward (&it, 0);
16085 it.current_y = 0;
16086 }
16087
16088 it.current_x = it.hpos = 0;
16089
16090 /* Set the window start position here explicitly, to avoid an
16091 infinite loop in case the functions in window-scroll-functions
16092 get errors. */
16093 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16094
16095 /* Run scroll hooks. */
16096 startp = run_window_scroll_functions (window, it.current.pos);
16097
16098 /* Redisplay the window. */
16099 if (!current_matrix_up_to_date_p
16100 || windows_or_buffers_changed
16101 || cursor_type_changed
16102 /* Don't use try_window_reusing_current_matrix in this case
16103 because it can have changed the buffer. */
16104 || !NILP (Vwindow_scroll_functions)
16105 || !just_this_one_p
16106 || MINI_WINDOW_P (w)
16107 || !(used_current_matrix_p
16108 = try_window_reusing_current_matrix (w)))
16109 try_window (window, startp, 0);
16110
16111 /* If new fonts have been loaded (due to fontsets), give up. We
16112 have to start a new redisplay since we need to re-adjust glyph
16113 matrices. */
16114 if (fonts_changed_p)
16115 goto need_larger_matrices;
16116
16117 /* If cursor did not appear assume that the middle of the window is
16118 in the first line of the window. Do it again with the next line.
16119 (Imagine a window of height 100, displaying two lines of height
16120 60. Moving back 50 from it->last_visible_y will end in the first
16121 line.) */
16122 if (w->cursor.vpos < 0)
16123 {
16124 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16125 {
16126 clear_glyph_matrix (w->desired_matrix);
16127 move_it_by_lines (&it, 1);
16128 try_window (window, it.current.pos, 0);
16129 }
16130 else if (PT < IT_CHARPOS (it))
16131 {
16132 clear_glyph_matrix (w->desired_matrix);
16133 move_it_by_lines (&it, -1);
16134 try_window (window, it.current.pos, 0);
16135 }
16136 else
16137 {
16138 /* Not much we can do about it. */
16139 }
16140 }
16141
16142 /* Consider the following case: Window starts at BEGV, there is
16143 invisible, intangible text at BEGV, so that display starts at
16144 some point START > BEGV. It can happen that we are called with
16145 PT somewhere between BEGV and START. Try to handle that case. */
16146 if (w->cursor.vpos < 0)
16147 {
16148 struct glyph_row *row = w->current_matrix->rows;
16149 if (row->mode_line_p)
16150 ++row;
16151 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16152 }
16153
16154 if (!cursor_row_fully_visible_p (w, 0, 0))
16155 {
16156 /* If vscroll is enabled, disable it and try again. */
16157 if (w->vscroll)
16158 {
16159 w->vscroll = 0;
16160 clear_glyph_matrix (w->desired_matrix);
16161 goto recenter;
16162 }
16163
16164 /* Users who set scroll-conservatively to a large number want
16165 point just above/below the scroll margin. If we ended up
16166 with point's row partially visible, move the window start to
16167 make that row fully visible and out of the margin. */
16168 if (scroll_conservatively > SCROLL_LIMIT)
16169 {
16170 int window_total_lines
16171 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16172 int margin =
16173 scroll_margin > 0
16174 ? min (scroll_margin, window_total_lines / 4)
16175 : 0;
16176 int move_down = w->cursor.vpos >= window_total_lines / 2;
16177
16178 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16179 clear_glyph_matrix (w->desired_matrix);
16180 if (1 == try_window (window, it.current.pos,
16181 TRY_WINDOW_CHECK_MARGINS))
16182 goto done;
16183 }
16184
16185 /* If centering point failed to make the whole line visible,
16186 put point at the top instead. That has to make the whole line
16187 visible, if it can be done. */
16188 if (centering_position == 0)
16189 goto done;
16190
16191 clear_glyph_matrix (w->desired_matrix);
16192 centering_position = 0;
16193 goto recenter;
16194 }
16195
16196 done:
16197
16198 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16199 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16200 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16201
16202 /* Display the mode line, if we must. */
16203 if ((update_mode_line
16204 /* If window not full width, must redo its mode line
16205 if (a) the window to its side is being redone and
16206 (b) we do a frame-based redisplay. This is a consequence
16207 of how inverted lines are drawn in frame-based redisplay. */
16208 || (!just_this_one_p
16209 && !FRAME_WINDOW_P (f)
16210 && !WINDOW_FULL_WIDTH_P (w))
16211 /* Line number to display. */
16212 || w->base_line_pos > 0
16213 /* Column number is displayed and different from the one displayed. */
16214 || (w->column_number_displayed != -1
16215 && (w->column_number_displayed != current_column ())))
16216 /* This means that the window has a mode line. */
16217 && (WINDOW_WANTS_MODELINE_P (w)
16218 || WINDOW_WANTS_HEADER_LINE_P (w)))
16219 {
16220 display_mode_lines (w);
16221
16222 /* If mode line height has changed, arrange for a thorough
16223 immediate redisplay using the correct mode line height. */
16224 if (WINDOW_WANTS_MODELINE_P (w)
16225 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16226 {
16227 fonts_changed_p = 1;
16228 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16229 = DESIRED_MODE_LINE_HEIGHT (w);
16230 }
16231
16232 /* If header line height has changed, arrange for a thorough
16233 immediate redisplay using the correct header line height. */
16234 if (WINDOW_WANTS_HEADER_LINE_P (w)
16235 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16236 {
16237 fonts_changed_p = 1;
16238 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16239 = DESIRED_HEADER_LINE_HEIGHT (w);
16240 }
16241
16242 if (fonts_changed_p)
16243 goto need_larger_matrices;
16244 }
16245
16246 if (!line_number_displayed && w->base_line_pos != -1)
16247 {
16248 w->base_line_pos = 0;
16249 w->base_line_number = 0;
16250 }
16251
16252 finish_menu_bars:
16253
16254 /* When we reach a frame's selected window, redo the frame's menu bar. */
16255 if (update_mode_line
16256 && EQ (FRAME_SELECTED_WINDOW (f), window))
16257 {
16258 int redisplay_menu_p = 0;
16259
16260 if (FRAME_WINDOW_P (f))
16261 {
16262 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16263 || defined (HAVE_NS) || defined (USE_GTK)
16264 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16265 #else
16266 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16267 #endif
16268 }
16269 else
16270 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16271
16272 if (redisplay_menu_p)
16273 display_menu_bar (w);
16274
16275 #ifdef HAVE_WINDOW_SYSTEM
16276 if (FRAME_WINDOW_P (f))
16277 {
16278 #if defined (USE_GTK) || defined (HAVE_NS)
16279 if (FRAME_EXTERNAL_TOOL_BAR (f))
16280 redisplay_tool_bar (f);
16281 #else
16282 if (WINDOWP (f->tool_bar_window)
16283 && (FRAME_TOOL_BAR_LINES (f) > 0
16284 || !NILP (Vauto_resize_tool_bars))
16285 && redisplay_tool_bar (f))
16286 ignore_mouse_drag_p = 1;
16287 #endif
16288 }
16289 #endif
16290 }
16291
16292 #ifdef HAVE_WINDOW_SYSTEM
16293 if (FRAME_WINDOW_P (f)
16294 && update_window_fringes (w, (just_this_one_p
16295 || (!used_current_matrix_p && !overlay_arrow_seen)
16296 || w->pseudo_window_p)))
16297 {
16298 update_begin (f);
16299 block_input ();
16300 if (draw_window_fringes (w, 1))
16301 x_draw_vertical_border (w);
16302 unblock_input ();
16303 update_end (f);
16304 }
16305 #endif /* HAVE_WINDOW_SYSTEM */
16306
16307 /* We go to this label, with fonts_changed_p set,
16308 if it is necessary to try again using larger glyph matrices.
16309 We have to redeem the scroll bar even in this case,
16310 because the loop in redisplay_internal expects that. */
16311 need_larger_matrices:
16312 ;
16313 finish_scroll_bars:
16314
16315 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16316 {
16317 /* Set the thumb's position and size. */
16318 set_vertical_scroll_bar (w);
16319
16320 /* Note that we actually used the scroll bar attached to this
16321 window, so it shouldn't be deleted at the end of redisplay. */
16322 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16323 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16324 }
16325
16326 /* Restore current_buffer and value of point in it. The window
16327 update may have changed the buffer, so first make sure `opoint'
16328 is still valid (Bug#6177). */
16329 if (CHARPOS (opoint) < BEGV)
16330 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16331 else if (CHARPOS (opoint) > ZV)
16332 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16333 else
16334 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16335
16336 set_buffer_internal_1 (old);
16337 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16338 shorter. This can be caused by log truncation in *Messages*. */
16339 if (CHARPOS (lpoint) <= ZV)
16340 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16341
16342 unbind_to (count, Qnil);
16343 }
16344
16345
16346 /* Build the complete desired matrix of WINDOW with a window start
16347 buffer position POS.
16348
16349 Value is 1 if successful. It is zero if fonts were loaded during
16350 redisplay which makes re-adjusting glyph matrices necessary, and -1
16351 if point would appear in the scroll margins.
16352 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16353 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16354 set in FLAGS.) */
16355
16356 int
16357 try_window (Lisp_Object window, struct text_pos pos, int flags)
16358 {
16359 struct window *w = XWINDOW (window);
16360 struct it it;
16361 struct glyph_row *last_text_row = NULL;
16362 struct frame *f = XFRAME (w->frame);
16363 int frame_line_height = default_line_pixel_height (w);
16364
16365 /* Make POS the new window start. */
16366 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16367
16368 /* Mark cursor position as unknown. No overlay arrow seen. */
16369 w->cursor.vpos = -1;
16370 overlay_arrow_seen = 0;
16371
16372 /* Initialize iterator and info to start at POS. */
16373 start_display (&it, w, pos);
16374
16375
16376
16377 /* Display all lines of W. */
16378 while (it.current_y < it.last_visible_y)
16379 {
16380 if (display_line (&it))
16381 last_text_row = it.glyph_row - 1;
16382 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16383 return 0;
16384 }
16385 #ifdef HAVE_XWIDGETS_xxx
16386 //currently this is needed to detect xwidget movement reliably. or probably not.
16387 printf("try_window\n");
16388 return 0;
16389 #endif
16390
16391 /* Don't let the cursor end in the scroll margins. */
16392 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16393 && !MINI_WINDOW_P (w))
16394 {
16395 int this_scroll_margin;
16396 int window_total_lines
16397 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16398
16399 if (scroll_margin > 0)
16400 {
16401 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16402 this_scroll_margin *= frame_line_height;
16403 }
16404 else
16405 this_scroll_margin = 0;
16406
16407 if ((w->cursor.y >= 0 /* not vscrolled */
16408 && w->cursor.y < this_scroll_margin
16409 && CHARPOS (pos) > BEGV
16410 && IT_CHARPOS (it) < ZV)
16411 /* rms: considering make_cursor_line_fully_visible_p here
16412 seems to give wrong results. We don't want to recenter
16413 when the last line is partly visible, we want to allow
16414 that case to be handled in the usual way. */
16415 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16416 {
16417 w->cursor.vpos = -1;
16418 clear_glyph_matrix (w->desired_matrix);
16419 return -1;
16420 }
16421 }
16422
16423 /* If bottom moved off end of frame, change mode line percentage. */
16424 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16425 w->update_mode_line = 1;
16426
16427 /* Set window_end_pos to the offset of the last character displayed
16428 on the window from the end of current_buffer. Set
16429 window_end_vpos to its row number. */
16430 if (last_text_row)
16431 {
16432 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16433 adjust_window_ends (w, last_text_row, 0);
16434 eassert
16435 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16436 w->window_end_vpos)));
16437 }
16438 else
16439 {
16440 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16441 w->window_end_pos = Z - ZV;
16442 w->window_end_vpos = 0;
16443 }
16444
16445 /* But that is not valid info until redisplay finishes. */
16446 w->window_end_valid = 0;
16447 return 1;
16448 }
16449
16450
16451 \f
16452 /************************************************************************
16453 Window redisplay reusing current matrix when buffer has not changed
16454 ************************************************************************/
16455
16456 /* Try redisplay of window W showing an unchanged buffer with a
16457 different window start than the last time it was displayed by
16458 reusing its current matrix. Value is non-zero if successful.
16459 W->start is the new window start. */
16460
16461 static int
16462 try_window_reusing_current_matrix (struct window *w)
16463 {
16464 struct frame *f = XFRAME (w->frame);
16465 struct glyph_row *bottom_row;
16466 struct it it;
16467 struct run run;
16468 struct text_pos start, new_start;
16469 int nrows_scrolled, i;
16470 struct glyph_row *last_text_row;
16471 struct glyph_row *last_reused_text_row;
16472 struct glyph_row *start_row;
16473 int start_vpos, min_y, max_y;
16474
16475 #ifdef GLYPH_DEBUG
16476 if (inhibit_try_window_reusing)
16477 return 0;
16478 #endif
16479
16480 #ifdef HAVE_XWIDGETS_xxx
16481 //currently this is needed to detect xwidget movement reliably. or probably not.
16482 printf("try_window_reusing_current_matrix\n");
16483 return 0;
16484 #endif
16485
16486
16487 if (/* This function doesn't handle terminal frames. */
16488 !FRAME_WINDOW_P (f)
16489 /* Don't try to reuse the display if windows have been split
16490 or such. */
16491 || windows_or_buffers_changed
16492 || cursor_type_changed)
16493 return 0;
16494
16495 /* Can't do this if region may have changed. */
16496 if (markpos_of_region () >= 0
16497 || w->region_showing
16498 || !NILP (Vshow_trailing_whitespace))
16499 return 0;
16500
16501 /* If top-line visibility has changed, give up. */
16502 if (WINDOW_WANTS_HEADER_LINE_P (w)
16503 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16504 return 0;
16505
16506 /* Give up if old or new display is scrolled vertically. We could
16507 make this function handle this, but right now it doesn't. */
16508 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16509 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16510 return 0;
16511
16512 /* The variable new_start now holds the new window start. The old
16513 start `start' can be determined from the current matrix. */
16514 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16515 start = start_row->minpos;
16516 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16517
16518 /* Clear the desired matrix for the display below. */
16519 clear_glyph_matrix (w->desired_matrix);
16520
16521 if (CHARPOS (new_start) <= CHARPOS (start))
16522 {
16523 /* Don't use this method if the display starts with an ellipsis
16524 displayed for invisible text. It's not easy to handle that case
16525 below, and it's certainly not worth the effort since this is
16526 not a frequent case. */
16527 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16528 return 0;
16529
16530 IF_DEBUG (debug_method_add (w, "twu1"));
16531
16532 /* Display up to a row that can be reused. The variable
16533 last_text_row is set to the last row displayed that displays
16534 text. Note that it.vpos == 0 if or if not there is a
16535 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16536 start_display (&it, w, new_start);
16537 w->cursor.vpos = -1;
16538 last_text_row = last_reused_text_row = NULL;
16539
16540 while (it.current_y < it.last_visible_y
16541 && !fonts_changed_p)
16542 {
16543 /* If we have reached into the characters in the START row,
16544 that means the line boundaries have changed. So we
16545 can't start copying with the row START. Maybe it will
16546 work to start copying with the following row. */
16547 while (IT_CHARPOS (it) > CHARPOS (start))
16548 {
16549 /* Advance to the next row as the "start". */
16550 start_row++;
16551 start = start_row->minpos;
16552 /* If there are no more rows to try, or just one, give up. */
16553 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16554 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16555 || CHARPOS (start) == ZV)
16556 {
16557 clear_glyph_matrix (w->desired_matrix);
16558 return 0;
16559 }
16560
16561 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16562 }
16563 /* If we have reached alignment, we can copy the rest of the
16564 rows. */
16565 if (IT_CHARPOS (it) == CHARPOS (start)
16566 /* Don't accept "alignment" inside a display vector,
16567 since start_row could have started in the middle of
16568 that same display vector (thus their character
16569 positions match), and we have no way of telling if
16570 that is the case. */
16571 && it.current.dpvec_index < 0)
16572 break;
16573
16574 if (display_line (&it))
16575 last_text_row = it.glyph_row - 1;
16576
16577 }
16578
16579 /* A value of current_y < last_visible_y means that we stopped
16580 at the previous window start, which in turn means that we
16581 have at least one reusable row. */
16582 if (it.current_y < it.last_visible_y)
16583 {
16584 struct glyph_row *row;
16585
16586 /* IT.vpos always starts from 0; it counts text lines. */
16587 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16588
16589 /* Find PT if not already found in the lines displayed. */
16590 if (w->cursor.vpos < 0)
16591 {
16592 int dy = it.current_y - start_row->y;
16593
16594 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16595 row = row_containing_pos (w, PT, row, NULL, dy);
16596 if (row)
16597 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16598 dy, nrows_scrolled);
16599 else
16600 {
16601 clear_glyph_matrix (w->desired_matrix);
16602 return 0;
16603 }
16604 }
16605
16606 /* Scroll the display. Do it before the current matrix is
16607 changed. The problem here is that update has not yet
16608 run, i.e. part of the current matrix is not up to date.
16609 scroll_run_hook will clear the cursor, and use the
16610 current matrix to get the height of the row the cursor is
16611 in. */
16612 run.current_y = start_row->y;
16613 run.desired_y = it.current_y;
16614 run.height = it.last_visible_y - it.current_y;
16615
16616 if (run.height > 0 && run.current_y != run.desired_y)
16617 {
16618 update_begin (f);
16619 FRAME_RIF (f)->update_window_begin_hook (w);
16620 FRAME_RIF (f)->clear_window_mouse_face (w);
16621 FRAME_RIF (f)->scroll_run_hook (w, &run);
16622 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16623 update_end (f);
16624 }
16625
16626 /* Shift current matrix down by nrows_scrolled lines. */
16627 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16628 rotate_matrix (w->current_matrix,
16629 start_vpos,
16630 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16631 nrows_scrolled);
16632
16633 /* Disable lines that must be updated. */
16634 for (i = 0; i < nrows_scrolled; ++i)
16635 (start_row + i)->enabled_p = 0;
16636
16637 /* Re-compute Y positions. */
16638 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16639 max_y = it.last_visible_y;
16640 for (row = start_row + nrows_scrolled;
16641 row < bottom_row;
16642 ++row)
16643 {
16644 row->y = it.current_y;
16645 row->visible_height = row->height;
16646
16647 if (row->y < min_y)
16648 row->visible_height -= min_y - row->y;
16649 if (row->y + row->height > max_y)
16650 row->visible_height -= row->y + row->height - max_y;
16651 if (row->fringe_bitmap_periodic_p)
16652 row->redraw_fringe_bitmaps_p = 1;
16653
16654 it.current_y += row->height;
16655
16656 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16657 last_reused_text_row = row;
16658 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16659 break;
16660 }
16661
16662 /* Disable lines in the current matrix which are now
16663 below the window. */
16664 for (++row; row < bottom_row; ++row)
16665 row->enabled_p = row->mode_line_p = 0;
16666 }
16667
16668 /* Update window_end_pos etc.; last_reused_text_row is the last
16669 reused row from the current matrix containing text, if any.
16670 The value of last_text_row is the last displayed line
16671 containing text. */
16672 if (last_reused_text_row)
16673 adjust_window_ends (w, last_reused_text_row, 1);
16674 else if (last_text_row)
16675 adjust_window_ends (w, last_text_row, 0);
16676 else
16677 {
16678 /* This window must be completely empty. */
16679 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16680 w->window_end_pos = Z - ZV;
16681 w->window_end_vpos = 0;
16682 }
16683 w->window_end_valid = 0;
16684
16685 /* Update hint: don't try scrolling again in update_window. */
16686 w->desired_matrix->no_scrolling_p = 1;
16687
16688 #ifdef GLYPH_DEBUG
16689 debug_method_add (w, "try_window_reusing_current_matrix 1");
16690 #endif
16691 return 1;
16692 }
16693 else if (CHARPOS (new_start) > CHARPOS (start))
16694 {
16695 struct glyph_row *pt_row, *row;
16696 struct glyph_row *first_reusable_row;
16697 struct glyph_row *first_row_to_display;
16698 int dy;
16699 int yb = window_text_bottom_y (w);
16700
16701 /* Find the row starting at new_start, if there is one. Don't
16702 reuse a partially visible line at the end. */
16703 first_reusable_row = start_row;
16704 while (first_reusable_row->enabled_p
16705 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16706 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16707 < CHARPOS (new_start)))
16708 ++first_reusable_row;
16709
16710 /* Give up if there is no row to reuse. */
16711 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16712 || !first_reusable_row->enabled_p
16713 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16714 != CHARPOS (new_start)))
16715 return 0;
16716
16717 /* We can reuse fully visible rows beginning with
16718 first_reusable_row to the end of the window. Set
16719 first_row_to_display to the first row that cannot be reused.
16720 Set pt_row to the row containing point, if there is any. */
16721 pt_row = NULL;
16722 for (first_row_to_display = first_reusable_row;
16723 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16724 ++first_row_to_display)
16725 {
16726 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16727 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16728 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16729 && first_row_to_display->ends_at_zv_p
16730 && pt_row == NULL)))
16731 pt_row = first_row_to_display;
16732 }
16733
16734 /* Start displaying at the start of first_row_to_display. */
16735 eassert (first_row_to_display->y < yb);
16736 init_to_row_start (&it, w, first_row_to_display);
16737
16738 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16739 - start_vpos);
16740 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16741 - nrows_scrolled);
16742 it.current_y = (first_row_to_display->y - first_reusable_row->y
16743 + WINDOW_HEADER_LINE_HEIGHT (w));
16744
16745 /* Display lines beginning with first_row_to_display in the
16746 desired matrix. Set last_text_row to the last row displayed
16747 that displays text. */
16748 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16749 if (pt_row == NULL)
16750 w->cursor.vpos = -1;
16751 last_text_row = NULL;
16752 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16753 if (display_line (&it))
16754 last_text_row = it.glyph_row - 1;
16755
16756 /* If point is in a reused row, adjust y and vpos of the cursor
16757 position. */
16758 if (pt_row)
16759 {
16760 w->cursor.vpos -= nrows_scrolled;
16761 w->cursor.y -= first_reusable_row->y - start_row->y;
16762 }
16763
16764 /* Give up if point isn't in a row displayed or reused. (This
16765 also handles the case where w->cursor.vpos < nrows_scrolled
16766 after the calls to display_line, which can happen with scroll
16767 margins. See bug#1295.) */
16768 if (w->cursor.vpos < 0)
16769 {
16770 clear_glyph_matrix (w->desired_matrix);
16771 return 0;
16772 }
16773
16774 /* Scroll the display. */
16775 run.current_y = first_reusable_row->y;
16776 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16777 run.height = it.last_visible_y - run.current_y;
16778 dy = run.current_y - run.desired_y;
16779
16780 if (run.height)
16781 {
16782 update_begin (f);
16783 FRAME_RIF (f)->update_window_begin_hook (w);
16784 FRAME_RIF (f)->clear_window_mouse_face (w);
16785 FRAME_RIF (f)->scroll_run_hook (w, &run);
16786 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16787 update_end (f);
16788 }
16789
16790 /* Adjust Y positions of reused rows. */
16791 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16792 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16793 max_y = it.last_visible_y;
16794 for (row = first_reusable_row; row < first_row_to_display; ++row)
16795 {
16796 row->y -= dy;
16797 row->visible_height = row->height;
16798 if (row->y < min_y)
16799 row->visible_height -= min_y - row->y;
16800 if (row->y + row->height > max_y)
16801 row->visible_height -= row->y + row->height - max_y;
16802 if (row->fringe_bitmap_periodic_p)
16803 row->redraw_fringe_bitmaps_p = 1;
16804 }
16805
16806 /* Scroll the current matrix. */
16807 eassert (nrows_scrolled > 0);
16808 rotate_matrix (w->current_matrix,
16809 start_vpos,
16810 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16811 -nrows_scrolled);
16812
16813 /* Disable rows not reused. */
16814 for (row -= nrows_scrolled; row < bottom_row; ++row)
16815 row->enabled_p = 0;
16816
16817 /* Point may have moved to a different line, so we cannot assume that
16818 the previous cursor position is valid; locate the correct row. */
16819 if (pt_row)
16820 {
16821 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16822 row < bottom_row
16823 && PT >= MATRIX_ROW_END_CHARPOS (row)
16824 && !row->ends_at_zv_p;
16825 row++)
16826 {
16827 w->cursor.vpos++;
16828 w->cursor.y = row->y;
16829 }
16830 if (row < bottom_row)
16831 {
16832 /* Can't simply scan the row for point with
16833 bidi-reordered glyph rows. Let set_cursor_from_row
16834 figure out where to put the cursor, and if it fails,
16835 give up. */
16836 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16837 {
16838 if (!set_cursor_from_row (w, row, w->current_matrix,
16839 0, 0, 0, 0))
16840 {
16841 clear_glyph_matrix (w->desired_matrix);
16842 return 0;
16843 }
16844 }
16845 else
16846 {
16847 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16848 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16849
16850 for (; glyph < end
16851 && (!BUFFERP (glyph->object)
16852 || glyph->charpos < PT);
16853 glyph++)
16854 {
16855 w->cursor.hpos++;
16856 w->cursor.x += glyph->pixel_width;
16857 }
16858 }
16859 }
16860 }
16861
16862 /* Adjust window end. A null value of last_text_row means that
16863 the window end is in reused rows which in turn means that
16864 only its vpos can have changed. */
16865 if (last_text_row)
16866 adjust_window_ends (w, last_text_row, 0);
16867 else
16868 w->window_end_vpos -= nrows_scrolled;
16869
16870 w->window_end_valid = 0;
16871 w->desired_matrix->no_scrolling_p = 1;
16872
16873 #ifdef GLYPH_DEBUG
16874 debug_method_add (w, "try_window_reusing_current_matrix 2");
16875 #endif
16876 return 1;
16877 }
16878
16879 return 0;
16880 }
16881
16882
16883 \f
16884 /************************************************************************
16885 Window redisplay reusing current matrix when buffer has changed
16886 ************************************************************************/
16887
16888 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16889 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16890 ptrdiff_t *, ptrdiff_t *);
16891 static struct glyph_row *
16892 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16893 struct glyph_row *);
16894
16895
16896 /* Return the last row in MATRIX displaying text. If row START is
16897 non-null, start searching with that row. IT gives the dimensions
16898 of the display. Value is null if matrix is empty; otherwise it is
16899 a pointer to the row found. */
16900
16901 static struct glyph_row *
16902 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16903 struct glyph_row *start)
16904 {
16905 struct glyph_row *row, *row_found;
16906
16907 /* Set row_found to the last row in IT->w's current matrix
16908 displaying text. The loop looks funny but think of partially
16909 visible lines. */
16910 row_found = NULL;
16911 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16912 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16913 {
16914 eassert (row->enabled_p);
16915 row_found = row;
16916 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16917 break;
16918 ++row;
16919 }
16920
16921 return row_found;
16922 }
16923
16924
16925 /* Return the last row in the current matrix of W that is not affected
16926 by changes at the start of current_buffer that occurred since W's
16927 current matrix was built. Value is null if no such row exists.
16928
16929 BEG_UNCHANGED us the number of characters unchanged at the start of
16930 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16931 first changed character in current_buffer. Characters at positions <
16932 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16933 when the current matrix was built. */
16934
16935 static struct glyph_row *
16936 find_last_unchanged_at_beg_row (struct window *w)
16937 {
16938 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16939 struct glyph_row *row;
16940 struct glyph_row *row_found = NULL;
16941 int yb = window_text_bottom_y (w);
16942
16943 /* Find the last row displaying unchanged text. */
16944 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16945 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16946 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16947 ++row)
16948 {
16949 if (/* If row ends before first_changed_pos, it is unchanged,
16950 except in some case. */
16951 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16952 /* When row ends in ZV and we write at ZV it is not
16953 unchanged. */
16954 && !row->ends_at_zv_p
16955 /* When first_changed_pos is the end of a continued line,
16956 row is not unchanged because it may be no longer
16957 continued. */
16958 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16959 && (row->continued_p
16960 || row->exact_window_width_line_p))
16961 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16962 needs to be recomputed, so don't consider this row as
16963 unchanged. This happens when the last line was
16964 bidi-reordered and was killed immediately before this
16965 redisplay cycle. In that case, ROW->end stores the
16966 buffer position of the first visual-order character of
16967 the killed text, which is now beyond ZV. */
16968 && CHARPOS (row->end.pos) <= ZV)
16969 row_found = row;
16970
16971 /* Stop if last visible row. */
16972 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16973 break;
16974 }
16975
16976 return row_found;
16977 }
16978
16979
16980 /* Find the first glyph row in the current matrix of W that is not
16981 affected by changes at the end of current_buffer since the
16982 time W's current matrix was built.
16983
16984 Return in *DELTA the number of chars by which buffer positions in
16985 unchanged text at the end of current_buffer must be adjusted.
16986
16987 Return in *DELTA_BYTES the corresponding number of bytes.
16988
16989 Value is null if no such row exists, i.e. all rows are affected by
16990 changes. */
16991
16992 static struct glyph_row *
16993 find_first_unchanged_at_end_row (struct window *w,
16994 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16995 {
16996 struct glyph_row *row;
16997 struct glyph_row *row_found = NULL;
16998
16999 *delta = *delta_bytes = 0;
17000
17001 /* Display must not have been paused, otherwise the current matrix
17002 is not up to date. */
17003 eassert (w->window_end_valid);
17004
17005 /* A value of window_end_pos >= END_UNCHANGED means that the window
17006 end is in the range of changed text. If so, there is no
17007 unchanged row at the end of W's current matrix. */
17008 if (w->window_end_pos >= END_UNCHANGED)
17009 return NULL;
17010
17011 /* Set row to the last row in W's current matrix displaying text. */
17012 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17013
17014 /* If matrix is entirely empty, no unchanged row exists. */
17015 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17016 {
17017 /* The value of row is the last glyph row in the matrix having a
17018 meaningful buffer position in it. The end position of row
17019 corresponds to window_end_pos. This allows us to translate
17020 buffer positions in the current matrix to current buffer
17021 positions for characters not in changed text. */
17022 ptrdiff_t Z_old =
17023 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17024 ptrdiff_t Z_BYTE_old =
17025 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17026 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17027 struct glyph_row *first_text_row
17028 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17029
17030 *delta = Z - Z_old;
17031 *delta_bytes = Z_BYTE - Z_BYTE_old;
17032
17033 /* Set last_unchanged_pos to the buffer position of the last
17034 character in the buffer that has not been changed. Z is the
17035 index + 1 of the last character in current_buffer, i.e. by
17036 subtracting END_UNCHANGED we get the index of the last
17037 unchanged character, and we have to add BEG to get its buffer
17038 position. */
17039 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17040 last_unchanged_pos_old = last_unchanged_pos - *delta;
17041
17042 /* Search backward from ROW for a row displaying a line that
17043 starts at a minimum position >= last_unchanged_pos_old. */
17044 for (; row > first_text_row; --row)
17045 {
17046 /* This used to abort, but it can happen.
17047 It is ok to just stop the search instead here. KFS. */
17048 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17049 break;
17050
17051 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17052 row_found = row;
17053 }
17054 }
17055
17056 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17057
17058 return row_found;
17059 }
17060
17061
17062 /* Make sure that glyph rows in the current matrix of window W
17063 reference the same glyph memory as corresponding rows in the
17064 frame's frame matrix. This function is called after scrolling W's
17065 current matrix on a terminal frame in try_window_id and
17066 try_window_reusing_current_matrix. */
17067
17068 static void
17069 sync_frame_with_window_matrix_rows (struct window *w)
17070 {
17071 struct frame *f = XFRAME (w->frame);
17072 struct glyph_row *window_row, *window_row_end, *frame_row;
17073
17074 /* Preconditions: W must be a leaf window and full-width. Its frame
17075 must have a frame matrix. */
17076 eassert (BUFFERP (w->contents));
17077 eassert (WINDOW_FULL_WIDTH_P (w));
17078 eassert (!FRAME_WINDOW_P (f));
17079
17080 /* If W is a full-width window, glyph pointers in W's current matrix
17081 have, by definition, to be the same as glyph pointers in the
17082 corresponding frame matrix. Note that frame matrices have no
17083 marginal areas (see build_frame_matrix). */
17084 window_row = w->current_matrix->rows;
17085 window_row_end = window_row + w->current_matrix->nrows;
17086 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17087 while (window_row < window_row_end)
17088 {
17089 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17090 struct glyph *end = window_row->glyphs[LAST_AREA];
17091
17092 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17093 frame_row->glyphs[TEXT_AREA] = start;
17094 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17095 frame_row->glyphs[LAST_AREA] = end;
17096
17097 /* Disable frame rows whose corresponding window rows have
17098 been disabled in try_window_id. */
17099 if (!window_row->enabled_p)
17100 frame_row->enabled_p = 0;
17101
17102 ++window_row, ++frame_row;
17103 }
17104 }
17105
17106
17107 /* Find the glyph row in window W containing CHARPOS. Consider all
17108 rows between START and END (not inclusive). END null means search
17109 all rows to the end of the display area of W. Value is the row
17110 containing CHARPOS or null. */
17111
17112 struct glyph_row *
17113 row_containing_pos (struct window *w, ptrdiff_t charpos,
17114 struct glyph_row *start, struct glyph_row *end, int dy)
17115 {
17116 struct glyph_row *row = start;
17117 struct glyph_row *best_row = NULL;
17118 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17119 int last_y;
17120
17121 /* If we happen to start on a header-line, skip that. */
17122 if (row->mode_line_p)
17123 ++row;
17124
17125 if ((end && row >= end) || !row->enabled_p)
17126 return NULL;
17127
17128 last_y = window_text_bottom_y (w) - dy;
17129
17130 while (1)
17131 {
17132 /* Give up if we have gone too far. */
17133 if (end && row >= end)
17134 return NULL;
17135 /* This formerly returned if they were equal.
17136 I think that both quantities are of a "last plus one" type;
17137 if so, when they are equal, the row is within the screen. -- rms. */
17138 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17139 return NULL;
17140
17141 /* If it is in this row, return this row. */
17142 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17143 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17144 /* The end position of a row equals the start
17145 position of the next row. If CHARPOS is there, we
17146 would rather consider it displayed in the next
17147 line, except when this line ends in ZV. */
17148 && !row_for_charpos_p (row, charpos)))
17149 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17150 {
17151 struct glyph *g;
17152
17153 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17154 || (!best_row && !row->continued_p))
17155 return row;
17156 /* In bidi-reordered rows, there could be several rows whose
17157 edges surround CHARPOS, all of these rows belonging to
17158 the same continued line. We need to find the row which
17159 fits CHARPOS the best. */
17160 for (g = row->glyphs[TEXT_AREA];
17161 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17162 g++)
17163 {
17164 if (!STRINGP (g->object))
17165 {
17166 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17167 {
17168 mindif = eabs (g->charpos - charpos);
17169 best_row = row;
17170 /* Exact match always wins. */
17171 if (mindif == 0)
17172 return best_row;
17173 }
17174 }
17175 }
17176 }
17177 else if (best_row && !row->continued_p)
17178 return best_row;
17179 ++row;
17180 }
17181 }
17182
17183
17184 /* Try to redisplay window W by reusing its existing display. W's
17185 current matrix must be up to date when this function is called,
17186 i.e. window_end_valid must be nonzero.
17187
17188 Value is
17189
17190 1 if display has been updated
17191 0 if otherwise unsuccessful
17192 -1 if redisplay with same window start is known not to succeed
17193
17194 The following steps are performed:
17195
17196 1. Find the last row in the current matrix of W that is not
17197 affected by changes at the start of current_buffer. If no such row
17198 is found, give up.
17199
17200 2. Find the first row in W's current matrix that is not affected by
17201 changes at the end of current_buffer. Maybe there is no such row.
17202
17203 3. Display lines beginning with the row + 1 found in step 1 to the
17204 row found in step 2 or, if step 2 didn't find a row, to the end of
17205 the window.
17206
17207 4. If cursor is not known to appear on the window, give up.
17208
17209 5. If display stopped at the row found in step 2, scroll the
17210 display and current matrix as needed.
17211
17212 6. Maybe display some lines at the end of W, if we must. This can
17213 happen under various circumstances, like a partially visible line
17214 becoming fully visible, or because newly displayed lines are displayed
17215 in smaller font sizes.
17216
17217 7. Update W's window end information. */
17218
17219 static int
17220 try_window_id (struct window *w)
17221 {
17222 struct frame *f = XFRAME (w->frame);
17223 struct glyph_matrix *current_matrix = w->current_matrix;
17224 struct glyph_matrix *desired_matrix = w->desired_matrix;
17225 struct glyph_row *last_unchanged_at_beg_row;
17226 struct glyph_row *first_unchanged_at_end_row;
17227 struct glyph_row *row;
17228 struct glyph_row *bottom_row;
17229 int bottom_vpos;
17230 struct it it;
17231 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17232 int dvpos, dy;
17233 struct text_pos start_pos;
17234 struct run run;
17235 int first_unchanged_at_end_vpos = 0;
17236 struct glyph_row *last_text_row, *last_text_row_at_end;
17237 struct text_pos start;
17238 ptrdiff_t first_changed_charpos, last_changed_charpos;
17239
17240 #ifdef GLYPH_DEBUG
17241 if (inhibit_try_window_id)
17242 return 0;
17243 #endif
17244
17245 #ifdef HAVE_XWIDGETS_xxx
17246 //maybe needed for proper xwidget movement
17247 printf("try_window_id\n");
17248 return -1;
17249 #endif
17250
17251
17252 /* This is handy for debugging. */
17253 #if 0
17254 #define GIVE_UP(X) \
17255 do { \
17256 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17257 return 0; \
17258 } while (0)
17259 #else
17260 #define GIVE_UP(X) return 0
17261 #endif
17262
17263 SET_TEXT_POS_FROM_MARKER (start, w->start);
17264
17265 /* Don't use this for mini-windows because these can show
17266 messages and mini-buffers, and we don't handle that here. */
17267 if (MINI_WINDOW_P (w))
17268 GIVE_UP (1);
17269
17270 /* This flag is used to prevent redisplay optimizations. */
17271 if (windows_or_buffers_changed || cursor_type_changed)
17272 GIVE_UP (2);
17273
17274 /* Verify that narrowing has not changed.
17275 Also verify that we were not told to prevent redisplay optimizations.
17276 It would be nice to further
17277 reduce the number of cases where this prevents try_window_id. */
17278 if (current_buffer->clip_changed
17279 || current_buffer->prevent_redisplay_optimizations_p)
17280 GIVE_UP (3);
17281
17282 /* Window must either use window-based redisplay or be full width. */
17283 if (!FRAME_WINDOW_P (f)
17284 && (!FRAME_LINE_INS_DEL_OK (f)
17285 || !WINDOW_FULL_WIDTH_P (w)))
17286 GIVE_UP (4);
17287
17288 /* Give up if point is known NOT to appear in W. */
17289 if (PT < CHARPOS (start))
17290 GIVE_UP (5);
17291
17292 /* Another way to prevent redisplay optimizations. */
17293 if (w->last_modified == 0)
17294 GIVE_UP (6);
17295
17296 /* Verify that window is not hscrolled. */
17297 if (w->hscroll != 0)
17298 GIVE_UP (7);
17299
17300 /* Verify that display wasn't paused. */
17301 if (!w->window_end_valid)
17302 GIVE_UP (8);
17303
17304 /* Can't use this if highlighting a region because a cursor movement
17305 will do more than just set the cursor. */
17306 if (markpos_of_region () >= 0)
17307 GIVE_UP (9);
17308
17309 /* Likewise if highlighting trailing whitespace. */
17310 if (!NILP (Vshow_trailing_whitespace))
17311 GIVE_UP (11);
17312
17313 /* Likewise if showing a region. */
17314 if (w->region_showing)
17315 GIVE_UP (10);
17316
17317 /* Can't use this if overlay arrow position and/or string have
17318 changed. */
17319 if (overlay_arrows_changed_p ())
17320 GIVE_UP (12);
17321
17322 /* When word-wrap is on, adding a space to the first word of a
17323 wrapped line can change the wrap position, altering the line
17324 above it. It might be worthwhile to handle this more
17325 intelligently, but for now just redisplay from scratch. */
17326 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17327 GIVE_UP (21);
17328
17329 /* Under bidi reordering, adding or deleting a character in the
17330 beginning of a paragraph, before the first strong directional
17331 character, can change the base direction of the paragraph (unless
17332 the buffer specifies a fixed paragraph direction), which will
17333 require to redisplay the whole paragraph. It might be worthwhile
17334 to find the paragraph limits and widen the range of redisplayed
17335 lines to that, but for now just give up this optimization and
17336 redisplay from scratch. */
17337 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17338 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17339 GIVE_UP (22);
17340
17341 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17342 only if buffer has really changed. The reason is that the gap is
17343 initially at Z for freshly visited files. The code below would
17344 set end_unchanged to 0 in that case. */
17345 if (MODIFF > SAVE_MODIFF
17346 /* This seems to happen sometimes after saving a buffer. */
17347 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17348 {
17349 if (GPT - BEG < BEG_UNCHANGED)
17350 BEG_UNCHANGED = GPT - BEG;
17351 if (Z - GPT < END_UNCHANGED)
17352 END_UNCHANGED = Z - GPT;
17353 }
17354
17355 /* The position of the first and last character that has been changed. */
17356 first_changed_charpos = BEG + BEG_UNCHANGED;
17357 last_changed_charpos = Z - END_UNCHANGED;
17358
17359 /* If window starts after a line end, and the last change is in
17360 front of that newline, then changes don't affect the display.
17361 This case happens with stealth-fontification. Note that although
17362 the display is unchanged, glyph positions in the matrix have to
17363 be adjusted, of course. */
17364 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17365 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17366 && ((last_changed_charpos < CHARPOS (start)
17367 && CHARPOS (start) == BEGV)
17368 || (last_changed_charpos < CHARPOS (start) - 1
17369 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17370 {
17371 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17372 struct glyph_row *r0;
17373
17374 /* Compute how many chars/bytes have been added to or removed
17375 from the buffer. */
17376 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17377 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17378 Z_delta = Z - Z_old;
17379 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17380
17381 /* Give up if PT is not in the window. Note that it already has
17382 been checked at the start of try_window_id that PT is not in
17383 front of the window start. */
17384 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17385 GIVE_UP (13);
17386
17387 /* If window start is unchanged, we can reuse the whole matrix
17388 as is, after adjusting glyph positions. No need to compute
17389 the window end again, since its offset from Z hasn't changed. */
17390 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17391 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17392 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17393 /* PT must not be in a partially visible line. */
17394 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17395 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17396 {
17397 /* Adjust positions in the glyph matrix. */
17398 if (Z_delta || Z_delta_bytes)
17399 {
17400 struct glyph_row *r1
17401 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17402 increment_matrix_positions (w->current_matrix,
17403 MATRIX_ROW_VPOS (r0, current_matrix),
17404 MATRIX_ROW_VPOS (r1, current_matrix),
17405 Z_delta, Z_delta_bytes);
17406 }
17407
17408 /* Set the cursor. */
17409 row = row_containing_pos (w, PT, r0, NULL, 0);
17410 if (row)
17411 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17412 else
17413 emacs_abort ();
17414 return 1;
17415 }
17416 }
17417
17418 /* Handle the case that changes are all below what is displayed in
17419 the window, and that PT is in the window. This shortcut cannot
17420 be taken if ZV is visible in the window, and text has been added
17421 there that is visible in the window. */
17422 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17423 /* ZV is not visible in the window, or there are no
17424 changes at ZV, actually. */
17425 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17426 || first_changed_charpos == last_changed_charpos))
17427 {
17428 struct glyph_row *r0;
17429
17430 /* Give up if PT is not in the window. Note that it already has
17431 been checked at the start of try_window_id that PT is not in
17432 front of the window start. */
17433 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17434 GIVE_UP (14);
17435
17436 /* If window start is unchanged, we can reuse the whole matrix
17437 as is, without changing glyph positions since no text has
17438 been added/removed in front of the window end. */
17439 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17440 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17441 /* PT must not be in a partially visible line. */
17442 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17443 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17444 {
17445 /* We have to compute the window end anew since text
17446 could have been added/removed after it. */
17447 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17448 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17449
17450 /* Set the cursor. */
17451 row = row_containing_pos (w, PT, r0, NULL, 0);
17452 if (row)
17453 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17454 else
17455 emacs_abort ();
17456 return 2;
17457 }
17458 }
17459
17460 /* Give up if window start is in the changed area.
17461
17462 The condition used to read
17463
17464 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17465
17466 but why that was tested escapes me at the moment. */
17467 if (CHARPOS (start) >= first_changed_charpos
17468 && CHARPOS (start) <= last_changed_charpos)
17469 GIVE_UP (15);
17470
17471 /* Check that window start agrees with the start of the first glyph
17472 row in its current matrix. Check this after we know the window
17473 start is not in changed text, otherwise positions would not be
17474 comparable. */
17475 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17476 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17477 GIVE_UP (16);
17478
17479 /* Give up if the window ends in strings. Overlay strings
17480 at the end are difficult to handle, so don't try. */
17481 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17482 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17483 GIVE_UP (20);
17484
17485 /* Compute the position at which we have to start displaying new
17486 lines. Some of the lines at the top of the window might be
17487 reusable because they are not displaying changed text. Find the
17488 last row in W's current matrix not affected by changes at the
17489 start of current_buffer. Value is null if changes start in the
17490 first line of window. */
17491 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17492 if (last_unchanged_at_beg_row)
17493 {
17494 /* Avoid starting to display in the middle of a character, a TAB
17495 for instance. This is easier than to set up the iterator
17496 exactly, and it's not a frequent case, so the additional
17497 effort wouldn't really pay off. */
17498 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17499 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17500 && last_unchanged_at_beg_row > w->current_matrix->rows)
17501 --last_unchanged_at_beg_row;
17502
17503 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17504 GIVE_UP (17);
17505
17506 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17507 GIVE_UP (18);
17508 start_pos = it.current.pos;
17509
17510 /* Start displaying new lines in the desired matrix at the same
17511 vpos we would use in the current matrix, i.e. below
17512 last_unchanged_at_beg_row. */
17513 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17514 current_matrix);
17515 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17516 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17517
17518 eassert (it.hpos == 0 && it.current_x == 0);
17519 }
17520 else
17521 {
17522 /* There are no reusable lines at the start of the window.
17523 Start displaying in the first text line. */
17524 start_display (&it, w, start);
17525 it.vpos = it.first_vpos;
17526 start_pos = it.current.pos;
17527 }
17528
17529 /* Find the first row that is not affected by changes at the end of
17530 the buffer. Value will be null if there is no unchanged row, in
17531 which case we must redisplay to the end of the window. delta
17532 will be set to the value by which buffer positions beginning with
17533 first_unchanged_at_end_row have to be adjusted due to text
17534 changes. */
17535 first_unchanged_at_end_row
17536 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17537 IF_DEBUG (debug_delta = delta);
17538 IF_DEBUG (debug_delta_bytes = delta_bytes);
17539
17540 /* Set stop_pos to the buffer position up to which we will have to
17541 display new lines. If first_unchanged_at_end_row != NULL, this
17542 is the buffer position of the start of the line displayed in that
17543 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17544 that we don't stop at a buffer position. */
17545 stop_pos = 0;
17546 if (first_unchanged_at_end_row)
17547 {
17548 eassert (last_unchanged_at_beg_row == NULL
17549 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17550
17551 /* If this is a continuation line, move forward to the next one
17552 that isn't. Changes in lines above affect this line.
17553 Caution: this may move first_unchanged_at_end_row to a row
17554 not displaying text. */
17555 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17556 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17557 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17558 < it.last_visible_y))
17559 ++first_unchanged_at_end_row;
17560
17561 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17562 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17563 >= it.last_visible_y))
17564 first_unchanged_at_end_row = NULL;
17565 else
17566 {
17567 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17568 + delta);
17569 first_unchanged_at_end_vpos
17570 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17571 eassert (stop_pos >= Z - END_UNCHANGED);
17572 }
17573 }
17574 else if (last_unchanged_at_beg_row == NULL)
17575 GIVE_UP (19);
17576
17577
17578 #ifdef GLYPH_DEBUG
17579
17580 /* Either there is no unchanged row at the end, or the one we have
17581 now displays text. This is a necessary condition for the window
17582 end pos calculation at the end of this function. */
17583 eassert (first_unchanged_at_end_row == NULL
17584 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17585
17586 debug_last_unchanged_at_beg_vpos
17587 = (last_unchanged_at_beg_row
17588 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17589 : -1);
17590 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17591
17592 #endif /* GLYPH_DEBUG */
17593
17594
17595 /* Display new lines. Set last_text_row to the last new line
17596 displayed which has text on it, i.e. might end up as being the
17597 line where the window_end_vpos is. */
17598 w->cursor.vpos = -1;
17599 last_text_row = NULL;
17600 overlay_arrow_seen = 0;
17601 while (it.current_y < it.last_visible_y
17602 && !fonts_changed_p
17603 && (first_unchanged_at_end_row == NULL
17604 || IT_CHARPOS (it) < stop_pos))
17605 {
17606 if (display_line (&it))
17607 last_text_row = it.glyph_row - 1;
17608 }
17609
17610 if (fonts_changed_p)
17611 return -1;
17612
17613
17614 /* Compute differences in buffer positions, y-positions etc. for
17615 lines reused at the bottom of the window. Compute what we can
17616 scroll. */
17617 if (first_unchanged_at_end_row
17618 /* No lines reused because we displayed everything up to the
17619 bottom of the window. */
17620 && it.current_y < it.last_visible_y)
17621 {
17622 dvpos = (it.vpos
17623 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17624 current_matrix));
17625 dy = it.current_y - first_unchanged_at_end_row->y;
17626 run.current_y = first_unchanged_at_end_row->y;
17627 run.desired_y = run.current_y + dy;
17628 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17629 }
17630 else
17631 {
17632 delta = delta_bytes = dvpos = dy
17633 = run.current_y = run.desired_y = run.height = 0;
17634 first_unchanged_at_end_row = NULL;
17635 }
17636 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17637
17638
17639 /* Find the cursor if not already found. We have to decide whether
17640 PT will appear on this window (it sometimes doesn't, but this is
17641 not a very frequent case.) This decision has to be made before
17642 the current matrix is altered. A value of cursor.vpos < 0 means
17643 that PT is either in one of the lines beginning at
17644 first_unchanged_at_end_row or below the window. Don't care for
17645 lines that might be displayed later at the window end; as
17646 mentioned, this is not a frequent case. */
17647 if (w->cursor.vpos < 0)
17648 {
17649 /* Cursor in unchanged rows at the top? */
17650 if (PT < CHARPOS (start_pos)
17651 && last_unchanged_at_beg_row)
17652 {
17653 row = row_containing_pos (w, PT,
17654 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17655 last_unchanged_at_beg_row + 1, 0);
17656 if (row)
17657 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17658 }
17659
17660 /* Start from first_unchanged_at_end_row looking for PT. */
17661 else if (first_unchanged_at_end_row)
17662 {
17663 row = row_containing_pos (w, PT - delta,
17664 first_unchanged_at_end_row, NULL, 0);
17665 if (row)
17666 set_cursor_from_row (w, row, w->current_matrix, delta,
17667 delta_bytes, dy, dvpos);
17668 }
17669
17670 /* Give up if cursor was not found. */
17671 if (w->cursor.vpos < 0)
17672 {
17673 clear_glyph_matrix (w->desired_matrix);
17674 return -1;
17675 }
17676 }
17677
17678 /* Don't let the cursor end in the scroll margins. */
17679 {
17680 int this_scroll_margin, cursor_height;
17681 int frame_line_height = default_line_pixel_height (w);
17682 int window_total_lines
17683 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17684
17685 this_scroll_margin =
17686 max (0, min (scroll_margin, window_total_lines / 4));
17687 this_scroll_margin *= frame_line_height;
17688 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17689
17690 if ((w->cursor.y < this_scroll_margin
17691 && CHARPOS (start) > BEGV)
17692 /* Old redisplay didn't take scroll margin into account at the bottom,
17693 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17694 || (w->cursor.y + (make_cursor_line_fully_visible_p
17695 ? cursor_height + this_scroll_margin
17696 : 1)) > it.last_visible_y)
17697 {
17698 w->cursor.vpos = -1;
17699 clear_glyph_matrix (w->desired_matrix);
17700 return -1;
17701 }
17702 }
17703
17704 /* Scroll the display. Do it before changing the current matrix so
17705 that xterm.c doesn't get confused about where the cursor glyph is
17706 found. */
17707 if (dy && run.height)
17708 {
17709 update_begin (f);
17710
17711 if (FRAME_WINDOW_P (f))
17712 {
17713 FRAME_RIF (f)->update_window_begin_hook (w);
17714 FRAME_RIF (f)->clear_window_mouse_face (w);
17715 FRAME_RIF (f)->scroll_run_hook (w, &run);
17716 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17717 }
17718 else
17719 {
17720 /* Terminal frame. In this case, dvpos gives the number of
17721 lines to scroll by; dvpos < 0 means scroll up. */
17722 int from_vpos
17723 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17724 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17725 int end = (WINDOW_TOP_EDGE_LINE (w)
17726 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17727 + window_internal_height (w));
17728
17729 #if defined (HAVE_GPM) || defined (MSDOS)
17730 x_clear_window_mouse_face (w);
17731 #endif
17732 /* Perform the operation on the screen. */
17733 if (dvpos > 0)
17734 {
17735 /* Scroll last_unchanged_at_beg_row to the end of the
17736 window down dvpos lines. */
17737 set_terminal_window (f, end);
17738
17739 /* On dumb terminals delete dvpos lines at the end
17740 before inserting dvpos empty lines. */
17741 if (!FRAME_SCROLL_REGION_OK (f))
17742 ins_del_lines (f, end - dvpos, -dvpos);
17743
17744 /* Insert dvpos empty lines in front of
17745 last_unchanged_at_beg_row. */
17746 ins_del_lines (f, from, dvpos);
17747 }
17748 else if (dvpos < 0)
17749 {
17750 /* Scroll up last_unchanged_at_beg_vpos to the end of
17751 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17752 set_terminal_window (f, end);
17753
17754 /* Delete dvpos lines in front of
17755 last_unchanged_at_beg_vpos. ins_del_lines will set
17756 the cursor to the given vpos and emit |dvpos| delete
17757 line sequences. */
17758 ins_del_lines (f, from + dvpos, dvpos);
17759
17760 /* On a dumb terminal insert dvpos empty lines at the
17761 end. */
17762 if (!FRAME_SCROLL_REGION_OK (f))
17763 ins_del_lines (f, end + dvpos, -dvpos);
17764 }
17765
17766 set_terminal_window (f, 0);
17767 }
17768
17769 update_end (f);
17770 }
17771
17772 /* Shift reused rows of the current matrix to the right position.
17773 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17774 text. */
17775 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17776 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17777 if (dvpos < 0)
17778 {
17779 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17780 bottom_vpos, dvpos);
17781 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17782 bottom_vpos);
17783 }
17784 else if (dvpos > 0)
17785 {
17786 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17787 bottom_vpos, dvpos);
17788 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17789 first_unchanged_at_end_vpos + dvpos);
17790 }
17791
17792 /* For frame-based redisplay, make sure that current frame and window
17793 matrix are in sync with respect to glyph memory. */
17794 if (!FRAME_WINDOW_P (f))
17795 sync_frame_with_window_matrix_rows (w);
17796
17797 /* Adjust buffer positions in reused rows. */
17798 if (delta || delta_bytes)
17799 increment_matrix_positions (current_matrix,
17800 first_unchanged_at_end_vpos + dvpos,
17801 bottom_vpos, delta, delta_bytes);
17802
17803 /* Adjust Y positions. */
17804 if (dy)
17805 shift_glyph_matrix (w, current_matrix,
17806 first_unchanged_at_end_vpos + dvpos,
17807 bottom_vpos, dy);
17808
17809 if (first_unchanged_at_end_row)
17810 {
17811 first_unchanged_at_end_row += dvpos;
17812 if (first_unchanged_at_end_row->y >= it.last_visible_y
17813 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17814 first_unchanged_at_end_row = NULL;
17815 }
17816
17817 /* If scrolling up, there may be some lines to display at the end of
17818 the window. */
17819 last_text_row_at_end = NULL;
17820 if (dy < 0)
17821 {
17822 /* Scrolling up can leave for example a partially visible line
17823 at the end of the window to be redisplayed. */
17824 /* Set last_row to the glyph row in the current matrix where the
17825 window end line is found. It has been moved up or down in
17826 the matrix by dvpos. */
17827 int last_vpos = w->window_end_vpos + dvpos;
17828 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17829
17830 /* If last_row is the window end line, it should display text. */
17831 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17832
17833 /* If window end line was partially visible before, begin
17834 displaying at that line. Otherwise begin displaying with the
17835 line following it. */
17836 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17837 {
17838 init_to_row_start (&it, w, last_row);
17839 it.vpos = last_vpos;
17840 it.current_y = last_row->y;
17841 }
17842 else
17843 {
17844 init_to_row_end (&it, w, last_row);
17845 it.vpos = 1 + last_vpos;
17846 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17847 ++last_row;
17848 }
17849
17850 /* We may start in a continuation line. If so, we have to
17851 get the right continuation_lines_width and current_x. */
17852 it.continuation_lines_width = last_row->continuation_lines_width;
17853 it.hpos = it.current_x = 0;
17854
17855 /* Display the rest of the lines at the window end. */
17856 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17857 while (it.current_y < it.last_visible_y
17858 && !fonts_changed_p)
17859 {
17860 /* Is it always sure that the display agrees with lines in
17861 the current matrix? I don't think so, so we mark rows
17862 displayed invalid in the current matrix by setting their
17863 enabled_p flag to zero. */
17864 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17865 if (display_line (&it))
17866 last_text_row_at_end = it.glyph_row - 1;
17867 }
17868 }
17869
17870 /* Update window_end_pos and window_end_vpos. */
17871 if (first_unchanged_at_end_row && !last_text_row_at_end)
17872 {
17873 /* Window end line if one of the preserved rows from the current
17874 matrix. Set row to the last row displaying text in current
17875 matrix starting at first_unchanged_at_end_row, after
17876 scrolling. */
17877 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17878 row = find_last_row_displaying_text (w->current_matrix, &it,
17879 first_unchanged_at_end_row);
17880 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17881 adjust_window_ends (w, row, 1);
17882 eassert (w->window_end_bytepos >= 0);
17883 IF_DEBUG (debug_method_add (w, "A"));
17884 }
17885 else if (last_text_row_at_end)
17886 {
17887 adjust_window_ends (w, last_text_row_at_end, 0);
17888 eassert (w->window_end_bytepos >= 0);
17889 IF_DEBUG (debug_method_add (w, "B"));
17890 }
17891 else if (last_text_row)
17892 {
17893 /* We have displayed either to the end of the window or at the
17894 end of the window, i.e. the last row with text is to be found
17895 in the desired matrix. */
17896 adjust_window_ends (w, last_text_row, 0);
17897 eassert (w->window_end_bytepos >= 0);
17898 }
17899 else if (first_unchanged_at_end_row == NULL
17900 && last_text_row == NULL
17901 && last_text_row_at_end == NULL)
17902 {
17903 /* Displayed to end of window, but no line containing text was
17904 displayed. Lines were deleted at the end of the window. */
17905 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17906 int vpos = w->window_end_vpos;
17907 struct glyph_row *current_row = current_matrix->rows + vpos;
17908 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17909
17910 for (row = NULL;
17911 row == NULL && vpos >= first_vpos;
17912 --vpos, --current_row, --desired_row)
17913 {
17914 if (desired_row->enabled_p)
17915 {
17916 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17917 row = desired_row;
17918 }
17919 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17920 row = current_row;
17921 }
17922
17923 eassert (row != NULL);
17924 w->window_end_vpos = vpos + 1;
17925 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17926 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17927 eassert (w->window_end_bytepos >= 0);
17928 IF_DEBUG (debug_method_add (w, "C"));
17929 }
17930 else
17931 emacs_abort ();
17932
17933 IF_DEBUG (debug_end_pos = w->window_end_pos;
17934 debug_end_vpos = w->window_end_vpos);
17935
17936 /* Record that display has not been completed. */
17937 w->window_end_valid = 0;
17938 w->desired_matrix->no_scrolling_p = 1;
17939 return 3;
17940
17941 #undef GIVE_UP
17942 }
17943
17944
17945 \f
17946 /***********************************************************************
17947 More debugging support
17948 ***********************************************************************/
17949
17950 #ifdef GLYPH_DEBUG
17951
17952 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17953 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17954 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17955
17956
17957 /* Dump the contents of glyph matrix MATRIX on stderr.
17958
17959 GLYPHS 0 means don't show glyph contents.
17960 GLYPHS 1 means show glyphs in short form
17961 GLYPHS > 1 means show glyphs in long form. */
17962
17963 void
17964 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17965 {
17966 int i;
17967 for (i = 0; i < matrix->nrows; ++i)
17968 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17969 }
17970
17971
17972 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17973 the glyph row and area where the glyph comes from. */
17974
17975 void
17976 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17977 {
17978 if (glyph->type == CHAR_GLYPH
17979 || glyph->type == GLYPHLESS_GLYPH)
17980 {
17981 fprintf (stderr,
17982 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17983 glyph - row->glyphs[TEXT_AREA],
17984 (glyph->type == CHAR_GLYPH
17985 ? 'C'
17986 : 'G'),
17987 glyph->charpos,
17988 (BUFFERP (glyph->object)
17989 ? 'B'
17990 : (STRINGP (glyph->object)
17991 ? 'S'
17992 : (INTEGERP (glyph->object)
17993 ? '0'
17994 : '-'))),
17995 glyph->pixel_width,
17996 glyph->u.ch,
17997 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17998 ? glyph->u.ch
17999 : '.'),
18000 glyph->face_id,
18001 glyph->left_box_line_p,
18002 glyph->right_box_line_p);
18003 }
18004 else if (glyph->type == STRETCH_GLYPH)
18005 {
18006 fprintf (stderr,
18007 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18008 glyph - row->glyphs[TEXT_AREA],
18009 'S',
18010 glyph->charpos,
18011 (BUFFERP (glyph->object)
18012 ? 'B'
18013 : (STRINGP (glyph->object)
18014 ? 'S'
18015 : (INTEGERP (glyph->object)
18016 ? '0'
18017 : '-'))),
18018 glyph->pixel_width,
18019 0,
18020 ' ',
18021 glyph->face_id,
18022 glyph->left_box_line_p,
18023 glyph->right_box_line_p);
18024 }
18025 else if (glyph->type == IMAGE_GLYPH)
18026 {
18027 fprintf (stderr,
18028 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18029 glyph - row->glyphs[TEXT_AREA],
18030 'I',
18031 glyph->charpos,
18032 (BUFFERP (glyph->object)
18033 ? 'B'
18034 : (STRINGP (glyph->object)
18035 ? 'S'
18036 : (INTEGERP (glyph->object)
18037 ? '0'
18038 : '-'))),
18039 glyph->pixel_width,
18040 glyph->u.img_id,
18041 '.',
18042 glyph->face_id,
18043 glyph->left_box_line_p,
18044 glyph->right_box_line_p);
18045 }
18046 else if (glyph->type == COMPOSITE_GLYPH)
18047 {
18048 fprintf (stderr,
18049 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18050 glyph - row->glyphs[TEXT_AREA],
18051 '+',
18052 glyph->charpos,
18053 (BUFFERP (glyph->object)
18054 ? 'B'
18055 : (STRINGP (glyph->object)
18056 ? 'S'
18057 : (INTEGERP (glyph->object)
18058 ? '0'
18059 : '-'))),
18060 glyph->pixel_width,
18061 glyph->u.cmp.id);
18062 if (glyph->u.cmp.automatic)
18063 fprintf (stderr,
18064 "[%d-%d]",
18065 glyph->slice.cmp.from, glyph->slice.cmp.to);
18066 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18067 glyph->face_id,
18068 glyph->left_box_line_p,
18069 glyph->right_box_line_p);
18070 }
18071 #ifdef HAVE_XWIDGETS
18072 else if (glyph->type == XWIDGET_GLYPH)
18073 {
18074 fprintf (stderr,
18075 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18076 glyph - row->glyphs[TEXT_AREA],
18077 'X',
18078 glyph->charpos,
18079 (BUFFERP (glyph->object)
18080 ? 'B'
18081 : (STRINGP (glyph->object)
18082 ? 'S'
18083 : '-')),
18084 glyph->pixel_width,
18085 glyph->u.xwidget,
18086 '.',
18087 glyph->face_id,
18088 glyph->left_box_line_p,
18089 glyph->right_box_line_p);
18090
18091 // printf("dump xwidget glyph\n");
18092 }
18093 #endif
18094 }
18095
18096
18097 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18098 GLYPHS 0 means don't show glyph contents.
18099 GLYPHS 1 means show glyphs in short form
18100 GLYPHS > 1 means show glyphs in long form. */
18101
18102 void
18103 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18104 {
18105 if (glyphs != 1)
18106 {
18107 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18108 fprintf (stderr, "==============================================================================\n");
18109
18110 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18111 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18112 vpos,
18113 MATRIX_ROW_START_CHARPOS (row),
18114 MATRIX_ROW_END_CHARPOS (row),
18115 row->used[TEXT_AREA],
18116 row->contains_overlapping_glyphs_p,
18117 row->enabled_p,
18118 row->truncated_on_left_p,
18119 row->truncated_on_right_p,
18120 row->continued_p,
18121 MATRIX_ROW_CONTINUATION_LINE_P (row),
18122 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18123 row->ends_at_zv_p,
18124 row->fill_line_p,
18125 row->ends_in_middle_of_char_p,
18126 row->starts_in_middle_of_char_p,
18127 row->mouse_face_p,
18128 row->x,
18129 row->y,
18130 row->pixel_width,
18131 row->height,
18132 row->visible_height,
18133 row->ascent,
18134 row->phys_ascent);
18135 /* The next 3 lines should align to "Start" in the header. */
18136 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18137 row->end.overlay_string_index,
18138 row->continuation_lines_width);
18139 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18140 CHARPOS (row->start.string_pos),
18141 CHARPOS (row->end.string_pos));
18142 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18143 row->end.dpvec_index);
18144 }
18145
18146 if (glyphs > 1)
18147 {
18148 int area;
18149
18150 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18151 {
18152 struct glyph *glyph = row->glyphs[area];
18153 struct glyph *glyph_end = glyph + row->used[area];
18154
18155 /* Glyph for a line end in text. */
18156 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18157 ++glyph_end;
18158
18159 if (glyph < glyph_end)
18160 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18161
18162 for (; glyph < glyph_end; ++glyph)
18163 dump_glyph (row, glyph, area);
18164 }
18165 }
18166 else if (glyphs == 1)
18167 {
18168 int area;
18169
18170 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18171 {
18172 char *s = alloca (row->used[area] + 4);
18173 int i;
18174
18175 for (i = 0; i < row->used[area]; ++i)
18176 {
18177 struct glyph *glyph = row->glyphs[area] + i;
18178 if (i == row->used[area] - 1
18179 && area == TEXT_AREA
18180 && INTEGERP (glyph->object)
18181 && glyph->type == CHAR_GLYPH
18182 && glyph->u.ch == ' ')
18183 {
18184 strcpy (&s[i], "[\\n]");
18185 i += 4;
18186 }
18187 else if (glyph->type == CHAR_GLYPH
18188 && glyph->u.ch < 0x80
18189 && glyph->u.ch >= ' ')
18190 s[i] = glyph->u.ch;
18191 else
18192 s[i] = '.';
18193 }
18194
18195 s[i] = '\0';
18196 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18197 }
18198 }
18199 }
18200
18201
18202 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18203 Sdump_glyph_matrix, 0, 1, "p",
18204 doc: /* Dump the current matrix of the selected window to stderr.
18205 Shows contents of glyph row structures. With non-nil
18206 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18207 glyphs in short form, otherwise show glyphs in long form. */)
18208 (Lisp_Object glyphs)
18209 {
18210 struct window *w = XWINDOW (selected_window);
18211 struct buffer *buffer = XBUFFER (w->contents);
18212
18213 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18214 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18215 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18216 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18217 fprintf (stderr, "=============================================\n");
18218 dump_glyph_matrix (w->current_matrix,
18219 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18220 return Qnil;
18221 }
18222
18223
18224 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18225 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18226 (void)
18227 {
18228 struct frame *f = XFRAME (selected_frame);
18229 dump_glyph_matrix (f->current_matrix, 1);
18230 return Qnil;
18231 }
18232
18233
18234 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18235 doc: /* Dump glyph row ROW to stderr.
18236 GLYPH 0 means don't dump glyphs.
18237 GLYPH 1 means dump glyphs in short form.
18238 GLYPH > 1 or omitted means dump glyphs in long form. */)
18239 (Lisp_Object row, Lisp_Object glyphs)
18240 {
18241 struct glyph_matrix *matrix;
18242 EMACS_INT vpos;
18243
18244 CHECK_NUMBER (row);
18245 matrix = XWINDOW (selected_window)->current_matrix;
18246 vpos = XINT (row);
18247 if (vpos >= 0 && vpos < matrix->nrows)
18248 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18249 vpos,
18250 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18251 return Qnil;
18252 }
18253
18254
18255 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18256 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18257 GLYPH 0 means don't dump glyphs.
18258 GLYPH 1 means dump glyphs in short form.
18259 GLYPH > 1 or omitted means dump glyphs in long form. */)
18260 (Lisp_Object row, Lisp_Object glyphs)
18261 {
18262 struct frame *sf = SELECTED_FRAME ();
18263 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18264 EMACS_INT vpos;
18265
18266 CHECK_NUMBER (row);
18267 vpos = XINT (row);
18268 if (vpos >= 0 && vpos < m->nrows)
18269 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18270 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18271 return Qnil;
18272 }
18273
18274
18275 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18276 doc: /* Toggle tracing of redisplay.
18277 With ARG, turn tracing on if and only if ARG is positive. */)
18278 (Lisp_Object arg)
18279 {
18280 if (NILP (arg))
18281 trace_redisplay_p = !trace_redisplay_p;
18282 else
18283 {
18284 arg = Fprefix_numeric_value (arg);
18285 trace_redisplay_p = XINT (arg) > 0;
18286 }
18287
18288 return Qnil;
18289 }
18290
18291
18292 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18293 doc: /* Like `format', but print result to stderr.
18294 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18295 (ptrdiff_t nargs, Lisp_Object *args)
18296 {
18297 Lisp_Object s = Fformat (nargs, args);
18298 fprintf (stderr, "%s", SDATA (s));
18299 return Qnil;
18300 }
18301
18302 #endif /* GLYPH_DEBUG */
18303
18304
18305 \f
18306 /***********************************************************************
18307 Building Desired Matrix Rows
18308 ***********************************************************************/
18309
18310 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18311 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18312
18313 static struct glyph_row *
18314 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18315 {
18316 struct frame *f = XFRAME (WINDOW_FRAME (w));
18317 struct buffer *buffer = XBUFFER (w->contents);
18318 struct buffer *old = current_buffer;
18319 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18320 int arrow_len = SCHARS (overlay_arrow_string);
18321 const unsigned char *arrow_end = arrow_string + arrow_len;
18322 const unsigned char *p;
18323 struct it it;
18324 bool multibyte_p;
18325 int n_glyphs_before;
18326
18327 set_buffer_temp (buffer);
18328 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18329 it.glyph_row->used[TEXT_AREA] = 0;
18330 SET_TEXT_POS (it.position, 0, 0);
18331
18332 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18333 p = arrow_string;
18334 while (p < arrow_end)
18335 {
18336 Lisp_Object face, ilisp;
18337
18338 /* Get the next character. */
18339 if (multibyte_p)
18340 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18341 else
18342 {
18343 it.c = it.char_to_display = *p, it.len = 1;
18344 if (! ASCII_CHAR_P (it.c))
18345 it.char_to_display = BYTE8_TO_CHAR (it.c);
18346 }
18347 p += it.len;
18348
18349 /* Get its face. */
18350 ilisp = make_number (p - arrow_string);
18351 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18352 it.face_id = compute_char_face (f, it.char_to_display, face);
18353
18354 /* Compute its width, get its glyphs. */
18355 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18356 SET_TEXT_POS (it.position, -1, -1);
18357 PRODUCE_GLYPHS (&it);
18358
18359 /* If this character doesn't fit any more in the line, we have
18360 to remove some glyphs. */
18361 if (it.current_x > it.last_visible_x)
18362 {
18363 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18364 break;
18365 }
18366 }
18367
18368 set_buffer_temp (old);
18369 return it.glyph_row;
18370 }
18371
18372
18373 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18374 glyphs to insert is determined by produce_special_glyphs. */
18375
18376 static void
18377 insert_left_trunc_glyphs (struct it *it)
18378 {
18379 struct it truncate_it;
18380 struct glyph *from, *end, *to, *toend;
18381
18382 eassert (!FRAME_WINDOW_P (it->f)
18383 || (!it->glyph_row->reversed_p
18384 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18385 || (it->glyph_row->reversed_p
18386 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18387
18388 /* Get the truncation glyphs. */
18389 truncate_it = *it;
18390 truncate_it.current_x = 0;
18391 truncate_it.face_id = DEFAULT_FACE_ID;
18392 truncate_it.glyph_row = &scratch_glyph_row;
18393 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18394 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18395 truncate_it.object = make_number (0);
18396 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18397
18398 /* Overwrite glyphs from IT with truncation glyphs. */
18399 if (!it->glyph_row->reversed_p)
18400 {
18401 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18402
18403 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18404 end = from + tused;
18405 to = it->glyph_row->glyphs[TEXT_AREA];
18406 toend = to + it->glyph_row->used[TEXT_AREA];
18407 if (FRAME_WINDOW_P (it->f))
18408 {
18409 /* On GUI frames, when variable-size fonts are displayed,
18410 the truncation glyphs may need more pixels than the row's
18411 glyphs they overwrite. We overwrite more glyphs to free
18412 enough screen real estate, and enlarge the stretch glyph
18413 on the right (see display_line), if there is one, to
18414 preserve the screen position of the truncation glyphs on
18415 the right. */
18416 int w = 0;
18417 struct glyph *g = to;
18418 short used;
18419
18420 /* The first glyph could be partially visible, in which case
18421 it->glyph_row->x will be negative. But we want the left
18422 truncation glyphs to be aligned at the left margin of the
18423 window, so we override the x coordinate at which the row
18424 will begin. */
18425 it->glyph_row->x = 0;
18426 while (g < toend && w < it->truncation_pixel_width)
18427 {
18428 w += g->pixel_width;
18429 ++g;
18430 }
18431 if (g - to - tused > 0)
18432 {
18433 memmove (to + tused, g, (toend - g) * sizeof(*g));
18434 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18435 }
18436 used = it->glyph_row->used[TEXT_AREA];
18437 if (it->glyph_row->truncated_on_right_p
18438 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18439 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18440 == STRETCH_GLYPH)
18441 {
18442 int extra = w - it->truncation_pixel_width;
18443
18444 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18445 }
18446 }
18447
18448 while (from < end)
18449 *to++ = *from++;
18450
18451 /* There may be padding glyphs left over. Overwrite them too. */
18452 if (!FRAME_WINDOW_P (it->f))
18453 {
18454 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18455 {
18456 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18457 while (from < end)
18458 *to++ = *from++;
18459 }
18460 }
18461
18462 if (to > toend)
18463 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18464 }
18465 else
18466 {
18467 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18468
18469 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18470 that back to front. */
18471 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18472 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18473 toend = it->glyph_row->glyphs[TEXT_AREA];
18474 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18475 if (FRAME_WINDOW_P (it->f))
18476 {
18477 int w = 0;
18478 struct glyph *g = to;
18479
18480 while (g >= toend && w < it->truncation_pixel_width)
18481 {
18482 w += g->pixel_width;
18483 --g;
18484 }
18485 if (to - g - tused > 0)
18486 to = g + tused;
18487 if (it->glyph_row->truncated_on_right_p
18488 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18489 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18490 {
18491 int extra = w - it->truncation_pixel_width;
18492
18493 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18494 }
18495 }
18496
18497 while (from >= end && to >= toend)
18498 *to-- = *from--;
18499 if (!FRAME_WINDOW_P (it->f))
18500 {
18501 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18502 {
18503 from =
18504 truncate_it.glyph_row->glyphs[TEXT_AREA]
18505 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18506 while (from >= end && to >= toend)
18507 *to-- = *from--;
18508 }
18509 }
18510 if (from >= end)
18511 {
18512 /* Need to free some room before prepending additional
18513 glyphs. */
18514 int move_by = from - end + 1;
18515 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18516 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18517
18518 for ( ; g >= g0; g--)
18519 g[move_by] = *g;
18520 while (from >= end)
18521 *to-- = *from--;
18522 it->glyph_row->used[TEXT_AREA] += move_by;
18523 }
18524 }
18525 }
18526
18527 /* Compute the hash code for ROW. */
18528 unsigned
18529 row_hash (struct glyph_row *row)
18530 {
18531 int area, k;
18532 unsigned hashval = 0;
18533
18534 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18535 for (k = 0; k < row->used[area]; ++k)
18536 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18537 + row->glyphs[area][k].u.val
18538 + row->glyphs[area][k].face_id
18539 + row->glyphs[area][k].padding_p
18540 + (row->glyphs[area][k].type << 2));
18541
18542 return hashval;
18543 }
18544
18545 /* Compute the pixel height and width of IT->glyph_row.
18546
18547 Most of the time, ascent and height of a display line will be equal
18548 to the max_ascent and max_height values of the display iterator
18549 structure. This is not the case if
18550
18551 1. We hit ZV without displaying anything. In this case, max_ascent
18552 and max_height will be zero.
18553
18554 2. We have some glyphs that don't contribute to the line height.
18555 (The glyph row flag contributes_to_line_height_p is for future
18556 pixmap extensions).
18557
18558 The first case is easily covered by using default values because in
18559 these cases, the line height does not really matter, except that it
18560 must not be zero. */
18561
18562 static void
18563 compute_line_metrics (struct it *it)
18564 {
18565 struct glyph_row *row = it->glyph_row;
18566
18567 if (FRAME_WINDOW_P (it->f))
18568 {
18569 int i, min_y, max_y;
18570
18571 /* The line may consist of one space only, that was added to
18572 place the cursor on it. If so, the row's height hasn't been
18573 computed yet. */
18574 if (row->height == 0)
18575 {
18576 if (it->max_ascent + it->max_descent == 0)
18577 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18578 row->ascent = it->max_ascent;
18579 row->height = it->max_ascent + it->max_descent;
18580 row->phys_ascent = it->max_phys_ascent;
18581 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18582 row->extra_line_spacing = it->max_extra_line_spacing;
18583 }
18584
18585 /* Compute the width of this line. */
18586 row->pixel_width = row->x;
18587 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18588 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18589
18590 eassert (row->pixel_width >= 0);
18591 eassert (row->ascent >= 0 && row->height > 0);
18592
18593 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18594 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18595
18596 /* If first line's physical ascent is larger than its logical
18597 ascent, use the physical ascent, and make the row taller.
18598 This makes accented characters fully visible. */
18599 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18600 && row->phys_ascent > row->ascent)
18601 {
18602 row->height += row->phys_ascent - row->ascent;
18603 row->ascent = row->phys_ascent;
18604 }
18605
18606 /* Compute how much of the line is visible. */
18607 row->visible_height = row->height;
18608
18609 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18610 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18611
18612 if (row->y < min_y)
18613 row->visible_height -= min_y - row->y;
18614 if (row->y + row->height > max_y)
18615 row->visible_height -= row->y + row->height - max_y;
18616 }
18617 else
18618 {
18619 row->pixel_width = row->used[TEXT_AREA];
18620 if (row->continued_p)
18621 row->pixel_width -= it->continuation_pixel_width;
18622 else if (row->truncated_on_right_p)
18623 row->pixel_width -= it->truncation_pixel_width;
18624 row->ascent = row->phys_ascent = 0;
18625 row->height = row->phys_height = row->visible_height = 1;
18626 row->extra_line_spacing = 0;
18627 }
18628
18629 /* Compute a hash code for this row. */
18630 row->hash = row_hash (row);
18631
18632 it->max_ascent = it->max_descent = 0;
18633 it->max_phys_ascent = it->max_phys_descent = 0;
18634 }
18635
18636
18637 /* Append one space to the glyph row of iterator IT if doing a
18638 window-based redisplay. The space has the same face as
18639 IT->face_id. Value is non-zero if a space was added.
18640
18641 This function is called to make sure that there is always one glyph
18642 at the end of a glyph row that the cursor can be set on under
18643 window-systems. (If there weren't such a glyph we would not know
18644 how wide and tall a box cursor should be displayed).
18645
18646 At the same time this space let's a nicely handle clearing to the
18647 end of the line if the row ends in italic text. */
18648
18649 static int
18650 append_space_for_newline (struct it *it, int default_face_p)
18651 {
18652 if (FRAME_WINDOW_P (it->f))
18653 {
18654 int n = it->glyph_row->used[TEXT_AREA];
18655
18656 if (it->glyph_row->glyphs[TEXT_AREA] + n
18657 < it->glyph_row->glyphs[1 + TEXT_AREA])
18658 {
18659 /* Save some values that must not be changed.
18660 Must save IT->c and IT->len because otherwise
18661 ITERATOR_AT_END_P wouldn't work anymore after
18662 append_space_for_newline has been called. */
18663 enum display_element_type saved_what = it->what;
18664 int saved_c = it->c, saved_len = it->len;
18665 int saved_char_to_display = it->char_to_display;
18666 int saved_x = it->current_x;
18667 int saved_face_id = it->face_id;
18668 int saved_box_end = it->end_of_box_run_p;
18669 struct text_pos saved_pos;
18670 Lisp_Object saved_object;
18671 struct face *face;
18672
18673 saved_object = it->object;
18674 saved_pos = it->position;
18675
18676 it->what = IT_CHARACTER;
18677 memset (&it->position, 0, sizeof it->position);
18678 it->object = make_number (0);
18679 it->c = it->char_to_display = ' ';
18680 it->len = 1;
18681
18682 /* If the default face was remapped, be sure to use the
18683 remapped face for the appended newline. */
18684 if (default_face_p)
18685 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18686 else if (it->face_before_selective_p)
18687 it->face_id = it->saved_face_id;
18688 face = FACE_FROM_ID (it->f, it->face_id);
18689 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18690 /* In R2L rows, we will prepend a stretch glyph that will
18691 have the end_of_box_run_p flag set for it, so there's no
18692 need for the appended newline glyph to have that flag
18693 set. */
18694 if (it->glyph_row->reversed_p
18695 /* But if the appended newline glyph goes all the way to
18696 the end of the row, there will be no stretch glyph,
18697 so leave the box flag set. */
18698 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18699 it->end_of_box_run_p = 0;
18700
18701 PRODUCE_GLYPHS (it);
18702
18703 it->override_ascent = -1;
18704 it->constrain_row_ascent_descent_p = 0;
18705 it->current_x = saved_x;
18706 it->object = saved_object;
18707 it->position = saved_pos;
18708 it->what = saved_what;
18709 it->face_id = saved_face_id;
18710 it->len = saved_len;
18711 it->c = saved_c;
18712 it->char_to_display = saved_char_to_display;
18713 it->end_of_box_run_p = saved_box_end;
18714 return 1;
18715 }
18716 }
18717
18718 return 0;
18719 }
18720
18721
18722 /* Extend the face of the last glyph in the text area of IT->glyph_row
18723 to the end of the display line. Called from display_line. If the
18724 glyph row is empty, add a space glyph to it so that we know the
18725 face to draw. Set the glyph row flag fill_line_p. If the glyph
18726 row is R2L, prepend a stretch glyph to cover the empty space to the
18727 left of the leftmost glyph. */
18728
18729 static void
18730 extend_face_to_end_of_line (struct it *it)
18731 {
18732 struct face *face, *default_face;
18733 struct frame *f = it->f;
18734
18735 /* If line is already filled, do nothing. Non window-system frames
18736 get a grace of one more ``pixel'' because their characters are
18737 1-``pixel'' wide, so they hit the equality too early. This grace
18738 is needed only for R2L rows that are not continued, to produce
18739 one extra blank where we could display the cursor. */
18740 if (it->current_x >= it->last_visible_x
18741 + (!FRAME_WINDOW_P (f)
18742 && it->glyph_row->reversed_p
18743 && !it->glyph_row->continued_p))
18744 return;
18745
18746 /* The default face, possibly remapped. */
18747 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18748
18749 /* Face extension extends the background and box of IT->face_id
18750 to the end of the line. If the background equals the background
18751 of the frame, we don't have to do anything. */
18752 if (it->face_before_selective_p)
18753 face = FACE_FROM_ID (f, it->saved_face_id);
18754 else
18755 face = FACE_FROM_ID (f, it->face_id);
18756
18757 if (FRAME_WINDOW_P (f)
18758 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18759 && face->box == FACE_NO_BOX
18760 && face->background == FRAME_BACKGROUND_PIXEL (f)
18761 && !face->stipple
18762 && !it->glyph_row->reversed_p)
18763 return;
18764
18765 /* Set the glyph row flag indicating that the face of the last glyph
18766 in the text area has to be drawn to the end of the text area. */
18767 it->glyph_row->fill_line_p = 1;
18768
18769 /* If current character of IT is not ASCII, make sure we have the
18770 ASCII face. This will be automatically undone the next time
18771 get_next_display_element returns a multibyte character. Note
18772 that the character will always be single byte in unibyte
18773 text. */
18774 if (!ASCII_CHAR_P (it->c))
18775 {
18776 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18777 }
18778
18779 if (FRAME_WINDOW_P (f))
18780 {
18781 /* If the row is empty, add a space with the current face of IT,
18782 so that we know which face to draw. */
18783 if (it->glyph_row->used[TEXT_AREA] == 0)
18784 {
18785 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18786 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18787 it->glyph_row->used[TEXT_AREA] = 1;
18788 }
18789 #ifdef HAVE_WINDOW_SYSTEM
18790 if (it->glyph_row->reversed_p)
18791 {
18792 /* Prepend a stretch glyph to the row, such that the
18793 rightmost glyph will be drawn flushed all the way to the
18794 right margin of the window. The stretch glyph that will
18795 occupy the empty space, if any, to the left of the
18796 glyphs. */
18797 struct font *font = face->font ? face->font : FRAME_FONT (f);
18798 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18799 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18800 struct glyph *g;
18801 int row_width, stretch_ascent, stretch_width;
18802 struct text_pos saved_pos;
18803 int saved_face_id, saved_avoid_cursor, saved_box_start;
18804
18805 for (row_width = 0, g = row_start; g < row_end; g++)
18806 row_width += g->pixel_width;
18807 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18808 if (stretch_width > 0)
18809 {
18810 stretch_ascent =
18811 (((it->ascent + it->descent)
18812 * FONT_BASE (font)) / FONT_HEIGHT (font));
18813 saved_pos = it->position;
18814 memset (&it->position, 0, sizeof it->position);
18815 saved_avoid_cursor = it->avoid_cursor_p;
18816 it->avoid_cursor_p = 1;
18817 saved_face_id = it->face_id;
18818 saved_box_start = it->start_of_box_run_p;
18819 /* The last row's stretch glyph should get the default
18820 face, to avoid painting the rest of the window with
18821 the region face, if the region ends at ZV. */
18822 if (it->glyph_row->ends_at_zv_p)
18823 it->face_id = default_face->id;
18824 else
18825 it->face_id = face->id;
18826 it->start_of_box_run_p = 0;
18827 append_stretch_glyph (it, make_number (0), stretch_width,
18828 it->ascent + it->descent, stretch_ascent);
18829 it->position = saved_pos;
18830 it->avoid_cursor_p = saved_avoid_cursor;
18831 it->face_id = saved_face_id;
18832 it->start_of_box_run_p = saved_box_start;
18833 }
18834 }
18835 #endif /* HAVE_WINDOW_SYSTEM */
18836 }
18837 else
18838 {
18839 /* Save some values that must not be changed. */
18840 int saved_x = it->current_x;
18841 struct text_pos saved_pos;
18842 Lisp_Object saved_object;
18843 enum display_element_type saved_what = it->what;
18844 int saved_face_id = it->face_id;
18845
18846 saved_object = it->object;
18847 saved_pos = it->position;
18848
18849 it->what = IT_CHARACTER;
18850 memset (&it->position, 0, sizeof it->position);
18851 it->object = make_number (0);
18852 it->c = it->char_to_display = ' ';
18853 it->len = 1;
18854 /* The last row's blank glyphs should get the default face, to
18855 avoid painting the rest of the window with the region face,
18856 if the region ends at ZV. */
18857 if (it->glyph_row->ends_at_zv_p)
18858 it->face_id = default_face->id;
18859 else
18860 it->face_id = face->id;
18861
18862 PRODUCE_GLYPHS (it);
18863
18864 while (it->current_x <= it->last_visible_x)
18865 PRODUCE_GLYPHS (it);
18866
18867 /* Don't count these blanks really. It would let us insert a left
18868 truncation glyph below and make us set the cursor on them, maybe. */
18869 it->current_x = saved_x;
18870 it->object = saved_object;
18871 it->position = saved_pos;
18872 it->what = saved_what;
18873 it->face_id = saved_face_id;
18874 }
18875 }
18876
18877
18878 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18879 trailing whitespace. */
18880
18881 static int
18882 trailing_whitespace_p (ptrdiff_t charpos)
18883 {
18884 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18885 int c = 0;
18886
18887 while (bytepos < ZV_BYTE
18888 && (c = FETCH_CHAR (bytepos),
18889 c == ' ' || c == '\t'))
18890 ++bytepos;
18891
18892 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18893 {
18894 if (bytepos != PT_BYTE)
18895 return 1;
18896 }
18897 return 0;
18898 }
18899
18900
18901 /* Highlight trailing whitespace, if any, in ROW. */
18902
18903 static void
18904 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18905 {
18906 int used = row->used[TEXT_AREA];
18907
18908 if (used)
18909 {
18910 struct glyph *start = row->glyphs[TEXT_AREA];
18911 struct glyph *glyph = start + used - 1;
18912
18913 if (row->reversed_p)
18914 {
18915 /* Right-to-left rows need to be processed in the opposite
18916 direction, so swap the edge pointers. */
18917 glyph = start;
18918 start = row->glyphs[TEXT_AREA] + used - 1;
18919 }
18920
18921 /* Skip over glyphs inserted to display the cursor at the
18922 end of a line, for extending the face of the last glyph
18923 to the end of the line on terminals, and for truncation
18924 and continuation glyphs. */
18925 if (!row->reversed_p)
18926 {
18927 while (glyph >= start
18928 && glyph->type == CHAR_GLYPH
18929 && INTEGERP (glyph->object))
18930 --glyph;
18931 }
18932 else
18933 {
18934 while (glyph <= start
18935 && glyph->type == CHAR_GLYPH
18936 && INTEGERP (glyph->object))
18937 ++glyph;
18938 }
18939
18940 /* If last glyph is a space or stretch, and it's trailing
18941 whitespace, set the face of all trailing whitespace glyphs in
18942 IT->glyph_row to `trailing-whitespace'. */
18943 if ((row->reversed_p ? glyph <= start : glyph >= start)
18944 && BUFFERP (glyph->object)
18945 && (glyph->type == STRETCH_GLYPH
18946 || (glyph->type == CHAR_GLYPH
18947 && glyph->u.ch == ' '))
18948 && trailing_whitespace_p (glyph->charpos))
18949 {
18950 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18951 if (face_id < 0)
18952 return;
18953
18954 if (!row->reversed_p)
18955 {
18956 while (glyph >= start
18957 && BUFFERP (glyph->object)
18958 && (glyph->type == STRETCH_GLYPH
18959 || (glyph->type == CHAR_GLYPH
18960 && glyph->u.ch == ' ')))
18961 (glyph--)->face_id = face_id;
18962 }
18963 else
18964 {
18965 while (glyph <= start
18966 && BUFFERP (glyph->object)
18967 && (glyph->type == STRETCH_GLYPH
18968 || (glyph->type == CHAR_GLYPH
18969 && glyph->u.ch == ' ')))
18970 (glyph++)->face_id = face_id;
18971 }
18972 }
18973 }
18974 }
18975
18976
18977 /* Value is non-zero if glyph row ROW should be
18978 considered to hold the buffer position CHARPOS. */
18979
18980 static int
18981 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18982 {
18983 int result = 1;
18984
18985 if (charpos == CHARPOS (row->end.pos)
18986 || charpos == MATRIX_ROW_END_CHARPOS (row))
18987 {
18988 /* Suppose the row ends on a string.
18989 Unless the row is continued, that means it ends on a newline
18990 in the string. If it's anything other than a display string
18991 (e.g., a before-string from an overlay), we don't want the
18992 cursor there. (This heuristic seems to give the optimal
18993 behavior for the various types of multi-line strings.)
18994 One exception: if the string has `cursor' property on one of
18995 its characters, we _do_ want the cursor there. */
18996 if (CHARPOS (row->end.string_pos) >= 0)
18997 {
18998 if (row->continued_p)
18999 result = 1;
19000 else
19001 {
19002 /* Check for `display' property. */
19003 struct glyph *beg = row->glyphs[TEXT_AREA];
19004 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19005 struct glyph *glyph;
19006
19007 result = 0;
19008 for (glyph = end; glyph >= beg; --glyph)
19009 if (STRINGP (glyph->object))
19010 {
19011 Lisp_Object prop
19012 = Fget_char_property (make_number (charpos),
19013 Qdisplay, Qnil);
19014 result =
19015 (!NILP (prop)
19016 && display_prop_string_p (prop, glyph->object));
19017 /* If there's a `cursor' property on one of the
19018 string's characters, this row is a cursor row,
19019 even though this is not a display string. */
19020 if (!result)
19021 {
19022 Lisp_Object s = glyph->object;
19023
19024 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19025 {
19026 ptrdiff_t gpos = glyph->charpos;
19027
19028 if (!NILP (Fget_char_property (make_number (gpos),
19029 Qcursor, s)))
19030 {
19031 result = 1;
19032 break;
19033 }
19034 }
19035 }
19036 break;
19037 }
19038 }
19039 }
19040 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19041 {
19042 /* If the row ends in middle of a real character,
19043 and the line is continued, we want the cursor here.
19044 That's because CHARPOS (ROW->end.pos) would equal
19045 PT if PT is before the character. */
19046 if (!row->ends_in_ellipsis_p)
19047 result = row->continued_p;
19048 else
19049 /* If the row ends in an ellipsis, then
19050 CHARPOS (ROW->end.pos) will equal point after the
19051 invisible text. We want that position to be displayed
19052 after the ellipsis. */
19053 result = 0;
19054 }
19055 /* If the row ends at ZV, display the cursor at the end of that
19056 row instead of at the start of the row below. */
19057 else if (row->ends_at_zv_p)
19058 result = 1;
19059 else
19060 result = 0;
19061 }
19062
19063 return result;
19064 }
19065
19066 /* Value is non-zero if glyph row ROW should be
19067 used to hold the cursor. */
19068
19069 static int
19070 cursor_row_p (struct glyph_row *row)
19071 {
19072 return row_for_charpos_p (row, PT);
19073 }
19074
19075 \f
19076
19077 /* Push the property PROP so that it will be rendered at the current
19078 position in IT. Return 1 if PROP was successfully pushed, 0
19079 otherwise. Called from handle_line_prefix to handle the
19080 `line-prefix' and `wrap-prefix' properties. */
19081
19082 static int
19083 push_prefix_prop (struct it *it, Lisp_Object prop)
19084 {
19085 struct text_pos pos =
19086 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19087
19088 eassert (it->method == GET_FROM_BUFFER
19089 || it->method == GET_FROM_DISPLAY_VECTOR
19090 || it->method == GET_FROM_STRING);
19091
19092 /* We need to save the current buffer/string position, so it will be
19093 restored by pop_it, because iterate_out_of_display_property
19094 depends on that being set correctly, but some situations leave
19095 it->position not yet set when this function is called. */
19096 push_it (it, &pos);
19097
19098 if (STRINGP (prop))
19099 {
19100 if (SCHARS (prop) == 0)
19101 {
19102 pop_it (it);
19103 return 0;
19104 }
19105
19106 it->string = prop;
19107 it->string_from_prefix_prop_p = 1;
19108 it->multibyte_p = STRING_MULTIBYTE (it->string);
19109 it->current.overlay_string_index = -1;
19110 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19111 it->end_charpos = it->string_nchars = SCHARS (it->string);
19112 it->method = GET_FROM_STRING;
19113 it->stop_charpos = 0;
19114 it->prev_stop = 0;
19115 it->base_level_stop = 0;
19116
19117 /* Force paragraph direction to be that of the parent
19118 buffer/string. */
19119 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19120 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19121 else
19122 it->paragraph_embedding = L2R;
19123
19124 /* Set up the bidi iterator for this display string. */
19125 if (it->bidi_p)
19126 {
19127 it->bidi_it.string.lstring = it->string;
19128 it->bidi_it.string.s = NULL;
19129 it->bidi_it.string.schars = it->end_charpos;
19130 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19131 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19132 it->bidi_it.string.unibyte = !it->multibyte_p;
19133 it->bidi_it.w = it->w;
19134 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19135 }
19136 }
19137 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19138 {
19139 it->method = GET_FROM_STRETCH;
19140 it->object = prop;
19141 }
19142 #ifdef HAVE_WINDOW_SYSTEM
19143 else if (IMAGEP (prop))
19144 {
19145 it->what = IT_IMAGE;
19146 it->image_id = lookup_image (it->f, prop);
19147 it->method = GET_FROM_IMAGE;
19148 }
19149 #endif /* HAVE_WINDOW_SYSTEM */
19150 else
19151 {
19152 pop_it (it); /* bogus display property, give up */
19153 return 0;
19154 }
19155
19156 return 1;
19157 }
19158
19159 /* Return the character-property PROP at the current position in IT. */
19160
19161 static Lisp_Object
19162 get_it_property (struct it *it, Lisp_Object prop)
19163 {
19164 Lisp_Object position, object = it->object;
19165
19166 if (STRINGP (object))
19167 position = make_number (IT_STRING_CHARPOS (*it));
19168 else if (BUFFERP (object))
19169 {
19170 position = make_number (IT_CHARPOS (*it));
19171 object = it->window;
19172 }
19173 else
19174 return Qnil;
19175
19176 return Fget_char_property (position, prop, object);
19177 }
19178
19179 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19180
19181 static void
19182 handle_line_prefix (struct it *it)
19183 {
19184 Lisp_Object prefix;
19185
19186 if (it->continuation_lines_width > 0)
19187 {
19188 prefix = get_it_property (it, Qwrap_prefix);
19189 if (NILP (prefix))
19190 prefix = Vwrap_prefix;
19191 }
19192 else
19193 {
19194 prefix = get_it_property (it, Qline_prefix);
19195 if (NILP (prefix))
19196 prefix = Vline_prefix;
19197 }
19198 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19199 {
19200 /* If the prefix is wider than the window, and we try to wrap
19201 it, it would acquire its own wrap prefix, and so on till the
19202 iterator stack overflows. So, don't wrap the prefix. */
19203 it->line_wrap = TRUNCATE;
19204 it->avoid_cursor_p = 1;
19205 }
19206 }
19207
19208 \f
19209
19210 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19211 only for R2L lines from display_line and display_string, when they
19212 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19213 the line/string needs to be continued on the next glyph row. */
19214 static void
19215 unproduce_glyphs (struct it *it, int n)
19216 {
19217 struct glyph *glyph, *end;
19218
19219 eassert (it->glyph_row);
19220 eassert (it->glyph_row->reversed_p);
19221 eassert (it->area == TEXT_AREA);
19222 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19223
19224 if (n > it->glyph_row->used[TEXT_AREA])
19225 n = it->glyph_row->used[TEXT_AREA];
19226 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19227 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19228 for ( ; glyph < end; glyph++)
19229 glyph[-n] = *glyph;
19230 }
19231
19232 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19233 and ROW->maxpos. */
19234 static void
19235 find_row_edges (struct it *it, struct glyph_row *row,
19236 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19237 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19238 {
19239 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19240 lines' rows is implemented for bidi-reordered rows. */
19241
19242 /* ROW->minpos is the value of min_pos, the minimal buffer position
19243 we have in ROW, or ROW->start.pos if that is smaller. */
19244 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19245 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19246 else
19247 /* We didn't find buffer positions smaller than ROW->start, or
19248 didn't find _any_ valid buffer positions in any of the glyphs,
19249 so we must trust the iterator's computed positions. */
19250 row->minpos = row->start.pos;
19251 if (max_pos <= 0)
19252 {
19253 max_pos = CHARPOS (it->current.pos);
19254 max_bpos = BYTEPOS (it->current.pos);
19255 }
19256
19257 /* Here are the various use-cases for ending the row, and the
19258 corresponding values for ROW->maxpos:
19259
19260 Line ends in a newline from buffer eol_pos + 1
19261 Line is continued from buffer max_pos + 1
19262 Line is truncated on right it->current.pos
19263 Line ends in a newline from string max_pos + 1(*)
19264 (*) + 1 only when line ends in a forward scan
19265 Line is continued from string max_pos
19266 Line is continued from display vector max_pos
19267 Line is entirely from a string min_pos == max_pos
19268 Line is entirely from a display vector min_pos == max_pos
19269 Line that ends at ZV ZV
19270
19271 If you discover other use-cases, please add them here as
19272 appropriate. */
19273 if (row->ends_at_zv_p)
19274 row->maxpos = it->current.pos;
19275 else if (row->used[TEXT_AREA])
19276 {
19277 int seen_this_string = 0;
19278 struct glyph_row *r1 = row - 1;
19279
19280 /* Did we see the same display string on the previous row? */
19281 if (STRINGP (it->object)
19282 /* this is not the first row */
19283 && row > it->w->desired_matrix->rows
19284 /* previous row is not the header line */
19285 && !r1->mode_line_p
19286 /* previous row also ends in a newline from a string */
19287 && r1->ends_in_newline_from_string_p)
19288 {
19289 struct glyph *start, *end;
19290
19291 /* Search for the last glyph of the previous row that came
19292 from buffer or string. Depending on whether the row is
19293 L2R or R2L, we need to process it front to back or the
19294 other way round. */
19295 if (!r1->reversed_p)
19296 {
19297 start = r1->glyphs[TEXT_AREA];
19298 end = start + r1->used[TEXT_AREA];
19299 /* Glyphs inserted by redisplay have an integer (zero)
19300 as their object. */
19301 while (end > start
19302 && INTEGERP ((end - 1)->object)
19303 && (end - 1)->charpos <= 0)
19304 --end;
19305 if (end > start)
19306 {
19307 if (EQ ((end - 1)->object, it->object))
19308 seen_this_string = 1;
19309 }
19310 else
19311 /* If all the glyphs of the previous row were inserted
19312 by redisplay, it means the previous row was
19313 produced from a single newline, which is only
19314 possible if that newline came from the same string
19315 as the one which produced this ROW. */
19316 seen_this_string = 1;
19317 }
19318 else
19319 {
19320 end = r1->glyphs[TEXT_AREA] - 1;
19321 start = end + r1->used[TEXT_AREA];
19322 while (end < start
19323 && INTEGERP ((end + 1)->object)
19324 && (end + 1)->charpos <= 0)
19325 ++end;
19326 if (end < start)
19327 {
19328 if (EQ ((end + 1)->object, it->object))
19329 seen_this_string = 1;
19330 }
19331 else
19332 seen_this_string = 1;
19333 }
19334 }
19335 /* Take note of each display string that covers a newline only
19336 once, the first time we see it. This is for when a display
19337 string includes more than one newline in it. */
19338 if (row->ends_in_newline_from_string_p && !seen_this_string)
19339 {
19340 /* If we were scanning the buffer forward when we displayed
19341 the string, we want to account for at least one buffer
19342 position that belongs to this row (position covered by
19343 the display string), so that cursor positioning will
19344 consider this row as a candidate when point is at the end
19345 of the visual line represented by this row. This is not
19346 required when scanning back, because max_pos will already
19347 have a much larger value. */
19348 if (CHARPOS (row->end.pos) > max_pos)
19349 INC_BOTH (max_pos, max_bpos);
19350 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19351 }
19352 else if (CHARPOS (it->eol_pos) > 0)
19353 SET_TEXT_POS (row->maxpos,
19354 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19355 else if (row->continued_p)
19356 {
19357 /* If max_pos is different from IT's current position, it
19358 means IT->method does not belong to the display element
19359 at max_pos. However, it also means that the display
19360 element at max_pos was displayed in its entirety on this
19361 line, which is equivalent to saying that the next line
19362 starts at the next buffer position. */
19363 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19364 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19365 else
19366 {
19367 INC_BOTH (max_pos, max_bpos);
19368 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19369 }
19370 }
19371 else if (row->truncated_on_right_p)
19372 /* display_line already called reseat_at_next_visible_line_start,
19373 which puts the iterator at the beginning of the next line, in
19374 the logical order. */
19375 row->maxpos = it->current.pos;
19376 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19377 /* A line that is entirely from a string/image/stretch... */
19378 row->maxpos = row->minpos;
19379 else
19380 emacs_abort ();
19381 }
19382 else
19383 row->maxpos = it->current.pos;
19384 }
19385
19386 /* Construct the glyph row IT->glyph_row in the desired matrix of
19387 IT->w from text at the current position of IT. See dispextern.h
19388 for an overview of struct it. Value is non-zero if
19389 IT->glyph_row displays text, as opposed to a line displaying ZV
19390 only. */
19391
19392 static int
19393 display_line (struct it *it)
19394 {
19395 struct glyph_row *row = it->glyph_row;
19396 Lisp_Object overlay_arrow_string;
19397 struct it wrap_it;
19398 void *wrap_data = NULL;
19399 int may_wrap = 0, wrap_x IF_LINT (= 0);
19400 int wrap_row_used = -1;
19401 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19402 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19403 int wrap_row_extra_line_spacing IF_LINT (= 0);
19404 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19405 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19406 int cvpos;
19407 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19408 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19409
19410 /* We always start displaying at hpos zero even if hscrolled. */
19411 eassert (it->hpos == 0 && it->current_x == 0);
19412
19413 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19414 >= it->w->desired_matrix->nrows)
19415 {
19416 it->w->nrows_scale_factor++;
19417 fonts_changed_p = 1;
19418 return 0;
19419 }
19420
19421 /* Is IT->w showing the region? */
19422 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19423
19424 /* Clear the result glyph row and enable it. */
19425 prepare_desired_row (row);
19426
19427 row->y = it->current_y;
19428 row->start = it->start;
19429 row->continuation_lines_width = it->continuation_lines_width;
19430 row->displays_text_p = 1;
19431 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19432 it->starts_in_middle_of_char_p = 0;
19433
19434 /* Arrange the overlays nicely for our purposes. Usually, we call
19435 display_line on only one line at a time, in which case this
19436 can't really hurt too much, or we call it on lines which appear
19437 one after another in the buffer, in which case all calls to
19438 recenter_overlay_lists but the first will be pretty cheap. */
19439 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19440
19441 /* Move over display elements that are not visible because we are
19442 hscrolled. This may stop at an x-position < IT->first_visible_x
19443 if the first glyph is partially visible or if we hit a line end. */
19444 if (it->current_x < it->first_visible_x)
19445 {
19446 enum move_it_result move_result;
19447
19448 this_line_min_pos = row->start.pos;
19449 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19450 MOVE_TO_POS | MOVE_TO_X);
19451 /* If we are under a large hscroll, move_it_in_display_line_to
19452 could hit the end of the line without reaching
19453 it->first_visible_x. Pretend that we did reach it. This is
19454 especially important on a TTY, where we will call
19455 extend_face_to_end_of_line, which needs to know how many
19456 blank glyphs to produce. */
19457 if (it->current_x < it->first_visible_x
19458 && (move_result == MOVE_NEWLINE_OR_CR
19459 || move_result == MOVE_POS_MATCH_OR_ZV))
19460 it->current_x = it->first_visible_x;
19461
19462 /* Record the smallest positions seen while we moved over
19463 display elements that are not visible. This is needed by
19464 redisplay_internal for optimizing the case where the cursor
19465 stays inside the same line. The rest of this function only
19466 considers positions that are actually displayed, so
19467 RECORD_MAX_MIN_POS will not otherwise record positions that
19468 are hscrolled to the left of the left edge of the window. */
19469 min_pos = CHARPOS (this_line_min_pos);
19470 min_bpos = BYTEPOS (this_line_min_pos);
19471 }
19472 else
19473 {
19474 /* We only do this when not calling `move_it_in_display_line_to'
19475 above, because move_it_in_display_line_to calls
19476 handle_line_prefix itself. */
19477 handle_line_prefix (it);
19478 }
19479
19480 /* Get the initial row height. This is either the height of the
19481 text hscrolled, if there is any, or zero. */
19482 row->ascent = it->max_ascent;
19483 row->height = it->max_ascent + it->max_descent;
19484 row->phys_ascent = it->max_phys_ascent;
19485 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19486 row->extra_line_spacing = it->max_extra_line_spacing;
19487
19488 /* Utility macro to record max and min buffer positions seen until now. */
19489 #define RECORD_MAX_MIN_POS(IT) \
19490 do \
19491 { \
19492 int composition_p = !STRINGP ((IT)->string) \
19493 && ((IT)->what == IT_COMPOSITION); \
19494 ptrdiff_t current_pos = \
19495 composition_p ? (IT)->cmp_it.charpos \
19496 : IT_CHARPOS (*(IT)); \
19497 ptrdiff_t current_bpos = \
19498 composition_p ? CHAR_TO_BYTE (current_pos) \
19499 : IT_BYTEPOS (*(IT)); \
19500 if (current_pos < min_pos) \
19501 { \
19502 min_pos = current_pos; \
19503 min_bpos = current_bpos; \
19504 } \
19505 if (IT_CHARPOS (*it) > max_pos) \
19506 { \
19507 max_pos = IT_CHARPOS (*it); \
19508 max_bpos = IT_BYTEPOS (*it); \
19509 } \
19510 } \
19511 while (0)
19512
19513 /* Loop generating characters. The loop is left with IT on the next
19514 character to display. */
19515 while (1)
19516 {
19517 int n_glyphs_before, hpos_before, x_before;
19518 int x, nglyphs;
19519 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19520
19521 /* Retrieve the next thing to display. Value is zero if end of
19522 buffer reached. */
19523 if (!get_next_display_element (it))
19524 {
19525 /* Maybe add a space at the end of this line that is used to
19526 display the cursor there under X. Set the charpos of the
19527 first glyph of blank lines not corresponding to any text
19528 to -1. */
19529 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19530 row->exact_window_width_line_p = 1;
19531 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19532 || row->used[TEXT_AREA] == 0)
19533 {
19534 row->glyphs[TEXT_AREA]->charpos = -1;
19535 row->displays_text_p = 0;
19536
19537 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19538 && (!MINI_WINDOW_P (it->w)
19539 || (minibuf_level && EQ (it->window, minibuf_window))))
19540 row->indicate_empty_line_p = 1;
19541 }
19542
19543 it->continuation_lines_width = 0;
19544 row->ends_at_zv_p = 1;
19545 /* A row that displays right-to-left text must always have
19546 its last face extended all the way to the end of line,
19547 even if this row ends in ZV, because we still write to
19548 the screen left to right. We also need to extend the
19549 last face if the default face is remapped to some
19550 different face, otherwise the functions that clear
19551 portions of the screen will clear with the default face's
19552 background color. */
19553 if (row->reversed_p
19554 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19555 extend_face_to_end_of_line (it);
19556 break;
19557 }
19558
19559 /* Now, get the metrics of what we want to display. This also
19560 generates glyphs in `row' (which is IT->glyph_row). */
19561 n_glyphs_before = row->used[TEXT_AREA];
19562 x = it->current_x;
19563
19564 /* Remember the line height so far in case the next element doesn't
19565 fit on the line. */
19566 if (it->line_wrap != TRUNCATE)
19567 {
19568 ascent = it->max_ascent;
19569 descent = it->max_descent;
19570 phys_ascent = it->max_phys_ascent;
19571 phys_descent = it->max_phys_descent;
19572
19573 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19574 {
19575 if (IT_DISPLAYING_WHITESPACE (it))
19576 may_wrap = 1;
19577 else if (may_wrap)
19578 {
19579 SAVE_IT (wrap_it, *it, wrap_data);
19580 wrap_x = x;
19581 wrap_row_used = row->used[TEXT_AREA];
19582 wrap_row_ascent = row->ascent;
19583 wrap_row_height = row->height;
19584 wrap_row_phys_ascent = row->phys_ascent;
19585 wrap_row_phys_height = row->phys_height;
19586 wrap_row_extra_line_spacing = row->extra_line_spacing;
19587 wrap_row_min_pos = min_pos;
19588 wrap_row_min_bpos = min_bpos;
19589 wrap_row_max_pos = max_pos;
19590 wrap_row_max_bpos = max_bpos;
19591 may_wrap = 0;
19592 }
19593 }
19594 }
19595
19596 PRODUCE_GLYPHS (it);
19597
19598 /* If this display element was in marginal areas, continue with
19599 the next one. */
19600 if (it->area != TEXT_AREA)
19601 {
19602 row->ascent = max (row->ascent, it->max_ascent);
19603 row->height = max (row->height, it->max_ascent + it->max_descent);
19604 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19605 row->phys_height = max (row->phys_height,
19606 it->max_phys_ascent + it->max_phys_descent);
19607 row->extra_line_spacing = max (row->extra_line_spacing,
19608 it->max_extra_line_spacing);
19609 set_iterator_to_next (it, 1);
19610 continue;
19611 }
19612
19613 /* Does the display element fit on the line? If we truncate
19614 lines, we should draw past the right edge of the window. If
19615 we don't truncate, we want to stop so that we can display the
19616 continuation glyph before the right margin. If lines are
19617 continued, there are two possible strategies for characters
19618 resulting in more than 1 glyph (e.g. tabs): Display as many
19619 glyphs as possible in this line and leave the rest for the
19620 continuation line, or display the whole element in the next
19621 line. Original redisplay did the former, so we do it also. */
19622 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19623 hpos_before = it->hpos;
19624 x_before = x;
19625
19626 if (/* Not a newline. */
19627 nglyphs > 0
19628 /* Glyphs produced fit entirely in the line. */
19629 && it->current_x < it->last_visible_x)
19630 {
19631 it->hpos += nglyphs;
19632 row->ascent = max (row->ascent, it->max_ascent);
19633 row->height = max (row->height, it->max_ascent + it->max_descent);
19634 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19635 row->phys_height = max (row->phys_height,
19636 it->max_phys_ascent + it->max_phys_descent);
19637 row->extra_line_spacing = max (row->extra_line_spacing,
19638 it->max_extra_line_spacing);
19639 if (it->current_x - it->pixel_width < it->first_visible_x)
19640 row->x = x - it->first_visible_x;
19641 /* Record the maximum and minimum buffer positions seen so
19642 far in glyphs that will be displayed by this row. */
19643 if (it->bidi_p)
19644 RECORD_MAX_MIN_POS (it);
19645 }
19646 else
19647 {
19648 int i, new_x;
19649 struct glyph *glyph;
19650
19651 for (i = 0; i < nglyphs; ++i, x = new_x)
19652 {
19653 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19654 new_x = x + glyph->pixel_width;
19655
19656 if (/* Lines are continued. */
19657 it->line_wrap != TRUNCATE
19658 && (/* Glyph doesn't fit on the line. */
19659 new_x > it->last_visible_x
19660 /* Or it fits exactly on a window system frame. */
19661 || (new_x == it->last_visible_x
19662 && FRAME_WINDOW_P (it->f)
19663 && (row->reversed_p
19664 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19665 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19666 {
19667 /* End of a continued line. */
19668
19669 if (it->hpos == 0
19670 || (new_x == it->last_visible_x
19671 && FRAME_WINDOW_P (it->f)
19672 && (row->reversed_p
19673 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19674 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19675 {
19676 /* Current glyph is the only one on the line or
19677 fits exactly on the line. We must continue
19678 the line because we can't draw the cursor
19679 after the glyph. */
19680 row->continued_p = 1;
19681 it->current_x = new_x;
19682 it->continuation_lines_width += new_x;
19683 ++it->hpos;
19684 if (i == nglyphs - 1)
19685 {
19686 /* If line-wrap is on, check if a previous
19687 wrap point was found. */
19688 if (wrap_row_used > 0
19689 /* Even if there is a previous wrap
19690 point, continue the line here as
19691 usual, if (i) the previous character
19692 was a space or tab AND (ii) the
19693 current character is not. */
19694 && (!may_wrap
19695 || IT_DISPLAYING_WHITESPACE (it)))
19696 goto back_to_wrap;
19697
19698 /* Record the maximum and minimum buffer
19699 positions seen so far in glyphs that will be
19700 displayed by this row. */
19701 if (it->bidi_p)
19702 RECORD_MAX_MIN_POS (it);
19703 set_iterator_to_next (it, 1);
19704 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19705 {
19706 if (!get_next_display_element (it))
19707 {
19708 row->exact_window_width_line_p = 1;
19709 it->continuation_lines_width = 0;
19710 row->continued_p = 0;
19711 row->ends_at_zv_p = 1;
19712 }
19713 else if (ITERATOR_AT_END_OF_LINE_P (it))
19714 {
19715 row->continued_p = 0;
19716 row->exact_window_width_line_p = 1;
19717 }
19718 }
19719 }
19720 else if (it->bidi_p)
19721 RECORD_MAX_MIN_POS (it);
19722 }
19723 else if (CHAR_GLYPH_PADDING_P (*glyph)
19724 && !FRAME_WINDOW_P (it->f))
19725 {
19726 /* A padding glyph that doesn't fit on this line.
19727 This means the whole character doesn't fit
19728 on the line. */
19729 if (row->reversed_p)
19730 unproduce_glyphs (it, row->used[TEXT_AREA]
19731 - n_glyphs_before);
19732 row->used[TEXT_AREA] = n_glyphs_before;
19733
19734 /* Fill the rest of the row with continuation
19735 glyphs like in 20.x. */
19736 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19737 < row->glyphs[1 + TEXT_AREA])
19738 produce_special_glyphs (it, IT_CONTINUATION);
19739
19740 row->continued_p = 1;
19741 it->current_x = x_before;
19742 it->continuation_lines_width += x_before;
19743
19744 /* Restore the height to what it was before the
19745 element not fitting on the line. */
19746 it->max_ascent = ascent;
19747 it->max_descent = descent;
19748 it->max_phys_ascent = phys_ascent;
19749 it->max_phys_descent = phys_descent;
19750 }
19751 else if (wrap_row_used > 0)
19752 {
19753 back_to_wrap:
19754 if (row->reversed_p)
19755 unproduce_glyphs (it,
19756 row->used[TEXT_AREA] - wrap_row_used);
19757 RESTORE_IT (it, &wrap_it, wrap_data);
19758 it->continuation_lines_width += wrap_x;
19759 row->used[TEXT_AREA] = wrap_row_used;
19760 row->ascent = wrap_row_ascent;
19761 row->height = wrap_row_height;
19762 row->phys_ascent = wrap_row_phys_ascent;
19763 row->phys_height = wrap_row_phys_height;
19764 row->extra_line_spacing = wrap_row_extra_line_spacing;
19765 min_pos = wrap_row_min_pos;
19766 min_bpos = wrap_row_min_bpos;
19767 max_pos = wrap_row_max_pos;
19768 max_bpos = wrap_row_max_bpos;
19769 row->continued_p = 1;
19770 row->ends_at_zv_p = 0;
19771 row->exact_window_width_line_p = 0;
19772 it->continuation_lines_width += x;
19773
19774 /* Make sure that a non-default face is extended
19775 up to the right margin of the window. */
19776 extend_face_to_end_of_line (it);
19777 }
19778 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19779 {
19780 /* A TAB that extends past the right edge of the
19781 window. This produces a single glyph on
19782 window system frames. We leave the glyph in
19783 this row and let it fill the row, but don't
19784 consume the TAB. */
19785 if ((row->reversed_p
19786 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19787 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19788 produce_special_glyphs (it, IT_CONTINUATION);
19789 it->continuation_lines_width += it->last_visible_x;
19790 row->ends_in_middle_of_char_p = 1;
19791 row->continued_p = 1;
19792 glyph->pixel_width = it->last_visible_x - x;
19793 it->starts_in_middle_of_char_p = 1;
19794 }
19795 else
19796 {
19797 /* Something other than a TAB that draws past
19798 the right edge of the window. Restore
19799 positions to values before the element. */
19800 if (row->reversed_p)
19801 unproduce_glyphs (it, row->used[TEXT_AREA]
19802 - (n_glyphs_before + i));
19803 row->used[TEXT_AREA] = n_glyphs_before + i;
19804
19805 /* Display continuation glyphs. */
19806 it->current_x = x_before;
19807 it->continuation_lines_width += x;
19808 if (!FRAME_WINDOW_P (it->f)
19809 || (row->reversed_p
19810 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19811 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19812 produce_special_glyphs (it, IT_CONTINUATION);
19813 row->continued_p = 1;
19814
19815 extend_face_to_end_of_line (it);
19816
19817 if (nglyphs > 1 && i > 0)
19818 {
19819 row->ends_in_middle_of_char_p = 1;
19820 it->starts_in_middle_of_char_p = 1;
19821 }
19822
19823 /* Restore the height to what it was before the
19824 element not fitting on the line. */
19825 it->max_ascent = ascent;
19826 it->max_descent = descent;
19827 it->max_phys_ascent = phys_ascent;
19828 it->max_phys_descent = phys_descent;
19829 }
19830
19831 break;
19832 }
19833 else if (new_x > it->first_visible_x)
19834 {
19835 /* Increment number of glyphs actually displayed. */
19836 ++it->hpos;
19837
19838 /* Record the maximum and minimum buffer positions
19839 seen so far in glyphs that will be displayed by
19840 this row. */
19841 if (it->bidi_p)
19842 RECORD_MAX_MIN_POS (it);
19843
19844 if (x < it->first_visible_x)
19845 /* Glyph is partially visible, i.e. row starts at
19846 negative X position. */
19847 row->x = x - it->first_visible_x;
19848 }
19849 else
19850 {
19851 /* Glyph is completely off the left margin of the
19852 window. This should not happen because of the
19853 move_it_in_display_line at the start of this
19854 function, unless the text display area of the
19855 window is empty. */
19856 eassert (it->first_visible_x <= it->last_visible_x);
19857 }
19858 }
19859 /* Even if this display element produced no glyphs at all,
19860 we want to record its position. */
19861 if (it->bidi_p && nglyphs == 0)
19862 RECORD_MAX_MIN_POS (it);
19863
19864 row->ascent = max (row->ascent, it->max_ascent);
19865 row->height = max (row->height, it->max_ascent + it->max_descent);
19866 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19867 row->phys_height = max (row->phys_height,
19868 it->max_phys_ascent + it->max_phys_descent);
19869 row->extra_line_spacing = max (row->extra_line_spacing,
19870 it->max_extra_line_spacing);
19871
19872 /* End of this display line if row is continued. */
19873 if (row->continued_p || row->ends_at_zv_p)
19874 break;
19875 }
19876
19877 at_end_of_line:
19878 /* Is this a line end? If yes, we're also done, after making
19879 sure that a non-default face is extended up to the right
19880 margin of the window. */
19881 if (ITERATOR_AT_END_OF_LINE_P (it))
19882 {
19883 int used_before = row->used[TEXT_AREA];
19884
19885 row->ends_in_newline_from_string_p = STRINGP (it->object);
19886
19887 /* Add a space at the end of the line that is used to
19888 display the cursor there. */
19889 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19890 append_space_for_newline (it, 0);
19891
19892 /* Extend the face to the end of the line. */
19893 extend_face_to_end_of_line (it);
19894
19895 /* Make sure we have the position. */
19896 if (used_before == 0)
19897 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19898
19899 /* Record the position of the newline, for use in
19900 find_row_edges. */
19901 it->eol_pos = it->current.pos;
19902
19903 /* Consume the line end. This skips over invisible lines. */
19904 set_iterator_to_next (it, 1);
19905 it->continuation_lines_width = 0;
19906 break;
19907 }
19908
19909 /* Proceed with next display element. Note that this skips
19910 over lines invisible because of selective display. */
19911 set_iterator_to_next (it, 1);
19912
19913 /* If we truncate lines, we are done when the last displayed
19914 glyphs reach past the right margin of the window. */
19915 if (it->line_wrap == TRUNCATE
19916 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19917 ? (it->current_x >= it->last_visible_x)
19918 : (it->current_x > it->last_visible_x)))
19919 {
19920 /* Maybe add truncation glyphs. */
19921 if (!FRAME_WINDOW_P (it->f)
19922 || (row->reversed_p
19923 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19924 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19925 {
19926 int i, n;
19927
19928 if (!row->reversed_p)
19929 {
19930 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19931 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19932 break;
19933 }
19934 else
19935 {
19936 for (i = 0; i < row->used[TEXT_AREA]; i++)
19937 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19938 break;
19939 /* Remove any padding glyphs at the front of ROW, to
19940 make room for the truncation glyphs we will be
19941 adding below. The loop below always inserts at
19942 least one truncation glyph, so also remove the
19943 last glyph added to ROW. */
19944 unproduce_glyphs (it, i + 1);
19945 /* Adjust i for the loop below. */
19946 i = row->used[TEXT_AREA] - (i + 1);
19947 }
19948
19949 it->current_x = x_before;
19950 if (!FRAME_WINDOW_P (it->f))
19951 {
19952 for (n = row->used[TEXT_AREA]; i < n; ++i)
19953 {
19954 row->used[TEXT_AREA] = i;
19955 produce_special_glyphs (it, IT_TRUNCATION);
19956 }
19957 }
19958 else
19959 {
19960 row->used[TEXT_AREA] = i;
19961 produce_special_glyphs (it, IT_TRUNCATION);
19962 }
19963 }
19964 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19965 {
19966 /* Don't truncate if we can overflow newline into fringe. */
19967 if (!get_next_display_element (it))
19968 {
19969 it->continuation_lines_width = 0;
19970 row->ends_at_zv_p = 1;
19971 row->exact_window_width_line_p = 1;
19972 break;
19973 }
19974 if (ITERATOR_AT_END_OF_LINE_P (it))
19975 {
19976 row->exact_window_width_line_p = 1;
19977 goto at_end_of_line;
19978 }
19979 it->current_x = x_before;
19980 }
19981
19982 row->truncated_on_right_p = 1;
19983 it->continuation_lines_width = 0;
19984 reseat_at_next_visible_line_start (it, 0);
19985 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19986 it->hpos = hpos_before;
19987 break;
19988 }
19989 }
19990
19991 if (wrap_data)
19992 bidi_unshelve_cache (wrap_data, 1);
19993
19994 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19995 at the left window margin. */
19996 if (it->first_visible_x
19997 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19998 {
19999 if (!FRAME_WINDOW_P (it->f)
20000 || (row->reversed_p
20001 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20002 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20003 insert_left_trunc_glyphs (it);
20004 row->truncated_on_left_p = 1;
20005 }
20006
20007 /* Remember the position at which this line ends.
20008
20009 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20010 cannot be before the call to find_row_edges below, since that is
20011 where these positions are determined. */
20012 row->end = it->current;
20013 if (!it->bidi_p)
20014 {
20015 row->minpos = row->start.pos;
20016 row->maxpos = row->end.pos;
20017 }
20018 else
20019 {
20020 /* ROW->minpos and ROW->maxpos must be the smallest and
20021 `1 + the largest' buffer positions in ROW. But if ROW was
20022 bidi-reordered, these two positions can be anywhere in the
20023 row, so we must determine them now. */
20024 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20025 }
20026
20027 /* If the start of this line is the overlay arrow-position, then
20028 mark this glyph row as the one containing the overlay arrow.
20029 This is clearly a mess with variable size fonts. It would be
20030 better to let it be displayed like cursors under X. */
20031 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20032 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20033 !NILP (overlay_arrow_string)))
20034 {
20035 /* Overlay arrow in window redisplay is a fringe bitmap. */
20036 if (STRINGP (overlay_arrow_string))
20037 {
20038 struct glyph_row *arrow_row
20039 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20040 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20041 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20042 struct glyph *p = row->glyphs[TEXT_AREA];
20043 struct glyph *p2, *end;
20044
20045 /* Copy the arrow glyphs. */
20046 while (glyph < arrow_end)
20047 *p++ = *glyph++;
20048
20049 /* Throw away padding glyphs. */
20050 p2 = p;
20051 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20052 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20053 ++p2;
20054 if (p2 > p)
20055 {
20056 while (p2 < end)
20057 *p++ = *p2++;
20058 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20059 }
20060 }
20061 else
20062 {
20063 eassert (INTEGERP (overlay_arrow_string));
20064 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20065 }
20066 overlay_arrow_seen = 1;
20067 }
20068
20069 /* Highlight trailing whitespace. */
20070 if (!NILP (Vshow_trailing_whitespace))
20071 highlight_trailing_whitespace (it->f, it->glyph_row);
20072
20073 /* Compute pixel dimensions of this line. */
20074 compute_line_metrics (it);
20075
20076 /* Implementation note: No changes in the glyphs of ROW or in their
20077 faces can be done past this point, because compute_line_metrics
20078 computes ROW's hash value and stores it within the glyph_row
20079 structure. */
20080
20081 /* Record whether this row ends inside an ellipsis. */
20082 row->ends_in_ellipsis_p
20083 = (it->method == GET_FROM_DISPLAY_VECTOR
20084 && it->ellipsis_p);
20085
20086 /* Save fringe bitmaps in this row. */
20087 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20088 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20089 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20090 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20091
20092 it->left_user_fringe_bitmap = 0;
20093 it->left_user_fringe_face_id = 0;
20094 it->right_user_fringe_bitmap = 0;
20095 it->right_user_fringe_face_id = 0;
20096
20097 /* Maybe set the cursor. */
20098 cvpos = it->w->cursor.vpos;
20099 if ((cvpos < 0
20100 /* In bidi-reordered rows, keep checking for proper cursor
20101 position even if one has been found already, because buffer
20102 positions in such rows change non-linearly with ROW->VPOS,
20103 when a line is continued. One exception: when we are at ZV,
20104 display cursor on the first suitable glyph row, since all
20105 the empty rows after that also have their position set to ZV. */
20106 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20107 lines' rows is implemented for bidi-reordered rows. */
20108 || (it->bidi_p
20109 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20110 && PT >= MATRIX_ROW_START_CHARPOS (row)
20111 && PT <= MATRIX_ROW_END_CHARPOS (row)
20112 && cursor_row_p (row))
20113 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20114
20115 /* Prepare for the next line. This line starts horizontally at (X
20116 HPOS) = (0 0). Vertical positions are incremented. As a
20117 convenience for the caller, IT->glyph_row is set to the next
20118 row to be used. */
20119 it->current_x = it->hpos = 0;
20120 it->current_y += row->height;
20121 SET_TEXT_POS (it->eol_pos, 0, 0);
20122 ++it->vpos;
20123 ++it->glyph_row;
20124 /* The next row should by default use the same value of the
20125 reversed_p flag as this one. set_iterator_to_next decides when
20126 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20127 the flag accordingly. */
20128 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20129 it->glyph_row->reversed_p = row->reversed_p;
20130 it->start = row->end;
20131 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20132
20133 #undef RECORD_MAX_MIN_POS
20134 }
20135
20136 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20137 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20138 doc: /* Return paragraph direction at point in BUFFER.
20139 Value is either `left-to-right' or `right-to-left'.
20140 If BUFFER is omitted or nil, it defaults to the current buffer.
20141
20142 Paragraph direction determines how the text in the paragraph is displayed.
20143 In left-to-right paragraphs, text begins at the left margin of the window
20144 and the reading direction is generally left to right. In right-to-left
20145 paragraphs, text begins at the right margin and is read from right to left.
20146
20147 See also `bidi-paragraph-direction'. */)
20148 (Lisp_Object buffer)
20149 {
20150 struct buffer *buf = current_buffer;
20151 struct buffer *old = buf;
20152
20153 if (! NILP (buffer))
20154 {
20155 CHECK_BUFFER (buffer);
20156 buf = XBUFFER (buffer);
20157 }
20158
20159 if (NILP (BVAR (buf, bidi_display_reordering))
20160 || NILP (BVAR (buf, enable_multibyte_characters))
20161 /* When we are loading loadup.el, the character property tables
20162 needed for bidi iteration are not yet available. */
20163 || !NILP (Vpurify_flag))
20164 return Qleft_to_right;
20165 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20166 return BVAR (buf, bidi_paragraph_direction);
20167 else
20168 {
20169 /* Determine the direction from buffer text. We could try to
20170 use current_matrix if it is up to date, but this seems fast
20171 enough as it is. */
20172 struct bidi_it itb;
20173 ptrdiff_t pos = BUF_PT (buf);
20174 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20175 int c;
20176 void *itb_data = bidi_shelve_cache ();
20177
20178 set_buffer_temp (buf);
20179 /* bidi_paragraph_init finds the base direction of the paragraph
20180 by searching forward from paragraph start. We need the base
20181 direction of the current or _previous_ paragraph, so we need
20182 to make sure we are within that paragraph. To that end, find
20183 the previous non-empty line. */
20184 if (pos >= ZV && pos > BEGV)
20185 DEC_BOTH (pos, bytepos);
20186 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20187 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20188 {
20189 while ((c = FETCH_BYTE (bytepos)) == '\n'
20190 || c == ' ' || c == '\t' || c == '\f')
20191 {
20192 if (bytepos <= BEGV_BYTE)
20193 break;
20194 bytepos--;
20195 pos--;
20196 }
20197 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20198 bytepos--;
20199 }
20200 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20201 itb.paragraph_dir = NEUTRAL_DIR;
20202 itb.string.s = NULL;
20203 itb.string.lstring = Qnil;
20204 itb.string.bufpos = 0;
20205 itb.string.unibyte = 0;
20206 /* We have no window to use here for ignoring window-specific
20207 overlays. Using NULL for window pointer will cause
20208 compute_display_string_pos to use the current buffer. */
20209 itb.w = NULL;
20210 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20211 bidi_unshelve_cache (itb_data, 0);
20212 set_buffer_temp (old);
20213 switch (itb.paragraph_dir)
20214 {
20215 case L2R:
20216 return Qleft_to_right;
20217 break;
20218 case R2L:
20219 return Qright_to_left;
20220 break;
20221 default:
20222 emacs_abort ();
20223 }
20224 }
20225 }
20226
20227 DEFUN ("move-point-visually", Fmove_point_visually,
20228 Smove_point_visually, 1, 1, 0,
20229 doc: /* Move point in the visual order in the specified DIRECTION.
20230 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20231 left.
20232
20233 Value is the new character position of point. */)
20234 (Lisp_Object direction)
20235 {
20236 struct window *w = XWINDOW (selected_window);
20237 struct buffer *b = XBUFFER (w->contents);
20238 struct glyph_row *row;
20239 int dir;
20240 Lisp_Object paragraph_dir;
20241
20242 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20243 (!(ROW)->continued_p \
20244 && INTEGERP ((GLYPH)->object) \
20245 && (GLYPH)->type == CHAR_GLYPH \
20246 && (GLYPH)->u.ch == ' ' \
20247 && (GLYPH)->charpos >= 0 \
20248 && !(GLYPH)->avoid_cursor_p)
20249
20250 CHECK_NUMBER (direction);
20251 dir = XINT (direction);
20252 if (dir > 0)
20253 dir = 1;
20254 else
20255 dir = -1;
20256
20257 /* If current matrix is up-to-date, we can use the information
20258 recorded in the glyphs, at least as long as the goal is on the
20259 screen. */
20260 if (w->window_end_valid
20261 && !windows_or_buffers_changed
20262 && b
20263 && !b->clip_changed
20264 && !b->prevent_redisplay_optimizations_p
20265 && !window_outdated (w)
20266 && w->cursor.vpos >= 0
20267 && w->cursor.vpos < w->current_matrix->nrows
20268 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20269 {
20270 struct glyph *g = row->glyphs[TEXT_AREA];
20271 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20272 struct glyph *gpt = g + w->cursor.hpos;
20273
20274 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20275 {
20276 if (BUFFERP (g->object) && g->charpos != PT)
20277 {
20278 SET_PT (g->charpos);
20279 w->cursor.vpos = -1;
20280 return make_number (PT);
20281 }
20282 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20283 {
20284 ptrdiff_t new_pos;
20285
20286 if (BUFFERP (gpt->object))
20287 {
20288 new_pos = PT;
20289 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20290 new_pos += (row->reversed_p ? -dir : dir);
20291 else
20292 new_pos -= (row->reversed_p ? -dir : dir);;
20293 }
20294 else if (BUFFERP (g->object))
20295 new_pos = g->charpos;
20296 else
20297 break;
20298 SET_PT (new_pos);
20299 w->cursor.vpos = -1;
20300 return make_number (PT);
20301 }
20302 else if (ROW_GLYPH_NEWLINE_P (row, g))
20303 {
20304 /* Glyphs inserted at the end of a non-empty line for
20305 positioning the cursor have zero charpos, so we must
20306 deduce the value of point by other means. */
20307 if (g->charpos > 0)
20308 SET_PT (g->charpos);
20309 else if (row->ends_at_zv_p && PT != ZV)
20310 SET_PT (ZV);
20311 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20312 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20313 else
20314 break;
20315 w->cursor.vpos = -1;
20316 return make_number (PT);
20317 }
20318 }
20319 if (g == e || INTEGERP (g->object))
20320 {
20321 if (row->truncated_on_left_p || row->truncated_on_right_p)
20322 goto simulate_display;
20323 if (!row->reversed_p)
20324 row += dir;
20325 else
20326 row -= dir;
20327 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20328 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20329 goto simulate_display;
20330
20331 if (dir > 0)
20332 {
20333 if (row->reversed_p && !row->continued_p)
20334 {
20335 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20336 w->cursor.vpos = -1;
20337 return make_number (PT);
20338 }
20339 g = row->glyphs[TEXT_AREA];
20340 e = g + row->used[TEXT_AREA];
20341 for ( ; g < e; g++)
20342 {
20343 if (BUFFERP (g->object)
20344 /* Empty lines have only one glyph, which stands
20345 for the newline, and whose charpos is the
20346 buffer position of the newline. */
20347 || ROW_GLYPH_NEWLINE_P (row, g)
20348 /* When the buffer ends in a newline, the line at
20349 EOB also has one glyph, but its charpos is -1. */
20350 || (row->ends_at_zv_p
20351 && !row->reversed_p
20352 && INTEGERP (g->object)
20353 && g->type == CHAR_GLYPH
20354 && g->u.ch == ' '))
20355 {
20356 if (g->charpos > 0)
20357 SET_PT (g->charpos);
20358 else if (!row->reversed_p
20359 && row->ends_at_zv_p
20360 && PT != ZV)
20361 SET_PT (ZV);
20362 else
20363 continue;
20364 w->cursor.vpos = -1;
20365 return make_number (PT);
20366 }
20367 }
20368 }
20369 else
20370 {
20371 if (!row->reversed_p && !row->continued_p)
20372 {
20373 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20374 w->cursor.vpos = -1;
20375 return make_number (PT);
20376 }
20377 e = row->glyphs[TEXT_AREA];
20378 g = e + row->used[TEXT_AREA] - 1;
20379 for ( ; g >= e; g--)
20380 {
20381 if (BUFFERP (g->object)
20382 || (ROW_GLYPH_NEWLINE_P (row, g)
20383 && g->charpos > 0)
20384 /* Empty R2L lines on GUI frames have the buffer
20385 position of the newline stored in the stretch
20386 glyph. */
20387 || g->type == STRETCH_GLYPH
20388 || (row->ends_at_zv_p
20389 && row->reversed_p
20390 && INTEGERP (g->object)
20391 && g->type == CHAR_GLYPH
20392 && g->u.ch == ' '))
20393 {
20394 if (g->charpos > 0)
20395 SET_PT (g->charpos);
20396 else if (row->reversed_p
20397 && row->ends_at_zv_p
20398 && PT != ZV)
20399 SET_PT (ZV);
20400 else
20401 continue;
20402 w->cursor.vpos = -1;
20403 return make_number (PT);
20404 }
20405 }
20406 }
20407 }
20408 }
20409
20410 simulate_display:
20411
20412 /* If we wind up here, we failed to move by using the glyphs, so we
20413 need to simulate display instead. */
20414
20415 if (b)
20416 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20417 else
20418 paragraph_dir = Qleft_to_right;
20419 if (EQ (paragraph_dir, Qright_to_left))
20420 dir = -dir;
20421 if (PT <= BEGV && dir < 0)
20422 xsignal0 (Qbeginning_of_buffer);
20423 else if (PT >= ZV && dir > 0)
20424 xsignal0 (Qend_of_buffer);
20425 else
20426 {
20427 struct text_pos pt;
20428 struct it it;
20429 int pt_x, target_x, pixel_width, pt_vpos;
20430 bool at_eol_p;
20431 bool overshoot_expected = false;
20432 bool target_is_eol_p = false;
20433
20434 /* Setup the arena. */
20435 SET_TEXT_POS (pt, PT, PT_BYTE);
20436 start_display (&it, w, pt);
20437
20438 if (it.cmp_it.id < 0
20439 && it.method == GET_FROM_STRING
20440 && it.area == TEXT_AREA
20441 && it.string_from_display_prop_p
20442 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20443 overshoot_expected = true;
20444
20445 /* Find the X coordinate of point. We start from the beginning
20446 of this or previous line to make sure we are before point in
20447 the logical order (since the move_it_* functions can only
20448 move forward). */
20449 reseat_at_previous_visible_line_start (&it);
20450 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20451 if (IT_CHARPOS (it) != PT)
20452 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20453 -1, -1, -1, MOVE_TO_POS);
20454 pt_x = it.current_x;
20455 pt_vpos = it.vpos;
20456 if (dir > 0 || overshoot_expected)
20457 {
20458 struct glyph_row *row = it.glyph_row;
20459
20460 /* When point is at beginning of line, we don't have
20461 information about the glyph there loaded into struct
20462 it. Calling get_next_display_element fixes that. */
20463 if (pt_x == 0)
20464 get_next_display_element (&it);
20465 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20466 it.glyph_row = NULL;
20467 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20468 it.glyph_row = row;
20469 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20470 it, lest it will become out of sync with it's buffer
20471 position. */
20472 it.current_x = pt_x;
20473 }
20474 else
20475 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20476 pixel_width = it.pixel_width;
20477 if (overshoot_expected && at_eol_p)
20478 pixel_width = 0;
20479 else if (pixel_width <= 0)
20480 pixel_width = 1;
20481
20482 /* If there's a display string at point, we are actually at the
20483 glyph to the left of point, so we need to correct the X
20484 coordinate. */
20485 if (overshoot_expected)
20486 pt_x += pixel_width;
20487
20488 /* Compute target X coordinate, either to the left or to the
20489 right of point. On TTY frames, all characters have the same
20490 pixel width of 1, so we can use that. On GUI frames we don't
20491 have an easy way of getting at the pixel width of the
20492 character to the left of point, so we use a different method
20493 of getting to that place. */
20494 if (dir > 0)
20495 target_x = pt_x + pixel_width;
20496 else
20497 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20498
20499 /* Target X coordinate could be one line above or below the line
20500 of point, in which case we need to adjust the target X
20501 coordinate. Also, if moving to the left, we need to begin at
20502 the left edge of the point's screen line. */
20503 if (dir < 0)
20504 {
20505 if (pt_x > 0)
20506 {
20507 start_display (&it, w, pt);
20508 reseat_at_previous_visible_line_start (&it);
20509 it.current_x = it.current_y = it.hpos = 0;
20510 if (pt_vpos != 0)
20511 move_it_by_lines (&it, pt_vpos);
20512 }
20513 else
20514 {
20515 move_it_by_lines (&it, -1);
20516 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20517 target_is_eol_p = true;
20518 }
20519 }
20520 else
20521 {
20522 if (at_eol_p
20523 || (target_x >= it.last_visible_x
20524 && it.line_wrap != TRUNCATE))
20525 {
20526 if (pt_x > 0)
20527 move_it_by_lines (&it, 0);
20528 move_it_by_lines (&it, 1);
20529 target_x = 0;
20530 }
20531 }
20532
20533 /* Move to the target X coordinate. */
20534 #ifdef HAVE_WINDOW_SYSTEM
20535 /* On GUI frames, as we don't know the X coordinate of the
20536 character to the left of point, moving point to the left
20537 requires walking, one grapheme cluster at a time, until we
20538 find ourself at a place immediately to the left of the
20539 character at point. */
20540 if (FRAME_WINDOW_P (it.f) && dir < 0)
20541 {
20542 struct text_pos new_pos = it.current.pos;
20543 enum move_it_result rc = MOVE_X_REACHED;
20544
20545 while (it.current_x + it.pixel_width <= target_x
20546 && rc == MOVE_X_REACHED)
20547 {
20548 int new_x = it.current_x + it.pixel_width;
20549
20550 new_pos = it.current.pos;
20551 if (new_x == it.current_x)
20552 new_x++;
20553 rc = move_it_in_display_line_to (&it, ZV, new_x,
20554 MOVE_TO_POS | MOVE_TO_X);
20555 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20556 break;
20557 }
20558 /* If we ended up on a composed character inside
20559 bidi-reordered text (e.g., Hebrew text with diacritics),
20560 the iterator gives us the buffer position of the last (in
20561 logical order) character of the composed grapheme cluster,
20562 which is not what we want. So we cheat: we compute the
20563 character position of the character that follows (in the
20564 logical order) the one where the above loop stopped. That
20565 character will appear on display to the left of point. */
20566 if (it.bidi_p
20567 && it.bidi_it.scan_dir == -1
20568 && new_pos.charpos - IT_CHARPOS (it) > 1)
20569 {
20570 new_pos.charpos = IT_CHARPOS (it) + 1;
20571 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20572 }
20573 it.current.pos = new_pos;
20574 }
20575 else
20576 #endif
20577 if (it.current_x != target_x)
20578 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20579
20580 /* When lines are truncated, the above loop will stop at the
20581 window edge. But we want to get to the end of line, even if
20582 it is beyond the window edge; automatic hscroll will then
20583 scroll the window to show point as appropriate. */
20584 if (target_is_eol_p && it.line_wrap == TRUNCATE
20585 && get_next_display_element (&it))
20586 {
20587 struct text_pos new_pos = it.current.pos;
20588
20589 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20590 {
20591 set_iterator_to_next (&it, 0);
20592 if (it.method == GET_FROM_BUFFER)
20593 new_pos = it.current.pos;
20594 if (!get_next_display_element (&it))
20595 break;
20596 }
20597
20598 it.current.pos = new_pos;
20599 }
20600
20601 /* If we ended up in a display string that covers point, move to
20602 buffer position to the right in the visual order. */
20603 if (dir > 0)
20604 {
20605 while (IT_CHARPOS (it) == PT)
20606 {
20607 set_iterator_to_next (&it, 0);
20608 if (!get_next_display_element (&it))
20609 break;
20610 }
20611 }
20612
20613 /* Move point to that position. */
20614 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20615 }
20616
20617 return make_number (PT);
20618
20619 #undef ROW_GLYPH_NEWLINE_P
20620 }
20621
20622 \f
20623 /***********************************************************************
20624 Menu Bar
20625 ***********************************************************************/
20626
20627 /* Redisplay the menu bar in the frame for window W.
20628
20629 The menu bar of X frames that don't have X toolkit support is
20630 displayed in a special window W->frame->menu_bar_window.
20631
20632 The menu bar of terminal frames is treated specially as far as
20633 glyph matrices are concerned. Menu bar lines are not part of
20634 windows, so the update is done directly on the frame matrix rows
20635 for the menu bar. */
20636
20637 static void
20638 display_menu_bar (struct window *w)
20639 {
20640 struct frame *f = XFRAME (WINDOW_FRAME (w));
20641 struct it it;
20642 Lisp_Object items;
20643 int i;
20644
20645 /* Don't do all this for graphical frames. */
20646 #ifdef HAVE_NTGUI
20647 if (FRAME_W32_P (f))
20648 return;
20649 #endif
20650 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20651 if (FRAME_X_P (f))
20652 return;
20653 #endif
20654
20655 #ifdef HAVE_NS
20656 if (FRAME_NS_P (f))
20657 return;
20658 #endif /* HAVE_NS */
20659
20660 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20661 eassert (!FRAME_WINDOW_P (f));
20662 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20663 it.first_visible_x = 0;
20664 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20665 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20666 if (FRAME_WINDOW_P (f))
20667 {
20668 /* Menu bar lines are displayed in the desired matrix of the
20669 dummy window menu_bar_window. */
20670 struct window *menu_w;
20671 menu_w = XWINDOW (f->menu_bar_window);
20672 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20673 MENU_FACE_ID);
20674 it.first_visible_x = 0;
20675 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20676 }
20677 else
20678 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20679 {
20680 /* This is a TTY frame, i.e. character hpos/vpos are used as
20681 pixel x/y. */
20682 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20683 MENU_FACE_ID);
20684 it.first_visible_x = 0;
20685 it.last_visible_x = FRAME_COLS (f);
20686 }
20687
20688 /* FIXME: This should be controlled by a user option. See the
20689 comments in redisplay_tool_bar and display_mode_line about
20690 this. */
20691 it.paragraph_embedding = L2R;
20692
20693 /* Clear all rows of the menu bar. */
20694 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20695 {
20696 struct glyph_row *row = it.glyph_row + i;
20697 clear_glyph_row (row);
20698 row->enabled_p = 1;
20699 row->full_width_p = 1;
20700 }
20701
20702 /* Display all items of the menu bar. */
20703 items = FRAME_MENU_BAR_ITEMS (it.f);
20704 for (i = 0; i < ASIZE (items); i += 4)
20705 {
20706 Lisp_Object string;
20707
20708 /* Stop at nil string. */
20709 string = AREF (items, i + 1);
20710 if (NILP (string))
20711 break;
20712
20713 /* Remember where item was displayed. */
20714 ASET (items, i + 3, make_number (it.hpos));
20715
20716 /* Display the item, pad with one space. */
20717 if (it.current_x < it.last_visible_x)
20718 display_string (NULL, string, Qnil, 0, 0, &it,
20719 SCHARS (string) + 1, 0, 0, -1);
20720 }
20721
20722 /* Fill out the line with spaces. */
20723 if (it.current_x < it.last_visible_x)
20724 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20725
20726 /* Compute the total height of the lines. */
20727 compute_line_metrics (&it);
20728 }
20729
20730
20731 \f
20732 /***********************************************************************
20733 Mode Line
20734 ***********************************************************************/
20735
20736 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20737 FORCE is non-zero, redisplay mode lines unconditionally.
20738 Otherwise, redisplay only mode lines that are garbaged. Value is
20739 the number of windows whose mode lines were redisplayed. */
20740
20741 static int
20742 redisplay_mode_lines (Lisp_Object window, int force)
20743 {
20744 int nwindows = 0;
20745
20746 while (!NILP (window))
20747 {
20748 struct window *w = XWINDOW (window);
20749
20750 if (WINDOWP (w->contents))
20751 nwindows += redisplay_mode_lines (w->contents, force);
20752 else if (force
20753 || FRAME_GARBAGED_P (XFRAME (w->frame))
20754 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20755 {
20756 struct text_pos lpoint;
20757 struct buffer *old = current_buffer;
20758
20759 /* Set the window's buffer for the mode line display. */
20760 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20761 set_buffer_internal_1 (XBUFFER (w->contents));
20762
20763 /* Point refers normally to the selected window. For any
20764 other window, set up appropriate value. */
20765 if (!EQ (window, selected_window))
20766 {
20767 struct text_pos pt;
20768
20769 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20770 if (CHARPOS (pt) < BEGV)
20771 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20772 else if (CHARPOS (pt) > (ZV - 1))
20773 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20774 else
20775 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20776 }
20777
20778 /* Display mode lines. */
20779 clear_glyph_matrix (w->desired_matrix);
20780 if (display_mode_lines (w))
20781 {
20782 ++nwindows;
20783 w->must_be_updated_p = 1;
20784 }
20785
20786 /* Restore old settings. */
20787 set_buffer_internal_1 (old);
20788 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20789 }
20790
20791 window = w->next;
20792 }
20793
20794 return nwindows;
20795 }
20796
20797
20798 /* Display the mode and/or header line of window W. Value is the
20799 sum number of mode lines and header lines displayed. */
20800
20801 static int
20802 display_mode_lines (struct window *w)
20803 {
20804 Lisp_Object old_selected_window = selected_window;
20805 Lisp_Object old_selected_frame = selected_frame;
20806 Lisp_Object new_frame = w->frame;
20807 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20808 int n = 0;
20809
20810 selected_frame = new_frame;
20811 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20812 or window's point, then we'd need select_window_1 here as well. */
20813 XSETWINDOW (selected_window, w);
20814 XFRAME (new_frame)->selected_window = selected_window;
20815
20816 /* These will be set while the mode line specs are processed. */
20817 line_number_displayed = 0;
20818 w->column_number_displayed = -1;
20819
20820 if (WINDOW_WANTS_MODELINE_P (w))
20821 {
20822 struct window *sel_w = XWINDOW (old_selected_window);
20823
20824 /* Select mode line face based on the real selected window. */
20825 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20826 BVAR (current_buffer, mode_line_format));
20827 ++n;
20828 }
20829
20830 if (WINDOW_WANTS_HEADER_LINE_P (w))
20831 {
20832 display_mode_line (w, HEADER_LINE_FACE_ID,
20833 BVAR (current_buffer, header_line_format));
20834 ++n;
20835 }
20836
20837 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20838 selected_frame = old_selected_frame;
20839 selected_window = old_selected_window;
20840 return n;
20841 }
20842
20843
20844 /* Display mode or header line of window W. FACE_ID specifies which
20845 line to display; it is either MODE_LINE_FACE_ID or
20846 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20847 display. Value is the pixel height of the mode/header line
20848 displayed. */
20849
20850 static int
20851 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20852 {
20853 struct it it;
20854 struct face *face;
20855 ptrdiff_t count = SPECPDL_INDEX ();
20856
20857 init_iterator (&it, w, -1, -1, NULL, face_id);
20858 /* Don't extend on a previously drawn mode-line.
20859 This may happen if called from pos_visible_p. */
20860 it.glyph_row->enabled_p = 0;
20861 prepare_desired_row (it.glyph_row);
20862
20863 it.glyph_row->mode_line_p = 1;
20864
20865 /* FIXME: This should be controlled by a user option. But
20866 supporting such an option is not trivial, since the mode line is
20867 made up of many separate strings. */
20868 it.paragraph_embedding = L2R;
20869
20870 record_unwind_protect (unwind_format_mode_line,
20871 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20872
20873 mode_line_target = MODE_LINE_DISPLAY;
20874
20875 /* Temporarily make frame's keyboard the current kboard so that
20876 kboard-local variables in the mode_line_format will get the right
20877 values. */
20878 push_kboard (FRAME_KBOARD (it.f));
20879 record_unwind_save_match_data ();
20880 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20881 pop_kboard ();
20882
20883 unbind_to (count, Qnil);
20884
20885 /* Fill up with spaces. */
20886 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20887
20888 compute_line_metrics (&it);
20889 it.glyph_row->full_width_p = 1;
20890 it.glyph_row->continued_p = 0;
20891 it.glyph_row->truncated_on_left_p = 0;
20892 it.glyph_row->truncated_on_right_p = 0;
20893
20894 /* Make a 3D mode-line have a shadow at its right end. */
20895 face = FACE_FROM_ID (it.f, face_id);
20896 extend_face_to_end_of_line (&it);
20897 if (face->box != FACE_NO_BOX)
20898 {
20899 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20900 + it.glyph_row->used[TEXT_AREA] - 1);
20901 last->right_box_line_p = 1;
20902 }
20903
20904 return it.glyph_row->height;
20905 }
20906
20907 /* Move element ELT in LIST to the front of LIST.
20908 Return the updated list. */
20909
20910 static Lisp_Object
20911 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20912 {
20913 register Lisp_Object tail, prev;
20914 register Lisp_Object tem;
20915
20916 tail = list;
20917 prev = Qnil;
20918 while (CONSP (tail))
20919 {
20920 tem = XCAR (tail);
20921
20922 if (EQ (elt, tem))
20923 {
20924 /* Splice out the link TAIL. */
20925 if (NILP (prev))
20926 list = XCDR (tail);
20927 else
20928 Fsetcdr (prev, XCDR (tail));
20929
20930 /* Now make it the first. */
20931 Fsetcdr (tail, list);
20932 return tail;
20933 }
20934 else
20935 prev = tail;
20936 tail = XCDR (tail);
20937 QUIT;
20938 }
20939
20940 /* Not found--return unchanged LIST. */
20941 return list;
20942 }
20943
20944 /* Contribute ELT to the mode line for window IT->w. How it
20945 translates into text depends on its data type.
20946
20947 IT describes the display environment in which we display, as usual.
20948
20949 DEPTH is the depth in recursion. It is used to prevent
20950 infinite recursion here.
20951
20952 FIELD_WIDTH is the number of characters the display of ELT should
20953 occupy in the mode line, and PRECISION is the maximum number of
20954 characters to display from ELT's representation. See
20955 display_string for details.
20956
20957 Returns the hpos of the end of the text generated by ELT.
20958
20959 PROPS is a property list to add to any string we encounter.
20960
20961 If RISKY is nonzero, remove (disregard) any properties in any string
20962 we encounter, and ignore :eval and :propertize.
20963
20964 The global variable `mode_line_target' determines whether the
20965 output is passed to `store_mode_line_noprop',
20966 `store_mode_line_string', or `display_string'. */
20967
20968 static int
20969 display_mode_element (struct it *it, int depth, int field_width, int precision,
20970 Lisp_Object elt, Lisp_Object props, int risky)
20971 {
20972 int n = 0, field, prec;
20973 int literal = 0;
20974
20975 tail_recurse:
20976 if (depth > 100)
20977 elt = build_string ("*too-deep*");
20978
20979 depth++;
20980
20981 switch (XTYPE (elt))
20982 {
20983 case Lisp_String:
20984 {
20985 /* A string: output it and check for %-constructs within it. */
20986 unsigned char c;
20987 ptrdiff_t offset = 0;
20988
20989 if (SCHARS (elt) > 0
20990 && (!NILP (props) || risky))
20991 {
20992 Lisp_Object oprops, aelt;
20993 oprops = Ftext_properties_at (make_number (0), elt);
20994
20995 /* If the starting string's properties are not what
20996 we want, translate the string. Also, if the string
20997 is risky, do that anyway. */
20998
20999 if (NILP (Fequal (props, oprops)) || risky)
21000 {
21001 /* If the starting string has properties,
21002 merge the specified ones onto the existing ones. */
21003 if (! NILP (oprops) && !risky)
21004 {
21005 Lisp_Object tem;
21006
21007 oprops = Fcopy_sequence (oprops);
21008 tem = props;
21009 while (CONSP (tem))
21010 {
21011 oprops = Fplist_put (oprops, XCAR (tem),
21012 XCAR (XCDR (tem)));
21013 tem = XCDR (XCDR (tem));
21014 }
21015 props = oprops;
21016 }
21017
21018 aelt = Fassoc (elt, mode_line_proptrans_alist);
21019 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21020 {
21021 /* AELT is what we want. Move it to the front
21022 without consing. */
21023 elt = XCAR (aelt);
21024 mode_line_proptrans_alist
21025 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21026 }
21027 else
21028 {
21029 Lisp_Object tem;
21030
21031 /* If AELT has the wrong props, it is useless.
21032 so get rid of it. */
21033 if (! NILP (aelt))
21034 mode_line_proptrans_alist
21035 = Fdelq (aelt, mode_line_proptrans_alist);
21036
21037 elt = Fcopy_sequence (elt);
21038 Fset_text_properties (make_number (0), Flength (elt),
21039 props, elt);
21040 /* Add this item to mode_line_proptrans_alist. */
21041 mode_line_proptrans_alist
21042 = Fcons (Fcons (elt, props),
21043 mode_line_proptrans_alist);
21044 /* Truncate mode_line_proptrans_alist
21045 to at most 50 elements. */
21046 tem = Fnthcdr (make_number (50),
21047 mode_line_proptrans_alist);
21048 if (! NILP (tem))
21049 XSETCDR (tem, Qnil);
21050 }
21051 }
21052 }
21053
21054 offset = 0;
21055
21056 if (literal)
21057 {
21058 prec = precision - n;
21059 switch (mode_line_target)
21060 {
21061 case MODE_LINE_NOPROP:
21062 case MODE_LINE_TITLE:
21063 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21064 break;
21065 case MODE_LINE_STRING:
21066 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21067 break;
21068 case MODE_LINE_DISPLAY:
21069 n += display_string (NULL, elt, Qnil, 0, 0, it,
21070 0, prec, 0, STRING_MULTIBYTE (elt));
21071 break;
21072 }
21073
21074 break;
21075 }
21076
21077 /* Handle the non-literal case. */
21078
21079 while ((precision <= 0 || n < precision)
21080 && SREF (elt, offset) != 0
21081 && (mode_line_target != MODE_LINE_DISPLAY
21082 || it->current_x < it->last_visible_x))
21083 {
21084 ptrdiff_t last_offset = offset;
21085
21086 /* Advance to end of string or next format specifier. */
21087 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21088 ;
21089
21090 if (offset - 1 != last_offset)
21091 {
21092 ptrdiff_t nchars, nbytes;
21093
21094 /* Output to end of string or up to '%'. Field width
21095 is length of string. Don't output more than
21096 PRECISION allows us. */
21097 offset--;
21098
21099 prec = c_string_width (SDATA (elt) + last_offset,
21100 offset - last_offset, precision - n,
21101 &nchars, &nbytes);
21102
21103 switch (mode_line_target)
21104 {
21105 case MODE_LINE_NOPROP:
21106 case MODE_LINE_TITLE:
21107 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21108 break;
21109 case MODE_LINE_STRING:
21110 {
21111 ptrdiff_t bytepos = last_offset;
21112 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21113 ptrdiff_t endpos = (precision <= 0
21114 ? string_byte_to_char (elt, offset)
21115 : charpos + nchars);
21116
21117 n += store_mode_line_string (NULL,
21118 Fsubstring (elt, make_number (charpos),
21119 make_number (endpos)),
21120 0, 0, 0, Qnil);
21121 }
21122 break;
21123 case MODE_LINE_DISPLAY:
21124 {
21125 ptrdiff_t bytepos = last_offset;
21126 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21127
21128 if (precision <= 0)
21129 nchars = string_byte_to_char (elt, offset) - charpos;
21130 n += display_string (NULL, elt, Qnil, 0, charpos,
21131 it, 0, nchars, 0,
21132 STRING_MULTIBYTE (elt));
21133 }
21134 break;
21135 }
21136 }
21137 else /* c == '%' */
21138 {
21139 ptrdiff_t percent_position = offset;
21140
21141 /* Get the specified minimum width. Zero means
21142 don't pad. */
21143 field = 0;
21144 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21145 field = field * 10 + c - '0';
21146
21147 /* Don't pad beyond the total padding allowed. */
21148 if (field_width - n > 0 && field > field_width - n)
21149 field = field_width - n;
21150
21151 /* Note that either PRECISION <= 0 or N < PRECISION. */
21152 prec = precision - n;
21153
21154 if (c == 'M')
21155 n += display_mode_element (it, depth, field, prec,
21156 Vglobal_mode_string, props,
21157 risky);
21158 else if (c != 0)
21159 {
21160 bool multibyte;
21161 ptrdiff_t bytepos, charpos;
21162 const char *spec;
21163 Lisp_Object string;
21164
21165 bytepos = percent_position;
21166 charpos = (STRING_MULTIBYTE (elt)
21167 ? string_byte_to_char (elt, bytepos)
21168 : bytepos);
21169 spec = decode_mode_spec (it->w, c, field, &string);
21170 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21171
21172 switch (mode_line_target)
21173 {
21174 case MODE_LINE_NOPROP:
21175 case MODE_LINE_TITLE:
21176 n += store_mode_line_noprop (spec, field, prec);
21177 break;
21178 case MODE_LINE_STRING:
21179 {
21180 Lisp_Object tem = build_string (spec);
21181 props = Ftext_properties_at (make_number (charpos), elt);
21182 /* Should only keep face property in props */
21183 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21184 }
21185 break;
21186 case MODE_LINE_DISPLAY:
21187 {
21188 int nglyphs_before, nwritten;
21189
21190 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21191 nwritten = display_string (spec, string, elt,
21192 charpos, 0, it,
21193 field, prec, 0,
21194 multibyte);
21195
21196 /* Assign to the glyphs written above the
21197 string where the `%x' came from, position
21198 of the `%'. */
21199 if (nwritten > 0)
21200 {
21201 struct glyph *glyph
21202 = (it->glyph_row->glyphs[TEXT_AREA]
21203 + nglyphs_before);
21204 int i;
21205
21206 for (i = 0; i < nwritten; ++i)
21207 {
21208 glyph[i].object = elt;
21209 glyph[i].charpos = charpos;
21210 }
21211
21212 n += nwritten;
21213 }
21214 }
21215 break;
21216 }
21217 }
21218 else /* c == 0 */
21219 break;
21220 }
21221 }
21222 }
21223 break;
21224
21225 case Lisp_Symbol:
21226 /* A symbol: process the value of the symbol recursively
21227 as if it appeared here directly. Avoid error if symbol void.
21228 Special case: if value of symbol is a string, output the string
21229 literally. */
21230 {
21231 register Lisp_Object tem;
21232
21233 /* If the variable is not marked as risky to set
21234 then its contents are risky to use. */
21235 if (NILP (Fget (elt, Qrisky_local_variable)))
21236 risky = 1;
21237
21238 tem = Fboundp (elt);
21239 if (!NILP (tem))
21240 {
21241 tem = Fsymbol_value (elt);
21242 /* If value is a string, output that string literally:
21243 don't check for % within it. */
21244 if (STRINGP (tem))
21245 literal = 1;
21246
21247 if (!EQ (tem, elt))
21248 {
21249 /* Give up right away for nil or t. */
21250 elt = tem;
21251 goto tail_recurse;
21252 }
21253 }
21254 }
21255 break;
21256
21257 case Lisp_Cons:
21258 {
21259 register Lisp_Object car, tem;
21260
21261 /* A cons cell: five distinct cases.
21262 If first element is :eval or :propertize, do something special.
21263 If first element is a string or a cons, process all the elements
21264 and effectively concatenate them.
21265 If first element is a negative number, truncate displaying cdr to
21266 at most that many characters. If positive, pad (with spaces)
21267 to at least that many characters.
21268 If first element is a symbol, process the cadr or caddr recursively
21269 according to whether the symbol's value is non-nil or nil. */
21270 car = XCAR (elt);
21271 if (EQ (car, QCeval))
21272 {
21273 /* An element of the form (:eval FORM) means evaluate FORM
21274 and use the result as mode line elements. */
21275
21276 if (risky)
21277 break;
21278
21279 if (CONSP (XCDR (elt)))
21280 {
21281 Lisp_Object spec;
21282 spec = safe_eval (XCAR (XCDR (elt)));
21283 n += display_mode_element (it, depth, field_width - n,
21284 precision - n, spec, props,
21285 risky);
21286 }
21287 }
21288 else if (EQ (car, QCpropertize))
21289 {
21290 /* An element of the form (:propertize ELT PROPS...)
21291 means display ELT but applying properties PROPS. */
21292
21293 if (risky)
21294 break;
21295
21296 if (CONSP (XCDR (elt)))
21297 n += display_mode_element (it, depth, field_width - n,
21298 precision - n, XCAR (XCDR (elt)),
21299 XCDR (XCDR (elt)), risky);
21300 }
21301 else if (SYMBOLP (car))
21302 {
21303 tem = Fboundp (car);
21304 elt = XCDR (elt);
21305 if (!CONSP (elt))
21306 goto invalid;
21307 /* elt is now the cdr, and we know it is a cons cell.
21308 Use its car if CAR has a non-nil value. */
21309 if (!NILP (tem))
21310 {
21311 tem = Fsymbol_value (car);
21312 if (!NILP (tem))
21313 {
21314 elt = XCAR (elt);
21315 goto tail_recurse;
21316 }
21317 }
21318 /* Symbol's value is nil (or symbol is unbound)
21319 Get the cddr of the original list
21320 and if possible find the caddr and use that. */
21321 elt = XCDR (elt);
21322 if (NILP (elt))
21323 break;
21324 else if (!CONSP (elt))
21325 goto invalid;
21326 elt = XCAR (elt);
21327 goto tail_recurse;
21328 }
21329 else if (INTEGERP (car))
21330 {
21331 register int lim = XINT (car);
21332 elt = XCDR (elt);
21333 if (lim < 0)
21334 {
21335 /* Negative int means reduce maximum width. */
21336 if (precision <= 0)
21337 precision = -lim;
21338 else
21339 precision = min (precision, -lim);
21340 }
21341 else if (lim > 0)
21342 {
21343 /* Padding specified. Don't let it be more than
21344 current maximum. */
21345 if (precision > 0)
21346 lim = min (precision, lim);
21347
21348 /* If that's more padding than already wanted, queue it.
21349 But don't reduce padding already specified even if
21350 that is beyond the current truncation point. */
21351 field_width = max (lim, field_width);
21352 }
21353 goto tail_recurse;
21354 }
21355 else if (STRINGP (car) || CONSP (car))
21356 {
21357 Lisp_Object halftail = elt;
21358 int len = 0;
21359
21360 while (CONSP (elt)
21361 && (precision <= 0 || n < precision))
21362 {
21363 n += display_mode_element (it, depth,
21364 /* Do padding only after the last
21365 element in the list. */
21366 (! CONSP (XCDR (elt))
21367 ? field_width - n
21368 : 0),
21369 precision - n, XCAR (elt),
21370 props, risky);
21371 elt = XCDR (elt);
21372 len++;
21373 if ((len & 1) == 0)
21374 halftail = XCDR (halftail);
21375 /* Check for cycle. */
21376 if (EQ (halftail, elt))
21377 break;
21378 }
21379 }
21380 }
21381 break;
21382
21383 default:
21384 invalid:
21385 elt = build_string ("*invalid*");
21386 goto tail_recurse;
21387 }
21388
21389 /* Pad to FIELD_WIDTH. */
21390 if (field_width > 0 && n < field_width)
21391 {
21392 switch (mode_line_target)
21393 {
21394 case MODE_LINE_NOPROP:
21395 case MODE_LINE_TITLE:
21396 n += store_mode_line_noprop ("", field_width - n, 0);
21397 break;
21398 case MODE_LINE_STRING:
21399 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21400 break;
21401 case MODE_LINE_DISPLAY:
21402 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21403 0, 0, 0);
21404 break;
21405 }
21406 }
21407
21408 return n;
21409 }
21410
21411 /* Store a mode-line string element in mode_line_string_list.
21412
21413 If STRING is non-null, display that C string. Otherwise, the Lisp
21414 string LISP_STRING is displayed.
21415
21416 FIELD_WIDTH is the minimum number of output glyphs to produce.
21417 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21418 with spaces. FIELD_WIDTH <= 0 means don't pad.
21419
21420 PRECISION is the maximum number of characters to output from
21421 STRING. PRECISION <= 0 means don't truncate the string.
21422
21423 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21424 properties to the string.
21425
21426 PROPS are the properties to add to the string.
21427 The mode_line_string_face face property is always added to the string.
21428 */
21429
21430 static int
21431 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21432 int field_width, int precision, Lisp_Object props)
21433 {
21434 ptrdiff_t len;
21435 int n = 0;
21436
21437 if (string != NULL)
21438 {
21439 len = strlen (string);
21440 if (precision > 0 && len > precision)
21441 len = precision;
21442 lisp_string = make_string (string, len);
21443 if (NILP (props))
21444 props = mode_line_string_face_prop;
21445 else if (!NILP (mode_line_string_face))
21446 {
21447 Lisp_Object face = Fplist_get (props, Qface);
21448 props = Fcopy_sequence (props);
21449 if (NILP (face))
21450 face = mode_line_string_face;
21451 else
21452 face = list2 (face, mode_line_string_face);
21453 props = Fplist_put (props, Qface, face);
21454 }
21455 Fadd_text_properties (make_number (0), make_number (len),
21456 props, lisp_string);
21457 }
21458 else
21459 {
21460 len = XFASTINT (Flength (lisp_string));
21461 if (precision > 0 && len > precision)
21462 {
21463 len = precision;
21464 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21465 precision = -1;
21466 }
21467 if (!NILP (mode_line_string_face))
21468 {
21469 Lisp_Object face;
21470 if (NILP (props))
21471 props = Ftext_properties_at (make_number (0), lisp_string);
21472 face = Fplist_get (props, Qface);
21473 if (NILP (face))
21474 face = mode_line_string_face;
21475 else
21476 face = list2 (face, mode_line_string_face);
21477 props = list2 (Qface, face);
21478 if (copy_string)
21479 lisp_string = Fcopy_sequence (lisp_string);
21480 }
21481 if (!NILP (props))
21482 Fadd_text_properties (make_number (0), make_number (len),
21483 props, lisp_string);
21484 }
21485
21486 if (len > 0)
21487 {
21488 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21489 n += len;
21490 }
21491
21492 if (field_width > len)
21493 {
21494 field_width -= len;
21495 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21496 if (!NILP (props))
21497 Fadd_text_properties (make_number (0), make_number (field_width),
21498 props, lisp_string);
21499 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21500 n += field_width;
21501 }
21502
21503 return n;
21504 }
21505
21506
21507 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21508 1, 4, 0,
21509 doc: /* Format a string out of a mode line format specification.
21510 First arg FORMAT specifies the mode line format (see `mode-line-format'
21511 for details) to use.
21512
21513 By default, the format is evaluated for the currently selected window.
21514
21515 Optional second arg FACE specifies the face property to put on all
21516 characters for which no face is specified. The value nil means the
21517 default face. The value t means whatever face the window's mode line
21518 currently uses (either `mode-line' or `mode-line-inactive',
21519 depending on whether the window is the selected window or not).
21520 An integer value means the value string has no text
21521 properties.
21522
21523 Optional third and fourth args WINDOW and BUFFER specify the window
21524 and buffer to use as the context for the formatting (defaults
21525 are the selected window and the WINDOW's buffer). */)
21526 (Lisp_Object format, Lisp_Object face,
21527 Lisp_Object window, Lisp_Object buffer)
21528 {
21529 struct it it;
21530 int len;
21531 struct window *w;
21532 struct buffer *old_buffer = NULL;
21533 int face_id;
21534 int no_props = INTEGERP (face);
21535 ptrdiff_t count = SPECPDL_INDEX ();
21536 Lisp_Object str;
21537 int string_start = 0;
21538
21539 w = decode_any_window (window);
21540 XSETWINDOW (window, w);
21541
21542 if (NILP (buffer))
21543 buffer = w->contents;
21544 CHECK_BUFFER (buffer);
21545
21546 /* Make formatting the modeline a non-op when noninteractive, otherwise
21547 there will be problems later caused by a partially initialized frame. */
21548 if (NILP (format) || noninteractive)
21549 return empty_unibyte_string;
21550
21551 if (no_props)
21552 face = Qnil;
21553
21554 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21555 : EQ (face, Qt) ? (EQ (window, selected_window)
21556 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21557 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21558 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21559 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21560 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21561 : DEFAULT_FACE_ID;
21562
21563 old_buffer = current_buffer;
21564
21565 /* Save things including mode_line_proptrans_alist,
21566 and set that to nil so that we don't alter the outer value. */
21567 record_unwind_protect (unwind_format_mode_line,
21568 format_mode_line_unwind_data
21569 (XFRAME (WINDOW_FRAME (w)),
21570 old_buffer, selected_window, 1));
21571 mode_line_proptrans_alist = Qnil;
21572
21573 Fselect_window (window, Qt);
21574 set_buffer_internal_1 (XBUFFER (buffer));
21575
21576 init_iterator (&it, w, -1, -1, NULL, face_id);
21577
21578 if (no_props)
21579 {
21580 mode_line_target = MODE_LINE_NOPROP;
21581 mode_line_string_face_prop = Qnil;
21582 mode_line_string_list = Qnil;
21583 string_start = MODE_LINE_NOPROP_LEN (0);
21584 }
21585 else
21586 {
21587 mode_line_target = MODE_LINE_STRING;
21588 mode_line_string_list = Qnil;
21589 mode_line_string_face = face;
21590 mode_line_string_face_prop
21591 = NILP (face) ? Qnil : list2 (Qface, face);
21592 }
21593
21594 push_kboard (FRAME_KBOARD (it.f));
21595 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21596 pop_kboard ();
21597
21598 if (no_props)
21599 {
21600 len = MODE_LINE_NOPROP_LEN (string_start);
21601 str = make_string (mode_line_noprop_buf + string_start, len);
21602 }
21603 else
21604 {
21605 mode_line_string_list = Fnreverse (mode_line_string_list);
21606 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21607 empty_unibyte_string);
21608 }
21609
21610 unbind_to (count, Qnil);
21611 return str;
21612 }
21613
21614 /* Write a null-terminated, right justified decimal representation of
21615 the positive integer D to BUF using a minimal field width WIDTH. */
21616
21617 static void
21618 pint2str (register char *buf, register int width, register ptrdiff_t d)
21619 {
21620 register char *p = buf;
21621
21622 if (d <= 0)
21623 *p++ = '0';
21624 else
21625 {
21626 while (d > 0)
21627 {
21628 *p++ = d % 10 + '0';
21629 d /= 10;
21630 }
21631 }
21632
21633 for (width -= (int) (p - buf); width > 0; --width)
21634 *p++ = ' ';
21635 *p-- = '\0';
21636 while (p > buf)
21637 {
21638 d = *buf;
21639 *buf++ = *p;
21640 *p-- = d;
21641 }
21642 }
21643
21644 /* Write a null-terminated, right justified decimal and "human
21645 readable" representation of the nonnegative integer D to BUF using
21646 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21647
21648 static const char power_letter[] =
21649 {
21650 0, /* no letter */
21651 'k', /* kilo */
21652 'M', /* mega */
21653 'G', /* giga */
21654 'T', /* tera */
21655 'P', /* peta */
21656 'E', /* exa */
21657 'Z', /* zetta */
21658 'Y' /* yotta */
21659 };
21660
21661 static void
21662 pint2hrstr (char *buf, int width, ptrdiff_t d)
21663 {
21664 /* We aim to represent the nonnegative integer D as
21665 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21666 ptrdiff_t quotient = d;
21667 int remainder = 0;
21668 /* -1 means: do not use TENTHS. */
21669 int tenths = -1;
21670 int exponent = 0;
21671
21672 /* Length of QUOTIENT.TENTHS as a string. */
21673 int length;
21674
21675 char * psuffix;
21676 char * p;
21677
21678 if (quotient >= 1000)
21679 {
21680 /* Scale to the appropriate EXPONENT. */
21681 do
21682 {
21683 remainder = quotient % 1000;
21684 quotient /= 1000;
21685 exponent++;
21686 }
21687 while (quotient >= 1000);
21688
21689 /* Round to nearest and decide whether to use TENTHS or not. */
21690 if (quotient <= 9)
21691 {
21692 tenths = remainder / 100;
21693 if (remainder % 100 >= 50)
21694 {
21695 if (tenths < 9)
21696 tenths++;
21697 else
21698 {
21699 quotient++;
21700 if (quotient == 10)
21701 tenths = -1;
21702 else
21703 tenths = 0;
21704 }
21705 }
21706 }
21707 else
21708 if (remainder >= 500)
21709 {
21710 if (quotient < 999)
21711 quotient++;
21712 else
21713 {
21714 quotient = 1;
21715 exponent++;
21716 tenths = 0;
21717 }
21718 }
21719 }
21720
21721 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21722 if (tenths == -1 && quotient <= 99)
21723 if (quotient <= 9)
21724 length = 1;
21725 else
21726 length = 2;
21727 else
21728 length = 3;
21729 p = psuffix = buf + max (width, length);
21730
21731 /* Print EXPONENT. */
21732 *psuffix++ = power_letter[exponent];
21733 *psuffix = '\0';
21734
21735 /* Print TENTHS. */
21736 if (tenths >= 0)
21737 {
21738 *--p = '0' + tenths;
21739 *--p = '.';
21740 }
21741
21742 /* Print QUOTIENT. */
21743 do
21744 {
21745 int digit = quotient % 10;
21746 *--p = '0' + digit;
21747 }
21748 while ((quotient /= 10) != 0);
21749
21750 /* Print leading spaces. */
21751 while (buf < p)
21752 *--p = ' ';
21753 }
21754
21755 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21756 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21757 type of CODING_SYSTEM. Return updated pointer into BUF. */
21758
21759 static unsigned char invalid_eol_type[] = "(*invalid*)";
21760
21761 static char *
21762 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21763 {
21764 Lisp_Object val;
21765 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21766 const unsigned char *eol_str;
21767 int eol_str_len;
21768 /* The EOL conversion we are using. */
21769 Lisp_Object eoltype;
21770
21771 val = CODING_SYSTEM_SPEC (coding_system);
21772 eoltype = Qnil;
21773
21774 if (!VECTORP (val)) /* Not yet decided. */
21775 {
21776 *buf++ = multibyte ? '-' : ' ';
21777 if (eol_flag)
21778 eoltype = eol_mnemonic_undecided;
21779 /* Don't mention EOL conversion if it isn't decided. */
21780 }
21781 else
21782 {
21783 Lisp_Object attrs;
21784 Lisp_Object eolvalue;
21785
21786 attrs = AREF (val, 0);
21787 eolvalue = AREF (val, 2);
21788
21789 *buf++ = multibyte
21790 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21791 : ' ';
21792
21793 if (eol_flag)
21794 {
21795 /* The EOL conversion that is normal on this system. */
21796
21797 if (NILP (eolvalue)) /* Not yet decided. */
21798 eoltype = eol_mnemonic_undecided;
21799 else if (VECTORP (eolvalue)) /* Not yet decided. */
21800 eoltype = eol_mnemonic_undecided;
21801 else /* eolvalue is Qunix, Qdos, or Qmac. */
21802 eoltype = (EQ (eolvalue, Qunix)
21803 ? eol_mnemonic_unix
21804 : (EQ (eolvalue, Qdos) == 1
21805 ? eol_mnemonic_dos : eol_mnemonic_mac));
21806 }
21807 }
21808
21809 if (eol_flag)
21810 {
21811 /* Mention the EOL conversion if it is not the usual one. */
21812 if (STRINGP (eoltype))
21813 {
21814 eol_str = SDATA (eoltype);
21815 eol_str_len = SBYTES (eoltype);
21816 }
21817 else if (CHARACTERP (eoltype))
21818 {
21819 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21820 int c = XFASTINT (eoltype);
21821 eol_str_len = CHAR_STRING (c, tmp);
21822 eol_str = tmp;
21823 }
21824 else
21825 {
21826 eol_str = invalid_eol_type;
21827 eol_str_len = sizeof (invalid_eol_type) - 1;
21828 }
21829 memcpy (buf, eol_str, eol_str_len);
21830 buf += eol_str_len;
21831 }
21832
21833 return buf;
21834 }
21835
21836 /* Return a string for the output of a mode line %-spec for window W,
21837 generated by character C. FIELD_WIDTH > 0 means pad the string
21838 returned with spaces to that value. Return a Lisp string in
21839 *STRING if the resulting string is taken from that Lisp string.
21840
21841 Note we operate on the current buffer for most purposes. */
21842
21843 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21844
21845 static const char *
21846 decode_mode_spec (struct window *w, register int c, int field_width,
21847 Lisp_Object *string)
21848 {
21849 Lisp_Object obj;
21850 struct frame *f = XFRAME (WINDOW_FRAME (w));
21851 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21852 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21853 produce strings from numerical values, so limit preposterously
21854 large values of FIELD_WIDTH to avoid overrunning the buffer's
21855 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21856 bytes plus the terminating null. */
21857 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21858 struct buffer *b = current_buffer;
21859
21860 obj = Qnil;
21861 *string = Qnil;
21862
21863 switch (c)
21864 {
21865 case '*':
21866 if (!NILP (BVAR (b, read_only)))
21867 return "%";
21868 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21869 return "*";
21870 return "-";
21871
21872 case '+':
21873 /* This differs from %* only for a modified read-only buffer. */
21874 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21875 return "*";
21876 if (!NILP (BVAR (b, read_only)))
21877 return "%";
21878 return "-";
21879
21880 case '&':
21881 /* This differs from %* in ignoring read-only-ness. */
21882 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21883 return "*";
21884 return "-";
21885
21886 case '%':
21887 return "%";
21888
21889 case '[':
21890 {
21891 int i;
21892 char *p;
21893
21894 if (command_loop_level > 5)
21895 return "[[[... ";
21896 p = decode_mode_spec_buf;
21897 for (i = 0; i < command_loop_level; i++)
21898 *p++ = '[';
21899 *p = 0;
21900 return decode_mode_spec_buf;
21901 }
21902
21903 case ']':
21904 {
21905 int i;
21906 char *p;
21907
21908 if (command_loop_level > 5)
21909 return " ...]]]";
21910 p = decode_mode_spec_buf;
21911 for (i = 0; i < command_loop_level; i++)
21912 *p++ = ']';
21913 *p = 0;
21914 return decode_mode_spec_buf;
21915 }
21916
21917 case '-':
21918 {
21919 register int i;
21920
21921 /* Let lots_of_dashes be a string of infinite length. */
21922 if (mode_line_target == MODE_LINE_NOPROP
21923 || mode_line_target == MODE_LINE_STRING)
21924 return "--";
21925 if (field_width <= 0
21926 || field_width > sizeof (lots_of_dashes))
21927 {
21928 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21929 decode_mode_spec_buf[i] = '-';
21930 decode_mode_spec_buf[i] = '\0';
21931 return decode_mode_spec_buf;
21932 }
21933 else
21934 return lots_of_dashes;
21935 }
21936
21937 case 'b':
21938 obj = BVAR (b, name);
21939 break;
21940
21941 case 'c':
21942 /* %c and %l are ignored in `frame-title-format'.
21943 (In redisplay_internal, the frame title is drawn _before_ the
21944 windows are updated, so the stuff which depends on actual
21945 window contents (such as %l) may fail to render properly, or
21946 even crash emacs.) */
21947 if (mode_line_target == MODE_LINE_TITLE)
21948 return "";
21949 else
21950 {
21951 ptrdiff_t col = current_column ();
21952 w->column_number_displayed = col;
21953 pint2str (decode_mode_spec_buf, width, col);
21954 return decode_mode_spec_buf;
21955 }
21956
21957 case 'e':
21958 #ifndef SYSTEM_MALLOC
21959 {
21960 if (NILP (Vmemory_full))
21961 return "";
21962 else
21963 return "!MEM FULL! ";
21964 }
21965 #else
21966 return "";
21967 #endif
21968
21969 case 'F':
21970 /* %F displays the frame name. */
21971 if (!NILP (f->title))
21972 return SSDATA (f->title);
21973 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21974 return SSDATA (f->name);
21975 return "Emacs";
21976
21977 case 'f':
21978 obj = BVAR (b, filename);
21979 break;
21980
21981 case 'i':
21982 {
21983 ptrdiff_t size = ZV - BEGV;
21984 pint2str (decode_mode_spec_buf, width, size);
21985 return decode_mode_spec_buf;
21986 }
21987
21988 case 'I':
21989 {
21990 ptrdiff_t size = ZV - BEGV;
21991 pint2hrstr (decode_mode_spec_buf, width, size);
21992 return decode_mode_spec_buf;
21993 }
21994
21995 case 'l':
21996 {
21997 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21998 ptrdiff_t topline, nlines, height;
21999 ptrdiff_t junk;
22000
22001 /* %c and %l are ignored in `frame-title-format'. */
22002 if (mode_line_target == MODE_LINE_TITLE)
22003 return "";
22004
22005 startpos = marker_position (w->start);
22006 startpos_byte = marker_byte_position (w->start);
22007 height = WINDOW_TOTAL_LINES (w);
22008
22009 /* If we decided that this buffer isn't suitable for line numbers,
22010 don't forget that too fast. */
22011 if (w->base_line_pos == -1)
22012 goto no_value;
22013
22014 /* If the buffer is very big, don't waste time. */
22015 if (INTEGERP (Vline_number_display_limit)
22016 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22017 {
22018 w->base_line_pos = 0;
22019 w->base_line_number = 0;
22020 goto no_value;
22021 }
22022
22023 if (w->base_line_number > 0
22024 && w->base_line_pos > 0
22025 && w->base_line_pos <= startpos)
22026 {
22027 line = w->base_line_number;
22028 linepos = w->base_line_pos;
22029 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22030 }
22031 else
22032 {
22033 line = 1;
22034 linepos = BUF_BEGV (b);
22035 linepos_byte = BUF_BEGV_BYTE (b);
22036 }
22037
22038 /* Count lines from base line to window start position. */
22039 nlines = display_count_lines (linepos_byte,
22040 startpos_byte,
22041 startpos, &junk);
22042
22043 topline = nlines + line;
22044
22045 /* Determine a new base line, if the old one is too close
22046 or too far away, or if we did not have one.
22047 "Too close" means it's plausible a scroll-down would
22048 go back past it. */
22049 if (startpos == BUF_BEGV (b))
22050 {
22051 w->base_line_number = topline;
22052 w->base_line_pos = BUF_BEGV (b);
22053 }
22054 else if (nlines < height + 25 || nlines > height * 3 + 50
22055 || linepos == BUF_BEGV (b))
22056 {
22057 ptrdiff_t limit = BUF_BEGV (b);
22058 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22059 ptrdiff_t position;
22060 ptrdiff_t distance =
22061 (height * 2 + 30) * line_number_display_limit_width;
22062
22063 if (startpos - distance > limit)
22064 {
22065 limit = startpos - distance;
22066 limit_byte = CHAR_TO_BYTE (limit);
22067 }
22068
22069 nlines = display_count_lines (startpos_byte,
22070 limit_byte,
22071 - (height * 2 + 30),
22072 &position);
22073 /* If we couldn't find the lines we wanted within
22074 line_number_display_limit_width chars per line,
22075 give up on line numbers for this window. */
22076 if (position == limit_byte && limit == startpos - distance)
22077 {
22078 w->base_line_pos = -1;
22079 w->base_line_number = 0;
22080 goto no_value;
22081 }
22082
22083 w->base_line_number = topline - nlines;
22084 w->base_line_pos = BYTE_TO_CHAR (position);
22085 }
22086
22087 /* Now count lines from the start pos to point. */
22088 nlines = display_count_lines (startpos_byte,
22089 PT_BYTE, PT, &junk);
22090
22091 /* Record that we did display the line number. */
22092 line_number_displayed = 1;
22093
22094 /* Make the string to show. */
22095 pint2str (decode_mode_spec_buf, width, topline + nlines);
22096 return decode_mode_spec_buf;
22097 no_value:
22098 {
22099 char* p = decode_mode_spec_buf;
22100 int pad = width - 2;
22101 while (pad-- > 0)
22102 *p++ = ' ';
22103 *p++ = '?';
22104 *p++ = '?';
22105 *p = '\0';
22106 return decode_mode_spec_buf;
22107 }
22108 }
22109 break;
22110
22111 case 'm':
22112 obj = BVAR (b, mode_name);
22113 break;
22114
22115 case 'n':
22116 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22117 return " Narrow";
22118 break;
22119
22120 case 'p':
22121 {
22122 ptrdiff_t pos = marker_position (w->start);
22123 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22124
22125 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22126 {
22127 if (pos <= BUF_BEGV (b))
22128 return "All";
22129 else
22130 return "Bottom";
22131 }
22132 else if (pos <= BUF_BEGV (b))
22133 return "Top";
22134 else
22135 {
22136 if (total > 1000000)
22137 /* Do it differently for a large value, to avoid overflow. */
22138 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22139 else
22140 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22141 /* We can't normally display a 3-digit number,
22142 so get us a 2-digit number that is close. */
22143 if (total == 100)
22144 total = 99;
22145 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22146 return decode_mode_spec_buf;
22147 }
22148 }
22149
22150 /* Display percentage of size above the bottom of the screen. */
22151 case 'P':
22152 {
22153 ptrdiff_t toppos = marker_position (w->start);
22154 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22155 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22156
22157 if (botpos >= BUF_ZV (b))
22158 {
22159 if (toppos <= BUF_BEGV (b))
22160 return "All";
22161 else
22162 return "Bottom";
22163 }
22164 else
22165 {
22166 if (total > 1000000)
22167 /* Do it differently for a large value, to avoid overflow. */
22168 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22169 else
22170 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22171 /* We can't normally display a 3-digit number,
22172 so get us a 2-digit number that is close. */
22173 if (total == 100)
22174 total = 99;
22175 if (toppos <= BUF_BEGV (b))
22176 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22177 else
22178 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22179 return decode_mode_spec_buf;
22180 }
22181 }
22182
22183 case 's':
22184 /* status of process */
22185 obj = Fget_buffer_process (Fcurrent_buffer ());
22186 if (NILP (obj))
22187 return "no process";
22188 #ifndef MSDOS
22189 obj = Fsymbol_name (Fprocess_status (obj));
22190 #endif
22191 break;
22192
22193 case '@':
22194 {
22195 ptrdiff_t count = inhibit_garbage_collection ();
22196 Lisp_Object val = call1 (intern ("file-remote-p"),
22197 BVAR (current_buffer, directory));
22198 unbind_to (count, Qnil);
22199
22200 if (NILP (val))
22201 return "-";
22202 else
22203 return "@";
22204 }
22205
22206 case 'z':
22207 /* coding-system (not including end-of-line format) */
22208 case 'Z':
22209 /* coding-system (including end-of-line type) */
22210 {
22211 int eol_flag = (c == 'Z');
22212 char *p = decode_mode_spec_buf;
22213
22214 if (! FRAME_WINDOW_P (f))
22215 {
22216 /* No need to mention EOL here--the terminal never needs
22217 to do EOL conversion. */
22218 p = decode_mode_spec_coding (CODING_ID_NAME
22219 (FRAME_KEYBOARD_CODING (f)->id),
22220 p, 0);
22221 p = decode_mode_spec_coding (CODING_ID_NAME
22222 (FRAME_TERMINAL_CODING (f)->id),
22223 p, 0);
22224 }
22225 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22226 p, eol_flag);
22227
22228 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22229 #ifdef subprocesses
22230 obj = Fget_buffer_process (Fcurrent_buffer ());
22231 if (PROCESSP (obj))
22232 {
22233 p = decode_mode_spec_coding
22234 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22235 p = decode_mode_spec_coding
22236 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22237 }
22238 #endif /* subprocesses */
22239 #endif /* 0 */
22240 *p = 0;
22241 return decode_mode_spec_buf;
22242 }
22243 }
22244
22245 if (STRINGP (obj))
22246 {
22247 *string = obj;
22248 return SSDATA (obj);
22249 }
22250 else
22251 return "";
22252 }
22253
22254
22255 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22256 means count lines back from START_BYTE. But don't go beyond
22257 LIMIT_BYTE. Return the number of lines thus found (always
22258 nonnegative).
22259
22260 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22261 either the position COUNT lines after/before START_BYTE, if we
22262 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22263 COUNT lines. */
22264
22265 static ptrdiff_t
22266 display_count_lines (ptrdiff_t start_byte,
22267 ptrdiff_t limit_byte, ptrdiff_t count,
22268 ptrdiff_t *byte_pos_ptr)
22269 {
22270 register unsigned char *cursor;
22271 unsigned char *base;
22272
22273 register ptrdiff_t ceiling;
22274 register unsigned char *ceiling_addr;
22275 ptrdiff_t orig_count = count;
22276
22277 /* If we are not in selective display mode,
22278 check only for newlines. */
22279 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22280 && !INTEGERP (BVAR (current_buffer, selective_display)));
22281
22282 if (count > 0)
22283 {
22284 while (start_byte < limit_byte)
22285 {
22286 ceiling = BUFFER_CEILING_OF (start_byte);
22287 ceiling = min (limit_byte - 1, ceiling);
22288 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22289 base = (cursor = BYTE_POS_ADDR (start_byte));
22290
22291 do
22292 {
22293 if (selective_display)
22294 {
22295 while (*cursor != '\n' && *cursor != 015
22296 && ++cursor != ceiling_addr)
22297 continue;
22298 if (cursor == ceiling_addr)
22299 break;
22300 }
22301 else
22302 {
22303 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22304 if (! cursor)
22305 break;
22306 }
22307
22308 cursor++;
22309
22310 if (--count == 0)
22311 {
22312 start_byte += cursor - base;
22313 *byte_pos_ptr = start_byte;
22314 return orig_count;
22315 }
22316 }
22317 while (cursor < ceiling_addr);
22318
22319 start_byte += ceiling_addr - base;
22320 }
22321 }
22322 else
22323 {
22324 while (start_byte > limit_byte)
22325 {
22326 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22327 ceiling = max (limit_byte, ceiling);
22328 ceiling_addr = BYTE_POS_ADDR (ceiling);
22329 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22330 while (1)
22331 {
22332 if (selective_display)
22333 {
22334 while (--cursor >= ceiling_addr
22335 && *cursor != '\n' && *cursor != 015)
22336 continue;
22337 if (cursor < ceiling_addr)
22338 break;
22339 }
22340 else
22341 {
22342 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22343 if (! cursor)
22344 break;
22345 }
22346
22347 if (++count == 0)
22348 {
22349 start_byte += cursor - base + 1;
22350 *byte_pos_ptr = start_byte;
22351 /* When scanning backwards, we should
22352 not count the newline posterior to which we stop. */
22353 return - orig_count - 1;
22354 }
22355 }
22356 start_byte += ceiling_addr - base;
22357 }
22358 }
22359
22360 *byte_pos_ptr = limit_byte;
22361
22362 if (count < 0)
22363 return - orig_count + count;
22364 return orig_count - count;
22365
22366 }
22367
22368
22369 \f
22370 /***********************************************************************
22371 Displaying strings
22372 ***********************************************************************/
22373
22374 /* Display a NUL-terminated string, starting with index START.
22375
22376 If STRING is non-null, display that C string. Otherwise, the Lisp
22377 string LISP_STRING is displayed. There's a case that STRING is
22378 non-null and LISP_STRING is not nil. It means STRING is a string
22379 data of LISP_STRING. In that case, we display LISP_STRING while
22380 ignoring its text properties.
22381
22382 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22383 FACE_STRING. Display STRING or LISP_STRING with the face at
22384 FACE_STRING_POS in FACE_STRING:
22385
22386 Display the string in the environment given by IT, but use the
22387 standard display table, temporarily.
22388
22389 FIELD_WIDTH is the minimum number of output glyphs to produce.
22390 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22391 with spaces. If STRING has more characters, more than FIELD_WIDTH
22392 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22393
22394 PRECISION is the maximum number of characters to output from
22395 STRING. PRECISION < 0 means don't truncate the string.
22396
22397 This is roughly equivalent to printf format specifiers:
22398
22399 FIELD_WIDTH PRECISION PRINTF
22400 ----------------------------------------
22401 -1 -1 %s
22402 -1 10 %.10s
22403 10 -1 %10s
22404 20 10 %20.10s
22405
22406 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22407 display them, and < 0 means obey the current buffer's value of
22408 enable_multibyte_characters.
22409
22410 Value is the number of columns displayed. */
22411
22412 static int
22413 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22414 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22415 int field_width, int precision, int max_x, int multibyte)
22416 {
22417 int hpos_at_start = it->hpos;
22418 int saved_face_id = it->face_id;
22419 struct glyph_row *row = it->glyph_row;
22420 ptrdiff_t it_charpos;
22421
22422 /* Initialize the iterator IT for iteration over STRING beginning
22423 with index START. */
22424 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22425 precision, field_width, multibyte);
22426 if (string && STRINGP (lisp_string))
22427 /* LISP_STRING is the one returned by decode_mode_spec. We should
22428 ignore its text properties. */
22429 it->stop_charpos = it->end_charpos;
22430
22431 /* If displaying STRING, set up the face of the iterator from
22432 FACE_STRING, if that's given. */
22433 if (STRINGP (face_string))
22434 {
22435 ptrdiff_t endptr;
22436 struct face *face;
22437
22438 it->face_id
22439 = face_at_string_position (it->w, face_string, face_string_pos,
22440 0, it->region_beg_charpos,
22441 it->region_end_charpos,
22442 &endptr, it->base_face_id, 0);
22443 face = FACE_FROM_ID (it->f, it->face_id);
22444 it->face_box_p = face->box != FACE_NO_BOX;
22445 }
22446
22447 /* Set max_x to the maximum allowed X position. Don't let it go
22448 beyond the right edge of the window. */
22449 if (max_x <= 0)
22450 max_x = it->last_visible_x;
22451 else
22452 max_x = min (max_x, it->last_visible_x);
22453
22454 /* Skip over display elements that are not visible. because IT->w is
22455 hscrolled. */
22456 if (it->current_x < it->first_visible_x)
22457 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22458 MOVE_TO_POS | MOVE_TO_X);
22459
22460 row->ascent = it->max_ascent;
22461 row->height = it->max_ascent + it->max_descent;
22462 row->phys_ascent = it->max_phys_ascent;
22463 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22464 row->extra_line_spacing = it->max_extra_line_spacing;
22465
22466 if (STRINGP (it->string))
22467 it_charpos = IT_STRING_CHARPOS (*it);
22468 else
22469 it_charpos = IT_CHARPOS (*it);
22470
22471 /* This condition is for the case that we are called with current_x
22472 past last_visible_x. */
22473 while (it->current_x < max_x)
22474 {
22475 int x_before, x, n_glyphs_before, i, nglyphs;
22476
22477 /* Get the next display element. */
22478 if (!get_next_display_element (it))
22479 break;
22480
22481 /* Produce glyphs. */
22482 x_before = it->current_x;
22483 n_glyphs_before = row->used[TEXT_AREA];
22484 PRODUCE_GLYPHS (it);
22485
22486 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22487 i = 0;
22488 x = x_before;
22489 while (i < nglyphs)
22490 {
22491 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22492
22493 if (it->line_wrap != TRUNCATE
22494 && x + glyph->pixel_width > max_x)
22495 {
22496 /* End of continued line or max_x reached. */
22497 if (CHAR_GLYPH_PADDING_P (*glyph))
22498 {
22499 /* A wide character is unbreakable. */
22500 if (row->reversed_p)
22501 unproduce_glyphs (it, row->used[TEXT_AREA]
22502 - n_glyphs_before);
22503 row->used[TEXT_AREA] = n_glyphs_before;
22504 it->current_x = x_before;
22505 }
22506 else
22507 {
22508 if (row->reversed_p)
22509 unproduce_glyphs (it, row->used[TEXT_AREA]
22510 - (n_glyphs_before + i));
22511 row->used[TEXT_AREA] = n_glyphs_before + i;
22512 it->current_x = x;
22513 }
22514 break;
22515 }
22516 else if (x + glyph->pixel_width >= it->first_visible_x)
22517 {
22518 /* Glyph is at least partially visible. */
22519 ++it->hpos;
22520 if (x < it->first_visible_x)
22521 row->x = x - it->first_visible_x;
22522 }
22523 else
22524 {
22525 /* Glyph is off the left margin of the display area.
22526 Should not happen. */
22527 emacs_abort ();
22528 }
22529
22530 row->ascent = max (row->ascent, it->max_ascent);
22531 row->height = max (row->height, it->max_ascent + it->max_descent);
22532 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22533 row->phys_height = max (row->phys_height,
22534 it->max_phys_ascent + it->max_phys_descent);
22535 row->extra_line_spacing = max (row->extra_line_spacing,
22536 it->max_extra_line_spacing);
22537 x += glyph->pixel_width;
22538 ++i;
22539 }
22540
22541 /* Stop if max_x reached. */
22542 if (i < nglyphs)
22543 break;
22544
22545 /* Stop at line ends. */
22546 if (ITERATOR_AT_END_OF_LINE_P (it))
22547 {
22548 it->continuation_lines_width = 0;
22549 break;
22550 }
22551
22552 set_iterator_to_next (it, 1);
22553 if (STRINGP (it->string))
22554 it_charpos = IT_STRING_CHARPOS (*it);
22555 else
22556 it_charpos = IT_CHARPOS (*it);
22557
22558 /* Stop if truncating at the right edge. */
22559 if (it->line_wrap == TRUNCATE
22560 && it->current_x >= it->last_visible_x)
22561 {
22562 /* Add truncation mark, but don't do it if the line is
22563 truncated at a padding space. */
22564 if (it_charpos < it->string_nchars)
22565 {
22566 if (!FRAME_WINDOW_P (it->f))
22567 {
22568 int ii, n;
22569
22570 if (it->current_x > it->last_visible_x)
22571 {
22572 if (!row->reversed_p)
22573 {
22574 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22575 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22576 break;
22577 }
22578 else
22579 {
22580 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22581 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22582 break;
22583 unproduce_glyphs (it, ii + 1);
22584 ii = row->used[TEXT_AREA] - (ii + 1);
22585 }
22586 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22587 {
22588 row->used[TEXT_AREA] = ii;
22589 produce_special_glyphs (it, IT_TRUNCATION);
22590 }
22591 }
22592 produce_special_glyphs (it, IT_TRUNCATION);
22593 }
22594 row->truncated_on_right_p = 1;
22595 }
22596 break;
22597 }
22598 }
22599
22600 /* Maybe insert a truncation at the left. */
22601 if (it->first_visible_x
22602 && it_charpos > 0)
22603 {
22604 if (!FRAME_WINDOW_P (it->f)
22605 || (row->reversed_p
22606 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22607 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22608 insert_left_trunc_glyphs (it);
22609 row->truncated_on_left_p = 1;
22610 }
22611
22612 it->face_id = saved_face_id;
22613
22614 /* Value is number of columns displayed. */
22615 return it->hpos - hpos_at_start;
22616 }
22617
22618
22619 \f
22620 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22621 appears as an element of LIST or as the car of an element of LIST.
22622 If PROPVAL is a list, compare each element against LIST in that
22623 way, and return 1/2 if any element of PROPVAL is found in LIST.
22624 Otherwise return 0. This function cannot quit.
22625 The return value is 2 if the text is invisible but with an ellipsis
22626 and 1 if it's invisible and without an ellipsis. */
22627
22628 int
22629 invisible_p (register Lisp_Object propval, Lisp_Object list)
22630 {
22631 register Lisp_Object tail, proptail;
22632
22633 for (tail = list; CONSP (tail); tail = XCDR (tail))
22634 {
22635 register Lisp_Object tem;
22636 tem = XCAR (tail);
22637 if (EQ (propval, tem))
22638 return 1;
22639 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22640 return NILP (XCDR (tem)) ? 1 : 2;
22641 }
22642
22643 if (CONSP (propval))
22644 {
22645 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22646 {
22647 Lisp_Object propelt;
22648 propelt = XCAR (proptail);
22649 for (tail = list; CONSP (tail); tail = XCDR (tail))
22650 {
22651 register Lisp_Object tem;
22652 tem = XCAR (tail);
22653 if (EQ (propelt, tem))
22654 return 1;
22655 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22656 return NILP (XCDR (tem)) ? 1 : 2;
22657 }
22658 }
22659 }
22660
22661 return 0;
22662 }
22663
22664 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22665 doc: /* Non-nil if the property makes the text invisible.
22666 POS-OR-PROP can be a marker or number, in which case it is taken to be
22667 a position in the current buffer and the value of the `invisible' property
22668 is checked; or it can be some other value, which is then presumed to be the
22669 value of the `invisible' property of the text of interest.
22670 The non-nil value returned can be t for truly invisible text or something
22671 else if the text is replaced by an ellipsis. */)
22672 (Lisp_Object pos_or_prop)
22673 {
22674 Lisp_Object prop
22675 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22676 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22677 : pos_or_prop);
22678 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22679 return (invis == 0 ? Qnil
22680 : invis == 1 ? Qt
22681 : make_number (invis));
22682 }
22683
22684 /* Calculate a width or height in pixels from a specification using
22685 the following elements:
22686
22687 SPEC ::=
22688 NUM - a (fractional) multiple of the default font width/height
22689 (NUM) - specifies exactly NUM pixels
22690 UNIT - a fixed number of pixels, see below.
22691 ELEMENT - size of a display element in pixels, see below.
22692 (NUM . SPEC) - equals NUM * SPEC
22693 (+ SPEC SPEC ...) - add pixel values
22694 (- SPEC SPEC ...) - subtract pixel values
22695 (- SPEC) - negate pixel value
22696
22697 NUM ::=
22698 INT or FLOAT - a number constant
22699 SYMBOL - use symbol's (buffer local) variable binding.
22700
22701 UNIT ::=
22702 in - pixels per inch *)
22703 mm - pixels per 1/1000 meter *)
22704 cm - pixels per 1/100 meter *)
22705 width - width of current font in pixels.
22706 height - height of current font in pixels.
22707
22708 *) using the ratio(s) defined in display-pixels-per-inch.
22709
22710 ELEMENT ::=
22711
22712 left-fringe - left fringe width in pixels
22713 right-fringe - right fringe width in pixels
22714
22715 left-margin - left margin width in pixels
22716 right-margin - right margin width in pixels
22717
22718 scroll-bar - scroll-bar area width in pixels
22719
22720 Examples:
22721
22722 Pixels corresponding to 5 inches:
22723 (5 . in)
22724
22725 Total width of non-text areas on left side of window (if scroll-bar is on left):
22726 '(space :width (+ left-fringe left-margin scroll-bar))
22727
22728 Align to first text column (in header line):
22729 '(space :align-to 0)
22730
22731 Align to middle of text area minus half the width of variable `my-image'
22732 containing a loaded image:
22733 '(space :align-to (0.5 . (- text my-image)))
22734
22735 Width of left margin minus width of 1 character in the default font:
22736 '(space :width (- left-margin 1))
22737
22738 Width of left margin minus width of 2 characters in the current font:
22739 '(space :width (- left-margin (2 . width)))
22740
22741 Center 1 character over left-margin (in header line):
22742 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22743
22744 Different ways to express width of left fringe plus left margin minus one pixel:
22745 '(space :width (- (+ left-fringe left-margin) (1)))
22746 '(space :width (+ left-fringe left-margin (- (1))))
22747 '(space :width (+ left-fringe left-margin (-1)))
22748
22749 */
22750
22751 static int
22752 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22753 struct font *font, int width_p, int *align_to)
22754 {
22755 double pixels;
22756
22757 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22758 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22759
22760 if (NILP (prop))
22761 return OK_PIXELS (0);
22762
22763 eassert (FRAME_LIVE_P (it->f));
22764
22765 if (SYMBOLP (prop))
22766 {
22767 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22768 {
22769 char *unit = SSDATA (SYMBOL_NAME (prop));
22770
22771 if (unit[0] == 'i' && unit[1] == 'n')
22772 pixels = 1.0;
22773 else if (unit[0] == 'm' && unit[1] == 'm')
22774 pixels = 25.4;
22775 else if (unit[0] == 'c' && unit[1] == 'm')
22776 pixels = 2.54;
22777 else
22778 pixels = 0;
22779 if (pixels > 0)
22780 {
22781 double ppi = (width_p ? FRAME_RES_X (it->f)
22782 : FRAME_RES_Y (it->f));
22783
22784 if (ppi > 0)
22785 return OK_PIXELS (ppi / pixels);
22786 return 0;
22787 }
22788 }
22789
22790 #ifdef HAVE_WINDOW_SYSTEM
22791 if (EQ (prop, Qheight))
22792 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22793 if (EQ (prop, Qwidth))
22794 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22795 #else
22796 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22797 return OK_PIXELS (1);
22798 #endif
22799
22800 if (EQ (prop, Qtext))
22801 return OK_PIXELS (width_p
22802 ? window_box_width (it->w, TEXT_AREA)
22803 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22804
22805 if (align_to && *align_to < 0)
22806 {
22807 *res = 0;
22808 if (EQ (prop, Qleft))
22809 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22810 if (EQ (prop, Qright))
22811 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22812 if (EQ (prop, Qcenter))
22813 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22814 + window_box_width (it->w, TEXT_AREA) / 2);
22815 if (EQ (prop, Qleft_fringe))
22816 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22817 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22818 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22819 if (EQ (prop, Qright_fringe))
22820 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22821 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22822 : window_box_right_offset (it->w, TEXT_AREA));
22823 if (EQ (prop, Qleft_margin))
22824 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22825 if (EQ (prop, Qright_margin))
22826 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22827 if (EQ (prop, Qscroll_bar))
22828 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22829 ? 0
22830 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22831 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22832 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22833 : 0)));
22834 }
22835 else
22836 {
22837 if (EQ (prop, Qleft_fringe))
22838 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22839 if (EQ (prop, Qright_fringe))
22840 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22841 if (EQ (prop, Qleft_margin))
22842 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22843 if (EQ (prop, Qright_margin))
22844 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22845 if (EQ (prop, Qscroll_bar))
22846 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22847 }
22848
22849 prop = buffer_local_value_1 (prop, it->w->contents);
22850 if (EQ (prop, Qunbound))
22851 prop = Qnil;
22852 }
22853
22854 if (INTEGERP (prop) || FLOATP (prop))
22855 {
22856 int base_unit = (width_p
22857 ? FRAME_COLUMN_WIDTH (it->f)
22858 : FRAME_LINE_HEIGHT (it->f));
22859 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22860 }
22861
22862 if (CONSP (prop))
22863 {
22864 Lisp_Object car = XCAR (prop);
22865 Lisp_Object cdr = XCDR (prop);
22866
22867 if (SYMBOLP (car))
22868 {
22869 #ifdef HAVE_WINDOW_SYSTEM
22870 if (FRAME_WINDOW_P (it->f)
22871 && valid_image_p (prop))
22872 {
22873 ptrdiff_t id = lookup_image (it->f, prop);
22874 struct image *img = IMAGE_FROM_ID (it->f, id);
22875
22876 return OK_PIXELS (width_p ? img->width : img->height);
22877 }
22878 #ifdef HAVE_XWIDGETS
22879 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
22880 {
22881 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
22882 return OK_PIXELS (width_p ? 100 : 100);
22883 }
22884 #endif
22885 #endif
22886 if (EQ (car, Qplus) || EQ (car, Qminus))
22887 {
22888 int first = 1;
22889 double px;
22890
22891 pixels = 0;
22892 while (CONSP (cdr))
22893 {
22894 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22895 font, width_p, align_to))
22896 return 0;
22897 if (first)
22898 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22899 else
22900 pixels += px;
22901 cdr = XCDR (cdr);
22902 }
22903 if (EQ (car, Qminus))
22904 pixels = -pixels;
22905 return OK_PIXELS (pixels);
22906 }
22907
22908 car = buffer_local_value_1 (car, it->w->contents);
22909 if (EQ (car, Qunbound))
22910 car = Qnil;
22911 }
22912
22913 if (INTEGERP (car) || FLOATP (car))
22914 {
22915 double fact;
22916 pixels = XFLOATINT (car);
22917 if (NILP (cdr))
22918 return OK_PIXELS (pixels);
22919 if (calc_pixel_width_or_height (&fact, it, cdr,
22920 font, width_p, align_to))
22921 return OK_PIXELS (pixels * fact);
22922 return 0;
22923 }
22924
22925 return 0;
22926 }
22927
22928 return 0;
22929 }
22930
22931 \f
22932 /***********************************************************************
22933 Glyph Display
22934 ***********************************************************************/
22935
22936 #ifdef HAVE_WINDOW_SYSTEM
22937
22938 #ifdef GLYPH_DEBUG
22939
22940 void
22941 dump_glyph_string (struct glyph_string *s)
22942 {
22943 fprintf (stderr, "glyph string\n");
22944 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22945 s->x, s->y, s->width, s->height);
22946 fprintf (stderr, " ybase = %d\n", s->ybase);
22947 fprintf (stderr, " hl = %d\n", s->hl);
22948 fprintf (stderr, " left overhang = %d, right = %d\n",
22949 s->left_overhang, s->right_overhang);
22950 fprintf (stderr, " nchars = %d\n", s->nchars);
22951 fprintf (stderr, " extends to end of line = %d\n",
22952 s->extends_to_end_of_line_p);
22953 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22954 fprintf (stderr, " bg width = %d\n", s->background_width);
22955 }
22956
22957 #endif /* GLYPH_DEBUG */
22958
22959 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22960 of XChar2b structures for S; it can't be allocated in
22961 init_glyph_string because it must be allocated via `alloca'. W
22962 is the window on which S is drawn. ROW and AREA are the glyph row
22963 and area within the row from which S is constructed. START is the
22964 index of the first glyph structure covered by S. HL is a
22965 face-override for drawing S. */
22966
22967 #ifdef HAVE_NTGUI
22968 #define OPTIONAL_HDC(hdc) HDC hdc,
22969 #define DECLARE_HDC(hdc) HDC hdc;
22970 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22971 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22972 #endif
22973
22974 #ifndef OPTIONAL_HDC
22975 #define OPTIONAL_HDC(hdc)
22976 #define DECLARE_HDC(hdc)
22977 #define ALLOCATE_HDC(hdc, f)
22978 #define RELEASE_HDC(hdc, f)
22979 #endif
22980
22981 static void
22982 init_glyph_string (struct glyph_string *s,
22983 OPTIONAL_HDC (hdc)
22984 XChar2b *char2b, struct window *w, struct glyph_row *row,
22985 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22986 {
22987 memset (s, 0, sizeof *s);
22988 s->w = w;
22989 s->f = XFRAME (w->frame);
22990 #ifdef HAVE_NTGUI
22991 s->hdc = hdc;
22992 #endif
22993 s->display = FRAME_X_DISPLAY (s->f);
22994 s->window = FRAME_X_WINDOW (s->f);
22995 s->char2b = char2b;
22996 s->hl = hl;
22997 s->row = row;
22998 s->area = area;
22999 s->first_glyph = row->glyphs[area] + start;
23000 s->height = row->height;
23001 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23002 s->ybase = s->y + row->ascent;
23003 }
23004
23005
23006 /* Append the list of glyph strings with head H and tail T to the list
23007 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23008
23009 static void
23010 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23011 struct glyph_string *h, struct glyph_string *t)
23012 {
23013 if (h)
23014 {
23015 if (*head)
23016 (*tail)->next = h;
23017 else
23018 *head = h;
23019 h->prev = *tail;
23020 *tail = t;
23021 }
23022 }
23023
23024
23025 /* Prepend the list of glyph strings with head H and tail T to the
23026 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23027 result. */
23028
23029 static void
23030 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23031 struct glyph_string *h, struct glyph_string *t)
23032 {
23033 if (h)
23034 {
23035 if (*head)
23036 (*head)->prev = t;
23037 else
23038 *tail = t;
23039 t->next = *head;
23040 *head = h;
23041 }
23042 }
23043
23044
23045 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23046 Set *HEAD and *TAIL to the resulting list. */
23047
23048 static void
23049 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23050 struct glyph_string *s)
23051 {
23052 s->next = s->prev = NULL;
23053 append_glyph_string_lists (head, tail, s, s);
23054 }
23055
23056
23057 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23058 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23059 make sure that X resources for the face returned are allocated.
23060 Value is a pointer to a realized face that is ready for display if
23061 DISPLAY_P is non-zero. */
23062
23063 static struct face *
23064 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23065 XChar2b *char2b, int display_p)
23066 {
23067 struct face *face = FACE_FROM_ID (f, face_id);
23068 unsigned code = 0;
23069
23070 if (face->font)
23071 {
23072 code = face->font->driver->encode_char (face->font, c);
23073
23074 if (code == FONT_INVALID_CODE)
23075 code = 0;
23076 }
23077 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23078
23079 /* Make sure X resources of the face are allocated. */
23080 #ifdef HAVE_X_WINDOWS
23081 if (display_p)
23082 #endif
23083 {
23084 eassert (face != NULL);
23085 PREPARE_FACE_FOR_DISPLAY (f, face);
23086 }
23087
23088 return face;
23089 }
23090
23091
23092 /* Get face and two-byte form of character glyph GLYPH on frame F.
23093 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23094 a pointer to a realized face that is ready for display. */
23095
23096 static struct face *
23097 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23098 XChar2b *char2b, int *two_byte_p)
23099 {
23100 struct face *face;
23101 unsigned code = 0;
23102
23103 eassert (glyph->type == CHAR_GLYPH);
23104 face = FACE_FROM_ID (f, glyph->face_id);
23105
23106 /* Make sure X resources of the face are allocated. */
23107 eassert (face != NULL);
23108 PREPARE_FACE_FOR_DISPLAY (f, face);
23109
23110 if (two_byte_p)
23111 *two_byte_p = 0;
23112
23113 if (face->font)
23114 {
23115 if (CHAR_BYTE8_P (glyph->u.ch))
23116 code = CHAR_TO_BYTE8 (glyph->u.ch);
23117 else
23118 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23119
23120 if (code == FONT_INVALID_CODE)
23121 code = 0;
23122 }
23123
23124 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23125 return face;
23126 }
23127
23128
23129 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23130 Return 1 if FONT has a glyph for C, otherwise return 0. */
23131
23132 static int
23133 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23134 {
23135 unsigned code;
23136
23137 if (CHAR_BYTE8_P (c))
23138 code = CHAR_TO_BYTE8 (c);
23139 else
23140 code = font->driver->encode_char (font, c);
23141
23142 if (code == FONT_INVALID_CODE)
23143 return 0;
23144 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23145 return 1;
23146 }
23147
23148
23149 /* Fill glyph string S with composition components specified by S->cmp.
23150
23151 BASE_FACE is the base face of the composition.
23152 S->cmp_from is the index of the first component for S.
23153
23154 OVERLAPS non-zero means S should draw the foreground only, and use
23155 its physical height for clipping. See also draw_glyphs.
23156
23157 Value is the index of a component not in S. */
23158
23159 static int
23160 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23161 int overlaps)
23162 {
23163 int i;
23164 /* For all glyphs of this composition, starting at the offset
23165 S->cmp_from, until we reach the end of the definition or encounter a
23166 glyph that requires the different face, add it to S. */
23167 struct face *face;
23168
23169 eassert (s);
23170
23171 s->for_overlaps = overlaps;
23172 s->face = NULL;
23173 s->font = NULL;
23174 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23175 {
23176 int c = COMPOSITION_GLYPH (s->cmp, i);
23177
23178 /* TAB in a composition means display glyphs with padding space
23179 on the left or right. */
23180 if (c != '\t')
23181 {
23182 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23183 -1, Qnil);
23184
23185 face = get_char_face_and_encoding (s->f, c, face_id,
23186 s->char2b + i, 1);
23187 if (face)
23188 {
23189 if (! s->face)
23190 {
23191 s->face = face;
23192 s->font = s->face->font;
23193 }
23194 else if (s->face != face)
23195 break;
23196 }
23197 }
23198 ++s->nchars;
23199 }
23200 s->cmp_to = i;
23201
23202 if (s->face == NULL)
23203 {
23204 s->face = base_face->ascii_face;
23205 s->font = s->face->font;
23206 }
23207
23208 /* All glyph strings for the same composition has the same width,
23209 i.e. the width set for the first component of the composition. */
23210 s->width = s->first_glyph->pixel_width;
23211
23212 /* If the specified font could not be loaded, use the frame's
23213 default font, but record the fact that we couldn't load it in
23214 the glyph string so that we can draw rectangles for the
23215 characters of the glyph string. */
23216 if (s->font == NULL)
23217 {
23218 s->font_not_found_p = 1;
23219 s->font = FRAME_FONT (s->f);
23220 }
23221
23222 /* Adjust base line for subscript/superscript text. */
23223 s->ybase += s->first_glyph->voffset;
23224
23225 /* This glyph string must always be drawn with 16-bit functions. */
23226 s->two_byte_p = 1;
23227
23228 return s->cmp_to;
23229 }
23230
23231 static int
23232 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23233 int start, int end, int overlaps)
23234 {
23235 struct glyph *glyph, *last;
23236 Lisp_Object lgstring;
23237 int i;
23238
23239 s->for_overlaps = overlaps;
23240 glyph = s->row->glyphs[s->area] + start;
23241 last = s->row->glyphs[s->area] + end;
23242 s->cmp_id = glyph->u.cmp.id;
23243 s->cmp_from = glyph->slice.cmp.from;
23244 s->cmp_to = glyph->slice.cmp.to + 1;
23245 s->face = FACE_FROM_ID (s->f, face_id);
23246 lgstring = composition_gstring_from_id (s->cmp_id);
23247 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23248 glyph++;
23249 while (glyph < last
23250 && glyph->u.cmp.automatic
23251 && glyph->u.cmp.id == s->cmp_id
23252 && s->cmp_to == glyph->slice.cmp.from)
23253 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23254
23255 for (i = s->cmp_from; i < s->cmp_to; i++)
23256 {
23257 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23258 unsigned code = LGLYPH_CODE (lglyph);
23259
23260 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23261 }
23262 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23263 return glyph - s->row->glyphs[s->area];
23264 }
23265
23266
23267 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23268 See the comment of fill_glyph_string for arguments.
23269 Value is the index of the first glyph not in S. */
23270
23271
23272 static int
23273 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23274 int start, int end, int overlaps)
23275 {
23276 struct glyph *glyph, *last;
23277 int voffset;
23278
23279 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23280 s->for_overlaps = overlaps;
23281 glyph = s->row->glyphs[s->area] + start;
23282 last = s->row->glyphs[s->area] + end;
23283 voffset = glyph->voffset;
23284 s->face = FACE_FROM_ID (s->f, face_id);
23285 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23286 s->nchars = 1;
23287 s->width = glyph->pixel_width;
23288 glyph++;
23289 while (glyph < last
23290 && glyph->type == GLYPHLESS_GLYPH
23291 && glyph->voffset == voffset
23292 && glyph->face_id == face_id)
23293 {
23294 s->nchars++;
23295 s->width += glyph->pixel_width;
23296 glyph++;
23297 }
23298 s->ybase += voffset;
23299 return glyph - s->row->glyphs[s->area];
23300 }
23301
23302
23303 /* Fill glyph string S from a sequence of character glyphs.
23304
23305 FACE_ID is the face id of the string. START is the index of the
23306 first glyph to consider, END is the index of the last + 1.
23307 OVERLAPS non-zero means S should draw the foreground only, and use
23308 its physical height for clipping. See also draw_glyphs.
23309
23310 Value is the index of the first glyph not in S. */
23311
23312 static int
23313 fill_glyph_string (struct glyph_string *s, int face_id,
23314 int start, int end, int overlaps)
23315 {
23316 struct glyph *glyph, *last;
23317 int voffset;
23318 int glyph_not_available_p;
23319
23320 eassert (s->f == XFRAME (s->w->frame));
23321 eassert (s->nchars == 0);
23322 eassert (start >= 0 && end > start);
23323
23324 s->for_overlaps = overlaps;
23325 glyph = s->row->glyphs[s->area] + start;
23326 last = s->row->glyphs[s->area] + end;
23327 voffset = glyph->voffset;
23328 s->padding_p = glyph->padding_p;
23329 glyph_not_available_p = glyph->glyph_not_available_p;
23330
23331 while (glyph < last
23332 && glyph->type == CHAR_GLYPH
23333 && glyph->voffset == voffset
23334 /* Same face id implies same font, nowadays. */
23335 && glyph->face_id == face_id
23336 && glyph->glyph_not_available_p == glyph_not_available_p)
23337 {
23338 int two_byte_p;
23339
23340 s->face = get_glyph_face_and_encoding (s->f, glyph,
23341 s->char2b + s->nchars,
23342 &two_byte_p);
23343 s->two_byte_p = two_byte_p;
23344 ++s->nchars;
23345 eassert (s->nchars <= end - start);
23346 s->width += glyph->pixel_width;
23347 if (glyph++->padding_p != s->padding_p)
23348 break;
23349 }
23350
23351 s->font = s->face->font;
23352
23353 /* If the specified font could not be loaded, use the frame's font,
23354 but record the fact that we couldn't load it in
23355 S->font_not_found_p so that we can draw rectangles for the
23356 characters of the glyph string. */
23357 if (s->font == NULL || glyph_not_available_p)
23358 {
23359 s->font_not_found_p = 1;
23360 s->font = FRAME_FONT (s->f);
23361 }
23362
23363 /* Adjust base line for subscript/superscript text. */
23364 s->ybase += voffset;
23365
23366 eassert (s->face && s->face->gc);
23367 return glyph - s->row->glyphs[s->area];
23368 }
23369
23370
23371 /* Fill glyph string S from image glyph S->first_glyph. */
23372
23373 static void
23374 fill_image_glyph_string (struct glyph_string *s)
23375 {
23376 eassert (s->first_glyph->type == IMAGE_GLYPH);
23377 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23378 eassert (s->img);
23379 s->slice = s->first_glyph->slice.img;
23380 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23381 s->font = s->face->font;
23382 s->width = s->first_glyph->pixel_width;
23383
23384 /* Adjust base line for subscript/superscript text. */
23385 s->ybase += s->first_glyph->voffset;
23386 }
23387
23388 #ifdef HAVE_XWIDGETS
23389 static void
23390 fill_xwidget_glyph_string (struct glyph_string *s)
23391 {
23392 eassert (s->first_glyph->type == XWIDGET_GLYPH);
23393 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
23394 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23395 s->font = s->face->font;
23396 s->width = s->first_glyph->pixel_width;
23397 s->ybase += s->first_glyph->voffset;
23398 s->xwidget = s->first_glyph->u.xwidget;
23399 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
23400 }
23401 #endif
23402 /* Fill glyph string S from a sequence of stretch glyphs.
23403
23404 START is the index of the first glyph to consider,
23405 END is the index of the last + 1.
23406
23407 Value is the index of the first glyph not in S. */
23408
23409 static int
23410 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23411 {
23412 struct glyph *glyph, *last;
23413 int voffset, face_id;
23414
23415 eassert (s->first_glyph->type == STRETCH_GLYPH);
23416
23417 glyph = s->row->glyphs[s->area] + start;
23418 last = s->row->glyphs[s->area] + end;
23419 face_id = glyph->face_id;
23420 s->face = FACE_FROM_ID (s->f, face_id);
23421 s->font = s->face->font;
23422 s->width = glyph->pixel_width;
23423 s->nchars = 1;
23424 voffset = glyph->voffset;
23425
23426 for (++glyph;
23427 (glyph < last
23428 && glyph->type == STRETCH_GLYPH
23429 && glyph->voffset == voffset
23430 && glyph->face_id == face_id);
23431 ++glyph)
23432 s->width += glyph->pixel_width;
23433
23434 /* Adjust base line for subscript/superscript text. */
23435 s->ybase += voffset;
23436
23437 /* The case that face->gc == 0 is handled when drawing the glyph
23438 string by calling PREPARE_FACE_FOR_DISPLAY. */
23439 eassert (s->face);
23440 return glyph - s->row->glyphs[s->area];
23441 }
23442
23443 static struct font_metrics *
23444 get_per_char_metric (struct font *font, XChar2b *char2b)
23445 {
23446 static struct font_metrics metrics;
23447 unsigned code;
23448
23449 if (! font)
23450 return NULL;
23451 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23452 if (code == FONT_INVALID_CODE)
23453 return NULL;
23454 font->driver->text_extents (font, &code, 1, &metrics);
23455 return &metrics;
23456 }
23457
23458 /* EXPORT for RIF:
23459 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23460 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23461 assumed to be zero. */
23462
23463 void
23464 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23465 {
23466 *left = *right = 0;
23467
23468 if (glyph->type == CHAR_GLYPH)
23469 {
23470 struct face *face;
23471 XChar2b char2b;
23472 struct font_metrics *pcm;
23473
23474 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23475 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23476 {
23477 if (pcm->rbearing > pcm->width)
23478 *right = pcm->rbearing - pcm->width;
23479 if (pcm->lbearing < 0)
23480 *left = -pcm->lbearing;
23481 }
23482 }
23483 else if (glyph->type == COMPOSITE_GLYPH)
23484 {
23485 if (! glyph->u.cmp.automatic)
23486 {
23487 struct composition *cmp = composition_table[glyph->u.cmp.id];
23488
23489 if (cmp->rbearing > cmp->pixel_width)
23490 *right = cmp->rbearing - cmp->pixel_width;
23491 if (cmp->lbearing < 0)
23492 *left = - cmp->lbearing;
23493 }
23494 else
23495 {
23496 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23497 struct font_metrics metrics;
23498
23499 composition_gstring_width (gstring, glyph->slice.cmp.from,
23500 glyph->slice.cmp.to + 1, &metrics);
23501 if (metrics.rbearing > metrics.width)
23502 *right = metrics.rbearing - metrics.width;
23503 if (metrics.lbearing < 0)
23504 *left = - metrics.lbearing;
23505 }
23506 }
23507 }
23508
23509
23510 /* Return the index of the first glyph preceding glyph string S that
23511 is overwritten by S because of S's left overhang. Value is -1
23512 if no glyphs are overwritten. */
23513
23514 static int
23515 left_overwritten (struct glyph_string *s)
23516 {
23517 int k;
23518
23519 if (s->left_overhang)
23520 {
23521 int x = 0, i;
23522 struct glyph *glyphs = s->row->glyphs[s->area];
23523 int first = s->first_glyph - glyphs;
23524
23525 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23526 x -= glyphs[i].pixel_width;
23527
23528 k = i + 1;
23529 }
23530 else
23531 k = -1;
23532
23533 return k;
23534 }
23535
23536
23537 /* Return the index of the first glyph preceding glyph string S that
23538 is overwriting S because of its right overhang. Value is -1 if no
23539 glyph in front of S overwrites S. */
23540
23541 static int
23542 left_overwriting (struct glyph_string *s)
23543 {
23544 int i, k, x;
23545 struct glyph *glyphs = s->row->glyphs[s->area];
23546 int first = s->first_glyph - glyphs;
23547
23548 k = -1;
23549 x = 0;
23550 for (i = first - 1; i >= 0; --i)
23551 {
23552 int left, right;
23553 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23554 if (x + right > 0)
23555 k = i;
23556 x -= glyphs[i].pixel_width;
23557 }
23558
23559 return k;
23560 }
23561
23562
23563 /* Return the index of the last glyph following glyph string S that is
23564 overwritten by S because of S's right overhang. Value is -1 if
23565 no such glyph is found. */
23566
23567 static int
23568 right_overwritten (struct glyph_string *s)
23569 {
23570 int k = -1;
23571
23572 if (s->right_overhang)
23573 {
23574 int x = 0, i;
23575 struct glyph *glyphs = s->row->glyphs[s->area];
23576 int first = (s->first_glyph - glyphs
23577 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23578 int end = s->row->used[s->area];
23579
23580 for (i = first; i < end && s->right_overhang > x; ++i)
23581 x += glyphs[i].pixel_width;
23582
23583 k = i;
23584 }
23585
23586 return k;
23587 }
23588
23589
23590 /* Return the index of the last glyph following glyph string S that
23591 overwrites S because of its left overhang. Value is negative
23592 if no such glyph is found. */
23593
23594 static int
23595 right_overwriting (struct glyph_string *s)
23596 {
23597 int i, k, x;
23598 int end = s->row->used[s->area];
23599 struct glyph *glyphs = s->row->glyphs[s->area];
23600 int first = (s->first_glyph - glyphs
23601 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23602
23603 k = -1;
23604 x = 0;
23605 for (i = first; i < end; ++i)
23606 {
23607 int left, right;
23608 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23609 if (x - left < 0)
23610 k = i;
23611 x += glyphs[i].pixel_width;
23612 }
23613
23614 return k;
23615 }
23616
23617
23618 /* Set background width of glyph string S. START is the index of the
23619 first glyph following S. LAST_X is the right-most x-position + 1
23620 in the drawing area. */
23621
23622 static void
23623 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23624 {
23625 /* If the face of this glyph string has to be drawn to the end of
23626 the drawing area, set S->extends_to_end_of_line_p. */
23627
23628 if (start == s->row->used[s->area]
23629 && s->area == TEXT_AREA
23630 && ((s->row->fill_line_p
23631 && (s->hl == DRAW_NORMAL_TEXT
23632 || s->hl == DRAW_IMAGE_RAISED
23633 || s->hl == DRAW_IMAGE_SUNKEN))
23634 || s->hl == DRAW_MOUSE_FACE))
23635 s->extends_to_end_of_line_p = 1;
23636
23637 /* If S extends its face to the end of the line, set its
23638 background_width to the distance to the right edge of the drawing
23639 area. */
23640 if (s->extends_to_end_of_line_p)
23641 s->background_width = last_x - s->x + 1;
23642 else
23643 s->background_width = s->width;
23644 }
23645
23646
23647 /* Compute overhangs and x-positions for glyph string S and its
23648 predecessors, or successors. X is the starting x-position for S.
23649 BACKWARD_P non-zero means process predecessors. */
23650
23651 static void
23652 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23653 {
23654 if (backward_p)
23655 {
23656 while (s)
23657 {
23658 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23659 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23660 x -= s->width;
23661 s->x = x;
23662 s = s->prev;
23663 }
23664 }
23665 else
23666 {
23667 while (s)
23668 {
23669 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23670 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23671 s->x = x;
23672 x += s->width;
23673 s = s->next;
23674 }
23675 }
23676 }
23677
23678
23679
23680 /* The following macros are only called from draw_glyphs below.
23681 They reference the following parameters of that function directly:
23682 `w', `row', `area', and `overlap_p'
23683 as well as the following local variables:
23684 `s', `f', and `hdc' (in W32) */
23685
23686 #ifdef HAVE_NTGUI
23687 /* On W32, silently add local `hdc' variable to argument list of
23688 init_glyph_string. */
23689 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23690 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23691 #else
23692 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23693 init_glyph_string (s, char2b, w, row, area, start, hl)
23694 #endif
23695
23696 /* Add a glyph string for a stretch glyph to the list of strings
23697 between HEAD and TAIL. START is the index of the stretch glyph in
23698 row area AREA of glyph row ROW. END is the index of the last glyph
23699 in that glyph row area. X is the current output position assigned
23700 to the new glyph string constructed. HL overrides that face of the
23701 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23702 is the right-most x-position of the drawing area. */
23703
23704 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23705 and below -- keep them on one line. */
23706 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23707 do \
23708 { \
23709 s = alloca (sizeof *s); \
23710 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23711 START = fill_stretch_glyph_string (s, START, END); \
23712 append_glyph_string (&HEAD, &TAIL, s); \
23713 s->x = (X); \
23714 } \
23715 while (0)
23716
23717
23718 /* Add a glyph string for an image glyph to the list of strings
23719 between HEAD and TAIL. START is the index of the image glyph in
23720 row area AREA of glyph row ROW. END is the index of the last glyph
23721 in that glyph row area. X is the current output position assigned
23722 to the new glyph string constructed. HL overrides that face of the
23723 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23724 is the right-most x-position of the drawing area. */
23725
23726 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23727 do \
23728 { \
23729 s = alloca (sizeof *s); \
23730 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23731 fill_image_glyph_string (s); \
23732 append_glyph_string (&HEAD, &TAIL, s); \
23733 ++START; \
23734 s->x = (X); \
23735 } \
23736 while (0)
23737
23738 #ifdef HAVE_XWIDGETS
23739 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23740 do \
23741 { \
23742 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
23743 s = (struct glyph_string *) alloca (sizeof *s); \
23744 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23745 fill_xwidget_glyph_string (s); \
23746 append_glyph_string (&HEAD, &TAIL, s); \
23747 ++START; \
23748 s->x = (X); \
23749 } \
23750 while (0)
23751 #endif
23752
23753
23754 /* Add a glyph string for a sequence of character glyphs to the list
23755 of strings between HEAD and TAIL. START is the index of the first
23756 glyph in row area AREA of glyph row ROW that is part of the new
23757 glyph string. END is the index of the last glyph in that glyph row
23758 area. X is the current output position assigned to the new glyph
23759 string constructed. HL overrides that face of the glyph; e.g. it
23760 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23761 right-most x-position of the drawing area. */
23762
23763 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23764 do \
23765 { \
23766 int face_id; \
23767 XChar2b *char2b; \
23768 \
23769 face_id = (row)->glyphs[area][START].face_id; \
23770 \
23771 s = alloca (sizeof *s); \
23772 char2b = alloca ((END - START) * sizeof *char2b); \
23773 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23774 append_glyph_string (&HEAD, &TAIL, s); \
23775 s->x = (X); \
23776 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23777 } \
23778 while (0)
23779
23780
23781 /* Add a glyph string for a composite sequence to the list of strings
23782 between HEAD and TAIL. START is the index of the first glyph in
23783 row area AREA of glyph row ROW that is part of the new glyph
23784 string. END is the index of the last glyph in that glyph row area.
23785 X is the current output position assigned to the new glyph string
23786 constructed. HL overrides that face of the glyph; e.g. it is
23787 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23788 x-position of the drawing area. */
23789
23790 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23791 do { \
23792 int face_id = (row)->glyphs[area][START].face_id; \
23793 struct face *base_face = FACE_FROM_ID (f, face_id); \
23794 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23795 struct composition *cmp = composition_table[cmp_id]; \
23796 XChar2b *char2b; \
23797 struct glyph_string *first_s = NULL; \
23798 int n; \
23799 \
23800 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23801 \
23802 /* Make glyph_strings for each glyph sequence that is drawable by \
23803 the same face, and append them to HEAD/TAIL. */ \
23804 for (n = 0; n < cmp->glyph_len;) \
23805 { \
23806 s = alloca (sizeof *s); \
23807 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23808 append_glyph_string (&(HEAD), &(TAIL), s); \
23809 s->cmp = cmp; \
23810 s->cmp_from = n; \
23811 s->x = (X); \
23812 if (n == 0) \
23813 first_s = s; \
23814 n = fill_composite_glyph_string (s, base_face, overlaps); \
23815 } \
23816 \
23817 ++START; \
23818 s = first_s; \
23819 } while (0)
23820
23821
23822 /* Add a glyph string for a glyph-string sequence to the list of strings
23823 between HEAD and TAIL. */
23824
23825 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23826 do { \
23827 int face_id; \
23828 XChar2b *char2b; \
23829 Lisp_Object gstring; \
23830 \
23831 face_id = (row)->glyphs[area][START].face_id; \
23832 gstring = (composition_gstring_from_id \
23833 ((row)->glyphs[area][START].u.cmp.id)); \
23834 s = alloca (sizeof *s); \
23835 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23836 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23837 append_glyph_string (&(HEAD), &(TAIL), s); \
23838 s->x = (X); \
23839 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23840 } while (0)
23841
23842
23843 /* Add a glyph string for a sequence of glyphless character's glyphs
23844 to the list of strings between HEAD and TAIL. The meanings of
23845 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23846
23847 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23848 do \
23849 { \
23850 int face_id; \
23851 \
23852 face_id = (row)->glyphs[area][START].face_id; \
23853 \
23854 s = alloca (sizeof *s); \
23855 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23856 append_glyph_string (&HEAD, &TAIL, s); \
23857 s->x = (X); \
23858 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23859 overlaps); \
23860 } \
23861 while (0)
23862
23863
23864 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23865 of AREA of glyph row ROW on window W between indices START and END.
23866 HL overrides the face for drawing glyph strings, e.g. it is
23867 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23868 x-positions of the drawing area.
23869
23870 This is an ugly monster macro construct because we must use alloca
23871 to allocate glyph strings (because draw_glyphs can be called
23872 asynchronously). */
23873
23874 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23875 do \
23876 { \
23877 HEAD = TAIL = NULL; \
23878 while (START < END) \
23879 { \
23880 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23881 switch (first_glyph->type) \
23882 { \
23883 case CHAR_GLYPH: \
23884 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23885 HL, X, LAST_X); \
23886 break; \
23887 \
23888 case COMPOSITE_GLYPH: \
23889 if (first_glyph->u.cmp.automatic) \
23890 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23891 HL, X, LAST_X); \
23892 else \
23893 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23894 HL, X, LAST_X); \
23895 break; \
23896 \
23897 case STRETCH_GLYPH: \
23898 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23899 HL, X, LAST_X); \
23900 break; \
23901 \
23902 case IMAGE_GLYPH: \
23903 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23904 HL, X, LAST_X); \
23905 break;
23906
23907 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23908 case XWIDGET_GLYPH: \
23909 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
23910 HL, X, LAST_X); \
23911 break;
23912
23913 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
23914 case GLYPHLESS_GLYPH: \
23915 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23916 HL, X, LAST_X); \
23917 break; \
23918 \
23919 default: \
23920 emacs_abort (); \
23921 } \
23922 \
23923 if (s) \
23924 { \
23925 set_glyph_string_background_width (s, START, LAST_X); \
23926 (X) += s->width; \
23927 } \
23928 } \
23929 } while (0)
23930
23931
23932 #ifdef HAVE_XWIDGETS
23933 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23934 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23935 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23936 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23937 #else
23938 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23939 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23940 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23941 #endif
23942
23943
23944 /* Draw glyphs between START and END in AREA of ROW on window W,
23945 starting at x-position X. X is relative to AREA in W. HL is a
23946 face-override with the following meaning:
23947
23948 DRAW_NORMAL_TEXT draw normally
23949 DRAW_CURSOR draw in cursor face
23950 DRAW_MOUSE_FACE draw in mouse face.
23951 DRAW_INVERSE_VIDEO draw in mode line face
23952 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23953 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23954
23955 If OVERLAPS is non-zero, draw only the foreground of characters and
23956 clip to the physical height of ROW. Non-zero value also defines
23957 the overlapping part to be drawn:
23958
23959 OVERLAPS_PRED overlap with preceding rows
23960 OVERLAPS_SUCC overlap with succeeding rows
23961 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23962 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23963
23964 Value is the x-position reached, relative to AREA of W. */
23965
23966 static int
23967 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23968 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23969 enum draw_glyphs_face hl, int overlaps)
23970 {
23971 struct glyph_string *head, *tail;
23972 struct glyph_string *s;
23973 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23974 int i, j, x_reached, last_x, area_left = 0;
23975 struct frame *f = XFRAME (WINDOW_FRAME (w));
23976 DECLARE_HDC (hdc);
23977
23978 ALLOCATE_HDC (hdc, f);
23979
23980 /* Let's rather be paranoid than getting a SEGV. */
23981 end = min (end, row->used[area]);
23982 start = clip_to_bounds (0, start, end);
23983
23984 /* Translate X to frame coordinates. Set last_x to the right
23985 end of the drawing area. */
23986 if (row->full_width_p)
23987 {
23988 /* X is relative to the left edge of W, without scroll bars
23989 or fringes. */
23990 area_left = WINDOW_LEFT_EDGE_X (w);
23991 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23992 }
23993 else
23994 {
23995 area_left = window_box_left (w, area);
23996 last_x = area_left + window_box_width (w, area);
23997 }
23998 x += area_left;
23999
24000 /* Build a doubly-linked list of glyph_string structures between
24001 head and tail from what we have to draw. Note that the macro
24002 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24003 the reason we use a separate variable `i'. */
24004 i = start;
24005 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24006 if (tail)
24007 x_reached = tail->x + tail->background_width;
24008 else
24009 x_reached = x;
24010
24011 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24012 the row, redraw some glyphs in front or following the glyph
24013 strings built above. */
24014 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24015 {
24016 struct glyph_string *h, *t;
24017 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24018 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24019 int check_mouse_face = 0;
24020 int dummy_x = 0;
24021
24022 /* If mouse highlighting is on, we may need to draw adjacent
24023 glyphs using mouse-face highlighting. */
24024 if (area == TEXT_AREA && row->mouse_face_p
24025 && hlinfo->mouse_face_beg_row >= 0
24026 && hlinfo->mouse_face_end_row >= 0)
24027 {
24028 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24029
24030 if (row_vpos >= hlinfo->mouse_face_beg_row
24031 && row_vpos <= hlinfo->mouse_face_end_row)
24032 {
24033 check_mouse_face = 1;
24034 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24035 ? hlinfo->mouse_face_beg_col : 0;
24036 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24037 ? hlinfo->mouse_face_end_col
24038 : row->used[TEXT_AREA];
24039 }
24040 }
24041
24042 /* Compute overhangs for all glyph strings. */
24043 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24044 for (s = head; s; s = s->next)
24045 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24046
24047 /* Prepend glyph strings for glyphs in front of the first glyph
24048 string that are overwritten because of the first glyph
24049 string's left overhang. The background of all strings
24050 prepended must be drawn because the first glyph string
24051 draws over it. */
24052 i = left_overwritten (head);
24053 if (i >= 0)
24054 {
24055 enum draw_glyphs_face overlap_hl;
24056
24057 /* If this row contains mouse highlighting, attempt to draw
24058 the overlapped glyphs with the correct highlight. This
24059 code fails if the overlap encompasses more than one glyph
24060 and mouse-highlight spans only some of these glyphs.
24061 However, making it work perfectly involves a lot more
24062 code, and I don't know if the pathological case occurs in
24063 practice, so we'll stick to this for now. --- cyd */
24064 if (check_mouse_face
24065 && mouse_beg_col < start && mouse_end_col > i)
24066 overlap_hl = DRAW_MOUSE_FACE;
24067 else
24068 overlap_hl = DRAW_NORMAL_TEXT;
24069
24070 j = i;
24071 BUILD_GLYPH_STRINGS (j, start, h, t,
24072 overlap_hl, dummy_x, last_x);
24073 start = i;
24074 compute_overhangs_and_x (t, head->x, 1);
24075 prepend_glyph_string_lists (&head, &tail, h, t);
24076 clip_head = head;
24077 }
24078
24079 /* Prepend glyph strings for glyphs in front of the first glyph
24080 string that overwrite that glyph string because of their
24081 right overhang. For these strings, only the foreground must
24082 be drawn, because it draws over the glyph string at `head'.
24083 The background must not be drawn because this would overwrite
24084 right overhangs of preceding glyphs for which no glyph
24085 strings exist. */
24086 i = left_overwriting (head);
24087 if (i >= 0)
24088 {
24089 enum draw_glyphs_face overlap_hl;
24090
24091 if (check_mouse_face
24092 && mouse_beg_col < start && mouse_end_col > i)
24093 overlap_hl = DRAW_MOUSE_FACE;
24094 else
24095 overlap_hl = DRAW_NORMAL_TEXT;
24096
24097 clip_head = head;
24098 BUILD_GLYPH_STRINGS (i, start, h, t,
24099 overlap_hl, dummy_x, last_x);
24100 for (s = h; s; s = s->next)
24101 s->background_filled_p = 1;
24102 compute_overhangs_and_x (t, head->x, 1);
24103 prepend_glyph_string_lists (&head, &tail, h, t);
24104 }
24105
24106 /* Append glyphs strings for glyphs following the last glyph
24107 string tail that are overwritten by tail. The background of
24108 these strings has to be drawn because tail's foreground draws
24109 over it. */
24110 i = right_overwritten (tail);
24111 if (i >= 0)
24112 {
24113 enum draw_glyphs_face overlap_hl;
24114
24115 if (check_mouse_face
24116 && mouse_beg_col < i && mouse_end_col > end)
24117 overlap_hl = DRAW_MOUSE_FACE;
24118 else
24119 overlap_hl = DRAW_NORMAL_TEXT;
24120
24121 BUILD_GLYPH_STRINGS (end, i, h, t,
24122 overlap_hl, x, last_x);
24123 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24124 we don't have `end = i;' here. */
24125 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24126 append_glyph_string_lists (&head, &tail, h, t);
24127 clip_tail = tail;
24128 }
24129
24130 /* Append glyph strings for glyphs following the last glyph
24131 string tail that overwrite tail. The foreground of such
24132 glyphs has to be drawn because it writes into the background
24133 of tail. The background must not be drawn because it could
24134 paint over the foreground of following glyphs. */
24135 i = right_overwriting (tail);
24136 if (i >= 0)
24137 {
24138 enum draw_glyphs_face overlap_hl;
24139 if (check_mouse_face
24140 && mouse_beg_col < i && mouse_end_col > end)
24141 overlap_hl = DRAW_MOUSE_FACE;
24142 else
24143 overlap_hl = DRAW_NORMAL_TEXT;
24144
24145 clip_tail = tail;
24146 i++; /* We must include the Ith glyph. */
24147 BUILD_GLYPH_STRINGS (end, i, h, t,
24148 overlap_hl, x, last_x);
24149 for (s = h; s; s = s->next)
24150 s->background_filled_p = 1;
24151 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24152 append_glyph_string_lists (&head, &tail, h, t);
24153 }
24154 if (clip_head || clip_tail)
24155 for (s = head; s; s = s->next)
24156 {
24157 s->clip_head = clip_head;
24158 s->clip_tail = clip_tail;
24159 }
24160 }
24161
24162 /* Draw all strings. */
24163 for (s = head; s; s = s->next)
24164 FRAME_RIF (f)->draw_glyph_string (s);
24165
24166 #ifndef HAVE_NS
24167 /* When focus a sole frame and move horizontally, this sets on_p to 0
24168 causing a failure to erase prev cursor position. */
24169 if (area == TEXT_AREA
24170 && !row->full_width_p
24171 /* When drawing overlapping rows, only the glyph strings'
24172 foreground is drawn, which doesn't erase a cursor
24173 completely. */
24174 && !overlaps)
24175 {
24176 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24177 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24178 : (tail ? tail->x + tail->background_width : x));
24179 x0 -= area_left;
24180 x1 -= area_left;
24181
24182 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24183 row->y, MATRIX_ROW_BOTTOM_Y (row));
24184 }
24185 #endif
24186
24187 /* Value is the x-position up to which drawn, relative to AREA of W.
24188 This doesn't include parts drawn because of overhangs. */
24189 if (row->full_width_p)
24190 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24191 else
24192 x_reached -= area_left;
24193
24194 RELEASE_HDC (hdc, f);
24195
24196 return x_reached;
24197 }
24198
24199 /* Expand row matrix if too narrow. Don't expand if area
24200 is not present. */
24201
24202 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24203 { \
24204 if (!fonts_changed_p \
24205 && (it->glyph_row->glyphs[area] \
24206 < it->glyph_row->glyphs[area + 1])) \
24207 { \
24208 it->w->ncols_scale_factor++; \
24209 fonts_changed_p = 1; \
24210 } \
24211 }
24212
24213 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24214 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24215
24216 static void
24217 append_glyph (struct it *it)
24218 {
24219 struct glyph *glyph;
24220 enum glyph_row_area area = it->area;
24221
24222 eassert (it->glyph_row);
24223 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24224
24225 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24226 if (glyph < it->glyph_row->glyphs[area + 1])
24227 {
24228 /* If the glyph row is reversed, we need to prepend the glyph
24229 rather than append it. */
24230 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24231 {
24232 struct glyph *g;
24233
24234 /* Make room for the additional glyph. */
24235 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24236 g[1] = *g;
24237 glyph = it->glyph_row->glyphs[area];
24238 }
24239 glyph->charpos = CHARPOS (it->position);
24240 glyph->object = it->object;
24241 if (it->pixel_width > 0)
24242 {
24243 glyph->pixel_width = it->pixel_width;
24244 glyph->padding_p = 0;
24245 }
24246 else
24247 {
24248 /* Assure at least 1-pixel width. Otherwise, cursor can't
24249 be displayed correctly. */
24250 glyph->pixel_width = 1;
24251 glyph->padding_p = 1;
24252 }
24253 glyph->ascent = it->ascent;
24254 glyph->descent = it->descent;
24255 glyph->voffset = it->voffset;
24256 glyph->type = CHAR_GLYPH;
24257 glyph->avoid_cursor_p = it->avoid_cursor_p;
24258 glyph->multibyte_p = it->multibyte_p;
24259 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24260 {
24261 /* In R2L rows, the left and the right box edges need to be
24262 drawn in reverse direction. */
24263 glyph->right_box_line_p = it->start_of_box_run_p;
24264 glyph->left_box_line_p = it->end_of_box_run_p;
24265 }
24266 else
24267 {
24268 glyph->left_box_line_p = it->start_of_box_run_p;
24269 glyph->right_box_line_p = it->end_of_box_run_p;
24270 }
24271 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24272 || it->phys_descent > it->descent);
24273 glyph->glyph_not_available_p = it->glyph_not_available_p;
24274 glyph->face_id = it->face_id;
24275 glyph->u.ch = it->char_to_display;
24276 glyph->slice.img = null_glyph_slice;
24277 glyph->font_type = FONT_TYPE_UNKNOWN;
24278 if (it->bidi_p)
24279 {
24280 glyph->resolved_level = it->bidi_it.resolved_level;
24281 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24282 emacs_abort ();
24283 glyph->bidi_type = it->bidi_it.type;
24284 }
24285 else
24286 {
24287 glyph->resolved_level = 0;
24288 glyph->bidi_type = UNKNOWN_BT;
24289 }
24290 ++it->glyph_row->used[area];
24291 }
24292 else
24293 IT_EXPAND_MATRIX_WIDTH (it, area);
24294 }
24295
24296 /* Store one glyph for the composition IT->cmp_it.id in
24297 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24298 non-null. */
24299
24300 static void
24301 append_composite_glyph (struct it *it)
24302 {
24303 struct glyph *glyph;
24304 enum glyph_row_area area = it->area;
24305
24306 eassert (it->glyph_row);
24307
24308 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24309 if (glyph < it->glyph_row->glyphs[area + 1])
24310 {
24311 /* If the glyph row is reversed, we need to prepend the glyph
24312 rather than append it. */
24313 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24314 {
24315 struct glyph *g;
24316
24317 /* Make room for the new glyph. */
24318 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24319 g[1] = *g;
24320 glyph = it->glyph_row->glyphs[it->area];
24321 }
24322 glyph->charpos = it->cmp_it.charpos;
24323 glyph->object = it->object;
24324 glyph->pixel_width = it->pixel_width;
24325 glyph->ascent = it->ascent;
24326 glyph->descent = it->descent;
24327 glyph->voffset = it->voffset;
24328 glyph->type = COMPOSITE_GLYPH;
24329 if (it->cmp_it.ch < 0)
24330 {
24331 glyph->u.cmp.automatic = 0;
24332 glyph->u.cmp.id = it->cmp_it.id;
24333 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24334 }
24335 else
24336 {
24337 glyph->u.cmp.automatic = 1;
24338 glyph->u.cmp.id = it->cmp_it.id;
24339 glyph->slice.cmp.from = it->cmp_it.from;
24340 glyph->slice.cmp.to = it->cmp_it.to - 1;
24341 }
24342 glyph->avoid_cursor_p = it->avoid_cursor_p;
24343 glyph->multibyte_p = it->multibyte_p;
24344 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24345 {
24346 /* In R2L rows, the left and the right box edges need to be
24347 drawn in reverse direction. */
24348 glyph->right_box_line_p = it->start_of_box_run_p;
24349 glyph->left_box_line_p = it->end_of_box_run_p;
24350 }
24351 else
24352 {
24353 glyph->left_box_line_p = it->start_of_box_run_p;
24354 glyph->right_box_line_p = it->end_of_box_run_p;
24355 }
24356 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24357 || it->phys_descent > it->descent);
24358 glyph->padding_p = 0;
24359 glyph->glyph_not_available_p = 0;
24360 glyph->face_id = it->face_id;
24361 glyph->font_type = FONT_TYPE_UNKNOWN;
24362 if (it->bidi_p)
24363 {
24364 glyph->resolved_level = it->bidi_it.resolved_level;
24365 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24366 emacs_abort ();
24367 glyph->bidi_type = it->bidi_it.type;
24368 }
24369 ++it->glyph_row->used[area];
24370 }
24371 else
24372 IT_EXPAND_MATRIX_WIDTH (it, area);
24373 }
24374
24375
24376 /* Change IT->ascent and IT->height according to the setting of
24377 IT->voffset. */
24378
24379 static void
24380 take_vertical_position_into_account (struct it *it)
24381 {
24382 if (it->voffset)
24383 {
24384 if (it->voffset < 0)
24385 /* Increase the ascent so that we can display the text higher
24386 in the line. */
24387 it->ascent -= it->voffset;
24388 else
24389 /* Increase the descent so that we can display the text lower
24390 in the line. */
24391 it->descent += it->voffset;
24392 }
24393 }
24394
24395
24396 /* Produce glyphs/get display metrics for the image IT is loaded with.
24397 See the description of struct display_iterator in dispextern.h for
24398 an overview of struct display_iterator. */
24399
24400 static void
24401 produce_image_glyph (struct it *it)
24402 {
24403 struct image *img;
24404 struct face *face;
24405 int glyph_ascent, crop;
24406 struct glyph_slice slice;
24407
24408 eassert (it->what == IT_IMAGE);
24409
24410 face = FACE_FROM_ID (it->f, it->face_id);
24411 eassert (face);
24412 /* Make sure X resources of the face is loaded. */
24413 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24414
24415 if (it->image_id < 0)
24416 {
24417 /* Fringe bitmap. */
24418 it->ascent = it->phys_ascent = 0;
24419 it->descent = it->phys_descent = 0;
24420 it->pixel_width = 0;
24421 it->nglyphs = 0;
24422 return;
24423 }
24424
24425 img = IMAGE_FROM_ID (it->f, it->image_id);
24426 eassert (img);
24427 /* Make sure X resources of the image is loaded. */
24428 prepare_image_for_display (it->f, img);
24429
24430 slice.x = slice.y = 0;
24431 slice.width = img->width;
24432 slice.height = img->height;
24433
24434 if (INTEGERP (it->slice.x))
24435 slice.x = XINT (it->slice.x);
24436 else if (FLOATP (it->slice.x))
24437 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24438
24439 if (INTEGERP (it->slice.y))
24440 slice.y = XINT (it->slice.y);
24441 else if (FLOATP (it->slice.y))
24442 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24443
24444 if (INTEGERP (it->slice.width))
24445 slice.width = XINT (it->slice.width);
24446 else if (FLOATP (it->slice.width))
24447 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24448
24449 if (INTEGERP (it->slice.height))
24450 slice.height = XINT (it->slice.height);
24451 else if (FLOATP (it->slice.height))
24452 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24453
24454 if (slice.x >= img->width)
24455 slice.x = img->width;
24456 if (slice.y >= img->height)
24457 slice.y = img->height;
24458 if (slice.x + slice.width >= img->width)
24459 slice.width = img->width - slice.x;
24460 if (slice.y + slice.height > img->height)
24461 slice.height = img->height - slice.y;
24462
24463 if (slice.width == 0 || slice.height == 0)
24464 return;
24465
24466 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24467
24468 it->descent = slice.height - glyph_ascent;
24469 if (slice.y == 0)
24470 it->descent += img->vmargin;
24471 if (slice.y + slice.height == img->height)
24472 it->descent += img->vmargin;
24473 it->phys_descent = it->descent;
24474
24475 it->pixel_width = slice.width;
24476 if (slice.x == 0)
24477 it->pixel_width += img->hmargin;
24478 if (slice.x + slice.width == img->width)
24479 it->pixel_width += img->hmargin;
24480
24481 /* It's quite possible for images to have an ascent greater than
24482 their height, so don't get confused in that case. */
24483 if (it->descent < 0)
24484 it->descent = 0;
24485
24486 it->nglyphs = 1;
24487
24488 if (face->box != FACE_NO_BOX)
24489 {
24490 if (face->box_line_width > 0)
24491 {
24492 if (slice.y == 0)
24493 it->ascent += face->box_line_width;
24494 if (slice.y + slice.height == img->height)
24495 it->descent += face->box_line_width;
24496 }
24497
24498 if (it->start_of_box_run_p && slice.x == 0)
24499 it->pixel_width += eabs (face->box_line_width);
24500 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24501 it->pixel_width += eabs (face->box_line_width);
24502 }
24503
24504 take_vertical_position_into_account (it);
24505
24506 /* Automatically crop wide image glyphs at right edge so we can
24507 draw the cursor on same display row. */
24508 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24509 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24510 {
24511 it->pixel_width -= crop;
24512 slice.width -= crop;
24513 }
24514
24515 if (it->glyph_row)
24516 {
24517 struct glyph *glyph;
24518 enum glyph_row_area area = it->area;
24519
24520 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24521 if (glyph < it->glyph_row->glyphs[area + 1])
24522 {
24523 glyph->charpos = CHARPOS (it->position);
24524 glyph->object = it->object;
24525 glyph->pixel_width = it->pixel_width;
24526 glyph->ascent = glyph_ascent;
24527 glyph->descent = it->descent;
24528 glyph->voffset = it->voffset;
24529 glyph->type = IMAGE_GLYPH;
24530 glyph->avoid_cursor_p = it->avoid_cursor_p;
24531 glyph->multibyte_p = it->multibyte_p;
24532 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24533 {
24534 /* In R2L rows, the left and the right box edges need to be
24535 drawn in reverse direction. */
24536 glyph->right_box_line_p = it->start_of_box_run_p;
24537 glyph->left_box_line_p = it->end_of_box_run_p;
24538 }
24539 else
24540 {
24541 glyph->left_box_line_p = it->start_of_box_run_p;
24542 glyph->right_box_line_p = it->end_of_box_run_p;
24543 }
24544 glyph->overlaps_vertically_p = 0;
24545 glyph->padding_p = 0;
24546 glyph->glyph_not_available_p = 0;
24547 glyph->face_id = it->face_id;
24548 glyph->u.img_id = img->id;
24549 glyph->slice.img = slice;
24550 glyph->font_type = FONT_TYPE_UNKNOWN;
24551 if (it->bidi_p)
24552 {
24553 glyph->resolved_level = it->bidi_it.resolved_level;
24554 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24555 emacs_abort ();
24556 glyph->bidi_type = it->bidi_it.type;
24557 }
24558 ++it->glyph_row->used[area];
24559 }
24560 else
24561 IT_EXPAND_MATRIX_WIDTH (it, area);
24562 }
24563 }
24564
24565 #ifdef HAVE_XWIDGETS
24566 static void
24567 produce_xwidget_glyph (struct it *it)
24568 {
24569 struct xwidget* xw;
24570 struct face *face;
24571 int glyph_ascent, crop;
24572 printf("produce_xwidget_glyph:\n");
24573 eassert (it->what == IT_XWIDGET);
24574
24575 face = FACE_FROM_ID (it->f, it->face_id);
24576 eassert (face);
24577 /* Make sure X resources of the face is loaded. */
24578 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24579
24580 xw = it->xwidget;
24581 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
24582 it->descent = xw->height/2;
24583 it->phys_descent = it->descent;
24584 it->pixel_width = xw->width;
24585 /* It's quite possible for images to have an ascent greater than
24586 their height, so don't get confused in that case. */
24587 if (it->descent < 0)
24588 it->descent = 0;
24589
24590 it->nglyphs = 1;
24591
24592 if (face->box != FACE_NO_BOX)
24593 {
24594 if (face->box_line_width > 0)
24595 {
24596 it->ascent += face->box_line_width;
24597 it->descent += face->box_line_width;
24598 }
24599
24600 if (it->start_of_box_run_p)
24601 it->pixel_width += eabs (face->box_line_width);
24602 it->pixel_width += eabs (face->box_line_width);
24603 }
24604
24605 take_vertical_position_into_account (it);
24606
24607 /* Automatically crop wide image glyphs at right edge so we can
24608 draw the cursor on same display row. */
24609 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24610 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24611 {
24612 it->pixel_width -= crop;
24613 }
24614
24615 if (it->glyph_row)
24616 {
24617 struct glyph *glyph;
24618 enum glyph_row_area area = it->area;
24619
24620 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24621 if (glyph < it->glyph_row->glyphs[area + 1])
24622 {
24623 glyph->charpos = CHARPOS (it->position);
24624 glyph->object = it->object;
24625 glyph->pixel_width = it->pixel_width;
24626 glyph->ascent = glyph_ascent;
24627 glyph->descent = it->descent;
24628 glyph->voffset = it->voffset;
24629 glyph->type = XWIDGET_GLYPH;
24630
24631 glyph->multibyte_p = it->multibyte_p;
24632 glyph->left_box_line_p = it->start_of_box_run_p;
24633 glyph->right_box_line_p = it->end_of_box_run_p;
24634 glyph->overlaps_vertically_p = 0;
24635 glyph->padding_p = 0;
24636 glyph->glyph_not_available_p = 0;
24637 glyph->face_id = it->face_id;
24638 glyph->u.xwidget = it->xwidget;
24639 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
24640 glyph->font_type = FONT_TYPE_UNKNOWN;
24641 ++it->glyph_row->used[area];
24642 }
24643 else
24644 IT_EXPAND_MATRIX_WIDTH (it, area);
24645 }
24646 }
24647 #endif
24648
24649 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24650 of the glyph, WIDTH and HEIGHT are the width and height of the
24651 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24652
24653 static void
24654 append_stretch_glyph (struct it *it, Lisp_Object object,
24655 int width, int height, int ascent)
24656 {
24657 struct glyph *glyph;
24658 enum glyph_row_area area = it->area;
24659
24660 eassert (ascent >= 0 && ascent <= height);
24661
24662 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24663 if (glyph < it->glyph_row->glyphs[area + 1])
24664 {
24665 /* If the glyph row is reversed, we need to prepend the glyph
24666 rather than append it. */
24667 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24668 {
24669 struct glyph *g;
24670
24671 /* Make room for the additional glyph. */
24672 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24673 g[1] = *g;
24674 glyph = it->glyph_row->glyphs[area];
24675 }
24676 glyph->charpos = CHARPOS (it->position);
24677 glyph->object = object;
24678 glyph->pixel_width = width;
24679 glyph->ascent = ascent;
24680 glyph->descent = height - ascent;
24681 glyph->voffset = it->voffset;
24682 glyph->type = STRETCH_GLYPH;
24683 glyph->avoid_cursor_p = it->avoid_cursor_p;
24684 glyph->multibyte_p = it->multibyte_p;
24685 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24686 {
24687 /* In R2L rows, the left and the right box edges need to be
24688 drawn in reverse direction. */
24689 glyph->right_box_line_p = it->start_of_box_run_p;
24690 glyph->left_box_line_p = it->end_of_box_run_p;
24691 }
24692 else
24693 {
24694 glyph->left_box_line_p = it->start_of_box_run_p;
24695 glyph->right_box_line_p = it->end_of_box_run_p;
24696 }
24697 glyph->overlaps_vertically_p = 0;
24698 glyph->padding_p = 0;
24699 glyph->glyph_not_available_p = 0;
24700 glyph->face_id = it->face_id;
24701 glyph->u.stretch.ascent = ascent;
24702 glyph->u.stretch.height = height;
24703 glyph->slice.img = null_glyph_slice;
24704 glyph->font_type = FONT_TYPE_UNKNOWN;
24705 if (it->bidi_p)
24706 {
24707 glyph->resolved_level = it->bidi_it.resolved_level;
24708 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24709 emacs_abort ();
24710 glyph->bidi_type = it->bidi_it.type;
24711 }
24712 else
24713 {
24714 glyph->resolved_level = 0;
24715 glyph->bidi_type = UNKNOWN_BT;
24716 }
24717 ++it->glyph_row->used[area];
24718 }
24719 else
24720 IT_EXPAND_MATRIX_WIDTH (it, area);
24721 }
24722
24723 #endif /* HAVE_WINDOW_SYSTEM */
24724
24725 /* Produce a stretch glyph for iterator IT. IT->object is the value
24726 of the glyph property displayed. The value must be a list
24727 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24728 being recognized:
24729
24730 1. `:width WIDTH' specifies that the space should be WIDTH *
24731 canonical char width wide. WIDTH may be an integer or floating
24732 point number.
24733
24734 2. `:relative-width FACTOR' specifies that the width of the stretch
24735 should be computed from the width of the first character having the
24736 `glyph' property, and should be FACTOR times that width.
24737
24738 3. `:align-to HPOS' specifies that the space should be wide enough
24739 to reach HPOS, a value in canonical character units.
24740
24741 Exactly one of the above pairs must be present.
24742
24743 4. `:height HEIGHT' specifies that the height of the stretch produced
24744 should be HEIGHT, measured in canonical character units.
24745
24746 5. `:relative-height FACTOR' specifies that the height of the
24747 stretch should be FACTOR times the height of the characters having
24748 the glyph property.
24749
24750 Either none or exactly one of 4 or 5 must be present.
24751
24752 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24753 of the stretch should be used for the ascent of the stretch.
24754 ASCENT must be in the range 0 <= ASCENT <= 100. */
24755
24756 void
24757 produce_stretch_glyph (struct it *it)
24758 {
24759 /* (space :width WIDTH :height HEIGHT ...) */
24760 Lisp_Object prop, plist;
24761 int width = 0, height = 0, align_to = -1;
24762 int zero_width_ok_p = 0;
24763 double tem;
24764 struct font *font = NULL;
24765
24766 #ifdef HAVE_WINDOW_SYSTEM
24767 int ascent = 0;
24768 int zero_height_ok_p = 0;
24769
24770 if (FRAME_WINDOW_P (it->f))
24771 {
24772 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24773 font = face->font ? face->font : FRAME_FONT (it->f);
24774 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24775 }
24776 #endif
24777
24778 /* List should start with `space'. */
24779 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24780 plist = XCDR (it->object);
24781
24782 /* Compute the width of the stretch. */
24783 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24784 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24785 {
24786 /* Absolute width `:width WIDTH' specified and valid. */
24787 zero_width_ok_p = 1;
24788 width = (int)tem;
24789 }
24790 #ifdef HAVE_WINDOW_SYSTEM
24791 else if (FRAME_WINDOW_P (it->f)
24792 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24793 {
24794 /* Relative width `:relative-width FACTOR' specified and valid.
24795 Compute the width of the characters having the `glyph'
24796 property. */
24797 struct it it2;
24798 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24799
24800 it2 = *it;
24801 if (it->multibyte_p)
24802 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24803 else
24804 {
24805 it2.c = it2.char_to_display = *p, it2.len = 1;
24806 if (! ASCII_CHAR_P (it2.c))
24807 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24808 }
24809
24810 it2.glyph_row = NULL;
24811 it2.what = IT_CHARACTER;
24812 x_produce_glyphs (&it2);
24813 width = NUMVAL (prop) * it2.pixel_width;
24814 }
24815 #endif /* HAVE_WINDOW_SYSTEM */
24816 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24817 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24818 {
24819 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24820 align_to = (align_to < 0
24821 ? 0
24822 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24823 else if (align_to < 0)
24824 align_to = window_box_left_offset (it->w, TEXT_AREA);
24825 width = max (0, (int)tem + align_to - it->current_x);
24826 zero_width_ok_p = 1;
24827 }
24828 else
24829 /* Nothing specified -> width defaults to canonical char width. */
24830 width = FRAME_COLUMN_WIDTH (it->f);
24831
24832 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24833 width = 1;
24834
24835 #ifdef HAVE_WINDOW_SYSTEM
24836 /* Compute height. */
24837 if (FRAME_WINDOW_P (it->f))
24838 {
24839 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24840 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24841 {
24842 height = (int)tem;
24843 zero_height_ok_p = 1;
24844 }
24845 else if (prop = Fplist_get (plist, QCrelative_height),
24846 NUMVAL (prop) > 0)
24847 height = FONT_HEIGHT (font) * NUMVAL (prop);
24848 else
24849 height = FONT_HEIGHT (font);
24850
24851 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24852 height = 1;
24853
24854 /* Compute percentage of height used for ascent. If
24855 `:ascent ASCENT' is present and valid, use that. Otherwise,
24856 derive the ascent from the font in use. */
24857 if (prop = Fplist_get (plist, QCascent),
24858 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24859 ascent = height * NUMVAL (prop) / 100.0;
24860 else if (!NILP (prop)
24861 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24862 ascent = min (max (0, (int)tem), height);
24863 else
24864 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24865 }
24866 else
24867 #endif /* HAVE_WINDOW_SYSTEM */
24868 height = 1;
24869
24870 if (width > 0 && it->line_wrap != TRUNCATE
24871 && it->current_x + width > it->last_visible_x)
24872 {
24873 width = it->last_visible_x - it->current_x;
24874 #ifdef HAVE_WINDOW_SYSTEM
24875 /* Subtract one more pixel from the stretch width, but only on
24876 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24877 width -= FRAME_WINDOW_P (it->f);
24878 #endif
24879 }
24880
24881 if (width > 0 && height > 0 && it->glyph_row)
24882 {
24883 Lisp_Object o_object = it->object;
24884 Lisp_Object object = it->stack[it->sp - 1].string;
24885 int n = width;
24886
24887 if (!STRINGP (object))
24888 object = it->w->contents;
24889 #ifdef HAVE_WINDOW_SYSTEM
24890 if (FRAME_WINDOW_P (it->f))
24891 append_stretch_glyph (it, object, width, height, ascent);
24892 else
24893 #endif
24894 {
24895 it->object = object;
24896 it->char_to_display = ' ';
24897 it->pixel_width = it->len = 1;
24898 while (n--)
24899 tty_append_glyph (it);
24900 it->object = o_object;
24901 }
24902 }
24903
24904 it->pixel_width = width;
24905 #ifdef HAVE_WINDOW_SYSTEM
24906 if (FRAME_WINDOW_P (it->f))
24907 {
24908 it->ascent = it->phys_ascent = ascent;
24909 it->descent = it->phys_descent = height - it->ascent;
24910 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24911 take_vertical_position_into_account (it);
24912 }
24913 else
24914 #endif
24915 it->nglyphs = width;
24916 }
24917
24918 /* Get information about special display element WHAT in an
24919 environment described by IT. WHAT is one of IT_TRUNCATION or
24920 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24921 non-null glyph_row member. This function ensures that fields like
24922 face_id, c, len of IT are left untouched. */
24923
24924 static void
24925 produce_special_glyphs (struct it *it, enum display_element_type what)
24926 {
24927 struct it temp_it;
24928 Lisp_Object gc;
24929 GLYPH glyph;
24930
24931 temp_it = *it;
24932 temp_it.object = make_number (0);
24933 memset (&temp_it.current, 0, sizeof temp_it.current);
24934
24935 if (what == IT_CONTINUATION)
24936 {
24937 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24938 if (it->bidi_it.paragraph_dir == R2L)
24939 SET_GLYPH_FROM_CHAR (glyph, '/');
24940 else
24941 SET_GLYPH_FROM_CHAR (glyph, '\\');
24942 if (it->dp
24943 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24944 {
24945 /* FIXME: Should we mirror GC for R2L lines? */
24946 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24947 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24948 }
24949 }
24950 else if (what == IT_TRUNCATION)
24951 {
24952 /* Truncation glyph. */
24953 SET_GLYPH_FROM_CHAR (glyph, '$');
24954 if (it->dp
24955 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24956 {
24957 /* FIXME: Should we mirror GC for R2L lines? */
24958 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24959 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24960 }
24961 }
24962 else
24963 emacs_abort ();
24964
24965 #ifdef HAVE_WINDOW_SYSTEM
24966 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24967 is turned off, we precede the truncation/continuation glyphs by a
24968 stretch glyph whose width is computed such that these special
24969 glyphs are aligned at the window margin, even when very different
24970 fonts are used in different glyph rows. */
24971 if (FRAME_WINDOW_P (temp_it.f)
24972 /* init_iterator calls this with it->glyph_row == NULL, and it
24973 wants only the pixel width of the truncation/continuation
24974 glyphs. */
24975 && temp_it.glyph_row
24976 /* insert_left_trunc_glyphs calls us at the beginning of the
24977 row, and it has its own calculation of the stretch glyph
24978 width. */
24979 && temp_it.glyph_row->used[TEXT_AREA] > 0
24980 && (temp_it.glyph_row->reversed_p
24981 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24982 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24983 {
24984 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24985
24986 if (stretch_width > 0)
24987 {
24988 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24989 struct font *font =
24990 face->font ? face->font : FRAME_FONT (temp_it.f);
24991 int stretch_ascent =
24992 (((temp_it.ascent + temp_it.descent)
24993 * FONT_BASE (font)) / FONT_HEIGHT (font));
24994
24995 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24996 temp_it.ascent + temp_it.descent,
24997 stretch_ascent);
24998 }
24999 }
25000 #endif
25001
25002 temp_it.dp = NULL;
25003 temp_it.what = IT_CHARACTER;
25004 temp_it.len = 1;
25005 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25006 temp_it.face_id = GLYPH_FACE (glyph);
25007 temp_it.len = CHAR_BYTES (temp_it.c);
25008
25009 PRODUCE_GLYPHS (&temp_it);
25010 it->pixel_width = temp_it.pixel_width;
25011 it->nglyphs = temp_it.pixel_width;
25012 }
25013
25014 #ifdef HAVE_WINDOW_SYSTEM
25015
25016 /* Calculate line-height and line-spacing properties.
25017 An integer value specifies explicit pixel value.
25018 A float value specifies relative value to current face height.
25019 A cons (float . face-name) specifies relative value to
25020 height of specified face font.
25021
25022 Returns height in pixels, or nil. */
25023
25024
25025 static Lisp_Object
25026 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25027 int boff, int override)
25028 {
25029 Lisp_Object face_name = Qnil;
25030 int ascent, descent, height;
25031
25032 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25033 return val;
25034
25035 if (CONSP (val))
25036 {
25037 face_name = XCAR (val);
25038 val = XCDR (val);
25039 if (!NUMBERP (val))
25040 val = make_number (1);
25041 if (NILP (face_name))
25042 {
25043 height = it->ascent + it->descent;
25044 goto scale;
25045 }
25046 }
25047
25048 if (NILP (face_name))
25049 {
25050 font = FRAME_FONT (it->f);
25051 boff = FRAME_BASELINE_OFFSET (it->f);
25052 }
25053 else if (EQ (face_name, Qt))
25054 {
25055 override = 0;
25056 }
25057 else
25058 {
25059 int face_id;
25060 struct face *face;
25061
25062 face_id = lookup_named_face (it->f, face_name, 0);
25063 if (face_id < 0)
25064 return make_number (-1);
25065
25066 face = FACE_FROM_ID (it->f, face_id);
25067 font = face->font;
25068 if (font == NULL)
25069 return make_number (-1);
25070 boff = font->baseline_offset;
25071 if (font->vertical_centering)
25072 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25073 }
25074
25075 ascent = FONT_BASE (font) + boff;
25076 descent = FONT_DESCENT (font) - boff;
25077
25078 if (override)
25079 {
25080 it->override_ascent = ascent;
25081 it->override_descent = descent;
25082 it->override_boff = boff;
25083 }
25084
25085 height = ascent + descent;
25086
25087 scale:
25088 if (FLOATP (val))
25089 height = (int)(XFLOAT_DATA (val) * height);
25090 else if (INTEGERP (val))
25091 height *= XINT (val);
25092
25093 return make_number (height);
25094 }
25095
25096
25097 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25098 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25099 and only if this is for a character for which no font was found.
25100
25101 If the display method (it->glyphless_method) is
25102 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25103 length of the acronym or the hexadecimal string, UPPER_XOFF and
25104 UPPER_YOFF are pixel offsets for the upper part of the string,
25105 LOWER_XOFF and LOWER_YOFF are for the lower part.
25106
25107 For the other display methods, LEN through LOWER_YOFF are zero. */
25108
25109 static void
25110 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25111 short upper_xoff, short upper_yoff,
25112 short lower_xoff, short lower_yoff)
25113 {
25114 struct glyph *glyph;
25115 enum glyph_row_area area = it->area;
25116
25117 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25118 if (glyph < it->glyph_row->glyphs[area + 1])
25119 {
25120 /* If the glyph row is reversed, we need to prepend the glyph
25121 rather than append it. */
25122 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25123 {
25124 struct glyph *g;
25125
25126 /* Make room for the additional glyph. */
25127 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25128 g[1] = *g;
25129 glyph = it->glyph_row->glyphs[area];
25130 }
25131 glyph->charpos = CHARPOS (it->position);
25132 glyph->object = it->object;
25133 glyph->pixel_width = it->pixel_width;
25134 glyph->ascent = it->ascent;
25135 glyph->descent = it->descent;
25136 glyph->voffset = it->voffset;
25137 glyph->type = GLYPHLESS_GLYPH;
25138 glyph->u.glyphless.method = it->glyphless_method;
25139 glyph->u.glyphless.for_no_font = for_no_font;
25140 glyph->u.glyphless.len = len;
25141 glyph->u.glyphless.ch = it->c;
25142 glyph->slice.glyphless.upper_xoff = upper_xoff;
25143 glyph->slice.glyphless.upper_yoff = upper_yoff;
25144 glyph->slice.glyphless.lower_xoff = lower_xoff;
25145 glyph->slice.glyphless.lower_yoff = lower_yoff;
25146 glyph->avoid_cursor_p = it->avoid_cursor_p;
25147 glyph->multibyte_p = it->multibyte_p;
25148 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25149 {
25150 /* In R2L rows, the left and the right box edges need to be
25151 drawn in reverse direction. */
25152 glyph->right_box_line_p = it->start_of_box_run_p;
25153 glyph->left_box_line_p = it->end_of_box_run_p;
25154 }
25155 else
25156 {
25157 glyph->left_box_line_p = it->start_of_box_run_p;
25158 glyph->right_box_line_p = it->end_of_box_run_p;
25159 }
25160 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25161 || it->phys_descent > it->descent);
25162 glyph->padding_p = 0;
25163 glyph->glyph_not_available_p = 0;
25164 glyph->face_id = face_id;
25165 glyph->font_type = FONT_TYPE_UNKNOWN;
25166 if (it->bidi_p)
25167 {
25168 glyph->resolved_level = it->bidi_it.resolved_level;
25169 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25170 emacs_abort ();
25171 glyph->bidi_type = it->bidi_it.type;
25172 }
25173 ++it->glyph_row->used[area];
25174 }
25175 else
25176 IT_EXPAND_MATRIX_WIDTH (it, area);
25177 }
25178
25179
25180 /* Produce a glyph for a glyphless character for iterator IT.
25181 IT->glyphless_method specifies which method to use for displaying
25182 the character. See the description of enum
25183 glyphless_display_method in dispextern.h for the detail.
25184
25185 FOR_NO_FONT is nonzero if and only if this is for a character for
25186 which no font was found. ACRONYM, if non-nil, is an acronym string
25187 for the character. */
25188
25189 static void
25190 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25191 {
25192 int face_id;
25193 struct face *face;
25194 struct font *font;
25195 int base_width, base_height, width, height;
25196 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25197 int len;
25198
25199 /* Get the metrics of the base font. We always refer to the current
25200 ASCII face. */
25201 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25202 font = face->font ? face->font : FRAME_FONT (it->f);
25203 it->ascent = FONT_BASE (font) + font->baseline_offset;
25204 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25205 base_height = it->ascent + it->descent;
25206 base_width = font->average_width;
25207
25208 /* Get a face ID for the glyph by utilizing a cache (the same way as
25209 done for `escape-glyph' in get_next_display_element). */
25210 if (it->f == last_glyphless_glyph_frame
25211 && it->face_id == last_glyphless_glyph_face_id)
25212 {
25213 face_id = last_glyphless_glyph_merged_face_id;
25214 }
25215 else
25216 {
25217 /* Merge the `glyphless-char' face into the current face. */
25218 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
25219 last_glyphless_glyph_frame = it->f;
25220 last_glyphless_glyph_face_id = it->face_id;
25221 last_glyphless_glyph_merged_face_id = face_id;
25222 }
25223
25224 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25225 {
25226 it->pixel_width = THIN_SPACE_WIDTH;
25227 len = 0;
25228 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25229 }
25230 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25231 {
25232 width = CHAR_WIDTH (it->c);
25233 if (width == 0)
25234 width = 1;
25235 else if (width > 4)
25236 width = 4;
25237 it->pixel_width = base_width * width;
25238 len = 0;
25239 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25240 }
25241 else
25242 {
25243 char buf[7];
25244 const char *str;
25245 unsigned int code[6];
25246 int upper_len;
25247 int ascent, descent;
25248 struct font_metrics metrics_upper, metrics_lower;
25249
25250 face = FACE_FROM_ID (it->f, face_id);
25251 font = face->font ? face->font : FRAME_FONT (it->f);
25252 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25253
25254 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25255 {
25256 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25257 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25258 if (CONSP (acronym))
25259 acronym = XCAR (acronym);
25260 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25261 }
25262 else
25263 {
25264 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25265 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25266 str = buf;
25267 }
25268 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25269 code[len] = font->driver->encode_char (font, str[len]);
25270 upper_len = (len + 1) / 2;
25271 font->driver->text_extents (font, code, upper_len,
25272 &metrics_upper);
25273 font->driver->text_extents (font, code + upper_len, len - upper_len,
25274 &metrics_lower);
25275
25276
25277
25278 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25279 width = max (metrics_upper.width, metrics_lower.width) + 4;
25280 upper_xoff = upper_yoff = 2; /* the typical case */
25281 if (base_width >= width)
25282 {
25283 /* Align the upper to the left, the lower to the right. */
25284 it->pixel_width = base_width;
25285 lower_xoff = base_width - 2 - metrics_lower.width;
25286 }
25287 else
25288 {
25289 /* Center the shorter one. */
25290 it->pixel_width = width;
25291 if (metrics_upper.width >= metrics_lower.width)
25292 lower_xoff = (width - metrics_lower.width) / 2;
25293 else
25294 {
25295 /* FIXME: This code doesn't look right. It formerly was
25296 missing the "lower_xoff = 0;", which couldn't have
25297 been right since it left lower_xoff uninitialized. */
25298 lower_xoff = 0;
25299 upper_xoff = (width - metrics_upper.width) / 2;
25300 }
25301 }
25302
25303 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25304 top, bottom, and between upper and lower strings. */
25305 height = (metrics_upper.ascent + metrics_upper.descent
25306 + metrics_lower.ascent + metrics_lower.descent) + 5;
25307 /* Center vertically.
25308 H:base_height, D:base_descent
25309 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25310
25311 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25312 descent = D - H/2 + h/2;
25313 lower_yoff = descent - 2 - ld;
25314 upper_yoff = lower_yoff - la - 1 - ud; */
25315 ascent = - (it->descent - (base_height + height + 1) / 2);
25316 descent = it->descent - (base_height - height) / 2;
25317 lower_yoff = descent - 2 - metrics_lower.descent;
25318 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25319 - metrics_upper.descent);
25320 /* Don't make the height shorter than the base height. */
25321 if (height > base_height)
25322 {
25323 it->ascent = ascent;
25324 it->descent = descent;
25325 }
25326 }
25327
25328 it->phys_ascent = it->ascent;
25329 it->phys_descent = it->descent;
25330 if (it->glyph_row)
25331 append_glyphless_glyph (it, face_id, for_no_font, len,
25332 upper_xoff, upper_yoff,
25333 lower_xoff, lower_yoff);
25334 it->nglyphs = 1;
25335 take_vertical_position_into_account (it);
25336 }
25337
25338
25339 /* RIF:
25340 Produce glyphs/get display metrics for the display element IT is
25341 loaded with. See the description of struct it in dispextern.h
25342 for an overview of struct it. */
25343
25344 void
25345 x_produce_glyphs (struct it *it)
25346 {
25347 int extra_line_spacing = it->extra_line_spacing;
25348
25349 it->glyph_not_available_p = 0;
25350
25351 if (it->what == IT_CHARACTER)
25352 {
25353 XChar2b char2b;
25354 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25355 struct font *font = face->font;
25356 struct font_metrics *pcm = NULL;
25357 int boff; /* baseline offset */
25358
25359 if (font == NULL)
25360 {
25361 /* When no suitable font is found, display this character by
25362 the method specified in the first extra slot of
25363 Vglyphless_char_display. */
25364 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25365
25366 eassert (it->what == IT_GLYPHLESS);
25367 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25368 goto done;
25369 }
25370
25371 boff = font->baseline_offset;
25372 if (font->vertical_centering)
25373 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25374
25375 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25376 {
25377 int stretched_p;
25378
25379 it->nglyphs = 1;
25380
25381 if (it->override_ascent >= 0)
25382 {
25383 it->ascent = it->override_ascent;
25384 it->descent = it->override_descent;
25385 boff = it->override_boff;
25386 }
25387 else
25388 {
25389 it->ascent = FONT_BASE (font) + boff;
25390 it->descent = FONT_DESCENT (font) - boff;
25391 }
25392
25393 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25394 {
25395 pcm = get_per_char_metric (font, &char2b);
25396 if (pcm->width == 0
25397 && pcm->rbearing == 0 && pcm->lbearing == 0)
25398 pcm = NULL;
25399 }
25400
25401 if (pcm)
25402 {
25403 it->phys_ascent = pcm->ascent + boff;
25404 it->phys_descent = pcm->descent - boff;
25405 it->pixel_width = pcm->width;
25406 }
25407 else
25408 {
25409 it->glyph_not_available_p = 1;
25410 it->phys_ascent = it->ascent;
25411 it->phys_descent = it->descent;
25412 it->pixel_width = font->space_width;
25413 }
25414
25415 if (it->constrain_row_ascent_descent_p)
25416 {
25417 if (it->descent > it->max_descent)
25418 {
25419 it->ascent += it->descent - it->max_descent;
25420 it->descent = it->max_descent;
25421 }
25422 if (it->ascent > it->max_ascent)
25423 {
25424 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25425 it->ascent = it->max_ascent;
25426 }
25427 it->phys_ascent = min (it->phys_ascent, it->ascent);
25428 it->phys_descent = min (it->phys_descent, it->descent);
25429 extra_line_spacing = 0;
25430 }
25431
25432 /* If this is a space inside a region of text with
25433 `space-width' property, change its width. */
25434 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25435 if (stretched_p)
25436 it->pixel_width *= XFLOATINT (it->space_width);
25437
25438 /* If face has a box, add the box thickness to the character
25439 height. If character has a box line to the left and/or
25440 right, add the box line width to the character's width. */
25441 if (face->box != FACE_NO_BOX)
25442 {
25443 int thick = face->box_line_width;
25444
25445 if (thick > 0)
25446 {
25447 it->ascent += thick;
25448 it->descent += thick;
25449 }
25450 else
25451 thick = -thick;
25452
25453 if (it->start_of_box_run_p)
25454 it->pixel_width += thick;
25455 if (it->end_of_box_run_p)
25456 it->pixel_width += thick;
25457 }
25458
25459 /* If face has an overline, add the height of the overline
25460 (1 pixel) and a 1 pixel margin to the character height. */
25461 if (face->overline_p)
25462 it->ascent += overline_margin;
25463
25464 if (it->constrain_row_ascent_descent_p)
25465 {
25466 if (it->ascent > it->max_ascent)
25467 it->ascent = it->max_ascent;
25468 if (it->descent > it->max_descent)
25469 it->descent = it->max_descent;
25470 }
25471
25472 take_vertical_position_into_account (it);
25473
25474 /* If we have to actually produce glyphs, do it. */
25475 if (it->glyph_row)
25476 {
25477 if (stretched_p)
25478 {
25479 /* Translate a space with a `space-width' property
25480 into a stretch glyph. */
25481 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25482 / FONT_HEIGHT (font));
25483 append_stretch_glyph (it, it->object, it->pixel_width,
25484 it->ascent + it->descent, ascent);
25485 }
25486 else
25487 append_glyph (it);
25488
25489 /* If characters with lbearing or rbearing are displayed
25490 in this line, record that fact in a flag of the
25491 glyph row. This is used to optimize X output code. */
25492 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25493 it->glyph_row->contains_overlapping_glyphs_p = 1;
25494 }
25495 if (! stretched_p && it->pixel_width == 0)
25496 /* We assure that all visible glyphs have at least 1-pixel
25497 width. */
25498 it->pixel_width = 1;
25499 }
25500 else if (it->char_to_display == '\n')
25501 {
25502 /* A newline has no width, but we need the height of the
25503 line. But if previous part of the line sets a height,
25504 don't increase that height */
25505
25506 Lisp_Object height;
25507 Lisp_Object total_height = Qnil;
25508
25509 it->override_ascent = -1;
25510 it->pixel_width = 0;
25511 it->nglyphs = 0;
25512
25513 height = get_it_property (it, Qline_height);
25514 /* Split (line-height total-height) list */
25515 if (CONSP (height)
25516 && CONSP (XCDR (height))
25517 && NILP (XCDR (XCDR (height))))
25518 {
25519 total_height = XCAR (XCDR (height));
25520 height = XCAR (height);
25521 }
25522 height = calc_line_height_property (it, height, font, boff, 1);
25523
25524 if (it->override_ascent >= 0)
25525 {
25526 it->ascent = it->override_ascent;
25527 it->descent = it->override_descent;
25528 boff = it->override_boff;
25529 }
25530 else
25531 {
25532 it->ascent = FONT_BASE (font) + boff;
25533 it->descent = FONT_DESCENT (font) - boff;
25534 }
25535
25536 if (EQ (height, Qt))
25537 {
25538 if (it->descent > it->max_descent)
25539 {
25540 it->ascent += it->descent - it->max_descent;
25541 it->descent = it->max_descent;
25542 }
25543 if (it->ascent > it->max_ascent)
25544 {
25545 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25546 it->ascent = it->max_ascent;
25547 }
25548 it->phys_ascent = min (it->phys_ascent, it->ascent);
25549 it->phys_descent = min (it->phys_descent, it->descent);
25550 it->constrain_row_ascent_descent_p = 1;
25551 extra_line_spacing = 0;
25552 }
25553 else
25554 {
25555 Lisp_Object spacing;
25556
25557 it->phys_ascent = it->ascent;
25558 it->phys_descent = it->descent;
25559
25560 if ((it->max_ascent > 0 || it->max_descent > 0)
25561 && face->box != FACE_NO_BOX
25562 && face->box_line_width > 0)
25563 {
25564 it->ascent += face->box_line_width;
25565 it->descent += face->box_line_width;
25566 }
25567 if (!NILP (height)
25568 && XINT (height) > it->ascent + it->descent)
25569 it->ascent = XINT (height) - it->descent;
25570
25571 if (!NILP (total_height))
25572 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25573 else
25574 {
25575 spacing = get_it_property (it, Qline_spacing);
25576 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25577 }
25578 if (INTEGERP (spacing))
25579 {
25580 extra_line_spacing = XINT (spacing);
25581 if (!NILP (total_height))
25582 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25583 }
25584 }
25585 }
25586 else /* i.e. (it->char_to_display == '\t') */
25587 {
25588 if (font->space_width > 0)
25589 {
25590 int tab_width = it->tab_width * font->space_width;
25591 int x = it->current_x + it->continuation_lines_width;
25592 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25593
25594 /* If the distance from the current position to the next tab
25595 stop is less than a space character width, use the
25596 tab stop after that. */
25597 if (next_tab_x - x < font->space_width)
25598 next_tab_x += tab_width;
25599
25600 it->pixel_width = next_tab_x - x;
25601 it->nglyphs = 1;
25602 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25603 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25604
25605 if (it->glyph_row)
25606 {
25607 append_stretch_glyph (it, it->object, it->pixel_width,
25608 it->ascent + it->descent, it->ascent);
25609 }
25610 }
25611 else
25612 {
25613 it->pixel_width = 0;
25614 it->nglyphs = 1;
25615 }
25616 }
25617 }
25618 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25619 {
25620 /* A static composition.
25621
25622 Note: A composition is represented as one glyph in the
25623 glyph matrix. There are no padding glyphs.
25624
25625 Important note: pixel_width, ascent, and descent are the
25626 values of what is drawn by draw_glyphs (i.e. the values of
25627 the overall glyphs composed). */
25628 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25629 int boff; /* baseline offset */
25630 struct composition *cmp = composition_table[it->cmp_it.id];
25631 int glyph_len = cmp->glyph_len;
25632 struct font *font = face->font;
25633
25634 it->nglyphs = 1;
25635
25636 /* If we have not yet calculated pixel size data of glyphs of
25637 the composition for the current face font, calculate them
25638 now. Theoretically, we have to check all fonts for the
25639 glyphs, but that requires much time and memory space. So,
25640 here we check only the font of the first glyph. This may
25641 lead to incorrect display, but it's very rare, and C-l
25642 (recenter-top-bottom) can correct the display anyway. */
25643 if (! cmp->font || cmp->font != font)
25644 {
25645 /* Ascent and descent of the font of the first character
25646 of this composition (adjusted by baseline offset).
25647 Ascent and descent of overall glyphs should not be less
25648 than these, respectively. */
25649 int font_ascent, font_descent, font_height;
25650 /* Bounding box of the overall glyphs. */
25651 int leftmost, rightmost, lowest, highest;
25652 int lbearing, rbearing;
25653 int i, width, ascent, descent;
25654 int left_padded = 0, right_padded = 0;
25655 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25656 XChar2b char2b;
25657 struct font_metrics *pcm;
25658 int font_not_found_p;
25659 ptrdiff_t pos;
25660
25661 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25662 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25663 break;
25664 if (glyph_len < cmp->glyph_len)
25665 right_padded = 1;
25666 for (i = 0; i < glyph_len; i++)
25667 {
25668 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25669 break;
25670 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25671 }
25672 if (i > 0)
25673 left_padded = 1;
25674
25675 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25676 : IT_CHARPOS (*it));
25677 /* If no suitable font is found, use the default font. */
25678 font_not_found_p = font == NULL;
25679 if (font_not_found_p)
25680 {
25681 face = face->ascii_face;
25682 font = face->font;
25683 }
25684 boff = font->baseline_offset;
25685 if (font->vertical_centering)
25686 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25687 font_ascent = FONT_BASE (font) + boff;
25688 font_descent = FONT_DESCENT (font) - boff;
25689 font_height = FONT_HEIGHT (font);
25690
25691 cmp->font = font;
25692
25693 pcm = NULL;
25694 if (! font_not_found_p)
25695 {
25696 get_char_face_and_encoding (it->f, c, it->face_id,
25697 &char2b, 0);
25698 pcm = get_per_char_metric (font, &char2b);
25699 }
25700
25701 /* Initialize the bounding box. */
25702 if (pcm)
25703 {
25704 width = cmp->glyph_len > 0 ? pcm->width : 0;
25705 ascent = pcm->ascent;
25706 descent = pcm->descent;
25707 lbearing = pcm->lbearing;
25708 rbearing = pcm->rbearing;
25709 }
25710 else
25711 {
25712 width = cmp->glyph_len > 0 ? font->space_width : 0;
25713 ascent = FONT_BASE (font);
25714 descent = FONT_DESCENT (font);
25715 lbearing = 0;
25716 rbearing = width;
25717 }
25718
25719 rightmost = width;
25720 leftmost = 0;
25721 lowest = - descent + boff;
25722 highest = ascent + boff;
25723
25724 if (! font_not_found_p
25725 && font->default_ascent
25726 && CHAR_TABLE_P (Vuse_default_ascent)
25727 && !NILP (Faref (Vuse_default_ascent,
25728 make_number (it->char_to_display))))
25729 highest = font->default_ascent + boff;
25730
25731 /* Draw the first glyph at the normal position. It may be
25732 shifted to right later if some other glyphs are drawn
25733 at the left. */
25734 cmp->offsets[i * 2] = 0;
25735 cmp->offsets[i * 2 + 1] = boff;
25736 cmp->lbearing = lbearing;
25737 cmp->rbearing = rbearing;
25738
25739 /* Set cmp->offsets for the remaining glyphs. */
25740 for (i++; i < glyph_len; i++)
25741 {
25742 int left, right, btm, top;
25743 int ch = COMPOSITION_GLYPH (cmp, i);
25744 int face_id;
25745 struct face *this_face;
25746
25747 if (ch == '\t')
25748 ch = ' ';
25749 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25750 this_face = FACE_FROM_ID (it->f, face_id);
25751 font = this_face->font;
25752
25753 if (font == NULL)
25754 pcm = NULL;
25755 else
25756 {
25757 get_char_face_and_encoding (it->f, ch, face_id,
25758 &char2b, 0);
25759 pcm = get_per_char_metric (font, &char2b);
25760 }
25761 if (! pcm)
25762 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25763 else
25764 {
25765 width = pcm->width;
25766 ascent = pcm->ascent;
25767 descent = pcm->descent;
25768 lbearing = pcm->lbearing;
25769 rbearing = pcm->rbearing;
25770 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25771 {
25772 /* Relative composition with or without
25773 alternate chars. */
25774 left = (leftmost + rightmost - width) / 2;
25775 btm = - descent + boff;
25776 if (font->relative_compose
25777 && (! CHAR_TABLE_P (Vignore_relative_composition)
25778 || NILP (Faref (Vignore_relative_composition,
25779 make_number (ch)))))
25780 {
25781
25782 if (- descent >= font->relative_compose)
25783 /* One extra pixel between two glyphs. */
25784 btm = highest + 1;
25785 else if (ascent <= 0)
25786 /* One extra pixel between two glyphs. */
25787 btm = lowest - 1 - ascent - descent;
25788 }
25789 }
25790 else
25791 {
25792 /* A composition rule is specified by an integer
25793 value that encodes global and new reference
25794 points (GREF and NREF). GREF and NREF are
25795 specified by numbers as below:
25796
25797 0---1---2 -- ascent
25798 | |
25799 | |
25800 | |
25801 9--10--11 -- center
25802 | |
25803 ---3---4---5--- baseline
25804 | |
25805 6---7---8 -- descent
25806 */
25807 int rule = COMPOSITION_RULE (cmp, i);
25808 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25809
25810 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25811 grefx = gref % 3, nrefx = nref % 3;
25812 grefy = gref / 3, nrefy = nref / 3;
25813 if (xoff)
25814 xoff = font_height * (xoff - 128) / 256;
25815 if (yoff)
25816 yoff = font_height * (yoff - 128) / 256;
25817
25818 left = (leftmost
25819 + grefx * (rightmost - leftmost) / 2
25820 - nrefx * width / 2
25821 + xoff);
25822
25823 btm = ((grefy == 0 ? highest
25824 : grefy == 1 ? 0
25825 : grefy == 2 ? lowest
25826 : (highest + lowest) / 2)
25827 - (nrefy == 0 ? ascent + descent
25828 : nrefy == 1 ? descent - boff
25829 : nrefy == 2 ? 0
25830 : (ascent + descent) / 2)
25831 + yoff);
25832 }
25833
25834 cmp->offsets[i * 2] = left;
25835 cmp->offsets[i * 2 + 1] = btm + descent;
25836
25837 /* Update the bounding box of the overall glyphs. */
25838 if (width > 0)
25839 {
25840 right = left + width;
25841 if (left < leftmost)
25842 leftmost = left;
25843 if (right > rightmost)
25844 rightmost = right;
25845 }
25846 top = btm + descent + ascent;
25847 if (top > highest)
25848 highest = top;
25849 if (btm < lowest)
25850 lowest = btm;
25851
25852 if (cmp->lbearing > left + lbearing)
25853 cmp->lbearing = left + lbearing;
25854 if (cmp->rbearing < left + rbearing)
25855 cmp->rbearing = left + rbearing;
25856 }
25857 }
25858
25859 /* If there are glyphs whose x-offsets are negative,
25860 shift all glyphs to the right and make all x-offsets
25861 non-negative. */
25862 if (leftmost < 0)
25863 {
25864 for (i = 0; i < cmp->glyph_len; i++)
25865 cmp->offsets[i * 2] -= leftmost;
25866 rightmost -= leftmost;
25867 cmp->lbearing -= leftmost;
25868 cmp->rbearing -= leftmost;
25869 }
25870
25871 if (left_padded && cmp->lbearing < 0)
25872 {
25873 for (i = 0; i < cmp->glyph_len; i++)
25874 cmp->offsets[i * 2] -= cmp->lbearing;
25875 rightmost -= cmp->lbearing;
25876 cmp->rbearing -= cmp->lbearing;
25877 cmp->lbearing = 0;
25878 }
25879 if (right_padded && rightmost < cmp->rbearing)
25880 {
25881 rightmost = cmp->rbearing;
25882 }
25883
25884 cmp->pixel_width = rightmost;
25885 cmp->ascent = highest;
25886 cmp->descent = - lowest;
25887 if (cmp->ascent < font_ascent)
25888 cmp->ascent = font_ascent;
25889 if (cmp->descent < font_descent)
25890 cmp->descent = font_descent;
25891 }
25892
25893 if (it->glyph_row
25894 && (cmp->lbearing < 0
25895 || cmp->rbearing > cmp->pixel_width))
25896 it->glyph_row->contains_overlapping_glyphs_p = 1;
25897
25898 it->pixel_width = cmp->pixel_width;
25899 it->ascent = it->phys_ascent = cmp->ascent;
25900 it->descent = it->phys_descent = cmp->descent;
25901 if (face->box != FACE_NO_BOX)
25902 {
25903 int thick = face->box_line_width;
25904
25905 if (thick > 0)
25906 {
25907 it->ascent += thick;
25908 it->descent += thick;
25909 }
25910 else
25911 thick = - thick;
25912
25913 if (it->start_of_box_run_p)
25914 it->pixel_width += thick;
25915 if (it->end_of_box_run_p)
25916 it->pixel_width += thick;
25917 }
25918
25919 /* If face has an overline, add the height of the overline
25920 (1 pixel) and a 1 pixel margin to the character height. */
25921 if (face->overline_p)
25922 it->ascent += overline_margin;
25923
25924 take_vertical_position_into_account (it);
25925 if (it->ascent < 0)
25926 it->ascent = 0;
25927 if (it->descent < 0)
25928 it->descent = 0;
25929
25930 if (it->glyph_row && cmp->glyph_len > 0)
25931 append_composite_glyph (it);
25932 }
25933 else if (it->what == IT_COMPOSITION)
25934 {
25935 /* A dynamic (automatic) composition. */
25936 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25937 Lisp_Object gstring;
25938 struct font_metrics metrics;
25939
25940 it->nglyphs = 1;
25941
25942 gstring = composition_gstring_from_id (it->cmp_it.id);
25943 it->pixel_width
25944 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25945 &metrics);
25946 if (it->glyph_row
25947 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25948 it->glyph_row->contains_overlapping_glyphs_p = 1;
25949 it->ascent = it->phys_ascent = metrics.ascent;
25950 it->descent = it->phys_descent = metrics.descent;
25951 if (face->box != FACE_NO_BOX)
25952 {
25953 int thick = face->box_line_width;
25954
25955 if (thick > 0)
25956 {
25957 it->ascent += thick;
25958 it->descent += thick;
25959 }
25960 else
25961 thick = - thick;
25962
25963 if (it->start_of_box_run_p)
25964 it->pixel_width += thick;
25965 if (it->end_of_box_run_p)
25966 it->pixel_width += thick;
25967 }
25968 /* If face has an overline, add the height of the overline
25969 (1 pixel) and a 1 pixel margin to the character height. */
25970 if (face->overline_p)
25971 it->ascent += overline_margin;
25972 take_vertical_position_into_account (it);
25973 if (it->ascent < 0)
25974 it->ascent = 0;
25975 if (it->descent < 0)
25976 it->descent = 0;
25977
25978 if (it->glyph_row)
25979 append_composite_glyph (it);
25980 }
25981 else if (it->what == IT_GLYPHLESS)
25982 produce_glyphless_glyph (it, 0, Qnil);
25983 else if (it->what == IT_IMAGE)
25984 produce_image_glyph (it);
25985 else if (it->what == IT_STRETCH)
25986 produce_stretch_glyph (it);
25987 #ifdef HAVE_XWIDGETS
25988 else if (it->what == IT_XWIDGET)
25989 produce_xwidget_glyph (it);
25990 #endif
25991 done:
25992 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25993 because this isn't true for images with `:ascent 100'. */
25994 eassert (it->ascent >= 0 && it->descent >= 0);
25995 if (it->area == TEXT_AREA)
25996 it->current_x += it->pixel_width;
25997
25998 if (extra_line_spacing > 0)
25999 {
26000 it->descent += extra_line_spacing;
26001 if (extra_line_spacing > it->max_extra_line_spacing)
26002 it->max_extra_line_spacing = extra_line_spacing;
26003 }
26004
26005 it->max_ascent = max (it->max_ascent, it->ascent);
26006 it->max_descent = max (it->max_descent, it->descent);
26007 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26008 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26009 }
26010
26011 /* EXPORT for RIF:
26012 Output LEN glyphs starting at START at the nominal cursor position.
26013 Advance the nominal cursor over the text. The global variable
26014 updated_row is the glyph row being updated, and updated_area is the
26015 area of that row being updated. */
26016
26017 void
26018 x_write_glyphs (struct window *w, struct glyph *start, int len)
26019 {
26020 int x, hpos, chpos = w->phys_cursor.hpos;
26021
26022 eassert (updated_row);
26023 /* When the window is hscrolled, cursor hpos can legitimately be out
26024 of bounds, but we draw the cursor at the corresponding window
26025 margin in that case. */
26026 if (!updated_row->reversed_p && chpos < 0)
26027 chpos = 0;
26028 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26029 chpos = updated_row->used[TEXT_AREA] - 1;
26030
26031 block_input ();
26032
26033 /* Write glyphs. */
26034
26035 hpos = start - updated_row->glyphs[updated_area];
26036 x = draw_glyphs (w, output_cursor.x,
26037 updated_row, updated_area,
26038 hpos, hpos + len,
26039 DRAW_NORMAL_TEXT, 0);
26040
26041 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26042 if (updated_area == TEXT_AREA
26043 && w->phys_cursor_on_p
26044 && w->phys_cursor.vpos == output_cursor.vpos
26045 && chpos >= hpos
26046 && chpos < hpos + len)
26047 w->phys_cursor_on_p = 0;
26048
26049 unblock_input ();
26050
26051 /* Advance the output cursor. */
26052 output_cursor.hpos += len;
26053 output_cursor.x = x;
26054 }
26055
26056
26057 /* EXPORT for RIF:
26058 Insert LEN glyphs from START at the nominal cursor position. */
26059
26060 void
26061 x_insert_glyphs (struct window *w, struct glyph *start, int len)
26062 {
26063 struct frame *f;
26064 int line_height, shift_by_width, shifted_region_width;
26065 struct glyph_row *row;
26066 struct glyph *glyph;
26067 int frame_x, frame_y;
26068 ptrdiff_t hpos;
26069
26070 eassert (updated_row);
26071 block_input ();
26072 f = XFRAME (WINDOW_FRAME (w));
26073
26074 /* Get the height of the line we are in. */
26075 row = updated_row;
26076 line_height = row->height;
26077
26078 /* Get the width of the glyphs to insert. */
26079 shift_by_width = 0;
26080 for (glyph = start; glyph < start + len; ++glyph)
26081 shift_by_width += glyph->pixel_width;
26082
26083 /* Get the width of the region to shift right. */
26084 shifted_region_width = (window_box_width (w, updated_area)
26085 - output_cursor.x
26086 - shift_by_width);
26087
26088 /* Shift right. */
26089 frame_x = window_box_left (w, updated_area) + output_cursor.x;
26090 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
26091
26092 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26093 line_height, shift_by_width);
26094
26095 /* Write the glyphs. */
26096 hpos = start - row->glyphs[updated_area];
26097 draw_glyphs (w, output_cursor.x, row, updated_area,
26098 hpos, hpos + len,
26099 DRAW_NORMAL_TEXT, 0);
26100
26101 /* Advance the output cursor. */
26102 output_cursor.hpos += len;
26103 output_cursor.x += shift_by_width;
26104 unblock_input ();
26105 }
26106
26107
26108 /* EXPORT for RIF:
26109 Erase the current text line from the nominal cursor position
26110 (inclusive) to pixel column TO_X (exclusive). The idea is that
26111 everything from TO_X onward is already erased.
26112
26113 TO_X is a pixel position relative to updated_area of currently
26114 updated window W. TO_X == -1 means clear to the end of this area. */
26115
26116 void
26117 x_clear_end_of_line (struct window *w, int to_x)
26118 {
26119 struct frame *f;
26120 int max_x, min_y, max_y;
26121 int from_x, from_y, to_y;
26122
26123 eassert (updated_row);
26124 f = XFRAME (w->frame);
26125
26126 if (updated_row->full_width_p)
26127 max_x = WINDOW_TOTAL_WIDTH (w);
26128 else
26129 max_x = window_box_width (w, updated_area);
26130 max_y = window_text_bottom_y (w);
26131
26132 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26133 of window. For TO_X > 0, truncate to end of drawing area. */
26134 if (to_x == 0)
26135 return;
26136 else if (to_x < 0)
26137 to_x = max_x;
26138 else
26139 to_x = min (to_x, max_x);
26140
26141 to_y = min (max_y, output_cursor.y + updated_row->height);
26142
26143 /* Notice if the cursor will be cleared by this operation. */
26144 if (!updated_row->full_width_p)
26145 notice_overwritten_cursor (w, updated_area,
26146 output_cursor.x, -1,
26147 updated_row->y,
26148 MATRIX_ROW_BOTTOM_Y (updated_row));
26149
26150 from_x = output_cursor.x;
26151
26152 /* Translate to frame coordinates. */
26153 if (updated_row->full_width_p)
26154 {
26155 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26156 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26157 }
26158 else
26159 {
26160 int area_left = window_box_left (w, updated_area);
26161 from_x += area_left;
26162 to_x += area_left;
26163 }
26164
26165 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26166 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
26167 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26168
26169 /* Prevent inadvertently clearing to end of the X window. */
26170 if (to_x > from_x && to_y > from_y)
26171 {
26172 block_input ();
26173 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26174 to_x - from_x, to_y - from_y);
26175 unblock_input ();
26176 }
26177 }
26178
26179 #endif /* HAVE_WINDOW_SYSTEM */
26180
26181
26182 \f
26183 /***********************************************************************
26184 Cursor types
26185 ***********************************************************************/
26186
26187 /* Value is the internal representation of the specified cursor type
26188 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26189 of the bar cursor. */
26190
26191 static enum text_cursor_kinds
26192 get_specified_cursor_type (Lisp_Object arg, int *width)
26193 {
26194 enum text_cursor_kinds type;
26195
26196 if (NILP (arg))
26197 return NO_CURSOR;
26198
26199 if (EQ (arg, Qbox))
26200 return FILLED_BOX_CURSOR;
26201
26202 if (EQ (arg, Qhollow))
26203 return HOLLOW_BOX_CURSOR;
26204
26205 if (EQ (arg, Qbar))
26206 {
26207 *width = 2;
26208 return BAR_CURSOR;
26209 }
26210
26211 if (CONSP (arg)
26212 && EQ (XCAR (arg), Qbar)
26213 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26214 {
26215 *width = XINT (XCDR (arg));
26216 return BAR_CURSOR;
26217 }
26218
26219 if (EQ (arg, Qhbar))
26220 {
26221 *width = 2;
26222 return HBAR_CURSOR;
26223 }
26224
26225 if (CONSP (arg)
26226 && EQ (XCAR (arg), Qhbar)
26227 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26228 {
26229 *width = XINT (XCDR (arg));
26230 return HBAR_CURSOR;
26231 }
26232
26233 /* Treat anything unknown as "hollow box cursor".
26234 It was bad to signal an error; people have trouble fixing
26235 .Xdefaults with Emacs, when it has something bad in it. */
26236 type = HOLLOW_BOX_CURSOR;
26237
26238 return type;
26239 }
26240
26241 /* Set the default cursor types for specified frame. */
26242 void
26243 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26244 {
26245 int width = 1;
26246 Lisp_Object tem;
26247
26248 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26249 FRAME_CURSOR_WIDTH (f) = width;
26250
26251 /* By default, set up the blink-off state depending on the on-state. */
26252
26253 tem = Fassoc (arg, Vblink_cursor_alist);
26254 if (!NILP (tem))
26255 {
26256 FRAME_BLINK_OFF_CURSOR (f)
26257 = get_specified_cursor_type (XCDR (tem), &width);
26258 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26259 }
26260 else
26261 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26262
26263 /* Make sure the cursor gets redrawn. */
26264 cursor_type_changed = 1;
26265 }
26266
26267
26268 #ifdef HAVE_WINDOW_SYSTEM
26269
26270 /* Return the cursor we want to be displayed in window W. Return
26271 width of bar/hbar cursor through WIDTH arg. Return with
26272 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26273 (i.e. if the `system caret' should track this cursor).
26274
26275 In a mini-buffer window, we want the cursor only to appear if we
26276 are reading input from this window. For the selected window, we
26277 want the cursor type given by the frame parameter or buffer local
26278 setting of cursor-type. If explicitly marked off, draw no cursor.
26279 In all other cases, we want a hollow box cursor. */
26280
26281 static enum text_cursor_kinds
26282 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26283 int *active_cursor)
26284 {
26285 struct frame *f = XFRAME (w->frame);
26286 struct buffer *b = XBUFFER (w->contents);
26287 int cursor_type = DEFAULT_CURSOR;
26288 Lisp_Object alt_cursor;
26289 int non_selected = 0;
26290
26291 *active_cursor = 1;
26292
26293 /* Echo area */
26294 if (cursor_in_echo_area
26295 && FRAME_HAS_MINIBUF_P (f)
26296 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26297 {
26298 if (w == XWINDOW (echo_area_window))
26299 {
26300 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26301 {
26302 *width = FRAME_CURSOR_WIDTH (f);
26303 return FRAME_DESIRED_CURSOR (f);
26304 }
26305 else
26306 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26307 }
26308
26309 *active_cursor = 0;
26310 non_selected = 1;
26311 }
26312
26313 /* Detect a nonselected window or nonselected frame. */
26314 else if (w != XWINDOW (f->selected_window)
26315 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26316 {
26317 *active_cursor = 0;
26318
26319 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26320 return NO_CURSOR;
26321
26322 non_selected = 1;
26323 }
26324
26325 /* Never display a cursor in a window in which cursor-type is nil. */
26326 if (NILP (BVAR (b, cursor_type)))
26327 return NO_CURSOR;
26328
26329 /* Get the normal cursor type for this window. */
26330 if (EQ (BVAR (b, cursor_type), Qt))
26331 {
26332 cursor_type = FRAME_DESIRED_CURSOR (f);
26333 *width = FRAME_CURSOR_WIDTH (f);
26334 }
26335 else
26336 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26337
26338 /* Use cursor-in-non-selected-windows instead
26339 for non-selected window or frame. */
26340 if (non_selected)
26341 {
26342 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26343 if (!EQ (Qt, alt_cursor))
26344 return get_specified_cursor_type (alt_cursor, width);
26345 /* t means modify the normal cursor type. */
26346 if (cursor_type == FILLED_BOX_CURSOR)
26347 cursor_type = HOLLOW_BOX_CURSOR;
26348 else if (cursor_type == BAR_CURSOR && *width > 1)
26349 --*width;
26350 return cursor_type;
26351 }
26352
26353 /* Use normal cursor if not blinked off. */
26354 if (!w->cursor_off_p)
26355 {
26356
26357 #ifdef HAVE_XWIDGETS
26358 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
26359 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
26360 return NO_CURSOR;
26361 }
26362 #endif
26363 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26364 {
26365 if (cursor_type == FILLED_BOX_CURSOR)
26366 {
26367 /* Using a block cursor on large images can be very annoying.
26368 So use a hollow cursor for "large" images.
26369 If image is not transparent (no mask), also use hollow cursor. */
26370 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26371 if (img != NULL && IMAGEP (img->spec))
26372 {
26373 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26374 where N = size of default frame font size.
26375 This should cover most of the "tiny" icons people may use. */
26376 if (!img->mask
26377 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26378 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26379 cursor_type = HOLLOW_BOX_CURSOR;
26380 }
26381 }
26382 else if (cursor_type != NO_CURSOR)
26383 {
26384 /* Display current only supports BOX and HOLLOW cursors for images.
26385 So for now, unconditionally use a HOLLOW cursor when cursor is
26386 not a solid box cursor. */
26387 cursor_type = HOLLOW_BOX_CURSOR;
26388 }
26389 }
26390 return cursor_type;
26391 }
26392
26393 /* Cursor is blinked off, so determine how to "toggle" it. */
26394
26395 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26396 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26397 return get_specified_cursor_type (XCDR (alt_cursor), width);
26398
26399 /* Then see if frame has specified a specific blink off cursor type. */
26400 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26401 {
26402 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26403 return FRAME_BLINK_OFF_CURSOR (f);
26404 }
26405
26406 #if 0
26407 /* Some people liked having a permanently visible blinking cursor,
26408 while others had very strong opinions against it. So it was
26409 decided to remove it. KFS 2003-09-03 */
26410
26411 /* Finally perform built-in cursor blinking:
26412 filled box <-> hollow box
26413 wide [h]bar <-> narrow [h]bar
26414 narrow [h]bar <-> no cursor
26415 other type <-> no cursor */
26416
26417 if (cursor_type == FILLED_BOX_CURSOR)
26418 return HOLLOW_BOX_CURSOR;
26419
26420 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26421 {
26422 *width = 1;
26423 return cursor_type;
26424 }
26425 #endif
26426
26427 return NO_CURSOR;
26428 }
26429
26430
26431 /* Notice when the text cursor of window W has been completely
26432 overwritten by a drawing operation that outputs glyphs in AREA
26433 starting at X0 and ending at X1 in the line starting at Y0 and
26434 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26435 the rest of the line after X0 has been written. Y coordinates
26436 are window-relative. */
26437
26438 static void
26439 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26440 int x0, int x1, int y0, int y1)
26441 {
26442 int cx0, cx1, cy0, cy1;
26443 struct glyph_row *row;
26444
26445 if (!w->phys_cursor_on_p)
26446 return;
26447 if (area != TEXT_AREA)
26448 return;
26449
26450 if (w->phys_cursor.vpos < 0
26451 || w->phys_cursor.vpos >= w->current_matrix->nrows
26452 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26453 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26454 return;
26455
26456 if (row->cursor_in_fringe_p)
26457 {
26458 row->cursor_in_fringe_p = 0;
26459 draw_fringe_bitmap (w, row, row->reversed_p);
26460 w->phys_cursor_on_p = 0;
26461 return;
26462 }
26463
26464 cx0 = w->phys_cursor.x;
26465 cx1 = cx0 + w->phys_cursor_width;
26466 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26467 return;
26468
26469 /* The cursor image will be completely removed from the
26470 screen if the output area intersects the cursor area in
26471 y-direction. When we draw in [y0 y1[, and some part of
26472 the cursor is at y < y0, that part must have been drawn
26473 before. When scrolling, the cursor is erased before
26474 actually scrolling, so we don't come here. When not
26475 scrolling, the rows above the old cursor row must have
26476 changed, and in this case these rows must have written
26477 over the cursor image.
26478
26479 Likewise if part of the cursor is below y1, with the
26480 exception of the cursor being in the first blank row at
26481 the buffer and window end because update_text_area
26482 doesn't draw that row. (Except when it does, but
26483 that's handled in update_text_area.) */
26484
26485 cy0 = w->phys_cursor.y;
26486 cy1 = cy0 + w->phys_cursor_height;
26487 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26488 return;
26489
26490 w->phys_cursor_on_p = 0;
26491 }
26492
26493 #endif /* HAVE_WINDOW_SYSTEM */
26494
26495 \f
26496 /************************************************************************
26497 Mouse Face
26498 ************************************************************************/
26499
26500 #ifdef HAVE_WINDOW_SYSTEM
26501
26502 /* EXPORT for RIF:
26503 Fix the display of area AREA of overlapping row ROW in window W
26504 with respect to the overlapping part OVERLAPS. */
26505
26506 void
26507 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26508 enum glyph_row_area area, int overlaps)
26509 {
26510 int i, x;
26511
26512 block_input ();
26513
26514 x = 0;
26515 for (i = 0; i < row->used[area];)
26516 {
26517 if (row->glyphs[area][i].overlaps_vertically_p)
26518 {
26519 int start = i, start_x = x;
26520
26521 do
26522 {
26523 x += row->glyphs[area][i].pixel_width;
26524 ++i;
26525 }
26526 while (i < row->used[area]
26527 && row->glyphs[area][i].overlaps_vertically_p);
26528
26529 draw_glyphs (w, start_x, row, area,
26530 start, i,
26531 DRAW_NORMAL_TEXT, overlaps);
26532 }
26533 else
26534 {
26535 x += row->glyphs[area][i].pixel_width;
26536 ++i;
26537 }
26538 }
26539
26540 unblock_input ();
26541 }
26542
26543
26544 /* EXPORT:
26545 Draw the cursor glyph of window W in glyph row ROW. See the
26546 comment of draw_glyphs for the meaning of HL. */
26547
26548 void
26549 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26550 enum draw_glyphs_face hl)
26551 {
26552 /* If cursor hpos is out of bounds, don't draw garbage. This can
26553 happen in mini-buffer windows when switching between echo area
26554 glyphs and mini-buffer. */
26555 if ((row->reversed_p
26556 ? (w->phys_cursor.hpos >= 0)
26557 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26558 {
26559 int on_p = w->phys_cursor_on_p;
26560 int x1;
26561 int hpos = w->phys_cursor.hpos;
26562
26563 /* When the window is hscrolled, cursor hpos can legitimately be
26564 out of bounds, but we draw the cursor at the corresponding
26565 window margin in that case. */
26566 if (!row->reversed_p && hpos < 0)
26567 hpos = 0;
26568 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26569 hpos = row->used[TEXT_AREA] - 1;
26570
26571 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26572 hl, 0);
26573 w->phys_cursor_on_p = on_p;
26574
26575 if (hl == DRAW_CURSOR)
26576 w->phys_cursor_width = x1 - w->phys_cursor.x;
26577 /* When we erase the cursor, and ROW is overlapped by other
26578 rows, make sure that these overlapping parts of other rows
26579 are redrawn. */
26580 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26581 {
26582 w->phys_cursor_width = x1 - w->phys_cursor.x;
26583
26584 if (row > w->current_matrix->rows
26585 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26586 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26587 OVERLAPS_ERASED_CURSOR);
26588
26589 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26590 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26591 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26592 OVERLAPS_ERASED_CURSOR);
26593 }
26594 }
26595 }
26596
26597
26598 /* EXPORT:
26599 Erase the image of a cursor of window W from the screen. */
26600
26601 void
26602 erase_phys_cursor (struct window *w)
26603 {
26604 struct frame *f = XFRAME (w->frame);
26605 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26606 int hpos = w->phys_cursor.hpos;
26607 int vpos = w->phys_cursor.vpos;
26608 int mouse_face_here_p = 0;
26609 struct glyph_matrix *active_glyphs = w->current_matrix;
26610 struct glyph_row *cursor_row;
26611 struct glyph *cursor_glyph;
26612 enum draw_glyphs_face hl;
26613
26614 /* No cursor displayed or row invalidated => nothing to do on the
26615 screen. */
26616 if (w->phys_cursor_type == NO_CURSOR)
26617 goto mark_cursor_off;
26618
26619 /* VPOS >= active_glyphs->nrows means that window has been resized.
26620 Don't bother to erase the cursor. */
26621 if (vpos >= active_glyphs->nrows)
26622 goto mark_cursor_off;
26623
26624 /* If row containing cursor is marked invalid, there is nothing we
26625 can do. */
26626 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26627 if (!cursor_row->enabled_p)
26628 goto mark_cursor_off;
26629
26630 /* If line spacing is > 0, old cursor may only be partially visible in
26631 window after split-window. So adjust visible height. */
26632 cursor_row->visible_height = min (cursor_row->visible_height,
26633 window_text_bottom_y (w) - cursor_row->y);
26634
26635 /* If row is completely invisible, don't attempt to delete a cursor which
26636 isn't there. This can happen if cursor is at top of a window, and
26637 we switch to a buffer with a header line in that window. */
26638 if (cursor_row->visible_height <= 0)
26639 goto mark_cursor_off;
26640
26641 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26642 if (cursor_row->cursor_in_fringe_p)
26643 {
26644 cursor_row->cursor_in_fringe_p = 0;
26645 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26646 goto mark_cursor_off;
26647 }
26648
26649 /* This can happen when the new row is shorter than the old one.
26650 In this case, either draw_glyphs or clear_end_of_line
26651 should have cleared the cursor. Note that we wouldn't be
26652 able to erase the cursor in this case because we don't have a
26653 cursor glyph at hand. */
26654 if ((cursor_row->reversed_p
26655 ? (w->phys_cursor.hpos < 0)
26656 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26657 goto mark_cursor_off;
26658
26659 /* When the window is hscrolled, cursor hpos can legitimately be out
26660 of bounds, but we draw the cursor at the corresponding window
26661 margin in that case. */
26662 if (!cursor_row->reversed_p && hpos < 0)
26663 hpos = 0;
26664 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26665 hpos = cursor_row->used[TEXT_AREA] - 1;
26666
26667 /* If the cursor is in the mouse face area, redisplay that when
26668 we clear the cursor. */
26669 if (! NILP (hlinfo->mouse_face_window)
26670 && coords_in_mouse_face_p (w, hpos, vpos)
26671 /* Don't redraw the cursor's spot in mouse face if it is at the
26672 end of a line (on a newline). The cursor appears there, but
26673 mouse highlighting does not. */
26674 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26675 mouse_face_here_p = 1;
26676
26677 /* Maybe clear the display under the cursor. */
26678 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26679 {
26680 int x, y, left_x;
26681 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26682 int width;
26683
26684 cursor_glyph = get_phys_cursor_glyph (w);
26685 if (cursor_glyph == NULL)
26686 goto mark_cursor_off;
26687
26688 width = cursor_glyph->pixel_width;
26689 left_x = window_box_left_offset (w, TEXT_AREA);
26690 x = w->phys_cursor.x;
26691 if (x < left_x)
26692 width -= left_x - x;
26693 width = min (width, window_box_width (w, TEXT_AREA) - x);
26694 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26695 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26696
26697 if (width > 0)
26698 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26699 }
26700
26701 /* Erase the cursor by redrawing the character underneath it. */
26702 if (mouse_face_here_p)
26703 hl = DRAW_MOUSE_FACE;
26704 else
26705 hl = DRAW_NORMAL_TEXT;
26706 draw_phys_cursor_glyph (w, cursor_row, hl);
26707
26708 mark_cursor_off:
26709 w->phys_cursor_on_p = 0;
26710 w->phys_cursor_type = NO_CURSOR;
26711 }
26712
26713
26714 /* EXPORT:
26715 Display or clear cursor of window W. If ON is zero, clear the
26716 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26717 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26718
26719 void
26720 display_and_set_cursor (struct window *w, int on,
26721 int hpos, int vpos, int x, int y)
26722 {
26723 struct frame *f = XFRAME (w->frame);
26724 int new_cursor_type;
26725 int new_cursor_width;
26726 int active_cursor;
26727 struct glyph_row *glyph_row;
26728 struct glyph *glyph;
26729
26730 /* This is pointless on invisible frames, and dangerous on garbaged
26731 windows and frames; in the latter case, the frame or window may
26732 be in the midst of changing its size, and x and y may be off the
26733 window. */
26734 if (! FRAME_VISIBLE_P (f)
26735 || FRAME_GARBAGED_P (f)
26736 || vpos >= w->current_matrix->nrows
26737 || hpos >= w->current_matrix->matrix_w)
26738 return;
26739
26740 /* If cursor is off and we want it off, return quickly. */
26741 if (!on && !w->phys_cursor_on_p)
26742 return;
26743
26744 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26745 /* If cursor row is not enabled, we don't really know where to
26746 display the cursor. */
26747 if (!glyph_row->enabled_p)
26748 {
26749 w->phys_cursor_on_p = 0;
26750 return;
26751 }
26752
26753 glyph = NULL;
26754 if (!glyph_row->exact_window_width_line_p
26755 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26756 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26757
26758 eassert (input_blocked_p ());
26759
26760 /* Set new_cursor_type to the cursor we want to be displayed. */
26761 new_cursor_type = get_window_cursor_type (w, glyph,
26762 &new_cursor_width, &active_cursor);
26763
26764 /* If cursor is currently being shown and we don't want it to be or
26765 it is in the wrong place, or the cursor type is not what we want,
26766 erase it. */
26767 if (w->phys_cursor_on_p
26768 && (!on
26769 || w->phys_cursor.x != x
26770 || w->phys_cursor.y != y
26771 || new_cursor_type != w->phys_cursor_type
26772 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26773 && new_cursor_width != w->phys_cursor_width)))
26774 erase_phys_cursor (w);
26775
26776 /* Don't check phys_cursor_on_p here because that flag is only set
26777 to zero in some cases where we know that the cursor has been
26778 completely erased, to avoid the extra work of erasing the cursor
26779 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26780 still not be visible, or it has only been partly erased. */
26781 if (on)
26782 {
26783 w->phys_cursor_ascent = glyph_row->ascent;
26784 w->phys_cursor_height = glyph_row->height;
26785
26786 /* Set phys_cursor_.* before x_draw_.* is called because some
26787 of them may need the information. */
26788 w->phys_cursor.x = x;
26789 w->phys_cursor.y = glyph_row->y;
26790 w->phys_cursor.hpos = hpos;
26791 w->phys_cursor.vpos = vpos;
26792 }
26793
26794 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26795 new_cursor_type, new_cursor_width,
26796 on, active_cursor);
26797 }
26798
26799
26800 /* Switch the display of W's cursor on or off, according to the value
26801 of ON. */
26802
26803 static void
26804 update_window_cursor (struct window *w, int on)
26805 {
26806 /* Don't update cursor in windows whose frame is in the process
26807 of being deleted. */
26808 if (w->current_matrix)
26809 {
26810 int hpos = w->phys_cursor.hpos;
26811 int vpos = w->phys_cursor.vpos;
26812 struct glyph_row *row;
26813
26814 if (vpos >= w->current_matrix->nrows
26815 || hpos >= w->current_matrix->matrix_w)
26816 return;
26817
26818 row = MATRIX_ROW (w->current_matrix, vpos);
26819
26820 /* When the window is hscrolled, cursor hpos can legitimately be
26821 out of bounds, but we draw the cursor at the corresponding
26822 window margin in that case. */
26823 if (!row->reversed_p && hpos < 0)
26824 hpos = 0;
26825 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26826 hpos = row->used[TEXT_AREA] - 1;
26827
26828 block_input ();
26829 display_and_set_cursor (w, on, hpos, vpos,
26830 w->phys_cursor.x, w->phys_cursor.y);
26831 unblock_input ();
26832 }
26833 }
26834
26835
26836 /* Call update_window_cursor with parameter ON_P on all leaf windows
26837 in the window tree rooted at W. */
26838
26839 static void
26840 update_cursor_in_window_tree (struct window *w, int on_p)
26841 {
26842 while (w)
26843 {
26844 if (WINDOWP (w->contents))
26845 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26846 else
26847 update_window_cursor (w, on_p);
26848
26849 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26850 }
26851 }
26852
26853
26854 /* EXPORT:
26855 Display the cursor on window W, or clear it, according to ON_P.
26856 Don't change the cursor's position. */
26857
26858 void
26859 x_update_cursor (struct frame *f, int on_p)
26860 {
26861 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26862 }
26863
26864
26865 /* EXPORT:
26866 Clear the cursor of window W to background color, and mark the
26867 cursor as not shown. This is used when the text where the cursor
26868 is about to be rewritten. */
26869
26870 void
26871 x_clear_cursor (struct window *w)
26872 {
26873 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26874 update_window_cursor (w, 0);
26875 }
26876
26877 #endif /* HAVE_WINDOW_SYSTEM */
26878
26879 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26880 and MSDOS. */
26881 static void
26882 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26883 int start_hpos, int end_hpos,
26884 enum draw_glyphs_face draw)
26885 {
26886 #ifdef HAVE_WINDOW_SYSTEM
26887 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26888 {
26889 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26890 return;
26891 }
26892 #endif
26893 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26894 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26895 #endif
26896 }
26897
26898 /* Display the active region described by mouse_face_* according to DRAW. */
26899
26900 static void
26901 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26902 {
26903 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26904 struct frame *f = XFRAME (WINDOW_FRAME (w));
26905
26906 if (/* If window is in the process of being destroyed, don't bother
26907 to do anything. */
26908 w->current_matrix != NULL
26909 /* Don't update mouse highlight if hidden */
26910 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26911 /* Recognize when we are called to operate on rows that don't exist
26912 anymore. This can happen when a window is split. */
26913 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26914 {
26915 int phys_cursor_on_p = w->phys_cursor_on_p;
26916 struct glyph_row *row, *first, *last;
26917
26918 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26919 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26920
26921 for (row = first; row <= last && row->enabled_p; ++row)
26922 {
26923 int start_hpos, end_hpos, start_x;
26924
26925 /* For all but the first row, the highlight starts at column 0. */
26926 if (row == first)
26927 {
26928 /* R2L rows have BEG and END in reversed order, but the
26929 screen drawing geometry is always left to right. So
26930 we need to mirror the beginning and end of the
26931 highlighted area in R2L rows. */
26932 if (!row->reversed_p)
26933 {
26934 start_hpos = hlinfo->mouse_face_beg_col;
26935 start_x = hlinfo->mouse_face_beg_x;
26936 }
26937 else if (row == last)
26938 {
26939 start_hpos = hlinfo->mouse_face_end_col;
26940 start_x = hlinfo->mouse_face_end_x;
26941 }
26942 else
26943 {
26944 start_hpos = 0;
26945 start_x = 0;
26946 }
26947 }
26948 else if (row->reversed_p && row == last)
26949 {
26950 start_hpos = hlinfo->mouse_face_end_col;
26951 start_x = hlinfo->mouse_face_end_x;
26952 }
26953 else
26954 {
26955 start_hpos = 0;
26956 start_x = 0;
26957 }
26958
26959 if (row == last)
26960 {
26961 if (!row->reversed_p)
26962 end_hpos = hlinfo->mouse_face_end_col;
26963 else if (row == first)
26964 end_hpos = hlinfo->mouse_face_beg_col;
26965 else
26966 {
26967 end_hpos = row->used[TEXT_AREA];
26968 if (draw == DRAW_NORMAL_TEXT)
26969 row->fill_line_p = 1; /* Clear to end of line */
26970 }
26971 }
26972 else if (row->reversed_p && row == first)
26973 end_hpos = hlinfo->mouse_face_beg_col;
26974 else
26975 {
26976 end_hpos = row->used[TEXT_AREA];
26977 if (draw == DRAW_NORMAL_TEXT)
26978 row->fill_line_p = 1; /* Clear to end of line */
26979 }
26980
26981 if (end_hpos > start_hpos)
26982 {
26983 draw_row_with_mouse_face (w, start_x, row,
26984 start_hpos, end_hpos, draw);
26985
26986 row->mouse_face_p
26987 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26988 }
26989 }
26990
26991 #ifdef HAVE_WINDOW_SYSTEM
26992 /* When we've written over the cursor, arrange for it to
26993 be displayed again. */
26994 if (FRAME_WINDOW_P (f)
26995 && phys_cursor_on_p && !w->phys_cursor_on_p)
26996 {
26997 int hpos = w->phys_cursor.hpos;
26998
26999 /* When the window is hscrolled, cursor hpos can legitimately be
27000 out of bounds, but we draw the cursor at the corresponding
27001 window margin in that case. */
27002 if (!row->reversed_p && hpos < 0)
27003 hpos = 0;
27004 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27005 hpos = row->used[TEXT_AREA] - 1;
27006
27007 block_input ();
27008 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27009 w->phys_cursor.x, w->phys_cursor.y);
27010 unblock_input ();
27011 }
27012 #endif /* HAVE_WINDOW_SYSTEM */
27013 }
27014
27015 #ifdef HAVE_WINDOW_SYSTEM
27016 /* Change the mouse cursor. */
27017 if (FRAME_WINDOW_P (f))
27018 {
27019 if (draw == DRAW_NORMAL_TEXT
27020 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27021 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27022 else if (draw == DRAW_MOUSE_FACE)
27023 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27024 else
27025 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27026 }
27027 #endif /* HAVE_WINDOW_SYSTEM */
27028 }
27029
27030 /* EXPORT:
27031 Clear out the mouse-highlighted active region.
27032 Redraw it un-highlighted first. Value is non-zero if mouse
27033 face was actually drawn unhighlighted. */
27034
27035 int
27036 clear_mouse_face (Mouse_HLInfo *hlinfo)
27037 {
27038 int cleared = 0;
27039
27040 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27041 {
27042 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27043 cleared = 1;
27044 }
27045
27046 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27047 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27048 hlinfo->mouse_face_window = Qnil;
27049 hlinfo->mouse_face_overlay = Qnil;
27050 return cleared;
27051 }
27052
27053 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
27054 within the mouse face on that window. */
27055 static int
27056 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27057 {
27058 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27059
27060 /* Quickly resolve the easy cases. */
27061 if (!(WINDOWP (hlinfo->mouse_face_window)
27062 && XWINDOW (hlinfo->mouse_face_window) == w))
27063 return 0;
27064 if (vpos < hlinfo->mouse_face_beg_row
27065 || vpos > hlinfo->mouse_face_end_row)
27066 return 0;
27067 if (vpos > hlinfo->mouse_face_beg_row
27068 && vpos < hlinfo->mouse_face_end_row)
27069 return 1;
27070
27071 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27072 {
27073 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27074 {
27075 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27076 return 1;
27077 }
27078 else if ((vpos == hlinfo->mouse_face_beg_row
27079 && hpos >= hlinfo->mouse_face_beg_col)
27080 || (vpos == hlinfo->mouse_face_end_row
27081 && hpos < hlinfo->mouse_face_end_col))
27082 return 1;
27083 }
27084 else
27085 {
27086 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27087 {
27088 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27089 return 1;
27090 }
27091 else if ((vpos == hlinfo->mouse_face_beg_row
27092 && hpos <= hlinfo->mouse_face_beg_col)
27093 || (vpos == hlinfo->mouse_face_end_row
27094 && hpos > hlinfo->mouse_face_end_col))
27095 return 1;
27096 }
27097 return 0;
27098 }
27099
27100
27101 /* EXPORT:
27102 Non-zero if physical cursor of window W is within mouse face. */
27103
27104 int
27105 cursor_in_mouse_face_p (struct window *w)
27106 {
27107 int hpos = w->phys_cursor.hpos;
27108 int vpos = w->phys_cursor.vpos;
27109 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27110
27111 /* When the window is hscrolled, cursor hpos can legitimately be out
27112 of bounds, but we draw the cursor at the corresponding window
27113 margin in that case. */
27114 if (!row->reversed_p && hpos < 0)
27115 hpos = 0;
27116 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27117 hpos = row->used[TEXT_AREA] - 1;
27118
27119 return coords_in_mouse_face_p (w, hpos, vpos);
27120 }
27121
27122
27123 \f
27124 /* Find the glyph rows START_ROW and END_ROW of window W that display
27125 characters between buffer positions START_CHARPOS and END_CHARPOS
27126 (excluding END_CHARPOS). DISP_STRING is a display string that
27127 covers these buffer positions. This is similar to
27128 row_containing_pos, but is more accurate when bidi reordering makes
27129 buffer positions change non-linearly with glyph rows. */
27130 static void
27131 rows_from_pos_range (struct window *w,
27132 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27133 Lisp_Object disp_string,
27134 struct glyph_row **start, struct glyph_row **end)
27135 {
27136 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27137 int last_y = window_text_bottom_y (w);
27138 struct glyph_row *row;
27139
27140 *start = NULL;
27141 *end = NULL;
27142
27143 while (!first->enabled_p
27144 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27145 first++;
27146
27147 /* Find the START row. */
27148 for (row = first;
27149 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27150 row++)
27151 {
27152 /* A row can potentially be the START row if the range of the
27153 characters it displays intersects the range
27154 [START_CHARPOS..END_CHARPOS). */
27155 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27156 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27157 /* See the commentary in row_containing_pos, for the
27158 explanation of the complicated way to check whether
27159 some position is beyond the end of the characters
27160 displayed by a row. */
27161 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27162 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27163 && !row->ends_at_zv_p
27164 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27165 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27166 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27167 && !row->ends_at_zv_p
27168 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27169 {
27170 /* Found a candidate row. Now make sure at least one of the
27171 glyphs it displays has a charpos from the range
27172 [START_CHARPOS..END_CHARPOS).
27173
27174 This is not obvious because bidi reordering could make
27175 buffer positions of a row be 1,2,3,102,101,100, and if we
27176 want to highlight characters in [50..60), we don't want
27177 this row, even though [50..60) does intersect [1..103),
27178 the range of character positions given by the row's start
27179 and end positions. */
27180 struct glyph *g = row->glyphs[TEXT_AREA];
27181 struct glyph *e = g + row->used[TEXT_AREA];
27182
27183 while (g < e)
27184 {
27185 if (((BUFFERP (g->object) || INTEGERP (g->object))
27186 && start_charpos <= g->charpos && g->charpos < end_charpos)
27187 /* A glyph that comes from DISP_STRING is by
27188 definition to be highlighted. */
27189 || EQ (g->object, disp_string))
27190 *start = row;
27191 g++;
27192 }
27193 if (*start)
27194 break;
27195 }
27196 }
27197
27198 /* Find the END row. */
27199 if (!*start
27200 /* If the last row is partially visible, start looking for END
27201 from that row, instead of starting from FIRST. */
27202 && !(row->enabled_p
27203 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27204 row = first;
27205 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27206 {
27207 struct glyph_row *next = row + 1;
27208 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27209
27210 if (!next->enabled_p
27211 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27212 /* The first row >= START whose range of displayed characters
27213 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27214 is the row END + 1. */
27215 || (start_charpos < next_start
27216 && end_charpos < next_start)
27217 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27218 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27219 && !next->ends_at_zv_p
27220 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27221 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27222 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27223 && !next->ends_at_zv_p
27224 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27225 {
27226 *end = row;
27227 break;
27228 }
27229 else
27230 {
27231 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27232 but none of the characters it displays are in the range, it is
27233 also END + 1. */
27234 struct glyph *g = next->glyphs[TEXT_AREA];
27235 struct glyph *s = g;
27236 struct glyph *e = g + next->used[TEXT_AREA];
27237
27238 while (g < e)
27239 {
27240 if (((BUFFERP (g->object) || INTEGERP (g->object))
27241 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27242 /* If the buffer position of the first glyph in
27243 the row is equal to END_CHARPOS, it means
27244 the last character to be highlighted is the
27245 newline of ROW, and we must consider NEXT as
27246 END, not END+1. */
27247 || (((!next->reversed_p && g == s)
27248 || (next->reversed_p && g == e - 1))
27249 && (g->charpos == end_charpos
27250 /* Special case for when NEXT is an
27251 empty line at ZV. */
27252 || (g->charpos == -1
27253 && !row->ends_at_zv_p
27254 && next_start == end_charpos)))))
27255 /* A glyph that comes from DISP_STRING is by
27256 definition to be highlighted. */
27257 || EQ (g->object, disp_string))
27258 break;
27259 g++;
27260 }
27261 if (g == e)
27262 {
27263 *end = row;
27264 break;
27265 }
27266 /* The first row that ends at ZV must be the last to be
27267 highlighted. */
27268 else if (next->ends_at_zv_p)
27269 {
27270 *end = next;
27271 break;
27272 }
27273 }
27274 }
27275 }
27276
27277 /* This function sets the mouse_face_* elements of HLINFO, assuming
27278 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27279 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27280 for the overlay or run of text properties specifying the mouse
27281 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27282 before-string and after-string that must also be highlighted.
27283 DISP_STRING, if non-nil, is a display string that may cover some
27284 or all of the highlighted text. */
27285
27286 static void
27287 mouse_face_from_buffer_pos (Lisp_Object window,
27288 Mouse_HLInfo *hlinfo,
27289 ptrdiff_t mouse_charpos,
27290 ptrdiff_t start_charpos,
27291 ptrdiff_t end_charpos,
27292 Lisp_Object before_string,
27293 Lisp_Object after_string,
27294 Lisp_Object disp_string)
27295 {
27296 struct window *w = XWINDOW (window);
27297 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27298 struct glyph_row *r1, *r2;
27299 struct glyph *glyph, *end;
27300 ptrdiff_t ignore, pos;
27301 int x;
27302
27303 eassert (NILP (disp_string) || STRINGP (disp_string));
27304 eassert (NILP (before_string) || STRINGP (before_string));
27305 eassert (NILP (after_string) || STRINGP (after_string));
27306
27307 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27308 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27309 if (r1 == NULL)
27310 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27311 /* If the before-string or display-string contains newlines,
27312 rows_from_pos_range skips to its last row. Move back. */
27313 if (!NILP (before_string) || !NILP (disp_string))
27314 {
27315 struct glyph_row *prev;
27316 while ((prev = r1 - 1, prev >= first)
27317 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27318 && prev->used[TEXT_AREA] > 0)
27319 {
27320 struct glyph *beg = prev->glyphs[TEXT_AREA];
27321 glyph = beg + prev->used[TEXT_AREA];
27322 while (--glyph >= beg && INTEGERP (glyph->object));
27323 if (glyph < beg
27324 || !(EQ (glyph->object, before_string)
27325 || EQ (glyph->object, disp_string)))
27326 break;
27327 r1 = prev;
27328 }
27329 }
27330 if (r2 == NULL)
27331 {
27332 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27333 hlinfo->mouse_face_past_end = 1;
27334 }
27335 else if (!NILP (after_string))
27336 {
27337 /* If the after-string has newlines, advance to its last row. */
27338 struct glyph_row *next;
27339 struct glyph_row *last
27340 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27341
27342 for (next = r2 + 1;
27343 next <= last
27344 && next->used[TEXT_AREA] > 0
27345 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27346 ++next)
27347 r2 = next;
27348 }
27349 /* The rest of the display engine assumes that mouse_face_beg_row is
27350 either above mouse_face_end_row or identical to it. But with
27351 bidi-reordered continued lines, the row for START_CHARPOS could
27352 be below the row for END_CHARPOS. If so, swap the rows and store
27353 them in correct order. */
27354 if (r1->y > r2->y)
27355 {
27356 struct glyph_row *tem = r2;
27357
27358 r2 = r1;
27359 r1 = tem;
27360 }
27361
27362 hlinfo->mouse_face_beg_y = r1->y;
27363 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27364 hlinfo->mouse_face_end_y = r2->y;
27365 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27366
27367 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27368 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27369 could be anywhere in the row and in any order. The strategy
27370 below is to find the leftmost and the rightmost glyph that
27371 belongs to either of these 3 strings, or whose position is
27372 between START_CHARPOS and END_CHARPOS, and highlight all the
27373 glyphs between those two. This may cover more than just the text
27374 between START_CHARPOS and END_CHARPOS if the range of characters
27375 strides the bidi level boundary, e.g. if the beginning is in R2L
27376 text while the end is in L2R text or vice versa. */
27377 if (!r1->reversed_p)
27378 {
27379 /* This row is in a left to right paragraph. Scan it left to
27380 right. */
27381 glyph = r1->glyphs[TEXT_AREA];
27382 end = glyph + r1->used[TEXT_AREA];
27383 x = r1->x;
27384
27385 /* Skip truncation glyphs at the start of the glyph row. */
27386 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27387 for (; glyph < end
27388 && INTEGERP (glyph->object)
27389 && glyph->charpos < 0;
27390 ++glyph)
27391 x += glyph->pixel_width;
27392
27393 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27394 or DISP_STRING, and the first glyph from buffer whose
27395 position is between START_CHARPOS and END_CHARPOS. */
27396 for (; glyph < end
27397 && !INTEGERP (glyph->object)
27398 && !EQ (glyph->object, disp_string)
27399 && !(BUFFERP (glyph->object)
27400 && (glyph->charpos >= start_charpos
27401 && glyph->charpos < end_charpos));
27402 ++glyph)
27403 {
27404 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27405 are present at buffer positions between START_CHARPOS and
27406 END_CHARPOS, or if they come from an overlay. */
27407 if (EQ (glyph->object, before_string))
27408 {
27409 pos = string_buffer_position (before_string,
27410 start_charpos);
27411 /* If pos == 0, it means before_string came from an
27412 overlay, not from a buffer position. */
27413 if (!pos || (pos >= start_charpos && pos < end_charpos))
27414 break;
27415 }
27416 else if (EQ (glyph->object, after_string))
27417 {
27418 pos = string_buffer_position (after_string, end_charpos);
27419 if (!pos || (pos >= start_charpos && pos < end_charpos))
27420 break;
27421 }
27422 x += glyph->pixel_width;
27423 }
27424 hlinfo->mouse_face_beg_x = x;
27425 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27426 }
27427 else
27428 {
27429 /* This row is in a right to left paragraph. Scan it right to
27430 left. */
27431 struct glyph *g;
27432
27433 end = r1->glyphs[TEXT_AREA] - 1;
27434 glyph = end + r1->used[TEXT_AREA];
27435
27436 /* Skip truncation glyphs at the start of the glyph row. */
27437 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27438 for (; glyph > end
27439 && INTEGERP (glyph->object)
27440 && glyph->charpos < 0;
27441 --glyph)
27442 ;
27443
27444 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27445 or DISP_STRING, and the first glyph from buffer whose
27446 position is between START_CHARPOS and END_CHARPOS. */
27447 for (; glyph > end
27448 && !INTEGERP (glyph->object)
27449 && !EQ (glyph->object, disp_string)
27450 && !(BUFFERP (glyph->object)
27451 && (glyph->charpos >= start_charpos
27452 && glyph->charpos < end_charpos));
27453 --glyph)
27454 {
27455 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27456 are present at buffer positions between START_CHARPOS and
27457 END_CHARPOS, or if they come from an overlay. */
27458 if (EQ (glyph->object, before_string))
27459 {
27460 pos = string_buffer_position (before_string, start_charpos);
27461 /* If pos == 0, it means before_string came from an
27462 overlay, not from a buffer position. */
27463 if (!pos || (pos >= start_charpos && pos < end_charpos))
27464 break;
27465 }
27466 else if (EQ (glyph->object, after_string))
27467 {
27468 pos = string_buffer_position (after_string, end_charpos);
27469 if (!pos || (pos >= start_charpos && pos < end_charpos))
27470 break;
27471 }
27472 }
27473
27474 glyph++; /* first glyph to the right of the highlighted area */
27475 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27476 x += g->pixel_width;
27477 hlinfo->mouse_face_beg_x = x;
27478 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27479 }
27480
27481 /* If the highlight ends in a different row, compute GLYPH and END
27482 for the end row. Otherwise, reuse the values computed above for
27483 the row where the highlight begins. */
27484 if (r2 != r1)
27485 {
27486 if (!r2->reversed_p)
27487 {
27488 glyph = r2->glyphs[TEXT_AREA];
27489 end = glyph + r2->used[TEXT_AREA];
27490 x = r2->x;
27491 }
27492 else
27493 {
27494 end = r2->glyphs[TEXT_AREA] - 1;
27495 glyph = end + r2->used[TEXT_AREA];
27496 }
27497 }
27498
27499 if (!r2->reversed_p)
27500 {
27501 /* Skip truncation and continuation glyphs near the end of the
27502 row, and also blanks and stretch glyphs inserted by
27503 extend_face_to_end_of_line. */
27504 while (end > glyph
27505 && INTEGERP ((end - 1)->object))
27506 --end;
27507 /* Scan the rest of the glyph row from the end, looking for the
27508 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27509 DISP_STRING, or whose position is between START_CHARPOS
27510 and END_CHARPOS */
27511 for (--end;
27512 end > glyph
27513 && !INTEGERP (end->object)
27514 && !EQ (end->object, disp_string)
27515 && !(BUFFERP (end->object)
27516 && (end->charpos >= start_charpos
27517 && end->charpos < end_charpos));
27518 --end)
27519 {
27520 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27521 are present at buffer positions between START_CHARPOS and
27522 END_CHARPOS, or if they come from an overlay. */
27523 if (EQ (end->object, before_string))
27524 {
27525 pos = string_buffer_position (before_string, start_charpos);
27526 if (!pos || (pos >= start_charpos && pos < end_charpos))
27527 break;
27528 }
27529 else if (EQ (end->object, after_string))
27530 {
27531 pos = string_buffer_position (after_string, end_charpos);
27532 if (!pos || (pos >= start_charpos && pos < end_charpos))
27533 break;
27534 }
27535 }
27536 /* Find the X coordinate of the last glyph to be highlighted. */
27537 for (; glyph <= end; ++glyph)
27538 x += glyph->pixel_width;
27539
27540 hlinfo->mouse_face_end_x = x;
27541 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27542 }
27543 else
27544 {
27545 /* Skip truncation and continuation glyphs near the end of the
27546 row, and also blanks and stretch glyphs inserted by
27547 extend_face_to_end_of_line. */
27548 x = r2->x;
27549 end++;
27550 while (end < glyph
27551 && INTEGERP (end->object))
27552 {
27553 x += end->pixel_width;
27554 ++end;
27555 }
27556 /* Scan the rest of the glyph row from the end, looking for the
27557 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27558 DISP_STRING, or whose position is between START_CHARPOS
27559 and END_CHARPOS */
27560 for ( ;
27561 end < glyph
27562 && !INTEGERP (end->object)
27563 && !EQ (end->object, disp_string)
27564 && !(BUFFERP (end->object)
27565 && (end->charpos >= start_charpos
27566 && end->charpos < end_charpos));
27567 ++end)
27568 {
27569 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27570 are present at buffer positions between START_CHARPOS and
27571 END_CHARPOS, or if they come from an overlay. */
27572 if (EQ (end->object, before_string))
27573 {
27574 pos = string_buffer_position (before_string, start_charpos);
27575 if (!pos || (pos >= start_charpos && pos < end_charpos))
27576 break;
27577 }
27578 else if (EQ (end->object, after_string))
27579 {
27580 pos = string_buffer_position (after_string, end_charpos);
27581 if (!pos || (pos >= start_charpos && pos < end_charpos))
27582 break;
27583 }
27584 x += end->pixel_width;
27585 }
27586 /* If we exited the above loop because we arrived at the last
27587 glyph of the row, and its buffer position is still not in
27588 range, it means the last character in range is the preceding
27589 newline. Bump the end column and x values to get past the
27590 last glyph. */
27591 if (end == glyph
27592 && BUFFERP (end->object)
27593 && (end->charpos < start_charpos
27594 || end->charpos >= end_charpos))
27595 {
27596 x += end->pixel_width;
27597 ++end;
27598 }
27599 hlinfo->mouse_face_end_x = x;
27600 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27601 }
27602
27603 hlinfo->mouse_face_window = window;
27604 hlinfo->mouse_face_face_id
27605 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27606 mouse_charpos + 1,
27607 !hlinfo->mouse_face_hidden, -1);
27608 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27609 }
27610
27611 /* The following function is not used anymore (replaced with
27612 mouse_face_from_string_pos), but I leave it here for the time
27613 being, in case someone would. */
27614
27615 #if 0 /* not used */
27616
27617 /* Find the position of the glyph for position POS in OBJECT in
27618 window W's current matrix, and return in *X, *Y the pixel
27619 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27620
27621 RIGHT_P non-zero means return the position of the right edge of the
27622 glyph, RIGHT_P zero means return the left edge position.
27623
27624 If no glyph for POS exists in the matrix, return the position of
27625 the glyph with the next smaller position that is in the matrix, if
27626 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27627 exists in the matrix, return the position of the glyph with the
27628 next larger position in OBJECT.
27629
27630 Value is non-zero if a glyph was found. */
27631
27632 static int
27633 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27634 int *hpos, int *vpos, int *x, int *y, int right_p)
27635 {
27636 int yb = window_text_bottom_y (w);
27637 struct glyph_row *r;
27638 struct glyph *best_glyph = NULL;
27639 struct glyph_row *best_row = NULL;
27640 int best_x = 0;
27641
27642 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27643 r->enabled_p && r->y < yb;
27644 ++r)
27645 {
27646 struct glyph *g = r->glyphs[TEXT_AREA];
27647 struct glyph *e = g + r->used[TEXT_AREA];
27648 int gx;
27649
27650 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27651 if (EQ (g->object, object))
27652 {
27653 if (g->charpos == pos)
27654 {
27655 best_glyph = g;
27656 best_x = gx;
27657 best_row = r;
27658 goto found;
27659 }
27660 else if (best_glyph == NULL
27661 || ((eabs (g->charpos - pos)
27662 < eabs (best_glyph->charpos - pos))
27663 && (right_p
27664 ? g->charpos < pos
27665 : g->charpos > pos)))
27666 {
27667 best_glyph = g;
27668 best_x = gx;
27669 best_row = r;
27670 }
27671 }
27672 }
27673
27674 found:
27675
27676 if (best_glyph)
27677 {
27678 *x = best_x;
27679 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27680
27681 if (right_p)
27682 {
27683 *x += best_glyph->pixel_width;
27684 ++*hpos;
27685 }
27686
27687 *y = best_row->y;
27688 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27689 }
27690
27691 return best_glyph != NULL;
27692 }
27693 #endif /* not used */
27694
27695 /* Find the positions of the first and the last glyphs in window W's
27696 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27697 (assumed to be a string), and return in HLINFO's mouse_face_*
27698 members the pixel and column/row coordinates of those glyphs. */
27699
27700 static void
27701 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27702 Lisp_Object object,
27703 ptrdiff_t startpos, ptrdiff_t endpos)
27704 {
27705 int yb = window_text_bottom_y (w);
27706 struct glyph_row *r;
27707 struct glyph *g, *e;
27708 int gx;
27709 int found = 0;
27710
27711 /* Find the glyph row with at least one position in the range
27712 [STARTPOS..ENDPOS], and the first glyph in that row whose
27713 position belongs to that range. */
27714 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27715 r->enabled_p && r->y < yb;
27716 ++r)
27717 {
27718 if (!r->reversed_p)
27719 {
27720 g = r->glyphs[TEXT_AREA];
27721 e = g + r->used[TEXT_AREA];
27722 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27723 if (EQ (g->object, object)
27724 && startpos <= g->charpos && g->charpos <= endpos)
27725 {
27726 hlinfo->mouse_face_beg_row
27727 = MATRIX_ROW_VPOS (r, w->current_matrix);
27728 hlinfo->mouse_face_beg_y = r->y;
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_y = r->y;
27748 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27749 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27750 gx += g1->pixel_width;
27751 hlinfo->mouse_face_beg_x = gx;
27752 found = 1;
27753 break;
27754 }
27755 }
27756 if (found)
27757 break;
27758 }
27759
27760 if (!found)
27761 return;
27762
27763 /* Starting with the next row, look for the first row which does NOT
27764 include any glyphs whose positions are in the range. */
27765 for (++r; r->enabled_p && r->y < yb; ++r)
27766 {
27767 g = r->glyphs[TEXT_AREA];
27768 e = g + r->used[TEXT_AREA];
27769 found = 0;
27770 for ( ; g < e; ++g)
27771 if (EQ (g->object, object)
27772 && startpos <= g->charpos && g->charpos <= endpos)
27773 {
27774 found = 1;
27775 break;
27776 }
27777 if (!found)
27778 break;
27779 }
27780
27781 /* The highlighted region ends on the previous row. */
27782 r--;
27783
27784 /* Set the end row and its vertical pixel coordinate. */
27785 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27786 hlinfo->mouse_face_end_y = r->y;
27787
27788 /* Compute and set the end column and the end column's horizontal
27789 pixel coordinate. */
27790 if (!r->reversed_p)
27791 {
27792 g = r->glyphs[TEXT_AREA];
27793 e = g + r->used[TEXT_AREA];
27794 for ( ; e > g; --e)
27795 if (EQ ((e-1)->object, object)
27796 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27797 break;
27798 hlinfo->mouse_face_end_col = e - g;
27799
27800 for (gx = r->x; g < e; ++g)
27801 gx += g->pixel_width;
27802 hlinfo->mouse_face_end_x = gx;
27803 }
27804 else
27805 {
27806 e = r->glyphs[TEXT_AREA];
27807 g = e + r->used[TEXT_AREA];
27808 for (gx = r->x ; e < g; ++e)
27809 {
27810 if (EQ (e->object, object)
27811 && startpos <= e->charpos && e->charpos <= endpos)
27812 break;
27813 gx += e->pixel_width;
27814 }
27815 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27816 hlinfo->mouse_face_end_x = gx;
27817 }
27818 }
27819
27820 #ifdef HAVE_WINDOW_SYSTEM
27821
27822 /* See if position X, Y is within a hot-spot of an image. */
27823
27824 static int
27825 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27826 {
27827 if (!CONSP (hot_spot))
27828 return 0;
27829
27830 if (EQ (XCAR (hot_spot), Qrect))
27831 {
27832 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27833 Lisp_Object rect = XCDR (hot_spot);
27834 Lisp_Object tem;
27835 if (!CONSP (rect))
27836 return 0;
27837 if (!CONSP (XCAR (rect)))
27838 return 0;
27839 if (!CONSP (XCDR (rect)))
27840 return 0;
27841 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27842 return 0;
27843 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27844 return 0;
27845 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27846 return 0;
27847 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27848 return 0;
27849 return 1;
27850 }
27851 else if (EQ (XCAR (hot_spot), Qcircle))
27852 {
27853 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27854 Lisp_Object circ = XCDR (hot_spot);
27855 Lisp_Object lr, lx0, ly0;
27856 if (CONSP (circ)
27857 && CONSP (XCAR (circ))
27858 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27859 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27860 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27861 {
27862 double r = XFLOATINT (lr);
27863 double dx = XINT (lx0) - x;
27864 double dy = XINT (ly0) - y;
27865 return (dx * dx + dy * dy <= r * r);
27866 }
27867 }
27868 else if (EQ (XCAR (hot_spot), Qpoly))
27869 {
27870 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27871 if (VECTORP (XCDR (hot_spot)))
27872 {
27873 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27874 Lisp_Object *poly = v->contents;
27875 ptrdiff_t n = v->header.size;
27876 ptrdiff_t i;
27877 int inside = 0;
27878 Lisp_Object lx, ly;
27879 int x0, y0;
27880
27881 /* Need an even number of coordinates, and at least 3 edges. */
27882 if (n < 6 || n & 1)
27883 return 0;
27884
27885 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27886 If count is odd, we are inside polygon. Pixels on edges
27887 may or may not be included depending on actual geometry of the
27888 polygon. */
27889 if ((lx = poly[n-2], !INTEGERP (lx))
27890 || (ly = poly[n-1], !INTEGERP (lx)))
27891 return 0;
27892 x0 = XINT (lx), y0 = XINT (ly);
27893 for (i = 0; i < n; i += 2)
27894 {
27895 int x1 = x0, y1 = y0;
27896 if ((lx = poly[i], !INTEGERP (lx))
27897 || (ly = poly[i+1], !INTEGERP (ly)))
27898 return 0;
27899 x0 = XINT (lx), y0 = XINT (ly);
27900
27901 /* Does this segment cross the X line? */
27902 if (x0 >= x)
27903 {
27904 if (x1 >= x)
27905 continue;
27906 }
27907 else if (x1 < x)
27908 continue;
27909 if (y > y0 && y > y1)
27910 continue;
27911 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27912 inside = !inside;
27913 }
27914 return inside;
27915 }
27916 }
27917 return 0;
27918 }
27919
27920 Lisp_Object
27921 find_hot_spot (Lisp_Object map, int x, int y)
27922 {
27923 while (CONSP (map))
27924 {
27925 if (CONSP (XCAR (map))
27926 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27927 return XCAR (map);
27928 map = XCDR (map);
27929 }
27930
27931 return Qnil;
27932 }
27933
27934 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27935 3, 3, 0,
27936 doc: /* Lookup in image map MAP coordinates X and Y.
27937 An image map is an alist where each element has the format (AREA ID PLIST).
27938 An AREA is specified as either a rectangle, a circle, or a polygon:
27939 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27940 pixel coordinates of the upper left and bottom right corners.
27941 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27942 and the radius of the circle; r may be a float or integer.
27943 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27944 vector describes one corner in the polygon.
27945 Returns the alist element for the first matching AREA in MAP. */)
27946 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27947 {
27948 if (NILP (map))
27949 return Qnil;
27950
27951 CHECK_NUMBER (x);
27952 CHECK_NUMBER (y);
27953
27954 return find_hot_spot (map,
27955 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27956 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27957 }
27958
27959
27960 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27961 static void
27962 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27963 {
27964 /* Do not change cursor shape while dragging mouse. */
27965 if (!NILP (do_mouse_tracking))
27966 return;
27967
27968 if (!NILP (pointer))
27969 {
27970 if (EQ (pointer, Qarrow))
27971 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27972 else if (EQ (pointer, Qhand))
27973 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27974 else if (EQ (pointer, Qtext))
27975 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27976 else if (EQ (pointer, intern ("hdrag")))
27977 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27978 #ifdef HAVE_X_WINDOWS
27979 else if (EQ (pointer, intern ("vdrag")))
27980 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27981 #endif
27982 else if (EQ (pointer, intern ("hourglass")))
27983 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27984 else if (EQ (pointer, Qmodeline))
27985 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27986 else
27987 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27988 }
27989
27990 if (cursor != No_Cursor)
27991 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27992 }
27993
27994 #endif /* HAVE_WINDOW_SYSTEM */
27995
27996 /* Take proper action when mouse has moved to the mode or header line
27997 or marginal area AREA of window W, x-position X and y-position Y.
27998 X is relative to the start of the text display area of W, so the
27999 width of bitmap areas and scroll bars must be subtracted to get a
28000 position relative to the start of the mode line. */
28001
28002 static void
28003 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28004 enum window_part area)
28005 {
28006 struct window *w = XWINDOW (window);
28007 struct frame *f = XFRAME (w->frame);
28008 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28009 #ifdef HAVE_WINDOW_SYSTEM
28010 Display_Info *dpyinfo;
28011 #endif
28012 Cursor cursor = No_Cursor;
28013 Lisp_Object pointer = Qnil;
28014 int dx, dy, width, height;
28015 ptrdiff_t charpos;
28016 Lisp_Object string, object = Qnil;
28017 Lisp_Object pos IF_LINT (= Qnil), help;
28018
28019 Lisp_Object mouse_face;
28020 int original_x_pixel = x;
28021 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28022 struct glyph_row *row IF_LINT (= 0);
28023
28024 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28025 {
28026 int x0;
28027 struct glyph *end;
28028
28029 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28030 returns them in row/column units! */
28031 string = mode_line_string (w, area, &x, &y, &charpos,
28032 &object, &dx, &dy, &width, &height);
28033
28034 row = (area == ON_MODE_LINE
28035 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28036 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28037
28038 /* Find the glyph under the mouse pointer. */
28039 if (row->mode_line_p && row->enabled_p)
28040 {
28041 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28042 end = glyph + row->used[TEXT_AREA];
28043
28044 for (x0 = original_x_pixel;
28045 glyph < end && x0 >= glyph->pixel_width;
28046 ++glyph)
28047 x0 -= glyph->pixel_width;
28048
28049 if (glyph >= end)
28050 glyph = NULL;
28051 }
28052 }
28053 else
28054 {
28055 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28056 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28057 returns them in row/column units! */
28058 string = marginal_area_string (w, area, &x, &y, &charpos,
28059 &object, &dx, &dy, &width, &height);
28060 }
28061
28062 help = Qnil;
28063
28064 #ifdef HAVE_WINDOW_SYSTEM
28065 if (IMAGEP (object))
28066 {
28067 Lisp_Object image_map, hotspot;
28068 if ((image_map = Fplist_get (XCDR (object), QCmap),
28069 !NILP (image_map))
28070 && (hotspot = find_hot_spot (image_map, dx, dy),
28071 CONSP (hotspot))
28072 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28073 {
28074 Lisp_Object plist;
28075
28076 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28077 If so, we could look for mouse-enter, mouse-leave
28078 properties in PLIST (and do something...). */
28079 hotspot = XCDR (hotspot);
28080 if (CONSP (hotspot)
28081 && (plist = XCAR (hotspot), CONSP (plist)))
28082 {
28083 pointer = Fplist_get (plist, Qpointer);
28084 if (NILP (pointer))
28085 pointer = Qhand;
28086 help = Fplist_get (plist, Qhelp_echo);
28087 if (!NILP (help))
28088 {
28089 help_echo_string = help;
28090 XSETWINDOW (help_echo_window, w);
28091 help_echo_object = w->contents;
28092 help_echo_pos = charpos;
28093 }
28094 }
28095 }
28096 if (NILP (pointer))
28097 pointer = Fplist_get (XCDR (object), QCpointer);
28098 }
28099 #endif /* HAVE_WINDOW_SYSTEM */
28100
28101 if (STRINGP (string))
28102 pos = make_number (charpos);
28103
28104 /* Set the help text and mouse pointer. If the mouse is on a part
28105 of the mode line without any text (e.g. past the right edge of
28106 the mode line text), use the default help text and pointer. */
28107 if (STRINGP (string) || area == ON_MODE_LINE)
28108 {
28109 /* Arrange to display the help by setting the global variables
28110 help_echo_string, help_echo_object, and help_echo_pos. */
28111 if (NILP (help))
28112 {
28113 if (STRINGP (string))
28114 help = Fget_text_property (pos, Qhelp_echo, string);
28115
28116 if (!NILP (help))
28117 {
28118 help_echo_string = help;
28119 XSETWINDOW (help_echo_window, w);
28120 help_echo_object = string;
28121 help_echo_pos = charpos;
28122 }
28123 else if (area == ON_MODE_LINE)
28124 {
28125 Lisp_Object default_help
28126 = buffer_local_value_1 (Qmode_line_default_help_echo,
28127 w->contents);
28128
28129 if (STRINGP (default_help))
28130 {
28131 help_echo_string = default_help;
28132 XSETWINDOW (help_echo_window, w);
28133 help_echo_object = Qnil;
28134 help_echo_pos = -1;
28135 }
28136 }
28137 }
28138
28139 #ifdef HAVE_WINDOW_SYSTEM
28140 /* Change the mouse pointer according to what is under it. */
28141 if (FRAME_WINDOW_P (f))
28142 {
28143 dpyinfo = FRAME_X_DISPLAY_INFO (f);
28144 if (STRINGP (string))
28145 {
28146 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28147
28148 if (NILP (pointer))
28149 pointer = Fget_text_property (pos, Qpointer, string);
28150
28151 /* Change the mouse pointer according to what is under X/Y. */
28152 if (NILP (pointer)
28153 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28154 {
28155 Lisp_Object map;
28156 map = Fget_text_property (pos, Qlocal_map, string);
28157 if (!KEYMAPP (map))
28158 map = Fget_text_property (pos, Qkeymap, string);
28159 if (!KEYMAPP (map))
28160 cursor = dpyinfo->vertical_scroll_bar_cursor;
28161 }
28162 }
28163 else
28164 /* Default mode-line pointer. */
28165 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28166 }
28167 #endif
28168 }
28169
28170 /* Change the mouse face according to what is under X/Y. */
28171 if (STRINGP (string))
28172 {
28173 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28174 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28175 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28176 && glyph)
28177 {
28178 Lisp_Object b, e;
28179
28180 struct glyph * tmp_glyph;
28181
28182 int gpos;
28183 int gseq_length;
28184 int total_pixel_width;
28185 ptrdiff_t begpos, endpos, ignore;
28186
28187 int vpos, hpos;
28188
28189 b = Fprevious_single_property_change (make_number (charpos + 1),
28190 Qmouse_face, string, Qnil);
28191 if (NILP (b))
28192 begpos = 0;
28193 else
28194 begpos = XINT (b);
28195
28196 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28197 if (NILP (e))
28198 endpos = SCHARS (string);
28199 else
28200 endpos = XINT (e);
28201
28202 /* Calculate the glyph position GPOS of GLYPH in the
28203 displayed string, relative to the beginning of the
28204 highlighted part of the string.
28205
28206 Note: GPOS is different from CHARPOS. CHARPOS is the
28207 position of GLYPH in the internal string object. A mode
28208 line string format has structures which are converted to
28209 a flattened string by the Emacs Lisp interpreter. The
28210 internal string is an element of those structures. The
28211 displayed string is the flattened string. */
28212 tmp_glyph = row_start_glyph;
28213 while (tmp_glyph < glyph
28214 && (!(EQ (tmp_glyph->object, glyph->object)
28215 && begpos <= tmp_glyph->charpos
28216 && tmp_glyph->charpos < endpos)))
28217 tmp_glyph++;
28218 gpos = glyph - tmp_glyph;
28219
28220 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28221 the highlighted part of the displayed string to which
28222 GLYPH belongs. Note: GSEQ_LENGTH is different from
28223 SCHARS (STRING), because the latter returns the length of
28224 the internal string. */
28225 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28226 tmp_glyph > glyph
28227 && (!(EQ (tmp_glyph->object, glyph->object)
28228 && begpos <= tmp_glyph->charpos
28229 && tmp_glyph->charpos < endpos));
28230 tmp_glyph--)
28231 ;
28232 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28233
28234 /* Calculate the total pixel width of all the glyphs between
28235 the beginning of the highlighted area and GLYPH. */
28236 total_pixel_width = 0;
28237 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28238 total_pixel_width += tmp_glyph->pixel_width;
28239
28240 /* Pre calculation of re-rendering position. Note: X is in
28241 column units here, after the call to mode_line_string or
28242 marginal_area_string. */
28243 hpos = x - gpos;
28244 vpos = (area == ON_MODE_LINE
28245 ? (w->current_matrix)->nrows - 1
28246 : 0);
28247
28248 /* If GLYPH's position is included in the region that is
28249 already drawn in mouse face, we have nothing to do. */
28250 if ( EQ (window, hlinfo->mouse_face_window)
28251 && (!row->reversed_p
28252 ? (hlinfo->mouse_face_beg_col <= hpos
28253 && hpos < hlinfo->mouse_face_end_col)
28254 /* In R2L rows we swap BEG and END, see below. */
28255 : (hlinfo->mouse_face_end_col <= hpos
28256 && hpos < hlinfo->mouse_face_beg_col))
28257 && hlinfo->mouse_face_beg_row == vpos )
28258 return;
28259
28260 if (clear_mouse_face (hlinfo))
28261 cursor = No_Cursor;
28262
28263 if (!row->reversed_p)
28264 {
28265 hlinfo->mouse_face_beg_col = hpos;
28266 hlinfo->mouse_face_beg_x = original_x_pixel
28267 - (total_pixel_width + dx);
28268 hlinfo->mouse_face_end_col = hpos + gseq_length;
28269 hlinfo->mouse_face_end_x = 0;
28270 }
28271 else
28272 {
28273 /* In R2L rows, show_mouse_face expects BEG and END
28274 coordinates to be swapped. */
28275 hlinfo->mouse_face_end_col = hpos;
28276 hlinfo->mouse_face_end_x = original_x_pixel
28277 - (total_pixel_width + dx);
28278 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28279 hlinfo->mouse_face_beg_x = 0;
28280 }
28281
28282 hlinfo->mouse_face_beg_row = vpos;
28283 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28284 hlinfo->mouse_face_beg_y = 0;
28285 hlinfo->mouse_face_end_y = 0;
28286 hlinfo->mouse_face_past_end = 0;
28287 hlinfo->mouse_face_window = window;
28288
28289 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28290 charpos,
28291 0, 0, 0,
28292 &ignore,
28293 glyph->face_id,
28294 1);
28295 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28296
28297 if (NILP (pointer))
28298 pointer = Qhand;
28299 }
28300 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28301 clear_mouse_face (hlinfo);
28302 }
28303 #ifdef HAVE_WINDOW_SYSTEM
28304 if (FRAME_WINDOW_P (f))
28305 define_frame_cursor1 (f, cursor, pointer);
28306 #endif
28307 }
28308
28309
28310 /* EXPORT:
28311 Take proper action when the mouse has moved to position X, Y on
28312 frame F with regards to highlighting portions of display that have
28313 mouse-face properties. Also de-highlight portions of display where
28314 the mouse was before, set the mouse pointer shape as appropriate
28315 for the mouse coordinates, and activate help echo (tooltips).
28316 X and Y can be negative or out of range. */
28317
28318 void
28319 note_mouse_highlight (struct frame *f, int x, int y)
28320 {
28321 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28322 enum window_part part = ON_NOTHING;
28323 Lisp_Object window;
28324 struct window *w;
28325 Cursor cursor = No_Cursor;
28326 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28327 struct buffer *b;
28328
28329 /* When a menu is active, don't highlight because this looks odd. */
28330 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28331 if (popup_activated ())
28332 return;
28333 #endif
28334
28335 if (!f->glyphs_initialized_p
28336 || f->pointer_invisible)
28337 return;
28338
28339 hlinfo->mouse_face_mouse_x = x;
28340 hlinfo->mouse_face_mouse_y = y;
28341 hlinfo->mouse_face_mouse_frame = f;
28342
28343 if (hlinfo->mouse_face_defer)
28344 return;
28345
28346 /* Which window is that in? */
28347 window = window_from_coordinates (f, x, y, &part, 1);
28348
28349 /* If displaying active text in another window, clear that. */
28350 if (! EQ (window, hlinfo->mouse_face_window)
28351 /* Also clear if we move out of text area in same window. */
28352 || (!NILP (hlinfo->mouse_face_window)
28353 && !NILP (window)
28354 && part != ON_TEXT
28355 && part != ON_MODE_LINE
28356 && part != ON_HEADER_LINE))
28357 clear_mouse_face (hlinfo);
28358
28359 /* Not on a window -> return. */
28360 if (!WINDOWP (window))
28361 return;
28362
28363 /* Reset help_echo_string. It will get recomputed below. */
28364 help_echo_string = Qnil;
28365
28366 /* Convert to window-relative pixel coordinates. */
28367 w = XWINDOW (window);
28368 frame_to_window_pixel_xy (w, &x, &y);
28369
28370 #ifdef HAVE_WINDOW_SYSTEM
28371 /* Handle tool-bar window differently since it doesn't display a
28372 buffer. */
28373 if (EQ (window, f->tool_bar_window))
28374 {
28375 note_tool_bar_highlight (f, x, y);
28376 return;
28377 }
28378 #endif
28379
28380 /* Mouse is on the mode, header line or margin? */
28381 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28382 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28383 {
28384 note_mode_line_or_margin_highlight (window, x, y, part);
28385 return;
28386 }
28387
28388 #ifdef HAVE_WINDOW_SYSTEM
28389 if (part == ON_VERTICAL_BORDER)
28390 {
28391 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28392 help_echo_string = build_string ("drag-mouse-1: resize");
28393 }
28394 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28395 || part == ON_SCROLL_BAR)
28396 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28397 else
28398 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28399 #endif
28400
28401 /* Are we in a window whose display is up to date?
28402 And verify the buffer's text has not changed. */
28403 b = XBUFFER (w->contents);
28404 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28405 {
28406 int hpos, vpos, dx, dy, area = LAST_AREA;
28407 ptrdiff_t pos;
28408 struct glyph *glyph;
28409 Lisp_Object object;
28410 Lisp_Object mouse_face = Qnil, position;
28411 Lisp_Object *overlay_vec = NULL;
28412 ptrdiff_t i, noverlays;
28413 struct buffer *obuf;
28414 ptrdiff_t obegv, ozv;
28415 int same_region;
28416
28417 /* Find the glyph under X/Y. */
28418 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28419
28420 #ifdef HAVE_WINDOW_SYSTEM
28421 /* Look for :pointer property on image. */
28422 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28423 {
28424 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28425 if (img != NULL && IMAGEP (img->spec))
28426 {
28427 Lisp_Object image_map, hotspot;
28428 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28429 !NILP (image_map))
28430 && (hotspot = find_hot_spot (image_map,
28431 glyph->slice.img.x + dx,
28432 glyph->slice.img.y + dy),
28433 CONSP (hotspot))
28434 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28435 {
28436 Lisp_Object plist;
28437
28438 /* Could check XCAR (hotspot) to see if we enter/leave
28439 this hot-spot.
28440 If so, we could look for mouse-enter, mouse-leave
28441 properties in PLIST (and do something...). */
28442 hotspot = XCDR (hotspot);
28443 if (CONSP (hotspot)
28444 && (plist = XCAR (hotspot), CONSP (plist)))
28445 {
28446 pointer = Fplist_get (plist, Qpointer);
28447 if (NILP (pointer))
28448 pointer = Qhand;
28449 help_echo_string = Fplist_get (plist, Qhelp_echo);
28450 if (!NILP (help_echo_string))
28451 {
28452 help_echo_window = window;
28453 help_echo_object = glyph->object;
28454 help_echo_pos = glyph->charpos;
28455 }
28456 }
28457 }
28458 if (NILP (pointer))
28459 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28460 }
28461 }
28462 #endif /* HAVE_WINDOW_SYSTEM */
28463
28464 /* Clear mouse face if X/Y not over text. */
28465 if (glyph == NULL
28466 || area != TEXT_AREA
28467 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28468 /* Glyph's OBJECT is an integer for glyphs inserted by the
28469 display engine for its internal purposes, like truncation
28470 and continuation glyphs and blanks beyond the end of
28471 line's text on text terminals. If we are over such a
28472 glyph, we are not over any text. */
28473 || INTEGERP (glyph->object)
28474 /* R2L rows have a stretch glyph at their front, which
28475 stands for no text, whereas L2R rows have no glyphs at
28476 all beyond the end of text. Treat such stretch glyphs
28477 like we do with NULL glyphs in L2R rows. */
28478 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28479 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28480 && glyph->type == STRETCH_GLYPH
28481 && glyph->avoid_cursor_p))
28482 {
28483 if (clear_mouse_face (hlinfo))
28484 cursor = No_Cursor;
28485 #ifdef HAVE_WINDOW_SYSTEM
28486 if (FRAME_WINDOW_P (f) && NILP (pointer))
28487 {
28488 if (area != TEXT_AREA)
28489 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28490 else
28491 pointer = Vvoid_text_area_pointer;
28492 }
28493 #endif
28494 goto set_cursor;
28495 }
28496
28497 pos = glyph->charpos;
28498 object = glyph->object;
28499 if (!STRINGP (object) && !BUFFERP (object))
28500 goto set_cursor;
28501
28502 /* If we get an out-of-range value, return now; avoid an error. */
28503 if (BUFFERP (object) && pos > BUF_Z (b))
28504 goto set_cursor;
28505
28506 /* Make the window's buffer temporarily current for
28507 overlays_at and compute_char_face. */
28508 obuf = current_buffer;
28509 current_buffer = b;
28510 obegv = BEGV;
28511 ozv = ZV;
28512 BEGV = BEG;
28513 ZV = Z;
28514
28515 /* Is this char mouse-active or does it have help-echo? */
28516 position = make_number (pos);
28517
28518 if (BUFFERP (object))
28519 {
28520 /* Put all the overlays we want in a vector in overlay_vec. */
28521 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28522 /* Sort overlays into increasing priority order. */
28523 noverlays = sort_overlays (overlay_vec, noverlays, w);
28524 }
28525 else
28526 noverlays = 0;
28527
28528 if (NILP (Vmouse_highlight))
28529 {
28530 clear_mouse_face (hlinfo);
28531 goto check_help_echo;
28532 }
28533
28534 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28535
28536 if (same_region)
28537 cursor = No_Cursor;
28538
28539 /* Check mouse-face highlighting. */
28540 if (! same_region
28541 /* If there exists an overlay with mouse-face overlapping
28542 the one we are currently highlighting, we have to
28543 check if we enter the overlapping overlay, and then
28544 highlight only that. */
28545 || (OVERLAYP (hlinfo->mouse_face_overlay)
28546 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28547 {
28548 /* Find the highest priority overlay with a mouse-face. */
28549 Lisp_Object overlay = Qnil;
28550 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28551 {
28552 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28553 if (!NILP (mouse_face))
28554 overlay = overlay_vec[i];
28555 }
28556
28557 /* If we're highlighting the same overlay as before, there's
28558 no need to do that again. */
28559 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28560 goto check_help_echo;
28561 hlinfo->mouse_face_overlay = overlay;
28562
28563 /* Clear the display of the old active region, if any. */
28564 if (clear_mouse_face (hlinfo))
28565 cursor = No_Cursor;
28566
28567 /* If no overlay applies, get a text property. */
28568 if (NILP (overlay))
28569 mouse_face = Fget_text_property (position, Qmouse_face, object);
28570
28571 /* Next, compute the bounds of the mouse highlighting and
28572 display it. */
28573 if (!NILP (mouse_face) && STRINGP (object))
28574 {
28575 /* The mouse-highlighting comes from a display string
28576 with a mouse-face. */
28577 Lisp_Object s, e;
28578 ptrdiff_t ignore;
28579
28580 s = Fprevious_single_property_change
28581 (make_number (pos + 1), Qmouse_face, object, Qnil);
28582 e = Fnext_single_property_change
28583 (position, Qmouse_face, object, Qnil);
28584 if (NILP (s))
28585 s = make_number (0);
28586 if (NILP (e))
28587 e = make_number (SCHARS (object) - 1);
28588 mouse_face_from_string_pos (w, hlinfo, object,
28589 XINT (s), XINT (e));
28590 hlinfo->mouse_face_past_end = 0;
28591 hlinfo->mouse_face_window = window;
28592 hlinfo->mouse_face_face_id
28593 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28594 glyph->face_id, 1);
28595 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28596 cursor = No_Cursor;
28597 }
28598 else
28599 {
28600 /* The mouse-highlighting, if any, comes from an overlay
28601 or text property in the buffer. */
28602 Lisp_Object buffer IF_LINT (= Qnil);
28603 Lisp_Object disp_string IF_LINT (= Qnil);
28604
28605 if (STRINGP (object))
28606 {
28607 /* If we are on a display string with no mouse-face,
28608 check if the text under it has one. */
28609 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28610 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28611 pos = string_buffer_position (object, start);
28612 if (pos > 0)
28613 {
28614 mouse_face = get_char_property_and_overlay
28615 (make_number (pos), Qmouse_face, w->contents, &overlay);
28616 buffer = w->contents;
28617 disp_string = object;
28618 }
28619 }
28620 else
28621 {
28622 buffer = object;
28623 disp_string = Qnil;
28624 }
28625
28626 if (!NILP (mouse_face))
28627 {
28628 Lisp_Object before, after;
28629 Lisp_Object before_string, after_string;
28630 /* To correctly find the limits of mouse highlight
28631 in a bidi-reordered buffer, we must not use the
28632 optimization of limiting the search in
28633 previous-single-property-change and
28634 next-single-property-change, because
28635 rows_from_pos_range needs the real start and end
28636 positions to DTRT in this case. That's because
28637 the first row visible in a window does not
28638 necessarily display the character whose position
28639 is the smallest. */
28640 Lisp_Object lim1 =
28641 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28642 ? Fmarker_position (w->start)
28643 : Qnil;
28644 Lisp_Object lim2 =
28645 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28646 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28647 : Qnil;
28648
28649 if (NILP (overlay))
28650 {
28651 /* Handle the text property case. */
28652 before = Fprevious_single_property_change
28653 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28654 after = Fnext_single_property_change
28655 (make_number (pos), Qmouse_face, buffer, lim2);
28656 before_string = after_string = Qnil;
28657 }
28658 else
28659 {
28660 /* Handle the overlay case. */
28661 before = Foverlay_start (overlay);
28662 after = Foverlay_end (overlay);
28663 before_string = Foverlay_get (overlay, Qbefore_string);
28664 after_string = Foverlay_get (overlay, Qafter_string);
28665
28666 if (!STRINGP (before_string)) before_string = Qnil;
28667 if (!STRINGP (after_string)) after_string = Qnil;
28668 }
28669
28670 mouse_face_from_buffer_pos (window, hlinfo, pos,
28671 NILP (before)
28672 ? 1
28673 : XFASTINT (before),
28674 NILP (after)
28675 ? BUF_Z (XBUFFER (buffer))
28676 : XFASTINT (after),
28677 before_string, after_string,
28678 disp_string);
28679 cursor = No_Cursor;
28680 }
28681 }
28682 }
28683
28684 check_help_echo:
28685
28686 /* Look for a `help-echo' property. */
28687 if (NILP (help_echo_string)) {
28688 Lisp_Object help, overlay;
28689
28690 /* Check overlays first. */
28691 help = overlay = Qnil;
28692 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28693 {
28694 overlay = overlay_vec[i];
28695 help = Foverlay_get (overlay, Qhelp_echo);
28696 }
28697
28698 if (!NILP (help))
28699 {
28700 help_echo_string = help;
28701 help_echo_window = window;
28702 help_echo_object = overlay;
28703 help_echo_pos = pos;
28704 }
28705 else
28706 {
28707 Lisp_Object obj = glyph->object;
28708 ptrdiff_t charpos = glyph->charpos;
28709
28710 /* Try text properties. */
28711 if (STRINGP (obj)
28712 && charpos >= 0
28713 && charpos < SCHARS (obj))
28714 {
28715 help = Fget_text_property (make_number (charpos),
28716 Qhelp_echo, obj);
28717 if (NILP (help))
28718 {
28719 /* If the string itself doesn't specify a help-echo,
28720 see if the buffer text ``under'' it does. */
28721 struct glyph_row *r
28722 = MATRIX_ROW (w->current_matrix, vpos);
28723 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28724 ptrdiff_t p = string_buffer_position (obj, start);
28725 if (p > 0)
28726 {
28727 help = Fget_char_property (make_number (p),
28728 Qhelp_echo, w->contents);
28729 if (!NILP (help))
28730 {
28731 charpos = p;
28732 obj = w->contents;
28733 }
28734 }
28735 }
28736 }
28737 else if (BUFFERP (obj)
28738 && charpos >= BEGV
28739 && charpos < ZV)
28740 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28741 obj);
28742
28743 if (!NILP (help))
28744 {
28745 help_echo_string = help;
28746 help_echo_window = window;
28747 help_echo_object = obj;
28748 help_echo_pos = charpos;
28749 }
28750 }
28751 }
28752
28753 #ifdef HAVE_WINDOW_SYSTEM
28754 /* Look for a `pointer' property. */
28755 if (FRAME_WINDOW_P (f) && NILP (pointer))
28756 {
28757 /* Check overlays first. */
28758 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28759 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28760
28761 if (NILP (pointer))
28762 {
28763 Lisp_Object obj = glyph->object;
28764 ptrdiff_t charpos = glyph->charpos;
28765
28766 /* Try text properties. */
28767 if (STRINGP (obj)
28768 && charpos >= 0
28769 && charpos < SCHARS (obj))
28770 {
28771 pointer = Fget_text_property (make_number (charpos),
28772 Qpointer, obj);
28773 if (NILP (pointer))
28774 {
28775 /* If the string itself doesn't specify a pointer,
28776 see if the buffer text ``under'' it does. */
28777 struct glyph_row *r
28778 = MATRIX_ROW (w->current_matrix, vpos);
28779 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28780 ptrdiff_t p = string_buffer_position (obj, start);
28781 if (p > 0)
28782 pointer = Fget_char_property (make_number (p),
28783 Qpointer, w->contents);
28784 }
28785 }
28786 else if (BUFFERP (obj)
28787 && charpos >= BEGV
28788 && charpos < ZV)
28789 pointer = Fget_text_property (make_number (charpos),
28790 Qpointer, obj);
28791 }
28792 }
28793 #endif /* HAVE_WINDOW_SYSTEM */
28794
28795 BEGV = obegv;
28796 ZV = ozv;
28797 current_buffer = obuf;
28798 }
28799
28800 set_cursor:
28801
28802 #ifdef HAVE_WINDOW_SYSTEM
28803 if (FRAME_WINDOW_P (f))
28804 define_frame_cursor1 (f, cursor, pointer);
28805 #else
28806 /* This is here to prevent a compiler error, about "label at end of
28807 compound statement". */
28808 return;
28809 #endif
28810 }
28811
28812
28813 /* EXPORT for RIF:
28814 Clear any mouse-face on window W. This function is part of the
28815 redisplay interface, and is called from try_window_id and similar
28816 functions to ensure the mouse-highlight is off. */
28817
28818 void
28819 x_clear_window_mouse_face (struct window *w)
28820 {
28821 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28822 Lisp_Object window;
28823
28824 block_input ();
28825 XSETWINDOW (window, w);
28826 if (EQ (window, hlinfo->mouse_face_window))
28827 clear_mouse_face (hlinfo);
28828 unblock_input ();
28829 }
28830
28831
28832 /* EXPORT:
28833 Just discard the mouse face information for frame F, if any.
28834 This is used when the size of F is changed. */
28835
28836 void
28837 cancel_mouse_face (struct frame *f)
28838 {
28839 Lisp_Object window;
28840 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28841
28842 window = hlinfo->mouse_face_window;
28843 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28844 {
28845 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28846 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28847 hlinfo->mouse_face_window = Qnil;
28848 }
28849 }
28850
28851
28852 \f
28853 /***********************************************************************
28854 Exposure Events
28855 ***********************************************************************/
28856
28857 #ifdef HAVE_WINDOW_SYSTEM
28858
28859 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28860 which intersects rectangle R. R is in window-relative coordinates. */
28861
28862 static void
28863 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28864 enum glyph_row_area area)
28865 {
28866 struct glyph *first = row->glyphs[area];
28867 struct glyph *end = row->glyphs[area] + row->used[area];
28868 struct glyph *last;
28869 int first_x, start_x, x;
28870
28871 if (area == TEXT_AREA && row->fill_line_p)
28872 /* If row extends face to end of line write the whole line. */
28873 draw_glyphs (w, 0, row, area,
28874 0, row->used[area],
28875 DRAW_NORMAL_TEXT, 0);
28876 else
28877 {
28878 /* Set START_X to the window-relative start position for drawing glyphs of
28879 AREA. The first glyph of the text area can be partially visible.
28880 The first glyphs of other areas cannot. */
28881 start_x = window_box_left_offset (w, area);
28882 x = start_x;
28883 if (area == TEXT_AREA)
28884 x += row->x;
28885
28886 /* Find the first glyph that must be redrawn. */
28887 while (first < end
28888 && x + first->pixel_width < r->x)
28889 {
28890 x += first->pixel_width;
28891 ++first;
28892 }
28893
28894 /* Find the last one. */
28895 last = first;
28896 first_x = x;
28897 while (last < end
28898 && x < r->x + r->width)
28899 {
28900 x += last->pixel_width;
28901 ++last;
28902 }
28903
28904 /* Repaint. */
28905 if (last > first)
28906 draw_glyphs (w, first_x - start_x, row, area,
28907 first - row->glyphs[area], last - row->glyphs[area],
28908 DRAW_NORMAL_TEXT, 0);
28909 }
28910 }
28911
28912
28913 /* Redraw the parts of the glyph row ROW on window W intersecting
28914 rectangle R. R is in window-relative coordinates. Value is
28915 non-zero if mouse-face was overwritten. */
28916
28917 static int
28918 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28919 {
28920 eassert (row->enabled_p);
28921
28922 if (row->mode_line_p || w->pseudo_window_p)
28923 draw_glyphs (w, 0, row, TEXT_AREA,
28924 0, row->used[TEXT_AREA],
28925 DRAW_NORMAL_TEXT, 0);
28926 else
28927 {
28928 if (row->used[LEFT_MARGIN_AREA])
28929 expose_area (w, row, r, LEFT_MARGIN_AREA);
28930 if (row->used[TEXT_AREA])
28931 expose_area (w, row, r, TEXT_AREA);
28932 if (row->used[RIGHT_MARGIN_AREA])
28933 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28934 draw_row_fringe_bitmaps (w, row);
28935 }
28936
28937 return row->mouse_face_p;
28938 }
28939
28940
28941 /* Redraw those parts of glyphs rows during expose event handling that
28942 overlap other rows. Redrawing of an exposed line writes over parts
28943 of lines overlapping that exposed line; this function fixes that.
28944
28945 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28946 row in W's current matrix that is exposed and overlaps other rows.
28947 LAST_OVERLAPPING_ROW is the last such row. */
28948
28949 static void
28950 expose_overlaps (struct window *w,
28951 struct glyph_row *first_overlapping_row,
28952 struct glyph_row *last_overlapping_row,
28953 XRectangle *r)
28954 {
28955 struct glyph_row *row;
28956
28957 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28958 if (row->overlapping_p)
28959 {
28960 eassert (row->enabled_p && !row->mode_line_p);
28961
28962 row->clip = r;
28963 if (row->used[LEFT_MARGIN_AREA])
28964 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28965
28966 if (row->used[TEXT_AREA])
28967 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28968
28969 if (row->used[RIGHT_MARGIN_AREA])
28970 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28971 row->clip = NULL;
28972 }
28973 }
28974
28975
28976 /* Return non-zero if W's cursor intersects rectangle R. */
28977
28978 static int
28979 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28980 {
28981 XRectangle cr, result;
28982 struct glyph *cursor_glyph;
28983 struct glyph_row *row;
28984
28985 if (w->phys_cursor.vpos >= 0
28986 && w->phys_cursor.vpos < w->current_matrix->nrows
28987 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28988 row->enabled_p)
28989 && row->cursor_in_fringe_p)
28990 {
28991 /* Cursor is in the fringe. */
28992 cr.x = window_box_right_offset (w,
28993 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28994 ? RIGHT_MARGIN_AREA
28995 : TEXT_AREA));
28996 cr.y = row->y;
28997 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28998 cr.height = row->height;
28999 return x_intersect_rectangles (&cr, r, &result);
29000 }
29001
29002 cursor_glyph = get_phys_cursor_glyph (w);
29003 if (cursor_glyph)
29004 {
29005 /* r is relative to W's box, but w->phys_cursor.x is relative
29006 to left edge of W's TEXT area. Adjust it. */
29007 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29008 cr.y = w->phys_cursor.y;
29009 cr.width = cursor_glyph->pixel_width;
29010 cr.height = w->phys_cursor_height;
29011 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29012 I assume the effect is the same -- and this is portable. */
29013 return x_intersect_rectangles (&cr, r, &result);
29014 }
29015 /* If we don't understand the format, pretend we're not in the hot-spot. */
29016 return 0;
29017 }
29018
29019
29020 /* EXPORT:
29021 Draw a vertical window border to the right of window W if W doesn't
29022 have vertical scroll bars. */
29023
29024 void
29025 x_draw_vertical_border (struct window *w)
29026 {
29027 struct frame *f = XFRAME (WINDOW_FRAME (w));
29028
29029 /* We could do better, if we knew what type of scroll-bar the adjacent
29030 windows (on either side) have... But we don't :-(
29031 However, I think this works ok. ++KFS 2003-04-25 */
29032
29033 /* Redraw borders between horizontally adjacent windows. Don't
29034 do it for frames with vertical scroll bars because either the
29035 right scroll bar of a window, or the left scroll bar of its
29036 neighbor will suffice as a border. */
29037 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
29038 return;
29039
29040 /* Note: It is necessary to redraw both the left and the right
29041 borders, for when only this single window W is being
29042 redisplayed. */
29043 if (!WINDOW_RIGHTMOST_P (w)
29044 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29045 {
29046 int x0, x1, y0, y1;
29047
29048 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
29049 y1 -= 1;
29050
29051 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29052 x1 -= 1;
29053
29054 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29055 }
29056 if (!WINDOW_LEFTMOST_P (w)
29057 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29058 {
29059 int x0, x1, y0, y1;
29060
29061 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
29062 y1 -= 1;
29063
29064 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29065 x0 -= 1;
29066
29067 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29068 }
29069 }
29070
29071
29072 /* Redraw the part of window W intersection rectangle FR. Pixel
29073 coordinates in FR are frame-relative. Call this function with
29074 input blocked. Value is non-zero if the exposure overwrites
29075 mouse-face. */
29076
29077 static int
29078 expose_window (struct window *w, XRectangle *fr)
29079 {
29080 struct frame *f = XFRAME (w->frame);
29081 XRectangle wr, r;
29082 int mouse_face_overwritten_p = 0;
29083
29084 /* If window is not yet fully initialized, do nothing. This can
29085 happen when toolkit scroll bars are used and a window is split.
29086 Reconfiguring the scroll bar will generate an expose for a newly
29087 created window. */
29088 if (w->current_matrix == NULL)
29089 return 0;
29090
29091 /* When we're currently updating the window, display and current
29092 matrix usually don't agree. Arrange for a thorough display
29093 later. */
29094 if (w->must_be_updated_p)
29095 {
29096 SET_FRAME_GARBAGED (f);
29097 return 0;
29098 }
29099
29100 /* Frame-relative pixel rectangle of W. */
29101 wr.x = WINDOW_LEFT_EDGE_X (w);
29102 wr.y = WINDOW_TOP_EDGE_Y (w);
29103 wr.width = WINDOW_TOTAL_WIDTH (w);
29104 wr.height = WINDOW_TOTAL_HEIGHT (w);
29105
29106 if (x_intersect_rectangles (fr, &wr, &r))
29107 {
29108 int yb = window_text_bottom_y (w);
29109 struct glyph_row *row;
29110 int cursor_cleared_p, phys_cursor_on_p;
29111 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29112
29113 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29114 r.x, r.y, r.width, r.height));
29115
29116 /* Convert to window coordinates. */
29117 r.x -= WINDOW_LEFT_EDGE_X (w);
29118 r.y -= WINDOW_TOP_EDGE_Y (w);
29119
29120 /* Turn off the cursor. */
29121 if (!w->pseudo_window_p
29122 && phys_cursor_in_rect_p (w, &r))
29123 {
29124 x_clear_cursor (w);
29125 cursor_cleared_p = 1;
29126 }
29127 else
29128 cursor_cleared_p = 0;
29129
29130 /* If the row containing the cursor extends face to end of line,
29131 then expose_area might overwrite the cursor outside the
29132 rectangle and thus notice_overwritten_cursor might clear
29133 w->phys_cursor_on_p. We remember the original value and
29134 check later if it is changed. */
29135 phys_cursor_on_p = w->phys_cursor_on_p;
29136
29137 /* Update lines intersecting rectangle R. */
29138 first_overlapping_row = last_overlapping_row = NULL;
29139 for (row = w->current_matrix->rows;
29140 row->enabled_p;
29141 ++row)
29142 {
29143 int y0 = row->y;
29144 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29145
29146 if ((y0 >= r.y && y0 < r.y + r.height)
29147 || (y1 > r.y && y1 < r.y + r.height)
29148 || (r.y >= y0 && r.y < y1)
29149 || (r.y + r.height > y0 && r.y + r.height < y1))
29150 {
29151 /* A header line may be overlapping, but there is no need
29152 to fix overlapping areas for them. KFS 2005-02-12 */
29153 if (row->overlapping_p && !row->mode_line_p)
29154 {
29155 if (first_overlapping_row == NULL)
29156 first_overlapping_row = row;
29157 last_overlapping_row = row;
29158 }
29159
29160 row->clip = fr;
29161 if (expose_line (w, row, &r))
29162 mouse_face_overwritten_p = 1;
29163 row->clip = NULL;
29164 }
29165 else if (row->overlapping_p)
29166 {
29167 /* We must redraw a row overlapping the exposed area. */
29168 if (y0 < r.y
29169 ? y0 + row->phys_height > r.y
29170 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29171 {
29172 if (first_overlapping_row == NULL)
29173 first_overlapping_row = row;
29174 last_overlapping_row = row;
29175 }
29176 }
29177
29178 if (y1 >= yb)
29179 break;
29180 }
29181
29182 /* Display the mode line if there is one. */
29183 if (WINDOW_WANTS_MODELINE_P (w)
29184 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29185 row->enabled_p)
29186 && row->y < r.y + r.height)
29187 {
29188 if (expose_line (w, row, &r))
29189 mouse_face_overwritten_p = 1;
29190 }
29191
29192 if (!w->pseudo_window_p)
29193 {
29194 /* Fix the display of overlapping rows. */
29195 if (first_overlapping_row)
29196 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29197 fr);
29198
29199 /* Draw border between windows. */
29200 x_draw_vertical_border (w);
29201
29202 /* Turn the cursor on again. */
29203 if (cursor_cleared_p
29204 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29205 update_window_cursor (w, 1);
29206 }
29207 }
29208
29209 return mouse_face_overwritten_p;
29210 }
29211
29212
29213
29214 /* Redraw (parts) of all windows in the window tree rooted at W that
29215 intersect R. R contains frame pixel coordinates. Value is
29216 non-zero if the exposure overwrites mouse-face. */
29217
29218 static int
29219 expose_window_tree (struct window *w, XRectangle *r)
29220 {
29221 struct frame *f = XFRAME (w->frame);
29222 int mouse_face_overwritten_p = 0;
29223
29224 while (w && !FRAME_GARBAGED_P (f))
29225 {
29226 if (WINDOWP (w->contents))
29227 mouse_face_overwritten_p
29228 |= expose_window_tree (XWINDOW (w->contents), r);
29229 else
29230 mouse_face_overwritten_p |= expose_window (w, r);
29231
29232 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29233 }
29234
29235 return mouse_face_overwritten_p;
29236 }
29237
29238
29239 /* EXPORT:
29240 Redisplay an exposed area of frame F. X and Y are the upper-left
29241 corner of the exposed rectangle. W and H are width and height of
29242 the exposed area. All are pixel values. W or H zero means redraw
29243 the entire frame. */
29244
29245 void
29246 expose_frame (struct frame *f, int x, int y, int w, int h)
29247 {
29248 XRectangle r;
29249 int mouse_face_overwritten_p = 0;
29250
29251 TRACE ((stderr, "expose_frame "));
29252
29253 /* No need to redraw if frame will be redrawn soon. */
29254 if (FRAME_GARBAGED_P (f))
29255 {
29256 TRACE ((stderr, " garbaged\n"));
29257 return;
29258 }
29259
29260 /* If basic faces haven't been realized yet, there is no point in
29261 trying to redraw anything. This can happen when we get an expose
29262 event while Emacs is starting, e.g. by moving another window. */
29263 if (FRAME_FACE_CACHE (f) == NULL
29264 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29265 {
29266 TRACE ((stderr, " no faces\n"));
29267 return;
29268 }
29269
29270 if (w == 0 || h == 0)
29271 {
29272 r.x = r.y = 0;
29273 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29274 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29275 }
29276 else
29277 {
29278 r.x = x;
29279 r.y = y;
29280 r.width = w;
29281 r.height = h;
29282 }
29283
29284 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29285 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29286
29287 if (WINDOWP (f->tool_bar_window))
29288 mouse_face_overwritten_p
29289 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29290
29291 #ifdef HAVE_X_WINDOWS
29292 #ifndef MSDOS
29293 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29294 if (WINDOWP (f->menu_bar_window))
29295 mouse_face_overwritten_p
29296 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29297 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29298 #endif
29299 #endif
29300
29301 /* Some window managers support a focus-follows-mouse style with
29302 delayed raising of frames. Imagine a partially obscured frame,
29303 and moving the mouse into partially obscured mouse-face on that
29304 frame. The visible part of the mouse-face will be highlighted,
29305 then the WM raises the obscured frame. With at least one WM, KDE
29306 2.1, Emacs is not getting any event for the raising of the frame
29307 (even tried with SubstructureRedirectMask), only Expose events.
29308 These expose events will draw text normally, i.e. not
29309 highlighted. Which means we must redo the highlight here.
29310 Subsume it under ``we love X''. --gerd 2001-08-15 */
29311 /* Included in Windows version because Windows most likely does not
29312 do the right thing if any third party tool offers
29313 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29314 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29315 {
29316 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29317 if (f == hlinfo->mouse_face_mouse_frame)
29318 {
29319 int mouse_x = hlinfo->mouse_face_mouse_x;
29320 int mouse_y = hlinfo->mouse_face_mouse_y;
29321 clear_mouse_face (hlinfo);
29322 note_mouse_highlight (f, mouse_x, mouse_y);
29323 }
29324 }
29325 }
29326
29327
29328 /* EXPORT:
29329 Determine the intersection of two rectangles R1 and R2. Return
29330 the intersection in *RESULT. Value is non-zero if RESULT is not
29331 empty. */
29332
29333 int
29334 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29335 {
29336 XRectangle *left, *right;
29337 XRectangle *upper, *lower;
29338 int intersection_p = 0;
29339
29340 /* Rearrange so that R1 is the left-most rectangle. */
29341 if (r1->x < r2->x)
29342 left = r1, right = r2;
29343 else
29344 left = r2, right = r1;
29345
29346 /* X0 of the intersection is right.x0, if this is inside R1,
29347 otherwise there is no intersection. */
29348 if (right->x <= left->x + left->width)
29349 {
29350 result->x = right->x;
29351
29352 /* The right end of the intersection is the minimum of
29353 the right ends of left and right. */
29354 result->width = (min (left->x + left->width, right->x + right->width)
29355 - result->x);
29356
29357 /* Same game for Y. */
29358 if (r1->y < r2->y)
29359 upper = r1, lower = r2;
29360 else
29361 upper = r2, lower = r1;
29362
29363 /* The upper end of the intersection is lower.y0, if this is inside
29364 of upper. Otherwise, there is no intersection. */
29365 if (lower->y <= upper->y + upper->height)
29366 {
29367 result->y = lower->y;
29368
29369 /* The lower end of the intersection is the minimum of the lower
29370 ends of upper and lower. */
29371 result->height = (min (lower->y + lower->height,
29372 upper->y + upper->height)
29373 - result->y);
29374 intersection_p = 1;
29375 }
29376 }
29377
29378 return intersection_p;
29379 }
29380
29381 #endif /* HAVE_WINDOW_SYSTEM */
29382
29383 \f
29384 /***********************************************************************
29385 Initialization
29386 ***********************************************************************/
29387
29388 void
29389 syms_of_xdisp (void)
29390 {
29391 Vwith_echo_area_save_vector = Qnil;
29392 staticpro (&Vwith_echo_area_save_vector);
29393
29394 Vmessage_stack = Qnil;
29395 staticpro (&Vmessage_stack);
29396
29397 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29398 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29399
29400 message_dolog_marker1 = Fmake_marker ();
29401 staticpro (&message_dolog_marker1);
29402 message_dolog_marker2 = Fmake_marker ();
29403 staticpro (&message_dolog_marker2);
29404 message_dolog_marker3 = Fmake_marker ();
29405 staticpro (&message_dolog_marker3);
29406
29407 #ifdef GLYPH_DEBUG
29408 defsubr (&Sdump_frame_glyph_matrix);
29409 defsubr (&Sdump_glyph_matrix);
29410 defsubr (&Sdump_glyph_row);
29411 defsubr (&Sdump_tool_bar_row);
29412 defsubr (&Strace_redisplay);
29413 defsubr (&Strace_to_stderr);
29414 #endif
29415 #ifdef HAVE_WINDOW_SYSTEM
29416 defsubr (&Stool_bar_lines_needed);
29417 defsubr (&Slookup_image_map);
29418 #endif
29419 defsubr (&Sline_pixel_height);
29420 defsubr (&Sformat_mode_line);
29421 defsubr (&Sinvisible_p);
29422 defsubr (&Scurrent_bidi_paragraph_direction);
29423 defsubr (&Smove_point_visually);
29424
29425 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29426 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29427 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29428 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29429 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29430 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29431 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29432 DEFSYM (Qeval, "eval");
29433 DEFSYM (QCdata, ":data");
29434 DEFSYM (Qdisplay, "display");
29435 DEFSYM (Qspace_width, "space-width");
29436 DEFSYM (Qraise, "raise");
29437 DEFSYM (Qslice, "slice");
29438 DEFSYM (Qspace, "space");
29439 DEFSYM (Qmargin, "margin");
29440 DEFSYM (Qpointer, "pointer");
29441 DEFSYM (Qleft_margin, "left-margin");
29442 DEFSYM (Qright_margin, "right-margin");
29443 DEFSYM (Qcenter, "center");
29444 DEFSYM (Qline_height, "line-height");
29445 DEFSYM (QCalign_to, ":align-to");
29446 DEFSYM (QCrelative_width, ":relative-width");
29447 DEFSYM (QCrelative_height, ":relative-height");
29448 DEFSYM (QCeval, ":eval");
29449 DEFSYM (QCpropertize, ":propertize");
29450 DEFSYM (QCfile, ":file");
29451 DEFSYM (Qfontified, "fontified");
29452 DEFSYM (Qfontification_functions, "fontification-functions");
29453 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29454 DEFSYM (Qescape_glyph, "escape-glyph");
29455 DEFSYM (Qnobreak_space, "nobreak-space");
29456 DEFSYM (Qimage, "image");
29457 DEFSYM (Qtext, "text");
29458 DEFSYM (Qboth, "both");
29459 DEFSYM (Qboth_horiz, "both-horiz");
29460 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29461 DEFSYM (QCmap, ":map");
29462 DEFSYM (QCpointer, ":pointer");
29463 DEFSYM (Qrect, "rect");
29464 DEFSYM (Qcircle, "circle");
29465 DEFSYM (Qpoly, "poly");
29466 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29467 DEFSYM (Qgrow_only, "grow-only");
29468 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29469 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29470 DEFSYM (Qposition, "position");
29471 DEFSYM (Qbuffer_position, "buffer-position");
29472 DEFSYM (Qobject, "object");
29473 DEFSYM (Qbar, "bar");
29474 DEFSYM (Qhbar, "hbar");
29475 DEFSYM (Qbox, "box");
29476 DEFSYM (Qhollow, "hollow");
29477 DEFSYM (Qhand, "hand");
29478 DEFSYM (Qarrow, "arrow");
29479 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29480
29481 list_of_error = list1 (list2 (intern_c_string ("error"),
29482 intern_c_string ("void-variable")));
29483 staticpro (&list_of_error);
29484
29485 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29486 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29487 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29488 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29489
29490 echo_buffer[0] = echo_buffer[1] = Qnil;
29491 staticpro (&echo_buffer[0]);
29492 staticpro (&echo_buffer[1]);
29493
29494 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29495 staticpro (&echo_area_buffer[0]);
29496 staticpro (&echo_area_buffer[1]);
29497
29498 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29499 staticpro (&Vmessages_buffer_name);
29500
29501 mode_line_proptrans_alist = Qnil;
29502 staticpro (&mode_line_proptrans_alist);
29503 mode_line_string_list = Qnil;
29504 staticpro (&mode_line_string_list);
29505 mode_line_string_face = Qnil;
29506 staticpro (&mode_line_string_face);
29507 mode_line_string_face_prop = Qnil;
29508 staticpro (&mode_line_string_face_prop);
29509 Vmode_line_unwind_vector = Qnil;
29510 staticpro (&Vmode_line_unwind_vector);
29511
29512 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29513
29514 help_echo_string = Qnil;
29515 staticpro (&help_echo_string);
29516 help_echo_object = Qnil;
29517 staticpro (&help_echo_object);
29518 help_echo_window = Qnil;
29519 staticpro (&help_echo_window);
29520 previous_help_echo_string = Qnil;
29521 staticpro (&previous_help_echo_string);
29522 help_echo_pos = -1;
29523
29524 DEFSYM (Qright_to_left, "right-to-left");
29525 DEFSYM (Qleft_to_right, "left-to-right");
29526
29527 #ifdef HAVE_WINDOW_SYSTEM
29528 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29529 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29530 For example, if a block cursor is over a tab, it will be drawn as
29531 wide as that tab on the display. */);
29532 x_stretch_cursor_p = 0;
29533 #endif
29534
29535 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29536 doc: /* Non-nil means highlight trailing whitespace.
29537 The face used for trailing whitespace is `trailing-whitespace'. */);
29538 Vshow_trailing_whitespace = Qnil;
29539
29540 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29541 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29542 If the value is t, Emacs highlights non-ASCII chars which have the
29543 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29544 or `escape-glyph' face respectively.
29545
29546 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29547 U+2011 (non-breaking hyphen) are affected.
29548
29549 Any other non-nil value means to display these characters as a escape
29550 glyph followed by an ordinary space or hyphen.
29551
29552 A value of nil means no special handling of these characters. */);
29553 Vnobreak_char_display = Qt;
29554
29555 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29556 doc: /* The pointer shape to show in void text areas.
29557 A value of nil means to show the text pointer. Other options are `arrow',
29558 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29559 Vvoid_text_area_pointer = Qarrow;
29560
29561 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29562 doc: /* Non-nil means don't actually do any redisplay.
29563 This is used for internal purposes. */);
29564 Vinhibit_redisplay = Qnil;
29565
29566 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29567 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29568 Vglobal_mode_string = Qnil;
29569
29570 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29571 doc: /* Marker for where to display an arrow on top of the buffer text.
29572 This must be the beginning of a line in order to work.
29573 See also `overlay-arrow-string'. */);
29574 Voverlay_arrow_position = Qnil;
29575
29576 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29577 doc: /* String to display as an arrow in non-window frames.
29578 See also `overlay-arrow-position'. */);
29579 Voverlay_arrow_string = build_pure_c_string ("=>");
29580
29581 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29582 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29583 The symbols on this list are examined during redisplay to determine
29584 where to display overlay arrows. */);
29585 Voverlay_arrow_variable_list
29586 = list1 (intern_c_string ("overlay-arrow-position"));
29587
29588 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29589 doc: /* The number of lines to try scrolling a window by when point moves out.
29590 If that fails to bring point back on frame, point is centered instead.
29591 If this is zero, point is always centered after it moves off frame.
29592 If you want scrolling to always be a line at a time, you should set
29593 `scroll-conservatively' to a large value rather than set this to 1. */);
29594
29595 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29596 doc: /* Scroll up to this many lines, to bring point back on screen.
29597 If point moves off-screen, redisplay will scroll by up to
29598 `scroll-conservatively' lines in order to bring point just barely
29599 onto the screen again. If that cannot be done, then redisplay
29600 recenters point as usual.
29601
29602 If the value is greater than 100, redisplay will never recenter point,
29603 but will always scroll just enough text to bring point into view, even
29604 if you move far away.
29605
29606 A value of zero means always recenter point if it moves off screen. */);
29607 scroll_conservatively = 0;
29608
29609 DEFVAR_INT ("scroll-margin", scroll_margin,
29610 doc: /* Number of lines of margin at the top and bottom of a window.
29611 Recenter the window whenever point gets within this many lines
29612 of the top or bottom of the window. */);
29613 scroll_margin = 0;
29614
29615 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29616 doc: /* Pixels per inch value for non-window system displays.
29617 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29618 Vdisplay_pixels_per_inch = make_float (72.0);
29619
29620 #ifdef GLYPH_DEBUG
29621 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29622 #endif
29623
29624 DEFVAR_LISP ("truncate-partial-width-windows",
29625 Vtruncate_partial_width_windows,
29626 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29627 For an integer value, truncate lines in each window narrower than the
29628 full frame width, provided the window width is less than that integer;
29629 otherwise, respect the value of `truncate-lines'.
29630
29631 For any other non-nil value, truncate lines in all windows that do
29632 not span the full frame width.
29633
29634 A value of nil means to respect the value of `truncate-lines'.
29635
29636 If `word-wrap' is enabled, you might want to reduce this. */);
29637 Vtruncate_partial_width_windows = make_number (50);
29638
29639 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29640 doc: /* Maximum buffer size for which line number should be displayed.
29641 If the buffer is bigger than this, the line number does not appear
29642 in the mode line. A value of nil means no limit. */);
29643 Vline_number_display_limit = Qnil;
29644
29645 DEFVAR_INT ("line-number-display-limit-width",
29646 line_number_display_limit_width,
29647 doc: /* Maximum line width (in characters) for line number display.
29648 If the average length of the lines near point is bigger than this, then the
29649 line number may be omitted from the mode line. */);
29650 line_number_display_limit_width = 200;
29651
29652 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29653 doc: /* Non-nil means highlight region even in nonselected windows. */);
29654 highlight_nonselected_windows = 0;
29655
29656 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29657 doc: /* Non-nil if more than one frame is visible on this display.
29658 Minibuffer-only frames don't count, but iconified frames do.
29659 This variable is not guaranteed to be accurate except while processing
29660 `frame-title-format' and `icon-title-format'. */);
29661
29662 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29663 doc: /* Template for displaying the title bar of visible frames.
29664 \(Assuming the window manager supports this feature.)
29665
29666 This variable has the same structure as `mode-line-format', except that
29667 the %c and %l constructs are ignored. It is used only on frames for
29668 which no explicit name has been set \(see `modify-frame-parameters'). */);
29669
29670 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29671 doc: /* Template for displaying the title bar of an iconified frame.
29672 \(Assuming the window manager supports this feature.)
29673 This variable has the same structure as `mode-line-format' (which see),
29674 and is used only on frames for which no explicit name has been set
29675 \(see `modify-frame-parameters'). */);
29676 Vicon_title_format
29677 = Vframe_title_format
29678 = listn (CONSTYPE_PURE, 3,
29679 intern_c_string ("multiple-frames"),
29680 build_pure_c_string ("%b"),
29681 listn (CONSTYPE_PURE, 4,
29682 empty_unibyte_string,
29683 intern_c_string ("invocation-name"),
29684 build_pure_c_string ("@"),
29685 intern_c_string ("system-name")));
29686
29687 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29688 doc: /* Maximum number of lines to keep in the message log buffer.
29689 If nil, disable message logging. If t, log messages but don't truncate
29690 the buffer when it becomes large. */);
29691 Vmessage_log_max = make_number (1000);
29692
29693 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29694 doc: /* Functions called before redisplay, if window sizes have changed.
29695 The value should be a list of functions that take one argument.
29696 Just before redisplay, for each frame, if any of its windows have changed
29697 size since the last redisplay, or have been split or deleted,
29698 all the functions in the list are called, with the frame as argument. */);
29699 Vwindow_size_change_functions = Qnil;
29700
29701 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29702 doc: /* List of functions to call before redisplaying a window with scrolling.
29703 Each function is called with two arguments, the window and its new
29704 display-start position. Note that these functions are also called by
29705 `set-window-buffer'. Also note that the value of `window-end' is not
29706 valid when these functions are called.
29707
29708 Warning: Do not use this feature to alter the way the window
29709 is scrolled. It is not designed for that, and such use probably won't
29710 work. */);
29711 Vwindow_scroll_functions = Qnil;
29712
29713 DEFVAR_LISP ("window-text-change-functions",
29714 Vwindow_text_change_functions,
29715 doc: /* Functions to call in redisplay when text in the window might change. */);
29716 Vwindow_text_change_functions = Qnil;
29717
29718 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29719 doc: /* Functions called when redisplay of a window reaches the end trigger.
29720 Each function is called with two arguments, the window and the end trigger value.
29721 See `set-window-redisplay-end-trigger'. */);
29722 Vredisplay_end_trigger_functions = Qnil;
29723
29724 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29725 doc: /* Non-nil means autoselect window with mouse pointer.
29726 If nil, do not autoselect windows.
29727 A positive number means delay autoselection by that many seconds: a
29728 window is autoselected only after the mouse has remained in that
29729 window for the duration of the delay.
29730 A negative number has a similar effect, but causes windows to be
29731 autoselected only after the mouse has stopped moving. \(Because of
29732 the way Emacs compares mouse events, you will occasionally wait twice
29733 that time before the window gets selected.\)
29734 Any other value means to autoselect window instantaneously when the
29735 mouse pointer enters it.
29736
29737 Autoselection selects the minibuffer only if it is active, and never
29738 unselects the minibuffer if it is active.
29739
29740 When customizing this variable make sure that the actual value of
29741 `focus-follows-mouse' matches the behavior of your window manager. */);
29742 Vmouse_autoselect_window = Qnil;
29743
29744 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29745 doc: /* Non-nil means automatically resize tool-bars.
29746 This dynamically changes the tool-bar's height to the minimum height
29747 that is needed to make all tool-bar items visible.
29748 If value is `grow-only', the tool-bar's height is only increased
29749 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29750 Vauto_resize_tool_bars = Qt;
29751
29752 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29753 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29754 auto_raise_tool_bar_buttons_p = 1;
29755
29756 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29757 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29758 make_cursor_line_fully_visible_p = 1;
29759
29760 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29761 doc: /* Border below tool-bar in pixels.
29762 If an integer, use it as the height of the border.
29763 If it is one of `internal-border-width' or `border-width', use the
29764 value of the corresponding frame parameter.
29765 Otherwise, no border is added below the tool-bar. */);
29766 Vtool_bar_border = Qinternal_border_width;
29767
29768 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29769 doc: /* Margin around tool-bar buttons in pixels.
29770 If an integer, use that for both horizontal and vertical margins.
29771 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29772 HORZ specifying the horizontal margin, and VERT specifying the
29773 vertical margin. */);
29774 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29775
29776 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29777 doc: /* Relief thickness of tool-bar buttons. */);
29778 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29779
29780 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29781 doc: /* Tool bar style to use.
29782 It can be one of
29783 image - show images only
29784 text - show text only
29785 both - show both, text below image
29786 both-horiz - show text to the right of the image
29787 text-image-horiz - show text to the left of the image
29788 any other - use system default or image if no system default.
29789
29790 This variable only affects the GTK+ toolkit version of Emacs. */);
29791 Vtool_bar_style = Qnil;
29792
29793 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29794 doc: /* Maximum number of characters a label can have to be shown.
29795 The tool bar style must also show labels for this to have any effect, see
29796 `tool-bar-style'. */);
29797 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29798
29799 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29800 doc: /* List of functions to call to fontify regions of text.
29801 Each function is called with one argument POS. Functions must
29802 fontify a region starting at POS in the current buffer, and give
29803 fontified regions the property `fontified'. */);
29804 Vfontification_functions = Qnil;
29805 Fmake_variable_buffer_local (Qfontification_functions);
29806
29807 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29808 unibyte_display_via_language_environment,
29809 doc: /* Non-nil means display unibyte text according to language environment.
29810 Specifically, this means that raw bytes in the range 160-255 decimal
29811 are displayed by converting them to the equivalent multibyte characters
29812 according to the current language environment. As a result, they are
29813 displayed according to the current fontset.
29814
29815 Note that this variable affects only how these bytes are displayed,
29816 but does not change the fact they are interpreted as raw bytes. */);
29817 unibyte_display_via_language_environment = 0;
29818
29819 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29820 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29821 If a float, it specifies a fraction of the mini-window frame's height.
29822 If an integer, it specifies a number of lines. */);
29823 Vmax_mini_window_height = make_float (0.25);
29824
29825 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29826 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29827 A value of nil means don't automatically resize mini-windows.
29828 A value of t means resize them to fit the text displayed in them.
29829 A value of `grow-only', the default, means let mini-windows grow only;
29830 they return to their normal size when the minibuffer is closed, or the
29831 echo area becomes empty. */);
29832 Vresize_mini_windows = Qgrow_only;
29833
29834 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29835 doc: /* Alist specifying how to blink the cursor off.
29836 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29837 `cursor-type' frame-parameter or variable equals ON-STATE,
29838 comparing using `equal', Emacs uses OFF-STATE to specify
29839 how to blink it off. ON-STATE and OFF-STATE are values for
29840 the `cursor-type' frame parameter.
29841
29842 If a frame's ON-STATE has no entry in this list,
29843 the frame's other specifications determine how to blink the cursor off. */);
29844 Vblink_cursor_alist = Qnil;
29845
29846 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29847 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29848 If non-nil, windows are automatically scrolled horizontally to make
29849 point visible. */);
29850 automatic_hscrolling_p = 1;
29851 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29852
29853 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29854 doc: /* How many columns away from the window edge point is allowed to get
29855 before automatic hscrolling will horizontally scroll the window. */);
29856 hscroll_margin = 5;
29857
29858 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29859 doc: /* How many columns to scroll the window when point gets too close to the edge.
29860 When point is less than `hscroll-margin' columns from the window
29861 edge, automatic hscrolling will scroll the window by the amount of columns
29862 determined by this variable. If its value is a positive integer, scroll that
29863 many columns. If it's a positive floating-point number, it specifies the
29864 fraction of the window's width to scroll. If it's nil or zero, point will be
29865 centered horizontally after the scroll. Any other value, including negative
29866 numbers, are treated as if the value were zero.
29867
29868 Automatic hscrolling always moves point outside the scroll margin, so if
29869 point was more than scroll step columns inside the margin, the window will
29870 scroll more than the value given by the scroll step.
29871
29872 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29873 and `scroll-right' overrides this variable's effect. */);
29874 Vhscroll_step = make_number (0);
29875
29876 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29877 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29878 Bind this around calls to `message' to let it take effect. */);
29879 message_truncate_lines = 0;
29880
29881 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29882 doc: /* Normal hook run to update the menu bar definitions.
29883 Redisplay runs this hook before it redisplays the menu bar.
29884 This is used to update submenus such as Buffers,
29885 whose contents depend on various data. */);
29886 Vmenu_bar_update_hook = Qnil;
29887
29888 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29889 doc: /* Frame for which we are updating a menu.
29890 The enable predicate for a menu binding should check this variable. */);
29891 Vmenu_updating_frame = Qnil;
29892
29893 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29894 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29895 inhibit_menubar_update = 0;
29896
29897 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29898 doc: /* Prefix prepended to all continuation lines at display time.
29899 The value may be a string, an image, or a stretch-glyph; it is
29900 interpreted in the same way as the value of a `display' text property.
29901
29902 This variable is overridden by any `wrap-prefix' text or overlay
29903 property.
29904
29905 To add a prefix to non-continuation lines, use `line-prefix'. */);
29906 Vwrap_prefix = Qnil;
29907 DEFSYM (Qwrap_prefix, "wrap-prefix");
29908 Fmake_variable_buffer_local (Qwrap_prefix);
29909
29910 DEFVAR_LISP ("line-prefix", Vline_prefix,
29911 doc: /* Prefix prepended to all non-continuation lines at display time.
29912 The value may be a string, an image, or a stretch-glyph; it is
29913 interpreted in the same way as the value of a `display' text property.
29914
29915 This variable is overridden by any `line-prefix' text or overlay
29916 property.
29917
29918 To add a prefix to continuation lines, use `wrap-prefix'. */);
29919 Vline_prefix = Qnil;
29920 DEFSYM (Qline_prefix, "line-prefix");
29921 Fmake_variable_buffer_local (Qline_prefix);
29922
29923 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29924 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29925 inhibit_eval_during_redisplay = 0;
29926
29927 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29928 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29929 inhibit_free_realized_faces = 0;
29930
29931 #ifdef GLYPH_DEBUG
29932 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29933 doc: /* Inhibit try_window_id display optimization. */);
29934 inhibit_try_window_id = 0;
29935
29936 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29937 doc: /* Inhibit try_window_reusing display optimization. */);
29938 inhibit_try_window_reusing = 0;
29939
29940 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29941 doc: /* Inhibit try_cursor_movement display optimization. */);
29942 inhibit_try_cursor_movement = 0;
29943 #endif /* GLYPH_DEBUG */
29944
29945 DEFVAR_INT ("overline-margin", overline_margin,
29946 doc: /* Space between overline and text, in pixels.
29947 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29948 margin to the character height. */);
29949 overline_margin = 2;
29950
29951 DEFVAR_INT ("underline-minimum-offset",
29952 underline_minimum_offset,
29953 doc: /* Minimum distance between baseline and underline.
29954 This can improve legibility of underlined text at small font sizes,
29955 particularly when using variable `x-use-underline-position-properties'
29956 with fonts that specify an UNDERLINE_POSITION relatively close to the
29957 baseline. The default value is 1. */);
29958 underline_minimum_offset = 1;
29959
29960 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29961 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29962 This feature only works when on a window system that can change
29963 cursor shapes. */);
29964 display_hourglass_p = 1;
29965
29966 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29967 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29968 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29969
29970 hourglass_atimer = NULL;
29971 hourglass_shown_p = 0;
29972
29973 DEFSYM (Qglyphless_char, "glyphless-char");
29974 DEFSYM (Qhex_code, "hex-code");
29975 DEFSYM (Qempty_box, "empty-box");
29976 DEFSYM (Qthin_space, "thin-space");
29977 DEFSYM (Qzero_width, "zero-width");
29978
29979 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29980 /* Intern this now in case it isn't already done.
29981 Setting this variable twice is harmless.
29982 But don't staticpro it here--that is done in alloc.c. */
29983 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29984 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29985
29986 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29987 doc: /* Char-table defining glyphless characters.
29988 Each element, if non-nil, should be one of the following:
29989 an ASCII acronym string: display this string in a box
29990 `hex-code': display the hexadecimal code of a character in a box
29991 `empty-box': display as an empty box
29992 `thin-space': display as 1-pixel width space
29993 `zero-width': don't display
29994 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29995 display method for graphical terminals and text terminals respectively.
29996 GRAPHICAL and TEXT should each have one of the values listed above.
29997
29998 The char-table has one extra slot to control the display of a character for
29999 which no font is found. This slot only takes effect on graphical terminals.
30000 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30001 `thin-space'. The default is `empty-box'. */);
30002 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30003 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30004 Qempty_box);
30005
30006 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30007 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30008 Vdebug_on_message = Qnil;
30009 }
30010
30011
30012 /* Initialize this module when Emacs starts. */
30013
30014 void
30015 init_xdisp (void)
30016 {
30017 current_header_line_height = current_mode_line_height = -1;
30018
30019 CHARPOS (this_line_start_pos) = 0;
30020
30021 if (!noninteractive)
30022 {
30023 struct window *m = XWINDOW (minibuf_window);
30024 Lisp_Object frame = m->frame;
30025 struct frame *f = XFRAME (frame);
30026 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30027 struct window *r = XWINDOW (root);
30028 int i;
30029
30030 echo_area_window = minibuf_window;
30031
30032 r->top_line = FRAME_TOP_MARGIN (f);
30033 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30034 r->total_cols = FRAME_COLS (f);
30035
30036 m->top_line = FRAME_LINES (f) - 1;
30037 m->total_lines = 1;
30038 m->total_cols = FRAME_COLS (f);
30039
30040 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30041 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30042 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30043
30044 /* The default ellipsis glyphs `...'. */
30045 for (i = 0; i < 3; ++i)
30046 default_invis_vector[i] = make_number ('.');
30047 }
30048
30049 {
30050 /* Allocate the buffer for frame titles.
30051 Also used for `format-mode-line'. */
30052 int size = 100;
30053 mode_line_noprop_buf = xmalloc (size);
30054 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30055 mode_line_noprop_ptr = mode_line_noprop_buf;
30056 mode_line_target = MODE_LINE_DISPLAY;
30057 }
30058
30059 help_echo_showing_p = 0;
30060 }
30061
30062 /* Platform-independent portion of hourglass implementation. */
30063
30064 /* Cancel a currently active hourglass timer, and start a new one. */
30065 void
30066 start_hourglass (void)
30067 {
30068 #if defined (HAVE_WINDOW_SYSTEM)
30069 EMACS_TIME delay;
30070
30071 cancel_hourglass ();
30072
30073 if (INTEGERP (Vhourglass_delay)
30074 && XINT (Vhourglass_delay) > 0)
30075 delay = make_emacs_time (min (XINT (Vhourglass_delay),
30076 TYPE_MAXIMUM (time_t)),
30077 0);
30078 else if (FLOATP (Vhourglass_delay)
30079 && XFLOAT_DATA (Vhourglass_delay) > 0)
30080 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
30081 else
30082 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
30083
30084 #ifdef HAVE_NTGUI
30085 {
30086 extern void w32_note_current_window (void);
30087 w32_note_current_window ();
30088 }
30089 #endif /* HAVE_NTGUI */
30090
30091 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30092 show_hourglass, NULL);
30093 #endif
30094 }
30095
30096
30097 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30098 shown. */
30099 void
30100 cancel_hourglass (void)
30101 {
30102 #if defined (HAVE_WINDOW_SYSTEM)
30103 if (hourglass_atimer)
30104 {
30105 cancel_atimer (hourglass_atimer);
30106 hourglass_atimer = NULL;
30107 }
30108
30109 if (hourglass_shown_p)
30110 hide_hourglass ();
30111 #endif
30112 }