]> code.delx.au - gnu-emacs/blob - src/xdisp.c
auto upstream
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
315
316 #include "font.h"
317 #ifdef HAVE_XWIDGETS
318 #include "xwidget.h"
319 #endif
320 #ifndef FRAME_X_OUTPUT
321 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
322 #endif
323
324 #define INFINITY 10000000
325
326 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
327 Lisp_Object Qwindow_scroll_functions;
328 static Lisp_Object Qwindow_text_change_functions;
329 static Lisp_Object Qredisplay_end_trigger_functions;
330 Lisp_Object Qinhibit_point_motion_hooks;
331 static Lisp_Object QCeval, QCpropertize;
332 Lisp_Object QCfile, QCdata;
333 static Lisp_Object Qfontified;
334 static Lisp_Object Qgrow_only;
335 static Lisp_Object Qinhibit_eval_during_redisplay;
336 static Lisp_Object Qbuffer_position, Qposition, Qobject;
337 static Lisp_Object Qright_to_left, Qleft_to_right;
338
339 /* Cursor shapes. */
340 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
341
342 /* Pointer shapes. */
343 static Lisp_Object Qarrow, Qhand;
344 Lisp_Object Qtext;
345
346 /* Holds the list (error). */
347 static Lisp_Object list_of_error;
348
349 static Lisp_Object Qfontification_functions;
350
351 static Lisp_Object Qwrap_prefix;
352 static Lisp_Object Qline_prefix;
353 static Lisp_Object Qredisplay_internal;
354
355 /* Non-nil means don't actually do any redisplay. */
356
357 Lisp_Object Qinhibit_redisplay;
358
359 /* Names of text properties relevant for redisplay. */
360
361 Lisp_Object Qdisplay;
362
363 Lisp_Object Qspace, QCalign_to;
364 static Lisp_Object QCrelative_width, QCrelative_height;
365 Lisp_Object Qleft_margin, Qright_margin;
366 static Lisp_Object Qspace_width, Qraise;
367 static Lisp_Object Qslice;
368 Lisp_Object Qcenter;
369 static Lisp_Object Qmargin, Qpointer;
370 static Lisp_Object Qline_height;
371
372 #ifdef HAVE_WINDOW_SYSTEM
373
374 /* Test if overflow newline into fringe. Called with iterator IT
375 at or past right window margin, and with IT->current_x set. */
376
377 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
378 (!NILP (Voverflow_newline_into_fringe) \
379 && FRAME_WINDOW_P ((IT)->f) \
380 && ((IT)->bidi_it.paragraph_dir == R2L \
381 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
382 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
383 && (IT)->current_x == (IT)->last_visible_x \
384 && (IT)->line_wrap != WORD_WRAP)
385
386 #else /* !HAVE_WINDOW_SYSTEM */
387 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
388 #endif /* HAVE_WINDOW_SYSTEM */
389
390 /* Test if the display element loaded in IT, or the underlying buffer
391 or string character, is a space or a TAB character. This is used
392 to determine where word wrapping can occur. */
393
394 #define IT_DISPLAYING_WHITESPACE(it) \
395 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
396 || ((STRINGP (it->string) \
397 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
398 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
399 || (it->s \
400 && (it->s[IT_BYTEPOS (*it)] == ' ' \
401 || it->s[IT_BYTEPOS (*it)] == '\t')) \
402 || (IT_BYTEPOS (*it) < ZV_BYTE \
403 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
404 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
405
406 /* Name of the face used to highlight trailing whitespace. */
407
408 static Lisp_Object Qtrailing_whitespace;
409
410 /* Name and number of the face used to highlight escape glyphs. */
411
412 static Lisp_Object Qescape_glyph;
413
414 /* Name and number of the face used to highlight non-breaking spaces. */
415
416 static Lisp_Object Qnobreak_space;
417
418 /* The symbol `image' which is the car of the lists used to represent
419 images in Lisp. Also a tool bar style. */
420
421 Lisp_Object Qimage;
422
423 /* The image map types. */
424 Lisp_Object QCmap;
425 static Lisp_Object QCpointer;
426 static Lisp_Object Qrect, Qcircle, Qpoly;
427
428 /* Tool bar styles */
429 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
430
431 /* Non-zero means print newline to stdout before next mini-buffer
432 message. */
433
434 int noninteractive_need_newline;
435
436 /* Non-zero means print newline to message log before next message. */
437
438 static int message_log_need_newline;
439
440 /* Three markers that message_dolog uses.
441 It could allocate them itself, but that causes trouble
442 in handling memory-full errors. */
443 static Lisp_Object message_dolog_marker1;
444 static Lisp_Object message_dolog_marker2;
445 static Lisp_Object message_dolog_marker3;
446 \f
447 /* The buffer position of the first character appearing entirely or
448 partially on the line of the selected window which contains the
449 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
450 redisplay optimization in redisplay_internal. */
451
452 static struct text_pos this_line_start_pos;
453
454 /* Number of characters past the end of the line above, including the
455 terminating newline. */
456
457 static struct text_pos this_line_end_pos;
458
459 /* The vertical positions and the height of this line. */
460
461 static int this_line_vpos;
462 static int this_line_y;
463 static int this_line_pixel_height;
464
465 /* X position at which this display line starts. Usually zero;
466 negative if first character is partially visible. */
467
468 static int this_line_start_x;
469
470 /* The smallest character position seen by move_it_* functions as they
471 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
472 hscrolled lines, see display_line. */
473
474 static struct text_pos this_line_min_pos;
475
476 /* Buffer that this_line_.* variables are referring to. */
477
478 static struct buffer *this_line_buffer;
479
480
481 /* Values of those variables at last redisplay are stored as
482 properties on `overlay-arrow-position' symbol. However, if
483 Voverlay_arrow_position is a marker, last-arrow-position is its
484 numerical position. */
485
486 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
487
488 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
489 properties on a symbol in overlay-arrow-variable-list. */
490
491 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
492
493 Lisp_Object Qmenu_bar_update_hook;
494
495 /* Nonzero if an overlay arrow has been displayed in this window. */
496
497 static int overlay_arrow_seen;
498
499 /* Vector containing glyphs for an ellipsis `...'. */
500
501 static Lisp_Object default_invis_vector[3];
502
503 /* This is the window where the echo area message was displayed. It
504 is always a mini-buffer window, but it may not be the same window
505 currently active as a mini-buffer. */
506
507 Lisp_Object echo_area_window;
508
509 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
510 pushes the current message and the value of
511 message_enable_multibyte on the stack, the function restore_message
512 pops the stack and displays MESSAGE again. */
513
514 static Lisp_Object Vmessage_stack;
515
516 /* Nonzero means multibyte characters were enabled when the echo area
517 message was specified. */
518
519 static int message_enable_multibyte;
520
521 /* Nonzero if we should redraw the mode lines on the next redisplay. */
522
523 int update_mode_lines;
524
525 /* Nonzero if window sizes or contents have changed since last
526 redisplay that finished. */
527
528 int windows_or_buffers_changed;
529
530 /* Nonzero means a frame's cursor type has been changed. */
531
532 int cursor_type_changed;
533
534 /* Nonzero after display_mode_line if %l was used and it displayed a
535 line number. */
536
537 static int line_number_displayed;
538
539 /* The name of the *Messages* buffer, a string. */
540
541 static Lisp_Object Vmessages_buffer_name;
542
543 /* Current, index 0, and last displayed echo area message. Either
544 buffers from echo_buffers, or nil to indicate no message. */
545
546 Lisp_Object echo_area_buffer[2];
547
548 /* The buffers referenced from echo_area_buffer. */
549
550 static Lisp_Object echo_buffer[2];
551
552 /* A vector saved used in with_area_buffer to reduce consing. */
553
554 static Lisp_Object Vwith_echo_area_save_vector;
555
556 /* Non-zero means display_echo_area should display the last echo area
557 message again. Set by redisplay_preserve_echo_area. */
558
559 static int display_last_displayed_message_p;
560
561 /* Nonzero if echo area is being used by print; zero if being used by
562 message. */
563
564 static int message_buf_print;
565
566 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
567
568 static Lisp_Object Qinhibit_menubar_update;
569 static Lisp_Object Qmessage_truncate_lines;
570
571 /* Set to 1 in clear_message to make redisplay_internal aware
572 of an emptied echo area. */
573
574 static int message_cleared_p;
575
576 /* A scratch glyph row with contents used for generating truncation
577 glyphs. Also used in direct_output_for_insert. */
578
579 #define MAX_SCRATCH_GLYPHS 100
580 static struct glyph_row scratch_glyph_row;
581 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
582
583 /* Ascent and height of the last line processed by move_it_to. */
584
585 static int last_max_ascent, last_height;
586
587 /* Non-zero if there's a help-echo in the echo area. */
588
589 int help_echo_showing_p;
590
591 /* If >= 0, computed, exact values of mode-line and header-line height
592 to use in the macros CURRENT_MODE_LINE_HEIGHT and
593 CURRENT_HEADER_LINE_HEIGHT. */
594
595 int current_mode_line_height, current_header_line_height;
596
597 /* The maximum distance to look ahead for text properties. Values
598 that are too small let us call compute_char_face and similar
599 functions too often which is expensive. Values that are too large
600 let us call compute_char_face and alike too often because we
601 might not be interested in text properties that far away. */
602
603 #define TEXT_PROP_DISTANCE_LIMIT 100
604
605 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
606 iterator state and later restore it. This is needed because the
607 bidi iterator on bidi.c keeps a stacked cache of its states, which
608 is really a singleton. When we use scratch iterator objects to
609 move around the buffer, we can cause the bidi cache to be pushed or
610 popped, and therefore we need to restore the cache state when we
611 return to the original iterator. */
612 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
613 do { \
614 if (CACHE) \
615 bidi_unshelve_cache (CACHE, 1); \
616 ITCOPY = ITORIG; \
617 CACHE = bidi_shelve_cache (); \
618 } while (0)
619
620 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
621 do { \
622 if (pITORIG != pITCOPY) \
623 *(pITORIG) = *(pITCOPY); \
624 bidi_unshelve_cache (CACHE, 0); \
625 CACHE = NULL; \
626 } while (0)
627
628 #ifdef GLYPH_DEBUG
629
630 /* Non-zero means print traces of redisplay if compiled with
631 GLYPH_DEBUG defined. */
632
633 int trace_redisplay_p;
634
635 #endif /* GLYPH_DEBUG */
636
637 #ifdef DEBUG_TRACE_MOVE
638 /* Non-zero means trace with TRACE_MOVE to stderr. */
639 int trace_move;
640
641 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
642 #else
643 #define TRACE_MOVE(x) (void) 0
644 #endif
645
646 static Lisp_Object Qauto_hscroll_mode;
647
648 /* Buffer being redisplayed -- for redisplay_window_error. */
649
650 static struct buffer *displayed_buffer;
651
652 /* Value returned from text property handlers (see below). */
653
654 enum prop_handled
655 {
656 HANDLED_NORMALLY,
657 HANDLED_RECOMPUTE_PROPS,
658 HANDLED_OVERLAY_STRING_CONSUMED,
659 HANDLED_RETURN
660 };
661
662 /* A description of text properties that redisplay is interested
663 in. */
664
665 struct props
666 {
667 /* The name of the property. */
668 Lisp_Object *name;
669
670 /* A unique index for the property. */
671 enum prop_idx idx;
672
673 /* A handler function called to set up iterator IT from the property
674 at IT's current position. Value is used to steer handle_stop. */
675 enum prop_handled (*handler) (struct it *it);
676 };
677
678 static enum prop_handled handle_face_prop (struct it *);
679 static enum prop_handled handle_invisible_prop (struct it *);
680 static enum prop_handled handle_display_prop (struct it *);
681 static enum prop_handled handle_composition_prop (struct it *);
682 static enum prop_handled handle_overlay_change (struct it *);
683 static enum prop_handled handle_fontified_prop (struct it *);
684
685 /* Properties handled by iterators. */
686
687 static struct props it_props[] =
688 {
689 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
690 /* Handle `face' before `display' because some sub-properties of
691 `display' need to know the face. */
692 {&Qface, FACE_PROP_IDX, handle_face_prop},
693 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
694 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
695 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
696 {NULL, 0, NULL}
697 };
698
699 /* Value is the position described by X. If X is a marker, value is
700 the marker_position of X. Otherwise, value is X. */
701
702 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
703
704 /* Enumeration returned by some move_it_.* functions internally. */
705
706 enum move_it_result
707 {
708 /* Not used. Undefined value. */
709 MOVE_UNDEFINED,
710
711 /* Move ended at the requested buffer position or ZV. */
712 MOVE_POS_MATCH_OR_ZV,
713
714 /* Move ended at the requested X pixel position. */
715 MOVE_X_REACHED,
716
717 /* Move within a line ended at the end of a line that must be
718 continued. */
719 MOVE_LINE_CONTINUED,
720
721 /* Move within a line ended at the end of a line that would
722 be displayed truncated. */
723 MOVE_LINE_TRUNCATED,
724
725 /* Move within a line ended at a line end. */
726 MOVE_NEWLINE_OR_CR
727 };
728
729 /* This counter is used to clear the face cache every once in a while
730 in redisplay_internal. It is incremented for each redisplay.
731 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
732 cleared. */
733
734 #define CLEAR_FACE_CACHE_COUNT 500
735 static int clear_face_cache_count;
736
737 /* Similarly for the image cache. */
738
739 #ifdef HAVE_WINDOW_SYSTEM
740 #define CLEAR_IMAGE_CACHE_COUNT 101
741 static int clear_image_cache_count;
742
743 /* Null glyph slice */
744 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
745 #endif
746
747 /* True while redisplay_internal is in progress. */
748
749 bool redisplaying_p;
750
751 static Lisp_Object Qinhibit_free_realized_faces;
752 static Lisp_Object Qmode_line_default_help_echo;
753
754 /* If a string, XTread_socket generates an event to display that string.
755 (The display is done in read_char.) */
756
757 Lisp_Object help_echo_string;
758 Lisp_Object help_echo_window;
759 Lisp_Object help_echo_object;
760 ptrdiff_t help_echo_pos;
761
762 /* Temporary variable for XTread_socket. */
763
764 Lisp_Object previous_help_echo_string;
765
766 /* Platform-independent portion of hourglass implementation. */
767
768 /* Non-zero means an hourglass cursor is currently shown. */
769 int hourglass_shown_p;
770
771 /* If non-null, an asynchronous timer that, when it expires, displays
772 an hourglass cursor on all frames. */
773 struct atimer *hourglass_atimer;
774
775 /* Name of the face used to display glyphless characters. */
776 Lisp_Object Qglyphless_char;
777
778 /* Symbol for the purpose of Vglyphless_char_display. */
779 static Lisp_Object Qglyphless_char_display;
780
781 /* Method symbols for Vglyphless_char_display. */
782 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
783
784 /* Default pixel width of `thin-space' display method. */
785 #define THIN_SPACE_WIDTH 1
786
787 /* Default number of seconds to wait before displaying an hourglass
788 cursor. */
789 #define DEFAULT_HOURGLASS_DELAY 1
790
791 \f
792 /* Function prototypes. */
793
794 static void setup_for_ellipsis (struct it *, int);
795 static void set_iterator_to_next (struct it *, int);
796 static void mark_window_display_accurate_1 (struct window *, int);
797 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
798 static int display_prop_string_p (Lisp_Object, Lisp_Object);
799 static int cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, int);
801 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
802
803 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
804
805 static void handle_line_prefix (struct it *);
806
807 static void pint2str (char *, int, ptrdiff_t);
808 static void pint2hrstr (char *, int, ptrdiff_t);
809 static struct text_pos run_window_scroll_functions (Lisp_Object,
810 struct text_pos);
811 static void reconsider_clip_changes (struct window *, struct buffer *);
812 static int text_outside_line_unchanged_p (struct window *,
813 ptrdiff_t, ptrdiff_t);
814 static void store_mode_line_noprop_char (char);
815 static int store_mode_line_noprop (const char *, int, int);
816 static void handle_stop (struct it *);
817 static void handle_stop_backwards (struct it *, ptrdiff_t);
818 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
819 static void ensure_echo_area_buffers (void);
820 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
821 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
822 static int with_echo_area_buffer (struct window *, int,
823 int (*) (ptrdiff_t, Lisp_Object),
824 ptrdiff_t, Lisp_Object);
825 static void clear_garbaged_frames (void);
826 static int current_message_1 (ptrdiff_t, Lisp_Object);
827 static void pop_message (void);
828 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
829 static void set_message (Lisp_Object);
830 static int set_message_1 (ptrdiff_t, Lisp_Object);
831 static int display_echo_area (struct window *);
832 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
833 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
834 static Lisp_Object unwind_redisplay (Lisp_Object);
835 static int string_char_and_length (const unsigned char *, int *);
836 static struct text_pos display_prop_end (struct it *, Lisp_Object,
837 struct text_pos);
838 static int compute_window_start_on_continuation_line (struct window *);
839 static void insert_left_trunc_glyphs (struct it *);
840 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
841 Lisp_Object);
842 static void extend_face_to_end_of_line (struct it *);
843 static int append_space_for_newline (struct it *, int);
844 static int cursor_row_fully_visible_p (struct window *, int, int);
845 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
846 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
847 static int trailing_whitespace_p (ptrdiff_t);
848 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
849 static void push_it (struct it *, struct text_pos *);
850 static void iterate_out_of_display_property (struct it *);
851 static void pop_it (struct it *);
852 static void sync_frame_with_window_matrix_rows (struct window *);
853 static void redisplay_internal (void);
854 static int echo_area_display (int);
855 static void redisplay_windows (Lisp_Object);
856 static void redisplay_window (Lisp_Object, int);
857 static Lisp_Object redisplay_window_error (Lisp_Object);
858 static Lisp_Object redisplay_window_0 (Lisp_Object);
859 static Lisp_Object redisplay_window_1 (Lisp_Object);
860 static int set_cursor_from_row (struct window *, struct glyph_row *,
861 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
862 int, int);
863 static int update_menu_bar (struct frame *, int, int);
864 static int try_window_reusing_current_matrix (struct window *);
865 static int try_window_id (struct window *);
866 static int display_line (struct it *);
867 static int display_mode_lines (struct window *);
868 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
869 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
870 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
871 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
872 static void display_menu_bar (struct window *);
873 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
874 ptrdiff_t *);
875 static int display_string (const char *, Lisp_Object, Lisp_Object,
876 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
877 static void compute_line_metrics (struct it *);
878 static void run_redisplay_end_trigger_hook (struct it *);
879 static int get_overlay_strings (struct it *, ptrdiff_t);
880 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
881 static void next_overlay_string (struct it *);
882 static void reseat (struct it *, struct text_pos, int);
883 static void reseat_1 (struct it *, struct text_pos, int);
884 static void back_to_previous_visible_line_start (struct it *);
885 void reseat_at_previous_visible_line_start (struct it *);
886 static void reseat_at_next_visible_line_start (struct it *, int);
887 static int next_element_from_ellipsis (struct it *);
888 static int next_element_from_display_vector (struct it *);
889 static int next_element_from_string (struct it *);
890 static int next_element_from_c_string (struct it *);
891 static int next_element_from_buffer (struct it *);
892 static int next_element_from_composition (struct it *);
893 static int next_element_from_image (struct it *);
894 #ifdef HAVE_XWIDGETS
895 static int next_element_from_xwidget(struct it *);
896 #endif
897 static int next_element_from_stretch (struct it *);
898 static void load_overlay_strings (struct it *, ptrdiff_t);
899 static int init_from_display_pos (struct it *, struct window *,
900 struct display_pos *);
901 static void reseat_to_string (struct it *, const char *,
902 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
903 static int get_next_display_element (struct it *);
904 static enum move_it_result
905 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
906 enum move_operation_enum);
907 void move_it_vertically_backward (struct it *, int);
908 static void get_visually_first_element (struct it *);
909 static void init_to_row_start (struct it *, struct window *,
910 struct glyph_row *);
911 static int init_to_row_end (struct it *, struct window *,
912 struct glyph_row *);
913 static void back_to_previous_line_start (struct it *);
914 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
915 static struct text_pos string_pos_nchars_ahead (struct text_pos,
916 Lisp_Object, ptrdiff_t);
917 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
918 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
919 static ptrdiff_t number_of_chars (const char *, bool);
920 static void compute_stop_pos (struct it *);
921 static void compute_string_pos (struct text_pos *, struct text_pos,
922 Lisp_Object);
923 static int face_before_or_after_it_pos (struct it *, int);
924 static ptrdiff_t next_overlay_change (ptrdiff_t);
925 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
926 Lisp_Object, struct text_pos *, ptrdiff_t, int);
927 static int handle_single_display_spec (struct it *, Lisp_Object,
928 Lisp_Object, Lisp_Object,
929 struct text_pos *, ptrdiff_t, int, int);
930 static int underlying_face_id (struct it *);
931 static int in_ellipses_for_invisible_text_p (struct display_pos *,
932 struct window *);
933
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
936
937 #ifdef HAVE_WINDOW_SYSTEM
938
939 static void x_consider_frame_title (Lisp_Object);
940 static int tool_bar_lines_needed (struct frame *, int *);
941 static void update_tool_bar (struct frame *, int);
942 static void build_desired_tool_bar_string (struct frame *f);
943 static int redisplay_tool_bar (struct frame *);
944 static void display_tool_bar_line (struct it *, int);
945 static void notice_overwritten_cursor (struct window *,
946 enum glyph_row_area,
947 int, int, int, int);
948 static void append_stretch_glyph (struct it *, Lisp_Object,
949 int, int, int);
950
951
952 #endif /* HAVE_WINDOW_SYSTEM */
953
954 static void produce_special_glyphs (struct it *, enum display_element_type);
955 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
956 static int coords_in_mouse_face_p (struct window *, int, int);
957
958
959 \f
960 /***********************************************************************
961 Window display dimensions
962 ***********************************************************************/
963
964 /* Return the bottom boundary y-position for text lines in window W.
965 This is the first y position at which a line cannot start.
966 It is relative to the top of the window.
967
968 This is the height of W minus the height of a mode line, if any. */
969
970 int
971 window_text_bottom_y (struct window *w)
972 {
973 int height = WINDOW_TOTAL_HEIGHT (w);
974
975 if (WINDOW_WANTS_MODELINE_P (w))
976 height -= CURRENT_MODE_LINE_HEIGHT (w);
977 return height;
978 }
979
980 /* Return the pixel width of display area AREA of window W. AREA < 0
981 means return the total width of W, not including fringes to
982 the left and right of the window. */
983
984 int
985 window_box_width (struct window *w, int area)
986 {
987 int cols = XFASTINT (w->total_cols);
988 int pixels = 0;
989
990 if (!w->pseudo_window_p)
991 {
992 cols -= WINDOW_SCROLL_BAR_COLS (w);
993
994 if (area == TEXT_AREA)
995 {
996 if (INTEGERP (w->left_margin_cols))
997 cols -= XFASTINT (w->left_margin_cols);
998 if (INTEGERP (w->right_margin_cols))
999 cols -= XFASTINT (w->right_margin_cols);
1000 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1001 }
1002 else if (area == LEFT_MARGIN_AREA)
1003 {
1004 cols = (INTEGERP (w->left_margin_cols)
1005 ? XFASTINT (w->left_margin_cols) : 0);
1006 pixels = 0;
1007 }
1008 else if (area == RIGHT_MARGIN_AREA)
1009 {
1010 cols = (INTEGERP (w->right_margin_cols)
1011 ? XFASTINT (w->right_margin_cols) : 0);
1012 pixels = 0;
1013 }
1014 }
1015
1016 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1017 }
1018
1019
1020 /* Return the pixel height of the display area of window W, not
1021 including mode lines of W, if any. */
1022
1023 int
1024 window_box_height (struct window *w)
1025 {
1026 struct frame *f = XFRAME (w->frame);
1027 int height = WINDOW_TOTAL_HEIGHT (w);
1028
1029 eassert (height >= 0);
1030
1031 /* Note: the code below that determines the mode-line/header-line
1032 height is essentially the same as that contained in the macro
1033 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1034 the appropriate glyph row has its `mode_line_p' flag set,
1035 and if it doesn't, uses estimate_mode_line_height instead. */
1036
1037 if (WINDOW_WANTS_MODELINE_P (w))
1038 {
1039 struct glyph_row *ml_row
1040 = (w->current_matrix && w->current_matrix->rows
1041 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1042 : 0);
1043 if (ml_row && ml_row->mode_line_p)
1044 height -= ml_row->height;
1045 else
1046 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1047 }
1048
1049 if (WINDOW_WANTS_HEADER_LINE_P (w))
1050 {
1051 struct glyph_row *hl_row
1052 = (w->current_matrix && w->current_matrix->rows
1053 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1054 : 0);
1055 if (hl_row && hl_row->mode_line_p)
1056 height -= hl_row->height;
1057 else
1058 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1059 }
1060
1061 /* With a very small font and a mode-line that's taller than
1062 default, we might end up with a negative height. */
1063 return max (0, height);
1064 }
1065
1066 /* Return the window-relative coordinate of the left edge of display
1067 area AREA of window W. AREA < 0 means return the left edge of the
1068 whole window, to the right of the left fringe of W. */
1069
1070 int
1071 window_box_left_offset (struct window *w, int area)
1072 {
1073 int x;
1074
1075 if (w->pseudo_window_p)
1076 return 0;
1077
1078 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079
1080 if (area == TEXT_AREA)
1081 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1082 + window_box_width (w, LEFT_MARGIN_AREA));
1083 else if (area == RIGHT_MARGIN_AREA)
1084 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1085 + window_box_width (w, LEFT_MARGIN_AREA)
1086 + window_box_width (w, TEXT_AREA)
1087 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 ? 0
1089 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1090 else if (area == LEFT_MARGIN_AREA
1091 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1092 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093
1094 return x;
1095 }
1096
1097
1098 /* Return the window-relative coordinate of the right edge of display
1099 area AREA of window W. AREA < 0 means return the right edge of the
1100 whole window, to the left of the right fringe of W. */
1101
1102 int
1103 window_box_right_offset (struct window *w, int area)
1104 {
1105 return window_box_left_offset (w, area) + window_box_width (w, area);
1106 }
1107
1108 /* Return the frame-relative coordinate of the left edge of display
1109 area AREA of window W. AREA < 0 means return the left edge of the
1110 whole window, to the right of the left fringe of W. */
1111
1112 int
1113 window_box_left (struct window *w, int area)
1114 {
1115 struct frame *f = XFRAME (w->frame);
1116 int x;
1117
1118 if (w->pseudo_window_p)
1119 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120
1121 x = (WINDOW_LEFT_EDGE_X (w)
1122 + window_box_left_offset (w, area));
1123
1124 return x;
1125 }
1126
1127
1128 /* Return the frame-relative coordinate of the right edge of display
1129 area AREA of window W. AREA < 0 means return the right edge of the
1130 whole window, to the left of the right fringe of W. */
1131
1132 int
1133 window_box_right (struct window *w, int area)
1134 {
1135 return window_box_left (w, area) + window_box_width (w, area);
1136 }
1137
1138 /* Get the bounding box of the display area AREA of window W, without
1139 mode lines, in frame-relative coordinates. AREA < 0 means the
1140 whole window, not including the left and right fringes of
1141 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1142 coordinates of the upper-left corner of the box. Return in
1143 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1144
1145 void
1146 window_box (struct window *w, int area, int *box_x, int *box_y,
1147 int *box_width, int *box_height)
1148 {
1149 if (box_width)
1150 *box_width = window_box_width (w, area);
1151 if (box_height)
1152 *box_height = window_box_height (w);
1153 if (box_x)
1154 *box_x = window_box_left (w, area);
1155 if (box_y)
1156 {
1157 *box_y = WINDOW_TOP_EDGE_Y (w);
1158 if (WINDOW_WANTS_HEADER_LINE_P (w))
1159 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1160 }
1161 }
1162
1163
1164 /* Get the bounding box of the display area AREA of window W, without
1165 mode lines. AREA < 0 means the whole window, not including the
1166 left and right fringe of the window. Return in *TOP_LEFT_X
1167 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1168 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1169 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1170 box. */
1171
1172 static void
1173 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1174 int *bottom_right_x, int *bottom_right_y)
1175 {
1176 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1177 bottom_right_y);
1178 *bottom_right_x += *top_left_x;
1179 *bottom_right_y += *top_left_y;
1180 }
1181
1182
1183 \f
1184 /***********************************************************************
1185 Utilities
1186 ***********************************************************************/
1187
1188 /* Return the bottom y-position of the line the iterator IT is in.
1189 This can modify IT's settings. */
1190
1191 int
1192 line_bottom_y (struct it *it)
1193 {
1194 int line_height = it->max_ascent + it->max_descent;
1195 int line_top_y = it->current_y;
1196
1197 if (line_height == 0)
1198 {
1199 if (last_height)
1200 line_height = last_height;
1201 else if (IT_CHARPOS (*it) < ZV)
1202 {
1203 move_it_by_lines (it, 1);
1204 line_height = (it->max_ascent || it->max_descent
1205 ? it->max_ascent + it->max_descent
1206 : last_height);
1207 }
1208 else
1209 {
1210 struct glyph_row *row = it->glyph_row;
1211
1212 /* Use the default character height. */
1213 it->glyph_row = NULL;
1214 it->what = IT_CHARACTER;
1215 it->c = ' ';
1216 it->len = 1;
1217 PRODUCE_GLYPHS (it);
1218 line_height = it->ascent + it->descent;
1219 it->glyph_row = row;
1220 }
1221 }
1222
1223 return line_top_y + line_height;
1224 }
1225
1226 /* Subroutine of pos_visible_p below. Extracts a display string, if
1227 any, from the display spec given as its argument. */
1228 static Lisp_Object
1229 string_from_display_spec (Lisp_Object spec)
1230 {
1231 if (CONSP (spec))
1232 {
1233 while (CONSP (spec))
1234 {
1235 if (STRINGP (XCAR (spec)))
1236 return XCAR (spec);
1237 spec = XCDR (spec);
1238 }
1239 }
1240 else if (VECTORP (spec))
1241 {
1242 ptrdiff_t i;
1243
1244 for (i = 0; i < ASIZE (spec); i++)
1245 {
1246 if (STRINGP (AREF (spec, i)))
1247 return AREF (spec, i);
1248 }
1249 return Qnil;
1250 }
1251
1252 return spec;
1253 }
1254
1255
1256 /* Limit insanely large values of W->hscroll on frame F to the largest
1257 value that will still prevent first_visible_x and last_visible_x of
1258 'struct it' from overflowing an int. */
1259 static int
1260 window_hscroll_limited (struct window *w, struct frame *f)
1261 {
1262 ptrdiff_t window_hscroll = w->hscroll;
1263 int window_text_width = window_box_width (w, TEXT_AREA);
1264 int colwidth = FRAME_COLUMN_WIDTH (f);
1265
1266 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1267 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1268
1269 return window_hscroll;
1270 }
1271
1272 /* Return 1 if position CHARPOS is visible in window W.
1273 CHARPOS < 0 means return info about WINDOW_END position.
1274 If visible, set *X and *Y to pixel coordinates of top left corner.
1275 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1276 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1277
1278 int
1279 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1280 int *rtop, int *rbot, int *rowh, int *vpos)
1281 {
1282 struct it it;
1283 void *itdata = bidi_shelve_cache ();
1284 struct text_pos top;
1285 int visible_p = 0;
1286 struct buffer *old_buffer = NULL;
1287
1288 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1289 return visible_p;
1290
1291 if (XBUFFER (w->buffer) != current_buffer)
1292 {
1293 old_buffer = current_buffer;
1294 set_buffer_internal_1 (XBUFFER (w->buffer));
1295 }
1296
1297 SET_TEXT_POS_FROM_MARKER (top, w->start);
1298 /* Scrolling a minibuffer window via scroll bar when the echo area
1299 shows long text sometimes resets the minibuffer contents behind
1300 our backs. */
1301 if (CHARPOS (top) > ZV)
1302 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1303
1304 /* Compute exact mode line heights. */
1305 if (WINDOW_WANTS_MODELINE_P (w))
1306 current_mode_line_height
1307 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1308 BVAR (current_buffer, mode_line_format));
1309
1310 if (WINDOW_WANTS_HEADER_LINE_P (w))
1311 current_header_line_height
1312 = display_mode_line (w, HEADER_LINE_FACE_ID,
1313 BVAR (current_buffer, header_line_format));
1314
1315 start_display (&it, w, top);
1316 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1317 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1318
1319 if (charpos >= 0
1320 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1321 && IT_CHARPOS (it) >= charpos)
1322 /* When scanning backwards under bidi iteration, move_it_to
1323 stops at or _before_ CHARPOS, because it stops at or to
1324 the _right_ of the character at CHARPOS. */
1325 || (it.bidi_p && it.bidi_it.scan_dir == -1
1326 && IT_CHARPOS (it) <= charpos)))
1327 {
1328 /* We have reached CHARPOS, or passed it. How the call to
1329 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1330 or covered by a display property, move_it_to stops at the end
1331 of the invisible text, to the right of CHARPOS. (ii) If
1332 CHARPOS is in a display vector, move_it_to stops on its last
1333 glyph. */
1334 int top_x = it.current_x;
1335 int top_y = it.current_y;
1336 /* Calling line_bottom_y may change it.method, it.position, etc. */
1337 enum it_method it_method = it.method;
1338 int bottom_y = (last_height = 0, line_bottom_y (&it));
1339 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1340
1341 if (top_y < window_top_y)
1342 visible_p = bottom_y > window_top_y;
1343 else if (top_y < it.last_visible_y)
1344 visible_p = 1;
1345 if (bottom_y >= it.last_visible_y
1346 && it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) < charpos)
1348 {
1349 /* When the last line of the window is scanned backwards
1350 under bidi iteration, we could be duped into thinking
1351 that we have passed CHARPOS, when in fact move_it_to
1352 simply stopped short of CHARPOS because it reached
1353 last_visible_y. To see if that's what happened, we call
1354 move_it_to again with a slightly larger vertical limit,
1355 and see if it actually moved vertically; if it did, we
1356 didn't really reach CHARPOS, which is beyond window end. */
1357 struct it save_it = it;
1358 /* Why 10? because we don't know how many canonical lines
1359 will the height of the next line(s) be. So we guess. */
1360 int ten_more_lines =
1361 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1362
1363 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1364 MOVE_TO_POS | MOVE_TO_Y);
1365 if (it.current_y > top_y)
1366 visible_p = 0;
1367
1368 it = save_it;
1369 }
1370 if (visible_p)
1371 {
1372 if (it_method == GET_FROM_DISPLAY_VECTOR)
1373 {
1374 /* We stopped on the last glyph of a display vector.
1375 Try and recompute. Hack alert! */
1376 if (charpos < 2 || top.charpos >= charpos)
1377 top_x = it.glyph_row->x;
1378 else
1379 {
1380 struct it it2;
1381 start_display (&it2, w, top);
1382 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1383 get_next_display_element (&it2);
1384 PRODUCE_GLYPHS (&it2);
1385 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1386 || it2.current_x > it2.last_visible_x)
1387 top_x = it.glyph_row->x;
1388 else
1389 {
1390 top_x = it2.current_x;
1391 top_y = it2.current_y;
1392 }
1393 }
1394 }
1395 else if (IT_CHARPOS (it) != charpos)
1396 {
1397 Lisp_Object cpos = make_number (charpos);
1398 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1399 Lisp_Object string = string_from_display_spec (spec);
1400 bool newline_in_string
1401 = (STRINGP (string)
1402 && memchr (SDATA (string), '\n', SBYTES (string)));
1403 /* The tricky code below is needed because there's a
1404 discrepancy between move_it_to and how we set cursor
1405 when the display line ends in a newline from a
1406 display string. move_it_to will stop _after_ such
1407 display strings, whereas set_cursor_from_row
1408 conspires with cursor_row_p to place the cursor on
1409 the first glyph produced from the display string. */
1410
1411 /* We have overshoot PT because it is covered by a
1412 display property whose value is a string. If the
1413 string includes embedded newlines, we are also in the
1414 wrong display line. Backtrack to the correct line,
1415 where the display string begins. */
1416 if (newline_in_string)
1417 {
1418 Lisp_Object startpos, endpos;
1419 EMACS_INT start, end;
1420 struct it it3;
1421 int it3_moved;
1422
1423 /* Find the first and the last buffer positions
1424 covered by the display string. */
1425 endpos =
1426 Fnext_single_char_property_change (cpos, Qdisplay,
1427 Qnil, Qnil);
1428 startpos =
1429 Fprevious_single_char_property_change (endpos, Qdisplay,
1430 Qnil, Qnil);
1431 start = XFASTINT (startpos);
1432 end = XFASTINT (endpos);
1433 /* Move to the last buffer position before the
1434 display property. */
1435 start_display (&it3, w, top);
1436 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1437 /* Move forward one more line if the position before
1438 the display string is a newline or if it is the
1439 rightmost character on a line that is
1440 continued or word-wrapped. */
1441 if (it3.method == GET_FROM_BUFFER
1442 && it3.c == '\n')
1443 move_it_by_lines (&it3, 1);
1444 else if (move_it_in_display_line_to (&it3, -1,
1445 it3.current_x
1446 + it3.pixel_width,
1447 MOVE_TO_X)
1448 == MOVE_LINE_CONTINUED)
1449 {
1450 move_it_by_lines (&it3, 1);
1451 /* When we are under word-wrap, the #$@%!
1452 move_it_by_lines moves 2 lines, so we need to
1453 fix that up. */
1454 if (it3.line_wrap == WORD_WRAP)
1455 move_it_by_lines (&it3, -1);
1456 }
1457
1458 /* Record the vertical coordinate of the display
1459 line where we wound up. */
1460 top_y = it3.current_y;
1461 if (it3.bidi_p)
1462 {
1463 /* When characters are reordered for display,
1464 the character displayed to the left of the
1465 display string could be _after_ the display
1466 property in the logical order. Use the
1467 smallest vertical position of these two. */
1468 start_display (&it3, w, top);
1469 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1470 if (it3.current_y < top_y)
1471 top_y = it3.current_y;
1472 }
1473 /* Move from the top of the window to the beginning
1474 of the display line where the display string
1475 begins. */
1476 start_display (&it3, w, top);
1477 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1478 /* If it3_moved stays zero after the 'while' loop
1479 below, that means we already were at a newline
1480 before the loop (e.g., the display string begins
1481 with a newline), so we don't need to (and cannot)
1482 inspect the glyphs of it3.glyph_row, because
1483 PRODUCE_GLYPHS will not produce anything for a
1484 newline, and thus it3.glyph_row stays at its
1485 stale content it got at top of the window. */
1486 it3_moved = 0;
1487 /* Finally, advance the iterator until we hit the
1488 first display element whose character position is
1489 CHARPOS, or until the first newline from the
1490 display string, which signals the end of the
1491 display line. */
1492 while (get_next_display_element (&it3))
1493 {
1494 PRODUCE_GLYPHS (&it3);
1495 if (IT_CHARPOS (it3) == charpos
1496 || ITERATOR_AT_END_OF_LINE_P (&it3))
1497 break;
1498 it3_moved = 1;
1499 set_iterator_to_next (&it3, 0);
1500 }
1501 top_x = it3.current_x - it3.pixel_width;
1502 /* Normally, we would exit the above loop because we
1503 found the display element whose character
1504 position is CHARPOS. For the contingency that we
1505 didn't, and stopped at the first newline from the
1506 display string, move back over the glyphs
1507 produced from the string, until we find the
1508 rightmost glyph not from the string. */
1509 if (it3_moved
1510 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1511 {
1512 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1513 + it3.glyph_row->used[TEXT_AREA];
1514
1515 while (EQ ((g - 1)->object, string))
1516 {
1517 --g;
1518 top_x -= g->pixel_width;
1519 }
1520 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1521 + it3.glyph_row->used[TEXT_AREA]);
1522 }
1523 }
1524 }
1525
1526 *x = top_x;
1527 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1528 *rtop = max (0, window_top_y - top_y);
1529 *rbot = max (0, bottom_y - it.last_visible_y);
1530 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1531 - max (top_y, window_top_y)));
1532 *vpos = it.vpos;
1533 }
1534 }
1535 else
1536 {
1537 /* We were asked to provide info about WINDOW_END. */
1538 struct it it2;
1539 void *it2data = NULL;
1540
1541 SAVE_IT (it2, it, it2data);
1542 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1543 move_it_by_lines (&it, 1);
1544 if (charpos < IT_CHARPOS (it)
1545 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1546 {
1547 visible_p = 1;
1548 RESTORE_IT (&it2, &it2, it2data);
1549 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1550 *x = it2.current_x;
1551 *y = it2.current_y + it2.max_ascent - it2.ascent;
1552 *rtop = max (0, -it2.current_y);
1553 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1554 - it.last_visible_y));
1555 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1556 it.last_visible_y)
1557 - max (it2.current_y,
1558 WINDOW_HEADER_LINE_HEIGHT (w))));
1559 *vpos = it2.vpos;
1560 }
1561 else
1562 bidi_unshelve_cache (it2data, 1);
1563 }
1564 bidi_unshelve_cache (itdata, 0);
1565
1566 if (old_buffer)
1567 set_buffer_internal_1 (old_buffer);
1568
1569 current_header_line_height = current_mode_line_height = -1;
1570
1571 if (visible_p && w->hscroll > 0)
1572 *x -=
1573 window_hscroll_limited (w, WINDOW_XFRAME (w))
1574 * WINDOW_FRAME_COLUMN_WIDTH (w);
1575
1576 #if 0
1577 /* Debugging code. */
1578 if (visible_p)
1579 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1580 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1581 else
1582 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1583 #endif
1584
1585 return visible_p;
1586 }
1587
1588
1589 /* Return the next character from STR. Return in *LEN the length of
1590 the character. This is like STRING_CHAR_AND_LENGTH but never
1591 returns an invalid character. If we find one, we return a `?', but
1592 with the length of the invalid character. */
1593
1594 static int
1595 string_char_and_length (const unsigned char *str, int *len)
1596 {
1597 int c;
1598
1599 c = STRING_CHAR_AND_LENGTH (str, *len);
1600 if (!CHAR_VALID_P (c))
1601 /* We may not change the length here because other places in Emacs
1602 don't use this function, i.e. they silently accept invalid
1603 characters. */
1604 c = '?';
1605
1606 return c;
1607 }
1608
1609
1610
1611 /* Given a position POS containing a valid character and byte position
1612 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1613
1614 static struct text_pos
1615 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1616 {
1617 eassert (STRINGP (string) && nchars >= 0);
1618
1619 if (STRING_MULTIBYTE (string))
1620 {
1621 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1622 int len;
1623
1624 while (nchars--)
1625 {
1626 string_char_and_length (p, &len);
1627 p += len;
1628 CHARPOS (pos) += 1;
1629 BYTEPOS (pos) += len;
1630 }
1631 }
1632 else
1633 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1634
1635 return pos;
1636 }
1637
1638
1639 /* Value is the text position, i.e. character and byte position,
1640 for character position CHARPOS in STRING. */
1641
1642 static struct text_pos
1643 string_pos (ptrdiff_t charpos, Lisp_Object string)
1644 {
1645 struct text_pos pos;
1646 eassert (STRINGP (string));
1647 eassert (charpos >= 0);
1648 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1649 return pos;
1650 }
1651
1652
1653 /* Value is a text position, i.e. character and byte position, for
1654 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1655 means recognize multibyte characters. */
1656
1657 static struct text_pos
1658 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1659 {
1660 struct text_pos pos;
1661
1662 eassert (s != NULL);
1663 eassert (charpos >= 0);
1664
1665 if (multibyte_p)
1666 {
1667 int len;
1668
1669 SET_TEXT_POS (pos, 0, 0);
1670 while (charpos--)
1671 {
1672 string_char_and_length ((const unsigned char *) s, &len);
1673 s += len;
1674 CHARPOS (pos) += 1;
1675 BYTEPOS (pos) += len;
1676 }
1677 }
1678 else
1679 SET_TEXT_POS (pos, charpos, charpos);
1680
1681 return pos;
1682 }
1683
1684
1685 /* Value is the number of characters in C string S. MULTIBYTE_P
1686 non-zero means recognize multibyte characters. */
1687
1688 static ptrdiff_t
1689 number_of_chars (const char *s, bool multibyte_p)
1690 {
1691 ptrdiff_t nchars;
1692
1693 if (multibyte_p)
1694 {
1695 ptrdiff_t rest = strlen (s);
1696 int len;
1697 const unsigned char *p = (const unsigned char *) s;
1698
1699 for (nchars = 0; rest > 0; ++nchars)
1700 {
1701 string_char_and_length (p, &len);
1702 rest -= len, p += len;
1703 }
1704 }
1705 else
1706 nchars = strlen (s);
1707
1708 return nchars;
1709 }
1710
1711
1712 /* Compute byte position NEWPOS->bytepos corresponding to
1713 NEWPOS->charpos. POS is a known position in string STRING.
1714 NEWPOS->charpos must be >= POS.charpos. */
1715
1716 static void
1717 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1718 {
1719 eassert (STRINGP (string));
1720 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1721
1722 if (STRING_MULTIBYTE (string))
1723 *newpos = string_pos_nchars_ahead (pos, string,
1724 CHARPOS (*newpos) - CHARPOS (pos));
1725 else
1726 BYTEPOS (*newpos) = CHARPOS (*newpos);
1727 }
1728
1729 /* EXPORT:
1730 Return an estimation of the pixel height of mode or header lines on
1731 frame F. FACE_ID specifies what line's height to estimate. */
1732
1733 int
1734 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1735 {
1736 #ifdef HAVE_WINDOW_SYSTEM
1737 if (FRAME_WINDOW_P (f))
1738 {
1739 int height = FONT_HEIGHT (FRAME_FONT (f));
1740
1741 /* This function is called so early when Emacs starts that the face
1742 cache and mode line face are not yet initialized. */
1743 if (FRAME_FACE_CACHE (f))
1744 {
1745 struct face *face = FACE_FROM_ID (f, face_id);
1746 if (face)
1747 {
1748 if (face->font)
1749 height = FONT_HEIGHT (face->font);
1750 if (face->box_line_width > 0)
1751 height += 2 * face->box_line_width;
1752 }
1753 }
1754
1755 return height;
1756 }
1757 #endif
1758
1759 return 1;
1760 }
1761
1762 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1763 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1764 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1765 not force the value into range. */
1766
1767 void
1768 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1769 int *x, int *y, NativeRectangle *bounds, int noclip)
1770 {
1771
1772 #ifdef HAVE_WINDOW_SYSTEM
1773 if (FRAME_WINDOW_P (f))
1774 {
1775 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1776 even for negative values. */
1777 if (pix_x < 0)
1778 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1779 if (pix_y < 0)
1780 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1781
1782 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1783 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1784
1785 if (bounds)
1786 STORE_NATIVE_RECT (*bounds,
1787 FRAME_COL_TO_PIXEL_X (f, pix_x),
1788 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1789 FRAME_COLUMN_WIDTH (f) - 1,
1790 FRAME_LINE_HEIGHT (f) - 1);
1791
1792 if (!noclip)
1793 {
1794 if (pix_x < 0)
1795 pix_x = 0;
1796 else if (pix_x > FRAME_TOTAL_COLS (f))
1797 pix_x = FRAME_TOTAL_COLS (f);
1798
1799 if (pix_y < 0)
1800 pix_y = 0;
1801 else if (pix_y > FRAME_LINES (f))
1802 pix_y = FRAME_LINES (f);
1803 }
1804 }
1805 #endif
1806
1807 *x = pix_x;
1808 *y = pix_y;
1809 }
1810
1811
1812 /* Find the glyph under window-relative coordinates X/Y in window W.
1813 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1814 strings. Return in *HPOS and *VPOS the row and column number of
1815 the glyph found. Return in *AREA the glyph area containing X.
1816 Value is a pointer to the glyph found or null if X/Y is not on
1817 text, or we can't tell because W's current matrix is not up to
1818 date. */
1819
1820 static
1821 struct glyph *
1822 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1823 int *dx, int *dy, int *area)
1824 {
1825 struct glyph *glyph, *end;
1826 struct glyph_row *row = NULL;
1827 int x0, i;
1828
1829 /* Find row containing Y. Give up if some row is not enabled. */
1830 for (i = 0; i < w->current_matrix->nrows; ++i)
1831 {
1832 row = MATRIX_ROW (w->current_matrix, i);
1833 if (!row->enabled_p)
1834 return NULL;
1835 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1836 break;
1837 }
1838
1839 *vpos = i;
1840 *hpos = 0;
1841
1842 /* Give up if Y is not in the window. */
1843 if (i == w->current_matrix->nrows)
1844 return NULL;
1845
1846 /* Get the glyph area containing X. */
1847 if (w->pseudo_window_p)
1848 {
1849 *area = TEXT_AREA;
1850 x0 = 0;
1851 }
1852 else
1853 {
1854 if (x < window_box_left_offset (w, TEXT_AREA))
1855 {
1856 *area = LEFT_MARGIN_AREA;
1857 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1858 }
1859 else if (x < window_box_right_offset (w, TEXT_AREA))
1860 {
1861 *area = TEXT_AREA;
1862 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1863 }
1864 else
1865 {
1866 *area = RIGHT_MARGIN_AREA;
1867 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1868 }
1869 }
1870
1871 /* Find glyph containing X. */
1872 glyph = row->glyphs[*area];
1873 end = glyph + row->used[*area];
1874 x -= x0;
1875 while (glyph < end && x >= glyph->pixel_width)
1876 {
1877 x -= glyph->pixel_width;
1878 ++glyph;
1879 }
1880
1881 if (glyph == end)
1882 return NULL;
1883
1884 if (dx)
1885 {
1886 *dx = x;
1887 *dy = y - (row->y + row->ascent - glyph->ascent);
1888 }
1889
1890 *hpos = glyph - row->glyphs[*area];
1891 return glyph;
1892 }
1893
1894 /* Convert frame-relative x/y to coordinates relative to window W.
1895 Takes pseudo-windows into account. */
1896
1897 static void
1898 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1899 {
1900 if (w->pseudo_window_p)
1901 {
1902 /* A pseudo-window is always full-width, and starts at the
1903 left edge of the frame, plus a frame border. */
1904 struct frame *f = XFRAME (w->frame);
1905 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1906 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1907 }
1908 else
1909 {
1910 *x -= WINDOW_LEFT_EDGE_X (w);
1911 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1912 }
1913 }
1914
1915 #ifdef HAVE_WINDOW_SYSTEM
1916
1917 /* EXPORT:
1918 Return in RECTS[] at most N clipping rectangles for glyph string S.
1919 Return the number of stored rectangles. */
1920
1921 int
1922 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1923 {
1924 XRectangle r;
1925
1926 if (n <= 0)
1927 return 0;
1928
1929 if (s->row->full_width_p)
1930 {
1931 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1932 r.x = WINDOW_LEFT_EDGE_X (s->w);
1933 r.width = WINDOW_TOTAL_WIDTH (s->w);
1934
1935 /* Unless displaying a mode or menu bar line, which are always
1936 fully visible, clip to the visible part of the row. */
1937 if (s->w->pseudo_window_p)
1938 r.height = s->row->visible_height;
1939 else
1940 r.height = s->height;
1941 }
1942 else
1943 {
1944 /* This is a text line that may be partially visible. */
1945 r.x = window_box_left (s->w, s->area);
1946 r.width = window_box_width (s->w, s->area);
1947 r.height = s->row->visible_height;
1948 }
1949
1950 if (s->clip_head)
1951 if (r.x < s->clip_head->x)
1952 {
1953 if (r.width >= s->clip_head->x - r.x)
1954 r.width -= s->clip_head->x - r.x;
1955 else
1956 r.width = 0;
1957 r.x = s->clip_head->x;
1958 }
1959 if (s->clip_tail)
1960 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1961 {
1962 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1963 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1964 else
1965 r.width = 0;
1966 }
1967
1968 /* If S draws overlapping rows, it's sufficient to use the top and
1969 bottom of the window for clipping because this glyph string
1970 intentionally draws over other lines. */
1971 if (s->for_overlaps)
1972 {
1973 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1974 r.height = window_text_bottom_y (s->w) - r.y;
1975
1976 /* Alas, the above simple strategy does not work for the
1977 environments with anti-aliased text: if the same text is
1978 drawn onto the same place multiple times, it gets thicker.
1979 If the overlap we are processing is for the erased cursor, we
1980 take the intersection with the rectangle of the cursor. */
1981 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1982 {
1983 XRectangle rc, r_save = r;
1984
1985 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1986 rc.y = s->w->phys_cursor.y;
1987 rc.width = s->w->phys_cursor_width;
1988 rc.height = s->w->phys_cursor_height;
1989
1990 x_intersect_rectangles (&r_save, &rc, &r);
1991 }
1992 }
1993 else
1994 {
1995 /* Don't use S->y for clipping because it doesn't take partially
1996 visible lines into account. For example, it can be negative for
1997 partially visible lines at the top of a window. */
1998 if (!s->row->full_width_p
1999 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2000 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2001 else
2002 r.y = max (0, s->row->y);
2003 }
2004
2005 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2006
2007 /* If drawing the cursor, don't let glyph draw outside its
2008 advertised boundaries. Cleartype does this under some circumstances. */
2009 if (s->hl == DRAW_CURSOR)
2010 {
2011 struct glyph *glyph = s->first_glyph;
2012 int height, max_y;
2013
2014 if (s->x > r.x)
2015 {
2016 r.width -= s->x - r.x;
2017 r.x = s->x;
2018 }
2019 r.width = min (r.width, glyph->pixel_width);
2020
2021 /* If r.y is below window bottom, ensure that we still see a cursor. */
2022 height = min (glyph->ascent + glyph->descent,
2023 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2024 max_y = window_text_bottom_y (s->w) - height;
2025 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2026 if (s->ybase - glyph->ascent > max_y)
2027 {
2028 r.y = max_y;
2029 r.height = height;
2030 }
2031 else
2032 {
2033 /* Don't draw cursor glyph taller than our actual glyph. */
2034 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2035 if (height < r.height)
2036 {
2037 max_y = r.y + r.height;
2038 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2039 r.height = min (max_y - r.y, height);
2040 }
2041 }
2042 }
2043
2044 if (s->row->clip)
2045 {
2046 XRectangle r_save = r;
2047
2048 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2049 r.width = 0;
2050 }
2051
2052 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2053 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2054 {
2055 #ifdef CONVERT_FROM_XRECT
2056 CONVERT_FROM_XRECT (r, *rects);
2057 #else
2058 *rects = r;
2059 #endif
2060 return 1;
2061 }
2062 else
2063 {
2064 /* If we are processing overlapping and allowed to return
2065 multiple clipping rectangles, we exclude the row of the glyph
2066 string from the clipping rectangle. This is to avoid drawing
2067 the same text on the environment with anti-aliasing. */
2068 #ifdef CONVERT_FROM_XRECT
2069 XRectangle rs[2];
2070 #else
2071 XRectangle *rs = rects;
2072 #endif
2073 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2074
2075 if (s->for_overlaps & OVERLAPS_PRED)
2076 {
2077 rs[i] = r;
2078 if (r.y + r.height > row_y)
2079 {
2080 if (r.y < row_y)
2081 rs[i].height = row_y - r.y;
2082 else
2083 rs[i].height = 0;
2084 }
2085 i++;
2086 }
2087 if (s->for_overlaps & OVERLAPS_SUCC)
2088 {
2089 rs[i] = r;
2090 if (r.y < row_y + s->row->visible_height)
2091 {
2092 if (r.y + r.height > row_y + s->row->visible_height)
2093 {
2094 rs[i].y = row_y + s->row->visible_height;
2095 rs[i].height = r.y + r.height - rs[i].y;
2096 }
2097 else
2098 rs[i].height = 0;
2099 }
2100 i++;
2101 }
2102
2103 n = i;
2104 #ifdef CONVERT_FROM_XRECT
2105 for (i = 0; i < n; i++)
2106 CONVERT_FROM_XRECT (rs[i], rects[i]);
2107 #endif
2108 return n;
2109 }
2110 }
2111
2112 /* EXPORT:
2113 Return in *NR the clipping rectangle for glyph string S. */
2114
2115 void
2116 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2117 {
2118 get_glyph_string_clip_rects (s, nr, 1);
2119 }
2120
2121
2122 /* EXPORT:
2123 Return the position and height of the phys cursor in window W.
2124 Set w->phys_cursor_width to width of phys cursor.
2125 */
2126
2127 void
2128 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2129 struct glyph *glyph, int *xp, int *yp, int *heightp)
2130 {
2131 struct frame *f = XFRAME (WINDOW_FRAME (w));
2132 int x, y, wd, h, h0, y0;
2133
2134 /* Compute the width of the rectangle to draw. If on a stretch
2135 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2136 rectangle as wide as the glyph, but use a canonical character
2137 width instead. */
2138 wd = glyph->pixel_width - 1;
2139 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2140 wd++; /* Why? */
2141 #endif
2142
2143 x = w->phys_cursor.x;
2144 if (x < 0)
2145 {
2146 wd += x;
2147 x = 0;
2148 }
2149
2150 if (glyph->type == STRETCH_GLYPH
2151 && !x_stretch_cursor_p)
2152 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2153 w->phys_cursor_width = wd;
2154
2155 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2156
2157 /* If y is below window bottom, ensure that we still see a cursor. */
2158 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2159
2160 h = max (h0, glyph->ascent + glyph->descent);
2161 h0 = min (h0, glyph->ascent + glyph->descent);
2162
2163 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2164 if (y < y0)
2165 {
2166 h = max (h - (y0 - y) + 1, h0);
2167 y = y0 - 1;
2168 }
2169 else
2170 {
2171 y0 = window_text_bottom_y (w) - h0;
2172 if (y > y0)
2173 {
2174 h += y - y0;
2175 y = y0;
2176 }
2177 }
2178
2179 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2180 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2181 *heightp = h;
2182 }
2183
2184 /*
2185 * Remember which glyph the mouse is over.
2186 */
2187
2188 void
2189 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2190 {
2191 Lisp_Object window;
2192 struct window *w;
2193 struct glyph_row *r, *gr, *end_row;
2194 enum window_part part;
2195 enum glyph_row_area area;
2196 int x, y, width, height;
2197
2198 /* Try to determine frame pixel position and size of the glyph under
2199 frame pixel coordinates X/Y on frame F. */
2200
2201 if (!f->glyphs_initialized_p
2202 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2203 NILP (window)))
2204 {
2205 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2206 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2207 goto virtual_glyph;
2208 }
2209
2210 w = XWINDOW (window);
2211 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2212 height = WINDOW_FRAME_LINE_HEIGHT (w);
2213
2214 x = window_relative_x_coord (w, part, gx);
2215 y = gy - WINDOW_TOP_EDGE_Y (w);
2216
2217 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2218 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2219
2220 if (w->pseudo_window_p)
2221 {
2222 area = TEXT_AREA;
2223 part = ON_MODE_LINE; /* Don't adjust margin. */
2224 goto text_glyph;
2225 }
2226
2227 switch (part)
2228 {
2229 case ON_LEFT_MARGIN:
2230 area = LEFT_MARGIN_AREA;
2231 goto text_glyph;
2232
2233 case ON_RIGHT_MARGIN:
2234 area = RIGHT_MARGIN_AREA;
2235 goto text_glyph;
2236
2237 case ON_HEADER_LINE:
2238 case ON_MODE_LINE:
2239 gr = (part == ON_HEADER_LINE
2240 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2241 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2242 gy = gr->y;
2243 area = TEXT_AREA;
2244 goto text_glyph_row_found;
2245
2246 case ON_TEXT:
2247 area = TEXT_AREA;
2248
2249 text_glyph:
2250 gr = 0; gy = 0;
2251 for (; r <= end_row && r->enabled_p; ++r)
2252 if (r->y + r->height > y)
2253 {
2254 gr = r; gy = r->y;
2255 break;
2256 }
2257
2258 text_glyph_row_found:
2259 if (gr && gy <= y)
2260 {
2261 struct glyph *g = gr->glyphs[area];
2262 struct glyph *end = g + gr->used[area];
2263
2264 height = gr->height;
2265 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2266 if (gx + g->pixel_width > x)
2267 break;
2268
2269 if (g < end)
2270 {
2271 if (g->type == IMAGE_GLYPH)
2272 {
2273 /* Don't remember when mouse is over image, as
2274 image may have hot-spots. */
2275 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2276 return;
2277 }
2278 width = g->pixel_width;
2279 }
2280 else
2281 {
2282 /* Use nominal char spacing at end of line. */
2283 x -= gx;
2284 gx += (x / width) * width;
2285 }
2286
2287 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2288 gx += window_box_left_offset (w, area);
2289 }
2290 else
2291 {
2292 /* Use nominal line height at end of window. */
2293 gx = (x / width) * width;
2294 y -= gy;
2295 gy += (y / height) * height;
2296 }
2297 break;
2298
2299 case ON_LEFT_FRINGE:
2300 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2301 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2302 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2303 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2304 goto row_glyph;
2305
2306 case ON_RIGHT_FRINGE:
2307 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2308 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2309 : window_box_right_offset (w, TEXT_AREA));
2310 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2311 goto row_glyph;
2312
2313 case ON_SCROLL_BAR:
2314 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2315 ? 0
2316 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2317 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2318 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2319 : 0)));
2320 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2321
2322 row_glyph:
2323 gr = 0, gy = 0;
2324 for (; r <= end_row && r->enabled_p; ++r)
2325 if (r->y + r->height > y)
2326 {
2327 gr = r; gy = r->y;
2328 break;
2329 }
2330
2331 if (gr && gy <= y)
2332 height = gr->height;
2333 else
2334 {
2335 /* Use nominal line height at end of window. */
2336 y -= gy;
2337 gy += (y / height) * height;
2338 }
2339 break;
2340
2341 default:
2342 ;
2343 virtual_glyph:
2344 /* If there is no glyph under the mouse, then we divide the screen
2345 into a grid of the smallest glyph in the frame, and use that
2346 as our "glyph". */
2347
2348 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2349 round down even for negative values. */
2350 if (gx < 0)
2351 gx -= width - 1;
2352 if (gy < 0)
2353 gy -= height - 1;
2354
2355 gx = (gx / width) * width;
2356 gy = (gy / height) * height;
2357
2358 goto store_rect;
2359 }
2360
2361 gx += WINDOW_LEFT_EDGE_X (w);
2362 gy += WINDOW_TOP_EDGE_Y (w);
2363
2364 store_rect:
2365 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2366
2367 /* Visible feedback for debugging. */
2368 #if 0
2369 #if HAVE_X_WINDOWS
2370 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2371 f->output_data.x->normal_gc,
2372 gx, gy, width, height);
2373 #endif
2374 #endif
2375 }
2376
2377
2378 #endif /* HAVE_WINDOW_SYSTEM */
2379
2380 \f
2381 /***********************************************************************
2382 Lisp form evaluation
2383 ***********************************************************************/
2384
2385 /* Error handler for safe_eval and safe_call. */
2386
2387 static Lisp_Object
2388 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2389 {
2390 add_to_log ("Error during redisplay: %S signaled %S",
2391 Flist (nargs, args), arg);
2392 return Qnil;
2393 }
2394
2395 /* Call function FUNC with the rest of NARGS - 1 arguments
2396 following. Return the result, or nil if something went
2397 wrong. Prevent redisplay during the evaluation. */
2398
2399 Lisp_Object
2400 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2401 {
2402 Lisp_Object val;
2403
2404 if (inhibit_eval_during_redisplay)
2405 val = Qnil;
2406 else
2407 {
2408 va_list ap;
2409 ptrdiff_t i;
2410 ptrdiff_t count = SPECPDL_INDEX ();
2411 struct gcpro gcpro1;
2412 Lisp_Object *args = alloca (nargs * word_size);
2413
2414 args[0] = func;
2415 va_start (ap, func);
2416 for (i = 1; i < nargs; i++)
2417 args[i] = va_arg (ap, Lisp_Object);
2418 va_end (ap);
2419
2420 GCPRO1 (args[0]);
2421 gcpro1.nvars = nargs;
2422 specbind (Qinhibit_redisplay, Qt);
2423 /* Use Qt to ensure debugger does not run,
2424 so there is no possibility of wanting to redisplay. */
2425 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2426 safe_eval_handler);
2427 UNGCPRO;
2428 val = unbind_to (count, val);
2429 }
2430
2431 return val;
2432 }
2433
2434
2435 /* Call function FN with one argument ARG.
2436 Return the result, or nil if something went wrong. */
2437
2438 Lisp_Object
2439 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2440 {
2441 return safe_call (2, fn, arg);
2442 }
2443
2444 static Lisp_Object Qeval;
2445
2446 Lisp_Object
2447 safe_eval (Lisp_Object sexpr)
2448 {
2449 return safe_call1 (Qeval, sexpr);
2450 }
2451
2452 /* Call function FN with two arguments ARG1 and ARG2.
2453 Return the result, or nil if something went wrong. */
2454
2455 Lisp_Object
2456 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2457 {
2458 return safe_call (3, fn, arg1, arg2);
2459 }
2460
2461
2462 \f
2463 /***********************************************************************
2464 Debugging
2465 ***********************************************************************/
2466
2467 #if 0
2468
2469 /* Define CHECK_IT to perform sanity checks on iterators.
2470 This is for debugging. It is too slow to do unconditionally. */
2471
2472 static void
2473 check_it (struct it *it)
2474 {
2475 if (it->method == GET_FROM_STRING)
2476 {
2477 eassert (STRINGP (it->string));
2478 eassert (IT_STRING_CHARPOS (*it) >= 0);
2479 }
2480 else
2481 {
2482 eassert (IT_STRING_CHARPOS (*it) < 0);
2483 if (it->method == GET_FROM_BUFFER)
2484 {
2485 /* Check that character and byte positions agree. */
2486 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2487 }
2488 }
2489
2490 if (it->dpvec)
2491 eassert (it->current.dpvec_index >= 0);
2492 else
2493 eassert (it->current.dpvec_index < 0);
2494 }
2495
2496 #define CHECK_IT(IT) check_it ((IT))
2497
2498 #else /* not 0 */
2499
2500 #define CHECK_IT(IT) (void) 0
2501
2502 #endif /* not 0 */
2503
2504
2505 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2506
2507 /* Check that the window end of window W is what we expect it
2508 to be---the last row in the current matrix displaying text. */
2509
2510 static void
2511 check_window_end (struct window *w)
2512 {
2513 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2514 {
2515 struct glyph_row *row;
2516 eassert ((row = MATRIX_ROW (w->current_matrix,
2517 XFASTINT (w->window_end_vpos)),
2518 !row->enabled_p
2519 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2520 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2521 }
2522 }
2523
2524 #define CHECK_WINDOW_END(W) check_window_end ((W))
2525
2526 #else
2527
2528 #define CHECK_WINDOW_END(W) (void) 0
2529
2530 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2531
2532 /* Return mark position if current buffer has the region of non-zero length,
2533 or -1 otherwise. */
2534
2535 static ptrdiff_t
2536 markpos_of_region (void)
2537 {
2538 if (!NILP (Vtransient_mark_mode)
2539 && !NILP (BVAR (current_buffer, mark_active))
2540 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2541 {
2542 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2543
2544 if (markpos != PT)
2545 return markpos;
2546 }
2547 return -1;
2548 }
2549
2550 /***********************************************************************
2551 Iterator initialization
2552 ***********************************************************************/
2553
2554 /* Initialize IT for displaying current_buffer in window W, starting
2555 at character position CHARPOS. CHARPOS < 0 means that no buffer
2556 position is specified which is useful when the iterator is assigned
2557 a position later. BYTEPOS is the byte position corresponding to
2558 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2559
2560 If ROW is not null, calls to produce_glyphs with IT as parameter
2561 will produce glyphs in that row.
2562
2563 BASE_FACE_ID is the id of a base face to use. It must be one of
2564 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2565 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2566 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2567
2568 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2569 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2570 will be initialized to use the corresponding mode line glyph row of
2571 the desired matrix of W. */
2572
2573 void
2574 init_iterator (struct it *it, struct window *w,
2575 ptrdiff_t charpos, ptrdiff_t bytepos,
2576 struct glyph_row *row, enum face_id base_face_id)
2577 {
2578 ptrdiff_t markpos;
2579 enum face_id remapped_base_face_id = base_face_id;
2580
2581 /* Some precondition checks. */
2582 eassert (w != NULL && it != NULL);
2583 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2584 && charpos <= ZV));
2585
2586 /* If face attributes have been changed since the last redisplay,
2587 free realized faces now because they depend on face definitions
2588 that might have changed. Don't free faces while there might be
2589 desired matrices pending which reference these faces. */
2590 if (face_change_count && !inhibit_free_realized_faces)
2591 {
2592 face_change_count = 0;
2593 free_all_realized_faces (Qnil);
2594 }
2595
2596 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2597 if (! NILP (Vface_remapping_alist))
2598 remapped_base_face_id
2599 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2600
2601 /* Use one of the mode line rows of W's desired matrix if
2602 appropriate. */
2603 if (row == NULL)
2604 {
2605 if (base_face_id == MODE_LINE_FACE_ID
2606 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2607 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2608 else if (base_face_id == HEADER_LINE_FACE_ID)
2609 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2610 }
2611
2612 /* Clear IT. */
2613 memset (it, 0, sizeof *it);
2614 it->current.overlay_string_index = -1;
2615 it->current.dpvec_index = -1;
2616 it->base_face_id = remapped_base_face_id;
2617 it->string = Qnil;
2618 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2619 it->paragraph_embedding = L2R;
2620 it->bidi_it.string.lstring = Qnil;
2621 it->bidi_it.string.s = NULL;
2622 it->bidi_it.string.bufpos = 0;
2623
2624 /* The window in which we iterate over current_buffer: */
2625 XSETWINDOW (it->window, w);
2626 it->w = w;
2627 it->f = XFRAME (w->frame);
2628
2629 it->cmp_it.id = -1;
2630
2631 /* Extra space between lines (on window systems only). */
2632 if (base_face_id == DEFAULT_FACE_ID
2633 && FRAME_WINDOW_P (it->f))
2634 {
2635 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2636 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2637 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2638 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2639 * FRAME_LINE_HEIGHT (it->f));
2640 else if (it->f->extra_line_spacing > 0)
2641 it->extra_line_spacing = it->f->extra_line_spacing;
2642 it->max_extra_line_spacing = 0;
2643 }
2644
2645 /* If realized faces have been removed, e.g. because of face
2646 attribute changes of named faces, recompute them. When running
2647 in batch mode, the face cache of the initial frame is null. If
2648 we happen to get called, make a dummy face cache. */
2649 if (FRAME_FACE_CACHE (it->f) == NULL)
2650 init_frame_faces (it->f);
2651 if (FRAME_FACE_CACHE (it->f)->used == 0)
2652 recompute_basic_faces (it->f);
2653
2654 /* Current value of the `slice', `space-width', and 'height' properties. */
2655 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2656 it->space_width = Qnil;
2657 it->font_height = Qnil;
2658 it->override_ascent = -1;
2659
2660 /* Are control characters displayed as `^C'? */
2661 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2662
2663 /* -1 means everything between a CR and the following line end
2664 is invisible. >0 means lines indented more than this value are
2665 invisible. */
2666 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2667 ? (clip_to_bounds
2668 (-1, XINT (BVAR (current_buffer, selective_display)),
2669 PTRDIFF_MAX))
2670 : (!NILP (BVAR (current_buffer, selective_display))
2671 ? -1 : 0));
2672 it->selective_display_ellipsis_p
2673 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2674
2675 /* Display table to use. */
2676 it->dp = window_display_table (w);
2677
2678 /* Are multibyte characters enabled in current_buffer? */
2679 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2680
2681 /* If visible region is of non-zero length, set IT->region_beg_charpos
2682 and IT->region_end_charpos to the start and end of a visible region
2683 in window IT->w. Set both to -1 to indicate no region. */
2684 markpos = markpos_of_region ();
2685 if (0 <= markpos
2686 /* Maybe highlight only in selected window. */
2687 && (/* Either show region everywhere. */
2688 highlight_nonselected_windows
2689 /* Or show region in the selected window. */
2690 || w == XWINDOW (selected_window)
2691 /* Or show the region if we are in the mini-buffer and W is
2692 the window the mini-buffer refers to. */
2693 || (MINI_WINDOW_P (XWINDOW (selected_window))
2694 && WINDOWP (minibuf_selected_window)
2695 && w == XWINDOW (minibuf_selected_window))))
2696 {
2697 it->region_beg_charpos = min (PT, markpos);
2698 it->region_end_charpos = max (PT, markpos);
2699 }
2700 else
2701 it->region_beg_charpos = it->region_end_charpos = -1;
2702
2703 /* Get the position at which the redisplay_end_trigger hook should
2704 be run, if it is to be run at all. */
2705 if (MARKERP (w->redisplay_end_trigger)
2706 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2707 it->redisplay_end_trigger_charpos
2708 = marker_position (w->redisplay_end_trigger);
2709 else if (INTEGERP (w->redisplay_end_trigger))
2710 it->redisplay_end_trigger_charpos =
2711 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2712
2713 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2714
2715 /* Are lines in the display truncated? */
2716 if (base_face_id != DEFAULT_FACE_ID
2717 || it->w->hscroll
2718 || (! WINDOW_FULL_WIDTH_P (it->w)
2719 && ((!NILP (Vtruncate_partial_width_windows)
2720 && !INTEGERP (Vtruncate_partial_width_windows))
2721 || (INTEGERP (Vtruncate_partial_width_windows)
2722 && (WINDOW_TOTAL_COLS (it->w)
2723 < XINT (Vtruncate_partial_width_windows))))))
2724 it->line_wrap = TRUNCATE;
2725 else if (NILP (BVAR (current_buffer, truncate_lines)))
2726 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2727 ? WINDOW_WRAP : WORD_WRAP;
2728 else
2729 it->line_wrap = TRUNCATE;
2730
2731 /* Get dimensions of truncation and continuation glyphs. These are
2732 displayed as fringe bitmaps under X, but we need them for such
2733 frames when the fringes are turned off. But leave the dimensions
2734 zero for tooltip frames, as these glyphs look ugly there and also
2735 sabotage calculations of tooltip dimensions in x-show-tip. */
2736 #ifdef HAVE_WINDOW_SYSTEM
2737 if (!(FRAME_WINDOW_P (it->f)
2738 && FRAMEP (tip_frame)
2739 && it->f == XFRAME (tip_frame)))
2740 #endif
2741 {
2742 if (it->line_wrap == TRUNCATE)
2743 {
2744 /* We will need the truncation glyph. */
2745 eassert (it->glyph_row == NULL);
2746 produce_special_glyphs (it, IT_TRUNCATION);
2747 it->truncation_pixel_width = it->pixel_width;
2748 }
2749 else
2750 {
2751 /* We will need the continuation glyph. */
2752 eassert (it->glyph_row == NULL);
2753 produce_special_glyphs (it, IT_CONTINUATION);
2754 it->continuation_pixel_width = it->pixel_width;
2755 }
2756 }
2757
2758 /* Reset these values to zero because the produce_special_glyphs
2759 above has changed them. */
2760 it->pixel_width = it->ascent = it->descent = 0;
2761 it->phys_ascent = it->phys_descent = 0;
2762
2763 /* Set this after getting the dimensions of truncation and
2764 continuation glyphs, so that we don't produce glyphs when calling
2765 produce_special_glyphs, above. */
2766 it->glyph_row = row;
2767 it->area = TEXT_AREA;
2768
2769 /* Forget any previous info about this row being reversed. */
2770 if (it->glyph_row)
2771 it->glyph_row->reversed_p = 0;
2772
2773 /* Get the dimensions of the display area. The display area
2774 consists of the visible window area plus a horizontally scrolled
2775 part to the left of the window. All x-values are relative to the
2776 start of this total display area. */
2777 if (base_face_id != DEFAULT_FACE_ID)
2778 {
2779 /* Mode lines, menu bar in terminal frames. */
2780 it->first_visible_x = 0;
2781 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2782 }
2783 else
2784 {
2785 it->first_visible_x =
2786 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2787 it->last_visible_x = (it->first_visible_x
2788 + window_box_width (w, TEXT_AREA));
2789
2790 /* If we truncate lines, leave room for the truncation glyph(s) at
2791 the right margin. Otherwise, leave room for the continuation
2792 glyph(s). Done only if the window has no fringes. Since we
2793 don't know at this point whether there will be any R2L lines in
2794 the window, we reserve space for truncation/continuation glyphs
2795 even if only one of the fringes is absent. */
2796 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2797 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2798 {
2799 if (it->line_wrap == TRUNCATE)
2800 it->last_visible_x -= it->truncation_pixel_width;
2801 else
2802 it->last_visible_x -= it->continuation_pixel_width;
2803 }
2804
2805 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2806 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2807 }
2808
2809 /* Leave room for a border glyph. */
2810 if (!FRAME_WINDOW_P (it->f)
2811 && !WINDOW_RIGHTMOST_P (it->w))
2812 it->last_visible_x -= 1;
2813
2814 it->last_visible_y = window_text_bottom_y (w);
2815
2816 /* For mode lines and alike, arrange for the first glyph having a
2817 left box line if the face specifies a box. */
2818 if (base_face_id != DEFAULT_FACE_ID)
2819 {
2820 struct face *face;
2821
2822 it->face_id = remapped_base_face_id;
2823
2824 /* If we have a boxed mode line, make the first character appear
2825 with a left box line. */
2826 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2827 if (face->box != FACE_NO_BOX)
2828 it->start_of_box_run_p = 1;
2829 }
2830
2831 /* If a buffer position was specified, set the iterator there,
2832 getting overlays and face properties from that position. */
2833 if (charpos >= BUF_BEG (current_buffer))
2834 {
2835 it->end_charpos = ZV;
2836 IT_CHARPOS (*it) = charpos;
2837
2838 /* We will rely on `reseat' to set this up properly, via
2839 handle_face_prop. */
2840 it->face_id = it->base_face_id;
2841
2842 /* Compute byte position if not specified. */
2843 if (bytepos < charpos)
2844 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2845 else
2846 IT_BYTEPOS (*it) = bytepos;
2847
2848 it->start = it->current;
2849 /* Do we need to reorder bidirectional text? Not if this is a
2850 unibyte buffer: by definition, none of the single-byte
2851 characters are strong R2L, so no reordering is needed. And
2852 bidi.c doesn't support unibyte buffers anyway. Also, don't
2853 reorder while we are loading loadup.el, since the tables of
2854 character properties needed for reordering are not yet
2855 available. */
2856 it->bidi_p =
2857 NILP (Vpurify_flag)
2858 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2859 && it->multibyte_p;
2860
2861 /* If we are to reorder bidirectional text, init the bidi
2862 iterator. */
2863 if (it->bidi_p)
2864 {
2865 /* Note the paragraph direction that this buffer wants to
2866 use. */
2867 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2868 Qleft_to_right))
2869 it->paragraph_embedding = L2R;
2870 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2871 Qright_to_left))
2872 it->paragraph_embedding = R2L;
2873 else
2874 it->paragraph_embedding = NEUTRAL_DIR;
2875 bidi_unshelve_cache (NULL, 0);
2876 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2877 &it->bidi_it);
2878 }
2879
2880 /* Compute faces etc. */
2881 reseat (it, it->current.pos, 1);
2882 }
2883
2884 CHECK_IT (it);
2885 }
2886
2887
2888 /* Initialize IT for the display of window W with window start POS. */
2889
2890 void
2891 start_display (struct it *it, struct window *w, struct text_pos pos)
2892 {
2893 struct glyph_row *row;
2894 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2895
2896 row = w->desired_matrix->rows + first_vpos;
2897 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2898 it->first_vpos = first_vpos;
2899
2900 /* Don't reseat to previous visible line start if current start
2901 position is in a string or image. */
2902 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2903 {
2904 int start_at_line_beg_p;
2905 int first_y = it->current_y;
2906
2907 /* If window start is not at a line start, skip forward to POS to
2908 get the correct continuation lines width. */
2909 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2910 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2911 if (!start_at_line_beg_p)
2912 {
2913 int new_x;
2914
2915 reseat_at_previous_visible_line_start (it);
2916 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2917
2918 new_x = it->current_x + it->pixel_width;
2919
2920 /* If lines are continued, this line may end in the middle
2921 of a multi-glyph character (e.g. a control character
2922 displayed as \003, or in the middle of an overlay
2923 string). In this case move_it_to above will not have
2924 taken us to the start of the continuation line but to the
2925 end of the continued line. */
2926 if (it->current_x > 0
2927 && it->line_wrap != TRUNCATE /* Lines are continued. */
2928 && (/* And glyph doesn't fit on the line. */
2929 new_x > it->last_visible_x
2930 /* Or it fits exactly and we're on a window
2931 system frame. */
2932 || (new_x == it->last_visible_x
2933 && FRAME_WINDOW_P (it->f)
2934 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2935 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2936 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2937 {
2938 if ((it->current.dpvec_index >= 0
2939 || it->current.overlay_string_index >= 0)
2940 /* If we are on a newline from a display vector or
2941 overlay string, then we are already at the end of
2942 a screen line; no need to go to the next line in
2943 that case, as this line is not really continued.
2944 (If we do go to the next line, C-e will not DTRT.) */
2945 && it->c != '\n')
2946 {
2947 set_iterator_to_next (it, 1);
2948 move_it_in_display_line_to (it, -1, -1, 0);
2949 }
2950
2951 it->continuation_lines_width += it->current_x;
2952 }
2953 /* If the character at POS is displayed via a display
2954 vector, move_it_to above stops at the final glyph of
2955 IT->dpvec. To make the caller redisplay that character
2956 again (a.k.a. start at POS), we need to reset the
2957 dpvec_index to the beginning of IT->dpvec. */
2958 else if (it->current.dpvec_index >= 0)
2959 it->current.dpvec_index = 0;
2960
2961 /* We're starting a new display line, not affected by the
2962 height of the continued line, so clear the appropriate
2963 fields in the iterator structure. */
2964 it->max_ascent = it->max_descent = 0;
2965 it->max_phys_ascent = it->max_phys_descent = 0;
2966
2967 it->current_y = first_y;
2968 it->vpos = 0;
2969 it->current_x = it->hpos = 0;
2970 }
2971 }
2972 }
2973
2974
2975 /* Return 1 if POS is a position in ellipses displayed for invisible
2976 text. W is the window we display, for text property lookup. */
2977
2978 static int
2979 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2980 {
2981 Lisp_Object prop, window;
2982 int ellipses_p = 0;
2983 ptrdiff_t charpos = CHARPOS (pos->pos);
2984
2985 /* If POS specifies a position in a display vector, this might
2986 be for an ellipsis displayed for invisible text. We won't
2987 get the iterator set up for delivering that ellipsis unless
2988 we make sure that it gets aware of the invisible text. */
2989 if (pos->dpvec_index >= 0
2990 && pos->overlay_string_index < 0
2991 && CHARPOS (pos->string_pos) < 0
2992 && charpos > BEGV
2993 && (XSETWINDOW (window, w),
2994 prop = Fget_char_property (make_number (charpos),
2995 Qinvisible, window),
2996 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2997 {
2998 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2999 window);
3000 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3001 }
3002
3003 return ellipses_p;
3004 }
3005
3006
3007 /* Initialize IT for stepping through current_buffer in window W,
3008 starting at position POS that includes overlay string and display
3009 vector/ control character translation position information. Value
3010 is zero if there are overlay strings with newlines at POS. */
3011
3012 static int
3013 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3014 {
3015 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3016 int i, overlay_strings_with_newlines = 0;
3017
3018 /* If POS specifies a position in a display vector, this might
3019 be for an ellipsis displayed for invisible text. We won't
3020 get the iterator set up for delivering that ellipsis unless
3021 we make sure that it gets aware of the invisible text. */
3022 if (in_ellipses_for_invisible_text_p (pos, w))
3023 {
3024 --charpos;
3025 bytepos = 0;
3026 }
3027
3028 /* Keep in mind: the call to reseat in init_iterator skips invisible
3029 text, so we might end up at a position different from POS. This
3030 is only a problem when POS is a row start after a newline and an
3031 overlay starts there with an after-string, and the overlay has an
3032 invisible property. Since we don't skip invisible text in
3033 display_line and elsewhere immediately after consuming the
3034 newline before the row start, such a POS will not be in a string,
3035 but the call to init_iterator below will move us to the
3036 after-string. */
3037 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3038
3039 /* This only scans the current chunk -- it should scan all chunks.
3040 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3041 to 16 in 22.1 to make this a lesser problem. */
3042 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3043 {
3044 const char *s = SSDATA (it->overlay_strings[i]);
3045 const char *e = s + SBYTES (it->overlay_strings[i]);
3046
3047 while (s < e && *s != '\n')
3048 ++s;
3049
3050 if (s < e)
3051 {
3052 overlay_strings_with_newlines = 1;
3053 break;
3054 }
3055 }
3056
3057 /* If position is within an overlay string, set up IT to the right
3058 overlay string. */
3059 if (pos->overlay_string_index >= 0)
3060 {
3061 int relative_index;
3062
3063 /* If the first overlay string happens to have a `display'
3064 property for an image, the iterator will be set up for that
3065 image, and we have to undo that setup first before we can
3066 correct the overlay string index. */
3067 if (it->method == GET_FROM_IMAGE)
3068 pop_it (it);
3069
3070 /* We already have the first chunk of overlay strings in
3071 IT->overlay_strings. Load more until the one for
3072 pos->overlay_string_index is in IT->overlay_strings. */
3073 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3074 {
3075 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3076 it->current.overlay_string_index = 0;
3077 while (n--)
3078 {
3079 load_overlay_strings (it, 0);
3080 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3081 }
3082 }
3083
3084 it->current.overlay_string_index = pos->overlay_string_index;
3085 relative_index = (it->current.overlay_string_index
3086 % OVERLAY_STRING_CHUNK_SIZE);
3087 it->string = it->overlay_strings[relative_index];
3088 eassert (STRINGP (it->string));
3089 it->current.string_pos = pos->string_pos;
3090 it->method = GET_FROM_STRING;
3091 it->end_charpos = SCHARS (it->string);
3092 /* Set up the bidi iterator for this overlay string. */
3093 if (it->bidi_p)
3094 {
3095 it->bidi_it.string.lstring = it->string;
3096 it->bidi_it.string.s = NULL;
3097 it->bidi_it.string.schars = SCHARS (it->string);
3098 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3099 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3100 it->bidi_it.string.unibyte = !it->multibyte_p;
3101 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3102 FRAME_WINDOW_P (it->f), &it->bidi_it);
3103
3104 /* Synchronize the state of the bidi iterator with
3105 pos->string_pos. For any string position other than
3106 zero, this will be done automagically when we resume
3107 iteration over the string and get_visually_first_element
3108 is called. But if string_pos is zero, and the string is
3109 to be reordered for display, we need to resync manually,
3110 since it could be that the iteration state recorded in
3111 pos ended at string_pos of 0 moving backwards in string. */
3112 if (CHARPOS (pos->string_pos) == 0)
3113 {
3114 get_visually_first_element (it);
3115 if (IT_STRING_CHARPOS (*it) != 0)
3116 do {
3117 /* Paranoia. */
3118 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3119 bidi_move_to_visually_next (&it->bidi_it);
3120 } while (it->bidi_it.charpos != 0);
3121 }
3122 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3123 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3124 }
3125 }
3126
3127 if (CHARPOS (pos->string_pos) >= 0)
3128 {
3129 /* Recorded position is not in an overlay string, but in another
3130 string. This can only be a string from a `display' property.
3131 IT should already be filled with that string. */
3132 it->current.string_pos = pos->string_pos;
3133 eassert (STRINGP (it->string));
3134 if (it->bidi_p)
3135 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3136 FRAME_WINDOW_P (it->f), &it->bidi_it);
3137 }
3138
3139 /* Restore position in display vector translations, control
3140 character translations or ellipses. */
3141 if (pos->dpvec_index >= 0)
3142 {
3143 if (it->dpvec == NULL)
3144 get_next_display_element (it);
3145 eassert (it->dpvec && it->current.dpvec_index == 0);
3146 it->current.dpvec_index = pos->dpvec_index;
3147 }
3148
3149 CHECK_IT (it);
3150 return !overlay_strings_with_newlines;
3151 }
3152
3153
3154 /* Initialize IT for stepping through current_buffer in window W
3155 starting at ROW->start. */
3156
3157 static void
3158 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3159 {
3160 init_from_display_pos (it, w, &row->start);
3161 it->start = row->start;
3162 it->continuation_lines_width = row->continuation_lines_width;
3163 CHECK_IT (it);
3164 }
3165
3166
3167 /* Initialize IT for stepping through current_buffer in window W
3168 starting in the line following ROW, i.e. starting at ROW->end.
3169 Value is zero if there are overlay strings with newlines at ROW's
3170 end position. */
3171
3172 static int
3173 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3174 {
3175 int success = 0;
3176
3177 if (init_from_display_pos (it, w, &row->end))
3178 {
3179 if (row->continued_p)
3180 it->continuation_lines_width
3181 = row->continuation_lines_width + row->pixel_width;
3182 CHECK_IT (it);
3183 success = 1;
3184 }
3185
3186 return success;
3187 }
3188
3189
3190
3191 \f
3192 /***********************************************************************
3193 Text properties
3194 ***********************************************************************/
3195
3196 /* Called when IT reaches IT->stop_charpos. Handle text property and
3197 overlay changes. Set IT->stop_charpos to the next position where
3198 to stop. */
3199
3200 static void
3201 handle_stop (struct it *it)
3202 {
3203 enum prop_handled handled;
3204 int handle_overlay_change_p;
3205 struct props *p;
3206
3207 it->dpvec = NULL;
3208 it->current.dpvec_index = -1;
3209 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3210 it->ignore_overlay_strings_at_pos_p = 0;
3211 it->ellipsis_p = 0;
3212
3213 /* Use face of preceding text for ellipsis (if invisible) */
3214 if (it->selective_display_ellipsis_p)
3215 it->saved_face_id = it->face_id;
3216
3217 do
3218 {
3219 handled = HANDLED_NORMALLY;
3220
3221 /* Call text property handlers. */
3222 for (p = it_props; p->handler; ++p)
3223 {
3224 handled = p->handler (it);
3225
3226 if (handled == HANDLED_RECOMPUTE_PROPS)
3227 break;
3228 else if (handled == HANDLED_RETURN)
3229 {
3230 /* We still want to show before and after strings from
3231 overlays even if the actual buffer text is replaced. */
3232 if (!handle_overlay_change_p
3233 || it->sp > 1
3234 /* Don't call get_overlay_strings_1 if we already
3235 have overlay strings loaded, because doing so
3236 will load them again and push the iterator state
3237 onto the stack one more time, which is not
3238 expected by the rest of the code that processes
3239 overlay strings. */
3240 || (it->current.overlay_string_index < 0
3241 ? !get_overlay_strings_1 (it, 0, 0)
3242 : 0))
3243 {
3244 if (it->ellipsis_p)
3245 setup_for_ellipsis (it, 0);
3246 /* When handling a display spec, we might load an
3247 empty string. In that case, discard it here. We
3248 used to discard it in handle_single_display_spec,
3249 but that causes get_overlay_strings_1, above, to
3250 ignore overlay strings that we must check. */
3251 if (STRINGP (it->string) && !SCHARS (it->string))
3252 pop_it (it);
3253 return;
3254 }
3255 else if (STRINGP (it->string) && !SCHARS (it->string))
3256 pop_it (it);
3257 else
3258 {
3259 it->ignore_overlay_strings_at_pos_p = 1;
3260 it->string_from_display_prop_p = 0;
3261 it->from_disp_prop_p = 0;
3262 handle_overlay_change_p = 0;
3263 }
3264 handled = HANDLED_RECOMPUTE_PROPS;
3265 break;
3266 }
3267 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3268 handle_overlay_change_p = 0;
3269 }
3270
3271 if (handled != HANDLED_RECOMPUTE_PROPS)
3272 {
3273 /* Don't check for overlay strings below when set to deliver
3274 characters from a display vector. */
3275 if (it->method == GET_FROM_DISPLAY_VECTOR)
3276 handle_overlay_change_p = 0;
3277
3278 /* Handle overlay changes.
3279 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3280 if it finds overlays. */
3281 if (handle_overlay_change_p)
3282 handled = handle_overlay_change (it);
3283 }
3284
3285 if (it->ellipsis_p)
3286 {
3287 setup_for_ellipsis (it, 0);
3288 break;
3289 }
3290 }
3291 while (handled == HANDLED_RECOMPUTE_PROPS);
3292
3293 /* Determine where to stop next. */
3294 if (handled == HANDLED_NORMALLY)
3295 compute_stop_pos (it);
3296 }
3297
3298
3299 /* Compute IT->stop_charpos from text property and overlay change
3300 information for IT's current position. */
3301
3302 static void
3303 compute_stop_pos (struct it *it)
3304 {
3305 register INTERVAL iv, next_iv;
3306 Lisp_Object object, limit, position;
3307 ptrdiff_t charpos, bytepos;
3308
3309 if (STRINGP (it->string))
3310 {
3311 /* Strings are usually short, so don't limit the search for
3312 properties. */
3313 it->stop_charpos = it->end_charpos;
3314 object = it->string;
3315 limit = Qnil;
3316 charpos = IT_STRING_CHARPOS (*it);
3317 bytepos = IT_STRING_BYTEPOS (*it);
3318 }
3319 else
3320 {
3321 ptrdiff_t pos;
3322
3323 /* If end_charpos is out of range for some reason, such as a
3324 misbehaving display function, rationalize it (Bug#5984). */
3325 if (it->end_charpos > ZV)
3326 it->end_charpos = ZV;
3327 it->stop_charpos = it->end_charpos;
3328
3329 /* If next overlay change is in front of the current stop pos
3330 (which is IT->end_charpos), stop there. Note: value of
3331 next_overlay_change is point-max if no overlay change
3332 follows. */
3333 charpos = IT_CHARPOS (*it);
3334 bytepos = IT_BYTEPOS (*it);
3335 pos = next_overlay_change (charpos);
3336 if (pos < it->stop_charpos)
3337 it->stop_charpos = pos;
3338
3339 /* If showing the region, we have to stop at the region
3340 start or end because the face might change there. */
3341 if (it->region_beg_charpos > 0)
3342 {
3343 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3344 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3345 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3346 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3347 }
3348
3349 /* Set up variables for computing the stop position from text
3350 property changes. */
3351 XSETBUFFER (object, current_buffer);
3352 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3353 }
3354
3355 /* Get the interval containing IT's position. Value is a null
3356 interval if there isn't such an interval. */
3357 position = make_number (charpos);
3358 iv = validate_interval_range (object, &position, &position, 0);
3359 if (iv)
3360 {
3361 Lisp_Object values_here[LAST_PROP_IDX];
3362 struct props *p;
3363
3364 /* Get properties here. */
3365 for (p = it_props; p->handler; ++p)
3366 values_here[p->idx] = textget (iv->plist, *p->name);
3367
3368 /* Look for an interval following iv that has different
3369 properties. */
3370 for (next_iv = next_interval (iv);
3371 (next_iv
3372 && (NILP (limit)
3373 || XFASTINT (limit) > next_iv->position));
3374 next_iv = next_interval (next_iv))
3375 {
3376 for (p = it_props; p->handler; ++p)
3377 {
3378 Lisp_Object new_value;
3379
3380 new_value = textget (next_iv->plist, *p->name);
3381 if (!EQ (values_here[p->idx], new_value))
3382 break;
3383 }
3384
3385 if (p->handler)
3386 break;
3387 }
3388
3389 if (next_iv)
3390 {
3391 if (INTEGERP (limit)
3392 && next_iv->position >= XFASTINT (limit))
3393 /* No text property change up to limit. */
3394 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3395 else
3396 /* Text properties change in next_iv. */
3397 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3398 }
3399 }
3400
3401 if (it->cmp_it.id < 0)
3402 {
3403 ptrdiff_t stoppos = it->end_charpos;
3404
3405 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3406 stoppos = -1;
3407 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3408 stoppos, it->string);
3409 }
3410
3411 eassert (STRINGP (it->string)
3412 || (it->stop_charpos >= BEGV
3413 && it->stop_charpos >= IT_CHARPOS (*it)));
3414 }
3415
3416
3417 /* Return the position of the next overlay change after POS in
3418 current_buffer. Value is point-max if no overlay change
3419 follows. This is like `next-overlay-change' but doesn't use
3420 xmalloc. */
3421
3422 static ptrdiff_t
3423 next_overlay_change (ptrdiff_t pos)
3424 {
3425 ptrdiff_t i, noverlays;
3426 ptrdiff_t endpos;
3427 Lisp_Object *overlays;
3428
3429 /* Get all overlays at the given position. */
3430 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3431
3432 /* If any of these overlays ends before endpos,
3433 use its ending point instead. */
3434 for (i = 0; i < noverlays; ++i)
3435 {
3436 Lisp_Object oend;
3437 ptrdiff_t oendpos;
3438
3439 oend = OVERLAY_END (overlays[i]);
3440 oendpos = OVERLAY_POSITION (oend);
3441 endpos = min (endpos, oendpos);
3442 }
3443
3444 return endpos;
3445 }
3446
3447 /* How many characters forward to search for a display property or
3448 display string. Searching too far forward makes the bidi display
3449 sluggish, especially in small windows. */
3450 #define MAX_DISP_SCAN 250
3451
3452 /* Return the character position of a display string at or after
3453 position specified by POSITION. If no display string exists at or
3454 after POSITION, return ZV. A display string is either an overlay
3455 with `display' property whose value is a string, or a `display'
3456 text property whose value is a string. STRING is data about the
3457 string to iterate; if STRING->lstring is nil, we are iterating a
3458 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3459 on a GUI frame. DISP_PROP is set to zero if we searched
3460 MAX_DISP_SCAN characters forward without finding any display
3461 strings, non-zero otherwise. It is set to 2 if the display string
3462 uses any kind of `(space ...)' spec that will produce a stretch of
3463 white space in the text area. */
3464 ptrdiff_t
3465 compute_display_string_pos (struct text_pos *position,
3466 struct bidi_string_data *string,
3467 int frame_window_p, int *disp_prop)
3468 {
3469 /* OBJECT = nil means current buffer. */
3470 Lisp_Object object =
3471 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3472 Lisp_Object pos, spec, limpos;
3473 int string_p = (string && (STRINGP (string->lstring) || string->s));
3474 ptrdiff_t eob = string_p ? string->schars : ZV;
3475 ptrdiff_t begb = string_p ? 0 : BEGV;
3476 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3477 ptrdiff_t lim =
3478 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3479 struct text_pos tpos;
3480 int rv = 0;
3481
3482 *disp_prop = 1;
3483
3484 if (charpos >= eob
3485 /* We don't support display properties whose values are strings
3486 that have display string properties. */
3487 || string->from_disp_str
3488 /* C strings cannot have display properties. */
3489 || (string->s && !STRINGP (object)))
3490 {
3491 *disp_prop = 0;
3492 return eob;
3493 }
3494
3495 /* If the character at CHARPOS is where the display string begins,
3496 return CHARPOS. */
3497 pos = make_number (charpos);
3498 if (STRINGP (object))
3499 bufpos = string->bufpos;
3500 else
3501 bufpos = charpos;
3502 tpos = *position;
3503 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3504 && (charpos <= begb
3505 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3506 object),
3507 spec))
3508 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3509 frame_window_p)))
3510 {
3511 if (rv == 2)
3512 *disp_prop = 2;
3513 return charpos;
3514 }
3515
3516 /* Look forward for the first character with a `display' property
3517 that will replace the underlying text when displayed. */
3518 limpos = make_number (lim);
3519 do {
3520 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3521 CHARPOS (tpos) = XFASTINT (pos);
3522 if (CHARPOS (tpos) >= lim)
3523 {
3524 *disp_prop = 0;
3525 break;
3526 }
3527 if (STRINGP (object))
3528 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3529 else
3530 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3531 spec = Fget_char_property (pos, Qdisplay, object);
3532 if (!STRINGP (object))
3533 bufpos = CHARPOS (tpos);
3534 } while (NILP (spec)
3535 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3536 bufpos, frame_window_p)));
3537 if (rv == 2)
3538 *disp_prop = 2;
3539
3540 return CHARPOS (tpos);
3541 }
3542
3543 /* Return the character position of the end of the display string that
3544 started at CHARPOS. If there's no display string at CHARPOS,
3545 return -1. A display string is either an overlay with `display'
3546 property whose value is a string or a `display' text property whose
3547 value is a string. */
3548 ptrdiff_t
3549 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3550 {
3551 /* OBJECT = nil means current buffer. */
3552 Lisp_Object object =
3553 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3554 Lisp_Object pos = make_number (charpos);
3555 ptrdiff_t eob =
3556 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3557
3558 if (charpos >= eob || (string->s && !STRINGP (object)))
3559 return eob;
3560
3561 /* It could happen that the display property or overlay was removed
3562 since we found it in compute_display_string_pos above. One way
3563 this can happen is if JIT font-lock was called (through
3564 handle_fontified_prop), and jit-lock-functions remove text
3565 properties or overlays from the portion of buffer that includes
3566 CHARPOS. Muse mode is known to do that, for example. In this
3567 case, we return -1 to the caller, to signal that no display
3568 string is actually present at CHARPOS. See bidi_fetch_char for
3569 how this is handled.
3570
3571 An alternative would be to never look for display properties past
3572 it->stop_charpos. But neither compute_display_string_pos nor
3573 bidi_fetch_char that calls it know or care where the next
3574 stop_charpos is. */
3575 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3576 return -1;
3577
3578 /* Look forward for the first character where the `display' property
3579 changes. */
3580 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3581
3582 return XFASTINT (pos);
3583 }
3584
3585
3586 \f
3587 /***********************************************************************
3588 Fontification
3589 ***********************************************************************/
3590
3591 /* Handle changes in the `fontified' property of the current buffer by
3592 calling hook functions from Qfontification_functions to fontify
3593 regions of text. */
3594
3595 static enum prop_handled
3596 handle_fontified_prop (struct it *it)
3597 {
3598 Lisp_Object prop, pos;
3599 enum prop_handled handled = HANDLED_NORMALLY;
3600
3601 if (!NILP (Vmemory_full))
3602 return handled;
3603
3604 /* Get the value of the `fontified' property at IT's current buffer
3605 position. (The `fontified' property doesn't have a special
3606 meaning in strings.) If the value is nil, call functions from
3607 Qfontification_functions. */
3608 if (!STRINGP (it->string)
3609 && it->s == NULL
3610 && !NILP (Vfontification_functions)
3611 && !NILP (Vrun_hooks)
3612 && (pos = make_number (IT_CHARPOS (*it)),
3613 prop = Fget_char_property (pos, Qfontified, Qnil),
3614 /* Ignore the special cased nil value always present at EOB since
3615 no amount of fontifying will be able to change it. */
3616 NILP (prop) && IT_CHARPOS (*it) < Z))
3617 {
3618 ptrdiff_t count = SPECPDL_INDEX ();
3619 Lisp_Object val;
3620 struct buffer *obuf = current_buffer;
3621 int begv = BEGV, zv = ZV;
3622 int old_clip_changed = current_buffer->clip_changed;
3623
3624 val = Vfontification_functions;
3625 specbind (Qfontification_functions, Qnil);
3626
3627 eassert (it->end_charpos == ZV);
3628
3629 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3630 safe_call1 (val, pos);
3631 else
3632 {
3633 Lisp_Object fns, fn;
3634 struct gcpro gcpro1, gcpro2;
3635
3636 fns = Qnil;
3637 GCPRO2 (val, fns);
3638
3639 for (; CONSP (val); val = XCDR (val))
3640 {
3641 fn = XCAR (val);
3642
3643 if (EQ (fn, Qt))
3644 {
3645 /* A value of t indicates this hook has a local
3646 binding; it means to run the global binding too.
3647 In a global value, t should not occur. If it
3648 does, we must ignore it to avoid an endless
3649 loop. */
3650 for (fns = Fdefault_value (Qfontification_functions);
3651 CONSP (fns);
3652 fns = XCDR (fns))
3653 {
3654 fn = XCAR (fns);
3655 if (!EQ (fn, Qt))
3656 safe_call1 (fn, pos);
3657 }
3658 }
3659 else
3660 safe_call1 (fn, pos);
3661 }
3662
3663 UNGCPRO;
3664 }
3665
3666 unbind_to (count, Qnil);
3667
3668 /* Fontification functions routinely call `save-restriction'.
3669 Normally, this tags clip_changed, which can confuse redisplay
3670 (see discussion in Bug#6671). Since we don't perform any
3671 special handling of fontification changes in the case where
3672 `save-restriction' isn't called, there's no point doing so in
3673 this case either. So, if the buffer's restrictions are
3674 actually left unchanged, reset clip_changed. */
3675 if (obuf == current_buffer)
3676 {
3677 if (begv == BEGV && zv == ZV)
3678 current_buffer->clip_changed = old_clip_changed;
3679 }
3680 /* There isn't much we can reasonably do to protect against
3681 misbehaving fontification, but here's a fig leaf. */
3682 else if (BUFFER_LIVE_P (obuf))
3683 set_buffer_internal_1 (obuf);
3684
3685 /* The fontification code may have added/removed text.
3686 It could do even a lot worse, but let's at least protect against
3687 the most obvious case where only the text past `pos' gets changed',
3688 as is/was done in grep.el where some escapes sequences are turned
3689 into face properties (bug#7876). */
3690 it->end_charpos = ZV;
3691
3692 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3693 something. This avoids an endless loop if they failed to
3694 fontify the text for which reason ever. */
3695 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3696 handled = HANDLED_RECOMPUTE_PROPS;
3697 }
3698
3699 return handled;
3700 }
3701
3702
3703 \f
3704 /***********************************************************************
3705 Faces
3706 ***********************************************************************/
3707
3708 /* Set up iterator IT from face properties at its current position.
3709 Called from handle_stop. */
3710
3711 static enum prop_handled
3712 handle_face_prop (struct it *it)
3713 {
3714 int new_face_id;
3715 ptrdiff_t next_stop;
3716
3717 if (!STRINGP (it->string))
3718 {
3719 new_face_id
3720 = face_at_buffer_position (it->w,
3721 IT_CHARPOS (*it),
3722 it->region_beg_charpos,
3723 it->region_end_charpos,
3724 &next_stop,
3725 (IT_CHARPOS (*it)
3726 + TEXT_PROP_DISTANCE_LIMIT),
3727 0, it->base_face_id);
3728
3729 /* Is this a start of a run of characters with box face?
3730 Caveat: this can be called for a freshly initialized
3731 iterator; face_id is -1 in this case. We know that the new
3732 face will not change until limit, i.e. if the new face has a
3733 box, all characters up to limit will have one. But, as
3734 usual, we don't know whether limit is really the end. */
3735 if (new_face_id != it->face_id)
3736 {
3737 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3738 /* If it->face_id is -1, old_face below will be NULL, see
3739 the definition of FACE_FROM_ID. This will happen if this
3740 is the initial call that gets the face. */
3741 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3742
3743 /* If the value of face_id of the iterator is -1, we have to
3744 look in front of IT's position and see whether there is a
3745 face there that's different from new_face_id. */
3746 if (!old_face && IT_CHARPOS (*it) > BEG)
3747 {
3748 int prev_face_id = face_before_it_pos (it);
3749
3750 old_face = FACE_FROM_ID (it->f, prev_face_id);
3751 }
3752
3753 /* If the new face has a box, but the old face does not,
3754 this is the start of a run of characters with box face,
3755 i.e. this character has a shadow on the left side. */
3756 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3757 && (old_face == NULL || !old_face->box));
3758 it->face_box_p = new_face->box != FACE_NO_BOX;
3759 }
3760 }
3761 else
3762 {
3763 int base_face_id;
3764 ptrdiff_t bufpos;
3765 int i;
3766 Lisp_Object from_overlay
3767 = (it->current.overlay_string_index >= 0
3768 ? it->string_overlays[it->current.overlay_string_index
3769 % OVERLAY_STRING_CHUNK_SIZE]
3770 : Qnil);
3771
3772 /* See if we got to this string directly or indirectly from
3773 an overlay property. That includes the before-string or
3774 after-string of an overlay, strings in display properties
3775 provided by an overlay, their text properties, etc.
3776
3777 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3778 if (! NILP (from_overlay))
3779 for (i = it->sp - 1; i >= 0; i--)
3780 {
3781 if (it->stack[i].current.overlay_string_index >= 0)
3782 from_overlay
3783 = it->string_overlays[it->stack[i].current.overlay_string_index
3784 % OVERLAY_STRING_CHUNK_SIZE];
3785 else if (! NILP (it->stack[i].from_overlay))
3786 from_overlay = it->stack[i].from_overlay;
3787
3788 if (!NILP (from_overlay))
3789 break;
3790 }
3791
3792 if (! NILP (from_overlay))
3793 {
3794 bufpos = IT_CHARPOS (*it);
3795 /* For a string from an overlay, the base face depends
3796 only on text properties and ignores overlays. */
3797 base_face_id
3798 = face_for_overlay_string (it->w,
3799 IT_CHARPOS (*it),
3800 it->region_beg_charpos,
3801 it->region_end_charpos,
3802 &next_stop,
3803 (IT_CHARPOS (*it)
3804 + TEXT_PROP_DISTANCE_LIMIT),
3805 0,
3806 from_overlay);
3807 }
3808 else
3809 {
3810 bufpos = 0;
3811
3812 /* For strings from a `display' property, use the face at
3813 IT's current buffer position as the base face to merge
3814 with, so that overlay strings appear in the same face as
3815 surrounding text, unless they specify their own
3816 faces. */
3817 base_face_id = it->string_from_prefix_prop_p
3818 ? DEFAULT_FACE_ID
3819 : underlying_face_id (it);
3820 }
3821
3822 new_face_id = face_at_string_position (it->w,
3823 it->string,
3824 IT_STRING_CHARPOS (*it),
3825 bufpos,
3826 it->region_beg_charpos,
3827 it->region_end_charpos,
3828 &next_stop,
3829 base_face_id, 0);
3830
3831 /* Is this a start of a run of characters with box? Caveat:
3832 this can be called for a freshly allocated iterator; face_id
3833 is -1 is this case. We know that the new face will not
3834 change until the next check pos, i.e. if the new face has a
3835 box, all characters up to that position will have a
3836 box. But, as usual, we don't know whether that position
3837 is really the end. */
3838 if (new_face_id != it->face_id)
3839 {
3840 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3841 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3842
3843 /* If new face has a box but old face hasn't, this is the
3844 start of a run of characters with box, i.e. it has a
3845 shadow on the left side. */
3846 it->start_of_box_run_p
3847 = new_face->box && (old_face == NULL || !old_face->box);
3848 it->face_box_p = new_face->box != FACE_NO_BOX;
3849 }
3850 }
3851
3852 it->face_id = new_face_id;
3853 return HANDLED_NORMALLY;
3854 }
3855
3856
3857 /* Return the ID of the face ``underlying'' IT's current position,
3858 which is in a string. If the iterator is associated with a
3859 buffer, return the face at IT's current buffer position.
3860 Otherwise, use the iterator's base_face_id. */
3861
3862 static int
3863 underlying_face_id (struct it *it)
3864 {
3865 int face_id = it->base_face_id, i;
3866
3867 eassert (STRINGP (it->string));
3868
3869 for (i = it->sp - 1; i >= 0; --i)
3870 if (NILP (it->stack[i].string))
3871 face_id = it->stack[i].face_id;
3872
3873 return face_id;
3874 }
3875
3876
3877 /* Compute the face one character before or after the current position
3878 of IT, in the visual order. BEFORE_P non-zero means get the face
3879 in front (to the left in L2R paragraphs, to the right in R2L
3880 paragraphs) of IT's screen position. Value is the ID of the face. */
3881
3882 static int
3883 face_before_or_after_it_pos (struct it *it, int before_p)
3884 {
3885 int face_id, limit;
3886 ptrdiff_t next_check_charpos;
3887 struct it it_copy;
3888 void *it_copy_data = NULL;
3889
3890 eassert (it->s == NULL);
3891
3892 if (STRINGP (it->string))
3893 {
3894 ptrdiff_t bufpos, charpos;
3895 int base_face_id;
3896
3897 /* No face change past the end of the string (for the case
3898 we are padding with spaces). No face change before the
3899 string start. */
3900 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3901 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3902 return it->face_id;
3903
3904 if (!it->bidi_p)
3905 {
3906 /* Set charpos to the position before or after IT's current
3907 position, in the logical order, which in the non-bidi
3908 case is the same as the visual order. */
3909 if (before_p)
3910 charpos = IT_STRING_CHARPOS (*it) - 1;
3911 else if (it->what == IT_COMPOSITION)
3912 /* For composition, we must check the character after the
3913 composition. */
3914 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3915 else
3916 charpos = IT_STRING_CHARPOS (*it) + 1;
3917 }
3918 else
3919 {
3920 if (before_p)
3921 {
3922 /* With bidi iteration, the character before the current
3923 in the visual order cannot be found by simple
3924 iteration, because "reverse" reordering is not
3925 supported. Instead, we need to use the move_it_*
3926 family of functions. */
3927 /* Ignore face changes before the first visible
3928 character on this display line. */
3929 if (it->current_x <= it->first_visible_x)
3930 return it->face_id;
3931 SAVE_IT (it_copy, *it, it_copy_data);
3932 /* Implementation note: Since move_it_in_display_line
3933 works in the iterator geometry, and thinks the first
3934 character is always the leftmost, even in R2L lines,
3935 we don't need to distinguish between the R2L and L2R
3936 cases here. */
3937 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3938 it_copy.current_x - 1, MOVE_TO_X);
3939 charpos = IT_STRING_CHARPOS (it_copy);
3940 RESTORE_IT (it, it, it_copy_data);
3941 }
3942 else
3943 {
3944 /* Set charpos to the string position of the character
3945 that comes after IT's current position in the visual
3946 order. */
3947 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3948
3949 it_copy = *it;
3950 while (n--)
3951 bidi_move_to_visually_next (&it_copy.bidi_it);
3952
3953 charpos = it_copy.bidi_it.charpos;
3954 }
3955 }
3956 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3957
3958 if (it->current.overlay_string_index >= 0)
3959 bufpos = IT_CHARPOS (*it);
3960 else
3961 bufpos = 0;
3962
3963 base_face_id = underlying_face_id (it);
3964
3965 /* Get the face for ASCII, or unibyte. */
3966 face_id = face_at_string_position (it->w,
3967 it->string,
3968 charpos,
3969 bufpos,
3970 it->region_beg_charpos,
3971 it->region_end_charpos,
3972 &next_check_charpos,
3973 base_face_id, 0);
3974
3975 /* Correct the face for charsets different from ASCII. Do it
3976 for the multibyte case only. The face returned above is
3977 suitable for unibyte text if IT->string is unibyte. */
3978 if (STRING_MULTIBYTE (it->string))
3979 {
3980 struct text_pos pos1 = string_pos (charpos, it->string);
3981 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3982 int c, len;
3983 struct face *face = FACE_FROM_ID (it->f, face_id);
3984
3985 c = string_char_and_length (p, &len);
3986 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3987 }
3988 }
3989 else
3990 {
3991 struct text_pos pos;
3992
3993 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3994 || (IT_CHARPOS (*it) <= BEGV && before_p))
3995 return it->face_id;
3996
3997 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3998 pos = it->current.pos;
3999
4000 if (!it->bidi_p)
4001 {
4002 if (before_p)
4003 DEC_TEXT_POS (pos, it->multibyte_p);
4004 else
4005 {
4006 if (it->what == IT_COMPOSITION)
4007 {
4008 /* For composition, we must check the position after
4009 the composition. */
4010 pos.charpos += it->cmp_it.nchars;
4011 pos.bytepos += it->len;
4012 }
4013 else
4014 INC_TEXT_POS (pos, it->multibyte_p);
4015 }
4016 }
4017 else
4018 {
4019 if (before_p)
4020 {
4021 /* With bidi iteration, the character before the current
4022 in the visual order cannot be found by simple
4023 iteration, because "reverse" reordering is not
4024 supported. Instead, we need to use the move_it_*
4025 family of functions. */
4026 /* Ignore face changes before the first visible
4027 character on this display line. */
4028 if (it->current_x <= it->first_visible_x)
4029 return it->face_id;
4030 SAVE_IT (it_copy, *it, it_copy_data);
4031 /* Implementation note: Since move_it_in_display_line
4032 works in the iterator geometry, and thinks the first
4033 character is always the leftmost, even in R2L lines,
4034 we don't need to distinguish between the R2L and L2R
4035 cases here. */
4036 move_it_in_display_line (&it_copy, ZV,
4037 it_copy.current_x - 1, MOVE_TO_X);
4038 pos = it_copy.current.pos;
4039 RESTORE_IT (it, it, it_copy_data);
4040 }
4041 else
4042 {
4043 /* Set charpos to the buffer position of the character
4044 that comes after IT's current position in the visual
4045 order. */
4046 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4047
4048 it_copy = *it;
4049 while (n--)
4050 bidi_move_to_visually_next (&it_copy.bidi_it);
4051
4052 SET_TEXT_POS (pos,
4053 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4054 }
4055 }
4056 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4057
4058 /* Determine face for CHARSET_ASCII, or unibyte. */
4059 face_id = face_at_buffer_position (it->w,
4060 CHARPOS (pos),
4061 it->region_beg_charpos,
4062 it->region_end_charpos,
4063 &next_check_charpos,
4064 limit, 0, -1);
4065
4066 /* Correct the face for charsets different from ASCII. Do it
4067 for the multibyte case only. The face returned above is
4068 suitable for unibyte text if current_buffer is unibyte. */
4069 if (it->multibyte_p)
4070 {
4071 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4072 struct face *face = FACE_FROM_ID (it->f, face_id);
4073 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4074 }
4075 }
4076
4077 return face_id;
4078 }
4079
4080
4081 \f
4082 /***********************************************************************
4083 Invisible text
4084 ***********************************************************************/
4085
4086 /* Set up iterator IT from invisible properties at its current
4087 position. Called from handle_stop. */
4088
4089 static enum prop_handled
4090 handle_invisible_prop (struct it *it)
4091 {
4092 enum prop_handled handled = HANDLED_NORMALLY;
4093 int invis_p;
4094 Lisp_Object prop;
4095
4096 if (STRINGP (it->string))
4097 {
4098 Lisp_Object end_charpos, limit, charpos;
4099
4100 /* Get the value of the invisible text property at the
4101 current position. Value will be nil if there is no such
4102 property. */
4103 charpos = make_number (IT_STRING_CHARPOS (*it));
4104 prop = Fget_text_property (charpos, Qinvisible, it->string);
4105 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4106
4107 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4108 {
4109 /* Record whether we have to display an ellipsis for the
4110 invisible text. */
4111 int display_ellipsis_p = (invis_p == 2);
4112 ptrdiff_t len, endpos;
4113
4114 handled = HANDLED_RECOMPUTE_PROPS;
4115
4116 /* Get the position at which the next visible text can be
4117 found in IT->string, if any. */
4118 endpos = len = SCHARS (it->string);
4119 XSETINT (limit, len);
4120 do
4121 {
4122 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4123 it->string, limit);
4124 if (INTEGERP (end_charpos))
4125 {
4126 endpos = XFASTINT (end_charpos);
4127 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4128 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4129 if (invis_p == 2)
4130 display_ellipsis_p = 1;
4131 }
4132 }
4133 while (invis_p && endpos < len);
4134
4135 if (display_ellipsis_p)
4136 it->ellipsis_p = 1;
4137
4138 if (endpos < len)
4139 {
4140 /* Text at END_CHARPOS is visible. Move IT there. */
4141 struct text_pos old;
4142 ptrdiff_t oldpos;
4143
4144 old = it->current.string_pos;
4145 oldpos = CHARPOS (old);
4146 if (it->bidi_p)
4147 {
4148 if (it->bidi_it.first_elt
4149 && it->bidi_it.charpos < SCHARS (it->string))
4150 bidi_paragraph_init (it->paragraph_embedding,
4151 &it->bidi_it, 1);
4152 /* Bidi-iterate out of the invisible text. */
4153 do
4154 {
4155 bidi_move_to_visually_next (&it->bidi_it);
4156 }
4157 while (oldpos <= it->bidi_it.charpos
4158 && it->bidi_it.charpos < endpos);
4159
4160 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4161 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4162 if (IT_CHARPOS (*it) >= endpos)
4163 it->prev_stop = endpos;
4164 }
4165 else
4166 {
4167 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4168 compute_string_pos (&it->current.string_pos, old, it->string);
4169 }
4170 }
4171 else
4172 {
4173 /* The rest of the string is invisible. If this is an
4174 overlay string, proceed with the next overlay string
4175 or whatever comes and return a character from there. */
4176 if (it->current.overlay_string_index >= 0
4177 && !display_ellipsis_p)
4178 {
4179 next_overlay_string (it);
4180 /* Don't check for overlay strings when we just
4181 finished processing them. */
4182 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4183 }
4184 else
4185 {
4186 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4187 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4188 }
4189 }
4190 }
4191 }
4192 else
4193 {
4194 ptrdiff_t newpos, next_stop, start_charpos, tem;
4195 Lisp_Object pos, overlay;
4196
4197 /* First of all, is there invisible text at this position? */
4198 tem = start_charpos = IT_CHARPOS (*it);
4199 pos = make_number (tem);
4200 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4201 &overlay);
4202 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4203
4204 /* If we are on invisible text, skip over it. */
4205 if (invis_p && start_charpos < it->end_charpos)
4206 {
4207 /* Record whether we have to display an ellipsis for the
4208 invisible text. */
4209 int display_ellipsis_p = invis_p == 2;
4210
4211 handled = HANDLED_RECOMPUTE_PROPS;
4212
4213 /* Loop skipping over invisible text. The loop is left at
4214 ZV or with IT on the first char being visible again. */
4215 do
4216 {
4217 /* Try to skip some invisible text. Return value is the
4218 position reached which can be equal to where we start
4219 if there is nothing invisible there. This skips both
4220 over invisible text properties and overlays with
4221 invisible property. */
4222 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4223
4224 /* If we skipped nothing at all we weren't at invisible
4225 text in the first place. If everything to the end of
4226 the buffer was skipped, end the loop. */
4227 if (newpos == tem || newpos >= ZV)
4228 invis_p = 0;
4229 else
4230 {
4231 /* We skipped some characters but not necessarily
4232 all there are. Check if we ended up on visible
4233 text. Fget_char_property returns the property of
4234 the char before the given position, i.e. if we
4235 get invis_p = 0, this means that the char at
4236 newpos is visible. */
4237 pos = make_number (newpos);
4238 prop = Fget_char_property (pos, Qinvisible, it->window);
4239 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4240 }
4241
4242 /* If we ended up on invisible text, proceed to
4243 skip starting with next_stop. */
4244 if (invis_p)
4245 tem = next_stop;
4246
4247 /* If there are adjacent invisible texts, don't lose the
4248 second one's ellipsis. */
4249 if (invis_p == 2)
4250 display_ellipsis_p = 1;
4251 }
4252 while (invis_p);
4253
4254 /* The position newpos is now either ZV or on visible text. */
4255 if (it->bidi_p)
4256 {
4257 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4258 int on_newline =
4259 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4260 int after_newline =
4261 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4262
4263 /* If the invisible text ends on a newline or on a
4264 character after a newline, we can avoid the costly,
4265 character by character, bidi iteration to NEWPOS, and
4266 instead simply reseat the iterator there. That's
4267 because all bidi reordering information is tossed at
4268 the newline. This is a big win for modes that hide
4269 complete lines, like Outline, Org, etc. */
4270 if (on_newline || after_newline)
4271 {
4272 struct text_pos tpos;
4273 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4274
4275 SET_TEXT_POS (tpos, newpos, bpos);
4276 reseat_1 (it, tpos, 0);
4277 /* If we reseat on a newline/ZV, we need to prep the
4278 bidi iterator for advancing to the next character
4279 after the newline/EOB, keeping the current paragraph
4280 direction (so that PRODUCE_GLYPHS does TRT wrt
4281 prepending/appending glyphs to a glyph row). */
4282 if (on_newline)
4283 {
4284 it->bidi_it.first_elt = 0;
4285 it->bidi_it.paragraph_dir = pdir;
4286 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4287 it->bidi_it.nchars = 1;
4288 it->bidi_it.ch_len = 1;
4289 }
4290 }
4291 else /* Must use the slow method. */
4292 {
4293 /* With bidi iteration, the region of invisible text
4294 could start and/or end in the middle of a
4295 non-base embedding level. Therefore, we need to
4296 skip invisible text using the bidi iterator,
4297 starting at IT's current position, until we find
4298 ourselves outside of the invisible text.
4299 Skipping invisible text _after_ bidi iteration
4300 avoids affecting the visual order of the
4301 displayed text when invisible properties are
4302 added or removed. */
4303 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4304 {
4305 /* If we were `reseat'ed to a new paragraph,
4306 determine the paragraph base direction. We
4307 need to do it now because
4308 next_element_from_buffer may not have a
4309 chance to do it, if we are going to skip any
4310 text at the beginning, which resets the
4311 FIRST_ELT flag. */
4312 bidi_paragraph_init (it->paragraph_embedding,
4313 &it->bidi_it, 1);
4314 }
4315 do
4316 {
4317 bidi_move_to_visually_next (&it->bidi_it);
4318 }
4319 while (it->stop_charpos <= it->bidi_it.charpos
4320 && it->bidi_it.charpos < newpos);
4321 IT_CHARPOS (*it) = it->bidi_it.charpos;
4322 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4323 /* If we overstepped NEWPOS, record its position in
4324 the iterator, so that we skip invisible text if
4325 later the bidi iteration lands us in the
4326 invisible region again. */
4327 if (IT_CHARPOS (*it) >= newpos)
4328 it->prev_stop = newpos;
4329 }
4330 }
4331 else
4332 {
4333 IT_CHARPOS (*it) = newpos;
4334 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4335 }
4336
4337 /* If there are before-strings at the start of invisible
4338 text, and the text is invisible because of a text
4339 property, arrange to show before-strings because 20.x did
4340 it that way. (If the text is invisible because of an
4341 overlay property instead of a text property, this is
4342 already handled in the overlay code.) */
4343 if (NILP (overlay)
4344 && get_overlay_strings (it, it->stop_charpos))
4345 {
4346 handled = HANDLED_RECOMPUTE_PROPS;
4347 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4348 }
4349 else if (display_ellipsis_p)
4350 {
4351 /* Make sure that the glyphs of the ellipsis will get
4352 correct `charpos' values. If we would not update
4353 it->position here, the glyphs would belong to the
4354 last visible character _before_ the invisible
4355 text, which confuses `set_cursor_from_row'.
4356
4357 We use the last invisible position instead of the
4358 first because this way the cursor is always drawn on
4359 the first "." of the ellipsis, whenever PT is inside
4360 the invisible text. Otherwise the cursor would be
4361 placed _after_ the ellipsis when the point is after the
4362 first invisible character. */
4363 if (!STRINGP (it->object))
4364 {
4365 it->position.charpos = newpos - 1;
4366 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4367 }
4368 it->ellipsis_p = 1;
4369 /* Let the ellipsis display before
4370 considering any properties of the following char.
4371 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4372 handled = HANDLED_RETURN;
4373 }
4374 }
4375 }
4376
4377 return handled;
4378 }
4379
4380
4381 /* Make iterator IT return `...' next.
4382 Replaces LEN characters from buffer. */
4383
4384 static void
4385 setup_for_ellipsis (struct it *it, int len)
4386 {
4387 /* Use the display table definition for `...'. Invalid glyphs
4388 will be handled by the method returning elements from dpvec. */
4389 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4390 {
4391 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4392 it->dpvec = v->contents;
4393 it->dpend = v->contents + v->header.size;
4394 }
4395 else
4396 {
4397 /* Default `...'. */
4398 it->dpvec = default_invis_vector;
4399 it->dpend = default_invis_vector + 3;
4400 }
4401
4402 it->dpvec_char_len = len;
4403 it->current.dpvec_index = 0;
4404 it->dpvec_face_id = -1;
4405
4406 /* Remember the current face id in case glyphs specify faces.
4407 IT's face is restored in set_iterator_to_next.
4408 saved_face_id was set to preceding char's face in handle_stop. */
4409 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4410 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4411
4412 it->method = GET_FROM_DISPLAY_VECTOR;
4413 it->ellipsis_p = 1;
4414 }
4415
4416
4417 \f
4418 /***********************************************************************
4419 'display' property
4420 ***********************************************************************/
4421
4422 /* Set up iterator IT from `display' property at its current position.
4423 Called from handle_stop.
4424 We return HANDLED_RETURN if some part of the display property
4425 overrides the display of the buffer text itself.
4426 Otherwise we return HANDLED_NORMALLY. */
4427
4428 static enum prop_handled
4429 handle_display_prop (struct it *it)
4430 {
4431 Lisp_Object propval, object, overlay;
4432 struct text_pos *position;
4433 ptrdiff_t bufpos;
4434 /* Nonzero if some property replaces the display of the text itself. */
4435 int display_replaced_p = 0;
4436
4437 if (STRINGP (it->string))
4438 {
4439 object = it->string;
4440 position = &it->current.string_pos;
4441 bufpos = CHARPOS (it->current.pos);
4442 }
4443 else
4444 {
4445 XSETWINDOW (object, it->w);
4446 position = &it->current.pos;
4447 bufpos = CHARPOS (*position);
4448 }
4449
4450 /* Reset those iterator values set from display property values. */
4451 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4452 it->space_width = Qnil;
4453 it->font_height = Qnil;
4454 it->voffset = 0;
4455
4456 /* We don't support recursive `display' properties, i.e. string
4457 values that have a string `display' property, that have a string
4458 `display' property etc. */
4459 if (!it->string_from_display_prop_p)
4460 it->area = TEXT_AREA;
4461
4462 propval = get_char_property_and_overlay (make_number (position->charpos),
4463 Qdisplay, object, &overlay);
4464 if (NILP (propval))
4465 return HANDLED_NORMALLY;
4466 /* Now OVERLAY is the overlay that gave us this property, or nil
4467 if it was a text property. */
4468
4469 if (!STRINGP (it->string))
4470 object = it->w->buffer;
4471
4472 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4473 position, bufpos,
4474 FRAME_WINDOW_P (it->f));
4475
4476 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4477 }
4478
4479 /* Subroutine of handle_display_prop. Returns non-zero if the display
4480 specification in SPEC is a replacing specification, i.e. it would
4481 replace the text covered by `display' property with something else,
4482 such as an image or a display string. If SPEC includes any kind or
4483 `(space ...) specification, the value is 2; this is used by
4484 compute_display_string_pos, which see.
4485
4486 See handle_single_display_spec for documentation of arguments.
4487 frame_window_p is non-zero if the window being redisplayed is on a
4488 GUI frame; this argument is used only if IT is NULL, see below.
4489
4490 IT can be NULL, if this is called by the bidi reordering code
4491 through compute_display_string_pos, which see. In that case, this
4492 function only examines SPEC, but does not otherwise "handle" it, in
4493 the sense that it doesn't set up members of IT from the display
4494 spec. */
4495 static int
4496 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4497 Lisp_Object overlay, struct text_pos *position,
4498 ptrdiff_t bufpos, int frame_window_p)
4499 {
4500 int replacing_p = 0;
4501 int rv;
4502
4503 if (CONSP (spec)
4504 /* Simple specifications. */
4505 && !EQ (XCAR (spec), Qimage)
4506 #ifdef HAVE_XWIDGETS
4507 && !EQ (XCAR (spec), Qxwidget)
4508 #endif
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->buffer : 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 #ifdef HAVE_XWIDGETS
4949 || XWIDGETP(value)
4950 #endif
4951 );
4952
4953 if (valid_p && !display_replaced_p)
4954 {
4955 int retval = 1;
4956
4957 if (!it)
4958 {
4959 /* Callers need to know whether the display spec is any kind
4960 of `(space ...)' spec that is about to affect text-area
4961 display. */
4962 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4963 retval = 2;
4964 return retval;
4965 }
4966
4967 /* Save current settings of IT so that we can restore them
4968 when we are finished with the glyph property value. */
4969 push_it (it, position);
4970 it->from_overlay = overlay;
4971 it->from_disp_prop_p = 1;
4972
4973 if (NILP (location))
4974 it->area = TEXT_AREA;
4975 else if (EQ (location, Qleft_margin))
4976 it->area = LEFT_MARGIN_AREA;
4977 else
4978 it->area = RIGHT_MARGIN_AREA;
4979
4980 if (STRINGP (value))
4981 {
4982 it->string = value;
4983 it->multibyte_p = STRING_MULTIBYTE (it->string);
4984 it->current.overlay_string_index = -1;
4985 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4986 it->end_charpos = it->string_nchars = SCHARS (it->string);
4987 it->method = GET_FROM_STRING;
4988 it->stop_charpos = 0;
4989 it->prev_stop = 0;
4990 it->base_level_stop = 0;
4991 it->string_from_display_prop_p = 1;
4992 /* Say that we haven't consumed the characters with
4993 `display' property yet. The call to pop_it in
4994 set_iterator_to_next will clean this up. */
4995 if (BUFFERP (object))
4996 *position = start_pos;
4997
4998 /* Force paragraph direction to be that of the parent
4999 object. If the parent object's paragraph direction is
5000 not yet determined, default to L2R. */
5001 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5002 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5003 else
5004 it->paragraph_embedding = L2R;
5005
5006 /* Set up the bidi iterator for this display string. */
5007 if (it->bidi_p)
5008 {
5009 it->bidi_it.string.lstring = it->string;
5010 it->bidi_it.string.s = NULL;
5011 it->bidi_it.string.schars = it->end_charpos;
5012 it->bidi_it.string.bufpos = bufpos;
5013 it->bidi_it.string.from_disp_str = 1;
5014 it->bidi_it.string.unibyte = !it->multibyte_p;
5015 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5016 }
5017 }
5018 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5019 {
5020 it->method = GET_FROM_STRETCH;
5021 it->object = value;
5022 *position = it->position = start_pos;
5023 retval = 1 + (it->area == TEXT_AREA);
5024 }
5025 #ifdef HAVE_XWIDGETS
5026 else if (XWIDGETP(value))
5027 {
5028 //printf("handle_single_display_spec: im an xwidget!!\n");
5029 it->what = IT_XWIDGET;
5030 it->method = GET_FROM_XWIDGET;
5031 it->position = start_pos;
5032 it->object = NILP (object) ? it->w->buffer : object;
5033 *position = start_pos;
5034
5035 it->xwidget = lookup_xwidget(value);
5036 }
5037 #endif
5038 #ifdef HAVE_WINDOW_SYSTEM
5039 else
5040 {
5041 it->what = IT_IMAGE;
5042 it->image_id = lookup_image (it->f, value);
5043 it->position = start_pos;
5044 it->object = NILP (object) ? it->w->buffer : object;
5045 it->method = GET_FROM_IMAGE;
5046
5047 /* Say that we haven't consumed the characters with
5048 `display' property yet. The call to pop_it in
5049 set_iterator_to_next will clean this up. */
5050 *position = start_pos;
5051 }
5052 #endif /* HAVE_WINDOW_SYSTEM */
5053
5054 return retval;
5055 }
5056
5057 /* Invalid property or property not supported. Restore
5058 POSITION to what it was before. */
5059 *position = start_pos;
5060 return 0;
5061 }
5062
5063 /* Check if PROP is a display property value whose text should be
5064 treated as intangible. OVERLAY is the overlay from which PROP
5065 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5066 specify the buffer position covered by PROP. */
5067
5068 int
5069 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5070 ptrdiff_t charpos, ptrdiff_t bytepos)
5071 {
5072 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5073 struct text_pos position;
5074
5075 SET_TEXT_POS (position, charpos, bytepos);
5076 return handle_display_spec (NULL, prop, Qnil, overlay,
5077 &position, charpos, frame_window_p);
5078 }
5079
5080
5081 /* Return 1 if PROP is a display sub-property value containing STRING.
5082
5083 Implementation note: this and the following function are really
5084 special cases of handle_display_spec and
5085 handle_single_display_spec, and should ideally use the same code.
5086 Until they do, these two pairs must be consistent and must be
5087 modified in sync. */
5088
5089 static int
5090 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5091 {
5092 if (EQ (string, prop))
5093 return 1;
5094
5095 /* Skip over `when FORM'. */
5096 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5097 {
5098 prop = XCDR (prop);
5099 if (!CONSP (prop))
5100 return 0;
5101 /* Actually, the condition following `when' should be eval'ed,
5102 like handle_single_display_spec does, and we should return
5103 zero if it evaluates to nil. However, this function is
5104 called only when the buffer was already displayed and some
5105 glyph in the glyph matrix was found to come from a display
5106 string. Therefore, the condition was already evaluated, and
5107 the result was non-nil, otherwise the display string wouldn't
5108 have been displayed and we would have never been called for
5109 this property. Thus, we can skip the evaluation and assume
5110 its result is non-nil. */
5111 prop = XCDR (prop);
5112 }
5113
5114 if (CONSP (prop))
5115 /* Skip over `margin LOCATION'. */
5116 if (EQ (XCAR (prop), Qmargin))
5117 {
5118 prop = XCDR (prop);
5119 if (!CONSP (prop))
5120 return 0;
5121
5122 prop = XCDR (prop);
5123 if (!CONSP (prop))
5124 return 0;
5125 }
5126
5127 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5128 }
5129
5130
5131 /* Return 1 if STRING appears in the `display' property PROP. */
5132
5133 static int
5134 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5135 {
5136 if (CONSP (prop)
5137 && !EQ (XCAR (prop), Qwhen)
5138 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5139 {
5140 /* A list of sub-properties. */
5141 while (CONSP (prop))
5142 {
5143 if (single_display_spec_string_p (XCAR (prop), string))
5144 return 1;
5145 prop = XCDR (prop);
5146 }
5147 }
5148 else if (VECTORP (prop))
5149 {
5150 /* A vector of sub-properties. */
5151 ptrdiff_t i;
5152 for (i = 0; i < ASIZE (prop); ++i)
5153 if (single_display_spec_string_p (AREF (prop, i), string))
5154 return 1;
5155 }
5156 else
5157 return single_display_spec_string_p (prop, string);
5158
5159 return 0;
5160 }
5161
5162 /* Look for STRING in overlays and text properties in the current
5163 buffer, between character positions FROM and TO (excluding TO).
5164 BACK_P non-zero means look back (in this case, TO is supposed to be
5165 less than FROM).
5166 Value is the first character position where STRING was found, or
5167 zero if it wasn't found before hitting TO.
5168
5169 This function may only use code that doesn't eval because it is
5170 called asynchronously from note_mouse_highlight. */
5171
5172 static ptrdiff_t
5173 string_buffer_position_lim (Lisp_Object string,
5174 ptrdiff_t from, ptrdiff_t to, int back_p)
5175 {
5176 Lisp_Object limit, prop, pos;
5177 int found = 0;
5178
5179 pos = make_number (max (from, BEGV));
5180
5181 if (!back_p) /* looking forward */
5182 {
5183 limit = make_number (min (to, ZV));
5184 while (!found && !EQ (pos, limit))
5185 {
5186 prop = Fget_char_property (pos, Qdisplay, Qnil);
5187 if (!NILP (prop) && display_prop_string_p (prop, string))
5188 found = 1;
5189 else
5190 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5191 limit);
5192 }
5193 }
5194 else /* looking back */
5195 {
5196 limit = make_number (max (to, BEGV));
5197 while (!found && !EQ (pos, limit))
5198 {
5199 prop = Fget_char_property (pos, Qdisplay, Qnil);
5200 if (!NILP (prop) && display_prop_string_p (prop, string))
5201 found = 1;
5202 else
5203 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5204 limit);
5205 }
5206 }
5207
5208 return found ? XINT (pos) : 0;
5209 }
5210
5211 /* Determine which buffer position in current buffer STRING comes from.
5212 AROUND_CHARPOS is an approximate position where it could come from.
5213 Value is the buffer position or 0 if it couldn't be determined.
5214
5215 This function is necessary because we don't record buffer positions
5216 in glyphs generated from strings (to keep struct glyph small).
5217 This function may only use code that doesn't eval because it is
5218 called asynchronously from note_mouse_highlight. */
5219
5220 static ptrdiff_t
5221 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5222 {
5223 const int MAX_DISTANCE = 1000;
5224 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5225 around_charpos + MAX_DISTANCE,
5226 0);
5227
5228 if (!found)
5229 found = string_buffer_position_lim (string, around_charpos,
5230 around_charpos - MAX_DISTANCE, 1);
5231 return found;
5232 }
5233
5234
5235 \f
5236 /***********************************************************************
5237 `composition' property
5238 ***********************************************************************/
5239
5240 /* Set up iterator IT from `composition' property at its current
5241 position. Called from handle_stop. */
5242
5243 static enum prop_handled
5244 handle_composition_prop (struct it *it)
5245 {
5246 Lisp_Object prop, string;
5247 ptrdiff_t pos, pos_byte, start, end;
5248
5249 if (STRINGP (it->string))
5250 {
5251 unsigned char *s;
5252
5253 pos = IT_STRING_CHARPOS (*it);
5254 pos_byte = IT_STRING_BYTEPOS (*it);
5255 string = it->string;
5256 s = SDATA (string) + pos_byte;
5257 it->c = STRING_CHAR (s);
5258 }
5259 else
5260 {
5261 pos = IT_CHARPOS (*it);
5262 pos_byte = IT_BYTEPOS (*it);
5263 string = Qnil;
5264 it->c = FETCH_CHAR (pos_byte);
5265 }
5266
5267 /* If there's a valid composition and point is not inside of the
5268 composition (in the case that the composition is from the current
5269 buffer), draw a glyph composed from the composition components. */
5270 if (find_composition (pos, -1, &start, &end, &prop, string)
5271 && COMPOSITION_VALID_P (start, end, prop)
5272 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5273 {
5274 if (start < pos)
5275 /* As we can't handle this situation (perhaps font-lock added
5276 a new composition), we just return here hoping that next
5277 redisplay will detect this composition much earlier. */
5278 return HANDLED_NORMALLY;
5279 if (start != pos)
5280 {
5281 if (STRINGP (it->string))
5282 pos_byte = string_char_to_byte (it->string, start);
5283 else
5284 pos_byte = CHAR_TO_BYTE (start);
5285 }
5286 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5287 prop, string);
5288
5289 if (it->cmp_it.id >= 0)
5290 {
5291 it->cmp_it.ch = -1;
5292 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5293 it->cmp_it.nglyphs = -1;
5294 }
5295 }
5296
5297 return HANDLED_NORMALLY;
5298 }
5299
5300
5301 \f
5302 /***********************************************************************
5303 Overlay strings
5304 ***********************************************************************/
5305
5306 /* The following structure is used to record overlay strings for
5307 later sorting in load_overlay_strings. */
5308
5309 struct overlay_entry
5310 {
5311 Lisp_Object overlay;
5312 Lisp_Object string;
5313 EMACS_INT priority;
5314 int after_string_p;
5315 };
5316
5317
5318 /* Set up iterator IT from overlay strings at its current position.
5319 Called from handle_stop. */
5320
5321 static enum prop_handled
5322 handle_overlay_change (struct it *it)
5323 {
5324 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5325 return HANDLED_RECOMPUTE_PROPS;
5326 else
5327 return HANDLED_NORMALLY;
5328 }
5329
5330
5331 /* Set up the next overlay string for delivery by IT, if there is an
5332 overlay string to deliver. Called by set_iterator_to_next when the
5333 end of the current overlay string is reached. If there are more
5334 overlay strings to display, IT->string and
5335 IT->current.overlay_string_index are set appropriately here.
5336 Otherwise IT->string is set to nil. */
5337
5338 static void
5339 next_overlay_string (struct it *it)
5340 {
5341 ++it->current.overlay_string_index;
5342 if (it->current.overlay_string_index == it->n_overlay_strings)
5343 {
5344 /* No more overlay strings. Restore IT's settings to what
5345 they were before overlay strings were processed, and
5346 continue to deliver from current_buffer. */
5347
5348 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5349 pop_it (it);
5350 eassert (it->sp > 0
5351 || (NILP (it->string)
5352 && it->method == GET_FROM_BUFFER
5353 && it->stop_charpos >= BEGV
5354 && it->stop_charpos <= it->end_charpos));
5355 it->current.overlay_string_index = -1;
5356 it->n_overlay_strings = 0;
5357 it->overlay_strings_charpos = -1;
5358 /* If there's an empty display string on the stack, pop the
5359 stack, to resync the bidi iterator with IT's position. Such
5360 empty strings are pushed onto the stack in
5361 get_overlay_strings_1. */
5362 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5363 pop_it (it);
5364
5365 /* If we're at the end of the buffer, record that we have
5366 processed the overlay strings there already, so that
5367 next_element_from_buffer doesn't try it again. */
5368 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5369 it->overlay_strings_at_end_processed_p = 1;
5370 }
5371 else
5372 {
5373 /* There are more overlay strings to process. If
5374 IT->current.overlay_string_index has advanced to a position
5375 where we must load IT->overlay_strings with more strings, do
5376 it. We must load at the IT->overlay_strings_charpos where
5377 IT->n_overlay_strings was originally computed; when invisible
5378 text is present, this might not be IT_CHARPOS (Bug#7016). */
5379 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5380
5381 if (it->current.overlay_string_index && i == 0)
5382 load_overlay_strings (it, it->overlay_strings_charpos);
5383
5384 /* Initialize IT to deliver display elements from the overlay
5385 string. */
5386 it->string = it->overlay_strings[i];
5387 it->multibyte_p = STRING_MULTIBYTE (it->string);
5388 SET_TEXT_POS (it->current.string_pos, 0, 0);
5389 it->method = GET_FROM_STRING;
5390 it->stop_charpos = 0;
5391 it->end_charpos = SCHARS (it->string);
5392 if (it->cmp_it.stop_pos >= 0)
5393 it->cmp_it.stop_pos = 0;
5394 it->prev_stop = 0;
5395 it->base_level_stop = 0;
5396
5397 /* Set up the bidi iterator for this overlay string. */
5398 if (it->bidi_p)
5399 {
5400 it->bidi_it.string.lstring = it->string;
5401 it->bidi_it.string.s = NULL;
5402 it->bidi_it.string.schars = SCHARS (it->string);
5403 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5404 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5405 it->bidi_it.string.unibyte = !it->multibyte_p;
5406 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5407 }
5408 }
5409
5410 CHECK_IT (it);
5411 }
5412
5413
5414 /* Compare two overlay_entry structures E1 and E2. Used as a
5415 comparison function for qsort in load_overlay_strings. Overlay
5416 strings for the same position are sorted so that
5417
5418 1. All after-strings come in front of before-strings, except
5419 when they come from the same overlay.
5420
5421 2. Within after-strings, strings are sorted so that overlay strings
5422 from overlays with higher priorities come first.
5423
5424 2. Within before-strings, strings are sorted so that overlay
5425 strings from overlays with higher priorities come last.
5426
5427 Value is analogous to strcmp. */
5428
5429
5430 static int
5431 compare_overlay_entries (const void *e1, const void *e2)
5432 {
5433 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5434 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5435 int result;
5436
5437 if (entry1->after_string_p != entry2->after_string_p)
5438 {
5439 /* Let after-strings appear in front of before-strings if
5440 they come from different overlays. */
5441 if (EQ (entry1->overlay, entry2->overlay))
5442 result = entry1->after_string_p ? 1 : -1;
5443 else
5444 result = entry1->after_string_p ? -1 : 1;
5445 }
5446 else if (entry1->priority != entry2->priority)
5447 {
5448 if (entry1->after_string_p)
5449 /* After-strings sorted in order of decreasing priority. */
5450 result = entry2->priority < entry1->priority ? -1 : 1;
5451 else
5452 /* Before-strings sorted in order of increasing priority. */
5453 result = entry1->priority < entry2->priority ? -1 : 1;
5454 }
5455 else
5456 result = 0;
5457
5458 return result;
5459 }
5460
5461
5462 /* Load the vector IT->overlay_strings with overlay strings from IT's
5463 current buffer position, or from CHARPOS if that is > 0. Set
5464 IT->n_overlays to the total number of overlay strings found.
5465
5466 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5467 a time. On entry into load_overlay_strings,
5468 IT->current.overlay_string_index gives the number of overlay
5469 strings that have already been loaded by previous calls to this
5470 function.
5471
5472 IT->add_overlay_start contains an additional overlay start
5473 position to consider for taking overlay strings from, if non-zero.
5474 This position comes into play when the overlay has an `invisible'
5475 property, and both before and after-strings. When we've skipped to
5476 the end of the overlay, because of its `invisible' property, we
5477 nevertheless want its before-string to appear.
5478 IT->add_overlay_start will contain the overlay start position
5479 in this case.
5480
5481 Overlay strings are sorted so that after-string strings come in
5482 front of before-string strings. Within before and after-strings,
5483 strings are sorted by overlay priority. See also function
5484 compare_overlay_entries. */
5485
5486 static void
5487 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5488 {
5489 Lisp_Object overlay, window, str, invisible;
5490 struct Lisp_Overlay *ov;
5491 ptrdiff_t start, end;
5492 ptrdiff_t size = 20;
5493 ptrdiff_t n = 0, i, j;
5494 int invis_p;
5495 struct overlay_entry *entries = alloca (size * sizeof *entries);
5496 USE_SAFE_ALLOCA;
5497
5498 if (charpos <= 0)
5499 charpos = IT_CHARPOS (*it);
5500
5501 /* Append the overlay string STRING of overlay OVERLAY to vector
5502 `entries' which has size `size' and currently contains `n'
5503 elements. AFTER_P non-zero means STRING is an after-string of
5504 OVERLAY. */
5505 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5506 do \
5507 { \
5508 Lisp_Object priority; \
5509 \
5510 if (n == size) \
5511 { \
5512 struct overlay_entry *old = entries; \
5513 SAFE_NALLOCA (entries, 2, size); \
5514 memcpy (entries, old, size * sizeof *entries); \
5515 size *= 2; \
5516 } \
5517 \
5518 entries[n].string = (STRING); \
5519 entries[n].overlay = (OVERLAY); \
5520 priority = Foverlay_get ((OVERLAY), Qpriority); \
5521 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5522 entries[n].after_string_p = (AFTER_P); \
5523 ++n; \
5524 } \
5525 while (0)
5526
5527 /* Process overlay before the overlay center. */
5528 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5529 {
5530 XSETMISC (overlay, ov);
5531 eassert (OVERLAYP (overlay));
5532 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5533 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5534
5535 if (end < charpos)
5536 break;
5537
5538 /* Skip this overlay if it doesn't start or end at IT's current
5539 position. */
5540 if (end != charpos && start != charpos)
5541 continue;
5542
5543 /* Skip this overlay if it doesn't apply to IT->w. */
5544 window = Foverlay_get (overlay, Qwindow);
5545 if (WINDOWP (window) && XWINDOW (window) != it->w)
5546 continue;
5547
5548 /* If the text ``under'' the overlay is invisible, both before-
5549 and after-strings from this overlay are visible; start and
5550 end position are indistinguishable. */
5551 invisible = Foverlay_get (overlay, Qinvisible);
5552 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5553
5554 /* If overlay has a non-empty before-string, record it. */
5555 if ((start == charpos || (end == charpos && invis_p))
5556 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5557 && SCHARS (str))
5558 RECORD_OVERLAY_STRING (overlay, str, 0);
5559
5560 /* If overlay has a non-empty after-string, record it. */
5561 if ((end == charpos || (start == charpos && invis_p))
5562 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5563 && SCHARS (str))
5564 RECORD_OVERLAY_STRING (overlay, str, 1);
5565 }
5566
5567 /* Process overlays after the overlay center. */
5568 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5569 {
5570 XSETMISC (overlay, ov);
5571 eassert (OVERLAYP (overlay));
5572 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5573 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5574
5575 if (start > charpos)
5576 break;
5577
5578 /* Skip this overlay if it doesn't start or end at IT's current
5579 position. */
5580 if (end != charpos && start != charpos)
5581 continue;
5582
5583 /* Skip this overlay if it doesn't apply to IT->w. */
5584 window = Foverlay_get (overlay, Qwindow);
5585 if (WINDOWP (window) && XWINDOW (window) != it->w)
5586 continue;
5587
5588 /* If the text ``under'' the overlay is invisible, it has a zero
5589 dimension, and both before- and after-strings apply. */
5590 invisible = Foverlay_get (overlay, Qinvisible);
5591 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5592
5593 /* If overlay has a non-empty before-string, record it. */
5594 if ((start == charpos || (end == charpos && invis_p))
5595 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5596 && SCHARS (str))
5597 RECORD_OVERLAY_STRING (overlay, str, 0);
5598
5599 /* If overlay has a non-empty after-string, record it. */
5600 if ((end == charpos || (start == charpos && invis_p))
5601 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5602 && SCHARS (str))
5603 RECORD_OVERLAY_STRING (overlay, str, 1);
5604 }
5605
5606 #undef RECORD_OVERLAY_STRING
5607
5608 /* Sort entries. */
5609 if (n > 1)
5610 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5611
5612 /* Record number of overlay strings, and where we computed it. */
5613 it->n_overlay_strings = n;
5614 it->overlay_strings_charpos = charpos;
5615
5616 /* IT->current.overlay_string_index is the number of overlay strings
5617 that have already been consumed by IT. Copy some of the
5618 remaining overlay strings to IT->overlay_strings. */
5619 i = 0;
5620 j = it->current.overlay_string_index;
5621 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5622 {
5623 it->overlay_strings[i] = entries[j].string;
5624 it->string_overlays[i++] = entries[j++].overlay;
5625 }
5626
5627 CHECK_IT (it);
5628 SAFE_FREE ();
5629 }
5630
5631
5632 /* Get the first chunk of overlay strings at IT's current buffer
5633 position, or at CHARPOS if that is > 0. Value is non-zero if at
5634 least one overlay string was found. */
5635
5636 static int
5637 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5638 {
5639 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5640 process. This fills IT->overlay_strings with strings, and sets
5641 IT->n_overlay_strings to the total number of strings to process.
5642 IT->pos.overlay_string_index has to be set temporarily to zero
5643 because load_overlay_strings needs this; it must be set to -1
5644 when no overlay strings are found because a zero value would
5645 indicate a position in the first overlay string. */
5646 it->current.overlay_string_index = 0;
5647 load_overlay_strings (it, charpos);
5648
5649 /* If we found overlay strings, set up IT to deliver display
5650 elements from the first one. Otherwise set up IT to deliver
5651 from current_buffer. */
5652 if (it->n_overlay_strings)
5653 {
5654 /* Make sure we know settings in current_buffer, so that we can
5655 restore meaningful values when we're done with the overlay
5656 strings. */
5657 if (compute_stop_p)
5658 compute_stop_pos (it);
5659 eassert (it->face_id >= 0);
5660
5661 /* Save IT's settings. They are restored after all overlay
5662 strings have been processed. */
5663 eassert (!compute_stop_p || it->sp == 0);
5664
5665 /* When called from handle_stop, there might be an empty display
5666 string loaded. In that case, don't bother saving it. But
5667 don't use this optimization with the bidi iterator, since we
5668 need the corresponding pop_it call to resync the bidi
5669 iterator's position with IT's position, after we are done
5670 with the overlay strings. (The corresponding call to pop_it
5671 in case of an empty display string is in
5672 next_overlay_string.) */
5673 if (!(!it->bidi_p
5674 && STRINGP (it->string) && !SCHARS (it->string)))
5675 push_it (it, NULL);
5676
5677 /* Set up IT to deliver display elements from the first overlay
5678 string. */
5679 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5680 it->string = it->overlay_strings[0];
5681 it->from_overlay = Qnil;
5682 it->stop_charpos = 0;
5683 eassert (STRINGP (it->string));
5684 it->end_charpos = SCHARS (it->string);
5685 it->prev_stop = 0;
5686 it->base_level_stop = 0;
5687 it->multibyte_p = STRING_MULTIBYTE (it->string);
5688 it->method = GET_FROM_STRING;
5689 it->from_disp_prop_p = 0;
5690
5691 /* Force paragraph direction to be that of the parent
5692 buffer. */
5693 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5694 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5695 else
5696 it->paragraph_embedding = L2R;
5697
5698 /* Set up the bidi iterator for this overlay string. */
5699 if (it->bidi_p)
5700 {
5701 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5702
5703 it->bidi_it.string.lstring = it->string;
5704 it->bidi_it.string.s = NULL;
5705 it->bidi_it.string.schars = SCHARS (it->string);
5706 it->bidi_it.string.bufpos = pos;
5707 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5708 it->bidi_it.string.unibyte = !it->multibyte_p;
5709 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5710 }
5711 return 1;
5712 }
5713
5714 it->current.overlay_string_index = -1;
5715 return 0;
5716 }
5717
5718 static int
5719 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5720 {
5721 it->string = Qnil;
5722 it->method = GET_FROM_BUFFER;
5723
5724 (void) get_overlay_strings_1 (it, charpos, 1);
5725
5726 CHECK_IT (it);
5727
5728 /* Value is non-zero if we found at least one overlay string. */
5729 return STRINGP (it->string);
5730 }
5731
5732
5733 \f
5734 /***********************************************************************
5735 Saving and restoring state
5736 ***********************************************************************/
5737
5738 /* Save current settings of IT on IT->stack. Called, for example,
5739 before setting up IT for an overlay string, to be able to restore
5740 IT's settings to what they were after the overlay string has been
5741 processed. If POSITION is non-NULL, it is the position to save on
5742 the stack instead of IT->position. */
5743
5744 static void
5745 push_it (struct it *it, struct text_pos *position)
5746 {
5747 struct iterator_stack_entry *p;
5748
5749 eassert (it->sp < IT_STACK_SIZE);
5750 p = it->stack + it->sp;
5751
5752 p->stop_charpos = it->stop_charpos;
5753 p->prev_stop = it->prev_stop;
5754 p->base_level_stop = it->base_level_stop;
5755 p->cmp_it = it->cmp_it;
5756 eassert (it->face_id >= 0);
5757 p->face_id = it->face_id;
5758 p->string = it->string;
5759 p->method = it->method;
5760 p->from_overlay = it->from_overlay;
5761 switch (p->method)
5762 {
5763 case GET_FROM_IMAGE:
5764 p->u.image.object = it->object;
5765 p->u.image.image_id = it->image_id;
5766 p->u.image.slice = it->slice;
5767 break;
5768 case GET_FROM_STRETCH:
5769 p->u.stretch.object = it->object;
5770 break;
5771 #ifdef HAVE_XWIDGETS
5772 case GET_FROM_XWIDGET:
5773 p->u.xwidget.object = it->object;
5774 break;
5775 #endif
5776 }
5777 p->position = position ? *position : it->position;
5778 p->current = it->current;
5779 p->end_charpos = it->end_charpos;
5780 p->string_nchars = it->string_nchars;
5781 p->area = it->area;
5782 p->multibyte_p = it->multibyte_p;
5783 p->avoid_cursor_p = it->avoid_cursor_p;
5784 p->space_width = it->space_width;
5785 p->font_height = it->font_height;
5786 p->voffset = it->voffset;
5787 p->string_from_display_prop_p = it->string_from_display_prop_p;
5788 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5789 p->display_ellipsis_p = 0;
5790 p->line_wrap = it->line_wrap;
5791 p->bidi_p = it->bidi_p;
5792 p->paragraph_embedding = it->paragraph_embedding;
5793 p->from_disp_prop_p = it->from_disp_prop_p;
5794 ++it->sp;
5795
5796 /* Save the state of the bidi iterator as well. */
5797 if (it->bidi_p)
5798 bidi_push_it (&it->bidi_it);
5799 }
5800
5801 static void
5802 iterate_out_of_display_property (struct it *it)
5803 {
5804 int buffer_p = !STRINGP (it->string);
5805 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5806 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5807
5808 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5809
5810 /* Maybe initialize paragraph direction. If we are at the beginning
5811 of a new paragraph, next_element_from_buffer may not have a
5812 chance to do that. */
5813 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5814 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5815 /* prev_stop can be zero, so check against BEGV as well. */
5816 while (it->bidi_it.charpos >= bob
5817 && it->prev_stop <= it->bidi_it.charpos
5818 && it->bidi_it.charpos < CHARPOS (it->position)
5819 && it->bidi_it.charpos < eob)
5820 bidi_move_to_visually_next (&it->bidi_it);
5821 /* Record the stop_pos we just crossed, for when we cross it
5822 back, maybe. */
5823 if (it->bidi_it.charpos > CHARPOS (it->position))
5824 it->prev_stop = CHARPOS (it->position);
5825 /* If we ended up not where pop_it put us, resync IT's
5826 positional members with the bidi iterator. */
5827 if (it->bidi_it.charpos != CHARPOS (it->position))
5828 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5829 if (buffer_p)
5830 it->current.pos = it->position;
5831 else
5832 it->current.string_pos = it->position;
5833 }
5834
5835 /* Restore IT's settings from IT->stack. Called, for example, when no
5836 more overlay strings must be processed, and we return to delivering
5837 display elements from a buffer, or when the end of a string from a
5838 `display' property is reached and we return to delivering display
5839 elements from an overlay string, or from a buffer. */
5840
5841 static void
5842 pop_it (struct it *it)
5843 {
5844 struct iterator_stack_entry *p;
5845 int from_display_prop = it->from_disp_prop_p;
5846
5847 eassert (it->sp > 0);
5848 --it->sp;
5849 p = it->stack + it->sp;
5850 it->stop_charpos = p->stop_charpos;
5851 it->prev_stop = p->prev_stop;
5852 it->base_level_stop = p->base_level_stop;
5853 it->cmp_it = p->cmp_it;
5854 it->face_id = p->face_id;
5855 it->current = p->current;
5856 it->position = p->position;
5857 it->string = p->string;
5858 it->from_overlay = p->from_overlay;
5859 if (NILP (it->string))
5860 SET_TEXT_POS (it->current.string_pos, -1, -1);
5861 it->method = p->method;
5862 switch (it->method)
5863 {
5864 case GET_FROM_IMAGE:
5865 it->image_id = p->u.image.image_id;
5866 it->object = p->u.image.object;
5867 it->slice = p->u.image.slice;
5868 break;
5869 #ifdef HAVE_XWIDGETS
5870 case GET_FROM_XWIDGET:
5871 it->object = p->u.xwidget.object;
5872 break;
5873 #endif
5874 case GET_FROM_STRETCH:
5875 it->object = p->u.stretch.object;
5876 break;
5877 case GET_FROM_BUFFER:
5878 it->object = it->w->buffer;
5879 break;
5880 case GET_FROM_STRING:
5881 it->object = it->string;
5882 break;
5883 case GET_FROM_DISPLAY_VECTOR:
5884 if (it->s)
5885 it->method = GET_FROM_C_STRING;
5886 else if (STRINGP (it->string))
5887 it->method = GET_FROM_STRING;
5888 else
5889 {
5890 it->method = GET_FROM_BUFFER;
5891 it->object = it->w->buffer;
5892 }
5893 }
5894 it->end_charpos = p->end_charpos;
5895 it->string_nchars = p->string_nchars;
5896 it->area = p->area;
5897 it->multibyte_p = p->multibyte_p;
5898 it->avoid_cursor_p = p->avoid_cursor_p;
5899 it->space_width = p->space_width;
5900 it->font_height = p->font_height;
5901 it->voffset = p->voffset;
5902 it->string_from_display_prop_p = p->string_from_display_prop_p;
5903 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5904 it->line_wrap = p->line_wrap;
5905 it->bidi_p = p->bidi_p;
5906 it->paragraph_embedding = p->paragraph_embedding;
5907 it->from_disp_prop_p = p->from_disp_prop_p;
5908 if (it->bidi_p)
5909 {
5910 bidi_pop_it (&it->bidi_it);
5911 /* Bidi-iterate until we get out of the portion of text, if any,
5912 covered by a `display' text property or by an overlay with
5913 `display' property. (We cannot just jump there, because the
5914 internal coherency of the bidi iterator state can not be
5915 preserved across such jumps.) We also must determine the
5916 paragraph base direction if the overlay we just processed is
5917 at the beginning of a new paragraph. */
5918 if (from_display_prop
5919 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5920 iterate_out_of_display_property (it);
5921
5922 eassert ((BUFFERP (it->object)
5923 && IT_CHARPOS (*it) == it->bidi_it.charpos
5924 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5925 || (STRINGP (it->object)
5926 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5927 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5928 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5929 }
5930 }
5931
5932
5933 \f
5934 /***********************************************************************
5935 Moving over lines
5936 ***********************************************************************/
5937
5938 /* Set IT's current position to the previous line start. */
5939
5940 static void
5941 back_to_previous_line_start (struct it *it)
5942 {
5943 IT_CHARPOS (*it)
5944 = find_next_newline_no_quit (IT_CHARPOS (*it) - 1,
5945 -1, &IT_BYTEPOS (*it));
5946 }
5947
5948
5949 /* Move IT to the next line start.
5950
5951 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5952 we skipped over part of the text (as opposed to moving the iterator
5953 continuously over the text). Otherwise, don't change the value
5954 of *SKIPPED_P.
5955
5956 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5957 iterator on the newline, if it was found.
5958
5959 Newlines may come from buffer text, overlay strings, or strings
5960 displayed via the `display' property. That's the reason we can't
5961 simply use find_next_newline_no_quit.
5962
5963 Note that this function may not skip over invisible text that is so
5964 because of text properties and immediately follows a newline. If
5965 it would, function reseat_at_next_visible_line_start, when called
5966 from set_iterator_to_next, would effectively make invisible
5967 characters following a newline part of the wrong glyph row, which
5968 leads to wrong cursor motion. */
5969
5970 static int
5971 forward_to_next_line_start (struct it *it, int *skipped_p,
5972 struct bidi_it *bidi_it_prev)
5973 {
5974 ptrdiff_t old_selective;
5975 int newline_found_p, n;
5976 const int MAX_NEWLINE_DISTANCE = 500;
5977
5978 /* If already on a newline, just consume it to avoid unintended
5979 skipping over invisible text below. */
5980 if (it->what == IT_CHARACTER
5981 && it->c == '\n'
5982 && CHARPOS (it->position) == IT_CHARPOS (*it))
5983 {
5984 if (it->bidi_p && bidi_it_prev)
5985 *bidi_it_prev = it->bidi_it;
5986 set_iterator_to_next (it, 0);
5987 it->c = 0;
5988 return 1;
5989 }
5990
5991 /* Don't handle selective display in the following. It's (a)
5992 unnecessary because it's done by the caller, and (b) leads to an
5993 infinite recursion because next_element_from_ellipsis indirectly
5994 calls this function. */
5995 old_selective = it->selective;
5996 it->selective = 0;
5997
5998 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5999 from buffer text. */
6000 for (n = newline_found_p = 0;
6001 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6002 n += STRINGP (it->string) ? 0 : 1)
6003 {
6004 if (!get_next_display_element (it))
6005 return 0;
6006 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6007 if (newline_found_p && it->bidi_p && bidi_it_prev)
6008 *bidi_it_prev = it->bidi_it;
6009 set_iterator_to_next (it, 0);
6010 }
6011
6012 /* If we didn't find a newline near enough, see if we can use a
6013 short-cut. */
6014 if (!newline_found_p)
6015 {
6016 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6017 ptrdiff_t limit = find_next_newline_no_quit (start, 1, &bytepos);
6018 Lisp_Object pos;
6019
6020 eassert (!STRINGP (it->string));
6021
6022 /* If there isn't any `display' property in sight, and no
6023 overlays, we can just use the position of the newline in
6024 buffer text. */
6025 if (it->stop_charpos >= limit
6026 || ((pos = Fnext_single_property_change (make_number (start),
6027 Qdisplay, Qnil,
6028 make_number (limit)),
6029 NILP (pos))
6030 && next_overlay_change (start) == ZV))
6031 {
6032 if (!it->bidi_p)
6033 {
6034 IT_CHARPOS (*it) = limit;
6035 IT_BYTEPOS (*it) = bytepos;
6036 }
6037 else
6038 {
6039 struct bidi_it bprev;
6040
6041 /* Help bidi.c avoid expensive searches for display
6042 properties and overlays, by telling it that there are
6043 none up to `limit'. */
6044 if (it->bidi_it.disp_pos < limit)
6045 {
6046 it->bidi_it.disp_pos = limit;
6047 it->bidi_it.disp_prop = 0;
6048 }
6049 do {
6050 bprev = it->bidi_it;
6051 bidi_move_to_visually_next (&it->bidi_it);
6052 } while (it->bidi_it.charpos != limit);
6053 IT_CHARPOS (*it) = limit;
6054 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6055 if (bidi_it_prev)
6056 *bidi_it_prev = bprev;
6057 }
6058 *skipped_p = newline_found_p = 1;
6059 }
6060 else
6061 {
6062 while (get_next_display_element (it)
6063 && !newline_found_p)
6064 {
6065 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6066 if (newline_found_p && it->bidi_p && bidi_it_prev)
6067 *bidi_it_prev = it->bidi_it;
6068 set_iterator_to_next (it, 0);
6069 }
6070 }
6071 }
6072
6073 it->selective = old_selective;
6074 return newline_found_p;
6075 }
6076
6077
6078 /* Set IT's current position to the previous visible line start. Skip
6079 invisible text that is so either due to text properties or due to
6080 selective display. Caution: this does not change IT->current_x and
6081 IT->hpos. */
6082
6083 static void
6084 back_to_previous_visible_line_start (struct it *it)
6085 {
6086 while (IT_CHARPOS (*it) > BEGV)
6087 {
6088 back_to_previous_line_start (it);
6089
6090 if (IT_CHARPOS (*it) <= BEGV)
6091 break;
6092
6093 /* If selective > 0, then lines indented more than its value are
6094 invisible. */
6095 if (it->selective > 0
6096 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6097 it->selective))
6098 continue;
6099
6100 /* Check the newline before point for invisibility. */
6101 {
6102 Lisp_Object prop;
6103 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6104 Qinvisible, it->window);
6105 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6106 continue;
6107 }
6108
6109 if (IT_CHARPOS (*it) <= BEGV)
6110 break;
6111
6112 {
6113 struct it it2;
6114 void *it2data = NULL;
6115 ptrdiff_t pos;
6116 ptrdiff_t beg, end;
6117 Lisp_Object val, overlay;
6118
6119 SAVE_IT (it2, *it, it2data);
6120
6121 /* If newline is part of a composition, continue from start of composition */
6122 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6123 && beg < IT_CHARPOS (*it))
6124 goto replaced;
6125
6126 /* If newline is replaced by a display property, find start of overlay
6127 or interval and continue search from that point. */
6128 pos = --IT_CHARPOS (it2);
6129 --IT_BYTEPOS (it2);
6130 it2.sp = 0;
6131 bidi_unshelve_cache (NULL, 0);
6132 it2.string_from_display_prop_p = 0;
6133 it2.from_disp_prop_p = 0;
6134 if (handle_display_prop (&it2) == HANDLED_RETURN
6135 && !NILP (val = get_char_property_and_overlay
6136 (make_number (pos), Qdisplay, Qnil, &overlay))
6137 && (OVERLAYP (overlay)
6138 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6139 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6140 {
6141 RESTORE_IT (it, it, it2data);
6142 goto replaced;
6143 }
6144
6145 /* Newline is not replaced by anything -- so we are done. */
6146 RESTORE_IT (it, it, it2data);
6147 break;
6148
6149 replaced:
6150 if (beg < BEGV)
6151 beg = BEGV;
6152 IT_CHARPOS (*it) = beg;
6153 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6154 }
6155 }
6156
6157 it->continuation_lines_width = 0;
6158
6159 eassert (IT_CHARPOS (*it) >= BEGV);
6160 eassert (IT_CHARPOS (*it) == BEGV
6161 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6162 CHECK_IT (it);
6163 }
6164
6165
6166 /* Reseat iterator IT at the previous visible line start. Skip
6167 invisible text that is so either due to text properties or due to
6168 selective display. At the end, update IT's overlay information,
6169 face information etc. */
6170
6171 void
6172 reseat_at_previous_visible_line_start (struct it *it)
6173 {
6174 back_to_previous_visible_line_start (it);
6175 reseat (it, it->current.pos, 1);
6176 CHECK_IT (it);
6177 }
6178
6179
6180 /* Reseat iterator IT on the next visible line start in the current
6181 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6182 preceding the line start. Skip over invisible text that is so
6183 because of selective display. Compute faces, overlays etc at the
6184 new position. Note that this function does not skip over text that
6185 is invisible because of text properties. */
6186
6187 static void
6188 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6189 {
6190 int newline_found_p, skipped_p = 0;
6191 struct bidi_it bidi_it_prev;
6192
6193 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6194
6195 /* Skip over lines that are invisible because they are indented
6196 more than the value of IT->selective. */
6197 if (it->selective > 0)
6198 while (IT_CHARPOS (*it) < ZV
6199 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6200 it->selective))
6201 {
6202 eassert (IT_BYTEPOS (*it) == BEGV
6203 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6204 newline_found_p =
6205 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6206 }
6207
6208 /* Position on the newline if that's what's requested. */
6209 if (on_newline_p && newline_found_p)
6210 {
6211 if (STRINGP (it->string))
6212 {
6213 if (IT_STRING_CHARPOS (*it) > 0)
6214 {
6215 if (!it->bidi_p)
6216 {
6217 --IT_STRING_CHARPOS (*it);
6218 --IT_STRING_BYTEPOS (*it);
6219 }
6220 else
6221 {
6222 /* We need to restore the bidi iterator to the state
6223 it had on the newline, and resync the IT's
6224 position with that. */
6225 it->bidi_it = bidi_it_prev;
6226 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6227 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6228 }
6229 }
6230 }
6231 else if (IT_CHARPOS (*it) > BEGV)
6232 {
6233 if (!it->bidi_p)
6234 {
6235 --IT_CHARPOS (*it);
6236 --IT_BYTEPOS (*it);
6237 }
6238 else
6239 {
6240 /* We need to restore the bidi iterator to the state it
6241 had on the newline and resync IT with that. */
6242 it->bidi_it = bidi_it_prev;
6243 IT_CHARPOS (*it) = it->bidi_it.charpos;
6244 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6245 }
6246 reseat (it, it->current.pos, 0);
6247 }
6248 }
6249 else if (skipped_p)
6250 reseat (it, it->current.pos, 0);
6251
6252 CHECK_IT (it);
6253 }
6254
6255
6256 \f
6257 /***********************************************************************
6258 Changing an iterator's position
6259 ***********************************************************************/
6260
6261 /* Change IT's current position to POS in current_buffer. If FORCE_P
6262 is non-zero, always check for text properties at the new position.
6263 Otherwise, text properties are only looked up if POS >=
6264 IT->check_charpos of a property. */
6265
6266 static void
6267 reseat (struct it *it, struct text_pos pos, int force_p)
6268 {
6269 ptrdiff_t original_pos = IT_CHARPOS (*it);
6270
6271 reseat_1 (it, pos, 0);
6272
6273 /* Determine where to check text properties. Avoid doing it
6274 where possible because text property lookup is very expensive. */
6275 if (force_p
6276 || CHARPOS (pos) > it->stop_charpos
6277 || CHARPOS (pos) < original_pos)
6278 {
6279 if (it->bidi_p)
6280 {
6281 /* For bidi iteration, we need to prime prev_stop and
6282 base_level_stop with our best estimations. */
6283 /* Implementation note: Of course, POS is not necessarily a
6284 stop position, so assigning prev_pos to it is a lie; we
6285 should have called compute_stop_backwards. However, if
6286 the current buffer does not include any R2L characters,
6287 that call would be a waste of cycles, because the
6288 iterator will never move back, and thus never cross this
6289 "fake" stop position. So we delay that backward search
6290 until the time we really need it, in next_element_from_buffer. */
6291 if (CHARPOS (pos) != it->prev_stop)
6292 it->prev_stop = CHARPOS (pos);
6293 if (CHARPOS (pos) < it->base_level_stop)
6294 it->base_level_stop = 0; /* meaning it's unknown */
6295 handle_stop (it);
6296 }
6297 else
6298 {
6299 handle_stop (it);
6300 it->prev_stop = it->base_level_stop = 0;
6301 }
6302
6303 }
6304
6305 CHECK_IT (it);
6306 }
6307
6308
6309 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6310 IT->stop_pos to POS, also. */
6311
6312 static void
6313 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6314 {
6315 /* Don't call this function when scanning a C string. */
6316 eassert (it->s == NULL);
6317
6318 /* POS must be a reasonable value. */
6319 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6320
6321 it->current.pos = it->position = pos;
6322 it->end_charpos = ZV;
6323 it->dpvec = NULL;
6324 it->current.dpvec_index = -1;
6325 it->current.overlay_string_index = -1;
6326 IT_STRING_CHARPOS (*it) = -1;
6327 IT_STRING_BYTEPOS (*it) = -1;
6328 it->string = Qnil;
6329 it->method = GET_FROM_BUFFER;
6330 it->object = it->w->buffer;
6331 it->area = TEXT_AREA;
6332 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6333 it->sp = 0;
6334 it->string_from_display_prop_p = 0;
6335 it->string_from_prefix_prop_p = 0;
6336
6337 it->from_disp_prop_p = 0;
6338 it->face_before_selective_p = 0;
6339 if (it->bidi_p)
6340 {
6341 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6342 &it->bidi_it);
6343 bidi_unshelve_cache (NULL, 0);
6344 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6345 it->bidi_it.string.s = NULL;
6346 it->bidi_it.string.lstring = Qnil;
6347 it->bidi_it.string.bufpos = 0;
6348 it->bidi_it.string.unibyte = 0;
6349 }
6350
6351 if (set_stop_p)
6352 {
6353 it->stop_charpos = CHARPOS (pos);
6354 it->base_level_stop = CHARPOS (pos);
6355 }
6356 /* This make the information stored in it->cmp_it invalidate. */
6357 it->cmp_it.id = -1;
6358 }
6359
6360
6361 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6362 If S is non-null, it is a C string to iterate over. Otherwise,
6363 STRING gives a Lisp string to iterate over.
6364
6365 If PRECISION > 0, don't return more then PRECISION number of
6366 characters from the string.
6367
6368 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6369 characters have been returned. FIELD_WIDTH < 0 means an infinite
6370 field width.
6371
6372 MULTIBYTE = 0 means disable processing of multibyte characters,
6373 MULTIBYTE > 0 means enable it,
6374 MULTIBYTE < 0 means use IT->multibyte_p.
6375
6376 IT must be initialized via a prior call to init_iterator before
6377 calling this function. */
6378
6379 static void
6380 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6381 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6382 int multibyte)
6383 {
6384 /* No region in strings. */
6385 it->region_beg_charpos = it->region_end_charpos = -1;
6386
6387 /* No text property checks performed by default, but see below. */
6388 it->stop_charpos = -1;
6389
6390 /* Set iterator position and end position. */
6391 memset (&it->current, 0, sizeof it->current);
6392 it->current.overlay_string_index = -1;
6393 it->current.dpvec_index = -1;
6394 eassert (charpos >= 0);
6395
6396 /* If STRING is specified, use its multibyteness, otherwise use the
6397 setting of MULTIBYTE, if specified. */
6398 if (multibyte >= 0)
6399 it->multibyte_p = multibyte > 0;
6400
6401 /* Bidirectional reordering of strings is controlled by the default
6402 value of bidi-display-reordering. Don't try to reorder while
6403 loading loadup.el, as the necessary character property tables are
6404 not yet available. */
6405 it->bidi_p =
6406 NILP (Vpurify_flag)
6407 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6408
6409 if (s == NULL)
6410 {
6411 eassert (STRINGP (string));
6412 it->string = string;
6413 it->s = NULL;
6414 it->end_charpos = it->string_nchars = SCHARS (string);
6415 it->method = GET_FROM_STRING;
6416 it->current.string_pos = string_pos (charpos, string);
6417
6418 if (it->bidi_p)
6419 {
6420 it->bidi_it.string.lstring = string;
6421 it->bidi_it.string.s = NULL;
6422 it->bidi_it.string.schars = it->end_charpos;
6423 it->bidi_it.string.bufpos = 0;
6424 it->bidi_it.string.from_disp_str = 0;
6425 it->bidi_it.string.unibyte = !it->multibyte_p;
6426 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6427 FRAME_WINDOW_P (it->f), &it->bidi_it);
6428 }
6429 }
6430 else
6431 {
6432 it->s = (const unsigned char *) s;
6433 it->string = Qnil;
6434
6435 /* Note that we use IT->current.pos, not it->current.string_pos,
6436 for displaying C strings. */
6437 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6438 if (it->multibyte_p)
6439 {
6440 it->current.pos = c_string_pos (charpos, s, 1);
6441 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6442 }
6443 else
6444 {
6445 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6446 it->end_charpos = it->string_nchars = strlen (s);
6447 }
6448
6449 if (it->bidi_p)
6450 {
6451 it->bidi_it.string.lstring = Qnil;
6452 it->bidi_it.string.s = (const unsigned char *) s;
6453 it->bidi_it.string.schars = it->end_charpos;
6454 it->bidi_it.string.bufpos = 0;
6455 it->bidi_it.string.from_disp_str = 0;
6456 it->bidi_it.string.unibyte = !it->multibyte_p;
6457 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6458 &it->bidi_it);
6459 }
6460 it->method = GET_FROM_C_STRING;
6461 }
6462
6463 /* PRECISION > 0 means don't return more than PRECISION characters
6464 from the string. */
6465 if (precision > 0 && it->end_charpos - charpos > precision)
6466 {
6467 it->end_charpos = it->string_nchars = charpos + precision;
6468 if (it->bidi_p)
6469 it->bidi_it.string.schars = it->end_charpos;
6470 }
6471
6472 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6473 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6474 FIELD_WIDTH < 0 means infinite field width. This is useful for
6475 padding with `-' at the end of a mode line. */
6476 if (field_width < 0)
6477 field_width = INFINITY;
6478 /* Implementation note: We deliberately don't enlarge
6479 it->bidi_it.string.schars here to fit it->end_charpos, because
6480 the bidi iterator cannot produce characters out of thin air. */
6481 if (field_width > it->end_charpos - charpos)
6482 it->end_charpos = charpos + field_width;
6483
6484 /* Use the standard display table for displaying strings. */
6485 if (DISP_TABLE_P (Vstandard_display_table))
6486 it->dp = XCHAR_TABLE (Vstandard_display_table);
6487
6488 it->stop_charpos = charpos;
6489 it->prev_stop = charpos;
6490 it->base_level_stop = 0;
6491 if (it->bidi_p)
6492 {
6493 it->bidi_it.first_elt = 1;
6494 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6495 it->bidi_it.disp_pos = -1;
6496 }
6497 if (s == NULL && it->multibyte_p)
6498 {
6499 ptrdiff_t endpos = SCHARS (it->string);
6500 if (endpos > it->end_charpos)
6501 endpos = it->end_charpos;
6502 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6503 it->string);
6504 }
6505 CHECK_IT (it);
6506 }
6507
6508
6509 \f
6510 /***********************************************************************
6511 Iteration
6512 ***********************************************************************/
6513
6514 /* Map enum it_method value to corresponding next_element_from_* function. */
6515
6516 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6517 {
6518 next_element_from_buffer,
6519 next_element_from_display_vector,
6520 next_element_from_string,
6521 next_element_from_c_string,
6522 next_element_from_image,
6523 next_element_from_stretch
6524 #ifdef HAVE_XWIDGETS
6525 ,next_element_from_xwidget
6526 #endif
6527 };
6528
6529 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6530
6531
6532 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6533 (possibly with the following characters). */
6534
6535 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6536 ((IT)->cmp_it.id >= 0 \
6537 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6538 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6539 END_CHARPOS, (IT)->w, \
6540 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6541 (IT)->string)))
6542
6543
6544 /* Lookup the char-table Vglyphless_char_display for character C (-1
6545 if we want information for no-font case), and return the display
6546 method symbol. By side-effect, update it->what and
6547 it->glyphless_method. This function is called from
6548 get_next_display_element for each character element, and from
6549 x_produce_glyphs when no suitable font was found. */
6550
6551 Lisp_Object
6552 lookup_glyphless_char_display (int c, struct it *it)
6553 {
6554 Lisp_Object glyphless_method = Qnil;
6555
6556 if (CHAR_TABLE_P (Vglyphless_char_display)
6557 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6558 {
6559 if (c >= 0)
6560 {
6561 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6562 if (CONSP (glyphless_method))
6563 glyphless_method = FRAME_WINDOW_P (it->f)
6564 ? XCAR (glyphless_method)
6565 : XCDR (glyphless_method);
6566 }
6567 else
6568 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6569 }
6570
6571 retry:
6572 if (NILP (glyphless_method))
6573 {
6574 if (c >= 0)
6575 /* The default is to display the character by a proper font. */
6576 return Qnil;
6577 /* The default for the no-font case is to display an empty box. */
6578 glyphless_method = Qempty_box;
6579 }
6580 if (EQ (glyphless_method, Qzero_width))
6581 {
6582 if (c >= 0)
6583 return glyphless_method;
6584 /* This method can't be used for the no-font case. */
6585 glyphless_method = Qempty_box;
6586 }
6587 if (EQ (glyphless_method, Qthin_space))
6588 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6589 else if (EQ (glyphless_method, Qempty_box))
6590 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6591 else if (EQ (glyphless_method, Qhex_code))
6592 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6593 else if (STRINGP (glyphless_method))
6594 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6595 else
6596 {
6597 /* Invalid value. We use the default method. */
6598 glyphless_method = Qnil;
6599 goto retry;
6600 }
6601 it->what = IT_GLYPHLESS;
6602 return glyphless_method;
6603 }
6604
6605 /* Load IT's display element fields with information about the next
6606 display element from the current position of IT. Value is zero if
6607 end of buffer (or C string) is reached. */
6608
6609 static struct frame *last_escape_glyph_frame = NULL;
6610 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6611 static int last_escape_glyph_merged_face_id = 0;
6612
6613 struct frame *last_glyphless_glyph_frame = NULL;
6614 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6615 int last_glyphless_glyph_merged_face_id = 0;
6616
6617 static int
6618 get_next_display_element (struct it *it)
6619 {
6620 /* Non-zero means that we found a display element. Zero means that
6621 we hit the end of what we iterate over. Performance note: the
6622 function pointer `method' used here turns out to be faster than
6623 using a sequence of if-statements. */
6624 int success_p;
6625
6626 get_next:
6627 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6628
6629 if (it->what == IT_CHARACTER)
6630 {
6631 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6632 and only if (a) the resolved directionality of that character
6633 is R..." */
6634 /* FIXME: Do we need an exception for characters from display
6635 tables? */
6636 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6637 it->c = bidi_mirror_char (it->c);
6638 /* Map via display table or translate control characters.
6639 IT->c, IT->len etc. have been set to the next character by
6640 the function call above. If we have a display table, and it
6641 contains an entry for IT->c, translate it. Don't do this if
6642 IT->c itself comes from a display table, otherwise we could
6643 end up in an infinite recursion. (An alternative could be to
6644 count the recursion depth of this function and signal an
6645 error when a certain maximum depth is reached.) Is it worth
6646 it? */
6647 if (success_p && it->dpvec == NULL)
6648 {
6649 Lisp_Object dv;
6650 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6651 int nonascii_space_p = 0;
6652 int nonascii_hyphen_p = 0;
6653 int c = it->c; /* This is the character to display. */
6654
6655 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6656 {
6657 eassert (SINGLE_BYTE_CHAR_P (c));
6658 if (unibyte_display_via_language_environment)
6659 {
6660 c = DECODE_CHAR (unibyte, c);
6661 if (c < 0)
6662 c = BYTE8_TO_CHAR (it->c);
6663 }
6664 else
6665 c = BYTE8_TO_CHAR (it->c);
6666 }
6667
6668 if (it->dp
6669 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6670 VECTORP (dv)))
6671 {
6672 struct Lisp_Vector *v = XVECTOR (dv);
6673
6674 /* Return the first character from the display table
6675 entry, if not empty. If empty, don't display the
6676 current character. */
6677 if (v->header.size)
6678 {
6679 it->dpvec_char_len = it->len;
6680 it->dpvec = v->contents;
6681 it->dpend = v->contents + v->header.size;
6682 it->current.dpvec_index = 0;
6683 it->dpvec_face_id = -1;
6684 it->saved_face_id = it->face_id;
6685 it->method = GET_FROM_DISPLAY_VECTOR;
6686 it->ellipsis_p = 0;
6687 }
6688 else
6689 {
6690 set_iterator_to_next (it, 0);
6691 }
6692 goto get_next;
6693 }
6694
6695 if (! NILP (lookup_glyphless_char_display (c, it)))
6696 {
6697 if (it->what == IT_GLYPHLESS)
6698 goto done;
6699 /* Don't display this character. */
6700 set_iterator_to_next (it, 0);
6701 goto get_next;
6702 }
6703
6704 /* If `nobreak-char-display' is non-nil, we display
6705 non-ASCII spaces and hyphens specially. */
6706 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6707 {
6708 if (c == 0xA0)
6709 nonascii_space_p = 1;
6710 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6711 nonascii_hyphen_p = 1;
6712 }
6713
6714 /* Translate control characters into `\003' or `^C' form.
6715 Control characters coming from a display table entry are
6716 currently not translated because we use IT->dpvec to hold
6717 the translation. This could easily be changed but I
6718 don't believe that it is worth doing.
6719
6720 The characters handled by `nobreak-char-display' must be
6721 translated too.
6722
6723 Non-printable characters and raw-byte characters are also
6724 translated to octal form. */
6725 if (((c < ' ' || c == 127) /* ASCII control chars */
6726 ? (it->area != TEXT_AREA
6727 /* In mode line, treat \n, \t like other crl chars. */
6728 || (c != '\t'
6729 && it->glyph_row
6730 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6731 || (c != '\n' && c != '\t'))
6732 : (nonascii_space_p
6733 || nonascii_hyphen_p
6734 || CHAR_BYTE8_P (c)
6735 || ! CHAR_PRINTABLE_P (c))))
6736 {
6737 /* C is a control character, non-ASCII space/hyphen,
6738 raw-byte, or a non-printable character which must be
6739 displayed either as '\003' or as `^C' where the '\\'
6740 and '^' can be defined in the display table. Fill
6741 IT->ctl_chars with glyphs for what we have to
6742 display. Then, set IT->dpvec to these glyphs. */
6743 Lisp_Object gc;
6744 int ctl_len;
6745 int face_id;
6746 int lface_id = 0;
6747 int escape_glyph;
6748
6749 /* Handle control characters with ^. */
6750
6751 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6752 {
6753 int g;
6754
6755 g = '^'; /* default glyph for Control */
6756 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6757 if (it->dp
6758 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6759 {
6760 g = GLYPH_CODE_CHAR (gc);
6761 lface_id = GLYPH_CODE_FACE (gc);
6762 }
6763 if (lface_id)
6764 {
6765 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6766 }
6767 else if (it->f == last_escape_glyph_frame
6768 && it->face_id == last_escape_glyph_face_id)
6769 {
6770 face_id = last_escape_glyph_merged_face_id;
6771 }
6772 else
6773 {
6774 /* Merge the escape-glyph face into the current face. */
6775 face_id = merge_faces (it->f, Qescape_glyph, 0,
6776 it->face_id);
6777 last_escape_glyph_frame = it->f;
6778 last_escape_glyph_face_id = it->face_id;
6779 last_escape_glyph_merged_face_id = face_id;
6780 }
6781
6782 XSETINT (it->ctl_chars[0], g);
6783 XSETINT (it->ctl_chars[1], c ^ 0100);
6784 ctl_len = 2;
6785 goto display_control;
6786 }
6787
6788 /* Handle non-ascii space in the mode where it only gets
6789 highlighting. */
6790
6791 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6792 {
6793 /* Merge `nobreak-space' into the current face. */
6794 face_id = merge_faces (it->f, Qnobreak_space, 0,
6795 it->face_id);
6796 XSETINT (it->ctl_chars[0], ' ');
6797 ctl_len = 1;
6798 goto display_control;
6799 }
6800
6801 /* Handle sequences that start with the "escape glyph". */
6802
6803 /* the default escape glyph is \. */
6804 escape_glyph = '\\';
6805
6806 if (it->dp
6807 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6808 {
6809 escape_glyph = GLYPH_CODE_CHAR (gc);
6810 lface_id = GLYPH_CODE_FACE (gc);
6811 }
6812 if (lface_id)
6813 {
6814 /* The display table specified a face.
6815 Merge it into face_id and also into escape_glyph. */
6816 face_id = merge_faces (it->f, Qt, lface_id,
6817 it->face_id);
6818 }
6819 else if (it->f == last_escape_glyph_frame
6820 && it->face_id == last_escape_glyph_face_id)
6821 {
6822 face_id = last_escape_glyph_merged_face_id;
6823 }
6824 else
6825 {
6826 /* Merge the escape-glyph face into the current face. */
6827 face_id = merge_faces (it->f, Qescape_glyph, 0,
6828 it->face_id);
6829 last_escape_glyph_frame = it->f;
6830 last_escape_glyph_face_id = it->face_id;
6831 last_escape_glyph_merged_face_id = face_id;
6832 }
6833
6834 /* Draw non-ASCII hyphen with just highlighting: */
6835
6836 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6837 {
6838 XSETINT (it->ctl_chars[0], '-');
6839 ctl_len = 1;
6840 goto display_control;
6841 }
6842
6843 /* Draw non-ASCII space/hyphen with escape glyph: */
6844
6845 if (nonascii_space_p || nonascii_hyphen_p)
6846 {
6847 XSETINT (it->ctl_chars[0], escape_glyph);
6848 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6849 ctl_len = 2;
6850 goto display_control;
6851 }
6852
6853 {
6854 char str[10];
6855 int len, i;
6856
6857 if (CHAR_BYTE8_P (c))
6858 /* Display \200 instead of \17777600. */
6859 c = CHAR_TO_BYTE8 (c);
6860 len = sprintf (str, "%03o", c);
6861
6862 XSETINT (it->ctl_chars[0], escape_glyph);
6863 for (i = 0; i < len; i++)
6864 XSETINT (it->ctl_chars[i + 1], str[i]);
6865 ctl_len = len + 1;
6866 }
6867
6868 display_control:
6869 /* Set up IT->dpvec and return first character from it. */
6870 it->dpvec_char_len = it->len;
6871 it->dpvec = it->ctl_chars;
6872 it->dpend = it->dpvec + ctl_len;
6873 it->current.dpvec_index = 0;
6874 it->dpvec_face_id = face_id;
6875 it->saved_face_id = it->face_id;
6876 it->method = GET_FROM_DISPLAY_VECTOR;
6877 it->ellipsis_p = 0;
6878 goto get_next;
6879 }
6880 it->char_to_display = c;
6881 }
6882 else if (success_p)
6883 {
6884 it->char_to_display = it->c;
6885 }
6886 }
6887
6888 /* Adjust face id for a multibyte character. There are no multibyte
6889 character in unibyte text. */
6890 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6891 && it->multibyte_p
6892 && success_p
6893 && FRAME_WINDOW_P (it->f))
6894 {
6895 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6896
6897 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6898 {
6899 /* Automatic composition with glyph-string. */
6900 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6901
6902 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6903 }
6904 else
6905 {
6906 ptrdiff_t pos = (it->s ? -1
6907 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6908 : IT_CHARPOS (*it));
6909 int c;
6910
6911 if (it->what == IT_CHARACTER)
6912 c = it->char_to_display;
6913 else
6914 {
6915 struct composition *cmp = composition_table[it->cmp_it.id];
6916 int i;
6917
6918 c = ' ';
6919 for (i = 0; i < cmp->glyph_len; i++)
6920 /* TAB in a composition means display glyphs with
6921 padding space on the left or right. */
6922 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6923 break;
6924 }
6925 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6926 }
6927 }
6928
6929 done:
6930 /* Is this character the last one of a run of characters with
6931 box? If yes, set IT->end_of_box_run_p to 1. */
6932 if (it->face_box_p
6933 && it->s == NULL)
6934 {
6935 if (it->method == GET_FROM_STRING && it->sp)
6936 {
6937 int face_id = underlying_face_id (it);
6938 struct face *face = FACE_FROM_ID (it->f, face_id);
6939
6940 if (face)
6941 {
6942 if (face->box == FACE_NO_BOX)
6943 {
6944 /* If the box comes from face properties in a
6945 display string, check faces in that string. */
6946 int string_face_id = face_after_it_pos (it);
6947 it->end_of_box_run_p
6948 = (FACE_FROM_ID (it->f, string_face_id)->box
6949 == FACE_NO_BOX);
6950 }
6951 /* Otherwise, the box comes from the underlying face.
6952 If this is the last string character displayed, check
6953 the next buffer location. */
6954 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6955 && (it->current.overlay_string_index
6956 == it->n_overlay_strings - 1))
6957 {
6958 ptrdiff_t ignore;
6959 int next_face_id;
6960 struct text_pos pos = it->current.pos;
6961 INC_TEXT_POS (pos, it->multibyte_p);
6962
6963 next_face_id = face_at_buffer_position
6964 (it->w, CHARPOS (pos), it->region_beg_charpos,
6965 it->region_end_charpos, &ignore,
6966 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6967 -1);
6968 it->end_of_box_run_p
6969 = (FACE_FROM_ID (it->f, next_face_id)->box
6970 == FACE_NO_BOX);
6971 }
6972 }
6973 }
6974 else
6975 {
6976 int face_id = face_after_it_pos (it);
6977 it->end_of_box_run_p
6978 = (face_id != it->face_id
6979 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6980 }
6981 }
6982 /* If we reached the end of the object we've been iterating (e.g., a
6983 display string or an overlay string), and there's something on
6984 IT->stack, proceed with what's on the stack. It doesn't make
6985 sense to return zero if there's unprocessed stuff on the stack,
6986 because otherwise that stuff will never be displayed. */
6987 if (!success_p && it->sp > 0)
6988 {
6989 set_iterator_to_next (it, 0);
6990 success_p = get_next_display_element (it);
6991 }
6992
6993 /* Value is 0 if end of buffer or string reached. */
6994 return success_p;
6995 }
6996
6997
6998 /* Move IT to the next display element.
6999
7000 RESEAT_P non-zero means if called on a newline in buffer text,
7001 skip to the next visible line start.
7002
7003 Functions get_next_display_element and set_iterator_to_next are
7004 separate because I find this arrangement easier to handle than a
7005 get_next_display_element function that also increments IT's
7006 position. The way it is we can first look at an iterator's current
7007 display element, decide whether it fits on a line, and if it does,
7008 increment the iterator position. The other way around we probably
7009 would either need a flag indicating whether the iterator has to be
7010 incremented the next time, or we would have to implement a
7011 decrement position function which would not be easy to write. */
7012
7013 void
7014 set_iterator_to_next (struct it *it, int reseat_p)
7015 {
7016 /* Reset flags indicating start and end of a sequence of characters
7017 with box. Reset them at the start of this function because
7018 moving the iterator to a new position might set them. */
7019 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7020
7021 switch (it->method)
7022 {
7023 case GET_FROM_BUFFER:
7024 /* The current display element of IT is a character from
7025 current_buffer. Advance in the buffer, and maybe skip over
7026 invisible lines that are so because of selective display. */
7027 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7028 reseat_at_next_visible_line_start (it, 0);
7029 else if (it->cmp_it.id >= 0)
7030 {
7031 /* We are currently getting glyphs from a composition. */
7032 int i;
7033
7034 if (! it->bidi_p)
7035 {
7036 IT_CHARPOS (*it) += it->cmp_it.nchars;
7037 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7038 if (it->cmp_it.to < it->cmp_it.nglyphs)
7039 {
7040 it->cmp_it.from = it->cmp_it.to;
7041 }
7042 else
7043 {
7044 it->cmp_it.id = -1;
7045 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7046 IT_BYTEPOS (*it),
7047 it->end_charpos, Qnil);
7048 }
7049 }
7050 else if (! it->cmp_it.reversed_p)
7051 {
7052 /* Composition created while scanning forward. */
7053 /* Update IT's char/byte positions to point to the first
7054 character of the next grapheme cluster, or to the
7055 character visually after the current composition. */
7056 for (i = 0; i < it->cmp_it.nchars; i++)
7057 bidi_move_to_visually_next (&it->bidi_it);
7058 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7059 IT_CHARPOS (*it) = it->bidi_it.charpos;
7060
7061 if (it->cmp_it.to < it->cmp_it.nglyphs)
7062 {
7063 /* Proceed to the next grapheme cluster. */
7064 it->cmp_it.from = it->cmp_it.to;
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 else
7080 {
7081 /* Composition created while scanning backward. */
7082 /* Update IT's char/byte positions to point to the last
7083 character of the previous grapheme cluster, or the
7084 character visually after the current composition. */
7085 for (i = 0; i < it->cmp_it.nchars; i++)
7086 bidi_move_to_visually_next (&it->bidi_it);
7087 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7088 IT_CHARPOS (*it) = it->bidi_it.charpos;
7089 if (it->cmp_it.from > 0)
7090 {
7091 /* Proceed to the previous grapheme cluster. */
7092 it->cmp_it.to = it->cmp_it.from;
7093 }
7094 else
7095 {
7096 /* No more grapheme clusters in this composition.
7097 Find the next stop position. */
7098 ptrdiff_t stop = it->end_charpos;
7099 if (it->bidi_it.scan_dir < 0)
7100 /* Now we are scanning backward and don't know
7101 where to stop. */
7102 stop = -1;
7103 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7104 IT_BYTEPOS (*it), stop, Qnil);
7105 }
7106 }
7107 }
7108 else
7109 {
7110 eassert (it->len != 0);
7111
7112 if (!it->bidi_p)
7113 {
7114 IT_BYTEPOS (*it) += it->len;
7115 IT_CHARPOS (*it) += 1;
7116 }
7117 else
7118 {
7119 int prev_scan_dir = it->bidi_it.scan_dir;
7120 /* If this is a new paragraph, determine its base
7121 direction (a.k.a. its base embedding level). */
7122 if (it->bidi_it.new_paragraph)
7123 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7124 bidi_move_to_visually_next (&it->bidi_it);
7125 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7126 IT_CHARPOS (*it) = it->bidi_it.charpos;
7127 if (prev_scan_dir != it->bidi_it.scan_dir)
7128 {
7129 /* As the scan direction was changed, we must
7130 re-compute the stop position for composition. */
7131 ptrdiff_t stop = it->end_charpos;
7132 if (it->bidi_it.scan_dir < 0)
7133 stop = -1;
7134 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7135 IT_BYTEPOS (*it), stop, Qnil);
7136 }
7137 }
7138 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7139 }
7140 break;
7141
7142 case GET_FROM_C_STRING:
7143 /* Current display element of IT is from a C string. */
7144 if (!it->bidi_p
7145 /* If the string position is beyond string's end, it means
7146 next_element_from_c_string is padding the string with
7147 blanks, in which case we bypass the bidi iterator,
7148 because it cannot deal with such virtual characters. */
7149 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7150 {
7151 IT_BYTEPOS (*it) += it->len;
7152 IT_CHARPOS (*it) += 1;
7153 }
7154 else
7155 {
7156 bidi_move_to_visually_next (&it->bidi_it);
7157 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7158 IT_CHARPOS (*it) = it->bidi_it.charpos;
7159 }
7160 break;
7161
7162 case GET_FROM_DISPLAY_VECTOR:
7163 /* Current display element of IT is from a display table entry.
7164 Advance in the display table definition. Reset it to null if
7165 end reached, and continue with characters from buffers/
7166 strings. */
7167 ++it->current.dpvec_index;
7168
7169 /* Restore face of the iterator to what they were before the
7170 display vector entry (these entries may contain faces). */
7171 it->face_id = it->saved_face_id;
7172
7173 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7174 {
7175 int recheck_faces = it->ellipsis_p;
7176
7177 if (it->s)
7178 it->method = GET_FROM_C_STRING;
7179 else if (STRINGP (it->string))
7180 it->method = GET_FROM_STRING;
7181 else
7182 {
7183 it->method = GET_FROM_BUFFER;
7184 it->object = it->w->buffer;
7185 }
7186
7187 it->dpvec = NULL;
7188 it->current.dpvec_index = -1;
7189
7190 /* Skip over characters which were displayed via IT->dpvec. */
7191 if (it->dpvec_char_len < 0)
7192 reseat_at_next_visible_line_start (it, 1);
7193 else if (it->dpvec_char_len > 0)
7194 {
7195 if (it->method == GET_FROM_STRING
7196 && it->n_overlay_strings > 0)
7197 it->ignore_overlay_strings_at_pos_p = 1;
7198 it->len = it->dpvec_char_len;
7199 set_iterator_to_next (it, reseat_p);
7200 }
7201
7202 /* Maybe recheck faces after display vector */
7203 if (recheck_faces)
7204 it->stop_charpos = IT_CHARPOS (*it);
7205 }
7206 break;
7207
7208 case GET_FROM_STRING:
7209 /* Current display element is a character from a Lisp string. */
7210 eassert (it->s == NULL && STRINGP (it->string));
7211 /* Don't advance past string end. These conditions are true
7212 when set_iterator_to_next is called at the end of
7213 get_next_display_element, in which case the Lisp string is
7214 already exhausted, and all we want is pop the iterator
7215 stack. */
7216 if (it->current.overlay_string_index >= 0)
7217 {
7218 /* This is an overlay string, so there's no padding with
7219 spaces, and the number of characters in the string is
7220 where the string ends. */
7221 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7222 goto consider_string_end;
7223 }
7224 else
7225 {
7226 /* Not an overlay string. There could be padding, so test
7227 against it->end_charpos . */
7228 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7229 goto consider_string_end;
7230 }
7231 if (it->cmp_it.id >= 0)
7232 {
7233 int i;
7234
7235 if (! it->bidi_p)
7236 {
7237 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7238 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7239 if (it->cmp_it.to < it->cmp_it.nglyphs)
7240 it->cmp_it.from = it->cmp_it.to;
7241 else
7242 {
7243 it->cmp_it.id = -1;
7244 composition_compute_stop_pos (&it->cmp_it,
7245 IT_STRING_CHARPOS (*it),
7246 IT_STRING_BYTEPOS (*it),
7247 it->end_charpos, it->string);
7248 }
7249 }
7250 else if (! it->cmp_it.reversed_p)
7251 {
7252 for (i = 0; i < it->cmp_it.nchars; i++)
7253 bidi_move_to_visually_next (&it->bidi_it);
7254 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7255 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7256
7257 if (it->cmp_it.to < it->cmp_it.nglyphs)
7258 it->cmp_it.from = it->cmp_it.to;
7259 else
7260 {
7261 ptrdiff_t stop = it->end_charpos;
7262 if (it->bidi_it.scan_dir < 0)
7263 stop = -1;
7264 composition_compute_stop_pos (&it->cmp_it,
7265 IT_STRING_CHARPOS (*it),
7266 IT_STRING_BYTEPOS (*it), stop,
7267 it->string);
7268 }
7269 }
7270 else
7271 {
7272 for (i = 0; i < it->cmp_it.nchars; i++)
7273 bidi_move_to_visually_next (&it->bidi_it);
7274 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7275 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7276 if (it->cmp_it.from > 0)
7277 it->cmp_it.to = it->cmp_it.from;
7278 else
7279 {
7280 ptrdiff_t stop = it->end_charpos;
7281 if (it->bidi_it.scan_dir < 0)
7282 stop = -1;
7283 composition_compute_stop_pos (&it->cmp_it,
7284 IT_STRING_CHARPOS (*it),
7285 IT_STRING_BYTEPOS (*it), stop,
7286 it->string);
7287 }
7288 }
7289 }
7290 else
7291 {
7292 if (!it->bidi_p
7293 /* If the string position is beyond string's end, it
7294 means next_element_from_string is padding the string
7295 with blanks, in which case we bypass the bidi
7296 iterator, because it cannot deal with such virtual
7297 characters. */
7298 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7299 {
7300 IT_STRING_BYTEPOS (*it) += it->len;
7301 IT_STRING_CHARPOS (*it) += 1;
7302 }
7303 else
7304 {
7305 int prev_scan_dir = it->bidi_it.scan_dir;
7306
7307 bidi_move_to_visually_next (&it->bidi_it);
7308 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7309 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7310 if (prev_scan_dir != it->bidi_it.scan_dir)
7311 {
7312 ptrdiff_t stop = it->end_charpos;
7313
7314 if (it->bidi_it.scan_dir < 0)
7315 stop = -1;
7316 composition_compute_stop_pos (&it->cmp_it,
7317 IT_STRING_CHARPOS (*it),
7318 IT_STRING_BYTEPOS (*it), stop,
7319 it->string);
7320 }
7321 }
7322 }
7323
7324 consider_string_end:
7325
7326 if (it->current.overlay_string_index >= 0)
7327 {
7328 /* IT->string is an overlay string. Advance to the
7329 next, if there is one. */
7330 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7331 {
7332 it->ellipsis_p = 0;
7333 next_overlay_string (it);
7334 if (it->ellipsis_p)
7335 setup_for_ellipsis (it, 0);
7336 }
7337 }
7338 else
7339 {
7340 /* IT->string is not an overlay string. If we reached
7341 its end, and there is something on IT->stack, proceed
7342 with what is on the stack. This can be either another
7343 string, this time an overlay string, or a buffer. */
7344 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7345 && it->sp > 0)
7346 {
7347 pop_it (it);
7348 if (it->method == GET_FROM_STRING)
7349 goto consider_string_end;
7350 }
7351 }
7352 break;
7353
7354 case GET_FROM_IMAGE:
7355 case GET_FROM_STRETCH:
7356 #ifdef HAVE_XWIDGETS
7357 case GET_FROM_XWIDGET:
7358
7359 /* The position etc with which we have to proceed are on
7360 the stack. The position may be at the end of a string,
7361 if the `display' property takes up the whole string. */
7362 eassert (it->sp > 0);
7363 pop_it (it);
7364 if (it->method == GET_FROM_STRING)
7365 goto consider_string_end;
7366 break;
7367 #endif
7368 default:
7369 /* There are no other methods defined, so this should be a bug. */
7370 emacs_abort ();
7371 }
7372
7373 eassert (it->method != GET_FROM_STRING
7374 || (STRINGP (it->string)
7375 && IT_STRING_CHARPOS (*it) >= 0));
7376 }
7377
7378 /* Load IT's display element fields with information about the next
7379 display element which comes from a display table entry or from the
7380 result of translating a control character to one of the forms `^C'
7381 or `\003'.
7382
7383 IT->dpvec holds the glyphs to return as characters.
7384 IT->saved_face_id holds the face id before the display vector--it
7385 is restored into IT->face_id in set_iterator_to_next. */
7386
7387 static int
7388 next_element_from_display_vector (struct it *it)
7389 {
7390 Lisp_Object gc;
7391
7392 /* Precondition. */
7393 eassert (it->dpvec && it->current.dpvec_index >= 0);
7394
7395 it->face_id = it->saved_face_id;
7396
7397 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7398 That seemed totally bogus - so I changed it... */
7399 gc = it->dpvec[it->current.dpvec_index];
7400
7401 if (GLYPH_CODE_P (gc))
7402 {
7403 it->c = GLYPH_CODE_CHAR (gc);
7404 it->len = CHAR_BYTES (it->c);
7405
7406 /* The entry may contain a face id to use. Such a face id is
7407 the id of a Lisp face, not a realized face. A face id of
7408 zero means no face is specified. */
7409 if (it->dpvec_face_id >= 0)
7410 it->face_id = it->dpvec_face_id;
7411 else
7412 {
7413 int lface_id = GLYPH_CODE_FACE (gc);
7414 if (lface_id > 0)
7415 it->face_id = merge_faces (it->f, Qt, lface_id,
7416 it->saved_face_id);
7417 }
7418 }
7419 else
7420 /* Display table entry is invalid. Return a space. */
7421 it->c = ' ', it->len = 1;
7422
7423 /* Don't change position and object of the iterator here. They are
7424 still the values of the character that had this display table
7425 entry or was translated, and that's what we want. */
7426 it->what = IT_CHARACTER;
7427 return 1;
7428 }
7429
7430 /* Get the first element of string/buffer in the visual order, after
7431 being reseated to a new position in a string or a buffer. */
7432 static void
7433 get_visually_first_element (struct it *it)
7434 {
7435 int string_p = STRINGP (it->string) || it->s;
7436 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7437 ptrdiff_t bob = (string_p ? 0 : BEGV);
7438
7439 if (STRINGP (it->string))
7440 {
7441 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7442 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7443 }
7444 else
7445 {
7446 it->bidi_it.charpos = IT_CHARPOS (*it);
7447 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7448 }
7449
7450 if (it->bidi_it.charpos == eob)
7451 {
7452 /* Nothing to do, but reset the FIRST_ELT flag, like
7453 bidi_paragraph_init does, because we are not going to
7454 call it. */
7455 it->bidi_it.first_elt = 0;
7456 }
7457 else if (it->bidi_it.charpos == bob
7458 || (!string_p
7459 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7460 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7461 {
7462 /* If we are at the beginning of a line/string, we can produce
7463 the next element right away. */
7464 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7465 bidi_move_to_visually_next (&it->bidi_it);
7466 }
7467 else
7468 {
7469 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7470
7471 /* We need to prime the bidi iterator starting at the line's or
7472 string's beginning, before we will be able to produce the
7473 next element. */
7474 if (string_p)
7475 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7476 else
7477 it->bidi_it.charpos
7478 = find_next_newline_no_quit (IT_CHARPOS (*it), -1,
7479 &it->bidi_it.bytepos);
7480 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7481 do
7482 {
7483 /* Now return to buffer/string position where we were asked
7484 to get the next display element, and produce that. */
7485 bidi_move_to_visually_next (&it->bidi_it);
7486 }
7487 while (it->bidi_it.bytepos != orig_bytepos
7488 && it->bidi_it.charpos < eob);
7489 }
7490
7491 /* Adjust IT's position information to where we ended up. */
7492 if (STRINGP (it->string))
7493 {
7494 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7495 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7496 }
7497 else
7498 {
7499 IT_CHARPOS (*it) = it->bidi_it.charpos;
7500 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7501 }
7502
7503 if (STRINGP (it->string) || !it->s)
7504 {
7505 ptrdiff_t stop, charpos, bytepos;
7506
7507 if (STRINGP (it->string))
7508 {
7509 eassert (!it->s);
7510 stop = SCHARS (it->string);
7511 if (stop > it->end_charpos)
7512 stop = it->end_charpos;
7513 charpos = IT_STRING_CHARPOS (*it);
7514 bytepos = IT_STRING_BYTEPOS (*it);
7515 }
7516 else
7517 {
7518 stop = it->end_charpos;
7519 charpos = IT_CHARPOS (*it);
7520 bytepos = IT_BYTEPOS (*it);
7521 }
7522 if (it->bidi_it.scan_dir < 0)
7523 stop = -1;
7524 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7525 it->string);
7526 }
7527 }
7528
7529 /* Load IT with the next display element from Lisp string IT->string.
7530 IT->current.string_pos is the current position within the string.
7531 If IT->current.overlay_string_index >= 0, the Lisp string is an
7532 overlay string. */
7533
7534 static int
7535 next_element_from_string (struct it *it)
7536 {
7537 struct text_pos position;
7538
7539 eassert (STRINGP (it->string));
7540 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7541 eassert (IT_STRING_CHARPOS (*it) >= 0);
7542 position = it->current.string_pos;
7543
7544 /* With bidi reordering, the character to display might not be the
7545 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7546 that we were reseat()ed to a new string, whose paragraph
7547 direction is not known. */
7548 if (it->bidi_p && it->bidi_it.first_elt)
7549 {
7550 get_visually_first_element (it);
7551 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7552 }
7553
7554 /* Time to check for invisible text? */
7555 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7556 {
7557 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7558 {
7559 if (!(!it->bidi_p
7560 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7561 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7562 {
7563 /* With bidi non-linear iteration, we could find
7564 ourselves far beyond the last computed stop_charpos,
7565 with several other stop positions in between that we
7566 missed. Scan them all now, in buffer's logical
7567 order, until we find and handle the last stop_charpos
7568 that precedes our current position. */
7569 handle_stop_backwards (it, it->stop_charpos);
7570 return GET_NEXT_DISPLAY_ELEMENT (it);
7571 }
7572 else
7573 {
7574 if (it->bidi_p)
7575 {
7576 /* Take note of the stop position we just moved
7577 across, for when we will move back across it. */
7578 it->prev_stop = it->stop_charpos;
7579 /* If we are at base paragraph embedding level, take
7580 note of the last stop position seen at this
7581 level. */
7582 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7583 it->base_level_stop = it->stop_charpos;
7584 }
7585 handle_stop (it);
7586
7587 /* Since a handler may have changed IT->method, we must
7588 recurse here. */
7589 return GET_NEXT_DISPLAY_ELEMENT (it);
7590 }
7591 }
7592 else if (it->bidi_p
7593 /* If we are before prev_stop, we may have overstepped
7594 on our way backwards a stop_pos, and if so, we need
7595 to handle that stop_pos. */
7596 && IT_STRING_CHARPOS (*it) < it->prev_stop
7597 /* We can sometimes back up for reasons that have nothing
7598 to do with bidi reordering. E.g., compositions. The
7599 code below is only needed when we are above the base
7600 embedding level, so test for that explicitly. */
7601 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7602 {
7603 /* If we lost track of base_level_stop, we have no better
7604 place for handle_stop_backwards to start from than string
7605 beginning. This happens, e.g., when we were reseated to
7606 the previous screenful of text by vertical-motion. */
7607 if (it->base_level_stop <= 0
7608 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7609 it->base_level_stop = 0;
7610 handle_stop_backwards (it, it->base_level_stop);
7611 return GET_NEXT_DISPLAY_ELEMENT (it);
7612 }
7613 }
7614
7615 if (it->current.overlay_string_index >= 0)
7616 {
7617 /* Get the next character from an overlay string. In overlay
7618 strings, there is no field width or padding with spaces to
7619 do. */
7620 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7621 {
7622 it->what = IT_EOB;
7623 return 0;
7624 }
7625 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7626 IT_STRING_BYTEPOS (*it),
7627 it->bidi_it.scan_dir < 0
7628 ? -1
7629 : SCHARS (it->string))
7630 && next_element_from_composition (it))
7631 {
7632 return 1;
7633 }
7634 else if (STRING_MULTIBYTE (it->string))
7635 {
7636 const unsigned char *s = (SDATA (it->string)
7637 + IT_STRING_BYTEPOS (*it));
7638 it->c = string_char_and_length (s, &it->len);
7639 }
7640 else
7641 {
7642 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7643 it->len = 1;
7644 }
7645 }
7646 else
7647 {
7648 /* Get the next character from a Lisp string that is not an
7649 overlay string. Such strings come from the mode line, for
7650 example. We may have to pad with spaces, or truncate the
7651 string. See also next_element_from_c_string. */
7652 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7653 {
7654 it->what = IT_EOB;
7655 return 0;
7656 }
7657 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7658 {
7659 /* Pad with spaces. */
7660 it->c = ' ', it->len = 1;
7661 CHARPOS (position) = BYTEPOS (position) = -1;
7662 }
7663 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7664 IT_STRING_BYTEPOS (*it),
7665 it->bidi_it.scan_dir < 0
7666 ? -1
7667 : it->string_nchars)
7668 && next_element_from_composition (it))
7669 {
7670 return 1;
7671 }
7672 else if (STRING_MULTIBYTE (it->string))
7673 {
7674 const unsigned char *s = (SDATA (it->string)
7675 + IT_STRING_BYTEPOS (*it));
7676 it->c = string_char_and_length (s, &it->len);
7677 }
7678 else
7679 {
7680 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7681 it->len = 1;
7682 }
7683 }
7684
7685 /* Record what we have and where it came from. */
7686 it->what = IT_CHARACTER;
7687 it->object = it->string;
7688 it->position = position;
7689 return 1;
7690 }
7691
7692
7693 /* Load IT with next display element from C string IT->s.
7694 IT->string_nchars is the maximum number of characters to return
7695 from the string. IT->end_charpos may be greater than
7696 IT->string_nchars when this function is called, in which case we
7697 may have to return padding spaces. Value is zero if end of string
7698 reached, including padding spaces. */
7699
7700 static int
7701 next_element_from_c_string (struct it *it)
7702 {
7703 int success_p = 1;
7704
7705 eassert (it->s);
7706 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7707 it->what = IT_CHARACTER;
7708 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7709 it->object = Qnil;
7710
7711 /* With bidi reordering, the character to display might not be the
7712 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7713 we were reseated to a new string, whose paragraph direction is
7714 not known. */
7715 if (it->bidi_p && it->bidi_it.first_elt)
7716 get_visually_first_element (it);
7717
7718 /* IT's position can be greater than IT->string_nchars in case a
7719 field width or precision has been specified when the iterator was
7720 initialized. */
7721 if (IT_CHARPOS (*it) >= it->end_charpos)
7722 {
7723 /* End of the game. */
7724 it->what = IT_EOB;
7725 success_p = 0;
7726 }
7727 else if (IT_CHARPOS (*it) >= it->string_nchars)
7728 {
7729 /* Pad with spaces. */
7730 it->c = ' ', it->len = 1;
7731 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7732 }
7733 else if (it->multibyte_p)
7734 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7735 else
7736 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7737
7738 return success_p;
7739 }
7740
7741
7742 /* Set up IT to return characters from an ellipsis, if appropriate.
7743 The definition of the ellipsis glyphs may come from a display table
7744 entry. This function fills IT with the first glyph from the
7745 ellipsis if an ellipsis is to be displayed. */
7746
7747 static int
7748 next_element_from_ellipsis (struct it *it)
7749 {
7750 if (it->selective_display_ellipsis_p)
7751 setup_for_ellipsis (it, it->len);
7752 else
7753 {
7754 /* The face at the current position may be different from the
7755 face we find after the invisible text. Remember what it
7756 was in IT->saved_face_id, and signal that it's there by
7757 setting face_before_selective_p. */
7758 it->saved_face_id = it->face_id;
7759 it->method = GET_FROM_BUFFER;
7760 it->object = it->w->buffer;
7761 reseat_at_next_visible_line_start (it, 1);
7762 it->face_before_selective_p = 1;
7763 }
7764
7765 return GET_NEXT_DISPLAY_ELEMENT (it);
7766 }
7767
7768
7769 /* Deliver an image display element. The iterator IT is already
7770 filled with image information (done in handle_display_prop). Value
7771 is always 1. */
7772
7773
7774 static int
7775 next_element_from_image (struct it *it)
7776 {
7777 it->what = IT_IMAGE;
7778 it->ignore_overlay_strings_at_pos_p = 0;
7779 return 1;
7780 }
7781
7782 #ifdef HAVE_XWIDGETS
7783 /* im not sure about this FIXME JAVE*/
7784 static int
7785 next_element_from_xwidget (struct it *it)
7786 {
7787 it->what = IT_XWIDGET;
7788 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
7789 //this is shaky because why do we set "what" if we dont set the other parts??
7790 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
7791 return 1;
7792 }
7793 #endif
7794
7795
7796 /* Fill iterator IT with next display element from a stretch glyph
7797 property. IT->object is the value of the text property. Value is
7798 always 1. */
7799
7800 static int
7801 next_element_from_stretch (struct it *it)
7802 {
7803 it->what = IT_STRETCH;
7804 return 1;
7805 }
7806
7807 /* Scan backwards from IT's current position until we find a stop
7808 position, or until BEGV. This is called when we find ourself
7809 before both the last known prev_stop and base_level_stop while
7810 reordering bidirectional text. */
7811
7812 static void
7813 compute_stop_pos_backwards (struct it *it)
7814 {
7815 const int SCAN_BACK_LIMIT = 1000;
7816 struct text_pos pos;
7817 struct display_pos save_current = it->current;
7818 struct text_pos save_position = it->position;
7819 ptrdiff_t charpos = IT_CHARPOS (*it);
7820 ptrdiff_t where_we_are = charpos;
7821 ptrdiff_t save_stop_pos = it->stop_charpos;
7822 ptrdiff_t save_end_pos = it->end_charpos;
7823
7824 eassert (NILP (it->string) && !it->s);
7825 eassert (it->bidi_p);
7826 it->bidi_p = 0;
7827 do
7828 {
7829 it->end_charpos = min (charpos + 1, ZV);
7830 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7831 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7832 reseat_1 (it, pos, 0);
7833 compute_stop_pos (it);
7834 /* We must advance forward, right? */
7835 if (it->stop_charpos <= charpos)
7836 emacs_abort ();
7837 }
7838 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7839
7840 if (it->stop_charpos <= where_we_are)
7841 it->prev_stop = it->stop_charpos;
7842 else
7843 it->prev_stop = BEGV;
7844 it->bidi_p = 1;
7845 it->current = save_current;
7846 it->position = save_position;
7847 it->stop_charpos = save_stop_pos;
7848 it->end_charpos = save_end_pos;
7849 }
7850
7851 /* Scan forward from CHARPOS in the current buffer/string, until we
7852 find a stop position > current IT's position. Then handle the stop
7853 position before that. This is called when we bump into a stop
7854 position while reordering bidirectional text. CHARPOS should be
7855 the last previously processed stop_pos (or BEGV/0, if none were
7856 processed yet) whose position is less that IT's current
7857 position. */
7858
7859 static void
7860 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7861 {
7862 int bufp = !STRINGP (it->string);
7863 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7864 struct display_pos save_current = it->current;
7865 struct text_pos save_position = it->position;
7866 struct text_pos pos1;
7867 ptrdiff_t next_stop;
7868
7869 /* Scan in strict logical order. */
7870 eassert (it->bidi_p);
7871 it->bidi_p = 0;
7872 do
7873 {
7874 it->prev_stop = charpos;
7875 if (bufp)
7876 {
7877 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7878 reseat_1 (it, pos1, 0);
7879 }
7880 else
7881 it->current.string_pos = string_pos (charpos, it->string);
7882 compute_stop_pos (it);
7883 /* We must advance forward, right? */
7884 if (it->stop_charpos <= it->prev_stop)
7885 emacs_abort ();
7886 charpos = it->stop_charpos;
7887 }
7888 while (charpos <= where_we_are);
7889
7890 it->bidi_p = 1;
7891 it->current = save_current;
7892 it->position = save_position;
7893 next_stop = it->stop_charpos;
7894 it->stop_charpos = it->prev_stop;
7895 handle_stop (it);
7896 it->stop_charpos = next_stop;
7897 }
7898
7899 /* Load IT with the next display element from current_buffer. Value
7900 is zero if end of buffer reached. IT->stop_charpos is the next
7901 position at which to stop and check for text properties or buffer
7902 end. */
7903
7904 static int
7905 next_element_from_buffer (struct it *it)
7906 {
7907 int success_p = 1;
7908
7909 eassert (IT_CHARPOS (*it) >= BEGV);
7910 eassert (NILP (it->string) && !it->s);
7911 eassert (!it->bidi_p
7912 || (EQ (it->bidi_it.string.lstring, Qnil)
7913 && it->bidi_it.string.s == NULL));
7914
7915 /* With bidi reordering, the character to display might not be the
7916 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7917 we were reseat()ed to a new buffer position, which is potentially
7918 a different paragraph. */
7919 if (it->bidi_p && it->bidi_it.first_elt)
7920 {
7921 get_visually_first_element (it);
7922 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7923 }
7924
7925 if (IT_CHARPOS (*it) >= it->stop_charpos)
7926 {
7927 if (IT_CHARPOS (*it) >= it->end_charpos)
7928 {
7929 int overlay_strings_follow_p;
7930
7931 /* End of the game, except when overlay strings follow that
7932 haven't been returned yet. */
7933 if (it->overlay_strings_at_end_processed_p)
7934 overlay_strings_follow_p = 0;
7935 else
7936 {
7937 it->overlay_strings_at_end_processed_p = 1;
7938 overlay_strings_follow_p = get_overlay_strings (it, 0);
7939 }
7940
7941 if (overlay_strings_follow_p)
7942 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7943 else
7944 {
7945 it->what = IT_EOB;
7946 it->position = it->current.pos;
7947 success_p = 0;
7948 }
7949 }
7950 else if (!(!it->bidi_p
7951 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7952 || IT_CHARPOS (*it) == it->stop_charpos))
7953 {
7954 /* With bidi non-linear iteration, we could find ourselves
7955 far beyond the last computed stop_charpos, with several
7956 other stop positions in between that we missed. Scan
7957 them all now, in buffer's logical order, until we find
7958 and handle the last stop_charpos that precedes our
7959 current position. */
7960 handle_stop_backwards (it, it->stop_charpos);
7961 return GET_NEXT_DISPLAY_ELEMENT (it);
7962 }
7963 else
7964 {
7965 if (it->bidi_p)
7966 {
7967 /* Take note of the stop position we just moved across,
7968 for when we will move back across it. */
7969 it->prev_stop = it->stop_charpos;
7970 /* If we are at base paragraph embedding level, take
7971 note of the last stop position seen at this
7972 level. */
7973 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7974 it->base_level_stop = it->stop_charpos;
7975 }
7976 handle_stop (it);
7977 return GET_NEXT_DISPLAY_ELEMENT (it);
7978 }
7979 }
7980 else if (it->bidi_p
7981 /* If we are before prev_stop, we may have overstepped on
7982 our way backwards a stop_pos, and if so, we need to
7983 handle that stop_pos. */
7984 && IT_CHARPOS (*it) < it->prev_stop
7985 /* We can sometimes back up for reasons that have nothing
7986 to do with bidi reordering. E.g., compositions. The
7987 code below is only needed when we are above the base
7988 embedding level, so test for that explicitly. */
7989 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7990 {
7991 if (it->base_level_stop <= 0
7992 || IT_CHARPOS (*it) < it->base_level_stop)
7993 {
7994 /* If we lost track of base_level_stop, we need to find
7995 prev_stop by looking backwards. This happens, e.g., when
7996 we were reseated to the previous screenful of text by
7997 vertical-motion. */
7998 it->base_level_stop = BEGV;
7999 compute_stop_pos_backwards (it);
8000 handle_stop_backwards (it, it->prev_stop);
8001 }
8002 else
8003 handle_stop_backwards (it, it->base_level_stop);
8004 return GET_NEXT_DISPLAY_ELEMENT (it);
8005 }
8006 else
8007 {
8008 /* No face changes, overlays etc. in sight, so just return a
8009 character from current_buffer. */
8010 unsigned char *p;
8011 ptrdiff_t stop;
8012
8013 /* Maybe run the redisplay end trigger hook. Performance note:
8014 This doesn't seem to cost measurable time. */
8015 if (it->redisplay_end_trigger_charpos
8016 && it->glyph_row
8017 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8018 run_redisplay_end_trigger_hook (it);
8019
8020 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8021 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8022 stop)
8023 && next_element_from_composition (it))
8024 {
8025 return 1;
8026 }
8027
8028 /* Get the next character, maybe multibyte. */
8029 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8030 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8031 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8032 else
8033 it->c = *p, it->len = 1;
8034
8035 /* Record what we have and where it came from. */
8036 it->what = IT_CHARACTER;
8037 it->object = it->w->buffer;
8038 it->position = it->current.pos;
8039
8040 /* Normally we return the character found above, except when we
8041 really want to return an ellipsis for selective display. */
8042 if (it->selective)
8043 {
8044 if (it->c == '\n')
8045 {
8046 /* A value of selective > 0 means hide lines indented more
8047 than that number of columns. */
8048 if (it->selective > 0
8049 && IT_CHARPOS (*it) + 1 < ZV
8050 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8051 IT_BYTEPOS (*it) + 1,
8052 it->selective))
8053 {
8054 success_p = next_element_from_ellipsis (it);
8055 it->dpvec_char_len = -1;
8056 }
8057 }
8058 else if (it->c == '\r' && it->selective == -1)
8059 {
8060 /* A value of selective == -1 means that everything from the
8061 CR to the end of the line is invisible, with maybe an
8062 ellipsis displayed for it. */
8063 success_p = next_element_from_ellipsis (it);
8064 it->dpvec_char_len = -1;
8065 }
8066 }
8067 }
8068
8069 /* Value is zero if end of buffer reached. */
8070 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8071 return success_p;
8072 }
8073
8074
8075 /* Run the redisplay end trigger hook for IT. */
8076
8077 static void
8078 run_redisplay_end_trigger_hook (struct it *it)
8079 {
8080 Lisp_Object args[3];
8081
8082 /* IT->glyph_row should be non-null, i.e. we should be actually
8083 displaying something, or otherwise we should not run the hook. */
8084 eassert (it->glyph_row);
8085
8086 /* Set up hook arguments. */
8087 args[0] = Qredisplay_end_trigger_functions;
8088 args[1] = it->window;
8089 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8090 it->redisplay_end_trigger_charpos = 0;
8091
8092 /* Since we are *trying* to run these functions, don't try to run
8093 them again, even if they get an error. */
8094 wset_redisplay_end_trigger (it->w, Qnil);
8095 Frun_hook_with_args (3, args);
8096
8097 /* Notice if it changed the face of the character we are on. */
8098 handle_face_prop (it);
8099 }
8100
8101
8102 /* Deliver a composition display element. Unlike the other
8103 next_element_from_XXX, this function is not registered in the array
8104 get_next_element[]. It is called from next_element_from_buffer and
8105 next_element_from_string when necessary. */
8106
8107 static int
8108 next_element_from_composition (struct it *it)
8109 {
8110 it->what = IT_COMPOSITION;
8111 it->len = it->cmp_it.nbytes;
8112 if (STRINGP (it->string))
8113 {
8114 if (it->c < 0)
8115 {
8116 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8117 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8118 return 0;
8119 }
8120 it->position = it->current.string_pos;
8121 it->object = it->string;
8122 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8123 IT_STRING_BYTEPOS (*it), it->string);
8124 }
8125 else
8126 {
8127 if (it->c < 0)
8128 {
8129 IT_CHARPOS (*it) += it->cmp_it.nchars;
8130 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8131 if (it->bidi_p)
8132 {
8133 if (it->bidi_it.new_paragraph)
8134 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8135 /* Resync the bidi iterator with IT's new position.
8136 FIXME: this doesn't support bidirectional text. */
8137 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8138 bidi_move_to_visually_next (&it->bidi_it);
8139 }
8140 return 0;
8141 }
8142 it->position = it->current.pos;
8143 it->object = it->w->buffer;
8144 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8145 IT_BYTEPOS (*it), Qnil);
8146 }
8147 return 1;
8148 }
8149
8150
8151 \f
8152 /***********************************************************************
8153 Moving an iterator without producing glyphs
8154 ***********************************************************************/
8155
8156 /* Check if iterator is at a position corresponding to a valid buffer
8157 position after some move_it_ call. */
8158
8159 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8160 ((it)->method == GET_FROM_STRING \
8161 ? IT_STRING_CHARPOS (*it) == 0 \
8162 : 1)
8163
8164
8165 /* Move iterator IT to a specified buffer or X position within one
8166 line on the display without producing glyphs.
8167
8168 OP should be a bit mask including some or all of these bits:
8169 MOVE_TO_X: Stop upon reaching x-position TO_X.
8170 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8171 Regardless of OP's value, stop upon reaching the end of the display line.
8172
8173 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8174 This means, in particular, that TO_X includes window's horizontal
8175 scroll amount.
8176
8177 The return value has several possible values that
8178 say what condition caused the scan to stop:
8179
8180 MOVE_POS_MATCH_OR_ZV
8181 - when TO_POS or ZV was reached.
8182
8183 MOVE_X_REACHED
8184 -when TO_X was reached before TO_POS or ZV were reached.
8185
8186 MOVE_LINE_CONTINUED
8187 - when we reached the end of the display area and the line must
8188 be continued.
8189
8190 MOVE_LINE_TRUNCATED
8191 - when we reached the end of the display area and the line is
8192 truncated.
8193
8194 MOVE_NEWLINE_OR_CR
8195 - when we stopped at a line end, i.e. a newline or a CR and selective
8196 display is on. */
8197
8198 static enum move_it_result
8199 move_it_in_display_line_to (struct it *it,
8200 ptrdiff_t to_charpos, int to_x,
8201 enum move_operation_enum op)
8202 {
8203 enum move_it_result result = MOVE_UNDEFINED;
8204 struct glyph_row *saved_glyph_row;
8205 struct it wrap_it, atpos_it, atx_it, ppos_it;
8206 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8207 void *ppos_data = NULL;
8208 int may_wrap = 0;
8209 enum it_method prev_method = it->method;
8210 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8211 int saw_smaller_pos = prev_pos < to_charpos;
8212
8213 /* Don't produce glyphs in produce_glyphs. */
8214 saved_glyph_row = it->glyph_row;
8215 it->glyph_row = NULL;
8216
8217 /* Use wrap_it to save a copy of IT wherever a word wrap could
8218 occur. Use atpos_it to save a copy of IT at the desired buffer
8219 position, if found, so that we can scan ahead and check if the
8220 word later overshoots the window edge. Use atx_it similarly, for
8221 pixel positions. */
8222 wrap_it.sp = -1;
8223 atpos_it.sp = -1;
8224 atx_it.sp = -1;
8225
8226 /* Use ppos_it under bidi reordering to save a copy of IT for the
8227 position > CHARPOS that is the closest to CHARPOS. We restore
8228 that position in IT when we have scanned the entire display line
8229 without finding a match for CHARPOS and all the character
8230 positions are greater than CHARPOS. */
8231 if (it->bidi_p)
8232 {
8233 SAVE_IT (ppos_it, *it, ppos_data);
8234 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8235 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8236 SAVE_IT (ppos_it, *it, ppos_data);
8237 }
8238
8239 #define BUFFER_POS_REACHED_P() \
8240 ((op & MOVE_TO_POS) != 0 \
8241 && BUFFERP (it->object) \
8242 && (IT_CHARPOS (*it) == to_charpos \
8243 || ((!it->bidi_p \
8244 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8245 && IT_CHARPOS (*it) > to_charpos) \
8246 || (it->what == IT_COMPOSITION \
8247 && ((IT_CHARPOS (*it) > to_charpos \
8248 && to_charpos >= it->cmp_it.charpos) \
8249 || (IT_CHARPOS (*it) < to_charpos \
8250 && to_charpos <= it->cmp_it.charpos)))) \
8251 && (it->method == GET_FROM_BUFFER \
8252 || (it->method == GET_FROM_DISPLAY_VECTOR \
8253 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8254
8255 /* If there's a line-/wrap-prefix, handle it. */
8256 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8257 && it->current_y < it->last_visible_y)
8258 handle_line_prefix (it);
8259
8260 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8261 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8262
8263 while (1)
8264 {
8265 int x, i, ascent = 0, descent = 0;
8266
8267 /* Utility macro to reset an iterator with x, ascent, and descent. */
8268 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8269 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8270 (IT)->max_descent = descent)
8271
8272 /* Stop if we move beyond TO_CHARPOS (after an image or a
8273 display string or stretch glyph). */
8274 if ((op & MOVE_TO_POS) != 0
8275 && BUFFERP (it->object)
8276 && it->method == GET_FROM_BUFFER
8277 && (((!it->bidi_p
8278 /* When the iterator is at base embedding level, we
8279 are guaranteed that characters are delivered for
8280 display in strictly increasing order of their
8281 buffer positions. */
8282 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8283 && IT_CHARPOS (*it) > to_charpos)
8284 || (it->bidi_p
8285 && (prev_method == GET_FROM_IMAGE
8286 || prev_method == GET_FROM_STRETCH
8287 || prev_method == GET_FROM_STRING)
8288 /* Passed TO_CHARPOS from left to right. */
8289 && ((prev_pos < to_charpos
8290 && IT_CHARPOS (*it) > to_charpos)
8291 /* Passed TO_CHARPOS from right to left. */
8292 || (prev_pos > to_charpos
8293 && IT_CHARPOS (*it) < to_charpos)))))
8294 {
8295 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8296 {
8297 result = MOVE_POS_MATCH_OR_ZV;
8298 break;
8299 }
8300 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8301 /* If wrap_it is valid, the current position might be in a
8302 word that is wrapped. So, save the iterator in
8303 atpos_it and continue to see if wrapping happens. */
8304 SAVE_IT (atpos_it, *it, atpos_data);
8305 }
8306
8307 /* Stop when ZV reached.
8308 We used to stop here when TO_CHARPOS reached as well, but that is
8309 too soon if this glyph does not fit on this line. So we handle it
8310 explicitly below. */
8311 if (!get_next_display_element (it))
8312 {
8313 result = MOVE_POS_MATCH_OR_ZV;
8314 break;
8315 }
8316
8317 if (it->line_wrap == TRUNCATE)
8318 {
8319 if (BUFFER_POS_REACHED_P ())
8320 {
8321 result = MOVE_POS_MATCH_OR_ZV;
8322 break;
8323 }
8324 }
8325 else
8326 {
8327 if (it->line_wrap == WORD_WRAP)
8328 {
8329 if (IT_DISPLAYING_WHITESPACE (it))
8330 may_wrap = 1;
8331 else if (may_wrap)
8332 {
8333 /* We have reached a glyph that follows one or more
8334 whitespace characters. If the position is
8335 already found, we are done. */
8336 if (atpos_it.sp >= 0)
8337 {
8338 RESTORE_IT (it, &atpos_it, atpos_data);
8339 result = MOVE_POS_MATCH_OR_ZV;
8340 goto done;
8341 }
8342 if (atx_it.sp >= 0)
8343 {
8344 RESTORE_IT (it, &atx_it, atx_data);
8345 result = MOVE_X_REACHED;
8346 goto done;
8347 }
8348 /* Otherwise, we can wrap here. */
8349 SAVE_IT (wrap_it, *it, wrap_data);
8350 may_wrap = 0;
8351 }
8352 }
8353 }
8354
8355 /* Remember the line height for the current line, in case
8356 the next element doesn't fit on the line. */
8357 ascent = it->max_ascent;
8358 descent = it->max_descent;
8359
8360 /* The call to produce_glyphs will get the metrics of the
8361 display element IT is loaded with. Record the x-position
8362 before this display element, in case it doesn't fit on the
8363 line. */
8364 x = it->current_x;
8365
8366 PRODUCE_GLYPHS (it);
8367
8368 if (it->area != TEXT_AREA)
8369 {
8370 prev_method = it->method;
8371 if (it->method == GET_FROM_BUFFER)
8372 prev_pos = IT_CHARPOS (*it);
8373 set_iterator_to_next (it, 1);
8374 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8375 SET_TEXT_POS (this_line_min_pos,
8376 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8377 if (it->bidi_p
8378 && (op & MOVE_TO_POS)
8379 && IT_CHARPOS (*it) > to_charpos
8380 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8381 SAVE_IT (ppos_it, *it, ppos_data);
8382 continue;
8383 }
8384
8385 /* The number of glyphs we get back in IT->nglyphs will normally
8386 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8387 character on a terminal frame, or (iii) a line end. For the
8388 second case, IT->nglyphs - 1 padding glyphs will be present.
8389 (On X frames, there is only one glyph produced for a
8390 composite character.)
8391
8392 The behavior implemented below means, for continuation lines,
8393 that as many spaces of a TAB as fit on the current line are
8394 displayed there. For terminal frames, as many glyphs of a
8395 multi-glyph character are displayed in the current line, too.
8396 This is what the old redisplay code did, and we keep it that
8397 way. Under X, the whole shape of a complex character must
8398 fit on the line or it will be completely displayed in the
8399 next line.
8400
8401 Note that both for tabs and padding glyphs, all glyphs have
8402 the same width. */
8403 if (it->nglyphs)
8404 {
8405 /* More than one glyph or glyph doesn't fit on line. All
8406 glyphs have the same width. */
8407 int single_glyph_width = it->pixel_width / it->nglyphs;
8408 int new_x;
8409 int x_before_this_char = x;
8410 int hpos_before_this_char = it->hpos;
8411
8412 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8413 {
8414 new_x = x + single_glyph_width;
8415
8416 /* We want to leave anything reaching TO_X to the caller. */
8417 if ((op & MOVE_TO_X) && new_x > to_x)
8418 {
8419 if (BUFFER_POS_REACHED_P ())
8420 {
8421 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8422 goto buffer_pos_reached;
8423 if (atpos_it.sp < 0)
8424 {
8425 SAVE_IT (atpos_it, *it, atpos_data);
8426 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8427 }
8428 }
8429 else
8430 {
8431 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8432 {
8433 it->current_x = x;
8434 result = MOVE_X_REACHED;
8435 break;
8436 }
8437 if (atx_it.sp < 0)
8438 {
8439 SAVE_IT (atx_it, *it, atx_data);
8440 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8441 }
8442 }
8443 }
8444
8445 if (/* Lines are continued. */
8446 it->line_wrap != TRUNCATE
8447 && (/* And glyph doesn't fit on the line. */
8448 new_x > it->last_visible_x
8449 /* Or it fits exactly and we're on a window
8450 system frame. */
8451 || (new_x == it->last_visible_x
8452 && FRAME_WINDOW_P (it->f)
8453 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8454 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8455 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8456 {
8457 if (/* IT->hpos == 0 means the very first glyph
8458 doesn't fit on the line, e.g. a wide image. */
8459 it->hpos == 0
8460 || (new_x == it->last_visible_x
8461 && FRAME_WINDOW_P (it->f)))
8462 {
8463 ++it->hpos;
8464 it->current_x = new_x;
8465
8466 /* The character's last glyph just barely fits
8467 in this row. */
8468 if (i == it->nglyphs - 1)
8469 {
8470 /* If this is the destination position,
8471 return a position *before* it in this row,
8472 now that we know it fits in this row. */
8473 if (BUFFER_POS_REACHED_P ())
8474 {
8475 if (it->line_wrap != WORD_WRAP
8476 || wrap_it.sp < 0)
8477 {
8478 it->hpos = hpos_before_this_char;
8479 it->current_x = x_before_this_char;
8480 result = MOVE_POS_MATCH_OR_ZV;
8481 break;
8482 }
8483 if (it->line_wrap == WORD_WRAP
8484 && atpos_it.sp < 0)
8485 {
8486 SAVE_IT (atpos_it, *it, atpos_data);
8487 atpos_it.current_x = x_before_this_char;
8488 atpos_it.hpos = hpos_before_this_char;
8489 }
8490 }
8491
8492 prev_method = it->method;
8493 if (it->method == GET_FROM_BUFFER)
8494 prev_pos = IT_CHARPOS (*it);
8495 set_iterator_to_next (it, 1);
8496 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8497 SET_TEXT_POS (this_line_min_pos,
8498 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8499 /* On graphical terminals, newlines may
8500 "overflow" into the fringe if
8501 overflow-newline-into-fringe is non-nil.
8502 On text terminals, and on graphical
8503 terminals with no right margin, newlines
8504 may overflow into the last glyph on the
8505 display line.*/
8506 if (!FRAME_WINDOW_P (it->f)
8507 || ((it->bidi_p
8508 && it->bidi_it.paragraph_dir == R2L)
8509 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8510 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8511 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8512 {
8513 if (!get_next_display_element (it))
8514 {
8515 result = MOVE_POS_MATCH_OR_ZV;
8516 break;
8517 }
8518 if (BUFFER_POS_REACHED_P ())
8519 {
8520 if (ITERATOR_AT_END_OF_LINE_P (it))
8521 result = MOVE_POS_MATCH_OR_ZV;
8522 else
8523 result = MOVE_LINE_CONTINUED;
8524 break;
8525 }
8526 if (ITERATOR_AT_END_OF_LINE_P (it))
8527 {
8528 result = MOVE_NEWLINE_OR_CR;
8529 break;
8530 }
8531 }
8532 }
8533 }
8534 else
8535 IT_RESET_X_ASCENT_DESCENT (it);
8536
8537 if (wrap_it.sp >= 0)
8538 {
8539 RESTORE_IT (it, &wrap_it, wrap_data);
8540 atpos_it.sp = -1;
8541 atx_it.sp = -1;
8542 }
8543
8544 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8545 IT_CHARPOS (*it)));
8546 result = MOVE_LINE_CONTINUED;
8547 break;
8548 }
8549
8550 if (BUFFER_POS_REACHED_P ())
8551 {
8552 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8553 goto buffer_pos_reached;
8554 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8555 {
8556 SAVE_IT (atpos_it, *it, atpos_data);
8557 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8558 }
8559 }
8560
8561 if (new_x > it->first_visible_x)
8562 {
8563 /* Glyph is visible. Increment number of glyphs that
8564 would be displayed. */
8565 ++it->hpos;
8566 }
8567 }
8568
8569 if (result != MOVE_UNDEFINED)
8570 break;
8571 }
8572 else if (BUFFER_POS_REACHED_P ())
8573 {
8574 buffer_pos_reached:
8575 IT_RESET_X_ASCENT_DESCENT (it);
8576 result = MOVE_POS_MATCH_OR_ZV;
8577 break;
8578 }
8579 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8580 {
8581 /* Stop when TO_X specified and reached. This check is
8582 necessary here because of lines consisting of a line end,
8583 only. The line end will not produce any glyphs and we
8584 would never get MOVE_X_REACHED. */
8585 eassert (it->nglyphs == 0);
8586 result = MOVE_X_REACHED;
8587 break;
8588 }
8589
8590 /* Is this a line end? If yes, we're done. */
8591 if (ITERATOR_AT_END_OF_LINE_P (it))
8592 {
8593 /* If we are past TO_CHARPOS, but never saw any character
8594 positions smaller than TO_CHARPOS, return
8595 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8596 did. */
8597 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8598 {
8599 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8600 {
8601 if (IT_CHARPOS (ppos_it) < ZV)
8602 {
8603 RESTORE_IT (it, &ppos_it, ppos_data);
8604 result = MOVE_POS_MATCH_OR_ZV;
8605 }
8606 else
8607 goto buffer_pos_reached;
8608 }
8609 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8610 && IT_CHARPOS (*it) > to_charpos)
8611 goto buffer_pos_reached;
8612 else
8613 result = MOVE_NEWLINE_OR_CR;
8614 }
8615 else
8616 result = MOVE_NEWLINE_OR_CR;
8617 break;
8618 }
8619
8620 prev_method = it->method;
8621 if (it->method == GET_FROM_BUFFER)
8622 prev_pos = IT_CHARPOS (*it);
8623 /* The current display element has been consumed. Advance
8624 to the next. */
8625 set_iterator_to_next (it, 1);
8626 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8627 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8628 if (IT_CHARPOS (*it) < to_charpos)
8629 saw_smaller_pos = 1;
8630 if (it->bidi_p
8631 && (op & MOVE_TO_POS)
8632 && IT_CHARPOS (*it) >= to_charpos
8633 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8634 SAVE_IT (ppos_it, *it, ppos_data);
8635
8636 /* Stop if lines are truncated and IT's current x-position is
8637 past the right edge of the window now. */
8638 if (it->line_wrap == TRUNCATE
8639 && it->current_x >= it->last_visible_x)
8640 {
8641 if (!FRAME_WINDOW_P (it->f)
8642 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8643 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8644 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8645 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8646 {
8647 int at_eob_p = 0;
8648
8649 if ((at_eob_p = !get_next_display_element (it))
8650 || BUFFER_POS_REACHED_P ()
8651 /* If we are past TO_CHARPOS, but never saw any
8652 character positions smaller than TO_CHARPOS,
8653 return MOVE_POS_MATCH_OR_ZV, like the
8654 unidirectional display did. */
8655 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8656 && !saw_smaller_pos
8657 && IT_CHARPOS (*it) > to_charpos))
8658 {
8659 if (it->bidi_p
8660 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8661 RESTORE_IT (it, &ppos_it, ppos_data);
8662 result = MOVE_POS_MATCH_OR_ZV;
8663 break;
8664 }
8665 if (ITERATOR_AT_END_OF_LINE_P (it))
8666 {
8667 result = MOVE_NEWLINE_OR_CR;
8668 break;
8669 }
8670 }
8671 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8672 && !saw_smaller_pos
8673 && IT_CHARPOS (*it) > to_charpos)
8674 {
8675 if (IT_CHARPOS (ppos_it) < ZV)
8676 RESTORE_IT (it, &ppos_it, ppos_data);
8677 result = MOVE_POS_MATCH_OR_ZV;
8678 break;
8679 }
8680 result = MOVE_LINE_TRUNCATED;
8681 break;
8682 }
8683 #undef IT_RESET_X_ASCENT_DESCENT
8684 }
8685
8686 #undef BUFFER_POS_REACHED_P
8687
8688 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8689 restore the saved iterator. */
8690 if (atpos_it.sp >= 0)
8691 RESTORE_IT (it, &atpos_it, atpos_data);
8692 else if (atx_it.sp >= 0)
8693 RESTORE_IT (it, &atx_it, atx_data);
8694
8695 done:
8696
8697 if (atpos_data)
8698 bidi_unshelve_cache (atpos_data, 1);
8699 if (atx_data)
8700 bidi_unshelve_cache (atx_data, 1);
8701 if (wrap_data)
8702 bidi_unshelve_cache (wrap_data, 1);
8703 if (ppos_data)
8704 bidi_unshelve_cache (ppos_data, 1);
8705
8706 /* Restore the iterator settings altered at the beginning of this
8707 function. */
8708 it->glyph_row = saved_glyph_row;
8709 return result;
8710 }
8711
8712 /* For external use. */
8713 void
8714 move_it_in_display_line (struct it *it,
8715 ptrdiff_t to_charpos, int to_x,
8716 enum move_operation_enum op)
8717 {
8718 if (it->line_wrap == WORD_WRAP
8719 && (op & MOVE_TO_X))
8720 {
8721 struct it save_it;
8722 void *save_data = NULL;
8723 int skip;
8724
8725 SAVE_IT (save_it, *it, save_data);
8726 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8727 /* When word-wrap is on, TO_X may lie past the end
8728 of a wrapped line. Then it->current is the
8729 character on the next line, so backtrack to the
8730 space before the wrap point. */
8731 if (skip == MOVE_LINE_CONTINUED)
8732 {
8733 int prev_x = max (it->current_x - 1, 0);
8734 RESTORE_IT (it, &save_it, save_data);
8735 move_it_in_display_line_to
8736 (it, -1, prev_x, MOVE_TO_X);
8737 }
8738 else
8739 bidi_unshelve_cache (save_data, 1);
8740 }
8741 else
8742 move_it_in_display_line_to (it, to_charpos, to_x, op);
8743 }
8744
8745
8746 /* Move IT forward until it satisfies one or more of the criteria in
8747 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8748
8749 OP is a bit-mask that specifies where to stop, and in particular,
8750 which of those four position arguments makes a difference. See the
8751 description of enum move_operation_enum.
8752
8753 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8754 screen line, this function will set IT to the next position that is
8755 displayed to the right of TO_CHARPOS on the screen. */
8756
8757 void
8758 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8759 {
8760 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8761 int line_height, line_start_x = 0, reached = 0;
8762 void *backup_data = NULL;
8763
8764 for (;;)
8765 {
8766 if (op & MOVE_TO_VPOS)
8767 {
8768 /* If no TO_CHARPOS and no TO_X specified, stop at the
8769 start of the line TO_VPOS. */
8770 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8771 {
8772 if (it->vpos == to_vpos)
8773 {
8774 reached = 1;
8775 break;
8776 }
8777 else
8778 skip = move_it_in_display_line_to (it, -1, -1, 0);
8779 }
8780 else
8781 {
8782 /* TO_VPOS >= 0 means stop at TO_X in the line at
8783 TO_VPOS, or at TO_POS, whichever comes first. */
8784 if (it->vpos == to_vpos)
8785 {
8786 reached = 2;
8787 break;
8788 }
8789
8790 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8791
8792 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8793 {
8794 reached = 3;
8795 break;
8796 }
8797 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8798 {
8799 /* We have reached TO_X but not in the line we want. */
8800 skip = move_it_in_display_line_to (it, to_charpos,
8801 -1, MOVE_TO_POS);
8802 if (skip == MOVE_POS_MATCH_OR_ZV)
8803 {
8804 reached = 4;
8805 break;
8806 }
8807 }
8808 }
8809 }
8810 else if (op & MOVE_TO_Y)
8811 {
8812 struct it it_backup;
8813
8814 if (it->line_wrap == WORD_WRAP)
8815 SAVE_IT (it_backup, *it, backup_data);
8816
8817 /* TO_Y specified means stop at TO_X in the line containing
8818 TO_Y---or at TO_CHARPOS if this is reached first. The
8819 problem is that we can't really tell whether the line
8820 contains TO_Y before we have completely scanned it, and
8821 this may skip past TO_X. What we do is to first scan to
8822 TO_X.
8823
8824 If TO_X is not specified, use a TO_X of zero. The reason
8825 is to make the outcome of this function more predictable.
8826 If we didn't use TO_X == 0, we would stop at the end of
8827 the line which is probably not what a caller would expect
8828 to happen. */
8829 skip = move_it_in_display_line_to
8830 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8831 (MOVE_TO_X | (op & MOVE_TO_POS)));
8832
8833 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8834 if (skip == MOVE_POS_MATCH_OR_ZV)
8835 reached = 5;
8836 else if (skip == MOVE_X_REACHED)
8837 {
8838 /* If TO_X was reached, we want to know whether TO_Y is
8839 in the line. We know this is the case if the already
8840 scanned glyphs make the line tall enough. Otherwise,
8841 we must check by scanning the rest of the line. */
8842 line_height = it->max_ascent + it->max_descent;
8843 if (to_y >= it->current_y
8844 && to_y < it->current_y + line_height)
8845 {
8846 reached = 6;
8847 break;
8848 }
8849 SAVE_IT (it_backup, *it, backup_data);
8850 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8851 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8852 op & MOVE_TO_POS);
8853 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8854 line_height = it->max_ascent + it->max_descent;
8855 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8856
8857 if (to_y >= it->current_y
8858 && to_y < it->current_y + line_height)
8859 {
8860 /* If TO_Y is in this line and TO_X was reached
8861 above, we scanned too far. We have to restore
8862 IT's settings to the ones before skipping. But
8863 keep the more accurate values of max_ascent and
8864 max_descent we've found while skipping the rest
8865 of the line, for the sake of callers, such as
8866 pos_visible_p, that need to know the line
8867 height. */
8868 int max_ascent = it->max_ascent;
8869 int max_descent = it->max_descent;
8870
8871 RESTORE_IT (it, &it_backup, backup_data);
8872 it->max_ascent = max_ascent;
8873 it->max_descent = max_descent;
8874 reached = 6;
8875 }
8876 else
8877 {
8878 skip = skip2;
8879 if (skip == MOVE_POS_MATCH_OR_ZV)
8880 reached = 7;
8881 }
8882 }
8883 else
8884 {
8885 /* Check whether TO_Y is in this line. */
8886 line_height = it->max_ascent + it->max_descent;
8887 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8888
8889 if (to_y >= it->current_y
8890 && to_y < it->current_y + line_height)
8891 {
8892 /* When word-wrap is on, TO_X may lie past the end
8893 of a wrapped line. Then it->current is the
8894 character on the next line, so backtrack to the
8895 space before the wrap point. */
8896 if (skip == MOVE_LINE_CONTINUED
8897 && it->line_wrap == WORD_WRAP)
8898 {
8899 int prev_x = max (it->current_x - 1, 0);
8900 RESTORE_IT (it, &it_backup, backup_data);
8901 skip = move_it_in_display_line_to
8902 (it, -1, prev_x, MOVE_TO_X);
8903 }
8904 reached = 6;
8905 }
8906 }
8907
8908 if (reached)
8909 break;
8910 }
8911 else if (BUFFERP (it->object)
8912 && (it->method == GET_FROM_BUFFER
8913 || it->method == GET_FROM_STRETCH)
8914 && IT_CHARPOS (*it) >= to_charpos
8915 /* Under bidi iteration, a call to set_iterator_to_next
8916 can scan far beyond to_charpos if the initial
8917 portion of the next line needs to be reordered. In
8918 that case, give move_it_in_display_line_to another
8919 chance below. */
8920 && !(it->bidi_p
8921 && it->bidi_it.scan_dir == -1))
8922 skip = MOVE_POS_MATCH_OR_ZV;
8923 else
8924 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8925
8926 switch (skip)
8927 {
8928 case MOVE_POS_MATCH_OR_ZV:
8929 reached = 8;
8930 goto out;
8931
8932 case MOVE_NEWLINE_OR_CR:
8933 set_iterator_to_next (it, 1);
8934 it->continuation_lines_width = 0;
8935 break;
8936
8937 case MOVE_LINE_TRUNCATED:
8938 it->continuation_lines_width = 0;
8939 reseat_at_next_visible_line_start (it, 0);
8940 if ((op & MOVE_TO_POS) != 0
8941 && IT_CHARPOS (*it) > to_charpos)
8942 {
8943 reached = 9;
8944 goto out;
8945 }
8946 break;
8947
8948 case MOVE_LINE_CONTINUED:
8949 /* For continued lines ending in a tab, some of the glyphs
8950 associated with the tab are displayed on the current
8951 line. Since it->current_x does not include these glyphs,
8952 we use it->last_visible_x instead. */
8953 if (it->c == '\t')
8954 {
8955 it->continuation_lines_width += it->last_visible_x;
8956 /* When moving by vpos, ensure that the iterator really
8957 advances to the next line (bug#847, bug#969). Fixme:
8958 do we need to do this in other circumstances? */
8959 if (it->current_x != it->last_visible_x
8960 && (op & MOVE_TO_VPOS)
8961 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8962 {
8963 line_start_x = it->current_x + it->pixel_width
8964 - it->last_visible_x;
8965 set_iterator_to_next (it, 0);
8966 }
8967 }
8968 else
8969 it->continuation_lines_width += it->current_x;
8970 break;
8971
8972 default:
8973 emacs_abort ();
8974 }
8975
8976 /* Reset/increment for the next run. */
8977 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8978 it->current_x = line_start_x;
8979 line_start_x = 0;
8980 it->hpos = 0;
8981 it->current_y += it->max_ascent + it->max_descent;
8982 ++it->vpos;
8983 last_height = it->max_ascent + it->max_descent;
8984 last_max_ascent = it->max_ascent;
8985 it->max_ascent = it->max_descent = 0;
8986 }
8987
8988 out:
8989
8990 /* On text terminals, we may stop at the end of a line in the middle
8991 of a multi-character glyph. If the glyph itself is continued,
8992 i.e. it is actually displayed on the next line, don't treat this
8993 stopping point as valid; move to the next line instead (unless
8994 that brings us offscreen). */
8995 if (!FRAME_WINDOW_P (it->f)
8996 && op & MOVE_TO_POS
8997 && IT_CHARPOS (*it) == to_charpos
8998 && it->what == IT_CHARACTER
8999 && it->nglyphs > 1
9000 && it->line_wrap == WINDOW_WRAP
9001 && it->current_x == it->last_visible_x - 1
9002 && it->c != '\n'
9003 && it->c != '\t'
9004 && it->vpos < XFASTINT (it->w->window_end_vpos))
9005 {
9006 it->continuation_lines_width += it->current_x;
9007 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9008 it->current_y += it->max_ascent + it->max_descent;
9009 ++it->vpos;
9010 last_height = it->max_ascent + it->max_descent;
9011 last_max_ascent = it->max_ascent;
9012 }
9013
9014 if (backup_data)
9015 bidi_unshelve_cache (backup_data, 1);
9016
9017 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9018 }
9019
9020
9021 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9022
9023 If DY > 0, move IT backward at least that many pixels. DY = 0
9024 means move IT backward to the preceding line start or BEGV. This
9025 function may move over more than DY pixels if IT->current_y - DY
9026 ends up in the middle of a line; in this case IT->current_y will be
9027 set to the top of the line moved to. */
9028
9029 void
9030 move_it_vertically_backward (struct it *it, int dy)
9031 {
9032 int nlines, h;
9033 struct it it2, it3;
9034 void *it2data = NULL, *it3data = NULL;
9035 ptrdiff_t start_pos;
9036 int nchars_per_row
9037 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9038 ptrdiff_t pos_limit;
9039
9040 move_further_back:
9041 eassert (dy >= 0);
9042
9043 start_pos = IT_CHARPOS (*it);
9044
9045 /* Estimate how many newlines we must move back. */
9046 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9047 if (it->line_wrap == TRUNCATE)
9048 pos_limit = BEGV;
9049 else
9050 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9051
9052 /* Set the iterator's position that many lines back. But don't go
9053 back more than NLINES full screen lines -- this wins a day with
9054 buffers which have very long lines. */
9055 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9056 back_to_previous_visible_line_start (it);
9057
9058 /* Reseat the iterator here. When moving backward, we don't want
9059 reseat to skip forward over invisible text, set up the iterator
9060 to deliver from overlay strings at the new position etc. So,
9061 use reseat_1 here. */
9062 reseat_1 (it, it->current.pos, 1);
9063
9064 /* We are now surely at a line start. */
9065 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9066 reordering is in effect. */
9067 it->continuation_lines_width = 0;
9068
9069 /* Move forward and see what y-distance we moved. First move to the
9070 start of the next line so that we get its height. We need this
9071 height to be able to tell whether we reached the specified
9072 y-distance. */
9073 SAVE_IT (it2, *it, it2data);
9074 it2.max_ascent = it2.max_descent = 0;
9075 do
9076 {
9077 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9078 MOVE_TO_POS | MOVE_TO_VPOS);
9079 }
9080 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9081 /* If we are in a display string which starts at START_POS,
9082 and that display string includes a newline, and we are
9083 right after that newline (i.e. at the beginning of a
9084 display line), exit the loop, because otherwise we will
9085 infloop, since move_it_to will see that it is already at
9086 START_POS and will not move. */
9087 || (it2.method == GET_FROM_STRING
9088 && IT_CHARPOS (it2) == start_pos
9089 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9090 eassert (IT_CHARPOS (*it) >= BEGV);
9091 SAVE_IT (it3, it2, it3data);
9092
9093 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9094 eassert (IT_CHARPOS (*it) >= BEGV);
9095 /* H is the actual vertical distance from the position in *IT
9096 and the starting position. */
9097 h = it2.current_y - it->current_y;
9098 /* NLINES is the distance in number of lines. */
9099 nlines = it2.vpos - it->vpos;
9100
9101 /* Correct IT's y and vpos position
9102 so that they are relative to the starting point. */
9103 it->vpos -= nlines;
9104 it->current_y -= h;
9105
9106 if (dy == 0)
9107 {
9108 /* DY == 0 means move to the start of the screen line. The
9109 value of nlines is > 0 if continuation lines were involved,
9110 or if the original IT position was at start of a line. */
9111 RESTORE_IT (it, it, it2data);
9112 if (nlines > 0)
9113 move_it_by_lines (it, nlines);
9114 /* The above code moves us to some position NLINES down,
9115 usually to its first glyph (leftmost in an L2R line), but
9116 that's not necessarily the start of the line, under bidi
9117 reordering. We want to get to the character position
9118 that is immediately after the newline of the previous
9119 line. */
9120 if (it->bidi_p
9121 && !it->continuation_lines_width
9122 && !STRINGP (it->string)
9123 && IT_CHARPOS (*it) > BEGV
9124 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9125 {
9126 ptrdiff_t nl_pos =
9127 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1, NULL);
9128
9129 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9130 }
9131 bidi_unshelve_cache (it3data, 1);
9132 }
9133 else
9134 {
9135 /* The y-position we try to reach, relative to *IT.
9136 Note that H has been subtracted in front of the if-statement. */
9137 int target_y = it->current_y + h - dy;
9138 int y0 = it3.current_y;
9139 int y1;
9140 int line_height;
9141
9142 RESTORE_IT (&it3, &it3, it3data);
9143 y1 = line_bottom_y (&it3);
9144 line_height = y1 - y0;
9145 RESTORE_IT (it, it, it2data);
9146 /* If we did not reach target_y, try to move further backward if
9147 we can. If we moved too far backward, try to move forward. */
9148 if (target_y < it->current_y
9149 /* This is heuristic. In a window that's 3 lines high, with
9150 a line height of 13 pixels each, recentering with point
9151 on the bottom line will try to move -39/2 = 19 pixels
9152 backward. Try to avoid moving into the first line. */
9153 && (it->current_y - target_y
9154 > min (window_box_height (it->w), line_height * 2 / 3))
9155 && IT_CHARPOS (*it) > BEGV)
9156 {
9157 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9158 target_y - it->current_y));
9159 dy = it->current_y - target_y;
9160 goto move_further_back;
9161 }
9162 else if (target_y >= it->current_y + line_height
9163 && IT_CHARPOS (*it) < ZV)
9164 {
9165 /* Should move forward by at least one line, maybe more.
9166
9167 Note: Calling move_it_by_lines can be expensive on
9168 terminal frames, where compute_motion is used (via
9169 vmotion) to do the job, when there are very long lines
9170 and truncate-lines is nil. That's the reason for
9171 treating terminal frames specially here. */
9172
9173 if (!FRAME_WINDOW_P (it->f))
9174 move_it_vertically (it, target_y - (it->current_y + line_height));
9175 else
9176 {
9177 do
9178 {
9179 move_it_by_lines (it, 1);
9180 }
9181 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9182 }
9183 }
9184 }
9185 }
9186
9187
9188 /* Move IT by a specified amount of pixel lines DY. DY negative means
9189 move backwards. DY = 0 means move to start of screen line. At the
9190 end, IT will be on the start of a screen line. */
9191
9192 void
9193 move_it_vertically (struct it *it, int dy)
9194 {
9195 if (dy <= 0)
9196 move_it_vertically_backward (it, -dy);
9197 else
9198 {
9199 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9200 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9201 MOVE_TO_POS | MOVE_TO_Y);
9202 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9203
9204 /* If buffer ends in ZV without a newline, move to the start of
9205 the line to satisfy the post-condition. */
9206 if (IT_CHARPOS (*it) == ZV
9207 && ZV > BEGV
9208 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9209 move_it_by_lines (it, 0);
9210 }
9211 }
9212
9213
9214 /* Move iterator IT past the end of the text line it is in. */
9215
9216 void
9217 move_it_past_eol (struct it *it)
9218 {
9219 enum move_it_result rc;
9220
9221 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9222 if (rc == MOVE_NEWLINE_OR_CR)
9223 set_iterator_to_next (it, 0);
9224 }
9225
9226
9227 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9228 negative means move up. DVPOS == 0 means move to the start of the
9229 screen line.
9230
9231 Optimization idea: If we would know that IT->f doesn't use
9232 a face with proportional font, we could be faster for
9233 truncate-lines nil. */
9234
9235 void
9236 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9237 {
9238
9239 /* The commented-out optimization uses vmotion on terminals. This
9240 gives bad results, because elements like it->what, on which
9241 callers such as pos_visible_p rely, aren't updated. */
9242 /* struct position pos;
9243 if (!FRAME_WINDOW_P (it->f))
9244 {
9245 struct text_pos textpos;
9246
9247 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9248 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9249 reseat (it, textpos, 1);
9250 it->vpos += pos.vpos;
9251 it->current_y += pos.vpos;
9252 }
9253 else */
9254
9255 if (dvpos == 0)
9256 {
9257 /* DVPOS == 0 means move to the start of the screen line. */
9258 move_it_vertically_backward (it, 0);
9259 /* Let next call to line_bottom_y calculate real line height */
9260 last_height = 0;
9261 }
9262 else if (dvpos > 0)
9263 {
9264 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9265 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9266 {
9267 /* Only move to the next buffer position if we ended up in a
9268 string from display property, not in an overlay string
9269 (before-string or after-string). That is because the
9270 latter don't conceal the underlying buffer position, so
9271 we can ask to move the iterator to the exact position we
9272 are interested in. Note that, even if we are already at
9273 IT_CHARPOS (*it), the call below is not a no-op, as it
9274 will detect that we are at the end of the string, pop the
9275 iterator, and compute it->current_x and it->hpos
9276 correctly. */
9277 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9278 -1, -1, -1, MOVE_TO_POS);
9279 }
9280 }
9281 else
9282 {
9283 struct it it2;
9284 void *it2data = NULL;
9285 ptrdiff_t start_charpos, i;
9286 int nchars_per_row
9287 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9288 ptrdiff_t pos_limit;
9289
9290 /* Start at the beginning of the screen line containing IT's
9291 position. This may actually move vertically backwards,
9292 in case of overlays, so adjust dvpos accordingly. */
9293 dvpos += it->vpos;
9294 move_it_vertically_backward (it, 0);
9295 dvpos -= it->vpos;
9296
9297 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9298 screen lines, and reseat the iterator there. */
9299 start_charpos = IT_CHARPOS (*it);
9300 if (it->line_wrap == TRUNCATE)
9301 pos_limit = BEGV;
9302 else
9303 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9304 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9305 back_to_previous_visible_line_start (it);
9306 reseat (it, it->current.pos, 1);
9307
9308 /* Move further back if we end up in a string or an image. */
9309 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9310 {
9311 /* First try to move to start of display line. */
9312 dvpos += it->vpos;
9313 move_it_vertically_backward (it, 0);
9314 dvpos -= it->vpos;
9315 if (IT_POS_VALID_AFTER_MOVE_P (it))
9316 break;
9317 /* If start of line is still in string or image,
9318 move further back. */
9319 back_to_previous_visible_line_start (it);
9320 reseat (it, it->current.pos, 1);
9321 dvpos--;
9322 }
9323
9324 it->current_x = it->hpos = 0;
9325
9326 /* Above call may have moved too far if continuation lines
9327 are involved. Scan forward and see if it did. */
9328 SAVE_IT (it2, *it, it2data);
9329 it2.vpos = it2.current_y = 0;
9330 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9331 it->vpos -= it2.vpos;
9332 it->current_y -= it2.current_y;
9333 it->current_x = it->hpos = 0;
9334
9335 /* If we moved too far back, move IT some lines forward. */
9336 if (it2.vpos > -dvpos)
9337 {
9338 int delta = it2.vpos + dvpos;
9339
9340 RESTORE_IT (&it2, &it2, it2data);
9341 SAVE_IT (it2, *it, it2data);
9342 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9343 /* Move back again if we got too far ahead. */
9344 if (IT_CHARPOS (*it) >= start_charpos)
9345 RESTORE_IT (it, &it2, it2data);
9346 else
9347 bidi_unshelve_cache (it2data, 1);
9348 }
9349 else
9350 RESTORE_IT (it, it, it2data);
9351 }
9352 }
9353
9354 /* Return 1 if IT points into the middle of a display vector. */
9355
9356 int
9357 in_display_vector_p (struct it *it)
9358 {
9359 return (it->method == GET_FROM_DISPLAY_VECTOR
9360 && it->current.dpvec_index > 0
9361 && it->dpvec + it->current.dpvec_index != it->dpend);
9362 }
9363
9364 \f
9365 /***********************************************************************
9366 Messages
9367 ***********************************************************************/
9368
9369
9370 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9371 to *Messages*. */
9372
9373 void
9374 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9375 {
9376 Lisp_Object args[3];
9377 Lisp_Object msg, fmt;
9378 char *buffer;
9379 ptrdiff_t len;
9380 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9381 USE_SAFE_ALLOCA;
9382
9383 fmt = msg = Qnil;
9384 GCPRO4 (fmt, msg, arg1, arg2);
9385
9386 args[0] = fmt = build_string (format);
9387 args[1] = arg1;
9388 args[2] = arg2;
9389 msg = Fformat (3, args);
9390
9391 len = SBYTES (msg) + 1;
9392 buffer = SAFE_ALLOCA (len);
9393 memcpy (buffer, SDATA (msg), len);
9394
9395 message_dolog (buffer, len - 1, 1, 0);
9396 SAFE_FREE ();
9397
9398 UNGCPRO;
9399 }
9400
9401
9402 /* Output a newline in the *Messages* buffer if "needs" one. */
9403
9404 void
9405 message_log_maybe_newline (void)
9406 {
9407 if (message_log_need_newline)
9408 message_dolog ("", 0, 1, 0);
9409 }
9410
9411
9412 /* Add a string M of length NBYTES to the message log, optionally
9413 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9414 true, means interpret the contents of M as multibyte. This
9415 function calls low-level routines in order to bypass text property
9416 hooks, etc. which might not be safe to run.
9417
9418 This may GC (insert may run before/after change hooks),
9419 so the buffer M must NOT point to a Lisp string. */
9420
9421 void
9422 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9423 {
9424 const unsigned char *msg = (const unsigned char *) m;
9425
9426 if (!NILP (Vmemory_full))
9427 return;
9428
9429 if (!NILP (Vmessage_log_max))
9430 {
9431 struct buffer *oldbuf;
9432 Lisp_Object oldpoint, oldbegv, oldzv;
9433 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9434 ptrdiff_t point_at_end = 0;
9435 ptrdiff_t zv_at_end = 0;
9436 Lisp_Object old_deactivate_mark;
9437 bool shown;
9438 struct gcpro gcpro1;
9439
9440 old_deactivate_mark = Vdeactivate_mark;
9441 oldbuf = current_buffer;
9442 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9443 bset_undo_list (current_buffer, Qt);
9444
9445 oldpoint = message_dolog_marker1;
9446 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9447 oldbegv = message_dolog_marker2;
9448 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9449 oldzv = message_dolog_marker3;
9450 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9451 GCPRO1 (old_deactivate_mark);
9452
9453 if (PT == Z)
9454 point_at_end = 1;
9455 if (ZV == Z)
9456 zv_at_end = 1;
9457
9458 BEGV = BEG;
9459 BEGV_BYTE = BEG_BYTE;
9460 ZV = Z;
9461 ZV_BYTE = Z_BYTE;
9462 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9463
9464 /* Insert the string--maybe converting multibyte to single byte
9465 or vice versa, so that all the text fits the buffer. */
9466 if (multibyte
9467 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9468 {
9469 ptrdiff_t i;
9470 int c, char_bytes;
9471 char work[1];
9472
9473 /* Convert a multibyte string to single-byte
9474 for the *Message* buffer. */
9475 for (i = 0; i < nbytes; i += char_bytes)
9476 {
9477 c = string_char_and_length (msg + i, &char_bytes);
9478 work[0] = (ASCII_CHAR_P (c)
9479 ? c
9480 : multibyte_char_to_unibyte (c));
9481 insert_1_both (work, 1, 1, 1, 0, 0);
9482 }
9483 }
9484 else if (! multibyte
9485 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9486 {
9487 ptrdiff_t i;
9488 int c, char_bytes;
9489 unsigned char str[MAX_MULTIBYTE_LENGTH];
9490 /* Convert a single-byte string to multibyte
9491 for the *Message* buffer. */
9492 for (i = 0; i < nbytes; i++)
9493 {
9494 c = msg[i];
9495 MAKE_CHAR_MULTIBYTE (c);
9496 char_bytes = CHAR_STRING (c, str);
9497 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9498 }
9499 }
9500 else if (nbytes)
9501 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9502
9503 if (nlflag)
9504 {
9505 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9506 printmax_t dups;
9507
9508 insert_1_both ("\n", 1, 1, 1, 0, 0);
9509
9510 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9511 this_bol = PT;
9512 this_bol_byte = PT_BYTE;
9513
9514 /* See if this line duplicates the previous one.
9515 If so, combine duplicates. */
9516 if (this_bol > BEG)
9517 {
9518 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9519 prev_bol = PT;
9520 prev_bol_byte = PT_BYTE;
9521
9522 dups = message_log_check_duplicate (prev_bol_byte,
9523 this_bol_byte);
9524 if (dups)
9525 {
9526 del_range_both (prev_bol, prev_bol_byte,
9527 this_bol, this_bol_byte, 0);
9528 if (dups > 1)
9529 {
9530 char dupstr[sizeof " [ times]"
9531 + INT_STRLEN_BOUND (printmax_t)];
9532
9533 /* If you change this format, don't forget to also
9534 change message_log_check_duplicate. */
9535 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9536 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9537 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9538 }
9539 }
9540 }
9541
9542 /* If we have more than the desired maximum number of lines
9543 in the *Messages* buffer now, delete the oldest ones.
9544 This is safe because we don't have undo in this buffer. */
9545
9546 if (NATNUMP (Vmessage_log_max))
9547 {
9548 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9549 -XFASTINT (Vmessage_log_max) - 1, 0);
9550 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9551 }
9552 }
9553 BEGV = marker_position (oldbegv);
9554 BEGV_BYTE = marker_byte_position (oldbegv);
9555
9556 if (zv_at_end)
9557 {
9558 ZV = Z;
9559 ZV_BYTE = Z_BYTE;
9560 }
9561 else
9562 {
9563 ZV = marker_position (oldzv);
9564 ZV_BYTE = marker_byte_position (oldzv);
9565 }
9566
9567 if (point_at_end)
9568 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9569 else
9570 /* We can't do Fgoto_char (oldpoint) because it will run some
9571 Lisp code. */
9572 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9573 marker_byte_position (oldpoint));
9574
9575 UNGCPRO;
9576 unchain_marker (XMARKER (oldpoint));
9577 unchain_marker (XMARKER (oldbegv));
9578 unchain_marker (XMARKER (oldzv));
9579
9580 shown = buffer_window_count (current_buffer) > 0;
9581 set_buffer_internal (oldbuf);
9582 if (!shown)
9583 windows_or_buffers_changed = old_windows_or_buffers_changed;
9584 message_log_need_newline = !nlflag;
9585 Vdeactivate_mark = old_deactivate_mark;
9586 }
9587 }
9588
9589
9590 /* We are at the end of the buffer after just having inserted a newline.
9591 (Note: We depend on the fact we won't be crossing the gap.)
9592 Check to see if the most recent message looks a lot like the previous one.
9593 Return 0 if different, 1 if the new one should just replace it, or a
9594 value N > 1 if we should also append " [N times]". */
9595
9596 static intmax_t
9597 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9598 {
9599 ptrdiff_t i;
9600 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9601 int seen_dots = 0;
9602 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9603 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9604
9605 for (i = 0; i < len; i++)
9606 {
9607 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9608 seen_dots = 1;
9609 if (p1[i] != p2[i])
9610 return seen_dots;
9611 }
9612 p1 += len;
9613 if (*p1 == '\n')
9614 return 2;
9615 if (*p1++ == ' ' && *p1++ == '[')
9616 {
9617 char *pend;
9618 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9619 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9620 return n + 1;
9621 }
9622 return 0;
9623 }
9624 \f
9625
9626 /* Display an echo area message M with a specified length of NBYTES
9627 bytes. The string may include null characters. If M is not a
9628 string, clear out any existing message, and let the mini-buffer
9629 text show through.
9630
9631 This function cancels echoing. */
9632
9633 void
9634 message3 (Lisp_Object m)
9635 {
9636 struct gcpro gcpro1;
9637
9638 GCPRO1 (m);
9639 clear_message (1,1);
9640 cancel_echoing ();
9641
9642 /* First flush out any partial line written with print. */
9643 message_log_maybe_newline ();
9644 if (STRINGP (m))
9645 {
9646 ptrdiff_t nbytes = SBYTES (m);
9647 bool multibyte = STRING_MULTIBYTE (m);
9648 USE_SAFE_ALLOCA;
9649 char *buffer = SAFE_ALLOCA (nbytes);
9650 memcpy (buffer, SDATA (m), nbytes);
9651 message_dolog (buffer, nbytes, 1, multibyte);
9652 SAFE_FREE ();
9653 }
9654 message3_nolog (m);
9655
9656 UNGCPRO;
9657 }
9658
9659
9660 /* The non-logging version of message3.
9661 This does not cancel echoing, because it is used for echoing.
9662 Perhaps we need to make a separate function for echoing
9663 and make this cancel echoing. */
9664
9665 void
9666 message3_nolog (Lisp_Object m)
9667 {
9668 struct frame *sf = SELECTED_FRAME ();
9669
9670 if (FRAME_INITIAL_P (sf))
9671 {
9672 if (noninteractive_need_newline)
9673 putc ('\n', stderr);
9674 noninteractive_need_newline = 0;
9675 if (STRINGP (m))
9676 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9677 if (cursor_in_echo_area == 0)
9678 fprintf (stderr, "\n");
9679 fflush (stderr);
9680 }
9681 /* Error messages get reported properly by cmd_error, so this must be just an
9682 informative message; if the frame hasn't really been initialized yet, just
9683 toss it. */
9684 else if (INTERACTIVE && sf->glyphs_initialized_p)
9685 {
9686 /* Get the frame containing the mini-buffer
9687 that the selected frame is using. */
9688 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9689 Lisp_Object frame = XWINDOW (mini_window)->frame;
9690 struct frame *f = XFRAME (frame);
9691
9692 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9693 Fmake_frame_visible (frame);
9694
9695 if (STRINGP (m) && SCHARS (m) > 0)
9696 {
9697 set_message (m);
9698 if (minibuffer_auto_raise)
9699 Fraise_frame (frame);
9700 /* Assume we are not echoing.
9701 (If we are, echo_now will override this.) */
9702 echo_message_buffer = Qnil;
9703 }
9704 else
9705 clear_message (1, 1);
9706
9707 do_pending_window_change (0);
9708 echo_area_display (1);
9709 do_pending_window_change (0);
9710 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9711 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9712 }
9713 }
9714
9715
9716 /* Display a null-terminated echo area message M. If M is 0, clear
9717 out any existing message, and let the mini-buffer text show through.
9718
9719 The buffer M must continue to exist until after the echo area gets
9720 cleared or some other message gets displayed there. Do not pass
9721 text that is stored in a Lisp string. Do not pass text in a buffer
9722 that was alloca'd. */
9723
9724 void
9725 message1 (const char *m)
9726 {
9727 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9728 }
9729
9730
9731 /* The non-logging counterpart of message1. */
9732
9733 void
9734 message1_nolog (const char *m)
9735 {
9736 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9737 }
9738
9739 /* Display a message M which contains a single %s
9740 which gets replaced with STRING. */
9741
9742 void
9743 message_with_string (const char *m, Lisp_Object string, int log)
9744 {
9745 CHECK_STRING (string);
9746
9747 if (noninteractive)
9748 {
9749 if (m)
9750 {
9751 if (noninteractive_need_newline)
9752 putc ('\n', stderr);
9753 noninteractive_need_newline = 0;
9754 fprintf (stderr, m, SDATA (string));
9755 if (!cursor_in_echo_area)
9756 fprintf (stderr, "\n");
9757 fflush (stderr);
9758 }
9759 }
9760 else if (INTERACTIVE)
9761 {
9762 /* The frame whose minibuffer we're going to display the message on.
9763 It may be larger than the selected frame, so we need
9764 to use its buffer, not the selected frame's buffer. */
9765 Lisp_Object mini_window;
9766 struct frame *f, *sf = SELECTED_FRAME ();
9767
9768 /* Get the frame containing the minibuffer
9769 that the selected frame is using. */
9770 mini_window = FRAME_MINIBUF_WINDOW (sf);
9771 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9772
9773 /* Error messages get reported properly by cmd_error, so this must be
9774 just an informative message; if the frame hasn't really been
9775 initialized yet, just toss it. */
9776 if (f->glyphs_initialized_p)
9777 {
9778 Lisp_Object args[2], msg;
9779 struct gcpro gcpro1, gcpro2;
9780
9781 args[0] = build_string (m);
9782 args[1] = msg = string;
9783 GCPRO2 (args[0], msg);
9784 gcpro1.nvars = 2;
9785
9786 msg = Fformat (2, args);
9787
9788 if (log)
9789 message3 (msg);
9790 else
9791 message3_nolog (msg);
9792
9793 UNGCPRO;
9794
9795 /* Print should start at the beginning of the message
9796 buffer next time. */
9797 message_buf_print = 0;
9798 }
9799 }
9800 }
9801
9802
9803 /* Dump an informative message to the minibuf. If M is 0, clear out
9804 any existing message, and let the mini-buffer text show through. */
9805
9806 static void
9807 vmessage (const char *m, va_list ap)
9808 {
9809 if (noninteractive)
9810 {
9811 if (m)
9812 {
9813 if (noninteractive_need_newline)
9814 putc ('\n', stderr);
9815 noninteractive_need_newline = 0;
9816 vfprintf (stderr, m, ap);
9817 if (cursor_in_echo_area == 0)
9818 fprintf (stderr, "\n");
9819 fflush (stderr);
9820 }
9821 }
9822 else if (INTERACTIVE)
9823 {
9824 /* The frame whose mini-buffer we're going to display the message
9825 on. It may be larger than the selected frame, so we need to
9826 use its buffer, not the selected frame's buffer. */
9827 Lisp_Object mini_window;
9828 struct frame *f, *sf = SELECTED_FRAME ();
9829
9830 /* Get the frame containing the mini-buffer
9831 that the selected frame is using. */
9832 mini_window = FRAME_MINIBUF_WINDOW (sf);
9833 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9834
9835 /* Error messages get reported properly by cmd_error, so this must be
9836 just an informative message; if the frame hasn't really been
9837 initialized yet, just toss it. */
9838 if (f->glyphs_initialized_p)
9839 {
9840 if (m)
9841 {
9842 ptrdiff_t len;
9843 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9844 char *message_buf = alloca (maxsize + 1);
9845
9846 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9847
9848 message3 (make_string (message_buf, len));
9849 }
9850 else
9851 message1 (0);
9852
9853 /* Print should start at the beginning of the message
9854 buffer next time. */
9855 message_buf_print = 0;
9856 }
9857 }
9858 }
9859
9860 void
9861 message (const char *m, ...)
9862 {
9863 va_list ap;
9864 va_start (ap, m);
9865 vmessage (m, ap);
9866 va_end (ap);
9867 }
9868
9869
9870 #if 0
9871 /* The non-logging version of message. */
9872
9873 void
9874 message_nolog (const char *m, ...)
9875 {
9876 Lisp_Object old_log_max;
9877 va_list ap;
9878 va_start (ap, m);
9879 old_log_max = Vmessage_log_max;
9880 Vmessage_log_max = Qnil;
9881 vmessage (m, ap);
9882 Vmessage_log_max = old_log_max;
9883 va_end (ap);
9884 }
9885 #endif
9886
9887
9888 /* Display the current message in the current mini-buffer. This is
9889 only called from error handlers in process.c, and is not time
9890 critical. */
9891
9892 void
9893 update_echo_area (void)
9894 {
9895 if (!NILP (echo_area_buffer[0]))
9896 {
9897 Lisp_Object string;
9898 string = Fcurrent_message ();
9899 message3 (string);
9900 }
9901 }
9902
9903
9904 /* Make sure echo area buffers in `echo_buffers' are live.
9905 If they aren't, make new ones. */
9906
9907 static void
9908 ensure_echo_area_buffers (void)
9909 {
9910 int i;
9911
9912 for (i = 0; i < 2; ++i)
9913 if (!BUFFERP (echo_buffer[i])
9914 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9915 {
9916 char name[30];
9917 Lisp_Object old_buffer;
9918 int j;
9919
9920 old_buffer = echo_buffer[i];
9921 echo_buffer[i] = Fget_buffer_create
9922 (make_formatted_string (name, " *Echo Area %d*", i));
9923 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9924 /* to force word wrap in echo area -
9925 it was decided to postpone this*/
9926 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9927
9928 for (j = 0; j < 2; ++j)
9929 if (EQ (old_buffer, echo_area_buffer[j]))
9930 echo_area_buffer[j] = echo_buffer[i];
9931 }
9932 }
9933
9934
9935 /* Call FN with args A1..A2 with either the current or last displayed
9936 echo_area_buffer as current buffer.
9937
9938 WHICH zero means use the current message buffer
9939 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9940 from echo_buffer[] and clear it.
9941
9942 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9943 suitable buffer from echo_buffer[] and clear it.
9944
9945 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9946 that the current message becomes the last displayed one, make
9947 choose a suitable buffer for echo_area_buffer[0], and clear it.
9948
9949 Value is what FN returns. */
9950
9951 static int
9952 with_echo_area_buffer (struct window *w, int which,
9953 int (*fn) (ptrdiff_t, Lisp_Object),
9954 ptrdiff_t a1, Lisp_Object a2)
9955 {
9956 Lisp_Object buffer;
9957 int this_one, the_other, clear_buffer_p, rc;
9958 ptrdiff_t count = SPECPDL_INDEX ();
9959
9960 /* If buffers aren't live, make new ones. */
9961 ensure_echo_area_buffers ();
9962
9963 clear_buffer_p = 0;
9964
9965 if (which == 0)
9966 this_one = 0, the_other = 1;
9967 else if (which > 0)
9968 this_one = 1, the_other = 0;
9969 else
9970 {
9971 this_one = 0, the_other = 1;
9972 clear_buffer_p = 1;
9973
9974 /* We need a fresh one in case the current echo buffer equals
9975 the one containing the last displayed echo area message. */
9976 if (!NILP (echo_area_buffer[this_one])
9977 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9978 echo_area_buffer[this_one] = Qnil;
9979 }
9980
9981 /* Choose a suitable buffer from echo_buffer[] is we don't
9982 have one. */
9983 if (NILP (echo_area_buffer[this_one]))
9984 {
9985 echo_area_buffer[this_one]
9986 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9987 ? echo_buffer[the_other]
9988 : echo_buffer[this_one]);
9989 clear_buffer_p = 1;
9990 }
9991
9992 buffer = echo_area_buffer[this_one];
9993
9994 /* Don't get confused by reusing the buffer used for echoing
9995 for a different purpose. */
9996 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9997 cancel_echoing ();
9998
9999 record_unwind_protect (unwind_with_echo_area_buffer,
10000 with_echo_area_buffer_unwind_data (w));
10001
10002 /* Make the echo area buffer current. Note that for display
10003 purposes, it is not necessary that the displayed window's buffer
10004 == current_buffer, except for text property lookup. So, let's
10005 only set that buffer temporarily here without doing a full
10006 Fset_window_buffer. We must also change w->pointm, though,
10007 because otherwise an assertions in unshow_buffer fails, and Emacs
10008 aborts. */
10009 set_buffer_internal_1 (XBUFFER (buffer));
10010 if (w)
10011 {
10012 wset_buffer (w, buffer);
10013 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10014 }
10015
10016 bset_undo_list (current_buffer, Qt);
10017 bset_read_only (current_buffer, Qnil);
10018 specbind (Qinhibit_read_only, Qt);
10019 specbind (Qinhibit_modification_hooks, Qt);
10020
10021 if (clear_buffer_p && Z > BEG)
10022 del_range (BEG, Z);
10023
10024 eassert (BEGV >= BEG);
10025 eassert (ZV <= Z && ZV >= BEGV);
10026
10027 rc = fn (a1, a2);
10028
10029 eassert (BEGV >= BEG);
10030 eassert (ZV <= Z && ZV >= BEGV);
10031
10032 unbind_to (count, Qnil);
10033 return rc;
10034 }
10035
10036
10037 /* Save state that should be preserved around the call to the function
10038 FN called in with_echo_area_buffer. */
10039
10040 static Lisp_Object
10041 with_echo_area_buffer_unwind_data (struct window *w)
10042 {
10043 int i = 0;
10044 Lisp_Object vector, tmp;
10045
10046 /* Reduce consing by keeping one vector in
10047 Vwith_echo_area_save_vector. */
10048 vector = Vwith_echo_area_save_vector;
10049 Vwith_echo_area_save_vector = Qnil;
10050
10051 if (NILP (vector))
10052 vector = Fmake_vector (make_number (7), Qnil);
10053
10054 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10055 ASET (vector, i, Vdeactivate_mark); ++i;
10056 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10057
10058 if (w)
10059 {
10060 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10061 ASET (vector, i, w->buffer); ++i;
10062 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10063 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10064 }
10065 else
10066 {
10067 int end = i + 4;
10068 for (; i < end; ++i)
10069 ASET (vector, i, Qnil);
10070 }
10071
10072 eassert (i == ASIZE (vector));
10073 return vector;
10074 }
10075
10076
10077 /* Restore global state from VECTOR which was created by
10078 with_echo_area_buffer_unwind_data. */
10079
10080 static Lisp_Object
10081 unwind_with_echo_area_buffer (Lisp_Object vector)
10082 {
10083 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10084 Vdeactivate_mark = AREF (vector, 1);
10085 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10086
10087 if (WINDOWP (AREF (vector, 3)))
10088 {
10089 struct window *w;
10090 Lisp_Object buffer, charpos, bytepos;
10091
10092 w = XWINDOW (AREF (vector, 3));
10093 buffer = AREF (vector, 4);
10094 charpos = AREF (vector, 5);
10095 bytepos = AREF (vector, 6);
10096
10097 wset_buffer (w, buffer);
10098 set_marker_both (w->pointm, buffer,
10099 XFASTINT (charpos), XFASTINT (bytepos));
10100 }
10101
10102 Vwith_echo_area_save_vector = vector;
10103 return Qnil;
10104 }
10105
10106
10107 /* Set up the echo area for use by print functions. MULTIBYTE_P
10108 non-zero means we will print multibyte. */
10109
10110 void
10111 setup_echo_area_for_printing (int multibyte_p)
10112 {
10113 /* If we can't find an echo area any more, exit. */
10114 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10115 Fkill_emacs (Qnil);
10116
10117 ensure_echo_area_buffers ();
10118
10119 if (!message_buf_print)
10120 {
10121 /* A message has been output since the last time we printed.
10122 Choose a fresh echo area buffer. */
10123 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10124 echo_area_buffer[0] = echo_buffer[1];
10125 else
10126 echo_area_buffer[0] = echo_buffer[0];
10127
10128 /* Switch to that buffer and clear it. */
10129 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10130 bset_truncate_lines (current_buffer, Qnil);
10131
10132 if (Z > BEG)
10133 {
10134 ptrdiff_t count = SPECPDL_INDEX ();
10135 specbind (Qinhibit_read_only, Qt);
10136 /* Note that undo recording is always disabled. */
10137 del_range (BEG, Z);
10138 unbind_to (count, Qnil);
10139 }
10140 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10141
10142 /* Set up the buffer for the multibyteness we need. */
10143 if (multibyte_p
10144 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10145 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10146
10147 /* Raise the frame containing the echo area. */
10148 if (minibuffer_auto_raise)
10149 {
10150 struct frame *sf = SELECTED_FRAME ();
10151 Lisp_Object mini_window;
10152 mini_window = FRAME_MINIBUF_WINDOW (sf);
10153 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10154 }
10155
10156 message_log_maybe_newline ();
10157 message_buf_print = 1;
10158 }
10159 else
10160 {
10161 if (NILP (echo_area_buffer[0]))
10162 {
10163 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10164 echo_area_buffer[0] = echo_buffer[1];
10165 else
10166 echo_area_buffer[0] = echo_buffer[0];
10167 }
10168
10169 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10170 {
10171 /* Someone switched buffers between print requests. */
10172 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10173 bset_truncate_lines (current_buffer, Qnil);
10174 }
10175 }
10176 }
10177
10178
10179 /* Display an echo area message in window W. Value is non-zero if W's
10180 height is changed. If display_last_displayed_message_p is
10181 non-zero, display the message that was last displayed, otherwise
10182 display the current message. */
10183
10184 static int
10185 display_echo_area (struct window *w)
10186 {
10187 int i, no_message_p, window_height_changed_p;
10188
10189 /* Temporarily disable garbage collections while displaying the echo
10190 area. This is done because a GC can print a message itself.
10191 That message would modify the echo area buffer's contents while a
10192 redisplay of the buffer is going on, and seriously confuse
10193 redisplay. */
10194 ptrdiff_t count = inhibit_garbage_collection ();
10195
10196 /* If there is no message, we must call display_echo_area_1
10197 nevertheless because it resizes the window. But we will have to
10198 reset the echo_area_buffer in question to nil at the end because
10199 with_echo_area_buffer will sets it to an empty buffer. */
10200 i = display_last_displayed_message_p ? 1 : 0;
10201 no_message_p = NILP (echo_area_buffer[i]);
10202
10203 window_height_changed_p
10204 = with_echo_area_buffer (w, display_last_displayed_message_p,
10205 display_echo_area_1,
10206 (intptr_t) w, Qnil);
10207
10208 if (no_message_p)
10209 echo_area_buffer[i] = Qnil;
10210
10211 unbind_to (count, Qnil);
10212 return window_height_changed_p;
10213 }
10214
10215
10216 /* Helper for display_echo_area. Display the current buffer which
10217 contains the current echo area message in window W, a mini-window,
10218 a pointer to which is passed in A1. A2..A4 are currently not used.
10219 Change the height of W so that all of the message is displayed.
10220 Value is non-zero if height of W was changed. */
10221
10222 static int
10223 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10224 {
10225 intptr_t i1 = a1;
10226 struct window *w = (struct window *) i1;
10227 Lisp_Object window;
10228 struct text_pos start;
10229 int window_height_changed_p = 0;
10230
10231 /* Do this before displaying, so that we have a large enough glyph
10232 matrix for the display. If we can't get enough space for the
10233 whole text, display the last N lines. That works by setting w->start. */
10234 window_height_changed_p = resize_mini_window (w, 0);
10235
10236 /* Use the starting position chosen by resize_mini_window. */
10237 SET_TEXT_POS_FROM_MARKER (start, w->start);
10238
10239 /* Display. */
10240 clear_glyph_matrix (w->desired_matrix);
10241 XSETWINDOW (window, w);
10242 try_window (window, start, 0);
10243
10244 return window_height_changed_p;
10245 }
10246
10247
10248 /* Resize the echo area window to exactly the size needed for the
10249 currently displayed message, if there is one. If a mini-buffer
10250 is active, don't shrink it. */
10251
10252 void
10253 resize_echo_area_exactly (void)
10254 {
10255 if (BUFFERP (echo_area_buffer[0])
10256 && WINDOWP (echo_area_window))
10257 {
10258 struct window *w = XWINDOW (echo_area_window);
10259 int resized_p;
10260 Lisp_Object resize_exactly;
10261
10262 if (minibuf_level == 0)
10263 resize_exactly = Qt;
10264 else
10265 resize_exactly = Qnil;
10266
10267 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10268 (intptr_t) w, resize_exactly);
10269 if (resized_p)
10270 {
10271 ++windows_or_buffers_changed;
10272 ++update_mode_lines;
10273 redisplay_internal ();
10274 }
10275 }
10276 }
10277
10278
10279 /* Callback function for with_echo_area_buffer, when used from
10280 resize_echo_area_exactly. A1 contains a pointer to the window to
10281 resize, EXACTLY non-nil means resize the mini-window exactly to the
10282 size of the text displayed. A3 and A4 are not used. Value is what
10283 resize_mini_window returns. */
10284
10285 static int
10286 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10287 {
10288 intptr_t i1 = a1;
10289 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10290 }
10291
10292
10293 /* Resize mini-window W to fit the size of its contents. EXACT_P
10294 means size the window exactly to the size needed. Otherwise, it's
10295 only enlarged until W's buffer is empty.
10296
10297 Set W->start to the right place to begin display. If the whole
10298 contents fit, start at the beginning. Otherwise, start so as
10299 to make the end of the contents appear. This is particularly
10300 important for y-or-n-p, but seems desirable generally.
10301
10302 Value is non-zero if the window height has been changed. */
10303
10304 int
10305 resize_mini_window (struct window *w, int exact_p)
10306 {
10307 struct frame *f = XFRAME (w->frame);
10308 int window_height_changed_p = 0;
10309
10310 eassert (MINI_WINDOW_P (w));
10311
10312 /* By default, start display at the beginning. */
10313 set_marker_both (w->start, w->buffer,
10314 BUF_BEGV (XBUFFER (w->buffer)),
10315 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10316
10317 /* Don't resize windows while redisplaying a window; it would
10318 confuse redisplay functions when the size of the window they are
10319 displaying changes from under them. Such a resizing can happen,
10320 for instance, when which-func prints a long message while
10321 we are running fontification-functions. We're running these
10322 functions with safe_call which binds inhibit-redisplay to t. */
10323 if (!NILP (Vinhibit_redisplay))
10324 return 0;
10325
10326 /* Nil means don't try to resize. */
10327 if (NILP (Vresize_mini_windows)
10328 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10329 return 0;
10330
10331 if (!FRAME_MINIBUF_ONLY_P (f))
10332 {
10333 struct it it;
10334 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10335 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10336 int height;
10337 EMACS_INT max_height;
10338 int unit = FRAME_LINE_HEIGHT (f);
10339 struct text_pos start;
10340 struct buffer *old_current_buffer = NULL;
10341
10342 if (current_buffer != XBUFFER (w->buffer))
10343 {
10344 old_current_buffer = current_buffer;
10345 set_buffer_internal (XBUFFER (w->buffer));
10346 }
10347
10348 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10349
10350 /* Compute the max. number of lines specified by the user. */
10351 if (FLOATP (Vmax_mini_window_height))
10352 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10353 else if (INTEGERP (Vmax_mini_window_height))
10354 max_height = XINT (Vmax_mini_window_height);
10355 else
10356 max_height = total_height / 4;
10357
10358 /* Correct that max. height if it's bogus. */
10359 max_height = clip_to_bounds (1, max_height, total_height);
10360
10361 /* Find out the height of the text in the window. */
10362 if (it.line_wrap == TRUNCATE)
10363 height = 1;
10364 else
10365 {
10366 last_height = 0;
10367 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10368 if (it.max_ascent == 0 && it.max_descent == 0)
10369 height = it.current_y + last_height;
10370 else
10371 height = it.current_y + it.max_ascent + it.max_descent;
10372 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10373 height = (height + unit - 1) / unit;
10374 }
10375
10376 /* Compute a suitable window start. */
10377 if (height > max_height)
10378 {
10379 height = max_height;
10380 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10381 move_it_vertically_backward (&it, (height - 1) * unit);
10382 start = it.current.pos;
10383 }
10384 else
10385 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10386 SET_MARKER_FROM_TEXT_POS (w->start, start);
10387
10388 if (EQ (Vresize_mini_windows, Qgrow_only))
10389 {
10390 /* Let it grow only, until we display an empty message, in which
10391 case the window shrinks again. */
10392 if (height > WINDOW_TOTAL_LINES (w))
10393 {
10394 int old_height = WINDOW_TOTAL_LINES (w);
10395 freeze_window_starts (f, 1);
10396 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10397 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10398 }
10399 else if (height < WINDOW_TOTAL_LINES (w)
10400 && (exact_p || BEGV == ZV))
10401 {
10402 int old_height = WINDOW_TOTAL_LINES (w);
10403 freeze_window_starts (f, 0);
10404 shrink_mini_window (w);
10405 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10406 }
10407 }
10408 else
10409 {
10410 /* Always resize to exact size needed. */
10411 if (height > WINDOW_TOTAL_LINES (w))
10412 {
10413 int old_height = WINDOW_TOTAL_LINES (w);
10414 freeze_window_starts (f, 1);
10415 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10416 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10417 }
10418 else if (height < WINDOW_TOTAL_LINES (w))
10419 {
10420 int old_height = WINDOW_TOTAL_LINES (w);
10421 freeze_window_starts (f, 0);
10422 shrink_mini_window (w);
10423
10424 if (height)
10425 {
10426 freeze_window_starts (f, 1);
10427 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10428 }
10429
10430 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10431 }
10432 }
10433
10434 if (old_current_buffer)
10435 set_buffer_internal (old_current_buffer);
10436 }
10437
10438 return window_height_changed_p;
10439 }
10440
10441
10442 /* Value is the current message, a string, or nil if there is no
10443 current message. */
10444
10445 Lisp_Object
10446 current_message (void)
10447 {
10448 Lisp_Object msg;
10449
10450 if (!BUFFERP (echo_area_buffer[0]))
10451 msg = Qnil;
10452 else
10453 {
10454 with_echo_area_buffer (0, 0, current_message_1,
10455 (intptr_t) &msg, Qnil);
10456 if (NILP (msg))
10457 echo_area_buffer[0] = Qnil;
10458 }
10459
10460 return msg;
10461 }
10462
10463
10464 static int
10465 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10466 {
10467 intptr_t i1 = a1;
10468 Lisp_Object *msg = (Lisp_Object *) i1;
10469
10470 if (Z > BEG)
10471 *msg = make_buffer_string (BEG, Z, 1);
10472 else
10473 *msg = Qnil;
10474 return 0;
10475 }
10476
10477
10478 /* Push the current message on Vmessage_stack for later restoration
10479 by restore_message. Value is non-zero if the current message isn't
10480 empty. This is a relatively infrequent operation, so it's not
10481 worth optimizing. */
10482
10483 bool
10484 push_message (void)
10485 {
10486 Lisp_Object msg = current_message ();
10487 Vmessage_stack = Fcons (msg, Vmessage_stack);
10488 return STRINGP (msg);
10489 }
10490
10491
10492 /* Restore message display from the top of Vmessage_stack. */
10493
10494 void
10495 restore_message (void)
10496 {
10497 eassert (CONSP (Vmessage_stack));
10498 message3_nolog (XCAR (Vmessage_stack));
10499 }
10500
10501
10502 /* Handler for record_unwind_protect calling pop_message. */
10503
10504 Lisp_Object
10505 pop_message_unwind (Lisp_Object dummy)
10506 {
10507 pop_message ();
10508 return Qnil;
10509 }
10510
10511 /* Pop the top-most entry off Vmessage_stack. */
10512
10513 static void
10514 pop_message (void)
10515 {
10516 eassert (CONSP (Vmessage_stack));
10517 Vmessage_stack = XCDR (Vmessage_stack);
10518 }
10519
10520
10521 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10522 exits. If the stack is not empty, we have a missing pop_message
10523 somewhere. */
10524
10525 void
10526 check_message_stack (void)
10527 {
10528 if (!NILP (Vmessage_stack))
10529 emacs_abort ();
10530 }
10531
10532
10533 /* Truncate to NCHARS what will be displayed in the echo area the next
10534 time we display it---but don't redisplay it now. */
10535
10536 void
10537 truncate_echo_area (ptrdiff_t nchars)
10538 {
10539 if (nchars == 0)
10540 echo_area_buffer[0] = Qnil;
10541 else if (!noninteractive
10542 && INTERACTIVE
10543 && !NILP (echo_area_buffer[0]))
10544 {
10545 struct frame *sf = SELECTED_FRAME ();
10546 /* Error messages get reported properly by cmd_error, so this must be
10547 just an informative message; if the frame hasn't really been
10548 initialized yet, just toss it. */
10549 if (sf->glyphs_initialized_p)
10550 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10551 }
10552 }
10553
10554
10555 /* Helper function for truncate_echo_area. Truncate the current
10556 message to at most NCHARS characters. */
10557
10558 static int
10559 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10560 {
10561 if (BEG + nchars < Z)
10562 del_range (BEG + nchars, Z);
10563 if (Z == BEG)
10564 echo_area_buffer[0] = Qnil;
10565 return 0;
10566 }
10567
10568 /* Set the current message to STRING. */
10569
10570 static void
10571 set_message (Lisp_Object string)
10572 {
10573 eassert (STRINGP (string));
10574
10575 message_enable_multibyte = STRING_MULTIBYTE (string);
10576
10577 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10578 message_buf_print = 0;
10579 help_echo_showing_p = 0;
10580
10581 if (STRINGP (Vdebug_on_message)
10582 && STRINGP (string)
10583 && fast_string_match (Vdebug_on_message, string) >= 0)
10584 call_debugger (list2 (Qerror, string));
10585 }
10586
10587
10588 /* Helper function for set_message. First argument is ignored and second
10589 argument has the same meaning as for set_message.
10590 This function is called with the echo area buffer being current. */
10591
10592 static int
10593 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10594 {
10595 eassert (STRINGP (string));
10596
10597 /* Change multibyteness of the echo buffer appropriately. */
10598 if (message_enable_multibyte
10599 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10600 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10601
10602 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10603 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10604 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10605
10606 /* Insert new message at BEG. */
10607 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10608
10609 /* This function takes care of single/multibyte conversion.
10610 We just have to ensure that the echo area buffer has the right
10611 setting of enable_multibyte_characters. */
10612 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10613
10614 return 0;
10615 }
10616
10617
10618 /* Clear messages. CURRENT_P non-zero means clear the current
10619 message. LAST_DISPLAYED_P non-zero means clear the message
10620 last displayed. */
10621
10622 void
10623 clear_message (int current_p, int last_displayed_p)
10624 {
10625 if (current_p)
10626 {
10627 echo_area_buffer[0] = Qnil;
10628 message_cleared_p = 1;
10629 }
10630
10631 if (last_displayed_p)
10632 echo_area_buffer[1] = Qnil;
10633
10634 message_buf_print = 0;
10635 }
10636
10637 /* Clear garbaged frames.
10638
10639 This function is used where the old redisplay called
10640 redraw_garbaged_frames which in turn called redraw_frame which in
10641 turn called clear_frame. The call to clear_frame was a source of
10642 flickering. I believe a clear_frame is not necessary. It should
10643 suffice in the new redisplay to invalidate all current matrices,
10644 and ensure a complete redisplay of all windows. */
10645
10646 static void
10647 clear_garbaged_frames (void)
10648 {
10649 if (frame_garbaged)
10650 {
10651 Lisp_Object tail, frame;
10652 int changed_count = 0;
10653
10654 FOR_EACH_FRAME (tail, frame)
10655 {
10656 struct frame *f = XFRAME (frame);
10657
10658 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10659 {
10660 if (f->resized_p)
10661 {
10662 redraw_frame (f);
10663 f->force_flush_display_p = 1;
10664 }
10665 clear_current_matrices (f);
10666 changed_count++;
10667 f->garbaged = 0;
10668 f->resized_p = 0;
10669 }
10670 }
10671
10672 frame_garbaged = 0;
10673 if (changed_count)
10674 ++windows_or_buffers_changed;
10675 }
10676 }
10677
10678
10679 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10680 is non-zero update selected_frame. Value is non-zero if the
10681 mini-windows height has been changed. */
10682
10683 static int
10684 echo_area_display (int update_frame_p)
10685 {
10686 Lisp_Object mini_window;
10687 struct window *w;
10688 struct frame *f;
10689 int window_height_changed_p = 0;
10690 struct frame *sf = SELECTED_FRAME ();
10691
10692 mini_window = FRAME_MINIBUF_WINDOW (sf);
10693 w = XWINDOW (mini_window);
10694 f = XFRAME (WINDOW_FRAME (w));
10695
10696 /* Don't display if frame is invisible or not yet initialized. */
10697 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10698 return 0;
10699
10700 #ifdef HAVE_WINDOW_SYSTEM
10701 /* When Emacs starts, selected_frame may be the initial terminal
10702 frame. If we let this through, a message would be displayed on
10703 the terminal. */
10704 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10705 return 0;
10706 #endif /* HAVE_WINDOW_SYSTEM */
10707
10708 /* Redraw garbaged frames. */
10709 clear_garbaged_frames ();
10710
10711 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10712 {
10713 echo_area_window = mini_window;
10714 window_height_changed_p = display_echo_area (w);
10715 w->must_be_updated_p = 1;
10716
10717 /* Update the display, unless called from redisplay_internal.
10718 Also don't update the screen during redisplay itself. The
10719 update will happen at the end of redisplay, and an update
10720 here could cause confusion. */
10721 if (update_frame_p && !redisplaying_p)
10722 {
10723 int n = 0;
10724
10725 /* If the display update has been interrupted by pending
10726 input, update mode lines in the frame. Due to the
10727 pending input, it might have been that redisplay hasn't
10728 been called, so that mode lines above the echo area are
10729 garbaged. This looks odd, so we prevent it here. */
10730 if (!display_completed)
10731 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10732
10733 if (window_height_changed_p
10734 /* Don't do this if Emacs is shutting down. Redisplay
10735 needs to run hooks. */
10736 && !NILP (Vrun_hooks))
10737 {
10738 /* Must update other windows. Likewise as in other
10739 cases, don't let this update be interrupted by
10740 pending input. */
10741 ptrdiff_t count = SPECPDL_INDEX ();
10742 specbind (Qredisplay_dont_pause, Qt);
10743 windows_or_buffers_changed = 1;
10744 redisplay_internal ();
10745 unbind_to (count, Qnil);
10746 }
10747 else if (FRAME_WINDOW_P (f) && n == 0)
10748 {
10749 /* Window configuration is the same as before.
10750 Can do with a display update of the echo area,
10751 unless we displayed some mode lines. */
10752 update_single_window (w, 1);
10753 FRAME_RIF (f)->flush_display (f);
10754 }
10755 else
10756 update_frame (f, 1, 1);
10757
10758 /* If cursor is in the echo area, make sure that the next
10759 redisplay displays the minibuffer, so that the cursor will
10760 be replaced with what the minibuffer wants. */
10761 if (cursor_in_echo_area)
10762 ++windows_or_buffers_changed;
10763 }
10764 }
10765 else if (!EQ (mini_window, selected_window))
10766 windows_or_buffers_changed++;
10767
10768 /* Last displayed message is now the current message. */
10769 echo_area_buffer[1] = echo_area_buffer[0];
10770 /* Inform read_char that we're not echoing. */
10771 echo_message_buffer = Qnil;
10772
10773 /* Prevent redisplay optimization in redisplay_internal by resetting
10774 this_line_start_pos. This is done because the mini-buffer now
10775 displays the message instead of its buffer text. */
10776 if (EQ (mini_window, selected_window))
10777 CHARPOS (this_line_start_pos) = 0;
10778
10779 return window_height_changed_p;
10780 }
10781
10782 /* Nonzero if the current window's buffer is shown in more than one
10783 window and was modified since last redisplay. */
10784
10785 static int
10786 buffer_shared_and_changed (void)
10787 {
10788 return (buffer_window_count (current_buffer) > 1
10789 && UNCHANGED_MODIFIED < MODIFF);
10790 }
10791
10792 /* Nonzero if W doesn't reflect the actual state of current buffer due
10793 to its text or overlays change. FIXME: this may be called when
10794 XBUFFER (w->buffer) != current_buffer, which looks suspicious. */
10795
10796 static int
10797 window_outdated (struct window *w)
10798 {
10799 return (w->last_modified < MODIFF
10800 || w->last_overlay_modified < OVERLAY_MODIFF);
10801 }
10802
10803 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10804 is enabled and mark of W's buffer was changed since last W's update. */
10805
10806 static int
10807 window_buffer_changed (struct window *w)
10808 {
10809 struct buffer *b = XBUFFER (w->buffer);
10810
10811 eassert (BUFFER_LIVE_P (b));
10812
10813 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10814 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10815 != (w->region_showing != 0)));
10816 }
10817
10818 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10819
10820 static int
10821 mode_line_update_needed (struct window *w)
10822 {
10823 return (w->column_number_displayed != -1
10824 && !(PT == w->last_point && !window_outdated (w))
10825 && (w->column_number_displayed != current_column ()));
10826 }
10827
10828 /***********************************************************************
10829 Mode Lines and Frame Titles
10830 ***********************************************************************/
10831
10832 /* A buffer for constructing non-propertized mode-line strings and
10833 frame titles in it; allocated from the heap in init_xdisp and
10834 resized as needed in store_mode_line_noprop_char. */
10835
10836 static char *mode_line_noprop_buf;
10837
10838 /* The buffer's end, and a current output position in it. */
10839
10840 static char *mode_line_noprop_buf_end;
10841 static char *mode_line_noprop_ptr;
10842
10843 #define MODE_LINE_NOPROP_LEN(start) \
10844 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10845
10846 static enum {
10847 MODE_LINE_DISPLAY = 0,
10848 MODE_LINE_TITLE,
10849 MODE_LINE_NOPROP,
10850 MODE_LINE_STRING
10851 } mode_line_target;
10852
10853 /* Alist that caches the results of :propertize.
10854 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10855 static Lisp_Object mode_line_proptrans_alist;
10856
10857 /* List of strings making up the mode-line. */
10858 static Lisp_Object mode_line_string_list;
10859
10860 /* Base face property when building propertized mode line string. */
10861 static Lisp_Object mode_line_string_face;
10862 static Lisp_Object mode_line_string_face_prop;
10863
10864
10865 /* Unwind data for mode line strings */
10866
10867 static Lisp_Object Vmode_line_unwind_vector;
10868
10869 static Lisp_Object
10870 format_mode_line_unwind_data (struct frame *target_frame,
10871 struct buffer *obuf,
10872 Lisp_Object owin,
10873 int save_proptrans)
10874 {
10875 Lisp_Object vector, tmp;
10876
10877 /* Reduce consing by keeping one vector in
10878 Vwith_echo_area_save_vector. */
10879 vector = Vmode_line_unwind_vector;
10880 Vmode_line_unwind_vector = Qnil;
10881
10882 if (NILP (vector))
10883 vector = Fmake_vector (make_number (10), Qnil);
10884
10885 ASET (vector, 0, make_number (mode_line_target));
10886 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10887 ASET (vector, 2, mode_line_string_list);
10888 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10889 ASET (vector, 4, mode_line_string_face);
10890 ASET (vector, 5, mode_line_string_face_prop);
10891
10892 if (obuf)
10893 XSETBUFFER (tmp, obuf);
10894 else
10895 tmp = Qnil;
10896 ASET (vector, 6, tmp);
10897 ASET (vector, 7, owin);
10898 if (target_frame)
10899 {
10900 /* Similarly to `with-selected-window', if the operation selects
10901 a window on another frame, we must restore that frame's
10902 selected window, and (for a tty) the top-frame. */
10903 ASET (vector, 8, target_frame->selected_window);
10904 if (FRAME_TERMCAP_P (target_frame))
10905 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10906 }
10907
10908 return vector;
10909 }
10910
10911 static Lisp_Object
10912 unwind_format_mode_line (Lisp_Object vector)
10913 {
10914 Lisp_Object old_window = AREF (vector, 7);
10915 Lisp_Object target_frame_window = AREF (vector, 8);
10916 Lisp_Object old_top_frame = AREF (vector, 9);
10917
10918 mode_line_target = XINT (AREF (vector, 0));
10919 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10920 mode_line_string_list = AREF (vector, 2);
10921 if (! EQ (AREF (vector, 3), Qt))
10922 mode_line_proptrans_alist = AREF (vector, 3);
10923 mode_line_string_face = AREF (vector, 4);
10924 mode_line_string_face_prop = AREF (vector, 5);
10925
10926 /* Select window before buffer, since it may change the buffer. */
10927 if (!NILP (old_window))
10928 {
10929 /* If the operation that we are unwinding had selected a window
10930 on a different frame, reset its frame-selected-window. For a
10931 text terminal, reset its top-frame if necessary. */
10932 if (!NILP (target_frame_window))
10933 {
10934 Lisp_Object frame
10935 = WINDOW_FRAME (XWINDOW (target_frame_window));
10936
10937 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10938 Fselect_window (target_frame_window, Qt);
10939
10940 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10941 Fselect_frame (old_top_frame, Qt);
10942 }
10943
10944 Fselect_window (old_window, Qt);
10945 }
10946
10947 if (!NILP (AREF (vector, 6)))
10948 {
10949 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10950 ASET (vector, 6, Qnil);
10951 }
10952
10953 Vmode_line_unwind_vector = vector;
10954 return Qnil;
10955 }
10956
10957
10958 /* Store a single character C for the frame title in mode_line_noprop_buf.
10959 Re-allocate mode_line_noprop_buf if necessary. */
10960
10961 static void
10962 store_mode_line_noprop_char (char c)
10963 {
10964 /* If output position has reached the end of the allocated buffer,
10965 increase the buffer's size. */
10966 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10967 {
10968 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10969 ptrdiff_t size = len;
10970 mode_line_noprop_buf =
10971 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10972 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10973 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10974 }
10975
10976 *mode_line_noprop_ptr++ = c;
10977 }
10978
10979
10980 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10981 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10982 characters that yield more columns than PRECISION; PRECISION <= 0
10983 means copy the whole string. Pad with spaces until FIELD_WIDTH
10984 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10985 pad. Called from display_mode_element when it is used to build a
10986 frame title. */
10987
10988 static int
10989 store_mode_line_noprop (const char *string, int field_width, int precision)
10990 {
10991 const unsigned char *str = (const unsigned char *) string;
10992 int n = 0;
10993 ptrdiff_t dummy, nbytes;
10994
10995 /* Copy at most PRECISION chars from STR. */
10996 nbytes = strlen (string);
10997 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10998 while (nbytes--)
10999 store_mode_line_noprop_char (*str++);
11000
11001 /* Fill up with spaces until FIELD_WIDTH reached. */
11002 while (field_width > 0
11003 && n < field_width)
11004 {
11005 store_mode_line_noprop_char (' ');
11006 ++n;
11007 }
11008
11009 return n;
11010 }
11011
11012 /***********************************************************************
11013 Frame Titles
11014 ***********************************************************************/
11015
11016 #ifdef HAVE_WINDOW_SYSTEM
11017
11018 /* Set the title of FRAME, if it has changed. The title format is
11019 Vicon_title_format if FRAME is iconified, otherwise it is
11020 frame_title_format. */
11021
11022 static void
11023 x_consider_frame_title (Lisp_Object frame)
11024 {
11025 struct frame *f = XFRAME (frame);
11026
11027 if (FRAME_WINDOW_P (f)
11028 || FRAME_MINIBUF_ONLY_P (f)
11029 || f->explicit_name)
11030 {
11031 /* Do we have more than one visible frame on this X display? */
11032 Lisp_Object tail, other_frame, fmt;
11033 ptrdiff_t title_start;
11034 char *title;
11035 ptrdiff_t len;
11036 struct it it;
11037 ptrdiff_t count = SPECPDL_INDEX ();
11038
11039 FOR_EACH_FRAME (tail, other_frame)
11040 {
11041 struct frame *tf = XFRAME (other_frame);
11042
11043 if (tf != f
11044 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11045 && !FRAME_MINIBUF_ONLY_P (tf)
11046 && !EQ (other_frame, tip_frame)
11047 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11048 break;
11049 }
11050
11051 /* Set global variable indicating that multiple frames exist. */
11052 multiple_frames = CONSP (tail);
11053
11054 /* Switch to the buffer of selected window of the frame. Set up
11055 mode_line_target so that display_mode_element will output into
11056 mode_line_noprop_buf; then display the title. */
11057 record_unwind_protect (unwind_format_mode_line,
11058 format_mode_line_unwind_data
11059 (f, current_buffer, selected_window, 0));
11060
11061 Fselect_window (f->selected_window, Qt);
11062 set_buffer_internal_1
11063 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11064 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11065
11066 mode_line_target = MODE_LINE_TITLE;
11067 title_start = MODE_LINE_NOPROP_LEN (0);
11068 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11069 NULL, DEFAULT_FACE_ID);
11070 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11071 len = MODE_LINE_NOPROP_LEN (title_start);
11072 title = mode_line_noprop_buf + title_start;
11073 unbind_to (count, Qnil);
11074
11075 /* Set the title only if it's changed. This avoids consing in
11076 the common case where it hasn't. (If it turns out that we've
11077 already wasted too much time by walking through the list with
11078 display_mode_element, then we might need to optimize at a
11079 higher level than this.) */
11080 if (! STRINGP (f->name)
11081 || SBYTES (f->name) != len
11082 || memcmp (title, SDATA (f->name), len) != 0)
11083 x_implicitly_set_name (f, make_string (title, len), Qnil);
11084 }
11085 }
11086
11087 #endif /* not HAVE_WINDOW_SYSTEM */
11088
11089 \f
11090 /***********************************************************************
11091 Menu Bars
11092 ***********************************************************************/
11093
11094
11095 /* Prepare for redisplay by updating menu-bar item lists when
11096 appropriate. This can call eval. */
11097
11098 void
11099 prepare_menu_bars (void)
11100 {
11101 int all_windows;
11102 struct gcpro gcpro1, gcpro2;
11103 struct frame *f;
11104 Lisp_Object tooltip_frame;
11105
11106 #ifdef HAVE_WINDOW_SYSTEM
11107 tooltip_frame = tip_frame;
11108 #else
11109 tooltip_frame = Qnil;
11110 #endif
11111
11112 /* Update all frame titles based on their buffer names, etc. We do
11113 this before the menu bars so that the buffer-menu will show the
11114 up-to-date frame titles. */
11115 #ifdef HAVE_WINDOW_SYSTEM
11116 if (windows_or_buffers_changed || update_mode_lines)
11117 {
11118 Lisp_Object tail, frame;
11119
11120 FOR_EACH_FRAME (tail, frame)
11121 {
11122 f = XFRAME (frame);
11123 if (!EQ (frame, tooltip_frame)
11124 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11125 x_consider_frame_title (frame);
11126 }
11127 }
11128 #endif /* HAVE_WINDOW_SYSTEM */
11129
11130 /* Update the menu bar item lists, if appropriate. This has to be
11131 done before any actual redisplay or generation of display lines. */
11132 all_windows = (update_mode_lines
11133 || buffer_shared_and_changed ()
11134 || windows_or_buffers_changed);
11135 if (all_windows)
11136 {
11137 Lisp_Object tail, frame;
11138 ptrdiff_t count = SPECPDL_INDEX ();
11139 /* 1 means that update_menu_bar has run its hooks
11140 so any further calls to update_menu_bar shouldn't do so again. */
11141 int menu_bar_hooks_run = 0;
11142
11143 record_unwind_save_match_data ();
11144
11145 FOR_EACH_FRAME (tail, frame)
11146 {
11147 f = XFRAME (frame);
11148
11149 /* Ignore tooltip frame. */
11150 if (EQ (frame, tooltip_frame))
11151 continue;
11152
11153 /* If a window on this frame changed size, report that to
11154 the user and clear the size-change flag. */
11155 if (FRAME_WINDOW_SIZES_CHANGED (f))
11156 {
11157 Lisp_Object functions;
11158
11159 /* Clear flag first in case we get an error below. */
11160 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11161 functions = Vwindow_size_change_functions;
11162 GCPRO2 (tail, functions);
11163
11164 while (CONSP (functions))
11165 {
11166 if (!EQ (XCAR (functions), Qt))
11167 call1 (XCAR (functions), frame);
11168 functions = XCDR (functions);
11169 }
11170 UNGCPRO;
11171 }
11172
11173 GCPRO1 (tail);
11174 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11175 #ifdef HAVE_WINDOW_SYSTEM
11176 update_tool_bar (f, 0);
11177 #endif
11178 #ifdef HAVE_NS
11179 if (windows_or_buffers_changed
11180 && FRAME_NS_P (f))
11181 ns_set_doc_edited
11182 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11183 #endif
11184 UNGCPRO;
11185 }
11186
11187 unbind_to (count, Qnil);
11188 }
11189 else
11190 {
11191 struct frame *sf = SELECTED_FRAME ();
11192 update_menu_bar (sf, 1, 0);
11193 #ifdef HAVE_WINDOW_SYSTEM
11194 update_tool_bar (sf, 1);
11195 #endif
11196 }
11197 }
11198
11199
11200 /* Update the menu bar item list for frame F. This has to be done
11201 before we start to fill in any display lines, because it can call
11202 eval.
11203
11204 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11205
11206 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11207 already ran the menu bar hooks for this redisplay, so there
11208 is no need to run them again. The return value is the
11209 updated value of this flag, to pass to the next call. */
11210
11211 static int
11212 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11213 {
11214 Lisp_Object window;
11215 register struct window *w;
11216
11217 /* If called recursively during a menu update, do nothing. This can
11218 happen when, for instance, an activate-menubar-hook causes a
11219 redisplay. */
11220 if (inhibit_menubar_update)
11221 return hooks_run;
11222
11223 window = FRAME_SELECTED_WINDOW (f);
11224 w = XWINDOW (window);
11225
11226 if (FRAME_WINDOW_P (f)
11227 ?
11228 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11229 || defined (HAVE_NS) || defined (USE_GTK)
11230 FRAME_EXTERNAL_MENU_BAR (f)
11231 #else
11232 FRAME_MENU_BAR_LINES (f) > 0
11233 #endif
11234 : FRAME_MENU_BAR_LINES (f) > 0)
11235 {
11236 /* If the user has switched buffers or windows, we need to
11237 recompute to reflect the new bindings. But we'll
11238 recompute when update_mode_lines is set too; that means
11239 that people can use force-mode-line-update to request
11240 that the menu bar be recomputed. The adverse effect on
11241 the rest of the redisplay algorithm is about the same as
11242 windows_or_buffers_changed anyway. */
11243 if (windows_or_buffers_changed
11244 /* This used to test w->update_mode_line, but we believe
11245 there is no need to recompute the menu in that case. */
11246 || update_mode_lines
11247 || window_buffer_changed (w))
11248 {
11249 struct buffer *prev = current_buffer;
11250 ptrdiff_t count = SPECPDL_INDEX ();
11251
11252 specbind (Qinhibit_menubar_update, Qt);
11253
11254 set_buffer_internal_1 (XBUFFER (w->buffer));
11255 if (save_match_data)
11256 record_unwind_save_match_data ();
11257 if (NILP (Voverriding_local_map_menu_flag))
11258 {
11259 specbind (Qoverriding_terminal_local_map, Qnil);
11260 specbind (Qoverriding_local_map, Qnil);
11261 }
11262
11263 if (!hooks_run)
11264 {
11265 /* Run the Lucid hook. */
11266 safe_run_hooks (Qactivate_menubar_hook);
11267
11268 /* If it has changed current-menubar from previous value,
11269 really recompute the menu-bar from the value. */
11270 if (! NILP (Vlucid_menu_bar_dirty_flag))
11271 call0 (Qrecompute_lucid_menubar);
11272
11273 safe_run_hooks (Qmenu_bar_update_hook);
11274
11275 hooks_run = 1;
11276 }
11277
11278 XSETFRAME (Vmenu_updating_frame, f);
11279 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11280
11281 /* Redisplay the menu bar in case we changed it. */
11282 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11283 || defined (HAVE_NS) || defined (USE_GTK)
11284 if (FRAME_WINDOW_P (f))
11285 {
11286 #if defined (HAVE_NS)
11287 /* All frames on Mac OS share the same menubar. So only
11288 the selected frame should be allowed to set it. */
11289 if (f == SELECTED_FRAME ())
11290 #endif
11291 set_frame_menubar (f, 0, 0);
11292 }
11293 else
11294 /* On a terminal screen, the menu bar is an ordinary screen
11295 line, and this makes it get updated. */
11296 w->update_mode_line = 1;
11297 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11298 /* In the non-toolkit version, the menu bar is an ordinary screen
11299 line, and this makes it get updated. */
11300 w->update_mode_line = 1;
11301 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11302
11303 unbind_to (count, Qnil);
11304 set_buffer_internal_1 (prev);
11305 }
11306 }
11307
11308 return hooks_run;
11309 }
11310
11311
11312 \f
11313 /***********************************************************************
11314 Output Cursor
11315 ***********************************************************************/
11316
11317 #ifdef HAVE_WINDOW_SYSTEM
11318
11319 /* EXPORT:
11320 Nominal cursor position -- where to draw output.
11321 HPOS and VPOS are window relative glyph matrix coordinates.
11322 X and Y are window relative pixel coordinates. */
11323
11324 struct cursor_pos output_cursor;
11325
11326
11327 /* EXPORT:
11328 Set the global variable output_cursor to CURSOR. All cursor
11329 positions are relative to updated_window. */
11330
11331 void
11332 set_output_cursor (struct cursor_pos *cursor)
11333 {
11334 output_cursor.hpos = cursor->hpos;
11335 output_cursor.vpos = cursor->vpos;
11336 output_cursor.x = cursor->x;
11337 output_cursor.y = cursor->y;
11338 }
11339
11340
11341 /* EXPORT for RIF:
11342 Set a nominal cursor position.
11343
11344 HPOS and VPOS are column/row positions in a window glyph matrix. X
11345 and Y are window text area relative pixel positions.
11346
11347 If this is done during an update, updated_window will contain the
11348 window that is being updated and the position is the future output
11349 cursor position for that window. If updated_window is null, use
11350 selected_window and display the cursor at the given position. */
11351
11352 void
11353 x_cursor_to (int vpos, int hpos, int y, int x)
11354 {
11355 struct window *w;
11356
11357 /* If updated_window is not set, work on selected_window. */
11358 if (updated_window)
11359 w = updated_window;
11360 else
11361 w = XWINDOW (selected_window);
11362
11363 /* Set the output cursor. */
11364 output_cursor.hpos = hpos;
11365 output_cursor.vpos = vpos;
11366 output_cursor.x = x;
11367 output_cursor.y = y;
11368
11369 /* If not called as part of an update, really display the cursor.
11370 This will also set the cursor position of W. */
11371 if (updated_window == NULL)
11372 {
11373 block_input ();
11374 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11375 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11376 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11377 unblock_input ();
11378 }
11379 }
11380
11381 #endif /* HAVE_WINDOW_SYSTEM */
11382
11383 \f
11384 /***********************************************************************
11385 Tool-bars
11386 ***********************************************************************/
11387
11388 #ifdef HAVE_WINDOW_SYSTEM
11389
11390 /* Where the mouse was last time we reported a mouse event. */
11391
11392 FRAME_PTR last_mouse_frame;
11393
11394 /* Tool-bar item index of the item on which a mouse button was pressed
11395 or -1. */
11396
11397 int last_tool_bar_item;
11398
11399 /* Select `frame' temporarily without running all the code in
11400 do_switch_frame.
11401 FIXME: Maybe do_switch_frame should be trimmed down similarly
11402 when `norecord' is set. */
11403 static Lisp_Object
11404 fast_set_selected_frame (Lisp_Object frame)
11405 {
11406 if (!EQ (selected_frame, frame))
11407 {
11408 selected_frame = frame;
11409 selected_window = XFRAME (frame)->selected_window;
11410 }
11411 return Qnil;
11412 }
11413
11414 /* Update the tool-bar item list for frame F. This has to be done
11415 before we start to fill in any display lines. Called from
11416 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11417 and restore it here. */
11418
11419 static void
11420 update_tool_bar (struct frame *f, int save_match_data)
11421 {
11422 #if defined (USE_GTK) || defined (HAVE_NS)
11423 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11424 #else
11425 int do_update = WINDOWP (f->tool_bar_window)
11426 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11427 #endif
11428
11429 if (do_update)
11430 {
11431 Lisp_Object window;
11432 struct window *w;
11433
11434 window = FRAME_SELECTED_WINDOW (f);
11435 w = XWINDOW (window);
11436
11437 /* If the user has switched buffers or windows, we need to
11438 recompute to reflect the new bindings. But we'll
11439 recompute when update_mode_lines is set too; that means
11440 that people can use force-mode-line-update to request
11441 that the menu bar be recomputed. The adverse effect on
11442 the rest of the redisplay algorithm is about the same as
11443 windows_or_buffers_changed anyway. */
11444 if (windows_or_buffers_changed
11445 || w->update_mode_line
11446 || update_mode_lines
11447 || window_buffer_changed (w))
11448 {
11449 struct buffer *prev = current_buffer;
11450 ptrdiff_t count = SPECPDL_INDEX ();
11451 Lisp_Object frame, new_tool_bar;
11452 int new_n_tool_bar;
11453 struct gcpro gcpro1;
11454
11455 /* Set current_buffer to the buffer of the selected
11456 window of the frame, so that we get the right local
11457 keymaps. */
11458 set_buffer_internal_1 (XBUFFER (w->buffer));
11459
11460 /* Save match data, if we must. */
11461 if (save_match_data)
11462 record_unwind_save_match_data ();
11463
11464 /* Make sure that we don't accidentally use bogus keymaps. */
11465 if (NILP (Voverriding_local_map_menu_flag))
11466 {
11467 specbind (Qoverriding_terminal_local_map, Qnil);
11468 specbind (Qoverriding_local_map, Qnil);
11469 }
11470
11471 GCPRO1 (new_tool_bar);
11472
11473 /* We must temporarily set the selected frame to this frame
11474 before calling tool_bar_items, because the calculation of
11475 the tool-bar keymap uses the selected frame (see
11476 `tool-bar-make-keymap' in tool-bar.el). */
11477 eassert (EQ (selected_window,
11478 /* Since we only explicitly preserve selected_frame,
11479 check that selected_window would be redundant. */
11480 XFRAME (selected_frame)->selected_window));
11481 record_unwind_protect (fast_set_selected_frame, selected_frame);
11482 XSETFRAME (frame, f);
11483 fast_set_selected_frame (frame);
11484
11485 /* Build desired tool-bar items from keymaps. */
11486 new_tool_bar
11487 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11488 &new_n_tool_bar);
11489
11490 /* Redisplay the tool-bar if we changed it. */
11491 if (new_n_tool_bar != f->n_tool_bar_items
11492 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11493 {
11494 /* Redisplay that happens asynchronously due to an expose event
11495 may access f->tool_bar_items. Make sure we update both
11496 variables within BLOCK_INPUT so no such event interrupts. */
11497 block_input ();
11498 fset_tool_bar_items (f, new_tool_bar);
11499 f->n_tool_bar_items = new_n_tool_bar;
11500 w->update_mode_line = 1;
11501 unblock_input ();
11502 }
11503
11504 UNGCPRO;
11505
11506 unbind_to (count, Qnil);
11507 set_buffer_internal_1 (prev);
11508 }
11509 }
11510 }
11511
11512
11513 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11514 F's desired tool-bar contents. F->tool_bar_items must have
11515 been set up previously by calling prepare_menu_bars. */
11516
11517 static void
11518 build_desired_tool_bar_string (struct frame *f)
11519 {
11520 int i, size, size_needed;
11521 struct gcpro gcpro1, gcpro2, gcpro3;
11522 Lisp_Object image, plist, props;
11523
11524 image = plist = props = Qnil;
11525 GCPRO3 (image, plist, props);
11526
11527 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11528 Otherwise, make a new string. */
11529
11530 /* The size of the string we might be able to reuse. */
11531 size = (STRINGP (f->desired_tool_bar_string)
11532 ? SCHARS (f->desired_tool_bar_string)
11533 : 0);
11534
11535 /* We need one space in the string for each image. */
11536 size_needed = f->n_tool_bar_items;
11537
11538 /* Reuse f->desired_tool_bar_string, if possible. */
11539 if (size < size_needed || NILP (f->desired_tool_bar_string))
11540 fset_desired_tool_bar_string
11541 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11542 else
11543 {
11544 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11545 Fremove_text_properties (make_number (0), make_number (size),
11546 props, f->desired_tool_bar_string);
11547 }
11548
11549 /* Put a `display' property on the string for the images to display,
11550 put a `menu_item' property on tool-bar items with a value that
11551 is the index of the item in F's tool-bar item vector. */
11552 for (i = 0; i < f->n_tool_bar_items; ++i)
11553 {
11554 #define PROP(IDX) \
11555 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11556
11557 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11558 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11559 int hmargin, vmargin, relief, idx, end;
11560
11561 /* If image is a vector, choose the image according to the
11562 button state. */
11563 image = PROP (TOOL_BAR_ITEM_IMAGES);
11564 if (VECTORP (image))
11565 {
11566 if (enabled_p)
11567 idx = (selected_p
11568 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11569 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11570 else
11571 idx = (selected_p
11572 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11573 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11574
11575 eassert (ASIZE (image) >= idx);
11576 image = AREF (image, idx);
11577 }
11578 else
11579 idx = -1;
11580
11581 /* Ignore invalid image specifications. */
11582 if (!valid_image_p (image))
11583 continue;
11584
11585 /* Display the tool-bar button pressed, or depressed. */
11586 plist = Fcopy_sequence (XCDR (image));
11587
11588 /* Compute margin and relief to draw. */
11589 relief = (tool_bar_button_relief >= 0
11590 ? tool_bar_button_relief
11591 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11592 hmargin = vmargin = relief;
11593
11594 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11595 INT_MAX - max (hmargin, vmargin)))
11596 {
11597 hmargin += XFASTINT (Vtool_bar_button_margin);
11598 vmargin += XFASTINT (Vtool_bar_button_margin);
11599 }
11600 else if (CONSP (Vtool_bar_button_margin))
11601 {
11602 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11603 INT_MAX - hmargin))
11604 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11605
11606 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11607 INT_MAX - vmargin))
11608 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11609 }
11610
11611 if (auto_raise_tool_bar_buttons_p)
11612 {
11613 /* Add a `:relief' property to the image spec if the item is
11614 selected. */
11615 if (selected_p)
11616 {
11617 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11618 hmargin -= relief;
11619 vmargin -= relief;
11620 }
11621 }
11622 else
11623 {
11624 /* If image is selected, display it pressed, i.e. with a
11625 negative relief. If it's not selected, display it with a
11626 raised relief. */
11627 plist = Fplist_put (plist, QCrelief,
11628 (selected_p
11629 ? make_number (-relief)
11630 : make_number (relief)));
11631 hmargin -= relief;
11632 vmargin -= relief;
11633 }
11634
11635 /* Put a margin around the image. */
11636 if (hmargin || vmargin)
11637 {
11638 if (hmargin == vmargin)
11639 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11640 else
11641 plist = Fplist_put (plist, QCmargin,
11642 Fcons (make_number (hmargin),
11643 make_number (vmargin)));
11644 }
11645
11646 /* If button is not enabled, and we don't have special images
11647 for the disabled state, make the image appear disabled by
11648 applying an appropriate algorithm to it. */
11649 if (!enabled_p && idx < 0)
11650 plist = Fplist_put (plist, QCconversion, Qdisabled);
11651
11652 /* Put a `display' text property on the string for the image to
11653 display. Put a `menu-item' property on the string that gives
11654 the start of this item's properties in the tool-bar items
11655 vector. */
11656 image = Fcons (Qimage, plist);
11657 props = list4 (Qdisplay, image,
11658 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11659
11660 /* Let the last image hide all remaining spaces in the tool bar
11661 string. The string can be longer than needed when we reuse a
11662 previous string. */
11663 if (i + 1 == f->n_tool_bar_items)
11664 end = SCHARS (f->desired_tool_bar_string);
11665 else
11666 end = i + 1;
11667 Fadd_text_properties (make_number (i), make_number (end),
11668 props, f->desired_tool_bar_string);
11669 #undef PROP
11670 }
11671
11672 UNGCPRO;
11673 }
11674
11675
11676 /* Display one line of the tool-bar of frame IT->f.
11677
11678 HEIGHT specifies the desired height of the tool-bar line.
11679 If the actual height of the glyph row is less than HEIGHT, the
11680 row's height is increased to HEIGHT, and the icons are centered
11681 vertically in the new height.
11682
11683 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11684 count a final empty row in case the tool-bar width exactly matches
11685 the window width.
11686 */
11687
11688 static void
11689 display_tool_bar_line (struct it *it, int height)
11690 {
11691 struct glyph_row *row = it->glyph_row;
11692 int max_x = it->last_visible_x;
11693 struct glyph *last;
11694
11695 prepare_desired_row (row);
11696 row->y = it->current_y;
11697
11698 /* Note that this isn't made use of if the face hasn't a box,
11699 so there's no need to check the face here. */
11700 it->start_of_box_run_p = 1;
11701
11702 while (it->current_x < max_x)
11703 {
11704 int x, n_glyphs_before, i, nglyphs;
11705 struct it it_before;
11706
11707 /* Get the next display element. */
11708 if (!get_next_display_element (it))
11709 {
11710 /* Don't count empty row if we are counting needed tool-bar lines. */
11711 if (height < 0 && !it->hpos)
11712 return;
11713 break;
11714 }
11715
11716 /* Produce glyphs. */
11717 n_glyphs_before = row->used[TEXT_AREA];
11718 it_before = *it;
11719
11720 PRODUCE_GLYPHS (it);
11721
11722 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11723 i = 0;
11724 x = it_before.current_x;
11725 while (i < nglyphs)
11726 {
11727 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11728
11729 if (x + glyph->pixel_width > max_x)
11730 {
11731 /* Glyph doesn't fit on line. Backtrack. */
11732 row->used[TEXT_AREA] = n_glyphs_before;
11733 *it = it_before;
11734 /* If this is the only glyph on this line, it will never fit on the
11735 tool-bar, so skip it. But ensure there is at least one glyph,
11736 so we don't accidentally disable the tool-bar. */
11737 if (n_glyphs_before == 0
11738 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11739 break;
11740 goto out;
11741 }
11742
11743 ++it->hpos;
11744 x += glyph->pixel_width;
11745 ++i;
11746 }
11747
11748 /* Stop at line end. */
11749 if (ITERATOR_AT_END_OF_LINE_P (it))
11750 break;
11751
11752 set_iterator_to_next (it, 1);
11753 }
11754
11755 out:;
11756
11757 row->displays_text_p = row->used[TEXT_AREA] != 0;
11758
11759 /* Use default face for the border below the tool bar.
11760
11761 FIXME: When auto-resize-tool-bars is grow-only, there is
11762 no additional border below the possibly empty tool-bar lines.
11763 So to make the extra empty lines look "normal", we have to
11764 use the tool-bar face for the border too. */
11765 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11766 it->face_id = DEFAULT_FACE_ID;
11767
11768 extend_face_to_end_of_line (it);
11769 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11770 last->right_box_line_p = 1;
11771 if (last == row->glyphs[TEXT_AREA])
11772 last->left_box_line_p = 1;
11773
11774 /* Make line the desired height and center it vertically. */
11775 if ((height -= it->max_ascent + it->max_descent) > 0)
11776 {
11777 /* Don't add more than one line height. */
11778 height %= FRAME_LINE_HEIGHT (it->f);
11779 it->max_ascent += height / 2;
11780 it->max_descent += (height + 1) / 2;
11781 }
11782
11783 compute_line_metrics (it);
11784
11785 /* If line is empty, make it occupy the rest of the tool-bar. */
11786 if (!row->displays_text_p)
11787 {
11788 row->height = row->phys_height = it->last_visible_y - row->y;
11789 row->visible_height = row->height;
11790 row->ascent = row->phys_ascent = 0;
11791 row->extra_line_spacing = 0;
11792 }
11793
11794 row->full_width_p = 1;
11795 row->continued_p = 0;
11796 row->truncated_on_left_p = 0;
11797 row->truncated_on_right_p = 0;
11798
11799 it->current_x = it->hpos = 0;
11800 it->current_y += row->height;
11801 ++it->vpos;
11802 ++it->glyph_row;
11803 }
11804
11805
11806 /* Max tool-bar height. */
11807
11808 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11809 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11810
11811 /* Value is the number of screen lines needed to make all tool-bar
11812 items of frame F visible. The number of actual rows needed is
11813 returned in *N_ROWS if non-NULL. */
11814
11815 static int
11816 tool_bar_lines_needed (struct frame *f, int *n_rows)
11817 {
11818 struct window *w = XWINDOW (f->tool_bar_window);
11819 struct it it;
11820 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11821 the desired matrix, so use (unused) mode-line row as temporary row to
11822 avoid destroying the first tool-bar row. */
11823 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11824
11825 /* Initialize an iterator for iteration over
11826 F->desired_tool_bar_string in the tool-bar window of frame F. */
11827 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11828 it.first_visible_x = 0;
11829 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11830 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11831 it.paragraph_embedding = L2R;
11832
11833 while (!ITERATOR_AT_END_P (&it))
11834 {
11835 clear_glyph_row (temp_row);
11836 it.glyph_row = temp_row;
11837 display_tool_bar_line (&it, -1);
11838 }
11839 clear_glyph_row (temp_row);
11840
11841 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11842 if (n_rows)
11843 *n_rows = it.vpos > 0 ? it.vpos : -1;
11844
11845 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11846 }
11847
11848
11849 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11850 0, 1, 0,
11851 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11852 If FRAME is nil or omitted, use the selected frame. */)
11853 (Lisp_Object frame)
11854 {
11855 struct frame *f = decode_any_frame (frame);
11856 struct window *w;
11857 int nlines = 0;
11858
11859 if (WINDOWP (f->tool_bar_window)
11860 && (w = XWINDOW (f->tool_bar_window),
11861 WINDOW_TOTAL_LINES (w) > 0))
11862 {
11863 update_tool_bar (f, 1);
11864 if (f->n_tool_bar_items)
11865 {
11866 build_desired_tool_bar_string (f);
11867 nlines = tool_bar_lines_needed (f, NULL);
11868 }
11869 }
11870
11871 return make_number (nlines);
11872 }
11873
11874
11875 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11876 height should be changed. */
11877
11878 static int
11879 redisplay_tool_bar (struct frame *f)
11880 {
11881 struct window *w;
11882 struct it it;
11883 struct glyph_row *row;
11884
11885 #if defined (USE_GTK) || defined (HAVE_NS)
11886 if (FRAME_EXTERNAL_TOOL_BAR (f))
11887 update_frame_tool_bar (f);
11888 return 0;
11889 #endif
11890
11891 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11892 do anything. This means you must start with tool-bar-lines
11893 non-zero to get the auto-sizing effect. Or in other words, you
11894 can turn off tool-bars by specifying tool-bar-lines zero. */
11895 if (!WINDOWP (f->tool_bar_window)
11896 || (w = XWINDOW (f->tool_bar_window),
11897 WINDOW_TOTAL_LINES (w) == 0))
11898 return 0;
11899
11900 /* Set up an iterator for the tool-bar window. */
11901 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11902 it.first_visible_x = 0;
11903 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11904 row = it.glyph_row;
11905
11906 /* Build a string that represents the contents of the tool-bar. */
11907 build_desired_tool_bar_string (f);
11908 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11909 /* FIXME: This should be controlled by a user option. But it
11910 doesn't make sense to have an R2L tool bar if the menu bar cannot
11911 be drawn also R2L, and making the menu bar R2L is tricky due
11912 toolkit-specific code that implements it. If an R2L tool bar is
11913 ever supported, display_tool_bar_line should also be augmented to
11914 call unproduce_glyphs like display_line and display_string
11915 do. */
11916 it.paragraph_embedding = L2R;
11917
11918 if (f->n_tool_bar_rows == 0)
11919 {
11920 int nlines;
11921
11922 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11923 nlines != WINDOW_TOTAL_LINES (w)))
11924 {
11925 Lisp_Object frame;
11926 int old_height = WINDOW_TOTAL_LINES (w);
11927
11928 XSETFRAME (frame, f);
11929 Fmodify_frame_parameters (frame,
11930 Fcons (Fcons (Qtool_bar_lines,
11931 make_number (nlines)),
11932 Qnil));
11933 if (WINDOW_TOTAL_LINES (w) != old_height)
11934 {
11935 clear_glyph_matrix (w->desired_matrix);
11936 fonts_changed_p = 1;
11937 return 1;
11938 }
11939 }
11940 }
11941
11942 /* Display as many lines as needed to display all tool-bar items. */
11943
11944 if (f->n_tool_bar_rows > 0)
11945 {
11946 int border, rows, height, extra;
11947
11948 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11949 border = XINT (Vtool_bar_border);
11950 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11951 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11952 else if (EQ (Vtool_bar_border, Qborder_width))
11953 border = f->border_width;
11954 else
11955 border = 0;
11956 if (border < 0)
11957 border = 0;
11958
11959 rows = f->n_tool_bar_rows;
11960 height = max (1, (it.last_visible_y - border) / rows);
11961 extra = it.last_visible_y - border - height * rows;
11962
11963 while (it.current_y < it.last_visible_y)
11964 {
11965 int h = 0;
11966 if (extra > 0 && rows-- > 0)
11967 {
11968 h = (extra + rows - 1) / rows;
11969 extra -= h;
11970 }
11971 display_tool_bar_line (&it, height + h);
11972 }
11973 }
11974 else
11975 {
11976 while (it.current_y < it.last_visible_y)
11977 display_tool_bar_line (&it, 0);
11978 }
11979
11980 /* It doesn't make much sense to try scrolling in the tool-bar
11981 window, so don't do it. */
11982 w->desired_matrix->no_scrolling_p = 1;
11983 w->must_be_updated_p = 1;
11984
11985 if (!NILP (Vauto_resize_tool_bars))
11986 {
11987 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11988 int change_height_p = 0;
11989
11990 /* If we couldn't display everything, change the tool-bar's
11991 height if there is room for more. */
11992 if (IT_STRING_CHARPOS (it) < it.end_charpos
11993 && it.current_y < max_tool_bar_height)
11994 change_height_p = 1;
11995
11996 row = it.glyph_row - 1;
11997
11998 /* If there are blank lines at the end, except for a partially
11999 visible blank line at the end that is smaller than
12000 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12001 if (!row->displays_text_p
12002 && row->height >= FRAME_LINE_HEIGHT (f))
12003 change_height_p = 1;
12004
12005 /* If row displays tool-bar items, but is partially visible,
12006 change the tool-bar's height. */
12007 if (row->displays_text_p
12008 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12009 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12010 change_height_p = 1;
12011
12012 /* Resize windows as needed by changing the `tool-bar-lines'
12013 frame parameter. */
12014 if (change_height_p)
12015 {
12016 Lisp_Object frame;
12017 int old_height = WINDOW_TOTAL_LINES (w);
12018 int nrows;
12019 int nlines = tool_bar_lines_needed (f, &nrows);
12020
12021 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12022 && !f->minimize_tool_bar_window_p)
12023 ? (nlines > old_height)
12024 : (nlines != old_height));
12025 f->minimize_tool_bar_window_p = 0;
12026
12027 if (change_height_p)
12028 {
12029 XSETFRAME (frame, f);
12030 Fmodify_frame_parameters (frame,
12031 Fcons (Fcons (Qtool_bar_lines,
12032 make_number (nlines)),
12033 Qnil));
12034 if (WINDOW_TOTAL_LINES (w) != old_height)
12035 {
12036 clear_glyph_matrix (w->desired_matrix);
12037 f->n_tool_bar_rows = nrows;
12038 fonts_changed_p = 1;
12039 return 1;
12040 }
12041 }
12042 }
12043 }
12044
12045 f->minimize_tool_bar_window_p = 0;
12046 return 0;
12047 }
12048
12049
12050 /* Get information about the tool-bar item which is displayed in GLYPH
12051 on frame F. Return in *PROP_IDX the index where tool-bar item
12052 properties start in F->tool_bar_items. Value is zero if
12053 GLYPH doesn't display a tool-bar item. */
12054
12055 static int
12056 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12057 {
12058 Lisp_Object prop;
12059 int success_p;
12060 int charpos;
12061
12062 /* This function can be called asynchronously, which means we must
12063 exclude any possibility that Fget_text_property signals an
12064 error. */
12065 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12066 charpos = max (0, charpos);
12067
12068 /* Get the text property `menu-item' at pos. The value of that
12069 property is the start index of this item's properties in
12070 F->tool_bar_items. */
12071 prop = Fget_text_property (make_number (charpos),
12072 Qmenu_item, f->current_tool_bar_string);
12073 if (INTEGERP (prop))
12074 {
12075 *prop_idx = XINT (prop);
12076 success_p = 1;
12077 }
12078 else
12079 success_p = 0;
12080
12081 return success_p;
12082 }
12083
12084 \f
12085 /* Get information about the tool-bar item at position X/Y on frame F.
12086 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12087 the current matrix of the tool-bar window of F, or NULL if not
12088 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12089 item in F->tool_bar_items. Value is
12090
12091 -1 if X/Y is not on a tool-bar item
12092 0 if X/Y is on the same item that was highlighted before.
12093 1 otherwise. */
12094
12095 static int
12096 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12097 int *hpos, int *vpos, int *prop_idx)
12098 {
12099 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12100 struct window *w = XWINDOW (f->tool_bar_window);
12101 int area;
12102
12103 /* Find the glyph under X/Y. */
12104 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12105 if (*glyph == NULL)
12106 return -1;
12107
12108 /* Get the start of this tool-bar item's properties in
12109 f->tool_bar_items. */
12110 if (!tool_bar_item_info (f, *glyph, prop_idx))
12111 return -1;
12112
12113 /* Is mouse on the highlighted item? */
12114 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12115 && *vpos >= hlinfo->mouse_face_beg_row
12116 && *vpos <= hlinfo->mouse_face_end_row
12117 && (*vpos > hlinfo->mouse_face_beg_row
12118 || *hpos >= hlinfo->mouse_face_beg_col)
12119 && (*vpos < hlinfo->mouse_face_end_row
12120 || *hpos < hlinfo->mouse_face_end_col
12121 || hlinfo->mouse_face_past_end))
12122 return 0;
12123
12124 return 1;
12125 }
12126
12127
12128 /* EXPORT:
12129 Handle mouse button event on the tool-bar of frame F, at
12130 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12131 0 for button release. MODIFIERS is event modifiers for button
12132 release. */
12133
12134 void
12135 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12136 int modifiers)
12137 {
12138 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12139 struct window *w = XWINDOW (f->tool_bar_window);
12140 int hpos, vpos, prop_idx;
12141 struct glyph *glyph;
12142 Lisp_Object enabled_p;
12143
12144 /* If not on the highlighted tool-bar item, return. */
12145 frame_to_window_pixel_xy (w, &x, &y);
12146 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12147 return;
12148
12149 /* If item is disabled, do nothing. */
12150 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12151 if (NILP (enabled_p))
12152 return;
12153
12154 if (down_p)
12155 {
12156 /* Show item in pressed state. */
12157 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12158 last_tool_bar_item = prop_idx;
12159 }
12160 else
12161 {
12162 Lisp_Object key, frame;
12163 struct input_event event;
12164 EVENT_INIT (event);
12165
12166 /* Show item in released state. */
12167 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12168
12169 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12170
12171 XSETFRAME (frame, f);
12172 event.kind = TOOL_BAR_EVENT;
12173 event.frame_or_window = frame;
12174 event.arg = frame;
12175 kbd_buffer_store_event (&event);
12176
12177 event.kind = TOOL_BAR_EVENT;
12178 event.frame_or_window = frame;
12179 event.arg = key;
12180 event.modifiers = modifiers;
12181 kbd_buffer_store_event (&event);
12182 last_tool_bar_item = -1;
12183 }
12184 }
12185
12186
12187 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12188 tool-bar window-relative coordinates X/Y. Called from
12189 note_mouse_highlight. */
12190
12191 static void
12192 note_tool_bar_highlight (struct frame *f, int x, int y)
12193 {
12194 Lisp_Object window = f->tool_bar_window;
12195 struct window *w = XWINDOW (window);
12196 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12197 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12198 int hpos, vpos;
12199 struct glyph *glyph;
12200 struct glyph_row *row;
12201 int i;
12202 Lisp_Object enabled_p;
12203 int prop_idx;
12204 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12205 int mouse_down_p, rc;
12206
12207 /* Function note_mouse_highlight is called with negative X/Y
12208 values when mouse moves outside of the frame. */
12209 if (x <= 0 || y <= 0)
12210 {
12211 clear_mouse_face (hlinfo);
12212 return;
12213 }
12214
12215 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12216 if (rc < 0)
12217 {
12218 /* Not on tool-bar item. */
12219 clear_mouse_face (hlinfo);
12220 return;
12221 }
12222 else if (rc == 0)
12223 /* On same tool-bar item as before. */
12224 goto set_help_echo;
12225
12226 clear_mouse_face (hlinfo);
12227
12228 /* Mouse is down, but on different tool-bar item? */
12229 mouse_down_p = (dpyinfo->grabbed
12230 && f == last_mouse_frame
12231 && FRAME_LIVE_P (f));
12232 if (mouse_down_p
12233 && last_tool_bar_item != prop_idx)
12234 return;
12235
12236 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12237
12238 /* If tool-bar item is not enabled, don't highlight it. */
12239 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12240 if (!NILP (enabled_p))
12241 {
12242 /* Compute the x-position of the glyph. In front and past the
12243 image is a space. We include this in the highlighted area. */
12244 row = MATRIX_ROW (w->current_matrix, vpos);
12245 for (i = x = 0; i < hpos; ++i)
12246 x += row->glyphs[TEXT_AREA][i].pixel_width;
12247
12248 /* Record this as the current active region. */
12249 hlinfo->mouse_face_beg_col = hpos;
12250 hlinfo->mouse_face_beg_row = vpos;
12251 hlinfo->mouse_face_beg_x = x;
12252 hlinfo->mouse_face_beg_y = row->y;
12253 hlinfo->mouse_face_past_end = 0;
12254
12255 hlinfo->mouse_face_end_col = hpos + 1;
12256 hlinfo->mouse_face_end_row = vpos;
12257 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12258 hlinfo->mouse_face_end_y = row->y;
12259 hlinfo->mouse_face_window = window;
12260 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12261
12262 /* Display it as active. */
12263 show_mouse_face (hlinfo, draw);
12264 }
12265
12266 set_help_echo:
12267
12268 /* Set help_echo_string to a help string to display for this tool-bar item.
12269 XTread_socket does the rest. */
12270 help_echo_object = help_echo_window = Qnil;
12271 help_echo_pos = -1;
12272 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12273 if (NILP (help_echo_string))
12274 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12275 }
12276
12277 #endif /* HAVE_WINDOW_SYSTEM */
12278
12279
12280 \f
12281 /************************************************************************
12282 Horizontal scrolling
12283 ************************************************************************/
12284
12285 static int hscroll_window_tree (Lisp_Object);
12286 static int hscroll_windows (Lisp_Object);
12287
12288 /* For all leaf windows in the window tree rooted at WINDOW, set their
12289 hscroll value so that PT is (i) visible in the window, and (ii) so
12290 that it is not within a certain margin at the window's left and
12291 right border. Value is non-zero if any window's hscroll has been
12292 changed. */
12293
12294 static int
12295 hscroll_window_tree (Lisp_Object window)
12296 {
12297 int hscrolled_p = 0;
12298 int hscroll_relative_p = FLOATP (Vhscroll_step);
12299 int hscroll_step_abs = 0;
12300 double hscroll_step_rel = 0;
12301
12302 if (hscroll_relative_p)
12303 {
12304 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12305 if (hscroll_step_rel < 0)
12306 {
12307 hscroll_relative_p = 0;
12308 hscroll_step_abs = 0;
12309 }
12310 }
12311 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12312 {
12313 hscroll_step_abs = XINT (Vhscroll_step);
12314 if (hscroll_step_abs < 0)
12315 hscroll_step_abs = 0;
12316 }
12317 else
12318 hscroll_step_abs = 0;
12319
12320 while (WINDOWP (window))
12321 {
12322 struct window *w = XWINDOW (window);
12323
12324 if (WINDOWP (w->hchild))
12325 hscrolled_p |= hscroll_window_tree (w->hchild);
12326 else if (WINDOWP (w->vchild))
12327 hscrolled_p |= hscroll_window_tree (w->vchild);
12328 else if (w->cursor.vpos >= 0)
12329 {
12330 int h_margin;
12331 int text_area_width;
12332 struct glyph_row *current_cursor_row
12333 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12334 struct glyph_row *desired_cursor_row
12335 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12336 struct glyph_row *cursor_row
12337 = (desired_cursor_row->enabled_p
12338 ? desired_cursor_row
12339 : current_cursor_row);
12340 int row_r2l_p = cursor_row->reversed_p;
12341
12342 text_area_width = window_box_width (w, TEXT_AREA);
12343
12344 /* Scroll when cursor is inside this scroll margin. */
12345 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12346
12347 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12348 /* For left-to-right rows, hscroll when cursor is either
12349 (i) inside the right hscroll margin, or (ii) if it is
12350 inside the left margin and the window is already
12351 hscrolled. */
12352 && ((!row_r2l_p
12353 && ((w->hscroll
12354 && w->cursor.x <= h_margin)
12355 || (cursor_row->enabled_p
12356 && cursor_row->truncated_on_right_p
12357 && (w->cursor.x >= text_area_width - h_margin))))
12358 /* For right-to-left rows, the logic is similar,
12359 except that rules for scrolling to left and right
12360 are reversed. E.g., if cursor.x <= h_margin, we
12361 need to hscroll "to the right" unconditionally,
12362 and that will scroll the screen to the left so as
12363 to reveal the next portion of the row. */
12364 || (row_r2l_p
12365 && ((cursor_row->enabled_p
12366 /* FIXME: It is confusing to set the
12367 truncated_on_right_p flag when R2L rows
12368 are actually truncated on the left. */
12369 && cursor_row->truncated_on_right_p
12370 && w->cursor.x <= h_margin)
12371 || (w->hscroll
12372 && (w->cursor.x >= text_area_width - h_margin))))))
12373 {
12374 struct it it;
12375 ptrdiff_t hscroll;
12376 struct buffer *saved_current_buffer;
12377 ptrdiff_t pt;
12378 int wanted_x;
12379
12380 /* Find point in a display of infinite width. */
12381 saved_current_buffer = current_buffer;
12382 current_buffer = XBUFFER (w->buffer);
12383
12384 if (w == XWINDOW (selected_window))
12385 pt = PT;
12386 else
12387 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12388
12389 /* Move iterator to pt starting at cursor_row->start in
12390 a line with infinite width. */
12391 init_to_row_start (&it, w, cursor_row);
12392 it.last_visible_x = INFINITY;
12393 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12394 current_buffer = saved_current_buffer;
12395
12396 /* Position cursor in window. */
12397 if (!hscroll_relative_p && hscroll_step_abs == 0)
12398 hscroll = max (0, (it.current_x
12399 - (ITERATOR_AT_END_OF_LINE_P (&it)
12400 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12401 : (text_area_width / 2))))
12402 / FRAME_COLUMN_WIDTH (it.f);
12403 else if ((!row_r2l_p
12404 && w->cursor.x >= text_area_width - h_margin)
12405 || (row_r2l_p && w->cursor.x <= h_margin))
12406 {
12407 if (hscroll_relative_p)
12408 wanted_x = text_area_width * (1 - hscroll_step_rel)
12409 - h_margin;
12410 else
12411 wanted_x = text_area_width
12412 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12413 - h_margin;
12414 hscroll
12415 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12416 }
12417 else
12418 {
12419 if (hscroll_relative_p)
12420 wanted_x = text_area_width * hscroll_step_rel
12421 + h_margin;
12422 else
12423 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12424 + h_margin;
12425 hscroll
12426 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12427 }
12428 hscroll = max (hscroll, w->min_hscroll);
12429
12430 /* Don't prevent redisplay optimizations if hscroll
12431 hasn't changed, as it will unnecessarily slow down
12432 redisplay. */
12433 if (w->hscroll != hscroll)
12434 {
12435 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12436 w->hscroll = hscroll;
12437 hscrolled_p = 1;
12438 }
12439 }
12440 }
12441
12442 window = w->next;
12443 }
12444
12445 /* Value is non-zero if hscroll of any leaf window has been changed. */
12446 return hscrolled_p;
12447 }
12448
12449
12450 /* Set hscroll so that cursor is visible and not inside horizontal
12451 scroll margins for all windows in the tree rooted at WINDOW. See
12452 also hscroll_window_tree above. Value is non-zero if any window's
12453 hscroll has been changed. If it has, desired matrices on the frame
12454 of WINDOW are cleared. */
12455
12456 static int
12457 hscroll_windows (Lisp_Object window)
12458 {
12459 int hscrolled_p = hscroll_window_tree (window);
12460 if (hscrolled_p)
12461 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12462 return hscrolled_p;
12463 }
12464
12465
12466 \f
12467 /************************************************************************
12468 Redisplay
12469 ************************************************************************/
12470
12471 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12472 to a non-zero value. This is sometimes handy to have in a debugger
12473 session. */
12474
12475 #ifdef GLYPH_DEBUG
12476
12477 /* First and last unchanged row for try_window_id. */
12478
12479 static int debug_first_unchanged_at_end_vpos;
12480 static int debug_last_unchanged_at_beg_vpos;
12481
12482 /* Delta vpos and y. */
12483
12484 static int debug_dvpos, debug_dy;
12485
12486 /* Delta in characters and bytes for try_window_id. */
12487
12488 static ptrdiff_t debug_delta, debug_delta_bytes;
12489
12490 /* Values of window_end_pos and window_end_vpos at the end of
12491 try_window_id. */
12492
12493 static ptrdiff_t debug_end_vpos;
12494
12495 /* Append a string to W->desired_matrix->method. FMT is a printf
12496 format string. If trace_redisplay_p is non-zero also printf the
12497 resulting string to stderr. */
12498
12499 static void debug_method_add (struct window *, char const *, ...)
12500 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12501
12502 static void
12503 debug_method_add (struct window *w, char const *fmt, ...)
12504 {
12505 char *method = w->desired_matrix->method;
12506 int len = strlen (method);
12507 int size = sizeof w->desired_matrix->method;
12508 int remaining = size - len - 1;
12509 va_list ap;
12510
12511 if (len && remaining)
12512 {
12513 method[len] = '|';
12514 --remaining, ++len;
12515 }
12516
12517 va_start (ap, fmt);
12518 vsnprintf (method + len, remaining + 1, fmt, ap);
12519 va_end (ap);
12520
12521 if (trace_redisplay_p)
12522 fprintf (stderr, "%p (%s): %s\n",
12523 w,
12524 ((BUFFERP (w->buffer)
12525 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12526 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12527 : "no buffer"),
12528 method + len);
12529 }
12530
12531 #endif /* GLYPH_DEBUG */
12532
12533
12534 /* Value is non-zero if all changes in window W, which displays
12535 current_buffer, are in the text between START and END. START is a
12536 buffer position, END is given as a distance from Z. Used in
12537 redisplay_internal for display optimization. */
12538
12539 static int
12540 text_outside_line_unchanged_p (struct window *w,
12541 ptrdiff_t start, ptrdiff_t end)
12542 {
12543 int unchanged_p = 1;
12544
12545 /* If text or overlays have changed, see where. */
12546 if (window_outdated (w))
12547 {
12548 /* Gap in the line? */
12549 if (GPT < start || Z - GPT < end)
12550 unchanged_p = 0;
12551
12552 /* Changes start in front of the line, or end after it? */
12553 if (unchanged_p
12554 && (BEG_UNCHANGED < start - 1
12555 || END_UNCHANGED < end))
12556 unchanged_p = 0;
12557
12558 /* If selective display, can't optimize if changes start at the
12559 beginning of the line. */
12560 if (unchanged_p
12561 && INTEGERP (BVAR (current_buffer, selective_display))
12562 && XINT (BVAR (current_buffer, selective_display)) > 0
12563 && (BEG_UNCHANGED < start || GPT <= start))
12564 unchanged_p = 0;
12565
12566 /* If there are overlays at the start or end of the line, these
12567 may have overlay strings with newlines in them. A change at
12568 START, for instance, may actually concern the display of such
12569 overlay strings as well, and they are displayed on different
12570 lines. So, quickly rule out this case. (For the future, it
12571 might be desirable to implement something more telling than
12572 just BEG/END_UNCHANGED.) */
12573 if (unchanged_p)
12574 {
12575 if (BEG + BEG_UNCHANGED == start
12576 && overlay_touches_p (start))
12577 unchanged_p = 0;
12578 if (END_UNCHANGED == end
12579 && overlay_touches_p (Z - end))
12580 unchanged_p = 0;
12581 }
12582
12583 /* Under bidi reordering, adding or deleting a character in the
12584 beginning of a paragraph, before the first strong directional
12585 character, can change the base direction of the paragraph (unless
12586 the buffer specifies a fixed paragraph direction), which will
12587 require to redisplay the whole paragraph. It might be worthwhile
12588 to find the paragraph limits and widen the range of redisplayed
12589 lines to that, but for now just give up this optimization. */
12590 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12591 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12592 unchanged_p = 0;
12593 }
12594
12595 return unchanged_p;
12596 }
12597
12598
12599 /* Do a frame update, taking possible shortcuts into account. This is
12600 the main external entry point for redisplay.
12601
12602 If the last redisplay displayed an echo area message and that message
12603 is no longer requested, we clear the echo area or bring back the
12604 mini-buffer if that is in use. */
12605
12606 void
12607 redisplay (void)
12608 {
12609 redisplay_internal ();
12610 }
12611
12612
12613 static Lisp_Object
12614 overlay_arrow_string_or_property (Lisp_Object var)
12615 {
12616 Lisp_Object val;
12617
12618 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12619 return val;
12620
12621 return Voverlay_arrow_string;
12622 }
12623
12624 /* Return 1 if there are any overlay-arrows in current_buffer. */
12625 static int
12626 overlay_arrow_in_current_buffer_p (void)
12627 {
12628 Lisp_Object vlist;
12629
12630 for (vlist = Voverlay_arrow_variable_list;
12631 CONSP (vlist);
12632 vlist = XCDR (vlist))
12633 {
12634 Lisp_Object var = XCAR (vlist);
12635 Lisp_Object val;
12636
12637 if (!SYMBOLP (var))
12638 continue;
12639 val = find_symbol_value (var);
12640 if (MARKERP (val)
12641 && current_buffer == XMARKER (val)->buffer)
12642 return 1;
12643 }
12644 return 0;
12645 }
12646
12647
12648 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12649 has changed. */
12650
12651 static int
12652 overlay_arrows_changed_p (void)
12653 {
12654 Lisp_Object vlist;
12655
12656 for (vlist = Voverlay_arrow_variable_list;
12657 CONSP (vlist);
12658 vlist = XCDR (vlist))
12659 {
12660 Lisp_Object var = XCAR (vlist);
12661 Lisp_Object val, pstr;
12662
12663 if (!SYMBOLP (var))
12664 continue;
12665 val = find_symbol_value (var);
12666 if (!MARKERP (val))
12667 continue;
12668 if (! EQ (COERCE_MARKER (val),
12669 Fget (var, Qlast_arrow_position))
12670 || ! (pstr = overlay_arrow_string_or_property (var),
12671 EQ (pstr, Fget (var, Qlast_arrow_string))))
12672 return 1;
12673 }
12674 return 0;
12675 }
12676
12677 /* Mark overlay arrows to be updated on next redisplay. */
12678
12679 static void
12680 update_overlay_arrows (int up_to_date)
12681 {
12682 Lisp_Object vlist;
12683
12684 for (vlist = Voverlay_arrow_variable_list;
12685 CONSP (vlist);
12686 vlist = XCDR (vlist))
12687 {
12688 Lisp_Object var = XCAR (vlist);
12689
12690 if (!SYMBOLP (var))
12691 continue;
12692
12693 if (up_to_date > 0)
12694 {
12695 Lisp_Object val = find_symbol_value (var);
12696 Fput (var, Qlast_arrow_position,
12697 COERCE_MARKER (val));
12698 Fput (var, Qlast_arrow_string,
12699 overlay_arrow_string_or_property (var));
12700 }
12701 else if (up_to_date < 0
12702 || !NILP (Fget (var, Qlast_arrow_position)))
12703 {
12704 Fput (var, Qlast_arrow_position, Qt);
12705 Fput (var, Qlast_arrow_string, Qt);
12706 }
12707 }
12708 }
12709
12710
12711 /* Return overlay arrow string to display at row.
12712 Return integer (bitmap number) for arrow bitmap in left fringe.
12713 Return nil if no overlay arrow. */
12714
12715 static Lisp_Object
12716 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12717 {
12718 Lisp_Object vlist;
12719
12720 for (vlist = Voverlay_arrow_variable_list;
12721 CONSP (vlist);
12722 vlist = XCDR (vlist))
12723 {
12724 Lisp_Object var = XCAR (vlist);
12725 Lisp_Object val;
12726
12727 if (!SYMBOLP (var))
12728 continue;
12729
12730 val = find_symbol_value (var);
12731
12732 if (MARKERP (val)
12733 && current_buffer == XMARKER (val)->buffer
12734 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12735 {
12736 if (FRAME_WINDOW_P (it->f)
12737 /* FIXME: if ROW->reversed_p is set, this should test
12738 the right fringe, not the left one. */
12739 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12740 {
12741 #ifdef HAVE_WINDOW_SYSTEM
12742 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12743 {
12744 int fringe_bitmap;
12745 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12746 return make_number (fringe_bitmap);
12747 }
12748 #endif
12749 return make_number (-1); /* Use default arrow bitmap. */
12750 }
12751 return overlay_arrow_string_or_property (var);
12752 }
12753 }
12754
12755 return Qnil;
12756 }
12757
12758 /* Return 1 if point moved out of or into a composition. Otherwise
12759 return 0. PREV_BUF and PREV_PT are the last point buffer and
12760 position. BUF and PT are the current point buffer and position. */
12761
12762 static int
12763 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12764 struct buffer *buf, ptrdiff_t pt)
12765 {
12766 ptrdiff_t start, end;
12767 Lisp_Object prop;
12768 Lisp_Object buffer;
12769
12770 XSETBUFFER (buffer, buf);
12771 /* Check a composition at the last point if point moved within the
12772 same buffer. */
12773 if (prev_buf == buf)
12774 {
12775 if (prev_pt == pt)
12776 /* Point didn't move. */
12777 return 0;
12778
12779 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12780 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12781 && COMPOSITION_VALID_P (start, end, prop)
12782 && start < prev_pt && end > prev_pt)
12783 /* The last point was within the composition. Return 1 iff
12784 point moved out of the composition. */
12785 return (pt <= start || pt >= end);
12786 }
12787
12788 /* Check a composition at the current point. */
12789 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12790 && find_composition (pt, -1, &start, &end, &prop, buffer)
12791 && COMPOSITION_VALID_P (start, end, prop)
12792 && start < pt && end > pt);
12793 }
12794
12795
12796 /* Reconsider the setting of B->clip_changed which is displayed
12797 in window W. */
12798
12799 static void
12800 reconsider_clip_changes (struct window *w, struct buffer *b)
12801 {
12802 if (b->clip_changed
12803 && w->window_end_valid
12804 && w->current_matrix->buffer == b
12805 && w->current_matrix->zv == BUF_ZV (b)
12806 && w->current_matrix->begv == BUF_BEGV (b))
12807 b->clip_changed = 0;
12808
12809 /* If display wasn't paused, and W is not a tool bar window, see if
12810 point has been moved into or out of a composition. In that case,
12811 we set b->clip_changed to 1 to force updating the screen. If
12812 b->clip_changed has already been set to 1, we can skip this
12813 check. */
12814 if (!b->clip_changed && BUFFERP (w->buffer) && w->window_end_valid)
12815 {
12816 ptrdiff_t pt;
12817
12818 if (w == XWINDOW (selected_window))
12819 pt = PT;
12820 else
12821 pt = marker_position (w->pointm);
12822
12823 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12824 || pt != w->last_point)
12825 && check_point_in_composition (w->current_matrix->buffer,
12826 w->last_point,
12827 XBUFFER (w->buffer), pt))
12828 b->clip_changed = 1;
12829 }
12830 }
12831 \f
12832
12833 #define STOP_POLLING \
12834 do { if (! polling_stopped_here) stop_polling (); \
12835 polling_stopped_here = 1; } while (0)
12836
12837 #define RESUME_POLLING \
12838 do { if (polling_stopped_here) start_polling (); \
12839 polling_stopped_here = 0; } while (0)
12840
12841
12842 /* Perhaps in the future avoid recentering windows if it
12843 is not necessary; currently that causes some problems. */
12844
12845 static void
12846 redisplay_internal (void)
12847 {
12848 struct window *w = XWINDOW (selected_window);
12849 struct window *sw;
12850 struct frame *fr;
12851 int pending;
12852 int must_finish = 0;
12853 struct text_pos tlbufpos, tlendpos;
12854 int number_of_visible_frames;
12855 ptrdiff_t count, count1;
12856 struct frame *sf;
12857 int polling_stopped_here = 0;
12858 Lisp_Object tail, frame;
12859 struct backtrace backtrace;
12860
12861 /* Non-zero means redisplay has to consider all windows on all
12862 frames. Zero means, only selected_window is considered. */
12863 int consider_all_windows_p;
12864
12865 /* Non-zero means redisplay has to redisplay the miniwindow. */
12866 int update_miniwindow_p = 0;
12867
12868 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12869
12870 /* No redisplay if running in batch mode or frame is not yet fully
12871 initialized, or redisplay is explicitly turned off by setting
12872 Vinhibit_redisplay. */
12873 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12874 || !NILP (Vinhibit_redisplay))
12875 return;
12876
12877 /* Don't examine these until after testing Vinhibit_redisplay.
12878 When Emacs is shutting down, perhaps because its connection to
12879 X has dropped, we should not look at them at all. */
12880 fr = XFRAME (w->frame);
12881 sf = SELECTED_FRAME ();
12882
12883 if (!fr->glyphs_initialized_p)
12884 return;
12885
12886 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12887 if (popup_activated ())
12888 return;
12889 #endif
12890
12891 /* I don't think this happens but let's be paranoid. */
12892 if (redisplaying_p)
12893 return;
12894
12895 /* Record a function that clears redisplaying_p
12896 when we leave this function. */
12897 count = SPECPDL_INDEX ();
12898 record_unwind_protect (unwind_redisplay, selected_frame);
12899 redisplaying_p = 1;
12900 specbind (Qinhibit_free_realized_faces, Qnil);
12901
12902 /* Record this function, so it appears on the profiler's backtraces. */
12903 backtrace.next = backtrace_list;
12904 backtrace.function = Qredisplay_internal;
12905 backtrace.args = &Qnil;
12906 backtrace.nargs = 0;
12907 backtrace.debug_on_exit = 0;
12908 backtrace_list = &backtrace;
12909
12910 FOR_EACH_FRAME (tail, frame)
12911 XFRAME (frame)->already_hscrolled_p = 0;
12912
12913 retry:
12914 /* Remember the currently selected window. */
12915 sw = w;
12916
12917 pending = 0;
12918 reconsider_clip_changes (w, current_buffer);
12919 last_escape_glyph_frame = NULL;
12920 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12921 last_glyphless_glyph_frame = NULL;
12922 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12923
12924 /* If new fonts have been loaded that make a glyph matrix adjustment
12925 necessary, do it. */
12926 if (fonts_changed_p)
12927 {
12928 adjust_glyphs (NULL);
12929 ++windows_or_buffers_changed;
12930 fonts_changed_p = 0;
12931 }
12932
12933 /* If face_change_count is non-zero, init_iterator will free all
12934 realized faces, which includes the faces referenced from current
12935 matrices. So, we can't reuse current matrices in this case. */
12936 if (face_change_count)
12937 ++windows_or_buffers_changed;
12938
12939 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12940 && FRAME_TTY (sf)->previous_frame != sf)
12941 {
12942 /* Since frames on a single ASCII terminal share the same
12943 display area, displaying a different frame means redisplay
12944 the whole thing. */
12945 windows_or_buffers_changed++;
12946 SET_FRAME_GARBAGED (sf);
12947 #ifndef DOS_NT
12948 set_tty_color_mode (FRAME_TTY (sf), sf);
12949 #endif
12950 FRAME_TTY (sf)->previous_frame = sf;
12951 }
12952
12953 /* Set the visible flags for all frames. Do this before checking for
12954 resized or garbaged frames; they want to know if their frames are
12955 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12956 number_of_visible_frames = 0;
12957
12958 FOR_EACH_FRAME (tail, frame)
12959 {
12960 struct frame *f = XFRAME (frame);
12961
12962 if (FRAME_VISIBLE_P (f))
12963 ++number_of_visible_frames;
12964 clear_desired_matrices (f);
12965 }
12966
12967 /* Notice any pending interrupt request to change frame size. */
12968 do_pending_window_change (1);
12969
12970 /* do_pending_window_change could change the selected_window due to
12971 frame resizing which makes the selected window too small. */
12972 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12973 {
12974 sw = w;
12975 reconsider_clip_changes (w, current_buffer);
12976 }
12977
12978 /* Clear frames marked as garbaged. */
12979 clear_garbaged_frames ();
12980
12981 /* Build menubar and tool-bar items. */
12982 if (NILP (Vmemory_full))
12983 prepare_menu_bars ();
12984
12985 if (windows_or_buffers_changed)
12986 update_mode_lines++;
12987
12988 /* Detect case that we need to write or remove a star in the mode line. */
12989 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12990 {
12991 w->update_mode_line = 1;
12992 if (buffer_shared_and_changed ())
12993 update_mode_lines++;
12994 }
12995
12996 /* Avoid invocation of point motion hooks by `current_column' below. */
12997 count1 = SPECPDL_INDEX ();
12998 specbind (Qinhibit_point_motion_hooks, Qt);
12999
13000 if (mode_line_update_needed (w))
13001 w->update_mode_line = 1;
13002
13003 unbind_to (count1, Qnil);
13004
13005 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13006
13007 consider_all_windows_p = (update_mode_lines
13008 || buffer_shared_and_changed ()
13009 || cursor_type_changed);
13010
13011 /* If specs for an arrow have changed, do thorough redisplay
13012 to ensure we remove any arrow that should no longer exist. */
13013 if (overlay_arrows_changed_p ())
13014 consider_all_windows_p = windows_or_buffers_changed = 1;
13015
13016 /* Normally the message* functions will have already displayed and
13017 updated the echo area, but the frame may have been trashed, or
13018 the update may have been preempted, so display the echo area
13019 again here. Checking message_cleared_p captures the case that
13020 the echo area should be cleared. */
13021 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13022 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13023 || (message_cleared_p
13024 && minibuf_level == 0
13025 /* If the mini-window is currently selected, this means the
13026 echo-area doesn't show through. */
13027 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13028 {
13029 int window_height_changed_p = echo_area_display (0);
13030
13031 if (message_cleared_p)
13032 update_miniwindow_p = 1;
13033
13034 must_finish = 1;
13035
13036 /* If we don't display the current message, don't clear the
13037 message_cleared_p flag, because, if we did, we wouldn't clear
13038 the echo area in the next redisplay which doesn't preserve
13039 the echo area. */
13040 if (!display_last_displayed_message_p)
13041 message_cleared_p = 0;
13042
13043 if (fonts_changed_p)
13044 goto retry;
13045 else if (window_height_changed_p)
13046 {
13047 consider_all_windows_p = 1;
13048 ++update_mode_lines;
13049 ++windows_or_buffers_changed;
13050
13051 /* If window configuration was changed, frames may have been
13052 marked garbaged. Clear them or we will experience
13053 surprises wrt scrolling. */
13054 clear_garbaged_frames ();
13055 }
13056 }
13057 else if (EQ (selected_window, minibuf_window)
13058 && (current_buffer->clip_changed || window_outdated (w))
13059 && resize_mini_window (w, 0))
13060 {
13061 /* Resized active mini-window to fit the size of what it is
13062 showing if its contents might have changed. */
13063 must_finish = 1;
13064 /* FIXME: this causes all frames to be updated, which seems unnecessary
13065 since only the current frame needs to be considered. This function
13066 needs to be rewritten with two variables, consider_all_windows and
13067 consider_all_frames. */
13068 consider_all_windows_p = 1;
13069 ++windows_or_buffers_changed;
13070 ++update_mode_lines;
13071
13072 /* If window configuration was changed, frames may have been
13073 marked garbaged. Clear them or we will experience
13074 surprises wrt scrolling. */
13075 clear_garbaged_frames ();
13076 }
13077
13078 /* If showing the region, and mark has changed, we must redisplay
13079 the whole window. The assignment to this_line_start_pos prevents
13080 the optimization directly below this if-statement. */
13081 if (((!NILP (Vtransient_mark_mode)
13082 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13083 != (w->region_showing > 0))
13084 || (w->region_showing
13085 && w->region_showing
13086 != XINT (Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13087 CHARPOS (this_line_start_pos) = 0;
13088
13089 /* Optimize the case that only the line containing the cursor in the
13090 selected window has changed. Variables starting with this_ are
13091 set in display_line and record information about the line
13092 containing the cursor. */
13093 tlbufpos = this_line_start_pos;
13094 tlendpos = this_line_end_pos;
13095 if (!consider_all_windows_p
13096 && CHARPOS (tlbufpos) > 0
13097 && !w->update_mode_line
13098 && !current_buffer->clip_changed
13099 && !current_buffer->prevent_redisplay_optimizations_p
13100 && FRAME_VISIBLE_P (XFRAME (w->frame))
13101 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13102 /* Make sure recorded data applies to current buffer, etc. */
13103 && this_line_buffer == current_buffer
13104 && current_buffer == XBUFFER (w->buffer)
13105 && !w->force_start
13106 && !w->optional_new_start
13107 /* Point must be on the line that we have info recorded about. */
13108 && PT >= CHARPOS (tlbufpos)
13109 && PT <= Z - CHARPOS (tlendpos)
13110 /* All text outside that line, including its final newline,
13111 must be unchanged. */
13112 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13113 CHARPOS (tlendpos)))
13114 {
13115 if (CHARPOS (tlbufpos) > BEGV
13116 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13117 && (CHARPOS (tlbufpos) == ZV
13118 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13119 /* Former continuation line has disappeared by becoming empty. */
13120 goto cancel;
13121 else if (window_outdated (w) || MINI_WINDOW_P (w))
13122 {
13123 /* We have to handle the case of continuation around a
13124 wide-column character (see the comment in indent.c around
13125 line 1340).
13126
13127 For instance, in the following case:
13128
13129 -------- Insert --------
13130 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13131 J_I_ ==> J_I_ `^^' are cursors.
13132 ^^ ^^
13133 -------- --------
13134
13135 As we have to redraw the line above, we cannot use this
13136 optimization. */
13137
13138 struct it it;
13139 int line_height_before = this_line_pixel_height;
13140
13141 /* Note that start_display will handle the case that the
13142 line starting at tlbufpos is a continuation line. */
13143 start_display (&it, w, tlbufpos);
13144
13145 /* Implementation note: It this still necessary? */
13146 if (it.current_x != this_line_start_x)
13147 goto cancel;
13148
13149 TRACE ((stderr, "trying display optimization 1\n"));
13150 w->cursor.vpos = -1;
13151 overlay_arrow_seen = 0;
13152 it.vpos = this_line_vpos;
13153 it.current_y = this_line_y;
13154 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13155 display_line (&it);
13156
13157 /* If line contains point, is not continued,
13158 and ends at same distance from eob as before, we win. */
13159 if (w->cursor.vpos >= 0
13160 /* Line is not continued, otherwise this_line_start_pos
13161 would have been set to 0 in display_line. */
13162 && CHARPOS (this_line_start_pos)
13163 /* Line ends as before. */
13164 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13165 /* Line has same height as before. Otherwise other lines
13166 would have to be shifted up or down. */
13167 && this_line_pixel_height == line_height_before)
13168 {
13169 /* If this is not the window's last line, we must adjust
13170 the charstarts of the lines below. */
13171 if (it.current_y < it.last_visible_y)
13172 {
13173 struct glyph_row *row
13174 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13175 ptrdiff_t delta, delta_bytes;
13176
13177 /* We used to distinguish between two cases here,
13178 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13179 when the line ends in a newline or the end of the
13180 buffer's accessible portion. But both cases did
13181 the same, so they were collapsed. */
13182 delta = (Z
13183 - CHARPOS (tlendpos)
13184 - MATRIX_ROW_START_CHARPOS (row));
13185 delta_bytes = (Z_BYTE
13186 - BYTEPOS (tlendpos)
13187 - MATRIX_ROW_START_BYTEPOS (row));
13188
13189 increment_matrix_positions (w->current_matrix,
13190 this_line_vpos + 1,
13191 w->current_matrix->nrows,
13192 delta, delta_bytes);
13193 }
13194
13195 /* If this row displays text now but previously didn't,
13196 or vice versa, w->window_end_vpos may have to be
13197 adjusted. */
13198 if ((it.glyph_row - 1)->displays_text_p)
13199 {
13200 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13201 wset_window_end_vpos (w, make_number (this_line_vpos));
13202 }
13203 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13204 && this_line_vpos > 0)
13205 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13206 w->window_end_valid = 0;
13207
13208 /* Update hint: No need to try to scroll in update_window. */
13209 w->desired_matrix->no_scrolling_p = 1;
13210
13211 #ifdef GLYPH_DEBUG
13212 *w->desired_matrix->method = 0;
13213 debug_method_add (w, "optimization 1");
13214 #endif
13215 #if HAVE_XWIDGETS
13216 //debug optimization movement issue
13217 //w->desired_matrix->no_scrolling_p = 1;
13218 //*w->desired_matrix->method = 0;
13219 //debug_method_add (w, "optimization 1");
13220 #endif
13221
13222 #ifdef HAVE_WINDOW_SYSTEM
13223 update_window_fringes (w, 0);
13224 #endif
13225 goto update;
13226 }
13227 else
13228 goto cancel;
13229 }
13230 else if (/* Cursor position hasn't changed. */
13231 PT == w->last_point
13232 /* Make sure the cursor was last displayed
13233 in this window. Otherwise we have to reposition it. */
13234 && 0 <= w->cursor.vpos
13235 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13236 {
13237 if (!must_finish)
13238 {
13239 do_pending_window_change (1);
13240 /* If selected_window changed, redisplay again. */
13241 if (WINDOWP (selected_window)
13242 && (w = XWINDOW (selected_window)) != sw)
13243 goto retry;
13244
13245 /* We used to always goto end_of_redisplay here, but this
13246 isn't enough if we have a blinking cursor. */
13247 if (w->cursor_off_p == w->last_cursor_off_p)
13248 goto end_of_redisplay;
13249 }
13250 goto update;
13251 }
13252 /* If highlighting the region, or if the cursor is in the echo area,
13253 then we can't just move the cursor. */
13254 else if (! (!NILP (Vtransient_mark_mode)
13255 && !NILP (BVAR (current_buffer, mark_active)))
13256 && (EQ (selected_window,
13257 BVAR (current_buffer, last_selected_window))
13258 || highlight_nonselected_windows)
13259 && !w->region_showing
13260 && NILP (Vshow_trailing_whitespace)
13261 && !cursor_in_echo_area)
13262 {
13263 struct it it;
13264 struct glyph_row *row;
13265
13266 /* Skip from tlbufpos to PT and see where it is. Note that
13267 PT may be in invisible text. If so, we will end at the
13268 next visible position. */
13269 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13270 NULL, DEFAULT_FACE_ID);
13271 it.current_x = this_line_start_x;
13272 it.current_y = this_line_y;
13273 it.vpos = this_line_vpos;
13274
13275 /* The call to move_it_to stops in front of PT, but
13276 moves over before-strings. */
13277 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13278
13279 if (it.vpos == this_line_vpos
13280 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13281 row->enabled_p))
13282 {
13283 eassert (this_line_vpos == it.vpos);
13284 eassert (this_line_y == it.current_y);
13285 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13286 #ifdef GLYPH_DEBUG
13287 *w->desired_matrix->method = 0;
13288 debug_method_add (w, "optimization 3");
13289 #endif
13290 goto update;
13291 }
13292 else
13293 goto cancel;
13294 }
13295
13296 cancel:
13297 /* Text changed drastically or point moved off of line. */
13298 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13299 }
13300
13301 CHARPOS (this_line_start_pos) = 0;
13302 consider_all_windows_p |= buffer_shared_and_changed ();
13303 ++clear_face_cache_count;
13304 #ifdef HAVE_WINDOW_SYSTEM
13305 ++clear_image_cache_count;
13306 #endif
13307
13308 /* Build desired matrices, and update the display. If
13309 consider_all_windows_p is non-zero, do it for all windows on all
13310 frames. Otherwise do it for selected_window, only. */
13311
13312 if (consider_all_windows_p)
13313 {
13314 FOR_EACH_FRAME (tail, frame)
13315 XFRAME (frame)->updated_p = 0;
13316
13317 FOR_EACH_FRAME (tail, frame)
13318 {
13319 struct frame *f = XFRAME (frame);
13320
13321 /* We don't have to do anything for unselected terminal
13322 frames. */
13323 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13324 && !EQ (FRAME_TTY (f)->top_frame, frame))
13325 continue;
13326
13327 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13328 {
13329 /* Mark all the scroll bars to be removed; we'll redeem
13330 the ones we want when we redisplay their windows. */
13331 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13332 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13333
13334 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13335 redisplay_windows (FRAME_ROOT_WINDOW (f));
13336
13337 /* The X error handler may have deleted that frame. */
13338 if (!FRAME_LIVE_P (f))
13339 continue;
13340
13341 /* Any scroll bars which redisplay_windows should have
13342 nuked should now go away. */
13343 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13344 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13345
13346 /* If fonts changed, display again. */
13347 /* ??? rms: I suspect it is a mistake to jump all the way
13348 back to retry here. It should just retry this frame. */
13349 if (fonts_changed_p)
13350 goto retry;
13351
13352 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13353 {
13354 /* See if we have to hscroll. */
13355 if (!f->already_hscrolled_p)
13356 {
13357 f->already_hscrolled_p = 1;
13358 if (hscroll_windows (f->root_window))
13359 goto retry;
13360 }
13361
13362 /* Prevent various kinds of signals during display
13363 update. stdio is not robust about handling
13364 signals, which can cause an apparent I/O
13365 error. */
13366 if (interrupt_input)
13367 unrequest_sigio ();
13368 STOP_POLLING;
13369
13370 /* Update the display. */
13371 set_window_update_flags (XWINDOW (f->root_window), 1);
13372 pending |= update_frame (f, 0, 0);
13373 f->updated_p = 1;
13374 }
13375 }
13376 }
13377
13378 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13379
13380 if (!pending)
13381 {
13382 /* Do the mark_window_display_accurate after all windows have
13383 been redisplayed because this call resets flags in buffers
13384 which are needed for proper redisplay. */
13385 FOR_EACH_FRAME (tail, frame)
13386 {
13387 struct frame *f = XFRAME (frame);
13388 if (f->updated_p)
13389 {
13390 mark_window_display_accurate (f->root_window, 1);
13391 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13392 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13393 }
13394 }
13395 }
13396 }
13397 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13398 {
13399 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13400 struct frame *mini_frame;
13401
13402 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13403 /* Use list_of_error, not Qerror, so that
13404 we catch only errors and don't run the debugger. */
13405 internal_condition_case_1 (redisplay_window_1, selected_window,
13406 list_of_error,
13407 redisplay_window_error);
13408 if (update_miniwindow_p)
13409 internal_condition_case_1 (redisplay_window_1, mini_window,
13410 list_of_error,
13411 redisplay_window_error);
13412
13413 /* Compare desired and current matrices, perform output. */
13414
13415 update:
13416 /* If fonts changed, display again. */
13417 if (fonts_changed_p)
13418 goto retry;
13419
13420 /* Prevent various kinds of signals during display update.
13421 stdio is not robust about handling signals,
13422 which can cause an apparent I/O error. */
13423 if (interrupt_input)
13424 unrequest_sigio ();
13425 STOP_POLLING;
13426
13427 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13428 {
13429 if (hscroll_windows (selected_window))
13430 goto retry;
13431
13432 XWINDOW (selected_window)->must_be_updated_p = 1;
13433 pending = update_frame (sf, 0, 0);
13434 }
13435
13436 /* We may have called echo_area_display at the top of this
13437 function. If the echo area is on another frame, that may
13438 have put text on a frame other than the selected one, so the
13439 above call to update_frame would not have caught it. Catch
13440 it here. */
13441 mini_window = FRAME_MINIBUF_WINDOW (sf);
13442 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13443
13444 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13445 {
13446 XWINDOW (mini_window)->must_be_updated_p = 1;
13447 pending |= update_frame (mini_frame, 0, 0);
13448 if (!pending && hscroll_windows (mini_window))
13449 goto retry;
13450 }
13451 }
13452
13453 /* If display was paused because of pending input, make sure we do a
13454 thorough update the next time. */
13455 if (pending)
13456 {
13457 /* Prevent the optimization at the beginning of
13458 redisplay_internal that tries a single-line update of the
13459 line containing the cursor in the selected window. */
13460 CHARPOS (this_line_start_pos) = 0;
13461
13462 /* Let the overlay arrow be updated the next time. */
13463 update_overlay_arrows (0);
13464
13465 /* If we pause after scrolling, some rows in the current
13466 matrices of some windows are not valid. */
13467 if (!WINDOW_FULL_WIDTH_P (w)
13468 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13469 update_mode_lines = 1;
13470 }
13471 else
13472 {
13473 if (!consider_all_windows_p)
13474 {
13475 /* This has already been done above if
13476 consider_all_windows_p is set. */
13477 mark_window_display_accurate_1 (w, 1);
13478
13479 /* Say overlay arrows are up to date. */
13480 update_overlay_arrows (1);
13481
13482 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13483 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13484 }
13485
13486 update_mode_lines = 0;
13487 windows_or_buffers_changed = 0;
13488 cursor_type_changed = 0;
13489 }
13490
13491 /* Start SIGIO interrupts coming again. Having them off during the
13492 code above makes it less likely one will discard output, but not
13493 impossible, since there might be stuff in the system buffer here.
13494 But it is much hairier to try to do anything about that. */
13495 if (interrupt_input)
13496 request_sigio ();
13497 RESUME_POLLING;
13498
13499 /* If a frame has become visible which was not before, redisplay
13500 again, so that we display it. Expose events for such a frame
13501 (which it gets when becoming visible) don't call the parts of
13502 redisplay constructing glyphs, so simply exposing a frame won't
13503 display anything in this case. So, we have to display these
13504 frames here explicitly. */
13505 if (!pending)
13506 {
13507 int new_count = 0;
13508
13509 FOR_EACH_FRAME (tail, frame)
13510 {
13511 int this_is_visible = 0;
13512
13513 if (XFRAME (frame)->visible)
13514 this_is_visible = 1;
13515
13516 if (this_is_visible)
13517 new_count++;
13518 }
13519
13520 if (new_count != number_of_visible_frames)
13521 windows_or_buffers_changed++;
13522 }
13523
13524 /* Change frame size now if a change is pending. */
13525 do_pending_window_change (1);
13526
13527 /* If we just did a pending size change, or have additional
13528 visible frames, or selected_window changed, redisplay again. */
13529 if ((windows_or_buffers_changed && !pending)
13530 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13531 goto retry;
13532
13533 /* Clear the face and image caches.
13534
13535 We used to do this only if consider_all_windows_p. But the cache
13536 needs to be cleared if a timer creates images in the current
13537 buffer (e.g. the test case in Bug#6230). */
13538
13539 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13540 {
13541 clear_face_cache (0);
13542 clear_face_cache_count = 0;
13543 }
13544
13545 #ifdef HAVE_WINDOW_SYSTEM
13546 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13547 {
13548 clear_image_caches (Qnil);
13549 clear_image_cache_count = 0;
13550 }
13551 #endif /* HAVE_WINDOW_SYSTEM */
13552
13553 end_of_redisplay:
13554 backtrace_list = backtrace.next;
13555 unbind_to (count, Qnil);
13556 RESUME_POLLING;
13557 }
13558
13559
13560 /* Redisplay, but leave alone any recent echo area message unless
13561 another message has been requested in its place.
13562
13563 This is useful in situations where you need to redisplay but no
13564 user action has occurred, making it inappropriate for the message
13565 area to be cleared. See tracking_off and
13566 wait_reading_process_output for examples of these situations.
13567
13568 FROM_WHERE is an integer saying from where this function was
13569 called. This is useful for debugging. */
13570
13571 void
13572 redisplay_preserve_echo_area (int from_where)
13573 {
13574 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13575
13576 if (!NILP (echo_area_buffer[1]))
13577 {
13578 /* We have a previously displayed message, but no current
13579 message. Redisplay the previous message. */
13580 display_last_displayed_message_p = 1;
13581 redisplay_internal ();
13582 display_last_displayed_message_p = 0;
13583 }
13584 else
13585 redisplay_internal ();
13586
13587 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13588 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13589 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13590 }
13591
13592
13593 /* Function registered with record_unwind_protect in redisplay_internal.
13594 Clear redisplaying_p. Also select the previously selected frame. */
13595
13596 static Lisp_Object
13597 unwind_redisplay (Lisp_Object old_frame)
13598 {
13599 redisplaying_p = 0;
13600 return Qnil;
13601 }
13602
13603
13604 /* Mark the display of leaf window W as accurate or inaccurate.
13605 If ACCURATE_P is non-zero mark display of W as accurate. If
13606 ACCURATE_P is zero, arrange for W to be redisplayed the next
13607 time redisplay_internal is called. */
13608
13609 static void
13610 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13611 {
13612 struct buffer *b = XBUFFER (w->buffer);
13613
13614 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13615 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13616 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13617
13618 if (accurate_p)
13619 {
13620 b->clip_changed = 0;
13621 b->prevent_redisplay_optimizations_p = 0;
13622
13623 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13624 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13625 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13626 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13627
13628 w->current_matrix->buffer = b;
13629 w->current_matrix->begv = BUF_BEGV (b);
13630 w->current_matrix->zv = BUF_ZV (b);
13631
13632 w->last_cursor = w->cursor;
13633 w->last_cursor_off_p = w->cursor_off_p;
13634
13635 if (w == XWINDOW (selected_window))
13636 w->last_point = BUF_PT (b);
13637 else
13638 w->last_point = marker_position (w->pointm);
13639
13640 w->window_end_valid = 1;
13641 w->update_mode_line = 0;
13642 }
13643 }
13644
13645
13646 /* Mark the display of windows in the window tree rooted at WINDOW as
13647 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13648 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13649 be redisplayed the next time redisplay_internal is called. */
13650
13651 void
13652 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13653 {
13654 struct window *w;
13655
13656 for (; !NILP (window); window = w->next)
13657 {
13658 w = XWINDOW (window);
13659 if (!NILP (w->vchild))
13660 mark_window_display_accurate (w->vchild, accurate_p);
13661 else if (!NILP (w->hchild))
13662 mark_window_display_accurate (w->hchild, accurate_p);
13663 else if (BUFFERP (w->buffer))
13664 mark_window_display_accurate_1 (w, accurate_p);
13665 }
13666
13667 if (accurate_p)
13668 update_overlay_arrows (1);
13669 else
13670 /* Force a thorough redisplay the next time by setting
13671 last_arrow_position and last_arrow_string to t, which is
13672 unequal to any useful value of Voverlay_arrow_... */
13673 update_overlay_arrows (-1);
13674 }
13675
13676
13677 /* Return value in display table DP (Lisp_Char_Table *) for character
13678 C. Since a display table doesn't have any parent, we don't have to
13679 follow parent. Do not call this function directly but use the
13680 macro DISP_CHAR_VECTOR. */
13681
13682 Lisp_Object
13683 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13684 {
13685 Lisp_Object val;
13686
13687 if (ASCII_CHAR_P (c))
13688 {
13689 val = dp->ascii;
13690 if (SUB_CHAR_TABLE_P (val))
13691 val = XSUB_CHAR_TABLE (val)->contents[c];
13692 }
13693 else
13694 {
13695 Lisp_Object table;
13696
13697 XSETCHAR_TABLE (table, dp);
13698 val = char_table_ref (table, c);
13699 }
13700 if (NILP (val))
13701 val = dp->defalt;
13702 return val;
13703 }
13704
13705
13706 \f
13707 /***********************************************************************
13708 Window Redisplay
13709 ***********************************************************************/
13710
13711 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13712
13713 static void
13714 redisplay_windows (Lisp_Object window)
13715 {
13716 while (!NILP (window))
13717 {
13718 struct window *w = XWINDOW (window);
13719
13720 if (!NILP (w->hchild))
13721 redisplay_windows (w->hchild);
13722 else if (!NILP (w->vchild))
13723 redisplay_windows (w->vchild);
13724 else if (!NILP (w->buffer))
13725 {
13726 displayed_buffer = XBUFFER (w->buffer);
13727 /* Use list_of_error, not Qerror, so that
13728 we catch only errors and don't run the debugger. */
13729 internal_condition_case_1 (redisplay_window_0, window,
13730 list_of_error,
13731 redisplay_window_error);
13732 }
13733
13734 window = w->next;
13735 }
13736 }
13737
13738 static Lisp_Object
13739 redisplay_window_error (Lisp_Object ignore)
13740 {
13741 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13742 return Qnil;
13743 }
13744
13745 static Lisp_Object
13746 redisplay_window_0 (Lisp_Object window)
13747 {
13748 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13749 redisplay_window (window, 0);
13750 return Qnil;
13751 }
13752
13753 static Lisp_Object
13754 redisplay_window_1 (Lisp_Object window)
13755 {
13756 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13757 redisplay_window (window, 1);
13758 return Qnil;
13759 }
13760 \f
13761
13762 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13763 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13764 which positions recorded in ROW differ from current buffer
13765 positions.
13766
13767 Return 0 if cursor is not on this row, 1 otherwise. */
13768
13769 static int
13770 set_cursor_from_row (struct window *w, struct glyph_row *row,
13771 struct glyph_matrix *matrix,
13772 ptrdiff_t delta, ptrdiff_t delta_bytes,
13773 int dy, int dvpos)
13774 {
13775 struct glyph *glyph = row->glyphs[TEXT_AREA];
13776 struct glyph *end = glyph + row->used[TEXT_AREA];
13777 struct glyph *cursor = NULL;
13778 /* The last known character position in row. */
13779 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13780 int x = row->x;
13781 ptrdiff_t pt_old = PT - delta;
13782 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13783 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13784 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13785 /* A glyph beyond the edge of TEXT_AREA which we should never
13786 touch. */
13787 struct glyph *glyphs_end = end;
13788 /* Non-zero means we've found a match for cursor position, but that
13789 glyph has the avoid_cursor_p flag set. */
13790 int match_with_avoid_cursor = 0;
13791 /* Non-zero means we've seen at least one glyph that came from a
13792 display string. */
13793 int string_seen = 0;
13794 /* Largest and smallest buffer positions seen so far during scan of
13795 glyph row. */
13796 ptrdiff_t bpos_max = pos_before;
13797 ptrdiff_t bpos_min = pos_after;
13798 /* Last buffer position covered by an overlay string with an integer
13799 `cursor' property. */
13800 ptrdiff_t bpos_covered = 0;
13801 /* Non-zero means the display string on which to display the cursor
13802 comes from a text property, not from an overlay. */
13803 int string_from_text_prop = 0;
13804
13805 /* Don't even try doing anything if called for a mode-line or
13806 header-line row, since the rest of the code isn't prepared to
13807 deal with such calamities. */
13808 eassert (!row->mode_line_p);
13809 if (row->mode_line_p)
13810 return 0;
13811
13812 /* Skip over glyphs not having an object at the start and the end of
13813 the row. These are special glyphs like truncation marks on
13814 terminal frames. */
13815 if (row->displays_text_p)
13816 {
13817 if (!row->reversed_p)
13818 {
13819 while (glyph < end
13820 && INTEGERP (glyph->object)
13821 && glyph->charpos < 0)
13822 {
13823 x += glyph->pixel_width;
13824 ++glyph;
13825 }
13826 while (end > glyph
13827 && INTEGERP ((end - 1)->object)
13828 /* CHARPOS is zero for blanks and stretch glyphs
13829 inserted by extend_face_to_end_of_line. */
13830 && (end - 1)->charpos <= 0)
13831 --end;
13832 glyph_before = glyph - 1;
13833 glyph_after = end;
13834 }
13835 else
13836 {
13837 struct glyph *g;
13838
13839 /* If the glyph row is reversed, we need to process it from back
13840 to front, so swap the edge pointers. */
13841 glyphs_end = end = glyph - 1;
13842 glyph += row->used[TEXT_AREA] - 1;
13843
13844 while (glyph > end + 1
13845 && INTEGERP (glyph->object)
13846 && glyph->charpos < 0)
13847 {
13848 --glyph;
13849 x -= glyph->pixel_width;
13850 }
13851 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13852 --glyph;
13853 /* By default, in reversed rows we put the cursor on the
13854 rightmost (first in the reading order) glyph. */
13855 for (g = end + 1; g < glyph; g++)
13856 x += g->pixel_width;
13857 while (end < glyph
13858 && INTEGERP ((end + 1)->object)
13859 && (end + 1)->charpos <= 0)
13860 ++end;
13861 glyph_before = glyph + 1;
13862 glyph_after = end;
13863 }
13864 }
13865 else if (row->reversed_p)
13866 {
13867 /* In R2L rows that don't display text, put the cursor on the
13868 rightmost glyph. Case in point: an empty last line that is
13869 part of an R2L paragraph. */
13870 cursor = end - 1;
13871 /* Avoid placing the cursor on the last glyph of the row, where
13872 on terminal frames we hold the vertical border between
13873 adjacent windows. */
13874 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13875 && !WINDOW_RIGHTMOST_P (w)
13876 && cursor == row->glyphs[LAST_AREA] - 1)
13877 cursor--;
13878 x = -1; /* will be computed below, at label compute_x */
13879 }
13880
13881 /* Step 1: Try to find the glyph whose character position
13882 corresponds to point. If that's not possible, find 2 glyphs
13883 whose character positions are the closest to point, one before
13884 point, the other after it. */
13885 if (!row->reversed_p)
13886 while (/* not marched to end of glyph row */
13887 glyph < end
13888 /* glyph was not inserted by redisplay for internal purposes */
13889 && !INTEGERP (glyph->object))
13890 {
13891 if (BUFFERP (glyph->object))
13892 {
13893 ptrdiff_t dpos = glyph->charpos - pt_old;
13894
13895 if (glyph->charpos > bpos_max)
13896 bpos_max = glyph->charpos;
13897 if (glyph->charpos < bpos_min)
13898 bpos_min = glyph->charpos;
13899 if (!glyph->avoid_cursor_p)
13900 {
13901 /* If we hit point, we've found the glyph on which to
13902 display the cursor. */
13903 if (dpos == 0)
13904 {
13905 match_with_avoid_cursor = 0;
13906 break;
13907 }
13908 /* See if we've found a better approximation to
13909 POS_BEFORE or to POS_AFTER. */
13910 if (0 > dpos && dpos > pos_before - pt_old)
13911 {
13912 pos_before = glyph->charpos;
13913 glyph_before = glyph;
13914 }
13915 else if (0 < dpos && dpos < pos_after - pt_old)
13916 {
13917 pos_after = glyph->charpos;
13918 glyph_after = glyph;
13919 }
13920 }
13921 else if (dpos == 0)
13922 match_with_avoid_cursor = 1;
13923 }
13924 else if (STRINGP (glyph->object))
13925 {
13926 Lisp_Object chprop;
13927 ptrdiff_t glyph_pos = glyph->charpos;
13928
13929 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13930 glyph->object);
13931 if (!NILP (chprop))
13932 {
13933 /* If the string came from a `display' text property,
13934 look up the buffer position of that property and
13935 use that position to update bpos_max, as if we
13936 actually saw such a position in one of the row's
13937 glyphs. This helps with supporting integer values
13938 of `cursor' property on the display string in
13939 situations where most or all of the row's buffer
13940 text is completely covered by display properties,
13941 so that no glyph with valid buffer positions is
13942 ever seen in the row. */
13943 ptrdiff_t prop_pos =
13944 string_buffer_position_lim (glyph->object, pos_before,
13945 pos_after, 0);
13946
13947 if (prop_pos >= pos_before)
13948 bpos_max = prop_pos - 1;
13949 }
13950 if (INTEGERP (chprop))
13951 {
13952 bpos_covered = bpos_max + XINT (chprop);
13953 /* If the `cursor' property covers buffer positions up
13954 to and including point, we should display cursor on
13955 this glyph. Note that, if a `cursor' property on one
13956 of the string's characters has an integer value, we
13957 will break out of the loop below _before_ we get to
13958 the position match above. IOW, integer values of
13959 the `cursor' property override the "exact match for
13960 point" strategy of positioning the cursor. */
13961 /* Implementation note: bpos_max == pt_old when, e.g.,
13962 we are in an empty line, where bpos_max is set to
13963 MATRIX_ROW_START_CHARPOS, see above. */
13964 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13965 {
13966 cursor = glyph;
13967 break;
13968 }
13969 }
13970
13971 string_seen = 1;
13972 }
13973 x += glyph->pixel_width;
13974 ++glyph;
13975 }
13976 else if (glyph > end) /* row is reversed */
13977 while (!INTEGERP (glyph->object))
13978 {
13979 if (BUFFERP (glyph->object))
13980 {
13981 ptrdiff_t dpos = glyph->charpos - pt_old;
13982
13983 if (glyph->charpos > bpos_max)
13984 bpos_max = glyph->charpos;
13985 if (glyph->charpos < bpos_min)
13986 bpos_min = glyph->charpos;
13987 if (!glyph->avoid_cursor_p)
13988 {
13989 if (dpos == 0)
13990 {
13991 match_with_avoid_cursor = 0;
13992 break;
13993 }
13994 if (0 > dpos && dpos > pos_before - pt_old)
13995 {
13996 pos_before = glyph->charpos;
13997 glyph_before = glyph;
13998 }
13999 else if (0 < dpos && dpos < pos_after - pt_old)
14000 {
14001 pos_after = glyph->charpos;
14002 glyph_after = glyph;
14003 }
14004 }
14005 else if (dpos == 0)
14006 match_with_avoid_cursor = 1;
14007 }
14008 else if (STRINGP (glyph->object))
14009 {
14010 Lisp_Object chprop;
14011 ptrdiff_t glyph_pos = glyph->charpos;
14012
14013 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14014 glyph->object);
14015 if (!NILP (chprop))
14016 {
14017 ptrdiff_t prop_pos =
14018 string_buffer_position_lim (glyph->object, pos_before,
14019 pos_after, 0);
14020
14021 if (prop_pos >= pos_before)
14022 bpos_max = prop_pos - 1;
14023 }
14024 if (INTEGERP (chprop))
14025 {
14026 bpos_covered = bpos_max + XINT (chprop);
14027 /* If the `cursor' property covers buffer positions up
14028 to and including point, we should display cursor on
14029 this glyph. */
14030 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14031 {
14032 cursor = glyph;
14033 break;
14034 }
14035 }
14036 string_seen = 1;
14037 }
14038 --glyph;
14039 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14040 {
14041 x--; /* can't use any pixel_width */
14042 break;
14043 }
14044 x -= glyph->pixel_width;
14045 }
14046
14047 /* Step 2: If we didn't find an exact match for point, we need to
14048 look for a proper place to put the cursor among glyphs between
14049 GLYPH_BEFORE and GLYPH_AFTER. */
14050 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14051 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14052 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14053 {
14054 /* An empty line has a single glyph whose OBJECT is zero and
14055 whose CHARPOS is the position of a newline on that line.
14056 Note that on a TTY, there are more glyphs after that, which
14057 were produced by extend_face_to_end_of_line, but their
14058 CHARPOS is zero or negative. */
14059 int empty_line_p =
14060 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14061 && INTEGERP (glyph->object) && glyph->charpos > 0
14062 /* On a TTY, continued and truncated rows also have a glyph at
14063 their end whose OBJECT is zero and whose CHARPOS is
14064 positive (the continuation and truncation glyphs), but such
14065 rows are obviously not "empty". */
14066 && !(row->continued_p || row->truncated_on_right_p);
14067
14068 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14069 {
14070 ptrdiff_t ellipsis_pos;
14071
14072 /* Scan back over the ellipsis glyphs. */
14073 if (!row->reversed_p)
14074 {
14075 ellipsis_pos = (glyph - 1)->charpos;
14076 while (glyph > row->glyphs[TEXT_AREA]
14077 && (glyph - 1)->charpos == ellipsis_pos)
14078 glyph--, x -= glyph->pixel_width;
14079 /* That loop always goes one position too far, including
14080 the glyph before the ellipsis. So scan forward over
14081 that one. */
14082 x += glyph->pixel_width;
14083 glyph++;
14084 }
14085 else /* row is reversed */
14086 {
14087 ellipsis_pos = (glyph + 1)->charpos;
14088 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14089 && (glyph + 1)->charpos == ellipsis_pos)
14090 glyph++, x += glyph->pixel_width;
14091 x -= glyph->pixel_width;
14092 glyph--;
14093 }
14094 }
14095 else if (match_with_avoid_cursor)
14096 {
14097 cursor = glyph_after;
14098 x = -1;
14099 }
14100 else if (string_seen)
14101 {
14102 int incr = row->reversed_p ? -1 : +1;
14103
14104 /* Need to find the glyph that came out of a string which is
14105 present at point. That glyph is somewhere between
14106 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14107 positioned between POS_BEFORE and POS_AFTER in the
14108 buffer. */
14109 struct glyph *start, *stop;
14110 ptrdiff_t pos = pos_before;
14111
14112 x = -1;
14113
14114 /* If the row ends in a newline from a display string,
14115 reordering could have moved the glyphs belonging to the
14116 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14117 in this case we extend the search to the last glyph in
14118 the row that was not inserted by redisplay. */
14119 if (row->ends_in_newline_from_string_p)
14120 {
14121 glyph_after = end;
14122 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14123 }
14124
14125 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14126 correspond to POS_BEFORE and POS_AFTER, respectively. We
14127 need START and STOP in the order that corresponds to the
14128 row's direction as given by its reversed_p flag. If the
14129 directionality of characters between POS_BEFORE and
14130 POS_AFTER is the opposite of the row's base direction,
14131 these characters will have been reordered for display,
14132 and we need to reverse START and STOP. */
14133 if (!row->reversed_p)
14134 {
14135 start = min (glyph_before, glyph_after);
14136 stop = max (glyph_before, glyph_after);
14137 }
14138 else
14139 {
14140 start = max (glyph_before, glyph_after);
14141 stop = min (glyph_before, glyph_after);
14142 }
14143 for (glyph = start + incr;
14144 row->reversed_p ? glyph > stop : glyph < stop; )
14145 {
14146
14147 /* Any glyphs that come from the buffer are here because
14148 of bidi reordering. Skip them, and only pay
14149 attention to glyphs that came from some string. */
14150 if (STRINGP (glyph->object))
14151 {
14152 Lisp_Object str;
14153 ptrdiff_t tem;
14154 /* If the display property covers the newline, we
14155 need to search for it one position farther. */
14156 ptrdiff_t lim = pos_after
14157 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14158
14159 string_from_text_prop = 0;
14160 str = glyph->object;
14161 tem = string_buffer_position_lim (str, pos, lim, 0);
14162 if (tem == 0 /* from overlay */
14163 || pos <= tem)
14164 {
14165 /* If the string from which this glyph came is
14166 found in the buffer at point, or at position
14167 that is closer to point than pos_after, then
14168 we've found the glyph we've been looking for.
14169 If it comes from an overlay (tem == 0), and
14170 it has the `cursor' property on one of its
14171 glyphs, record that glyph as a candidate for
14172 displaying the cursor. (As in the
14173 unidirectional version, we will display the
14174 cursor on the last candidate we find.) */
14175 if (tem == 0
14176 || tem == pt_old
14177 || (tem - pt_old > 0 && tem < pos_after))
14178 {
14179 /* The glyphs from this string could have
14180 been reordered. Find the one with the
14181 smallest string position. Or there could
14182 be a character in the string with the
14183 `cursor' property, which means display
14184 cursor on that character's glyph. */
14185 ptrdiff_t strpos = glyph->charpos;
14186
14187 if (tem)
14188 {
14189 cursor = glyph;
14190 string_from_text_prop = 1;
14191 }
14192 for ( ;
14193 (row->reversed_p ? glyph > stop : glyph < stop)
14194 && EQ (glyph->object, str);
14195 glyph += incr)
14196 {
14197 Lisp_Object cprop;
14198 ptrdiff_t gpos = glyph->charpos;
14199
14200 cprop = Fget_char_property (make_number (gpos),
14201 Qcursor,
14202 glyph->object);
14203 if (!NILP (cprop))
14204 {
14205 cursor = glyph;
14206 break;
14207 }
14208 if (tem && glyph->charpos < strpos)
14209 {
14210 strpos = glyph->charpos;
14211 cursor = glyph;
14212 }
14213 }
14214
14215 if (tem == pt_old
14216 || (tem - pt_old > 0 && tem < pos_after))
14217 goto compute_x;
14218 }
14219 if (tem)
14220 pos = tem + 1; /* don't find previous instances */
14221 }
14222 /* This string is not what we want; skip all of the
14223 glyphs that came from it. */
14224 while ((row->reversed_p ? glyph > stop : glyph < stop)
14225 && EQ (glyph->object, str))
14226 glyph += incr;
14227 }
14228 else
14229 glyph += incr;
14230 }
14231
14232 /* If we reached the end of the line, and END was from a string,
14233 the cursor is not on this line. */
14234 if (cursor == NULL
14235 && (row->reversed_p ? glyph <= end : glyph >= end)
14236 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14237 && STRINGP (end->object)
14238 && row->continued_p)
14239 return 0;
14240 }
14241 /* A truncated row may not include PT among its character positions.
14242 Setting the cursor inside the scroll margin will trigger
14243 recalculation of hscroll in hscroll_window_tree. But if a
14244 display string covers point, defer to the string-handling
14245 code below to figure this out. */
14246 else if (row->truncated_on_left_p && pt_old < bpos_min)
14247 {
14248 cursor = glyph_before;
14249 x = -1;
14250 }
14251 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14252 /* Zero-width characters produce no glyphs. */
14253 || (!empty_line_p
14254 && (row->reversed_p
14255 ? glyph_after > glyphs_end
14256 : glyph_after < glyphs_end)))
14257 {
14258 cursor = glyph_after;
14259 x = -1;
14260 }
14261 }
14262
14263 compute_x:
14264 if (cursor != NULL)
14265 glyph = cursor;
14266 else if (glyph == glyphs_end
14267 && pos_before == pos_after
14268 && STRINGP ((row->reversed_p
14269 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14270 : row->glyphs[TEXT_AREA])->object))
14271 {
14272 /* If all the glyphs of this row came from strings, put the
14273 cursor on the first glyph of the row. This avoids having the
14274 cursor outside of the text area in this very rare and hard
14275 use case. */
14276 glyph =
14277 row->reversed_p
14278 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14279 : row->glyphs[TEXT_AREA];
14280 }
14281 if (x < 0)
14282 {
14283 struct glyph *g;
14284
14285 /* Need to compute x that corresponds to GLYPH. */
14286 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14287 {
14288 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14289 emacs_abort ();
14290 x += g->pixel_width;
14291 }
14292 }
14293
14294 /* ROW could be part of a continued line, which, under bidi
14295 reordering, might have other rows whose start and end charpos
14296 occlude point. Only set w->cursor if we found a better
14297 approximation to the cursor position than we have from previously
14298 examined candidate rows belonging to the same continued line. */
14299 if (/* we already have a candidate row */
14300 w->cursor.vpos >= 0
14301 /* that candidate is not the row we are processing */
14302 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14303 /* Make sure cursor.vpos specifies a row whose start and end
14304 charpos occlude point, and it is valid candidate for being a
14305 cursor-row. This is because some callers of this function
14306 leave cursor.vpos at the row where the cursor was displayed
14307 during the last redisplay cycle. */
14308 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14309 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14310 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14311 {
14312 struct glyph *g1 =
14313 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14314
14315 /* Don't consider glyphs that are outside TEXT_AREA. */
14316 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14317 return 0;
14318 /* Keep the candidate whose buffer position is the closest to
14319 point or has the `cursor' property. */
14320 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14321 w->cursor.hpos >= 0
14322 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14323 && ((BUFFERP (g1->object)
14324 && (g1->charpos == pt_old /* an exact match always wins */
14325 || (BUFFERP (glyph->object)
14326 && eabs (g1->charpos - pt_old)
14327 < eabs (glyph->charpos - pt_old))))
14328 /* previous candidate is a glyph from a string that has
14329 a non-nil `cursor' property */
14330 || (STRINGP (g1->object)
14331 && (!NILP (Fget_char_property (make_number (g1->charpos),
14332 Qcursor, g1->object))
14333 /* previous candidate is from the same display
14334 string as this one, and the display string
14335 came from a text property */
14336 || (EQ (g1->object, glyph->object)
14337 && string_from_text_prop)
14338 /* this candidate is from newline and its
14339 position is not an exact match */
14340 || (INTEGERP (glyph->object)
14341 && glyph->charpos != pt_old)))))
14342 return 0;
14343 /* If this candidate gives an exact match, use that. */
14344 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14345 /* If this candidate is a glyph created for the
14346 terminating newline of a line, and point is on that
14347 newline, it wins because it's an exact match. */
14348 || (!row->continued_p
14349 && INTEGERP (glyph->object)
14350 && glyph->charpos == 0
14351 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14352 /* Otherwise, keep the candidate that comes from a row
14353 spanning less buffer positions. This may win when one or
14354 both candidate positions are on glyphs that came from
14355 display strings, for which we cannot compare buffer
14356 positions. */
14357 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14358 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14359 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14360 return 0;
14361 }
14362 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14363 w->cursor.x = x;
14364 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14365 w->cursor.y = row->y + dy;
14366
14367 if (w == XWINDOW (selected_window))
14368 {
14369 if (!row->continued_p
14370 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14371 && row->x == 0)
14372 {
14373 this_line_buffer = XBUFFER (w->buffer);
14374
14375 CHARPOS (this_line_start_pos)
14376 = MATRIX_ROW_START_CHARPOS (row) + delta;
14377 BYTEPOS (this_line_start_pos)
14378 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14379
14380 CHARPOS (this_line_end_pos)
14381 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14382 BYTEPOS (this_line_end_pos)
14383 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14384
14385 this_line_y = w->cursor.y;
14386 this_line_pixel_height = row->height;
14387 this_line_vpos = w->cursor.vpos;
14388 this_line_start_x = row->x;
14389 }
14390 else
14391 CHARPOS (this_line_start_pos) = 0;
14392 }
14393
14394 return 1;
14395 }
14396
14397
14398 /* Run window scroll functions, if any, for WINDOW with new window
14399 start STARTP. Sets the window start of WINDOW to that position.
14400
14401 We assume that the window's buffer is really current. */
14402
14403 static struct text_pos
14404 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14405 {
14406 struct window *w = XWINDOW (window);
14407 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14408
14409 if (current_buffer != XBUFFER (w->buffer))
14410 emacs_abort ();
14411
14412 if (!NILP (Vwindow_scroll_functions))
14413 {
14414 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14415 make_number (CHARPOS (startp)));
14416 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14417 /* In case the hook functions switch buffers. */
14418 set_buffer_internal (XBUFFER (w->buffer));
14419 }
14420
14421 return startp;
14422 }
14423
14424
14425 /* Make sure the line containing the cursor is fully visible.
14426 A value of 1 means there is nothing to be done.
14427 (Either the line is fully visible, or it cannot be made so,
14428 or we cannot tell.)
14429
14430 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14431 is higher than window.
14432
14433 A value of 0 means the caller should do scrolling
14434 as if point had gone off the screen. */
14435
14436 static int
14437 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14438 {
14439 struct glyph_matrix *matrix;
14440 struct glyph_row *row;
14441 int window_height;
14442
14443 if (!make_cursor_line_fully_visible_p)
14444 return 1;
14445
14446 /* It's not always possible to find the cursor, e.g, when a window
14447 is full of overlay strings. Don't do anything in that case. */
14448 if (w->cursor.vpos < 0)
14449 return 1;
14450
14451 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14452 row = MATRIX_ROW (matrix, w->cursor.vpos);
14453
14454 /* If the cursor row is not partially visible, there's nothing to do. */
14455 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14456 return 1;
14457
14458 /* If the row the cursor is in is taller than the window's height,
14459 it's not clear what to do, so do nothing. */
14460 window_height = window_box_height (w);
14461 if (row->height >= window_height)
14462 {
14463 if (!force_p || MINI_WINDOW_P (w)
14464 || w->vscroll || w->cursor.vpos == 0)
14465 return 1;
14466 }
14467 return 0;
14468 }
14469
14470
14471 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14472 non-zero means only WINDOW is redisplayed in redisplay_internal.
14473 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14474 in redisplay_window to bring a partially visible line into view in
14475 the case that only the cursor has moved.
14476
14477 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14478 last screen line's vertical height extends past the end of the screen.
14479
14480 Value is
14481
14482 1 if scrolling succeeded
14483
14484 0 if scrolling didn't find point.
14485
14486 -1 if new fonts have been loaded so that we must interrupt
14487 redisplay, adjust glyph matrices, and try again. */
14488
14489 enum
14490 {
14491 SCROLLING_SUCCESS,
14492 SCROLLING_FAILED,
14493 SCROLLING_NEED_LARGER_MATRICES
14494 };
14495
14496 /* If scroll-conservatively is more than this, never recenter.
14497
14498 If you change this, don't forget to update the doc string of
14499 `scroll-conservatively' and the Emacs manual. */
14500 #define SCROLL_LIMIT 100
14501
14502 static int
14503 try_scrolling (Lisp_Object window, int just_this_one_p,
14504 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14505 int temp_scroll_step, int last_line_misfit)
14506 {
14507 struct window *w = XWINDOW (window);
14508 struct frame *f = XFRAME (w->frame);
14509 struct text_pos pos, startp;
14510 struct it it;
14511 int this_scroll_margin, scroll_max, rc, height;
14512 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14513 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14514 Lisp_Object aggressive;
14515 /* We will never try scrolling more than this number of lines. */
14516 int scroll_limit = SCROLL_LIMIT;
14517
14518 #ifdef GLYPH_DEBUG
14519 debug_method_add (w, "try_scrolling");
14520 #endif
14521
14522 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14523
14524 /* Compute scroll margin height in pixels. We scroll when point is
14525 within this distance from the top or bottom of the window. */
14526 if (scroll_margin > 0)
14527 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14528 * FRAME_LINE_HEIGHT (f);
14529 else
14530 this_scroll_margin = 0;
14531
14532 /* Force arg_scroll_conservatively to have a reasonable value, to
14533 avoid scrolling too far away with slow move_it_* functions. Note
14534 that the user can supply scroll-conservatively equal to
14535 `most-positive-fixnum', which can be larger than INT_MAX. */
14536 if (arg_scroll_conservatively > scroll_limit)
14537 {
14538 arg_scroll_conservatively = scroll_limit + 1;
14539 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14540 }
14541 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14542 /* Compute how much we should try to scroll maximally to bring
14543 point into view. */
14544 scroll_max = (max (scroll_step,
14545 max (arg_scroll_conservatively, temp_scroll_step))
14546 * FRAME_LINE_HEIGHT (f));
14547 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14548 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14549 /* We're trying to scroll because of aggressive scrolling but no
14550 scroll_step is set. Choose an arbitrary one. */
14551 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14552 else
14553 scroll_max = 0;
14554
14555 too_near_end:
14556
14557 /* Decide whether to scroll down. */
14558 if (PT > CHARPOS (startp))
14559 {
14560 int scroll_margin_y;
14561
14562 /* Compute the pixel ypos of the scroll margin, then move IT to
14563 either that ypos or PT, whichever comes first. */
14564 start_display (&it, w, startp);
14565 scroll_margin_y = it.last_visible_y - this_scroll_margin
14566 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14567 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14568 (MOVE_TO_POS | MOVE_TO_Y));
14569
14570 if (PT > CHARPOS (it.current.pos))
14571 {
14572 int y0 = line_bottom_y (&it);
14573 /* Compute how many pixels below window bottom to stop searching
14574 for PT. This avoids costly search for PT that is far away if
14575 the user limited scrolling by a small number of lines, but
14576 always finds PT if scroll_conservatively is set to a large
14577 number, such as most-positive-fixnum. */
14578 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14579 int y_to_move = it.last_visible_y + slack;
14580
14581 /* Compute the distance from the scroll margin to PT or to
14582 the scroll limit, whichever comes first. This should
14583 include the height of the cursor line, to make that line
14584 fully visible. */
14585 move_it_to (&it, PT, -1, y_to_move,
14586 -1, MOVE_TO_POS | MOVE_TO_Y);
14587 dy = line_bottom_y (&it) - y0;
14588
14589 if (dy > scroll_max)
14590 return SCROLLING_FAILED;
14591
14592 if (dy > 0)
14593 scroll_down_p = 1;
14594 }
14595 }
14596
14597 if (scroll_down_p)
14598 {
14599 /* Point is in or below the bottom scroll margin, so move the
14600 window start down. If scrolling conservatively, move it just
14601 enough down to make point visible. If scroll_step is set,
14602 move it down by scroll_step. */
14603 if (arg_scroll_conservatively)
14604 amount_to_scroll
14605 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14606 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14607 else if (scroll_step || temp_scroll_step)
14608 amount_to_scroll = scroll_max;
14609 else
14610 {
14611 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14612 height = WINDOW_BOX_TEXT_HEIGHT (w);
14613 if (NUMBERP (aggressive))
14614 {
14615 double float_amount = XFLOATINT (aggressive) * height;
14616 int aggressive_scroll = float_amount;
14617 if (aggressive_scroll == 0 && float_amount > 0)
14618 aggressive_scroll = 1;
14619 /* Don't let point enter the scroll margin near top of
14620 the window. This could happen if the value of
14621 scroll_up_aggressively is too large and there are
14622 non-zero margins, because scroll_up_aggressively
14623 means put point that fraction of window height
14624 _from_the_bottom_margin_. */
14625 if (aggressive_scroll + 2*this_scroll_margin > height)
14626 aggressive_scroll = height - 2*this_scroll_margin;
14627 amount_to_scroll = dy + aggressive_scroll;
14628 }
14629 }
14630
14631 if (amount_to_scroll <= 0)
14632 return SCROLLING_FAILED;
14633
14634 start_display (&it, w, startp);
14635 if (arg_scroll_conservatively <= scroll_limit)
14636 move_it_vertically (&it, amount_to_scroll);
14637 else
14638 {
14639 /* Extra precision for users who set scroll-conservatively
14640 to a large number: make sure the amount we scroll
14641 the window start is never less than amount_to_scroll,
14642 which was computed as distance from window bottom to
14643 point. This matters when lines at window top and lines
14644 below window bottom have different height. */
14645 struct it it1;
14646 void *it1data = NULL;
14647 /* We use a temporary it1 because line_bottom_y can modify
14648 its argument, if it moves one line down; see there. */
14649 int start_y;
14650
14651 SAVE_IT (it1, it, it1data);
14652 start_y = line_bottom_y (&it1);
14653 do {
14654 RESTORE_IT (&it, &it, it1data);
14655 move_it_by_lines (&it, 1);
14656 SAVE_IT (it1, it, it1data);
14657 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14658 }
14659
14660 /* If STARTP is unchanged, move it down another screen line. */
14661 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14662 move_it_by_lines (&it, 1);
14663 startp = it.current.pos;
14664 }
14665 else
14666 {
14667 struct text_pos scroll_margin_pos = startp;
14668 int y_offset = 0;
14669
14670 /* See if point is inside the scroll margin at the top of the
14671 window. */
14672 if (this_scroll_margin)
14673 {
14674 int y_start;
14675
14676 start_display (&it, w, startp);
14677 y_start = it.current_y;
14678 move_it_vertically (&it, this_scroll_margin);
14679 scroll_margin_pos = it.current.pos;
14680 /* If we didn't move enough before hitting ZV, request
14681 additional amount of scroll, to move point out of the
14682 scroll margin. */
14683 if (IT_CHARPOS (it) == ZV
14684 && it.current_y - y_start < this_scroll_margin)
14685 y_offset = this_scroll_margin - (it.current_y - y_start);
14686 }
14687
14688 if (PT < CHARPOS (scroll_margin_pos))
14689 {
14690 /* Point is in the scroll margin at the top of the window or
14691 above what is displayed in the window. */
14692 int y0, y_to_move;
14693
14694 /* Compute the vertical distance from PT to the scroll
14695 margin position. Move as far as scroll_max allows, or
14696 one screenful, or 10 screen lines, whichever is largest.
14697 Give up if distance is greater than scroll_max or if we
14698 didn't reach the scroll margin position. */
14699 SET_TEXT_POS (pos, PT, PT_BYTE);
14700 start_display (&it, w, pos);
14701 y0 = it.current_y;
14702 y_to_move = max (it.last_visible_y,
14703 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14704 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14705 y_to_move, -1,
14706 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14707 dy = it.current_y - y0;
14708 if (dy > scroll_max
14709 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14710 return SCROLLING_FAILED;
14711
14712 /* Additional scroll for when ZV was too close to point. */
14713 dy += y_offset;
14714
14715 /* Compute new window start. */
14716 start_display (&it, w, startp);
14717
14718 if (arg_scroll_conservatively)
14719 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14720 max (scroll_step, temp_scroll_step));
14721 else if (scroll_step || temp_scroll_step)
14722 amount_to_scroll = scroll_max;
14723 else
14724 {
14725 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14726 height = WINDOW_BOX_TEXT_HEIGHT (w);
14727 if (NUMBERP (aggressive))
14728 {
14729 double float_amount = XFLOATINT (aggressive) * height;
14730 int aggressive_scroll = float_amount;
14731 if (aggressive_scroll == 0 && float_amount > 0)
14732 aggressive_scroll = 1;
14733 /* Don't let point enter the scroll margin near
14734 bottom of the window, if the value of
14735 scroll_down_aggressively happens to be too
14736 large. */
14737 if (aggressive_scroll + 2*this_scroll_margin > height)
14738 aggressive_scroll = height - 2*this_scroll_margin;
14739 amount_to_scroll = dy + aggressive_scroll;
14740 }
14741 }
14742
14743 if (amount_to_scroll <= 0)
14744 return SCROLLING_FAILED;
14745
14746 move_it_vertically_backward (&it, amount_to_scroll);
14747 startp = it.current.pos;
14748 }
14749 }
14750
14751 /* Run window scroll functions. */
14752 startp = run_window_scroll_functions (window, startp);
14753
14754 /* Display the window. Give up if new fonts are loaded, or if point
14755 doesn't appear. */
14756 if (!try_window (window, startp, 0))
14757 rc = SCROLLING_NEED_LARGER_MATRICES;
14758 else if (w->cursor.vpos < 0)
14759 {
14760 clear_glyph_matrix (w->desired_matrix);
14761 rc = SCROLLING_FAILED;
14762 }
14763 else
14764 {
14765 /* Maybe forget recorded base line for line number display. */
14766 if (!just_this_one_p
14767 || current_buffer->clip_changed
14768 || BEG_UNCHANGED < CHARPOS (startp))
14769 w->base_line_number = 0;
14770
14771 /* If cursor ends up on a partially visible line,
14772 treat that as being off the bottom of the screen. */
14773 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14774 /* It's possible that the cursor is on the first line of the
14775 buffer, which is partially obscured due to a vscroll
14776 (Bug#7537). In that case, avoid looping forever . */
14777 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14778 {
14779 clear_glyph_matrix (w->desired_matrix);
14780 ++extra_scroll_margin_lines;
14781 goto too_near_end;
14782 }
14783 rc = SCROLLING_SUCCESS;
14784 }
14785
14786 return rc;
14787 }
14788
14789
14790 /* Compute a suitable window start for window W if display of W starts
14791 on a continuation line. Value is non-zero if a new window start
14792 was computed.
14793
14794 The new window start will be computed, based on W's width, starting
14795 from the start of the continued line. It is the start of the
14796 screen line with the minimum distance from the old start W->start. */
14797
14798 static int
14799 compute_window_start_on_continuation_line (struct window *w)
14800 {
14801 struct text_pos pos, start_pos;
14802 int window_start_changed_p = 0;
14803
14804 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14805
14806 /* If window start is on a continuation line... Window start may be
14807 < BEGV in case there's invisible text at the start of the
14808 buffer (M-x rmail, for example). */
14809 if (CHARPOS (start_pos) > BEGV
14810 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14811 {
14812 struct it it;
14813 struct glyph_row *row;
14814
14815 /* Handle the case that the window start is out of range. */
14816 if (CHARPOS (start_pos) < BEGV)
14817 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14818 else if (CHARPOS (start_pos) > ZV)
14819 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14820
14821 /* Find the start of the continued line. This should be fast
14822 because find_newline is fast (newline cache). */
14823 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14824 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14825 row, DEFAULT_FACE_ID);
14826 reseat_at_previous_visible_line_start (&it);
14827
14828 /* If the line start is "too far" away from the window start,
14829 say it takes too much time to compute a new window start. */
14830 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14831 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14832 {
14833 int min_distance, distance;
14834
14835 /* Move forward by display lines to find the new window
14836 start. If window width was enlarged, the new start can
14837 be expected to be > the old start. If window width was
14838 decreased, the new window start will be < the old start.
14839 So, we're looking for the display line start with the
14840 minimum distance from the old window start. */
14841 pos = it.current.pos;
14842 min_distance = INFINITY;
14843 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14844 distance < min_distance)
14845 {
14846 min_distance = distance;
14847 pos = it.current.pos;
14848 move_it_by_lines (&it, 1);
14849 }
14850
14851 /* Set the window start there. */
14852 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14853 window_start_changed_p = 1;
14854 }
14855 }
14856
14857 return window_start_changed_p;
14858 }
14859
14860
14861 /* Try cursor movement in case text has not changed in window WINDOW,
14862 with window start STARTP. Value is
14863
14864 CURSOR_MOVEMENT_SUCCESS if successful
14865
14866 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14867
14868 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14869 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14870 we want to scroll as if scroll-step were set to 1. See the code.
14871
14872 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14873 which case we have to abort this redisplay, and adjust matrices
14874 first. */
14875
14876 enum
14877 {
14878 CURSOR_MOVEMENT_SUCCESS,
14879 CURSOR_MOVEMENT_CANNOT_BE_USED,
14880 CURSOR_MOVEMENT_MUST_SCROLL,
14881 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14882 };
14883
14884 static int
14885 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14886 {
14887 struct window *w = XWINDOW (window);
14888 struct frame *f = XFRAME (w->frame);
14889 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14890
14891 #ifdef GLYPH_DEBUG
14892 if (inhibit_try_cursor_movement)
14893 return rc;
14894 #endif
14895
14896 /* Previously, there was a check for Lisp integer in the
14897 if-statement below. Now, this field is converted to
14898 ptrdiff_t, thus zero means invalid position in a buffer. */
14899 eassert (w->last_point > 0);
14900
14901 /* Handle case where text has not changed, only point, and it has
14902 not moved off the frame. */
14903 if (/* Point may be in this window. */
14904 PT >= CHARPOS (startp)
14905 /* Selective display hasn't changed. */
14906 && !current_buffer->clip_changed
14907 /* Function force-mode-line-update is used to force a thorough
14908 redisplay. It sets either windows_or_buffers_changed or
14909 update_mode_lines. So don't take a shortcut here for these
14910 cases. */
14911 && !update_mode_lines
14912 && !windows_or_buffers_changed
14913 && !cursor_type_changed
14914 /* Can't use this case if highlighting a region. When a
14915 region exists, cursor movement has to do more than just
14916 set the cursor. */
14917 && markpos_of_region () < 0
14918 && !w->region_showing
14919 && NILP (Vshow_trailing_whitespace)
14920 /* This code is not used for mini-buffer for the sake of the case
14921 of redisplaying to replace an echo area message; since in
14922 that case the mini-buffer contents per se are usually
14923 unchanged. This code is of no real use in the mini-buffer
14924 since the handling of this_line_start_pos, etc., in redisplay
14925 handles the same cases. */
14926 && !EQ (window, minibuf_window)
14927 /* When splitting windows or for new windows, it happens that
14928 redisplay is called with a nil window_end_vpos or one being
14929 larger than the window. This should really be fixed in
14930 window.c. I don't have this on my list, now, so we do
14931 approximately the same as the old redisplay code. --gerd. */
14932 && INTEGERP (w->window_end_vpos)
14933 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14934 && (FRAME_WINDOW_P (f)
14935 || !overlay_arrow_in_current_buffer_p ()))
14936 {
14937 int this_scroll_margin, top_scroll_margin;
14938 struct glyph_row *row = NULL;
14939
14940 #ifdef GLYPH_DEBUG
14941 debug_method_add (w, "cursor movement");
14942 #endif
14943
14944 /* Scroll if point within this distance from the top or bottom
14945 of the window. This is a pixel value. */
14946 if (scroll_margin > 0)
14947 {
14948 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14949 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14950 }
14951 else
14952 this_scroll_margin = 0;
14953
14954 top_scroll_margin = this_scroll_margin;
14955 if (WINDOW_WANTS_HEADER_LINE_P (w))
14956 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14957
14958 /* Start with the row the cursor was displayed during the last
14959 not paused redisplay. Give up if that row is not valid. */
14960 if (w->last_cursor.vpos < 0
14961 || w->last_cursor.vpos >= w->current_matrix->nrows)
14962 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14963 else
14964 {
14965 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14966 if (row->mode_line_p)
14967 ++row;
14968 if (!row->enabled_p)
14969 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14970 }
14971
14972 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14973 {
14974 int scroll_p = 0, must_scroll = 0;
14975 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14976
14977 if (PT > w->last_point)
14978 {
14979 /* Point has moved forward. */
14980 while (MATRIX_ROW_END_CHARPOS (row) < PT
14981 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14982 {
14983 eassert (row->enabled_p);
14984 ++row;
14985 }
14986
14987 /* If the end position of a row equals the start
14988 position of the next row, and PT is at that position,
14989 we would rather display cursor in the next line. */
14990 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14991 && MATRIX_ROW_END_CHARPOS (row) == PT
14992 && row < w->current_matrix->rows
14993 + w->current_matrix->nrows - 1
14994 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14995 && !cursor_row_p (row))
14996 ++row;
14997
14998 /* If within the scroll margin, scroll. Note that
14999 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15000 the next line would be drawn, and that
15001 this_scroll_margin can be zero. */
15002 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15003 || PT > MATRIX_ROW_END_CHARPOS (row)
15004 /* Line is completely visible last line in window
15005 and PT is to be set in the next line. */
15006 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15007 && PT == MATRIX_ROW_END_CHARPOS (row)
15008 && !row->ends_at_zv_p
15009 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15010 scroll_p = 1;
15011 }
15012 else if (PT < w->last_point)
15013 {
15014 /* Cursor has to be moved backward. Note that PT >=
15015 CHARPOS (startp) because of the outer if-statement. */
15016 while (!row->mode_line_p
15017 && (MATRIX_ROW_START_CHARPOS (row) > PT
15018 || (MATRIX_ROW_START_CHARPOS (row) == PT
15019 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15020 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15021 row > w->current_matrix->rows
15022 && (row-1)->ends_in_newline_from_string_p))))
15023 && (row->y > top_scroll_margin
15024 || CHARPOS (startp) == BEGV))
15025 {
15026 eassert (row->enabled_p);
15027 --row;
15028 }
15029
15030 /* Consider the following case: Window starts at BEGV,
15031 there is invisible, intangible text at BEGV, so that
15032 display starts at some point START > BEGV. It can
15033 happen that we are called with PT somewhere between
15034 BEGV and START. Try to handle that case. */
15035 if (row < w->current_matrix->rows
15036 || row->mode_line_p)
15037 {
15038 row = w->current_matrix->rows;
15039 if (row->mode_line_p)
15040 ++row;
15041 }
15042
15043 /* Due to newlines in overlay strings, we may have to
15044 skip forward over overlay strings. */
15045 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15046 && MATRIX_ROW_END_CHARPOS (row) == PT
15047 && !cursor_row_p (row))
15048 ++row;
15049
15050 /* If within the scroll margin, scroll. */
15051 if (row->y < top_scroll_margin
15052 && CHARPOS (startp) != BEGV)
15053 scroll_p = 1;
15054 }
15055 else
15056 {
15057 /* Cursor did not move. So don't scroll even if cursor line
15058 is partially visible, as it was so before. */
15059 rc = CURSOR_MOVEMENT_SUCCESS;
15060 }
15061
15062 if (PT < MATRIX_ROW_START_CHARPOS (row)
15063 || PT > MATRIX_ROW_END_CHARPOS (row))
15064 {
15065 /* if PT is not in the glyph row, give up. */
15066 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15067 must_scroll = 1;
15068 }
15069 else if (rc != CURSOR_MOVEMENT_SUCCESS
15070 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15071 {
15072 struct glyph_row *row1;
15073
15074 /* If rows are bidi-reordered and point moved, back up
15075 until we find a row that does not belong to a
15076 continuation line. This is because we must consider
15077 all rows of a continued line as candidates for the
15078 new cursor positioning, since row start and end
15079 positions change non-linearly with vertical position
15080 in such rows. */
15081 /* FIXME: Revisit this when glyph ``spilling'' in
15082 continuation lines' rows is implemented for
15083 bidi-reordered rows. */
15084 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15085 MATRIX_ROW_CONTINUATION_LINE_P (row);
15086 --row)
15087 {
15088 /* If we hit the beginning of the displayed portion
15089 without finding the first row of a continued
15090 line, give up. */
15091 if (row <= row1)
15092 {
15093 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15094 break;
15095 }
15096 eassert (row->enabled_p);
15097 }
15098 }
15099 if (must_scroll)
15100 ;
15101 else if (rc != CURSOR_MOVEMENT_SUCCESS
15102 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15103 /* Make sure this isn't a header line by any chance, since
15104 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15105 && !row->mode_line_p
15106 && make_cursor_line_fully_visible_p)
15107 {
15108 if (PT == MATRIX_ROW_END_CHARPOS (row)
15109 && !row->ends_at_zv_p
15110 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15111 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15112 else if (row->height > window_box_height (w))
15113 {
15114 /* If we end up in a partially visible line, let's
15115 make it fully visible, except when it's taller
15116 than the window, in which case we can't do much
15117 about it. */
15118 *scroll_step = 1;
15119 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15120 }
15121 else
15122 {
15123 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15124 if (!cursor_row_fully_visible_p (w, 0, 1))
15125 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15126 else
15127 rc = CURSOR_MOVEMENT_SUCCESS;
15128 }
15129 }
15130 else if (scroll_p)
15131 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15132 else if (rc != CURSOR_MOVEMENT_SUCCESS
15133 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15134 {
15135 /* With bidi-reordered rows, there could be more than
15136 one candidate row whose start and end positions
15137 occlude point. We need to let set_cursor_from_row
15138 find the best candidate. */
15139 /* FIXME: Revisit this when glyph ``spilling'' in
15140 continuation lines' rows is implemented for
15141 bidi-reordered rows. */
15142 int rv = 0;
15143
15144 do
15145 {
15146 int at_zv_p = 0, exact_match_p = 0;
15147
15148 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15149 && PT <= MATRIX_ROW_END_CHARPOS (row)
15150 && cursor_row_p (row))
15151 rv |= set_cursor_from_row (w, row, w->current_matrix,
15152 0, 0, 0, 0);
15153 /* As soon as we've found the exact match for point,
15154 or the first suitable row whose ends_at_zv_p flag
15155 is set, we are done. */
15156 at_zv_p =
15157 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15158 if (rv && !at_zv_p
15159 && w->cursor.hpos >= 0
15160 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15161 w->cursor.vpos))
15162 {
15163 struct glyph_row *candidate =
15164 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15165 struct glyph *g =
15166 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15167 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15168
15169 exact_match_p =
15170 (BUFFERP (g->object) && g->charpos == PT)
15171 || (INTEGERP (g->object)
15172 && (g->charpos == PT
15173 || (g->charpos == 0 && endpos - 1 == PT)));
15174 }
15175 if (rv && (at_zv_p || exact_match_p))
15176 {
15177 rc = CURSOR_MOVEMENT_SUCCESS;
15178 break;
15179 }
15180 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15181 break;
15182 ++row;
15183 }
15184 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15185 || row->continued_p)
15186 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15187 || (MATRIX_ROW_START_CHARPOS (row) == PT
15188 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15189 /* If we didn't find any candidate rows, or exited the
15190 loop before all the candidates were examined, signal
15191 to the caller that this method failed. */
15192 if (rc != CURSOR_MOVEMENT_SUCCESS
15193 && !(rv
15194 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15195 && !row->continued_p))
15196 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15197 else if (rv)
15198 rc = CURSOR_MOVEMENT_SUCCESS;
15199 }
15200 else
15201 {
15202 do
15203 {
15204 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15205 {
15206 rc = CURSOR_MOVEMENT_SUCCESS;
15207 break;
15208 }
15209 ++row;
15210 }
15211 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15212 && MATRIX_ROW_START_CHARPOS (row) == PT
15213 && cursor_row_p (row));
15214 }
15215 }
15216 }
15217
15218 return rc;
15219 }
15220
15221 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15222 static
15223 #endif
15224 void
15225 set_vertical_scroll_bar (struct window *w)
15226 {
15227 ptrdiff_t start, end, whole;
15228
15229 /* Calculate the start and end positions for the current window.
15230 At some point, it would be nice to choose between scrollbars
15231 which reflect the whole buffer size, with special markers
15232 indicating narrowing, and scrollbars which reflect only the
15233 visible region.
15234
15235 Note that mini-buffers sometimes aren't displaying any text. */
15236 if (!MINI_WINDOW_P (w)
15237 || (w == XWINDOW (minibuf_window)
15238 && NILP (echo_area_buffer[0])))
15239 {
15240 struct buffer *buf = XBUFFER (w->buffer);
15241 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15242 start = marker_position (w->start) - BUF_BEGV (buf);
15243 /* I don't think this is guaranteed to be right. For the
15244 moment, we'll pretend it is. */
15245 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15246
15247 if (end < start)
15248 end = start;
15249 if (whole < (end - start))
15250 whole = end - start;
15251 }
15252 else
15253 start = end = whole = 0;
15254
15255 /* Indicate what this scroll bar ought to be displaying now. */
15256 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15257 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15258 (w, end - start, whole, start);
15259 }
15260
15261
15262 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15263 selected_window is redisplayed.
15264
15265 We can return without actually redisplaying the window if
15266 fonts_changed_p. In that case, redisplay_internal will
15267 retry. */
15268
15269 static void
15270 redisplay_window (Lisp_Object window, int just_this_one_p)
15271 {
15272 struct window *w = XWINDOW (window);
15273 struct frame *f = XFRAME (w->frame);
15274 struct buffer *buffer = XBUFFER (w->buffer);
15275 struct buffer *old = current_buffer;
15276 struct text_pos lpoint, opoint, startp;
15277 int update_mode_line;
15278 int tem;
15279 struct it it;
15280 /* Record it now because it's overwritten. */
15281 int current_matrix_up_to_date_p = 0;
15282 int used_current_matrix_p = 0;
15283 /* This is less strict than current_matrix_up_to_date_p.
15284 It indicates that the buffer contents and narrowing are unchanged. */
15285 int buffer_unchanged_p = 0;
15286 int temp_scroll_step = 0;
15287 ptrdiff_t count = SPECPDL_INDEX ();
15288 int rc;
15289 int centering_position = -1;
15290 int last_line_misfit = 0;
15291 ptrdiff_t beg_unchanged, end_unchanged;
15292
15293 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15294 opoint = lpoint;
15295
15296 /* W must be a leaf window here. */
15297 eassert (!NILP (w->buffer));
15298 #ifdef GLYPH_DEBUG
15299 *w->desired_matrix->method = 0;
15300 #endif
15301
15302 restart:
15303 reconsider_clip_changes (w, buffer);
15304
15305 /* Has the mode line to be updated? */
15306 update_mode_line = (w->update_mode_line
15307 || update_mode_lines
15308 || buffer->clip_changed
15309 || buffer->prevent_redisplay_optimizations_p);
15310
15311 if (MINI_WINDOW_P (w))
15312 {
15313 if (w == XWINDOW (echo_area_window)
15314 && !NILP (echo_area_buffer[0]))
15315 {
15316 if (update_mode_line)
15317 /* We may have to update a tty frame's menu bar or a
15318 tool-bar. Example `M-x C-h C-h C-g'. */
15319 goto finish_menu_bars;
15320 else
15321 /* We've already displayed the echo area glyphs in this window. */
15322 goto finish_scroll_bars;
15323 }
15324 else if ((w != XWINDOW (minibuf_window)
15325 || minibuf_level == 0)
15326 /* When buffer is nonempty, redisplay window normally. */
15327 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15328 /* Quail displays non-mini buffers in minibuffer window.
15329 In that case, redisplay the window normally. */
15330 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15331 {
15332 /* W is a mini-buffer window, but it's not active, so clear
15333 it. */
15334 int yb = window_text_bottom_y (w);
15335 struct glyph_row *row;
15336 int y;
15337
15338 for (y = 0, row = w->desired_matrix->rows;
15339 y < yb;
15340 y += row->height, ++row)
15341 blank_row (w, row, y);
15342 goto finish_scroll_bars;
15343 }
15344
15345 clear_glyph_matrix (w->desired_matrix);
15346 }
15347
15348 /* Otherwise set up data on this window; select its buffer and point
15349 value. */
15350 /* Really select the buffer, for the sake of buffer-local
15351 variables. */
15352 set_buffer_internal_1 (XBUFFER (w->buffer));
15353
15354 current_matrix_up_to_date_p
15355 = (w->window_end_valid
15356 && !current_buffer->clip_changed
15357 && !current_buffer->prevent_redisplay_optimizations_p
15358 && !window_outdated (w));
15359
15360 /* Run the window-bottom-change-functions
15361 if it is possible that the text on the screen has changed
15362 (either due to modification of the text, or any other reason). */
15363 if (!current_matrix_up_to_date_p
15364 && !NILP (Vwindow_text_change_functions))
15365 {
15366 safe_run_hooks (Qwindow_text_change_functions);
15367 goto restart;
15368 }
15369
15370 beg_unchanged = BEG_UNCHANGED;
15371 end_unchanged = END_UNCHANGED;
15372
15373 SET_TEXT_POS (opoint, PT, PT_BYTE);
15374
15375 specbind (Qinhibit_point_motion_hooks, Qt);
15376
15377 buffer_unchanged_p
15378 = (w->window_end_valid
15379 && !current_buffer->clip_changed
15380 && !window_outdated (w));
15381
15382 /* When windows_or_buffers_changed is non-zero, we can't rely on
15383 the window end being valid, so set it to nil there. */
15384 if (windows_or_buffers_changed)
15385 {
15386 /* If window starts on a continuation line, maybe adjust the
15387 window start in case the window's width changed. */
15388 if (XMARKER (w->start)->buffer == current_buffer)
15389 compute_window_start_on_continuation_line (w);
15390
15391 w->window_end_valid = 0;
15392 }
15393
15394 /* Some sanity checks. */
15395 CHECK_WINDOW_END (w);
15396 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15397 emacs_abort ();
15398 if (BYTEPOS (opoint) < CHARPOS (opoint))
15399 emacs_abort ();
15400
15401 if (mode_line_update_needed (w))
15402 update_mode_line = 1;
15403
15404 /* Point refers normally to the selected window. For any other
15405 window, set up appropriate value. */
15406 if (!EQ (window, selected_window))
15407 {
15408 ptrdiff_t new_pt = marker_position (w->pointm);
15409 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15410 if (new_pt < BEGV)
15411 {
15412 new_pt = BEGV;
15413 new_pt_byte = BEGV_BYTE;
15414 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15415 }
15416 else if (new_pt > (ZV - 1))
15417 {
15418 new_pt = ZV;
15419 new_pt_byte = ZV_BYTE;
15420 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15421 }
15422
15423 /* We don't use SET_PT so that the point-motion hooks don't run. */
15424 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15425 }
15426
15427 /* If any of the character widths specified in the display table
15428 have changed, invalidate the width run cache. It's true that
15429 this may be a bit late to catch such changes, but the rest of
15430 redisplay goes (non-fatally) haywire when the display table is
15431 changed, so why should we worry about doing any better? */
15432 if (current_buffer->width_run_cache)
15433 {
15434 struct Lisp_Char_Table *disptab = buffer_display_table ();
15435
15436 if (! disptab_matches_widthtab
15437 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15438 {
15439 invalidate_region_cache (current_buffer,
15440 current_buffer->width_run_cache,
15441 BEG, Z);
15442 recompute_width_table (current_buffer, disptab);
15443 }
15444 }
15445
15446 /* If window-start is screwed up, choose a new one. */
15447 if (XMARKER (w->start)->buffer != current_buffer)
15448 goto recenter;
15449
15450 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15451
15452 /* If someone specified a new starting point but did not insist,
15453 check whether it can be used. */
15454 if (w->optional_new_start
15455 && CHARPOS (startp) >= BEGV
15456 && CHARPOS (startp) <= ZV)
15457 {
15458 w->optional_new_start = 0;
15459 start_display (&it, w, startp);
15460 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15461 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15462 if (IT_CHARPOS (it) == PT)
15463 w->force_start = 1;
15464 /* IT may overshoot PT if text at PT is invisible. */
15465 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15466 w->force_start = 1;
15467 }
15468
15469 force_start:
15470
15471 /* Handle case where place to start displaying has been specified,
15472 unless the specified location is outside the accessible range. */
15473 if (w->force_start || w->frozen_window_start_p)
15474 {
15475 /* We set this later on if we have to adjust point. */
15476 int new_vpos = -1;
15477
15478 w->force_start = 0;
15479 w->vscroll = 0;
15480 w->window_end_valid = 0;
15481
15482 /* Forget any recorded base line for line number display. */
15483 if (!buffer_unchanged_p)
15484 w->base_line_number = 0;
15485
15486 /* Redisplay the mode line. Select the buffer properly for that.
15487 Also, run the hook window-scroll-functions
15488 because we have scrolled. */
15489 /* Note, we do this after clearing force_start because
15490 if there's an error, it is better to forget about force_start
15491 than to get into an infinite loop calling the hook functions
15492 and having them get more errors. */
15493 if (!update_mode_line
15494 || ! NILP (Vwindow_scroll_functions))
15495 {
15496 update_mode_line = 1;
15497 w->update_mode_line = 1;
15498 startp = run_window_scroll_functions (window, startp);
15499 }
15500
15501 w->last_modified = 0;
15502 w->last_overlay_modified = 0;
15503 if (CHARPOS (startp) < BEGV)
15504 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15505 else if (CHARPOS (startp) > ZV)
15506 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15507
15508 /* Redisplay, then check if cursor has been set during the
15509 redisplay. Give up if new fonts were loaded. */
15510 /* We used to issue a CHECK_MARGINS argument to try_window here,
15511 but this causes scrolling to fail when point begins inside
15512 the scroll margin (bug#148) -- cyd */
15513 if (!try_window (window, startp, 0))
15514 {
15515 w->force_start = 1;
15516 clear_glyph_matrix (w->desired_matrix);
15517 goto need_larger_matrices;
15518 }
15519
15520 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15521 {
15522 /* If point does not appear, try to move point so it does
15523 appear. The desired matrix has been built above, so we
15524 can use it here. */
15525 new_vpos = window_box_height (w) / 2;
15526 }
15527
15528 if (!cursor_row_fully_visible_p (w, 0, 0))
15529 {
15530 /* Point does appear, but on a line partly visible at end of window.
15531 Move it back to a fully-visible line. */
15532 new_vpos = window_box_height (w);
15533 }
15534 else if (w->cursor.vpos >=0)
15535 {
15536 /* Some people insist on not letting point enter the scroll
15537 margin, even though this part handles windows that didn't
15538 scroll at all. */
15539 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15540 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15541 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15542
15543 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15544 below, which finds the row to move point to, advances by
15545 the Y coordinate of the _next_ row, see the definition of
15546 MATRIX_ROW_BOTTOM_Y. */
15547 if (w->cursor.vpos < margin + header_line)
15548 new_vpos
15549 = pixel_margin + (header_line
15550 ? CURRENT_HEADER_LINE_HEIGHT (w)
15551 : 0) + FRAME_LINE_HEIGHT (f);
15552 else
15553 {
15554 int window_height = window_box_height (w);
15555
15556 if (header_line)
15557 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15558 if (w->cursor.y >= window_height - pixel_margin)
15559 new_vpos = window_height - pixel_margin;
15560 }
15561 }
15562
15563 /* If we need to move point for either of the above reasons,
15564 now actually do it. */
15565 if (new_vpos >= 0)
15566 {
15567 struct glyph_row *row;
15568
15569 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15570 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15571 ++row;
15572
15573 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15574 MATRIX_ROW_START_BYTEPOS (row));
15575
15576 if (w != XWINDOW (selected_window))
15577 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15578 else if (current_buffer == old)
15579 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15580
15581 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15582
15583 /* If we are highlighting the region, then we just changed
15584 the region, so redisplay to show it. */
15585 if (0 <= markpos_of_region ())
15586 {
15587 clear_glyph_matrix (w->desired_matrix);
15588 if (!try_window (window, startp, 0))
15589 goto need_larger_matrices;
15590 }
15591 }
15592
15593 #ifdef GLYPH_DEBUG
15594 debug_method_add (w, "forced window start");
15595 #endif
15596 goto done;
15597 }
15598
15599 /* Handle case where text has not changed, only point, and it has
15600 not moved off the frame, and we are not retrying after hscroll.
15601 (current_matrix_up_to_date_p is nonzero when retrying.) */
15602 if (current_matrix_up_to_date_p
15603 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15604 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15605 {
15606 switch (rc)
15607 {
15608 case CURSOR_MOVEMENT_SUCCESS:
15609 used_current_matrix_p = 1;
15610 goto done;
15611
15612 case CURSOR_MOVEMENT_MUST_SCROLL:
15613 goto try_to_scroll;
15614
15615 default:
15616 emacs_abort ();
15617 }
15618 }
15619 /* If current starting point was originally the beginning of a line
15620 but no longer is, find a new starting point. */
15621 else if (w->start_at_line_beg
15622 && !(CHARPOS (startp) <= BEGV
15623 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15624 {
15625 #ifdef GLYPH_DEBUG
15626 debug_method_add (w, "recenter 1");
15627 #endif
15628 goto recenter;
15629 }
15630
15631 /* Try scrolling with try_window_id. Value is > 0 if update has
15632 been done, it is -1 if we know that the same window start will
15633 not work. It is 0 if unsuccessful for some other reason. */
15634 else if ((tem = try_window_id (w)) != 0)
15635 {
15636 #ifdef GLYPH_DEBUG
15637 debug_method_add (w, "try_window_id %d", tem);
15638 #endif
15639
15640 if (fonts_changed_p)
15641 goto need_larger_matrices;
15642 if (tem > 0)
15643 goto done;
15644
15645 /* Otherwise try_window_id has returned -1 which means that we
15646 don't want the alternative below this comment to execute. */
15647 }
15648 else if (CHARPOS (startp) >= BEGV
15649 && CHARPOS (startp) <= ZV
15650 && PT >= CHARPOS (startp)
15651 && (CHARPOS (startp) < ZV
15652 /* Avoid starting at end of buffer. */
15653 || CHARPOS (startp) == BEGV
15654 || !window_outdated (w)))
15655 {
15656 int d1, d2, d3, d4, d5, d6;
15657
15658 /* If first window line is a continuation line, and window start
15659 is inside the modified region, but the first change is before
15660 current window start, we must select a new window start.
15661
15662 However, if this is the result of a down-mouse event (e.g. by
15663 extending the mouse-drag-overlay), we don't want to select a
15664 new window start, since that would change the position under
15665 the mouse, resulting in an unwanted mouse-movement rather
15666 than a simple mouse-click. */
15667 if (!w->start_at_line_beg
15668 && NILP (do_mouse_tracking)
15669 && CHARPOS (startp) > BEGV
15670 && CHARPOS (startp) > BEG + beg_unchanged
15671 && CHARPOS (startp) <= Z - end_unchanged
15672 /* Even if w->start_at_line_beg is nil, a new window may
15673 start at a line_beg, since that's how set_buffer_window
15674 sets it. So, we need to check the return value of
15675 compute_window_start_on_continuation_line. (See also
15676 bug#197). */
15677 && XMARKER (w->start)->buffer == current_buffer
15678 && compute_window_start_on_continuation_line (w)
15679 /* It doesn't make sense to force the window start like we
15680 do at label force_start if it is already known that point
15681 will not be visible in the resulting window, because
15682 doing so will move point from its correct position
15683 instead of scrolling the window to bring point into view.
15684 See bug#9324. */
15685 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15686 {
15687 w->force_start = 1;
15688 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15689 goto force_start;
15690 }
15691
15692 #ifdef GLYPH_DEBUG
15693 debug_method_add (w, "same window start");
15694 #endif
15695
15696 /* Try to redisplay starting at same place as before.
15697 If point has not moved off frame, accept the results. */
15698 if (!current_matrix_up_to_date_p
15699 /* Don't use try_window_reusing_current_matrix in this case
15700 because a window scroll function can have changed the
15701 buffer. */
15702 || !NILP (Vwindow_scroll_functions)
15703 || MINI_WINDOW_P (w)
15704 || !(used_current_matrix_p
15705 = try_window_reusing_current_matrix (w)))
15706 {
15707 IF_DEBUG (debug_method_add (w, "1"));
15708 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15709 /* -1 means we need to scroll.
15710 0 means we need new matrices, but fonts_changed_p
15711 is set in that case, so we will detect it below. */
15712 goto try_to_scroll;
15713 }
15714
15715 if (fonts_changed_p)
15716 goto need_larger_matrices;
15717
15718 if (w->cursor.vpos >= 0)
15719 {
15720 if (!just_this_one_p
15721 || current_buffer->clip_changed
15722 || BEG_UNCHANGED < CHARPOS (startp))
15723 /* Forget any recorded base line for line number display. */
15724 w->base_line_number = 0;
15725
15726 if (!cursor_row_fully_visible_p (w, 1, 0))
15727 {
15728 clear_glyph_matrix (w->desired_matrix);
15729 last_line_misfit = 1;
15730 }
15731 /* Drop through and scroll. */
15732 else
15733 goto done;
15734 }
15735 else
15736 clear_glyph_matrix (w->desired_matrix);
15737 }
15738
15739 try_to_scroll:
15740
15741 w->last_modified = 0;
15742 w->last_overlay_modified = 0;
15743
15744 /* Redisplay the mode line. Select the buffer properly for that. */
15745 if (!update_mode_line)
15746 {
15747 update_mode_line = 1;
15748 w->update_mode_line = 1;
15749 }
15750
15751 /* Try to scroll by specified few lines. */
15752 if ((scroll_conservatively
15753 || emacs_scroll_step
15754 || temp_scroll_step
15755 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15756 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15757 && CHARPOS (startp) >= BEGV
15758 && CHARPOS (startp) <= ZV)
15759 {
15760 /* The function returns -1 if new fonts were loaded, 1 if
15761 successful, 0 if not successful. */
15762 int ss = try_scrolling (window, just_this_one_p,
15763 scroll_conservatively,
15764 emacs_scroll_step,
15765 temp_scroll_step, last_line_misfit);
15766 switch (ss)
15767 {
15768 case SCROLLING_SUCCESS:
15769 goto done;
15770
15771 case SCROLLING_NEED_LARGER_MATRICES:
15772 goto need_larger_matrices;
15773
15774 case SCROLLING_FAILED:
15775 break;
15776
15777 default:
15778 emacs_abort ();
15779 }
15780 }
15781
15782 /* Finally, just choose a place to start which positions point
15783 according to user preferences. */
15784
15785 recenter:
15786
15787 #ifdef GLYPH_DEBUG
15788 debug_method_add (w, "recenter");
15789 #endif
15790
15791 /* Forget any previously recorded base line for line number display. */
15792 if (!buffer_unchanged_p)
15793 w->base_line_number = 0;
15794
15795 /* Determine the window start relative to point. */
15796 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15797 it.current_y = it.last_visible_y;
15798 if (centering_position < 0)
15799 {
15800 int margin =
15801 scroll_margin > 0
15802 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15803 : 0;
15804 ptrdiff_t margin_pos = CHARPOS (startp);
15805 Lisp_Object aggressive;
15806 int scrolling_up;
15807
15808 /* If there is a scroll margin at the top of the window, find
15809 its character position. */
15810 if (margin
15811 /* Cannot call start_display if startp is not in the
15812 accessible region of the buffer. This can happen when we
15813 have just switched to a different buffer and/or changed
15814 its restriction. In that case, startp is initialized to
15815 the character position 1 (BEGV) because we did not yet
15816 have chance to display the buffer even once. */
15817 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15818 {
15819 struct it it1;
15820 void *it1data = NULL;
15821
15822 SAVE_IT (it1, it, it1data);
15823 start_display (&it1, w, startp);
15824 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15825 margin_pos = IT_CHARPOS (it1);
15826 RESTORE_IT (&it, &it, it1data);
15827 }
15828 scrolling_up = PT > margin_pos;
15829 aggressive =
15830 scrolling_up
15831 ? BVAR (current_buffer, scroll_up_aggressively)
15832 : BVAR (current_buffer, scroll_down_aggressively);
15833
15834 if (!MINI_WINDOW_P (w)
15835 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15836 {
15837 int pt_offset = 0;
15838
15839 /* Setting scroll-conservatively overrides
15840 scroll-*-aggressively. */
15841 if (!scroll_conservatively && NUMBERP (aggressive))
15842 {
15843 double float_amount = XFLOATINT (aggressive);
15844
15845 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15846 if (pt_offset == 0 && float_amount > 0)
15847 pt_offset = 1;
15848 if (pt_offset && margin > 0)
15849 margin -= 1;
15850 }
15851 /* Compute how much to move the window start backward from
15852 point so that point will be displayed where the user
15853 wants it. */
15854 if (scrolling_up)
15855 {
15856 centering_position = it.last_visible_y;
15857 if (pt_offset)
15858 centering_position -= pt_offset;
15859 centering_position -=
15860 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15861 + WINDOW_HEADER_LINE_HEIGHT (w);
15862 /* Don't let point enter the scroll margin near top of
15863 the window. */
15864 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15865 centering_position = margin * FRAME_LINE_HEIGHT (f);
15866 }
15867 else
15868 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15869 }
15870 else
15871 /* Set the window start half the height of the window backward
15872 from point. */
15873 centering_position = window_box_height (w) / 2;
15874 }
15875 move_it_vertically_backward (&it, centering_position);
15876
15877 eassert (IT_CHARPOS (it) >= BEGV);
15878
15879 /* The function move_it_vertically_backward may move over more
15880 than the specified y-distance. If it->w is small, e.g. a
15881 mini-buffer window, we may end up in front of the window's
15882 display area. Start displaying at the start of the line
15883 containing PT in this case. */
15884 if (it.current_y <= 0)
15885 {
15886 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15887 move_it_vertically_backward (&it, 0);
15888 it.current_y = 0;
15889 }
15890
15891 it.current_x = it.hpos = 0;
15892
15893 /* Set the window start position here explicitly, to avoid an
15894 infinite loop in case the functions in window-scroll-functions
15895 get errors. */
15896 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15897
15898 /* Run scroll hooks. */
15899 startp = run_window_scroll_functions (window, it.current.pos);
15900
15901 /* Redisplay the window. */
15902 if (!current_matrix_up_to_date_p
15903 || windows_or_buffers_changed
15904 || cursor_type_changed
15905 /* Don't use try_window_reusing_current_matrix in this case
15906 because it can have changed the buffer. */
15907 || !NILP (Vwindow_scroll_functions)
15908 || !just_this_one_p
15909 || MINI_WINDOW_P (w)
15910 || !(used_current_matrix_p
15911 = try_window_reusing_current_matrix (w)))
15912 try_window (window, startp, 0);
15913
15914 /* If new fonts have been loaded (due to fontsets), give up. We
15915 have to start a new redisplay since we need to re-adjust glyph
15916 matrices. */
15917 if (fonts_changed_p)
15918 goto need_larger_matrices;
15919
15920 /* If cursor did not appear assume that the middle of the window is
15921 in the first line of the window. Do it again with the next line.
15922 (Imagine a window of height 100, displaying two lines of height
15923 60. Moving back 50 from it->last_visible_y will end in the first
15924 line.) */
15925 if (w->cursor.vpos < 0)
15926 {
15927 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15928 {
15929 clear_glyph_matrix (w->desired_matrix);
15930 move_it_by_lines (&it, 1);
15931 try_window (window, it.current.pos, 0);
15932 }
15933 else if (PT < IT_CHARPOS (it))
15934 {
15935 clear_glyph_matrix (w->desired_matrix);
15936 move_it_by_lines (&it, -1);
15937 try_window (window, it.current.pos, 0);
15938 }
15939 else
15940 {
15941 /* Not much we can do about it. */
15942 }
15943 }
15944
15945 /* Consider the following case: Window starts at BEGV, there is
15946 invisible, intangible text at BEGV, so that display starts at
15947 some point START > BEGV. It can happen that we are called with
15948 PT somewhere between BEGV and START. Try to handle that case. */
15949 if (w->cursor.vpos < 0)
15950 {
15951 struct glyph_row *row = w->current_matrix->rows;
15952 if (row->mode_line_p)
15953 ++row;
15954 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15955 }
15956
15957 if (!cursor_row_fully_visible_p (w, 0, 0))
15958 {
15959 /* If vscroll is enabled, disable it and try again. */
15960 if (w->vscroll)
15961 {
15962 w->vscroll = 0;
15963 clear_glyph_matrix (w->desired_matrix);
15964 goto recenter;
15965 }
15966
15967 /* Users who set scroll-conservatively to a large number want
15968 point just above/below the scroll margin. If we ended up
15969 with point's row partially visible, move the window start to
15970 make that row fully visible and out of the margin. */
15971 if (scroll_conservatively > SCROLL_LIMIT)
15972 {
15973 int margin =
15974 scroll_margin > 0
15975 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15976 : 0;
15977 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15978
15979 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15980 clear_glyph_matrix (w->desired_matrix);
15981 if (1 == try_window (window, it.current.pos,
15982 TRY_WINDOW_CHECK_MARGINS))
15983 goto done;
15984 }
15985
15986 /* If centering point failed to make the whole line visible,
15987 put point at the top instead. That has to make the whole line
15988 visible, if it can be done. */
15989 if (centering_position == 0)
15990 goto done;
15991
15992 clear_glyph_matrix (w->desired_matrix);
15993 centering_position = 0;
15994 goto recenter;
15995 }
15996
15997 done:
15998
15999 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16000 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16001 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16002
16003 /* Display the mode line, if we must. */
16004 if ((update_mode_line
16005 /* If window not full width, must redo its mode line
16006 if (a) the window to its side is being redone and
16007 (b) we do a frame-based redisplay. This is a consequence
16008 of how inverted lines are drawn in frame-based redisplay. */
16009 || (!just_this_one_p
16010 && !FRAME_WINDOW_P (f)
16011 && !WINDOW_FULL_WIDTH_P (w))
16012 /* Line number to display. */
16013 || w->base_line_pos > 0
16014 /* Column number is displayed and different from the one displayed. */
16015 || (w->column_number_displayed != -1
16016 && (w->column_number_displayed != current_column ())))
16017 /* This means that the window has a mode line. */
16018 && (WINDOW_WANTS_MODELINE_P (w)
16019 || WINDOW_WANTS_HEADER_LINE_P (w)))
16020 {
16021 display_mode_lines (w);
16022
16023 /* If mode line height has changed, arrange for a thorough
16024 immediate redisplay using the correct mode line height. */
16025 if (WINDOW_WANTS_MODELINE_P (w)
16026 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16027 {
16028 fonts_changed_p = 1;
16029 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16030 = DESIRED_MODE_LINE_HEIGHT (w);
16031 }
16032
16033 /* If header line height has changed, arrange for a thorough
16034 immediate redisplay using the correct header line height. */
16035 if (WINDOW_WANTS_HEADER_LINE_P (w)
16036 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16037 {
16038 fonts_changed_p = 1;
16039 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16040 = DESIRED_HEADER_LINE_HEIGHT (w);
16041 }
16042
16043 if (fonts_changed_p)
16044 goto need_larger_matrices;
16045 }
16046
16047 if (!line_number_displayed && w->base_line_pos != -1)
16048 {
16049 w->base_line_pos = 0;
16050 w->base_line_number = 0;
16051 }
16052
16053 finish_menu_bars:
16054
16055 /* When we reach a frame's selected window, redo the frame's menu bar. */
16056 if (update_mode_line
16057 && EQ (FRAME_SELECTED_WINDOW (f), window))
16058 {
16059 int redisplay_menu_p = 0;
16060
16061 if (FRAME_WINDOW_P (f))
16062 {
16063 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16064 || defined (HAVE_NS) || defined (USE_GTK)
16065 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16066 #else
16067 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16068 #endif
16069 }
16070 else
16071 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16072
16073 if (redisplay_menu_p)
16074 display_menu_bar (w);
16075
16076 #ifdef HAVE_WINDOW_SYSTEM
16077 if (FRAME_WINDOW_P (f))
16078 {
16079 #if defined (USE_GTK) || defined (HAVE_NS)
16080 if (FRAME_EXTERNAL_TOOL_BAR (f))
16081 redisplay_tool_bar (f);
16082 #else
16083 if (WINDOWP (f->tool_bar_window)
16084 && (FRAME_TOOL_BAR_LINES (f) > 0
16085 || !NILP (Vauto_resize_tool_bars))
16086 && redisplay_tool_bar (f))
16087 ignore_mouse_drag_p = 1;
16088 #endif
16089 }
16090 #endif
16091 }
16092
16093 #ifdef HAVE_WINDOW_SYSTEM
16094 if (FRAME_WINDOW_P (f)
16095 && update_window_fringes (w, (just_this_one_p
16096 || (!used_current_matrix_p && !overlay_arrow_seen)
16097 || w->pseudo_window_p)))
16098 {
16099 update_begin (f);
16100 block_input ();
16101 if (draw_window_fringes (w, 1))
16102 x_draw_vertical_border (w);
16103 unblock_input ();
16104 update_end (f);
16105 }
16106 #endif /* HAVE_WINDOW_SYSTEM */
16107
16108 /* We go to this label, with fonts_changed_p set,
16109 if it is necessary to try again using larger glyph matrices.
16110 We have to redeem the scroll bar even in this case,
16111 because the loop in redisplay_internal expects that. */
16112 need_larger_matrices:
16113 ;
16114 finish_scroll_bars:
16115
16116 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16117 {
16118 /* Set the thumb's position and size. */
16119 set_vertical_scroll_bar (w);
16120
16121 /* Note that we actually used the scroll bar attached to this
16122 window, so it shouldn't be deleted at the end of redisplay. */
16123 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16124 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16125 }
16126
16127 /* Restore current_buffer and value of point in it. The window
16128 update may have changed the buffer, so first make sure `opoint'
16129 is still valid (Bug#6177). */
16130 if (CHARPOS (opoint) < BEGV)
16131 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16132 else if (CHARPOS (opoint) > ZV)
16133 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16134 else
16135 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16136
16137 set_buffer_internal_1 (old);
16138 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16139 shorter. This can be caused by log truncation in *Messages*. */
16140 if (CHARPOS (lpoint) <= ZV)
16141 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16142
16143 unbind_to (count, Qnil);
16144 }
16145
16146
16147 /* Build the complete desired matrix of WINDOW with a window start
16148 buffer position POS.
16149
16150 Value is 1 if successful. It is zero if fonts were loaded during
16151 redisplay which makes re-adjusting glyph matrices necessary, and -1
16152 if point would appear in the scroll margins.
16153 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16154 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16155 set in FLAGS.) */
16156
16157 int
16158 try_window (Lisp_Object window, struct text_pos pos, int flags)
16159 {
16160 struct window *w = XWINDOW (window);
16161 struct it it;
16162 struct glyph_row *last_text_row = NULL;
16163 struct frame *f = XFRAME (w->frame);
16164
16165 /* Make POS the new window start. */
16166 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16167
16168 /* Mark cursor position as unknown. No overlay arrow seen. */
16169 w->cursor.vpos = -1;
16170 overlay_arrow_seen = 0;
16171
16172 /* Initialize iterator and info to start at POS. */
16173 start_display (&it, w, pos);
16174
16175
16176
16177 /* Display all lines of W. */
16178 while (it.current_y < it.last_visible_y)
16179 {
16180 if (display_line (&it))
16181 last_text_row = it.glyph_row - 1;
16182 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16183 return 0;
16184 }
16185 #ifdef HAVE_XWIDGETS_xxx
16186 //currently this is needed to detect xwidget movement reliably. or probably not.
16187 printf("try_window\n");
16188 return 0;
16189 #endif
16190
16191 /* Don't let the cursor end in the scroll margins. */
16192 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16193 && !MINI_WINDOW_P (w))
16194 {
16195 int this_scroll_margin;
16196
16197 if (scroll_margin > 0)
16198 {
16199 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16200 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16201 }
16202 else
16203 this_scroll_margin = 0;
16204
16205 if ((w->cursor.y >= 0 /* not vscrolled */
16206 && w->cursor.y < this_scroll_margin
16207 && CHARPOS (pos) > BEGV
16208 && IT_CHARPOS (it) < ZV)
16209 /* rms: considering make_cursor_line_fully_visible_p here
16210 seems to give wrong results. We don't want to recenter
16211 when the last line is partly visible, we want to allow
16212 that case to be handled in the usual way. */
16213 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16214 {
16215 w->cursor.vpos = -1;
16216 clear_glyph_matrix (w->desired_matrix);
16217 return -1;
16218 }
16219 }
16220
16221 /* If bottom moved off end of frame, change mode line percentage. */
16222 if (XFASTINT (w->window_end_pos) <= 0
16223 && Z != IT_CHARPOS (it))
16224 w->update_mode_line = 1;
16225
16226 /* Set window_end_pos to the offset of the last character displayed
16227 on the window from the end of current_buffer. Set
16228 window_end_vpos to its row number. */
16229 if (last_text_row)
16230 {
16231 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16232 w->window_end_bytepos
16233 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16234 wset_window_end_pos
16235 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16236 wset_window_end_vpos
16237 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16238 eassert
16239 (MATRIX_ROW (w->desired_matrix,
16240 XFASTINT (w->window_end_vpos))->displays_text_p);
16241 }
16242 else
16243 {
16244 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16245 wset_window_end_pos (w, make_number (Z - ZV));
16246 wset_window_end_vpos (w, make_number (0));
16247 }
16248
16249 /* But that is not valid info until redisplay finishes. */
16250 w->window_end_valid = 0;
16251 return 1;
16252 }
16253
16254
16255 \f
16256 /************************************************************************
16257 Window redisplay reusing current matrix when buffer has not changed
16258 ************************************************************************/
16259
16260 /* Try redisplay of window W showing an unchanged buffer with a
16261 different window start than the last time it was displayed by
16262 reusing its current matrix. Value is non-zero if successful.
16263 W->start is the new window start. */
16264
16265 static int
16266 try_window_reusing_current_matrix (struct window *w)
16267 {
16268 struct frame *f = XFRAME (w->frame);
16269 struct glyph_row *bottom_row;
16270 struct it it;
16271 struct run run;
16272 struct text_pos start, new_start;
16273 int nrows_scrolled, i;
16274 struct glyph_row *last_text_row;
16275 struct glyph_row *last_reused_text_row;
16276 struct glyph_row *start_row;
16277 int start_vpos, min_y, max_y;
16278
16279 #ifdef GLYPH_DEBUG
16280 if (inhibit_try_window_reusing)
16281 return 0;
16282 #endif
16283
16284 #ifdef HAVE_XWIDGETS_xxx
16285 //currently this is needed to detect xwidget movement reliably. or probably not.
16286 printf("try_window_reusing_current_matrix\n");
16287 return 0;
16288 #endif
16289
16290
16291 if (/* This function doesn't handle terminal frames. */
16292 !FRAME_WINDOW_P (f)
16293 /* Don't try to reuse the display if windows have been split
16294 or such. */
16295 || windows_or_buffers_changed
16296 || cursor_type_changed)
16297 return 0;
16298
16299 /* Can't do this if region may have changed. */
16300 if (0 <= markpos_of_region ()
16301 || w->region_showing
16302 || !NILP (Vshow_trailing_whitespace))
16303 return 0;
16304
16305 /* If top-line visibility has changed, give up. */
16306 if (WINDOW_WANTS_HEADER_LINE_P (w)
16307 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16308 return 0;
16309
16310 /* Give up if old or new display is scrolled vertically. We could
16311 make this function handle this, but right now it doesn't. */
16312 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16313 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16314 return 0;
16315
16316 /* The variable new_start now holds the new window start. The old
16317 start `start' can be determined from the current matrix. */
16318 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16319 start = start_row->minpos;
16320 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16321
16322 /* Clear the desired matrix for the display below. */
16323 clear_glyph_matrix (w->desired_matrix);
16324
16325 if (CHARPOS (new_start) <= CHARPOS (start))
16326 {
16327 /* Don't use this method if the display starts with an ellipsis
16328 displayed for invisible text. It's not easy to handle that case
16329 below, and it's certainly not worth the effort since this is
16330 not a frequent case. */
16331 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16332 return 0;
16333
16334 IF_DEBUG (debug_method_add (w, "twu1"));
16335
16336 /* Display up to a row that can be reused. The variable
16337 last_text_row is set to the last row displayed that displays
16338 text. Note that it.vpos == 0 if or if not there is a
16339 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16340 start_display (&it, w, new_start);
16341 w->cursor.vpos = -1;
16342 last_text_row = last_reused_text_row = NULL;
16343
16344 while (it.current_y < it.last_visible_y
16345 && !fonts_changed_p)
16346 {
16347 /* If we have reached into the characters in the START row,
16348 that means the line boundaries have changed. So we
16349 can't start copying with the row START. Maybe it will
16350 work to start copying with the following row. */
16351 while (IT_CHARPOS (it) > CHARPOS (start))
16352 {
16353 /* Advance to the next row as the "start". */
16354 start_row++;
16355 start = start_row->minpos;
16356 /* If there are no more rows to try, or just one, give up. */
16357 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16358 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16359 || CHARPOS (start) == ZV)
16360 {
16361 clear_glyph_matrix (w->desired_matrix);
16362 return 0;
16363 }
16364
16365 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16366 }
16367 /* If we have reached alignment, we can copy the rest of the
16368 rows. */
16369 if (IT_CHARPOS (it) == CHARPOS (start)
16370 /* Don't accept "alignment" inside a display vector,
16371 since start_row could have started in the middle of
16372 that same display vector (thus their character
16373 positions match), and we have no way of telling if
16374 that is the case. */
16375 && it.current.dpvec_index < 0)
16376 break;
16377
16378 if (display_line (&it))
16379 last_text_row = it.glyph_row - 1;
16380
16381 }
16382
16383 /* A value of current_y < last_visible_y means that we stopped
16384 at the previous window start, which in turn means that we
16385 have at least one reusable row. */
16386 if (it.current_y < it.last_visible_y)
16387 {
16388 struct glyph_row *row;
16389
16390 /* IT.vpos always starts from 0; it counts text lines. */
16391 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16392
16393 /* Find PT if not already found in the lines displayed. */
16394 if (w->cursor.vpos < 0)
16395 {
16396 int dy = it.current_y - start_row->y;
16397
16398 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16399 row = row_containing_pos (w, PT, row, NULL, dy);
16400 if (row)
16401 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16402 dy, nrows_scrolled);
16403 else
16404 {
16405 clear_glyph_matrix (w->desired_matrix);
16406 return 0;
16407 }
16408 }
16409
16410 /* Scroll the display. Do it before the current matrix is
16411 changed. The problem here is that update has not yet
16412 run, i.e. part of the current matrix is not up to date.
16413 scroll_run_hook will clear the cursor, and use the
16414 current matrix to get the height of the row the cursor is
16415 in. */
16416 run.current_y = start_row->y;
16417 run.desired_y = it.current_y;
16418 run.height = it.last_visible_y - it.current_y;
16419
16420 if (run.height > 0 && run.current_y != run.desired_y)
16421 {
16422 update_begin (f);
16423 FRAME_RIF (f)->update_window_begin_hook (w);
16424 FRAME_RIF (f)->clear_window_mouse_face (w);
16425 FRAME_RIF (f)->scroll_run_hook (w, &run);
16426 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16427 update_end (f);
16428 }
16429
16430 /* Shift current matrix down by nrows_scrolled lines. */
16431 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16432 rotate_matrix (w->current_matrix,
16433 start_vpos,
16434 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16435 nrows_scrolled);
16436
16437 /* Disable lines that must be updated. */
16438 for (i = 0; i < nrows_scrolled; ++i)
16439 (start_row + i)->enabled_p = 0;
16440
16441 /* Re-compute Y positions. */
16442 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16443 max_y = it.last_visible_y;
16444 for (row = start_row + nrows_scrolled;
16445 row < bottom_row;
16446 ++row)
16447 {
16448 row->y = it.current_y;
16449 row->visible_height = row->height;
16450
16451 if (row->y < min_y)
16452 row->visible_height -= min_y - row->y;
16453 if (row->y + row->height > max_y)
16454 row->visible_height -= row->y + row->height - max_y;
16455 if (row->fringe_bitmap_periodic_p)
16456 row->redraw_fringe_bitmaps_p = 1;
16457
16458 it.current_y += row->height;
16459
16460 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16461 last_reused_text_row = row;
16462 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16463 break;
16464 }
16465
16466 /* Disable lines in the current matrix which are now
16467 below the window. */
16468 for (++row; row < bottom_row; ++row)
16469 row->enabled_p = row->mode_line_p = 0;
16470 }
16471
16472 /* Update window_end_pos etc.; last_reused_text_row is the last
16473 reused row from the current matrix containing text, if any.
16474 The value of last_text_row is the last displayed line
16475 containing text. */
16476 if (last_reused_text_row)
16477 {
16478 w->window_end_bytepos
16479 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16480 wset_window_end_pos
16481 (w, make_number (Z
16482 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16483 wset_window_end_vpos
16484 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16485 w->current_matrix)));
16486 }
16487 else if (last_text_row)
16488 {
16489 w->window_end_bytepos
16490 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16491 wset_window_end_pos
16492 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16493 wset_window_end_vpos
16494 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16495 w->desired_matrix)));
16496 }
16497 else
16498 {
16499 /* This window must be completely empty. */
16500 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16501 wset_window_end_pos (w, make_number (Z - ZV));
16502 wset_window_end_vpos (w, make_number (0));
16503 }
16504 w->window_end_valid = 0;
16505
16506 /* Update hint: don't try scrolling again in update_window. */
16507 w->desired_matrix->no_scrolling_p = 1;
16508
16509 #ifdef GLYPH_DEBUG
16510 debug_method_add (w, "try_window_reusing_current_matrix 1");
16511 #endif
16512 return 1;
16513 }
16514 else if (CHARPOS (new_start) > CHARPOS (start))
16515 {
16516 struct glyph_row *pt_row, *row;
16517 struct glyph_row *first_reusable_row;
16518 struct glyph_row *first_row_to_display;
16519 int dy;
16520 int yb = window_text_bottom_y (w);
16521
16522 /* Find the row starting at new_start, if there is one. Don't
16523 reuse a partially visible line at the end. */
16524 first_reusable_row = start_row;
16525 while (first_reusable_row->enabled_p
16526 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16527 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16528 < CHARPOS (new_start)))
16529 ++first_reusable_row;
16530
16531 /* Give up if there is no row to reuse. */
16532 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16533 || !first_reusable_row->enabled_p
16534 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16535 != CHARPOS (new_start)))
16536 return 0;
16537
16538 /* We can reuse fully visible rows beginning with
16539 first_reusable_row to the end of the window. Set
16540 first_row_to_display to the first row that cannot be reused.
16541 Set pt_row to the row containing point, if there is any. */
16542 pt_row = NULL;
16543 for (first_row_to_display = first_reusable_row;
16544 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16545 ++first_row_to_display)
16546 {
16547 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16548 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16549 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16550 && first_row_to_display->ends_at_zv_p
16551 && pt_row == NULL)))
16552 pt_row = first_row_to_display;
16553 }
16554
16555 /* Start displaying at the start of first_row_to_display. */
16556 eassert (first_row_to_display->y < yb);
16557 init_to_row_start (&it, w, first_row_to_display);
16558
16559 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16560 - start_vpos);
16561 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16562 - nrows_scrolled);
16563 it.current_y = (first_row_to_display->y - first_reusable_row->y
16564 + WINDOW_HEADER_LINE_HEIGHT (w));
16565
16566 /* Display lines beginning with first_row_to_display in the
16567 desired matrix. Set last_text_row to the last row displayed
16568 that displays text. */
16569 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16570 if (pt_row == NULL)
16571 w->cursor.vpos = -1;
16572 last_text_row = NULL;
16573 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16574 if (display_line (&it))
16575 last_text_row = it.glyph_row - 1;
16576
16577 /* If point is in a reused row, adjust y and vpos of the cursor
16578 position. */
16579 if (pt_row)
16580 {
16581 w->cursor.vpos -= nrows_scrolled;
16582 w->cursor.y -= first_reusable_row->y - start_row->y;
16583 }
16584
16585 /* Give up if point isn't in a row displayed or reused. (This
16586 also handles the case where w->cursor.vpos < nrows_scrolled
16587 after the calls to display_line, which can happen with scroll
16588 margins. See bug#1295.) */
16589 if (w->cursor.vpos < 0)
16590 {
16591 clear_glyph_matrix (w->desired_matrix);
16592 return 0;
16593 }
16594
16595 /* Scroll the display. */
16596 run.current_y = first_reusable_row->y;
16597 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16598 run.height = it.last_visible_y - run.current_y;
16599 dy = run.current_y - run.desired_y;
16600
16601 if (run.height)
16602 {
16603 update_begin (f);
16604 FRAME_RIF (f)->update_window_begin_hook (w);
16605 FRAME_RIF (f)->clear_window_mouse_face (w);
16606 FRAME_RIF (f)->scroll_run_hook (w, &run);
16607 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16608 update_end (f);
16609 }
16610
16611 /* Adjust Y positions of reused rows. */
16612 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16613 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16614 max_y = it.last_visible_y;
16615 for (row = first_reusable_row; row < first_row_to_display; ++row)
16616 {
16617 row->y -= dy;
16618 row->visible_height = row->height;
16619 if (row->y < min_y)
16620 row->visible_height -= min_y - row->y;
16621 if (row->y + row->height > max_y)
16622 row->visible_height -= row->y + row->height - max_y;
16623 if (row->fringe_bitmap_periodic_p)
16624 row->redraw_fringe_bitmaps_p = 1;
16625 }
16626
16627 /* Scroll the current matrix. */
16628 eassert (nrows_scrolled > 0);
16629 rotate_matrix (w->current_matrix,
16630 start_vpos,
16631 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16632 -nrows_scrolled);
16633
16634 /* Disable rows not reused. */
16635 for (row -= nrows_scrolled; row < bottom_row; ++row)
16636 row->enabled_p = 0;
16637
16638 /* Point may have moved to a different line, so we cannot assume that
16639 the previous cursor position is valid; locate the correct row. */
16640 if (pt_row)
16641 {
16642 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16643 row < bottom_row
16644 && PT >= MATRIX_ROW_END_CHARPOS (row)
16645 && !row->ends_at_zv_p;
16646 row++)
16647 {
16648 w->cursor.vpos++;
16649 w->cursor.y = row->y;
16650 }
16651 if (row < bottom_row)
16652 {
16653 /* Can't simply scan the row for point with
16654 bidi-reordered glyph rows. Let set_cursor_from_row
16655 figure out where to put the cursor, and if it fails,
16656 give up. */
16657 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16658 {
16659 if (!set_cursor_from_row (w, row, w->current_matrix,
16660 0, 0, 0, 0))
16661 {
16662 clear_glyph_matrix (w->desired_matrix);
16663 return 0;
16664 }
16665 }
16666 else
16667 {
16668 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16669 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16670
16671 for (; glyph < end
16672 && (!BUFFERP (glyph->object)
16673 || glyph->charpos < PT);
16674 glyph++)
16675 {
16676 w->cursor.hpos++;
16677 w->cursor.x += glyph->pixel_width;
16678 }
16679 }
16680 }
16681 }
16682
16683 /* Adjust window end. A null value of last_text_row means that
16684 the window end is in reused rows which in turn means that
16685 only its vpos can have changed. */
16686 if (last_text_row)
16687 {
16688 w->window_end_bytepos
16689 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16690 wset_window_end_pos
16691 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16692 wset_window_end_vpos
16693 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16694 w->desired_matrix)));
16695 }
16696 else
16697 {
16698 wset_window_end_vpos
16699 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16700 }
16701
16702 w->window_end_valid = 0;
16703 w->desired_matrix->no_scrolling_p = 1;
16704
16705 #ifdef GLYPH_DEBUG
16706 debug_method_add (w, "try_window_reusing_current_matrix 2");
16707 #endif
16708 return 1;
16709 }
16710
16711 return 0;
16712 }
16713
16714
16715 \f
16716 /************************************************************************
16717 Window redisplay reusing current matrix when buffer has changed
16718 ************************************************************************/
16719
16720 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16721 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16722 ptrdiff_t *, ptrdiff_t *);
16723 static struct glyph_row *
16724 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16725 struct glyph_row *);
16726
16727
16728 /* Return the last row in MATRIX displaying text. If row START is
16729 non-null, start searching with that row. IT gives the dimensions
16730 of the display. Value is null if matrix is empty; otherwise it is
16731 a pointer to the row found. */
16732
16733 static struct glyph_row *
16734 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16735 struct glyph_row *start)
16736 {
16737 struct glyph_row *row, *row_found;
16738
16739 /* Set row_found to the last row in IT->w's current matrix
16740 displaying text. The loop looks funny but think of partially
16741 visible lines. */
16742 row_found = NULL;
16743 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16744 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16745 {
16746 eassert (row->enabled_p);
16747 row_found = row;
16748 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16749 break;
16750 ++row;
16751 }
16752
16753 return row_found;
16754 }
16755
16756
16757 /* Return the last row in the current matrix of W that is not affected
16758 by changes at the start of current_buffer that occurred since W's
16759 current matrix was built. Value is null if no such row exists.
16760
16761 BEG_UNCHANGED us the number of characters unchanged at the start of
16762 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16763 first changed character in current_buffer. Characters at positions <
16764 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16765 when the current matrix was built. */
16766
16767 static struct glyph_row *
16768 find_last_unchanged_at_beg_row (struct window *w)
16769 {
16770 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16771 struct glyph_row *row;
16772 struct glyph_row *row_found = NULL;
16773 int yb = window_text_bottom_y (w);
16774
16775 /* Find the last row displaying unchanged text. */
16776 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16777 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16778 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16779 ++row)
16780 {
16781 if (/* If row ends before first_changed_pos, it is unchanged,
16782 except in some case. */
16783 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16784 /* When row ends in ZV and we write at ZV it is not
16785 unchanged. */
16786 && !row->ends_at_zv_p
16787 /* When first_changed_pos is the end of a continued line,
16788 row is not unchanged because it may be no longer
16789 continued. */
16790 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16791 && (row->continued_p
16792 || row->exact_window_width_line_p))
16793 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16794 needs to be recomputed, so don't consider this row as
16795 unchanged. This happens when the last line was
16796 bidi-reordered and was killed immediately before this
16797 redisplay cycle. In that case, ROW->end stores the
16798 buffer position of the first visual-order character of
16799 the killed text, which is now beyond ZV. */
16800 && CHARPOS (row->end.pos) <= ZV)
16801 row_found = row;
16802
16803 /* Stop if last visible row. */
16804 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16805 break;
16806 }
16807
16808 return row_found;
16809 }
16810
16811
16812 /* Find the first glyph row in the current matrix of W that is not
16813 affected by changes at the end of current_buffer since the
16814 time W's current matrix was built.
16815
16816 Return in *DELTA the number of chars by which buffer positions in
16817 unchanged text at the end of current_buffer must be adjusted.
16818
16819 Return in *DELTA_BYTES the corresponding number of bytes.
16820
16821 Value is null if no such row exists, i.e. all rows are affected by
16822 changes. */
16823
16824 static struct glyph_row *
16825 find_first_unchanged_at_end_row (struct window *w,
16826 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16827 {
16828 struct glyph_row *row;
16829 struct glyph_row *row_found = NULL;
16830
16831 *delta = *delta_bytes = 0;
16832
16833 /* Display must not have been paused, otherwise the current matrix
16834 is not up to date. */
16835 eassert (w->window_end_valid);
16836
16837 /* A value of window_end_pos >= END_UNCHANGED means that the window
16838 end is in the range of changed text. If so, there is no
16839 unchanged row at the end of W's current matrix. */
16840 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16841 return NULL;
16842
16843 /* Set row to the last row in W's current matrix displaying text. */
16844 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16845
16846 /* If matrix is entirely empty, no unchanged row exists. */
16847 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16848 {
16849 /* The value of row is the last glyph row in the matrix having a
16850 meaningful buffer position in it. The end position of row
16851 corresponds to window_end_pos. This allows us to translate
16852 buffer positions in the current matrix to current buffer
16853 positions for characters not in changed text. */
16854 ptrdiff_t Z_old =
16855 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16856 ptrdiff_t Z_BYTE_old =
16857 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16858 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16859 struct glyph_row *first_text_row
16860 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16861
16862 *delta = Z - Z_old;
16863 *delta_bytes = Z_BYTE - Z_BYTE_old;
16864
16865 /* Set last_unchanged_pos to the buffer position of the last
16866 character in the buffer that has not been changed. Z is the
16867 index + 1 of the last character in current_buffer, i.e. by
16868 subtracting END_UNCHANGED we get the index of the last
16869 unchanged character, and we have to add BEG to get its buffer
16870 position. */
16871 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16872 last_unchanged_pos_old = last_unchanged_pos - *delta;
16873
16874 /* Search backward from ROW for a row displaying a line that
16875 starts at a minimum position >= last_unchanged_pos_old. */
16876 for (; row > first_text_row; --row)
16877 {
16878 /* This used to abort, but it can happen.
16879 It is ok to just stop the search instead here. KFS. */
16880 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16881 break;
16882
16883 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16884 row_found = row;
16885 }
16886 }
16887
16888 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16889
16890 return row_found;
16891 }
16892
16893
16894 /* Make sure that glyph rows in the current matrix of window W
16895 reference the same glyph memory as corresponding rows in the
16896 frame's frame matrix. This function is called after scrolling W's
16897 current matrix on a terminal frame in try_window_id and
16898 try_window_reusing_current_matrix. */
16899
16900 static void
16901 sync_frame_with_window_matrix_rows (struct window *w)
16902 {
16903 struct frame *f = XFRAME (w->frame);
16904 struct glyph_row *window_row, *window_row_end, *frame_row;
16905
16906 /* Preconditions: W must be a leaf window and full-width. Its frame
16907 must have a frame matrix. */
16908 eassert (NILP (w->hchild) && NILP (w->vchild));
16909 eassert (WINDOW_FULL_WIDTH_P (w));
16910 eassert (!FRAME_WINDOW_P (f));
16911
16912 /* If W is a full-width window, glyph pointers in W's current matrix
16913 have, by definition, to be the same as glyph pointers in the
16914 corresponding frame matrix. Note that frame matrices have no
16915 marginal areas (see build_frame_matrix). */
16916 window_row = w->current_matrix->rows;
16917 window_row_end = window_row + w->current_matrix->nrows;
16918 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16919 while (window_row < window_row_end)
16920 {
16921 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16922 struct glyph *end = window_row->glyphs[LAST_AREA];
16923
16924 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16925 frame_row->glyphs[TEXT_AREA] = start;
16926 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16927 frame_row->glyphs[LAST_AREA] = end;
16928
16929 /* Disable frame rows whose corresponding window rows have
16930 been disabled in try_window_id. */
16931 if (!window_row->enabled_p)
16932 frame_row->enabled_p = 0;
16933
16934 ++window_row, ++frame_row;
16935 }
16936 }
16937
16938
16939 /* Find the glyph row in window W containing CHARPOS. Consider all
16940 rows between START and END (not inclusive). END null means search
16941 all rows to the end of the display area of W. Value is the row
16942 containing CHARPOS or null. */
16943
16944 struct glyph_row *
16945 row_containing_pos (struct window *w, ptrdiff_t charpos,
16946 struct glyph_row *start, struct glyph_row *end, int dy)
16947 {
16948 struct glyph_row *row = start;
16949 struct glyph_row *best_row = NULL;
16950 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16951 int last_y;
16952
16953 /* If we happen to start on a header-line, skip that. */
16954 if (row->mode_line_p)
16955 ++row;
16956
16957 if ((end && row >= end) || !row->enabled_p)
16958 return NULL;
16959
16960 last_y = window_text_bottom_y (w) - dy;
16961
16962 while (1)
16963 {
16964 /* Give up if we have gone too far. */
16965 if (end && row >= end)
16966 return NULL;
16967 /* This formerly returned if they were equal.
16968 I think that both quantities are of a "last plus one" type;
16969 if so, when they are equal, the row is within the screen. -- rms. */
16970 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16971 return NULL;
16972
16973 /* If it is in this row, return this row. */
16974 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16975 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16976 /* The end position of a row equals the start
16977 position of the next row. If CHARPOS is there, we
16978 would rather display it in the next line, except
16979 when this line ends in ZV. */
16980 && !row->ends_at_zv_p
16981 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16982 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16983 {
16984 struct glyph *g;
16985
16986 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16987 || (!best_row && !row->continued_p))
16988 return row;
16989 /* In bidi-reordered rows, there could be several rows
16990 occluding point, all of them belonging to the same
16991 continued line. We need to find the row which fits
16992 CHARPOS the best. */
16993 for (g = row->glyphs[TEXT_AREA];
16994 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16995 g++)
16996 {
16997 if (!STRINGP (g->object))
16998 {
16999 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17000 {
17001 mindif = eabs (g->charpos - charpos);
17002 best_row = row;
17003 /* Exact match always wins. */
17004 if (mindif == 0)
17005 return best_row;
17006 }
17007 }
17008 }
17009 }
17010 else if (best_row && !row->continued_p)
17011 return best_row;
17012 ++row;
17013 }
17014 }
17015
17016
17017 /* Try to redisplay window W by reusing its existing display. W's
17018 current matrix must be up to date when this function is called,
17019 i.e. window_end_valid must be nonzero.
17020
17021 Value is
17022
17023 1 if display has been updated
17024 0 if otherwise unsuccessful
17025 -1 if redisplay with same window start is known not to succeed
17026
17027 The following steps are performed:
17028
17029 1. Find the last row in the current matrix of W that is not
17030 affected by changes at the start of current_buffer. If no such row
17031 is found, give up.
17032
17033 2. Find the first row in W's current matrix that is not affected by
17034 changes at the end of current_buffer. Maybe there is no such row.
17035
17036 3. Display lines beginning with the row + 1 found in step 1 to the
17037 row found in step 2 or, if step 2 didn't find a row, to the end of
17038 the window.
17039
17040 4. If cursor is not known to appear on the window, give up.
17041
17042 5. If display stopped at the row found in step 2, scroll the
17043 display and current matrix as needed.
17044
17045 6. Maybe display some lines at the end of W, if we must. This can
17046 happen under various circumstances, like a partially visible line
17047 becoming fully visible, or because newly displayed lines are displayed
17048 in smaller font sizes.
17049
17050 7. Update W's window end information. */
17051
17052 static int
17053 try_window_id (struct window *w)
17054 {
17055 struct frame *f = XFRAME (w->frame);
17056 struct glyph_matrix *current_matrix = w->current_matrix;
17057 struct glyph_matrix *desired_matrix = w->desired_matrix;
17058 struct glyph_row *last_unchanged_at_beg_row;
17059 struct glyph_row *first_unchanged_at_end_row;
17060 struct glyph_row *row;
17061 struct glyph_row *bottom_row;
17062 int bottom_vpos;
17063 struct it it;
17064 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17065 int dvpos, dy;
17066 struct text_pos start_pos;
17067 struct run run;
17068 int first_unchanged_at_end_vpos = 0;
17069 struct glyph_row *last_text_row, *last_text_row_at_end;
17070 struct text_pos start;
17071 ptrdiff_t first_changed_charpos, last_changed_charpos;
17072
17073 #ifdef GLYPH_DEBUG
17074 if (inhibit_try_window_id)
17075 return 0;
17076 #endif
17077
17078 #ifdef HAVE_XWIDGETS_xxx
17079 //maybe needed for proper xwidget movement
17080 printf("try_window_id\n");
17081 return -1;
17082 #endif
17083
17084
17085 /* This is handy for debugging. */
17086 #if 0
17087 #define GIVE_UP(X) \
17088 do { \
17089 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17090 return 0; \
17091 } while (0)
17092 #else
17093 #define GIVE_UP(X) return 0
17094 #endif
17095
17096 SET_TEXT_POS_FROM_MARKER (start, w->start);
17097
17098 /* Don't use this for mini-windows because these can show
17099 messages and mini-buffers, and we don't handle that here. */
17100 if (MINI_WINDOW_P (w))
17101 GIVE_UP (1);
17102
17103 /* This flag is used to prevent redisplay optimizations. */
17104 if (windows_or_buffers_changed || cursor_type_changed)
17105 GIVE_UP (2);
17106
17107 /* Verify that narrowing has not changed.
17108 Also verify that we were not told to prevent redisplay optimizations.
17109 It would be nice to further
17110 reduce the number of cases where this prevents try_window_id. */
17111 if (current_buffer->clip_changed
17112 || current_buffer->prevent_redisplay_optimizations_p)
17113 GIVE_UP (3);
17114
17115 /* Window must either use window-based redisplay or be full width. */
17116 if (!FRAME_WINDOW_P (f)
17117 && (!FRAME_LINE_INS_DEL_OK (f)
17118 || !WINDOW_FULL_WIDTH_P (w)))
17119 GIVE_UP (4);
17120
17121 /* Give up if point is known NOT to appear in W. */
17122 if (PT < CHARPOS (start))
17123 GIVE_UP (5);
17124
17125 /* Another way to prevent redisplay optimizations. */
17126 if (w->last_modified == 0)
17127 GIVE_UP (6);
17128
17129 /* Verify that window is not hscrolled. */
17130 if (w->hscroll != 0)
17131 GIVE_UP (7);
17132
17133 /* Verify that display wasn't paused. */
17134 if (!w->window_end_valid)
17135 GIVE_UP (8);
17136
17137 /* Can't use this if highlighting a region because a cursor movement
17138 will do more than just set the cursor. */
17139 if (0 <= markpos_of_region ())
17140 GIVE_UP (9);
17141
17142 /* Likewise if highlighting trailing whitespace. */
17143 if (!NILP (Vshow_trailing_whitespace))
17144 GIVE_UP (11);
17145
17146 /* Likewise if showing a region. */
17147 if (w->region_showing)
17148 GIVE_UP (10);
17149
17150 /* Can't use this if overlay arrow position and/or string have
17151 changed. */
17152 if (overlay_arrows_changed_p ())
17153 GIVE_UP (12);
17154
17155 /* When word-wrap is on, adding a space to the first word of a
17156 wrapped line can change the wrap position, altering the line
17157 above it. It might be worthwhile to handle this more
17158 intelligently, but for now just redisplay from scratch. */
17159 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17160 GIVE_UP (21);
17161
17162 /* Under bidi reordering, adding or deleting a character in the
17163 beginning of a paragraph, before the first strong directional
17164 character, can change the base direction of the paragraph (unless
17165 the buffer specifies a fixed paragraph direction), which will
17166 require to redisplay the whole paragraph. It might be worthwhile
17167 to find the paragraph limits and widen the range of redisplayed
17168 lines to that, but for now just give up this optimization and
17169 redisplay from scratch. */
17170 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17171 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17172 GIVE_UP (22);
17173
17174 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17175 only if buffer has really changed. The reason is that the gap is
17176 initially at Z for freshly visited files. The code below would
17177 set end_unchanged to 0 in that case. */
17178 if (MODIFF > SAVE_MODIFF
17179 /* This seems to happen sometimes after saving a buffer. */
17180 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17181 {
17182 if (GPT - BEG < BEG_UNCHANGED)
17183 BEG_UNCHANGED = GPT - BEG;
17184 if (Z - GPT < END_UNCHANGED)
17185 END_UNCHANGED = Z - GPT;
17186 }
17187
17188 /* The position of the first and last character that has been changed. */
17189 first_changed_charpos = BEG + BEG_UNCHANGED;
17190 last_changed_charpos = Z - END_UNCHANGED;
17191
17192 /* If window starts after a line end, and the last change is in
17193 front of that newline, then changes don't affect the display.
17194 This case happens with stealth-fontification. Note that although
17195 the display is unchanged, glyph positions in the matrix have to
17196 be adjusted, of course. */
17197 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17198 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17199 && ((last_changed_charpos < CHARPOS (start)
17200 && CHARPOS (start) == BEGV)
17201 || (last_changed_charpos < CHARPOS (start) - 1
17202 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17203 {
17204 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17205 struct glyph_row *r0;
17206
17207 /* Compute how many chars/bytes have been added to or removed
17208 from the buffer. */
17209 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17210 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17211 Z_delta = Z - Z_old;
17212 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17213
17214 /* Give up if PT is not in the window. Note that it already has
17215 been checked at the start of try_window_id that PT is not in
17216 front of the window start. */
17217 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17218 GIVE_UP (13);
17219
17220 /* If window start is unchanged, we can reuse the whole matrix
17221 as is, after adjusting glyph positions. No need to compute
17222 the window end again, since its offset from Z hasn't changed. */
17223 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17224 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17225 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17226 /* PT must not be in a partially visible line. */
17227 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17228 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17229 {
17230 /* Adjust positions in the glyph matrix. */
17231 if (Z_delta || Z_delta_bytes)
17232 {
17233 struct glyph_row *r1
17234 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17235 increment_matrix_positions (w->current_matrix,
17236 MATRIX_ROW_VPOS (r0, current_matrix),
17237 MATRIX_ROW_VPOS (r1, current_matrix),
17238 Z_delta, Z_delta_bytes);
17239 }
17240
17241 /* Set the cursor. */
17242 row = row_containing_pos (w, PT, r0, NULL, 0);
17243 if (row)
17244 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17245 else
17246 emacs_abort ();
17247 return 1;
17248 }
17249 }
17250
17251 /* Handle the case that changes are all below what is displayed in
17252 the window, and that PT is in the window. This shortcut cannot
17253 be taken if ZV is visible in the window, and text has been added
17254 there that is visible in the window. */
17255 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17256 /* ZV is not visible in the window, or there are no
17257 changes at ZV, actually. */
17258 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17259 || first_changed_charpos == last_changed_charpos))
17260 {
17261 struct glyph_row *r0;
17262
17263 /* Give up if PT is not in the window. Note that it already has
17264 been checked at the start of try_window_id that PT is not in
17265 front of the window start. */
17266 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17267 GIVE_UP (14);
17268
17269 /* If window start is unchanged, we can reuse the whole matrix
17270 as is, without changing glyph positions since no text has
17271 been added/removed in front of the window end. */
17272 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17273 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17274 /* PT must not be in a partially visible line. */
17275 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17276 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17277 {
17278 /* We have to compute the window end anew since text
17279 could have been added/removed after it. */
17280 wset_window_end_pos
17281 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17282 w->window_end_bytepos
17283 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17284
17285 /* Set the cursor. */
17286 row = row_containing_pos (w, PT, r0, NULL, 0);
17287 if (row)
17288 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17289 else
17290 emacs_abort ();
17291 return 2;
17292 }
17293 }
17294
17295 /* Give up if window start is in the changed area.
17296
17297 The condition used to read
17298
17299 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17300
17301 but why that was tested escapes me at the moment. */
17302 if (CHARPOS (start) >= first_changed_charpos
17303 && CHARPOS (start) <= last_changed_charpos)
17304 GIVE_UP (15);
17305
17306 /* Check that window start agrees with the start of the first glyph
17307 row in its current matrix. Check this after we know the window
17308 start is not in changed text, otherwise positions would not be
17309 comparable. */
17310 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17311 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17312 GIVE_UP (16);
17313
17314 /* Give up if the window ends in strings. Overlay strings
17315 at the end are difficult to handle, so don't try. */
17316 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17317 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17318 GIVE_UP (20);
17319
17320 /* Compute the position at which we have to start displaying new
17321 lines. Some of the lines at the top of the window might be
17322 reusable because they are not displaying changed text. Find the
17323 last row in W's current matrix not affected by changes at the
17324 start of current_buffer. Value is null if changes start in the
17325 first line of window. */
17326 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17327 if (last_unchanged_at_beg_row)
17328 {
17329 /* Avoid starting to display in the middle of a character, a TAB
17330 for instance. This is easier than to set up the iterator
17331 exactly, and it's not a frequent case, so the additional
17332 effort wouldn't really pay off. */
17333 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17334 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17335 && last_unchanged_at_beg_row > w->current_matrix->rows)
17336 --last_unchanged_at_beg_row;
17337
17338 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17339 GIVE_UP (17);
17340
17341 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17342 GIVE_UP (18);
17343 start_pos = it.current.pos;
17344
17345 /* Start displaying new lines in the desired matrix at the same
17346 vpos we would use in the current matrix, i.e. below
17347 last_unchanged_at_beg_row. */
17348 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17349 current_matrix);
17350 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17351 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17352
17353 eassert (it.hpos == 0 && it.current_x == 0);
17354 }
17355 else
17356 {
17357 /* There are no reusable lines at the start of the window.
17358 Start displaying in the first text line. */
17359 start_display (&it, w, start);
17360 it.vpos = it.first_vpos;
17361 start_pos = it.current.pos;
17362 }
17363
17364 /* Find the first row that is not affected by changes at the end of
17365 the buffer. Value will be null if there is no unchanged row, in
17366 which case we must redisplay to the end of the window. delta
17367 will be set to the value by which buffer positions beginning with
17368 first_unchanged_at_end_row have to be adjusted due to text
17369 changes. */
17370 first_unchanged_at_end_row
17371 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17372 IF_DEBUG (debug_delta = delta);
17373 IF_DEBUG (debug_delta_bytes = delta_bytes);
17374
17375 /* Set stop_pos to the buffer position up to which we will have to
17376 display new lines. If first_unchanged_at_end_row != NULL, this
17377 is the buffer position of the start of the line displayed in that
17378 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17379 that we don't stop at a buffer position. */
17380 stop_pos = 0;
17381 if (first_unchanged_at_end_row)
17382 {
17383 eassert (last_unchanged_at_beg_row == NULL
17384 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17385
17386 /* If this is a continuation line, move forward to the next one
17387 that isn't. Changes in lines above affect this line.
17388 Caution: this may move first_unchanged_at_end_row to a row
17389 not displaying text. */
17390 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17391 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17392 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17393 < it.last_visible_y))
17394 ++first_unchanged_at_end_row;
17395
17396 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17397 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17398 >= it.last_visible_y))
17399 first_unchanged_at_end_row = NULL;
17400 else
17401 {
17402 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17403 + delta);
17404 first_unchanged_at_end_vpos
17405 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17406 eassert (stop_pos >= Z - END_UNCHANGED);
17407 }
17408 }
17409 else if (last_unchanged_at_beg_row == NULL)
17410 GIVE_UP (19);
17411
17412
17413 #ifdef GLYPH_DEBUG
17414
17415 /* Either there is no unchanged row at the end, or the one we have
17416 now displays text. This is a necessary condition for the window
17417 end pos calculation at the end of this function. */
17418 eassert (first_unchanged_at_end_row == NULL
17419 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17420
17421 debug_last_unchanged_at_beg_vpos
17422 = (last_unchanged_at_beg_row
17423 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17424 : -1);
17425 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17426
17427 #endif /* GLYPH_DEBUG */
17428
17429
17430 /* Display new lines. Set last_text_row to the last new line
17431 displayed which has text on it, i.e. might end up as being the
17432 line where the window_end_vpos is. */
17433 w->cursor.vpos = -1;
17434 last_text_row = NULL;
17435 overlay_arrow_seen = 0;
17436 while (it.current_y < it.last_visible_y
17437 && !fonts_changed_p
17438 && (first_unchanged_at_end_row == NULL
17439 || IT_CHARPOS (it) < stop_pos))
17440 {
17441 if (display_line (&it))
17442 last_text_row = it.glyph_row - 1;
17443 }
17444
17445 if (fonts_changed_p)
17446 return -1;
17447
17448
17449 /* Compute differences in buffer positions, y-positions etc. for
17450 lines reused at the bottom of the window. Compute what we can
17451 scroll. */
17452 if (first_unchanged_at_end_row
17453 /* No lines reused because we displayed everything up to the
17454 bottom of the window. */
17455 && it.current_y < it.last_visible_y)
17456 {
17457 dvpos = (it.vpos
17458 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17459 current_matrix));
17460 dy = it.current_y - first_unchanged_at_end_row->y;
17461 run.current_y = first_unchanged_at_end_row->y;
17462 run.desired_y = run.current_y + dy;
17463 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17464 }
17465 else
17466 {
17467 delta = delta_bytes = dvpos = dy
17468 = run.current_y = run.desired_y = run.height = 0;
17469 first_unchanged_at_end_row = NULL;
17470 }
17471 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17472
17473
17474 /* Find the cursor if not already found. We have to decide whether
17475 PT will appear on this window (it sometimes doesn't, but this is
17476 not a very frequent case.) This decision has to be made before
17477 the current matrix is altered. A value of cursor.vpos < 0 means
17478 that PT is either in one of the lines beginning at
17479 first_unchanged_at_end_row or below the window. Don't care for
17480 lines that might be displayed later at the window end; as
17481 mentioned, this is not a frequent case. */
17482 if (w->cursor.vpos < 0)
17483 {
17484 /* Cursor in unchanged rows at the top? */
17485 if (PT < CHARPOS (start_pos)
17486 && last_unchanged_at_beg_row)
17487 {
17488 row = row_containing_pos (w, PT,
17489 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17490 last_unchanged_at_beg_row + 1, 0);
17491 if (row)
17492 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17493 }
17494
17495 /* Start from first_unchanged_at_end_row looking for PT. */
17496 else if (first_unchanged_at_end_row)
17497 {
17498 row = row_containing_pos (w, PT - delta,
17499 first_unchanged_at_end_row, NULL, 0);
17500 if (row)
17501 set_cursor_from_row (w, row, w->current_matrix, delta,
17502 delta_bytes, dy, dvpos);
17503 }
17504
17505 /* Give up if cursor was not found. */
17506 if (w->cursor.vpos < 0)
17507 {
17508 clear_glyph_matrix (w->desired_matrix);
17509 return -1;
17510 }
17511 }
17512
17513 /* Don't let the cursor end in the scroll margins. */
17514 {
17515 int this_scroll_margin, cursor_height;
17516
17517 this_scroll_margin =
17518 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17519 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17520 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17521
17522 if ((w->cursor.y < this_scroll_margin
17523 && CHARPOS (start) > BEGV)
17524 /* Old redisplay didn't take scroll margin into account at the bottom,
17525 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17526 || (w->cursor.y + (make_cursor_line_fully_visible_p
17527 ? cursor_height + this_scroll_margin
17528 : 1)) > it.last_visible_y)
17529 {
17530 w->cursor.vpos = -1;
17531 clear_glyph_matrix (w->desired_matrix);
17532 return -1;
17533 }
17534 }
17535
17536 /* Scroll the display. Do it before changing the current matrix so
17537 that xterm.c doesn't get confused about where the cursor glyph is
17538 found. */
17539 if (dy && run.height)
17540 {
17541 update_begin (f);
17542
17543 if (FRAME_WINDOW_P (f))
17544 {
17545 FRAME_RIF (f)->update_window_begin_hook (w);
17546 FRAME_RIF (f)->clear_window_mouse_face (w);
17547 FRAME_RIF (f)->scroll_run_hook (w, &run);
17548 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17549 }
17550 else
17551 {
17552 /* Terminal frame. In this case, dvpos gives the number of
17553 lines to scroll by; dvpos < 0 means scroll up. */
17554 int from_vpos
17555 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17556 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17557 int end = (WINDOW_TOP_EDGE_LINE (w)
17558 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17559 + window_internal_height (w));
17560
17561 #if defined (HAVE_GPM) || defined (MSDOS)
17562 x_clear_window_mouse_face (w);
17563 #endif
17564 /* Perform the operation on the screen. */
17565 if (dvpos > 0)
17566 {
17567 /* Scroll last_unchanged_at_beg_row to the end of the
17568 window down dvpos lines. */
17569 set_terminal_window (f, end);
17570
17571 /* On dumb terminals delete dvpos lines at the end
17572 before inserting dvpos empty lines. */
17573 if (!FRAME_SCROLL_REGION_OK (f))
17574 ins_del_lines (f, end - dvpos, -dvpos);
17575
17576 /* Insert dvpos empty lines in front of
17577 last_unchanged_at_beg_row. */
17578 ins_del_lines (f, from, dvpos);
17579 }
17580 else if (dvpos < 0)
17581 {
17582 /* Scroll up last_unchanged_at_beg_vpos to the end of
17583 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17584 set_terminal_window (f, end);
17585
17586 /* Delete dvpos lines in front of
17587 last_unchanged_at_beg_vpos. ins_del_lines will set
17588 the cursor to the given vpos and emit |dvpos| delete
17589 line sequences. */
17590 ins_del_lines (f, from + dvpos, dvpos);
17591
17592 /* On a dumb terminal insert dvpos empty lines at the
17593 end. */
17594 if (!FRAME_SCROLL_REGION_OK (f))
17595 ins_del_lines (f, end + dvpos, -dvpos);
17596 }
17597
17598 set_terminal_window (f, 0);
17599 }
17600
17601 update_end (f);
17602 }
17603
17604 /* Shift reused rows of the current matrix to the right position.
17605 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17606 text. */
17607 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17608 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17609 if (dvpos < 0)
17610 {
17611 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17612 bottom_vpos, dvpos);
17613 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17614 bottom_vpos);
17615 }
17616 else if (dvpos > 0)
17617 {
17618 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17619 bottom_vpos, dvpos);
17620 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17621 first_unchanged_at_end_vpos + dvpos);
17622 }
17623
17624 /* For frame-based redisplay, make sure that current frame and window
17625 matrix are in sync with respect to glyph memory. */
17626 if (!FRAME_WINDOW_P (f))
17627 sync_frame_with_window_matrix_rows (w);
17628
17629 /* Adjust buffer positions in reused rows. */
17630 if (delta || delta_bytes)
17631 increment_matrix_positions (current_matrix,
17632 first_unchanged_at_end_vpos + dvpos,
17633 bottom_vpos, delta, delta_bytes);
17634
17635 /* Adjust Y positions. */
17636 if (dy)
17637 shift_glyph_matrix (w, current_matrix,
17638 first_unchanged_at_end_vpos + dvpos,
17639 bottom_vpos, dy);
17640
17641 if (first_unchanged_at_end_row)
17642 {
17643 first_unchanged_at_end_row += dvpos;
17644 if (first_unchanged_at_end_row->y >= it.last_visible_y
17645 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17646 first_unchanged_at_end_row = NULL;
17647 }
17648
17649 /* If scrolling up, there may be some lines to display at the end of
17650 the window. */
17651 last_text_row_at_end = NULL;
17652 if (dy < 0)
17653 {
17654 /* Scrolling up can leave for example a partially visible line
17655 at the end of the window to be redisplayed. */
17656 /* Set last_row to the glyph row in the current matrix where the
17657 window end line is found. It has been moved up or down in
17658 the matrix by dvpos. */
17659 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17660 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17661
17662 /* If last_row is the window end line, it should display text. */
17663 eassert (last_row->displays_text_p);
17664
17665 /* If window end line was partially visible before, begin
17666 displaying at that line. Otherwise begin displaying with the
17667 line following it. */
17668 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17669 {
17670 init_to_row_start (&it, w, last_row);
17671 it.vpos = last_vpos;
17672 it.current_y = last_row->y;
17673 }
17674 else
17675 {
17676 init_to_row_end (&it, w, last_row);
17677 it.vpos = 1 + last_vpos;
17678 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17679 ++last_row;
17680 }
17681
17682 /* We may start in a continuation line. If so, we have to
17683 get the right continuation_lines_width and current_x. */
17684 it.continuation_lines_width = last_row->continuation_lines_width;
17685 it.hpos = it.current_x = 0;
17686
17687 /* Display the rest of the lines at the window end. */
17688 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17689 while (it.current_y < it.last_visible_y
17690 && !fonts_changed_p)
17691 {
17692 /* Is it always sure that the display agrees with lines in
17693 the current matrix? I don't think so, so we mark rows
17694 displayed invalid in the current matrix by setting their
17695 enabled_p flag to zero. */
17696 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17697 if (display_line (&it))
17698 last_text_row_at_end = it.glyph_row - 1;
17699 }
17700 }
17701
17702 /* Update window_end_pos and window_end_vpos. */
17703 if (first_unchanged_at_end_row
17704 && !last_text_row_at_end)
17705 {
17706 /* Window end line if one of the preserved rows from the current
17707 matrix. Set row to the last row displaying text in current
17708 matrix starting at first_unchanged_at_end_row, after
17709 scrolling. */
17710 eassert (first_unchanged_at_end_row->displays_text_p);
17711 row = find_last_row_displaying_text (w->current_matrix, &it,
17712 first_unchanged_at_end_row);
17713 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17714
17715 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17716 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17717 wset_window_end_vpos
17718 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17719 eassert (w->window_end_bytepos >= 0);
17720 IF_DEBUG (debug_method_add (w, "A"));
17721 }
17722 else if (last_text_row_at_end)
17723 {
17724 wset_window_end_pos
17725 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17726 w->window_end_bytepos
17727 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17728 wset_window_end_vpos
17729 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17730 desired_matrix)));
17731 eassert (w->window_end_bytepos >= 0);
17732 IF_DEBUG (debug_method_add (w, "B"));
17733 }
17734 else if (last_text_row)
17735 {
17736 /* We have displayed either to the end of the window or at the
17737 end of the window, i.e. the last row with text is to be found
17738 in the desired matrix. */
17739 wset_window_end_pos
17740 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17741 w->window_end_bytepos
17742 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17743 wset_window_end_vpos
17744 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17745 eassert (w->window_end_bytepos >= 0);
17746 }
17747 else if (first_unchanged_at_end_row == NULL
17748 && last_text_row == NULL
17749 && last_text_row_at_end == NULL)
17750 {
17751 /* Displayed to end of window, but no line containing text was
17752 displayed. Lines were deleted at the end of the window. */
17753 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17754 int vpos = XFASTINT (w->window_end_vpos);
17755 struct glyph_row *current_row = current_matrix->rows + vpos;
17756 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17757
17758 for (row = NULL;
17759 row == NULL && vpos >= first_vpos;
17760 --vpos, --current_row, --desired_row)
17761 {
17762 if (desired_row->enabled_p)
17763 {
17764 if (desired_row->displays_text_p)
17765 row = desired_row;
17766 }
17767 else if (current_row->displays_text_p)
17768 row = current_row;
17769 }
17770
17771 eassert (row != NULL);
17772 wset_window_end_vpos (w, make_number (vpos + 1));
17773 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17774 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17775 eassert (w->window_end_bytepos >= 0);
17776 IF_DEBUG (debug_method_add (w, "C"));
17777 }
17778 else
17779 emacs_abort ();
17780
17781 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17782 debug_end_vpos = XFASTINT (w->window_end_vpos));
17783
17784 /* Record that display has not been completed. */
17785 w->window_end_valid = 0;
17786 w->desired_matrix->no_scrolling_p = 1;
17787 return 3;
17788
17789 #undef GIVE_UP
17790 }
17791
17792
17793 \f
17794 /***********************************************************************
17795 More debugging support
17796 ***********************************************************************/
17797
17798 #ifdef GLYPH_DEBUG
17799
17800 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17801 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17802 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17803
17804
17805 /* Dump the contents of glyph matrix MATRIX on stderr.
17806
17807 GLYPHS 0 means don't show glyph contents.
17808 GLYPHS 1 means show glyphs in short form
17809 GLYPHS > 1 means show glyphs in long form. */
17810
17811 void
17812 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17813 {
17814 int i;
17815 for (i = 0; i < matrix->nrows; ++i)
17816 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17817 }
17818
17819
17820 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17821 the glyph row and area where the glyph comes from. */
17822
17823 void
17824 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17825 {
17826 if (glyph->type == CHAR_GLYPH
17827 || glyph->type == GLYPHLESS_GLYPH)
17828 {
17829 fprintf (stderr,
17830 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17831 glyph - row->glyphs[TEXT_AREA],
17832 (glyph->type == CHAR_GLYPH
17833 ? 'C'
17834 : 'G'),
17835 glyph->charpos,
17836 (BUFFERP (glyph->object)
17837 ? 'B'
17838 : (STRINGP (glyph->object)
17839 ? 'S'
17840 : (INTEGERP (glyph->object)
17841 ? '0'
17842 : '-'))),
17843 glyph->pixel_width,
17844 glyph->u.ch,
17845 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17846 ? glyph->u.ch
17847 : '.'),
17848 glyph->face_id,
17849 glyph->left_box_line_p,
17850 glyph->right_box_line_p);
17851 }
17852 else if (glyph->type == STRETCH_GLYPH)
17853 {
17854 fprintf (stderr,
17855 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17856 glyph - row->glyphs[TEXT_AREA],
17857 'S',
17858 glyph->charpos,
17859 (BUFFERP (glyph->object)
17860 ? 'B'
17861 : (STRINGP (glyph->object)
17862 ? 'S'
17863 : (INTEGERP (glyph->object)
17864 ? '0'
17865 : '-'))),
17866 glyph->pixel_width,
17867 0,
17868 ' ',
17869 glyph->face_id,
17870 glyph->left_box_line_p,
17871 glyph->right_box_line_p);
17872 }
17873 else if (glyph->type == IMAGE_GLYPH)
17874 {
17875 fprintf (stderr,
17876 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17877 glyph - row->glyphs[TEXT_AREA],
17878 'I',
17879 glyph->charpos,
17880 (BUFFERP (glyph->object)
17881 ? 'B'
17882 : (STRINGP (glyph->object)
17883 ? 'S'
17884 : (INTEGERP (glyph->object)
17885 ? '0'
17886 : '-'))),
17887 glyph->pixel_width,
17888 glyph->u.img_id,
17889 '.',
17890 glyph->face_id,
17891 glyph->left_box_line_p,
17892 glyph->right_box_line_p);
17893 }
17894 else if (glyph->type == COMPOSITE_GLYPH)
17895 {
17896 fprintf (stderr,
17897 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17898 glyph - row->glyphs[TEXT_AREA],
17899 '+',
17900 glyph->charpos,
17901 (BUFFERP (glyph->object)
17902 ? 'B'
17903 : (STRINGP (glyph->object)
17904 ? 'S'
17905 : (INTEGERP (glyph->object)
17906 ? '0'
17907 : '-'))),
17908 glyph->pixel_width,
17909 glyph->u.cmp.id);
17910 if (glyph->u.cmp.automatic)
17911 fprintf (stderr,
17912 "[%d-%d]",
17913 glyph->slice.cmp.from, glyph->slice.cmp.to);
17914 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17915 glyph->face_id,
17916 glyph->left_box_line_p,
17917 glyph->right_box_line_p);
17918 }
17919 #ifdef HAVE_XWIDGETS
17920 else if (glyph->type == XWIDGET_GLYPH)
17921 {
17922 fprintf (stderr,
17923 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17924 glyph - row->glyphs[TEXT_AREA],
17925 'X',
17926 glyph->charpos,
17927 (BUFFERP (glyph->object)
17928 ? 'B'
17929 : (STRINGP (glyph->object)
17930 ? 'S'
17931 : '-')),
17932 glyph->pixel_width,
17933 glyph->u.xwidget,
17934 '.',
17935 glyph->face_id,
17936 glyph->left_box_line_p,
17937 glyph->right_box_line_p);
17938
17939 // printf("dump xwidget glyph\n");
17940 }
17941 #endif
17942 }
17943
17944
17945 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17946 GLYPHS 0 means don't show glyph contents.
17947 GLYPHS 1 means show glyphs in short form
17948 GLYPHS > 1 means show glyphs in long form. */
17949
17950 void
17951 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17952 {
17953 if (glyphs != 1)
17954 {
17955 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17956 fprintf (stderr, "==============================================================================\n");
17957
17958 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17959 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17960 vpos,
17961 MATRIX_ROW_START_CHARPOS (row),
17962 MATRIX_ROW_END_CHARPOS (row),
17963 row->used[TEXT_AREA],
17964 row->contains_overlapping_glyphs_p,
17965 row->enabled_p,
17966 row->truncated_on_left_p,
17967 row->truncated_on_right_p,
17968 row->continued_p,
17969 MATRIX_ROW_CONTINUATION_LINE_P (row),
17970 row->displays_text_p,
17971 row->ends_at_zv_p,
17972 row->fill_line_p,
17973 row->ends_in_middle_of_char_p,
17974 row->starts_in_middle_of_char_p,
17975 row->mouse_face_p,
17976 row->x,
17977 row->y,
17978 row->pixel_width,
17979 row->height,
17980 row->visible_height,
17981 row->ascent,
17982 row->phys_ascent);
17983 /* The next 3 lines should align to "Start" in the header. */
17984 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17985 row->end.overlay_string_index,
17986 row->continuation_lines_width);
17987 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17988 CHARPOS (row->start.string_pos),
17989 CHARPOS (row->end.string_pos));
17990 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17991 row->end.dpvec_index);
17992 }
17993
17994 if (glyphs > 1)
17995 {
17996 int area;
17997
17998 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17999 {
18000 struct glyph *glyph = row->glyphs[area];
18001 struct glyph *glyph_end = glyph + row->used[area];
18002
18003 /* Glyph for a line end in text. */
18004 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18005 ++glyph_end;
18006
18007 if (glyph < glyph_end)
18008 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18009
18010 for (; glyph < glyph_end; ++glyph)
18011 dump_glyph (row, glyph, area);
18012 }
18013 }
18014 else if (glyphs == 1)
18015 {
18016 int area;
18017
18018 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18019 {
18020 char *s = alloca (row->used[area] + 4);
18021 int i;
18022
18023 for (i = 0; i < row->used[area]; ++i)
18024 {
18025 struct glyph *glyph = row->glyphs[area] + i;
18026 if (i == row->used[area] - 1
18027 && area == TEXT_AREA
18028 && INTEGERP (glyph->object)
18029 && glyph->type == CHAR_GLYPH
18030 && glyph->u.ch == ' ')
18031 {
18032 strcpy (&s[i], "[\\n]");
18033 i += 4;
18034 }
18035 else if (glyph->type == CHAR_GLYPH
18036 && glyph->u.ch < 0x80
18037 && glyph->u.ch >= ' ')
18038 s[i] = glyph->u.ch;
18039 else
18040 s[i] = '.';
18041 }
18042
18043 s[i] = '\0';
18044 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18045 }
18046 }
18047 }
18048
18049
18050 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18051 Sdump_glyph_matrix, 0, 1, "p",
18052 doc: /* Dump the current matrix of the selected window to stderr.
18053 Shows contents of glyph row structures. With non-nil
18054 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18055 glyphs in short form, otherwise show glyphs in long form. */)
18056 (Lisp_Object glyphs)
18057 {
18058 struct window *w = XWINDOW (selected_window);
18059 struct buffer *buffer = XBUFFER (w->buffer);
18060
18061 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18062 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18063 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18064 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18065 fprintf (stderr, "=============================================\n");
18066 dump_glyph_matrix (w->current_matrix,
18067 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18068 return Qnil;
18069 }
18070
18071
18072 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18073 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18074 (void)
18075 {
18076 struct frame *f = XFRAME (selected_frame);
18077 dump_glyph_matrix (f->current_matrix, 1);
18078 return Qnil;
18079 }
18080
18081
18082 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18083 doc: /* Dump glyph row ROW to stderr.
18084 GLYPH 0 means don't dump glyphs.
18085 GLYPH 1 means dump glyphs in short form.
18086 GLYPH > 1 or omitted means dump glyphs in long form. */)
18087 (Lisp_Object row, Lisp_Object glyphs)
18088 {
18089 struct glyph_matrix *matrix;
18090 EMACS_INT vpos;
18091
18092 CHECK_NUMBER (row);
18093 matrix = XWINDOW (selected_window)->current_matrix;
18094 vpos = XINT (row);
18095 if (vpos >= 0 && vpos < matrix->nrows)
18096 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18097 vpos,
18098 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18099 return Qnil;
18100 }
18101
18102
18103 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18104 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18105 GLYPH 0 means don't dump glyphs.
18106 GLYPH 1 means dump glyphs in short form.
18107 GLYPH > 1 or omitted means dump glyphs in long form. */)
18108 (Lisp_Object row, Lisp_Object glyphs)
18109 {
18110 struct frame *sf = SELECTED_FRAME ();
18111 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18112 EMACS_INT vpos;
18113
18114 CHECK_NUMBER (row);
18115 vpos = XINT (row);
18116 if (vpos >= 0 && vpos < m->nrows)
18117 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18118 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18119 return Qnil;
18120 }
18121
18122
18123 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18124 doc: /* Toggle tracing of redisplay.
18125 With ARG, turn tracing on if and only if ARG is positive. */)
18126 (Lisp_Object arg)
18127 {
18128 if (NILP (arg))
18129 trace_redisplay_p = !trace_redisplay_p;
18130 else
18131 {
18132 arg = Fprefix_numeric_value (arg);
18133 trace_redisplay_p = XINT (arg) > 0;
18134 }
18135
18136 return Qnil;
18137 }
18138
18139
18140 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18141 doc: /* Like `format', but print result to stderr.
18142 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18143 (ptrdiff_t nargs, Lisp_Object *args)
18144 {
18145 Lisp_Object s = Fformat (nargs, args);
18146 fprintf (stderr, "%s", SDATA (s));
18147 return Qnil;
18148 }
18149
18150 #endif /* GLYPH_DEBUG */
18151
18152
18153 \f
18154 /***********************************************************************
18155 Building Desired Matrix Rows
18156 ***********************************************************************/
18157
18158 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18159 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18160
18161 static struct glyph_row *
18162 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18163 {
18164 struct frame *f = XFRAME (WINDOW_FRAME (w));
18165 struct buffer *buffer = XBUFFER (w->buffer);
18166 struct buffer *old = current_buffer;
18167 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18168 int arrow_len = SCHARS (overlay_arrow_string);
18169 const unsigned char *arrow_end = arrow_string + arrow_len;
18170 const unsigned char *p;
18171 struct it it;
18172 bool multibyte_p;
18173 int n_glyphs_before;
18174
18175 set_buffer_temp (buffer);
18176 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18177 it.glyph_row->used[TEXT_AREA] = 0;
18178 SET_TEXT_POS (it.position, 0, 0);
18179
18180 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18181 p = arrow_string;
18182 while (p < arrow_end)
18183 {
18184 Lisp_Object face, ilisp;
18185
18186 /* Get the next character. */
18187 if (multibyte_p)
18188 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18189 else
18190 {
18191 it.c = it.char_to_display = *p, it.len = 1;
18192 if (! ASCII_CHAR_P (it.c))
18193 it.char_to_display = BYTE8_TO_CHAR (it.c);
18194 }
18195 p += it.len;
18196
18197 /* Get its face. */
18198 ilisp = make_number (p - arrow_string);
18199 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18200 it.face_id = compute_char_face (f, it.char_to_display, face);
18201
18202 /* Compute its width, get its glyphs. */
18203 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18204 SET_TEXT_POS (it.position, -1, -1);
18205 PRODUCE_GLYPHS (&it);
18206
18207 /* If this character doesn't fit any more in the line, we have
18208 to remove some glyphs. */
18209 if (it.current_x > it.last_visible_x)
18210 {
18211 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18212 break;
18213 }
18214 }
18215
18216 set_buffer_temp (old);
18217 return it.glyph_row;
18218 }
18219
18220
18221 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18222 glyphs to insert is determined by produce_special_glyphs. */
18223
18224 static void
18225 insert_left_trunc_glyphs (struct it *it)
18226 {
18227 struct it truncate_it;
18228 struct glyph *from, *end, *to, *toend;
18229
18230 eassert (!FRAME_WINDOW_P (it->f)
18231 || (!it->glyph_row->reversed_p
18232 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18233 || (it->glyph_row->reversed_p
18234 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18235
18236 /* Get the truncation glyphs. */
18237 truncate_it = *it;
18238 truncate_it.current_x = 0;
18239 truncate_it.face_id = DEFAULT_FACE_ID;
18240 truncate_it.glyph_row = &scratch_glyph_row;
18241 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18242 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18243 truncate_it.object = make_number (0);
18244 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18245
18246 /* Overwrite glyphs from IT with truncation glyphs. */
18247 if (!it->glyph_row->reversed_p)
18248 {
18249 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18250
18251 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18252 end = from + tused;
18253 to = it->glyph_row->glyphs[TEXT_AREA];
18254 toend = to + it->glyph_row->used[TEXT_AREA];
18255 if (FRAME_WINDOW_P (it->f))
18256 {
18257 /* On GUI frames, when variable-size fonts are displayed,
18258 the truncation glyphs may need more pixels than the row's
18259 glyphs they overwrite. We overwrite more glyphs to free
18260 enough screen real estate, and enlarge the stretch glyph
18261 on the right (see display_line), if there is one, to
18262 preserve the screen position of the truncation glyphs on
18263 the right. */
18264 int w = 0;
18265 struct glyph *g = to;
18266 short used;
18267
18268 /* The first glyph could be partially visible, in which case
18269 it->glyph_row->x will be negative. But we want the left
18270 truncation glyphs to be aligned at the left margin of the
18271 window, so we override the x coordinate at which the row
18272 will begin. */
18273 it->glyph_row->x = 0;
18274 while (g < toend && w < it->truncation_pixel_width)
18275 {
18276 w += g->pixel_width;
18277 ++g;
18278 }
18279 if (g - to - tused > 0)
18280 {
18281 memmove (to + tused, g, (toend - g) * sizeof(*g));
18282 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18283 }
18284 used = it->glyph_row->used[TEXT_AREA];
18285 if (it->glyph_row->truncated_on_right_p
18286 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18287 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18288 == STRETCH_GLYPH)
18289 {
18290 int extra = w - it->truncation_pixel_width;
18291
18292 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18293 }
18294 }
18295
18296 while (from < end)
18297 *to++ = *from++;
18298
18299 /* There may be padding glyphs left over. Overwrite them too. */
18300 if (!FRAME_WINDOW_P (it->f))
18301 {
18302 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18303 {
18304 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18305 while (from < end)
18306 *to++ = *from++;
18307 }
18308 }
18309
18310 if (to > toend)
18311 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18312 }
18313 else
18314 {
18315 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18316
18317 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18318 that back to front. */
18319 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18320 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18321 toend = it->glyph_row->glyphs[TEXT_AREA];
18322 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18323 if (FRAME_WINDOW_P (it->f))
18324 {
18325 int w = 0;
18326 struct glyph *g = to;
18327
18328 while (g >= toend && w < it->truncation_pixel_width)
18329 {
18330 w += g->pixel_width;
18331 --g;
18332 }
18333 if (to - g - tused > 0)
18334 to = g + tused;
18335 if (it->glyph_row->truncated_on_right_p
18336 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18337 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18338 {
18339 int extra = w - it->truncation_pixel_width;
18340
18341 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18342 }
18343 }
18344
18345 while (from >= end && to >= toend)
18346 *to-- = *from--;
18347 if (!FRAME_WINDOW_P (it->f))
18348 {
18349 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18350 {
18351 from =
18352 truncate_it.glyph_row->glyphs[TEXT_AREA]
18353 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18354 while (from >= end && to >= toend)
18355 *to-- = *from--;
18356 }
18357 }
18358 if (from >= end)
18359 {
18360 /* Need to free some room before prepending additional
18361 glyphs. */
18362 int move_by = from - end + 1;
18363 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18364 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18365
18366 for ( ; g >= g0; g--)
18367 g[move_by] = *g;
18368 while (from >= end)
18369 *to-- = *from--;
18370 it->glyph_row->used[TEXT_AREA] += move_by;
18371 }
18372 }
18373 }
18374
18375 /* Compute the hash code for ROW. */
18376 unsigned
18377 row_hash (struct glyph_row *row)
18378 {
18379 int area, k;
18380 unsigned hashval = 0;
18381
18382 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18383 for (k = 0; k < row->used[area]; ++k)
18384 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18385 + row->glyphs[area][k].u.val
18386 + row->glyphs[area][k].face_id
18387 + row->glyphs[area][k].padding_p
18388 + (row->glyphs[area][k].type << 2));
18389
18390 return hashval;
18391 }
18392
18393 /* Compute the pixel height and width of IT->glyph_row.
18394
18395 Most of the time, ascent and height of a display line will be equal
18396 to the max_ascent and max_height values of the display iterator
18397 structure. This is not the case if
18398
18399 1. We hit ZV without displaying anything. In this case, max_ascent
18400 and max_height will be zero.
18401
18402 2. We have some glyphs that don't contribute to the line height.
18403 (The glyph row flag contributes_to_line_height_p is for future
18404 pixmap extensions).
18405
18406 The first case is easily covered by using default values because in
18407 these cases, the line height does not really matter, except that it
18408 must not be zero. */
18409
18410 static void
18411 compute_line_metrics (struct it *it)
18412 {
18413 struct glyph_row *row = it->glyph_row;
18414
18415 if (FRAME_WINDOW_P (it->f))
18416 {
18417 int i, min_y, max_y;
18418
18419 /* The line may consist of one space only, that was added to
18420 place the cursor on it. If so, the row's height hasn't been
18421 computed yet. */
18422 if (row->height == 0)
18423 {
18424 if (it->max_ascent + it->max_descent == 0)
18425 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18426 row->ascent = it->max_ascent;
18427 row->height = it->max_ascent + it->max_descent;
18428 row->phys_ascent = it->max_phys_ascent;
18429 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18430 row->extra_line_spacing = it->max_extra_line_spacing;
18431 }
18432
18433 /* Compute the width of this line. */
18434 row->pixel_width = row->x;
18435 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18436 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18437
18438 eassert (row->pixel_width >= 0);
18439 eassert (row->ascent >= 0 && row->height > 0);
18440
18441 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18442 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18443
18444 /* If first line's physical ascent is larger than its logical
18445 ascent, use the physical ascent, and make the row taller.
18446 This makes accented characters fully visible. */
18447 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18448 && row->phys_ascent > row->ascent)
18449 {
18450 row->height += row->phys_ascent - row->ascent;
18451 row->ascent = row->phys_ascent;
18452 }
18453
18454 /* Compute how much of the line is visible. */
18455 row->visible_height = row->height;
18456
18457 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18458 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18459
18460 if (row->y < min_y)
18461 row->visible_height -= min_y - row->y;
18462 if (row->y + row->height > max_y)
18463 row->visible_height -= row->y + row->height - max_y;
18464 }
18465 else
18466 {
18467 row->pixel_width = row->used[TEXT_AREA];
18468 if (row->continued_p)
18469 row->pixel_width -= it->continuation_pixel_width;
18470 else if (row->truncated_on_right_p)
18471 row->pixel_width -= it->truncation_pixel_width;
18472 row->ascent = row->phys_ascent = 0;
18473 row->height = row->phys_height = row->visible_height = 1;
18474 row->extra_line_spacing = 0;
18475 }
18476
18477 /* Compute a hash code for this row. */
18478 row->hash = row_hash (row);
18479
18480 it->max_ascent = it->max_descent = 0;
18481 it->max_phys_ascent = it->max_phys_descent = 0;
18482 }
18483
18484
18485 /* Append one space to the glyph row of iterator IT if doing a
18486 window-based redisplay. The space has the same face as
18487 IT->face_id. Value is non-zero if a space was added.
18488
18489 This function is called to make sure that there is always one glyph
18490 at the end of a glyph row that the cursor can be set on under
18491 window-systems. (If there weren't such a glyph we would not know
18492 how wide and tall a box cursor should be displayed).
18493
18494 At the same time this space let's a nicely handle clearing to the
18495 end of the line if the row ends in italic text. */
18496
18497 static int
18498 append_space_for_newline (struct it *it, int default_face_p)
18499 {
18500 if (FRAME_WINDOW_P (it->f))
18501 {
18502 int n = it->glyph_row->used[TEXT_AREA];
18503
18504 if (it->glyph_row->glyphs[TEXT_AREA] + n
18505 < it->glyph_row->glyphs[1 + TEXT_AREA])
18506 {
18507 /* Save some values that must not be changed.
18508 Must save IT->c and IT->len because otherwise
18509 ITERATOR_AT_END_P wouldn't work anymore after
18510 append_space_for_newline has been called. */
18511 enum display_element_type saved_what = it->what;
18512 int saved_c = it->c, saved_len = it->len;
18513 int saved_char_to_display = it->char_to_display;
18514 int saved_x = it->current_x;
18515 int saved_face_id = it->face_id;
18516 int saved_box_end = it->end_of_box_run_p;
18517 struct text_pos saved_pos;
18518 Lisp_Object saved_object;
18519 struct face *face;
18520
18521 saved_object = it->object;
18522 saved_pos = it->position;
18523
18524 it->what = IT_CHARACTER;
18525 memset (&it->position, 0, sizeof it->position);
18526 it->object = make_number (0);
18527 it->c = it->char_to_display = ' ';
18528 it->len = 1;
18529
18530 /* If the default face was remapped, be sure to use the
18531 remapped face for the appended newline. */
18532 if (default_face_p)
18533 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18534 else if (it->face_before_selective_p)
18535 it->face_id = it->saved_face_id;
18536 face = FACE_FROM_ID (it->f, it->face_id);
18537 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18538 /* In R2L rows, we will prepend a stretch glyph that will
18539 have the end_of_box_run_p flag set for it, so there's no
18540 need for the appended newline glyph to have that flag
18541 set. */
18542 if (it->glyph_row->reversed_p
18543 /* But if the appended newline glyph goes all the way to
18544 the end of the row, there will be no stretch glyph,
18545 so leave the box flag set. */
18546 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18547 it->end_of_box_run_p = 0;
18548
18549 PRODUCE_GLYPHS (it);
18550
18551 it->override_ascent = -1;
18552 it->constrain_row_ascent_descent_p = 0;
18553 it->current_x = saved_x;
18554 it->object = saved_object;
18555 it->position = saved_pos;
18556 it->what = saved_what;
18557 it->face_id = saved_face_id;
18558 it->len = saved_len;
18559 it->c = saved_c;
18560 it->char_to_display = saved_char_to_display;
18561 it->end_of_box_run_p = saved_box_end;
18562 return 1;
18563 }
18564 }
18565
18566 return 0;
18567 }
18568
18569
18570 /* Extend the face of the last glyph in the text area of IT->glyph_row
18571 to the end of the display line. Called from display_line. If the
18572 glyph row is empty, add a space glyph to it so that we know the
18573 face to draw. Set the glyph row flag fill_line_p. If the glyph
18574 row is R2L, prepend a stretch glyph to cover the empty space to the
18575 left of the leftmost glyph. */
18576
18577 static void
18578 extend_face_to_end_of_line (struct it *it)
18579 {
18580 struct face *face, *default_face;
18581 struct frame *f = it->f;
18582
18583 /* If line is already filled, do nothing. Non window-system frames
18584 get a grace of one more ``pixel'' because their characters are
18585 1-``pixel'' wide, so they hit the equality too early. This grace
18586 is needed only for R2L rows that are not continued, to produce
18587 one extra blank where we could display the cursor. */
18588 if (it->current_x >= it->last_visible_x
18589 + (!FRAME_WINDOW_P (f)
18590 && it->glyph_row->reversed_p
18591 && !it->glyph_row->continued_p))
18592 return;
18593
18594 /* The default face, possibly remapped. */
18595 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18596
18597 /* Face extension extends the background and box of IT->face_id
18598 to the end of the line. If the background equals the background
18599 of the frame, we don't have to do anything. */
18600 if (it->face_before_selective_p)
18601 face = FACE_FROM_ID (f, it->saved_face_id);
18602 else
18603 face = FACE_FROM_ID (f, it->face_id);
18604
18605 if (FRAME_WINDOW_P (f)
18606 && it->glyph_row->displays_text_p
18607 && face->box == FACE_NO_BOX
18608 && face->background == FRAME_BACKGROUND_PIXEL (f)
18609 && !face->stipple
18610 && !it->glyph_row->reversed_p)
18611 return;
18612
18613 /* Set the glyph row flag indicating that the face of the last glyph
18614 in the text area has to be drawn to the end of the text area. */
18615 it->glyph_row->fill_line_p = 1;
18616
18617 /* If current character of IT is not ASCII, make sure we have the
18618 ASCII face. This will be automatically undone the next time
18619 get_next_display_element returns a multibyte character. Note
18620 that the character will always be single byte in unibyte
18621 text. */
18622 if (!ASCII_CHAR_P (it->c))
18623 {
18624 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18625 }
18626
18627 if (FRAME_WINDOW_P (f))
18628 {
18629 /* If the row is empty, add a space with the current face of IT,
18630 so that we know which face to draw. */
18631 if (it->glyph_row->used[TEXT_AREA] == 0)
18632 {
18633 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18634 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18635 it->glyph_row->used[TEXT_AREA] = 1;
18636 }
18637 #ifdef HAVE_WINDOW_SYSTEM
18638 if (it->glyph_row->reversed_p)
18639 {
18640 /* Prepend a stretch glyph to the row, such that the
18641 rightmost glyph will be drawn flushed all the way to the
18642 right margin of the window. The stretch glyph that will
18643 occupy the empty space, if any, to the left of the
18644 glyphs. */
18645 struct font *font = face->font ? face->font : FRAME_FONT (f);
18646 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18647 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18648 struct glyph *g;
18649 int row_width, stretch_ascent, stretch_width;
18650 struct text_pos saved_pos;
18651 int saved_face_id, saved_avoid_cursor, saved_box_start;
18652
18653 for (row_width = 0, g = row_start; g < row_end; g++)
18654 row_width += g->pixel_width;
18655 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18656 if (stretch_width > 0)
18657 {
18658 stretch_ascent =
18659 (((it->ascent + it->descent)
18660 * FONT_BASE (font)) / FONT_HEIGHT (font));
18661 saved_pos = it->position;
18662 memset (&it->position, 0, sizeof it->position);
18663 saved_avoid_cursor = it->avoid_cursor_p;
18664 it->avoid_cursor_p = 1;
18665 saved_face_id = it->face_id;
18666 saved_box_start = it->start_of_box_run_p;
18667 /* The last row's stretch glyph should get the default
18668 face, to avoid painting the rest of the window with
18669 the region face, if the region ends at ZV. */
18670 if (it->glyph_row->ends_at_zv_p)
18671 it->face_id = default_face->id;
18672 else
18673 it->face_id = face->id;
18674 it->start_of_box_run_p = 0;
18675 append_stretch_glyph (it, make_number (0), stretch_width,
18676 it->ascent + it->descent, stretch_ascent);
18677 it->position = saved_pos;
18678 it->avoid_cursor_p = saved_avoid_cursor;
18679 it->face_id = saved_face_id;
18680 it->start_of_box_run_p = saved_box_start;
18681 }
18682 }
18683 #endif /* HAVE_WINDOW_SYSTEM */
18684 }
18685 else
18686 {
18687 /* Save some values that must not be changed. */
18688 int saved_x = it->current_x;
18689 struct text_pos saved_pos;
18690 Lisp_Object saved_object;
18691 enum display_element_type saved_what = it->what;
18692 int saved_face_id = it->face_id;
18693
18694 saved_object = it->object;
18695 saved_pos = it->position;
18696
18697 it->what = IT_CHARACTER;
18698 memset (&it->position, 0, sizeof it->position);
18699 it->object = make_number (0);
18700 it->c = it->char_to_display = ' ';
18701 it->len = 1;
18702 /* The last row's blank glyphs should get the default face, to
18703 avoid painting the rest of the window with the region face,
18704 if the region ends at ZV. */
18705 if (it->glyph_row->ends_at_zv_p)
18706 it->face_id = default_face->id;
18707 else
18708 it->face_id = face->id;
18709
18710 PRODUCE_GLYPHS (it);
18711
18712 while (it->current_x <= it->last_visible_x)
18713 PRODUCE_GLYPHS (it);
18714
18715 /* Don't count these blanks really. It would let us insert a left
18716 truncation glyph below and make us set the cursor on them, maybe. */
18717 it->current_x = saved_x;
18718 it->object = saved_object;
18719 it->position = saved_pos;
18720 it->what = saved_what;
18721 it->face_id = saved_face_id;
18722 }
18723 }
18724
18725
18726 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18727 trailing whitespace. */
18728
18729 static int
18730 trailing_whitespace_p (ptrdiff_t charpos)
18731 {
18732 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18733 int c = 0;
18734
18735 while (bytepos < ZV_BYTE
18736 && (c = FETCH_CHAR (bytepos),
18737 c == ' ' || c == '\t'))
18738 ++bytepos;
18739
18740 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18741 {
18742 if (bytepos != PT_BYTE)
18743 return 1;
18744 }
18745 return 0;
18746 }
18747
18748
18749 /* Highlight trailing whitespace, if any, in ROW. */
18750
18751 static void
18752 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18753 {
18754 int used = row->used[TEXT_AREA];
18755
18756 if (used)
18757 {
18758 struct glyph *start = row->glyphs[TEXT_AREA];
18759 struct glyph *glyph = start + used - 1;
18760
18761 if (row->reversed_p)
18762 {
18763 /* Right-to-left rows need to be processed in the opposite
18764 direction, so swap the edge pointers. */
18765 glyph = start;
18766 start = row->glyphs[TEXT_AREA] + used - 1;
18767 }
18768
18769 /* Skip over glyphs inserted to display the cursor at the
18770 end of a line, for extending the face of the last glyph
18771 to the end of the line on terminals, and for truncation
18772 and continuation glyphs. */
18773 if (!row->reversed_p)
18774 {
18775 while (glyph >= start
18776 && glyph->type == CHAR_GLYPH
18777 && INTEGERP (glyph->object))
18778 --glyph;
18779 }
18780 else
18781 {
18782 while (glyph <= start
18783 && glyph->type == CHAR_GLYPH
18784 && INTEGERP (glyph->object))
18785 ++glyph;
18786 }
18787
18788 /* If last glyph is a space or stretch, and it's trailing
18789 whitespace, set the face of all trailing whitespace glyphs in
18790 IT->glyph_row to `trailing-whitespace'. */
18791 if ((row->reversed_p ? glyph <= start : glyph >= start)
18792 && BUFFERP (glyph->object)
18793 && (glyph->type == STRETCH_GLYPH
18794 || (glyph->type == CHAR_GLYPH
18795 && glyph->u.ch == ' '))
18796 && trailing_whitespace_p (glyph->charpos))
18797 {
18798 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18799 if (face_id < 0)
18800 return;
18801
18802 if (!row->reversed_p)
18803 {
18804 while (glyph >= start
18805 && BUFFERP (glyph->object)
18806 && (glyph->type == STRETCH_GLYPH
18807 || (glyph->type == CHAR_GLYPH
18808 && glyph->u.ch == ' ')))
18809 (glyph--)->face_id = face_id;
18810 }
18811 else
18812 {
18813 while (glyph <= start
18814 && BUFFERP (glyph->object)
18815 && (glyph->type == STRETCH_GLYPH
18816 || (glyph->type == CHAR_GLYPH
18817 && glyph->u.ch == ' ')))
18818 (glyph++)->face_id = face_id;
18819 }
18820 }
18821 }
18822 }
18823
18824
18825 /* Value is non-zero if glyph row ROW should be
18826 used to hold the cursor. */
18827
18828 static int
18829 cursor_row_p (struct glyph_row *row)
18830 {
18831 int result = 1;
18832
18833 if (PT == CHARPOS (row->end.pos)
18834 || PT == MATRIX_ROW_END_CHARPOS (row))
18835 {
18836 /* Suppose the row ends on a string.
18837 Unless the row is continued, that means it ends on a newline
18838 in the string. If it's anything other than a display string
18839 (e.g., a before-string from an overlay), we don't want the
18840 cursor there. (This heuristic seems to give the optimal
18841 behavior for the various types of multi-line strings.)
18842 One exception: if the string has `cursor' property on one of
18843 its characters, we _do_ want the cursor there. */
18844 if (CHARPOS (row->end.string_pos) >= 0)
18845 {
18846 if (row->continued_p)
18847 result = 1;
18848 else
18849 {
18850 /* Check for `display' property. */
18851 struct glyph *beg = row->glyphs[TEXT_AREA];
18852 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18853 struct glyph *glyph;
18854
18855 result = 0;
18856 for (glyph = end; glyph >= beg; --glyph)
18857 if (STRINGP (glyph->object))
18858 {
18859 Lisp_Object prop
18860 = Fget_char_property (make_number (PT),
18861 Qdisplay, Qnil);
18862 result =
18863 (!NILP (prop)
18864 && display_prop_string_p (prop, glyph->object));
18865 /* If there's a `cursor' property on one of the
18866 string's characters, this row is a cursor row,
18867 even though this is not a display string. */
18868 if (!result)
18869 {
18870 Lisp_Object s = glyph->object;
18871
18872 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18873 {
18874 ptrdiff_t gpos = glyph->charpos;
18875
18876 if (!NILP (Fget_char_property (make_number (gpos),
18877 Qcursor, s)))
18878 {
18879 result = 1;
18880 break;
18881 }
18882 }
18883 }
18884 break;
18885 }
18886 }
18887 }
18888 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18889 {
18890 /* If the row ends in middle of a real character,
18891 and the line is continued, we want the cursor here.
18892 That's because CHARPOS (ROW->end.pos) would equal
18893 PT if PT is before the character. */
18894 if (!row->ends_in_ellipsis_p)
18895 result = row->continued_p;
18896 else
18897 /* If the row ends in an ellipsis, then
18898 CHARPOS (ROW->end.pos) will equal point after the
18899 invisible text. We want that position to be displayed
18900 after the ellipsis. */
18901 result = 0;
18902 }
18903 /* If the row ends at ZV, display the cursor at the end of that
18904 row instead of at the start of the row below. */
18905 else if (row->ends_at_zv_p)
18906 result = 1;
18907 else
18908 result = 0;
18909 }
18910
18911 return result;
18912 }
18913
18914 \f
18915
18916 /* Push the property PROP so that it will be rendered at the current
18917 position in IT. Return 1 if PROP was successfully pushed, 0
18918 otherwise. Called from handle_line_prefix to handle the
18919 `line-prefix' and `wrap-prefix' properties. */
18920
18921 static int
18922 push_prefix_prop (struct it *it, Lisp_Object prop)
18923 {
18924 struct text_pos pos =
18925 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18926
18927 eassert (it->method == GET_FROM_BUFFER
18928 || it->method == GET_FROM_DISPLAY_VECTOR
18929 || it->method == GET_FROM_STRING);
18930
18931 /* We need to save the current buffer/string position, so it will be
18932 restored by pop_it, because iterate_out_of_display_property
18933 depends on that being set correctly, but some situations leave
18934 it->position not yet set when this function is called. */
18935 push_it (it, &pos);
18936
18937 if (STRINGP (prop))
18938 {
18939 if (SCHARS (prop) == 0)
18940 {
18941 pop_it (it);
18942 return 0;
18943 }
18944
18945 it->string = prop;
18946 it->string_from_prefix_prop_p = 1;
18947 it->multibyte_p = STRING_MULTIBYTE (it->string);
18948 it->current.overlay_string_index = -1;
18949 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18950 it->end_charpos = it->string_nchars = SCHARS (it->string);
18951 it->method = GET_FROM_STRING;
18952 it->stop_charpos = 0;
18953 it->prev_stop = 0;
18954 it->base_level_stop = 0;
18955
18956 /* Force paragraph direction to be that of the parent
18957 buffer/string. */
18958 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18959 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18960 else
18961 it->paragraph_embedding = L2R;
18962
18963 /* Set up the bidi iterator for this display string. */
18964 if (it->bidi_p)
18965 {
18966 it->bidi_it.string.lstring = it->string;
18967 it->bidi_it.string.s = NULL;
18968 it->bidi_it.string.schars = it->end_charpos;
18969 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18970 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18971 it->bidi_it.string.unibyte = !it->multibyte_p;
18972 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18973 }
18974 }
18975 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18976 {
18977 it->method = GET_FROM_STRETCH;
18978 it->object = prop;
18979 }
18980 #ifdef HAVE_WINDOW_SYSTEM
18981 else if (IMAGEP (prop))
18982 {
18983 it->what = IT_IMAGE;
18984 it->image_id = lookup_image (it->f, prop);
18985 it->method = GET_FROM_IMAGE;
18986 }
18987 #endif /* HAVE_WINDOW_SYSTEM */
18988 else
18989 {
18990 pop_it (it); /* bogus display property, give up */
18991 return 0;
18992 }
18993
18994 return 1;
18995 }
18996
18997 /* Return the character-property PROP at the current position in IT. */
18998
18999 static Lisp_Object
19000 get_it_property (struct it *it, Lisp_Object prop)
19001 {
19002 Lisp_Object position;
19003
19004 if (STRINGP (it->object))
19005 position = make_number (IT_STRING_CHARPOS (*it));
19006 else if (BUFFERP (it->object))
19007 position = make_number (IT_CHARPOS (*it));
19008 else
19009 return Qnil;
19010
19011 return Fget_char_property (position, prop, it->object);
19012 }
19013
19014 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19015
19016 static void
19017 handle_line_prefix (struct it *it)
19018 {
19019 Lisp_Object prefix;
19020
19021 if (it->continuation_lines_width > 0)
19022 {
19023 prefix = get_it_property (it, Qwrap_prefix);
19024 if (NILP (prefix))
19025 prefix = Vwrap_prefix;
19026 }
19027 else
19028 {
19029 prefix = get_it_property (it, Qline_prefix);
19030 if (NILP (prefix))
19031 prefix = Vline_prefix;
19032 }
19033 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19034 {
19035 /* If the prefix is wider than the window, and we try to wrap
19036 it, it would acquire its own wrap prefix, and so on till the
19037 iterator stack overflows. So, don't wrap the prefix. */
19038 it->line_wrap = TRUNCATE;
19039 it->avoid_cursor_p = 1;
19040 }
19041 }
19042
19043 \f
19044
19045 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19046 only for R2L lines from display_line and display_string, when they
19047 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19048 the line/string needs to be continued on the next glyph row. */
19049 static void
19050 unproduce_glyphs (struct it *it, int n)
19051 {
19052 struct glyph *glyph, *end;
19053
19054 eassert (it->glyph_row);
19055 eassert (it->glyph_row->reversed_p);
19056 eassert (it->area == TEXT_AREA);
19057 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19058
19059 if (n > it->glyph_row->used[TEXT_AREA])
19060 n = it->glyph_row->used[TEXT_AREA];
19061 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19062 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19063 for ( ; glyph < end; glyph++)
19064 glyph[-n] = *glyph;
19065 }
19066
19067 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19068 and ROW->maxpos. */
19069 static void
19070 find_row_edges (struct it *it, struct glyph_row *row,
19071 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19072 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19073 {
19074 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19075 lines' rows is implemented for bidi-reordered rows. */
19076
19077 /* ROW->minpos is the value of min_pos, the minimal buffer position
19078 we have in ROW, or ROW->start.pos if that is smaller. */
19079 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19080 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19081 else
19082 /* We didn't find buffer positions smaller than ROW->start, or
19083 didn't find _any_ valid buffer positions in any of the glyphs,
19084 so we must trust the iterator's computed positions. */
19085 row->minpos = row->start.pos;
19086 if (max_pos <= 0)
19087 {
19088 max_pos = CHARPOS (it->current.pos);
19089 max_bpos = BYTEPOS (it->current.pos);
19090 }
19091
19092 /* Here are the various use-cases for ending the row, and the
19093 corresponding values for ROW->maxpos:
19094
19095 Line ends in a newline from buffer eol_pos + 1
19096 Line is continued from buffer max_pos + 1
19097 Line is truncated on right it->current.pos
19098 Line ends in a newline from string max_pos + 1(*)
19099 (*) + 1 only when line ends in a forward scan
19100 Line is continued from string max_pos
19101 Line is continued from display vector max_pos
19102 Line is entirely from a string min_pos == max_pos
19103 Line is entirely from a display vector min_pos == max_pos
19104 Line that ends at ZV ZV
19105
19106 If you discover other use-cases, please add them here as
19107 appropriate. */
19108 if (row->ends_at_zv_p)
19109 row->maxpos = it->current.pos;
19110 else if (row->used[TEXT_AREA])
19111 {
19112 int seen_this_string = 0;
19113 struct glyph_row *r1 = row - 1;
19114
19115 /* Did we see the same display string on the previous row? */
19116 if (STRINGP (it->object)
19117 /* this is not the first row */
19118 && row > it->w->desired_matrix->rows
19119 /* previous row is not the header line */
19120 && !r1->mode_line_p
19121 /* previous row also ends in a newline from a string */
19122 && r1->ends_in_newline_from_string_p)
19123 {
19124 struct glyph *start, *end;
19125
19126 /* Search for the last glyph of the previous row that came
19127 from buffer or string. Depending on whether the row is
19128 L2R or R2L, we need to process it front to back or the
19129 other way round. */
19130 if (!r1->reversed_p)
19131 {
19132 start = r1->glyphs[TEXT_AREA];
19133 end = start + r1->used[TEXT_AREA];
19134 /* Glyphs inserted by redisplay have an integer (zero)
19135 as their object. */
19136 while (end > start
19137 && INTEGERP ((end - 1)->object)
19138 && (end - 1)->charpos <= 0)
19139 --end;
19140 if (end > start)
19141 {
19142 if (EQ ((end - 1)->object, it->object))
19143 seen_this_string = 1;
19144 }
19145 else
19146 /* If all the glyphs of the previous row were inserted
19147 by redisplay, it means the previous row was
19148 produced from a single newline, which is only
19149 possible if that newline came from the same string
19150 as the one which produced this ROW. */
19151 seen_this_string = 1;
19152 }
19153 else
19154 {
19155 end = r1->glyphs[TEXT_AREA] - 1;
19156 start = end + r1->used[TEXT_AREA];
19157 while (end < start
19158 && INTEGERP ((end + 1)->object)
19159 && (end + 1)->charpos <= 0)
19160 ++end;
19161 if (end < start)
19162 {
19163 if (EQ ((end + 1)->object, it->object))
19164 seen_this_string = 1;
19165 }
19166 else
19167 seen_this_string = 1;
19168 }
19169 }
19170 /* Take note of each display string that covers a newline only
19171 once, the first time we see it. This is for when a display
19172 string includes more than one newline in it. */
19173 if (row->ends_in_newline_from_string_p && !seen_this_string)
19174 {
19175 /* If we were scanning the buffer forward when we displayed
19176 the string, we want to account for at least one buffer
19177 position that belongs to this row (position covered by
19178 the display string), so that cursor positioning will
19179 consider this row as a candidate when point is at the end
19180 of the visual line represented by this row. This is not
19181 required when scanning back, because max_pos will already
19182 have a much larger value. */
19183 if (CHARPOS (row->end.pos) > max_pos)
19184 INC_BOTH (max_pos, max_bpos);
19185 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19186 }
19187 else if (CHARPOS (it->eol_pos) > 0)
19188 SET_TEXT_POS (row->maxpos,
19189 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19190 else if (row->continued_p)
19191 {
19192 /* If max_pos is different from IT's current position, it
19193 means IT->method does not belong to the display element
19194 at max_pos. However, it also means that the display
19195 element at max_pos was displayed in its entirety on this
19196 line, which is equivalent to saying that the next line
19197 starts at the next buffer position. */
19198 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19199 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19200 else
19201 {
19202 INC_BOTH (max_pos, max_bpos);
19203 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19204 }
19205 }
19206 else if (row->truncated_on_right_p)
19207 /* display_line already called reseat_at_next_visible_line_start,
19208 which puts the iterator at the beginning of the next line, in
19209 the logical order. */
19210 row->maxpos = it->current.pos;
19211 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19212 /* A line that is entirely from a string/image/stretch... */
19213 row->maxpos = row->minpos;
19214 else
19215 emacs_abort ();
19216 }
19217 else
19218 row->maxpos = it->current.pos;
19219 }
19220
19221 /* Construct the glyph row IT->glyph_row in the desired matrix of
19222 IT->w from text at the current position of IT. See dispextern.h
19223 for an overview of struct it. Value is non-zero if
19224 IT->glyph_row displays text, as opposed to a line displaying ZV
19225 only. */
19226
19227 static int
19228 display_line (struct it *it)
19229 {
19230 struct glyph_row *row = it->glyph_row;
19231 Lisp_Object overlay_arrow_string;
19232 struct it wrap_it;
19233 void *wrap_data = NULL;
19234 int may_wrap = 0, wrap_x IF_LINT (= 0);
19235 int wrap_row_used = -1;
19236 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19237 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19238 int wrap_row_extra_line_spacing IF_LINT (= 0);
19239 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19240 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19241 int cvpos;
19242 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19243 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19244
19245 /* We always start displaying at hpos zero even if hscrolled. */
19246 eassert (it->hpos == 0 && it->current_x == 0);
19247
19248 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19249 >= it->w->desired_matrix->nrows)
19250 {
19251 it->w->nrows_scale_factor++;
19252 fonts_changed_p = 1;
19253 return 0;
19254 }
19255
19256 /* Is IT->w showing the region? */
19257 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19258
19259 /* Clear the result glyph row and enable it. */
19260 prepare_desired_row (row);
19261
19262 row->y = it->current_y;
19263 row->start = it->start;
19264 row->continuation_lines_width = it->continuation_lines_width;
19265 row->displays_text_p = 1;
19266 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19267 it->starts_in_middle_of_char_p = 0;
19268
19269 /* Arrange the overlays nicely for our purposes. Usually, we call
19270 display_line on only one line at a time, in which case this
19271 can't really hurt too much, or we call it on lines which appear
19272 one after another in the buffer, in which case all calls to
19273 recenter_overlay_lists but the first will be pretty cheap. */
19274 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19275
19276 /* Move over display elements that are not visible because we are
19277 hscrolled. This may stop at an x-position < IT->first_visible_x
19278 if the first glyph is partially visible or if we hit a line end. */
19279 if (it->current_x < it->first_visible_x)
19280 {
19281 enum move_it_result move_result;
19282
19283 this_line_min_pos = row->start.pos;
19284 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19285 MOVE_TO_POS | MOVE_TO_X);
19286 /* If we are under a large hscroll, move_it_in_display_line_to
19287 could hit the end of the line without reaching
19288 it->first_visible_x. Pretend that we did reach it. This is
19289 especially important on a TTY, where we will call
19290 extend_face_to_end_of_line, which needs to know how many
19291 blank glyphs to produce. */
19292 if (it->current_x < it->first_visible_x
19293 && (move_result == MOVE_NEWLINE_OR_CR
19294 || move_result == MOVE_POS_MATCH_OR_ZV))
19295 it->current_x = it->first_visible_x;
19296
19297 /* Record the smallest positions seen while we moved over
19298 display elements that are not visible. This is needed by
19299 redisplay_internal for optimizing the case where the cursor
19300 stays inside the same line. The rest of this function only
19301 considers positions that are actually displayed, so
19302 RECORD_MAX_MIN_POS will not otherwise record positions that
19303 are hscrolled to the left of the left edge of the window. */
19304 min_pos = CHARPOS (this_line_min_pos);
19305 min_bpos = BYTEPOS (this_line_min_pos);
19306 }
19307 else
19308 {
19309 /* We only do this when not calling `move_it_in_display_line_to'
19310 above, because move_it_in_display_line_to calls
19311 handle_line_prefix itself. */
19312 handle_line_prefix (it);
19313 }
19314
19315 /* Get the initial row height. This is either the height of the
19316 text hscrolled, if there is any, or zero. */
19317 row->ascent = it->max_ascent;
19318 row->height = it->max_ascent + it->max_descent;
19319 row->phys_ascent = it->max_phys_ascent;
19320 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19321 row->extra_line_spacing = it->max_extra_line_spacing;
19322
19323 /* Utility macro to record max and min buffer positions seen until now. */
19324 #define RECORD_MAX_MIN_POS(IT) \
19325 do \
19326 { \
19327 int composition_p = !STRINGP ((IT)->string) \
19328 && ((IT)->what == IT_COMPOSITION); \
19329 ptrdiff_t current_pos = \
19330 composition_p ? (IT)->cmp_it.charpos \
19331 : IT_CHARPOS (*(IT)); \
19332 ptrdiff_t current_bpos = \
19333 composition_p ? CHAR_TO_BYTE (current_pos) \
19334 : IT_BYTEPOS (*(IT)); \
19335 if (current_pos < min_pos) \
19336 { \
19337 min_pos = current_pos; \
19338 min_bpos = current_bpos; \
19339 } \
19340 if (IT_CHARPOS (*it) > max_pos) \
19341 { \
19342 max_pos = IT_CHARPOS (*it); \
19343 max_bpos = IT_BYTEPOS (*it); \
19344 } \
19345 } \
19346 while (0)
19347
19348 /* Loop generating characters. The loop is left with IT on the next
19349 character to display. */
19350 while (1)
19351 {
19352 int n_glyphs_before, hpos_before, x_before;
19353 int x, nglyphs;
19354 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19355
19356 /* Retrieve the next thing to display. Value is zero if end of
19357 buffer reached. */
19358 if (!get_next_display_element (it))
19359 {
19360 /* Maybe add a space at the end of this line that is used to
19361 display the cursor there under X. Set the charpos of the
19362 first glyph of blank lines not corresponding to any text
19363 to -1. */
19364 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19365 row->exact_window_width_line_p = 1;
19366 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19367 || row->used[TEXT_AREA] == 0)
19368 {
19369 row->glyphs[TEXT_AREA]->charpos = -1;
19370 row->displays_text_p = 0;
19371
19372 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19373 && (!MINI_WINDOW_P (it->w)
19374 || (minibuf_level && EQ (it->window, minibuf_window))))
19375 row->indicate_empty_line_p = 1;
19376 }
19377
19378 it->continuation_lines_width = 0;
19379 row->ends_at_zv_p = 1;
19380 /* A row that displays right-to-left text must always have
19381 its last face extended all the way to the end of line,
19382 even if this row ends in ZV, because we still write to
19383 the screen left to right. We also need to extend the
19384 last face if the default face is remapped to some
19385 different face, otherwise the functions that clear
19386 portions of the screen will clear with the default face's
19387 background color. */
19388 if (row->reversed_p
19389 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19390 extend_face_to_end_of_line (it);
19391 break;
19392 }
19393
19394 /* Now, get the metrics of what we want to display. This also
19395 generates glyphs in `row' (which is IT->glyph_row). */
19396 n_glyphs_before = row->used[TEXT_AREA];
19397 x = it->current_x;
19398
19399 /* Remember the line height so far in case the next element doesn't
19400 fit on the line. */
19401 if (it->line_wrap != TRUNCATE)
19402 {
19403 ascent = it->max_ascent;
19404 descent = it->max_descent;
19405 phys_ascent = it->max_phys_ascent;
19406 phys_descent = it->max_phys_descent;
19407
19408 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19409 {
19410 if (IT_DISPLAYING_WHITESPACE (it))
19411 may_wrap = 1;
19412 else if (may_wrap)
19413 {
19414 SAVE_IT (wrap_it, *it, wrap_data);
19415 wrap_x = x;
19416 wrap_row_used = row->used[TEXT_AREA];
19417 wrap_row_ascent = row->ascent;
19418 wrap_row_height = row->height;
19419 wrap_row_phys_ascent = row->phys_ascent;
19420 wrap_row_phys_height = row->phys_height;
19421 wrap_row_extra_line_spacing = row->extra_line_spacing;
19422 wrap_row_min_pos = min_pos;
19423 wrap_row_min_bpos = min_bpos;
19424 wrap_row_max_pos = max_pos;
19425 wrap_row_max_bpos = max_bpos;
19426 may_wrap = 0;
19427 }
19428 }
19429 }
19430
19431 PRODUCE_GLYPHS (it);
19432
19433 /* If this display element was in marginal areas, continue with
19434 the next one. */
19435 if (it->area != TEXT_AREA)
19436 {
19437 row->ascent = max (row->ascent, it->max_ascent);
19438 row->height = max (row->height, it->max_ascent + it->max_descent);
19439 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19440 row->phys_height = max (row->phys_height,
19441 it->max_phys_ascent + it->max_phys_descent);
19442 row->extra_line_spacing = max (row->extra_line_spacing,
19443 it->max_extra_line_spacing);
19444 set_iterator_to_next (it, 1);
19445 continue;
19446 }
19447
19448 /* Does the display element fit on the line? If we truncate
19449 lines, we should draw past the right edge of the window. If
19450 we don't truncate, we want to stop so that we can display the
19451 continuation glyph before the right margin. If lines are
19452 continued, there are two possible strategies for characters
19453 resulting in more than 1 glyph (e.g. tabs): Display as many
19454 glyphs as possible in this line and leave the rest for the
19455 continuation line, or display the whole element in the next
19456 line. Original redisplay did the former, so we do it also. */
19457 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19458 hpos_before = it->hpos;
19459 x_before = x;
19460
19461 if (/* Not a newline. */
19462 nglyphs > 0
19463 /* Glyphs produced fit entirely in the line. */
19464 && it->current_x < it->last_visible_x)
19465 {
19466 it->hpos += nglyphs;
19467 row->ascent = max (row->ascent, it->max_ascent);
19468 row->height = max (row->height, it->max_ascent + it->max_descent);
19469 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19470 row->phys_height = max (row->phys_height,
19471 it->max_phys_ascent + it->max_phys_descent);
19472 row->extra_line_spacing = max (row->extra_line_spacing,
19473 it->max_extra_line_spacing);
19474 if (it->current_x - it->pixel_width < it->first_visible_x)
19475 row->x = x - it->first_visible_x;
19476 /* Record the maximum and minimum buffer positions seen so
19477 far in glyphs that will be displayed by this row. */
19478 if (it->bidi_p)
19479 RECORD_MAX_MIN_POS (it);
19480 }
19481 else
19482 {
19483 int i, new_x;
19484 struct glyph *glyph;
19485
19486 for (i = 0; i < nglyphs; ++i, x = new_x)
19487 {
19488 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19489 new_x = x + glyph->pixel_width;
19490
19491 if (/* Lines are continued. */
19492 it->line_wrap != TRUNCATE
19493 && (/* Glyph doesn't fit on the line. */
19494 new_x > it->last_visible_x
19495 /* Or it fits exactly on a window system frame. */
19496 || (new_x == it->last_visible_x
19497 && FRAME_WINDOW_P (it->f)
19498 && (row->reversed_p
19499 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19500 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19501 {
19502 /* End of a continued line. */
19503
19504 if (it->hpos == 0
19505 || (new_x == it->last_visible_x
19506 && FRAME_WINDOW_P (it->f)
19507 && (row->reversed_p
19508 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19509 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19510 {
19511 /* Current glyph is the only one on the line or
19512 fits exactly on the line. We must continue
19513 the line because we can't draw the cursor
19514 after the glyph. */
19515 row->continued_p = 1;
19516 it->current_x = new_x;
19517 it->continuation_lines_width += new_x;
19518 ++it->hpos;
19519 if (i == nglyphs - 1)
19520 {
19521 /* If line-wrap is on, check if a previous
19522 wrap point was found. */
19523 if (wrap_row_used > 0
19524 /* Even if there is a previous wrap
19525 point, continue the line here as
19526 usual, if (i) the previous character
19527 was a space or tab AND (ii) the
19528 current character is not. */
19529 && (!may_wrap
19530 || IT_DISPLAYING_WHITESPACE (it)))
19531 goto back_to_wrap;
19532
19533 /* Record the maximum and minimum buffer
19534 positions seen so far in glyphs that will be
19535 displayed by this row. */
19536 if (it->bidi_p)
19537 RECORD_MAX_MIN_POS (it);
19538 set_iterator_to_next (it, 1);
19539 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19540 {
19541 if (!get_next_display_element (it))
19542 {
19543 row->exact_window_width_line_p = 1;
19544 it->continuation_lines_width = 0;
19545 row->continued_p = 0;
19546 row->ends_at_zv_p = 1;
19547 }
19548 else if (ITERATOR_AT_END_OF_LINE_P (it))
19549 {
19550 row->continued_p = 0;
19551 row->exact_window_width_line_p = 1;
19552 }
19553 }
19554 }
19555 else if (it->bidi_p)
19556 RECORD_MAX_MIN_POS (it);
19557 }
19558 else if (CHAR_GLYPH_PADDING_P (*glyph)
19559 && !FRAME_WINDOW_P (it->f))
19560 {
19561 /* A padding glyph that doesn't fit on this line.
19562 This means the whole character doesn't fit
19563 on the line. */
19564 if (row->reversed_p)
19565 unproduce_glyphs (it, row->used[TEXT_AREA]
19566 - n_glyphs_before);
19567 row->used[TEXT_AREA] = n_glyphs_before;
19568
19569 /* Fill the rest of the row with continuation
19570 glyphs like in 20.x. */
19571 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19572 < row->glyphs[1 + TEXT_AREA])
19573 produce_special_glyphs (it, IT_CONTINUATION);
19574
19575 row->continued_p = 1;
19576 it->current_x = x_before;
19577 it->continuation_lines_width += x_before;
19578
19579 /* Restore the height to what it was before the
19580 element not fitting on the line. */
19581 it->max_ascent = ascent;
19582 it->max_descent = descent;
19583 it->max_phys_ascent = phys_ascent;
19584 it->max_phys_descent = phys_descent;
19585 }
19586 else if (wrap_row_used > 0)
19587 {
19588 back_to_wrap:
19589 if (row->reversed_p)
19590 unproduce_glyphs (it,
19591 row->used[TEXT_AREA] - wrap_row_used);
19592 RESTORE_IT (it, &wrap_it, wrap_data);
19593 it->continuation_lines_width += wrap_x;
19594 row->used[TEXT_AREA] = wrap_row_used;
19595 row->ascent = wrap_row_ascent;
19596 row->height = wrap_row_height;
19597 row->phys_ascent = wrap_row_phys_ascent;
19598 row->phys_height = wrap_row_phys_height;
19599 row->extra_line_spacing = wrap_row_extra_line_spacing;
19600 min_pos = wrap_row_min_pos;
19601 min_bpos = wrap_row_min_bpos;
19602 max_pos = wrap_row_max_pos;
19603 max_bpos = wrap_row_max_bpos;
19604 row->continued_p = 1;
19605 row->ends_at_zv_p = 0;
19606 row->exact_window_width_line_p = 0;
19607 it->continuation_lines_width += x;
19608
19609 /* Make sure that a non-default face is extended
19610 up to the right margin of the window. */
19611 extend_face_to_end_of_line (it);
19612 }
19613 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19614 {
19615 /* A TAB that extends past the right edge of the
19616 window. This produces a single glyph on
19617 window system frames. We leave the glyph in
19618 this row and let it fill the row, but don't
19619 consume the TAB. */
19620 if ((row->reversed_p
19621 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19622 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19623 produce_special_glyphs (it, IT_CONTINUATION);
19624 it->continuation_lines_width += it->last_visible_x;
19625 row->ends_in_middle_of_char_p = 1;
19626 row->continued_p = 1;
19627 glyph->pixel_width = it->last_visible_x - x;
19628 it->starts_in_middle_of_char_p = 1;
19629 }
19630 else
19631 {
19632 /* Something other than a TAB that draws past
19633 the right edge of the window. Restore
19634 positions to values before the element. */
19635 if (row->reversed_p)
19636 unproduce_glyphs (it, row->used[TEXT_AREA]
19637 - (n_glyphs_before + i));
19638 row->used[TEXT_AREA] = n_glyphs_before + i;
19639
19640 /* Display continuation glyphs. */
19641 it->current_x = x_before;
19642 it->continuation_lines_width += x;
19643 if (!FRAME_WINDOW_P (it->f)
19644 || (row->reversed_p
19645 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19646 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19647 produce_special_glyphs (it, IT_CONTINUATION);
19648 row->continued_p = 1;
19649
19650 extend_face_to_end_of_line (it);
19651
19652 if (nglyphs > 1 && i > 0)
19653 {
19654 row->ends_in_middle_of_char_p = 1;
19655 it->starts_in_middle_of_char_p = 1;
19656 }
19657
19658 /* Restore the height to what it was before the
19659 element not fitting on the line. */
19660 it->max_ascent = ascent;
19661 it->max_descent = descent;
19662 it->max_phys_ascent = phys_ascent;
19663 it->max_phys_descent = phys_descent;
19664 }
19665
19666 break;
19667 }
19668 else if (new_x > it->first_visible_x)
19669 {
19670 /* Increment number of glyphs actually displayed. */
19671 ++it->hpos;
19672
19673 /* Record the maximum and minimum buffer positions
19674 seen so far in glyphs that will be displayed by
19675 this row. */
19676 if (it->bidi_p)
19677 RECORD_MAX_MIN_POS (it);
19678
19679 if (x < it->first_visible_x)
19680 /* Glyph is partially visible, i.e. row starts at
19681 negative X position. */
19682 row->x = x - it->first_visible_x;
19683 }
19684 else
19685 {
19686 /* Glyph is completely off the left margin of the
19687 window. This should not happen because of the
19688 move_it_in_display_line at the start of this
19689 function, unless the text display area of the
19690 window is empty. */
19691 eassert (it->first_visible_x <= it->last_visible_x);
19692 }
19693 }
19694 /* Even if this display element produced no glyphs at all,
19695 we want to record its position. */
19696 if (it->bidi_p && nglyphs == 0)
19697 RECORD_MAX_MIN_POS (it);
19698
19699 row->ascent = max (row->ascent, it->max_ascent);
19700 row->height = max (row->height, it->max_ascent + it->max_descent);
19701 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19702 row->phys_height = max (row->phys_height,
19703 it->max_phys_ascent + it->max_phys_descent);
19704 row->extra_line_spacing = max (row->extra_line_spacing,
19705 it->max_extra_line_spacing);
19706
19707 /* End of this display line if row is continued. */
19708 if (row->continued_p || row->ends_at_zv_p)
19709 break;
19710 }
19711
19712 at_end_of_line:
19713 /* Is this a line end? If yes, we're also done, after making
19714 sure that a non-default face is extended up to the right
19715 margin of the window. */
19716 if (ITERATOR_AT_END_OF_LINE_P (it))
19717 {
19718 int used_before = row->used[TEXT_AREA];
19719
19720 row->ends_in_newline_from_string_p = STRINGP (it->object);
19721
19722 /* Add a space at the end of the line that is used to
19723 display the cursor there. */
19724 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19725 append_space_for_newline (it, 0);
19726
19727 /* Extend the face to the end of the line. */
19728 extend_face_to_end_of_line (it);
19729
19730 /* Make sure we have the position. */
19731 if (used_before == 0)
19732 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19733
19734 /* Record the position of the newline, for use in
19735 find_row_edges. */
19736 it->eol_pos = it->current.pos;
19737
19738 /* Consume the line end. This skips over invisible lines. */
19739 set_iterator_to_next (it, 1);
19740 it->continuation_lines_width = 0;
19741 break;
19742 }
19743
19744 /* Proceed with next display element. Note that this skips
19745 over lines invisible because of selective display. */
19746 set_iterator_to_next (it, 1);
19747
19748 /* If we truncate lines, we are done when the last displayed
19749 glyphs reach past the right margin of the window. */
19750 if (it->line_wrap == TRUNCATE
19751 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19752 ? (it->current_x >= it->last_visible_x)
19753 : (it->current_x > it->last_visible_x)))
19754 {
19755 /* Maybe add truncation glyphs. */
19756 if (!FRAME_WINDOW_P (it->f)
19757 || (row->reversed_p
19758 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19759 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19760 {
19761 int i, n;
19762
19763 if (!row->reversed_p)
19764 {
19765 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19766 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19767 break;
19768 }
19769 else
19770 {
19771 for (i = 0; i < row->used[TEXT_AREA]; i++)
19772 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19773 break;
19774 /* Remove any padding glyphs at the front of ROW, to
19775 make room for the truncation glyphs we will be
19776 adding below. The loop below always inserts at
19777 least one truncation glyph, so also remove the
19778 last glyph added to ROW. */
19779 unproduce_glyphs (it, i + 1);
19780 /* Adjust i for the loop below. */
19781 i = row->used[TEXT_AREA] - (i + 1);
19782 }
19783
19784 it->current_x = x_before;
19785 if (!FRAME_WINDOW_P (it->f))
19786 {
19787 for (n = row->used[TEXT_AREA]; i < n; ++i)
19788 {
19789 row->used[TEXT_AREA] = i;
19790 produce_special_glyphs (it, IT_TRUNCATION);
19791 }
19792 }
19793 else
19794 {
19795 row->used[TEXT_AREA] = i;
19796 produce_special_glyphs (it, IT_TRUNCATION);
19797 }
19798 }
19799 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19800 {
19801 /* Don't truncate if we can overflow newline into fringe. */
19802 if (!get_next_display_element (it))
19803 {
19804 it->continuation_lines_width = 0;
19805 row->ends_at_zv_p = 1;
19806 row->exact_window_width_line_p = 1;
19807 break;
19808 }
19809 if (ITERATOR_AT_END_OF_LINE_P (it))
19810 {
19811 row->exact_window_width_line_p = 1;
19812 goto at_end_of_line;
19813 }
19814 it->current_x = x_before;
19815 }
19816
19817 row->truncated_on_right_p = 1;
19818 it->continuation_lines_width = 0;
19819 reseat_at_next_visible_line_start (it, 0);
19820 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19821 it->hpos = hpos_before;
19822 break;
19823 }
19824 }
19825
19826 if (wrap_data)
19827 bidi_unshelve_cache (wrap_data, 1);
19828
19829 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19830 at the left window margin. */
19831 if (it->first_visible_x
19832 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19833 {
19834 if (!FRAME_WINDOW_P (it->f)
19835 || (row->reversed_p
19836 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19837 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19838 insert_left_trunc_glyphs (it);
19839 row->truncated_on_left_p = 1;
19840 }
19841
19842 /* Remember the position at which this line ends.
19843
19844 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19845 cannot be before the call to find_row_edges below, since that is
19846 where these positions are determined. */
19847 row->end = it->current;
19848 if (!it->bidi_p)
19849 {
19850 row->minpos = row->start.pos;
19851 row->maxpos = row->end.pos;
19852 }
19853 else
19854 {
19855 /* ROW->minpos and ROW->maxpos must be the smallest and
19856 `1 + the largest' buffer positions in ROW. But if ROW was
19857 bidi-reordered, these two positions can be anywhere in the
19858 row, so we must determine them now. */
19859 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19860 }
19861
19862 /* If the start of this line is the overlay arrow-position, then
19863 mark this glyph row as the one containing the overlay arrow.
19864 This is clearly a mess with variable size fonts. It would be
19865 better to let it be displayed like cursors under X. */
19866 if ((row->displays_text_p || !overlay_arrow_seen)
19867 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19868 !NILP (overlay_arrow_string)))
19869 {
19870 /* Overlay arrow in window redisplay is a fringe bitmap. */
19871 if (STRINGP (overlay_arrow_string))
19872 {
19873 struct glyph_row *arrow_row
19874 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19875 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19876 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19877 struct glyph *p = row->glyphs[TEXT_AREA];
19878 struct glyph *p2, *end;
19879
19880 /* Copy the arrow glyphs. */
19881 while (glyph < arrow_end)
19882 *p++ = *glyph++;
19883
19884 /* Throw away padding glyphs. */
19885 p2 = p;
19886 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19887 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19888 ++p2;
19889 if (p2 > p)
19890 {
19891 while (p2 < end)
19892 *p++ = *p2++;
19893 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19894 }
19895 }
19896 else
19897 {
19898 eassert (INTEGERP (overlay_arrow_string));
19899 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19900 }
19901 overlay_arrow_seen = 1;
19902 }
19903
19904 /* Highlight trailing whitespace. */
19905 if (!NILP (Vshow_trailing_whitespace))
19906 highlight_trailing_whitespace (it->f, it->glyph_row);
19907
19908 /* Compute pixel dimensions of this line. */
19909 compute_line_metrics (it);
19910
19911 /* Implementation note: No changes in the glyphs of ROW or in their
19912 faces can be done past this point, because compute_line_metrics
19913 computes ROW's hash value and stores it within the glyph_row
19914 structure. */
19915
19916 /* Record whether this row ends inside an ellipsis. */
19917 row->ends_in_ellipsis_p
19918 = (it->method == GET_FROM_DISPLAY_VECTOR
19919 && it->ellipsis_p);
19920
19921 /* Save fringe bitmaps in this row. */
19922 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19923 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19924 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19925 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19926
19927 it->left_user_fringe_bitmap = 0;
19928 it->left_user_fringe_face_id = 0;
19929 it->right_user_fringe_bitmap = 0;
19930 it->right_user_fringe_face_id = 0;
19931
19932 /* Maybe set the cursor. */
19933 cvpos = it->w->cursor.vpos;
19934 if ((cvpos < 0
19935 /* In bidi-reordered rows, keep checking for proper cursor
19936 position even if one has been found already, because buffer
19937 positions in such rows change non-linearly with ROW->VPOS,
19938 when a line is continued. One exception: when we are at ZV,
19939 display cursor on the first suitable glyph row, since all
19940 the empty rows after that also have their position set to ZV. */
19941 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19942 lines' rows is implemented for bidi-reordered rows. */
19943 || (it->bidi_p
19944 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19945 && PT >= MATRIX_ROW_START_CHARPOS (row)
19946 && PT <= MATRIX_ROW_END_CHARPOS (row)
19947 && cursor_row_p (row))
19948 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19949
19950 /* Prepare for the next line. This line starts horizontally at (X
19951 HPOS) = (0 0). Vertical positions are incremented. As a
19952 convenience for the caller, IT->glyph_row is set to the next
19953 row to be used. */
19954 it->current_x = it->hpos = 0;
19955 it->current_y += row->height;
19956 SET_TEXT_POS (it->eol_pos, 0, 0);
19957 ++it->vpos;
19958 ++it->glyph_row;
19959 /* The next row should by default use the same value of the
19960 reversed_p flag as this one. set_iterator_to_next decides when
19961 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19962 the flag accordingly. */
19963 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19964 it->glyph_row->reversed_p = row->reversed_p;
19965 it->start = row->end;
19966 return row->displays_text_p;
19967
19968 #undef RECORD_MAX_MIN_POS
19969 }
19970
19971 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19972 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19973 doc: /* Return paragraph direction at point in BUFFER.
19974 Value is either `left-to-right' or `right-to-left'.
19975 If BUFFER is omitted or nil, it defaults to the current buffer.
19976
19977 Paragraph direction determines how the text in the paragraph is displayed.
19978 In left-to-right paragraphs, text begins at the left margin of the window
19979 and the reading direction is generally left to right. In right-to-left
19980 paragraphs, text begins at the right margin and is read from right to left.
19981
19982 See also `bidi-paragraph-direction'. */)
19983 (Lisp_Object buffer)
19984 {
19985 struct buffer *buf = current_buffer;
19986 struct buffer *old = buf;
19987
19988 if (! NILP (buffer))
19989 {
19990 CHECK_BUFFER (buffer);
19991 buf = XBUFFER (buffer);
19992 }
19993
19994 if (NILP (BVAR (buf, bidi_display_reordering))
19995 || NILP (BVAR (buf, enable_multibyte_characters))
19996 /* When we are loading loadup.el, the character property tables
19997 needed for bidi iteration are not yet available. */
19998 || !NILP (Vpurify_flag))
19999 return Qleft_to_right;
20000 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20001 return BVAR (buf, bidi_paragraph_direction);
20002 else
20003 {
20004 /* Determine the direction from buffer text. We could try to
20005 use current_matrix if it is up to date, but this seems fast
20006 enough as it is. */
20007 struct bidi_it itb;
20008 ptrdiff_t pos = BUF_PT (buf);
20009 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20010 int c;
20011 void *itb_data = bidi_shelve_cache ();
20012
20013 set_buffer_temp (buf);
20014 /* bidi_paragraph_init finds the base direction of the paragraph
20015 by searching forward from paragraph start. We need the base
20016 direction of the current or _previous_ paragraph, so we need
20017 to make sure we are within that paragraph. To that end, find
20018 the previous non-empty line. */
20019 if (pos >= ZV && pos > BEGV)
20020 {
20021 pos--;
20022 bytepos = CHAR_TO_BYTE (pos);
20023 }
20024 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20025 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20026 {
20027 while ((c = FETCH_BYTE (bytepos)) == '\n'
20028 || c == ' ' || c == '\t' || c == '\f')
20029 {
20030 if (bytepos <= BEGV_BYTE)
20031 break;
20032 bytepos--;
20033 pos--;
20034 }
20035 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20036 bytepos--;
20037 }
20038 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20039 itb.paragraph_dir = NEUTRAL_DIR;
20040 itb.string.s = NULL;
20041 itb.string.lstring = Qnil;
20042 itb.string.bufpos = 0;
20043 itb.string.unibyte = 0;
20044 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20045 bidi_unshelve_cache (itb_data, 0);
20046 set_buffer_temp (old);
20047 switch (itb.paragraph_dir)
20048 {
20049 case L2R:
20050 return Qleft_to_right;
20051 break;
20052 case R2L:
20053 return Qright_to_left;
20054 break;
20055 default:
20056 emacs_abort ();
20057 }
20058 }
20059 }
20060
20061
20062 \f
20063 /***********************************************************************
20064 Menu Bar
20065 ***********************************************************************/
20066
20067 /* Redisplay the menu bar in the frame for window W.
20068
20069 The menu bar of X frames that don't have X toolkit support is
20070 displayed in a special window W->frame->menu_bar_window.
20071
20072 The menu bar of terminal frames is treated specially as far as
20073 glyph matrices are concerned. Menu bar lines are not part of
20074 windows, so the update is done directly on the frame matrix rows
20075 for the menu bar. */
20076
20077 static void
20078 display_menu_bar (struct window *w)
20079 {
20080 struct frame *f = XFRAME (WINDOW_FRAME (w));
20081 struct it it;
20082 Lisp_Object items;
20083 int i;
20084
20085 /* Don't do all this for graphical frames. */
20086 #ifdef HAVE_NTGUI
20087 if (FRAME_W32_P (f))
20088 return;
20089 #endif
20090 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20091 if (FRAME_X_P (f))
20092 return;
20093 #endif
20094
20095 #ifdef HAVE_NS
20096 if (FRAME_NS_P (f))
20097 return;
20098 #endif /* HAVE_NS */
20099
20100 #ifdef USE_X_TOOLKIT
20101 eassert (!FRAME_WINDOW_P (f));
20102 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20103 it.first_visible_x = 0;
20104 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20105 #else /* not USE_X_TOOLKIT */
20106 if (FRAME_WINDOW_P (f))
20107 {
20108 /* Menu bar lines are displayed in the desired matrix of the
20109 dummy window menu_bar_window. */
20110 struct window *menu_w;
20111 eassert (WINDOWP (f->menu_bar_window));
20112 menu_w = XWINDOW (f->menu_bar_window);
20113 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20114 MENU_FACE_ID);
20115 it.first_visible_x = 0;
20116 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20117 }
20118 else
20119 {
20120 /* This is a TTY frame, i.e. character hpos/vpos are used as
20121 pixel x/y. */
20122 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20123 MENU_FACE_ID);
20124 it.first_visible_x = 0;
20125 it.last_visible_x = FRAME_COLS (f);
20126 }
20127 #endif /* not USE_X_TOOLKIT */
20128
20129 /* FIXME: This should be controlled by a user option. See the
20130 comments in redisplay_tool_bar and display_mode_line about
20131 this. */
20132 it.paragraph_embedding = L2R;
20133
20134 /* Clear all rows of the menu bar. */
20135 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20136 {
20137 struct glyph_row *row = it.glyph_row + i;
20138 clear_glyph_row (row);
20139 row->enabled_p = 1;
20140 row->full_width_p = 1;
20141 }
20142
20143 /* Display all items of the menu bar. */
20144 items = FRAME_MENU_BAR_ITEMS (it.f);
20145 for (i = 0; i < ASIZE (items); i += 4)
20146 {
20147 Lisp_Object string;
20148
20149 /* Stop at nil string. */
20150 string = AREF (items, i + 1);
20151 if (NILP (string))
20152 break;
20153
20154 /* Remember where item was displayed. */
20155 ASET (items, i + 3, make_number (it.hpos));
20156
20157 /* Display the item, pad with one space. */
20158 if (it.current_x < it.last_visible_x)
20159 display_string (NULL, string, Qnil, 0, 0, &it,
20160 SCHARS (string) + 1, 0, 0, -1);
20161 }
20162
20163 /* Fill out the line with spaces. */
20164 if (it.current_x < it.last_visible_x)
20165 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20166
20167 /* Compute the total height of the lines. */
20168 compute_line_metrics (&it);
20169 }
20170
20171
20172 \f
20173 /***********************************************************************
20174 Mode Line
20175 ***********************************************************************/
20176
20177 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20178 FORCE is non-zero, redisplay mode lines unconditionally.
20179 Otherwise, redisplay only mode lines that are garbaged. Value is
20180 the number of windows whose mode lines were redisplayed. */
20181
20182 static int
20183 redisplay_mode_lines (Lisp_Object window, int force)
20184 {
20185 int nwindows = 0;
20186
20187 while (!NILP (window))
20188 {
20189 struct window *w = XWINDOW (window);
20190
20191 if (WINDOWP (w->hchild))
20192 nwindows += redisplay_mode_lines (w->hchild, force);
20193 else if (WINDOWP (w->vchild))
20194 nwindows += redisplay_mode_lines (w->vchild, force);
20195 else if (force
20196 || FRAME_GARBAGED_P (XFRAME (w->frame))
20197 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20198 {
20199 struct text_pos lpoint;
20200 struct buffer *old = current_buffer;
20201
20202 /* Set the window's buffer for the mode line display. */
20203 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20204 set_buffer_internal_1 (XBUFFER (w->buffer));
20205
20206 /* Point refers normally to the selected window. For any
20207 other window, set up appropriate value. */
20208 if (!EQ (window, selected_window))
20209 {
20210 struct text_pos pt;
20211
20212 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20213 if (CHARPOS (pt) < BEGV)
20214 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20215 else if (CHARPOS (pt) > (ZV - 1))
20216 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20217 else
20218 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20219 }
20220
20221 /* Display mode lines. */
20222 clear_glyph_matrix (w->desired_matrix);
20223 if (display_mode_lines (w))
20224 {
20225 ++nwindows;
20226 w->must_be_updated_p = 1;
20227 }
20228
20229 /* Restore old settings. */
20230 set_buffer_internal_1 (old);
20231 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20232 }
20233
20234 window = w->next;
20235 }
20236
20237 return nwindows;
20238 }
20239
20240
20241 /* Display the mode and/or header line of window W. Value is the
20242 sum number of mode lines and header lines displayed. */
20243
20244 static int
20245 display_mode_lines (struct window *w)
20246 {
20247 Lisp_Object old_selected_window = selected_window;
20248 Lisp_Object old_selected_frame = selected_frame;
20249 Lisp_Object new_frame = w->frame;
20250 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20251 int n = 0;
20252
20253 selected_frame = new_frame;
20254 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20255 or window's point, then we'd need select_window_1 here as well. */
20256 XSETWINDOW (selected_window, w);
20257 XFRAME (new_frame)->selected_window = selected_window;
20258
20259 /* These will be set while the mode line specs are processed. */
20260 line_number_displayed = 0;
20261 w->column_number_displayed = -1;
20262
20263 if (WINDOW_WANTS_MODELINE_P (w))
20264 {
20265 struct window *sel_w = XWINDOW (old_selected_window);
20266
20267 /* Select mode line face based on the real selected window. */
20268 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20269 BVAR (current_buffer, mode_line_format));
20270 ++n;
20271 }
20272
20273 if (WINDOW_WANTS_HEADER_LINE_P (w))
20274 {
20275 display_mode_line (w, HEADER_LINE_FACE_ID,
20276 BVAR (current_buffer, header_line_format));
20277 ++n;
20278 }
20279
20280 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20281 selected_frame = old_selected_frame;
20282 selected_window = old_selected_window;
20283 return n;
20284 }
20285
20286
20287 /* Display mode or header line of window W. FACE_ID specifies which
20288 line to display; it is either MODE_LINE_FACE_ID or
20289 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20290 display. Value is the pixel height of the mode/header line
20291 displayed. */
20292
20293 static int
20294 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20295 {
20296 struct it it;
20297 struct face *face;
20298 ptrdiff_t count = SPECPDL_INDEX ();
20299
20300 init_iterator (&it, w, -1, -1, NULL, face_id);
20301 /* Don't extend on a previously drawn mode-line.
20302 This may happen if called from pos_visible_p. */
20303 it.glyph_row->enabled_p = 0;
20304 prepare_desired_row (it.glyph_row);
20305
20306 it.glyph_row->mode_line_p = 1;
20307
20308 /* FIXME: This should be controlled by a user option. But
20309 supporting such an option is not trivial, since the mode line is
20310 made up of many separate strings. */
20311 it.paragraph_embedding = L2R;
20312
20313 record_unwind_protect (unwind_format_mode_line,
20314 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20315
20316 mode_line_target = MODE_LINE_DISPLAY;
20317
20318 /* Temporarily make frame's keyboard the current kboard so that
20319 kboard-local variables in the mode_line_format will get the right
20320 values. */
20321 push_kboard (FRAME_KBOARD (it.f));
20322 record_unwind_save_match_data ();
20323 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20324 pop_kboard ();
20325
20326 unbind_to (count, Qnil);
20327
20328 /* Fill up with spaces. */
20329 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20330
20331 compute_line_metrics (&it);
20332 it.glyph_row->full_width_p = 1;
20333 it.glyph_row->continued_p = 0;
20334 it.glyph_row->truncated_on_left_p = 0;
20335 it.glyph_row->truncated_on_right_p = 0;
20336
20337 /* Make a 3D mode-line have a shadow at its right end. */
20338 face = FACE_FROM_ID (it.f, face_id);
20339 extend_face_to_end_of_line (&it);
20340 if (face->box != FACE_NO_BOX)
20341 {
20342 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20343 + it.glyph_row->used[TEXT_AREA] - 1);
20344 last->right_box_line_p = 1;
20345 }
20346
20347 return it.glyph_row->height;
20348 }
20349
20350 /* Move element ELT in LIST to the front of LIST.
20351 Return the updated list. */
20352
20353 static Lisp_Object
20354 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20355 {
20356 register Lisp_Object tail, prev;
20357 register Lisp_Object tem;
20358
20359 tail = list;
20360 prev = Qnil;
20361 while (CONSP (tail))
20362 {
20363 tem = XCAR (tail);
20364
20365 if (EQ (elt, tem))
20366 {
20367 /* Splice out the link TAIL. */
20368 if (NILP (prev))
20369 list = XCDR (tail);
20370 else
20371 Fsetcdr (prev, XCDR (tail));
20372
20373 /* Now make it the first. */
20374 Fsetcdr (tail, list);
20375 return tail;
20376 }
20377 else
20378 prev = tail;
20379 tail = XCDR (tail);
20380 QUIT;
20381 }
20382
20383 /* Not found--return unchanged LIST. */
20384 return list;
20385 }
20386
20387 /* Contribute ELT to the mode line for window IT->w. How it
20388 translates into text depends on its data type.
20389
20390 IT describes the display environment in which we display, as usual.
20391
20392 DEPTH is the depth in recursion. It is used to prevent
20393 infinite recursion here.
20394
20395 FIELD_WIDTH is the number of characters the display of ELT should
20396 occupy in the mode line, and PRECISION is the maximum number of
20397 characters to display from ELT's representation. See
20398 display_string for details.
20399
20400 Returns the hpos of the end of the text generated by ELT.
20401
20402 PROPS is a property list to add to any string we encounter.
20403
20404 If RISKY is nonzero, remove (disregard) any properties in any string
20405 we encounter, and ignore :eval and :propertize.
20406
20407 The global variable `mode_line_target' determines whether the
20408 output is passed to `store_mode_line_noprop',
20409 `store_mode_line_string', or `display_string'. */
20410
20411 static int
20412 display_mode_element (struct it *it, int depth, int field_width, int precision,
20413 Lisp_Object elt, Lisp_Object props, int risky)
20414 {
20415 int n = 0, field, prec;
20416 int literal = 0;
20417
20418 tail_recurse:
20419 if (depth > 100)
20420 elt = build_string ("*too-deep*");
20421
20422 depth++;
20423
20424 switch (XTYPE (elt))
20425 {
20426 case Lisp_String:
20427 {
20428 /* A string: output it and check for %-constructs within it. */
20429 unsigned char c;
20430 ptrdiff_t offset = 0;
20431
20432 if (SCHARS (elt) > 0
20433 && (!NILP (props) || risky))
20434 {
20435 Lisp_Object oprops, aelt;
20436 oprops = Ftext_properties_at (make_number (0), elt);
20437
20438 /* If the starting string's properties are not what
20439 we want, translate the string. Also, if the string
20440 is risky, do that anyway. */
20441
20442 if (NILP (Fequal (props, oprops)) || risky)
20443 {
20444 /* If the starting string has properties,
20445 merge the specified ones onto the existing ones. */
20446 if (! NILP (oprops) && !risky)
20447 {
20448 Lisp_Object tem;
20449
20450 oprops = Fcopy_sequence (oprops);
20451 tem = props;
20452 while (CONSP (tem))
20453 {
20454 oprops = Fplist_put (oprops, XCAR (tem),
20455 XCAR (XCDR (tem)));
20456 tem = XCDR (XCDR (tem));
20457 }
20458 props = oprops;
20459 }
20460
20461 aelt = Fassoc (elt, mode_line_proptrans_alist);
20462 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20463 {
20464 /* AELT is what we want. Move it to the front
20465 without consing. */
20466 elt = XCAR (aelt);
20467 mode_line_proptrans_alist
20468 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20469 }
20470 else
20471 {
20472 Lisp_Object tem;
20473
20474 /* If AELT has the wrong props, it is useless.
20475 so get rid of it. */
20476 if (! NILP (aelt))
20477 mode_line_proptrans_alist
20478 = Fdelq (aelt, mode_line_proptrans_alist);
20479
20480 elt = Fcopy_sequence (elt);
20481 Fset_text_properties (make_number (0), Flength (elt),
20482 props, elt);
20483 /* Add this item to mode_line_proptrans_alist. */
20484 mode_line_proptrans_alist
20485 = Fcons (Fcons (elt, props),
20486 mode_line_proptrans_alist);
20487 /* Truncate mode_line_proptrans_alist
20488 to at most 50 elements. */
20489 tem = Fnthcdr (make_number (50),
20490 mode_line_proptrans_alist);
20491 if (! NILP (tem))
20492 XSETCDR (tem, Qnil);
20493 }
20494 }
20495 }
20496
20497 offset = 0;
20498
20499 if (literal)
20500 {
20501 prec = precision - n;
20502 switch (mode_line_target)
20503 {
20504 case MODE_LINE_NOPROP:
20505 case MODE_LINE_TITLE:
20506 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20507 break;
20508 case MODE_LINE_STRING:
20509 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20510 break;
20511 case MODE_LINE_DISPLAY:
20512 n += display_string (NULL, elt, Qnil, 0, 0, it,
20513 0, prec, 0, STRING_MULTIBYTE (elt));
20514 break;
20515 }
20516
20517 break;
20518 }
20519
20520 /* Handle the non-literal case. */
20521
20522 while ((precision <= 0 || n < precision)
20523 && SREF (elt, offset) != 0
20524 && (mode_line_target != MODE_LINE_DISPLAY
20525 || it->current_x < it->last_visible_x))
20526 {
20527 ptrdiff_t last_offset = offset;
20528
20529 /* Advance to end of string or next format specifier. */
20530 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20531 ;
20532
20533 if (offset - 1 != last_offset)
20534 {
20535 ptrdiff_t nchars, nbytes;
20536
20537 /* Output to end of string or up to '%'. Field width
20538 is length of string. Don't output more than
20539 PRECISION allows us. */
20540 offset--;
20541
20542 prec = c_string_width (SDATA (elt) + last_offset,
20543 offset - last_offset, precision - n,
20544 &nchars, &nbytes);
20545
20546 switch (mode_line_target)
20547 {
20548 case MODE_LINE_NOPROP:
20549 case MODE_LINE_TITLE:
20550 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20551 break;
20552 case MODE_LINE_STRING:
20553 {
20554 ptrdiff_t bytepos = last_offset;
20555 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20556 ptrdiff_t endpos = (precision <= 0
20557 ? string_byte_to_char (elt, offset)
20558 : charpos + nchars);
20559
20560 n += store_mode_line_string (NULL,
20561 Fsubstring (elt, make_number (charpos),
20562 make_number (endpos)),
20563 0, 0, 0, Qnil);
20564 }
20565 break;
20566 case MODE_LINE_DISPLAY:
20567 {
20568 ptrdiff_t bytepos = last_offset;
20569 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20570
20571 if (precision <= 0)
20572 nchars = string_byte_to_char (elt, offset) - charpos;
20573 n += display_string (NULL, elt, Qnil, 0, charpos,
20574 it, 0, nchars, 0,
20575 STRING_MULTIBYTE (elt));
20576 }
20577 break;
20578 }
20579 }
20580 else /* c == '%' */
20581 {
20582 ptrdiff_t percent_position = offset;
20583
20584 /* Get the specified minimum width. Zero means
20585 don't pad. */
20586 field = 0;
20587 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20588 field = field * 10 + c - '0';
20589
20590 /* Don't pad beyond the total padding allowed. */
20591 if (field_width - n > 0 && field > field_width - n)
20592 field = field_width - n;
20593
20594 /* Note that either PRECISION <= 0 or N < PRECISION. */
20595 prec = precision - n;
20596
20597 if (c == 'M')
20598 n += display_mode_element (it, depth, field, prec,
20599 Vglobal_mode_string, props,
20600 risky);
20601 else if (c != 0)
20602 {
20603 bool multibyte;
20604 ptrdiff_t bytepos, charpos;
20605 const char *spec;
20606 Lisp_Object string;
20607
20608 bytepos = percent_position;
20609 charpos = (STRING_MULTIBYTE (elt)
20610 ? string_byte_to_char (elt, bytepos)
20611 : bytepos);
20612 spec = decode_mode_spec (it->w, c, field, &string);
20613 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20614
20615 switch (mode_line_target)
20616 {
20617 case MODE_LINE_NOPROP:
20618 case MODE_LINE_TITLE:
20619 n += store_mode_line_noprop (spec, field, prec);
20620 break;
20621 case MODE_LINE_STRING:
20622 {
20623 Lisp_Object tem = build_string (spec);
20624 props = Ftext_properties_at (make_number (charpos), elt);
20625 /* Should only keep face property in props */
20626 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20627 }
20628 break;
20629 case MODE_LINE_DISPLAY:
20630 {
20631 int nglyphs_before, nwritten;
20632
20633 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20634 nwritten = display_string (spec, string, elt,
20635 charpos, 0, it,
20636 field, prec, 0,
20637 multibyte);
20638
20639 /* Assign to the glyphs written above the
20640 string where the `%x' came from, position
20641 of the `%'. */
20642 if (nwritten > 0)
20643 {
20644 struct glyph *glyph
20645 = (it->glyph_row->glyphs[TEXT_AREA]
20646 + nglyphs_before);
20647 int i;
20648
20649 for (i = 0; i < nwritten; ++i)
20650 {
20651 glyph[i].object = elt;
20652 glyph[i].charpos = charpos;
20653 }
20654
20655 n += nwritten;
20656 }
20657 }
20658 break;
20659 }
20660 }
20661 else /* c == 0 */
20662 break;
20663 }
20664 }
20665 }
20666 break;
20667
20668 case Lisp_Symbol:
20669 /* A symbol: process the value of the symbol recursively
20670 as if it appeared here directly. Avoid error if symbol void.
20671 Special case: if value of symbol is a string, output the string
20672 literally. */
20673 {
20674 register Lisp_Object tem;
20675
20676 /* If the variable is not marked as risky to set
20677 then its contents are risky to use. */
20678 if (NILP (Fget (elt, Qrisky_local_variable)))
20679 risky = 1;
20680
20681 tem = Fboundp (elt);
20682 if (!NILP (tem))
20683 {
20684 tem = Fsymbol_value (elt);
20685 /* If value is a string, output that string literally:
20686 don't check for % within it. */
20687 if (STRINGP (tem))
20688 literal = 1;
20689
20690 if (!EQ (tem, elt))
20691 {
20692 /* Give up right away for nil or t. */
20693 elt = tem;
20694 goto tail_recurse;
20695 }
20696 }
20697 }
20698 break;
20699
20700 case Lisp_Cons:
20701 {
20702 register Lisp_Object car, tem;
20703
20704 /* A cons cell: five distinct cases.
20705 If first element is :eval or :propertize, do something special.
20706 If first element is a string or a cons, process all the elements
20707 and effectively concatenate them.
20708 If first element is a negative number, truncate displaying cdr to
20709 at most that many characters. If positive, pad (with spaces)
20710 to at least that many characters.
20711 If first element is a symbol, process the cadr or caddr recursively
20712 according to whether the symbol's value is non-nil or nil. */
20713 car = XCAR (elt);
20714 if (EQ (car, QCeval))
20715 {
20716 /* An element of the form (:eval FORM) means evaluate FORM
20717 and use the result as mode line elements. */
20718
20719 if (risky)
20720 break;
20721
20722 if (CONSP (XCDR (elt)))
20723 {
20724 Lisp_Object spec;
20725 spec = safe_eval (XCAR (XCDR (elt)));
20726 n += display_mode_element (it, depth, field_width - n,
20727 precision - n, spec, props,
20728 risky);
20729 }
20730 }
20731 else if (EQ (car, QCpropertize))
20732 {
20733 /* An element of the form (:propertize ELT PROPS...)
20734 means display ELT but applying properties PROPS. */
20735
20736 if (risky)
20737 break;
20738
20739 if (CONSP (XCDR (elt)))
20740 n += display_mode_element (it, depth, field_width - n,
20741 precision - n, XCAR (XCDR (elt)),
20742 XCDR (XCDR (elt)), risky);
20743 }
20744 else if (SYMBOLP (car))
20745 {
20746 tem = Fboundp (car);
20747 elt = XCDR (elt);
20748 if (!CONSP (elt))
20749 goto invalid;
20750 /* elt is now the cdr, and we know it is a cons cell.
20751 Use its car if CAR has a non-nil value. */
20752 if (!NILP (tem))
20753 {
20754 tem = Fsymbol_value (car);
20755 if (!NILP (tem))
20756 {
20757 elt = XCAR (elt);
20758 goto tail_recurse;
20759 }
20760 }
20761 /* Symbol's value is nil (or symbol is unbound)
20762 Get the cddr of the original list
20763 and if possible find the caddr and use that. */
20764 elt = XCDR (elt);
20765 if (NILP (elt))
20766 break;
20767 else if (!CONSP (elt))
20768 goto invalid;
20769 elt = XCAR (elt);
20770 goto tail_recurse;
20771 }
20772 else if (INTEGERP (car))
20773 {
20774 register int lim = XINT (car);
20775 elt = XCDR (elt);
20776 if (lim < 0)
20777 {
20778 /* Negative int means reduce maximum width. */
20779 if (precision <= 0)
20780 precision = -lim;
20781 else
20782 precision = min (precision, -lim);
20783 }
20784 else if (lim > 0)
20785 {
20786 /* Padding specified. Don't let it be more than
20787 current maximum. */
20788 if (precision > 0)
20789 lim = min (precision, lim);
20790
20791 /* If that's more padding than already wanted, queue it.
20792 But don't reduce padding already specified even if
20793 that is beyond the current truncation point. */
20794 field_width = max (lim, field_width);
20795 }
20796 goto tail_recurse;
20797 }
20798 else if (STRINGP (car) || CONSP (car))
20799 {
20800 Lisp_Object halftail = elt;
20801 int len = 0;
20802
20803 while (CONSP (elt)
20804 && (precision <= 0 || n < precision))
20805 {
20806 n += display_mode_element (it, depth,
20807 /* Do padding only after the last
20808 element in the list. */
20809 (! CONSP (XCDR (elt))
20810 ? field_width - n
20811 : 0),
20812 precision - n, XCAR (elt),
20813 props, risky);
20814 elt = XCDR (elt);
20815 len++;
20816 if ((len & 1) == 0)
20817 halftail = XCDR (halftail);
20818 /* Check for cycle. */
20819 if (EQ (halftail, elt))
20820 break;
20821 }
20822 }
20823 }
20824 break;
20825
20826 default:
20827 invalid:
20828 elt = build_string ("*invalid*");
20829 goto tail_recurse;
20830 }
20831
20832 /* Pad to FIELD_WIDTH. */
20833 if (field_width > 0 && n < field_width)
20834 {
20835 switch (mode_line_target)
20836 {
20837 case MODE_LINE_NOPROP:
20838 case MODE_LINE_TITLE:
20839 n += store_mode_line_noprop ("", field_width - n, 0);
20840 break;
20841 case MODE_LINE_STRING:
20842 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20843 break;
20844 case MODE_LINE_DISPLAY:
20845 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20846 0, 0, 0);
20847 break;
20848 }
20849 }
20850
20851 return n;
20852 }
20853
20854 /* Store a mode-line string element in mode_line_string_list.
20855
20856 If STRING is non-null, display that C string. Otherwise, the Lisp
20857 string LISP_STRING is displayed.
20858
20859 FIELD_WIDTH is the minimum number of output glyphs to produce.
20860 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20861 with spaces. FIELD_WIDTH <= 0 means don't pad.
20862
20863 PRECISION is the maximum number of characters to output from
20864 STRING. PRECISION <= 0 means don't truncate the string.
20865
20866 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20867 properties to the string.
20868
20869 PROPS are the properties to add to the string.
20870 The mode_line_string_face face property is always added to the string.
20871 */
20872
20873 static int
20874 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20875 int field_width, int precision, Lisp_Object props)
20876 {
20877 ptrdiff_t len;
20878 int n = 0;
20879
20880 if (string != NULL)
20881 {
20882 len = strlen (string);
20883 if (precision > 0 && len > precision)
20884 len = precision;
20885 lisp_string = make_string (string, len);
20886 if (NILP (props))
20887 props = mode_line_string_face_prop;
20888 else if (!NILP (mode_line_string_face))
20889 {
20890 Lisp_Object face = Fplist_get (props, Qface);
20891 props = Fcopy_sequence (props);
20892 if (NILP (face))
20893 face = mode_line_string_face;
20894 else
20895 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20896 props = Fplist_put (props, Qface, face);
20897 }
20898 Fadd_text_properties (make_number (0), make_number (len),
20899 props, lisp_string);
20900 }
20901 else
20902 {
20903 len = XFASTINT (Flength (lisp_string));
20904 if (precision > 0 && len > precision)
20905 {
20906 len = precision;
20907 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20908 precision = -1;
20909 }
20910 if (!NILP (mode_line_string_face))
20911 {
20912 Lisp_Object face;
20913 if (NILP (props))
20914 props = Ftext_properties_at (make_number (0), lisp_string);
20915 face = Fplist_get (props, Qface);
20916 if (NILP (face))
20917 face = mode_line_string_face;
20918 else
20919 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20920 props = Fcons (Qface, Fcons (face, Qnil));
20921 if (copy_string)
20922 lisp_string = Fcopy_sequence (lisp_string);
20923 }
20924 if (!NILP (props))
20925 Fadd_text_properties (make_number (0), make_number (len),
20926 props, lisp_string);
20927 }
20928
20929 if (len > 0)
20930 {
20931 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20932 n += len;
20933 }
20934
20935 if (field_width > len)
20936 {
20937 field_width -= len;
20938 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20939 if (!NILP (props))
20940 Fadd_text_properties (make_number (0), make_number (field_width),
20941 props, lisp_string);
20942 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20943 n += field_width;
20944 }
20945
20946 return n;
20947 }
20948
20949
20950 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20951 1, 4, 0,
20952 doc: /* Format a string out of a mode line format specification.
20953 First arg FORMAT specifies the mode line format (see `mode-line-format'
20954 for details) to use.
20955
20956 By default, the format is evaluated for the currently selected window.
20957
20958 Optional second arg FACE specifies the face property to put on all
20959 characters for which no face is specified. The value nil means the
20960 default face. The value t means whatever face the window's mode line
20961 currently uses (either `mode-line' or `mode-line-inactive',
20962 depending on whether the window is the selected window or not).
20963 An integer value means the value string has no text
20964 properties.
20965
20966 Optional third and fourth args WINDOW and BUFFER specify the window
20967 and buffer to use as the context for the formatting (defaults
20968 are the selected window and the WINDOW's buffer). */)
20969 (Lisp_Object format, Lisp_Object face,
20970 Lisp_Object window, Lisp_Object buffer)
20971 {
20972 struct it it;
20973 int len;
20974 struct window *w;
20975 struct buffer *old_buffer = NULL;
20976 int face_id;
20977 int no_props = INTEGERP (face);
20978 ptrdiff_t count = SPECPDL_INDEX ();
20979 Lisp_Object str;
20980 int string_start = 0;
20981
20982 w = decode_any_window (window);
20983 XSETWINDOW (window, w);
20984
20985 if (NILP (buffer))
20986 buffer = w->buffer;
20987 CHECK_BUFFER (buffer);
20988
20989 /* Make formatting the modeline a non-op when noninteractive, otherwise
20990 there will be problems later caused by a partially initialized frame. */
20991 if (NILP (format) || noninteractive)
20992 return empty_unibyte_string;
20993
20994 if (no_props)
20995 face = Qnil;
20996
20997 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20998 : EQ (face, Qt) ? (EQ (window, selected_window)
20999 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21000 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21001 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21002 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21003 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21004 : DEFAULT_FACE_ID;
21005
21006 old_buffer = current_buffer;
21007
21008 /* Save things including mode_line_proptrans_alist,
21009 and set that to nil so that we don't alter the outer value. */
21010 record_unwind_protect (unwind_format_mode_line,
21011 format_mode_line_unwind_data
21012 (XFRAME (WINDOW_FRAME (w)),
21013 old_buffer, selected_window, 1));
21014 mode_line_proptrans_alist = Qnil;
21015
21016 Fselect_window (window, Qt);
21017 set_buffer_internal_1 (XBUFFER (buffer));
21018
21019 init_iterator (&it, w, -1, -1, NULL, face_id);
21020
21021 if (no_props)
21022 {
21023 mode_line_target = MODE_LINE_NOPROP;
21024 mode_line_string_face_prop = Qnil;
21025 mode_line_string_list = Qnil;
21026 string_start = MODE_LINE_NOPROP_LEN (0);
21027 }
21028 else
21029 {
21030 mode_line_target = MODE_LINE_STRING;
21031 mode_line_string_list = Qnil;
21032 mode_line_string_face = face;
21033 mode_line_string_face_prop
21034 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21035 }
21036
21037 push_kboard (FRAME_KBOARD (it.f));
21038 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21039 pop_kboard ();
21040
21041 if (no_props)
21042 {
21043 len = MODE_LINE_NOPROP_LEN (string_start);
21044 str = make_string (mode_line_noprop_buf + string_start, len);
21045 }
21046 else
21047 {
21048 mode_line_string_list = Fnreverse (mode_line_string_list);
21049 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21050 empty_unibyte_string);
21051 }
21052
21053 unbind_to (count, Qnil);
21054 return str;
21055 }
21056
21057 /* Write a null-terminated, right justified decimal representation of
21058 the positive integer D to BUF using a minimal field width WIDTH. */
21059
21060 static void
21061 pint2str (register char *buf, register int width, register ptrdiff_t d)
21062 {
21063 register char *p = buf;
21064
21065 if (d <= 0)
21066 *p++ = '0';
21067 else
21068 {
21069 while (d > 0)
21070 {
21071 *p++ = d % 10 + '0';
21072 d /= 10;
21073 }
21074 }
21075
21076 for (width -= (int) (p - buf); width > 0; --width)
21077 *p++ = ' ';
21078 *p-- = '\0';
21079 while (p > buf)
21080 {
21081 d = *buf;
21082 *buf++ = *p;
21083 *p-- = d;
21084 }
21085 }
21086
21087 /* Write a null-terminated, right justified decimal and "human
21088 readable" representation of the nonnegative integer D to BUF using
21089 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21090
21091 static const char power_letter[] =
21092 {
21093 0, /* no letter */
21094 'k', /* kilo */
21095 'M', /* mega */
21096 'G', /* giga */
21097 'T', /* tera */
21098 'P', /* peta */
21099 'E', /* exa */
21100 'Z', /* zetta */
21101 'Y' /* yotta */
21102 };
21103
21104 static void
21105 pint2hrstr (char *buf, int width, ptrdiff_t d)
21106 {
21107 /* We aim to represent the nonnegative integer D as
21108 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21109 ptrdiff_t quotient = d;
21110 int remainder = 0;
21111 /* -1 means: do not use TENTHS. */
21112 int tenths = -1;
21113 int exponent = 0;
21114
21115 /* Length of QUOTIENT.TENTHS as a string. */
21116 int length;
21117
21118 char * psuffix;
21119 char * p;
21120
21121 if (1000 <= quotient)
21122 {
21123 /* Scale to the appropriate EXPONENT. */
21124 do
21125 {
21126 remainder = quotient % 1000;
21127 quotient /= 1000;
21128 exponent++;
21129 }
21130 while (1000 <= quotient);
21131
21132 /* Round to nearest and decide whether to use TENTHS or not. */
21133 if (quotient <= 9)
21134 {
21135 tenths = remainder / 100;
21136 if (50 <= remainder % 100)
21137 {
21138 if (tenths < 9)
21139 tenths++;
21140 else
21141 {
21142 quotient++;
21143 if (quotient == 10)
21144 tenths = -1;
21145 else
21146 tenths = 0;
21147 }
21148 }
21149 }
21150 else
21151 if (500 <= remainder)
21152 {
21153 if (quotient < 999)
21154 quotient++;
21155 else
21156 {
21157 quotient = 1;
21158 exponent++;
21159 tenths = 0;
21160 }
21161 }
21162 }
21163
21164 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21165 if (tenths == -1 && quotient <= 99)
21166 if (quotient <= 9)
21167 length = 1;
21168 else
21169 length = 2;
21170 else
21171 length = 3;
21172 p = psuffix = buf + max (width, length);
21173
21174 /* Print EXPONENT. */
21175 *psuffix++ = power_letter[exponent];
21176 *psuffix = '\0';
21177
21178 /* Print TENTHS. */
21179 if (tenths >= 0)
21180 {
21181 *--p = '0' + tenths;
21182 *--p = '.';
21183 }
21184
21185 /* Print QUOTIENT. */
21186 do
21187 {
21188 int digit = quotient % 10;
21189 *--p = '0' + digit;
21190 }
21191 while ((quotient /= 10) != 0);
21192
21193 /* Print leading spaces. */
21194 while (buf < p)
21195 *--p = ' ';
21196 }
21197
21198 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21199 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21200 type of CODING_SYSTEM. Return updated pointer into BUF. */
21201
21202 static unsigned char invalid_eol_type[] = "(*invalid*)";
21203
21204 static char *
21205 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21206 {
21207 Lisp_Object val;
21208 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21209 const unsigned char *eol_str;
21210 int eol_str_len;
21211 /* The EOL conversion we are using. */
21212 Lisp_Object eoltype;
21213
21214 val = CODING_SYSTEM_SPEC (coding_system);
21215 eoltype = Qnil;
21216
21217 if (!VECTORP (val)) /* Not yet decided. */
21218 {
21219 *buf++ = multibyte ? '-' : ' ';
21220 if (eol_flag)
21221 eoltype = eol_mnemonic_undecided;
21222 /* Don't mention EOL conversion if it isn't decided. */
21223 }
21224 else
21225 {
21226 Lisp_Object attrs;
21227 Lisp_Object eolvalue;
21228
21229 attrs = AREF (val, 0);
21230 eolvalue = AREF (val, 2);
21231
21232 *buf++ = multibyte
21233 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21234 : ' ';
21235
21236 if (eol_flag)
21237 {
21238 /* The EOL conversion that is normal on this system. */
21239
21240 if (NILP (eolvalue)) /* Not yet decided. */
21241 eoltype = eol_mnemonic_undecided;
21242 else if (VECTORP (eolvalue)) /* Not yet decided. */
21243 eoltype = eol_mnemonic_undecided;
21244 else /* eolvalue is Qunix, Qdos, or Qmac. */
21245 eoltype = (EQ (eolvalue, Qunix)
21246 ? eol_mnemonic_unix
21247 : (EQ (eolvalue, Qdos) == 1
21248 ? eol_mnemonic_dos : eol_mnemonic_mac));
21249 }
21250 }
21251
21252 if (eol_flag)
21253 {
21254 /* Mention the EOL conversion if it is not the usual one. */
21255 if (STRINGP (eoltype))
21256 {
21257 eol_str = SDATA (eoltype);
21258 eol_str_len = SBYTES (eoltype);
21259 }
21260 else if (CHARACTERP (eoltype))
21261 {
21262 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21263 int c = XFASTINT (eoltype);
21264 eol_str_len = CHAR_STRING (c, tmp);
21265 eol_str = tmp;
21266 }
21267 else
21268 {
21269 eol_str = invalid_eol_type;
21270 eol_str_len = sizeof (invalid_eol_type) - 1;
21271 }
21272 memcpy (buf, eol_str, eol_str_len);
21273 buf += eol_str_len;
21274 }
21275
21276 return buf;
21277 }
21278
21279 /* Return a string for the output of a mode line %-spec for window W,
21280 generated by character C. FIELD_WIDTH > 0 means pad the string
21281 returned with spaces to that value. Return a Lisp string in
21282 *STRING if the resulting string is taken from that Lisp string.
21283
21284 Note we operate on the current buffer for most purposes. */
21285
21286 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21287
21288 static const char *
21289 decode_mode_spec (struct window *w, register int c, int field_width,
21290 Lisp_Object *string)
21291 {
21292 Lisp_Object obj;
21293 struct frame *f = XFRAME (WINDOW_FRAME (w));
21294 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21295 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21296 produce strings from numerical values, so limit preposterously
21297 large values of FIELD_WIDTH to avoid overrunning the buffer's
21298 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21299 bytes plus the terminating null. */
21300 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21301 struct buffer *b = current_buffer;
21302
21303 obj = Qnil;
21304 *string = Qnil;
21305
21306 switch (c)
21307 {
21308 case '*':
21309 if (!NILP (BVAR (b, read_only)))
21310 return "%";
21311 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21312 return "*";
21313 return "-";
21314
21315 case '+':
21316 /* This differs from %* only for a modified read-only buffer. */
21317 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21318 return "*";
21319 if (!NILP (BVAR (b, read_only)))
21320 return "%";
21321 return "-";
21322
21323 case '&':
21324 /* This differs from %* in ignoring read-only-ness. */
21325 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21326 return "*";
21327 return "-";
21328
21329 case '%':
21330 return "%";
21331
21332 case '[':
21333 {
21334 int i;
21335 char *p;
21336
21337 if (command_loop_level > 5)
21338 return "[[[... ";
21339 p = decode_mode_spec_buf;
21340 for (i = 0; i < command_loop_level; i++)
21341 *p++ = '[';
21342 *p = 0;
21343 return decode_mode_spec_buf;
21344 }
21345
21346 case ']':
21347 {
21348 int i;
21349 char *p;
21350
21351 if (command_loop_level > 5)
21352 return " ...]]]";
21353 p = decode_mode_spec_buf;
21354 for (i = 0; i < command_loop_level; i++)
21355 *p++ = ']';
21356 *p = 0;
21357 return decode_mode_spec_buf;
21358 }
21359
21360 case '-':
21361 {
21362 register int i;
21363
21364 /* Let lots_of_dashes be a string of infinite length. */
21365 if (mode_line_target == MODE_LINE_NOPROP
21366 || mode_line_target == MODE_LINE_STRING)
21367 return "--";
21368 if (field_width <= 0
21369 || field_width > sizeof (lots_of_dashes))
21370 {
21371 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21372 decode_mode_spec_buf[i] = '-';
21373 decode_mode_spec_buf[i] = '\0';
21374 return decode_mode_spec_buf;
21375 }
21376 else
21377 return lots_of_dashes;
21378 }
21379
21380 case 'b':
21381 obj = BVAR (b, name);
21382 break;
21383
21384 case 'c':
21385 /* %c and %l are ignored in `frame-title-format'.
21386 (In redisplay_internal, the frame title is drawn _before_ the
21387 windows are updated, so the stuff which depends on actual
21388 window contents (such as %l) may fail to render properly, or
21389 even crash emacs.) */
21390 if (mode_line_target == MODE_LINE_TITLE)
21391 return "";
21392 else
21393 {
21394 ptrdiff_t col = current_column ();
21395 w->column_number_displayed = col;
21396 pint2str (decode_mode_spec_buf, width, col);
21397 return decode_mode_spec_buf;
21398 }
21399
21400 case 'e':
21401 #ifndef SYSTEM_MALLOC
21402 {
21403 if (NILP (Vmemory_full))
21404 return "";
21405 else
21406 return "!MEM FULL! ";
21407 }
21408 #else
21409 return "";
21410 #endif
21411
21412 case 'F':
21413 /* %F displays the frame name. */
21414 if (!NILP (f->title))
21415 return SSDATA (f->title);
21416 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21417 return SSDATA (f->name);
21418 return "Emacs";
21419
21420 case 'f':
21421 obj = BVAR (b, filename);
21422 break;
21423
21424 case 'i':
21425 {
21426 ptrdiff_t size = ZV - BEGV;
21427 pint2str (decode_mode_spec_buf, width, size);
21428 return decode_mode_spec_buf;
21429 }
21430
21431 case 'I':
21432 {
21433 ptrdiff_t size = ZV - BEGV;
21434 pint2hrstr (decode_mode_spec_buf, width, size);
21435 return decode_mode_spec_buf;
21436 }
21437
21438 case 'l':
21439 {
21440 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21441 ptrdiff_t topline, nlines, height;
21442 ptrdiff_t junk;
21443
21444 /* %c and %l are ignored in `frame-title-format'. */
21445 if (mode_line_target == MODE_LINE_TITLE)
21446 return "";
21447
21448 startpos = marker_position (w->start);
21449 startpos_byte = marker_byte_position (w->start);
21450 height = WINDOW_TOTAL_LINES (w);
21451
21452 /* If we decided that this buffer isn't suitable for line numbers,
21453 don't forget that too fast. */
21454 if (w->base_line_pos == -1)
21455 goto no_value;
21456
21457 /* If the buffer is very big, don't waste time. */
21458 if (INTEGERP (Vline_number_display_limit)
21459 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21460 {
21461 w->base_line_pos = 0;
21462 w->base_line_number = 0;
21463 goto no_value;
21464 }
21465
21466 if (w->base_line_number > 0
21467 && w->base_line_pos > 0
21468 && w->base_line_pos <= startpos)
21469 {
21470 line = w->base_line_number;
21471 linepos = w->base_line_pos;
21472 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21473 }
21474 else
21475 {
21476 line = 1;
21477 linepos = BUF_BEGV (b);
21478 linepos_byte = BUF_BEGV_BYTE (b);
21479 }
21480
21481 /* Count lines from base line to window start position. */
21482 nlines = display_count_lines (linepos_byte,
21483 startpos_byte,
21484 startpos, &junk);
21485
21486 topline = nlines + line;
21487
21488 /* Determine a new base line, if the old one is too close
21489 or too far away, or if we did not have one.
21490 "Too close" means it's plausible a scroll-down would
21491 go back past it. */
21492 if (startpos == BUF_BEGV (b))
21493 {
21494 w->base_line_number = topline;
21495 w->base_line_pos = BUF_BEGV (b);
21496 }
21497 else if (nlines < height + 25 || nlines > height * 3 + 50
21498 || linepos == BUF_BEGV (b))
21499 {
21500 ptrdiff_t limit = BUF_BEGV (b);
21501 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21502 ptrdiff_t position;
21503 ptrdiff_t distance =
21504 (height * 2 + 30) * line_number_display_limit_width;
21505
21506 if (startpos - distance > limit)
21507 {
21508 limit = startpos - distance;
21509 limit_byte = CHAR_TO_BYTE (limit);
21510 }
21511
21512 nlines = display_count_lines (startpos_byte,
21513 limit_byte,
21514 - (height * 2 + 30),
21515 &position);
21516 /* If we couldn't find the lines we wanted within
21517 line_number_display_limit_width chars per line,
21518 give up on line numbers for this window. */
21519 if (position == limit_byte && limit == startpos - distance)
21520 {
21521 w->base_line_pos = -1;
21522 w->base_line_number = 0;
21523 goto no_value;
21524 }
21525
21526 w->base_line_number = topline - nlines;
21527 w->base_line_pos = BYTE_TO_CHAR (position);
21528 }
21529
21530 /* Now count lines from the start pos to point. */
21531 nlines = display_count_lines (startpos_byte,
21532 PT_BYTE, PT, &junk);
21533
21534 /* Record that we did display the line number. */
21535 line_number_displayed = 1;
21536
21537 /* Make the string to show. */
21538 pint2str (decode_mode_spec_buf, width, topline + nlines);
21539 return decode_mode_spec_buf;
21540 no_value:
21541 {
21542 char* p = decode_mode_spec_buf;
21543 int pad = width - 2;
21544 while (pad-- > 0)
21545 *p++ = ' ';
21546 *p++ = '?';
21547 *p++ = '?';
21548 *p = '\0';
21549 return decode_mode_spec_buf;
21550 }
21551 }
21552 break;
21553
21554 case 'm':
21555 obj = BVAR (b, mode_name);
21556 break;
21557
21558 case 'n':
21559 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21560 return " Narrow";
21561 break;
21562
21563 case 'p':
21564 {
21565 ptrdiff_t pos = marker_position (w->start);
21566 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21567
21568 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21569 {
21570 if (pos <= BUF_BEGV (b))
21571 return "All";
21572 else
21573 return "Bottom";
21574 }
21575 else if (pos <= BUF_BEGV (b))
21576 return "Top";
21577 else
21578 {
21579 if (total > 1000000)
21580 /* Do it differently for a large value, to avoid overflow. */
21581 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21582 else
21583 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21584 /* We can't normally display a 3-digit number,
21585 so get us a 2-digit number that is close. */
21586 if (total == 100)
21587 total = 99;
21588 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21589 return decode_mode_spec_buf;
21590 }
21591 }
21592
21593 /* Display percentage of size above the bottom of the screen. */
21594 case 'P':
21595 {
21596 ptrdiff_t toppos = marker_position (w->start);
21597 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21598 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21599
21600 if (botpos >= BUF_ZV (b))
21601 {
21602 if (toppos <= BUF_BEGV (b))
21603 return "All";
21604 else
21605 return "Bottom";
21606 }
21607 else
21608 {
21609 if (total > 1000000)
21610 /* Do it differently for a large value, to avoid overflow. */
21611 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21612 else
21613 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21614 /* We can't normally display a 3-digit number,
21615 so get us a 2-digit number that is close. */
21616 if (total == 100)
21617 total = 99;
21618 if (toppos <= BUF_BEGV (b))
21619 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21620 else
21621 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21622 return decode_mode_spec_buf;
21623 }
21624 }
21625
21626 case 's':
21627 /* status of process */
21628 obj = Fget_buffer_process (Fcurrent_buffer ());
21629 if (NILP (obj))
21630 return "no process";
21631 #ifndef MSDOS
21632 obj = Fsymbol_name (Fprocess_status (obj));
21633 #endif
21634 break;
21635
21636 case '@':
21637 {
21638 ptrdiff_t count = inhibit_garbage_collection ();
21639 Lisp_Object val = call1 (intern ("file-remote-p"),
21640 BVAR (current_buffer, directory));
21641 unbind_to (count, Qnil);
21642
21643 if (NILP (val))
21644 return "-";
21645 else
21646 return "@";
21647 }
21648
21649 case 'z':
21650 /* coding-system (not including end-of-line format) */
21651 case 'Z':
21652 /* coding-system (including end-of-line type) */
21653 {
21654 int eol_flag = (c == 'Z');
21655 char *p = decode_mode_spec_buf;
21656
21657 if (! FRAME_WINDOW_P (f))
21658 {
21659 /* No need to mention EOL here--the terminal never needs
21660 to do EOL conversion. */
21661 p = decode_mode_spec_coding (CODING_ID_NAME
21662 (FRAME_KEYBOARD_CODING (f)->id),
21663 p, 0);
21664 p = decode_mode_spec_coding (CODING_ID_NAME
21665 (FRAME_TERMINAL_CODING (f)->id),
21666 p, 0);
21667 }
21668 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21669 p, eol_flag);
21670
21671 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21672 #ifdef subprocesses
21673 obj = Fget_buffer_process (Fcurrent_buffer ());
21674 if (PROCESSP (obj))
21675 {
21676 p = decode_mode_spec_coding
21677 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21678 p = decode_mode_spec_coding
21679 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21680 }
21681 #endif /* subprocesses */
21682 #endif /* 0 */
21683 *p = 0;
21684 return decode_mode_spec_buf;
21685 }
21686 }
21687
21688 if (STRINGP (obj))
21689 {
21690 *string = obj;
21691 return SSDATA (obj);
21692 }
21693 else
21694 return "";
21695 }
21696
21697
21698 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21699 means count lines back from START_BYTE. But don't go beyond
21700 LIMIT_BYTE. Return the number of lines thus found (always
21701 nonnegative).
21702
21703 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21704 either the position COUNT lines after/before START_BYTE, if we
21705 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21706 COUNT lines. */
21707
21708 static ptrdiff_t
21709 display_count_lines (ptrdiff_t start_byte,
21710 ptrdiff_t limit_byte, ptrdiff_t count,
21711 ptrdiff_t *byte_pos_ptr)
21712 {
21713 register unsigned char *cursor;
21714 unsigned char *base;
21715
21716 register ptrdiff_t ceiling;
21717 register unsigned char *ceiling_addr;
21718 ptrdiff_t orig_count = count;
21719
21720 /* If we are not in selective display mode,
21721 check only for newlines. */
21722 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21723 && !INTEGERP (BVAR (current_buffer, selective_display)));
21724
21725 if (count > 0)
21726 {
21727 while (start_byte < limit_byte)
21728 {
21729 ceiling = BUFFER_CEILING_OF (start_byte);
21730 ceiling = min (limit_byte - 1, ceiling);
21731 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21732 base = (cursor = BYTE_POS_ADDR (start_byte));
21733
21734 do
21735 {
21736 if (selective_display)
21737 {
21738 while (*cursor != '\n' && *cursor != 015
21739 && ++cursor != ceiling_addr)
21740 continue;
21741 if (cursor == ceiling_addr)
21742 break;
21743 }
21744 else
21745 {
21746 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
21747 if (! cursor)
21748 break;
21749 }
21750
21751 cursor++;
21752
21753 if (--count == 0)
21754 {
21755 start_byte += cursor - base;
21756 *byte_pos_ptr = start_byte;
21757 return orig_count;
21758 }
21759 }
21760 while (cursor < ceiling_addr);
21761
21762 start_byte += ceiling_addr - base;
21763 }
21764 }
21765 else
21766 {
21767 while (start_byte > limit_byte)
21768 {
21769 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21770 ceiling = max (limit_byte, ceiling);
21771 ceiling_addr = BYTE_POS_ADDR (ceiling);
21772 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21773 while (1)
21774 {
21775 if (selective_display)
21776 {
21777 while (--cursor >= ceiling_addr
21778 && *cursor != '\n' && *cursor != 015)
21779 continue;
21780 if (cursor < ceiling_addr)
21781 break;
21782 }
21783 else
21784 {
21785 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
21786 if (! cursor)
21787 break;
21788 }
21789
21790 if (++count == 0)
21791 {
21792 start_byte += cursor - base + 1;
21793 *byte_pos_ptr = start_byte;
21794 /* When scanning backwards, we should
21795 not count the newline posterior to which we stop. */
21796 return - orig_count - 1;
21797 }
21798 }
21799 start_byte += ceiling_addr - base;
21800 }
21801 }
21802
21803 *byte_pos_ptr = limit_byte;
21804
21805 if (count < 0)
21806 return - orig_count + count;
21807 return orig_count - count;
21808
21809 }
21810
21811
21812 \f
21813 /***********************************************************************
21814 Displaying strings
21815 ***********************************************************************/
21816
21817 /* Display a NUL-terminated string, starting with index START.
21818
21819 If STRING is non-null, display that C string. Otherwise, the Lisp
21820 string LISP_STRING is displayed. There's a case that STRING is
21821 non-null and LISP_STRING is not nil. It means STRING is a string
21822 data of LISP_STRING. In that case, we display LISP_STRING while
21823 ignoring its text properties.
21824
21825 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21826 FACE_STRING. Display STRING or LISP_STRING with the face at
21827 FACE_STRING_POS in FACE_STRING:
21828
21829 Display the string in the environment given by IT, but use the
21830 standard display table, temporarily.
21831
21832 FIELD_WIDTH is the minimum number of output glyphs to produce.
21833 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21834 with spaces. If STRING has more characters, more than FIELD_WIDTH
21835 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21836
21837 PRECISION is the maximum number of characters to output from
21838 STRING. PRECISION < 0 means don't truncate the string.
21839
21840 This is roughly equivalent to printf format specifiers:
21841
21842 FIELD_WIDTH PRECISION PRINTF
21843 ----------------------------------------
21844 -1 -1 %s
21845 -1 10 %.10s
21846 10 -1 %10s
21847 20 10 %20.10s
21848
21849 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21850 display them, and < 0 means obey the current buffer's value of
21851 enable_multibyte_characters.
21852
21853 Value is the number of columns displayed. */
21854
21855 static int
21856 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21857 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21858 int field_width, int precision, int max_x, int multibyte)
21859 {
21860 int hpos_at_start = it->hpos;
21861 int saved_face_id = it->face_id;
21862 struct glyph_row *row = it->glyph_row;
21863 ptrdiff_t it_charpos;
21864
21865 /* Initialize the iterator IT for iteration over STRING beginning
21866 with index START. */
21867 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21868 precision, field_width, multibyte);
21869 if (string && STRINGP (lisp_string))
21870 /* LISP_STRING is the one returned by decode_mode_spec. We should
21871 ignore its text properties. */
21872 it->stop_charpos = it->end_charpos;
21873
21874 /* If displaying STRING, set up the face of the iterator from
21875 FACE_STRING, if that's given. */
21876 if (STRINGP (face_string))
21877 {
21878 ptrdiff_t endptr;
21879 struct face *face;
21880
21881 it->face_id
21882 = face_at_string_position (it->w, face_string, face_string_pos,
21883 0, it->region_beg_charpos,
21884 it->region_end_charpos,
21885 &endptr, it->base_face_id, 0);
21886 face = FACE_FROM_ID (it->f, it->face_id);
21887 it->face_box_p = face->box != FACE_NO_BOX;
21888 }
21889
21890 /* Set max_x to the maximum allowed X position. Don't let it go
21891 beyond the right edge of the window. */
21892 if (max_x <= 0)
21893 max_x = it->last_visible_x;
21894 else
21895 max_x = min (max_x, it->last_visible_x);
21896
21897 /* Skip over display elements that are not visible. because IT->w is
21898 hscrolled. */
21899 if (it->current_x < it->first_visible_x)
21900 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21901 MOVE_TO_POS | MOVE_TO_X);
21902
21903 row->ascent = it->max_ascent;
21904 row->height = it->max_ascent + it->max_descent;
21905 row->phys_ascent = it->max_phys_ascent;
21906 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21907 row->extra_line_spacing = it->max_extra_line_spacing;
21908
21909 if (STRINGP (it->string))
21910 it_charpos = IT_STRING_CHARPOS (*it);
21911 else
21912 it_charpos = IT_CHARPOS (*it);
21913
21914 /* This condition is for the case that we are called with current_x
21915 past last_visible_x. */
21916 while (it->current_x < max_x)
21917 {
21918 int x_before, x, n_glyphs_before, i, nglyphs;
21919
21920 /* Get the next display element. */
21921 if (!get_next_display_element (it))
21922 break;
21923
21924 /* Produce glyphs. */
21925 x_before = it->current_x;
21926 n_glyphs_before = row->used[TEXT_AREA];
21927 PRODUCE_GLYPHS (it);
21928
21929 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21930 i = 0;
21931 x = x_before;
21932 while (i < nglyphs)
21933 {
21934 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21935
21936 if (it->line_wrap != TRUNCATE
21937 && x + glyph->pixel_width > max_x)
21938 {
21939 /* End of continued line or max_x reached. */
21940 if (CHAR_GLYPH_PADDING_P (*glyph))
21941 {
21942 /* A wide character is unbreakable. */
21943 if (row->reversed_p)
21944 unproduce_glyphs (it, row->used[TEXT_AREA]
21945 - n_glyphs_before);
21946 row->used[TEXT_AREA] = n_glyphs_before;
21947 it->current_x = x_before;
21948 }
21949 else
21950 {
21951 if (row->reversed_p)
21952 unproduce_glyphs (it, row->used[TEXT_AREA]
21953 - (n_glyphs_before + i));
21954 row->used[TEXT_AREA] = n_glyphs_before + i;
21955 it->current_x = x;
21956 }
21957 break;
21958 }
21959 else if (x + glyph->pixel_width >= it->first_visible_x)
21960 {
21961 /* Glyph is at least partially visible. */
21962 ++it->hpos;
21963 if (x < it->first_visible_x)
21964 row->x = x - it->first_visible_x;
21965 }
21966 else
21967 {
21968 /* Glyph is off the left margin of the display area.
21969 Should not happen. */
21970 emacs_abort ();
21971 }
21972
21973 row->ascent = max (row->ascent, it->max_ascent);
21974 row->height = max (row->height, it->max_ascent + it->max_descent);
21975 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21976 row->phys_height = max (row->phys_height,
21977 it->max_phys_ascent + it->max_phys_descent);
21978 row->extra_line_spacing = max (row->extra_line_spacing,
21979 it->max_extra_line_spacing);
21980 x += glyph->pixel_width;
21981 ++i;
21982 }
21983
21984 /* Stop if max_x reached. */
21985 if (i < nglyphs)
21986 break;
21987
21988 /* Stop at line ends. */
21989 if (ITERATOR_AT_END_OF_LINE_P (it))
21990 {
21991 it->continuation_lines_width = 0;
21992 break;
21993 }
21994
21995 set_iterator_to_next (it, 1);
21996 if (STRINGP (it->string))
21997 it_charpos = IT_STRING_CHARPOS (*it);
21998 else
21999 it_charpos = IT_CHARPOS (*it);
22000
22001 /* Stop if truncating at the right edge. */
22002 if (it->line_wrap == TRUNCATE
22003 && it->current_x >= it->last_visible_x)
22004 {
22005 /* Add truncation mark, but don't do it if the line is
22006 truncated at a padding space. */
22007 if (it_charpos < it->string_nchars)
22008 {
22009 if (!FRAME_WINDOW_P (it->f))
22010 {
22011 int ii, n;
22012
22013 if (it->current_x > it->last_visible_x)
22014 {
22015 if (!row->reversed_p)
22016 {
22017 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22018 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22019 break;
22020 }
22021 else
22022 {
22023 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22024 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22025 break;
22026 unproduce_glyphs (it, ii + 1);
22027 ii = row->used[TEXT_AREA] - (ii + 1);
22028 }
22029 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22030 {
22031 row->used[TEXT_AREA] = ii;
22032 produce_special_glyphs (it, IT_TRUNCATION);
22033 }
22034 }
22035 produce_special_glyphs (it, IT_TRUNCATION);
22036 }
22037 row->truncated_on_right_p = 1;
22038 }
22039 break;
22040 }
22041 }
22042
22043 /* Maybe insert a truncation at the left. */
22044 if (it->first_visible_x
22045 && it_charpos > 0)
22046 {
22047 if (!FRAME_WINDOW_P (it->f)
22048 || (row->reversed_p
22049 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22050 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22051 insert_left_trunc_glyphs (it);
22052 row->truncated_on_left_p = 1;
22053 }
22054
22055 it->face_id = saved_face_id;
22056
22057 /* Value is number of columns displayed. */
22058 return it->hpos - hpos_at_start;
22059 }
22060
22061
22062 \f
22063 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22064 appears as an element of LIST or as the car of an element of LIST.
22065 If PROPVAL is a list, compare each element against LIST in that
22066 way, and return 1/2 if any element of PROPVAL is found in LIST.
22067 Otherwise return 0. This function cannot quit.
22068 The return value is 2 if the text is invisible but with an ellipsis
22069 and 1 if it's invisible and without an ellipsis. */
22070
22071 int
22072 invisible_p (register Lisp_Object propval, Lisp_Object list)
22073 {
22074 register Lisp_Object tail, proptail;
22075
22076 for (tail = list; CONSP (tail); tail = XCDR (tail))
22077 {
22078 register Lisp_Object tem;
22079 tem = XCAR (tail);
22080 if (EQ (propval, tem))
22081 return 1;
22082 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22083 return NILP (XCDR (tem)) ? 1 : 2;
22084 }
22085
22086 if (CONSP (propval))
22087 {
22088 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22089 {
22090 Lisp_Object propelt;
22091 propelt = XCAR (proptail);
22092 for (tail = list; CONSP (tail); tail = XCDR (tail))
22093 {
22094 register Lisp_Object tem;
22095 tem = XCAR (tail);
22096 if (EQ (propelt, tem))
22097 return 1;
22098 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22099 return NILP (XCDR (tem)) ? 1 : 2;
22100 }
22101 }
22102 }
22103
22104 return 0;
22105 }
22106
22107 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22108 doc: /* Non-nil if the property makes the text invisible.
22109 POS-OR-PROP can be a marker or number, in which case it is taken to be
22110 a position in the current buffer and the value of the `invisible' property
22111 is checked; or it can be some other value, which is then presumed to be the
22112 value of the `invisible' property of the text of interest.
22113 The non-nil value returned can be t for truly invisible text or something
22114 else if the text is replaced by an ellipsis. */)
22115 (Lisp_Object pos_or_prop)
22116 {
22117 Lisp_Object prop
22118 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22119 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22120 : pos_or_prop);
22121 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22122 return (invis == 0 ? Qnil
22123 : invis == 1 ? Qt
22124 : make_number (invis));
22125 }
22126
22127 /* Calculate a width or height in pixels from a specification using
22128 the following elements:
22129
22130 SPEC ::=
22131 NUM - a (fractional) multiple of the default font width/height
22132 (NUM) - specifies exactly NUM pixels
22133 UNIT - a fixed number of pixels, see below.
22134 ELEMENT - size of a display element in pixels, see below.
22135 (NUM . SPEC) - equals NUM * SPEC
22136 (+ SPEC SPEC ...) - add pixel values
22137 (- SPEC SPEC ...) - subtract pixel values
22138 (- SPEC) - negate pixel value
22139
22140 NUM ::=
22141 INT or FLOAT - a number constant
22142 SYMBOL - use symbol's (buffer local) variable binding.
22143
22144 UNIT ::=
22145 in - pixels per inch *)
22146 mm - pixels per 1/1000 meter *)
22147 cm - pixels per 1/100 meter *)
22148 width - width of current font in pixels.
22149 height - height of current font in pixels.
22150
22151 *) using the ratio(s) defined in display-pixels-per-inch.
22152
22153 ELEMENT ::=
22154
22155 left-fringe - left fringe width in pixels
22156 right-fringe - right fringe width in pixels
22157
22158 left-margin - left margin width in pixels
22159 right-margin - right margin width in pixels
22160
22161 scroll-bar - scroll-bar area width in pixels
22162
22163 Examples:
22164
22165 Pixels corresponding to 5 inches:
22166 (5 . in)
22167
22168 Total width of non-text areas on left side of window (if scroll-bar is on left):
22169 '(space :width (+ left-fringe left-margin scroll-bar))
22170
22171 Align to first text column (in header line):
22172 '(space :align-to 0)
22173
22174 Align to middle of text area minus half the width of variable `my-image'
22175 containing a loaded image:
22176 '(space :align-to (0.5 . (- text my-image)))
22177
22178 Width of left margin minus width of 1 character in the default font:
22179 '(space :width (- left-margin 1))
22180
22181 Width of left margin minus width of 2 characters in the current font:
22182 '(space :width (- left-margin (2 . width)))
22183
22184 Center 1 character over left-margin (in header line):
22185 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22186
22187 Different ways to express width of left fringe plus left margin minus one pixel:
22188 '(space :width (- (+ left-fringe left-margin) (1)))
22189 '(space :width (+ left-fringe left-margin (- (1))))
22190 '(space :width (+ left-fringe left-margin (-1)))
22191
22192 */
22193
22194 #define NUMVAL(X) \
22195 ((INTEGERP (X) || FLOATP (X)) \
22196 ? XFLOATINT (X) \
22197 : - 1)
22198
22199 static int
22200 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22201 struct font *font, int width_p, int *align_to)
22202 {
22203 double pixels;
22204
22205 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22206 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22207
22208 if (NILP (prop))
22209 return OK_PIXELS (0);
22210
22211 eassert (FRAME_LIVE_P (it->f));
22212
22213 if (SYMBOLP (prop))
22214 {
22215 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22216 {
22217 char *unit = SSDATA (SYMBOL_NAME (prop));
22218
22219 if (unit[0] == 'i' && unit[1] == 'n')
22220 pixels = 1.0;
22221 else if (unit[0] == 'm' && unit[1] == 'm')
22222 pixels = 25.4;
22223 else if (unit[0] == 'c' && unit[1] == 'm')
22224 pixels = 2.54;
22225 else
22226 pixels = 0;
22227 if (pixels > 0)
22228 {
22229 double ppi;
22230 #ifdef HAVE_WINDOW_SYSTEM
22231 if (FRAME_WINDOW_P (it->f)
22232 && (ppi = (width_p
22233 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22234 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22235 ppi > 0))
22236 return OK_PIXELS (ppi / pixels);
22237 #endif
22238
22239 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22240 || (CONSP (Vdisplay_pixels_per_inch)
22241 && (ppi = (width_p
22242 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22243 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22244 ppi > 0)))
22245 return OK_PIXELS (ppi / pixels);
22246
22247 return 0;
22248 }
22249 }
22250
22251 #ifdef HAVE_WINDOW_SYSTEM
22252 if (EQ (prop, Qheight))
22253 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22254 if (EQ (prop, Qwidth))
22255 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22256 #else
22257 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22258 return OK_PIXELS (1);
22259 #endif
22260
22261 if (EQ (prop, Qtext))
22262 return OK_PIXELS (width_p
22263 ? window_box_width (it->w, TEXT_AREA)
22264 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22265
22266 if (align_to && *align_to < 0)
22267 {
22268 *res = 0;
22269 if (EQ (prop, Qleft))
22270 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22271 if (EQ (prop, Qright))
22272 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22273 if (EQ (prop, Qcenter))
22274 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22275 + window_box_width (it->w, TEXT_AREA) / 2);
22276 if (EQ (prop, Qleft_fringe))
22277 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22278 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22279 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22280 if (EQ (prop, Qright_fringe))
22281 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22282 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22283 : window_box_right_offset (it->w, TEXT_AREA));
22284 if (EQ (prop, Qleft_margin))
22285 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22286 if (EQ (prop, Qright_margin))
22287 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22288 if (EQ (prop, Qscroll_bar))
22289 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22290 ? 0
22291 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22292 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22293 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22294 : 0)));
22295 }
22296 else
22297 {
22298 if (EQ (prop, Qleft_fringe))
22299 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22300 if (EQ (prop, Qright_fringe))
22301 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22302 if (EQ (prop, Qleft_margin))
22303 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22304 if (EQ (prop, Qright_margin))
22305 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22306 if (EQ (prop, Qscroll_bar))
22307 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22308 }
22309
22310 prop = buffer_local_value_1 (prop, it->w->buffer);
22311 if (EQ (prop, Qunbound))
22312 prop = Qnil;
22313 }
22314
22315 if (INTEGERP (prop) || FLOATP (prop))
22316 {
22317 int base_unit = (width_p
22318 ? FRAME_COLUMN_WIDTH (it->f)
22319 : FRAME_LINE_HEIGHT (it->f));
22320 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22321 }
22322
22323 if (CONSP (prop))
22324 {
22325 Lisp_Object car = XCAR (prop);
22326 Lisp_Object cdr = XCDR (prop);
22327
22328 if (SYMBOLP (car))
22329 {
22330 #ifdef HAVE_WINDOW_SYSTEM
22331 if (FRAME_WINDOW_P (it->f)
22332 && valid_image_p (prop))
22333 {
22334 ptrdiff_t id = lookup_image (it->f, prop);
22335 struct image *img = IMAGE_FROM_ID (it->f, id);
22336
22337 return OK_PIXELS (width_p ? img->width : img->height);
22338 }
22339 #ifdef HAVE_XWIDGETS
22340 if (FRAME_WINDOW_P (it->f) && valid_xwidget_p (prop))
22341 {
22342 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
22343 return OK_PIXELS (width_p ? 100 : 100);
22344 }
22345 #endif
22346 #endif
22347 if (EQ (car, Qplus) || EQ (car, Qminus))
22348 {
22349 int first = 1;
22350 double px;
22351
22352 pixels = 0;
22353 while (CONSP (cdr))
22354 {
22355 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22356 font, width_p, align_to))
22357 return 0;
22358 if (first)
22359 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22360 else
22361 pixels += px;
22362 cdr = XCDR (cdr);
22363 }
22364 if (EQ (car, Qminus))
22365 pixels = -pixels;
22366 return OK_PIXELS (pixels);
22367 }
22368
22369 car = buffer_local_value_1 (car, it->w->buffer);
22370 if (EQ (car, Qunbound))
22371 car = Qnil;
22372 }
22373
22374 if (INTEGERP (car) || FLOATP (car))
22375 {
22376 double fact;
22377 pixels = XFLOATINT (car);
22378 if (NILP (cdr))
22379 return OK_PIXELS (pixels);
22380 if (calc_pixel_width_or_height (&fact, it, cdr,
22381 font, width_p, align_to))
22382 return OK_PIXELS (pixels * fact);
22383 return 0;
22384 }
22385
22386 return 0;
22387 }
22388
22389 return 0;
22390 }
22391
22392 \f
22393 /***********************************************************************
22394 Glyph Display
22395 ***********************************************************************/
22396
22397 #ifdef HAVE_WINDOW_SYSTEM
22398
22399 #ifdef GLYPH_DEBUG
22400
22401 void
22402 dump_glyph_string (struct glyph_string *s)
22403 {
22404 fprintf (stderr, "glyph string\n");
22405 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22406 s->x, s->y, s->width, s->height);
22407 fprintf (stderr, " ybase = %d\n", s->ybase);
22408 fprintf (stderr, " hl = %d\n", s->hl);
22409 fprintf (stderr, " left overhang = %d, right = %d\n",
22410 s->left_overhang, s->right_overhang);
22411 fprintf (stderr, " nchars = %d\n", s->nchars);
22412 fprintf (stderr, " extends to end of line = %d\n",
22413 s->extends_to_end_of_line_p);
22414 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22415 fprintf (stderr, " bg width = %d\n", s->background_width);
22416 }
22417
22418 #endif /* GLYPH_DEBUG */
22419
22420 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22421 of XChar2b structures for S; it can't be allocated in
22422 init_glyph_string because it must be allocated via `alloca'. W
22423 is the window on which S is drawn. ROW and AREA are the glyph row
22424 and area within the row from which S is constructed. START is the
22425 index of the first glyph structure covered by S. HL is a
22426 face-override for drawing S. */
22427
22428 #ifdef HAVE_NTGUI
22429 #define OPTIONAL_HDC(hdc) HDC hdc,
22430 #define DECLARE_HDC(hdc) HDC hdc;
22431 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22432 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22433 #endif
22434
22435 #ifndef OPTIONAL_HDC
22436 #define OPTIONAL_HDC(hdc)
22437 #define DECLARE_HDC(hdc)
22438 #define ALLOCATE_HDC(hdc, f)
22439 #define RELEASE_HDC(hdc, f)
22440 #endif
22441
22442 static void
22443 init_glyph_string (struct glyph_string *s,
22444 OPTIONAL_HDC (hdc)
22445 XChar2b *char2b, struct window *w, struct glyph_row *row,
22446 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22447 {
22448 memset (s, 0, sizeof *s);
22449 s->w = w;
22450 s->f = XFRAME (w->frame);
22451 #ifdef HAVE_NTGUI
22452 s->hdc = hdc;
22453 #endif
22454 s->display = FRAME_X_DISPLAY (s->f);
22455 s->window = FRAME_X_WINDOW (s->f);
22456 s->char2b = char2b;
22457 s->hl = hl;
22458 s->row = row;
22459 s->area = area;
22460 s->first_glyph = row->glyphs[area] + start;
22461 s->height = row->height;
22462 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22463 s->ybase = s->y + row->ascent;
22464 }
22465
22466
22467 /* Append the list of glyph strings with head H and tail T to the list
22468 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22469
22470 static void
22471 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22472 struct glyph_string *h, struct glyph_string *t)
22473 {
22474 if (h)
22475 {
22476 if (*head)
22477 (*tail)->next = h;
22478 else
22479 *head = h;
22480 h->prev = *tail;
22481 *tail = t;
22482 }
22483 }
22484
22485
22486 /* Prepend the list of glyph strings with head H and tail T to the
22487 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22488 result. */
22489
22490 static void
22491 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22492 struct glyph_string *h, struct glyph_string *t)
22493 {
22494 if (h)
22495 {
22496 if (*head)
22497 (*head)->prev = t;
22498 else
22499 *tail = t;
22500 t->next = *head;
22501 *head = h;
22502 }
22503 }
22504
22505
22506 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22507 Set *HEAD and *TAIL to the resulting list. */
22508
22509 static void
22510 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22511 struct glyph_string *s)
22512 {
22513 s->next = s->prev = NULL;
22514 append_glyph_string_lists (head, tail, s, s);
22515 }
22516
22517
22518 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22519 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22520 make sure that X resources for the face returned are allocated.
22521 Value is a pointer to a realized face that is ready for display if
22522 DISPLAY_P is non-zero. */
22523
22524 static struct face *
22525 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22526 XChar2b *char2b, int display_p)
22527 {
22528 struct face *face = FACE_FROM_ID (f, face_id);
22529
22530 if (face->font)
22531 {
22532 unsigned code = face->font->driver->encode_char (face->font, c);
22533
22534 if (code != FONT_INVALID_CODE)
22535 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22536 else
22537 STORE_XCHAR2B (char2b, 0, 0);
22538 }
22539
22540 /* Make sure X resources of the face are allocated. */
22541 #ifdef HAVE_X_WINDOWS
22542 if (display_p)
22543 #endif
22544 {
22545 eassert (face != NULL);
22546 PREPARE_FACE_FOR_DISPLAY (f, face);
22547 }
22548
22549 return face;
22550 }
22551
22552
22553 /* Get face and two-byte form of character glyph GLYPH on frame F.
22554 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22555 a pointer to a realized face that is ready for display. */
22556
22557 static struct face *
22558 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22559 XChar2b *char2b, int *two_byte_p)
22560 {
22561 struct face *face;
22562
22563 eassert (glyph->type == CHAR_GLYPH);
22564 face = FACE_FROM_ID (f, glyph->face_id);
22565
22566 if (two_byte_p)
22567 *two_byte_p = 0;
22568
22569 if (face->font)
22570 {
22571 unsigned code;
22572
22573 if (CHAR_BYTE8_P (glyph->u.ch))
22574 code = CHAR_TO_BYTE8 (glyph->u.ch);
22575 else
22576 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22577
22578 if (code != FONT_INVALID_CODE)
22579 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22580 else
22581 STORE_XCHAR2B (char2b, 0, 0);
22582 }
22583
22584 /* Make sure X resources of the face are allocated. */
22585 eassert (face != NULL);
22586 PREPARE_FACE_FOR_DISPLAY (f, face);
22587 return face;
22588 }
22589
22590
22591 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22592 Return 1 if FONT has a glyph for C, otherwise return 0. */
22593
22594 static int
22595 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22596 {
22597 unsigned code;
22598
22599 if (CHAR_BYTE8_P (c))
22600 code = CHAR_TO_BYTE8 (c);
22601 else
22602 code = font->driver->encode_char (font, c);
22603
22604 if (code == FONT_INVALID_CODE)
22605 return 0;
22606 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22607 return 1;
22608 }
22609
22610
22611 /* Fill glyph string S with composition components specified by S->cmp.
22612
22613 BASE_FACE is the base face of the composition.
22614 S->cmp_from is the index of the first component for S.
22615
22616 OVERLAPS non-zero means S should draw the foreground only, and use
22617 its physical height for clipping. See also draw_glyphs.
22618
22619 Value is the index of a component not in S. */
22620
22621 static int
22622 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22623 int overlaps)
22624 {
22625 int i;
22626 /* For all glyphs of this composition, starting at the offset
22627 S->cmp_from, until we reach the end of the definition or encounter a
22628 glyph that requires the different face, add it to S. */
22629 struct face *face;
22630
22631 eassert (s);
22632
22633 s->for_overlaps = overlaps;
22634 s->face = NULL;
22635 s->font = NULL;
22636 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22637 {
22638 int c = COMPOSITION_GLYPH (s->cmp, i);
22639
22640 /* TAB in a composition means display glyphs with padding space
22641 on the left or right. */
22642 if (c != '\t')
22643 {
22644 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22645 -1, Qnil);
22646
22647 face = get_char_face_and_encoding (s->f, c, face_id,
22648 s->char2b + i, 1);
22649 if (face)
22650 {
22651 if (! s->face)
22652 {
22653 s->face = face;
22654 s->font = s->face->font;
22655 }
22656 else if (s->face != face)
22657 break;
22658 }
22659 }
22660 ++s->nchars;
22661 }
22662 s->cmp_to = i;
22663
22664 if (s->face == NULL)
22665 {
22666 s->face = base_face->ascii_face;
22667 s->font = s->face->font;
22668 }
22669
22670 /* All glyph strings for the same composition has the same width,
22671 i.e. the width set for the first component of the composition. */
22672 s->width = s->first_glyph->pixel_width;
22673
22674 /* If the specified font could not be loaded, use the frame's
22675 default font, but record the fact that we couldn't load it in
22676 the glyph string so that we can draw rectangles for the
22677 characters of the glyph string. */
22678 if (s->font == NULL)
22679 {
22680 s->font_not_found_p = 1;
22681 s->font = FRAME_FONT (s->f);
22682 }
22683
22684 /* Adjust base line for subscript/superscript text. */
22685 s->ybase += s->first_glyph->voffset;
22686
22687 /* This glyph string must always be drawn with 16-bit functions. */
22688 s->two_byte_p = 1;
22689
22690 return s->cmp_to;
22691 }
22692
22693 static int
22694 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22695 int start, int end, int overlaps)
22696 {
22697 struct glyph *glyph, *last;
22698 Lisp_Object lgstring;
22699 int i;
22700
22701 s->for_overlaps = overlaps;
22702 glyph = s->row->glyphs[s->area] + start;
22703 last = s->row->glyphs[s->area] + end;
22704 s->cmp_id = glyph->u.cmp.id;
22705 s->cmp_from = glyph->slice.cmp.from;
22706 s->cmp_to = glyph->slice.cmp.to + 1;
22707 s->face = FACE_FROM_ID (s->f, face_id);
22708 lgstring = composition_gstring_from_id (s->cmp_id);
22709 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22710 glyph++;
22711 while (glyph < last
22712 && glyph->u.cmp.automatic
22713 && glyph->u.cmp.id == s->cmp_id
22714 && s->cmp_to == glyph->slice.cmp.from)
22715 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22716
22717 for (i = s->cmp_from; i < s->cmp_to; i++)
22718 {
22719 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22720 unsigned code = LGLYPH_CODE (lglyph);
22721
22722 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22723 }
22724 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22725 return glyph - s->row->glyphs[s->area];
22726 }
22727
22728
22729 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22730 See the comment of fill_glyph_string for arguments.
22731 Value is the index of the first glyph not in S. */
22732
22733
22734 static int
22735 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22736 int start, int end, int overlaps)
22737 {
22738 struct glyph *glyph, *last;
22739 int voffset;
22740
22741 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22742 s->for_overlaps = overlaps;
22743 glyph = s->row->glyphs[s->area] + start;
22744 last = s->row->glyphs[s->area] + end;
22745 voffset = glyph->voffset;
22746 s->face = FACE_FROM_ID (s->f, face_id);
22747 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22748 s->nchars = 1;
22749 s->width = glyph->pixel_width;
22750 glyph++;
22751 while (glyph < last
22752 && glyph->type == GLYPHLESS_GLYPH
22753 && glyph->voffset == voffset
22754 && glyph->face_id == face_id)
22755 {
22756 s->nchars++;
22757 s->width += glyph->pixel_width;
22758 glyph++;
22759 }
22760 s->ybase += voffset;
22761 return glyph - s->row->glyphs[s->area];
22762 }
22763
22764
22765 /* Fill glyph string S from a sequence of character glyphs.
22766
22767 FACE_ID is the face id of the string. START is the index of the
22768 first glyph to consider, END is the index of the last + 1.
22769 OVERLAPS non-zero means S should draw the foreground only, and use
22770 its physical height for clipping. See also draw_glyphs.
22771
22772 Value is the index of the first glyph not in S. */
22773
22774 static int
22775 fill_glyph_string (struct glyph_string *s, int face_id,
22776 int start, int end, int overlaps)
22777 {
22778 struct glyph *glyph, *last;
22779 int voffset;
22780 int glyph_not_available_p;
22781
22782 eassert (s->f == XFRAME (s->w->frame));
22783 eassert (s->nchars == 0);
22784 eassert (start >= 0 && end > start);
22785
22786 s->for_overlaps = overlaps;
22787 glyph = s->row->glyphs[s->area] + start;
22788 last = s->row->glyphs[s->area] + end;
22789 voffset = glyph->voffset;
22790 s->padding_p = glyph->padding_p;
22791 glyph_not_available_p = glyph->glyph_not_available_p;
22792
22793 while (glyph < last
22794 && glyph->type == CHAR_GLYPH
22795 && glyph->voffset == voffset
22796 /* Same face id implies same font, nowadays. */
22797 && glyph->face_id == face_id
22798 && glyph->glyph_not_available_p == glyph_not_available_p)
22799 {
22800 int two_byte_p;
22801
22802 s->face = get_glyph_face_and_encoding (s->f, glyph,
22803 s->char2b + s->nchars,
22804 &two_byte_p);
22805 s->two_byte_p = two_byte_p;
22806 ++s->nchars;
22807 eassert (s->nchars <= end - start);
22808 s->width += glyph->pixel_width;
22809 if (glyph++->padding_p != s->padding_p)
22810 break;
22811 }
22812
22813 s->font = s->face->font;
22814
22815 /* If the specified font could not be loaded, use the frame's font,
22816 but record the fact that we couldn't load it in
22817 S->font_not_found_p so that we can draw rectangles for the
22818 characters of the glyph string. */
22819 if (s->font == NULL || glyph_not_available_p)
22820 {
22821 s->font_not_found_p = 1;
22822 s->font = FRAME_FONT (s->f);
22823 }
22824
22825 /* Adjust base line for subscript/superscript text. */
22826 s->ybase += voffset;
22827
22828 eassert (s->face && s->face->gc);
22829 return glyph - s->row->glyphs[s->area];
22830 }
22831
22832
22833 /* Fill glyph string S from image glyph S->first_glyph. */
22834
22835 static void
22836 fill_image_glyph_string (struct glyph_string *s)
22837 {
22838 eassert (s->first_glyph->type == IMAGE_GLYPH);
22839 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22840 eassert (s->img);
22841 s->slice = s->first_glyph->slice.img;
22842 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22843 s->font = s->face->font;
22844 s->width = s->first_glyph->pixel_width;
22845
22846 /* Adjust base line for subscript/superscript text. */
22847 s->ybase += s->first_glyph->voffset;
22848 }
22849
22850 #ifdef HAVE_XWIDGETS
22851 static void
22852 fill_xwidget_glyph_string (struct glyph_string *s)
22853 {
22854 eassert (s->first_glyph->type == XWIDGET_GLYPH);
22855 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
22856 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22857 s->font = s->face->font;
22858 s->width = s->first_glyph->pixel_width;
22859 s->ybase += s->first_glyph->voffset;
22860 s->xwidget = s->first_glyph->u.xwidget;
22861 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
22862 }
22863 #endif
22864 /* Fill glyph string S from a sequence of stretch glyphs.
22865
22866 START is the index of the first glyph to consider,
22867 END is the index of the last + 1.
22868
22869 Value is the index of the first glyph not in S. */
22870
22871 static int
22872 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22873 {
22874 struct glyph *glyph, *last;
22875 int voffset, face_id;
22876
22877 eassert (s->first_glyph->type == STRETCH_GLYPH);
22878
22879 glyph = s->row->glyphs[s->area] + start;
22880 last = s->row->glyphs[s->area] + end;
22881 face_id = glyph->face_id;
22882 s->face = FACE_FROM_ID (s->f, face_id);
22883 s->font = s->face->font;
22884 s->width = glyph->pixel_width;
22885 s->nchars = 1;
22886 voffset = glyph->voffset;
22887
22888 for (++glyph;
22889 (glyph < last
22890 && glyph->type == STRETCH_GLYPH
22891 && glyph->voffset == voffset
22892 && glyph->face_id == face_id);
22893 ++glyph)
22894 s->width += glyph->pixel_width;
22895
22896 /* Adjust base line for subscript/superscript text. */
22897 s->ybase += voffset;
22898
22899 /* The case that face->gc == 0 is handled when drawing the glyph
22900 string by calling PREPARE_FACE_FOR_DISPLAY. */
22901 eassert (s->face);
22902 return glyph - s->row->glyphs[s->area];
22903 }
22904
22905 static struct font_metrics *
22906 get_per_char_metric (struct font *font, XChar2b *char2b)
22907 {
22908 static struct font_metrics metrics;
22909 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22910
22911 if (! font || code == FONT_INVALID_CODE)
22912 return NULL;
22913 font->driver->text_extents (font, &code, 1, &metrics);
22914 return &metrics;
22915 }
22916
22917 /* EXPORT for RIF:
22918 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22919 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22920 assumed to be zero. */
22921
22922 void
22923 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22924 {
22925 *left = *right = 0;
22926
22927 if (glyph->type == CHAR_GLYPH)
22928 {
22929 struct face *face;
22930 XChar2b char2b;
22931 struct font_metrics *pcm;
22932
22933 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22934 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22935 {
22936 if (pcm->rbearing > pcm->width)
22937 *right = pcm->rbearing - pcm->width;
22938 if (pcm->lbearing < 0)
22939 *left = -pcm->lbearing;
22940 }
22941 }
22942 else if (glyph->type == COMPOSITE_GLYPH)
22943 {
22944 if (! glyph->u.cmp.automatic)
22945 {
22946 struct composition *cmp = composition_table[glyph->u.cmp.id];
22947
22948 if (cmp->rbearing > cmp->pixel_width)
22949 *right = cmp->rbearing - cmp->pixel_width;
22950 if (cmp->lbearing < 0)
22951 *left = - cmp->lbearing;
22952 }
22953 else
22954 {
22955 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22956 struct font_metrics metrics;
22957
22958 composition_gstring_width (gstring, glyph->slice.cmp.from,
22959 glyph->slice.cmp.to + 1, &metrics);
22960 if (metrics.rbearing > metrics.width)
22961 *right = metrics.rbearing - metrics.width;
22962 if (metrics.lbearing < 0)
22963 *left = - metrics.lbearing;
22964 }
22965 }
22966 }
22967
22968
22969 /* Return the index of the first glyph preceding glyph string S that
22970 is overwritten by S because of S's left overhang. Value is -1
22971 if no glyphs are overwritten. */
22972
22973 static int
22974 left_overwritten (struct glyph_string *s)
22975 {
22976 int k;
22977
22978 if (s->left_overhang)
22979 {
22980 int x = 0, i;
22981 struct glyph *glyphs = s->row->glyphs[s->area];
22982 int first = s->first_glyph - glyphs;
22983
22984 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22985 x -= glyphs[i].pixel_width;
22986
22987 k = i + 1;
22988 }
22989 else
22990 k = -1;
22991
22992 return k;
22993 }
22994
22995
22996 /* Return the index of the first glyph preceding glyph string S that
22997 is overwriting S because of its right overhang. Value is -1 if no
22998 glyph in front of S overwrites S. */
22999
23000 static int
23001 left_overwriting (struct glyph_string *s)
23002 {
23003 int i, k, x;
23004 struct glyph *glyphs = s->row->glyphs[s->area];
23005 int first = s->first_glyph - glyphs;
23006
23007 k = -1;
23008 x = 0;
23009 for (i = first - 1; i >= 0; --i)
23010 {
23011 int left, right;
23012 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23013 if (x + right > 0)
23014 k = i;
23015 x -= glyphs[i].pixel_width;
23016 }
23017
23018 return k;
23019 }
23020
23021
23022 /* Return the index of the last glyph following glyph string S that is
23023 overwritten by S because of S's right overhang. Value is -1 if
23024 no such glyph is found. */
23025
23026 static int
23027 right_overwritten (struct glyph_string *s)
23028 {
23029 int k = -1;
23030
23031 if (s->right_overhang)
23032 {
23033 int x = 0, i;
23034 struct glyph *glyphs = s->row->glyphs[s->area];
23035 int first = (s->first_glyph - glyphs
23036 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23037 int end = s->row->used[s->area];
23038
23039 for (i = first; i < end && s->right_overhang > x; ++i)
23040 x += glyphs[i].pixel_width;
23041
23042 k = i;
23043 }
23044
23045 return k;
23046 }
23047
23048
23049 /* Return the index of the last glyph following glyph string S that
23050 overwrites S because of its left overhang. Value is negative
23051 if no such glyph is found. */
23052
23053 static int
23054 right_overwriting (struct glyph_string *s)
23055 {
23056 int i, k, x;
23057 int end = s->row->used[s->area];
23058 struct glyph *glyphs = s->row->glyphs[s->area];
23059 int first = (s->first_glyph - glyphs
23060 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23061
23062 k = -1;
23063 x = 0;
23064 for (i = first; i < end; ++i)
23065 {
23066 int left, right;
23067 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23068 if (x - left < 0)
23069 k = i;
23070 x += glyphs[i].pixel_width;
23071 }
23072
23073 return k;
23074 }
23075
23076
23077 /* Set background width of glyph string S. START is the index of the
23078 first glyph following S. LAST_X is the right-most x-position + 1
23079 in the drawing area. */
23080
23081 static void
23082 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23083 {
23084 /* If the face of this glyph string has to be drawn to the end of
23085 the drawing area, set S->extends_to_end_of_line_p. */
23086
23087 if (start == s->row->used[s->area]
23088 && s->area == TEXT_AREA
23089 && ((s->row->fill_line_p
23090 && (s->hl == DRAW_NORMAL_TEXT
23091 || s->hl == DRAW_IMAGE_RAISED
23092 || s->hl == DRAW_IMAGE_SUNKEN))
23093 || s->hl == DRAW_MOUSE_FACE))
23094 s->extends_to_end_of_line_p = 1;
23095
23096 /* If S extends its face to the end of the line, set its
23097 background_width to the distance to the right edge of the drawing
23098 area. */
23099 if (s->extends_to_end_of_line_p)
23100 s->background_width = last_x - s->x + 1;
23101 else
23102 s->background_width = s->width;
23103 }
23104
23105
23106 /* Compute overhangs and x-positions for glyph string S and its
23107 predecessors, or successors. X is the starting x-position for S.
23108 BACKWARD_P non-zero means process predecessors. */
23109
23110 static void
23111 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23112 {
23113 if (backward_p)
23114 {
23115 while (s)
23116 {
23117 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23118 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23119 x -= s->width;
23120 s->x = x;
23121 s = s->prev;
23122 }
23123 }
23124 else
23125 {
23126 while (s)
23127 {
23128 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23129 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23130 s->x = x;
23131 x += s->width;
23132 s = s->next;
23133 }
23134 }
23135 }
23136
23137
23138
23139 /* The following macros are only called from draw_glyphs below.
23140 They reference the following parameters of that function directly:
23141 `w', `row', `area', and `overlap_p'
23142 as well as the following local variables:
23143 `s', `f', and `hdc' (in W32) */
23144
23145 #ifdef HAVE_NTGUI
23146 /* On W32, silently add local `hdc' variable to argument list of
23147 init_glyph_string. */
23148 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23149 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23150 #else
23151 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23152 init_glyph_string (s, char2b, w, row, area, start, hl)
23153 #endif
23154
23155 /* Add a glyph string for a stretch glyph to the list of strings
23156 between HEAD and TAIL. START is the index of the stretch glyph in
23157 row area AREA of glyph row ROW. END is the index of the last glyph
23158 in that glyph row area. X is the current output position assigned
23159 to the new glyph string constructed. HL overrides that face of the
23160 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23161 is the right-most x-position of the drawing area. */
23162
23163 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23164 and below -- keep them on one line. */
23165 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23166 do \
23167 { \
23168 s = alloca (sizeof *s); \
23169 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23170 START = fill_stretch_glyph_string (s, START, END); \
23171 append_glyph_string (&HEAD, &TAIL, s); \
23172 s->x = (X); \
23173 } \
23174 while (0)
23175
23176
23177 /* Add a glyph string for an image glyph to the list of strings
23178 between HEAD and TAIL. START is the index of the image glyph in
23179 row area AREA of glyph row ROW. END is the index of the last glyph
23180 in that glyph row area. X is the current output position assigned
23181 to the new glyph string constructed. HL overrides that face of the
23182 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23183 is the right-most x-position of the drawing area. */
23184
23185 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23186 do \
23187 { \
23188 s = alloca (sizeof *s); \
23189 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23190 fill_image_glyph_string (s); \
23191 append_glyph_string (&HEAD, &TAIL, s); \
23192 ++START; \
23193 s->x = (X); \
23194 } \
23195 while (0)
23196
23197 #ifdef HAVE_XWIDGETS
23198 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23199 do \
23200 { \
23201 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
23202 s = (struct glyph_string *) alloca (sizeof *s); \
23203 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23204 fill_xwidget_glyph_string (s); \
23205 append_glyph_string (&HEAD, &TAIL, s); \
23206 ++START; \
23207 s->x = (X); \
23208 } \
23209 while (0)
23210 #endif
23211
23212
23213 /* Add a glyph string for a sequence of character glyphs to the list
23214 of strings between HEAD and TAIL. START is the index of the first
23215 glyph in row area AREA of glyph row ROW that is part of the new
23216 glyph string. END is the index of the last glyph in that glyph row
23217 area. X is the current output position assigned to the new glyph
23218 string constructed. HL overrides that face of the glyph; e.g. it
23219 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23220 right-most x-position of the drawing area. */
23221
23222 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23223 do \
23224 { \
23225 int face_id; \
23226 XChar2b *char2b; \
23227 \
23228 face_id = (row)->glyphs[area][START].face_id; \
23229 \
23230 s = alloca (sizeof *s); \
23231 char2b = alloca ((END - START) * sizeof *char2b); \
23232 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23233 append_glyph_string (&HEAD, &TAIL, s); \
23234 s->x = (X); \
23235 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23236 } \
23237 while (0)
23238
23239
23240 /* Add a glyph string for a composite sequence to the list of strings
23241 between HEAD and TAIL. START is the index of the first glyph in
23242 row area AREA of glyph row ROW that is part of the new glyph
23243 string. END is the index of the last glyph in that glyph row area.
23244 X is the current output position assigned to the new glyph string
23245 constructed. HL overrides that face of the glyph; e.g. it is
23246 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23247 x-position of the drawing area. */
23248
23249 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23250 do { \
23251 int face_id = (row)->glyphs[area][START].face_id; \
23252 struct face *base_face = FACE_FROM_ID (f, face_id); \
23253 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23254 struct composition *cmp = composition_table[cmp_id]; \
23255 XChar2b *char2b; \
23256 struct glyph_string *first_s = NULL; \
23257 int n; \
23258 \
23259 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23260 \
23261 /* Make glyph_strings for each glyph sequence that is drawable by \
23262 the same face, and append them to HEAD/TAIL. */ \
23263 for (n = 0; n < cmp->glyph_len;) \
23264 { \
23265 s = alloca (sizeof *s); \
23266 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23267 append_glyph_string (&(HEAD), &(TAIL), s); \
23268 s->cmp = cmp; \
23269 s->cmp_from = n; \
23270 s->x = (X); \
23271 if (n == 0) \
23272 first_s = s; \
23273 n = fill_composite_glyph_string (s, base_face, overlaps); \
23274 } \
23275 \
23276 ++START; \
23277 s = first_s; \
23278 } while (0)
23279
23280
23281 /* Add a glyph string for a glyph-string sequence to the list of strings
23282 between HEAD and TAIL. */
23283
23284 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23285 do { \
23286 int face_id; \
23287 XChar2b *char2b; \
23288 Lisp_Object gstring; \
23289 \
23290 face_id = (row)->glyphs[area][START].face_id; \
23291 gstring = (composition_gstring_from_id \
23292 ((row)->glyphs[area][START].u.cmp.id)); \
23293 s = alloca (sizeof *s); \
23294 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23295 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23296 append_glyph_string (&(HEAD), &(TAIL), s); \
23297 s->x = (X); \
23298 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23299 } while (0)
23300
23301
23302 /* Add a glyph string for a sequence of glyphless character's glyphs
23303 to the list of strings between HEAD and TAIL. The meanings of
23304 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23305
23306 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23307 do \
23308 { \
23309 int face_id; \
23310 \
23311 face_id = (row)->glyphs[area][START].face_id; \
23312 \
23313 s = alloca (sizeof *s); \
23314 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23315 append_glyph_string (&HEAD, &TAIL, s); \
23316 s->x = (X); \
23317 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23318 overlaps); \
23319 } \
23320 while (0)
23321
23322
23323 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23324 of AREA of glyph row ROW on window W between indices START and END.
23325 HL overrides the face for drawing glyph strings, e.g. it is
23326 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23327 x-positions of the drawing area.
23328
23329 This is an ugly monster macro construct because we must use alloca
23330 to allocate glyph strings (because draw_glyphs can be called
23331 asynchronously). */
23332
23333 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23334 do \
23335 { \
23336 HEAD = TAIL = NULL; \
23337 while (START < END) \
23338 { \
23339 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23340 switch (first_glyph->type) \
23341 { \
23342 case CHAR_GLYPH: \
23343 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23344 HL, X, LAST_X); \
23345 break; \
23346 \
23347 case COMPOSITE_GLYPH: \
23348 if (first_glyph->u.cmp.automatic) \
23349 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23350 HL, X, LAST_X); \
23351 else \
23352 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23353 HL, X, LAST_X); \
23354 break; \
23355 \
23356 case STRETCH_GLYPH: \
23357 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23358 HL, X, LAST_X); \
23359 break; \
23360 \
23361 case IMAGE_GLYPH: \
23362 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23363 HL, X, LAST_X); \
23364 break;
23365
23366 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23367 case XWIDGET_GLYPH: \
23368 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
23369 HL, X, LAST_X); \
23370 break;
23371
23372 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
23373 case GLYPHLESS_GLYPH: \
23374 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23375 HL, X, LAST_X); \
23376 break; \
23377 \
23378 default: \
23379 emacs_abort (); \
23380 } \
23381 \
23382 if (s) \
23383 { \
23384 set_glyph_string_background_width (s, START, LAST_X); \
23385 (X) += s->width; \
23386 } \
23387 } \
23388 } while (0)
23389
23390
23391 #ifdef HAVE_XWIDGETS
23392 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23393 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23394 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23395 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23396 #else
23397 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23398 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23399 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23400 #endif
23401
23402
23403 /* Draw glyphs between START and END in AREA of ROW on window W,
23404 starting at x-position X. X is relative to AREA in W. HL is a
23405 face-override with the following meaning:
23406
23407 DRAW_NORMAL_TEXT draw normally
23408 DRAW_CURSOR draw in cursor face
23409 DRAW_MOUSE_FACE draw in mouse face.
23410 DRAW_INVERSE_VIDEO draw in mode line face
23411 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23412 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23413
23414 If OVERLAPS is non-zero, draw only the foreground of characters and
23415 clip to the physical height of ROW. Non-zero value also defines
23416 the overlapping part to be drawn:
23417
23418 OVERLAPS_PRED overlap with preceding rows
23419 OVERLAPS_SUCC overlap with succeeding rows
23420 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23421 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23422
23423 Value is the x-position reached, relative to AREA of W. */
23424
23425 static int
23426 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23427 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23428 enum draw_glyphs_face hl, int overlaps)
23429 {
23430 struct glyph_string *head, *tail;
23431 struct glyph_string *s;
23432 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23433 int i, j, x_reached, last_x, area_left = 0;
23434 struct frame *f = XFRAME (WINDOW_FRAME (w));
23435 DECLARE_HDC (hdc);
23436
23437 ALLOCATE_HDC (hdc, f);
23438
23439 /* Let's rather be paranoid than getting a SEGV. */
23440 end = min (end, row->used[area]);
23441 start = clip_to_bounds (0, start, end);
23442
23443 /* Translate X to frame coordinates. Set last_x to the right
23444 end of the drawing area. */
23445 if (row->full_width_p)
23446 {
23447 /* X is relative to the left edge of W, without scroll bars
23448 or fringes. */
23449 area_left = WINDOW_LEFT_EDGE_X (w);
23450 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23451 }
23452 else
23453 {
23454 area_left = window_box_left (w, area);
23455 last_x = area_left + window_box_width (w, area);
23456 }
23457 x += area_left;
23458
23459 /* Build a doubly-linked list of glyph_string structures between
23460 head and tail from what we have to draw. Note that the macro
23461 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23462 the reason we use a separate variable `i'. */
23463 i = start;
23464 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23465 if (tail)
23466 x_reached = tail->x + tail->background_width;
23467 else
23468 x_reached = x;
23469
23470 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23471 the row, redraw some glyphs in front or following the glyph
23472 strings built above. */
23473 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23474 {
23475 struct glyph_string *h, *t;
23476 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23477 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23478 int check_mouse_face = 0;
23479 int dummy_x = 0;
23480
23481 /* If mouse highlighting is on, we may need to draw adjacent
23482 glyphs using mouse-face highlighting. */
23483 if (area == TEXT_AREA && row->mouse_face_p
23484 && hlinfo->mouse_face_beg_row >= 0
23485 && hlinfo->mouse_face_end_row >= 0)
23486 {
23487 struct glyph_row *mouse_beg_row, *mouse_end_row;
23488
23489 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23490 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23491
23492 if (row >= mouse_beg_row && row <= mouse_end_row)
23493 {
23494 check_mouse_face = 1;
23495 mouse_beg_col = (row == mouse_beg_row)
23496 ? hlinfo->mouse_face_beg_col : 0;
23497 mouse_end_col = (row == mouse_end_row)
23498 ? hlinfo->mouse_face_end_col
23499 : row->used[TEXT_AREA];
23500 }
23501 }
23502
23503 /* Compute overhangs for all glyph strings. */
23504 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23505 for (s = head; s; s = s->next)
23506 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23507
23508 /* Prepend glyph strings for glyphs in front of the first glyph
23509 string that are overwritten because of the first glyph
23510 string's left overhang. The background of all strings
23511 prepended must be drawn because the first glyph string
23512 draws over it. */
23513 i = left_overwritten (head);
23514 if (i >= 0)
23515 {
23516 enum draw_glyphs_face overlap_hl;
23517
23518 /* If this row contains mouse highlighting, attempt to draw
23519 the overlapped glyphs with the correct highlight. This
23520 code fails if the overlap encompasses more than one glyph
23521 and mouse-highlight spans only some of these glyphs.
23522 However, making it work perfectly involves a lot more
23523 code, and I don't know if the pathological case occurs in
23524 practice, so we'll stick to this for now. --- cyd */
23525 if (check_mouse_face
23526 && mouse_beg_col < start && mouse_end_col > i)
23527 overlap_hl = DRAW_MOUSE_FACE;
23528 else
23529 overlap_hl = DRAW_NORMAL_TEXT;
23530
23531 j = i;
23532 BUILD_GLYPH_STRINGS (j, start, h, t,
23533 overlap_hl, dummy_x, last_x);
23534 start = i;
23535 compute_overhangs_and_x (t, head->x, 1);
23536 prepend_glyph_string_lists (&head, &tail, h, t);
23537 clip_head = head;
23538 }
23539
23540 /* Prepend glyph strings for glyphs in front of the first glyph
23541 string that overwrite that glyph string because of their
23542 right overhang. For these strings, only the foreground must
23543 be drawn, because it draws over the glyph string at `head'.
23544 The background must not be drawn because this would overwrite
23545 right overhangs of preceding glyphs for which no glyph
23546 strings exist. */
23547 i = left_overwriting (head);
23548 if (i >= 0)
23549 {
23550 enum draw_glyphs_face overlap_hl;
23551
23552 if (check_mouse_face
23553 && mouse_beg_col < start && mouse_end_col > i)
23554 overlap_hl = DRAW_MOUSE_FACE;
23555 else
23556 overlap_hl = DRAW_NORMAL_TEXT;
23557
23558 clip_head = head;
23559 BUILD_GLYPH_STRINGS (i, start, h, t,
23560 overlap_hl, dummy_x, last_x);
23561 for (s = h; s; s = s->next)
23562 s->background_filled_p = 1;
23563 compute_overhangs_and_x (t, head->x, 1);
23564 prepend_glyph_string_lists (&head, &tail, h, t);
23565 }
23566
23567 /* Append glyphs strings for glyphs following the last glyph
23568 string tail that are overwritten by tail. The background of
23569 these strings has to be drawn because tail's foreground draws
23570 over it. */
23571 i = right_overwritten (tail);
23572 if (i >= 0)
23573 {
23574 enum draw_glyphs_face overlap_hl;
23575
23576 if (check_mouse_face
23577 && mouse_beg_col < i && mouse_end_col > end)
23578 overlap_hl = DRAW_MOUSE_FACE;
23579 else
23580 overlap_hl = DRAW_NORMAL_TEXT;
23581
23582 BUILD_GLYPH_STRINGS (end, i, h, t,
23583 overlap_hl, x, last_x);
23584 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23585 we don't have `end = i;' here. */
23586 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23587 append_glyph_string_lists (&head, &tail, h, t);
23588 clip_tail = tail;
23589 }
23590
23591 /* Append glyph strings for glyphs following the last glyph
23592 string tail that overwrite tail. The foreground of such
23593 glyphs has to be drawn because it writes into the background
23594 of tail. The background must not be drawn because it could
23595 paint over the foreground of following glyphs. */
23596 i = right_overwriting (tail);
23597 if (i >= 0)
23598 {
23599 enum draw_glyphs_face overlap_hl;
23600 if (check_mouse_face
23601 && mouse_beg_col < i && mouse_end_col > end)
23602 overlap_hl = DRAW_MOUSE_FACE;
23603 else
23604 overlap_hl = DRAW_NORMAL_TEXT;
23605
23606 clip_tail = tail;
23607 i++; /* We must include the Ith glyph. */
23608 BUILD_GLYPH_STRINGS (end, i, h, t,
23609 overlap_hl, x, last_x);
23610 for (s = h; s; s = s->next)
23611 s->background_filled_p = 1;
23612 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23613 append_glyph_string_lists (&head, &tail, h, t);
23614 }
23615 if (clip_head || clip_tail)
23616 for (s = head; s; s = s->next)
23617 {
23618 s->clip_head = clip_head;
23619 s->clip_tail = clip_tail;
23620 }
23621 }
23622
23623 /* Draw all strings. */
23624 for (s = head; s; s = s->next)
23625 FRAME_RIF (f)->draw_glyph_string (s);
23626
23627 #ifndef HAVE_NS
23628 /* When focus a sole frame and move horizontally, this sets on_p to 0
23629 causing a failure to erase prev cursor position. */
23630 if (area == TEXT_AREA
23631 && !row->full_width_p
23632 /* When drawing overlapping rows, only the glyph strings'
23633 foreground is drawn, which doesn't erase a cursor
23634 completely. */
23635 && !overlaps)
23636 {
23637 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23638 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23639 : (tail ? tail->x + tail->background_width : x));
23640 x0 -= area_left;
23641 x1 -= area_left;
23642
23643 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23644 row->y, MATRIX_ROW_BOTTOM_Y (row));
23645 }
23646 #endif
23647
23648 /* Value is the x-position up to which drawn, relative to AREA of W.
23649 This doesn't include parts drawn because of overhangs. */
23650 if (row->full_width_p)
23651 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23652 else
23653 x_reached -= area_left;
23654
23655 RELEASE_HDC (hdc, f);
23656
23657 return x_reached;
23658 }
23659
23660 /* Expand row matrix if too narrow. Don't expand if area
23661 is not present. */
23662
23663 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23664 { \
23665 if (!fonts_changed_p \
23666 && (it->glyph_row->glyphs[area] \
23667 < it->glyph_row->glyphs[area + 1])) \
23668 { \
23669 it->w->ncols_scale_factor++; \
23670 fonts_changed_p = 1; \
23671 } \
23672 }
23673
23674 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23675 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23676
23677 static void
23678 append_glyph (struct it *it)
23679 {
23680 struct glyph *glyph;
23681 enum glyph_row_area area = it->area;
23682
23683 eassert (it->glyph_row);
23684 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23685
23686 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23687 if (glyph < it->glyph_row->glyphs[area + 1])
23688 {
23689 /* If the glyph row is reversed, we need to prepend the glyph
23690 rather than append it. */
23691 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23692 {
23693 struct glyph *g;
23694
23695 /* Make room for the additional glyph. */
23696 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23697 g[1] = *g;
23698 glyph = it->glyph_row->glyphs[area];
23699 }
23700 glyph->charpos = CHARPOS (it->position);
23701 glyph->object = it->object;
23702 if (it->pixel_width > 0)
23703 {
23704 glyph->pixel_width = it->pixel_width;
23705 glyph->padding_p = 0;
23706 }
23707 else
23708 {
23709 /* Assure at least 1-pixel width. Otherwise, cursor can't
23710 be displayed correctly. */
23711 glyph->pixel_width = 1;
23712 glyph->padding_p = 1;
23713 }
23714 glyph->ascent = it->ascent;
23715 glyph->descent = it->descent;
23716 glyph->voffset = it->voffset;
23717 glyph->type = CHAR_GLYPH;
23718 glyph->avoid_cursor_p = it->avoid_cursor_p;
23719 glyph->multibyte_p = it->multibyte_p;
23720 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23721 {
23722 /* In R2L rows, the left and the right box edges need to be
23723 drawn in reverse direction. */
23724 glyph->right_box_line_p = it->start_of_box_run_p;
23725 glyph->left_box_line_p = it->end_of_box_run_p;
23726 }
23727 else
23728 {
23729 glyph->left_box_line_p = it->start_of_box_run_p;
23730 glyph->right_box_line_p = it->end_of_box_run_p;
23731 }
23732 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23733 || it->phys_descent > it->descent);
23734 glyph->glyph_not_available_p = it->glyph_not_available_p;
23735 glyph->face_id = it->face_id;
23736 glyph->u.ch = it->char_to_display;
23737 glyph->slice.img = null_glyph_slice;
23738 glyph->font_type = FONT_TYPE_UNKNOWN;
23739 if (it->bidi_p)
23740 {
23741 glyph->resolved_level = it->bidi_it.resolved_level;
23742 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23743 emacs_abort ();
23744 glyph->bidi_type = it->bidi_it.type;
23745 }
23746 else
23747 {
23748 glyph->resolved_level = 0;
23749 glyph->bidi_type = UNKNOWN_BT;
23750 }
23751 ++it->glyph_row->used[area];
23752 }
23753 else
23754 IT_EXPAND_MATRIX_WIDTH (it, area);
23755 }
23756
23757 /* Store one glyph for the composition IT->cmp_it.id in
23758 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23759 non-null. */
23760
23761 static void
23762 append_composite_glyph (struct it *it)
23763 {
23764 struct glyph *glyph;
23765 enum glyph_row_area area = it->area;
23766
23767 eassert (it->glyph_row);
23768
23769 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23770 if (glyph < it->glyph_row->glyphs[area + 1])
23771 {
23772 /* If the glyph row is reversed, we need to prepend the glyph
23773 rather than append it. */
23774 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23775 {
23776 struct glyph *g;
23777
23778 /* Make room for the new glyph. */
23779 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23780 g[1] = *g;
23781 glyph = it->glyph_row->glyphs[it->area];
23782 }
23783 glyph->charpos = it->cmp_it.charpos;
23784 glyph->object = it->object;
23785 glyph->pixel_width = it->pixel_width;
23786 glyph->ascent = it->ascent;
23787 glyph->descent = it->descent;
23788 glyph->voffset = it->voffset;
23789 glyph->type = COMPOSITE_GLYPH;
23790 if (it->cmp_it.ch < 0)
23791 {
23792 glyph->u.cmp.automatic = 0;
23793 glyph->u.cmp.id = it->cmp_it.id;
23794 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23795 }
23796 else
23797 {
23798 glyph->u.cmp.automatic = 1;
23799 glyph->u.cmp.id = it->cmp_it.id;
23800 glyph->slice.cmp.from = it->cmp_it.from;
23801 glyph->slice.cmp.to = it->cmp_it.to - 1;
23802 }
23803 glyph->avoid_cursor_p = it->avoid_cursor_p;
23804 glyph->multibyte_p = it->multibyte_p;
23805 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23806 {
23807 /* In R2L rows, the left and the right box edges need to be
23808 drawn in reverse direction. */
23809 glyph->right_box_line_p = it->start_of_box_run_p;
23810 glyph->left_box_line_p = it->end_of_box_run_p;
23811 }
23812 else
23813 {
23814 glyph->left_box_line_p = it->start_of_box_run_p;
23815 glyph->right_box_line_p = it->end_of_box_run_p;
23816 }
23817 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23818 || it->phys_descent > it->descent);
23819 glyph->padding_p = 0;
23820 glyph->glyph_not_available_p = 0;
23821 glyph->face_id = it->face_id;
23822 glyph->font_type = FONT_TYPE_UNKNOWN;
23823 if (it->bidi_p)
23824 {
23825 glyph->resolved_level = it->bidi_it.resolved_level;
23826 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23827 emacs_abort ();
23828 glyph->bidi_type = it->bidi_it.type;
23829 }
23830 ++it->glyph_row->used[area];
23831 }
23832 else
23833 IT_EXPAND_MATRIX_WIDTH (it, area);
23834 }
23835
23836
23837 /* Change IT->ascent and IT->height according to the setting of
23838 IT->voffset. */
23839
23840 static void
23841 take_vertical_position_into_account (struct it *it)
23842 {
23843 if (it->voffset)
23844 {
23845 if (it->voffset < 0)
23846 /* Increase the ascent so that we can display the text higher
23847 in the line. */
23848 it->ascent -= it->voffset;
23849 else
23850 /* Increase the descent so that we can display the text lower
23851 in the line. */
23852 it->descent += it->voffset;
23853 }
23854 }
23855
23856
23857 /* Produce glyphs/get display metrics for the image IT is loaded with.
23858 See the description of struct display_iterator in dispextern.h for
23859 an overview of struct display_iterator. */
23860
23861 static void
23862 produce_image_glyph (struct it *it)
23863 {
23864 struct image *img;
23865 struct face *face;
23866 int glyph_ascent, crop;
23867 struct glyph_slice slice;
23868
23869 eassert (it->what == IT_IMAGE);
23870
23871 face = FACE_FROM_ID (it->f, it->face_id);
23872 eassert (face);
23873 /* Make sure X resources of the face is loaded. */
23874 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23875
23876 if (it->image_id < 0)
23877 {
23878 /* Fringe bitmap. */
23879 it->ascent = it->phys_ascent = 0;
23880 it->descent = it->phys_descent = 0;
23881 it->pixel_width = 0;
23882 it->nglyphs = 0;
23883 return;
23884 }
23885
23886 img = IMAGE_FROM_ID (it->f, it->image_id);
23887 eassert (img);
23888 /* Make sure X resources of the image is loaded. */
23889 prepare_image_for_display (it->f, img);
23890
23891 slice.x = slice.y = 0;
23892 slice.width = img->width;
23893 slice.height = img->height;
23894
23895 if (INTEGERP (it->slice.x))
23896 slice.x = XINT (it->slice.x);
23897 else if (FLOATP (it->slice.x))
23898 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23899
23900 if (INTEGERP (it->slice.y))
23901 slice.y = XINT (it->slice.y);
23902 else if (FLOATP (it->slice.y))
23903 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23904
23905 if (INTEGERP (it->slice.width))
23906 slice.width = XINT (it->slice.width);
23907 else if (FLOATP (it->slice.width))
23908 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23909
23910 if (INTEGERP (it->slice.height))
23911 slice.height = XINT (it->slice.height);
23912 else if (FLOATP (it->slice.height))
23913 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23914
23915 if (slice.x >= img->width)
23916 slice.x = img->width;
23917 if (slice.y >= img->height)
23918 slice.y = img->height;
23919 if (slice.x + slice.width >= img->width)
23920 slice.width = img->width - slice.x;
23921 if (slice.y + slice.height > img->height)
23922 slice.height = img->height - slice.y;
23923
23924 if (slice.width == 0 || slice.height == 0)
23925 return;
23926
23927 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23928
23929 it->descent = slice.height - glyph_ascent;
23930 if (slice.y == 0)
23931 it->descent += img->vmargin;
23932 if (slice.y + slice.height == img->height)
23933 it->descent += img->vmargin;
23934 it->phys_descent = it->descent;
23935
23936 it->pixel_width = slice.width;
23937 if (slice.x == 0)
23938 it->pixel_width += img->hmargin;
23939 if (slice.x + slice.width == img->width)
23940 it->pixel_width += img->hmargin;
23941
23942 /* It's quite possible for images to have an ascent greater than
23943 their height, so don't get confused in that case. */
23944 if (it->descent < 0)
23945 it->descent = 0;
23946
23947 it->nglyphs = 1;
23948
23949 if (face->box != FACE_NO_BOX)
23950 {
23951 if (face->box_line_width > 0)
23952 {
23953 if (slice.y == 0)
23954 it->ascent += face->box_line_width;
23955 if (slice.y + slice.height == img->height)
23956 it->descent += face->box_line_width;
23957 }
23958
23959 if (it->start_of_box_run_p && slice.x == 0)
23960 it->pixel_width += eabs (face->box_line_width);
23961 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23962 it->pixel_width += eabs (face->box_line_width);
23963 }
23964
23965 take_vertical_position_into_account (it);
23966
23967 /* Automatically crop wide image glyphs at right edge so we can
23968 draw the cursor on same display row. */
23969 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23970 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23971 {
23972 it->pixel_width -= crop;
23973 slice.width -= crop;
23974 }
23975
23976 if (it->glyph_row)
23977 {
23978 struct glyph *glyph;
23979 enum glyph_row_area area = it->area;
23980
23981 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23982 if (glyph < it->glyph_row->glyphs[area + 1])
23983 {
23984 glyph->charpos = CHARPOS (it->position);
23985 glyph->object = it->object;
23986 glyph->pixel_width = it->pixel_width;
23987 glyph->ascent = glyph_ascent;
23988 glyph->descent = it->descent;
23989 glyph->voffset = it->voffset;
23990 glyph->type = IMAGE_GLYPH;
23991 glyph->avoid_cursor_p = it->avoid_cursor_p;
23992 glyph->multibyte_p = it->multibyte_p;
23993 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23994 {
23995 /* In R2L rows, the left and the right box edges need to be
23996 drawn in reverse direction. */
23997 glyph->right_box_line_p = it->start_of_box_run_p;
23998 glyph->left_box_line_p = it->end_of_box_run_p;
23999 }
24000 else
24001 {
24002 glyph->left_box_line_p = it->start_of_box_run_p;
24003 glyph->right_box_line_p = it->end_of_box_run_p;
24004 }
24005 glyph->overlaps_vertically_p = 0;
24006 glyph->padding_p = 0;
24007 glyph->glyph_not_available_p = 0;
24008 glyph->face_id = it->face_id;
24009 glyph->u.img_id = img->id;
24010 glyph->slice.img = slice;
24011 glyph->font_type = FONT_TYPE_UNKNOWN;
24012 if (it->bidi_p)
24013 {
24014 glyph->resolved_level = it->bidi_it.resolved_level;
24015 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24016 emacs_abort ();
24017 glyph->bidi_type = it->bidi_it.type;
24018 }
24019 ++it->glyph_row->used[area];
24020 }
24021 else
24022 IT_EXPAND_MATRIX_WIDTH (it, area);
24023 }
24024 }
24025
24026 #ifdef HAVE_XWIDGETS
24027 static void
24028 produce_xwidget_glyph (struct it *it)
24029 {
24030 struct xwidget* xw;
24031 struct face *face;
24032 int glyph_ascent, crop;
24033 printf("produce_xwidget_glyph:\n");
24034 eassert (it->what == IT_XWIDGET);
24035
24036 face = FACE_FROM_ID (it->f, it->face_id);
24037 eassert (face);
24038 /* Make sure X resources of the face is loaded. */
24039 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24040
24041 xw = it->xwidget;
24042 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
24043 it->descent = xw->height/2;
24044 it->phys_descent = it->descent;
24045 it->pixel_width = xw->width;
24046 /* It's quite possible for images to have an ascent greater than
24047 their height, so don't get confused in that case. */
24048 if (it->descent < 0)
24049 it->descent = 0;
24050
24051 it->nglyphs = 1;
24052
24053 if (face->box != FACE_NO_BOX)
24054 {
24055 if (face->box_line_width > 0)
24056 {
24057 it->ascent += face->box_line_width;
24058 it->descent += face->box_line_width;
24059 }
24060
24061 if (it->start_of_box_run_p)
24062 it->pixel_width += eabs (face->box_line_width);
24063 it->pixel_width += eabs (face->box_line_width);
24064 }
24065
24066 take_vertical_position_into_account (it);
24067
24068 /* Automatically crop wide image glyphs at right edge so we can
24069 draw the cursor on same display row. */
24070 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24071 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24072 {
24073 it->pixel_width -= crop;
24074 }
24075
24076 if (it->glyph_row)
24077 {
24078 struct glyph *glyph;
24079 enum glyph_row_area area = it->area;
24080
24081 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24082 if (glyph < it->glyph_row->glyphs[area + 1])
24083 {
24084 glyph->charpos = CHARPOS (it->position);
24085 glyph->object = it->object;
24086 glyph->pixel_width = it->pixel_width;
24087 glyph->ascent = glyph_ascent;
24088 glyph->descent = it->descent;
24089 glyph->voffset = it->voffset;
24090 glyph->type = XWIDGET_GLYPH;
24091
24092 glyph->multibyte_p = it->multibyte_p;
24093 glyph->left_box_line_p = it->start_of_box_run_p;
24094 glyph->right_box_line_p = it->end_of_box_run_p;
24095 glyph->overlaps_vertically_p = 0;
24096 glyph->padding_p = 0;
24097 glyph->glyph_not_available_p = 0;
24098 glyph->face_id = it->face_id;
24099 glyph->u.xwidget = it->xwidget;
24100 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
24101 glyph->font_type = FONT_TYPE_UNKNOWN;
24102 ++it->glyph_row->used[area];
24103 }
24104 else
24105 IT_EXPAND_MATRIX_WIDTH (it, area);
24106 }
24107 }
24108 #endif
24109
24110 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24111 of the glyph, WIDTH and HEIGHT are the width and height of the
24112 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24113
24114 static void
24115 append_stretch_glyph (struct it *it, Lisp_Object object,
24116 int width, int height, int ascent)
24117 {
24118 struct glyph *glyph;
24119 enum glyph_row_area area = it->area;
24120
24121 eassert (ascent >= 0 && ascent <= height);
24122
24123 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24124 if (glyph < it->glyph_row->glyphs[area + 1])
24125 {
24126 /* If the glyph row is reversed, we need to prepend the glyph
24127 rather than append it. */
24128 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24129 {
24130 struct glyph *g;
24131
24132 /* Make room for the additional glyph. */
24133 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24134 g[1] = *g;
24135 glyph = it->glyph_row->glyphs[area];
24136 }
24137 glyph->charpos = CHARPOS (it->position);
24138 glyph->object = object;
24139 glyph->pixel_width = width;
24140 glyph->ascent = ascent;
24141 glyph->descent = height - ascent;
24142 glyph->voffset = it->voffset;
24143 glyph->type = STRETCH_GLYPH;
24144 glyph->avoid_cursor_p = it->avoid_cursor_p;
24145 glyph->multibyte_p = it->multibyte_p;
24146 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24147 {
24148 /* In R2L rows, the left and the right box edges need to be
24149 drawn in reverse direction. */
24150 glyph->right_box_line_p = it->start_of_box_run_p;
24151 glyph->left_box_line_p = it->end_of_box_run_p;
24152 }
24153 else
24154 {
24155 glyph->left_box_line_p = it->start_of_box_run_p;
24156 glyph->right_box_line_p = it->end_of_box_run_p;
24157 }
24158 glyph->overlaps_vertically_p = 0;
24159 glyph->padding_p = 0;
24160 glyph->glyph_not_available_p = 0;
24161 glyph->face_id = it->face_id;
24162 glyph->u.stretch.ascent = ascent;
24163 glyph->u.stretch.height = height;
24164 glyph->slice.img = null_glyph_slice;
24165 glyph->font_type = FONT_TYPE_UNKNOWN;
24166 if (it->bidi_p)
24167 {
24168 glyph->resolved_level = it->bidi_it.resolved_level;
24169 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24170 emacs_abort ();
24171 glyph->bidi_type = it->bidi_it.type;
24172 }
24173 else
24174 {
24175 glyph->resolved_level = 0;
24176 glyph->bidi_type = UNKNOWN_BT;
24177 }
24178 ++it->glyph_row->used[area];
24179 }
24180 else
24181 IT_EXPAND_MATRIX_WIDTH (it, area);
24182 }
24183
24184 #endif /* HAVE_WINDOW_SYSTEM */
24185
24186 /* Produce a stretch glyph for iterator IT. IT->object is the value
24187 of the glyph property displayed. The value must be a list
24188 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24189 being recognized:
24190
24191 1. `:width WIDTH' specifies that the space should be WIDTH *
24192 canonical char width wide. WIDTH may be an integer or floating
24193 point number.
24194
24195 2. `:relative-width FACTOR' specifies that the width of the stretch
24196 should be computed from the width of the first character having the
24197 `glyph' property, and should be FACTOR times that width.
24198
24199 3. `:align-to HPOS' specifies that the space should be wide enough
24200 to reach HPOS, a value in canonical character units.
24201
24202 Exactly one of the above pairs must be present.
24203
24204 4. `:height HEIGHT' specifies that the height of the stretch produced
24205 should be HEIGHT, measured in canonical character units.
24206
24207 5. `:relative-height FACTOR' specifies that the height of the
24208 stretch should be FACTOR times the height of the characters having
24209 the glyph property.
24210
24211 Either none or exactly one of 4 or 5 must be present.
24212
24213 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24214 of the stretch should be used for the ascent of the stretch.
24215 ASCENT must be in the range 0 <= ASCENT <= 100. */
24216
24217 void
24218 produce_stretch_glyph (struct it *it)
24219 {
24220 /* (space :width WIDTH :height HEIGHT ...) */
24221 Lisp_Object prop, plist;
24222 int width = 0, height = 0, align_to = -1;
24223 int zero_width_ok_p = 0;
24224 double tem;
24225 struct font *font = NULL;
24226
24227 #ifdef HAVE_WINDOW_SYSTEM
24228 int ascent = 0;
24229 int zero_height_ok_p = 0;
24230
24231 if (FRAME_WINDOW_P (it->f))
24232 {
24233 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24234 font = face->font ? face->font : FRAME_FONT (it->f);
24235 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24236 }
24237 #endif
24238
24239 /* List should start with `space'. */
24240 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24241 plist = XCDR (it->object);
24242
24243 /* Compute the width of the stretch. */
24244 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24245 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24246 {
24247 /* Absolute width `:width WIDTH' specified and valid. */
24248 zero_width_ok_p = 1;
24249 width = (int)tem;
24250 }
24251 #ifdef HAVE_WINDOW_SYSTEM
24252 else if (FRAME_WINDOW_P (it->f)
24253 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24254 {
24255 /* Relative width `:relative-width FACTOR' specified and valid.
24256 Compute the width of the characters having the `glyph'
24257 property. */
24258 struct it it2;
24259 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24260
24261 it2 = *it;
24262 if (it->multibyte_p)
24263 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24264 else
24265 {
24266 it2.c = it2.char_to_display = *p, it2.len = 1;
24267 if (! ASCII_CHAR_P (it2.c))
24268 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24269 }
24270
24271 it2.glyph_row = NULL;
24272 it2.what = IT_CHARACTER;
24273 x_produce_glyphs (&it2);
24274 width = NUMVAL (prop) * it2.pixel_width;
24275 }
24276 #endif /* HAVE_WINDOW_SYSTEM */
24277 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24278 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24279 {
24280 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24281 align_to = (align_to < 0
24282 ? 0
24283 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24284 else if (align_to < 0)
24285 align_to = window_box_left_offset (it->w, TEXT_AREA);
24286 width = max (0, (int)tem + align_to - it->current_x);
24287 zero_width_ok_p = 1;
24288 }
24289 else
24290 /* Nothing specified -> width defaults to canonical char width. */
24291 width = FRAME_COLUMN_WIDTH (it->f);
24292
24293 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24294 width = 1;
24295
24296 #ifdef HAVE_WINDOW_SYSTEM
24297 /* Compute height. */
24298 if (FRAME_WINDOW_P (it->f))
24299 {
24300 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24301 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24302 {
24303 height = (int)tem;
24304 zero_height_ok_p = 1;
24305 }
24306 else if (prop = Fplist_get (plist, QCrelative_height),
24307 NUMVAL (prop) > 0)
24308 height = FONT_HEIGHT (font) * NUMVAL (prop);
24309 else
24310 height = FONT_HEIGHT (font);
24311
24312 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24313 height = 1;
24314
24315 /* Compute percentage of height used for ascent. If
24316 `:ascent ASCENT' is present and valid, use that. Otherwise,
24317 derive the ascent from the font in use. */
24318 if (prop = Fplist_get (plist, QCascent),
24319 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24320 ascent = height * NUMVAL (prop) / 100.0;
24321 else if (!NILP (prop)
24322 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24323 ascent = min (max (0, (int)tem), height);
24324 else
24325 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24326 }
24327 else
24328 #endif /* HAVE_WINDOW_SYSTEM */
24329 height = 1;
24330
24331 if (width > 0 && it->line_wrap != TRUNCATE
24332 && it->current_x + width > it->last_visible_x)
24333 {
24334 width = it->last_visible_x - it->current_x;
24335 #ifdef HAVE_WINDOW_SYSTEM
24336 /* Subtract one more pixel from the stretch width, but only on
24337 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24338 width -= FRAME_WINDOW_P (it->f);
24339 #endif
24340 }
24341
24342 if (width > 0 && height > 0 && it->glyph_row)
24343 {
24344 Lisp_Object o_object = it->object;
24345 Lisp_Object object = it->stack[it->sp - 1].string;
24346 int n = width;
24347
24348 if (!STRINGP (object))
24349 object = it->w->buffer;
24350 #ifdef HAVE_WINDOW_SYSTEM
24351 if (FRAME_WINDOW_P (it->f))
24352 append_stretch_glyph (it, object, width, height, ascent);
24353 else
24354 #endif
24355 {
24356 it->object = object;
24357 it->char_to_display = ' ';
24358 it->pixel_width = it->len = 1;
24359 while (n--)
24360 tty_append_glyph (it);
24361 it->object = o_object;
24362 }
24363 }
24364
24365 it->pixel_width = width;
24366 #ifdef HAVE_WINDOW_SYSTEM
24367 if (FRAME_WINDOW_P (it->f))
24368 {
24369 it->ascent = it->phys_ascent = ascent;
24370 it->descent = it->phys_descent = height - it->ascent;
24371 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24372 take_vertical_position_into_account (it);
24373 }
24374 else
24375 #endif
24376 it->nglyphs = width;
24377 }
24378
24379 /* Get information about special display element WHAT in an
24380 environment described by IT. WHAT is one of IT_TRUNCATION or
24381 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24382 non-null glyph_row member. This function ensures that fields like
24383 face_id, c, len of IT are left untouched. */
24384
24385 static void
24386 produce_special_glyphs (struct it *it, enum display_element_type what)
24387 {
24388 struct it temp_it;
24389 Lisp_Object gc;
24390 GLYPH glyph;
24391
24392 temp_it = *it;
24393 temp_it.object = make_number (0);
24394 memset (&temp_it.current, 0, sizeof temp_it.current);
24395
24396 if (what == IT_CONTINUATION)
24397 {
24398 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24399 if (it->bidi_it.paragraph_dir == R2L)
24400 SET_GLYPH_FROM_CHAR (glyph, '/');
24401 else
24402 SET_GLYPH_FROM_CHAR (glyph, '\\');
24403 if (it->dp
24404 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24405 {
24406 /* FIXME: Should we mirror GC for R2L lines? */
24407 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24408 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24409 }
24410 }
24411 else if (what == IT_TRUNCATION)
24412 {
24413 /* Truncation glyph. */
24414 SET_GLYPH_FROM_CHAR (glyph, '$');
24415 if (it->dp
24416 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24417 {
24418 /* FIXME: Should we mirror GC for R2L lines? */
24419 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24420 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24421 }
24422 }
24423 else
24424 emacs_abort ();
24425
24426 #ifdef HAVE_WINDOW_SYSTEM
24427 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24428 is turned off, we precede the truncation/continuation glyphs by a
24429 stretch glyph whose width is computed such that these special
24430 glyphs are aligned at the window margin, even when very different
24431 fonts are used in different glyph rows. */
24432 if (FRAME_WINDOW_P (temp_it.f)
24433 /* init_iterator calls this with it->glyph_row == NULL, and it
24434 wants only the pixel width of the truncation/continuation
24435 glyphs. */
24436 && temp_it.glyph_row
24437 /* insert_left_trunc_glyphs calls us at the beginning of the
24438 row, and it has its own calculation of the stretch glyph
24439 width. */
24440 && temp_it.glyph_row->used[TEXT_AREA] > 0
24441 && (temp_it.glyph_row->reversed_p
24442 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24443 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24444 {
24445 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24446
24447 if (stretch_width > 0)
24448 {
24449 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24450 struct font *font =
24451 face->font ? face->font : FRAME_FONT (temp_it.f);
24452 int stretch_ascent =
24453 (((temp_it.ascent + temp_it.descent)
24454 * FONT_BASE (font)) / FONT_HEIGHT (font));
24455
24456 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24457 temp_it.ascent + temp_it.descent,
24458 stretch_ascent);
24459 }
24460 }
24461 #endif
24462
24463 temp_it.dp = NULL;
24464 temp_it.what = IT_CHARACTER;
24465 temp_it.len = 1;
24466 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24467 temp_it.face_id = GLYPH_FACE (glyph);
24468 temp_it.len = CHAR_BYTES (temp_it.c);
24469
24470 PRODUCE_GLYPHS (&temp_it);
24471 it->pixel_width = temp_it.pixel_width;
24472 it->nglyphs = temp_it.pixel_width;
24473 }
24474
24475 #ifdef HAVE_WINDOW_SYSTEM
24476
24477 /* Calculate line-height and line-spacing properties.
24478 An integer value specifies explicit pixel value.
24479 A float value specifies relative value to current face height.
24480 A cons (float . face-name) specifies relative value to
24481 height of specified face font.
24482
24483 Returns height in pixels, or nil. */
24484
24485
24486 static Lisp_Object
24487 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24488 int boff, int override)
24489 {
24490 Lisp_Object face_name = Qnil;
24491 int ascent, descent, height;
24492
24493 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24494 return val;
24495
24496 if (CONSP (val))
24497 {
24498 face_name = XCAR (val);
24499 val = XCDR (val);
24500 if (!NUMBERP (val))
24501 val = make_number (1);
24502 if (NILP (face_name))
24503 {
24504 height = it->ascent + it->descent;
24505 goto scale;
24506 }
24507 }
24508
24509 if (NILP (face_name))
24510 {
24511 font = FRAME_FONT (it->f);
24512 boff = FRAME_BASELINE_OFFSET (it->f);
24513 }
24514 else if (EQ (face_name, Qt))
24515 {
24516 override = 0;
24517 }
24518 else
24519 {
24520 int face_id;
24521 struct face *face;
24522
24523 face_id = lookup_named_face (it->f, face_name, 0);
24524 if (face_id < 0)
24525 return make_number (-1);
24526
24527 face = FACE_FROM_ID (it->f, face_id);
24528 font = face->font;
24529 if (font == NULL)
24530 return make_number (-1);
24531 boff = font->baseline_offset;
24532 if (font->vertical_centering)
24533 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24534 }
24535
24536 ascent = FONT_BASE (font) + boff;
24537 descent = FONT_DESCENT (font) - boff;
24538
24539 if (override)
24540 {
24541 it->override_ascent = ascent;
24542 it->override_descent = descent;
24543 it->override_boff = boff;
24544 }
24545
24546 height = ascent + descent;
24547
24548 scale:
24549 if (FLOATP (val))
24550 height = (int)(XFLOAT_DATA (val) * height);
24551 else if (INTEGERP (val))
24552 height *= XINT (val);
24553
24554 return make_number (height);
24555 }
24556
24557
24558 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24559 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24560 and only if this is for a character for which no font was found.
24561
24562 If the display method (it->glyphless_method) is
24563 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24564 length of the acronym or the hexadecimal string, UPPER_XOFF and
24565 UPPER_YOFF are pixel offsets for the upper part of the string,
24566 LOWER_XOFF and LOWER_YOFF are for the lower part.
24567
24568 For the other display methods, LEN through LOWER_YOFF are zero. */
24569
24570 static void
24571 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24572 short upper_xoff, short upper_yoff,
24573 short lower_xoff, short lower_yoff)
24574 {
24575 struct glyph *glyph;
24576 enum glyph_row_area area = it->area;
24577
24578 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24579 if (glyph < it->glyph_row->glyphs[area + 1])
24580 {
24581 /* If the glyph row is reversed, we need to prepend the glyph
24582 rather than append it. */
24583 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24584 {
24585 struct glyph *g;
24586
24587 /* Make room for the additional glyph. */
24588 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24589 g[1] = *g;
24590 glyph = it->glyph_row->glyphs[area];
24591 }
24592 glyph->charpos = CHARPOS (it->position);
24593 glyph->object = it->object;
24594 glyph->pixel_width = it->pixel_width;
24595 glyph->ascent = it->ascent;
24596 glyph->descent = it->descent;
24597 glyph->voffset = it->voffset;
24598 glyph->type = GLYPHLESS_GLYPH;
24599 glyph->u.glyphless.method = it->glyphless_method;
24600 glyph->u.glyphless.for_no_font = for_no_font;
24601 glyph->u.glyphless.len = len;
24602 glyph->u.glyphless.ch = it->c;
24603 glyph->slice.glyphless.upper_xoff = upper_xoff;
24604 glyph->slice.glyphless.upper_yoff = upper_yoff;
24605 glyph->slice.glyphless.lower_xoff = lower_xoff;
24606 glyph->slice.glyphless.lower_yoff = lower_yoff;
24607 glyph->avoid_cursor_p = it->avoid_cursor_p;
24608 glyph->multibyte_p = it->multibyte_p;
24609 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24610 {
24611 /* In R2L rows, the left and the right box edges need to be
24612 drawn in reverse direction. */
24613 glyph->right_box_line_p = it->start_of_box_run_p;
24614 glyph->left_box_line_p = it->end_of_box_run_p;
24615 }
24616 else
24617 {
24618 glyph->left_box_line_p = it->start_of_box_run_p;
24619 glyph->right_box_line_p = it->end_of_box_run_p;
24620 }
24621 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24622 || it->phys_descent > it->descent);
24623 glyph->padding_p = 0;
24624 glyph->glyph_not_available_p = 0;
24625 glyph->face_id = face_id;
24626 glyph->font_type = FONT_TYPE_UNKNOWN;
24627 if (it->bidi_p)
24628 {
24629 glyph->resolved_level = it->bidi_it.resolved_level;
24630 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24631 emacs_abort ();
24632 glyph->bidi_type = it->bidi_it.type;
24633 }
24634 ++it->glyph_row->used[area];
24635 }
24636 else
24637 IT_EXPAND_MATRIX_WIDTH (it, area);
24638 }
24639
24640
24641 /* Produce a glyph for a glyphless character for iterator IT.
24642 IT->glyphless_method specifies which method to use for displaying
24643 the character. See the description of enum
24644 glyphless_display_method in dispextern.h for the detail.
24645
24646 FOR_NO_FONT is nonzero if and only if this is for a character for
24647 which no font was found. ACRONYM, if non-nil, is an acronym string
24648 for the character. */
24649
24650 static void
24651 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24652 {
24653 int face_id;
24654 struct face *face;
24655 struct font *font;
24656 int base_width, base_height, width, height;
24657 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24658 int len;
24659
24660 /* Get the metrics of the base font. We always refer to the current
24661 ASCII face. */
24662 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24663 font = face->font ? face->font : FRAME_FONT (it->f);
24664 it->ascent = FONT_BASE (font) + font->baseline_offset;
24665 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24666 base_height = it->ascent + it->descent;
24667 base_width = font->average_width;
24668
24669 /* Get a face ID for the glyph by utilizing a cache (the same way as
24670 done for `escape-glyph' in get_next_display_element). */
24671 if (it->f == last_glyphless_glyph_frame
24672 && it->face_id == last_glyphless_glyph_face_id)
24673 {
24674 face_id = last_glyphless_glyph_merged_face_id;
24675 }
24676 else
24677 {
24678 /* Merge the `glyphless-char' face into the current face. */
24679 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24680 last_glyphless_glyph_frame = it->f;
24681 last_glyphless_glyph_face_id = it->face_id;
24682 last_glyphless_glyph_merged_face_id = face_id;
24683 }
24684
24685 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24686 {
24687 it->pixel_width = THIN_SPACE_WIDTH;
24688 len = 0;
24689 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24690 }
24691 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24692 {
24693 width = CHAR_WIDTH (it->c);
24694 if (width == 0)
24695 width = 1;
24696 else if (width > 4)
24697 width = 4;
24698 it->pixel_width = base_width * width;
24699 len = 0;
24700 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24701 }
24702 else
24703 {
24704 char buf[7];
24705 const char *str;
24706 unsigned int code[6];
24707 int upper_len;
24708 int ascent, descent;
24709 struct font_metrics metrics_upper, metrics_lower;
24710
24711 face = FACE_FROM_ID (it->f, face_id);
24712 font = face->font ? face->font : FRAME_FONT (it->f);
24713 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24714
24715 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24716 {
24717 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24718 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24719 if (CONSP (acronym))
24720 acronym = XCAR (acronym);
24721 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24722 }
24723 else
24724 {
24725 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24726 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24727 str = buf;
24728 }
24729 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24730 code[len] = font->driver->encode_char (font, str[len]);
24731 upper_len = (len + 1) / 2;
24732 font->driver->text_extents (font, code, upper_len,
24733 &metrics_upper);
24734 font->driver->text_extents (font, code + upper_len, len - upper_len,
24735 &metrics_lower);
24736
24737
24738
24739 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24740 width = max (metrics_upper.width, metrics_lower.width) + 4;
24741 upper_xoff = upper_yoff = 2; /* the typical case */
24742 if (base_width >= width)
24743 {
24744 /* Align the upper to the left, the lower to the right. */
24745 it->pixel_width = base_width;
24746 lower_xoff = base_width - 2 - metrics_lower.width;
24747 }
24748 else
24749 {
24750 /* Center the shorter one. */
24751 it->pixel_width = width;
24752 if (metrics_upper.width >= metrics_lower.width)
24753 lower_xoff = (width - metrics_lower.width) / 2;
24754 else
24755 {
24756 /* FIXME: This code doesn't look right. It formerly was
24757 missing the "lower_xoff = 0;", which couldn't have
24758 been right since it left lower_xoff uninitialized. */
24759 lower_xoff = 0;
24760 upper_xoff = (width - metrics_upper.width) / 2;
24761 }
24762 }
24763
24764 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24765 top, bottom, and between upper and lower strings. */
24766 height = (metrics_upper.ascent + metrics_upper.descent
24767 + metrics_lower.ascent + metrics_lower.descent) + 5;
24768 /* Center vertically.
24769 H:base_height, D:base_descent
24770 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24771
24772 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24773 descent = D - H/2 + h/2;
24774 lower_yoff = descent - 2 - ld;
24775 upper_yoff = lower_yoff - la - 1 - ud; */
24776 ascent = - (it->descent - (base_height + height + 1) / 2);
24777 descent = it->descent - (base_height - height) / 2;
24778 lower_yoff = descent - 2 - metrics_lower.descent;
24779 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24780 - metrics_upper.descent);
24781 /* Don't make the height shorter than the base height. */
24782 if (height > base_height)
24783 {
24784 it->ascent = ascent;
24785 it->descent = descent;
24786 }
24787 }
24788
24789 it->phys_ascent = it->ascent;
24790 it->phys_descent = it->descent;
24791 if (it->glyph_row)
24792 append_glyphless_glyph (it, face_id, for_no_font, len,
24793 upper_xoff, upper_yoff,
24794 lower_xoff, lower_yoff);
24795 it->nglyphs = 1;
24796 take_vertical_position_into_account (it);
24797 }
24798
24799
24800 /* RIF:
24801 Produce glyphs/get display metrics for the display element IT is
24802 loaded with. See the description of struct it in dispextern.h
24803 for an overview of struct it. */
24804
24805 void
24806 x_produce_glyphs (struct it *it)
24807 {
24808 int extra_line_spacing = it->extra_line_spacing;
24809
24810 it->glyph_not_available_p = 0;
24811
24812 if (it->what == IT_CHARACTER)
24813 {
24814 XChar2b char2b;
24815 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24816 struct font *font = face->font;
24817 struct font_metrics *pcm = NULL;
24818 int boff; /* baseline offset */
24819
24820 if (font == NULL)
24821 {
24822 /* When no suitable font is found, display this character by
24823 the method specified in the first extra slot of
24824 Vglyphless_char_display. */
24825 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24826
24827 eassert (it->what == IT_GLYPHLESS);
24828 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24829 goto done;
24830 }
24831
24832 boff = font->baseline_offset;
24833 if (font->vertical_centering)
24834 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24835
24836 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24837 {
24838 int stretched_p;
24839
24840 it->nglyphs = 1;
24841
24842 if (it->override_ascent >= 0)
24843 {
24844 it->ascent = it->override_ascent;
24845 it->descent = it->override_descent;
24846 boff = it->override_boff;
24847 }
24848 else
24849 {
24850 it->ascent = FONT_BASE (font) + boff;
24851 it->descent = FONT_DESCENT (font) - boff;
24852 }
24853
24854 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24855 {
24856 pcm = get_per_char_metric (font, &char2b);
24857 if (pcm->width == 0
24858 && pcm->rbearing == 0 && pcm->lbearing == 0)
24859 pcm = NULL;
24860 }
24861
24862 if (pcm)
24863 {
24864 it->phys_ascent = pcm->ascent + boff;
24865 it->phys_descent = pcm->descent - boff;
24866 it->pixel_width = pcm->width;
24867 }
24868 else
24869 {
24870 it->glyph_not_available_p = 1;
24871 it->phys_ascent = it->ascent;
24872 it->phys_descent = it->descent;
24873 it->pixel_width = font->space_width;
24874 }
24875
24876 if (it->constrain_row_ascent_descent_p)
24877 {
24878 if (it->descent > it->max_descent)
24879 {
24880 it->ascent += it->descent - it->max_descent;
24881 it->descent = it->max_descent;
24882 }
24883 if (it->ascent > it->max_ascent)
24884 {
24885 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24886 it->ascent = it->max_ascent;
24887 }
24888 it->phys_ascent = min (it->phys_ascent, it->ascent);
24889 it->phys_descent = min (it->phys_descent, it->descent);
24890 extra_line_spacing = 0;
24891 }
24892
24893 /* If this is a space inside a region of text with
24894 `space-width' property, change its width. */
24895 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24896 if (stretched_p)
24897 it->pixel_width *= XFLOATINT (it->space_width);
24898
24899 /* If face has a box, add the box thickness to the character
24900 height. If character has a box line to the left and/or
24901 right, add the box line width to the character's width. */
24902 if (face->box != FACE_NO_BOX)
24903 {
24904 int thick = face->box_line_width;
24905
24906 if (thick > 0)
24907 {
24908 it->ascent += thick;
24909 it->descent += thick;
24910 }
24911 else
24912 thick = -thick;
24913
24914 if (it->start_of_box_run_p)
24915 it->pixel_width += thick;
24916 if (it->end_of_box_run_p)
24917 it->pixel_width += thick;
24918 }
24919
24920 /* If face has an overline, add the height of the overline
24921 (1 pixel) and a 1 pixel margin to the character height. */
24922 if (face->overline_p)
24923 it->ascent += overline_margin;
24924
24925 if (it->constrain_row_ascent_descent_p)
24926 {
24927 if (it->ascent > it->max_ascent)
24928 it->ascent = it->max_ascent;
24929 if (it->descent > it->max_descent)
24930 it->descent = it->max_descent;
24931 }
24932
24933 take_vertical_position_into_account (it);
24934
24935 /* If we have to actually produce glyphs, do it. */
24936 if (it->glyph_row)
24937 {
24938 if (stretched_p)
24939 {
24940 /* Translate a space with a `space-width' property
24941 into a stretch glyph. */
24942 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24943 / FONT_HEIGHT (font));
24944 append_stretch_glyph (it, it->object, it->pixel_width,
24945 it->ascent + it->descent, ascent);
24946 }
24947 else
24948 append_glyph (it);
24949
24950 /* If characters with lbearing or rbearing are displayed
24951 in this line, record that fact in a flag of the
24952 glyph row. This is used to optimize X output code. */
24953 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24954 it->glyph_row->contains_overlapping_glyphs_p = 1;
24955 }
24956 if (! stretched_p && it->pixel_width == 0)
24957 /* We assure that all visible glyphs have at least 1-pixel
24958 width. */
24959 it->pixel_width = 1;
24960 }
24961 else if (it->char_to_display == '\n')
24962 {
24963 /* A newline has no width, but we need the height of the
24964 line. But if previous part of the line sets a height,
24965 don't increase that height */
24966
24967 Lisp_Object height;
24968 Lisp_Object total_height = Qnil;
24969
24970 it->override_ascent = -1;
24971 it->pixel_width = 0;
24972 it->nglyphs = 0;
24973
24974 height = get_it_property (it, Qline_height);
24975 /* Split (line-height total-height) list */
24976 if (CONSP (height)
24977 && CONSP (XCDR (height))
24978 && NILP (XCDR (XCDR (height))))
24979 {
24980 total_height = XCAR (XCDR (height));
24981 height = XCAR (height);
24982 }
24983 height = calc_line_height_property (it, height, font, boff, 1);
24984
24985 if (it->override_ascent >= 0)
24986 {
24987 it->ascent = it->override_ascent;
24988 it->descent = it->override_descent;
24989 boff = it->override_boff;
24990 }
24991 else
24992 {
24993 it->ascent = FONT_BASE (font) + boff;
24994 it->descent = FONT_DESCENT (font) - boff;
24995 }
24996
24997 if (EQ (height, Qt))
24998 {
24999 if (it->descent > it->max_descent)
25000 {
25001 it->ascent += it->descent - it->max_descent;
25002 it->descent = it->max_descent;
25003 }
25004 if (it->ascent > it->max_ascent)
25005 {
25006 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25007 it->ascent = it->max_ascent;
25008 }
25009 it->phys_ascent = min (it->phys_ascent, it->ascent);
25010 it->phys_descent = min (it->phys_descent, it->descent);
25011 it->constrain_row_ascent_descent_p = 1;
25012 extra_line_spacing = 0;
25013 }
25014 else
25015 {
25016 Lisp_Object spacing;
25017
25018 it->phys_ascent = it->ascent;
25019 it->phys_descent = it->descent;
25020
25021 if ((it->max_ascent > 0 || it->max_descent > 0)
25022 && face->box != FACE_NO_BOX
25023 && face->box_line_width > 0)
25024 {
25025 it->ascent += face->box_line_width;
25026 it->descent += face->box_line_width;
25027 }
25028 if (!NILP (height)
25029 && XINT (height) > it->ascent + it->descent)
25030 it->ascent = XINT (height) - it->descent;
25031
25032 if (!NILP (total_height))
25033 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25034 else
25035 {
25036 spacing = get_it_property (it, Qline_spacing);
25037 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25038 }
25039 if (INTEGERP (spacing))
25040 {
25041 extra_line_spacing = XINT (spacing);
25042 if (!NILP (total_height))
25043 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25044 }
25045 }
25046 }
25047 else /* i.e. (it->char_to_display == '\t') */
25048 {
25049 if (font->space_width > 0)
25050 {
25051 int tab_width = it->tab_width * font->space_width;
25052 int x = it->current_x + it->continuation_lines_width;
25053 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25054
25055 /* If the distance from the current position to the next tab
25056 stop is less than a space character width, use the
25057 tab stop after that. */
25058 if (next_tab_x - x < font->space_width)
25059 next_tab_x += tab_width;
25060
25061 it->pixel_width = next_tab_x - x;
25062 it->nglyphs = 1;
25063 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25064 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25065
25066 if (it->glyph_row)
25067 {
25068 append_stretch_glyph (it, it->object, it->pixel_width,
25069 it->ascent + it->descent, it->ascent);
25070 }
25071 }
25072 else
25073 {
25074 it->pixel_width = 0;
25075 it->nglyphs = 1;
25076 }
25077 }
25078 }
25079 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25080 {
25081 /* A static composition.
25082
25083 Note: A composition is represented as one glyph in the
25084 glyph matrix. There are no padding glyphs.
25085
25086 Important note: pixel_width, ascent, and descent are the
25087 values of what is drawn by draw_glyphs (i.e. the values of
25088 the overall glyphs composed). */
25089 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25090 int boff; /* baseline offset */
25091 struct composition *cmp = composition_table[it->cmp_it.id];
25092 int glyph_len = cmp->glyph_len;
25093 struct font *font = face->font;
25094
25095 it->nglyphs = 1;
25096
25097 /* If we have not yet calculated pixel size data of glyphs of
25098 the composition for the current face font, calculate them
25099 now. Theoretically, we have to check all fonts for the
25100 glyphs, but that requires much time and memory space. So,
25101 here we check only the font of the first glyph. This may
25102 lead to incorrect display, but it's very rare, and C-l
25103 (recenter-top-bottom) can correct the display anyway. */
25104 if (! cmp->font || cmp->font != font)
25105 {
25106 /* Ascent and descent of the font of the first character
25107 of this composition (adjusted by baseline offset).
25108 Ascent and descent of overall glyphs should not be less
25109 than these, respectively. */
25110 int font_ascent, font_descent, font_height;
25111 /* Bounding box of the overall glyphs. */
25112 int leftmost, rightmost, lowest, highest;
25113 int lbearing, rbearing;
25114 int i, width, ascent, descent;
25115 int left_padded = 0, right_padded = 0;
25116 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25117 XChar2b char2b;
25118 struct font_metrics *pcm;
25119 int font_not_found_p;
25120 ptrdiff_t pos;
25121
25122 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25123 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25124 break;
25125 if (glyph_len < cmp->glyph_len)
25126 right_padded = 1;
25127 for (i = 0; i < glyph_len; i++)
25128 {
25129 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25130 break;
25131 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25132 }
25133 if (i > 0)
25134 left_padded = 1;
25135
25136 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25137 : IT_CHARPOS (*it));
25138 /* If no suitable font is found, use the default font. */
25139 font_not_found_p = font == NULL;
25140 if (font_not_found_p)
25141 {
25142 face = face->ascii_face;
25143 font = face->font;
25144 }
25145 boff = font->baseline_offset;
25146 if (font->vertical_centering)
25147 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25148 font_ascent = FONT_BASE (font) + boff;
25149 font_descent = FONT_DESCENT (font) - boff;
25150 font_height = FONT_HEIGHT (font);
25151
25152 cmp->font = font;
25153
25154 pcm = NULL;
25155 if (! font_not_found_p)
25156 {
25157 get_char_face_and_encoding (it->f, c, it->face_id,
25158 &char2b, 0);
25159 pcm = get_per_char_metric (font, &char2b);
25160 }
25161
25162 /* Initialize the bounding box. */
25163 if (pcm)
25164 {
25165 width = cmp->glyph_len > 0 ? pcm->width : 0;
25166 ascent = pcm->ascent;
25167 descent = pcm->descent;
25168 lbearing = pcm->lbearing;
25169 rbearing = pcm->rbearing;
25170 }
25171 else
25172 {
25173 width = cmp->glyph_len > 0 ? font->space_width : 0;
25174 ascent = FONT_BASE (font);
25175 descent = FONT_DESCENT (font);
25176 lbearing = 0;
25177 rbearing = width;
25178 }
25179
25180 rightmost = width;
25181 leftmost = 0;
25182 lowest = - descent + boff;
25183 highest = ascent + boff;
25184
25185 if (! font_not_found_p
25186 && font->default_ascent
25187 && CHAR_TABLE_P (Vuse_default_ascent)
25188 && !NILP (Faref (Vuse_default_ascent,
25189 make_number (it->char_to_display))))
25190 highest = font->default_ascent + boff;
25191
25192 /* Draw the first glyph at the normal position. It may be
25193 shifted to right later if some other glyphs are drawn
25194 at the left. */
25195 cmp->offsets[i * 2] = 0;
25196 cmp->offsets[i * 2 + 1] = boff;
25197 cmp->lbearing = lbearing;
25198 cmp->rbearing = rbearing;
25199
25200 /* Set cmp->offsets for the remaining glyphs. */
25201 for (i++; i < glyph_len; i++)
25202 {
25203 int left, right, btm, top;
25204 int ch = COMPOSITION_GLYPH (cmp, i);
25205 int face_id;
25206 struct face *this_face;
25207
25208 if (ch == '\t')
25209 ch = ' ';
25210 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25211 this_face = FACE_FROM_ID (it->f, face_id);
25212 font = this_face->font;
25213
25214 if (font == NULL)
25215 pcm = NULL;
25216 else
25217 {
25218 get_char_face_and_encoding (it->f, ch, face_id,
25219 &char2b, 0);
25220 pcm = get_per_char_metric (font, &char2b);
25221 }
25222 if (! pcm)
25223 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25224 else
25225 {
25226 width = pcm->width;
25227 ascent = pcm->ascent;
25228 descent = pcm->descent;
25229 lbearing = pcm->lbearing;
25230 rbearing = pcm->rbearing;
25231 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25232 {
25233 /* Relative composition with or without
25234 alternate chars. */
25235 left = (leftmost + rightmost - width) / 2;
25236 btm = - descent + boff;
25237 if (font->relative_compose
25238 && (! CHAR_TABLE_P (Vignore_relative_composition)
25239 || NILP (Faref (Vignore_relative_composition,
25240 make_number (ch)))))
25241 {
25242
25243 if (- descent >= font->relative_compose)
25244 /* One extra pixel between two glyphs. */
25245 btm = highest + 1;
25246 else if (ascent <= 0)
25247 /* One extra pixel between two glyphs. */
25248 btm = lowest - 1 - ascent - descent;
25249 }
25250 }
25251 else
25252 {
25253 /* A composition rule is specified by an integer
25254 value that encodes global and new reference
25255 points (GREF and NREF). GREF and NREF are
25256 specified by numbers as below:
25257
25258 0---1---2 -- ascent
25259 | |
25260 | |
25261 | |
25262 9--10--11 -- center
25263 | |
25264 ---3---4---5--- baseline
25265 | |
25266 6---7---8 -- descent
25267 */
25268 int rule = COMPOSITION_RULE (cmp, i);
25269 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25270
25271 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25272 grefx = gref % 3, nrefx = nref % 3;
25273 grefy = gref / 3, nrefy = nref / 3;
25274 if (xoff)
25275 xoff = font_height * (xoff - 128) / 256;
25276 if (yoff)
25277 yoff = font_height * (yoff - 128) / 256;
25278
25279 left = (leftmost
25280 + grefx * (rightmost - leftmost) / 2
25281 - nrefx * width / 2
25282 + xoff);
25283
25284 btm = ((grefy == 0 ? highest
25285 : grefy == 1 ? 0
25286 : grefy == 2 ? lowest
25287 : (highest + lowest) / 2)
25288 - (nrefy == 0 ? ascent + descent
25289 : nrefy == 1 ? descent - boff
25290 : nrefy == 2 ? 0
25291 : (ascent + descent) / 2)
25292 + yoff);
25293 }
25294
25295 cmp->offsets[i * 2] = left;
25296 cmp->offsets[i * 2 + 1] = btm + descent;
25297
25298 /* Update the bounding box of the overall glyphs. */
25299 if (width > 0)
25300 {
25301 right = left + width;
25302 if (left < leftmost)
25303 leftmost = left;
25304 if (right > rightmost)
25305 rightmost = right;
25306 }
25307 top = btm + descent + ascent;
25308 if (top > highest)
25309 highest = top;
25310 if (btm < lowest)
25311 lowest = btm;
25312
25313 if (cmp->lbearing > left + lbearing)
25314 cmp->lbearing = left + lbearing;
25315 if (cmp->rbearing < left + rbearing)
25316 cmp->rbearing = left + rbearing;
25317 }
25318 }
25319
25320 /* If there are glyphs whose x-offsets are negative,
25321 shift all glyphs to the right and make all x-offsets
25322 non-negative. */
25323 if (leftmost < 0)
25324 {
25325 for (i = 0; i < cmp->glyph_len; i++)
25326 cmp->offsets[i * 2] -= leftmost;
25327 rightmost -= leftmost;
25328 cmp->lbearing -= leftmost;
25329 cmp->rbearing -= leftmost;
25330 }
25331
25332 if (left_padded && cmp->lbearing < 0)
25333 {
25334 for (i = 0; i < cmp->glyph_len; i++)
25335 cmp->offsets[i * 2] -= cmp->lbearing;
25336 rightmost -= cmp->lbearing;
25337 cmp->rbearing -= cmp->lbearing;
25338 cmp->lbearing = 0;
25339 }
25340 if (right_padded && rightmost < cmp->rbearing)
25341 {
25342 rightmost = cmp->rbearing;
25343 }
25344
25345 cmp->pixel_width = rightmost;
25346 cmp->ascent = highest;
25347 cmp->descent = - lowest;
25348 if (cmp->ascent < font_ascent)
25349 cmp->ascent = font_ascent;
25350 if (cmp->descent < font_descent)
25351 cmp->descent = font_descent;
25352 }
25353
25354 if (it->glyph_row
25355 && (cmp->lbearing < 0
25356 || cmp->rbearing > cmp->pixel_width))
25357 it->glyph_row->contains_overlapping_glyphs_p = 1;
25358
25359 it->pixel_width = cmp->pixel_width;
25360 it->ascent = it->phys_ascent = cmp->ascent;
25361 it->descent = it->phys_descent = cmp->descent;
25362 if (face->box != FACE_NO_BOX)
25363 {
25364 int thick = face->box_line_width;
25365
25366 if (thick > 0)
25367 {
25368 it->ascent += thick;
25369 it->descent += thick;
25370 }
25371 else
25372 thick = - thick;
25373
25374 if (it->start_of_box_run_p)
25375 it->pixel_width += thick;
25376 if (it->end_of_box_run_p)
25377 it->pixel_width += thick;
25378 }
25379
25380 /* If face has an overline, add the height of the overline
25381 (1 pixel) and a 1 pixel margin to the character height. */
25382 if (face->overline_p)
25383 it->ascent += overline_margin;
25384
25385 take_vertical_position_into_account (it);
25386 if (it->ascent < 0)
25387 it->ascent = 0;
25388 if (it->descent < 0)
25389 it->descent = 0;
25390
25391 if (it->glyph_row && cmp->glyph_len > 0)
25392 append_composite_glyph (it);
25393 }
25394 else if (it->what == IT_COMPOSITION)
25395 {
25396 /* A dynamic (automatic) composition. */
25397 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25398 Lisp_Object gstring;
25399 struct font_metrics metrics;
25400
25401 it->nglyphs = 1;
25402
25403 gstring = composition_gstring_from_id (it->cmp_it.id);
25404 it->pixel_width
25405 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25406 &metrics);
25407 if (it->glyph_row
25408 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25409 it->glyph_row->contains_overlapping_glyphs_p = 1;
25410 it->ascent = it->phys_ascent = metrics.ascent;
25411 it->descent = it->phys_descent = metrics.descent;
25412 if (face->box != FACE_NO_BOX)
25413 {
25414 int thick = face->box_line_width;
25415
25416 if (thick > 0)
25417 {
25418 it->ascent += thick;
25419 it->descent += thick;
25420 }
25421 else
25422 thick = - thick;
25423
25424 if (it->start_of_box_run_p)
25425 it->pixel_width += thick;
25426 if (it->end_of_box_run_p)
25427 it->pixel_width += thick;
25428 }
25429 /* If face has an overline, add the height of the overline
25430 (1 pixel) and a 1 pixel margin to the character height. */
25431 if (face->overline_p)
25432 it->ascent += overline_margin;
25433 take_vertical_position_into_account (it);
25434 if (it->ascent < 0)
25435 it->ascent = 0;
25436 if (it->descent < 0)
25437 it->descent = 0;
25438
25439 if (it->glyph_row)
25440 append_composite_glyph (it);
25441 }
25442 else if (it->what == IT_GLYPHLESS)
25443 produce_glyphless_glyph (it, 0, Qnil);
25444 else if (it->what == IT_IMAGE)
25445 produce_image_glyph (it);
25446 else if (it->what == IT_STRETCH)
25447 produce_stretch_glyph (it);
25448 #ifdef HAVE_XWIDGETS
25449 else if (it->what == IT_XWIDGET)
25450 produce_xwidget_glyph (it);
25451 #endif
25452 done:
25453 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25454 because this isn't true for images with `:ascent 100'. */
25455 eassert (it->ascent >= 0 && it->descent >= 0);
25456 if (it->area == TEXT_AREA)
25457 it->current_x += it->pixel_width;
25458
25459 if (extra_line_spacing > 0)
25460 {
25461 it->descent += extra_line_spacing;
25462 if (extra_line_spacing > it->max_extra_line_spacing)
25463 it->max_extra_line_spacing = extra_line_spacing;
25464 }
25465
25466 it->max_ascent = max (it->max_ascent, it->ascent);
25467 it->max_descent = max (it->max_descent, it->descent);
25468 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25469 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25470 }
25471
25472 /* EXPORT for RIF:
25473 Output LEN glyphs starting at START at the nominal cursor position.
25474 Advance the nominal cursor over the text. The global variable
25475 updated_window contains the window being updated, updated_row is
25476 the glyph row being updated, and updated_area is the area of that
25477 row being updated. */
25478
25479 void
25480 x_write_glyphs (struct glyph *start, int len)
25481 {
25482 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25483
25484 eassert (updated_window && updated_row);
25485 /* When the window is hscrolled, cursor hpos can legitimately be out
25486 of bounds, but we draw the cursor at the corresponding window
25487 margin in that case. */
25488 if (!updated_row->reversed_p && chpos < 0)
25489 chpos = 0;
25490 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25491 chpos = updated_row->used[TEXT_AREA] - 1;
25492
25493 block_input ();
25494
25495 /* Write glyphs. */
25496
25497 hpos = start - updated_row->glyphs[updated_area];
25498 x = draw_glyphs (updated_window, output_cursor.x,
25499 updated_row, updated_area,
25500 hpos, hpos + len,
25501 DRAW_NORMAL_TEXT, 0);
25502
25503 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25504 if (updated_area == TEXT_AREA
25505 && updated_window->phys_cursor_on_p
25506 && updated_window->phys_cursor.vpos == output_cursor.vpos
25507 && chpos >= hpos
25508 && chpos < hpos + len)
25509 updated_window->phys_cursor_on_p = 0;
25510
25511 unblock_input ();
25512
25513 /* Advance the output cursor. */
25514 output_cursor.hpos += len;
25515 output_cursor.x = x;
25516 }
25517
25518
25519 /* EXPORT for RIF:
25520 Insert LEN glyphs from START at the nominal cursor position. */
25521
25522 void
25523 x_insert_glyphs (struct glyph *start, int len)
25524 {
25525 struct frame *f;
25526 struct window *w;
25527 int line_height, shift_by_width, shifted_region_width;
25528 struct glyph_row *row;
25529 struct glyph *glyph;
25530 int frame_x, frame_y;
25531 ptrdiff_t hpos;
25532
25533 eassert (updated_window && updated_row);
25534 block_input ();
25535 w = updated_window;
25536 f = XFRAME (WINDOW_FRAME (w));
25537
25538 /* Get the height of the line we are in. */
25539 row = updated_row;
25540 line_height = row->height;
25541
25542 /* Get the width of the glyphs to insert. */
25543 shift_by_width = 0;
25544 for (glyph = start; glyph < start + len; ++glyph)
25545 shift_by_width += glyph->pixel_width;
25546
25547 /* Get the width of the region to shift right. */
25548 shifted_region_width = (window_box_width (w, updated_area)
25549 - output_cursor.x
25550 - shift_by_width);
25551
25552 /* Shift right. */
25553 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25554 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25555
25556 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25557 line_height, shift_by_width);
25558
25559 /* Write the glyphs. */
25560 hpos = start - row->glyphs[updated_area];
25561 draw_glyphs (w, output_cursor.x, row, updated_area,
25562 hpos, hpos + len,
25563 DRAW_NORMAL_TEXT, 0);
25564
25565 /* Advance the output cursor. */
25566 output_cursor.hpos += len;
25567 output_cursor.x += shift_by_width;
25568 unblock_input ();
25569 }
25570
25571
25572 /* EXPORT for RIF:
25573 Erase the current text line from the nominal cursor position
25574 (inclusive) to pixel column TO_X (exclusive). The idea is that
25575 everything from TO_X onward is already erased.
25576
25577 TO_X is a pixel position relative to updated_area of
25578 updated_window. TO_X == -1 means clear to the end of this area. */
25579
25580 void
25581 x_clear_end_of_line (int to_x)
25582 {
25583 struct frame *f;
25584 struct window *w = updated_window;
25585 int max_x, min_y, max_y;
25586 int from_x, from_y, to_y;
25587
25588 eassert (updated_window && updated_row);
25589 f = XFRAME (w->frame);
25590
25591 if (updated_row->full_width_p)
25592 max_x = WINDOW_TOTAL_WIDTH (w);
25593 else
25594 max_x = window_box_width (w, updated_area);
25595 max_y = window_text_bottom_y (w);
25596
25597 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25598 of window. For TO_X > 0, truncate to end of drawing area. */
25599 if (to_x == 0)
25600 return;
25601 else if (to_x < 0)
25602 to_x = max_x;
25603 else
25604 to_x = min (to_x, max_x);
25605
25606 to_y = min (max_y, output_cursor.y + updated_row->height);
25607
25608 /* Notice if the cursor will be cleared by this operation. */
25609 if (!updated_row->full_width_p)
25610 notice_overwritten_cursor (w, updated_area,
25611 output_cursor.x, -1,
25612 updated_row->y,
25613 MATRIX_ROW_BOTTOM_Y (updated_row));
25614
25615 from_x = output_cursor.x;
25616
25617 /* Translate to frame coordinates. */
25618 if (updated_row->full_width_p)
25619 {
25620 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25621 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25622 }
25623 else
25624 {
25625 int area_left = window_box_left (w, updated_area);
25626 from_x += area_left;
25627 to_x += area_left;
25628 }
25629
25630 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25631 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25632 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25633
25634 /* Prevent inadvertently clearing to end of the X window. */
25635 if (to_x > from_x && to_y > from_y)
25636 {
25637 block_input ();
25638 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25639 to_x - from_x, to_y - from_y);
25640 unblock_input ();
25641 }
25642 }
25643
25644 #endif /* HAVE_WINDOW_SYSTEM */
25645
25646
25647 \f
25648 /***********************************************************************
25649 Cursor types
25650 ***********************************************************************/
25651
25652 /* Value is the internal representation of the specified cursor type
25653 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25654 of the bar cursor. */
25655
25656 static enum text_cursor_kinds
25657 get_specified_cursor_type (Lisp_Object arg, int *width)
25658 {
25659 enum text_cursor_kinds type;
25660
25661 if (NILP (arg))
25662 return NO_CURSOR;
25663
25664 if (EQ (arg, Qbox))
25665 return FILLED_BOX_CURSOR;
25666
25667 if (EQ (arg, Qhollow))
25668 return HOLLOW_BOX_CURSOR;
25669
25670 if (EQ (arg, Qbar))
25671 {
25672 *width = 2;
25673 return BAR_CURSOR;
25674 }
25675
25676 if (CONSP (arg)
25677 && EQ (XCAR (arg), Qbar)
25678 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25679 {
25680 *width = XINT (XCDR (arg));
25681 return BAR_CURSOR;
25682 }
25683
25684 if (EQ (arg, Qhbar))
25685 {
25686 *width = 2;
25687 return HBAR_CURSOR;
25688 }
25689
25690 if (CONSP (arg)
25691 && EQ (XCAR (arg), Qhbar)
25692 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25693 {
25694 *width = XINT (XCDR (arg));
25695 return HBAR_CURSOR;
25696 }
25697
25698 /* Treat anything unknown as "hollow box cursor".
25699 It was bad to signal an error; people have trouble fixing
25700 .Xdefaults with Emacs, when it has something bad in it. */
25701 type = HOLLOW_BOX_CURSOR;
25702
25703 return type;
25704 }
25705
25706 /* Set the default cursor types for specified frame. */
25707 void
25708 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25709 {
25710 int width = 1;
25711 Lisp_Object tem;
25712
25713 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25714 FRAME_CURSOR_WIDTH (f) = width;
25715
25716 /* By default, set up the blink-off state depending on the on-state. */
25717
25718 tem = Fassoc (arg, Vblink_cursor_alist);
25719 if (!NILP (tem))
25720 {
25721 FRAME_BLINK_OFF_CURSOR (f)
25722 = get_specified_cursor_type (XCDR (tem), &width);
25723 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25724 }
25725 else
25726 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25727 }
25728
25729
25730 #ifdef HAVE_WINDOW_SYSTEM
25731
25732 /* Return the cursor we want to be displayed in window W. Return
25733 width of bar/hbar cursor through WIDTH arg. Return with
25734 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25735 (i.e. if the `system caret' should track this cursor).
25736
25737 In a mini-buffer window, we want the cursor only to appear if we
25738 are reading input from this window. For the selected window, we
25739 want the cursor type given by the frame parameter or buffer local
25740 setting of cursor-type. If explicitly marked off, draw no cursor.
25741 In all other cases, we want a hollow box cursor. */
25742
25743 static enum text_cursor_kinds
25744 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25745 int *active_cursor)
25746 {
25747 struct frame *f = XFRAME (w->frame);
25748 struct buffer *b = XBUFFER (w->buffer);
25749 int cursor_type = DEFAULT_CURSOR;
25750 Lisp_Object alt_cursor;
25751 int non_selected = 0;
25752
25753 *active_cursor = 1;
25754
25755 /* Echo area */
25756 if (cursor_in_echo_area
25757 && FRAME_HAS_MINIBUF_P (f)
25758 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25759 {
25760 if (w == XWINDOW (echo_area_window))
25761 {
25762 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25763 {
25764 *width = FRAME_CURSOR_WIDTH (f);
25765 return FRAME_DESIRED_CURSOR (f);
25766 }
25767 else
25768 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25769 }
25770
25771 *active_cursor = 0;
25772 non_selected = 1;
25773 }
25774
25775 /* Detect a nonselected window or nonselected frame. */
25776 else if (w != XWINDOW (f->selected_window)
25777 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25778 {
25779 *active_cursor = 0;
25780
25781 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25782 return NO_CURSOR;
25783
25784 non_selected = 1;
25785 }
25786
25787 /* Never display a cursor in a window in which cursor-type is nil. */
25788 if (NILP (BVAR (b, cursor_type)))
25789 return NO_CURSOR;
25790
25791 /* Get the normal cursor type for this window. */
25792 if (EQ (BVAR (b, cursor_type), Qt))
25793 {
25794 cursor_type = FRAME_DESIRED_CURSOR (f);
25795 *width = FRAME_CURSOR_WIDTH (f);
25796 }
25797 else
25798 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25799
25800 /* Use cursor-in-non-selected-windows instead
25801 for non-selected window or frame. */
25802 if (non_selected)
25803 {
25804 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25805 if (!EQ (Qt, alt_cursor))
25806 return get_specified_cursor_type (alt_cursor, width);
25807 /* t means modify the normal cursor type. */
25808 if (cursor_type == FILLED_BOX_CURSOR)
25809 cursor_type = HOLLOW_BOX_CURSOR;
25810 else if (cursor_type == BAR_CURSOR && *width > 1)
25811 --*width;
25812 return cursor_type;
25813 }
25814
25815 /* Use normal cursor if not blinked off. */
25816 if (!w->cursor_off_p)
25817 {
25818
25819 #ifdef HAVE_XWIDGETS
25820 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
25821 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
25822 return NO_CURSOR;
25823 }
25824 #endif
25825 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25826 {
25827 if (cursor_type == FILLED_BOX_CURSOR)
25828 {
25829 /* Using a block cursor on large images can be very annoying.
25830 So use a hollow cursor for "large" images.
25831 If image is not transparent (no mask), also use hollow cursor. */
25832 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25833 if (img != NULL && IMAGEP (img->spec))
25834 {
25835 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25836 where N = size of default frame font size.
25837 This should cover most of the "tiny" icons people may use. */
25838 if (!img->mask
25839 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25840 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25841 cursor_type = HOLLOW_BOX_CURSOR;
25842 }
25843 }
25844 else if (cursor_type != NO_CURSOR)
25845 {
25846 /* Display current only supports BOX and HOLLOW cursors for images.
25847 So for now, unconditionally use a HOLLOW cursor when cursor is
25848 not a solid box cursor. */
25849 cursor_type = HOLLOW_BOX_CURSOR;
25850 }
25851 }
25852 return cursor_type;
25853 }
25854
25855 /* Cursor is blinked off, so determine how to "toggle" it. */
25856
25857 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25858 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25859 return get_specified_cursor_type (XCDR (alt_cursor), width);
25860
25861 /* Then see if frame has specified a specific blink off cursor type. */
25862 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25863 {
25864 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25865 return FRAME_BLINK_OFF_CURSOR (f);
25866 }
25867
25868 #if 0
25869 /* Some people liked having a permanently visible blinking cursor,
25870 while others had very strong opinions against it. So it was
25871 decided to remove it. KFS 2003-09-03 */
25872
25873 /* Finally perform built-in cursor blinking:
25874 filled box <-> hollow box
25875 wide [h]bar <-> narrow [h]bar
25876 narrow [h]bar <-> no cursor
25877 other type <-> no cursor */
25878
25879 if (cursor_type == FILLED_BOX_CURSOR)
25880 return HOLLOW_BOX_CURSOR;
25881
25882 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25883 {
25884 *width = 1;
25885 return cursor_type;
25886 }
25887 #endif
25888
25889 return NO_CURSOR;
25890 }
25891
25892
25893 /* Notice when the text cursor of window W has been completely
25894 overwritten by a drawing operation that outputs glyphs in AREA
25895 starting at X0 and ending at X1 in the line starting at Y0 and
25896 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25897 the rest of the line after X0 has been written. Y coordinates
25898 are window-relative. */
25899
25900 static void
25901 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25902 int x0, int x1, int y0, int y1)
25903 {
25904 int cx0, cx1, cy0, cy1;
25905 struct glyph_row *row;
25906
25907 if (!w->phys_cursor_on_p)
25908 return;
25909 if (area != TEXT_AREA)
25910 return;
25911
25912 if (w->phys_cursor.vpos < 0
25913 || w->phys_cursor.vpos >= w->current_matrix->nrows
25914 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25915 !(row->enabled_p && row->displays_text_p)))
25916 return;
25917
25918 if (row->cursor_in_fringe_p)
25919 {
25920 row->cursor_in_fringe_p = 0;
25921 draw_fringe_bitmap (w, row, row->reversed_p);
25922 w->phys_cursor_on_p = 0;
25923 return;
25924 }
25925
25926 cx0 = w->phys_cursor.x;
25927 cx1 = cx0 + w->phys_cursor_width;
25928 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25929 return;
25930
25931 /* The cursor image will be completely removed from the
25932 screen if the output area intersects the cursor area in
25933 y-direction. When we draw in [y0 y1[, and some part of
25934 the cursor is at y < y0, that part must have been drawn
25935 before. When scrolling, the cursor is erased before
25936 actually scrolling, so we don't come here. When not
25937 scrolling, the rows above the old cursor row must have
25938 changed, and in this case these rows must have written
25939 over the cursor image.
25940
25941 Likewise if part of the cursor is below y1, with the
25942 exception of the cursor being in the first blank row at
25943 the buffer and window end because update_text_area
25944 doesn't draw that row. (Except when it does, but
25945 that's handled in update_text_area.) */
25946
25947 cy0 = w->phys_cursor.y;
25948 cy1 = cy0 + w->phys_cursor_height;
25949 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25950 return;
25951
25952 w->phys_cursor_on_p = 0;
25953 }
25954
25955 #endif /* HAVE_WINDOW_SYSTEM */
25956
25957 \f
25958 /************************************************************************
25959 Mouse Face
25960 ************************************************************************/
25961
25962 #ifdef HAVE_WINDOW_SYSTEM
25963
25964 /* EXPORT for RIF:
25965 Fix the display of area AREA of overlapping row ROW in window W
25966 with respect to the overlapping part OVERLAPS. */
25967
25968 void
25969 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25970 enum glyph_row_area area, int overlaps)
25971 {
25972 int i, x;
25973
25974 block_input ();
25975
25976 x = 0;
25977 for (i = 0; i < row->used[area];)
25978 {
25979 if (row->glyphs[area][i].overlaps_vertically_p)
25980 {
25981 int start = i, start_x = x;
25982
25983 do
25984 {
25985 x += row->glyphs[area][i].pixel_width;
25986 ++i;
25987 }
25988 while (i < row->used[area]
25989 && row->glyphs[area][i].overlaps_vertically_p);
25990
25991 draw_glyphs (w, start_x, row, area,
25992 start, i,
25993 DRAW_NORMAL_TEXT, overlaps);
25994 }
25995 else
25996 {
25997 x += row->glyphs[area][i].pixel_width;
25998 ++i;
25999 }
26000 }
26001
26002 unblock_input ();
26003 }
26004
26005
26006 /* EXPORT:
26007 Draw the cursor glyph of window W in glyph row ROW. See the
26008 comment of draw_glyphs for the meaning of HL. */
26009
26010 void
26011 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26012 enum draw_glyphs_face hl)
26013 {
26014 /* If cursor hpos is out of bounds, don't draw garbage. This can
26015 happen in mini-buffer windows when switching between echo area
26016 glyphs and mini-buffer. */
26017 if ((row->reversed_p
26018 ? (w->phys_cursor.hpos >= 0)
26019 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26020 {
26021 int on_p = w->phys_cursor_on_p;
26022 int x1;
26023 int hpos = w->phys_cursor.hpos;
26024
26025 /* When the window is hscrolled, cursor hpos can legitimately be
26026 out of bounds, but we draw the cursor at the corresponding
26027 window margin in that case. */
26028 if (!row->reversed_p && hpos < 0)
26029 hpos = 0;
26030 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26031 hpos = row->used[TEXT_AREA] - 1;
26032
26033 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26034 hl, 0);
26035 w->phys_cursor_on_p = on_p;
26036
26037 if (hl == DRAW_CURSOR)
26038 w->phys_cursor_width = x1 - w->phys_cursor.x;
26039 /* When we erase the cursor, and ROW is overlapped by other
26040 rows, make sure that these overlapping parts of other rows
26041 are redrawn. */
26042 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26043 {
26044 w->phys_cursor_width = x1 - w->phys_cursor.x;
26045
26046 if (row > w->current_matrix->rows
26047 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26048 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26049 OVERLAPS_ERASED_CURSOR);
26050
26051 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26052 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26053 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26054 OVERLAPS_ERASED_CURSOR);
26055 }
26056 }
26057 }
26058
26059
26060 /* EXPORT:
26061 Erase the image of a cursor of window W from the screen. */
26062
26063 void
26064 erase_phys_cursor (struct window *w)
26065 {
26066 struct frame *f = XFRAME (w->frame);
26067 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26068 int hpos = w->phys_cursor.hpos;
26069 int vpos = w->phys_cursor.vpos;
26070 int mouse_face_here_p = 0;
26071 struct glyph_matrix *active_glyphs = w->current_matrix;
26072 struct glyph_row *cursor_row;
26073 struct glyph *cursor_glyph;
26074 enum draw_glyphs_face hl;
26075
26076 /* No cursor displayed or row invalidated => nothing to do on the
26077 screen. */
26078 if (w->phys_cursor_type == NO_CURSOR)
26079 goto mark_cursor_off;
26080
26081 /* VPOS >= active_glyphs->nrows means that window has been resized.
26082 Don't bother to erase the cursor. */
26083 if (vpos >= active_glyphs->nrows)
26084 goto mark_cursor_off;
26085
26086 /* If row containing cursor is marked invalid, there is nothing we
26087 can do. */
26088 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26089 if (!cursor_row->enabled_p)
26090 goto mark_cursor_off;
26091
26092 /* If line spacing is > 0, old cursor may only be partially visible in
26093 window after split-window. So adjust visible height. */
26094 cursor_row->visible_height = min (cursor_row->visible_height,
26095 window_text_bottom_y (w) - cursor_row->y);
26096
26097 /* If row is completely invisible, don't attempt to delete a cursor which
26098 isn't there. This can happen if cursor is at top of a window, and
26099 we switch to a buffer with a header line in that window. */
26100 if (cursor_row->visible_height <= 0)
26101 goto mark_cursor_off;
26102
26103 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26104 if (cursor_row->cursor_in_fringe_p)
26105 {
26106 cursor_row->cursor_in_fringe_p = 0;
26107 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26108 goto mark_cursor_off;
26109 }
26110
26111 /* This can happen when the new row is shorter than the old one.
26112 In this case, either draw_glyphs or clear_end_of_line
26113 should have cleared the cursor. Note that we wouldn't be
26114 able to erase the cursor in this case because we don't have a
26115 cursor glyph at hand. */
26116 if ((cursor_row->reversed_p
26117 ? (w->phys_cursor.hpos < 0)
26118 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26119 goto mark_cursor_off;
26120
26121 /* When the window is hscrolled, cursor hpos can legitimately be out
26122 of bounds, but we draw the cursor at the corresponding window
26123 margin in that case. */
26124 if (!cursor_row->reversed_p && hpos < 0)
26125 hpos = 0;
26126 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26127 hpos = cursor_row->used[TEXT_AREA] - 1;
26128
26129 /* If the cursor is in the mouse face area, redisplay that when
26130 we clear the cursor. */
26131 if (! NILP (hlinfo->mouse_face_window)
26132 && coords_in_mouse_face_p (w, hpos, vpos)
26133 /* Don't redraw the cursor's spot in mouse face if it is at the
26134 end of a line (on a newline). The cursor appears there, but
26135 mouse highlighting does not. */
26136 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26137 mouse_face_here_p = 1;
26138
26139 /* Maybe clear the display under the cursor. */
26140 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26141 {
26142 int x, y, left_x;
26143 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26144 int width;
26145
26146 cursor_glyph = get_phys_cursor_glyph (w);
26147 if (cursor_glyph == NULL)
26148 goto mark_cursor_off;
26149
26150 width = cursor_glyph->pixel_width;
26151 left_x = window_box_left_offset (w, TEXT_AREA);
26152 x = w->phys_cursor.x;
26153 if (x < left_x)
26154 width -= left_x - x;
26155 width = min (width, window_box_width (w, TEXT_AREA) - x);
26156 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26157 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26158
26159 if (width > 0)
26160 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26161 }
26162
26163 /* Erase the cursor by redrawing the character underneath it. */
26164 if (mouse_face_here_p)
26165 hl = DRAW_MOUSE_FACE;
26166 else
26167 hl = DRAW_NORMAL_TEXT;
26168 draw_phys_cursor_glyph (w, cursor_row, hl);
26169
26170 mark_cursor_off:
26171 w->phys_cursor_on_p = 0;
26172 w->phys_cursor_type = NO_CURSOR;
26173 }
26174
26175
26176 /* EXPORT:
26177 Display or clear cursor of window W. If ON is zero, clear the
26178 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26179 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26180
26181 void
26182 display_and_set_cursor (struct window *w, int on,
26183 int hpos, int vpos, int x, int y)
26184 {
26185 struct frame *f = XFRAME (w->frame);
26186 int new_cursor_type;
26187 int new_cursor_width;
26188 int active_cursor;
26189 struct glyph_row *glyph_row;
26190 struct glyph *glyph;
26191
26192 /* This is pointless on invisible frames, and dangerous on garbaged
26193 windows and frames; in the latter case, the frame or window may
26194 be in the midst of changing its size, and x and y may be off the
26195 window. */
26196 if (! FRAME_VISIBLE_P (f)
26197 || FRAME_GARBAGED_P (f)
26198 || vpos >= w->current_matrix->nrows
26199 || hpos >= w->current_matrix->matrix_w)
26200 return;
26201
26202 /* If cursor is off and we want it off, return quickly. */
26203 if (!on && !w->phys_cursor_on_p)
26204 return;
26205
26206 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26207 /* If cursor row is not enabled, we don't really know where to
26208 display the cursor. */
26209 if (!glyph_row->enabled_p)
26210 {
26211 w->phys_cursor_on_p = 0;
26212 return;
26213 }
26214
26215 glyph = NULL;
26216 if (!glyph_row->exact_window_width_line_p
26217 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26218 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26219
26220 eassert (input_blocked_p ());
26221
26222 /* Set new_cursor_type to the cursor we want to be displayed. */
26223 new_cursor_type = get_window_cursor_type (w, glyph,
26224 &new_cursor_width, &active_cursor);
26225
26226 /* If cursor is currently being shown and we don't want it to be or
26227 it is in the wrong place, or the cursor type is not what we want,
26228 erase it. */
26229 if (w->phys_cursor_on_p
26230 && (!on
26231 || w->phys_cursor.x != x
26232 || w->phys_cursor.y != y
26233 || new_cursor_type != w->phys_cursor_type
26234 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26235 && new_cursor_width != w->phys_cursor_width)))
26236 erase_phys_cursor (w);
26237
26238 /* Don't check phys_cursor_on_p here because that flag is only set
26239 to zero in some cases where we know that the cursor has been
26240 completely erased, to avoid the extra work of erasing the cursor
26241 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26242 still not be visible, or it has only been partly erased. */
26243 if (on)
26244 {
26245 w->phys_cursor_ascent = glyph_row->ascent;
26246 w->phys_cursor_height = glyph_row->height;
26247
26248 /* Set phys_cursor_.* before x_draw_.* is called because some
26249 of them may need the information. */
26250 w->phys_cursor.x = x;
26251 w->phys_cursor.y = glyph_row->y;
26252 w->phys_cursor.hpos = hpos;
26253 w->phys_cursor.vpos = vpos;
26254 }
26255
26256 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26257 new_cursor_type, new_cursor_width,
26258 on, active_cursor);
26259 }
26260
26261
26262 /* Switch the display of W's cursor on or off, according to the value
26263 of ON. */
26264
26265 static void
26266 update_window_cursor (struct window *w, int on)
26267 {
26268 /* Don't update cursor in windows whose frame is in the process
26269 of being deleted. */
26270 if (w->current_matrix)
26271 {
26272 int hpos = w->phys_cursor.hpos;
26273 int vpos = w->phys_cursor.vpos;
26274 struct glyph_row *row;
26275
26276 if (vpos >= w->current_matrix->nrows
26277 || hpos >= w->current_matrix->matrix_w)
26278 return;
26279
26280 row = MATRIX_ROW (w->current_matrix, vpos);
26281
26282 /* When the window is hscrolled, cursor hpos can legitimately be
26283 out of bounds, but we draw the cursor at the corresponding
26284 window margin in that case. */
26285 if (!row->reversed_p && hpos < 0)
26286 hpos = 0;
26287 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26288 hpos = row->used[TEXT_AREA] - 1;
26289
26290 block_input ();
26291 display_and_set_cursor (w, on, hpos, vpos,
26292 w->phys_cursor.x, w->phys_cursor.y);
26293 unblock_input ();
26294 }
26295 }
26296
26297
26298 /* Call update_window_cursor with parameter ON_P on all leaf windows
26299 in the window tree rooted at W. */
26300
26301 static void
26302 update_cursor_in_window_tree (struct window *w, int on_p)
26303 {
26304 while (w)
26305 {
26306 if (!NILP (w->hchild))
26307 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26308 else if (!NILP (w->vchild))
26309 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26310 else
26311 update_window_cursor (w, on_p);
26312
26313 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26314 }
26315 }
26316
26317
26318 /* EXPORT:
26319 Display the cursor on window W, or clear it, according to ON_P.
26320 Don't change the cursor's position. */
26321
26322 void
26323 x_update_cursor (struct frame *f, int on_p)
26324 {
26325 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26326 }
26327
26328
26329 /* EXPORT:
26330 Clear the cursor of window W to background color, and mark the
26331 cursor as not shown. This is used when the text where the cursor
26332 is about to be rewritten. */
26333
26334 void
26335 x_clear_cursor (struct window *w)
26336 {
26337 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26338 update_window_cursor (w, 0);
26339 }
26340
26341 #endif /* HAVE_WINDOW_SYSTEM */
26342
26343 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26344 and MSDOS. */
26345 static void
26346 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26347 int start_hpos, int end_hpos,
26348 enum draw_glyphs_face draw)
26349 {
26350 #ifdef HAVE_WINDOW_SYSTEM
26351 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26352 {
26353 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26354 return;
26355 }
26356 #endif
26357 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26358 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26359 #endif
26360 }
26361
26362 /* Display the active region described by mouse_face_* according to DRAW. */
26363
26364 static void
26365 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26366 {
26367 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26368 struct frame *f = XFRAME (WINDOW_FRAME (w));
26369
26370 if (/* If window is in the process of being destroyed, don't bother
26371 to do anything. */
26372 w->current_matrix != NULL
26373 /* Don't update mouse highlight if hidden */
26374 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26375 /* Recognize when we are called to operate on rows that don't exist
26376 anymore. This can happen when a window is split. */
26377 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26378 {
26379 int phys_cursor_on_p = w->phys_cursor_on_p;
26380 struct glyph_row *row, *first, *last;
26381
26382 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26383 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26384
26385 for (row = first; row <= last && row->enabled_p; ++row)
26386 {
26387 int start_hpos, end_hpos, start_x;
26388
26389 /* For all but the first row, the highlight starts at column 0. */
26390 if (row == first)
26391 {
26392 /* R2L rows have BEG and END in reversed order, but the
26393 screen drawing geometry is always left to right. So
26394 we need to mirror the beginning and end of the
26395 highlighted area in R2L rows. */
26396 if (!row->reversed_p)
26397 {
26398 start_hpos = hlinfo->mouse_face_beg_col;
26399 start_x = hlinfo->mouse_face_beg_x;
26400 }
26401 else if (row == last)
26402 {
26403 start_hpos = hlinfo->mouse_face_end_col;
26404 start_x = hlinfo->mouse_face_end_x;
26405 }
26406 else
26407 {
26408 start_hpos = 0;
26409 start_x = 0;
26410 }
26411 }
26412 else if (row->reversed_p && row == last)
26413 {
26414 start_hpos = hlinfo->mouse_face_end_col;
26415 start_x = hlinfo->mouse_face_end_x;
26416 }
26417 else
26418 {
26419 start_hpos = 0;
26420 start_x = 0;
26421 }
26422
26423 if (row == last)
26424 {
26425 if (!row->reversed_p)
26426 end_hpos = hlinfo->mouse_face_end_col;
26427 else if (row == first)
26428 end_hpos = hlinfo->mouse_face_beg_col;
26429 else
26430 {
26431 end_hpos = row->used[TEXT_AREA];
26432 if (draw == DRAW_NORMAL_TEXT)
26433 row->fill_line_p = 1; /* Clear to end of line */
26434 }
26435 }
26436 else if (row->reversed_p && row == first)
26437 end_hpos = hlinfo->mouse_face_beg_col;
26438 else
26439 {
26440 end_hpos = row->used[TEXT_AREA];
26441 if (draw == DRAW_NORMAL_TEXT)
26442 row->fill_line_p = 1; /* Clear to end of line */
26443 }
26444
26445 if (end_hpos > start_hpos)
26446 {
26447 draw_row_with_mouse_face (w, start_x, row,
26448 start_hpos, end_hpos, draw);
26449
26450 row->mouse_face_p
26451 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26452 }
26453 }
26454
26455 #ifdef HAVE_WINDOW_SYSTEM
26456 /* When we've written over the cursor, arrange for it to
26457 be displayed again. */
26458 if (FRAME_WINDOW_P (f)
26459 && phys_cursor_on_p && !w->phys_cursor_on_p)
26460 {
26461 int hpos = w->phys_cursor.hpos;
26462
26463 /* When the window is hscrolled, cursor hpos can legitimately be
26464 out of bounds, but we draw the cursor at the corresponding
26465 window margin in that case. */
26466 if (!row->reversed_p && hpos < 0)
26467 hpos = 0;
26468 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26469 hpos = row->used[TEXT_AREA] - 1;
26470
26471 block_input ();
26472 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26473 w->phys_cursor.x, w->phys_cursor.y);
26474 unblock_input ();
26475 }
26476 #endif /* HAVE_WINDOW_SYSTEM */
26477 }
26478
26479 #ifdef HAVE_WINDOW_SYSTEM
26480 /* Change the mouse cursor. */
26481 if (FRAME_WINDOW_P (f))
26482 {
26483 if (draw == DRAW_NORMAL_TEXT
26484 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26485 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26486 else if (draw == DRAW_MOUSE_FACE)
26487 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26488 else
26489 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26490 }
26491 #endif /* HAVE_WINDOW_SYSTEM */
26492 }
26493
26494 /* EXPORT:
26495 Clear out the mouse-highlighted active region.
26496 Redraw it un-highlighted first. Value is non-zero if mouse
26497 face was actually drawn unhighlighted. */
26498
26499 int
26500 clear_mouse_face (Mouse_HLInfo *hlinfo)
26501 {
26502 int cleared = 0;
26503
26504 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26505 {
26506 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26507 cleared = 1;
26508 }
26509
26510 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26511 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26512 hlinfo->mouse_face_window = Qnil;
26513 hlinfo->mouse_face_overlay = Qnil;
26514 return cleared;
26515 }
26516
26517 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26518 within the mouse face on that window. */
26519 static int
26520 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26521 {
26522 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26523
26524 /* Quickly resolve the easy cases. */
26525 if (!(WINDOWP (hlinfo->mouse_face_window)
26526 && XWINDOW (hlinfo->mouse_face_window) == w))
26527 return 0;
26528 if (vpos < hlinfo->mouse_face_beg_row
26529 || vpos > hlinfo->mouse_face_end_row)
26530 return 0;
26531 if (vpos > hlinfo->mouse_face_beg_row
26532 && vpos < hlinfo->mouse_face_end_row)
26533 return 1;
26534
26535 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26536 {
26537 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26538 {
26539 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26540 return 1;
26541 }
26542 else if ((vpos == hlinfo->mouse_face_beg_row
26543 && hpos >= hlinfo->mouse_face_beg_col)
26544 || (vpos == hlinfo->mouse_face_end_row
26545 && hpos < hlinfo->mouse_face_end_col))
26546 return 1;
26547 }
26548 else
26549 {
26550 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26551 {
26552 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26553 return 1;
26554 }
26555 else if ((vpos == hlinfo->mouse_face_beg_row
26556 && hpos <= hlinfo->mouse_face_beg_col)
26557 || (vpos == hlinfo->mouse_face_end_row
26558 && hpos > hlinfo->mouse_face_end_col))
26559 return 1;
26560 }
26561 return 0;
26562 }
26563
26564
26565 /* EXPORT:
26566 Non-zero if physical cursor of window W is within mouse face. */
26567
26568 int
26569 cursor_in_mouse_face_p (struct window *w)
26570 {
26571 int hpos = w->phys_cursor.hpos;
26572 int vpos = w->phys_cursor.vpos;
26573 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26574
26575 /* When the window is hscrolled, cursor hpos can legitimately be out
26576 of bounds, but we draw the cursor at the corresponding window
26577 margin in that case. */
26578 if (!row->reversed_p && hpos < 0)
26579 hpos = 0;
26580 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26581 hpos = row->used[TEXT_AREA] - 1;
26582
26583 return coords_in_mouse_face_p (w, hpos, vpos);
26584 }
26585
26586
26587 \f
26588 /* Find the glyph rows START_ROW and END_ROW of window W that display
26589 characters between buffer positions START_CHARPOS and END_CHARPOS
26590 (excluding END_CHARPOS). DISP_STRING is a display string that
26591 covers these buffer positions. This is similar to
26592 row_containing_pos, but is more accurate when bidi reordering makes
26593 buffer positions change non-linearly with glyph rows. */
26594 static void
26595 rows_from_pos_range (struct window *w,
26596 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26597 Lisp_Object disp_string,
26598 struct glyph_row **start, struct glyph_row **end)
26599 {
26600 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26601 int last_y = window_text_bottom_y (w);
26602 struct glyph_row *row;
26603
26604 *start = NULL;
26605 *end = NULL;
26606
26607 while (!first->enabled_p
26608 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26609 first++;
26610
26611 /* Find the START row. */
26612 for (row = first;
26613 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26614 row++)
26615 {
26616 /* A row can potentially be the START row if the range of the
26617 characters it displays intersects the range
26618 [START_CHARPOS..END_CHARPOS). */
26619 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26620 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26621 /* See the commentary in row_containing_pos, for the
26622 explanation of the complicated way to check whether
26623 some position is beyond the end of the characters
26624 displayed by a row. */
26625 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26626 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26627 && !row->ends_at_zv_p
26628 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26629 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26630 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26631 && !row->ends_at_zv_p
26632 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26633 {
26634 /* Found a candidate row. Now make sure at least one of the
26635 glyphs it displays has a charpos from the range
26636 [START_CHARPOS..END_CHARPOS).
26637
26638 This is not obvious because bidi reordering could make
26639 buffer positions of a row be 1,2,3,102,101,100, and if we
26640 want to highlight characters in [50..60), we don't want
26641 this row, even though [50..60) does intersect [1..103),
26642 the range of character positions given by the row's start
26643 and end positions. */
26644 struct glyph *g = row->glyphs[TEXT_AREA];
26645 struct glyph *e = g + row->used[TEXT_AREA];
26646
26647 while (g < e)
26648 {
26649 if (((BUFFERP (g->object) || INTEGERP (g->object))
26650 && start_charpos <= g->charpos && g->charpos < end_charpos)
26651 /* A glyph that comes from DISP_STRING is by
26652 definition to be highlighted. */
26653 || EQ (g->object, disp_string))
26654 *start = row;
26655 g++;
26656 }
26657 if (*start)
26658 break;
26659 }
26660 }
26661
26662 /* Find the END row. */
26663 if (!*start
26664 /* If the last row is partially visible, start looking for END
26665 from that row, instead of starting from FIRST. */
26666 && !(row->enabled_p
26667 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26668 row = first;
26669 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26670 {
26671 struct glyph_row *next = row + 1;
26672 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26673
26674 if (!next->enabled_p
26675 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26676 /* The first row >= START whose range of displayed characters
26677 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26678 is the row END + 1. */
26679 || (start_charpos < next_start
26680 && end_charpos < next_start)
26681 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26682 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26683 && !next->ends_at_zv_p
26684 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26685 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26686 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26687 && !next->ends_at_zv_p
26688 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26689 {
26690 *end = row;
26691 break;
26692 }
26693 else
26694 {
26695 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26696 but none of the characters it displays are in the range, it is
26697 also END + 1. */
26698 struct glyph *g = next->glyphs[TEXT_AREA];
26699 struct glyph *s = g;
26700 struct glyph *e = g + next->used[TEXT_AREA];
26701
26702 while (g < e)
26703 {
26704 if (((BUFFERP (g->object) || INTEGERP (g->object))
26705 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26706 /* If the buffer position of the first glyph in
26707 the row is equal to END_CHARPOS, it means
26708 the last character to be highlighted is the
26709 newline of ROW, and we must consider NEXT as
26710 END, not END+1. */
26711 || (((!next->reversed_p && g == s)
26712 || (next->reversed_p && g == e - 1))
26713 && (g->charpos == end_charpos
26714 /* Special case for when NEXT is an
26715 empty line at ZV. */
26716 || (g->charpos == -1
26717 && !row->ends_at_zv_p
26718 && next_start == end_charpos)))))
26719 /* A glyph that comes from DISP_STRING is by
26720 definition to be highlighted. */
26721 || EQ (g->object, disp_string))
26722 break;
26723 g++;
26724 }
26725 if (g == e)
26726 {
26727 *end = row;
26728 break;
26729 }
26730 /* The first row that ends at ZV must be the last to be
26731 highlighted. */
26732 else if (next->ends_at_zv_p)
26733 {
26734 *end = next;
26735 break;
26736 }
26737 }
26738 }
26739 }
26740
26741 /* This function sets the mouse_face_* elements of HLINFO, assuming
26742 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26743 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26744 for the overlay or run of text properties specifying the mouse
26745 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26746 before-string and after-string that must also be highlighted.
26747 DISP_STRING, if non-nil, is a display string that may cover some
26748 or all of the highlighted text. */
26749
26750 static void
26751 mouse_face_from_buffer_pos (Lisp_Object window,
26752 Mouse_HLInfo *hlinfo,
26753 ptrdiff_t mouse_charpos,
26754 ptrdiff_t start_charpos,
26755 ptrdiff_t end_charpos,
26756 Lisp_Object before_string,
26757 Lisp_Object after_string,
26758 Lisp_Object disp_string)
26759 {
26760 struct window *w = XWINDOW (window);
26761 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26762 struct glyph_row *r1, *r2;
26763 struct glyph *glyph, *end;
26764 ptrdiff_t ignore, pos;
26765 int x;
26766
26767 eassert (NILP (disp_string) || STRINGP (disp_string));
26768 eassert (NILP (before_string) || STRINGP (before_string));
26769 eassert (NILP (after_string) || STRINGP (after_string));
26770
26771 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26772 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26773 if (r1 == NULL)
26774 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26775 /* If the before-string or display-string contains newlines,
26776 rows_from_pos_range skips to its last row. Move back. */
26777 if (!NILP (before_string) || !NILP (disp_string))
26778 {
26779 struct glyph_row *prev;
26780 while ((prev = r1 - 1, prev >= first)
26781 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26782 && prev->used[TEXT_AREA] > 0)
26783 {
26784 struct glyph *beg = prev->glyphs[TEXT_AREA];
26785 glyph = beg + prev->used[TEXT_AREA];
26786 while (--glyph >= beg && INTEGERP (glyph->object));
26787 if (glyph < beg
26788 || !(EQ (glyph->object, before_string)
26789 || EQ (glyph->object, disp_string)))
26790 break;
26791 r1 = prev;
26792 }
26793 }
26794 if (r2 == NULL)
26795 {
26796 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26797 hlinfo->mouse_face_past_end = 1;
26798 }
26799 else if (!NILP (after_string))
26800 {
26801 /* If the after-string has newlines, advance to its last row. */
26802 struct glyph_row *next;
26803 struct glyph_row *last
26804 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26805
26806 for (next = r2 + 1;
26807 next <= last
26808 && next->used[TEXT_AREA] > 0
26809 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26810 ++next)
26811 r2 = next;
26812 }
26813 /* The rest of the display engine assumes that mouse_face_beg_row is
26814 either above mouse_face_end_row or identical to it. But with
26815 bidi-reordered continued lines, the row for START_CHARPOS could
26816 be below the row for END_CHARPOS. If so, swap the rows and store
26817 them in correct order. */
26818 if (r1->y > r2->y)
26819 {
26820 struct glyph_row *tem = r2;
26821
26822 r2 = r1;
26823 r1 = tem;
26824 }
26825
26826 hlinfo->mouse_face_beg_y = r1->y;
26827 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26828 hlinfo->mouse_face_end_y = r2->y;
26829 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26830
26831 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26832 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26833 could be anywhere in the row and in any order. The strategy
26834 below is to find the leftmost and the rightmost glyph that
26835 belongs to either of these 3 strings, or whose position is
26836 between START_CHARPOS and END_CHARPOS, and highlight all the
26837 glyphs between those two. This may cover more than just the text
26838 between START_CHARPOS and END_CHARPOS if the range of characters
26839 strides the bidi level boundary, e.g. if the beginning is in R2L
26840 text while the end is in L2R text or vice versa. */
26841 if (!r1->reversed_p)
26842 {
26843 /* This row is in a left to right paragraph. Scan it left to
26844 right. */
26845 glyph = r1->glyphs[TEXT_AREA];
26846 end = glyph + r1->used[TEXT_AREA];
26847 x = r1->x;
26848
26849 /* Skip truncation glyphs at the start of the glyph row. */
26850 if (r1->displays_text_p)
26851 for (; glyph < end
26852 && INTEGERP (glyph->object)
26853 && glyph->charpos < 0;
26854 ++glyph)
26855 x += glyph->pixel_width;
26856
26857 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26858 or DISP_STRING, and the first glyph from buffer whose
26859 position is between START_CHARPOS and END_CHARPOS. */
26860 for (; glyph < end
26861 && !INTEGERP (glyph->object)
26862 && !EQ (glyph->object, disp_string)
26863 && !(BUFFERP (glyph->object)
26864 && (glyph->charpos >= start_charpos
26865 && glyph->charpos < end_charpos));
26866 ++glyph)
26867 {
26868 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26869 are present at buffer positions between START_CHARPOS and
26870 END_CHARPOS, or if they come from an overlay. */
26871 if (EQ (glyph->object, before_string))
26872 {
26873 pos = string_buffer_position (before_string,
26874 start_charpos);
26875 /* If pos == 0, it means before_string came from an
26876 overlay, not from a buffer position. */
26877 if (!pos || (pos >= start_charpos && pos < end_charpos))
26878 break;
26879 }
26880 else if (EQ (glyph->object, after_string))
26881 {
26882 pos = string_buffer_position (after_string, end_charpos);
26883 if (!pos || (pos >= start_charpos && pos < end_charpos))
26884 break;
26885 }
26886 x += glyph->pixel_width;
26887 }
26888 hlinfo->mouse_face_beg_x = x;
26889 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26890 }
26891 else
26892 {
26893 /* This row is in a right to left paragraph. Scan it right to
26894 left. */
26895 struct glyph *g;
26896
26897 end = r1->glyphs[TEXT_AREA] - 1;
26898 glyph = end + r1->used[TEXT_AREA];
26899
26900 /* Skip truncation glyphs at the start of the glyph row. */
26901 if (r1->displays_text_p)
26902 for (; glyph > end
26903 && INTEGERP (glyph->object)
26904 && glyph->charpos < 0;
26905 --glyph)
26906 ;
26907
26908 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26909 or DISP_STRING, and the first glyph from buffer whose
26910 position is between START_CHARPOS and END_CHARPOS. */
26911 for (; glyph > end
26912 && !INTEGERP (glyph->object)
26913 && !EQ (glyph->object, disp_string)
26914 && !(BUFFERP (glyph->object)
26915 && (glyph->charpos >= start_charpos
26916 && glyph->charpos < end_charpos));
26917 --glyph)
26918 {
26919 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26920 are present at buffer positions between START_CHARPOS and
26921 END_CHARPOS, or if they come from an overlay. */
26922 if (EQ (glyph->object, before_string))
26923 {
26924 pos = string_buffer_position (before_string, start_charpos);
26925 /* If pos == 0, it means before_string came from an
26926 overlay, not from a buffer position. */
26927 if (!pos || (pos >= start_charpos && pos < end_charpos))
26928 break;
26929 }
26930 else if (EQ (glyph->object, after_string))
26931 {
26932 pos = string_buffer_position (after_string, end_charpos);
26933 if (!pos || (pos >= start_charpos && pos < end_charpos))
26934 break;
26935 }
26936 }
26937
26938 glyph++; /* first glyph to the right of the highlighted area */
26939 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26940 x += g->pixel_width;
26941 hlinfo->mouse_face_beg_x = x;
26942 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26943 }
26944
26945 /* If the highlight ends in a different row, compute GLYPH and END
26946 for the end row. Otherwise, reuse the values computed above for
26947 the row where the highlight begins. */
26948 if (r2 != r1)
26949 {
26950 if (!r2->reversed_p)
26951 {
26952 glyph = r2->glyphs[TEXT_AREA];
26953 end = glyph + r2->used[TEXT_AREA];
26954 x = r2->x;
26955 }
26956 else
26957 {
26958 end = r2->glyphs[TEXT_AREA] - 1;
26959 glyph = end + r2->used[TEXT_AREA];
26960 }
26961 }
26962
26963 if (!r2->reversed_p)
26964 {
26965 /* Skip truncation and continuation glyphs near the end of the
26966 row, and also blanks and stretch glyphs inserted by
26967 extend_face_to_end_of_line. */
26968 while (end > glyph
26969 && INTEGERP ((end - 1)->object))
26970 --end;
26971 /* Scan the rest of the glyph row from the end, looking for the
26972 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26973 DISP_STRING, or whose position is between START_CHARPOS
26974 and END_CHARPOS */
26975 for (--end;
26976 end > glyph
26977 && !INTEGERP (end->object)
26978 && !EQ (end->object, disp_string)
26979 && !(BUFFERP (end->object)
26980 && (end->charpos >= start_charpos
26981 && end->charpos < end_charpos));
26982 --end)
26983 {
26984 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26985 are present at buffer positions between START_CHARPOS and
26986 END_CHARPOS, or if they come from an overlay. */
26987 if (EQ (end->object, before_string))
26988 {
26989 pos = string_buffer_position (before_string, start_charpos);
26990 if (!pos || (pos >= start_charpos && pos < end_charpos))
26991 break;
26992 }
26993 else if (EQ (end->object, after_string))
26994 {
26995 pos = string_buffer_position (after_string, end_charpos);
26996 if (!pos || (pos >= start_charpos && pos < end_charpos))
26997 break;
26998 }
26999 }
27000 /* Find the X coordinate of the last glyph to be highlighted. */
27001 for (; glyph <= end; ++glyph)
27002 x += glyph->pixel_width;
27003
27004 hlinfo->mouse_face_end_x = x;
27005 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27006 }
27007 else
27008 {
27009 /* Skip truncation and continuation glyphs near the end of the
27010 row, and also blanks and stretch glyphs inserted by
27011 extend_face_to_end_of_line. */
27012 x = r2->x;
27013 end++;
27014 while (end < glyph
27015 && INTEGERP (end->object))
27016 {
27017 x += end->pixel_width;
27018 ++end;
27019 }
27020 /* Scan the rest of the glyph row from the end, looking for the
27021 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27022 DISP_STRING, or whose position is between START_CHARPOS
27023 and END_CHARPOS */
27024 for ( ;
27025 end < glyph
27026 && !INTEGERP (end->object)
27027 && !EQ (end->object, disp_string)
27028 && !(BUFFERP (end->object)
27029 && (end->charpos >= start_charpos
27030 && end->charpos < end_charpos));
27031 ++end)
27032 {
27033 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27034 are present at buffer positions between START_CHARPOS and
27035 END_CHARPOS, or if they come from an overlay. */
27036 if (EQ (end->object, before_string))
27037 {
27038 pos = string_buffer_position (before_string, start_charpos);
27039 if (!pos || (pos >= start_charpos && pos < end_charpos))
27040 break;
27041 }
27042 else if (EQ (end->object, after_string))
27043 {
27044 pos = string_buffer_position (after_string, end_charpos);
27045 if (!pos || (pos >= start_charpos && pos < end_charpos))
27046 break;
27047 }
27048 x += end->pixel_width;
27049 }
27050 /* If we exited the above loop because we arrived at the last
27051 glyph of the row, and its buffer position is still not in
27052 range, it means the last character in range is the preceding
27053 newline. Bump the end column and x values to get past the
27054 last glyph. */
27055 if (end == glyph
27056 && BUFFERP (end->object)
27057 && (end->charpos < start_charpos
27058 || end->charpos >= end_charpos))
27059 {
27060 x += end->pixel_width;
27061 ++end;
27062 }
27063 hlinfo->mouse_face_end_x = x;
27064 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27065 }
27066
27067 hlinfo->mouse_face_window = window;
27068 hlinfo->mouse_face_face_id
27069 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27070 mouse_charpos + 1,
27071 !hlinfo->mouse_face_hidden, -1);
27072 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27073 }
27074
27075 /* The following function is not used anymore (replaced with
27076 mouse_face_from_string_pos), but I leave it here for the time
27077 being, in case someone would. */
27078
27079 #if 0 /* not used */
27080
27081 /* Find the position of the glyph for position POS in OBJECT in
27082 window W's current matrix, and return in *X, *Y the pixel
27083 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27084
27085 RIGHT_P non-zero means return the position of the right edge of the
27086 glyph, RIGHT_P zero means return the left edge position.
27087
27088 If no glyph for POS exists in the matrix, return the position of
27089 the glyph with the next smaller position that is in the matrix, if
27090 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27091 exists in the matrix, return the position of the glyph with the
27092 next larger position in OBJECT.
27093
27094 Value is non-zero if a glyph was found. */
27095
27096 static int
27097 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27098 int *hpos, int *vpos, int *x, int *y, int right_p)
27099 {
27100 int yb = window_text_bottom_y (w);
27101 struct glyph_row *r;
27102 struct glyph *best_glyph = NULL;
27103 struct glyph_row *best_row = NULL;
27104 int best_x = 0;
27105
27106 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27107 r->enabled_p && r->y < yb;
27108 ++r)
27109 {
27110 struct glyph *g = r->glyphs[TEXT_AREA];
27111 struct glyph *e = g + r->used[TEXT_AREA];
27112 int gx;
27113
27114 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27115 if (EQ (g->object, object))
27116 {
27117 if (g->charpos == pos)
27118 {
27119 best_glyph = g;
27120 best_x = gx;
27121 best_row = r;
27122 goto found;
27123 }
27124 else if (best_glyph == NULL
27125 || ((eabs (g->charpos - pos)
27126 < eabs (best_glyph->charpos - pos))
27127 && (right_p
27128 ? g->charpos < pos
27129 : g->charpos > pos)))
27130 {
27131 best_glyph = g;
27132 best_x = gx;
27133 best_row = r;
27134 }
27135 }
27136 }
27137
27138 found:
27139
27140 if (best_glyph)
27141 {
27142 *x = best_x;
27143 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27144
27145 if (right_p)
27146 {
27147 *x += best_glyph->pixel_width;
27148 ++*hpos;
27149 }
27150
27151 *y = best_row->y;
27152 *vpos = best_row - w->current_matrix->rows;
27153 }
27154
27155 return best_glyph != NULL;
27156 }
27157 #endif /* not used */
27158
27159 /* Find the positions of the first and the last glyphs in window W's
27160 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27161 (assumed to be a string), and return in HLINFO's mouse_face_*
27162 members the pixel and column/row coordinates of those glyphs. */
27163
27164 static void
27165 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27166 Lisp_Object object,
27167 ptrdiff_t startpos, ptrdiff_t endpos)
27168 {
27169 int yb = window_text_bottom_y (w);
27170 struct glyph_row *r;
27171 struct glyph *g, *e;
27172 int gx;
27173 int found = 0;
27174
27175 /* Find the glyph row with at least one position in the range
27176 [STARTPOS..ENDPOS], and the first glyph in that row whose
27177 position belongs to that range. */
27178 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27179 r->enabled_p && r->y < yb;
27180 ++r)
27181 {
27182 if (!r->reversed_p)
27183 {
27184 g = r->glyphs[TEXT_AREA];
27185 e = g + r->used[TEXT_AREA];
27186 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27187 if (EQ (g->object, object)
27188 && startpos <= g->charpos && g->charpos <= endpos)
27189 {
27190 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27191 hlinfo->mouse_face_beg_y = r->y;
27192 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27193 hlinfo->mouse_face_beg_x = gx;
27194 found = 1;
27195 break;
27196 }
27197 }
27198 else
27199 {
27200 struct glyph *g1;
27201
27202 e = r->glyphs[TEXT_AREA];
27203 g = e + r->used[TEXT_AREA];
27204 for ( ; g > e; --g)
27205 if (EQ ((g-1)->object, object)
27206 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27207 {
27208 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27209 hlinfo->mouse_face_beg_y = r->y;
27210 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27211 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27212 gx += g1->pixel_width;
27213 hlinfo->mouse_face_beg_x = gx;
27214 found = 1;
27215 break;
27216 }
27217 }
27218 if (found)
27219 break;
27220 }
27221
27222 if (!found)
27223 return;
27224
27225 /* Starting with the next row, look for the first row which does NOT
27226 include any glyphs whose positions are in the range. */
27227 for (++r; r->enabled_p && r->y < yb; ++r)
27228 {
27229 g = r->glyphs[TEXT_AREA];
27230 e = g + r->used[TEXT_AREA];
27231 found = 0;
27232 for ( ; g < e; ++g)
27233 if (EQ (g->object, object)
27234 && startpos <= g->charpos && g->charpos <= endpos)
27235 {
27236 found = 1;
27237 break;
27238 }
27239 if (!found)
27240 break;
27241 }
27242
27243 /* The highlighted region ends on the previous row. */
27244 r--;
27245
27246 /* Set the end row and its vertical pixel coordinate. */
27247 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27248 hlinfo->mouse_face_end_y = r->y;
27249
27250 /* Compute and set the end column and the end column's horizontal
27251 pixel coordinate. */
27252 if (!r->reversed_p)
27253 {
27254 g = r->glyphs[TEXT_AREA];
27255 e = g + r->used[TEXT_AREA];
27256 for ( ; e > g; --e)
27257 if (EQ ((e-1)->object, object)
27258 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27259 break;
27260 hlinfo->mouse_face_end_col = e - g;
27261
27262 for (gx = r->x; g < e; ++g)
27263 gx += g->pixel_width;
27264 hlinfo->mouse_face_end_x = gx;
27265 }
27266 else
27267 {
27268 e = r->glyphs[TEXT_AREA];
27269 g = e + r->used[TEXT_AREA];
27270 for (gx = r->x ; e < g; ++e)
27271 {
27272 if (EQ (e->object, object)
27273 && startpos <= e->charpos && e->charpos <= endpos)
27274 break;
27275 gx += e->pixel_width;
27276 }
27277 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27278 hlinfo->mouse_face_end_x = gx;
27279 }
27280 }
27281
27282 #ifdef HAVE_WINDOW_SYSTEM
27283
27284 /* See if position X, Y is within a hot-spot of an image. */
27285
27286 static int
27287 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27288 {
27289 if (!CONSP (hot_spot))
27290 return 0;
27291
27292 if (EQ (XCAR (hot_spot), Qrect))
27293 {
27294 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27295 Lisp_Object rect = XCDR (hot_spot);
27296 Lisp_Object tem;
27297 if (!CONSP (rect))
27298 return 0;
27299 if (!CONSP (XCAR (rect)))
27300 return 0;
27301 if (!CONSP (XCDR (rect)))
27302 return 0;
27303 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27304 return 0;
27305 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27306 return 0;
27307 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27308 return 0;
27309 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27310 return 0;
27311 return 1;
27312 }
27313 else if (EQ (XCAR (hot_spot), Qcircle))
27314 {
27315 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27316 Lisp_Object circ = XCDR (hot_spot);
27317 Lisp_Object lr, lx0, ly0;
27318 if (CONSP (circ)
27319 && CONSP (XCAR (circ))
27320 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27321 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27322 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27323 {
27324 double r = XFLOATINT (lr);
27325 double dx = XINT (lx0) - x;
27326 double dy = XINT (ly0) - y;
27327 return (dx * dx + dy * dy <= r * r);
27328 }
27329 }
27330 else if (EQ (XCAR (hot_spot), Qpoly))
27331 {
27332 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27333 if (VECTORP (XCDR (hot_spot)))
27334 {
27335 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27336 Lisp_Object *poly = v->contents;
27337 ptrdiff_t n = v->header.size;
27338 ptrdiff_t i;
27339 int inside = 0;
27340 Lisp_Object lx, ly;
27341 int x0, y0;
27342
27343 /* Need an even number of coordinates, and at least 3 edges. */
27344 if (n < 6 || n & 1)
27345 return 0;
27346
27347 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27348 If count is odd, we are inside polygon. Pixels on edges
27349 may or may not be included depending on actual geometry of the
27350 polygon. */
27351 if ((lx = poly[n-2], !INTEGERP (lx))
27352 || (ly = poly[n-1], !INTEGERP (lx)))
27353 return 0;
27354 x0 = XINT (lx), y0 = XINT (ly);
27355 for (i = 0; i < n; i += 2)
27356 {
27357 int x1 = x0, y1 = y0;
27358 if ((lx = poly[i], !INTEGERP (lx))
27359 || (ly = poly[i+1], !INTEGERP (ly)))
27360 return 0;
27361 x0 = XINT (lx), y0 = XINT (ly);
27362
27363 /* Does this segment cross the X line? */
27364 if (x0 >= x)
27365 {
27366 if (x1 >= x)
27367 continue;
27368 }
27369 else if (x1 < x)
27370 continue;
27371 if (y > y0 && y > y1)
27372 continue;
27373 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27374 inside = !inside;
27375 }
27376 return inside;
27377 }
27378 }
27379 return 0;
27380 }
27381
27382 Lisp_Object
27383 find_hot_spot (Lisp_Object map, int x, int y)
27384 {
27385 while (CONSP (map))
27386 {
27387 if (CONSP (XCAR (map))
27388 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27389 return XCAR (map);
27390 map = XCDR (map);
27391 }
27392
27393 return Qnil;
27394 }
27395
27396 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27397 3, 3, 0,
27398 doc: /* Lookup in image map MAP coordinates X and Y.
27399 An image map is an alist where each element has the format (AREA ID PLIST).
27400 An AREA is specified as either a rectangle, a circle, or a polygon:
27401 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27402 pixel coordinates of the upper left and bottom right corners.
27403 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27404 and the radius of the circle; r may be a float or integer.
27405 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27406 vector describes one corner in the polygon.
27407 Returns the alist element for the first matching AREA in MAP. */)
27408 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27409 {
27410 if (NILP (map))
27411 return Qnil;
27412
27413 CHECK_NUMBER (x);
27414 CHECK_NUMBER (y);
27415
27416 return find_hot_spot (map,
27417 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27418 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27419 }
27420
27421
27422 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27423 static void
27424 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27425 {
27426 /* Do not change cursor shape while dragging mouse. */
27427 if (!NILP (do_mouse_tracking))
27428 return;
27429
27430 if (!NILP (pointer))
27431 {
27432 if (EQ (pointer, Qarrow))
27433 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27434 else if (EQ (pointer, Qhand))
27435 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27436 else if (EQ (pointer, Qtext))
27437 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27438 else if (EQ (pointer, intern ("hdrag")))
27439 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27440 #ifdef HAVE_X_WINDOWS
27441 else if (EQ (pointer, intern ("vdrag")))
27442 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27443 #endif
27444 else if (EQ (pointer, intern ("hourglass")))
27445 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27446 else if (EQ (pointer, Qmodeline))
27447 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27448 else
27449 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27450 }
27451
27452 if (cursor != No_Cursor)
27453 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27454 }
27455
27456 #endif /* HAVE_WINDOW_SYSTEM */
27457
27458 /* Take proper action when mouse has moved to the mode or header line
27459 or marginal area AREA of window W, x-position X and y-position Y.
27460 X is relative to the start of the text display area of W, so the
27461 width of bitmap areas and scroll bars must be subtracted to get a
27462 position relative to the start of the mode line. */
27463
27464 static void
27465 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27466 enum window_part area)
27467 {
27468 struct window *w = XWINDOW (window);
27469 struct frame *f = XFRAME (w->frame);
27470 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27471 #ifdef HAVE_WINDOW_SYSTEM
27472 Display_Info *dpyinfo;
27473 #endif
27474 Cursor cursor = No_Cursor;
27475 Lisp_Object pointer = Qnil;
27476 int dx, dy, width, height;
27477 ptrdiff_t charpos;
27478 Lisp_Object string, object = Qnil;
27479 Lisp_Object pos IF_LINT (= Qnil), help;
27480
27481 Lisp_Object mouse_face;
27482 int original_x_pixel = x;
27483 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27484 struct glyph_row *row IF_LINT (= 0);
27485
27486 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27487 {
27488 int x0;
27489 struct glyph *end;
27490
27491 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27492 returns them in row/column units! */
27493 string = mode_line_string (w, area, &x, &y, &charpos,
27494 &object, &dx, &dy, &width, &height);
27495
27496 row = (area == ON_MODE_LINE
27497 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27498 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27499
27500 /* Find the glyph under the mouse pointer. */
27501 if (row->mode_line_p && row->enabled_p)
27502 {
27503 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27504 end = glyph + row->used[TEXT_AREA];
27505
27506 for (x0 = original_x_pixel;
27507 glyph < end && x0 >= glyph->pixel_width;
27508 ++glyph)
27509 x0 -= glyph->pixel_width;
27510
27511 if (glyph >= end)
27512 glyph = NULL;
27513 }
27514 }
27515 else
27516 {
27517 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27518 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27519 returns them in row/column units! */
27520 string = marginal_area_string (w, area, &x, &y, &charpos,
27521 &object, &dx, &dy, &width, &height);
27522 }
27523
27524 help = Qnil;
27525
27526 #ifdef HAVE_WINDOW_SYSTEM
27527 if (IMAGEP (object))
27528 {
27529 Lisp_Object image_map, hotspot;
27530 if ((image_map = Fplist_get (XCDR (object), QCmap),
27531 !NILP (image_map))
27532 && (hotspot = find_hot_spot (image_map, dx, dy),
27533 CONSP (hotspot))
27534 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27535 {
27536 Lisp_Object plist;
27537
27538 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27539 If so, we could look for mouse-enter, mouse-leave
27540 properties in PLIST (and do something...). */
27541 hotspot = XCDR (hotspot);
27542 if (CONSP (hotspot)
27543 && (plist = XCAR (hotspot), CONSP (plist)))
27544 {
27545 pointer = Fplist_get (plist, Qpointer);
27546 if (NILP (pointer))
27547 pointer = Qhand;
27548 help = Fplist_get (plist, Qhelp_echo);
27549 if (!NILP (help))
27550 {
27551 help_echo_string = help;
27552 XSETWINDOW (help_echo_window, w);
27553 help_echo_object = w->buffer;
27554 help_echo_pos = charpos;
27555 }
27556 }
27557 }
27558 if (NILP (pointer))
27559 pointer = Fplist_get (XCDR (object), QCpointer);
27560 }
27561 #endif /* HAVE_WINDOW_SYSTEM */
27562
27563 if (STRINGP (string))
27564 pos = make_number (charpos);
27565
27566 /* Set the help text and mouse pointer. If the mouse is on a part
27567 of the mode line without any text (e.g. past the right edge of
27568 the mode line text), use the default help text and pointer. */
27569 if (STRINGP (string) || area == ON_MODE_LINE)
27570 {
27571 /* Arrange to display the help by setting the global variables
27572 help_echo_string, help_echo_object, and help_echo_pos. */
27573 if (NILP (help))
27574 {
27575 if (STRINGP (string))
27576 help = Fget_text_property (pos, Qhelp_echo, string);
27577
27578 if (!NILP (help))
27579 {
27580 help_echo_string = help;
27581 XSETWINDOW (help_echo_window, w);
27582 help_echo_object = string;
27583 help_echo_pos = charpos;
27584 }
27585 else if (area == ON_MODE_LINE)
27586 {
27587 Lisp_Object default_help
27588 = buffer_local_value_1 (Qmode_line_default_help_echo,
27589 w->buffer);
27590
27591 if (STRINGP (default_help))
27592 {
27593 help_echo_string = default_help;
27594 XSETWINDOW (help_echo_window, w);
27595 help_echo_object = Qnil;
27596 help_echo_pos = -1;
27597 }
27598 }
27599 }
27600
27601 #ifdef HAVE_WINDOW_SYSTEM
27602 /* Change the mouse pointer according to what is under it. */
27603 if (FRAME_WINDOW_P (f))
27604 {
27605 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27606 if (STRINGP (string))
27607 {
27608 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27609
27610 if (NILP (pointer))
27611 pointer = Fget_text_property (pos, Qpointer, string);
27612
27613 /* Change the mouse pointer according to what is under X/Y. */
27614 if (NILP (pointer)
27615 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27616 {
27617 Lisp_Object map;
27618 map = Fget_text_property (pos, Qlocal_map, string);
27619 if (!KEYMAPP (map))
27620 map = Fget_text_property (pos, Qkeymap, string);
27621 if (!KEYMAPP (map))
27622 cursor = dpyinfo->vertical_scroll_bar_cursor;
27623 }
27624 }
27625 else
27626 /* Default mode-line pointer. */
27627 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27628 }
27629 #endif
27630 }
27631
27632 /* Change the mouse face according to what is under X/Y. */
27633 if (STRINGP (string))
27634 {
27635 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27636 if (!NILP (mouse_face)
27637 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27638 && glyph)
27639 {
27640 Lisp_Object b, e;
27641
27642 struct glyph * tmp_glyph;
27643
27644 int gpos;
27645 int gseq_length;
27646 int total_pixel_width;
27647 ptrdiff_t begpos, endpos, ignore;
27648
27649 int vpos, hpos;
27650
27651 b = Fprevious_single_property_change (make_number (charpos + 1),
27652 Qmouse_face, string, Qnil);
27653 if (NILP (b))
27654 begpos = 0;
27655 else
27656 begpos = XINT (b);
27657
27658 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27659 if (NILP (e))
27660 endpos = SCHARS (string);
27661 else
27662 endpos = XINT (e);
27663
27664 /* Calculate the glyph position GPOS of GLYPH in the
27665 displayed string, relative to the beginning of the
27666 highlighted part of the string.
27667
27668 Note: GPOS is different from CHARPOS. CHARPOS is the
27669 position of GLYPH in the internal string object. A mode
27670 line string format has structures which are converted to
27671 a flattened string by the Emacs Lisp interpreter. The
27672 internal string is an element of those structures. The
27673 displayed string is the flattened string. */
27674 tmp_glyph = row_start_glyph;
27675 while (tmp_glyph < glyph
27676 && (!(EQ (tmp_glyph->object, glyph->object)
27677 && begpos <= tmp_glyph->charpos
27678 && tmp_glyph->charpos < endpos)))
27679 tmp_glyph++;
27680 gpos = glyph - tmp_glyph;
27681
27682 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27683 the highlighted part of the displayed string to which
27684 GLYPH belongs. Note: GSEQ_LENGTH is different from
27685 SCHARS (STRING), because the latter returns the length of
27686 the internal string. */
27687 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27688 tmp_glyph > glyph
27689 && (!(EQ (tmp_glyph->object, glyph->object)
27690 && begpos <= tmp_glyph->charpos
27691 && tmp_glyph->charpos < endpos));
27692 tmp_glyph--)
27693 ;
27694 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27695
27696 /* Calculate the total pixel width of all the glyphs between
27697 the beginning of the highlighted area and GLYPH. */
27698 total_pixel_width = 0;
27699 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27700 total_pixel_width += tmp_glyph->pixel_width;
27701
27702 /* Pre calculation of re-rendering position. Note: X is in
27703 column units here, after the call to mode_line_string or
27704 marginal_area_string. */
27705 hpos = x - gpos;
27706 vpos = (area == ON_MODE_LINE
27707 ? (w->current_matrix)->nrows - 1
27708 : 0);
27709
27710 /* If GLYPH's position is included in the region that is
27711 already drawn in mouse face, we have nothing to do. */
27712 if ( EQ (window, hlinfo->mouse_face_window)
27713 && (!row->reversed_p
27714 ? (hlinfo->mouse_face_beg_col <= hpos
27715 && hpos < hlinfo->mouse_face_end_col)
27716 /* In R2L rows we swap BEG and END, see below. */
27717 : (hlinfo->mouse_face_end_col <= hpos
27718 && hpos < hlinfo->mouse_face_beg_col))
27719 && hlinfo->mouse_face_beg_row == vpos )
27720 return;
27721
27722 if (clear_mouse_face (hlinfo))
27723 cursor = No_Cursor;
27724
27725 if (!row->reversed_p)
27726 {
27727 hlinfo->mouse_face_beg_col = hpos;
27728 hlinfo->mouse_face_beg_x = original_x_pixel
27729 - (total_pixel_width + dx);
27730 hlinfo->mouse_face_end_col = hpos + gseq_length;
27731 hlinfo->mouse_face_end_x = 0;
27732 }
27733 else
27734 {
27735 /* In R2L rows, show_mouse_face expects BEG and END
27736 coordinates to be swapped. */
27737 hlinfo->mouse_face_end_col = hpos;
27738 hlinfo->mouse_face_end_x = original_x_pixel
27739 - (total_pixel_width + dx);
27740 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27741 hlinfo->mouse_face_beg_x = 0;
27742 }
27743
27744 hlinfo->mouse_face_beg_row = vpos;
27745 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27746 hlinfo->mouse_face_beg_y = 0;
27747 hlinfo->mouse_face_end_y = 0;
27748 hlinfo->mouse_face_past_end = 0;
27749 hlinfo->mouse_face_window = window;
27750
27751 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27752 charpos,
27753 0, 0, 0,
27754 &ignore,
27755 glyph->face_id,
27756 1);
27757 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27758
27759 if (NILP (pointer))
27760 pointer = Qhand;
27761 }
27762 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27763 clear_mouse_face (hlinfo);
27764 }
27765 #ifdef HAVE_WINDOW_SYSTEM
27766 if (FRAME_WINDOW_P (f))
27767 define_frame_cursor1 (f, cursor, pointer);
27768 #endif
27769 }
27770
27771
27772 /* EXPORT:
27773 Take proper action when the mouse has moved to position X, Y on
27774 frame F as regards highlighting characters that have mouse-face
27775 properties. Also de-highlighting chars where the mouse was before.
27776 X and Y can be negative or out of range. */
27777
27778 void
27779 note_mouse_highlight (struct frame *f, int x, int y)
27780 {
27781 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27782 enum window_part part = ON_NOTHING;
27783 Lisp_Object window;
27784 struct window *w;
27785 Cursor cursor = No_Cursor;
27786 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27787 struct buffer *b;
27788
27789 /* When a menu is active, don't highlight because this looks odd. */
27790 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27791 if (popup_activated ())
27792 return;
27793 #endif
27794
27795 if (NILP (Vmouse_highlight)
27796 || !f->glyphs_initialized_p
27797 || f->pointer_invisible)
27798 return;
27799
27800 hlinfo->mouse_face_mouse_x = x;
27801 hlinfo->mouse_face_mouse_y = y;
27802 hlinfo->mouse_face_mouse_frame = f;
27803
27804 if (hlinfo->mouse_face_defer)
27805 return;
27806
27807 /* Which window is that in? */
27808 window = window_from_coordinates (f, x, y, &part, 1);
27809
27810 /* If displaying active text in another window, clear that. */
27811 if (! EQ (window, hlinfo->mouse_face_window)
27812 /* Also clear if we move out of text area in same window. */
27813 || (!NILP (hlinfo->mouse_face_window)
27814 && !NILP (window)
27815 && part != ON_TEXT
27816 && part != ON_MODE_LINE
27817 && part != ON_HEADER_LINE))
27818 clear_mouse_face (hlinfo);
27819
27820 /* Not on a window -> return. */
27821 if (!WINDOWP (window))
27822 return;
27823
27824 /* Reset help_echo_string. It will get recomputed below. */
27825 help_echo_string = Qnil;
27826
27827 /* Convert to window-relative pixel coordinates. */
27828 w = XWINDOW (window);
27829 frame_to_window_pixel_xy (w, &x, &y);
27830
27831 #ifdef HAVE_WINDOW_SYSTEM
27832 /* Handle tool-bar window differently since it doesn't display a
27833 buffer. */
27834 if (EQ (window, f->tool_bar_window))
27835 {
27836 note_tool_bar_highlight (f, x, y);
27837 return;
27838 }
27839 #endif
27840
27841 /* Mouse is on the mode, header line or margin? */
27842 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27843 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27844 {
27845 note_mode_line_or_margin_highlight (window, x, y, part);
27846 return;
27847 }
27848
27849 #ifdef HAVE_WINDOW_SYSTEM
27850 if (part == ON_VERTICAL_BORDER)
27851 {
27852 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27853 help_echo_string = build_string ("drag-mouse-1: resize");
27854 }
27855 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27856 || part == ON_SCROLL_BAR)
27857 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27858 else
27859 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27860 #endif
27861
27862 /* Are we in a window whose display is up to date?
27863 And verify the buffer's text has not changed. */
27864 b = XBUFFER (w->buffer);
27865 if (part == ON_TEXT
27866 && w->window_end_valid
27867 && w->last_modified == BUF_MODIFF (b)
27868 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27869 {
27870 int hpos, vpos, dx, dy, area = LAST_AREA;
27871 ptrdiff_t pos;
27872 struct glyph *glyph;
27873 Lisp_Object object;
27874 Lisp_Object mouse_face = Qnil, position;
27875 Lisp_Object *overlay_vec = NULL;
27876 ptrdiff_t i, noverlays;
27877 struct buffer *obuf;
27878 ptrdiff_t obegv, ozv;
27879 int same_region;
27880
27881 /* Find the glyph under X/Y. */
27882 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27883
27884 #ifdef HAVE_WINDOW_SYSTEM
27885 /* Look for :pointer property on image. */
27886 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27887 {
27888 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27889 if (img != NULL && IMAGEP (img->spec))
27890 {
27891 Lisp_Object image_map, hotspot;
27892 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27893 !NILP (image_map))
27894 && (hotspot = find_hot_spot (image_map,
27895 glyph->slice.img.x + dx,
27896 glyph->slice.img.y + dy),
27897 CONSP (hotspot))
27898 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27899 {
27900 Lisp_Object plist;
27901
27902 /* Could check XCAR (hotspot) to see if we enter/leave
27903 this hot-spot.
27904 If so, we could look for mouse-enter, mouse-leave
27905 properties in PLIST (and do something...). */
27906 hotspot = XCDR (hotspot);
27907 if (CONSP (hotspot)
27908 && (plist = XCAR (hotspot), CONSP (plist)))
27909 {
27910 pointer = Fplist_get (plist, Qpointer);
27911 if (NILP (pointer))
27912 pointer = Qhand;
27913 help_echo_string = Fplist_get (plist, Qhelp_echo);
27914 if (!NILP (help_echo_string))
27915 {
27916 help_echo_window = window;
27917 help_echo_object = glyph->object;
27918 help_echo_pos = glyph->charpos;
27919 }
27920 }
27921 }
27922 if (NILP (pointer))
27923 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27924 }
27925 }
27926 #endif /* HAVE_WINDOW_SYSTEM */
27927
27928 /* Clear mouse face if X/Y not over text. */
27929 if (glyph == NULL
27930 || area != TEXT_AREA
27931 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27932 /* Glyph's OBJECT is an integer for glyphs inserted by the
27933 display engine for its internal purposes, like truncation
27934 and continuation glyphs and blanks beyond the end of
27935 line's text on text terminals. If we are over such a
27936 glyph, we are not over any text. */
27937 || INTEGERP (glyph->object)
27938 /* R2L rows have a stretch glyph at their front, which
27939 stands for no text, whereas L2R rows have no glyphs at
27940 all beyond the end of text. Treat such stretch glyphs
27941 like we do with NULL glyphs in L2R rows. */
27942 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27943 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27944 && glyph->type == STRETCH_GLYPH
27945 && glyph->avoid_cursor_p))
27946 {
27947 if (clear_mouse_face (hlinfo))
27948 cursor = No_Cursor;
27949 #ifdef HAVE_WINDOW_SYSTEM
27950 if (FRAME_WINDOW_P (f) && NILP (pointer))
27951 {
27952 if (area != TEXT_AREA)
27953 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27954 else
27955 pointer = Vvoid_text_area_pointer;
27956 }
27957 #endif
27958 goto set_cursor;
27959 }
27960
27961 pos = glyph->charpos;
27962 object = glyph->object;
27963 if (!STRINGP (object) && !BUFFERP (object))
27964 goto set_cursor;
27965
27966 /* If we get an out-of-range value, return now; avoid an error. */
27967 if (BUFFERP (object) && pos > BUF_Z (b))
27968 goto set_cursor;
27969
27970 /* Make the window's buffer temporarily current for
27971 overlays_at and compute_char_face. */
27972 obuf = current_buffer;
27973 current_buffer = b;
27974 obegv = BEGV;
27975 ozv = ZV;
27976 BEGV = BEG;
27977 ZV = Z;
27978
27979 /* Is this char mouse-active or does it have help-echo? */
27980 position = make_number (pos);
27981
27982 if (BUFFERP (object))
27983 {
27984 /* Put all the overlays we want in a vector in overlay_vec. */
27985 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27986 /* Sort overlays into increasing priority order. */
27987 noverlays = sort_overlays (overlay_vec, noverlays, w);
27988 }
27989 else
27990 noverlays = 0;
27991
27992 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27993
27994 if (same_region)
27995 cursor = No_Cursor;
27996
27997 /* Check mouse-face highlighting. */
27998 if (! same_region
27999 /* If there exists an overlay with mouse-face overlapping
28000 the one we are currently highlighting, we have to
28001 check if we enter the overlapping overlay, and then
28002 highlight only that. */
28003 || (OVERLAYP (hlinfo->mouse_face_overlay)
28004 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28005 {
28006 /* Find the highest priority overlay with a mouse-face. */
28007 Lisp_Object overlay = Qnil;
28008 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28009 {
28010 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28011 if (!NILP (mouse_face))
28012 overlay = overlay_vec[i];
28013 }
28014
28015 /* If we're highlighting the same overlay as before, there's
28016 no need to do that again. */
28017 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28018 goto check_help_echo;
28019 hlinfo->mouse_face_overlay = overlay;
28020
28021 /* Clear the display of the old active region, if any. */
28022 if (clear_mouse_face (hlinfo))
28023 cursor = No_Cursor;
28024
28025 /* If no overlay applies, get a text property. */
28026 if (NILP (overlay))
28027 mouse_face = Fget_text_property (position, Qmouse_face, object);
28028
28029 /* Next, compute the bounds of the mouse highlighting and
28030 display it. */
28031 if (!NILP (mouse_face) && STRINGP (object))
28032 {
28033 /* The mouse-highlighting comes from a display string
28034 with a mouse-face. */
28035 Lisp_Object s, e;
28036 ptrdiff_t ignore;
28037
28038 s = Fprevious_single_property_change
28039 (make_number (pos + 1), Qmouse_face, object, Qnil);
28040 e = Fnext_single_property_change
28041 (position, Qmouse_face, object, Qnil);
28042 if (NILP (s))
28043 s = make_number (0);
28044 if (NILP (e))
28045 e = make_number (SCHARS (object) - 1);
28046 mouse_face_from_string_pos (w, hlinfo, object,
28047 XINT (s), XINT (e));
28048 hlinfo->mouse_face_past_end = 0;
28049 hlinfo->mouse_face_window = window;
28050 hlinfo->mouse_face_face_id
28051 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28052 glyph->face_id, 1);
28053 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28054 cursor = No_Cursor;
28055 }
28056 else
28057 {
28058 /* The mouse-highlighting, if any, comes from an overlay
28059 or text property in the buffer. */
28060 Lisp_Object buffer IF_LINT (= Qnil);
28061 Lisp_Object disp_string IF_LINT (= Qnil);
28062
28063 if (STRINGP (object))
28064 {
28065 /* If we are on a display string with no mouse-face,
28066 check if the text under it has one. */
28067 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28068 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28069 pos = string_buffer_position (object, start);
28070 if (pos > 0)
28071 {
28072 mouse_face = get_char_property_and_overlay
28073 (make_number (pos), Qmouse_face, w->buffer, &overlay);
28074 buffer = w->buffer;
28075 disp_string = object;
28076 }
28077 }
28078 else
28079 {
28080 buffer = object;
28081 disp_string = Qnil;
28082 }
28083
28084 if (!NILP (mouse_face))
28085 {
28086 Lisp_Object before, after;
28087 Lisp_Object before_string, after_string;
28088 /* To correctly find the limits of mouse highlight
28089 in a bidi-reordered buffer, we must not use the
28090 optimization of limiting the search in
28091 previous-single-property-change and
28092 next-single-property-change, because
28093 rows_from_pos_range needs the real start and end
28094 positions to DTRT in this case. That's because
28095 the first row visible in a window does not
28096 necessarily display the character whose position
28097 is the smallest. */
28098 Lisp_Object lim1 =
28099 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28100 ? Fmarker_position (w->start)
28101 : Qnil;
28102 Lisp_Object lim2 =
28103 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28104 ? make_number (BUF_Z (XBUFFER (buffer))
28105 - XFASTINT (w->window_end_pos))
28106 : Qnil;
28107
28108 if (NILP (overlay))
28109 {
28110 /* Handle the text property case. */
28111 before = Fprevious_single_property_change
28112 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28113 after = Fnext_single_property_change
28114 (make_number (pos), Qmouse_face, buffer, lim2);
28115 before_string = after_string = Qnil;
28116 }
28117 else
28118 {
28119 /* Handle the overlay case. */
28120 before = Foverlay_start (overlay);
28121 after = Foverlay_end (overlay);
28122 before_string = Foverlay_get (overlay, Qbefore_string);
28123 after_string = Foverlay_get (overlay, Qafter_string);
28124
28125 if (!STRINGP (before_string)) before_string = Qnil;
28126 if (!STRINGP (after_string)) after_string = Qnil;
28127 }
28128
28129 mouse_face_from_buffer_pos (window, hlinfo, pos,
28130 NILP (before)
28131 ? 1
28132 : XFASTINT (before),
28133 NILP (after)
28134 ? BUF_Z (XBUFFER (buffer))
28135 : XFASTINT (after),
28136 before_string, after_string,
28137 disp_string);
28138 cursor = No_Cursor;
28139 }
28140 }
28141 }
28142
28143 check_help_echo:
28144
28145 /* Look for a `help-echo' property. */
28146 if (NILP (help_echo_string)) {
28147 Lisp_Object help, overlay;
28148
28149 /* Check overlays first. */
28150 help = overlay = Qnil;
28151 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28152 {
28153 overlay = overlay_vec[i];
28154 help = Foverlay_get (overlay, Qhelp_echo);
28155 }
28156
28157 if (!NILP (help))
28158 {
28159 help_echo_string = help;
28160 help_echo_window = window;
28161 help_echo_object = overlay;
28162 help_echo_pos = pos;
28163 }
28164 else
28165 {
28166 Lisp_Object obj = glyph->object;
28167 ptrdiff_t charpos = glyph->charpos;
28168
28169 /* Try text properties. */
28170 if (STRINGP (obj)
28171 && charpos >= 0
28172 && charpos < SCHARS (obj))
28173 {
28174 help = Fget_text_property (make_number (charpos),
28175 Qhelp_echo, obj);
28176 if (NILP (help))
28177 {
28178 /* If the string itself doesn't specify a help-echo,
28179 see if the buffer text ``under'' it does. */
28180 struct glyph_row *r
28181 = MATRIX_ROW (w->current_matrix, vpos);
28182 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28183 ptrdiff_t p = string_buffer_position (obj, start);
28184 if (p > 0)
28185 {
28186 help = Fget_char_property (make_number (p),
28187 Qhelp_echo, w->buffer);
28188 if (!NILP (help))
28189 {
28190 charpos = p;
28191 obj = w->buffer;
28192 }
28193 }
28194 }
28195 }
28196 else if (BUFFERP (obj)
28197 && charpos >= BEGV
28198 && charpos < ZV)
28199 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28200 obj);
28201
28202 if (!NILP (help))
28203 {
28204 help_echo_string = help;
28205 help_echo_window = window;
28206 help_echo_object = obj;
28207 help_echo_pos = charpos;
28208 }
28209 }
28210 }
28211
28212 #ifdef HAVE_WINDOW_SYSTEM
28213 /* Look for a `pointer' property. */
28214 if (FRAME_WINDOW_P (f) && NILP (pointer))
28215 {
28216 /* Check overlays first. */
28217 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28218 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28219
28220 if (NILP (pointer))
28221 {
28222 Lisp_Object obj = glyph->object;
28223 ptrdiff_t charpos = glyph->charpos;
28224
28225 /* Try text properties. */
28226 if (STRINGP (obj)
28227 && charpos >= 0
28228 && charpos < SCHARS (obj))
28229 {
28230 pointer = Fget_text_property (make_number (charpos),
28231 Qpointer, obj);
28232 if (NILP (pointer))
28233 {
28234 /* If the string itself doesn't specify a pointer,
28235 see if the buffer text ``under'' it does. */
28236 struct glyph_row *r
28237 = MATRIX_ROW (w->current_matrix, vpos);
28238 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28239 ptrdiff_t p = string_buffer_position (obj, start);
28240 if (p > 0)
28241 pointer = Fget_char_property (make_number (p),
28242 Qpointer, w->buffer);
28243 }
28244 }
28245 else if (BUFFERP (obj)
28246 && charpos >= BEGV
28247 && charpos < ZV)
28248 pointer = Fget_text_property (make_number (charpos),
28249 Qpointer, obj);
28250 }
28251 }
28252 #endif /* HAVE_WINDOW_SYSTEM */
28253
28254 BEGV = obegv;
28255 ZV = ozv;
28256 current_buffer = obuf;
28257 }
28258
28259 set_cursor:
28260
28261 #ifdef HAVE_WINDOW_SYSTEM
28262 if (FRAME_WINDOW_P (f))
28263 define_frame_cursor1 (f, cursor, pointer);
28264 #else
28265 /* This is here to prevent a compiler error, about "label at end of
28266 compound statement". */
28267 return;
28268 #endif
28269 }
28270
28271
28272 /* EXPORT for RIF:
28273 Clear any mouse-face on window W. This function is part of the
28274 redisplay interface, and is called from try_window_id and similar
28275 functions to ensure the mouse-highlight is off. */
28276
28277 void
28278 x_clear_window_mouse_face (struct window *w)
28279 {
28280 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28281 Lisp_Object window;
28282
28283 block_input ();
28284 XSETWINDOW (window, w);
28285 if (EQ (window, hlinfo->mouse_face_window))
28286 clear_mouse_face (hlinfo);
28287 unblock_input ();
28288 }
28289
28290
28291 /* EXPORT:
28292 Just discard the mouse face information for frame F, if any.
28293 This is used when the size of F is changed. */
28294
28295 void
28296 cancel_mouse_face (struct frame *f)
28297 {
28298 Lisp_Object window;
28299 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28300
28301 window = hlinfo->mouse_face_window;
28302 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28303 {
28304 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28305 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28306 hlinfo->mouse_face_window = Qnil;
28307 }
28308 }
28309
28310
28311 \f
28312 /***********************************************************************
28313 Exposure Events
28314 ***********************************************************************/
28315
28316 #ifdef HAVE_WINDOW_SYSTEM
28317
28318 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28319 which intersects rectangle R. R is in window-relative coordinates. */
28320
28321 static void
28322 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28323 enum glyph_row_area area)
28324 {
28325 struct glyph *first = row->glyphs[area];
28326 struct glyph *end = row->glyphs[area] + row->used[area];
28327 struct glyph *last;
28328 int first_x, start_x, x;
28329
28330 if (area == TEXT_AREA && row->fill_line_p)
28331 /* If row extends face to end of line write the whole line. */
28332 draw_glyphs (w, 0, row, area,
28333 0, row->used[area],
28334 DRAW_NORMAL_TEXT, 0);
28335 else
28336 {
28337 /* Set START_X to the window-relative start position for drawing glyphs of
28338 AREA. The first glyph of the text area can be partially visible.
28339 The first glyphs of other areas cannot. */
28340 start_x = window_box_left_offset (w, area);
28341 x = start_x;
28342 if (area == TEXT_AREA)
28343 x += row->x;
28344
28345 /* Find the first glyph that must be redrawn. */
28346 while (first < end
28347 && x + first->pixel_width < r->x)
28348 {
28349 x += first->pixel_width;
28350 ++first;
28351 }
28352
28353 /* Find the last one. */
28354 last = first;
28355 first_x = x;
28356 while (last < end
28357 && x < r->x + r->width)
28358 {
28359 x += last->pixel_width;
28360 ++last;
28361 }
28362
28363 /* Repaint. */
28364 if (last > first)
28365 draw_glyphs (w, first_x - start_x, row, area,
28366 first - row->glyphs[area], last - row->glyphs[area],
28367 DRAW_NORMAL_TEXT, 0);
28368 }
28369 }
28370
28371
28372 /* Redraw the parts of the glyph row ROW on window W intersecting
28373 rectangle R. R is in window-relative coordinates. Value is
28374 non-zero if mouse-face was overwritten. */
28375
28376 static int
28377 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28378 {
28379 eassert (row->enabled_p);
28380
28381 if (row->mode_line_p || w->pseudo_window_p)
28382 draw_glyphs (w, 0, row, TEXT_AREA,
28383 0, row->used[TEXT_AREA],
28384 DRAW_NORMAL_TEXT, 0);
28385 else
28386 {
28387 if (row->used[LEFT_MARGIN_AREA])
28388 expose_area (w, row, r, LEFT_MARGIN_AREA);
28389 if (row->used[TEXT_AREA])
28390 expose_area (w, row, r, TEXT_AREA);
28391 if (row->used[RIGHT_MARGIN_AREA])
28392 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28393 draw_row_fringe_bitmaps (w, row);
28394 }
28395
28396 return row->mouse_face_p;
28397 }
28398
28399
28400 /* Redraw those parts of glyphs rows during expose event handling that
28401 overlap other rows. Redrawing of an exposed line writes over parts
28402 of lines overlapping that exposed line; this function fixes that.
28403
28404 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28405 row in W's current matrix that is exposed and overlaps other rows.
28406 LAST_OVERLAPPING_ROW is the last such row. */
28407
28408 static void
28409 expose_overlaps (struct window *w,
28410 struct glyph_row *first_overlapping_row,
28411 struct glyph_row *last_overlapping_row,
28412 XRectangle *r)
28413 {
28414 struct glyph_row *row;
28415
28416 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28417 if (row->overlapping_p)
28418 {
28419 eassert (row->enabled_p && !row->mode_line_p);
28420
28421 row->clip = r;
28422 if (row->used[LEFT_MARGIN_AREA])
28423 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28424
28425 if (row->used[TEXT_AREA])
28426 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28427
28428 if (row->used[RIGHT_MARGIN_AREA])
28429 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28430 row->clip = NULL;
28431 }
28432 }
28433
28434
28435 /* Return non-zero if W's cursor intersects rectangle R. */
28436
28437 static int
28438 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28439 {
28440 XRectangle cr, result;
28441 struct glyph *cursor_glyph;
28442 struct glyph_row *row;
28443
28444 if (w->phys_cursor.vpos >= 0
28445 && w->phys_cursor.vpos < w->current_matrix->nrows
28446 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28447 row->enabled_p)
28448 && row->cursor_in_fringe_p)
28449 {
28450 /* Cursor is in the fringe. */
28451 cr.x = window_box_right_offset (w,
28452 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28453 ? RIGHT_MARGIN_AREA
28454 : TEXT_AREA));
28455 cr.y = row->y;
28456 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28457 cr.height = row->height;
28458 return x_intersect_rectangles (&cr, r, &result);
28459 }
28460
28461 cursor_glyph = get_phys_cursor_glyph (w);
28462 if (cursor_glyph)
28463 {
28464 /* r is relative to W's box, but w->phys_cursor.x is relative
28465 to left edge of W's TEXT area. Adjust it. */
28466 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28467 cr.y = w->phys_cursor.y;
28468 cr.width = cursor_glyph->pixel_width;
28469 cr.height = w->phys_cursor_height;
28470 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28471 I assume the effect is the same -- and this is portable. */
28472 return x_intersect_rectangles (&cr, r, &result);
28473 }
28474 /* If we don't understand the format, pretend we're not in the hot-spot. */
28475 return 0;
28476 }
28477
28478
28479 /* EXPORT:
28480 Draw a vertical window border to the right of window W if W doesn't
28481 have vertical scroll bars. */
28482
28483 void
28484 x_draw_vertical_border (struct window *w)
28485 {
28486 struct frame *f = XFRAME (WINDOW_FRAME (w));
28487
28488 /* We could do better, if we knew what type of scroll-bar the adjacent
28489 windows (on either side) have... But we don't :-(
28490 However, I think this works ok. ++KFS 2003-04-25 */
28491
28492 /* Redraw borders between horizontally adjacent windows. Don't
28493 do it for frames with vertical scroll bars because either the
28494 right scroll bar of a window, or the left scroll bar of its
28495 neighbor will suffice as a border. */
28496 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28497 return;
28498
28499 /* Note: It is necessary to redraw both the left and the right
28500 borders, for when only this single window W is being
28501 redisplayed. */
28502 if (!WINDOW_RIGHTMOST_P (w)
28503 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28504 {
28505 int x0, x1, y0, y1;
28506
28507 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28508 y1 -= 1;
28509
28510 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28511 x1 -= 1;
28512
28513 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28514 }
28515 if (!WINDOW_LEFTMOST_P (w)
28516 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28517 {
28518 int x0, x1, y0, y1;
28519
28520 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28521 y1 -= 1;
28522
28523 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28524 x0 -= 1;
28525
28526 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28527 }
28528 }
28529
28530
28531 /* Redraw the part of window W intersection rectangle FR. Pixel
28532 coordinates in FR are frame-relative. Call this function with
28533 input blocked. Value is non-zero if the exposure overwrites
28534 mouse-face. */
28535
28536 static int
28537 expose_window (struct window *w, XRectangle *fr)
28538 {
28539 struct frame *f = XFRAME (w->frame);
28540 XRectangle wr, r;
28541 int mouse_face_overwritten_p = 0;
28542
28543 /* If window is not yet fully initialized, do nothing. This can
28544 happen when toolkit scroll bars are used and a window is split.
28545 Reconfiguring the scroll bar will generate an expose for a newly
28546 created window. */
28547 if (w->current_matrix == NULL)
28548 return 0;
28549
28550 /* When we're currently updating the window, display and current
28551 matrix usually don't agree. Arrange for a thorough display
28552 later. */
28553 if (w == updated_window)
28554 {
28555 SET_FRAME_GARBAGED (f);
28556 return 0;
28557 }
28558
28559 /* Frame-relative pixel rectangle of W. */
28560 wr.x = WINDOW_LEFT_EDGE_X (w);
28561 wr.y = WINDOW_TOP_EDGE_Y (w);
28562 wr.width = WINDOW_TOTAL_WIDTH (w);
28563 wr.height = WINDOW_TOTAL_HEIGHT (w);
28564
28565 if (x_intersect_rectangles (fr, &wr, &r))
28566 {
28567 int yb = window_text_bottom_y (w);
28568 struct glyph_row *row;
28569 int cursor_cleared_p, phys_cursor_on_p;
28570 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28571
28572 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28573 r.x, r.y, r.width, r.height));
28574
28575 /* Convert to window coordinates. */
28576 r.x -= WINDOW_LEFT_EDGE_X (w);
28577 r.y -= WINDOW_TOP_EDGE_Y (w);
28578
28579 /* Turn off the cursor. */
28580 if (!w->pseudo_window_p
28581 && phys_cursor_in_rect_p (w, &r))
28582 {
28583 x_clear_cursor (w);
28584 cursor_cleared_p = 1;
28585 }
28586 else
28587 cursor_cleared_p = 0;
28588
28589 /* If the row containing the cursor extends face to end of line,
28590 then expose_area might overwrite the cursor outside the
28591 rectangle and thus notice_overwritten_cursor might clear
28592 w->phys_cursor_on_p. We remember the original value and
28593 check later if it is changed. */
28594 phys_cursor_on_p = w->phys_cursor_on_p;
28595
28596 /* Update lines intersecting rectangle R. */
28597 first_overlapping_row = last_overlapping_row = NULL;
28598 for (row = w->current_matrix->rows;
28599 row->enabled_p;
28600 ++row)
28601 {
28602 int y0 = row->y;
28603 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28604
28605 if ((y0 >= r.y && y0 < r.y + r.height)
28606 || (y1 > r.y && y1 < r.y + r.height)
28607 || (r.y >= y0 && r.y < y1)
28608 || (r.y + r.height > y0 && r.y + r.height < y1))
28609 {
28610 /* A header line may be overlapping, but there is no need
28611 to fix overlapping areas for them. KFS 2005-02-12 */
28612 if (row->overlapping_p && !row->mode_line_p)
28613 {
28614 if (first_overlapping_row == NULL)
28615 first_overlapping_row = row;
28616 last_overlapping_row = row;
28617 }
28618
28619 row->clip = fr;
28620 if (expose_line (w, row, &r))
28621 mouse_face_overwritten_p = 1;
28622 row->clip = NULL;
28623 }
28624 else if (row->overlapping_p)
28625 {
28626 /* We must redraw a row overlapping the exposed area. */
28627 if (y0 < r.y
28628 ? y0 + row->phys_height > r.y
28629 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28630 {
28631 if (first_overlapping_row == NULL)
28632 first_overlapping_row = row;
28633 last_overlapping_row = row;
28634 }
28635 }
28636
28637 if (y1 >= yb)
28638 break;
28639 }
28640
28641 /* Display the mode line if there is one. */
28642 if (WINDOW_WANTS_MODELINE_P (w)
28643 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28644 row->enabled_p)
28645 && row->y < r.y + r.height)
28646 {
28647 if (expose_line (w, row, &r))
28648 mouse_face_overwritten_p = 1;
28649 }
28650
28651 if (!w->pseudo_window_p)
28652 {
28653 /* Fix the display of overlapping rows. */
28654 if (first_overlapping_row)
28655 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28656 fr);
28657
28658 /* Draw border between windows. */
28659 x_draw_vertical_border (w);
28660
28661 /* Turn the cursor on again. */
28662 if (cursor_cleared_p
28663 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28664 update_window_cursor (w, 1);
28665 }
28666 }
28667
28668 return mouse_face_overwritten_p;
28669 }
28670
28671
28672
28673 /* Redraw (parts) of all windows in the window tree rooted at W that
28674 intersect R. R contains frame pixel coordinates. Value is
28675 non-zero if the exposure overwrites mouse-face. */
28676
28677 static int
28678 expose_window_tree (struct window *w, XRectangle *r)
28679 {
28680 struct frame *f = XFRAME (w->frame);
28681 int mouse_face_overwritten_p = 0;
28682
28683 while (w && !FRAME_GARBAGED_P (f))
28684 {
28685 if (!NILP (w->hchild))
28686 mouse_face_overwritten_p
28687 |= expose_window_tree (XWINDOW (w->hchild), r);
28688 else if (!NILP (w->vchild))
28689 mouse_face_overwritten_p
28690 |= expose_window_tree (XWINDOW (w->vchild), r);
28691 else
28692 mouse_face_overwritten_p |= expose_window (w, r);
28693
28694 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28695 }
28696
28697 return mouse_face_overwritten_p;
28698 }
28699
28700
28701 /* EXPORT:
28702 Redisplay an exposed area of frame F. X and Y are the upper-left
28703 corner of the exposed rectangle. W and H are width and height of
28704 the exposed area. All are pixel values. W or H zero means redraw
28705 the entire frame. */
28706
28707 void
28708 expose_frame (struct frame *f, int x, int y, int w, int h)
28709 {
28710 XRectangle r;
28711 int mouse_face_overwritten_p = 0;
28712
28713 TRACE ((stderr, "expose_frame "));
28714
28715 /* No need to redraw if frame will be redrawn soon. */
28716 if (FRAME_GARBAGED_P (f))
28717 {
28718 TRACE ((stderr, " garbaged\n"));
28719 return;
28720 }
28721
28722 /* If basic faces haven't been realized yet, there is no point in
28723 trying to redraw anything. This can happen when we get an expose
28724 event while Emacs is starting, e.g. by moving another window. */
28725 if (FRAME_FACE_CACHE (f) == NULL
28726 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28727 {
28728 TRACE ((stderr, " no faces\n"));
28729 return;
28730 }
28731
28732 if (w == 0 || h == 0)
28733 {
28734 r.x = r.y = 0;
28735 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28736 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28737 }
28738 else
28739 {
28740 r.x = x;
28741 r.y = y;
28742 r.width = w;
28743 r.height = h;
28744 }
28745
28746 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28747 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28748
28749 if (WINDOWP (f->tool_bar_window))
28750 mouse_face_overwritten_p
28751 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28752
28753 #ifdef HAVE_X_WINDOWS
28754 #ifndef MSDOS
28755 #ifndef USE_X_TOOLKIT
28756 if (WINDOWP (f->menu_bar_window))
28757 mouse_face_overwritten_p
28758 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28759 #endif /* not USE_X_TOOLKIT */
28760 #endif
28761 #endif
28762
28763 /* Some window managers support a focus-follows-mouse style with
28764 delayed raising of frames. Imagine a partially obscured frame,
28765 and moving the mouse into partially obscured mouse-face on that
28766 frame. The visible part of the mouse-face will be highlighted,
28767 then the WM raises the obscured frame. With at least one WM, KDE
28768 2.1, Emacs is not getting any event for the raising of the frame
28769 (even tried with SubstructureRedirectMask), only Expose events.
28770 These expose events will draw text normally, i.e. not
28771 highlighted. Which means we must redo the highlight here.
28772 Subsume it under ``we love X''. --gerd 2001-08-15 */
28773 /* Included in Windows version because Windows most likely does not
28774 do the right thing if any third party tool offers
28775 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28776 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28777 {
28778 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28779 if (f == hlinfo->mouse_face_mouse_frame)
28780 {
28781 int mouse_x = hlinfo->mouse_face_mouse_x;
28782 int mouse_y = hlinfo->mouse_face_mouse_y;
28783 clear_mouse_face (hlinfo);
28784 note_mouse_highlight (f, mouse_x, mouse_y);
28785 }
28786 }
28787 }
28788
28789
28790 /* EXPORT:
28791 Determine the intersection of two rectangles R1 and R2. Return
28792 the intersection in *RESULT. Value is non-zero if RESULT is not
28793 empty. */
28794
28795 int
28796 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28797 {
28798 XRectangle *left, *right;
28799 XRectangle *upper, *lower;
28800 int intersection_p = 0;
28801
28802 /* Rearrange so that R1 is the left-most rectangle. */
28803 if (r1->x < r2->x)
28804 left = r1, right = r2;
28805 else
28806 left = r2, right = r1;
28807
28808 /* X0 of the intersection is right.x0, if this is inside R1,
28809 otherwise there is no intersection. */
28810 if (right->x <= left->x + left->width)
28811 {
28812 result->x = right->x;
28813
28814 /* The right end of the intersection is the minimum of
28815 the right ends of left and right. */
28816 result->width = (min (left->x + left->width, right->x + right->width)
28817 - result->x);
28818
28819 /* Same game for Y. */
28820 if (r1->y < r2->y)
28821 upper = r1, lower = r2;
28822 else
28823 upper = r2, lower = r1;
28824
28825 /* The upper end of the intersection is lower.y0, if this is inside
28826 of upper. Otherwise, there is no intersection. */
28827 if (lower->y <= upper->y + upper->height)
28828 {
28829 result->y = lower->y;
28830
28831 /* The lower end of the intersection is the minimum of the lower
28832 ends of upper and lower. */
28833 result->height = (min (lower->y + lower->height,
28834 upper->y + upper->height)
28835 - result->y);
28836 intersection_p = 1;
28837 }
28838 }
28839
28840 return intersection_p;
28841 }
28842
28843 #endif /* HAVE_WINDOW_SYSTEM */
28844
28845 \f
28846 /***********************************************************************
28847 Initialization
28848 ***********************************************************************/
28849
28850 void
28851 syms_of_xdisp (void)
28852 {
28853 Vwith_echo_area_save_vector = Qnil;
28854 staticpro (&Vwith_echo_area_save_vector);
28855
28856 Vmessage_stack = Qnil;
28857 staticpro (&Vmessage_stack);
28858
28859 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28860 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28861
28862 message_dolog_marker1 = Fmake_marker ();
28863 staticpro (&message_dolog_marker1);
28864 message_dolog_marker2 = Fmake_marker ();
28865 staticpro (&message_dolog_marker2);
28866 message_dolog_marker3 = Fmake_marker ();
28867 staticpro (&message_dolog_marker3);
28868
28869 #ifdef GLYPH_DEBUG
28870 defsubr (&Sdump_frame_glyph_matrix);
28871 defsubr (&Sdump_glyph_matrix);
28872 defsubr (&Sdump_glyph_row);
28873 defsubr (&Sdump_tool_bar_row);
28874 defsubr (&Strace_redisplay);
28875 defsubr (&Strace_to_stderr);
28876 #endif
28877 #ifdef HAVE_WINDOW_SYSTEM
28878 defsubr (&Stool_bar_lines_needed);
28879 defsubr (&Slookup_image_map);
28880 #endif
28881 defsubr (&Sformat_mode_line);
28882 defsubr (&Sinvisible_p);
28883 defsubr (&Scurrent_bidi_paragraph_direction);
28884
28885 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28886 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28887 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28888 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28889 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28890 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28891 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28892 DEFSYM (Qeval, "eval");
28893 DEFSYM (QCdata, ":data");
28894 DEFSYM (Qdisplay, "display");
28895 DEFSYM (Qspace_width, "space-width");
28896 DEFSYM (Qraise, "raise");
28897 DEFSYM (Qslice, "slice");
28898 DEFSYM (Qspace, "space");
28899 DEFSYM (Qmargin, "margin");
28900 DEFSYM (Qpointer, "pointer");
28901 DEFSYM (Qleft_margin, "left-margin");
28902 DEFSYM (Qright_margin, "right-margin");
28903 DEFSYM (Qcenter, "center");
28904 DEFSYM (Qline_height, "line-height");
28905 DEFSYM (QCalign_to, ":align-to");
28906 DEFSYM (QCrelative_width, ":relative-width");
28907 DEFSYM (QCrelative_height, ":relative-height");
28908 DEFSYM (QCeval, ":eval");
28909 DEFSYM (QCpropertize, ":propertize");
28910 DEFSYM (QCfile, ":file");
28911 DEFSYM (Qfontified, "fontified");
28912 DEFSYM (Qfontification_functions, "fontification-functions");
28913 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28914 DEFSYM (Qescape_glyph, "escape-glyph");
28915 DEFSYM (Qnobreak_space, "nobreak-space");
28916 DEFSYM (Qimage, "image");
28917 DEFSYM (Qtext, "text");
28918 DEFSYM (Qboth, "both");
28919 DEFSYM (Qboth_horiz, "both-horiz");
28920 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28921 DEFSYM (QCmap, ":map");
28922 DEFSYM (QCpointer, ":pointer");
28923 DEFSYM (Qrect, "rect");
28924 DEFSYM (Qcircle, "circle");
28925 DEFSYM (Qpoly, "poly");
28926 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28927 DEFSYM (Qgrow_only, "grow-only");
28928 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28929 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28930 DEFSYM (Qposition, "position");
28931 DEFSYM (Qbuffer_position, "buffer-position");
28932 DEFSYM (Qobject, "object");
28933 DEFSYM (Qbar, "bar");
28934 DEFSYM (Qhbar, "hbar");
28935 DEFSYM (Qbox, "box");
28936 DEFSYM (Qhollow, "hollow");
28937 DEFSYM (Qhand, "hand");
28938 DEFSYM (Qarrow, "arrow");
28939 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28940
28941 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28942 Fcons (intern_c_string ("void-variable"), Qnil)),
28943 Qnil);
28944 staticpro (&list_of_error);
28945
28946 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28947 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28948 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28949 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28950
28951 echo_buffer[0] = echo_buffer[1] = Qnil;
28952 staticpro (&echo_buffer[0]);
28953 staticpro (&echo_buffer[1]);
28954
28955 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28956 staticpro (&echo_area_buffer[0]);
28957 staticpro (&echo_area_buffer[1]);
28958
28959 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28960 staticpro (&Vmessages_buffer_name);
28961
28962 mode_line_proptrans_alist = Qnil;
28963 staticpro (&mode_line_proptrans_alist);
28964 mode_line_string_list = Qnil;
28965 staticpro (&mode_line_string_list);
28966 mode_line_string_face = Qnil;
28967 staticpro (&mode_line_string_face);
28968 mode_line_string_face_prop = Qnil;
28969 staticpro (&mode_line_string_face_prop);
28970 Vmode_line_unwind_vector = Qnil;
28971 staticpro (&Vmode_line_unwind_vector);
28972
28973 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28974
28975 help_echo_string = Qnil;
28976 staticpro (&help_echo_string);
28977 help_echo_object = Qnil;
28978 staticpro (&help_echo_object);
28979 help_echo_window = Qnil;
28980 staticpro (&help_echo_window);
28981 previous_help_echo_string = Qnil;
28982 staticpro (&previous_help_echo_string);
28983 help_echo_pos = -1;
28984
28985 DEFSYM (Qright_to_left, "right-to-left");
28986 DEFSYM (Qleft_to_right, "left-to-right");
28987
28988 #ifdef HAVE_WINDOW_SYSTEM
28989 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28990 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28991 For example, if a block cursor is over a tab, it will be drawn as
28992 wide as that tab on the display. */);
28993 x_stretch_cursor_p = 0;
28994 #endif
28995
28996 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28997 doc: /* Non-nil means highlight trailing whitespace.
28998 The face used for trailing whitespace is `trailing-whitespace'. */);
28999 Vshow_trailing_whitespace = Qnil;
29000
29001 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29002 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29003 If the value is t, Emacs highlights non-ASCII chars which have the
29004 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29005 or `escape-glyph' face respectively.
29006
29007 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29008 U+2011 (non-breaking hyphen) are affected.
29009
29010 Any other non-nil value means to display these characters as a escape
29011 glyph followed by an ordinary space or hyphen.
29012
29013 A value of nil means no special handling of these characters. */);
29014 Vnobreak_char_display = Qt;
29015
29016 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29017 doc: /* The pointer shape to show in void text areas.
29018 A value of nil means to show the text pointer. Other options are `arrow',
29019 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29020 Vvoid_text_area_pointer = Qarrow;
29021
29022 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29023 doc: /* Non-nil means don't actually do any redisplay.
29024 This is used for internal purposes. */);
29025 Vinhibit_redisplay = Qnil;
29026
29027 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29028 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29029 Vglobal_mode_string = Qnil;
29030
29031 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29032 doc: /* Marker for where to display an arrow on top of the buffer text.
29033 This must be the beginning of a line in order to work.
29034 See also `overlay-arrow-string'. */);
29035 Voverlay_arrow_position = Qnil;
29036
29037 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29038 doc: /* String to display as an arrow in non-window frames.
29039 See also `overlay-arrow-position'. */);
29040 Voverlay_arrow_string = build_pure_c_string ("=>");
29041
29042 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29043 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29044 The symbols on this list are examined during redisplay to determine
29045 where to display overlay arrows. */);
29046 Voverlay_arrow_variable_list
29047 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
29048
29049 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29050 doc: /* The number of lines to try scrolling a window by when point moves out.
29051 If that fails to bring point back on frame, point is centered instead.
29052 If this is zero, point is always centered after it moves off frame.
29053 If you want scrolling to always be a line at a time, you should set
29054 `scroll-conservatively' to a large value rather than set this to 1. */);
29055
29056 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29057 doc: /* Scroll up to this many lines, to bring point back on screen.
29058 If point moves off-screen, redisplay will scroll by up to
29059 `scroll-conservatively' lines in order to bring point just barely
29060 onto the screen again. If that cannot be done, then redisplay
29061 recenters point as usual.
29062
29063 If the value is greater than 100, redisplay will never recenter point,
29064 but will always scroll just enough text to bring point into view, even
29065 if you move far away.
29066
29067 A value of zero means always recenter point if it moves off screen. */);
29068 scroll_conservatively = 0;
29069
29070 DEFVAR_INT ("scroll-margin", scroll_margin,
29071 doc: /* Number of lines of margin at the top and bottom of a window.
29072 Recenter the window whenever point gets within this many lines
29073 of the top or bottom of the window. */);
29074 scroll_margin = 0;
29075
29076 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29077 doc: /* Pixels per inch value for non-window system displays.
29078 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29079 Vdisplay_pixels_per_inch = make_float (72.0);
29080
29081 #ifdef GLYPH_DEBUG
29082 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29083 #endif
29084
29085 DEFVAR_LISP ("truncate-partial-width-windows",
29086 Vtruncate_partial_width_windows,
29087 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29088 For an integer value, truncate lines in each window narrower than the
29089 full frame width, provided the window width is less than that integer;
29090 otherwise, respect the value of `truncate-lines'.
29091
29092 For any other non-nil value, truncate lines in all windows that do
29093 not span the full frame width.
29094
29095 A value of nil means to respect the value of `truncate-lines'.
29096
29097 If `word-wrap' is enabled, you might want to reduce this. */);
29098 Vtruncate_partial_width_windows = make_number (50);
29099
29100 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29101 doc: /* Maximum buffer size for which line number should be displayed.
29102 If the buffer is bigger than this, the line number does not appear
29103 in the mode line. A value of nil means no limit. */);
29104 Vline_number_display_limit = Qnil;
29105
29106 DEFVAR_INT ("line-number-display-limit-width",
29107 line_number_display_limit_width,
29108 doc: /* Maximum line width (in characters) for line number display.
29109 If the average length of the lines near point is bigger than this, then the
29110 line number may be omitted from the mode line. */);
29111 line_number_display_limit_width = 200;
29112
29113 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29114 doc: /* Non-nil means highlight region even in nonselected windows. */);
29115 highlight_nonselected_windows = 0;
29116
29117 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29118 doc: /* Non-nil if more than one frame is visible on this display.
29119 Minibuffer-only frames don't count, but iconified frames do.
29120 This variable is not guaranteed to be accurate except while processing
29121 `frame-title-format' and `icon-title-format'. */);
29122
29123 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29124 doc: /* Template for displaying the title bar of visible frames.
29125 \(Assuming the window manager supports this feature.)
29126
29127 This variable has the same structure as `mode-line-format', except that
29128 the %c and %l constructs are ignored. It is used only on frames for
29129 which no explicit name has been set \(see `modify-frame-parameters'). */);
29130
29131 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29132 doc: /* Template for displaying the title bar of an iconified frame.
29133 \(Assuming the window manager supports this feature.)
29134 This variable has the same structure as `mode-line-format' (which see),
29135 and is used only on frames for which no explicit name has been set
29136 \(see `modify-frame-parameters'). */);
29137 Vicon_title_format
29138 = Vframe_title_format
29139 = listn (CONSTYPE_PURE, 3,
29140 intern_c_string ("multiple-frames"),
29141 build_pure_c_string ("%b"),
29142 listn (CONSTYPE_PURE, 4,
29143 empty_unibyte_string,
29144 intern_c_string ("invocation-name"),
29145 build_pure_c_string ("@"),
29146 intern_c_string ("system-name")));
29147
29148 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29149 doc: /* Maximum number of lines to keep in the message log buffer.
29150 If nil, disable message logging. If t, log messages but don't truncate
29151 the buffer when it becomes large. */);
29152 Vmessage_log_max = make_number (1000);
29153
29154 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29155 doc: /* Functions called before redisplay, if window sizes have changed.
29156 The value should be a list of functions that take one argument.
29157 Just before redisplay, for each frame, if any of its windows have changed
29158 size since the last redisplay, or have been split or deleted,
29159 all the functions in the list are called, with the frame as argument. */);
29160 Vwindow_size_change_functions = Qnil;
29161
29162 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29163 doc: /* List of functions to call before redisplaying a window with scrolling.
29164 Each function is called with two arguments, the window and its new
29165 display-start position. Note that these functions are also called by
29166 `set-window-buffer'. Also note that the value of `window-end' is not
29167 valid when these functions are called.
29168
29169 Warning: Do not use this feature to alter the way the window
29170 is scrolled. It is not designed for that, and such use probably won't
29171 work. */);
29172 Vwindow_scroll_functions = Qnil;
29173
29174 DEFVAR_LISP ("window-text-change-functions",
29175 Vwindow_text_change_functions,
29176 doc: /* Functions to call in redisplay when text in the window might change. */);
29177 Vwindow_text_change_functions = Qnil;
29178
29179 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29180 doc: /* Functions called when redisplay of a window reaches the end trigger.
29181 Each function is called with two arguments, the window and the end trigger value.
29182 See `set-window-redisplay-end-trigger'. */);
29183 Vredisplay_end_trigger_functions = Qnil;
29184
29185 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29186 doc: /* Non-nil means autoselect window with mouse pointer.
29187 If nil, do not autoselect windows.
29188 A positive number means delay autoselection by that many seconds: a
29189 window is autoselected only after the mouse has remained in that
29190 window for the duration of the delay.
29191 A negative number has a similar effect, but causes windows to be
29192 autoselected only after the mouse has stopped moving. \(Because of
29193 the way Emacs compares mouse events, you will occasionally wait twice
29194 that time before the window gets selected.\)
29195 Any other value means to autoselect window instantaneously when the
29196 mouse pointer enters it.
29197
29198 Autoselection selects the minibuffer only if it is active, and never
29199 unselects the minibuffer if it is active.
29200
29201 When customizing this variable make sure that the actual value of
29202 `focus-follows-mouse' matches the behavior of your window manager. */);
29203 Vmouse_autoselect_window = Qnil;
29204
29205 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29206 doc: /* Non-nil means automatically resize tool-bars.
29207 This dynamically changes the tool-bar's height to the minimum height
29208 that is needed to make all tool-bar items visible.
29209 If value is `grow-only', the tool-bar's height is only increased
29210 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29211 Vauto_resize_tool_bars = Qt;
29212
29213 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29214 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29215 auto_raise_tool_bar_buttons_p = 1;
29216
29217 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29218 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29219 make_cursor_line_fully_visible_p = 1;
29220
29221 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29222 doc: /* Border below tool-bar in pixels.
29223 If an integer, use it as the height of the border.
29224 If it is one of `internal-border-width' or `border-width', use the
29225 value of the corresponding frame parameter.
29226 Otherwise, no border is added below the tool-bar. */);
29227 Vtool_bar_border = Qinternal_border_width;
29228
29229 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29230 doc: /* Margin around tool-bar buttons in pixels.
29231 If an integer, use that for both horizontal and vertical margins.
29232 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29233 HORZ specifying the horizontal margin, and VERT specifying the
29234 vertical margin. */);
29235 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29236
29237 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29238 doc: /* Relief thickness of tool-bar buttons. */);
29239 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29240
29241 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29242 doc: /* Tool bar style to use.
29243 It can be one of
29244 image - show images only
29245 text - show text only
29246 both - show both, text below image
29247 both-horiz - show text to the right of the image
29248 text-image-horiz - show text to the left of the image
29249 any other - use system default or image if no system default.
29250
29251 This variable only affects the GTK+ toolkit version of Emacs. */);
29252 Vtool_bar_style = Qnil;
29253
29254 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29255 doc: /* Maximum number of characters a label can have to be shown.
29256 The tool bar style must also show labels for this to have any effect, see
29257 `tool-bar-style'. */);
29258 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29259
29260 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29261 doc: /* List of functions to call to fontify regions of text.
29262 Each function is called with one argument POS. Functions must
29263 fontify a region starting at POS in the current buffer, and give
29264 fontified regions the property `fontified'. */);
29265 Vfontification_functions = Qnil;
29266 Fmake_variable_buffer_local (Qfontification_functions);
29267
29268 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29269 unibyte_display_via_language_environment,
29270 doc: /* Non-nil means display unibyte text according to language environment.
29271 Specifically, this means that raw bytes in the range 160-255 decimal
29272 are displayed by converting them to the equivalent multibyte characters
29273 according to the current language environment. As a result, they are
29274 displayed according to the current fontset.
29275
29276 Note that this variable affects only how these bytes are displayed,
29277 but does not change the fact they are interpreted as raw bytes. */);
29278 unibyte_display_via_language_environment = 0;
29279
29280 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29281 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29282 If a float, it specifies a fraction of the mini-window frame's height.
29283 If an integer, it specifies a number of lines. */);
29284 Vmax_mini_window_height = make_float (0.25);
29285
29286 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29287 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29288 A value of nil means don't automatically resize mini-windows.
29289 A value of t means resize them to fit the text displayed in them.
29290 A value of `grow-only', the default, means let mini-windows grow only;
29291 they return to their normal size when the minibuffer is closed, or the
29292 echo area becomes empty. */);
29293 Vresize_mini_windows = Qgrow_only;
29294
29295 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29296 doc: /* Alist specifying how to blink the cursor off.
29297 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29298 `cursor-type' frame-parameter or variable equals ON-STATE,
29299 comparing using `equal', Emacs uses OFF-STATE to specify
29300 how to blink it off. ON-STATE and OFF-STATE are values for
29301 the `cursor-type' frame parameter.
29302
29303 If a frame's ON-STATE has no entry in this list,
29304 the frame's other specifications determine how to blink the cursor off. */);
29305 Vblink_cursor_alist = Qnil;
29306
29307 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29308 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29309 If non-nil, windows are automatically scrolled horizontally to make
29310 point visible. */);
29311 automatic_hscrolling_p = 1;
29312 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29313
29314 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29315 doc: /* How many columns away from the window edge point is allowed to get
29316 before automatic hscrolling will horizontally scroll the window. */);
29317 hscroll_margin = 5;
29318
29319 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29320 doc: /* How many columns to scroll the window when point gets too close to the edge.
29321 When point is less than `hscroll-margin' columns from the window
29322 edge, automatic hscrolling will scroll the window by the amount of columns
29323 determined by this variable. If its value is a positive integer, scroll that
29324 many columns. If it's a positive floating-point number, it specifies the
29325 fraction of the window's width to scroll. If it's nil or zero, point will be
29326 centered horizontally after the scroll. Any other value, including negative
29327 numbers, are treated as if the value were zero.
29328
29329 Automatic hscrolling always moves point outside the scroll margin, so if
29330 point was more than scroll step columns inside the margin, the window will
29331 scroll more than the value given by the scroll step.
29332
29333 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29334 and `scroll-right' overrides this variable's effect. */);
29335 Vhscroll_step = make_number (0);
29336
29337 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29338 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29339 Bind this around calls to `message' to let it take effect. */);
29340 message_truncate_lines = 0;
29341
29342 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29343 doc: /* Normal hook run to update the menu bar definitions.
29344 Redisplay runs this hook before it redisplays the menu bar.
29345 This is used to update submenus such as Buffers,
29346 whose contents depend on various data. */);
29347 Vmenu_bar_update_hook = Qnil;
29348
29349 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29350 doc: /* Frame for which we are updating a menu.
29351 The enable predicate for a menu binding should check this variable. */);
29352 Vmenu_updating_frame = Qnil;
29353
29354 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29355 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29356 inhibit_menubar_update = 0;
29357
29358 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29359 doc: /* Prefix prepended to all continuation lines at display time.
29360 The value may be a string, an image, or a stretch-glyph; it is
29361 interpreted in the same way as the value of a `display' text property.
29362
29363 This variable is overridden by any `wrap-prefix' text or overlay
29364 property.
29365
29366 To add a prefix to non-continuation lines, use `line-prefix'. */);
29367 Vwrap_prefix = Qnil;
29368 DEFSYM (Qwrap_prefix, "wrap-prefix");
29369 Fmake_variable_buffer_local (Qwrap_prefix);
29370
29371 DEFVAR_LISP ("line-prefix", Vline_prefix,
29372 doc: /* Prefix prepended to all non-continuation lines at display time.
29373 The value may be a string, an image, or a stretch-glyph; it is
29374 interpreted in the same way as the value of a `display' text property.
29375
29376 This variable is overridden by any `line-prefix' text or overlay
29377 property.
29378
29379 To add a prefix to continuation lines, use `wrap-prefix'. */);
29380 Vline_prefix = Qnil;
29381 DEFSYM (Qline_prefix, "line-prefix");
29382 Fmake_variable_buffer_local (Qline_prefix);
29383
29384 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29385 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29386 inhibit_eval_during_redisplay = 0;
29387
29388 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29389 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29390 inhibit_free_realized_faces = 0;
29391
29392 #ifdef GLYPH_DEBUG
29393 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29394 doc: /* Inhibit try_window_id display optimization. */);
29395 inhibit_try_window_id = 0;
29396
29397 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29398 doc: /* Inhibit try_window_reusing display optimization. */);
29399 inhibit_try_window_reusing = 0;
29400
29401 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29402 doc: /* Inhibit try_cursor_movement display optimization. */);
29403 inhibit_try_cursor_movement = 0;
29404 #endif /* GLYPH_DEBUG */
29405
29406 DEFVAR_INT ("overline-margin", overline_margin,
29407 doc: /* Space between overline and text, in pixels.
29408 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29409 margin to the character height. */);
29410 overline_margin = 2;
29411
29412 DEFVAR_INT ("underline-minimum-offset",
29413 underline_minimum_offset,
29414 doc: /* Minimum distance between baseline and underline.
29415 This can improve legibility of underlined text at small font sizes,
29416 particularly when using variable `x-use-underline-position-properties'
29417 with fonts that specify an UNDERLINE_POSITION relatively close to the
29418 baseline. The default value is 1. */);
29419 underline_minimum_offset = 1;
29420
29421 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29422 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29423 This feature only works when on a window system that can change
29424 cursor shapes. */);
29425 display_hourglass_p = 1;
29426
29427 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29428 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29429 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29430
29431 hourglass_atimer = NULL;
29432 hourglass_shown_p = 0;
29433
29434 DEFSYM (Qglyphless_char, "glyphless-char");
29435 DEFSYM (Qhex_code, "hex-code");
29436 DEFSYM (Qempty_box, "empty-box");
29437 DEFSYM (Qthin_space, "thin-space");
29438 DEFSYM (Qzero_width, "zero-width");
29439
29440 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29441 /* Intern this now in case it isn't already done.
29442 Setting this variable twice is harmless.
29443 But don't staticpro it here--that is done in alloc.c. */
29444 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29445 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29446
29447 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29448 doc: /* Char-table defining glyphless characters.
29449 Each element, if non-nil, should be one of the following:
29450 an ASCII acronym string: display this string in a box
29451 `hex-code': display the hexadecimal code of a character in a box
29452 `empty-box': display as an empty box
29453 `thin-space': display as 1-pixel width space
29454 `zero-width': don't display
29455 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29456 display method for graphical terminals and text terminals respectively.
29457 GRAPHICAL and TEXT should each have one of the values listed above.
29458
29459 The char-table has one extra slot to control the display of a character for
29460 which no font is found. This slot only takes effect on graphical terminals.
29461 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29462 `thin-space'. The default is `empty-box'. */);
29463 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29464 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29465 Qempty_box);
29466
29467 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29468 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29469 Vdebug_on_message = Qnil;
29470 }
29471
29472
29473 /* Initialize this module when Emacs starts. */
29474
29475 void
29476 init_xdisp (void)
29477 {
29478 current_header_line_height = current_mode_line_height = -1;
29479
29480 CHARPOS (this_line_start_pos) = 0;
29481
29482 if (!noninteractive)
29483 {
29484 struct window *m = XWINDOW (minibuf_window);
29485 Lisp_Object frame = m->frame;
29486 struct frame *f = XFRAME (frame);
29487 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29488 struct window *r = XWINDOW (root);
29489 int i;
29490
29491 echo_area_window = minibuf_window;
29492
29493 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29494 wset_total_lines
29495 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29496 wset_total_cols (r, make_number (FRAME_COLS (f)));
29497 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29498 wset_total_lines (m, make_number (1));
29499 wset_total_cols (m, make_number (FRAME_COLS (f)));
29500
29501 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29502 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29503 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29504
29505 /* The default ellipsis glyphs `...'. */
29506 for (i = 0; i < 3; ++i)
29507 default_invis_vector[i] = make_number ('.');
29508 }
29509
29510 {
29511 /* Allocate the buffer for frame titles.
29512 Also used for `format-mode-line'. */
29513 int size = 100;
29514 mode_line_noprop_buf = xmalloc (size);
29515 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29516 mode_line_noprop_ptr = mode_line_noprop_buf;
29517 mode_line_target = MODE_LINE_DISPLAY;
29518 }
29519
29520 help_echo_showing_p = 0;
29521 }
29522
29523 /* Platform-independent portion of hourglass implementation. */
29524
29525 /* Cancel a currently active hourglass timer, and start a new one. */
29526 void
29527 start_hourglass (void)
29528 {
29529 #if defined (HAVE_WINDOW_SYSTEM)
29530 EMACS_TIME delay;
29531
29532 cancel_hourglass ();
29533
29534 if (INTEGERP (Vhourglass_delay)
29535 && XINT (Vhourglass_delay) > 0)
29536 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29537 TYPE_MAXIMUM (time_t)),
29538 0);
29539 else if (FLOATP (Vhourglass_delay)
29540 && XFLOAT_DATA (Vhourglass_delay) > 0)
29541 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29542 else
29543 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29544
29545 #ifdef HAVE_NTGUI
29546 {
29547 extern void w32_note_current_window (void);
29548 w32_note_current_window ();
29549 }
29550 #endif /* HAVE_NTGUI */
29551
29552 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29553 show_hourglass, NULL);
29554 #endif
29555 }
29556
29557
29558 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29559 shown. */
29560 void
29561 cancel_hourglass (void)
29562 {
29563 #if defined (HAVE_WINDOW_SYSTEM)
29564 if (hourglass_atimer)
29565 {
29566 cancel_atimer (hourglass_atimer);
29567 hourglass_atimer = NULL;
29568 }
29569
29570 if (hourglass_shown_p)
29571 hide_hourglass ();
29572 #endif
29573 }