merge trunk
[bpt/emacs.git] / 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
318 #ifndef FRAME_X_OUTPUT
319 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
320 #endif
321
322 #define INFINITY 10000000
323
324 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
325 Lisp_Object Qwindow_scroll_functions;
326 static Lisp_Object Qwindow_text_change_functions;
327 static Lisp_Object Qredisplay_end_trigger_functions;
328 Lisp_Object Qinhibit_point_motion_hooks;
329 static Lisp_Object QCeval, QCpropertize;
330 Lisp_Object QCfile, QCdata;
331 static Lisp_Object Qfontified;
332 static Lisp_Object Qgrow_only;
333 static Lisp_Object Qinhibit_eval_during_redisplay;
334 static Lisp_Object Qbuffer_position, Qposition, Qobject;
335 static Lisp_Object Qright_to_left, Qleft_to_right;
336
337 /* Cursor shapes. */
338 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339
340 /* Pointer shapes. */
341 static Lisp_Object Qarrow, Qhand;
342 Lisp_Object Qtext;
343
344 /* Holds the list (error). */
345 static Lisp_Object list_of_error;
346
347 static Lisp_Object Qfontification_functions;
348
349 static Lisp_Object Qwrap_prefix;
350 static Lisp_Object Qline_prefix;
351 static Lisp_Object Qredisplay_internal;
352
353 /* Non-nil means don't actually do any redisplay. */
354
355 Lisp_Object Qinhibit_redisplay;
356
357 /* Names of text properties relevant for redisplay. */
358
359 Lisp_Object Qdisplay;
360
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
369
370 #ifdef HAVE_WINDOW_SYSTEM
371
372 /* Test if overflow newline into fringe. Called with iterator IT
373 at or past right window margin, and with IT->current_x set. */
374
375 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
376 (!NILP (Voverflow_newline_into_fringe) \
377 && FRAME_WINDOW_P ((IT)->f) \
378 && ((IT)->bidi_it.paragraph_dir == R2L \
379 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
380 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
381 && (IT)->current_x == (IT)->last_visible_x \
382 && (IT)->line_wrap != WORD_WRAP)
383
384 #else /* !HAVE_WINDOW_SYSTEM */
385 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
386 #endif /* HAVE_WINDOW_SYSTEM */
387
388 /* Test if the display element loaded in IT, or the underlying buffer
389 or string character, is a space or a TAB character. This is used
390 to determine where word wrapping can occur. */
391
392 #define IT_DISPLAYING_WHITESPACE(it) \
393 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
394 || ((STRINGP (it->string) \
395 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
396 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
397 || (it->s \
398 && (it->s[IT_BYTEPOS (*it)] == ' ' \
399 || it->s[IT_BYTEPOS (*it)] == '\t')) \
400 || (IT_BYTEPOS (*it) < ZV_BYTE \
401 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
402 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
403
404 /* Name of the face used to highlight trailing whitespace. */
405
406 static Lisp_Object Qtrailing_whitespace;
407
408 /* Name and number of the face used to highlight escape glyphs. */
409
410 static Lisp_Object Qescape_glyph;
411
412 /* Name and number of the face used to highlight non-breaking spaces. */
413
414 static Lisp_Object Qnobreak_space;
415
416 /* The symbol `image' which is the car of the lists used to represent
417 images in Lisp. Also a tool bar style. */
418
419 Lisp_Object Qimage;
420
421 /* The image map types. */
422 Lisp_Object QCmap;
423 static Lisp_Object QCpointer;
424 static Lisp_Object Qrect, Qcircle, Qpoly;
425
426 /* Tool bar styles */
427 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
428
429 /* Non-zero means print newline to stdout before next mini-buffer
430 message. */
431
432 int noninteractive_need_newline;
433
434 /* Non-zero means print newline to message log before next message. */
435
436 static int message_log_need_newline;
437
438 /* Three markers that message_dolog uses.
439 It could allocate them itself, but that causes trouble
440 in handling memory-full errors. */
441 static Lisp_Object message_dolog_marker1;
442 static Lisp_Object message_dolog_marker2;
443 static Lisp_Object message_dolog_marker3;
444 \f
445 /* The buffer position of the first character appearing entirely or
446 partially on the line of the selected window which contains the
447 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
448 redisplay optimization in redisplay_internal. */
449
450 static struct text_pos this_line_start_pos;
451
452 /* Number of characters past the end of the line above, including the
453 terminating newline. */
454
455 static struct text_pos this_line_end_pos;
456
457 /* The vertical positions and the height of this line. */
458
459 static int this_line_vpos;
460 static int this_line_y;
461 static int this_line_pixel_height;
462
463 /* X position at which this display line starts. Usually zero;
464 negative if first character is partially visible. */
465
466 static int this_line_start_x;
467
468 /* The smallest character position seen by move_it_* functions as they
469 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
470 hscrolled lines, see display_line. */
471
472 static struct text_pos this_line_min_pos;
473
474 /* Buffer that this_line_.* variables are referring to. */
475
476 static struct buffer *this_line_buffer;
477
478
479 /* Values of those variables at last redisplay are stored as
480 properties on `overlay-arrow-position' symbol. However, if
481 Voverlay_arrow_position is a marker, last-arrow-position is its
482 numerical position. */
483
484 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
485
486 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
487 properties on a symbol in overlay-arrow-variable-list. */
488
489 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
490
491 Lisp_Object Qmenu_bar_update_hook;
492
493 /* Nonzero if an overlay arrow has been displayed in this window. */
494
495 static int overlay_arrow_seen;
496
497 /* Vector containing glyphs for an ellipsis `...'. */
498
499 static Lisp_Object default_invis_vector[3];
500
501 /* This is the window where the echo area message was displayed. It
502 is always a mini-buffer window, but it may not be the same window
503 currently active as a mini-buffer. */
504
505 Lisp_Object echo_area_window;
506
507 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
508 pushes the current message and the value of
509 message_enable_multibyte on the stack, the function restore_message
510 pops the stack and displays MESSAGE again. */
511
512 static Lisp_Object Vmessage_stack;
513
514 /* Nonzero means multibyte characters were enabled when the echo area
515 message was specified. */
516
517 static int message_enable_multibyte;
518
519 /* Nonzero if we should redraw the mode lines on the next redisplay. */
520
521 int update_mode_lines;
522
523 /* Nonzero if window sizes or contents have changed since last
524 redisplay that finished. */
525
526 int windows_or_buffers_changed;
527
528 /* Nonzero means a frame's cursor type has been changed. */
529
530 int cursor_type_changed;
531
532 /* Nonzero after display_mode_line if %l was used and it displayed a
533 line number. */
534
535 static int line_number_displayed;
536
537 /* The name of the *Messages* buffer, a string. */
538
539 static Lisp_Object Vmessages_buffer_name;
540
541 /* Current, index 0, and last displayed echo area message. Either
542 buffers from echo_buffers, or nil to indicate no message. */
543
544 Lisp_Object echo_area_buffer[2];
545
546 /* The buffers referenced from echo_area_buffer. */
547
548 static Lisp_Object echo_buffer[2];
549
550 /* A vector saved used in with_area_buffer to reduce consing. */
551
552 static Lisp_Object Vwith_echo_area_save_vector;
553
554 /* Non-zero means display_echo_area should display the last echo area
555 message again. Set by redisplay_preserve_echo_area. */
556
557 static int display_last_displayed_message_p;
558
559 /* Nonzero if echo area is being used by print; zero if being used by
560 message. */
561
562 static int message_buf_print;
563
564 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
565
566 static Lisp_Object Qinhibit_menubar_update;
567 static Lisp_Object Qmessage_truncate_lines;
568
569 /* Set to 1 in clear_message to make redisplay_internal aware
570 of an emptied echo area. */
571
572 static int message_cleared_p;
573
574 /* A scratch glyph row with contents used for generating truncation
575 glyphs. Also used in direct_output_for_insert. */
576
577 #define MAX_SCRATCH_GLYPHS 100
578 static struct glyph_row scratch_glyph_row;
579 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
580
581 /* Ascent and height of the last line processed by move_it_to. */
582
583 static int last_height;
584
585 /* Non-zero if there's a help-echo in the echo area. */
586
587 int help_echo_showing_p;
588
589 /* If >= 0, computed, exact values of mode-line and header-line height
590 to use in the macros CURRENT_MODE_LINE_HEIGHT and
591 CURRENT_HEADER_LINE_HEIGHT. */
592
593 int current_mode_line_height, current_header_line_height;
594
595 /* The maximum distance to look ahead for text properties. Values
596 that are too small let us call compute_char_face and similar
597 functions too often which is expensive. Values that are too large
598 let us call compute_char_face and alike too often because we
599 might not be interested in text properties that far away. */
600
601 #define TEXT_PROP_DISTANCE_LIMIT 100
602
603 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
604 iterator state and later restore it. This is needed because the
605 bidi iterator on bidi.c keeps a stacked cache of its states, which
606 is really a singleton. When we use scratch iterator objects to
607 move around the buffer, we can cause the bidi cache to be pushed or
608 popped, and therefore we need to restore the cache state when we
609 return to the original iterator. */
610 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
611 do { \
612 if (CACHE) \
613 bidi_unshelve_cache (CACHE, 1); \
614 ITCOPY = ITORIG; \
615 CACHE = bidi_shelve_cache (); \
616 } while (0)
617
618 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
619 do { \
620 if (pITORIG != pITCOPY) \
621 *(pITORIG) = *(pITCOPY); \
622 bidi_unshelve_cache (CACHE, 0); \
623 CACHE = NULL; \
624 } while (0)
625
626 #ifdef GLYPH_DEBUG
627
628 /* Non-zero means print traces of redisplay if compiled with
629 GLYPH_DEBUG defined. */
630
631 int trace_redisplay_p;
632
633 #endif /* GLYPH_DEBUG */
634
635 #ifdef DEBUG_TRACE_MOVE
636 /* Non-zero means trace with TRACE_MOVE to stderr. */
637 int trace_move;
638
639 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
640 #else
641 #define TRACE_MOVE(x) (void) 0
642 #endif
643
644 static Lisp_Object Qauto_hscroll_mode;
645
646 /* Buffer being redisplayed -- for redisplay_window_error. */
647
648 static struct buffer *displayed_buffer;
649
650 /* Value returned from text property handlers (see below). */
651
652 enum prop_handled
653 {
654 HANDLED_NORMALLY,
655 HANDLED_RECOMPUTE_PROPS,
656 HANDLED_OVERLAY_STRING_CONSUMED,
657 HANDLED_RETURN
658 };
659
660 /* A description of text properties that redisplay is interested
661 in. */
662
663 struct props
664 {
665 /* The name of the property. */
666 Lisp_Object *name;
667
668 /* A unique index for the property. */
669 enum prop_idx idx;
670
671 /* A handler function called to set up iterator IT from the property
672 at IT's current position. Value is used to steer handle_stop. */
673 enum prop_handled (*handler) (struct it *it);
674 };
675
676 static enum prop_handled handle_face_prop (struct it *);
677 static enum prop_handled handle_invisible_prop (struct it *);
678 static enum prop_handled handle_display_prop (struct it *);
679 static enum prop_handled handle_composition_prop (struct it *);
680 static enum prop_handled handle_overlay_change (struct it *);
681 static enum prop_handled handle_fontified_prop (struct it *);
682
683 /* Properties handled by iterators. */
684
685 static struct props it_props[] =
686 {
687 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
688 /* Handle `face' before `display' because some sub-properties of
689 `display' need to know the face. */
690 {&Qface, FACE_PROP_IDX, handle_face_prop},
691 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
692 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
693 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
694 {NULL, 0, NULL}
695 };
696
697 /* Value is the position described by X. If X is a marker, value is
698 the marker_position of X. Otherwise, value is X. */
699
700 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
701
702 /* Enumeration returned by some move_it_.* functions internally. */
703
704 enum move_it_result
705 {
706 /* Not used. Undefined value. */
707 MOVE_UNDEFINED,
708
709 /* Move ended at the requested buffer position or ZV. */
710 MOVE_POS_MATCH_OR_ZV,
711
712 /* Move ended at the requested X pixel position. */
713 MOVE_X_REACHED,
714
715 /* Move within a line ended at the end of a line that must be
716 continued. */
717 MOVE_LINE_CONTINUED,
718
719 /* Move within a line ended at the end of a line that would
720 be displayed truncated. */
721 MOVE_LINE_TRUNCATED,
722
723 /* Move within a line ended at a line end. */
724 MOVE_NEWLINE_OR_CR
725 };
726
727 /* This counter is used to clear the face cache every once in a while
728 in redisplay_internal. It is incremented for each redisplay.
729 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
730 cleared. */
731
732 #define CLEAR_FACE_CACHE_COUNT 500
733 static int clear_face_cache_count;
734
735 /* Similarly for the image cache. */
736
737 #ifdef HAVE_WINDOW_SYSTEM
738 #define CLEAR_IMAGE_CACHE_COUNT 101
739 static int clear_image_cache_count;
740
741 /* Null glyph slice */
742 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
743 #endif
744
745 /* True while redisplay_internal is in progress. */
746
747 bool redisplaying_p;
748
749 static Lisp_Object Qinhibit_free_realized_faces;
750 static Lisp_Object Qmode_line_default_help_echo;
751
752 /* If a string, XTread_socket generates an event to display that string.
753 (The display is done in read_char.) */
754
755 Lisp_Object help_echo_string;
756 Lisp_Object help_echo_window;
757 Lisp_Object help_echo_object;
758 ptrdiff_t help_echo_pos;
759
760 /* Temporary variable for XTread_socket. */
761
762 Lisp_Object previous_help_echo_string;
763
764 /* Platform-independent portion of hourglass implementation. */
765
766 /* Non-zero means an hourglass cursor is currently shown. */
767 int hourglass_shown_p;
768
769 /* If non-null, an asynchronous timer that, when it expires, displays
770 an hourglass cursor on all frames. */
771 struct atimer *hourglass_atimer;
772
773 /* Name of the face used to display glyphless characters. */
774 Lisp_Object Qglyphless_char;
775
776 /* Symbol for the purpose of Vglyphless_char_display. */
777 static Lisp_Object Qglyphless_char_display;
778
779 /* Method symbols for Vglyphless_char_display. */
780 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
781
782 /* Default pixel width of `thin-space' display method. */
783 #define THIN_SPACE_WIDTH 1
784
785 /* Default number of seconds to wait before displaying an hourglass
786 cursor. */
787 #define DEFAULT_HOURGLASS_DELAY 1
788
789 \f
790 /* Function prototypes. */
791
792 static void setup_for_ellipsis (struct it *, int);
793 static void set_iterator_to_next (struct it *, int);
794 static void mark_window_display_accurate_1 (struct window *, int);
795 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
796 static int display_prop_string_p (Lisp_Object, Lisp_Object);
797 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
798 static int cursor_row_p (struct glyph_row *);
799 static int redisplay_mode_lines (Lisp_Object, int);
800 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
801
802 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
803
804 static void handle_line_prefix (struct it *);
805
806 static void pint2str (char *, int, ptrdiff_t);
807 static void pint2hrstr (char *, int, ptrdiff_t);
808 static struct text_pos run_window_scroll_functions (Lisp_Object,
809 struct text_pos);
810 static void reconsider_clip_changes (struct window *, struct buffer *);
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 Lisp_Object 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 void pop_message (void);
827 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
828 static void set_message (Lisp_Object);
829 static int set_message_1 (ptrdiff_t, Lisp_Object);
830 static int display_echo_area (struct window *);
831 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
832 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
833 static Lisp_Object unwind_redisplay (Lisp_Object);
834 static int string_char_and_length (const unsigned char *, int *);
835 static struct text_pos display_prop_end (struct it *, Lisp_Object,
836 struct text_pos);
837 static int compute_window_start_on_continuation_line (struct window *);
838 static void insert_left_trunc_glyphs (struct it *);
839 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
840 Lisp_Object);
841 static void extend_face_to_end_of_line (struct it *);
842 static int append_space_for_newline (struct it *, int);
843 static int cursor_row_fully_visible_p (struct window *, int, int);
844 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
845 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
846 static int trailing_whitespace_p (ptrdiff_t);
847 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
848 static void push_it (struct it *, struct text_pos *);
849 static void iterate_out_of_display_property (struct it *);
850 static void pop_it (struct it *);
851 static void sync_frame_with_window_matrix_rows (struct window *);
852 static void redisplay_internal (void);
853 static int echo_area_display (int);
854 static void redisplay_windows (Lisp_Object);
855 static void redisplay_window (Lisp_Object, int);
856 static Lisp_Object redisplay_window_error (Lisp_Object);
857 static Lisp_Object redisplay_window_0 (Lisp_Object);
858 static Lisp_Object redisplay_window_1 (Lisp_Object);
859 static int set_cursor_from_row (struct window *, struct glyph_row *,
860 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
861 int, int);
862 static int update_menu_bar (struct frame *, int, int);
863 static int try_window_reusing_current_matrix (struct window *);
864 static int try_window_id (struct window *);
865 static int display_line (struct it *);
866 static int display_mode_lines (struct window *);
867 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
868 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
869 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
870 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
871 static void display_menu_bar (struct window *);
872 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
873 ptrdiff_t *);
874 static int display_string (const char *, Lisp_Object, Lisp_Object,
875 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
876 static void compute_line_metrics (struct it *);
877 static void run_redisplay_end_trigger_hook (struct it *);
878 static int get_overlay_strings (struct it *, ptrdiff_t);
879 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
880 static void next_overlay_string (struct it *);
881 static void reseat (struct it *, struct text_pos, int);
882 static void reseat_1 (struct it *, struct text_pos, int);
883 static void back_to_previous_visible_line_start (struct it *);
884 static void reseat_at_next_visible_line_start (struct it *, int);
885 static int next_element_from_ellipsis (struct it *);
886 static int next_element_from_display_vector (struct it *);
887 static int next_element_from_string (struct it *);
888 static int next_element_from_c_string (struct it *);
889 static int next_element_from_buffer (struct it *);
890 static int next_element_from_composition (struct it *);
891 static int next_element_from_image (struct it *);
892 static int next_element_from_stretch (struct it *);
893 static void load_overlay_strings (struct it *, ptrdiff_t);
894 static int init_from_display_pos (struct it *, struct window *,
895 struct display_pos *);
896 static void reseat_to_string (struct it *, const char *,
897 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
898 static int get_next_display_element (struct it *);
899 static enum move_it_result
900 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
901 enum move_operation_enum);
902 static void get_visually_first_element (struct it *);
903 static void init_to_row_start (struct it *, struct window *,
904 struct glyph_row *);
905 static int init_to_row_end (struct it *, struct window *,
906 struct glyph_row *);
907 static void back_to_previous_line_start (struct it *);
908 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
909 static struct text_pos string_pos_nchars_ahead (struct text_pos,
910 Lisp_Object, ptrdiff_t);
911 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
912 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
913 static ptrdiff_t number_of_chars (const char *, bool);
914 static void compute_stop_pos (struct it *);
915 static void compute_string_pos (struct text_pos *, struct text_pos,
916 Lisp_Object);
917 static int face_before_or_after_it_pos (struct it *, int);
918 static ptrdiff_t next_overlay_change (ptrdiff_t);
919 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
920 Lisp_Object, struct text_pos *, ptrdiff_t, int);
921 static int handle_single_display_spec (struct it *, Lisp_Object,
922 Lisp_Object, Lisp_Object,
923 struct text_pos *, ptrdiff_t, int, int);
924 static int underlying_face_id (struct it *);
925 static int in_ellipses_for_invisible_text_p (struct display_pos *,
926 struct window *);
927
928 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
929 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
930
931 #ifdef HAVE_WINDOW_SYSTEM
932
933 static void x_consider_frame_title (Lisp_Object);
934 static int tool_bar_lines_needed (struct frame *, int *);
935 static void update_tool_bar (struct frame *, int);
936 static void build_desired_tool_bar_string (struct frame *f);
937 static int redisplay_tool_bar (struct frame *);
938 static void display_tool_bar_line (struct it *, int);
939 static void notice_overwritten_cursor (struct window *,
940 enum glyph_row_area,
941 int, int, int, int);
942 static void append_stretch_glyph (struct it *, Lisp_Object,
943 int, int, int);
944
945
946 #endif /* HAVE_WINDOW_SYSTEM */
947
948 static void produce_special_glyphs (struct it *, enum display_element_type);
949 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
950 static int coords_in_mouse_face_p (struct window *, int, int);
951
952
953 \f
954 /***********************************************************************
955 Window display dimensions
956 ***********************************************************************/
957
958 /* Return the bottom boundary y-position for text lines in window W.
959 This is the first y position at which a line cannot start.
960 It is relative to the top of the window.
961
962 This is the height of W minus the height of a mode line, if any. */
963
964 int
965 window_text_bottom_y (struct window *w)
966 {
967 int height = WINDOW_TOTAL_HEIGHT (w);
968
969 if (WINDOW_WANTS_MODELINE_P (w))
970 height -= CURRENT_MODE_LINE_HEIGHT (w);
971 return height;
972 }
973
974 /* Return the pixel width of display area AREA of window W. AREA < 0
975 means return the total width of W, not including fringes to
976 the left and right of the window. */
977
978 int
979 window_box_width (struct window *w, int area)
980 {
981 int cols = w->total_cols;
982 int pixels = 0;
983
984 if (!w->pseudo_window_p)
985 {
986 cols -= WINDOW_SCROLL_BAR_COLS (w);
987
988 if (area == TEXT_AREA)
989 {
990 if (INTEGERP (w->left_margin_cols))
991 cols -= XFASTINT (w->left_margin_cols);
992 if (INTEGERP (w->right_margin_cols))
993 cols -= XFASTINT (w->right_margin_cols);
994 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
995 }
996 else if (area == LEFT_MARGIN_AREA)
997 {
998 cols = (INTEGERP (w->left_margin_cols)
999 ? XFASTINT (w->left_margin_cols) : 0);
1000 pixels = 0;
1001 }
1002 else if (area == RIGHT_MARGIN_AREA)
1003 {
1004 cols = (INTEGERP (w->right_margin_cols)
1005 ? XFASTINT (w->right_margin_cols) : 0);
1006 pixels = 0;
1007 }
1008 }
1009
1010 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1011 }
1012
1013
1014 /* Return the pixel height of the display area of window W, not
1015 including mode lines of W, if any. */
1016
1017 int
1018 window_box_height (struct window *w)
1019 {
1020 struct frame *f = XFRAME (w->frame);
1021 int height = WINDOW_TOTAL_HEIGHT (w);
1022
1023 eassert (height >= 0);
1024
1025 /* Note: the code below that determines the mode-line/header-line
1026 height is essentially the same as that contained in the macro
1027 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1028 the appropriate glyph row has its `mode_line_p' flag set,
1029 and if it doesn't, uses estimate_mode_line_height instead. */
1030
1031 if (WINDOW_WANTS_MODELINE_P (w))
1032 {
1033 struct glyph_row *ml_row
1034 = (w->current_matrix && w->current_matrix->rows
1035 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1036 : 0);
1037 if (ml_row && ml_row->mode_line_p)
1038 height -= ml_row->height;
1039 else
1040 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1041 }
1042
1043 if (WINDOW_WANTS_HEADER_LINE_P (w))
1044 {
1045 struct glyph_row *hl_row
1046 = (w->current_matrix && w->current_matrix->rows
1047 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1048 : 0);
1049 if (hl_row && hl_row->mode_line_p)
1050 height -= hl_row->height;
1051 else
1052 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1053 }
1054
1055 /* With a very small font and a mode-line that's taller than
1056 default, we might end up with a negative height. */
1057 return max (0, height);
1058 }
1059
1060 /* Return the window-relative coordinate of the left edge of display
1061 area AREA of window W. AREA < 0 means return the left edge of the
1062 whole window, to the right of the left fringe of W. */
1063
1064 int
1065 window_box_left_offset (struct window *w, int area)
1066 {
1067 int x;
1068
1069 if (w->pseudo_window_p)
1070 return 0;
1071
1072 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1073
1074 if (area == TEXT_AREA)
1075 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1076 + window_box_width (w, LEFT_MARGIN_AREA));
1077 else if (area == RIGHT_MARGIN_AREA)
1078 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1079 + window_box_width (w, LEFT_MARGIN_AREA)
1080 + window_box_width (w, TEXT_AREA)
1081 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1082 ? 0
1083 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1084 else if (area == LEFT_MARGIN_AREA
1085 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1086 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1087
1088 return x;
1089 }
1090
1091
1092 /* Return the window-relative coordinate of the right edge of display
1093 area AREA of window W. AREA < 0 means return the right edge of the
1094 whole window, to the left of the right fringe of W. */
1095
1096 int
1097 window_box_right_offset (struct window *w, int area)
1098 {
1099 return window_box_left_offset (w, area) + window_box_width (w, area);
1100 }
1101
1102 /* Return the frame-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the right of the left fringe of W. */
1105
1106 int
1107 window_box_left (struct window *w, int area)
1108 {
1109 struct frame *f = XFRAME (w->frame);
1110 int x;
1111
1112 if (w->pseudo_window_p)
1113 return FRAME_INTERNAL_BORDER_WIDTH (f);
1114
1115 x = (WINDOW_LEFT_EDGE_X (w)
1116 + window_box_left_offset (w, area));
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the frame-relative coordinate of the right edge of display
1123 area AREA of window W. AREA < 0 means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 int
1127 window_box_right (struct window *w, int area)
1128 {
1129 return window_box_left (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Get the bounding box of the display area AREA of window W, without
1133 mode lines, in frame-relative coordinates. AREA < 0 means the
1134 whole window, not including the left and right fringes of
1135 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1136 coordinates of the upper-left corner of the box. Return in
1137 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1138
1139 void
1140 window_box (struct window *w, int area, int *box_x, int *box_y,
1141 int *box_width, int *box_height)
1142 {
1143 if (box_width)
1144 *box_width = window_box_width (w, area);
1145 if (box_height)
1146 *box_height = window_box_height (w);
1147 if (box_x)
1148 *box_x = window_box_left (w, area);
1149 if (box_y)
1150 {
1151 *box_y = WINDOW_TOP_EDGE_Y (w);
1152 if (WINDOW_WANTS_HEADER_LINE_P (w))
1153 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1154 }
1155 }
1156
1157
1158 /* Get the bounding box of the display area AREA of window W, without
1159 mode lines. AREA < 0 means the whole window, not including the
1160 left and right fringe of the window. Return in *TOP_LEFT_X
1161 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1162 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1163 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1164 box. */
1165
1166 static void
1167 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1168 int *bottom_right_x, int *bottom_right_y)
1169 {
1170 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1171 bottom_right_y);
1172 *bottom_right_x += *top_left_x;
1173 *bottom_right_y += *top_left_y;
1174 }
1175
1176
1177 \f
1178 /***********************************************************************
1179 Utilities
1180 ***********************************************************************/
1181
1182 /* Return the bottom y-position of the line the iterator IT is in.
1183 This can modify IT's settings. */
1184
1185 int
1186 line_bottom_y (struct it *it)
1187 {
1188 int line_height = it->max_ascent + it->max_descent;
1189 int line_top_y = it->current_y;
1190
1191 if (line_height == 0)
1192 {
1193 if (last_height)
1194 line_height = last_height;
1195 else if (IT_CHARPOS (*it) < ZV)
1196 {
1197 move_it_by_lines (it, 1);
1198 line_height = (it->max_ascent || it->max_descent
1199 ? it->max_ascent + it->max_descent
1200 : last_height);
1201 }
1202 else
1203 {
1204 struct glyph_row *row = it->glyph_row;
1205
1206 /* Use the default character height. */
1207 it->glyph_row = NULL;
1208 it->what = IT_CHARACTER;
1209 it->c = ' ';
1210 it->len = 1;
1211 PRODUCE_GLYPHS (it);
1212 line_height = it->ascent + it->descent;
1213 it->glyph_row = row;
1214 }
1215 }
1216
1217 return line_top_y + line_height;
1218 }
1219
1220 /* Subroutine of pos_visible_p below. Extracts a display string, if
1221 any, from the display spec given as its argument. */
1222 static Lisp_Object
1223 string_from_display_spec (Lisp_Object spec)
1224 {
1225 if (CONSP (spec))
1226 {
1227 while (CONSP (spec))
1228 {
1229 if (STRINGP (XCAR (spec)))
1230 return XCAR (spec);
1231 spec = XCDR (spec);
1232 }
1233 }
1234 else if (VECTORP (spec))
1235 {
1236 ptrdiff_t i;
1237
1238 for (i = 0; i < ASIZE (spec); i++)
1239 {
1240 if (STRINGP (AREF (spec, i)))
1241 return AREF (spec, i);
1242 }
1243 return Qnil;
1244 }
1245
1246 return spec;
1247 }
1248
1249
1250 /* Limit insanely large values of W->hscroll on frame F to the largest
1251 value that will still prevent first_visible_x and last_visible_x of
1252 'struct it' from overflowing an int. */
1253 static int
1254 window_hscroll_limited (struct window *w, struct frame *f)
1255 {
1256 ptrdiff_t window_hscroll = w->hscroll;
1257 int window_text_width = window_box_width (w, TEXT_AREA);
1258 int colwidth = FRAME_COLUMN_WIDTH (f);
1259
1260 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1261 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1262
1263 return window_hscroll;
1264 }
1265
1266 /* Return 1 if position CHARPOS is visible in window W.
1267 CHARPOS < 0 means return info about WINDOW_END position.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1271
1272 int
1273 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1274 int *rtop, int *rbot, int *rowh, int *vpos)
1275 {
1276 struct it it;
1277 void *itdata = bidi_shelve_cache ();
1278 struct text_pos top;
1279 int visible_p = 0;
1280 struct buffer *old_buffer = NULL;
1281
1282 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1283 return visible_p;
1284
1285 if (XBUFFER (w->contents) != current_buffer)
1286 {
1287 old_buffer = current_buffer;
1288 set_buffer_internal_1 (XBUFFER (w->contents));
1289 }
1290
1291 SET_TEXT_POS_FROM_MARKER (top, w->start);
1292 /* Scrolling a minibuffer window via scroll bar when the echo area
1293 shows long text sometimes resets the minibuffer contents behind
1294 our backs. */
1295 if (CHARPOS (top) > ZV)
1296 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1297
1298 /* Compute exact mode line heights. */
1299 if (WINDOW_WANTS_MODELINE_P (w))
1300 current_mode_line_height
1301 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1302 BVAR (current_buffer, mode_line_format));
1303
1304 if (WINDOW_WANTS_HEADER_LINE_P (w))
1305 current_header_line_height
1306 = display_mode_line (w, HEADER_LINE_FACE_ID,
1307 BVAR (current_buffer, header_line_format));
1308
1309 start_display (&it, w, top);
1310 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1311 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1312
1313 if (charpos >= 0
1314 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1315 && IT_CHARPOS (it) >= charpos)
1316 /* When scanning backwards under bidi iteration, move_it_to
1317 stops at or _before_ CHARPOS, because it stops at or to
1318 the _right_ of the character at CHARPOS. */
1319 || (it.bidi_p && it.bidi_it.scan_dir == -1
1320 && IT_CHARPOS (it) <= charpos)))
1321 {
1322 /* We have reached CHARPOS, or passed it. How the call to
1323 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1324 or covered by a display property, move_it_to stops at the end
1325 of the invisible text, to the right of CHARPOS. (ii) If
1326 CHARPOS is in a display vector, move_it_to stops on its last
1327 glyph. */
1328 int top_x = it.current_x;
1329 int top_y = it.current_y;
1330 /* Calling line_bottom_y may change it.method, it.position, etc. */
1331 enum it_method it_method = it.method;
1332 int bottom_y = (last_height = 0, line_bottom_y (&it));
1333 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1334
1335 if (top_y < window_top_y)
1336 visible_p = bottom_y > window_top_y;
1337 else if (top_y < it.last_visible_y)
1338 visible_p = 1;
1339 if (bottom_y >= it.last_visible_y
1340 && it.bidi_p && it.bidi_it.scan_dir == -1
1341 && IT_CHARPOS (it) < charpos)
1342 {
1343 /* When the last line of the window is scanned backwards
1344 under bidi iteration, we could be duped into thinking
1345 that we have passed CHARPOS, when in fact move_it_to
1346 simply stopped short of CHARPOS because it reached
1347 last_visible_y. To see if that's what happened, we call
1348 move_it_to again with a slightly larger vertical limit,
1349 and see if it actually moved vertically; if it did, we
1350 didn't really reach CHARPOS, which is beyond window end. */
1351 struct it save_it = it;
1352 /* Why 10? because we don't know how many canonical lines
1353 will the height of the next line(s) be. So we guess. */
1354 int ten_more_lines =
1355 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1356
1357 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1358 MOVE_TO_POS | MOVE_TO_Y);
1359 if (it.current_y > top_y)
1360 visible_p = 0;
1361
1362 it = save_it;
1363 }
1364 if (visible_p)
1365 {
1366 if (it_method == GET_FROM_DISPLAY_VECTOR)
1367 {
1368 /* We stopped on the last glyph of a display vector.
1369 Try and recompute. Hack alert! */
1370 if (charpos < 2 || top.charpos >= charpos)
1371 top_x = it.glyph_row->x;
1372 else
1373 {
1374 struct it it2;
1375 start_display (&it2, w, top);
1376 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1377 get_next_display_element (&it2);
1378 PRODUCE_GLYPHS (&it2);
1379 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1380 || it2.current_x > it2.last_visible_x)
1381 top_x = it.glyph_row->x;
1382 else
1383 {
1384 top_x = it2.current_x;
1385 top_y = it2.current_y;
1386 }
1387 }
1388 }
1389 else if (IT_CHARPOS (it) != charpos)
1390 {
1391 Lisp_Object cpos = make_number (charpos);
1392 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1393 Lisp_Object string = string_from_display_spec (spec);
1394 struct text_pos tpos;
1395 int replacing_spec_p;
1396 bool newline_in_string
1397 = (STRINGP (string)
1398 && memchr (SDATA (string), '\n', SBYTES (string)));
1399
1400 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1401 replacing_spec_p
1402 = (!NILP (spec)
1403 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1404 charpos, FRAME_WINDOW_P (it.f)));
1405 /* The tricky code below is needed because there's a
1406 discrepancy between move_it_to and how we set cursor
1407 when PT is at the beginning of a portion of text
1408 covered by a display property or an overlay with a
1409 display property, or the display line ends in a
1410 newline from a display string. move_it_to will stop
1411 _after_ such display strings, whereas
1412 set_cursor_from_row conspires with cursor_row_p to
1413 place the cursor on the first glyph produced from the
1414 display string. */
1415
1416 /* We have overshoot PT because it is covered by a
1417 display property that replaces the text it covers.
1418 If the string includes embedded newlines, we are also
1419 in the wrong display line. Backtrack to the correct
1420 line, where the display property begins. */
1421 if (replacing_spec_p)
1422 {
1423 Lisp_Object startpos, endpos;
1424 EMACS_INT start, end;
1425 struct it it3;
1426 int it3_moved;
1427
1428 /* Find the first and the last buffer positions
1429 covered by the display string. */
1430 endpos =
1431 Fnext_single_char_property_change (cpos, Qdisplay,
1432 Qnil, Qnil);
1433 startpos =
1434 Fprevious_single_char_property_change (endpos, Qdisplay,
1435 Qnil, Qnil);
1436 start = XFASTINT (startpos);
1437 end = XFASTINT (endpos);
1438 /* Move to the last buffer position before the
1439 display property. */
1440 start_display (&it3, w, top);
1441 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1442 /* Move forward one more line if the position before
1443 the display string is a newline or if it is the
1444 rightmost character on a line that is
1445 continued or word-wrapped. */
1446 if (it3.method == GET_FROM_BUFFER
1447 && (it3.c == '\n'
1448 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1449 move_it_by_lines (&it3, 1);
1450 else if (move_it_in_display_line_to (&it3, -1,
1451 it3.current_x
1452 + it3.pixel_width,
1453 MOVE_TO_X)
1454 == MOVE_LINE_CONTINUED)
1455 {
1456 move_it_by_lines (&it3, 1);
1457 /* When we are under word-wrap, the #$@%!
1458 move_it_by_lines moves 2 lines, so we need to
1459 fix that up. */
1460 if (it3.line_wrap == WORD_WRAP)
1461 move_it_by_lines (&it3, -1);
1462 }
1463
1464 /* Record the vertical coordinate of the display
1465 line where we wound up. */
1466 top_y = it3.current_y;
1467 if (it3.bidi_p)
1468 {
1469 /* When characters are reordered for display,
1470 the character displayed to the left of the
1471 display string could be _after_ the display
1472 property in the logical order. Use the
1473 smallest vertical position of these two. */
1474 start_display (&it3, w, top);
1475 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1476 if (it3.current_y < top_y)
1477 top_y = it3.current_y;
1478 }
1479 /* Move from the top of the window to the beginning
1480 of the display line where the display string
1481 begins. */
1482 start_display (&it3, w, top);
1483 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1484 /* If it3_moved stays zero after the 'while' loop
1485 below, that means we already were at a newline
1486 before the loop (e.g., the display string begins
1487 with a newline), so we don't need to (and cannot)
1488 inspect the glyphs of it3.glyph_row, because
1489 PRODUCE_GLYPHS will not produce anything for a
1490 newline, and thus it3.glyph_row stays at its
1491 stale content it got at top of the window. */
1492 it3_moved = 0;
1493 /* Finally, advance the iterator until we hit the
1494 first display element whose character position is
1495 CHARPOS, or until the first newline from the
1496 display string, which signals the end of the
1497 display line. */
1498 while (get_next_display_element (&it3))
1499 {
1500 PRODUCE_GLYPHS (&it3);
1501 if (IT_CHARPOS (it3) == charpos
1502 || ITERATOR_AT_END_OF_LINE_P (&it3))
1503 break;
1504 it3_moved = 1;
1505 set_iterator_to_next (&it3, 0);
1506 }
1507 top_x = it3.current_x - it3.pixel_width;
1508 /* Normally, we would exit the above loop because we
1509 found the display element whose character
1510 position is CHARPOS. For the contingency that we
1511 didn't, and stopped at the first newline from the
1512 display string, move back over the glyphs
1513 produced from the string, until we find the
1514 rightmost glyph not from the string. */
1515 if (it3_moved
1516 && newline_in_string
1517 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1518 {
1519 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1520 + it3.glyph_row->used[TEXT_AREA];
1521
1522 while (EQ ((g - 1)->object, string))
1523 {
1524 --g;
1525 top_x -= g->pixel_width;
1526 }
1527 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1528 + it3.glyph_row->used[TEXT_AREA]);
1529 }
1530 }
1531 }
1532
1533 *x = top_x;
1534 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1535 *rtop = max (0, window_top_y - top_y);
1536 *rbot = max (0, bottom_y - it.last_visible_y);
1537 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1538 - max (top_y, window_top_y)));
1539 *vpos = it.vpos;
1540 }
1541 }
1542 else
1543 {
1544 /* We were asked to provide info about WINDOW_END. */
1545 struct it it2;
1546 void *it2data = NULL;
1547
1548 SAVE_IT (it2, it, it2data);
1549 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1550 move_it_by_lines (&it, 1);
1551 if (charpos < IT_CHARPOS (it)
1552 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1553 {
1554 visible_p = 1;
1555 RESTORE_IT (&it2, &it2, it2data);
1556 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1557 *x = it2.current_x;
1558 *y = it2.current_y + it2.max_ascent - it2.ascent;
1559 *rtop = max (0, -it2.current_y);
1560 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1561 - it.last_visible_y));
1562 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1563 it.last_visible_y)
1564 - max (it2.current_y,
1565 WINDOW_HEADER_LINE_HEIGHT (w))));
1566 *vpos = it2.vpos;
1567 }
1568 else
1569 bidi_unshelve_cache (it2data, 1);
1570 }
1571 bidi_unshelve_cache (itdata, 0);
1572
1573 if (old_buffer)
1574 set_buffer_internal_1 (old_buffer);
1575
1576 current_header_line_height = current_mode_line_height = -1;
1577
1578 if (visible_p && w->hscroll > 0)
1579 *x -=
1580 window_hscroll_limited (w, WINDOW_XFRAME (w))
1581 * WINDOW_FRAME_COLUMN_WIDTH (w);
1582
1583 #if 0
1584 /* Debugging code. */
1585 if (visible_p)
1586 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1587 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1588 else
1589 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1590 #endif
1591
1592 return visible_p;
1593 }
1594
1595
1596 /* Return the next character from STR. Return in *LEN the length of
1597 the character. This is like STRING_CHAR_AND_LENGTH but never
1598 returns an invalid character. If we find one, we return a `?', but
1599 with the length of the invalid character. */
1600
1601 static int
1602 string_char_and_length (const unsigned char *str, int *len)
1603 {
1604 int c;
1605
1606 c = STRING_CHAR_AND_LENGTH (str, *len);
1607 if (!CHAR_VALID_P (c))
1608 /* We may not change the length here because other places in Emacs
1609 don't use this function, i.e. they silently accept invalid
1610 characters. */
1611 c = '?';
1612
1613 return c;
1614 }
1615
1616
1617
1618 /* Given a position POS containing a valid character and byte position
1619 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1620
1621 static struct text_pos
1622 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1623 {
1624 eassert (STRINGP (string) && nchars >= 0);
1625
1626 if (STRING_MULTIBYTE (string))
1627 {
1628 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1629 int len;
1630
1631 while (nchars--)
1632 {
1633 string_char_and_length (p, &len);
1634 p += len;
1635 CHARPOS (pos) += 1;
1636 BYTEPOS (pos) += len;
1637 }
1638 }
1639 else
1640 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1641
1642 return pos;
1643 }
1644
1645
1646 /* Value is the text position, i.e. character and byte position,
1647 for character position CHARPOS in STRING. */
1648
1649 static struct text_pos
1650 string_pos (ptrdiff_t charpos, Lisp_Object string)
1651 {
1652 struct text_pos pos;
1653 eassert (STRINGP (string));
1654 eassert (charpos >= 0);
1655 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1656 return pos;
1657 }
1658
1659
1660 /* Value is a text position, i.e. character and byte position, for
1661 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1662 means recognize multibyte characters. */
1663
1664 static struct text_pos
1665 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1666 {
1667 struct text_pos pos;
1668
1669 eassert (s != NULL);
1670 eassert (charpos >= 0);
1671
1672 if (multibyte_p)
1673 {
1674 int len;
1675
1676 SET_TEXT_POS (pos, 0, 0);
1677 while (charpos--)
1678 {
1679 string_char_and_length ((const unsigned char *) s, &len);
1680 s += len;
1681 CHARPOS (pos) += 1;
1682 BYTEPOS (pos) += len;
1683 }
1684 }
1685 else
1686 SET_TEXT_POS (pos, charpos, charpos);
1687
1688 return pos;
1689 }
1690
1691
1692 /* Value is the number of characters in C string S. MULTIBYTE_P
1693 non-zero means recognize multibyte characters. */
1694
1695 static ptrdiff_t
1696 number_of_chars (const char *s, bool multibyte_p)
1697 {
1698 ptrdiff_t nchars;
1699
1700 if (multibyte_p)
1701 {
1702 ptrdiff_t rest = strlen (s);
1703 int len;
1704 const unsigned char *p = (const unsigned char *) s;
1705
1706 for (nchars = 0; rest > 0; ++nchars)
1707 {
1708 string_char_and_length (p, &len);
1709 rest -= len, p += len;
1710 }
1711 }
1712 else
1713 nchars = strlen (s);
1714
1715 return nchars;
1716 }
1717
1718
1719 /* Compute byte position NEWPOS->bytepos corresponding to
1720 NEWPOS->charpos. POS is a known position in string STRING.
1721 NEWPOS->charpos must be >= POS.charpos. */
1722
1723 static void
1724 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1725 {
1726 eassert (STRINGP (string));
1727 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1728
1729 if (STRING_MULTIBYTE (string))
1730 *newpos = string_pos_nchars_ahead (pos, string,
1731 CHARPOS (*newpos) - CHARPOS (pos));
1732 else
1733 BYTEPOS (*newpos) = CHARPOS (*newpos);
1734 }
1735
1736 /* EXPORT:
1737 Return an estimation of the pixel height of mode or header lines on
1738 frame F. FACE_ID specifies what line's height to estimate. */
1739
1740 int
1741 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1742 {
1743 #ifdef HAVE_WINDOW_SYSTEM
1744 if (FRAME_WINDOW_P (f))
1745 {
1746 int height = FONT_HEIGHT (FRAME_FONT (f));
1747
1748 /* This function is called so early when Emacs starts that the face
1749 cache and mode line face are not yet initialized. */
1750 if (FRAME_FACE_CACHE (f))
1751 {
1752 struct face *face = FACE_FROM_ID (f, face_id);
1753 if (face)
1754 {
1755 if (face->font)
1756 height = FONT_HEIGHT (face->font);
1757 if (face->box_line_width > 0)
1758 height += 2 * face->box_line_width;
1759 }
1760 }
1761
1762 return height;
1763 }
1764 #endif
1765
1766 return 1;
1767 }
1768
1769 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1770 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1771 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1772 not force the value into range. */
1773
1774 void
1775 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1776 int *x, int *y, NativeRectangle *bounds, int noclip)
1777 {
1778
1779 #ifdef HAVE_WINDOW_SYSTEM
1780 if (FRAME_WINDOW_P (f))
1781 {
1782 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1783 even for negative values. */
1784 if (pix_x < 0)
1785 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1786 if (pix_y < 0)
1787 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1788
1789 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1790 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1791
1792 if (bounds)
1793 STORE_NATIVE_RECT (*bounds,
1794 FRAME_COL_TO_PIXEL_X (f, pix_x),
1795 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1796 FRAME_COLUMN_WIDTH (f) - 1,
1797 FRAME_LINE_HEIGHT (f) - 1);
1798
1799 if (!noclip)
1800 {
1801 if (pix_x < 0)
1802 pix_x = 0;
1803 else if (pix_x > FRAME_TOTAL_COLS (f))
1804 pix_x = FRAME_TOTAL_COLS (f);
1805
1806 if (pix_y < 0)
1807 pix_y = 0;
1808 else if (pix_y > FRAME_LINES (f))
1809 pix_y = FRAME_LINES (f);
1810 }
1811 }
1812 #endif
1813
1814 *x = pix_x;
1815 *y = pix_y;
1816 }
1817
1818
1819 /* Find the glyph under window-relative coordinates X/Y in window W.
1820 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1821 strings. Return in *HPOS and *VPOS the row and column number of
1822 the glyph found. Return in *AREA the glyph area containing X.
1823 Value is a pointer to the glyph found or null if X/Y is not on
1824 text, or we can't tell because W's current matrix is not up to
1825 date. */
1826
1827 static
1828 struct glyph *
1829 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1830 int *dx, int *dy, int *area)
1831 {
1832 struct glyph *glyph, *end;
1833 struct glyph_row *row = NULL;
1834 int x0, i;
1835
1836 /* Find row containing Y. Give up if some row is not enabled. */
1837 for (i = 0; i < w->current_matrix->nrows; ++i)
1838 {
1839 row = MATRIX_ROW (w->current_matrix, i);
1840 if (!row->enabled_p)
1841 return NULL;
1842 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1843 break;
1844 }
1845
1846 *vpos = i;
1847 *hpos = 0;
1848
1849 /* Give up if Y is not in the window. */
1850 if (i == w->current_matrix->nrows)
1851 return NULL;
1852
1853 /* Get the glyph area containing X. */
1854 if (w->pseudo_window_p)
1855 {
1856 *area = TEXT_AREA;
1857 x0 = 0;
1858 }
1859 else
1860 {
1861 if (x < window_box_left_offset (w, TEXT_AREA))
1862 {
1863 *area = LEFT_MARGIN_AREA;
1864 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1865 }
1866 else if (x < window_box_right_offset (w, TEXT_AREA))
1867 {
1868 *area = TEXT_AREA;
1869 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1870 }
1871 else
1872 {
1873 *area = RIGHT_MARGIN_AREA;
1874 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1875 }
1876 }
1877
1878 /* Find glyph containing X. */
1879 glyph = row->glyphs[*area];
1880 end = glyph + row->used[*area];
1881 x -= x0;
1882 while (glyph < end && x >= glyph->pixel_width)
1883 {
1884 x -= glyph->pixel_width;
1885 ++glyph;
1886 }
1887
1888 if (glyph == end)
1889 return NULL;
1890
1891 if (dx)
1892 {
1893 *dx = x;
1894 *dy = y - (row->y + row->ascent - glyph->ascent);
1895 }
1896
1897 *hpos = glyph - row->glyphs[*area];
1898 return glyph;
1899 }
1900
1901 /* Convert frame-relative x/y to coordinates relative to window W.
1902 Takes pseudo-windows into account. */
1903
1904 static void
1905 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1906 {
1907 if (w->pseudo_window_p)
1908 {
1909 /* A pseudo-window is always full-width, and starts at the
1910 left edge of the frame, plus a frame border. */
1911 struct frame *f = XFRAME (w->frame);
1912 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1913 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1914 }
1915 else
1916 {
1917 *x -= WINDOW_LEFT_EDGE_X (w);
1918 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1919 }
1920 }
1921
1922 #ifdef HAVE_WINDOW_SYSTEM
1923
1924 /* EXPORT:
1925 Return in RECTS[] at most N clipping rectangles for glyph string S.
1926 Return the number of stored rectangles. */
1927
1928 int
1929 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1930 {
1931 XRectangle r;
1932
1933 if (n <= 0)
1934 return 0;
1935
1936 if (s->row->full_width_p)
1937 {
1938 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1939 r.x = WINDOW_LEFT_EDGE_X (s->w);
1940 r.width = WINDOW_TOTAL_WIDTH (s->w);
1941
1942 /* Unless displaying a mode or menu bar line, which are always
1943 fully visible, clip to the visible part of the row. */
1944 if (s->w->pseudo_window_p)
1945 r.height = s->row->visible_height;
1946 else
1947 r.height = s->height;
1948 }
1949 else
1950 {
1951 /* This is a text line that may be partially visible. */
1952 r.x = window_box_left (s->w, s->area);
1953 r.width = window_box_width (s->w, s->area);
1954 r.height = s->row->visible_height;
1955 }
1956
1957 if (s->clip_head)
1958 if (r.x < s->clip_head->x)
1959 {
1960 if (r.width >= s->clip_head->x - r.x)
1961 r.width -= s->clip_head->x - r.x;
1962 else
1963 r.width = 0;
1964 r.x = s->clip_head->x;
1965 }
1966 if (s->clip_tail)
1967 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1968 {
1969 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1970 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1971 else
1972 r.width = 0;
1973 }
1974
1975 /* If S draws overlapping rows, it's sufficient to use the top and
1976 bottom of the window for clipping because this glyph string
1977 intentionally draws over other lines. */
1978 if (s->for_overlaps)
1979 {
1980 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1981 r.height = window_text_bottom_y (s->w) - r.y;
1982
1983 /* Alas, the above simple strategy does not work for the
1984 environments with anti-aliased text: if the same text is
1985 drawn onto the same place multiple times, it gets thicker.
1986 If the overlap we are processing is for the erased cursor, we
1987 take the intersection with the rectangle of the cursor. */
1988 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1989 {
1990 XRectangle rc, r_save = r;
1991
1992 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1993 rc.y = s->w->phys_cursor.y;
1994 rc.width = s->w->phys_cursor_width;
1995 rc.height = s->w->phys_cursor_height;
1996
1997 x_intersect_rectangles (&r_save, &rc, &r);
1998 }
1999 }
2000 else
2001 {
2002 /* Don't use S->y for clipping because it doesn't take partially
2003 visible lines into account. For example, it can be negative for
2004 partially visible lines at the top of a window. */
2005 if (!s->row->full_width_p
2006 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2007 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2008 else
2009 r.y = max (0, s->row->y);
2010 }
2011
2012 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2013
2014 /* If drawing the cursor, don't let glyph draw outside its
2015 advertised boundaries. Cleartype does this under some circumstances. */
2016 if (s->hl == DRAW_CURSOR)
2017 {
2018 struct glyph *glyph = s->first_glyph;
2019 int height, max_y;
2020
2021 if (s->x > r.x)
2022 {
2023 r.width -= s->x - r.x;
2024 r.x = s->x;
2025 }
2026 r.width = min (r.width, glyph->pixel_width);
2027
2028 /* If r.y is below window bottom, ensure that we still see a cursor. */
2029 height = min (glyph->ascent + glyph->descent,
2030 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2031 max_y = window_text_bottom_y (s->w) - height;
2032 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2033 if (s->ybase - glyph->ascent > max_y)
2034 {
2035 r.y = max_y;
2036 r.height = height;
2037 }
2038 else
2039 {
2040 /* Don't draw cursor glyph taller than our actual glyph. */
2041 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2042 if (height < r.height)
2043 {
2044 max_y = r.y + r.height;
2045 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2046 r.height = min (max_y - r.y, height);
2047 }
2048 }
2049 }
2050
2051 if (s->row->clip)
2052 {
2053 XRectangle r_save = r;
2054
2055 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2056 r.width = 0;
2057 }
2058
2059 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2060 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2061 {
2062 #ifdef CONVERT_FROM_XRECT
2063 CONVERT_FROM_XRECT (r, *rects);
2064 #else
2065 *rects = r;
2066 #endif
2067 return 1;
2068 }
2069 else
2070 {
2071 /* If we are processing overlapping and allowed to return
2072 multiple clipping rectangles, we exclude the row of the glyph
2073 string from the clipping rectangle. This is to avoid drawing
2074 the same text on the environment with anti-aliasing. */
2075 #ifdef CONVERT_FROM_XRECT
2076 XRectangle rs[2];
2077 #else
2078 XRectangle *rs = rects;
2079 #endif
2080 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2081
2082 if (s->for_overlaps & OVERLAPS_PRED)
2083 {
2084 rs[i] = r;
2085 if (r.y + r.height > row_y)
2086 {
2087 if (r.y < row_y)
2088 rs[i].height = row_y - r.y;
2089 else
2090 rs[i].height = 0;
2091 }
2092 i++;
2093 }
2094 if (s->for_overlaps & OVERLAPS_SUCC)
2095 {
2096 rs[i] = r;
2097 if (r.y < row_y + s->row->visible_height)
2098 {
2099 if (r.y + r.height > row_y + s->row->visible_height)
2100 {
2101 rs[i].y = row_y + s->row->visible_height;
2102 rs[i].height = r.y + r.height - rs[i].y;
2103 }
2104 else
2105 rs[i].height = 0;
2106 }
2107 i++;
2108 }
2109
2110 n = i;
2111 #ifdef CONVERT_FROM_XRECT
2112 for (i = 0; i < n; i++)
2113 CONVERT_FROM_XRECT (rs[i], rects[i]);
2114 #endif
2115 return n;
2116 }
2117 }
2118
2119 /* EXPORT:
2120 Return in *NR the clipping rectangle for glyph string S. */
2121
2122 void
2123 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2124 {
2125 get_glyph_string_clip_rects (s, nr, 1);
2126 }
2127
2128
2129 /* EXPORT:
2130 Return the position and height of the phys cursor in window W.
2131 Set w->phys_cursor_width to width of phys cursor.
2132 */
2133
2134 void
2135 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2136 struct glyph *glyph, int *xp, int *yp, int *heightp)
2137 {
2138 struct frame *f = XFRAME (WINDOW_FRAME (w));
2139 int x, y, wd, h, h0, y0;
2140
2141 /* Compute the width of the rectangle to draw. If on a stretch
2142 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2143 rectangle as wide as the glyph, but use a canonical character
2144 width instead. */
2145 wd = glyph->pixel_width - 1;
2146 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2147 wd++; /* Why? */
2148 #endif
2149
2150 x = w->phys_cursor.x;
2151 if (x < 0)
2152 {
2153 wd += x;
2154 x = 0;
2155 }
2156
2157 if (glyph->type == STRETCH_GLYPH
2158 && !x_stretch_cursor_p)
2159 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2160 w->phys_cursor_width = wd;
2161
2162 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2163
2164 /* If y is below window bottom, ensure that we still see a cursor. */
2165 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2166
2167 h = max (h0, glyph->ascent + glyph->descent);
2168 h0 = min (h0, glyph->ascent + glyph->descent);
2169
2170 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2171 if (y < y0)
2172 {
2173 h = max (h - (y0 - y) + 1, h0);
2174 y = y0 - 1;
2175 }
2176 else
2177 {
2178 y0 = window_text_bottom_y (w) - h0;
2179 if (y > y0)
2180 {
2181 h += y - y0;
2182 y = y0;
2183 }
2184 }
2185
2186 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2187 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2188 *heightp = h;
2189 }
2190
2191 /*
2192 * Remember which glyph the mouse is over.
2193 */
2194
2195 void
2196 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2197 {
2198 Lisp_Object window;
2199 struct window *w;
2200 struct glyph_row *r, *gr, *end_row;
2201 enum window_part part;
2202 enum glyph_row_area area;
2203 int x, y, width, height;
2204
2205 /* Try to determine frame pixel position and size of the glyph under
2206 frame pixel coordinates X/Y on frame F. */
2207
2208 if (!f->glyphs_initialized_p
2209 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2210 NILP (window)))
2211 {
2212 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2213 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2214 goto virtual_glyph;
2215 }
2216
2217 w = XWINDOW (window);
2218 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2219 height = WINDOW_FRAME_LINE_HEIGHT (w);
2220
2221 x = window_relative_x_coord (w, part, gx);
2222 y = gy - WINDOW_TOP_EDGE_Y (w);
2223
2224 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2225 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2226
2227 if (w->pseudo_window_p)
2228 {
2229 area = TEXT_AREA;
2230 part = ON_MODE_LINE; /* Don't adjust margin. */
2231 goto text_glyph;
2232 }
2233
2234 switch (part)
2235 {
2236 case ON_LEFT_MARGIN:
2237 area = LEFT_MARGIN_AREA;
2238 goto text_glyph;
2239
2240 case ON_RIGHT_MARGIN:
2241 area = RIGHT_MARGIN_AREA;
2242 goto text_glyph;
2243
2244 case ON_HEADER_LINE:
2245 case ON_MODE_LINE:
2246 gr = (part == ON_HEADER_LINE
2247 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2248 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2249 gy = gr->y;
2250 area = TEXT_AREA;
2251 goto text_glyph_row_found;
2252
2253 case ON_TEXT:
2254 area = TEXT_AREA;
2255
2256 text_glyph:
2257 gr = 0; gy = 0;
2258 for (; r <= end_row && r->enabled_p; ++r)
2259 if (r->y + r->height > y)
2260 {
2261 gr = r; gy = r->y;
2262 break;
2263 }
2264
2265 text_glyph_row_found:
2266 if (gr && gy <= y)
2267 {
2268 struct glyph *g = gr->glyphs[area];
2269 struct glyph *end = g + gr->used[area];
2270
2271 height = gr->height;
2272 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2273 if (gx + g->pixel_width > x)
2274 break;
2275
2276 if (g < end)
2277 {
2278 if (g->type == IMAGE_GLYPH)
2279 {
2280 /* Don't remember when mouse is over image, as
2281 image may have hot-spots. */
2282 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2283 return;
2284 }
2285 width = g->pixel_width;
2286 }
2287 else
2288 {
2289 /* Use nominal char spacing at end of line. */
2290 x -= gx;
2291 gx += (x / width) * width;
2292 }
2293
2294 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2295 gx += window_box_left_offset (w, area);
2296 }
2297 else
2298 {
2299 /* Use nominal line height at end of window. */
2300 gx = (x / width) * width;
2301 y -= gy;
2302 gy += (y / height) * height;
2303 }
2304 break;
2305
2306 case ON_LEFT_FRINGE:
2307 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2308 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2309 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2310 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2311 goto row_glyph;
2312
2313 case ON_RIGHT_FRINGE:
2314 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2315 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2316 : window_box_right_offset (w, TEXT_AREA));
2317 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2318 goto row_glyph;
2319
2320 case ON_SCROLL_BAR:
2321 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2322 ? 0
2323 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2324 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2325 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2326 : 0)));
2327 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2328
2329 row_glyph:
2330 gr = 0, gy = 0;
2331 for (; r <= end_row && r->enabled_p; ++r)
2332 if (r->y + r->height > y)
2333 {
2334 gr = r; gy = r->y;
2335 break;
2336 }
2337
2338 if (gr && gy <= y)
2339 height = gr->height;
2340 else
2341 {
2342 /* Use nominal line height at end of window. */
2343 y -= gy;
2344 gy += (y / height) * height;
2345 }
2346 break;
2347
2348 default:
2349 ;
2350 virtual_glyph:
2351 /* If there is no glyph under the mouse, then we divide the screen
2352 into a grid of the smallest glyph in the frame, and use that
2353 as our "glyph". */
2354
2355 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2356 round down even for negative values. */
2357 if (gx < 0)
2358 gx -= width - 1;
2359 if (gy < 0)
2360 gy -= height - 1;
2361
2362 gx = (gx / width) * width;
2363 gy = (gy / height) * height;
2364
2365 goto store_rect;
2366 }
2367
2368 gx += WINDOW_LEFT_EDGE_X (w);
2369 gy += WINDOW_TOP_EDGE_Y (w);
2370
2371 store_rect:
2372 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2373
2374 /* Visible feedback for debugging. */
2375 #if 0
2376 #if HAVE_X_WINDOWS
2377 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2378 f->output_data.x->normal_gc,
2379 gx, gy, width, height);
2380 #endif
2381 #endif
2382 }
2383
2384
2385 #endif /* HAVE_WINDOW_SYSTEM */
2386
2387 \f
2388 /***********************************************************************
2389 Lisp form evaluation
2390 ***********************************************************************/
2391
2392 /* Error handler for safe_eval and safe_call. */
2393
2394 static Lisp_Object
2395 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2396 {
2397 add_to_log ("Error during redisplay: %S signaled %S",
2398 Flist (nargs, args), arg);
2399 return Qnil;
2400 }
2401
2402 /* Call function FUNC with the rest of NARGS - 1 arguments
2403 following. Return the result, or nil if something went
2404 wrong. Prevent redisplay during the evaluation. */
2405
2406 Lisp_Object
2407 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2408 {
2409 Lisp_Object val;
2410
2411 if (inhibit_eval_during_redisplay)
2412 val = Qnil;
2413 else
2414 {
2415 va_list ap;
2416 ptrdiff_t i;
2417 ptrdiff_t count = SPECPDL_INDEX ();
2418 struct gcpro gcpro1;
2419 Lisp_Object *args = alloca (nargs * word_size);
2420
2421 args[0] = func;
2422 va_start (ap, func);
2423 for (i = 1; i < nargs; i++)
2424 args[i] = va_arg (ap, Lisp_Object);
2425 va_end (ap);
2426
2427 GCPRO1 (args[0]);
2428 gcpro1.nvars = nargs;
2429 specbind (Qinhibit_redisplay, Qt);
2430 /* Use Qt to ensure debugger does not run,
2431 so there is no possibility of wanting to redisplay. */
2432 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2433 safe_eval_handler);
2434 UNGCPRO;
2435 val = unbind_to (count, val);
2436 }
2437
2438 return val;
2439 }
2440
2441
2442 /* Call function FN with one argument ARG.
2443 Return the result, or nil if something went wrong. */
2444
2445 Lisp_Object
2446 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2447 {
2448 return safe_call (2, fn, arg);
2449 }
2450
2451 static Lisp_Object Qeval;
2452
2453 Lisp_Object
2454 safe_eval (Lisp_Object sexpr)
2455 {
2456 return safe_call1 (Qeval, sexpr);
2457 }
2458
2459 /* Call function FN with two arguments ARG1 and ARG2.
2460 Return the result, or nil if something went wrong. */
2461
2462 Lisp_Object
2463 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2464 {
2465 return safe_call (3, fn, arg1, arg2);
2466 }
2467
2468
2469 \f
2470 /***********************************************************************
2471 Debugging
2472 ***********************************************************************/
2473
2474 #if 0
2475
2476 /* Define CHECK_IT to perform sanity checks on iterators.
2477 This is for debugging. It is too slow to do unconditionally. */
2478
2479 static void
2480 check_it (struct it *it)
2481 {
2482 if (it->method == GET_FROM_STRING)
2483 {
2484 eassert (STRINGP (it->string));
2485 eassert (IT_STRING_CHARPOS (*it) >= 0);
2486 }
2487 else
2488 {
2489 eassert (IT_STRING_CHARPOS (*it) < 0);
2490 if (it->method == GET_FROM_BUFFER)
2491 {
2492 /* Check that character and byte positions agree. */
2493 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2494 }
2495 }
2496
2497 if (it->dpvec)
2498 eassert (it->current.dpvec_index >= 0);
2499 else
2500 eassert (it->current.dpvec_index < 0);
2501 }
2502
2503 #define CHECK_IT(IT) check_it ((IT))
2504
2505 #else /* not 0 */
2506
2507 #define CHECK_IT(IT) (void) 0
2508
2509 #endif /* not 0 */
2510
2511
2512 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2513
2514 /* Check that the window end of window W is what we expect it
2515 to be---the last row in the current matrix displaying text. */
2516
2517 static void
2518 check_window_end (struct window *w)
2519 {
2520 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2521 {
2522 struct glyph_row *row;
2523 eassert ((row = MATRIX_ROW (w->current_matrix,
2524 XFASTINT (w->window_end_vpos)),
2525 !row->enabled_p
2526 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2527 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2528 }
2529 }
2530
2531 #define CHECK_WINDOW_END(W) check_window_end ((W))
2532
2533 #else
2534
2535 #define CHECK_WINDOW_END(W) (void) 0
2536
2537 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2538
2539 /* Return mark position if current buffer has the region of non-zero length,
2540 or -1 otherwise. */
2541
2542 static ptrdiff_t
2543 markpos_of_region (void)
2544 {
2545 if (!NILP (Vtransient_mark_mode)
2546 && !NILP (BVAR (current_buffer, mark_active))
2547 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2548 {
2549 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2550
2551 if (markpos != PT)
2552 return markpos;
2553 }
2554 return -1;
2555 }
2556
2557 /***********************************************************************
2558 Iterator initialization
2559 ***********************************************************************/
2560
2561 /* Initialize IT for displaying current_buffer in window W, starting
2562 at character position CHARPOS. CHARPOS < 0 means that no buffer
2563 position is specified which is useful when the iterator is assigned
2564 a position later. BYTEPOS is the byte position corresponding to
2565 CHARPOS.
2566
2567 If ROW is not null, calls to produce_glyphs with IT as parameter
2568 will produce glyphs in that row.
2569
2570 BASE_FACE_ID is the id of a base face to use. It must be one of
2571 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2572 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2573 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2574
2575 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2576 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2577 will be initialized to use the corresponding mode line glyph row of
2578 the desired matrix of W. */
2579
2580 void
2581 init_iterator (struct it *it, struct window *w,
2582 ptrdiff_t charpos, ptrdiff_t bytepos,
2583 struct glyph_row *row, enum face_id base_face_id)
2584 {
2585 ptrdiff_t markpos;
2586 enum face_id remapped_base_face_id = base_face_id;
2587
2588 /* Some precondition checks. */
2589 eassert (w != NULL && it != NULL);
2590 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2591 && charpos <= ZV));
2592
2593 /* If face attributes have been changed since the last redisplay,
2594 free realized faces now because they depend on face definitions
2595 that might have changed. Don't free faces while there might be
2596 desired matrices pending which reference these faces. */
2597 if (face_change_count && !inhibit_free_realized_faces)
2598 {
2599 face_change_count = 0;
2600 free_all_realized_faces (Qnil);
2601 }
2602
2603 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2604 if (! NILP (Vface_remapping_alist))
2605 remapped_base_face_id
2606 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2607
2608 /* Use one of the mode line rows of W's desired matrix if
2609 appropriate. */
2610 if (row == NULL)
2611 {
2612 if (base_face_id == MODE_LINE_FACE_ID
2613 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2614 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2615 else if (base_face_id == HEADER_LINE_FACE_ID)
2616 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2617 }
2618
2619 /* Clear IT. */
2620 memset (it, 0, sizeof *it);
2621 it->current.overlay_string_index = -1;
2622 it->current.dpvec_index = -1;
2623 it->base_face_id = remapped_base_face_id;
2624 it->string = Qnil;
2625 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2626 it->paragraph_embedding = L2R;
2627 it->bidi_it.string.lstring = Qnil;
2628 it->bidi_it.string.s = NULL;
2629 it->bidi_it.string.bufpos = 0;
2630
2631 /* The window in which we iterate over current_buffer: */
2632 XSETWINDOW (it->window, w);
2633 it->w = w;
2634 it->f = XFRAME (w->frame);
2635
2636 it->cmp_it.id = -1;
2637
2638 /* Extra space between lines (on window systems only). */
2639 if (base_face_id == DEFAULT_FACE_ID
2640 && FRAME_WINDOW_P (it->f))
2641 {
2642 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2643 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2644 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2645 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2646 * FRAME_LINE_HEIGHT (it->f));
2647 else if (it->f->extra_line_spacing > 0)
2648 it->extra_line_spacing = it->f->extra_line_spacing;
2649 it->max_extra_line_spacing = 0;
2650 }
2651
2652 /* If realized faces have been removed, e.g. because of face
2653 attribute changes of named faces, recompute them. When running
2654 in batch mode, the face cache of the initial frame is null. If
2655 we happen to get called, make a dummy face cache. */
2656 if (FRAME_FACE_CACHE (it->f) == NULL)
2657 init_frame_faces (it->f);
2658 if (FRAME_FACE_CACHE (it->f)->used == 0)
2659 recompute_basic_faces (it->f);
2660
2661 /* Current value of the `slice', `space-width', and 'height' properties. */
2662 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2663 it->space_width = Qnil;
2664 it->font_height = Qnil;
2665 it->override_ascent = -1;
2666
2667 /* Are control characters displayed as `^C'? */
2668 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2669
2670 /* -1 means everything between a CR and the following line end
2671 is invisible. >0 means lines indented more than this value are
2672 invisible. */
2673 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2674 ? (clip_to_bounds
2675 (-1, XINT (BVAR (current_buffer, selective_display)),
2676 PTRDIFF_MAX))
2677 : (!NILP (BVAR (current_buffer, selective_display))
2678 ? -1 : 0));
2679 it->selective_display_ellipsis_p
2680 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2681
2682 /* Display table to use. */
2683 it->dp = window_display_table (w);
2684
2685 /* Are multibyte characters enabled in current_buffer? */
2686 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2687
2688 /* If visible region is of non-zero length, set IT->region_beg_charpos
2689 and IT->region_end_charpos to the start and end of a visible region
2690 in window IT->w. Set both to -1 to indicate no region. */
2691 markpos = markpos_of_region ();
2692 if (markpos >= 0
2693 /* Maybe highlight only in selected window. */
2694 && (/* Either show region everywhere. */
2695 highlight_nonselected_windows
2696 /* Or show region in the selected window. */
2697 || w == XWINDOW (selected_window)
2698 /* Or show the region if we are in the mini-buffer and W is
2699 the window the mini-buffer refers to. */
2700 || (MINI_WINDOW_P (XWINDOW (selected_window))
2701 && WINDOWP (minibuf_selected_window)
2702 && w == XWINDOW (minibuf_selected_window))))
2703 {
2704 it->region_beg_charpos = min (PT, markpos);
2705 it->region_end_charpos = max (PT, markpos);
2706 }
2707 else
2708 it->region_beg_charpos = it->region_end_charpos = -1;
2709
2710 /* Get the position at which the redisplay_end_trigger hook should
2711 be run, if it is to be run at all. */
2712 if (MARKERP (w->redisplay_end_trigger)
2713 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2714 it->redisplay_end_trigger_charpos
2715 = marker_position (w->redisplay_end_trigger);
2716 else if (INTEGERP (w->redisplay_end_trigger))
2717 it->redisplay_end_trigger_charpos =
2718 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2719
2720 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2721
2722 /* Are lines in the display truncated? */
2723 if (base_face_id != DEFAULT_FACE_ID
2724 || it->w->hscroll
2725 || (! WINDOW_FULL_WIDTH_P (it->w)
2726 && ((!NILP (Vtruncate_partial_width_windows)
2727 && !INTEGERP (Vtruncate_partial_width_windows))
2728 || (INTEGERP (Vtruncate_partial_width_windows)
2729 && (WINDOW_TOTAL_COLS (it->w)
2730 < XINT (Vtruncate_partial_width_windows))))))
2731 it->line_wrap = TRUNCATE;
2732 else if (NILP (BVAR (current_buffer, truncate_lines)))
2733 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2734 ? WINDOW_WRAP : WORD_WRAP;
2735 else
2736 it->line_wrap = TRUNCATE;
2737
2738 /* Get dimensions of truncation and continuation glyphs. These are
2739 displayed as fringe bitmaps under X, but we need them for such
2740 frames when the fringes are turned off. But leave the dimensions
2741 zero for tooltip frames, as these glyphs look ugly there and also
2742 sabotage calculations of tooltip dimensions in x-show-tip. */
2743 #ifdef HAVE_WINDOW_SYSTEM
2744 if (!(FRAME_WINDOW_P (it->f)
2745 && FRAMEP (tip_frame)
2746 && it->f == XFRAME (tip_frame)))
2747 #endif
2748 {
2749 if (it->line_wrap == TRUNCATE)
2750 {
2751 /* We will need the truncation glyph. */
2752 eassert (it->glyph_row == NULL);
2753 produce_special_glyphs (it, IT_TRUNCATION);
2754 it->truncation_pixel_width = it->pixel_width;
2755 }
2756 else
2757 {
2758 /* We will need the continuation glyph. */
2759 eassert (it->glyph_row == NULL);
2760 produce_special_glyphs (it, IT_CONTINUATION);
2761 it->continuation_pixel_width = it->pixel_width;
2762 }
2763 }
2764
2765 /* Reset these values to zero because the produce_special_glyphs
2766 above has changed them. */
2767 it->pixel_width = it->ascent = it->descent = 0;
2768 it->phys_ascent = it->phys_descent = 0;
2769
2770 /* Set this after getting the dimensions of truncation and
2771 continuation glyphs, so that we don't produce glyphs when calling
2772 produce_special_glyphs, above. */
2773 it->glyph_row = row;
2774 it->area = TEXT_AREA;
2775
2776 /* Forget any previous info about this row being reversed. */
2777 if (it->glyph_row)
2778 it->glyph_row->reversed_p = 0;
2779
2780 /* Get the dimensions of the display area. The display area
2781 consists of the visible window area plus a horizontally scrolled
2782 part to the left of the window. All x-values are relative to the
2783 start of this total display area. */
2784 if (base_face_id != DEFAULT_FACE_ID)
2785 {
2786 /* Mode lines, menu bar in terminal frames. */
2787 it->first_visible_x = 0;
2788 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2789 }
2790 else
2791 {
2792 it->first_visible_x =
2793 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2794 it->last_visible_x = (it->first_visible_x
2795 + window_box_width (w, TEXT_AREA));
2796
2797 /* If we truncate lines, leave room for the truncation glyph(s) at
2798 the right margin. Otherwise, leave room for the continuation
2799 glyph(s). Done only if the window has no fringes. Since we
2800 don't know at this point whether there will be any R2L lines in
2801 the window, we reserve space for truncation/continuation glyphs
2802 even if only one of the fringes is absent. */
2803 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2804 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2805 {
2806 if (it->line_wrap == TRUNCATE)
2807 it->last_visible_x -= it->truncation_pixel_width;
2808 else
2809 it->last_visible_x -= it->continuation_pixel_width;
2810 }
2811
2812 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2813 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2814 }
2815
2816 /* Leave room for a border glyph. */
2817 if (!FRAME_WINDOW_P (it->f)
2818 && !WINDOW_RIGHTMOST_P (it->w))
2819 it->last_visible_x -= 1;
2820
2821 it->last_visible_y = window_text_bottom_y (w);
2822
2823 /* For mode lines and alike, arrange for the first glyph having a
2824 left box line if the face specifies a box. */
2825 if (base_face_id != DEFAULT_FACE_ID)
2826 {
2827 struct face *face;
2828
2829 it->face_id = remapped_base_face_id;
2830
2831 /* If we have a boxed mode line, make the first character appear
2832 with a left box line. */
2833 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2834 if (face->box != FACE_NO_BOX)
2835 it->start_of_box_run_p = 1;
2836 }
2837
2838 /* If a buffer position was specified, set the iterator there,
2839 getting overlays and face properties from that position. */
2840 if (charpos >= BUF_BEG (current_buffer))
2841 {
2842 it->end_charpos = ZV;
2843 eassert (charpos == BYTE_TO_CHAR (bytepos));
2844 IT_CHARPOS (*it) = charpos;
2845 IT_BYTEPOS (*it) = bytepos;
2846
2847 /* We will rely on `reseat' to set this up properly, via
2848 handle_face_prop. */
2849 it->face_id = it->base_face_id;
2850
2851 it->start = it->current;
2852 /* Do we need to reorder bidirectional text? Not if this is a
2853 unibyte buffer: by definition, none of the single-byte
2854 characters are strong R2L, so no reordering is needed. And
2855 bidi.c doesn't support unibyte buffers anyway. Also, don't
2856 reorder while we are loading loadup.el, since the tables of
2857 character properties needed for reordering are not yet
2858 available. */
2859 it->bidi_p =
2860 NILP (Vpurify_flag)
2861 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2862 && it->multibyte_p;
2863
2864 /* If we are to reorder bidirectional text, init the bidi
2865 iterator. */
2866 if (it->bidi_p)
2867 {
2868 /* Note the paragraph direction that this buffer wants to
2869 use. */
2870 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2871 Qleft_to_right))
2872 it->paragraph_embedding = L2R;
2873 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2874 Qright_to_left))
2875 it->paragraph_embedding = R2L;
2876 else
2877 it->paragraph_embedding = NEUTRAL_DIR;
2878 bidi_unshelve_cache (NULL, 0);
2879 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2880 &it->bidi_it);
2881 }
2882
2883 /* Compute faces etc. */
2884 reseat (it, it->current.pos, 1);
2885 }
2886
2887 CHECK_IT (it);
2888 }
2889
2890
2891 /* Initialize IT for the display of window W with window start POS. */
2892
2893 void
2894 start_display (struct it *it, struct window *w, struct text_pos pos)
2895 {
2896 struct glyph_row *row;
2897 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2898
2899 row = w->desired_matrix->rows + first_vpos;
2900 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2901 it->first_vpos = first_vpos;
2902
2903 /* Don't reseat to previous visible line start if current start
2904 position is in a string or image. */
2905 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2906 {
2907 int start_at_line_beg_p;
2908 int first_y = it->current_y;
2909
2910 /* If window start is not at a line start, skip forward to POS to
2911 get the correct continuation lines width. */
2912 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2913 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2914 if (!start_at_line_beg_p)
2915 {
2916 int new_x;
2917
2918 reseat_at_previous_visible_line_start (it);
2919 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2920
2921 new_x = it->current_x + it->pixel_width;
2922
2923 /* If lines are continued, this line may end in the middle
2924 of a multi-glyph character (e.g. a control character
2925 displayed as \003, or in the middle of an overlay
2926 string). In this case move_it_to above will not have
2927 taken us to the start of the continuation line but to the
2928 end of the continued line. */
2929 if (it->current_x > 0
2930 && it->line_wrap != TRUNCATE /* Lines are continued. */
2931 && (/* And glyph doesn't fit on the line. */
2932 new_x > it->last_visible_x
2933 /* Or it fits exactly and we're on a window
2934 system frame. */
2935 || (new_x == it->last_visible_x
2936 && FRAME_WINDOW_P (it->f)
2937 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2938 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2939 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2940 {
2941 if ((it->current.dpvec_index >= 0
2942 || it->current.overlay_string_index >= 0)
2943 /* If we are on a newline from a display vector or
2944 overlay string, then we are already at the end of
2945 a screen line; no need to go to the next line in
2946 that case, as this line is not really continued.
2947 (If we do go to the next line, C-e will not DTRT.) */
2948 && it->c != '\n')
2949 {
2950 set_iterator_to_next (it, 1);
2951 move_it_in_display_line_to (it, -1, -1, 0);
2952 }
2953
2954 it->continuation_lines_width += it->current_x;
2955 }
2956 /* If the character at POS is displayed via a display
2957 vector, move_it_to above stops at the final glyph of
2958 IT->dpvec. To make the caller redisplay that character
2959 again (a.k.a. start at POS), we need to reset the
2960 dpvec_index to the beginning of IT->dpvec. */
2961 else if (it->current.dpvec_index >= 0)
2962 it->current.dpvec_index = 0;
2963
2964 /* We're starting a new display line, not affected by the
2965 height of the continued line, so clear the appropriate
2966 fields in the iterator structure. */
2967 it->max_ascent = it->max_descent = 0;
2968 it->max_phys_ascent = it->max_phys_descent = 0;
2969
2970 it->current_y = first_y;
2971 it->vpos = 0;
2972 it->current_x = it->hpos = 0;
2973 }
2974 }
2975 }
2976
2977
2978 /* Return 1 if POS is a position in ellipses displayed for invisible
2979 text. W is the window we display, for text property lookup. */
2980
2981 static int
2982 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2983 {
2984 Lisp_Object prop, window;
2985 int ellipses_p = 0;
2986 ptrdiff_t charpos = CHARPOS (pos->pos);
2987
2988 /* If POS specifies a position in a display vector, this might
2989 be for an ellipsis displayed for invisible text. We won't
2990 get the iterator set up for delivering that ellipsis unless
2991 we make sure that it gets aware of the invisible text. */
2992 if (pos->dpvec_index >= 0
2993 && pos->overlay_string_index < 0
2994 && CHARPOS (pos->string_pos) < 0
2995 && charpos > BEGV
2996 && (XSETWINDOW (window, w),
2997 prop = Fget_char_property (make_number (charpos),
2998 Qinvisible, window),
2999 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3000 {
3001 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3002 window);
3003 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3004 }
3005
3006 return ellipses_p;
3007 }
3008
3009
3010 /* Initialize IT for stepping through current_buffer in window W,
3011 starting at position POS that includes overlay string and display
3012 vector/ control character translation position information. Value
3013 is zero if there are overlay strings with newlines at POS. */
3014
3015 static int
3016 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3017 {
3018 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3019 int i, overlay_strings_with_newlines = 0;
3020
3021 /* If POS specifies a position in a display vector, this might
3022 be for an ellipsis displayed for invisible text. We won't
3023 get the iterator set up for delivering that ellipsis unless
3024 we make sure that it gets aware of the invisible text. */
3025 if (in_ellipses_for_invisible_text_p (pos, w))
3026 {
3027 --charpos;
3028 bytepos = 0;
3029 }
3030
3031 /* Keep in mind: the call to reseat in init_iterator skips invisible
3032 text, so we might end up at a position different from POS. This
3033 is only a problem when POS is a row start after a newline and an
3034 overlay starts there with an after-string, and the overlay has an
3035 invisible property. Since we don't skip invisible text in
3036 display_line and elsewhere immediately after consuming the
3037 newline before the row start, such a POS will not be in a string,
3038 but the call to init_iterator below will move us to the
3039 after-string. */
3040 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3041
3042 /* This only scans the current chunk -- it should scan all chunks.
3043 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3044 to 16 in 22.1 to make this a lesser problem. */
3045 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3046 {
3047 const char *s = SSDATA (it->overlay_strings[i]);
3048 const char *e = s + SBYTES (it->overlay_strings[i]);
3049
3050 while (s < e && *s != '\n')
3051 ++s;
3052
3053 if (s < e)
3054 {
3055 overlay_strings_with_newlines = 1;
3056 break;
3057 }
3058 }
3059
3060 /* If position is within an overlay string, set up IT to the right
3061 overlay string. */
3062 if (pos->overlay_string_index >= 0)
3063 {
3064 int relative_index;
3065
3066 /* If the first overlay string happens to have a `display'
3067 property for an image, the iterator will be set up for that
3068 image, and we have to undo that setup first before we can
3069 correct the overlay string index. */
3070 if (it->method == GET_FROM_IMAGE)
3071 pop_it (it);
3072
3073 /* We already have the first chunk of overlay strings in
3074 IT->overlay_strings. Load more until the one for
3075 pos->overlay_string_index is in IT->overlay_strings. */
3076 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3077 {
3078 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3079 it->current.overlay_string_index = 0;
3080 while (n--)
3081 {
3082 load_overlay_strings (it, 0);
3083 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3084 }
3085 }
3086
3087 it->current.overlay_string_index = pos->overlay_string_index;
3088 relative_index = (it->current.overlay_string_index
3089 % OVERLAY_STRING_CHUNK_SIZE);
3090 it->string = it->overlay_strings[relative_index];
3091 eassert (STRINGP (it->string));
3092 it->current.string_pos = pos->string_pos;
3093 it->method = GET_FROM_STRING;
3094 it->end_charpos = SCHARS (it->string);
3095 /* Set up the bidi iterator for this overlay string. */
3096 if (it->bidi_p)
3097 {
3098 it->bidi_it.string.lstring = it->string;
3099 it->bidi_it.string.s = NULL;
3100 it->bidi_it.string.schars = SCHARS (it->string);
3101 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3102 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3103 it->bidi_it.string.unibyte = !it->multibyte_p;
3104 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3105 FRAME_WINDOW_P (it->f), &it->bidi_it);
3106
3107 /* Synchronize the state of the bidi iterator with
3108 pos->string_pos. For any string position other than
3109 zero, this will be done automagically when we resume
3110 iteration over the string and get_visually_first_element
3111 is called. But if string_pos is zero, and the string is
3112 to be reordered for display, we need to resync manually,
3113 since it could be that the iteration state recorded in
3114 pos ended at string_pos of 0 moving backwards in string. */
3115 if (CHARPOS (pos->string_pos) == 0)
3116 {
3117 get_visually_first_element (it);
3118 if (IT_STRING_CHARPOS (*it) != 0)
3119 do {
3120 /* Paranoia. */
3121 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3122 bidi_move_to_visually_next (&it->bidi_it);
3123 } while (it->bidi_it.charpos != 0);
3124 }
3125 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3126 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3127 }
3128 }
3129
3130 if (CHARPOS (pos->string_pos) >= 0)
3131 {
3132 /* Recorded position is not in an overlay string, but in another
3133 string. This can only be a string from a `display' property.
3134 IT should already be filled with that string. */
3135 it->current.string_pos = pos->string_pos;
3136 eassert (STRINGP (it->string));
3137 if (it->bidi_p)
3138 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3139 FRAME_WINDOW_P (it->f), &it->bidi_it);
3140 }
3141
3142 /* Restore position in display vector translations, control
3143 character translations or ellipses. */
3144 if (pos->dpvec_index >= 0)
3145 {
3146 if (it->dpvec == NULL)
3147 get_next_display_element (it);
3148 eassert (it->dpvec && it->current.dpvec_index == 0);
3149 it->current.dpvec_index = pos->dpvec_index;
3150 }
3151
3152 CHECK_IT (it);
3153 return !overlay_strings_with_newlines;
3154 }
3155
3156
3157 /* Initialize IT for stepping through current_buffer in window W
3158 starting at ROW->start. */
3159
3160 static void
3161 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3162 {
3163 init_from_display_pos (it, w, &row->start);
3164 it->start = row->start;
3165 it->continuation_lines_width = row->continuation_lines_width;
3166 CHECK_IT (it);
3167 }
3168
3169
3170 /* Initialize IT for stepping through current_buffer in window W
3171 starting in the line following ROW, i.e. starting at ROW->end.
3172 Value is zero if there are overlay strings with newlines at ROW's
3173 end position. */
3174
3175 static int
3176 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3177 {
3178 int success = 0;
3179
3180 if (init_from_display_pos (it, w, &row->end))
3181 {
3182 if (row->continued_p)
3183 it->continuation_lines_width
3184 = row->continuation_lines_width + row->pixel_width;
3185 CHECK_IT (it);
3186 success = 1;
3187 }
3188
3189 return success;
3190 }
3191
3192
3193
3194 \f
3195 /***********************************************************************
3196 Text properties
3197 ***********************************************************************/
3198
3199 /* Called when IT reaches IT->stop_charpos. Handle text property and
3200 overlay changes. Set IT->stop_charpos to the next position where
3201 to stop. */
3202
3203 static void
3204 handle_stop (struct it *it)
3205 {
3206 enum prop_handled handled;
3207 int handle_overlay_change_p;
3208 struct props *p;
3209
3210 it->dpvec = NULL;
3211 it->current.dpvec_index = -1;
3212 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3213 it->ignore_overlay_strings_at_pos_p = 0;
3214 it->ellipsis_p = 0;
3215
3216 /* Use face of preceding text for ellipsis (if invisible) */
3217 if (it->selective_display_ellipsis_p)
3218 it->saved_face_id = it->face_id;
3219
3220 do
3221 {
3222 handled = HANDLED_NORMALLY;
3223
3224 /* Call text property handlers. */
3225 for (p = it_props; p->handler; ++p)
3226 {
3227 handled = p->handler (it);
3228
3229 if (handled == HANDLED_RECOMPUTE_PROPS)
3230 break;
3231 else if (handled == HANDLED_RETURN)
3232 {
3233 /* We still want to show before and after strings from
3234 overlays even if the actual buffer text is replaced. */
3235 if (!handle_overlay_change_p
3236 || it->sp > 1
3237 /* Don't call get_overlay_strings_1 if we already
3238 have overlay strings loaded, because doing so
3239 will load them again and push the iterator state
3240 onto the stack one more time, which is not
3241 expected by the rest of the code that processes
3242 overlay strings. */
3243 || (it->current.overlay_string_index < 0
3244 ? !get_overlay_strings_1 (it, 0, 0)
3245 : 0))
3246 {
3247 if (it->ellipsis_p)
3248 setup_for_ellipsis (it, 0);
3249 /* When handling a display spec, we might load an
3250 empty string. In that case, discard it here. We
3251 used to discard it in handle_single_display_spec,
3252 but that causes get_overlay_strings_1, above, to
3253 ignore overlay strings that we must check. */
3254 if (STRINGP (it->string) && !SCHARS (it->string))
3255 pop_it (it);
3256 return;
3257 }
3258 else if (STRINGP (it->string) && !SCHARS (it->string))
3259 pop_it (it);
3260 else
3261 {
3262 it->ignore_overlay_strings_at_pos_p = 1;
3263 it->string_from_display_prop_p = 0;
3264 it->from_disp_prop_p = 0;
3265 handle_overlay_change_p = 0;
3266 }
3267 handled = HANDLED_RECOMPUTE_PROPS;
3268 break;
3269 }
3270 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3271 handle_overlay_change_p = 0;
3272 }
3273
3274 if (handled != HANDLED_RECOMPUTE_PROPS)
3275 {
3276 /* Don't check for overlay strings below when set to deliver
3277 characters from a display vector. */
3278 if (it->method == GET_FROM_DISPLAY_VECTOR)
3279 handle_overlay_change_p = 0;
3280
3281 /* Handle overlay changes.
3282 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3283 if it finds overlays. */
3284 if (handle_overlay_change_p)
3285 handled = handle_overlay_change (it);
3286 }
3287
3288 if (it->ellipsis_p)
3289 {
3290 setup_for_ellipsis (it, 0);
3291 break;
3292 }
3293 }
3294 while (handled == HANDLED_RECOMPUTE_PROPS);
3295
3296 /* Determine where to stop next. */
3297 if (handled == HANDLED_NORMALLY)
3298 compute_stop_pos (it);
3299 }
3300
3301
3302 /* Compute IT->stop_charpos from text property and overlay change
3303 information for IT's current position. */
3304
3305 static void
3306 compute_stop_pos (struct it *it)
3307 {
3308 register INTERVAL iv, next_iv;
3309 Lisp_Object object, limit, position;
3310 ptrdiff_t charpos, bytepos;
3311
3312 if (STRINGP (it->string))
3313 {
3314 /* Strings are usually short, so don't limit the search for
3315 properties. */
3316 it->stop_charpos = it->end_charpos;
3317 object = it->string;
3318 limit = Qnil;
3319 charpos = IT_STRING_CHARPOS (*it);
3320 bytepos = IT_STRING_BYTEPOS (*it);
3321 }
3322 else
3323 {
3324 ptrdiff_t pos;
3325
3326 /* If end_charpos is out of range for some reason, such as a
3327 misbehaving display function, rationalize it (Bug#5984). */
3328 if (it->end_charpos > ZV)
3329 it->end_charpos = ZV;
3330 it->stop_charpos = it->end_charpos;
3331
3332 /* If next overlay change is in front of the current stop pos
3333 (which is IT->end_charpos), stop there. Note: value of
3334 next_overlay_change is point-max if no overlay change
3335 follows. */
3336 charpos = IT_CHARPOS (*it);
3337 bytepos = IT_BYTEPOS (*it);
3338 pos = next_overlay_change (charpos);
3339 if (pos < it->stop_charpos)
3340 it->stop_charpos = pos;
3341
3342 /* If showing the region, we have to stop at the region
3343 start or end because the face might change there. */
3344 if (it->region_beg_charpos > 0)
3345 {
3346 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3347 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3348 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3349 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3350 }
3351
3352 /* Set up variables for computing the stop position from text
3353 property changes. */
3354 XSETBUFFER (object, current_buffer);
3355 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3356 }
3357
3358 /* Get the interval containing IT's position. Value is a null
3359 interval if there isn't such an interval. */
3360 position = make_number (charpos);
3361 iv = validate_interval_range (object, &position, &position, 0);
3362 if (iv)
3363 {
3364 Lisp_Object values_here[LAST_PROP_IDX];
3365 struct props *p;
3366
3367 /* Get properties here. */
3368 for (p = it_props; p->handler; ++p)
3369 values_here[p->idx] = textget (iv->plist, *p->name);
3370
3371 /* Look for an interval following iv that has different
3372 properties. */
3373 for (next_iv = next_interval (iv);
3374 (next_iv
3375 && (NILP (limit)
3376 || XFASTINT (limit) > next_iv->position));
3377 next_iv = next_interval (next_iv))
3378 {
3379 for (p = it_props; p->handler; ++p)
3380 {
3381 Lisp_Object new_value;
3382
3383 new_value = textget (next_iv->plist, *p->name);
3384 if (!EQ (values_here[p->idx], new_value))
3385 break;
3386 }
3387
3388 if (p->handler)
3389 break;
3390 }
3391
3392 if (next_iv)
3393 {
3394 if (INTEGERP (limit)
3395 && next_iv->position >= XFASTINT (limit))
3396 /* No text property change up to limit. */
3397 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3398 else
3399 /* Text properties change in next_iv. */
3400 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3401 }
3402 }
3403
3404 if (it->cmp_it.id < 0)
3405 {
3406 ptrdiff_t stoppos = it->end_charpos;
3407
3408 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3409 stoppos = -1;
3410 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3411 stoppos, it->string);
3412 }
3413
3414 eassert (STRINGP (it->string)
3415 || (it->stop_charpos >= BEGV
3416 && it->stop_charpos >= IT_CHARPOS (*it)));
3417 }
3418
3419
3420 /* Return the position of the next overlay change after POS in
3421 current_buffer. Value is point-max if no overlay change
3422 follows. This is like `next-overlay-change' but doesn't use
3423 xmalloc. */
3424
3425 static ptrdiff_t
3426 next_overlay_change (ptrdiff_t pos)
3427 {
3428 ptrdiff_t i, noverlays;
3429 ptrdiff_t endpos;
3430 Lisp_Object *overlays;
3431
3432 /* Get all overlays at the given position. */
3433 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3434
3435 /* If any of these overlays ends before endpos,
3436 use its ending point instead. */
3437 for (i = 0; i < noverlays; ++i)
3438 {
3439 Lisp_Object oend;
3440 ptrdiff_t oendpos;
3441
3442 oend = OVERLAY_END (overlays[i]);
3443 oendpos = OVERLAY_POSITION (oend);
3444 endpos = min (endpos, oendpos);
3445 }
3446
3447 return endpos;
3448 }
3449
3450 /* How many characters forward to search for a display property or
3451 display string. Searching too far forward makes the bidi display
3452 sluggish, especially in small windows. */
3453 #define MAX_DISP_SCAN 250
3454
3455 /* Return the character position of a display string at or after
3456 position specified by POSITION. If no display string exists at or
3457 after POSITION, return ZV. A display string is either an overlay
3458 with `display' property whose value is a string, or a `display'
3459 text property whose value is a string. STRING is data about the
3460 string to iterate; if STRING->lstring is nil, we are iterating a
3461 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3462 on a GUI frame. DISP_PROP is set to zero if we searched
3463 MAX_DISP_SCAN characters forward without finding any display
3464 strings, non-zero otherwise. It is set to 2 if the display string
3465 uses any kind of `(space ...)' spec that will produce a stretch of
3466 white space in the text area. */
3467 ptrdiff_t
3468 compute_display_string_pos (struct text_pos *position,
3469 struct bidi_string_data *string,
3470 int frame_window_p, int *disp_prop)
3471 {
3472 /* OBJECT = nil means current buffer. */
3473 Lisp_Object object =
3474 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3475 Lisp_Object pos, spec, limpos;
3476 int string_p = (string && (STRINGP (string->lstring) || string->s));
3477 ptrdiff_t eob = string_p ? string->schars : ZV;
3478 ptrdiff_t begb = string_p ? 0 : BEGV;
3479 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3480 ptrdiff_t lim =
3481 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3482 struct text_pos tpos;
3483 int rv = 0;
3484
3485 *disp_prop = 1;
3486
3487 if (charpos >= eob
3488 /* We don't support display properties whose values are strings
3489 that have display string properties. */
3490 || string->from_disp_str
3491 /* C strings cannot have display properties. */
3492 || (string->s && !STRINGP (object)))
3493 {
3494 *disp_prop = 0;
3495 return eob;
3496 }
3497
3498 /* If the character at CHARPOS is where the display string begins,
3499 return CHARPOS. */
3500 pos = make_number (charpos);
3501 if (STRINGP (object))
3502 bufpos = string->bufpos;
3503 else
3504 bufpos = charpos;
3505 tpos = *position;
3506 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3507 && (charpos <= begb
3508 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3509 object),
3510 spec))
3511 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3512 frame_window_p)))
3513 {
3514 if (rv == 2)
3515 *disp_prop = 2;
3516 return charpos;
3517 }
3518
3519 /* Look forward for the first character with a `display' property
3520 that will replace the underlying text when displayed. */
3521 limpos = make_number (lim);
3522 do {
3523 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3524 CHARPOS (tpos) = XFASTINT (pos);
3525 if (CHARPOS (tpos) >= lim)
3526 {
3527 *disp_prop = 0;
3528 break;
3529 }
3530 if (STRINGP (object))
3531 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3532 else
3533 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3534 spec = Fget_char_property (pos, Qdisplay, object);
3535 if (!STRINGP (object))
3536 bufpos = CHARPOS (tpos);
3537 } while (NILP (spec)
3538 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3539 bufpos, frame_window_p)));
3540 if (rv == 2)
3541 *disp_prop = 2;
3542
3543 return CHARPOS (tpos);
3544 }
3545
3546 /* Return the character position of the end of the display string that
3547 started at CHARPOS. If there's no display string at CHARPOS,
3548 return -1. A display string is either an overlay with `display'
3549 property whose value is a string or a `display' text property whose
3550 value is a string. */
3551 ptrdiff_t
3552 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3553 {
3554 /* OBJECT = nil means current buffer. */
3555 Lisp_Object object =
3556 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3557 Lisp_Object pos = make_number (charpos);
3558 ptrdiff_t eob =
3559 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3560
3561 if (charpos >= eob || (string->s && !STRINGP (object)))
3562 return eob;
3563
3564 /* It could happen that the display property or overlay was removed
3565 since we found it in compute_display_string_pos above. One way
3566 this can happen is if JIT font-lock was called (through
3567 handle_fontified_prop), and jit-lock-functions remove text
3568 properties or overlays from the portion of buffer that includes
3569 CHARPOS. Muse mode is known to do that, for example. In this
3570 case, we return -1 to the caller, to signal that no display
3571 string is actually present at CHARPOS. See bidi_fetch_char for
3572 how this is handled.
3573
3574 An alternative would be to never look for display properties past
3575 it->stop_charpos. But neither compute_display_string_pos nor
3576 bidi_fetch_char that calls it know or care where the next
3577 stop_charpos is. */
3578 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3579 return -1;
3580
3581 /* Look forward for the first character where the `display' property
3582 changes. */
3583 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3584
3585 return XFASTINT (pos);
3586 }
3587
3588
3589 \f
3590 /***********************************************************************
3591 Fontification
3592 ***********************************************************************/
3593
3594 /* Handle changes in the `fontified' property of the current buffer by
3595 calling hook functions from Qfontification_functions to fontify
3596 regions of text. */
3597
3598 static enum prop_handled
3599 handle_fontified_prop (struct it *it)
3600 {
3601 Lisp_Object prop, pos;
3602 enum prop_handled handled = HANDLED_NORMALLY;
3603
3604 if (!NILP (Vmemory_full))
3605 return handled;
3606
3607 /* Get the value of the `fontified' property at IT's current buffer
3608 position. (The `fontified' property doesn't have a special
3609 meaning in strings.) If the value is nil, call functions from
3610 Qfontification_functions. */
3611 if (!STRINGP (it->string)
3612 && it->s == NULL
3613 && !NILP (Vfontification_functions)
3614 && !NILP (Vrun_hooks)
3615 && (pos = make_number (IT_CHARPOS (*it)),
3616 prop = Fget_char_property (pos, Qfontified, Qnil),
3617 /* Ignore the special cased nil value always present at EOB since
3618 no amount of fontifying will be able to change it. */
3619 NILP (prop) && IT_CHARPOS (*it) < Z))
3620 {
3621 ptrdiff_t count = SPECPDL_INDEX ();
3622 Lisp_Object val;
3623 struct buffer *obuf = current_buffer;
3624 int begv = BEGV, zv = ZV;
3625 int old_clip_changed = current_buffer->clip_changed;
3626
3627 val = Vfontification_functions;
3628 specbind (Qfontification_functions, Qnil);
3629
3630 eassert (it->end_charpos == ZV);
3631
3632 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3633 safe_call1 (val, pos);
3634 else
3635 {
3636 Lisp_Object fns, fn;
3637 struct gcpro gcpro1, gcpro2;
3638
3639 fns = Qnil;
3640 GCPRO2 (val, fns);
3641
3642 for (; CONSP (val); val = XCDR (val))
3643 {
3644 fn = XCAR (val);
3645
3646 if (EQ (fn, Qt))
3647 {
3648 /* A value of t indicates this hook has a local
3649 binding; it means to run the global binding too.
3650 In a global value, t should not occur. If it
3651 does, we must ignore it to avoid an endless
3652 loop. */
3653 for (fns = Fdefault_value (Qfontification_functions);
3654 CONSP (fns);
3655 fns = XCDR (fns))
3656 {
3657 fn = XCAR (fns);
3658 if (!EQ (fn, Qt))
3659 safe_call1 (fn, pos);
3660 }
3661 }
3662 else
3663 safe_call1 (fn, pos);
3664 }
3665
3666 UNGCPRO;
3667 }
3668
3669 unbind_to (count, Qnil);
3670
3671 /* Fontification functions routinely call `save-restriction'.
3672 Normally, this tags clip_changed, which can confuse redisplay
3673 (see discussion in Bug#6671). Since we don't perform any
3674 special handling of fontification changes in the case where
3675 `save-restriction' isn't called, there's no point doing so in
3676 this case either. So, if the buffer's restrictions are
3677 actually left unchanged, reset clip_changed. */
3678 if (obuf == current_buffer)
3679 {
3680 if (begv == BEGV && zv == ZV)
3681 current_buffer->clip_changed = old_clip_changed;
3682 }
3683 /* There isn't much we can reasonably do to protect against
3684 misbehaving fontification, but here's a fig leaf. */
3685 else if (BUFFER_LIVE_P (obuf))
3686 set_buffer_internal_1 (obuf);
3687
3688 /* The fontification code may have added/removed text.
3689 It could do even a lot worse, but let's at least protect against
3690 the most obvious case where only the text past `pos' gets changed',
3691 as is/was done in grep.el where some escapes sequences are turned
3692 into face properties (bug#7876). */
3693 it->end_charpos = ZV;
3694
3695 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3696 something. This avoids an endless loop if they failed to
3697 fontify the text for which reason ever. */
3698 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3699 handled = HANDLED_RECOMPUTE_PROPS;
3700 }
3701
3702 return handled;
3703 }
3704
3705
3706 \f
3707 /***********************************************************************
3708 Faces
3709 ***********************************************************************/
3710
3711 /* Set up iterator IT from face properties at its current position.
3712 Called from handle_stop. */
3713
3714 static enum prop_handled
3715 handle_face_prop (struct it *it)
3716 {
3717 int new_face_id;
3718 ptrdiff_t next_stop;
3719
3720 if (!STRINGP (it->string))
3721 {
3722 new_face_id
3723 = face_at_buffer_position (it->w,
3724 IT_CHARPOS (*it),
3725 it->region_beg_charpos,
3726 it->region_end_charpos,
3727 &next_stop,
3728 (IT_CHARPOS (*it)
3729 + TEXT_PROP_DISTANCE_LIMIT),
3730 0, it->base_face_id);
3731
3732 /* Is this a start of a run of characters with box face?
3733 Caveat: this can be called for a freshly initialized
3734 iterator; face_id is -1 in this case. We know that the new
3735 face will not change until limit, i.e. if the new face has a
3736 box, all characters up to limit will have one. But, as
3737 usual, we don't know whether limit is really the end. */
3738 if (new_face_id != it->face_id)
3739 {
3740 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3741 /* If it->face_id is -1, old_face below will be NULL, see
3742 the definition of FACE_FROM_ID. This will happen if this
3743 is the initial call that gets the face. */
3744 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3745
3746 /* If the value of face_id of the iterator is -1, we have to
3747 look in front of IT's position and see whether there is a
3748 face there that's different from new_face_id. */
3749 if (!old_face && IT_CHARPOS (*it) > BEG)
3750 {
3751 int prev_face_id = face_before_it_pos (it);
3752
3753 old_face = FACE_FROM_ID (it->f, prev_face_id);
3754 }
3755
3756 /* If the new face has a box, but the old face does not,
3757 this is the start of a run of characters with box face,
3758 i.e. this character has a shadow on the left side. */
3759 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3760 && (old_face == NULL || !old_face->box));
3761 it->face_box_p = new_face->box != FACE_NO_BOX;
3762 }
3763 }
3764 else
3765 {
3766 int base_face_id;
3767 ptrdiff_t bufpos;
3768 int i;
3769 Lisp_Object from_overlay
3770 = (it->current.overlay_string_index >= 0
3771 ? it->string_overlays[it->current.overlay_string_index
3772 % OVERLAY_STRING_CHUNK_SIZE]
3773 : Qnil);
3774
3775 /* See if we got to this string directly or indirectly from
3776 an overlay property. That includes the before-string or
3777 after-string of an overlay, strings in display properties
3778 provided by an overlay, their text properties, etc.
3779
3780 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3781 if (! NILP (from_overlay))
3782 for (i = it->sp - 1; i >= 0; i--)
3783 {
3784 if (it->stack[i].current.overlay_string_index >= 0)
3785 from_overlay
3786 = it->string_overlays[it->stack[i].current.overlay_string_index
3787 % OVERLAY_STRING_CHUNK_SIZE];
3788 else if (! NILP (it->stack[i].from_overlay))
3789 from_overlay = it->stack[i].from_overlay;
3790
3791 if (!NILP (from_overlay))
3792 break;
3793 }
3794
3795 if (! NILP (from_overlay))
3796 {
3797 bufpos = IT_CHARPOS (*it);
3798 /* For a string from an overlay, the base face depends
3799 only on text properties and ignores overlays. */
3800 base_face_id
3801 = face_for_overlay_string (it->w,
3802 IT_CHARPOS (*it),
3803 it->region_beg_charpos,
3804 it->region_end_charpos,
3805 &next_stop,
3806 (IT_CHARPOS (*it)
3807 + TEXT_PROP_DISTANCE_LIMIT),
3808 0,
3809 from_overlay);
3810 }
3811 else
3812 {
3813 bufpos = 0;
3814
3815 /* For strings from a `display' property, use the face at
3816 IT's current buffer position as the base face to merge
3817 with, so that overlay strings appear in the same face as
3818 surrounding text, unless they specify their own
3819 faces. */
3820 base_face_id = it->string_from_prefix_prop_p
3821 ? DEFAULT_FACE_ID
3822 : underlying_face_id (it);
3823 }
3824
3825 new_face_id = face_at_string_position (it->w,
3826 it->string,
3827 IT_STRING_CHARPOS (*it),
3828 bufpos,
3829 it->region_beg_charpos,
3830 it->region_end_charpos,
3831 &next_stop,
3832 base_face_id, 0);
3833
3834 /* Is this a start of a run of characters with box? Caveat:
3835 this can be called for a freshly allocated iterator; face_id
3836 is -1 is this case. We know that the new face will not
3837 change until the next check pos, i.e. if the new face has a
3838 box, all characters up to that position will have a
3839 box. But, as usual, we don't know whether that position
3840 is really the end. */
3841 if (new_face_id != it->face_id)
3842 {
3843 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3844 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3845
3846 /* If new face has a box but old face hasn't, this is the
3847 start of a run of characters with box, i.e. it has a
3848 shadow on the left side. */
3849 it->start_of_box_run_p
3850 = new_face->box && (old_face == NULL || !old_face->box);
3851 it->face_box_p = new_face->box != FACE_NO_BOX;
3852 }
3853 }
3854
3855 it->face_id = new_face_id;
3856 return HANDLED_NORMALLY;
3857 }
3858
3859
3860 /* Return the ID of the face ``underlying'' IT's current position,
3861 which is in a string. If the iterator is associated with a
3862 buffer, return the face at IT's current buffer position.
3863 Otherwise, use the iterator's base_face_id. */
3864
3865 static int
3866 underlying_face_id (struct it *it)
3867 {
3868 int face_id = it->base_face_id, i;
3869
3870 eassert (STRINGP (it->string));
3871
3872 for (i = it->sp - 1; i >= 0; --i)
3873 if (NILP (it->stack[i].string))
3874 face_id = it->stack[i].face_id;
3875
3876 return face_id;
3877 }
3878
3879
3880 /* Compute the face one character before or after the current position
3881 of IT, in the visual order. BEFORE_P non-zero means get the face
3882 in front (to the left in L2R paragraphs, to the right in R2L
3883 paragraphs) of IT's screen position. Value is the ID of the face. */
3884
3885 static int
3886 face_before_or_after_it_pos (struct it *it, int before_p)
3887 {
3888 int face_id, limit;
3889 ptrdiff_t next_check_charpos;
3890 struct it it_copy;
3891 void *it_copy_data = NULL;
3892
3893 eassert (it->s == NULL);
3894
3895 if (STRINGP (it->string))
3896 {
3897 ptrdiff_t bufpos, charpos;
3898 int base_face_id;
3899
3900 /* No face change past the end of the string (for the case
3901 we are padding with spaces). No face change before the
3902 string start. */
3903 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3904 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3905 return it->face_id;
3906
3907 if (!it->bidi_p)
3908 {
3909 /* Set charpos to the position before or after IT's current
3910 position, in the logical order, which in the non-bidi
3911 case is the same as the visual order. */
3912 if (before_p)
3913 charpos = IT_STRING_CHARPOS (*it) - 1;
3914 else if (it->what == IT_COMPOSITION)
3915 /* For composition, we must check the character after the
3916 composition. */
3917 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3918 else
3919 charpos = IT_STRING_CHARPOS (*it) + 1;
3920 }
3921 else
3922 {
3923 if (before_p)
3924 {
3925 /* With bidi iteration, the character before the current
3926 in the visual order cannot be found by simple
3927 iteration, because "reverse" reordering is not
3928 supported. Instead, we need to use the move_it_*
3929 family of functions. */
3930 /* Ignore face changes before the first visible
3931 character on this display line. */
3932 if (it->current_x <= it->first_visible_x)
3933 return it->face_id;
3934 SAVE_IT (it_copy, *it, it_copy_data);
3935 /* Implementation note: Since move_it_in_display_line
3936 works in the iterator geometry, and thinks the first
3937 character is always the leftmost, even in R2L lines,
3938 we don't need to distinguish between the R2L and L2R
3939 cases here. */
3940 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3941 it_copy.current_x - 1, MOVE_TO_X);
3942 charpos = IT_STRING_CHARPOS (it_copy);
3943 RESTORE_IT (it, it, it_copy_data);
3944 }
3945 else
3946 {
3947 /* Set charpos to the string position of the character
3948 that comes after IT's current position in the visual
3949 order. */
3950 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3951
3952 it_copy = *it;
3953 while (n--)
3954 bidi_move_to_visually_next (&it_copy.bidi_it);
3955
3956 charpos = it_copy.bidi_it.charpos;
3957 }
3958 }
3959 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3960
3961 if (it->current.overlay_string_index >= 0)
3962 bufpos = IT_CHARPOS (*it);
3963 else
3964 bufpos = 0;
3965
3966 base_face_id = underlying_face_id (it);
3967
3968 /* Get the face for ASCII, or unibyte. */
3969 face_id = face_at_string_position (it->w,
3970 it->string,
3971 charpos,
3972 bufpos,
3973 it->region_beg_charpos,
3974 it->region_end_charpos,
3975 &next_check_charpos,
3976 base_face_id, 0);
3977
3978 /* Correct the face for charsets different from ASCII. Do it
3979 for the multibyte case only. The face returned above is
3980 suitable for unibyte text if IT->string is unibyte. */
3981 if (STRING_MULTIBYTE (it->string))
3982 {
3983 struct text_pos pos1 = string_pos (charpos, it->string);
3984 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3985 int c, len;
3986 struct face *face = FACE_FROM_ID (it->f, face_id);
3987
3988 c = string_char_and_length (p, &len);
3989 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3990 }
3991 }
3992 else
3993 {
3994 struct text_pos pos;
3995
3996 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3997 || (IT_CHARPOS (*it) <= BEGV && before_p))
3998 return it->face_id;
3999
4000 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4001 pos = it->current.pos;
4002
4003 if (!it->bidi_p)
4004 {
4005 if (before_p)
4006 DEC_TEXT_POS (pos, it->multibyte_p);
4007 else
4008 {
4009 if (it->what == IT_COMPOSITION)
4010 {
4011 /* For composition, we must check the position after
4012 the composition. */
4013 pos.charpos += it->cmp_it.nchars;
4014 pos.bytepos += it->len;
4015 }
4016 else
4017 INC_TEXT_POS (pos, it->multibyte_p);
4018 }
4019 }
4020 else
4021 {
4022 if (before_p)
4023 {
4024 /* With bidi iteration, the character before the current
4025 in the visual order cannot be found by simple
4026 iteration, because "reverse" reordering is not
4027 supported. Instead, we need to use the move_it_*
4028 family of functions. */
4029 /* Ignore face changes before the first visible
4030 character on this display line. */
4031 if (it->current_x <= it->first_visible_x)
4032 return it->face_id;
4033 SAVE_IT (it_copy, *it, it_copy_data);
4034 /* Implementation note: Since move_it_in_display_line
4035 works in the iterator geometry, and thinks the first
4036 character is always the leftmost, even in R2L lines,
4037 we don't need to distinguish between the R2L and L2R
4038 cases here. */
4039 move_it_in_display_line (&it_copy, ZV,
4040 it_copy.current_x - 1, MOVE_TO_X);
4041 pos = it_copy.current.pos;
4042 RESTORE_IT (it, it, it_copy_data);
4043 }
4044 else
4045 {
4046 /* Set charpos to the buffer position of the character
4047 that comes after IT's current position in the visual
4048 order. */
4049 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4050
4051 it_copy = *it;
4052 while (n--)
4053 bidi_move_to_visually_next (&it_copy.bidi_it);
4054
4055 SET_TEXT_POS (pos,
4056 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4057 }
4058 }
4059 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4060
4061 /* Determine face for CHARSET_ASCII, or unibyte. */
4062 face_id = face_at_buffer_position (it->w,
4063 CHARPOS (pos),
4064 it->region_beg_charpos,
4065 it->region_end_charpos,
4066 &next_check_charpos,
4067 limit, 0, -1);
4068
4069 /* Correct the face for charsets different from ASCII. Do it
4070 for the multibyte case only. The face returned above is
4071 suitable for unibyte text if current_buffer is unibyte. */
4072 if (it->multibyte_p)
4073 {
4074 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4075 struct face *face = FACE_FROM_ID (it->f, face_id);
4076 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4077 }
4078 }
4079
4080 return face_id;
4081 }
4082
4083
4084 \f
4085 /***********************************************************************
4086 Invisible text
4087 ***********************************************************************/
4088
4089 /* Set up iterator IT from invisible properties at its current
4090 position. Called from handle_stop. */
4091
4092 static enum prop_handled
4093 handle_invisible_prop (struct it *it)
4094 {
4095 enum prop_handled handled = HANDLED_NORMALLY;
4096 int invis_p;
4097 Lisp_Object prop;
4098
4099 if (STRINGP (it->string))
4100 {
4101 Lisp_Object end_charpos, limit, charpos;
4102
4103 /* Get the value of the invisible text property at the
4104 current position. Value will be nil if there is no such
4105 property. */
4106 charpos = make_number (IT_STRING_CHARPOS (*it));
4107 prop = Fget_text_property (charpos, Qinvisible, it->string);
4108 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4109
4110 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4111 {
4112 /* Record whether we have to display an ellipsis for the
4113 invisible text. */
4114 int display_ellipsis_p = (invis_p == 2);
4115 ptrdiff_t len, endpos;
4116
4117 handled = HANDLED_RECOMPUTE_PROPS;
4118
4119 /* Get the position at which the next visible text can be
4120 found in IT->string, if any. */
4121 endpos = len = SCHARS (it->string);
4122 XSETINT (limit, len);
4123 do
4124 {
4125 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4126 it->string, limit);
4127 if (INTEGERP (end_charpos))
4128 {
4129 endpos = XFASTINT (end_charpos);
4130 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4131 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4132 if (invis_p == 2)
4133 display_ellipsis_p = 1;
4134 }
4135 }
4136 while (invis_p && endpos < len);
4137
4138 if (display_ellipsis_p)
4139 it->ellipsis_p = 1;
4140
4141 if (endpos < len)
4142 {
4143 /* Text at END_CHARPOS is visible. Move IT there. */
4144 struct text_pos old;
4145 ptrdiff_t oldpos;
4146
4147 old = it->current.string_pos;
4148 oldpos = CHARPOS (old);
4149 if (it->bidi_p)
4150 {
4151 if (it->bidi_it.first_elt
4152 && it->bidi_it.charpos < SCHARS (it->string))
4153 bidi_paragraph_init (it->paragraph_embedding,
4154 &it->bidi_it, 1);
4155 /* Bidi-iterate out of the invisible text. */
4156 do
4157 {
4158 bidi_move_to_visually_next (&it->bidi_it);
4159 }
4160 while (oldpos <= it->bidi_it.charpos
4161 && it->bidi_it.charpos < endpos);
4162
4163 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4164 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4165 if (IT_CHARPOS (*it) >= endpos)
4166 it->prev_stop = endpos;
4167 }
4168 else
4169 {
4170 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4171 compute_string_pos (&it->current.string_pos, old, it->string);
4172 }
4173 }
4174 else
4175 {
4176 /* The rest of the string is invisible. If this is an
4177 overlay string, proceed with the next overlay string
4178 or whatever comes and return a character from there. */
4179 if (it->current.overlay_string_index >= 0
4180 && !display_ellipsis_p)
4181 {
4182 next_overlay_string (it);
4183 /* Don't check for overlay strings when we just
4184 finished processing them. */
4185 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4186 }
4187 else
4188 {
4189 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4190 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4191 }
4192 }
4193 }
4194 }
4195 else
4196 {
4197 ptrdiff_t newpos, next_stop, start_charpos, tem;
4198 Lisp_Object pos, overlay;
4199
4200 /* First of all, is there invisible text at this position? */
4201 tem = start_charpos = IT_CHARPOS (*it);
4202 pos = make_number (tem);
4203 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4204 &overlay);
4205 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4206
4207 /* If we are on invisible text, skip over it. */
4208 if (invis_p && start_charpos < it->end_charpos)
4209 {
4210 /* Record whether we have to display an ellipsis for the
4211 invisible text. */
4212 int display_ellipsis_p = invis_p == 2;
4213
4214 handled = HANDLED_RECOMPUTE_PROPS;
4215
4216 /* Loop skipping over invisible text. The loop is left at
4217 ZV or with IT on the first char being visible again. */
4218 do
4219 {
4220 /* Try to skip some invisible text. Return value is the
4221 position reached which can be equal to where we start
4222 if there is nothing invisible there. This skips both
4223 over invisible text properties and overlays with
4224 invisible property. */
4225 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4226
4227 /* If we skipped nothing at all we weren't at invisible
4228 text in the first place. If everything to the end of
4229 the buffer was skipped, end the loop. */
4230 if (newpos == tem || newpos >= ZV)
4231 invis_p = 0;
4232 else
4233 {
4234 /* We skipped some characters but not necessarily
4235 all there are. Check if we ended up on visible
4236 text. Fget_char_property returns the property of
4237 the char before the given position, i.e. if we
4238 get invis_p = 0, this means that the char at
4239 newpos is visible. */
4240 pos = make_number (newpos);
4241 prop = Fget_char_property (pos, Qinvisible, it->window);
4242 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4243 }
4244
4245 /* If we ended up on invisible text, proceed to
4246 skip starting with next_stop. */
4247 if (invis_p)
4248 tem = next_stop;
4249
4250 /* If there are adjacent invisible texts, don't lose the
4251 second one's ellipsis. */
4252 if (invis_p == 2)
4253 display_ellipsis_p = 1;
4254 }
4255 while (invis_p);
4256
4257 /* The position newpos is now either ZV or on visible text. */
4258 if (it->bidi_p)
4259 {
4260 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4261 int on_newline =
4262 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4263 int after_newline =
4264 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4265
4266 /* If the invisible text ends on a newline or on a
4267 character after a newline, we can avoid the costly,
4268 character by character, bidi iteration to NEWPOS, and
4269 instead simply reseat the iterator there. That's
4270 because all bidi reordering information is tossed at
4271 the newline. This is a big win for modes that hide
4272 complete lines, like Outline, Org, etc. */
4273 if (on_newline || after_newline)
4274 {
4275 struct text_pos tpos;
4276 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4277
4278 SET_TEXT_POS (tpos, newpos, bpos);
4279 reseat_1 (it, tpos, 0);
4280 /* If we reseat on a newline/ZV, we need to prep the
4281 bidi iterator for advancing to the next character
4282 after the newline/EOB, keeping the current paragraph
4283 direction (so that PRODUCE_GLYPHS does TRT wrt
4284 prepending/appending glyphs to a glyph row). */
4285 if (on_newline)
4286 {
4287 it->bidi_it.first_elt = 0;
4288 it->bidi_it.paragraph_dir = pdir;
4289 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4290 it->bidi_it.nchars = 1;
4291 it->bidi_it.ch_len = 1;
4292 }
4293 }
4294 else /* Must use the slow method. */
4295 {
4296 /* With bidi iteration, the region of invisible text
4297 could start and/or end in the middle of a
4298 non-base embedding level. Therefore, we need to
4299 skip invisible text using the bidi iterator,
4300 starting at IT's current position, until we find
4301 ourselves outside of the invisible text.
4302 Skipping invisible text _after_ bidi iteration
4303 avoids affecting the visual order of the
4304 displayed text when invisible properties are
4305 added or removed. */
4306 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4307 {
4308 /* If we were `reseat'ed to a new paragraph,
4309 determine the paragraph base direction. We
4310 need to do it now because
4311 next_element_from_buffer may not have a
4312 chance to do it, if we are going to skip any
4313 text at the beginning, which resets the
4314 FIRST_ELT flag. */
4315 bidi_paragraph_init (it->paragraph_embedding,
4316 &it->bidi_it, 1);
4317 }
4318 do
4319 {
4320 bidi_move_to_visually_next (&it->bidi_it);
4321 }
4322 while (it->stop_charpos <= it->bidi_it.charpos
4323 && it->bidi_it.charpos < newpos);
4324 IT_CHARPOS (*it) = it->bidi_it.charpos;
4325 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4326 /* If we overstepped NEWPOS, record its position in
4327 the iterator, so that we skip invisible text if
4328 later the bidi iteration lands us in the
4329 invisible region again. */
4330 if (IT_CHARPOS (*it) >= newpos)
4331 it->prev_stop = newpos;
4332 }
4333 }
4334 else
4335 {
4336 IT_CHARPOS (*it) = newpos;
4337 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4338 }
4339
4340 /* If there are before-strings at the start of invisible
4341 text, and the text is invisible because of a text
4342 property, arrange to show before-strings because 20.x did
4343 it that way. (If the text is invisible because of an
4344 overlay property instead of a text property, this is
4345 already handled in the overlay code.) */
4346 if (NILP (overlay)
4347 && get_overlay_strings (it, it->stop_charpos))
4348 {
4349 handled = HANDLED_RECOMPUTE_PROPS;
4350 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4351 }
4352 else if (display_ellipsis_p)
4353 {
4354 /* Make sure that the glyphs of the ellipsis will get
4355 correct `charpos' values. If we would not update
4356 it->position here, the glyphs would belong to the
4357 last visible character _before_ the invisible
4358 text, which confuses `set_cursor_from_row'.
4359
4360 We use the last invisible position instead of the
4361 first because this way the cursor is always drawn on
4362 the first "." of the ellipsis, whenever PT is inside
4363 the invisible text. Otherwise the cursor would be
4364 placed _after_ the ellipsis when the point is after the
4365 first invisible character. */
4366 if (!STRINGP (it->object))
4367 {
4368 it->position.charpos = newpos - 1;
4369 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4370 }
4371 it->ellipsis_p = 1;
4372 /* Let the ellipsis display before
4373 considering any properties of the following char.
4374 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4375 handled = HANDLED_RETURN;
4376 }
4377 }
4378 }
4379
4380 return handled;
4381 }
4382
4383
4384 /* Make iterator IT return `...' next.
4385 Replaces LEN characters from buffer. */
4386
4387 static void
4388 setup_for_ellipsis (struct it *it, int len)
4389 {
4390 /* Use the display table definition for `...'. Invalid glyphs
4391 will be handled by the method returning elements from dpvec. */
4392 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4393 {
4394 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4395 it->dpvec = v->contents;
4396 it->dpend = v->contents + v->header.size;
4397 }
4398 else
4399 {
4400 /* Default `...'. */
4401 it->dpvec = default_invis_vector;
4402 it->dpend = default_invis_vector + 3;
4403 }
4404
4405 it->dpvec_char_len = len;
4406 it->current.dpvec_index = 0;
4407 it->dpvec_face_id = -1;
4408
4409 /* Remember the current face id in case glyphs specify faces.
4410 IT's face is restored in set_iterator_to_next.
4411 saved_face_id was set to preceding char's face in handle_stop. */
4412 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4413 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4414
4415 it->method = GET_FROM_DISPLAY_VECTOR;
4416 it->ellipsis_p = 1;
4417 }
4418
4419
4420 \f
4421 /***********************************************************************
4422 'display' property
4423 ***********************************************************************/
4424
4425 /* Set up iterator IT from `display' property at its current position.
4426 Called from handle_stop.
4427 We return HANDLED_RETURN if some part of the display property
4428 overrides the display of the buffer text itself.
4429 Otherwise we return HANDLED_NORMALLY. */
4430
4431 static enum prop_handled
4432 handle_display_prop (struct it *it)
4433 {
4434 Lisp_Object propval, object, overlay;
4435 struct text_pos *position;
4436 ptrdiff_t bufpos;
4437 /* Nonzero if some property replaces the display of the text itself. */
4438 int display_replaced_p = 0;
4439
4440 if (STRINGP (it->string))
4441 {
4442 object = it->string;
4443 position = &it->current.string_pos;
4444 bufpos = CHARPOS (it->current.pos);
4445 }
4446 else
4447 {
4448 XSETWINDOW (object, it->w);
4449 position = &it->current.pos;
4450 bufpos = CHARPOS (*position);
4451 }
4452
4453 /* Reset those iterator values set from display property values. */
4454 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4455 it->space_width = Qnil;
4456 it->font_height = Qnil;
4457 it->voffset = 0;
4458
4459 /* We don't support recursive `display' properties, i.e. string
4460 values that have a string `display' property, that have a string
4461 `display' property etc. */
4462 if (!it->string_from_display_prop_p)
4463 it->area = TEXT_AREA;
4464
4465 propval = get_char_property_and_overlay (make_number (position->charpos),
4466 Qdisplay, object, &overlay);
4467 if (NILP (propval))
4468 return HANDLED_NORMALLY;
4469 /* Now OVERLAY is the overlay that gave us this property, or nil
4470 if it was a text property. */
4471
4472 if (!STRINGP (it->string))
4473 object = it->w->contents;
4474
4475 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4476 position, bufpos,
4477 FRAME_WINDOW_P (it->f));
4478
4479 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4480 }
4481
4482 /* Subroutine of handle_display_prop. Returns non-zero if the display
4483 specification in SPEC is a replacing specification, i.e. it would
4484 replace the text covered by `display' property with something else,
4485 such as an image or a display string. If SPEC includes any kind or
4486 `(space ...) specification, the value is 2; this is used by
4487 compute_display_string_pos, which see.
4488
4489 See handle_single_display_spec for documentation of arguments.
4490 frame_window_p is non-zero if the window being redisplayed is on a
4491 GUI frame; this argument is used only if IT is NULL, see below.
4492
4493 IT can be NULL, if this is called by the bidi reordering code
4494 through compute_display_string_pos, which see. In that case, this
4495 function only examines SPEC, but does not otherwise "handle" it, in
4496 the sense that it doesn't set up members of IT from the display
4497 spec. */
4498 static int
4499 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4500 Lisp_Object overlay, struct text_pos *position,
4501 ptrdiff_t bufpos, int frame_window_p)
4502 {
4503 int replacing_p = 0;
4504 int rv;
4505
4506 if (CONSP (spec)
4507 /* Simple specifications. */
4508 && !EQ (XCAR (spec), Qimage)
4509 && !EQ (XCAR (spec), Qspace)
4510 && !EQ (XCAR (spec), Qwhen)
4511 && !EQ (XCAR (spec), Qslice)
4512 && !EQ (XCAR (spec), Qspace_width)
4513 && !EQ (XCAR (spec), Qheight)
4514 && !EQ (XCAR (spec), Qraise)
4515 /* Marginal area specifications. */
4516 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4517 && !EQ (XCAR (spec), Qleft_fringe)
4518 && !EQ (XCAR (spec), Qright_fringe)
4519 && !NILP (XCAR (spec)))
4520 {
4521 for (; CONSP (spec); spec = XCDR (spec))
4522 {
4523 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4524 overlay, position, bufpos,
4525 replacing_p, frame_window_p)))
4526 {
4527 replacing_p = rv;
4528 /* If some text in a string is replaced, `position' no
4529 longer points to the position of `object'. */
4530 if (!it || STRINGP (object))
4531 break;
4532 }
4533 }
4534 }
4535 else if (VECTORP (spec))
4536 {
4537 ptrdiff_t i;
4538 for (i = 0; i < ASIZE (spec); ++i)
4539 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4540 overlay, position, bufpos,
4541 replacing_p, frame_window_p)))
4542 {
4543 replacing_p = rv;
4544 /* If some text in a string is replaced, `position' no
4545 longer points to the position of `object'. */
4546 if (!it || STRINGP (object))
4547 break;
4548 }
4549 }
4550 else
4551 {
4552 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4553 position, bufpos, 0,
4554 frame_window_p)))
4555 replacing_p = rv;
4556 }
4557
4558 return replacing_p;
4559 }
4560
4561 /* Value is the position of the end of the `display' property starting
4562 at START_POS in OBJECT. */
4563
4564 static struct text_pos
4565 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4566 {
4567 Lisp_Object end;
4568 struct text_pos end_pos;
4569
4570 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4571 Qdisplay, object, Qnil);
4572 CHARPOS (end_pos) = XFASTINT (end);
4573 if (STRINGP (object))
4574 compute_string_pos (&end_pos, start_pos, it->string);
4575 else
4576 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4577
4578 return end_pos;
4579 }
4580
4581
4582 /* Set up IT from a single `display' property specification SPEC. OBJECT
4583 is the object in which the `display' property was found. *POSITION
4584 is the position in OBJECT at which the `display' property was found.
4585 BUFPOS is the buffer position of OBJECT (different from POSITION if
4586 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4587 previously saw a display specification which already replaced text
4588 display with something else, for example an image; we ignore such
4589 properties after the first one has been processed.
4590
4591 OVERLAY is the overlay this `display' property came from,
4592 or nil if it was a text property.
4593
4594 If SPEC is a `space' or `image' specification, and in some other
4595 cases too, set *POSITION to the position where the `display'
4596 property ends.
4597
4598 If IT is NULL, only examine the property specification in SPEC, but
4599 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4600 is intended to be displayed in a window on a GUI frame.
4601
4602 Value is non-zero if something was found which replaces the display
4603 of buffer or string text. */
4604
4605 static int
4606 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4607 Lisp_Object overlay, struct text_pos *position,
4608 ptrdiff_t bufpos, int display_replaced_p,
4609 int frame_window_p)
4610 {
4611 Lisp_Object form;
4612 Lisp_Object location, value;
4613 struct text_pos start_pos = *position;
4614 int valid_p;
4615
4616 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4617 If the result is non-nil, use VALUE instead of SPEC. */
4618 form = Qt;
4619 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4620 {
4621 spec = XCDR (spec);
4622 if (!CONSP (spec))
4623 return 0;
4624 form = XCAR (spec);
4625 spec = XCDR (spec);
4626 }
4627
4628 if (!NILP (form) && !EQ (form, Qt))
4629 {
4630 ptrdiff_t count = SPECPDL_INDEX ();
4631 struct gcpro gcpro1;
4632
4633 /* Bind `object' to the object having the `display' property, a
4634 buffer or string. Bind `position' to the position in the
4635 object where the property was found, and `buffer-position'
4636 to the current position in the buffer. */
4637
4638 if (NILP (object))
4639 XSETBUFFER (object, current_buffer);
4640 specbind (Qobject, object);
4641 specbind (Qposition, make_number (CHARPOS (*position)));
4642 specbind (Qbuffer_position, make_number (bufpos));
4643 GCPRO1 (form);
4644 form = safe_eval (form);
4645 UNGCPRO;
4646 unbind_to (count, Qnil);
4647 }
4648
4649 if (NILP (form))
4650 return 0;
4651
4652 /* Handle `(height HEIGHT)' specifications. */
4653 if (CONSP (spec)
4654 && EQ (XCAR (spec), Qheight)
4655 && CONSP (XCDR (spec)))
4656 {
4657 if (it)
4658 {
4659 if (!FRAME_WINDOW_P (it->f))
4660 return 0;
4661
4662 it->font_height = XCAR (XCDR (spec));
4663 if (!NILP (it->font_height))
4664 {
4665 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4666 int new_height = -1;
4667
4668 if (CONSP (it->font_height)
4669 && (EQ (XCAR (it->font_height), Qplus)
4670 || EQ (XCAR (it->font_height), Qminus))
4671 && CONSP (XCDR (it->font_height))
4672 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4673 {
4674 /* `(+ N)' or `(- N)' where N is an integer. */
4675 int steps = XINT (XCAR (XCDR (it->font_height)));
4676 if (EQ (XCAR (it->font_height), Qplus))
4677 steps = - steps;
4678 it->face_id = smaller_face (it->f, it->face_id, steps);
4679 }
4680 else if (FUNCTIONP (it->font_height))
4681 {
4682 /* Call function with current height as argument.
4683 Value is the new height. */
4684 Lisp_Object height;
4685 height = safe_call1 (it->font_height,
4686 face->lface[LFACE_HEIGHT_INDEX]);
4687 if (NUMBERP (height))
4688 new_height = XFLOATINT (height);
4689 }
4690 else if (NUMBERP (it->font_height))
4691 {
4692 /* Value is a multiple of the canonical char height. */
4693 struct face *f;
4694
4695 f = FACE_FROM_ID (it->f,
4696 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4697 new_height = (XFLOATINT (it->font_height)
4698 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4699 }
4700 else
4701 {
4702 /* Evaluate IT->font_height with `height' bound to the
4703 current specified height to get the new height. */
4704 ptrdiff_t count = SPECPDL_INDEX ();
4705
4706 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4707 value = safe_eval (it->font_height);
4708 unbind_to (count, Qnil);
4709
4710 if (NUMBERP (value))
4711 new_height = XFLOATINT (value);
4712 }
4713
4714 if (new_height > 0)
4715 it->face_id = face_with_height (it->f, it->face_id, new_height);
4716 }
4717 }
4718
4719 return 0;
4720 }
4721
4722 /* Handle `(space-width WIDTH)'. */
4723 if (CONSP (spec)
4724 && EQ (XCAR (spec), Qspace_width)
4725 && CONSP (XCDR (spec)))
4726 {
4727 if (it)
4728 {
4729 if (!FRAME_WINDOW_P (it->f))
4730 return 0;
4731
4732 value = XCAR (XCDR (spec));
4733 if (NUMBERP (value) && XFLOATINT (value) > 0)
4734 it->space_width = value;
4735 }
4736
4737 return 0;
4738 }
4739
4740 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4741 if (CONSP (spec)
4742 && EQ (XCAR (spec), Qslice))
4743 {
4744 Lisp_Object tem;
4745
4746 if (it)
4747 {
4748 if (!FRAME_WINDOW_P (it->f))
4749 return 0;
4750
4751 if (tem = XCDR (spec), CONSP (tem))
4752 {
4753 it->slice.x = XCAR (tem);
4754 if (tem = XCDR (tem), CONSP (tem))
4755 {
4756 it->slice.y = XCAR (tem);
4757 if (tem = XCDR (tem), CONSP (tem))
4758 {
4759 it->slice.width = XCAR (tem);
4760 if (tem = XCDR (tem), CONSP (tem))
4761 it->slice.height = XCAR (tem);
4762 }
4763 }
4764 }
4765 }
4766
4767 return 0;
4768 }
4769
4770 /* Handle `(raise FACTOR)'. */
4771 if (CONSP (spec)
4772 && EQ (XCAR (spec), Qraise)
4773 && CONSP (XCDR (spec)))
4774 {
4775 if (it)
4776 {
4777 if (!FRAME_WINDOW_P (it->f))
4778 return 0;
4779
4780 #ifdef HAVE_WINDOW_SYSTEM
4781 value = XCAR (XCDR (spec));
4782 if (NUMBERP (value))
4783 {
4784 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4785 it->voffset = - (XFLOATINT (value)
4786 * (FONT_HEIGHT (face->font)));
4787 }
4788 #endif /* HAVE_WINDOW_SYSTEM */
4789 }
4790
4791 return 0;
4792 }
4793
4794 /* Don't handle the other kinds of display specifications
4795 inside a string that we got from a `display' property. */
4796 if (it && it->string_from_display_prop_p)
4797 return 0;
4798
4799 /* Characters having this form of property are not displayed, so
4800 we have to find the end of the property. */
4801 if (it)
4802 {
4803 start_pos = *position;
4804 *position = display_prop_end (it, object, start_pos);
4805 }
4806 value = Qnil;
4807
4808 /* Stop the scan at that end position--we assume that all
4809 text properties change there. */
4810 if (it)
4811 it->stop_charpos = position->charpos;
4812
4813 /* Handle `(left-fringe BITMAP [FACE])'
4814 and `(right-fringe BITMAP [FACE])'. */
4815 if (CONSP (spec)
4816 && (EQ (XCAR (spec), Qleft_fringe)
4817 || EQ (XCAR (spec), Qright_fringe))
4818 && CONSP (XCDR (spec)))
4819 {
4820 int fringe_bitmap;
4821
4822 if (it)
4823 {
4824 if (!FRAME_WINDOW_P (it->f))
4825 /* If we return here, POSITION has been advanced
4826 across the text with this property. */
4827 {
4828 /* Synchronize the bidi iterator with POSITION. This is
4829 needed because we are not going to push the iterator
4830 on behalf of this display property, so there will be
4831 no pop_it call to do this synchronization for us. */
4832 if (it->bidi_p)
4833 {
4834 it->position = *position;
4835 iterate_out_of_display_property (it);
4836 *position = it->position;
4837 }
4838 return 1;
4839 }
4840 }
4841 else if (!frame_window_p)
4842 return 1;
4843
4844 #ifdef HAVE_WINDOW_SYSTEM
4845 value = XCAR (XCDR (spec));
4846 if (!SYMBOLP (value)
4847 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4848 /* If we return here, POSITION has been advanced
4849 across the text with this property. */
4850 {
4851 if (it && it->bidi_p)
4852 {
4853 it->position = *position;
4854 iterate_out_of_display_property (it);
4855 *position = it->position;
4856 }
4857 return 1;
4858 }
4859
4860 if (it)
4861 {
4862 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4863
4864 if (CONSP (XCDR (XCDR (spec))))
4865 {
4866 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4867 int face_id2 = lookup_derived_face (it->f, face_name,
4868 FRINGE_FACE_ID, 0);
4869 if (face_id2 >= 0)
4870 face_id = face_id2;
4871 }
4872
4873 /* Save current settings of IT so that we can restore them
4874 when we are finished with the glyph property value. */
4875 push_it (it, position);
4876
4877 it->area = TEXT_AREA;
4878 it->what = IT_IMAGE;
4879 it->image_id = -1; /* no image */
4880 it->position = start_pos;
4881 it->object = NILP (object) ? it->w->contents : object;
4882 it->method = GET_FROM_IMAGE;
4883 it->from_overlay = Qnil;
4884 it->face_id = face_id;
4885 it->from_disp_prop_p = 1;
4886
4887 /* Say that we haven't consumed the characters with
4888 `display' property yet. The call to pop_it in
4889 set_iterator_to_next will clean this up. */
4890 *position = start_pos;
4891
4892 if (EQ (XCAR (spec), Qleft_fringe))
4893 {
4894 it->left_user_fringe_bitmap = fringe_bitmap;
4895 it->left_user_fringe_face_id = face_id;
4896 }
4897 else
4898 {
4899 it->right_user_fringe_bitmap = fringe_bitmap;
4900 it->right_user_fringe_face_id = face_id;
4901 }
4902 }
4903 #endif /* HAVE_WINDOW_SYSTEM */
4904 return 1;
4905 }
4906
4907 /* Prepare to handle `((margin left-margin) ...)',
4908 `((margin right-margin) ...)' and `((margin nil) ...)'
4909 prefixes for display specifications. */
4910 location = Qunbound;
4911 if (CONSP (spec) && CONSP (XCAR (spec)))
4912 {
4913 Lisp_Object tem;
4914
4915 value = XCDR (spec);
4916 if (CONSP (value))
4917 value = XCAR (value);
4918
4919 tem = XCAR (spec);
4920 if (EQ (XCAR (tem), Qmargin)
4921 && (tem = XCDR (tem),
4922 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4923 (NILP (tem)
4924 || EQ (tem, Qleft_margin)
4925 || EQ (tem, Qright_margin))))
4926 location = tem;
4927 }
4928
4929 if (EQ (location, Qunbound))
4930 {
4931 location = Qnil;
4932 value = spec;
4933 }
4934
4935 /* After this point, VALUE is the property after any
4936 margin prefix has been stripped. It must be a string,
4937 an image specification, or `(space ...)'.
4938
4939 LOCATION specifies where to display: `left-margin',
4940 `right-margin' or nil. */
4941
4942 valid_p = (STRINGP (value)
4943 #ifdef HAVE_WINDOW_SYSTEM
4944 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4945 && valid_image_p (value))
4946 #endif /* not HAVE_WINDOW_SYSTEM */
4947 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4948
4949 if (valid_p && !display_replaced_p)
4950 {
4951 int retval = 1;
4952
4953 if (!it)
4954 {
4955 /* Callers need to know whether the display spec is any kind
4956 of `(space ...)' spec that is about to affect text-area
4957 display. */
4958 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4959 retval = 2;
4960 return retval;
4961 }
4962
4963 /* Save current settings of IT so that we can restore them
4964 when we are finished with the glyph property value. */
4965 push_it (it, position);
4966 it->from_overlay = overlay;
4967 it->from_disp_prop_p = 1;
4968
4969 if (NILP (location))
4970 it->area = TEXT_AREA;
4971 else if (EQ (location, Qleft_margin))
4972 it->area = LEFT_MARGIN_AREA;
4973 else
4974 it->area = RIGHT_MARGIN_AREA;
4975
4976 if (STRINGP (value))
4977 {
4978 it->string = value;
4979 it->multibyte_p = STRING_MULTIBYTE (it->string);
4980 it->current.overlay_string_index = -1;
4981 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4982 it->end_charpos = it->string_nchars = SCHARS (it->string);
4983 it->method = GET_FROM_STRING;
4984 it->stop_charpos = 0;
4985 it->prev_stop = 0;
4986 it->base_level_stop = 0;
4987 it->string_from_display_prop_p = 1;
4988 /* Say that we haven't consumed the characters with
4989 `display' property yet. The call to pop_it in
4990 set_iterator_to_next will clean this up. */
4991 if (BUFFERP (object))
4992 *position = start_pos;
4993
4994 /* Force paragraph direction to be that of the parent
4995 object. If the parent object's paragraph direction is
4996 not yet determined, default to L2R. */
4997 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4998 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4999 else
5000 it->paragraph_embedding = L2R;
5001
5002 /* Set up the bidi iterator for this display string. */
5003 if (it->bidi_p)
5004 {
5005 it->bidi_it.string.lstring = it->string;
5006 it->bidi_it.string.s = NULL;
5007 it->bidi_it.string.schars = it->end_charpos;
5008 it->bidi_it.string.bufpos = bufpos;
5009 it->bidi_it.string.from_disp_str = 1;
5010 it->bidi_it.string.unibyte = !it->multibyte_p;
5011 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5012 }
5013 }
5014 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5015 {
5016 it->method = GET_FROM_STRETCH;
5017 it->object = value;
5018 *position = it->position = start_pos;
5019 retval = 1 + (it->area == TEXT_AREA);
5020 }
5021 #ifdef HAVE_WINDOW_SYSTEM
5022 else
5023 {
5024 it->what = IT_IMAGE;
5025 it->image_id = lookup_image (it->f, value);
5026 it->position = start_pos;
5027 it->object = NILP (object) ? it->w->contents : object;
5028 it->method = GET_FROM_IMAGE;
5029
5030 /* Say that we haven't consumed the characters with
5031 `display' property yet. The call to pop_it in
5032 set_iterator_to_next will clean this up. */
5033 *position = start_pos;
5034 }
5035 #endif /* HAVE_WINDOW_SYSTEM */
5036
5037 return retval;
5038 }
5039
5040 /* Invalid property or property not supported. Restore
5041 POSITION to what it was before. */
5042 *position = start_pos;
5043 return 0;
5044 }
5045
5046 /* Check if PROP is a display property value whose text should be
5047 treated as intangible. OVERLAY is the overlay from which PROP
5048 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5049 specify the buffer position covered by PROP. */
5050
5051 int
5052 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5053 ptrdiff_t charpos, ptrdiff_t bytepos)
5054 {
5055 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5056 struct text_pos position;
5057
5058 SET_TEXT_POS (position, charpos, bytepos);
5059 return handle_display_spec (NULL, prop, Qnil, overlay,
5060 &position, charpos, frame_window_p);
5061 }
5062
5063
5064 /* Return 1 if PROP is a display sub-property value containing STRING.
5065
5066 Implementation note: this and the following function are really
5067 special cases of handle_display_spec and
5068 handle_single_display_spec, and should ideally use the same code.
5069 Until they do, these two pairs must be consistent and must be
5070 modified in sync. */
5071
5072 static int
5073 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5074 {
5075 if (EQ (string, prop))
5076 return 1;
5077
5078 /* Skip over `when FORM'. */
5079 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5080 {
5081 prop = XCDR (prop);
5082 if (!CONSP (prop))
5083 return 0;
5084 /* Actually, the condition following `when' should be eval'ed,
5085 like handle_single_display_spec does, and we should return
5086 zero if it evaluates to nil. However, this function is
5087 called only when the buffer was already displayed and some
5088 glyph in the glyph matrix was found to come from a display
5089 string. Therefore, the condition was already evaluated, and
5090 the result was non-nil, otherwise the display string wouldn't
5091 have been displayed and we would have never been called for
5092 this property. Thus, we can skip the evaluation and assume
5093 its result is non-nil. */
5094 prop = XCDR (prop);
5095 }
5096
5097 if (CONSP (prop))
5098 /* Skip over `margin LOCATION'. */
5099 if (EQ (XCAR (prop), Qmargin))
5100 {
5101 prop = XCDR (prop);
5102 if (!CONSP (prop))
5103 return 0;
5104
5105 prop = XCDR (prop);
5106 if (!CONSP (prop))
5107 return 0;
5108 }
5109
5110 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5111 }
5112
5113
5114 /* Return 1 if STRING appears in the `display' property PROP. */
5115
5116 static int
5117 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5118 {
5119 if (CONSP (prop)
5120 && !EQ (XCAR (prop), Qwhen)
5121 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5122 {
5123 /* A list of sub-properties. */
5124 while (CONSP (prop))
5125 {
5126 if (single_display_spec_string_p (XCAR (prop), string))
5127 return 1;
5128 prop = XCDR (prop);
5129 }
5130 }
5131 else if (VECTORP (prop))
5132 {
5133 /* A vector of sub-properties. */
5134 ptrdiff_t i;
5135 for (i = 0; i < ASIZE (prop); ++i)
5136 if (single_display_spec_string_p (AREF (prop, i), string))
5137 return 1;
5138 }
5139 else
5140 return single_display_spec_string_p (prop, string);
5141
5142 return 0;
5143 }
5144
5145 /* Look for STRING in overlays and text properties in the current
5146 buffer, between character positions FROM and TO (excluding TO).
5147 BACK_P non-zero means look back (in this case, TO is supposed to be
5148 less than FROM).
5149 Value is the first character position where STRING was found, or
5150 zero if it wasn't found before hitting TO.
5151
5152 This function may only use code that doesn't eval because it is
5153 called asynchronously from note_mouse_highlight. */
5154
5155 static ptrdiff_t
5156 string_buffer_position_lim (Lisp_Object string,
5157 ptrdiff_t from, ptrdiff_t to, int back_p)
5158 {
5159 Lisp_Object limit, prop, pos;
5160 int found = 0;
5161
5162 pos = make_number (max (from, BEGV));
5163
5164 if (!back_p) /* looking forward */
5165 {
5166 limit = make_number (min (to, ZV));
5167 while (!found && !EQ (pos, limit))
5168 {
5169 prop = Fget_char_property (pos, Qdisplay, Qnil);
5170 if (!NILP (prop) && display_prop_string_p (prop, string))
5171 found = 1;
5172 else
5173 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5174 limit);
5175 }
5176 }
5177 else /* looking back */
5178 {
5179 limit = make_number (max (to, BEGV));
5180 while (!found && !EQ (pos, limit))
5181 {
5182 prop = Fget_char_property (pos, Qdisplay, Qnil);
5183 if (!NILP (prop) && display_prop_string_p (prop, string))
5184 found = 1;
5185 else
5186 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5187 limit);
5188 }
5189 }
5190
5191 return found ? XINT (pos) : 0;
5192 }
5193
5194 /* Determine which buffer position in current buffer STRING comes from.
5195 AROUND_CHARPOS is an approximate position where it could come from.
5196 Value is the buffer position or 0 if it couldn't be determined.
5197
5198 This function is necessary because we don't record buffer positions
5199 in glyphs generated from strings (to keep struct glyph small).
5200 This function may only use code that doesn't eval because it is
5201 called asynchronously from note_mouse_highlight. */
5202
5203 static ptrdiff_t
5204 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5205 {
5206 const int MAX_DISTANCE = 1000;
5207 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5208 around_charpos + MAX_DISTANCE,
5209 0);
5210
5211 if (!found)
5212 found = string_buffer_position_lim (string, around_charpos,
5213 around_charpos - MAX_DISTANCE, 1);
5214 return found;
5215 }
5216
5217
5218 \f
5219 /***********************************************************************
5220 `composition' property
5221 ***********************************************************************/
5222
5223 /* Set up iterator IT from `composition' property at its current
5224 position. Called from handle_stop. */
5225
5226 static enum prop_handled
5227 handle_composition_prop (struct it *it)
5228 {
5229 Lisp_Object prop, string;
5230 ptrdiff_t pos, pos_byte, start, end;
5231
5232 if (STRINGP (it->string))
5233 {
5234 unsigned char *s;
5235
5236 pos = IT_STRING_CHARPOS (*it);
5237 pos_byte = IT_STRING_BYTEPOS (*it);
5238 string = it->string;
5239 s = SDATA (string) + pos_byte;
5240 it->c = STRING_CHAR (s);
5241 }
5242 else
5243 {
5244 pos = IT_CHARPOS (*it);
5245 pos_byte = IT_BYTEPOS (*it);
5246 string = Qnil;
5247 it->c = FETCH_CHAR (pos_byte);
5248 }
5249
5250 /* If there's a valid composition and point is not inside of the
5251 composition (in the case that the composition is from the current
5252 buffer), draw a glyph composed from the composition components. */
5253 if (find_composition (pos, -1, &start, &end, &prop, string)
5254 && COMPOSITION_VALID_P (start, end, prop)
5255 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5256 {
5257 if (start < pos)
5258 /* As we can't handle this situation (perhaps font-lock added
5259 a new composition), we just return here hoping that next
5260 redisplay will detect this composition much earlier. */
5261 return HANDLED_NORMALLY;
5262 if (start != pos)
5263 {
5264 if (STRINGP (it->string))
5265 pos_byte = string_char_to_byte (it->string, start);
5266 else
5267 pos_byte = CHAR_TO_BYTE (start);
5268 }
5269 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5270 prop, string);
5271
5272 if (it->cmp_it.id >= 0)
5273 {
5274 it->cmp_it.ch = -1;
5275 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5276 it->cmp_it.nglyphs = -1;
5277 }
5278 }
5279
5280 return HANDLED_NORMALLY;
5281 }
5282
5283
5284 \f
5285 /***********************************************************************
5286 Overlay strings
5287 ***********************************************************************/
5288
5289 /* The following structure is used to record overlay strings for
5290 later sorting in load_overlay_strings. */
5291
5292 struct overlay_entry
5293 {
5294 Lisp_Object overlay;
5295 Lisp_Object string;
5296 EMACS_INT priority;
5297 int after_string_p;
5298 };
5299
5300
5301 /* Set up iterator IT from overlay strings at its current position.
5302 Called from handle_stop. */
5303
5304 static enum prop_handled
5305 handle_overlay_change (struct it *it)
5306 {
5307 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5308 return HANDLED_RECOMPUTE_PROPS;
5309 else
5310 return HANDLED_NORMALLY;
5311 }
5312
5313
5314 /* Set up the next overlay string for delivery by IT, if there is an
5315 overlay string to deliver. Called by set_iterator_to_next when the
5316 end of the current overlay string is reached. If there are more
5317 overlay strings to display, IT->string and
5318 IT->current.overlay_string_index are set appropriately here.
5319 Otherwise IT->string is set to nil. */
5320
5321 static void
5322 next_overlay_string (struct it *it)
5323 {
5324 ++it->current.overlay_string_index;
5325 if (it->current.overlay_string_index == it->n_overlay_strings)
5326 {
5327 /* No more overlay strings. Restore IT's settings to what
5328 they were before overlay strings were processed, and
5329 continue to deliver from current_buffer. */
5330
5331 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5332 pop_it (it);
5333 eassert (it->sp > 0
5334 || (NILP (it->string)
5335 && it->method == GET_FROM_BUFFER
5336 && it->stop_charpos >= BEGV
5337 && it->stop_charpos <= it->end_charpos));
5338 it->current.overlay_string_index = -1;
5339 it->n_overlay_strings = 0;
5340 it->overlay_strings_charpos = -1;
5341 /* If there's an empty display string on the stack, pop the
5342 stack, to resync the bidi iterator with IT's position. Such
5343 empty strings are pushed onto the stack in
5344 get_overlay_strings_1. */
5345 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5346 pop_it (it);
5347
5348 /* If we're at the end of the buffer, record that we have
5349 processed the overlay strings there already, so that
5350 next_element_from_buffer doesn't try it again. */
5351 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5352 it->overlay_strings_at_end_processed_p = 1;
5353 }
5354 else
5355 {
5356 /* There are more overlay strings to process. If
5357 IT->current.overlay_string_index has advanced to a position
5358 where we must load IT->overlay_strings with more strings, do
5359 it. We must load at the IT->overlay_strings_charpos where
5360 IT->n_overlay_strings was originally computed; when invisible
5361 text is present, this might not be IT_CHARPOS (Bug#7016). */
5362 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5363
5364 if (it->current.overlay_string_index && i == 0)
5365 load_overlay_strings (it, it->overlay_strings_charpos);
5366
5367 /* Initialize IT to deliver display elements from the overlay
5368 string. */
5369 it->string = it->overlay_strings[i];
5370 it->multibyte_p = STRING_MULTIBYTE (it->string);
5371 SET_TEXT_POS (it->current.string_pos, 0, 0);
5372 it->method = GET_FROM_STRING;
5373 it->stop_charpos = 0;
5374 it->end_charpos = SCHARS (it->string);
5375 if (it->cmp_it.stop_pos >= 0)
5376 it->cmp_it.stop_pos = 0;
5377 it->prev_stop = 0;
5378 it->base_level_stop = 0;
5379
5380 /* Set up the bidi iterator for this overlay string. */
5381 if (it->bidi_p)
5382 {
5383 it->bidi_it.string.lstring = it->string;
5384 it->bidi_it.string.s = NULL;
5385 it->bidi_it.string.schars = SCHARS (it->string);
5386 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5387 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5388 it->bidi_it.string.unibyte = !it->multibyte_p;
5389 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5390 }
5391 }
5392
5393 CHECK_IT (it);
5394 }
5395
5396
5397 /* Compare two overlay_entry structures E1 and E2. Used as a
5398 comparison function for qsort in load_overlay_strings. Overlay
5399 strings for the same position are sorted so that
5400
5401 1. All after-strings come in front of before-strings, except
5402 when they come from the same overlay.
5403
5404 2. Within after-strings, strings are sorted so that overlay strings
5405 from overlays with higher priorities come first.
5406
5407 2. Within before-strings, strings are sorted so that overlay
5408 strings from overlays with higher priorities come last.
5409
5410 Value is analogous to strcmp. */
5411
5412
5413 static int
5414 compare_overlay_entries (const void *e1, const void *e2)
5415 {
5416 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5417 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5418 int result;
5419
5420 if (entry1->after_string_p != entry2->after_string_p)
5421 {
5422 /* Let after-strings appear in front of before-strings if
5423 they come from different overlays. */
5424 if (EQ (entry1->overlay, entry2->overlay))
5425 result = entry1->after_string_p ? 1 : -1;
5426 else
5427 result = entry1->after_string_p ? -1 : 1;
5428 }
5429 else if (entry1->priority != entry2->priority)
5430 {
5431 if (entry1->after_string_p)
5432 /* After-strings sorted in order of decreasing priority. */
5433 result = entry2->priority < entry1->priority ? -1 : 1;
5434 else
5435 /* Before-strings sorted in order of increasing priority. */
5436 result = entry1->priority < entry2->priority ? -1 : 1;
5437 }
5438 else
5439 result = 0;
5440
5441 return result;
5442 }
5443
5444
5445 /* Load the vector IT->overlay_strings with overlay strings from IT's
5446 current buffer position, or from CHARPOS if that is > 0. Set
5447 IT->n_overlays to the total number of overlay strings found.
5448
5449 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5450 a time. On entry into load_overlay_strings,
5451 IT->current.overlay_string_index gives the number of overlay
5452 strings that have already been loaded by previous calls to this
5453 function.
5454
5455 IT->add_overlay_start contains an additional overlay start
5456 position to consider for taking overlay strings from, if non-zero.
5457 This position comes into play when the overlay has an `invisible'
5458 property, and both before and after-strings. When we've skipped to
5459 the end of the overlay, because of its `invisible' property, we
5460 nevertheless want its before-string to appear.
5461 IT->add_overlay_start will contain the overlay start position
5462 in this case.
5463
5464 Overlay strings are sorted so that after-string strings come in
5465 front of before-string strings. Within before and after-strings,
5466 strings are sorted by overlay priority. See also function
5467 compare_overlay_entries. */
5468
5469 static void
5470 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5471 {
5472 Lisp_Object overlay, window, str, invisible;
5473 struct Lisp_Overlay *ov;
5474 ptrdiff_t start, end;
5475 ptrdiff_t size = 20;
5476 ptrdiff_t n = 0, i, j;
5477 int invis_p;
5478 struct overlay_entry *entries = alloca (size * sizeof *entries);
5479 USE_SAFE_ALLOCA;
5480
5481 if (charpos <= 0)
5482 charpos = IT_CHARPOS (*it);
5483
5484 /* Append the overlay string STRING of overlay OVERLAY to vector
5485 `entries' which has size `size' and currently contains `n'
5486 elements. AFTER_P non-zero means STRING is an after-string of
5487 OVERLAY. */
5488 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5489 do \
5490 { \
5491 Lisp_Object priority; \
5492 \
5493 if (n == size) \
5494 { \
5495 struct overlay_entry *old = entries; \
5496 SAFE_NALLOCA (entries, 2, size); \
5497 memcpy (entries, old, size * sizeof *entries); \
5498 size *= 2; \
5499 } \
5500 \
5501 entries[n].string = (STRING); \
5502 entries[n].overlay = (OVERLAY); \
5503 priority = Foverlay_get ((OVERLAY), Qpriority); \
5504 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5505 entries[n].after_string_p = (AFTER_P); \
5506 ++n; \
5507 } \
5508 while (0)
5509
5510 /* Process overlay before the overlay center. */
5511 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5512 {
5513 XSETMISC (overlay, ov);
5514 eassert (OVERLAYP (overlay));
5515 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5516 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5517
5518 if (end < charpos)
5519 break;
5520
5521 /* Skip this overlay if it doesn't start or end at IT's current
5522 position. */
5523 if (end != charpos && start != charpos)
5524 continue;
5525
5526 /* Skip this overlay if it doesn't apply to IT->w. */
5527 window = Foverlay_get (overlay, Qwindow);
5528 if (WINDOWP (window) && XWINDOW (window) != it->w)
5529 continue;
5530
5531 /* If the text ``under'' the overlay is invisible, both before-
5532 and after-strings from this overlay are visible; start and
5533 end position are indistinguishable. */
5534 invisible = Foverlay_get (overlay, Qinvisible);
5535 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5536
5537 /* If overlay has a non-empty before-string, record it. */
5538 if ((start == charpos || (end == charpos && invis_p))
5539 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5540 && SCHARS (str))
5541 RECORD_OVERLAY_STRING (overlay, str, 0);
5542
5543 /* If overlay has a non-empty after-string, record it. */
5544 if ((end == charpos || (start == charpos && invis_p))
5545 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5546 && SCHARS (str))
5547 RECORD_OVERLAY_STRING (overlay, str, 1);
5548 }
5549
5550 /* Process overlays after the overlay center. */
5551 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5552 {
5553 XSETMISC (overlay, ov);
5554 eassert (OVERLAYP (overlay));
5555 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5556 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5557
5558 if (start > charpos)
5559 break;
5560
5561 /* Skip this overlay if it doesn't start or end at IT's current
5562 position. */
5563 if (end != charpos && start != charpos)
5564 continue;
5565
5566 /* Skip this overlay if it doesn't apply to IT->w. */
5567 window = Foverlay_get (overlay, Qwindow);
5568 if (WINDOWP (window) && XWINDOW (window) != it->w)
5569 continue;
5570
5571 /* If the text ``under'' the overlay is invisible, it has a zero
5572 dimension, and both before- and after-strings apply. */
5573 invisible = Foverlay_get (overlay, Qinvisible);
5574 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5575
5576 /* If overlay has a non-empty before-string, record it. */
5577 if ((start == charpos || (end == charpos && invis_p))
5578 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5579 && SCHARS (str))
5580 RECORD_OVERLAY_STRING (overlay, str, 0);
5581
5582 /* If overlay has a non-empty after-string, record it. */
5583 if ((end == charpos || (start == charpos && invis_p))
5584 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5585 && SCHARS (str))
5586 RECORD_OVERLAY_STRING (overlay, str, 1);
5587 }
5588
5589 #undef RECORD_OVERLAY_STRING
5590
5591 /* Sort entries. */
5592 if (n > 1)
5593 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5594
5595 /* Record number of overlay strings, and where we computed it. */
5596 it->n_overlay_strings = n;
5597 it->overlay_strings_charpos = charpos;
5598
5599 /* IT->current.overlay_string_index is the number of overlay strings
5600 that have already been consumed by IT. Copy some of the
5601 remaining overlay strings to IT->overlay_strings. */
5602 i = 0;
5603 j = it->current.overlay_string_index;
5604 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5605 {
5606 it->overlay_strings[i] = entries[j].string;
5607 it->string_overlays[i++] = entries[j++].overlay;
5608 }
5609
5610 CHECK_IT (it);
5611 SAFE_FREE ();
5612 }
5613
5614
5615 /* Get the first chunk of overlay strings at IT's current buffer
5616 position, or at CHARPOS if that is > 0. Value is non-zero if at
5617 least one overlay string was found. */
5618
5619 static int
5620 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5621 {
5622 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5623 process. This fills IT->overlay_strings with strings, and sets
5624 IT->n_overlay_strings to the total number of strings to process.
5625 IT->pos.overlay_string_index has to be set temporarily to zero
5626 because load_overlay_strings needs this; it must be set to -1
5627 when no overlay strings are found because a zero value would
5628 indicate a position in the first overlay string. */
5629 it->current.overlay_string_index = 0;
5630 load_overlay_strings (it, charpos);
5631
5632 /* If we found overlay strings, set up IT to deliver display
5633 elements from the first one. Otherwise set up IT to deliver
5634 from current_buffer. */
5635 if (it->n_overlay_strings)
5636 {
5637 /* Make sure we know settings in current_buffer, so that we can
5638 restore meaningful values when we're done with the overlay
5639 strings. */
5640 if (compute_stop_p)
5641 compute_stop_pos (it);
5642 eassert (it->face_id >= 0);
5643
5644 /* Save IT's settings. They are restored after all overlay
5645 strings have been processed. */
5646 eassert (!compute_stop_p || it->sp == 0);
5647
5648 /* When called from handle_stop, there might be an empty display
5649 string loaded. In that case, don't bother saving it. But
5650 don't use this optimization with the bidi iterator, since we
5651 need the corresponding pop_it call to resync the bidi
5652 iterator's position with IT's position, after we are done
5653 with the overlay strings. (The corresponding call to pop_it
5654 in case of an empty display string is in
5655 next_overlay_string.) */
5656 if (!(!it->bidi_p
5657 && STRINGP (it->string) && !SCHARS (it->string)))
5658 push_it (it, NULL);
5659
5660 /* Set up IT to deliver display elements from the first overlay
5661 string. */
5662 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5663 it->string = it->overlay_strings[0];
5664 it->from_overlay = Qnil;
5665 it->stop_charpos = 0;
5666 eassert (STRINGP (it->string));
5667 it->end_charpos = SCHARS (it->string);
5668 it->prev_stop = 0;
5669 it->base_level_stop = 0;
5670 it->multibyte_p = STRING_MULTIBYTE (it->string);
5671 it->method = GET_FROM_STRING;
5672 it->from_disp_prop_p = 0;
5673
5674 /* Force paragraph direction to be that of the parent
5675 buffer. */
5676 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5677 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5678 else
5679 it->paragraph_embedding = L2R;
5680
5681 /* Set up the bidi iterator for this overlay string. */
5682 if (it->bidi_p)
5683 {
5684 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5685
5686 it->bidi_it.string.lstring = it->string;
5687 it->bidi_it.string.s = NULL;
5688 it->bidi_it.string.schars = SCHARS (it->string);
5689 it->bidi_it.string.bufpos = pos;
5690 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5691 it->bidi_it.string.unibyte = !it->multibyte_p;
5692 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5693 }
5694 return 1;
5695 }
5696
5697 it->current.overlay_string_index = -1;
5698 return 0;
5699 }
5700
5701 static int
5702 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5703 {
5704 it->string = Qnil;
5705 it->method = GET_FROM_BUFFER;
5706
5707 (void) get_overlay_strings_1 (it, charpos, 1);
5708
5709 CHECK_IT (it);
5710
5711 /* Value is non-zero if we found at least one overlay string. */
5712 return STRINGP (it->string);
5713 }
5714
5715
5716 \f
5717 /***********************************************************************
5718 Saving and restoring state
5719 ***********************************************************************/
5720
5721 /* Save current settings of IT on IT->stack. Called, for example,
5722 before setting up IT for an overlay string, to be able to restore
5723 IT's settings to what they were after the overlay string has been
5724 processed. If POSITION is non-NULL, it is the position to save on
5725 the stack instead of IT->position. */
5726
5727 static void
5728 push_it (struct it *it, struct text_pos *position)
5729 {
5730 struct iterator_stack_entry *p;
5731
5732 eassert (it->sp < IT_STACK_SIZE);
5733 p = it->stack + it->sp;
5734
5735 p->stop_charpos = it->stop_charpos;
5736 p->prev_stop = it->prev_stop;
5737 p->base_level_stop = it->base_level_stop;
5738 p->cmp_it = it->cmp_it;
5739 eassert (it->face_id >= 0);
5740 p->face_id = it->face_id;
5741 p->string = it->string;
5742 p->method = it->method;
5743 p->from_overlay = it->from_overlay;
5744 switch (p->method)
5745 {
5746 case GET_FROM_IMAGE:
5747 p->u.image.object = it->object;
5748 p->u.image.image_id = it->image_id;
5749 p->u.image.slice = it->slice;
5750 break;
5751 case GET_FROM_STRETCH:
5752 p->u.stretch.object = it->object;
5753 break;
5754 }
5755 p->position = position ? *position : it->position;
5756 p->current = it->current;
5757 p->end_charpos = it->end_charpos;
5758 p->string_nchars = it->string_nchars;
5759 p->area = it->area;
5760 p->multibyte_p = it->multibyte_p;
5761 p->avoid_cursor_p = it->avoid_cursor_p;
5762 p->space_width = it->space_width;
5763 p->font_height = it->font_height;
5764 p->voffset = it->voffset;
5765 p->string_from_display_prop_p = it->string_from_display_prop_p;
5766 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5767 p->display_ellipsis_p = 0;
5768 p->line_wrap = it->line_wrap;
5769 p->bidi_p = it->bidi_p;
5770 p->paragraph_embedding = it->paragraph_embedding;
5771 p->from_disp_prop_p = it->from_disp_prop_p;
5772 ++it->sp;
5773
5774 /* Save the state of the bidi iterator as well. */
5775 if (it->bidi_p)
5776 bidi_push_it (&it->bidi_it);
5777 }
5778
5779 static void
5780 iterate_out_of_display_property (struct it *it)
5781 {
5782 int buffer_p = !STRINGP (it->string);
5783 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5784 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5785
5786 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5787
5788 /* Maybe initialize paragraph direction. If we are at the beginning
5789 of a new paragraph, next_element_from_buffer may not have a
5790 chance to do that. */
5791 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5792 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5793 /* prev_stop can be zero, so check against BEGV as well. */
5794 while (it->bidi_it.charpos >= bob
5795 && it->prev_stop <= it->bidi_it.charpos
5796 && it->bidi_it.charpos < CHARPOS (it->position)
5797 && it->bidi_it.charpos < eob)
5798 bidi_move_to_visually_next (&it->bidi_it);
5799 /* Record the stop_pos we just crossed, for when we cross it
5800 back, maybe. */
5801 if (it->bidi_it.charpos > CHARPOS (it->position))
5802 it->prev_stop = CHARPOS (it->position);
5803 /* If we ended up not where pop_it put us, resync IT's
5804 positional members with the bidi iterator. */
5805 if (it->bidi_it.charpos != CHARPOS (it->position))
5806 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5807 if (buffer_p)
5808 it->current.pos = it->position;
5809 else
5810 it->current.string_pos = it->position;
5811 }
5812
5813 /* Restore IT's settings from IT->stack. Called, for example, when no
5814 more overlay strings must be processed, and we return to delivering
5815 display elements from a buffer, or when the end of a string from a
5816 `display' property is reached and we return to delivering display
5817 elements from an overlay string, or from a buffer. */
5818
5819 static void
5820 pop_it (struct it *it)
5821 {
5822 struct iterator_stack_entry *p;
5823 int from_display_prop = it->from_disp_prop_p;
5824
5825 eassert (it->sp > 0);
5826 --it->sp;
5827 p = it->stack + it->sp;
5828 it->stop_charpos = p->stop_charpos;
5829 it->prev_stop = p->prev_stop;
5830 it->base_level_stop = p->base_level_stop;
5831 it->cmp_it = p->cmp_it;
5832 it->face_id = p->face_id;
5833 it->current = p->current;
5834 it->position = p->position;
5835 it->string = p->string;
5836 it->from_overlay = p->from_overlay;
5837 if (NILP (it->string))
5838 SET_TEXT_POS (it->current.string_pos, -1, -1);
5839 it->method = p->method;
5840 switch (it->method)
5841 {
5842 case GET_FROM_IMAGE:
5843 it->image_id = p->u.image.image_id;
5844 it->object = p->u.image.object;
5845 it->slice = p->u.image.slice;
5846 break;
5847 case GET_FROM_STRETCH:
5848 it->object = p->u.stretch.object;
5849 break;
5850 case GET_FROM_BUFFER:
5851 it->object = it->w->contents;
5852 break;
5853 case GET_FROM_STRING:
5854 it->object = it->string;
5855 break;
5856 case GET_FROM_DISPLAY_VECTOR:
5857 if (it->s)
5858 it->method = GET_FROM_C_STRING;
5859 else if (STRINGP (it->string))
5860 it->method = GET_FROM_STRING;
5861 else
5862 {
5863 it->method = GET_FROM_BUFFER;
5864 it->object = it->w->contents;
5865 }
5866 }
5867 it->end_charpos = p->end_charpos;
5868 it->string_nchars = p->string_nchars;
5869 it->area = p->area;
5870 it->multibyte_p = p->multibyte_p;
5871 it->avoid_cursor_p = p->avoid_cursor_p;
5872 it->space_width = p->space_width;
5873 it->font_height = p->font_height;
5874 it->voffset = p->voffset;
5875 it->string_from_display_prop_p = p->string_from_display_prop_p;
5876 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5877 it->line_wrap = p->line_wrap;
5878 it->bidi_p = p->bidi_p;
5879 it->paragraph_embedding = p->paragraph_embedding;
5880 it->from_disp_prop_p = p->from_disp_prop_p;
5881 if (it->bidi_p)
5882 {
5883 bidi_pop_it (&it->bidi_it);
5884 /* Bidi-iterate until we get out of the portion of text, if any,
5885 covered by a `display' text property or by an overlay with
5886 `display' property. (We cannot just jump there, because the
5887 internal coherency of the bidi iterator state can not be
5888 preserved across such jumps.) We also must determine the
5889 paragraph base direction if the overlay we just processed is
5890 at the beginning of a new paragraph. */
5891 if (from_display_prop
5892 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5893 iterate_out_of_display_property (it);
5894
5895 eassert ((BUFFERP (it->object)
5896 && IT_CHARPOS (*it) == it->bidi_it.charpos
5897 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5898 || (STRINGP (it->object)
5899 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5900 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5901 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5902 }
5903 }
5904
5905
5906 \f
5907 /***********************************************************************
5908 Moving over lines
5909 ***********************************************************************/
5910
5911 /* Set IT's current position to the previous line start. */
5912
5913 static void
5914 back_to_previous_line_start (struct it *it)
5915 {
5916 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5917
5918 DEC_BOTH (cp, bp);
5919 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5920 }
5921
5922
5923 /* Move IT to the next line start.
5924
5925 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5926 we skipped over part of the text (as opposed to moving the iterator
5927 continuously over the text). Otherwise, don't change the value
5928 of *SKIPPED_P.
5929
5930 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5931 iterator on the newline, if it was found.
5932
5933 Newlines may come from buffer text, overlay strings, or strings
5934 displayed via the `display' property. That's the reason we can't
5935 simply use find_newline_no_quit.
5936
5937 Note that this function may not skip over invisible text that is so
5938 because of text properties and immediately follows a newline. If
5939 it would, function reseat_at_next_visible_line_start, when called
5940 from set_iterator_to_next, would effectively make invisible
5941 characters following a newline part of the wrong glyph row, which
5942 leads to wrong cursor motion. */
5943
5944 static int
5945 forward_to_next_line_start (struct it *it, int *skipped_p,
5946 struct bidi_it *bidi_it_prev)
5947 {
5948 ptrdiff_t old_selective;
5949 int newline_found_p, n;
5950 const int MAX_NEWLINE_DISTANCE = 500;
5951
5952 /* If already on a newline, just consume it to avoid unintended
5953 skipping over invisible text below. */
5954 if (it->what == IT_CHARACTER
5955 && it->c == '\n'
5956 && CHARPOS (it->position) == IT_CHARPOS (*it))
5957 {
5958 if (it->bidi_p && bidi_it_prev)
5959 *bidi_it_prev = it->bidi_it;
5960 set_iterator_to_next (it, 0);
5961 it->c = 0;
5962 return 1;
5963 }
5964
5965 /* Don't handle selective display in the following. It's (a)
5966 unnecessary because it's done by the caller, and (b) leads to an
5967 infinite recursion because next_element_from_ellipsis indirectly
5968 calls this function. */
5969 old_selective = it->selective;
5970 it->selective = 0;
5971
5972 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5973 from buffer text. */
5974 for (n = newline_found_p = 0;
5975 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5976 n += STRINGP (it->string) ? 0 : 1)
5977 {
5978 if (!get_next_display_element (it))
5979 return 0;
5980 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5981 if (newline_found_p && it->bidi_p && bidi_it_prev)
5982 *bidi_it_prev = it->bidi_it;
5983 set_iterator_to_next (it, 0);
5984 }
5985
5986 /* If we didn't find a newline near enough, see if we can use a
5987 short-cut. */
5988 if (!newline_found_p)
5989 {
5990 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
5991 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
5992 1, &bytepos);
5993 Lisp_Object pos;
5994
5995 eassert (!STRINGP (it->string));
5996
5997 /* If there isn't any `display' property in sight, and no
5998 overlays, we can just use the position of the newline in
5999 buffer text. */
6000 if (it->stop_charpos >= limit
6001 || ((pos = Fnext_single_property_change (make_number (start),
6002 Qdisplay, Qnil,
6003 make_number (limit)),
6004 NILP (pos))
6005 && next_overlay_change (start) == ZV))
6006 {
6007 if (!it->bidi_p)
6008 {
6009 IT_CHARPOS (*it) = limit;
6010 IT_BYTEPOS (*it) = bytepos;
6011 }
6012 else
6013 {
6014 struct bidi_it bprev;
6015
6016 /* Help bidi.c avoid expensive searches for display
6017 properties and overlays, by telling it that there are
6018 none up to `limit'. */
6019 if (it->bidi_it.disp_pos < limit)
6020 {
6021 it->bidi_it.disp_pos = limit;
6022 it->bidi_it.disp_prop = 0;
6023 }
6024 do {
6025 bprev = it->bidi_it;
6026 bidi_move_to_visually_next (&it->bidi_it);
6027 } while (it->bidi_it.charpos != limit);
6028 IT_CHARPOS (*it) = limit;
6029 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6030 if (bidi_it_prev)
6031 *bidi_it_prev = bprev;
6032 }
6033 *skipped_p = newline_found_p = 1;
6034 }
6035 else
6036 {
6037 while (get_next_display_element (it)
6038 && !newline_found_p)
6039 {
6040 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6041 if (newline_found_p && it->bidi_p && bidi_it_prev)
6042 *bidi_it_prev = it->bidi_it;
6043 set_iterator_to_next (it, 0);
6044 }
6045 }
6046 }
6047
6048 it->selective = old_selective;
6049 return newline_found_p;
6050 }
6051
6052
6053 /* Set IT's current position to the previous visible line start. Skip
6054 invisible text that is so either due to text properties or due to
6055 selective display. Caution: this does not change IT->current_x and
6056 IT->hpos. */
6057
6058 static void
6059 back_to_previous_visible_line_start (struct it *it)
6060 {
6061 while (IT_CHARPOS (*it) > BEGV)
6062 {
6063 back_to_previous_line_start (it);
6064
6065 if (IT_CHARPOS (*it) <= BEGV)
6066 break;
6067
6068 /* If selective > 0, then lines indented more than its value are
6069 invisible. */
6070 if (it->selective > 0
6071 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6072 it->selective))
6073 continue;
6074
6075 /* Check the newline before point for invisibility. */
6076 {
6077 Lisp_Object prop;
6078 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6079 Qinvisible, it->window);
6080 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6081 continue;
6082 }
6083
6084 if (IT_CHARPOS (*it) <= BEGV)
6085 break;
6086
6087 {
6088 struct it it2;
6089 void *it2data = NULL;
6090 ptrdiff_t pos;
6091 ptrdiff_t beg, end;
6092 Lisp_Object val, overlay;
6093
6094 SAVE_IT (it2, *it, it2data);
6095
6096 /* If newline is part of a composition, continue from start of composition */
6097 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6098 && beg < IT_CHARPOS (*it))
6099 goto replaced;
6100
6101 /* If newline is replaced by a display property, find start of overlay
6102 or interval and continue search from that point. */
6103 pos = --IT_CHARPOS (it2);
6104 --IT_BYTEPOS (it2);
6105 it2.sp = 0;
6106 bidi_unshelve_cache (NULL, 0);
6107 it2.string_from_display_prop_p = 0;
6108 it2.from_disp_prop_p = 0;
6109 if (handle_display_prop (&it2) == HANDLED_RETURN
6110 && !NILP (val = get_char_property_and_overlay
6111 (make_number (pos), Qdisplay, Qnil, &overlay))
6112 && (OVERLAYP (overlay)
6113 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6114 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6115 {
6116 RESTORE_IT (it, it, it2data);
6117 goto replaced;
6118 }
6119
6120 /* Newline is not replaced by anything -- so we are done. */
6121 RESTORE_IT (it, it, it2data);
6122 break;
6123
6124 replaced:
6125 if (beg < BEGV)
6126 beg = BEGV;
6127 IT_CHARPOS (*it) = beg;
6128 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6129 }
6130 }
6131
6132 it->continuation_lines_width = 0;
6133
6134 eassert (IT_CHARPOS (*it) >= BEGV);
6135 eassert (IT_CHARPOS (*it) == BEGV
6136 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6137 CHECK_IT (it);
6138 }
6139
6140
6141 /* Reseat iterator IT at the previous visible line start. Skip
6142 invisible text that is so either due to text properties or due to
6143 selective display. At the end, update IT's overlay information,
6144 face information etc. */
6145
6146 void
6147 reseat_at_previous_visible_line_start (struct it *it)
6148 {
6149 back_to_previous_visible_line_start (it);
6150 reseat (it, it->current.pos, 1);
6151 CHECK_IT (it);
6152 }
6153
6154
6155 /* Reseat iterator IT on the next visible line start in the current
6156 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6157 preceding the line start. Skip over invisible text that is so
6158 because of selective display. Compute faces, overlays etc at the
6159 new position. Note that this function does not skip over text that
6160 is invisible because of text properties. */
6161
6162 static void
6163 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6164 {
6165 int newline_found_p, skipped_p = 0;
6166 struct bidi_it bidi_it_prev;
6167
6168 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6169
6170 /* Skip over lines that are invisible because they are indented
6171 more than the value of IT->selective. */
6172 if (it->selective > 0)
6173 while (IT_CHARPOS (*it) < ZV
6174 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6175 it->selective))
6176 {
6177 eassert (IT_BYTEPOS (*it) == BEGV
6178 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6179 newline_found_p =
6180 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6181 }
6182
6183 /* Position on the newline if that's what's requested. */
6184 if (on_newline_p && newline_found_p)
6185 {
6186 if (STRINGP (it->string))
6187 {
6188 if (IT_STRING_CHARPOS (*it) > 0)
6189 {
6190 if (!it->bidi_p)
6191 {
6192 --IT_STRING_CHARPOS (*it);
6193 --IT_STRING_BYTEPOS (*it);
6194 }
6195 else
6196 {
6197 /* We need to restore the bidi iterator to the state
6198 it had on the newline, and resync the IT's
6199 position with that. */
6200 it->bidi_it = bidi_it_prev;
6201 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6202 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6203 }
6204 }
6205 }
6206 else if (IT_CHARPOS (*it) > BEGV)
6207 {
6208 if (!it->bidi_p)
6209 {
6210 --IT_CHARPOS (*it);
6211 --IT_BYTEPOS (*it);
6212 }
6213 else
6214 {
6215 /* We need to restore the bidi iterator to the state it
6216 had on the newline and resync IT with that. */
6217 it->bidi_it = bidi_it_prev;
6218 IT_CHARPOS (*it) = it->bidi_it.charpos;
6219 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6220 }
6221 reseat (it, it->current.pos, 0);
6222 }
6223 }
6224 else if (skipped_p)
6225 reseat (it, it->current.pos, 0);
6226
6227 CHECK_IT (it);
6228 }
6229
6230
6231 \f
6232 /***********************************************************************
6233 Changing an iterator's position
6234 ***********************************************************************/
6235
6236 /* Change IT's current position to POS in current_buffer. If FORCE_P
6237 is non-zero, always check for text properties at the new position.
6238 Otherwise, text properties are only looked up if POS >=
6239 IT->check_charpos of a property. */
6240
6241 static void
6242 reseat (struct it *it, struct text_pos pos, int force_p)
6243 {
6244 ptrdiff_t original_pos = IT_CHARPOS (*it);
6245
6246 reseat_1 (it, pos, 0);
6247
6248 /* Determine where to check text properties. Avoid doing it
6249 where possible because text property lookup is very expensive. */
6250 if (force_p
6251 || CHARPOS (pos) > it->stop_charpos
6252 || CHARPOS (pos) < original_pos)
6253 {
6254 if (it->bidi_p)
6255 {
6256 /* For bidi iteration, we need to prime prev_stop and
6257 base_level_stop with our best estimations. */
6258 /* Implementation note: Of course, POS is not necessarily a
6259 stop position, so assigning prev_pos to it is a lie; we
6260 should have called compute_stop_backwards. However, if
6261 the current buffer does not include any R2L characters,
6262 that call would be a waste of cycles, because the
6263 iterator will never move back, and thus never cross this
6264 "fake" stop position. So we delay that backward search
6265 until the time we really need it, in next_element_from_buffer. */
6266 if (CHARPOS (pos) != it->prev_stop)
6267 it->prev_stop = CHARPOS (pos);
6268 if (CHARPOS (pos) < it->base_level_stop)
6269 it->base_level_stop = 0; /* meaning it's unknown */
6270 handle_stop (it);
6271 }
6272 else
6273 {
6274 handle_stop (it);
6275 it->prev_stop = it->base_level_stop = 0;
6276 }
6277
6278 }
6279
6280 CHECK_IT (it);
6281 }
6282
6283
6284 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6285 IT->stop_pos to POS, also. */
6286
6287 static void
6288 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6289 {
6290 /* Don't call this function when scanning a C string. */
6291 eassert (it->s == NULL);
6292
6293 /* POS must be a reasonable value. */
6294 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6295
6296 it->current.pos = it->position = pos;
6297 it->end_charpos = ZV;
6298 it->dpvec = NULL;
6299 it->current.dpvec_index = -1;
6300 it->current.overlay_string_index = -1;
6301 IT_STRING_CHARPOS (*it) = -1;
6302 IT_STRING_BYTEPOS (*it) = -1;
6303 it->string = Qnil;
6304 it->method = GET_FROM_BUFFER;
6305 it->object = it->w->contents;
6306 it->area = TEXT_AREA;
6307 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6308 it->sp = 0;
6309 it->string_from_display_prop_p = 0;
6310 it->string_from_prefix_prop_p = 0;
6311
6312 it->from_disp_prop_p = 0;
6313 it->face_before_selective_p = 0;
6314 if (it->bidi_p)
6315 {
6316 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6317 &it->bidi_it);
6318 bidi_unshelve_cache (NULL, 0);
6319 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6320 it->bidi_it.string.s = NULL;
6321 it->bidi_it.string.lstring = Qnil;
6322 it->bidi_it.string.bufpos = 0;
6323 it->bidi_it.string.unibyte = 0;
6324 }
6325
6326 if (set_stop_p)
6327 {
6328 it->stop_charpos = CHARPOS (pos);
6329 it->base_level_stop = CHARPOS (pos);
6330 }
6331 /* This make the information stored in it->cmp_it invalidate. */
6332 it->cmp_it.id = -1;
6333 }
6334
6335
6336 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6337 If S is non-null, it is a C string to iterate over. Otherwise,
6338 STRING gives a Lisp string to iterate over.
6339
6340 If PRECISION > 0, don't return more then PRECISION number of
6341 characters from the string.
6342
6343 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6344 characters have been returned. FIELD_WIDTH < 0 means an infinite
6345 field width.
6346
6347 MULTIBYTE = 0 means disable processing of multibyte characters,
6348 MULTIBYTE > 0 means enable it,
6349 MULTIBYTE < 0 means use IT->multibyte_p.
6350
6351 IT must be initialized via a prior call to init_iterator before
6352 calling this function. */
6353
6354 static void
6355 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6356 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6357 int multibyte)
6358 {
6359 /* No region in strings. */
6360 it->region_beg_charpos = it->region_end_charpos = -1;
6361
6362 /* No text property checks performed by default, but see below. */
6363 it->stop_charpos = -1;
6364
6365 /* Set iterator position and end position. */
6366 memset (&it->current, 0, sizeof it->current);
6367 it->current.overlay_string_index = -1;
6368 it->current.dpvec_index = -1;
6369 eassert (charpos >= 0);
6370
6371 /* If STRING is specified, use its multibyteness, otherwise use the
6372 setting of MULTIBYTE, if specified. */
6373 if (multibyte >= 0)
6374 it->multibyte_p = multibyte > 0;
6375
6376 /* Bidirectional reordering of strings is controlled by the default
6377 value of bidi-display-reordering. Don't try to reorder while
6378 loading loadup.el, as the necessary character property tables are
6379 not yet available. */
6380 it->bidi_p =
6381 NILP (Vpurify_flag)
6382 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6383
6384 if (s == NULL)
6385 {
6386 eassert (STRINGP (string));
6387 it->string = string;
6388 it->s = NULL;
6389 it->end_charpos = it->string_nchars = SCHARS (string);
6390 it->method = GET_FROM_STRING;
6391 it->current.string_pos = string_pos (charpos, string);
6392
6393 if (it->bidi_p)
6394 {
6395 it->bidi_it.string.lstring = string;
6396 it->bidi_it.string.s = NULL;
6397 it->bidi_it.string.schars = it->end_charpos;
6398 it->bidi_it.string.bufpos = 0;
6399 it->bidi_it.string.from_disp_str = 0;
6400 it->bidi_it.string.unibyte = !it->multibyte_p;
6401 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6402 FRAME_WINDOW_P (it->f), &it->bidi_it);
6403 }
6404 }
6405 else
6406 {
6407 it->s = (const unsigned char *) s;
6408 it->string = Qnil;
6409
6410 /* Note that we use IT->current.pos, not it->current.string_pos,
6411 for displaying C strings. */
6412 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6413 if (it->multibyte_p)
6414 {
6415 it->current.pos = c_string_pos (charpos, s, 1);
6416 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6417 }
6418 else
6419 {
6420 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6421 it->end_charpos = it->string_nchars = strlen (s);
6422 }
6423
6424 if (it->bidi_p)
6425 {
6426 it->bidi_it.string.lstring = Qnil;
6427 it->bidi_it.string.s = (const unsigned char *) s;
6428 it->bidi_it.string.schars = it->end_charpos;
6429 it->bidi_it.string.bufpos = 0;
6430 it->bidi_it.string.from_disp_str = 0;
6431 it->bidi_it.string.unibyte = !it->multibyte_p;
6432 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6433 &it->bidi_it);
6434 }
6435 it->method = GET_FROM_C_STRING;
6436 }
6437
6438 /* PRECISION > 0 means don't return more than PRECISION characters
6439 from the string. */
6440 if (precision > 0 && it->end_charpos - charpos > precision)
6441 {
6442 it->end_charpos = it->string_nchars = charpos + precision;
6443 if (it->bidi_p)
6444 it->bidi_it.string.schars = it->end_charpos;
6445 }
6446
6447 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6448 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6449 FIELD_WIDTH < 0 means infinite field width. This is useful for
6450 padding with `-' at the end of a mode line. */
6451 if (field_width < 0)
6452 field_width = INFINITY;
6453 /* Implementation note: We deliberately don't enlarge
6454 it->bidi_it.string.schars here to fit it->end_charpos, because
6455 the bidi iterator cannot produce characters out of thin air. */
6456 if (field_width > it->end_charpos - charpos)
6457 it->end_charpos = charpos + field_width;
6458
6459 /* Use the standard display table for displaying strings. */
6460 if (DISP_TABLE_P (Vstandard_display_table))
6461 it->dp = XCHAR_TABLE (Vstandard_display_table);
6462
6463 it->stop_charpos = charpos;
6464 it->prev_stop = charpos;
6465 it->base_level_stop = 0;
6466 if (it->bidi_p)
6467 {
6468 it->bidi_it.first_elt = 1;
6469 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6470 it->bidi_it.disp_pos = -1;
6471 }
6472 if (s == NULL && it->multibyte_p)
6473 {
6474 ptrdiff_t endpos = SCHARS (it->string);
6475 if (endpos > it->end_charpos)
6476 endpos = it->end_charpos;
6477 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6478 it->string);
6479 }
6480 CHECK_IT (it);
6481 }
6482
6483
6484 \f
6485 /***********************************************************************
6486 Iteration
6487 ***********************************************************************/
6488
6489 /* Map enum it_method value to corresponding next_element_from_* function. */
6490
6491 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6492 {
6493 next_element_from_buffer,
6494 next_element_from_display_vector,
6495 next_element_from_string,
6496 next_element_from_c_string,
6497 next_element_from_image,
6498 next_element_from_stretch
6499 };
6500
6501 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6502
6503
6504 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6505 (possibly with the following characters). */
6506
6507 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6508 ((IT)->cmp_it.id >= 0 \
6509 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6510 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6511 END_CHARPOS, (IT)->w, \
6512 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6513 (IT)->string)))
6514
6515
6516 /* Lookup the char-table Vglyphless_char_display for character C (-1
6517 if we want information for no-font case), and return the display
6518 method symbol. By side-effect, update it->what and
6519 it->glyphless_method. This function is called from
6520 get_next_display_element for each character element, and from
6521 x_produce_glyphs when no suitable font was found. */
6522
6523 Lisp_Object
6524 lookup_glyphless_char_display (int c, struct it *it)
6525 {
6526 Lisp_Object glyphless_method = Qnil;
6527
6528 if (CHAR_TABLE_P (Vglyphless_char_display)
6529 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6530 {
6531 if (c >= 0)
6532 {
6533 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6534 if (CONSP (glyphless_method))
6535 glyphless_method = FRAME_WINDOW_P (it->f)
6536 ? XCAR (glyphless_method)
6537 : XCDR (glyphless_method);
6538 }
6539 else
6540 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6541 }
6542
6543 retry:
6544 if (NILP (glyphless_method))
6545 {
6546 if (c >= 0)
6547 /* The default is to display the character by a proper font. */
6548 return Qnil;
6549 /* The default for the no-font case is to display an empty box. */
6550 glyphless_method = Qempty_box;
6551 }
6552 if (EQ (glyphless_method, Qzero_width))
6553 {
6554 if (c >= 0)
6555 return glyphless_method;
6556 /* This method can't be used for the no-font case. */
6557 glyphless_method = Qempty_box;
6558 }
6559 if (EQ (glyphless_method, Qthin_space))
6560 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6561 else if (EQ (glyphless_method, Qempty_box))
6562 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6563 else if (EQ (glyphless_method, Qhex_code))
6564 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6565 else if (STRINGP (glyphless_method))
6566 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6567 else
6568 {
6569 /* Invalid value. We use the default method. */
6570 glyphless_method = Qnil;
6571 goto retry;
6572 }
6573 it->what = IT_GLYPHLESS;
6574 return glyphless_method;
6575 }
6576
6577 /* Load IT's display element fields with information about the next
6578 display element from the current position of IT. Value is zero if
6579 end of buffer (or C string) is reached. */
6580
6581 static struct frame *last_escape_glyph_frame = NULL;
6582 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6583 static int last_escape_glyph_merged_face_id = 0;
6584
6585 struct frame *last_glyphless_glyph_frame = NULL;
6586 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6587 int last_glyphless_glyph_merged_face_id = 0;
6588
6589 static int
6590 get_next_display_element (struct it *it)
6591 {
6592 /* Non-zero means that we found a display element. Zero means that
6593 we hit the end of what we iterate over. Performance note: the
6594 function pointer `method' used here turns out to be faster than
6595 using a sequence of if-statements. */
6596 int success_p;
6597
6598 get_next:
6599 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6600
6601 if (it->what == IT_CHARACTER)
6602 {
6603 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6604 and only if (a) the resolved directionality of that character
6605 is R..." */
6606 /* FIXME: Do we need an exception for characters from display
6607 tables? */
6608 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6609 it->c = bidi_mirror_char (it->c);
6610 /* Map via display table or translate control characters.
6611 IT->c, IT->len etc. have been set to the next character by
6612 the function call above. If we have a display table, and it
6613 contains an entry for IT->c, translate it. Don't do this if
6614 IT->c itself comes from a display table, otherwise we could
6615 end up in an infinite recursion. (An alternative could be to
6616 count the recursion depth of this function and signal an
6617 error when a certain maximum depth is reached.) Is it worth
6618 it? */
6619 if (success_p && it->dpvec == NULL)
6620 {
6621 Lisp_Object dv;
6622 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6623 int nonascii_space_p = 0;
6624 int nonascii_hyphen_p = 0;
6625 int c = it->c; /* This is the character to display. */
6626
6627 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6628 {
6629 eassert (SINGLE_BYTE_CHAR_P (c));
6630 if (unibyte_display_via_language_environment)
6631 {
6632 c = DECODE_CHAR (unibyte, c);
6633 if (c < 0)
6634 c = BYTE8_TO_CHAR (it->c);
6635 }
6636 else
6637 c = BYTE8_TO_CHAR (it->c);
6638 }
6639
6640 if (it->dp
6641 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6642 VECTORP (dv)))
6643 {
6644 struct Lisp_Vector *v = XVECTOR (dv);
6645
6646 /* Return the first character from the display table
6647 entry, if not empty. If empty, don't display the
6648 current character. */
6649 if (v->header.size)
6650 {
6651 it->dpvec_char_len = it->len;
6652 it->dpvec = v->contents;
6653 it->dpend = v->contents + v->header.size;
6654 it->current.dpvec_index = 0;
6655 it->dpvec_face_id = -1;
6656 it->saved_face_id = it->face_id;
6657 it->method = GET_FROM_DISPLAY_VECTOR;
6658 it->ellipsis_p = 0;
6659 }
6660 else
6661 {
6662 set_iterator_to_next (it, 0);
6663 }
6664 goto get_next;
6665 }
6666
6667 if (! NILP (lookup_glyphless_char_display (c, it)))
6668 {
6669 if (it->what == IT_GLYPHLESS)
6670 goto done;
6671 /* Don't display this character. */
6672 set_iterator_to_next (it, 0);
6673 goto get_next;
6674 }
6675
6676 /* If `nobreak-char-display' is non-nil, we display
6677 non-ASCII spaces and hyphens specially. */
6678 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6679 {
6680 if (c == 0xA0)
6681 nonascii_space_p = 1;
6682 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6683 nonascii_hyphen_p = 1;
6684 }
6685
6686 /* Translate control characters into `\003' or `^C' form.
6687 Control characters coming from a display table entry are
6688 currently not translated because we use IT->dpvec to hold
6689 the translation. This could easily be changed but I
6690 don't believe that it is worth doing.
6691
6692 The characters handled by `nobreak-char-display' must be
6693 translated too.
6694
6695 Non-printable characters and raw-byte characters are also
6696 translated to octal form. */
6697 if (((c < ' ' || c == 127) /* ASCII control chars */
6698 ? (it->area != TEXT_AREA
6699 /* In mode line, treat \n, \t like other crl chars. */
6700 || (c != '\t'
6701 && it->glyph_row
6702 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6703 || (c != '\n' && c != '\t'))
6704 : (nonascii_space_p
6705 || nonascii_hyphen_p
6706 || CHAR_BYTE8_P (c)
6707 || ! CHAR_PRINTABLE_P (c))))
6708 {
6709 /* C is a control character, non-ASCII space/hyphen,
6710 raw-byte, or a non-printable character which must be
6711 displayed either as '\003' or as `^C' where the '\\'
6712 and '^' can be defined in the display table. Fill
6713 IT->ctl_chars with glyphs for what we have to
6714 display. Then, set IT->dpvec to these glyphs. */
6715 Lisp_Object gc;
6716 int ctl_len;
6717 int face_id;
6718 int lface_id = 0;
6719 int escape_glyph;
6720
6721 /* Handle control characters with ^. */
6722
6723 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6724 {
6725 int g;
6726
6727 g = '^'; /* default glyph for Control */
6728 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6729 if (it->dp
6730 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6731 {
6732 g = GLYPH_CODE_CHAR (gc);
6733 lface_id = GLYPH_CODE_FACE (gc);
6734 }
6735 if (lface_id)
6736 {
6737 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6738 }
6739 else if (it->f == last_escape_glyph_frame
6740 && it->face_id == last_escape_glyph_face_id)
6741 {
6742 face_id = last_escape_glyph_merged_face_id;
6743 }
6744 else
6745 {
6746 /* Merge the escape-glyph face into the current face. */
6747 face_id = merge_faces (it->f, Qescape_glyph, 0,
6748 it->face_id);
6749 last_escape_glyph_frame = it->f;
6750 last_escape_glyph_face_id = it->face_id;
6751 last_escape_glyph_merged_face_id = face_id;
6752 }
6753
6754 XSETINT (it->ctl_chars[0], g);
6755 XSETINT (it->ctl_chars[1], c ^ 0100);
6756 ctl_len = 2;
6757 goto display_control;
6758 }
6759
6760 /* Handle non-ascii space in the mode where it only gets
6761 highlighting. */
6762
6763 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6764 {
6765 /* Merge `nobreak-space' into the current face. */
6766 face_id = merge_faces (it->f, Qnobreak_space, 0,
6767 it->face_id);
6768 XSETINT (it->ctl_chars[0], ' ');
6769 ctl_len = 1;
6770 goto display_control;
6771 }
6772
6773 /* Handle sequences that start with the "escape glyph". */
6774
6775 /* the default escape glyph is \. */
6776 escape_glyph = '\\';
6777
6778 if (it->dp
6779 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6780 {
6781 escape_glyph = GLYPH_CODE_CHAR (gc);
6782 lface_id = GLYPH_CODE_FACE (gc);
6783 }
6784 if (lface_id)
6785 {
6786 /* The display table specified a face.
6787 Merge it into face_id and also into escape_glyph. */
6788 face_id = merge_faces (it->f, Qt, lface_id,
6789 it->face_id);
6790 }
6791 else if (it->f == last_escape_glyph_frame
6792 && it->face_id == last_escape_glyph_face_id)
6793 {
6794 face_id = last_escape_glyph_merged_face_id;
6795 }
6796 else
6797 {
6798 /* Merge the escape-glyph face into the current face. */
6799 face_id = merge_faces (it->f, Qescape_glyph, 0,
6800 it->face_id);
6801 last_escape_glyph_frame = it->f;
6802 last_escape_glyph_face_id = it->face_id;
6803 last_escape_glyph_merged_face_id = face_id;
6804 }
6805
6806 /* Draw non-ASCII hyphen with just highlighting: */
6807
6808 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6809 {
6810 XSETINT (it->ctl_chars[0], '-');
6811 ctl_len = 1;
6812 goto display_control;
6813 }
6814
6815 /* Draw non-ASCII space/hyphen with escape glyph: */
6816
6817 if (nonascii_space_p || nonascii_hyphen_p)
6818 {
6819 XSETINT (it->ctl_chars[0], escape_glyph);
6820 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6821 ctl_len = 2;
6822 goto display_control;
6823 }
6824
6825 {
6826 char str[10];
6827 int len, i;
6828
6829 if (CHAR_BYTE8_P (c))
6830 /* Display \200 instead of \17777600. */
6831 c = CHAR_TO_BYTE8 (c);
6832 len = sprintf (str, "%03o", c);
6833
6834 XSETINT (it->ctl_chars[0], escape_glyph);
6835 for (i = 0; i < len; i++)
6836 XSETINT (it->ctl_chars[i + 1], str[i]);
6837 ctl_len = len + 1;
6838 }
6839
6840 display_control:
6841 /* Set up IT->dpvec and return first character from it. */
6842 it->dpvec_char_len = it->len;
6843 it->dpvec = it->ctl_chars;
6844 it->dpend = it->dpvec + ctl_len;
6845 it->current.dpvec_index = 0;
6846 it->dpvec_face_id = face_id;
6847 it->saved_face_id = it->face_id;
6848 it->method = GET_FROM_DISPLAY_VECTOR;
6849 it->ellipsis_p = 0;
6850 goto get_next;
6851 }
6852 it->char_to_display = c;
6853 }
6854 else if (success_p)
6855 {
6856 it->char_to_display = it->c;
6857 }
6858 }
6859
6860 /* Adjust face id for a multibyte character. There are no multibyte
6861 character in unibyte text. */
6862 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6863 && it->multibyte_p
6864 && success_p
6865 && FRAME_WINDOW_P (it->f))
6866 {
6867 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6868
6869 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6870 {
6871 /* Automatic composition with glyph-string. */
6872 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6873
6874 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6875 }
6876 else
6877 {
6878 ptrdiff_t pos = (it->s ? -1
6879 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6880 : IT_CHARPOS (*it));
6881 int c;
6882
6883 if (it->what == IT_CHARACTER)
6884 c = it->char_to_display;
6885 else
6886 {
6887 struct composition *cmp = composition_table[it->cmp_it.id];
6888 int i;
6889
6890 c = ' ';
6891 for (i = 0; i < cmp->glyph_len; i++)
6892 /* TAB in a composition means display glyphs with
6893 padding space on the left or right. */
6894 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6895 break;
6896 }
6897 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6898 }
6899 }
6900
6901 done:
6902 /* Is this character the last one of a run of characters with
6903 box? If yes, set IT->end_of_box_run_p to 1. */
6904 if (it->face_box_p
6905 && it->s == NULL)
6906 {
6907 if (it->method == GET_FROM_STRING && it->sp)
6908 {
6909 int face_id = underlying_face_id (it);
6910 struct face *face = FACE_FROM_ID (it->f, face_id);
6911
6912 if (face)
6913 {
6914 if (face->box == FACE_NO_BOX)
6915 {
6916 /* If the box comes from face properties in a
6917 display string, check faces in that string. */
6918 int string_face_id = face_after_it_pos (it);
6919 it->end_of_box_run_p
6920 = (FACE_FROM_ID (it->f, string_face_id)->box
6921 == FACE_NO_BOX);
6922 }
6923 /* Otherwise, the box comes from the underlying face.
6924 If this is the last string character displayed, check
6925 the next buffer location. */
6926 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6927 && (it->current.overlay_string_index
6928 == it->n_overlay_strings - 1))
6929 {
6930 ptrdiff_t ignore;
6931 int next_face_id;
6932 struct text_pos pos = it->current.pos;
6933 INC_TEXT_POS (pos, it->multibyte_p);
6934
6935 next_face_id = face_at_buffer_position
6936 (it->w, CHARPOS (pos), it->region_beg_charpos,
6937 it->region_end_charpos, &ignore,
6938 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6939 -1);
6940 it->end_of_box_run_p
6941 = (FACE_FROM_ID (it->f, next_face_id)->box
6942 == FACE_NO_BOX);
6943 }
6944 }
6945 }
6946 else
6947 {
6948 int face_id = face_after_it_pos (it);
6949 it->end_of_box_run_p
6950 = (face_id != it->face_id
6951 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6952 }
6953 }
6954 /* If we reached the end of the object we've been iterating (e.g., a
6955 display string or an overlay string), and there's something on
6956 IT->stack, proceed with what's on the stack. It doesn't make
6957 sense to return zero if there's unprocessed stuff on the stack,
6958 because otherwise that stuff will never be displayed. */
6959 if (!success_p && it->sp > 0)
6960 {
6961 set_iterator_to_next (it, 0);
6962 success_p = get_next_display_element (it);
6963 }
6964
6965 /* Value is 0 if end of buffer or string reached. */
6966 return success_p;
6967 }
6968
6969
6970 /* Move IT to the next display element.
6971
6972 RESEAT_P non-zero means if called on a newline in buffer text,
6973 skip to the next visible line start.
6974
6975 Functions get_next_display_element and set_iterator_to_next are
6976 separate because I find this arrangement easier to handle than a
6977 get_next_display_element function that also increments IT's
6978 position. The way it is we can first look at an iterator's current
6979 display element, decide whether it fits on a line, and if it does,
6980 increment the iterator position. The other way around we probably
6981 would either need a flag indicating whether the iterator has to be
6982 incremented the next time, or we would have to implement a
6983 decrement position function which would not be easy to write. */
6984
6985 void
6986 set_iterator_to_next (struct it *it, int reseat_p)
6987 {
6988 /* Reset flags indicating start and end of a sequence of characters
6989 with box. Reset them at the start of this function because
6990 moving the iterator to a new position might set them. */
6991 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6992
6993 switch (it->method)
6994 {
6995 case GET_FROM_BUFFER:
6996 /* The current display element of IT is a character from
6997 current_buffer. Advance in the buffer, and maybe skip over
6998 invisible lines that are so because of selective display. */
6999 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7000 reseat_at_next_visible_line_start (it, 0);
7001 else if (it->cmp_it.id >= 0)
7002 {
7003 /* We are currently getting glyphs from a composition. */
7004 int i;
7005
7006 if (! it->bidi_p)
7007 {
7008 IT_CHARPOS (*it) += it->cmp_it.nchars;
7009 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7010 if (it->cmp_it.to < it->cmp_it.nglyphs)
7011 {
7012 it->cmp_it.from = it->cmp_it.to;
7013 }
7014 else
7015 {
7016 it->cmp_it.id = -1;
7017 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7018 IT_BYTEPOS (*it),
7019 it->end_charpos, Qnil);
7020 }
7021 }
7022 else if (! it->cmp_it.reversed_p)
7023 {
7024 /* Composition created while scanning forward. */
7025 /* Update IT's char/byte positions to point to the first
7026 character of the next grapheme cluster, or to the
7027 character visually after the current composition. */
7028 for (i = 0; i < it->cmp_it.nchars; i++)
7029 bidi_move_to_visually_next (&it->bidi_it);
7030 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7031 IT_CHARPOS (*it) = it->bidi_it.charpos;
7032
7033 if (it->cmp_it.to < it->cmp_it.nglyphs)
7034 {
7035 /* Proceed to the next grapheme cluster. */
7036 it->cmp_it.from = it->cmp_it.to;
7037 }
7038 else
7039 {
7040 /* No more grapheme clusters in this composition.
7041 Find the next stop position. */
7042 ptrdiff_t stop = it->end_charpos;
7043 if (it->bidi_it.scan_dir < 0)
7044 /* Now we are scanning backward and don't know
7045 where to stop. */
7046 stop = -1;
7047 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7048 IT_BYTEPOS (*it), stop, Qnil);
7049 }
7050 }
7051 else
7052 {
7053 /* Composition created while scanning backward. */
7054 /* Update IT's char/byte positions to point to the last
7055 character of the previous grapheme cluster, or the
7056 character visually after the current composition. */
7057 for (i = 0; i < it->cmp_it.nchars; i++)
7058 bidi_move_to_visually_next (&it->bidi_it);
7059 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7060 IT_CHARPOS (*it) = it->bidi_it.charpos;
7061 if (it->cmp_it.from > 0)
7062 {
7063 /* Proceed to the previous grapheme cluster. */
7064 it->cmp_it.to = it->cmp_it.from;
7065 }
7066 else
7067 {
7068 /* No more grapheme clusters in this composition.
7069 Find the next stop position. */
7070 ptrdiff_t stop = it->end_charpos;
7071 if (it->bidi_it.scan_dir < 0)
7072 /* Now we are scanning backward and don't know
7073 where to stop. */
7074 stop = -1;
7075 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7076 IT_BYTEPOS (*it), stop, Qnil);
7077 }
7078 }
7079 }
7080 else
7081 {
7082 eassert (it->len != 0);
7083
7084 if (!it->bidi_p)
7085 {
7086 IT_BYTEPOS (*it) += it->len;
7087 IT_CHARPOS (*it) += 1;
7088 }
7089 else
7090 {
7091 int prev_scan_dir = it->bidi_it.scan_dir;
7092 /* If this is a new paragraph, determine its base
7093 direction (a.k.a. its base embedding level). */
7094 if (it->bidi_it.new_paragraph)
7095 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7096 bidi_move_to_visually_next (&it->bidi_it);
7097 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7098 IT_CHARPOS (*it) = it->bidi_it.charpos;
7099 if (prev_scan_dir != it->bidi_it.scan_dir)
7100 {
7101 /* As the scan direction was changed, we must
7102 re-compute the stop position for composition. */
7103 ptrdiff_t stop = it->end_charpos;
7104 if (it->bidi_it.scan_dir < 0)
7105 stop = -1;
7106 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7107 IT_BYTEPOS (*it), stop, Qnil);
7108 }
7109 }
7110 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7111 }
7112 break;
7113
7114 case GET_FROM_C_STRING:
7115 /* Current display element of IT is from a C string. */
7116 if (!it->bidi_p
7117 /* If the string position is beyond string's end, it means
7118 next_element_from_c_string is padding the string with
7119 blanks, in which case we bypass the bidi iterator,
7120 because it cannot deal with such virtual characters. */
7121 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7122 {
7123 IT_BYTEPOS (*it) += it->len;
7124 IT_CHARPOS (*it) += 1;
7125 }
7126 else
7127 {
7128 bidi_move_to_visually_next (&it->bidi_it);
7129 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7130 IT_CHARPOS (*it) = it->bidi_it.charpos;
7131 }
7132 break;
7133
7134 case GET_FROM_DISPLAY_VECTOR:
7135 /* Current display element of IT is from a display table entry.
7136 Advance in the display table definition. Reset it to null if
7137 end reached, and continue with characters from buffers/
7138 strings. */
7139 ++it->current.dpvec_index;
7140
7141 /* Restore face of the iterator to what they were before the
7142 display vector entry (these entries may contain faces). */
7143 it->face_id = it->saved_face_id;
7144
7145 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7146 {
7147 int recheck_faces = it->ellipsis_p;
7148
7149 if (it->s)
7150 it->method = GET_FROM_C_STRING;
7151 else if (STRINGP (it->string))
7152 it->method = GET_FROM_STRING;
7153 else
7154 {
7155 it->method = GET_FROM_BUFFER;
7156 it->object = it->w->contents;
7157 }
7158
7159 it->dpvec = NULL;
7160 it->current.dpvec_index = -1;
7161
7162 /* Skip over characters which were displayed via IT->dpvec. */
7163 if (it->dpvec_char_len < 0)
7164 reseat_at_next_visible_line_start (it, 1);
7165 else if (it->dpvec_char_len > 0)
7166 {
7167 if (it->method == GET_FROM_STRING
7168 && it->current.overlay_string_index >= 0
7169 && it->n_overlay_strings > 0)
7170 it->ignore_overlay_strings_at_pos_p = 1;
7171 it->len = it->dpvec_char_len;
7172 set_iterator_to_next (it, reseat_p);
7173 }
7174
7175 /* Maybe recheck faces after display vector */
7176 if (recheck_faces)
7177 it->stop_charpos = IT_CHARPOS (*it);
7178 }
7179 break;
7180
7181 case GET_FROM_STRING:
7182 /* Current display element is a character from a Lisp string. */
7183 eassert (it->s == NULL && STRINGP (it->string));
7184 /* Don't advance past string end. These conditions are true
7185 when set_iterator_to_next is called at the end of
7186 get_next_display_element, in which case the Lisp string is
7187 already exhausted, and all we want is pop the iterator
7188 stack. */
7189 if (it->current.overlay_string_index >= 0)
7190 {
7191 /* This is an overlay string, so there's no padding with
7192 spaces, and the number of characters in the string is
7193 where the string ends. */
7194 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7195 goto consider_string_end;
7196 }
7197 else
7198 {
7199 /* Not an overlay string. There could be padding, so test
7200 against it->end_charpos . */
7201 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7202 goto consider_string_end;
7203 }
7204 if (it->cmp_it.id >= 0)
7205 {
7206 int i;
7207
7208 if (! it->bidi_p)
7209 {
7210 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7211 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7212 if (it->cmp_it.to < it->cmp_it.nglyphs)
7213 it->cmp_it.from = it->cmp_it.to;
7214 else
7215 {
7216 it->cmp_it.id = -1;
7217 composition_compute_stop_pos (&it->cmp_it,
7218 IT_STRING_CHARPOS (*it),
7219 IT_STRING_BYTEPOS (*it),
7220 it->end_charpos, it->string);
7221 }
7222 }
7223 else if (! it->cmp_it.reversed_p)
7224 {
7225 for (i = 0; i < it->cmp_it.nchars; i++)
7226 bidi_move_to_visually_next (&it->bidi_it);
7227 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7228 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7229
7230 if (it->cmp_it.to < it->cmp_it.nglyphs)
7231 it->cmp_it.from = it->cmp_it.to;
7232 else
7233 {
7234 ptrdiff_t stop = it->end_charpos;
7235 if (it->bidi_it.scan_dir < 0)
7236 stop = -1;
7237 composition_compute_stop_pos (&it->cmp_it,
7238 IT_STRING_CHARPOS (*it),
7239 IT_STRING_BYTEPOS (*it), stop,
7240 it->string);
7241 }
7242 }
7243 else
7244 {
7245 for (i = 0; i < it->cmp_it.nchars; i++)
7246 bidi_move_to_visually_next (&it->bidi_it);
7247 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7248 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7249 if (it->cmp_it.from > 0)
7250 it->cmp_it.to = it->cmp_it.from;
7251 else
7252 {
7253 ptrdiff_t stop = it->end_charpos;
7254 if (it->bidi_it.scan_dir < 0)
7255 stop = -1;
7256 composition_compute_stop_pos (&it->cmp_it,
7257 IT_STRING_CHARPOS (*it),
7258 IT_STRING_BYTEPOS (*it), stop,
7259 it->string);
7260 }
7261 }
7262 }
7263 else
7264 {
7265 if (!it->bidi_p
7266 /* If the string position is beyond string's end, it
7267 means next_element_from_string is padding the string
7268 with blanks, in which case we bypass the bidi
7269 iterator, because it cannot deal with such virtual
7270 characters. */
7271 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7272 {
7273 IT_STRING_BYTEPOS (*it) += it->len;
7274 IT_STRING_CHARPOS (*it) += 1;
7275 }
7276 else
7277 {
7278 int prev_scan_dir = it->bidi_it.scan_dir;
7279
7280 bidi_move_to_visually_next (&it->bidi_it);
7281 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7282 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7283 if (prev_scan_dir != it->bidi_it.scan_dir)
7284 {
7285 ptrdiff_t stop = it->end_charpos;
7286
7287 if (it->bidi_it.scan_dir < 0)
7288 stop = -1;
7289 composition_compute_stop_pos (&it->cmp_it,
7290 IT_STRING_CHARPOS (*it),
7291 IT_STRING_BYTEPOS (*it), stop,
7292 it->string);
7293 }
7294 }
7295 }
7296
7297 consider_string_end:
7298
7299 if (it->current.overlay_string_index >= 0)
7300 {
7301 /* IT->string is an overlay string. Advance to the
7302 next, if there is one. */
7303 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7304 {
7305 it->ellipsis_p = 0;
7306 next_overlay_string (it);
7307 if (it->ellipsis_p)
7308 setup_for_ellipsis (it, 0);
7309 }
7310 }
7311 else
7312 {
7313 /* IT->string is not an overlay string. If we reached
7314 its end, and there is something on IT->stack, proceed
7315 with what is on the stack. This can be either another
7316 string, this time an overlay string, or a buffer. */
7317 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7318 && it->sp > 0)
7319 {
7320 pop_it (it);
7321 if (it->method == GET_FROM_STRING)
7322 goto consider_string_end;
7323 }
7324 }
7325 break;
7326
7327 case GET_FROM_IMAGE:
7328 case GET_FROM_STRETCH:
7329 /* The position etc with which we have to proceed are on
7330 the stack. The position may be at the end of a string,
7331 if the `display' property takes up the whole string. */
7332 eassert (it->sp > 0);
7333 pop_it (it);
7334 if (it->method == GET_FROM_STRING)
7335 goto consider_string_end;
7336 break;
7337
7338 default:
7339 /* There are no other methods defined, so this should be a bug. */
7340 emacs_abort ();
7341 }
7342
7343 eassert (it->method != GET_FROM_STRING
7344 || (STRINGP (it->string)
7345 && IT_STRING_CHARPOS (*it) >= 0));
7346 }
7347
7348 /* Load IT's display element fields with information about the next
7349 display element which comes from a display table entry or from the
7350 result of translating a control character to one of the forms `^C'
7351 or `\003'.
7352
7353 IT->dpvec holds the glyphs to return as characters.
7354 IT->saved_face_id holds the face id before the display vector--it
7355 is restored into IT->face_id in set_iterator_to_next. */
7356
7357 static int
7358 next_element_from_display_vector (struct it *it)
7359 {
7360 Lisp_Object gc;
7361
7362 /* Precondition. */
7363 eassert (it->dpvec && it->current.dpvec_index >= 0);
7364
7365 it->face_id = it->saved_face_id;
7366
7367 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7368 That seemed totally bogus - so I changed it... */
7369 gc = it->dpvec[it->current.dpvec_index];
7370
7371 if (GLYPH_CODE_P (gc))
7372 {
7373 it->c = GLYPH_CODE_CHAR (gc);
7374 it->len = CHAR_BYTES (it->c);
7375
7376 /* The entry may contain a face id to use. Such a face id is
7377 the id of a Lisp face, not a realized face. A face id of
7378 zero means no face is specified. */
7379 if (it->dpvec_face_id >= 0)
7380 it->face_id = it->dpvec_face_id;
7381 else
7382 {
7383 int lface_id = GLYPH_CODE_FACE (gc);
7384 if (lface_id > 0)
7385 it->face_id = merge_faces (it->f, Qt, lface_id,
7386 it->saved_face_id);
7387 }
7388 }
7389 else
7390 /* Display table entry is invalid. Return a space. */
7391 it->c = ' ', it->len = 1;
7392
7393 /* Don't change position and object of the iterator here. They are
7394 still the values of the character that had this display table
7395 entry or was translated, and that's what we want. */
7396 it->what = IT_CHARACTER;
7397 return 1;
7398 }
7399
7400 /* Get the first element of string/buffer in the visual order, after
7401 being reseated to a new position in a string or a buffer. */
7402 static void
7403 get_visually_first_element (struct it *it)
7404 {
7405 int string_p = STRINGP (it->string) || it->s;
7406 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7407 ptrdiff_t bob = (string_p ? 0 : BEGV);
7408
7409 if (STRINGP (it->string))
7410 {
7411 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7412 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7413 }
7414 else
7415 {
7416 it->bidi_it.charpos = IT_CHARPOS (*it);
7417 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7418 }
7419
7420 if (it->bidi_it.charpos == eob)
7421 {
7422 /* Nothing to do, but reset the FIRST_ELT flag, like
7423 bidi_paragraph_init does, because we are not going to
7424 call it. */
7425 it->bidi_it.first_elt = 0;
7426 }
7427 else if (it->bidi_it.charpos == bob
7428 || (!string_p
7429 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7430 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7431 {
7432 /* If we are at the beginning of a line/string, we can produce
7433 the next element right away. */
7434 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7435 bidi_move_to_visually_next (&it->bidi_it);
7436 }
7437 else
7438 {
7439 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7440
7441 /* We need to prime the bidi iterator starting at the line's or
7442 string's beginning, before we will be able to produce the
7443 next element. */
7444 if (string_p)
7445 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7446 else
7447 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7448 IT_BYTEPOS (*it), -1,
7449 &it->bidi_it.bytepos);
7450 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7451 do
7452 {
7453 /* Now return to buffer/string position where we were asked
7454 to get the next display element, and produce that. */
7455 bidi_move_to_visually_next (&it->bidi_it);
7456 }
7457 while (it->bidi_it.bytepos != orig_bytepos
7458 && it->bidi_it.charpos < eob);
7459 }
7460
7461 /* Adjust IT's position information to where we ended up. */
7462 if (STRINGP (it->string))
7463 {
7464 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7465 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7466 }
7467 else
7468 {
7469 IT_CHARPOS (*it) = it->bidi_it.charpos;
7470 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7471 }
7472
7473 if (STRINGP (it->string) || !it->s)
7474 {
7475 ptrdiff_t stop, charpos, bytepos;
7476
7477 if (STRINGP (it->string))
7478 {
7479 eassert (!it->s);
7480 stop = SCHARS (it->string);
7481 if (stop > it->end_charpos)
7482 stop = it->end_charpos;
7483 charpos = IT_STRING_CHARPOS (*it);
7484 bytepos = IT_STRING_BYTEPOS (*it);
7485 }
7486 else
7487 {
7488 stop = it->end_charpos;
7489 charpos = IT_CHARPOS (*it);
7490 bytepos = IT_BYTEPOS (*it);
7491 }
7492 if (it->bidi_it.scan_dir < 0)
7493 stop = -1;
7494 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7495 it->string);
7496 }
7497 }
7498
7499 /* Load IT with the next display element from Lisp string IT->string.
7500 IT->current.string_pos is the current position within the string.
7501 If IT->current.overlay_string_index >= 0, the Lisp string is an
7502 overlay string. */
7503
7504 static int
7505 next_element_from_string (struct it *it)
7506 {
7507 struct text_pos position;
7508
7509 eassert (STRINGP (it->string));
7510 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7511 eassert (IT_STRING_CHARPOS (*it) >= 0);
7512 position = it->current.string_pos;
7513
7514 /* With bidi reordering, the character to display might not be the
7515 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7516 that we were reseat()ed to a new string, whose paragraph
7517 direction is not known. */
7518 if (it->bidi_p && it->bidi_it.first_elt)
7519 {
7520 get_visually_first_element (it);
7521 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7522 }
7523
7524 /* Time to check for invisible text? */
7525 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7526 {
7527 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7528 {
7529 if (!(!it->bidi_p
7530 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7531 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7532 {
7533 /* With bidi non-linear iteration, we could find
7534 ourselves far beyond the last computed stop_charpos,
7535 with several other stop positions in between that we
7536 missed. Scan them all now, in buffer's logical
7537 order, until we find and handle the last stop_charpos
7538 that precedes our current position. */
7539 handle_stop_backwards (it, it->stop_charpos);
7540 return GET_NEXT_DISPLAY_ELEMENT (it);
7541 }
7542 else
7543 {
7544 if (it->bidi_p)
7545 {
7546 /* Take note of the stop position we just moved
7547 across, for when we will move back across it. */
7548 it->prev_stop = it->stop_charpos;
7549 /* If we are at base paragraph embedding level, take
7550 note of the last stop position seen at this
7551 level. */
7552 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7553 it->base_level_stop = it->stop_charpos;
7554 }
7555 handle_stop (it);
7556
7557 /* Since a handler may have changed IT->method, we must
7558 recurse here. */
7559 return GET_NEXT_DISPLAY_ELEMENT (it);
7560 }
7561 }
7562 else if (it->bidi_p
7563 /* If we are before prev_stop, we may have overstepped
7564 on our way backwards a stop_pos, and if so, we need
7565 to handle that stop_pos. */
7566 && IT_STRING_CHARPOS (*it) < it->prev_stop
7567 /* We can sometimes back up for reasons that have nothing
7568 to do with bidi reordering. E.g., compositions. The
7569 code below is only needed when we are above the base
7570 embedding level, so test for that explicitly. */
7571 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7572 {
7573 /* If we lost track of base_level_stop, we have no better
7574 place for handle_stop_backwards to start from than string
7575 beginning. This happens, e.g., when we were reseated to
7576 the previous screenful of text by vertical-motion. */
7577 if (it->base_level_stop <= 0
7578 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7579 it->base_level_stop = 0;
7580 handle_stop_backwards (it, it->base_level_stop);
7581 return GET_NEXT_DISPLAY_ELEMENT (it);
7582 }
7583 }
7584
7585 if (it->current.overlay_string_index >= 0)
7586 {
7587 /* Get the next character from an overlay string. In overlay
7588 strings, there is no field width or padding with spaces to
7589 do. */
7590 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7591 {
7592 it->what = IT_EOB;
7593 return 0;
7594 }
7595 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7596 IT_STRING_BYTEPOS (*it),
7597 it->bidi_it.scan_dir < 0
7598 ? -1
7599 : SCHARS (it->string))
7600 && next_element_from_composition (it))
7601 {
7602 return 1;
7603 }
7604 else if (STRING_MULTIBYTE (it->string))
7605 {
7606 const unsigned char *s = (SDATA (it->string)
7607 + IT_STRING_BYTEPOS (*it));
7608 it->c = string_char_and_length (s, &it->len);
7609 }
7610 else
7611 {
7612 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7613 it->len = 1;
7614 }
7615 }
7616 else
7617 {
7618 /* Get the next character from a Lisp string that is not an
7619 overlay string. Such strings come from the mode line, for
7620 example. We may have to pad with spaces, or truncate the
7621 string. See also next_element_from_c_string. */
7622 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7623 {
7624 it->what = IT_EOB;
7625 return 0;
7626 }
7627 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7628 {
7629 /* Pad with spaces. */
7630 it->c = ' ', it->len = 1;
7631 CHARPOS (position) = BYTEPOS (position) = -1;
7632 }
7633 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7634 IT_STRING_BYTEPOS (*it),
7635 it->bidi_it.scan_dir < 0
7636 ? -1
7637 : it->string_nchars)
7638 && next_element_from_composition (it))
7639 {
7640 return 1;
7641 }
7642 else if (STRING_MULTIBYTE (it->string))
7643 {
7644 const unsigned char *s = (SDATA (it->string)
7645 + IT_STRING_BYTEPOS (*it));
7646 it->c = string_char_and_length (s, &it->len);
7647 }
7648 else
7649 {
7650 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7651 it->len = 1;
7652 }
7653 }
7654
7655 /* Record what we have and where it came from. */
7656 it->what = IT_CHARACTER;
7657 it->object = it->string;
7658 it->position = position;
7659 return 1;
7660 }
7661
7662
7663 /* Load IT with next display element from C string IT->s.
7664 IT->string_nchars is the maximum number of characters to return
7665 from the string. IT->end_charpos may be greater than
7666 IT->string_nchars when this function is called, in which case we
7667 may have to return padding spaces. Value is zero if end of string
7668 reached, including padding spaces. */
7669
7670 static int
7671 next_element_from_c_string (struct it *it)
7672 {
7673 int success_p = 1;
7674
7675 eassert (it->s);
7676 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7677 it->what = IT_CHARACTER;
7678 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7679 it->object = Qnil;
7680
7681 /* With bidi reordering, the character to display might not be the
7682 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7683 we were reseated to a new string, whose paragraph direction is
7684 not known. */
7685 if (it->bidi_p && it->bidi_it.first_elt)
7686 get_visually_first_element (it);
7687
7688 /* IT's position can be greater than IT->string_nchars in case a
7689 field width or precision has been specified when the iterator was
7690 initialized. */
7691 if (IT_CHARPOS (*it) >= it->end_charpos)
7692 {
7693 /* End of the game. */
7694 it->what = IT_EOB;
7695 success_p = 0;
7696 }
7697 else if (IT_CHARPOS (*it) >= it->string_nchars)
7698 {
7699 /* Pad with spaces. */
7700 it->c = ' ', it->len = 1;
7701 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7702 }
7703 else if (it->multibyte_p)
7704 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7705 else
7706 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7707
7708 return success_p;
7709 }
7710
7711
7712 /* Set up IT to return characters from an ellipsis, if appropriate.
7713 The definition of the ellipsis glyphs may come from a display table
7714 entry. This function fills IT with the first glyph from the
7715 ellipsis if an ellipsis is to be displayed. */
7716
7717 static int
7718 next_element_from_ellipsis (struct it *it)
7719 {
7720 if (it->selective_display_ellipsis_p)
7721 setup_for_ellipsis (it, it->len);
7722 else
7723 {
7724 /* The face at the current position may be different from the
7725 face we find after the invisible text. Remember what it
7726 was in IT->saved_face_id, and signal that it's there by
7727 setting face_before_selective_p. */
7728 it->saved_face_id = it->face_id;
7729 it->method = GET_FROM_BUFFER;
7730 it->object = it->w->contents;
7731 reseat_at_next_visible_line_start (it, 1);
7732 it->face_before_selective_p = 1;
7733 }
7734
7735 return GET_NEXT_DISPLAY_ELEMENT (it);
7736 }
7737
7738
7739 /* Deliver an image display element. The iterator IT is already
7740 filled with image information (done in handle_display_prop). Value
7741 is always 1. */
7742
7743
7744 static int
7745 next_element_from_image (struct it *it)
7746 {
7747 it->what = IT_IMAGE;
7748 it->ignore_overlay_strings_at_pos_p = 0;
7749 return 1;
7750 }
7751
7752
7753 /* Fill iterator IT with next display element from a stretch glyph
7754 property. IT->object is the value of the text property. Value is
7755 always 1. */
7756
7757 static int
7758 next_element_from_stretch (struct it *it)
7759 {
7760 it->what = IT_STRETCH;
7761 return 1;
7762 }
7763
7764 /* Scan backwards from IT's current position until we find a stop
7765 position, or until BEGV. This is called when we find ourself
7766 before both the last known prev_stop and base_level_stop while
7767 reordering bidirectional text. */
7768
7769 static void
7770 compute_stop_pos_backwards (struct it *it)
7771 {
7772 const int SCAN_BACK_LIMIT = 1000;
7773 struct text_pos pos;
7774 struct display_pos save_current = it->current;
7775 struct text_pos save_position = it->position;
7776 ptrdiff_t charpos = IT_CHARPOS (*it);
7777 ptrdiff_t where_we_are = charpos;
7778 ptrdiff_t save_stop_pos = it->stop_charpos;
7779 ptrdiff_t save_end_pos = it->end_charpos;
7780
7781 eassert (NILP (it->string) && !it->s);
7782 eassert (it->bidi_p);
7783 it->bidi_p = 0;
7784 do
7785 {
7786 it->end_charpos = min (charpos + 1, ZV);
7787 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7788 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7789 reseat_1 (it, pos, 0);
7790 compute_stop_pos (it);
7791 /* We must advance forward, right? */
7792 if (it->stop_charpos <= charpos)
7793 emacs_abort ();
7794 }
7795 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7796
7797 if (it->stop_charpos <= where_we_are)
7798 it->prev_stop = it->stop_charpos;
7799 else
7800 it->prev_stop = BEGV;
7801 it->bidi_p = 1;
7802 it->current = save_current;
7803 it->position = save_position;
7804 it->stop_charpos = save_stop_pos;
7805 it->end_charpos = save_end_pos;
7806 }
7807
7808 /* Scan forward from CHARPOS in the current buffer/string, until we
7809 find a stop position > current IT's position. Then handle the stop
7810 position before that. This is called when we bump into a stop
7811 position while reordering bidirectional text. CHARPOS should be
7812 the last previously processed stop_pos (or BEGV/0, if none were
7813 processed yet) whose position is less that IT's current
7814 position. */
7815
7816 static void
7817 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7818 {
7819 int bufp = !STRINGP (it->string);
7820 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7821 struct display_pos save_current = it->current;
7822 struct text_pos save_position = it->position;
7823 struct text_pos pos1;
7824 ptrdiff_t next_stop;
7825
7826 /* Scan in strict logical order. */
7827 eassert (it->bidi_p);
7828 it->bidi_p = 0;
7829 do
7830 {
7831 it->prev_stop = charpos;
7832 if (bufp)
7833 {
7834 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7835 reseat_1 (it, pos1, 0);
7836 }
7837 else
7838 it->current.string_pos = string_pos (charpos, it->string);
7839 compute_stop_pos (it);
7840 /* We must advance forward, right? */
7841 if (it->stop_charpos <= it->prev_stop)
7842 emacs_abort ();
7843 charpos = it->stop_charpos;
7844 }
7845 while (charpos <= where_we_are);
7846
7847 it->bidi_p = 1;
7848 it->current = save_current;
7849 it->position = save_position;
7850 next_stop = it->stop_charpos;
7851 it->stop_charpos = it->prev_stop;
7852 handle_stop (it);
7853 it->stop_charpos = next_stop;
7854 }
7855
7856 /* Load IT with the next display element from current_buffer. Value
7857 is zero if end of buffer reached. IT->stop_charpos is the next
7858 position at which to stop and check for text properties or buffer
7859 end. */
7860
7861 static int
7862 next_element_from_buffer (struct it *it)
7863 {
7864 int success_p = 1;
7865
7866 eassert (IT_CHARPOS (*it) >= BEGV);
7867 eassert (NILP (it->string) && !it->s);
7868 eassert (!it->bidi_p
7869 || (EQ (it->bidi_it.string.lstring, Qnil)
7870 && it->bidi_it.string.s == NULL));
7871
7872 /* With bidi reordering, the character to display might not be the
7873 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7874 we were reseat()ed to a new buffer position, which is potentially
7875 a different paragraph. */
7876 if (it->bidi_p && it->bidi_it.first_elt)
7877 {
7878 get_visually_first_element (it);
7879 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7880 }
7881
7882 if (IT_CHARPOS (*it) >= it->stop_charpos)
7883 {
7884 if (IT_CHARPOS (*it) >= it->end_charpos)
7885 {
7886 int overlay_strings_follow_p;
7887
7888 /* End of the game, except when overlay strings follow that
7889 haven't been returned yet. */
7890 if (it->overlay_strings_at_end_processed_p)
7891 overlay_strings_follow_p = 0;
7892 else
7893 {
7894 it->overlay_strings_at_end_processed_p = 1;
7895 overlay_strings_follow_p = get_overlay_strings (it, 0);
7896 }
7897
7898 if (overlay_strings_follow_p)
7899 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7900 else
7901 {
7902 it->what = IT_EOB;
7903 it->position = it->current.pos;
7904 success_p = 0;
7905 }
7906 }
7907 else if (!(!it->bidi_p
7908 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7909 || IT_CHARPOS (*it) == it->stop_charpos))
7910 {
7911 /* With bidi non-linear iteration, we could find ourselves
7912 far beyond the last computed stop_charpos, with several
7913 other stop positions in between that we missed. Scan
7914 them all now, in buffer's logical order, until we find
7915 and handle the last stop_charpos that precedes our
7916 current position. */
7917 handle_stop_backwards (it, it->stop_charpos);
7918 return GET_NEXT_DISPLAY_ELEMENT (it);
7919 }
7920 else
7921 {
7922 if (it->bidi_p)
7923 {
7924 /* Take note of the stop position we just moved across,
7925 for when we will move back across it. */
7926 it->prev_stop = it->stop_charpos;
7927 /* If we are at base paragraph embedding level, take
7928 note of the last stop position seen at this
7929 level. */
7930 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7931 it->base_level_stop = it->stop_charpos;
7932 }
7933 handle_stop (it);
7934 return GET_NEXT_DISPLAY_ELEMENT (it);
7935 }
7936 }
7937 else if (it->bidi_p
7938 /* If we are before prev_stop, we may have overstepped on
7939 our way backwards a stop_pos, and if so, we need to
7940 handle that stop_pos. */
7941 && IT_CHARPOS (*it) < it->prev_stop
7942 /* We can sometimes back up for reasons that have nothing
7943 to do with bidi reordering. E.g., compositions. The
7944 code below is only needed when we are above the base
7945 embedding level, so test for that explicitly. */
7946 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7947 {
7948 if (it->base_level_stop <= 0
7949 || IT_CHARPOS (*it) < it->base_level_stop)
7950 {
7951 /* If we lost track of base_level_stop, we need to find
7952 prev_stop by looking backwards. This happens, e.g., when
7953 we were reseated to the previous screenful of text by
7954 vertical-motion. */
7955 it->base_level_stop = BEGV;
7956 compute_stop_pos_backwards (it);
7957 handle_stop_backwards (it, it->prev_stop);
7958 }
7959 else
7960 handle_stop_backwards (it, it->base_level_stop);
7961 return GET_NEXT_DISPLAY_ELEMENT (it);
7962 }
7963 else
7964 {
7965 /* No face changes, overlays etc. in sight, so just return a
7966 character from current_buffer. */
7967 unsigned char *p;
7968 ptrdiff_t stop;
7969
7970 /* Maybe run the redisplay end trigger hook. Performance note:
7971 This doesn't seem to cost measurable time. */
7972 if (it->redisplay_end_trigger_charpos
7973 && it->glyph_row
7974 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7975 run_redisplay_end_trigger_hook (it);
7976
7977 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7978 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7979 stop)
7980 && next_element_from_composition (it))
7981 {
7982 return 1;
7983 }
7984
7985 /* Get the next character, maybe multibyte. */
7986 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7987 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7988 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7989 else
7990 it->c = *p, it->len = 1;
7991
7992 /* Record what we have and where it came from. */
7993 it->what = IT_CHARACTER;
7994 it->object = it->w->contents;
7995 it->position = it->current.pos;
7996
7997 /* Normally we return the character found above, except when we
7998 really want to return an ellipsis for selective display. */
7999 if (it->selective)
8000 {
8001 if (it->c == '\n')
8002 {
8003 /* A value of selective > 0 means hide lines indented more
8004 than that number of columns. */
8005 if (it->selective > 0
8006 && IT_CHARPOS (*it) + 1 < ZV
8007 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8008 IT_BYTEPOS (*it) + 1,
8009 it->selective))
8010 {
8011 success_p = next_element_from_ellipsis (it);
8012 it->dpvec_char_len = -1;
8013 }
8014 }
8015 else if (it->c == '\r' && it->selective == -1)
8016 {
8017 /* A value of selective == -1 means that everything from the
8018 CR to the end of the line is invisible, with maybe an
8019 ellipsis displayed for it. */
8020 success_p = next_element_from_ellipsis (it);
8021 it->dpvec_char_len = -1;
8022 }
8023 }
8024 }
8025
8026 /* Value is zero if end of buffer reached. */
8027 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8028 return success_p;
8029 }
8030
8031
8032 /* Run the redisplay end trigger hook for IT. */
8033
8034 static void
8035 run_redisplay_end_trigger_hook (struct it *it)
8036 {
8037 Lisp_Object args[3];
8038
8039 /* IT->glyph_row should be non-null, i.e. we should be actually
8040 displaying something, or otherwise we should not run the hook. */
8041 eassert (it->glyph_row);
8042
8043 /* Set up hook arguments. */
8044 args[0] = Qredisplay_end_trigger_functions;
8045 args[1] = it->window;
8046 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8047 it->redisplay_end_trigger_charpos = 0;
8048
8049 /* Since we are *trying* to run these functions, don't try to run
8050 them again, even if they get an error. */
8051 wset_redisplay_end_trigger (it->w, Qnil);
8052 Frun_hook_with_args (3, args);
8053
8054 /* Notice if it changed the face of the character we are on. */
8055 handle_face_prop (it);
8056 }
8057
8058
8059 /* Deliver a composition display element. Unlike the other
8060 next_element_from_XXX, this function is not registered in the array
8061 get_next_element[]. It is called from next_element_from_buffer and
8062 next_element_from_string when necessary. */
8063
8064 static int
8065 next_element_from_composition (struct it *it)
8066 {
8067 it->what = IT_COMPOSITION;
8068 it->len = it->cmp_it.nbytes;
8069 if (STRINGP (it->string))
8070 {
8071 if (it->c < 0)
8072 {
8073 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8074 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8075 return 0;
8076 }
8077 it->position = it->current.string_pos;
8078 it->object = it->string;
8079 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8080 IT_STRING_BYTEPOS (*it), it->string);
8081 }
8082 else
8083 {
8084 if (it->c < 0)
8085 {
8086 IT_CHARPOS (*it) += it->cmp_it.nchars;
8087 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8088 if (it->bidi_p)
8089 {
8090 if (it->bidi_it.new_paragraph)
8091 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8092 /* Resync the bidi iterator with IT's new position.
8093 FIXME: this doesn't support bidirectional text. */
8094 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8095 bidi_move_to_visually_next (&it->bidi_it);
8096 }
8097 return 0;
8098 }
8099 it->position = it->current.pos;
8100 it->object = it->w->contents;
8101 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8102 IT_BYTEPOS (*it), Qnil);
8103 }
8104 return 1;
8105 }
8106
8107
8108 \f
8109 /***********************************************************************
8110 Moving an iterator without producing glyphs
8111 ***********************************************************************/
8112
8113 /* Check if iterator is at a position corresponding to a valid buffer
8114 position after some move_it_ call. */
8115
8116 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8117 ((it)->method == GET_FROM_STRING \
8118 ? IT_STRING_CHARPOS (*it) == 0 \
8119 : 1)
8120
8121
8122 /* Move iterator IT to a specified buffer or X position within one
8123 line on the display without producing glyphs.
8124
8125 OP should be a bit mask including some or all of these bits:
8126 MOVE_TO_X: Stop upon reaching x-position TO_X.
8127 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8128 Regardless of OP's value, stop upon reaching the end of the display line.
8129
8130 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8131 This means, in particular, that TO_X includes window's horizontal
8132 scroll amount.
8133
8134 The return value has several possible values that
8135 say what condition caused the scan to stop:
8136
8137 MOVE_POS_MATCH_OR_ZV
8138 - when TO_POS or ZV was reached.
8139
8140 MOVE_X_REACHED
8141 -when TO_X was reached before TO_POS or ZV were reached.
8142
8143 MOVE_LINE_CONTINUED
8144 - when we reached the end of the display area and the line must
8145 be continued.
8146
8147 MOVE_LINE_TRUNCATED
8148 - when we reached the end of the display area and the line is
8149 truncated.
8150
8151 MOVE_NEWLINE_OR_CR
8152 - when we stopped at a line end, i.e. a newline or a CR and selective
8153 display is on. */
8154
8155 static enum move_it_result
8156 move_it_in_display_line_to (struct it *it,
8157 ptrdiff_t to_charpos, int to_x,
8158 enum move_operation_enum op)
8159 {
8160 enum move_it_result result = MOVE_UNDEFINED;
8161 struct glyph_row *saved_glyph_row;
8162 struct it wrap_it, atpos_it, atx_it, ppos_it;
8163 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8164 void *ppos_data = NULL;
8165 int may_wrap = 0;
8166 enum it_method prev_method = it->method;
8167 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8168 int saw_smaller_pos = prev_pos < to_charpos;
8169
8170 /* Don't produce glyphs in produce_glyphs. */
8171 saved_glyph_row = it->glyph_row;
8172 it->glyph_row = NULL;
8173
8174 /* Use wrap_it to save a copy of IT wherever a word wrap could
8175 occur. Use atpos_it to save a copy of IT at the desired buffer
8176 position, if found, so that we can scan ahead and check if the
8177 word later overshoots the window edge. Use atx_it similarly, for
8178 pixel positions. */
8179 wrap_it.sp = -1;
8180 atpos_it.sp = -1;
8181 atx_it.sp = -1;
8182
8183 /* Use ppos_it under bidi reordering to save a copy of IT for the
8184 position > CHARPOS that is the closest to CHARPOS. We restore
8185 that position in IT when we have scanned the entire display line
8186 without finding a match for CHARPOS and all the character
8187 positions are greater than CHARPOS. */
8188 if (it->bidi_p)
8189 {
8190 SAVE_IT (ppos_it, *it, ppos_data);
8191 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8192 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8193 SAVE_IT (ppos_it, *it, ppos_data);
8194 }
8195
8196 #define BUFFER_POS_REACHED_P() \
8197 ((op & MOVE_TO_POS) != 0 \
8198 && BUFFERP (it->object) \
8199 && (IT_CHARPOS (*it) == to_charpos \
8200 || ((!it->bidi_p \
8201 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8202 && IT_CHARPOS (*it) > to_charpos) \
8203 || (it->what == IT_COMPOSITION \
8204 && ((IT_CHARPOS (*it) > to_charpos \
8205 && to_charpos >= it->cmp_it.charpos) \
8206 || (IT_CHARPOS (*it) < to_charpos \
8207 && to_charpos <= it->cmp_it.charpos)))) \
8208 && (it->method == GET_FROM_BUFFER \
8209 || (it->method == GET_FROM_DISPLAY_VECTOR \
8210 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8211
8212 /* If there's a line-/wrap-prefix, handle it. */
8213 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8214 && it->current_y < it->last_visible_y)
8215 handle_line_prefix (it);
8216
8217 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8218 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8219
8220 while (1)
8221 {
8222 int x, i, ascent = 0, descent = 0;
8223
8224 /* Utility macro to reset an iterator with x, ascent, and descent. */
8225 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8226 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8227 (IT)->max_descent = descent)
8228
8229 /* Stop if we move beyond TO_CHARPOS (after an image or a
8230 display string or stretch glyph). */
8231 if ((op & MOVE_TO_POS) != 0
8232 && BUFFERP (it->object)
8233 && it->method == GET_FROM_BUFFER
8234 && (((!it->bidi_p
8235 /* When the iterator is at base embedding level, we
8236 are guaranteed that characters are delivered for
8237 display in strictly increasing order of their
8238 buffer positions. */
8239 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8240 && IT_CHARPOS (*it) > to_charpos)
8241 || (it->bidi_p
8242 && (prev_method == GET_FROM_IMAGE
8243 || prev_method == GET_FROM_STRETCH
8244 || prev_method == GET_FROM_STRING)
8245 /* Passed TO_CHARPOS from left to right. */
8246 && ((prev_pos < to_charpos
8247 && IT_CHARPOS (*it) > to_charpos)
8248 /* Passed TO_CHARPOS from right to left. */
8249 || (prev_pos > to_charpos
8250 && IT_CHARPOS (*it) < to_charpos)))))
8251 {
8252 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8253 {
8254 result = MOVE_POS_MATCH_OR_ZV;
8255 break;
8256 }
8257 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8258 /* If wrap_it is valid, the current position might be in a
8259 word that is wrapped. So, save the iterator in
8260 atpos_it and continue to see if wrapping happens. */
8261 SAVE_IT (atpos_it, *it, atpos_data);
8262 }
8263
8264 /* Stop when ZV reached.
8265 We used to stop here when TO_CHARPOS reached as well, but that is
8266 too soon if this glyph does not fit on this line. So we handle it
8267 explicitly below. */
8268 if (!get_next_display_element (it))
8269 {
8270 result = MOVE_POS_MATCH_OR_ZV;
8271 break;
8272 }
8273
8274 if (it->line_wrap == TRUNCATE)
8275 {
8276 if (BUFFER_POS_REACHED_P ())
8277 {
8278 result = MOVE_POS_MATCH_OR_ZV;
8279 break;
8280 }
8281 }
8282 else
8283 {
8284 if (it->line_wrap == WORD_WRAP)
8285 {
8286 if (IT_DISPLAYING_WHITESPACE (it))
8287 may_wrap = 1;
8288 else if (may_wrap)
8289 {
8290 /* We have reached a glyph that follows one or more
8291 whitespace characters. If the position is
8292 already found, we are done. */
8293 if (atpos_it.sp >= 0)
8294 {
8295 RESTORE_IT (it, &atpos_it, atpos_data);
8296 result = MOVE_POS_MATCH_OR_ZV;
8297 goto done;
8298 }
8299 if (atx_it.sp >= 0)
8300 {
8301 RESTORE_IT (it, &atx_it, atx_data);
8302 result = MOVE_X_REACHED;
8303 goto done;
8304 }
8305 /* Otherwise, we can wrap here. */
8306 SAVE_IT (wrap_it, *it, wrap_data);
8307 may_wrap = 0;
8308 }
8309 }
8310 }
8311
8312 /* Remember the line height for the current line, in case
8313 the next element doesn't fit on the line. */
8314 ascent = it->max_ascent;
8315 descent = it->max_descent;
8316
8317 /* The call to produce_glyphs will get the metrics of the
8318 display element IT is loaded with. Record the x-position
8319 before this display element, in case it doesn't fit on the
8320 line. */
8321 x = it->current_x;
8322
8323 PRODUCE_GLYPHS (it);
8324
8325 if (it->area != TEXT_AREA)
8326 {
8327 prev_method = it->method;
8328 if (it->method == GET_FROM_BUFFER)
8329 prev_pos = IT_CHARPOS (*it);
8330 set_iterator_to_next (it, 1);
8331 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8332 SET_TEXT_POS (this_line_min_pos,
8333 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8334 if (it->bidi_p
8335 && (op & MOVE_TO_POS)
8336 && IT_CHARPOS (*it) > to_charpos
8337 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8338 SAVE_IT (ppos_it, *it, ppos_data);
8339 continue;
8340 }
8341
8342 /* The number of glyphs we get back in IT->nglyphs will normally
8343 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8344 character on a terminal frame, or (iii) a line end. For the
8345 second case, IT->nglyphs - 1 padding glyphs will be present.
8346 (On X frames, there is only one glyph produced for a
8347 composite character.)
8348
8349 The behavior implemented below means, for continuation lines,
8350 that as many spaces of a TAB as fit on the current line are
8351 displayed there. For terminal frames, as many glyphs of a
8352 multi-glyph character are displayed in the current line, too.
8353 This is what the old redisplay code did, and we keep it that
8354 way. Under X, the whole shape of a complex character must
8355 fit on the line or it will be completely displayed in the
8356 next line.
8357
8358 Note that both for tabs and padding glyphs, all glyphs have
8359 the same width. */
8360 if (it->nglyphs)
8361 {
8362 /* More than one glyph or glyph doesn't fit on line. All
8363 glyphs have the same width. */
8364 int single_glyph_width = it->pixel_width / it->nglyphs;
8365 int new_x;
8366 int x_before_this_char = x;
8367 int hpos_before_this_char = it->hpos;
8368
8369 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8370 {
8371 new_x = x + single_glyph_width;
8372
8373 /* We want to leave anything reaching TO_X to the caller. */
8374 if ((op & MOVE_TO_X) && new_x > to_x)
8375 {
8376 if (BUFFER_POS_REACHED_P ())
8377 {
8378 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8379 goto buffer_pos_reached;
8380 if (atpos_it.sp < 0)
8381 {
8382 SAVE_IT (atpos_it, *it, atpos_data);
8383 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8384 }
8385 }
8386 else
8387 {
8388 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8389 {
8390 it->current_x = x;
8391 result = MOVE_X_REACHED;
8392 break;
8393 }
8394 if (atx_it.sp < 0)
8395 {
8396 SAVE_IT (atx_it, *it, atx_data);
8397 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8398 }
8399 }
8400 }
8401
8402 if (/* Lines are continued. */
8403 it->line_wrap != TRUNCATE
8404 && (/* And glyph doesn't fit on the line. */
8405 new_x > it->last_visible_x
8406 /* Or it fits exactly and we're on a window
8407 system frame. */
8408 || (new_x == it->last_visible_x
8409 && FRAME_WINDOW_P (it->f)
8410 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8411 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8412 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8413 {
8414 if (/* IT->hpos == 0 means the very first glyph
8415 doesn't fit on the line, e.g. a wide image. */
8416 it->hpos == 0
8417 || (new_x == it->last_visible_x
8418 && FRAME_WINDOW_P (it->f)))
8419 {
8420 ++it->hpos;
8421 it->current_x = new_x;
8422
8423 /* The character's last glyph just barely fits
8424 in this row. */
8425 if (i == it->nglyphs - 1)
8426 {
8427 /* If this is the destination position,
8428 return a position *before* it in this row,
8429 now that we know it fits in this row. */
8430 if (BUFFER_POS_REACHED_P ())
8431 {
8432 if (it->line_wrap != WORD_WRAP
8433 || wrap_it.sp < 0)
8434 {
8435 it->hpos = hpos_before_this_char;
8436 it->current_x = x_before_this_char;
8437 result = MOVE_POS_MATCH_OR_ZV;
8438 break;
8439 }
8440 if (it->line_wrap == WORD_WRAP
8441 && atpos_it.sp < 0)
8442 {
8443 SAVE_IT (atpos_it, *it, atpos_data);
8444 atpos_it.current_x = x_before_this_char;
8445 atpos_it.hpos = hpos_before_this_char;
8446 }
8447 }
8448
8449 prev_method = it->method;
8450 if (it->method == GET_FROM_BUFFER)
8451 prev_pos = IT_CHARPOS (*it);
8452 set_iterator_to_next (it, 1);
8453 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8454 SET_TEXT_POS (this_line_min_pos,
8455 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8456 /* On graphical terminals, newlines may
8457 "overflow" into the fringe if
8458 overflow-newline-into-fringe is non-nil.
8459 On text terminals, and on graphical
8460 terminals with no right margin, newlines
8461 may overflow into the last glyph on the
8462 display line.*/
8463 if (!FRAME_WINDOW_P (it->f)
8464 || ((it->bidi_p
8465 && it->bidi_it.paragraph_dir == R2L)
8466 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8467 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8468 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8469 {
8470 if (!get_next_display_element (it))
8471 {
8472 result = MOVE_POS_MATCH_OR_ZV;
8473 break;
8474 }
8475 if (BUFFER_POS_REACHED_P ())
8476 {
8477 if (ITERATOR_AT_END_OF_LINE_P (it))
8478 result = MOVE_POS_MATCH_OR_ZV;
8479 else
8480 result = MOVE_LINE_CONTINUED;
8481 break;
8482 }
8483 if (ITERATOR_AT_END_OF_LINE_P (it))
8484 {
8485 result = MOVE_NEWLINE_OR_CR;
8486 break;
8487 }
8488 }
8489 }
8490 }
8491 else
8492 IT_RESET_X_ASCENT_DESCENT (it);
8493
8494 if (wrap_it.sp >= 0)
8495 {
8496 RESTORE_IT (it, &wrap_it, wrap_data);
8497 atpos_it.sp = -1;
8498 atx_it.sp = -1;
8499 }
8500
8501 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8502 IT_CHARPOS (*it)));
8503 result = MOVE_LINE_CONTINUED;
8504 break;
8505 }
8506
8507 if (BUFFER_POS_REACHED_P ())
8508 {
8509 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8510 goto buffer_pos_reached;
8511 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8512 {
8513 SAVE_IT (atpos_it, *it, atpos_data);
8514 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8515 }
8516 }
8517
8518 if (new_x > it->first_visible_x)
8519 {
8520 /* Glyph is visible. Increment number of glyphs that
8521 would be displayed. */
8522 ++it->hpos;
8523 }
8524 }
8525
8526 if (result != MOVE_UNDEFINED)
8527 break;
8528 }
8529 else if (BUFFER_POS_REACHED_P ())
8530 {
8531 buffer_pos_reached:
8532 IT_RESET_X_ASCENT_DESCENT (it);
8533 result = MOVE_POS_MATCH_OR_ZV;
8534 break;
8535 }
8536 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8537 {
8538 /* Stop when TO_X specified and reached. This check is
8539 necessary here because of lines consisting of a line end,
8540 only. The line end will not produce any glyphs and we
8541 would never get MOVE_X_REACHED. */
8542 eassert (it->nglyphs == 0);
8543 result = MOVE_X_REACHED;
8544 break;
8545 }
8546
8547 /* Is this a line end? If yes, we're done. */
8548 if (ITERATOR_AT_END_OF_LINE_P (it))
8549 {
8550 /* If we are past TO_CHARPOS, but never saw any character
8551 positions smaller than TO_CHARPOS, return
8552 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8553 did. */
8554 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8555 {
8556 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8557 {
8558 if (IT_CHARPOS (ppos_it) < ZV)
8559 {
8560 RESTORE_IT (it, &ppos_it, ppos_data);
8561 result = MOVE_POS_MATCH_OR_ZV;
8562 }
8563 else
8564 goto buffer_pos_reached;
8565 }
8566 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8567 && IT_CHARPOS (*it) > to_charpos)
8568 goto buffer_pos_reached;
8569 else
8570 result = MOVE_NEWLINE_OR_CR;
8571 }
8572 else
8573 result = MOVE_NEWLINE_OR_CR;
8574 break;
8575 }
8576
8577 prev_method = it->method;
8578 if (it->method == GET_FROM_BUFFER)
8579 prev_pos = IT_CHARPOS (*it);
8580 /* The current display element has been consumed. Advance
8581 to the next. */
8582 set_iterator_to_next (it, 1);
8583 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8584 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8585 if (IT_CHARPOS (*it) < to_charpos)
8586 saw_smaller_pos = 1;
8587 if (it->bidi_p
8588 && (op & MOVE_TO_POS)
8589 && IT_CHARPOS (*it) >= to_charpos
8590 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8591 SAVE_IT (ppos_it, *it, ppos_data);
8592
8593 /* Stop if lines are truncated and IT's current x-position is
8594 past the right edge of the window now. */
8595 if (it->line_wrap == TRUNCATE
8596 && it->current_x >= it->last_visible_x)
8597 {
8598 if (!FRAME_WINDOW_P (it->f)
8599 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8600 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8601 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8602 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8603 {
8604 int at_eob_p = 0;
8605
8606 if ((at_eob_p = !get_next_display_element (it))
8607 || BUFFER_POS_REACHED_P ()
8608 /* If we are past TO_CHARPOS, but never saw any
8609 character positions smaller than TO_CHARPOS,
8610 return MOVE_POS_MATCH_OR_ZV, like the
8611 unidirectional display did. */
8612 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8613 && !saw_smaller_pos
8614 && IT_CHARPOS (*it) > to_charpos))
8615 {
8616 if (it->bidi_p
8617 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8618 RESTORE_IT (it, &ppos_it, ppos_data);
8619 result = MOVE_POS_MATCH_OR_ZV;
8620 break;
8621 }
8622 if (ITERATOR_AT_END_OF_LINE_P (it))
8623 {
8624 result = MOVE_NEWLINE_OR_CR;
8625 break;
8626 }
8627 }
8628 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8629 && !saw_smaller_pos
8630 && IT_CHARPOS (*it) > to_charpos)
8631 {
8632 if (IT_CHARPOS (ppos_it) < ZV)
8633 RESTORE_IT (it, &ppos_it, ppos_data);
8634 result = MOVE_POS_MATCH_OR_ZV;
8635 break;
8636 }
8637 result = MOVE_LINE_TRUNCATED;
8638 break;
8639 }
8640 #undef IT_RESET_X_ASCENT_DESCENT
8641 }
8642
8643 #undef BUFFER_POS_REACHED_P
8644
8645 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8646 restore the saved iterator. */
8647 if (atpos_it.sp >= 0)
8648 RESTORE_IT (it, &atpos_it, atpos_data);
8649 else if (atx_it.sp >= 0)
8650 RESTORE_IT (it, &atx_it, atx_data);
8651
8652 done:
8653
8654 if (atpos_data)
8655 bidi_unshelve_cache (atpos_data, 1);
8656 if (atx_data)
8657 bidi_unshelve_cache (atx_data, 1);
8658 if (wrap_data)
8659 bidi_unshelve_cache (wrap_data, 1);
8660 if (ppos_data)
8661 bidi_unshelve_cache (ppos_data, 1);
8662
8663 /* Restore the iterator settings altered at the beginning of this
8664 function. */
8665 it->glyph_row = saved_glyph_row;
8666 return result;
8667 }
8668
8669 /* For external use. */
8670 void
8671 move_it_in_display_line (struct it *it,
8672 ptrdiff_t to_charpos, int to_x,
8673 enum move_operation_enum op)
8674 {
8675 if (it->line_wrap == WORD_WRAP
8676 && (op & MOVE_TO_X))
8677 {
8678 struct it save_it;
8679 void *save_data = NULL;
8680 int skip;
8681
8682 SAVE_IT (save_it, *it, save_data);
8683 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8684 /* When word-wrap is on, TO_X may lie past the end
8685 of a wrapped line. Then it->current is the
8686 character on the next line, so backtrack to the
8687 space before the wrap point. */
8688 if (skip == MOVE_LINE_CONTINUED)
8689 {
8690 int prev_x = max (it->current_x - 1, 0);
8691 RESTORE_IT (it, &save_it, save_data);
8692 move_it_in_display_line_to
8693 (it, -1, prev_x, MOVE_TO_X);
8694 }
8695 else
8696 bidi_unshelve_cache (save_data, 1);
8697 }
8698 else
8699 move_it_in_display_line_to (it, to_charpos, to_x, op);
8700 }
8701
8702
8703 /* Move IT forward until it satisfies one or more of the criteria in
8704 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8705
8706 OP is a bit-mask that specifies where to stop, and in particular,
8707 which of those four position arguments makes a difference. See the
8708 description of enum move_operation_enum.
8709
8710 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8711 screen line, this function will set IT to the next position that is
8712 displayed to the right of TO_CHARPOS on the screen. */
8713
8714 void
8715 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8716 {
8717 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8718 int line_height, line_start_x = 0, reached = 0;
8719 void *backup_data = NULL;
8720
8721 for (;;)
8722 {
8723 if (op & MOVE_TO_VPOS)
8724 {
8725 /* If no TO_CHARPOS and no TO_X specified, stop at the
8726 start of the line TO_VPOS. */
8727 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8728 {
8729 if (it->vpos == to_vpos)
8730 {
8731 reached = 1;
8732 break;
8733 }
8734 else
8735 skip = move_it_in_display_line_to (it, -1, -1, 0);
8736 }
8737 else
8738 {
8739 /* TO_VPOS >= 0 means stop at TO_X in the line at
8740 TO_VPOS, or at TO_POS, whichever comes first. */
8741 if (it->vpos == to_vpos)
8742 {
8743 reached = 2;
8744 break;
8745 }
8746
8747 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8748
8749 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8750 {
8751 reached = 3;
8752 break;
8753 }
8754 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8755 {
8756 /* We have reached TO_X but not in the line we want. */
8757 skip = move_it_in_display_line_to (it, to_charpos,
8758 -1, MOVE_TO_POS);
8759 if (skip == MOVE_POS_MATCH_OR_ZV)
8760 {
8761 reached = 4;
8762 break;
8763 }
8764 }
8765 }
8766 }
8767 else if (op & MOVE_TO_Y)
8768 {
8769 struct it it_backup;
8770
8771 if (it->line_wrap == WORD_WRAP)
8772 SAVE_IT (it_backup, *it, backup_data);
8773
8774 /* TO_Y specified means stop at TO_X in the line containing
8775 TO_Y---or at TO_CHARPOS if this is reached first. The
8776 problem is that we can't really tell whether the line
8777 contains TO_Y before we have completely scanned it, and
8778 this may skip past TO_X. What we do is to first scan to
8779 TO_X.
8780
8781 If TO_X is not specified, use a TO_X of zero. The reason
8782 is to make the outcome of this function more predictable.
8783 If we didn't use TO_X == 0, we would stop at the end of
8784 the line which is probably not what a caller would expect
8785 to happen. */
8786 skip = move_it_in_display_line_to
8787 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8788 (MOVE_TO_X | (op & MOVE_TO_POS)));
8789
8790 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8791 if (skip == MOVE_POS_MATCH_OR_ZV)
8792 reached = 5;
8793 else if (skip == MOVE_X_REACHED)
8794 {
8795 /* If TO_X was reached, we want to know whether TO_Y is
8796 in the line. We know this is the case if the already
8797 scanned glyphs make the line tall enough. Otherwise,
8798 we must check by scanning the rest of the line. */
8799 line_height = it->max_ascent + it->max_descent;
8800 if (to_y >= it->current_y
8801 && to_y < it->current_y + line_height)
8802 {
8803 reached = 6;
8804 break;
8805 }
8806 SAVE_IT (it_backup, *it, backup_data);
8807 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8808 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8809 op & MOVE_TO_POS);
8810 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8811 line_height = it->max_ascent + it->max_descent;
8812 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8813
8814 if (to_y >= it->current_y
8815 && to_y < it->current_y + line_height)
8816 {
8817 /* If TO_Y is in this line and TO_X was reached
8818 above, we scanned too far. We have to restore
8819 IT's settings to the ones before skipping. But
8820 keep the more accurate values of max_ascent and
8821 max_descent we've found while skipping the rest
8822 of the line, for the sake of callers, such as
8823 pos_visible_p, that need to know the line
8824 height. */
8825 int max_ascent = it->max_ascent;
8826 int max_descent = it->max_descent;
8827
8828 RESTORE_IT (it, &it_backup, backup_data);
8829 it->max_ascent = max_ascent;
8830 it->max_descent = max_descent;
8831 reached = 6;
8832 }
8833 else
8834 {
8835 skip = skip2;
8836 if (skip == MOVE_POS_MATCH_OR_ZV)
8837 reached = 7;
8838 }
8839 }
8840 else
8841 {
8842 /* Check whether TO_Y is in this line. */
8843 line_height = it->max_ascent + it->max_descent;
8844 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8845
8846 if (to_y >= it->current_y
8847 && to_y < it->current_y + line_height)
8848 {
8849 /* When word-wrap is on, TO_X may lie past the end
8850 of a wrapped line. Then it->current is the
8851 character on the next line, so backtrack to the
8852 space before the wrap point. */
8853 if (skip == MOVE_LINE_CONTINUED
8854 && it->line_wrap == WORD_WRAP)
8855 {
8856 int prev_x = max (it->current_x - 1, 0);
8857 RESTORE_IT (it, &it_backup, backup_data);
8858 skip = move_it_in_display_line_to
8859 (it, -1, prev_x, MOVE_TO_X);
8860 }
8861 reached = 6;
8862 }
8863 }
8864
8865 if (reached)
8866 break;
8867 }
8868 else if (BUFFERP (it->object)
8869 && (it->method == GET_FROM_BUFFER
8870 || it->method == GET_FROM_STRETCH)
8871 && IT_CHARPOS (*it) >= to_charpos
8872 /* Under bidi iteration, a call to set_iterator_to_next
8873 can scan far beyond to_charpos if the initial
8874 portion of the next line needs to be reordered. In
8875 that case, give move_it_in_display_line_to another
8876 chance below. */
8877 && !(it->bidi_p
8878 && it->bidi_it.scan_dir == -1))
8879 skip = MOVE_POS_MATCH_OR_ZV;
8880 else
8881 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8882
8883 switch (skip)
8884 {
8885 case MOVE_POS_MATCH_OR_ZV:
8886 reached = 8;
8887 goto out;
8888
8889 case MOVE_NEWLINE_OR_CR:
8890 set_iterator_to_next (it, 1);
8891 it->continuation_lines_width = 0;
8892 break;
8893
8894 case MOVE_LINE_TRUNCATED:
8895 it->continuation_lines_width = 0;
8896 reseat_at_next_visible_line_start (it, 0);
8897 if ((op & MOVE_TO_POS) != 0
8898 && IT_CHARPOS (*it) > to_charpos)
8899 {
8900 reached = 9;
8901 goto out;
8902 }
8903 break;
8904
8905 case MOVE_LINE_CONTINUED:
8906 /* For continued lines ending in a tab, some of the glyphs
8907 associated with the tab are displayed on the current
8908 line. Since it->current_x does not include these glyphs,
8909 we use it->last_visible_x instead. */
8910 if (it->c == '\t')
8911 {
8912 it->continuation_lines_width += it->last_visible_x;
8913 /* When moving by vpos, ensure that the iterator really
8914 advances to the next line (bug#847, bug#969). Fixme:
8915 do we need to do this in other circumstances? */
8916 if (it->current_x != it->last_visible_x
8917 && (op & MOVE_TO_VPOS)
8918 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8919 {
8920 line_start_x = it->current_x + it->pixel_width
8921 - it->last_visible_x;
8922 set_iterator_to_next (it, 0);
8923 }
8924 }
8925 else
8926 it->continuation_lines_width += it->current_x;
8927 break;
8928
8929 default:
8930 emacs_abort ();
8931 }
8932
8933 /* Reset/increment for the next run. */
8934 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8935 it->current_x = line_start_x;
8936 line_start_x = 0;
8937 it->hpos = 0;
8938 it->current_y += it->max_ascent + it->max_descent;
8939 ++it->vpos;
8940 last_height = it->max_ascent + it->max_descent;
8941 it->max_ascent = it->max_descent = 0;
8942 }
8943
8944 out:
8945
8946 /* On text terminals, we may stop at the end of a line in the middle
8947 of a multi-character glyph. If the glyph itself is continued,
8948 i.e. it is actually displayed on the next line, don't treat this
8949 stopping point as valid; move to the next line instead (unless
8950 that brings us offscreen). */
8951 if (!FRAME_WINDOW_P (it->f)
8952 && op & MOVE_TO_POS
8953 && IT_CHARPOS (*it) == to_charpos
8954 && it->what == IT_CHARACTER
8955 && it->nglyphs > 1
8956 && it->line_wrap == WINDOW_WRAP
8957 && it->current_x == it->last_visible_x - 1
8958 && it->c != '\n'
8959 && it->c != '\t'
8960 && it->vpos < XFASTINT (it->w->window_end_vpos))
8961 {
8962 it->continuation_lines_width += it->current_x;
8963 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8964 it->current_y += it->max_ascent + it->max_descent;
8965 ++it->vpos;
8966 last_height = it->max_ascent + it->max_descent;
8967 }
8968
8969 if (backup_data)
8970 bidi_unshelve_cache (backup_data, 1);
8971
8972 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8973 }
8974
8975
8976 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8977
8978 If DY > 0, move IT backward at least that many pixels. DY = 0
8979 means move IT backward to the preceding line start or BEGV. This
8980 function may move over more than DY pixels if IT->current_y - DY
8981 ends up in the middle of a line; in this case IT->current_y will be
8982 set to the top of the line moved to. */
8983
8984 void
8985 move_it_vertically_backward (struct it *it, int dy)
8986 {
8987 int nlines, h;
8988 struct it it2, it3;
8989 void *it2data = NULL, *it3data = NULL;
8990 ptrdiff_t start_pos;
8991 int nchars_per_row
8992 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
8993 ptrdiff_t pos_limit;
8994
8995 move_further_back:
8996 eassert (dy >= 0);
8997
8998 start_pos = IT_CHARPOS (*it);
8999
9000 /* Estimate how many newlines we must move back. */
9001 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9002 if (it->line_wrap == TRUNCATE)
9003 pos_limit = BEGV;
9004 else
9005 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9006
9007 /* Set the iterator's position that many lines back. But don't go
9008 back more than NLINES full screen lines -- this wins a day with
9009 buffers which have very long lines. */
9010 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9011 back_to_previous_visible_line_start (it);
9012
9013 /* Reseat the iterator here. When moving backward, we don't want
9014 reseat to skip forward over invisible text, set up the iterator
9015 to deliver from overlay strings at the new position etc. So,
9016 use reseat_1 here. */
9017 reseat_1 (it, it->current.pos, 1);
9018
9019 /* We are now surely at a line start. */
9020 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9021 reordering is in effect. */
9022 it->continuation_lines_width = 0;
9023
9024 /* Move forward and see what y-distance we moved. First move to the
9025 start of the next line so that we get its height. We need this
9026 height to be able to tell whether we reached the specified
9027 y-distance. */
9028 SAVE_IT (it2, *it, it2data);
9029 it2.max_ascent = it2.max_descent = 0;
9030 do
9031 {
9032 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9033 MOVE_TO_POS | MOVE_TO_VPOS);
9034 }
9035 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9036 /* If we are in a display string which starts at START_POS,
9037 and that display string includes a newline, and we are
9038 right after that newline (i.e. at the beginning of a
9039 display line), exit the loop, because otherwise we will
9040 infloop, since move_it_to will see that it is already at
9041 START_POS and will not move. */
9042 || (it2.method == GET_FROM_STRING
9043 && IT_CHARPOS (it2) == start_pos
9044 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9045 eassert (IT_CHARPOS (*it) >= BEGV);
9046 SAVE_IT (it3, it2, it3data);
9047
9048 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9049 eassert (IT_CHARPOS (*it) >= BEGV);
9050 /* H is the actual vertical distance from the position in *IT
9051 and the starting position. */
9052 h = it2.current_y - it->current_y;
9053 /* NLINES is the distance in number of lines. */
9054 nlines = it2.vpos - it->vpos;
9055
9056 /* Correct IT's y and vpos position
9057 so that they are relative to the starting point. */
9058 it->vpos -= nlines;
9059 it->current_y -= h;
9060
9061 if (dy == 0)
9062 {
9063 /* DY == 0 means move to the start of the screen line. The
9064 value of nlines is > 0 if continuation lines were involved,
9065 or if the original IT position was at start of a line. */
9066 RESTORE_IT (it, it, it2data);
9067 if (nlines > 0)
9068 move_it_by_lines (it, nlines);
9069 /* The above code moves us to some position NLINES down,
9070 usually to its first glyph (leftmost in an L2R line), but
9071 that's not necessarily the start of the line, under bidi
9072 reordering. We want to get to the character position
9073 that is immediately after the newline of the previous
9074 line. */
9075 if (it->bidi_p
9076 && !it->continuation_lines_width
9077 && !STRINGP (it->string)
9078 && IT_CHARPOS (*it) > BEGV
9079 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9080 {
9081 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9082
9083 DEC_BOTH (cp, bp);
9084 cp = find_newline_no_quit (cp, bp, -1, NULL);
9085 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9086 }
9087 bidi_unshelve_cache (it3data, 1);
9088 }
9089 else
9090 {
9091 /* The y-position we try to reach, relative to *IT.
9092 Note that H has been subtracted in front of the if-statement. */
9093 int target_y = it->current_y + h - dy;
9094 int y0 = it3.current_y;
9095 int y1;
9096 int line_height;
9097
9098 RESTORE_IT (&it3, &it3, it3data);
9099 y1 = line_bottom_y (&it3);
9100 line_height = y1 - y0;
9101 RESTORE_IT (it, it, it2data);
9102 /* If we did not reach target_y, try to move further backward if
9103 we can. If we moved too far backward, try to move forward. */
9104 if (target_y < it->current_y
9105 /* This is heuristic. In a window that's 3 lines high, with
9106 a line height of 13 pixels each, recentering with point
9107 on the bottom line will try to move -39/2 = 19 pixels
9108 backward. Try to avoid moving into the first line. */
9109 && (it->current_y - target_y
9110 > min (window_box_height (it->w), line_height * 2 / 3))
9111 && IT_CHARPOS (*it) > BEGV)
9112 {
9113 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9114 target_y - it->current_y));
9115 dy = it->current_y - target_y;
9116 goto move_further_back;
9117 }
9118 else if (target_y >= it->current_y + line_height
9119 && IT_CHARPOS (*it) < ZV)
9120 {
9121 /* Should move forward by at least one line, maybe more.
9122
9123 Note: Calling move_it_by_lines can be expensive on
9124 terminal frames, where compute_motion is used (via
9125 vmotion) to do the job, when there are very long lines
9126 and truncate-lines is nil. That's the reason for
9127 treating terminal frames specially here. */
9128
9129 if (!FRAME_WINDOW_P (it->f))
9130 move_it_vertically (it, target_y - (it->current_y + line_height));
9131 else
9132 {
9133 do
9134 {
9135 move_it_by_lines (it, 1);
9136 }
9137 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9138 }
9139 }
9140 }
9141 }
9142
9143
9144 /* Move IT by a specified amount of pixel lines DY. DY negative means
9145 move backwards. DY = 0 means move to start of screen line. At the
9146 end, IT will be on the start of a screen line. */
9147
9148 void
9149 move_it_vertically (struct it *it, int dy)
9150 {
9151 if (dy <= 0)
9152 move_it_vertically_backward (it, -dy);
9153 else
9154 {
9155 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9156 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9157 MOVE_TO_POS | MOVE_TO_Y);
9158 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9159
9160 /* If buffer ends in ZV without a newline, move to the start of
9161 the line to satisfy the post-condition. */
9162 if (IT_CHARPOS (*it) == ZV
9163 && ZV > BEGV
9164 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9165 move_it_by_lines (it, 0);
9166 }
9167 }
9168
9169
9170 /* Move iterator IT past the end of the text line it is in. */
9171
9172 void
9173 move_it_past_eol (struct it *it)
9174 {
9175 enum move_it_result rc;
9176
9177 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9178 if (rc == MOVE_NEWLINE_OR_CR)
9179 set_iterator_to_next (it, 0);
9180 }
9181
9182
9183 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9184 negative means move up. DVPOS == 0 means move to the start of the
9185 screen line.
9186
9187 Optimization idea: If we would know that IT->f doesn't use
9188 a face with proportional font, we could be faster for
9189 truncate-lines nil. */
9190
9191 void
9192 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9193 {
9194
9195 /* The commented-out optimization uses vmotion on terminals. This
9196 gives bad results, because elements like it->what, on which
9197 callers such as pos_visible_p rely, aren't updated. */
9198 /* struct position pos;
9199 if (!FRAME_WINDOW_P (it->f))
9200 {
9201 struct text_pos textpos;
9202
9203 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9204 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9205 reseat (it, textpos, 1);
9206 it->vpos += pos.vpos;
9207 it->current_y += pos.vpos;
9208 }
9209 else */
9210
9211 if (dvpos == 0)
9212 {
9213 /* DVPOS == 0 means move to the start of the screen line. */
9214 move_it_vertically_backward (it, 0);
9215 /* Let next call to line_bottom_y calculate real line height */
9216 last_height = 0;
9217 }
9218 else if (dvpos > 0)
9219 {
9220 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9221 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9222 {
9223 /* Only move to the next buffer position if we ended up in a
9224 string from display property, not in an overlay string
9225 (before-string or after-string). That is because the
9226 latter don't conceal the underlying buffer position, so
9227 we can ask to move the iterator to the exact position we
9228 are interested in. Note that, even if we are already at
9229 IT_CHARPOS (*it), the call below is not a no-op, as it
9230 will detect that we are at the end of the string, pop the
9231 iterator, and compute it->current_x and it->hpos
9232 correctly. */
9233 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9234 -1, -1, -1, MOVE_TO_POS);
9235 }
9236 }
9237 else
9238 {
9239 struct it it2;
9240 void *it2data = NULL;
9241 ptrdiff_t start_charpos, i;
9242 int nchars_per_row
9243 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9244 ptrdiff_t pos_limit;
9245
9246 /* Start at the beginning of the screen line containing IT's
9247 position. This may actually move vertically backwards,
9248 in case of overlays, so adjust dvpos accordingly. */
9249 dvpos += it->vpos;
9250 move_it_vertically_backward (it, 0);
9251 dvpos -= it->vpos;
9252
9253 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9254 screen lines, and reseat the iterator there. */
9255 start_charpos = IT_CHARPOS (*it);
9256 if (it->line_wrap == TRUNCATE)
9257 pos_limit = BEGV;
9258 else
9259 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9260 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9261 back_to_previous_visible_line_start (it);
9262 reseat (it, it->current.pos, 1);
9263
9264 /* Move further back if we end up in a string or an image. */
9265 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9266 {
9267 /* First try to move to start of display line. */
9268 dvpos += it->vpos;
9269 move_it_vertically_backward (it, 0);
9270 dvpos -= it->vpos;
9271 if (IT_POS_VALID_AFTER_MOVE_P (it))
9272 break;
9273 /* If start of line is still in string or image,
9274 move further back. */
9275 back_to_previous_visible_line_start (it);
9276 reseat (it, it->current.pos, 1);
9277 dvpos--;
9278 }
9279
9280 it->current_x = it->hpos = 0;
9281
9282 /* Above call may have moved too far if continuation lines
9283 are involved. Scan forward and see if it did. */
9284 SAVE_IT (it2, *it, it2data);
9285 it2.vpos = it2.current_y = 0;
9286 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9287 it->vpos -= it2.vpos;
9288 it->current_y -= it2.current_y;
9289 it->current_x = it->hpos = 0;
9290
9291 /* If we moved too far back, move IT some lines forward. */
9292 if (it2.vpos > -dvpos)
9293 {
9294 int delta = it2.vpos + dvpos;
9295
9296 RESTORE_IT (&it2, &it2, it2data);
9297 SAVE_IT (it2, *it, it2data);
9298 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9299 /* Move back again if we got too far ahead. */
9300 if (IT_CHARPOS (*it) >= start_charpos)
9301 RESTORE_IT (it, &it2, it2data);
9302 else
9303 bidi_unshelve_cache (it2data, 1);
9304 }
9305 else
9306 RESTORE_IT (it, it, it2data);
9307 }
9308 }
9309
9310 /* Return 1 if IT points into the middle of a display vector. */
9311
9312 int
9313 in_display_vector_p (struct it *it)
9314 {
9315 return (it->method == GET_FROM_DISPLAY_VECTOR
9316 && it->current.dpvec_index > 0
9317 && it->dpvec + it->current.dpvec_index != it->dpend);
9318 }
9319
9320 \f
9321 /***********************************************************************
9322 Messages
9323 ***********************************************************************/
9324
9325
9326 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9327 to *Messages*. */
9328
9329 void
9330 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9331 {
9332 Lisp_Object args[3];
9333 Lisp_Object msg, fmt;
9334 char *buffer;
9335 ptrdiff_t len;
9336 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9337 USE_SAFE_ALLOCA;
9338
9339 fmt = msg = Qnil;
9340 GCPRO4 (fmt, msg, arg1, arg2);
9341
9342 args[0] = fmt = build_string (format);
9343 args[1] = arg1;
9344 args[2] = arg2;
9345 msg = Fformat (3, args);
9346
9347 len = SBYTES (msg) + 1;
9348 buffer = SAFE_ALLOCA (len);
9349 memcpy (buffer, SDATA (msg), len);
9350
9351 message_dolog (buffer, len - 1, 1, 0);
9352 SAFE_FREE ();
9353
9354 UNGCPRO;
9355 }
9356
9357
9358 /* Output a newline in the *Messages* buffer if "needs" one. */
9359
9360 void
9361 message_log_maybe_newline (void)
9362 {
9363 if (message_log_need_newline)
9364 message_dolog ("", 0, 1, 0);
9365 }
9366
9367
9368 /* Add a string M of length NBYTES to the message log, optionally
9369 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9370 true, means interpret the contents of M as multibyte. This
9371 function calls low-level routines in order to bypass text property
9372 hooks, etc. which might not be safe to run.
9373
9374 This may GC (insert may run before/after change hooks),
9375 so the buffer M must NOT point to a Lisp string. */
9376
9377 void
9378 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9379 {
9380 const unsigned char *msg = (const unsigned char *) m;
9381
9382 if (!NILP (Vmemory_full))
9383 return;
9384
9385 if (!NILP (Vmessage_log_max))
9386 {
9387 struct buffer *oldbuf;
9388 Lisp_Object oldpoint, oldbegv, oldzv;
9389 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9390 ptrdiff_t point_at_end = 0;
9391 ptrdiff_t zv_at_end = 0;
9392 Lisp_Object old_deactivate_mark;
9393 bool shown;
9394 struct gcpro gcpro1;
9395
9396 old_deactivate_mark = Vdeactivate_mark;
9397 oldbuf = current_buffer;
9398 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9399 bset_undo_list (current_buffer, Qt);
9400
9401 oldpoint = message_dolog_marker1;
9402 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9403 oldbegv = message_dolog_marker2;
9404 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9405 oldzv = message_dolog_marker3;
9406 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9407 GCPRO1 (old_deactivate_mark);
9408
9409 if (PT == Z)
9410 point_at_end = 1;
9411 if (ZV == Z)
9412 zv_at_end = 1;
9413
9414 BEGV = BEG;
9415 BEGV_BYTE = BEG_BYTE;
9416 ZV = Z;
9417 ZV_BYTE = Z_BYTE;
9418 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9419
9420 /* Insert the string--maybe converting multibyte to single byte
9421 or vice versa, so that all the text fits the buffer. */
9422 if (multibyte
9423 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9424 {
9425 ptrdiff_t i;
9426 int c, char_bytes;
9427 char work[1];
9428
9429 /* Convert a multibyte string to single-byte
9430 for the *Message* buffer. */
9431 for (i = 0; i < nbytes; i += char_bytes)
9432 {
9433 c = string_char_and_length (msg + i, &char_bytes);
9434 work[0] = (ASCII_CHAR_P (c)
9435 ? c
9436 : multibyte_char_to_unibyte (c));
9437 insert_1_both (work, 1, 1, 1, 0, 0);
9438 }
9439 }
9440 else if (! multibyte
9441 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9442 {
9443 ptrdiff_t i;
9444 int c, char_bytes;
9445 unsigned char str[MAX_MULTIBYTE_LENGTH];
9446 /* Convert a single-byte string to multibyte
9447 for the *Message* buffer. */
9448 for (i = 0; i < nbytes; i++)
9449 {
9450 c = msg[i];
9451 MAKE_CHAR_MULTIBYTE (c);
9452 char_bytes = CHAR_STRING (c, str);
9453 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9454 }
9455 }
9456 else if (nbytes)
9457 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9458
9459 if (nlflag)
9460 {
9461 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9462 printmax_t dups;
9463
9464 insert_1_both ("\n", 1, 1, 1, 0, 0);
9465
9466 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9467 this_bol = PT;
9468 this_bol_byte = PT_BYTE;
9469
9470 /* See if this line duplicates the previous one.
9471 If so, combine duplicates. */
9472 if (this_bol > BEG)
9473 {
9474 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9475 prev_bol = PT;
9476 prev_bol_byte = PT_BYTE;
9477
9478 dups = message_log_check_duplicate (prev_bol_byte,
9479 this_bol_byte);
9480 if (dups)
9481 {
9482 del_range_both (prev_bol, prev_bol_byte,
9483 this_bol, this_bol_byte, 0);
9484 if (dups > 1)
9485 {
9486 char dupstr[sizeof " [ times]"
9487 + INT_STRLEN_BOUND (printmax_t)];
9488
9489 /* If you change this format, don't forget to also
9490 change message_log_check_duplicate. */
9491 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9492 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9493 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9494 }
9495 }
9496 }
9497
9498 /* If we have more than the desired maximum number of lines
9499 in the *Messages* buffer now, delete the oldest ones.
9500 This is safe because we don't have undo in this buffer. */
9501
9502 if (NATNUMP (Vmessage_log_max))
9503 {
9504 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9505 -XFASTINT (Vmessage_log_max) - 1, 0);
9506 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9507 }
9508 }
9509 BEGV = marker_position (oldbegv);
9510 BEGV_BYTE = marker_byte_position (oldbegv);
9511
9512 if (zv_at_end)
9513 {
9514 ZV = Z;
9515 ZV_BYTE = Z_BYTE;
9516 }
9517 else
9518 {
9519 ZV = marker_position (oldzv);
9520 ZV_BYTE = marker_byte_position (oldzv);
9521 }
9522
9523 if (point_at_end)
9524 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9525 else
9526 /* We can't do Fgoto_char (oldpoint) because it will run some
9527 Lisp code. */
9528 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9529 marker_byte_position (oldpoint));
9530
9531 UNGCPRO;
9532 unchain_marker (XMARKER (oldpoint));
9533 unchain_marker (XMARKER (oldbegv));
9534 unchain_marker (XMARKER (oldzv));
9535
9536 shown = buffer_window_count (current_buffer) > 0;
9537 set_buffer_internal (oldbuf);
9538 /* We called insert_1_both above with its 5th argument (PREPARE)
9539 zero, which prevents insert_1_both from calling
9540 prepare_to_modify_buffer, which in turns prevents us from
9541 incrementing windows_or_buffers_changed even if *Messages* is
9542 shown in some window. So we must manually incrementing
9543 windows_or_buffers_changed here to make up for that. */
9544 if (shown)
9545 windows_or_buffers_changed++;
9546 else
9547 windows_or_buffers_changed = old_windows_or_buffers_changed;
9548 message_log_need_newline = !nlflag;
9549 Vdeactivate_mark = old_deactivate_mark;
9550 }
9551 }
9552
9553
9554 /* We are at the end of the buffer after just having inserted a newline.
9555 (Note: We depend on the fact we won't be crossing the gap.)
9556 Check to see if the most recent message looks a lot like the previous one.
9557 Return 0 if different, 1 if the new one should just replace it, or a
9558 value N > 1 if we should also append " [N times]". */
9559
9560 static intmax_t
9561 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9562 {
9563 ptrdiff_t i;
9564 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9565 int seen_dots = 0;
9566 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9567 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9568
9569 for (i = 0; i < len; i++)
9570 {
9571 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9572 seen_dots = 1;
9573 if (p1[i] != p2[i])
9574 return seen_dots;
9575 }
9576 p1 += len;
9577 if (*p1 == '\n')
9578 return 2;
9579 if (*p1++ == ' ' && *p1++ == '[')
9580 {
9581 char *pend;
9582 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9583 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9584 return n + 1;
9585 }
9586 return 0;
9587 }
9588 \f
9589
9590 /* Display an echo area message M with a specified length of NBYTES
9591 bytes. The string may include null characters. If M is not a
9592 string, clear out any existing message, and let the mini-buffer
9593 text show through.
9594
9595 This function cancels echoing. */
9596
9597 void
9598 message3 (Lisp_Object m)
9599 {
9600 struct gcpro gcpro1;
9601
9602 GCPRO1 (m);
9603 clear_message (1,1);
9604 cancel_echoing ();
9605
9606 /* First flush out any partial line written with print. */
9607 message_log_maybe_newline ();
9608 if (STRINGP (m))
9609 {
9610 ptrdiff_t nbytes = SBYTES (m);
9611 bool multibyte = STRING_MULTIBYTE (m);
9612 USE_SAFE_ALLOCA;
9613 char *buffer = SAFE_ALLOCA (nbytes);
9614 memcpy (buffer, SDATA (m), nbytes);
9615 message_dolog (buffer, nbytes, 1, multibyte);
9616 SAFE_FREE ();
9617 }
9618 message3_nolog (m);
9619
9620 UNGCPRO;
9621 }
9622
9623
9624 /* The non-logging version of message3.
9625 This does not cancel echoing, because it is used for echoing.
9626 Perhaps we need to make a separate function for echoing
9627 and make this cancel echoing. */
9628
9629 void
9630 message3_nolog (Lisp_Object m)
9631 {
9632 struct frame *sf = SELECTED_FRAME ();
9633
9634 if (FRAME_INITIAL_P (sf))
9635 {
9636 if (noninteractive_need_newline)
9637 putc ('\n', stderr);
9638 noninteractive_need_newline = 0;
9639 if (STRINGP (m))
9640 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9641 if (cursor_in_echo_area == 0)
9642 fprintf (stderr, "\n");
9643 fflush (stderr);
9644 }
9645 /* Error messages get reported properly by cmd_error, so this must be just an
9646 informative message; if the frame hasn't really been initialized yet, just
9647 toss it. */
9648 else if (INTERACTIVE && sf->glyphs_initialized_p)
9649 {
9650 /* Get the frame containing the mini-buffer
9651 that the selected frame is using. */
9652 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9653 Lisp_Object frame = XWINDOW (mini_window)->frame;
9654 struct frame *f = XFRAME (frame);
9655
9656 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9657 Fmake_frame_visible (frame);
9658
9659 if (STRINGP (m) && SCHARS (m) > 0)
9660 {
9661 set_message (m);
9662 if (minibuffer_auto_raise)
9663 Fraise_frame (frame);
9664 /* Assume we are not echoing.
9665 (If we are, echo_now will override this.) */
9666 echo_message_buffer = Qnil;
9667 }
9668 else
9669 clear_message (1, 1);
9670
9671 do_pending_window_change (0);
9672 echo_area_display (1);
9673 do_pending_window_change (0);
9674 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9675 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9676 }
9677 }
9678
9679
9680 /* Display a null-terminated echo area message M. If M is 0, clear
9681 out any existing message, and let the mini-buffer text show through.
9682
9683 The buffer M must continue to exist until after the echo area gets
9684 cleared or some other message gets displayed there. Do not pass
9685 text that is stored in a Lisp string. Do not pass text in a buffer
9686 that was alloca'd. */
9687
9688 void
9689 message1 (const char *m)
9690 {
9691 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9692 }
9693
9694
9695 /* The non-logging counterpart of message1. */
9696
9697 void
9698 message1_nolog (const char *m)
9699 {
9700 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9701 }
9702
9703 /* Display a message M which contains a single %s
9704 which gets replaced with STRING. */
9705
9706 void
9707 message_with_string (const char *m, Lisp_Object string, int log)
9708 {
9709 CHECK_STRING (string);
9710
9711 if (noninteractive)
9712 {
9713 if (m)
9714 {
9715 if (noninteractive_need_newline)
9716 putc ('\n', stderr);
9717 noninteractive_need_newline = 0;
9718 fprintf (stderr, m, SDATA (string));
9719 if (!cursor_in_echo_area)
9720 fprintf (stderr, "\n");
9721 fflush (stderr);
9722 }
9723 }
9724 else if (INTERACTIVE)
9725 {
9726 /* The frame whose minibuffer we're going to display the message on.
9727 It may be larger than the selected frame, so we need
9728 to use its buffer, not the selected frame's buffer. */
9729 Lisp_Object mini_window;
9730 struct frame *f, *sf = SELECTED_FRAME ();
9731
9732 /* Get the frame containing the minibuffer
9733 that the selected frame is using. */
9734 mini_window = FRAME_MINIBUF_WINDOW (sf);
9735 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9736
9737 /* Error messages get reported properly by cmd_error, so this must be
9738 just an informative message; if the frame hasn't really been
9739 initialized yet, just toss it. */
9740 if (f->glyphs_initialized_p)
9741 {
9742 Lisp_Object args[2], msg;
9743 struct gcpro gcpro1, gcpro2;
9744
9745 args[0] = build_string (m);
9746 args[1] = msg = string;
9747 GCPRO2 (args[0], msg);
9748 gcpro1.nvars = 2;
9749
9750 msg = Fformat (2, args);
9751
9752 if (log)
9753 message3 (msg);
9754 else
9755 message3_nolog (msg);
9756
9757 UNGCPRO;
9758
9759 /* Print should start at the beginning of the message
9760 buffer next time. */
9761 message_buf_print = 0;
9762 }
9763 }
9764 }
9765
9766
9767 /* Dump an informative message to the minibuf. If M is 0, clear out
9768 any existing message, and let the mini-buffer text show through. */
9769
9770 static void
9771 vmessage (const char *m, va_list ap)
9772 {
9773 if (noninteractive)
9774 {
9775 if (m)
9776 {
9777 if (noninteractive_need_newline)
9778 putc ('\n', stderr);
9779 noninteractive_need_newline = 0;
9780 vfprintf (stderr, m, ap);
9781 if (cursor_in_echo_area == 0)
9782 fprintf (stderr, "\n");
9783 fflush (stderr);
9784 }
9785 }
9786 else if (INTERACTIVE)
9787 {
9788 /* The frame whose mini-buffer we're going to display the message
9789 on. It may be larger than the selected frame, so we need to
9790 use its buffer, not the selected frame's buffer. */
9791 Lisp_Object mini_window;
9792 struct frame *f, *sf = SELECTED_FRAME ();
9793
9794 /* Get the frame containing the mini-buffer
9795 that the selected frame is using. */
9796 mini_window = FRAME_MINIBUF_WINDOW (sf);
9797 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9798
9799 /* Error messages get reported properly by cmd_error, so this must be
9800 just an informative message; if the frame hasn't really been
9801 initialized yet, just toss it. */
9802 if (f->glyphs_initialized_p)
9803 {
9804 if (m)
9805 {
9806 ptrdiff_t len;
9807 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9808 char *message_buf = alloca (maxsize + 1);
9809
9810 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9811
9812 message3 (make_string (message_buf, len));
9813 }
9814 else
9815 message1 (0);
9816
9817 /* Print should start at the beginning of the message
9818 buffer next time. */
9819 message_buf_print = 0;
9820 }
9821 }
9822 }
9823
9824 void
9825 message (const char *m, ...)
9826 {
9827 va_list ap;
9828 va_start (ap, m);
9829 vmessage (m, ap);
9830 va_end (ap);
9831 }
9832
9833
9834 #if 0
9835 /* The non-logging version of message. */
9836
9837 void
9838 message_nolog (const char *m, ...)
9839 {
9840 Lisp_Object old_log_max;
9841 va_list ap;
9842 va_start (ap, m);
9843 old_log_max = Vmessage_log_max;
9844 Vmessage_log_max = Qnil;
9845 vmessage (m, ap);
9846 Vmessage_log_max = old_log_max;
9847 va_end (ap);
9848 }
9849 #endif
9850
9851
9852 /* Display the current message in the current mini-buffer. This is
9853 only called from error handlers in process.c, and is not time
9854 critical. */
9855
9856 void
9857 update_echo_area (void)
9858 {
9859 if (!NILP (echo_area_buffer[0]))
9860 {
9861 Lisp_Object string;
9862 string = Fcurrent_message ();
9863 message3 (string);
9864 }
9865 }
9866
9867
9868 /* Make sure echo area buffers in `echo_buffers' are live.
9869 If they aren't, make new ones. */
9870
9871 static void
9872 ensure_echo_area_buffers (void)
9873 {
9874 int i;
9875
9876 for (i = 0; i < 2; ++i)
9877 if (!BUFFERP (echo_buffer[i])
9878 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9879 {
9880 char name[30];
9881 Lisp_Object old_buffer;
9882 int j;
9883
9884 old_buffer = echo_buffer[i];
9885 echo_buffer[i] = Fget_buffer_create
9886 (make_formatted_string (name, " *Echo Area %d*", i));
9887 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9888 /* to force word wrap in echo area -
9889 it was decided to postpone this*/
9890 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9891
9892 for (j = 0; j < 2; ++j)
9893 if (EQ (old_buffer, echo_area_buffer[j]))
9894 echo_area_buffer[j] = echo_buffer[i];
9895 }
9896 }
9897
9898
9899 /* Call FN with args A1..A2 with either the current or last displayed
9900 echo_area_buffer as current buffer.
9901
9902 WHICH zero means use the current message buffer
9903 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9904 from echo_buffer[] and clear it.
9905
9906 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9907 suitable buffer from echo_buffer[] and clear it.
9908
9909 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9910 that the current message becomes the last displayed one, make
9911 choose a suitable buffer for echo_area_buffer[0], and clear it.
9912
9913 Value is what FN returns. */
9914
9915 static int
9916 with_echo_area_buffer (struct window *w, int which,
9917 int (*fn) (ptrdiff_t, Lisp_Object),
9918 ptrdiff_t a1, Lisp_Object a2)
9919 {
9920 Lisp_Object buffer;
9921 int this_one, the_other, clear_buffer_p, rc;
9922 ptrdiff_t count = SPECPDL_INDEX ();
9923
9924 /* If buffers aren't live, make new ones. */
9925 ensure_echo_area_buffers ();
9926
9927 clear_buffer_p = 0;
9928
9929 if (which == 0)
9930 this_one = 0, the_other = 1;
9931 else if (which > 0)
9932 this_one = 1, the_other = 0;
9933 else
9934 {
9935 this_one = 0, the_other = 1;
9936 clear_buffer_p = 1;
9937
9938 /* We need a fresh one in case the current echo buffer equals
9939 the one containing the last displayed echo area message. */
9940 if (!NILP (echo_area_buffer[this_one])
9941 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9942 echo_area_buffer[this_one] = Qnil;
9943 }
9944
9945 /* Choose a suitable buffer from echo_buffer[] is we don't
9946 have one. */
9947 if (NILP (echo_area_buffer[this_one]))
9948 {
9949 echo_area_buffer[this_one]
9950 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9951 ? echo_buffer[the_other]
9952 : echo_buffer[this_one]);
9953 clear_buffer_p = 1;
9954 }
9955
9956 buffer = echo_area_buffer[this_one];
9957
9958 /* Don't get confused by reusing the buffer used for echoing
9959 for a different purpose. */
9960 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9961 cancel_echoing ();
9962
9963 record_unwind_protect (unwind_with_echo_area_buffer,
9964 with_echo_area_buffer_unwind_data (w));
9965
9966 /* Make the echo area buffer current. Note that for display
9967 purposes, it is not necessary that the displayed window's buffer
9968 == current_buffer, except for text property lookup. So, let's
9969 only set that buffer temporarily here without doing a full
9970 Fset_window_buffer. We must also change w->pointm, though,
9971 because otherwise an assertions in unshow_buffer fails, and Emacs
9972 aborts. */
9973 set_buffer_internal_1 (XBUFFER (buffer));
9974 if (w)
9975 {
9976 wset_buffer (w, buffer);
9977 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9978 }
9979
9980 bset_undo_list (current_buffer, Qt);
9981 bset_read_only (current_buffer, Qnil);
9982 specbind (Qinhibit_read_only, Qt);
9983 specbind (Qinhibit_modification_hooks, Qt);
9984
9985 if (clear_buffer_p && Z > BEG)
9986 del_range (BEG, Z);
9987
9988 eassert (BEGV >= BEG);
9989 eassert (ZV <= Z && ZV >= BEGV);
9990
9991 rc = fn (a1, a2);
9992
9993 eassert (BEGV >= BEG);
9994 eassert (ZV <= Z && ZV >= BEGV);
9995
9996 unbind_to (count, Qnil);
9997 return rc;
9998 }
9999
10000
10001 /* Save state that should be preserved around the call to the function
10002 FN called in with_echo_area_buffer. */
10003
10004 static Lisp_Object
10005 with_echo_area_buffer_unwind_data (struct window *w)
10006 {
10007 int i = 0;
10008 Lisp_Object vector, tmp;
10009
10010 /* Reduce consing by keeping one vector in
10011 Vwith_echo_area_save_vector. */
10012 vector = Vwith_echo_area_save_vector;
10013 Vwith_echo_area_save_vector = Qnil;
10014
10015 if (NILP (vector))
10016 vector = Fmake_vector (make_number (9), Qnil);
10017
10018 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10019 ASET (vector, i, Vdeactivate_mark); ++i;
10020 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10021
10022 if (w)
10023 {
10024 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10025 ASET (vector, i, w->contents); ++i;
10026 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10027 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10028 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10029 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10030 }
10031 else
10032 {
10033 int end = i + 6;
10034 for (; i < end; ++i)
10035 ASET (vector, i, Qnil);
10036 }
10037
10038 eassert (i == ASIZE (vector));
10039 return vector;
10040 }
10041
10042
10043 /* Restore global state from VECTOR which was created by
10044 with_echo_area_buffer_unwind_data. */
10045
10046 static Lisp_Object
10047 unwind_with_echo_area_buffer (Lisp_Object vector)
10048 {
10049 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10050 Vdeactivate_mark = AREF (vector, 1);
10051 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10052
10053 if (WINDOWP (AREF (vector, 3)))
10054 {
10055 struct window *w;
10056 Lisp_Object buffer;
10057
10058 w = XWINDOW (AREF (vector, 3));
10059 buffer = AREF (vector, 4);
10060
10061 wset_buffer (w, buffer);
10062 set_marker_both (w->pointm, buffer,
10063 XFASTINT (AREF (vector, 5)),
10064 XFASTINT (AREF (vector, 6)));
10065 set_marker_both (w->start, buffer,
10066 XFASTINT (AREF (vector, 7)),
10067 XFASTINT (AREF (vector, 8)));
10068 }
10069
10070 Vwith_echo_area_save_vector = vector;
10071 return Qnil;
10072 }
10073
10074
10075 /* Set up the echo area for use by print functions. MULTIBYTE_P
10076 non-zero means we will print multibyte. */
10077
10078 void
10079 setup_echo_area_for_printing (int multibyte_p)
10080 {
10081 /* If we can't find an echo area any more, exit. */
10082 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10083 Fkill_emacs (Qnil);
10084
10085 ensure_echo_area_buffers ();
10086
10087 if (!message_buf_print)
10088 {
10089 /* A message has been output since the last time we printed.
10090 Choose a fresh echo area buffer. */
10091 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10092 echo_area_buffer[0] = echo_buffer[1];
10093 else
10094 echo_area_buffer[0] = echo_buffer[0];
10095
10096 /* Switch to that buffer and clear it. */
10097 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10098 bset_truncate_lines (current_buffer, Qnil);
10099
10100 if (Z > BEG)
10101 {
10102 ptrdiff_t count = SPECPDL_INDEX ();
10103 specbind (Qinhibit_read_only, Qt);
10104 /* Note that undo recording is always disabled. */
10105 del_range (BEG, Z);
10106 unbind_to (count, Qnil);
10107 }
10108 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10109
10110 /* Set up the buffer for the multibyteness we need. */
10111 if (multibyte_p
10112 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10113 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10114
10115 /* Raise the frame containing the echo area. */
10116 if (minibuffer_auto_raise)
10117 {
10118 struct frame *sf = SELECTED_FRAME ();
10119 Lisp_Object mini_window;
10120 mini_window = FRAME_MINIBUF_WINDOW (sf);
10121 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10122 }
10123
10124 message_log_maybe_newline ();
10125 message_buf_print = 1;
10126 }
10127 else
10128 {
10129 if (NILP (echo_area_buffer[0]))
10130 {
10131 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10132 echo_area_buffer[0] = echo_buffer[1];
10133 else
10134 echo_area_buffer[0] = echo_buffer[0];
10135 }
10136
10137 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10138 {
10139 /* Someone switched buffers between print requests. */
10140 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10141 bset_truncate_lines (current_buffer, Qnil);
10142 }
10143 }
10144 }
10145
10146
10147 /* Display an echo area message in window W. Value is non-zero if W's
10148 height is changed. If display_last_displayed_message_p is
10149 non-zero, display the message that was last displayed, otherwise
10150 display the current message. */
10151
10152 static int
10153 display_echo_area (struct window *w)
10154 {
10155 int i, no_message_p, window_height_changed_p;
10156
10157 /* Temporarily disable garbage collections while displaying the echo
10158 area. This is done because a GC can print a message itself.
10159 That message would modify the echo area buffer's contents while a
10160 redisplay of the buffer is going on, and seriously confuse
10161 redisplay. */
10162 ptrdiff_t count = inhibit_garbage_collection ();
10163
10164 /* If there is no message, we must call display_echo_area_1
10165 nevertheless because it resizes the window. But we will have to
10166 reset the echo_area_buffer in question to nil at the end because
10167 with_echo_area_buffer will sets it to an empty buffer. */
10168 i = display_last_displayed_message_p ? 1 : 0;
10169 no_message_p = NILP (echo_area_buffer[i]);
10170
10171 window_height_changed_p
10172 = with_echo_area_buffer (w, display_last_displayed_message_p,
10173 display_echo_area_1,
10174 (intptr_t) w, Qnil);
10175
10176 if (no_message_p)
10177 echo_area_buffer[i] = Qnil;
10178
10179 unbind_to (count, Qnil);
10180 return window_height_changed_p;
10181 }
10182
10183
10184 /* Helper for display_echo_area. Display the current buffer which
10185 contains the current echo area message in window W, a mini-window,
10186 a pointer to which is passed in A1. A2..A4 are currently not used.
10187 Change the height of W so that all of the message is displayed.
10188 Value is non-zero if height of W was changed. */
10189
10190 static int
10191 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10192 {
10193 intptr_t i1 = a1;
10194 struct window *w = (struct window *) i1;
10195 Lisp_Object window;
10196 struct text_pos start;
10197 int window_height_changed_p = 0;
10198
10199 /* Do this before displaying, so that we have a large enough glyph
10200 matrix for the display. If we can't get enough space for the
10201 whole text, display the last N lines. That works by setting w->start. */
10202 window_height_changed_p = resize_mini_window (w, 0);
10203
10204 /* Use the starting position chosen by resize_mini_window. */
10205 SET_TEXT_POS_FROM_MARKER (start, w->start);
10206
10207 /* Display. */
10208 clear_glyph_matrix (w->desired_matrix);
10209 XSETWINDOW (window, w);
10210 try_window (window, start, 0);
10211
10212 return window_height_changed_p;
10213 }
10214
10215
10216 /* Resize the echo area window to exactly the size needed for the
10217 currently displayed message, if there is one. If a mini-buffer
10218 is active, don't shrink it. */
10219
10220 void
10221 resize_echo_area_exactly (void)
10222 {
10223 if (BUFFERP (echo_area_buffer[0])
10224 && WINDOWP (echo_area_window))
10225 {
10226 struct window *w = XWINDOW (echo_area_window);
10227 int resized_p;
10228 Lisp_Object resize_exactly;
10229
10230 if (minibuf_level == 0)
10231 resize_exactly = Qt;
10232 else
10233 resize_exactly = Qnil;
10234
10235 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10236 (intptr_t) w, resize_exactly);
10237 if (resized_p)
10238 {
10239 ++windows_or_buffers_changed;
10240 ++update_mode_lines;
10241 redisplay_internal ();
10242 }
10243 }
10244 }
10245
10246
10247 /* Callback function for with_echo_area_buffer, when used from
10248 resize_echo_area_exactly. A1 contains a pointer to the window to
10249 resize, EXACTLY non-nil means resize the mini-window exactly to the
10250 size of the text displayed. A3 and A4 are not used. Value is what
10251 resize_mini_window returns. */
10252
10253 static int
10254 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10255 {
10256 intptr_t i1 = a1;
10257 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10258 }
10259
10260
10261 /* Resize mini-window W to fit the size of its contents. EXACT_P
10262 means size the window exactly to the size needed. Otherwise, it's
10263 only enlarged until W's buffer is empty.
10264
10265 Set W->start to the right place to begin display. If the whole
10266 contents fit, start at the beginning. Otherwise, start so as
10267 to make the end of the contents appear. This is particularly
10268 important for y-or-n-p, but seems desirable generally.
10269
10270 Value is non-zero if the window height has been changed. */
10271
10272 int
10273 resize_mini_window (struct window *w, int exact_p)
10274 {
10275 struct frame *f = XFRAME (w->frame);
10276 int window_height_changed_p = 0;
10277
10278 eassert (MINI_WINDOW_P (w));
10279
10280 /* By default, start display at the beginning. */
10281 set_marker_both (w->start, w->contents,
10282 BUF_BEGV (XBUFFER (w->contents)),
10283 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10284
10285 /* Don't resize windows while redisplaying a window; it would
10286 confuse redisplay functions when the size of the window they are
10287 displaying changes from under them. Such a resizing can happen,
10288 for instance, when which-func prints a long message while
10289 we are running fontification-functions. We're running these
10290 functions with safe_call which binds inhibit-redisplay to t. */
10291 if (!NILP (Vinhibit_redisplay))
10292 return 0;
10293
10294 /* Nil means don't try to resize. */
10295 if (NILP (Vresize_mini_windows)
10296 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10297 return 0;
10298
10299 if (!FRAME_MINIBUF_ONLY_P (f))
10300 {
10301 struct it it;
10302 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10303 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10304 int height;
10305 EMACS_INT max_height;
10306 int unit = FRAME_LINE_HEIGHT (f);
10307 struct text_pos start;
10308 struct buffer *old_current_buffer = NULL;
10309
10310 if (current_buffer != XBUFFER (w->contents))
10311 {
10312 old_current_buffer = current_buffer;
10313 set_buffer_internal (XBUFFER (w->contents));
10314 }
10315
10316 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10317
10318 /* Compute the max. number of lines specified by the user. */
10319 if (FLOATP (Vmax_mini_window_height))
10320 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10321 else if (INTEGERP (Vmax_mini_window_height))
10322 max_height = XINT (Vmax_mini_window_height);
10323 else
10324 max_height = total_height / 4;
10325
10326 /* Correct that max. height if it's bogus. */
10327 max_height = clip_to_bounds (1, max_height, total_height);
10328
10329 /* Find out the height of the text in the window. */
10330 if (it.line_wrap == TRUNCATE)
10331 height = 1;
10332 else
10333 {
10334 last_height = 0;
10335 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10336 if (it.max_ascent == 0 && it.max_descent == 0)
10337 height = it.current_y + last_height;
10338 else
10339 height = it.current_y + it.max_ascent + it.max_descent;
10340 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10341 height = (height + unit - 1) / unit;
10342 }
10343
10344 /* Compute a suitable window start. */
10345 if (height > max_height)
10346 {
10347 height = max_height;
10348 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10349 move_it_vertically_backward (&it, (height - 1) * unit);
10350 start = it.current.pos;
10351 }
10352 else
10353 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10354 SET_MARKER_FROM_TEXT_POS (w->start, start);
10355
10356 if (EQ (Vresize_mini_windows, Qgrow_only))
10357 {
10358 /* Let it grow only, until we display an empty message, in which
10359 case the window shrinks again. */
10360 if (height > WINDOW_TOTAL_LINES (w))
10361 {
10362 int old_height = WINDOW_TOTAL_LINES (w);
10363 freeze_window_starts (f, 1);
10364 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10365 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10366 }
10367 else if (height < WINDOW_TOTAL_LINES (w)
10368 && (exact_p || BEGV == ZV))
10369 {
10370 int old_height = WINDOW_TOTAL_LINES (w);
10371 freeze_window_starts (f, 0);
10372 shrink_mini_window (w);
10373 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10374 }
10375 }
10376 else
10377 {
10378 /* Always resize to exact size needed. */
10379 if (height > WINDOW_TOTAL_LINES (w))
10380 {
10381 int old_height = WINDOW_TOTAL_LINES (w);
10382 freeze_window_starts (f, 1);
10383 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10384 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10385 }
10386 else if (height < WINDOW_TOTAL_LINES (w))
10387 {
10388 int old_height = WINDOW_TOTAL_LINES (w);
10389 freeze_window_starts (f, 0);
10390 shrink_mini_window (w);
10391
10392 if (height)
10393 {
10394 freeze_window_starts (f, 1);
10395 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10396 }
10397
10398 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10399 }
10400 }
10401
10402 if (old_current_buffer)
10403 set_buffer_internal (old_current_buffer);
10404 }
10405
10406 return window_height_changed_p;
10407 }
10408
10409
10410 /* Value is the current message, a string, or nil if there is no
10411 current message. */
10412
10413 Lisp_Object
10414 current_message (void)
10415 {
10416 Lisp_Object msg;
10417
10418 if (!BUFFERP (echo_area_buffer[0]))
10419 msg = Qnil;
10420 else
10421 {
10422 with_echo_area_buffer (0, 0, current_message_1,
10423 (intptr_t) &msg, Qnil);
10424 if (NILP (msg))
10425 echo_area_buffer[0] = Qnil;
10426 }
10427
10428 return msg;
10429 }
10430
10431
10432 static int
10433 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10434 {
10435 intptr_t i1 = a1;
10436 Lisp_Object *msg = (Lisp_Object *) i1;
10437
10438 if (Z > BEG)
10439 *msg = make_buffer_string (BEG, Z, 1);
10440 else
10441 *msg = Qnil;
10442 return 0;
10443 }
10444
10445
10446 /* Push the current message on Vmessage_stack for later restoration
10447 by restore_message. Value is non-zero if the current message isn't
10448 empty. This is a relatively infrequent operation, so it's not
10449 worth optimizing. */
10450
10451 bool
10452 push_message (void)
10453 {
10454 Lisp_Object msg = current_message ();
10455 Vmessage_stack = Fcons (msg, Vmessage_stack);
10456 return STRINGP (msg);
10457 }
10458
10459
10460 /* Restore message display from the top of Vmessage_stack. */
10461
10462 void
10463 restore_message (void)
10464 {
10465 eassert (CONSP (Vmessage_stack));
10466 message3_nolog (XCAR (Vmessage_stack));
10467 }
10468
10469
10470 /* Handler for record_unwind_protect calling pop_message. */
10471
10472 Lisp_Object
10473 pop_message_unwind (Lisp_Object dummy)
10474 {
10475 pop_message ();
10476 return Qnil;
10477 }
10478
10479 /* Pop the top-most entry off Vmessage_stack. */
10480
10481 static void
10482 pop_message (void)
10483 {
10484 eassert (CONSP (Vmessage_stack));
10485 Vmessage_stack = XCDR (Vmessage_stack);
10486 }
10487
10488
10489 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10490 exits. If the stack is not empty, we have a missing pop_message
10491 somewhere. */
10492
10493 void
10494 check_message_stack (void)
10495 {
10496 if (!NILP (Vmessage_stack))
10497 emacs_abort ();
10498 }
10499
10500
10501 /* Truncate to NCHARS what will be displayed in the echo area the next
10502 time we display it---but don't redisplay it now. */
10503
10504 void
10505 truncate_echo_area (ptrdiff_t nchars)
10506 {
10507 if (nchars == 0)
10508 echo_area_buffer[0] = Qnil;
10509 else if (!noninteractive
10510 && INTERACTIVE
10511 && !NILP (echo_area_buffer[0]))
10512 {
10513 struct frame *sf = SELECTED_FRAME ();
10514 /* Error messages get reported properly by cmd_error, so this must be
10515 just an informative message; if the frame hasn't really been
10516 initialized yet, just toss it. */
10517 if (sf->glyphs_initialized_p)
10518 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10519 }
10520 }
10521
10522
10523 /* Helper function for truncate_echo_area. Truncate the current
10524 message to at most NCHARS characters. */
10525
10526 static int
10527 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10528 {
10529 if (BEG + nchars < Z)
10530 del_range (BEG + nchars, Z);
10531 if (Z == BEG)
10532 echo_area_buffer[0] = Qnil;
10533 return 0;
10534 }
10535
10536 /* Set the current message to STRING. */
10537
10538 static void
10539 set_message (Lisp_Object string)
10540 {
10541 eassert (STRINGP (string));
10542
10543 message_enable_multibyte = STRING_MULTIBYTE (string);
10544
10545 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10546 message_buf_print = 0;
10547 help_echo_showing_p = 0;
10548
10549 if (STRINGP (Vdebug_on_message)
10550 && STRINGP (string)
10551 && fast_string_match (Vdebug_on_message, string) >= 0)
10552 call_debugger (list2 (Qerror, string));
10553 }
10554
10555
10556 /* Helper function for set_message. First argument is ignored and second
10557 argument has the same meaning as for set_message.
10558 This function is called with the echo area buffer being current. */
10559
10560 static int
10561 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10562 {
10563 eassert (STRINGP (string));
10564
10565 /* Change multibyteness of the echo buffer appropriately. */
10566 if (message_enable_multibyte
10567 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10568 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10569
10570 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10571 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10572 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10573
10574 /* Insert new message at BEG. */
10575 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10576
10577 /* This function takes care of single/multibyte conversion.
10578 We just have to ensure that the echo area buffer has the right
10579 setting of enable_multibyte_characters. */
10580 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10581
10582 return 0;
10583 }
10584
10585
10586 /* Clear messages. CURRENT_P non-zero means clear the current
10587 message. LAST_DISPLAYED_P non-zero means clear the message
10588 last displayed. */
10589
10590 void
10591 clear_message (int current_p, int last_displayed_p)
10592 {
10593 if (current_p)
10594 {
10595 echo_area_buffer[0] = Qnil;
10596 message_cleared_p = 1;
10597 }
10598
10599 if (last_displayed_p)
10600 echo_area_buffer[1] = Qnil;
10601
10602 message_buf_print = 0;
10603 }
10604
10605 /* Clear garbaged frames.
10606
10607 This function is used where the old redisplay called
10608 redraw_garbaged_frames which in turn called redraw_frame which in
10609 turn called clear_frame. The call to clear_frame was a source of
10610 flickering. I believe a clear_frame is not necessary. It should
10611 suffice in the new redisplay to invalidate all current matrices,
10612 and ensure a complete redisplay of all windows. */
10613
10614 static void
10615 clear_garbaged_frames (void)
10616 {
10617 if (frame_garbaged)
10618 {
10619 Lisp_Object tail, frame;
10620 int changed_count = 0;
10621
10622 FOR_EACH_FRAME (tail, frame)
10623 {
10624 struct frame *f = XFRAME (frame);
10625
10626 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10627 {
10628 if (f->resized_p)
10629 {
10630 redraw_frame (f);
10631 f->force_flush_display_p = 1;
10632 }
10633 clear_current_matrices (f);
10634 changed_count++;
10635 f->garbaged = 0;
10636 f->resized_p = 0;
10637 }
10638 }
10639
10640 frame_garbaged = 0;
10641 if (changed_count)
10642 ++windows_or_buffers_changed;
10643 }
10644 }
10645
10646
10647 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10648 is non-zero update selected_frame. Value is non-zero if the
10649 mini-windows height has been changed. */
10650
10651 static int
10652 echo_area_display (int update_frame_p)
10653 {
10654 Lisp_Object mini_window;
10655 struct window *w;
10656 struct frame *f;
10657 int window_height_changed_p = 0;
10658 struct frame *sf = SELECTED_FRAME ();
10659
10660 mini_window = FRAME_MINIBUF_WINDOW (sf);
10661 w = XWINDOW (mini_window);
10662 f = XFRAME (WINDOW_FRAME (w));
10663
10664 /* Don't display if frame is invisible or not yet initialized. */
10665 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10666 return 0;
10667
10668 #ifdef HAVE_WINDOW_SYSTEM
10669 /* When Emacs starts, selected_frame may be the initial terminal
10670 frame. If we let this through, a message would be displayed on
10671 the terminal. */
10672 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10673 return 0;
10674 #endif /* HAVE_WINDOW_SYSTEM */
10675
10676 /* Redraw garbaged frames. */
10677 clear_garbaged_frames ();
10678
10679 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10680 {
10681 echo_area_window = mini_window;
10682 window_height_changed_p = display_echo_area (w);
10683 w->must_be_updated_p = 1;
10684
10685 /* Update the display, unless called from redisplay_internal.
10686 Also don't update the screen during redisplay itself. The
10687 update will happen at the end of redisplay, and an update
10688 here could cause confusion. */
10689 if (update_frame_p && !redisplaying_p)
10690 {
10691 int n = 0;
10692
10693 /* If the display update has been interrupted by pending
10694 input, update mode lines in the frame. Due to the
10695 pending input, it might have been that redisplay hasn't
10696 been called, so that mode lines above the echo area are
10697 garbaged. This looks odd, so we prevent it here. */
10698 if (!display_completed)
10699 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10700
10701 if (window_height_changed_p
10702 /* Don't do this if Emacs is shutting down. Redisplay
10703 needs to run hooks. */
10704 && !NILP (Vrun_hooks))
10705 {
10706 /* Must update other windows. Likewise as in other
10707 cases, don't let this update be interrupted by
10708 pending input. */
10709 ptrdiff_t count = SPECPDL_INDEX ();
10710 specbind (Qredisplay_dont_pause, Qt);
10711 windows_or_buffers_changed = 1;
10712 redisplay_internal ();
10713 unbind_to (count, Qnil);
10714 }
10715 else if (FRAME_WINDOW_P (f) && n == 0)
10716 {
10717 /* Window configuration is the same as before.
10718 Can do with a display update of the echo area,
10719 unless we displayed some mode lines. */
10720 update_single_window (w, 1);
10721 FRAME_RIF (f)->flush_display (f);
10722 }
10723 else
10724 update_frame (f, 1, 1);
10725
10726 /* If cursor is in the echo area, make sure that the next
10727 redisplay displays the minibuffer, so that the cursor will
10728 be replaced with what the minibuffer wants. */
10729 if (cursor_in_echo_area)
10730 ++windows_or_buffers_changed;
10731 }
10732 }
10733 else if (!EQ (mini_window, selected_window))
10734 windows_or_buffers_changed++;
10735
10736 /* Last displayed message is now the current message. */
10737 echo_area_buffer[1] = echo_area_buffer[0];
10738 /* Inform read_char that we're not echoing. */
10739 echo_message_buffer = Qnil;
10740
10741 /* Prevent redisplay optimization in redisplay_internal by resetting
10742 this_line_start_pos. This is done because the mini-buffer now
10743 displays the message instead of its buffer text. */
10744 if (EQ (mini_window, selected_window))
10745 CHARPOS (this_line_start_pos) = 0;
10746
10747 return window_height_changed_p;
10748 }
10749
10750 /* Nonzero if the current window's buffer is shown in more than one
10751 window and was modified since last redisplay. */
10752
10753 static int
10754 buffer_shared_and_changed (void)
10755 {
10756 return (buffer_window_count (current_buffer) > 1
10757 && UNCHANGED_MODIFIED < MODIFF);
10758 }
10759
10760 /* Nonzero if W doesn't reflect the actual state of current buffer due
10761 to its text or overlays change. FIXME: this may be called when
10762 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10763
10764 static int
10765 window_outdated (struct window *w)
10766 {
10767 return (w->last_modified < MODIFF
10768 || w->last_overlay_modified < OVERLAY_MODIFF);
10769 }
10770
10771 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10772 is enabled and mark of W's buffer was changed since last W's update. */
10773
10774 static int
10775 window_buffer_changed (struct window *w)
10776 {
10777 struct buffer *b = XBUFFER (w->contents);
10778
10779 eassert (BUFFER_LIVE_P (b));
10780
10781 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10782 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10783 != (w->region_showing != 0)));
10784 }
10785
10786 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10787
10788 static int
10789 mode_line_update_needed (struct window *w)
10790 {
10791 return (w->column_number_displayed != -1
10792 && !(PT == w->last_point && !window_outdated (w))
10793 && (w->column_number_displayed != current_column ()));
10794 }
10795
10796 /***********************************************************************
10797 Mode Lines and Frame Titles
10798 ***********************************************************************/
10799
10800 /* A buffer for constructing non-propertized mode-line strings and
10801 frame titles in it; allocated from the heap in init_xdisp and
10802 resized as needed in store_mode_line_noprop_char. */
10803
10804 static char *mode_line_noprop_buf;
10805
10806 /* The buffer's end, and a current output position in it. */
10807
10808 static char *mode_line_noprop_buf_end;
10809 static char *mode_line_noprop_ptr;
10810
10811 #define MODE_LINE_NOPROP_LEN(start) \
10812 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10813
10814 static enum {
10815 MODE_LINE_DISPLAY = 0,
10816 MODE_LINE_TITLE,
10817 MODE_LINE_NOPROP,
10818 MODE_LINE_STRING
10819 } mode_line_target;
10820
10821 /* Alist that caches the results of :propertize.
10822 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10823 static Lisp_Object mode_line_proptrans_alist;
10824
10825 /* List of strings making up the mode-line. */
10826 static Lisp_Object mode_line_string_list;
10827
10828 /* Base face property when building propertized mode line string. */
10829 static Lisp_Object mode_line_string_face;
10830 static Lisp_Object mode_line_string_face_prop;
10831
10832
10833 /* Unwind data for mode line strings */
10834
10835 static Lisp_Object Vmode_line_unwind_vector;
10836
10837 static Lisp_Object
10838 format_mode_line_unwind_data (struct frame *target_frame,
10839 struct buffer *obuf,
10840 Lisp_Object owin,
10841 int save_proptrans)
10842 {
10843 Lisp_Object vector, tmp;
10844
10845 /* Reduce consing by keeping one vector in
10846 Vwith_echo_area_save_vector. */
10847 vector = Vmode_line_unwind_vector;
10848 Vmode_line_unwind_vector = Qnil;
10849
10850 if (NILP (vector))
10851 vector = Fmake_vector (make_number (10), Qnil);
10852
10853 ASET (vector, 0, make_number (mode_line_target));
10854 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10855 ASET (vector, 2, mode_line_string_list);
10856 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10857 ASET (vector, 4, mode_line_string_face);
10858 ASET (vector, 5, mode_line_string_face_prop);
10859
10860 if (obuf)
10861 XSETBUFFER (tmp, obuf);
10862 else
10863 tmp = Qnil;
10864 ASET (vector, 6, tmp);
10865 ASET (vector, 7, owin);
10866 if (target_frame)
10867 {
10868 /* Similarly to `with-selected-window', if the operation selects
10869 a window on another frame, we must restore that frame's
10870 selected window, and (for a tty) the top-frame. */
10871 ASET (vector, 8, target_frame->selected_window);
10872 if (FRAME_TERMCAP_P (target_frame))
10873 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10874 }
10875
10876 return vector;
10877 }
10878
10879 static Lisp_Object
10880 unwind_format_mode_line (Lisp_Object vector)
10881 {
10882 Lisp_Object old_window = AREF (vector, 7);
10883 Lisp_Object target_frame_window = AREF (vector, 8);
10884 Lisp_Object old_top_frame = AREF (vector, 9);
10885
10886 mode_line_target = XINT (AREF (vector, 0));
10887 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10888 mode_line_string_list = AREF (vector, 2);
10889 if (! EQ (AREF (vector, 3), Qt))
10890 mode_line_proptrans_alist = AREF (vector, 3);
10891 mode_line_string_face = AREF (vector, 4);
10892 mode_line_string_face_prop = AREF (vector, 5);
10893
10894 /* Select window before buffer, since it may change the buffer. */
10895 if (!NILP (old_window))
10896 {
10897 /* If the operation that we are unwinding had selected a window
10898 on a different frame, reset its frame-selected-window. For a
10899 text terminal, reset its top-frame if necessary. */
10900 if (!NILP (target_frame_window))
10901 {
10902 Lisp_Object frame
10903 = WINDOW_FRAME (XWINDOW (target_frame_window));
10904
10905 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10906 Fselect_window (target_frame_window, Qt);
10907
10908 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10909 Fselect_frame (old_top_frame, Qt);
10910 }
10911
10912 Fselect_window (old_window, Qt);
10913 }
10914
10915 if (!NILP (AREF (vector, 6)))
10916 {
10917 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10918 ASET (vector, 6, Qnil);
10919 }
10920
10921 Vmode_line_unwind_vector = vector;
10922 return Qnil;
10923 }
10924
10925
10926 /* Store a single character C for the frame title in mode_line_noprop_buf.
10927 Re-allocate mode_line_noprop_buf if necessary. */
10928
10929 static void
10930 store_mode_line_noprop_char (char c)
10931 {
10932 /* If output position has reached the end of the allocated buffer,
10933 increase the buffer's size. */
10934 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10935 {
10936 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10937 ptrdiff_t size = len;
10938 mode_line_noprop_buf =
10939 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10940 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10941 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10942 }
10943
10944 *mode_line_noprop_ptr++ = c;
10945 }
10946
10947
10948 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10949 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10950 characters that yield more columns than PRECISION; PRECISION <= 0
10951 means copy the whole string. Pad with spaces until FIELD_WIDTH
10952 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10953 pad. Called from display_mode_element when it is used to build a
10954 frame title. */
10955
10956 static int
10957 store_mode_line_noprop (const char *string, int field_width, int precision)
10958 {
10959 const unsigned char *str = (const unsigned char *) string;
10960 int n = 0;
10961 ptrdiff_t dummy, nbytes;
10962
10963 /* Copy at most PRECISION chars from STR. */
10964 nbytes = strlen (string);
10965 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10966 while (nbytes--)
10967 store_mode_line_noprop_char (*str++);
10968
10969 /* Fill up with spaces until FIELD_WIDTH reached. */
10970 while (field_width > 0
10971 && n < field_width)
10972 {
10973 store_mode_line_noprop_char (' ');
10974 ++n;
10975 }
10976
10977 return n;
10978 }
10979
10980 /***********************************************************************
10981 Frame Titles
10982 ***********************************************************************/
10983
10984 #ifdef HAVE_WINDOW_SYSTEM
10985
10986 /* Set the title of FRAME, if it has changed. The title format is
10987 Vicon_title_format if FRAME is iconified, otherwise it is
10988 frame_title_format. */
10989
10990 static void
10991 x_consider_frame_title (Lisp_Object frame)
10992 {
10993 struct frame *f = XFRAME (frame);
10994
10995 if (FRAME_WINDOW_P (f)
10996 || FRAME_MINIBUF_ONLY_P (f)
10997 || f->explicit_name)
10998 {
10999 /* Do we have more than one visible frame on this X display? */
11000 Lisp_Object tail, other_frame, fmt;
11001 ptrdiff_t title_start;
11002 char *title;
11003 ptrdiff_t len;
11004 struct it it;
11005 ptrdiff_t count = SPECPDL_INDEX ();
11006
11007 FOR_EACH_FRAME (tail, other_frame)
11008 {
11009 struct frame *tf = XFRAME (other_frame);
11010
11011 if (tf != f
11012 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11013 && !FRAME_MINIBUF_ONLY_P (tf)
11014 && !EQ (other_frame, tip_frame)
11015 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11016 break;
11017 }
11018
11019 /* Set global variable indicating that multiple frames exist. */
11020 multiple_frames = CONSP (tail);
11021
11022 /* Switch to the buffer of selected window of the frame. Set up
11023 mode_line_target so that display_mode_element will output into
11024 mode_line_noprop_buf; then display the title. */
11025 record_unwind_protect (unwind_format_mode_line,
11026 format_mode_line_unwind_data
11027 (f, current_buffer, selected_window, 0));
11028
11029 Fselect_window (f->selected_window, Qt);
11030 set_buffer_internal_1
11031 (XBUFFER (XWINDOW (f->selected_window)->contents));
11032 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11033
11034 mode_line_target = MODE_LINE_TITLE;
11035 title_start = MODE_LINE_NOPROP_LEN (0);
11036 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11037 NULL, DEFAULT_FACE_ID);
11038 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11039 len = MODE_LINE_NOPROP_LEN (title_start);
11040 title = mode_line_noprop_buf + title_start;
11041 unbind_to (count, Qnil);
11042
11043 /* Set the title only if it's changed. This avoids consing in
11044 the common case where it hasn't. (If it turns out that we've
11045 already wasted too much time by walking through the list with
11046 display_mode_element, then we might need to optimize at a
11047 higher level than this.) */
11048 if (! STRINGP (f->name)
11049 || SBYTES (f->name) != len
11050 || memcmp (title, SDATA (f->name), len) != 0)
11051 x_implicitly_set_name (f, make_string (title, len), Qnil);
11052 }
11053 }
11054
11055 #endif /* not HAVE_WINDOW_SYSTEM */
11056
11057 \f
11058 /***********************************************************************
11059 Menu Bars
11060 ***********************************************************************/
11061
11062
11063 /* Prepare for redisplay by updating menu-bar item lists when
11064 appropriate. This can call eval. */
11065
11066 void
11067 prepare_menu_bars (void)
11068 {
11069 int all_windows;
11070 struct gcpro gcpro1, gcpro2;
11071 struct frame *f;
11072 Lisp_Object tooltip_frame;
11073
11074 #ifdef HAVE_WINDOW_SYSTEM
11075 tooltip_frame = tip_frame;
11076 #else
11077 tooltip_frame = Qnil;
11078 #endif
11079
11080 /* Update all frame titles based on their buffer names, etc. We do
11081 this before the menu bars so that the buffer-menu will show the
11082 up-to-date frame titles. */
11083 #ifdef HAVE_WINDOW_SYSTEM
11084 if (windows_or_buffers_changed || update_mode_lines)
11085 {
11086 Lisp_Object tail, frame;
11087
11088 FOR_EACH_FRAME (tail, frame)
11089 {
11090 f = XFRAME (frame);
11091 if (!EQ (frame, tooltip_frame)
11092 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11093 x_consider_frame_title (frame);
11094 }
11095 }
11096 #endif /* HAVE_WINDOW_SYSTEM */
11097
11098 /* Update the menu bar item lists, if appropriate. This has to be
11099 done before any actual redisplay or generation of display lines. */
11100 all_windows = (update_mode_lines
11101 || buffer_shared_and_changed ()
11102 || windows_or_buffers_changed);
11103 if (all_windows)
11104 {
11105 Lisp_Object tail, frame;
11106 ptrdiff_t count = SPECPDL_INDEX ();
11107 /* 1 means that update_menu_bar has run its hooks
11108 so any further calls to update_menu_bar shouldn't do so again. */
11109 int menu_bar_hooks_run = 0;
11110
11111 record_unwind_save_match_data ();
11112
11113 FOR_EACH_FRAME (tail, frame)
11114 {
11115 f = XFRAME (frame);
11116
11117 /* Ignore tooltip frame. */
11118 if (EQ (frame, tooltip_frame))
11119 continue;
11120
11121 /* If a window on this frame changed size, report that to
11122 the user and clear the size-change flag. */
11123 if (FRAME_WINDOW_SIZES_CHANGED (f))
11124 {
11125 Lisp_Object functions;
11126
11127 /* Clear flag first in case we get an error below. */
11128 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11129 functions = Vwindow_size_change_functions;
11130 GCPRO2 (tail, functions);
11131
11132 while (CONSP (functions))
11133 {
11134 if (!EQ (XCAR (functions), Qt))
11135 call1 (XCAR (functions), frame);
11136 functions = XCDR (functions);
11137 }
11138 UNGCPRO;
11139 }
11140
11141 GCPRO1 (tail);
11142 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11143 #ifdef HAVE_WINDOW_SYSTEM
11144 update_tool_bar (f, 0);
11145 #endif
11146 #ifdef HAVE_NS
11147 if (windows_or_buffers_changed
11148 && FRAME_NS_P (f))
11149 ns_set_doc_edited
11150 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11151 #endif
11152 UNGCPRO;
11153 }
11154
11155 unbind_to (count, Qnil);
11156 }
11157 else
11158 {
11159 struct frame *sf = SELECTED_FRAME ();
11160 update_menu_bar (sf, 1, 0);
11161 #ifdef HAVE_WINDOW_SYSTEM
11162 update_tool_bar (sf, 1);
11163 #endif
11164 }
11165 }
11166
11167
11168 /* Update the menu bar item list for frame F. This has to be done
11169 before we start to fill in any display lines, because it can call
11170 eval.
11171
11172 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11173
11174 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11175 already ran the menu bar hooks for this redisplay, so there
11176 is no need to run them again. The return value is the
11177 updated value of this flag, to pass to the next call. */
11178
11179 static int
11180 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11181 {
11182 Lisp_Object window;
11183 register struct window *w;
11184
11185 /* If called recursively during a menu update, do nothing. This can
11186 happen when, for instance, an activate-menubar-hook causes a
11187 redisplay. */
11188 if (inhibit_menubar_update)
11189 return hooks_run;
11190
11191 window = FRAME_SELECTED_WINDOW (f);
11192 w = XWINDOW (window);
11193
11194 if (FRAME_WINDOW_P (f)
11195 ?
11196 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11197 || defined (HAVE_NS) || defined (USE_GTK)
11198 FRAME_EXTERNAL_MENU_BAR (f)
11199 #else
11200 FRAME_MENU_BAR_LINES (f) > 0
11201 #endif
11202 : FRAME_MENU_BAR_LINES (f) > 0)
11203 {
11204 /* If the user has switched buffers or windows, we need to
11205 recompute to reflect the new bindings. But we'll
11206 recompute when update_mode_lines is set too; that means
11207 that people can use force-mode-line-update to request
11208 that the menu bar be recomputed. The adverse effect on
11209 the rest of the redisplay algorithm is about the same as
11210 windows_or_buffers_changed anyway. */
11211 if (windows_or_buffers_changed
11212 /* This used to test w->update_mode_line, but we believe
11213 there is no need to recompute the menu in that case. */
11214 || update_mode_lines
11215 || window_buffer_changed (w))
11216 {
11217 struct buffer *prev = current_buffer;
11218 ptrdiff_t count = SPECPDL_INDEX ();
11219
11220 specbind (Qinhibit_menubar_update, Qt);
11221
11222 set_buffer_internal_1 (XBUFFER (w->contents));
11223 if (save_match_data)
11224 record_unwind_save_match_data ();
11225 if (NILP (Voverriding_local_map_menu_flag))
11226 {
11227 specbind (Qoverriding_terminal_local_map, Qnil);
11228 specbind (Qoverriding_local_map, Qnil);
11229 }
11230
11231 if (!hooks_run)
11232 {
11233 /* Run the Lucid hook. */
11234 safe_run_hooks (Qactivate_menubar_hook);
11235
11236 /* If it has changed current-menubar from previous value,
11237 really recompute the menu-bar from the value. */
11238 if (! NILP (Vlucid_menu_bar_dirty_flag))
11239 call0 (Qrecompute_lucid_menubar);
11240
11241 safe_run_hooks (Qmenu_bar_update_hook);
11242
11243 hooks_run = 1;
11244 }
11245
11246 XSETFRAME (Vmenu_updating_frame, f);
11247 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11248
11249 /* Redisplay the menu bar in case we changed it. */
11250 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11251 || defined (HAVE_NS) || defined (USE_GTK)
11252 if (FRAME_WINDOW_P (f))
11253 {
11254 #if defined (HAVE_NS)
11255 /* All frames on Mac OS share the same menubar. So only
11256 the selected frame should be allowed to set it. */
11257 if (f == SELECTED_FRAME ())
11258 #endif
11259 set_frame_menubar (f, 0, 0);
11260 }
11261 else
11262 /* On a terminal screen, the menu bar is an ordinary screen
11263 line, and this makes it get updated. */
11264 w->update_mode_line = 1;
11265 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11266 /* In the non-toolkit version, the menu bar is an ordinary screen
11267 line, and this makes it get updated. */
11268 w->update_mode_line = 1;
11269 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11270
11271 unbind_to (count, Qnil);
11272 set_buffer_internal_1 (prev);
11273 }
11274 }
11275
11276 return hooks_run;
11277 }
11278
11279
11280 \f
11281 /***********************************************************************
11282 Output Cursor
11283 ***********************************************************************/
11284
11285 #ifdef HAVE_WINDOW_SYSTEM
11286
11287 /* EXPORT:
11288 Nominal cursor position -- where to draw output.
11289 HPOS and VPOS are window relative glyph matrix coordinates.
11290 X and Y are window relative pixel coordinates. */
11291
11292 struct cursor_pos output_cursor;
11293
11294
11295 /* EXPORT:
11296 Set the global variable output_cursor to CURSOR. All cursor
11297 positions are relative to updated_window. */
11298
11299 void
11300 set_output_cursor (struct cursor_pos *cursor)
11301 {
11302 output_cursor.hpos = cursor->hpos;
11303 output_cursor.vpos = cursor->vpos;
11304 output_cursor.x = cursor->x;
11305 output_cursor.y = cursor->y;
11306 }
11307
11308
11309 /* EXPORT for RIF:
11310 Set a nominal cursor position.
11311
11312 HPOS and VPOS are column/row positions in a window glyph matrix. X
11313 and Y are window text area relative pixel positions.
11314
11315 If this is done during an update, updated_window will contain the
11316 window that is being updated and the position is the future output
11317 cursor position for that window. If updated_window is null, use
11318 selected_window and display the cursor at the given position. */
11319
11320 void
11321 x_cursor_to (int vpos, int hpos, int y, int x)
11322 {
11323 struct window *w;
11324
11325 /* If updated_window is not set, work on selected_window. */
11326 if (updated_window)
11327 w = updated_window;
11328 else
11329 w = XWINDOW (selected_window);
11330
11331 /* Set the output cursor. */
11332 output_cursor.hpos = hpos;
11333 output_cursor.vpos = vpos;
11334 output_cursor.x = x;
11335 output_cursor.y = y;
11336
11337 /* If not called as part of an update, really display the cursor.
11338 This will also set the cursor position of W. */
11339 if (updated_window == NULL)
11340 {
11341 block_input ();
11342 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11343 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11344 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11345 unblock_input ();
11346 }
11347 }
11348
11349 #endif /* HAVE_WINDOW_SYSTEM */
11350
11351 \f
11352 /***********************************************************************
11353 Tool-bars
11354 ***********************************************************************/
11355
11356 #ifdef HAVE_WINDOW_SYSTEM
11357
11358 /* Where the mouse was last time we reported a mouse event. */
11359
11360 FRAME_PTR last_mouse_frame;
11361
11362 /* Tool-bar item index of the item on which a mouse button was pressed
11363 or -1. */
11364
11365 int last_tool_bar_item;
11366
11367 /* Select `frame' temporarily without running all the code in
11368 do_switch_frame.
11369 FIXME: Maybe do_switch_frame should be trimmed down similarly
11370 when `norecord' is set. */
11371 static Lisp_Object
11372 fast_set_selected_frame (Lisp_Object frame)
11373 {
11374 if (!EQ (selected_frame, frame))
11375 {
11376 selected_frame = frame;
11377 selected_window = XFRAME (frame)->selected_window;
11378 }
11379 return Qnil;
11380 }
11381
11382 /* Update the tool-bar item list for frame F. This has to be done
11383 before we start to fill in any display lines. Called from
11384 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11385 and restore it here. */
11386
11387 static void
11388 update_tool_bar (struct frame *f, int save_match_data)
11389 {
11390 #if defined (USE_GTK) || defined (HAVE_NS)
11391 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11392 #else
11393 int do_update = WINDOWP (f->tool_bar_window)
11394 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11395 #endif
11396
11397 if (do_update)
11398 {
11399 Lisp_Object window;
11400 struct window *w;
11401
11402 window = FRAME_SELECTED_WINDOW (f);
11403 w = XWINDOW (window);
11404
11405 /* If the user has switched buffers or windows, we need to
11406 recompute to reflect the new bindings. But we'll
11407 recompute when update_mode_lines is set too; that means
11408 that people can use force-mode-line-update to request
11409 that the menu bar be recomputed. The adverse effect on
11410 the rest of the redisplay algorithm is about the same as
11411 windows_or_buffers_changed anyway. */
11412 if (windows_or_buffers_changed
11413 || w->update_mode_line
11414 || update_mode_lines
11415 || window_buffer_changed (w))
11416 {
11417 struct buffer *prev = current_buffer;
11418 ptrdiff_t count = SPECPDL_INDEX ();
11419 Lisp_Object frame, new_tool_bar;
11420 int new_n_tool_bar;
11421 struct gcpro gcpro1;
11422
11423 /* Set current_buffer to the buffer of the selected
11424 window of the frame, so that we get the right local
11425 keymaps. */
11426 set_buffer_internal_1 (XBUFFER (w->contents));
11427
11428 /* Save match data, if we must. */
11429 if (save_match_data)
11430 record_unwind_save_match_data ();
11431
11432 /* Make sure that we don't accidentally use bogus keymaps. */
11433 if (NILP (Voverriding_local_map_menu_flag))
11434 {
11435 specbind (Qoverriding_terminal_local_map, Qnil);
11436 specbind (Qoverriding_local_map, Qnil);
11437 }
11438
11439 GCPRO1 (new_tool_bar);
11440
11441 /* We must temporarily set the selected frame to this frame
11442 before calling tool_bar_items, because the calculation of
11443 the tool-bar keymap uses the selected frame (see
11444 `tool-bar-make-keymap' in tool-bar.el). */
11445 eassert (EQ (selected_window,
11446 /* Since we only explicitly preserve selected_frame,
11447 check that selected_window would be redundant. */
11448 XFRAME (selected_frame)->selected_window));
11449 record_unwind_protect (fast_set_selected_frame, selected_frame);
11450 XSETFRAME (frame, f);
11451 fast_set_selected_frame (frame);
11452
11453 /* Build desired tool-bar items from keymaps. */
11454 new_tool_bar
11455 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11456 &new_n_tool_bar);
11457
11458 /* Redisplay the tool-bar if we changed it. */
11459 if (new_n_tool_bar != f->n_tool_bar_items
11460 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11461 {
11462 /* Redisplay that happens asynchronously due to an expose event
11463 may access f->tool_bar_items. Make sure we update both
11464 variables within BLOCK_INPUT so no such event interrupts. */
11465 block_input ();
11466 fset_tool_bar_items (f, new_tool_bar);
11467 f->n_tool_bar_items = new_n_tool_bar;
11468 w->update_mode_line = 1;
11469 unblock_input ();
11470 }
11471
11472 UNGCPRO;
11473
11474 unbind_to (count, Qnil);
11475 set_buffer_internal_1 (prev);
11476 }
11477 }
11478 }
11479
11480
11481 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11482 F's desired tool-bar contents. F->tool_bar_items must have
11483 been set up previously by calling prepare_menu_bars. */
11484
11485 static void
11486 build_desired_tool_bar_string (struct frame *f)
11487 {
11488 int i, size, size_needed;
11489 struct gcpro gcpro1, gcpro2, gcpro3;
11490 Lisp_Object image, plist, props;
11491
11492 image = plist = props = Qnil;
11493 GCPRO3 (image, plist, props);
11494
11495 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11496 Otherwise, make a new string. */
11497
11498 /* The size of the string we might be able to reuse. */
11499 size = (STRINGP (f->desired_tool_bar_string)
11500 ? SCHARS (f->desired_tool_bar_string)
11501 : 0);
11502
11503 /* We need one space in the string for each image. */
11504 size_needed = f->n_tool_bar_items;
11505
11506 /* Reuse f->desired_tool_bar_string, if possible. */
11507 if (size < size_needed || NILP (f->desired_tool_bar_string))
11508 fset_desired_tool_bar_string
11509 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11510 else
11511 {
11512 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11513 Fremove_text_properties (make_number (0), make_number (size),
11514 props, f->desired_tool_bar_string);
11515 }
11516
11517 /* Put a `display' property on the string for the images to display,
11518 put a `menu_item' property on tool-bar items with a value that
11519 is the index of the item in F's tool-bar item vector. */
11520 for (i = 0; i < f->n_tool_bar_items; ++i)
11521 {
11522 #define PROP(IDX) \
11523 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11524
11525 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11526 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11527 int hmargin, vmargin, relief, idx, end;
11528
11529 /* If image is a vector, choose the image according to the
11530 button state. */
11531 image = PROP (TOOL_BAR_ITEM_IMAGES);
11532 if (VECTORP (image))
11533 {
11534 if (enabled_p)
11535 idx = (selected_p
11536 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11537 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11538 else
11539 idx = (selected_p
11540 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11541 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11542
11543 eassert (ASIZE (image) >= idx);
11544 image = AREF (image, idx);
11545 }
11546 else
11547 idx = -1;
11548
11549 /* Ignore invalid image specifications. */
11550 if (!valid_image_p (image))
11551 continue;
11552
11553 /* Display the tool-bar button pressed, or depressed. */
11554 plist = Fcopy_sequence (XCDR (image));
11555
11556 /* Compute margin and relief to draw. */
11557 relief = (tool_bar_button_relief >= 0
11558 ? tool_bar_button_relief
11559 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11560 hmargin = vmargin = relief;
11561
11562 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11563 INT_MAX - max (hmargin, vmargin)))
11564 {
11565 hmargin += XFASTINT (Vtool_bar_button_margin);
11566 vmargin += XFASTINT (Vtool_bar_button_margin);
11567 }
11568 else if (CONSP (Vtool_bar_button_margin))
11569 {
11570 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11571 INT_MAX - hmargin))
11572 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11573
11574 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11575 INT_MAX - vmargin))
11576 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11577 }
11578
11579 if (auto_raise_tool_bar_buttons_p)
11580 {
11581 /* Add a `:relief' property to the image spec if the item is
11582 selected. */
11583 if (selected_p)
11584 {
11585 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11586 hmargin -= relief;
11587 vmargin -= relief;
11588 }
11589 }
11590 else
11591 {
11592 /* If image is selected, display it pressed, i.e. with a
11593 negative relief. If it's not selected, display it with a
11594 raised relief. */
11595 plist = Fplist_put (plist, QCrelief,
11596 (selected_p
11597 ? make_number (-relief)
11598 : make_number (relief)));
11599 hmargin -= relief;
11600 vmargin -= relief;
11601 }
11602
11603 /* Put a margin around the image. */
11604 if (hmargin || vmargin)
11605 {
11606 if (hmargin == vmargin)
11607 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11608 else
11609 plist = Fplist_put (plist, QCmargin,
11610 Fcons (make_number (hmargin),
11611 make_number (vmargin)));
11612 }
11613
11614 /* If button is not enabled, and we don't have special images
11615 for the disabled state, make the image appear disabled by
11616 applying an appropriate algorithm to it. */
11617 if (!enabled_p && idx < 0)
11618 plist = Fplist_put (plist, QCconversion, Qdisabled);
11619
11620 /* Put a `display' text property on the string for the image to
11621 display. Put a `menu-item' property on the string that gives
11622 the start of this item's properties in the tool-bar items
11623 vector. */
11624 image = Fcons (Qimage, plist);
11625 props = list4 (Qdisplay, image,
11626 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11627
11628 /* Let the last image hide all remaining spaces in the tool bar
11629 string. The string can be longer than needed when we reuse a
11630 previous string. */
11631 if (i + 1 == f->n_tool_bar_items)
11632 end = SCHARS (f->desired_tool_bar_string);
11633 else
11634 end = i + 1;
11635 Fadd_text_properties (make_number (i), make_number (end),
11636 props, f->desired_tool_bar_string);
11637 #undef PROP
11638 }
11639
11640 UNGCPRO;
11641 }
11642
11643
11644 /* Display one line of the tool-bar of frame IT->f.
11645
11646 HEIGHT specifies the desired height of the tool-bar line.
11647 If the actual height of the glyph row is less than HEIGHT, the
11648 row's height is increased to HEIGHT, and the icons are centered
11649 vertically in the new height.
11650
11651 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11652 count a final empty row in case the tool-bar width exactly matches
11653 the window width.
11654 */
11655
11656 static void
11657 display_tool_bar_line (struct it *it, int height)
11658 {
11659 struct glyph_row *row = it->glyph_row;
11660 int max_x = it->last_visible_x;
11661 struct glyph *last;
11662
11663 prepare_desired_row (row);
11664 row->y = it->current_y;
11665
11666 /* Note that this isn't made use of if the face hasn't a box,
11667 so there's no need to check the face here. */
11668 it->start_of_box_run_p = 1;
11669
11670 while (it->current_x < max_x)
11671 {
11672 int x, n_glyphs_before, i, nglyphs;
11673 struct it it_before;
11674
11675 /* Get the next display element. */
11676 if (!get_next_display_element (it))
11677 {
11678 /* Don't count empty row if we are counting needed tool-bar lines. */
11679 if (height < 0 && !it->hpos)
11680 return;
11681 break;
11682 }
11683
11684 /* Produce glyphs. */
11685 n_glyphs_before = row->used[TEXT_AREA];
11686 it_before = *it;
11687
11688 PRODUCE_GLYPHS (it);
11689
11690 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11691 i = 0;
11692 x = it_before.current_x;
11693 while (i < nglyphs)
11694 {
11695 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11696
11697 if (x + glyph->pixel_width > max_x)
11698 {
11699 /* Glyph doesn't fit on line. Backtrack. */
11700 row->used[TEXT_AREA] = n_glyphs_before;
11701 *it = it_before;
11702 /* If this is the only glyph on this line, it will never fit on the
11703 tool-bar, so skip it. But ensure there is at least one glyph,
11704 so we don't accidentally disable the tool-bar. */
11705 if (n_glyphs_before == 0
11706 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11707 break;
11708 goto out;
11709 }
11710
11711 ++it->hpos;
11712 x += glyph->pixel_width;
11713 ++i;
11714 }
11715
11716 /* Stop at line end. */
11717 if (ITERATOR_AT_END_OF_LINE_P (it))
11718 break;
11719
11720 set_iterator_to_next (it, 1);
11721 }
11722
11723 out:;
11724
11725 row->displays_text_p = row->used[TEXT_AREA] != 0;
11726
11727 /* Use default face for the border below the tool bar.
11728
11729 FIXME: When auto-resize-tool-bars is grow-only, there is
11730 no additional border below the possibly empty tool-bar lines.
11731 So to make the extra empty lines look "normal", we have to
11732 use the tool-bar face for the border too. */
11733 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11734 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11735 it->face_id = DEFAULT_FACE_ID;
11736
11737 extend_face_to_end_of_line (it);
11738 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11739 last->right_box_line_p = 1;
11740 if (last == row->glyphs[TEXT_AREA])
11741 last->left_box_line_p = 1;
11742
11743 /* Make line the desired height and center it vertically. */
11744 if ((height -= it->max_ascent + it->max_descent) > 0)
11745 {
11746 /* Don't add more than one line height. */
11747 height %= FRAME_LINE_HEIGHT (it->f);
11748 it->max_ascent += height / 2;
11749 it->max_descent += (height + 1) / 2;
11750 }
11751
11752 compute_line_metrics (it);
11753
11754 /* If line is empty, make it occupy the rest of the tool-bar. */
11755 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11756 {
11757 row->height = row->phys_height = it->last_visible_y - row->y;
11758 row->visible_height = row->height;
11759 row->ascent = row->phys_ascent = 0;
11760 row->extra_line_spacing = 0;
11761 }
11762
11763 row->full_width_p = 1;
11764 row->continued_p = 0;
11765 row->truncated_on_left_p = 0;
11766 row->truncated_on_right_p = 0;
11767
11768 it->current_x = it->hpos = 0;
11769 it->current_y += row->height;
11770 ++it->vpos;
11771 ++it->glyph_row;
11772 }
11773
11774
11775 /* Max tool-bar height. */
11776
11777 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11778 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11779
11780 /* Value is the number of screen lines needed to make all tool-bar
11781 items of frame F visible. The number of actual rows needed is
11782 returned in *N_ROWS if non-NULL. */
11783
11784 static int
11785 tool_bar_lines_needed (struct frame *f, int *n_rows)
11786 {
11787 struct window *w = XWINDOW (f->tool_bar_window);
11788 struct it it;
11789 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11790 the desired matrix, so use (unused) mode-line row as temporary row to
11791 avoid destroying the first tool-bar row. */
11792 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11793
11794 /* Initialize an iterator for iteration over
11795 F->desired_tool_bar_string in the tool-bar window of frame F. */
11796 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11797 it.first_visible_x = 0;
11798 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11799 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11800 it.paragraph_embedding = L2R;
11801
11802 while (!ITERATOR_AT_END_P (&it))
11803 {
11804 clear_glyph_row (temp_row);
11805 it.glyph_row = temp_row;
11806 display_tool_bar_line (&it, -1);
11807 }
11808 clear_glyph_row (temp_row);
11809
11810 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11811 if (n_rows)
11812 *n_rows = it.vpos > 0 ? it.vpos : -1;
11813
11814 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11815 }
11816
11817
11818 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11819 0, 1, 0,
11820 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11821 If FRAME is nil or omitted, use the selected frame. */)
11822 (Lisp_Object frame)
11823 {
11824 struct frame *f = decode_any_frame (frame);
11825 struct window *w;
11826 int nlines = 0;
11827
11828 if (WINDOWP (f->tool_bar_window)
11829 && (w = XWINDOW (f->tool_bar_window),
11830 WINDOW_TOTAL_LINES (w) > 0))
11831 {
11832 update_tool_bar (f, 1);
11833 if (f->n_tool_bar_items)
11834 {
11835 build_desired_tool_bar_string (f);
11836 nlines = tool_bar_lines_needed (f, NULL);
11837 }
11838 }
11839
11840 return make_number (nlines);
11841 }
11842
11843
11844 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11845 height should be changed. */
11846
11847 static int
11848 redisplay_tool_bar (struct frame *f)
11849 {
11850 struct window *w;
11851 struct it it;
11852 struct glyph_row *row;
11853
11854 #if defined (USE_GTK) || defined (HAVE_NS)
11855 if (FRAME_EXTERNAL_TOOL_BAR (f))
11856 update_frame_tool_bar (f);
11857 return 0;
11858 #endif
11859
11860 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11861 do anything. This means you must start with tool-bar-lines
11862 non-zero to get the auto-sizing effect. Or in other words, you
11863 can turn off tool-bars by specifying tool-bar-lines zero. */
11864 if (!WINDOWP (f->tool_bar_window)
11865 || (w = XWINDOW (f->tool_bar_window),
11866 WINDOW_TOTAL_LINES (w) == 0))
11867 return 0;
11868
11869 /* Set up an iterator for the tool-bar window. */
11870 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11871 it.first_visible_x = 0;
11872 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11873 row = it.glyph_row;
11874
11875 /* Build a string that represents the contents of the tool-bar. */
11876 build_desired_tool_bar_string (f);
11877 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11878 /* FIXME: This should be controlled by a user option. But it
11879 doesn't make sense to have an R2L tool bar if the menu bar cannot
11880 be drawn also R2L, and making the menu bar R2L is tricky due
11881 toolkit-specific code that implements it. If an R2L tool bar is
11882 ever supported, display_tool_bar_line should also be augmented to
11883 call unproduce_glyphs like display_line and display_string
11884 do. */
11885 it.paragraph_embedding = L2R;
11886
11887 if (f->n_tool_bar_rows == 0)
11888 {
11889 int nlines;
11890
11891 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11892 nlines != WINDOW_TOTAL_LINES (w)))
11893 {
11894 Lisp_Object frame;
11895 int old_height = WINDOW_TOTAL_LINES (w);
11896
11897 XSETFRAME (frame, f);
11898 Fmodify_frame_parameters (frame,
11899 Fcons (Fcons (Qtool_bar_lines,
11900 make_number (nlines)),
11901 Qnil));
11902 if (WINDOW_TOTAL_LINES (w) != old_height)
11903 {
11904 clear_glyph_matrix (w->desired_matrix);
11905 fonts_changed_p = 1;
11906 return 1;
11907 }
11908 }
11909 }
11910
11911 /* Display as many lines as needed to display all tool-bar items. */
11912
11913 if (f->n_tool_bar_rows > 0)
11914 {
11915 int border, rows, height, extra;
11916
11917 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11918 border = XINT (Vtool_bar_border);
11919 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11920 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11921 else if (EQ (Vtool_bar_border, Qborder_width))
11922 border = f->border_width;
11923 else
11924 border = 0;
11925 if (border < 0)
11926 border = 0;
11927
11928 rows = f->n_tool_bar_rows;
11929 height = max (1, (it.last_visible_y - border) / rows);
11930 extra = it.last_visible_y - border - height * rows;
11931
11932 while (it.current_y < it.last_visible_y)
11933 {
11934 int h = 0;
11935 if (extra > 0 && rows-- > 0)
11936 {
11937 h = (extra + rows - 1) / rows;
11938 extra -= h;
11939 }
11940 display_tool_bar_line (&it, height + h);
11941 }
11942 }
11943 else
11944 {
11945 while (it.current_y < it.last_visible_y)
11946 display_tool_bar_line (&it, 0);
11947 }
11948
11949 /* It doesn't make much sense to try scrolling in the tool-bar
11950 window, so don't do it. */
11951 w->desired_matrix->no_scrolling_p = 1;
11952 w->must_be_updated_p = 1;
11953
11954 if (!NILP (Vauto_resize_tool_bars))
11955 {
11956 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11957 int change_height_p = 0;
11958
11959 /* If we couldn't display everything, change the tool-bar's
11960 height if there is room for more. */
11961 if (IT_STRING_CHARPOS (it) < it.end_charpos
11962 && it.current_y < max_tool_bar_height)
11963 change_height_p = 1;
11964
11965 row = it.glyph_row - 1;
11966
11967 /* If there are blank lines at the end, except for a partially
11968 visible blank line at the end that is smaller than
11969 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11970 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11971 && row->height >= FRAME_LINE_HEIGHT (f))
11972 change_height_p = 1;
11973
11974 /* If row displays tool-bar items, but is partially visible,
11975 change the tool-bar's height. */
11976 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11977 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11978 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11979 change_height_p = 1;
11980
11981 /* Resize windows as needed by changing the `tool-bar-lines'
11982 frame parameter. */
11983 if (change_height_p)
11984 {
11985 Lisp_Object frame;
11986 int old_height = WINDOW_TOTAL_LINES (w);
11987 int nrows;
11988 int nlines = tool_bar_lines_needed (f, &nrows);
11989
11990 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11991 && !f->minimize_tool_bar_window_p)
11992 ? (nlines > old_height)
11993 : (nlines != old_height));
11994 f->minimize_tool_bar_window_p = 0;
11995
11996 if (change_height_p)
11997 {
11998 XSETFRAME (frame, f);
11999 Fmodify_frame_parameters (frame,
12000 Fcons (Fcons (Qtool_bar_lines,
12001 make_number (nlines)),
12002 Qnil));
12003 if (WINDOW_TOTAL_LINES (w) != old_height)
12004 {
12005 clear_glyph_matrix (w->desired_matrix);
12006 f->n_tool_bar_rows = nrows;
12007 fonts_changed_p = 1;
12008 return 1;
12009 }
12010 }
12011 }
12012 }
12013
12014 f->minimize_tool_bar_window_p = 0;
12015 return 0;
12016 }
12017
12018
12019 /* Get information about the tool-bar item which is displayed in GLYPH
12020 on frame F. Return in *PROP_IDX the index where tool-bar item
12021 properties start in F->tool_bar_items. Value is zero if
12022 GLYPH doesn't display a tool-bar item. */
12023
12024 static int
12025 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12026 {
12027 Lisp_Object prop;
12028 int success_p;
12029 int charpos;
12030
12031 /* This function can be called asynchronously, which means we must
12032 exclude any possibility that Fget_text_property signals an
12033 error. */
12034 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12035 charpos = max (0, charpos);
12036
12037 /* Get the text property `menu-item' at pos. The value of that
12038 property is the start index of this item's properties in
12039 F->tool_bar_items. */
12040 prop = Fget_text_property (make_number (charpos),
12041 Qmenu_item, f->current_tool_bar_string);
12042 if (INTEGERP (prop))
12043 {
12044 *prop_idx = XINT (prop);
12045 success_p = 1;
12046 }
12047 else
12048 success_p = 0;
12049
12050 return success_p;
12051 }
12052
12053 \f
12054 /* Get information about the tool-bar item at position X/Y on frame F.
12055 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12056 the current matrix of the tool-bar window of F, or NULL if not
12057 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12058 item in F->tool_bar_items. Value is
12059
12060 -1 if X/Y is not on a tool-bar item
12061 0 if X/Y is on the same item that was highlighted before.
12062 1 otherwise. */
12063
12064 static int
12065 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12066 int *hpos, int *vpos, int *prop_idx)
12067 {
12068 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12069 struct window *w = XWINDOW (f->tool_bar_window);
12070 int area;
12071
12072 /* Find the glyph under X/Y. */
12073 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12074 if (*glyph == NULL)
12075 return -1;
12076
12077 /* Get the start of this tool-bar item's properties in
12078 f->tool_bar_items. */
12079 if (!tool_bar_item_info (f, *glyph, prop_idx))
12080 return -1;
12081
12082 /* Is mouse on the highlighted item? */
12083 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12084 && *vpos >= hlinfo->mouse_face_beg_row
12085 && *vpos <= hlinfo->mouse_face_end_row
12086 && (*vpos > hlinfo->mouse_face_beg_row
12087 || *hpos >= hlinfo->mouse_face_beg_col)
12088 && (*vpos < hlinfo->mouse_face_end_row
12089 || *hpos < hlinfo->mouse_face_end_col
12090 || hlinfo->mouse_face_past_end))
12091 return 0;
12092
12093 return 1;
12094 }
12095
12096
12097 /* EXPORT:
12098 Handle mouse button event on the tool-bar of frame F, at
12099 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12100 0 for button release. MODIFIERS is event modifiers for button
12101 release. */
12102
12103 void
12104 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12105 int modifiers)
12106 {
12107 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12108 struct window *w = XWINDOW (f->tool_bar_window);
12109 int hpos, vpos, prop_idx;
12110 struct glyph *glyph;
12111 Lisp_Object enabled_p;
12112
12113 /* If not on the highlighted tool-bar item, return. */
12114 frame_to_window_pixel_xy (w, &x, &y);
12115 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12116 return;
12117
12118 /* If item is disabled, do nothing. */
12119 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12120 if (NILP (enabled_p))
12121 return;
12122
12123 if (down_p)
12124 {
12125 /* Show item in pressed state. */
12126 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12127 last_tool_bar_item = prop_idx;
12128 }
12129 else
12130 {
12131 Lisp_Object key, frame;
12132 struct input_event event;
12133 EVENT_INIT (event);
12134
12135 /* Show item in released state. */
12136 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12137
12138 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12139
12140 XSETFRAME (frame, f);
12141 event.kind = TOOL_BAR_EVENT;
12142 event.frame_or_window = frame;
12143 event.arg = frame;
12144 kbd_buffer_store_event (&event);
12145
12146 event.kind = TOOL_BAR_EVENT;
12147 event.frame_or_window = frame;
12148 event.arg = key;
12149 event.modifiers = modifiers;
12150 kbd_buffer_store_event (&event);
12151 last_tool_bar_item = -1;
12152 }
12153 }
12154
12155
12156 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12157 tool-bar window-relative coordinates X/Y. Called from
12158 note_mouse_highlight. */
12159
12160 static void
12161 note_tool_bar_highlight (struct frame *f, int x, int y)
12162 {
12163 Lisp_Object window = f->tool_bar_window;
12164 struct window *w = XWINDOW (window);
12165 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12166 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12167 int hpos, vpos;
12168 struct glyph *glyph;
12169 struct glyph_row *row;
12170 int i;
12171 Lisp_Object enabled_p;
12172 int prop_idx;
12173 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12174 int mouse_down_p, rc;
12175
12176 /* Function note_mouse_highlight is called with negative X/Y
12177 values when mouse moves outside of the frame. */
12178 if (x <= 0 || y <= 0)
12179 {
12180 clear_mouse_face (hlinfo);
12181 return;
12182 }
12183
12184 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12185 if (rc < 0)
12186 {
12187 /* Not on tool-bar item. */
12188 clear_mouse_face (hlinfo);
12189 return;
12190 }
12191 else if (rc == 0)
12192 /* On same tool-bar item as before. */
12193 goto set_help_echo;
12194
12195 clear_mouse_face (hlinfo);
12196
12197 /* Mouse is down, but on different tool-bar item? */
12198 mouse_down_p = (dpyinfo->grabbed
12199 && f == last_mouse_frame
12200 && FRAME_LIVE_P (f));
12201 if (mouse_down_p
12202 && last_tool_bar_item != prop_idx)
12203 return;
12204
12205 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12206
12207 /* If tool-bar item is not enabled, don't highlight it. */
12208 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12209 if (!NILP (enabled_p))
12210 {
12211 /* Compute the x-position of the glyph. In front and past the
12212 image is a space. We include this in the highlighted area. */
12213 row = MATRIX_ROW (w->current_matrix, vpos);
12214 for (i = x = 0; i < hpos; ++i)
12215 x += row->glyphs[TEXT_AREA][i].pixel_width;
12216
12217 /* Record this as the current active region. */
12218 hlinfo->mouse_face_beg_col = hpos;
12219 hlinfo->mouse_face_beg_row = vpos;
12220 hlinfo->mouse_face_beg_x = x;
12221 hlinfo->mouse_face_beg_y = row->y;
12222 hlinfo->mouse_face_past_end = 0;
12223
12224 hlinfo->mouse_face_end_col = hpos + 1;
12225 hlinfo->mouse_face_end_row = vpos;
12226 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12227 hlinfo->mouse_face_end_y = row->y;
12228 hlinfo->mouse_face_window = window;
12229 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12230
12231 /* Display it as active. */
12232 show_mouse_face (hlinfo, draw);
12233 }
12234
12235 set_help_echo:
12236
12237 /* Set help_echo_string to a help string to display for this tool-bar item.
12238 XTread_socket does the rest. */
12239 help_echo_object = help_echo_window = Qnil;
12240 help_echo_pos = -1;
12241 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12242 if (NILP (help_echo_string))
12243 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12244 }
12245
12246 #endif /* HAVE_WINDOW_SYSTEM */
12247
12248
12249 \f
12250 /************************************************************************
12251 Horizontal scrolling
12252 ************************************************************************/
12253
12254 static int hscroll_window_tree (Lisp_Object);
12255 static int hscroll_windows (Lisp_Object);
12256
12257 /* For all leaf windows in the window tree rooted at WINDOW, set their
12258 hscroll value so that PT is (i) visible in the window, and (ii) so
12259 that it is not within a certain margin at the window's left and
12260 right border. Value is non-zero if any window's hscroll has been
12261 changed. */
12262
12263 static int
12264 hscroll_window_tree (Lisp_Object window)
12265 {
12266 int hscrolled_p = 0;
12267 int hscroll_relative_p = FLOATP (Vhscroll_step);
12268 int hscroll_step_abs = 0;
12269 double hscroll_step_rel = 0;
12270
12271 if (hscroll_relative_p)
12272 {
12273 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12274 if (hscroll_step_rel < 0)
12275 {
12276 hscroll_relative_p = 0;
12277 hscroll_step_abs = 0;
12278 }
12279 }
12280 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12281 {
12282 hscroll_step_abs = XINT (Vhscroll_step);
12283 if (hscroll_step_abs < 0)
12284 hscroll_step_abs = 0;
12285 }
12286 else
12287 hscroll_step_abs = 0;
12288
12289 while (WINDOWP (window))
12290 {
12291 struct window *w = XWINDOW (window);
12292
12293 if (WINDOWP (w->contents))
12294 hscrolled_p |= hscroll_window_tree (w->contents);
12295 else if (w->cursor.vpos >= 0)
12296 {
12297 int h_margin;
12298 int text_area_width;
12299 struct glyph_row *current_cursor_row
12300 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12301 struct glyph_row *desired_cursor_row
12302 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12303 struct glyph_row *cursor_row
12304 = (desired_cursor_row->enabled_p
12305 ? desired_cursor_row
12306 : current_cursor_row);
12307 int row_r2l_p = cursor_row->reversed_p;
12308
12309 text_area_width = window_box_width (w, TEXT_AREA);
12310
12311 /* Scroll when cursor is inside this scroll margin. */
12312 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12313
12314 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12315 /* For left-to-right rows, hscroll when cursor is either
12316 (i) inside the right hscroll margin, or (ii) if it is
12317 inside the left margin and the window is already
12318 hscrolled. */
12319 && ((!row_r2l_p
12320 && ((w->hscroll
12321 && w->cursor.x <= h_margin)
12322 || (cursor_row->enabled_p
12323 && cursor_row->truncated_on_right_p
12324 && (w->cursor.x >= text_area_width - h_margin))))
12325 /* For right-to-left rows, the logic is similar,
12326 except that rules for scrolling to left and right
12327 are reversed. E.g., if cursor.x <= h_margin, we
12328 need to hscroll "to the right" unconditionally,
12329 and that will scroll the screen to the left so as
12330 to reveal the next portion of the row. */
12331 || (row_r2l_p
12332 && ((cursor_row->enabled_p
12333 /* FIXME: It is confusing to set the
12334 truncated_on_right_p flag when R2L rows
12335 are actually truncated on the left. */
12336 && cursor_row->truncated_on_right_p
12337 && w->cursor.x <= h_margin)
12338 || (w->hscroll
12339 && (w->cursor.x >= text_area_width - h_margin))))))
12340 {
12341 struct it it;
12342 ptrdiff_t hscroll;
12343 struct buffer *saved_current_buffer;
12344 ptrdiff_t pt;
12345 int wanted_x;
12346
12347 /* Find point in a display of infinite width. */
12348 saved_current_buffer = current_buffer;
12349 current_buffer = XBUFFER (w->contents);
12350
12351 if (w == XWINDOW (selected_window))
12352 pt = PT;
12353 else
12354 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12355
12356 /* Move iterator to pt starting at cursor_row->start in
12357 a line with infinite width. */
12358 init_to_row_start (&it, w, cursor_row);
12359 it.last_visible_x = INFINITY;
12360 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12361 current_buffer = saved_current_buffer;
12362
12363 /* Position cursor in window. */
12364 if (!hscroll_relative_p && hscroll_step_abs == 0)
12365 hscroll = max (0, (it.current_x
12366 - (ITERATOR_AT_END_OF_LINE_P (&it)
12367 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12368 : (text_area_width / 2))))
12369 / FRAME_COLUMN_WIDTH (it.f);
12370 else if ((!row_r2l_p
12371 && w->cursor.x >= text_area_width - h_margin)
12372 || (row_r2l_p && w->cursor.x <= h_margin))
12373 {
12374 if (hscroll_relative_p)
12375 wanted_x = text_area_width * (1 - hscroll_step_rel)
12376 - h_margin;
12377 else
12378 wanted_x = text_area_width
12379 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12380 - h_margin;
12381 hscroll
12382 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12383 }
12384 else
12385 {
12386 if (hscroll_relative_p)
12387 wanted_x = text_area_width * hscroll_step_rel
12388 + h_margin;
12389 else
12390 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12391 + h_margin;
12392 hscroll
12393 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12394 }
12395 hscroll = max (hscroll, w->min_hscroll);
12396
12397 /* Don't prevent redisplay optimizations if hscroll
12398 hasn't changed, as it will unnecessarily slow down
12399 redisplay. */
12400 if (w->hscroll != hscroll)
12401 {
12402 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12403 w->hscroll = hscroll;
12404 hscrolled_p = 1;
12405 }
12406 }
12407 }
12408
12409 window = w->next;
12410 }
12411
12412 /* Value is non-zero if hscroll of any leaf window has been changed. */
12413 return hscrolled_p;
12414 }
12415
12416
12417 /* Set hscroll so that cursor is visible and not inside horizontal
12418 scroll margins for all windows in the tree rooted at WINDOW. See
12419 also hscroll_window_tree above. Value is non-zero if any window's
12420 hscroll has been changed. If it has, desired matrices on the frame
12421 of WINDOW are cleared. */
12422
12423 static int
12424 hscroll_windows (Lisp_Object window)
12425 {
12426 int hscrolled_p = hscroll_window_tree (window);
12427 if (hscrolled_p)
12428 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12429 return hscrolled_p;
12430 }
12431
12432
12433 \f
12434 /************************************************************************
12435 Redisplay
12436 ************************************************************************/
12437
12438 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12439 to a non-zero value. This is sometimes handy to have in a debugger
12440 session. */
12441
12442 #ifdef GLYPH_DEBUG
12443
12444 /* First and last unchanged row for try_window_id. */
12445
12446 static int debug_first_unchanged_at_end_vpos;
12447 static int debug_last_unchanged_at_beg_vpos;
12448
12449 /* Delta vpos and y. */
12450
12451 static int debug_dvpos, debug_dy;
12452
12453 /* Delta in characters and bytes for try_window_id. */
12454
12455 static ptrdiff_t debug_delta, debug_delta_bytes;
12456
12457 /* Values of window_end_pos and window_end_vpos at the end of
12458 try_window_id. */
12459
12460 static ptrdiff_t debug_end_vpos;
12461
12462 /* Append a string to W->desired_matrix->method. FMT is a printf
12463 format string. If trace_redisplay_p is non-zero also printf the
12464 resulting string to stderr. */
12465
12466 static void debug_method_add (struct window *, char const *, ...)
12467 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12468
12469 static void
12470 debug_method_add (struct window *w, char const *fmt, ...)
12471 {
12472 char *method = w->desired_matrix->method;
12473 int len = strlen (method);
12474 int size = sizeof w->desired_matrix->method;
12475 int remaining = size - len - 1;
12476 va_list ap;
12477
12478 if (len && remaining)
12479 {
12480 method[len] = '|';
12481 --remaining, ++len;
12482 }
12483
12484 va_start (ap, fmt);
12485 vsnprintf (method + len, remaining + 1, fmt, ap);
12486 va_end (ap);
12487
12488 if (trace_redisplay_p)
12489 fprintf (stderr, "%p (%s): %s\n",
12490 w,
12491 ((BUFFERP (w->contents)
12492 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12493 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12494 : "no buffer"),
12495 method + len);
12496 }
12497
12498 #endif /* GLYPH_DEBUG */
12499
12500
12501 /* Value is non-zero if all changes in window W, which displays
12502 current_buffer, are in the text between START and END. START is a
12503 buffer position, END is given as a distance from Z. Used in
12504 redisplay_internal for display optimization. */
12505
12506 static int
12507 text_outside_line_unchanged_p (struct window *w,
12508 ptrdiff_t start, ptrdiff_t end)
12509 {
12510 int unchanged_p = 1;
12511
12512 /* If text or overlays have changed, see where. */
12513 if (window_outdated (w))
12514 {
12515 /* Gap in the line? */
12516 if (GPT < start || Z - GPT < end)
12517 unchanged_p = 0;
12518
12519 /* Changes start in front of the line, or end after it? */
12520 if (unchanged_p
12521 && (BEG_UNCHANGED < start - 1
12522 || END_UNCHANGED < end))
12523 unchanged_p = 0;
12524
12525 /* If selective display, can't optimize if changes start at the
12526 beginning of the line. */
12527 if (unchanged_p
12528 && INTEGERP (BVAR (current_buffer, selective_display))
12529 && XINT (BVAR (current_buffer, selective_display)) > 0
12530 && (BEG_UNCHANGED < start || GPT <= start))
12531 unchanged_p = 0;
12532
12533 /* If there are overlays at the start or end of the line, these
12534 may have overlay strings with newlines in them. A change at
12535 START, for instance, may actually concern the display of such
12536 overlay strings as well, and they are displayed on different
12537 lines. So, quickly rule out this case. (For the future, it
12538 might be desirable to implement something more telling than
12539 just BEG/END_UNCHANGED.) */
12540 if (unchanged_p)
12541 {
12542 if (BEG + BEG_UNCHANGED == start
12543 && overlay_touches_p (start))
12544 unchanged_p = 0;
12545 if (END_UNCHANGED == end
12546 && overlay_touches_p (Z - end))
12547 unchanged_p = 0;
12548 }
12549
12550 /* Under bidi reordering, adding or deleting a character in the
12551 beginning of a paragraph, before the first strong directional
12552 character, can change the base direction of the paragraph (unless
12553 the buffer specifies a fixed paragraph direction), which will
12554 require to redisplay the whole paragraph. It might be worthwhile
12555 to find the paragraph limits and widen the range of redisplayed
12556 lines to that, but for now just give up this optimization. */
12557 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12558 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12559 unchanged_p = 0;
12560 }
12561
12562 return unchanged_p;
12563 }
12564
12565
12566 /* Do a frame update, taking possible shortcuts into account. This is
12567 the main external entry point for redisplay.
12568
12569 If the last redisplay displayed an echo area message and that message
12570 is no longer requested, we clear the echo area or bring back the
12571 mini-buffer if that is in use. */
12572
12573 void
12574 redisplay (void)
12575 {
12576 redisplay_internal ();
12577 }
12578
12579
12580 static Lisp_Object
12581 overlay_arrow_string_or_property (Lisp_Object var)
12582 {
12583 Lisp_Object val;
12584
12585 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12586 return val;
12587
12588 return Voverlay_arrow_string;
12589 }
12590
12591 /* Return 1 if there are any overlay-arrows in current_buffer. */
12592 static int
12593 overlay_arrow_in_current_buffer_p (void)
12594 {
12595 Lisp_Object vlist;
12596
12597 for (vlist = Voverlay_arrow_variable_list;
12598 CONSP (vlist);
12599 vlist = XCDR (vlist))
12600 {
12601 Lisp_Object var = XCAR (vlist);
12602 Lisp_Object val;
12603
12604 if (!SYMBOLP (var))
12605 continue;
12606 val = find_symbol_value (var);
12607 if (MARKERP (val)
12608 && current_buffer == XMARKER (val)->buffer)
12609 return 1;
12610 }
12611 return 0;
12612 }
12613
12614
12615 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12616 has changed. */
12617
12618 static int
12619 overlay_arrows_changed_p (void)
12620 {
12621 Lisp_Object vlist;
12622
12623 for (vlist = Voverlay_arrow_variable_list;
12624 CONSP (vlist);
12625 vlist = XCDR (vlist))
12626 {
12627 Lisp_Object var = XCAR (vlist);
12628 Lisp_Object val, pstr;
12629
12630 if (!SYMBOLP (var))
12631 continue;
12632 val = find_symbol_value (var);
12633 if (!MARKERP (val))
12634 continue;
12635 if (! EQ (COERCE_MARKER (val),
12636 Fget (var, Qlast_arrow_position))
12637 || ! (pstr = overlay_arrow_string_or_property (var),
12638 EQ (pstr, Fget (var, Qlast_arrow_string))))
12639 return 1;
12640 }
12641 return 0;
12642 }
12643
12644 /* Mark overlay arrows to be updated on next redisplay. */
12645
12646 static void
12647 update_overlay_arrows (int up_to_date)
12648 {
12649 Lisp_Object vlist;
12650
12651 for (vlist = Voverlay_arrow_variable_list;
12652 CONSP (vlist);
12653 vlist = XCDR (vlist))
12654 {
12655 Lisp_Object var = XCAR (vlist);
12656
12657 if (!SYMBOLP (var))
12658 continue;
12659
12660 if (up_to_date > 0)
12661 {
12662 Lisp_Object val = find_symbol_value (var);
12663 Fput (var, Qlast_arrow_position,
12664 COERCE_MARKER (val));
12665 Fput (var, Qlast_arrow_string,
12666 overlay_arrow_string_or_property (var));
12667 }
12668 else if (up_to_date < 0
12669 || !NILP (Fget (var, Qlast_arrow_position)))
12670 {
12671 Fput (var, Qlast_arrow_position, Qt);
12672 Fput (var, Qlast_arrow_string, Qt);
12673 }
12674 }
12675 }
12676
12677
12678 /* Return overlay arrow string to display at row.
12679 Return integer (bitmap number) for arrow bitmap in left fringe.
12680 Return nil if no overlay arrow. */
12681
12682 static Lisp_Object
12683 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12684 {
12685 Lisp_Object vlist;
12686
12687 for (vlist = Voverlay_arrow_variable_list;
12688 CONSP (vlist);
12689 vlist = XCDR (vlist))
12690 {
12691 Lisp_Object var = XCAR (vlist);
12692 Lisp_Object val;
12693
12694 if (!SYMBOLP (var))
12695 continue;
12696
12697 val = find_symbol_value (var);
12698
12699 if (MARKERP (val)
12700 && current_buffer == XMARKER (val)->buffer
12701 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12702 {
12703 if (FRAME_WINDOW_P (it->f)
12704 /* FIXME: if ROW->reversed_p is set, this should test
12705 the right fringe, not the left one. */
12706 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12707 {
12708 #ifdef HAVE_WINDOW_SYSTEM
12709 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12710 {
12711 int fringe_bitmap;
12712 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12713 return make_number (fringe_bitmap);
12714 }
12715 #endif
12716 return make_number (-1); /* Use default arrow bitmap. */
12717 }
12718 return overlay_arrow_string_or_property (var);
12719 }
12720 }
12721
12722 return Qnil;
12723 }
12724
12725 /* Return 1 if point moved out of or into a composition. Otherwise
12726 return 0. PREV_BUF and PREV_PT are the last point buffer and
12727 position. BUF and PT are the current point buffer and position. */
12728
12729 static int
12730 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12731 struct buffer *buf, ptrdiff_t pt)
12732 {
12733 ptrdiff_t start, end;
12734 Lisp_Object prop;
12735 Lisp_Object buffer;
12736
12737 XSETBUFFER (buffer, buf);
12738 /* Check a composition at the last point if point moved within the
12739 same buffer. */
12740 if (prev_buf == buf)
12741 {
12742 if (prev_pt == pt)
12743 /* Point didn't move. */
12744 return 0;
12745
12746 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12747 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12748 && COMPOSITION_VALID_P (start, end, prop)
12749 && start < prev_pt && end > prev_pt)
12750 /* The last point was within the composition. Return 1 iff
12751 point moved out of the composition. */
12752 return (pt <= start || pt >= end);
12753 }
12754
12755 /* Check a composition at the current point. */
12756 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12757 && find_composition (pt, -1, &start, &end, &prop, buffer)
12758 && COMPOSITION_VALID_P (start, end, prop)
12759 && start < pt && end > pt);
12760 }
12761
12762
12763 /* Reconsider the setting of B->clip_changed which is displayed
12764 in window W. */
12765
12766 static void
12767 reconsider_clip_changes (struct window *w, struct buffer *b)
12768 {
12769 if (b->clip_changed
12770 && w->window_end_valid
12771 && w->current_matrix->buffer == b
12772 && w->current_matrix->zv == BUF_ZV (b)
12773 && w->current_matrix->begv == BUF_BEGV (b))
12774 b->clip_changed = 0;
12775
12776 /* If display wasn't paused, and W is not a tool bar window, see if
12777 point has been moved into or out of a composition. In that case,
12778 we set b->clip_changed to 1 to force updating the screen. If
12779 b->clip_changed has already been set to 1, we can skip this
12780 check. */
12781 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12782 {
12783 ptrdiff_t pt;
12784
12785 if (w == XWINDOW (selected_window))
12786 pt = PT;
12787 else
12788 pt = marker_position (w->pointm);
12789
12790 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12791 || pt != w->last_point)
12792 && check_point_in_composition (w->current_matrix->buffer,
12793 w->last_point,
12794 XBUFFER (w->contents), pt))
12795 b->clip_changed = 1;
12796 }
12797 }
12798 \f
12799
12800 #define STOP_POLLING \
12801 do { if (! polling_stopped_here) stop_polling (); \
12802 polling_stopped_here = 1; } while (0)
12803
12804 #define RESUME_POLLING \
12805 do { if (polling_stopped_here) start_polling (); \
12806 polling_stopped_here = 0; } while (0)
12807
12808
12809 /* Perhaps in the future avoid recentering windows if it
12810 is not necessary; currently that causes some problems. */
12811
12812 static void
12813 redisplay_internal (void)
12814 {
12815 struct window *w = XWINDOW (selected_window);
12816 struct window *sw;
12817 struct frame *fr;
12818 int pending;
12819 int must_finish = 0;
12820 struct text_pos tlbufpos, tlendpos;
12821 int number_of_visible_frames;
12822 ptrdiff_t count, count1;
12823 struct frame *sf;
12824 int polling_stopped_here = 0;
12825 Lisp_Object tail, frame;
12826 struct backtrace backtrace;
12827
12828 /* Non-zero means redisplay has to consider all windows on all
12829 frames. Zero means, only selected_window is considered. */
12830 int consider_all_windows_p;
12831
12832 /* Non-zero means redisplay has to redisplay the miniwindow. */
12833 int update_miniwindow_p = 0;
12834
12835 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12836
12837 /* No redisplay if running in batch mode or frame is not yet fully
12838 initialized, or redisplay is explicitly turned off by setting
12839 Vinhibit_redisplay. */
12840 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12841 || !NILP (Vinhibit_redisplay))
12842 return;
12843
12844 /* Don't examine these until after testing Vinhibit_redisplay.
12845 When Emacs is shutting down, perhaps because its connection to
12846 X has dropped, we should not look at them at all. */
12847 fr = XFRAME (w->frame);
12848 sf = SELECTED_FRAME ();
12849
12850 if (!fr->glyphs_initialized_p)
12851 return;
12852
12853 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12854 if (popup_activated ())
12855 return;
12856 #endif
12857
12858 /* I don't think this happens but let's be paranoid. */
12859 if (redisplaying_p)
12860 return;
12861
12862 /* Record a function that clears redisplaying_p
12863 when we leave this function. */
12864 count = SPECPDL_INDEX ();
12865 record_unwind_protect (unwind_redisplay, selected_frame);
12866 redisplaying_p = 1;
12867 specbind (Qinhibit_free_realized_faces, Qnil);
12868
12869 /* Record this function, so it appears on the profiler's backtraces. */
12870 backtrace.next = backtrace_list;
12871 backtrace.function = Qredisplay_internal;
12872 backtrace.args = &Qnil;
12873 backtrace.nargs = 0;
12874 backtrace.debug_on_exit = 0;
12875 backtrace_list = &backtrace;
12876
12877 FOR_EACH_FRAME (tail, frame)
12878 XFRAME (frame)->already_hscrolled_p = 0;
12879
12880 retry:
12881 /* Remember the currently selected window. */
12882 sw = w;
12883
12884 pending = 0;
12885 reconsider_clip_changes (w, current_buffer);
12886 last_escape_glyph_frame = NULL;
12887 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12888 last_glyphless_glyph_frame = NULL;
12889 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12890
12891 /* If new fonts have been loaded that make a glyph matrix adjustment
12892 necessary, do it. */
12893 if (fonts_changed_p)
12894 {
12895 adjust_glyphs (NULL);
12896 ++windows_or_buffers_changed;
12897 fonts_changed_p = 0;
12898 }
12899
12900 /* If face_change_count is non-zero, init_iterator will free all
12901 realized faces, which includes the faces referenced from current
12902 matrices. So, we can't reuse current matrices in this case. */
12903 if (face_change_count)
12904 ++windows_or_buffers_changed;
12905
12906 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12907 && FRAME_TTY (sf)->previous_frame != sf)
12908 {
12909 /* Since frames on a single ASCII terminal share the same
12910 display area, displaying a different frame means redisplay
12911 the whole thing. */
12912 windows_or_buffers_changed++;
12913 SET_FRAME_GARBAGED (sf);
12914 #ifndef DOS_NT
12915 set_tty_color_mode (FRAME_TTY (sf), sf);
12916 #endif
12917 FRAME_TTY (sf)->previous_frame = sf;
12918 }
12919
12920 /* Set the visible flags for all frames. Do this before checking for
12921 resized or garbaged frames; they want to know if their frames are
12922 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12923 number_of_visible_frames = 0;
12924
12925 FOR_EACH_FRAME (tail, frame)
12926 {
12927 struct frame *f = XFRAME (frame);
12928
12929 if (FRAME_VISIBLE_P (f))
12930 ++number_of_visible_frames;
12931 clear_desired_matrices (f);
12932 }
12933
12934 /* Notice any pending interrupt request to change frame size. */
12935 do_pending_window_change (1);
12936
12937 /* do_pending_window_change could change the selected_window due to
12938 frame resizing which makes the selected window too small. */
12939 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12940 {
12941 sw = w;
12942 reconsider_clip_changes (w, current_buffer);
12943 }
12944
12945 /* Clear frames marked as garbaged. */
12946 clear_garbaged_frames ();
12947
12948 /* Build menubar and tool-bar items. */
12949 if (NILP (Vmemory_full))
12950 prepare_menu_bars ();
12951
12952 if (windows_or_buffers_changed)
12953 update_mode_lines++;
12954
12955 /* Detect case that we need to write or remove a star in the mode line. */
12956 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12957 {
12958 w->update_mode_line = 1;
12959 if (buffer_shared_and_changed ())
12960 update_mode_lines++;
12961 }
12962
12963 /* Avoid invocation of point motion hooks by `current_column' below. */
12964 count1 = SPECPDL_INDEX ();
12965 specbind (Qinhibit_point_motion_hooks, Qt);
12966
12967 if (mode_line_update_needed (w))
12968 w->update_mode_line = 1;
12969
12970 unbind_to (count1, Qnil);
12971
12972 consider_all_windows_p = (update_mode_lines
12973 || buffer_shared_and_changed ()
12974 || cursor_type_changed);
12975
12976 /* If specs for an arrow have changed, do thorough redisplay
12977 to ensure we remove any arrow that should no longer exist. */
12978 if (overlay_arrows_changed_p ())
12979 consider_all_windows_p = windows_or_buffers_changed = 1;
12980
12981 /* Normally the message* functions will have already displayed and
12982 updated the echo area, but the frame may have been trashed, or
12983 the update may have been preempted, so display the echo area
12984 again here. Checking message_cleared_p captures the case that
12985 the echo area should be cleared. */
12986 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12987 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12988 || (message_cleared_p
12989 && minibuf_level == 0
12990 /* If the mini-window is currently selected, this means the
12991 echo-area doesn't show through. */
12992 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12993 {
12994 int window_height_changed_p = echo_area_display (0);
12995
12996 if (message_cleared_p)
12997 update_miniwindow_p = 1;
12998
12999 must_finish = 1;
13000
13001 /* If we don't display the current message, don't clear the
13002 message_cleared_p flag, because, if we did, we wouldn't clear
13003 the echo area in the next redisplay which doesn't preserve
13004 the echo area. */
13005 if (!display_last_displayed_message_p)
13006 message_cleared_p = 0;
13007
13008 if (fonts_changed_p)
13009 goto retry;
13010 else if (window_height_changed_p)
13011 {
13012 consider_all_windows_p = 1;
13013 ++update_mode_lines;
13014 ++windows_or_buffers_changed;
13015
13016 /* If window configuration was changed, frames may have been
13017 marked garbaged. Clear them or we will experience
13018 surprises wrt scrolling. */
13019 clear_garbaged_frames ();
13020 }
13021 }
13022 else if (EQ (selected_window, minibuf_window)
13023 && (current_buffer->clip_changed || window_outdated (w))
13024 && resize_mini_window (w, 0))
13025 {
13026 /* Resized active mini-window to fit the size of what it is
13027 showing if its contents might have changed. */
13028 must_finish = 1;
13029 /* FIXME: this causes all frames to be updated, which seems unnecessary
13030 since only the current frame needs to be considered. This function
13031 needs to be rewritten with two variables, consider_all_windows and
13032 consider_all_frames. */
13033 consider_all_windows_p = 1;
13034 ++windows_or_buffers_changed;
13035 ++update_mode_lines;
13036
13037 /* If window configuration was changed, frames may have been
13038 marked garbaged. Clear them or we will experience
13039 surprises wrt scrolling. */
13040 clear_garbaged_frames ();
13041 }
13042
13043 /* If showing the region, and mark has changed, we must redisplay
13044 the whole window. The assignment to this_line_start_pos prevents
13045 the optimization directly below this if-statement. */
13046 if (((!NILP (Vtransient_mark_mode)
13047 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13048 != (w->region_showing > 0))
13049 || (w->region_showing
13050 && w->region_showing
13051 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13052 CHARPOS (this_line_start_pos) = 0;
13053
13054 /* Optimize the case that only the line containing the cursor in the
13055 selected window has changed. Variables starting with this_ are
13056 set in display_line and record information about the line
13057 containing the cursor. */
13058 tlbufpos = this_line_start_pos;
13059 tlendpos = this_line_end_pos;
13060 if (!consider_all_windows_p
13061 && CHARPOS (tlbufpos) > 0
13062 && !w->update_mode_line
13063 && !current_buffer->clip_changed
13064 && !current_buffer->prevent_redisplay_optimizations_p
13065 && FRAME_VISIBLE_P (XFRAME (w->frame))
13066 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13067 /* Make sure recorded data applies to current buffer, etc. */
13068 && this_line_buffer == current_buffer
13069 && current_buffer == XBUFFER (w->contents)
13070 && !w->force_start
13071 && !w->optional_new_start
13072 /* Point must be on the line that we have info recorded about. */
13073 && PT >= CHARPOS (tlbufpos)
13074 && PT <= Z - CHARPOS (tlendpos)
13075 /* All text outside that line, including its final newline,
13076 must be unchanged. */
13077 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13078 CHARPOS (tlendpos)))
13079 {
13080 if (CHARPOS (tlbufpos) > BEGV
13081 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13082 && (CHARPOS (tlbufpos) == ZV
13083 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13084 /* Former continuation line has disappeared by becoming empty. */
13085 goto cancel;
13086 else if (window_outdated (w) || MINI_WINDOW_P (w))
13087 {
13088 /* We have to handle the case of continuation around a
13089 wide-column character (see the comment in indent.c around
13090 line 1340).
13091
13092 For instance, in the following case:
13093
13094 -------- Insert --------
13095 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13096 J_I_ ==> J_I_ `^^' are cursors.
13097 ^^ ^^
13098 -------- --------
13099
13100 As we have to redraw the line above, we cannot use this
13101 optimization. */
13102
13103 struct it it;
13104 int line_height_before = this_line_pixel_height;
13105
13106 /* Note that start_display will handle the case that the
13107 line starting at tlbufpos is a continuation line. */
13108 start_display (&it, w, tlbufpos);
13109
13110 /* Implementation note: It this still necessary? */
13111 if (it.current_x != this_line_start_x)
13112 goto cancel;
13113
13114 TRACE ((stderr, "trying display optimization 1\n"));
13115 w->cursor.vpos = -1;
13116 overlay_arrow_seen = 0;
13117 it.vpos = this_line_vpos;
13118 it.current_y = this_line_y;
13119 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13120 display_line (&it);
13121
13122 /* If line contains point, is not continued,
13123 and ends at same distance from eob as before, we win. */
13124 if (w->cursor.vpos >= 0
13125 /* Line is not continued, otherwise this_line_start_pos
13126 would have been set to 0 in display_line. */
13127 && CHARPOS (this_line_start_pos)
13128 /* Line ends as before. */
13129 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13130 /* Line has same height as before. Otherwise other lines
13131 would have to be shifted up or down. */
13132 && this_line_pixel_height == line_height_before)
13133 {
13134 /* If this is not the window's last line, we must adjust
13135 the charstarts of the lines below. */
13136 if (it.current_y < it.last_visible_y)
13137 {
13138 struct glyph_row *row
13139 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13140 ptrdiff_t delta, delta_bytes;
13141
13142 /* We used to distinguish between two cases here,
13143 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13144 when the line ends in a newline or the end of the
13145 buffer's accessible portion. But both cases did
13146 the same, so they were collapsed. */
13147 delta = (Z
13148 - CHARPOS (tlendpos)
13149 - MATRIX_ROW_START_CHARPOS (row));
13150 delta_bytes = (Z_BYTE
13151 - BYTEPOS (tlendpos)
13152 - MATRIX_ROW_START_BYTEPOS (row));
13153
13154 increment_matrix_positions (w->current_matrix,
13155 this_line_vpos + 1,
13156 w->current_matrix->nrows,
13157 delta, delta_bytes);
13158 }
13159
13160 /* If this row displays text now but previously didn't,
13161 or vice versa, w->window_end_vpos may have to be
13162 adjusted. */
13163 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13164 {
13165 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13166 wset_window_end_vpos (w, make_number (this_line_vpos));
13167 }
13168 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13169 && this_line_vpos > 0)
13170 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13171 w->window_end_valid = 0;
13172
13173 /* Update hint: No need to try to scroll in update_window. */
13174 w->desired_matrix->no_scrolling_p = 1;
13175
13176 #ifdef GLYPH_DEBUG
13177 *w->desired_matrix->method = 0;
13178 debug_method_add (w, "optimization 1");
13179 #endif
13180 #ifdef HAVE_WINDOW_SYSTEM
13181 update_window_fringes (w, 0);
13182 #endif
13183 goto update;
13184 }
13185 else
13186 goto cancel;
13187 }
13188 else if (/* Cursor position hasn't changed. */
13189 PT == w->last_point
13190 /* Make sure the cursor was last displayed
13191 in this window. Otherwise we have to reposition it. */
13192 && 0 <= w->cursor.vpos
13193 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13194 {
13195 if (!must_finish)
13196 {
13197 do_pending_window_change (1);
13198 /* If selected_window changed, redisplay again. */
13199 if (WINDOWP (selected_window)
13200 && (w = XWINDOW (selected_window)) != sw)
13201 goto retry;
13202
13203 /* We used to always goto end_of_redisplay here, but this
13204 isn't enough if we have a blinking cursor. */
13205 if (w->cursor_off_p == w->last_cursor_off_p)
13206 goto end_of_redisplay;
13207 }
13208 goto update;
13209 }
13210 /* If highlighting the region, or if the cursor is in the echo area,
13211 then we can't just move the cursor. */
13212 else if (! (!NILP (Vtransient_mark_mode)
13213 && !NILP (BVAR (current_buffer, mark_active)))
13214 && (EQ (selected_window,
13215 BVAR (current_buffer, last_selected_window))
13216 || highlight_nonselected_windows)
13217 && !w->region_showing
13218 && NILP (Vshow_trailing_whitespace)
13219 && !cursor_in_echo_area)
13220 {
13221 struct it it;
13222 struct glyph_row *row;
13223
13224 /* Skip from tlbufpos to PT and see where it is. Note that
13225 PT may be in invisible text. If so, we will end at the
13226 next visible position. */
13227 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13228 NULL, DEFAULT_FACE_ID);
13229 it.current_x = this_line_start_x;
13230 it.current_y = this_line_y;
13231 it.vpos = this_line_vpos;
13232
13233 /* The call to move_it_to stops in front of PT, but
13234 moves over before-strings. */
13235 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13236
13237 if (it.vpos == this_line_vpos
13238 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13239 row->enabled_p))
13240 {
13241 eassert (this_line_vpos == it.vpos);
13242 eassert (this_line_y == it.current_y);
13243 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13244 #ifdef GLYPH_DEBUG
13245 *w->desired_matrix->method = 0;
13246 debug_method_add (w, "optimization 3");
13247 #endif
13248 goto update;
13249 }
13250 else
13251 goto cancel;
13252 }
13253
13254 cancel:
13255 /* Text changed drastically or point moved off of line. */
13256 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13257 }
13258
13259 CHARPOS (this_line_start_pos) = 0;
13260 consider_all_windows_p |= buffer_shared_and_changed ();
13261 ++clear_face_cache_count;
13262 #ifdef HAVE_WINDOW_SYSTEM
13263 ++clear_image_cache_count;
13264 #endif
13265
13266 /* Build desired matrices, and update the display. If
13267 consider_all_windows_p is non-zero, do it for all windows on all
13268 frames. Otherwise do it for selected_window, only. */
13269
13270 if (consider_all_windows_p)
13271 {
13272 FOR_EACH_FRAME (tail, frame)
13273 XFRAME (frame)->updated_p = 0;
13274
13275 FOR_EACH_FRAME (tail, frame)
13276 {
13277 struct frame *f = XFRAME (frame);
13278
13279 /* We don't have to do anything for unselected terminal
13280 frames. */
13281 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13282 && !EQ (FRAME_TTY (f)->top_frame, frame))
13283 continue;
13284
13285 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13286 {
13287 /* Mark all the scroll bars to be removed; we'll redeem
13288 the ones we want when we redisplay their windows. */
13289 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13290 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13291
13292 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13293 redisplay_windows (FRAME_ROOT_WINDOW (f));
13294
13295 /* The X error handler may have deleted that frame. */
13296 if (!FRAME_LIVE_P (f))
13297 continue;
13298
13299 /* Any scroll bars which redisplay_windows should have
13300 nuked should now go away. */
13301 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13302 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13303
13304 /* If fonts changed, display again. */
13305 /* ??? rms: I suspect it is a mistake to jump all the way
13306 back to retry here. It should just retry this frame. */
13307 if (fonts_changed_p)
13308 goto retry;
13309
13310 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13311 {
13312 /* See if we have to hscroll. */
13313 if (!f->already_hscrolled_p)
13314 {
13315 f->already_hscrolled_p = 1;
13316 if (hscroll_windows (f->root_window))
13317 goto retry;
13318 }
13319
13320 /* Prevent various kinds of signals during display
13321 update. stdio is not robust about handling
13322 signals, which can cause an apparent I/O
13323 error. */
13324 if (interrupt_input)
13325 unrequest_sigio ();
13326 STOP_POLLING;
13327
13328 /* Update the display. */
13329 set_window_update_flags (XWINDOW (f->root_window), 1);
13330 pending |= update_frame (f, 0, 0);
13331 f->updated_p = 1;
13332 }
13333 }
13334 }
13335
13336 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13337
13338 if (!pending)
13339 {
13340 /* Do the mark_window_display_accurate after all windows have
13341 been redisplayed because this call resets flags in buffers
13342 which are needed for proper redisplay. */
13343 FOR_EACH_FRAME (tail, frame)
13344 {
13345 struct frame *f = XFRAME (frame);
13346 if (f->updated_p)
13347 {
13348 mark_window_display_accurate (f->root_window, 1);
13349 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13350 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13351 }
13352 }
13353 }
13354 }
13355 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13356 {
13357 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13358 struct frame *mini_frame;
13359
13360 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13361 /* Use list_of_error, not Qerror, so that
13362 we catch only errors and don't run the debugger. */
13363 internal_condition_case_1 (redisplay_window_1, selected_window,
13364 list_of_error,
13365 redisplay_window_error);
13366 if (update_miniwindow_p)
13367 internal_condition_case_1 (redisplay_window_1, mini_window,
13368 list_of_error,
13369 redisplay_window_error);
13370
13371 /* Compare desired and current matrices, perform output. */
13372
13373 update:
13374 /* If fonts changed, display again. */
13375 if (fonts_changed_p)
13376 goto retry;
13377
13378 /* Prevent various kinds of signals during display update.
13379 stdio is not robust about handling signals,
13380 which can cause an apparent I/O error. */
13381 if (interrupt_input)
13382 unrequest_sigio ();
13383 STOP_POLLING;
13384
13385 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13386 {
13387 if (hscroll_windows (selected_window))
13388 goto retry;
13389
13390 XWINDOW (selected_window)->must_be_updated_p = 1;
13391 pending = update_frame (sf, 0, 0);
13392 }
13393
13394 /* We may have called echo_area_display at the top of this
13395 function. If the echo area is on another frame, that may
13396 have put text on a frame other than the selected one, so the
13397 above call to update_frame would not have caught it. Catch
13398 it here. */
13399 mini_window = FRAME_MINIBUF_WINDOW (sf);
13400 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13401
13402 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13403 {
13404 XWINDOW (mini_window)->must_be_updated_p = 1;
13405 pending |= update_frame (mini_frame, 0, 0);
13406 if (!pending && hscroll_windows (mini_window))
13407 goto retry;
13408 }
13409 }
13410
13411 /* If display was paused because of pending input, make sure we do a
13412 thorough update the next time. */
13413 if (pending)
13414 {
13415 /* Prevent the optimization at the beginning of
13416 redisplay_internal that tries a single-line update of the
13417 line containing the cursor in the selected window. */
13418 CHARPOS (this_line_start_pos) = 0;
13419
13420 /* Let the overlay arrow be updated the next time. */
13421 update_overlay_arrows (0);
13422
13423 /* If we pause after scrolling, some rows in the current
13424 matrices of some windows are not valid. */
13425 if (!WINDOW_FULL_WIDTH_P (w)
13426 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13427 update_mode_lines = 1;
13428 }
13429 else
13430 {
13431 if (!consider_all_windows_p)
13432 {
13433 /* This has already been done above if
13434 consider_all_windows_p is set. */
13435 mark_window_display_accurate_1 (w, 1);
13436
13437 /* Say overlay arrows are up to date. */
13438 update_overlay_arrows (1);
13439
13440 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13441 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13442 }
13443
13444 update_mode_lines = 0;
13445 windows_or_buffers_changed = 0;
13446 cursor_type_changed = 0;
13447 }
13448
13449 /* Start SIGIO interrupts coming again. Having them off during the
13450 code above makes it less likely one will discard output, but not
13451 impossible, since there might be stuff in the system buffer here.
13452 But it is much hairier to try to do anything about that. */
13453 if (interrupt_input)
13454 request_sigio ();
13455 RESUME_POLLING;
13456
13457 /* If a frame has become visible which was not before, redisplay
13458 again, so that we display it. Expose events for such a frame
13459 (which it gets when becoming visible) don't call the parts of
13460 redisplay constructing glyphs, so simply exposing a frame won't
13461 display anything in this case. So, we have to display these
13462 frames here explicitly. */
13463 if (!pending)
13464 {
13465 int new_count = 0;
13466
13467 FOR_EACH_FRAME (tail, frame)
13468 {
13469 int this_is_visible = 0;
13470
13471 if (XFRAME (frame)->visible)
13472 this_is_visible = 1;
13473
13474 if (this_is_visible)
13475 new_count++;
13476 }
13477
13478 if (new_count != number_of_visible_frames)
13479 windows_or_buffers_changed++;
13480 }
13481
13482 /* Change frame size now if a change is pending. */
13483 do_pending_window_change (1);
13484
13485 /* If we just did a pending size change, or have additional
13486 visible frames, or selected_window changed, redisplay again. */
13487 if ((windows_or_buffers_changed && !pending)
13488 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13489 goto retry;
13490
13491 /* Clear the face and image caches.
13492
13493 We used to do this only if consider_all_windows_p. But the cache
13494 needs to be cleared if a timer creates images in the current
13495 buffer (e.g. the test case in Bug#6230). */
13496
13497 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13498 {
13499 clear_face_cache (0);
13500 clear_face_cache_count = 0;
13501 }
13502
13503 #ifdef HAVE_WINDOW_SYSTEM
13504 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13505 {
13506 clear_image_caches (Qnil);
13507 clear_image_cache_count = 0;
13508 }
13509 #endif /* HAVE_WINDOW_SYSTEM */
13510
13511 end_of_redisplay:
13512 backtrace_list = backtrace.next;
13513 unbind_to (count, Qnil);
13514 RESUME_POLLING;
13515 }
13516
13517
13518 /* Redisplay, but leave alone any recent echo area message unless
13519 another message has been requested in its place.
13520
13521 This is useful in situations where you need to redisplay but no
13522 user action has occurred, making it inappropriate for the message
13523 area to be cleared. See tracking_off and
13524 wait_reading_process_output for examples of these situations.
13525
13526 FROM_WHERE is an integer saying from where this function was
13527 called. This is useful for debugging. */
13528
13529 void
13530 redisplay_preserve_echo_area (int from_where)
13531 {
13532 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13533
13534 if (!NILP (echo_area_buffer[1]))
13535 {
13536 /* We have a previously displayed message, but no current
13537 message. Redisplay the previous message. */
13538 display_last_displayed_message_p = 1;
13539 redisplay_internal ();
13540 display_last_displayed_message_p = 0;
13541 }
13542 else
13543 redisplay_internal ();
13544
13545 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13546 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13547 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13548 }
13549
13550
13551 /* Function registered with record_unwind_protect in redisplay_internal.
13552 Clear redisplaying_p. Also select the previously selected frame. */
13553
13554 static Lisp_Object
13555 unwind_redisplay (Lisp_Object old_frame)
13556 {
13557 redisplaying_p = 0;
13558 return Qnil;
13559 }
13560
13561
13562 /* Mark the display of leaf window W as accurate or inaccurate.
13563 If ACCURATE_P is non-zero mark display of W as accurate. If
13564 ACCURATE_P is zero, arrange for W to be redisplayed the next
13565 time redisplay_internal is called. */
13566
13567 static void
13568 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13569 {
13570 struct buffer *b = XBUFFER (w->contents);
13571
13572 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13573 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13574 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13575
13576 if (accurate_p)
13577 {
13578 b->clip_changed = 0;
13579 b->prevent_redisplay_optimizations_p = 0;
13580
13581 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13582 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13583 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13584 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13585
13586 w->current_matrix->buffer = b;
13587 w->current_matrix->begv = BUF_BEGV (b);
13588 w->current_matrix->zv = BUF_ZV (b);
13589
13590 w->last_cursor = w->cursor;
13591 w->last_cursor_off_p = w->cursor_off_p;
13592
13593 if (w == XWINDOW (selected_window))
13594 w->last_point = BUF_PT (b);
13595 else
13596 w->last_point = marker_position (w->pointm);
13597
13598 w->window_end_valid = 1;
13599 w->update_mode_line = 0;
13600 }
13601 }
13602
13603
13604 /* Mark the display of windows in the window tree rooted at WINDOW as
13605 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13606 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13607 be redisplayed the next time redisplay_internal is called. */
13608
13609 void
13610 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13611 {
13612 struct window *w;
13613
13614 for (; !NILP (window); window = w->next)
13615 {
13616 w = XWINDOW (window);
13617 if (WINDOWP (w->contents))
13618 mark_window_display_accurate (w->contents, accurate_p);
13619 else
13620 mark_window_display_accurate_1 (w, accurate_p);
13621 }
13622
13623 if (accurate_p)
13624 update_overlay_arrows (1);
13625 else
13626 /* Force a thorough redisplay the next time by setting
13627 last_arrow_position and last_arrow_string to t, which is
13628 unequal to any useful value of Voverlay_arrow_... */
13629 update_overlay_arrows (-1);
13630 }
13631
13632
13633 /* Return value in display table DP (Lisp_Char_Table *) for character
13634 C. Since a display table doesn't have any parent, we don't have to
13635 follow parent. Do not call this function directly but use the
13636 macro DISP_CHAR_VECTOR. */
13637
13638 Lisp_Object
13639 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13640 {
13641 Lisp_Object val;
13642
13643 if (ASCII_CHAR_P (c))
13644 {
13645 val = dp->ascii;
13646 if (SUB_CHAR_TABLE_P (val))
13647 val = XSUB_CHAR_TABLE (val)->contents[c];
13648 }
13649 else
13650 {
13651 Lisp_Object table;
13652
13653 XSETCHAR_TABLE (table, dp);
13654 val = char_table_ref (table, c);
13655 }
13656 if (NILP (val))
13657 val = dp->defalt;
13658 return val;
13659 }
13660
13661
13662 \f
13663 /***********************************************************************
13664 Window Redisplay
13665 ***********************************************************************/
13666
13667 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13668
13669 static void
13670 redisplay_windows (Lisp_Object window)
13671 {
13672 while (!NILP (window))
13673 {
13674 struct window *w = XWINDOW (window);
13675
13676 if (WINDOWP (w->contents))
13677 redisplay_windows (w->contents);
13678 else if (BUFFERP (w->contents))
13679 {
13680 displayed_buffer = XBUFFER (w->contents);
13681 /* Use list_of_error, not Qerror, so that
13682 we catch only errors and don't run the debugger. */
13683 internal_condition_case_1 (redisplay_window_0, window,
13684 list_of_error,
13685 redisplay_window_error);
13686 }
13687
13688 window = w->next;
13689 }
13690 }
13691
13692 static Lisp_Object
13693 redisplay_window_error (Lisp_Object ignore)
13694 {
13695 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13696 return Qnil;
13697 }
13698
13699 static Lisp_Object
13700 redisplay_window_0 (Lisp_Object window)
13701 {
13702 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13703 redisplay_window (window, 0);
13704 return Qnil;
13705 }
13706
13707 static Lisp_Object
13708 redisplay_window_1 (Lisp_Object window)
13709 {
13710 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13711 redisplay_window (window, 1);
13712 return Qnil;
13713 }
13714 \f
13715
13716 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13717 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13718 which positions recorded in ROW differ from current buffer
13719 positions.
13720
13721 Return 0 if cursor is not on this row, 1 otherwise. */
13722
13723 static int
13724 set_cursor_from_row (struct window *w, struct glyph_row *row,
13725 struct glyph_matrix *matrix,
13726 ptrdiff_t delta, ptrdiff_t delta_bytes,
13727 int dy, int dvpos)
13728 {
13729 struct glyph *glyph = row->glyphs[TEXT_AREA];
13730 struct glyph *end = glyph + row->used[TEXT_AREA];
13731 struct glyph *cursor = NULL;
13732 /* The last known character position in row. */
13733 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13734 int x = row->x;
13735 ptrdiff_t pt_old = PT - delta;
13736 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13737 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13738 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13739 /* A glyph beyond the edge of TEXT_AREA which we should never
13740 touch. */
13741 struct glyph *glyphs_end = end;
13742 /* Non-zero means we've found a match for cursor position, but that
13743 glyph has the avoid_cursor_p flag set. */
13744 int match_with_avoid_cursor = 0;
13745 /* Non-zero means we've seen at least one glyph that came from a
13746 display string. */
13747 int string_seen = 0;
13748 /* Largest and smallest buffer positions seen so far during scan of
13749 glyph row. */
13750 ptrdiff_t bpos_max = pos_before;
13751 ptrdiff_t bpos_min = pos_after;
13752 /* Last buffer position covered by an overlay string with an integer
13753 `cursor' property. */
13754 ptrdiff_t bpos_covered = 0;
13755 /* Non-zero means the display string on which to display the cursor
13756 comes from a text property, not from an overlay. */
13757 int string_from_text_prop = 0;
13758
13759 /* Don't even try doing anything if called for a mode-line or
13760 header-line row, since the rest of the code isn't prepared to
13761 deal with such calamities. */
13762 eassert (!row->mode_line_p);
13763 if (row->mode_line_p)
13764 return 0;
13765
13766 /* Skip over glyphs not having an object at the start and the end of
13767 the row. These are special glyphs like truncation marks on
13768 terminal frames. */
13769 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13770 {
13771 if (!row->reversed_p)
13772 {
13773 while (glyph < end
13774 && INTEGERP (glyph->object)
13775 && glyph->charpos < 0)
13776 {
13777 x += glyph->pixel_width;
13778 ++glyph;
13779 }
13780 while (end > glyph
13781 && INTEGERP ((end - 1)->object)
13782 /* CHARPOS is zero for blanks and stretch glyphs
13783 inserted by extend_face_to_end_of_line. */
13784 && (end - 1)->charpos <= 0)
13785 --end;
13786 glyph_before = glyph - 1;
13787 glyph_after = end;
13788 }
13789 else
13790 {
13791 struct glyph *g;
13792
13793 /* If the glyph row is reversed, we need to process it from back
13794 to front, so swap the edge pointers. */
13795 glyphs_end = end = glyph - 1;
13796 glyph += row->used[TEXT_AREA] - 1;
13797
13798 while (glyph > end + 1
13799 && INTEGERP (glyph->object)
13800 && glyph->charpos < 0)
13801 {
13802 --glyph;
13803 x -= glyph->pixel_width;
13804 }
13805 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13806 --glyph;
13807 /* By default, in reversed rows we put the cursor on the
13808 rightmost (first in the reading order) glyph. */
13809 for (g = end + 1; g < glyph; g++)
13810 x += g->pixel_width;
13811 while (end < glyph
13812 && INTEGERP ((end + 1)->object)
13813 && (end + 1)->charpos <= 0)
13814 ++end;
13815 glyph_before = glyph + 1;
13816 glyph_after = end;
13817 }
13818 }
13819 else if (row->reversed_p)
13820 {
13821 /* In R2L rows that don't display text, put the cursor on the
13822 rightmost glyph. Case in point: an empty last line that is
13823 part of an R2L paragraph. */
13824 cursor = end - 1;
13825 /* Avoid placing the cursor on the last glyph of the row, where
13826 on terminal frames we hold the vertical border between
13827 adjacent windows. */
13828 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13829 && !WINDOW_RIGHTMOST_P (w)
13830 && cursor == row->glyphs[LAST_AREA] - 1)
13831 cursor--;
13832 x = -1; /* will be computed below, at label compute_x */
13833 }
13834
13835 /* Step 1: Try to find the glyph whose character position
13836 corresponds to point. If that's not possible, find 2 glyphs
13837 whose character positions are the closest to point, one before
13838 point, the other after it. */
13839 if (!row->reversed_p)
13840 while (/* not marched to end of glyph row */
13841 glyph < end
13842 /* glyph was not inserted by redisplay for internal purposes */
13843 && !INTEGERP (glyph->object))
13844 {
13845 if (BUFFERP (glyph->object))
13846 {
13847 ptrdiff_t dpos = glyph->charpos - pt_old;
13848
13849 if (glyph->charpos > bpos_max)
13850 bpos_max = glyph->charpos;
13851 if (glyph->charpos < bpos_min)
13852 bpos_min = glyph->charpos;
13853 if (!glyph->avoid_cursor_p)
13854 {
13855 /* If we hit point, we've found the glyph on which to
13856 display the cursor. */
13857 if (dpos == 0)
13858 {
13859 match_with_avoid_cursor = 0;
13860 break;
13861 }
13862 /* See if we've found a better approximation to
13863 POS_BEFORE or to POS_AFTER. */
13864 if (0 > dpos && dpos > pos_before - pt_old)
13865 {
13866 pos_before = glyph->charpos;
13867 glyph_before = glyph;
13868 }
13869 else if (0 < dpos && dpos < pos_after - pt_old)
13870 {
13871 pos_after = glyph->charpos;
13872 glyph_after = glyph;
13873 }
13874 }
13875 else if (dpos == 0)
13876 match_with_avoid_cursor = 1;
13877 }
13878 else if (STRINGP (glyph->object))
13879 {
13880 Lisp_Object chprop;
13881 ptrdiff_t glyph_pos = glyph->charpos;
13882
13883 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13884 glyph->object);
13885 if (!NILP (chprop))
13886 {
13887 /* If the string came from a `display' text property,
13888 look up the buffer position of that property and
13889 use that position to update bpos_max, as if we
13890 actually saw such a position in one of the row's
13891 glyphs. This helps with supporting integer values
13892 of `cursor' property on the display string in
13893 situations where most or all of the row's buffer
13894 text is completely covered by display properties,
13895 so that no glyph with valid buffer positions is
13896 ever seen in the row. */
13897 ptrdiff_t prop_pos =
13898 string_buffer_position_lim (glyph->object, pos_before,
13899 pos_after, 0);
13900
13901 if (prop_pos >= pos_before)
13902 bpos_max = prop_pos - 1;
13903 }
13904 if (INTEGERP (chprop))
13905 {
13906 bpos_covered = bpos_max + XINT (chprop);
13907 /* If the `cursor' property covers buffer positions up
13908 to and including point, we should display cursor on
13909 this glyph. Note that, if a `cursor' property on one
13910 of the string's characters has an integer value, we
13911 will break out of the loop below _before_ we get to
13912 the position match above. IOW, integer values of
13913 the `cursor' property override the "exact match for
13914 point" strategy of positioning the cursor. */
13915 /* Implementation note: bpos_max == pt_old when, e.g.,
13916 we are in an empty line, where bpos_max is set to
13917 MATRIX_ROW_START_CHARPOS, see above. */
13918 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13919 {
13920 cursor = glyph;
13921 break;
13922 }
13923 }
13924
13925 string_seen = 1;
13926 }
13927 x += glyph->pixel_width;
13928 ++glyph;
13929 }
13930 else if (glyph > end) /* row is reversed */
13931 while (!INTEGERP (glyph->object))
13932 {
13933 if (BUFFERP (glyph->object))
13934 {
13935 ptrdiff_t dpos = glyph->charpos - pt_old;
13936
13937 if (glyph->charpos > bpos_max)
13938 bpos_max = glyph->charpos;
13939 if (glyph->charpos < bpos_min)
13940 bpos_min = glyph->charpos;
13941 if (!glyph->avoid_cursor_p)
13942 {
13943 if (dpos == 0)
13944 {
13945 match_with_avoid_cursor = 0;
13946 break;
13947 }
13948 if (0 > dpos && dpos > pos_before - pt_old)
13949 {
13950 pos_before = glyph->charpos;
13951 glyph_before = glyph;
13952 }
13953 else if (0 < dpos && dpos < pos_after - pt_old)
13954 {
13955 pos_after = glyph->charpos;
13956 glyph_after = glyph;
13957 }
13958 }
13959 else if (dpos == 0)
13960 match_with_avoid_cursor = 1;
13961 }
13962 else if (STRINGP (glyph->object))
13963 {
13964 Lisp_Object chprop;
13965 ptrdiff_t glyph_pos = glyph->charpos;
13966
13967 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13968 glyph->object);
13969 if (!NILP (chprop))
13970 {
13971 ptrdiff_t prop_pos =
13972 string_buffer_position_lim (glyph->object, pos_before,
13973 pos_after, 0);
13974
13975 if (prop_pos >= pos_before)
13976 bpos_max = prop_pos - 1;
13977 }
13978 if (INTEGERP (chprop))
13979 {
13980 bpos_covered = bpos_max + XINT (chprop);
13981 /* If the `cursor' property covers buffer positions up
13982 to and including point, we should display cursor on
13983 this glyph. */
13984 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13985 {
13986 cursor = glyph;
13987 break;
13988 }
13989 }
13990 string_seen = 1;
13991 }
13992 --glyph;
13993 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13994 {
13995 x--; /* can't use any pixel_width */
13996 break;
13997 }
13998 x -= glyph->pixel_width;
13999 }
14000
14001 /* Step 2: If we didn't find an exact match for point, we need to
14002 look for a proper place to put the cursor among glyphs between
14003 GLYPH_BEFORE and GLYPH_AFTER. */
14004 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14005 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14006 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14007 {
14008 /* An empty line has a single glyph whose OBJECT is zero and
14009 whose CHARPOS is the position of a newline on that line.
14010 Note that on a TTY, there are more glyphs after that, which
14011 were produced by extend_face_to_end_of_line, but their
14012 CHARPOS is zero or negative. */
14013 int empty_line_p =
14014 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14015 && INTEGERP (glyph->object) && glyph->charpos > 0
14016 /* On a TTY, continued and truncated rows also have a glyph at
14017 their end whose OBJECT is zero and whose CHARPOS is
14018 positive (the continuation and truncation glyphs), but such
14019 rows are obviously not "empty". */
14020 && !(row->continued_p || row->truncated_on_right_p);
14021
14022 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14023 {
14024 ptrdiff_t ellipsis_pos;
14025
14026 /* Scan back over the ellipsis glyphs. */
14027 if (!row->reversed_p)
14028 {
14029 ellipsis_pos = (glyph - 1)->charpos;
14030 while (glyph > row->glyphs[TEXT_AREA]
14031 && (glyph - 1)->charpos == ellipsis_pos)
14032 glyph--, x -= glyph->pixel_width;
14033 /* That loop always goes one position too far, including
14034 the glyph before the ellipsis. So scan forward over
14035 that one. */
14036 x += glyph->pixel_width;
14037 glyph++;
14038 }
14039 else /* row is reversed */
14040 {
14041 ellipsis_pos = (glyph + 1)->charpos;
14042 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14043 && (glyph + 1)->charpos == ellipsis_pos)
14044 glyph++, x += glyph->pixel_width;
14045 x -= glyph->pixel_width;
14046 glyph--;
14047 }
14048 }
14049 else if (match_with_avoid_cursor)
14050 {
14051 cursor = glyph_after;
14052 x = -1;
14053 }
14054 else if (string_seen)
14055 {
14056 int incr = row->reversed_p ? -1 : +1;
14057
14058 /* Need to find the glyph that came out of a string which is
14059 present at point. That glyph is somewhere between
14060 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14061 positioned between POS_BEFORE and POS_AFTER in the
14062 buffer. */
14063 struct glyph *start, *stop;
14064 ptrdiff_t pos = pos_before;
14065
14066 x = -1;
14067
14068 /* If the row ends in a newline from a display string,
14069 reordering could have moved the glyphs belonging to the
14070 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14071 in this case we extend the search to the last glyph in
14072 the row that was not inserted by redisplay. */
14073 if (row->ends_in_newline_from_string_p)
14074 {
14075 glyph_after = end;
14076 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14077 }
14078
14079 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14080 correspond to POS_BEFORE and POS_AFTER, respectively. We
14081 need START and STOP in the order that corresponds to the
14082 row's direction as given by its reversed_p flag. If the
14083 directionality of characters between POS_BEFORE and
14084 POS_AFTER is the opposite of the row's base direction,
14085 these characters will have been reordered for display,
14086 and we need to reverse START and STOP. */
14087 if (!row->reversed_p)
14088 {
14089 start = min (glyph_before, glyph_after);
14090 stop = max (glyph_before, glyph_after);
14091 }
14092 else
14093 {
14094 start = max (glyph_before, glyph_after);
14095 stop = min (glyph_before, glyph_after);
14096 }
14097 for (glyph = start + incr;
14098 row->reversed_p ? glyph > stop : glyph < stop; )
14099 {
14100
14101 /* Any glyphs that come from the buffer are here because
14102 of bidi reordering. Skip them, and only pay
14103 attention to glyphs that came from some string. */
14104 if (STRINGP (glyph->object))
14105 {
14106 Lisp_Object str;
14107 ptrdiff_t tem;
14108 /* If the display property covers the newline, we
14109 need to search for it one position farther. */
14110 ptrdiff_t lim = pos_after
14111 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14112
14113 string_from_text_prop = 0;
14114 str = glyph->object;
14115 tem = string_buffer_position_lim (str, pos, lim, 0);
14116 if (tem == 0 /* from overlay */
14117 || pos <= tem)
14118 {
14119 /* If the string from which this glyph came is
14120 found in the buffer at point, or at position
14121 that is closer to point than pos_after, then
14122 we've found the glyph we've been looking for.
14123 If it comes from an overlay (tem == 0), and
14124 it has the `cursor' property on one of its
14125 glyphs, record that glyph as a candidate for
14126 displaying the cursor. (As in the
14127 unidirectional version, we will display the
14128 cursor on the last candidate we find.) */
14129 if (tem == 0
14130 || tem == pt_old
14131 || (tem - pt_old > 0 && tem < pos_after))
14132 {
14133 /* The glyphs from this string could have
14134 been reordered. Find the one with the
14135 smallest string position. Or there could
14136 be a character in the string with the
14137 `cursor' property, which means display
14138 cursor on that character's glyph. */
14139 ptrdiff_t strpos = glyph->charpos;
14140
14141 if (tem)
14142 {
14143 cursor = glyph;
14144 string_from_text_prop = 1;
14145 }
14146 for ( ;
14147 (row->reversed_p ? glyph > stop : glyph < stop)
14148 && EQ (glyph->object, str);
14149 glyph += incr)
14150 {
14151 Lisp_Object cprop;
14152 ptrdiff_t gpos = glyph->charpos;
14153
14154 cprop = Fget_char_property (make_number (gpos),
14155 Qcursor,
14156 glyph->object);
14157 if (!NILP (cprop))
14158 {
14159 cursor = glyph;
14160 break;
14161 }
14162 if (tem && glyph->charpos < strpos)
14163 {
14164 strpos = glyph->charpos;
14165 cursor = glyph;
14166 }
14167 }
14168
14169 if (tem == pt_old
14170 || (tem - pt_old > 0 && tem < pos_after))
14171 goto compute_x;
14172 }
14173 if (tem)
14174 pos = tem + 1; /* don't find previous instances */
14175 }
14176 /* This string is not what we want; skip all of the
14177 glyphs that came from it. */
14178 while ((row->reversed_p ? glyph > stop : glyph < stop)
14179 && EQ (glyph->object, str))
14180 glyph += incr;
14181 }
14182 else
14183 glyph += incr;
14184 }
14185
14186 /* If we reached the end of the line, and END was from a string,
14187 the cursor is not on this line. */
14188 if (cursor == NULL
14189 && (row->reversed_p ? glyph <= end : glyph >= end)
14190 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14191 && STRINGP (end->object)
14192 && row->continued_p)
14193 return 0;
14194 }
14195 /* A truncated row may not include PT among its character positions.
14196 Setting the cursor inside the scroll margin will trigger
14197 recalculation of hscroll in hscroll_window_tree. But if a
14198 display string covers point, defer to the string-handling
14199 code below to figure this out. */
14200 else if (row->truncated_on_left_p && pt_old < bpos_min)
14201 {
14202 cursor = glyph_before;
14203 x = -1;
14204 }
14205 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14206 /* Zero-width characters produce no glyphs. */
14207 || (!empty_line_p
14208 && (row->reversed_p
14209 ? glyph_after > glyphs_end
14210 : glyph_after < glyphs_end)))
14211 {
14212 cursor = glyph_after;
14213 x = -1;
14214 }
14215 }
14216
14217 compute_x:
14218 if (cursor != NULL)
14219 glyph = cursor;
14220 else if (glyph == glyphs_end
14221 && pos_before == pos_after
14222 && STRINGP ((row->reversed_p
14223 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14224 : row->glyphs[TEXT_AREA])->object))
14225 {
14226 /* If all the glyphs of this row came from strings, put the
14227 cursor on the first glyph of the row. This avoids having the
14228 cursor outside of the text area in this very rare and hard
14229 use case. */
14230 glyph =
14231 row->reversed_p
14232 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14233 : row->glyphs[TEXT_AREA];
14234 }
14235 if (x < 0)
14236 {
14237 struct glyph *g;
14238
14239 /* Need to compute x that corresponds to GLYPH. */
14240 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14241 {
14242 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14243 emacs_abort ();
14244 x += g->pixel_width;
14245 }
14246 }
14247
14248 /* ROW could be part of a continued line, which, under bidi
14249 reordering, might have other rows whose start and end charpos
14250 occlude point. Only set w->cursor if we found a better
14251 approximation to the cursor position than we have from previously
14252 examined candidate rows belonging to the same continued line. */
14253 if (/* we already have a candidate row */
14254 w->cursor.vpos >= 0
14255 /* that candidate is not the row we are processing */
14256 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14257 /* Make sure cursor.vpos specifies a row whose start and end
14258 charpos occlude point, and it is valid candidate for being a
14259 cursor-row. This is because some callers of this function
14260 leave cursor.vpos at the row where the cursor was displayed
14261 during the last redisplay cycle. */
14262 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14263 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14264 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14265 {
14266 struct glyph *g1 =
14267 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14268
14269 /* Don't consider glyphs that are outside TEXT_AREA. */
14270 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14271 return 0;
14272 /* Keep the candidate whose buffer position is the closest to
14273 point or has the `cursor' property. */
14274 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14275 w->cursor.hpos >= 0
14276 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14277 && ((BUFFERP (g1->object)
14278 && (g1->charpos == pt_old /* an exact match always wins */
14279 || (BUFFERP (glyph->object)
14280 && eabs (g1->charpos - pt_old)
14281 < eabs (glyph->charpos - pt_old))))
14282 /* previous candidate is a glyph from a string that has
14283 a non-nil `cursor' property */
14284 || (STRINGP (g1->object)
14285 && (!NILP (Fget_char_property (make_number (g1->charpos),
14286 Qcursor, g1->object))
14287 /* previous candidate is from the same display
14288 string as this one, and the display string
14289 came from a text property */
14290 || (EQ (g1->object, glyph->object)
14291 && string_from_text_prop)
14292 /* this candidate is from newline and its
14293 position is not an exact match */
14294 || (INTEGERP (glyph->object)
14295 && glyph->charpos != pt_old)))))
14296 return 0;
14297 /* If this candidate gives an exact match, use that. */
14298 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14299 /* If this candidate is a glyph created for the
14300 terminating newline of a line, and point is on that
14301 newline, it wins because it's an exact match. */
14302 || (!row->continued_p
14303 && INTEGERP (glyph->object)
14304 && glyph->charpos == 0
14305 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14306 /* Otherwise, keep the candidate that comes from a row
14307 spanning less buffer positions. This may win when one or
14308 both candidate positions are on glyphs that came from
14309 display strings, for which we cannot compare buffer
14310 positions. */
14311 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14312 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14313 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14314 return 0;
14315 }
14316 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14317 w->cursor.x = x;
14318 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14319 w->cursor.y = row->y + dy;
14320
14321 if (w == XWINDOW (selected_window))
14322 {
14323 if (!row->continued_p
14324 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14325 && row->x == 0)
14326 {
14327 this_line_buffer = XBUFFER (w->contents);
14328
14329 CHARPOS (this_line_start_pos)
14330 = MATRIX_ROW_START_CHARPOS (row) + delta;
14331 BYTEPOS (this_line_start_pos)
14332 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14333
14334 CHARPOS (this_line_end_pos)
14335 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14336 BYTEPOS (this_line_end_pos)
14337 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14338
14339 this_line_y = w->cursor.y;
14340 this_line_pixel_height = row->height;
14341 this_line_vpos = w->cursor.vpos;
14342 this_line_start_x = row->x;
14343 }
14344 else
14345 CHARPOS (this_line_start_pos) = 0;
14346 }
14347
14348 return 1;
14349 }
14350
14351
14352 /* Run window scroll functions, if any, for WINDOW with new window
14353 start STARTP. Sets the window start of WINDOW to that position.
14354
14355 We assume that the window's buffer is really current. */
14356
14357 static struct text_pos
14358 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14359 {
14360 struct window *w = XWINDOW (window);
14361 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14362
14363 if (current_buffer != XBUFFER (w->contents))
14364 emacs_abort ();
14365
14366 if (!NILP (Vwindow_scroll_functions))
14367 {
14368 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14369 make_number (CHARPOS (startp)));
14370 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14371 /* In case the hook functions switch buffers. */
14372 set_buffer_internal (XBUFFER (w->contents));
14373 }
14374
14375 return startp;
14376 }
14377
14378
14379 /* Make sure the line containing the cursor is fully visible.
14380 A value of 1 means there is nothing to be done.
14381 (Either the line is fully visible, or it cannot be made so,
14382 or we cannot tell.)
14383
14384 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14385 is higher than window.
14386
14387 A value of 0 means the caller should do scrolling
14388 as if point had gone off the screen. */
14389
14390 static int
14391 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14392 {
14393 struct glyph_matrix *matrix;
14394 struct glyph_row *row;
14395 int window_height;
14396
14397 if (!make_cursor_line_fully_visible_p)
14398 return 1;
14399
14400 /* It's not always possible to find the cursor, e.g, when a window
14401 is full of overlay strings. Don't do anything in that case. */
14402 if (w->cursor.vpos < 0)
14403 return 1;
14404
14405 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14406 row = MATRIX_ROW (matrix, w->cursor.vpos);
14407
14408 /* If the cursor row is not partially visible, there's nothing to do. */
14409 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14410 return 1;
14411
14412 /* If the row the cursor is in is taller than the window's height,
14413 it's not clear what to do, so do nothing. */
14414 window_height = window_box_height (w);
14415 if (row->height >= window_height)
14416 {
14417 if (!force_p || MINI_WINDOW_P (w)
14418 || w->vscroll || w->cursor.vpos == 0)
14419 return 1;
14420 }
14421 return 0;
14422 }
14423
14424
14425 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14426 non-zero means only WINDOW is redisplayed in redisplay_internal.
14427 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14428 in redisplay_window to bring a partially visible line into view in
14429 the case that only the cursor has moved.
14430
14431 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14432 last screen line's vertical height extends past the end of the screen.
14433
14434 Value is
14435
14436 1 if scrolling succeeded
14437
14438 0 if scrolling didn't find point.
14439
14440 -1 if new fonts have been loaded so that we must interrupt
14441 redisplay, adjust glyph matrices, and try again. */
14442
14443 enum
14444 {
14445 SCROLLING_SUCCESS,
14446 SCROLLING_FAILED,
14447 SCROLLING_NEED_LARGER_MATRICES
14448 };
14449
14450 /* If scroll-conservatively is more than this, never recenter.
14451
14452 If you change this, don't forget to update the doc string of
14453 `scroll-conservatively' and the Emacs manual. */
14454 #define SCROLL_LIMIT 100
14455
14456 static int
14457 try_scrolling (Lisp_Object window, int just_this_one_p,
14458 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14459 int temp_scroll_step, int last_line_misfit)
14460 {
14461 struct window *w = XWINDOW (window);
14462 struct frame *f = XFRAME (w->frame);
14463 struct text_pos pos, startp;
14464 struct it it;
14465 int this_scroll_margin, scroll_max, rc, height;
14466 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14467 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14468 Lisp_Object aggressive;
14469 /* We will never try scrolling more than this number of lines. */
14470 int scroll_limit = SCROLL_LIMIT;
14471
14472 #ifdef GLYPH_DEBUG
14473 debug_method_add (w, "try_scrolling");
14474 #endif
14475
14476 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14477
14478 /* Compute scroll margin height in pixels. We scroll when point is
14479 within this distance from the top or bottom of the window. */
14480 if (scroll_margin > 0)
14481 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14482 * FRAME_LINE_HEIGHT (f);
14483 else
14484 this_scroll_margin = 0;
14485
14486 /* Force arg_scroll_conservatively to have a reasonable value, to
14487 avoid scrolling too far away with slow move_it_* functions. Note
14488 that the user can supply scroll-conservatively equal to
14489 `most-positive-fixnum', which can be larger than INT_MAX. */
14490 if (arg_scroll_conservatively > scroll_limit)
14491 {
14492 arg_scroll_conservatively = scroll_limit + 1;
14493 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14494 }
14495 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14496 /* Compute how much we should try to scroll maximally to bring
14497 point into view. */
14498 scroll_max = (max (scroll_step,
14499 max (arg_scroll_conservatively, temp_scroll_step))
14500 * FRAME_LINE_HEIGHT (f));
14501 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14502 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14503 /* We're trying to scroll because of aggressive scrolling but no
14504 scroll_step is set. Choose an arbitrary one. */
14505 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14506 else
14507 scroll_max = 0;
14508
14509 too_near_end:
14510
14511 /* Decide whether to scroll down. */
14512 if (PT > CHARPOS (startp))
14513 {
14514 int scroll_margin_y;
14515
14516 /* Compute the pixel ypos of the scroll margin, then move IT to
14517 either that ypos or PT, whichever comes first. */
14518 start_display (&it, w, startp);
14519 scroll_margin_y = it.last_visible_y - this_scroll_margin
14520 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14521 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14522 (MOVE_TO_POS | MOVE_TO_Y));
14523
14524 if (PT > CHARPOS (it.current.pos))
14525 {
14526 int y0 = line_bottom_y (&it);
14527 /* Compute how many pixels below window bottom to stop searching
14528 for PT. This avoids costly search for PT that is far away if
14529 the user limited scrolling by a small number of lines, but
14530 always finds PT if scroll_conservatively is set to a large
14531 number, such as most-positive-fixnum. */
14532 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14533 int y_to_move = it.last_visible_y + slack;
14534
14535 /* Compute the distance from the scroll margin to PT or to
14536 the scroll limit, whichever comes first. This should
14537 include the height of the cursor line, to make that line
14538 fully visible. */
14539 move_it_to (&it, PT, -1, y_to_move,
14540 -1, MOVE_TO_POS | MOVE_TO_Y);
14541 dy = line_bottom_y (&it) - y0;
14542
14543 if (dy > scroll_max)
14544 return SCROLLING_FAILED;
14545
14546 if (dy > 0)
14547 scroll_down_p = 1;
14548 }
14549 }
14550
14551 if (scroll_down_p)
14552 {
14553 /* Point is in or below the bottom scroll margin, so move the
14554 window start down. If scrolling conservatively, move it just
14555 enough down to make point visible. If scroll_step is set,
14556 move it down by scroll_step. */
14557 if (arg_scroll_conservatively)
14558 amount_to_scroll
14559 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14560 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14561 else if (scroll_step || temp_scroll_step)
14562 amount_to_scroll = scroll_max;
14563 else
14564 {
14565 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14566 height = WINDOW_BOX_TEXT_HEIGHT (w);
14567 if (NUMBERP (aggressive))
14568 {
14569 double float_amount = XFLOATINT (aggressive) * height;
14570 int aggressive_scroll = float_amount;
14571 if (aggressive_scroll == 0 && float_amount > 0)
14572 aggressive_scroll = 1;
14573 /* Don't let point enter the scroll margin near top of
14574 the window. This could happen if the value of
14575 scroll_up_aggressively is too large and there are
14576 non-zero margins, because scroll_up_aggressively
14577 means put point that fraction of window height
14578 _from_the_bottom_margin_. */
14579 if (aggressive_scroll + 2*this_scroll_margin > height)
14580 aggressive_scroll = height - 2*this_scroll_margin;
14581 amount_to_scroll = dy + aggressive_scroll;
14582 }
14583 }
14584
14585 if (amount_to_scroll <= 0)
14586 return SCROLLING_FAILED;
14587
14588 start_display (&it, w, startp);
14589 if (arg_scroll_conservatively <= scroll_limit)
14590 move_it_vertically (&it, amount_to_scroll);
14591 else
14592 {
14593 /* Extra precision for users who set scroll-conservatively
14594 to a large number: make sure the amount we scroll
14595 the window start is never less than amount_to_scroll,
14596 which was computed as distance from window bottom to
14597 point. This matters when lines at window top and lines
14598 below window bottom have different height. */
14599 struct it it1;
14600 void *it1data = NULL;
14601 /* We use a temporary it1 because line_bottom_y can modify
14602 its argument, if it moves one line down; see there. */
14603 int start_y;
14604
14605 SAVE_IT (it1, it, it1data);
14606 start_y = line_bottom_y (&it1);
14607 do {
14608 RESTORE_IT (&it, &it, it1data);
14609 move_it_by_lines (&it, 1);
14610 SAVE_IT (it1, it, it1data);
14611 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14612 }
14613
14614 /* If STARTP is unchanged, move it down another screen line. */
14615 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14616 move_it_by_lines (&it, 1);
14617 startp = it.current.pos;
14618 }
14619 else
14620 {
14621 struct text_pos scroll_margin_pos = startp;
14622 int y_offset = 0;
14623
14624 /* See if point is inside the scroll margin at the top of the
14625 window. */
14626 if (this_scroll_margin)
14627 {
14628 int y_start;
14629
14630 start_display (&it, w, startp);
14631 y_start = it.current_y;
14632 move_it_vertically (&it, this_scroll_margin);
14633 scroll_margin_pos = it.current.pos;
14634 /* If we didn't move enough before hitting ZV, request
14635 additional amount of scroll, to move point out of the
14636 scroll margin. */
14637 if (IT_CHARPOS (it) == ZV
14638 && it.current_y - y_start < this_scroll_margin)
14639 y_offset = this_scroll_margin - (it.current_y - y_start);
14640 }
14641
14642 if (PT < CHARPOS (scroll_margin_pos))
14643 {
14644 /* Point is in the scroll margin at the top of the window or
14645 above what is displayed in the window. */
14646 int y0, y_to_move;
14647
14648 /* Compute the vertical distance from PT to the scroll
14649 margin position. Move as far as scroll_max allows, or
14650 one screenful, or 10 screen lines, whichever is largest.
14651 Give up if distance is greater than scroll_max or if we
14652 didn't reach the scroll margin position. */
14653 SET_TEXT_POS (pos, PT, PT_BYTE);
14654 start_display (&it, w, pos);
14655 y0 = it.current_y;
14656 y_to_move = max (it.last_visible_y,
14657 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14658 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14659 y_to_move, -1,
14660 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14661 dy = it.current_y - y0;
14662 if (dy > scroll_max
14663 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14664 return SCROLLING_FAILED;
14665
14666 /* Additional scroll for when ZV was too close to point. */
14667 dy += y_offset;
14668
14669 /* Compute new window start. */
14670 start_display (&it, w, startp);
14671
14672 if (arg_scroll_conservatively)
14673 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14674 max (scroll_step, temp_scroll_step));
14675 else if (scroll_step || temp_scroll_step)
14676 amount_to_scroll = scroll_max;
14677 else
14678 {
14679 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14680 height = WINDOW_BOX_TEXT_HEIGHT (w);
14681 if (NUMBERP (aggressive))
14682 {
14683 double float_amount = XFLOATINT (aggressive) * height;
14684 int aggressive_scroll = float_amount;
14685 if (aggressive_scroll == 0 && float_amount > 0)
14686 aggressive_scroll = 1;
14687 /* Don't let point enter the scroll margin near
14688 bottom of the window, if the value of
14689 scroll_down_aggressively happens to be too
14690 large. */
14691 if (aggressive_scroll + 2*this_scroll_margin > height)
14692 aggressive_scroll = height - 2*this_scroll_margin;
14693 amount_to_scroll = dy + aggressive_scroll;
14694 }
14695 }
14696
14697 if (amount_to_scroll <= 0)
14698 return SCROLLING_FAILED;
14699
14700 move_it_vertically_backward (&it, amount_to_scroll);
14701 startp = it.current.pos;
14702 }
14703 }
14704
14705 /* Run window scroll functions. */
14706 startp = run_window_scroll_functions (window, startp);
14707
14708 /* Display the window. Give up if new fonts are loaded, or if point
14709 doesn't appear. */
14710 if (!try_window (window, startp, 0))
14711 rc = SCROLLING_NEED_LARGER_MATRICES;
14712 else if (w->cursor.vpos < 0)
14713 {
14714 clear_glyph_matrix (w->desired_matrix);
14715 rc = SCROLLING_FAILED;
14716 }
14717 else
14718 {
14719 /* Maybe forget recorded base line for line number display. */
14720 if (!just_this_one_p
14721 || current_buffer->clip_changed
14722 || BEG_UNCHANGED < CHARPOS (startp))
14723 w->base_line_number = 0;
14724
14725 /* If cursor ends up on a partially visible line,
14726 treat that as being off the bottom of the screen. */
14727 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14728 /* It's possible that the cursor is on the first line of the
14729 buffer, which is partially obscured due to a vscroll
14730 (Bug#7537). In that case, avoid looping forever . */
14731 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14732 {
14733 clear_glyph_matrix (w->desired_matrix);
14734 ++extra_scroll_margin_lines;
14735 goto too_near_end;
14736 }
14737 rc = SCROLLING_SUCCESS;
14738 }
14739
14740 return rc;
14741 }
14742
14743
14744 /* Compute a suitable window start for window W if display of W starts
14745 on a continuation line. Value is non-zero if a new window start
14746 was computed.
14747
14748 The new window start will be computed, based on W's width, starting
14749 from the start of the continued line. It is the start of the
14750 screen line with the minimum distance from the old start W->start. */
14751
14752 static int
14753 compute_window_start_on_continuation_line (struct window *w)
14754 {
14755 struct text_pos pos, start_pos;
14756 int window_start_changed_p = 0;
14757
14758 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14759
14760 /* If window start is on a continuation line... Window start may be
14761 < BEGV in case there's invisible text at the start of the
14762 buffer (M-x rmail, for example). */
14763 if (CHARPOS (start_pos) > BEGV
14764 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14765 {
14766 struct it it;
14767 struct glyph_row *row;
14768
14769 /* Handle the case that the window start is out of range. */
14770 if (CHARPOS (start_pos) < BEGV)
14771 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14772 else if (CHARPOS (start_pos) > ZV)
14773 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14774
14775 /* Find the start of the continued line. This should be fast
14776 because find_newline is fast (newline cache). */
14777 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14778 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14779 row, DEFAULT_FACE_ID);
14780 reseat_at_previous_visible_line_start (&it);
14781
14782 /* If the line start is "too far" away from the window start,
14783 say it takes too much time to compute a new window start. */
14784 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14785 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14786 {
14787 int min_distance, distance;
14788
14789 /* Move forward by display lines to find the new window
14790 start. If window width was enlarged, the new start can
14791 be expected to be > the old start. If window width was
14792 decreased, the new window start will be < the old start.
14793 So, we're looking for the display line start with the
14794 minimum distance from the old window start. */
14795 pos = it.current.pos;
14796 min_distance = INFINITY;
14797 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14798 distance < min_distance)
14799 {
14800 min_distance = distance;
14801 pos = it.current.pos;
14802 move_it_by_lines (&it, 1);
14803 }
14804
14805 /* Set the window start there. */
14806 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14807 window_start_changed_p = 1;
14808 }
14809 }
14810
14811 return window_start_changed_p;
14812 }
14813
14814
14815 /* Try cursor movement in case text has not changed in window WINDOW,
14816 with window start STARTP. Value is
14817
14818 CURSOR_MOVEMENT_SUCCESS if successful
14819
14820 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14821
14822 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14823 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14824 we want to scroll as if scroll-step were set to 1. See the code.
14825
14826 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14827 which case we have to abort this redisplay, and adjust matrices
14828 first. */
14829
14830 enum
14831 {
14832 CURSOR_MOVEMENT_SUCCESS,
14833 CURSOR_MOVEMENT_CANNOT_BE_USED,
14834 CURSOR_MOVEMENT_MUST_SCROLL,
14835 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14836 };
14837
14838 static int
14839 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14840 {
14841 struct window *w = XWINDOW (window);
14842 struct frame *f = XFRAME (w->frame);
14843 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14844
14845 #ifdef GLYPH_DEBUG
14846 if (inhibit_try_cursor_movement)
14847 return rc;
14848 #endif
14849
14850 /* Previously, there was a check for Lisp integer in the
14851 if-statement below. Now, this field is converted to
14852 ptrdiff_t, thus zero means invalid position in a buffer. */
14853 eassert (w->last_point > 0);
14854
14855 /* Handle case where text has not changed, only point, and it has
14856 not moved off the frame. */
14857 if (/* Point may be in this window. */
14858 PT >= CHARPOS (startp)
14859 /* Selective display hasn't changed. */
14860 && !current_buffer->clip_changed
14861 /* Function force-mode-line-update is used to force a thorough
14862 redisplay. It sets either windows_or_buffers_changed or
14863 update_mode_lines. So don't take a shortcut here for these
14864 cases. */
14865 && !update_mode_lines
14866 && !windows_or_buffers_changed
14867 && !cursor_type_changed
14868 /* Can't use this case if highlighting a region. When a
14869 region exists, cursor movement has to do more than just
14870 set the cursor. */
14871 && markpos_of_region () < 0
14872 && !w->region_showing
14873 && NILP (Vshow_trailing_whitespace)
14874 /* This code is not used for mini-buffer for the sake of the case
14875 of redisplaying to replace an echo area message; since in
14876 that case the mini-buffer contents per se are usually
14877 unchanged. This code is of no real use in the mini-buffer
14878 since the handling of this_line_start_pos, etc., in redisplay
14879 handles the same cases. */
14880 && !EQ (window, minibuf_window)
14881 /* When splitting windows or for new windows, it happens that
14882 redisplay is called with a nil window_end_vpos or one being
14883 larger than the window. This should really be fixed in
14884 window.c. I don't have this on my list, now, so we do
14885 approximately the same as the old redisplay code. --gerd. */
14886 && INTEGERP (w->window_end_vpos)
14887 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14888 && (FRAME_WINDOW_P (f)
14889 || !overlay_arrow_in_current_buffer_p ()))
14890 {
14891 int this_scroll_margin, top_scroll_margin;
14892 struct glyph_row *row = NULL;
14893
14894 #ifdef GLYPH_DEBUG
14895 debug_method_add (w, "cursor movement");
14896 #endif
14897
14898 /* Scroll if point within this distance from the top or bottom
14899 of the window. This is a pixel value. */
14900 if (scroll_margin > 0)
14901 {
14902 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14903 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14904 }
14905 else
14906 this_scroll_margin = 0;
14907
14908 top_scroll_margin = this_scroll_margin;
14909 if (WINDOW_WANTS_HEADER_LINE_P (w))
14910 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14911
14912 /* Start with the row the cursor was displayed during the last
14913 not paused redisplay. Give up if that row is not valid. */
14914 if (w->last_cursor.vpos < 0
14915 || w->last_cursor.vpos >= w->current_matrix->nrows)
14916 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14917 else
14918 {
14919 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14920 if (row->mode_line_p)
14921 ++row;
14922 if (!row->enabled_p)
14923 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14924 }
14925
14926 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14927 {
14928 int scroll_p = 0, must_scroll = 0;
14929 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14930
14931 if (PT > w->last_point)
14932 {
14933 /* Point has moved forward. */
14934 while (MATRIX_ROW_END_CHARPOS (row) < PT
14935 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14936 {
14937 eassert (row->enabled_p);
14938 ++row;
14939 }
14940
14941 /* If the end position of a row equals the start
14942 position of the next row, and PT is at that position,
14943 we would rather display cursor in the next line. */
14944 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14945 && MATRIX_ROW_END_CHARPOS (row) == PT
14946 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
14947 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14948 && !cursor_row_p (row))
14949 ++row;
14950
14951 /* If within the scroll margin, scroll. Note that
14952 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14953 the next line would be drawn, and that
14954 this_scroll_margin can be zero. */
14955 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14956 || PT > MATRIX_ROW_END_CHARPOS (row)
14957 /* Line is completely visible last line in window
14958 and PT is to be set in the next line. */
14959 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14960 && PT == MATRIX_ROW_END_CHARPOS (row)
14961 && !row->ends_at_zv_p
14962 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14963 scroll_p = 1;
14964 }
14965 else if (PT < w->last_point)
14966 {
14967 /* Cursor has to be moved backward. Note that PT >=
14968 CHARPOS (startp) because of the outer if-statement. */
14969 while (!row->mode_line_p
14970 && (MATRIX_ROW_START_CHARPOS (row) > PT
14971 || (MATRIX_ROW_START_CHARPOS (row) == PT
14972 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14973 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14974 row > w->current_matrix->rows
14975 && (row-1)->ends_in_newline_from_string_p))))
14976 && (row->y > top_scroll_margin
14977 || CHARPOS (startp) == BEGV))
14978 {
14979 eassert (row->enabled_p);
14980 --row;
14981 }
14982
14983 /* Consider the following case: Window starts at BEGV,
14984 there is invisible, intangible text at BEGV, so that
14985 display starts at some point START > BEGV. It can
14986 happen that we are called with PT somewhere between
14987 BEGV and START. Try to handle that case. */
14988 if (row < w->current_matrix->rows
14989 || row->mode_line_p)
14990 {
14991 row = w->current_matrix->rows;
14992 if (row->mode_line_p)
14993 ++row;
14994 }
14995
14996 /* Due to newlines in overlay strings, we may have to
14997 skip forward over overlay strings. */
14998 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14999 && MATRIX_ROW_END_CHARPOS (row) == PT
15000 && !cursor_row_p (row))
15001 ++row;
15002
15003 /* If within the scroll margin, scroll. */
15004 if (row->y < top_scroll_margin
15005 && CHARPOS (startp) != BEGV)
15006 scroll_p = 1;
15007 }
15008 else
15009 {
15010 /* Cursor did not move. So don't scroll even if cursor line
15011 is partially visible, as it was so before. */
15012 rc = CURSOR_MOVEMENT_SUCCESS;
15013 }
15014
15015 if (PT < MATRIX_ROW_START_CHARPOS (row)
15016 || PT > MATRIX_ROW_END_CHARPOS (row))
15017 {
15018 /* if PT is not in the glyph row, give up. */
15019 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15020 must_scroll = 1;
15021 }
15022 else if (rc != CURSOR_MOVEMENT_SUCCESS
15023 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15024 {
15025 struct glyph_row *row1;
15026
15027 /* If rows are bidi-reordered and point moved, back up
15028 until we find a row that does not belong to a
15029 continuation line. This is because we must consider
15030 all rows of a continued line as candidates for the
15031 new cursor positioning, since row start and end
15032 positions change non-linearly with vertical position
15033 in such rows. */
15034 /* FIXME: Revisit this when glyph ``spilling'' in
15035 continuation lines' rows is implemented for
15036 bidi-reordered rows. */
15037 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15038 MATRIX_ROW_CONTINUATION_LINE_P (row);
15039 --row)
15040 {
15041 /* If we hit the beginning of the displayed portion
15042 without finding the first row of a continued
15043 line, give up. */
15044 if (row <= row1)
15045 {
15046 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15047 break;
15048 }
15049 eassert (row->enabled_p);
15050 }
15051 }
15052 if (must_scroll)
15053 ;
15054 else if (rc != CURSOR_MOVEMENT_SUCCESS
15055 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15056 /* Make sure this isn't a header line by any chance, since
15057 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15058 && !row->mode_line_p
15059 && make_cursor_line_fully_visible_p)
15060 {
15061 if (PT == MATRIX_ROW_END_CHARPOS (row)
15062 && !row->ends_at_zv_p
15063 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15064 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15065 else if (row->height > window_box_height (w))
15066 {
15067 /* If we end up in a partially visible line, let's
15068 make it fully visible, except when it's taller
15069 than the window, in which case we can't do much
15070 about it. */
15071 *scroll_step = 1;
15072 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15073 }
15074 else
15075 {
15076 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15077 if (!cursor_row_fully_visible_p (w, 0, 1))
15078 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15079 else
15080 rc = CURSOR_MOVEMENT_SUCCESS;
15081 }
15082 }
15083 else if (scroll_p)
15084 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15085 else if (rc != CURSOR_MOVEMENT_SUCCESS
15086 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15087 {
15088 /* With bidi-reordered rows, there could be more than
15089 one candidate row whose start and end positions
15090 occlude point. We need to let set_cursor_from_row
15091 find the best candidate. */
15092 /* FIXME: Revisit this when glyph ``spilling'' in
15093 continuation lines' rows is implemented for
15094 bidi-reordered rows. */
15095 int rv = 0;
15096
15097 do
15098 {
15099 int at_zv_p = 0, exact_match_p = 0;
15100
15101 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15102 && PT <= MATRIX_ROW_END_CHARPOS (row)
15103 && cursor_row_p (row))
15104 rv |= set_cursor_from_row (w, row, w->current_matrix,
15105 0, 0, 0, 0);
15106 /* As soon as we've found the exact match for point,
15107 or the first suitable row whose ends_at_zv_p flag
15108 is set, we are done. */
15109 at_zv_p =
15110 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15111 if (rv && !at_zv_p
15112 && w->cursor.hpos >= 0
15113 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15114 w->cursor.vpos))
15115 {
15116 struct glyph_row *candidate =
15117 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15118 struct glyph *g =
15119 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15120 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15121
15122 exact_match_p =
15123 (BUFFERP (g->object) && g->charpos == PT)
15124 || (INTEGERP (g->object)
15125 && (g->charpos == PT
15126 || (g->charpos == 0 && endpos - 1 == PT)));
15127 }
15128 if (rv && (at_zv_p || exact_match_p))
15129 {
15130 rc = CURSOR_MOVEMENT_SUCCESS;
15131 break;
15132 }
15133 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15134 break;
15135 ++row;
15136 }
15137 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15138 || row->continued_p)
15139 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15140 || (MATRIX_ROW_START_CHARPOS (row) == PT
15141 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15142 /* If we didn't find any candidate rows, or exited the
15143 loop before all the candidates were examined, signal
15144 to the caller that this method failed. */
15145 if (rc != CURSOR_MOVEMENT_SUCCESS
15146 && !(rv
15147 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15148 && !row->continued_p))
15149 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15150 else if (rv)
15151 rc = CURSOR_MOVEMENT_SUCCESS;
15152 }
15153 else
15154 {
15155 do
15156 {
15157 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15158 {
15159 rc = CURSOR_MOVEMENT_SUCCESS;
15160 break;
15161 }
15162 ++row;
15163 }
15164 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15165 && MATRIX_ROW_START_CHARPOS (row) == PT
15166 && cursor_row_p (row));
15167 }
15168 }
15169 }
15170
15171 return rc;
15172 }
15173
15174 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15175 static
15176 #endif
15177 void
15178 set_vertical_scroll_bar (struct window *w)
15179 {
15180 ptrdiff_t start, end, whole;
15181
15182 /* Calculate the start and end positions for the current window.
15183 At some point, it would be nice to choose between scrollbars
15184 which reflect the whole buffer size, with special markers
15185 indicating narrowing, and scrollbars which reflect only the
15186 visible region.
15187
15188 Note that mini-buffers sometimes aren't displaying any text. */
15189 if (!MINI_WINDOW_P (w)
15190 || (w == XWINDOW (minibuf_window)
15191 && NILP (echo_area_buffer[0])))
15192 {
15193 struct buffer *buf = XBUFFER (w->contents);
15194 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15195 start = marker_position (w->start) - BUF_BEGV (buf);
15196 /* I don't think this is guaranteed to be right. For the
15197 moment, we'll pretend it is. */
15198 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15199
15200 if (end < start)
15201 end = start;
15202 if (whole < (end - start))
15203 whole = end - start;
15204 }
15205 else
15206 start = end = whole = 0;
15207
15208 /* Indicate what this scroll bar ought to be displaying now. */
15209 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15210 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15211 (w, end - start, whole, start);
15212 }
15213
15214
15215 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15216 selected_window is redisplayed.
15217
15218 We can return without actually redisplaying the window if
15219 fonts_changed_p. In that case, redisplay_internal will
15220 retry. */
15221
15222 static void
15223 redisplay_window (Lisp_Object window, int just_this_one_p)
15224 {
15225 struct window *w = XWINDOW (window);
15226 struct frame *f = XFRAME (w->frame);
15227 struct buffer *buffer = XBUFFER (w->contents);
15228 struct buffer *old = current_buffer;
15229 struct text_pos lpoint, opoint, startp;
15230 int update_mode_line;
15231 int tem;
15232 struct it it;
15233 /* Record it now because it's overwritten. */
15234 int current_matrix_up_to_date_p = 0;
15235 int used_current_matrix_p = 0;
15236 /* This is less strict than current_matrix_up_to_date_p.
15237 It indicates that the buffer contents and narrowing are unchanged. */
15238 int buffer_unchanged_p = 0;
15239 int temp_scroll_step = 0;
15240 ptrdiff_t count = SPECPDL_INDEX ();
15241 int rc;
15242 int centering_position = -1;
15243 int last_line_misfit = 0;
15244 ptrdiff_t beg_unchanged, end_unchanged;
15245
15246 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15247 opoint = lpoint;
15248
15249 #ifdef GLYPH_DEBUG
15250 *w->desired_matrix->method = 0;
15251 #endif
15252
15253 /* Make sure that both W's markers are valid. */
15254 eassert (XMARKER (w->start)->buffer == buffer);
15255 eassert (XMARKER (w->pointm)->buffer == buffer);
15256
15257 restart:
15258 reconsider_clip_changes (w, buffer);
15259
15260 /* Has the mode line to be updated? */
15261 update_mode_line = (w->update_mode_line
15262 || update_mode_lines
15263 || buffer->clip_changed
15264 || buffer->prevent_redisplay_optimizations_p);
15265
15266 if (MINI_WINDOW_P (w))
15267 {
15268 if (w == XWINDOW (echo_area_window)
15269 && !NILP (echo_area_buffer[0]))
15270 {
15271 if (update_mode_line)
15272 /* We may have to update a tty frame's menu bar or a
15273 tool-bar. Example `M-x C-h C-h C-g'. */
15274 goto finish_menu_bars;
15275 else
15276 /* We've already displayed the echo area glyphs in this window. */
15277 goto finish_scroll_bars;
15278 }
15279 else if ((w != XWINDOW (minibuf_window)
15280 || minibuf_level == 0)
15281 /* When buffer is nonempty, redisplay window normally. */
15282 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15283 /* Quail displays non-mini buffers in minibuffer window.
15284 In that case, redisplay the window normally. */
15285 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15286 {
15287 /* W is a mini-buffer window, but it's not active, so clear
15288 it. */
15289 int yb = window_text_bottom_y (w);
15290 struct glyph_row *row;
15291 int y;
15292
15293 for (y = 0, row = w->desired_matrix->rows;
15294 y < yb;
15295 y += row->height, ++row)
15296 blank_row (w, row, y);
15297 goto finish_scroll_bars;
15298 }
15299
15300 clear_glyph_matrix (w->desired_matrix);
15301 }
15302
15303 /* Otherwise set up data on this window; select its buffer and point
15304 value. */
15305 /* Really select the buffer, for the sake of buffer-local
15306 variables. */
15307 set_buffer_internal_1 (XBUFFER (w->contents));
15308
15309 current_matrix_up_to_date_p
15310 = (w->window_end_valid
15311 && !current_buffer->clip_changed
15312 && !current_buffer->prevent_redisplay_optimizations_p
15313 && !window_outdated (w));
15314
15315 /* Run the window-bottom-change-functions
15316 if it is possible that the text on the screen has changed
15317 (either due to modification of the text, or any other reason). */
15318 if (!current_matrix_up_to_date_p
15319 && !NILP (Vwindow_text_change_functions))
15320 {
15321 safe_run_hooks (Qwindow_text_change_functions);
15322 goto restart;
15323 }
15324
15325 beg_unchanged = BEG_UNCHANGED;
15326 end_unchanged = END_UNCHANGED;
15327
15328 SET_TEXT_POS (opoint, PT, PT_BYTE);
15329
15330 specbind (Qinhibit_point_motion_hooks, Qt);
15331
15332 buffer_unchanged_p
15333 = (w->window_end_valid
15334 && !current_buffer->clip_changed
15335 && !window_outdated (w));
15336
15337 /* When windows_or_buffers_changed is non-zero, we can't rely on
15338 the window end being valid, so set it to nil there. */
15339 if (windows_or_buffers_changed)
15340 {
15341 /* If window starts on a continuation line, maybe adjust the
15342 window start in case the window's width changed. */
15343 if (XMARKER (w->start)->buffer == current_buffer)
15344 compute_window_start_on_continuation_line (w);
15345
15346 w->window_end_valid = 0;
15347 }
15348
15349 /* Some sanity checks. */
15350 CHECK_WINDOW_END (w);
15351 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15352 emacs_abort ();
15353 if (BYTEPOS (opoint) < CHARPOS (opoint))
15354 emacs_abort ();
15355
15356 if (mode_line_update_needed (w))
15357 update_mode_line = 1;
15358
15359 /* Point refers normally to the selected window. For any other
15360 window, set up appropriate value. */
15361 if (!EQ (window, selected_window))
15362 {
15363 ptrdiff_t new_pt = marker_position (w->pointm);
15364 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15365 if (new_pt < BEGV)
15366 {
15367 new_pt = BEGV;
15368 new_pt_byte = BEGV_BYTE;
15369 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15370 }
15371 else if (new_pt > (ZV - 1))
15372 {
15373 new_pt = ZV;
15374 new_pt_byte = ZV_BYTE;
15375 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15376 }
15377
15378 /* We don't use SET_PT so that the point-motion hooks don't run. */
15379 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15380 }
15381
15382 /* If any of the character widths specified in the display table
15383 have changed, invalidate the width run cache. It's true that
15384 this may be a bit late to catch such changes, but the rest of
15385 redisplay goes (non-fatally) haywire when the display table is
15386 changed, so why should we worry about doing any better? */
15387 if (current_buffer->width_run_cache)
15388 {
15389 struct Lisp_Char_Table *disptab = buffer_display_table ();
15390
15391 if (! disptab_matches_widthtab
15392 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15393 {
15394 invalidate_region_cache (current_buffer,
15395 current_buffer->width_run_cache,
15396 BEG, Z);
15397 recompute_width_table (current_buffer, disptab);
15398 }
15399 }
15400
15401 /* If window-start is screwed up, choose a new one. */
15402 if (XMARKER (w->start)->buffer != current_buffer)
15403 goto recenter;
15404
15405 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15406
15407 /* If someone specified a new starting point but did not insist,
15408 check whether it can be used. */
15409 if (w->optional_new_start
15410 && CHARPOS (startp) >= BEGV
15411 && CHARPOS (startp) <= ZV)
15412 {
15413 w->optional_new_start = 0;
15414 start_display (&it, w, startp);
15415 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15416 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15417 if (IT_CHARPOS (it) == PT)
15418 w->force_start = 1;
15419 /* IT may overshoot PT if text at PT is invisible. */
15420 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15421 w->force_start = 1;
15422 }
15423
15424 force_start:
15425
15426 /* Handle case where place to start displaying has been specified,
15427 unless the specified location is outside the accessible range. */
15428 if (w->force_start || w->frozen_window_start_p)
15429 {
15430 /* We set this later on if we have to adjust point. */
15431 int new_vpos = -1;
15432
15433 w->force_start = 0;
15434 w->vscroll = 0;
15435 w->window_end_valid = 0;
15436
15437 /* Forget any recorded base line for line number display. */
15438 if (!buffer_unchanged_p)
15439 w->base_line_number = 0;
15440
15441 /* Redisplay the mode line. Select the buffer properly for that.
15442 Also, run the hook window-scroll-functions
15443 because we have scrolled. */
15444 /* Note, we do this after clearing force_start because
15445 if there's an error, it is better to forget about force_start
15446 than to get into an infinite loop calling the hook functions
15447 and having them get more errors. */
15448 if (!update_mode_line
15449 || ! NILP (Vwindow_scroll_functions))
15450 {
15451 update_mode_line = 1;
15452 w->update_mode_line = 1;
15453 startp = run_window_scroll_functions (window, startp);
15454 }
15455
15456 w->last_modified = 0;
15457 w->last_overlay_modified = 0;
15458 if (CHARPOS (startp) < BEGV)
15459 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15460 else if (CHARPOS (startp) > ZV)
15461 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15462
15463 /* Redisplay, then check if cursor has been set during the
15464 redisplay. Give up if new fonts were loaded. */
15465 /* We used to issue a CHECK_MARGINS argument to try_window here,
15466 but this causes scrolling to fail when point begins inside
15467 the scroll margin (bug#148) -- cyd */
15468 if (!try_window (window, startp, 0))
15469 {
15470 w->force_start = 1;
15471 clear_glyph_matrix (w->desired_matrix);
15472 goto need_larger_matrices;
15473 }
15474
15475 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15476 {
15477 /* If point does not appear, try to move point so it does
15478 appear. The desired matrix has been built above, so we
15479 can use it here. */
15480 new_vpos = window_box_height (w) / 2;
15481 }
15482
15483 if (!cursor_row_fully_visible_p (w, 0, 0))
15484 {
15485 /* Point does appear, but on a line partly visible at end of window.
15486 Move it back to a fully-visible line. */
15487 new_vpos = window_box_height (w);
15488 }
15489 else if (w->cursor.vpos >=0)
15490 {
15491 /* Some people insist on not letting point enter the scroll
15492 margin, even though this part handles windows that didn't
15493 scroll at all. */
15494 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15495 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15496 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15497
15498 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15499 below, which finds the row to move point to, advances by
15500 the Y coordinate of the _next_ row, see the definition of
15501 MATRIX_ROW_BOTTOM_Y. */
15502 if (w->cursor.vpos < margin + header_line)
15503 new_vpos
15504 = pixel_margin + (header_line
15505 ? CURRENT_HEADER_LINE_HEIGHT (w)
15506 : 0) + FRAME_LINE_HEIGHT (f);
15507 else
15508 {
15509 int window_height = window_box_height (w);
15510
15511 if (header_line)
15512 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15513 if (w->cursor.y >= window_height - pixel_margin)
15514 new_vpos = window_height - pixel_margin;
15515 }
15516 }
15517
15518 /* If we need to move point for either of the above reasons,
15519 now actually do it. */
15520 if (new_vpos >= 0)
15521 {
15522 struct glyph_row *row;
15523
15524 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15525 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15526 ++row;
15527
15528 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15529 MATRIX_ROW_START_BYTEPOS (row));
15530
15531 if (w != XWINDOW (selected_window))
15532 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15533 else if (current_buffer == old)
15534 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15535
15536 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15537
15538 /* If we are highlighting the region, then we just changed
15539 the region, so redisplay to show it. */
15540 if (markpos_of_region () >= 0)
15541 {
15542 clear_glyph_matrix (w->desired_matrix);
15543 if (!try_window (window, startp, 0))
15544 goto need_larger_matrices;
15545 }
15546 }
15547
15548 #ifdef GLYPH_DEBUG
15549 debug_method_add (w, "forced window start");
15550 #endif
15551 goto done;
15552 }
15553
15554 /* Handle case where text has not changed, only point, and it has
15555 not moved off the frame, and we are not retrying after hscroll.
15556 (current_matrix_up_to_date_p is nonzero when retrying.) */
15557 if (current_matrix_up_to_date_p
15558 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15559 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15560 {
15561 switch (rc)
15562 {
15563 case CURSOR_MOVEMENT_SUCCESS:
15564 used_current_matrix_p = 1;
15565 goto done;
15566
15567 case CURSOR_MOVEMENT_MUST_SCROLL:
15568 goto try_to_scroll;
15569
15570 default:
15571 emacs_abort ();
15572 }
15573 }
15574 /* If current starting point was originally the beginning of a line
15575 but no longer is, find a new starting point. */
15576 else if (w->start_at_line_beg
15577 && !(CHARPOS (startp) <= BEGV
15578 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15579 {
15580 #ifdef GLYPH_DEBUG
15581 debug_method_add (w, "recenter 1");
15582 #endif
15583 goto recenter;
15584 }
15585
15586 /* Try scrolling with try_window_id. Value is > 0 if update has
15587 been done, it is -1 if we know that the same window start will
15588 not work. It is 0 if unsuccessful for some other reason. */
15589 else if ((tem = try_window_id (w)) != 0)
15590 {
15591 #ifdef GLYPH_DEBUG
15592 debug_method_add (w, "try_window_id %d", tem);
15593 #endif
15594
15595 if (fonts_changed_p)
15596 goto need_larger_matrices;
15597 if (tem > 0)
15598 goto done;
15599
15600 /* Otherwise try_window_id has returned -1 which means that we
15601 don't want the alternative below this comment to execute. */
15602 }
15603 else if (CHARPOS (startp) >= BEGV
15604 && CHARPOS (startp) <= ZV
15605 && PT >= CHARPOS (startp)
15606 && (CHARPOS (startp) < ZV
15607 /* Avoid starting at end of buffer. */
15608 || CHARPOS (startp) == BEGV
15609 || !window_outdated (w)))
15610 {
15611 int d1, d2, d3, d4, d5, d6;
15612
15613 /* If first window line is a continuation line, and window start
15614 is inside the modified region, but the first change is before
15615 current window start, we must select a new window start.
15616
15617 However, if this is the result of a down-mouse event (e.g. by
15618 extending the mouse-drag-overlay), we don't want to select a
15619 new window start, since that would change the position under
15620 the mouse, resulting in an unwanted mouse-movement rather
15621 than a simple mouse-click. */
15622 if (!w->start_at_line_beg
15623 && NILP (do_mouse_tracking)
15624 && CHARPOS (startp) > BEGV
15625 && CHARPOS (startp) > BEG + beg_unchanged
15626 && CHARPOS (startp) <= Z - end_unchanged
15627 /* Even if w->start_at_line_beg is nil, a new window may
15628 start at a line_beg, since that's how set_buffer_window
15629 sets it. So, we need to check the return value of
15630 compute_window_start_on_continuation_line. (See also
15631 bug#197). */
15632 && XMARKER (w->start)->buffer == current_buffer
15633 && compute_window_start_on_continuation_line (w)
15634 /* It doesn't make sense to force the window start like we
15635 do at label force_start if it is already known that point
15636 will not be visible in the resulting window, because
15637 doing so will move point from its correct position
15638 instead of scrolling the window to bring point into view.
15639 See bug#9324. */
15640 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15641 {
15642 w->force_start = 1;
15643 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15644 goto force_start;
15645 }
15646
15647 #ifdef GLYPH_DEBUG
15648 debug_method_add (w, "same window start");
15649 #endif
15650
15651 /* Try to redisplay starting at same place as before.
15652 If point has not moved off frame, accept the results. */
15653 if (!current_matrix_up_to_date_p
15654 /* Don't use try_window_reusing_current_matrix in this case
15655 because a window scroll function can have changed the
15656 buffer. */
15657 || !NILP (Vwindow_scroll_functions)
15658 || MINI_WINDOW_P (w)
15659 || !(used_current_matrix_p
15660 = try_window_reusing_current_matrix (w)))
15661 {
15662 IF_DEBUG (debug_method_add (w, "1"));
15663 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15664 /* -1 means we need to scroll.
15665 0 means we need new matrices, but fonts_changed_p
15666 is set in that case, so we will detect it below. */
15667 goto try_to_scroll;
15668 }
15669
15670 if (fonts_changed_p)
15671 goto need_larger_matrices;
15672
15673 if (w->cursor.vpos >= 0)
15674 {
15675 if (!just_this_one_p
15676 || current_buffer->clip_changed
15677 || BEG_UNCHANGED < CHARPOS (startp))
15678 /* Forget any recorded base line for line number display. */
15679 w->base_line_number = 0;
15680
15681 if (!cursor_row_fully_visible_p (w, 1, 0))
15682 {
15683 clear_glyph_matrix (w->desired_matrix);
15684 last_line_misfit = 1;
15685 }
15686 /* Drop through and scroll. */
15687 else
15688 goto done;
15689 }
15690 else
15691 clear_glyph_matrix (w->desired_matrix);
15692 }
15693
15694 try_to_scroll:
15695
15696 w->last_modified = 0;
15697 w->last_overlay_modified = 0;
15698
15699 /* Redisplay the mode line. Select the buffer properly for that. */
15700 if (!update_mode_line)
15701 {
15702 update_mode_line = 1;
15703 w->update_mode_line = 1;
15704 }
15705
15706 /* Try to scroll by specified few lines. */
15707 if ((scroll_conservatively
15708 || emacs_scroll_step
15709 || temp_scroll_step
15710 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15711 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15712 && CHARPOS (startp) >= BEGV
15713 && CHARPOS (startp) <= ZV)
15714 {
15715 /* The function returns -1 if new fonts were loaded, 1 if
15716 successful, 0 if not successful. */
15717 int ss = try_scrolling (window, just_this_one_p,
15718 scroll_conservatively,
15719 emacs_scroll_step,
15720 temp_scroll_step, last_line_misfit);
15721 switch (ss)
15722 {
15723 case SCROLLING_SUCCESS:
15724 goto done;
15725
15726 case SCROLLING_NEED_LARGER_MATRICES:
15727 goto need_larger_matrices;
15728
15729 case SCROLLING_FAILED:
15730 break;
15731
15732 default:
15733 emacs_abort ();
15734 }
15735 }
15736
15737 /* Finally, just choose a place to start which positions point
15738 according to user preferences. */
15739
15740 recenter:
15741
15742 #ifdef GLYPH_DEBUG
15743 debug_method_add (w, "recenter");
15744 #endif
15745
15746 /* Forget any previously recorded base line for line number display. */
15747 if (!buffer_unchanged_p)
15748 w->base_line_number = 0;
15749
15750 /* Determine the window start relative to point. */
15751 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15752 it.current_y = it.last_visible_y;
15753 if (centering_position < 0)
15754 {
15755 int margin =
15756 scroll_margin > 0
15757 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15758 : 0;
15759 ptrdiff_t margin_pos = CHARPOS (startp);
15760 Lisp_Object aggressive;
15761 int scrolling_up;
15762
15763 /* If there is a scroll margin at the top of the window, find
15764 its character position. */
15765 if (margin
15766 /* Cannot call start_display if startp is not in the
15767 accessible region of the buffer. This can happen when we
15768 have just switched to a different buffer and/or changed
15769 its restriction. In that case, startp is initialized to
15770 the character position 1 (BEGV) because we did not yet
15771 have chance to display the buffer even once. */
15772 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15773 {
15774 struct it it1;
15775 void *it1data = NULL;
15776
15777 SAVE_IT (it1, it, it1data);
15778 start_display (&it1, w, startp);
15779 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15780 margin_pos = IT_CHARPOS (it1);
15781 RESTORE_IT (&it, &it, it1data);
15782 }
15783 scrolling_up = PT > margin_pos;
15784 aggressive =
15785 scrolling_up
15786 ? BVAR (current_buffer, scroll_up_aggressively)
15787 : BVAR (current_buffer, scroll_down_aggressively);
15788
15789 if (!MINI_WINDOW_P (w)
15790 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15791 {
15792 int pt_offset = 0;
15793
15794 /* Setting scroll-conservatively overrides
15795 scroll-*-aggressively. */
15796 if (!scroll_conservatively && NUMBERP (aggressive))
15797 {
15798 double float_amount = XFLOATINT (aggressive);
15799
15800 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15801 if (pt_offset == 0 && float_amount > 0)
15802 pt_offset = 1;
15803 if (pt_offset && margin > 0)
15804 margin -= 1;
15805 }
15806 /* Compute how much to move the window start backward from
15807 point so that point will be displayed where the user
15808 wants it. */
15809 if (scrolling_up)
15810 {
15811 centering_position = it.last_visible_y;
15812 if (pt_offset)
15813 centering_position -= pt_offset;
15814 centering_position -=
15815 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15816 + WINDOW_HEADER_LINE_HEIGHT (w);
15817 /* Don't let point enter the scroll margin near top of
15818 the window. */
15819 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15820 centering_position = margin * FRAME_LINE_HEIGHT (f);
15821 }
15822 else
15823 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15824 }
15825 else
15826 /* Set the window start half the height of the window backward
15827 from point. */
15828 centering_position = window_box_height (w) / 2;
15829 }
15830 move_it_vertically_backward (&it, centering_position);
15831
15832 eassert (IT_CHARPOS (it) >= BEGV);
15833
15834 /* The function move_it_vertically_backward may move over more
15835 than the specified y-distance. If it->w is small, e.g. a
15836 mini-buffer window, we may end up in front of the window's
15837 display area. Start displaying at the start of the line
15838 containing PT in this case. */
15839 if (it.current_y <= 0)
15840 {
15841 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15842 move_it_vertically_backward (&it, 0);
15843 it.current_y = 0;
15844 }
15845
15846 it.current_x = it.hpos = 0;
15847
15848 /* Set the window start position here explicitly, to avoid an
15849 infinite loop in case the functions in window-scroll-functions
15850 get errors. */
15851 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15852
15853 /* Run scroll hooks. */
15854 startp = run_window_scroll_functions (window, it.current.pos);
15855
15856 /* Redisplay the window. */
15857 if (!current_matrix_up_to_date_p
15858 || windows_or_buffers_changed
15859 || cursor_type_changed
15860 /* Don't use try_window_reusing_current_matrix in this case
15861 because it can have changed the buffer. */
15862 || !NILP (Vwindow_scroll_functions)
15863 || !just_this_one_p
15864 || MINI_WINDOW_P (w)
15865 || !(used_current_matrix_p
15866 = try_window_reusing_current_matrix (w)))
15867 try_window (window, startp, 0);
15868
15869 /* If new fonts have been loaded (due to fontsets), give up. We
15870 have to start a new redisplay since we need to re-adjust glyph
15871 matrices. */
15872 if (fonts_changed_p)
15873 goto need_larger_matrices;
15874
15875 /* If cursor did not appear assume that the middle of the window is
15876 in the first line of the window. Do it again with the next line.
15877 (Imagine a window of height 100, displaying two lines of height
15878 60. Moving back 50 from it->last_visible_y will end in the first
15879 line.) */
15880 if (w->cursor.vpos < 0)
15881 {
15882 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15883 {
15884 clear_glyph_matrix (w->desired_matrix);
15885 move_it_by_lines (&it, 1);
15886 try_window (window, it.current.pos, 0);
15887 }
15888 else if (PT < IT_CHARPOS (it))
15889 {
15890 clear_glyph_matrix (w->desired_matrix);
15891 move_it_by_lines (&it, -1);
15892 try_window (window, it.current.pos, 0);
15893 }
15894 else
15895 {
15896 /* Not much we can do about it. */
15897 }
15898 }
15899
15900 /* Consider the following case: Window starts at BEGV, there is
15901 invisible, intangible text at BEGV, so that display starts at
15902 some point START > BEGV. It can happen that we are called with
15903 PT somewhere between BEGV and START. Try to handle that case. */
15904 if (w->cursor.vpos < 0)
15905 {
15906 struct glyph_row *row = w->current_matrix->rows;
15907 if (row->mode_line_p)
15908 ++row;
15909 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15910 }
15911
15912 if (!cursor_row_fully_visible_p (w, 0, 0))
15913 {
15914 /* If vscroll is enabled, disable it and try again. */
15915 if (w->vscroll)
15916 {
15917 w->vscroll = 0;
15918 clear_glyph_matrix (w->desired_matrix);
15919 goto recenter;
15920 }
15921
15922 /* Users who set scroll-conservatively to a large number want
15923 point just above/below the scroll margin. If we ended up
15924 with point's row partially visible, move the window start to
15925 make that row fully visible and out of the margin. */
15926 if (scroll_conservatively > SCROLL_LIMIT)
15927 {
15928 int margin =
15929 scroll_margin > 0
15930 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15931 : 0;
15932 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15933
15934 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15935 clear_glyph_matrix (w->desired_matrix);
15936 if (1 == try_window (window, it.current.pos,
15937 TRY_WINDOW_CHECK_MARGINS))
15938 goto done;
15939 }
15940
15941 /* If centering point failed to make the whole line visible,
15942 put point at the top instead. That has to make the whole line
15943 visible, if it can be done. */
15944 if (centering_position == 0)
15945 goto done;
15946
15947 clear_glyph_matrix (w->desired_matrix);
15948 centering_position = 0;
15949 goto recenter;
15950 }
15951
15952 done:
15953
15954 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15955 w->start_at_line_beg = (CHARPOS (startp) == BEGV
15956 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
15957
15958 /* Display the mode line, if we must. */
15959 if ((update_mode_line
15960 /* If window not full width, must redo its mode line
15961 if (a) the window to its side is being redone and
15962 (b) we do a frame-based redisplay. This is a consequence
15963 of how inverted lines are drawn in frame-based redisplay. */
15964 || (!just_this_one_p
15965 && !FRAME_WINDOW_P (f)
15966 && !WINDOW_FULL_WIDTH_P (w))
15967 /* Line number to display. */
15968 || w->base_line_pos > 0
15969 /* Column number is displayed and different from the one displayed. */
15970 || (w->column_number_displayed != -1
15971 && (w->column_number_displayed != current_column ())))
15972 /* This means that the window has a mode line. */
15973 && (WINDOW_WANTS_MODELINE_P (w)
15974 || WINDOW_WANTS_HEADER_LINE_P (w)))
15975 {
15976 display_mode_lines (w);
15977
15978 /* If mode line height has changed, arrange for a thorough
15979 immediate redisplay using the correct mode line height. */
15980 if (WINDOW_WANTS_MODELINE_P (w)
15981 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15982 {
15983 fonts_changed_p = 1;
15984 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15985 = DESIRED_MODE_LINE_HEIGHT (w);
15986 }
15987
15988 /* If header line height has changed, arrange for a thorough
15989 immediate redisplay using the correct header line height. */
15990 if (WINDOW_WANTS_HEADER_LINE_P (w)
15991 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15992 {
15993 fonts_changed_p = 1;
15994 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15995 = DESIRED_HEADER_LINE_HEIGHT (w);
15996 }
15997
15998 if (fonts_changed_p)
15999 goto need_larger_matrices;
16000 }
16001
16002 if (!line_number_displayed && w->base_line_pos != -1)
16003 {
16004 w->base_line_pos = 0;
16005 w->base_line_number = 0;
16006 }
16007
16008 finish_menu_bars:
16009
16010 /* When we reach a frame's selected window, redo the frame's menu bar. */
16011 if (update_mode_line
16012 && EQ (FRAME_SELECTED_WINDOW (f), window))
16013 {
16014 int redisplay_menu_p = 0;
16015
16016 if (FRAME_WINDOW_P (f))
16017 {
16018 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16019 || defined (HAVE_NS) || defined (USE_GTK)
16020 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16021 #else
16022 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16023 #endif
16024 }
16025 else
16026 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16027
16028 if (redisplay_menu_p)
16029 display_menu_bar (w);
16030
16031 #ifdef HAVE_WINDOW_SYSTEM
16032 if (FRAME_WINDOW_P (f))
16033 {
16034 #if defined (USE_GTK) || defined (HAVE_NS)
16035 if (FRAME_EXTERNAL_TOOL_BAR (f))
16036 redisplay_tool_bar (f);
16037 #else
16038 if (WINDOWP (f->tool_bar_window)
16039 && (FRAME_TOOL_BAR_LINES (f) > 0
16040 || !NILP (Vauto_resize_tool_bars))
16041 && redisplay_tool_bar (f))
16042 ignore_mouse_drag_p = 1;
16043 #endif
16044 }
16045 #endif
16046 }
16047
16048 #ifdef HAVE_WINDOW_SYSTEM
16049 if (FRAME_WINDOW_P (f)
16050 && update_window_fringes (w, (just_this_one_p
16051 || (!used_current_matrix_p && !overlay_arrow_seen)
16052 || w->pseudo_window_p)))
16053 {
16054 update_begin (f);
16055 block_input ();
16056 if (draw_window_fringes (w, 1))
16057 x_draw_vertical_border (w);
16058 unblock_input ();
16059 update_end (f);
16060 }
16061 #endif /* HAVE_WINDOW_SYSTEM */
16062
16063 /* We go to this label, with fonts_changed_p set,
16064 if it is necessary to try again using larger glyph matrices.
16065 We have to redeem the scroll bar even in this case,
16066 because the loop in redisplay_internal expects that. */
16067 need_larger_matrices:
16068 ;
16069 finish_scroll_bars:
16070
16071 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16072 {
16073 /* Set the thumb's position and size. */
16074 set_vertical_scroll_bar (w);
16075
16076 /* Note that we actually used the scroll bar attached to this
16077 window, so it shouldn't be deleted at the end of redisplay. */
16078 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16079 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16080 }
16081
16082 /* Restore current_buffer and value of point in it. The window
16083 update may have changed the buffer, so first make sure `opoint'
16084 is still valid (Bug#6177). */
16085 if (CHARPOS (opoint) < BEGV)
16086 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16087 else if (CHARPOS (opoint) > ZV)
16088 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16089 else
16090 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16091
16092 set_buffer_internal_1 (old);
16093 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16094 shorter. This can be caused by log truncation in *Messages*. */
16095 if (CHARPOS (lpoint) <= ZV)
16096 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16097
16098 unbind_to (count, Qnil);
16099 }
16100
16101
16102 /* Build the complete desired matrix of WINDOW with a window start
16103 buffer position POS.
16104
16105 Value is 1 if successful. It is zero if fonts were loaded during
16106 redisplay which makes re-adjusting glyph matrices necessary, and -1
16107 if point would appear in the scroll margins.
16108 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16109 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16110 set in FLAGS.) */
16111
16112 int
16113 try_window (Lisp_Object window, struct text_pos pos, int flags)
16114 {
16115 struct window *w = XWINDOW (window);
16116 struct it it;
16117 struct glyph_row *last_text_row = NULL;
16118 struct frame *f = XFRAME (w->frame);
16119
16120 /* Make POS the new window start. */
16121 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16122
16123 /* Mark cursor position as unknown. No overlay arrow seen. */
16124 w->cursor.vpos = -1;
16125 overlay_arrow_seen = 0;
16126
16127 /* Initialize iterator and info to start at POS. */
16128 start_display (&it, w, pos);
16129
16130 /* Display all lines of W. */
16131 while (it.current_y < it.last_visible_y)
16132 {
16133 if (display_line (&it))
16134 last_text_row = it.glyph_row - 1;
16135 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16136 return 0;
16137 }
16138
16139 /* Don't let the cursor end in the scroll margins. */
16140 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16141 && !MINI_WINDOW_P (w))
16142 {
16143 int this_scroll_margin;
16144
16145 if (scroll_margin > 0)
16146 {
16147 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16148 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16149 }
16150 else
16151 this_scroll_margin = 0;
16152
16153 if ((w->cursor.y >= 0 /* not vscrolled */
16154 && w->cursor.y < this_scroll_margin
16155 && CHARPOS (pos) > BEGV
16156 && IT_CHARPOS (it) < ZV)
16157 /* rms: considering make_cursor_line_fully_visible_p here
16158 seems to give wrong results. We don't want to recenter
16159 when the last line is partly visible, we want to allow
16160 that case to be handled in the usual way. */
16161 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16162 {
16163 w->cursor.vpos = -1;
16164 clear_glyph_matrix (w->desired_matrix);
16165 return -1;
16166 }
16167 }
16168
16169 /* If bottom moved off end of frame, change mode line percentage. */
16170 if (XFASTINT (w->window_end_pos) <= 0
16171 && Z != IT_CHARPOS (it))
16172 w->update_mode_line = 1;
16173
16174 /* Set window_end_pos to the offset of the last character displayed
16175 on the window from the end of current_buffer. Set
16176 window_end_vpos to its row number. */
16177 if (last_text_row)
16178 {
16179 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16180 w->window_end_bytepos
16181 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16182 wset_window_end_pos
16183 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16184 wset_window_end_vpos
16185 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16186 eassert
16187 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16188 XFASTINT (w->window_end_vpos))));
16189 }
16190 else
16191 {
16192 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16193 wset_window_end_pos (w, make_number (Z - ZV));
16194 wset_window_end_vpos (w, make_number (0));
16195 }
16196
16197 /* But that is not valid info until redisplay finishes. */
16198 w->window_end_valid = 0;
16199 return 1;
16200 }
16201
16202
16203 \f
16204 /************************************************************************
16205 Window redisplay reusing current matrix when buffer has not changed
16206 ************************************************************************/
16207
16208 /* Try redisplay of window W showing an unchanged buffer with a
16209 different window start than the last time it was displayed by
16210 reusing its current matrix. Value is non-zero if successful.
16211 W->start is the new window start. */
16212
16213 static int
16214 try_window_reusing_current_matrix (struct window *w)
16215 {
16216 struct frame *f = XFRAME (w->frame);
16217 struct glyph_row *bottom_row;
16218 struct it it;
16219 struct run run;
16220 struct text_pos start, new_start;
16221 int nrows_scrolled, i;
16222 struct glyph_row *last_text_row;
16223 struct glyph_row *last_reused_text_row;
16224 struct glyph_row *start_row;
16225 int start_vpos, min_y, max_y;
16226
16227 #ifdef GLYPH_DEBUG
16228 if (inhibit_try_window_reusing)
16229 return 0;
16230 #endif
16231
16232 if (/* This function doesn't handle terminal frames. */
16233 !FRAME_WINDOW_P (f)
16234 /* Don't try to reuse the display if windows have been split
16235 or such. */
16236 || windows_or_buffers_changed
16237 || cursor_type_changed)
16238 return 0;
16239
16240 /* Can't do this if region may have changed. */
16241 if (markpos_of_region () >= 0
16242 || w->region_showing
16243 || !NILP (Vshow_trailing_whitespace))
16244 return 0;
16245
16246 /* If top-line visibility has changed, give up. */
16247 if (WINDOW_WANTS_HEADER_LINE_P (w)
16248 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16249 return 0;
16250
16251 /* Give up if old or new display is scrolled vertically. We could
16252 make this function handle this, but right now it doesn't. */
16253 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16254 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16255 return 0;
16256
16257 /* The variable new_start now holds the new window start. The old
16258 start `start' can be determined from the current matrix. */
16259 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16260 start = start_row->minpos;
16261 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16262
16263 /* Clear the desired matrix for the display below. */
16264 clear_glyph_matrix (w->desired_matrix);
16265
16266 if (CHARPOS (new_start) <= CHARPOS (start))
16267 {
16268 /* Don't use this method if the display starts with an ellipsis
16269 displayed for invisible text. It's not easy to handle that case
16270 below, and it's certainly not worth the effort since this is
16271 not a frequent case. */
16272 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16273 return 0;
16274
16275 IF_DEBUG (debug_method_add (w, "twu1"));
16276
16277 /* Display up to a row that can be reused. The variable
16278 last_text_row is set to the last row displayed that displays
16279 text. Note that it.vpos == 0 if or if not there is a
16280 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16281 start_display (&it, w, new_start);
16282 w->cursor.vpos = -1;
16283 last_text_row = last_reused_text_row = NULL;
16284
16285 while (it.current_y < it.last_visible_y
16286 && !fonts_changed_p)
16287 {
16288 /* If we have reached into the characters in the START row,
16289 that means the line boundaries have changed. So we
16290 can't start copying with the row START. Maybe it will
16291 work to start copying with the following row. */
16292 while (IT_CHARPOS (it) > CHARPOS (start))
16293 {
16294 /* Advance to the next row as the "start". */
16295 start_row++;
16296 start = start_row->minpos;
16297 /* If there are no more rows to try, or just one, give up. */
16298 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16299 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16300 || CHARPOS (start) == ZV)
16301 {
16302 clear_glyph_matrix (w->desired_matrix);
16303 return 0;
16304 }
16305
16306 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16307 }
16308 /* If we have reached alignment, we can copy the rest of the
16309 rows. */
16310 if (IT_CHARPOS (it) == CHARPOS (start)
16311 /* Don't accept "alignment" inside a display vector,
16312 since start_row could have started in the middle of
16313 that same display vector (thus their character
16314 positions match), and we have no way of telling if
16315 that is the case. */
16316 && it.current.dpvec_index < 0)
16317 break;
16318
16319 if (display_line (&it))
16320 last_text_row = it.glyph_row - 1;
16321
16322 }
16323
16324 /* A value of current_y < last_visible_y means that we stopped
16325 at the previous window start, which in turn means that we
16326 have at least one reusable row. */
16327 if (it.current_y < it.last_visible_y)
16328 {
16329 struct glyph_row *row;
16330
16331 /* IT.vpos always starts from 0; it counts text lines. */
16332 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16333
16334 /* Find PT if not already found in the lines displayed. */
16335 if (w->cursor.vpos < 0)
16336 {
16337 int dy = it.current_y - start_row->y;
16338
16339 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16340 row = row_containing_pos (w, PT, row, NULL, dy);
16341 if (row)
16342 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16343 dy, nrows_scrolled);
16344 else
16345 {
16346 clear_glyph_matrix (w->desired_matrix);
16347 return 0;
16348 }
16349 }
16350
16351 /* Scroll the display. Do it before the current matrix is
16352 changed. The problem here is that update has not yet
16353 run, i.e. part of the current matrix is not up to date.
16354 scroll_run_hook will clear the cursor, and use the
16355 current matrix to get the height of the row the cursor is
16356 in. */
16357 run.current_y = start_row->y;
16358 run.desired_y = it.current_y;
16359 run.height = it.last_visible_y - it.current_y;
16360
16361 if (run.height > 0 && run.current_y != run.desired_y)
16362 {
16363 update_begin (f);
16364 FRAME_RIF (f)->update_window_begin_hook (w);
16365 FRAME_RIF (f)->clear_window_mouse_face (w);
16366 FRAME_RIF (f)->scroll_run_hook (w, &run);
16367 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16368 update_end (f);
16369 }
16370
16371 /* Shift current matrix down by nrows_scrolled lines. */
16372 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16373 rotate_matrix (w->current_matrix,
16374 start_vpos,
16375 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16376 nrows_scrolled);
16377
16378 /* Disable lines that must be updated. */
16379 for (i = 0; i < nrows_scrolled; ++i)
16380 (start_row + i)->enabled_p = 0;
16381
16382 /* Re-compute Y positions. */
16383 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16384 max_y = it.last_visible_y;
16385 for (row = start_row + nrows_scrolled;
16386 row < bottom_row;
16387 ++row)
16388 {
16389 row->y = it.current_y;
16390 row->visible_height = row->height;
16391
16392 if (row->y < min_y)
16393 row->visible_height -= min_y - row->y;
16394 if (row->y + row->height > max_y)
16395 row->visible_height -= row->y + row->height - max_y;
16396 if (row->fringe_bitmap_periodic_p)
16397 row->redraw_fringe_bitmaps_p = 1;
16398
16399 it.current_y += row->height;
16400
16401 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16402 last_reused_text_row = row;
16403 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16404 break;
16405 }
16406
16407 /* Disable lines in the current matrix which are now
16408 below the window. */
16409 for (++row; row < bottom_row; ++row)
16410 row->enabled_p = row->mode_line_p = 0;
16411 }
16412
16413 /* Update window_end_pos etc.; last_reused_text_row is the last
16414 reused row from the current matrix containing text, if any.
16415 The value of last_text_row is the last displayed line
16416 containing text. */
16417 if (last_reused_text_row)
16418 {
16419 w->window_end_bytepos
16420 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16421 wset_window_end_pos
16422 (w, make_number (Z
16423 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16424 wset_window_end_vpos
16425 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16426 w->current_matrix)));
16427 }
16428 else if (last_text_row)
16429 {
16430 w->window_end_bytepos
16431 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16432 wset_window_end_pos
16433 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16434 wset_window_end_vpos
16435 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16436 w->desired_matrix)));
16437 }
16438 else
16439 {
16440 /* This window must be completely empty. */
16441 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16442 wset_window_end_pos (w, make_number (Z - ZV));
16443 wset_window_end_vpos (w, make_number (0));
16444 }
16445 w->window_end_valid = 0;
16446
16447 /* Update hint: don't try scrolling again in update_window. */
16448 w->desired_matrix->no_scrolling_p = 1;
16449
16450 #ifdef GLYPH_DEBUG
16451 debug_method_add (w, "try_window_reusing_current_matrix 1");
16452 #endif
16453 return 1;
16454 }
16455 else if (CHARPOS (new_start) > CHARPOS (start))
16456 {
16457 struct glyph_row *pt_row, *row;
16458 struct glyph_row *first_reusable_row;
16459 struct glyph_row *first_row_to_display;
16460 int dy;
16461 int yb = window_text_bottom_y (w);
16462
16463 /* Find the row starting at new_start, if there is one. Don't
16464 reuse a partially visible line at the end. */
16465 first_reusable_row = start_row;
16466 while (first_reusable_row->enabled_p
16467 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16468 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16469 < CHARPOS (new_start)))
16470 ++first_reusable_row;
16471
16472 /* Give up if there is no row to reuse. */
16473 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16474 || !first_reusable_row->enabled_p
16475 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16476 != CHARPOS (new_start)))
16477 return 0;
16478
16479 /* We can reuse fully visible rows beginning with
16480 first_reusable_row to the end of the window. Set
16481 first_row_to_display to the first row that cannot be reused.
16482 Set pt_row to the row containing point, if there is any. */
16483 pt_row = NULL;
16484 for (first_row_to_display = first_reusable_row;
16485 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16486 ++first_row_to_display)
16487 {
16488 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16489 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16490 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16491 && first_row_to_display->ends_at_zv_p
16492 && pt_row == NULL)))
16493 pt_row = first_row_to_display;
16494 }
16495
16496 /* Start displaying at the start of first_row_to_display. */
16497 eassert (first_row_to_display->y < yb);
16498 init_to_row_start (&it, w, first_row_to_display);
16499
16500 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16501 - start_vpos);
16502 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16503 - nrows_scrolled);
16504 it.current_y = (first_row_to_display->y - first_reusable_row->y
16505 + WINDOW_HEADER_LINE_HEIGHT (w));
16506
16507 /* Display lines beginning with first_row_to_display in the
16508 desired matrix. Set last_text_row to the last row displayed
16509 that displays text. */
16510 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16511 if (pt_row == NULL)
16512 w->cursor.vpos = -1;
16513 last_text_row = NULL;
16514 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16515 if (display_line (&it))
16516 last_text_row = it.glyph_row - 1;
16517
16518 /* If point is in a reused row, adjust y and vpos of the cursor
16519 position. */
16520 if (pt_row)
16521 {
16522 w->cursor.vpos -= nrows_scrolled;
16523 w->cursor.y -= first_reusable_row->y - start_row->y;
16524 }
16525
16526 /* Give up if point isn't in a row displayed or reused. (This
16527 also handles the case where w->cursor.vpos < nrows_scrolled
16528 after the calls to display_line, which can happen with scroll
16529 margins. See bug#1295.) */
16530 if (w->cursor.vpos < 0)
16531 {
16532 clear_glyph_matrix (w->desired_matrix);
16533 return 0;
16534 }
16535
16536 /* Scroll the display. */
16537 run.current_y = first_reusable_row->y;
16538 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16539 run.height = it.last_visible_y - run.current_y;
16540 dy = run.current_y - run.desired_y;
16541
16542 if (run.height)
16543 {
16544 update_begin (f);
16545 FRAME_RIF (f)->update_window_begin_hook (w);
16546 FRAME_RIF (f)->clear_window_mouse_face (w);
16547 FRAME_RIF (f)->scroll_run_hook (w, &run);
16548 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16549 update_end (f);
16550 }
16551
16552 /* Adjust Y positions of reused rows. */
16553 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16554 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16555 max_y = it.last_visible_y;
16556 for (row = first_reusable_row; row < first_row_to_display; ++row)
16557 {
16558 row->y -= dy;
16559 row->visible_height = row->height;
16560 if (row->y < min_y)
16561 row->visible_height -= min_y - row->y;
16562 if (row->y + row->height > max_y)
16563 row->visible_height -= row->y + row->height - max_y;
16564 if (row->fringe_bitmap_periodic_p)
16565 row->redraw_fringe_bitmaps_p = 1;
16566 }
16567
16568 /* Scroll the current matrix. */
16569 eassert (nrows_scrolled > 0);
16570 rotate_matrix (w->current_matrix,
16571 start_vpos,
16572 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16573 -nrows_scrolled);
16574
16575 /* Disable rows not reused. */
16576 for (row -= nrows_scrolled; row < bottom_row; ++row)
16577 row->enabled_p = 0;
16578
16579 /* Point may have moved to a different line, so we cannot assume that
16580 the previous cursor position is valid; locate the correct row. */
16581 if (pt_row)
16582 {
16583 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16584 row < bottom_row
16585 && PT >= MATRIX_ROW_END_CHARPOS (row)
16586 && !row->ends_at_zv_p;
16587 row++)
16588 {
16589 w->cursor.vpos++;
16590 w->cursor.y = row->y;
16591 }
16592 if (row < bottom_row)
16593 {
16594 /* Can't simply scan the row for point with
16595 bidi-reordered glyph rows. Let set_cursor_from_row
16596 figure out where to put the cursor, and if it fails,
16597 give up. */
16598 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16599 {
16600 if (!set_cursor_from_row (w, row, w->current_matrix,
16601 0, 0, 0, 0))
16602 {
16603 clear_glyph_matrix (w->desired_matrix);
16604 return 0;
16605 }
16606 }
16607 else
16608 {
16609 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16610 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16611
16612 for (; glyph < end
16613 && (!BUFFERP (glyph->object)
16614 || glyph->charpos < PT);
16615 glyph++)
16616 {
16617 w->cursor.hpos++;
16618 w->cursor.x += glyph->pixel_width;
16619 }
16620 }
16621 }
16622 }
16623
16624 /* Adjust window end. A null value of last_text_row means that
16625 the window end is in reused rows which in turn means that
16626 only its vpos can have changed. */
16627 if (last_text_row)
16628 {
16629 w->window_end_bytepos
16630 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16631 wset_window_end_pos
16632 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16633 wset_window_end_vpos
16634 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16635 w->desired_matrix)));
16636 }
16637 else
16638 {
16639 wset_window_end_vpos
16640 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16641 }
16642
16643 w->window_end_valid = 0;
16644 w->desired_matrix->no_scrolling_p = 1;
16645
16646 #ifdef GLYPH_DEBUG
16647 debug_method_add (w, "try_window_reusing_current_matrix 2");
16648 #endif
16649 return 1;
16650 }
16651
16652 return 0;
16653 }
16654
16655
16656 \f
16657 /************************************************************************
16658 Window redisplay reusing current matrix when buffer has changed
16659 ************************************************************************/
16660
16661 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16662 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16663 ptrdiff_t *, ptrdiff_t *);
16664 static struct glyph_row *
16665 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16666 struct glyph_row *);
16667
16668
16669 /* Return the last row in MATRIX displaying text. If row START is
16670 non-null, start searching with that row. IT gives the dimensions
16671 of the display. Value is null if matrix is empty; otherwise it is
16672 a pointer to the row found. */
16673
16674 static struct glyph_row *
16675 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16676 struct glyph_row *start)
16677 {
16678 struct glyph_row *row, *row_found;
16679
16680 /* Set row_found to the last row in IT->w's current matrix
16681 displaying text. The loop looks funny but think of partially
16682 visible lines. */
16683 row_found = NULL;
16684 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16685 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16686 {
16687 eassert (row->enabled_p);
16688 row_found = row;
16689 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16690 break;
16691 ++row;
16692 }
16693
16694 return row_found;
16695 }
16696
16697
16698 /* Return the last row in the current matrix of W that is not affected
16699 by changes at the start of current_buffer that occurred since W's
16700 current matrix was built. Value is null if no such row exists.
16701
16702 BEG_UNCHANGED us the number of characters unchanged at the start of
16703 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16704 first changed character in current_buffer. Characters at positions <
16705 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16706 when the current matrix was built. */
16707
16708 static struct glyph_row *
16709 find_last_unchanged_at_beg_row (struct window *w)
16710 {
16711 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16712 struct glyph_row *row;
16713 struct glyph_row *row_found = NULL;
16714 int yb = window_text_bottom_y (w);
16715
16716 /* Find the last row displaying unchanged text. */
16717 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16718 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16719 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16720 ++row)
16721 {
16722 if (/* If row ends before first_changed_pos, it is unchanged,
16723 except in some case. */
16724 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16725 /* When row ends in ZV and we write at ZV it is not
16726 unchanged. */
16727 && !row->ends_at_zv_p
16728 /* When first_changed_pos is the end of a continued line,
16729 row is not unchanged because it may be no longer
16730 continued. */
16731 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16732 && (row->continued_p
16733 || row->exact_window_width_line_p))
16734 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16735 needs to be recomputed, so don't consider this row as
16736 unchanged. This happens when the last line was
16737 bidi-reordered and was killed immediately before this
16738 redisplay cycle. In that case, ROW->end stores the
16739 buffer position of the first visual-order character of
16740 the killed text, which is now beyond ZV. */
16741 && CHARPOS (row->end.pos) <= ZV)
16742 row_found = row;
16743
16744 /* Stop if last visible row. */
16745 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16746 break;
16747 }
16748
16749 return row_found;
16750 }
16751
16752
16753 /* Find the first glyph row in the current matrix of W that is not
16754 affected by changes at the end of current_buffer since the
16755 time W's current matrix was built.
16756
16757 Return in *DELTA the number of chars by which buffer positions in
16758 unchanged text at the end of current_buffer must be adjusted.
16759
16760 Return in *DELTA_BYTES the corresponding number of bytes.
16761
16762 Value is null if no such row exists, i.e. all rows are affected by
16763 changes. */
16764
16765 static struct glyph_row *
16766 find_first_unchanged_at_end_row (struct window *w,
16767 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16768 {
16769 struct glyph_row *row;
16770 struct glyph_row *row_found = NULL;
16771
16772 *delta = *delta_bytes = 0;
16773
16774 /* Display must not have been paused, otherwise the current matrix
16775 is not up to date. */
16776 eassert (w->window_end_valid);
16777
16778 /* A value of window_end_pos >= END_UNCHANGED means that the window
16779 end is in the range of changed text. If so, there is no
16780 unchanged row at the end of W's current matrix. */
16781 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16782 return NULL;
16783
16784 /* Set row to the last row in W's current matrix displaying text. */
16785 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16786
16787 /* If matrix is entirely empty, no unchanged row exists. */
16788 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16789 {
16790 /* The value of row is the last glyph row in the matrix having a
16791 meaningful buffer position in it. The end position of row
16792 corresponds to window_end_pos. This allows us to translate
16793 buffer positions in the current matrix to current buffer
16794 positions for characters not in changed text. */
16795 ptrdiff_t Z_old =
16796 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16797 ptrdiff_t Z_BYTE_old =
16798 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16799 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16800 struct glyph_row *first_text_row
16801 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16802
16803 *delta = Z - Z_old;
16804 *delta_bytes = Z_BYTE - Z_BYTE_old;
16805
16806 /* Set last_unchanged_pos to the buffer position of the last
16807 character in the buffer that has not been changed. Z is the
16808 index + 1 of the last character in current_buffer, i.e. by
16809 subtracting END_UNCHANGED we get the index of the last
16810 unchanged character, and we have to add BEG to get its buffer
16811 position. */
16812 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16813 last_unchanged_pos_old = last_unchanged_pos - *delta;
16814
16815 /* Search backward from ROW for a row displaying a line that
16816 starts at a minimum position >= last_unchanged_pos_old. */
16817 for (; row > first_text_row; --row)
16818 {
16819 /* This used to abort, but it can happen.
16820 It is ok to just stop the search instead here. KFS. */
16821 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16822 break;
16823
16824 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16825 row_found = row;
16826 }
16827 }
16828
16829 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16830
16831 return row_found;
16832 }
16833
16834
16835 /* Make sure that glyph rows in the current matrix of window W
16836 reference the same glyph memory as corresponding rows in the
16837 frame's frame matrix. This function is called after scrolling W's
16838 current matrix on a terminal frame in try_window_id and
16839 try_window_reusing_current_matrix. */
16840
16841 static void
16842 sync_frame_with_window_matrix_rows (struct window *w)
16843 {
16844 struct frame *f = XFRAME (w->frame);
16845 struct glyph_row *window_row, *window_row_end, *frame_row;
16846
16847 /* Preconditions: W must be a leaf window and full-width. Its frame
16848 must have a frame matrix. */
16849 eassert (BUFFERP (w->contents));
16850 eassert (WINDOW_FULL_WIDTH_P (w));
16851 eassert (!FRAME_WINDOW_P (f));
16852
16853 /* If W is a full-width window, glyph pointers in W's current matrix
16854 have, by definition, to be the same as glyph pointers in the
16855 corresponding frame matrix. Note that frame matrices have no
16856 marginal areas (see build_frame_matrix). */
16857 window_row = w->current_matrix->rows;
16858 window_row_end = window_row + w->current_matrix->nrows;
16859 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16860 while (window_row < window_row_end)
16861 {
16862 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16863 struct glyph *end = window_row->glyphs[LAST_AREA];
16864
16865 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16866 frame_row->glyphs[TEXT_AREA] = start;
16867 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16868 frame_row->glyphs[LAST_AREA] = end;
16869
16870 /* Disable frame rows whose corresponding window rows have
16871 been disabled in try_window_id. */
16872 if (!window_row->enabled_p)
16873 frame_row->enabled_p = 0;
16874
16875 ++window_row, ++frame_row;
16876 }
16877 }
16878
16879
16880 /* Find the glyph row in window W containing CHARPOS. Consider all
16881 rows between START and END (not inclusive). END null means search
16882 all rows to the end of the display area of W. Value is the row
16883 containing CHARPOS or null. */
16884
16885 struct glyph_row *
16886 row_containing_pos (struct window *w, ptrdiff_t charpos,
16887 struct glyph_row *start, struct glyph_row *end, int dy)
16888 {
16889 struct glyph_row *row = start;
16890 struct glyph_row *best_row = NULL;
16891 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16892 int last_y;
16893
16894 /* If we happen to start on a header-line, skip that. */
16895 if (row->mode_line_p)
16896 ++row;
16897
16898 if ((end && row >= end) || !row->enabled_p)
16899 return NULL;
16900
16901 last_y = window_text_bottom_y (w) - dy;
16902
16903 while (1)
16904 {
16905 /* Give up if we have gone too far. */
16906 if (end && row >= end)
16907 return NULL;
16908 /* This formerly returned if they were equal.
16909 I think that both quantities are of a "last plus one" type;
16910 if so, when they are equal, the row is within the screen. -- rms. */
16911 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16912 return NULL;
16913
16914 /* If it is in this row, return this row. */
16915 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16916 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16917 /* The end position of a row equals the start
16918 position of the next row. If CHARPOS is there, we
16919 would rather consider it displayed in the next
16920 line, except when this line ends in ZV. */
16921 && !row_for_charpos_p (row, charpos)))
16922 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16923 {
16924 struct glyph *g;
16925
16926 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16927 || (!best_row && !row->continued_p))
16928 return row;
16929 /* In bidi-reordered rows, there could be several rows whose
16930 edges surround CHARPOS, all of these rows belonging to
16931 the same continued line. We need to find the row which
16932 fits CHARPOS the best. */
16933 for (g = row->glyphs[TEXT_AREA];
16934 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16935 g++)
16936 {
16937 if (!STRINGP (g->object))
16938 {
16939 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16940 {
16941 mindif = eabs (g->charpos - charpos);
16942 best_row = row;
16943 /* Exact match always wins. */
16944 if (mindif == 0)
16945 return best_row;
16946 }
16947 }
16948 }
16949 }
16950 else if (best_row && !row->continued_p)
16951 return best_row;
16952 ++row;
16953 }
16954 }
16955
16956
16957 /* Try to redisplay window W by reusing its existing display. W's
16958 current matrix must be up to date when this function is called,
16959 i.e. window_end_valid must be nonzero.
16960
16961 Value is
16962
16963 1 if display has been updated
16964 0 if otherwise unsuccessful
16965 -1 if redisplay with same window start is known not to succeed
16966
16967 The following steps are performed:
16968
16969 1. Find the last row in the current matrix of W that is not
16970 affected by changes at the start of current_buffer. If no such row
16971 is found, give up.
16972
16973 2. Find the first row in W's current matrix that is not affected by
16974 changes at the end of current_buffer. Maybe there is no such row.
16975
16976 3. Display lines beginning with the row + 1 found in step 1 to the
16977 row found in step 2 or, if step 2 didn't find a row, to the end of
16978 the window.
16979
16980 4. If cursor is not known to appear on the window, give up.
16981
16982 5. If display stopped at the row found in step 2, scroll the
16983 display and current matrix as needed.
16984
16985 6. Maybe display some lines at the end of W, if we must. This can
16986 happen under various circumstances, like a partially visible line
16987 becoming fully visible, or because newly displayed lines are displayed
16988 in smaller font sizes.
16989
16990 7. Update W's window end information. */
16991
16992 static int
16993 try_window_id (struct window *w)
16994 {
16995 struct frame *f = XFRAME (w->frame);
16996 struct glyph_matrix *current_matrix = w->current_matrix;
16997 struct glyph_matrix *desired_matrix = w->desired_matrix;
16998 struct glyph_row *last_unchanged_at_beg_row;
16999 struct glyph_row *first_unchanged_at_end_row;
17000 struct glyph_row *row;
17001 struct glyph_row *bottom_row;
17002 int bottom_vpos;
17003 struct it it;
17004 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17005 int dvpos, dy;
17006 struct text_pos start_pos;
17007 struct run run;
17008 int first_unchanged_at_end_vpos = 0;
17009 struct glyph_row *last_text_row, *last_text_row_at_end;
17010 struct text_pos start;
17011 ptrdiff_t first_changed_charpos, last_changed_charpos;
17012
17013 #ifdef GLYPH_DEBUG
17014 if (inhibit_try_window_id)
17015 return 0;
17016 #endif
17017
17018 /* This is handy for debugging. */
17019 #if 0
17020 #define GIVE_UP(X) \
17021 do { \
17022 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17023 return 0; \
17024 } while (0)
17025 #else
17026 #define GIVE_UP(X) return 0
17027 #endif
17028
17029 SET_TEXT_POS_FROM_MARKER (start, w->start);
17030
17031 /* Don't use this for mini-windows because these can show
17032 messages and mini-buffers, and we don't handle that here. */
17033 if (MINI_WINDOW_P (w))
17034 GIVE_UP (1);
17035
17036 /* This flag is used to prevent redisplay optimizations. */
17037 if (windows_or_buffers_changed || cursor_type_changed)
17038 GIVE_UP (2);
17039
17040 /* Verify that narrowing has not changed.
17041 Also verify that we were not told to prevent redisplay optimizations.
17042 It would be nice to further
17043 reduce the number of cases where this prevents try_window_id. */
17044 if (current_buffer->clip_changed
17045 || current_buffer->prevent_redisplay_optimizations_p)
17046 GIVE_UP (3);
17047
17048 /* Window must either use window-based redisplay or be full width. */
17049 if (!FRAME_WINDOW_P (f)
17050 && (!FRAME_LINE_INS_DEL_OK (f)
17051 || !WINDOW_FULL_WIDTH_P (w)))
17052 GIVE_UP (4);
17053
17054 /* Give up if point is known NOT to appear in W. */
17055 if (PT < CHARPOS (start))
17056 GIVE_UP (5);
17057
17058 /* Another way to prevent redisplay optimizations. */
17059 if (w->last_modified == 0)
17060 GIVE_UP (6);
17061
17062 /* Verify that window is not hscrolled. */
17063 if (w->hscroll != 0)
17064 GIVE_UP (7);
17065
17066 /* Verify that display wasn't paused. */
17067 if (!w->window_end_valid)
17068 GIVE_UP (8);
17069
17070 /* Can't use this if highlighting a region because a cursor movement
17071 will do more than just set the cursor. */
17072 if (markpos_of_region () >= 0)
17073 GIVE_UP (9);
17074
17075 /* Likewise if highlighting trailing whitespace. */
17076 if (!NILP (Vshow_trailing_whitespace))
17077 GIVE_UP (11);
17078
17079 /* Likewise if showing a region. */
17080 if (w->region_showing)
17081 GIVE_UP (10);
17082
17083 /* Can't use this if overlay arrow position and/or string have
17084 changed. */
17085 if (overlay_arrows_changed_p ())
17086 GIVE_UP (12);
17087
17088 /* When word-wrap is on, adding a space to the first word of a
17089 wrapped line can change the wrap position, altering the line
17090 above it. It might be worthwhile to handle this more
17091 intelligently, but for now just redisplay from scratch. */
17092 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17093 GIVE_UP (21);
17094
17095 /* Under bidi reordering, adding or deleting a character in the
17096 beginning of a paragraph, before the first strong directional
17097 character, can change the base direction of the paragraph (unless
17098 the buffer specifies a fixed paragraph direction), which will
17099 require to redisplay the whole paragraph. It might be worthwhile
17100 to find the paragraph limits and widen the range of redisplayed
17101 lines to that, but for now just give up this optimization and
17102 redisplay from scratch. */
17103 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17104 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17105 GIVE_UP (22);
17106
17107 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17108 only if buffer has really changed. The reason is that the gap is
17109 initially at Z for freshly visited files. The code below would
17110 set end_unchanged to 0 in that case. */
17111 if (MODIFF > SAVE_MODIFF
17112 /* This seems to happen sometimes after saving a buffer. */
17113 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17114 {
17115 if (GPT - BEG < BEG_UNCHANGED)
17116 BEG_UNCHANGED = GPT - BEG;
17117 if (Z - GPT < END_UNCHANGED)
17118 END_UNCHANGED = Z - GPT;
17119 }
17120
17121 /* The position of the first and last character that has been changed. */
17122 first_changed_charpos = BEG + BEG_UNCHANGED;
17123 last_changed_charpos = Z - END_UNCHANGED;
17124
17125 /* If window starts after a line end, and the last change is in
17126 front of that newline, then changes don't affect the display.
17127 This case happens with stealth-fontification. Note that although
17128 the display is unchanged, glyph positions in the matrix have to
17129 be adjusted, of course. */
17130 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17131 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17132 && ((last_changed_charpos < CHARPOS (start)
17133 && CHARPOS (start) == BEGV)
17134 || (last_changed_charpos < CHARPOS (start) - 1
17135 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17136 {
17137 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17138 struct glyph_row *r0;
17139
17140 /* Compute how many chars/bytes have been added to or removed
17141 from the buffer. */
17142 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17143 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17144 Z_delta = Z - Z_old;
17145 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17146
17147 /* Give up if PT is not in the window. Note that it already has
17148 been checked at the start of try_window_id that PT is not in
17149 front of the window start. */
17150 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17151 GIVE_UP (13);
17152
17153 /* If window start is unchanged, we can reuse the whole matrix
17154 as is, after adjusting glyph positions. No need to compute
17155 the window end again, since its offset from Z hasn't changed. */
17156 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17157 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17158 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17159 /* PT must not be in a partially visible line. */
17160 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17161 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17162 {
17163 /* Adjust positions in the glyph matrix. */
17164 if (Z_delta || Z_delta_bytes)
17165 {
17166 struct glyph_row *r1
17167 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17168 increment_matrix_positions (w->current_matrix,
17169 MATRIX_ROW_VPOS (r0, current_matrix),
17170 MATRIX_ROW_VPOS (r1, current_matrix),
17171 Z_delta, Z_delta_bytes);
17172 }
17173
17174 /* Set the cursor. */
17175 row = row_containing_pos (w, PT, r0, NULL, 0);
17176 if (row)
17177 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17178 else
17179 emacs_abort ();
17180 return 1;
17181 }
17182 }
17183
17184 /* Handle the case that changes are all below what is displayed in
17185 the window, and that PT is in the window. This shortcut cannot
17186 be taken if ZV is visible in the window, and text has been added
17187 there that is visible in the window. */
17188 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17189 /* ZV is not visible in the window, or there are no
17190 changes at ZV, actually. */
17191 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17192 || first_changed_charpos == last_changed_charpos))
17193 {
17194 struct glyph_row *r0;
17195
17196 /* Give up if PT is not in the window. Note that it already has
17197 been checked at the start of try_window_id that PT is not in
17198 front of the window start. */
17199 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17200 GIVE_UP (14);
17201
17202 /* If window start is unchanged, we can reuse the whole matrix
17203 as is, without changing glyph positions since no text has
17204 been added/removed in front of the window end. */
17205 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17206 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17207 /* PT must not be in a partially visible line. */
17208 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17209 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17210 {
17211 /* We have to compute the window end anew since text
17212 could have been added/removed after it. */
17213 wset_window_end_pos
17214 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17215 w->window_end_bytepos
17216 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17217
17218 /* Set the cursor. */
17219 row = row_containing_pos (w, PT, r0, NULL, 0);
17220 if (row)
17221 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17222 else
17223 emacs_abort ();
17224 return 2;
17225 }
17226 }
17227
17228 /* Give up if window start is in the changed area.
17229
17230 The condition used to read
17231
17232 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17233
17234 but why that was tested escapes me at the moment. */
17235 if (CHARPOS (start) >= first_changed_charpos
17236 && CHARPOS (start) <= last_changed_charpos)
17237 GIVE_UP (15);
17238
17239 /* Check that window start agrees with the start of the first glyph
17240 row in its current matrix. Check this after we know the window
17241 start is not in changed text, otherwise positions would not be
17242 comparable. */
17243 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17244 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17245 GIVE_UP (16);
17246
17247 /* Give up if the window ends in strings. Overlay strings
17248 at the end are difficult to handle, so don't try. */
17249 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17250 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17251 GIVE_UP (20);
17252
17253 /* Compute the position at which we have to start displaying new
17254 lines. Some of the lines at the top of the window might be
17255 reusable because they are not displaying changed text. Find the
17256 last row in W's current matrix not affected by changes at the
17257 start of current_buffer. Value is null if changes start in the
17258 first line of window. */
17259 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17260 if (last_unchanged_at_beg_row)
17261 {
17262 /* Avoid starting to display in the middle of a character, a TAB
17263 for instance. This is easier than to set up the iterator
17264 exactly, and it's not a frequent case, so the additional
17265 effort wouldn't really pay off. */
17266 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17267 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17268 && last_unchanged_at_beg_row > w->current_matrix->rows)
17269 --last_unchanged_at_beg_row;
17270
17271 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17272 GIVE_UP (17);
17273
17274 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17275 GIVE_UP (18);
17276 start_pos = it.current.pos;
17277
17278 /* Start displaying new lines in the desired matrix at the same
17279 vpos we would use in the current matrix, i.e. below
17280 last_unchanged_at_beg_row. */
17281 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17282 current_matrix);
17283 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17284 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17285
17286 eassert (it.hpos == 0 && it.current_x == 0);
17287 }
17288 else
17289 {
17290 /* There are no reusable lines at the start of the window.
17291 Start displaying in the first text line. */
17292 start_display (&it, w, start);
17293 it.vpos = it.first_vpos;
17294 start_pos = it.current.pos;
17295 }
17296
17297 /* Find the first row that is not affected by changes at the end of
17298 the buffer. Value will be null if there is no unchanged row, in
17299 which case we must redisplay to the end of the window. delta
17300 will be set to the value by which buffer positions beginning with
17301 first_unchanged_at_end_row have to be adjusted due to text
17302 changes. */
17303 first_unchanged_at_end_row
17304 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17305 IF_DEBUG (debug_delta = delta);
17306 IF_DEBUG (debug_delta_bytes = delta_bytes);
17307
17308 /* Set stop_pos to the buffer position up to which we will have to
17309 display new lines. If first_unchanged_at_end_row != NULL, this
17310 is the buffer position of the start of the line displayed in that
17311 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17312 that we don't stop at a buffer position. */
17313 stop_pos = 0;
17314 if (first_unchanged_at_end_row)
17315 {
17316 eassert (last_unchanged_at_beg_row == NULL
17317 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17318
17319 /* If this is a continuation line, move forward to the next one
17320 that isn't. Changes in lines above affect this line.
17321 Caution: this may move first_unchanged_at_end_row to a row
17322 not displaying text. */
17323 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17324 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17325 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17326 < it.last_visible_y))
17327 ++first_unchanged_at_end_row;
17328
17329 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17330 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17331 >= it.last_visible_y))
17332 first_unchanged_at_end_row = NULL;
17333 else
17334 {
17335 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17336 + delta);
17337 first_unchanged_at_end_vpos
17338 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17339 eassert (stop_pos >= Z - END_UNCHANGED);
17340 }
17341 }
17342 else if (last_unchanged_at_beg_row == NULL)
17343 GIVE_UP (19);
17344
17345
17346 #ifdef GLYPH_DEBUG
17347
17348 /* Either there is no unchanged row at the end, or the one we have
17349 now displays text. This is a necessary condition for the window
17350 end pos calculation at the end of this function. */
17351 eassert (first_unchanged_at_end_row == NULL
17352 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17353
17354 debug_last_unchanged_at_beg_vpos
17355 = (last_unchanged_at_beg_row
17356 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17357 : -1);
17358 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17359
17360 #endif /* GLYPH_DEBUG */
17361
17362
17363 /* Display new lines. Set last_text_row to the last new line
17364 displayed which has text on it, i.e. might end up as being the
17365 line where the window_end_vpos is. */
17366 w->cursor.vpos = -1;
17367 last_text_row = NULL;
17368 overlay_arrow_seen = 0;
17369 while (it.current_y < it.last_visible_y
17370 && !fonts_changed_p
17371 && (first_unchanged_at_end_row == NULL
17372 || IT_CHARPOS (it) < stop_pos))
17373 {
17374 if (display_line (&it))
17375 last_text_row = it.glyph_row - 1;
17376 }
17377
17378 if (fonts_changed_p)
17379 return -1;
17380
17381
17382 /* Compute differences in buffer positions, y-positions etc. for
17383 lines reused at the bottom of the window. Compute what we can
17384 scroll. */
17385 if (first_unchanged_at_end_row
17386 /* No lines reused because we displayed everything up to the
17387 bottom of the window. */
17388 && it.current_y < it.last_visible_y)
17389 {
17390 dvpos = (it.vpos
17391 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17392 current_matrix));
17393 dy = it.current_y - first_unchanged_at_end_row->y;
17394 run.current_y = first_unchanged_at_end_row->y;
17395 run.desired_y = run.current_y + dy;
17396 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17397 }
17398 else
17399 {
17400 delta = delta_bytes = dvpos = dy
17401 = run.current_y = run.desired_y = run.height = 0;
17402 first_unchanged_at_end_row = NULL;
17403 }
17404 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17405
17406
17407 /* Find the cursor if not already found. We have to decide whether
17408 PT will appear on this window (it sometimes doesn't, but this is
17409 not a very frequent case.) This decision has to be made before
17410 the current matrix is altered. A value of cursor.vpos < 0 means
17411 that PT is either in one of the lines beginning at
17412 first_unchanged_at_end_row or below the window. Don't care for
17413 lines that might be displayed later at the window end; as
17414 mentioned, this is not a frequent case. */
17415 if (w->cursor.vpos < 0)
17416 {
17417 /* Cursor in unchanged rows at the top? */
17418 if (PT < CHARPOS (start_pos)
17419 && last_unchanged_at_beg_row)
17420 {
17421 row = row_containing_pos (w, PT,
17422 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17423 last_unchanged_at_beg_row + 1, 0);
17424 if (row)
17425 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17426 }
17427
17428 /* Start from first_unchanged_at_end_row looking for PT. */
17429 else if (first_unchanged_at_end_row)
17430 {
17431 row = row_containing_pos (w, PT - delta,
17432 first_unchanged_at_end_row, NULL, 0);
17433 if (row)
17434 set_cursor_from_row (w, row, w->current_matrix, delta,
17435 delta_bytes, dy, dvpos);
17436 }
17437
17438 /* Give up if cursor was not found. */
17439 if (w->cursor.vpos < 0)
17440 {
17441 clear_glyph_matrix (w->desired_matrix);
17442 return -1;
17443 }
17444 }
17445
17446 /* Don't let the cursor end in the scroll margins. */
17447 {
17448 int this_scroll_margin, cursor_height;
17449
17450 this_scroll_margin =
17451 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17452 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17453 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17454
17455 if ((w->cursor.y < this_scroll_margin
17456 && CHARPOS (start) > BEGV)
17457 /* Old redisplay didn't take scroll margin into account at the bottom,
17458 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17459 || (w->cursor.y + (make_cursor_line_fully_visible_p
17460 ? cursor_height + this_scroll_margin
17461 : 1)) > it.last_visible_y)
17462 {
17463 w->cursor.vpos = -1;
17464 clear_glyph_matrix (w->desired_matrix);
17465 return -1;
17466 }
17467 }
17468
17469 /* Scroll the display. Do it before changing the current matrix so
17470 that xterm.c doesn't get confused about where the cursor glyph is
17471 found. */
17472 if (dy && run.height)
17473 {
17474 update_begin (f);
17475
17476 if (FRAME_WINDOW_P (f))
17477 {
17478 FRAME_RIF (f)->update_window_begin_hook (w);
17479 FRAME_RIF (f)->clear_window_mouse_face (w);
17480 FRAME_RIF (f)->scroll_run_hook (w, &run);
17481 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17482 }
17483 else
17484 {
17485 /* Terminal frame. In this case, dvpos gives the number of
17486 lines to scroll by; dvpos < 0 means scroll up. */
17487 int from_vpos
17488 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17489 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17490 int end = (WINDOW_TOP_EDGE_LINE (w)
17491 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17492 + window_internal_height (w));
17493
17494 #if defined (HAVE_GPM) || defined (MSDOS)
17495 x_clear_window_mouse_face (w);
17496 #endif
17497 /* Perform the operation on the screen. */
17498 if (dvpos > 0)
17499 {
17500 /* Scroll last_unchanged_at_beg_row to the end of the
17501 window down dvpos lines. */
17502 set_terminal_window (f, end);
17503
17504 /* On dumb terminals delete dvpos lines at the end
17505 before inserting dvpos empty lines. */
17506 if (!FRAME_SCROLL_REGION_OK (f))
17507 ins_del_lines (f, end - dvpos, -dvpos);
17508
17509 /* Insert dvpos empty lines in front of
17510 last_unchanged_at_beg_row. */
17511 ins_del_lines (f, from, dvpos);
17512 }
17513 else if (dvpos < 0)
17514 {
17515 /* Scroll up last_unchanged_at_beg_vpos to the end of
17516 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17517 set_terminal_window (f, end);
17518
17519 /* Delete dvpos lines in front of
17520 last_unchanged_at_beg_vpos. ins_del_lines will set
17521 the cursor to the given vpos and emit |dvpos| delete
17522 line sequences. */
17523 ins_del_lines (f, from + dvpos, dvpos);
17524
17525 /* On a dumb terminal insert dvpos empty lines at the
17526 end. */
17527 if (!FRAME_SCROLL_REGION_OK (f))
17528 ins_del_lines (f, end + dvpos, -dvpos);
17529 }
17530
17531 set_terminal_window (f, 0);
17532 }
17533
17534 update_end (f);
17535 }
17536
17537 /* Shift reused rows of the current matrix to the right position.
17538 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17539 text. */
17540 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17541 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17542 if (dvpos < 0)
17543 {
17544 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17545 bottom_vpos, dvpos);
17546 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17547 bottom_vpos);
17548 }
17549 else if (dvpos > 0)
17550 {
17551 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17552 bottom_vpos, dvpos);
17553 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17554 first_unchanged_at_end_vpos + dvpos);
17555 }
17556
17557 /* For frame-based redisplay, make sure that current frame and window
17558 matrix are in sync with respect to glyph memory. */
17559 if (!FRAME_WINDOW_P (f))
17560 sync_frame_with_window_matrix_rows (w);
17561
17562 /* Adjust buffer positions in reused rows. */
17563 if (delta || delta_bytes)
17564 increment_matrix_positions (current_matrix,
17565 first_unchanged_at_end_vpos + dvpos,
17566 bottom_vpos, delta, delta_bytes);
17567
17568 /* Adjust Y positions. */
17569 if (dy)
17570 shift_glyph_matrix (w, current_matrix,
17571 first_unchanged_at_end_vpos + dvpos,
17572 bottom_vpos, dy);
17573
17574 if (first_unchanged_at_end_row)
17575 {
17576 first_unchanged_at_end_row += dvpos;
17577 if (first_unchanged_at_end_row->y >= it.last_visible_y
17578 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17579 first_unchanged_at_end_row = NULL;
17580 }
17581
17582 /* If scrolling up, there may be some lines to display at the end of
17583 the window. */
17584 last_text_row_at_end = NULL;
17585 if (dy < 0)
17586 {
17587 /* Scrolling up can leave for example a partially visible line
17588 at the end of the window to be redisplayed. */
17589 /* Set last_row to the glyph row in the current matrix where the
17590 window end line is found. It has been moved up or down in
17591 the matrix by dvpos. */
17592 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17593 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17594
17595 /* If last_row is the window end line, it should display text. */
17596 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17597
17598 /* If window end line was partially visible before, begin
17599 displaying at that line. Otherwise begin displaying with the
17600 line following it. */
17601 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17602 {
17603 init_to_row_start (&it, w, last_row);
17604 it.vpos = last_vpos;
17605 it.current_y = last_row->y;
17606 }
17607 else
17608 {
17609 init_to_row_end (&it, w, last_row);
17610 it.vpos = 1 + last_vpos;
17611 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17612 ++last_row;
17613 }
17614
17615 /* We may start in a continuation line. If so, we have to
17616 get the right continuation_lines_width and current_x. */
17617 it.continuation_lines_width = last_row->continuation_lines_width;
17618 it.hpos = it.current_x = 0;
17619
17620 /* Display the rest of the lines at the window end. */
17621 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17622 while (it.current_y < it.last_visible_y
17623 && !fonts_changed_p)
17624 {
17625 /* Is it always sure that the display agrees with lines in
17626 the current matrix? I don't think so, so we mark rows
17627 displayed invalid in the current matrix by setting their
17628 enabled_p flag to zero. */
17629 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17630 if (display_line (&it))
17631 last_text_row_at_end = it.glyph_row - 1;
17632 }
17633 }
17634
17635 /* Update window_end_pos and window_end_vpos. */
17636 if (first_unchanged_at_end_row
17637 && !last_text_row_at_end)
17638 {
17639 /* Window end line if one of the preserved rows from the current
17640 matrix. Set row to the last row displaying text in current
17641 matrix starting at first_unchanged_at_end_row, after
17642 scrolling. */
17643 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17644 row = find_last_row_displaying_text (w->current_matrix, &it,
17645 first_unchanged_at_end_row);
17646 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17647
17648 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17649 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17650 wset_window_end_vpos
17651 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17652 eassert (w->window_end_bytepos >= 0);
17653 IF_DEBUG (debug_method_add (w, "A"));
17654 }
17655 else if (last_text_row_at_end)
17656 {
17657 wset_window_end_pos
17658 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17659 w->window_end_bytepos
17660 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17661 wset_window_end_vpos
17662 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17663 desired_matrix)));
17664 eassert (w->window_end_bytepos >= 0);
17665 IF_DEBUG (debug_method_add (w, "B"));
17666 }
17667 else if (last_text_row)
17668 {
17669 /* We have displayed either to the end of the window or at the
17670 end of the window, i.e. the last row with text is to be found
17671 in the desired matrix. */
17672 wset_window_end_pos
17673 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17674 w->window_end_bytepos
17675 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17676 wset_window_end_vpos
17677 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17678 eassert (w->window_end_bytepos >= 0);
17679 }
17680 else if (first_unchanged_at_end_row == NULL
17681 && last_text_row == NULL
17682 && last_text_row_at_end == NULL)
17683 {
17684 /* Displayed to end of window, but no line containing text was
17685 displayed. Lines were deleted at the end of the window. */
17686 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17687 int vpos = XFASTINT (w->window_end_vpos);
17688 struct glyph_row *current_row = current_matrix->rows + vpos;
17689 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17690
17691 for (row = NULL;
17692 row == NULL && vpos >= first_vpos;
17693 --vpos, --current_row, --desired_row)
17694 {
17695 if (desired_row->enabled_p)
17696 {
17697 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17698 row = desired_row;
17699 }
17700 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17701 row = current_row;
17702 }
17703
17704 eassert (row != NULL);
17705 wset_window_end_vpos (w, make_number (vpos + 1));
17706 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17707 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17708 eassert (w->window_end_bytepos >= 0);
17709 IF_DEBUG (debug_method_add (w, "C"));
17710 }
17711 else
17712 emacs_abort ();
17713
17714 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17715 debug_end_vpos = XFASTINT (w->window_end_vpos));
17716
17717 /* Record that display has not been completed. */
17718 w->window_end_valid = 0;
17719 w->desired_matrix->no_scrolling_p = 1;
17720 return 3;
17721
17722 #undef GIVE_UP
17723 }
17724
17725
17726 \f
17727 /***********************************************************************
17728 More debugging support
17729 ***********************************************************************/
17730
17731 #ifdef GLYPH_DEBUG
17732
17733 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17734 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17735 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17736
17737
17738 /* Dump the contents of glyph matrix MATRIX on stderr.
17739
17740 GLYPHS 0 means don't show glyph contents.
17741 GLYPHS 1 means show glyphs in short form
17742 GLYPHS > 1 means show glyphs in long form. */
17743
17744 void
17745 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17746 {
17747 int i;
17748 for (i = 0; i < matrix->nrows; ++i)
17749 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17750 }
17751
17752
17753 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17754 the glyph row and area where the glyph comes from. */
17755
17756 void
17757 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17758 {
17759 if (glyph->type == CHAR_GLYPH
17760 || glyph->type == GLYPHLESS_GLYPH)
17761 {
17762 fprintf (stderr,
17763 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17764 glyph - row->glyphs[TEXT_AREA],
17765 (glyph->type == CHAR_GLYPH
17766 ? 'C'
17767 : 'G'),
17768 glyph->charpos,
17769 (BUFFERP (glyph->object)
17770 ? 'B'
17771 : (STRINGP (glyph->object)
17772 ? 'S'
17773 : (INTEGERP (glyph->object)
17774 ? '0'
17775 : '-'))),
17776 glyph->pixel_width,
17777 glyph->u.ch,
17778 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17779 ? glyph->u.ch
17780 : '.'),
17781 glyph->face_id,
17782 glyph->left_box_line_p,
17783 glyph->right_box_line_p);
17784 }
17785 else if (glyph->type == STRETCH_GLYPH)
17786 {
17787 fprintf (stderr,
17788 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17789 glyph - row->glyphs[TEXT_AREA],
17790 'S',
17791 glyph->charpos,
17792 (BUFFERP (glyph->object)
17793 ? 'B'
17794 : (STRINGP (glyph->object)
17795 ? 'S'
17796 : (INTEGERP (glyph->object)
17797 ? '0'
17798 : '-'))),
17799 glyph->pixel_width,
17800 0,
17801 ' ',
17802 glyph->face_id,
17803 glyph->left_box_line_p,
17804 glyph->right_box_line_p);
17805 }
17806 else if (glyph->type == IMAGE_GLYPH)
17807 {
17808 fprintf (stderr,
17809 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17810 glyph - row->glyphs[TEXT_AREA],
17811 'I',
17812 glyph->charpos,
17813 (BUFFERP (glyph->object)
17814 ? 'B'
17815 : (STRINGP (glyph->object)
17816 ? 'S'
17817 : (INTEGERP (glyph->object)
17818 ? '0'
17819 : '-'))),
17820 glyph->pixel_width,
17821 glyph->u.img_id,
17822 '.',
17823 glyph->face_id,
17824 glyph->left_box_line_p,
17825 glyph->right_box_line_p);
17826 }
17827 else if (glyph->type == COMPOSITE_GLYPH)
17828 {
17829 fprintf (stderr,
17830 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17831 glyph - row->glyphs[TEXT_AREA],
17832 '+',
17833 glyph->charpos,
17834 (BUFFERP (glyph->object)
17835 ? 'B'
17836 : (STRINGP (glyph->object)
17837 ? 'S'
17838 : (INTEGERP (glyph->object)
17839 ? '0'
17840 : '-'))),
17841 glyph->pixel_width,
17842 glyph->u.cmp.id);
17843 if (glyph->u.cmp.automatic)
17844 fprintf (stderr,
17845 "[%d-%d]",
17846 glyph->slice.cmp.from, glyph->slice.cmp.to);
17847 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17848 glyph->face_id,
17849 glyph->left_box_line_p,
17850 glyph->right_box_line_p);
17851 }
17852 }
17853
17854
17855 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17856 GLYPHS 0 means don't show glyph contents.
17857 GLYPHS 1 means show glyphs in short form
17858 GLYPHS > 1 means show glyphs in long form. */
17859
17860 void
17861 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17862 {
17863 if (glyphs != 1)
17864 {
17865 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17866 fprintf (stderr, "==============================================================================\n");
17867
17868 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17869 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17870 vpos,
17871 MATRIX_ROW_START_CHARPOS (row),
17872 MATRIX_ROW_END_CHARPOS (row),
17873 row->used[TEXT_AREA],
17874 row->contains_overlapping_glyphs_p,
17875 row->enabled_p,
17876 row->truncated_on_left_p,
17877 row->truncated_on_right_p,
17878 row->continued_p,
17879 MATRIX_ROW_CONTINUATION_LINE_P (row),
17880 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17881 row->ends_at_zv_p,
17882 row->fill_line_p,
17883 row->ends_in_middle_of_char_p,
17884 row->starts_in_middle_of_char_p,
17885 row->mouse_face_p,
17886 row->x,
17887 row->y,
17888 row->pixel_width,
17889 row->height,
17890 row->visible_height,
17891 row->ascent,
17892 row->phys_ascent);
17893 /* The next 3 lines should align to "Start" in the header. */
17894 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17895 row->end.overlay_string_index,
17896 row->continuation_lines_width);
17897 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17898 CHARPOS (row->start.string_pos),
17899 CHARPOS (row->end.string_pos));
17900 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17901 row->end.dpvec_index);
17902 }
17903
17904 if (glyphs > 1)
17905 {
17906 int area;
17907
17908 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17909 {
17910 struct glyph *glyph = row->glyphs[area];
17911 struct glyph *glyph_end = glyph + row->used[area];
17912
17913 /* Glyph for a line end in text. */
17914 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17915 ++glyph_end;
17916
17917 if (glyph < glyph_end)
17918 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17919
17920 for (; glyph < glyph_end; ++glyph)
17921 dump_glyph (row, glyph, area);
17922 }
17923 }
17924 else if (glyphs == 1)
17925 {
17926 int area;
17927
17928 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17929 {
17930 char *s = alloca (row->used[area] + 4);
17931 int i;
17932
17933 for (i = 0; i < row->used[area]; ++i)
17934 {
17935 struct glyph *glyph = row->glyphs[area] + i;
17936 if (i == row->used[area] - 1
17937 && area == TEXT_AREA
17938 && INTEGERP (glyph->object)
17939 && glyph->type == CHAR_GLYPH
17940 && glyph->u.ch == ' ')
17941 {
17942 strcpy (&s[i], "[\\n]");
17943 i += 4;
17944 }
17945 else if (glyph->type == CHAR_GLYPH
17946 && glyph->u.ch < 0x80
17947 && glyph->u.ch >= ' ')
17948 s[i] = glyph->u.ch;
17949 else
17950 s[i] = '.';
17951 }
17952
17953 s[i] = '\0';
17954 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17955 }
17956 }
17957 }
17958
17959
17960 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17961 Sdump_glyph_matrix, 0, 1, "p",
17962 doc: /* Dump the current matrix of the selected window to stderr.
17963 Shows contents of glyph row structures. With non-nil
17964 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17965 glyphs in short form, otherwise show glyphs in long form. */)
17966 (Lisp_Object glyphs)
17967 {
17968 struct window *w = XWINDOW (selected_window);
17969 struct buffer *buffer = XBUFFER (w->contents);
17970
17971 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17972 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17973 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17974 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17975 fprintf (stderr, "=============================================\n");
17976 dump_glyph_matrix (w->current_matrix,
17977 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17978 return Qnil;
17979 }
17980
17981
17982 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17983 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17984 (void)
17985 {
17986 struct frame *f = XFRAME (selected_frame);
17987 dump_glyph_matrix (f->current_matrix, 1);
17988 return Qnil;
17989 }
17990
17991
17992 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17993 doc: /* Dump glyph row ROW to stderr.
17994 GLYPH 0 means don't dump glyphs.
17995 GLYPH 1 means dump glyphs in short form.
17996 GLYPH > 1 or omitted means dump glyphs in long form. */)
17997 (Lisp_Object row, Lisp_Object glyphs)
17998 {
17999 struct glyph_matrix *matrix;
18000 EMACS_INT vpos;
18001
18002 CHECK_NUMBER (row);
18003 matrix = XWINDOW (selected_window)->current_matrix;
18004 vpos = XINT (row);
18005 if (vpos >= 0 && vpos < matrix->nrows)
18006 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18007 vpos,
18008 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18009 return Qnil;
18010 }
18011
18012
18013 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18014 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18015 GLYPH 0 means don't dump glyphs.
18016 GLYPH 1 means dump glyphs in short form.
18017 GLYPH > 1 or omitted means dump glyphs in long form. */)
18018 (Lisp_Object row, Lisp_Object glyphs)
18019 {
18020 struct frame *sf = SELECTED_FRAME ();
18021 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18022 EMACS_INT vpos;
18023
18024 CHECK_NUMBER (row);
18025 vpos = XINT (row);
18026 if (vpos >= 0 && vpos < m->nrows)
18027 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18028 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18029 return Qnil;
18030 }
18031
18032
18033 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18034 doc: /* Toggle tracing of redisplay.
18035 With ARG, turn tracing on if and only if ARG is positive. */)
18036 (Lisp_Object arg)
18037 {
18038 if (NILP (arg))
18039 trace_redisplay_p = !trace_redisplay_p;
18040 else
18041 {
18042 arg = Fprefix_numeric_value (arg);
18043 trace_redisplay_p = XINT (arg) > 0;
18044 }
18045
18046 return Qnil;
18047 }
18048
18049
18050 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18051 doc: /* Like `format', but print result to stderr.
18052 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18053 (ptrdiff_t nargs, Lisp_Object *args)
18054 {
18055 Lisp_Object s = Fformat (nargs, args);
18056 fprintf (stderr, "%s", SDATA (s));
18057 return Qnil;
18058 }
18059
18060 #endif /* GLYPH_DEBUG */
18061
18062
18063 \f
18064 /***********************************************************************
18065 Building Desired Matrix Rows
18066 ***********************************************************************/
18067
18068 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18069 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18070
18071 static struct glyph_row *
18072 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18073 {
18074 struct frame *f = XFRAME (WINDOW_FRAME (w));
18075 struct buffer *buffer = XBUFFER (w->contents);
18076 struct buffer *old = current_buffer;
18077 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18078 int arrow_len = SCHARS (overlay_arrow_string);
18079 const unsigned char *arrow_end = arrow_string + arrow_len;
18080 const unsigned char *p;
18081 struct it it;
18082 bool multibyte_p;
18083 int n_glyphs_before;
18084
18085 set_buffer_temp (buffer);
18086 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18087 it.glyph_row->used[TEXT_AREA] = 0;
18088 SET_TEXT_POS (it.position, 0, 0);
18089
18090 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18091 p = arrow_string;
18092 while (p < arrow_end)
18093 {
18094 Lisp_Object face, ilisp;
18095
18096 /* Get the next character. */
18097 if (multibyte_p)
18098 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18099 else
18100 {
18101 it.c = it.char_to_display = *p, it.len = 1;
18102 if (! ASCII_CHAR_P (it.c))
18103 it.char_to_display = BYTE8_TO_CHAR (it.c);
18104 }
18105 p += it.len;
18106
18107 /* Get its face. */
18108 ilisp = make_number (p - arrow_string);
18109 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18110 it.face_id = compute_char_face (f, it.char_to_display, face);
18111
18112 /* Compute its width, get its glyphs. */
18113 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18114 SET_TEXT_POS (it.position, -1, -1);
18115 PRODUCE_GLYPHS (&it);
18116
18117 /* If this character doesn't fit any more in the line, we have
18118 to remove some glyphs. */
18119 if (it.current_x > it.last_visible_x)
18120 {
18121 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18122 break;
18123 }
18124 }
18125
18126 set_buffer_temp (old);
18127 return it.glyph_row;
18128 }
18129
18130
18131 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18132 glyphs to insert is determined by produce_special_glyphs. */
18133
18134 static void
18135 insert_left_trunc_glyphs (struct it *it)
18136 {
18137 struct it truncate_it;
18138 struct glyph *from, *end, *to, *toend;
18139
18140 eassert (!FRAME_WINDOW_P (it->f)
18141 || (!it->glyph_row->reversed_p
18142 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18143 || (it->glyph_row->reversed_p
18144 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18145
18146 /* Get the truncation glyphs. */
18147 truncate_it = *it;
18148 truncate_it.current_x = 0;
18149 truncate_it.face_id = DEFAULT_FACE_ID;
18150 truncate_it.glyph_row = &scratch_glyph_row;
18151 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18152 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18153 truncate_it.object = make_number (0);
18154 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18155
18156 /* Overwrite glyphs from IT with truncation glyphs. */
18157 if (!it->glyph_row->reversed_p)
18158 {
18159 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18160
18161 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18162 end = from + tused;
18163 to = it->glyph_row->glyphs[TEXT_AREA];
18164 toend = to + it->glyph_row->used[TEXT_AREA];
18165 if (FRAME_WINDOW_P (it->f))
18166 {
18167 /* On GUI frames, when variable-size fonts are displayed,
18168 the truncation glyphs may need more pixels than the row's
18169 glyphs they overwrite. We overwrite more glyphs to free
18170 enough screen real estate, and enlarge the stretch glyph
18171 on the right (see display_line), if there is one, to
18172 preserve the screen position of the truncation glyphs on
18173 the right. */
18174 int w = 0;
18175 struct glyph *g = to;
18176 short used;
18177
18178 /* The first glyph could be partially visible, in which case
18179 it->glyph_row->x will be negative. But we want the left
18180 truncation glyphs to be aligned at the left margin of the
18181 window, so we override the x coordinate at which the row
18182 will begin. */
18183 it->glyph_row->x = 0;
18184 while (g < toend && w < it->truncation_pixel_width)
18185 {
18186 w += g->pixel_width;
18187 ++g;
18188 }
18189 if (g - to - tused > 0)
18190 {
18191 memmove (to + tused, g, (toend - g) * sizeof(*g));
18192 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18193 }
18194 used = it->glyph_row->used[TEXT_AREA];
18195 if (it->glyph_row->truncated_on_right_p
18196 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18197 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18198 == STRETCH_GLYPH)
18199 {
18200 int extra = w - it->truncation_pixel_width;
18201
18202 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18203 }
18204 }
18205
18206 while (from < end)
18207 *to++ = *from++;
18208
18209 /* There may be padding glyphs left over. Overwrite them too. */
18210 if (!FRAME_WINDOW_P (it->f))
18211 {
18212 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18213 {
18214 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18215 while (from < end)
18216 *to++ = *from++;
18217 }
18218 }
18219
18220 if (to > toend)
18221 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18222 }
18223 else
18224 {
18225 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18226
18227 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18228 that back to front. */
18229 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18230 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18231 toend = it->glyph_row->glyphs[TEXT_AREA];
18232 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18233 if (FRAME_WINDOW_P (it->f))
18234 {
18235 int w = 0;
18236 struct glyph *g = to;
18237
18238 while (g >= toend && w < it->truncation_pixel_width)
18239 {
18240 w += g->pixel_width;
18241 --g;
18242 }
18243 if (to - g - tused > 0)
18244 to = g + tused;
18245 if (it->glyph_row->truncated_on_right_p
18246 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18247 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18248 {
18249 int extra = w - it->truncation_pixel_width;
18250
18251 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18252 }
18253 }
18254
18255 while (from >= end && to >= toend)
18256 *to-- = *from--;
18257 if (!FRAME_WINDOW_P (it->f))
18258 {
18259 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18260 {
18261 from =
18262 truncate_it.glyph_row->glyphs[TEXT_AREA]
18263 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18264 while (from >= end && to >= toend)
18265 *to-- = *from--;
18266 }
18267 }
18268 if (from >= end)
18269 {
18270 /* Need to free some room before prepending additional
18271 glyphs. */
18272 int move_by = from - end + 1;
18273 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18274 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18275
18276 for ( ; g >= g0; g--)
18277 g[move_by] = *g;
18278 while (from >= end)
18279 *to-- = *from--;
18280 it->glyph_row->used[TEXT_AREA] += move_by;
18281 }
18282 }
18283 }
18284
18285 /* Compute the hash code for ROW. */
18286 unsigned
18287 row_hash (struct glyph_row *row)
18288 {
18289 int area, k;
18290 unsigned hashval = 0;
18291
18292 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18293 for (k = 0; k < row->used[area]; ++k)
18294 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18295 + row->glyphs[area][k].u.val
18296 + row->glyphs[area][k].face_id
18297 + row->glyphs[area][k].padding_p
18298 + (row->glyphs[area][k].type << 2));
18299
18300 return hashval;
18301 }
18302
18303 /* Compute the pixel height and width of IT->glyph_row.
18304
18305 Most of the time, ascent and height of a display line will be equal
18306 to the max_ascent and max_height values of the display iterator
18307 structure. This is not the case if
18308
18309 1. We hit ZV without displaying anything. In this case, max_ascent
18310 and max_height will be zero.
18311
18312 2. We have some glyphs that don't contribute to the line height.
18313 (The glyph row flag contributes_to_line_height_p is for future
18314 pixmap extensions).
18315
18316 The first case is easily covered by using default values because in
18317 these cases, the line height does not really matter, except that it
18318 must not be zero. */
18319
18320 static void
18321 compute_line_metrics (struct it *it)
18322 {
18323 struct glyph_row *row = it->glyph_row;
18324
18325 if (FRAME_WINDOW_P (it->f))
18326 {
18327 int i, min_y, max_y;
18328
18329 /* The line may consist of one space only, that was added to
18330 place the cursor on it. If so, the row's height hasn't been
18331 computed yet. */
18332 if (row->height == 0)
18333 {
18334 if (it->max_ascent + it->max_descent == 0)
18335 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18336 row->ascent = it->max_ascent;
18337 row->height = it->max_ascent + it->max_descent;
18338 row->phys_ascent = it->max_phys_ascent;
18339 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18340 row->extra_line_spacing = it->max_extra_line_spacing;
18341 }
18342
18343 /* Compute the width of this line. */
18344 row->pixel_width = row->x;
18345 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18346 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18347
18348 eassert (row->pixel_width >= 0);
18349 eassert (row->ascent >= 0 && row->height > 0);
18350
18351 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18352 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18353
18354 /* If first line's physical ascent is larger than its logical
18355 ascent, use the physical ascent, and make the row taller.
18356 This makes accented characters fully visible. */
18357 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18358 && row->phys_ascent > row->ascent)
18359 {
18360 row->height += row->phys_ascent - row->ascent;
18361 row->ascent = row->phys_ascent;
18362 }
18363
18364 /* Compute how much of the line is visible. */
18365 row->visible_height = row->height;
18366
18367 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18368 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18369
18370 if (row->y < min_y)
18371 row->visible_height -= min_y - row->y;
18372 if (row->y + row->height > max_y)
18373 row->visible_height -= row->y + row->height - max_y;
18374 }
18375 else
18376 {
18377 row->pixel_width = row->used[TEXT_AREA];
18378 if (row->continued_p)
18379 row->pixel_width -= it->continuation_pixel_width;
18380 else if (row->truncated_on_right_p)
18381 row->pixel_width -= it->truncation_pixel_width;
18382 row->ascent = row->phys_ascent = 0;
18383 row->height = row->phys_height = row->visible_height = 1;
18384 row->extra_line_spacing = 0;
18385 }
18386
18387 /* Compute a hash code for this row. */
18388 row->hash = row_hash (row);
18389
18390 it->max_ascent = it->max_descent = 0;
18391 it->max_phys_ascent = it->max_phys_descent = 0;
18392 }
18393
18394
18395 /* Append one space to the glyph row of iterator IT if doing a
18396 window-based redisplay. The space has the same face as
18397 IT->face_id. Value is non-zero if a space was added.
18398
18399 This function is called to make sure that there is always one glyph
18400 at the end of a glyph row that the cursor can be set on under
18401 window-systems. (If there weren't such a glyph we would not know
18402 how wide and tall a box cursor should be displayed).
18403
18404 At the same time this space let's a nicely handle clearing to the
18405 end of the line if the row ends in italic text. */
18406
18407 static int
18408 append_space_for_newline (struct it *it, int default_face_p)
18409 {
18410 if (FRAME_WINDOW_P (it->f))
18411 {
18412 int n = it->glyph_row->used[TEXT_AREA];
18413
18414 if (it->glyph_row->glyphs[TEXT_AREA] + n
18415 < it->glyph_row->glyphs[1 + TEXT_AREA])
18416 {
18417 /* Save some values that must not be changed.
18418 Must save IT->c and IT->len because otherwise
18419 ITERATOR_AT_END_P wouldn't work anymore after
18420 append_space_for_newline has been called. */
18421 enum display_element_type saved_what = it->what;
18422 int saved_c = it->c, saved_len = it->len;
18423 int saved_char_to_display = it->char_to_display;
18424 int saved_x = it->current_x;
18425 int saved_face_id = it->face_id;
18426 int saved_box_end = it->end_of_box_run_p;
18427 struct text_pos saved_pos;
18428 Lisp_Object saved_object;
18429 struct face *face;
18430
18431 saved_object = it->object;
18432 saved_pos = it->position;
18433
18434 it->what = IT_CHARACTER;
18435 memset (&it->position, 0, sizeof it->position);
18436 it->object = make_number (0);
18437 it->c = it->char_to_display = ' ';
18438 it->len = 1;
18439
18440 /* If the default face was remapped, be sure to use the
18441 remapped face for the appended newline. */
18442 if (default_face_p)
18443 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18444 else if (it->face_before_selective_p)
18445 it->face_id = it->saved_face_id;
18446 face = FACE_FROM_ID (it->f, it->face_id);
18447 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18448 /* In R2L rows, we will prepend a stretch glyph that will
18449 have the end_of_box_run_p flag set for it, so there's no
18450 need for the appended newline glyph to have that flag
18451 set. */
18452 if (it->glyph_row->reversed_p
18453 /* But if the appended newline glyph goes all the way to
18454 the end of the row, there will be no stretch glyph,
18455 so leave the box flag set. */
18456 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18457 it->end_of_box_run_p = 0;
18458
18459 PRODUCE_GLYPHS (it);
18460
18461 it->override_ascent = -1;
18462 it->constrain_row_ascent_descent_p = 0;
18463 it->current_x = saved_x;
18464 it->object = saved_object;
18465 it->position = saved_pos;
18466 it->what = saved_what;
18467 it->face_id = saved_face_id;
18468 it->len = saved_len;
18469 it->c = saved_c;
18470 it->char_to_display = saved_char_to_display;
18471 it->end_of_box_run_p = saved_box_end;
18472 return 1;
18473 }
18474 }
18475
18476 return 0;
18477 }
18478
18479
18480 /* Extend the face of the last glyph in the text area of IT->glyph_row
18481 to the end of the display line. Called from display_line. If the
18482 glyph row is empty, add a space glyph to it so that we know the
18483 face to draw. Set the glyph row flag fill_line_p. If the glyph
18484 row is R2L, prepend a stretch glyph to cover the empty space to the
18485 left of the leftmost glyph. */
18486
18487 static void
18488 extend_face_to_end_of_line (struct it *it)
18489 {
18490 struct face *face, *default_face;
18491 struct frame *f = it->f;
18492
18493 /* If line is already filled, do nothing. Non window-system frames
18494 get a grace of one more ``pixel'' because their characters are
18495 1-``pixel'' wide, so they hit the equality too early. This grace
18496 is needed only for R2L rows that are not continued, to produce
18497 one extra blank where we could display the cursor. */
18498 if (it->current_x >= it->last_visible_x
18499 + (!FRAME_WINDOW_P (f)
18500 && it->glyph_row->reversed_p
18501 && !it->glyph_row->continued_p))
18502 return;
18503
18504 /* The default face, possibly remapped. */
18505 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18506
18507 /* Face extension extends the background and box of IT->face_id
18508 to the end of the line. If the background equals the background
18509 of the frame, we don't have to do anything. */
18510 if (it->face_before_selective_p)
18511 face = FACE_FROM_ID (f, it->saved_face_id);
18512 else
18513 face = FACE_FROM_ID (f, it->face_id);
18514
18515 if (FRAME_WINDOW_P (f)
18516 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18517 && face->box == FACE_NO_BOX
18518 && face->background == FRAME_BACKGROUND_PIXEL (f)
18519 && !face->stipple
18520 && !it->glyph_row->reversed_p)
18521 return;
18522
18523 /* Set the glyph row flag indicating that the face of the last glyph
18524 in the text area has to be drawn to the end of the text area. */
18525 it->glyph_row->fill_line_p = 1;
18526
18527 /* If current character of IT is not ASCII, make sure we have the
18528 ASCII face. This will be automatically undone the next time
18529 get_next_display_element returns a multibyte character. Note
18530 that the character will always be single byte in unibyte
18531 text. */
18532 if (!ASCII_CHAR_P (it->c))
18533 {
18534 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18535 }
18536
18537 if (FRAME_WINDOW_P (f))
18538 {
18539 /* If the row is empty, add a space with the current face of IT,
18540 so that we know which face to draw. */
18541 if (it->glyph_row->used[TEXT_AREA] == 0)
18542 {
18543 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18544 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18545 it->glyph_row->used[TEXT_AREA] = 1;
18546 }
18547 #ifdef HAVE_WINDOW_SYSTEM
18548 if (it->glyph_row->reversed_p)
18549 {
18550 /* Prepend a stretch glyph to the row, such that the
18551 rightmost glyph will be drawn flushed all the way to the
18552 right margin of the window. The stretch glyph that will
18553 occupy the empty space, if any, to the left of the
18554 glyphs. */
18555 struct font *font = face->font ? face->font : FRAME_FONT (f);
18556 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18557 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18558 struct glyph *g;
18559 int row_width, stretch_ascent, stretch_width;
18560 struct text_pos saved_pos;
18561 int saved_face_id, saved_avoid_cursor, saved_box_start;
18562
18563 for (row_width = 0, g = row_start; g < row_end; g++)
18564 row_width += g->pixel_width;
18565 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18566 if (stretch_width > 0)
18567 {
18568 stretch_ascent =
18569 (((it->ascent + it->descent)
18570 * FONT_BASE (font)) / FONT_HEIGHT (font));
18571 saved_pos = it->position;
18572 memset (&it->position, 0, sizeof it->position);
18573 saved_avoid_cursor = it->avoid_cursor_p;
18574 it->avoid_cursor_p = 1;
18575 saved_face_id = it->face_id;
18576 saved_box_start = it->start_of_box_run_p;
18577 /* The last row's stretch glyph should get the default
18578 face, to avoid painting the rest of the window with
18579 the region face, if the region ends at ZV. */
18580 if (it->glyph_row->ends_at_zv_p)
18581 it->face_id = default_face->id;
18582 else
18583 it->face_id = face->id;
18584 it->start_of_box_run_p = 0;
18585 append_stretch_glyph (it, make_number (0), stretch_width,
18586 it->ascent + it->descent, stretch_ascent);
18587 it->position = saved_pos;
18588 it->avoid_cursor_p = saved_avoid_cursor;
18589 it->face_id = saved_face_id;
18590 it->start_of_box_run_p = saved_box_start;
18591 }
18592 }
18593 #endif /* HAVE_WINDOW_SYSTEM */
18594 }
18595 else
18596 {
18597 /* Save some values that must not be changed. */
18598 int saved_x = it->current_x;
18599 struct text_pos saved_pos;
18600 Lisp_Object saved_object;
18601 enum display_element_type saved_what = it->what;
18602 int saved_face_id = it->face_id;
18603
18604 saved_object = it->object;
18605 saved_pos = it->position;
18606
18607 it->what = IT_CHARACTER;
18608 memset (&it->position, 0, sizeof it->position);
18609 it->object = make_number (0);
18610 it->c = it->char_to_display = ' ';
18611 it->len = 1;
18612 /* The last row's blank glyphs should get the default face, to
18613 avoid painting the rest of the window with the region face,
18614 if the region ends at ZV. */
18615 if (it->glyph_row->ends_at_zv_p)
18616 it->face_id = default_face->id;
18617 else
18618 it->face_id = face->id;
18619
18620 PRODUCE_GLYPHS (it);
18621
18622 while (it->current_x <= it->last_visible_x)
18623 PRODUCE_GLYPHS (it);
18624
18625 /* Don't count these blanks really. It would let us insert a left
18626 truncation glyph below and make us set the cursor on them, maybe. */
18627 it->current_x = saved_x;
18628 it->object = saved_object;
18629 it->position = saved_pos;
18630 it->what = saved_what;
18631 it->face_id = saved_face_id;
18632 }
18633 }
18634
18635
18636 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18637 trailing whitespace. */
18638
18639 static int
18640 trailing_whitespace_p (ptrdiff_t charpos)
18641 {
18642 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18643 int c = 0;
18644
18645 while (bytepos < ZV_BYTE
18646 && (c = FETCH_CHAR (bytepos),
18647 c == ' ' || c == '\t'))
18648 ++bytepos;
18649
18650 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18651 {
18652 if (bytepos != PT_BYTE)
18653 return 1;
18654 }
18655 return 0;
18656 }
18657
18658
18659 /* Highlight trailing whitespace, if any, in ROW. */
18660
18661 static void
18662 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18663 {
18664 int used = row->used[TEXT_AREA];
18665
18666 if (used)
18667 {
18668 struct glyph *start = row->glyphs[TEXT_AREA];
18669 struct glyph *glyph = start + used - 1;
18670
18671 if (row->reversed_p)
18672 {
18673 /* Right-to-left rows need to be processed in the opposite
18674 direction, so swap the edge pointers. */
18675 glyph = start;
18676 start = row->glyphs[TEXT_AREA] + used - 1;
18677 }
18678
18679 /* Skip over glyphs inserted to display the cursor at the
18680 end of a line, for extending the face of the last glyph
18681 to the end of the line on terminals, and for truncation
18682 and continuation glyphs. */
18683 if (!row->reversed_p)
18684 {
18685 while (glyph >= start
18686 && glyph->type == CHAR_GLYPH
18687 && INTEGERP (glyph->object))
18688 --glyph;
18689 }
18690 else
18691 {
18692 while (glyph <= start
18693 && glyph->type == CHAR_GLYPH
18694 && INTEGERP (glyph->object))
18695 ++glyph;
18696 }
18697
18698 /* If last glyph is a space or stretch, and it's trailing
18699 whitespace, set the face of all trailing whitespace glyphs in
18700 IT->glyph_row to `trailing-whitespace'. */
18701 if ((row->reversed_p ? glyph <= start : glyph >= start)
18702 && BUFFERP (glyph->object)
18703 && (glyph->type == STRETCH_GLYPH
18704 || (glyph->type == CHAR_GLYPH
18705 && glyph->u.ch == ' '))
18706 && trailing_whitespace_p (glyph->charpos))
18707 {
18708 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18709 if (face_id < 0)
18710 return;
18711
18712 if (!row->reversed_p)
18713 {
18714 while (glyph >= start
18715 && BUFFERP (glyph->object)
18716 && (glyph->type == STRETCH_GLYPH
18717 || (glyph->type == CHAR_GLYPH
18718 && glyph->u.ch == ' ')))
18719 (glyph--)->face_id = face_id;
18720 }
18721 else
18722 {
18723 while (glyph <= start
18724 && BUFFERP (glyph->object)
18725 && (glyph->type == STRETCH_GLYPH
18726 || (glyph->type == CHAR_GLYPH
18727 && glyph->u.ch == ' ')))
18728 (glyph++)->face_id = face_id;
18729 }
18730 }
18731 }
18732 }
18733
18734
18735 /* Value is non-zero if glyph row ROW should be
18736 considered to hold the buffer position CHARPOS. */
18737
18738 static int
18739 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18740 {
18741 int result = 1;
18742
18743 if (charpos == CHARPOS (row->end.pos)
18744 || charpos == MATRIX_ROW_END_CHARPOS (row))
18745 {
18746 /* Suppose the row ends on a string.
18747 Unless the row is continued, that means it ends on a newline
18748 in the string. If it's anything other than a display string
18749 (e.g., a before-string from an overlay), we don't want the
18750 cursor there. (This heuristic seems to give the optimal
18751 behavior for the various types of multi-line strings.)
18752 One exception: if the string has `cursor' property on one of
18753 its characters, we _do_ want the cursor there. */
18754 if (CHARPOS (row->end.string_pos) >= 0)
18755 {
18756 if (row->continued_p)
18757 result = 1;
18758 else
18759 {
18760 /* Check for `display' property. */
18761 struct glyph *beg = row->glyphs[TEXT_AREA];
18762 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18763 struct glyph *glyph;
18764
18765 result = 0;
18766 for (glyph = end; glyph >= beg; --glyph)
18767 if (STRINGP (glyph->object))
18768 {
18769 Lisp_Object prop
18770 = Fget_char_property (make_number (charpos),
18771 Qdisplay, Qnil);
18772 result =
18773 (!NILP (prop)
18774 && display_prop_string_p (prop, glyph->object));
18775 /* If there's a `cursor' property on one of the
18776 string's characters, this row is a cursor row,
18777 even though this is not a display string. */
18778 if (!result)
18779 {
18780 Lisp_Object s = glyph->object;
18781
18782 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18783 {
18784 ptrdiff_t gpos = glyph->charpos;
18785
18786 if (!NILP (Fget_char_property (make_number (gpos),
18787 Qcursor, s)))
18788 {
18789 result = 1;
18790 break;
18791 }
18792 }
18793 }
18794 break;
18795 }
18796 }
18797 }
18798 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18799 {
18800 /* If the row ends in middle of a real character,
18801 and the line is continued, we want the cursor here.
18802 That's because CHARPOS (ROW->end.pos) would equal
18803 PT if PT is before the character. */
18804 if (!row->ends_in_ellipsis_p)
18805 result = row->continued_p;
18806 else
18807 /* If the row ends in an ellipsis, then
18808 CHARPOS (ROW->end.pos) will equal point after the
18809 invisible text. We want that position to be displayed
18810 after the ellipsis. */
18811 result = 0;
18812 }
18813 /* If the row ends at ZV, display the cursor at the end of that
18814 row instead of at the start of the row below. */
18815 else if (row->ends_at_zv_p)
18816 result = 1;
18817 else
18818 result = 0;
18819 }
18820
18821 return result;
18822 }
18823
18824 /* Value is non-zero if glyph row ROW should be
18825 used to hold the cursor. */
18826
18827 static int
18828 cursor_row_p (struct glyph_row *row)
18829 {
18830 return row_for_charpos_p (row, PT);
18831 }
18832
18833 \f
18834
18835 /* Push the property PROP so that it will be rendered at the current
18836 position in IT. Return 1 if PROP was successfully pushed, 0
18837 otherwise. Called from handle_line_prefix to handle the
18838 `line-prefix' and `wrap-prefix' properties. */
18839
18840 static int
18841 push_prefix_prop (struct it *it, Lisp_Object prop)
18842 {
18843 struct text_pos pos =
18844 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18845
18846 eassert (it->method == GET_FROM_BUFFER
18847 || it->method == GET_FROM_DISPLAY_VECTOR
18848 || it->method == GET_FROM_STRING);
18849
18850 /* We need to save the current buffer/string position, so it will be
18851 restored by pop_it, because iterate_out_of_display_property
18852 depends on that being set correctly, but some situations leave
18853 it->position not yet set when this function is called. */
18854 push_it (it, &pos);
18855
18856 if (STRINGP (prop))
18857 {
18858 if (SCHARS (prop) == 0)
18859 {
18860 pop_it (it);
18861 return 0;
18862 }
18863
18864 it->string = prop;
18865 it->string_from_prefix_prop_p = 1;
18866 it->multibyte_p = STRING_MULTIBYTE (it->string);
18867 it->current.overlay_string_index = -1;
18868 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18869 it->end_charpos = it->string_nchars = SCHARS (it->string);
18870 it->method = GET_FROM_STRING;
18871 it->stop_charpos = 0;
18872 it->prev_stop = 0;
18873 it->base_level_stop = 0;
18874
18875 /* Force paragraph direction to be that of the parent
18876 buffer/string. */
18877 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18878 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18879 else
18880 it->paragraph_embedding = L2R;
18881
18882 /* Set up the bidi iterator for this display string. */
18883 if (it->bidi_p)
18884 {
18885 it->bidi_it.string.lstring = it->string;
18886 it->bidi_it.string.s = NULL;
18887 it->bidi_it.string.schars = it->end_charpos;
18888 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18889 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18890 it->bidi_it.string.unibyte = !it->multibyte_p;
18891 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18892 }
18893 }
18894 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18895 {
18896 it->method = GET_FROM_STRETCH;
18897 it->object = prop;
18898 }
18899 #ifdef HAVE_WINDOW_SYSTEM
18900 else if (IMAGEP (prop))
18901 {
18902 it->what = IT_IMAGE;
18903 it->image_id = lookup_image (it->f, prop);
18904 it->method = GET_FROM_IMAGE;
18905 }
18906 #endif /* HAVE_WINDOW_SYSTEM */
18907 else
18908 {
18909 pop_it (it); /* bogus display property, give up */
18910 return 0;
18911 }
18912
18913 return 1;
18914 }
18915
18916 /* Return the character-property PROP at the current position in IT. */
18917
18918 static Lisp_Object
18919 get_it_property (struct it *it, Lisp_Object prop)
18920 {
18921 Lisp_Object position;
18922
18923 if (STRINGP (it->object))
18924 position = make_number (IT_STRING_CHARPOS (*it));
18925 else if (BUFFERP (it->object))
18926 position = make_number (IT_CHARPOS (*it));
18927 else
18928 return Qnil;
18929
18930 return Fget_char_property (position, prop, it->object);
18931 }
18932
18933 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18934
18935 static void
18936 handle_line_prefix (struct it *it)
18937 {
18938 Lisp_Object prefix;
18939
18940 if (it->continuation_lines_width > 0)
18941 {
18942 prefix = get_it_property (it, Qwrap_prefix);
18943 if (NILP (prefix))
18944 prefix = Vwrap_prefix;
18945 }
18946 else
18947 {
18948 prefix = get_it_property (it, Qline_prefix);
18949 if (NILP (prefix))
18950 prefix = Vline_prefix;
18951 }
18952 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18953 {
18954 /* If the prefix is wider than the window, and we try to wrap
18955 it, it would acquire its own wrap prefix, and so on till the
18956 iterator stack overflows. So, don't wrap the prefix. */
18957 it->line_wrap = TRUNCATE;
18958 it->avoid_cursor_p = 1;
18959 }
18960 }
18961
18962 \f
18963
18964 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18965 only for R2L lines from display_line and display_string, when they
18966 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18967 the line/string needs to be continued on the next glyph row. */
18968 static void
18969 unproduce_glyphs (struct it *it, int n)
18970 {
18971 struct glyph *glyph, *end;
18972
18973 eassert (it->glyph_row);
18974 eassert (it->glyph_row->reversed_p);
18975 eassert (it->area == TEXT_AREA);
18976 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18977
18978 if (n > it->glyph_row->used[TEXT_AREA])
18979 n = it->glyph_row->used[TEXT_AREA];
18980 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18981 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18982 for ( ; glyph < end; glyph++)
18983 glyph[-n] = *glyph;
18984 }
18985
18986 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18987 and ROW->maxpos. */
18988 static void
18989 find_row_edges (struct it *it, struct glyph_row *row,
18990 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18991 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18992 {
18993 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18994 lines' rows is implemented for bidi-reordered rows. */
18995
18996 /* ROW->minpos is the value of min_pos, the minimal buffer position
18997 we have in ROW, or ROW->start.pos if that is smaller. */
18998 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18999 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19000 else
19001 /* We didn't find buffer positions smaller than ROW->start, or
19002 didn't find _any_ valid buffer positions in any of the glyphs,
19003 so we must trust the iterator's computed positions. */
19004 row->minpos = row->start.pos;
19005 if (max_pos <= 0)
19006 {
19007 max_pos = CHARPOS (it->current.pos);
19008 max_bpos = BYTEPOS (it->current.pos);
19009 }
19010
19011 /* Here are the various use-cases for ending the row, and the
19012 corresponding values for ROW->maxpos:
19013
19014 Line ends in a newline from buffer eol_pos + 1
19015 Line is continued from buffer max_pos + 1
19016 Line is truncated on right it->current.pos
19017 Line ends in a newline from string max_pos + 1(*)
19018 (*) + 1 only when line ends in a forward scan
19019 Line is continued from string max_pos
19020 Line is continued from display vector max_pos
19021 Line is entirely from a string min_pos == max_pos
19022 Line is entirely from a display vector min_pos == max_pos
19023 Line that ends at ZV ZV
19024
19025 If you discover other use-cases, please add them here as
19026 appropriate. */
19027 if (row->ends_at_zv_p)
19028 row->maxpos = it->current.pos;
19029 else if (row->used[TEXT_AREA])
19030 {
19031 int seen_this_string = 0;
19032 struct glyph_row *r1 = row - 1;
19033
19034 /* Did we see the same display string on the previous row? */
19035 if (STRINGP (it->object)
19036 /* this is not the first row */
19037 && row > it->w->desired_matrix->rows
19038 /* previous row is not the header line */
19039 && !r1->mode_line_p
19040 /* previous row also ends in a newline from a string */
19041 && r1->ends_in_newline_from_string_p)
19042 {
19043 struct glyph *start, *end;
19044
19045 /* Search for the last glyph of the previous row that came
19046 from buffer or string. Depending on whether the row is
19047 L2R or R2L, we need to process it front to back or the
19048 other way round. */
19049 if (!r1->reversed_p)
19050 {
19051 start = r1->glyphs[TEXT_AREA];
19052 end = start + r1->used[TEXT_AREA];
19053 /* Glyphs inserted by redisplay have an integer (zero)
19054 as their object. */
19055 while (end > start
19056 && INTEGERP ((end - 1)->object)
19057 && (end - 1)->charpos <= 0)
19058 --end;
19059 if (end > start)
19060 {
19061 if (EQ ((end - 1)->object, it->object))
19062 seen_this_string = 1;
19063 }
19064 else
19065 /* If all the glyphs of the previous row were inserted
19066 by redisplay, it means the previous row was
19067 produced from a single newline, which is only
19068 possible if that newline came from the same string
19069 as the one which produced this ROW. */
19070 seen_this_string = 1;
19071 }
19072 else
19073 {
19074 end = r1->glyphs[TEXT_AREA] - 1;
19075 start = end + r1->used[TEXT_AREA];
19076 while (end < start
19077 && INTEGERP ((end + 1)->object)
19078 && (end + 1)->charpos <= 0)
19079 ++end;
19080 if (end < start)
19081 {
19082 if (EQ ((end + 1)->object, it->object))
19083 seen_this_string = 1;
19084 }
19085 else
19086 seen_this_string = 1;
19087 }
19088 }
19089 /* Take note of each display string that covers a newline only
19090 once, the first time we see it. This is for when a display
19091 string includes more than one newline in it. */
19092 if (row->ends_in_newline_from_string_p && !seen_this_string)
19093 {
19094 /* If we were scanning the buffer forward when we displayed
19095 the string, we want to account for at least one buffer
19096 position that belongs to this row (position covered by
19097 the display string), so that cursor positioning will
19098 consider this row as a candidate when point is at the end
19099 of the visual line represented by this row. This is not
19100 required when scanning back, because max_pos will already
19101 have a much larger value. */
19102 if (CHARPOS (row->end.pos) > max_pos)
19103 INC_BOTH (max_pos, max_bpos);
19104 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19105 }
19106 else if (CHARPOS (it->eol_pos) > 0)
19107 SET_TEXT_POS (row->maxpos,
19108 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19109 else if (row->continued_p)
19110 {
19111 /* If max_pos is different from IT's current position, it
19112 means IT->method does not belong to the display element
19113 at max_pos. However, it also means that the display
19114 element at max_pos was displayed in its entirety on this
19115 line, which is equivalent to saying that the next line
19116 starts at the next buffer position. */
19117 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19118 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19119 else
19120 {
19121 INC_BOTH (max_pos, max_bpos);
19122 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19123 }
19124 }
19125 else if (row->truncated_on_right_p)
19126 /* display_line already called reseat_at_next_visible_line_start,
19127 which puts the iterator at the beginning of the next line, in
19128 the logical order. */
19129 row->maxpos = it->current.pos;
19130 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19131 /* A line that is entirely from a string/image/stretch... */
19132 row->maxpos = row->minpos;
19133 else
19134 emacs_abort ();
19135 }
19136 else
19137 row->maxpos = it->current.pos;
19138 }
19139
19140 /* Construct the glyph row IT->glyph_row in the desired matrix of
19141 IT->w from text at the current position of IT. See dispextern.h
19142 for an overview of struct it. Value is non-zero if
19143 IT->glyph_row displays text, as opposed to a line displaying ZV
19144 only. */
19145
19146 static int
19147 display_line (struct it *it)
19148 {
19149 struct glyph_row *row = it->glyph_row;
19150 Lisp_Object overlay_arrow_string;
19151 struct it wrap_it;
19152 void *wrap_data = NULL;
19153 int may_wrap = 0, wrap_x IF_LINT (= 0);
19154 int wrap_row_used = -1;
19155 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19156 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19157 int wrap_row_extra_line_spacing IF_LINT (= 0);
19158 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19159 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19160 int cvpos;
19161 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19162 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19163
19164 /* We always start displaying at hpos zero even if hscrolled. */
19165 eassert (it->hpos == 0 && it->current_x == 0);
19166
19167 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19168 >= it->w->desired_matrix->nrows)
19169 {
19170 it->w->nrows_scale_factor++;
19171 fonts_changed_p = 1;
19172 return 0;
19173 }
19174
19175 /* Is IT->w showing the region? */
19176 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19177
19178 /* Clear the result glyph row and enable it. */
19179 prepare_desired_row (row);
19180
19181 row->y = it->current_y;
19182 row->start = it->start;
19183 row->continuation_lines_width = it->continuation_lines_width;
19184 row->displays_text_p = 1;
19185 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19186 it->starts_in_middle_of_char_p = 0;
19187
19188 /* Arrange the overlays nicely for our purposes. Usually, we call
19189 display_line on only one line at a time, in which case this
19190 can't really hurt too much, or we call it on lines which appear
19191 one after another in the buffer, in which case all calls to
19192 recenter_overlay_lists but the first will be pretty cheap. */
19193 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19194
19195 /* Move over display elements that are not visible because we are
19196 hscrolled. This may stop at an x-position < IT->first_visible_x
19197 if the first glyph is partially visible or if we hit a line end. */
19198 if (it->current_x < it->first_visible_x)
19199 {
19200 enum move_it_result move_result;
19201
19202 this_line_min_pos = row->start.pos;
19203 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19204 MOVE_TO_POS | MOVE_TO_X);
19205 /* If we are under a large hscroll, move_it_in_display_line_to
19206 could hit the end of the line without reaching
19207 it->first_visible_x. Pretend that we did reach it. This is
19208 especially important on a TTY, where we will call
19209 extend_face_to_end_of_line, which needs to know how many
19210 blank glyphs to produce. */
19211 if (it->current_x < it->first_visible_x
19212 && (move_result == MOVE_NEWLINE_OR_CR
19213 || move_result == MOVE_POS_MATCH_OR_ZV))
19214 it->current_x = it->first_visible_x;
19215
19216 /* Record the smallest positions seen while we moved over
19217 display elements that are not visible. This is needed by
19218 redisplay_internal for optimizing the case where the cursor
19219 stays inside the same line. The rest of this function only
19220 considers positions that are actually displayed, so
19221 RECORD_MAX_MIN_POS will not otherwise record positions that
19222 are hscrolled to the left of the left edge of the window. */
19223 min_pos = CHARPOS (this_line_min_pos);
19224 min_bpos = BYTEPOS (this_line_min_pos);
19225 }
19226 else
19227 {
19228 /* We only do this when not calling `move_it_in_display_line_to'
19229 above, because move_it_in_display_line_to calls
19230 handle_line_prefix itself. */
19231 handle_line_prefix (it);
19232 }
19233
19234 /* Get the initial row height. This is either the height of the
19235 text hscrolled, if there is any, or zero. */
19236 row->ascent = it->max_ascent;
19237 row->height = it->max_ascent + it->max_descent;
19238 row->phys_ascent = it->max_phys_ascent;
19239 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19240 row->extra_line_spacing = it->max_extra_line_spacing;
19241
19242 /* Utility macro to record max and min buffer positions seen until now. */
19243 #define RECORD_MAX_MIN_POS(IT) \
19244 do \
19245 { \
19246 int composition_p = !STRINGP ((IT)->string) \
19247 && ((IT)->what == IT_COMPOSITION); \
19248 ptrdiff_t current_pos = \
19249 composition_p ? (IT)->cmp_it.charpos \
19250 : IT_CHARPOS (*(IT)); \
19251 ptrdiff_t current_bpos = \
19252 composition_p ? CHAR_TO_BYTE (current_pos) \
19253 : IT_BYTEPOS (*(IT)); \
19254 if (current_pos < min_pos) \
19255 { \
19256 min_pos = current_pos; \
19257 min_bpos = current_bpos; \
19258 } \
19259 if (IT_CHARPOS (*it) > max_pos) \
19260 { \
19261 max_pos = IT_CHARPOS (*it); \
19262 max_bpos = IT_BYTEPOS (*it); \
19263 } \
19264 } \
19265 while (0)
19266
19267 /* Loop generating characters. The loop is left with IT on the next
19268 character to display. */
19269 while (1)
19270 {
19271 int n_glyphs_before, hpos_before, x_before;
19272 int x, nglyphs;
19273 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19274
19275 /* Retrieve the next thing to display. Value is zero if end of
19276 buffer reached. */
19277 if (!get_next_display_element (it))
19278 {
19279 /* Maybe add a space at the end of this line that is used to
19280 display the cursor there under X. Set the charpos of the
19281 first glyph of blank lines not corresponding to any text
19282 to -1. */
19283 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19284 row->exact_window_width_line_p = 1;
19285 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19286 || row->used[TEXT_AREA] == 0)
19287 {
19288 row->glyphs[TEXT_AREA]->charpos = -1;
19289 row->displays_text_p = 0;
19290
19291 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19292 && (!MINI_WINDOW_P (it->w)
19293 || (minibuf_level && EQ (it->window, minibuf_window))))
19294 row->indicate_empty_line_p = 1;
19295 }
19296
19297 it->continuation_lines_width = 0;
19298 row->ends_at_zv_p = 1;
19299 /* A row that displays right-to-left text must always have
19300 its last face extended all the way to the end of line,
19301 even if this row ends in ZV, because we still write to
19302 the screen left to right. We also need to extend the
19303 last face if the default face is remapped to some
19304 different face, otherwise the functions that clear
19305 portions of the screen will clear with the default face's
19306 background color. */
19307 if (row->reversed_p
19308 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19309 extend_face_to_end_of_line (it);
19310 break;
19311 }
19312
19313 /* Now, get the metrics of what we want to display. This also
19314 generates glyphs in `row' (which is IT->glyph_row). */
19315 n_glyphs_before = row->used[TEXT_AREA];
19316 x = it->current_x;
19317
19318 /* Remember the line height so far in case the next element doesn't
19319 fit on the line. */
19320 if (it->line_wrap != TRUNCATE)
19321 {
19322 ascent = it->max_ascent;
19323 descent = it->max_descent;
19324 phys_ascent = it->max_phys_ascent;
19325 phys_descent = it->max_phys_descent;
19326
19327 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19328 {
19329 if (IT_DISPLAYING_WHITESPACE (it))
19330 may_wrap = 1;
19331 else if (may_wrap)
19332 {
19333 SAVE_IT (wrap_it, *it, wrap_data);
19334 wrap_x = x;
19335 wrap_row_used = row->used[TEXT_AREA];
19336 wrap_row_ascent = row->ascent;
19337 wrap_row_height = row->height;
19338 wrap_row_phys_ascent = row->phys_ascent;
19339 wrap_row_phys_height = row->phys_height;
19340 wrap_row_extra_line_spacing = row->extra_line_spacing;
19341 wrap_row_min_pos = min_pos;
19342 wrap_row_min_bpos = min_bpos;
19343 wrap_row_max_pos = max_pos;
19344 wrap_row_max_bpos = max_bpos;
19345 may_wrap = 0;
19346 }
19347 }
19348 }
19349
19350 PRODUCE_GLYPHS (it);
19351
19352 /* If this display element was in marginal areas, continue with
19353 the next one. */
19354 if (it->area != TEXT_AREA)
19355 {
19356 row->ascent = max (row->ascent, it->max_ascent);
19357 row->height = max (row->height, it->max_ascent + it->max_descent);
19358 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19359 row->phys_height = max (row->phys_height,
19360 it->max_phys_ascent + it->max_phys_descent);
19361 row->extra_line_spacing = max (row->extra_line_spacing,
19362 it->max_extra_line_spacing);
19363 set_iterator_to_next (it, 1);
19364 continue;
19365 }
19366
19367 /* Does the display element fit on the line? If we truncate
19368 lines, we should draw past the right edge of the window. If
19369 we don't truncate, we want to stop so that we can display the
19370 continuation glyph before the right margin. If lines are
19371 continued, there are two possible strategies for characters
19372 resulting in more than 1 glyph (e.g. tabs): Display as many
19373 glyphs as possible in this line and leave the rest for the
19374 continuation line, or display the whole element in the next
19375 line. Original redisplay did the former, so we do it also. */
19376 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19377 hpos_before = it->hpos;
19378 x_before = x;
19379
19380 if (/* Not a newline. */
19381 nglyphs > 0
19382 /* Glyphs produced fit entirely in the line. */
19383 && it->current_x < it->last_visible_x)
19384 {
19385 it->hpos += nglyphs;
19386 row->ascent = max (row->ascent, it->max_ascent);
19387 row->height = max (row->height, it->max_ascent + it->max_descent);
19388 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19389 row->phys_height = max (row->phys_height,
19390 it->max_phys_ascent + it->max_phys_descent);
19391 row->extra_line_spacing = max (row->extra_line_spacing,
19392 it->max_extra_line_spacing);
19393 if (it->current_x - it->pixel_width < it->first_visible_x)
19394 row->x = x - it->first_visible_x;
19395 /* Record the maximum and minimum buffer positions seen so
19396 far in glyphs that will be displayed by this row. */
19397 if (it->bidi_p)
19398 RECORD_MAX_MIN_POS (it);
19399 }
19400 else
19401 {
19402 int i, new_x;
19403 struct glyph *glyph;
19404
19405 for (i = 0; i < nglyphs; ++i, x = new_x)
19406 {
19407 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19408 new_x = x + glyph->pixel_width;
19409
19410 if (/* Lines are continued. */
19411 it->line_wrap != TRUNCATE
19412 && (/* Glyph doesn't fit on the line. */
19413 new_x > it->last_visible_x
19414 /* Or it fits exactly on a window system frame. */
19415 || (new_x == it->last_visible_x
19416 && FRAME_WINDOW_P (it->f)
19417 && (row->reversed_p
19418 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19419 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19420 {
19421 /* End of a continued line. */
19422
19423 if (it->hpos == 0
19424 || (new_x == it->last_visible_x
19425 && FRAME_WINDOW_P (it->f)
19426 && (row->reversed_p
19427 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19428 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19429 {
19430 /* Current glyph is the only one on the line or
19431 fits exactly on the line. We must continue
19432 the line because we can't draw the cursor
19433 after the glyph. */
19434 row->continued_p = 1;
19435 it->current_x = new_x;
19436 it->continuation_lines_width += new_x;
19437 ++it->hpos;
19438 if (i == nglyphs - 1)
19439 {
19440 /* If line-wrap is on, check if a previous
19441 wrap point was found. */
19442 if (wrap_row_used > 0
19443 /* Even if there is a previous wrap
19444 point, continue the line here as
19445 usual, if (i) the previous character
19446 was a space or tab AND (ii) the
19447 current character is not. */
19448 && (!may_wrap
19449 || IT_DISPLAYING_WHITESPACE (it)))
19450 goto back_to_wrap;
19451
19452 /* Record the maximum and minimum buffer
19453 positions seen so far in glyphs that will be
19454 displayed by this row. */
19455 if (it->bidi_p)
19456 RECORD_MAX_MIN_POS (it);
19457 set_iterator_to_next (it, 1);
19458 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19459 {
19460 if (!get_next_display_element (it))
19461 {
19462 row->exact_window_width_line_p = 1;
19463 it->continuation_lines_width = 0;
19464 row->continued_p = 0;
19465 row->ends_at_zv_p = 1;
19466 }
19467 else if (ITERATOR_AT_END_OF_LINE_P (it))
19468 {
19469 row->continued_p = 0;
19470 row->exact_window_width_line_p = 1;
19471 }
19472 }
19473 }
19474 else if (it->bidi_p)
19475 RECORD_MAX_MIN_POS (it);
19476 }
19477 else if (CHAR_GLYPH_PADDING_P (*glyph)
19478 && !FRAME_WINDOW_P (it->f))
19479 {
19480 /* A padding glyph that doesn't fit on this line.
19481 This means the whole character doesn't fit
19482 on the line. */
19483 if (row->reversed_p)
19484 unproduce_glyphs (it, row->used[TEXT_AREA]
19485 - n_glyphs_before);
19486 row->used[TEXT_AREA] = n_glyphs_before;
19487
19488 /* Fill the rest of the row with continuation
19489 glyphs like in 20.x. */
19490 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19491 < row->glyphs[1 + TEXT_AREA])
19492 produce_special_glyphs (it, IT_CONTINUATION);
19493
19494 row->continued_p = 1;
19495 it->current_x = x_before;
19496 it->continuation_lines_width += x_before;
19497
19498 /* Restore the height to what it was before the
19499 element not fitting on the line. */
19500 it->max_ascent = ascent;
19501 it->max_descent = descent;
19502 it->max_phys_ascent = phys_ascent;
19503 it->max_phys_descent = phys_descent;
19504 }
19505 else if (wrap_row_used > 0)
19506 {
19507 back_to_wrap:
19508 if (row->reversed_p)
19509 unproduce_glyphs (it,
19510 row->used[TEXT_AREA] - wrap_row_used);
19511 RESTORE_IT (it, &wrap_it, wrap_data);
19512 it->continuation_lines_width += wrap_x;
19513 row->used[TEXT_AREA] = wrap_row_used;
19514 row->ascent = wrap_row_ascent;
19515 row->height = wrap_row_height;
19516 row->phys_ascent = wrap_row_phys_ascent;
19517 row->phys_height = wrap_row_phys_height;
19518 row->extra_line_spacing = wrap_row_extra_line_spacing;
19519 min_pos = wrap_row_min_pos;
19520 min_bpos = wrap_row_min_bpos;
19521 max_pos = wrap_row_max_pos;
19522 max_bpos = wrap_row_max_bpos;
19523 row->continued_p = 1;
19524 row->ends_at_zv_p = 0;
19525 row->exact_window_width_line_p = 0;
19526 it->continuation_lines_width += x;
19527
19528 /* Make sure that a non-default face is extended
19529 up to the right margin of the window. */
19530 extend_face_to_end_of_line (it);
19531 }
19532 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19533 {
19534 /* A TAB that extends past the right edge of the
19535 window. This produces a single glyph on
19536 window system frames. We leave the glyph in
19537 this row and let it fill the row, but don't
19538 consume the TAB. */
19539 if ((row->reversed_p
19540 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19541 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19542 produce_special_glyphs (it, IT_CONTINUATION);
19543 it->continuation_lines_width += it->last_visible_x;
19544 row->ends_in_middle_of_char_p = 1;
19545 row->continued_p = 1;
19546 glyph->pixel_width = it->last_visible_x - x;
19547 it->starts_in_middle_of_char_p = 1;
19548 }
19549 else
19550 {
19551 /* Something other than a TAB that draws past
19552 the right edge of the window. Restore
19553 positions to values before the element. */
19554 if (row->reversed_p)
19555 unproduce_glyphs (it, row->used[TEXT_AREA]
19556 - (n_glyphs_before + i));
19557 row->used[TEXT_AREA] = n_glyphs_before + i;
19558
19559 /* Display continuation glyphs. */
19560 it->current_x = x_before;
19561 it->continuation_lines_width += x;
19562 if (!FRAME_WINDOW_P (it->f)
19563 || (row->reversed_p
19564 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19565 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19566 produce_special_glyphs (it, IT_CONTINUATION);
19567 row->continued_p = 1;
19568
19569 extend_face_to_end_of_line (it);
19570
19571 if (nglyphs > 1 && i > 0)
19572 {
19573 row->ends_in_middle_of_char_p = 1;
19574 it->starts_in_middle_of_char_p = 1;
19575 }
19576
19577 /* Restore the height to what it was before the
19578 element not fitting on the line. */
19579 it->max_ascent = ascent;
19580 it->max_descent = descent;
19581 it->max_phys_ascent = phys_ascent;
19582 it->max_phys_descent = phys_descent;
19583 }
19584
19585 break;
19586 }
19587 else if (new_x > it->first_visible_x)
19588 {
19589 /* Increment number of glyphs actually displayed. */
19590 ++it->hpos;
19591
19592 /* Record the maximum and minimum buffer positions
19593 seen so far in glyphs that will be displayed by
19594 this row. */
19595 if (it->bidi_p)
19596 RECORD_MAX_MIN_POS (it);
19597
19598 if (x < it->first_visible_x)
19599 /* Glyph is partially visible, i.e. row starts at
19600 negative X position. */
19601 row->x = x - it->first_visible_x;
19602 }
19603 else
19604 {
19605 /* Glyph is completely off the left margin of the
19606 window. This should not happen because of the
19607 move_it_in_display_line at the start of this
19608 function, unless the text display area of the
19609 window is empty. */
19610 eassert (it->first_visible_x <= it->last_visible_x);
19611 }
19612 }
19613 /* Even if this display element produced no glyphs at all,
19614 we want to record its position. */
19615 if (it->bidi_p && nglyphs == 0)
19616 RECORD_MAX_MIN_POS (it);
19617
19618 row->ascent = max (row->ascent, it->max_ascent);
19619 row->height = max (row->height, it->max_ascent + it->max_descent);
19620 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19621 row->phys_height = max (row->phys_height,
19622 it->max_phys_ascent + it->max_phys_descent);
19623 row->extra_line_spacing = max (row->extra_line_spacing,
19624 it->max_extra_line_spacing);
19625
19626 /* End of this display line if row is continued. */
19627 if (row->continued_p || row->ends_at_zv_p)
19628 break;
19629 }
19630
19631 at_end_of_line:
19632 /* Is this a line end? If yes, we're also done, after making
19633 sure that a non-default face is extended up to the right
19634 margin of the window. */
19635 if (ITERATOR_AT_END_OF_LINE_P (it))
19636 {
19637 int used_before = row->used[TEXT_AREA];
19638
19639 row->ends_in_newline_from_string_p = STRINGP (it->object);
19640
19641 /* Add a space at the end of the line that is used to
19642 display the cursor there. */
19643 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19644 append_space_for_newline (it, 0);
19645
19646 /* Extend the face to the end of the line. */
19647 extend_face_to_end_of_line (it);
19648
19649 /* Make sure we have the position. */
19650 if (used_before == 0)
19651 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19652
19653 /* Record the position of the newline, for use in
19654 find_row_edges. */
19655 it->eol_pos = it->current.pos;
19656
19657 /* Consume the line end. This skips over invisible lines. */
19658 set_iterator_to_next (it, 1);
19659 it->continuation_lines_width = 0;
19660 break;
19661 }
19662
19663 /* Proceed with next display element. Note that this skips
19664 over lines invisible because of selective display. */
19665 set_iterator_to_next (it, 1);
19666
19667 /* If we truncate lines, we are done when the last displayed
19668 glyphs reach past the right margin of the window. */
19669 if (it->line_wrap == TRUNCATE
19670 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19671 ? (it->current_x >= it->last_visible_x)
19672 : (it->current_x > it->last_visible_x)))
19673 {
19674 /* Maybe add truncation glyphs. */
19675 if (!FRAME_WINDOW_P (it->f)
19676 || (row->reversed_p
19677 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19678 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19679 {
19680 int i, n;
19681
19682 if (!row->reversed_p)
19683 {
19684 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19685 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19686 break;
19687 }
19688 else
19689 {
19690 for (i = 0; i < row->used[TEXT_AREA]; i++)
19691 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19692 break;
19693 /* Remove any padding glyphs at the front of ROW, to
19694 make room for the truncation glyphs we will be
19695 adding below. The loop below always inserts at
19696 least one truncation glyph, so also remove the
19697 last glyph added to ROW. */
19698 unproduce_glyphs (it, i + 1);
19699 /* Adjust i for the loop below. */
19700 i = row->used[TEXT_AREA] - (i + 1);
19701 }
19702
19703 it->current_x = x_before;
19704 if (!FRAME_WINDOW_P (it->f))
19705 {
19706 for (n = row->used[TEXT_AREA]; i < n; ++i)
19707 {
19708 row->used[TEXT_AREA] = i;
19709 produce_special_glyphs (it, IT_TRUNCATION);
19710 }
19711 }
19712 else
19713 {
19714 row->used[TEXT_AREA] = i;
19715 produce_special_glyphs (it, IT_TRUNCATION);
19716 }
19717 }
19718 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19719 {
19720 /* Don't truncate if we can overflow newline into fringe. */
19721 if (!get_next_display_element (it))
19722 {
19723 it->continuation_lines_width = 0;
19724 row->ends_at_zv_p = 1;
19725 row->exact_window_width_line_p = 1;
19726 break;
19727 }
19728 if (ITERATOR_AT_END_OF_LINE_P (it))
19729 {
19730 row->exact_window_width_line_p = 1;
19731 goto at_end_of_line;
19732 }
19733 it->current_x = x_before;
19734 }
19735
19736 row->truncated_on_right_p = 1;
19737 it->continuation_lines_width = 0;
19738 reseat_at_next_visible_line_start (it, 0);
19739 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19740 it->hpos = hpos_before;
19741 break;
19742 }
19743 }
19744
19745 if (wrap_data)
19746 bidi_unshelve_cache (wrap_data, 1);
19747
19748 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19749 at the left window margin. */
19750 if (it->first_visible_x
19751 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19752 {
19753 if (!FRAME_WINDOW_P (it->f)
19754 || (row->reversed_p
19755 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19756 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19757 insert_left_trunc_glyphs (it);
19758 row->truncated_on_left_p = 1;
19759 }
19760
19761 /* Remember the position at which this line ends.
19762
19763 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19764 cannot be before the call to find_row_edges below, since that is
19765 where these positions are determined. */
19766 row->end = it->current;
19767 if (!it->bidi_p)
19768 {
19769 row->minpos = row->start.pos;
19770 row->maxpos = row->end.pos;
19771 }
19772 else
19773 {
19774 /* ROW->minpos and ROW->maxpos must be the smallest and
19775 `1 + the largest' buffer positions in ROW. But if ROW was
19776 bidi-reordered, these two positions can be anywhere in the
19777 row, so we must determine them now. */
19778 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19779 }
19780
19781 /* If the start of this line is the overlay arrow-position, then
19782 mark this glyph row as the one containing the overlay arrow.
19783 This is clearly a mess with variable size fonts. It would be
19784 better to let it be displayed like cursors under X. */
19785 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19786 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19787 !NILP (overlay_arrow_string)))
19788 {
19789 /* Overlay arrow in window redisplay is a fringe bitmap. */
19790 if (STRINGP (overlay_arrow_string))
19791 {
19792 struct glyph_row *arrow_row
19793 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19794 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19795 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19796 struct glyph *p = row->glyphs[TEXT_AREA];
19797 struct glyph *p2, *end;
19798
19799 /* Copy the arrow glyphs. */
19800 while (glyph < arrow_end)
19801 *p++ = *glyph++;
19802
19803 /* Throw away padding glyphs. */
19804 p2 = p;
19805 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19806 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19807 ++p2;
19808 if (p2 > p)
19809 {
19810 while (p2 < end)
19811 *p++ = *p2++;
19812 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19813 }
19814 }
19815 else
19816 {
19817 eassert (INTEGERP (overlay_arrow_string));
19818 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19819 }
19820 overlay_arrow_seen = 1;
19821 }
19822
19823 /* Highlight trailing whitespace. */
19824 if (!NILP (Vshow_trailing_whitespace))
19825 highlight_trailing_whitespace (it->f, it->glyph_row);
19826
19827 /* Compute pixel dimensions of this line. */
19828 compute_line_metrics (it);
19829
19830 /* Implementation note: No changes in the glyphs of ROW or in their
19831 faces can be done past this point, because compute_line_metrics
19832 computes ROW's hash value and stores it within the glyph_row
19833 structure. */
19834
19835 /* Record whether this row ends inside an ellipsis. */
19836 row->ends_in_ellipsis_p
19837 = (it->method == GET_FROM_DISPLAY_VECTOR
19838 && it->ellipsis_p);
19839
19840 /* Save fringe bitmaps in this row. */
19841 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19842 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19843 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19844 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19845
19846 it->left_user_fringe_bitmap = 0;
19847 it->left_user_fringe_face_id = 0;
19848 it->right_user_fringe_bitmap = 0;
19849 it->right_user_fringe_face_id = 0;
19850
19851 /* Maybe set the cursor. */
19852 cvpos = it->w->cursor.vpos;
19853 if ((cvpos < 0
19854 /* In bidi-reordered rows, keep checking for proper cursor
19855 position even if one has been found already, because buffer
19856 positions in such rows change non-linearly with ROW->VPOS,
19857 when a line is continued. One exception: when we are at ZV,
19858 display cursor on the first suitable glyph row, since all
19859 the empty rows after that also have their position set to ZV. */
19860 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19861 lines' rows is implemented for bidi-reordered rows. */
19862 || (it->bidi_p
19863 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19864 && PT >= MATRIX_ROW_START_CHARPOS (row)
19865 && PT <= MATRIX_ROW_END_CHARPOS (row)
19866 && cursor_row_p (row))
19867 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19868
19869 /* Prepare for the next line. This line starts horizontally at (X
19870 HPOS) = (0 0). Vertical positions are incremented. As a
19871 convenience for the caller, IT->glyph_row is set to the next
19872 row to be used. */
19873 it->current_x = it->hpos = 0;
19874 it->current_y += row->height;
19875 SET_TEXT_POS (it->eol_pos, 0, 0);
19876 ++it->vpos;
19877 ++it->glyph_row;
19878 /* The next row should by default use the same value of the
19879 reversed_p flag as this one. set_iterator_to_next decides when
19880 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19881 the flag accordingly. */
19882 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19883 it->glyph_row->reversed_p = row->reversed_p;
19884 it->start = row->end;
19885 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19886
19887 #undef RECORD_MAX_MIN_POS
19888 }
19889
19890 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19891 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19892 doc: /* Return paragraph direction at point in BUFFER.
19893 Value is either `left-to-right' or `right-to-left'.
19894 If BUFFER is omitted or nil, it defaults to the current buffer.
19895
19896 Paragraph direction determines how the text in the paragraph is displayed.
19897 In left-to-right paragraphs, text begins at the left margin of the window
19898 and the reading direction is generally left to right. In right-to-left
19899 paragraphs, text begins at the right margin and is read from right to left.
19900
19901 See also `bidi-paragraph-direction'. */)
19902 (Lisp_Object buffer)
19903 {
19904 struct buffer *buf = current_buffer;
19905 struct buffer *old = buf;
19906
19907 if (! NILP (buffer))
19908 {
19909 CHECK_BUFFER (buffer);
19910 buf = XBUFFER (buffer);
19911 }
19912
19913 if (NILP (BVAR (buf, bidi_display_reordering))
19914 || NILP (BVAR (buf, enable_multibyte_characters))
19915 /* When we are loading loadup.el, the character property tables
19916 needed for bidi iteration are not yet available. */
19917 || !NILP (Vpurify_flag))
19918 return Qleft_to_right;
19919 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19920 return BVAR (buf, bidi_paragraph_direction);
19921 else
19922 {
19923 /* Determine the direction from buffer text. We could try to
19924 use current_matrix if it is up to date, but this seems fast
19925 enough as it is. */
19926 struct bidi_it itb;
19927 ptrdiff_t pos = BUF_PT (buf);
19928 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19929 int c;
19930 void *itb_data = bidi_shelve_cache ();
19931
19932 set_buffer_temp (buf);
19933 /* bidi_paragraph_init finds the base direction of the paragraph
19934 by searching forward from paragraph start. We need the base
19935 direction of the current or _previous_ paragraph, so we need
19936 to make sure we are within that paragraph. To that end, find
19937 the previous non-empty line. */
19938 if (pos >= ZV && pos > BEGV)
19939 DEC_BOTH (pos, bytepos);
19940 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19941 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19942 {
19943 while ((c = FETCH_BYTE (bytepos)) == '\n'
19944 || c == ' ' || c == '\t' || c == '\f')
19945 {
19946 if (bytepos <= BEGV_BYTE)
19947 break;
19948 bytepos--;
19949 pos--;
19950 }
19951 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19952 bytepos--;
19953 }
19954 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19955 itb.paragraph_dir = NEUTRAL_DIR;
19956 itb.string.s = NULL;
19957 itb.string.lstring = Qnil;
19958 itb.string.bufpos = 0;
19959 itb.string.unibyte = 0;
19960 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19961 bidi_unshelve_cache (itb_data, 0);
19962 set_buffer_temp (old);
19963 switch (itb.paragraph_dir)
19964 {
19965 case L2R:
19966 return Qleft_to_right;
19967 break;
19968 case R2L:
19969 return Qright_to_left;
19970 break;
19971 default:
19972 emacs_abort ();
19973 }
19974 }
19975 }
19976
19977
19978 \f
19979 /***********************************************************************
19980 Menu Bar
19981 ***********************************************************************/
19982
19983 /* Redisplay the menu bar in the frame for window W.
19984
19985 The menu bar of X frames that don't have X toolkit support is
19986 displayed in a special window W->frame->menu_bar_window.
19987
19988 The menu bar of terminal frames is treated specially as far as
19989 glyph matrices are concerned. Menu bar lines are not part of
19990 windows, so the update is done directly on the frame matrix rows
19991 for the menu bar. */
19992
19993 static void
19994 display_menu_bar (struct window *w)
19995 {
19996 struct frame *f = XFRAME (WINDOW_FRAME (w));
19997 struct it it;
19998 Lisp_Object items;
19999 int i;
20000
20001 /* Don't do all this for graphical frames. */
20002 #ifdef HAVE_NTGUI
20003 if (FRAME_W32_P (f))
20004 return;
20005 #endif
20006 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20007 if (FRAME_X_P (f))
20008 return;
20009 #endif
20010
20011 #ifdef HAVE_NS
20012 if (FRAME_NS_P (f))
20013 return;
20014 #endif /* HAVE_NS */
20015
20016 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20017 eassert (!FRAME_WINDOW_P (f));
20018 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20019 it.first_visible_x = 0;
20020 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20021 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20022 if (FRAME_WINDOW_P (f))
20023 {
20024 /* Menu bar lines are displayed in the desired matrix of the
20025 dummy window menu_bar_window. */
20026 struct window *menu_w;
20027 menu_w = XWINDOW (f->menu_bar_window);
20028 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20029 MENU_FACE_ID);
20030 it.first_visible_x = 0;
20031 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20032 }
20033 else
20034 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20035 {
20036 /* This is a TTY frame, i.e. character hpos/vpos are used as
20037 pixel x/y. */
20038 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20039 MENU_FACE_ID);
20040 it.first_visible_x = 0;
20041 it.last_visible_x = FRAME_COLS (f);
20042 }
20043
20044 /* FIXME: This should be controlled by a user option. See the
20045 comments in redisplay_tool_bar and display_mode_line about
20046 this. */
20047 it.paragraph_embedding = L2R;
20048
20049 /* Clear all rows of the menu bar. */
20050 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20051 {
20052 struct glyph_row *row = it.glyph_row + i;
20053 clear_glyph_row (row);
20054 row->enabled_p = 1;
20055 row->full_width_p = 1;
20056 }
20057
20058 /* Display all items of the menu bar. */
20059 items = FRAME_MENU_BAR_ITEMS (it.f);
20060 for (i = 0; i < ASIZE (items); i += 4)
20061 {
20062 Lisp_Object string;
20063
20064 /* Stop at nil string. */
20065 string = AREF (items, i + 1);
20066 if (NILP (string))
20067 break;
20068
20069 /* Remember where item was displayed. */
20070 ASET (items, i + 3, make_number (it.hpos));
20071
20072 /* Display the item, pad with one space. */
20073 if (it.current_x < it.last_visible_x)
20074 display_string (NULL, string, Qnil, 0, 0, &it,
20075 SCHARS (string) + 1, 0, 0, -1);
20076 }
20077
20078 /* Fill out the line with spaces. */
20079 if (it.current_x < it.last_visible_x)
20080 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20081
20082 /* Compute the total height of the lines. */
20083 compute_line_metrics (&it);
20084 }
20085
20086
20087 \f
20088 /***********************************************************************
20089 Mode Line
20090 ***********************************************************************/
20091
20092 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20093 FORCE is non-zero, redisplay mode lines unconditionally.
20094 Otherwise, redisplay only mode lines that are garbaged. Value is
20095 the number of windows whose mode lines were redisplayed. */
20096
20097 static int
20098 redisplay_mode_lines (Lisp_Object window, int force)
20099 {
20100 int nwindows = 0;
20101
20102 while (!NILP (window))
20103 {
20104 struct window *w = XWINDOW (window);
20105
20106 if (WINDOWP (w->contents))
20107 nwindows += redisplay_mode_lines (w->contents, force);
20108 else if (force
20109 || FRAME_GARBAGED_P (XFRAME (w->frame))
20110 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20111 {
20112 struct text_pos lpoint;
20113 struct buffer *old = current_buffer;
20114
20115 /* Set the window's buffer for the mode line display. */
20116 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20117 set_buffer_internal_1 (XBUFFER (w->contents));
20118
20119 /* Point refers normally to the selected window. For any
20120 other window, set up appropriate value. */
20121 if (!EQ (window, selected_window))
20122 {
20123 struct text_pos pt;
20124
20125 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20126 if (CHARPOS (pt) < BEGV)
20127 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20128 else if (CHARPOS (pt) > (ZV - 1))
20129 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20130 else
20131 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20132 }
20133
20134 /* Display mode lines. */
20135 clear_glyph_matrix (w->desired_matrix);
20136 if (display_mode_lines (w))
20137 {
20138 ++nwindows;
20139 w->must_be_updated_p = 1;
20140 }
20141
20142 /* Restore old settings. */
20143 set_buffer_internal_1 (old);
20144 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20145 }
20146
20147 window = w->next;
20148 }
20149
20150 return nwindows;
20151 }
20152
20153
20154 /* Display the mode and/or header line of window W. Value is the
20155 sum number of mode lines and header lines displayed. */
20156
20157 static int
20158 display_mode_lines (struct window *w)
20159 {
20160 Lisp_Object old_selected_window = selected_window;
20161 Lisp_Object old_selected_frame = selected_frame;
20162 Lisp_Object new_frame = w->frame;
20163 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20164 int n = 0;
20165
20166 selected_frame = new_frame;
20167 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20168 or window's point, then we'd need select_window_1 here as well. */
20169 XSETWINDOW (selected_window, w);
20170 XFRAME (new_frame)->selected_window = selected_window;
20171
20172 /* These will be set while the mode line specs are processed. */
20173 line_number_displayed = 0;
20174 w->column_number_displayed = -1;
20175
20176 if (WINDOW_WANTS_MODELINE_P (w))
20177 {
20178 struct window *sel_w = XWINDOW (old_selected_window);
20179
20180 /* Select mode line face based on the real selected window. */
20181 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20182 BVAR (current_buffer, mode_line_format));
20183 ++n;
20184 }
20185
20186 if (WINDOW_WANTS_HEADER_LINE_P (w))
20187 {
20188 display_mode_line (w, HEADER_LINE_FACE_ID,
20189 BVAR (current_buffer, header_line_format));
20190 ++n;
20191 }
20192
20193 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20194 selected_frame = old_selected_frame;
20195 selected_window = old_selected_window;
20196 return n;
20197 }
20198
20199
20200 /* Display mode or header line of window W. FACE_ID specifies which
20201 line to display; it is either MODE_LINE_FACE_ID or
20202 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20203 display. Value is the pixel height of the mode/header line
20204 displayed. */
20205
20206 static int
20207 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20208 {
20209 struct it it;
20210 struct face *face;
20211 ptrdiff_t count = SPECPDL_INDEX ();
20212
20213 init_iterator (&it, w, -1, -1, NULL, face_id);
20214 /* Don't extend on a previously drawn mode-line.
20215 This may happen if called from pos_visible_p. */
20216 it.glyph_row->enabled_p = 0;
20217 prepare_desired_row (it.glyph_row);
20218
20219 it.glyph_row->mode_line_p = 1;
20220
20221 /* FIXME: This should be controlled by a user option. But
20222 supporting such an option is not trivial, since the mode line is
20223 made up of many separate strings. */
20224 it.paragraph_embedding = L2R;
20225
20226 record_unwind_protect (unwind_format_mode_line,
20227 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20228
20229 mode_line_target = MODE_LINE_DISPLAY;
20230
20231 /* Temporarily make frame's keyboard the current kboard so that
20232 kboard-local variables in the mode_line_format will get the right
20233 values. */
20234 push_kboard (FRAME_KBOARD (it.f));
20235 record_unwind_save_match_data ();
20236 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20237 pop_kboard ();
20238
20239 unbind_to (count, Qnil);
20240
20241 /* Fill up with spaces. */
20242 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20243
20244 compute_line_metrics (&it);
20245 it.glyph_row->full_width_p = 1;
20246 it.glyph_row->continued_p = 0;
20247 it.glyph_row->truncated_on_left_p = 0;
20248 it.glyph_row->truncated_on_right_p = 0;
20249
20250 /* Make a 3D mode-line have a shadow at its right end. */
20251 face = FACE_FROM_ID (it.f, face_id);
20252 extend_face_to_end_of_line (&it);
20253 if (face->box != FACE_NO_BOX)
20254 {
20255 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20256 + it.glyph_row->used[TEXT_AREA] - 1);
20257 last->right_box_line_p = 1;
20258 }
20259
20260 return it.glyph_row->height;
20261 }
20262
20263 /* Move element ELT in LIST to the front of LIST.
20264 Return the updated list. */
20265
20266 static Lisp_Object
20267 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20268 {
20269 register Lisp_Object tail, prev;
20270 register Lisp_Object tem;
20271
20272 tail = list;
20273 prev = Qnil;
20274 while (CONSP (tail))
20275 {
20276 tem = XCAR (tail);
20277
20278 if (EQ (elt, tem))
20279 {
20280 /* Splice out the link TAIL. */
20281 if (NILP (prev))
20282 list = XCDR (tail);
20283 else
20284 Fsetcdr (prev, XCDR (tail));
20285
20286 /* Now make it the first. */
20287 Fsetcdr (tail, list);
20288 return tail;
20289 }
20290 else
20291 prev = tail;
20292 tail = XCDR (tail);
20293 QUIT;
20294 }
20295
20296 /* Not found--return unchanged LIST. */
20297 return list;
20298 }
20299
20300 /* Contribute ELT to the mode line for window IT->w. How it
20301 translates into text depends on its data type.
20302
20303 IT describes the display environment in which we display, as usual.
20304
20305 DEPTH is the depth in recursion. It is used to prevent
20306 infinite recursion here.
20307
20308 FIELD_WIDTH is the number of characters the display of ELT should
20309 occupy in the mode line, and PRECISION is the maximum number of
20310 characters to display from ELT's representation. See
20311 display_string for details.
20312
20313 Returns the hpos of the end of the text generated by ELT.
20314
20315 PROPS is a property list to add to any string we encounter.
20316
20317 If RISKY is nonzero, remove (disregard) any properties in any string
20318 we encounter, and ignore :eval and :propertize.
20319
20320 The global variable `mode_line_target' determines whether the
20321 output is passed to `store_mode_line_noprop',
20322 `store_mode_line_string', or `display_string'. */
20323
20324 static int
20325 display_mode_element (struct it *it, int depth, int field_width, int precision,
20326 Lisp_Object elt, Lisp_Object props, int risky)
20327 {
20328 int n = 0, field, prec;
20329 int literal = 0;
20330
20331 tail_recurse:
20332 if (depth > 100)
20333 elt = build_string ("*too-deep*");
20334
20335 depth++;
20336
20337 switch (XTYPE (elt))
20338 {
20339 case Lisp_String:
20340 {
20341 /* A string: output it and check for %-constructs within it. */
20342 unsigned char c;
20343 ptrdiff_t offset = 0;
20344
20345 if (SCHARS (elt) > 0
20346 && (!NILP (props) || risky))
20347 {
20348 Lisp_Object oprops, aelt;
20349 oprops = Ftext_properties_at (make_number (0), elt);
20350
20351 /* If the starting string's properties are not what
20352 we want, translate the string. Also, if the string
20353 is risky, do that anyway. */
20354
20355 if (NILP (Fequal (props, oprops)) || risky)
20356 {
20357 /* If the starting string has properties,
20358 merge the specified ones onto the existing ones. */
20359 if (! NILP (oprops) && !risky)
20360 {
20361 Lisp_Object tem;
20362
20363 oprops = Fcopy_sequence (oprops);
20364 tem = props;
20365 while (CONSP (tem))
20366 {
20367 oprops = Fplist_put (oprops, XCAR (tem),
20368 XCAR (XCDR (tem)));
20369 tem = XCDR (XCDR (tem));
20370 }
20371 props = oprops;
20372 }
20373
20374 aelt = Fassoc (elt, mode_line_proptrans_alist);
20375 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20376 {
20377 /* AELT is what we want. Move it to the front
20378 without consing. */
20379 elt = XCAR (aelt);
20380 mode_line_proptrans_alist
20381 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20382 }
20383 else
20384 {
20385 Lisp_Object tem;
20386
20387 /* If AELT has the wrong props, it is useless.
20388 so get rid of it. */
20389 if (! NILP (aelt))
20390 mode_line_proptrans_alist
20391 = Fdelq (aelt, mode_line_proptrans_alist);
20392
20393 elt = Fcopy_sequence (elt);
20394 Fset_text_properties (make_number (0), Flength (elt),
20395 props, elt);
20396 /* Add this item to mode_line_proptrans_alist. */
20397 mode_line_proptrans_alist
20398 = Fcons (Fcons (elt, props),
20399 mode_line_proptrans_alist);
20400 /* Truncate mode_line_proptrans_alist
20401 to at most 50 elements. */
20402 tem = Fnthcdr (make_number (50),
20403 mode_line_proptrans_alist);
20404 if (! NILP (tem))
20405 XSETCDR (tem, Qnil);
20406 }
20407 }
20408 }
20409
20410 offset = 0;
20411
20412 if (literal)
20413 {
20414 prec = precision - n;
20415 switch (mode_line_target)
20416 {
20417 case MODE_LINE_NOPROP:
20418 case MODE_LINE_TITLE:
20419 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20420 break;
20421 case MODE_LINE_STRING:
20422 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20423 break;
20424 case MODE_LINE_DISPLAY:
20425 n += display_string (NULL, elt, Qnil, 0, 0, it,
20426 0, prec, 0, STRING_MULTIBYTE (elt));
20427 break;
20428 }
20429
20430 break;
20431 }
20432
20433 /* Handle the non-literal case. */
20434
20435 while ((precision <= 0 || n < precision)
20436 && SREF (elt, offset) != 0
20437 && (mode_line_target != MODE_LINE_DISPLAY
20438 || it->current_x < it->last_visible_x))
20439 {
20440 ptrdiff_t last_offset = offset;
20441
20442 /* Advance to end of string or next format specifier. */
20443 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20444 ;
20445
20446 if (offset - 1 != last_offset)
20447 {
20448 ptrdiff_t nchars, nbytes;
20449
20450 /* Output to end of string or up to '%'. Field width
20451 is length of string. Don't output more than
20452 PRECISION allows us. */
20453 offset--;
20454
20455 prec = c_string_width (SDATA (elt) + last_offset,
20456 offset - last_offset, precision - n,
20457 &nchars, &nbytes);
20458
20459 switch (mode_line_target)
20460 {
20461 case MODE_LINE_NOPROP:
20462 case MODE_LINE_TITLE:
20463 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20464 break;
20465 case MODE_LINE_STRING:
20466 {
20467 ptrdiff_t bytepos = last_offset;
20468 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20469 ptrdiff_t endpos = (precision <= 0
20470 ? string_byte_to_char (elt, offset)
20471 : charpos + nchars);
20472
20473 n += store_mode_line_string (NULL,
20474 Fsubstring (elt, make_number (charpos),
20475 make_number (endpos)),
20476 0, 0, 0, Qnil);
20477 }
20478 break;
20479 case MODE_LINE_DISPLAY:
20480 {
20481 ptrdiff_t bytepos = last_offset;
20482 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20483
20484 if (precision <= 0)
20485 nchars = string_byte_to_char (elt, offset) - charpos;
20486 n += display_string (NULL, elt, Qnil, 0, charpos,
20487 it, 0, nchars, 0,
20488 STRING_MULTIBYTE (elt));
20489 }
20490 break;
20491 }
20492 }
20493 else /* c == '%' */
20494 {
20495 ptrdiff_t percent_position = offset;
20496
20497 /* Get the specified minimum width. Zero means
20498 don't pad. */
20499 field = 0;
20500 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20501 field = field * 10 + c - '0';
20502
20503 /* Don't pad beyond the total padding allowed. */
20504 if (field_width - n > 0 && field > field_width - n)
20505 field = field_width - n;
20506
20507 /* Note that either PRECISION <= 0 or N < PRECISION. */
20508 prec = precision - n;
20509
20510 if (c == 'M')
20511 n += display_mode_element (it, depth, field, prec,
20512 Vglobal_mode_string, props,
20513 risky);
20514 else if (c != 0)
20515 {
20516 bool multibyte;
20517 ptrdiff_t bytepos, charpos;
20518 const char *spec;
20519 Lisp_Object string;
20520
20521 bytepos = percent_position;
20522 charpos = (STRING_MULTIBYTE (elt)
20523 ? string_byte_to_char (elt, bytepos)
20524 : bytepos);
20525 spec = decode_mode_spec (it->w, c, field, &string);
20526 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20527
20528 switch (mode_line_target)
20529 {
20530 case MODE_LINE_NOPROP:
20531 case MODE_LINE_TITLE:
20532 n += store_mode_line_noprop (spec, field, prec);
20533 break;
20534 case MODE_LINE_STRING:
20535 {
20536 Lisp_Object tem = build_string (spec);
20537 props = Ftext_properties_at (make_number (charpos), elt);
20538 /* Should only keep face property in props */
20539 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20540 }
20541 break;
20542 case MODE_LINE_DISPLAY:
20543 {
20544 int nglyphs_before, nwritten;
20545
20546 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20547 nwritten = display_string (spec, string, elt,
20548 charpos, 0, it,
20549 field, prec, 0,
20550 multibyte);
20551
20552 /* Assign to the glyphs written above the
20553 string where the `%x' came from, position
20554 of the `%'. */
20555 if (nwritten > 0)
20556 {
20557 struct glyph *glyph
20558 = (it->glyph_row->glyphs[TEXT_AREA]
20559 + nglyphs_before);
20560 int i;
20561
20562 for (i = 0; i < nwritten; ++i)
20563 {
20564 glyph[i].object = elt;
20565 glyph[i].charpos = charpos;
20566 }
20567
20568 n += nwritten;
20569 }
20570 }
20571 break;
20572 }
20573 }
20574 else /* c == 0 */
20575 break;
20576 }
20577 }
20578 }
20579 break;
20580
20581 case Lisp_Symbol:
20582 /* A symbol: process the value of the symbol recursively
20583 as if it appeared here directly. Avoid error if symbol void.
20584 Special case: if value of symbol is a string, output the string
20585 literally. */
20586 {
20587 register Lisp_Object tem;
20588
20589 /* If the variable is not marked as risky to set
20590 then its contents are risky to use. */
20591 if (NILP (Fget (elt, Qrisky_local_variable)))
20592 risky = 1;
20593
20594 tem = Fboundp (elt);
20595 if (!NILP (tem))
20596 {
20597 tem = Fsymbol_value (elt);
20598 /* If value is a string, output that string literally:
20599 don't check for % within it. */
20600 if (STRINGP (tem))
20601 literal = 1;
20602
20603 if (!EQ (tem, elt))
20604 {
20605 /* Give up right away for nil or t. */
20606 elt = tem;
20607 goto tail_recurse;
20608 }
20609 }
20610 }
20611 break;
20612
20613 case Lisp_Cons:
20614 {
20615 register Lisp_Object car, tem;
20616
20617 /* A cons cell: five distinct cases.
20618 If first element is :eval or :propertize, do something special.
20619 If first element is a string or a cons, process all the elements
20620 and effectively concatenate them.
20621 If first element is a negative number, truncate displaying cdr to
20622 at most that many characters. If positive, pad (with spaces)
20623 to at least that many characters.
20624 If first element is a symbol, process the cadr or caddr recursively
20625 according to whether the symbol's value is non-nil or nil. */
20626 car = XCAR (elt);
20627 if (EQ (car, QCeval))
20628 {
20629 /* An element of the form (:eval FORM) means evaluate FORM
20630 and use the result as mode line elements. */
20631
20632 if (risky)
20633 break;
20634
20635 if (CONSP (XCDR (elt)))
20636 {
20637 Lisp_Object spec;
20638 spec = safe_eval (XCAR (XCDR (elt)));
20639 n += display_mode_element (it, depth, field_width - n,
20640 precision - n, spec, props,
20641 risky);
20642 }
20643 }
20644 else if (EQ (car, QCpropertize))
20645 {
20646 /* An element of the form (:propertize ELT PROPS...)
20647 means display ELT but applying properties PROPS. */
20648
20649 if (risky)
20650 break;
20651
20652 if (CONSP (XCDR (elt)))
20653 n += display_mode_element (it, depth, field_width - n,
20654 precision - n, XCAR (XCDR (elt)),
20655 XCDR (XCDR (elt)), risky);
20656 }
20657 else if (SYMBOLP (car))
20658 {
20659 tem = Fboundp (car);
20660 elt = XCDR (elt);
20661 if (!CONSP (elt))
20662 goto invalid;
20663 /* elt is now the cdr, and we know it is a cons cell.
20664 Use its car if CAR has a non-nil value. */
20665 if (!NILP (tem))
20666 {
20667 tem = Fsymbol_value (car);
20668 if (!NILP (tem))
20669 {
20670 elt = XCAR (elt);
20671 goto tail_recurse;
20672 }
20673 }
20674 /* Symbol's value is nil (or symbol is unbound)
20675 Get the cddr of the original list
20676 and if possible find the caddr and use that. */
20677 elt = XCDR (elt);
20678 if (NILP (elt))
20679 break;
20680 else if (!CONSP (elt))
20681 goto invalid;
20682 elt = XCAR (elt);
20683 goto tail_recurse;
20684 }
20685 else if (INTEGERP (car))
20686 {
20687 register int lim = XINT (car);
20688 elt = XCDR (elt);
20689 if (lim < 0)
20690 {
20691 /* Negative int means reduce maximum width. */
20692 if (precision <= 0)
20693 precision = -lim;
20694 else
20695 precision = min (precision, -lim);
20696 }
20697 else if (lim > 0)
20698 {
20699 /* Padding specified. Don't let it be more than
20700 current maximum. */
20701 if (precision > 0)
20702 lim = min (precision, lim);
20703
20704 /* If that's more padding than already wanted, queue it.
20705 But don't reduce padding already specified even if
20706 that is beyond the current truncation point. */
20707 field_width = max (lim, field_width);
20708 }
20709 goto tail_recurse;
20710 }
20711 else if (STRINGP (car) || CONSP (car))
20712 {
20713 Lisp_Object halftail = elt;
20714 int len = 0;
20715
20716 while (CONSP (elt)
20717 && (precision <= 0 || n < precision))
20718 {
20719 n += display_mode_element (it, depth,
20720 /* Do padding only after the last
20721 element in the list. */
20722 (! CONSP (XCDR (elt))
20723 ? field_width - n
20724 : 0),
20725 precision - n, XCAR (elt),
20726 props, risky);
20727 elt = XCDR (elt);
20728 len++;
20729 if ((len & 1) == 0)
20730 halftail = XCDR (halftail);
20731 /* Check for cycle. */
20732 if (EQ (halftail, elt))
20733 break;
20734 }
20735 }
20736 }
20737 break;
20738
20739 default:
20740 invalid:
20741 elt = build_string ("*invalid*");
20742 goto tail_recurse;
20743 }
20744
20745 /* Pad to FIELD_WIDTH. */
20746 if (field_width > 0 && n < field_width)
20747 {
20748 switch (mode_line_target)
20749 {
20750 case MODE_LINE_NOPROP:
20751 case MODE_LINE_TITLE:
20752 n += store_mode_line_noprop ("", field_width - n, 0);
20753 break;
20754 case MODE_LINE_STRING:
20755 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20756 break;
20757 case MODE_LINE_DISPLAY:
20758 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20759 0, 0, 0);
20760 break;
20761 }
20762 }
20763
20764 return n;
20765 }
20766
20767 /* Store a mode-line string element in mode_line_string_list.
20768
20769 If STRING is non-null, display that C string. Otherwise, the Lisp
20770 string LISP_STRING is displayed.
20771
20772 FIELD_WIDTH is the minimum number of output glyphs to produce.
20773 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20774 with spaces. FIELD_WIDTH <= 0 means don't pad.
20775
20776 PRECISION is the maximum number of characters to output from
20777 STRING. PRECISION <= 0 means don't truncate the string.
20778
20779 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20780 properties to the string.
20781
20782 PROPS are the properties to add to the string.
20783 The mode_line_string_face face property is always added to the string.
20784 */
20785
20786 static int
20787 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20788 int field_width, int precision, Lisp_Object props)
20789 {
20790 ptrdiff_t len;
20791 int n = 0;
20792
20793 if (string != NULL)
20794 {
20795 len = strlen (string);
20796 if (precision > 0 && len > precision)
20797 len = precision;
20798 lisp_string = make_string (string, len);
20799 if (NILP (props))
20800 props = mode_line_string_face_prop;
20801 else if (!NILP (mode_line_string_face))
20802 {
20803 Lisp_Object face = Fplist_get (props, Qface);
20804 props = Fcopy_sequence (props);
20805 if (NILP (face))
20806 face = mode_line_string_face;
20807 else
20808 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20809 props = Fplist_put (props, Qface, face);
20810 }
20811 Fadd_text_properties (make_number (0), make_number (len),
20812 props, lisp_string);
20813 }
20814 else
20815 {
20816 len = XFASTINT (Flength (lisp_string));
20817 if (precision > 0 && len > precision)
20818 {
20819 len = precision;
20820 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20821 precision = -1;
20822 }
20823 if (!NILP (mode_line_string_face))
20824 {
20825 Lisp_Object face;
20826 if (NILP (props))
20827 props = Ftext_properties_at (make_number (0), lisp_string);
20828 face = Fplist_get (props, Qface);
20829 if (NILP (face))
20830 face = mode_line_string_face;
20831 else
20832 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20833 props = Fcons (Qface, Fcons (face, Qnil));
20834 if (copy_string)
20835 lisp_string = Fcopy_sequence (lisp_string);
20836 }
20837 if (!NILP (props))
20838 Fadd_text_properties (make_number (0), make_number (len),
20839 props, lisp_string);
20840 }
20841
20842 if (len > 0)
20843 {
20844 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20845 n += len;
20846 }
20847
20848 if (field_width > len)
20849 {
20850 field_width -= len;
20851 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20852 if (!NILP (props))
20853 Fadd_text_properties (make_number (0), make_number (field_width),
20854 props, lisp_string);
20855 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20856 n += field_width;
20857 }
20858
20859 return n;
20860 }
20861
20862
20863 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20864 1, 4, 0,
20865 doc: /* Format a string out of a mode line format specification.
20866 First arg FORMAT specifies the mode line format (see `mode-line-format'
20867 for details) to use.
20868
20869 By default, the format is evaluated for the currently selected window.
20870
20871 Optional second arg FACE specifies the face property to put on all
20872 characters for which no face is specified. The value nil means the
20873 default face. The value t means whatever face the window's mode line
20874 currently uses (either `mode-line' or `mode-line-inactive',
20875 depending on whether the window is the selected window or not).
20876 An integer value means the value string has no text
20877 properties.
20878
20879 Optional third and fourth args WINDOW and BUFFER specify the window
20880 and buffer to use as the context for the formatting (defaults
20881 are the selected window and the WINDOW's buffer). */)
20882 (Lisp_Object format, Lisp_Object face,
20883 Lisp_Object window, Lisp_Object buffer)
20884 {
20885 struct it it;
20886 int len;
20887 struct window *w;
20888 struct buffer *old_buffer = NULL;
20889 int face_id;
20890 int no_props = INTEGERP (face);
20891 ptrdiff_t count = SPECPDL_INDEX ();
20892 Lisp_Object str;
20893 int string_start = 0;
20894
20895 w = decode_any_window (window);
20896 XSETWINDOW (window, w);
20897
20898 if (NILP (buffer))
20899 buffer = w->contents;
20900 CHECK_BUFFER (buffer);
20901
20902 /* Make formatting the modeline a non-op when noninteractive, otherwise
20903 there will be problems later caused by a partially initialized frame. */
20904 if (NILP (format) || noninteractive)
20905 return empty_unibyte_string;
20906
20907 if (no_props)
20908 face = Qnil;
20909
20910 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20911 : EQ (face, Qt) ? (EQ (window, selected_window)
20912 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20913 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20914 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20915 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20916 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20917 : DEFAULT_FACE_ID;
20918
20919 old_buffer = current_buffer;
20920
20921 /* Save things including mode_line_proptrans_alist,
20922 and set that to nil so that we don't alter the outer value. */
20923 record_unwind_protect (unwind_format_mode_line,
20924 format_mode_line_unwind_data
20925 (XFRAME (WINDOW_FRAME (w)),
20926 old_buffer, selected_window, 1));
20927 mode_line_proptrans_alist = Qnil;
20928
20929 Fselect_window (window, Qt);
20930 set_buffer_internal_1 (XBUFFER (buffer));
20931
20932 init_iterator (&it, w, -1, -1, NULL, face_id);
20933
20934 if (no_props)
20935 {
20936 mode_line_target = MODE_LINE_NOPROP;
20937 mode_line_string_face_prop = Qnil;
20938 mode_line_string_list = Qnil;
20939 string_start = MODE_LINE_NOPROP_LEN (0);
20940 }
20941 else
20942 {
20943 mode_line_target = MODE_LINE_STRING;
20944 mode_line_string_list = Qnil;
20945 mode_line_string_face = face;
20946 mode_line_string_face_prop
20947 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20948 }
20949
20950 push_kboard (FRAME_KBOARD (it.f));
20951 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20952 pop_kboard ();
20953
20954 if (no_props)
20955 {
20956 len = MODE_LINE_NOPROP_LEN (string_start);
20957 str = make_string (mode_line_noprop_buf + string_start, len);
20958 }
20959 else
20960 {
20961 mode_line_string_list = Fnreverse (mode_line_string_list);
20962 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20963 empty_unibyte_string);
20964 }
20965
20966 unbind_to (count, Qnil);
20967 return str;
20968 }
20969
20970 /* Write a null-terminated, right justified decimal representation of
20971 the positive integer D to BUF using a minimal field width WIDTH. */
20972
20973 static void
20974 pint2str (register char *buf, register int width, register ptrdiff_t d)
20975 {
20976 register char *p = buf;
20977
20978 if (d <= 0)
20979 *p++ = '0';
20980 else
20981 {
20982 while (d > 0)
20983 {
20984 *p++ = d % 10 + '0';
20985 d /= 10;
20986 }
20987 }
20988
20989 for (width -= (int) (p - buf); width > 0; --width)
20990 *p++ = ' ';
20991 *p-- = '\0';
20992 while (p > buf)
20993 {
20994 d = *buf;
20995 *buf++ = *p;
20996 *p-- = d;
20997 }
20998 }
20999
21000 /* Write a null-terminated, right justified decimal and "human
21001 readable" representation of the nonnegative integer D to BUF using
21002 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21003
21004 static const char power_letter[] =
21005 {
21006 0, /* no letter */
21007 'k', /* kilo */
21008 'M', /* mega */
21009 'G', /* giga */
21010 'T', /* tera */
21011 'P', /* peta */
21012 'E', /* exa */
21013 'Z', /* zetta */
21014 'Y' /* yotta */
21015 };
21016
21017 static void
21018 pint2hrstr (char *buf, int width, ptrdiff_t d)
21019 {
21020 /* We aim to represent the nonnegative integer D as
21021 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21022 ptrdiff_t quotient = d;
21023 int remainder = 0;
21024 /* -1 means: do not use TENTHS. */
21025 int tenths = -1;
21026 int exponent = 0;
21027
21028 /* Length of QUOTIENT.TENTHS as a string. */
21029 int length;
21030
21031 char * psuffix;
21032 char * p;
21033
21034 if (quotient >= 1000)
21035 {
21036 /* Scale to the appropriate EXPONENT. */
21037 do
21038 {
21039 remainder = quotient % 1000;
21040 quotient /= 1000;
21041 exponent++;
21042 }
21043 while (quotient >= 1000);
21044
21045 /* Round to nearest and decide whether to use TENTHS or not. */
21046 if (quotient <= 9)
21047 {
21048 tenths = remainder / 100;
21049 if (remainder % 100 >= 50)
21050 {
21051 if (tenths < 9)
21052 tenths++;
21053 else
21054 {
21055 quotient++;
21056 if (quotient == 10)
21057 tenths = -1;
21058 else
21059 tenths = 0;
21060 }
21061 }
21062 }
21063 else
21064 if (remainder >= 500)
21065 {
21066 if (quotient < 999)
21067 quotient++;
21068 else
21069 {
21070 quotient = 1;
21071 exponent++;
21072 tenths = 0;
21073 }
21074 }
21075 }
21076
21077 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21078 if (tenths == -1 && quotient <= 99)
21079 if (quotient <= 9)
21080 length = 1;
21081 else
21082 length = 2;
21083 else
21084 length = 3;
21085 p = psuffix = buf + max (width, length);
21086
21087 /* Print EXPONENT. */
21088 *psuffix++ = power_letter[exponent];
21089 *psuffix = '\0';
21090
21091 /* Print TENTHS. */
21092 if (tenths >= 0)
21093 {
21094 *--p = '0' + tenths;
21095 *--p = '.';
21096 }
21097
21098 /* Print QUOTIENT. */
21099 do
21100 {
21101 int digit = quotient % 10;
21102 *--p = '0' + digit;
21103 }
21104 while ((quotient /= 10) != 0);
21105
21106 /* Print leading spaces. */
21107 while (buf < p)
21108 *--p = ' ';
21109 }
21110
21111 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21112 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21113 type of CODING_SYSTEM. Return updated pointer into BUF. */
21114
21115 static unsigned char invalid_eol_type[] = "(*invalid*)";
21116
21117 static char *
21118 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21119 {
21120 Lisp_Object val;
21121 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21122 const unsigned char *eol_str;
21123 int eol_str_len;
21124 /* The EOL conversion we are using. */
21125 Lisp_Object eoltype;
21126
21127 val = CODING_SYSTEM_SPEC (coding_system);
21128 eoltype = Qnil;
21129
21130 if (!VECTORP (val)) /* Not yet decided. */
21131 {
21132 *buf++ = multibyte ? '-' : ' ';
21133 if (eol_flag)
21134 eoltype = eol_mnemonic_undecided;
21135 /* Don't mention EOL conversion if it isn't decided. */
21136 }
21137 else
21138 {
21139 Lisp_Object attrs;
21140 Lisp_Object eolvalue;
21141
21142 attrs = AREF (val, 0);
21143 eolvalue = AREF (val, 2);
21144
21145 *buf++ = multibyte
21146 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21147 : ' ';
21148
21149 if (eol_flag)
21150 {
21151 /* The EOL conversion that is normal on this system. */
21152
21153 if (NILP (eolvalue)) /* Not yet decided. */
21154 eoltype = eol_mnemonic_undecided;
21155 else if (VECTORP (eolvalue)) /* Not yet decided. */
21156 eoltype = eol_mnemonic_undecided;
21157 else /* eolvalue is Qunix, Qdos, or Qmac. */
21158 eoltype = (EQ (eolvalue, Qunix)
21159 ? eol_mnemonic_unix
21160 : (EQ (eolvalue, Qdos) == 1
21161 ? eol_mnemonic_dos : eol_mnemonic_mac));
21162 }
21163 }
21164
21165 if (eol_flag)
21166 {
21167 /* Mention the EOL conversion if it is not the usual one. */
21168 if (STRINGP (eoltype))
21169 {
21170 eol_str = SDATA (eoltype);
21171 eol_str_len = SBYTES (eoltype);
21172 }
21173 else if (CHARACTERP (eoltype))
21174 {
21175 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21176 int c = XFASTINT (eoltype);
21177 eol_str_len = CHAR_STRING (c, tmp);
21178 eol_str = tmp;
21179 }
21180 else
21181 {
21182 eol_str = invalid_eol_type;
21183 eol_str_len = sizeof (invalid_eol_type) - 1;
21184 }
21185 memcpy (buf, eol_str, eol_str_len);
21186 buf += eol_str_len;
21187 }
21188
21189 return buf;
21190 }
21191
21192 /* Return a string for the output of a mode line %-spec for window W,
21193 generated by character C. FIELD_WIDTH > 0 means pad the string
21194 returned with spaces to that value. Return a Lisp string in
21195 *STRING if the resulting string is taken from that Lisp string.
21196
21197 Note we operate on the current buffer for most purposes. */
21198
21199 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21200
21201 static const char *
21202 decode_mode_spec (struct window *w, register int c, int field_width,
21203 Lisp_Object *string)
21204 {
21205 Lisp_Object obj;
21206 struct frame *f = XFRAME (WINDOW_FRAME (w));
21207 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21208 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21209 produce strings from numerical values, so limit preposterously
21210 large values of FIELD_WIDTH to avoid overrunning the buffer's
21211 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21212 bytes plus the terminating null. */
21213 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21214 struct buffer *b = current_buffer;
21215
21216 obj = Qnil;
21217 *string = Qnil;
21218
21219 switch (c)
21220 {
21221 case '*':
21222 if (!NILP (BVAR (b, read_only)))
21223 return "%";
21224 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21225 return "*";
21226 return "-";
21227
21228 case '+':
21229 /* This differs from %* only for a modified read-only buffer. */
21230 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21231 return "*";
21232 if (!NILP (BVAR (b, read_only)))
21233 return "%";
21234 return "-";
21235
21236 case '&':
21237 /* This differs from %* in ignoring read-only-ness. */
21238 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21239 return "*";
21240 return "-";
21241
21242 case '%':
21243 return "%";
21244
21245 case '[':
21246 {
21247 int i;
21248 char *p;
21249
21250 if (command_loop_level > 5)
21251 return "[[[... ";
21252 p = decode_mode_spec_buf;
21253 for (i = 0; i < command_loop_level; i++)
21254 *p++ = '[';
21255 *p = 0;
21256 return decode_mode_spec_buf;
21257 }
21258
21259 case ']':
21260 {
21261 int i;
21262 char *p;
21263
21264 if (command_loop_level > 5)
21265 return " ...]]]";
21266 p = decode_mode_spec_buf;
21267 for (i = 0; i < command_loop_level; i++)
21268 *p++ = ']';
21269 *p = 0;
21270 return decode_mode_spec_buf;
21271 }
21272
21273 case '-':
21274 {
21275 register int i;
21276
21277 /* Let lots_of_dashes be a string of infinite length. */
21278 if (mode_line_target == MODE_LINE_NOPROP
21279 || mode_line_target == MODE_LINE_STRING)
21280 return "--";
21281 if (field_width <= 0
21282 || field_width > sizeof (lots_of_dashes))
21283 {
21284 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21285 decode_mode_spec_buf[i] = '-';
21286 decode_mode_spec_buf[i] = '\0';
21287 return decode_mode_spec_buf;
21288 }
21289 else
21290 return lots_of_dashes;
21291 }
21292
21293 case 'b':
21294 obj = BVAR (b, name);
21295 break;
21296
21297 case 'c':
21298 /* %c and %l are ignored in `frame-title-format'.
21299 (In redisplay_internal, the frame title is drawn _before_ the
21300 windows are updated, so the stuff which depends on actual
21301 window contents (such as %l) may fail to render properly, or
21302 even crash emacs.) */
21303 if (mode_line_target == MODE_LINE_TITLE)
21304 return "";
21305 else
21306 {
21307 ptrdiff_t col = current_column ();
21308 w->column_number_displayed = col;
21309 pint2str (decode_mode_spec_buf, width, col);
21310 return decode_mode_spec_buf;
21311 }
21312
21313 case 'e':
21314 #ifndef SYSTEM_MALLOC
21315 {
21316 if (NILP (Vmemory_full))
21317 return "";
21318 else
21319 return "!MEM FULL! ";
21320 }
21321 #else
21322 return "";
21323 #endif
21324
21325 case 'F':
21326 /* %F displays the frame name. */
21327 if (!NILP (f->title))
21328 return SSDATA (f->title);
21329 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21330 return SSDATA (f->name);
21331 return "Emacs";
21332
21333 case 'f':
21334 obj = BVAR (b, filename);
21335 break;
21336
21337 case 'i':
21338 {
21339 ptrdiff_t size = ZV - BEGV;
21340 pint2str (decode_mode_spec_buf, width, size);
21341 return decode_mode_spec_buf;
21342 }
21343
21344 case 'I':
21345 {
21346 ptrdiff_t size = ZV - BEGV;
21347 pint2hrstr (decode_mode_spec_buf, width, size);
21348 return decode_mode_spec_buf;
21349 }
21350
21351 case 'l':
21352 {
21353 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21354 ptrdiff_t topline, nlines, height;
21355 ptrdiff_t junk;
21356
21357 /* %c and %l are ignored in `frame-title-format'. */
21358 if (mode_line_target == MODE_LINE_TITLE)
21359 return "";
21360
21361 startpos = marker_position (w->start);
21362 startpos_byte = marker_byte_position (w->start);
21363 height = WINDOW_TOTAL_LINES (w);
21364
21365 /* If we decided that this buffer isn't suitable for line numbers,
21366 don't forget that too fast. */
21367 if (w->base_line_pos == -1)
21368 goto no_value;
21369
21370 /* If the buffer is very big, don't waste time. */
21371 if (INTEGERP (Vline_number_display_limit)
21372 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21373 {
21374 w->base_line_pos = 0;
21375 w->base_line_number = 0;
21376 goto no_value;
21377 }
21378
21379 if (w->base_line_number > 0
21380 && w->base_line_pos > 0
21381 && w->base_line_pos <= startpos)
21382 {
21383 line = w->base_line_number;
21384 linepos = w->base_line_pos;
21385 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21386 }
21387 else
21388 {
21389 line = 1;
21390 linepos = BUF_BEGV (b);
21391 linepos_byte = BUF_BEGV_BYTE (b);
21392 }
21393
21394 /* Count lines from base line to window start position. */
21395 nlines = display_count_lines (linepos_byte,
21396 startpos_byte,
21397 startpos, &junk);
21398
21399 topline = nlines + line;
21400
21401 /* Determine a new base line, if the old one is too close
21402 or too far away, or if we did not have one.
21403 "Too close" means it's plausible a scroll-down would
21404 go back past it. */
21405 if (startpos == BUF_BEGV (b))
21406 {
21407 w->base_line_number = topline;
21408 w->base_line_pos = BUF_BEGV (b);
21409 }
21410 else if (nlines < height + 25 || nlines > height * 3 + 50
21411 || linepos == BUF_BEGV (b))
21412 {
21413 ptrdiff_t limit = BUF_BEGV (b);
21414 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21415 ptrdiff_t position;
21416 ptrdiff_t distance =
21417 (height * 2 + 30) * line_number_display_limit_width;
21418
21419 if (startpos - distance > limit)
21420 {
21421 limit = startpos - distance;
21422 limit_byte = CHAR_TO_BYTE (limit);
21423 }
21424
21425 nlines = display_count_lines (startpos_byte,
21426 limit_byte,
21427 - (height * 2 + 30),
21428 &position);
21429 /* If we couldn't find the lines we wanted within
21430 line_number_display_limit_width chars per line,
21431 give up on line numbers for this window. */
21432 if (position == limit_byte && limit == startpos - distance)
21433 {
21434 w->base_line_pos = -1;
21435 w->base_line_number = 0;
21436 goto no_value;
21437 }
21438
21439 w->base_line_number = topline - nlines;
21440 w->base_line_pos = BYTE_TO_CHAR (position);
21441 }
21442
21443 /* Now count lines from the start pos to point. */
21444 nlines = display_count_lines (startpos_byte,
21445 PT_BYTE, PT, &junk);
21446
21447 /* Record that we did display the line number. */
21448 line_number_displayed = 1;
21449
21450 /* Make the string to show. */
21451 pint2str (decode_mode_spec_buf, width, topline + nlines);
21452 return decode_mode_spec_buf;
21453 no_value:
21454 {
21455 char* p = decode_mode_spec_buf;
21456 int pad = width - 2;
21457 while (pad-- > 0)
21458 *p++ = ' ';
21459 *p++ = '?';
21460 *p++ = '?';
21461 *p = '\0';
21462 return decode_mode_spec_buf;
21463 }
21464 }
21465 break;
21466
21467 case 'm':
21468 obj = BVAR (b, mode_name);
21469 break;
21470
21471 case 'n':
21472 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21473 return " Narrow";
21474 break;
21475
21476 case 'p':
21477 {
21478 ptrdiff_t pos = marker_position (w->start);
21479 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21480
21481 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21482 {
21483 if (pos <= BUF_BEGV (b))
21484 return "All";
21485 else
21486 return "Bottom";
21487 }
21488 else if (pos <= BUF_BEGV (b))
21489 return "Top";
21490 else
21491 {
21492 if (total > 1000000)
21493 /* Do it differently for a large value, to avoid overflow. */
21494 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21495 else
21496 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21497 /* We can't normally display a 3-digit number,
21498 so get us a 2-digit number that is close. */
21499 if (total == 100)
21500 total = 99;
21501 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21502 return decode_mode_spec_buf;
21503 }
21504 }
21505
21506 /* Display percentage of size above the bottom of the screen. */
21507 case 'P':
21508 {
21509 ptrdiff_t toppos = marker_position (w->start);
21510 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21511 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21512
21513 if (botpos >= BUF_ZV (b))
21514 {
21515 if (toppos <= BUF_BEGV (b))
21516 return "All";
21517 else
21518 return "Bottom";
21519 }
21520 else
21521 {
21522 if (total > 1000000)
21523 /* Do it differently for a large value, to avoid overflow. */
21524 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21525 else
21526 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21527 /* We can't normally display a 3-digit number,
21528 so get us a 2-digit number that is close. */
21529 if (total == 100)
21530 total = 99;
21531 if (toppos <= BUF_BEGV (b))
21532 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21533 else
21534 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21535 return decode_mode_spec_buf;
21536 }
21537 }
21538
21539 case 's':
21540 /* status of process */
21541 obj = Fget_buffer_process (Fcurrent_buffer ());
21542 if (NILP (obj))
21543 return "no process";
21544 #ifndef MSDOS
21545 obj = Fsymbol_name (Fprocess_status (obj));
21546 #endif
21547 break;
21548
21549 case '@':
21550 {
21551 ptrdiff_t count = inhibit_garbage_collection ();
21552 Lisp_Object val = call1 (intern ("file-remote-p"),
21553 BVAR (current_buffer, directory));
21554 unbind_to (count, Qnil);
21555
21556 if (NILP (val))
21557 return "-";
21558 else
21559 return "@";
21560 }
21561
21562 case 'z':
21563 /* coding-system (not including end-of-line format) */
21564 case 'Z':
21565 /* coding-system (including end-of-line type) */
21566 {
21567 int eol_flag = (c == 'Z');
21568 char *p = decode_mode_spec_buf;
21569
21570 if (! FRAME_WINDOW_P (f))
21571 {
21572 /* No need to mention EOL here--the terminal never needs
21573 to do EOL conversion. */
21574 p = decode_mode_spec_coding (CODING_ID_NAME
21575 (FRAME_KEYBOARD_CODING (f)->id),
21576 p, 0);
21577 p = decode_mode_spec_coding (CODING_ID_NAME
21578 (FRAME_TERMINAL_CODING (f)->id),
21579 p, 0);
21580 }
21581 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21582 p, eol_flag);
21583
21584 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21585 #ifdef subprocesses
21586 obj = Fget_buffer_process (Fcurrent_buffer ());
21587 if (PROCESSP (obj))
21588 {
21589 p = decode_mode_spec_coding
21590 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21591 p = decode_mode_spec_coding
21592 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21593 }
21594 #endif /* subprocesses */
21595 #endif /* 0 */
21596 *p = 0;
21597 return decode_mode_spec_buf;
21598 }
21599 }
21600
21601 if (STRINGP (obj))
21602 {
21603 *string = obj;
21604 return SSDATA (obj);
21605 }
21606 else
21607 return "";
21608 }
21609
21610
21611 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21612 means count lines back from START_BYTE. But don't go beyond
21613 LIMIT_BYTE. Return the number of lines thus found (always
21614 nonnegative).
21615
21616 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21617 either the position COUNT lines after/before START_BYTE, if we
21618 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21619 COUNT lines. */
21620
21621 static ptrdiff_t
21622 display_count_lines (ptrdiff_t start_byte,
21623 ptrdiff_t limit_byte, ptrdiff_t count,
21624 ptrdiff_t *byte_pos_ptr)
21625 {
21626 register unsigned char *cursor;
21627 unsigned char *base;
21628
21629 register ptrdiff_t ceiling;
21630 register unsigned char *ceiling_addr;
21631 ptrdiff_t orig_count = count;
21632
21633 /* If we are not in selective display mode,
21634 check only for newlines. */
21635 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21636 && !INTEGERP (BVAR (current_buffer, selective_display)));
21637
21638 if (count > 0)
21639 {
21640 while (start_byte < limit_byte)
21641 {
21642 ceiling = BUFFER_CEILING_OF (start_byte);
21643 ceiling = min (limit_byte - 1, ceiling);
21644 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21645 base = (cursor = BYTE_POS_ADDR (start_byte));
21646
21647 do
21648 {
21649 if (selective_display)
21650 {
21651 while (*cursor != '\n' && *cursor != 015
21652 && ++cursor != ceiling_addr)
21653 continue;
21654 if (cursor == ceiling_addr)
21655 break;
21656 }
21657 else
21658 {
21659 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
21660 if (! cursor)
21661 break;
21662 }
21663
21664 cursor++;
21665
21666 if (--count == 0)
21667 {
21668 start_byte += cursor - base;
21669 *byte_pos_ptr = start_byte;
21670 return orig_count;
21671 }
21672 }
21673 while (cursor < ceiling_addr);
21674
21675 start_byte += ceiling_addr - base;
21676 }
21677 }
21678 else
21679 {
21680 while (start_byte > limit_byte)
21681 {
21682 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21683 ceiling = max (limit_byte, ceiling);
21684 ceiling_addr = BYTE_POS_ADDR (ceiling);
21685 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21686 while (1)
21687 {
21688 if (selective_display)
21689 {
21690 while (--cursor >= ceiling_addr
21691 && *cursor != '\n' && *cursor != 015)
21692 continue;
21693 if (cursor < ceiling_addr)
21694 break;
21695 }
21696 else
21697 {
21698 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
21699 if (! cursor)
21700 break;
21701 }
21702
21703 if (++count == 0)
21704 {
21705 start_byte += cursor - base + 1;
21706 *byte_pos_ptr = start_byte;
21707 /* When scanning backwards, we should
21708 not count the newline posterior to which we stop. */
21709 return - orig_count - 1;
21710 }
21711 }
21712 start_byte += ceiling_addr - base;
21713 }
21714 }
21715
21716 *byte_pos_ptr = limit_byte;
21717
21718 if (count < 0)
21719 return - orig_count + count;
21720 return orig_count - count;
21721
21722 }
21723
21724
21725 \f
21726 /***********************************************************************
21727 Displaying strings
21728 ***********************************************************************/
21729
21730 /* Display a NUL-terminated string, starting with index START.
21731
21732 If STRING is non-null, display that C string. Otherwise, the Lisp
21733 string LISP_STRING is displayed. There's a case that STRING is
21734 non-null and LISP_STRING is not nil. It means STRING is a string
21735 data of LISP_STRING. In that case, we display LISP_STRING while
21736 ignoring its text properties.
21737
21738 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21739 FACE_STRING. Display STRING or LISP_STRING with the face at
21740 FACE_STRING_POS in FACE_STRING:
21741
21742 Display the string in the environment given by IT, but use the
21743 standard display table, temporarily.
21744
21745 FIELD_WIDTH is the minimum number of output glyphs to produce.
21746 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21747 with spaces. If STRING has more characters, more than FIELD_WIDTH
21748 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21749
21750 PRECISION is the maximum number of characters to output from
21751 STRING. PRECISION < 0 means don't truncate the string.
21752
21753 This is roughly equivalent to printf format specifiers:
21754
21755 FIELD_WIDTH PRECISION PRINTF
21756 ----------------------------------------
21757 -1 -1 %s
21758 -1 10 %.10s
21759 10 -1 %10s
21760 20 10 %20.10s
21761
21762 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21763 display them, and < 0 means obey the current buffer's value of
21764 enable_multibyte_characters.
21765
21766 Value is the number of columns displayed. */
21767
21768 static int
21769 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21770 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21771 int field_width, int precision, int max_x, int multibyte)
21772 {
21773 int hpos_at_start = it->hpos;
21774 int saved_face_id = it->face_id;
21775 struct glyph_row *row = it->glyph_row;
21776 ptrdiff_t it_charpos;
21777
21778 /* Initialize the iterator IT for iteration over STRING beginning
21779 with index START. */
21780 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21781 precision, field_width, multibyte);
21782 if (string && STRINGP (lisp_string))
21783 /* LISP_STRING is the one returned by decode_mode_spec. We should
21784 ignore its text properties. */
21785 it->stop_charpos = it->end_charpos;
21786
21787 /* If displaying STRING, set up the face of the iterator from
21788 FACE_STRING, if that's given. */
21789 if (STRINGP (face_string))
21790 {
21791 ptrdiff_t endptr;
21792 struct face *face;
21793
21794 it->face_id
21795 = face_at_string_position (it->w, face_string, face_string_pos,
21796 0, it->region_beg_charpos,
21797 it->region_end_charpos,
21798 &endptr, it->base_face_id, 0);
21799 face = FACE_FROM_ID (it->f, it->face_id);
21800 it->face_box_p = face->box != FACE_NO_BOX;
21801 }
21802
21803 /* Set max_x to the maximum allowed X position. Don't let it go
21804 beyond the right edge of the window. */
21805 if (max_x <= 0)
21806 max_x = it->last_visible_x;
21807 else
21808 max_x = min (max_x, it->last_visible_x);
21809
21810 /* Skip over display elements that are not visible. because IT->w is
21811 hscrolled. */
21812 if (it->current_x < it->first_visible_x)
21813 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21814 MOVE_TO_POS | MOVE_TO_X);
21815
21816 row->ascent = it->max_ascent;
21817 row->height = it->max_ascent + it->max_descent;
21818 row->phys_ascent = it->max_phys_ascent;
21819 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21820 row->extra_line_spacing = it->max_extra_line_spacing;
21821
21822 if (STRINGP (it->string))
21823 it_charpos = IT_STRING_CHARPOS (*it);
21824 else
21825 it_charpos = IT_CHARPOS (*it);
21826
21827 /* This condition is for the case that we are called with current_x
21828 past last_visible_x. */
21829 while (it->current_x < max_x)
21830 {
21831 int x_before, x, n_glyphs_before, i, nglyphs;
21832
21833 /* Get the next display element. */
21834 if (!get_next_display_element (it))
21835 break;
21836
21837 /* Produce glyphs. */
21838 x_before = it->current_x;
21839 n_glyphs_before = row->used[TEXT_AREA];
21840 PRODUCE_GLYPHS (it);
21841
21842 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21843 i = 0;
21844 x = x_before;
21845 while (i < nglyphs)
21846 {
21847 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21848
21849 if (it->line_wrap != TRUNCATE
21850 && x + glyph->pixel_width > max_x)
21851 {
21852 /* End of continued line or max_x reached. */
21853 if (CHAR_GLYPH_PADDING_P (*glyph))
21854 {
21855 /* A wide character is unbreakable. */
21856 if (row->reversed_p)
21857 unproduce_glyphs (it, row->used[TEXT_AREA]
21858 - n_glyphs_before);
21859 row->used[TEXT_AREA] = n_glyphs_before;
21860 it->current_x = x_before;
21861 }
21862 else
21863 {
21864 if (row->reversed_p)
21865 unproduce_glyphs (it, row->used[TEXT_AREA]
21866 - (n_glyphs_before + i));
21867 row->used[TEXT_AREA] = n_glyphs_before + i;
21868 it->current_x = x;
21869 }
21870 break;
21871 }
21872 else if (x + glyph->pixel_width >= it->first_visible_x)
21873 {
21874 /* Glyph is at least partially visible. */
21875 ++it->hpos;
21876 if (x < it->first_visible_x)
21877 row->x = x - it->first_visible_x;
21878 }
21879 else
21880 {
21881 /* Glyph is off the left margin of the display area.
21882 Should not happen. */
21883 emacs_abort ();
21884 }
21885
21886 row->ascent = max (row->ascent, it->max_ascent);
21887 row->height = max (row->height, it->max_ascent + it->max_descent);
21888 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21889 row->phys_height = max (row->phys_height,
21890 it->max_phys_ascent + it->max_phys_descent);
21891 row->extra_line_spacing = max (row->extra_line_spacing,
21892 it->max_extra_line_spacing);
21893 x += glyph->pixel_width;
21894 ++i;
21895 }
21896
21897 /* Stop if max_x reached. */
21898 if (i < nglyphs)
21899 break;
21900
21901 /* Stop at line ends. */
21902 if (ITERATOR_AT_END_OF_LINE_P (it))
21903 {
21904 it->continuation_lines_width = 0;
21905 break;
21906 }
21907
21908 set_iterator_to_next (it, 1);
21909 if (STRINGP (it->string))
21910 it_charpos = IT_STRING_CHARPOS (*it);
21911 else
21912 it_charpos = IT_CHARPOS (*it);
21913
21914 /* Stop if truncating at the right edge. */
21915 if (it->line_wrap == TRUNCATE
21916 && it->current_x >= it->last_visible_x)
21917 {
21918 /* Add truncation mark, but don't do it if the line is
21919 truncated at a padding space. */
21920 if (it_charpos < it->string_nchars)
21921 {
21922 if (!FRAME_WINDOW_P (it->f))
21923 {
21924 int ii, n;
21925
21926 if (it->current_x > it->last_visible_x)
21927 {
21928 if (!row->reversed_p)
21929 {
21930 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21931 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21932 break;
21933 }
21934 else
21935 {
21936 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21937 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21938 break;
21939 unproduce_glyphs (it, ii + 1);
21940 ii = row->used[TEXT_AREA] - (ii + 1);
21941 }
21942 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21943 {
21944 row->used[TEXT_AREA] = ii;
21945 produce_special_glyphs (it, IT_TRUNCATION);
21946 }
21947 }
21948 produce_special_glyphs (it, IT_TRUNCATION);
21949 }
21950 row->truncated_on_right_p = 1;
21951 }
21952 break;
21953 }
21954 }
21955
21956 /* Maybe insert a truncation at the left. */
21957 if (it->first_visible_x
21958 && it_charpos > 0)
21959 {
21960 if (!FRAME_WINDOW_P (it->f)
21961 || (row->reversed_p
21962 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21963 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21964 insert_left_trunc_glyphs (it);
21965 row->truncated_on_left_p = 1;
21966 }
21967
21968 it->face_id = saved_face_id;
21969
21970 /* Value is number of columns displayed. */
21971 return it->hpos - hpos_at_start;
21972 }
21973
21974
21975 \f
21976 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21977 appears as an element of LIST or as the car of an element of LIST.
21978 If PROPVAL is a list, compare each element against LIST in that
21979 way, and return 1/2 if any element of PROPVAL is found in LIST.
21980 Otherwise return 0. This function cannot quit.
21981 The return value is 2 if the text is invisible but with an ellipsis
21982 and 1 if it's invisible and without an ellipsis. */
21983
21984 int
21985 invisible_p (register Lisp_Object propval, Lisp_Object list)
21986 {
21987 register Lisp_Object tail, proptail;
21988
21989 for (tail = list; CONSP (tail); tail = XCDR (tail))
21990 {
21991 register Lisp_Object tem;
21992 tem = XCAR (tail);
21993 if (EQ (propval, tem))
21994 return 1;
21995 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21996 return NILP (XCDR (tem)) ? 1 : 2;
21997 }
21998
21999 if (CONSP (propval))
22000 {
22001 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22002 {
22003 Lisp_Object propelt;
22004 propelt = XCAR (proptail);
22005 for (tail = list; CONSP (tail); tail = XCDR (tail))
22006 {
22007 register Lisp_Object tem;
22008 tem = XCAR (tail);
22009 if (EQ (propelt, tem))
22010 return 1;
22011 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22012 return NILP (XCDR (tem)) ? 1 : 2;
22013 }
22014 }
22015 }
22016
22017 return 0;
22018 }
22019
22020 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22021 doc: /* Non-nil if the property makes the text invisible.
22022 POS-OR-PROP can be a marker or number, in which case it is taken to be
22023 a position in the current buffer and the value of the `invisible' property
22024 is checked; or it can be some other value, which is then presumed to be the
22025 value of the `invisible' property of the text of interest.
22026 The non-nil value returned can be t for truly invisible text or something
22027 else if the text is replaced by an ellipsis. */)
22028 (Lisp_Object pos_or_prop)
22029 {
22030 Lisp_Object prop
22031 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22032 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22033 : pos_or_prop);
22034 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22035 return (invis == 0 ? Qnil
22036 : invis == 1 ? Qt
22037 : make_number (invis));
22038 }
22039
22040 /* Calculate a width or height in pixels from a specification using
22041 the following elements:
22042
22043 SPEC ::=
22044 NUM - a (fractional) multiple of the default font width/height
22045 (NUM) - specifies exactly NUM pixels
22046 UNIT - a fixed number of pixels, see below.
22047 ELEMENT - size of a display element in pixels, see below.
22048 (NUM . SPEC) - equals NUM * SPEC
22049 (+ SPEC SPEC ...) - add pixel values
22050 (- SPEC SPEC ...) - subtract pixel values
22051 (- SPEC) - negate pixel value
22052
22053 NUM ::=
22054 INT or FLOAT - a number constant
22055 SYMBOL - use symbol's (buffer local) variable binding.
22056
22057 UNIT ::=
22058 in - pixels per inch *)
22059 mm - pixels per 1/1000 meter *)
22060 cm - pixels per 1/100 meter *)
22061 width - width of current font in pixels.
22062 height - height of current font in pixels.
22063
22064 *) using the ratio(s) defined in display-pixels-per-inch.
22065
22066 ELEMENT ::=
22067
22068 left-fringe - left fringe width in pixels
22069 right-fringe - right fringe width in pixels
22070
22071 left-margin - left margin width in pixels
22072 right-margin - right margin width in pixels
22073
22074 scroll-bar - scroll-bar area width in pixels
22075
22076 Examples:
22077
22078 Pixels corresponding to 5 inches:
22079 (5 . in)
22080
22081 Total width of non-text areas on left side of window (if scroll-bar is on left):
22082 '(space :width (+ left-fringe left-margin scroll-bar))
22083
22084 Align to first text column (in header line):
22085 '(space :align-to 0)
22086
22087 Align to middle of text area minus half the width of variable `my-image'
22088 containing a loaded image:
22089 '(space :align-to (0.5 . (- text my-image)))
22090
22091 Width of left margin minus width of 1 character in the default font:
22092 '(space :width (- left-margin 1))
22093
22094 Width of left margin minus width of 2 characters in the current font:
22095 '(space :width (- left-margin (2 . width)))
22096
22097 Center 1 character over left-margin (in header line):
22098 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22099
22100 Different ways to express width of left fringe plus left margin minus one pixel:
22101 '(space :width (- (+ left-fringe left-margin) (1)))
22102 '(space :width (+ left-fringe left-margin (- (1))))
22103 '(space :width (+ left-fringe left-margin (-1)))
22104
22105 */
22106
22107 static int
22108 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22109 struct font *font, int width_p, int *align_to)
22110 {
22111 double pixels;
22112
22113 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22114 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22115
22116 if (NILP (prop))
22117 return OK_PIXELS (0);
22118
22119 eassert (FRAME_LIVE_P (it->f));
22120
22121 if (SYMBOLP (prop))
22122 {
22123 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22124 {
22125 char *unit = SSDATA (SYMBOL_NAME (prop));
22126
22127 if (unit[0] == 'i' && unit[1] == 'n')
22128 pixels = 1.0;
22129 else if (unit[0] == 'm' && unit[1] == 'm')
22130 pixels = 25.4;
22131 else if (unit[0] == 'c' && unit[1] == 'm')
22132 pixels = 2.54;
22133 else
22134 pixels = 0;
22135 if (pixels > 0)
22136 {
22137 double ppi = (width_p ? FRAME_RES_X (it->f)
22138 : FRAME_RES_Y (it->f));
22139
22140 if (ppi > 0)
22141 return OK_PIXELS (ppi / pixels);
22142 return 0;
22143 }
22144 }
22145
22146 #ifdef HAVE_WINDOW_SYSTEM
22147 if (EQ (prop, Qheight))
22148 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22149 if (EQ (prop, Qwidth))
22150 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22151 #else
22152 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22153 return OK_PIXELS (1);
22154 #endif
22155
22156 if (EQ (prop, Qtext))
22157 return OK_PIXELS (width_p
22158 ? window_box_width (it->w, TEXT_AREA)
22159 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22160
22161 if (align_to && *align_to < 0)
22162 {
22163 *res = 0;
22164 if (EQ (prop, Qleft))
22165 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22166 if (EQ (prop, Qright))
22167 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22168 if (EQ (prop, Qcenter))
22169 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22170 + window_box_width (it->w, TEXT_AREA) / 2);
22171 if (EQ (prop, Qleft_fringe))
22172 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22173 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22174 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22175 if (EQ (prop, Qright_fringe))
22176 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22177 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22178 : window_box_right_offset (it->w, TEXT_AREA));
22179 if (EQ (prop, Qleft_margin))
22180 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22181 if (EQ (prop, Qright_margin))
22182 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22183 if (EQ (prop, Qscroll_bar))
22184 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22185 ? 0
22186 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22187 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22188 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22189 : 0)));
22190 }
22191 else
22192 {
22193 if (EQ (prop, Qleft_fringe))
22194 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22195 if (EQ (prop, Qright_fringe))
22196 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22197 if (EQ (prop, Qleft_margin))
22198 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22199 if (EQ (prop, Qright_margin))
22200 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22201 if (EQ (prop, Qscroll_bar))
22202 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22203 }
22204
22205 prop = buffer_local_value_1 (prop, it->w->contents);
22206 if (EQ (prop, Qunbound))
22207 prop = Qnil;
22208 }
22209
22210 if (INTEGERP (prop) || FLOATP (prop))
22211 {
22212 int base_unit = (width_p
22213 ? FRAME_COLUMN_WIDTH (it->f)
22214 : FRAME_LINE_HEIGHT (it->f));
22215 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22216 }
22217
22218 if (CONSP (prop))
22219 {
22220 Lisp_Object car = XCAR (prop);
22221 Lisp_Object cdr = XCDR (prop);
22222
22223 if (SYMBOLP (car))
22224 {
22225 #ifdef HAVE_WINDOW_SYSTEM
22226 if (FRAME_WINDOW_P (it->f)
22227 && valid_image_p (prop))
22228 {
22229 ptrdiff_t id = lookup_image (it->f, prop);
22230 struct image *img = IMAGE_FROM_ID (it->f, id);
22231
22232 return OK_PIXELS (width_p ? img->width : img->height);
22233 }
22234 #endif
22235 if (EQ (car, Qplus) || EQ (car, Qminus))
22236 {
22237 int first = 1;
22238 double px;
22239
22240 pixels = 0;
22241 while (CONSP (cdr))
22242 {
22243 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22244 font, width_p, align_to))
22245 return 0;
22246 if (first)
22247 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22248 else
22249 pixels += px;
22250 cdr = XCDR (cdr);
22251 }
22252 if (EQ (car, Qminus))
22253 pixels = -pixels;
22254 return OK_PIXELS (pixels);
22255 }
22256
22257 car = buffer_local_value_1 (car, it->w->contents);
22258 if (EQ (car, Qunbound))
22259 car = Qnil;
22260 }
22261
22262 if (INTEGERP (car) || FLOATP (car))
22263 {
22264 double fact;
22265 pixels = XFLOATINT (car);
22266 if (NILP (cdr))
22267 return OK_PIXELS (pixels);
22268 if (calc_pixel_width_or_height (&fact, it, cdr,
22269 font, width_p, align_to))
22270 return OK_PIXELS (pixels * fact);
22271 return 0;
22272 }
22273
22274 return 0;
22275 }
22276
22277 return 0;
22278 }
22279
22280 \f
22281 /***********************************************************************
22282 Glyph Display
22283 ***********************************************************************/
22284
22285 #ifdef HAVE_WINDOW_SYSTEM
22286
22287 #ifdef GLYPH_DEBUG
22288
22289 void
22290 dump_glyph_string (struct glyph_string *s)
22291 {
22292 fprintf (stderr, "glyph string\n");
22293 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22294 s->x, s->y, s->width, s->height);
22295 fprintf (stderr, " ybase = %d\n", s->ybase);
22296 fprintf (stderr, " hl = %d\n", s->hl);
22297 fprintf (stderr, " left overhang = %d, right = %d\n",
22298 s->left_overhang, s->right_overhang);
22299 fprintf (stderr, " nchars = %d\n", s->nchars);
22300 fprintf (stderr, " extends to end of line = %d\n",
22301 s->extends_to_end_of_line_p);
22302 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22303 fprintf (stderr, " bg width = %d\n", s->background_width);
22304 }
22305
22306 #endif /* GLYPH_DEBUG */
22307
22308 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22309 of XChar2b structures for S; it can't be allocated in
22310 init_glyph_string because it must be allocated via `alloca'. W
22311 is the window on which S is drawn. ROW and AREA are the glyph row
22312 and area within the row from which S is constructed. START is the
22313 index of the first glyph structure covered by S. HL is a
22314 face-override for drawing S. */
22315
22316 #ifdef HAVE_NTGUI
22317 #define OPTIONAL_HDC(hdc) HDC hdc,
22318 #define DECLARE_HDC(hdc) HDC hdc;
22319 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22320 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22321 #endif
22322
22323 #ifndef OPTIONAL_HDC
22324 #define OPTIONAL_HDC(hdc)
22325 #define DECLARE_HDC(hdc)
22326 #define ALLOCATE_HDC(hdc, f)
22327 #define RELEASE_HDC(hdc, f)
22328 #endif
22329
22330 static void
22331 init_glyph_string (struct glyph_string *s,
22332 OPTIONAL_HDC (hdc)
22333 XChar2b *char2b, struct window *w, struct glyph_row *row,
22334 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22335 {
22336 memset (s, 0, sizeof *s);
22337 s->w = w;
22338 s->f = XFRAME (w->frame);
22339 #ifdef HAVE_NTGUI
22340 s->hdc = hdc;
22341 #endif
22342 s->display = FRAME_X_DISPLAY (s->f);
22343 s->window = FRAME_X_WINDOW (s->f);
22344 s->char2b = char2b;
22345 s->hl = hl;
22346 s->row = row;
22347 s->area = area;
22348 s->first_glyph = row->glyphs[area] + start;
22349 s->height = row->height;
22350 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22351 s->ybase = s->y + row->ascent;
22352 }
22353
22354
22355 /* Append the list of glyph strings with head H and tail T to the list
22356 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22357
22358 static void
22359 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22360 struct glyph_string *h, struct glyph_string *t)
22361 {
22362 if (h)
22363 {
22364 if (*head)
22365 (*tail)->next = h;
22366 else
22367 *head = h;
22368 h->prev = *tail;
22369 *tail = t;
22370 }
22371 }
22372
22373
22374 /* Prepend the list of glyph strings with head H and tail T to the
22375 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22376 result. */
22377
22378 static void
22379 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22380 struct glyph_string *h, struct glyph_string *t)
22381 {
22382 if (h)
22383 {
22384 if (*head)
22385 (*head)->prev = t;
22386 else
22387 *tail = t;
22388 t->next = *head;
22389 *head = h;
22390 }
22391 }
22392
22393
22394 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22395 Set *HEAD and *TAIL to the resulting list. */
22396
22397 static void
22398 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22399 struct glyph_string *s)
22400 {
22401 s->next = s->prev = NULL;
22402 append_glyph_string_lists (head, tail, s, s);
22403 }
22404
22405
22406 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22407 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22408 make sure that X resources for the face returned are allocated.
22409 Value is a pointer to a realized face that is ready for display if
22410 DISPLAY_P is non-zero. */
22411
22412 static struct face *
22413 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22414 XChar2b *char2b, int display_p)
22415 {
22416 struct face *face = FACE_FROM_ID (f, face_id);
22417 unsigned code = 0;
22418
22419 if (face->font)
22420 {
22421 code = face->font->driver->encode_char (face->font, c);
22422
22423 if (code == FONT_INVALID_CODE)
22424 code = 0;
22425 }
22426 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22427
22428 /* Make sure X resources of the face are allocated. */
22429 #ifdef HAVE_X_WINDOWS
22430 if (display_p)
22431 #endif
22432 {
22433 eassert (face != NULL);
22434 PREPARE_FACE_FOR_DISPLAY (f, face);
22435 }
22436
22437 return face;
22438 }
22439
22440
22441 /* Get face and two-byte form of character glyph GLYPH on frame F.
22442 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22443 a pointer to a realized face that is ready for display. */
22444
22445 static struct face *
22446 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22447 XChar2b *char2b, int *two_byte_p)
22448 {
22449 struct face *face;
22450 unsigned code = 0;
22451
22452 eassert (glyph->type == CHAR_GLYPH);
22453 face = FACE_FROM_ID (f, glyph->face_id);
22454
22455 /* Make sure X resources of the face are allocated. */
22456 eassert (face != NULL);
22457 PREPARE_FACE_FOR_DISPLAY (f, face);
22458
22459 if (two_byte_p)
22460 *two_byte_p = 0;
22461
22462 if (face->font)
22463 {
22464 if (CHAR_BYTE8_P (glyph->u.ch))
22465 code = CHAR_TO_BYTE8 (glyph->u.ch);
22466 else
22467 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22468
22469 if (code == FONT_INVALID_CODE)
22470 code = 0;
22471 }
22472
22473 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22474 return face;
22475 }
22476
22477
22478 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22479 Return 1 if FONT has a glyph for C, otherwise return 0. */
22480
22481 static int
22482 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22483 {
22484 unsigned code;
22485
22486 if (CHAR_BYTE8_P (c))
22487 code = CHAR_TO_BYTE8 (c);
22488 else
22489 code = font->driver->encode_char (font, c);
22490
22491 if (code == FONT_INVALID_CODE)
22492 return 0;
22493 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22494 return 1;
22495 }
22496
22497
22498 /* Fill glyph string S with composition components specified by S->cmp.
22499
22500 BASE_FACE is the base face of the composition.
22501 S->cmp_from is the index of the first component for S.
22502
22503 OVERLAPS non-zero means S should draw the foreground only, and use
22504 its physical height for clipping. See also draw_glyphs.
22505
22506 Value is the index of a component not in S. */
22507
22508 static int
22509 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22510 int overlaps)
22511 {
22512 int i;
22513 /* For all glyphs of this composition, starting at the offset
22514 S->cmp_from, until we reach the end of the definition or encounter a
22515 glyph that requires the different face, add it to S. */
22516 struct face *face;
22517
22518 eassert (s);
22519
22520 s->for_overlaps = overlaps;
22521 s->face = NULL;
22522 s->font = NULL;
22523 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22524 {
22525 int c = COMPOSITION_GLYPH (s->cmp, i);
22526
22527 /* TAB in a composition means display glyphs with padding space
22528 on the left or right. */
22529 if (c != '\t')
22530 {
22531 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22532 -1, Qnil);
22533
22534 face = get_char_face_and_encoding (s->f, c, face_id,
22535 s->char2b + i, 1);
22536 if (face)
22537 {
22538 if (! s->face)
22539 {
22540 s->face = face;
22541 s->font = s->face->font;
22542 }
22543 else if (s->face != face)
22544 break;
22545 }
22546 }
22547 ++s->nchars;
22548 }
22549 s->cmp_to = i;
22550
22551 if (s->face == NULL)
22552 {
22553 s->face = base_face->ascii_face;
22554 s->font = s->face->font;
22555 }
22556
22557 /* All glyph strings for the same composition has the same width,
22558 i.e. the width set for the first component of the composition. */
22559 s->width = s->first_glyph->pixel_width;
22560
22561 /* If the specified font could not be loaded, use the frame's
22562 default font, but record the fact that we couldn't load it in
22563 the glyph string so that we can draw rectangles for the
22564 characters of the glyph string. */
22565 if (s->font == NULL)
22566 {
22567 s->font_not_found_p = 1;
22568 s->font = FRAME_FONT (s->f);
22569 }
22570
22571 /* Adjust base line for subscript/superscript text. */
22572 s->ybase += s->first_glyph->voffset;
22573
22574 /* This glyph string must always be drawn with 16-bit functions. */
22575 s->two_byte_p = 1;
22576
22577 return s->cmp_to;
22578 }
22579
22580 static int
22581 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22582 int start, int end, int overlaps)
22583 {
22584 struct glyph *glyph, *last;
22585 Lisp_Object lgstring;
22586 int i;
22587
22588 s->for_overlaps = overlaps;
22589 glyph = s->row->glyphs[s->area] + start;
22590 last = s->row->glyphs[s->area] + end;
22591 s->cmp_id = glyph->u.cmp.id;
22592 s->cmp_from = glyph->slice.cmp.from;
22593 s->cmp_to = glyph->slice.cmp.to + 1;
22594 s->face = FACE_FROM_ID (s->f, face_id);
22595 lgstring = composition_gstring_from_id (s->cmp_id);
22596 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22597 glyph++;
22598 while (glyph < last
22599 && glyph->u.cmp.automatic
22600 && glyph->u.cmp.id == s->cmp_id
22601 && s->cmp_to == glyph->slice.cmp.from)
22602 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22603
22604 for (i = s->cmp_from; i < s->cmp_to; i++)
22605 {
22606 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22607 unsigned code = LGLYPH_CODE (lglyph);
22608
22609 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22610 }
22611 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22612 return glyph - s->row->glyphs[s->area];
22613 }
22614
22615
22616 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22617 See the comment of fill_glyph_string for arguments.
22618 Value is the index of the first glyph not in S. */
22619
22620
22621 static int
22622 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22623 int start, int end, int overlaps)
22624 {
22625 struct glyph *glyph, *last;
22626 int voffset;
22627
22628 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22629 s->for_overlaps = overlaps;
22630 glyph = s->row->glyphs[s->area] + start;
22631 last = s->row->glyphs[s->area] + end;
22632 voffset = glyph->voffset;
22633 s->face = FACE_FROM_ID (s->f, face_id);
22634 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22635 s->nchars = 1;
22636 s->width = glyph->pixel_width;
22637 glyph++;
22638 while (glyph < last
22639 && glyph->type == GLYPHLESS_GLYPH
22640 && glyph->voffset == voffset
22641 && glyph->face_id == face_id)
22642 {
22643 s->nchars++;
22644 s->width += glyph->pixel_width;
22645 glyph++;
22646 }
22647 s->ybase += voffset;
22648 return glyph - s->row->glyphs[s->area];
22649 }
22650
22651
22652 /* Fill glyph string S from a sequence of character glyphs.
22653
22654 FACE_ID is the face id of the string. START is the index of the
22655 first glyph to consider, END is the index of the last + 1.
22656 OVERLAPS non-zero means S should draw the foreground only, and use
22657 its physical height for clipping. See also draw_glyphs.
22658
22659 Value is the index of the first glyph not in S. */
22660
22661 static int
22662 fill_glyph_string (struct glyph_string *s, int face_id,
22663 int start, int end, int overlaps)
22664 {
22665 struct glyph *glyph, *last;
22666 int voffset;
22667 int glyph_not_available_p;
22668
22669 eassert (s->f == XFRAME (s->w->frame));
22670 eassert (s->nchars == 0);
22671 eassert (start >= 0 && end > start);
22672
22673 s->for_overlaps = overlaps;
22674 glyph = s->row->glyphs[s->area] + start;
22675 last = s->row->glyphs[s->area] + end;
22676 voffset = glyph->voffset;
22677 s->padding_p = glyph->padding_p;
22678 glyph_not_available_p = glyph->glyph_not_available_p;
22679
22680 while (glyph < last
22681 && glyph->type == CHAR_GLYPH
22682 && glyph->voffset == voffset
22683 /* Same face id implies same font, nowadays. */
22684 && glyph->face_id == face_id
22685 && glyph->glyph_not_available_p == glyph_not_available_p)
22686 {
22687 int two_byte_p;
22688
22689 s->face = get_glyph_face_and_encoding (s->f, glyph,
22690 s->char2b + s->nchars,
22691 &two_byte_p);
22692 s->two_byte_p = two_byte_p;
22693 ++s->nchars;
22694 eassert (s->nchars <= end - start);
22695 s->width += glyph->pixel_width;
22696 if (glyph++->padding_p != s->padding_p)
22697 break;
22698 }
22699
22700 s->font = s->face->font;
22701
22702 /* If the specified font could not be loaded, use the frame's font,
22703 but record the fact that we couldn't load it in
22704 S->font_not_found_p so that we can draw rectangles for the
22705 characters of the glyph string. */
22706 if (s->font == NULL || glyph_not_available_p)
22707 {
22708 s->font_not_found_p = 1;
22709 s->font = FRAME_FONT (s->f);
22710 }
22711
22712 /* Adjust base line for subscript/superscript text. */
22713 s->ybase += voffset;
22714
22715 eassert (s->face && s->face->gc);
22716 return glyph - s->row->glyphs[s->area];
22717 }
22718
22719
22720 /* Fill glyph string S from image glyph S->first_glyph. */
22721
22722 static void
22723 fill_image_glyph_string (struct glyph_string *s)
22724 {
22725 eassert (s->first_glyph->type == IMAGE_GLYPH);
22726 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22727 eassert (s->img);
22728 s->slice = s->first_glyph->slice.img;
22729 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22730 s->font = s->face->font;
22731 s->width = s->first_glyph->pixel_width;
22732
22733 /* Adjust base line for subscript/superscript text. */
22734 s->ybase += s->first_glyph->voffset;
22735 }
22736
22737
22738 /* Fill glyph string S from a sequence of stretch glyphs.
22739
22740 START is the index of the first glyph to consider,
22741 END is the index of the last + 1.
22742
22743 Value is the index of the first glyph not in S. */
22744
22745 static int
22746 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22747 {
22748 struct glyph *glyph, *last;
22749 int voffset, face_id;
22750
22751 eassert (s->first_glyph->type == STRETCH_GLYPH);
22752
22753 glyph = s->row->glyphs[s->area] + start;
22754 last = s->row->glyphs[s->area] + end;
22755 face_id = glyph->face_id;
22756 s->face = FACE_FROM_ID (s->f, face_id);
22757 s->font = s->face->font;
22758 s->width = glyph->pixel_width;
22759 s->nchars = 1;
22760 voffset = glyph->voffset;
22761
22762 for (++glyph;
22763 (glyph < last
22764 && glyph->type == STRETCH_GLYPH
22765 && glyph->voffset == voffset
22766 && glyph->face_id == face_id);
22767 ++glyph)
22768 s->width += glyph->pixel_width;
22769
22770 /* Adjust base line for subscript/superscript text. */
22771 s->ybase += voffset;
22772
22773 /* The case that face->gc == 0 is handled when drawing the glyph
22774 string by calling PREPARE_FACE_FOR_DISPLAY. */
22775 eassert (s->face);
22776 return glyph - s->row->glyphs[s->area];
22777 }
22778
22779 static struct font_metrics *
22780 get_per_char_metric (struct font *font, XChar2b *char2b)
22781 {
22782 static struct font_metrics metrics;
22783 unsigned code;
22784
22785 if (! font)
22786 return NULL;
22787 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22788 if (code == FONT_INVALID_CODE)
22789 return NULL;
22790 font->driver->text_extents (font, &code, 1, &metrics);
22791 return &metrics;
22792 }
22793
22794 /* EXPORT for RIF:
22795 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22796 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22797 assumed to be zero. */
22798
22799 void
22800 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22801 {
22802 *left = *right = 0;
22803
22804 if (glyph->type == CHAR_GLYPH)
22805 {
22806 struct face *face;
22807 XChar2b char2b;
22808 struct font_metrics *pcm;
22809
22810 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22811 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22812 {
22813 if (pcm->rbearing > pcm->width)
22814 *right = pcm->rbearing - pcm->width;
22815 if (pcm->lbearing < 0)
22816 *left = -pcm->lbearing;
22817 }
22818 }
22819 else if (glyph->type == COMPOSITE_GLYPH)
22820 {
22821 if (! glyph->u.cmp.automatic)
22822 {
22823 struct composition *cmp = composition_table[glyph->u.cmp.id];
22824
22825 if (cmp->rbearing > cmp->pixel_width)
22826 *right = cmp->rbearing - cmp->pixel_width;
22827 if (cmp->lbearing < 0)
22828 *left = - cmp->lbearing;
22829 }
22830 else
22831 {
22832 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22833 struct font_metrics metrics;
22834
22835 composition_gstring_width (gstring, glyph->slice.cmp.from,
22836 glyph->slice.cmp.to + 1, &metrics);
22837 if (metrics.rbearing > metrics.width)
22838 *right = metrics.rbearing - metrics.width;
22839 if (metrics.lbearing < 0)
22840 *left = - metrics.lbearing;
22841 }
22842 }
22843 }
22844
22845
22846 /* Return the index of the first glyph preceding glyph string S that
22847 is overwritten by S because of S's left overhang. Value is -1
22848 if no glyphs are overwritten. */
22849
22850 static int
22851 left_overwritten (struct glyph_string *s)
22852 {
22853 int k;
22854
22855 if (s->left_overhang)
22856 {
22857 int x = 0, i;
22858 struct glyph *glyphs = s->row->glyphs[s->area];
22859 int first = s->first_glyph - glyphs;
22860
22861 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22862 x -= glyphs[i].pixel_width;
22863
22864 k = i + 1;
22865 }
22866 else
22867 k = -1;
22868
22869 return k;
22870 }
22871
22872
22873 /* Return the index of the first glyph preceding glyph string S that
22874 is overwriting S because of its right overhang. Value is -1 if no
22875 glyph in front of S overwrites S. */
22876
22877 static int
22878 left_overwriting (struct glyph_string *s)
22879 {
22880 int i, k, x;
22881 struct glyph *glyphs = s->row->glyphs[s->area];
22882 int first = s->first_glyph - glyphs;
22883
22884 k = -1;
22885 x = 0;
22886 for (i = first - 1; i >= 0; --i)
22887 {
22888 int left, right;
22889 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22890 if (x + right > 0)
22891 k = i;
22892 x -= glyphs[i].pixel_width;
22893 }
22894
22895 return k;
22896 }
22897
22898
22899 /* Return the index of the last glyph following glyph string S that is
22900 overwritten by S because of S's right overhang. Value is -1 if
22901 no such glyph is found. */
22902
22903 static int
22904 right_overwritten (struct glyph_string *s)
22905 {
22906 int k = -1;
22907
22908 if (s->right_overhang)
22909 {
22910 int x = 0, i;
22911 struct glyph *glyphs = s->row->glyphs[s->area];
22912 int first = (s->first_glyph - glyphs
22913 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22914 int end = s->row->used[s->area];
22915
22916 for (i = first; i < end && s->right_overhang > x; ++i)
22917 x += glyphs[i].pixel_width;
22918
22919 k = i;
22920 }
22921
22922 return k;
22923 }
22924
22925
22926 /* Return the index of the last glyph following glyph string S that
22927 overwrites S because of its left overhang. Value is negative
22928 if no such glyph is found. */
22929
22930 static int
22931 right_overwriting (struct glyph_string *s)
22932 {
22933 int i, k, x;
22934 int end = s->row->used[s->area];
22935 struct glyph *glyphs = s->row->glyphs[s->area];
22936 int first = (s->first_glyph - glyphs
22937 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22938
22939 k = -1;
22940 x = 0;
22941 for (i = first; i < end; ++i)
22942 {
22943 int left, right;
22944 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22945 if (x - left < 0)
22946 k = i;
22947 x += glyphs[i].pixel_width;
22948 }
22949
22950 return k;
22951 }
22952
22953
22954 /* Set background width of glyph string S. START is the index of the
22955 first glyph following S. LAST_X is the right-most x-position + 1
22956 in the drawing area. */
22957
22958 static void
22959 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22960 {
22961 /* If the face of this glyph string has to be drawn to the end of
22962 the drawing area, set S->extends_to_end_of_line_p. */
22963
22964 if (start == s->row->used[s->area]
22965 && s->area == TEXT_AREA
22966 && ((s->row->fill_line_p
22967 && (s->hl == DRAW_NORMAL_TEXT
22968 || s->hl == DRAW_IMAGE_RAISED
22969 || s->hl == DRAW_IMAGE_SUNKEN))
22970 || s->hl == DRAW_MOUSE_FACE))
22971 s->extends_to_end_of_line_p = 1;
22972
22973 /* If S extends its face to the end of the line, set its
22974 background_width to the distance to the right edge of the drawing
22975 area. */
22976 if (s->extends_to_end_of_line_p)
22977 s->background_width = last_x - s->x + 1;
22978 else
22979 s->background_width = s->width;
22980 }
22981
22982
22983 /* Compute overhangs and x-positions for glyph string S and its
22984 predecessors, or successors. X is the starting x-position for S.
22985 BACKWARD_P non-zero means process predecessors. */
22986
22987 static void
22988 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22989 {
22990 if (backward_p)
22991 {
22992 while (s)
22993 {
22994 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22995 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22996 x -= s->width;
22997 s->x = x;
22998 s = s->prev;
22999 }
23000 }
23001 else
23002 {
23003 while (s)
23004 {
23005 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23006 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23007 s->x = x;
23008 x += s->width;
23009 s = s->next;
23010 }
23011 }
23012 }
23013
23014
23015
23016 /* The following macros are only called from draw_glyphs below.
23017 They reference the following parameters of that function directly:
23018 `w', `row', `area', and `overlap_p'
23019 as well as the following local variables:
23020 `s', `f', and `hdc' (in W32) */
23021
23022 #ifdef HAVE_NTGUI
23023 /* On W32, silently add local `hdc' variable to argument list of
23024 init_glyph_string. */
23025 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23026 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23027 #else
23028 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23029 init_glyph_string (s, char2b, w, row, area, start, hl)
23030 #endif
23031
23032 /* Add a glyph string for a stretch glyph to the list of strings
23033 between HEAD and TAIL. START is the index of the stretch glyph in
23034 row area AREA of glyph row ROW. END is the index of the last glyph
23035 in that glyph row area. X is the current output position assigned
23036 to the new glyph string constructed. HL overrides that face of the
23037 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23038 is the right-most x-position of the drawing area. */
23039
23040 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23041 and below -- keep them on one line. */
23042 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23043 do \
23044 { \
23045 s = alloca (sizeof *s); \
23046 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23047 START = fill_stretch_glyph_string (s, START, END); \
23048 append_glyph_string (&HEAD, &TAIL, s); \
23049 s->x = (X); \
23050 } \
23051 while (0)
23052
23053
23054 /* Add a glyph string for an image glyph to the list of strings
23055 between HEAD and TAIL. START is the index of the image glyph in
23056 row area AREA of glyph row ROW. END is the index of the last glyph
23057 in that glyph row area. X is the current output position assigned
23058 to the new glyph string constructed. HL overrides that face of the
23059 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23060 is the right-most x-position of the drawing area. */
23061
23062 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23063 do \
23064 { \
23065 s = alloca (sizeof *s); \
23066 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23067 fill_image_glyph_string (s); \
23068 append_glyph_string (&HEAD, &TAIL, s); \
23069 ++START; \
23070 s->x = (X); \
23071 } \
23072 while (0)
23073
23074
23075 /* Add a glyph string for a sequence of character glyphs to the list
23076 of strings between HEAD and TAIL. START is the index of the first
23077 glyph in row area AREA of glyph row ROW that is part of the new
23078 glyph string. END is the index of the last glyph in that glyph row
23079 area. X is the current output position assigned to the new glyph
23080 string constructed. HL overrides that face of the glyph; e.g. it
23081 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23082 right-most x-position of the drawing area. */
23083
23084 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23085 do \
23086 { \
23087 int face_id; \
23088 XChar2b *char2b; \
23089 \
23090 face_id = (row)->glyphs[area][START].face_id; \
23091 \
23092 s = alloca (sizeof *s); \
23093 char2b = alloca ((END - START) * sizeof *char2b); \
23094 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23095 append_glyph_string (&HEAD, &TAIL, s); \
23096 s->x = (X); \
23097 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23098 } \
23099 while (0)
23100
23101
23102 /* Add a glyph string for a composite sequence to the list of strings
23103 between HEAD and TAIL. START is the index of the first glyph in
23104 row area AREA of glyph row ROW that is part of the new glyph
23105 string. END is the index of the last glyph in that glyph row area.
23106 X is the current output position assigned to the new glyph string
23107 constructed. HL overrides that face of the glyph; e.g. it is
23108 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23109 x-position of the drawing area. */
23110
23111 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23112 do { \
23113 int face_id = (row)->glyphs[area][START].face_id; \
23114 struct face *base_face = FACE_FROM_ID (f, face_id); \
23115 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23116 struct composition *cmp = composition_table[cmp_id]; \
23117 XChar2b *char2b; \
23118 struct glyph_string *first_s = NULL; \
23119 int n; \
23120 \
23121 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23122 \
23123 /* Make glyph_strings for each glyph sequence that is drawable by \
23124 the same face, and append them to HEAD/TAIL. */ \
23125 for (n = 0; n < cmp->glyph_len;) \
23126 { \
23127 s = alloca (sizeof *s); \
23128 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23129 append_glyph_string (&(HEAD), &(TAIL), s); \
23130 s->cmp = cmp; \
23131 s->cmp_from = n; \
23132 s->x = (X); \
23133 if (n == 0) \
23134 first_s = s; \
23135 n = fill_composite_glyph_string (s, base_face, overlaps); \
23136 } \
23137 \
23138 ++START; \
23139 s = first_s; \
23140 } while (0)
23141
23142
23143 /* Add a glyph string for a glyph-string sequence to the list of strings
23144 between HEAD and TAIL. */
23145
23146 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23147 do { \
23148 int face_id; \
23149 XChar2b *char2b; \
23150 Lisp_Object gstring; \
23151 \
23152 face_id = (row)->glyphs[area][START].face_id; \
23153 gstring = (composition_gstring_from_id \
23154 ((row)->glyphs[area][START].u.cmp.id)); \
23155 s = alloca (sizeof *s); \
23156 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23157 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23158 append_glyph_string (&(HEAD), &(TAIL), s); \
23159 s->x = (X); \
23160 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23161 } while (0)
23162
23163
23164 /* Add a glyph string for a sequence of glyphless character's glyphs
23165 to the list of strings between HEAD and TAIL. The meanings of
23166 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23167
23168 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23169 do \
23170 { \
23171 int face_id; \
23172 \
23173 face_id = (row)->glyphs[area][START].face_id; \
23174 \
23175 s = alloca (sizeof *s); \
23176 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23177 append_glyph_string (&HEAD, &TAIL, s); \
23178 s->x = (X); \
23179 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23180 overlaps); \
23181 } \
23182 while (0)
23183
23184
23185 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23186 of AREA of glyph row ROW on window W between indices START and END.
23187 HL overrides the face for drawing glyph strings, e.g. it is
23188 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23189 x-positions of the drawing area.
23190
23191 This is an ugly monster macro construct because we must use alloca
23192 to allocate glyph strings (because draw_glyphs can be called
23193 asynchronously). */
23194
23195 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23196 do \
23197 { \
23198 HEAD = TAIL = NULL; \
23199 while (START < END) \
23200 { \
23201 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23202 switch (first_glyph->type) \
23203 { \
23204 case CHAR_GLYPH: \
23205 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23206 HL, X, LAST_X); \
23207 break; \
23208 \
23209 case COMPOSITE_GLYPH: \
23210 if (first_glyph->u.cmp.automatic) \
23211 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23212 HL, X, LAST_X); \
23213 else \
23214 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23215 HL, X, LAST_X); \
23216 break; \
23217 \
23218 case STRETCH_GLYPH: \
23219 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23220 HL, X, LAST_X); \
23221 break; \
23222 \
23223 case IMAGE_GLYPH: \
23224 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23225 HL, X, LAST_X); \
23226 break; \
23227 \
23228 case GLYPHLESS_GLYPH: \
23229 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23230 HL, X, LAST_X); \
23231 break; \
23232 \
23233 default: \
23234 emacs_abort (); \
23235 } \
23236 \
23237 if (s) \
23238 { \
23239 set_glyph_string_background_width (s, START, LAST_X); \
23240 (X) += s->width; \
23241 } \
23242 } \
23243 } while (0)
23244
23245
23246 /* Draw glyphs between START and END in AREA of ROW on window W,
23247 starting at x-position X. X is relative to AREA in W. HL is a
23248 face-override with the following meaning:
23249
23250 DRAW_NORMAL_TEXT draw normally
23251 DRAW_CURSOR draw in cursor face
23252 DRAW_MOUSE_FACE draw in mouse face.
23253 DRAW_INVERSE_VIDEO draw in mode line face
23254 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23255 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23256
23257 If OVERLAPS is non-zero, draw only the foreground of characters and
23258 clip to the physical height of ROW. Non-zero value also defines
23259 the overlapping part to be drawn:
23260
23261 OVERLAPS_PRED overlap with preceding rows
23262 OVERLAPS_SUCC overlap with succeeding rows
23263 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23264 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23265
23266 Value is the x-position reached, relative to AREA of W. */
23267
23268 static int
23269 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23270 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23271 enum draw_glyphs_face hl, int overlaps)
23272 {
23273 struct glyph_string *head, *tail;
23274 struct glyph_string *s;
23275 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23276 int i, j, x_reached, last_x, area_left = 0;
23277 struct frame *f = XFRAME (WINDOW_FRAME (w));
23278 DECLARE_HDC (hdc);
23279
23280 ALLOCATE_HDC (hdc, f);
23281
23282 /* Let's rather be paranoid than getting a SEGV. */
23283 end = min (end, row->used[area]);
23284 start = clip_to_bounds (0, start, end);
23285
23286 /* Translate X to frame coordinates. Set last_x to the right
23287 end of the drawing area. */
23288 if (row->full_width_p)
23289 {
23290 /* X is relative to the left edge of W, without scroll bars
23291 or fringes. */
23292 area_left = WINDOW_LEFT_EDGE_X (w);
23293 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23294 }
23295 else
23296 {
23297 area_left = window_box_left (w, area);
23298 last_x = area_left + window_box_width (w, area);
23299 }
23300 x += area_left;
23301
23302 /* Build a doubly-linked list of glyph_string structures between
23303 head and tail from what we have to draw. Note that the macro
23304 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23305 the reason we use a separate variable `i'. */
23306 i = start;
23307 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23308 if (tail)
23309 x_reached = tail->x + tail->background_width;
23310 else
23311 x_reached = x;
23312
23313 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23314 the row, redraw some glyphs in front or following the glyph
23315 strings built above. */
23316 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23317 {
23318 struct glyph_string *h, *t;
23319 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23320 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23321 int check_mouse_face = 0;
23322 int dummy_x = 0;
23323
23324 /* If mouse highlighting is on, we may need to draw adjacent
23325 glyphs using mouse-face highlighting. */
23326 if (area == TEXT_AREA && row->mouse_face_p
23327 && hlinfo->mouse_face_beg_row >= 0
23328 && hlinfo->mouse_face_end_row >= 0)
23329 {
23330 struct glyph_row *mouse_beg_row, *mouse_end_row;
23331
23332 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23333 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23334
23335 if (row >= mouse_beg_row && row <= mouse_end_row)
23336 {
23337 check_mouse_face = 1;
23338 mouse_beg_col = (row == mouse_beg_row)
23339 ? hlinfo->mouse_face_beg_col : 0;
23340 mouse_end_col = (row == mouse_end_row)
23341 ? hlinfo->mouse_face_end_col
23342 : row->used[TEXT_AREA];
23343 }
23344 }
23345
23346 /* Compute overhangs for all glyph strings. */
23347 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23348 for (s = head; s; s = s->next)
23349 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23350
23351 /* Prepend glyph strings for glyphs in front of the first glyph
23352 string that are overwritten because of the first glyph
23353 string's left overhang. The background of all strings
23354 prepended must be drawn because the first glyph string
23355 draws over it. */
23356 i = left_overwritten (head);
23357 if (i >= 0)
23358 {
23359 enum draw_glyphs_face overlap_hl;
23360
23361 /* If this row contains mouse highlighting, attempt to draw
23362 the overlapped glyphs with the correct highlight. This
23363 code fails if the overlap encompasses more than one glyph
23364 and mouse-highlight spans only some of these glyphs.
23365 However, making it work perfectly involves a lot more
23366 code, and I don't know if the pathological case occurs in
23367 practice, so we'll stick to this for now. --- cyd */
23368 if (check_mouse_face
23369 && mouse_beg_col < start && mouse_end_col > i)
23370 overlap_hl = DRAW_MOUSE_FACE;
23371 else
23372 overlap_hl = DRAW_NORMAL_TEXT;
23373
23374 j = i;
23375 BUILD_GLYPH_STRINGS (j, start, h, t,
23376 overlap_hl, dummy_x, last_x);
23377 start = i;
23378 compute_overhangs_and_x (t, head->x, 1);
23379 prepend_glyph_string_lists (&head, &tail, h, t);
23380 clip_head = head;
23381 }
23382
23383 /* Prepend glyph strings for glyphs in front of the first glyph
23384 string that overwrite that glyph string because of their
23385 right overhang. For these strings, only the foreground must
23386 be drawn, because it draws over the glyph string at `head'.
23387 The background must not be drawn because this would overwrite
23388 right overhangs of preceding glyphs for which no glyph
23389 strings exist. */
23390 i = left_overwriting (head);
23391 if (i >= 0)
23392 {
23393 enum draw_glyphs_face overlap_hl;
23394
23395 if (check_mouse_face
23396 && mouse_beg_col < start && mouse_end_col > i)
23397 overlap_hl = DRAW_MOUSE_FACE;
23398 else
23399 overlap_hl = DRAW_NORMAL_TEXT;
23400
23401 clip_head = head;
23402 BUILD_GLYPH_STRINGS (i, start, h, t,
23403 overlap_hl, dummy_x, last_x);
23404 for (s = h; s; s = s->next)
23405 s->background_filled_p = 1;
23406 compute_overhangs_and_x (t, head->x, 1);
23407 prepend_glyph_string_lists (&head, &tail, h, t);
23408 }
23409
23410 /* Append glyphs strings for glyphs following the last glyph
23411 string tail that are overwritten by tail. The background of
23412 these strings has to be drawn because tail's foreground draws
23413 over it. */
23414 i = right_overwritten (tail);
23415 if (i >= 0)
23416 {
23417 enum draw_glyphs_face overlap_hl;
23418
23419 if (check_mouse_face
23420 && mouse_beg_col < i && mouse_end_col > end)
23421 overlap_hl = DRAW_MOUSE_FACE;
23422 else
23423 overlap_hl = DRAW_NORMAL_TEXT;
23424
23425 BUILD_GLYPH_STRINGS (end, i, h, t,
23426 overlap_hl, x, last_x);
23427 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23428 we don't have `end = i;' here. */
23429 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23430 append_glyph_string_lists (&head, &tail, h, t);
23431 clip_tail = tail;
23432 }
23433
23434 /* Append glyph strings for glyphs following the last glyph
23435 string tail that overwrite tail. The foreground of such
23436 glyphs has to be drawn because it writes into the background
23437 of tail. The background must not be drawn because it could
23438 paint over the foreground of following glyphs. */
23439 i = right_overwriting (tail);
23440 if (i >= 0)
23441 {
23442 enum draw_glyphs_face overlap_hl;
23443 if (check_mouse_face
23444 && mouse_beg_col < i && mouse_end_col > end)
23445 overlap_hl = DRAW_MOUSE_FACE;
23446 else
23447 overlap_hl = DRAW_NORMAL_TEXT;
23448
23449 clip_tail = tail;
23450 i++; /* We must include the Ith glyph. */
23451 BUILD_GLYPH_STRINGS (end, i, h, t,
23452 overlap_hl, x, last_x);
23453 for (s = h; s; s = s->next)
23454 s->background_filled_p = 1;
23455 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23456 append_glyph_string_lists (&head, &tail, h, t);
23457 }
23458 if (clip_head || clip_tail)
23459 for (s = head; s; s = s->next)
23460 {
23461 s->clip_head = clip_head;
23462 s->clip_tail = clip_tail;
23463 }
23464 }
23465
23466 /* Draw all strings. */
23467 for (s = head; s; s = s->next)
23468 FRAME_RIF (f)->draw_glyph_string (s);
23469
23470 #ifndef HAVE_NS
23471 /* When focus a sole frame and move horizontally, this sets on_p to 0
23472 causing a failure to erase prev cursor position. */
23473 if (area == TEXT_AREA
23474 && !row->full_width_p
23475 /* When drawing overlapping rows, only the glyph strings'
23476 foreground is drawn, which doesn't erase a cursor
23477 completely. */
23478 && !overlaps)
23479 {
23480 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23481 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23482 : (tail ? tail->x + tail->background_width : x));
23483 x0 -= area_left;
23484 x1 -= area_left;
23485
23486 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23487 row->y, MATRIX_ROW_BOTTOM_Y (row));
23488 }
23489 #endif
23490
23491 /* Value is the x-position up to which drawn, relative to AREA of W.
23492 This doesn't include parts drawn because of overhangs. */
23493 if (row->full_width_p)
23494 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23495 else
23496 x_reached -= area_left;
23497
23498 RELEASE_HDC (hdc, f);
23499
23500 return x_reached;
23501 }
23502
23503 /* Expand row matrix if too narrow. Don't expand if area
23504 is not present. */
23505
23506 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23507 { \
23508 if (!fonts_changed_p \
23509 && (it->glyph_row->glyphs[area] \
23510 < it->glyph_row->glyphs[area + 1])) \
23511 { \
23512 it->w->ncols_scale_factor++; \
23513 fonts_changed_p = 1; \
23514 } \
23515 }
23516
23517 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23518 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23519
23520 static void
23521 append_glyph (struct it *it)
23522 {
23523 struct glyph *glyph;
23524 enum glyph_row_area area = it->area;
23525
23526 eassert (it->glyph_row);
23527 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23528
23529 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23530 if (glyph < it->glyph_row->glyphs[area + 1])
23531 {
23532 /* If the glyph row is reversed, we need to prepend the glyph
23533 rather than append it. */
23534 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23535 {
23536 struct glyph *g;
23537
23538 /* Make room for the additional glyph. */
23539 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23540 g[1] = *g;
23541 glyph = it->glyph_row->glyphs[area];
23542 }
23543 glyph->charpos = CHARPOS (it->position);
23544 glyph->object = it->object;
23545 if (it->pixel_width > 0)
23546 {
23547 glyph->pixel_width = it->pixel_width;
23548 glyph->padding_p = 0;
23549 }
23550 else
23551 {
23552 /* Assure at least 1-pixel width. Otherwise, cursor can't
23553 be displayed correctly. */
23554 glyph->pixel_width = 1;
23555 glyph->padding_p = 1;
23556 }
23557 glyph->ascent = it->ascent;
23558 glyph->descent = it->descent;
23559 glyph->voffset = it->voffset;
23560 glyph->type = CHAR_GLYPH;
23561 glyph->avoid_cursor_p = it->avoid_cursor_p;
23562 glyph->multibyte_p = it->multibyte_p;
23563 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23564 {
23565 /* In R2L rows, the left and the right box edges need to be
23566 drawn in reverse direction. */
23567 glyph->right_box_line_p = it->start_of_box_run_p;
23568 glyph->left_box_line_p = it->end_of_box_run_p;
23569 }
23570 else
23571 {
23572 glyph->left_box_line_p = it->start_of_box_run_p;
23573 glyph->right_box_line_p = it->end_of_box_run_p;
23574 }
23575 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23576 || it->phys_descent > it->descent);
23577 glyph->glyph_not_available_p = it->glyph_not_available_p;
23578 glyph->face_id = it->face_id;
23579 glyph->u.ch = it->char_to_display;
23580 glyph->slice.img = null_glyph_slice;
23581 glyph->font_type = FONT_TYPE_UNKNOWN;
23582 if (it->bidi_p)
23583 {
23584 glyph->resolved_level = it->bidi_it.resolved_level;
23585 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23586 emacs_abort ();
23587 glyph->bidi_type = it->bidi_it.type;
23588 }
23589 else
23590 {
23591 glyph->resolved_level = 0;
23592 glyph->bidi_type = UNKNOWN_BT;
23593 }
23594 ++it->glyph_row->used[area];
23595 }
23596 else
23597 IT_EXPAND_MATRIX_WIDTH (it, area);
23598 }
23599
23600 /* Store one glyph for the composition IT->cmp_it.id in
23601 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23602 non-null. */
23603
23604 static void
23605 append_composite_glyph (struct it *it)
23606 {
23607 struct glyph *glyph;
23608 enum glyph_row_area area = it->area;
23609
23610 eassert (it->glyph_row);
23611
23612 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23613 if (glyph < it->glyph_row->glyphs[area + 1])
23614 {
23615 /* If the glyph row is reversed, we need to prepend the glyph
23616 rather than append it. */
23617 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23618 {
23619 struct glyph *g;
23620
23621 /* Make room for the new glyph. */
23622 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23623 g[1] = *g;
23624 glyph = it->glyph_row->glyphs[it->area];
23625 }
23626 glyph->charpos = it->cmp_it.charpos;
23627 glyph->object = it->object;
23628 glyph->pixel_width = it->pixel_width;
23629 glyph->ascent = it->ascent;
23630 glyph->descent = it->descent;
23631 glyph->voffset = it->voffset;
23632 glyph->type = COMPOSITE_GLYPH;
23633 if (it->cmp_it.ch < 0)
23634 {
23635 glyph->u.cmp.automatic = 0;
23636 glyph->u.cmp.id = it->cmp_it.id;
23637 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23638 }
23639 else
23640 {
23641 glyph->u.cmp.automatic = 1;
23642 glyph->u.cmp.id = it->cmp_it.id;
23643 glyph->slice.cmp.from = it->cmp_it.from;
23644 glyph->slice.cmp.to = it->cmp_it.to - 1;
23645 }
23646 glyph->avoid_cursor_p = it->avoid_cursor_p;
23647 glyph->multibyte_p = it->multibyte_p;
23648 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23649 {
23650 /* In R2L rows, the left and the right box edges need to be
23651 drawn in reverse direction. */
23652 glyph->right_box_line_p = it->start_of_box_run_p;
23653 glyph->left_box_line_p = it->end_of_box_run_p;
23654 }
23655 else
23656 {
23657 glyph->left_box_line_p = it->start_of_box_run_p;
23658 glyph->right_box_line_p = it->end_of_box_run_p;
23659 }
23660 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23661 || it->phys_descent > it->descent);
23662 glyph->padding_p = 0;
23663 glyph->glyph_not_available_p = 0;
23664 glyph->face_id = it->face_id;
23665 glyph->font_type = FONT_TYPE_UNKNOWN;
23666 if (it->bidi_p)
23667 {
23668 glyph->resolved_level = it->bidi_it.resolved_level;
23669 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23670 emacs_abort ();
23671 glyph->bidi_type = it->bidi_it.type;
23672 }
23673 ++it->glyph_row->used[area];
23674 }
23675 else
23676 IT_EXPAND_MATRIX_WIDTH (it, area);
23677 }
23678
23679
23680 /* Change IT->ascent and IT->height according to the setting of
23681 IT->voffset. */
23682
23683 static void
23684 take_vertical_position_into_account (struct it *it)
23685 {
23686 if (it->voffset)
23687 {
23688 if (it->voffset < 0)
23689 /* Increase the ascent so that we can display the text higher
23690 in the line. */
23691 it->ascent -= it->voffset;
23692 else
23693 /* Increase the descent so that we can display the text lower
23694 in the line. */
23695 it->descent += it->voffset;
23696 }
23697 }
23698
23699
23700 /* Produce glyphs/get display metrics for the image IT is loaded with.
23701 See the description of struct display_iterator in dispextern.h for
23702 an overview of struct display_iterator. */
23703
23704 static void
23705 produce_image_glyph (struct it *it)
23706 {
23707 struct image *img;
23708 struct face *face;
23709 int glyph_ascent, crop;
23710 struct glyph_slice slice;
23711
23712 eassert (it->what == IT_IMAGE);
23713
23714 face = FACE_FROM_ID (it->f, it->face_id);
23715 eassert (face);
23716 /* Make sure X resources of the face is loaded. */
23717 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23718
23719 if (it->image_id < 0)
23720 {
23721 /* Fringe bitmap. */
23722 it->ascent = it->phys_ascent = 0;
23723 it->descent = it->phys_descent = 0;
23724 it->pixel_width = 0;
23725 it->nglyphs = 0;
23726 return;
23727 }
23728
23729 img = IMAGE_FROM_ID (it->f, it->image_id);
23730 eassert (img);
23731 /* Make sure X resources of the image is loaded. */
23732 prepare_image_for_display (it->f, img);
23733
23734 slice.x = slice.y = 0;
23735 slice.width = img->width;
23736 slice.height = img->height;
23737
23738 if (INTEGERP (it->slice.x))
23739 slice.x = XINT (it->slice.x);
23740 else if (FLOATP (it->slice.x))
23741 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23742
23743 if (INTEGERP (it->slice.y))
23744 slice.y = XINT (it->slice.y);
23745 else if (FLOATP (it->slice.y))
23746 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23747
23748 if (INTEGERP (it->slice.width))
23749 slice.width = XINT (it->slice.width);
23750 else if (FLOATP (it->slice.width))
23751 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23752
23753 if (INTEGERP (it->slice.height))
23754 slice.height = XINT (it->slice.height);
23755 else if (FLOATP (it->slice.height))
23756 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23757
23758 if (slice.x >= img->width)
23759 slice.x = img->width;
23760 if (slice.y >= img->height)
23761 slice.y = img->height;
23762 if (slice.x + slice.width >= img->width)
23763 slice.width = img->width - slice.x;
23764 if (slice.y + slice.height > img->height)
23765 slice.height = img->height - slice.y;
23766
23767 if (slice.width == 0 || slice.height == 0)
23768 return;
23769
23770 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23771
23772 it->descent = slice.height - glyph_ascent;
23773 if (slice.y == 0)
23774 it->descent += img->vmargin;
23775 if (slice.y + slice.height == img->height)
23776 it->descent += img->vmargin;
23777 it->phys_descent = it->descent;
23778
23779 it->pixel_width = slice.width;
23780 if (slice.x == 0)
23781 it->pixel_width += img->hmargin;
23782 if (slice.x + slice.width == img->width)
23783 it->pixel_width += img->hmargin;
23784
23785 /* It's quite possible for images to have an ascent greater than
23786 their height, so don't get confused in that case. */
23787 if (it->descent < 0)
23788 it->descent = 0;
23789
23790 it->nglyphs = 1;
23791
23792 if (face->box != FACE_NO_BOX)
23793 {
23794 if (face->box_line_width > 0)
23795 {
23796 if (slice.y == 0)
23797 it->ascent += face->box_line_width;
23798 if (slice.y + slice.height == img->height)
23799 it->descent += face->box_line_width;
23800 }
23801
23802 if (it->start_of_box_run_p && slice.x == 0)
23803 it->pixel_width += eabs (face->box_line_width);
23804 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23805 it->pixel_width += eabs (face->box_line_width);
23806 }
23807
23808 take_vertical_position_into_account (it);
23809
23810 /* Automatically crop wide image glyphs at right edge so we can
23811 draw the cursor on same display row. */
23812 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23813 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23814 {
23815 it->pixel_width -= crop;
23816 slice.width -= crop;
23817 }
23818
23819 if (it->glyph_row)
23820 {
23821 struct glyph *glyph;
23822 enum glyph_row_area area = it->area;
23823
23824 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23825 if (glyph < it->glyph_row->glyphs[area + 1])
23826 {
23827 glyph->charpos = CHARPOS (it->position);
23828 glyph->object = it->object;
23829 glyph->pixel_width = it->pixel_width;
23830 glyph->ascent = glyph_ascent;
23831 glyph->descent = it->descent;
23832 glyph->voffset = it->voffset;
23833 glyph->type = IMAGE_GLYPH;
23834 glyph->avoid_cursor_p = it->avoid_cursor_p;
23835 glyph->multibyte_p = it->multibyte_p;
23836 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23837 {
23838 /* In R2L rows, the left and the right box edges need to be
23839 drawn in reverse direction. */
23840 glyph->right_box_line_p = it->start_of_box_run_p;
23841 glyph->left_box_line_p = it->end_of_box_run_p;
23842 }
23843 else
23844 {
23845 glyph->left_box_line_p = it->start_of_box_run_p;
23846 glyph->right_box_line_p = it->end_of_box_run_p;
23847 }
23848 glyph->overlaps_vertically_p = 0;
23849 glyph->padding_p = 0;
23850 glyph->glyph_not_available_p = 0;
23851 glyph->face_id = it->face_id;
23852 glyph->u.img_id = img->id;
23853 glyph->slice.img = slice;
23854 glyph->font_type = FONT_TYPE_UNKNOWN;
23855 if (it->bidi_p)
23856 {
23857 glyph->resolved_level = it->bidi_it.resolved_level;
23858 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23859 emacs_abort ();
23860 glyph->bidi_type = it->bidi_it.type;
23861 }
23862 ++it->glyph_row->used[area];
23863 }
23864 else
23865 IT_EXPAND_MATRIX_WIDTH (it, area);
23866 }
23867 }
23868
23869
23870 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23871 of the glyph, WIDTH and HEIGHT are the width and height of the
23872 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23873
23874 static void
23875 append_stretch_glyph (struct it *it, Lisp_Object object,
23876 int width, int height, int ascent)
23877 {
23878 struct glyph *glyph;
23879 enum glyph_row_area area = it->area;
23880
23881 eassert (ascent >= 0 && ascent <= height);
23882
23883 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23884 if (glyph < it->glyph_row->glyphs[area + 1])
23885 {
23886 /* If the glyph row is reversed, we need to prepend the glyph
23887 rather than append it. */
23888 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23889 {
23890 struct glyph *g;
23891
23892 /* Make room for the additional glyph. */
23893 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23894 g[1] = *g;
23895 glyph = it->glyph_row->glyphs[area];
23896 }
23897 glyph->charpos = CHARPOS (it->position);
23898 glyph->object = object;
23899 glyph->pixel_width = width;
23900 glyph->ascent = ascent;
23901 glyph->descent = height - ascent;
23902 glyph->voffset = it->voffset;
23903 glyph->type = STRETCH_GLYPH;
23904 glyph->avoid_cursor_p = it->avoid_cursor_p;
23905 glyph->multibyte_p = it->multibyte_p;
23906 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23907 {
23908 /* In R2L rows, the left and the right box edges need to be
23909 drawn in reverse direction. */
23910 glyph->right_box_line_p = it->start_of_box_run_p;
23911 glyph->left_box_line_p = it->end_of_box_run_p;
23912 }
23913 else
23914 {
23915 glyph->left_box_line_p = it->start_of_box_run_p;
23916 glyph->right_box_line_p = it->end_of_box_run_p;
23917 }
23918 glyph->overlaps_vertically_p = 0;
23919 glyph->padding_p = 0;
23920 glyph->glyph_not_available_p = 0;
23921 glyph->face_id = it->face_id;
23922 glyph->u.stretch.ascent = ascent;
23923 glyph->u.stretch.height = height;
23924 glyph->slice.img = null_glyph_slice;
23925 glyph->font_type = FONT_TYPE_UNKNOWN;
23926 if (it->bidi_p)
23927 {
23928 glyph->resolved_level = it->bidi_it.resolved_level;
23929 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23930 emacs_abort ();
23931 glyph->bidi_type = it->bidi_it.type;
23932 }
23933 else
23934 {
23935 glyph->resolved_level = 0;
23936 glyph->bidi_type = UNKNOWN_BT;
23937 }
23938 ++it->glyph_row->used[area];
23939 }
23940 else
23941 IT_EXPAND_MATRIX_WIDTH (it, area);
23942 }
23943
23944 #endif /* HAVE_WINDOW_SYSTEM */
23945
23946 /* Produce a stretch glyph for iterator IT. IT->object is the value
23947 of the glyph property displayed. The value must be a list
23948 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23949 being recognized:
23950
23951 1. `:width WIDTH' specifies that the space should be WIDTH *
23952 canonical char width wide. WIDTH may be an integer or floating
23953 point number.
23954
23955 2. `:relative-width FACTOR' specifies that the width of the stretch
23956 should be computed from the width of the first character having the
23957 `glyph' property, and should be FACTOR times that width.
23958
23959 3. `:align-to HPOS' specifies that the space should be wide enough
23960 to reach HPOS, a value in canonical character units.
23961
23962 Exactly one of the above pairs must be present.
23963
23964 4. `:height HEIGHT' specifies that the height of the stretch produced
23965 should be HEIGHT, measured in canonical character units.
23966
23967 5. `:relative-height FACTOR' specifies that the height of the
23968 stretch should be FACTOR times the height of the characters having
23969 the glyph property.
23970
23971 Either none or exactly one of 4 or 5 must be present.
23972
23973 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23974 of the stretch should be used for the ascent of the stretch.
23975 ASCENT must be in the range 0 <= ASCENT <= 100. */
23976
23977 void
23978 produce_stretch_glyph (struct it *it)
23979 {
23980 /* (space :width WIDTH :height HEIGHT ...) */
23981 Lisp_Object prop, plist;
23982 int width = 0, height = 0, align_to = -1;
23983 int zero_width_ok_p = 0;
23984 double tem;
23985 struct font *font = NULL;
23986
23987 #ifdef HAVE_WINDOW_SYSTEM
23988 int ascent = 0;
23989 int zero_height_ok_p = 0;
23990
23991 if (FRAME_WINDOW_P (it->f))
23992 {
23993 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23994 font = face->font ? face->font : FRAME_FONT (it->f);
23995 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23996 }
23997 #endif
23998
23999 /* List should start with `space'. */
24000 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24001 plist = XCDR (it->object);
24002
24003 /* Compute the width of the stretch. */
24004 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24005 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24006 {
24007 /* Absolute width `:width WIDTH' specified and valid. */
24008 zero_width_ok_p = 1;
24009 width = (int)tem;
24010 }
24011 #ifdef HAVE_WINDOW_SYSTEM
24012 else if (FRAME_WINDOW_P (it->f)
24013 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24014 {
24015 /* Relative width `:relative-width FACTOR' specified and valid.
24016 Compute the width of the characters having the `glyph'
24017 property. */
24018 struct it it2;
24019 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24020
24021 it2 = *it;
24022 if (it->multibyte_p)
24023 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24024 else
24025 {
24026 it2.c = it2.char_to_display = *p, it2.len = 1;
24027 if (! ASCII_CHAR_P (it2.c))
24028 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24029 }
24030
24031 it2.glyph_row = NULL;
24032 it2.what = IT_CHARACTER;
24033 x_produce_glyphs (&it2);
24034 width = NUMVAL (prop) * it2.pixel_width;
24035 }
24036 #endif /* HAVE_WINDOW_SYSTEM */
24037 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24038 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24039 {
24040 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24041 align_to = (align_to < 0
24042 ? 0
24043 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24044 else if (align_to < 0)
24045 align_to = window_box_left_offset (it->w, TEXT_AREA);
24046 width = max (0, (int)tem + align_to - it->current_x);
24047 zero_width_ok_p = 1;
24048 }
24049 else
24050 /* Nothing specified -> width defaults to canonical char width. */
24051 width = FRAME_COLUMN_WIDTH (it->f);
24052
24053 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24054 width = 1;
24055
24056 #ifdef HAVE_WINDOW_SYSTEM
24057 /* Compute height. */
24058 if (FRAME_WINDOW_P (it->f))
24059 {
24060 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24061 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24062 {
24063 height = (int)tem;
24064 zero_height_ok_p = 1;
24065 }
24066 else if (prop = Fplist_get (plist, QCrelative_height),
24067 NUMVAL (prop) > 0)
24068 height = FONT_HEIGHT (font) * NUMVAL (prop);
24069 else
24070 height = FONT_HEIGHT (font);
24071
24072 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24073 height = 1;
24074
24075 /* Compute percentage of height used for ascent. If
24076 `:ascent ASCENT' is present and valid, use that. Otherwise,
24077 derive the ascent from the font in use. */
24078 if (prop = Fplist_get (plist, QCascent),
24079 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24080 ascent = height * NUMVAL (prop) / 100.0;
24081 else if (!NILP (prop)
24082 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24083 ascent = min (max (0, (int)tem), height);
24084 else
24085 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24086 }
24087 else
24088 #endif /* HAVE_WINDOW_SYSTEM */
24089 height = 1;
24090
24091 if (width > 0 && it->line_wrap != TRUNCATE
24092 && it->current_x + width > it->last_visible_x)
24093 {
24094 width = it->last_visible_x - it->current_x;
24095 #ifdef HAVE_WINDOW_SYSTEM
24096 /* Subtract one more pixel from the stretch width, but only on
24097 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24098 width -= FRAME_WINDOW_P (it->f);
24099 #endif
24100 }
24101
24102 if (width > 0 && height > 0 && it->glyph_row)
24103 {
24104 Lisp_Object o_object = it->object;
24105 Lisp_Object object = it->stack[it->sp - 1].string;
24106 int n = width;
24107
24108 if (!STRINGP (object))
24109 object = it->w->contents;
24110 #ifdef HAVE_WINDOW_SYSTEM
24111 if (FRAME_WINDOW_P (it->f))
24112 append_stretch_glyph (it, object, width, height, ascent);
24113 else
24114 #endif
24115 {
24116 it->object = object;
24117 it->char_to_display = ' ';
24118 it->pixel_width = it->len = 1;
24119 while (n--)
24120 tty_append_glyph (it);
24121 it->object = o_object;
24122 }
24123 }
24124
24125 it->pixel_width = width;
24126 #ifdef HAVE_WINDOW_SYSTEM
24127 if (FRAME_WINDOW_P (it->f))
24128 {
24129 it->ascent = it->phys_ascent = ascent;
24130 it->descent = it->phys_descent = height - it->ascent;
24131 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24132 take_vertical_position_into_account (it);
24133 }
24134 else
24135 #endif
24136 it->nglyphs = width;
24137 }
24138
24139 /* Get information about special display element WHAT in an
24140 environment described by IT. WHAT is one of IT_TRUNCATION or
24141 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24142 non-null glyph_row member. This function ensures that fields like
24143 face_id, c, len of IT are left untouched. */
24144
24145 static void
24146 produce_special_glyphs (struct it *it, enum display_element_type what)
24147 {
24148 struct it temp_it;
24149 Lisp_Object gc;
24150 GLYPH glyph;
24151
24152 temp_it = *it;
24153 temp_it.object = make_number (0);
24154 memset (&temp_it.current, 0, sizeof temp_it.current);
24155
24156 if (what == IT_CONTINUATION)
24157 {
24158 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24159 if (it->bidi_it.paragraph_dir == R2L)
24160 SET_GLYPH_FROM_CHAR (glyph, '/');
24161 else
24162 SET_GLYPH_FROM_CHAR (glyph, '\\');
24163 if (it->dp
24164 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24165 {
24166 /* FIXME: Should we mirror GC for R2L lines? */
24167 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24168 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24169 }
24170 }
24171 else if (what == IT_TRUNCATION)
24172 {
24173 /* Truncation glyph. */
24174 SET_GLYPH_FROM_CHAR (glyph, '$');
24175 if (it->dp
24176 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24177 {
24178 /* FIXME: Should we mirror GC for R2L lines? */
24179 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24180 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24181 }
24182 }
24183 else
24184 emacs_abort ();
24185
24186 #ifdef HAVE_WINDOW_SYSTEM
24187 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24188 is turned off, we precede the truncation/continuation glyphs by a
24189 stretch glyph whose width is computed such that these special
24190 glyphs are aligned at the window margin, even when very different
24191 fonts are used in different glyph rows. */
24192 if (FRAME_WINDOW_P (temp_it.f)
24193 /* init_iterator calls this with it->glyph_row == NULL, and it
24194 wants only the pixel width of the truncation/continuation
24195 glyphs. */
24196 && temp_it.glyph_row
24197 /* insert_left_trunc_glyphs calls us at the beginning of the
24198 row, and it has its own calculation of the stretch glyph
24199 width. */
24200 && temp_it.glyph_row->used[TEXT_AREA] > 0
24201 && (temp_it.glyph_row->reversed_p
24202 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24203 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24204 {
24205 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24206
24207 if (stretch_width > 0)
24208 {
24209 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24210 struct font *font =
24211 face->font ? face->font : FRAME_FONT (temp_it.f);
24212 int stretch_ascent =
24213 (((temp_it.ascent + temp_it.descent)
24214 * FONT_BASE (font)) / FONT_HEIGHT (font));
24215
24216 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24217 temp_it.ascent + temp_it.descent,
24218 stretch_ascent);
24219 }
24220 }
24221 #endif
24222
24223 temp_it.dp = NULL;
24224 temp_it.what = IT_CHARACTER;
24225 temp_it.len = 1;
24226 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24227 temp_it.face_id = GLYPH_FACE (glyph);
24228 temp_it.len = CHAR_BYTES (temp_it.c);
24229
24230 PRODUCE_GLYPHS (&temp_it);
24231 it->pixel_width = temp_it.pixel_width;
24232 it->nglyphs = temp_it.pixel_width;
24233 }
24234
24235 #ifdef HAVE_WINDOW_SYSTEM
24236
24237 /* Calculate line-height and line-spacing properties.
24238 An integer value specifies explicit pixel value.
24239 A float value specifies relative value to current face height.
24240 A cons (float . face-name) specifies relative value to
24241 height of specified face font.
24242
24243 Returns height in pixels, or nil. */
24244
24245
24246 static Lisp_Object
24247 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24248 int boff, int override)
24249 {
24250 Lisp_Object face_name = Qnil;
24251 int ascent, descent, height;
24252
24253 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24254 return val;
24255
24256 if (CONSP (val))
24257 {
24258 face_name = XCAR (val);
24259 val = XCDR (val);
24260 if (!NUMBERP (val))
24261 val = make_number (1);
24262 if (NILP (face_name))
24263 {
24264 height = it->ascent + it->descent;
24265 goto scale;
24266 }
24267 }
24268
24269 if (NILP (face_name))
24270 {
24271 font = FRAME_FONT (it->f);
24272 boff = FRAME_BASELINE_OFFSET (it->f);
24273 }
24274 else if (EQ (face_name, Qt))
24275 {
24276 override = 0;
24277 }
24278 else
24279 {
24280 int face_id;
24281 struct face *face;
24282
24283 face_id = lookup_named_face (it->f, face_name, 0);
24284 if (face_id < 0)
24285 return make_number (-1);
24286
24287 face = FACE_FROM_ID (it->f, face_id);
24288 font = face->font;
24289 if (font == NULL)
24290 return make_number (-1);
24291 boff = font->baseline_offset;
24292 if (font->vertical_centering)
24293 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24294 }
24295
24296 ascent = FONT_BASE (font) + boff;
24297 descent = FONT_DESCENT (font) - boff;
24298
24299 if (override)
24300 {
24301 it->override_ascent = ascent;
24302 it->override_descent = descent;
24303 it->override_boff = boff;
24304 }
24305
24306 height = ascent + descent;
24307
24308 scale:
24309 if (FLOATP (val))
24310 height = (int)(XFLOAT_DATA (val) * height);
24311 else if (INTEGERP (val))
24312 height *= XINT (val);
24313
24314 return make_number (height);
24315 }
24316
24317
24318 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24319 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24320 and only if this is for a character for which no font was found.
24321
24322 If the display method (it->glyphless_method) is
24323 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24324 length of the acronym or the hexadecimal string, UPPER_XOFF and
24325 UPPER_YOFF are pixel offsets for the upper part of the string,
24326 LOWER_XOFF and LOWER_YOFF are for the lower part.
24327
24328 For the other display methods, LEN through LOWER_YOFF are zero. */
24329
24330 static void
24331 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24332 short upper_xoff, short upper_yoff,
24333 short lower_xoff, short lower_yoff)
24334 {
24335 struct glyph *glyph;
24336 enum glyph_row_area area = it->area;
24337
24338 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24339 if (glyph < it->glyph_row->glyphs[area + 1])
24340 {
24341 /* If the glyph row is reversed, we need to prepend the glyph
24342 rather than append it. */
24343 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24344 {
24345 struct glyph *g;
24346
24347 /* Make room for the additional glyph. */
24348 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24349 g[1] = *g;
24350 glyph = it->glyph_row->glyphs[area];
24351 }
24352 glyph->charpos = CHARPOS (it->position);
24353 glyph->object = it->object;
24354 glyph->pixel_width = it->pixel_width;
24355 glyph->ascent = it->ascent;
24356 glyph->descent = it->descent;
24357 glyph->voffset = it->voffset;
24358 glyph->type = GLYPHLESS_GLYPH;
24359 glyph->u.glyphless.method = it->glyphless_method;
24360 glyph->u.glyphless.for_no_font = for_no_font;
24361 glyph->u.glyphless.len = len;
24362 glyph->u.glyphless.ch = it->c;
24363 glyph->slice.glyphless.upper_xoff = upper_xoff;
24364 glyph->slice.glyphless.upper_yoff = upper_yoff;
24365 glyph->slice.glyphless.lower_xoff = lower_xoff;
24366 glyph->slice.glyphless.lower_yoff = lower_yoff;
24367 glyph->avoid_cursor_p = it->avoid_cursor_p;
24368 glyph->multibyte_p = it->multibyte_p;
24369 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24370 {
24371 /* In R2L rows, the left and the right box edges need to be
24372 drawn in reverse direction. */
24373 glyph->right_box_line_p = it->start_of_box_run_p;
24374 glyph->left_box_line_p = it->end_of_box_run_p;
24375 }
24376 else
24377 {
24378 glyph->left_box_line_p = it->start_of_box_run_p;
24379 glyph->right_box_line_p = it->end_of_box_run_p;
24380 }
24381 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24382 || it->phys_descent > it->descent);
24383 glyph->padding_p = 0;
24384 glyph->glyph_not_available_p = 0;
24385 glyph->face_id = face_id;
24386 glyph->font_type = FONT_TYPE_UNKNOWN;
24387 if (it->bidi_p)
24388 {
24389 glyph->resolved_level = it->bidi_it.resolved_level;
24390 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24391 emacs_abort ();
24392 glyph->bidi_type = it->bidi_it.type;
24393 }
24394 ++it->glyph_row->used[area];
24395 }
24396 else
24397 IT_EXPAND_MATRIX_WIDTH (it, area);
24398 }
24399
24400
24401 /* Produce a glyph for a glyphless character for iterator IT.
24402 IT->glyphless_method specifies which method to use for displaying
24403 the character. See the description of enum
24404 glyphless_display_method in dispextern.h for the detail.
24405
24406 FOR_NO_FONT is nonzero if and only if this is for a character for
24407 which no font was found. ACRONYM, if non-nil, is an acronym string
24408 for the character. */
24409
24410 static void
24411 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24412 {
24413 int face_id;
24414 struct face *face;
24415 struct font *font;
24416 int base_width, base_height, width, height;
24417 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24418 int len;
24419
24420 /* Get the metrics of the base font. We always refer to the current
24421 ASCII face. */
24422 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24423 font = face->font ? face->font : FRAME_FONT (it->f);
24424 it->ascent = FONT_BASE (font) + font->baseline_offset;
24425 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24426 base_height = it->ascent + it->descent;
24427 base_width = font->average_width;
24428
24429 /* Get a face ID for the glyph by utilizing a cache (the same way as
24430 done for `escape-glyph' in get_next_display_element). */
24431 if (it->f == last_glyphless_glyph_frame
24432 && it->face_id == last_glyphless_glyph_face_id)
24433 {
24434 face_id = last_glyphless_glyph_merged_face_id;
24435 }
24436 else
24437 {
24438 /* Merge the `glyphless-char' face into the current face. */
24439 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24440 last_glyphless_glyph_frame = it->f;
24441 last_glyphless_glyph_face_id = it->face_id;
24442 last_glyphless_glyph_merged_face_id = face_id;
24443 }
24444
24445 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24446 {
24447 it->pixel_width = THIN_SPACE_WIDTH;
24448 len = 0;
24449 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24450 }
24451 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24452 {
24453 width = CHAR_WIDTH (it->c);
24454 if (width == 0)
24455 width = 1;
24456 else if (width > 4)
24457 width = 4;
24458 it->pixel_width = base_width * width;
24459 len = 0;
24460 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24461 }
24462 else
24463 {
24464 char buf[7];
24465 const char *str;
24466 unsigned int code[6];
24467 int upper_len;
24468 int ascent, descent;
24469 struct font_metrics metrics_upper, metrics_lower;
24470
24471 face = FACE_FROM_ID (it->f, face_id);
24472 font = face->font ? face->font : FRAME_FONT (it->f);
24473 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24474
24475 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24476 {
24477 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24478 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24479 if (CONSP (acronym))
24480 acronym = XCAR (acronym);
24481 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24482 }
24483 else
24484 {
24485 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24486 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24487 str = buf;
24488 }
24489 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24490 code[len] = font->driver->encode_char (font, str[len]);
24491 upper_len = (len + 1) / 2;
24492 font->driver->text_extents (font, code, upper_len,
24493 &metrics_upper);
24494 font->driver->text_extents (font, code + upper_len, len - upper_len,
24495 &metrics_lower);
24496
24497
24498
24499 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24500 width = max (metrics_upper.width, metrics_lower.width) + 4;
24501 upper_xoff = upper_yoff = 2; /* the typical case */
24502 if (base_width >= width)
24503 {
24504 /* Align the upper to the left, the lower to the right. */
24505 it->pixel_width = base_width;
24506 lower_xoff = base_width - 2 - metrics_lower.width;
24507 }
24508 else
24509 {
24510 /* Center the shorter one. */
24511 it->pixel_width = width;
24512 if (metrics_upper.width >= metrics_lower.width)
24513 lower_xoff = (width - metrics_lower.width) / 2;
24514 else
24515 {
24516 /* FIXME: This code doesn't look right. It formerly was
24517 missing the "lower_xoff = 0;", which couldn't have
24518 been right since it left lower_xoff uninitialized. */
24519 lower_xoff = 0;
24520 upper_xoff = (width - metrics_upper.width) / 2;
24521 }
24522 }
24523
24524 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24525 top, bottom, and between upper and lower strings. */
24526 height = (metrics_upper.ascent + metrics_upper.descent
24527 + metrics_lower.ascent + metrics_lower.descent) + 5;
24528 /* Center vertically.
24529 H:base_height, D:base_descent
24530 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24531
24532 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24533 descent = D - H/2 + h/2;
24534 lower_yoff = descent - 2 - ld;
24535 upper_yoff = lower_yoff - la - 1 - ud; */
24536 ascent = - (it->descent - (base_height + height + 1) / 2);
24537 descent = it->descent - (base_height - height) / 2;
24538 lower_yoff = descent - 2 - metrics_lower.descent;
24539 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24540 - metrics_upper.descent);
24541 /* Don't make the height shorter than the base height. */
24542 if (height > base_height)
24543 {
24544 it->ascent = ascent;
24545 it->descent = descent;
24546 }
24547 }
24548
24549 it->phys_ascent = it->ascent;
24550 it->phys_descent = it->descent;
24551 if (it->glyph_row)
24552 append_glyphless_glyph (it, face_id, for_no_font, len,
24553 upper_xoff, upper_yoff,
24554 lower_xoff, lower_yoff);
24555 it->nglyphs = 1;
24556 take_vertical_position_into_account (it);
24557 }
24558
24559
24560 /* RIF:
24561 Produce glyphs/get display metrics for the display element IT is
24562 loaded with. See the description of struct it in dispextern.h
24563 for an overview of struct it. */
24564
24565 void
24566 x_produce_glyphs (struct it *it)
24567 {
24568 int extra_line_spacing = it->extra_line_spacing;
24569
24570 it->glyph_not_available_p = 0;
24571
24572 if (it->what == IT_CHARACTER)
24573 {
24574 XChar2b char2b;
24575 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24576 struct font *font = face->font;
24577 struct font_metrics *pcm = NULL;
24578 int boff; /* baseline offset */
24579
24580 if (font == NULL)
24581 {
24582 /* When no suitable font is found, display this character by
24583 the method specified in the first extra slot of
24584 Vglyphless_char_display. */
24585 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24586
24587 eassert (it->what == IT_GLYPHLESS);
24588 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24589 goto done;
24590 }
24591
24592 boff = font->baseline_offset;
24593 if (font->vertical_centering)
24594 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24595
24596 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24597 {
24598 int stretched_p;
24599
24600 it->nglyphs = 1;
24601
24602 if (it->override_ascent >= 0)
24603 {
24604 it->ascent = it->override_ascent;
24605 it->descent = it->override_descent;
24606 boff = it->override_boff;
24607 }
24608 else
24609 {
24610 it->ascent = FONT_BASE (font) + boff;
24611 it->descent = FONT_DESCENT (font) - boff;
24612 }
24613
24614 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24615 {
24616 pcm = get_per_char_metric (font, &char2b);
24617 if (pcm->width == 0
24618 && pcm->rbearing == 0 && pcm->lbearing == 0)
24619 pcm = NULL;
24620 }
24621
24622 if (pcm)
24623 {
24624 it->phys_ascent = pcm->ascent + boff;
24625 it->phys_descent = pcm->descent - boff;
24626 it->pixel_width = pcm->width;
24627 }
24628 else
24629 {
24630 it->glyph_not_available_p = 1;
24631 it->phys_ascent = it->ascent;
24632 it->phys_descent = it->descent;
24633 it->pixel_width = font->space_width;
24634 }
24635
24636 if (it->constrain_row_ascent_descent_p)
24637 {
24638 if (it->descent > it->max_descent)
24639 {
24640 it->ascent += it->descent - it->max_descent;
24641 it->descent = it->max_descent;
24642 }
24643 if (it->ascent > it->max_ascent)
24644 {
24645 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24646 it->ascent = it->max_ascent;
24647 }
24648 it->phys_ascent = min (it->phys_ascent, it->ascent);
24649 it->phys_descent = min (it->phys_descent, it->descent);
24650 extra_line_spacing = 0;
24651 }
24652
24653 /* If this is a space inside a region of text with
24654 `space-width' property, change its width. */
24655 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24656 if (stretched_p)
24657 it->pixel_width *= XFLOATINT (it->space_width);
24658
24659 /* If face has a box, add the box thickness to the character
24660 height. If character has a box line to the left and/or
24661 right, add the box line width to the character's width. */
24662 if (face->box != FACE_NO_BOX)
24663 {
24664 int thick = face->box_line_width;
24665
24666 if (thick > 0)
24667 {
24668 it->ascent += thick;
24669 it->descent += thick;
24670 }
24671 else
24672 thick = -thick;
24673
24674 if (it->start_of_box_run_p)
24675 it->pixel_width += thick;
24676 if (it->end_of_box_run_p)
24677 it->pixel_width += thick;
24678 }
24679
24680 /* If face has an overline, add the height of the overline
24681 (1 pixel) and a 1 pixel margin to the character height. */
24682 if (face->overline_p)
24683 it->ascent += overline_margin;
24684
24685 if (it->constrain_row_ascent_descent_p)
24686 {
24687 if (it->ascent > it->max_ascent)
24688 it->ascent = it->max_ascent;
24689 if (it->descent > it->max_descent)
24690 it->descent = it->max_descent;
24691 }
24692
24693 take_vertical_position_into_account (it);
24694
24695 /* If we have to actually produce glyphs, do it. */
24696 if (it->glyph_row)
24697 {
24698 if (stretched_p)
24699 {
24700 /* Translate a space with a `space-width' property
24701 into a stretch glyph. */
24702 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24703 / FONT_HEIGHT (font));
24704 append_stretch_glyph (it, it->object, it->pixel_width,
24705 it->ascent + it->descent, ascent);
24706 }
24707 else
24708 append_glyph (it);
24709
24710 /* If characters with lbearing or rbearing are displayed
24711 in this line, record that fact in a flag of the
24712 glyph row. This is used to optimize X output code. */
24713 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24714 it->glyph_row->contains_overlapping_glyphs_p = 1;
24715 }
24716 if (! stretched_p && it->pixel_width == 0)
24717 /* We assure that all visible glyphs have at least 1-pixel
24718 width. */
24719 it->pixel_width = 1;
24720 }
24721 else if (it->char_to_display == '\n')
24722 {
24723 /* A newline has no width, but we need the height of the
24724 line. But if previous part of the line sets a height,
24725 don't increase that height */
24726
24727 Lisp_Object height;
24728 Lisp_Object total_height = Qnil;
24729
24730 it->override_ascent = -1;
24731 it->pixel_width = 0;
24732 it->nglyphs = 0;
24733
24734 height = get_it_property (it, Qline_height);
24735 /* Split (line-height total-height) list */
24736 if (CONSP (height)
24737 && CONSP (XCDR (height))
24738 && NILP (XCDR (XCDR (height))))
24739 {
24740 total_height = XCAR (XCDR (height));
24741 height = XCAR (height);
24742 }
24743 height = calc_line_height_property (it, height, font, boff, 1);
24744
24745 if (it->override_ascent >= 0)
24746 {
24747 it->ascent = it->override_ascent;
24748 it->descent = it->override_descent;
24749 boff = it->override_boff;
24750 }
24751 else
24752 {
24753 it->ascent = FONT_BASE (font) + boff;
24754 it->descent = FONT_DESCENT (font) - boff;
24755 }
24756
24757 if (EQ (height, Qt))
24758 {
24759 if (it->descent > it->max_descent)
24760 {
24761 it->ascent += it->descent - it->max_descent;
24762 it->descent = it->max_descent;
24763 }
24764 if (it->ascent > it->max_ascent)
24765 {
24766 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24767 it->ascent = it->max_ascent;
24768 }
24769 it->phys_ascent = min (it->phys_ascent, it->ascent);
24770 it->phys_descent = min (it->phys_descent, it->descent);
24771 it->constrain_row_ascent_descent_p = 1;
24772 extra_line_spacing = 0;
24773 }
24774 else
24775 {
24776 Lisp_Object spacing;
24777
24778 it->phys_ascent = it->ascent;
24779 it->phys_descent = it->descent;
24780
24781 if ((it->max_ascent > 0 || it->max_descent > 0)
24782 && face->box != FACE_NO_BOX
24783 && face->box_line_width > 0)
24784 {
24785 it->ascent += face->box_line_width;
24786 it->descent += face->box_line_width;
24787 }
24788 if (!NILP (height)
24789 && XINT (height) > it->ascent + it->descent)
24790 it->ascent = XINT (height) - it->descent;
24791
24792 if (!NILP (total_height))
24793 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24794 else
24795 {
24796 spacing = get_it_property (it, Qline_spacing);
24797 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24798 }
24799 if (INTEGERP (spacing))
24800 {
24801 extra_line_spacing = XINT (spacing);
24802 if (!NILP (total_height))
24803 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24804 }
24805 }
24806 }
24807 else /* i.e. (it->char_to_display == '\t') */
24808 {
24809 if (font->space_width > 0)
24810 {
24811 int tab_width = it->tab_width * font->space_width;
24812 int x = it->current_x + it->continuation_lines_width;
24813 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24814
24815 /* If the distance from the current position to the next tab
24816 stop is less than a space character width, use the
24817 tab stop after that. */
24818 if (next_tab_x - x < font->space_width)
24819 next_tab_x += tab_width;
24820
24821 it->pixel_width = next_tab_x - x;
24822 it->nglyphs = 1;
24823 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24824 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24825
24826 if (it->glyph_row)
24827 {
24828 append_stretch_glyph (it, it->object, it->pixel_width,
24829 it->ascent + it->descent, it->ascent);
24830 }
24831 }
24832 else
24833 {
24834 it->pixel_width = 0;
24835 it->nglyphs = 1;
24836 }
24837 }
24838 }
24839 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24840 {
24841 /* A static composition.
24842
24843 Note: A composition is represented as one glyph in the
24844 glyph matrix. There are no padding glyphs.
24845
24846 Important note: pixel_width, ascent, and descent are the
24847 values of what is drawn by draw_glyphs (i.e. the values of
24848 the overall glyphs composed). */
24849 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24850 int boff; /* baseline offset */
24851 struct composition *cmp = composition_table[it->cmp_it.id];
24852 int glyph_len = cmp->glyph_len;
24853 struct font *font = face->font;
24854
24855 it->nglyphs = 1;
24856
24857 /* If we have not yet calculated pixel size data of glyphs of
24858 the composition for the current face font, calculate them
24859 now. Theoretically, we have to check all fonts for the
24860 glyphs, but that requires much time and memory space. So,
24861 here we check only the font of the first glyph. This may
24862 lead to incorrect display, but it's very rare, and C-l
24863 (recenter-top-bottom) can correct the display anyway. */
24864 if (! cmp->font || cmp->font != font)
24865 {
24866 /* Ascent and descent of the font of the first character
24867 of this composition (adjusted by baseline offset).
24868 Ascent and descent of overall glyphs should not be less
24869 than these, respectively. */
24870 int font_ascent, font_descent, font_height;
24871 /* Bounding box of the overall glyphs. */
24872 int leftmost, rightmost, lowest, highest;
24873 int lbearing, rbearing;
24874 int i, width, ascent, descent;
24875 int left_padded = 0, right_padded = 0;
24876 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24877 XChar2b char2b;
24878 struct font_metrics *pcm;
24879 int font_not_found_p;
24880 ptrdiff_t pos;
24881
24882 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24883 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24884 break;
24885 if (glyph_len < cmp->glyph_len)
24886 right_padded = 1;
24887 for (i = 0; i < glyph_len; i++)
24888 {
24889 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24890 break;
24891 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24892 }
24893 if (i > 0)
24894 left_padded = 1;
24895
24896 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24897 : IT_CHARPOS (*it));
24898 /* If no suitable font is found, use the default font. */
24899 font_not_found_p = font == NULL;
24900 if (font_not_found_p)
24901 {
24902 face = face->ascii_face;
24903 font = face->font;
24904 }
24905 boff = font->baseline_offset;
24906 if (font->vertical_centering)
24907 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24908 font_ascent = FONT_BASE (font) + boff;
24909 font_descent = FONT_DESCENT (font) - boff;
24910 font_height = FONT_HEIGHT (font);
24911
24912 cmp->font = font;
24913
24914 pcm = NULL;
24915 if (! font_not_found_p)
24916 {
24917 get_char_face_and_encoding (it->f, c, it->face_id,
24918 &char2b, 0);
24919 pcm = get_per_char_metric (font, &char2b);
24920 }
24921
24922 /* Initialize the bounding box. */
24923 if (pcm)
24924 {
24925 width = cmp->glyph_len > 0 ? pcm->width : 0;
24926 ascent = pcm->ascent;
24927 descent = pcm->descent;
24928 lbearing = pcm->lbearing;
24929 rbearing = pcm->rbearing;
24930 }
24931 else
24932 {
24933 width = cmp->glyph_len > 0 ? font->space_width : 0;
24934 ascent = FONT_BASE (font);
24935 descent = FONT_DESCENT (font);
24936 lbearing = 0;
24937 rbearing = width;
24938 }
24939
24940 rightmost = width;
24941 leftmost = 0;
24942 lowest = - descent + boff;
24943 highest = ascent + boff;
24944
24945 if (! font_not_found_p
24946 && font->default_ascent
24947 && CHAR_TABLE_P (Vuse_default_ascent)
24948 && !NILP (Faref (Vuse_default_ascent,
24949 make_number (it->char_to_display))))
24950 highest = font->default_ascent + boff;
24951
24952 /* Draw the first glyph at the normal position. It may be
24953 shifted to right later if some other glyphs are drawn
24954 at the left. */
24955 cmp->offsets[i * 2] = 0;
24956 cmp->offsets[i * 2 + 1] = boff;
24957 cmp->lbearing = lbearing;
24958 cmp->rbearing = rbearing;
24959
24960 /* Set cmp->offsets for the remaining glyphs. */
24961 for (i++; i < glyph_len; i++)
24962 {
24963 int left, right, btm, top;
24964 int ch = COMPOSITION_GLYPH (cmp, i);
24965 int face_id;
24966 struct face *this_face;
24967
24968 if (ch == '\t')
24969 ch = ' ';
24970 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24971 this_face = FACE_FROM_ID (it->f, face_id);
24972 font = this_face->font;
24973
24974 if (font == NULL)
24975 pcm = NULL;
24976 else
24977 {
24978 get_char_face_and_encoding (it->f, ch, face_id,
24979 &char2b, 0);
24980 pcm = get_per_char_metric (font, &char2b);
24981 }
24982 if (! pcm)
24983 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24984 else
24985 {
24986 width = pcm->width;
24987 ascent = pcm->ascent;
24988 descent = pcm->descent;
24989 lbearing = pcm->lbearing;
24990 rbearing = pcm->rbearing;
24991 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24992 {
24993 /* Relative composition with or without
24994 alternate chars. */
24995 left = (leftmost + rightmost - width) / 2;
24996 btm = - descent + boff;
24997 if (font->relative_compose
24998 && (! CHAR_TABLE_P (Vignore_relative_composition)
24999 || NILP (Faref (Vignore_relative_composition,
25000 make_number (ch)))))
25001 {
25002
25003 if (- descent >= font->relative_compose)
25004 /* One extra pixel between two glyphs. */
25005 btm = highest + 1;
25006 else if (ascent <= 0)
25007 /* One extra pixel between two glyphs. */
25008 btm = lowest - 1 - ascent - descent;
25009 }
25010 }
25011 else
25012 {
25013 /* A composition rule is specified by an integer
25014 value that encodes global and new reference
25015 points (GREF and NREF). GREF and NREF are
25016 specified by numbers as below:
25017
25018 0---1---2 -- ascent
25019 | |
25020 | |
25021 | |
25022 9--10--11 -- center
25023 | |
25024 ---3---4---5--- baseline
25025 | |
25026 6---7---8 -- descent
25027 */
25028 int rule = COMPOSITION_RULE (cmp, i);
25029 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25030
25031 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25032 grefx = gref % 3, nrefx = nref % 3;
25033 grefy = gref / 3, nrefy = nref / 3;
25034 if (xoff)
25035 xoff = font_height * (xoff - 128) / 256;
25036 if (yoff)
25037 yoff = font_height * (yoff - 128) / 256;
25038
25039 left = (leftmost
25040 + grefx * (rightmost - leftmost) / 2
25041 - nrefx * width / 2
25042 + xoff);
25043
25044 btm = ((grefy == 0 ? highest
25045 : grefy == 1 ? 0
25046 : grefy == 2 ? lowest
25047 : (highest + lowest) / 2)
25048 - (nrefy == 0 ? ascent + descent
25049 : nrefy == 1 ? descent - boff
25050 : nrefy == 2 ? 0
25051 : (ascent + descent) / 2)
25052 + yoff);
25053 }
25054
25055 cmp->offsets[i * 2] = left;
25056 cmp->offsets[i * 2 + 1] = btm + descent;
25057
25058 /* Update the bounding box of the overall glyphs. */
25059 if (width > 0)
25060 {
25061 right = left + width;
25062 if (left < leftmost)
25063 leftmost = left;
25064 if (right > rightmost)
25065 rightmost = right;
25066 }
25067 top = btm + descent + ascent;
25068 if (top > highest)
25069 highest = top;
25070 if (btm < lowest)
25071 lowest = btm;
25072
25073 if (cmp->lbearing > left + lbearing)
25074 cmp->lbearing = left + lbearing;
25075 if (cmp->rbearing < left + rbearing)
25076 cmp->rbearing = left + rbearing;
25077 }
25078 }
25079
25080 /* If there are glyphs whose x-offsets are negative,
25081 shift all glyphs to the right and make all x-offsets
25082 non-negative. */
25083 if (leftmost < 0)
25084 {
25085 for (i = 0; i < cmp->glyph_len; i++)
25086 cmp->offsets[i * 2] -= leftmost;
25087 rightmost -= leftmost;
25088 cmp->lbearing -= leftmost;
25089 cmp->rbearing -= leftmost;
25090 }
25091
25092 if (left_padded && cmp->lbearing < 0)
25093 {
25094 for (i = 0; i < cmp->glyph_len; i++)
25095 cmp->offsets[i * 2] -= cmp->lbearing;
25096 rightmost -= cmp->lbearing;
25097 cmp->rbearing -= cmp->lbearing;
25098 cmp->lbearing = 0;
25099 }
25100 if (right_padded && rightmost < cmp->rbearing)
25101 {
25102 rightmost = cmp->rbearing;
25103 }
25104
25105 cmp->pixel_width = rightmost;
25106 cmp->ascent = highest;
25107 cmp->descent = - lowest;
25108 if (cmp->ascent < font_ascent)
25109 cmp->ascent = font_ascent;
25110 if (cmp->descent < font_descent)
25111 cmp->descent = font_descent;
25112 }
25113
25114 if (it->glyph_row
25115 && (cmp->lbearing < 0
25116 || cmp->rbearing > cmp->pixel_width))
25117 it->glyph_row->contains_overlapping_glyphs_p = 1;
25118
25119 it->pixel_width = cmp->pixel_width;
25120 it->ascent = it->phys_ascent = cmp->ascent;
25121 it->descent = it->phys_descent = cmp->descent;
25122 if (face->box != FACE_NO_BOX)
25123 {
25124 int thick = face->box_line_width;
25125
25126 if (thick > 0)
25127 {
25128 it->ascent += thick;
25129 it->descent += thick;
25130 }
25131 else
25132 thick = - thick;
25133
25134 if (it->start_of_box_run_p)
25135 it->pixel_width += thick;
25136 if (it->end_of_box_run_p)
25137 it->pixel_width += thick;
25138 }
25139
25140 /* If face has an overline, add the height of the overline
25141 (1 pixel) and a 1 pixel margin to the character height. */
25142 if (face->overline_p)
25143 it->ascent += overline_margin;
25144
25145 take_vertical_position_into_account (it);
25146 if (it->ascent < 0)
25147 it->ascent = 0;
25148 if (it->descent < 0)
25149 it->descent = 0;
25150
25151 if (it->glyph_row && cmp->glyph_len > 0)
25152 append_composite_glyph (it);
25153 }
25154 else if (it->what == IT_COMPOSITION)
25155 {
25156 /* A dynamic (automatic) composition. */
25157 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25158 Lisp_Object gstring;
25159 struct font_metrics metrics;
25160
25161 it->nglyphs = 1;
25162
25163 gstring = composition_gstring_from_id (it->cmp_it.id);
25164 it->pixel_width
25165 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25166 &metrics);
25167 if (it->glyph_row
25168 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25169 it->glyph_row->contains_overlapping_glyphs_p = 1;
25170 it->ascent = it->phys_ascent = metrics.ascent;
25171 it->descent = it->phys_descent = metrics.descent;
25172 if (face->box != FACE_NO_BOX)
25173 {
25174 int thick = face->box_line_width;
25175
25176 if (thick > 0)
25177 {
25178 it->ascent += thick;
25179 it->descent += thick;
25180 }
25181 else
25182 thick = - thick;
25183
25184 if (it->start_of_box_run_p)
25185 it->pixel_width += thick;
25186 if (it->end_of_box_run_p)
25187 it->pixel_width += thick;
25188 }
25189 /* If face has an overline, add the height of the overline
25190 (1 pixel) and a 1 pixel margin to the character height. */
25191 if (face->overline_p)
25192 it->ascent += overline_margin;
25193 take_vertical_position_into_account (it);
25194 if (it->ascent < 0)
25195 it->ascent = 0;
25196 if (it->descent < 0)
25197 it->descent = 0;
25198
25199 if (it->glyph_row)
25200 append_composite_glyph (it);
25201 }
25202 else if (it->what == IT_GLYPHLESS)
25203 produce_glyphless_glyph (it, 0, Qnil);
25204 else if (it->what == IT_IMAGE)
25205 produce_image_glyph (it);
25206 else if (it->what == IT_STRETCH)
25207 produce_stretch_glyph (it);
25208
25209 done:
25210 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25211 because this isn't true for images with `:ascent 100'. */
25212 eassert (it->ascent >= 0 && it->descent >= 0);
25213 if (it->area == TEXT_AREA)
25214 it->current_x += it->pixel_width;
25215
25216 if (extra_line_spacing > 0)
25217 {
25218 it->descent += extra_line_spacing;
25219 if (extra_line_spacing > it->max_extra_line_spacing)
25220 it->max_extra_line_spacing = extra_line_spacing;
25221 }
25222
25223 it->max_ascent = max (it->max_ascent, it->ascent);
25224 it->max_descent = max (it->max_descent, it->descent);
25225 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25226 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25227 }
25228
25229 /* EXPORT for RIF:
25230 Output LEN glyphs starting at START at the nominal cursor position.
25231 Advance the nominal cursor over the text. The global variable
25232 updated_window contains the window being updated, updated_row is
25233 the glyph row being updated, and updated_area is the area of that
25234 row being updated. */
25235
25236 void
25237 x_write_glyphs (struct glyph *start, int len)
25238 {
25239 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25240
25241 eassert (updated_window && updated_row);
25242 /* When the window is hscrolled, cursor hpos can legitimately be out
25243 of bounds, but we draw the cursor at the corresponding window
25244 margin in that case. */
25245 if (!updated_row->reversed_p && chpos < 0)
25246 chpos = 0;
25247 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25248 chpos = updated_row->used[TEXT_AREA] - 1;
25249
25250 block_input ();
25251
25252 /* Write glyphs. */
25253
25254 hpos = start - updated_row->glyphs[updated_area];
25255 x = draw_glyphs (updated_window, output_cursor.x,
25256 updated_row, updated_area,
25257 hpos, hpos + len,
25258 DRAW_NORMAL_TEXT, 0);
25259
25260 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25261 if (updated_area == TEXT_AREA
25262 && updated_window->phys_cursor_on_p
25263 && updated_window->phys_cursor.vpos == output_cursor.vpos
25264 && chpos >= hpos
25265 && chpos < hpos + len)
25266 updated_window->phys_cursor_on_p = 0;
25267
25268 unblock_input ();
25269
25270 /* Advance the output cursor. */
25271 output_cursor.hpos += len;
25272 output_cursor.x = x;
25273 }
25274
25275
25276 /* EXPORT for RIF:
25277 Insert LEN glyphs from START at the nominal cursor position. */
25278
25279 void
25280 x_insert_glyphs (struct glyph *start, int len)
25281 {
25282 struct frame *f;
25283 struct window *w;
25284 int line_height, shift_by_width, shifted_region_width;
25285 struct glyph_row *row;
25286 struct glyph *glyph;
25287 int frame_x, frame_y;
25288 ptrdiff_t hpos;
25289
25290 eassert (updated_window && updated_row);
25291 block_input ();
25292 w = updated_window;
25293 f = XFRAME (WINDOW_FRAME (w));
25294
25295 /* Get the height of the line we are in. */
25296 row = updated_row;
25297 line_height = row->height;
25298
25299 /* Get the width of the glyphs to insert. */
25300 shift_by_width = 0;
25301 for (glyph = start; glyph < start + len; ++glyph)
25302 shift_by_width += glyph->pixel_width;
25303
25304 /* Get the width of the region to shift right. */
25305 shifted_region_width = (window_box_width (w, updated_area)
25306 - output_cursor.x
25307 - shift_by_width);
25308
25309 /* Shift right. */
25310 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25311 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25312
25313 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25314 line_height, shift_by_width);
25315
25316 /* Write the glyphs. */
25317 hpos = start - row->glyphs[updated_area];
25318 draw_glyphs (w, output_cursor.x, row, updated_area,
25319 hpos, hpos + len,
25320 DRAW_NORMAL_TEXT, 0);
25321
25322 /* Advance the output cursor. */
25323 output_cursor.hpos += len;
25324 output_cursor.x += shift_by_width;
25325 unblock_input ();
25326 }
25327
25328
25329 /* EXPORT for RIF:
25330 Erase the current text line from the nominal cursor position
25331 (inclusive) to pixel column TO_X (exclusive). The idea is that
25332 everything from TO_X onward is already erased.
25333
25334 TO_X is a pixel position relative to updated_area of
25335 updated_window. TO_X == -1 means clear to the end of this area. */
25336
25337 void
25338 x_clear_end_of_line (int to_x)
25339 {
25340 struct frame *f;
25341 struct window *w = updated_window;
25342 int max_x, min_y, max_y;
25343 int from_x, from_y, to_y;
25344
25345 eassert (updated_window && updated_row);
25346 f = XFRAME (w->frame);
25347
25348 if (updated_row->full_width_p)
25349 max_x = WINDOW_TOTAL_WIDTH (w);
25350 else
25351 max_x = window_box_width (w, updated_area);
25352 max_y = window_text_bottom_y (w);
25353
25354 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25355 of window. For TO_X > 0, truncate to end of drawing area. */
25356 if (to_x == 0)
25357 return;
25358 else if (to_x < 0)
25359 to_x = max_x;
25360 else
25361 to_x = min (to_x, max_x);
25362
25363 to_y = min (max_y, output_cursor.y + updated_row->height);
25364
25365 /* Notice if the cursor will be cleared by this operation. */
25366 if (!updated_row->full_width_p)
25367 notice_overwritten_cursor (w, updated_area,
25368 output_cursor.x, -1,
25369 updated_row->y,
25370 MATRIX_ROW_BOTTOM_Y (updated_row));
25371
25372 from_x = output_cursor.x;
25373
25374 /* Translate to frame coordinates. */
25375 if (updated_row->full_width_p)
25376 {
25377 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25378 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25379 }
25380 else
25381 {
25382 int area_left = window_box_left (w, updated_area);
25383 from_x += area_left;
25384 to_x += area_left;
25385 }
25386
25387 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25388 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25389 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25390
25391 /* Prevent inadvertently clearing to end of the X window. */
25392 if (to_x > from_x && to_y > from_y)
25393 {
25394 block_input ();
25395 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25396 to_x - from_x, to_y - from_y);
25397 unblock_input ();
25398 }
25399 }
25400
25401 #endif /* HAVE_WINDOW_SYSTEM */
25402
25403
25404 \f
25405 /***********************************************************************
25406 Cursor types
25407 ***********************************************************************/
25408
25409 /* Value is the internal representation of the specified cursor type
25410 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25411 of the bar cursor. */
25412
25413 static enum text_cursor_kinds
25414 get_specified_cursor_type (Lisp_Object arg, int *width)
25415 {
25416 enum text_cursor_kinds type;
25417
25418 if (NILP (arg))
25419 return NO_CURSOR;
25420
25421 if (EQ (arg, Qbox))
25422 return FILLED_BOX_CURSOR;
25423
25424 if (EQ (arg, Qhollow))
25425 return HOLLOW_BOX_CURSOR;
25426
25427 if (EQ (arg, Qbar))
25428 {
25429 *width = 2;
25430 return BAR_CURSOR;
25431 }
25432
25433 if (CONSP (arg)
25434 && EQ (XCAR (arg), Qbar)
25435 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25436 {
25437 *width = XINT (XCDR (arg));
25438 return BAR_CURSOR;
25439 }
25440
25441 if (EQ (arg, Qhbar))
25442 {
25443 *width = 2;
25444 return HBAR_CURSOR;
25445 }
25446
25447 if (CONSP (arg)
25448 && EQ (XCAR (arg), Qhbar)
25449 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25450 {
25451 *width = XINT (XCDR (arg));
25452 return HBAR_CURSOR;
25453 }
25454
25455 /* Treat anything unknown as "hollow box cursor".
25456 It was bad to signal an error; people have trouble fixing
25457 .Xdefaults with Emacs, when it has something bad in it. */
25458 type = HOLLOW_BOX_CURSOR;
25459
25460 return type;
25461 }
25462
25463 /* Set the default cursor types for specified frame. */
25464 void
25465 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25466 {
25467 int width = 1;
25468 Lisp_Object tem;
25469
25470 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25471 FRAME_CURSOR_WIDTH (f) = width;
25472
25473 /* By default, set up the blink-off state depending on the on-state. */
25474
25475 tem = Fassoc (arg, Vblink_cursor_alist);
25476 if (!NILP (tem))
25477 {
25478 FRAME_BLINK_OFF_CURSOR (f)
25479 = get_specified_cursor_type (XCDR (tem), &width);
25480 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25481 }
25482 else
25483 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25484 }
25485
25486
25487 #ifdef HAVE_WINDOW_SYSTEM
25488
25489 /* Return the cursor we want to be displayed in window W. Return
25490 width of bar/hbar cursor through WIDTH arg. Return with
25491 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25492 (i.e. if the `system caret' should track this cursor).
25493
25494 In a mini-buffer window, we want the cursor only to appear if we
25495 are reading input from this window. For the selected window, we
25496 want the cursor type given by the frame parameter or buffer local
25497 setting of cursor-type. If explicitly marked off, draw no cursor.
25498 In all other cases, we want a hollow box cursor. */
25499
25500 static enum text_cursor_kinds
25501 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25502 int *active_cursor)
25503 {
25504 struct frame *f = XFRAME (w->frame);
25505 struct buffer *b = XBUFFER (w->contents);
25506 int cursor_type = DEFAULT_CURSOR;
25507 Lisp_Object alt_cursor;
25508 int non_selected = 0;
25509
25510 *active_cursor = 1;
25511
25512 /* Echo area */
25513 if (cursor_in_echo_area
25514 && FRAME_HAS_MINIBUF_P (f)
25515 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25516 {
25517 if (w == XWINDOW (echo_area_window))
25518 {
25519 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25520 {
25521 *width = FRAME_CURSOR_WIDTH (f);
25522 return FRAME_DESIRED_CURSOR (f);
25523 }
25524 else
25525 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25526 }
25527
25528 *active_cursor = 0;
25529 non_selected = 1;
25530 }
25531
25532 /* Detect a nonselected window or nonselected frame. */
25533 else if (w != XWINDOW (f->selected_window)
25534 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25535 {
25536 *active_cursor = 0;
25537
25538 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25539 return NO_CURSOR;
25540
25541 non_selected = 1;
25542 }
25543
25544 /* Never display a cursor in a window in which cursor-type is nil. */
25545 if (NILP (BVAR (b, cursor_type)))
25546 return NO_CURSOR;
25547
25548 /* Get the normal cursor type for this window. */
25549 if (EQ (BVAR (b, cursor_type), Qt))
25550 {
25551 cursor_type = FRAME_DESIRED_CURSOR (f);
25552 *width = FRAME_CURSOR_WIDTH (f);
25553 }
25554 else
25555 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25556
25557 /* Use cursor-in-non-selected-windows instead
25558 for non-selected window or frame. */
25559 if (non_selected)
25560 {
25561 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25562 if (!EQ (Qt, alt_cursor))
25563 return get_specified_cursor_type (alt_cursor, width);
25564 /* t means modify the normal cursor type. */
25565 if (cursor_type == FILLED_BOX_CURSOR)
25566 cursor_type = HOLLOW_BOX_CURSOR;
25567 else if (cursor_type == BAR_CURSOR && *width > 1)
25568 --*width;
25569 return cursor_type;
25570 }
25571
25572 /* Use normal cursor if not blinked off. */
25573 if (!w->cursor_off_p)
25574 {
25575 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25576 {
25577 if (cursor_type == FILLED_BOX_CURSOR)
25578 {
25579 /* Using a block cursor on large images can be very annoying.
25580 So use a hollow cursor for "large" images.
25581 If image is not transparent (no mask), also use hollow cursor. */
25582 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25583 if (img != NULL && IMAGEP (img->spec))
25584 {
25585 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25586 where N = size of default frame font size.
25587 This should cover most of the "tiny" icons people may use. */
25588 if (!img->mask
25589 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25590 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25591 cursor_type = HOLLOW_BOX_CURSOR;
25592 }
25593 }
25594 else if (cursor_type != NO_CURSOR)
25595 {
25596 /* Display current only supports BOX and HOLLOW cursors for images.
25597 So for now, unconditionally use a HOLLOW cursor when cursor is
25598 not a solid box cursor. */
25599 cursor_type = HOLLOW_BOX_CURSOR;
25600 }
25601 }
25602 return cursor_type;
25603 }
25604
25605 /* Cursor is blinked off, so determine how to "toggle" it. */
25606
25607 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25608 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25609 return get_specified_cursor_type (XCDR (alt_cursor), width);
25610
25611 /* Then see if frame has specified a specific blink off cursor type. */
25612 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25613 {
25614 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25615 return FRAME_BLINK_OFF_CURSOR (f);
25616 }
25617
25618 #if 0
25619 /* Some people liked having a permanently visible blinking cursor,
25620 while others had very strong opinions against it. So it was
25621 decided to remove it. KFS 2003-09-03 */
25622
25623 /* Finally perform built-in cursor blinking:
25624 filled box <-> hollow box
25625 wide [h]bar <-> narrow [h]bar
25626 narrow [h]bar <-> no cursor
25627 other type <-> no cursor */
25628
25629 if (cursor_type == FILLED_BOX_CURSOR)
25630 return HOLLOW_BOX_CURSOR;
25631
25632 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25633 {
25634 *width = 1;
25635 return cursor_type;
25636 }
25637 #endif
25638
25639 return NO_CURSOR;
25640 }
25641
25642
25643 /* Notice when the text cursor of window W has been completely
25644 overwritten by a drawing operation that outputs glyphs in AREA
25645 starting at X0 and ending at X1 in the line starting at Y0 and
25646 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25647 the rest of the line after X0 has been written. Y coordinates
25648 are window-relative. */
25649
25650 static void
25651 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25652 int x0, int x1, int y0, int y1)
25653 {
25654 int cx0, cx1, cy0, cy1;
25655 struct glyph_row *row;
25656
25657 if (!w->phys_cursor_on_p)
25658 return;
25659 if (area != TEXT_AREA)
25660 return;
25661
25662 if (w->phys_cursor.vpos < 0
25663 || w->phys_cursor.vpos >= w->current_matrix->nrows
25664 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25665 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
25666 return;
25667
25668 if (row->cursor_in_fringe_p)
25669 {
25670 row->cursor_in_fringe_p = 0;
25671 draw_fringe_bitmap (w, row, row->reversed_p);
25672 w->phys_cursor_on_p = 0;
25673 return;
25674 }
25675
25676 cx0 = w->phys_cursor.x;
25677 cx1 = cx0 + w->phys_cursor_width;
25678 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25679 return;
25680
25681 /* The cursor image will be completely removed from the
25682 screen if the output area intersects the cursor area in
25683 y-direction. When we draw in [y0 y1[, and some part of
25684 the cursor is at y < y0, that part must have been drawn
25685 before. When scrolling, the cursor is erased before
25686 actually scrolling, so we don't come here. When not
25687 scrolling, the rows above the old cursor row must have
25688 changed, and in this case these rows must have written
25689 over the cursor image.
25690
25691 Likewise if part of the cursor is below y1, with the
25692 exception of the cursor being in the first blank row at
25693 the buffer and window end because update_text_area
25694 doesn't draw that row. (Except when it does, but
25695 that's handled in update_text_area.) */
25696
25697 cy0 = w->phys_cursor.y;
25698 cy1 = cy0 + w->phys_cursor_height;
25699 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25700 return;
25701
25702 w->phys_cursor_on_p = 0;
25703 }
25704
25705 #endif /* HAVE_WINDOW_SYSTEM */
25706
25707 \f
25708 /************************************************************************
25709 Mouse Face
25710 ************************************************************************/
25711
25712 #ifdef HAVE_WINDOW_SYSTEM
25713
25714 /* EXPORT for RIF:
25715 Fix the display of area AREA of overlapping row ROW in window W
25716 with respect to the overlapping part OVERLAPS. */
25717
25718 void
25719 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25720 enum glyph_row_area area, int overlaps)
25721 {
25722 int i, x;
25723
25724 block_input ();
25725
25726 x = 0;
25727 for (i = 0; i < row->used[area];)
25728 {
25729 if (row->glyphs[area][i].overlaps_vertically_p)
25730 {
25731 int start = i, start_x = x;
25732
25733 do
25734 {
25735 x += row->glyphs[area][i].pixel_width;
25736 ++i;
25737 }
25738 while (i < row->used[area]
25739 && row->glyphs[area][i].overlaps_vertically_p);
25740
25741 draw_glyphs (w, start_x, row, area,
25742 start, i,
25743 DRAW_NORMAL_TEXT, overlaps);
25744 }
25745 else
25746 {
25747 x += row->glyphs[area][i].pixel_width;
25748 ++i;
25749 }
25750 }
25751
25752 unblock_input ();
25753 }
25754
25755
25756 /* EXPORT:
25757 Draw the cursor glyph of window W in glyph row ROW. See the
25758 comment of draw_glyphs for the meaning of HL. */
25759
25760 void
25761 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25762 enum draw_glyphs_face hl)
25763 {
25764 /* If cursor hpos is out of bounds, don't draw garbage. This can
25765 happen in mini-buffer windows when switching between echo area
25766 glyphs and mini-buffer. */
25767 if ((row->reversed_p
25768 ? (w->phys_cursor.hpos >= 0)
25769 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25770 {
25771 int on_p = w->phys_cursor_on_p;
25772 int x1;
25773 int hpos = w->phys_cursor.hpos;
25774
25775 /* When the window is hscrolled, cursor hpos can legitimately be
25776 out of bounds, but we draw the cursor at the corresponding
25777 window margin in that case. */
25778 if (!row->reversed_p && hpos < 0)
25779 hpos = 0;
25780 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25781 hpos = row->used[TEXT_AREA] - 1;
25782
25783 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25784 hl, 0);
25785 w->phys_cursor_on_p = on_p;
25786
25787 if (hl == DRAW_CURSOR)
25788 w->phys_cursor_width = x1 - w->phys_cursor.x;
25789 /* When we erase the cursor, and ROW is overlapped by other
25790 rows, make sure that these overlapping parts of other rows
25791 are redrawn. */
25792 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25793 {
25794 w->phys_cursor_width = x1 - w->phys_cursor.x;
25795
25796 if (row > w->current_matrix->rows
25797 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25798 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25799 OVERLAPS_ERASED_CURSOR);
25800
25801 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25802 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25803 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25804 OVERLAPS_ERASED_CURSOR);
25805 }
25806 }
25807 }
25808
25809
25810 /* EXPORT:
25811 Erase the image of a cursor of window W from the screen. */
25812
25813 void
25814 erase_phys_cursor (struct window *w)
25815 {
25816 struct frame *f = XFRAME (w->frame);
25817 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25818 int hpos = w->phys_cursor.hpos;
25819 int vpos = w->phys_cursor.vpos;
25820 int mouse_face_here_p = 0;
25821 struct glyph_matrix *active_glyphs = w->current_matrix;
25822 struct glyph_row *cursor_row;
25823 struct glyph *cursor_glyph;
25824 enum draw_glyphs_face hl;
25825
25826 /* No cursor displayed or row invalidated => nothing to do on the
25827 screen. */
25828 if (w->phys_cursor_type == NO_CURSOR)
25829 goto mark_cursor_off;
25830
25831 /* VPOS >= active_glyphs->nrows means that window has been resized.
25832 Don't bother to erase the cursor. */
25833 if (vpos >= active_glyphs->nrows)
25834 goto mark_cursor_off;
25835
25836 /* If row containing cursor is marked invalid, there is nothing we
25837 can do. */
25838 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25839 if (!cursor_row->enabled_p)
25840 goto mark_cursor_off;
25841
25842 /* If line spacing is > 0, old cursor may only be partially visible in
25843 window after split-window. So adjust visible height. */
25844 cursor_row->visible_height = min (cursor_row->visible_height,
25845 window_text_bottom_y (w) - cursor_row->y);
25846
25847 /* If row is completely invisible, don't attempt to delete a cursor which
25848 isn't there. This can happen if cursor is at top of a window, and
25849 we switch to a buffer with a header line in that window. */
25850 if (cursor_row->visible_height <= 0)
25851 goto mark_cursor_off;
25852
25853 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25854 if (cursor_row->cursor_in_fringe_p)
25855 {
25856 cursor_row->cursor_in_fringe_p = 0;
25857 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25858 goto mark_cursor_off;
25859 }
25860
25861 /* This can happen when the new row is shorter than the old one.
25862 In this case, either draw_glyphs or clear_end_of_line
25863 should have cleared the cursor. Note that we wouldn't be
25864 able to erase the cursor in this case because we don't have a
25865 cursor glyph at hand. */
25866 if ((cursor_row->reversed_p
25867 ? (w->phys_cursor.hpos < 0)
25868 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25869 goto mark_cursor_off;
25870
25871 /* When the window is hscrolled, cursor hpos can legitimately be out
25872 of bounds, but we draw the cursor at the corresponding window
25873 margin in that case. */
25874 if (!cursor_row->reversed_p && hpos < 0)
25875 hpos = 0;
25876 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25877 hpos = cursor_row->used[TEXT_AREA] - 1;
25878
25879 /* If the cursor is in the mouse face area, redisplay that when
25880 we clear the cursor. */
25881 if (! NILP (hlinfo->mouse_face_window)
25882 && coords_in_mouse_face_p (w, hpos, vpos)
25883 /* Don't redraw the cursor's spot in mouse face if it is at the
25884 end of a line (on a newline). The cursor appears there, but
25885 mouse highlighting does not. */
25886 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25887 mouse_face_here_p = 1;
25888
25889 /* Maybe clear the display under the cursor. */
25890 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25891 {
25892 int x, y, left_x;
25893 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25894 int width;
25895
25896 cursor_glyph = get_phys_cursor_glyph (w);
25897 if (cursor_glyph == NULL)
25898 goto mark_cursor_off;
25899
25900 width = cursor_glyph->pixel_width;
25901 left_x = window_box_left_offset (w, TEXT_AREA);
25902 x = w->phys_cursor.x;
25903 if (x < left_x)
25904 width -= left_x - x;
25905 width = min (width, window_box_width (w, TEXT_AREA) - x);
25906 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25907 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25908
25909 if (width > 0)
25910 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25911 }
25912
25913 /* Erase the cursor by redrawing the character underneath it. */
25914 if (mouse_face_here_p)
25915 hl = DRAW_MOUSE_FACE;
25916 else
25917 hl = DRAW_NORMAL_TEXT;
25918 draw_phys_cursor_glyph (w, cursor_row, hl);
25919
25920 mark_cursor_off:
25921 w->phys_cursor_on_p = 0;
25922 w->phys_cursor_type = NO_CURSOR;
25923 }
25924
25925
25926 /* EXPORT:
25927 Display or clear cursor of window W. If ON is zero, clear the
25928 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25929 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25930
25931 void
25932 display_and_set_cursor (struct window *w, int on,
25933 int hpos, int vpos, int x, int y)
25934 {
25935 struct frame *f = XFRAME (w->frame);
25936 int new_cursor_type;
25937 int new_cursor_width;
25938 int active_cursor;
25939 struct glyph_row *glyph_row;
25940 struct glyph *glyph;
25941
25942 /* This is pointless on invisible frames, and dangerous on garbaged
25943 windows and frames; in the latter case, the frame or window may
25944 be in the midst of changing its size, and x and y may be off the
25945 window. */
25946 if (! FRAME_VISIBLE_P (f)
25947 || FRAME_GARBAGED_P (f)
25948 || vpos >= w->current_matrix->nrows
25949 || hpos >= w->current_matrix->matrix_w)
25950 return;
25951
25952 /* If cursor is off and we want it off, return quickly. */
25953 if (!on && !w->phys_cursor_on_p)
25954 return;
25955
25956 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25957 /* If cursor row is not enabled, we don't really know where to
25958 display the cursor. */
25959 if (!glyph_row->enabled_p)
25960 {
25961 w->phys_cursor_on_p = 0;
25962 return;
25963 }
25964
25965 glyph = NULL;
25966 if (!glyph_row->exact_window_width_line_p
25967 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25968 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25969
25970 eassert (input_blocked_p ());
25971
25972 /* Set new_cursor_type to the cursor we want to be displayed. */
25973 new_cursor_type = get_window_cursor_type (w, glyph,
25974 &new_cursor_width, &active_cursor);
25975
25976 /* If cursor is currently being shown and we don't want it to be or
25977 it is in the wrong place, or the cursor type is not what we want,
25978 erase it. */
25979 if (w->phys_cursor_on_p
25980 && (!on
25981 || w->phys_cursor.x != x
25982 || w->phys_cursor.y != y
25983 || new_cursor_type != w->phys_cursor_type
25984 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25985 && new_cursor_width != w->phys_cursor_width)))
25986 erase_phys_cursor (w);
25987
25988 /* Don't check phys_cursor_on_p here because that flag is only set
25989 to zero in some cases where we know that the cursor has been
25990 completely erased, to avoid the extra work of erasing the cursor
25991 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25992 still not be visible, or it has only been partly erased. */
25993 if (on)
25994 {
25995 w->phys_cursor_ascent = glyph_row->ascent;
25996 w->phys_cursor_height = glyph_row->height;
25997
25998 /* Set phys_cursor_.* before x_draw_.* is called because some
25999 of them may need the information. */
26000 w->phys_cursor.x = x;
26001 w->phys_cursor.y = glyph_row->y;
26002 w->phys_cursor.hpos = hpos;
26003 w->phys_cursor.vpos = vpos;
26004 }
26005
26006 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26007 new_cursor_type, new_cursor_width,
26008 on, active_cursor);
26009 }
26010
26011
26012 /* Switch the display of W's cursor on or off, according to the value
26013 of ON. */
26014
26015 static void
26016 update_window_cursor (struct window *w, int on)
26017 {
26018 /* Don't update cursor in windows whose frame is in the process
26019 of being deleted. */
26020 if (w->current_matrix)
26021 {
26022 int hpos = w->phys_cursor.hpos;
26023 int vpos = w->phys_cursor.vpos;
26024 struct glyph_row *row;
26025
26026 if (vpos >= w->current_matrix->nrows
26027 || hpos >= w->current_matrix->matrix_w)
26028 return;
26029
26030 row = MATRIX_ROW (w->current_matrix, vpos);
26031
26032 /* When the window is hscrolled, cursor hpos can legitimately be
26033 out of bounds, but we draw the cursor at the corresponding
26034 window margin in that case. */
26035 if (!row->reversed_p && hpos < 0)
26036 hpos = 0;
26037 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26038 hpos = row->used[TEXT_AREA] - 1;
26039
26040 block_input ();
26041 display_and_set_cursor (w, on, hpos, vpos,
26042 w->phys_cursor.x, w->phys_cursor.y);
26043 unblock_input ();
26044 }
26045 }
26046
26047
26048 /* Call update_window_cursor with parameter ON_P on all leaf windows
26049 in the window tree rooted at W. */
26050
26051 static void
26052 update_cursor_in_window_tree (struct window *w, int on_p)
26053 {
26054 while (w)
26055 {
26056 if (WINDOWP (w->contents))
26057 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26058 else
26059 update_window_cursor (w, on_p);
26060
26061 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26062 }
26063 }
26064
26065
26066 /* EXPORT:
26067 Display the cursor on window W, or clear it, according to ON_P.
26068 Don't change the cursor's position. */
26069
26070 void
26071 x_update_cursor (struct frame *f, int on_p)
26072 {
26073 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26074 }
26075
26076
26077 /* EXPORT:
26078 Clear the cursor of window W to background color, and mark the
26079 cursor as not shown. This is used when the text where the cursor
26080 is about to be rewritten. */
26081
26082 void
26083 x_clear_cursor (struct window *w)
26084 {
26085 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26086 update_window_cursor (w, 0);
26087 }
26088
26089 #endif /* HAVE_WINDOW_SYSTEM */
26090
26091 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26092 and MSDOS. */
26093 static void
26094 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26095 int start_hpos, int end_hpos,
26096 enum draw_glyphs_face draw)
26097 {
26098 #ifdef HAVE_WINDOW_SYSTEM
26099 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26100 {
26101 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26102 return;
26103 }
26104 #endif
26105 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26106 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26107 #endif
26108 }
26109
26110 /* Display the active region described by mouse_face_* according to DRAW. */
26111
26112 static void
26113 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26114 {
26115 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26116 struct frame *f = XFRAME (WINDOW_FRAME (w));
26117
26118 if (/* If window is in the process of being destroyed, don't bother
26119 to do anything. */
26120 w->current_matrix != NULL
26121 /* Don't update mouse highlight if hidden */
26122 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26123 /* Recognize when we are called to operate on rows that don't exist
26124 anymore. This can happen when a window is split. */
26125 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26126 {
26127 int phys_cursor_on_p = w->phys_cursor_on_p;
26128 struct glyph_row *row, *first, *last;
26129
26130 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26131 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26132
26133 for (row = first; row <= last && row->enabled_p; ++row)
26134 {
26135 int start_hpos, end_hpos, start_x;
26136
26137 /* For all but the first row, the highlight starts at column 0. */
26138 if (row == first)
26139 {
26140 /* R2L rows have BEG and END in reversed order, but the
26141 screen drawing geometry is always left to right. So
26142 we need to mirror the beginning and end of the
26143 highlighted area in R2L rows. */
26144 if (!row->reversed_p)
26145 {
26146 start_hpos = hlinfo->mouse_face_beg_col;
26147 start_x = hlinfo->mouse_face_beg_x;
26148 }
26149 else if (row == last)
26150 {
26151 start_hpos = hlinfo->mouse_face_end_col;
26152 start_x = hlinfo->mouse_face_end_x;
26153 }
26154 else
26155 {
26156 start_hpos = 0;
26157 start_x = 0;
26158 }
26159 }
26160 else if (row->reversed_p && row == last)
26161 {
26162 start_hpos = hlinfo->mouse_face_end_col;
26163 start_x = hlinfo->mouse_face_end_x;
26164 }
26165 else
26166 {
26167 start_hpos = 0;
26168 start_x = 0;
26169 }
26170
26171 if (row == last)
26172 {
26173 if (!row->reversed_p)
26174 end_hpos = hlinfo->mouse_face_end_col;
26175 else if (row == first)
26176 end_hpos = hlinfo->mouse_face_beg_col;
26177 else
26178 {
26179 end_hpos = row->used[TEXT_AREA];
26180 if (draw == DRAW_NORMAL_TEXT)
26181 row->fill_line_p = 1; /* Clear to end of line */
26182 }
26183 }
26184 else if (row->reversed_p && row == first)
26185 end_hpos = hlinfo->mouse_face_beg_col;
26186 else
26187 {
26188 end_hpos = row->used[TEXT_AREA];
26189 if (draw == DRAW_NORMAL_TEXT)
26190 row->fill_line_p = 1; /* Clear to end of line */
26191 }
26192
26193 if (end_hpos > start_hpos)
26194 {
26195 draw_row_with_mouse_face (w, start_x, row,
26196 start_hpos, end_hpos, draw);
26197
26198 row->mouse_face_p
26199 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26200 }
26201 }
26202
26203 #ifdef HAVE_WINDOW_SYSTEM
26204 /* When we've written over the cursor, arrange for it to
26205 be displayed again. */
26206 if (FRAME_WINDOW_P (f)
26207 && phys_cursor_on_p && !w->phys_cursor_on_p)
26208 {
26209 int hpos = w->phys_cursor.hpos;
26210
26211 /* When the window is hscrolled, cursor hpos can legitimately be
26212 out of bounds, but we draw the cursor at the corresponding
26213 window margin in that case. */
26214 if (!row->reversed_p && hpos < 0)
26215 hpos = 0;
26216 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26217 hpos = row->used[TEXT_AREA] - 1;
26218
26219 block_input ();
26220 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26221 w->phys_cursor.x, w->phys_cursor.y);
26222 unblock_input ();
26223 }
26224 #endif /* HAVE_WINDOW_SYSTEM */
26225 }
26226
26227 #ifdef HAVE_WINDOW_SYSTEM
26228 /* Change the mouse cursor. */
26229 if (FRAME_WINDOW_P (f))
26230 {
26231 if (draw == DRAW_NORMAL_TEXT
26232 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26233 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26234 else if (draw == DRAW_MOUSE_FACE)
26235 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26236 else
26237 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26238 }
26239 #endif /* HAVE_WINDOW_SYSTEM */
26240 }
26241
26242 /* EXPORT:
26243 Clear out the mouse-highlighted active region.
26244 Redraw it un-highlighted first. Value is non-zero if mouse
26245 face was actually drawn unhighlighted. */
26246
26247 int
26248 clear_mouse_face (Mouse_HLInfo *hlinfo)
26249 {
26250 int cleared = 0;
26251
26252 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26253 {
26254 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26255 cleared = 1;
26256 }
26257
26258 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26259 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26260 hlinfo->mouse_face_window = Qnil;
26261 hlinfo->mouse_face_overlay = Qnil;
26262 return cleared;
26263 }
26264
26265 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26266 within the mouse face on that window. */
26267 static int
26268 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26269 {
26270 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26271
26272 /* Quickly resolve the easy cases. */
26273 if (!(WINDOWP (hlinfo->mouse_face_window)
26274 && XWINDOW (hlinfo->mouse_face_window) == w))
26275 return 0;
26276 if (vpos < hlinfo->mouse_face_beg_row
26277 || vpos > hlinfo->mouse_face_end_row)
26278 return 0;
26279 if (vpos > hlinfo->mouse_face_beg_row
26280 && vpos < hlinfo->mouse_face_end_row)
26281 return 1;
26282
26283 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26284 {
26285 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26286 {
26287 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26288 return 1;
26289 }
26290 else if ((vpos == hlinfo->mouse_face_beg_row
26291 && hpos >= hlinfo->mouse_face_beg_col)
26292 || (vpos == hlinfo->mouse_face_end_row
26293 && hpos < hlinfo->mouse_face_end_col))
26294 return 1;
26295 }
26296 else
26297 {
26298 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26299 {
26300 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26301 return 1;
26302 }
26303 else if ((vpos == hlinfo->mouse_face_beg_row
26304 && hpos <= hlinfo->mouse_face_beg_col)
26305 || (vpos == hlinfo->mouse_face_end_row
26306 && hpos > hlinfo->mouse_face_end_col))
26307 return 1;
26308 }
26309 return 0;
26310 }
26311
26312
26313 /* EXPORT:
26314 Non-zero if physical cursor of window W is within mouse face. */
26315
26316 int
26317 cursor_in_mouse_face_p (struct window *w)
26318 {
26319 int hpos = w->phys_cursor.hpos;
26320 int vpos = w->phys_cursor.vpos;
26321 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26322
26323 /* When the window is hscrolled, cursor hpos can legitimately be out
26324 of bounds, but we draw the cursor at the corresponding window
26325 margin in that case. */
26326 if (!row->reversed_p && hpos < 0)
26327 hpos = 0;
26328 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26329 hpos = row->used[TEXT_AREA] - 1;
26330
26331 return coords_in_mouse_face_p (w, hpos, vpos);
26332 }
26333
26334
26335 \f
26336 /* Find the glyph rows START_ROW and END_ROW of window W that display
26337 characters between buffer positions START_CHARPOS and END_CHARPOS
26338 (excluding END_CHARPOS). DISP_STRING is a display string that
26339 covers these buffer positions. This is similar to
26340 row_containing_pos, but is more accurate when bidi reordering makes
26341 buffer positions change non-linearly with glyph rows. */
26342 static void
26343 rows_from_pos_range (struct window *w,
26344 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26345 Lisp_Object disp_string,
26346 struct glyph_row **start, struct glyph_row **end)
26347 {
26348 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26349 int last_y = window_text_bottom_y (w);
26350 struct glyph_row *row;
26351
26352 *start = NULL;
26353 *end = NULL;
26354
26355 while (!first->enabled_p
26356 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26357 first++;
26358
26359 /* Find the START row. */
26360 for (row = first;
26361 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26362 row++)
26363 {
26364 /* A row can potentially be the START row if the range of the
26365 characters it displays intersects the range
26366 [START_CHARPOS..END_CHARPOS). */
26367 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26368 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26369 /* See the commentary in row_containing_pos, for the
26370 explanation of the complicated way to check whether
26371 some position is beyond the end of the characters
26372 displayed by a row. */
26373 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26374 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26375 && !row->ends_at_zv_p
26376 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26377 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26378 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26379 && !row->ends_at_zv_p
26380 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26381 {
26382 /* Found a candidate row. Now make sure at least one of the
26383 glyphs it displays has a charpos from the range
26384 [START_CHARPOS..END_CHARPOS).
26385
26386 This is not obvious because bidi reordering could make
26387 buffer positions of a row be 1,2,3,102,101,100, and if we
26388 want to highlight characters in [50..60), we don't want
26389 this row, even though [50..60) does intersect [1..103),
26390 the range of character positions given by the row's start
26391 and end positions. */
26392 struct glyph *g = row->glyphs[TEXT_AREA];
26393 struct glyph *e = g + row->used[TEXT_AREA];
26394
26395 while (g < e)
26396 {
26397 if (((BUFFERP (g->object) || INTEGERP (g->object))
26398 && start_charpos <= g->charpos && g->charpos < end_charpos)
26399 /* A glyph that comes from DISP_STRING is by
26400 definition to be highlighted. */
26401 || EQ (g->object, disp_string))
26402 *start = row;
26403 g++;
26404 }
26405 if (*start)
26406 break;
26407 }
26408 }
26409
26410 /* Find the END row. */
26411 if (!*start
26412 /* If the last row is partially visible, start looking for END
26413 from that row, instead of starting from FIRST. */
26414 && !(row->enabled_p
26415 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26416 row = first;
26417 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26418 {
26419 struct glyph_row *next = row + 1;
26420 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26421
26422 if (!next->enabled_p
26423 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26424 /* The first row >= START whose range of displayed characters
26425 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26426 is the row END + 1. */
26427 || (start_charpos < next_start
26428 && end_charpos < next_start)
26429 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26430 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26431 && !next->ends_at_zv_p
26432 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26433 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26434 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26435 && !next->ends_at_zv_p
26436 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26437 {
26438 *end = row;
26439 break;
26440 }
26441 else
26442 {
26443 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26444 but none of the characters it displays are in the range, it is
26445 also END + 1. */
26446 struct glyph *g = next->glyphs[TEXT_AREA];
26447 struct glyph *s = g;
26448 struct glyph *e = g + next->used[TEXT_AREA];
26449
26450 while (g < e)
26451 {
26452 if (((BUFFERP (g->object) || INTEGERP (g->object))
26453 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26454 /* If the buffer position of the first glyph in
26455 the row is equal to END_CHARPOS, it means
26456 the last character to be highlighted is the
26457 newline of ROW, and we must consider NEXT as
26458 END, not END+1. */
26459 || (((!next->reversed_p && g == s)
26460 || (next->reversed_p && g == e - 1))
26461 && (g->charpos == end_charpos
26462 /* Special case for when NEXT is an
26463 empty line at ZV. */
26464 || (g->charpos == -1
26465 && !row->ends_at_zv_p
26466 && next_start == end_charpos)))))
26467 /* A glyph that comes from DISP_STRING is by
26468 definition to be highlighted. */
26469 || EQ (g->object, disp_string))
26470 break;
26471 g++;
26472 }
26473 if (g == e)
26474 {
26475 *end = row;
26476 break;
26477 }
26478 /* The first row that ends at ZV must be the last to be
26479 highlighted. */
26480 else if (next->ends_at_zv_p)
26481 {
26482 *end = next;
26483 break;
26484 }
26485 }
26486 }
26487 }
26488
26489 /* This function sets the mouse_face_* elements of HLINFO, assuming
26490 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26491 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26492 for the overlay or run of text properties specifying the mouse
26493 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26494 before-string and after-string that must also be highlighted.
26495 DISP_STRING, if non-nil, is a display string that may cover some
26496 or all of the highlighted text. */
26497
26498 static void
26499 mouse_face_from_buffer_pos (Lisp_Object window,
26500 Mouse_HLInfo *hlinfo,
26501 ptrdiff_t mouse_charpos,
26502 ptrdiff_t start_charpos,
26503 ptrdiff_t end_charpos,
26504 Lisp_Object before_string,
26505 Lisp_Object after_string,
26506 Lisp_Object disp_string)
26507 {
26508 struct window *w = XWINDOW (window);
26509 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26510 struct glyph_row *r1, *r2;
26511 struct glyph *glyph, *end;
26512 ptrdiff_t ignore, pos;
26513 int x;
26514
26515 eassert (NILP (disp_string) || STRINGP (disp_string));
26516 eassert (NILP (before_string) || STRINGP (before_string));
26517 eassert (NILP (after_string) || STRINGP (after_string));
26518
26519 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26520 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26521 if (r1 == NULL)
26522 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26523 /* If the before-string or display-string contains newlines,
26524 rows_from_pos_range skips to its last row. Move back. */
26525 if (!NILP (before_string) || !NILP (disp_string))
26526 {
26527 struct glyph_row *prev;
26528 while ((prev = r1 - 1, prev >= first)
26529 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26530 && prev->used[TEXT_AREA] > 0)
26531 {
26532 struct glyph *beg = prev->glyphs[TEXT_AREA];
26533 glyph = beg + prev->used[TEXT_AREA];
26534 while (--glyph >= beg && INTEGERP (glyph->object));
26535 if (glyph < beg
26536 || !(EQ (glyph->object, before_string)
26537 || EQ (glyph->object, disp_string)))
26538 break;
26539 r1 = prev;
26540 }
26541 }
26542 if (r2 == NULL)
26543 {
26544 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26545 hlinfo->mouse_face_past_end = 1;
26546 }
26547 else if (!NILP (after_string))
26548 {
26549 /* If the after-string has newlines, advance to its last row. */
26550 struct glyph_row *next;
26551 struct glyph_row *last
26552 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26553
26554 for (next = r2 + 1;
26555 next <= last
26556 && next->used[TEXT_AREA] > 0
26557 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26558 ++next)
26559 r2 = next;
26560 }
26561 /* The rest of the display engine assumes that mouse_face_beg_row is
26562 either above mouse_face_end_row or identical to it. But with
26563 bidi-reordered continued lines, the row for START_CHARPOS could
26564 be below the row for END_CHARPOS. If so, swap the rows and store
26565 them in correct order. */
26566 if (r1->y > r2->y)
26567 {
26568 struct glyph_row *tem = r2;
26569
26570 r2 = r1;
26571 r1 = tem;
26572 }
26573
26574 hlinfo->mouse_face_beg_y = r1->y;
26575 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26576 hlinfo->mouse_face_end_y = r2->y;
26577 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26578
26579 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26580 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26581 could be anywhere in the row and in any order. The strategy
26582 below is to find the leftmost and the rightmost glyph that
26583 belongs to either of these 3 strings, or whose position is
26584 between START_CHARPOS and END_CHARPOS, and highlight all the
26585 glyphs between those two. This may cover more than just the text
26586 between START_CHARPOS and END_CHARPOS if the range of characters
26587 strides the bidi level boundary, e.g. if the beginning is in R2L
26588 text while the end is in L2R text or vice versa. */
26589 if (!r1->reversed_p)
26590 {
26591 /* This row is in a left to right paragraph. Scan it left to
26592 right. */
26593 glyph = r1->glyphs[TEXT_AREA];
26594 end = glyph + r1->used[TEXT_AREA];
26595 x = r1->x;
26596
26597 /* Skip truncation glyphs at the start of the glyph row. */
26598 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26599 for (; glyph < end
26600 && INTEGERP (glyph->object)
26601 && glyph->charpos < 0;
26602 ++glyph)
26603 x += glyph->pixel_width;
26604
26605 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26606 or DISP_STRING, and the first glyph from buffer whose
26607 position is between START_CHARPOS and END_CHARPOS. */
26608 for (; glyph < end
26609 && !INTEGERP (glyph->object)
26610 && !EQ (glyph->object, disp_string)
26611 && !(BUFFERP (glyph->object)
26612 && (glyph->charpos >= start_charpos
26613 && glyph->charpos < end_charpos));
26614 ++glyph)
26615 {
26616 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26617 are present at buffer positions between START_CHARPOS and
26618 END_CHARPOS, or if they come from an overlay. */
26619 if (EQ (glyph->object, before_string))
26620 {
26621 pos = string_buffer_position (before_string,
26622 start_charpos);
26623 /* If pos == 0, it means before_string came from an
26624 overlay, not from a buffer position. */
26625 if (!pos || (pos >= start_charpos && pos < end_charpos))
26626 break;
26627 }
26628 else if (EQ (glyph->object, after_string))
26629 {
26630 pos = string_buffer_position (after_string, end_charpos);
26631 if (!pos || (pos >= start_charpos && pos < end_charpos))
26632 break;
26633 }
26634 x += glyph->pixel_width;
26635 }
26636 hlinfo->mouse_face_beg_x = x;
26637 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26638 }
26639 else
26640 {
26641 /* This row is in a right to left paragraph. Scan it right to
26642 left. */
26643 struct glyph *g;
26644
26645 end = r1->glyphs[TEXT_AREA] - 1;
26646 glyph = end + r1->used[TEXT_AREA];
26647
26648 /* Skip truncation glyphs at the start of the glyph row. */
26649 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26650 for (; glyph > end
26651 && INTEGERP (glyph->object)
26652 && glyph->charpos < 0;
26653 --glyph)
26654 ;
26655
26656 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26657 or DISP_STRING, and the first glyph from buffer whose
26658 position is between START_CHARPOS and END_CHARPOS. */
26659 for (; glyph > end
26660 && !INTEGERP (glyph->object)
26661 && !EQ (glyph->object, disp_string)
26662 && !(BUFFERP (glyph->object)
26663 && (glyph->charpos >= start_charpos
26664 && glyph->charpos < end_charpos));
26665 --glyph)
26666 {
26667 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26668 are present at buffer positions between START_CHARPOS and
26669 END_CHARPOS, or if they come from an overlay. */
26670 if (EQ (glyph->object, before_string))
26671 {
26672 pos = string_buffer_position (before_string, start_charpos);
26673 /* If pos == 0, it means before_string came from an
26674 overlay, not from a buffer position. */
26675 if (!pos || (pos >= start_charpos && pos < end_charpos))
26676 break;
26677 }
26678 else if (EQ (glyph->object, after_string))
26679 {
26680 pos = string_buffer_position (after_string, end_charpos);
26681 if (!pos || (pos >= start_charpos && pos < end_charpos))
26682 break;
26683 }
26684 }
26685
26686 glyph++; /* first glyph to the right of the highlighted area */
26687 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26688 x += g->pixel_width;
26689 hlinfo->mouse_face_beg_x = x;
26690 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26691 }
26692
26693 /* If the highlight ends in a different row, compute GLYPH and END
26694 for the end row. Otherwise, reuse the values computed above for
26695 the row where the highlight begins. */
26696 if (r2 != r1)
26697 {
26698 if (!r2->reversed_p)
26699 {
26700 glyph = r2->glyphs[TEXT_AREA];
26701 end = glyph + r2->used[TEXT_AREA];
26702 x = r2->x;
26703 }
26704 else
26705 {
26706 end = r2->glyphs[TEXT_AREA] - 1;
26707 glyph = end + r2->used[TEXT_AREA];
26708 }
26709 }
26710
26711 if (!r2->reversed_p)
26712 {
26713 /* Skip truncation and continuation glyphs near the end of the
26714 row, and also blanks and stretch glyphs inserted by
26715 extend_face_to_end_of_line. */
26716 while (end > glyph
26717 && INTEGERP ((end - 1)->object))
26718 --end;
26719 /* Scan the rest of the glyph row from the end, looking for the
26720 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26721 DISP_STRING, or whose position is between START_CHARPOS
26722 and END_CHARPOS */
26723 for (--end;
26724 end > glyph
26725 && !INTEGERP (end->object)
26726 && !EQ (end->object, disp_string)
26727 && !(BUFFERP (end->object)
26728 && (end->charpos >= start_charpos
26729 && end->charpos < end_charpos));
26730 --end)
26731 {
26732 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26733 are present at buffer positions between START_CHARPOS and
26734 END_CHARPOS, or if they come from an overlay. */
26735 if (EQ (end->object, before_string))
26736 {
26737 pos = string_buffer_position (before_string, start_charpos);
26738 if (!pos || (pos >= start_charpos && pos < end_charpos))
26739 break;
26740 }
26741 else if (EQ (end->object, after_string))
26742 {
26743 pos = string_buffer_position (after_string, end_charpos);
26744 if (!pos || (pos >= start_charpos && pos < end_charpos))
26745 break;
26746 }
26747 }
26748 /* Find the X coordinate of the last glyph to be highlighted. */
26749 for (; glyph <= end; ++glyph)
26750 x += glyph->pixel_width;
26751
26752 hlinfo->mouse_face_end_x = x;
26753 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26754 }
26755 else
26756 {
26757 /* Skip truncation and continuation glyphs near the end of the
26758 row, and also blanks and stretch glyphs inserted by
26759 extend_face_to_end_of_line. */
26760 x = r2->x;
26761 end++;
26762 while (end < glyph
26763 && INTEGERP (end->object))
26764 {
26765 x += end->pixel_width;
26766 ++end;
26767 }
26768 /* Scan the rest of the glyph row from the end, looking for the
26769 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26770 DISP_STRING, or whose position is between START_CHARPOS
26771 and END_CHARPOS */
26772 for ( ;
26773 end < glyph
26774 && !INTEGERP (end->object)
26775 && !EQ (end->object, disp_string)
26776 && !(BUFFERP (end->object)
26777 && (end->charpos >= start_charpos
26778 && end->charpos < end_charpos));
26779 ++end)
26780 {
26781 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26782 are present at buffer positions between START_CHARPOS and
26783 END_CHARPOS, or if they come from an overlay. */
26784 if (EQ (end->object, before_string))
26785 {
26786 pos = string_buffer_position (before_string, start_charpos);
26787 if (!pos || (pos >= start_charpos && pos < end_charpos))
26788 break;
26789 }
26790 else if (EQ (end->object, after_string))
26791 {
26792 pos = string_buffer_position (after_string, end_charpos);
26793 if (!pos || (pos >= start_charpos && pos < end_charpos))
26794 break;
26795 }
26796 x += end->pixel_width;
26797 }
26798 /* If we exited the above loop because we arrived at the last
26799 glyph of the row, and its buffer position is still not in
26800 range, it means the last character in range is the preceding
26801 newline. Bump the end column and x values to get past the
26802 last glyph. */
26803 if (end == glyph
26804 && BUFFERP (end->object)
26805 && (end->charpos < start_charpos
26806 || end->charpos >= end_charpos))
26807 {
26808 x += end->pixel_width;
26809 ++end;
26810 }
26811 hlinfo->mouse_face_end_x = x;
26812 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26813 }
26814
26815 hlinfo->mouse_face_window = window;
26816 hlinfo->mouse_face_face_id
26817 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26818 mouse_charpos + 1,
26819 !hlinfo->mouse_face_hidden, -1);
26820 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26821 }
26822
26823 /* The following function is not used anymore (replaced with
26824 mouse_face_from_string_pos), but I leave it here for the time
26825 being, in case someone would. */
26826
26827 #if 0 /* not used */
26828
26829 /* Find the position of the glyph for position POS in OBJECT in
26830 window W's current matrix, and return in *X, *Y the pixel
26831 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26832
26833 RIGHT_P non-zero means return the position of the right edge of the
26834 glyph, RIGHT_P zero means return the left edge position.
26835
26836 If no glyph for POS exists in the matrix, return the position of
26837 the glyph with the next smaller position that is in the matrix, if
26838 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26839 exists in the matrix, return the position of the glyph with the
26840 next larger position in OBJECT.
26841
26842 Value is non-zero if a glyph was found. */
26843
26844 static int
26845 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26846 int *hpos, int *vpos, int *x, int *y, int right_p)
26847 {
26848 int yb = window_text_bottom_y (w);
26849 struct glyph_row *r;
26850 struct glyph *best_glyph = NULL;
26851 struct glyph_row *best_row = NULL;
26852 int best_x = 0;
26853
26854 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26855 r->enabled_p && r->y < yb;
26856 ++r)
26857 {
26858 struct glyph *g = r->glyphs[TEXT_AREA];
26859 struct glyph *e = g + r->used[TEXT_AREA];
26860 int gx;
26861
26862 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26863 if (EQ (g->object, object))
26864 {
26865 if (g->charpos == pos)
26866 {
26867 best_glyph = g;
26868 best_x = gx;
26869 best_row = r;
26870 goto found;
26871 }
26872 else if (best_glyph == NULL
26873 || ((eabs (g->charpos - pos)
26874 < eabs (best_glyph->charpos - pos))
26875 && (right_p
26876 ? g->charpos < pos
26877 : g->charpos > pos)))
26878 {
26879 best_glyph = g;
26880 best_x = gx;
26881 best_row = r;
26882 }
26883 }
26884 }
26885
26886 found:
26887
26888 if (best_glyph)
26889 {
26890 *x = best_x;
26891 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26892
26893 if (right_p)
26894 {
26895 *x += best_glyph->pixel_width;
26896 ++*hpos;
26897 }
26898
26899 *y = best_row->y;
26900 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
26901 }
26902
26903 return best_glyph != NULL;
26904 }
26905 #endif /* not used */
26906
26907 /* Find the positions of the first and the last glyphs in window W's
26908 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26909 (assumed to be a string), and return in HLINFO's mouse_face_*
26910 members the pixel and column/row coordinates of those glyphs. */
26911
26912 static void
26913 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26914 Lisp_Object object,
26915 ptrdiff_t startpos, ptrdiff_t endpos)
26916 {
26917 int yb = window_text_bottom_y (w);
26918 struct glyph_row *r;
26919 struct glyph *g, *e;
26920 int gx;
26921 int found = 0;
26922
26923 /* Find the glyph row with at least one position in the range
26924 [STARTPOS..ENDPOS], and the first glyph in that row whose
26925 position belongs to that range. */
26926 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26927 r->enabled_p && r->y < yb;
26928 ++r)
26929 {
26930 if (!r->reversed_p)
26931 {
26932 g = r->glyphs[TEXT_AREA];
26933 e = g + r->used[TEXT_AREA];
26934 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26935 if (EQ (g->object, object)
26936 && startpos <= g->charpos && g->charpos <= endpos)
26937 {
26938 hlinfo->mouse_face_beg_row
26939 = MATRIX_ROW_VPOS (r, w->current_matrix);
26940 hlinfo->mouse_face_beg_y = r->y;
26941 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26942 hlinfo->mouse_face_beg_x = gx;
26943 found = 1;
26944 break;
26945 }
26946 }
26947 else
26948 {
26949 struct glyph *g1;
26950
26951 e = r->glyphs[TEXT_AREA];
26952 g = e + r->used[TEXT_AREA];
26953 for ( ; g > e; --g)
26954 if (EQ ((g-1)->object, object)
26955 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26956 {
26957 hlinfo->mouse_face_beg_row
26958 = MATRIX_ROW_VPOS (r, w->current_matrix);
26959 hlinfo->mouse_face_beg_y = r->y;
26960 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26961 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26962 gx += g1->pixel_width;
26963 hlinfo->mouse_face_beg_x = gx;
26964 found = 1;
26965 break;
26966 }
26967 }
26968 if (found)
26969 break;
26970 }
26971
26972 if (!found)
26973 return;
26974
26975 /* Starting with the next row, look for the first row which does NOT
26976 include any glyphs whose positions are in the range. */
26977 for (++r; r->enabled_p && r->y < yb; ++r)
26978 {
26979 g = r->glyphs[TEXT_AREA];
26980 e = g + r->used[TEXT_AREA];
26981 found = 0;
26982 for ( ; g < e; ++g)
26983 if (EQ (g->object, object)
26984 && startpos <= g->charpos && g->charpos <= endpos)
26985 {
26986 found = 1;
26987 break;
26988 }
26989 if (!found)
26990 break;
26991 }
26992
26993 /* The highlighted region ends on the previous row. */
26994 r--;
26995
26996 /* Set the end row and its vertical pixel coordinate. */
26997 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
26998 hlinfo->mouse_face_end_y = r->y;
26999
27000 /* Compute and set the end column and the end column's horizontal
27001 pixel coordinate. */
27002 if (!r->reversed_p)
27003 {
27004 g = r->glyphs[TEXT_AREA];
27005 e = g + r->used[TEXT_AREA];
27006 for ( ; e > g; --e)
27007 if (EQ ((e-1)->object, object)
27008 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27009 break;
27010 hlinfo->mouse_face_end_col = e - g;
27011
27012 for (gx = r->x; g < e; ++g)
27013 gx += g->pixel_width;
27014 hlinfo->mouse_face_end_x = gx;
27015 }
27016 else
27017 {
27018 e = r->glyphs[TEXT_AREA];
27019 g = e + r->used[TEXT_AREA];
27020 for (gx = r->x ; e < g; ++e)
27021 {
27022 if (EQ (e->object, object)
27023 && startpos <= e->charpos && e->charpos <= endpos)
27024 break;
27025 gx += e->pixel_width;
27026 }
27027 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27028 hlinfo->mouse_face_end_x = gx;
27029 }
27030 }
27031
27032 #ifdef HAVE_WINDOW_SYSTEM
27033
27034 /* See if position X, Y is within a hot-spot of an image. */
27035
27036 static int
27037 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27038 {
27039 if (!CONSP (hot_spot))
27040 return 0;
27041
27042 if (EQ (XCAR (hot_spot), Qrect))
27043 {
27044 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27045 Lisp_Object rect = XCDR (hot_spot);
27046 Lisp_Object tem;
27047 if (!CONSP (rect))
27048 return 0;
27049 if (!CONSP (XCAR (rect)))
27050 return 0;
27051 if (!CONSP (XCDR (rect)))
27052 return 0;
27053 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27054 return 0;
27055 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27056 return 0;
27057 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27058 return 0;
27059 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27060 return 0;
27061 return 1;
27062 }
27063 else if (EQ (XCAR (hot_spot), Qcircle))
27064 {
27065 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27066 Lisp_Object circ = XCDR (hot_spot);
27067 Lisp_Object lr, lx0, ly0;
27068 if (CONSP (circ)
27069 && CONSP (XCAR (circ))
27070 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27071 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27072 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27073 {
27074 double r = XFLOATINT (lr);
27075 double dx = XINT (lx0) - x;
27076 double dy = XINT (ly0) - y;
27077 return (dx * dx + dy * dy <= r * r);
27078 }
27079 }
27080 else if (EQ (XCAR (hot_spot), Qpoly))
27081 {
27082 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27083 if (VECTORP (XCDR (hot_spot)))
27084 {
27085 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27086 Lisp_Object *poly = v->contents;
27087 ptrdiff_t n = v->header.size;
27088 ptrdiff_t i;
27089 int inside = 0;
27090 Lisp_Object lx, ly;
27091 int x0, y0;
27092
27093 /* Need an even number of coordinates, and at least 3 edges. */
27094 if (n < 6 || n & 1)
27095 return 0;
27096
27097 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27098 If count is odd, we are inside polygon. Pixels on edges
27099 may or may not be included depending on actual geometry of the
27100 polygon. */
27101 if ((lx = poly[n-2], !INTEGERP (lx))
27102 || (ly = poly[n-1], !INTEGERP (lx)))
27103 return 0;
27104 x0 = XINT (lx), y0 = XINT (ly);
27105 for (i = 0; i < n; i += 2)
27106 {
27107 int x1 = x0, y1 = y0;
27108 if ((lx = poly[i], !INTEGERP (lx))
27109 || (ly = poly[i+1], !INTEGERP (ly)))
27110 return 0;
27111 x0 = XINT (lx), y0 = XINT (ly);
27112
27113 /* Does this segment cross the X line? */
27114 if (x0 >= x)
27115 {
27116 if (x1 >= x)
27117 continue;
27118 }
27119 else if (x1 < x)
27120 continue;
27121 if (y > y0 && y > y1)
27122 continue;
27123 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27124 inside = !inside;
27125 }
27126 return inside;
27127 }
27128 }
27129 return 0;
27130 }
27131
27132 Lisp_Object
27133 find_hot_spot (Lisp_Object map, int x, int y)
27134 {
27135 while (CONSP (map))
27136 {
27137 if (CONSP (XCAR (map))
27138 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27139 return XCAR (map);
27140 map = XCDR (map);
27141 }
27142
27143 return Qnil;
27144 }
27145
27146 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27147 3, 3, 0,
27148 doc: /* Lookup in image map MAP coordinates X and Y.
27149 An image map is an alist where each element has the format (AREA ID PLIST).
27150 An AREA is specified as either a rectangle, a circle, or a polygon:
27151 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27152 pixel coordinates of the upper left and bottom right corners.
27153 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27154 and the radius of the circle; r may be a float or integer.
27155 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27156 vector describes one corner in the polygon.
27157 Returns the alist element for the first matching AREA in MAP. */)
27158 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27159 {
27160 if (NILP (map))
27161 return Qnil;
27162
27163 CHECK_NUMBER (x);
27164 CHECK_NUMBER (y);
27165
27166 return find_hot_spot (map,
27167 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27168 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27169 }
27170
27171
27172 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27173 static void
27174 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27175 {
27176 /* Do not change cursor shape while dragging mouse. */
27177 if (!NILP (do_mouse_tracking))
27178 return;
27179
27180 if (!NILP (pointer))
27181 {
27182 if (EQ (pointer, Qarrow))
27183 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27184 else if (EQ (pointer, Qhand))
27185 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27186 else if (EQ (pointer, Qtext))
27187 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27188 else if (EQ (pointer, intern ("hdrag")))
27189 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27190 #ifdef HAVE_X_WINDOWS
27191 else if (EQ (pointer, intern ("vdrag")))
27192 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27193 #endif
27194 else if (EQ (pointer, intern ("hourglass")))
27195 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27196 else if (EQ (pointer, Qmodeline))
27197 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27198 else
27199 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27200 }
27201
27202 if (cursor != No_Cursor)
27203 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27204 }
27205
27206 #endif /* HAVE_WINDOW_SYSTEM */
27207
27208 /* Take proper action when mouse has moved to the mode or header line
27209 or marginal area AREA of window W, x-position X and y-position Y.
27210 X is relative to the start of the text display area of W, so the
27211 width of bitmap areas and scroll bars must be subtracted to get a
27212 position relative to the start of the mode line. */
27213
27214 static void
27215 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27216 enum window_part area)
27217 {
27218 struct window *w = XWINDOW (window);
27219 struct frame *f = XFRAME (w->frame);
27220 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27221 #ifdef HAVE_WINDOW_SYSTEM
27222 Display_Info *dpyinfo;
27223 #endif
27224 Cursor cursor = No_Cursor;
27225 Lisp_Object pointer = Qnil;
27226 int dx, dy, width, height;
27227 ptrdiff_t charpos;
27228 Lisp_Object string, object = Qnil;
27229 Lisp_Object pos IF_LINT (= Qnil), help;
27230
27231 Lisp_Object mouse_face;
27232 int original_x_pixel = x;
27233 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27234 struct glyph_row *row IF_LINT (= 0);
27235
27236 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27237 {
27238 int x0;
27239 struct glyph *end;
27240
27241 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27242 returns them in row/column units! */
27243 string = mode_line_string (w, area, &x, &y, &charpos,
27244 &object, &dx, &dy, &width, &height);
27245
27246 row = (area == ON_MODE_LINE
27247 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27248 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27249
27250 /* Find the glyph under the mouse pointer. */
27251 if (row->mode_line_p && row->enabled_p)
27252 {
27253 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27254 end = glyph + row->used[TEXT_AREA];
27255
27256 for (x0 = original_x_pixel;
27257 glyph < end && x0 >= glyph->pixel_width;
27258 ++glyph)
27259 x0 -= glyph->pixel_width;
27260
27261 if (glyph >= end)
27262 glyph = NULL;
27263 }
27264 }
27265 else
27266 {
27267 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27268 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27269 returns them in row/column units! */
27270 string = marginal_area_string (w, area, &x, &y, &charpos,
27271 &object, &dx, &dy, &width, &height);
27272 }
27273
27274 help = Qnil;
27275
27276 #ifdef HAVE_WINDOW_SYSTEM
27277 if (IMAGEP (object))
27278 {
27279 Lisp_Object image_map, hotspot;
27280 if ((image_map = Fplist_get (XCDR (object), QCmap),
27281 !NILP (image_map))
27282 && (hotspot = find_hot_spot (image_map, dx, dy),
27283 CONSP (hotspot))
27284 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27285 {
27286 Lisp_Object plist;
27287
27288 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27289 If so, we could look for mouse-enter, mouse-leave
27290 properties in PLIST (and do something...). */
27291 hotspot = XCDR (hotspot);
27292 if (CONSP (hotspot)
27293 && (plist = XCAR (hotspot), CONSP (plist)))
27294 {
27295 pointer = Fplist_get (plist, Qpointer);
27296 if (NILP (pointer))
27297 pointer = Qhand;
27298 help = Fplist_get (plist, Qhelp_echo);
27299 if (!NILP (help))
27300 {
27301 help_echo_string = help;
27302 XSETWINDOW (help_echo_window, w);
27303 help_echo_object = w->contents;
27304 help_echo_pos = charpos;
27305 }
27306 }
27307 }
27308 if (NILP (pointer))
27309 pointer = Fplist_get (XCDR (object), QCpointer);
27310 }
27311 #endif /* HAVE_WINDOW_SYSTEM */
27312
27313 if (STRINGP (string))
27314 pos = make_number (charpos);
27315
27316 /* Set the help text and mouse pointer. If the mouse is on a part
27317 of the mode line without any text (e.g. past the right edge of
27318 the mode line text), use the default help text and pointer. */
27319 if (STRINGP (string) || area == ON_MODE_LINE)
27320 {
27321 /* Arrange to display the help by setting the global variables
27322 help_echo_string, help_echo_object, and help_echo_pos. */
27323 if (NILP (help))
27324 {
27325 if (STRINGP (string))
27326 help = Fget_text_property (pos, Qhelp_echo, string);
27327
27328 if (!NILP (help))
27329 {
27330 help_echo_string = help;
27331 XSETWINDOW (help_echo_window, w);
27332 help_echo_object = string;
27333 help_echo_pos = charpos;
27334 }
27335 else if (area == ON_MODE_LINE)
27336 {
27337 Lisp_Object default_help
27338 = buffer_local_value_1 (Qmode_line_default_help_echo,
27339 w->contents);
27340
27341 if (STRINGP (default_help))
27342 {
27343 help_echo_string = default_help;
27344 XSETWINDOW (help_echo_window, w);
27345 help_echo_object = Qnil;
27346 help_echo_pos = -1;
27347 }
27348 }
27349 }
27350
27351 #ifdef HAVE_WINDOW_SYSTEM
27352 /* Change the mouse pointer according to what is under it. */
27353 if (FRAME_WINDOW_P (f))
27354 {
27355 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27356 if (STRINGP (string))
27357 {
27358 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27359
27360 if (NILP (pointer))
27361 pointer = Fget_text_property (pos, Qpointer, string);
27362
27363 /* Change the mouse pointer according to what is under X/Y. */
27364 if (NILP (pointer)
27365 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27366 {
27367 Lisp_Object map;
27368 map = Fget_text_property (pos, Qlocal_map, string);
27369 if (!KEYMAPP (map))
27370 map = Fget_text_property (pos, Qkeymap, string);
27371 if (!KEYMAPP (map))
27372 cursor = dpyinfo->vertical_scroll_bar_cursor;
27373 }
27374 }
27375 else
27376 /* Default mode-line pointer. */
27377 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27378 }
27379 #endif
27380 }
27381
27382 /* Change the mouse face according to what is under X/Y. */
27383 if (STRINGP (string))
27384 {
27385 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27386 if (!NILP (mouse_face)
27387 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27388 && glyph)
27389 {
27390 Lisp_Object b, e;
27391
27392 struct glyph * tmp_glyph;
27393
27394 int gpos;
27395 int gseq_length;
27396 int total_pixel_width;
27397 ptrdiff_t begpos, endpos, ignore;
27398
27399 int vpos, hpos;
27400
27401 b = Fprevious_single_property_change (make_number (charpos + 1),
27402 Qmouse_face, string, Qnil);
27403 if (NILP (b))
27404 begpos = 0;
27405 else
27406 begpos = XINT (b);
27407
27408 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27409 if (NILP (e))
27410 endpos = SCHARS (string);
27411 else
27412 endpos = XINT (e);
27413
27414 /* Calculate the glyph position GPOS of GLYPH in the
27415 displayed string, relative to the beginning of the
27416 highlighted part of the string.
27417
27418 Note: GPOS is different from CHARPOS. CHARPOS is the
27419 position of GLYPH in the internal string object. A mode
27420 line string format has structures which are converted to
27421 a flattened string by the Emacs Lisp interpreter. The
27422 internal string is an element of those structures. The
27423 displayed string is the flattened string. */
27424 tmp_glyph = row_start_glyph;
27425 while (tmp_glyph < glyph
27426 && (!(EQ (tmp_glyph->object, glyph->object)
27427 && begpos <= tmp_glyph->charpos
27428 && tmp_glyph->charpos < endpos)))
27429 tmp_glyph++;
27430 gpos = glyph - tmp_glyph;
27431
27432 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27433 the highlighted part of the displayed string to which
27434 GLYPH belongs. Note: GSEQ_LENGTH is different from
27435 SCHARS (STRING), because the latter returns the length of
27436 the internal string. */
27437 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27438 tmp_glyph > glyph
27439 && (!(EQ (tmp_glyph->object, glyph->object)
27440 && begpos <= tmp_glyph->charpos
27441 && tmp_glyph->charpos < endpos));
27442 tmp_glyph--)
27443 ;
27444 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27445
27446 /* Calculate the total pixel width of all the glyphs between
27447 the beginning of the highlighted area and GLYPH. */
27448 total_pixel_width = 0;
27449 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27450 total_pixel_width += tmp_glyph->pixel_width;
27451
27452 /* Pre calculation of re-rendering position. Note: X is in
27453 column units here, after the call to mode_line_string or
27454 marginal_area_string. */
27455 hpos = x - gpos;
27456 vpos = (area == ON_MODE_LINE
27457 ? (w->current_matrix)->nrows - 1
27458 : 0);
27459
27460 /* If GLYPH's position is included in the region that is
27461 already drawn in mouse face, we have nothing to do. */
27462 if ( EQ (window, hlinfo->mouse_face_window)
27463 && (!row->reversed_p
27464 ? (hlinfo->mouse_face_beg_col <= hpos
27465 && hpos < hlinfo->mouse_face_end_col)
27466 /* In R2L rows we swap BEG and END, see below. */
27467 : (hlinfo->mouse_face_end_col <= hpos
27468 && hpos < hlinfo->mouse_face_beg_col))
27469 && hlinfo->mouse_face_beg_row == vpos )
27470 return;
27471
27472 if (clear_mouse_face (hlinfo))
27473 cursor = No_Cursor;
27474
27475 if (!row->reversed_p)
27476 {
27477 hlinfo->mouse_face_beg_col = hpos;
27478 hlinfo->mouse_face_beg_x = original_x_pixel
27479 - (total_pixel_width + dx);
27480 hlinfo->mouse_face_end_col = hpos + gseq_length;
27481 hlinfo->mouse_face_end_x = 0;
27482 }
27483 else
27484 {
27485 /* In R2L rows, show_mouse_face expects BEG and END
27486 coordinates to be swapped. */
27487 hlinfo->mouse_face_end_col = hpos;
27488 hlinfo->mouse_face_end_x = original_x_pixel
27489 - (total_pixel_width + dx);
27490 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27491 hlinfo->mouse_face_beg_x = 0;
27492 }
27493
27494 hlinfo->mouse_face_beg_row = vpos;
27495 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27496 hlinfo->mouse_face_beg_y = 0;
27497 hlinfo->mouse_face_end_y = 0;
27498 hlinfo->mouse_face_past_end = 0;
27499 hlinfo->mouse_face_window = window;
27500
27501 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27502 charpos,
27503 0, 0, 0,
27504 &ignore,
27505 glyph->face_id,
27506 1);
27507 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27508
27509 if (NILP (pointer))
27510 pointer = Qhand;
27511 }
27512 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27513 clear_mouse_face (hlinfo);
27514 }
27515 #ifdef HAVE_WINDOW_SYSTEM
27516 if (FRAME_WINDOW_P (f))
27517 define_frame_cursor1 (f, cursor, pointer);
27518 #endif
27519 }
27520
27521
27522 /* EXPORT:
27523 Take proper action when the mouse has moved to position X, Y on
27524 frame F as regards highlighting characters that have mouse-face
27525 properties. Also de-highlighting chars where the mouse was before.
27526 X and Y can be negative or out of range. */
27527
27528 void
27529 note_mouse_highlight (struct frame *f, int x, int y)
27530 {
27531 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27532 enum window_part part = ON_NOTHING;
27533 Lisp_Object window;
27534 struct window *w;
27535 Cursor cursor = No_Cursor;
27536 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27537 struct buffer *b;
27538
27539 /* When a menu is active, don't highlight because this looks odd. */
27540 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27541 if (popup_activated ())
27542 return;
27543 #endif
27544
27545 if (NILP (Vmouse_highlight)
27546 || !f->glyphs_initialized_p
27547 || f->pointer_invisible)
27548 return;
27549
27550 hlinfo->mouse_face_mouse_x = x;
27551 hlinfo->mouse_face_mouse_y = y;
27552 hlinfo->mouse_face_mouse_frame = f;
27553
27554 if (hlinfo->mouse_face_defer)
27555 return;
27556
27557 /* Which window is that in? */
27558 window = window_from_coordinates (f, x, y, &part, 1);
27559
27560 /* If displaying active text in another window, clear that. */
27561 if (! EQ (window, hlinfo->mouse_face_window)
27562 /* Also clear if we move out of text area in same window. */
27563 || (!NILP (hlinfo->mouse_face_window)
27564 && !NILP (window)
27565 && part != ON_TEXT
27566 && part != ON_MODE_LINE
27567 && part != ON_HEADER_LINE))
27568 clear_mouse_face (hlinfo);
27569
27570 /* Not on a window -> return. */
27571 if (!WINDOWP (window))
27572 return;
27573
27574 /* Reset help_echo_string. It will get recomputed below. */
27575 help_echo_string = Qnil;
27576
27577 /* Convert to window-relative pixel coordinates. */
27578 w = XWINDOW (window);
27579 frame_to_window_pixel_xy (w, &x, &y);
27580
27581 #ifdef HAVE_WINDOW_SYSTEM
27582 /* Handle tool-bar window differently since it doesn't display a
27583 buffer. */
27584 if (EQ (window, f->tool_bar_window))
27585 {
27586 note_tool_bar_highlight (f, x, y);
27587 return;
27588 }
27589 #endif
27590
27591 /* Mouse is on the mode, header line or margin? */
27592 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27593 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27594 {
27595 note_mode_line_or_margin_highlight (window, x, y, part);
27596 return;
27597 }
27598
27599 #ifdef HAVE_WINDOW_SYSTEM
27600 if (part == ON_VERTICAL_BORDER)
27601 {
27602 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27603 help_echo_string = build_string ("drag-mouse-1: resize");
27604 }
27605 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27606 || part == ON_SCROLL_BAR)
27607 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27608 else
27609 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27610 #endif
27611
27612 /* Are we in a window whose display is up to date?
27613 And verify the buffer's text has not changed. */
27614 b = XBUFFER (w->contents);
27615 if (part == ON_TEXT
27616 && w->window_end_valid
27617 && w->last_modified == BUF_MODIFF (b)
27618 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27619 {
27620 int hpos, vpos, dx, dy, area = LAST_AREA;
27621 ptrdiff_t pos;
27622 struct glyph *glyph;
27623 Lisp_Object object;
27624 Lisp_Object mouse_face = Qnil, position;
27625 Lisp_Object *overlay_vec = NULL;
27626 ptrdiff_t i, noverlays;
27627 struct buffer *obuf;
27628 ptrdiff_t obegv, ozv;
27629 int same_region;
27630
27631 /* Find the glyph under X/Y. */
27632 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27633
27634 #ifdef HAVE_WINDOW_SYSTEM
27635 /* Look for :pointer property on image. */
27636 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27637 {
27638 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27639 if (img != NULL && IMAGEP (img->spec))
27640 {
27641 Lisp_Object image_map, hotspot;
27642 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27643 !NILP (image_map))
27644 && (hotspot = find_hot_spot (image_map,
27645 glyph->slice.img.x + dx,
27646 glyph->slice.img.y + dy),
27647 CONSP (hotspot))
27648 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27649 {
27650 Lisp_Object plist;
27651
27652 /* Could check XCAR (hotspot) to see if we enter/leave
27653 this hot-spot.
27654 If so, we could look for mouse-enter, mouse-leave
27655 properties in PLIST (and do something...). */
27656 hotspot = XCDR (hotspot);
27657 if (CONSP (hotspot)
27658 && (plist = XCAR (hotspot), CONSP (plist)))
27659 {
27660 pointer = Fplist_get (plist, Qpointer);
27661 if (NILP (pointer))
27662 pointer = Qhand;
27663 help_echo_string = Fplist_get (plist, Qhelp_echo);
27664 if (!NILP (help_echo_string))
27665 {
27666 help_echo_window = window;
27667 help_echo_object = glyph->object;
27668 help_echo_pos = glyph->charpos;
27669 }
27670 }
27671 }
27672 if (NILP (pointer))
27673 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27674 }
27675 }
27676 #endif /* HAVE_WINDOW_SYSTEM */
27677
27678 /* Clear mouse face if X/Y not over text. */
27679 if (glyph == NULL
27680 || area != TEXT_AREA
27681 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
27682 /* Glyph's OBJECT is an integer for glyphs inserted by the
27683 display engine for its internal purposes, like truncation
27684 and continuation glyphs and blanks beyond the end of
27685 line's text on text terminals. If we are over such a
27686 glyph, we are not over any text. */
27687 || INTEGERP (glyph->object)
27688 /* R2L rows have a stretch glyph at their front, which
27689 stands for no text, whereas L2R rows have no glyphs at
27690 all beyond the end of text. Treat such stretch glyphs
27691 like we do with NULL glyphs in L2R rows. */
27692 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27693 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
27694 && glyph->type == STRETCH_GLYPH
27695 && glyph->avoid_cursor_p))
27696 {
27697 if (clear_mouse_face (hlinfo))
27698 cursor = No_Cursor;
27699 #ifdef HAVE_WINDOW_SYSTEM
27700 if (FRAME_WINDOW_P (f) && NILP (pointer))
27701 {
27702 if (area != TEXT_AREA)
27703 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27704 else
27705 pointer = Vvoid_text_area_pointer;
27706 }
27707 #endif
27708 goto set_cursor;
27709 }
27710
27711 pos = glyph->charpos;
27712 object = glyph->object;
27713 if (!STRINGP (object) && !BUFFERP (object))
27714 goto set_cursor;
27715
27716 /* If we get an out-of-range value, return now; avoid an error. */
27717 if (BUFFERP (object) && pos > BUF_Z (b))
27718 goto set_cursor;
27719
27720 /* Make the window's buffer temporarily current for
27721 overlays_at and compute_char_face. */
27722 obuf = current_buffer;
27723 current_buffer = b;
27724 obegv = BEGV;
27725 ozv = ZV;
27726 BEGV = BEG;
27727 ZV = Z;
27728
27729 /* Is this char mouse-active or does it have help-echo? */
27730 position = make_number (pos);
27731
27732 if (BUFFERP (object))
27733 {
27734 /* Put all the overlays we want in a vector in overlay_vec. */
27735 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27736 /* Sort overlays into increasing priority order. */
27737 noverlays = sort_overlays (overlay_vec, noverlays, w);
27738 }
27739 else
27740 noverlays = 0;
27741
27742 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27743
27744 if (same_region)
27745 cursor = No_Cursor;
27746
27747 /* Check mouse-face highlighting. */
27748 if (! same_region
27749 /* If there exists an overlay with mouse-face overlapping
27750 the one we are currently highlighting, we have to
27751 check if we enter the overlapping overlay, and then
27752 highlight only that. */
27753 || (OVERLAYP (hlinfo->mouse_face_overlay)
27754 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27755 {
27756 /* Find the highest priority overlay with a mouse-face. */
27757 Lisp_Object overlay = Qnil;
27758 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27759 {
27760 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27761 if (!NILP (mouse_face))
27762 overlay = overlay_vec[i];
27763 }
27764
27765 /* If we're highlighting the same overlay as before, there's
27766 no need to do that again. */
27767 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27768 goto check_help_echo;
27769 hlinfo->mouse_face_overlay = overlay;
27770
27771 /* Clear the display of the old active region, if any. */
27772 if (clear_mouse_face (hlinfo))
27773 cursor = No_Cursor;
27774
27775 /* If no overlay applies, get a text property. */
27776 if (NILP (overlay))
27777 mouse_face = Fget_text_property (position, Qmouse_face, object);
27778
27779 /* Next, compute the bounds of the mouse highlighting and
27780 display it. */
27781 if (!NILP (mouse_face) && STRINGP (object))
27782 {
27783 /* The mouse-highlighting comes from a display string
27784 with a mouse-face. */
27785 Lisp_Object s, e;
27786 ptrdiff_t ignore;
27787
27788 s = Fprevious_single_property_change
27789 (make_number (pos + 1), Qmouse_face, object, Qnil);
27790 e = Fnext_single_property_change
27791 (position, Qmouse_face, object, Qnil);
27792 if (NILP (s))
27793 s = make_number (0);
27794 if (NILP (e))
27795 e = make_number (SCHARS (object) - 1);
27796 mouse_face_from_string_pos (w, hlinfo, object,
27797 XINT (s), XINT (e));
27798 hlinfo->mouse_face_past_end = 0;
27799 hlinfo->mouse_face_window = window;
27800 hlinfo->mouse_face_face_id
27801 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27802 glyph->face_id, 1);
27803 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27804 cursor = No_Cursor;
27805 }
27806 else
27807 {
27808 /* The mouse-highlighting, if any, comes from an overlay
27809 or text property in the buffer. */
27810 Lisp_Object buffer IF_LINT (= Qnil);
27811 Lisp_Object disp_string IF_LINT (= Qnil);
27812
27813 if (STRINGP (object))
27814 {
27815 /* If we are on a display string with no mouse-face,
27816 check if the text under it has one. */
27817 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27818 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27819 pos = string_buffer_position (object, start);
27820 if (pos > 0)
27821 {
27822 mouse_face = get_char_property_and_overlay
27823 (make_number (pos), Qmouse_face, w->contents, &overlay);
27824 buffer = w->contents;
27825 disp_string = object;
27826 }
27827 }
27828 else
27829 {
27830 buffer = object;
27831 disp_string = Qnil;
27832 }
27833
27834 if (!NILP (mouse_face))
27835 {
27836 Lisp_Object before, after;
27837 Lisp_Object before_string, after_string;
27838 /* To correctly find the limits of mouse highlight
27839 in a bidi-reordered buffer, we must not use the
27840 optimization of limiting the search in
27841 previous-single-property-change and
27842 next-single-property-change, because
27843 rows_from_pos_range needs the real start and end
27844 positions to DTRT in this case. That's because
27845 the first row visible in a window does not
27846 necessarily display the character whose position
27847 is the smallest. */
27848 Lisp_Object lim1 =
27849 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27850 ? Fmarker_position (w->start)
27851 : Qnil;
27852 Lisp_Object lim2 =
27853 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27854 ? make_number (BUF_Z (XBUFFER (buffer))
27855 - XFASTINT (w->window_end_pos))
27856 : Qnil;
27857
27858 if (NILP (overlay))
27859 {
27860 /* Handle the text property case. */
27861 before = Fprevious_single_property_change
27862 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27863 after = Fnext_single_property_change
27864 (make_number (pos), Qmouse_face, buffer, lim2);
27865 before_string = after_string = Qnil;
27866 }
27867 else
27868 {
27869 /* Handle the overlay case. */
27870 before = Foverlay_start (overlay);
27871 after = Foverlay_end (overlay);
27872 before_string = Foverlay_get (overlay, Qbefore_string);
27873 after_string = Foverlay_get (overlay, Qafter_string);
27874
27875 if (!STRINGP (before_string)) before_string = Qnil;
27876 if (!STRINGP (after_string)) after_string = Qnil;
27877 }
27878
27879 mouse_face_from_buffer_pos (window, hlinfo, pos,
27880 NILP (before)
27881 ? 1
27882 : XFASTINT (before),
27883 NILP (after)
27884 ? BUF_Z (XBUFFER (buffer))
27885 : XFASTINT (after),
27886 before_string, after_string,
27887 disp_string);
27888 cursor = No_Cursor;
27889 }
27890 }
27891 }
27892
27893 check_help_echo:
27894
27895 /* Look for a `help-echo' property. */
27896 if (NILP (help_echo_string)) {
27897 Lisp_Object help, overlay;
27898
27899 /* Check overlays first. */
27900 help = overlay = Qnil;
27901 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27902 {
27903 overlay = overlay_vec[i];
27904 help = Foverlay_get (overlay, Qhelp_echo);
27905 }
27906
27907 if (!NILP (help))
27908 {
27909 help_echo_string = help;
27910 help_echo_window = window;
27911 help_echo_object = overlay;
27912 help_echo_pos = pos;
27913 }
27914 else
27915 {
27916 Lisp_Object obj = glyph->object;
27917 ptrdiff_t charpos = glyph->charpos;
27918
27919 /* Try text properties. */
27920 if (STRINGP (obj)
27921 && charpos >= 0
27922 && charpos < SCHARS (obj))
27923 {
27924 help = Fget_text_property (make_number (charpos),
27925 Qhelp_echo, obj);
27926 if (NILP (help))
27927 {
27928 /* If the string itself doesn't specify a help-echo,
27929 see if the buffer text ``under'' it does. */
27930 struct glyph_row *r
27931 = MATRIX_ROW (w->current_matrix, vpos);
27932 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27933 ptrdiff_t p = string_buffer_position (obj, start);
27934 if (p > 0)
27935 {
27936 help = Fget_char_property (make_number (p),
27937 Qhelp_echo, w->contents);
27938 if (!NILP (help))
27939 {
27940 charpos = p;
27941 obj = w->contents;
27942 }
27943 }
27944 }
27945 }
27946 else if (BUFFERP (obj)
27947 && charpos >= BEGV
27948 && charpos < ZV)
27949 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27950 obj);
27951
27952 if (!NILP (help))
27953 {
27954 help_echo_string = help;
27955 help_echo_window = window;
27956 help_echo_object = obj;
27957 help_echo_pos = charpos;
27958 }
27959 }
27960 }
27961
27962 #ifdef HAVE_WINDOW_SYSTEM
27963 /* Look for a `pointer' property. */
27964 if (FRAME_WINDOW_P (f) && NILP (pointer))
27965 {
27966 /* Check overlays first. */
27967 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27968 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27969
27970 if (NILP (pointer))
27971 {
27972 Lisp_Object obj = glyph->object;
27973 ptrdiff_t charpos = glyph->charpos;
27974
27975 /* Try text properties. */
27976 if (STRINGP (obj)
27977 && charpos >= 0
27978 && charpos < SCHARS (obj))
27979 {
27980 pointer = Fget_text_property (make_number (charpos),
27981 Qpointer, obj);
27982 if (NILP (pointer))
27983 {
27984 /* If the string itself doesn't specify a pointer,
27985 see if the buffer text ``under'' it does. */
27986 struct glyph_row *r
27987 = MATRIX_ROW (w->current_matrix, vpos);
27988 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27989 ptrdiff_t p = string_buffer_position (obj, start);
27990 if (p > 0)
27991 pointer = Fget_char_property (make_number (p),
27992 Qpointer, w->contents);
27993 }
27994 }
27995 else if (BUFFERP (obj)
27996 && charpos >= BEGV
27997 && charpos < ZV)
27998 pointer = Fget_text_property (make_number (charpos),
27999 Qpointer, obj);
28000 }
28001 }
28002 #endif /* HAVE_WINDOW_SYSTEM */
28003
28004 BEGV = obegv;
28005 ZV = ozv;
28006 current_buffer = obuf;
28007 }
28008
28009 set_cursor:
28010
28011 #ifdef HAVE_WINDOW_SYSTEM
28012 if (FRAME_WINDOW_P (f))
28013 define_frame_cursor1 (f, cursor, pointer);
28014 #else
28015 /* This is here to prevent a compiler error, about "label at end of
28016 compound statement". */
28017 return;
28018 #endif
28019 }
28020
28021
28022 /* EXPORT for RIF:
28023 Clear any mouse-face on window W. This function is part of the
28024 redisplay interface, and is called from try_window_id and similar
28025 functions to ensure the mouse-highlight is off. */
28026
28027 void
28028 x_clear_window_mouse_face (struct window *w)
28029 {
28030 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28031 Lisp_Object window;
28032
28033 block_input ();
28034 XSETWINDOW (window, w);
28035 if (EQ (window, hlinfo->mouse_face_window))
28036 clear_mouse_face (hlinfo);
28037 unblock_input ();
28038 }
28039
28040
28041 /* EXPORT:
28042 Just discard the mouse face information for frame F, if any.
28043 This is used when the size of F is changed. */
28044
28045 void
28046 cancel_mouse_face (struct frame *f)
28047 {
28048 Lisp_Object window;
28049 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28050
28051 window = hlinfo->mouse_face_window;
28052 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28053 {
28054 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28055 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28056 hlinfo->mouse_face_window = Qnil;
28057 }
28058 }
28059
28060
28061 \f
28062 /***********************************************************************
28063 Exposure Events
28064 ***********************************************************************/
28065
28066 #ifdef HAVE_WINDOW_SYSTEM
28067
28068 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28069 which intersects rectangle R. R is in window-relative coordinates. */
28070
28071 static void
28072 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28073 enum glyph_row_area area)
28074 {
28075 struct glyph *first = row->glyphs[area];
28076 struct glyph *end = row->glyphs[area] + row->used[area];
28077 struct glyph *last;
28078 int first_x, start_x, x;
28079
28080 if (area == TEXT_AREA && row->fill_line_p)
28081 /* If row extends face to end of line write the whole line. */
28082 draw_glyphs (w, 0, row, area,
28083 0, row->used[area],
28084 DRAW_NORMAL_TEXT, 0);
28085 else
28086 {
28087 /* Set START_X to the window-relative start position for drawing glyphs of
28088 AREA. The first glyph of the text area can be partially visible.
28089 The first glyphs of other areas cannot. */
28090 start_x = window_box_left_offset (w, area);
28091 x = start_x;
28092 if (area == TEXT_AREA)
28093 x += row->x;
28094
28095 /* Find the first glyph that must be redrawn. */
28096 while (first < end
28097 && x + first->pixel_width < r->x)
28098 {
28099 x += first->pixel_width;
28100 ++first;
28101 }
28102
28103 /* Find the last one. */
28104 last = first;
28105 first_x = x;
28106 while (last < end
28107 && x < r->x + r->width)
28108 {
28109 x += last->pixel_width;
28110 ++last;
28111 }
28112
28113 /* Repaint. */
28114 if (last > first)
28115 draw_glyphs (w, first_x - start_x, row, area,
28116 first - row->glyphs[area], last - row->glyphs[area],
28117 DRAW_NORMAL_TEXT, 0);
28118 }
28119 }
28120
28121
28122 /* Redraw the parts of the glyph row ROW on window W intersecting
28123 rectangle R. R is in window-relative coordinates. Value is
28124 non-zero if mouse-face was overwritten. */
28125
28126 static int
28127 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28128 {
28129 eassert (row->enabled_p);
28130
28131 if (row->mode_line_p || w->pseudo_window_p)
28132 draw_glyphs (w, 0, row, TEXT_AREA,
28133 0, row->used[TEXT_AREA],
28134 DRAW_NORMAL_TEXT, 0);
28135 else
28136 {
28137 if (row->used[LEFT_MARGIN_AREA])
28138 expose_area (w, row, r, LEFT_MARGIN_AREA);
28139 if (row->used[TEXT_AREA])
28140 expose_area (w, row, r, TEXT_AREA);
28141 if (row->used[RIGHT_MARGIN_AREA])
28142 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28143 draw_row_fringe_bitmaps (w, row);
28144 }
28145
28146 return row->mouse_face_p;
28147 }
28148
28149
28150 /* Redraw those parts of glyphs rows during expose event handling that
28151 overlap other rows. Redrawing of an exposed line writes over parts
28152 of lines overlapping that exposed line; this function fixes that.
28153
28154 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28155 row in W's current matrix that is exposed and overlaps other rows.
28156 LAST_OVERLAPPING_ROW is the last such row. */
28157
28158 static void
28159 expose_overlaps (struct window *w,
28160 struct glyph_row *first_overlapping_row,
28161 struct glyph_row *last_overlapping_row,
28162 XRectangle *r)
28163 {
28164 struct glyph_row *row;
28165
28166 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28167 if (row->overlapping_p)
28168 {
28169 eassert (row->enabled_p && !row->mode_line_p);
28170
28171 row->clip = r;
28172 if (row->used[LEFT_MARGIN_AREA])
28173 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28174
28175 if (row->used[TEXT_AREA])
28176 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28177
28178 if (row->used[RIGHT_MARGIN_AREA])
28179 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28180 row->clip = NULL;
28181 }
28182 }
28183
28184
28185 /* Return non-zero if W's cursor intersects rectangle R. */
28186
28187 static int
28188 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28189 {
28190 XRectangle cr, result;
28191 struct glyph *cursor_glyph;
28192 struct glyph_row *row;
28193
28194 if (w->phys_cursor.vpos >= 0
28195 && w->phys_cursor.vpos < w->current_matrix->nrows
28196 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28197 row->enabled_p)
28198 && row->cursor_in_fringe_p)
28199 {
28200 /* Cursor is in the fringe. */
28201 cr.x = window_box_right_offset (w,
28202 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28203 ? RIGHT_MARGIN_AREA
28204 : TEXT_AREA));
28205 cr.y = row->y;
28206 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28207 cr.height = row->height;
28208 return x_intersect_rectangles (&cr, r, &result);
28209 }
28210
28211 cursor_glyph = get_phys_cursor_glyph (w);
28212 if (cursor_glyph)
28213 {
28214 /* r is relative to W's box, but w->phys_cursor.x is relative
28215 to left edge of W's TEXT area. Adjust it. */
28216 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28217 cr.y = w->phys_cursor.y;
28218 cr.width = cursor_glyph->pixel_width;
28219 cr.height = w->phys_cursor_height;
28220 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28221 I assume the effect is the same -- and this is portable. */
28222 return x_intersect_rectangles (&cr, r, &result);
28223 }
28224 /* If we don't understand the format, pretend we're not in the hot-spot. */
28225 return 0;
28226 }
28227
28228
28229 /* EXPORT:
28230 Draw a vertical window border to the right of window W if W doesn't
28231 have vertical scroll bars. */
28232
28233 void
28234 x_draw_vertical_border (struct window *w)
28235 {
28236 struct frame *f = XFRAME (WINDOW_FRAME (w));
28237
28238 /* We could do better, if we knew what type of scroll-bar the adjacent
28239 windows (on either side) have... But we don't :-(
28240 However, I think this works ok. ++KFS 2003-04-25 */
28241
28242 /* Redraw borders between horizontally adjacent windows. Don't
28243 do it for frames with vertical scroll bars because either the
28244 right scroll bar of a window, or the left scroll bar of its
28245 neighbor will suffice as a border. */
28246 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28247 return;
28248
28249 /* Note: It is necessary to redraw both the left and the right
28250 borders, for when only this single window W is being
28251 redisplayed. */
28252 if (!WINDOW_RIGHTMOST_P (w)
28253 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28254 {
28255 int x0, x1, y0, y1;
28256
28257 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28258 y1 -= 1;
28259
28260 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28261 x1 -= 1;
28262
28263 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28264 }
28265 if (!WINDOW_LEFTMOST_P (w)
28266 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28267 {
28268 int x0, x1, y0, y1;
28269
28270 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28271 y1 -= 1;
28272
28273 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28274 x0 -= 1;
28275
28276 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28277 }
28278 }
28279
28280
28281 /* Redraw the part of window W intersection rectangle FR. Pixel
28282 coordinates in FR are frame-relative. Call this function with
28283 input blocked. Value is non-zero if the exposure overwrites
28284 mouse-face. */
28285
28286 static int
28287 expose_window (struct window *w, XRectangle *fr)
28288 {
28289 struct frame *f = XFRAME (w->frame);
28290 XRectangle wr, r;
28291 int mouse_face_overwritten_p = 0;
28292
28293 /* If window is not yet fully initialized, do nothing. This can
28294 happen when toolkit scroll bars are used and a window is split.
28295 Reconfiguring the scroll bar will generate an expose for a newly
28296 created window. */
28297 if (w->current_matrix == NULL)
28298 return 0;
28299
28300 /* When we're currently updating the window, display and current
28301 matrix usually don't agree. Arrange for a thorough display
28302 later. */
28303 if (w == updated_window)
28304 {
28305 SET_FRAME_GARBAGED (f);
28306 return 0;
28307 }
28308
28309 /* Frame-relative pixel rectangle of W. */
28310 wr.x = WINDOW_LEFT_EDGE_X (w);
28311 wr.y = WINDOW_TOP_EDGE_Y (w);
28312 wr.width = WINDOW_TOTAL_WIDTH (w);
28313 wr.height = WINDOW_TOTAL_HEIGHT (w);
28314
28315 if (x_intersect_rectangles (fr, &wr, &r))
28316 {
28317 int yb = window_text_bottom_y (w);
28318 struct glyph_row *row;
28319 int cursor_cleared_p, phys_cursor_on_p;
28320 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28321
28322 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28323 r.x, r.y, r.width, r.height));
28324
28325 /* Convert to window coordinates. */
28326 r.x -= WINDOW_LEFT_EDGE_X (w);
28327 r.y -= WINDOW_TOP_EDGE_Y (w);
28328
28329 /* Turn off the cursor. */
28330 if (!w->pseudo_window_p
28331 && phys_cursor_in_rect_p (w, &r))
28332 {
28333 x_clear_cursor (w);
28334 cursor_cleared_p = 1;
28335 }
28336 else
28337 cursor_cleared_p = 0;
28338
28339 /* If the row containing the cursor extends face to end of line,
28340 then expose_area might overwrite the cursor outside the
28341 rectangle and thus notice_overwritten_cursor might clear
28342 w->phys_cursor_on_p. We remember the original value and
28343 check later if it is changed. */
28344 phys_cursor_on_p = w->phys_cursor_on_p;
28345
28346 /* Update lines intersecting rectangle R. */
28347 first_overlapping_row = last_overlapping_row = NULL;
28348 for (row = w->current_matrix->rows;
28349 row->enabled_p;
28350 ++row)
28351 {
28352 int y0 = row->y;
28353 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28354
28355 if ((y0 >= r.y && y0 < r.y + r.height)
28356 || (y1 > r.y && y1 < r.y + r.height)
28357 || (r.y >= y0 && r.y < y1)
28358 || (r.y + r.height > y0 && r.y + r.height < y1))
28359 {
28360 /* A header line may be overlapping, but there is no need
28361 to fix overlapping areas for them. KFS 2005-02-12 */
28362 if (row->overlapping_p && !row->mode_line_p)
28363 {
28364 if (first_overlapping_row == NULL)
28365 first_overlapping_row = row;
28366 last_overlapping_row = row;
28367 }
28368
28369 row->clip = fr;
28370 if (expose_line (w, row, &r))
28371 mouse_face_overwritten_p = 1;
28372 row->clip = NULL;
28373 }
28374 else if (row->overlapping_p)
28375 {
28376 /* We must redraw a row overlapping the exposed area. */
28377 if (y0 < r.y
28378 ? y0 + row->phys_height > r.y
28379 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28380 {
28381 if (first_overlapping_row == NULL)
28382 first_overlapping_row = row;
28383 last_overlapping_row = row;
28384 }
28385 }
28386
28387 if (y1 >= yb)
28388 break;
28389 }
28390
28391 /* Display the mode line if there is one. */
28392 if (WINDOW_WANTS_MODELINE_P (w)
28393 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28394 row->enabled_p)
28395 && row->y < r.y + r.height)
28396 {
28397 if (expose_line (w, row, &r))
28398 mouse_face_overwritten_p = 1;
28399 }
28400
28401 if (!w->pseudo_window_p)
28402 {
28403 /* Fix the display of overlapping rows. */
28404 if (first_overlapping_row)
28405 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28406 fr);
28407
28408 /* Draw border between windows. */
28409 x_draw_vertical_border (w);
28410
28411 /* Turn the cursor on again. */
28412 if (cursor_cleared_p
28413 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28414 update_window_cursor (w, 1);
28415 }
28416 }
28417
28418 return mouse_face_overwritten_p;
28419 }
28420
28421
28422
28423 /* Redraw (parts) of all windows in the window tree rooted at W that
28424 intersect R. R contains frame pixel coordinates. Value is
28425 non-zero if the exposure overwrites mouse-face. */
28426
28427 static int
28428 expose_window_tree (struct window *w, XRectangle *r)
28429 {
28430 struct frame *f = XFRAME (w->frame);
28431 int mouse_face_overwritten_p = 0;
28432
28433 while (w && !FRAME_GARBAGED_P (f))
28434 {
28435 if (WINDOWP (w->contents))
28436 mouse_face_overwritten_p
28437 |= expose_window_tree (XWINDOW (w->contents), r);
28438 else
28439 mouse_face_overwritten_p |= expose_window (w, r);
28440
28441 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28442 }
28443
28444 return mouse_face_overwritten_p;
28445 }
28446
28447
28448 /* EXPORT:
28449 Redisplay an exposed area of frame F. X and Y are the upper-left
28450 corner of the exposed rectangle. W and H are width and height of
28451 the exposed area. All are pixel values. W or H zero means redraw
28452 the entire frame. */
28453
28454 void
28455 expose_frame (struct frame *f, int x, int y, int w, int h)
28456 {
28457 XRectangle r;
28458 int mouse_face_overwritten_p = 0;
28459
28460 TRACE ((stderr, "expose_frame "));
28461
28462 /* No need to redraw if frame will be redrawn soon. */
28463 if (FRAME_GARBAGED_P (f))
28464 {
28465 TRACE ((stderr, " garbaged\n"));
28466 return;
28467 }
28468
28469 /* If basic faces haven't been realized yet, there is no point in
28470 trying to redraw anything. This can happen when we get an expose
28471 event while Emacs is starting, e.g. by moving another window. */
28472 if (FRAME_FACE_CACHE (f) == NULL
28473 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28474 {
28475 TRACE ((stderr, " no faces\n"));
28476 return;
28477 }
28478
28479 if (w == 0 || h == 0)
28480 {
28481 r.x = r.y = 0;
28482 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28483 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28484 }
28485 else
28486 {
28487 r.x = x;
28488 r.y = y;
28489 r.width = w;
28490 r.height = h;
28491 }
28492
28493 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28494 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28495
28496 if (WINDOWP (f->tool_bar_window))
28497 mouse_face_overwritten_p
28498 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28499
28500 #ifdef HAVE_X_WINDOWS
28501 #ifndef MSDOS
28502 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28503 if (WINDOWP (f->menu_bar_window))
28504 mouse_face_overwritten_p
28505 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28506 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28507 #endif
28508 #endif
28509
28510 /* Some window managers support a focus-follows-mouse style with
28511 delayed raising of frames. Imagine a partially obscured frame,
28512 and moving the mouse into partially obscured mouse-face on that
28513 frame. The visible part of the mouse-face will be highlighted,
28514 then the WM raises the obscured frame. With at least one WM, KDE
28515 2.1, Emacs is not getting any event for the raising of the frame
28516 (even tried with SubstructureRedirectMask), only Expose events.
28517 These expose events will draw text normally, i.e. not
28518 highlighted. Which means we must redo the highlight here.
28519 Subsume it under ``we love X''. --gerd 2001-08-15 */
28520 /* Included in Windows version because Windows most likely does not
28521 do the right thing if any third party tool offers
28522 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28523 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28524 {
28525 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28526 if (f == hlinfo->mouse_face_mouse_frame)
28527 {
28528 int mouse_x = hlinfo->mouse_face_mouse_x;
28529 int mouse_y = hlinfo->mouse_face_mouse_y;
28530 clear_mouse_face (hlinfo);
28531 note_mouse_highlight (f, mouse_x, mouse_y);
28532 }
28533 }
28534 }
28535
28536
28537 /* EXPORT:
28538 Determine the intersection of two rectangles R1 and R2. Return
28539 the intersection in *RESULT. Value is non-zero if RESULT is not
28540 empty. */
28541
28542 int
28543 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28544 {
28545 XRectangle *left, *right;
28546 XRectangle *upper, *lower;
28547 int intersection_p = 0;
28548
28549 /* Rearrange so that R1 is the left-most rectangle. */
28550 if (r1->x < r2->x)
28551 left = r1, right = r2;
28552 else
28553 left = r2, right = r1;
28554
28555 /* X0 of the intersection is right.x0, if this is inside R1,
28556 otherwise there is no intersection. */
28557 if (right->x <= left->x + left->width)
28558 {
28559 result->x = right->x;
28560
28561 /* The right end of the intersection is the minimum of
28562 the right ends of left and right. */
28563 result->width = (min (left->x + left->width, right->x + right->width)
28564 - result->x);
28565
28566 /* Same game for Y. */
28567 if (r1->y < r2->y)
28568 upper = r1, lower = r2;
28569 else
28570 upper = r2, lower = r1;
28571
28572 /* The upper end of the intersection is lower.y0, if this is inside
28573 of upper. Otherwise, there is no intersection. */
28574 if (lower->y <= upper->y + upper->height)
28575 {
28576 result->y = lower->y;
28577
28578 /* The lower end of the intersection is the minimum of the lower
28579 ends of upper and lower. */
28580 result->height = (min (lower->y + lower->height,
28581 upper->y + upper->height)
28582 - result->y);
28583 intersection_p = 1;
28584 }
28585 }
28586
28587 return intersection_p;
28588 }
28589
28590 #endif /* HAVE_WINDOW_SYSTEM */
28591
28592 \f
28593 /***********************************************************************
28594 Initialization
28595 ***********************************************************************/
28596
28597 void
28598 syms_of_xdisp (void)
28599 {
28600 Vwith_echo_area_save_vector = Qnil;
28601 staticpro (&Vwith_echo_area_save_vector);
28602
28603 Vmessage_stack = Qnil;
28604 staticpro (&Vmessage_stack);
28605
28606 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28607 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28608
28609 message_dolog_marker1 = Fmake_marker ();
28610 staticpro (&message_dolog_marker1);
28611 message_dolog_marker2 = Fmake_marker ();
28612 staticpro (&message_dolog_marker2);
28613 message_dolog_marker3 = Fmake_marker ();
28614 staticpro (&message_dolog_marker3);
28615
28616 #ifdef GLYPH_DEBUG
28617 defsubr (&Sdump_frame_glyph_matrix);
28618 defsubr (&Sdump_glyph_matrix);
28619 defsubr (&Sdump_glyph_row);
28620 defsubr (&Sdump_tool_bar_row);
28621 defsubr (&Strace_redisplay);
28622 defsubr (&Strace_to_stderr);
28623 #endif
28624 #ifdef HAVE_WINDOW_SYSTEM
28625 defsubr (&Stool_bar_lines_needed);
28626 defsubr (&Slookup_image_map);
28627 #endif
28628 defsubr (&Sformat_mode_line);
28629 defsubr (&Sinvisible_p);
28630 defsubr (&Scurrent_bidi_paragraph_direction);
28631
28632 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28633 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28634 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28635 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28636 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28637 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28638 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28639 DEFSYM (Qeval, "eval");
28640 DEFSYM (QCdata, ":data");
28641 DEFSYM (Qdisplay, "display");
28642 DEFSYM (Qspace_width, "space-width");
28643 DEFSYM (Qraise, "raise");
28644 DEFSYM (Qslice, "slice");
28645 DEFSYM (Qspace, "space");
28646 DEFSYM (Qmargin, "margin");
28647 DEFSYM (Qpointer, "pointer");
28648 DEFSYM (Qleft_margin, "left-margin");
28649 DEFSYM (Qright_margin, "right-margin");
28650 DEFSYM (Qcenter, "center");
28651 DEFSYM (Qline_height, "line-height");
28652 DEFSYM (QCalign_to, ":align-to");
28653 DEFSYM (QCrelative_width, ":relative-width");
28654 DEFSYM (QCrelative_height, ":relative-height");
28655 DEFSYM (QCeval, ":eval");
28656 DEFSYM (QCpropertize, ":propertize");
28657 DEFSYM (QCfile, ":file");
28658 DEFSYM (Qfontified, "fontified");
28659 DEFSYM (Qfontification_functions, "fontification-functions");
28660 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28661 DEFSYM (Qescape_glyph, "escape-glyph");
28662 DEFSYM (Qnobreak_space, "nobreak-space");
28663 DEFSYM (Qimage, "image");
28664 DEFSYM (Qtext, "text");
28665 DEFSYM (Qboth, "both");
28666 DEFSYM (Qboth_horiz, "both-horiz");
28667 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28668 DEFSYM (QCmap, ":map");
28669 DEFSYM (QCpointer, ":pointer");
28670 DEFSYM (Qrect, "rect");
28671 DEFSYM (Qcircle, "circle");
28672 DEFSYM (Qpoly, "poly");
28673 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28674 DEFSYM (Qgrow_only, "grow-only");
28675 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28676 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28677 DEFSYM (Qposition, "position");
28678 DEFSYM (Qbuffer_position, "buffer-position");
28679 DEFSYM (Qobject, "object");
28680 DEFSYM (Qbar, "bar");
28681 DEFSYM (Qhbar, "hbar");
28682 DEFSYM (Qbox, "box");
28683 DEFSYM (Qhollow, "hollow");
28684 DEFSYM (Qhand, "hand");
28685 DEFSYM (Qarrow, "arrow");
28686 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28687
28688 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28689 Fcons (intern_c_string ("void-variable"), Qnil)),
28690 Qnil);
28691 staticpro (&list_of_error);
28692
28693 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28694 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28695 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28696 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28697
28698 echo_buffer[0] = echo_buffer[1] = Qnil;
28699 staticpro (&echo_buffer[0]);
28700 staticpro (&echo_buffer[1]);
28701
28702 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28703 staticpro (&echo_area_buffer[0]);
28704 staticpro (&echo_area_buffer[1]);
28705
28706 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28707 staticpro (&Vmessages_buffer_name);
28708
28709 mode_line_proptrans_alist = Qnil;
28710 staticpro (&mode_line_proptrans_alist);
28711 mode_line_string_list = Qnil;
28712 staticpro (&mode_line_string_list);
28713 mode_line_string_face = Qnil;
28714 staticpro (&mode_line_string_face);
28715 mode_line_string_face_prop = Qnil;
28716 staticpro (&mode_line_string_face_prop);
28717 Vmode_line_unwind_vector = Qnil;
28718 staticpro (&Vmode_line_unwind_vector);
28719
28720 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28721
28722 help_echo_string = Qnil;
28723 staticpro (&help_echo_string);
28724 help_echo_object = Qnil;
28725 staticpro (&help_echo_object);
28726 help_echo_window = Qnil;
28727 staticpro (&help_echo_window);
28728 previous_help_echo_string = Qnil;
28729 staticpro (&previous_help_echo_string);
28730 help_echo_pos = -1;
28731
28732 DEFSYM (Qright_to_left, "right-to-left");
28733 DEFSYM (Qleft_to_right, "left-to-right");
28734
28735 #ifdef HAVE_WINDOW_SYSTEM
28736 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28737 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28738 For example, if a block cursor is over a tab, it will be drawn as
28739 wide as that tab on the display. */);
28740 x_stretch_cursor_p = 0;
28741 #endif
28742
28743 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28744 doc: /* Non-nil means highlight trailing whitespace.
28745 The face used for trailing whitespace is `trailing-whitespace'. */);
28746 Vshow_trailing_whitespace = Qnil;
28747
28748 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28749 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28750 If the value is t, Emacs highlights non-ASCII chars which have the
28751 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28752 or `escape-glyph' face respectively.
28753
28754 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28755 U+2011 (non-breaking hyphen) are affected.
28756
28757 Any other non-nil value means to display these characters as a escape
28758 glyph followed by an ordinary space or hyphen.
28759
28760 A value of nil means no special handling of these characters. */);
28761 Vnobreak_char_display = Qt;
28762
28763 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28764 doc: /* The pointer shape to show in void text areas.
28765 A value of nil means to show the text pointer. Other options are `arrow',
28766 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28767 Vvoid_text_area_pointer = Qarrow;
28768
28769 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28770 doc: /* Non-nil means don't actually do any redisplay.
28771 This is used for internal purposes. */);
28772 Vinhibit_redisplay = Qnil;
28773
28774 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28775 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28776 Vglobal_mode_string = Qnil;
28777
28778 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28779 doc: /* Marker for where to display an arrow on top of the buffer text.
28780 This must be the beginning of a line in order to work.
28781 See also `overlay-arrow-string'. */);
28782 Voverlay_arrow_position = Qnil;
28783
28784 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28785 doc: /* String to display as an arrow in non-window frames.
28786 See also `overlay-arrow-position'. */);
28787 Voverlay_arrow_string = build_pure_c_string ("=>");
28788
28789 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28790 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28791 The symbols on this list are examined during redisplay to determine
28792 where to display overlay arrows. */);
28793 Voverlay_arrow_variable_list
28794 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28795
28796 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28797 doc: /* The number of lines to try scrolling a window by when point moves out.
28798 If that fails to bring point back on frame, point is centered instead.
28799 If this is zero, point is always centered after it moves off frame.
28800 If you want scrolling to always be a line at a time, you should set
28801 `scroll-conservatively' to a large value rather than set this to 1. */);
28802
28803 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28804 doc: /* Scroll up to this many lines, to bring point back on screen.
28805 If point moves off-screen, redisplay will scroll by up to
28806 `scroll-conservatively' lines in order to bring point just barely
28807 onto the screen again. If that cannot be done, then redisplay
28808 recenters point as usual.
28809
28810 If the value is greater than 100, redisplay will never recenter point,
28811 but will always scroll just enough text to bring point into view, even
28812 if you move far away.
28813
28814 A value of zero means always recenter point if it moves off screen. */);
28815 scroll_conservatively = 0;
28816
28817 DEFVAR_INT ("scroll-margin", scroll_margin,
28818 doc: /* Number of lines of margin at the top and bottom of a window.
28819 Recenter the window whenever point gets within this many lines
28820 of the top or bottom of the window. */);
28821 scroll_margin = 0;
28822
28823 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28824 doc: /* Pixels per inch value for non-window system displays.
28825 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28826 Vdisplay_pixels_per_inch = make_float (72.0);
28827
28828 #ifdef GLYPH_DEBUG
28829 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28830 #endif
28831
28832 DEFVAR_LISP ("truncate-partial-width-windows",
28833 Vtruncate_partial_width_windows,
28834 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28835 For an integer value, truncate lines in each window narrower than the
28836 full frame width, provided the window width is less than that integer;
28837 otherwise, respect the value of `truncate-lines'.
28838
28839 For any other non-nil value, truncate lines in all windows that do
28840 not span the full frame width.
28841
28842 A value of nil means to respect the value of `truncate-lines'.
28843
28844 If `word-wrap' is enabled, you might want to reduce this. */);
28845 Vtruncate_partial_width_windows = make_number (50);
28846
28847 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28848 doc: /* Maximum buffer size for which line number should be displayed.
28849 If the buffer is bigger than this, the line number does not appear
28850 in the mode line. A value of nil means no limit. */);
28851 Vline_number_display_limit = Qnil;
28852
28853 DEFVAR_INT ("line-number-display-limit-width",
28854 line_number_display_limit_width,
28855 doc: /* Maximum line width (in characters) for line number display.
28856 If the average length of the lines near point is bigger than this, then the
28857 line number may be omitted from the mode line. */);
28858 line_number_display_limit_width = 200;
28859
28860 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28861 doc: /* Non-nil means highlight region even in nonselected windows. */);
28862 highlight_nonselected_windows = 0;
28863
28864 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28865 doc: /* Non-nil if more than one frame is visible on this display.
28866 Minibuffer-only frames don't count, but iconified frames do.
28867 This variable is not guaranteed to be accurate except while processing
28868 `frame-title-format' and `icon-title-format'. */);
28869
28870 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28871 doc: /* Template for displaying the title bar of visible frames.
28872 \(Assuming the window manager supports this feature.)
28873
28874 This variable has the same structure as `mode-line-format', except that
28875 the %c and %l constructs are ignored. It is used only on frames for
28876 which no explicit name has been set \(see `modify-frame-parameters'). */);
28877
28878 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28879 doc: /* Template for displaying the title bar of an iconified frame.
28880 \(Assuming the window manager supports this feature.)
28881 This variable has the same structure as `mode-line-format' (which see),
28882 and is used only on frames for which no explicit name has been set
28883 \(see `modify-frame-parameters'). */);
28884 Vicon_title_format
28885 = Vframe_title_format
28886 = listn (CONSTYPE_PURE, 3,
28887 intern_c_string ("multiple-frames"),
28888 build_pure_c_string ("%b"),
28889 listn (CONSTYPE_PURE, 4,
28890 empty_unibyte_string,
28891 intern_c_string ("invocation-name"),
28892 build_pure_c_string ("@"),
28893 intern_c_string ("system-name")));
28894
28895 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28896 doc: /* Maximum number of lines to keep in the message log buffer.
28897 If nil, disable message logging. If t, log messages but don't truncate
28898 the buffer when it becomes large. */);
28899 Vmessage_log_max = make_number (1000);
28900
28901 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28902 doc: /* Functions called before redisplay, if window sizes have changed.
28903 The value should be a list of functions that take one argument.
28904 Just before redisplay, for each frame, if any of its windows have changed
28905 size since the last redisplay, or have been split or deleted,
28906 all the functions in the list are called, with the frame as argument. */);
28907 Vwindow_size_change_functions = Qnil;
28908
28909 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28910 doc: /* List of functions to call before redisplaying a window with scrolling.
28911 Each function is called with two arguments, the window and its new
28912 display-start position. Note that these functions are also called by
28913 `set-window-buffer'. Also note that the value of `window-end' is not
28914 valid when these functions are called.
28915
28916 Warning: Do not use this feature to alter the way the window
28917 is scrolled. It is not designed for that, and such use probably won't
28918 work. */);
28919 Vwindow_scroll_functions = Qnil;
28920
28921 DEFVAR_LISP ("window-text-change-functions",
28922 Vwindow_text_change_functions,
28923 doc: /* Functions to call in redisplay when text in the window might change. */);
28924 Vwindow_text_change_functions = Qnil;
28925
28926 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28927 doc: /* Functions called when redisplay of a window reaches the end trigger.
28928 Each function is called with two arguments, the window and the end trigger value.
28929 See `set-window-redisplay-end-trigger'. */);
28930 Vredisplay_end_trigger_functions = Qnil;
28931
28932 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28933 doc: /* Non-nil means autoselect window with mouse pointer.
28934 If nil, do not autoselect windows.
28935 A positive number means delay autoselection by that many seconds: a
28936 window is autoselected only after the mouse has remained in that
28937 window for the duration of the delay.
28938 A negative number has a similar effect, but causes windows to be
28939 autoselected only after the mouse has stopped moving. \(Because of
28940 the way Emacs compares mouse events, you will occasionally wait twice
28941 that time before the window gets selected.\)
28942 Any other value means to autoselect window instantaneously when the
28943 mouse pointer enters it.
28944
28945 Autoselection selects the minibuffer only if it is active, and never
28946 unselects the minibuffer if it is active.
28947
28948 When customizing this variable make sure that the actual value of
28949 `focus-follows-mouse' matches the behavior of your window manager. */);
28950 Vmouse_autoselect_window = Qnil;
28951
28952 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28953 doc: /* Non-nil means automatically resize tool-bars.
28954 This dynamically changes the tool-bar's height to the minimum height
28955 that is needed to make all tool-bar items visible.
28956 If value is `grow-only', the tool-bar's height is only increased
28957 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28958 Vauto_resize_tool_bars = Qt;
28959
28960 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28961 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28962 auto_raise_tool_bar_buttons_p = 1;
28963
28964 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28965 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28966 make_cursor_line_fully_visible_p = 1;
28967
28968 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28969 doc: /* Border below tool-bar in pixels.
28970 If an integer, use it as the height of the border.
28971 If it is one of `internal-border-width' or `border-width', use the
28972 value of the corresponding frame parameter.
28973 Otherwise, no border is added below the tool-bar. */);
28974 Vtool_bar_border = Qinternal_border_width;
28975
28976 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28977 doc: /* Margin around tool-bar buttons in pixels.
28978 If an integer, use that for both horizontal and vertical margins.
28979 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28980 HORZ specifying the horizontal margin, and VERT specifying the
28981 vertical margin. */);
28982 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28983
28984 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28985 doc: /* Relief thickness of tool-bar buttons. */);
28986 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28987
28988 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28989 doc: /* Tool bar style to use.
28990 It can be one of
28991 image - show images only
28992 text - show text only
28993 both - show both, text below image
28994 both-horiz - show text to the right of the image
28995 text-image-horiz - show text to the left of the image
28996 any other - use system default or image if no system default.
28997
28998 This variable only affects the GTK+ toolkit version of Emacs. */);
28999 Vtool_bar_style = Qnil;
29000
29001 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29002 doc: /* Maximum number of characters a label can have to be shown.
29003 The tool bar style must also show labels for this to have any effect, see
29004 `tool-bar-style'. */);
29005 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29006
29007 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29008 doc: /* List of functions to call to fontify regions of text.
29009 Each function is called with one argument POS. Functions must
29010 fontify a region starting at POS in the current buffer, and give
29011 fontified regions the property `fontified'. */);
29012 Vfontification_functions = Qnil;
29013 Fmake_variable_buffer_local (Qfontification_functions);
29014
29015 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29016 unibyte_display_via_language_environment,
29017 doc: /* Non-nil means display unibyte text according to language environment.
29018 Specifically, this means that raw bytes in the range 160-255 decimal
29019 are displayed by converting them to the equivalent multibyte characters
29020 according to the current language environment. As a result, they are
29021 displayed according to the current fontset.
29022
29023 Note that this variable affects only how these bytes are displayed,
29024 but does not change the fact they are interpreted as raw bytes. */);
29025 unibyte_display_via_language_environment = 0;
29026
29027 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29028 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29029 If a float, it specifies a fraction of the mini-window frame's height.
29030 If an integer, it specifies a number of lines. */);
29031 Vmax_mini_window_height = make_float (0.25);
29032
29033 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29034 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29035 A value of nil means don't automatically resize mini-windows.
29036 A value of t means resize them to fit the text displayed in them.
29037 A value of `grow-only', the default, means let mini-windows grow only;
29038 they return to their normal size when the minibuffer is closed, or the
29039 echo area becomes empty. */);
29040 Vresize_mini_windows = Qgrow_only;
29041
29042 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29043 doc: /* Alist specifying how to blink the cursor off.
29044 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29045 `cursor-type' frame-parameter or variable equals ON-STATE,
29046 comparing using `equal', Emacs uses OFF-STATE to specify
29047 how to blink it off. ON-STATE and OFF-STATE are values for
29048 the `cursor-type' frame parameter.
29049
29050 If a frame's ON-STATE has no entry in this list,
29051 the frame's other specifications determine how to blink the cursor off. */);
29052 Vblink_cursor_alist = Qnil;
29053
29054 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29055 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29056 If non-nil, windows are automatically scrolled horizontally to make
29057 point visible. */);
29058 automatic_hscrolling_p = 1;
29059 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29060
29061 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29062 doc: /* How many columns away from the window edge point is allowed to get
29063 before automatic hscrolling will horizontally scroll the window. */);
29064 hscroll_margin = 5;
29065
29066 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29067 doc: /* How many columns to scroll the window when point gets too close to the edge.
29068 When point is less than `hscroll-margin' columns from the window
29069 edge, automatic hscrolling will scroll the window by the amount of columns
29070 determined by this variable. If its value is a positive integer, scroll that
29071 many columns. If it's a positive floating-point number, it specifies the
29072 fraction of the window's width to scroll. If it's nil or zero, point will be
29073 centered horizontally after the scroll. Any other value, including negative
29074 numbers, are treated as if the value were zero.
29075
29076 Automatic hscrolling always moves point outside the scroll margin, so if
29077 point was more than scroll step columns inside the margin, the window will
29078 scroll more than the value given by the scroll step.
29079
29080 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29081 and `scroll-right' overrides this variable's effect. */);
29082 Vhscroll_step = make_number (0);
29083
29084 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29085 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29086 Bind this around calls to `message' to let it take effect. */);
29087 message_truncate_lines = 0;
29088
29089 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29090 doc: /* Normal hook run to update the menu bar definitions.
29091 Redisplay runs this hook before it redisplays the menu bar.
29092 This is used to update submenus such as Buffers,
29093 whose contents depend on various data. */);
29094 Vmenu_bar_update_hook = Qnil;
29095
29096 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29097 doc: /* Frame for which we are updating a menu.
29098 The enable predicate for a menu binding should check this variable. */);
29099 Vmenu_updating_frame = Qnil;
29100
29101 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29102 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29103 inhibit_menubar_update = 0;
29104
29105 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29106 doc: /* Prefix prepended to all continuation lines at display time.
29107 The value may be a string, an image, or a stretch-glyph; it is
29108 interpreted in the same way as the value of a `display' text property.
29109
29110 This variable is overridden by any `wrap-prefix' text or overlay
29111 property.
29112
29113 To add a prefix to non-continuation lines, use `line-prefix'. */);
29114 Vwrap_prefix = Qnil;
29115 DEFSYM (Qwrap_prefix, "wrap-prefix");
29116 Fmake_variable_buffer_local (Qwrap_prefix);
29117
29118 DEFVAR_LISP ("line-prefix", Vline_prefix,
29119 doc: /* Prefix prepended to all non-continuation lines at display time.
29120 The value may be a string, an image, or a stretch-glyph; it is
29121 interpreted in the same way as the value of a `display' text property.
29122
29123 This variable is overridden by any `line-prefix' text or overlay
29124 property.
29125
29126 To add a prefix to continuation lines, use `wrap-prefix'. */);
29127 Vline_prefix = Qnil;
29128 DEFSYM (Qline_prefix, "line-prefix");
29129 Fmake_variable_buffer_local (Qline_prefix);
29130
29131 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29132 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29133 inhibit_eval_during_redisplay = 0;
29134
29135 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29136 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29137 inhibit_free_realized_faces = 0;
29138
29139 #ifdef GLYPH_DEBUG
29140 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29141 doc: /* Inhibit try_window_id display optimization. */);
29142 inhibit_try_window_id = 0;
29143
29144 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29145 doc: /* Inhibit try_window_reusing display optimization. */);
29146 inhibit_try_window_reusing = 0;
29147
29148 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29149 doc: /* Inhibit try_cursor_movement display optimization. */);
29150 inhibit_try_cursor_movement = 0;
29151 #endif /* GLYPH_DEBUG */
29152
29153 DEFVAR_INT ("overline-margin", overline_margin,
29154 doc: /* Space between overline and text, in pixels.
29155 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29156 margin to the character height. */);
29157 overline_margin = 2;
29158
29159 DEFVAR_INT ("underline-minimum-offset",
29160 underline_minimum_offset,
29161 doc: /* Minimum distance between baseline and underline.
29162 This can improve legibility of underlined text at small font sizes,
29163 particularly when using variable `x-use-underline-position-properties'
29164 with fonts that specify an UNDERLINE_POSITION relatively close to the
29165 baseline. The default value is 1. */);
29166 underline_minimum_offset = 1;
29167
29168 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29169 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29170 This feature only works when on a window system that can change
29171 cursor shapes. */);
29172 display_hourglass_p = 1;
29173
29174 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29175 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29176 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29177
29178 hourglass_atimer = NULL;
29179 hourglass_shown_p = 0;
29180
29181 DEFSYM (Qglyphless_char, "glyphless-char");
29182 DEFSYM (Qhex_code, "hex-code");
29183 DEFSYM (Qempty_box, "empty-box");
29184 DEFSYM (Qthin_space, "thin-space");
29185 DEFSYM (Qzero_width, "zero-width");
29186
29187 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29188 /* Intern this now in case it isn't already done.
29189 Setting this variable twice is harmless.
29190 But don't staticpro it here--that is done in alloc.c. */
29191 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29192 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29193
29194 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29195 doc: /* Char-table defining glyphless characters.
29196 Each element, if non-nil, should be one of the following:
29197 an ASCII acronym string: display this string in a box
29198 `hex-code': display the hexadecimal code of a character in a box
29199 `empty-box': display as an empty box
29200 `thin-space': display as 1-pixel width space
29201 `zero-width': don't display
29202 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29203 display method for graphical terminals and text terminals respectively.
29204 GRAPHICAL and TEXT should each have one of the values listed above.
29205
29206 The char-table has one extra slot to control the display of a character for
29207 which no font is found. This slot only takes effect on graphical terminals.
29208 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29209 `thin-space'. The default is `empty-box'. */);
29210 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29211 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29212 Qempty_box);
29213
29214 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29215 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29216 Vdebug_on_message = Qnil;
29217 }
29218
29219
29220 /* Initialize this module when Emacs starts. */
29221
29222 void
29223 init_xdisp (void)
29224 {
29225 current_header_line_height = current_mode_line_height = -1;
29226
29227 CHARPOS (this_line_start_pos) = 0;
29228
29229 if (!noninteractive)
29230 {
29231 struct window *m = XWINDOW (minibuf_window);
29232 Lisp_Object frame = m->frame;
29233 struct frame *f = XFRAME (frame);
29234 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29235 struct window *r = XWINDOW (root);
29236 int i;
29237
29238 echo_area_window = minibuf_window;
29239
29240 r->top_line = FRAME_TOP_MARGIN (f);
29241 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29242 r->total_cols = FRAME_COLS (f);
29243
29244 m->top_line = FRAME_LINES (f) - 1;
29245 m->total_lines = 1;
29246 m->total_cols = FRAME_COLS (f);
29247
29248 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29249 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29250 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29251
29252 /* The default ellipsis glyphs `...'. */
29253 for (i = 0; i < 3; ++i)
29254 default_invis_vector[i] = make_number ('.');
29255 }
29256
29257 {
29258 /* Allocate the buffer for frame titles.
29259 Also used for `format-mode-line'. */
29260 int size = 100;
29261 mode_line_noprop_buf = xmalloc (size);
29262 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29263 mode_line_noprop_ptr = mode_line_noprop_buf;
29264 mode_line_target = MODE_LINE_DISPLAY;
29265 }
29266
29267 help_echo_showing_p = 0;
29268 }
29269
29270 /* Platform-independent portion of hourglass implementation. */
29271
29272 /* Cancel a currently active hourglass timer, and start a new one. */
29273 void
29274 start_hourglass (void)
29275 {
29276 #if defined (HAVE_WINDOW_SYSTEM)
29277 EMACS_TIME delay;
29278
29279 cancel_hourglass ();
29280
29281 if (INTEGERP (Vhourglass_delay)
29282 && XINT (Vhourglass_delay) > 0)
29283 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29284 TYPE_MAXIMUM (time_t)),
29285 0);
29286 else if (FLOATP (Vhourglass_delay)
29287 && XFLOAT_DATA (Vhourglass_delay) > 0)
29288 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29289 else
29290 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29291
29292 #ifdef HAVE_NTGUI
29293 {
29294 extern void w32_note_current_window (void);
29295 w32_note_current_window ();
29296 }
29297 #endif /* HAVE_NTGUI */
29298
29299 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29300 show_hourglass, NULL);
29301 #endif
29302 }
29303
29304
29305 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29306 shown. */
29307 void
29308 cancel_hourglass (void)
29309 {
29310 #if defined (HAVE_WINDOW_SYSTEM)
29311 if (hourglass_atimer)
29312 {
29313 cancel_atimer (hourglass_atimer);
29314 hourglass_atimer = NULL;
29315 }
29316
29317 if (hourglass_shown_p)
29318 hide_hourglass ();
29319 #endif
29320 }