Merge from emacs-24; up to 2012-04-21T14:12:27Z!sdl.web@gmail.com
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
389
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401
402 /* Name of the face used to highlight trailing whitespace. */
403
404 static Lisp_Object Qtrailing_whitespace;
405
406 /* Name and number of the face used to highlight escape glyphs. */
407
408 static Lisp_Object Qescape_glyph;
409
410 /* Name and number of the face used to highlight non-breaking spaces. */
411
412 static Lisp_Object Qnobreak_space;
413
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
416
417 Lisp_Object Qimage;
418
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
423
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
429
430 int noninteractive_need_newline;
431
432 /* Non-zero means print newline to message log before next message. */
433
434 static int message_log_need_newline;
435
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
442 \f
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
447
448 static struct text_pos this_line_start_pos;
449
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
452
453 static struct text_pos this_line_end_pos;
454
455 /* The vertical positions and the height of this line. */
456
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
460
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
463
464 static int this_line_start_x;
465
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
469
470 static struct text_pos this_line_min_pos;
471
472 /* Buffer that this_line_.* variables are referring to. */
473
474 static struct buffer *this_line_buffer;
475
476
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
481
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
486
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488
489 Lisp_Object Qmenu_bar_update_hook;
490
491 /* Nonzero if an overlay arrow has been displayed in this window. */
492
493 static int overlay_arrow_seen;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
508
509 Lisp_Object echo_area_window;
510
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
515
516 static Lisp_Object Vmessage_stack;
517
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
520
521 static int message_enable_multibyte;
522
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
524
525 int update_mode_lines;
526
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
529
530 int windows_or_buffers_changed;
531
532 /* Nonzero means a frame's cursor type has been changed. */
533
534 int cursor_type_changed;
535
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
538
539 static int line_number_displayed;
540
541 /* The name of the *Messages* buffer, a string. */
542
543 static Lisp_Object Vmessages_buffer_name;
544
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
547
548 Lisp_Object echo_area_buffer[2];
549
550 /* The buffers referenced from echo_area_buffer. */
551
552 static Lisp_Object echo_buffer[2];
553
554 /* A vector saved used in with_area_buffer to reduce consing. */
555
556 static Lisp_Object Vwith_echo_area_save_vector;
557
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
560
561 static int display_last_displayed_message_p;
562
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
565
566 static int message_buf_print;
567
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
569
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
572
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
575
576 static int message_cleared_p;
577
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
580
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
584
585 /* Ascent and height of the last line processed by move_it_to. */
586
587 static int last_max_ascent, last_height;
588
589 /* Non-zero if there's a help-echo in the echo area. */
590
591 int help_echo_showing_p;
592
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
596
597 int current_mode_line_height, current_header_line_height;
598
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
604
605 #define TEXT_PROP_DISTANCE_LIMIT 100
606
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
621
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
629
630 #if GLYPH_DEBUG
631
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG != 0. */
634
635 int trace_redisplay_p;
636
637 #endif /* GLYPH_DEBUG */
638
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
642
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
647
648 static Lisp_Object Qauto_hscroll_mode;
649
650 /* Buffer being redisplayed -- for redisplay_window_error. */
651
652 static struct buffer *displayed_buffer;
653
654 /* Value returned from text property handlers (see below). */
655
656 enum prop_handled
657 {
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
662 };
663
664 /* A description of text properties that redisplay is interested
665 in. */
666
667 struct props
668 {
669 /* The name of the property. */
670 Lisp_Object *name;
671
672 /* A unique index for the property. */
673 enum prop_idx idx;
674
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
678 };
679
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
686
687 /* Properties handled by iterators. */
688
689 static struct props it_props[] =
690 {
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
699 };
700
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
703
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
705
706 /* Enumeration returned by some move_it_.* functions internally. */
707
708 enum move_it_result
709 {
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
712
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
715
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
718
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
722
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
726
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
729 };
730
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
735
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
738
739 /* Similarly for the image cache. */
740
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
744
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
748
749 /* Non-zero while redisplay_internal is in progress. */
750
751 int redisplaying_p;
752
753 static Lisp_Object Qinhibit_free_realized_faces;
754
755 /* If a string, XTread_socket generates an event to display that string.
756 (The display is done in read_char.) */
757
758 Lisp_Object help_echo_string;
759 Lisp_Object help_echo_window;
760 Lisp_Object help_echo_object;
761 EMACS_INT help_echo_pos;
762
763 /* Temporary variable for XTread_socket. */
764
765 Lisp_Object previous_help_echo_string;
766
767 /* Platform-independent portion of hourglass implementation. */
768
769 /* Non-zero means an hourglass cursor is currently shown. */
770 int hourglass_shown_p;
771
772 /* If non-null, an asynchronous timer that, when it expires, displays
773 an hourglass cursor on all frames. */
774 struct atimer *hourglass_atimer;
775
776 /* Name of the face used to display glyphless characters. */
777 Lisp_Object Qglyphless_char;
778
779 /* Symbol for the purpose of Vglyphless_char_display. */
780 static Lisp_Object Qglyphless_char_display;
781
782 /* Method symbols for Vglyphless_char_display. */
783 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
784
785 /* Default pixel width of `thin-space' display method. */
786 #define THIN_SPACE_WIDTH 1
787
788 /* Default number of seconds to wait before displaying an hourglass
789 cursor. */
790 #define DEFAULT_HOURGLASS_DELAY 1
791
792 \f
793 /* Function prototypes. */
794
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, int);
797 static void mark_window_display_accurate_1 (struct window *, int);
798 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
799 static int display_prop_string_p (Lisp_Object, Lisp_Object);
800 static int cursor_row_p (struct glyph_row *);
801 static int redisplay_mode_lines (Lisp_Object, int);
802 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
803
804 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
805
806 static void handle_line_prefix (struct it *);
807
808 static void pint2str (char *, int, EMACS_INT);
809 static void pint2hrstr (char *, int, EMACS_INT);
810 static struct text_pos run_window_scroll_functions (Lisp_Object,
811 struct text_pos);
812 static void reconsider_clip_changes (struct window *, struct buffer *);
813 static int text_outside_line_unchanged_p (struct window *,
814 EMACS_INT, EMACS_INT);
815 static void store_mode_line_noprop_char (char);
816 static int store_mode_line_noprop (const char *, int, int);
817 static void handle_stop (struct it *);
818 static void handle_stop_backwards (struct it *, EMACS_INT);
819 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
820 static void ensure_echo_area_buffers (void);
821 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
822 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
823 static int with_echo_area_buffer (struct window *, int,
824 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
825 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
826 static void clear_garbaged_frames (void);
827 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
828 static void pop_message (void);
829 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
830 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
831 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
832 static int display_echo_area (struct window *);
833 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
834 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
835 static Lisp_Object unwind_redisplay (Lisp_Object);
836 static int string_char_and_length (const unsigned char *, int *);
837 static struct text_pos display_prop_end (struct it *, Lisp_Object,
838 struct text_pos);
839 static int compute_window_start_on_continuation_line (struct window *);
840 static Lisp_Object safe_eval_handler (Lisp_Object);
841 static void insert_left_trunc_glyphs (struct it *);
842 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
843 Lisp_Object);
844 static void extend_face_to_end_of_line (struct it *);
845 static int append_space_for_newline (struct it *, int);
846 static int cursor_row_fully_visible_p (struct window *, int, int);
847 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
848 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
849 static int trailing_whitespace_p (EMACS_INT);
850 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
851 static void push_it (struct it *, struct text_pos *);
852 static void pop_it (struct it *);
853 static void sync_frame_with_window_matrix_rows (struct window *);
854 static void select_frame_for_redisplay (Lisp_Object);
855 static void redisplay_internal (void);
856 static int echo_area_display (int);
857 static void redisplay_windows (Lisp_Object);
858 static void redisplay_window (Lisp_Object, int);
859 static Lisp_Object redisplay_window_error (Lisp_Object);
860 static Lisp_Object redisplay_window_0 (Lisp_Object);
861 static Lisp_Object redisplay_window_1 (Lisp_Object);
862 static int set_cursor_from_row (struct window *, struct glyph_row *,
863 struct glyph_matrix *, EMACS_INT, EMACS_INT,
864 int, int);
865 static int update_menu_bar (struct frame *, int, int);
866 static int try_window_reusing_current_matrix (struct window *);
867 static int try_window_id (struct window *);
868 static int display_line (struct it *);
869 static int display_mode_lines (struct window *);
870 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
871 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
872 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
873 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
874 static void display_menu_bar (struct window *);
875 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
876 EMACS_INT *);
877 static int display_string (const char *, Lisp_Object, Lisp_Object,
878 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
879 static void compute_line_metrics (struct it *);
880 static void run_redisplay_end_trigger_hook (struct it *);
881 static int get_overlay_strings (struct it *, EMACS_INT);
882 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
883 static void next_overlay_string (struct it *);
884 static void reseat (struct it *, struct text_pos, int);
885 static void reseat_1 (struct it *, struct text_pos, int);
886 static void back_to_previous_visible_line_start (struct it *);
887 void reseat_at_previous_visible_line_start (struct it *);
888 static void reseat_at_next_visible_line_start (struct it *, int);
889 static int next_element_from_ellipsis (struct it *);
890 static int next_element_from_display_vector (struct it *);
891 static int next_element_from_string (struct it *);
892 static int next_element_from_c_string (struct it *);
893 static int next_element_from_buffer (struct it *);
894 static int next_element_from_composition (struct it *);
895 static int next_element_from_image (struct it *);
896 static int next_element_from_stretch (struct it *);
897 static void load_overlay_strings (struct it *, EMACS_INT);
898 static int init_from_display_pos (struct it *, struct window *,
899 struct display_pos *);
900 static void reseat_to_string (struct it *, const char *,
901 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
902 static int get_next_display_element (struct it *);
903 static enum move_it_result
904 move_it_in_display_line_to (struct it *, EMACS_INT, int,
905 enum move_operation_enum);
906 void move_it_vertically_backward (struct it *, int);
907 static void init_to_row_start (struct it *, struct window *,
908 struct glyph_row *);
909 static int init_to_row_end (struct it *, struct window *,
910 struct glyph_row *);
911 static void back_to_previous_line_start (struct it *);
912 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
913 static struct text_pos string_pos_nchars_ahead (struct text_pos,
914 Lisp_Object, EMACS_INT);
915 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
916 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
917 static EMACS_INT number_of_chars (const char *, int);
918 static void compute_stop_pos (struct it *);
919 static void compute_string_pos (struct text_pos *, struct text_pos,
920 Lisp_Object);
921 static int face_before_or_after_it_pos (struct it *, int);
922 static EMACS_INT next_overlay_change (EMACS_INT);
923 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
924 Lisp_Object, struct text_pos *, EMACS_INT, int);
925 static int handle_single_display_spec (struct it *, Lisp_Object,
926 Lisp_Object, Lisp_Object,
927 struct text_pos *, EMACS_INT, int, int);
928 static int underlying_face_id (struct it *);
929 static int in_ellipses_for_invisible_text_p (struct display_pos *,
930 struct window *);
931
932 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
933 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
934
935 #ifdef HAVE_WINDOW_SYSTEM
936
937 static void x_consider_frame_title (Lisp_Object);
938 static int tool_bar_lines_needed (struct frame *, int *);
939 static void update_tool_bar (struct frame *, int);
940 static void build_desired_tool_bar_string (struct frame *f);
941 static int redisplay_tool_bar (struct frame *);
942 static void display_tool_bar_line (struct it *, int);
943 static void notice_overwritten_cursor (struct window *,
944 enum glyph_row_area,
945 int, int, int, int);
946 static void append_stretch_glyph (struct it *, Lisp_Object,
947 int, int, int);
948
949
950 #endif /* HAVE_WINDOW_SYSTEM */
951
952 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
953 static int coords_in_mouse_face_p (struct window *, int, int);
954
955
956 \f
957 /***********************************************************************
958 Window display dimensions
959 ***********************************************************************/
960
961 /* Return the bottom boundary y-position for text lines in window W.
962 This is the first y position at which a line cannot start.
963 It is relative to the top of the window.
964
965 This is the height of W minus the height of a mode line, if any. */
966
967 int
968 window_text_bottom_y (struct window *w)
969 {
970 int height = WINDOW_TOTAL_HEIGHT (w);
971
972 if (WINDOW_WANTS_MODELINE_P (w))
973 height -= CURRENT_MODE_LINE_HEIGHT (w);
974 return height;
975 }
976
977 /* Return the pixel width of display area AREA of window W. AREA < 0
978 means return the total width of W, not including fringes to
979 the left and right of the window. */
980
981 int
982 window_box_width (struct window *w, int area)
983 {
984 int cols = XFASTINT (w->total_cols);
985 int pixels = 0;
986
987 if (!w->pseudo_window_p)
988 {
989 cols -= WINDOW_SCROLL_BAR_COLS (w);
990
991 if (area == TEXT_AREA)
992 {
993 if (INTEGERP (w->left_margin_cols))
994 cols -= XFASTINT (w->left_margin_cols);
995 if (INTEGERP (w->right_margin_cols))
996 cols -= XFASTINT (w->right_margin_cols);
997 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
998 }
999 else if (area == LEFT_MARGIN_AREA)
1000 {
1001 cols = (INTEGERP (w->left_margin_cols)
1002 ? XFASTINT (w->left_margin_cols) : 0);
1003 pixels = 0;
1004 }
1005 else if (area == RIGHT_MARGIN_AREA)
1006 {
1007 cols = (INTEGERP (w->right_margin_cols)
1008 ? XFASTINT (w->right_margin_cols) : 0);
1009 pixels = 0;
1010 }
1011 }
1012
1013 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1014 }
1015
1016
1017 /* Return the pixel height of the display area of window W, not
1018 including mode lines of W, if any. */
1019
1020 int
1021 window_box_height (struct window *w)
1022 {
1023 struct frame *f = XFRAME (w->frame);
1024 int height = WINDOW_TOTAL_HEIGHT (w);
1025
1026 xassert (height >= 0);
1027
1028 /* Note: the code below that determines the mode-line/header-line
1029 height is essentially the same as that contained in the macro
1030 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1031 the appropriate glyph row has its `mode_line_p' flag set,
1032 and if it doesn't, uses estimate_mode_line_height instead. */
1033
1034 if (WINDOW_WANTS_MODELINE_P (w))
1035 {
1036 struct glyph_row *ml_row
1037 = (w->current_matrix && w->current_matrix->rows
1038 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1039 : 0);
1040 if (ml_row && ml_row->mode_line_p)
1041 height -= ml_row->height;
1042 else
1043 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1044 }
1045
1046 if (WINDOW_WANTS_HEADER_LINE_P (w))
1047 {
1048 struct glyph_row *hl_row
1049 = (w->current_matrix && w->current_matrix->rows
1050 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1051 : 0);
1052 if (hl_row && hl_row->mode_line_p)
1053 height -= hl_row->height;
1054 else
1055 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1056 }
1057
1058 /* With a very small font and a mode-line that's taller than
1059 default, we might end up with a negative height. */
1060 return max (0, height);
1061 }
1062
1063 /* Return the window-relative coordinate of the left edge of display
1064 area AREA of window W. AREA < 0 means return the left edge of the
1065 whole window, to the right of the left fringe of W. */
1066
1067 int
1068 window_box_left_offset (struct window *w, int area)
1069 {
1070 int x;
1071
1072 if (w->pseudo_window_p)
1073 return 0;
1074
1075 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1076
1077 if (area == TEXT_AREA)
1078 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1079 + window_box_width (w, LEFT_MARGIN_AREA));
1080 else if (area == RIGHT_MARGIN_AREA)
1081 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1082 + window_box_width (w, LEFT_MARGIN_AREA)
1083 + window_box_width (w, TEXT_AREA)
1084 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1085 ? 0
1086 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1087 else if (area == LEFT_MARGIN_AREA
1088 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1089 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1090
1091 return x;
1092 }
1093
1094
1095 /* Return the window-relative coordinate of the right edge of display
1096 area AREA of window W. AREA < 0 means return the right edge of the
1097 whole window, to the left of the right fringe of W. */
1098
1099 int
1100 window_box_right_offset (struct window *w, int area)
1101 {
1102 return window_box_left_offset (w, area) + window_box_width (w, area);
1103 }
1104
1105 /* Return the frame-relative coordinate of the left edge of display
1106 area AREA of window W. AREA < 0 means return the left edge of the
1107 whole window, to the right of the left fringe of W. */
1108
1109 int
1110 window_box_left (struct window *w, int area)
1111 {
1112 struct frame *f = XFRAME (w->frame);
1113 int x;
1114
1115 if (w->pseudo_window_p)
1116 return FRAME_INTERNAL_BORDER_WIDTH (f);
1117
1118 x = (WINDOW_LEFT_EDGE_X (w)
1119 + window_box_left_offset (w, area));
1120
1121 return x;
1122 }
1123
1124
1125 /* Return the frame-relative coordinate of the right edge of display
1126 area AREA of window W. AREA < 0 means return the right edge of the
1127 whole window, to the left of the right fringe of W. */
1128
1129 int
1130 window_box_right (struct window *w, int area)
1131 {
1132 return window_box_left (w, area) + window_box_width (w, area);
1133 }
1134
1135 /* Get the bounding box of the display area AREA of window W, without
1136 mode lines, in frame-relative coordinates. AREA < 0 means the
1137 whole window, not including the left and right fringes of
1138 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1139 coordinates of the upper-left corner of the box. Return in
1140 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1141
1142 void
1143 window_box (struct window *w, int area, int *box_x, int *box_y,
1144 int *box_width, int *box_height)
1145 {
1146 if (box_width)
1147 *box_width = window_box_width (w, area);
1148 if (box_height)
1149 *box_height = window_box_height (w);
1150 if (box_x)
1151 *box_x = window_box_left (w, area);
1152 if (box_y)
1153 {
1154 *box_y = WINDOW_TOP_EDGE_Y (w);
1155 if (WINDOW_WANTS_HEADER_LINE_P (w))
1156 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1157 }
1158 }
1159
1160
1161 /* Get the bounding box of the display area AREA of window W, without
1162 mode lines. AREA < 0 means the whole window, not including the
1163 left and right fringe of the window. Return in *TOP_LEFT_X
1164 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1165 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1166 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1167 box. */
1168
1169 static inline void
1170 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1171 int *bottom_right_x, int *bottom_right_y)
1172 {
1173 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1174 bottom_right_y);
1175 *bottom_right_x += *top_left_x;
1176 *bottom_right_y += *top_left_y;
1177 }
1178
1179
1180 \f
1181 /***********************************************************************
1182 Utilities
1183 ***********************************************************************/
1184
1185 /* Return the bottom y-position of the line the iterator IT is in.
1186 This can modify IT's settings. */
1187
1188 int
1189 line_bottom_y (struct it *it)
1190 {
1191 int line_height = it->max_ascent + it->max_descent;
1192 int line_top_y = it->current_y;
1193
1194 if (line_height == 0)
1195 {
1196 if (last_height)
1197 line_height = last_height;
1198 else if (IT_CHARPOS (*it) < ZV)
1199 {
1200 move_it_by_lines (it, 1);
1201 line_height = (it->max_ascent || it->max_descent
1202 ? it->max_ascent + it->max_descent
1203 : last_height);
1204 }
1205 else
1206 {
1207 struct glyph_row *row = it->glyph_row;
1208
1209 /* Use the default character height. */
1210 it->glyph_row = NULL;
1211 it->what = IT_CHARACTER;
1212 it->c = ' ';
1213 it->len = 1;
1214 PRODUCE_GLYPHS (it);
1215 line_height = it->ascent + it->descent;
1216 it->glyph_row = row;
1217 }
1218 }
1219
1220 return line_top_y + line_height;
1221 }
1222
1223 /* Subroutine of pos_visible_p below. Extracts a display string, if
1224 any, from the display spec given as its argument. */
1225 static Lisp_Object
1226 string_from_display_spec (Lisp_Object spec)
1227 {
1228 if (CONSP (spec))
1229 {
1230 while (CONSP (spec))
1231 {
1232 if (STRINGP (XCAR (spec)))
1233 return XCAR (spec);
1234 spec = XCDR (spec);
1235 }
1236 }
1237 else if (VECTORP (spec))
1238 {
1239 ptrdiff_t i;
1240
1241 for (i = 0; i < ASIZE (spec); i++)
1242 {
1243 if (STRINGP (AREF (spec, i)))
1244 return AREF (spec, i);
1245 }
1246 return Qnil;
1247 }
1248
1249 return spec;
1250 }
1251
1252 /* Return 1 if position CHARPOS is visible in window W.
1253 CHARPOS < 0 means return info about WINDOW_END position.
1254 If visible, set *X and *Y to pixel coordinates of top left corner.
1255 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1256 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1257
1258 int
1259 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1260 int *rtop, int *rbot, int *rowh, int *vpos)
1261 {
1262 struct it it;
1263 void *itdata = bidi_shelve_cache ();
1264 struct text_pos top;
1265 int visible_p = 0;
1266 struct buffer *old_buffer = NULL;
1267
1268 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1269 return visible_p;
1270
1271 if (XBUFFER (w->buffer) != current_buffer)
1272 {
1273 old_buffer = current_buffer;
1274 set_buffer_internal_1 (XBUFFER (w->buffer));
1275 }
1276
1277 SET_TEXT_POS_FROM_MARKER (top, w->start);
1278 /* Scrolling a minibuffer window via scroll bar when the echo area
1279 shows long text sometimes resets the minibuffer contents behind
1280 our backs. */
1281 if (CHARPOS (top) > ZV)
1282 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1283
1284 /* Compute exact mode line heights. */
1285 if (WINDOW_WANTS_MODELINE_P (w))
1286 current_mode_line_height
1287 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1288 BVAR (current_buffer, mode_line_format));
1289
1290 if (WINDOW_WANTS_HEADER_LINE_P (w))
1291 current_header_line_height
1292 = display_mode_line (w, HEADER_LINE_FACE_ID,
1293 BVAR (current_buffer, header_line_format));
1294
1295 start_display (&it, w, top);
1296 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1297 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1298
1299 if (charpos >= 0
1300 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1301 && IT_CHARPOS (it) >= charpos)
1302 /* When scanning backwards under bidi iteration, move_it_to
1303 stops at or _before_ CHARPOS, because it stops at or to
1304 the _right_ of the character at CHARPOS. */
1305 || (it.bidi_p && it.bidi_it.scan_dir == -1
1306 && IT_CHARPOS (it) <= charpos)))
1307 {
1308 /* We have reached CHARPOS, or passed it. How the call to
1309 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1310 or covered by a display property, move_it_to stops at the end
1311 of the invisible text, to the right of CHARPOS. (ii) If
1312 CHARPOS is in a display vector, move_it_to stops on its last
1313 glyph. */
1314 int top_x = it.current_x;
1315 int top_y = it.current_y;
1316 enum it_method it_method = it.method;
1317 /* Calling line_bottom_y may change it.method, it.position, etc. */
1318 int bottom_y = (last_height = 0, line_bottom_y (&it));
1319 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1320
1321 if (top_y < window_top_y)
1322 visible_p = bottom_y > window_top_y;
1323 else if (top_y < it.last_visible_y)
1324 visible_p = 1;
1325 if (visible_p)
1326 {
1327 if (it_method == GET_FROM_DISPLAY_VECTOR)
1328 {
1329 /* We stopped on the last glyph of a display vector.
1330 Try and recompute. Hack alert! */
1331 if (charpos < 2 || top.charpos >= charpos)
1332 top_x = it.glyph_row->x;
1333 else
1334 {
1335 struct it it2;
1336 start_display (&it2, w, top);
1337 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1338 get_next_display_element (&it2);
1339 PRODUCE_GLYPHS (&it2);
1340 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1341 || it2.current_x > it2.last_visible_x)
1342 top_x = it.glyph_row->x;
1343 else
1344 {
1345 top_x = it2.current_x;
1346 top_y = it2.current_y;
1347 }
1348 }
1349 }
1350 else if (IT_CHARPOS (it) != charpos)
1351 {
1352 Lisp_Object cpos = make_number (charpos);
1353 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1354 Lisp_Object string = string_from_display_spec (spec);
1355 int newline_in_string = 0;
1356
1357 if (STRINGP (string))
1358 {
1359 const char *s = SSDATA (string);
1360 const char *e = s + SBYTES (string);
1361 while (s < e)
1362 {
1363 if (*s++ == '\n')
1364 {
1365 newline_in_string = 1;
1366 break;
1367 }
1368 }
1369 }
1370 /* The tricky code below is needed because there's a
1371 discrepancy between move_it_to and how we set cursor
1372 when the display line ends in a newline from a
1373 display string. move_it_to will stop _after_ such
1374 display strings, whereas set_cursor_from_row
1375 conspires with cursor_row_p to place the cursor on
1376 the first glyph produced from the display string. */
1377
1378 /* We have overshoot PT because it is covered by a
1379 display property whose value is a string. If the
1380 string includes embedded newlines, we are also in the
1381 wrong display line. Backtrack to the correct line,
1382 where the display string begins. */
1383 if (newline_in_string)
1384 {
1385 Lisp_Object startpos, endpos;
1386 EMACS_INT start, end;
1387 struct it it3;
1388 int it3_moved;
1389
1390 /* Find the first and the last buffer positions
1391 covered by the display string. */
1392 endpos =
1393 Fnext_single_char_property_change (cpos, Qdisplay,
1394 Qnil, Qnil);
1395 startpos =
1396 Fprevious_single_char_property_change (endpos, Qdisplay,
1397 Qnil, Qnil);
1398 start = XFASTINT (startpos);
1399 end = XFASTINT (endpos);
1400 /* Move to the last buffer position before the
1401 display property. */
1402 start_display (&it3, w, top);
1403 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1404 /* Move forward one more line if the position before
1405 the display string is a newline or if it is the
1406 rightmost character on a line that is
1407 continued or word-wrapped. */
1408 if (it3.method == GET_FROM_BUFFER
1409 && it3.c == '\n')
1410 move_it_by_lines (&it3, 1);
1411 else if (move_it_in_display_line_to (&it3, -1,
1412 it3.current_x
1413 + it3.pixel_width,
1414 MOVE_TO_X)
1415 == MOVE_LINE_CONTINUED)
1416 {
1417 move_it_by_lines (&it3, 1);
1418 /* When we are under word-wrap, the #$@%!
1419 move_it_by_lines moves 2 lines, so we need to
1420 fix that up. */
1421 if (it3.line_wrap == WORD_WRAP)
1422 move_it_by_lines (&it3, -1);
1423 }
1424
1425 /* Record the vertical coordinate of the display
1426 line where we wound up. */
1427 top_y = it3.current_y;
1428 if (it3.bidi_p)
1429 {
1430 /* When characters are reordered for display,
1431 the character displayed to the left of the
1432 display string could be _after_ the display
1433 property in the logical order. Use the
1434 smallest vertical position of these two. */
1435 start_display (&it3, w, top);
1436 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1437 if (it3.current_y < top_y)
1438 top_y = it3.current_y;
1439 }
1440 /* Move from the top of the window to the beginning
1441 of the display line where the display string
1442 begins. */
1443 start_display (&it3, w, top);
1444 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1445 /* If it3_moved stays zero after the 'while' loop
1446 below, that means we already were at a newline
1447 before the loop (e.g., the display string begins
1448 with a newline), so we don't need to (and cannot)
1449 inspect the glyphs of it3.glyph_row, because
1450 PRODUCE_GLYPHS will not produce anything for a
1451 newline, and thus it3.glyph_row stays at its
1452 stale content it got at top of the window. */
1453 it3_moved = 0;
1454 /* Finally, advance the iterator until we hit the
1455 first display element whose character position is
1456 CHARPOS, or until the first newline from the
1457 display string, which signals the end of the
1458 display line. */
1459 while (get_next_display_element (&it3))
1460 {
1461 PRODUCE_GLYPHS (&it3);
1462 if (IT_CHARPOS (it3) == charpos
1463 || ITERATOR_AT_END_OF_LINE_P (&it3))
1464 break;
1465 it3_moved = 1;
1466 set_iterator_to_next (&it3, 0);
1467 }
1468 top_x = it3.current_x - it3.pixel_width;
1469 /* Normally, we would exit the above loop because we
1470 found the display element whose character
1471 position is CHARPOS. For the contingency that we
1472 didn't, and stopped at the first newline from the
1473 display string, move back over the glyphs
1474 produced from the string, until we find the
1475 rightmost glyph not from the string. */
1476 if (it3_moved
1477 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1478 {
1479 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1480 + it3.glyph_row->used[TEXT_AREA];
1481
1482 while (EQ ((g - 1)->object, string))
1483 {
1484 --g;
1485 top_x -= g->pixel_width;
1486 }
1487 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1488 + it3.glyph_row->used[TEXT_AREA]);
1489 }
1490 }
1491 }
1492
1493 *x = top_x;
1494 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1495 *rtop = max (0, window_top_y - top_y);
1496 *rbot = max (0, bottom_y - it.last_visible_y);
1497 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1498 - max (top_y, window_top_y)));
1499 *vpos = it.vpos;
1500 }
1501 }
1502 else
1503 {
1504 /* We were asked to provide info about WINDOW_END. */
1505 struct it it2;
1506 void *it2data = NULL;
1507
1508 SAVE_IT (it2, it, it2data);
1509 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1510 move_it_by_lines (&it, 1);
1511 if (charpos < IT_CHARPOS (it)
1512 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1513 {
1514 visible_p = 1;
1515 RESTORE_IT (&it2, &it2, it2data);
1516 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1517 *x = it2.current_x;
1518 *y = it2.current_y + it2.max_ascent - it2.ascent;
1519 *rtop = max (0, -it2.current_y);
1520 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1521 - it.last_visible_y));
1522 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1523 it.last_visible_y)
1524 - max (it2.current_y,
1525 WINDOW_HEADER_LINE_HEIGHT (w))));
1526 *vpos = it2.vpos;
1527 }
1528 else
1529 bidi_unshelve_cache (it2data, 1);
1530 }
1531 bidi_unshelve_cache (itdata, 0);
1532
1533 if (old_buffer)
1534 set_buffer_internal_1 (old_buffer);
1535
1536 current_header_line_height = current_mode_line_height = -1;
1537
1538 if (visible_p && XFASTINT (w->hscroll) > 0)
1539 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1540
1541 #if 0
1542 /* Debugging code. */
1543 if (visible_p)
1544 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1545 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1546 else
1547 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1548 #endif
1549
1550 return visible_p;
1551 }
1552
1553
1554 /* Return the next character from STR. Return in *LEN the length of
1555 the character. This is like STRING_CHAR_AND_LENGTH but never
1556 returns an invalid character. If we find one, we return a `?', but
1557 with the length of the invalid character. */
1558
1559 static inline int
1560 string_char_and_length (const unsigned char *str, int *len)
1561 {
1562 int c;
1563
1564 c = STRING_CHAR_AND_LENGTH (str, *len);
1565 if (!CHAR_VALID_P (c))
1566 /* We may not change the length here because other places in Emacs
1567 don't use this function, i.e. they silently accept invalid
1568 characters. */
1569 c = '?';
1570
1571 return c;
1572 }
1573
1574
1575
1576 /* Given a position POS containing a valid character and byte position
1577 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1578
1579 static struct text_pos
1580 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1581 {
1582 xassert (STRINGP (string) && nchars >= 0);
1583
1584 if (STRING_MULTIBYTE (string))
1585 {
1586 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1587 int len;
1588
1589 while (nchars--)
1590 {
1591 string_char_and_length (p, &len);
1592 p += len;
1593 CHARPOS (pos) += 1;
1594 BYTEPOS (pos) += len;
1595 }
1596 }
1597 else
1598 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1599
1600 return pos;
1601 }
1602
1603
1604 /* Value is the text position, i.e. character and byte position,
1605 for character position CHARPOS in STRING. */
1606
1607 static inline struct text_pos
1608 string_pos (EMACS_INT charpos, Lisp_Object string)
1609 {
1610 struct text_pos pos;
1611 xassert (STRINGP (string));
1612 xassert (charpos >= 0);
1613 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1614 return pos;
1615 }
1616
1617
1618 /* Value is a text position, i.e. character and byte position, for
1619 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1620 means recognize multibyte characters. */
1621
1622 static struct text_pos
1623 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1624 {
1625 struct text_pos pos;
1626
1627 xassert (s != NULL);
1628 xassert (charpos >= 0);
1629
1630 if (multibyte_p)
1631 {
1632 int len;
1633
1634 SET_TEXT_POS (pos, 0, 0);
1635 while (charpos--)
1636 {
1637 string_char_and_length ((const unsigned char *) s, &len);
1638 s += len;
1639 CHARPOS (pos) += 1;
1640 BYTEPOS (pos) += len;
1641 }
1642 }
1643 else
1644 SET_TEXT_POS (pos, charpos, charpos);
1645
1646 return pos;
1647 }
1648
1649
1650 /* Value is the number of characters in C string S. MULTIBYTE_P
1651 non-zero means recognize multibyte characters. */
1652
1653 static EMACS_INT
1654 number_of_chars (const char *s, int multibyte_p)
1655 {
1656 EMACS_INT nchars;
1657
1658 if (multibyte_p)
1659 {
1660 EMACS_INT rest = strlen (s);
1661 int len;
1662 const unsigned char *p = (const unsigned char *) s;
1663
1664 for (nchars = 0; rest > 0; ++nchars)
1665 {
1666 string_char_and_length (p, &len);
1667 rest -= len, p += len;
1668 }
1669 }
1670 else
1671 nchars = strlen (s);
1672
1673 return nchars;
1674 }
1675
1676
1677 /* Compute byte position NEWPOS->bytepos corresponding to
1678 NEWPOS->charpos. POS is a known position in string STRING.
1679 NEWPOS->charpos must be >= POS.charpos. */
1680
1681 static void
1682 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1683 {
1684 xassert (STRINGP (string));
1685 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1686
1687 if (STRING_MULTIBYTE (string))
1688 *newpos = string_pos_nchars_ahead (pos, string,
1689 CHARPOS (*newpos) - CHARPOS (pos));
1690 else
1691 BYTEPOS (*newpos) = CHARPOS (*newpos);
1692 }
1693
1694 /* EXPORT:
1695 Return an estimation of the pixel height of mode or header lines on
1696 frame F. FACE_ID specifies what line's height to estimate. */
1697
1698 int
1699 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1700 {
1701 #ifdef HAVE_WINDOW_SYSTEM
1702 if (FRAME_WINDOW_P (f))
1703 {
1704 int height = FONT_HEIGHT (FRAME_FONT (f));
1705
1706 /* This function is called so early when Emacs starts that the face
1707 cache and mode line face are not yet initialized. */
1708 if (FRAME_FACE_CACHE (f))
1709 {
1710 struct face *face = FACE_FROM_ID (f, face_id);
1711 if (face)
1712 {
1713 if (face->font)
1714 height = FONT_HEIGHT (face->font);
1715 if (face->box_line_width > 0)
1716 height += 2 * face->box_line_width;
1717 }
1718 }
1719
1720 return height;
1721 }
1722 #endif
1723
1724 return 1;
1725 }
1726
1727 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1728 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1729 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1730 not force the value into range. */
1731
1732 void
1733 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1734 int *x, int *y, NativeRectangle *bounds, int noclip)
1735 {
1736
1737 #ifdef HAVE_WINDOW_SYSTEM
1738 if (FRAME_WINDOW_P (f))
1739 {
1740 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1741 even for negative values. */
1742 if (pix_x < 0)
1743 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1744 if (pix_y < 0)
1745 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1746
1747 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1748 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1749
1750 if (bounds)
1751 STORE_NATIVE_RECT (*bounds,
1752 FRAME_COL_TO_PIXEL_X (f, pix_x),
1753 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1754 FRAME_COLUMN_WIDTH (f) - 1,
1755 FRAME_LINE_HEIGHT (f) - 1);
1756
1757 if (!noclip)
1758 {
1759 if (pix_x < 0)
1760 pix_x = 0;
1761 else if (pix_x > FRAME_TOTAL_COLS (f))
1762 pix_x = FRAME_TOTAL_COLS (f);
1763
1764 if (pix_y < 0)
1765 pix_y = 0;
1766 else if (pix_y > FRAME_LINES (f))
1767 pix_y = FRAME_LINES (f);
1768 }
1769 }
1770 #endif
1771
1772 *x = pix_x;
1773 *y = pix_y;
1774 }
1775
1776
1777 /* Find the glyph under window-relative coordinates X/Y in window W.
1778 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1779 strings. Return in *HPOS and *VPOS the row and column number of
1780 the glyph found. Return in *AREA the glyph area containing X.
1781 Value is a pointer to the glyph found or null if X/Y is not on
1782 text, or we can't tell because W's current matrix is not up to
1783 date. */
1784
1785 static
1786 struct glyph *
1787 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1788 int *dx, int *dy, int *area)
1789 {
1790 struct glyph *glyph, *end;
1791 struct glyph_row *row = NULL;
1792 int x0, i;
1793
1794 /* Find row containing Y. Give up if some row is not enabled. */
1795 for (i = 0; i < w->current_matrix->nrows; ++i)
1796 {
1797 row = MATRIX_ROW (w->current_matrix, i);
1798 if (!row->enabled_p)
1799 return NULL;
1800 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1801 break;
1802 }
1803
1804 *vpos = i;
1805 *hpos = 0;
1806
1807 /* Give up if Y is not in the window. */
1808 if (i == w->current_matrix->nrows)
1809 return NULL;
1810
1811 /* Get the glyph area containing X. */
1812 if (w->pseudo_window_p)
1813 {
1814 *area = TEXT_AREA;
1815 x0 = 0;
1816 }
1817 else
1818 {
1819 if (x < window_box_left_offset (w, TEXT_AREA))
1820 {
1821 *area = LEFT_MARGIN_AREA;
1822 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1823 }
1824 else if (x < window_box_right_offset (w, TEXT_AREA))
1825 {
1826 *area = TEXT_AREA;
1827 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1828 }
1829 else
1830 {
1831 *area = RIGHT_MARGIN_AREA;
1832 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1833 }
1834 }
1835
1836 /* Find glyph containing X. */
1837 glyph = row->glyphs[*area];
1838 end = glyph + row->used[*area];
1839 x -= x0;
1840 while (glyph < end && x >= glyph->pixel_width)
1841 {
1842 x -= glyph->pixel_width;
1843 ++glyph;
1844 }
1845
1846 if (glyph == end)
1847 return NULL;
1848
1849 if (dx)
1850 {
1851 *dx = x;
1852 *dy = y - (row->y + row->ascent - glyph->ascent);
1853 }
1854
1855 *hpos = glyph - row->glyphs[*area];
1856 return glyph;
1857 }
1858
1859 /* Convert frame-relative x/y to coordinates relative to window W.
1860 Takes pseudo-windows into account. */
1861
1862 static void
1863 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1864 {
1865 if (w->pseudo_window_p)
1866 {
1867 /* A pseudo-window is always full-width, and starts at the
1868 left edge of the frame, plus a frame border. */
1869 struct frame *f = XFRAME (w->frame);
1870 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1871 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1872 }
1873 else
1874 {
1875 *x -= WINDOW_LEFT_EDGE_X (w);
1876 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1877 }
1878 }
1879
1880 #ifdef HAVE_WINDOW_SYSTEM
1881
1882 /* EXPORT:
1883 Return in RECTS[] at most N clipping rectangles for glyph string S.
1884 Return the number of stored rectangles. */
1885
1886 int
1887 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1888 {
1889 XRectangle r;
1890
1891 if (n <= 0)
1892 return 0;
1893
1894 if (s->row->full_width_p)
1895 {
1896 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1897 r.x = WINDOW_LEFT_EDGE_X (s->w);
1898 r.width = WINDOW_TOTAL_WIDTH (s->w);
1899
1900 /* Unless displaying a mode or menu bar line, which are always
1901 fully visible, clip to the visible part of the row. */
1902 if (s->w->pseudo_window_p)
1903 r.height = s->row->visible_height;
1904 else
1905 r.height = s->height;
1906 }
1907 else
1908 {
1909 /* This is a text line that may be partially visible. */
1910 r.x = window_box_left (s->w, s->area);
1911 r.width = window_box_width (s->w, s->area);
1912 r.height = s->row->visible_height;
1913 }
1914
1915 if (s->clip_head)
1916 if (r.x < s->clip_head->x)
1917 {
1918 if (r.width >= s->clip_head->x - r.x)
1919 r.width -= s->clip_head->x - r.x;
1920 else
1921 r.width = 0;
1922 r.x = s->clip_head->x;
1923 }
1924 if (s->clip_tail)
1925 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1926 {
1927 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1928 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1929 else
1930 r.width = 0;
1931 }
1932
1933 /* If S draws overlapping rows, it's sufficient to use the top and
1934 bottom of the window for clipping because this glyph string
1935 intentionally draws over other lines. */
1936 if (s->for_overlaps)
1937 {
1938 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1939 r.height = window_text_bottom_y (s->w) - r.y;
1940
1941 /* Alas, the above simple strategy does not work for the
1942 environments with anti-aliased text: if the same text is
1943 drawn onto the same place multiple times, it gets thicker.
1944 If the overlap we are processing is for the erased cursor, we
1945 take the intersection with the rectangle of the cursor. */
1946 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1947 {
1948 XRectangle rc, r_save = r;
1949
1950 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1951 rc.y = s->w->phys_cursor.y;
1952 rc.width = s->w->phys_cursor_width;
1953 rc.height = s->w->phys_cursor_height;
1954
1955 x_intersect_rectangles (&r_save, &rc, &r);
1956 }
1957 }
1958 else
1959 {
1960 /* Don't use S->y for clipping because it doesn't take partially
1961 visible lines into account. For example, it can be negative for
1962 partially visible lines at the top of a window. */
1963 if (!s->row->full_width_p
1964 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1965 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1966 else
1967 r.y = max (0, s->row->y);
1968 }
1969
1970 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1971
1972 /* If drawing the cursor, don't let glyph draw outside its
1973 advertised boundaries. Cleartype does this under some circumstances. */
1974 if (s->hl == DRAW_CURSOR)
1975 {
1976 struct glyph *glyph = s->first_glyph;
1977 int height, max_y;
1978
1979 if (s->x > r.x)
1980 {
1981 r.width -= s->x - r.x;
1982 r.x = s->x;
1983 }
1984 r.width = min (r.width, glyph->pixel_width);
1985
1986 /* If r.y is below window bottom, ensure that we still see a cursor. */
1987 height = min (glyph->ascent + glyph->descent,
1988 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1989 max_y = window_text_bottom_y (s->w) - height;
1990 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1991 if (s->ybase - glyph->ascent > max_y)
1992 {
1993 r.y = max_y;
1994 r.height = height;
1995 }
1996 else
1997 {
1998 /* Don't draw cursor glyph taller than our actual glyph. */
1999 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2000 if (height < r.height)
2001 {
2002 max_y = r.y + r.height;
2003 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2004 r.height = min (max_y - r.y, height);
2005 }
2006 }
2007 }
2008
2009 if (s->row->clip)
2010 {
2011 XRectangle r_save = r;
2012
2013 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2014 r.width = 0;
2015 }
2016
2017 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2018 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2019 {
2020 #ifdef CONVERT_FROM_XRECT
2021 CONVERT_FROM_XRECT (r, *rects);
2022 #else
2023 *rects = r;
2024 #endif
2025 return 1;
2026 }
2027 else
2028 {
2029 /* If we are processing overlapping and allowed to return
2030 multiple clipping rectangles, we exclude the row of the glyph
2031 string from the clipping rectangle. This is to avoid drawing
2032 the same text on the environment with anti-aliasing. */
2033 #ifdef CONVERT_FROM_XRECT
2034 XRectangle rs[2];
2035 #else
2036 XRectangle *rs = rects;
2037 #endif
2038 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2039
2040 if (s->for_overlaps & OVERLAPS_PRED)
2041 {
2042 rs[i] = r;
2043 if (r.y + r.height > row_y)
2044 {
2045 if (r.y < row_y)
2046 rs[i].height = row_y - r.y;
2047 else
2048 rs[i].height = 0;
2049 }
2050 i++;
2051 }
2052 if (s->for_overlaps & OVERLAPS_SUCC)
2053 {
2054 rs[i] = r;
2055 if (r.y < row_y + s->row->visible_height)
2056 {
2057 if (r.y + r.height > row_y + s->row->visible_height)
2058 {
2059 rs[i].y = row_y + s->row->visible_height;
2060 rs[i].height = r.y + r.height - rs[i].y;
2061 }
2062 else
2063 rs[i].height = 0;
2064 }
2065 i++;
2066 }
2067
2068 n = i;
2069 #ifdef CONVERT_FROM_XRECT
2070 for (i = 0; i < n; i++)
2071 CONVERT_FROM_XRECT (rs[i], rects[i]);
2072 #endif
2073 return n;
2074 }
2075 }
2076
2077 /* EXPORT:
2078 Return in *NR the clipping rectangle for glyph string S. */
2079
2080 void
2081 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2082 {
2083 get_glyph_string_clip_rects (s, nr, 1);
2084 }
2085
2086
2087 /* EXPORT:
2088 Return the position and height of the phys cursor in window W.
2089 Set w->phys_cursor_width to width of phys cursor.
2090 */
2091
2092 void
2093 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2094 struct glyph *glyph, int *xp, int *yp, int *heightp)
2095 {
2096 struct frame *f = XFRAME (WINDOW_FRAME (w));
2097 int x, y, wd, h, h0, y0;
2098
2099 /* Compute the width of the rectangle to draw. If on a stretch
2100 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2101 rectangle as wide as the glyph, but use a canonical character
2102 width instead. */
2103 wd = glyph->pixel_width - 1;
2104 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2105 wd++; /* Why? */
2106 #endif
2107
2108 x = w->phys_cursor.x;
2109 if (x < 0)
2110 {
2111 wd += x;
2112 x = 0;
2113 }
2114
2115 if (glyph->type == STRETCH_GLYPH
2116 && !x_stretch_cursor_p)
2117 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2118 w->phys_cursor_width = wd;
2119
2120 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2121
2122 /* If y is below window bottom, ensure that we still see a cursor. */
2123 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2124
2125 h = max (h0, glyph->ascent + glyph->descent);
2126 h0 = min (h0, glyph->ascent + glyph->descent);
2127
2128 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2129 if (y < y0)
2130 {
2131 h = max (h - (y0 - y) + 1, h0);
2132 y = y0 - 1;
2133 }
2134 else
2135 {
2136 y0 = window_text_bottom_y (w) - h0;
2137 if (y > y0)
2138 {
2139 h += y - y0;
2140 y = y0;
2141 }
2142 }
2143
2144 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2145 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2146 *heightp = h;
2147 }
2148
2149 /*
2150 * Remember which glyph the mouse is over.
2151 */
2152
2153 void
2154 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2155 {
2156 Lisp_Object window;
2157 struct window *w;
2158 struct glyph_row *r, *gr, *end_row;
2159 enum window_part part;
2160 enum glyph_row_area area;
2161 int x, y, width, height;
2162
2163 /* Try to determine frame pixel position and size of the glyph under
2164 frame pixel coordinates X/Y on frame F. */
2165
2166 if (!f->glyphs_initialized_p
2167 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2168 NILP (window)))
2169 {
2170 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2171 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2172 goto virtual_glyph;
2173 }
2174
2175 w = XWINDOW (window);
2176 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2177 height = WINDOW_FRAME_LINE_HEIGHT (w);
2178
2179 x = window_relative_x_coord (w, part, gx);
2180 y = gy - WINDOW_TOP_EDGE_Y (w);
2181
2182 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2183 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2184
2185 if (w->pseudo_window_p)
2186 {
2187 area = TEXT_AREA;
2188 part = ON_MODE_LINE; /* Don't adjust margin. */
2189 goto text_glyph;
2190 }
2191
2192 switch (part)
2193 {
2194 case ON_LEFT_MARGIN:
2195 area = LEFT_MARGIN_AREA;
2196 goto text_glyph;
2197
2198 case ON_RIGHT_MARGIN:
2199 area = RIGHT_MARGIN_AREA;
2200 goto text_glyph;
2201
2202 case ON_HEADER_LINE:
2203 case ON_MODE_LINE:
2204 gr = (part == ON_HEADER_LINE
2205 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2206 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2207 gy = gr->y;
2208 area = TEXT_AREA;
2209 goto text_glyph_row_found;
2210
2211 case ON_TEXT:
2212 area = TEXT_AREA;
2213
2214 text_glyph:
2215 gr = 0; gy = 0;
2216 for (; r <= end_row && r->enabled_p; ++r)
2217 if (r->y + r->height > y)
2218 {
2219 gr = r; gy = r->y;
2220 break;
2221 }
2222
2223 text_glyph_row_found:
2224 if (gr && gy <= y)
2225 {
2226 struct glyph *g = gr->glyphs[area];
2227 struct glyph *end = g + gr->used[area];
2228
2229 height = gr->height;
2230 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2231 if (gx + g->pixel_width > x)
2232 break;
2233
2234 if (g < end)
2235 {
2236 if (g->type == IMAGE_GLYPH)
2237 {
2238 /* Don't remember when mouse is over image, as
2239 image may have hot-spots. */
2240 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2241 return;
2242 }
2243 width = g->pixel_width;
2244 }
2245 else
2246 {
2247 /* Use nominal char spacing at end of line. */
2248 x -= gx;
2249 gx += (x / width) * width;
2250 }
2251
2252 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2253 gx += window_box_left_offset (w, area);
2254 }
2255 else
2256 {
2257 /* Use nominal line height at end of window. */
2258 gx = (x / width) * width;
2259 y -= gy;
2260 gy += (y / height) * height;
2261 }
2262 break;
2263
2264 case ON_LEFT_FRINGE:
2265 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2266 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2267 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2268 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2269 goto row_glyph;
2270
2271 case ON_RIGHT_FRINGE:
2272 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2273 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2274 : window_box_right_offset (w, TEXT_AREA));
2275 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2276 goto row_glyph;
2277
2278 case ON_SCROLL_BAR:
2279 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2280 ? 0
2281 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2282 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2283 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2284 : 0)));
2285 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2286
2287 row_glyph:
2288 gr = 0, gy = 0;
2289 for (; r <= end_row && r->enabled_p; ++r)
2290 if (r->y + r->height > y)
2291 {
2292 gr = r; gy = r->y;
2293 break;
2294 }
2295
2296 if (gr && gy <= y)
2297 height = gr->height;
2298 else
2299 {
2300 /* Use nominal line height at end of window. */
2301 y -= gy;
2302 gy += (y / height) * height;
2303 }
2304 break;
2305
2306 default:
2307 ;
2308 virtual_glyph:
2309 /* If there is no glyph under the mouse, then we divide the screen
2310 into a grid of the smallest glyph in the frame, and use that
2311 as our "glyph". */
2312
2313 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2314 round down even for negative values. */
2315 if (gx < 0)
2316 gx -= width - 1;
2317 if (gy < 0)
2318 gy -= height - 1;
2319
2320 gx = (gx / width) * width;
2321 gy = (gy / height) * height;
2322
2323 goto store_rect;
2324 }
2325
2326 gx += WINDOW_LEFT_EDGE_X (w);
2327 gy += WINDOW_TOP_EDGE_Y (w);
2328
2329 store_rect:
2330 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2331
2332 /* Visible feedback for debugging. */
2333 #if 0
2334 #if HAVE_X_WINDOWS
2335 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2336 f->output_data.x->normal_gc,
2337 gx, gy, width, height);
2338 #endif
2339 #endif
2340 }
2341
2342
2343 #endif /* HAVE_WINDOW_SYSTEM */
2344
2345 \f
2346 /***********************************************************************
2347 Lisp form evaluation
2348 ***********************************************************************/
2349
2350 /* Error handler for safe_eval and safe_call. */
2351
2352 static Lisp_Object
2353 safe_eval_handler (Lisp_Object arg)
2354 {
2355 add_to_log ("Error during redisplay: %S", arg, Qnil);
2356 return Qnil;
2357 }
2358
2359
2360 /* Evaluate SEXPR and return the result, or nil if something went
2361 wrong. Prevent redisplay during the evaluation. */
2362
2363 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2364 Return the result, or nil if something went wrong. Prevent
2365 redisplay during the evaluation. */
2366
2367 Lisp_Object
2368 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2369 {
2370 Lisp_Object val;
2371
2372 if (inhibit_eval_during_redisplay)
2373 val = Qnil;
2374 else
2375 {
2376 int count = SPECPDL_INDEX ();
2377 struct gcpro gcpro1;
2378
2379 GCPRO1 (args[0]);
2380 gcpro1.nvars = nargs;
2381 specbind (Qinhibit_redisplay, Qt);
2382 /* Use Qt to ensure debugger does not run,
2383 so there is no possibility of wanting to redisplay. */
2384 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2385 safe_eval_handler);
2386 UNGCPRO;
2387 val = unbind_to (count, val);
2388 }
2389
2390 return val;
2391 }
2392
2393
2394 /* Call function FN with one argument ARG.
2395 Return the result, or nil if something went wrong. */
2396
2397 Lisp_Object
2398 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2399 {
2400 Lisp_Object args[2];
2401 args[0] = fn;
2402 args[1] = arg;
2403 return safe_call (2, args);
2404 }
2405
2406 static Lisp_Object Qeval;
2407
2408 Lisp_Object
2409 safe_eval (Lisp_Object sexpr)
2410 {
2411 return safe_call1 (Qeval, sexpr);
2412 }
2413
2414 /* Call function FN with one argument ARG.
2415 Return the result, or nil if something went wrong. */
2416
2417 Lisp_Object
2418 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2419 {
2420 Lisp_Object args[3];
2421 args[0] = fn;
2422 args[1] = arg1;
2423 args[2] = arg2;
2424 return safe_call (3, args);
2425 }
2426
2427
2428 \f
2429 /***********************************************************************
2430 Debugging
2431 ***********************************************************************/
2432
2433 #if 0
2434
2435 /* Define CHECK_IT to perform sanity checks on iterators.
2436 This is for debugging. It is too slow to do unconditionally. */
2437
2438 static void
2439 check_it (struct it *it)
2440 {
2441 if (it->method == GET_FROM_STRING)
2442 {
2443 xassert (STRINGP (it->string));
2444 xassert (IT_STRING_CHARPOS (*it) >= 0);
2445 }
2446 else
2447 {
2448 xassert (IT_STRING_CHARPOS (*it) < 0);
2449 if (it->method == GET_FROM_BUFFER)
2450 {
2451 /* Check that character and byte positions agree. */
2452 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2453 }
2454 }
2455
2456 if (it->dpvec)
2457 xassert (it->current.dpvec_index >= 0);
2458 else
2459 xassert (it->current.dpvec_index < 0);
2460 }
2461
2462 #define CHECK_IT(IT) check_it ((IT))
2463
2464 #else /* not 0 */
2465
2466 #define CHECK_IT(IT) (void) 0
2467
2468 #endif /* not 0 */
2469
2470
2471 #if GLYPH_DEBUG && XASSERTS
2472
2473 /* Check that the window end of window W is what we expect it
2474 to be---the last row in the current matrix displaying text. */
2475
2476 static void
2477 check_window_end (struct window *w)
2478 {
2479 if (!MINI_WINDOW_P (w)
2480 && !NILP (w->window_end_valid))
2481 {
2482 struct glyph_row *row;
2483 xassert ((row = MATRIX_ROW (w->current_matrix,
2484 XFASTINT (w->window_end_vpos)),
2485 !row->enabled_p
2486 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2487 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2488 }
2489 }
2490
2491 #define CHECK_WINDOW_END(W) check_window_end ((W))
2492
2493 #else
2494
2495 #define CHECK_WINDOW_END(W) (void) 0
2496
2497 #endif
2498
2499
2500 \f
2501 /***********************************************************************
2502 Iterator initialization
2503 ***********************************************************************/
2504
2505 /* Initialize IT for displaying current_buffer in window W, starting
2506 at character position CHARPOS. CHARPOS < 0 means that no buffer
2507 position is specified which is useful when the iterator is assigned
2508 a position later. BYTEPOS is the byte position corresponding to
2509 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2510
2511 If ROW is not null, calls to produce_glyphs with IT as parameter
2512 will produce glyphs in that row.
2513
2514 BASE_FACE_ID is the id of a base face to use. It must be one of
2515 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2516 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2517 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2518
2519 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2520 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2521 will be initialized to use the corresponding mode line glyph row of
2522 the desired matrix of W. */
2523
2524 void
2525 init_iterator (struct it *it, struct window *w,
2526 EMACS_INT charpos, EMACS_INT bytepos,
2527 struct glyph_row *row, enum face_id base_face_id)
2528 {
2529 int highlight_region_p;
2530 enum face_id remapped_base_face_id = base_face_id;
2531
2532 /* Some precondition checks. */
2533 xassert (w != NULL && it != NULL);
2534 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2535 && charpos <= ZV));
2536
2537 /* If face attributes have been changed since the last redisplay,
2538 free realized faces now because they depend on face definitions
2539 that might have changed. Don't free faces while there might be
2540 desired matrices pending which reference these faces. */
2541 if (face_change_count && !inhibit_free_realized_faces)
2542 {
2543 face_change_count = 0;
2544 free_all_realized_faces (Qnil);
2545 }
2546
2547 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2548 if (! NILP (Vface_remapping_alist))
2549 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2550
2551 /* Use one of the mode line rows of W's desired matrix if
2552 appropriate. */
2553 if (row == NULL)
2554 {
2555 if (base_face_id == MODE_LINE_FACE_ID
2556 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2557 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2558 else if (base_face_id == HEADER_LINE_FACE_ID)
2559 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2560 }
2561
2562 /* Clear IT. */
2563 memset (it, 0, sizeof *it);
2564 it->current.overlay_string_index = -1;
2565 it->current.dpvec_index = -1;
2566 it->base_face_id = remapped_base_face_id;
2567 it->string = Qnil;
2568 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2569 it->paragraph_embedding = L2R;
2570 it->bidi_it.string.lstring = Qnil;
2571 it->bidi_it.string.s = NULL;
2572 it->bidi_it.string.bufpos = 0;
2573
2574 /* The window in which we iterate over current_buffer: */
2575 XSETWINDOW (it->window, w);
2576 it->w = w;
2577 it->f = XFRAME (w->frame);
2578
2579 it->cmp_it.id = -1;
2580
2581 /* Extra space between lines (on window systems only). */
2582 if (base_face_id == DEFAULT_FACE_ID
2583 && FRAME_WINDOW_P (it->f))
2584 {
2585 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2586 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2587 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2588 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2589 * FRAME_LINE_HEIGHT (it->f));
2590 else if (it->f->extra_line_spacing > 0)
2591 it->extra_line_spacing = it->f->extra_line_spacing;
2592 it->max_extra_line_spacing = 0;
2593 }
2594
2595 /* If realized faces have been removed, e.g. because of face
2596 attribute changes of named faces, recompute them. When running
2597 in batch mode, the face cache of the initial frame is null. If
2598 we happen to get called, make a dummy face cache. */
2599 if (FRAME_FACE_CACHE (it->f) == NULL)
2600 init_frame_faces (it->f);
2601 if (FRAME_FACE_CACHE (it->f)->used == 0)
2602 recompute_basic_faces (it->f);
2603
2604 /* Current value of the `slice', `space-width', and 'height' properties. */
2605 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2606 it->space_width = Qnil;
2607 it->font_height = Qnil;
2608 it->override_ascent = -1;
2609
2610 /* Are control characters displayed as `^C'? */
2611 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2612
2613 /* -1 means everything between a CR and the following line end
2614 is invisible. >0 means lines indented more than this value are
2615 invisible. */
2616 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2617 ? XINT (BVAR (current_buffer, selective_display))
2618 : (!NILP (BVAR (current_buffer, selective_display))
2619 ? -1 : 0));
2620 it->selective_display_ellipsis_p
2621 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2622
2623 /* Display table to use. */
2624 it->dp = window_display_table (w);
2625
2626 /* Are multibyte characters enabled in current_buffer? */
2627 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2628
2629 /* Non-zero if we should highlight the region. */
2630 highlight_region_p
2631 = (!NILP (Vtransient_mark_mode)
2632 && !NILP (BVAR (current_buffer, mark_active))
2633 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2634
2635 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2636 start and end of a visible region in window IT->w. Set both to
2637 -1 to indicate no region. */
2638 if (highlight_region_p
2639 /* Maybe highlight only in selected window. */
2640 && (/* Either show region everywhere. */
2641 highlight_nonselected_windows
2642 /* Or show region in the selected window. */
2643 || w == XWINDOW (selected_window)
2644 /* Or show the region if we are in the mini-buffer and W is
2645 the window the mini-buffer refers to. */
2646 || (MINI_WINDOW_P (XWINDOW (selected_window))
2647 && WINDOWP (minibuf_selected_window)
2648 && w == XWINDOW (minibuf_selected_window))))
2649 {
2650 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2651 it->region_beg_charpos = min (PT, markpos);
2652 it->region_end_charpos = max (PT, markpos);
2653 }
2654 else
2655 it->region_beg_charpos = it->region_end_charpos = -1;
2656
2657 /* Get the position at which the redisplay_end_trigger hook should
2658 be run, if it is to be run at all. */
2659 if (MARKERP (w->redisplay_end_trigger)
2660 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2661 it->redisplay_end_trigger_charpos
2662 = marker_position (w->redisplay_end_trigger);
2663 else if (INTEGERP (w->redisplay_end_trigger))
2664 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2665
2666 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2667
2668 /* Are lines in the display truncated? */
2669 if (base_face_id != DEFAULT_FACE_ID
2670 || XINT (it->w->hscroll)
2671 || (! WINDOW_FULL_WIDTH_P (it->w)
2672 && ((!NILP (Vtruncate_partial_width_windows)
2673 && !INTEGERP (Vtruncate_partial_width_windows))
2674 || (INTEGERP (Vtruncate_partial_width_windows)
2675 && (WINDOW_TOTAL_COLS (it->w)
2676 < XINT (Vtruncate_partial_width_windows))))))
2677 it->line_wrap = TRUNCATE;
2678 else if (NILP (BVAR (current_buffer, truncate_lines)))
2679 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2680 ? WINDOW_WRAP : WORD_WRAP;
2681 else
2682 it->line_wrap = TRUNCATE;
2683
2684 /* Get dimensions of truncation and continuation glyphs. These are
2685 displayed as fringe bitmaps under X, so we don't need them for such
2686 frames. */
2687 if (!FRAME_WINDOW_P (it->f))
2688 {
2689 if (it->line_wrap == TRUNCATE)
2690 {
2691 /* We will need the truncation glyph. */
2692 xassert (it->glyph_row == NULL);
2693 produce_special_glyphs (it, IT_TRUNCATION);
2694 it->truncation_pixel_width = it->pixel_width;
2695 }
2696 else
2697 {
2698 /* We will need the continuation glyph. */
2699 xassert (it->glyph_row == NULL);
2700 produce_special_glyphs (it, IT_CONTINUATION);
2701 it->continuation_pixel_width = it->pixel_width;
2702 }
2703
2704 /* Reset these values to zero because the produce_special_glyphs
2705 above has changed them. */
2706 it->pixel_width = it->ascent = it->descent = 0;
2707 it->phys_ascent = it->phys_descent = 0;
2708 }
2709
2710 /* Set this after getting the dimensions of truncation and
2711 continuation glyphs, so that we don't produce glyphs when calling
2712 produce_special_glyphs, above. */
2713 it->glyph_row = row;
2714 it->area = TEXT_AREA;
2715
2716 /* Forget any previous info about this row being reversed. */
2717 if (it->glyph_row)
2718 it->glyph_row->reversed_p = 0;
2719
2720 /* Get the dimensions of the display area. The display area
2721 consists of the visible window area plus a horizontally scrolled
2722 part to the left of the window. All x-values are relative to the
2723 start of this total display area. */
2724 if (base_face_id != DEFAULT_FACE_ID)
2725 {
2726 /* Mode lines, menu bar in terminal frames. */
2727 it->first_visible_x = 0;
2728 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2729 }
2730 else
2731 {
2732 it->first_visible_x
2733 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2734 it->last_visible_x = (it->first_visible_x
2735 + window_box_width (w, TEXT_AREA));
2736
2737 /* If we truncate lines, leave room for the truncator glyph(s) at
2738 the right margin. Otherwise, leave room for the continuation
2739 glyph(s). Truncation and continuation glyphs are not inserted
2740 for window-based redisplay. */
2741 if (!FRAME_WINDOW_P (it->f))
2742 {
2743 if (it->line_wrap == TRUNCATE)
2744 it->last_visible_x -= it->truncation_pixel_width;
2745 else
2746 it->last_visible_x -= it->continuation_pixel_width;
2747 }
2748
2749 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2750 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2751 }
2752
2753 /* Leave room for a border glyph. */
2754 if (!FRAME_WINDOW_P (it->f)
2755 && !WINDOW_RIGHTMOST_P (it->w))
2756 it->last_visible_x -= 1;
2757
2758 it->last_visible_y = window_text_bottom_y (w);
2759
2760 /* For mode lines and alike, arrange for the first glyph having a
2761 left box line if the face specifies a box. */
2762 if (base_face_id != DEFAULT_FACE_ID)
2763 {
2764 struct face *face;
2765
2766 it->face_id = remapped_base_face_id;
2767
2768 /* If we have a boxed mode line, make the first character appear
2769 with a left box line. */
2770 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2771 if (face->box != FACE_NO_BOX)
2772 it->start_of_box_run_p = 1;
2773 }
2774
2775 /* If a buffer position was specified, set the iterator there,
2776 getting overlays and face properties from that position. */
2777 if (charpos >= BUF_BEG (current_buffer))
2778 {
2779 it->end_charpos = ZV;
2780 IT_CHARPOS (*it) = charpos;
2781
2782 /* We will rely on `reseat' to set this up properly, via
2783 handle_face_prop. */
2784 it->face_id = it->base_face_id;
2785
2786 /* Compute byte position if not specified. */
2787 if (bytepos < charpos)
2788 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2789 else
2790 IT_BYTEPOS (*it) = bytepos;
2791
2792 it->start = it->current;
2793 /* Do we need to reorder bidirectional text? Not if this is a
2794 unibyte buffer: by definition, none of the single-byte
2795 characters are strong R2L, so no reordering is needed. And
2796 bidi.c doesn't support unibyte buffers anyway. Also, don't
2797 reorder while we are loading loadup.el, since the tables of
2798 character properties needed for reordering are not yet
2799 available. */
2800 it->bidi_p =
2801 NILP (Vpurify_flag)
2802 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2803 && it->multibyte_p;
2804
2805 /* If we are to reorder bidirectional text, init the bidi
2806 iterator. */
2807 if (it->bidi_p)
2808 {
2809 /* Note the paragraph direction that this buffer wants to
2810 use. */
2811 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2812 Qleft_to_right))
2813 it->paragraph_embedding = L2R;
2814 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2815 Qright_to_left))
2816 it->paragraph_embedding = R2L;
2817 else
2818 it->paragraph_embedding = NEUTRAL_DIR;
2819 bidi_unshelve_cache (NULL, 0);
2820 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2821 &it->bidi_it);
2822 }
2823
2824 /* Compute faces etc. */
2825 reseat (it, it->current.pos, 1);
2826 }
2827
2828 CHECK_IT (it);
2829 }
2830
2831
2832 /* Initialize IT for the display of window W with window start POS. */
2833
2834 void
2835 start_display (struct it *it, struct window *w, struct text_pos pos)
2836 {
2837 struct glyph_row *row;
2838 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2839
2840 row = w->desired_matrix->rows + first_vpos;
2841 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2842 it->first_vpos = first_vpos;
2843
2844 /* Don't reseat to previous visible line start if current start
2845 position is in a string or image. */
2846 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2847 {
2848 int start_at_line_beg_p;
2849 int first_y = it->current_y;
2850
2851 /* If window start is not at a line start, skip forward to POS to
2852 get the correct continuation lines width. */
2853 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2854 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2855 if (!start_at_line_beg_p)
2856 {
2857 int new_x;
2858
2859 reseat_at_previous_visible_line_start (it);
2860 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2861
2862 new_x = it->current_x + it->pixel_width;
2863
2864 /* If lines are continued, this line may end in the middle
2865 of a multi-glyph character (e.g. a control character
2866 displayed as \003, or in the middle of an overlay
2867 string). In this case move_it_to above will not have
2868 taken us to the start of the continuation line but to the
2869 end of the continued line. */
2870 if (it->current_x > 0
2871 && it->line_wrap != TRUNCATE /* Lines are continued. */
2872 && (/* And glyph doesn't fit on the line. */
2873 new_x > it->last_visible_x
2874 /* Or it fits exactly and we're on a window
2875 system frame. */
2876 || (new_x == it->last_visible_x
2877 && FRAME_WINDOW_P (it->f))))
2878 {
2879 if ((it->current.dpvec_index >= 0
2880 || it->current.overlay_string_index >= 0)
2881 /* If we are on a newline from a display vector or
2882 overlay string, then we are already at the end of
2883 a screen line; no need to go to the next line in
2884 that case, as this line is not really continued.
2885 (If we do go to the next line, C-e will not DTRT.) */
2886 && it->c != '\n')
2887 {
2888 set_iterator_to_next (it, 1);
2889 move_it_in_display_line_to (it, -1, -1, 0);
2890 }
2891
2892 it->continuation_lines_width += it->current_x;
2893 }
2894 /* If the character at POS is displayed via a display
2895 vector, move_it_to above stops at the final glyph of
2896 IT->dpvec. To make the caller redisplay that character
2897 again (a.k.a. start at POS), we need to reset the
2898 dpvec_index to the beginning of IT->dpvec. */
2899 else if (it->current.dpvec_index >= 0)
2900 it->current.dpvec_index = 0;
2901
2902 /* We're starting a new display line, not affected by the
2903 height of the continued line, so clear the appropriate
2904 fields in the iterator structure. */
2905 it->max_ascent = it->max_descent = 0;
2906 it->max_phys_ascent = it->max_phys_descent = 0;
2907
2908 it->current_y = first_y;
2909 it->vpos = 0;
2910 it->current_x = it->hpos = 0;
2911 }
2912 }
2913 }
2914
2915
2916 /* Return 1 if POS is a position in ellipses displayed for invisible
2917 text. W is the window we display, for text property lookup. */
2918
2919 static int
2920 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2921 {
2922 Lisp_Object prop, window;
2923 int ellipses_p = 0;
2924 EMACS_INT charpos = CHARPOS (pos->pos);
2925
2926 /* If POS specifies a position in a display vector, this might
2927 be for an ellipsis displayed for invisible text. We won't
2928 get the iterator set up for delivering that ellipsis unless
2929 we make sure that it gets aware of the invisible text. */
2930 if (pos->dpvec_index >= 0
2931 && pos->overlay_string_index < 0
2932 && CHARPOS (pos->string_pos) < 0
2933 && charpos > BEGV
2934 && (XSETWINDOW (window, w),
2935 prop = Fget_char_property (make_number (charpos),
2936 Qinvisible, window),
2937 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2938 {
2939 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2940 window);
2941 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2942 }
2943
2944 return ellipses_p;
2945 }
2946
2947
2948 /* Initialize IT for stepping through current_buffer in window W,
2949 starting at position POS that includes overlay string and display
2950 vector/ control character translation position information. Value
2951 is zero if there are overlay strings with newlines at POS. */
2952
2953 static int
2954 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2955 {
2956 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2957 int i, overlay_strings_with_newlines = 0;
2958
2959 /* If POS specifies a position in a display vector, this might
2960 be for an ellipsis displayed for invisible text. We won't
2961 get the iterator set up for delivering that ellipsis unless
2962 we make sure that it gets aware of the invisible text. */
2963 if (in_ellipses_for_invisible_text_p (pos, w))
2964 {
2965 --charpos;
2966 bytepos = 0;
2967 }
2968
2969 /* Keep in mind: the call to reseat in init_iterator skips invisible
2970 text, so we might end up at a position different from POS. This
2971 is only a problem when POS is a row start after a newline and an
2972 overlay starts there with an after-string, and the overlay has an
2973 invisible property. Since we don't skip invisible text in
2974 display_line and elsewhere immediately after consuming the
2975 newline before the row start, such a POS will not be in a string,
2976 but the call to init_iterator below will move us to the
2977 after-string. */
2978 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2979
2980 /* This only scans the current chunk -- it should scan all chunks.
2981 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2982 to 16 in 22.1 to make this a lesser problem. */
2983 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2984 {
2985 const char *s = SSDATA (it->overlay_strings[i]);
2986 const char *e = s + SBYTES (it->overlay_strings[i]);
2987
2988 while (s < e && *s != '\n')
2989 ++s;
2990
2991 if (s < e)
2992 {
2993 overlay_strings_with_newlines = 1;
2994 break;
2995 }
2996 }
2997
2998 /* If position is within an overlay string, set up IT to the right
2999 overlay string. */
3000 if (pos->overlay_string_index >= 0)
3001 {
3002 int relative_index;
3003
3004 /* If the first overlay string happens to have a `display'
3005 property for an image, the iterator will be set up for that
3006 image, and we have to undo that setup first before we can
3007 correct the overlay string index. */
3008 if (it->method == GET_FROM_IMAGE)
3009 pop_it (it);
3010
3011 /* We already have the first chunk of overlay strings in
3012 IT->overlay_strings. Load more until the one for
3013 pos->overlay_string_index is in IT->overlay_strings. */
3014 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3015 {
3016 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3017 it->current.overlay_string_index = 0;
3018 while (n--)
3019 {
3020 load_overlay_strings (it, 0);
3021 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3022 }
3023 }
3024
3025 it->current.overlay_string_index = pos->overlay_string_index;
3026 relative_index = (it->current.overlay_string_index
3027 % OVERLAY_STRING_CHUNK_SIZE);
3028 it->string = it->overlay_strings[relative_index];
3029 xassert (STRINGP (it->string));
3030 it->current.string_pos = pos->string_pos;
3031 it->method = GET_FROM_STRING;
3032 }
3033
3034 if (CHARPOS (pos->string_pos) >= 0)
3035 {
3036 /* Recorded position is not in an overlay string, but in another
3037 string. This can only be a string from a `display' property.
3038 IT should already be filled with that string. */
3039 it->current.string_pos = pos->string_pos;
3040 xassert (STRINGP (it->string));
3041 }
3042
3043 /* Restore position in display vector translations, control
3044 character translations or ellipses. */
3045 if (pos->dpvec_index >= 0)
3046 {
3047 if (it->dpvec == NULL)
3048 get_next_display_element (it);
3049 xassert (it->dpvec && it->current.dpvec_index == 0);
3050 it->current.dpvec_index = pos->dpvec_index;
3051 }
3052
3053 CHECK_IT (it);
3054 return !overlay_strings_with_newlines;
3055 }
3056
3057
3058 /* Initialize IT for stepping through current_buffer in window W
3059 starting at ROW->start. */
3060
3061 static void
3062 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3063 {
3064 init_from_display_pos (it, w, &row->start);
3065 it->start = row->start;
3066 it->continuation_lines_width = row->continuation_lines_width;
3067 CHECK_IT (it);
3068 }
3069
3070
3071 /* Initialize IT for stepping through current_buffer in window W
3072 starting in the line following ROW, i.e. starting at ROW->end.
3073 Value is zero if there are overlay strings with newlines at ROW's
3074 end position. */
3075
3076 static int
3077 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3078 {
3079 int success = 0;
3080
3081 if (init_from_display_pos (it, w, &row->end))
3082 {
3083 if (row->continued_p)
3084 it->continuation_lines_width
3085 = row->continuation_lines_width + row->pixel_width;
3086 CHECK_IT (it);
3087 success = 1;
3088 }
3089
3090 return success;
3091 }
3092
3093
3094
3095 \f
3096 /***********************************************************************
3097 Text properties
3098 ***********************************************************************/
3099
3100 /* Called when IT reaches IT->stop_charpos. Handle text property and
3101 overlay changes. Set IT->stop_charpos to the next position where
3102 to stop. */
3103
3104 static void
3105 handle_stop (struct it *it)
3106 {
3107 enum prop_handled handled;
3108 int handle_overlay_change_p;
3109 struct props *p;
3110
3111 it->dpvec = NULL;
3112 it->current.dpvec_index = -1;
3113 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3114 it->ignore_overlay_strings_at_pos_p = 0;
3115 it->ellipsis_p = 0;
3116
3117 /* Use face of preceding text for ellipsis (if invisible) */
3118 if (it->selective_display_ellipsis_p)
3119 it->saved_face_id = it->face_id;
3120
3121 do
3122 {
3123 handled = HANDLED_NORMALLY;
3124
3125 /* Call text property handlers. */
3126 for (p = it_props; p->handler; ++p)
3127 {
3128 handled = p->handler (it);
3129
3130 if (handled == HANDLED_RECOMPUTE_PROPS)
3131 break;
3132 else if (handled == HANDLED_RETURN)
3133 {
3134 /* We still want to show before and after strings from
3135 overlays even if the actual buffer text is replaced. */
3136 if (!handle_overlay_change_p
3137 || it->sp > 1
3138 || !get_overlay_strings_1 (it, 0, 0))
3139 {
3140 if (it->ellipsis_p)
3141 setup_for_ellipsis (it, 0);
3142 /* When handling a display spec, we might load an
3143 empty string. In that case, discard it here. We
3144 used to discard it in handle_single_display_spec,
3145 but that causes get_overlay_strings_1, above, to
3146 ignore overlay strings that we must check. */
3147 if (STRINGP (it->string) && !SCHARS (it->string))
3148 pop_it (it);
3149 return;
3150 }
3151 else if (STRINGP (it->string) && !SCHARS (it->string))
3152 pop_it (it);
3153 else
3154 {
3155 it->ignore_overlay_strings_at_pos_p = 1;
3156 it->string_from_display_prop_p = 0;
3157 it->from_disp_prop_p = 0;
3158 handle_overlay_change_p = 0;
3159 }
3160 handled = HANDLED_RECOMPUTE_PROPS;
3161 break;
3162 }
3163 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3164 handle_overlay_change_p = 0;
3165 }
3166
3167 if (handled != HANDLED_RECOMPUTE_PROPS)
3168 {
3169 /* Don't check for overlay strings below when set to deliver
3170 characters from a display vector. */
3171 if (it->method == GET_FROM_DISPLAY_VECTOR)
3172 handle_overlay_change_p = 0;
3173
3174 /* Handle overlay changes.
3175 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3176 if it finds overlays. */
3177 if (handle_overlay_change_p)
3178 handled = handle_overlay_change (it);
3179 }
3180
3181 if (it->ellipsis_p)
3182 {
3183 setup_for_ellipsis (it, 0);
3184 break;
3185 }
3186 }
3187 while (handled == HANDLED_RECOMPUTE_PROPS);
3188
3189 /* Determine where to stop next. */
3190 if (handled == HANDLED_NORMALLY)
3191 compute_stop_pos (it);
3192 }
3193
3194
3195 /* Compute IT->stop_charpos from text property and overlay change
3196 information for IT's current position. */
3197
3198 static void
3199 compute_stop_pos (struct it *it)
3200 {
3201 register INTERVAL iv, next_iv;
3202 Lisp_Object object, limit, position;
3203 EMACS_INT charpos, bytepos;
3204
3205 if (STRINGP (it->string))
3206 {
3207 /* Strings are usually short, so don't limit the search for
3208 properties. */
3209 it->stop_charpos = it->end_charpos;
3210 object = it->string;
3211 limit = Qnil;
3212 charpos = IT_STRING_CHARPOS (*it);
3213 bytepos = IT_STRING_BYTEPOS (*it);
3214 }
3215 else
3216 {
3217 EMACS_INT pos;
3218
3219 /* If end_charpos is out of range for some reason, such as a
3220 misbehaving display function, rationalize it (Bug#5984). */
3221 if (it->end_charpos > ZV)
3222 it->end_charpos = ZV;
3223 it->stop_charpos = it->end_charpos;
3224
3225 /* If next overlay change is in front of the current stop pos
3226 (which is IT->end_charpos), stop there. Note: value of
3227 next_overlay_change is point-max if no overlay change
3228 follows. */
3229 charpos = IT_CHARPOS (*it);
3230 bytepos = IT_BYTEPOS (*it);
3231 pos = next_overlay_change (charpos);
3232 if (pos < it->stop_charpos)
3233 it->stop_charpos = pos;
3234
3235 /* If showing the region, we have to stop at the region
3236 start or end because the face might change there. */
3237 if (it->region_beg_charpos > 0)
3238 {
3239 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3240 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3241 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3242 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3243 }
3244
3245 /* Set up variables for computing the stop position from text
3246 property changes. */
3247 XSETBUFFER (object, current_buffer);
3248 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3249 }
3250
3251 /* Get the interval containing IT's position. Value is a null
3252 interval if there isn't such an interval. */
3253 position = make_number (charpos);
3254 iv = validate_interval_range (object, &position, &position, 0);
3255 if (!NULL_INTERVAL_P (iv))
3256 {
3257 Lisp_Object values_here[LAST_PROP_IDX];
3258 struct props *p;
3259
3260 /* Get properties here. */
3261 for (p = it_props; p->handler; ++p)
3262 values_here[p->idx] = textget (iv->plist, *p->name);
3263
3264 /* Look for an interval following iv that has different
3265 properties. */
3266 for (next_iv = next_interval (iv);
3267 (!NULL_INTERVAL_P (next_iv)
3268 && (NILP (limit)
3269 || XFASTINT (limit) > next_iv->position));
3270 next_iv = next_interval (next_iv))
3271 {
3272 for (p = it_props; p->handler; ++p)
3273 {
3274 Lisp_Object new_value;
3275
3276 new_value = textget (next_iv->plist, *p->name);
3277 if (!EQ (values_here[p->idx], new_value))
3278 break;
3279 }
3280
3281 if (p->handler)
3282 break;
3283 }
3284
3285 if (!NULL_INTERVAL_P (next_iv))
3286 {
3287 if (INTEGERP (limit)
3288 && next_iv->position >= XFASTINT (limit))
3289 /* No text property change up to limit. */
3290 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3291 else
3292 /* Text properties change in next_iv. */
3293 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3294 }
3295 }
3296
3297 if (it->cmp_it.id < 0)
3298 {
3299 EMACS_INT stoppos = it->end_charpos;
3300
3301 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3302 stoppos = -1;
3303 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3304 stoppos, it->string);
3305 }
3306
3307 xassert (STRINGP (it->string)
3308 || (it->stop_charpos >= BEGV
3309 && it->stop_charpos >= IT_CHARPOS (*it)));
3310 }
3311
3312
3313 /* Return the position of the next overlay change after POS in
3314 current_buffer. Value is point-max if no overlay change
3315 follows. This is like `next-overlay-change' but doesn't use
3316 xmalloc. */
3317
3318 static EMACS_INT
3319 next_overlay_change (EMACS_INT pos)
3320 {
3321 ptrdiff_t i, noverlays;
3322 EMACS_INT endpos;
3323 Lisp_Object *overlays;
3324
3325 /* Get all overlays at the given position. */
3326 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3327
3328 /* If any of these overlays ends before endpos,
3329 use its ending point instead. */
3330 for (i = 0; i < noverlays; ++i)
3331 {
3332 Lisp_Object oend;
3333 EMACS_INT oendpos;
3334
3335 oend = OVERLAY_END (overlays[i]);
3336 oendpos = OVERLAY_POSITION (oend);
3337 endpos = min (endpos, oendpos);
3338 }
3339
3340 return endpos;
3341 }
3342
3343 /* How many characters forward to search for a display property or
3344 display string. Searching too far forward makes the bidi display
3345 sluggish, especially in small windows. */
3346 #define MAX_DISP_SCAN 250
3347
3348 /* Return the character position of a display string at or after
3349 position specified by POSITION. If no display string exists at or
3350 after POSITION, return ZV. A display string is either an overlay
3351 with `display' property whose value is a string, or a `display'
3352 text property whose value is a string. STRING is data about the
3353 string to iterate; if STRING->lstring is nil, we are iterating a
3354 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3355 on a GUI frame. DISP_PROP is set to zero if we searched
3356 MAX_DISP_SCAN characters forward without finding any display
3357 strings, non-zero otherwise. It is set to 2 if the display string
3358 uses any kind of `(space ...)' spec that will produce a stretch of
3359 white space in the text area. */
3360 EMACS_INT
3361 compute_display_string_pos (struct text_pos *position,
3362 struct bidi_string_data *string,
3363 int frame_window_p, int *disp_prop)
3364 {
3365 /* OBJECT = nil means current buffer. */
3366 Lisp_Object object =
3367 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3368 Lisp_Object pos, spec, limpos;
3369 int string_p = (string && (STRINGP (string->lstring) || string->s));
3370 EMACS_INT eob = string_p ? string->schars : ZV;
3371 EMACS_INT begb = string_p ? 0 : BEGV;
3372 EMACS_INT bufpos, charpos = CHARPOS (*position);
3373 EMACS_INT lim =
3374 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3375 struct text_pos tpos;
3376 int rv = 0;
3377
3378 *disp_prop = 1;
3379
3380 if (charpos >= eob
3381 /* We don't support display properties whose values are strings
3382 that have display string properties. */
3383 || string->from_disp_str
3384 /* C strings cannot have display properties. */
3385 || (string->s && !STRINGP (object)))
3386 {
3387 *disp_prop = 0;
3388 return eob;
3389 }
3390
3391 /* If the character at CHARPOS is where the display string begins,
3392 return CHARPOS. */
3393 pos = make_number (charpos);
3394 if (STRINGP (object))
3395 bufpos = string->bufpos;
3396 else
3397 bufpos = charpos;
3398 tpos = *position;
3399 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3400 && (charpos <= begb
3401 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3402 object),
3403 spec))
3404 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3405 frame_window_p)))
3406 {
3407 if (rv == 2)
3408 *disp_prop = 2;
3409 return charpos;
3410 }
3411
3412 /* Look forward for the first character with a `display' property
3413 that will replace the underlying text when displayed. */
3414 limpos = make_number (lim);
3415 do {
3416 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3417 CHARPOS (tpos) = XFASTINT (pos);
3418 if (CHARPOS (tpos) >= lim)
3419 {
3420 *disp_prop = 0;
3421 break;
3422 }
3423 if (STRINGP (object))
3424 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3425 else
3426 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3427 spec = Fget_char_property (pos, Qdisplay, object);
3428 if (!STRINGP (object))
3429 bufpos = CHARPOS (tpos);
3430 } while (NILP (spec)
3431 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3432 bufpos, frame_window_p)));
3433 if (rv == 2)
3434 *disp_prop = 2;
3435
3436 return CHARPOS (tpos);
3437 }
3438
3439 /* Return the character position of the end of the display string that
3440 started at CHARPOS. If there's no display string at CHARPOS,
3441 return -1. A display string is either an overlay with `display'
3442 property whose value is a string or a `display' text property whose
3443 value is a string. */
3444 EMACS_INT
3445 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3446 {
3447 /* OBJECT = nil means current buffer. */
3448 Lisp_Object object =
3449 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3450 Lisp_Object pos = make_number (charpos);
3451 EMACS_INT eob =
3452 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3453
3454 if (charpos >= eob || (string->s && !STRINGP (object)))
3455 return eob;
3456
3457 /* It could happen that the display property or overlay was removed
3458 since we found it in compute_display_string_pos above. One way
3459 this can happen is if JIT font-lock was called (through
3460 handle_fontified_prop), and jit-lock-functions remove text
3461 properties or overlays from the portion of buffer that includes
3462 CHARPOS. Muse mode is known to do that, for example. In this
3463 case, we return -1 to the caller, to signal that no display
3464 string is actually present at CHARPOS. See bidi_fetch_char for
3465 how this is handled.
3466
3467 An alternative would be to never look for display properties past
3468 it->stop_charpos. But neither compute_display_string_pos nor
3469 bidi_fetch_char that calls it know or care where the next
3470 stop_charpos is. */
3471 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3472 return -1;
3473
3474 /* Look forward for the first character where the `display' property
3475 changes. */
3476 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3477
3478 return XFASTINT (pos);
3479 }
3480
3481
3482 \f
3483 /***********************************************************************
3484 Fontification
3485 ***********************************************************************/
3486
3487 /* Handle changes in the `fontified' property of the current buffer by
3488 calling hook functions from Qfontification_functions to fontify
3489 regions of text. */
3490
3491 static enum prop_handled
3492 handle_fontified_prop (struct it *it)
3493 {
3494 Lisp_Object prop, pos;
3495 enum prop_handled handled = HANDLED_NORMALLY;
3496
3497 if (!NILP (Vmemory_full))
3498 return handled;
3499
3500 /* Get the value of the `fontified' property at IT's current buffer
3501 position. (The `fontified' property doesn't have a special
3502 meaning in strings.) If the value is nil, call functions from
3503 Qfontification_functions. */
3504 if (!STRINGP (it->string)
3505 && it->s == NULL
3506 && !NILP (Vfontification_functions)
3507 && !NILP (Vrun_hooks)
3508 && (pos = make_number (IT_CHARPOS (*it)),
3509 prop = Fget_char_property (pos, Qfontified, Qnil),
3510 /* Ignore the special cased nil value always present at EOB since
3511 no amount of fontifying will be able to change it. */
3512 NILP (prop) && IT_CHARPOS (*it) < Z))
3513 {
3514 int count = SPECPDL_INDEX ();
3515 Lisp_Object val;
3516 struct buffer *obuf = current_buffer;
3517 int begv = BEGV, zv = ZV;
3518 int old_clip_changed = current_buffer->clip_changed;
3519
3520 val = Vfontification_functions;
3521 specbind (Qfontification_functions, Qnil);
3522
3523 xassert (it->end_charpos == ZV);
3524
3525 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3526 safe_call1 (val, pos);
3527 else
3528 {
3529 Lisp_Object fns, fn;
3530 struct gcpro gcpro1, gcpro2;
3531
3532 fns = Qnil;
3533 GCPRO2 (val, fns);
3534
3535 for (; CONSP (val); val = XCDR (val))
3536 {
3537 fn = XCAR (val);
3538
3539 if (EQ (fn, Qt))
3540 {
3541 /* A value of t indicates this hook has a local
3542 binding; it means to run the global binding too.
3543 In a global value, t should not occur. If it
3544 does, we must ignore it to avoid an endless
3545 loop. */
3546 for (fns = Fdefault_value (Qfontification_functions);
3547 CONSP (fns);
3548 fns = XCDR (fns))
3549 {
3550 fn = XCAR (fns);
3551 if (!EQ (fn, Qt))
3552 safe_call1 (fn, pos);
3553 }
3554 }
3555 else
3556 safe_call1 (fn, pos);
3557 }
3558
3559 UNGCPRO;
3560 }
3561
3562 unbind_to (count, Qnil);
3563
3564 /* Fontification functions routinely call `save-restriction'.
3565 Normally, this tags clip_changed, which can confuse redisplay
3566 (see discussion in Bug#6671). Since we don't perform any
3567 special handling of fontification changes in the case where
3568 `save-restriction' isn't called, there's no point doing so in
3569 this case either. So, if the buffer's restrictions are
3570 actually left unchanged, reset clip_changed. */
3571 if (obuf == current_buffer)
3572 {
3573 if (begv == BEGV && zv == ZV)
3574 current_buffer->clip_changed = old_clip_changed;
3575 }
3576 /* There isn't much we can reasonably do to protect against
3577 misbehaving fontification, but here's a fig leaf. */
3578 else if (!NILP (BVAR (obuf, name)))
3579 set_buffer_internal_1 (obuf);
3580
3581 /* The fontification code may have added/removed text.
3582 It could do even a lot worse, but let's at least protect against
3583 the most obvious case where only the text past `pos' gets changed',
3584 as is/was done in grep.el where some escapes sequences are turned
3585 into face properties (bug#7876). */
3586 it->end_charpos = ZV;
3587
3588 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3589 something. This avoids an endless loop if they failed to
3590 fontify the text for which reason ever. */
3591 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3592 handled = HANDLED_RECOMPUTE_PROPS;
3593 }
3594
3595 return handled;
3596 }
3597
3598
3599 \f
3600 /***********************************************************************
3601 Faces
3602 ***********************************************************************/
3603
3604 /* Set up iterator IT from face properties at its current position.
3605 Called from handle_stop. */
3606
3607 static enum prop_handled
3608 handle_face_prop (struct it *it)
3609 {
3610 int new_face_id;
3611 EMACS_INT next_stop;
3612
3613 if (!STRINGP (it->string))
3614 {
3615 new_face_id
3616 = face_at_buffer_position (it->w,
3617 IT_CHARPOS (*it),
3618 it->region_beg_charpos,
3619 it->region_end_charpos,
3620 &next_stop,
3621 (IT_CHARPOS (*it)
3622 + TEXT_PROP_DISTANCE_LIMIT),
3623 0, it->base_face_id);
3624
3625 /* Is this a start of a run of characters with box face?
3626 Caveat: this can be called for a freshly initialized
3627 iterator; face_id is -1 in this case. We know that the new
3628 face will not change until limit, i.e. if the new face has a
3629 box, all characters up to limit will have one. But, as
3630 usual, we don't know whether limit is really the end. */
3631 if (new_face_id != it->face_id)
3632 {
3633 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3634
3635 /* If new face has a box but old face has not, this is
3636 the start of a run of characters with box, i.e. it has
3637 a shadow on the left side. The value of face_id of the
3638 iterator will be -1 if this is the initial call that gets
3639 the face. In this case, we have to look in front of IT's
3640 position and see whether there is a face != new_face_id. */
3641 it->start_of_box_run_p
3642 = (new_face->box != FACE_NO_BOX
3643 && (it->face_id >= 0
3644 || IT_CHARPOS (*it) == BEG
3645 || new_face_id != face_before_it_pos (it)));
3646 it->face_box_p = new_face->box != FACE_NO_BOX;
3647 }
3648 }
3649 else
3650 {
3651 int base_face_id;
3652 EMACS_INT bufpos;
3653 int i;
3654 Lisp_Object from_overlay
3655 = (it->current.overlay_string_index >= 0
3656 ? it->string_overlays[it->current.overlay_string_index]
3657 : Qnil);
3658
3659 /* See if we got to this string directly or indirectly from
3660 an overlay property. That includes the before-string or
3661 after-string of an overlay, strings in display properties
3662 provided by an overlay, their text properties, etc.
3663
3664 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3665 if (! NILP (from_overlay))
3666 for (i = it->sp - 1; i >= 0; i--)
3667 {
3668 if (it->stack[i].current.overlay_string_index >= 0)
3669 from_overlay
3670 = it->string_overlays[it->stack[i].current.overlay_string_index];
3671 else if (! NILP (it->stack[i].from_overlay))
3672 from_overlay = it->stack[i].from_overlay;
3673
3674 if (!NILP (from_overlay))
3675 break;
3676 }
3677
3678 if (! NILP (from_overlay))
3679 {
3680 bufpos = IT_CHARPOS (*it);
3681 /* For a string from an overlay, the base face depends
3682 only on text properties and ignores overlays. */
3683 base_face_id
3684 = face_for_overlay_string (it->w,
3685 IT_CHARPOS (*it),
3686 it->region_beg_charpos,
3687 it->region_end_charpos,
3688 &next_stop,
3689 (IT_CHARPOS (*it)
3690 + TEXT_PROP_DISTANCE_LIMIT),
3691 0,
3692 from_overlay);
3693 }
3694 else
3695 {
3696 bufpos = 0;
3697
3698 /* For strings from a `display' property, use the face at
3699 IT's current buffer position as the base face to merge
3700 with, so that overlay strings appear in the same face as
3701 surrounding text, unless they specify their own
3702 faces. */
3703 base_face_id = it->string_from_prefix_prop_p
3704 ? DEFAULT_FACE_ID
3705 : underlying_face_id (it);
3706 }
3707
3708 new_face_id = face_at_string_position (it->w,
3709 it->string,
3710 IT_STRING_CHARPOS (*it),
3711 bufpos,
3712 it->region_beg_charpos,
3713 it->region_end_charpos,
3714 &next_stop,
3715 base_face_id, 0);
3716
3717 /* Is this a start of a run of characters with box? Caveat:
3718 this can be called for a freshly allocated iterator; face_id
3719 is -1 is this case. We know that the new face will not
3720 change until the next check pos, i.e. if the new face has a
3721 box, all characters up to that position will have a
3722 box. But, as usual, we don't know whether that position
3723 is really the end. */
3724 if (new_face_id != it->face_id)
3725 {
3726 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3727 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3728
3729 /* If new face has a box but old face hasn't, this is the
3730 start of a run of characters with box, i.e. it has a
3731 shadow on the left side. */
3732 it->start_of_box_run_p
3733 = new_face->box && (old_face == NULL || !old_face->box);
3734 it->face_box_p = new_face->box != FACE_NO_BOX;
3735 }
3736 }
3737
3738 it->face_id = new_face_id;
3739 return HANDLED_NORMALLY;
3740 }
3741
3742
3743 /* Return the ID of the face ``underlying'' IT's current position,
3744 which is in a string. If the iterator is associated with a
3745 buffer, return the face at IT's current buffer position.
3746 Otherwise, use the iterator's base_face_id. */
3747
3748 static int
3749 underlying_face_id (struct it *it)
3750 {
3751 int face_id = it->base_face_id, i;
3752
3753 xassert (STRINGP (it->string));
3754
3755 for (i = it->sp - 1; i >= 0; --i)
3756 if (NILP (it->stack[i].string))
3757 face_id = it->stack[i].face_id;
3758
3759 return face_id;
3760 }
3761
3762
3763 /* Compute the face one character before or after the current position
3764 of IT, in the visual order. BEFORE_P non-zero means get the face
3765 in front (to the left in L2R paragraphs, to the right in R2L
3766 paragraphs) of IT's screen position. Value is the ID of the face. */
3767
3768 static int
3769 face_before_or_after_it_pos (struct it *it, int before_p)
3770 {
3771 int face_id, limit;
3772 EMACS_INT next_check_charpos;
3773 struct it it_copy;
3774 void *it_copy_data = NULL;
3775
3776 xassert (it->s == NULL);
3777
3778 if (STRINGP (it->string))
3779 {
3780 EMACS_INT bufpos, charpos;
3781 int base_face_id;
3782
3783 /* No face change past the end of the string (for the case
3784 we are padding with spaces). No face change before the
3785 string start. */
3786 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3787 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3788 return it->face_id;
3789
3790 if (!it->bidi_p)
3791 {
3792 /* Set charpos to the position before or after IT's current
3793 position, in the logical order, which in the non-bidi
3794 case is the same as the visual order. */
3795 if (before_p)
3796 charpos = IT_STRING_CHARPOS (*it) - 1;
3797 else if (it->what == IT_COMPOSITION)
3798 /* For composition, we must check the character after the
3799 composition. */
3800 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3801 else
3802 charpos = IT_STRING_CHARPOS (*it) + 1;
3803 }
3804 else
3805 {
3806 if (before_p)
3807 {
3808 /* With bidi iteration, the character before the current
3809 in the visual order cannot be found by simple
3810 iteration, because "reverse" reordering is not
3811 supported. Instead, we need to use the move_it_*
3812 family of functions. */
3813 /* Ignore face changes before the first visible
3814 character on this display line. */
3815 if (it->current_x <= it->first_visible_x)
3816 return it->face_id;
3817 SAVE_IT (it_copy, *it, it_copy_data);
3818 /* Implementation note: Since move_it_in_display_line
3819 works in the iterator geometry, and thinks the first
3820 character is always the leftmost, even in R2L lines,
3821 we don't need to distinguish between the R2L and L2R
3822 cases here. */
3823 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3824 it_copy.current_x - 1, MOVE_TO_X);
3825 charpos = IT_STRING_CHARPOS (it_copy);
3826 RESTORE_IT (it, it, it_copy_data);
3827 }
3828 else
3829 {
3830 /* Set charpos to the string position of the character
3831 that comes after IT's current position in the visual
3832 order. */
3833 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3834
3835 it_copy = *it;
3836 while (n--)
3837 bidi_move_to_visually_next (&it_copy.bidi_it);
3838
3839 charpos = it_copy.bidi_it.charpos;
3840 }
3841 }
3842 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3843
3844 if (it->current.overlay_string_index >= 0)
3845 bufpos = IT_CHARPOS (*it);
3846 else
3847 bufpos = 0;
3848
3849 base_face_id = underlying_face_id (it);
3850
3851 /* Get the face for ASCII, or unibyte. */
3852 face_id = face_at_string_position (it->w,
3853 it->string,
3854 charpos,
3855 bufpos,
3856 it->region_beg_charpos,
3857 it->region_end_charpos,
3858 &next_check_charpos,
3859 base_face_id, 0);
3860
3861 /* Correct the face for charsets different from ASCII. Do it
3862 for the multibyte case only. The face returned above is
3863 suitable for unibyte text if IT->string is unibyte. */
3864 if (STRING_MULTIBYTE (it->string))
3865 {
3866 struct text_pos pos1 = string_pos (charpos, it->string);
3867 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3868 int c, len;
3869 struct face *face = FACE_FROM_ID (it->f, face_id);
3870
3871 c = string_char_and_length (p, &len);
3872 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3873 }
3874 }
3875 else
3876 {
3877 struct text_pos pos;
3878
3879 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3880 || (IT_CHARPOS (*it) <= BEGV && before_p))
3881 return it->face_id;
3882
3883 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3884 pos = it->current.pos;
3885
3886 if (!it->bidi_p)
3887 {
3888 if (before_p)
3889 DEC_TEXT_POS (pos, it->multibyte_p);
3890 else
3891 {
3892 if (it->what == IT_COMPOSITION)
3893 {
3894 /* For composition, we must check the position after
3895 the composition. */
3896 pos.charpos += it->cmp_it.nchars;
3897 pos.bytepos += it->len;
3898 }
3899 else
3900 INC_TEXT_POS (pos, it->multibyte_p);
3901 }
3902 }
3903 else
3904 {
3905 if (before_p)
3906 {
3907 /* With bidi iteration, the character before the current
3908 in the visual order cannot be found by simple
3909 iteration, because "reverse" reordering is not
3910 supported. Instead, we need to use the move_it_*
3911 family of functions. */
3912 /* Ignore face changes before the first visible
3913 character on this display line. */
3914 if (it->current_x <= it->first_visible_x)
3915 return it->face_id;
3916 SAVE_IT (it_copy, *it, it_copy_data);
3917 /* Implementation note: Since move_it_in_display_line
3918 works in the iterator geometry, and thinks the first
3919 character is always the leftmost, even in R2L lines,
3920 we don't need to distinguish between the R2L and L2R
3921 cases here. */
3922 move_it_in_display_line (&it_copy, ZV,
3923 it_copy.current_x - 1, MOVE_TO_X);
3924 pos = it_copy.current.pos;
3925 RESTORE_IT (it, it, it_copy_data);
3926 }
3927 else
3928 {
3929 /* Set charpos to the buffer position of the character
3930 that comes after IT's current position in the visual
3931 order. */
3932 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3933
3934 it_copy = *it;
3935 while (n--)
3936 bidi_move_to_visually_next (&it_copy.bidi_it);
3937
3938 SET_TEXT_POS (pos,
3939 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3940 }
3941 }
3942 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3943
3944 /* Determine face for CHARSET_ASCII, or unibyte. */
3945 face_id = face_at_buffer_position (it->w,
3946 CHARPOS (pos),
3947 it->region_beg_charpos,
3948 it->region_end_charpos,
3949 &next_check_charpos,
3950 limit, 0, -1);
3951
3952 /* Correct the face for charsets different from ASCII. Do it
3953 for the multibyte case only. The face returned above is
3954 suitable for unibyte text if current_buffer is unibyte. */
3955 if (it->multibyte_p)
3956 {
3957 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3958 struct face *face = FACE_FROM_ID (it->f, face_id);
3959 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3960 }
3961 }
3962
3963 return face_id;
3964 }
3965
3966
3967 \f
3968 /***********************************************************************
3969 Invisible text
3970 ***********************************************************************/
3971
3972 /* Set up iterator IT from invisible properties at its current
3973 position. Called from handle_stop. */
3974
3975 static enum prop_handled
3976 handle_invisible_prop (struct it *it)
3977 {
3978 enum prop_handled handled = HANDLED_NORMALLY;
3979
3980 if (STRINGP (it->string))
3981 {
3982 Lisp_Object prop, end_charpos, limit, charpos;
3983
3984 /* Get the value of the invisible text property at the
3985 current position. Value will be nil if there is no such
3986 property. */
3987 charpos = make_number (IT_STRING_CHARPOS (*it));
3988 prop = Fget_text_property (charpos, Qinvisible, it->string);
3989
3990 if (!NILP (prop)
3991 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3992 {
3993 EMACS_INT endpos;
3994
3995 handled = HANDLED_RECOMPUTE_PROPS;
3996
3997 /* Get the position at which the next change of the
3998 invisible text property can be found in IT->string.
3999 Value will be nil if the property value is the same for
4000 all the rest of IT->string. */
4001 XSETINT (limit, SCHARS (it->string));
4002 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4003 it->string, limit);
4004
4005 /* Text at current position is invisible. The next
4006 change in the property is at position end_charpos.
4007 Move IT's current position to that position. */
4008 if (INTEGERP (end_charpos)
4009 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4010 {
4011 struct text_pos old;
4012 EMACS_INT oldpos;
4013
4014 old = it->current.string_pos;
4015 oldpos = CHARPOS (old);
4016 if (it->bidi_p)
4017 {
4018 if (it->bidi_it.first_elt
4019 && it->bidi_it.charpos < SCHARS (it->string))
4020 bidi_paragraph_init (it->paragraph_embedding,
4021 &it->bidi_it, 1);
4022 /* Bidi-iterate out of the invisible text. */
4023 do
4024 {
4025 bidi_move_to_visually_next (&it->bidi_it);
4026 }
4027 while (oldpos <= it->bidi_it.charpos
4028 && it->bidi_it.charpos < endpos);
4029
4030 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4031 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4032 if (IT_CHARPOS (*it) >= endpos)
4033 it->prev_stop = endpos;
4034 }
4035 else
4036 {
4037 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4038 compute_string_pos (&it->current.string_pos, old, it->string);
4039 }
4040 }
4041 else
4042 {
4043 /* The rest of the string is invisible. If this is an
4044 overlay string, proceed with the next overlay string
4045 or whatever comes and return a character from there. */
4046 if (it->current.overlay_string_index >= 0)
4047 {
4048 next_overlay_string (it);
4049 /* Don't check for overlay strings when we just
4050 finished processing them. */
4051 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4052 }
4053 else
4054 {
4055 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4056 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4057 }
4058 }
4059 }
4060 }
4061 else
4062 {
4063 int invis_p;
4064 EMACS_INT newpos, next_stop, start_charpos, tem;
4065 Lisp_Object pos, prop, overlay;
4066
4067 /* First of all, is there invisible text at this position? */
4068 tem = start_charpos = IT_CHARPOS (*it);
4069 pos = make_number (tem);
4070 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4071 &overlay);
4072 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4073
4074 /* If we are on invisible text, skip over it. */
4075 if (invis_p && start_charpos < it->end_charpos)
4076 {
4077 /* Record whether we have to display an ellipsis for the
4078 invisible text. */
4079 int display_ellipsis_p = invis_p == 2;
4080
4081 handled = HANDLED_RECOMPUTE_PROPS;
4082
4083 /* Loop skipping over invisible text. The loop is left at
4084 ZV or with IT on the first char being visible again. */
4085 do
4086 {
4087 /* Try to skip some invisible text. Return value is the
4088 position reached which can be equal to where we start
4089 if there is nothing invisible there. This skips both
4090 over invisible text properties and overlays with
4091 invisible property. */
4092 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4093
4094 /* If we skipped nothing at all we weren't at invisible
4095 text in the first place. If everything to the end of
4096 the buffer was skipped, end the loop. */
4097 if (newpos == tem || newpos >= ZV)
4098 invis_p = 0;
4099 else
4100 {
4101 /* We skipped some characters but not necessarily
4102 all there are. Check if we ended up on visible
4103 text. Fget_char_property returns the property of
4104 the char before the given position, i.e. if we
4105 get invis_p = 0, this means that the char at
4106 newpos is visible. */
4107 pos = make_number (newpos);
4108 prop = Fget_char_property (pos, Qinvisible, it->window);
4109 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4110 }
4111
4112 /* If we ended up on invisible text, proceed to
4113 skip starting with next_stop. */
4114 if (invis_p)
4115 tem = next_stop;
4116
4117 /* If there are adjacent invisible texts, don't lose the
4118 second one's ellipsis. */
4119 if (invis_p == 2)
4120 display_ellipsis_p = 1;
4121 }
4122 while (invis_p);
4123
4124 /* The position newpos is now either ZV or on visible text. */
4125 if (it->bidi_p)
4126 {
4127 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4128 int on_newline =
4129 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4130 int after_newline =
4131 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4132
4133 /* If the invisible text ends on a newline or on a
4134 character after a newline, we can avoid the costly,
4135 character by character, bidi iteration to NEWPOS, and
4136 instead simply reseat the iterator there. That's
4137 because all bidi reordering information is tossed at
4138 the newline. This is a big win for modes that hide
4139 complete lines, like Outline, Org, etc. */
4140 if (on_newline || after_newline)
4141 {
4142 struct text_pos tpos;
4143 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4144
4145 SET_TEXT_POS (tpos, newpos, bpos);
4146 reseat_1 (it, tpos, 0);
4147 /* If we reseat on a newline/ZV, we need to prep the
4148 bidi iterator for advancing to the next character
4149 after the newline/EOB, keeping the current paragraph
4150 direction (so that PRODUCE_GLYPHS does TRT wrt
4151 prepending/appending glyphs to a glyph row). */
4152 if (on_newline)
4153 {
4154 it->bidi_it.first_elt = 0;
4155 it->bidi_it.paragraph_dir = pdir;
4156 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4157 it->bidi_it.nchars = 1;
4158 it->bidi_it.ch_len = 1;
4159 }
4160 }
4161 else /* Must use the slow method. */
4162 {
4163 /* With bidi iteration, the region of invisible text
4164 could start and/or end in the middle of a
4165 non-base embedding level. Therefore, we need to
4166 skip invisible text using the bidi iterator,
4167 starting at IT's current position, until we find
4168 ourselves outside of the invisible text.
4169 Skipping invisible text _after_ bidi iteration
4170 avoids affecting the visual order of the
4171 displayed text when invisible properties are
4172 added or removed. */
4173 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4174 {
4175 /* If we were `reseat'ed to a new paragraph,
4176 determine the paragraph base direction. We
4177 need to do it now because
4178 next_element_from_buffer may not have a
4179 chance to do it, if we are going to skip any
4180 text at the beginning, which resets the
4181 FIRST_ELT flag. */
4182 bidi_paragraph_init (it->paragraph_embedding,
4183 &it->bidi_it, 1);
4184 }
4185 do
4186 {
4187 bidi_move_to_visually_next (&it->bidi_it);
4188 }
4189 while (it->stop_charpos <= it->bidi_it.charpos
4190 && it->bidi_it.charpos < newpos);
4191 IT_CHARPOS (*it) = it->bidi_it.charpos;
4192 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4193 /* If we overstepped NEWPOS, record its position in
4194 the iterator, so that we skip invisible text if
4195 later the bidi iteration lands us in the
4196 invisible region again. */
4197 if (IT_CHARPOS (*it) >= newpos)
4198 it->prev_stop = newpos;
4199 }
4200 }
4201 else
4202 {
4203 IT_CHARPOS (*it) = newpos;
4204 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4205 }
4206
4207 /* If there are before-strings at the start of invisible
4208 text, and the text is invisible because of a text
4209 property, arrange to show before-strings because 20.x did
4210 it that way. (If the text is invisible because of an
4211 overlay property instead of a text property, this is
4212 already handled in the overlay code.) */
4213 if (NILP (overlay)
4214 && get_overlay_strings (it, it->stop_charpos))
4215 {
4216 handled = HANDLED_RECOMPUTE_PROPS;
4217 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4218 }
4219 else if (display_ellipsis_p)
4220 {
4221 /* Make sure that the glyphs of the ellipsis will get
4222 correct `charpos' values. If we would not update
4223 it->position here, the glyphs would belong to the
4224 last visible character _before_ the invisible
4225 text, which confuses `set_cursor_from_row'.
4226
4227 We use the last invisible position instead of the
4228 first because this way the cursor is always drawn on
4229 the first "." of the ellipsis, whenever PT is inside
4230 the invisible text. Otherwise the cursor would be
4231 placed _after_ the ellipsis when the point is after the
4232 first invisible character. */
4233 if (!STRINGP (it->object))
4234 {
4235 it->position.charpos = newpos - 1;
4236 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4237 }
4238 it->ellipsis_p = 1;
4239 /* Let the ellipsis display before
4240 considering any properties of the following char.
4241 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4242 handled = HANDLED_RETURN;
4243 }
4244 }
4245 }
4246
4247 return handled;
4248 }
4249
4250
4251 /* Make iterator IT return `...' next.
4252 Replaces LEN characters from buffer. */
4253
4254 static void
4255 setup_for_ellipsis (struct it *it, int len)
4256 {
4257 /* Use the display table definition for `...'. Invalid glyphs
4258 will be handled by the method returning elements from dpvec. */
4259 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4260 {
4261 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4262 it->dpvec = v->contents;
4263 it->dpend = v->contents + v->header.size;
4264 }
4265 else
4266 {
4267 /* Default `...'. */
4268 it->dpvec = default_invis_vector;
4269 it->dpend = default_invis_vector + 3;
4270 }
4271
4272 it->dpvec_char_len = len;
4273 it->current.dpvec_index = 0;
4274 it->dpvec_face_id = -1;
4275
4276 /* Remember the current face id in case glyphs specify faces.
4277 IT's face is restored in set_iterator_to_next.
4278 saved_face_id was set to preceding char's face in handle_stop. */
4279 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4280 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4281
4282 it->method = GET_FROM_DISPLAY_VECTOR;
4283 it->ellipsis_p = 1;
4284 }
4285
4286
4287 \f
4288 /***********************************************************************
4289 'display' property
4290 ***********************************************************************/
4291
4292 /* Set up iterator IT from `display' property at its current position.
4293 Called from handle_stop.
4294 We return HANDLED_RETURN if some part of the display property
4295 overrides the display of the buffer text itself.
4296 Otherwise we return HANDLED_NORMALLY. */
4297
4298 static enum prop_handled
4299 handle_display_prop (struct it *it)
4300 {
4301 Lisp_Object propval, object, overlay;
4302 struct text_pos *position;
4303 EMACS_INT bufpos;
4304 /* Nonzero if some property replaces the display of the text itself. */
4305 int display_replaced_p = 0;
4306
4307 if (STRINGP (it->string))
4308 {
4309 object = it->string;
4310 position = &it->current.string_pos;
4311 bufpos = CHARPOS (it->current.pos);
4312 }
4313 else
4314 {
4315 XSETWINDOW (object, it->w);
4316 position = &it->current.pos;
4317 bufpos = CHARPOS (*position);
4318 }
4319
4320 /* Reset those iterator values set from display property values. */
4321 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4322 it->space_width = Qnil;
4323 it->font_height = Qnil;
4324 it->voffset = 0;
4325
4326 /* We don't support recursive `display' properties, i.e. string
4327 values that have a string `display' property, that have a string
4328 `display' property etc. */
4329 if (!it->string_from_display_prop_p)
4330 it->area = TEXT_AREA;
4331
4332 propval = get_char_property_and_overlay (make_number (position->charpos),
4333 Qdisplay, object, &overlay);
4334 if (NILP (propval))
4335 return HANDLED_NORMALLY;
4336 /* Now OVERLAY is the overlay that gave us this property, or nil
4337 if it was a text property. */
4338
4339 if (!STRINGP (it->string))
4340 object = it->w->buffer;
4341
4342 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4343 position, bufpos,
4344 FRAME_WINDOW_P (it->f));
4345
4346 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4347 }
4348
4349 /* Subroutine of handle_display_prop. Returns non-zero if the display
4350 specification in SPEC is a replacing specification, i.e. it would
4351 replace the text covered by `display' property with something else,
4352 such as an image or a display string. If SPEC includes any kind or
4353 `(space ...) specification, the value is 2; this is used by
4354 compute_display_string_pos, which see.
4355
4356 See handle_single_display_spec for documentation of arguments.
4357 frame_window_p is non-zero if the window being redisplayed is on a
4358 GUI frame; this argument is used only if IT is NULL, see below.
4359
4360 IT can be NULL, if this is called by the bidi reordering code
4361 through compute_display_string_pos, which see. In that case, this
4362 function only examines SPEC, but does not otherwise "handle" it, in
4363 the sense that it doesn't set up members of IT from the display
4364 spec. */
4365 static int
4366 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4367 Lisp_Object overlay, struct text_pos *position,
4368 EMACS_INT bufpos, int frame_window_p)
4369 {
4370 int replacing_p = 0;
4371 int rv;
4372
4373 if (CONSP (spec)
4374 /* Simple specifications. */
4375 && !EQ (XCAR (spec), Qimage)
4376 && !EQ (XCAR (spec), Qspace)
4377 && !EQ (XCAR (spec), Qwhen)
4378 && !EQ (XCAR (spec), Qslice)
4379 && !EQ (XCAR (spec), Qspace_width)
4380 && !EQ (XCAR (spec), Qheight)
4381 && !EQ (XCAR (spec), Qraise)
4382 /* Marginal area specifications. */
4383 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4384 && !EQ (XCAR (spec), Qleft_fringe)
4385 && !EQ (XCAR (spec), Qright_fringe)
4386 && !NILP (XCAR (spec)))
4387 {
4388 for (; CONSP (spec); spec = XCDR (spec))
4389 {
4390 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4391 overlay, position, bufpos,
4392 replacing_p, frame_window_p)))
4393 {
4394 replacing_p = rv;
4395 /* If some text in a string is replaced, `position' no
4396 longer points to the position of `object'. */
4397 if (!it || STRINGP (object))
4398 break;
4399 }
4400 }
4401 }
4402 else if (VECTORP (spec))
4403 {
4404 int i;
4405 for (i = 0; i < ASIZE (spec); ++i)
4406 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4407 overlay, position, bufpos,
4408 replacing_p, frame_window_p)))
4409 {
4410 replacing_p = rv;
4411 /* If some text in a string is replaced, `position' no
4412 longer points to the position of `object'. */
4413 if (!it || STRINGP (object))
4414 break;
4415 }
4416 }
4417 else
4418 {
4419 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4420 position, bufpos, 0,
4421 frame_window_p)))
4422 replacing_p = rv;
4423 }
4424
4425 return replacing_p;
4426 }
4427
4428 /* Value is the position of the end of the `display' property starting
4429 at START_POS in OBJECT. */
4430
4431 static struct text_pos
4432 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4433 {
4434 Lisp_Object end;
4435 struct text_pos end_pos;
4436
4437 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4438 Qdisplay, object, Qnil);
4439 CHARPOS (end_pos) = XFASTINT (end);
4440 if (STRINGP (object))
4441 compute_string_pos (&end_pos, start_pos, it->string);
4442 else
4443 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4444
4445 return end_pos;
4446 }
4447
4448
4449 /* Set up IT from a single `display' property specification SPEC. OBJECT
4450 is the object in which the `display' property was found. *POSITION
4451 is the position in OBJECT at which the `display' property was found.
4452 BUFPOS is the buffer position of OBJECT (different from POSITION if
4453 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4454 previously saw a display specification which already replaced text
4455 display with something else, for example an image; we ignore such
4456 properties after the first one has been processed.
4457
4458 OVERLAY is the overlay this `display' property came from,
4459 or nil if it was a text property.
4460
4461 If SPEC is a `space' or `image' specification, and in some other
4462 cases too, set *POSITION to the position where the `display'
4463 property ends.
4464
4465 If IT is NULL, only examine the property specification in SPEC, but
4466 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4467 is intended to be displayed in a window on a GUI frame.
4468
4469 Value is non-zero if something was found which replaces the display
4470 of buffer or string text. */
4471
4472 static int
4473 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4474 Lisp_Object overlay, struct text_pos *position,
4475 EMACS_INT bufpos, int display_replaced_p,
4476 int frame_window_p)
4477 {
4478 Lisp_Object form;
4479 Lisp_Object location, value;
4480 struct text_pos start_pos = *position;
4481 int valid_p;
4482
4483 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4484 If the result is non-nil, use VALUE instead of SPEC. */
4485 form = Qt;
4486 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4487 {
4488 spec = XCDR (spec);
4489 if (!CONSP (spec))
4490 return 0;
4491 form = XCAR (spec);
4492 spec = XCDR (spec);
4493 }
4494
4495 if (!NILP (form) && !EQ (form, Qt))
4496 {
4497 int count = SPECPDL_INDEX ();
4498 struct gcpro gcpro1;
4499
4500 /* Bind `object' to the object having the `display' property, a
4501 buffer or string. Bind `position' to the position in the
4502 object where the property was found, and `buffer-position'
4503 to the current position in the buffer. */
4504
4505 if (NILP (object))
4506 XSETBUFFER (object, current_buffer);
4507 specbind (Qobject, object);
4508 specbind (Qposition, make_number (CHARPOS (*position)));
4509 specbind (Qbuffer_position, make_number (bufpos));
4510 GCPRO1 (form);
4511 form = safe_eval (form);
4512 UNGCPRO;
4513 unbind_to (count, Qnil);
4514 }
4515
4516 if (NILP (form))
4517 return 0;
4518
4519 /* Handle `(height HEIGHT)' specifications. */
4520 if (CONSP (spec)
4521 && EQ (XCAR (spec), Qheight)
4522 && CONSP (XCDR (spec)))
4523 {
4524 if (it)
4525 {
4526 if (!FRAME_WINDOW_P (it->f))
4527 return 0;
4528
4529 it->font_height = XCAR (XCDR (spec));
4530 if (!NILP (it->font_height))
4531 {
4532 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4533 int new_height = -1;
4534
4535 if (CONSP (it->font_height)
4536 && (EQ (XCAR (it->font_height), Qplus)
4537 || EQ (XCAR (it->font_height), Qminus))
4538 && CONSP (XCDR (it->font_height))
4539 && INTEGERP (XCAR (XCDR (it->font_height))))
4540 {
4541 /* `(+ N)' or `(- N)' where N is an integer. */
4542 int steps = XINT (XCAR (XCDR (it->font_height)));
4543 if (EQ (XCAR (it->font_height), Qplus))
4544 steps = - steps;
4545 it->face_id = smaller_face (it->f, it->face_id, steps);
4546 }
4547 else if (FUNCTIONP (it->font_height))
4548 {
4549 /* Call function with current height as argument.
4550 Value is the new height. */
4551 Lisp_Object height;
4552 height = safe_call1 (it->font_height,
4553 face->lface[LFACE_HEIGHT_INDEX]);
4554 if (NUMBERP (height))
4555 new_height = XFLOATINT (height);
4556 }
4557 else if (NUMBERP (it->font_height))
4558 {
4559 /* Value is a multiple of the canonical char height. */
4560 struct face *f;
4561
4562 f = FACE_FROM_ID (it->f,
4563 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4564 new_height = (XFLOATINT (it->font_height)
4565 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4566 }
4567 else
4568 {
4569 /* Evaluate IT->font_height with `height' bound to the
4570 current specified height to get the new height. */
4571 int count = SPECPDL_INDEX ();
4572
4573 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4574 value = safe_eval (it->font_height);
4575 unbind_to (count, Qnil);
4576
4577 if (NUMBERP (value))
4578 new_height = XFLOATINT (value);
4579 }
4580
4581 if (new_height > 0)
4582 it->face_id = face_with_height (it->f, it->face_id, new_height);
4583 }
4584 }
4585
4586 return 0;
4587 }
4588
4589 /* Handle `(space-width WIDTH)'. */
4590 if (CONSP (spec)
4591 && EQ (XCAR (spec), Qspace_width)
4592 && CONSP (XCDR (spec)))
4593 {
4594 if (it)
4595 {
4596 if (!FRAME_WINDOW_P (it->f))
4597 return 0;
4598
4599 value = XCAR (XCDR (spec));
4600 if (NUMBERP (value) && XFLOATINT (value) > 0)
4601 it->space_width = value;
4602 }
4603
4604 return 0;
4605 }
4606
4607 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4608 if (CONSP (spec)
4609 && EQ (XCAR (spec), Qslice))
4610 {
4611 Lisp_Object tem;
4612
4613 if (it)
4614 {
4615 if (!FRAME_WINDOW_P (it->f))
4616 return 0;
4617
4618 if (tem = XCDR (spec), CONSP (tem))
4619 {
4620 it->slice.x = XCAR (tem);
4621 if (tem = XCDR (tem), CONSP (tem))
4622 {
4623 it->slice.y = XCAR (tem);
4624 if (tem = XCDR (tem), CONSP (tem))
4625 {
4626 it->slice.width = XCAR (tem);
4627 if (tem = XCDR (tem), CONSP (tem))
4628 it->slice.height = XCAR (tem);
4629 }
4630 }
4631 }
4632 }
4633
4634 return 0;
4635 }
4636
4637 /* Handle `(raise FACTOR)'. */
4638 if (CONSP (spec)
4639 && EQ (XCAR (spec), Qraise)
4640 && CONSP (XCDR (spec)))
4641 {
4642 if (it)
4643 {
4644 if (!FRAME_WINDOW_P (it->f))
4645 return 0;
4646
4647 #ifdef HAVE_WINDOW_SYSTEM
4648 value = XCAR (XCDR (spec));
4649 if (NUMBERP (value))
4650 {
4651 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4652 it->voffset = - (XFLOATINT (value)
4653 * (FONT_HEIGHT (face->font)));
4654 }
4655 #endif /* HAVE_WINDOW_SYSTEM */
4656 }
4657
4658 return 0;
4659 }
4660
4661 /* Don't handle the other kinds of display specifications
4662 inside a string that we got from a `display' property. */
4663 if (it && it->string_from_display_prop_p)
4664 return 0;
4665
4666 /* Characters having this form of property are not displayed, so
4667 we have to find the end of the property. */
4668 if (it)
4669 {
4670 start_pos = *position;
4671 *position = display_prop_end (it, object, start_pos);
4672 }
4673 value = Qnil;
4674
4675 /* Stop the scan at that end position--we assume that all
4676 text properties change there. */
4677 if (it)
4678 it->stop_charpos = position->charpos;
4679
4680 /* Handle `(left-fringe BITMAP [FACE])'
4681 and `(right-fringe BITMAP [FACE])'. */
4682 if (CONSP (spec)
4683 && (EQ (XCAR (spec), Qleft_fringe)
4684 || EQ (XCAR (spec), Qright_fringe))
4685 && CONSP (XCDR (spec)))
4686 {
4687 int fringe_bitmap;
4688
4689 if (it)
4690 {
4691 if (!FRAME_WINDOW_P (it->f))
4692 /* If we return here, POSITION has been advanced
4693 across the text with this property. */
4694 return 0;
4695 }
4696 else if (!frame_window_p)
4697 return 0;
4698
4699 #ifdef HAVE_WINDOW_SYSTEM
4700 value = XCAR (XCDR (spec));
4701 if (!SYMBOLP (value)
4702 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4703 /* If we return here, POSITION has been advanced
4704 across the text with this property. */
4705 return 0;
4706
4707 if (it)
4708 {
4709 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4710
4711 if (CONSP (XCDR (XCDR (spec))))
4712 {
4713 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4714 int face_id2 = lookup_derived_face (it->f, face_name,
4715 FRINGE_FACE_ID, 0);
4716 if (face_id2 >= 0)
4717 face_id = face_id2;
4718 }
4719
4720 /* Save current settings of IT so that we can restore them
4721 when we are finished with the glyph property value. */
4722 push_it (it, position);
4723
4724 it->area = TEXT_AREA;
4725 it->what = IT_IMAGE;
4726 it->image_id = -1; /* no image */
4727 it->position = start_pos;
4728 it->object = NILP (object) ? it->w->buffer : object;
4729 it->method = GET_FROM_IMAGE;
4730 it->from_overlay = Qnil;
4731 it->face_id = face_id;
4732 it->from_disp_prop_p = 1;
4733
4734 /* Say that we haven't consumed the characters with
4735 `display' property yet. The call to pop_it in
4736 set_iterator_to_next will clean this up. */
4737 *position = start_pos;
4738
4739 if (EQ (XCAR (spec), Qleft_fringe))
4740 {
4741 it->left_user_fringe_bitmap = fringe_bitmap;
4742 it->left_user_fringe_face_id = face_id;
4743 }
4744 else
4745 {
4746 it->right_user_fringe_bitmap = fringe_bitmap;
4747 it->right_user_fringe_face_id = face_id;
4748 }
4749 }
4750 #endif /* HAVE_WINDOW_SYSTEM */
4751 return 1;
4752 }
4753
4754 /* Prepare to handle `((margin left-margin) ...)',
4755 `((margin right-margin) ...)' and `((margin nil) ...)'
4756 prefixes for display specifications. */
4757 location = Qunbound;
4758 if (CONSP (spec) && CONSP (XCAR (spec)))
4759 {
4760 Lisp_Object tem;
4761
4762 value = XCDR (spec);
4763 if (CONSP (value))
4764 value = XCAR (value);
4765
4766 tem = XCAR (spec);
4767 if (EQ (XCAR (tem), Qmargin)
4768 && (tem = XCDR (tem),
4769 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4770 (NILP (tem)
4771 || EQ (tem, Qleft_margin)
4772 || EQ (tem, Qright_margin))))
4773 location = tem;
4774 }
4775
4776 if (EQ (location, Qunbound))
4777 {
4778 location = Qnil;
4779 value = spec;
4780 }
4781
4782 /* After this point, VALUE is the property after any
4783 margin prefix has been stripped. It must be a string,
4784 an image specification, or `(space ...)'.
4785
4786 LOCATION specifies where to display: `left-margin',
4787 `right-margin' or nil. */
4788
4789 valid_p = (STRINGP (value)
4790 #ifdef HAVE_WINDOW_SYSTEM
4791 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4792 && valid_image_p (value))
4793 #endif /* not HAVE_WINDOW_SYSTEM */
4794 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4795
4796 if (valid_p && !display_replaced_p)
4797 {
4798 int retval = 1;
4799
4800 if (!it)
4801 {
4802 /* Callers need to know whether the display spec is any kind
4803 of `(space ...)' spec that is about to affect text-area
4804 display. */
4805 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4806 retval = 2;
4807 return retval;
4808 }
4809
4810 /* Save current settings of IT so that we can restore them
4811 when we are finished with the glyph property value. */
4812 push_it (it, position);
4813 it->from_overlay = overlay;
4814 it->from_disp_prop_p = 1;
4815
4816 if (NILP (location))
4817 it->area = TEXT_AREA;
4818 else if (EQ (location, Qleft_margin))
4819 it->area = LEFT_MARGIN_AREA;
4820 else
4821 it->area = RIGHT_MARGIN_AREA;
4822
4823 if (STRINGP (value))
4824 {
4825 it->string = value;
4826 it->multibyte_p = STRING_MULTIBYTE (it->string);
4827 it->current.overlay_string_index = -1;
4828 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4829 it->end_charpos = it->string_nchars = SCHARS (it->string);
4830 it->method = GET_FROM_STRING;
4831 it->stop_charpos = 0;
4832 it->prev_stop = 0;
4833 it->base_level_stop = 0;
4834 it->string_from_display_prop_p = 1;
4835 /* Say that we haven't consumed the characters with
4836 `display' property yet. The call to pop_it in
4837 set_iterator_to_next will clean this up. */
4838 if (BUFFERP (object))
4839 *position = start_pos;
4840
4841 /* Force paragraph direction to be that of the parent
4842 object. If the parent object's paragraph direction is
4843 not yet determined, default to L2R. */
4844 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4845 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4846 else
4847 it->paragraph_embedding = L2R;
4848
4849 /* Set up the bidi iterator for this display string. */
4850 if (it->bidi_p)
4851 {
4852 it->bidi_it.string.lstring = it->string;
4853 it->bidi_it.string.s = NULL;
4854 it->bidi_it.string.schars = it->end_charpos;
4855 it->bidi_it.string.bufpos = bufpos;
4856 it->bidi_it.string.from_disp_str = 1;
4857 it->bidi_it.string.unibyte = !it->multibyte_p;
4858 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4859 }
4860 }
4861 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4862 {
4863 it->method = GET_FROM_STRETCH;
4864 it->object = value;
4865 *position = it->position = start_pos;
4866 retval = 1 + (it->area == TEXT_AREA);
4867 }
4868 #ifdef HAVE_WINDOW_SYSTEM
4869 else
4870 {
4871 it->what = IT_IMAGE;
4872 it->image_id = lookup_image (it->f, value);
4873 it->position = start_pos;
4874 it->object = NILP (object) ? it->w->buffer : object;
4875 it->method = GET_FROM_IMAGE;
4876
4877 /* Say that we haven't consumed the characters with
4878 `display' property yet. The call to pop_it in
4879 set_iterator_to_next will clean this up. */
4880 *position = start_pos;
4881 }
4882 #endif /* HAVE_WINDOW_SYSTEM */
4883
4884 return retval;
4885 }
4886
4887 /* Invalid property or property not supported. Restore
4888 POSITION to what it was before. */
4889 *position = start_pos;
4890 return 0;
4891 }
4892
4893 /* Check if PROP is a display property value whose text should be
4894 treated as intangible. OVERLAY is the overlay from which PROP
4895 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4896 specify the buffer position covered by PROP. */
4897
4898 int
4899 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4900 EMACS_INT charpos, EMACS_INT bytepos)
4901 {
4902 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4903 struct text_pos position;
4904
4905 SET_TEXT_POS (position, charpos, bytepos);
4906 return handle_display_spec (NULL, prop, Qnil, overlay,
4907 &position, charpos, frame_window_p);
4908 }
4909
4910
4911 /* Return 1 if PROP is a display sub-property value containing STRING.
4912
4913 Implementation note: this and the following function are really
4914 special cases of handle_display_spec and
4915 handle_single_display_spec, and should ideally use the same code.
4916 Until they do, these two pairs must be consistent and must be
4917 modified in sync. */
4918
4919 static int
4920 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4921 {
4922 if (EQ (string, prop))
4923 return 1;
4924
4925 /* Skip over `when FORM'. */
4926 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4927 {
4928 prop = XCDR (prop);
4929 if (!CONSP (prop))
4930 return 0;
4931 /* Actually, the condition following `when' should be eval'ed,
4932 like handle_single_display_spec does, and we should return
4933 zero if it evaluates to nil. However, this function is
4934 called only when the buffer was already displayed and some
4935 glyph in the glyph matrix was found to come from a display
4936 string. Therefore, the condition was already evaluated, and
4937 the result was non-nil, otherwise the display string wouldn't
4938 have been displayed and we would have never been called for
4939 this property. Thus, we can skip the evaluation and assume
4940 its result is non-nil. */
4941 prop = XCDR (prop);
4942 }
4943
4944 if (CONSP (prop))
4945 /* Skip over `margin LOCATION'. */
4946 if (EQ (XCAR (prop), Qmargin))
4947 {
4948 prop = XCDR (prop);
4949 if (!CONSP (prop))
4950 return 0;
4951
4952 prop = XCDR (prop);
4953 if (!CONSP (prop))
4954 return 0;
4955 }
4956
4957 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4958 }
4959
4960
4961 /* Return 1 if STRING appears in the `display' property PROP. */
4962
4963 static int
4964 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4965 {
4966 if (CONSP (prop)
4967 && !EQ (XCAR (prop), Qwhen)
4968 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4969 {
4970 /* A list of sub-properties. */
4971 while (CONSP (prop))
4972 {
4973 if (single_display_spec_string_p (XCAR (prop), string))
4974 return 1;
4975 prop = XCDR (prop);
4976 }
4977 }
4978 else if (VECTORP (prop))
4979 {
4980 /* A vector of sub-properties. */
4981 int i;
4982 for (i = 0; i < ASIZE (prop); ++i)
4983 if (single_display_spec_string_p (AREF (prop, i), string))
4984 return 1;
4985 }
4986 else
4987 return single_display_spec_string_p (prop, string);
4988
4989 return 0;
4990 }
4991
4992 /* Look for STRING in overlays and text properties in the current
4993 buffer, between character positions FROM and TO (excluding TO).
4994 BACK_P non-zero means look back (in this case, TO is supposed to be
4995 less than FROM).
4996 Value is the first character position where STRING was found, or
4997 zero if it wasn't found before hitting TO.
4998
4999 This function may only use code that doesn't eval because it is
5000 called asynchronously from note_mouse_highlight. */
5001
5002 static EMACS_INT
5003 string_buffer_position_lim (Lisp_Object string,
5004 EMACS_INT from, EMACS_INT to, int back_p)
5005 {
5006 Lisp_Object limit, prop, pos;
5007 int found = 0;
5008
5009 pos = make_number (max (from, BEGV));
5010
5011 if (!back_p) /* looking forward */
5012 {
5013 limit = make_number (min (to, ZV));
5014 while (!found && !EQ (pos, limit))
5015 {
5016 prop = Fget_char_property (pos, Qdisplay, Qnil);
5017 if (!NILP (prop) && display_prop_string_p (prop, string))
5018 found = 1;
5019 else
5020 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5021 limit);
5022 }
5023 }
5024 else /* looking back */
5025 {
5026 limit = make_number (max (to, BEGV));
5027 while (!found && !EQ (pos, limit))
5028 {
5029 prop = Fget_char_property (pos, Qdisplay, Qnil);
5030 if (!NILP (prop) && display_prop_string_p (prop, string))
5031 found = 1;
5032 else
5033 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5034 limit);
5035 }
5036 }
5037
5038 return found ? XINT (pos) : 0;
5039 }
5040
5041 /* Determine which buffer position in current buffer STRING comes from.
5042 AROUND_CHARPOS is an approximate position where it could come from.
5043 Value is the buffer position or 0 if it couldn't be determined.
5044
5045 This function is necessary because we don't record buffer positions
5046 in glyphs generated from strings (to keep struct glyph small).
5047 This function may only use code that doesn't eval because it is
5048 called asynchronously from note_mouse_highlight. */
5049
5050 static EMACS_INT
5051 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5052 {
5053 const int MAX_DISTANCE = 1000;
5054 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5055 around_charpos + MAX_DISTANCE,
5056 0);
5057
5058 if (!found)
5059 found = string_buffer_position_lim (string, around_charpos,
5060 around_charpos - MAX_DISTANCE, 1);
5061 return found;
5062 }
5063
5064
5065 \f
5066 /***********************************************************************
5067 `composition' property
5068 ***********************************************************************/
5069
5070 /* Set up iterator IT from `composition' property at its current
5071 position. Called from handle_stop. */
5072
5073 static enum prop_handled
5074 handle_composition_prop (struct it *it)
5075 {
5076 Lisp_Object prop, string;
5077 EMACS_INT pos, pos_byte, start, end;
5078
5079 if (STRINGP (it->string))
5080 {
5081 unsigned char *s;
5082
5083 pos = IT_STRING_CHARPOS (*it);
5084 pos_byte = IT_STRING_BYTEPOS (*it);
5085 string = it->string;
5086 s = SDATA (string) + pos_byte;
5087 it->c = STRING_CHAR (s);
5088 }
5089 else
5090 {
5091 pos = IT_CHARPOS (*it);
5092 pos_byte = IT_BYTEPOS (*it);
5093 string = Qnil;
5094 it->c = FETCH_CHAR (pos_byte);
5095 }
5096
5097 /* If there's a valid composition and point is not inside of the
5098 composition (in the case that the composition is from the current
5099 buffer), draw a glyph composed from the composition components. */
5100 if (find_composition (pos, -1, &start, &end, &prop, string)
5101 && COMPOSITION_VALID_P (start, end, prop)
5102 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5103 {
5104 if (start < pos)
5105 /* As we can't handle this situation (perhaps font-lock added
5106 a new composition), we just return here hoping that next
5107 redisplay will detect this composition much earlier. */
5108 return HANDLED_NORMALLY;
5109 if (start != pos)
5110 {
5111 if (STRINGP (it->string))
5112 pos_byte = string_char_to_byte (it->string, start);
5113 else
5114 pos_byte = CHAR_TO_BYTE (start);
5115 }
5116 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5117 prop, string);
5118
5119 if (it->cmp_it.id >= 0)
5120 {
5121 it->cmp_it.ch = -1;
5122 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5123 it->cmp_it.nglyphs = -1;
5124 }
5125 }
5126
5127 return HANDLED_NORMALLY;
5128 }
5129
5130
5131 \f
5132 /***********************************************************************
5133 Overlay strings
5134 ***********************************************************************/
5135
5136 /* The following structure is used to record overlay strings for
5137 later sorting in load_overlay_strings. */
5138
5139 struct overlay_entry
5140 {
5141 Lisp_Object overlay;
5142 Lisp_Object string;
5143 int priority;
5144 int after_string_p;
5145 };
5146
5147
5148 /* Set up iterator IT from overlay strings at its current position.
5149 Called from handle_stop. */
5150
5151 static enum prop_handled
5152 handle_overlay_change (struct it *it)
5153 {
5154 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5155 return HANDLED_RECOMPUTE_PROPS;
5156 else
5157 return HANDLED_NORMALLY;
5158 }
5159
5160
5161 /* Set up the next overlay string for delivery by IT, if there is an
5162 overlay string to deliver. Called by set_iterator_to_next when the
5163 end of the current overlay string is reached. If there are more
5164 overlay strings to display, IT->string and
5165 IT->current.overlay_string_index are set appropriately here.
5166 Otherwise IT->string is set to nil. */
5167
5168 static void
5169 next_overlay_string (struct it *it)
5170 {
5171 ++it->current.overlay_string_index;
5172 if (it->current.overlay_string_index == it->n_overlay_strings)
5173 {
5174 /* No more overlay strings. Restore IT's settings to what
5175 they were before overlay strings were processed, and
5176 continue to deliver from current_buffer. */
5177
5178 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5179 pop_it (it);
5180 xassert (it->sp > 0
5181 || (NILP (it->string)
5182 && it->method == GET_FROM_BUFFER
5183 && it->stop_charpos >= BEGV
5184 && it->stop_charpos <= it->end_charpos));
5185 it->current.overlay_string_index = -1;
5186 it->n_overlay_strings = 0;
5187 it->overlay_strings_charpos = -1;
5188 /* If there's an empty display string on the stack, pop the
5189 stack, to resync the bidi iterator with IT's position. Such
5190 empty strings are pushed onto the stack in
5191 get_overlay_strings_1. */
5192 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5193 pop_it (it);
5194
5195 /* If we're at the end of the buffer, record that we have
5196 processed the overlay strings there already, so that
5197 next_element_from_buffer doesn't try it again. */
5198 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5199 it->overlay_strings_at_end_processed_p = 1;
5200 }
5201 else
5202 {
5203 /* There are more overlay strings to process. If
5204 IT->current.overlay_string_index has advanced to a position
5205 where we must load IT->overlay_strings with more strings, do
5206 it. We must load at the IT->overlay_strings_charpos where
5207 IT->n_overlay_strings was originally computed; when invisible
5208 text is present, this might not be IT_CHARPOS (Bug#7016). */
5209 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5210
5211 if (it->current.overlay_string_index && i == 0)
5212 load_overlay_strings (it, it->overlay_strings_charpos);
5213
5214 /* Initialize IT to deliver display elements from the overlay
5215 string. */
5216 it->string = it->overlay_strings[i];
5217 it->multibyte_p = STRING_MULTIBYTE (it->string);
5218 SET_TEXT_POS (it->current.string_pos, 0, 0);
5219 it->method = GET_FROM_STRING;
5220 it->stop_charpos = 0;
5221 if (it->cmp_it.stop_pos >= 0)
5222 it->cmp_it.stop_pos = 0;
5223 it->prev_stop = 0;
5224 it->base_level_stop = 0;
5225
5226 /* Set up the bidi iterator for this overlay string. */
5227 if (it->bidi_p)
5228 {
5229 it->bidi_it.string.lstring = it->string;
5230 it->bidi_it.string.s = NULL;
5231 it->bidi_it.string.schars = SCHARS (it->string);
5232 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5233 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5234 it->bidi_it.string.unibyte = !it->multibyte_p;
5235 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5236 }
5237 }
5238
5239 CHECK_IT (it);
5240 }
5241
5242
5243 /* Compare two overlay_entry structures E1 and E2. Used as a
5244 comparison function for qsort in load_overlay_strings. Overlay
5245 strings for the same position are sorted so that
5246
5247 1. All after-strings come in front of before-strings, except
5248 when they come from the same overlay.
5249
5250 2. Within after-strings, strings are sorted so that overlay strings
5251 from overlays with higher priorities come first.
5252
5253 2. Within before-strings, strings are sorted so that overlay
5254 strings from overlays with higher priorities come last.
5255
5256 Value is analogous to strcmp. */
5257
5258
5259 static int
5260 compare_overlay_entries (const void *e1, const void *e2)
5261 {
5262 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5263 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5264 int result;
5265
5266 if (entry1->after_string_p != entry2->after_string_p)
5267 {
5268 /* Let after-strings appear in front of before-strings if
5269 they come from different overlays. */
5270 if (EQ (entry1->overlay, entry2->overlay))
5271 result = entry1->after_string_p ? 1 : -1;
5272 else
5273 result = entry1->after_string_p ? -1 : 1;
5274 }
5275 else if (entry1->after_string_p)
5276 /* After-strings sorted in order of decreasing priority. */
5277 result = entry2->priority - entry1->priority;
5278 else
5279 /* Before-strings sorted in order of increasing priority. */
5280 result = entry1->priority - entry2->priority;
5281
5282 return result;
5283 }
5284
5285
5286 /* Load the vector IT->overlay_strings with overlay strings from IT's
5287 current buffer position, or from CHARPOS if that is > 0. Set
5288 IT->n_overlays to the total number of overlay strings found.
5289
5290 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5291 a time. On entry into load_overlay_strings,
5292 IT->current.overlay_string_index gives the number of overlay
5293 strings that have already been loaded by previous calls to this
5294 function.
5295
5296 IT->add_overlay_start contains an additional overlay start
5297 position to consider for taking overlay strings from, if non-zero.
5298 This position comes into play when the overlay has an `invisible'
5299 property, and both before and after-strings. When we've skipped to
5300 the end of the overlay, because of its `invisible' property, we
5301 nevertheless want its before-string to appear.
5302 IT->add_overlay_start will contain the overlay start position
5303 in this case.
5304
5305 Overlay strings are sorted so that after-string strings come in
5306 front of before-string strings. Within before and after-strings,
5307 strings are sorted by overlay priority. See also function
5308 compare_overlay_entries. */
5309
5310 static void
5311 load_overlay_strings (struct it *it, EMACS_INT charpos)
5312 {
5313 Lisp_Object overlay, window, str, invisible;
5314 struct Lisp_Overlay *ov;
5315 EMACS_INT start, end;
5316 int size = 20;
5317 int n = 0, i, j, invis_p;
5318 struct overlay_entry *entries
5319 = (struct overlay_entry *) alloca (size * sizeof *entries);
5320
5321 if (charpos <= 0)
5322 charpos = IT_CHARPOS (*it);
5323
5324 /* Append the overlay string STRING of overlay OVERLAY to vector
5325 `entries' which has size `size' and currently contains `n'
5326 elements. AFTER_P non-zero means STRING is an after-string of
5327 OVERLAY. */
5328 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5329 do \
5330 { \
5331 Lisp_Object priority; \
5332 \
5333 if (n == size) \
5334 { \
5335 int new_size = 2 * size; \
5336 struct overlay_entry *old = entries; \
5337 entries = \
5338 (struct overlay_entry *) alloca (new_size \
5339 * sizeof *entries); \
5340 memcpy (entries, old, size * sizeof *entries); \
5341 size = new_size; \
5342 } \
5343 \
5344 entries[n].string = (STRING); \
5345 entries[n].overlay = (OVERLAY); \
5346 priority = Foverlay_get ((OVERLAY), Qpriority); \
5347 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5348 entries[n].after_string_p = (AFTER_P); \
5349 ++n; \
5350 } \
5351 while (0)
5352
5353 /* Process overlay before the overlay center. */
5354 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5355 {
5356 XSETMISC (overlay, ov);
5357 xassert (OVERLAYP (overlay));
5358 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5359 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5360
5361 if (end < charpos)
5362 break;
5363
5364 /* Skip this overlay if it doesn't start or end at IT's current
5365 position. */
5366 if (end != charpos && start != charpos)
5367 continue;
5368
5369 /* Skip this overlay if it doesn't apply to IT->w. */
5370 window = Foverlay_get (overlay, Qwindow);
5371 if (WINDOWP (window) && XWINDOW (window) != it->w)
5372 continue;
5373
5374 /* If the text ``under'' the overlay is invisible, both before-
5375 and after-strings from this overlay are visible; start and
5376 end position are indistinguishable. */
5377 invisible = Foverlay_get (overlay, Qinvisible);
5378 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5379
5380 /* If overlay has a non-empty before-string, record it. */
5381 if ((start == charpos || (end == charpos && invis_p))
5382 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5383 && SCHARS (str))
5384 RECORD_OVERLAY_STRING (overlay, str, 0);
5385
5386 /* If overlay has a non-empty after-string, record it. */
5387 if ((end == charpos || (start == charpos && invis_p))
5388 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5389 && SCHARS (str))
5390 RECORD_OVERLAY_STRING (overlay, str, 1);
5391 }
5392
5393 /* Process overlays after the overlay center. */
5394 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5395 {
5396 XSETMISC (overlay, ov);
5397 xassert (OVERLAYP (overlay));
5398 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5399 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5400
5401 if (start > charpos)
5402 break;
5403
5404 /* Skip this overlay if it doesn't start or end at IT's current
5405 position. */
5406 if (end != charpos && start != charpos)
5407 continue;
5408
5409 /* Skip this overlay if it doesn't apply to IT->w. */
5410 window = Foverlay_get (overlay, Qwindow);
5411 if (WINDOWP (window) && XWINDOW (window) != it->w)
5412 continue;
5413
5414 /* If the text ``under'' the overlay is invisible, it has a zero
5415 dimension, and both before- and after-strings apply. */
5416 invisible = Foverlay_get (overlay, Qinvisible);
5417 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5418
5419 /* If overlay has a non-empty before-string, record it. */
5420 if ((start == charpos || (end == charpos && invis_p))
5421 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5422 && SCHARS (str))
5423 RECORD_OVERLAY_STRING (overlay, str, 0);
5424
5425 /* If overlay has a non-empty after-string, record it. */
5426 if ((end == charpos || (start == charpos && invis_p))
5427 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5428 && SCHARS (str))
5429 RECORD_OVERLAY_STRING (overlay, str, 1);
5430 }
5431
5432 #undef RECORD_OVERLAY_STRING
5433
5434 /* Sort entries. */
5435 if (n > 1)
5436 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5437
5438 /* Record number of overlay strings, and where we computed it. */
5439 it->n_overlay_strings = n;
5440 it->overlay_strings_charpos = charpos;
5441
5442 /* IT->current.overlay_string_index is the number of overlay strings
5443 that have already been consumed by IT. Copy some of the
5444 remaining overlay strings to IT->overlay_strings. */
5445 i = 0;
5446 j = it->current.overlay_string_index;
5447 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5448 {
5449 it->overlay_strings[i] = entries[j].string;
5450 it->string_overlays[i++] = entries[j++].overlay;
5451 }
5452
5453 CHECK_IT (it);
5454 }
5455
5456
5457 /* Get the first chunk of overlay strings at IT's current buffer
5458 position, or at CHARPOS if that is > 0. Value is non-zero if at
5459 least one overlay string was found. */
5460
5461 static int
5462 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5463 {
5464 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5465 process. This fills IT->overlay_strings with strings, and sets
5466 IT->n_overlay_strings to the total number of strings to process.
5467 IT->pos.overlay_string_index has to be set temporarily to zero
5468 because load_overlay_strings needs this; it must be set to -1
5469 when no overlay strings are found because a zero value would
5470 indicate a position in the first overlay string. */
5471 it->current.overlay_string_index = 0;
5472 load_overlay_strings (it, charpos);
5473
5474 /* If we found overlay strings, set up IT to deliver display
5475 elements from the first one. Otherwise set up IT to deliver
5476 from current_buffer. */
5477 if (it->n_overlay_strings)
5478 {
5479 /* Make sure we know settings in current_buffer, so that we can
5480 restore meaningful values when we're done with the overlay
5481 strings. */
5482 if (compute_stop_p)
5483 compute_stop_pos (it);
5484 xassert (it->face_id >= 0);
5485
5486 /* Save IT's settings. They are restored after all overlay
5487 strings have been processed. */
5488 xassert (!compute_stop_p || it->sp == 0);
5489
5490 /* When called from handle_stop, there might be an empty display
5491 string loaded. In that case, don't bother saving it. But
5492 don't use this optimization with the bidi iterator, since we
5493 need the corresponding pop_it call to resync the bidi
5494 iterator's position with IT's position, after we are done
5495 with the overlay strings. (The corresponding call to pop_it
5496 in case of an empty display string is in
5497 next_overlay_string.) */
5498 if (!(!it->bidi_p
5499 && STRINGP (it->string) && !SCHARS (it->string)))
5500 push_it (it, NULL);
5501
5502 /* Set up IT to deliver display elements from the first overlay
5503 string. */
5504 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5505 it->string = it->overlay_strings[0];
5506 it->from_overlay = Qnil;
5507 it->stop_charpos = 0;
5508 xassert (STRINGP (it->string));
5509 it->end_charpos = SCHARS (it->string);
5510 it->prev_stop = 0;
5511 it->base_level_stop = 0;
5512 it->multibyte_p = STRING_MULTIBYTE (it->string);
5513 it->method = GET_FROM_STRING;
5514 it->from_disp_prop_p = 0;
5515
5516 /* Force paragraph direction to be that of the parent
5517 buffer. */
5518 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5519 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5520 else
5521 it->paragraph_embedding = L2R;
5522
5523 /* Set up the bidi iterator for this overlay string. */
5524 if (it->bidi_p)
5525 {
5526 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5527
5528 it->bidi_it.string.lstring = it->string;
5529 it->bidi_it.string.s = NULL;
5530 it->bidi_it.string.schars = SCHARS (it->string);
5531 it->bidi_it.string.bufpos = pos;
5532 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5533 it->bidi_it.string.unibyte = !it->multibyte_p;
5534 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5535 }
5536 return 1;
5537 }
5538
5539 it->current.overlay_string_index = -1;
5540 return 0;
5541 }
5542
5543 static int
5544 get_overlay_strings (struct it *it, EMACS_INT charpos)
5545 {
5546 it->string = Qnil;
5547 it->method = GET_FROM_BUFFER;
5548
5549 (void) get_overlay_strings_1 (it, charpos, 1);
5550
5551 CHECK_IT (it);
5552
5553 /* Value is non-zero if we found at least one overlay string. */
5554 return STRINGP (it->string);
5555 }
5556
5557
5558 \f
5559 /***********************************************************************
5560 Saving and restoring state
5561 ***********************************************************************/
5562
5563 /* Save current settings of IT on IT->stack. Called, for example,
5564 before setting up IT for an overlay string, to be able to restore
5565 IT's settings to what they were after the overlay string has been
5566 processed. If POSITION is non-NULL, it is the position to save on
5567 the stack instead of IT->position. */
5568
5569 static void
5570 push_it (struct it *it, struct text_pos *position)
5571 {
5572 struct iterator_stack_entry *p;
5573
5574 xassert (it->sp < IT_STACK_SIZE);
5575 p = it->stack + it->sp;
5576
5577 p->stop_charpos = it->stop_charpos;
5578 p->prev_stop = it->prev_stop;
5579 p->base_level_stop = it->base_level_stop;
5580 p->cmp_it = it->cmp_it;
5581 xassert (it->face_id >= 0);
5582 p->face_id = it->face_id;
5583 p->string = it->string;
5584 p->method = it->method;
5585 p->from_overlay = it->from_overlay;
5586 switch (p->method)
5587 {
5588 case GET_FROM_IMAGE:
5589 p->u.image.object = it->object;
5590 p->u.image.image_id = it->image_id;
5591 p->u.image.slice = it->slice;
5592 break;
5593 case GET_FROM_STRETCH:
5594 p->u.stretch.object = it->object;
5595 break;
5596 }
5597 p->position = position ? *position : it->position;
5598 p->current = it->current;
5599 p->end_charpos = it->end_charpos;
5600 p->string_nchars = it->string_nchars;
5601 p->area = it->area;
5602 p->multibyte_p = it->multibyte_p;
5603 p->avoid_cursor_p = it->avoid_cursor_p;
5604 p->space_width = it->space_width;
5605 p->font_height = it->font_height;
5606 p->voffset = it->voffset;
5607 p->string_from_display_prop_p = it->string_from_display_prop_p;
5608 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5609 p->display_ellipsis_p = 0;
5610 p->line_wrap = it->line_wrap;
5611 p->bidi_p = it->bidi_p;
5612 p->paragraph_embedding = it->paragraph_embedding;
5613 p->from_disp_prop_p = it->from_disp_prop_p;
5614 ++it->sp;
5615
5616 /* Save the state of the bidi iterator as well. */
5617 if (it->bidi_p)
5618 bidi_push_it (&it->bidi_it);
5619 }
5620
5621 static void
5622 iterate_out_of_display_property (struct it *it)
5623 {
5624 int buffer_p = BUFFERP (it->object);
5625 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5626 EMACS_INT bob = (buffer_p ? BEGV : 0);
5627
5628 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5629
5630 /* Maybe initialize paragraph direction. If we are at the beginning
5631 of a new paragraph, next_element_from_buffer may not have a
5632 chance to do that. */
5633 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5634 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5635 /* prev_stop can be zero, so check against BEGV as well. */
5636 while (it->bidi_it.charpos >= bob
5637 && it->prev_stop <= it->bidi_it.charpos
5638 && it->bidi_it.charpos < CHARPOS (it->position)
5639 && it->bidi_it.charpos < eob)
5640 bidi_move_to_visually_next (&it->bidi_it);
5641 /* Record the stop_pos we just crossed, for when we cross it
5642 back, maybe. */
5643 if (it->bidi_it.charpos > CHARPOS (it->position))
5644 it->prev_stop = CHARPOS (it->position);
5645 /* If we ended up not where pop_it put us, resync IT's
5646 positional members with the bidi iterator. */
5647 if (it->bidi_it.charpos != CHARPOS (it->position))
5648 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5649 if (buffer_p)
5650 it->current.pos = it->position;
5651 else
5652 it->current.string_pos = it->position;
5653 }
5654
5655 /* Restore IT's settings from IT->stack. Called, for example, when no
5656 more overlay strings must be processed, and we return to delivering
5657 display elements from a buffer, or when the end of a string from a
5658 `display' property is reached and we return to delivering display
5659 elements from an overlay string, or from a buffer. */
5660
5661 static void
5662 pop_it (struct it *it)
5663 {
5664 struct iterator_stack_entry *p;
5665 int from_display_prop = it->from_disp_prop_p;
5666
5667 xassert (it->sp > 0);
5668 --it->sp;
5669 p = it->stack + it->sp;
5670 it->stop_charpos = p->stop_charpos;
5671 it->prev_stop = p->prev_stop;
5672 it->base_level_stop = p->base_level_stop;
5673 it->cmp_it = p->cmp_it;
5674 it->face_id = p->face_id;
5675 it->current = p->current;
5676 it->position = p->position;
5677 it->string = p->string;
5678 it->from_overlay = p->from_overlay;
5679 if (NILP (it->string))
5680 SET_TEXT_POS (it->current.string_pos, -1, -1);
5681 it->method = p->method;
5682 switch (it->method)
5683 {
5684 case GET_FROM_IMAGE:
5685 it->image_id = p->u.image.image_id;
5686 it->object = p->u.image.object;
5687 it->slice = p->u.image.slice;
5688 break;
5689 case GET_FROM_STRETCH:
5690 it->object = p->u.stretch.object;
5691 break;
5692 case GET_FROM_BUFFER:
5693 it->object = it->w->buffer;
5694 break;
5695 case GET_FROM_STRING:
5696 it->object = it->string;
5697 break;
5698 case GET_FROM_DISPLAY_VECTOR:
5699 if (it->s)
5700 it->method = GET_FROM_C_STRING;
5701 else if (STRINGP (it->string))
5702 it->method = GET_FROM_STRING;
5703 else
5704 {
5705 it->method = GET_FROM_BUFFER;
5706 it->object = it->w->buffer;
5707 }
5708 }
5709 it->end_charpos = p->end_charpos;
5710 it->string_nchars = p->string_nchars;
5711 it->area = p->area;
5712 it->multibyte_p = p->multibyte_p;
5713 it->avoid_cursor_p = p->avoid_cursor_p;
5714 it->space_width = p->space_width;
5715 it->font_height = p->font_height;
5716 it->voffset = p->voffset;
5717 it->string_from_display_prop_p = p->string_from_display_prop_p;
5718 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5719 it->line_wrap = p->line_wrap;
5720 it->bidi_p = p->bidi_p;
5721 it->paragraph_embedding = p->paragraph_embedding;
5722 it->from_disp_prop_p = p->from_disp_prop_p;
5723 if (it->bidi_p)
5724 {
5725 bidi_pop_it (&it->bidi_it);
5726 /* Bidi-iterate until we get out of the portion of text, if any,
5727 covered by a `display' text property or by an overlay with
5728 `display' property. (We cannot just jump there, because the
5729 internal coherency of the bidi iterator state can not be
5730 preserved across such jumps.) We also must determine the
5731 paragraph base direction if the overlay we just processed is
5732 at the beginning of a new paragraph. */
5733 if (from_display_prop
5734 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5735 iterate_out_of_display_property (it);
5736
5737 xassert ((BUFFERP (it->object)
5738 && IT_CHARPOS (*it) == it->bidi_it.charpos
5739 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5740 || (STRINGP (it->object)
5741 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5742 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5743 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5744 }
5745 }
5746
5747
5748 \f
5749 /***********************************************************************
5750 Moving over lines
5751 ***********************************************************************/
5752
5753 /* Set IT's current position to the previous line start. */
5754
5755 static void
5756 back_to_previous_line_start (struct it *it)
5757 {
5758 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5759 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5760 }
5761
5762
5763 /* Move IT to the next line start.
5764
5765 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5766 we skipped over part of the text (as opposed to moving the iterator
5767 continuously over the text). Otherwise, don't change the value
5768 of *SKIPPED_P.
5769
5770 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5771 iterator on the newline, if it was found.
5772
5773 Newlines may come from buffer text, overlay strings, or strings
5774 displayed via the `display' property. That's the reason we can't
5775 simply use find_next_newline_no_quit.
5776
5777 Note that this function may not skip over invisible text that is so
5778 because of text properties and immediately follows a newline. If
5779 it would, function reseat_at_next_visible_line_start, when called
5780 from set_iterator_to_next, would effectively make invisible
5781 characters following a newline part of the wrong glyph row, which
5782 leads to wrong cursor motion. */
5783
5784 static int
5785 forward_to_next_line_start (struct it *it, int *skipped_p,
5786 struct bidi_it *bidi_it_prev)
5787 {
5788 EMACS_INT old_selective;
5789 int newline_found_p, n;
5790 const int MAX_NEWLINE_DISTANCE = 500;
5791
5792 /* If already on a newline, just consume it to avoid unintended
5793 skipping over invisible text below. */
5794 if (it->what == IT_CHARACTER
5795 && it->c == '\n'
5796 && CHARPOS (it->position) == IT_CHARPOS (*it))
5797 {
5798 if (it->bidi_p && bidi_it_prev)
5799 *bidi_it_prev = it->bidi_it;
5800 set_iterator_to_next (it, 0);
5801 it->c = 0;
5802 return 1;
5803 }
5804
5805 /* Don't handle selective display in the following. It's (a)
5806 unnecessary because it's done by the caller, and (b) leads to an
5807 infinite recursion because next_element_from_ellipsis indirectly
5808 calls this function. */
5809 old_selective = it->selective;
5810 it->selective = 0;
5811
5812 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5813 from buffer text. */
5814 for (n = newline_found_p = 0;
5815 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5816 n += STRINGP (it->string) ? 0 : 1)
5817 {
5818 if (!get_next_display_element (it))
5819 return 0;
5820 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5821 if (newline_found_p && it->bidi_p && bidi_it_prev)
5822 *bidi_it_prev = it->bidi_it;
5823 set_iterator_to_next (it, 0);
5824 }
5825
5826 /* If we didn't find a newline near enough, see if we can use a
5827 short-cut. */
5828 if (!newline_found_p)
5829 {
5830 EMACS_INT start = IT_CHARPOS (*it);
5831 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5832 Lisp_Object pos;
5833
5834 xassert (!STRINGP (it->string));
5835
5836 /* If there isn't any `display' property in sight, and no
5837 overlays, we can just use the position of the newline in
5838 buffer text. */
5839 if (it->stop_charpos >= limit
5840 || ((pos = Fnext_single_property_change (make_number (start),
5841 Qdisplay, Qnil,
5842 make_number (limit)),
5843 NILP (pos))
5844 && next_overlay_change (start) == ZV))
5845 {
5846 if (!it->bidi_p)
5847 {
5848 IT_CHARPOS (*it) = limit;
5849 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5850 }
5851 else
5852 {
5853 struct bidi_it bprev;
5854
5855 /* Help bidi.c avoid expensive searches for display
5856 properties and overlays, by telling it that there are
5857 none up to `limit'. */
5858 if (it->bidi_it.disp_pos < limit)
5859 {
5860 it->bidi_it.disp_pos = limit;
5861 it->bidi_it.disp_prop = 0;
5862 }
5863 do {
5864 bprev = it->bidi_it;
5865 bidi_move_to_visually_next (&it->bidi_it);
5866 } while (it->bidi_it.charpos != limit);
5867 IT_CHARPOS (*it) = limit;
5868 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5869 if (bidi_it_prev)
5870 *bidi_it_prev = bprev;
5871 }
5872 *skipped_p = newline_found_p = 1;
5873 }
5874 else
5875 {
5876 while (get_next_display_element (it)
5877 && !newline_found_p)
5878 {
5879 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5880 if (newline_found_p && it->bidi_p && bidi_it_prev)
5881 *bidi_it_prev = it->bidi_it;
5882 set_iterator_to_next (it, 0);
5883 }
5884 }
5885 }
5886
5887 it->selective = old_selective;
5888 return newline_found_p;
5889 }
5890
5891
5892 /* Set IT's current position to the previous visible line start. Skip
5893 invisible text that is so either due to text properties or due to
5894 selective display. Caution: this does not change IT->current_x and
5895 IT->hpos. */
5896
5897 static void
5898 back_to_previous_visible_line_start (struct it *it)
5899 {
5900 while (IT_CHARPOS (*it) > BEGV)
5901 {
5902 back_to_previous_line_start (it);
5903
5904 if (IT_CHARPOS (*it) <= BEGV)
5905 break;
5906
5907 /* If selective > 0, then lines indented more than its value are
5908 invisible. */
5909 if (it->selective > 0
5910 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5911 it->selective))
5912 continue;
5913
5914 /* Check the newline before point for invisibility. */
5915 {
5916 Lisp_Object prop;
5917 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5918 Qinvisible, it->window);
5919 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5920 continue;
5921 }
5922
5923 if (IT_CHARPOS (*it) <= BEGV)
5924 break;
5925
5926 {
5927 struct it it2;
5928 void *it2data = NULL;
5929 EMACS_INT pos;
5930 EMACS_INT beg, end;
5931 Lisp_Object val, overlay;
5932
5933 SAVE_IT (it2, *it, it2data);
5934
5935 /* If newline is part of a composition, continue from start of composition */
5936 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5937 && beg < IT_CHARPOS (*it))
5938 goto replaced;
5939
5940 /* If newline is replaced by a display property, find start of overlay
5941 or interval and continue search from that point. */
5942 pos = --IT_CHARPOS (it2);
5943 --IT_BYTEPOS (it2);
5944 it2.sp = 0;
5945 bidi_unshelve_cache (NULL, 0);
5946 it2.string_from_display_prop_p = 0;
5947 it2.from_disp_prop_p = 0;
5948 if (handle_display_prop (&it2) == HANDLED_RETURN
5949 && !NILP (val = get_char_property_and_overlay
5950 (make_number (pos), Qdisplay, Qnil, &overlay))
5951 && (OVERLAYP (overlay)
5952 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5953 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5954 {
5955 RESTORE_IT (it, it, it2data);
5956 goto replaced;
5957 }
5958
5959 /* Newline is not replaced by anything -- so we are done. */
5960 RESTORE_IT (it, it, it2data);
5961 break;
5962
5963 replaced:
5964 if (beg < BEGV)
5965 beg = BEGV;
5966 IT_CHARPOS (*it) = beg;
5967 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5968 }
5969 }
5970
5971 it->continuation_lines_width = 0;
5972
5973 xassert (IT_CHARPOS (*it) >= BEGV);
5974 xassert (IT_CHARPOS (*it) == BEGV
5975 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5976 CHECK_IT (it);
5977 }
5978
5979
5980 /* Reseat iterator IT at the previous visible line start. Skip
5981 invisible text that is so either due to text properties or due to
5982 selective display. At the end, update IT's overlay information,
5983 face information etc. */
5984
5985 void
5986 reseat_at_previous_visible_line_start (struct it *it)
5987 {
5988 back_to_previous_visible_line_start (it);
5989 reseat (it, it->current.pos, 1);
5990 CHECK_IT (it);
5991 }
5992
5993
5994 /* Reseat iterator IT on the next visible line start in the current
5995 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5996 preceding the line start. Skip over invisible text that is so
5997 because of selective display. Compute faces, overlays etc at the
5998 new position. Note that this function does not skip over text that
5999 is invisible because of text properties. */
6000
6001 static void
6002 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6003 {
6004 int newline_found_p, skipped_p = 0;
6005 struct bidi_it bidi_it_prev;
6006
6007 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6008
6009 /* Skip over lines that are invisible because they are indented
6010 more than the value of IT->selective. */
6011 if (it->selective > 0)
6012 while (IT_CHARPOS (*it) < ZV
6013 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6014 it->selective))
6015 {
6016 xassert (IT_BYTEPOS (*it) == BEGV
6017 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6018 newline_found_p =
6019 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6020 }
6021
6022 /* Position on the newline if that's what's requested. */
6023 if (on_newline_p && newline_found_p)
6024 {
6025 if (STRINGP (it->string))
6026 {
6027 if (IT_STRING_CHARPOS (*it) > 0)
6028 {
6029 if (!it->bidi_p)
6030 {
6031 --IT_STRING_CHARPOS (*it);
6032 --IT_STRING_BYTEPOS (*it);
6033 }
6034 else
6035 {
6036 /* We need to restore the bidi iterator to the state
6037 it had on the newline, and resync the IT's
6038 position with that. */
6039 it->bidi_it = bidi_it_prev;
6040 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6041 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6042 }
6043 }
6044 }
6045 else if (IT_CHARPOS (*it) > BEGV)
6046 {
6047 if (!it->bidi_p)
6048 {
6049 --IT_CHARPOS (*it);
6050 --IT_BYTEPOS (*it);
6051 }
6052 else
6053 {
6054 /* We need to restore the bidi iterator to the state it
6055 had on the newline and resync IT with that. */
6056 it->bidi_it = bidi_it_prev;
6057 IT_CHARPOS (*it) = it->bidi_it.charpos;
6058 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6059 }
6060 reseat (it, it->current.pos, 0);
6061 }
6062 }
6063 else if (skipped_p)
6064 reseat (it, it->current.pos, 0);
6065
6066 CHECK_IT (it);
6067 }
6068
6069
6070 \f
6071 /***********************************************************************
6072 Changing an iterator's position
6073 ***********************************************************************/
6074
6075 /* Change IT's current position to POS in current_buffer. If FORCE_P
6076 is non-zero, always check for text properties at the new position.
6077 Otherwise, text properties are only looked up if POS >=
6078 IT->check_charpos of a property. */
6079
6080 static void
6081 reseat (struct it *it, struct text_pos pos, int force_p)
6082 {
6083 EMACS_INT original_pos = IT_CHARPOS (*it);
6084
6085 reseat_1 (it, pos, 0);
6086
6087 /* Determine where to check text properties. Avoid doing it
6088 where possible because text property lookup is very expensive. */
6089 if (force_p
6090 || CHARPOS (pos) > it->stop_charpos
6091 || CHARPOS (pos) < original_pos)
6092 {
6093 if (it->bidi_p)
6094 {
6095 /* For bidi iteration, we need to prime prev_stop and
6096 base_level_stop with our best estimations. */
6097 /* Implementation note: Of course, POS is not necessarily a
6098 stop position, so assigning prev_pos to it is a lie; we
6099 should have called compute_stop_backwards. However, if
6100 the current buffer does not include any R2L characters,
6101 that call would be a waste of cycles, because the
6102 iterator will never move back, and thus never cross this
6103 "fake" stop position. So we delay that backward search
6104 until the time we really need it, in next_element_from_buffer. */
6105 if (CHARPOS (pos) != it->prev_stop)
6106 it->prev_stop = CHARPOS (pos);
6107 if (CHARPOS (pos) < it->base_level_stop)
6108 it->base_level_stop = 0; /* meaning it's unknown */
6109 handle_stop (it);
6110 }
6111 else
6112 {
6113 handle_stop (it);
6114 it->prev_stop = it->base_level_stop = 0;
6115 }
6116
6117 }
6118
6119 CHECK_IT (it);
6120 }
6121
6122
6123 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6124 IT->stop_pos to POS, also. */
6125
6126 static void
6127 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6128 {
6129 /* Don't call this function when scanning a C string. */
6130 xassert (it->s == NULL);
6131
6132 /* POS must be a reasonable value. */
6133 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6134
6135 it->current.pos = it->position = pos;
6136 it->end_charpos = ZV;
6137 it->dpvec = NULL;
6138 it->current.dpvec_index = -1;
6139 it->current.overlay_string_index = -1;
6140 IT_STRING_CHARPOS (*it) = -1;
6141 IT_STRING_BYTEPOS (*it) = -1;
6142 it->string = Qnil;
6143 it->method = GET_FROM_BUFFER;
6144 it->object = it->w->buffer;
6145 it->area = TEXT_AREA;
6146 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6147 it->sp = 0;
6148 it->string_from_display_prop_p = 0;
6149 it->string_from_prefix_prop_p = 0;
6150
6151 it->from_disp_prop_p = 0;
6152 it->face_before_selective_p = 0;
6153 if (it->bidi_p)
6154 {
6155 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6156 &it->bidi_it);
6157 bidi_unshelve_cache (NULL, 0);
6158 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6159 it->bidi_it.string.s = NULL;
6160 it->bidi_it.string.lstring = Qnil;
6161 it->bidi_it.string.bufpos = 0;
6162 it->bidi_it.string.unibyte = 0;
6163 }
6164
6165 if (set_stop_p)
6166 {
6167 it->stop_charpos = CHARPOS (pos);
6168 it->base_level_stop = CHARPOS (pos);
6169 }
6170 }
6171
6172
6173 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6174 If S is non-null, it is a C string to iterate over. Otherwise,
6175 STRING gives a Lisp string to iterate over.
6176
6177 If PRECISION > 0, don't return more then PRECISION number of
6178 characters from the string.
6179
6180 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6181 characters have been returned. FIELD_WIDTH < 0 means an infinite
6182 field width.
6183
6184 MULTIBYTE = 0 means disable processing of multibyte characters,
6185 MULTIBYTE > 0 means enable it,
6186 MULTIBYTE < 0 means use IT->multibyte_p.
6187
6188 IT must be initialized via a prior call to init_iterator before
6189 calling this function. */
6190
6191 static void
6192 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6193 EMACS_INT charpos, EMACS_INT precision, int field_width,
6194 int multibyte)
6195 {
6196 /* No region in strings. */
6197 it->region_beg_charpos = it->region_end_charpos = -1;
6198
6199 /* No text property checks performed by default, but see below. */
6200 it->stop_charpos = -1;
6201
6202 /* Set iterator position and end position. */
6203 memset (&it->current, 0, sizeof it->current);
6204 it->current.overlay_string_index = -1;
6205 it->current.dpvec_index = -1;
6206 xassert (charpos >= 0);
6207
6208 /* If STRING is specified, use its multibyteness, otherwise use the
6209 setting of MULTIBYTE, if specified. */
6210 if (multibyte >= 0)
6211 it->multibyte_p = multibyte > 0;
6212
6213 /* Bidirectional reordering of strings is controlled by the default
6214 value of bidi-display-reordering. Don't try to reorder while
6215 loading loadup.el, as the necessary character property tables are
6216 not yet available. */
6217 it->bidi_p =
6218 NILP (Vpurify_flag)
6219 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6220
6221 if (s == NULL)
6222 {
6223 xassert (STRINGP (string));
6224 it->string = string;
6225 it->s = NULL;
6226 it->end_charpos = it->string_nchars = SCHARS (string);
6227 it->method = GET_FROM_STRING;
6228 it->current.string_pos = string_pos (charpos, string);
6229
6230 if (it->bidi_p)
6231 {
6232 it->bidi_it.string.lstring = string;
6233 it->bidi_it.string.s = NULL;
6234 it->bidi_it.string.schars = it->end_charpos;
6235 it->bidi_it.string.bufpos = 0;
6236 it->bidi_it.string.from_disp_str = 0;
6237 it->bidi_it.string.unibyte = !it->multibyte_p;
6238 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6239 FRAME_WINDOW_P (it->f), &it->bidi_it);
6240 }
6241 }
6242 else
6243 {
6244 it->s = (const unsigned char *) s;
6245 it->string = Qnil;
6246
6247 /* Note that we use IT->current.pos, not it->current.string_pos,
6248 for displaying C strings. */
6249 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6250 if (it->multibyte_p)
6251 {
6252 it->current.pos = c_string_pos (charpos, s, 1);
6253 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6254 }
6255 else
6256 {
6257 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6258 it->end_charpos = it->string_nchars = strlen (s);
6259 }
6260
6261 if (it->bidi_p)
6262 {
6263 it->bidi_it.string.lstring = Qnil;
6264 it->bidi_it.string.s = (const unsigned char *) s;
6265 it->bidi_it.string.schars = it->end_charpos;
6266 it->bidi_it.string.bufpos = 0;
6267 it->bidi_it.string.from_disp_str = 0;
6268 it->bidi_it.string.unibyte = !it->multibyte_p;
6269 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6270 &it->bidi_it);
6271 }
6272 it->method = GET_FROM_C_STRING;
6273 }
6274
6275 /* PRECISION > 0 means don't return more than PRECISION characters
6276 from the string. */
6277 if (precision > 0 && it->end_charpos - charpos > precision)
6278 {
6279 it->end_charpos = it->string_nchars = charpos + precision;
6280 if (it->bidi_p)
6281 it->bidi_it.string.schars = it->end_charpos;
6282 }
6283
6284 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6285 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6286 FIELD_WIDTH < 0 means infinite field width. This is useful for
6287 padding with `-' at the end of a mode line. */
6288 if (field_width < 0)
6289 field_width = INFINITY;
6290 /* Implementation note: We deliberately don't enlarge
6291 it->bidi_it.string.schars here to fit it->end_charpos, because
6292 the bidi iterator cannot produce characters out of thin air. */
6293 if (field_width > it->end_charpos - charpos)
6294 it->end_charpos = charpos + field_width;
6295
6296 /* Use the standard display table for displaying strings. */
6297 if (DISP_TABLE_P (Vstandard_display_table))
6298 it->dp = XCHAR_TABLE (Vstandard_display_table);
6299
6300 it->stop_charpos = charpos;
6301 it->prev_stop = charpos;
6302 it->base_level_stop = 0;
6303 if (it->bidi_p)
6304 {
6305 it->bidi_it.first_elt = 1;
6306 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6307 it->bidi_it.disp_pos = -1;
6308 }
6309 if (s == NULL && it->multibyte_p)
6310 {
6311 EMACS_INT endpos = SCHARS (it->string);
6312 if (endpos > it->end_charpos)
6313 endpos = it->end_charpos;
6314 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6315 it->string);
6316 }
6317 CHECK_IT (it);
6318 }
6319
6320
6321 \f
6322 /***********************************************************************
6323 Iteration
6324 ***********************************************************************/
6325
6326 /* Map enum it_method value to corresponding next_element_from_* function. */
6327
6328 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6329 {
6330 next_element_from_buffer,
6331 next_element_from_display_vector,
6332 next_element_from_string,
6333 next_element_from_c_string,
6334 next_element_from_image,
6335 next_element_from_stretch
6336 };
6337
6338 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6339
6340
6341 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6342 (possibly with the following characters). */
6343
6344 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6345 ((IT)->cmp_it.id >= 0 \
6346 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6347 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6348 END_CHARPOS, (IT)->w, \
6349 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6350 (IT)->string)))
6351
6352
6353 /* Lookup the char-table Vglyphless_char_display for character C (-1
6354 if we want information for no-font case), and return the display
6355 method symbol. By side-effect, update it->what and
6356 it->glyphless_method. This function is called from
6357 get_next_display_element for each character element, and from
6358 x_produce_glyphs when no suitable font was found. */
6359
6360 Lisp_Object
6361 lookup_glyphless_char_display (int c, struct it *it)
6362 {
6363 Lisp_Object glyphless_method = Qnil;
6364
6365 if (CHAR_TABLE_P (Vglyphless_char_display)
6366 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6367 {
6368 if (c >= 0)
6369 {
6370 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6371 if (CONSP (glyphless_method))
6372 glyphless_method = FRAME_WINDOW_P (it->f)
6373 ? XCAR (glyphless_method)
6374 : XCDR (glyphless_method);
6375 }
6376 else
6377 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6378 }
6379
6380 retry:
6381 if (NILP (glyphless_method))
6382 {
6383 if (c >= 0)
6384 /* The default is to display the character by a proper font. */
6385 return Qnil;
6386 /* The default for the no-font case is to display an empty box. */
6387 glyphless_method = Qempty_box;
6388 }
6389 if (EQ (glyphless_method, Qzero_width))
6390 {
6391 if (c >= 0)
6392 return glyphless_method;
6393 /* This method can't be used for the no-font case. */
6394 glyphless_method = Qempty_box;
6395 }
6396 if (EQ (glyphless_method, Qthin_space))
6397 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6398 else if (EQ (glyphless_method, Qempty_box))
6399 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6400 else if (EQ (glyphless_method, Qhex_code))
6401 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6402 else if (STRINGP (glyphless_method))
6403 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6404 else
6405 {
6406 /* Invalid value. We use the default method. */
6407 glyphless_method = Qnil;
6408 goto retry;
6409 }
6410 it->what = IT_GLYPHLESS;
6411 return glyphless_method;
6412 }
6413
6414 /* Load IT's display element fields with information about the next
6415 display element from the current position of IT. Value is zero if
6416 end of buffer (or C string) is reached. */
6417
6418 static struct frame *last_escape_glyph_frame = NULL;
6419 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6420 static int last_escape_glyph_merged_face_id = 0;
6421
6422 struct frame *last_glyphless_glyph_frame = NULL;
6423 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6424 int last_glyphless_glyph_merged_face_id = 0;
6425
6426 static int
6427 get_next_display_element (struct it *it)
6428 {
6429 /* Non-zero means that we found a display element. Zero means that
6430 we hit the end of what we iterate over. Performance note: the
6431 function pointer `method' used here turns out to be faster than
6432 using a sequence of if-statements. */
6433 int success_p;
6434
6435 get_next:
6436 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6437
6438 if (it->what == IT_CHARACTER)
6439 {
6440 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6441 and only if (a) the resolved directionality of that character
6442 is R..." */
6443 /* FIXME: Do we need an exception for characters from display
6444 tables? */
6445 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6446 it->c = bidi_mirror_char (it->c);
6447 /* Map via display table or translate control characters.
6448 IT->c, IT->len etc. have been set to the next character by
6449 the function call above. If we have a display table, and it
6450 contains an entry for IT->c, translate it. Don't do this if
6451 IT->c itself comes from a display table, otherwise we could
6452 end up in an infinite recursion. (An alternative could be to
6453 count the recursion depth of this function and signal an
6454 error when a certain maximum depth is reached.) Is it worth
6455 it? */
6456 if (success_p && it->dpvec == NULL)
6457 {
6458 Lisp_Object dv;
6459 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6460 int nonascii_space_p = 0;
6461 int nonascii_hyphen_p = 0;
6462 int c = it->c; /* This is the character to display. */
6463
6464 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6465 {
6466 xassert (SINGLE_BYTE_CHAR_P (c));
6467 if (unibyte_display_via_language_environment)
6468 {
6469 c = DECODE_CHAR (unibyte, c);
6470 if (c < 0)
6471 c = BYTE8_TO_CHAR (it->c);
6472 }
6473 else
6474 c = BYTE8_TO_CHAR (it->c);
6475 }
6476
6477 if (it->dp
6478 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6479 VECTORP (dv)))
6480 {
6481 struct Lisp_Vector *v = XVECTOR (dv);
6482
6483 /* Return the first character from the display table
6484 entry, if not empty. If empty, don't display the
6485 current character. */
6486 if (v->header.size)
6487 {
6488 it->dpvec_char_len = it->len;
6489 it->dpvec = v->contents;
6490 it->dpend = v->contents + v->header.size;
6491 it->current.dpvec_index = 0;
6492 it->dpvec_face_id = -1;
6493 it->saved_face_id = it->face_id;
6494 it->method = GET_FROM_DISPLAY_VECTOR;
6495 it->ellipsis_p = 0;
6496 }
6497 else
6498 {
6499 set_iterator_to_next (it, 0);
6500 }
6501 goto get_next;
6502 }
6503
6504 if (! NILP (lookup_glyphless_char_display (c, it)))
6505 {
6506 if (it->what == IT_GLYPHLESS)
6507 goto done;
6508 /* Don't display this character. */
6509 set_iterator_to_next (it, 0);
6510 goto get_next;
6511 }
6512
6513 /* If `nobreak-char-display' is non-nil, we display
6514 non-ASCII spaces and hyphens specially. */
6515 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6516 {
6517 if (c == 0xA0)
6518 nonascii_space_p = 1;
6519 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6520 nonascii_hyphen_p = 1;
6521 }
6522
6523 /* Translate control characters into `\003' or `^C' form.
6524 Control characters coming from a display table entry are
6525 currently not translated because we use IT->dpvec to hold
6526 the translation. This could easily be changed but I
6527 don't believe that it is worth doing.
6528
6529 The characters handled by `nobreak-char-display' must be
6530 translated too.
6531
6532 Non-printable characters and raw-byte characters are also
6533 translated to octal form. */
6534 if (((c < ' ' || c == 127) /* ASCII control chars */
6535 ? (it->area != TEXT_AREA
6536 /* In mode line, treat \n, \t like other crl chars. */
6537 || (c != '\t'
6538 && it->glyph_row
6539 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6540 || (c != '\n' && c != '\t'))
6541 : (nonascii_space_p
6542 || nonascii_hyphen_p
6543 || CHAR_BYTE8_P (c)
6544 || ! CHAR_PRINTABLE_P (c))))
6545 {
6546 /* C is a control character, non-ASCII space/hyphen,
6547 raw-byte, or a non-printable character which must be
6548 displayed either as '\003' or as `^C' where the '\\'
6549 and '^' can be defined in the display table. Fill
6550 IT->ctl_chars with glyphs for what we have to
6551 display. Then, set IT->dpvec to these glyphs. */
6552 Lisp_Object gc;
6553 int ctl_len;
6554 int face_id;
6555 EMACS_INT lface_id = 0;
6556 int escape_glyph;
6557
6558 /* Handle control characters with ^. */
6559
6560 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6561 {
6562 int g;
6563
6564 g = '^'; /* default glyph for Control */
6565 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6566 if (it->dp
6567 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6568 && GLYPH_CODE_CHAR_VALID_P (gc))
6569 {
6570 g = GLYPH_CODE_CHAR (gc);
6571 lface_id = GLYPH_CODE_FACE (gc);
6572 }
6573 if (lface_id)
6574 {
6575 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6576 }
6577 else if (it->f == last_escape_glyph_frame
6578 && it->face_id == last_escape_glyph_face_id)
6579 {
6580 face_id = last_escape_glyph_merged_face_id;
6581 }
6582 else
6583 {
6584 /* Merge the escape-glyph face into the current face. */
6585 face_id = merge_faces (it->f, Qescape_glyph, 0,
6586 it->face_id);
6587 last_escape_glyph_frame = it->f;
6588 last_escape_glyph_face_id = it->face_id;
6589 last_escape_glyph_merged_face_id = face_id;
6590 }
6591
6592 XSETINT (it->ctl_chars[0], g);
6593 XSETINT (it->ctl_chars[1], c ^ 0100);
6594 ctl_len = 2;
6595 goto display_control;
6596 }
6597
6598 /* Handle non-ascii space in the mode where it only gets
6599 highlighting. */
6600
6601 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6602 {
6603 /* Merge `nobreak-space' into the current face. */
6604 face_id = merge_faces (it->f, Qnobreak_space, 0,
6605 it->face_id);
6606 XSETINT (it->ctl_chars[0], ' ');
6607 ctl_len = 1;
6608 goto display_control;
6609 }
6610
6611 /* Handle sequences that start with the "escape glyph". */
6612
6613 /* the default escape glyph is \. */
6614 escape_glyph = '\\';
6615
6616 if (it->dp
6617 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6618 && GLYPH_CODE_CHAR_VALID_P (gc))
6619 {
6620 escape_glyph = GLYPH_CODE_CHAR (gc);
6621 lface_id = GLYPH_CODE_FACE (gc);
6622 }
6623 if (lface_id)
6624 {
6625 /* The display table specified a face.
6626 Merge it into face_id and also into escape_glyph. */
6627 face_id = merge_faces (it->f, Qt, lface_id,
6628 it->face_id);
6629 }
6630 else if (it->f == last_escape_glyph_frame
6631 && it->face_id == last_escape_glyph_face_id)
6632 {
6633 face_id = last_escape_glyph_merged_face_id;
6634 }
6635 else
6636 {
6637 /* Merge the escape-glyph face into the current face. */
6638 face_id = merge_faces (it->f, Qescape_glyph, 0,
6639 it->face_id);
6640 last_escape_glyph_frame = it->f;
6641 last_escape_glyph_face_id = it->face_id;
6642 last_escape_glyph_merged_face_id = face_id;
6643 }
6644
6645 /* Draw non-ASCII hyphen with just highlighting: */
6646
6647 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6648 {
6649 XSETINT (it->ctl_chars[0], '-');
6650 ctl_len = 1;
6651 goto display_control;
6652 }
6653
6654 /* Draw non-ASCII space/hyphen with escape glyph: */
6655
6656 if (nonascii_space_p || nonascii_hyphen_p)
6657 {
6658 XSETINT (it->ctl_chars[0], escape_glyph);
6659 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6660 ctl_len = 2;
6661 goto display_control;
6662 }
6663
6664 {
6665 char str[10];
6666 int len, i;
6667
6668 if (CHAR_BYTE8_P (c))
6669 /* Display \200 instead of \17777600. */
6670 c = CHAR_TO_BYTE8 (c);
6671 len = sprintf (str, "%03o", c);
6672
6673 XSETINT (it->ctl_chars[0], escape_glyph);
6674 for (i = 0; i < len; i++)
6675 XSETINT (it->ctl_chars[i + 1], str[i]);
6676 ctl_len = len + 1;
6677 }
6678
6679 display_control:
6680 /* Set up IT->dpvec and return first character from it. */
6681 it->dpvec_char_len = it->len;
6682 it->dpvec = it->ctl_chars;
6683 it->dpend = it->dpvec + ctl_len;
6684 it->current.dpvec_index = 0;
6685 it->dpvec_face_id = face_id;
6686 it->saved_face_id = it->face_id;
6687 it->method = GET_FROM_DISPLAY_VECTOR;
6688 it->ellipsis_p = 0;
6689 goto get_next;
6690 }
6691 it->char_to_display = c;
6692 }
6693 else if (success_p)
6694 {
6695 it->char_to_display = it->c;
6696 }
6697 }
6698
6699 /* Adjust face id for a multibyte character. There are no multibyte
6700 character in unibyte text. */
6701 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6702 && it->multibyte_p
6703 && success_p
6704 && FRAME_WINDOW_P (it->f))
6705 {
6706 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6707
6708 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6709 {
6710 /* Automatic composition with glyph-string. */
6711 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6712
6713 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6714 }
6715 else
6716 {
6717 EMACS_INT pos = (it->s ? -1
6718 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6719 : IT_CHARPOS (*it));
6720 int c;
6721
6722 if (it->what == IT_CHARACTER)
6723 c = it->char_to_display;
6724 else
6725 {
6726 struct composition *cmp = composition_table[it->cmp_it.id];
6727 int i;
6728
6729 c = ' ';
6730 for (i = 0; i < cmp->glyph_len; i++)
6731 /* TAB in a composition means display glyphs with
6732 padding space on the left or right. */
6733 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6734 break;
6735 }
6736 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6737 }
6738 }
6739
6740 done:
6741 /* Is this character the last one of a run of characters with
6742 box? If yes, set IT->end_of_box_run_p to 1. */
6743 if (it->face_box_p
6744 && it->s == NULL)
6745 {
6746 if (it->method == GET_FROM_STRING && it->sp)
6747 {
6748 int face_id = underlying_face_id (it);
6749 struct face *face = FACE_FROM_ID (it->f, face_id);
6750
6751 if (face)
6752 {
6753 if (face->box == FACE_NO_BOX)
6754 {
6755 /* If the box comes from face properties in a
6756 display string, check faces in that string. */
6757 int string_face_id = face_after_it_pos (it);
6758 it->end_of_box_run_p
6759 = (FACE_FROM_ID (it->f, string_face_id)->box
6760 == FACE_NO_BOX);
6761 }
6762 /* Otherwise, the box comes from the underlying face.
6763 If this is the last string character displayed, check
6764 the next buffer location. */
6765 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6766 && (it->current.overlay_string_index
6767 == it->n_overlay_strings - 1))
6768 {
6769 EMACS_INT ignore;
6770 int next_face_id;
6771 struct text_pos pos = it->current.pos;
6772 INC_TEXT_POS (pos, it->multibyte_p);
6773
6774 next_face_id = face_at_buffer_position
6775 (it->w, CHARPOS (pos), it->region_beg_charpos,
6776 it->region_end_charpos, &ignore,
6777 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6778 -1);
6779 it->end_of_box_run_p
6780 = (FACE_FROM_ID (it->f, next_face_id)->box
6781 == FACE_NO_BOX);
6782 }
6783 }
6784 }
6785 else
6786 {
6787 int face_id = face_after_it_pos (it);
6788 it->end_of_box_run_p
6789 = (face_id != it->face_id
6790 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6791 }
6792 }
6793
6794 /* Value is 0 if end of buffer or string reached. */
6795 return success_p;
6796 }
6797
6798
6799 /* Move IT to the next display element.
6800
6801 RESEAT_P non-zero means if called on a newline in buffer text,
6802 skip to the next visible line start.
6803
6804 Functions get_next_display_element and set_iterator_to_next are
6805 separate because I find this arrangement easier to handle than a
6806 get_next_display_element function that also increments IT's
6807 position. The way it is we can first look at an iterator's current
6808 display element, decide whether it fits on a line, and if it does,
6809 increment the iterator position. The other way around we probably
6810 would either need a flag indicating whether the iterator has to be
6811 incremented the next time, or we would have to implement a
6812 decrement position function which would not be easy to write. */
6813
6814 void
6815 set_iterator_to_next (struct it *it, int reseat_p)
6816 {
6817 /* Reset flags indicating start and end of a sequence of characters
6818 with box. Reset them at the start of this function because
6819 moving the iterator to a new position might set them. */
6820 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6821
6822 switch (it->method)
6823 {
6824 case GET_FROM_BUFFER:
6825 /* The current display element of IT is a character from
6826 current_buffer. Advance in the buffer, and maybe skip over
6827 invisible lines that are so because of selective display. */
6828 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6829 reseat_at_next_visible_line_start (it, 0);
6830 else if (it->cmp_it.id >= 0)
6831 {
6832 /* We are currently getting glyphs from a composition. */
6833 int i;
6834
6835 if (! it->bidi_p)
6836 {
6837 IT_CHARPOS (*it) += it->cmp_it.nchars;
6838 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6839 if (it->cmp_it.to < it->cmp_it.nglyphs)
6840 {
6841 it->cmp_it.from = it->cmp_it.to;
6842 }
6843 else
6844 {
6845 it->cmp_it.id = -1;
6846 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6847 IT_BYTEPOS (*it),
6848 it->end_charpos, Qnil);
6849 }
6850 }
6851 else if (! it->cmp_it.reversed_p)
6852 {
6853 /* Composition created while scanning forward. */
6854 /* Update IT's char/byte positions to point to the first
6855 character of the next grapheme cluster, or to the
6856 character visually after the current composition. */
6857 for (i = 0; i < it->cmp_it.nchars; i++)
6858 bidi_move_to_visually_next (&it->bidi_it);
6859 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6860 IT_CHARPOS (*it) = it->bidi_it.charpos;
6861
6862 if (it->cmp_it.to < it->cmp_it.nglyphs)
6863 {
6864 /* Proceed to the next grapheme cluster. */
6865 it->cmp_it.from = it->cmp_it.to;
6866 }
6867 else
6868 {
6869 /* No more grapheme clusters in this composition.
6870 Find the next stop position. */
6871 EMACS_INT stop = it->end_charpos;
6872 if (it->bidi_it.scan_dir < 0)
6873 /* Now we are scanning backward and don't know
6874 where to stop. */
6875 stop = -1;
6876 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6877 IT_BYTEPOS (*it), stop, Qnil);
6878 }
6879 }
6880 else
6881 {
6882 /* Composition created while scanning backward. */
6883 /* Update IT's char/byte positions to point to the last
6884 character of the previous grapheme cluster, or the
6885 character visually after the current composition. */
6886 for (i = 0; i < it->cmp_it.nchars; i++)
6887 bidi_move_to_visually_next (&it->bidi_it);
6888 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6889 IT_CHARPOS (*it) = it->bidi_it.charpos;
6890 if (it->cmp_it.from > 0)
6891 {
6892 /* Proceed to the previous grapheme cluster. */
6893 it->cmp_it.to = it->cmp_it.from;
6894 }
6895 else
6896 {
6897 /* No more grapheme clusters in this composition.
6898 Find the next stop position. */
6899 EMACS_INT stop = it->end_charpos;
6900 if (it->bidi_it.scan_dir < 0)
6901 /* Now we are scanning backward and don't know
6902 where to stop. */
6903 stop = -1;
6904 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6905 IT_BYTEPOS (*it), stop, Qnil);
6906 }
6907 }
6908 }
6909 else
6910 {
6911 xassert (it->len != 0);
6912
6913 if (!it->bidi_p)
6914 {
6915 IT_BYTEPOS (*it) += it->len;
6916 IT_CHARPOS (*it) += 1;
6917 }
6918 else
6919 {
6920 int prev_scan_dir = it->bidi_it.scan_dir;
6921 /* If this is a new paragraph, determine its base
6922 direction (a.k.a. its base embedding level). */
6923 if (it->bidi_it.new_paragraph)
6924 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6925 bidi_move_to_visually_next (&it->bidi_it);
6926 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6927 IT_CHARPOS (*it) = it->bidi_it.charpos;
6928 if (prev_scan_dir != it->bidi_it.scan_dir)
6929 {
6930 /* As the scan direction was changed, we must
6931 re-compute the stop position for composition. */
6932 EMACS_INT stop = it->end_charpos;
6933 if (it->bidi_it.scan_dir < 0)
6934 stop = -1;
6935 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6936 IT_BYTEPOS (*it), stop, Qnil);
6937 }
6938 }
6939 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6940 }
6941 break;
6942
6943 case GET_FROM_C_STRING:
6944 /* Current display element of IT is from a C string. */
6945 if (!it->bidi_p
6946 /* If the string position is beyond string's end, it means
6947 next_element_from_c_string is padding the string with
6948 blanks, in which case we bypass the bidi iterator,
6949 because it cannot deal with such virtual characters. */
6950 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6951 {
6952 IT_BYTEPOS (*it) += it->len;
6953 IT_CHARPOS (*it) += 1;
6954 }
6955 else
6956 {
6957 bidi_move_to_visually_next (&it->bidi_it);
6958 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6959 IT_CHARPOS (*it) = it->bidi_it.charpos;
6960 }
6961 break;
6962
6963 case GET_FROM_DISPLAY_VECTOR:
6964 /* Current display element of IT is from a display table entry.
6965 Advance in the display table definition. Reset it to null if
6966 end reached, and continue with characters from buffers/
6967 strings. */
6968 ++it->current.dpvec_index;
6969
6970 /* Restore face of the iterator to what they were before the
6971 display vector entry (these entries may contain faces). */
6972 it->face_id = it->saved_face_id;
6973
6974 if (it->dpvec + it->current.dpvec_index == it->dpend)
6975 {
6976 int recheck_faces = it->ellipsis_p;
6977
6978 if (it->s)
6979 it->method = GET_FROM_C_STRING;
6980 else if (STRINGP (it->string))
6981 it->method = GET_FROM_STRING;
6982 else
6983 {
6984 it->method = GET_FROM_BUFFER;
6985 it->object = it->w->buffer;
6986 }
6987
6988 it->dpvec = NULL;
6989 it->current.dpvec_index = -1;
6990
6991 /* Skip over characters which were displayed via IT->dpvec. */
6992 if (it->dpvec_char_len < 0)
6993 reseat_at_next_visible_line_start (it, 1);
6994 else if (it->dpvec_char_len > 0)
6995 {
6996 if (it->method == GET_FROM_STRING
6997 && it->n_overlay_strings > 0)
6998 it->ignore_overlay_strings_at_pos_p = 1;
6999 it->len = it->dpvec_char_len;
7000 set_iterator_to_next (it, reseat_p);
7001 }
7002
7003 /* Maybe recheck faces after display vector */
7004 if (recheck_faces)
7005 it->stop_charpos = IT_CHARPOS (*it);
7006 }
7007 break;
7008
7009 case GET_FROM_STRING:
7010 /* Current display element is a character from a Lisp string. */
7011 xassert (it->s == NULL && STRINGP (it->string));
7012 if (it->cmp_it.id >= 0)
7013 {
7014 int i;
7015
7016 if (! it->bidi_p)
7017 {
7018 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7019 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7020 if (it->cmp_it.to < it->cmp_it.nglyphs)
7021 it->cmp_it.from = it->cmp_it.to;
7022 else
7023 {
7024 it->cmp_it.id = -1;
7025 composition_compute_stop_pos (&it->cmp_it,
7026 IT_STRING_CHARPOS (*it),
7027 IT_STRING_BYTEPOS (*it),
7028 it->end_charpos, it->string);
7029 }
7030 }
7031 else if (! it->cmp_it.reversed_p)
7032 {
7033 for (i = 0; i < it->cmp_it.nchars; i++)
7034 bidi_move_to_visually_next (&it->bidi_it);
7035 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7036 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7037
7038 if (it->cmp_it.to < it->cmp_it.nglyphs)
7039 it->cmp_it.from = it->cmp_it.to;
7040 else
7041 {
7042 EMACS_INT stop = it->end_charpos;
7043 if (it->bidi_it.scan_dir < 0)
7044 stop = -1;
7045 composition_compute_stop_pos (&it->cmp_it,
7046 IT_STRING_CHARPOS (*it),
7047 IT_STRING_BYTEPOS (*it), stop,
7048 it->string);
7049 }
7050 }
7051 else
7052 {
7053 for (i = 0; i < it->cmp_it.nchars; i++)
7054 bidi_move_to_visually_next (&it->bidi_it);
7055 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7056 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7057 if (it->cmp_it.from > 0)
7058 it->cmp_it.to = it->cmp_it.from;
7059 else
7060 {
7061 EMACS_INT stop = it->end_charpos;
7062 if (it->bidi_it.scan_dir < 0)
7063 stop = -1;
7064 composition_compute_stop_pos (&it->cmp_it,
7065 IT_STRING_CHARPOS (*it),
7066 IT_STRING_BYTEPOS (*it), stop,
7067 it->string);
7068 }
7069 }
7070 }
7071 else
7072 {
7073 if (!it->bidi_p
7074 /* If the string position is beyond string's end, it
7075 means next_element_from_string is padding the string
7076 with blanks, in which case we bypass the bidi
7077 iterator, because it cannot deal with such virtual
7078 characters. */
7079 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7080 {
7081 IT_STRING_BYTEPOS (*it) += it->len;
7082 IT_STRING_CHARPOS (*it) += 1;
7083 }
7084 else
7085 {
7086 int prev_scan_dir = it->bidi_it.scan_dir;
7087
7088 bidi_move_to_visually_next (&it->bidi_it);
7089 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7090 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7091 if (prev_scan_dir != it->bidi_it.scan_dir)
7092 {
7093 EMACS_INT stop = it->end_charpos;
7094
7095 if (it->bidi_it.scan_dir < 0)
7096 stop = -1;
7097 composition_compute_stop_pos (&it->cmp_it,
7098 IT_STRING_CHARPOS (*it),
7099 IT_STRING_BYTEPOS (*it), stop,
7100 it->string);
7101 }
7102 }
7103 }
7104
7105 consider_string_end:
7106
7107 if (it->current.overlay_string_index >= 0)
7108 {
7109 /* IT->string is an overlay string. Advance to the
7110 next, if there is one. */
7111 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7112 {
7113 it->ellipsis_p = 0;
7114 next_overlay_string (it);
7115 if (it->ellipsis_p)
7116 setup_for_ellipsis (it, 0);
7117 }
7118 }
7119 else
7120 {
7121 /* IT->string is not an overlay string. If we reached
7122 its end, and there is something on IT->stack, proceed
7123 with what is on the stack. This can be either another
7124 string, this time an overlay string, or a buffer. */
7125 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7126 && it->sp > 0)
7127 {
7128 pop_it (it);
7129 if (it->method == GET_FROM_STRING)
7130 goto consider_string_end;
7131 }
7132 }
7133 break;
7134
7135 case GET_FROM_IMAGE:
7136 case GET_FROM_STRETCH:
7137 /* The position etc with which we have to proceed are on
7138 the stack. The position may be at the end of a string,
7139 if the `display' property takes up the whole string. */
7140 xassert (it->sp > 0);
7141 pop_it (it);
7142 if (it->method == GET_FROM_STRING)
7143 goto consider_string_end;
7144 break;
7145
7146 default:
7147 /* There are no other methods defined, so this should be a bug. */
7148 abort ();
7149 }
7150
7151 xassert (it->method != GET_FROM_STRING
7152 || (STRINGP (it->string)
7153 && IT_STRING_CHARPOS (*it) >= 0));
7154 }
7155
7156 /* Load IT's display element fields with information about the next
7157 display element which comes from a display table entry or from the
7158 result of translating a control character to one of the forms `^C'
7159 or `\003'.
7160
7161 IT->dpvec holds the glyphs to return as characters.
7162 IT->saved_face_id holds the face id before the display vector--it
7163 is restored into IT->face_id in set_iterator_to_next. */
7164
7165 static int
7166 next_element_from_display_vector (struct it *it)
7167 {
7168 Lisp_Object gc;
7169
7170 /* Precondition. */
7171 xassert (it->dpvec && it->current.dpvec_index >= 0);
7172
7173 it->face_id = it->saved_face_id;
7174
7175 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7176 That seemed totally bogus - so I changed it... */
7177 gc = it->dpvec[it->current.dpvec_index];
7178
7179 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7180 {
7181 it->c = GLYPH_CODE_CHAR (gc);
7182 it->len = CHAR_BYTES (it->c);
7183
7184 /* The entry may contain a face id to use. Such a face id is
7185 the id of a Lisp face, not a realized face. A face id of
7186 zero means no face is specified. */
7187 if (it->dpvec_face_id >= 0)
7188 it->face_id = it->dpvec_face_id;
7189 else
7190 {
7191 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7192 if (lface_id > 0)
7193 it->face_id = merge_faces (it->f, Qt, lface_id,
7194 it->saved_face_id);
7195 }
7196 }
7197 else
7198 /* Display table entry is invalid. Return a space. */
7199 it->c = ' ', it->len = 1;
7200
7201 /* Don't change position and object of the iterator here. They are
7202 still the values of the character that had this display table
7203 entry or was translated, and that's what we want. */
7204 it->what = IT_CHARACTER;
7205 return 1;
7206 }
7207
7208 /* Get the first element of string/buffer in the visual order, after
7209 being reseated to a new position in a string or a buffer. */
7210 static void
7211 get_visually_first_element (struct it *it)
7212 {
7213 int string_p = STRINGP (it->string) || it->s;
7214 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7215 EMACS_INT bob = (string_p ? 0 : BEGV);
7216
7217 if (STRINGP (it->string))
7218 {
7219 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7220 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7221 }
7222 else
7223 {
7224 it->bidi_it.charpos = IT_CHARPOS (*it);
7225 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7226 }
7227
7228 if (it->bidi_it.charpos == eob)
7229 {
7230 /* Nothing to do, but reset the FIRST_ELT flag, like
7231 bidi_paragraph_init does, because we are not going to
7232 call it. */
7233 it->bidi_it.first_elt = 0;
7234 }
7235 else if (it->bidi_it.charpos == bob
7236 || (!string_p
7237 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7238 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7239 {
7240 /* If we are at the beginning of a line/string, we can produce
7241 the next element right away. */
7242 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7243 bidi_move_to_visually_next (&it->bidi_it);
7244 }
7245 else
7246 {
7247 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7248
7249 /* We need to prime the bidi iterator starting at the line's or
7250 string's beginning, before we will be able to produce the
7251 next element. */
7252 if (string_p)
7253 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7254 else
7255 {
7256 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7257 -1);
7258 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7259 }
7260 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7261 do
7262 {
7263 /* Now return to buffer/string position where we were asked
7264 to get the next display element, and produce that. */
7265 bidi_move_to_visually_next (&it->bidi_it);
7266 }
7267 while (it->bidi_it.bytepos != orig_bytepos
7268 && it->bidi_it.charpos < eob);
7269 }
7270
7271 /* Adjust IT's position information to where we ended up. */
7272 if (STRINGP (it->string))
7273 {
7274 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7275 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7276 }
7277 else
7278 {
7279 IT_CHARPOS (*it) = it->bidi_it.charpos;
7280 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7281 }
7282
7283 if (STRINGP (it->string) || !it->s)
7284 {
7285 EMACS_INT stop, charpos, bytepos;
7286
7287 if (STRINGP (it->string))
7288 {
7289 xassert (!it->s);
7290 stop = SCHARS (it->string);
7291 if (stop > it->end_charpos)
7292 stop = it->end_charpos;
7293 charpos = IT_STRING_CHARPOS (*it);
7294 bytepos = IT_STRING_BYTEPOS (*it);
7295 }
7296 else
7297 {
7298 stop = it->end_charpos;
7299 charpos = IT_CHARPOS (*it);
7300 bytepos = IT_BYTEPOS (*it);
7301 }
7302 if (it->bidi_it.scan_dir < 0)
7303 stop = -1;
7304 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7305 it->string);
7306 }
7307 }
7308
7309 /* Load IT with the next display element from Lisp string IT->string.
7310 IT->current.string_pos is the current position within the string.
7311 If IT->current.overlay_string_index >= 0, the Lisp string is an
7312 overlay string. */
7313
7314 static int
7315 next_element_from_string (struct it *it)
7316 {
7317 struct text_pos position;
7318
7319 xassert (STRINGP (it->string));
7320 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7321 xassert (IT_STRING_CHARPOS (*it) >= 0);
7322 position = it->current.string_pos;
7323
7324 /* With bidi reordering, the character to display might not be the
7325 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7326 that we were reseat()ed to a new string, whose paragraph
7327 direction is not known. */
7328 if (it->bidi_p && it->bidi_it.first_elt)
7329 {
7330 get_visually_first_element (it);
7331 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7332 }
7333
7334 /* Time to check for invisible text? */
7335 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7336 {
7337 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7338 {
7339 if (!(!it->bidi_p
7340 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7341 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7342 {
7343 /* With bidi non-linear iteration, we could find
7344 ourselves far beyond the last computed stop_charpos,
7345 with several other stop positions in between that we
7346 missed. Scan them all now, in buffer's logical
7347 order, until we find and handle the last stop_charpos
7348 that precedes our current position. */
7349 handle_stop_backwards (it, it->stop_charpos);
7350 return GET_NEXT_DISPLAY_ELEMENT (it);
7351 }
7352 else
7353 {
7354 if (it->bidi_p)
7355 {
7356 /* Take note of the stop position we just moved
7357 across, for when we will move back across it. */
7358 it->prev_stop = it->stop_charpos;
7359 /* If we are at base paragraph embedding level, take
7360 note of the last stop position seen at this
7361 level. */
7362 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7363 it->base_level_stop = it->stop_charpos;
7364 }
7365 handle_stop (it);
7366
7367 /* Since a handler may have changed IT->method, we must
7368 recurse here. */
7369 return GET_NEXT_DISPLAY_ELEMENT (it);
7370 }
7371 }
7372 else if (it->bidi_p
7373 /* If we are before prev_stop, we may have overstepped
7374 on our way backwards a stop_pos, and if so, we need
7375 to handle that stop_pos. */
7376 && IT_STRING_CHARPOS (*it) < it->prev_stop
7377 /* We can sometimes back up for reasons that have nothing
7378 to do with bidi reordering. E.g., compositions. The
7379 code below is only needed when we are above the base
7380 embedding level, so test for that explicitly. */
7381 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7382 {
7383 /* If we lost track of base_level_stop, we have no better
7384 place for handle_stop_backwards to start from than string
7385 beginning. This happens, e.g., when we were reseated to
7386 the previous screenful of text by vertical-motion. */
7387 if (it->base_level_stop <= 0
7388 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7389 it->base_level_stop = 0;
7390 handle_stop_backwards (it, it->base_level_stop);
7391 return GET_NEXT_DISPLAY_ELEMENT (it);
7392 }
7393 }
7394
7395 if (it->current.overlay_string_index >= 0)
7396 {
7397 /* Get the next character from an overlay string. In overlay
7398 strings, there is no field width or padding with spaces to
7399 do. */
7400 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7401 {
7402 it->what = IT_EOB;
7403 return 0;
7404 }
7405 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7406 IT_STRING_BYTEPOS (*it),
7407 it->bidi_it.scan_dir < 0
7408 ? -1
7409 : SCHARS (it->string))
7410 && next_element_from_composition (it))
7411 {
7412 return 1;
7413 }
7414 else if (STRING_MULTIBYTE (it->string))
7415 {
7416 const unsigned char *s = (SDATA (it->string)
7417 + IT_STRING_BYTEPOS (*it));
7418 it->c = string_char_and_length (s, &it->len);
7419 }
7420 else
7421 {
7422 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7423 it->len = 1;
7424 }
7425 }
7426 else
7427 {
7428 /* Get the next character from a Lisp string that is not an
7429 overlay string. Such strings come from the mode line, for
7430 example. We may have to pad with spaces, or truncate the
7431 string. See also next_element_from_c_string. */
7432 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7433 {
7434 it->what = IT_EOB;
7435 return 0;
7436 }
7437 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7438 {
7439 /* Pad with spaces. */
7440 it->c = ' ', it->len = 1;
7441 CHARPOS (position) = BYTEPOS (position) = -1;
7442 }
7443 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7444 IT_STRING_BYTEPOS (*it),
7445 it->bidi_it.scan_dir < 0
7446 ? -1
7447 : it->string_nchars)
7448 && next_element_from_composition (it))
7449 {
7450 return 1;
7451 }
7452 else if (STRING_MULTIBYTE (it->string))
7453 {
7454 const unsigned char *s = (SDATA (it->string)
7455 + IT_STRING_BYTEPOS (*it));
7456 it->c = string_char_and_length (s, &it->len);
7457 }
7458 else
7459 {
7460 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7461 it->len = 1;
7462 }
7463 }
7464
7465 /* Record what we have and where it came from. */
7466 it->what = IT_CHARACTER;
7467 it->object = it->string;
7468 it->position = position;
7469 return 1;
7470 }
7471
7472
7473 /* Load IT with next display element from C string IT->s.
7474 IT->string_nchars is the maximum number of characters to return
7475 from the string. IT->end_charpos may be greater than
7476 IT->string_nchars when this function is called, in which case we
7477 may have to return padding spaces. Value is zero if end of string
7478 reached, including padding spaces. */
7479
7480 static int
7481 next_element_from_c_string (struct it *it)
7482 {
7483 int success_p = 1;
7484
7485 xassert (it->s);
7486 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7487 it->what = IT_CHARACTER;
7488 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7489 it->object = Qnil;
7490
7491 /* With bidi reordering, the character to display might not be the
7492 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7493 we were reseated to a new string, whose paragraph direction is
7494 not known. */
7495 if (it->bidi_p && it->bidi_it.first_elt)
7496 get_visually_first_element (it);
7497
7498 /* IT's position can be greater than IT->string_nchars in case a
7499 field width or precision has been specified when the iterator was
7500 initialized. */
7501 if (IT_CHARPOS (*it) >= it->end_charpos)
7502 {
7503 /* End of the game. */
7504 it->what = IT_EOB;
7505 success_p = 0;
7506 }
7507 else if (IT_CHARPOS (*it) >= it->string_nchars)
7508 {
7509 /* Pad with spaces. */
7510 it->c = ' ', it->len = 1;
7511 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7512 }
7513 else if (it->multibyte_p)
7514 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7515 else
7516 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7517
7518 return success_p;
7519 }
7520
7521
7522 /* Set up IT to return characters from an ellipsis, if appropriate.
7523 The definition of the ellipsis glyphs may come from a display table
7524 entry. This function fills IT with the first glyph from the
7525 ellipsis if an ellipsis is to be displayed. */
7526
7527 static int
7528 next_element_from_ellipsis (struct it *it)
7529 {
7530 if (it->selective_display_ellipsis_p)
7531 setup_for_ellipsis (it, it->len);
7532 else
7533 {
7534 /* The face at the current position may be different from the
7535 face we find after the invisible text. Remember what it
7536 was in IT->saved_face_id, and signal that it's there by
7537 setting face_before_selective_p. */
7538 it->saved_face_id = it->face_id;
7539 it->method = GET_FROM_BUFFER;
7540 it->object = it->w->buffer;
7541 reseat_at_next_visible_line_start (it, 1);
7542 it->face_before_selective_p = 1;
7543 }
7544
7545 return GET_NEXT_DISPLAY_ELEMENT (it);
7546 }
7547
7548
7549 /* Deliver an image display element. The iterator IT is already
7550 filled with image information (done in handle_display_prop). Value
7551 is always 1. */
7552
7553
7554 static int
7555 next_element_from_image (struct it *it)
7556 {
7557 it->what = IT_IMAGE;
7558 it->ignore_overlay_strings_at_pos_p = 0;
7559 return 1;
7560 }
7561
7562
7563 /* Fill iterator IT with next display element from a stretch glyph
7564 property. IT->object is the value of the text property. Value is
7565 always 1. */
7566
7567 static int
7568 next_element_from_stretch (struct it *it)
7569 {
7570 it->what = IT_STRETCH;
7571 return 1;
7572 }
7573
7574 /* Scan backwards from IT's current position until we find a stop
7575 position, or until BEGV. This is called when we find ourself
7576 before both the last known prev_stop and base_level_stop while
7577 reordering bidirectional text. */
7578
7579 static void
7580 compute_stop_pos_backwards (struct it *it)
7581 {
7582 const int SCAN_BACK_LIMIT = 1000;
7583 struct text_pos pos;
7584 struct display_pos save_current = it->current;
7585 struct text_pos save_position = it->position;
7586 EMACS_INT charpos = IT_CHARPOS (*it);
7587 EMACS_INT where_we_are = charpos;
7588 EMACS_INT save_stop_pos = it->stop_charpos;
7589 EMACS_INT save_end_pos = it->end_charpos;
7590
7591 xassert (NILP (it->string) && !it->s);
7592 xassert (it->bidi_p);
7593 it->bidi_p = 0;
7594 do
7595 {
7596 it->end_charpos = min (charpos + 1, ZV);
7597 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7598 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7599 reseat_1 (it, pos, 0);
7600 compute_stop_pos (it);
7601 /* We must advance forward, right? */
7602 if (it->stop_charpos <= charpos)
7603 abort ();
7604 }
7605 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7606
7607 if (it->stop_charpos <= where_we_are)
7608 it->prev_stop = it->stop_charpos;
7609 else
7610 it->prev_stop = BEGV;
7611 it->bidi_p = 1;
7612 it->current = save_current;
7613 it->position = save_position;
7614 it->stop_charpos = save_stop_pos;
7615 it->end_charpos = save_end_pos;
7616 }
7617
7618 /* Scan forward from CHARPOS in the current buffer/string, until we
7619 find a stop position > current IT's position. Then handle the stop
7620 position before that. This is called when we bump into a stop
7621 position while reordering bidirectional text. CHARPOS should be
7622 the last previously processed stop_pos (or BEGV/0, if none were
7623 processed yet) whose position is less that IT's current
7624 position. */
7625
7626 static void
7627 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7628 {
7629 int bufp = !STRINGP (it->string);
7630 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7631 struct display_pos save_current = it->current;
7632 struct text_pos save_position = it->position;
7633 struct text_pos pos1;
7634 EMACS_INT next_stop;
7635
7636 /* Scan in strict logical order. */
7637 xassert (it->bidi_p);
7638 it->bidi_p = 0;
7639 do
7640 {
7641 it->prev_stop = charpos;
7642 if (bufp)
7643 {
7644 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7645 reseat_1 (it, pos1, 0);
7646 }
7647 else
7648 it->current.string_pos = string_pos (charpos, it->string);
7649 compute_stop_pos (it);
7650 /* We must advance forward, right? */
7651 if (it->stop_charpos <= it->prev_stop)
7652 abort ();
7653 charpos = it->stop_charpos;
7654 }
7655 while (charpos <= where_we_are);
7656
7657 it->bidi_p = 1;
7658 it->current = save_current;
7659 it->position = save_position;
7660 next_stop = it->stop_charpos;
7661 it->stop_charpos = it->prev_stop;
7662 handle_stop (it);
7663 it->stop_charpos = next_stop;
7664 }
7665
7666 /* Load IT with the next display element from current_buffer. Value
7667 is zero if end of buffer reached. IT->stop_charpos is the next
7668 position at which to stop and check for text properties or buffer
7669 end. */
7670
7671 static int
7672 next_element_from_buffer (struct it *it)
7673 {
7674 int success_p = 1;
7675
7676 xassert (IT_CHARPOS (*it) >= BEGV);
7677 xassert (NILP (it->string) && !it->s);
7678 xassert (!it->bidi_p
7679 || (EQ (it->bidi_it.string.lstring, Qnil)
7680 && it->bidi_it.string.s == NULL));
7681
7682 /* With bidi reordering, the character to display might not be the
7683 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7684 we were reseat()ed to a new buffer position, which is potentially
7685 a different paragraph. */
7686 if (it->bidi_p && it->bidi_it.first_elt)
7687 {
7688 get_visually_first_element (it);
7689 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7690 }
7691
7692 if (IT_CHARPOS (*it) >= it->stop_charpos)
7693 {
7694 if (IT_CHARPOS (*it) >= it->end_charpos)
7695 {
7696 int overlay_strings_follow_p;
7697
7698 /* End of the game, except when overlay strings follow that
7699 haven't been returned yet. */
7700 if (it->overlay_strings_at_end_processed_p)
7701 overlay_strings_follow_p = 0;
7702 else
7703 {
7704 it->overlay_strings_at_end_processed_p = 1;
7705 overlay_strings_follow_p = get_overlay_strings (it, 0);
7706 }
7707
7708 if (overlay_strings_follow_p)
7709 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7710 else
7711 {
7712 it->what = IT_EOB;
7713 it->position = it->current.pos;
7714 success_p = 0;
7715 }
7716 }
7717 else if (!(!it->bidi_p
7718 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7719 || IT_CHARPOS (*it) == it->stop_charpos))
7720 {
7721 /* With bidi non-linear iteration, we could find ourselves
7722 far beyond the last computed stop_charpos, with several
7723 other stop positions in between that we missed. Scan
7724 them all now, in buffer's logical order, until we find
7725 and handle the last stop_charpos that precedes our
7726 current position. */
7727 handle_stop_backwards (it, it->stop_charpos);
7728 return GET_NEXT_DISPLAY_ELEMENT (it);
7729 }
7730 else
7731 {
7732 if (it->bidi_p)
7733 {
7734 /* Take note of the stop position we just moved across,
7735 for when we will move back across it. */
7736 it->prev_stop = it->stop_charpos;
7737 /* If we are at base paragraph embedding level, take
7738 note of the last stop position seen at this
7739 level. */
7740 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7741 it->base_level_stop = it->stop_charpos;
7742 }
7743 handle_stop (it);
7744 return GET_NEXT_DISPLAY_ELEMENT (it);
7745 }
7746 }
7747 else if (it->bidi_p
7748 /* If we are before prev_stop, we may have overstepped on
7749 our way backwards a stop_pos, and if so, we need to
7750 handle that stop_pos. */
7751 && IT_CHARPOS (*it) < it->prev_stop
7752 /* We can sometimes back up for reasons that have nothing
7753 to do with bidi reordering. E.g., compositions. The
7754 code below is only needed when we are above the base
7755 embedding level, so test for that explicitly. */
7756 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7757 {
7758 if (it->base_level_stop <= 0
7759 || IT_CHARPOS (*it) < it->base_level_stop)
7760 {
7761 /* If we lost track of base_level_stop, we need to find
7762 prev_stop by looking backwards. This happens, e.g., when
7763 we were reseated to the previous screenful of text by
7764 vertical-motion. */
7765 it->base_level_stop = BEGV;
7766 compute_stop_pos_backwards (it);
7767 handle_stop_backwards (it, it->prev_stop);
7768 }
7769 else
7770 handle_stop_backwards (it, it->base_level_stop);
7771 return GET_NEXT_DISPLAY_ELEMENT (it);
7772 }
7773 else
7774 {
7775 /* No face changes, overlays etc. in sight, so just return a
7776 character from current_buffer. */
7777 unsigned char *p;
7778 EMACS_INT stop;
7779
7780 /* Maybe run the redisplay end trigger hook. Performance note:
7781 This doesn't seem to cost measurable time. */
7782 if (it->redisplay_end_trigger_charpos
7783 && it->glyph_row
7784 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7785 run_redisplay_end_trigger_hook (it);
7786
7787 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7788 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7789 stop)
7790 && next_element_from_composition (it))
7791 {
7792 return 1;
7793 }
7794
7795 /* Get the next character, maybe multibyte. */
7796 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7797 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7798 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7799 else
7800 it->c = *p, it->len = 1;
7801
7802 /* Record what we have and where it came from. */
7803 it->what = IT_CHARACTER;
7804 it->object = it->w->buffer;
7805 it->position = it->current.pos;
7806
7807 /* Normally we return the character found above, except when we
7808 really want to return an ellipsis for selective display. */
7809 if (it->selective)
7810 {
7811 if (it->c == '\n')
7812 {
7813 /* A value of selective > 0 means hide lines indented more
7814 than that number of columns. */
7815 if (it->selective > 0
7816 && IT_CHARPOS (*it) + 1 < ZV
7817 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7818 IT_BYTEPOS (*it) + 1,
7819 it->selective))
7820 {
7821 success_p = next_element_from_ellipsis (it);
7822 it->dpvec_char_len = -1;
7823 }
7824 }
7825 else if (it->c == '\r' && it->selective == -1)
7826 {
7827 /* A value of selective == -1 means that everything from the
7828 CR to the end of the line is invisible, with maybe an
7829 ellipsis displayed for it. */
7830 success_p = next_element_from_ellipsis (it);
7831 it->dpvec_char_len = -1;
7832 }
7833 }
7834 }
7835
7836 /* Value is zero if end of buffer reached. */
7837 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7838 return success_p;
7839 }
7840
7841
7842 /* Run the redisplay end trigger hook for IT. */
7843
7844 static void
7845 run_redisplay_end_trigger_hook (struct it *it)
7846 {
7847 Lisp_Object args[3];
7848
7849 /* IT->glyph_row should be non-null, i.e. we should be actually
7850 displaying something, or otherwise we should not run the hook. */
7851 xassert (it->glyph_row);
7852
7853 /* Set up hook arguments. */
7854 args[0] = Qredisplay_end_trigger_functions;
7855 args[1] = it->window;
7856 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7857 it->redisplay_end_trigger_charpos = 0;
7858
7859 /* Since we are *trying* to run these functions, don't try to run
7860 them again, even if they get an error. */
7861 it->w->redisplay_end_trigger = Qnil;
7862 Frun_hook_with_args (3, args);
7863
7864 /* Notice if it changed the face of the character we are on. */
7865 handle_face_prop (it);
7866 }
7867
7868
7869 /* Deliver a composition display element. Unlike the other
7870 next_element_from_XXX, this function is not registered in the array
7871 get_next_element[]. It is called from next_element_from_buffer and
7872 next_element_from_string when necessary. */
7873
7874 static int
7875 next_element_from_composition (struct it *it)
7876 {
7877 it->what = IT_COMPOSITION;
7878 it->len = it->cmp_it.nbytes;
7879 if (STRINGP (it->string))
7880 {
7881 if (it->c < 0)
7882 {
7883 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7884 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7885 return 0;
7886 }
7887 it->position = it->current.string_pos;
7888 it->object = it->string;
7889 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7890 IT_STRING_BYTEPOS (*it), it->string);
7891 }
7892 else
7893 {
7894 if (it->c < 0)
7895 {
7896 IT_CHARPOS (*it) += it->cmp_it.nchars;
7897 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7898 if (it->bidi_p)
7899 {
7900 if (it->bidi_it.new_paragraph)
7901 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7902 /* Resync the bidi iterator with IT's new position.
7903 FIXME: this doesn't support bidirectional text. */
7904 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7905 bidi_move_to_visually_next (&it->bidi_it);
7906 }
7907 return 0;
7908 }
7909 it->position = it->current.pos;
7910 it->object = it->w->buffer;
7911 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7912 IT_BYTEPOS (*it), Qnil);
7913 }
7914 return 1;
7915 }
7916
7917
7918 \f
7919 /***********************************************************************
7920 Moving an iterator without producing glyphs
7921 ***********************************************************************/
7922
7923 /* Check if iterator is at a position corresponding to a valid buffer
7924 position after some move_it_ call. */
7925
7926 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7927 ((it)->method == GET_FROM_STRING \
7928 ? IT_STRING_CHARPOS (*it) == 0 \
7929 : 1)
7930
7931
7932 /* Move iterator IT to a specified buffer or X position within one
7933 line on the display without producing glyphs.
7934
7935 OP should be a bit mask including some or all of these bits:
7936 MOVE_TO_X: Stop upon reaching x-position TO_X.
7937 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7938 Regardless of OP's value, stop upon reaching the end of the display line.
7939
7940 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7941 This means, in particular, that TO_X includes window's horizontal
7942 scroll amount.
7943
7944 The return value has several possible values that
7945 say what condition caused the scan to stop:
7946
7947 MOVE_POS_MATCH_OR_ZV
7948 - when TO_POS or ZV was reached.
7949
7950 MOVE_X_REACHED
7951 -when TO_X was reached before TO_POS or ZV were reached.
7952
7953 MOVE_LINE_CONTINUED
7954 - when we reached the end of the display area and the line must
7955 be continued.
7956
7957 MOVE_LINE_TRUNCATED
7958 - when we reached the end of the display area and the line is
7959 truncated.
7960
7961 MOVE_NEWLINE_OR_CR
7962 - when we stopped at a line end, i.e. a newline or a CR and selective
7963 display is on. */
7964
7965 static enum move_it_result
7966 move_it_in_display_line_to (struct it *it,
7967 EMACS_INT to_charpos, int to_x,
7968 enum move_operation_enum op)
7969 {
7970 enum move_it_result result = MOVE_UNDEFINED;
7971 struct glyph_row *saved_glyph_row;
7972 struct it wrap_it, atpos_it, atx_it, ppos_it;
7973 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7974 void *ppos_data = NULL;
7975 int may_wrap = 0;
7976 enum it_method prev_method = it->method;
7977 EMACS_INT prev_pos = IT_CHARPOS (*it);
7978 int saw_smaller_pos = prev_pos < to_charpos;
7979
7980 /* Don't produce glyphs in produce_glyphs. */
7981 saved_glyph_row = it->glyph_row;
7982 it->glyph_row = NULL;
7983
7984 /* Use wrap_it to save a copy of IT wherever a word wrap could
7985 occur. Use atpos_it to save a copy of IT at the desired buffer
7986 position, if found, so that we can scan ahead and check if the
7987 word later overshoots the window edge. Use atx_it similarly, for
7988 pixel positions. */
7989 wrap_it.sp = -1;
7990 atpos_it.sp = -1;
7991 atx_it.sp = -1;
7992
7993 /* Use ppos_it under bidi reordering to save a copy of IT for the
7994 position > CHARPOS that is the closest to CHARPOS. We restore
7995 that position in IT when we have scanned the entire display line
7996 without finding a match for CHARPOS and all the character
7997 positions are greater than CHARPOS. */
7998 if (it->bidi_p)
7999 {
8000 SAVE_IT (ppos_it, *it, ppos_data);
8001 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8002 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8003 SAVE_IT (ppos_it, *it, ppos_data);
8004 }
8005
8006 #define BUFFER_POS_REACHED_P() \
8007 ((op & MOVE_TO_POS) != 0 \
8008 && BUFFERP (it->object) \
8009 && (IT_CHARPOS (*it) == to_charpos \
8010 || ((!it->bidi_p \
8011 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8012 && IT_CHARPOS (*it) > to_charpos) \
8013 || (it->what == IT_COMPOSITION \
8014 && ((IT_CHARPOS (*it) > to_charpos \
8015 && to_charpos >= it->cmp_it.charpos) \
8016 || (IT_CHARPOS (*it) < to_charpos \
8017 && to_charpos <= it->cmp_it.charpos)))) \
8018 && (it->method == GET_FROM_BUFFER \
8019 || (it->method == GET_FROM_DISPLAY_VECTOR \
8020 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8021
8022 /* If there's a line-/wrap-prefix, handle it. */
8023 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8024 && it->current_y < it->last_visible_y)
8025 handle_line_prefix (it);
8026
8027 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8028 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8029
8030 while (1)
8031 {
8032 int x, i, ascent = 0, descent = 0;
8033
8034 /* Utility macro to reset an iterator with x, ascent, and descent. */
8035 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8036 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8037 (IT)->max_descent = descent)
8038
8039 /* Stop if we move beyond TO_CHARPOS (after an image or a
8040 display string or stretch glyph). */
8041 if ((op & MOVE_TO_POS) != 0
8042 && BUFFERP (it->object)
8043 && it->method == GET_FROM_BUFFER
8044 && (((!it->bidi_p
8045 /* When the iterator is at base embedding level, we
8046 are guaranteed that characters are delivered for
8047 display in strictly increasing order of their
8048 buffer positions. */
8049 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8050 && IT_CHARPOS (*it) > to_charpos)
8051 || (it->bidi_p
8052 && (prev_method == GET_FROM_IMAGE
8053 || prev_method == GET_FROM_STRETCH
8054 || prev_method == GET_FROM_STRING)
8055 /* Passed TO_CHARPOS from left to right. */
8056 && ((prev_pos < to_charpos
8057 && IT_CHARPOS (*it) > to_charpos)
8058 /* Passed TO_CHARPOS from right to left. */
8059 || (prev_pos > to_charpos
8060 && IT_CHARPOS (*it) < to_charpos)))))
8061 {
8062 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8063 {
8064 result = MOVE_POS_MATCH_OR_ZV;
8065 break;
8066 }
8067 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8068 /* If wrap_it is valid, the current position might be in a
8069 word that is wrapped. So, save the iterator in
8070 atpos_it and continue to see if wrapping happens. */
8071 SAVE_IT (atpos_it, *it, atpos_data);
8072 }
8073
8074 /* Stop when ZV reached.
8075 We used to stop here when TO_CHARPOS reached as well, but that is
8076 too soon if this glyph does not fit on this line. So we handle it
8077 explicitly below. */
8078 if (!get_next_display_element (it))
8079 {
8080 result = MOVE_POS_MATCH_OR_ZV;
8081 break;
8082 }
8083
8084 if (it->line_wrap == TRUNCATE)
8085 {
8086 if (BUFFER_POS_REACHED_P ())
8087 {
8088 result = MOVE_POS_MATCH_OR_ZV;
8089 break;
8090 }
8091 }
8092 else
8093 {
8094 if (it->line_wrap == WORD_WRAP)
8095 {
8096 if (IT_DISPLAYING_WHITESPACE (it))
8097 may_wrap = 1;
8098 else if (may_wrap)
8099 {
8100 /* We have reached a glyph that follows one or more
8101 whitespace characters. If the position is
8102 already found, we are done. */
8103 if (atpos_it.sp >= 0)
8104 {
8105 RESTORE_IT (it, &atpos_it, atpos_data);
8106 result = MOVE_POS_MATCH_OR_ZV;
8107 goto done;
8108 }
8109 if (atx_it.sp >= 0)
8110 {
8111 RESTORE_IT (it, &atx_it, atx_data);
8112 result = MOVE_X_REACHED;
8113 goto done;
8114 }
8115 /* Otherwise, we can wrap here. */
8116 SAVE_IT (wrap_it, *it, wrap_data);
8117 may_wrap = 0;
8118 }
8119 }
8120 }
8121
8122 /* Remember the line height for the current line, in case
8123 the next element doesn't fit on the line. */
8124 ascent = it->max_ascent;
8125 descent = it->max_descent;
8126
8127 /* The call to produce_glyphs will get the metrics of the
8128 display element IT is loaded with. Record the x-position
8129 before this display element, in case it doesn't fit on the
8130 line. */
8131 x = it->current_x;
8132
8133 PRODUCE_GLYPHS (it);
8134
8135 if (it->area != TEXT_AREA)
8136 {
8137 prev_method = it->method;
8138 if (it->method == GET_FROM_BUFFER)
8139 prev_pos = IT_CHARPOS (*it);
8140 set_iterator_to_next (it, 1);
8141 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8142 SET_TEXT_POS (this_line_min_pos,
8143 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8144 if (it->bidi_p
8145 && (op & MOVE_TO_POS)
8146 && IT_CHARPOS (*it) > to_charpos
8147 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8148 SAVE_IT (ppos_it, *it, ppos_data);
8149 continue;
8150 }
8151
8152 /* The number of glyphs we get back in IT->nglyphs will normally
8153 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8154 character on a terminal frame, or (iii) a line end. For the
8155 second case, IT->nglyphs - 1 padding glyphs will be present.
8156 (On X frames, there is only one glyph produced for a
8157 composite character.)
8158
8159 The behavior implemented below means, for continuation lines,
8160 that as many spaces of a TAB as fit on the current line are
8161 displayed there. For terminal frames, as many glyphs of a
8162 multi-glyph character are displayed in the current line, too.
8163 This is what the old redisplay code did, and we keep it that
8164 way. Under X, the whole shape of a complex character must
8165 fit on the line or it will be completely displayed in the
8166 next line.
8167
8168 Note that both for tabs and padding glyphs, all glyphs have
8169 the same width. */
8170 if (it->nglyphs)
8171 {
8172 /* More than one glyph or glyph doesn't fit on line. All
8173 glyphs have the same width. */
8174 int single_glyph_width = it->pixel_width / it->nglyphs;
8175 int new_x;
8176 int x_before_this_char = x;
8177 int hpos_before_this_char = it->hpos;
8178
8179 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8180 {
8181 new_x = x + single_glyph_width;
8182
8183 /* We want to leave anything reaching TO_X to the caller. */
8184 if ((op & MOVE_TO_X) && new_x > to_x)
8185 {
8186 if (BUFFER_POS_REACHED_P ())
8187 {
8188 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8189 goto buffer_pos_reached;
8190 if (atpos_it.sp < 0)
8191 {
8192 SAVE_IT (atpos_it, *it, atpos_data);
8193 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8194 }
8195 }
8196 else
8197 {
8198 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8199 {
8200 it->current_x = x;
8201 result = MOVE_X_REACHED;
8202 break;
8203 }
8204 if (atx_it.sp < 0)
8205 {
8206 SAVE_IT (atx_it, *it, atx_data);
8207 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8208 }
8209 }
8210 }
8211
8212 if (/* Lines are continued. */
8213 it->line_wrap != TRUNCATE
8214 && (/* And glyph doesn't fit on the line. */
8215 new_x > it->last_visible_x
8216 /* Or it fits exactly and we're on a window
8217 system frame. */
8218 || (new_x == it->last_visible_x
8219 && FRAME_WINDOW_P (it->f))))
8220 {
8221 if (/* IT->hpos == 0 means the very first glyph
8222 doesn't fit on the line, e.g. a wide image. */
8223 it->hpos == 0
8224 || (new_x == it->last_visible_x
8225 && FRAME_WINDOW_P (it->f)))
8226 {
8227 ++it->hpos;
8228 it->current_x = new_x;
8229
8230 /* The character's last glyph just barely fits
8231 in this row. */
8232 if (i == it->nglyphs - 1)
8233 {
8234 /* If this is the destination position,
8235 return a position *before* it in this row,
8236 now that we know it fits in this row. */
8237 if (BUFFER_POS_REACHED_P ())
8238 {
8239 if (it->line_wrap != WORD_WRAP
8240 || wrap_it.sp < 0)
8241 {
8242 it->hpos = hpos_before_this_char;
8243 it->current_x = x_before_this_char;
8244 result = MOVE_POS_MATCH_OR_ZV;
8245 break;
8246 }
8247 if (it->line_wrap == WORD_WRAP
8248 && atpos_it.sp < 0)
8249 {
8250 SAVE_IT (atpos_it, *it, atpos_data);
8251 atpos_it.current_x = x_before_this_char;
8252 atpos_it.hpos = hpos_before_this_char;
8253 }
8254 }
8255
8256 prev_method = it->method;
8257 if (it->method == GET_FROM_BUFFER)
8258 prev_pos = IT_CHARPOS (*it);
8259 set_iterator_to_next (it, 1);
8260 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8261 SET_TEXT_POS (this_line_min_pos,
8262 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8263 /* On graphical terminals, newlines may
8264 "overflow" into the fringe if
8265 overflow-newline-into-fringe is non-nil.
8266 On text-only terminals, newlines may
8267 overflow into the last glyph on the
8268 display line.*/
8269 if (!FRAME_WINDOW_P (it->f)
8270 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8271 {
8272 if (!get_next_display_element (it))
8273 {
8274 result = MOVE_POS_MATCH_OR_ZV;
8275 break;
8276 }
8277 if (BUFFER_POS_REACHED_P ())
8278 {
8279 if (ITERATOR_AT_END_OF_LINE_P (it))
8280 result = MOVE_POS_MATCH_OR_ZV;
8281 else
8282 result = MOVE_LINE_CONTINUED;
8283 break;
8284 }
8285 if (ITERATOR_AT_END_OF_LINE_P (it))
8286 {
8287 result = MOVE_NEWLINE_OR_CR;
8288 break;
8289 }
8290 }
8291 }
8292 }
8293 else
8294 IT_RESET_X_ASCENT_DESCENT (it);
8295
8296 if (wrap_it.sp >= 0)
8297 {
8298 RESTORE_IT (it, &wrap_it, wrap_data);
8299 atpos_it.sp = -1;
8300 atx_it.sp = -1;
8301 }
8302
8303 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8304 IT_CHARPOS (*it)));
8305 result = MOVE_LINE_CONTINUED;
8306 break;
8307 }
8308
8309 if (BUFFER_POS_REACHED_P ())
8310 {
8311 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8312 goto buffer_pos_reached;
8313 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8314 {
8315 SAVE_IT (atpos_it, *it, atpos_data);
8316 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8317 }
8318 }
8319
8320 if (new_x > it->first_visible_x)
8321 {
8322 /* Glyph is visible. Increment number of glyphs that
8323 would be displayed. */
8324 ++it->hpos;
8325 }
8326 }
8327
8328 if (result != MOVE_UNDEFINED)
8329 break;
8330 }
8331 else if (BUFFER_POS_REACHED_P ())
8332 {
8333 buffer_pos_reached:
8334 IT_RESET_X_ASCENT_DESCENT (it);
8335 result = MOVE_POS_MATCH_OR_ZV;
8336 break;
8337 }
8338 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8339 {
8340 /* Stop when TO_X specified and reached. This check is
8341 necessary here because of lines consisting of a line end,
8342 only. The line end will not produce any glyphs and we
8343 would never get MOVE_X_REACHED. */
8344 xassert (it->nglyphs == 0);
8345 result = MOVE_X_REACHED;
8346 break;
8347 }
8348
8349 /* Is this a line end? If yes, we're done. */
8350 if (ITERATOR_AT_END_OF_LINE_P (it))
8351 {
8352 /* If we are past TO_CHARPOS, but never saw any character
8353 positions smaller than TO_CHARPOS, return
8354 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8355 did. */
8356 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8357 {
8358 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8359 {
8360 if (IT_CHARPOS (ppos_it) < ZV)
8361 {
8362 RESTORE_IT (it, &ppos_it, ppos_data);
8363 result = MOVE_POS_MATCH_OR_ZV;
8364 }
8365 else
8366 goto buffer_pos_reached;
8367 }
8368 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8369 && IT_CHARPOS (*it) > to_charpos)
8370 goto buffer_pos_reached;
8371 else
8372 result = MOVE_NEWLINE_OR_CR;
8373 }
8374 else
8375 result = MOVE_NEWLINE_OR_CR;
8376 break;
8377 }
8378
8379 prev_method = it->method;
8380 if (it->method == GET_FROM_BUFFER)
8381 prev_pos = IT_CHARPOS (*it);
8382 /* The current display element has been consumed. Advance
8383 to the next. */
8384 set_iterator_to_next (it, 1);
8385 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8386 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8387 if (IT_CHARPOS (*it) < to_charpos)
8388 saw_smaller_pos = 1;
8389 if (it->bidi_p
8390 && (op & MOVE_TO_POS)
8391 && IT_CHARPOS (*it) >= to_charpos
8392 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8393 SAVE_IT (ppos_it, *it, ppos_data);
8394
8395 /* Stop if lines are truncated and IT's current x-position is
8396 past the right edge of the window now. */
8397 if (it->line_wrap == TRUNCATE
8398 && it->current_x >= it->last_visible_x)
8399 {
8400 if (!FRAME_WINDOW_P (it->f)
8401 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8402 {
8403 int at_eob_p = 0;
8404
8405 if ((at_eob_p = !get_next_display_element (it))
8406 || BUFFER_POS_REACHED_P ()
8407 /* If we are past TO_CHARPOS, but never saw any
8408 character positions smaller than TO_CHARPOS,
8409 return MOVE_POS_MATCH_OR_ZV, like the
8410 unidirectional display did. */
8411 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8412 && !saw_smaller_pos
8413 && IT_CHARPOS (*it) > to_charpos))
8414 {
8415 if (it->bidi_p
8416 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8417 RESTORE_IT (it, &ppos_it, ppos_data);
8418 result = MOVE_POS_MATCH_OR_ZV;
8419 break;
8420 }
8421 if (ITERATOR_AT_END_OF_LINE_P (it))
8422 {
8423 result = MOVE_NEWLINE_OR_CR;
8424 break;
8425 }
8426 }
8427 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8428 && !saw_smaller_pos
8429 && IT_CHARPOS (*it) > to_charpos)
8430 {
8431 if (IT_CHARPOS (ppos_it) < ZV)
8432 RESTORE_IT (it, &ppos_it, ppos_data);
8433 result = MOVE_POS_MATCH_OR_ZV;
8434 break;
8435 }
8436 result = MOVE_LINE_TRUNCATED;
8437 break;
8438 }
8439 #undef IT_RESET_X_ASCENT_DESCENT
8440 }
8441
8442 #undef BUFFER_POS_REACHED_P
8443
8444 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8445 restore the saved iterator. */
8446 if (atpos_it.sp >= 0)
8447 RESTORE_IT (it, &atpos_it, atpos_data);
8448 else if (atx_it.sp >= 0)
8449 RESTORE_IT (it, &atx_it, atx_data);
8450
8451 done:
8452
8453 if (atpos_data)
8454 bidi_unshelve_cache (atpos_data, 1);
8455 if (atx_data)
8456 bidi_unshelve_cache (atx_data, 1);
8457 if (wrap_data)
8458 bidi_unshelve_cache (wrap_data, 1);
8459 if (ppos_data)
8460 bidi_unshelve_cache (ppos_data, 1);
8461
8462 /* Restore the iterator settings altered at the beginning of this
8463 function. */
8464 it->glyph_row = saved_glyph_row;
8465 return result;
8466 }
8467
8468 /* For external use. */
8469 void
8470 move_it_in_display_line (struct it *it,
8471 EMACS_INT to_charpos, int to_x,
8472 enum move_operation_enum op)
8473 {
8474 if (it->line_wrap == WORD_WRAP
8475 && (op & MOVE_TO_X))
8476 {
8477 struct it save_it;
8478 void *save_data = NULL;
8479 int skip;
8480
8481 SAVE_IT (save_it, *it, save_data);
8482 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8483 /* When word-wrap is on, TO_X may lie past the end
8484 of a wrapped line. Then it->current is the
8485 character on the next line, so backtrack to the
8486 space before the wrap point. */
8487 if (skip == MOVE_LINE_CONTINUED)
8488 {
8489 int prev_x = max (it->current_x - 1, 0);
8490 RESTORE_IT (it, &save_it, save_data);
8491 move_it_in_display_line_to
8492 (it, -1, prev_x, MOVE_TO_X);
8493 }
8494 else
8495 bidi_unshelve_cache (save_data, 1);
8496 }
8497 else
8498 move_it_in_display_line_to (it, to_charpos, to_x, op);
8499 }
8500
8501
8502 /* Move IT forward until it satisfies one or more of the criteria in
8503 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8504
8505 OP is a bit-mask that specifies where to stop, and in particular,
8506 which of those four position arguments makes a difference. See the
8507 description of enum move_operation_enum.
8508
8509 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8510 screen line, this function will set IT to the next position that is
8511 displayed to the right of TO_CHARPOS on the screen. */
8512
8513 void
8514 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8515 {
8516 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8517 int line_height, line_start_x = 0, reached = 0;
8518 void *backup_data = NULL;
8519
8520 for (;;)
8521 {
8522 if (op & MOVE_TO_VPOS)
8523 {
8524 /* If no TO_CHARPOS and no TO_X specified, stop at the
8525 start of the line TO_VPOS. */
8526 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8527 {
8528 if (it->vpos == to_vpos)
8529 {
8530 reached = 1;
8531 break;
8532 }
8533 else
8534 skip = move_it_in_display_line_to (it, -1, -1, 0);
8535 }
8536 else
8537 {
8538 /* TO_VPOS >= 0 means stop at TO_X in the line at
8539 TO_VPOS, or at TO_POS, whichever comes first. */
8540 if (it->vpos == to_vpos)
8541 {
8542 reached = 2;
8543 break;
8544 }
8545
8546 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8547
8548 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8549 {
8550 reached = 3;
8551 break;
8552 }
8553 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8554 {
8555 /* We have reached TO_X but not in the line we want. */
8556 skip = move_it_in_display_line_to (it, to_charpos,
8557 -1, MOVE_TO_POS);
8558 if (skip == MOVE_POS_MATCH_OR_ZV)
8559 {
8560 reached = 4;
8561 break;
8562 }
8563 }
8564 }
8565 }
8566 else if (op & MOVE_TO_Y)
8567 {
8568 struct it it_backup;
8569
8570 if (it->line_wrap == WORD_WRAP)
8571 SAVE_IT (it_backup, *it, backup_data);
8572
8573 /* TO_Y specified means stop at TO_X in the line containing
8574 TO_Y---or at TO_CHARPOS if this is reached first. The
8575 problem is that we can't really tell whether the line
8576 contains TO_Y before we have completely scanned it, and
8577 this may skip past TO_X. What we do is to first scan to
8578 TO_X.
8579
8580 If TO_X is not specified, use a TO_X of zero. The reason
8581 is to make the outcome of this function more predictable.
8582 If we didn't use TO_X == 0, we would stop at the end of
8583 the line which is probably not what a caller would expect
8584 to happen. */
8585 skip = move_it_in_display_line_to
8586 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8587 (MOVE_TO_X | (op & MOVE_TO_POS)));
8588
8589 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8590 if (skip == MOVE_POS_MATCH_OR_ZV)
8591 reached = 5;
8592 else if (skip == MOVE_X_REACHED)
8593 {
8594 /* If TO_X was reached, we want to know whether TO_Y is
8595 in the line. We know this is the case if the already
8596 scanned glyphs make the line tall enough. Otherwise,
8597 we must check by scanning the rest of the line. */
8598 line_height = it->max_ascent + it->max_descent;
8599 if (to_y >= it->current_y
8600 && to_y < it->current_y + line_height)
8601 {
8602 reached = 6;
8603 break;
8604 }
8605 SAVE_IT (it_backup, *it, backup_data);
8606 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8607 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8608 op & MOVE_TO_POS);
8609 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8610 line_height = it->max_ascent + it->max_descent;
8611 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8612
8613 if (to_y >= it->current_y
8614 && to_y < it->current_y + line_height)
8615 {
8616 /* If TO_Y is in this line and TO_X was reached
8617 above, we scanned too far. We have to restore
8618 IT's settings to the ones before skipping. */
8619 RESTORE_IT (it, &it_backup, backup_data);
8620 reached = 6;
8621 }
8622 else
8623 {
8624 skip = skip2;
8625 if (skip == MOVE_POS_MATCH_OR_ZV)
8626 reached = 7;
8627 }
8628 }
8629 else
8630 {
8631 /* Check whether TO_Y is in this line. */
8632 line_height = it->max_ascent + it->max_descent;
8633 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8634
8635 if (to_y >= it->current_y
8636 && to_y < it->current_y + line_height)
8637 {
8638 /* When word-wrap is on, TO_X may lie past the end
8639 of a wrapped line. Then it->current is the
8640 character on the next line, so backtrack to the
8641 space before the wrap point. */
8642 if (skip == MOVE_LINE_CONTINUED
8643 && it->line_wrap == WORD_WRAP)
8644 {
8645 int prev_x = max (it->current_x - 1, 0);
8646 RESTORE_IT (it, &it_backup, backup_data);
8647 skip = move_it_in_display_line_to
8648 (it, -1, prev_x, MOVE_TO_X);
8649 }
8650 reached = 6;
8651 }
8652 }
8653
8654 if (reached)
8655 break;
8656 }
8657 else if (BUFFERP (it->object)
8658 && (it->method == GET_FROM_BUFFER
8659 || it->method == GET_FROM_STRETCH)
8660 && IT_CHARPOS (*it) >= to_charpos
8661 /* Under bidi iteration, a call to set_iterator_to_next
8662 can scan far beyond to_charpos if the initial
8663 portion of the next line needs to be reordered. In
8664 that case, give move_it_in_display_line_to another
8665 chance below. */
8666 && !(it->bidi_p
8667 && it->bidi_it.scan_dir == -1))
8668 skip = MOVE_POS_MATCH_OR_ZV;
8669 else
8670 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8671
8672 switch (skip)
8673 {
8674 case MOVE_POS_MATCH_OR_ZV:
8675 reached = 8;
8676 goto out;
8677
8678 case MOVE_NEWLINE_OR_CR:
8679 set_iterator_to_next (it, 1);
8680 it->continuation_lines_width = 0;
8681 break;
8682
8683 case MOVE_LINE_TRUNCATED:
8684 it->continuation_lines_width = 0;
8685 reseat_at_next_visible_line_start (it, 0);
8686 if ((op & MOVE_TO_POS) != 0
8687 && IT_CHARPOS (*it) > to_charpos)
8688 {
8689 reached = 9;
8690 goto out;
8691 }
8692 break;
8693
8694 case MOVE_LINE_CONTINUED:
8695 /* For continued lines ending in a tab, some of the glyphs
8696 associated with the tab are displayed on the current
8697 line. Since it->current_x does not include these glyphs,
8698 we use it->last_visible_x instead. */
8699 if (it->c == '\t')
8700 {
8701 it->continuation_lines_width += it->last_visible_x;
8702 /* When moving by vpos, ensure that the iterator really
8703 advances to the next line (bug#847, bug#969). Fixme:
8704 do we need to do this in other circumstances? */
8705 if (it->current_x != it->last_visible_x
8706 && (op & MOVE_TO_VPOS)
8707 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8708 {
8709 line_start_x = it->current_x + it->pixel_width
8710 - it->last_visible_x;
8711 set_iterator_to_next (it, 0);
8712 }
8713 }
8714 else
8715 it->continuation_lines_width += it->current_x;
8716 break;
8717
8718 default:
8719 abort ();
8720 }
8721
8722 /* Reset/increment for the next run. */
8723 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8724 it->current_x = line_start_x;
8725 line_start_x = 0;
8726 it->hpos = 0;
8727 it->current_y += it->max_ascent + it->max_descent;
8728 ++it->vpos;
8729 last_height = it->max_ascent + it->max_descent;
8730 last_max_ascent = it->max_ascent;
8731 it->max_ascent = it->max_descent = 0;
8732 }
8733
8734 out:
8735
8736 /* On text terminals, we may stop at the end of a line in the middle
8737 of a multi-character glyph. If the glyph itself is continued,
8738 i.e. it is actually displayed on the next line, don't treat this
8739 stopping point as valid; move to the next line instead (unless
8740 that brings us offscreen). */
8741 if (!FRAME_WINDOW_P (it->f)
8742 && op & MOVE_TO_POS
8743 && IT_CHARPOS (*it) == to_charpos
8744 && it->what == IT_CHARACTER
8745 && it->nglyphs > 1
8746 && it->line_wrap == WINDOW_WRAP
8747 && it->current_x == it->last_visible_x - 1
8748 && it->c != '\n'
8749 && it->c != '\t'
8750 && it->vpos < XFASTINT (it->w->window_end_vpos))
8751 {
8752 it->continuation_lines_width += it->current_x;
8753 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8754 it->current_y += it->max_ascent + it->max_descent;
8755 ++it->vpos;
8756 last_height = it->max_ascent + it->max_descent;
8757 last_max_ascent = it->max_ascent;
8758 }
8759
8760 if (backup_data)
8761 bidi_unshelve_cache (backup_data, 1);
8762
8763 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8764 }
8765
8766
8767 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8768
8769 If DY > 0, move IT backward at least that many pixels. DY = 0
8770 means move IT backward to the preceding line start or BEGV. This
8771 function may move over more than DY pixels if IT->current_y - DY
8772 ends up in the middle of a line; in this case IT->current_y will be
8773 set to the top of the line moved to. */
8774
8775 void
8776 move_it_vertically_backward (struct it *it, int dy)
8777 {
8778 int nlines, h;
8779 struct it it2, it3;
8780 void *it2data = NULL, *it3data = NULL;
8781 EMACS_INT start_pos;
8782
8783 move_further_back:
8784 xassert (dy >= 0);
8785
8786 start_pos = IT_CHARPOS (*it);
8787
8788 /* Estimate how many newlines we must move back. */
8789 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8790
8791 /* Set the iterator's position that many lines back. */
8792 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8793 back_to_previous_visible_line_start (it);
8794
8795 /* Reseat the iterator here. When moving backward, we don't want
8796 reseat to skip forward over invisible text, set up the iterator
8797 to deliver from overlay strings at the new position etc. So,
8798 use reseat_1 here. */
8799 reseat_1 (it, it->current.pos, 1);
8800
8801 /* We are now surely at a line start. */
8802 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8803 reordering is in effect. */
8804 it->continuation_lines_width = 0;
8805
8806 /* Move forward and see what y-distance we moved. First move to the
8807 start of the next line so that we get its height. We need this
8808 height to be able to tell whether we reached the specified
8809 y-distance. */
8810 SAVE_IT (it2, *it, it2data);
8811 it2.max_ascent = it2.max_descent = 0;
8812 do
8813 {
8814 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8815 MOVE_TO_POS | MOVE_TO_VPOS);
8816 }
8817 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8818 /* If we are in a display string which starts at START_POS,
8819 and that display string includes a newline, and we are
8820 right after that newline (i.e. at the beginning of a
8821 display line), exit the loop, because otherwise we will
8822 infloop, since move_it_to will see that it is already at
8823 START_POS and will not move. */
8824 || (it2.method == GET_FROM_STRING
8825 && IT_CHARPOS (it2) == start_pos
8826 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8827 xassert (IT_CHARPOS (*it) >= BEGV);
8828 SAVE_IT (it3, it2, it3data);
8829
8830 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8831 xassert (IT_CHARPOS (*it) >= BEGV);
8832 /* H is the actual vertical distance from the position in *IT
8833 and the starting position. */
8834 h = it2.current_y - it->current_y;
8835 /* NLINES is the distance in number of lines. */
8836 nlines = it2.vpos - it->vpos;
8837
8838 /* Correct IT's y and vpos position
8839 so that they are relative to the starting point. */
8840 it->vpos -= nlines;
8841 it->current_y -= h;
8842
8843 if (dy == 0)
8844 {
8845 /* DY == 0 means move to the start of the screen line. The
8846 value of nlines is > 0 if continuation lines were involved,
8847 or if the original IT position was at start of a line. */
8848 RESTORE_IT (it, it, it2data);
8849 if (nlines > 0)
8850 move_it_by_lines (it, nlines);
8851 /* The above code moves us to some position NLINES down,
8852 usually to its first glyph (leftmost in an L2R line), but
8853 that's not necessarily the start of the line, under bidi
8854 reordering. We want to get to the character position
8855 that is immediately after the newline of the previous
8856 line. */
8857 if (it->bidi_p
8858 && !it->continuation_lines_width
8859 && !STRINGP (it->string)
8860 && IT_CHARPOS (*it) > BEGV
8861 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8862 {
8863 EMACS_INT nl_pos =
8864 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8865
8866 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8867 }
8868 bidi_unshelve_cache (it3data, 1);
8869 }
8870 else
8871 {
8872 /* The y-position we try to reach, relative to *IT.
8873 Note that H has been subtracted in front of the if-statement. */
8874 int target_y = it->current_y + h - dy;
8875 int y0 = it3.current_y;
8876 int y1;
8877 int line_height;
8878
8879 RESTORE_IT (&it3, &it3, it3data);
8880 y1 = line_bottom_y (&it3);
8881 line_height = y1 - y0;
8882 RESTORE_IT (it, it, it2data);
8883 /* If we did not reach target_y, try to move further backward if
8884 we can. If we moved too far backward, try to move forward. */
8885 if (target_y < it->current_y
8886 /* This is heuristic. In a window that's 3 lines high, with
8887 a line height of 13 pixels each, recentering with point
8888 on the bottom line will try to move -39/2 = 19 pixels
8889 backward. Try to avoid moving into the first line. */
8890 && (it->current_y - target_y
8891 > min (window_box_height (it->w), line_height * 2 / 3))
8892 && IT_CHARPOS (*it) > BEGV)
8893 {
8894 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8895 target_y - it->current_y));
8896 dy = it->current_y - target_y;
8897 goto move_further_back;
8898 }
8899 else if (target_y >= it->current_y + line_height
8900 && IT_CHARPOS (*it) < ZV)
8901 {
8902 /* Should move forward by at least one line, maybe more.
8903
8904 Note: Calling move_it_by_lines can be expensive on
8905 terminal frames, where compute_motion is used (via
8906 vmotion) to do the job, when there are very long lines
8907 and truncate-lines is nil. That's the reason for
8908 treating terminal frames specially here. */
8909
8910 if (!FRAME_WINDOW_P (it->f))
8911 move_it_vertically (it, target_y - (it->current_y + line_height));
8912 else
8913 {
8914 do
8915 {
8916 move_it_by_lines (it, 1);
8917 }
8918 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8919 }
8920 }
8921 }
8922 }
8923
8924
8925 /* Move IT by a specified amount of pixel lines DY. DY negative means
8926 move backwards. DY = 0 means move to start of screen line. At the
8927 end, IT will be on the start of a screen line. */
8928
8929 void
8930 move_it_vertically (struct it *it, int dy)
8931 {
8932 if (dy <= 0)
8933 move_it_vertically_backward (it, -dy);
8934 else
8935 {
8936 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8937 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8938 MOVE_TO_POS | MOVE_TO_Y);
8939 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8940
8941 /* If buffer ends in ZV without a newline, move to the start of
8942 the line to satisfy the post-condition. */
8943 if (IT_CHARPOS (*it) == ZV
8944 && ZV > BEGV
8945 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8946 move_it_by_lines (it, 0);
8947 }
8948 }
8949
8950
8951 /* Move iterator IT past the end of the text line it is in. */
8952
8953 void
8954 move_it_past_eol (struct it *it)
8955 {
8956 enum move_it_result rc;
8957
8958 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8959 if (rc == MOVE_NEWLINE_OR_CR)
8960 set_iterator_to_next (it, 0);
8961 }
8962
8963
8964 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8965 negative means move up. DVPOS == 0 means move to the start of the
8966 screen line.
8967
8968 Optimization idea: If we would know that IT->f doesn't use
8969 a face with proportional font, we could be faster for
8970 truncate-lines nil. */
8971
8972 void
8973 move_it_by_lines (struct it *it, int dvpos)
8974 {
8975
8976 /* The commented-out optimization uses vmotion on terminals. This
8977 gives bad results, because elements like it->what, on which
8978 callers such as pos_visible_p rely, aren't updated. */
8979 /* struct position pos;
8980 if (!FRAME_WINDOW_P (it->f))
8981 {
8982 struct text_pos textpos;
8983
8984 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8985 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8986 reseat (it, textpos, 1);
8987 it->vpos += pos.vpos;
8988 it->current_y += pos.vpos;
8989 }
8990 else */
8991
8992 if (dvpos == 0)
8993 {
8994 /* DVPOS == 0 means move to the start of the screen line. */
8995 move_it_vertically_backward (it, 0);
8996 /* Let next call to line_bottom_y calculate real line height */
8997 last_height = 0;
8998 }
8999 else if (dvpos > 0)
9000 {
9001 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9002 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9003 {
9004 /* Only move to the next buffer position if we ended up in a
9005 string from display property, not in an overlay string
9006 (before-string or after-string). That is because the
9007 latter don't conceal the underlying buffer position, so
9008 we can ask to move the iterator to the exact position we
9009 are interested in. Note that, even if we are already at
9010 IT_CHARPOS (*it), the call below is not a no-op, as it
9011 will detect that we are at the end of the string, pop the
9012 iterator, and compute it->current_x and it->hpos
9013 correctly. */
9014 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9015 -1, -1, -1, MOVE_TO_POS);
9016 }
9017 }
9018 else
9019 {
9020 struct it it2;
9021 void *it2data = NULL;
9022 EMACS_INT start_charpos, i;
9023
9024 /* Start at the beginning of the screen line containing IT's
9025 position. This may actually move vertically backwards,
9026 in case of overlays, so adjust dvpos accordingly. */
9027 dvpos += it->vpos;
9028 move_it_vertically_backward (it, 0);
9029 dvpos -= it->vpos;
9030
9031 /* Go back -DVPOS visible lines and reseat the iterator there. */
9032 start_charpos = IT_CHARPOS (*it);
9033 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9034 back_to_previous_visible_line_start (it);
9035 reseat (it, it->current.pos, 1);
9036
9037 /* Move further back if we end up in a string or an image. */
9038 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9039 {
9040 /* First try to move to start of display line. */
9041 dvpos += it->vpos;
9042 move_it_vertically_backward (it, 0);
9043 dvpos -= it->vpos;
9044 if (IT_POS_VALID_AFTER_MOVE_P (it))
9045 break;
9046 /* If start of line is still in string or image,
9047 move further back. */
9048 back_to_previous_visible_line_start (it);
9049 reseat (it, it->current.pos, 1);
9050 dvpos--;
9051 }
9052
9053 it->current_x = it->hpos = 0;
9054
9055 /* Above call may have moved too far if continuation lines
9056 are involved. Scan forward and see if it did. */
9057 SAVE_IT (it2, *it, it2data);
9058 it2.vpos = it2.current_y = 0;
9059 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9060 it->vpos -= it2.vpos;
9061 it->current_y -= it2.current_y;
9062 it->current_x = it->hpos = 0;
9063
9064 /* If we moved too far back, move IT some lines forward. */
9065 if (it2.vpos > -dvpos)
9066 {
9067 int delta = it2.vpos + dvpos;
9068
9069 RESTORE_IT (&it2, &it2, it2data);
9070 SAVE_IT (it2, *it, it2data);
9071 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9072 /* Move back again if we got too far ahead. */
9073 if (IT_CHARPOS (*it) >= start_charpos)
9074 RESTORE_IT (it, &it2, it2data);
9075 else
9076 bidi_unshelve_cache (it2data, 1);
9077 }
9078 else
9079 RESTORE_IT (it, it, it2data);
9080 }
9081 }
9082
9083 /* Return 1 if IT points into the middle of a display vector. */
9084
9085 int
9086 in_display_vector_p (struct it *it)
9087 {
9088 return (it->method == GET_FROM_DISPLAY_VECTOR
9089 && it->current.dpvec_index > 0
9090 && it->dpvec + it->current.dpvec_index != it->dpend);
9091 }
9092
9093 \f
9094 /***********************************************************************
9095 Messages
9096 ***********************************************************************/
9097
9098
9099 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9100 to *Messages*. */
9101
9102 void
9103 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9104 {
9105 Lisp_Object args[3];
9106 Lisp_Object msg, fmt;
9107 char *buffer;
9108 EMACS_INT len;
9109 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9110 USE_SAFE_ALLOCA;
9111
9112 /* Do nothing if called asynchronously. Inserting text into
9113 a buffer may call after-change-functions and alike and
9114 that would means running Lisp asynchronously. */
9115 if (handling_signal)
9116 return;
9117
9118 fmt = msg = Qnil;
9119 GCPRO4 (fmt, msg, arg1, arg2);
9120
9121 args[0] = fmt = build_string (format);
9122 args[1] = arg1;
9123 args[2] = arg2;
9124 msg = Fformat (3, args);
9125
9126 len = SBYTES (msg) + 1;
9127 SAFE_ALLOCA (buffer, char *, len);
9128 memcpy (buffer, SDATA (msg), len);
9129
9130 message_dolog (buffer, len - 1, 1, 0);
9131 SAFE_FREE ();
9132
9133 UNGCPRO;
9134 }
9135
9136
9137 /* Output a newline in the *Messages* buffer if "needs" one. */
9138
9139 void
9140 message_log_maybe_newline (void)
9141 {
9142 if (message_log_need_newline)
9143 message_dolog ("", 0, 1, 0);
9144 }
9145
9146
9147 /* Add a string M of length NBYTES to the message log, optionally
9148 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9149 nonzero, means interpret the contents of M as multibyte. This
9150 function calls low-level routines in order to bypass text property
9151 hooks, etc. which might not be safe to run.
9152
9153 This may GC (insert may run before/after change hooks),
9154 so the buffer M must NOT point to a Lisp string. */
9155
9156 void
9157 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9158 {
9159 const unsigned char *msg = (const unsigned char *) m;
9160
9161 if (!NILP (Vmemory_full))
9162 return;
9163
9164 if (!NILP (Vmessage_log_max))
9165 {
9166 struct buffer *oldbuf;
9167 Lisp_Object oldpoint, oldbegv, oldzv;
9168 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9169 EMACS_INT point_at_end = 0;
9170 EMACS_INT zv_at_end = 0;
9171 Lisp_Object old_deactivate_mark, tem;
9172 struct gcpro gcpro1;
9173
9174 old_deactivate_mark = Vdeactivate_mark;
9175 oldbuf = current_buffer;
9176 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9177 BVAR (current_buffer, undo_list) = Qt;
9178
9179 oldpoint = message_dolog_marker1;
9180 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9181 oldbegv = message_dolog_marker2;
9182 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9183 oldzv = message_dolog_marker3;
9184 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9185 GCPRO1 (old_deactivate_mark);
9186
9187 if (PT == Z)
9188 point_at_end = 1;
9189 if (ZV == Z)
9190 zv_at_end = 1;
9191
9192 BEGV = BEG;
9193 BEGV_BYTE = BEG_BYTE;
9194 ZV = Z;
9195 ZV_BYTE = Z_BYTE;
9196 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9197
9198 /* Insert the string--maybe converting multibyte to single byte
9199 or vice versa, so that all the text fits the buffer. */
9200 if (multibyte
9201 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9202 {
9203 EMACS_INT i;
9204 int c, char_bytes;
9205 char work[1];
9206
9207 /* Convert a multibyte string to single-byte
9208 for the *Message* buffer. */
9209 for (i = 0; i < nbytes; i += char_bytes)
9210 {
9211 c = string_char_and_length (msg + i, &char_bytes);
9212 work[0] = (ASCII_CHAR_P (c)
9213 ? c
9214 : multibyte_char_to_unibyte (c));
9215 insert_1_both (work, 1, 1, 1, 0, 0);
9216 }
9217 }
9218 else if (! multibyte
9219 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9220 {
9221 EMACS_INT i;
9222 int c, char_bytes;
9223 unsigned char str[MAX_MULTIBYTE_LENGTH];
9224 /* Convert a single-byte string to multibyte
9225 for the *Message* buffer. */
9226 for (i = 0; i < nbytes; i++)
9227 {
9228 c = msg[i];
9229 MAKE_CHAR_MULTIBYTE (c);
9230 char_bytes = CHAR_STRING (c, str);
9231 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9232 }
9233 }
9234 else if (nbytes)
9235 insert_1 (m, nbytes, 1, 0, 0);
9236
9237 if (nlflag)
9238 {
9239 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9240 printmax_t dups;
9241 insert_1 ("\n", 1, 1, 0, 0);
9242
9243 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9244 this_bol = PT;
9245 this_bol_byte = PT_BYTE;
9246
9247 /* See if this line duplicates the previous one.
9248 If so, combine duplicates. */
9249 if (this_bol > BEG)
9250 {
9251 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9252 prev_bol = PT;
9253 prev_bol_byte = PT_BYTE;
9254
9255 dups = message_log_check_duplicate (prev_bol_byte,
9256 this_bol_byte);
9257 if (dups)
9258 {
9259 del_range_both (prev_bol, prev_bol_byte,
9260 this_bol, this_bol_byte, 0);
9261 if (dups > 1)
9262 {
9263 char dupstr[sizeof " [ times]"
9264 + INT_STRLEN_BOUND (printmax_t)];
9265 int duplen;
9266
9267 /* If you change this format, don't forget to also
9268 change message_log_check_duplicate. */
9269 sprintf (dupstr, " [%"pMd" times]", dups);
9270 duplen = strlen (dupstr);
9271 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9272 insert_1 (dupstr, duplen, 1, 0, 1);
9273 }
9274 }
9275 }
9276
9277 /* If we have more than the desired maximum number of lines
9278 in the *Messages* buffer now, delete the oldest ones.
9279 This is safe because we don't have undo in this buffer. */
9280
9281 if (NATNUMP (Vmessage_log_max))
9282 {
9283 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9284 -XFASTINT (Vmessage_log_max) - 1, 0);
9285 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9286 }
9287 }
9288 BEGV = XMARKER (oldbegv)->charpos;
9289 BEGV_BYTE = marker_byte_position (oldbegv);
9290
9291 if (zv_at_end)
9292 {
9293 ZV = Z;
9294 ZV_BYTE = Z_BYTE;
9295 }
9296 else
9297 {
9298 ZV = XMARKER (oldzv)->charpos;
9299 ZV_BYTE = marker_byte_position (oldzv);
9300 }
9301
9302 if (point_at_end)
9303 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9304 else
9305 /* We can't do Fgoto_char (oldpoint) because it will run some
9306 Lisp code. */
9307 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9308 XMARKER (oldpoint)->bytepos);
9309
9310 UNGCPRO;
9311 unchain_marker (XMARKER (oldpoint));
9312 unchain_marker (XMARKER (oldbegv));
9313 unchain_marker (XMARKER (oldzv));
9314
9315 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9316 set_buffer_internal (oldbuf);
9317 if (NILP (tem))
9318 windows_or_buffers_changed = old_windows_or_buffers_changed;
9319 message_log_need_newline = !nlflag;
9320 Vdeactivate_mark = old_deactivate_mark;
9321 }
9322 }
9323
9324
9325 /* We are at the end of the buffer after just having inserted a newline.
9326 (Note: We depend on the fact we won't be crossing the gap.)
9327 Check to see if the most recent message looks a lot like the previous one.
9328 Return 0 if different, 1 if the new one should just replace it, or a
9329 value N > 1 if we should also append " [N times]". */
9330
9331 static intmax_t
9332 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9333 {
9334 EMACS_INT i;
9335 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9336 int seen_dots = 0;
9337 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9338 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9339
9340 for (i = 0; i < len; i++)
9341 {
9342 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9343 seen_dots = 1;
9344 if (p1[i] != p2[i])
9345 return seen_dots;
9346 }
9347 p1 += len;
9348 if (*p1 == '\n')
9349 return 2;
9350 if (*p1++ == ' ' && *p1++ == '[')
9351 {
9352 char *pend;
9353 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9354 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9355 return n+1;
9356 }
9357 return 0;
9358 }
9359 \f
9360
9361 /* Display an echo area message M with a specified length of NBYTES
9362 bytes. The string may include null characters. If M is 0, clear
9363 out any existing message, and let the mini-buffer text show
9364 through.
9365
9366 This may GC, so the buffer M must NOT point to a Lisp string. */
9367
9368 void
9369 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9370 {
9371 /* First flush out any partial line written with print. */
9372 message_log_maybe_newline ();
9373 if (m)
9374 message_dolog (m, nbytes, 1, multibyte);
9375 message2_nolog (m, nbytes, multibyte);
9376 }
9377
9378
9379 /* The non-logging counterpart of message2. */
9380
9381 void
9382 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9383 {
9384 struct frame *sf = SELECTED_FRAME ();
9385 message_enable_multibyte = multibyte;
9386
9387 if (FRAME_INITIAL_P (sf))
9388 {
9389 if (noninteractive_need_newline)
9390 putc ('\n', stderr);
9391 noninteractive_need_newline = 0;
9392 if (m)
9393 fwrite (m, nbytes, 1, stderr);
9394 if (cursor_in_echo_area == 0)
9395 fprintf (stderr, "\n");
9396 fflush (stderr);
9397 }
9398 /* A null message buffer means that the frame hasn't really been
9399 initialized yet. Error messages get reported properly by
9400 cmd_error, so this must be just an informative message; toss it. */
9401 else if (INTERACTIVE
9402 && sf->glyphs_initialized_p
9403 && FRAME_MESSAGE_BUF (sf))
9404 {
9405 Lisp_Object mini_window;
9406 struct frame *f;
9407
9408 /* Get the frame containing the mini-buffer
9409 that the selected frame is using. */
9410 mini_window = FRAME_MINIBUF_WINDOW (sf);
9411 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9412
9413 FRAME_SAMPLE_VISIBILITY (f);
9414 if (FRAME_VISIBLE_P (sf)
9415 && ! FRAME_VISIBLE_P (f))
9416 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9417
9418 if (m)
9419 {
9420 set_message (m, Qnil, nbytes, multibyte);
9421 if (minibuffer_auto_raise)
9422 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9423 }
9424 else
9425 clear_message (1, 1);
9426
9427 do_pending_window_change (0);
9428 echo_area_display (1);
9429 do_pending_window_change (0);
9430 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9431 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9432 }
9433 }
9434
9435
9436 /* Display an echo area message M with a specified length of NBYTES
9437 bytes. The string may include null characters. If M is not a
9438 string, clear out any existing message, and let the mini-buffer
9439 text show through.
9440
9441 This function cancels echoing. */
9442
9443 void
9444 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9445 {
9446 struct gcpro gcpro1;
9447
9448 GCPRO1 (m);
9449 clear_message (1,1);
9450 cancel_echoing ();
9451
9452 /* First flush out any partial line written with print. */
9453 message_log_maybe_newline ();
9454 if (STRINGP (m))
9455 {
9456 char *buffer;
9457 USE_SAFE_ALLOCA;
9458
9459 SAFE_ALLOCA (buffer, char *, nbytes);
9460 memcpy (buffer, SDATA (m), nbytes);
9461 message_dolog (buffer, nbytes, 1, multibyte);
9462 SAFE_FREE ();
9463 }
9464 message3_nolog (m, nbytes, multibyte);
9465
9466 UNGCPRO;
9467 }
9468
9469
9470 /* The non-logging version of message3.
9471 This does not cancel echoing, because it is used for echoing.
9472 Perhaps we need to make a separate function for echoing
9473 and make this cancel echoing. */
9474
9475 void
9476 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9477 {
9478 struct frame *sf = SELECTED_FRAME ();
9479 message_enable_multibyte = multibyte;
9480
9481 if (FRAME_INITIAL_P (sf))
9482 {
9483 if (noninteractive_need_newline)
9484 putc ('\n', stderr);
9485 noninteractive_need_newline = 0;
9486 if (STRINGP (m))
9487 fwrite (SDATA (m), nbytes, 1, stderr);
9488 if (cursor_in_echo_area == 0)
9489 fprintf (stderr, "\n");
9490 fflush (stderr);
9491 }
9492 /* A null message buffer means that the frame hasn't really been
9493 initialized yet. Error messages get reported properly by
9494 cmd_error, so this must be just an informative message; toss it. */
9495 else if (INTERACTIVE
9496 && sf->glyphs_initialized_p
9497 && FRAME_MESSAGE_BUF (sf))
9498 {
9499 Lisp_Object mini_window;
9500 Lisp_Object frame;
9501 struct frame *f;
9502
9503 /* Get the frame containing the mini-buffer
9504 that the selected frame is using. */
9505 mini_window = FRAME_MINIBUF_WINDOW (sf);
9506 frame = XWINDOW (mini_window)->frame;
9507 f = XFRAME (frame);
9508
9509 FRAME_SAMPLE_VISIBILITY (f);
9510 if (FRAME_VISIBLE_P (sf)
9511 && !FRAME_VISIBLE_P (f))
9512 Fmake_frame_visible (frame);
9513
9514 if (STRINGP (m) && SCHARS (m) > 0)
9515 {
9516 set_message (NULL, m, nbytes, multibyte);
9517 if (minibuffer_auto_raise)
9518 Fraise_frame (frame);
9519 /* Assume we are not echoing.
9520 (If we are, echo_now will override this.) */
9521 echo_message_buffer = Qnil;
9522 }
9523 else
9524 clear_message (1, 1);
9525
9526 do_pending_window_change (0);
9527 echo_area_display (1);
9528 do_pending_window_change (0);
9529 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9530 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9531 }
9532 }
9533
9534
9535 /* Display a null-terminated echo area message M. If M is 0, clear
9536 out any existing message, and let the mini-buffer text show through.
9537
9538 The buffer M must continue to exist until after the echo area gets
9539 cleared or some other message gets displayed there. Do not pass
9540 text that is stored in a Lisp string. Do not pass text in a buffer
9541 that was alloca'd. */
9542
9543 void
9544 message1 (const char *m)
9545 {
9546 message2 (m, (m ? strlen (m) : 0), 0);
9547 }
9548
9549
9550 /* The non-logging counterpart of message1. */
9551
9552 void
9553 message1_nolog (const char *m)
9554 {
9555 message2_nolog (m, (m ? strlen (m) : 0), 0);
9556 }
9557
9558 /* Display a message M which contains a single %s
9559 which gets replaced with STRING. */
9560
9561 void
9562 message_with_string (const char *m, Lisp_Object string, int log)
9563 {
9564 CHECK_STRING (string);
9565
9566 if (noninteractive)
9567 {
9568 if (m)
9569 {
9570 if (noninteractive_need_newline)
9571 putc ('\n', stderr);
9572 noninteractive_need_newline = 0;
9573 fprintf (stderr, m, SDATA (string));
9574 if (!cursor_in_echo_area)
9575 fprintf (stderr, "\n");
9576 fflush (stderr);
9577 }
9578 }
9579 else if (INTERACTIVE)
9580 {
9581 /* The frame whose minibuffer we're going to display the message on.
9582 It may be larger than the selected frame, so we need
9583 to use its buffer, not the selected frame's buffer. */
9584 Lisp_Object mini_window;
9585 struct frame *f, *sf = SELECTED_FRAME ();
9586
9587 /* Get the frame containing the minibuffer
9588 that the selected frame is using. */
9589 mini_window = FRAME_MINIBUF_WINDOW (sf);
9590 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9591
9592 /* A null message buffer means that the frame hasn't really been
9593 initialized yet. Error messages get reported properly by
9594 cmd_error, so this must be just an informative message; toss it. */
9595 if (FRAME_MESSAGE_BUF (f))
9596 {
9597 Lisp_Object args[2], msg;
9598 struct gcpro gcpro1, gcpro2;
9599
9600 args[0] = build_string (m);
9601 args[1] = msg = string;
9602 GCPRO2 (args[0], msg);
9603 gcpro1.nvars = 2;
9604
9605 msg = Fformat (2, args);
9606
9607 if (log)
9608 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9609 else
9610 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9611
9612 UNGCPRO;
9613
9614 /* Print should start at the beginning of the message
9615 buffer next time. */
9616 message_buf_print = 0;
9617 }
9618 }
9619 }
9620
9621
9622 /* Dump an informative message to the minibuf. If M is 0, clear out
9623 any existing message, and let the mini-buffer text show through. */
9624
9625 static void
9626 vmessage (const char *m, va_list ap)
9627 {
9628 if (noninteractive)
9629 {
9630 if (m)
9631 {
9632 if (noninteractive_need_newline)
9633 putc ('\n', stderr);
9634 noninteractive_need_newline = 0;
9635 vfprintf (stderr, m, ap);
9636 if (cursor_in_echo_area == 0)
9637 fprintf (stderr, "\n");
9638 fflush (stderr);
9639 }
9640 }
9641 else if (INTERACTIVE)
9642 {
9643 /* The frame whose mini-buffer we're going to display the message
9644 on. It may be larger than the selected frame, so we need to
9645 use its buffer, not the selected frame's buffer. */
9646 Lisp_Object mini_window;
9647 struct frame *f, *sf = SELECTED_FRAME ();
9648
9649 /* Get the frame containing the mini-buffer
9650 that the selected frame is using. */
9651 mini_window = FRAME_MINIBUF_WINDOW (sf);
9652 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9653
9654 /* A null message buffer means that the frame hasn't really been
9655 initialized yet. Error messages get reported properly by
9656 cmd_error, so this must be just an informative message; toss
9657 it. */
9658 if (FRAME_MESSAGE_BUF (f))
9659 {
9660 if (m)
9661 {
9662 ptrdiff_t len;
9663
9664 len = doprnt (FRAME_MESSAGE_BUF (f),
9665 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9666
9667 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9668 }
9669 else
9670 message1 (0);
9671
9672 /* Print should start at the beginning of the message
9673 buffer next time. */
9674 message_buf_print = 0;
9675 }
9676 }
9677 }
9678
9679 void
9680 message (const char *m, ...)
9681 {
9682 va_list ap;
9683 va_start (ap, m);
9684 vmessage (m, ap);
9685 va_end (ap);
9686 }
9687
9688
9689 #if 0
9690 /* The non-logging version of message. */
9691
9692 void
9693 message_nolog (const char *m, ...)
9694 {
9695 Lisp_Object old_log_max;
9696 va_list ap;
9697 va_start (ap, m);
9698 old_log_max = Vmessage_log_max;
9699 Vmessage_log_max = Qnil;
9700 vmessage (m, ap);
9701 Vmessage_log_max = old_log_max;
9702 va_end (ap);
9703 }
9704 #endif
9705
9706
9707 /* Display the current message in the current mini-buffer. This is
9708 only called from error handlers in process.c, and is not time
9709 critical. */
9710
9711 void
9712 update_echo_area (void)
9713 {
9714 if (!NILP (echo_area_buffer[0]))
9715 {
9716 Lisp_Object string;
9717 string = Fcurrent_message ();
9718 message3 (string, SBYTES (string),
9719 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9720 }
9721 }
9722
9723
9724 /* Make sure echo area buffers in `echo_buffers' are live.
9725 If they aren't, make new ones. */
9726
9727 static void
9728 ensure_echo_area_buffers (void)
9729 {
9730 int i;
9731
9732 for (i = 0; i < 2; ++i)
9733 if (!BUFFERP (echo_buffer[i])
9734 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9735 {
9736 char name[30];
9737 Lisp_Object old_buffer;
9738 int j;
9739
9740 old_buffer = echo_buffer[i];
9741 sprintf (name, " *Echo Area %d*", i);
9742 echo_buffer[i] = Fget_buffer_create (build_string (name));
9743 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9744 /* to force word wrap in echo area -
9745 it was decided to postpone this*/
9746 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9747
9748 for (j = 0; j < 2; ++j)
9749 if (EQ (old_buffer, echo_area_buffer[j]))
9750 echo_area_buffer[j] = echo_buffer[i];
9751 }
9752 }
9753
9754
9755 /* Call FN with args A1..A4 with either the current or last displayed
9756 echo_area_buffer as current buffer.
9757
9758 WHICH zero means use the current message buffer
9759 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9760 from echo_buffer[] and clear it.
9761
9762 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9763 suitable buffer from echo_buffer[] and clear it.
9764
9765 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9766 that the current message becomes the last displayed one, make
9767 choose a suitable buffer for echo_area_buffer[0], and clear it.
9768
9769 Value is what FN returns. */
9770
9771 static int
9772 with_echo_area_buffer (struct window *w, int which,
9773 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9774 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9775 {
9776 Lisp_Object buffer;
9777 int this_one, the_other, clear_buffer_p, rc;
9778 int count = SPECPDL_INDEX ();
9779
9780 /* If buffers aren't live, make new ones. */
9781 ensure_echo_area_buffers ();
9782
9783 clear_buffer_p = 0;
9784
9785 if (which == 0)
9786 this_one = 0, the_other = 1;
9787 else if (which > 0)
9788 this_one = 1, the_other = 0;
9789 else
9790 {
9791 this_one = 0, the_other = 1;
9792 clear_buffer_p = 1;
9793
9794 /* We need a fresh one in case the current echo buffer equals
9795 the one containing the last displayed echo area message. */
9796 if (!NILP (echo_area_buffer[this_one])
9797 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9798 echo_area_buffer[this_one] = Qnil;
9799 }
9800
9801 /* Choose a suitable buffer from echo_buffer[] is we don't
9802 have one. */
9803 if (NILP (echo_area_buffer[this_one]))
9804 {
9805 echo_area_buffer[this_one]
9806 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9807 ? echo_buffer[the_other]
9808 : echo_buffer[this_one]);
9809 clear_buffer_p = 1;
9810 }
9811
9812 buffer = echo_area_buffer[this_one];
9813
9814 /* Don't get confused by reusing the buffer used for echoing
9815 for a different purpose. */
9816 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9817 cancel_echoing ();
9818
9819 record_unwind_protect (unwind_with_echo_area_buffer,
9820 with_echo_area_buffer_unwind_data (w));
9821
9822 /* Make the echo area buffer current. Note that for display
9823 purposes, it is not necessary that the displayed window's buffer
9824 == current_buffer, except for text property lookup. So, let's
9825 only set that buffer temporarily here without doing a full
9826 Fset_window_buffer. We must also change w->pointm, though,
9827 because otherwise an assertions in unshow_buffer fails, and Emacs
9828 aborts. */
9829 set_buffer_internal_1 (XBUFFER (buffer));
9830 if (w)
9831 {
9832 w->buffer = buffer;
9833 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9834 }
9835
9836 BVAR (current_buffer, undo_list) = Qt;
9837 BVAR (current_buffer, read_only) = Qnil;
9838 specbind (Qinhibit_read_only, Qt);
9839 specbind (Qinhibit_modification_hooks, Qt);
9840
9841 if (clear_buffer_p && Z > BEG)
9842 del_range (BEG, Z);
9843
9844 xassert (BEGV >= BEG);
9845 xassert (ZV <= Z && ZV >= BEGV);
9846
9847 rc = fn (a1, a2, a3, a4);
9848
9849 xassert (BEGV >= BEG);
9850 xassert (ZV <= Z && ZV >= BEGV);
9851
9852 unbind_to (count, Qnil);
9853 return rc;
9854 }
9855
9856
9857 /* Save state that should be preserved around the call to the function
9858 FN called in with_echo_area_buffer. */
9859
9860 static Lisp_Object
9861 with_echo_area_buffer_unwind_data (struct window *w)
9862 {
9863 int i = 0;
9864 Lisp_Object vector, tmp;
9865
9866 /* Reduce consing by keeping one vector in
9867 Vwith_echo_area_save_vector. */
9868 vector = Vwith_echo_area_save_vector;
9869 Vwith_echo_area_save_vector = Qnil;
9870
9871 if (NILP (vector))
9872 vector = Fmake_vector (make_number (7), Qnil);
9873
9874 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9875 ASET (vector, i, Vdeactivate_mark); ++i;
9876 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9877
9878 if (w)
9879 {
9880 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9881 ASET (vector, i, w->buffer); ++i;
9882 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9883 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9884 }
9885 else
9886 {
9887 int end = i + 4;
9888 for (; i < end; ++i)
9889 ASET (vector, i, Qnil);
9890 }
9891
9892 xassert (i == ASIZE (vector));
9893 return vector;
9894 }
9895
9896
9897 /* Restore global state from VECTOR which was created by
9898 with_echo_area_buffer_unwind_data. */
9899
9900 static Lisp_Object
9901 unwind_with_echo_area_buffer (Lisp_Object vector)
9902 {
9903 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9904 Vdeactivate_mark = AREF (vector, 1);
9905 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9906
9907 if (WINDOWP (AREF (vector, 3)))
9908 {
9909 struct window *w;
9910 Lisp_Object buffer, charpos, bytepos;
9911
9912 w = XWINDOW (AREF (vector, 3));
9913 buffer = AREF (vector, 4);
9914 charpos = AREF (vector, 5);
9915 bytepos = AREF (vector, 6);
9916
9917 w->buffer = buffer;
9918 set_marker_both (w->pointm, buffer,
9919 XFASTINT (charpos), XFASTINT (bytepos));
9920 }
9921
9922 Vwith_echo_area_save_vector = vector;
9923 return Qnil;
9924 }
9925
9926
9927 /* Set up the echo area for use by print functions. MULTIBYTE_P
9928 non-zero means we will print multibyte. */
9929
9930 void
9931 setup_echo_area_for_printing (int multibyte_p)
9932 {
9933 /* If we can't find an echo area any more, exit. */
9934 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9935 Fkill_emacs (Qnil);
9936
9937 ensure_echo_area_buffers ();
9938
9939 if (!message_buf_print)
9940 {
9941 /* A message has been output since the last time we printed.
9942 Choose a fresh echo area buffer. */
9943 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9944 echo_area_buffer[0] = echo_buffer[1];
9945 else
9946 echo_area_buffer[0] = echo_buffer[0];
9947
9948 /* Switch to that buffer and clear it. */
9949 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9950 BVAR (current_buffer, truncate_lines) = Qnil;
9951
9952 if (Z > BEG)
9953 {
9954 int count = SPECPDL_INDEX ();
9955 specbind (Qinhibit_read_only, Qt);
9956 /* Note that undo recording is always disabled. */
9957 del_range (BEG, Z);
9958 unbind_to (count, Qnil);
9959 }
9960 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9961
9962 /* Set up the buffer for the multibyteness we need. */
9963 if (multibyte_p
9964 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9965 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9966
9967 /* Raise the frame containing the echo area. */
9968 if (minibuffer_auto_raise)
9969 {
9970 struct frame *sf = SELECTED_FRAME ();
9971 Lisp_Object mini_window;
9972 mini_window = FRAME_MINIBUF_WINDOW (sf);
9973 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9974 }
9975
9976 message_log_maybe_newline ();
9977 message_buf_print = 1;
9978 }
9979 else
9980 {
9981 if (NILP (echo_area_buffer[0]))
9982 {
9983 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9984 echo_area_buffer[0] = echo_buffer[1];
9985 else
9986 echo_area_buffer[0] = echo_buffer[0];
9987 }
9988
9989 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9990 {
9991 /* Someone switched buffers between print requests. */
9992 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9993 BVAR (current_buffer, truncate_lines) = Qnil;
9994 }
9995 }
9996 }
9997
9998
9999 /* Display an echo area message in window W. Value is non-zero if W's
10000 height is changed. If display_last_displayed_message_p is
10001 non-zero, display the message that was last displayed, otherwise
10002 display the current message. */
10003
10004 static int
10005 display_echo_area (struct window *w)
10006 {
10007 int i, no_message_p, window_height_changed_p, count;
10008
10009 /* Temporarily disable garbage collections while displaying the echo
10010 area. This is done because a GC can print a message itself.
10011 That message would modify the echo area buffer's contents while a
10012 redisplay of the buffer is going on, and seriously confuse
10013 redisplay. */
10014 count = inhibit_garbage_collection ();
10015
10016 /* If there is no message, we must call display_echo_area_1
10017 nevertheless because it resizes the window. But we will have to
10018 reset the echo_area_buffer in question to nil at the end because
10019 with_echo_area_buffer will sets it to an empty buffer. */
10020 i = display_last_displayed_message_p ? 1 : 0;
10021 no_message_p = NILP (echo_area_buffer[i]);
10022
10023 window_height_changed_p
10024 = with_echo_area_buffer (w, display_last_displayed_message_p,
10025 display_echo_area_1,
10026 (intptr_t) w, Qnil, 0, 0);
10027
10028 if (no_message_p)
10029 echo_area_buffer[i] = Qnil;
10030
10031 unbind_to (count, Qnil);
10032 return window_height_changed_p;
10033 }
10034
10035
10036 /* Helper for display_echo_area. Display the current buffer which
10037 contains the current echo area message in window W, a mini-window,
10038 a pointer to which is passed in A1. A2..A4 are currently not used.
10039 Change the height of W so that all of the message is displayed.
10040 Value is non-zero if height of W was changed. */
10041
10042 static int
10043 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10044 {
10045 intptr_t i1 = a1;
10046 struct window *w = (struct window *) i1;
10047 Lisp_Object window;
10048 struct text_pos start;
10049 int window_height_changed_p = 0;
10050
10051 /* Do this before displaying, so that we have a large enough glyph
10052 matrix for the display. If we can't get enough space for the
10053 whole text, display the last N lines. That works by setting w->start. */
10054 window_height_changed_p = resize_mini_window (w, 0);
10055
10056 /* Use the starting position chosen by resize_mini_window. */
10057 SET_TEXT_POS_FROM_MARKER (start, w->start);
10058
10059 /* Display. */
10060 clear_glyph_matrix (w->desired_matrix);
10061 XSETWINDOW (window, w);
10062 try_window (window, start, 0);
10063
10064 return window_height_changed_p;
10065 }
10066
10067
10068 /* Resize the echo area window to exactly the size needed for the
10069 currently displayed message, if there is one. If a mini-buffer
10070 is active, don't shrink it. */
10071
10072 void
10073 resize_echo_area_exactly (void)
10074 {
10075 if (BUFFERP (echo_area_buffer[0])
10076 && WINDOWP (echo_area_window))
10077 {
10078 struct window *w = XWINDOW (echo_area_window);
10079 int resized_p;
10080 Lisp_Object resize_exactly;
10081
10082 if (minibuf_level == 0)
10083 resize_exactly = Qt;
10084 else
10085 resize_exactly = Qnil;
10086
10087 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10088 (intptr_t) w, resize_exactly,
10089 0, 0);
10090 if (resized_p)
10091 {
10092 ++windows_or_buffers_changed;
10093 ++update_mode_lines;
10094 redisplay_internal ();
10095 }
10096 }
10097 }
10098
10099
10100 /* Callback function for with_echo_area_buffer, when used from
10101 resize_echo_area_exactly. A1 contains a pointer to the window to
10102 resize, EXACTLY non-nil means resize the mini-window exactly to the
10103 size of the text displayed. A3 and A4 are not used. Value is what
10104 resize_mini_window returns. */
10105
10106 static int
10107 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10108 {
10109 intptr_t i1 = a1;
10110 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10111 }
10112
10113
10114 /* Resize mini-window W to fit the size of its contents. EXACT_P
10115 means size the window exactly to the size needed. Otherwise, it's
10116 only enlarged until W's buffer is empty.
10117
10118 Set W->start to the right place to begin display. If the whole
10119 contents fit, start at the beginning. Otherwise, start so as
10120 to make the end of the contents appear. This is particularly
10121 important for y-or-n-p, but seems desirable generally.
10122
10123 Value is non-zero if the window height has been changed. */
10124
10125 int
10126 resize_mini_window (struct window *w, int exact_p)
10127 {
10128 struct frame *f = XFRAME (w->frame);
10129 int window_height_changed_p = 0;
10130
10131 xassert (MINI_WINDOW_P (w));
10132
10133 /* By default, start display at the beginning. */
10134 set_marker_both (w->start, w->buffer,
10135 BUF_BEGV (XBUFFER (w->buffer)),
10136 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10137
10138 /* Don't resize windows while redisplaying a window; it would
10139 confuse redisplay functions when the size of the window they are
10140 displaying changes from under them. Such a resizing can happen,
10141 for instance, when which-func prints a long message while
10142 we are running fontification-functions. We're running these
10143 functions with safe_call which binds inhibit-redisplay to t. */
10144 if (!NILP (Vinhibit_redisplay))
10145 return 0;
10146
10147 /* Nil means don't try to resize. */
10148 if (NILP (Vresize_mini_windows)
10149 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10150 return 0;
10151
10152 if (!FRAME_MINIBUF_ONLY_P (f))
10153 {
10154 struct it it;
10155 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10156 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10157 int height, max_height;
10158 int unit = FRAME_LINE_HEIGHT (f);
10159 struct text_pos start;
10160 struct buffer *old_current_buffer = NULL;
10161
10162 if (current_buffer != XBUFFER (w->buffer))
10163 {
10164 old_current_buffer = current_buffer;
10165 set_buffer_internal (XBUFFER (w->buffer));
10166 }
10167
10168 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10169
10170 /* Compute the max. number of lines specified by the user. */
10171 if (FLOATP (Vmax_mini_window_height))
10172 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10173 else if (INTEGERP (Vmax_mini_window_height))
10174 max_height = XINT (Vmax_mini_window_height);
10175 else
10176 max_height = total_height / 4;
10177
10178 /* Correct that max. height if it's bogus. */
10179 max_height = max (1, max_height);
10180 max_height = min (total_height, max_height);
10181
10182 /* Find out the height of the text in the window. */
10183 if (it.line_wrap == TRUNCATE)
10184 height = 1;
10185 else
10186 {
10187 last_height = 0;
10188 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10189 if (it.max_ascent == 0 && it.max_descent == 0)
10190 height = it.current_y + last_height;
10191 else
10192 height = it.current_y + it.max_ascent + it.max_descent;
10193 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10194 height = (height + unit - 1) / unit;
10195 }
10196
10197 /* Compute a suitable window start. */
10198 if (height > max_height)
10199 {
10200 height = max_height;
10201 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10202 move_it_vertically_backward (&it, (height - 1) * unit);
10203 start = it.current.pos;
10204 }
10205 else
10206 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10207 SET_MARKER_FROM_TEXT_POS (w->start, start);
10208
10209 if (EQ (Vresize_mini_windows, Qgrow_only))
10210 {
10211 /* Let it grow only, until we display an empty message, in which
10212 case the window shrinks again. */
10213 if (height > WINDOW_TOTAL_LINES (w))
10214 {
10215 int old_height = WINDOW_TOTAL_LINES (w);
10216 freeze_window_starts (f, 1);
10217 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10218 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10219 }
10220 else if (height < WINDOW_TOTAL_LINES (w)
10221 && (exact_p || BEGV == ZV))
10222 {
10223 int old_height = WINDOW_TOTAL_LINES (w);
10224 freeze_window_starts (f, 0);
10225 shrink_mini_window (w);
10226 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10227 }
10228 }
10229 else
10230 {
10231 /* Always resize to exact size needed. */
10232 if (height > WINDOW_TOTAL_LINES (w))
10233 {
10234 int old_height = WINDOW_TOTAL_LINES (w);
10235 freeze_window_starts (f, 1);
10236 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10237 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10238 }
10239 else if (height < WINDOW_TOTAL_LINES (w))
10240 {
10241 int old_height = WINDOW_TOTAL_LINES (w);
10242 freeze_window_starts (f, 0);
10243 shrink_mini_window (w);
10244
10245 if (height)
10246 {
10247 freeze_window_starts (f, 1);
10248 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10249 }
10250
10251 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10252 }
10253 }
10254
10255 if (old_current_buffer)
10256 set_buffer_internal (old_current_buffer);
10257 }
10258
10259 return window_height_changed_p;
10260 }
10261
10262
10263 /* Value is the current message, a string, or nil if there is no
10264 current message. */
10265
10266 Lisp_Object
10267 current_message (void)
10268 {
10269 Lisp_Object msg;
10270
10271 if (!BUFFERP (echo_area_buffer[0]))
10272 msg = Qnil;
10273 else
10274 {
10275 with_echo_area_buffer (0, 0, current_message_1,
10276 (intptr_t) &msg, Qnil, 0, 0);
10277 if (NILP (msg))
10278 echo_area_buffer[0] = Qnil;
10279 }
10280
10281 return msg;
10282 }
10283
10284
10285 static int
10286 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10287 {
10288 intptr_t i1 = a1;
10289 Lisp_Object *msg = (Lisp_Object *) i1;
10290
10291 if (Z > BEG)
10292 *msg = make_buffer_string (BEG, Z, 1);
10293 else
10294 *msg = Qnil;
10295 return 0;
10296 }
10297
10298
10299 /* Push the current message on Vmessage_stack for later restoration
10300 by restore_message. Value is non-zero if the current message isn't
10301 empty. This is a relatively infrequent operation, so it's not
10302 worth optimizing. */
10303
10304 int
10305 push_message (void)
10306 {
10307 Lisp_Object msg;
10308 msg = current_message ();
10309 Vmessage_stack = Fcons (msg, Vmessage_stack);
10310 return STRINGP (msg);
10311 }
10312
10313
10314 /* Restore message display from the top of Vmessage_stack. */
10315
10316 void
10317 restore_message (void)
10318 {
10319 Lisp_Object msg;
10320
10321 xassert (CONSP (Vmessage_stack));
10322 msg = XCAR (Vmessage_stack);
10323 if (STRINGP (msg))
10324 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10325 else
10326 message3_nolog (msg, 0, 0);
10327 }
10328
10329
10330 /* Handler for record_unwind_protect calling pop_message. */
10331
10332 Lisp_Object
10333 pop_message_unwind (Lisp_Object dummy)
10334 {
10335 pop_message ();
10336 return Qnil;
10337 }
10338
10339 /* Pop the top-most entry off Vmessage_stack. */
10340
10341 static void
10342 pop_message (void)
10343 {
10344 xassert (CONSP (Vmessage_stack));
10345 Vmessage_stack = XCDR (Vmessage_stack);
10346 }
10347
10348
10349 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10350 exits. If the stack is not empty, we have a missing pop_message
10351 somewhere. */
10352
10353 void
10354 check_message_stack (void)
10355 {
10356 if (!NILP (Vmessage_stack))
10357 abort ();
10358 }
10359
10360
10361 /* Truncate to NCHARS what will be displayed in the echo area the next
10362 time we display it---but don't redisplay it now. */
10363
10364 void
10365 truncate_echo_area (EMACS_INT nchars)
10366 {
10367 if (nchars == 0)
10368 echo_area_buffer[0] = Qnil;
10369 /* A null message buffer means that the frame hasn't really been
10370 initialized yet. Error messages get reported properly by
10371 cmd_error, so this must be just an informative message; toss it. */
10372 else if (!noninteractive
10373 && INTERACTIVE
10374 && !NILP (echo_area_buffer[0]))
10375 {
10376 struct frame *sf = SELECTED_FRAME ();
10377 if (FRAME_MESSAGE_BUF (sf))
10378 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10379 }
10380 }
10381
10382
10383 /* Helper function for truncate_echo_area. Truncate the current
10384 message to at most NCHARS characters. */
10385
10386 static int
10387 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10388 {
10389 if (BEG + nchars < Z)
10390 del_range (BEG + nchars, Z);
10391 if (Z == BEG)
10392 echo_area_buffer[0] = Qnil;
10393 return 0;
10394 }
10395
10396
10397 /* Set the current message to a substring of S or STRING.
10398
10399 If STRING is a Lisp string, set the message to the first NBYTES
10400 bytes from STRING. NBYTES zero means use the whole string. If
10401 STRING is multibyte, the message will be displayed multibyte.
10402
10403 If S is not null, set the message to the first LEN bytes of S. LEN
10404 zero means use the whole string. MULTIBYTE_P non-zero means S is
10405 multibyte. Display the message multibyte in that case.
10406
10407 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10408 to t before calling set_message_1 (which calls insert).
10409 */
10410
10411 static void
10412 set_message (const char *s, Lisp_Object string,
10413 EMACS_INT nbytes, int multibyte_p)
10414 {
10415 message_enable_multibyte
10416 = ((s && multibyte_p)
10417 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10418
10419 with_echo_area_buffer (0, -1, set_message_1,
10420 (intptr_t) s, string, nbytes, multibyte_p);
10421 message_buf_print = 0;
10422 help_echo_showing_p = 0;
10423 }
10424
10425
10426 /* Helper function for set_message. Arguments have the same meaning
10427 as there, with A1 corresponding to S and A2 corresponding to STRING
10428 This function is called with the echo area buffer being
10429 current. */
10430
10431 static int
10432 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10433 {
10434 intptr_t i1 = a1;
10435 const char *s = (const char *) i1;
10436 const unsigned char *msg = (const unsigned char *) s;
10437 Lisp_Object string = a2;
10438
10439 /* Change multibyteness of the echo buffer appropriately. */
10440 if (message_enable_multibyte
10441 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10442 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10443
10444 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10445 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10446 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10447
10448 /* Insert new message at BEG. */
10449 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10450
10451 if (STRINGP (string))
10452 {
10453 EMACS_INT nchars;
10454
10455 if (nbytes == 0)
10456 nbytes = SBYTES (string);
10457 nchars = string_byte_to_char (string, nbytes);
10458
10459 /* This function takes care of single/multibyte conversion. We
10460 just have to ensure that the echo area buffer has the right
10461 setting of enable_multibyte_characters. */
10462 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10463 }
10464 else if (s)
10465 {
10466 if (nbytes == 0)
10467 nbytes = strlen (s);
10468
10469 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10470 {
10471 /* Convert from multi-byte to single-byte. */
10472 EMACS_INT i;
10473 int c, n;
10474 char work[1];
10475
10476 /* Convert a multibyte string to single-byte. */
10477 for (i = 0; i < nbytes; i += n)
10478 {
10479 c = string_char_and_length (msg + i, &n);
10480 work[0] = (ASCII_CHAR_P (c)
10481 ? c
10482 : multibyte_char_to_unibyte (c));
10483 insert_1_both (work, 1, 1, 1, 0, 0);
10484 }
10485 }
10486 else if (!multibyte_p
10487 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10488 {
10489 /* Convert from single-byte to multi-byte. */
10490 EMACS_INT i;
10491 int c, n;
10492 unsigned char str[MAX_MULTIBYTE_LENGTH];
10493
10494 /* Convert a single-byte string to multibyte. */
10495 for (i = 0; i < nbytes; i++)
10496 {
10497 c = msg[i];
10498 MAKE_CHAR_MULTIBYTE (c);
10499 n = CHAR_STRING (c, str);
10500 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10501 }
10502 }
10503 else
10504 insert_1 (s, nbytes, 1, 0, 0);
10505 }
10506
10507 return 0;
10508 }
10509
10510
10511 /* Clear messages. CURRENT_P non-zero means clear the current
10512 message. LAST_DISPLAYED_P non-zero means clear the message
10513 last displayed. */
10514
10515 void
10516 clear_message (int current_p, int last_displayed_p)
10517 {
10518 if (current_p)
10519 {
10520 echo_area_buffer[0] = Qnil;
10521 message_cleared_p = 1;
10522 }
10523
10524 if (last_displayed_p)
10525 echo_area_buffer[1] = Qnil;
10526
10527 message_buf_print = 0;
10528 }
10529
10530 /* Clear garbaged frames.
10531
10532 This function is used where the old redisplay called
10533 redraw_garbaged_frames which in turn called redraw_frame which in
10534 turn called clear_frame. The call to clear_frame was a source of
10535 flickering. I believe a clear_frame is not necessary. It should
10536 suffice in the new redisplay to invalidate all current matrices,
10537 and ensure a complete redisplay of all windows. */
10538
10539 static void
10540 clear_garbaged_frames (void)
10541 {
10542 if (frame_garbaged)
10543 {
10544 Lisp_Object tail, frame;
10545 int changed_count = 0;
10546
10547 FOR_EACH_FRAME (tail, frame)
10548 {
10549 struct frame *f = XFRAME (frame);
10550
10551 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10552 {
10553 if (f->resized_p)
10554 {
10555 Fredraw_frame (frame);
10556 f->force_flush_display_p = 1;
10557 }
10558 clear_current_matrices (f);
10559 changed_count++;
10560 f->garbaged = 0;
10561 f->resized_p = 0;
10562 }
10563 }
10564
10565 frame_garbaged = 0;
10566 if (changed_count)
10567 ++windows_or_buffers_changed;
10568 }
10569 }
10570
10571
10572 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10573 is non-zero update selected_frame. Value is non-zero if the
10574 mini-windows height has been changed. */
10575
10576 static int
10577 echo_area_display (int update_frame_p)
10578 {
10579 Lisp_Object mini_window;
10580 struct window *w;
10581 struct frame *f;
10582 int window_height_changed_p = 0;
10583 struct frame *sf = SELECTED_FRAME ();
10584
10585 mini_window = FRAME_MINIBUF_WINDOW (sf);
10586 w = XWINDOW (mini_window);
10587 f = XFRAME (WINDOW_FRAME (w));
10588
10589 /* Don't display if frame is invisible or not yet initialized. */
10590 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10591 return 0;
10592
10593 #ifdef HAVE_WINDOW_SYSTEM
10594 /* When Emacs starts, selected_frame may be the initial terminal
10595 frame. If we let this through, a message would be displayed on
10596 the terminal. */
10597 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10598 return 0;
10599 #endif /* HAVE_WINDOW_SYSTEM */
10600
10601 /* Redraw garbaged frames. */
10602 if (frame_garbaged)
10603 clear_garbaged_frames ();
10604
10605 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10606 {
10607 echo_area_window = mini_window;
10608 window_height_changed_p = display_echo_area (w);
10609 w->must_be_updated_p = 1;
10610
10611 /* Update the display, unless called from redisplay_internal.
10612 Also don't update the screen during redisplay itself. The
10613 update will happen at the end of redisplay, and an update
10614 here could cause confusion. */
10615 if (update_frame_p && !redisplaying_p)
10616 {
10617 int n = 0;
10618
10619 /* If the display update has been interrupted by pending
10620 input, update mode lines in the frame. Due to the
10621 pending input, it might have been that redisplay hasn't
10622 been called, so that mode lines above the echo area are
10623 garbaged. This looks odd, so we prevent it here. */
10624 if (!display_completed)
10625 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10626
10627 if (window_height_changed_p
10628 /* Don't do this if Emacs is shutting down. Redisplay
10629 needs to run hooks. */
10630 && !NILP (Vrun_hooks))
10631 {
10632 /* Must update other windows. Likewise as in other
10633 cases, don't let this update be interrupted by
10634 pending input. */
10635 int count = SPECPDL_INDEX ();
10636 specbind (Qredisplay_dont_pause, Qt);
10637 windows_or_buffers_changed = 1;
10638 redisplay_internal ();
10639 unbind_to (count, Qnil);
10640 }
10641 else if (FRAME_WINDOW_P (f) && n == 0)
10642 {
10643 /* Window configuration is the same as before.
10644 Can do with a display update of the echo area,
10645 unless we displayed some mode lines. */
10646 update_single_window (w, 1);
10647 FRAME_RIF (f)->flush_display (f);
10648 }
10649 else
10650 update_frame (f, 1, 1);
10651
10652 /* If cursor is in the echo area, make sure that the next
10653 redisplay displays the minibuffer, so that the cursor will
10654 be replaced with what the minibuffer wants. */
10655 if (cursor_in_echo_area)
10656 ++windows_or_buffers_changed;
10657 }
10658 }
10659 else if (!EQ (mini_window, selected_window))
10660 windows_or_buffers_changed++;
10661
10662 /* Last displayed message is now the current message. */
10663 echo_area_buffer[1] = echo_area_buffer[0];
10664 /* Inform read_char that we're not echoing. */
10665 echo_message_buffer = Qnil;
10666
10667 /* Prevent redisplay optimization in redisplay_internal by resetting
10668 this_line_start_pos. This is done because the mini-buffer now
10669 displays the message instead of its buffer text. */
10670 if (EQ (mini_window, selected_window))
10671 CHARPOS (this_line_start_pos) = 0;
10672
10673 return window_height_changed_p;
10674 }
10675
10676
10677 \f
10678 /***********************************************************************
10679 Mode Lines and Frame Titles
10680 ***********************************************************************/
10681
10682 /* A buffer for constructing non-propertized mode-line strings and
10683 frame titles in it; allocated from the heap in init_xdisp and
10684 resized as needed in store_mode_line_noprop_char. */
10685
10686 static char *mode_line_noprop_buf;
10687
10688 /* The buffer's end, and a current output position in it. */
10689
10690 static char *mode_line_noprop_buf_end;
10691 static char *mode_line_noprop_ptr;
10692
10693 #define MODE_LINE_NOPROP_LEN(start) \
10694 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10695
10696 static enum {
10697 MODE_LINE_DISPLAY = 0,
10698 MODE_LINE_TITLE,
10699 MODE_LINE_NOPROP,
10700 MODE_LINE_STRING
10701 } mode_line_target;
10702
10703 /* Alist that caches the results of :propertize.
10704 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10705 static Lisp_Object mode_line_proptrans_alist;
10706
10707 /* List of strings making up the mode-line. */
10708 static Lisp_Object mode_line_string_list;
10709
10710 /* Base face property when building propertized mode line string. */
10711 static Lisp_Object mode_line_string_face;
10712 static Lisp_Object mode_line_string_face_prop;
10713
10714
10715 /* Unwind data for mode line strings */
10716
10717 static Lisp_Object Vmode_line_unwind_vector;
10718
10719 static Lisp_Object
10720 format_mode_line_unwind_data (struct buffer *obuf,
10721 Lisp_Object owin,
10722 int save_proptrans)
10723 {
10724 Lisp_Object vector, tmp;
10725
10726 /* Reduce consing by keeping one vector in
10727 Vwith_echo_area_save_vector. */
10728 vector = Vmode_line_unwind_vector;
10729 Vmode_line_unwind_vector = Qnil;
10730
10731 if (NILP (vector))
10732 vector = Fmake_vector (make_number (8), Qnil);
10733
10734 ASET (vector, 0, make_number (mode_line_target));
10735 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10736 ASET (vector, 2, mode_line_string_list);
10737 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10738 ASET (vector, 4, mode_line_string_face);
10739 ASET (vector, 5, mode_line_string_face_prop);
10740
10741 if (obuf)
10742 XSETBUFFER (tmp, obuf);
10743 else
10744 tmp = Qnil;
10745 ASET (vector, 6, tmp);
10746 ASET (vector, 7, owin);
10747
10748 return vector;
10749 }
10750
10751 static Lisp_Object
10752 unwind_format_mode_line (Lisp_Object vector)
10753 {
10754 mode_line_target = XINT (AREF (vector, 0));
10755 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10756 mode_line_string_list = AREF (vector, 2);
10757 if (! EQ (AREF (vector, 3), Qt))
10758 mode_line_proptrans_alist = AREF (vector, 3);
10759 mode_line_string_face = AREF (vector, 4);
10760 mode_line_string_face_prop = AREF (vector, 5);
10761
10762 if (!NILP (AREF (vector, 7)))
10763 /* Select window before buffer, since it may change the buffer. */
10764 Fselect_window (AREF (vector, 7), Qt);
10765
10766 if (!NILP (AREF (vector, 6)))
10767 {
10768 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10769 ASET (vector, 6, Qnil);
10770 }
10771
10772 Vmode_line_unwind_vector = vector;
10773 return Qnil;
10774 }
10775
10776
10777 /* Store a single character C for the frame title in mode_line_noprop_buf.
10778 Re-allocate mode_line_noprop_buf if necessary. */
10779
10780 static void
10781 store_mode_line_noprop_char (char c)
10782 {
10783 /* If output position has reached the end of the allocated buffer,
10784 increase the buffer's size. */
10785 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10786 {
10787 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10788 ptrdiff_t size = len;
10789 mode_line_noprop_buf =
10790 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10791 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10792 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10793 }
10794
10795 *mode_line_noprop_ptr++ = c;
10796 }
10797
10798
10799 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10800 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10801 characters that yield more columns than PRECISION; PRECISION <= 0
10802 means copy the whole string. Pad with spaces until FIELD_WIDTH
10803 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10804 pad. Called from display_mode_element when it is used to build a
10805 frame title. */
10806
10807 static int
10808 store_mode_line_noprop (const char *string, int field_width, int precision)
10809 {
10810 const unsigned char *str = (const unsigned char *) string;
10811 int n = 0;
10812 EMACS_INT dummy, nbytes;
10813
10814 /* Copy at most PRECISION chars from STR. */
10815 nbytes = strlen (string);
10816 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10817 while (nbytes--)
10818 store_mode_line_noprop_char (*str++);
10819
10820 /* Fill up with spaces until FIELD_WIDTH reached. */
10821 while (field_width > 0
10822 && n < field_width)
10823 {
10824 store_mode_line_noprop_char (' ');
10825 ++n;
10826 }
10827
10828 return n;
10829 }
10830
10831 /***********************************************************************
10832 Frame Titles
10833 ***********************************************************************/
10834
10835 #ifdef HAVE_WINDOW_SYSTEM
10836
10837 /* Set the title of FRAME, if it has changed. The title format is
10838 Vicon_title_format if FRAME is iconified, otherwise it is
10839 frame_title_format. */
10840
10841 static void
10842 x_consider_frame_title (Lisp_Object frame)
10843 {
10844 struct frame *f = XFRAME (frame);
10845
10846 if (FRAME_WINDOW_P (f)
10847 || FRAME_MINIBUF_ONLY_P (f)
10848 || f->explicit_name)
10849 {
10850 /* Do we have more than one visible frame on this X display? */
10851 Lisp_Object tail;
10852 Lisp_Object fmt;
10853 ptrdiff_t title_start;
10854 char *title;
10855 ptrdiff_t len;
10856 struct it it;
10857 int count = SPECPDL_INDEX ();
10858
10859 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10860 {
10861 Lisp_Object other_frame = XCAR (tail);
10862 struct frame *tf = XFRAME (other_frame);
10863
10864 if (tf != f
10865 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10866 && !FRAME_MINIBUF_ONLY_P (tf)
10867 && !EQ (other_frame, tip_frame)
10868 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10869 break;
10870 }
10871
10872 /* Set global variable indicating that multiple frames exist. */
10873 multiple_frames = CONSP (tail);
10874
10875 /* Switch to the buffer of selected window of the frame. Set up
10876 mode_line_target so that display_mode_element will output into
10877 mode_line_noprop_buf; then display the title. */
10878 record_unwind_protect (unwind_format_mode_line,
10879 format_mode_line_unwind_data
10880 (current_buffer, selected_window, 0));
10881
10882 Fselect_window (f->selected_window, Qt);
10883 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10884 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10885
10886 mode_line_target = MODE_LINE_TITLE;
10887 title_start = MODE_LINE_NOPROP_LEN (0);
10888 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10889 NULL, DEFAULT_FACE_ID);
10890 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10891 len = MODE_LINE_NOPROP_LEN (title_start);
10892 title = mode_line_noprop_buf + title_start;
10893 unbind_to (count, Qnil);
10894
10895 /* Set the title only if it's changed. This avoids consing in
10896 the common case where it hasn't. (If it turns out that we've
10897 already wasted too much time by walking through the list with
10898 display_mode_element, then we might need to optimize at a
10899 higher level than this.) */
10900 if (! STRINGP (f->name)
10901 || SBYTES (f->name) != len
10902 || memcmp (title, SDATA (f->name), len) != 0)
10903 x_implicitly_set_name (f, make_string (title, len), Qnil);
10904 }
10905 }
10906
10907 #endif /* not HAVE_WINDOW_SYSTEM */
10908
10909
10910
10911 \f
10912 /***********************************************************************
10913 Menu Bars
10914 ***********************************************************************/
10915
10916
10917 /* Prepare for redisplay by updating menu-bar item lists when
10918 appropriate. This can call eval. */
10919
10920 void
10921 prepare_menu_bars (void)
10922 {
10923 int all_windows;
10924 struct gcpro gcpro1, gcpro2;
10925 struct frame *f;
10926 Lisp_Object tooltip_frame;
10927
10928 #ifdef HAVE_WINDOW_SYSTEM
10929 tooltip_frame = tip_frame;
10930 #else
10931 tooltip_frame = Qnil;
10932 #endif
10933
10934 /* Update all frame titles based on their buffer names, etc. We do
10935 this before the menu bars so that the buffer-menu will show the
10936 up-to-date frame titles. */
10937 #ifdef HAVE_WINDOW_SYSTEM
10938 if (windows_or_buffers_changed || update_mode_lines)
10939 {
10940 Lisp_Object tail, frame;
10941
10942 FOR_EACH_FRAME (tail, frame)
10943 {
10944 f = XFRAME (frame);
10945 if (!EQ (frame, tooltip_frame)
10946 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10947 x_consider_frame_title (frame);
10948 }
10949 }
10950 #endif /* HAVE_WINDOW_SYSTEM */
10951
10952 /* Update the menu bar item lists, if appropriate. This has to be
10953 done before any actual redisplay or generation of display lines. */
10954 all_windows = (update_mode_lines
10955 || buffer_shared > 1
10956 || windows_or_buffers_changed);
10957 if (all_windows)
10958 {
10959 Lisp_Object tail, frame;
10960 int count = SPECPDL_INDEX ();
10961 /* 1 means that update_menu_bar has run its hooks
10962 so any further calls to update_menu_bar shouldn't do so again. */
10963 int menu_bar_hooks_run = 0;
10964
10965 record_unwind_save_match_data ();
10966
10967 FOR_EACH_FRAME (tail, frame)
10968 {
10969 f = XFRAME (frame);
10970
10971 /* Ignore tooltip frame. */
10972 if (EQ (frame, tooltip_frame))
10973 continue;
10974
10975 /* If a window on this frame changed size, report that to
10976 the user and clear the size-change flag. */
10977 if (FRAME_WINDOW_SIZES_CHANGED (f))
10978 {
10979 Lisp_Object functions;
10980
10981 /* Clear flag first in case we get an error below. */
10982 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10983 functions = Vwindow_size_change_functions;
10984 GCPRO2 (tail, functions);
10985
10986 while (CONSP (functions))
10987 {
10988 if (!EQ (XCAR (functions), Qt))
10989 call1 (XCAR (functions), frame);
10990 functions = XCDR (functions);
10991 }
10992 UNGCPRO;
10993 }
10994
10995 GCPRO1 (tail);
10996 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10997 #ifdef HAVE_WINDOW_SYSTEM
10998 update_tool_bar (f, 0);
10999 #endif
11000 #ifdef HAVE_NS
11001 if (windows_or_buffers_changed
11002 && FRAME_NS_P (f))
11003 ns_set_doc_edited (f, Fbuffer_modified_p
11004 (XWINDOW (f->selected_window)->buffer));
11005 #endif
11006 UNGCPRO;
11007 }
11008
11009 unbind_to (count, Qnil);
11010 }
11011 else
11012 {
11013 struct frame *sf = SELECTED_FRAME ();
11014 update_menu_bar (sf, 1, 0);
11015 #ifdef HAVE_WINDOW_SYSTEM
11016 update_tool_bar (sf, 1);
11017 #endif
11018 }
11019 }
11020
11021
11022 /* Update the menu bar item list for frame F. This has to be done
11023 before we start to fill in any display lines, because it can call
11024 eval.
11025
11026 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11027
11028 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11029 already ran the menu bar hooks for this redisplay, so there
11030 is no need to run them again. The return value is the
11031 updated value of this flag, to pass to the next call. */
11032
11033 static int
11034 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11035 {
11036 Lisp_Object window;
11037 register struct window *w;
11038
11039 /* If called recursively during a menu update, do nothing. This can
11040 happen when, for instance, an activate-menubar-hook causes a
11041 redisplay. */
11042 if (inhibit_menubar_update)
11043 return hooks_run;
11044
11045 window = FRAME_SELECTED_WINDOW (f);
11046 w = XWINDOW (window);
11047
11048 if (FRAME_WINDOW_P (f)
11049 ?
11050 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11051 || defined (HAVE_NS) || defined (USE_GTK)
11052 FRAME_EXTERNAL_MENU_BAR (f)
11053 #else
11054 FRAME_MENU_BAR_LINES (f) > 0
11055 #endif
11056 : FRAME_MENU_BAR_LINES (f) > 0)
11057 {
11058 /* If the user has switched buffers or windows, we need to
11059 recompute to reflect the new bindings. But we'll
11060 recompute when update_mode_lines is set too; that means
11061 that people can use force-mode-line-update to request
11062 that the menu bar be recomputed. The adverse effect on
11063 the rest of the redisplay algorithm is about the same as
11064 windows_or_buffers_changed anyway. */
11065 if (windows_or_buffers_changed
11066 /* This used to test w->update_mode_line, but we believe
11067 there is no need to recompute the menu in that case. */
11068 || update_mode_lines
11069 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11070 < BUF_MODIFF (XBUFFER (w->buffer)))
11071 != !NILP (w->last_had_star))
11072 || ((!NILP (Vtransient_mark_mode)
11073 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11074 != !NILP (w->region_showing)))
11075 {
11076 struct buffer *prev = current_buffer;
11077 int count = SPECPDL_INDEX ();
11078
11079 specbind (Qinhibit_menubar_update, Qt);
11080
11081 set_buffer_internal_1 (XBUFFER (w->buffer));
11082 if (save_match_data)
11083 record_unwind_save_match_data ();
11084 if (NILP (Voverriding_local_map_menu_flag))
11085 {
11086 specbind (Qoverriding_terminal_local_map, Qnil);
11087 specbind (Qoverriding_local_map, Qnil);
11088 }
11089
11090 if (!hooks_run)
11091 {
11092 /* Run the Lucid hook. */
11093 safe_run_hooks (Qactivate_menubar_hook);
11094
11095 /* If it has changed current-menubar from previous value,
11096 really recompute the menu-bar from the value. */
11097 if (! NILP (Vlucid_menu_bar_dirty_flag))
11098 call0 (Qrecompute_lucid_menubar);
11099
11100 safe_run_hooks (Qmenu_bar_update_hook);
11101
11102 hooks_run = 1;
11103 }
11104
11105 XSETFRAME (Vmenu_updating_frame, f);
11106 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11107
11108 /* Redisplay the menu bar in case we changed it. */
11109 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11110 || defined (HAVE_NS) || defined (USE_GTK)
11111 if (FRAME_WINDOW_P (f))
11112 {
11113 #if defined (HAVE_NS)
11114 /* All frames on Mac OS share the same menubar. So only
11115 the selected frame should be allowed to set it. */
11116 if (f == SELECTED_FRAME ())
11117 #endif
11118 set_frame_menubar (f, 0, 0);
11119 }
11120 else
11121 /* On a terminal screen, the menu bar is an ordinary screen
11122 line, and this makes it get updated. */
11123 w->update_mode_line = Qt;
11124 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11125 /* In the non-toolkit version, the menu bar is an ordinary screen
11126 line, and this makes it get updated. */
11127 w->update_mode_line = Qt;
11128 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11129
11130 unbind_to (count, Qnil);
11131 set_buffer_internal_1 (prev);
11132 }
11133 }
11134
11135 return hooks_run;
11136 }
11137
11138
11139 \f
11140 /***********************************************************************
11141 Output Cursor
11142 ***********************************************************************/
11143
11144 #ifdef HAVE_WINDOW_SYSTEM
11145
11146 /* EXPORT:
11147 Nominal cursor position -- where to draw output.
11148 HPOS and VPOS are window relative glyph matrix coordinates.
11149 X and Y are window relative pixel coordinates. */
11150
11151 struct cursor_pos output_cursor;
11152
11153
11154 /* EXPORT:
11155 Set the global variable output_cursor to CURSOR. All cursor
11156 positions are relative to updated_window. */
11157
11158 void
11159 set_output_cursor (struct cursor_pos *cursor)
11160 {
11161 output_cursor.hpos = cursor->hpos;
11162 output_cursor.vpos = cursor->vpos;
11163 output_cursor.x = cursor->x;
11164 output_cursor.y = cursor->y;
11165 }
11166
11167
11168 /* EXPORT for RIF:
11169 Set a nominal cursor position.
11170
11171 HPOS and VPOS are column/row positions in a window glyph matrix. X
11172 and Y are window text area relative pixel positions.
11173
11174 If this is done during an update, updated_window will contain the
11175 window that is being updated and the position is the future output
11176 cursor position for that window. If updated_window is null, use
11177 selected_window and display the cursor at the given position. */
11178
11179 void
11180 x_cursor_to (int vpos, int hpos, int y, int x)
11181 {
11182 struct window *w;
11183
11184 /* If updated_window is not set, work on selected_window. */
11185 if (updated_window)
11186 w = updated_window;
11187 else
11188 w = XWINDOW (selected_window);
11189
11190 /* Set the output cursor. */
11191 output_cursor.hpos = hpos;
11192 output_cursor.vpos = vpos;
11193 output_cursor.x = x;
11194 output_cursor.y = y;
11195
11196 /* If not called as part of an update, really display the cursor.
11197 This will also set the cursor position of W. */
11198 if (updated_window == NULL)
11199 {
11200 BLOCK_INPUT;
11201 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11202 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11203 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11204 UNBLOCK_INPUT;
11205 }
11206 }
11207
11208 #endif /* HAVE_WINDOW_SYSTEM */
11209
11210 \f
11211 /***********************************************************************
11212 Tool-bars
11213 ***********************************************************************/
11214
11215 #ifdef HAVE_WINDOW_SYSTEM
11216
11217 /* Where the mouse was last time we reported a mouse event. */
11218
11219 FRAME_PTR last_mouse_frame;
11220
11221 /* Tool-bar item index of the item on which a mouse button was pressed
11222 or -1. */
11223
11224 int last_tool_bar_item;
11225
11226
11227 static Lisp_Object
11228 update_tool_bar_unwind (Lisp_Object frame)
11229 {
11230 selected_frame = frame;
11231 return Qnil;
11232 }
11233
11234 /* Update the tool-bar item list for frame F. This has to be done
11235 before we start to fill in any display lines. Called from
11236 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11237 and restore it here. */
11238
11239 static void
11240 update_tool_bar (struct frame *f, int save_match_data)
11241 {
11242 #if defined (USE_GTK) || defined (HAVE_NS)
11243 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11244 #else
11245 int do_update = WINDOWP (f->tool_bar_window)
11246 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11247 #endif
11248
11249 if (do_update)
11250 {
11251 Lisp_Object window;
11252 struct window *w;
11253
11254 window = FRAME_SELECTED_WINDOW (f);
11255 w = XWINDOW (window);
11256
11257 /* If the user has switched buffers or windows, we need to
11258 recompute to reflect the new bindings. But we'll
11259 recompute when update_mode_lines is set too; that means
11260 that people can use force-mode-line-update to request
11261 that the menu bar be recomputed. The adverse effect on
11262 the rest of the redisplay algorithm is about the same as
11263 windows_or_buffers_changed anyway. */
11264 if (windows_or_buffers_changed
11265 || !NILP (w->update_mode_line)
11266 || update_mode_lines
11267 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11268 < BUF_MODIFF (XBUFFER (w->buffer)))
11269 != !NILP (w->last_had_star))
11270 || ((!NILP (Vtransient_mark_mode)
11271 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11272 != !NILP (w->region_showing)))
11273 {
11274 struct buffer *prev = current_buffer;
11275 int count = SPECPDL_INDEX ();
11276 Lisp_Object frame, new_tool_bar;
11277 int new_n_tool_bar;
11278 struct gcpro gcpro1;
11279
11280 /* Set current_buffer to the buffer of the selected
11281 window of the frame, so that we get the right local
11282 keymaps. */
11283 set_buffer_internal_1 (XBUFFER (w->buffer));
11284
11285 /* Save match data, if we must. */
11286 if (save_match_data)
11287 record_unwind_save_match_data ();
11288
11289 /* Make sure that we don't accidentally use bogus keymaps. */
11290 if (NILP (Voverriding_local_map_menu_flag))
11291 {
11292 specbind (Qoverriding_terminal_local_map, Qnil);
11293 specbind (Qoverriding_local_map, Qnil);
11294 }
11295
11296 GCPRO1 (new_tool_bar);
11297
11298 /* We must temporarily set the selected frame to this frame
11299 before calling tool_bar_items, because the calculation of
11300 the tool-bar keymap uses the selected frame (see
11301 `tool-bar-make-keymap' in tool-bar.el). */
11302 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11303 XSETFRAME (frame, f);
11304 selected_frame = frame;
11305
11306 /* Build desired tool-bar items from keymaps. */
11307 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11308 &new_n_tool_bar);
11309
11310 /* Redisplay the tool-bar if we changed it. */
11311 if (new_n_tool_bar != f->n_tool_bar_items
11312 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11313 {
11314 /* Redisplay that happens asynchronously due to an expose event
11315 may access f->tool_bar_items. Make sure we update both
11316 variables within BLOCK_INPUT so no such event interrupts. */
11317 BLOCK_INPUT;
11318 f->tool_bar_items = new_tool_bar;
11319 f->n_tool_bar_items = new_n_tool_bar;
11320 w->update_mode_line = Qt;
11321 UNBLOCK_INPUT;
11322 }
11323
11324 UNGCPRO;
11325
11326 unbind_to (count, Qnil);
11327 set_buffer_internal_1 (prev);
11328 }
11329 }
11330 }
11331
11332
11333 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11334 F's desired tool-bar contents. F->tool_bar_items must have
11335 been set up previously by calling prepare_menu_bars. */
11336
11337 static void
11338 build_desired_tool_bar_string (struct frame *f)
11339 {
11340 int i, size, size_needed;
11341 struct gcpro gcpro1, gcpro2, gcpro3;
11342 Lisp_Object image, plist, props;
11343
11344 image = plist = props = Qnil;
11345 GCPRO3 (image, plist, props);
11346
11347 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11348 Otherwise, make a new string. */
11349
11350 /* The size of the string we might be able to reuse. */
11351 size = (STRINGP (f->desired_tool_bar_string)
11352 ? SCHARS (f->desired_tool_bar_string)
11353 : 0);
11354
11355 /* We need one space in the string for each image. */
11356 size_needed = f->n_tool_bar_items;
11357
11358 /* Reuse f->desired_tool_bar_string, if possible. */
11359 if (size < size_needed || NILP (f->desired_tool_bar_string))
11360 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11361 make_number (' '));
11362 else
11363 {
11364 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11365 Fremove_text_properties (make_number (0), make_number (size),
11366 props, f->desired_tool_bar_string);
11367 }
11368
11369 /* Put a `display' property on the string for the images to display,
11370 put a `menu_item' property on tool-bar items with a value that
11371 is the index of the item in F's tool-bar item vector. */
11372 for (i = 0; i < f->n_tool_bar_items; ++i)
11373 {
11374 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11375
11376 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11377 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11378 int hmargin, vmargin, relief, idx, end;
11379
11380 /* If image is a vector, choose the image according to the
11381 button state. */
11382 image = PROP (TOOL_BAR_ITEM_IMAGES);
11383 if (VECTORP (image))
11384 {
11385 if (enabled_p)
11386 idx = (selected_p
11387 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11388 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11389 else
11390 idx = (selected_p
11391 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11392 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11393
11394 xassert (ASIZE (image) >= idx);
11395 image = AREF (image, idx);
11396 }
11397 else
11398 idx = -1;
11399
11400 /* Ignore invalid image specifications. */
11401 if (!valid_image_p (image))
11402 continue;
11403
11404 /* Display the tool-bar button pressed, or depressed. */
11405 plist = Fcopy_sequence (XCDR (image));
11406
11407 /* Compute margin and relief to draw. */
11408 relief = (tool_bar_button_relief >= 0
11409 ? tool_bar_button_relief
11410 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11411 hmargin = vmargin = relief;
11412
11413 if (INTEGERP (Vtool_bar_button_margin)
11414 && XINT (Vtool_bar_button_margin) > 0)
11415 {
11416 hmargin += XFASTINT (Vtool_bar_button_margin);
11417 vmargin += XFASTINT (Vtool_bar_button_margin);
11418 }
11419 else if (CONSP (Vtool_bar_button_margin))
11420 {
11421 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11422 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11423 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11424
11425 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11426 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11427 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11428 }
11429
11430 if (auto_raise_tool_bar_buttons_p)
11431 {
11432 /* Add a `:relief' property to the image spec if the item is
11433 selected. */
11434 if (selected_p)
11435 {
11436 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11437 hmargin -= relief;
11438 vmargin -= relief;
11439 }
11440 }
11441 else
11442 {
11443 /* If image is selected, display it pressed, i.e. with a
11444 negative relief. If it's not selected, display it with a
11445 raised relief. */
11446 plist = Fplist_put (plist, QCrelief,
11447 (selected_p
11448 ? make_number (-relief)
11449 : make_number (relief)));
11450 hmargin -= relief;
11451 vmargin -= relief;
11452 }
11453
11454 /* Put a margin around the image. */
11455 if (hmargin || vmargin)
11456 {
11457 if (hmargin == vmargin)
11458 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11459 else
11460 plist = Fplist_put (plist, QCmargin,
11461 Fcons (make_number (hmargin),
11462 make_number (vmargin)));
11463 }
11464
11465 /* If button is not enabled, and we don't have special images
11466 for the disabled state, make the image appear disabled by
11467 applying an appropriate algorithm to it. */
11468 if (!enabled_p && idx < 0)
11469 plist = Fplist_put (plist, QCconversion, Qdisabled);
11470
11471 /* Put a `display' text property on the string for the image to
11472 display. Put a `menu-item' property on the string that gives
11473 the start of this item's properties in the tool-bar items
11474 vector. */
11475 image = Fcons (Qimage, plist);
11476 props = list4 (Qdisplay, image,
11477 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11478
11479 /* Let the last image hide all remaining spaces in the tool bar
11480 string. The string can be longer than needed when we reuse a
11481 previous string. */
11482 if (i + 1 == f->n_tool_bar_items)
11483 end = SCHARS (f->desired_tool_bar_string);
11484 else
11485 end = i + 1;
11486 Fadd_text_properties (make_number (i), make_number (end),
11487 props, f->desired_tool_bar_string);
11488 #undef PROP
11489 }
11490
11491 UNGCPRO;
11492 }
11493
11494
11495 /* Display one line of the tool-bar of frame IT->f.
11496
11497 HEIGHT specifies the desired height of the tool-bar line.
11498 If the actual height of the glyph row is less than HEIGHT, the
11499 row's height is increased to HEIGHT, and the icons are centered
11500 vertically in the new height.
11501
11502 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11503 count a final empty row in case the tool-bar width exactly matches
11504 the window width.
11505 */
11506
11507 static void
11508 display_tool_bar_line (struct it *it, int height)
11509 {
11510 struct glyph_row *row = it->glyph_row;
11511 int max_x = it->last_visible_x;
11512 struct glyph *last;
11513
11514 prepare_desired_row (row);
11515 row->y = it->current_y;
11516
11517 /* Note that this isn't made use of if the face hasn't a box,
11518 so there's no need to check the face here. */
11519 it->start_of_box_run_p = 1;
11520
11521 while (it->current_x < max_x)
11522 {
11523 int x, n_glyphs_before, i, nglyphs;
11524 struct it it_before;
11525
11526 /* Get the next display element. */
11527 if (!get_next_display_element (it))
11528 {
11529 /* Don't count empty row if we are counting needed tool-bar lines. */
11530 if (height < 0 && !it->hpos)
11531 return;
11532 break;
11533 }
11534
11535 /* Produce glyphs. */
11536 n_glyphs_before = row->used[TEXT_AREA];
11537 it_before = *it;
11538
11539 PRODUCE_GLYPHS (it);
11540
11541 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11542 i = 0;
11543 x = it_before.current_x;
11544 while (i < nglyphs)
11545 {
11546 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11547
11548 if (x + glyph->pixel_width > max_x)
11549 {
11550 /* Glyph doesn't fit on line. Backtrack. */
11551 row->used[TEXT_AREA] = n_glyphs_before;
11552 *it = it_before;
11553 /* If this is the only glyph on this line, it will never fit on the
11554 tool-bar, so skip it. But ensure there is at least one glyph,
11555 so we don't accidentally disable the tool-bar. */
11556 if (n_glyphs_before == 0
11557 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11558 break;
11559 goto out;
11560 }
11561
11562 ++it->hpos;
11563 x += glyph->pixel_width;
11564 ++i;
11565 }
11566
11567 /* Stop at line end. */
11568 if (ITERATOR_AT_END_OF_LINE_P (it))
11569 break;
11570
11571 set_iterator_to_next (it, 1);
11572 }
11573
11574 out:;
11575
11576 row->displays_text_p = row->used[TEXT_AREA] != 0;
11577
11578 /* Use default face for the border below the tool bar.
11579
11580 FIXME: When auto-resize-tool-bars is grow-only, there is
11581 no additional border below the possibly empty tool-bar lines.
11582 So to make the extra empty lines look "normal", we have to
11583 use the tool-bar face for the border too. */
11584 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11585 it->face_id = DEFAULT_FACE_ID;
11586
11587 extend_face_to_end_of_line (it);
11588 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11589 last->right_box_line_p = 1;
11590 if (last == row->glyphs[TEXT_AREA])
11591 last->left_box_line_p = 1;
11592
11593 /* Make line the desired height and center it vertically. */
11594 if ((height -= it->max_ascent + it->max_descent) > 0)
11595 {
11596 /* Don't add more than one line height. */
11597 height %= FRAME_LINE_HEIGHT (it->f);
11598 it->max_ascent += height / 2;
11599 it->max_descent += (height + 1) / 2;
11600 }
11601
11602 compute_line_metrics (it);
11603
11604 /* If line is empty, make it occupy the rest of the tool-bar. */
11605 if (!row->displays_text_p)
11606 {
11607 row->height = row->phys_height = it->last_visible_y - row->y;
11608 row->visible_height = row->height;
11609 row->ascent = row->phys_ascent = 0;
11610 row->extra_line_spacing = 0;
11611 }
11612
11613 row->full_width_p = 1;
11614 row->continued_p = 0;
11615 row->truncated_on_left_p = 0;
11616 row->truncated_on_right_p = 0;
11617
11618 it->current_x = it->hpos = 0;
11619 it->current_y += row->height;
11620 ++it->vpos;
11621 ++it->glyph_row;
11622 }
11623
11624
11625 /* Max tool-bar height. */
11626
11627 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11628 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11629
11630 /* Value is the number of screen lines needed to make all tool-bar
11631 items of frame F visible. The number of actual rows needed is
11632 returned in *N_ROWS if non-NULL. */
11633
11634 static int
11635 tool_bar_lines_needed (struct frame *f, int *n_rows)
11636 {
11637 struct window *w = XWINDOW (f->tool_bar_window);
11638 struct it it;
11639 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11640 the desired matrix, so use (unused) mode-line row as temporary row to
11641 avoid destroying the first tool-bar row. */
11642 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11643
11644 /* Initialize an iterator for iteration over
11645 F->desired_tool_bar_string in the tool-bar window of frame F. */
11646 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11647 it.first_visible_x = 0;
11648 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11649 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11650 it.paragraph_embedding = L2R;
11651
11652 while (!ITERATOR_AT_END_P (&it))
11653 {
11654 clear_glyph_row (temp_row);
11655 it.glyph_row = temp_row;
11656 display_tool_bar_line (&it, -1);
11657 }
11658 clear_glyph_row (temp_row);
11659
11660 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11661 if (n_rows)
11662 *n_rows = it.vpos > 0 ? it.vpos : -1;
11663
11664 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11665 }
11666
11667
11668 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11669 0, 1, 0,
11670 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11671 (Lisp_Object frame)
11672 {
11673 struct frame *f;
11674 struct window *w;
11675 int nlines = 0;
11676
11677 if (NILP (frame))
11678 frame = selected_frame;
11679 else
11680 CHECK_FRAME (frame);
11681 f = XFRAME (frame);
11682
11683 if (WINDOWP (f->tool_bar_window)
11684 && (w = XWINDOW (f->tool_bar_window),
11685 WINDOW_TOTAL_LINES (w) > 0))
11686 {
11687 update_tool_bar (f, 1);
11688 if (f->n_tool_bar_items)
11689 {
11690 build_desired_tool_bar_string (f);
11691 nlines = tool_bar_lines_needed (f, NULL);
11692 }
11693 }
11694
11695 return make_number (nlines);
11696 }
11697
11698
11699 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11700 height should be changed. */
11701
11702 static int
11703 redisplay_tool_bar (struct frame *f)
11704 {
11705 struct window *w;
11706 struct it it;
11707 struct glyph_row *row;
11708
11709 #if defined (USE_GTK) || defined (HAVE_NS)
11710 if (FRAME_EXTERNAL_TOOL_BAR (f))
11711 update_frame_tool_bar (f);
11712 return 0;
11713 #endif
11714
11715 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11716 do anything. This means you must start with tool-bar-lines
11717 non-zero to get the auto-sizing effect. Or in other words, you
11718 can turn off tool-bars by specifying tool-bar-lines zero. */
11719 if (!WINDOWP (f->tool_bar_window)
11720 || (w = XWINDOW (f->tool_bar_window),
11721 WINDOW_TOTAL_LINES (w) == 0))
11722 return 0;
11723
11724 /* Set up an iterator for the tool-bar window. */
11725 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11726 it.first_visible_x = 0;
11727 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11728 row = it.glyph_row;
11729
11730 /* Build a string that represents the contents of the tool-bar. */
11731 build_desired_tool_bar_string (f);
11732 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11733 /* FIXME: This should be controlled by a user option. But it
11734 doesn't make sense to have an R2L tool bar if the menu bar cannot
11735 be drawn also R2L, and making the menu bar R2L is tricky due
11736 toolkit-specific code that implements it. If an R2L tool bar is
11737 ever supported, display_tool_bar_line should also be augmented to
11738 call unproduce_glyphs like display_line and display_string
11739 do. */
11740 it.paragraph_embedding = L2R;
11741
11742 if (f->n_tool_bar_rows == 0)
11743 {
11744 int nlines;
11745
11746 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11747 nlines != WINDOW_TOTAL_LINES (w)))
11748 {
11749 Lisp_Object frame;
11750 int old_height = WINDOW_TOTAL_LINES (w);
11751
11752 XSETFRAME (frame, f);
11753 Fmodify_frame_parameters (frame,
11754 Fcons (Fcons (Qtool_bar_lines,
11755 make_number (nlines)),
11756 Qnil));
11757 if (WINDOW_TOTAL_LINES (w) != old_height)
11758 {
11759 clear_glyph_matrix (w->desired_matrix);
11760 fonts_changed_p = 1;
11761 return 1;
11762 }
11763 }
11764 }
11765
11766 /* Display as many lines as needed to display all tool-bar items. */
11767
11768 if (f->n_tool_bar_rows > 0)
11769 {
11770 int border, rows, height, extra;
11771
11772 if (INTEGERP (Vtool_bar_border))
11773 border = XINT (Vtool_bar_border);
11774 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11775 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11776 else if (EQ (Vtool_bar_border, Qborder_width))
11777 border = f->border_width;
11778 else
11779 border = 0;
11780 if (border < 0)
11781 border = 0;
11782
11783 rows = f->n_tool_bar_rows;
11784 height = max (1, (it.last_visible_y - border) / rows);
11785 extra = it.last_visible_y - border - height * rows;
11786
11787 while (it.current_y < it.last_visible_y)
11788 {
11789 int h = 0;
11790 if (extra > 0 && rows-- > 0)
11791 {
11792 h = (extra + rows - 1) / rows;
11793 extra -= h;
11794 }
11795 display_tool_bar_line (&it, height + h);
11796 }
11797 }
11798 else
11799 {
11800 while (it.current_y < it.last_visible_y)
11801 display_tool_bar_line (&it, 0);
11802 }
11803
11804 /* It doesn't make much sense to try scrolling in the tool-bar
11805 window, so don't do it. */
11806 w->desired_matrix->no_scrolling_p = 1;
11807 w->must_be_updated_p = 1;
11808
11809 if (!NILP (Vauto_resize_tool_bars))
11810 {
11811 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11812 int change_height_p = 0;
11813
11814 /* If we couldn't display everything, change the tool-bar's
11815 height if there is room for more. */
11816 if (IT_STRING_CHARPOS (it) < it.end_charpos
11817 && it.current_y < max_tool_bar_height)
11818 change_height_p = 1;
11819
11820 row = it.glyph_row - 1;
11821
11822 /* If there are blank lines at the end, except for a partially
11823 visible blank line at the end that is smaller than
11824 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11825 if (!row->displays_text_p
11826 && row->height >= FRAME_LINE_HEIGHT (f))
11827 change_height_p = 1;
11828
11829 /* If row displays tool-bar items, but is partially visible,
11830 change the tool-bar's height. */
11831 if (row->displays_text_p
11832 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11833 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11834 change_height_p = 1;
11835
11836 /* Resize windows as needed by changing the `tool-bar-lines'
11837 frame parameter. */
11838 if (change_height_p)
11839 {
11840 Lisp_Object frame;
11841 int old_height = WINDOW_TOTAL_LINES (w);
11842 int nrows;
11843 int nlines = tool_bar_lines_needed (f, &nrows);
11844
11845 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11846 && !f->minimize_tool_bar_window_p)
11847 ? (nlines > old_height)
11848 : (nlines != old_height));
11849 f->minimize_tool_bar_window_p = 0;
11850
11851 if (change_height_p)
11852 {
11853 XSETFRAME (frame, f);
11854 Fmodify_frame_parameters (frame,
11855 Fcons (Fcons (Qtool_bar_lines,
11856 make_number (nlines)),
11857 Qnil));
11858 if (WINDOW_TOTAL_LINES (w) != old_height)
11859 {
11860 clear_glyph_matrix (w->desired_matrix);
11861 f->n_tool_bar_rows = nrows;
11862 fonts_changed_p = 1;
11863 return 1;
11864 }
11865 }
11866 }
11867 }
11868
11869 f->minimize_tool_bar_window_p = 0;
11870 return 0;
11871 }
11872
11873
11874 /* Get information about the tool-bar item which is displayed in GLYPH
11875 on frame F. Return in *PROP_IDX the index where tool-bar item
11876 properties start in F->tool_bar_items. Value is zero if
11877 GLYPH doesn't display a tool-bar item. */
11878
11879 static int
11880 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11881 {
11882 Lisp_Object prop;
11883 int success_p;
11884 int charpos;
11885
11886 /* This function can be called asynchronously, which means we must
11887 exclude any possibility that Fget_text_property signals an
11888 error. */
11889 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11890 charpos = max (0, charpos);
11891
11892 /* Get the text property `menu-item' at pos. The value of that
11893 property is the start index of this item's properties in
11894 F->tool_bar_items. */
11895 prop = Fget_text_property (make_number (charpos),
11896 Qmenu_item, f->current_tool_bar_string);
11897 if (INTEGERP (prop))
11898 {
11899 *prop_idx = XINT (prop);
11900 success_p = 1;
11901 }
11902 else
11903 success_p = 0;
11904
11905 return success_p;
11906 }
11907
11908 \f
11909 /* Get information about the tool-bar item at position X/Y on frame F.
11910 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11911 the current matrix of the tool-bar window of F, or NULL if not
11912 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11913 item in F->tool_bar_items. Value is
11914
11915 -1 if X/Y is not on a tool-bar item
11916 0 if X/Y is on the same item that was highlighted before.
11917 1 otherwise. */
11918
11919 static int
11920 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11921 int *hpos, int *vpos, int *prop_idx)
11922 {
11923 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11924 struct window *w = XWINDOW (f->tool_bar_window);
11925 int area;
11926
11927 /* Find the glyph under X/Y. */
11928 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11929 if (*glyph == NULL)
11930 return -1;
11931
11932 /* Get the start of this tool-bar item's properties in
11933 f->tool_bar_items. */
11934 if (!tool_bar_item_info (f, *glyph, prop_idx))
11935 return -1;
11936
11937 /* Is mouse on the highlighted item? */
11938 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11939 && *vpos >= hlinfo->mouse_face_beg_row
11940 && *vpos <= hlinfo->mouse_face_end_row
11941 && (*vpos > hlinfo->mouse_face_beg_row
11942 || *hpos >= hlinfo->mouse_face_beg_col)
11943 && (*vpos < hlinfo->mouse_face_end_row
11944 || *hpos < hlinfo->mouse_face_end_col
11945 || hlinfo->mouse_face_past_end))
11946 return 0;
11947
11948 return 1;
11949 }
11950
11951
11952 /* EXPORT:
11953 Handle mouse button event on the tool-bar of frame F, at
11954 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11955 0 for button release. MODIFIERS is event modifiers for button
11956 release. */
11957
11958 void
11959 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11960 unsigned int modifiers)
11961 {
11962 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11963 struct window *w = XWINDOW (f->tool_bar_window);
11964 int hpos, vpos, prop_idx;
11965 struct glyph *glyph;
11966 Lisp_Object enabled_p;
11967
11968 /* If not on the highlighted tool-bar item, return. */
11969 frame_to_window_pixel_xy (w, &x, &y);
11970 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11971 return;
11972
11973 /* If item is disabled, do nothing. */
11974 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11975 if (NILP (enabled_p))
11976 return;
11977
11978 if (down_p)
11979 {
11980 /* Show item in pressed state. */
11981 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11982 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11983 last_tool_bar_item = prop_idx;
11984 }
11985 else
11986 {
11987 Lisp_Object key, frame;
11988 struct input_event event;
11989 EVENT_INIT (event);
11990
11991 /* Show item in released state. */
11992 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11993 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11994
11995 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11996
11997 XSETFRAME (frame, f);
11998 event.kind = TOOL_BAR_EVENT;
11999 event.frame_or_window = frame;
12000 event.arg = frame;
12001 kbd_buffer_store_event (&event);
12002
12003 event.kind = TOOL_BAR_EVENT;
12004 event.frame_or_window = frame;
12005 event.arg = key;
12006 event.modifiers = modifiers;
12007 kbd_buffer_store_event (&event);
12008 last_tool_bar_item = -1;
12009 }
12010 }
12011
12012
12013 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12014 tool-bar window-relative coordinates X/Y. Called from
12015 note_mouse_highlight. */
12016
12017 static void
12018 note_tool_bar_highlight (struct frame *f, int x, int y)
12019 {
12020 Lisp_Object window = f->tool_bar_window;
12021 struct window *w = XWINDOW (window);
12022 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12023 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12024 int hpos, vpos;
12025 struct glyph *glyph;
12026 struct glyph_row *row;
12027 int i;
12028 Lisp_Object enabled_p;
12029 int prop_idx;
12030 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12031 int mouse_down_p, rc;
12032
12033 /* Function note_mouse_highlight is called with negative X/Y
12034 values when mouse moves outside of the frame. */
12035 if (x <= 0 || y <= 0)
12036 {
12037 clear_mouse_face (hlinfo);
12038 return;
12039 }
12040
12041 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12042 if (rc < 0)
12043 {
12044 /* Not on tool-bar item. */
12045 clear_mouse_face (hlinfo);
12046 return;
12047 }
12048 else if (rc == 0)
12049 /* On same tool-bar item as before. */
12050 goto set_help_echo;
12051
12052 clear_mouse_face (hlinfo);
12053
12054 /* Mouse is down, but on different tool-bar item? */
12055 mouse_down_p = (dpyinfo->grabbed
12056 && f == last_mouse_frame
12057 && FRAME_LIVE_P (f));
12058 if (mouse_down_p
12059 && last_tool_bar_item != prop_idx)
12060 return;
12061
12062 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12063 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12064
12065 /* If tool-bar item is not enabled, don't highlight it. */
12066 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12067 if (!NILP (enabled_p))
12068 {
12069 /* Compute the x-position of the glyph. In front and past the
12070 image is a space. We include this in the highlighted area. */
12071 row = MATRIX_ROW (w->current_matrix, vpos);
12072 for (i = x = 0; i < hpos; ++i)
12073 x += row->glyphs[TEXT_AREA][i].pixel_width;
12074
12075 /* Record this as the current active region. */
12076 hlinfo->mouse_face_beg_col = hpos;
12077 hlinfo->mouse_face_beg_row = vpos;
12078 hlinfo->mouse_face_beg_x = x;
12079 hlinfo->mouse_face_beg_y = row->y;
12080 hlinfo->mouse_face_past_end = 0;
12081
12082 hlinfo->mouse_face_end_col = hpos + 1;
12083 hlinfo->mouse_face_end_row = vpos;
12084 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12085 hlinfo->mouse_face_end_y = row->y;
12086 hlinfo->mouse_face_window = window;
12087 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12088
12089 /* Display it as active. */
12090 show_mouse_face (hlinfo, draw);
12091 hlinfo->mouse_face_image_state = draw;
12092 }
12093
12094 set_help_echo:
12095
12096 /* Set help_echo_string to a help string to display for this tool-bar item.
12097 XTread_socket does the rest. */
12098 help_echo_object = help_echo_window = Qnil;
12099 help_echo_pos = -1;
12100 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12101 if (NILP (help_echo_string))
12102 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12103 }
12104
12105 #endif /* HAVE_WINDOW_SYSTEM */
12106
12107
12108 \f
12109 /************************************************************************
12110 Horizontal scrolling
12111 ************************************************************************/
12112
12113 static int hscroll_window_tree (Lisp_Object);
12114 static int hscroll_windows (Lisp_Object);
12115
12116 /* For all leaf windows in the window tree rooted at WINDOW, set their
12117 hscroll value so that PT is (i) visible in the window, and (ii) so
12118 that it is not within a certain margin at the window's left and
12119 right border. Value is non-zero if any window's hscroll has been
12120 changed. */
12121
12122 static int
12123 hscroll_window_tree (Lisp_Object window)
12124 {
12125 int hscrolled_p = 0;
12126 int hscroll_relative_p = FLOATP (Vhscroll_step);
12127 int hscroll_step_abs = 0;
12128 double hscroll_step_rel = 0;
12129
12130 if (hscroll_relative_p)
12131 {
12132 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12133 if (hscroll_step_rel < 0)
12134 {
12135 hscroll_relative_p = 0;
12136 hscroll_step_abs = 0;
12137 }
12138 }
12139 else if (INTEGERP (Vhscroll_step))
12140 {
12141 hscroll_step_abs = XINT (Vhscroll_step);
12142 if (hscroll_step_abs < 0)
12143 hscroll_step_abs = 0;
12144 }
12145 else
12146 hscroll_step_abs = 0;
12147
12148 while (WINDOWP (window))
12149 {
12150 struct window *w = XWINDOW (window);
12151
12152 if (WINDOWP (w->hchild))
12153 hscrolled_p |= hscroll_window_tree (w->hchild);
12154 else if (WINDOWP (w->vchild))
12155 hscrolled_p |= hscroll_window_tree (w->vchild);
12156 else if (w->cursor.vpos >= 0)
12157 {
12158 int h_margin;
12159 int text_area_width;
12160 struct glyph_row *current_cursor_row
12161 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12162 struct glyph_row *desired_cursor_row
12163 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12164 struct glyph_row *cursor_row
12165 = (desired_cursor_row->enabled_p
12166 ? desired_cursor_row
12167 : current_cursor_row);
12168 int row_r2l_p = cursor_row->reversed_p;
12169
12170 text_area_width = window_box_width (w, TEXT_AREA);
12171
12172 /* Scroll when cursor is inside this scroll margin. */
12173 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12174
12175 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12176 /* For left-to-right rows, hscroll when cursor is either
12177 (i) inside the right hscroll margin, or (ii) if it is
12178 inside the left margin and the window is already
12179 hscrolled. */
12180 && ((!row_r2l_p
12181 && ((XFASTINT (w->hscroll)
12182 && w->cursor.x <= h_margin)
12183 || (cursor_row->enabled_p
12184 && cursor_row->truncated_on_right_p
12185 && (w->cursor.x >= text_area_width - h_margin))))
12186 /* For right-to-left rows, the logic is similar,
12187 except that rules for scrolling to left and right
12188 are reversed. E.g., if cursor.x <= h_margin, we
12189 need to hscroll "to the right" unconditionally,
12190 and that will scroll the screen to the left so as
12191 to reveal the next portion of the row. */
12192 || (row_r2l_p
12193 && ((cursor_row->enabled_p
12194 /* FIXME: It is confusing to set the
12195 truncated_on_right_p flag when R2L rows
12196 are actually truncated on the left. */
12197 && cursor_row->truncated_on_right_p
12198 && w->cursor.x <= h_margin)
12199 || (XFASTINT (w->hscroll)
12200 && (w->cursor.x >= text_area_width - h_margin))))))
12201 {
12202 struct it it;
12203 int hscroll;
12204 struct buffer *saved_current_buffer;
12205 EMACS_INT pt;
12206 int wanted_x;
12207
12208 /* Find point in a display of infinite width. */
12209 saved_current_buffer = current_buffer;
12210 current_buffer = XBUFFER (w->buffer);
12211
12212 if (w == XWINDOW (selected_window))
12213 pt = PT;
12214 else
12215 {
12216 pt = marker_position (w->pointm);
12217 pt = max (BEGV, pt);
12218 pt = min (ZV, pt);
12219 }
12220
12221 /* Move iterator to pt starting at cursor_row->start in
12222 a line with infinite width. */
12223 init_to_row_start (&it, w, cursor_row);
12224 it.last_visible_x = INFINITY;
12225 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12226 current_buffer = saved_current_buffer;
12227
12228 /* Position cursor in window. */
12229 if (!hscroll_relative_p && hscroll_step_abs == 0)
12230 hscroll = max (0, (it.current_x
12231 - (ITERATOR_AT_END_OF_LINE_P (&it)
12232 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12233 : (text_area_width / 2))))
12234 / FRAME_COLUMN_WIDTH (it.f);
12235 else if ((!row_r2l_p
12236 && w->cursor.x >= text_area_width - h_margin)
12237 || (row_r2l_p && w->cursor.x <= h_margin))
12238 {
12239 if (hscroll_relative_p)
12240 wanted_x = text_area_width * (1 - hscroll_step_rel)
12241 - h_margin;
12242 else
12243 wanted_x = text_area_width
12244 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12245 - h_margin;
12246 hscroll
12247 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12248 }
12249 else
12250 {
12251 if (hscroll_relative_p)
12252 wanted_x = text_area_width * hscroll_step_rel
12253 + h_margin;
12254 else
12255 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12256 + h_margin;
12257 hscroll
12258 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12259 }
12260 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12261
12262 /* Don't prevent redisplay optimizations if hscroll
12263 hasn't changed, as it will unnecessarily slow down
12264 redisplay. */
12265 if (XFASTINT (w->hscroll) != hscroll)
12266 {
12267 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12268 w->hscroll = make_number (hscroll);
12269 hscrolled_p = 1;
12270 }
12271 }
12272 }
12273
12274 window = w->next;
12275 }
12276
12277 /* Value is non-zero if hscroll of any leaf window has been changed. */
12278 return hscrolled_p;
12279 }
12280
12281
12282 /* Set hscroll so that cursor is visible and not inside horizontal
12283 scroll margins for all windows in the tree rooted at WINDOW. See
12284 also hscroll_window_tree above. Value is non-zero if any window's
12285 hscroll has been changed. If it has, desired matrices on the frame
12286 of WINDOW are cleared. */
12287
12288 static int
12289 hscroll_windows (Lisp_Object window)
12290 {
12291 int hscrolled_p = hscroll_window_tree (window);
12292 if (hscrolled_p)
12293 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12294 return hscrolled_p;
12295 }
12296
12297
12298 \f
12299 /************************************************************************
12300 Redisplay
12301 ************************************************************************/
12302
12303 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12304 to a non-zero value. This is sometimes handy to have in a debugger
12305 session. */
12306
12307 #if GLYPH_DEBUG
12308
12309 /* First and last unchanged row for try_window_id. */
12310
12311 static int debug_first_unchanged_at_end_vpos;
12312 static int debug_last_unchanged_at_beg_vpos;
12313
12314 /* Delta vpos and y. */
12315
12316 static int debug_dvpos, debug_dy;
12317
12318 /* Delta in characters and bytes for try_window_id. */
12319
12320 static EMACS_INT debug_delta, debug_delta_bytes;
12321
12322 /* Values of window_end_pos and window_end_vpos at the end of
12323 try_window_id. */
12324
12325 static EMACS_INT debug_end_vpos;
12326
12327 /* Append a string to W->desired_matrix->method. FMT is a printf
12328 format string. If trace_redisplay_p is non-zero also printf the
12329 resulting string to stderr. */
12330
12331 static void debug_method_add (struct window *, char const *, ...)
12332 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12333
12334 static void
12335 debug_method_add (struct window *w, char const *fmt, ...)
12336 {
12337 char buffer[512];
12338 char *method = w->desired_matrix->method;
12339 int len = strlen (method);
12340 int size = sizeof w->desired_matrix->method;
12341 int remaining = size - len - 1;
12342 va_list ap;
12343
12344 va_start (ap, fmt);
12345 vsprintf (buffer, fmt, ap);
12346 va_end (ap);
12347 if (len && remaining)
12348 {
12349 method[len] = '|';
12350 --remaining, ++len;
12351 }
12352
12353 strncpy (method + len, buffer, remaining);
12354
12355 if (trace_redisplay_p)
12356 fprintf (stderr, "%p (%s): %s\n",
12357 w,
12358 ((BUFFERP (w->buffer)
12359 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12360 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12361 : "no buffer"),
12362 buffer);
12363 }
12364
12365 #endif /* GLYPH_DEBUG */
12366
12367
12368 /* Value is non-zero if all changes in window W, which displays
12369 current_buffer, are in the text between START and END. START is a
12370 buffer position, END is given as a distance from Z. Used in
12371 redisplay_internal for display optimization. */
12372
12373 static inline int
12374 text_outside_line_unchanged_p (struct window *w,
12375 EMACS_INT start, EMACS_INT end)
12376 {
12377 int unchanged_p = 1;
12378
12379 /* If text or overlays have changed, see where. */
12380 if (XFASTINT (w->last_modified) < MODIFF
12381 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12382 {
12383 /* Gap in the line? */
12384 if (GPT < start || Z - GPT < end)
12385 unchanged_p = 0;
12386
12387 /* Changes start in front of the line, or end after it? */
12388 if (unchanged_p
12389 && (BEG_UNCHANGED < start - 1
12390 || END_UNCHANGED < end))
12391 unchanged_p = 0;
12392
12393 /* If selective display, can't optimize if changes start at the
12394 beginning of the line. */
12395 if (unchanged_p
12396 && INTEGERP (BVAR (current_buffer, selective_display))
12397 && XINT (BVAR (current_buffer, selective_display)) > 0
12398 && (BEG_UNCHANGED < start || GPT <= start))
12399 unchanged_p = 0;
12400
12401 /* If there are overlays at the start or end of the line, these
12402 may have overlay strings with newlines in them. A change at
12403 START, for instance, may actually concern the display of such
12404 overlay strings as well, and they are displayed on different
12405 lines. So, quickly rule out this case. (For the future, it
12406 might be desirable to implement something more telling than
12407 just BEG/END_UNCHANGED.) */
12408 if (unchanged_p)
12409 {
12410 if (BEG + BEG_UNCHANGED == start
12411 && overlay_touches_p (start))
12412 unchanged_p = 0;
12413 if (END_UNCHANGED == end
12414 && overlay_touches_p (Z - end))
12415 unchanged_p = 0;
12416 }
12417
12418 /* Under bidi reordering, adding or deleting a character in the
12419 beginning of a paragraph, before the first strong directional
12420 character, can change the base direction of the paragraph (unless
12421 the buffer specifies a fixed paragraph direction), which will
12422 require to redisplay the whole paragraph. It might be worthwhile
12423 to find the paragraph limits and widen the range of redisplayed
12424 lines to that, but for now just give up this optimization. */
12425 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12426 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12427 unchanged_p = 0;
12428 }
12429
12430 return unchanged_p;
12431 }
12432
12433
12434 /* Do a frame update, taking possible shortcuts into account. This is
12435 the main external entry point for redisplay.
12436
12437 If the last redisplay displayed an echo area message and that message
12438 is no longer requested, we clear the echo area or bring back the
12439 mini-buffer if that is in use. */
12440
12441 void
12442 redisplay (void)
12443 {
12444 redisplay_internal ();
12445 }
12446
12447
12448 static Lisp_Object
12449 overlay_arrow_string_or_property (Lisp_Object var)
12450 {
12451 Lisp_Object val;
12452
12453 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12454 return val;
12455
12456 return Voverlay_arrow_string;
12457 }
12458
12459 /* Return 1 if there are any overlay-arrows in current_buffer. */
12460 static int
12461 overlay_arrow_in_current_buffer_p (void)
12462 {
12463 Lisp_Object vlist;
12464
12465 for (vlist = Voverlay_arrow_variable_list;
12466 CONSP (vlist);
12467 vlist = XCDR (vlist))
12468 {
12469 Lisp_Object var = XCAR (vlist);
12470 Lisp_Object val;
12471
12472 if (!SYMBOLP (var))
12473 continue;
12474 val = find_symbol_value (var);
12475 if (MARKERP (val)
12476 && current_buffer == XMARKER (val)->buffer)
12477 return 1;
12478 }
12479 return 0;
12480 }
12481
12482
12483 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12484 has changed. */
12485
12486 static int
12487 overlay_arrows_changed_p (void)
12488 {
12489 Lisp_Object vlist;
12490
12491 for (vlist = Voverlay_arrow_variable_list;
12492 CONSP (vlist);
12493 vlist = XCDR (vlist))
12494 {
12495 Lisp_Object var = XCAR (vlist);
12496 Lisp_Object val, pstr;
12497
12498 if (!SYMBOLP (var))
12499 continue;
12500 val = find_symbol_value (var);
12501 if (!MARKERP (val))
12502 continue;
12503 if (! EQ (COERCE_MARKER (val),
12504 Fget (var, Qlast_arrow_position))
12505 || ! (pstr = overlay_arrow_string_or_property (var),
12506 EQ (pstr, Fget (var, Qlast_arrow_string))))
12507 return 1;
12508 }
12509 return 0;
12510 }
12511
12512 /* Mark overlay arrows to be updated on next redisplay. */
12513
12514 static void
12515 update_overlay_arrows (int up_to_date)
12516 {
12517 Lisp_Object vlist;
12518
12519 for (vlist = Voverlay_arrow_variable_list;
12520 CONSP (vlist);
12521 vlist = XCDR (vlist))
12522 {
12523 Lisp_Object var = XCAR (vlist);
12524
12525 if (!SYMBOLP (var))
12526 continue;
12527
12528 if (up_to_date > 0)
12529 {
12530 Lisp_Object val = find_symbol_value (var);
12531 Fput (var, Qlast_arrow_position,
12532 COERCE_MARKER (val));
12533 Fput (var, Qlast_arrow_string,
12534 overlay_arrow_string_or_property (var));
12535 }
12536 else if (up_to_date < 0
12537 || !NILP (Fget (var, Qlast_arrow_position)))
12538 {
12539 Fput (var, Qlast_arrow_position, Qt);
12540 Fput (var, Qlast_arrow_string, Qt);
12541 }
12542 }
12543 }
12544
12545
12546 /* Return overlay arrow string to display at row.
12547 Return integer (bitmap number) for arrow bitmap in left fringe.
12548 Return nil if no overlay arrow. */
12549
12550 static Lisp_Object
12551 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12552 {
12553 Lisp_Object vlist;
12554
12555 for (vlist = Voverlay_arrow_variable_list;
12556 CONSP (vlist);
12557 vlist = XCDR (vlist))
12558 {
12559 Lisp_Object var = XCAR (vlist);
12560 Lisp_Object val;
12561
12562 if (!SYMBOLP (var))
12563 continue;
12564
12565 val = find_symbol_value (var);
12566
12567 if (MARKERP (val)
12568 && current_buffer == XMARKER (val)->buffer
12569 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12570 {
12571 if (FRAME_WINDOW_P (it->f)
12572 /* FIXME: if ROW->reversed_p is set, this should test
12573 the right fringe, not the left one. */
12574 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12575 {
12576 #ifdef HAVE_WINDOW_SYSTEM
12577 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12578 {
12579 int fringe_bitmap;
12580 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12581 return make_number (fringe_bitmap);
12582 }
12583 #endif
12584 return make_number (-1); /* Use default arrow bitmap */
12585 }
12586 return overlay_arrow_string_or_property (var);
12587 }
12588 }
12589
12590 return Qnil;
12591 }
12592
12593 /* Return 1 if point moved out of or into a composition. Otherwise
12594 return 0. PREV_BUF and PREV_PT are the last point buffer and
12595 position. BUF and PT are the current point buffer and position. */
12596
12597 static int
12598 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12599 struct buffer *buf, EMACS_INT pt)
12600 {
12601 EMACS_INT start, end;
12602 Lisp_Object prop;
12603 Lisp_Object buffer;
12604
12605 XSETBUFFER (buffer, buf);
12606 /* Check a composition at the last point if point moved within the
12607 same buffer. */
12608 if (prev_buf == buf)
12609 {
12610 if (prev_pt == pt)
12611 /* Point didn't move. */
12612 return 0;
12613
12614 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12615 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12616 && COMPOSITION_VALID_P (start, end, prop)
12617 && start < prev_pt && end > prev_pt)
12618 /* The last point was within the composition. Return 1 iff
12619 point moved out of the composition. */
12620 return (pt <= start || pt >= end);
12621 }
12622
12623 /* Check a composition at the current point. */
12624 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12625 && find_composition (pt, -1, &start, &end, &prop, buffer)
12626 && COMPOSITION_VALID_P (start, end, prop)
12627 && start < pt && end > pt);
12628 }
12629
12630
12631 /* Reconsider the setting of B->clip_changed which is displayed
12632 in window W. */
12633
12634 static inline void
12635 reconsider_clip_changes (struct window *w, struct buffer *b)
12636 {
12637 if (b->clip_changed
12638 && !NILP (w->window_end_valid)
12639 && w->current_matrix->buffer == b
12640 && w->current_matrix->zv == BUF_ZV (b)
12641 && w->current_matrix->begv == BUF_BEGV (b))
12642 b->clip_changed = 0;
12643
12644 /* If display wasn't paused, and W is not a tool bar window, see if
12645 point has been moved into or out of a composition. In that case,
12646 we set b->clip_changed to 1 to force updating the screen. If
12647 b->clip_changed has already been set to 1, we can skip this
12648 check. */
12649 if (!b->clip_changed
12650 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12651 {
12652 EMACS_INT pt;
12653
12654 if (w == XWINDOW (selected_window))
12655 pt = PT;
12656 else
12657 pt = marker_position (w->pointm);
12658
12659 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12660 || pt != XINT (w->last_point))
12661 && check_point_in_composition (w->current_matrix->buffer,
12662 XINT (w->last_point),
12663 XBUFFER (w->buffer), pt))
12664 b->clip_changed = 1;
12665 }
12666 }
12667 \f
12668
12669 /* Select FRAME to forward the values of frame-local variables into C
12670 variables so that the redisplay routines can access those values
12671 directly. */
12672
12673 static void
12674 select_frame_for_redisplay (Lisp_Object frame)
12675 {
12676 Lisp_Object tail, tem;
12677 Lisp_Object old = selected_frame;
12678 struct Lisp_Symbol *sym;
12679
12680 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12681
12682 selected_frame = frame;
12683
12684 do {
12685 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12686 if (CONSP (XCAR (tail))
12687 && (tem = XCAR (XCAR (tail)),
12688 SYMBOLP (tem))
12689 && (sym = indirect_variable (XSYMBOL (tem)),
12690 sym->redirect == SYMBOL_LOCALIZED)
12691 && sym->val.blv->frame_local)
12692 /* Use find_symbol_value rather than Fsymbol_value
12693 to avoid an error if it is void. */
12694 find_symbol_value (tem);
12695 } while (!EQ (frame, old) && (frame = old, 1));
12696 }
12697
12698
12699 #define STOP_POLLING \
12700 do { if (! polling_stopped_here) stop_polling (); \
12701 polling_stopped_here = 1; } while (0)
12702
12703 #define RESUME_POLLING \
12704 do { if (polling_stopped_here) start_polling (); \
12705 polling_stopped_here = 0; } while (0)
12706
12707
12708 /* Perhaps in the future avoid recentering windows if it
12709 is not necessary; currently that causes some problems. */
12710
12711 static void
12712 redisplay_internal (void)
12713 {
12714 struct window *w = XWINDOW (selected_window);
12715 struct window *sw;
12716 struct frame *fr;
12717 int pending;
12718 int must_finish = 0;
12719 struct text_pos tlbufpos, tlendpos;
12720 int number_of_visible_frames;
12721 int count, count1;
12722 struct frame *sf;
12723 int polling_stopped_here = 0;
12724 Lisp_Object old_frame = selected_frame;
12725
12726 /* Non-zero means redisplay has to consider all windows on all
12727 frames. Zero means, only selected_window is considered. */
12728 int consider_all_windows_p;
12729
12730 /* Non-zero means redisplay has to redisplay the miniwindow */
12731 int update_miniwindow_p = 0;
12732
12733 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12734
12735 /* No redisplay if running in batch mode or frame is not yet fully
12736 initialized, or redisplay is explicitly turned off by setting
12737 Vinhibit_redisplay. */
12738 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12739 || !NILP (Vinhibit_redisplay))
12740 return;
12741
12742 /* Don't examine these until after testing Vinhibit_redisplay.
12743 When Emacs is shutting down, perhaps because its connection to
12744 X has dropped, we should not look at them at all. */
12745 fr = XFRAME (w->frame);
12746 sf = SELECTED_FRAME ();
12747
12748 if (!fr->glyphs_initialized_p)
12749 return;
12750
12751 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12752 if (popup_activated ())
12753 return;
12754 #endif
12755
12756 /* I don't think this happens but let's be paranoid. */
12757 if (redisplaying_p)
12758 return;
12759
12760 /* Record a function that resets redisplaying_p to its old value
12761 when we leave this function. */
12762 count = SPECPDL_INDEX ();
12763 record_unwind_protect (unwind_redisplay,
12764 Fcons (make_number (redisplaying_p), selected_frame));
12765 ++redisplaying_p;
12766 specbind (Qinhibit_free_realized_faces, Qnil);
12767
12768 {
12769 Lisp_Object tail, frame;
12770
12771 FOR_EACH_FRAME (tail, frame)
12772 {
12773 struct frame *f = XFRAME (frame);
12774 f->already_hscrolled_p = 0;
12775 }
12776 }
12777
12778 retry:
12779 /* Remember the currently selected window. */
12780 sw = w;
12781
12782 if (!EQ (old_frame, selected_frame)
12783 && FRAME_LIVE_P (XFRAME (old_frame)))
12784 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12785 selected_frame and selected_window to be temporarily out-of-sync so
12786 when we come back here via `goto retry', we need to resync because we
12787 may need to run Elisp code (via prepare_menu_bars). */
12788 select_frame_for_redisplay (old_frame);
12789
12790 pending = 0;
12791 reconsider_clip_changes (w, current_buffer);
12792 last_escape_glyph_frame = NULL;
12793 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12794 last_glyphless_glyph_frame = NULL;
12795 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12796
12797 /* If new fonts have been loaded that make a glyph matrix adjustment
12798 necessary, do it. */
12799 if (fonts_changed_p)
12800 {
12801 adjust_glyphs (NULL);
12802 ++windows_or_buffers_changed;
12803 fonts_changed_p = 0;
12804 }
12805
12806 /* If face_change_count is non-zero, init_iterator will free all
12807 realized faces, which includes the faces referenced from current
12808 matrices. So, we can't reuse current matrices in this case. */
12809 if (face_change_count)
12810 ++windows_or_buffers_changed;
12811
12812 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12813 && FRAME_TTY (sf)->previous_frame != sf)
12814 {
12815 /* Since frames on a single ASCII terminal share the same
12816 display area, displaying a different frame means redisplay
12817 the whole thing. */
12818 windows_or_buffers_changed++;
12819 SET_FRAME_GARBAGED (sf);
12820 #ifndef DOS_NT
12821 set_tty_color_mode (FRAME_TTY (sf), sf);
12822 #endif
12823 FRAME_TTY (sf)->previous_frame = sf;
12824 }
12825
12826 /* Set the visible flags for all frames. Do this before checking
12827 for resized or garbaged frames; they want to know if their frames
12828 are visible. See the comment in frame.h for
12829 FRAME_SAMPLE_VISIBILITY. */
12830 {
12831 Lisp_Object tail, frame;
12832
12833 number_of_visible_frames = 0;
12834
12835 FOR_EACH_FRAME (tail, frame)
12836 {
12837 struct frame *f = XFRAME (frame);
12838
12839 FRAME_SAMPLE_VISIBILITY (f);
12840 if (FRAME_VISIBLE_P (f))
12841 ++number_of_visible_frames;
12842 clear_desired_matrices (f);
12843 }
12844 }
12845
12846 /* Notice any pending interrupt request to change frame size. */
12847 do_pending_window_change (1);
12848
12849 /* do_pending_window_change could change the selected_window due to
12850 frame resizing which makes the selected window too small. */
12851 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12852 {
12853 sw = w;
12854 reconsider_clip_changes (w, current_buffer);
12855 }
12856
12857 /* Clear frames marked as garbaged. */
12858 if (frame_garbaged)
12859 clear_garbaged_frames ();
12860
12861 /* Build menubar and tool-bar items. */
12862 if (NILP (Vmemory_full))
12863 prepare_menu_bars ();
12864
12865 if (windows_or_buffers_changed)
12866 update_mode_lines++;
12867
12868 /* Detect case that we need to write or remove a star in the mode line. */
12869 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12870 {
12871 w->update_mode_line = Qt;
12872 if (buffer_shared > 1)
12873 update_mode_lines++;
12874 }
12875
12876 /* Avoid invocation of point motion hooks by `current_column' below. */
12877 count1 = SPECPDL_INDEX ();
12878 specbind (Qinhibit_point_motion_hooks, Qt);
12879
12880 /* If %c is in the mode line, update it if needed. */
12881 if (!NILP (w->column_number_displayed)
12882 /* This alternative quickly identifies a common case
12883 where no change is needed. */
12884 && !(PT == XFASTINT (w->last_point)
12885 && XFASTINT (w->last_modified) >= MODIFF
12886 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12887 && (XFASTINT (w->column_number_displayed) != current_column ()))
12888 w->update_mode_line = Qt;
12889
12890 unbind_to (count1, Qnil);
12891
12892 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12893
12894 /* The variable buffer_shared is set in redisplay_window and
12895 indicates that we redisplay a buffer in different windows. See
12896 there. */
12897 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12898 || cursor_type_changed);
12899
12900 /* If specs for an arrow have changed, do thorough redisplay
12901 to ensure we remove any arrow that should no longer exist. */
12902 if (overlay_arrows_changed_p ())
12903 consider_all_windows_p = windows_or_buffers_changed = 1;
12904
12905 /* Normally the message* functions will have already displayed and
12906 updated the echo area, but the frame may have been trashed, or
12907 the update may have been preempted, so display the echo area
12908 again here. Checking message_cleared_p captures the case that
12909 the echo area should be cleared. */
12910 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12911 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12912 || (message_cleared_p
12913 && minibuf_level == 0
12914 /* If the mini-window is currently selected, this means the
12915 echo-area doesn't show through. */
12916 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12917 {
12918 int window_height_changed_p = echo_area_display (0);
12919
12920 if (message_cleared_p)
12921 update_miniwindow_p = 1;
12922
12923 must_finish = 1;
12924
12925 /* If we don't display the current message, don't clear the
12926 message_cleared_p flag, because, if we did, we wouldn't clear
12927 the echo area in the next redisplay which doesn't preserve
12928 the echo area. */
12929 if (!display_last_displayed_message_p)
12930 message_cleared_p = 0;
12931
12932 if (fonts_changed_p)
12933 goto retry;
12934 else if (window_height_changed_p)
12935 {
12936 consider_all_windows_p = 1;
12937 ++update_mode_lines;
12938 ++windows_or_buffers_changed;
12939
12940 /* If window configuration was changed, frames may have been
12941 marked garbaged. Clear them or we will experience
12942 surprises wrt scrolling. */
12943 if (frame_garbaged)
12944 clear_garbaged_frames ();
12945 }
12946 }
12947 else if (EQ (selected_window, minibuf_window)
12948 && (current_buffer->clip_changed
12949 || XFASTINT (w->last_modified) < MODIFF
12950 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12951 && resize_mini_window (w, 0))
12952 {
12953 /* Resized active mini-window to fit the size of what it is
12954 showing if its contents might have changed. */
12955 must_finish = 1;
12956 /* FIXME: this causes all frames to be updated, which seems unnecessary
12957 since only the current frame needs to be considered. This function needs
12958 to be rewritten with two variables, consider_all_windows and
12959 consider_all_frames. */
12960 consider_all_windows_p = 1;
12961 ++windows_or_buffers_changed;
12962 ++update_mode_lines;
12963
12964 /* If window configuration was changed, frames may have been
12965 marked garbaged. Clear them or we will experience
12966 surprises wrt scrolling. */
12967 if (frame_garbaged)
12968 clear_garbaged_frames ();
12969 }
12970
12971
12972 /* If showing the region, and mark has changed, we must redisplay
12973 the whole window. The assignment to this_line_start_pos prevents
12974 the optimization directly below this if-statement. */
12975 if (((!NILP (Vtransient_mark_mode)
12976 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12977 != !NILP (w->region_showing))
12978 || (!NILP (w->region_showing)
12979 && !EQ (w->region_showing,
12980 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12981 CHARPOS (this_line_start_pos) = 0;
12982
12983 /* Optimize the case that only the line containing the cursor in the
12984 selected window has changed. Variables starting with this_ are
12985 set in display_line and record information about the line
12986 containing the cursor. */
12987 tlbufpos = this_line_start_pos;
12988 tlendpos = this_line_end_pos;
12989 if (!consider_all_windows_p
12990 && CHARPOS (tlbufpos) > 0
12991 && NILP (w->update_mode_line)
12992 && !current_buffer->clip_changed
12993 && !current_buffer->prevent_redisplay_optimizations_p
12994 && FRAME_VISIBLE_P (XFRAME (w->frame))
12995 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12996 /* Make sure recorded data applies to current buffer, etc. */
12997 && this_line_buffer == current_buffer
12998 && current_buffer == XBUFFER (w->buffer)
12999 && NILP (w->force_start)
13000 && NILP (w->optional_new_start)
13001 /* Point must be on the line that we have info recorded about. */
13002 && PT >= CHARPOS (tlbufpos)
13003 && PT <= Z - CHARPOS (tlendpos)
13004 /* All text outside that line, including its final newline,
13005 must be unchanged. */
13006 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13007 CHARPOS (tlendpos)))
13008 {
13009 if (CHARPOS (tlbufpos) > BEGV
13010 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13011 && (CHARPOS (tlbufpos) == ZV
13012 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13013 /* Former continuation line has disappeared by becoming empty. */
13014 goto cancel;
13015 else if (XFASTINT (w->last_modified) < MODIFF
13016 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13017 || MINI_WINDOW_P (w))
13018 {
13019 /* We have to handle the case of continuation around a
13020 wide-column character (see the comment in indent.c around
13021 line 1340).
13022
13023 For instance, in the following case:
13024
13025 -------- Insert --------
13026 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13027 J_I_ ==> J_I_ `^^' are cursors.
13028 ^^ ^^
13029 -------- --------
13030
13031 As we have to redraw the line above, we cannot use this
13032 optimization. */
13033
13034 struct it it;
13035 int line_height_before = this_line_pixel_height;
13036
13037 /* Note that start_display will handle the case that the
13038 line starting at tlbufpos is a continuation line. */
13039 start_display (&it, w, tlbufpos);
13040
13041 /* Implementation note: It this still necessary? */
13042 if (it.current_x != this_line_start_x)
13043 goto cancel;
13044
13045 TRACE ((stderr, "trying display optimization 1\n"));
13046 w->cursor.vpos = -1;
13047 overlay_arrow_seen = 0;
13048 it.vpos = this_line_vpos;
13049 it.current_y = this_line_y;
13050 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13051 display_line (&it);
13052
13053 /* If line contains point, is not continued,
13054 and ends at same distance from eob as before, we win. */
13055 if (w->cursor.vpos >= 0
13056 /* Line is not continued, otherwise this_line_start_pos
13057 would have been set to 0 in display_line. */
13058 && CHARPOS (this_line_start_pos)
13059 /* Line ends as before. */
13060 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13061 /* Line has same height as before. Otherwise other lines
13062 would have to be shifted up or down. */
13063 && this_line_pixel_height == line_height_before)
13064 {
13065 /* If this is not the window's last line, we must adjust
13066 the charstarts of the lines below. */
13067 if (it.current_y < it.last_visible_y)
13068 {
13069 struct glyph_row *row
13070 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13071 EMACS_INT delta, delta_bytes;
13072
13073 /* We used to distinguish between two cases here,
13074 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13075 when the line ends in a newline or the end of the
13076 buffer's accessible portion. But both cases did
13077 the same, so they were collapsed. */
13078 delta = (Z
13079 - CHARPOS (tlendpos)
13080 - MATRIX_ROW_START_CHARPOS (row));
13081 delta_bytes = (Z_BYTE
13082 - BYTEPOS (tlendpos)
13083 - MATRIX_ROW_START_BYTEPOS (row));
13084
13085 increment_matrix_positions (w->current_matrix,
13086 this_line_vpos + 1,
13087 w->current_matrix->nrows,
13088 delta, delta_bytes);
13089 }
13090
13091 /* If this row displays text now but previously didn't,
13092 or vice versa, w->window_end_vpos may have to be
13093 adjusted. */
13094 if ((it.glyph_row - 1)->displays_text_p)
13095 {
13096 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13097 XSETINT (w->window_end_vpos, this_line_vpos);
13098 }
13099 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13100 && this_line_vpos > 0)
13101 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13102 w->window_end_valid = Qnil;
13103
13104 /* Update hint: No need to try to scroll in update_window. */
13105 w->desired_matrix->no_scrolling_p = 1;
13106
13107 #if GLYPH_DEBUG
13108 *w->desired_matrix->method = 0;
13109 debug_method_add (w, "optimization 1");
13110 #endif
13111 #ifdef HAVE_WINDOW_SYSTEM
13112 update_window_fringes (w, 0);
13113 #endif
13114 goto update;
13115 }
13116 else
13117 goto cancel;
13118 }
13119 else if (/* Cursor position hasn't changed. */
13120 PT == XFASTINT (w->last_point)
13121 /* Make sure the cursor was last displayed
13122 in this window. Otherwise we have to reposition it. */
13123 && 0 <= w->cursor.vpos
13124 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13125 {
13126 if (!must_finish)
13127 {
13128 do_pending_window_change (1);
13129 /* If selected_window changed, redisplay again. */
13130 if (WINDOWP (selected_window)
13131 && (w = XWINDOW (selected_window)) != sw)
13132 goto retry;
13133
13134 /* We used to always goto end_of_redisplay here, but this
13135 isn't enough if we have a blinking cursor. */
13136 if (w->cursor_off_p == w->last_cursor_off_p)
13137 goto end_of_redisplay;
13138 }
13139 goto update;
13140 }
13141 /* If highlighting the region, or if the cursor is in the echo area,
13142 then we can't just move the cursor. */
13143 else if (! (!NILP (Vtransient_mark_mode)
13144 && !NILP (BVAR (current_buffer, mark_active)))
13145 && (EQ (selected_window,
13146 BVAR (current_buffer, last_selected_window))
13147 || highlight_nonselected_windows)
13148 && NILP (w->region_showing)
13149 && NILP (Vshow_trailing_whitespace)
13150 && !cursor_in_echo_area)
13151 {
13152 struct it it;
13153 struct glyph_row *row;
13154
13155 /* Skip from tlbufpos to PT and see where it is. Note that
13156 PT may be in invisible text. If so, we will end at the
13157 next visible position. */
13158 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13159 NULL, DEFAULT_FACE_ID);
13160 it.current_x = this_line_start_x;
13161 it.current_y = this_line_y;
13162 it.vpos = this_line_vpos;
13163
13164 /* The call to move_it_to stops in front of PT, but
13165 moves over before-strings. */
13166 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13167
13168 if (it.vpos == this_line_vpos
13169 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13170 row->enabled_p))
13171 {
13172 xassert (this_line_vpos == it.vpos);
13173 xassert (this_line_y == it.current_y);
13174 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13175 #if GLYPH_DEBUG
13176 *w->desired_matrix->method = 0;
13177 debug_method_add (w, "optimization 3");
13178 #endif
13179 goto update;
13180 }
13181 else
13182 goto cancel;
13183 }
13184
13185 cancel:
13186 /* Text changed drastically or point moved off of line. */
13187 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13188 }
13189
13190 CHARPOS (this_line_start_pos) = 0;
13191 consider_all_windows_p |= buffer_shared > 1;
13192 ++clear_face_cache_count;
13193 #ifdef HAVE_WINDOW_SYSTEM
13194 ++clear_image_cache_count;
13195 #endif
13196
13197 /* Build desired matrices, and update the display. If
13198 consider_all_windows_p is non-zero, do it for all windows on all
13199 frames. Otherwise do it for selected_window, only. */
13200
13201 if (consider_all_windows_p)
13202 {
13203 Lisp_Object tail, frame;
13204
13205 FOR_EACH_FRAME (tail, frame)
13206 XFRAME (frame)->updated_p = 0;
13207
13208 /* Recompute # windows showing selected buffer. This will be
13209 incremented each time such a window is displayed. */
13210 buffer_shared = 0;
13211
13212 FOR_EACH_FRAME (tail, frame)
13213 {
13214 struct frame *f = XFRAME (frame);
13215
13216 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13217 {
13218 if (! EQ (frame, selected_frame))
13219 /* Select the frame, for the sake of frame-local
13220 variables. */
13221 select_frame_for_redisplay (frame);
13222
13223 /* Mark all the scroll bars to be removed; we'll redeem
13224 the ones we want when we redisplay their windows. */
13225 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13226 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13227
13228 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13229 redisplay_windows (FRAME_ROOT_WINDOW (f));
13230
13231 /* The X error handler may have deleted that frame. */
13232 if (!FRAME_LIVE_P (f))
13233 continue;
13234
13235 /* Any scroll bars which redisplay_windows should have
13236 nuked should now go away. */
13237 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13238 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13239
13240 /* If fonts changed, display again. */
13241 /* ??? rms: I suspect it is a mistake to jump all the way
13242 back to retry here. It should just retry this frame. */
13243 if (fonts_changed_p)
13244 goto retry;
13245
13246 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13247 {
13248 /* See if we have to hscroll. */
13249 if (!f->already_hscrolled_p)
13250 {
13251 f->already_hscrolled_p = 1;
13252 if (hscroll_windows (f->root_window))
13253 goto retry;
13254 }
13255
13256 /* Prevent various kinds of signals during display
13257 update. stdio is not robust about handling
13258 signals, which can cause an apparent I/O
13259 error. */
13260 if (interrupt_input)
13261 unrequest_sigio ();
13262 STOP_POLLING;
13263
13264 /* Update the display. */
13265 set_window_update_flags (XWINDOW (f->root_window), 1);
13266 pending |= update_frame (f, 0, 0);
13267 f->updated_p = 1;
13268 }
13269 }
13270 }
13271
13272 if (!EQ (old_frame, selected_frame)
13273 && FRAME_LIVE_P (XFRAME (old_frame)))
13274 /* We played a bit fast-and-loose above and allowed selected_frame
13275 and selected_window to be temporarily out-of-sync but let's make
13276 sure this stays contained. */
13277 select_frame_for_redisplay (old_frame);
13278 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13279
13280 if (!pending)
13281 {
13282 /* Do the mark_window_display_accurate after all windows have
13283 been redisplayed because this call resets flags in buffers
13284 which are needed for proper redisplay. */
13285 FOR_EACH_FRAME (tail, frame)
13286 {
13287 struct frame *f = XFRAME (frame);
13288 if (f->updated_p)
13289 {
13290 mark_window_display_accurate (f->root_window, 1);
13291 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13292 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13293 }
13294 }
13295 }
13296 }
13297 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13298 {
13299 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13300 struct frame *mini_frame;
13301
13302 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13303 /* Use list_of_error, not Qerror, so that
13304 we catch only errors and don't run the debugger. */
13305 internal_condition_case_1 (redisplay_window_1, selected_window,
13306 list_of_error,
13307 redisplay_window_error);
13308 if (update_miniwindow_p)
13309 internal_condition_case_1 (redisplay_window_1, mini_window,
13310 list_of_error,
13311 redisplay_window_error);
13312
13313 /* Compare desired and current matrices, perform output. */
13314
13315 update:
13316 /* If fonts changed, display again. */
13317 if (fonts_changed_p)
13318 goto retry;
13319
13320 /* Prevent various kinds of signals during display update.
13321 stdio is not robust about handling signals,
13322 which can cause an apparent I/O error. */
13323 if (interrupt_input)
13324 unrequest_sigio ();
13325 STOP_POLLING;
13326
13327 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13328 {
13329 if (hscroll_windows (selected_window))
13330 goto retry;
13331
13332 XWINDOW (selected_window)->must_be_updated_p = 1;
13333 pending = update_frame (sf, 0, 0);
13334 }
13335
13336 /* We may have called echo_area_display at the top of this
13337 function. If the echo area is on another frame, that may
13338 have put text on a frame other than the selected one, so the
13339 above call to update_frame would not have caught it. Catch
13340 it here. */
13341 mini_window = FRAME_MINIBUF_WINDOW (sf);
13342 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13343
13344 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13345 {
13346 XWINDOW (mini_window)->must_be_updated_p = 1;
13347 pending |= update_frame (mini_frame, 0, 0);
13348 if (!pending && hscroll_windows (mini_window))
13349 goto retry;
13350 }
13351 }
13352
13353 /* If display was paused because of pending input, make sure we do a
13354 thorough update the next time. */
13355 if (pending)
13356 {
13357 /* Prevent the optimization at the beginning of
13358 redisplay_internal that tries a single-line update of the
13359 line containing the cursor in the selected window. */
13360 CHARPOS (this_line_start_pos) = 0;
13361
13362 /* Let the overlay arrow be updated the next time. */
13363 update_overlay_arrows (0);
13364
13365 /* If we pause after scrolling, some rows in the current
13366 matrices of some windows are not valid. */
13367 if (!WINDOW_FULL_WIDTH_P (w)
13368 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13369 update_mode_lines = 1;
13370 }
13371 else
13372 {
13373 if (!consider_all_windows_p)
13374 {
13375 /* This has already been done above if
13376 consider_all_windows_p is set. */
13377 mark_window_display_accurate_1 (w, 1);
13378
13379 /* Say overlay arrows are up to date. */
13380 update_overlay_arrows (1);
13381
13382 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13383 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13384 }
13385
13386 update_mode_lines = 0;
13387 windows_or_buffers_changed = 0;
13388 cursor_type_changed = 0;
13389 }
13390
13391 /* Start SIGIO interrupts coming again. Having them off during the
13392 code above makes it less likely one will discard output, but not
13393 impossible, since there might be stuff in the system buffer here.
13394 But it is much hairier to try to do anything about that. */
13395 if (interrupt_input)
13396 request_sigio ();
13397 RESUME_POLLING;
13398
13399 /* If a frame has become visible which was not before, redisplay
13400 again, so that we display it. Expose events for such a frame
13401 (which it gets when becoming visible) don't call the parts of
13402 redisplay constructing glyphs, so simply exposing a frame won't
13403 display anything in this case. So, we have to display these
13404 frames here explicitly. */
13405 if (!pending)
13406 {
13407 Lisp_Object tail, frame;
13408 int new_count = 0;
13409
13410 FOR_EACH_FRAME (tail, frame)
13411 {
13412 int this_is_visible = 0;
13413
13414 if (XFRAME (frame)->visible)
13415 this_is_visible = 1;
13416 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13417 if (XFRAME (frame)->visible)
13418 this_is_visible = 1;
13419
13420 if (this_is_visible)
13421 new_count++;
13422 }
13423
13424 if (new_count != number_of_visible_frames)
13425 windows_or_buffers_changed++;
13426 }
13427
13428 /* Change frame size now if a change is pending. */
13429 do_pending_window_change (1);
13430
13431 /* If we just did a pending size change, or have additional
13432 visible frames, or selected_window changed, redisplay again. */
13433 if ((windows_or_buffers_changed && !pending)
13434 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13435 goto retry;
13436
13437 /* Clear the face and image caches.
13438
13439 We used to do this only if consider_all_windows_p. But the cache
13440 needs to be cleared if a timer creates images in the current
13441 buffer (e.g. the test case in Bug#6230). */
13442
13443 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13444 {
13445 clear_face_cache (0);
13446 clear_face_cache_count = 0;
13447 }
13448
13449 #ifdef HAVE_WINDOW_SYSTEM
13450 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13451 {
13452 clear_image_caches (Qnil);
13453 clear_image_cache_count = 0;
13454 }
13455 #endif /* HAVE_WINDOW_SYSTEM */
13456
13457 end_of_redisplay:
13458 unbind_to (count, Qnil);
13459 RESUME_POLLING;
13460 }
13461
13462
13463 /* Redisplay, but leave alone any recent echo area message unless
13464 another message has been requested in its place.
13465
13466 This is useful in situations where you need to redisplay but no
13467 user action has occurred, making it inappropriate for the message
13468 area to be cleared. See tracking_off and
13469 wait_reading_process_output for examples of these situations.
13470
13471 FROM_WHERE is an integer saying from where this function was
13472 called. This is useful for debugging. */
13473
13474 void
13475 redisplay_preserve_echo_area (int from_where)
13476 {
13477 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13478
13479 if (!NILP (echo_area_buffer[1]))
13480 {
13481 /* We have a previously displayed message, but no current
13482 message. Redisplay the previous message. */
13483 display_last_displayed_message_p = 1;
13484 redisplay_internal ();
13485 display_last_displayed_message_p = 0;
13486 }
13487 else
13488 redisplay_internal ();
13489
13490 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13491 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13492 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13493 }
13494
13495
13496 /* Function registered with record_unwind_protect in
13497 redisplay_internal. Reset redisplaying_p to the value it had
13498 before redisplay_internal was called, and clear
13499 prevent_freeing_realized_faces_p. It also selects the previously
13500 selected frame, unless it has been deleted (by an X connection
13501 failure during redisplay, for example). */
13502
13503 static Lisp_Object
13504 unwind_redisplay (Lisp_Object val)
13505 {
13506 Lisp_Object old_redisplaying_p, old_frame;
13507
13508 old_redisplaying_p = XCAR (val);
13509 redisplaying_p = XFASTINT (old_redisplaying_p);
13510 old_frame = XCDR (val);
13511 if (! EQ (old_frame, selected_frame)
13512 && FRAME_LIVE_P (XFRAME (old_frame)))
13513 select_frame_for_redisplay (old_frame);
13514 return Qnil;
13515 }
13516
13517
13518 /* Mark the display of window W as accurate or inaccurate. If
13519 ACCURATE_P is non-zero mark display of W as accurate. If
13520 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13521 redisplay_internal is called. */
13522
13523 static void
13524 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13525 {
13526 if (BUFFERP (w->buffer))
13527 {
13528 struct buffer *b = XBUFFER (w->buffer);
13529
13530 w->last_modified
13531 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13532 w->last_overlay_modified
13533 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13534 w->last_had_star
13535 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13536
13537 if (accurate_p)
13538 {
13539 b->clip_changed = 0;
13540 b->prevent_redisplay_optimizations_p = 0;
13541
13542 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13543 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13544 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13545 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13546
13547 w->current_matrix->buffer = b;
13548 w->current_matrix->begv = BUF_BEGV (b);
13549 w->current_matrix->zv = BUF_ZV (b);
13550
13551 w->last_cursor = w->cursor;
13552 w->last_cursor_off_p = w->cursor_off_p;
13553
13554 if (w == XWINDOW (selected_window))
13555 w->last_point = make_number (BUF_PT (b));
13556 else
13557 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13558 }
13559 }
13560
13561 if (accurate_p)
13562 {
13563 w->window_end_valid = w->buffer;
13564 w->update_mode_line = Qnil;
13565 }
13566 }
13567
13568
13569 /* Mark the display of windows in the window tree rooted at WINDOW as
13570 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13571 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13572 be redisplayed the next time redisplay_internal is called. */
13573
13574 void
13575 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13576 {
13577 struct window *w;
13578
13579 for (; !NILP (window); window = w->next)
13580 {
13581 w = XWINDOW (window);
13582 mark_window_display_accurate_1 (w, accurate_p);
13583
13584 if (!NILP (w->vchild))
13585 mark_window_display_accurate (w->vchild, accurate_p);
13586 if (!NILP (w->hchild))
13587 mark_window_display_accurate (w->hchild, accurate_p);
13588 }
13589
13590 if (accurate_p)
13591 {
13592 update_overlay_arrows (1);
13593 }
13594 else
13595 {
13596 /* Force a thorough redisplay the next time by setting
13597 last_arrow_position and last_arrow_string to t, which is
13598 unequal to any useful value of Voverlay_arrow_... */
13599 update_overlay_arrows (-1);
13600 }
13601 }
13602
13603
13604 /* Return value in display table DP (Lisp_Char_Table *) for character
13605 C. Since a display table doesn't have any parent, we don't have to
13606 follow parent. Do not call this function directly but use the
13607 macro DISP_CHAR_VECTOR. */
13608
13609 Lisp_Object
13610 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13611 {
13612 Lisp_Object val;
13613
13614 if (ASCII_CHAR_P (c))
13615 {
13616 val = dp->ascii;
13617 if (SUB_CHAR_TABLE_P (val))
13618 val = XSUB_CHAR_TABLE (val)->contents[c];
13619 }
13620 else
13621 {
13622 Lisp_Object table;
13623
13624 XSETCHAR_TABLE (table, dp);
13625 val = char_table_ref (table, c);
13626 }
13627 if (NILP (val))
13628 val = dp->defalt;
13629 return val;
13630 }
13631
13632
13633 \f
13634 /***********************************************************************
13635 Window Redisplay
13636 ***********************************************************************/
13637
13638 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13639
13640 static void
13641 redisplay_windows (Lisp_Object window)
13642 {
13643 while (!NILP (window))
13644 {
13645 struct window *w = XWINDOW (window);
13646
13647 if (!NILP (w->hchild))
13648 redisplay_windows (w->hchild);
13649 else if (!NILP (w->vchild))
13650 redisplay_windows (w->vchild);
13651 else if (!NILP (w->buffer))
13652 {
13653 displayed_buffer = XBUFFER (w->buffer);
13654 /* Use list_of_error, not Qerror, so that
13655 we catch only errors and don't run the debugger. */
13656 internal_condition_case_1 (redisplay_window_0, window,
13657 list_of_error,
13658 redisplay_window_error);
13659 }
13660
13661 window = w->next;
13662 }
13663 }
13664
13665 static Lisp_Object
13666 redisplay_window_error (Lisp_Object ignore)
13667 {
13668 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13669 return Qnil;
13670 }
13671
13672 static Lisp_Object
13673 redisplay_window_0 (Lisp_Object window)
13674 {
13675 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13676 redisplay_window (window, 0);
13677 return Qnil;
13678 }
13679
13680 static Lisp_Object
13681 redisplay_window_1 (Lisp_Object window)
13682 {
13683 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13684 redisplay_window (window, 1);
13685 return Qnil;
13686 }
13687 \f
13688
13689 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13690 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13691 which positions recorded in ROW differ from current buffer
13692 positions.
13693
13694 Return 0 if cursor is not on this row, 1 otherwise. */
13695
13696 static int
13697 set_cursor_from_row (struct window *w, struct glyph_row *row,
13698 struct glyph_matrix *matrix,
13699 EMACS_INT delta, EMACS_INT delta_bytes,
13700 int dy, int dvpos)
13701 {
13702 struct glyph *glyph = row->glyphs[TEXT_AREA];
13703 struct glyph *end = glyph + row->used[TEXT_AREA];
13704 struct glyph *cursor = NULL;
13705 /* The last known character position in row. */
13706 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13707 int x = row->x;
13708 EMACS_INT pt_old = PT - delta;
13709 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13710 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13711 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13712 /* A glyph beyond the edge of TEXT_AREA which we should never
13713 touch. */
13714 struct glyph *glyphs_end = end;
13715 /* Non-zero means we've found a match for cursor position, but that
13716 glyph has the avoid_cursor_p flag set. */
13717 int match_with_avoid_cursor = 0;
13718 /* Non-zero means we've seen at least one glyph that came from a
13719 display string. */
13720 int string_seen = 0;
13721 /* Largest and smallest buffer positions seen so far during scan of
13722 glyph row. */
13723 EMACS_INT bpos_max = pos_before;
13724 EMACS_INT bpos_min = pos_after;
13725 /* Last buffer position covered by an overlay string with an integer
13726 `cursor' property. */
13727 EMACS_INT bpos_covered = 0;
13728 /* Non-zero means the display string on which to display the cursor
13729 comes from a text property, not from an overlay. */
13730 int string_from_text_prop = 0;
13731
13732 /* Don't even try doing anything if called for a mode-line or
13733 header-line row, since the rest of the code isn't prepared to
13734 deal with such calamities. */
13735 xassert (!row->mode_line_p);
13736 if (row->mode_line_p)
13737 return 0;
13738
13739 /* Skip over glyphs not having an object at the start and the end of
13740 the row. These are special glyphs like truncation marks on
13741 terminal frames. */
13742 if (row->displays_text_p)
13743 {
13744 if (!row->reversed_p)
13745 {
13746 while (glyph < end
13747 && INTEGERP (glyph->object)
13748 && glyph->charpos < 0)
13749 {
13750 x += glyph->pixel_width;
13751 ++glyph;
13752 }
13753 while (end > glyph
13754 && INTEGERP ((end - 1)->object)
13755 /* CHARPOS is zero for blanks and stretch glyphs
13756 inserted by extend_face_to_end_of_line. */
13757 && (end - 1)->charpos <= 0)
13758 --end;
13759 glyph_before = glyph - 1;
13760 glyph_after = end;
13761 }
13762 else
13763 {
13764 struct glyph *g;
13765
13766 /* If the glyph row is reversed, we need to process it from back
13767 to front, so swap the edge pointers. */
13768 glyphs_end = end = glyph - 1;
13769 glyph += row->used[TEXT_AREA] - 1;
13770
13771 while (glyph > end + 1
13772 && INTEGERP (glyph->object)
13773 && glyph->charpos < 0)
13774 {
13775 --glyph;
13776 x -= glyph->pixel_width;
13777 }
13778 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13779 --glyph;
13780 /* By default, in reversed rows we put the cursor on the
13781 rightmost (first in the reading order) glyph. */
13782 for (g = end + 1; g < glyph; g++)
13783 x += g->pixel_width;
13784 while (end < glyph
13785 && INTEGERP ((end + 1)->object)
13786 && (end + 1)->charpos <= 0)
13787 ++end;
13788 glyph_before = glyph + 1;
13789 glyph_after = end;
13790 }
13791 }
13792 else if (row->reversed_p)
13793 {
13794 /* In R2L rows that don't display text, put the cursor on the
13795 rightmost glyph. Case in point: an empty last line that is
13796 part of an R2L paragraph. */
13797 cursor = end - 1;
13798 /* Avoid placing the cursor on the last glyph of the row, where
13799 on terminal frames we hold the vertical border between
13800 adjacent windows. */
13801 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13802 && !WINDOW_RIGHTMOST_P (w)
13803 && cursor == row->glyphs[LAST_AREA] - 1)
13804 cursor--;
13805 x = -1; /* will be computed below, at label compute_x */
13806 }
13807
13808 /* Step 1: Try to find the glyph whose character position
13809 corresponds to point. If that's not possible, find 2 glyphs
13810 whose character positions are the closest to point, one before
13811 point, the other after it. */
13812 if (!row->reversed_p)
13813 while (/* not marched to end of glyph row */
13814 glyph < end
13815 /* glyph was not inserted by redisplay for internal purposes */
13816 && !INTEGERP (glyph->object))
13817 {
13818 if (BUFFERP (glyph->object))
13819 {
13820 EMACS_INT dpos = glyph->charpos - pt_old;
13821
13822 if (glyph->charpos > bpos_max)
13823 bpos_max = glyph->charpos;
13824 if (glyph->charpos < bpos_min)
13825 bpos_min = glyph->charpos;
13826 if (!glyph->avoid_cursor_p)
13827 {
13828 /* If we hit point, we've found the glyph on which to
13829 display the cursor. */
13830 if (dpos == 0)
13831 {
13832 match_with_avoid_cursor = 0;
13833 break;
13834 }
13835 /* See if we've found a better approximation to
13836 POS_BEFORE or to POS_AFTER. Note that we want the
13837 first (leftmost) glyph of all those that are the
13838 closest from below, and the last (rightmost) of all
13839 those from above. */
13840 if (0 > dpos && dpos > pos_before - pt_old)
13841 {
13842 pos_before = glyph->charpos;
13843 glyph_before = glyph;
13844 }
13845 else if (0 < dpos && dpos <= pos_after - pt_old)
13846 {
13847 pos_after = glyph->charpos;
13848 glyph_after = glyph;
13849 }
13850 }
13851 else if (dpos == 0)
13852 match_with_avoid_cursor = 1;
13853 }
13854 else if (STRINGP (glyph->object))
13855 {
13856 Lisp_Object chprop;
13857 EMACS_INT glyph_pos = glyph->charpos;
13858
13859 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13860 glyph->object);
13861 if (!NILP (chprop))
13862 {
13863 /* If the string came from a `display' text property,
13864 look up the buffer position of that property and
13865 use that position to update bpos_max, as if we
13866 actually saw such a position in one of the row's
13867 glyphs. This helps with supporting integer values
13868 of `cursor' property on the display string in
13869 situations where most or all of the row's buffer
13870 text is completely covered by display properties,
13871 so that no glyph with valid buffer positions is
13872 ever seen in the row. */
13873 EMACS_INT prop_pos =
13874 string_buffer_position_lim (glyph->object, pos_before,
13875 pos_after, 0);
13876
13877 if (prop_pos >= pos_before)
13878 bpos_max = prop_pos - 1;
13879 }
13880 if (INTEGERP (chprop))
13881 {
13882 bpos_covered = bpos_max + XINT (chprop);
13883 /* If the `cursor' property covers buffer positions up
13884 to and including point, we should display cursor on
13885 this glyph. Note that, if a `cursor' property on one
13886 of the string's characters has an integer value, we
13887 will break out of the loop below _before_ we get to
13888 the position match above. IOW, integer values of
13889 the `cursor' property override the "exact match for
13890 point" strategy of positioning the cursor. */
13891 /* Implementation note: bpos_max == pt_old when, e.g.,
13892 we are in an empty line, where bpos_max is set to
13893 MATRIX_ROW_START_CHARPOS, see above. */
13894 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13895 {
13896 cursor = glyph;
13897 break;
13898 }
13899 }
13900
13901 string_seen = 1;
13902 }
13903 x += glyph->pixel_width;
13904 ++glyph;
13905 }
13906 else if (glyph > end) /* row is reversed */
13907 while (!INTEGERP (glyph->object))
13908 {
13909 if (BUFFERP (glyph->object))
13910 {
13911 EMACS_INT dpos = glyph->charpos - pt_old;
13912
13913 if (glyph->charpos > bpos_max)
13914 bpos_max = glyph->charpos;
13915 if (glyph->charpos < bpos_min)
13916 bpos_min = glyph->charpos;
13917 if (!glyph->avoid_cursor_p)
13918 {
13919 if (dpos == 0)
13920 {
13921 match_with_avoid_cursor = 0;
13922 break;
13923 }
13924 if (0 > dpos && dpos > pos_before - pt_old)
13925 {
13926 pos_before = glyph->charpos;
13927 glyph_before = glyph;
13928 }
13929 else if (0 < dpos && dpos <= pos_after - pt_old)
13930 {
13931 pos_after = glyph->charpos;
13932 glyph_after = glyph;
13933 }
13934 }
13935 else if (dpos == 0)
13936 match_with_avoid_cursor = 1;
13937 }
13938 else if (STRINGP (glyph->object))
13939 {
13940 Lisp_Object chprop;
13941 EMACS_INT glyph_pos = glyph->charpos;
13942
13943 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13944 glyph->object);
13945 if (!NILP (chprop))
13946 {
13947 EMACS_INT prop_pos =
13948 string_buffer_position_lim (glyph->object, pos_before,
13949 pos_after, 0);
13950
13951 if (prop_pos >= pos_before)
13952 bpos_max = prop_pos - 1;
13953 }
13954 if (INTEGERP (chprop))
13955 {
13956 bpos_covered = bpos_max + XINT (chprop);
13957 /* If the `cursor' property covers buffer positions up
13958 to and including point, we should display cursor on
13959 this glyph. */
13960 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13961 {
13962 cursor = glyph;
13963 break;
13964 }
13965 }
13966 string_seen = 1;
13967 }
13968 --glyph;
13969 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13970 {
13971 x--; /* can't use any pixel_width */
13972 break;
13973 }
13974 x -= glyph->pixel_width;
13975 }
13976
13977 /* Step 2: If we didn't find an exact match for point, we need to
13978 look for a proper place to put the cursor among glyphs between
13979 GLYPH_BEFORE and GLYPH_AFTER. */
13980 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13981 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13982 && bpos_covered < pt_old)
13983 {
13984 /* An empty line has a single glyph whose OBJECT is zero and
13985 whose CHARPOS is the position of a newline on that line.
13986 Note that on a TTY, there are more glyphs after that, which
13987 were produced by extend_face_to_end_of_line, but their
13988 CHARPOS is zero or negative. */
13989 int empty_line_p =
13990 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13991 && INTEGERP (glyph->object) && glyph->charpos > 0;
13992
13993 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13994 {
13995 EMACS_INT ellipsis_pos;
13996
13997 /* Scan back over the ellipsis glyphs. */
13998 if (!row->reversed_p)
13999 {
14000 ellipsis_pos = (glyph - 1)->charpos;
14001 while (glyph > row->glyphs[TEXT_AREA]
14002 && (glyph - 1)->charpos == ellipsis_pos)
14003 glyph--, x -= glyph->pixel_width;
14004 /* That loop always goes one position too far, including
14005 the glyph before the ellipsis. So scan forward over
14006 that one. */
14007 x += glyph->pixel_width;
14008 glyph++;
14009 }
14010 else /* row is reversed */
14011 {
14012 ellipsis_pos = (glyph + 1)->charpos;
14013 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14014 && (glyph + 1)->charpos == ellipsis_pos)
14015 glyph++, x += glyph->pixel_width;
14016 x -= glyph->pixel_width;
14017 glyph--;
14018 }
14019 }
14020 else if (match_with_avoid_cursor)
14021 {
14022 cursor = glyph_after;
14023 x = -1;
14024 }
14025 else if (string_seen)
14026 {
14027 int incr = row->reversed_p ? -1 : +1;
14028
14029 /* Need to find the glyph that came out of a string which is
14030 present at point. That glyph is somewhere between
14031 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14032 positioned between POS_BEFORE and POS_AFTER in the
14033 buffer. */
14034 struct glyph *start, *stop;
14035 EMACS_INT pos = pos_before;
14036
14037 x = -1;
14038
14039 /* If the row ends in a newline from a display string,
14040 reordering could have moved the glyphs belonging to the
14041 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14042 in this case we extend the search to the last glyph in
14043 the row that was not inserted by redisplay. */
14044 if (row->ends_in_newline_from_string_p)
14045 {
14046 glyph_after = end;
14047 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14048 }
14049
14050 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14051 correspond to POS_BEFORE and POS_AFTER, respectively. We
14052 need START and STOP in the order that corresponds to the
14053 row's direction as given by its reversed_p flag. If the
14054 directionality of characters between POS_BEFORE and
14055 POS_AFTER is the opposite of the row's base direction,
14056 these characters will have been reordered for display,
14057 and we need to reverse START and STOP. */
14058 if (!row->reversed_p)
14059 {
14060 start = min (glyph_before, glyph_after);
14061 stop = max (glyph_before, glyph_after);
14062 }
14063 else
14064 {
14065 start = max (glyph_before, glyph_after);
14066 stop = min (glyph_before, glyph_after);
14067 }
14068 for (glyph = start + incr;
14069 row->reversed_p ? glyph > stop : glyph < stop; )
14070 {
14071
14072 /* Any glyphs that come from the buffer are here because
14073 of bidi reordering. Skip them, and only pay
14074 attention to glyphs that came from some string. */
14075 if (STRINGP (glyph->object))
14076 {
14077 Lisp_Object str;
14078 EMACS_INT tem;
14079 /* If the display property covers the newline, we
14080 need to search for it one position farther. */
14081 EMACS_INT lim = pos_after
14082 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14083
14084 string_from_text_prop = 0;
14085 str = glyph->object;
14086 tem = string_buffer_position_lim (str, pos, lim, 0);
14087 if (tem == 0 /* from overlay */
14088 || pos <= tem)
14089 {
14090 /* If the string from which this glyph came is
14091 found in the buffer at point, or at position
14092 that is closer to point than pos_after, then
14093 we've found the glyph we've been looking for.
14094 If it comes from an overlay (tem == 0), and
14095 it has the `cursor' property on one of its
14096 glyphs, record that glyph as a candidate for
14097 displaying the cursor. (As in the
14098 unidirectional version, we will display the
14099 cursor on the last candidate we find.) */
14100 if (tem == 0
14101 || tem == pt_old
14102 || (tem - pt_old > 0 && tem < pos_after))
14103 {
14104 /* The glyphs from this string could have
14105 been reordered. Find the one with the
14106 smallest string position. Or there could
14107 be a character in the string with the
14108 `cursor' property, which means display
14109 cursor on that character's glyph. */
14110 EMACS_INT strpos = glyph->charpos;
14111
14112 if (tem)
14113 {
14114 cursor = glyph;
14115 string_from_text_prop = 1;
14116 }
14117 for ( ;
14118 (row->reversed_p ? glyph > stop : glyph < stop)
14119 && EQ (glyph->object, str);
14120 glyph += incr)
14121 {
14122 Lisp_Object cprop;
14123 EMACS_INT gpos = glyph->charpos;
14124
14125 cprop = Fget_char_property (make_number (gpos),
14126 Qcursor,
14127 glyph->object);
14128 if (!NILP (cprop))
14129 {
14130 cursor = glyph;
14131 break;
14132 }
14133 if (tem && glyph->charpos < strpos)
14134 {
14135 strpos = glyph->charpos;
14136 cursor = glyph;
14137 }
14138 }
14139
14140 if (tem == pt_old
14141 || (tem - pt_old > 0 && tem < pos_after))
14142 goto compute_x;
14143 }
14144 if (tem)
14145 pos = tem + 1; /* don't find previous instances */
14146 }
14147 /* This string is not what we want; skip all of the
14148 glyphs that came from it. */
14149 while ((row->reversed_p ? glyph > stop : glyph < stop)
14150 && EQ (glyph->object, str))
14151 glyph += incr;
14152 }
14153 else
14154 glyph += incr;
14155 }
14156
14157 /* If we reached the end of the line, and END was from a string,
14158 the cursor is not on this line. */
14159 if (cursor == NULL
14160 && (row->reversed_p ? glyph <= end : glyph >= end)
14161 && STRINGP (end->object)
14162 && row->continued_p)
14163 return 0;
14164 }
14165 /* A truncated row may not include PT among its character positions.
14166 Setting the cursor inside the scroll margin will trigger
14167 recalculation of hscroll in hscroll_window_tree. But if a
14168 display string covers point, defer to the string-handling
14169 code below to figure this out. */
14170 else if (row->truncated_on_left_p && pt_old < bpos_min)
14171 {
14172 cursor = glyph_before;
14173 x = -1;
14174 }
14175 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14176 /* Zero-width characters produce no glyphs. */
14177 || (!empty_line_p
14178 && (row->reversed_p
14179 ? glyph_after > glyphs_end
14180 : glyph_after < glyphs_end)))
14181 {
14182 cursor = glyph_after;
14183 x = -1;
14184 }
14185 }
14186
14187 compute_x:
14188 if (cursor != NULL)
14189 glyph = cursor;
14190 if (x < 0)
14191 {
14192 struct glyph *g;
14193
14194 /* Need to compute x that corresponds to GLYPH. */
14195 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14196 {
14197 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14198 abort ();
14199 x += g->pixel_width;
14200 }
14201 }
14202
14203 /* ROW could be part of a continued line, which, under bidi
14204 reordering, might have other rows whose start and end charpos
14205 occlude point. Only set w->cursor if we found a better
14206 approximation to the cursor position than we have from previously
14207 examined candidate rows belonging to the same continued line. */
14208 if (/* we already have a candidate row */
14209 w->cursor.vpos >= 0
14210 /* that candidate is not the row we are processing */
14211 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14212 /* Make sure cursor.vpos specifies a row whose start and end
14213 charpos occlude point, and it is valid candidate for being a
14214 cursor-row. This is because some callers of this function
14215 leave cursor.vpos at the row where the cursor was displayed
14216 during the last redisplay cycle. */
14217 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14218 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14219 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14220 {
14221 struct glyph *g1 =
14222 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14223
14224 /* Don't consider glyphs that are outside TEXT_AREA. */
14225 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14226 return 0;
14227 /* Keep the candidate whose buffer position is the closest to
14228 point or has the `cursor' property. */
14229 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14230 w->cursor.hpos >= 0
14231 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14232 && ((BUFFERP (g1->object)
14233 && (g1->charpos == pt_old /* an exact match always wins */
14234 || (BUFFERP (glyph->object)
14235 && eabs (g1->charpos - pt_old)
14236 < eabs (glyph->charpos - pt_old))))
14237 /* previous candidate is a glyph from a string that has
14238 a non-nil `cursor' property */
14239 || (STRINGP (g1->object)
14240 && (!NILP (Fget_char_property (make_number (g1->charpos),
14241 Qcursor, g1->object))
14242 /* previous candidate is from the same display
14243 string as this one, and the display string
14244 came from a text property */
14245 || (EQ (g1->object, glyph->object)
14246 && string_from_text_prop)
14247 /* this candidate is from newline and its
14248 position is not an exact match */
14249 || (INTEGERP (glyph->object)
14250 && glyph->charpos != pt_old)))))
14251 return 0;
14252 /* If this candidate gives an exact match, use that. */
14253 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14254 /* If this candidate is a glyph created for the
14255 terminating newline of a line, and point is on that
14256 newline, it wins because it's an exact match. */
14257 || (!row->continued_p
14258 && INTEGERP (glyph->object)
14259 && glyph->charpos == 0
14260 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14261 /* Otherwise, keep the candidate that comes from a row
14262 spanning less buffer positions. This may win when one or
14263 both candidate positions are on glyphs that came from
14264 display strings, for which we cannot compare buffer
14265 positions. */
14266 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14267 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14268 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14269 return 0;
14270 }
14271 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14272 w->cursor.x = x;
14273 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14274 w->cursor.y = row->y + dy;
14275
14276 if (w == XWINDOW (selected_window))
14277 {
14278 if (!row->continued_p
14279 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14280 && row->x == 0)
14281 {
14282 this_line_buffer = XBUFFER (w->buffer);
14283
14284 CHARPOS (this_line_start_pos)
14285 = MATRIX_ROW_START_CHARPOS (row) + delta;
14286 BYTEPOS (this_line_start_pos)
14287 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14288
14289 CHARPOS (this_line_end_pos)
14290 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14291 BYTEPOS (this_line_end_pos)
14292 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14293
14294 this_line_y = w->cursor.y;
14295 this_line_pixel_height = row->height;
14296 this_line_vpos = w->cursor.vpos;
14297 this_line_start_x = row->x;
14298 }
14299 else
14300 CHARPOS (this_line_start_pos) = 0;
14301 }
14302
14303 return 1;
14304 }
14305
14306
14307 /* Run window scroll functions, if any, for WINDOW with new window
14308 start STARTP. Sets the window start of WINDOW to that position.
14309
14310 We assume that the window's buffer is really current. */
14311
14312 static inline struct text_pos
14313 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14314 {
14315 struct window *w = XWINDOW (window);
14316 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14317
14318 if (current_buffer != XBUFFER (w->buffer))
14319 abort ();
14320
14321 if (!NILP (Vwindow_scroll_functions))
14322 {
14323 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14324 make_number (CHARPOS (startp)));
14325 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14326 /* In case the hook functions switch buffers. */
14327 if (current_buffer != XBUFFER (w->buffer))
14328 set_buffer_internal_1 (XBUFFER (w->buffer));
14329 }
14330
14331 return startp;
14332 }
14333
14334
14335 /* Make sure the line containing the cursor is fully visible.
14336 A value of 1 means there is nothing to be done.
14337 (Either the line is fully visible, or it cannot be made so,
14338 or we cannot tell.)
14339
14340 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14341 is higher than window.
14342
14343 A value of 0 means the caller should do scrolling
14344 as if point had gone off the screen. */
14345
14346 static int
14347 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14348 {
14349 struct glyph_matrix *matrix;
14350 struct glyph_row *row;
14351 int window_height;
14352
14353 if (!make_cursor_line_fully_visible_p)
14354 return 1;
14355
14356 /* It's not always possible to find the cursor, e.g, when a window
14357 is full of overlay strings. Don't do anything in that case. */
14358 if (w->cursor.vpos < 0)
14359 return 1;
14360
14361 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14362 row = MATRIX_ROW (matrix, w->cursor.vpos);
14363
14364 /* If the cursor row is not partially visible, there's nothing to do. */
14365 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14366 return 1;
14367
14368 /* If the row the cursor is in is taller than the window's height,
14369 it's not clear what to do, so do nothing. */
14370 window_height = window_box_height (w);
14371 if (row->height >= window_height)
14372 {
14373 if (!force_p || MINI_WINDOW_P (w)
14374 || w->vscroll || w->cursor.vpos == 0)
14375 return 1;
14376 }
14377 return 0;
14378 }
14379
14380
14381 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14382 non-zero means only WINDOW is redisplayed in redisplay_internal.
14383 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14384 in redisplay_window to bring a partially visible line into view in
14385 the case that only the cursor has moved.
14386
14387 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14388 last screen line's vertical height extends past the end of the screen.
14389
14390 Value is
14391
14392 1 if scrolling succeeded
14393
14394 0 if scrolling didn't find point.
14395
14396 -1 if new fonts have been loaded so that we must interrupt
14397 redisplay, adjust glyph matrices, and try again. */
14398
14399 enum
14400 {
14401 SCROLLING_SUCCESS,
14402 SCROLLING_FAILED,
14403 SCROLLING_NEED_LARGER_MATRICES
14404 };
14405
14406 /* If scroll-conservatively is more than this, never recenter.
14407
14408 If you change this, don't forget to update the doc string of
14409 `scroll-conservatively' and the Emacs manual. */
14410 #define SCROLL_LIMIT 100
14411
14412 static int
14413 try_scrolling (Lisp_Object window, int just_this_one_p,
14414 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14415 int temp_scroll_step, int last_line_misfit)
14416 {
14417 struct window *w = XWINDOW (window);
14418 struct frame *f = XFRAME (w->frame);
14419 struct text_pos pos, startp;
14420 struct it it;
14421 int this_scroll_margin, scroll_max, rc, height;
14422 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14423 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14424 Lisp_Object aggressive;
14425 /* We will never try scrolling more than this number of lines. */
14426 int scroll_limit = SCROLL_LIMIT;
14427
14428 #if GLYPH_DEBUG
14429 debug_method_add (w, "try_scrolling");
14430 #endif
14431
14432 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14433
14434 /* Compute scroll margin height in pixels. We scroll when point is
14435 within this distance from the top or bottom of the window. */
14436 if (scroll_margin > 0)
14437 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14438 * FRAME_LINE_HEIGHT (f);
14439 else
14440 this_scroll_margin = 0;
14441
14442 /* Force arg_scroll_conservatively to have a reasonable value, to
14443 avoid scrolling too far away with slow move_it_* functions. Note
14444 that the user can supply scroll-conservatively equal to
14445 `most-positive-fixnum', which can be larger than INT_MAX. */
14446 if (arg_scroll_conservatively > scroll_limit)
14447 {
14448 arg_scroll_conservatively = scroll_limit + 1;
14449 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14450 }
14451 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14452 /* Compute how much we should try to scroll maximally to bring
14453 point into view. */
14454 scroll_max = (max (scroll_step,
14455 max (arg_scroll_conservatively, temp_scroll_step))
14456 * FRAME_LINE_HEIGHT (f));
14457 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14458 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14459 /* We're trying to scroll because of aggressive scrolling but no
14460 scroll_step is set. Choose an arbitrary one. */
14461 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14462 else
14463 scroll_max = 0;
14464
14465 too_near_end:
14466
14467 /* Decide whether to scroll down. */
14468 if (PT > CHARPOS (startp))
14469 {
14470 int scroll_margin_y;
14471
14472 /* Compute the pixel ypos of the scroll margin, then move IT to
14473 either that ypos or PT, whichever comes first. */
14474 start_display (&it, w, startp);
14475 scroll_margin_y = it.last_visible_y - this_scroll_margin
14476 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14477 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14478 (MOVE_TO_POS | MOVE_TO_Y));
14479
14480 if (PT > CHARPOS (it.current.pos))
14481 {
14482 int y0 = line_bottom_y (&it);
14483 /* Compute how many pixels below window bottom to stop searching
14484 for PT. This avoids costly search for PT that is far away if
14485 the user limited scrolling by a small number of lines, but
14486 always finds PT if scroll_conservatively is set to a large
14487 number, such as most-positive-fixnum. */
14488 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14489 int y_to_move = it.last_visible_y + slack;
14490
14491 /* Compute the distance from the scroll margin to PT or to
14492 the scroll limit, whichever comes first. This should
14493 include the height of the cursor line, to make that line
14494 fully visible. */
14495 move_it_to (&it, PT, -1, y_to_move,
14496 -1, MOVE_TO_POS | MOVE_TO_Y);
14497 dy = line_bottom_y (&it) - y0;
14498
14499 if (dy > scroll_max)
14500 return SCROLLING_FAILED;
14501
14502 if (dy > 0)
14503 scroll_down_p = 1;
14504 }
14505 }
14506
14507 if (scroll_down_p)
14508 {
14509 /* Point is in or below the bottom scroll margin, so move the
14510 window start down. If scrolling conservatively, move it just
14511 enough down to make point visible. If scroll_step is set,
14512 move it down by scroll_step. */
14513 if (arg_scroll_conservatively)
14514 amount_to_scroll
14515 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14516 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14517 else if (scroll_step || temp_scroll_step)
14518 amount_to_scroll = scroll_max;
14519 else
14520 {
14521 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14522 height = WINDOW_BOX_TEXT_HEIGHT (w);
14523 if (NUMBERP (aggressive))
14524 {
14525 double float_amount = XFLOATINT (aggressive) * height;
14526 amount_to_scroll = float_amount;
14527 if (amount_to_scroll == 0 && float_amount > 0)
14528 amount_to_scroll = 1;
14529 /* Don't let point enter the scroll margin near top of
14530 the window. */
14531 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14532 amount_to_scroll = height - 2*this_scroll_margin + dy;
14533 }
14534 }
14535
14536 if (amount_to_scroll <= 0)
14537 return SCROLLING_FAILED;
14538
14539 start_display (&it, w, startp);
14540 if (arg_scroll_conservatively <= scroll_limit)
14541 move_it_vertically (&it, amount_to_scroll);
14542 else
14543 {
14544 /* Extra precision for users who set scroll-conservatively
14545 to a large number: make sure the amount we scroll
14546 the window start is never less than amount_to_scroll,
14547 which was computed as distance from window bottom to
14548 point. This matters when lines at window top and lines
14549 below window bottom have different height. */
14550 struct it it1;
14551 void *it1data = NULL;
14552 /* We use a temporary it1 because line_bottom_y can modify
14553 its argument, if it moves one line down; see there. */
14554 int start_y;
14555
14556 SAVE_IT (it1, it, it1data);
14557 start_y = line_bottom_y (&it1);
14558 do {
14559 RESTORE_IT (&it, &it, it1data);
14560 move_it_by_lines (&it, 1);
14561 SAVE_IT (it1, it, it1data);
14562 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14563 }
14564
14565 /* If STARTP is unchanged, move it down another screen line. */
14566 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14567 move_it_by_lines (&it, 1);
14568 startp = it.current.pos;
14569 }
14570 else
14571 {
14572 struct text_pos scroll_margin_pos = startp;
14573
14574 /* See if point is inside the scroll margin at the top of the
14575 window. */
14576 if (this_scroll_margin)
14577 {
14578 start_display (&it, w, startp);
14579 move_it_vertically (&it, this_scroll_margin);
14580 scroll_margin_pos = it.current.pos;
14581 }
14582
14583 if (PT < CHARPOS (scroll_margin_pos))
14584 {
14585 /* Point is in the scroll margin at the top of the window or
14586 above what is displayed in the window. */
14587 int y0, y_to_move;
14588
14589 /* Compute the vertical distance from PT to the scroll
14590 margin position. Move as far as scroll_max allows, or
14591 one screenful, or 10 screen lines, whichever is largest.
14592 Give up if distance is greater than scroll_max. */
14593 SET_TEXT_POS (pos, PT, PT_BYTE);
14594 start_display (&it, w, pos);
14595 y0 = it.current_y;
14596 y_to_move = max (it.last_visible_y,
14597 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14598 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14599 y_to_move, -1,
14600 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14601 dy = it.current_y - y0;
14602 if (dy > scroll_max)
14603 return SCROLLING_FAILED;
14604
14605 /* Compute new window start. */
14606 start_display (&it, w, startp);
14607
14608 if (arg_scroll_conservatively)
14609 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14610 max (scroll_step, temp_scroll_step));
14611 else if (scroll_step || temp_scroll_step)
14612 amount_to_scroll = scroll_max;
14613 else
14614 {
14615 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14616 height = WINDOW_BOX_TEXT_HEIGHT (w);
14617 if (NUMBERP (aggressive))
14618 {
14619 double float_amount = XFLOATINT (aggressive) * height;
14620 amount_to_scroll = float_amount;
14621 if (amount_to_scroll == 0 && float_amount > 0)
14622 amount_to_scroll = 1;
14623 amount_to_scroll -=
14624 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14625 /* Don't let point enter the scroll margin near
14626 bottom of the window. */
14627 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14628 amount_to_scroll = height - 2*this_scroll_margin + dy;
14629 }
14630 }
14631
14632 if (amount_to_scroll <= 0)
14633 return SCROLLING_FAILED;
14634
14635 move_it_vertically_backward (&it, amount_to_scroll);
14636 startp = it.current.pos;
14637 }
14638 }
14639
14640 /* Run window scroll functions. */
14641 startp = run_window_scroll_functions (window, startp);
14642
14643 /* Display the window. Give up if new fonts are loaded, or if point
14644 doesn't appear. */
14645 if (!try_window (window, startp, 0))
14646 rc = SCROLLING_NEED_LARGER_MATRICES;
14647 else if (w->cursor.vpos < 0)
14648 {
14649 clear_glyph_matrix (w->desired_matrix);
14650 rc = SCROLLING_FAILED;
14651 }
14652 else
14653 {
14654 /* Maybe forget recorded base line for line number display. */
14655 if (!just_this_one_p
14656 || current_buffer->clip_changed
14657 || BEG_UNCHANGED < CHARPOS (startp))
14658 w->base_line_number = Qnil;
14659
14660 /* If cursor ends up on a partially visible line,
14661 treat that as being off the bottom of the screen. */
14662 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14663 /* It's possible that the cursor is on the first line of the
14664 buffer, which is partially obscured due to a vscroll
14665 (Bug#7537). In that case, avoid looping forever . */
14666 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14667 {
14668 clear_glyph_matrix (w->desired_matrix);
14669 ++extra_scroll_margin_lines;
14670 goto too_near_end;
14671 }
14672 rc = SCROLLING_SUCCESS;
14673 }
14674
14675 return rc;
14676 }
14677
14678
14679 /* Compute a suitable window start for window W if display of W starts
14680 on a continuation line. Value is non-zero if a new window start
14681 was computed.
14682
14683 The new window start will be computed, based on W's width, starting
14684 from the start of the continued line. It is the start of the
14685 screen line with the minimum distance from the old start W->start. */
14686
14687 static int
14688 compute_window_start_on_continuation_line (struct window *w)
14689 {
14690 struct text_pos pos, start_pos;
14691 int window_start_changed_p = 0;
14692
14693 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14694
14695 /* If window start is on a continuation line... Window start may be
14696 < BEGV in case there's invisible text at the start of the
14697 buffer (M-x rmail, for example). */
14698 if (CHARPOS (start_pos) > BEGV
14699 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14700 {
14701 struct it it;
14702 struct glyph_row *row;
14703
14704 /* Handle the case that the window start is out of range. */
14705 if (CHARPOS (start_pos) < BEGV)
14706 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14707 else if (CHARPOS (start_pos) > ZV)
14708 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14709
14710 /* Find the start of the continued line. This should be fast
14711 because scan_buffer is fast (newline cache). */
14712 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14713 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14714 row, DEFAULT_FACE_ID);
14715 reseat_at_previous_visible_line_start (&it);
14716
14717 /* If the line start is "too far" away from the window start,
14718 say it takes too much time to compute a new window start. */
14719 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14720 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14721 {
14722 int min_distance, distance;
14723
14724 /* Move forward by display lines to find the new window
14725 start. If window width was enlarged, the new start can
14726 be expected to be > the old start. If window width was
14727 decreased, the new window start will be < the old start.
14728 So, we're looking for the display line start with the
14729 minimum distance from the old window start. */
14730 pos = it.current.pos;
14731 min_distance = INFINITY;
14732 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14733 distance < min_distance)
14734 {
14735 min_distance = distance;
14736 pos = it.current.pos;
14737 move_it_by_lines (&it, 1);
14738 }
14739
14740 /* Set the window start there. */
14741 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14742 window_start_changed_p = 1;
14743 }
14744 }
14745
14746 return window_start_changed_p;
14747 }
14748
14749
14750 /* Try cursor movement in case text has not changed in window WINDOW,
14751 with window start STARTP. Value is
14752
14753 CURSOR_MOVEMENT_SUCCESS if successful
14754
14755 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14756
14757 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14758 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14759 we want to scroll as if scroll-step were set to 1. See the code.
14760
14761 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14762 which case we have to abort this redisplay, and adjust matrices
14763 first. */
14764
14765 enum
14766 {
14767 CURSOR_MOVEMENT_SUCCESS,
14768 CURSOR_MOVEMENT_CANNOT_BE_USED,
14769 CURSOR_MOVEMENT_MUST_SCROLL,
14770 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14771 };
14772
14773 static int
14774 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14775 {
14776 struct window *w = XWINDOW (window);
14777 struct frame *f = XFRAME (w->frame);
14778 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14779
14780 #if GLYPH_DEBUG
14781 if (inhibit_try_cursor_movement)
14782 return rc;
14783 #endif
14784
14785 /* Handle case where text has not changed, only point, and it has
14786 not moved off the frame. */
14787 if (/* Point may be in this window. */
14788 PT >= CHARPOS (startp)
14789 /* Selective display hasn't changed. */
14790 && !current_buffer->clip_changed
14791 /* Function force-mode-line-update is used to force a thorough
14792 redisplay. It sets either windows_or_buffers_changed or
14793 update_mode_lines. So don't take a shortcut here for these
14794 cases. */
14795 && !update_mode_lines
14796 && !windows_or_buffers_changed
14797 && !cursor_type_changed
14798 /* Can't use this case if highlighting a region. When a
14799 region exists, cursor movement has to do more than just
14800 set the cursor. */
14801 && !(!NILP (Vtransient_mark_mode)
14802 && !NILP (BVAR (current_buffer, mark_active)))
14803 && NILP (w->region_showing)
14804 && NILP (Vshow_trailing_whitespace)
14805 /* Right after splitting windows, last_point may be nil. */
14806 && INTEGERP (w->last_point)
14807 /* This code is not used for mini-buffer for the sake of the case
14808 of redisplaying to replace an echo area message; since in
14809 that case the mini-buffer contents per se are usually
14810 unchanged. This code is of no real use in the mini-buffer
14811 since the handling of this_line_start_pos, etc., in redisplay
14812 handles the same cases. */
14813 && !EQ (window, minibuf_window)
14814 /* When splitting windows or for new windows, it happens that
14815 redisplay is called with a nil window_end_vpos or one being
14816 larger than the window. This should really be fixed in
14817 window.c. I don't have this on my list, now, so we do
14818 approximately the same as the old redisplay code. --gerd. */
14819 && INTEGERP (w->window_end_vpos)
14820 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14821 && (FRAME_WINDOW_P (f)
14822 || !overlay_arrow_in_current_buffer_p ()))
14823 {
14824 int this_scroll_margin, top_scroll_margin;
14825 struct glyph_row *row = NULL;
14826
14827 #if GLYPH_DEBUG
14828 debug_method_add (w, "cursor movement");
14829 #endif
14830
14831 /* Scroll if point within this distance from the top or bottom
14832 of the window. This is a pixel value. */
14833 if (scroll_margin > 0)
14834 {
14835 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14836 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14837 }
14838 else
14839 this_scroll_margin = 0;
14840
14841 top_scroll_margin = this_scroll_margin;
14842 if (WINDOW_WANTS_HEADER_LINE_P (w))
14843 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14844
14845 /* Start with the row the cursor was displayed during the last
14846 not paused redisplay. Give up if that row is not valid. */
14847 if (w->last_cursor.vpos < 0
14848 || w->last_cursor.vpos >= w->current_matrix->nrows)
14849 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14850 else
14851 {
14852 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14853 if (row->mode_line_p)
14854 ++row;
14855 if (!row->enabled_p)
14856 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14857 }
14858
14859 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14860 {
14861 int scroll_p = 0, must_scroll = 0;
14862 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14863
14864 if (PT > XFASTINT (w->last_point))
14865 {
14866 /* Point has moved forward. */
14867 while (MATRIX_ROW_END_CHARPOS (row) < PT
14868 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14869 {
14870 xassert (row->enabled_p);
14871 ++row;
14872 }
14873
14874 /* If the end position of a row equals the start
14875 position of the next row, and PT is at that position,
14876 we would rather display cursor in the next line. */
14877 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14878 && MATRIX_ROW_END_CHARPOS (row) == PT
14879 && row < w->current_matrix->rows
14880 + w->current_matrix->nrows - 1
14881 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14882 && !cursor_row_p (row))
14883 ++row;
14884
14885 /* If within the scroll margin, scroll. Note that
14886 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14887 the next line would be drawn, and that
14888 this_scroll_margin can be zero. */
14889 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14890 || PT > MATRIX_ROW_END_CHARPOS (row)
14891 /* Line is completely visible last line in window
14892 and PT is to be set in the next line. */
14893 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14894 && PT == MATRIX_ROW_END_CHARPOS (row)
14895 && !row->ends_at_zv_p
14896 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14897 scroll_p = 1;
14898 }
14899 else if (PT < XFASTINT (w->last_point))
14900 {
14901 /* Cursor has to be moved backward. Note that PT >=
14902 CHARPOS (startp) because of the outer if-statement. */
14903 while (!row->mode_line_p
14904 && (MATRIX_ROW_START_CHARPOS (row) > PT
14905 || (MATRIX_ROW_START_CHARPOS (row) == PT
14906 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14907 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14908 row > w->current_matrix->rows
14909 && (row-1)->ends_in_newline_from_string_p))))
14910 && (row->y > top_scroll_margin
14911 || CHARPOS (startp) == BEGV))
14912 {
14913 xassert (row->enabled_p);
14914 --row;
14915 }
14916
14917 /* Consider the following case: Window starts at BEGV,
14918 there is invisible, intangible text at BEGV, so that
14919 display starts at some point START > BEGV. It can
14920 happen that we are called with PT somewhere between
14921 BEGV and START. Try to handle that case. */
14922 if (row < w->current_matrix->rows
14923 || row->mode_line_p)
14924 {
14925 row = w->current_matrix->rows;
14926 if (row->mode_line_p)
14927 ++row;
14928 }
14929
14930 /* Due to newlines in overlay strings, we may have to
14931 skip forward over overlay strings. */
14932 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14933 && MATRIX_ROW_END_CHARPOS (row) == PT
14934 && !cursor_row_p (row))
14935 ++row;
14936
14937 /* If within the scroll margin, scroll. */
14938 if (row->y < top_scroll_margin
14939 && CHARPOS (startp) != BEGV)
14940 scroll_p = 1;
14941 }
14942 else
14943 {
14944 /* Cursor did not move. So don't scroll even if cursor line
14945 is partially visible, as it was so before. */
14946 rc = CURSOR_MOVEMENT_SUCCESS;
14947 }
14948
14949 if (PT < MATRIX_ROW_START_CHARPOS (row)
14950 || PT > MATRIX_ROW_END_CHARPOS (row))
14951 {
14952 /* if PT is not in the glyph row, give up. */
14953 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14954 must_scroll = 1;
14955 }
14956 else if (rc != CURSOR_MOVEMENT_SUCCESS
14957 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14958 {
14959 struct glyph_row *row1;
14960
14961 /* If rows are bidi-reordered and point moved, back up
14962 until we find a row that does not belong to a
14963 continuation line. This is because we must consider
14964 all rows of a continued line as candidates for the
14965 new cursor positioning, since row start and end
14966 positions change non-linearly with vertical position
14967 in such rows. */
14968 /* FIXME: Revisit this when glyph ``spilling'' in
14969 continuation lines' rows is implemented for
14970 bidi-reordered rows. */
14971 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14972 MATRIX_ROW_CONTINUATION_LINE_P (row);
14973 --row)
14974 {
14975 /* If we hit the beginning of the displayed portion
14976 without finding the first row of a continued
14977 line, give up. */
14978 if (row <= row1)
14979 {
14980 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14981 break;
14982 }
14983 xassert (row->enabled_p);
14984 }
14985 }
14986 if (must_scroll)
14987 ;
14988 else if (rc != CURSOR_MOVEMENT_SUCCESS
14989 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14990 /* Make sure this isn't a header line by any chance, since
14991 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
14992 && !row->mode_line_p
14993 && make_cursor_line_fully_visible_p)
14994 {
14995 if (PT == MATRIX_ROW_END_CHARPOS (row)
14996 && !row->ends_at_zv_p
14997 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14998 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14999 else if (row->height > window_box_height (w))
15000 {
15001 /* If we end up in a partially visible line, let's
15002 make it fully visible, except when it's taller
15003 than the window, in which case we can't do much
15004 about it. */
15005 *scroll_step = 1;
15006 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15007 }
15008 else
15009 {
15010 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15011 if (!cursor_row_fully_visible_p (w, 0, 1))
15012 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15013 else
15014 rc = CURSOR_MOVEMENT_SUCCESS;
15015 }
15016 }
15017 else if (scroll_p)
15018 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15019 else if (rc != CURSOR_MOVEMENT_SUCCESS
15020 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15021 {
15022 /* With bidi-reordered rows, there could be more than
15023 one candidate row whose start and end positions
15024 occlude point. We need to let set_cursor_from_row
15025 find the best candidate. */
15026 /* FIXME: Revisit this when glyph ``spilling'' in
15027 continuation lines' rows is implemented for
15028 bidi-reordered rows. */
15029 int rv = 0;
15030
15031 do
15032 {
15033 int at_zv_p = 0, exact_match_p = 0;
15034
15035 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15036 && PT <= MATRIX_ROW_END_CHARPOS (row)
15037 && cursor_row_p (row))
15038 rv |= set_cursor_from_row (w, row, w->current_matrix,
15039 0, 0, 0, 0);
15040 /* As soon as we've found the exact match for point,
15041 or the first suitable row whose ends_at_zv_p flag
15042 is set, we are done. */
15043 at_zv_p =
15044 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15045 if (rv && !at_zv_p
15046 && w->cursor.hpos >= 0
15047 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15048 w->cursor.vpos))
15049 {
15050 struct glyph_row *candidate =
15051 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15052 struct glyph *g =
15053 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15054 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
15055
15056 exact_match_p =
15057 (BUFFERP (g->object) && g->charpos == PT)
15058 || (INTEGERP (g->object)
15059 && (g->charpos == PT
15060 || (g->charpos == 0 && endpos - 1 == PT)));
15061 }
15062 if (rv && (at_zv_p || exact_match_p))
15063 {
15064 rc = CURSOR_MOVEMENT_SUCCESS;
15065 break;
15066 }
15067 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15068 break;
15069 ++row;
15070 }
15071 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15072 || row->continued_p)
15073 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15074 || (MATRIX_ROW_START_CHARPOS (row) == PT
15075 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15076 /* If we didn't find any candidate rows, or exited the
15077 loop before all the candidates were examined, signal
15078 to the caller that this method failed. */
15079 if (rc != CURSOR_MOVEMENT_SUCCESS
15080 && !(rv
15081 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15082 && !row->continued_p))
15083 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15084 else if (rv)
15085 rc = CURSOR_MOVEMENT_SUCCESS;
15086 }
15087 else
15088 {
15089 do
15090 {
15091 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15092 {
15093 rc = CURSOR_MOVEMENT_SUCCESS;
15094 break;
15095 }
15096 ++row;
15097 }
15098 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15099 && MATRIX_ROW_START_CHARPOS (row) == PT
15100 && cursor_row_p (row));
15101 }
15102 }
15103 }
15104
15105 return rc;
15106 }
15107
15108 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15109 static
15110 #endif
15111 void
15112 set_vertical_scroll_bar (struct window *w)
15113 {
15114 EMACS_INT start, end, whole;
15115
15116 /* Calculate the start and end positions for the current window.
15117 At some point, it would be nice to choose between scrollbars
15118 which reflect the whole buffer size, with special markers
15119 indicating narrowing, and scrollbars which reflect only the
15120 visible region.
15121
15122 Note that mini-buffers sometimes aren't displaying any text. */
15123 if (!MINI_WINDOW_P (w)
15124 || (w == XWINDOW (minibuf_window)
15125 && NILP (echo_area_buffer[0])))
15126 {
15127 struct buffer *buf = XBUFFER (w->buffer);
15128 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15129 start = marker_position (w->start) - BUF_BEGV (buf);
15130 /* I don't think this is guaranteed to be right. For the
15131 moment, we'll pretend it is. */
15132 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15133
15134 if (end < start)
15135 end = start;
15136 if (whole < (end - start))
15137 whole = end - start;
15138 }
15139 else
15140 start = end = whole = 0;
15141
15142 /* Indicate what this scroll bar ought to be displaying now. */
15143 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15144 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15145 (w, end - start, whole, start);
15146 }
15147
15148
15149 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15150 selected_window is redisplayed.
15151
15152 We can return without actually redisplaying the window if
15153 fonts_changed_p is nonzero. In that case, redisplay_internal will
15154 retry. */
15155
15156 static void
15157 redisplay_window (Lisp_Object window, int just_this_one_p)
15158 {
15159 struct window *w = XWINDOW (window);
15160 struct frame *f = XFRAME (w->frame);
15161 struct buffer *buffer = XBUFFER (w->buffer);
15162 struct buffer *old = current_buffer;
15163 struct text_pos lpoint, opoint, startp;
15164 int update_mode_line;
15165 int tem;
15166 struct it it;
15167 /* Record it now because it's overwritten. */
15168 int current_matrix_up_to_date_p = 0;
15169 int used_current_matrix_p = 0;
15170 /* This is less strict than current_matrix_up_to_date_p.
15171 It indicates that the buffer contents and narrowing are unchanged. */
15172 int buffer_unchanged_p = 0;
15173 int temp_scroll_step = 0;
15174 int count = SPECPDL_INDEX ();
15175 int rc;
15176 int centering_position = -1;
15177 int last_line_misfit = 0;
15178 EMACS_INT beg_unchanged, end_unchanged;
15179
15180 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15181 opoint = lpoint;
15182
15183 /* W must be a leaf window here. */
15184 xassert (!NILP (w->buffer));
15185 #if GLYPH_DEBUG
15186 *w->desired_matrix->method = 0;
15187 #endif
15188
15189 restart:
15190 reconsider_clip_changes (w, buffer);
15191
15192 /* Has the mode line to be updated? */
15193 update_mode_line = (!NILP (w->update_mode_line)
15194 || update_mode_lines
15195 || buffer->clip_changed
15196 || buffer->prevent_redisplay_optimizations_p);
15197
15198 if (MINI_WINDOW_P (w))
15199 {
15200 if (w == XWINDOW (echo_area_window)
15201 && !NILP (echo_area_buffer[0]))
15202 {
15203 if (update_mode_line)
15204 /* We may have to update a tty frame's menu bar or a
15205 tool-bar. Example `M-x C-h C-h C-g'. */
15206 goto finish_menu_bars;
15207 else
15208 /* We've already displayed the echo area glyphs in this window. */
15209 goto finish_scroll_bars;
15210 }
15211 else if ((w != XWINDOW (minibuf_window)
15212 || minibuf_level == 0)
15213 /* When buffer is nonempty, redisplay window normally. */
15214 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15215 /* Quail displays non-mini buffers in minibuffer window.
15216 In that case, redisplay the window normally. */
15217 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15218 {
15219 /* W is a mini-buffer window, but it's not active, so clear
15220 it. */
15221 int yb = window_text_bottom_y (w);
15222 struct glyph_row *row;
15223 int y;
15224
15225 for (y = 0, row = w->desired_matrix->rows;
15226 y < yb;
15227 y += row->height, ++row)
15228 blank_row (w, row, y);
15229 goto finish_scroll_bars;
15230 }
15231
15232 clear_glyph_matrix (w->desired_matrix);
15233 }
15234
15235 /* Otherwise set up data on this window; select its buffer and point
15236 value. */
15237 /* Really select the buffer, for the sake of buffer-local
15238 variables. */
15239 set_buffer_internal_1 (XBUFFER (w->buffer));
15240
15241 current_matrix_up_to_date_p
15242 = (!NILP (w->window_end_valid)
15243 && !current_buffer->clip_changed
15244 && !current_buffer->prevent_redisplay_optimizations_p
15245 && XFASTINT (w->last_modified) >= MODIFF
15246 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15247
15248 /* Run the window-bottom-change-functions
15249 if it is possible that the text on the screen has changed
15250 (either due to modification of the text, or any other reason). */
15251 if (!current_matrix_up_to_date_p
15252 && !NILP (Vwindow_text_change_functions))
15253 {
15254 safe_run_hooks (Qwindow_text_change_functions);
15255 goto restart;
15256 }
15257
15258 beg_unchanged = BEG_UNCHANGED;
15259 end_unchanged = END_UNCHANGED;
15260
15261 SET_TEXT_POS (opoint, PT, PT_BYTE);
15262
15263 specbind (Qinhibit_point_motion_hooks, Qt);
15264
15265 buffer_unchanged_p
15266 = (!NILP (w->window_end_valid)
15267 && !current_buffer->clip_changed
15268 && XFASTINT (w->last_modified) >= MODIFF
15269 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15270
15271 /* When windows_or_buffers_changed is non-zero, we can't rely on
15272 the window end being valid, so set it to nil there. */
15273 if (windows_or_buffers_changed)
15274 {
15275 /* If window starts on a continuation line, maybe adjust the
15276 window start in case the window's width changed. */
15277 if (XMARKER (w->start)->buffer == current_buffer)
15278 compute_window_start_on_continuation_line (w);
15279
15280 w->window_end_valid = Qnil;
15281 }
15282
15283 /* Some sanity checks. */
15284 CHECK_WINDOW_END (w);
15285 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15286 abort ();
15287 if (BYTEPOS (opoint) < CHARPOS (opoint))
15288 abort ();
15289
15290 /* If %c is in mode line, update it if needed. */
15291 if (!NILP (w->column_number_displayed)
15292 /* This alternative quickly identifies a common case
15293 where no change is needed. */
15294 && !(PT == XFASTINT (w->last_point)
15295 && XFASTINT (w->last_modified) >= MODIFF
15296 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15297 && (XFASTINT (w->column_number_displayed) != current_column ()))
15298 update_mode_line = 1;
15299
15300 /* Count number of windows showing the selected buffer. An indirect
15301 buffer counts as its base buffer. */
15302 if (!just_this_one_p)
15303 {
15304 struct buffer *current_base, *window_base;
15305 current_base = current_buffer;
15306 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15307 if (current_base->base_buffer)
15308 current_base = current_base->base_buffer;
15309 if (window_base->base_buffer)
15310 window_base = window_base->base_buffer;
15311 if (current_base == window_base)
15312 buffer_shared++;
15313 }
15314
15315 /* Point refers normally to the selected window. For any other
15316 window, set up appropriate value. */
15317 if (!EQ (window, selected_window))
15318 {
15319 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15320 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15321 if (new_pt < BEGV)
15322 {
15323 new_pt = BEGV;
15324 new_pt_byte = BEGV_BYTE;
15325 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15326 }
15327 else if (new_pt > (ZV - 1))
15328 {
15329 new_pt = ZV;
15330 new_pt_byte = ZV_BYTE;
15331 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15332 }
15333
15334 /* We don't use SET_PT so that the point-motion hooks don't run. */
15335 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15336 }
15337
15338 /* If any of the character widths specified in the display table
15339 have changed, invalidate the width run cache. It's true that
15340 this may be a bit late to catch such changes, but the rest of
15341 redisplay goes (non-fatally) haywire when the display table is
15342 changed, so why should we worry about doing any better? */
15343 if (current_buffer->width_run_cache)
15344 {
15345 struct Lisp_Char_Table *disptab = buffer_display_table ();
15346
15347 if (! disptab_matches_widthtab (disptab,
15348 XVECTOR (BVAR (current_buffer, width_table))))
15349 {
15350 invalidate_region_cache (current_buffer,
15351 current_buffer->width_run_cache,
15352 BEG, Z);
15353 recompute_width_table (current_buffer, disptab);
15354 }
15355 }
15356
15357 /* If window-start is screwed up, choose a new one. */
15358 if (XMARKER (w->start)->buffer != current_buffer)
15359 goto recenter;
15360
15361 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15362
15363 /* If someone specified a new starting point but did not insist,
15364 check whether it can be used. */
15365 if (!NILP (w->optional_new_start)
15366 && CHARPOS (startp) >= BEGV
15367 && CHARPOS (startp) <= ZV)
15368 {
15369 w->optional_new_start = Qnil;
15370 start_display (&it, w, startp);
15371 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15372 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15373 if (IT_CHARPOS (it) == PT)
15374 w->force_start = Qt;
15375 /* IT may overshoot PT if text at PT is invisible. */
15376 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15377 w->force_start = Qt;
15378 }
15379
15380 force_start:
15381
15382 /* Handle case where place to start displaying has been specified,
15383 unless the specified location is outside the accessible range. */
15384 if (!NILP (w->force_start)
15385 || w->frozen_window_start_p)
15386 {
15387 /* We set this later on if we have to adjust point. */
15388 int new_vpos = -1;
15389
15390 w->force_start = Qnil;
15391 w->vscroll = 0;
15392 w->window_end_valid = Qnil;
15393
15394 /* Forget any recorded base line for line number display. */
15395 if (!buffer_unchanged_p)
15396 w->base_line_number = Qnil;
15397
15398 /* Redisplay the mode line. Select the buffer properly for that.
15399 Also, run the hook window-scroll-functions
15400 because we have scrolled. */
15401 /* Note, we do this after clearing force_start because
15402 if there's an error, it is better to forget about force_start
15403 than to get into an infinite loop calling the hook functions
15404 and having them get more errors. */
15405 if (!update_mode_line
15406 || ! NILP (Vwindow_scroll_functions))
15407 {
15408 update_mode_line = 1;
15409 w->update_mode_line = Qt;
15410 startp = run_window_scroll_functions (window, startp);
15411 }
15412
15413 w->last_modified = make_number (0);
15414 w->last_overlay_modified = make_number (0);
15415 if (CHARPOS (startp) < BEGV)
15416 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15417 else if (CHARPOS (startp) > ZV)
15418 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15419
15420 /* Redisplay, then check if cursor has been set during the
15421 redisplay. Give up if new fonts were loaded. */
15422 /* We used to issue a CHECK_MARGINS argument to try_window here,
15423 but this causes scrolling to fail when point begins inside
15424 the scroll margin (bug#148) -- cyd */
15425 if (!try_window (window, startp, 0))
15426 {
15427 w->force_start = Qt;
15428 clear_glyph_matrix (w->desired_matrix);
15429 goto need_larger_matrices;
15430 }
15431
15432 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15433 {
15434 /* If point does not appear, try to move point so it does
15435 appear. The desired matrix has been built above, so we
15436 can use it here. */
15437 new_vpos = window_box_height (w) / 2;
15438 }
15439
15440 if (!cursor_row_fully_visible_p (w, 0, 0))
15441 {
15442 /* Point does appear, but on a line partly visible at end of window.
15443 Move it back to a fully-visible line. */
15444 new_vpos = window_box_height (w);
15445 }
15446
15447 /* If we need to move point for either of the above reasons,
15448 now actually do it. */
15449 if (new_vpos >= 0)
15450 {
15451 struct glyph_row *row;
15452
15453 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15454 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15455 ++row;
15456
15457 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15458 MATRIX_ROW_START_BYTEPOS (row));
15459
15460 if (w != XWINDOW (selected_window))
15461 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15462 else if (current_buffer == old)
15463 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15464
15465 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15466
15467 /* If we are highlighting the region, then we just changed
15468 the region, so redisplay to show it. */
15469 if (!NILP (Vtransient_mark_mode)
15470 && !NILP (BVAR (current_buffer, mark_active)))
15471 {
15472 clear_glyph_matrix (w->desired_matrix);
15473 if (!try_window (window, startp, 0))
15474 goto need_larger_matrices;
15475 }
15476 }
15477
15478 #if GLYPH_DEBUG
15479 debug_method_add (w, "forced window start");
15480 #endif
15481 goto done;
15482 }
15483
15484 /* Handle case where text has not changed, only point, and it has
15485 not moved off the frame, and we are not retrying after hscroll.
15486 (current_matrix_up_to_date_p is nonzero when retrying.) */
15487 if (current_matrix_up_to_date_p
15488 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15489 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15490 {
15491 switch (rc)
15492 {
15493 case CURSOR_MOVEMENT_SUCCESS:
15494 used_current_matrix_p = 1;
15495 goto done;
15496
15497 case CURSOR_MOVEMENT_MUST_SCROLL:
15498 goto try_to_scroll;
15499
15500 default:
15501 abort ();
15502 }
15503 }
15504 /* If current starting point was originally the beginning of a line
15505 but no longer is, find a new starting point. */
15506 else if (!NILP (w->start_at_line_beg)
15507 && !(CHARPOS (startp) <= BEGV
15508 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15509 {
15510 #if GLYPH_DEBUG
15511 debug_method_add (w, "recenter 1");
15512 #endif
15513 goto recenter;
15514 }
15515
15516 /* Try scrolling with try_window_id. Value is > 0 if update has
15517 been done, it is -1 if we know that the same window start will
15518 not work. It is 0 if unsuccessful for some other reason. */
15519 else if ((tem = try_window_id (w)) != 0)
15520 {
15521 #if GLYPH_DEBUG
15522 debug_method_add (w, "try_window_id %d", tem);
15523 #endif
15524
15525 if (fonts_changed_p)
15526 goto need_larger_matrices;
15527 if (tem > 0)
15528 goto done;
15529
15530 /* Otherwise try_window_id has returned -1 which means that we
15531 don't want the alternative below this comment to execute. */
15532 }
15533 else if (CHARPOS (startp) >= BEGV
15534 && CHARPOS (startp) <= ZV
15535 && PT >= CHARPOS (startp)
15536 && (CHARPOS (startp) < ZV
15537 /* Avoid starting at end of buffer. */
15538 || CHARPOS (startp) == BEGV
15539 || (XFASTINT (w->last_modified) >= MODIFF
15540 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15541 {
15542 int d1, d2, d3, d4, d5, d6;
15543
15544 /* If first window line is a continuation line, and window start
15545 is inside the modified region, but the first change is before
15546 current window start, we must select a new window start.
15547
15548 However, if this is the result of a down-mouse event (e.g. by
15549 extending the mouse-drag-overlay), we don't want to select a
15550 new window start, since that would change the position under
15551 the mouse, resulting in an unwanted mouse-movement rather
15552 than a simple mouse-click. */
15553 if (NILP (w->start_at_line_beg)
15554 && NILP (do_mouse_tracking)
15555 && CHARPOS (startp) > BEGV
15556 && CHARPOS (startp) > BEG + beg_unchanged
15557 && CHARPOS (startp) <= Z - end_unchanged
15558 /* Even if w->start_at_line_beg is nil, a new window may
15559 start at a line_beg, since that's how set_buffer_window
15560 sets it. So, we need to check the return value of
15561 compute_window_start_on_continuation_line. (See also
15562 bug#197). */
15563 && XMARKER (w->start)->buffer == current_buffer
15564 && compute_window_start_on_continuation_line (w)
15565 /* It doesn't make sense to force the window start like we
15566 do at label force_start if it is already known that point
15567 will not be visible in the resulting window, because
15568 doing so will move point from its correct position
15569 instead of scrolling the window to bring point into view.
15570 See bug#9324. */
15571 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15572 {
15573 w->force_start = Qt;
15574 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15575 goto force_start;
15576 }
15577
15578 #if GLYPH_DEBUG
15579 debug_method_add (w, "same window start");
15580 #endif
15581
15582 /* Try to redisplay starting at same place as before.
15583 If point has not moved off frame, accept the results. */
15584 if (!current_matrix_up_to_date_p
15585 /* Don't use try_window_reusing_current_matrix in this case
15586 because a window scroll function can have changed the
15587 buffer. */
15588 || !NILP (Vwindow_scroll_functions)
15589 || MINI_WINDOW_P (w)
15590 || !(used_current_matrix_p
15591 = try_window_reusing_current_matrix (w)))
15592 {
15593 IF_DEBUG (debug_method_add (w, "1"));
15594 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15595 /* -1 means we need to scroll.
15596 0 means we need new matrices, but fonts_changed_p
15597 is set in that case, so we will detect it below. */
15598 goto try_to_scroll;
15599 }
15600
15601 if (fonts_changed_p)
15602 goto need_larger_matrices;
15603
15604 if (w->cursor.vpos >= 0)
15605 {
15606 if (!just_this_one_p
15607 || current_buffer->clip_changed
15608 || BEG_UNCHANGED < CHARPOS (startp))
15609 /* Forget any recorded base line for line number display. */
15610 w->base_line_number = Qnil;
15611
15612 if (!cursor_row_fully_visible_p (w, 1, 0))
15613 {
15614 clear_glyph_matrix (w->desired_matrix);
15615 last_line_misfit = 1;
15616 }
15617 /* Drop through and scroll. */
15618 else
15619 goto done;
15620 }
15621 else
15622 clear_glyph_matrix (w->desired_matrix);
15623 }
15624
15625 try_to_scroll:
15626
15627 w->last_modified = make_number (0);
15628 w->last_overlay_modified = make_number (0);
15629
15630 /* Redisplay the mode line. Select the buffer properly for that. */
15631 if (!update_mode_line)
15632 {
15633 update_mode_line = 1;
15634 w->update_mode_line = Qt;
15635 }
15636
15637 /* Try to scroll by specified few lines. */
15638 if ((scroll_conservatively
15639 || emacs_scroll_step
15640 || temp_scroll_step
15641 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15642 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15643 && CHARPOS (startp) >= BEGV
15644 && CHARPOS (startp) <= ZV)
15645 {
15646 /* The function returns -1 if new fonts were loaded, 1 if
15647 successful, 0 if not successful. */
15648 int ss = try_scrolling (window, just_this_one_p,
15649 scroll_conservatively,
15650 emacs_scroll_step,
15651 temp_scroll_step, last_line_misfit);
15652 switch (ss)
15653 {
15654 case SCROLLING_SUCCESS:
15655 goto done;
15656
15657 case SCROLLING_NEED_LARGER_MATRICES:
15658 goto need_larger_matrices;
15659
15660 case SCROLLING_FAILED:
15661 break;
15662
15663 default:
15664 abort ();
15665 }
15666 }
15667
15668 /* Finally, just choose a place to start which positions point
15669 according to user preferences. */
15670
15671 recenter:
15672
15673 #if GLYPH_DEBUG
15674 debug_method_add (w, "recenter");
15675 #endif
15676
15677 /* w->vscroll = 0; */
15678
15679 /* Forget any previously recorded base line for line number display. */
15680 if (!buffer_unchanged_p)
15681 w->base_line_number = Qnil;
15682
15683 /* Determine the window start relative to point. */
15684 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15685 it.current_y = it.last_visible_y;
15686 if (centering_position < 0)
15687 {
15688 int margin =
15689 scroll_margin > 0
15690 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15691 : 0;
15692 EMACS_INT margin_pos = CHARPOS (startp);
15693 Lisp_Object aggressive;
15694 int scrolling_up;
15695
15696 /* If there is a scroll margin at the top of the window, find
15697 its character position. */
15698 if (margin
15699 /* Cannot call start_display if startp is not in the
15700 accessible region of the buffer. This can happen when we
15701 have just switched to a different buffer and/or changed
15702 its restriction. In that case, startp is initialized to
15703 the character position 1 (BEGV) because we did not yet
15704 have chance to display the buffer even once. */
15705 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15706 {
15707 struct it it1;
15708 void *it1data = NULL;
15709
15710 SAVE_IT (it1, it, it1data);
15711 start_display (&it1, w, startp);
15712 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15713 margin_pos = IT_CHARPOS (it1);
15714 RESTORE_IT (&it, &it, it1data);
15715 }
15716 scrolling_up = PT > margin_pos;
15717 aggressive =
15718 scrolling_up
15719 ? BVAR (current_buffer, scroll_up_aggressively)
15720 : BVAR (current_buffer, scroll_down_aggressively);
15721
15722 if (!MINI_WINDOW_P (w)
15723 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15724 {
15725 int pt_offset = 0;
15726
15727 /* Setting scroll-conservatively overrides
15728 scroll-*-aggressively. */
15729 if (!scroll_conservatively && NUMBERP (aggressive))
15730 {
15731 double float_amount = XFLOATINT (aggressive);
15732
15733 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15734 if (pt_offset == 0 && float_amount > 0)
15735 pt_offset = 1;
15736 if (pt_offset && margin > 0)
15737 margin -= 1;
15738 }
15739 /* Compute how much to move the window start backward from
15740 point so that point will be displayed where the user
15741 wants it. */
15742 if (scrolling_up)
15743 {
15744 centering_position = it.last_visible_y;
15745 if (pt_offset)
15746 centering_position -= pt_offset;
15747 centering_position -=
15748 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15749 + WINDOW_HEADER_LINE_HEIGHT (w);
15750 /* Don't let point enter the scroll margin near top of
15751 the window. */
15752 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15753 centering_position = margin * FRAME_LINE_HEIGHT (f);
15754 }
15755 else
15756 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15757 }
15758 else
15759 /* Set the window start half the height of the window backward
15760 from point. */
15761 centering_position = window_box_height (w) / 2;
15762 }
15763 move_it_vertically_backward (&it, centering_position);
15764
15765 xassert (IT_CHARPOS (it) >= BEGV);
15766
15767 /* The function move_it_vertically_backward may move over more
15768 than the specified y-distance. If it->w is small, e.g. a
15769 mini-buffer window, we may end up in front of the window's
15770 display area. Start displaying at the start of the line
15771 containing PT in this case. */
15772 if (it.current_y <= 0)
15773 {
15774 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15775 move_it_vertically_backward (&it, 0);
15776 it.current_y = 0;
15777 }
15778
15779 it.current_x = it.hpos = 0;
15780
15781 /* Set the window start position here explicitly, to avoid an
15782 infinite loop in case the functions in window-scroll-functions
15783 get errors. */
15784 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15785
15786 /* Run scroll hooks. */
15787 startp = run_window_scroll_functions (window, it.current.pos);
15788
15789 /* Redisplay the window. */
15790 if (!current_matrix_up_to_date_p
15791 || windows_or_buffers_changed
15792 || cursor_type_changed
15793 /* Don't use try_window_reusing_current_matrix in this case
15794 because it can have changed the buffer. */
15795 || !NILP (Vwindow_scroll_functions)
15796 || !just_this_one_p
15797 || MINI_WINDOW_P (w)
15798 || !(used_current_matrix_p
15799 = try_window_reusing_current_matrix (w)))
15800 try_window (window, startp, 0);
15801
15802 /* If new fonts have been loaded (due to fontsets), give up. We
15803 have to start a new redisplay since we need to re-adjust glyph
15804 matrices. */
15805 if (fonts_changed_p)
15806 goto need_larger_matrices;
15807
15808 /* If cursor did not appear assume that the middle of the window is
15809 in the first line of the window. Do it again with the next line.
15810 (Imagine a window of height 100, displaying two lines of height
15811 60. Moving back 50 from it->last_visible_y will end in the first
15812 line.) */
15813 if (w->cursor.vpos < 0)
15814 {
15815 if (!NILP (w->window_end_valid)
15816 && PT >= Z - XFASTINT (w->window_end_pos))
15817 {
15818 clear_glyph_matrix (w->desired_matrix);
15819 move_it_by_lines (&it, 1);
15820 try_window (window, it.current.pos, 0);
15821 }
15822 else if (PT < IT_CHARPOS (it))
15823 {
15824 clear_glyph_matrix (w->desired_matrix);
15825 move_it_by_lines (&it, -1);
15826 try_window (window, it.current.pos, 0);
15827 }
15828 else
15829 {
15830 /* Not much we can do about it. */
15831 }
15832 }
15833
15834 /* Consider the following case: Window starts at BEGV, there is
15835 invisible, intangible text at BEGV, so that display starts at
15836 some point START > BEGV. It can happen that we are called with
15837 PT somewhere between BEGV and START. Try to handle that case. */
15838 if (w->cursor.vpos < 0)
15839 {
15840 struct glyph_row *row = w->current_matrix->rows;
15841 if (row->mode_line_p)
15842 ++row;
15843 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15844 }
15845
15846 if (!cursor_row_fully_visible_p (w, 0, 0))
15847 {
15848 /* If vscroll is enabled, disable it and try again. */
15849 if (w->vscroll)
15850 {
15851 w->vscroll = 0;
15852 clear_glyph_matrix (w->desired_matrix);
15853 goto recenter;
15854 }
15855
15856 /* Users who set scroll-conservatively to a large number want
15857 point just above/below the scroll margin. If we ended up
15858 with point's row partially visible, move the window start to
15859 make that row fully visible and out of the margin. */
15860 if (scroll_conservatively > SCROLL_LIMIT)
15861 {
15862 int margin =
15863 scroll_margin > 0
15864 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15865 : 0;
15866 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15867
15868 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15869 clear_glyph_matrix (w->desired_matrix);
15870 if (1 == try_window (window, it.current.pos,
15871 TRY_WINDOW_CHECK_MARGINS))
15872 goto done;
15873 }
15874
15875 /* If centering point failed to make the whole line visible,
15876 put point at the top instead. That has to make the whole line
15877 visible, if it can be done. */
15878 if (centering_position == 0)
15879 goto done;
15880
15881 clear_glyph_matrix (w->desired_matrix);
15882 centering_position = 0;
15883 goto recenter;
15884 }
15885
15886 done:
15887
15888 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15889 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15890 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15891 ? Qt : Qnil);
15892
15893 /* Display the mode line, if we must. */
15894 if ((update_mode_line
15895 /* If window not full width, must redo its mode line
15896 if (a) the window to its side is being redone and
15897 (b) we do a frame-based redisplay. This is a consequence
15898 of how inverted lines are drawn in frame-based redisplay. */
15899 || (!just_this_one_p
15900 && !FRAME_WINDOW_P (f)
15901 && !WINDOW_FULL_WIDTH_P (w))
15902 /* Line number to display. */
15903 || INTEGERP (w->base_line_pos)
15904 /* Column number is displayed and different from the one displayed. */
15905 || (!NILP (w->column_number_displayed)
15906 && (XFASTINT (w->column_number_displayed) != current_column ())))
15907 /* This means that the window has a mode line. */
15908 && (WINDOW_WANTS_MODELINE_P (w)
15909 || WINDOW_WANTS_HEADER_LINE_P (w)))
15910 {
15911 display_mode_lines (w);
15912
15913 /* If mode line height has changed, arrange for a thorough
15914 immediate redisplay using the correct mode line height. */
15915 if (WINDOW_WANTS_MODELINE_P (w)
15916 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15917 {
15918 fonts_changed_p = 1;
15919 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15920 = DESIRED_MODE_LINE_HEIGHT (w);
15921 }
15922
15923 /* If header line height has changed, arrange for a thorough
15924 immediate redisplay using the correct header line height. */
15925 if (WINDOW_WANTS_HEADER_LINE_P (w)
15926 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15927 {
15928 fonts_changed_p = 1;
15929 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15930 = DESIRED_HEADER_LINE_HEIGHT (w);
15931 }
15932
15933 if (fonts_changed_p)
15934 goto need_larger_matrices;
15935 }
15936
15937 if (!line_number_displayed
15938 && !BUFFERP (w->base_line_pos))
15939 {
15940 w->base_line_pos = Qnil;
15941 w->base_line_number = Qnil;
15942 }
15943
15944 finish_menu_bars:
15945
15946 /* When we reach a frame's selected window, redo the frame's menu bar. */
15947 if (update_mode_line
15948 && EQ (FRAME_SELECTED_WINDOW (f), window))
15949 {
15950 int redisplay_menu_p = 0;
15951
15952 if (FRAME_WINDOW_P (f))
15953 {
15954 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15955 || defined (HAVE_NS) || defined (USE_GTK)
15956 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15957 #else
15958 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15959 #endif
15960 }
15961 else
15962 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15963
15964 if (redisplay_menu_p)
15965 display_menu_bar (w);
15966
15967 #ifdef HAVE_WINDOW_SYSTEM
15968 if (FRAME_WINDOW_P (f))
15969 {
15970 #if defined (USE_GTK) || defined (HAVE_NS)
15971 if (FRAME_EXTERNAL_TOOL_BAR (f))
15972 redisplay_tool_bar (f);
15973 #else
15974 if (WINDOWP (f->tool_bar_window)
15975 && (FRAME_TOOL_BAR_LINES (f) > 0
15976 || !NILP (Vauto_resize_tool_bars))
15977 && redisplay_tool_bar (f))
15978 ignore_mouse_drag_p = 1;
15979 #endif
15980 }
15981 #endif
15982 }
15983
15984 #ifdef HAVE_WINDOW_SYSTEM
15985 if (FRAME_WINDOW_P (f)
15986 && update_window_fringes (w, (just_this_one_p
15987 || (!used_current_matrix_p && !overlay_arrow_seen)
15988 || w->pseudo_window_p)))
15989 {
15990 update_begin (f);
15991 BLOCK_INPUT;
15992 if (draw_window_fringes (w, 1))
15993 x_draw_vertical_border (w);
15994 UNBLOCK_INPUT;
15995 update_end (f);
15996 }
15997 #endif /* HAVE_WINDOW_SYSTEM */
15998
15999 /* We go to this label, with fonts_changed_p nonzero,
16000 if it is necessary to try again using larger glyph matrices.
16001 We have to redeem the scroll bar even in this case,
16002 because the loop in redisplay_internal expects that. */
16003 need_larger_matrices:
16004 ;
16005 finish_scroll_bars:
16006
16007 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16008 {
16009 /* Set the thumb's position and size. */
16010 set_vertical_scroll_bar (w);
16011
16012 /* Note that we actually used the scroll bar attached to this
16013 window, so it shouldn't be deleted at the end of redisplay. */
16014 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16015 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16016 }
16017
16018 /* Restore current_buffer and value of point in it. The window
16019 update may have changed the buffer, so first make sure `opoint'
16020 is still valid (Bug#6177). */
16021 if (CHARPOS (opoint) < BEGV)
16022 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16023 else if (CHARPOS (opoint) > ZV)
16024 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16025 else
16026 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16027
16028 set_buffer_internal_1 (old);
16029 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16030 shorter. This can be caused by log truncation in *Messages*. */
16031 if (CHARPOS (lpoint) <= ZV)
16032 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16033
16034 unbind_to (count, Qnil);
16035 }
16036
16037
16038 /* Build the complete desired matrix of WINDOW with a window start
16039 buffer position POS.
16040
16041 Value is 1 if successful. It is zero if fonts were loaded during
16042 redisplay which makes re-adjusting glyph matrices necessary, and -1
16043 if point would appear in the scroll margins.
16044 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16045 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16046 set in FLAGS.) */
16047
16048 int
16049 try_window (Lisp_Object window, struct text_pos pos, int flags)
16050 {
16051 struct window *w = XWINDOW (window);
16052 struct it it;
16053 struct glyph_row *last_text_row = NULL;
16054 struct frame *f = XFRAME (w->frame);
16055
16056 /* Make POS the new window start. */
16057 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16058
16059 /* Mark cursor position as unknown. No overlay arrow seen. */
16060 w->cursor.vpos = -1;
16061 overlay_arrow_seen = 0;
16062
16063 /* Initialize iterator and info to start at POS. */
16064 start_display (&it, w, pos);
16065
16066 /* Display all lines of W. */
16067 while (it.current_y < it.last_visible_y)
16068 {
16069 if (display_line (&it))
16070 last_text_row = it.glyph_row - 1;
16071 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16072 return 0;
16073 }
16074
16075 /* Don't let the cursor end in the scroll margins. */
16076 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16077 && !MINI_WINDOW_P (w))
16078 {
16079 int this_scroll_margin;
16080
16081 if (scroll_margin > 0)
16082 {
16083 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16084 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16085 }
16086 else
16087 this_scroll_margin = 0;
16088
16089 if ((w->cursor.y >= 0 /* not vscrolled */
16090 && w->cursor.y < this_scroll_margin
16091 && CHARPOS (pos) > BEGV
16092 && IT_CHARPOS (it) < ZV)
16093 /* rms: considering make_cursor_line_fully_visible_p here
16094 seems to give wrong results. We don't want to recenter
16095 when the last line is partly visible, we want to allow
16096 that case to be handled in the usual way. */
16097 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16098 {
16099 w->cursor.vpos = -1;
16100 clear_glyph_matrix (w->desired_matrix);
16101 return -1;
16102 }
16103 }
16104
16105 /* If bottom moved off end of frame, change mode line percentage. */
16106 if (XFASTINT (w->window_end_pos) <= 0
16107 && Z != IT_CHARPOS (it))
16108 w->update_mode_line = Qt;
16109
16110 /* Set window_end_pos to the offset of the last character displayed
16111 on the window from the end of current_buffer. Set
16112 window_end_vpos to its row number. */
16113 if (last_text_row)
16114 {
16115 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16116 w->window_end_bytepos
16117 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16118 w->window_end_pos
16119 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16120 w->window_end_vpos
16121 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16122 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16123 ->displays_text_p);
16124 }
16125 else
16126 {
16127 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16128 w->window_end_pos = make_number (Z - ZV);
16129 w->window_end_vpos = make_number (0);
16130 }
16131
16132 /* But that is not valid info until redisplay finishes. */
16133 w->window_end_valid = Qnil;
16134 return 1;
16135 }
16136
16137
16138 \f
16139 /************************************************************************
16140 Window redisplay reusing current matrix when buffer has not changed
16141 ************************************************************************/
16142
16143 /* Try redisplay of window W showing an unchanged buffer with a
16144 different window start than the last time it was displayed by
16145 reusing its current matrix. Value is non-zero if successful.
16146 W->start is the new window start. */
16147
16148 static int
16149 try_window_reusing_current_matrix (struct window *w)
16150 {
16151 struct frame *f = XFRAME (w->frame);
16152 struct glyph_row *bottom_row;
16153 struct it it;
16154 struct run run;
16155 struct text_pos start, new_start;
16156 int nrows_scrolled, i;
16157 struct glyph_row *last_text_row;
16158 struct glyph_row *last_reused_text_row;
16159 struct glyph_row *start_row;
16160 int start_vpos, min_y, max_y;
16161
16162 #if GLYPH_DEBUG
16163 if (inhibit_try_window_reusing)
16164 return 0;
16165 #endif
16166
16167 if (/* This function doesn't handle terminal frames. */
16168 !FRAME_WINDOW_P (f)
16169 /* Don't try to reuse the display if windows have been split
16170 or such. */
16171 || windows_or_buffers_changed
16172 || cursor_type_changed)
16173 return 0;
16174
16175 /* Can't do this if region may have changed. */
16176 if ((!NILP (Vtransient_mark_mode)
16177 && !NILP (BVAR (current_buffer, mark_active)))
16178 || !NILP (w->region_showing)
16179 || !NILP (Vshow_trailing_whitespace))
16180 return 0;
16181
16182 /* If top-line visibility has changed, give up. */
16183 if (WINDOW_WANTS_HEADER_LINE_P (w)
16184 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16185 return 0;
16186
16187 /* Give up if old or new display is scrolled vertically. We could
16188 make this function handle this, but right now it doesn't. */
16189 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16190 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16191 return 0;
16192
16193 /* The variable new_start now holds the new window start. The old
16194 start `start' can be determined from the current matrix. */
16195 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16196 start = start_row->minpos;
16197 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16198
16199 /* Clear the desired matrix for the display below. */
16200 clear_glyph_matrix (w->desired_matrix);
16201
16202 if (CHARPOS (new_start) <= CHARPOS (start))
16203 {
16204 /* Don't use this method if the display starts with an ellipsis
16205 displayed for invisible text. It's not easy to handle that case
16206 below, and it's certainly not worth the effort since this is
16207 not a frequent case. */
16208 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16209 return 0;
16210
16211 IF_DEBUG (debug_method_add (w, "twu1"));
16212
16213 /* Display up to a row that can be reused. The variable
16214 last_text_row is set to the last row displayed that displays
16215 text. Note that it.vpos == 0 if or if not there is a
16216 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16217 start_display (&it, w, new_start);
16218 w->cursor.vpos = -1;
16219 last_text_row = last_reused_text_row = NULL;
16220
16221 while (it.current_y < it.last_visible_y
16222 && !fonts_changed_p)
16223 {
16224 /* If we have reached into the characters in the START row,
16225 that means the line boundaries have changed. So we
16226 can't start copying with the row START. Maybe it will
16227 work to start copying with the following row. */
16228 while (IT_CHARPOS (it) > CHARPOS (start))
16229 {
16230 /* Advance to the next row as the "start". */
16231 start_row++;
16232 start = start_row->minpos;
16233 /* If there are no more rows to try, or just one, give up. */
16234 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16235 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16236 || CHARPOS (start) == ZV)
16237 {
16238 clear_glyph_matrix (w->desired_matrix);
16239 return 0;
16240 }
16241
16242 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16243 }
16244 /* If we have reached alignment, we can copy the rest of the
16245 rows. */
16246 if (IT_CHARPOS (it) == CHARPOS (start)
16247 /* Don't accept "alignment" inside a display vector,
16248 since start_row could have started in the middle of
16249 that same display vector (thus their character
16250 positions match), and we have no way of telling if
16251 that is the case. */
16252 && it.current.dpvec_index < 0)
16253 break;
16254
16255 if (display_line (&it))
16256 last_text_row = it.glyph_row - 1;
16257
16258 }
16259
16260 /* A value of current_y < last_visible_y means that we stopped
16261 at the previous window start, which in turn means that we
16262 have at least one reusable row. */
16263 if (it.current_y < it.last_visible_y)
16264 {
16265 struct glyph_row *row;
16266
16267 /* IT.vpos always starts from 0; it counts text lines. */
16268 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16269
16270 /* Find PT if not already found in the lines displayed. */
16271 if (w->cursor.vpos < 0)
16272 {
16273 int dy = it.current_y - start_row->y;
16274
16275 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16276 row = row_containing_pos (w, PT, row, NULL, dy);
16277 if (row)
16278 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16279 dy, nrows_scrolled);
16280 else
16281 {
16282 clear_glyph_matrix (w->desired_matrix);
16283 return 0;
16284 }
16285 }
16286
16287 /* Scroll the display. Do it before the current matrix is
16288 changed. The problem here is that update has not yet
16289 run, i.e. part of the current matrix is not up to date.
16290 scroll_run_hook will clear the cursor, and use the
16291 current matrix to get the height of the row the cursor is
16292 in. */
16293 run.current_y = start_row->y;
16294 run.desired_y = it.current_y;
16295 run.height = it.last_visible_y - it.current_y;
16296
16297 if (run.height > 0 && run.current_y != run.desired_y)
16298 {
16299 update_begin (f);
16300 FRAME_RIF (f)->update_window_begin_hook (w);
16301 FRAME_RIF (f)->clear_window_mouse_face (w);
16302 FRAME_RIF (f)->scroll_run_hook (w, &run);
16303 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16304 update_end (f);
16305 }
16306
16307 /* Shift current matrix down by nrows_scrolled lines. */
16308 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16309 rotate_matrix (w->current_matrix,
16310 start_vpos,
16311 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16312 nrows_scrolled);
16313
16314 /* Disable lines that must be updated. */
16315 for (i = 0; i < nrows_scrolled; ++i)
16316 (start_row + i)->enabled_p = 0;
16317
16318 /* Re-compute Y positions. */
16319 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16320 max_y = it.last_visible_y;
16321 for (row = start_row + nrows_scrolled;
16322 row < bottom_row;
16323 ++row)
16324 {
16325 row->y = it.current_y;
16326 row->visible_height = row->height;
16327
16328 if (row->y < min_y)
16329 row->visible_height -= min_y - row->y;
16330 if (row->y + row->height > max_y)
16331 row->visible_height -= row->y + row->height - max_y;
16332 if (row->fringe_bitmap_periodic_p)
16333 row->redraw_fringe_bitmaps_p = 1;
16334
16335 it.current_y += row->height;
16336
16337 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16338 last_reused_text_row = row;
16339 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16340 break;
16341 }
16342
16343 /* Disable lines in the current matrix which are now
16344 below the window. */
16345 for (++row; row < bottom_row; ++row)
16346 row->enabled_p = row->mode_line_p = 0;
16347 }
16348
16349 /* Update window_end_pos etc.; last_reused_text_row is the last
16350 reused row from the current matrix containing text, if any.
16351 The value of last_text_row is the last displayed line
16352 containing text. */
16353 if (last_reused_text_row)
16354 {
16355 w->window_end_bytepos
16356 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16357 w->window_end_pos
16358 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16359 w->window_end_vpos
16360 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16361 w->current_matrix));
16362 }
16363 else if (last_text_row)
16364 {
16365 w->window_end_bytepos
16366 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16367 w->window_end_pos
16368 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16369 w->window_end_vpos
16370 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16371 }
16372 else
16373 {
16374 /* This window must be completely empty. */
16375 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16376 w->window_end_pos = make_number (Z - ZV);
16377 w->window_end_vpos = make_number (0);
16378 }
16379 w->window_end_valid = Qnil;
16380
16381 /* Update hint: don't try scrolling again in update_window. */
16382 w->desired_matrix->no_scrolling_p = 1;
16383
16384 #if GLYPH_DEBUG
16385 debug_method_add (w, "try_window_reusing_current_matrix 1");
16386 #endif
16387 return 1;
16388 }
16389 else if (CHARPOS (new_start) > CHARPOS (start))
16390 {
16391 struct glyph_row *pt_row, *row;
16392 struct glyph_row *first_reusable_row;
16393 struct glyph_row *first_row_to_display;
16394 int dy;
16395 int yb = window_text_bottom_y (w);
16396
16397 /* Find the row starting at new_start, if there is one. Don't
16398 reuse a partially visible line at the end. */
16399 first_reusable_row = start_row;
16400 while (first_reusable_row->enabled_p
16401 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16402 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16403 < CHARPOS (new_start)))
16404 ++first_reusable_row;
16405
16406 /* Give up if there is no row to reuse. */
16407 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16408 || !first_reusable_row->enabled_p
16409 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16410 != CHARPOS (new_start)))
16411 return 0;
16412
16413 /* We can reuse fully visible rows beginning with
16414 first_reusable_row to the end of the window. Set
16415 first_row_to_display to the first row that cannot be reused.
16416 Set pt_row to the row containing point, if there is any. */
16417 pt_row = NULL;
16418 for (first_row_to_display = first_reusable_row;
16419 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16420 ++first_row_to_display)
16421 {
16422 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16423 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16424 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16425 && first_row_to_display->ends_at_zv_p
16426 && pt_row == NULL)))
16427 pt_row = first_row_to_display;
16428 }
16429
16430 /* Start displaying at the start of first_row_to_display. */
16431 xassert (first_row_to_display->y < yb);
16432 init_to_row_start (&it, w, first_row_to_display);
16433
16434 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16435 - start_vpos);
16436 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16437 - nrows_scrolled);
16438 it.current_y = (first_row_to_display->y - first_reusable_row->y
16439 + WINDOW_HEADER_LINE_HEIGHT (w));
16440
16441 /* Display lines beginning with first_row_to_display in the
16442 desired matrix. Set last_text_row to the last row displayed
16443 that displays text. */
16444 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16445 if (pt_row == NULL)
16446 w->cursor.vpos = -1;
16447 last_text_row = NULL;
16448 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16449 if (display_line (&it))
16450 last_text_row = it.glyph_row - 1;
16451
16452 /* If point is in a reused row, adjust y and vpos of the cursor
16453 position. */
16454 if (pt_row)
16455 {
16456 w->cursor.vpos -= nrows_scrolled;
16457 w->cursor.y -= first_reusable_row->y - start_row->y;
16458 }
16459
16460 /* Give up if point isn't in a row displayed or reused. (This
16461 also handles the case where w->cursor.vpos < nrows_scrolled
16462 after the calls to display_line, which can happen with scroll
16463 margins. See bug#1295.) */
16464 if (w->cursor.vpos < 0)
16465 {
16466 clear_glyph_matrix (w->desired_matrix);
16467 return 0;
16468 }
16469
16470 /* Scroll the display. */
16471 run.current_y = first_reusable_row->y;
16472 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16473 run.height = it.last_visible_y - run.current_y;
16474 dy = run.current_y - run.desired_y;
16475
16476 if (run.height)
16477 {
16478 update_begin (f);
16479 FRAME_RIF (f)->update_window_begin_hook (w);
16480 FRAME_RIF (f)->clear_window_mouse_face (w);
16481 FRAME_RIF (f)->scroll_run_hook (w, &run);
16482 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16483 update_end (f);
16484 }
16485
16486 /* Adjust Y positions of reused rows. */
16487 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16488 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16489 max_y = it.last_visible_y;
16490 for (row = first_reusable_row; row < first_row_to_display; ++row)
16491 {
16492 row->y -= dy;
16493 row->visible_height = row->height;
16494 if (row->y < min_y)
16495 row->visible_height -= min_y - row->y;
16496 if (row->y + row->height > max_y)
16497 row->visible_height -= row->y + row->height - max_y;
16498 if (row->fringe_bitmap_periodic_p)
16499 row->redraw_fringe_bitmaps_p = 1;
16500 }
16501
16502 /* Scroll the current matrix. */
16503 xassert (nrows_scrolled > 0);
16504 rotate_matrix (w->current_matrix,
16505 start_vpos,
16506 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16507 -nrows_scrolled);
16508
16509 /* Disable rows not reused. */
16510 for (row -= nrows_scrolled; row < bottom_row; ++row)
16511 row->enabled_p = 0;
16512
16513 /* Point may have moved to a different line, so we cannot assume that
16514 the previous cursor position is valid; locate the correct row. */
16515 if (pt_row)
16516 {
16517 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16518 row < bottom_row
16519 && PT >= MATRIX_ROW_END_CHARPOS (row)
16520 && !row->ends_at_zv_p;
16521 row++)
16522 {
16523 w->cursor.vpos++;
16524 w->cursor.y = row->y;
16525 }
16526 if (row < bottom_row)
16527 {
16528 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16529 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16530
16531 /* Can't use this optimization with bidi-reordered glyph
16532 rows, unless cursor is already at point. */
16533 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16534 {
16535 if (!(w->cursor.hpos >= 0
16536 && w->cursor.hpos < row->used[TEXT_AREA]
16537 && BUFFERP (glyph->object)
16538 && glyph->charpos == PT))
16539 return 0;
16540 }
16541 else
16542 for (; glyph < end
16543 && (!BUFFERP (glyph->object)
16544 || glyph->charpos < PT);
16545 glyph++)
16546 {
16547 w->cursor.hpos++;
16548 w->cursor.x += glyph->pixel_width;
16549 }
16550 }
16551 }
16552
16553 /* Adjust window end. A null value of last_text_row means that
16554 the window end is in reused rows which in turn means that
16555 only its vpos can have changed. */
16556 if (last_text_row)
16557 {
16558 w->window_end_bytepos
16559 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16560 w->window_end_pos
16561 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16562 w->window_end_vpos
16563 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16564 }
16565 else
16566 {
16567 w->window_end_vpos
16568 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16569 }
16570
16571 w->window_end_valid = Qnil;
16572 w->desired_matrix->no_scrolling_p = 1;
16573
16574 #if GLYPH_DEBUG
16575 debug_method_add (w, "try_window_reusing_current_matrix 2");
16576 #endif
16577 return 1;
16578 }
16579
16580 return 0;
16581 }
16582
16583
16584 \f
16585 /************************************************************************
16586 Window redisplay reusing current matrix when buffer has changed
16587 ************************************************************************/
16588
16589 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16590 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16591 EMACS_INT *, EMACS_INT *);
16592 static struct glyph_row *
16593 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16594 struct glyph_row *);
16595
16596
16597 /* Return the last row in MATRIX displaying text. If row START is
16598 non-null, start searching with that row. IT gives the dimensions
16599 of the display. Value is null if matrix is empty; otherwise it is
16600 a pointer to the row found. */
16601
16602 static struct glyph_row *
16603 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16604 struct glyph_row *start)
16605 {
16606 struct glyph_row *row, *row_found;
16607
16608 /* Set row_found to the last row in IT->w's current matrix
16609 displaying text. The loop looks funny but think of partially
16610 visible lines. */
16611 row_found = NULL;
16612 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16613 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16614 {
16615 xassert (row->enabled_p);
16616 row_found = row;
16617 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16618 break;
16619 ++row;
16620 }
16621
16622 return row_found;
16623 }
16624
16625
16626 /* Return the last row in the current matrix of W that is not affected
16627 by changes at the start of current_buffer that occurred since W's
16628 current matrix was built. Value is null if no such row exists.
16629
16630 BEG_UNCHANGED us the number of characters unchanged at the start of
16631 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16632 first changed character in current_buffer. Characters at positions <
16633 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16634 when the current matrix was built. */
16635
16636 static struct glyph_row *
16637 find_last_unchanged_at_beg_row (struct window *w)
16638 {
16639 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16640 struct glyph_row *row;
16641 struct glyph_row *row_found = NULL;
16642 int yb = window_text_bottom_y (w);
16643
16644 /* Find the last row displaying unchanged text. */
16645 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16646 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16647 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16648 ++row)
16649 {
16650 if (/* If row ends before first_changed_pos, it is unchanged,
16651 except in some case. */
16652 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16653 /* When row ends in ZV and we write at ZV it is not
16654 unchanged. */
16655 && !row->ends_at_zv_p
16656 /* When first_changed_pos is the end of a continued line,
16657 row is not unchanged because it may be no longer
16658 continued. */
16659 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16660 && (row->continued_p
16661 || row->exact_window_width_line_p))
16662 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16663 needs to be recomputed, so don't consider this row as
16664 unchanged. This happens when the last line was
16665 bidi-reordered and was killed immediately before this
16666 redisplay cycle. In that case, ROW->end stores the
16667 buffer position of the first visual-order character of
16668 the killed text, which is now beyond ZV. */
16669 && CHARPOS (row->end.pos) <= ZV)
16670 row_found = row;
16671
16672 /* Stop if last visible row. */
16673 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16674 break;
16675 }
16676
16677 return row_found;
16678 }
16679
16680
16681 /* Find the first glyph row in the current matrix of W that is not
16682 affected by changes at the end of current_buffer since the
16683 time W's current matrix was built.
16684
16685 Return in *DELTA the number of chars by which buffer positions in
16686 unchanged text at the end of current_buffer must be adjusted.
16687
16688 Return in *DELTA_BYTES the corresponding number of bytes.
16689
16690 Value is null if no such row exists, i.e. all rows are affected by
16691 changes. */
16692
16693 static struct glyph_row *
16694 find_first_unchanged_at_end_row (struct window *w,
16695 EMACS_INT *delta, EMACS_INT *delta_bytes)
16696 {
16697 struct glyph_row *row;
16698 struct glyph_row *row_found = NULL;
16699
16700 *delta = *delta_bytes = 0;
16701
16702 /* Display must not have been paused, otherwise the current matrix
16703 is not up to date. */
16704 eassert (!NILP (w->window_end_valid));
16705
16706 /* A value of window_end_pos >= END_UNCHANGED means that the window
16707 end is in the range of changed text. If so, there is no
16708 unchanged row at the end of W's current matrix. */
16709 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16710 return NULL;
16711
16712 /* Set row to the last row in W's current matrix displaying text. */
16713 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16714
16715 /* If matrix is entirely empty, no unchanged row exists. */
16716 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16717 {
16718 /* The value of row is the last glyph row in the matrix having a
16719 meaningful buffer position in it. The end position of row
16720 corresponds to window_end_pos. This allows us to translate
16721 buffer positions in the current matrix to current buffer
16722 positions for characters not in changed text. */
16723 EMACS_INT Z_old =
16724 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16725 EMACS_INT Z_BYTE_old =
16726 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16727 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16728 struct glyph_row *first_text_row
16729 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16730
16731 *delta = Z - Z_old;
16732 *delta_bytes = Z_BYTE - Z_BYTE_old;
16733
16734 /* Set last_unchanged_pos to the buffer position of the last
16735 character in the buffer that has not been changed. Z is the
16736 index + 1 of the last character in current_buffer, i.e. by
16737 subtracting END_UNCHANGED we get the index of the last
16738 unchanged character, and we have to add BEG to get its buffer
16739 position. */
16740 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16741 last_unchanged_pos_old = last_unchanged_pos - *delta;
16742
16743 /* Search backward from ROW for a row displaying a line that
16744 starts at a minimum position >= last_unchanged_pos_old. */
16745 for (; row > first_text_row; --row)
16746 {
16747 /* This used to abort, but it can happen.
16748 It is ok to just stop the search instead here. KFS. */
16749 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16750 break;
16751
16752 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16753 row_found = row;
16754 }
16755 }
16756
16757 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16758
16759 return row_found;
16760 }
16761
16762
16763 /* Make sure that glyph rows in the current matrix of window W
16764 reference the same glyph memory as corresponding rows in the
16765 frame's frame matrix. This function is called after scrolling W's
16766 current matrix on a terminal frame in try_window_id and
16767 try_window_reusing_current_matrix. */
16768
16769 static void
16770 sync_frame_with_window_matrix_rows (struct window *w)
16771 {
16772 struct frame *f = XFRAME (w->frame);
16773 struct glyph_row *window_row, *window_row_end, *frame_row;
16774
16775 /* Preconditions: W must be a leaf window and full-width. Its frame
16776 must have a frame matrix. */
16777 xassert (NILP (w->hchild) && NILP (w->vchild));
16778 xassert (WINDOW_FULL_WIDTH_P (w));
16779 xassert (!FRAME_WINDOW_P (f));
16780
16781 /* If W is a full-width window, glyph pointers in W's current matrix
16782 have, by definition, to be the same as glyph pointers in the
16783 corresponding frame matrix. Note that frame matrices have no
16784 marginal areas (see build_frame_matrix). */
16785 window_row = w->current_matrix->rows;
16786 window_row_end = window_row + w->current_matrix->nrows;
16787 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16788 while (window_row < window_row_end)
16789 {
16790 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16791 struct glyph *end = window_row->glyphs[LAST_AREA];
16792
16793 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16794 frame_row->glyphs[TEXT_AREA] = start;
16795 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16796 frame_row->glyphs[LAST_AREA] = end;
16797
16798 /* Disable frame rows whose corresponding window rows have
16799 been disabled in try_window_id. */
16800 if (!window_row->enabled_p)
16801 frame_row->enabled_p = 0;
16802
16803 ++window_row, ++frame_row;
16804 }
16805 }
16806
16807
16808 /* Find the glyph row in window W containing CHARPOS. Consider all
16809 rows between START and END (not inclusive). END null means search
16810 all rows to the end of the display area of W. Value is the row
16811 containing CHARPOS or null. */
16812
16813 struct glyph_row *
16814 row_containing_pos (struct window *w, EMACS_INT charpos,
16815 struct glyph_row *start, struct glyph_row *end, int dy)
16816 {
16817 struct glyph_row *row = start;
16818 struct glyph_row *best_row = NULL;
16819 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16820 int last_y;
16821
16822 /* If we happen to start on a header-line, skip that. */
16823 if (row->mode_line_p)
16824 ++row;
16825
16826 if ((end && row >= end) || !row->enabled_p)
16827 return NULL;
16828
16829 last_y = window_text_bottom_y (w) - dy;
16830
16831 while (1)
16832 {
16833 /* Give up if we have gone too far. */
16834 if (end && row >= end)
16835 return NULL;
16836 /* This formerly returned if they were equal.
16837 I think that both quantities are of a "last plus one" type;
16838 if so, when they are equal, the row is within the screen. -- rms. */
16839 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16840 return NULL;
16841
16842 /* If it is in this row, return this row. */
16843 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16844 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16845 /* The end position of a row equals the start
16846 position of the next row. If CHARPOS is there, we
16847 would rather display it in the next line, except
16848 when this line ends in ZV. */
16849 && !row->ends_at_zv_p
16850 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16851 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16852 {
16853 struct glyph *g;
16854
16855 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16856 || (!best_row && !row->continued_p))
16857 return row;
16858 /* In bidi-reordered rows, there could be several rows
16859 occluding point, all of them belonging to the same
16860 continued line. We need to find the row which fits
16861 CHARPOS the best. */
16862 for (g = row->glyphs[TEXT_AREA];
16863 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16864 g++)
16865 {
16866 if (!STRINGP (g->object))
16867 {
16868 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16869 {
16870 mindif = eabs (g->charpos - charpos);
16871 best_row = row;
16872 /* Exact match always wins. */
16873 if (mindif == 0)
16874 return best_row;
16875 }
16876 }
16877 }
16878 }
16879 else if (best_row && !row->continued_p)
16880 return best_row;
16881 ++row;
16882 }
16883 }
16884
16885
16886 /* Try to redisplay window W by reusing its existing display. W's
16887 current matrix must be up to date when this function is called,
16888 i.e. window_end_valid must not be nil.
16889
16890 Value is
16891
16892 1 if display has been updated
16893 0 if otherwise unsuccessful
16894 -1 if redisplay with same window start is known not to succeed
16895
16896 The following steps are performed:
16897
16898 1. Find the last row in the current matrix of W that is not
16899 affected by changes at the start of current_buffer. If no such row
16900 is found, give up.
16901
16902 2. Find the first row in W's current matrix that is not affected by
16903 changes at the end of current_buffer. Maybe there is no such row.
16904
16905 3. Display lines beginning with the row + 1 found in step 1 to the
16906 row found in step 2 or, if step 2 didn't find a row, to the end of
16907 the window.
16908
16909 4. If cursor is not known to appear on the window, give up.
16910
16911 5. If display stopped at the row found in step 2, scroll the
16912 display and current matrix as needed.
16913
16914 6. Maybe display some lines at the end of W, if we must. This can
16915 happen under various circumstances, like a partially visible line
16916 becoming fully visible, or because newly displayed lines are displayed
16917 in smaller font sizes.
16918
16919 7. Update W's window end information. */
16920
16921 static int
16922 try_window_id (struct window *w)
16923 {
16924 struct frame *f = XFRAME (w->frame);
16925 struct glyph_matrix *current_matrix = w->current_matrix;
16926 struct glyph_matrix *desired_matrix = w->desired_matrix;
16927 struct glyph_row *last_unchanged_at_beg_row;
16928 struct glyph_row *first_unchanged_at_end_row;
16929 struct glyph_row *row;
16930 struct glyph_row *bottom_row;
16931 int bottom_vpos;
16932 struct it it;
16933 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16934 int dvpos, dy;
16935 struct text_pos start_pos;
16936 struct run run;
16937 int first_unchanged_at_end_vpos = 0;
16938 struct glyph_row *last_text_row, *last_text_row_at_end;
16939 struct text_pos start;
16940 EMACS_INT first_changed_charpos, last_changed_charpos;
16941
16942 #if GLYPH_DEBUG
16943 if (inhibit_try_window_id)
16944 return 0;
16945 #endif
16946
16947 /* This is handy for debugging. */
16948 #if 0
16949 #define GIVE_UP(X) \
16950 do { \
16951 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16952 return 0; \
16953 } while (0)
16954 #else
16955 #define GIVE_UP(X) return 0
16956 #endif
16957
16958 SET_TEXT_POS_FROM_MARKER (start, w->start);
16959
16960 /* Don't use this for mini-windows because these can show
16961 messages and mini-buffers, and we don't handle that here. */
16962 if (MINI_WINDOW_P (w))
16963 GIVE_UP (1);
16964
16965 /* This flag is used to prevent redisplay optimizations. */
16966 if (windows_or_buffers_changed || cursor_type_changed)
16967 GIVE_UP (2);
16968
16969 /* Verify that narrowing has not changed.
16970 Also verify that we were not told to prevent redisplay optimizations.
16971 It would be nice to further
16972 reduce the number of cases where this prevents try_window_id. */
16973 if (current_buffer->clip_changed
16974 || current_buffer->prevent_redisplay_optimizations_p)
16975 GIVE_UP (3);
16976
16977 /* Window must either use window-based redisplay or be full width. */
16978 if (!FRAME_WINDOW_P (f)
16979 && (!FRAME_LINE_INS_DEL_OK (f)
16980 || !WINDOW_FULL_WIDTH_P (w)))
16981 GIVE_UP (4);
16982
16983 /* Give up if point is known NOT to appear in W. */
16984 if (PT < CHARPOS (start))
16985 GIVE_UP (5);
16986
16987 /* Another way to prevent redisplay optimizations. */
16988 if (XFASTINT (w->last_modified) == 0)
16989 GIVE_UP (6);
16990
16991 /* Verify that window is not hscrolled. */
16992 if (XFASTINT (w->hscroll) != 0)
16993 GIVE_UP (7);
16994
16995 /* Verify that display wasn't paused. */
16996 if (NILP (w->window_end_valid))
16997 GIVE_UP (8);
16998
16999 /* Can't use this if highlighting a region because a cursor movement
17000 will do more than just set the cursor. */
17001 if (!NILP (Vtransient_mark_mode)
17002 && !NILP (BVAR (current_buffer, mark_active)))
17003 GIVE_UP (9);
17004
17005 /* Likewise if highlighting trailing whitespace. */
17006 if (!NILP (Vshow_trailing_whitespace))
17007 GIVE_UP (11);
17008
17009 /* Likewise if showing a region. */
17010 if (!NILP (w->region_showing))
17011 GIVE_UP (10);
17012
17013 /* Can't use this if overlay arrow position and/or string have
17014 changed. */
17015 if (overlay_arrows_changed_p ())
17016 GIVE_UP (12);
17017
17018 /* When word-wrap is on, adding a space to the first word of a
17019 wrapped line can change the wrap position, altering the line
17020 above it. It might be worthwhile to handle this more
17021 intelligently, but for now just redisplay from scratch. */
17022 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17023 GIVE_UP (21);
17024
17025 /* Under bidi reordering, adding or deleting a character in the
17026 beginning of a paragraph, before the first strong directional
17027 character, can change the base direction of the paragraph (unless
17028 the buffer specifies a fixed paragraph direction), which will
17029 require to redisplay the whole paragraph. It might be worthwhile
17030 to find the paragraph limits and widen the range of redisplayed
17031 lines to that, but for now just give up this optimization and
17032 redisplay from scratch. */
17033 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17034 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17035 GIVE_UP (22);
17036
17037 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17038 only if buffer has really changed. The reason is that the gap is
17039 initially at Z for freshly visited files. The code below would
17040 set end_unchanged to 0 in that case. */
17041 if (MODIFF > SAVE_MODIFF
17042 /* This seems to happen sometimes after saving a buffer. */
17043 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17044 {
17045 if (GPT - BEG < BEG_UNCHANGED)
17046 BEG_UNCHANGED = GPT - BEG;
17047 if (Z - GPT < END_UNCHANGED)
17048 END_UNCHANGED = Z - GPT;
17049 }
17050
17051 /* The position of the first and last character that has been changed. */
17052 first_changed_charpos = BEG + BEG_UNCHANGED;
17053 last_changed_charpos = Z - END_UNCHANGED;
17054
17055 /* If window starts after a line end, and the last change is in
17056 front of that newline, then changes don't affect the display.
17057 This case happens with stealth-fontification. Note that although
17058 the display is unchanged, glyph positions in the matrix have to
17059 be adjusted, of course. */
17060 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17061 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17062 && ((last_changed_charpos < CHARPOS (start)
17063 && CHARPOS (start) == BEGV)
17064 || (last_changed_charpos < CHARPOS (start) - 1
17065 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17066 {
17067 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17068 struct glyph_row *r0;
17069
17070 /* Compute how many chars/bytes have been added to or removed
17071 from the buffer. */
17072 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17073 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17074 Z_delta = Z - Z_old;
17075 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17076
17077 /* Give up if PT is not in the window. Note that it already has
17078 been checked at the start of try_window_id that PT is not in
17079 front of the window start. */
17080 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17081 GIVE_UP (13);
17082
17083 /* If window start is unchanged, we can reuse the whole matrix
17084 as is, after adjusting glyph positions. No need to compute
17085 the window end again, since its offset from Z hasn't changed. */
17086 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17087 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17088 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17089 /* PT must not be in a partially visible line. */
17090 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17091 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17092 {
17093 /* Adjust positions in the glyph matrix. */
17094 if (Z_delta || Z_delta_bytes)
17095 {
17096 struct glyph_row *r1
17097 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17098 increment_matrix_positions (w->current_matrix,
17099 MATRIX_ROW_VPOS (r0, current_matrix),
17100 MATRIX_ROW_VPOS (r1, current_matrix),
17101 Z_delta, Z_delta_bytes);
17102 }
17103
17104 /* Set the cursor. */
17105 row = row_containing_pos (w, PT, r0, NULL, 0);
17106 if (row)
17107 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17108 else
17109 abort ();
17110 return 1;
17111 }
17112 }
17113
17114 /* Handle the case that changes are all below what is displayed in
17115 the window, and that PT is in the window. This shortcut cannot
17116 be taken if ZV is visible in the window, and text has been added
17117 there that is visible in the window. */
17118 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17119 /* ZV is not visible in the window, or there are no
17120 changes at ZV, actually. */
17121 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17122 || first_changed_charpos == last_changed_charpos))
17123 {
17124 struct glyph_row *r0;
17125
17126 /* Give up if PT is not in the window. Note that it already has
17127 been checked at the start of try_window_id that PT is not in
17128 front of the window start. */
17129 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17130 GIVE_UP (14);
17131
17132 /* If window start is unchanged, we can reuse the whole matrix
17133 as is, without changing glyph positions since no text has
17134 been added/removed in front of the window end. */
17135 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17136 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17137 /* PT must not be in a partially visible line. */
17138 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17139 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17140 {
17141 /* We have to compute the window end anew since text
17142 could have been added/removed after it. */
17143 w->window_end_pos
17144 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17145 w->window_end_bytepos
17146 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17147
17148 /* Set the cursor. */
17149 row = row_containing_pos (w, PT, r0, NULL, 0);
17150 if (row)
17151 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17152 else
17153 abort ();
17154 return 2;
17155 }
17156 }
17157
17158 /* Give up if window start is in the changed area.
17159
17160 The condition used to read
17161
17162 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17163
17164 but why that was tested escapes me at the moment. */
17165 if (CHARPOS (start) >= first_changed_charpos
17166 && CHARPOS (start) <= last_changed_charpos)
17167 GIVE_UP (15);
17168
17169 /* Check that window start agrees with the start of the first glyph
17170 row in its current matrix. Check this after we know the window
17171 start is not in changed text, otherwise positions would not be
17172 comparable. */
17173 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17174 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17175 GIVE_UP (16);
17176
17177 /* Give up if the window ends in strings. Overlay strings
17178 at the end are difficult to handle, so don't try. */
17179 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17180 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17181 GIVE_UP (20);
17182
17183 /* Compute the position at which we have to start displaying new
17184 lines. Some of the lines at the top of the window might be
17185 reusable because they are not displaying changed text. Find the
17186 last row in W's current matrix not affected by changes at the
17187 start of current_buffer. Value is null if changes start in the
17188 first line of window. */
17189 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17190 if (last_unchanged_at_beg_row)
17191 {
17192 /* Avoid starting to display in the middle of a character, a TAB
17193 for instance. This is easier than to set up the iterator
17194 exactly, and it's not a frequent case, so the additional
17195 effort wouldn't really pay off. */
17196 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17197 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17198 && last_unchanged_at_beg_row > w->current_matrix->rows)
17199 --last_unchanged_at_beg_row;
17200
17201 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17202 GIVE_UP (17);
17203
17204 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17205 GIVE_UP (18);
17206 start_pos = it.current.pos;
17207
17208 /* Start displaying new lines in the desired matrix at the same
17209 vpos we would use in the current matrix, i.e. below
17210 last_unchanged_at_beg_row. */
17211 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17212 current_matrix);
17213 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17214 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17215
17216 xassert (it.hpos == 0 && it.current_x == 0);
17217 }
17218 else
17219 {
17220 /* There are no reusable lines at the start of the window.
17221 Start displaying in the first text line. */
17222 start_display (&it, w, start);
17223 it.vpos = it.first_vpos;
17224 start_pos = it.current.pos;
17225 }
17226
17227 /* Find the first row that is not affected by changes at the end of
17228 the buffer. Value will be null if there is no unchanged row, in
17229 which case we must redisplay to the end of the window. delta
17230 will be set to the value by which buffer positions beginning with
17231 first_unchanged_at_end_row have to be adjusted due to text
17232 changes. */
17233 first_unchanged_at_end_row
17234 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17235 IF_DEBUG (debug_delta = delta);
17236 IF_DEBUG (debug_delta_bytes = delta_bytes);
17237
17238 /* Set stop_pos to the buffer position up to which we will have to
17239 display new lines. If first_unchanged_at_end_row != NULL, this
17240 is the buffer position of the start of the line displayed in that
17241 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17242 that we don't stop at a buffer position. */
17243 stop_pos = 0;
17244 if (first_unchanged_at_end_row)
17245 {
17246 xassert (last_unchanged_at_beg_row == NULL
17247 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17248
17249 /* If this is a continuation line, move forward to the next one
17250 that isn't. Changes in lines above affect this line.
17251 Caution: this may move first_unchanged_at_end_row to a row
17252 not displaying text. */
17253 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17254 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17255 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17256 < it.last_visible_y))
17257 ++first_unchanged_at_end_row;
17258
17259 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17260 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17261 >= it.last_visible_y))
17262 first_unchanged_at_end_row = NULL;
17263 else
17264 {
17265 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17266 + delta);
17267 first_unchanged_at_end_vpos
17268 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17269 xassert (stop_pos >= Z - END_UNCHANGED);
17270 }
17271 }
17272 else if (last_unchanged_at_beg_row == NULL)
17273 GIVE_UP (19);
17274
17275
17276 #if GLYPH_DEBUG
17277
17278 /* Either there is no unchanged row at the end, or the one we have
17279 now displays text. This is a necessary condition for the window
17280 end pos calculation at the end of this function. */
17281 xassert (first_unchanged_at_end_row == NULL
17282 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17283
17284 debug_last_unchanged_at_beg_vpos
17285 = (last_unchanged_at_beg_row
17286 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17287 : -1);
17288 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17289
17290 #endif /* GLYPH_DEBUG != 0 */
17291
17292
17293 /* Display new lines. Set last_text_row to the last new line
17294 displayed which has text on it, i.e. might end up as being the
17295 line where the window_end_vpos is. */
17296 w->cursor.vpos = -1;
17297 last_text_row = NULL;
17298 overlay_arrow_seen = 0;
17299 while (it.current_y < it.last_visible_y
17300 && !fonts_changed_p
17301 && (first_unchanged_at_end_row == NULL
17302 || IT_CHARPOS (it) < stop_pos))
17303 {
17304 if (display_line (&it))
17305 last_text_row = it.glyph_row - 1;
17306 }
17307
17308 if (fonts_changed_p)
17309 return -1;
17310
17311
17312 /* Compute differences in buffer positions, y-positions etc. for
17313 lines reused at the bottom of the window. Compute what we can
17314 scroll. */
17315 if (first_unchanged_at_end_row
17316 /* No lines reused because we displayed everything up to the
17317 bottom of the window. */
17318 && it.current_y < it.last_visible_y)
17319 {
17320 dvpos = (it.vpos
17321 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17322 current_matrix));
17323 dy = it.current_y - first_unchanged_at_end_row->y;
17324 run.current_y = first_unchanged_at_end_row->y;
17325 run.desired_y = run.current_y + dy;
17326 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17327 }
17328 else
17329 {
17330 delta = delta_bytes = dvpos = dy
17331 = run.current_y = run.desired_y = run.height = 0;
17332 first_unchanged_at_end_row = NULL;
17333 }
17334 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17335
17336
17337 /* Find the cursor if not already found. We have to decide whether
17338 PT will appear on this window (it sometimes doesn't, but this is
17339 not a very frequent case.) This decision has to be made before
17340 the current matrix is altered. A value of cursor.vpos < 0 means
17341 that PT is either in one of the lines beginning at
17342 first_unchanged_at_end_row or below the window. Don't care for
17343 lines that might be displayed later at the window end; as
17344 mentioned, this is not a frequent case. */
17345 if (w->cursor.vpos < 0)
17346 {
17347 /* Cursor in unchanged rows at the top? */
17348 if (PT < CHARPOS (start_pos)
17349 && last_unchanged_at_beg_row)
17350 {
17351 row = row_containing_pos (w, PT,
17352 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17353 last_unchanged_at_beg_row + 1, 0);
17354 if (row)
17355 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17356 }
17357
17358 /* Start from first_unchanged_at_end_row looking for PT. */
17359 else if (first_unchanged_at_end_row)
17360 {
17361 row = row_containing_pos (w, PT - delta,
17362 first_unchanged_at_end_row, NULL, 0);
17363 if (row)
17364 set_cursor_from_row (w, row, w->current_matrix, delta,
17365 delta_bytes, dy, dvpos);
17366 }
17367
17368 /* Give up if cursor was not found. */
17369 if (w->cursor.vpos < 0)
17370 {
17371 clear_glyph_matrix (w->desired_matrix);
17372 return -1;
17373 }
17374 }
17375
17376 /* Don't let the cursor end in the scroll margins. */
17377 {
17378 int this_scroll_margin, cursor_height;
17379
17380 this_scroll_margin =
17381 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17382 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17383 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17384
17385 if ((w->cursor.y < this_scroll_margin
17386 && CHARPOS (start) > BEGV)
17387 /* Old redisplay didn't take scroll margin into account at the bottom,
17388 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17389 || (w->cursor.y + (make_cursor_line_fully_visible_p
17390 ? cursor_height + this_scroll_margin
17391 : 1)) > it.last_visible_y)
17392 {
17393 w->cursor.vpos = -1;
17394 clear_glyph_matrix (w->desired_matrix);
17395 return -1;
17396 }
17397 }
17398
17399 /* Scroll the display. Do it before changing the current matrix so
17400 that xterm.c doesn't get confused about where the cursor glyph is
17401 found. */
17402 if (dy && run.height)
17403 {
17404 update_begin (f);
17405
17406 if (FRAME_WINDOW_P (f))
17407 {
17408 FRAME_RIF (f)->update_window_begin_hook (w);
17409 FRAME_RIF (f)->clear_window_mouse_face (w);
17410 FRAME_RIF (f)->scroll_run_hook (w, &run);
17411 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17412 }
17413 else
17414 {
17415 /* Terminal frame. In this case, dvpos gives the number of
17416 lines to scroll by; dvpos < 0 means scroll up. */
17417 int from_vpos
17418 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17419 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17420 int end = (WINDOW_TOP_EDGE_LINE (w)
17421 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17422 + window_internal_height (w));
17423
17424 #if defined (HAVE_GPM) || defined (MSDOS)
17425 x_clear_window_mouse_face (w);
17426 #endif
17427 /* Perform the operation on the screen. */
17428 if (dvpos > 0)
17429 {
17430 /* Scroll last_unchanged_at_beg_row to the end of the
17431 window down dvpos lines. */
17432 set_terminal_window (f, end);
17433
17434 /* On dumb terminals delete dvpos lines at the end
17435 before inserting dvpos empty lines. */
17436 if (!FRAME_SCROLL_REGION_OK (f))
17437 ins_del_lines (f, end - dvpos, -dvpos);
17438
17439 /* Insert dvpos empty lines in front of
17440 last_unchanged_at_beg_row. */
17441 ins_del_lines (f, from, dvpos);
17442 }
17443 else if (dvpos < 0)
17444 {
17445 /* Scroll up last_unchanged_at_beg_vpos to the end of
17446 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17447 set_terminal_window (f, end);
17448
17449 /* Delete dvpos lines in front of
17450 last_unchanged_at_beg_vpos. ins_del_lines will set
17451 the cursor to the given vpos and emit |dvpos| delete
17452 line sequences. */
17453 ins_del_lines (f, from + dvpos, dvpos);
17454
17455 /* On a dumb terminal insert dvpos empty lines at the
17456 end. */
17457 if (!FRAME_SCROLL_REGION_OK (f))
17458 ins_del_lines (f, end + dvpos, -dvpos);
17459 }
17460
17461 set_terminal_window (f, 0);
17462 }
17463
17464 update_end (f);
17465 }
17466
17467 /* Shift reused rows of the current matrix to the right position.
17468 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17469 text. */
17470 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17471 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17472 if (dvpos < 0)
17473 {
17474 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17475 bottom_vpos, dvpos);
17476 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17477 bottom_vpos, 0);
17478 }
17479 else if (dvpos > 0)
17480 {
17481 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17482 bottom_vpos, dvpos);
17483 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17484 first_unchanged_at_end_vpos + dvpos, 0);
17485 }
17486
17487 /* For frame-based redisplay, make sure that current frame and window
17488 matrix are in sync with respect to glyph memory. */
17489 if (!FRAME_WINDOW_P (f))
17490 sync_frame_with_window_matrix_rows (w);
17491
17492 /* Adjust buffer positions in reused rows. */
17493 if (delta || delta_bytes)
17494 increment_matrix_positions (current_matrix,
17495 first_unchanged_at_end_vpos + dvpos,
17496 bottom_vpos, delta, delta_bytes);
17497
17498 /* Adjust Y positions. */
17499 if (dy)
17500 shift_glyph_matrix (w, current_matrix,
17501 first_unchanged_at_end_vpos + dvpos,
17502 bottom_vpos, dy);
17503
17504 if (first_unchanged_at_end_row)
17505 {
17506 first_unchanged_at_end_row += dvpos;
17507 if (first_unchanged_at_end_row->y >= it.last_visible_y
17508 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17509 first_unchanged_at_end_row = NULL;
17510 }
17511
17512 /* If scrolling up, there may be some lines to display at the end of
17513 the window. */
17514 last_text_row_at_end = NULL;
17515 if (dy < 0)
17516 {
17517 /* Scrolling up can leave for example a partially visible line
17518 at the end of the window to be redisplayed. */
17519 /* Set last_row to the glyph row in the current matrix where the
17520 window end line is found. It has been moved up or down in
17521 the matrix by dvpos. */
17522 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17523 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17524
17525 /* If last_row is the window end line, it should display text. */
17526 xassert (last_row->displays_text_p);
17527
17528 /* If window end line was partially visible before, begin
17529 displaying at that line. Otherwise begin displaying with the
17530 line following it. */
17531 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17532 {
17533 init_to_row_start (&it, w, last_row);
17534 it.vpos = last_vpos;
17535 it.current_y = last_row->y;
17536 }
17537 else
17538 {
17539 init_to_row_end (&it, w, last_row);
17540 it.vpos = 1 + last_vpos;
17541 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17542 ++last_row;
17543 }
17544
17545 /* We may start in a continuation line. If so, we have to
17546 get the right continuation_lines_width and current_x. */
17547 it.continuation_lines_width = last_row->continuation_lines_width;
17548 it.hpos = it.current_x = 0;
17549
17550 /* Display the rest of the lines at the window end. */
17551 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17552 while (it.current_y < it.last_visible_y
17553 && !fonts_changed_p)
17554 {
17555 /* Is it always sure that the display agrees with lines in
17556 the current matrix? I don't think so, so we mark rows
17557 displayed invalid in the current matrix by setting their
17558 enabled_p flag to zero. */
17559 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17560 if (display_line (&it))
17561 last_text_row_at_end = it.glyph_row - 1;
17562 }
17563 }
17564
17565 /* Update window_end_pos and window_end_vpos. */
17566 if (first_unchanged_at_end_row
17567 && !last_text_row_at_end)
17568 {
17569 /* Window end line if one of the preserved rows from the current
17570 matrix. Set row to the last row displaying text in current
17571 matrix starting at first_unchanged_at_end_row, after
17572 scrolling. */
17573 xassert (first_unchanged_at_end_row->displays_text_p);
17574 row = find_last_row_displaying_text (w->current_matrix, &it,
17575 first_unchanged_at_end_row);
17576 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17577
17578 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17579 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17580 w->window_end_vpos
17581 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17582 xassert (w->window_end_bytepos >= 0);
17583 IF_DEBUG (debug_method_add (w, "A"));
17584 }
17585 else if (last_text_row_at_end)
17586 {
17587 w->window_end_pos
17588 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17589 w->window_end_bytepos
17590 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17591 w->window_end_vpos
17592 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17593 xassert (w->window_end_bytepos >= 0);
17594 IF_DEBUG (debug_method_add (w, "B"));
17595 }
17596 else if (last_text_row)
17597 {
17598 /* We have displayed either to the end of the window or at the
17599 end of the window, i.e. the last row with text is to be found
17600 in the desired matrix. */
17601 w->window_end_pos
17602 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17603 w->window_end_bytepos
17604 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17605 w->window_end_vpos
17606 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17607 xassert (w->window_end_bytepos >= 0);
17608 }
17609 else if (first_unchanged_at_end_row == NULL
17610 && last_text_row == NULL
17611 && last_text_row_at_end == NULL)
17612 {
17613 /* Displayed to end of window, but no line containing text was
17614 displayed. Lines were deleted at the end of the window. */
17615 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17616 int vpos = XFASTINT (w->window_end_vpos);
17617 struct glyph_row *current_row = current_matrix->rows + vpos;
17618 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17619
17620 for (row = NULL;
17621 row == NULL && vpos >= first_vpos;
17622 --vpos, --current_row, --desired_row)
17623 {
17624 if (desired_row->enabled_p)
17625 {
17626 if (desired_row->displays_text_p)
17627 row = desired_row;
17628 }
17629 else if (current_row->displays_text_p)
17630 row = current_row;
17631 }
17632
17633 xassert (row != NULL);
17634 w->window_end_vpos = make_number (vpos + 1);
17635 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17636 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17637 xassert (w->window_end_bytepos >= 0);
17638 IF_DEBUG (debug_method_add (w, "C"));
17639 }
17640 else
17641 abort ();
17642
17643 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17644 debug_end_vpos = XFASTINT (w->window_end_vpos));
17645
17646 /* Record that display has not been completed. */
17647 w->window_end_valid = Qnil;
17648 w->desired_matrix->no_scrolling_p = 1;
17649 return 3;
17650
17651 #undef GIVE_UP
17652 }
17653
17654
17655 \f
17656 /***********************************************************************
17657 More debugging support
17658 ***********************************************************************/
17659
17660 #if GLYPH_DEBUG
17661
17662 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17663 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17664 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17665
17666
17667 /* Dump the contents of glyph matrix MATRIX on stderr.
17668
17669 GLYPHS 0 means don't show glyph contents.
17670 GLYPHS 1 means show glyphs in short form
17671 GLYPHS > 1 means show glyphs in long form. */
17672
17673 void
17674 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17675 {
17676 int i;
17677 for (i = 0; i < matrix->nrows; ++i)
17678 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17679 }
17680
17681
17682 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17683 the glyph row and area where the glyph comes from. */
17684
17685 void
17686 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17687 {
17688 if (glyph->type == CHAR_GLYPH)
17689 {
17690 fprintf (stderr,
17691 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17692 glyph - row->glyphs[TEXT_AREA],
17693 'C',
17694 glyph->charpos,
17695 (BUFFERP (glyph->object)
17696 ? 'B'
17697 : (STRINGP (glyph->object)
17698 ? 'S'
17699 : '-')),
17700 glyph->pixel_width,
17701 glyph->u.ch,
17702 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17703 ? glyph->u.ch
17704 : '.'),
17705 glyph->face_id,
17706 glyph->left_box_line_p,
17707 glyph->right_box_line_p);
17708 }
17709 else if (glyph->type == STRETCH_GLYPH)
17710 {
17711 fprintf (stderr,
17712 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17713 glyph - row->glyphs[TEXT_AREA],
17714 'S',
17715 glyph->charpos,
17716 (BUFFERP (glyph->object)
17717 ? 'B'
17718 : (STRINGP (glyph->object)
17719 ? 'S'
17720 : '-')),
17721 glyph->pixel_width,
17722 0,
17723 '.',
17724 glyph->face_id,
17725 glyph->left_box_line_p,
17726 glyph->right_box_line_p);
17727 }
17728 else if (glyph->type == IMAGE_GLYPH)
17729 {
17730 fprintf (stderr,
17731 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17732 glyph - row->glyphs[TEXT_AREA],
17733 'I',
17734 glyph->charpos,
17735 (BUFFERP (glyph->object)
17736 ? 'B'
17737 : (STRINGP (glyph->object)
17738 ? 'S'
17739 : '-')),
17740 glyph->pixel_width,
17741 glyph->u.img_id,
17742 '.',
17743 glyph->face_id,
17744 glyph->left_box_line_p,
17745 glyph->right_box_line_p);
17746 }
17747 else if (glyph->type == COMPOSITE_GLYPH)
17748 {
17749 fprintf (stderr,
17750 " %5td %4c %6"pI"d %c %3d 0x%05x",
17751 glyph - row->glyphs[TEXT_AREA],
17752 '+',
17753 glyph->charpos,
17754 (BUFFERP (glyph->object)
17755 ? 'B'
17756 : (STRINGP (glyph->object)
17757 ? 'S'
17758 : '-')),
17759 glyph->pixel_width,
17760 glyph->u.cmp.id);
17761 if (glyph->u.cmp.automatic)
17762 fprintf (stderr,
17763 "[%d-%d]",
17764 glyph->slice.cmp.from, glyph->slice.cmp.to);
17765 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17766 glyph->face_id,
17767 glyph->left_box_line_p,
17768 glyph->right_box_line_p);
17769 }
17770 }
17771
17772
17773 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17774 GLYPHS 0 means don't show glyph contents.
17775 GLYPHS 1 means show glyphs in short form
17776 GLYPHS > 1 means show glyphs in long form. */
17777
17778 void
17779 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17780 {
17781 if (glyphs != 1)
17782 {
17783 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17784 fprintf (stderr, "======================================================================\n");
17785
17786 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17787 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17788 vpos,
17789 MATRIX_ROW_START_CHARPOS (row),
17790 MATRIX_ROW_END_CHARPOS (row),
17791 row->used[TEXT_AREA],
17792 row->contains_overlapping_glyphs_p,
17793 row->enabled_p,
17794 row->truncated_on_left_p,
17795 row->truncated_on_right_p,
17796 row->continued_p,
17797 MATRIX_ROW_CONTINUATION_LINE_P (row),
17798 row->displays_text_p,
17799 row->ends_at_zv_p,
17800 row->fill_line_p,
17801 row->ends_in_middle_of_char_p,
17802 row->starts_in_middle_of_char_p,
17803 row->mouse_face_p,
17804 row->x,
17805 row->y,
17806 row->pixel_width,
17807 row->height,
17808 row->visible_height,
17809 row->ascent,
17810 row->phys_ascent);
17811 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17812 row->end.overlay_string_index,
17813 row->continuation_lines_width);
17814 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17815 CHARPOS (row->start.string_pos),
17816 CHARPOS (row->end.string_pos));
17817 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17818 row->end.dpvec_index);
17819 }
17820
17821 if (glyphs > 1)
17822 {
17823 int area;
17824
17825 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17826 {
17827 struct glyph *glyph = row->glyphs[area];
17828 struct glyph *glyph_end = glyph + row->used[area];
17829
17830 /* Glyph for a line end in text. */
17831 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17832 ++glyph_end;
17833
17834 if (glyph < glyph_end)
17835 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17836
17837 for (; glyph < glyph_end; ++glyph)
17838 dump_glyph (row, glyph, area);
17839 }
17840 }
17841 else if (glyphs == 1)
17842 {
17843 int area;
17844
17845 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17846 {
17847 char *s = (char *) alloca (row->used[area] + 1);
17848 int i;
17849
17850 for (i = 0; i < row->used[area]; ++i)
17851 {
17852 struct glyph *glyph = row->glyphs[area] + i;
17853 if (glyph->type == CHAR_GLYPH
17854 && glyph->u.ch < 0x80
17855 && glyph->u.ch >= ' ')
17856 s[i] = glyph->u.ch;
17857 else
17858 s[i] = '.';
17859 }
17860
17861 s[i] = '\0';
17862 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17863 }
17864 }
17865 }
17866
17867
17868 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17869 Sdump_glyph_matrix, 0, 1, "p",
17870 doc: /* Dump the current matrix of the selected window to stderr.
17871 Shows contents of glyph row structures. With non-nil
17872 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17873 glyphs in short form, otherwise show glyphs in long form. */)
17874 (Lisp_Object glyphs)
17875 {
17876 struct window *w = XWINDOW (selected_window);
17877 struct buffer *buffer = XBUFFER (w->buffer);
17878
17879 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17880 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17881 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17882 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17883 fprintf (stderr, "=============================================\n");
17884 dump_glyph_matrix (w->current_matrix,
17885 NILP (glyphs) ? 0 : XINT (glyphs));
17886 return Qnil;
17887 }
17888
17889
17890 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17891 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17892 (void)
17893 {
17894 struct frame *f = XFRAME (selected_frame);
17895 dump_glyph_matrix (f->current_matrix, 1);
17896 return Qnil;
17897 }
17898
17899
17900 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17901 doc: /* Dump glyph row ROW to stderr.
17902 GLYPH 0 means don't dump glyphs.
17903 GLYPH 1 means dump glyphs in short form.
17904 GLYPH > 1 or omitted means dump glyphs in long form. */)
17905 (Lisp_Object row, Lisp_Object glyphs)
17906 {
17907 struct glyph_matrix *matrix;
17908 int vpos;
17909
17910 CHECK_NUMBER (row);
17911 matrix = XWINDOW (selected_window)->current_matrix;
17912 vpos = XINT (row);
17913 if (vpos >= 0 && vpos < matrix->nrows)
17914 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17915 vpos,
17916 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17917 return Qnil;
17918 }
17919
17920
17921 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17922 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17923 GLYPH 0 means don't dump glyphs.
17924 GLYPH 1 means dump glyphs in short form.
17925 GLYPH > 1 or omitted means dump glyphs in long form. */)
17926 (Lisp_Object row, Lisp_Object glyphs)
17927 {
17928 struct frame *sf = SELECTED_FRAME ();
17929 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17930 int vpos;
17931
17932 CHECK_NUMBER (row);
17933 vpos = XINT (row);
17934 if (vpos >= 0 && vpos < m->nrows)
17935 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17936 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17937 return Qnil;
17938 }
17939
17940
17941 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17942 doc: /* Toggle tracing of redisplay.
17943 With ARG, turn tracing on if and only if ARG is positive. */)
17944 (Lisp_Object arg)
17945 {
17946 if (NILP (arg))
17947 trace_redisplay_p = !trace_redisplay_p;
17948 else
17949 {
17950 arg = Fprefix_numeric_value (arg);
17951 trace_redisplay_p = XINT (arg) > 0;
17952 }
17953
17954 return Qnil;
17955 }
17956
17957
17958 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17959 doc: /* Like `format', but print result to stderr.
17960 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17961 (ptrdiff_t nargs, Lisp_Object *args)
17962 {
17963 Lisp_Object s = Fformat (nargs, args);
17964 fprintf (stderr, "%s", SDATA (s));
17965 return Qnil;
17966 }
17967
17968 #endif /* GLYPH_DEBUG */
17969
17970
17971 \f
17972 /***********************************************************************
17973 Building Desired Matrix Rows
17974 ***********************************************************************/
17975
17976 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17977 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17978
17979 static struct glyph_row *
17980 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17981 {
17982 struct frame *f = XFRAME (WINDOW_FRAME (w));
17983 struct buffer *buffer = XBUFFER (w->buffer);
17984 struct buffer *old = current_buffer;
17985 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17986 int arrow_len = SCHARS (overlay_arrow_string);
17987 const unsigned char *arrow_end = arrow_string + arrow_len;
17988 const unsigned char *p;
17989 struct it it;
17990 int multibyte_p;
17991 int n_glyphs_before;
17992
17993 set_buffer_temp (buffer);
17994 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17995 it.glyph_row->used[TEXT_AREA] = 0;
17996 SET_TEXT_POS (it.position, 0, 0);
17997
17998 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17999 p = arrow_string;
18000 while (p < arrow_end)
18001 {
18002 Lisp_Object face, ilisp;
18003
18004 /* Get the next character. */
18005 if (multibyte_p)
18006 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18007 else
18008 {
18009 it.c = it.char_to_display = *p, it.len = 1;
18010 if (! ASCII_CHAR_P (it.c))
18011 it.char_to_display = BYTE8_TO_CHAR (it.c);
18012 }
18013 p += it.len;
18014
18015 /* Get its face. */
18016 ilisp = make_number (p - arrow_string);
18017 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18018 it.face_id = compute_char_face (f, it.char_to_display, face);
18019
18020 /* Compute its width, get its glyphs. */
18021 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18022 SET_TEXT_POS (it.position, -1, -1);
18023 PRODUCE_GLYPHS (&it);
18024
18025 /* If this character doesn't fit any more in the line, we have
18026 to remove some glyphs. */
18027 if (it.current_x > it.last_visible_x)
18028 {
18029 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18030 break;
18031 }
18032 }
18033
18034 set_buffer_temp (old);
18035 return it.glyph_row;
18036 }
18037
18038
18039 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18040 glyphs are only inserted for terminal frames since we can't really
18041 win with truncation glyphs when partially visible glyphs are
18042 involved. Which glyphs to insert is determined by
18043 produce_special_glyphs. */
18044
18045 static void
18046 insert_left_trunc_glyphs (struct it *it)
18047 {
18048 struct it truncate_it;
18049 struct glyph *from, *end, *to, *toend;
18050
18051 xassert (!FRAME_WINDOW_P (it->f));
18052
18053 /* Get the truncation glyphs. */
18054 truncate_it = *it;
18055 truncate_it.current_x = 0;
18056 truncate_it.face_id = DEFAULT_FACE_ID;
18057 truncate_it.glyph_row = &scratch_glyph_row;
18058 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18059 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18060 truncate_it.object = make_number (0);
18061 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18062
18063 /* Overwrite glyphs from IT with truncation glyphs. */
18064 if (!it->glyph_row->reversed_p)
18065 {
18066 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18067 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18068 to = it->glyph_row->glyphs[TEXT_AREA];
18069 toend = to + it->glyph_row->used[TEXT_AREA];
18070
18071 while (from < end)
18072 *to++ = *from++;
18073
18074 /* There may be padding glyphs left over. Overwrite them too. */
18075 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18076 {
18077 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18078 while (from < end)
18079 *to++ = *from++;
18080 }
18081
18082 if (to > toend)
18083 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18084 }
18085 else
18086 {
18087 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18088 that back to front. */
18089 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18090 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18091 toend = it->glyph_row->glyphs[TEXT_AREA];
18092 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18093
18094 while (from >= end && to >= toend)
18095 *to-- = *from--;
18096 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18097 {
18098 from =
18099 truncate_it.glyph_row->glyphs[TEXT_AREA]
18100 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18101 while (from >= end && to >= toend)
18102 *to-- = *from--;
18103 }
18104 if (from >= end)
18105 {
18106 /* Need to free some room before prepending additional
18107 glyphs. */
18108 int move_by = from - end + 1;
18109 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18110 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18111
18112 for ( ; g >= g0; g--)
18113 g[move_by] = *g;
18114 while (from >= end)
18115 *to-- = *from--;
18116 it->glyph_row->used[TEXT_AREA] += move_by;
18117 }
18118 }
18119 }
18120
18121 /* Compute the hash code for ROW. */
18122 unsigned
18123 row_hash (struct glyph_row *row)
18124 {
18125 int area, k;
18126 unsigned hashval = 0;
18127
18128 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18129 for (k = 0; k < row->used[area]; ++k)
18130 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18131 + row->glyphs[area][k].u.val
18132 + row->glyphs[area][k].face_id
18133 + row->glyphs[area][k].padding_p
18134 + (row->glyphs[area][k].type << 2));
18135
18136 return hashval;
18137 }
18138
18139 /* Compute the pixel height and width of IT->glyph_row.
18140
18141 Most of the time, ascent and height of a display line will be equal
18142 to the max_ascent and max_height values of the display iterator
18143 structure. This is not the case if
18144
18145 1. We hit ZV without displaying anything. In this case, max_ascent
18146 and max_height will be zero.
18147
18148 2. We have some glyphs that don't contribute to the line height.
18149 (The glyph row flag contributes_to_line_height_p is for future
18150 pixmap extensions).
18151
18152 The first case is easily covered by using default values because in
18153 these cases, the line height does not really matter, except that it
18154 must not be zero. */
18155
18156 static void
18157 compute_line_metrics (struct it *it)
18158 {
18159 struct glyph_row *row = it->glyph_row;
18160
18161 if (FRAME_WINDOW_P (it->f))
18162 {
18163 int i, min_y, max_y;
18164
18165 /* The line may consist of one space only, that was added to
18166 place the cursor on it. If so, the row's height hasn't been
18167 computed yet. */
18168 if (row->height == 0)
18169 {
18170 if (it->max_ascent + it->max_descent == 0)
18171 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18172 row->ascent = it->max_ascent;
18173 row->height = it->max_ascent + it->max_descent;
18174 row->phys_ascent = it->max_phys_ascent;
18175 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18176 row->extra_line_spacing = it->max_extra_line_spacing;
18177 }
18178
18179 /* Compute the width of this line. */
18180 row->pixel_width = row->x;
18181 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18182 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18183
18184 xassert (row->pixel_width >= 0);
18185 xassert (row->ascent >= 0 && row->height > 0);
18186
18187 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18188 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18189
18190 /* If first line's physical ascent is larger than its logical
18191 ascent, use the physical ascent, and make the row taller.
18192 This makes accented characters fully visible. */
18193 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18194 && row->phys_ascent > row->ascent)
18195 {
18196 row->height += row->phys_ascent - row->ascent;
18197 row->ascent = row->phys_ascent;
18198 }
18199
18200 /* Compute how much of the line is visible. */
18201 row->visible_height = row->height;
18202
18203 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18204 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18205
18206 if (row->y < min_y)
18207 row->visible_height -= min_y - row->y;
18208 if (row->y + row->height > max_y)
18209 row->visible_height -= row->y + row->height - max_y;
18210 }
18211 else
18212 {
18213 row->pixel_width = row->used[TEXT_AREA];
18214 if (row->continued_p)
18215 row->pixel_width -= it->continuation_pixel_width;
18216 else if (row->truncated_on_right_p)
18217 row->pixel_width -= it->truncation_pixel_width;
18218 row->ascent = row->phys_ascent = 0;
18219 row->height = row->phys_height = row->visible_height = 1;
18220 row->extra_line_spacing = 0;
18221 }
18222
18223 /* Compute a hash code for this row. */
18224 row->hash = row_hash (row);
18225
18226 it->max_ascent = it->max_descent = 0;
18227 it->max_phys_ascent = it->max_phys_descent = 0;
18228 }
18229
18230
18231 /* Append one space to the glyph row of iterator IT if doing a
18232 window-based redisplay. The space has the same face as
18233 IT->face_id. Value is non-zero if a space was added.
18234
18235 This function is called to make sure that there is always one glyph
18236 at the end of a glyph row that the cursor can be set on under
18237 window-systems. (If there weren't such a glyph we would not know
18238 how wide and tall a box cursor should be displayed).
18239
18240 At the same time this space let's a nicely handle clearing to the
18241 end of the line if the row ends in italic text. */
18242
18243 static int
18244 append_space_for_newline (struct it *it, int default_face_p)
18245 {
18246 if (FRAME_WINDOW_P (it->f))
18247 {
18248 int n = it->glyph_row->used[TEXT_AREA];
18249
18250 if (it->glyph_row->glyphs[TEXT_AREA] + n
18251 < it->glyph_row->glyphs[1 + TEXT_AREA])
18252 {
18253 /* Save some values that must not be changed.
18254 Must save IT->c and IT->len because otherwise
18255 ITERATOR_AT_END_P wouldn't work anymore after
18256 append_space_for_newline has been called. */
18257 enum display_element_type saved_what = it->what;
18258 int saved_c = it->c, saved_len = it->len;
18259 int saved_char_to_display = it->char_to_display;
18260 int saved_x = it->current_x;
18261 int saved_face_id = it->face_id;
18262 struct text_pos saved_pos;
18263 Lisp_Object saved_object;
18264 struct face *face;
18265
18266 saved_object = it->object;
18267 saved_pos = it->position;
18268
18269 it->what = IT_CHARACTER;
18270 memset (&it->position, 0, sizeof it->position);
18271 it->object = make_number (0);
18272 it->c = it->char_to_display = ' ';
18273 it->len = 1;
18274
18275 /* If the default face was remapped, be sure to use the
18276 remapped face for the appended newline. */
18277 if (default_face_p)
18278 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18279 else if (it->face_before_selective_p)
18280 it->face_id = it->saved_face_id;
18281 face = FACE_FROM_ID (it->f, it->face_id);
18282 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18283
18284 PRODUCE_GLYPHS (it);
18285
18286 it->override_ascent = -1;
18287 it->constrain_row_ascent_descent_p = 0;
18288 it->current_x = saved_x;
18289 it->object = saved_object;
18290 it->position = saved_pos;
18291 it->what = saved_what;
18292 it->face_id = saved_face_id;
18293 it->len = saved_len;
18294 it->c = saved_c;
18295 it->char_to_display = saved_char_to_display;
18296 return 1;
18297 }
18298 }
18299
18300 return 0;
18301 }
18302
18303
18304 /* Extend the face of the last glyph in the text area of IT->glyph_row
18305 to the end of the display line. Called from display_line. If the
18306 glyph row is empty, add a space glyph to it so that we know the
18307 face to draw. Set the glyph row flag fill_line_p. If the glyph
18308 row is R2L, prepend a stretch glyph to cover the empty space to the
18309 left of the leftmost glyph. */
18310
18311 static void
18312 extend_face_to_end_of_line (struct it *it)
18313 {
18314 struct face *face, *default_face;
18315 struct frame *f = it->f;
18316
18317 /* If line is already filled, do nothing. Non window-system frames
18318 get a grace of one more ``pixel'' because their characters are
18319 1-``pixel'' wide, so they hit the equality too early. This grace
18320 is needed only for R2L rows that are not continued, to produce
18321 one extra blank where we could display the cursor. */
18322 if (it->current_x >= it->last_visible_x
18323 + (!FRAME_WINDOW_P (f)
18324 && it->glyph_row->reversed_p
18325 && !it->glyph_row->continued_p))
18326 return;
18327
18328 /* The default face, possibly remapped. */
18329 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18330
18331 /* Face extension extends the background and box of IT->face_id
18332 to the end of the line. If the background equals the background
18333 of the frame, we don't have to do anything. */
18334 if (it->face_before_selective_p)
18335 face = FACE_FROM_ID (f, it->saved_face_id);
18336 else
18337 face = FACE_FROM_ID (f, it->face_id);
18338
18339 if (FRAME_WINDOW_P (f)
18340 && it->glyph_row->displays_text_p
18341 && face->box == FACE_NO_BOX
18342 && face->background == FRAME_BACKGROUND_PIXEL (f)
18343 && !face->stipple
18344 && !it->glyph_row->reversed_p)
18345 return;
18346
18347 /* Set the glyph row flag indicating that the face of the last glyph
18348 in the text area has to be drawn to the end of the text area. */
18349 it->glyph_row->fill_line_p = 1;
18350
18351 /* If current character of IT is not ASCII, make sure we have the
18352 ASCII face. This will be automatically undone the next time
18353 get_next_display_element returns a multibyte character. Note
18354 that the character will always be single byte in unibyte
18355 text. */
18356 if (!ASCII_CHAR_P (it->c))
18357 {
18358 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18359 }
18360
18361 if (FRAME_WINDOW_P (f))
18362 {
18363 /* If the row is empty, add a space with the current face of IT,
18364 so that we know which face to draw. */
18365 if (it->glyph_row->used[TEXT_AREA] == 0)
18366 {
18367 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18368 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18369 it->glyph_row->used[TEXT_AREA] = 1;
18370 }
18371 #ifdef HAVE_WINDOW_SYSTEM
18372 if (it->glyph_row->reversed_p)
18373 {
18374 /* Prepend a stretch glyph to the row, such that the
18375 rightmost glyph will be drawn flushed all the way to the
18376 right margin of the window. The stretch glyph that will
18377 occupy the empty space, if any, to the left of the
18378 glyphs. */
18379 struct font *font = face->font ? face->font : FRAME_FONT (f);
18380 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18381 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18382 struct glyph *g;
18383 int row_width, stretch_ascent, stretch_width;
18384 struct text_pos saved_pos;
18385 int saved_face_id, saved_avoid_cursor;
18386
18387 for (row_width = 0, g = row_start; g < row_end; g++)
18388 row_width += g->pixel_width;
18389 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18390 if (stretch_width > 0)
18391 {
18392 stretch_ascent =
18393 (((it->ascent + it->descent)
18394 * FONT_BASE (font)) / FONT_HEIGHT (font));
18395 saved_pos = it->position;
18396 memset (&it->position, 0, sizeof it->position);
18397 saved_avoid_cursor = it->avoid_cursor_p;
18398 it->avoid_cursor_p = 1;
18399 saved_face_id = it->face_id;
18400 /* The last row's stretch glyph should get the default
18401 face, to avoid painting the rest of the window with
18402 the region face, if the region ends at ZV. */
18403 if (it->glyph_row->ends_at_zv_p)
18404 it->face_id = default_face->id;
18405 else
18406 it->face_id = face->id;
18407 append_stretch_glyph (it, make_number (0), stretch_width,
18408 it->ascent + it->descent, stretch_ascent);
18409 it->position = saved_pos;
18410 it->avoid_cursor_p = saved_avoid_cursor;
18411 it->face_id = saved_face_id;
18412 }
18413 }
18414 #endif /* HAVE_WINDOW_SYSTEM */
18415 }
18416 else
18417 {
18418 /* Save some values that must not be changed. */
18419 int saved_x = it->current_x;
18420 struct text_pos saved_pos;
18421 Lisp_Object saved_object;
18422 enum display_element_type saved_what = it->what;
18423 int saved_face_id = it->face_id;
18424
18425 saved_object = it->object;
18426 saved_pos = it->position;
18427
18428 it->what = IT_CHARACTER;
18429 memset (&it->position, 0, sizeof it->position);
18430 it->object = make_number (0);
18431 it->c = it->char_to_display = ' ';
18432 it->len = 1;
18433 /* The last row's blank glyphs should get the default face, to
18434 avoid painting the rest of the window with the region face,
18435 if the region ends at ZV. */
18436 if (it->glyph_row->ends_at_zv_p)
18437 it->face_id = default_face->id;
18438 else
18439 it->face_id = face->id;
18440
18441 PRODUCE_GLYPHS (it);
18442
18443 while (it->current_x <= it->last_visible_x)
18444 PRODUCE_GLYPHS (it);
18445
18446 /* Don't count these blanks really. It would let us insert a left
18447 truncation glyph below and make us set the cursor on them, maybe. */
18448 it->current_x = saved_x;
18449 it->object = saved_object;
18450 it->position = saved_pos;
18451 it->what = saved_what;
18452 it->face_id = saved_face_id;
18453 }
18454 }
18455
18456
18457 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18458 trailing whitespace. */
18459
18460 static int
18461 trailing_whitespace_p (EMACS_INT charpos)
18462 {
18463 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18464 int c = 0;
18465
18466 while (bytepos < ZV_BYTE
18467 && (c = FETCH_CHAR (bytepos),
18468 c == ' ' || c == '\t'))
18469 ++bytepos;
18470
18471 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18472 {
18473 if (bytepos != PT_BYTE)
18474 return 1;
18475 }
18476 return 0;
18477 }
18478
18479
18480 /* Highlight trailing whitespace, if any, in ROW. */
18481
18482 static void
18483 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18484 {
18485 int used = row->used[TEXT_AREA];
18486
18487 if (used)
18488 {
18489 struct glyph *start = row->glyphs[TEXT_AREA];
18490 struct glyph *glyph = start + used - 1;
18491
18492 if (row->reversed_p)
18493 {
18494 /* Right-to-left rows need to be processed in the opposite
18495 direction, so swap the edge pointers. */
18496 glyph = start;
18497 start = row->glyphs[TEXT_AREA] + used - 1;
18498 }
18499
18500 /* Skip over glyphs inserted to display the cursor at the
18501 end of a line, for extending the face of the last glyph
18502 to the end of the line on terminals, and for truncation
18503 and continuation glyphs. */
18504 if (!row->reversed_p)
18505 {
18506 while (glyph >= start
18507 && glyph->type == CHAR_GLYPH
18508 && INTEGERP (glyph->object))
18509 --glyph;
18510 }
18511 else
18512 {
18513 while (glyph <= start
18514 && glyph->type == CHAR_GLYPH
18515 && INTEGERP (glyph->object))
18516 ++glyph;
18517 }
18518
18519 /* If last glyph is a space or stretch, and it's trailing
18520 whitespace, set the face of all trailing whitespace glyphs in
18521 IT->glyph_row to `trailing-whitespace'. */
18522 if ((row->reversed_p ? glyph <= start : glyph >= start)
18523 && BUFFERP (glyph->object)
18524 && (glyph->type == STRETCH_GLYPH
18525 || (glyph->type == CHAR_GLYPH
18526 && glyph->u.ch == ' '))
18527 && trailing_whitespace_p (glyph->charpos))
18528 {
18529 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18530 if (face_id < 0)
18531 return;
18532
18533 if (!row->reversed_p)
18534 {
18535 while (glyph >= start
18536 && BUFFERP (glyph->object)
18537 && (glyph->type == STRETCH_GLYPH
18538 || (glyph->type == CHAR_GLYPH
18539 && glyph->u.ch == ' ')))
18540 (glyph--)->face_id = face_id;
18541 }
18542 else
18543 {
18544 while (glyph <= start
18545 && BUFFERP (glyph->object)
18546 && (glyph->type == STRETCH_GLYPH
18547 || (glyph->type == CHAR_GLYPH
18548 && glyph->u.ch == ' ')))
18549 (glyph++)->face_id = face_id;
18550 }
18551 }
18552 }
18553 }
18554
18555
18556 /* Value is non-zero if glyph row ROW should be
18557 used to hold the cursor. */
18558
18559 static int
18560 cursor_row_p (struct glyph_row *row)
18561 {
18562 int result = 1;
18563
18564 if (PT == CHARPOS (row->end.pos)
18565 || PT == MATRIX_ROW_END_CHARPOS (row))
18566 {
18567 /* Suppose the row ends on a string.
18568 Unless the row is continued, that means it ends on a newline
18569 in the string. If it's anything other than a display string
18570 (e.g., a before-string from an overlay), we don't want the
18571 cursor there. (This heuristic seems to give the optimal
18572 behavior for the various types of multi-line strings.)
18573 One exception: if the string has `cursor' property on one of
18574 its characters, we _do_ want the cursor there. */
18575 if (CHARPOS (row->end.string_pos) >= 0)
18576 {
18577 if (row->continued_p)
18578 result = 1;
18579 else
18580 {
18581 /* Check for `display' property. */
18582 struct glyph *beg = row->glyphs[TEXT_AREA];
18583 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18584 struct glyph *glyph;
18585
18586 result = 0;
18587 for (glyph = end; glyph >= beg; --glyph)
18588 if (STRINGP (glyph->object))
18589 {
18590 Lisp_Object prop
18591 = Fget_char_property (make_number (PT),
18592 Qdisplay, Qnil);
18593 result =
18594 (!NILP (prop)
18595 && display_prop_string_p (prop, glyph->object));
18596 /* If there's a `cursor' property on one of the
18597 string's characters, this row is a cursor row,
18598 even though this is not a display string. */
18599 if (!result)
18600 {
18601 Lisp_Object s = glyph->object;
18602
18603 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18604 {
18605 EMACS_INT gpos = glyph->charpos;
18606
18607 if (!NILP (Fget_char_property (make_number (gpos),
18608 Qcursor, s)))
18609 {
18610 result = 1;
18611 break;
18612 }
18613 }
18614 }
18615 break;
18616 }
18617 }
18618 }
18619 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18620 {
18621 /* If the row ends in middle of a real character,
18622 and the line is continued, we want the cursor here.
18623 That's because CHARPOS (ROW->end.pos) would equal
18624 PT if PT is before the character. */
18625 if (!row->ends_in_ellipsis_p)
18626 result = row->continued_p;
18627 else
18628 /* If the row ends in an ellipsis, then
18629 CHARPOS (ROW->end.pos) will equal point after the
18630 invisible text. We want that position to be displayed
18631 after the ellipsis. */
18632 result = 0;
18633 }
18634 /* If the row ends at ZV, display the cursor at the end of that
18635 row instead of at the start of the row below. */
18636 else if (row->ends_at_zv_p)
18637 result = 1;
18638 else
18639 result = 0;
18640 }
18641
18642 return result;
18643 }
18644
18645 \f
18646
18647 /* Push the property PROP so that it will be rendered at the current
18648 position in IT. Return 1 if PROP was successfully pushed, 0
18649 otherwise. Called from handle_line_prefix to handle the
18650 `line-prefix' and `wrap-prefix' properties. */
18651
18652 static int
18653 push_prefix_prop (struct it *it, Lisp_Object prop)
18654 {
18655 struct text_pos pos =
18656 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18657
18658 xassert (it->method == GET_FROM_BUFFER
18659 || it->method == GET_FROM_DISPLAY_VECTOR
18660 || it->method == GET_FROM_STRING);
18661
18662 /* We need to save the current buffer/string position, so it will be
18663 restored by pop_it, because iterate_out_of_display_property
18664 depends on that being set correctly, but some situations leave
18665 it->position not yet set when this function is called. */
18666 push_it (it, &pos);
18667
18668 if (STRINGP (prop))
18669 {
18670 if (SCHARS (prop) == 0)
18671 {
18672 pop_it (it);
18673 return 0;
18674 }
18675
18676 it->string = prop;
18677 it->string_from_prefix_prop_p = 1;
18678 it->multibyte_p = STRING_MULTIBYTE (it->string);
18679 it->current.overlay_string_index = -1;
18680 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18681 it->end_charpos = it->string_nchars = SCHARS (it->string);
18682 it->method = GET_FROM_STRING;
18683 it->stop_charpos = 0;
18684 it->prev_stop = 0;
18685 it->base_level_stop = 0;
18686
18687 /* Force paragraph direction to be that of the parent
18688 buffer/string. */
18689 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18690 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18691 else
18692 it->paragraph_embedding = L2R;
18693
18694 /* Set up the bidi iterator for this display string. */
18695 if (it->bidi_p)
18696 {
18697 it->bidi_it.string.lstring = it->string;
18698 it->bidi_it.string.s = NULL;
18699 it->bidi_it.string.schars = it->end_charpos;
18700 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18701 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18702 it->bidi_it.string.unibyte = !it->multibyte_p;
18703 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18704 }
18705 }
18706 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18707 {
18708 it->method = GET_FROM_STRETCH;
18709 it->object = prop;
18710 }
18711 #ifdef HAVE_WINDOW_SYSTEM
18712 else if (IMAGEP (prop))
18713 {
18714 it->what = IT_IMAGE;
18715 it->image_id = lookup_image (it->f, prop);
18716 it->method = GET_FROM_IMAGE;
18717 }
18718 #endif /* HAVE_WINDOW_SYSTEM */
18719 else
18720 {
18721 pop_it (it); /* bogus display property, give up */
18722 return 0;
18723 }
18724
18725 return 1;
18726 }
18727
18728 /* Return the character-property PROP at the current position in IT. */
18729
18730 static Lisp_Object
18731 get_it_property (struct it *it, Lisp_Object prop)
18732 {
18733 Lisp_Object position;
18734
18735 if (STRINGP (it->object))
18736 position = make_number (IT_STRING_CHARPOS (*it));
18737 else if (BUFFERP (it->object))
18738 position = make_number (IT_CHARPOS (*it));
18739 else
18740 return Qnil;
18741
18742 return Fget_char_property (position, prop, it->object);
18743 }
18744
18745 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18746
18747 static void
18748 handle_line_prefix (struct it *it)
18749 {
18750 Lisp_Object prefix;
18751
18752 if (it->continuation_lines_width > 0)
18753 {
18754 prefix = get_it_property (it, Qwrap_prefix);
18755 if (NILP (prefix))
18756 prefix = Vwrap_prefix;
18757 }
18758 else
18759 {
18760 prefix = get_it_property (it, Qline_prefix);
18761 if (NILP (prefix))
18762 prefix = Vline_prefix;
18763 }
18764 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18765 {
18766 /* If the prefix is wider than the window, and we try to wrap
18767 it, it would acquire its own wrap prefix, and so on till the
18768 iterator stack overflows. So, don't wrap the prefix. */
18769 it->line_wrap = TRUNCATE;
18770 it->avoid_cursor_p = 1;
18771 }
18772 }
18773
18774 \f
18775
18776 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18777 only for R2L lines from display_line and display_string, when they
18778 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18779 the line/string needs to be continued on the next glyph row. */
18780 static void
18781 unproduce_glyphs (struct it *it, int n)
18782 {
18783 struct glyph *glyph, *end;
18784
18785 xassert (it->glyph_row);
18786 xassert (it->glyph_row->reversed_p);
18787 xassert (it->area == TEXT_AREA);
18788 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18789
18790 if (n > it->glyph_row->used[TEXT_AREA])
18791 n = it->glyph_row->used[TEXT_AREA];
18792 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18793 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18794 for ( ; glyph < end; glyph++)
18795 glyph[-n] = *glyph;
18796 }
18797
18798 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18799 and ROW->maxpos. */
18800 static void
18801 find_row_edges (struct it *it, struct glyph_row *row,
18802 EMACS_INT min_pos, EMACS_INT min_bpos,
18803 EMACS_INT max_pos, EMACS_INT max_bpos)
18804 {
18805 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18806 lines' rows is implemented for bidi-reordered rows. */
18807
18808 /* ROW->minpos is the value of min_pos, the minimal buffer position
18809 we have in ROW, or ROW->start.pos if that is smaller. */
18810 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18811 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18812 else
18813 /* We didn't find buffer positions smaller than ROW->start, or
18814 didn't find _any_ valid buffer positions in any of the glyphs,
18815 so we must trust the iterator's computed positions. */
18816 row->minpos = row->start.pos;
18817 if (max_pos <= 0)
18818 {
18819 max_pos = CHARPOS (it->current.pos);
18820 max_bpos = BYTEPOS (it->current.pos);
18821 }
18822
18823 /* Here are the various use-cases for ending the row, and the
18824 corresponding values for ROW->maxpos:
18825
18826 Line ends in a newline from buffer eol_pos + 1
18827 Line is continued from buffer max_pos + 1
18828 Line is truncated on right it->current.pos
18829 Line ends in a newline from string max_pos + 1(*)
18830 (*) + 1 only when line ends in a forward scan
18831 Line is continued from string max_pos
18832 Line is continued from display vector max_pos
18833 Line is entirely from a string min_pos == max_pos
18834 Line is entirely from a display vector min_pos == max_pos
18835 Line that ends at ZV ZV
18836
18837 If you discover other use-cases, please add them here as
18838 appropriate. */
18839 if (row->ends_at_zv_p)
18840 row->maxpos = it->current.pos;
18841 else if (row->used[TEXT_AREA])
18842 {
18843 int seen_this_string = 0;
18844 struct glyph_row *r1 = row - 1;
18845
18846 /* Did we see the same display string on the previous row? */
18847 if (STRINGP (it->object)
18848 /* this is not the first row */
18849 && row > it->w->desired_matrix->rows
18850 /* previous row is not the header line */
18851 && !r1->mode_line_p
18852 /* previous row also ends in a newline from a string */
18853 && r1->ends_in_newline_from_string_p)
18854 {
18855 struct glyph *start, *end;
18856
18857 /* Search for the last glyph of the previous row that came
18858 from buffer or string. Depending on whether the row is
18859 L2R or R2L, we need to process it front to back or the
18860 other way round. */
18861 if (!r1->reversed_p)
18862 {
18863 start = r1->glyphs[TEXT_AREA];
18864 end = start + r1->used[TEXT_AREA];
18865 /* Glyphs inserted by redisplay have an integer (zero)
18866 as their object. */
18867 while (end > start
18868 && INTEGERP ((end - 1)->object)
18869 && (end - 1)->charpos <= 0)
18870 --end;
18871 if (end > start)
18872 {
18873 if (EQ ((end - 1)->object, it->object))
18874 seen_this_string = 1;
18875 }
18876 else
18877 /* If all the glyphs of the previous row were inserted
18878 by redisplay, it means the previous row was
18879 produced from a single newline, which is only
18880 possible if that newline came from the same string
18881 as the one which produced this ROW. */
18882 seen_this_string = 1;
18883 }
18884 else
18885 {
18886 end = r1->glyphs[TEXT_AREA] - 1;
18887 start = end + r1->used[TEXT_AREA];
18888 while (end < start
18889 && INTEGERP ((end + 1)->object)
18890 && (end + 1)->charpos <= 0)
18891 ++end;
18892 if (end < start)
18893 {
18894 if (EQ ((end + 1)->object, it->object))
18895 seen_this_string = 1;
18896 }
18897 else
18898 seen_this_string = 1;
18899 }
18900 }
18901 /* Take note of each display string that covers a newline only
18902 once, the first time we see it. This is for when a display
18903 string includes more than one newline in it. */
18904 if (row->ends_in_newline_from_string_p && !seen_this_string)
18905 {
18906 /* If we were scanning the buffer forward when we displayed
18907 the string, we want to account for at least one buffer
18908 position that belongs to this row (position covered by
18909 the display string), so that cursor positioning will
18910 consider this row as a candidate when point is at the end
18911 of the visual line represented by this row. This is not
18912 required when scanning back, because max_pos will already
18913 have a much larger value. */
18914 if (CHARPOS (row->end.pos) > max_pos)
18915 INC_BOTH (max_pos, max_bpos);
18916 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18917 }
18918 else if (CHARPOS (it->eol_pos) > 0)
18919 SET_TEXT_POS (row->maxpos,
18920 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18921 else if (row->continued_p)
18922 {
18923 /* If max_pos is different from IT's current position, it
18924 means IT->method does not belong to the display element
18925 at max_pos. However, it also means that the display
18926 element at max_pos was displayed in its entirety on this
18927 line, which is equivalent to saying that the next line
18928 starts at the next buffer position. */
18929 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18930 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18931 else
18932 {
18933 INC_BOTH (max_pos, max_bpos);
18934 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18935 }
18936 }
18937 else if (row->truncated_on_right_p)
18938 /* display_line already called reseat_at_next_visible_line_start,
18939 which puts the iterator at the beginning of the next line, in
18940 the logical order. */
18941 row->maxpos = it->current.pos;
18942 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18943 /* A line that is entirely from a string/image/stretch... */
18944 row->maxpos = row->minpos;
18945 else
18946 abort ();
18947 }
18948 else
18949 row->maxpos = it->current.pos;
18950 }
18951
18952 /* Construct the glyph row IT->glyph_row in the desired matrix of
18953 IT->w from text at the current position of IT. See dispextern.h
18954 for an overview of struct it. Value is non-zero if
18955 IT->glyph_row displays text, as opposed to a line displaying ZV
18956 only. */
18957
18958 static int
18959 display_line (struct it *it)
18960 {
18961 struct glyph_row *row = it->glyph_row;
18962 Lisp_Object overlay_arrow_string;
18963 struct it wrap_it;
18964 void *wrap_data = NULL;
18965 int may_wrap = 0, wrap_x IF_LINT (= 0);
18966 int wrap_row_used = -1;
18967 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18968 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18969 int wrap_row_extra_line_spacing IF_LINT (= 0);
18970 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18971 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18972 int cvpos;
18973 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18974 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18975
18976 /* We always start displaying at hpos zero even if hscrolled. */
18977 xassert (it->hpos == 0 && it->current_x == 0);
18978
18979 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18980 >= it->w->desired_matrix->nrows)
18981 {
18982 it->w->nrows_scale_factor++;
18983 fonts_changed_p = 1;
18984 return 0;
18985 }
18986
18987 /* Is IT->w showing the region? */
18988 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18989
18990 /* Clear the result glyph row and enable it. */
18991 prepare_desired_row (row);
18992
18993 row->y = it->current_y;
18994 row->start = it->start;
18995 row->continuation_lines_width = it->continuation_lines_width;
18996 row->displays_text_p = 1;
18997 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18998 it->starts_in_middle_of_char_p = 0;
18999
19000 /* Arrange the overlays nicely for our purposes. Usually, we call
19001 display_line on only one line at a time, in which case this
19002 can't really hurt too much, or we call it on lines which appear
19003 one after another in the buffer, in which case all calls to
19004 recenter_overlay_lists but the first will be pretty cheap. */
19005 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19006
19007 /* Move over display elements that are not visible because we are
19008 hscrolled. This may stop at an x-position < IT->first_visible_x
19009 if the first glyph is partially visible or if we hit a line end. */
19010 if (it->current_x < it->first_visible_x)
19011 {
19012 this_line_min_pos = row->start.pos;
19013 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19014 MOVE_TO_POS | MOVE_TO_X);
19015 /* Record the smallest positions seen while we moved over
19016 display elements that are not visible. This is needed by
19017 redisplay_internal for optimizing the case where the cursor
19018 stays inside the same line. The rest of this function only
19019 considers positions that are actually displayed, so
19020 RECORD_MAX_MIN_POS will not otherwise record positions that
19021 are hscrolled to the left of the left edge of the window. */
19022 min_pos = CHARPOS (this_line_min_pos);
19023 min_bpos = BYTEPOS (this_line_min_pos);
19024 }
19025 else
19026 {
19027 /* We only do this when not calling `move_it_in_display_line_to'
19028 above, because move_it_in_display_line_to calls
19029 handle_line_prefix itself. */
19030 handle_line_prefix (it);
19031 }
19032
19033 /* Get the initial row height. This is either the height of the
19034 text hscrolled, if there is any, or zero. */
19035 row->ascent = it->max_ascent;
19036 row->height = it->max_ascent + it->max_descent;
19037 row->phys_ascent = it->max_phys_ascent;
19038 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19039 row->extra_line_spacing = it->max_extra_line_spacing;
19040
19041 /* Utility macro to record max and min buffer positions seen until now. */
19042 #define RECORD_MAX_MIN_POS(IT) \
19043 do \
19044 { \
19045 int composition_p = !STRINGP ((IT)->string) \
19046 && ((IT)->what == IT_COMPOSITION); \
19047 EMACS_INT current_pos = \
19048 composition_p ? (IT)->cmp_it.charpos \
19049 : IT_CHARPOS (*(IT)); \
19050 EMACS_INT current_bpos = \
19051 composition_p ? CHAR_TO_BYTE (current_pos) \
19052 : IT_BYTEPOS (*(IT)); \
19053 if (current_pos < min_pos) \
19054 { \
19055 min_pos = current_pos; \
19056 min_bpos = current_bpos; \
19057 } \
19058 if (IT_CHARPOS (*it) > max_pos) \
19059 { \
19060 max_pos = IT_CHARPOS (*it); \
19061 max_bpos = IT_BYTEPOS (*it); \
19062 } \
19063 } \
19064 while (0)
19065
19066 /* Loop generating characters. The loop is left with IT on the next
19067 character to display. */
19068 while (1)
19069 {
19070 int n_glyphs_before, hpos_before, x_before;
19071 int x, nglyphs;
19072 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19073
19074 /* Retrieve the next thing to display. Value is zero if end of
19075 buffer reached. */
19076 if (!get_next_display_element (it))
19077 {
19078 /* Maybe add a space at the end of this line that is used to
19079 display the cursor there under X. Set the charpos of the
19080 first glyph of blank lines not corresponding to any text
19081 to -1. */
19082 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19083 row->exact_window_width_line_p = 1;
19084 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19085 || row->used[TEXT_AREA] == 0)
19086 {
19087 row->glyphs[TEXT_AREA]->charpos = -1;
19088 row->displays_text_p = 0;
19089
19090 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19091 && (!MINI_WINDOW_P (it->w)
19092 || (minibuf_level && EQ (it->window, minibuf_window))))
19093 row->indicate_empty_line_p = 1;
19094 }
19095
19096 it->continuation_lines_width = 0;
19097 row->ends_at_zv_p = 1;
19098 /* A row that displays right-to-left text must always have
19099 its last face extended all the way to the end of line,
19100 even if this row ends in ZV, because we still write to
19101 the screen left to right. We also need to extend the
19102 last face if the default face is remapped to some
19103 different face, otherwise the functions that clear
19104 portions of the screen will clear with the default face's
19105 background color. */
19106 if (row->reversed_p
19107 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19108 extend_face_to_end_of_line (it);
19109 break;
19110 }
19111
19112 /* Now, get the metrics of what we want to display. This also
19113 generates glyphs in `row' (which is IT->glyph_row). */
19114 n_glyphs_before = row->used[TEXT_AREA];
19115 x = it->current_x;
19116
19117 /* Remember the line height so far in case the next element doesn't
19118 fit on the line. */
19119 if (it->line_wrap != TRUNCATE)
19120 {
19121 ascent = it->max_ascent;
19122 descent = it->max_descent;
19123 phys_ascent = it->max_phys_ascent;
19124 phys_descent = it->max_phys_descent;
19125
19126 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19127 {
19128 if (IT_DISPLAYING_WHITESPACE (it))
19129 may_wrap = 1;
19130 else if (may_wrap)
19131 {
19132 SAVE_IT (wrap_it, *it, wrap_data);
19133 wrap_x = x;
19134 wrap_row_used = row->used[TEXT_AREA];
19135 wrap_row_ascent = row->ascent;
19136 wrap_row_height = row->height;
19137 wrap_row_phys_ascent = row->phys_ascent;
19138 wrap_row_phys_height = row->phys_height;
19139 wrap_row_extra_line_spacing = row->extra_line_spacing;
19140 wrap_row_min_pos = min_pos;
19141 wrap_row_min_bpos = min_bpos;
19142 wrap_row_max_pos = max_pos;
19143 wrap_row_max_bpos = max_bpos;
19144 may_wrap = 0;
19145 }
19146 }
19147 }
19148
19149 PRODUCE_GLYPHS (it);
19150
19151 /* If this display element was in marginal areas, continue with
19152 the next one. */
19153 if (it->area != TEXT_AREA)
19154 {
19155 row->ascent = max (row->ascent, it->max_ascent);
19156 row->height = max (row->height, it->max_ascent + it->max_descent);
19157 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19158 row->phys_height = max (row->phys_height,
19159 it->max_phys_ascent + it->max_phys_descent);
19160 row->extra_line_spacing = max (row->extra_line_spacing,
19161 it->max_extra_line_spacing);
19162 set_iterator_to_next (it, 1);
19163 continue;
19164 }
19165
19166 /* Does the display element fit on the line? If we truncate
19167 lines, we should draw past the right edge of the window. If
19168 we don't truncate, we want to stop so that we can display the
19169 continuation glyph before the right margin. If lines are
19170 continued, there are two possible strategies for characters
19171 resulting in more than 1 glyph (e.g. tabs): Display as many
19172 glyphs as possible in this line and leave the rest for the
19173 continuation line, or display the whole element in the next
19174 line. Original redisplay did the former, so we do it also. */
19175 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19176 hpos_before = it->hpos;
19177 x_before = x;
19178
19179 if (/* Not a newline. */
19180 nglyphs > 0
19181 /* Glyphs produced fit entirely in the line. */
19182 && it->current_x < it->last_visible_x)
19183 {
19184 it->hpos += nglyphs;
19185 row->ascent = max (row->ascent, it->max_ascent);
19186 row->height = max (row->height, it->max_ascent + it->max_descent);
19187 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19188 row->phys_height = max (row->phys_height,
19189 it->max_phys_ascent + it->max_phys_descent);
19190 row->extra_line_spacing = max (row->extra_line_spacing,
19191 it->max_extra_line_spacing);
19192 if (it->current_x - it->pixel_width < it->first_visible_x)
19193 row->x = x - it->first_visible_x;
19194 /* Record the maximum and minimum buffer positions seen so
19195 far in glyphs that will be displayed by this row. */
19196 if (it->bidi_p)
19197 RECORD_MAX_MIN_POS (it);
19198 }
19199 else
19200 {
19201 int i, new_x;
19202 struct glyph *glyph;
19203
19204 for (i = 0; i < nglyphs; ++i, x = new_x)
19205 {
19206 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19207 new_x = x + glyph->pixel_width;
19208
19209 if (/* Lines are continued. */
19210 it->line_wrap != TRUNCATE
19211 && (/* Glyph doesn't fit on the line. */
19212 new_x > it->last_visible_x
19213 /* Or it fits exactly on a window system frame. */
19214 || (new_x == it->last_visible_x
19215 && FRAME_WINDOW_P (it->f))))
19216 {
19217 /* End of a continued line. */
19218
19219 if (it->hpos == 0
19220 || (new_x == it->last_visible_x
19221 && FRAME_WINDOW_P (it->f)))
19222 {
19223 /* Current glyph is the only one on the line or
19224 fits exactly on the line. We must continue
19225 the line because we can't draw the cursor
19226 after the glyph. */
19227 row->continued_p = 1;
19228 it->current_x = new_x;
19229 it->continuation_lines_width += new_x;
19230 ++it->hpos;
19231 if (i == nglyphs - 1)
19232 {
19233 /* If line-wrap is on, check if a previous
19234 wrap point was found. */
19235 if (wrap_row_used > 0
19236 /* Even if there is a previous wrap
19237 point, continue the line here as
19238 usual, if (i) the previous character
19239 was a space or tab AND (ii) the
19240 current character is not. */
19241 && (!may_wrap
19242 || IT_DISPLAYING_WHITESPACE (it)))
19243 goto back_to_wrap;
19244
19245 /* Record the maximum and minimum buffer
19246 positions seen so far in glyphs that will be
19247 displayed by this row. */
19248 if (it->bidi_p)
19249 RECORD_MAX_MIN_POS (it);
19250 set_iterator_to_next (it, 1);
19251 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19252 {
19253 if (!get_next_display_element (it))
19254 {
19255 row->exact_window_width_line_p = 1;
19256 it->continuation_lines_width = 0;
19257 row->continued_p = 0;
19258 row->ends_at_zv_p = 1;
19259 }
19260 else if (ITERATOR_AT_END_OF_LINE_P (it))
19261 {
19262 row->continued_p = 0;
19263 row->exact_window_width_line_p = 1;
19264 }
19265 }
19266 }
19267 else if (it->bidi_p)
19268 RECORD_MAX_MIN_POS (it);
19269 }
19270 else if (CHAR_GLYPH_PADDING_P (*glyph)
19271 && !FRAME_WINDOW_P (it->f))
19272 {
19273 /* A padding glyph that doesn't fit on this line.
19274 This means the whole character doesn't fit
19275 on the line. */
19276 if (row->reversed_p)
19277 unproduce_glyphs (it, row->used[TEXT_AREA]
19278 - n_glyphs_before);
19279 row->used[TEXT_AREA] = n_glyphs_before;
19280
19281 /* Fill the rest of the row with continuation
19282 glyphs like in 20.x. */
19283 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19284 < row->glyphs[1 + TEXT_AREA])
19285 produce_special_glyphs (it, IT_CONTINUATION);
19286
19287 row->continued_p = 1;
19288 it->current_x = x_before;
19289 it->continuation_lines_width += x_before;
19290
19291 /* Restore the height to what it was before the
19292 element not fitting on the line. */
19293 it->max_ascent = ascent;
19294 it->max_descent = descent;
19295 it->max_phys_ascent = phys_ascent;
19296 it->max_phys_descent = phys_descent;
19297 }
19298 else if (wrap_row_used > 0)
19299 {
19300 back_to_wrap:
19301 if (row->reversed_p)
19302 unproduce_glyphs (it,
19303 row->used[TEXT_AREA] - wrap_row_used);
19304 RESTORE_IT (it, &wrap_it, wrap_data);
19305 it->continuation_lines_width += wrap_x;
19306 row->used[TEXT_AREA] = wrap_row_used;
19307 row->ascent = wrap_row_ascent;
19308 row->height = wrap_row_height;
19309 row->phys_ascent = wrap_row_phys_ascent;
19310 row->phys_height = wrap_row_phys_height;
19311 row->extra_line_spacing = wrap_row_extra_line_spacing;
19312 min_pos = wrap_row_min_pos;
19313 min_bpos = wrap_row_min_bpos;
19314 max_pos = wrap_row_max_pos;
19315 max_bpos = wrap_row_max_bpos;
19316 row->continued_p = 1;
19317 row->ends_at_zv_p = 0;
19318 row->exact_window_width_line_p = 0;
19319 it->continuation_lines_width += x;
19320
19321 /* Make sure that a non-default face is extended
19322 up to the right margin of the window. */
19323 extend_face_to_end_of_line (it);
19324 }
19325 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19326 {
19327 /* A TAB that extends past the right edge of the
19328 window. This produces a single glyph on
19329 window system frames. We leave the glyph in
19330 this row and let it fill the row, but don't
19331 consume the TAB. */
19332 it->continuation_lines_width += it->last_visible_x;
19333 row->ends_in_middle_of_char_p = 1;
19334 row->continued_p = 1;
19335 glyph->pixel_width = it->last_visible_x - x;
19336 it->starts_in_middle_of_char_p = 1;
19337 }
19338 else
19339 {
19340 /* Something other than a TAB that draws past
19341 the right edge of the window. Restore
19342 positions to values before the element. */
19343 if (row->reversed_p)
19344 unproduce_glyphs (it, row->used[TEXT_AREA]
19345 - (n_glyphs_before + i));
19346 row->used[TEXT_AREA] = n_glyphs_before + i;
19347
19348 /* Display continuation glyphs. */
19349 if (!FRAME_WINDOW_P (it->f))
19350 produce_special_glyphs (it, IT_CONTINUATION);
19351 row->continued_p = 1;
19352
19353 it->current_x = x_before;
19354 it->continuation_lines_width += x;
19355 extend_face_to_end_of_line (it);
19356
19357 if (nglyphs > 1 && i > 0)
19358 {
19359 row->ends_in_middle_of_char_p = 1;
19360 it->starts_in_middle_of_char_p = 1;
19361 }
19362
19363 /* Restore the height to what it was before the
19364 element not fitting on the line. */
19365 it->max_ascent = ascent;
19366 it->max_descent = descent;
19367 it->max_phys_ascent = phys_ascent;
19368 it->max_phys_descent = phys_descent;
19369 }
19370
19371 break;
19372 }
19373 else if (new_x > it->first_visible_x)
19374 {
19375 /* Increment number of glyphs actually displayed. */
19376 ++it->hpos;
19377
19378 /* Record the maximum and minimum buffer positions
19379 seen so far in glyphs that will be displayed by
19380 this row. */
19381 if (it->bidi_p)
19382 RECORD_MAX_MIN_POS (it);
19383
19384 if (x < it->first_visible_x)
19385 /* Glyph is partially visible, i.e. row starts at
19386 negative X position. */
19387 row->x = x - it->first_visible_x;
19388 }
19389 else
19390 {
19391 /* Glyph is completely off the left margin of the
19392 window. This should not happen because of the
19393 move_it_in_display_line at the start of this
19394 function, unless the text display area of the
19395 window is empty. */
19396 xassert (it->first_visible_x <= it->last_visible_x);
19397 }
19398 }
19399 /* Even if this display element produced no glyphs at all,
19400 we want to record its position. */
19401 if (it->bidi_p && nglyphs == 0)
19402 RECORD_MAX_MIN_POS (it);
19403
19404 row->ascent = max (row->ascent, it->max_ascent);
19405 row->height = max (row->height, it->max_ascent + it->max_descent);
19406 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19407 row->phys_height = max (row->phys_height,
19408 it->max_phys_ascent + it->max_phys_descent);
19409 row->extra_line_spacing = max (row->extra_line_spacing,
19410 it->max_extra_line_spacing);
19411
19412 /* End of this display line if row is continued. */
19413 if (row->continued_p || row->ends_at_zv_p)
19414 break;
19415 }
19416
19417 at_end_of_line:
19418 /* Is this a line end? If yes, we're also done, after making
19419 sure that a non-default face is extended up to the right
19420 margin of the window. */
19421 if (ITERATOR_AT_END_OF_LINE_P (it))
19422 {
19423 int used_before = row->used[TEXT_AREA];
19424
19425 row->ends_in_newline_from_string_p = STRINGP (it->object);
19426
19427 /* Add a space at the end of the line that is used to
19428 display the cursor there. */
19429 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19430 append_space_for_newline (it, 0);
19431
19432 /* Extend the face to the end of the line. */
19433 extend_face_to_end_of_line (it);
19434
19435 /* Make sure we have the position. */
19436 if (used_before == 0)
19437 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19438
19439 /* Record the position of the newline, for use in
19440 find_row_edges. */
19441 it->eol_pos = it->current.pos;
19442
19443 /* Consume the line end. This skips over invisible lines. */
19444 set_iterator_to_next (it, 1);
19445 it->continuation_lines_width = 0;
19446 break;
19447 }
19448
19449 /* Proceed with next display element. Note that this skips
19450 over lines invisible because of selective display. */
19451 set_iterator_to_next (it, 1);
19452
19453 /* If we truncate lines, we are done when the last displayed
19454 glyphs reach past the right margin of the window. */
19455 if (it->line_wrap == TRUNCATE
19456 && (FRAME_WINDOW_P (it->f)
19457 ? (it->current_x >= it->last_visible_x)
19458 : (it->current_x > it->last_visible_x)))
19459 {
19460 /* Maybe add truncation glyphs. */
19461 if (!FRAME_WINDOW_P (it->f))
19462 {
19463 int i, n;
19464
19465 if (!row->reversed_p)
19466 {
19467 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19468 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19469 break;
19470 }
19471 else
19472 {
19473 for (i = 0; i < row->used[TEXT_AREA]; i++)
19474 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19475 break;
19476 /* Remove any padding glyphs at the front of ROW, to
19477 make room for the truncation glyphs we will be
19478 adding below. The loop below always inserts at
19479 least one truncation glyph, so also remove the
19480 last glyph added to ROW. */
19481 unproduce_glyphs (it, i + 1);
19482 /* Adjust i for the loop below. */
19483 i = row->used[TEXT_AREA] - (i + 1);
19484 }
19485
19486 for (n = row->used[TEXT_AREA]; i < n; ++i)
19487 {
19488 row->used[TEXT_AREA] = i;
19489 produce_special_glyphs (it, IT_TRUNCATION);
19490 }
19491 }
19492 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19493 {
19494 /* Don't truncate if we can overflow newline into fringe. */
19495 if (!get_next_display_element (it))
19496 {
19497 it->continuation_lines_width = 0;
19498 row->ends_at_zv_p = 1;
19499 row->exact_window_width_line_p = 1;
19500 break;
19501 }
19502 if (ITERATOR_AT_END_OF_LINE_P (it))
19503 {
19504 row->exact_window_width_line_p = 1;
19505 goto at_end_of_line;
19506 }
19507 }
19508
19509 row->truncated_on_right_p = 1;
19510 it->continuation_lines_width = 0;
19511 reseat_at_next_visible_line_start (it, 0);
19512 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19513 it->hpos = hpos_before;
19514 it->current_x = x_before;
19515 break;
19516 }
19517 }
19518
19519 if (wrap_data)
19520 bidi_unshelve_cache (wrap_data, 1);
19521
19522 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19523 at the left window margin. */
19524 if (it->first_visible_x
19525 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19526 {
19527 if (!FRAME_WINDOW_P (it->f))
19528 insert_left_trunc_glyphs (it);
19529 row->truncated_on_left_p = 1;
19530 }
19531
19532 /* Remember the position at which this line ends.
19533
19534 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19535 cannot be before the call to find_row_edges below, since that is
19536 where these positions are determined. */
19537 row->end = it->current;
19538 if (!it->bidi_p)
19539 {
19540 row->minpos = row->start.pos;
19541 row->maxpos = row->end.pos;
19542 }
19543 else
19544 {
19545 /* ROW->minpos and ROW->maxpos must be the smallest and
19546 `1 + the largest' buffer positions in ROW. But if ROW was
19547 bidi-reordered, these two positions can be anywhere in the
19548 row, so we must determine them now. */
19549 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19550 }
19551
19552 /* If the start of this line is the overlay arrow-position, then
19553 mark this glyph row as the one containing the overlay arrow.
19554 This is clearly a mess with variable size fonts. It would be
19555 better to let it be displayed like cursors under X. */
19556 if ((row->displays_text_p || !overlay_arrow_seen)
19557 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19558 !NILP (overlay_arrow_string)))
19559 {
19560 /* Overlay arrow in window redisplay is a fringe bitmap. */
19561 if (STRINGP (overlay_arrow_string))
19562 {
19563 struct glyph_row *arrow_row
19564 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19565 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19566 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19567 struct glyph *p = row->glyphs[TEXT_AREA];
19568 struct glyph *p2, *end;
19569
19570 /* Copy the arrow glyphs. */
19571 while (glyph < arrow_end)
19572 *p++ = *glyph++;
19573
19574 /* Throw away padding glyphs. */
19575 p2 = p;
19576 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19577 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19578 ++p2;
19579 if (p2 > p)
19580 {
19581 while (p2 < end)
19582 *p++ = *p2++;
19583 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19584 }
19585 }
19586 else
19587 {
19588 xassert (INTEGERP (overlay_arrow_string));
19589 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19590 }
19591 overlay_arrow_seen = 1;
19592 }
19593
19594 /* Highlight trailing whitespace. */
19595 if (!NILP (Vshow_trailing_whitespace))
19596 highlight_trailing_whitespace (it->f, it->glyph_row);
19597
19598 /* Compute pixel dimensions of this line. */
19599 compute_line_metrics (it);
19600
19601 /* Implementation note: No changes in the glyphs of ROW or in their
19602 faces can be done past this point, because compute_line_metrics
19603 computes ROW's hash value and stores it within the glyph_row
19604 structure. */
19605
19606 /* Record whether this row ends inside an ellipsis. */
19607 row->ends_in_ellipsis_p
19608 = (it->method == GET_FROM_DISPLAY_VECTOR
19609 && it->ellipsis_p);
19610
19611 /* Save fringe bitmaps in this row. */
19612 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19613 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19614 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19615 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19616
19617 it->left_user_fringe_bitmap = 0;
19618 it->left_user_fringe_face_id = 0;
19619 it->right_user_fringe_bitmap = 0;
19620 it->right_user_fringe_face_id = 0;
19621
19622 /* Maybe set the cursor. */
19623 cvpos = it->w->cursor.vpos;
19624 if ((cvpos < 0
19625 /* In bidi-reordered rows, keep checking for proper cursor
19626 position even if one has been found already, because buffer
19627 positions in such rows change non-linearly with ROW->VPOS,
19628 when a line is continued. One exception: when we are at ZV,
19629 display cursor on the first suitable glyph row, since all
19630 the empty rows after that also have their position set to ZV. */
19631 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19632 lines' rows is implemented for bidi-reordered rows. */
19633 || (it->bidi_p
19634 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19635 && PT >= MATRIX_ROW_START_CHARPOS (row)
19636 && PT <= MATRIX_ROW_END_CHARPOS (row)
19637 && cursor_row_p (row))
19638 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19639
19640 /* Prepare for the next line. This line starts horizontally at (X
19641 HPOS) = (0 0). Vertical positions are incremented. As a
19642 convenience for the caller, IT->glyph_row is set to the next
19643 row to be used. */
19644 it->current_x = it->hpos = 0;
19645 it->current_y += row->height;
19646 SET_TEXT_POS (it->eol_pos, 0, 0);
19647 ++it->vpos;
19648 ++it->glyph_row;
19649 /* The next row should by default use the same value of the
19650 reversed_p flag as this one. set_iterator_to_next decides when
19651 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19652 the flag accordingly. */
19653 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19654 it->glyph_row->reversed_p = row->reversed_p;
19655 it->start = row->end;
19656 return row->displays_text_p;
19657
19658 #undef RECORD_MAX_MIN_POS
19659 }
19660
19661 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19662 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19663 doc: /* Return paragraph direction at point in BUFFER.
19664 Value is either `left-to-right' or `right-to-left'.
19665 If BUFFER is omitted or nil, it defaults to the current buffer.
19666
19667 Paragraph direction determines how the text in the paragraph is displayed.
19668 In left-to-right paragraphs, text begins at the left margin of the window
19669 and the reading direction is generally left to right. In right-to-left
19670 paragraphs, text begins at the right margin and is read from right to left.
19671
19672 See also `bidi-paragraph-direction'. */)
19673 (Lisp_Object buffer)
19674 {
19675 struct buffer *buf = current_buffer;
19676 struct buffer *old = buf;
19677
19678 if (! NILP (buffer))
19679 {
19680 CHECK_BUFFER (buffer);
19681 buf = XBUFFER (buffer);
19682 }
19683
19684 if (NILP (BVAR (buf, bidi_display_reordering))
19685 || NILP (BVAR (buf, enable_multibyte_characters))
19686 /* When we are loading loadup.el, the character property tables
19687 needed for bidi iteration are not yet available. */
19688 || !NILP (Vpurify_flag))
19689 return Qleft_to_right;
19690 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19691 return BVAR (buf, bidi_paragraph_direction);
19692 else
19693 {
19694 /* Determine the direction from buffer text. We could try to
19695 use current_matrix if it is up to date, but this seems fast
19696 enough as it is. */
19697 struct bidi_it itb;
19698 EMACS_INT pos = BUF_PT (buf);
19699 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19700 int c;
19701 void *itb_data = bidi_shelve_cache ();
19702
19703 set_buffer_temp (buf);
19704 /* bidi_paragraph_init finds the base direction of the paragraph
19705 by searching forward from paragraph start. We need the base
19706 direction of the current or _previous_ paragraph, so we need
19707 to make sure we are within that paragraph. To that end, find
19708 the previous non-empty line. */
19709 if (pos >= ZV && pos > BEGV)
19710 {
19711 pos--;
19712 bytepos = CHAR_TO_BYTE (pos);
19713 }
19714 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19715 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19716 {
19717 while ((c = FETCH_BYTE (bytepos)) == '\n'
19718 || c == ' ' || c == '\t' || c == '\f')
19719 {
19720 if (bytepos <= BEGV_BYTE)
19721 break;
19722 bytepos--;
19723 pos--;
19724 }
19725 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19726 bytepos--;
19727 }
19728 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19729 itb.paragraph_dir = NEUTRAL_DIR;
19730 itb.string.s = NULL;
19731 itb.string.lstring = Qnil;
19732 itb.string.bufpos = 0;
19733 itb.string.unibyte = 0;
19734 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19735 bidi_unshelve_cache (itb_data, 0);
19736 set_buffer_temp (old);
19737 switch (itb.paragraph_dir)
19738 {
19739 case L2R:
19740 return Qleft_to_right;
19741 break;
19742 case R2L:
19743 return Qright_to_left;
19744 break;
19745 default:
19746 abort ();
19747 }
19748 }
19749 }
19750
19751
19752 \f
19753 /***********************************************************************
19754 Menu Bar
19755 ***********************************************************************/
19756
19757 /* Redisplay the menu bar in the frame for window W.
19758
19759 The menu bar of X frames that don't have X toolkit support is
19760 displayed in a special window W->frame->menu_bar_window.
19761
19762 The menu bar of terminal frames is treated specially as far as
19763 glyph matrices are concerned. Menu bar lines are not part of
19764 windows, so the update is done directly on the frame matrix rows
19765 for the menu bar. */
19766
19767 static void
19768 display_menu_bar (struct window *w)
19769 {
19770 struct frame *f = XFRAME (WINDOW_FRAME (w));
19771 struct it it;
19772 Lisp_Object items;
19773 int i;
19774
19775 /* Don't do all this for graphical frames. */
19776 #ifdef HAVE_NTGUI
19777 if (FRAME_W32_P (f))
19778 return;
19779 #endif
19780 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19781 if (FRAME_X_P (f))
19782 return;
19783 #endif
19784
19785 #ifdef HAVE_NS
19786 if (FRAME_NS_P (f))
19787 return;
19788 #endif /* HAVE_NS */
19789
19790 #ifdef USE_X_TOOLKIT
19791 xassert (!FRAME_WINDOW_P (f));
19792 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19793 it.first_visible_x = 0;
19794 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19795 #else /* not USE_X_TOOLKIT */
19796 if (FRAME_WINDOW_P (f))
19797 {
19798 /* Menu bar lines are displayed in the desired matrix of the
19799 dummy window menu_bar_window. */
19800 struct window *menu_w;
19801 xassert (WINDOWP (f->menu_bar_window));
19802 menu_w = XWINDOW (f->menu_bar_window);
19803 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19804 MENU_FACE_ID);
19805 it.first_visible_x = 0;
19806 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19807 }
19808 else
19809 {
19810 /* This is a TTY frame, i.e. character hpos/vpos are used as
19811 pixel x/y. */
19812 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19813 MENU_FACE_ID);
19814 it.first_visible_x = 0;
19815 it.last_visible_x = FRAME_COLS (f);
19816 }
19817 #endif /* not USE_X_TOOLKIT */
19818
19819 /* FIXME: This should be controlled by a user option. See the
19820 comments in redisplay_tool_bar and display_mode_line about
19821 this. */
19822 it.paragraph_embedding = L2R;
19823
19824 if (! mode_line_inverse_video)
19825 /* Force the menu-bar to be displayed in the default face. */
19826 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19827
19828 /* Clear all rows of the menu bar. */
19829 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19830 {
19831 struct glyph_row *row = it.glyph_row + i;
19832 clear_glyph_row (row);
19833 row->enabled_p = 1;
19834 row->full_width_p = 1;
19835 }
19836
19837 /* Display all items of the menu bar. */
19838 items = FRAME_MENU_BAR_ITEMS (it.f);
19839 for (i = 0; i < ASIZE (items); i += 4)
19840 {
19841 Lisp_Object string;
19842
19843 /* Stop at nil string. */
19844 string = AREF (items, i + 1);
19845 if (NILP (string))
19846 break;
19847
19848 /* Remember where item was displayed. */
19849 ASET (items, i + 3, make_number (it.hpos));
19850
19851 /* Display the item, pad with one space. */
19852 if (it.current_x < it.last_visible_x)
19853 display_string (NULL, string, Qnil, 0, 0, &it,
19854 SCHARS (string) + 1, 0, 0, -1);
19855 }
19856
19857 /* Fill out the line with spaces. */
19858 if (it.current_x < it.last_visible_x)
19859 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19860
19861 /* Compute the total height of the lines. */
19862 compute_line_metrics (&it);
19863 }
19864
19865
19866 \f
19867 /***********************************************************************
19868 Mode Line
19869 ***********************************************************************/
19870
19871 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19872 FORCE is non-zero, redisplay mode lines unconditionally.
19873 Otherwise, redisplay only mode lines that are garbaged. Value is
19874 the number of windows whose mode lines were redisplayed. */
19875
19876 static int
19877 redisplay_mode_lines (Lisp_Object window, int force)
19878 {
19879 int nwindows = 0;
19880
19881 while (!NILP (window))
19882 {
19883 struct window *w = XWINDOW (window);
19884
19885 if (WINDOWP (w->hchild))
19886 nwindows += redisplay_mode_lines (w->hchild, force);
19887 else if (WINDOWP (w->vchild))
19888 nwindows += redisplay_mode_lines (w->vchild, force);
19889 else if (force
19890 || FRAME_GARBAGED_P (XFRAME (w->frame))
19891 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19892 {
19893 struct text_pos lpoint;
19894 struct buffer *old = current_buffer;
19895
19896 /* Set the window's buffer for the mode line display. */
19897 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19898 set_buffer_internal_1 (XBUFFER (w->buffer));
19899
19900 /* Point refers normally to the selected window. For any
19901 other window, set up appropriate value. */
19902 if (!EQ (window, selected_window))
19903 {
19904 struct text_pos pt;
19905
19906 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19907 if (CHARPOS (pt) < BEGV)
19908 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19909 else if (CHARPOS (pt) > (ZV - 1))
19910 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19911 else
19912 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19913 }
19914
19915 /* Display mode lines. */
19916 clear_glyph_matrix (w->desired_matrix);
19917 if (display_mode_lines (w))
19918 {
19919 ++nwindows;
19920 w->must_be_updated_p = 1;
19921 }
19922
19923 /* Restore old settings. */
19924 set_buffer_internal_1 (old);
19925 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19926 }
19927
19928 window = w->next;
19929 }
19930
19931 return nwindows;
19932 }
19933
19934
19935 /* Display the mode and/or header line of window W. Value is the
19936 sum number of mode lines and header lines displayed. */
19937
19938 static int
19939 display_mode_lines (struct window *w)
19940 {
19941 Lisp_Object old_selected_window, old_selected_frame;
19942 int n = 0;
19943
19944 old_selected_frame = selected_frame;
19945 selected_frame = w->frame;
19946 old_selected_window = selected_window;
19947 XSETWINDOW (selected_window, w);
19948
19949 /* These will be set while the mode line specs are processed. */
19950 line_number_displayed = 0;
19951 w->column_number_displayed = Qnil;
19952
19953 if (WINDOW_WANTS_MODELINE_P (w))
19954 {
19955 struct window *sel_w = XWINDOW (old_selected_window);
19956
19957 /* Select mode line face based on the real selected window. */
19958 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19959 BVAR (current_buffer, mode_line_format));
19960 ++n;
19961 }
19962
19963 if (WINDOW_WANTS_HEADER_LINE_P (w))
19964 {
19965 display_mode_line (w, HEADER_LINE_FACE_ID,
19966 BVAR (current_buffer, header_line_format));
19967 ++n;
19968 }
19969
19970 selected_frame = old_selected_frame;
19971 selected_window = old_selected_window;
19972 return n;
19973 }
19974
19975
19976 /* Display mode or header line of window W. FACE_ID specifies which
19977 line to display; it is either MODE_LINE_FACE_ID or
19978 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19979 display. Value is the pixel height of the mode/header line
19980 displayed. */
19981
19982 static int
19983 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19984 {
19985 struct it it;
19986 struct face *face;
19987 int count = SPECPDL_INDEX ();
19988
19989 init_iterator (&it, w, -1, -1, NULL, face_id);
19990 /* Don't extend on a previously drawn mode-line.
19991 This may happen if called from pos_visible_p. */
19992 it.glyph_row->enabled_p = 0;
19993 prepare_desired_row (it.glyph_row);
19994
19995 it.glyph_row->mode_line_p = 1;
19996
19997 if (! mode_line_inverse_video)
19998 /* Force the mode-line to be displayed in the default face. */
19999 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20000
20001 /* FIXME: This should be controlled by a user option. But
20002 supporting such an option is not trivial, since the mode line is
20003 made up of many separate strings. */
20004 it.paragraph_embedding = L2R;
20005
20006 record_unwind_protect (unwind_format_mode_line,
20007 format_mode_line_unwind_data (NULL, Qnil, 0));
20008
20009 mode_line_target = MODE_LINE_DISPLAY;
20010
20011 /* Temporarily make frame's keyboard the current kboard so that
20012 kboard-local variables in the mode_line_format will get the right
20013 values. */
20014 push_kboard (FRAME_KBOARD (it.f));
20015 record_unwind_save_match_data ();
20016 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20017 pop_kboard ();
20018
20019 unbind_to (count, Qnil);
20020
20021 /* Fill up with spaces. */
20022 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20023
20024 compute_line_metrics (&it);
20025 it.glyph_row->full_width_p = 1;
20026 it.glyph_row->continued_p = 0;
20027 it.glyph_row->truncated_on_left_p = 0;
20028 it.glyph_row->truncated_on_right_p = 0;
20029
20030 /* Make a 3D mode-line have a shadow at its right end. */
20031 face = FACE_FROM_ID (it.f, face_id);
20032 extend_face_to_end_of_line (&it);
20033 if (face->box != FACE_NO_BOX)
20034 {
20035 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20036 + it.glyph_row->used[TEXT_AREA] - 1);
20037 last->right_box_line_p = 1;
20038 }
20039
20040 return it.glyph_row->height;
20041 }
20042
20043 /* Move element ELT in LIST to the front of LIST.
20044 Return the updated list. */
20045
20046 static Lisp_Object
20047 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20048 {
20049 register Lisp_Object tail, prev;
20050 register Lisp_Object tem;
20051
20052 tail = list;
20053 prev = Qnil;
20054 while (CONSP (tail))
20055 {
20056 tem = XCAR (tail);
20057
20058 if (EQ (elt, tem))
20059 {
20060 /* Splice out the link TAIL. */
20061 if (NILP (prev))
20062 list = XCDR (tail);
20063 else
20064 Fsetcdr (prev, XCDR (tail));
20065
20066 /* Now make it the first. */
20067 Fsetcdr (tail, list);
20068 return tail;
20069 }
20070 else
20071 prev = tail;
20072 tail = XCDR (tail);
20073 QUIT;
20074 }
20075
20076 /* Not found--return unchanged LIST. */
20077 return list;
20078 }
20079
20080 /* Contribute ELT to the mode line for window IT->w. How it
20081 translates into text depends on its data type.
20082
20083 IT describes the display environment in which we display, as usual.
20084
20085 DEPTH is the depth in recursion. It is used to prevent
20086 infinite recursion here.
20087
20088 FIELD_WIDTH is the number of characters the display of ELT should
20089 occupy in the mode line, and PRECISION is the maximum number of
20090 characters to display from ELT's representation. See
20091 display_string for details.
20092
20093 Returns the hpos of the end of the text generated by ELT.
20094
20095 PROPS is a property list to add to any string we encounter.
20096
20097 If RISKY is nonzero, remove (disregard) any properties in any string
20098 we encounter, and ignore :eval and :propertize.
20099
20100 The global variable `mode_line_target' determines whether the
20101 output is passed to `store_mode_line_noprop',
20102 `store_mode_line_string', or `display_string'. */
20103
20104 static int
20105 display_mode_element (struct it *it, int depth, int field_width, int precision,
20106 Lisp_Object elt, Lisp_Object props, int risky)
20107 {
20108 int n = 0, field, prec;
20109 int literal = 0;
20110
20111 tail_recurse:
20112 if (depth > 100)
20113 elt = build_string ("*too-deep*");
20114
20115 depth++;
20116
20117 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20118 {
20119 case Lisp_String:
20120 {
20121 /* A string: output it and check for %-constructs within it. */
20122 unsigned char c;
20123 EMACS_INT offset = 0;
20124
20125 if (SCHARS (elt) > 0
20126 && (!NILP (props) || risky))
20127 {
20128 Lisp_Object oprops, aelt;
20129 oprops = Ftext_properties_at (make_number (0), elt);
20130
20131 /* If the starting string's properties are not what
20132 we want, translate the string. Also, if the string
20133 is risky, do that anyway. */
20134
20135 if (NILP (Fequal (props, oprops)) || risky)
20136 {
20137 /* If the starting string has properties,
20138 merge the specified ones onto the existing ones. */
20139 if (! NILP (oprops) && !risky)
20140 {
20141 Lisp_Object tem;
20142
20143 oprops = Fcopy_sequence (oprops);
20144 tem = props;
20145 while (CONSP (tem))
20146 {
20147 oprops = Fplist_put (oprops, XCAR (tem),
20148 XCAR (XCDR (tem)));
20149 tem = XCDR (XCDR (tem));
20150 }
20151 props = oprops;
20152 }
20153
20154 aelt = Fassoc (elt, mode_line_proptrans_alist);
20155 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20156 {
20157 /* AELT is what we want. Move it to the front
20158 without consing. */
20159 elt = XCAR (aelt);
20160 mode_line_proptrans_alist
20161 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20162 }
20163 else
20164 {
20165 Lisp_Object tem;
20166
20167 /* If AELT has the wrong props, it is useless.
20168 so get rid of it. */
20169 if (! NILP (aelt))
20170 mode_line_proptrans_alist
20171 = Fdelq (aelt, mode_line_proptrans_alist);
20172
20173 elt = Fcopy_sequence (elt);
20174 Fset_text_properties (make_number (0), Flength (elt),
20175 props, elt);
20176 /* Add this item to mode_line_proptrans_alist. */
20177 mode_line_proptrans_alist
20178 = Fcons (Fcons (elt, props),
20179 mode_line_proptrans_alist);
20180 /* Truncate mode_line_proptrans_alist
20181 to at most 50 elements. */
20182 tem = Fnthcdr (make_number (50),
20183 mode_line_proptrans_alist);
20184 if (! NILP (tem))
20185 XSETCDR (tem, Qnil);
20186 }
20187 }
20188 }
20189
20190 offset = 0;
20191
20192 if (literal)
20193 {
20194 prec = precision - n;
20195 switch (mode_line_target)
20196 {
20197 case MODE_LINE_NOPROP:
20198 case MODE_LINE_TITLE:
20199 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20200 break;
20201 case MODE_LINE_STRING:
20202 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20203 break;
20204 case MODE_LINE_DISPLAY:
20205 n += display_string (NULL, elt, Qnil, 0, 0, it,
20206 0, prec, 0, STRING_MULTIBYTE (elt));
20207 break;
20208 }
20209
20210 break;
20211 }
20212
20213 /* Handle the non-literal case. */
20214
20215 while ((precision <= 0 || n < precision)
20216 && SREF (elt, offset) != 0
20217 && (mode_line_target != MODE_LINE_DISPLAY
20218 || it->current_x < it->last_visible_x))
20219 {
20220 EMACS_INT last_offset = offset;
20221
20222 /* Advance to end of string or next format specifier. */
20223 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20224 ;
20225
20226 if (offset - 1 != last_offset)
20227 {
20228 EMACS_INT nchars, nbytes;
20229
20230 /* Output to end of string or up to '%'. Field width
20231 is length of string. Don't output more than
20232 PRECISION allows us. */
20233 offset--;
20234
20235 prec = c_string_width (SDATA (elt) + last_offset,
20236 offset - last_offset, precision - n,
20237 &nchars, &nbytes);
20238
20239 switch (mode_line_target)
20240 {
20241 case MODE_LINE_NOPROP:
20242 case MODE_LINE_TITLE:
20243 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20244 break;
20245 case MODE_LINE_STRING:
20246 {
20247 EMACS_INT bytepos = last_offset;
20248 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20249 EMACS_INT endpos = (precision <= 0
20250 ? string_byte_to_char (elt, offset)
20251 : charpos + nchars);
20252
20253 n += store_mode_line_string (NULL,
20254 Fsubstring (elt, make_number (charpos),
20255 make_number (endpos)),
20256 0, 0, 0, Qnil);
20257 }
20258 break;
20259 case MODE_LINE_DISPLAY:
20260 {
20261 EMACS_INT bytepos = last_offset;
20262 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20263
20264 if (precision <= 0)
20265 nchars = string_byte_to_char (elt, offset) - charpos;
20266 n += display_string (NULL, elt, Qnil, 0, charpos,
20267 it, 0, nchars, 0,
20268 STRING_MULTIBYTE (elt));
20269 }
20270 break;
20271 }
20272 }
20273 else /* c == '%' */
20274 {
20275 EMACS_INT percent_position = offset;
20276
20277 /* Get the specified minimum width. Zero means
20278 don't pad. */
20279 field = 0;
20280 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20281 field = field * 10 + c - '0';
20282
20283 /* Don't pad beyond the total padding allowed. */
20284 if (field_width - n > 0 && field > field_width - n)
20285 field = field_width - n;
20286
20287 /* Note that either PRECISION <= 0 or N < PRECISION. */
20288 prec = precision - n;
20289
20290 if (c == 'M')
20291 n += display_mode_element (it, depth, field, prec,
20292 Vglobal_mode_string, props,
20293 risky);
20294 else if (c != 0)
20295 {
20296 int multibyte;
20297 EMACS_INT bytepos, charpos;
20298 const char *spec;
20299 Lisp_Object string;
20300
20301 bytepos = percent_position;
20302 charpos = (STRING_MULTIBYTE (elt)
20303 ? string_byte_to_char (elt, bytepos)
20304 : bytepos);
20305 spec = decode_mode_spec (it->w, c, field, &string);
20306 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20307
20308 switch (mode_line_target)
20309 {
20310 case MODE_LINE_NOPROP:
20311 case MODE_LINE_TITLE:
20312 n += store_mode_line_noprop (spec, field, prec);
20313 break;
20314 case MODE_LINE_STRING:
20315 {
20316 Lisp_Object tem = build_string (spec);
20317 props = Ftext_properties_at (make_number (charpos), elt);
20318 /* Should only keep face property in props */
20319 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20320 }
20321 break;
20322 case MODE_LINE_DISPLAY:
20323 {
20324 int nglyphs_before, nwritten;
20325
20326 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20327 nwritten = display_string (spec, string, elt,
20328 charpos, 0, it,
20329 field, prec, 0,
20330 multibyte);
20331
20332 /* Assign to the glyphs written above the
20333 string where the `%x' came from, position
20334 of the `%'. */
20335 if (nwritten > 0)
20336 {
20337 struct glyph *glyph
20338 = (it->glyph_row->glyphs[TEXT_AREA]
20339 + nglyphs_before);
20340 int i;
20341
20342 for (i = 0; i < nwritten; ++i)
20343 {
20344 glyph[i].object = elt;
20345 glyph[i].charpos = charpos;
20346 }
20347
20348 n += nwritten;
20349 }
20350 }
20351 break;
20352 }
20353 }
20354 else /* c == 0 */
20355 break;
20356 }
20357 }
20358 }
20359 break;
20360
20361 case Lisp_Symbol:
20362 /* A symbol: process the value of the symbol recursively
20363 as if it appeared here directly. Avoid error if symbol void.
20364 Special case: if value of symbol is a string, output the string
20365 literally. */
20366 {
20367 register Lisp_Object tem;
20368
20369 /* If the variable is not marked as risky to set
20370 then its contents are risky to use. */
20371 if (NILP (Fget (elt, Qrisky_local_variable)))
20372 risky = 1;
20373
20374 tem = Fboundp (elt);
20375 if (!NILP (tem))
20376 {
20377 tem = Fsymbol_value (elt);
20378 /* If value is a string, output that string literally:
20379 don't check for % within it. */
20380 if (STRINGP (tem))
20381 literal = 1;
20382
20383 if (!EQ (tem, elt))
20384 {
20385 /* Give up right away for nil or t. */
20386 elt = tem;
20387 goto tail_recurse;
20388 }
20389 }
20390 }
20391 break;
20392
20393 case Lisp_Cons:
20394 {
20395 register Lisp_Object car, tem;
20396
20397 /* A cons cell: five distinct cases.
20398 If first element is :eval or :propertize, do something special.
20399 If first element is a string or a cons, process all the elements
20400 and effectively concatenate them.
20401 If first element is a negative number, truncate displaying cdr to
20402 at most that many characters. If positive, pad (with spaces)
20403 to at least that many characters.
20404 If first element is a symbol, process the cadr or caddr recursively
20405 according to whether the symbol's value is non-nil or nil. */
20406 car = XCAR (elt);
20407 if (EQ (car, QCeval))
20408 {
20409 /* An element of the form (:eval FORM) means evaluate FORM
20410 and use the result as mode line elements. */
20411
20412 if (risky)
20413 break;
20414
20415 if (CONSP (XCDR (elt)))
20416 {
20417 Lisp_Object spec;
20418 spec = safe_eval (XCAR (XCDR (elt)));
20419 n += display_mode_element (it, depth, field_width - n,
20420 precision - n, spec, props,
20421 risky);
20422 }
20423 }
20424 else if (EQ (car, QCpropertize))
20425 {
20426 /* An element of the form (:propertize ELT PROPS...)
20427 means display ELT but applying properties PROPS. */
20428
20429 if (risky)
20430 break;
20431
20432 if (CONSP (XCDR (elt)))
20433 n += display_mode_element (it, depth, field_width - n,
20434 precision - n, XCAR (XCDR (elt)),
20435 XCDR (XCDR (elt)), risky);
20436 }
20437 else if (SYMBOLP (car))
20438 {
20439 tem = Fboundp (car);
20440 elt = XCDR (elt);
20441 if (!CONSP (elt))
20442 goto invalid;
20443 /* elt is now the cdr, and we know it is a cons cell.
20444 Use its car if CAR has a non-nil value. */
20445 if (!NILP (tem))
20446 {
20447 tem = Fsymbol_value (car);
20448 if (!NILP (tem))
20449 {
20450 elt = XCAR (elt);
20451 goto tail_recurse;
20452 }
20453 }
20454 /* Symbol's value is nil (or symbol is unbound)
20455 Get the cddr of the original list
20456 and if possible find the caddr and use that. */
20457 elt = XCDR (elt);
20458 if (NILP (elt))
20459 break;
20460 else if (!CONSP (elt))
20461 goto invalid;
20462 elt = XCAR (elt);
20463 goto tail_recurse;
20464 }
20465 else if (INTEGERP (car))
20466 {
20467 register int lim = XINT (car);
20468 elt = XCDR (elt);
20469 if (lim < 0)
20470 {
20471 /* Negative int means reduce maximum width. */
20472 if (precision <= 0)
20473 precision = -lim;
20474 else
20475 precision = min (precision, -lim);
20476 }
20477 else if (lim > 0)
20478 {
20479 /* Padding specified. Don't let it be more than
20480 current maximum. */
20481 if (precision > 0)
20482 lim = min (precision, lim);
20483
20484 /* If that's more padding than already wanted, queue it.
20485 But don't reduce padding already specified even if
20486 that is beyond the current truncation point. */
20487 field_width = max (lim, field_width);
20488 }
20489 goto tail_recurse;
20490 }
20491 else if (STRINGP (car) || CONSP (car))
20492 {
20493 Lisp_Object halftail = elt;
20494 int len = 0;
20495
20496 while (CONSP (elt)
20497 && (precision <= 0 || n < precision))
20498 {
20499 n += display_mode_element (it, depth,
20500 /* Do padding only after the last
20501 element in the list. */
20502 (! CONSP (XCDR (elt))
20503 ? field_width - n
20504 : 0),
20505 precision - n, XCAR (elt),
20506 props, risky);
20507 elt = XCDR (elt);
20508 len++;
20509 if ((len & 1) == 0)
20510 halftail = XCDR (halftail);
20511 /* Check for cycle. */
20512 if (EQ (halftail, elt))
20513 break;
20514 }
20515 }
20516 }
20517 break;
20518
20519 default:
20520 invalid:
20521 elt = build_string ("*invalid*");
20522 goto tail_recurse;
20523 }
20524
20525 /* Pad to FIELD_WIDTH. */
20526 if (field_width > 0 && n < field_width)
20527 {
20528 switch (mode_line_target)
20529 {
20530 case MODE_LINE_NOPROP:
20531 case MODE_LINE_TITLE:
20532 n += store_mode_line_noprop ("", field_width - n, 0);
20533 break;
20534 case MODE_LINE_STRING:
20535 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20536 break;
20537 case MODE_LINE_DISPLAY:
20538 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20539 0, 0, 0);
20540 break;
20541 }
20542 }
20543
20544 return n;
20545 }
20546
20547 /* Store a mode-line string element in mode_line_string_list.
20548
20549 If STRING is non-null, display that C string. Otherwise, the Lisp
20550 string LISP_STRING is displayed.
20551
20552 FIELD_WIDTH is the minimum number of output glyphs to produce.
20553 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20554 with spaces. FIELD_WIDTH <= 0 means don't pad.
20555
20556 PRECISION is the maximum number of characters to output from
20557 STRING. PRECISION <= 0 means don't truncate the string.
20558
20559 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20560 properties to the string.
20561
20562 PROPS are the properties to add to the string.
20563 The mode_line_string_face face property is always added to the string.
20564 */
20565
20566 static int
20567 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20568 int field_width, int precision, Lisp_Object props)
20569 {
20570 EMACS_INT len;
20571 int n = 0;
20572
20573 if (string != NULL)
20574 {
20575 len = strlen (string);
20576 if (precision > 0 && len > precision)
20577 len = precision;
20578 lisp_string = make_string (string, len);
20579 if (NILP (props))
20580 props = mode_line_string_face_prop;
20581 else if (!NILP (mode_line_string_face))
20582 {
20583 Lisp_Object face = Fplist_get (props, Qface);
20584 props = Fcopy_sequence (props);
20585 if (NILP (face))
20586 face = mode_line_string_face;
20587 else
20588 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20589 props = Fplist_put (props, Qface, face);
20590 }
20591 Fadd_text_properties (make_number (0), make_number (len),
20592 props, lisp_string);
20593 }
20594 else
20595 {
20596 len = XFASTINT (Flength (lisp_string));
20597 if (precision > 0 && len > precision)
20598 {
20599 len = precision;
20600 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20601 precision = -1;
20602 }
20603 if (!NILP (mode_line_string_face))
20604 {
20605 Lisp_Object face;
20606 if (NILP (props))
20607 props = Ftext_properties_at (make_number (0), lisp_string);
20608 face = Fplist_get (props, Qface);
20609 if (NILP (face))
20610 face = mode_line_string_face;
20611 else
20612 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20613 props = Fcons (Qface, Fcons (face, Qnil));
20614 if (copy_string)
20615 lisp_string = Fcopy_sequence (lisp_string);
20616 }
20617 if (!NILP (props))
20618 Fadd_text_properties (make_number (0), make_number (len),
20619 props, lisp_string);
20620 }
20621
20622 if (len > 0)
20623 {
20624 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20625 n += len;
20626 }
20627
20628 if (field_width > len)
20629 {
20630 field_width -= len;
20631 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20632 if (!NILP (props))
20633 Fadd_text_properties (make_number (0), make_number (field_width),
20634 props, lisp_string);
20635 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20636 n += field_width;
20637 }
20638
20639 return n;
20640 }
20641
20642
20643 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20644 1, 4, 0,
20645 doc: /* Format a string out of a mode line format specification.
20646 First arg FORMAT specifies the mode line format (see `mode-line-format'
20647 for details) to use.
20648
20649 By default, the format is evaluated for the currently selected window.
20650
20651 Optional second arg FACE specifies the face property to put on all
20652 characters for which no face is specified. The value nil means the
20653 default face. The value t means whatever face the window's mode line
20654 currently uses (either `mode-line' or `mode-line-inactive',
20655 depending on whether the window is the selected window or not).
20656 An integer value means the value string has no text
20657 properties.
20658
20659 Optional third and fourth args WINDOW and BUFFER specify the window
20660 and buffer to use as the context for the formatting (defaults
20661 are the selected window and the WINDOW's buffer). */)
20662 (Lisp_Object format, Lisp_Object face,
20663 Lisp_Object window, Lisp_Object buffer)
20664 {
20665 struct it it;
20666 int len;
20667 struct window *w;
20668 struct buffer *old_buffer = NULL;
20669 int face_id;
20670 int no_props = INTEGERP (face);
20671 int count = SPECPDL_INDEX ();
20672 Lisp_Object str;
20673 int string_start = 0;
20674
20675 if (NILP (window))
20676 window = selected_window;
20677 CHECK_WINDOW (window);
20678 w = XWINDOW (window);
20679
20680 if (NILP (buffer))
20681 buffer = w->buffer;
20682 CHECK_BUFFER (buffer);
20683
20684 /* Make formatting the modeline a non-op when noninteractive, otherwise
20685 there will be problems later caused by a partially initialized frame. */
20686 if (NILP (format) || noninteractive)
20687 return empty_unibyte_string;
20688
20689 if (no_props)
20690 face = Qnil;
20691
20692 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20693 : EQ (face, Qt) ? (EQ (window, selected_window)
20694 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20695 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20696 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20697 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20698 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20699 : DEFAULT_FACE_ID;
20700
20701 if (XBUFFER (buffer) != current_buffer)
20702 old_buffer = current_buffer;
20703
20704 /* Save things including mode_line_proptrans_alist,
20705 and set that to nil so that we don't alter the outer value. */
20706 record_unwind_protect (unwind_format_mode_line,
20707 format_mode_line_unwind_data
20708 (old_buffer, selected_window, 1));
20709 mode_line_proptrans_alist = Qnil;
20710
20711 Fselect_window (window, Qt);
20712 if (old_buffer)
20713 set_buffer_internal_1 (XBUFFER (buffer));
20714
20715 init_iterator (&it, w, -1, -1, NULL, face_id);
20716
20717 if (no_props)
20718 {
20719 mode_line_target = MODE_LINE_NOPROP;
20720 mode_line_string_face_prop = Qnil;
20721 mode_line_string_list = Qnil;
20722 string_start = MODE_LINE_NOPROP_LEN (0);
20723 }
20724 else
20725 {
20726 mode_line_target = MODE_LINE_STRING;
20727 mode_line_string_list = Qnil;
20728 mode_line_string_face = face;
20729 mode_line_string_face_prop
20730 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20731 }
20732
20733 push_kboard (FRAME_KBOARD (it.f));
20734 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20735 pop_kboard ();
20736
20737 if (no_props)
20738 {
20739 len = MODE_LINE_NOPROP_LEN (string_start);
20740 str = make_string (mode_line_noprop_buf + string_start, len);
20741 }
20742 else
20743 {
20744 mode_line_string_list = Fnreverse (mode_line_string_list);
20745 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20746 empty_unibyte_string);
20747 }
20748
20749 unbind_to (count, Qnil);
20750 return str;
20751 }
20752
20753 /* Write a null-terminated, right justified decimal representation of
20754 the positive integer D to BUF using a minimal field width WIDTH. */
20755
20756 static void
20757 pint2str (register char *buf, register int width, register EMACS_INT d)
20758 {
20759 register char *p = buf;
20760
20761 if (d <= 0)
20762 *p++ = '0';
20763 else
20764 {
20765 while (d > 0)
20766 {
20767 *p++ = d % 10 + '0';
20768 d /= 10;
20769 }
20770 }
20771
20772 for (width -= (int) (p - buf); width > 0; --width)
20773 *p++ = ' ';
20774 *p-- = '\0';
20775 while (p > buf)
20776 {
20777 d = *buf;
20778 *buf++ = *p;
20779 *p-- = d;
20780 }
20781 }
20782
20783 /* Write a null-terminated, right justified decimal and "human
20784 readable" representation of the nonnegative integer D to BUF using
20785 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20786
20787 static const char power_letter[] =
20788 {
20789 0, /* no letter */
20790 'k', /* kilo */
20791 'M', /* mega */
20792 'G', /* giga */
20793 'T', /* tera */
20794 'P', /* peta */
20795 'E', /* exa */
20796 'Z', /* zetta */
20797 'Y' /* yotta */
20798 };
20799
20800 static void
20801 pint2hrstr (char *buf, int width, EMACS_INT d)
20802 {
20803 /* We aim to represent the nonnegative integer D as
20804 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20805 EMACS_INT quotient = d;
20806 int remainder = 0;
20807 /* -1 means: do not use TENTHS. */
20808 int tenths = -1;
20809 int exponent = 0;
20810
20811 /* Length of QUOTIENT.TENTHS as a string. */
20812 int length;
20813
20814 char * psuffix;
20815 char * p;
20816
20817 if (1000 <= quotient)
20818 {
20819 /* Scale to the appropriate EXPONENT. */
20820 do
20821 {
20822 remainder = quotient % 1000;
20823 quotient /= 1000;
20824 exponent++;
20825 }
20826 while (1000 <= quotient);
20827
20828 /* Round to nearest and decide whether to use TENTHS or not. */
20829 if (quotient <= 9)
20830 {
20831 tenths = remainder / 100;
20832 if (50 <= remainder % 100)
20833 {
20834 if (tenths < 9)
20835 tenths++;
20836 else
20837 {
20838 quotient++;
20839 if (quotient == 10)
20840 tenths = -1;
20841 else
20842 tenths = 0;
20843 }
20844 }
20845 }
20846 else
20847 if (500 <= remainder)
20848 {
20849 if (quotient < 999)
20850 quotient++;
20851 else
20852 {
20853 quotient = 1;
20854 exponent++;
20855 tenths = 0;
20856 }
20857 }
20858 }
20859
20860 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20861 if (tenths == -1 && quotient <= 99)
20862 if (quotient <= 9)
20863 length = 1;
20864 else
20865 length = 2;
20866 else
20867 length = 3;
20868 p = psuffix = buf + max (width, length);
20869
20870 /* Print EXPONENT. */
20871 *psuffix++ = power_letter[exponent];
20872 *psuffix = '\0';
20873
20874 /* Print TENTHS. */
20875 if (tenths >= 0)
20876 {
20877 *--p = '0' + tenths;
20878 *--p = '.';
20879 }
20880
20881 /* Print QUOTIENT. */
20882 do
20883 {
20884 int digit = quotient % 10;
20885 *--p = '0' + digit;
20886 }
20887 while ((quotient /= 10) != 0);
20888
20889 /* Print leading spaces. */
20890 while (buf < p)
20891 *--p = ' ';
20892 }
20893
20894 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20895 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20896 type of CODING_SYSTEM. Return updated pointer into BUF. */
20897
20898 static unsigned char invalid_eol_type[] = "(*invalid*)";
20899
20900 static char *
20901 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20902 {
20903 Lisp_Object val;
20904 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20905 const unsigned char *eol_str;
20906 int eol_str_len;
20907 /* The EOL conversion we are using. */
20908 Lisp_Object eoltype;
20909
20910 val = CODING_SYSTEM_SPEC (coding_system);
20911 eoltype = Qnil;
20912
20913 if (!VECTORP (val)) /* Not yet decided. */
20914 {
20915 if (multibyte)
20916 *buf++ = '-';
20917 if (eol_flag)
20918 eoltype = eol_mnemonic_undecided;
20919 /* Don't mention EOL conversion if it isn't decided. */
20920 }
20921 else
20922 {
20923 Lisp_Object attrs;
20924 Lisp_Object eolvalue;
20925
20926 attrs = AREF (val, 0);
20927 eolvalue = AREF (val, 2);
20928
20929 if (multibyte)
20930 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20931
20932 if (eol_flag)
20933 {
20934 /* The EOL conversion that is normal on this system. */
20935
20936 if (NILP (eolvalue)) /* Not yet decided. */
20937 eoltype = eol_mnemonic_undecided;
20938 else if (VECTORP (eolvalue)) /* Not yet decided. */
20939 eoltype = eol_mnemonic_undecided;
20940 else /* eolvalue is Qunix, Qdos, or Qmac. */
20941 eoltype = (EQ (eolvalue, Qunix)
20942 ? eol_mnemonic_unix
20943 : (EQ (eolvalue, Qdos) == 1
20944 ? eol_mnemonic_dos : eol_mnemonic_mac));
20945 }
20946 }
20947
20948 if (eol_flag)
20949 {
20950 /* Mention the EOL conversion if it is not the usual one. */
20951 if (STRINGP (eoltype))
20952 {
20953 eol_str = SDATA (eoltype);
20954 eol_str_len = SBYTES (eoltype);
20955 }
20956 else if (CHARACTERP (eoltype))
20957 {
20958 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20959 int c = XFASTINT (eoltype);
20960 eol_str_len = CHAR_STRING (c, tmp);
20961 eol_str = tmp;
20962 }
20963 else
20964 {
20965 eol_str = invalid_eol_type;
20966 eol_str_len = sizeof (invalid_eol_type) - 1;
20967 }
20968 memcpy (buf, eol_str, eol_str_len);
20969 buf += eol_str_len;
20970 }
20971
20972 return buf;
20973 }
20974
20975 /* Return a string for the output of a mode line %-spec for window W,
20976 generated by character C. FIELD_WIDTH > 0 means pad the string
20977 returned with spaces to that value. Return a Lisp string in
20978 *STRING if the resulting string is taken from that Lisp string.
20979
20980 Note we operate on the current buffer for most purposes,
20981 the exception being w->base_line_pos. */
20982
20983 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20984
20985 static const char *
20986 decode_mode_spec (struct window *w, register int c, int field_width,
20987 Lisp_Object *string)
20988 {
20989 Lisp_Object obj;
20990 struct frame *f = XFRAME (WINDOW_FRAME (w));
20991 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20992 struct buffer *b = current_buffer;
20993
20994 obj = Qnil;
20995 *string = Qnil;
20996
20997 switch (c)
20998 {
20999 case '*':
21000 if (!NILP (BVAR (b, read_only)))
21001 return "%";
21002 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21003 return "*";
21004 return "-";
21005
21006 case '+':
21007 /* This differs from %* only for a modified read-only buffer. */
21008 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21009 return "*";
21010 if (!NILP (BVAR (b, read_only)))
21011 return "%";
21012 return "-";
21013
21014 case '&':
21015 /* This differs from %* in ignoring read-only-ness. */
21016 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21017 return "*";
21018 return "-";
21019
21020 case '%':
21021 return "%";
21022
21023 case '[':
21024 {
21025 int i;
21026 char *p;
21027
21028 if (command_loop_level > 5)
21029 return "[[[... ";
21030 p = decode_mode_spec_buf;
21031 for (i = 0; i < command_loop_level; i++)
21032 *p++ = '[';
21033 *p = 0;
21034 return decode_mode_spec_buf;
21035 }
21036
21037 case ']':
21038 {
21039 int i;
21040 char *p;
21041
21042 if (command_loop_level > 5)
21043 return " ...]]]";
21044 p = decode_mode_spec_buf;
21045 for (i = 0; i < command_loop_level; i++)
21046 *p++ = ']';
21047 *p = 0;
21048 return decode_mode_spec_buf;
21049 }
21050
21051 case '-':
21052 {
21053 register int i;
21054
21055 /* Let lots_of_dashes be a string of infinite length. */
21056 if (mode_line_target == MODE_LINE_NOPROP ||
21057 mode_line_target == MODE_LINE_STRING)
21058 return "--";
21059 if (field_width <= 0
21060 || field_width > sizeof (lots_of_dashes))
21061 {
21062 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21063 decode_mode_spec_buf[i] = '-';
21064 decode_mode_spec_buf[i] = '\0';
21065 return decode_mode_spec_buf;
21066 }
21067 else
21068 return lots_of_dashes;
21069 }
21070
21071 case 'b':
21072 obj = BVAR (b, name);
21073 break;
21074
21075 case 'c':
21076 /* %c and %l are ignored in `frame-title-format'.
21077 (In redisplay_internal, the frame title is drawn _before_ the
21078 windows are updated, so the stuff which depends on actual
21079 window contents (such as %l) may fail to render properly, or
21080 even crash emacs.) */
21081 if (mode_line_target == MODE_LINE_TITLE)
21082 return "";
21083 else
21084 {
21085 EMACS_INT col = current_column ();
21086 w->column_number_displayed = make_number (col);
21087 pint2str (decode_mode_spec_buf, field_width, col);
21088 return decode_mode_spec_buf;
21089 }
21090
21091 case 'e':
21092 #ifndef SYSTEM_MALLOC
21093 {
21094 if (NILP (Vmemory_full))
21095 return "";
21096 else
21097 return "!MEM FULL! ";
21098 }
21099 #else
21100 return "";
21101 #endif
21102
21103 case 'F':
21104 /* %F displays the frame name. */
21105 if (!NILP (f->title))
21106 return SSDATA (f->title);
21107 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21108 return SSDATA (f->name);
21109 return "Emacs";
21110
21111 case 'f':
21112 obj = BVAR (b, filename);
21113 break;
21114
21115 case 'i':
21116 {
21117 EMACS_INT size = ZV - BEGV;
21118 pint2str (decode_mode_spec_buf, field_width, size);
21119 return decode_mode_spec_buf;
21120 }
21121
21122 case 'I':
21123 {
21124 EMACS_INT size = ZV - BEGV;
21125 pint2hrstr (decode_mode_spec_buf, field_width, size);
21126 return decode_mode_spec_buf;
21127 }
21128
21129 case 'l':
21130 {
21131 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
21132 EMACS_INT topline, nlines, height;
21133 EMACS_INT junk;
21134
21135 /* %c and %l are ignored in `frame-title-format'. */
21136 if (mode_line_target == MODE_LINE_TITLE)
21137 return "";
21138
21139 startpos = XMARKER (w->start)->charpos;
21140 startpos_byte = marker_byte_position (w->start);
21141 height = WINDOW_TOTAL_LINES (w);
21142
21143 /* If we decided that this buffer isn't suitable for line numbers,
21144 don't forget that too fast. */
21145 if (EQ (w->base_line_pos, w->buffer))
21146 goto no_value;
21147 /* But do forget it, if the window shows a different buffer now. */
21148 else if (BUFFERP (w->base_line_pos))
21149 w->base_line_pos = Qnil;
21150
21151 /* If the buffer is very big, don't waste time. */
21152 if (INTEGERP (Vline_number_display_limit)
21153 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21154 {
21155 w->base_line_pos = Qnil;
21156 w->base_line_number = Qnil;
21157 goto no_value;
21158 }
21159
21160 if (INTEGERP (w->base_line_number)
21161 && INTEGERP (w->base_line_pos)
21162 && XFASTINT (w->base_line_pos) <= startpos)
21163 {
21164 line = XFASTINT (w->base_line_number);
21165 linepos = XFASTINT (w->base_line_pos);
21166 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21167 }
21168 else
21169 {
21170 line = 1;
21171 linepos = BUF_BEGV (b);
21172 linepos_byte = BUF_BEGV_BYTE (b);
21173 }
21174
21175 /* Count lines from base line to window start position. */
21176 nlines = display_count_lines (linepos_byte,
21177 startpos_byte,
21178 startpos, &junk);
21179
21180 topline = nlines + line;
21181
21182 /* Determine a new base line, if the old one is too close
21183 or too far away, or if we did not have one.
21184 "Too close" means it's plausible a scroll-down would
21185 go back past it. */
21186 if (startpos == BUF_BEGV (b))
21187 {
21188 w->base_line_number = make_number (topline);
21189 w->base_line_pos = make_number (BUF_BEGV (b));
21190 }
21191 else if (nlines < height + 25 || nlines > height * 3 + 50
21192 || linepos == BUF_BEGV (b))
21193 {
21194 EMACS_INT limit = BUF_BEGV (b);
21195 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21196 EMACS_INT position;
21197 EMACS_INT distance =
21198 (height * 2 + 30) * line_number_display_limit_width;
21199
21200 if (startpos - distance > limit)
21201 {
21202 limit = startpos - distance;
21203 limit_byte = CHAR_TO_BYTE (limit);
21204 }
21205
21206 nlines = display_count_lines (startpos_byte,
21207 limit_byte,
21208 - (height * 2 + 30),
21209 &position);
21210 /* If we couldn't find the lines we wanted within
21211 line_number_display_limit_width chars per line,
21212 give up on line numbers for this window. */
21213 if (position == limit_byte && limit == startpos - distance)
21214 {
21215 w->base_line_pos = w->buffer;
21216 w->base_line_number = Qnil;
21217 goto no_value;
21218 }
21219
21220 w->base_line_number = make_number (topline - nlines);
21221 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21222 }
21223
21224 /* Now count lines from the start pos to point. */
21225 nlines = display_count_lines (startpos_byte,
21226 PT_BYTE, PT, &junk);
21227
21228 /* Record that we did display the line number. */
21229 line_number_displayed = 1;
21230
21231 /* Make the string to show. */
21232 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21233 return decode_mode_spec_buf;
21234 no_value:
21235 {
21236 char* p = decode_mode_spec_buf;
21237 int pad = field_width - 2;
21238 while (pad-- > 0)
21239 *p++ = ' ';
21240 *p++ = '?';
21241 *p++ = '?';
21242 *p = '\0';
21243 return decode_mode_spec_buf;
21244 }
21245 }
21246 break;
21247
21248 case 'm':
21249 obj = BVAR (b, mode_name);
21250 break;
21251
21252 case 'n':
21253 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21254 return " Narrow";
21255 break;
21256
21257 case 'p':
21258 {
21259 EMACS_INT pos = marker_position (w->start);
21260 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21261
21262 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21263 {
21264 if (pos <= BUF_BEGV (b))
21265 return "All";
21266 else
21267 return "Bottom";
21268 }
21269 else if (pos <= BUF_BEGV (b))
21270 return "Top";
21271 else
21272 {
21273 if (total > 1000000)
21274 /* Do it differently for a large value, to avoid overflow. */
21275 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21276 else
21277 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21278 /* We can't normally display a 3-digit number,
21279 so get us a 2-digit number that is close. */
21280 if (total == 100)
21281 total = 99;
21282 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21283 return decode_mode_spec_buf;
21284 }
21285 }
21286
21287 /* Display percentage of size above the bottom of the screen. */
21288 case 'P':
21289 {
21290 EMACS_INT toppos = marker_position (w->start);
21291 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21292 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21293
21294 if (botpos >= BUF_ZV (b))
21295 {
21296 if (toppos <= BUF_BEGV (b))
21297 return "All";
21298 else
21299 return "Bottom";
21300 }
21301 else
21302 {
21303 if (total > 1000000)
21304 /* Do it differently for a large value, to avoid overflow. */
21305 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21306 else
21307 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21308 /* We can't normally display a 3-digit number,
21309 so get us a 2-digit number that is close. */
21310 if (total == 100)
21311 total = 99;
21312 if (toppos <= BUF_BEGV (b))
21313 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21314 else
21315 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21316 return decode_mode_spec_buf;
21317 }
21318 }
21319
21320 case 's':
21321 /* status of process */
21322 obj = Fget_buffer_process (Fcurrent_buffer ());
21323 if (NILP (obj))
21324 return "no process";
21325 #ifndef MSDOS
21326 obj = Fsymbol_name (Fprocess_status (obj));
21327 #endif
21328 break;
21329
21330 case '@':
21331 {
21332 int count = inhibit_garbage_collection ();
21333 Lisp_Object val = call1 (intern ("file-remote-p"),
21334 BVAR (current_buffer, directory));
21335 unbind_to (count, Qnil);
21336
21337 if (NILP (val))
21338 return "-";
21339 else
21340 return "@";
21341 }
21342
21343 case 't': /* indicate TEXT or BINARY */
21344 return "T";
21345
21346 case 'z':
21347 /* coding-system (not including end-of-line format) */
21348 case 'Z':
21349 /* coding-system (including end-of-line type) */
21350 {
21351 int eol_flag = (c == 'Z');
21352 char *p = decode_mode_spec_buf;
21353
21354 if (! FRAME_WINDOW_P (f))
21355 {
21356 /* No need to mention EOL here--the terminal never needs
21357 to do EOL conversion. */
21358 p = decode_mode_spec_coding (CODING_ID_NAME
21359 (FRAME_KEYBOARD_CODING (f)->id),
21360 p, 0);
21361 p = decode_mode_spec_coding (CODING_ID_NAME
21362 (FRAME_TERMINAL_CODING (f)->id),
21363 p, 0);
21364 }
21365 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21366 p, eol_flag);
21367
21368 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21369 #ifdef subprocesses
21370 obj = Fget_buffer_process (Fcurrent_buffer ());
21371 if (PROCESSP (obj))
21372 {
21373 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21374 p, eol_flag);
21375 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21376 p, eol_flag);
21377 }
21378 #endif /* subprocesses */
21379 #endif /* 0 */
21380 *p = 0;
21381 return decode_mode_spec_buf;
21382 }
21383 }
21384
21385 if (STRINGP (obj))
21386 {
21387 *string = obj;
21388 return SSDATA (obj);
21389 }
21390 else
21391 return "";
21392 }
21393
21394
21395 /* Count up to COUNT lines starting from START_BYTE.
21396 But don't go beyond LIMIT_BYTE.
21397 Return the number of lines thus found (always nonnegative).
21398
21399 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21400
21401 static EMACS_INT
21402 display_count_lines (EMACS_INT start_byte,
21403 EMACS_INT limit_byte, EMACS_INT count,
21404 EMACS_INT *byte_pos_ptr)
21405 {
21406 register unsigned char *cursor;
21407 unsigned char *base;
21408
21409 register EMACS_INT ceiling;
21410 register unsigned char *ceiling_addr;
21411 EMACS_INT orig_count = count;
21412
21413 /* If we are not in selective display mode,
21414 check only for newlines. */
21415 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21416 && !INTEGERP (BVAR (current_buffer, selective_display)));
21417
21418 if (count > 0)
21419 {
21420 while (start_byte < limit_byte)
21421 {
21422 ceiling = BUFFER_CEILING_OF (start_byte);
21423 ceiling = min (limit_byte - 1, ceiling);
21424 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21425 base = (cursor = BYTE_POS_ADDR (start_byte));
21426 while (1)
21427 {
21428 if (selective_display)
21429 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21430 ;
21431 else
21432 while (*cursor != '\n' && ++cursor != ceiling_addr)
21433 ;
21434
21435 if (cursor != ceiling_addr)
21436 {
21437 if (--count == 0)
21438 {
21439 start_byte += cursor - base + 1;
21440 *byte_pos_ptr = start_byte;
21441 return orig_count;
21442 }
21443 else
21444 if (++cursor == ceiling_addr)
21445 break;
21446 }
21447 else
21448 break;
21449 }
21450 start_byte += cursor - base;
21451 }
21452 }
21453 else
21454 {
21455 while (start_byte > limit_byte)
21456 {
21457 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21458 ceiling = max (limit_byte, ceiling);
21459 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21460 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21461 while (1)
21462 {
21463 if (selective_display)
21464 while (--cursor != ceiling_addr
21465 && *cursor != '\n' && *cursor != 015)
21466 ;
21467 else
21468 while (--cursor != ceiling_addr && *cursor != '\n')
21469 ;
21470
21471 if (cursor != ceiling_addr)
21472 {
21473 if (++count == 0)
21474 {
21475 start_byte += cursor - base + 1;
21476 *byte_pos_ptr = start_byte;
21477 /* When scanning backwards, we should
21478 not count the newline posterior to which we stop. */
21479 return - orig_count - 1;
21480 }
21481 }
21482 else
21483 break;
21484 }
21485 /* Here we add 1 to compensate for the last decrement
21486 of CURSOR, which took it past the valid range. */
21487 start_byte += cursor - base + 1;
21488 }
21489 }
21490
21491 *byte_pos_ptr = limit_byte;
21492
21493 if (count < 0)
21494 return - orig_count + count;
21495 return orig_count - count;
21496
21497 }
21498
21499
21500 \f
21501 /***********************************************************************
21502 Displaying strings
21503 ***********************************************************************/
21504
21505 /* Display a NUL-terminated string, starting with index START.
21506
21507 If STRING is non-null, display that C string. Otherwise, the Lisp
21508 string LISP_STRING is displayed. There's a case that STRING is
21509 non-null and LISP_STRING is not nil. It means STRING is a string
21510 data of LISP_STRING. In that case, we display LISP_STRING while
21511 ignoring its text properties.
21512
21513 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21514 FACE_STRING. Display STRING or LISP_STRING with the face at
21515 FACE_STRING_POS in FACE_STRING:
21516
21517 Display the string in the environment given by IT, but use the
21518 standard display table, temporarily.
21519
21520 FIELD_WIDTH is the minimum number of output glyphs to produce.
21521 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21522 with spaces. If STRING has more characters, more than FIELD_WIDTH
21523 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21524
21525 PRECISION is the maximum number of characters to output from
21526 STRING. PRECISION < 0 means don't truncate the string.
21527
21528 This is roughly equivalent to printf format specifiers:
21529
21530 FIELD_WIDTH PRECISION PRINTF
21531 ----------------------------------------
21532 -1 -1 %s
21533 -1 10 %.10s
21534 10 -1 %10s
21535 20 10 %20.10s
21536
21537 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21538 display them, and < 0 means obey the current buffer's value of
21539 enable_multibyte_characters.
21540
21541 Value is the number of columns displayed. */
21542
21543 static int
21544 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21545 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21546 int field_width, int precision, int max_x, int multibyte)
21547 {
21548 int hpos_at_start = it->hpos;
21549 int saved_face_id = it->face_id;
21550 struct glyph_row *row = it->glyph_row;
21551 EMACS_INT it_charpos;
21552
21553 /* Initialize the iterator IT for iteration over STRING beginning
21554 with index START. */
21555 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21556 precision, field_width, multibyte);
21557 if (string && STRINGP (lisp_string))
21558 /* LISP_STRING is the one returned by decode_mode_spec. We should
21559 ignore its text properties. */
21560 it->stop_charpos = it->end_charpos;
21561
21562 /* If displaying STRING, set up the face of the iterator from
21563 FACE_STRING, if that's given. */
21564 if (STRINGP (face_string))
21565 {
21566 EMACS_INT endptr;
21567 struct face *face;
21568
21569 it->face_id
21570 = face_at_string_position (it->w, face_string, face_string_pos,
21571 0, it->region_beg_charpos,
21572 it->region_end_charpos,
21573 &endptr, it->base_face_id, 0);
21574 face = FACE_FROM_ID (it->f, it->face_id);
21575 it->face_box_p = face->box != FACE_NO_BOX;
21576 }
21577
21578 /* Set max_x to the maximum allowed X position. Don't let it go
21579 beyond the right edge of the window. */
21580 if (max_x <= 0)
21581 max_x = it->last_visible_x;
21582 else
21583 max_x = min (max_x, it->last_visible_x);
21584
21585 /* Skip over display elements that are not visible. because IT->w is
21586 hscrolled. */
21587 if (it->current_x < it->first_visible_x)
21588 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21589 MOVE_TO_POS | MOVE_TO_X);
21590
21591 row->ascent = it->max_ascent;
21592 row->height = it->max_ascent + it->max_descent;
21593 row->phys_ascent = it->max_phys_ascent;
21594 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21595 row->extra_line_spacing = it->max_extra_line_spacing;
21596
21597 if (STRINGP (it->string))
21598 it_charpos = IT_STRING_CHARPOS (*it);
21599 else
21600 it_charpos = IT_CHARPOS (*it);
21601
21602 /* This condition is for the case that we are called with current_x
21603 past last_visible_x. */
21604 while (it->current_x < max_x)
21605 {
21606 int x_before, x, n_glyphs_before, i, nglyphs;
21607
21608 /* Get the next display element. */
21609 if (!get_next_display_element (it))
21610 break;
21611
21612 /* Produce glyphs. */
21613 x_before = it->current_x;
21614 n_glyphs_before = row->used[TEXT_AREA];
21615 PRODUCE_GLYPHS (it);
21616
21617 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21618 i = 0;
21619 x = x_before;
21620 while (i < nglyphs)
21621 {
21622 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21623
21624 if (it->line_wrap != TRUNCATE
21625 && x + glyph->pixel_width > max_x)
21626 {
21627 /* End of continued line or max_x reached. */
21628 if (CHAR_GLYPH_PADDING_P (*glyph))
21629 {
21630 /* A wide character is unbreakable. */
21631 if (row->reversed_p)
21632 unproduce_glyphs (it, row->used[TEXT_AREA]
21633 - n_glyphs_before);
21634 row->used[TEXT_AREA] = n_glyphs_before;
21635 it->current_x = x_before;
21636 }
21637 else
21638 {
21639 if (row->reversed_p)
21640 unproduce_glyphs (it, row->used[TEXT_AREA]
21641 - (n_glyphs_before + i));
21642 row->used[TEXT_AREA] = n_glyphs_before + i;
21643 it->current_x = x;
21644 }
21645 break;
21646 }
21647 else if (x + glyph->pixel_width >= it->first_visible_x)
21648 {
21649 /* Glyph is at least partially visible. */
21650 ++it->hpos;
21651 if (x < it->first_visible_x)
21652 row->x = x - it->first_visible_x;
21653 }
21654 else
21655 {
21656 /* Glyph is off the left margin of the display area.
21657 Should not happen. */
21658 abort ();
21659 }
21660
21661 row->ascent = max (row->ascent, it->max_ascent);
21662 row->height = max (row->height, it->max_ascent + it->max_descent);
21663 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21664 row->phys_height = max (row->phys_height,
21665 it->max_phys_ascent + it->max_phys_descent);
21666 row->extra_line_spacing = max (row->extra_line_spacing,
21667 it->max_extra_line_spacing);
21668 x += glyph->pixel_width;
21669 ++i;
21670 }
21671
21672 /* Stop if max_x reached. */
21673 if (i < nglyphs)
21674 break;
21675
21676 /* Stop at line ends. */
21677 if (ITERATOR_AT_END_OF_LINE_P (it))
21678 {
21679 it->continuation_lines_width = 0;
21680 break;
21681 }
21682
21683 set_iterator_to_next (it, 1);
21684 if (STRINGP (it->string))
21685 it_charpos = IT_STRING_CHARPOS (*it);
21686 else
21687 it_charpos = IT_CHARPOS (*it);
21688
21689 /* Stop if truncating at the right edge. */
21690 if (it->line_wrap == TRUNCATE
21691 && it->current_x >= it->last_visible_x)
21692 {
21693 /* Add truncation mark, but don't do it if the line is
21694 truncated at a padding space. */
21695 if (it_charpos < it->string_nchars)
21696 {
21697 if (!FRAME_WINDOW_P (it->f))
21698 {
21699 int ii, n;
21700
21701 if (it->current_x > it->last_visible_x)
21702 {
21703 if (!row->reversed_p)
21704 {
21705 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21706 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21707 break;
21708 }
21709 else
21710 {
21711 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21712 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21713 break;
21714 unproduce_glyphs (it, ii + 1);
21715 ii = row->used[TEXT_AREA] - (ii + 1);
21716 }
21717 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21718 {
21719 row->used[TEXT_AREA] = ii;
21720 produce_special_glyphs (it, IT_TRUNCATION);
21721 }
21722 }
21723 produce_special_glyphs (it, IT_TRUNCATION);
21724 }
21725 row->truncated_on_right_p = 1;
21726 }
21727 break;
21728 }
21729 }
21730
21731 /* Maybe insert a truncation at the left. */
21732 if (it->first_visible_x
21733 && it_charpos > 0)
21734 {
21735 if (!FRAME_WINDOW_P (it->f))
21736 insert_left_trunc_glyphs (it);
21737 row->truncated_on_left_p = 1;
21738 }
21739
21740 it->face_id = saved_face_id;
21741
21742 /* Value is number of columns displayed. */
21743 return it->hpos - hpos_at_start;
21744 }
21745
21746
21747 \f
21748 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21749 appears as an element of LIST or as the car of an element of LIST.
21750 If PROPVAL is a list, compare each element against LIST in that
21751 way, and return 1/2 if any element of PROPVAL is found in LIST.
21752 Otherwise return 0. This function cannot quit.
21753 The return value is 2 if the text is invisible but with an ellipsis
21754 and 1 if it's invisible and without an ellipsis. */
21755
21756 int
21757 invisible_p (register Lisp_Object propval, Lisp_Object list)
21758 {
21759 register Lisp_Object tail, proptail;
21760
21761 for (tail = list; CONSP (tail); tail = XCDR (tail))
21762 {
21763 register Lisp_Object tem;
21764 tem = XCAR (tail);
21765 if (EQ (propval, tem))
21766 return 1;
21767 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21768 return NILP (XCDR (tem)) ? 1 : 2;
21769 }
21770
21771 if (CONSP (propval))
21772 {
21773 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21774 {
21775 Lisp_Object propelt;
21776 propelt = XCAR (proptail);
21777 for (tail = list; CONSP (tail); tail = XCDR (tail))
21778 {
21779 register Lisp_Object tem;
21780 tem = XCAR (tail);
21781 if (EQ (propelt, tem))
21782 return 1;
21783 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21784 return NILP (XCDR (tem)) ? 1 : 2;
21785 }
21786 }
21787 }
21788
21789 return 0;
21790 }
21791
21792 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21793 doc: /* Non-nil if the property makes the text invisible.
21794 POS-OR-PROP can be a marker or number, in which case it is taken to be
21795 a position in the current buffer and the value of the `invisible' property
21796 is checked; or it can be some other value, which is then presumed to be the
21797 value of the `invisible' property of the text of interest.
21798 The non-nil value returned can be t for truly invisible text or something
21799 else if the text is replaced by an ellipsis. */)
21800 (Lisp_Object pos_or_prop)
21801 {
21802 Lisp_Object prop
21803 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21804 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21805 : pos_or_prop);
21806 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21807 return (invis == 0 ? Qnil
21808 : invis == 1 ? Qt
21809 : make_number (invis));
21810 }
21811
21812 /* Calculate a width or height in pixels from a specification using
21813 the following elements:
21814
21815 SPEC ::=
21816 NUM - a (fractional) multiple of the default font width/height
21817 (NUM) - specifies exactly NUM pixels
21818 UNIT - a fixed number of pixels, see below.
21819 ELEMENT - size of a display element in pixels, see below.
21820 (NUM . SPEC) - equals NUM * SPEC
21821 (+ SPEC SPEC ...) - add pixel values
21822 (- SPEC SPEC ...) - subtract pixel values
21823 (- SPEC) - negate pixel value
21824
21825 NUM ::=
21826 INT or FLOAT - a number constant
21827 SYMBOL - use symbol's (buffer local) variable binding.
21828
21829 UNIT ::=
21830 in - pixels per inch *)
21831 mm - pixels per 1/1000 meter *)
21832 cm - pixels per 1/100 meter *)
21833 width - width of current font in pixels.
21834 height - height of current font in pixels.
21835
21836 *) using the ratio(s) defined in display-pixels-per-inch.
21837
21838 ELEMENT ::=
21839
21840 left-fringe - left fringe width in pixels
21841 right-fringe - right fringe width in pixels
21842
21843 left-margin - left margin width in pixels
21844 right-margin - right margin width in pixels
21845
21846 scroll-bar - scroll-bar area width in pixels
21847
21848 Examples:
21849
21850 Pixels corresponding to 5 inches:
21851 (5 . in)
21852
21853 Total width of non-text areas on left side of window (if scroll-bar is on left):
21854 '(space :width (+ left-fringe left-margin scroll-bar))
21855
21856 Align to first text column (in header line):
21857 '(space :align-to 0)
21858
21859 Align to middle of text area minus half the width of variable `my-image'
21860 containing a loaded image:
21861 '(space :align-to (0.5 . (- text my-image)))
21862
21863 Width of left margin minus width of 1 character in the default font:
21864 '(space :width (- left-margin 1))
21865
21866 Width of left margin minus width of 2 characters in the current font:
21867 '(space :width (- left-margin (2 . width)))
21868
21869 Center 1 character over left-margin (in header line):
21870 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21871
21872 Different ways to express width of left fringe plus left margin minus one pixel:
21873 '(space :width (- (+ left-fringe left-margin) (1)))
21874 '(space :width (+ left-fringe left-margin (- (1))))
21875 '(space :width (+ left-fringe left-margin (-1)))
21876
21877 */
21878
21879 #define NUMVAL(X) \
21880 ((INTEGERP (X) || FLOATP (X)) \
21881 ? XFLOATINT (X) \
21882 : - 1)
21883
21884 static int
21885 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21886 struct font *font, int width_p, int *align_to)
21887 {
21888 double pixels;
21889
21890 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21891 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21892
21893 if (NILP (prop))
21894 return OK_PIXELS (0);
21895
21896 xassert (FRAME_LIVE_P (it->f));
21897
21898 if (SYMBOLP (prop))
21899 {
21900 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21901 {
21902 char *unit = SSDATA (SYMBOL_NAME (prop));
21903
21904 if (unit[0] == 'i' && unit[1] == 'n')
21905 pixels = 1.0;
21906 else if (unit[0] == 'm' && unit[1] == 'm')
21907 pixels = 25.4;
21908 else if (unit[0] == 'c' && unit[1] == 'm')
21909 pixels = 2.54;
21910 else
21911 pixels = 0;
21912 if (pixels > 0)
21913 {
21914 double ppi;
21915 #ifdef HAVE_WINDOW_SYSTEM
21916 if (FRAME_WINDOW_P (it->f)
21917 && (ppi = (width_p
21918 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21919 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21920 ppi > 0))
21921 return OK_PIXELS (ppi / pixels);
21922 #endif
21923
21924 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21925 || (CONSP (Vdisplay_pixels_per_inch)
21926 && (ppi = (width_p
21927 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21928 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21929 ppi > 0)))
21930 return OK_PIXELS (ppi / pixels);
21931
21932 return 0;
21933 }
21934 }
21935
21936 #ifdef HAVE_WINDOW_SYSTEM
21937 if (EQ (prop, Qheight))
21938 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21939 if (EQ (prop, Qwidth))
21940 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21941 #else
21942 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21943 return OK_PIXELS (1);
21944 #endif
21945
21946 if (EQ (prop, Qtext))
21947 return OK_PIXELS (width_p
21948 ? window_box_width (it->w, TEXT_AREA)
21949 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21950
21951 if (align_to && *align_to < 0)
21952 {
21953 *res = 0;
21954 if (EQ (prop, Qleft))
21955 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21956 if (EQ (prop, Qright))
21957 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21958 if (EQ (prop, Qcenter))
21959 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21960 + window_box_width (it->w, TEXT_AREA) / 2);
21961 if (EQ (prop, Qleft_fringe))
21962 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21963 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21964 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21965 if (EQ (prop, Qright_fringe))
21966 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21967 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21968 : window_box_right_offset (it->w, TEXT_AREA));
21969 if (EQ (prop, Qleft_margin))
21970 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21971 if (EQ (prop, Qright_margin))
21972 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21973 if (EQ (prop, Qscroll_bar))
21974 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21975 ? 0
21976 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21977 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21978 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21979 : 0)));
21980 }
21981 else
21982 {
21983 if (EQ (prop, Qleft_fringe))
21984 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21985 if (EQ (prop, Qright_fringe))
21986 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21987 if (EQ (prop, Qleft_margin))
21988 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21989 if (EQ (prop, Qright_margin))
21990 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21991 if (EQ (prop, Qscroll_bar))
21992 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21993 }
21994
21995 prop = Fbuffer_local_value (prop, it->w->buffer);
21996 }
21997
21998 if (INTEGERP (prop) || FLOATP (prop))
21999 {
22000 int base_unit = (width_p
22001 ? FRAME_COLUMN_WIDTH (it->f)
22002 : FRAME_LINE_HEIGHT (it->f));
22003 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22004 }
22005
22006 if (CONSP (prop))
22007 {
22008 Lisp_Object car = XCAR (prop);
22009 Lisp_Object cdr = XCDR (prop);
22010
22011 if (SYMBOLP (car))
22012 {
22013 #ifdef HAVE_WINDOW_SYSTEM
22014 if (FRAME_WINDOW_P (it->f)
22015 && valid_image_p (prop))
22016 {
22017 ptrdiff_t id = lookup_image (it->f, prop);
22018 struct image *img = IMAGE_FROM_ID (it->f, id);
22019
22020 return OK_PIXELS (width_p ? img->width : img->height);
22021 }
22022 #endif
22023 if (EQ (car, Qplus) || EQ (car, Qminus))
22024 {
22025 int first = 1;
22026 double px;
22027
22028 pixels = 0;
22029 while (CONSP (cdr))
22030 {
22031 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22032 font, width_p, align_to))
22033 return 0;
22034 if (first)
22035 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22036 else
22037 pixels += px;
22038 cdr = XCDR (cdr);
22039 }
22040 if (EQ (car, Qminus))
22041 pixels = -pixels;
22042 return OK_PIXELS (pixels);
22043 }
22044
22045 car = Fbuffer_local_value (car, it->w->buffer);
22046 }
22047
22048 if (INTEGERP (car) || FLOATP (car))
22049 {
22050 double fact;
22051 pixels = XFLOATINT (car);
22052 if (NILP (cdr))
22053 return OK_PIXELS (pixels);
22054 if (calc_pixel_width_or_height (&fact, it, cdr,
22055 font, width_p, align_to))
22056 return OK_PIXELS (pixels * fact);
22057 return 0;
22058 }
22059
22060 return 0;
22061 }
22062
22063 return 0;
22064 }
22065
22066 \f
22067 /***********************************************************************
22068 Glyph Display
22069 ***********************************************************************/
22070
22071 #ifdef HAVE_WINDOW_SYSTEM
22072
22073 #if GLYPH_DEBUG
22074
22075 void
22076 dump_glyph_string (struct glyph_string *s)
22077 {
22078 fprintf (stderr, "glyph string\n");
22079 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22080 s->x, s->y, s->width, s->height);
22081 fprintf (stderr, " ybase = %d\n", s->ybase);
22082 fprintf (stderr, " hl = %d\n", s->hl);
22083 fprintf (stderr, " left overhang = %d, right = %d\n",
22084 s->left_overhang, s->right_overhang);
22085 fprintf (stderr, " nchars = %d\n", s->nchars);
22086 fprintf (stderr, " extends to end of line = %d\n",
22087 s->extends_to_end_of_line_p);
22088 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22089 fprintf (stderr, " bg width = %d\n", s->background_width);
22090 }
22091
22092 #endif /* GLYPH_DEBUG */
22093
22094 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22095 of XChar2b structures for S; it can't be allocated in
22096 init_glyph_string because it must be allocated via `alloca'. W
22097 is the window on which S is drawn. ROW and AREA are the glyph row
22098 and area within the row from which S is constructed. START is the
22099 index of the first glyph structure covered by S. HL is a
22100 face-override for drawing S. */
22101
22102 #ifdef HAVE_NTGUI
22103 #define OPTIONAL_HDC(hdc) HDC hdc,
22104 #define DECLARE_HDC(hdc) HDC hdc;
22105 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22106 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22107 #endif
22108
22109 #ifndef OPTIONAL_HDC
22110 #define OPTIONAL_HDC(hdc)
22111 #define DECLARE_HDC(hdc)
22112 #define ALLOCATE_HDC(hdc, f)
22113 #define RELEASE_HDC(hdc, f)
22114 #endif
22115
22116 static void
22117 init_glyph_string (struct glyph_string *s,
22118 OPTIONAL_HDC (hdc)
22119 XChar2b *char2b, struct window *w, struct glyph_row *row,
22120 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22121 {
22122 memset (s, 0, sizeof *s);
22123 s->w = w;
22124 s->f = XFRAME (w->frame);
22125 #ifdef HAVE_NTGUI
22126 s->hdc = hdc;
22127 #endif
22128 s->display = FRAME_X_DISPLAY (s->f);
22129 s->window = FRAME_X_WINDOW (s->f);
22130 s->char2b = char2b;
22131 s->hl = hl;
22132 s->row = row;
22133 s->area = area;
22134 s->first_glyph = row->glyphs[area] + start;
22135 s->height = row->height;
22136 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22137 s->ybase = s->y + row->ascent;
22138 }
22139
22140
22141 /* Append the list of glyph strings with head H and tail T to the list
22142 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22143
22144 static inline void
22145 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22146 struct glyph_string *h, struct glyph_string *t)
22147 {
22148 if (h)
22149 {
22150 if (*head)
22151 (*tail)->next = h;
22152 else
22153 *head = h;
22154 h->prev = *tail;
22155 *tail = t;
22156 }
22157 }
22158
22159
22160 /* Prepend the list of glyph strings with head H and tail T to the
22161 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22162 result. */
22163
22164 static inline void
22165 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22166 struct glyph_string *h, struct glyph_string *t)
22167 {
22168 if (h)
22169 {
22170 if (*head)
22171 (*head)->prev = t;
22172 else
22173 *tail = t;
22174 t->next = *head;
22175 *head = h;
22176 }
22177 }
22178
22179
22180 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22181 Set *HEAD and *TAIL to the resulting list. */
22182
22183 static inline void
22184 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22185 struct glyph_string *s)
22186 {
22187 s->next = s->prev = NULL;
22188 append_glyph_string_lists (head, tail, s, s);
22189 }
22190
22191
22192 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22193 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22194 make sure that X resources for the face returned are allocated.
22195 Value is a pointer to a realized face that is ready for display if
22196 DISPLAY_P is non-zero. */
22197
22198 static inline struct face *
22199 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22200 XChar2b *char2b, int display_p)
22201 {
22202 struct face *face = FACE_FROM_ID (f, face_id);
22203
22204 if (face->font)
22205 {
22206 unsigned code = face->font->driver->encode_char (face->font, c);
22207
22208 if (code != FONT_INVALID_CODE)
22209 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22210 else
22211 STORE_XCHAR2B (char2b, 0, 0);
22212 }
22213
22214 /* Make sure X resources of the face are allocated. */
22215 #ifdef HAVE_X_WINDOWS
22216 if (display_p)
22217 #endif
22218 {
22219 xassert (face != NULL);
22220 PREPARE_FACE_FOR_DISPLAY (f, face);
22221 }
22222
22223 return face;
22224 }
22225
22226
22227 /* Get face and two-byte form of character glyph GLYPH on frame F.
22228 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22229 a pointer to a realized face that is ready for display. */
22230
22231 static inline struct face *
22232 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22233 XChar2b *char2b, int *two_byte_p)
22234 {
22235 struct face *face;
22236
22237 xassert (glyph->type == CHAR_GLYPH);
22238 face = FACE_FROM_ID (f, glyph->face_id);
22239
22240 if (two_byte_p)
22241 *two_byte_p = 0;
22242
22243 if (face->font)
22244 {
22245 unsigned code;
22246
22247 if (CHAR_BYTE8_P (glyph->u.ch))
22248 code = CHAR_TO_BYTE8 (glyph->u.ch);
22249 else
22250 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22251
22252 if (code != FONT_INVALID_CODE)
22253 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22254 else
22255 STORE_XCHAR2B (char2b, 0, 0);
22256 }
22257
22258 /* Make sure X resources of the face are allocated. */
22259 xassert (face != NULL);
22260 PREPARE_FACE_FOR_DISPLAY (f, face);
22261 return face;
22262 }
22263
22264
22265 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22266 Return 1 if FONT has a glyph for C, otherwise return 0. */
22267
22268 static inline int
22269 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22270 {
22271 unsigned code;
22272
22273 if (CHAR_BYTE8_P (c))
22274 code = CHAR_TO_BYTE8 (c);
22275 else
22276 code = font->driver->encode_char (font, c);
22277
22278 if (code == FONT_INVALID_CODE)
22279 return 0;
22280 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22281 return 1;
22282 }
22283
22284
22285 /* Fill glyph string S with composition components specified by S->cmp.
22286
22287 BASE_FACE is the base face of the composition.
22288 S->cmp_from is the index of the first component for S.
22289
22290 OVERLAPS non-zero means S should draw the foreground only, and use
22291 its physical height for clipping. See also draw_glyphs.
22292
22293 Value is the index of a component not in S. */
22294
22295 static int
22296 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22297 int overlaps)
22298 {
22299 int i;
22300 /* For all glyphs of this composition, starting at the offset
22301 S->cmp_from, until we reach the end of the definition or encounter a
22302 glyph that requires the different face, add it to S. */
22303 struct face *face;
22304
22305 xassert (s);
22306
22307 s->for_overlaps = overlaps;
22308 s->face = NULL;
22309 s->font = NULL;
22310 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22311 {
22312 int c = COMPOSITION_GLYPH (s->cmp, i);
22313
22314 /* TAB in a composition means display glyphs with padding space
22315 on the left or right. */
22316 if (c != '\t')
22317 {
22318 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22319 -1, Qnil);
22320
22321 face = get_char_face_and_encoding (s->f, c, face_id,
22322 s->char2b + i, 1);
22323 if (face)
22324 {
22325 if (! s->face)
22326 {
22327 s->face = face;
22328 s->font = s->face->font;
22329 }
22330 else if (s->face != face)
22331 break;
22332 }
22333 }
22334 ++s->nchars;
22335 }
22336 s->cmp_to = i;
22337
22338 if (s->face == NULL)
22339 {
22340 s->face = base_face->ascii_face;
22341 s->font = s->face->font;
22342 }
22343
22344 /* All glyph strings for the same composition has the same width,
22345 i.e. the width set for the first component of the composition. */
22346 s->width = s->first_glyph->pixel_width;
22347
22348 /* If the specified font could not be loaded, use the frame's
22349 default font, but record the fact that we couldn't load it in
22350 the glyph string so that we can draw rectangles for the
22351 characters of the glyph string. */
22352 if (s->font == NULL)
22353 {
22354 s->font_not_found_p = 1;
22355 s->font = FRAME_FONT (s->f);
22356 }
22357
22358 /* Adjust base line for subscript/superscript text. */
22359 s->ybase += s->first_glyph->voffset;
22360
22361 /* This glyph string must always be drawn with 16-bit functions. */
22362 s->two_byte_p = 1;
22363
22364 return s->cmp_to;
22365 }
22366
22367 static int
22368 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22369 int start, int end, int overlaps)
22370 {
22371 struct glyph *glyph, *last;
22372 Lisp_Object lgstring;
22373 int i;
22374
22375 s->for_overlaps = overlaps;
22376 glyph = s->row->glyphs[s->area] + start;
22377 last = s->row->glyphs[s->area] + end;
22378 s->cmp_id = glyph->u.cmp.id;
22379 s->cmp_from = glyph->slice.cmp.from;
22380 s->cmp_to = glyph->slice.cmp.to + 1;
22381 s->face = FACE_FROM_ID (s->f, face_id);
22382 lgstring = composition_gstring_from_id (s->cmp_id);
22383 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22384 glyph++;
22385 while (glyph < last
22386 && glyph->u.cmp.automatic
22387 && glyph->u.cmp.id == s->cmp_id
22388 && s->cmp_to == glyph->slice.cmp.from)
22389 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22390
22391 for (i = s->cmp_from; i < s->cmp_to; i++)
22392 {
22393 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22394 unsigned code = LGLYPH_CODE (lglyph);
22395
22396 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22397 }
22398 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22399 return glyph - s->row->glyphs[s->area];
22400 }
22401
22402
22403 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22404 See the comment of fill_glyph_string for arguments.
22405 Value is the index of the first glyph not in S. */
22406
22407
22408 static int
22409 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22410 int start, int end, int overlaps)
22411 {
22412 struct glyph *glyph, *last;
22413 int voffset;
22414
22415 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22416 s->for_overlaps = overlaps;
22417 glyph = s->row->glyphs[s->area] + start;
22418 last = s->row->glyphs[s->area] + end;
22419 voffset = glyph->voffset;
22420 s->face = FACE_FROM_ID (s->f, face_id);
22421 s->font = s->face->font;
22422 s->nchars = 1;
22423 s->width = glyph->pixel_width;
22424 glyph++;
22425 while (glyph < last
22426 && glyph->type == GLYPHLESS_GLYPH
22427 && glyph->voffset == voffset
22428 && glyph->face_id == face_id)
22429 {
22430 s->nchars++;
22431 s->width += glyph->pixel_width;
22432 glyph++;
22433 }
22434 s->ybase += voffset;
22435 return glyph - s->row->glyphs[s->area];
22436 }
22437
22438
22439 /* Fill glyph string S from a sequence of character glyphs.
22440
22441 FACE_ID is the face id of the string. START is the index of the
22442 first glyph to consider, END is the index of the last + 1.
22443 OVERLAPS non-zero means S should draw the foreground only, and use
22444 its physical height for clipping. See also draw_glyphs.
22445
22446 Value is the index of the first glyph not in S. */
22447
22448 static int
22449 fill_glyph_string (struct glyph_string *s, int face_id,
22450 int start, int end, int overlaps)
22451 {
22452 struct glyph *glyph, *last;
22453 int voffset;
22454 int glyph_not_available_p;
22455
22456 xassert (s->f == XFRAME (s->w->frame));
22457 xassert (s->nchars == 0);
22458 xassert (start >= 0 && end > start);
22459
22460 s->for_overlaps = overlaps;
22461 glyph = s->row->glyphs[s->area] + start;
22462 last = s->row->glyphs[s->area] + end;
22463 voffset = glyph->voffset;
22464 s->padding_p = glyph->padding_p;
22465 glyph_not_available_p = glyph->glyph_not_available_p;
22466
22467 while (glyph < last
22468 && glyph->type == CHAR_GLYPH
22469 && glyph->voffset == voffset
22470 /* Same face id implies same font, nowadays. */
22471 && glyph->face_id == face_id
22472 && glyph->glyph_not_available_p == glyph_not_available_p)
22473 {
22474 int two_byte_p;
22475
22476 s->face = get_glyph_face_and_encoding (s->f, glyph,
22477 s->char2b + s->nchars,
22478 &two_byte_p);
22479 s->two_byte_p = two_byte_p;
22480 ++s->nchars;
22481 xassert (s->nchars <= end - start);
22482 s->width += glyph->pixel_width;
22483 if (glyph++->padding_p != s->padding_p)
22484 break;
22485 }
22486
22487 s->font = s->face->font;
22488
22489 /* If the specified font could not be loaded, use the frame's font,
22490 but record the fact that we couldn't load it in
22491 S->font_not_found_p so that we can draw rectangles for the
22492 characters of the glyph string. */
22493 if (s->font == NULL || glyph_not_available_p)
22494 {
22495 s->font_not_found_p = 1;
22496 s->font = FRAME_FONT (s->f);
22497 }
22498
22499 /* Adjust base line for subscript/superscript text. */
22500 s->ybase += voffset;
22501
22502 xassert (s->face && s->face->gc);
22503 return glyph - s->row->glyphs[s->area];
22504 }
22505
22506
22507 /* Fill glyph string S from image glyph S->first_glyph. */
22508
22509 static void
22510 fill_image_glyph_string (struct glyph_string *s)
22511 {
22512 xassert (s->first_glyph->type == IMAGE_GLYPH);
22513 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22514 xassert (s->img);
22515 s->slice = s->first_glyph->slice.img;
22516 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22517 s->font = s->face->font;
22518 s->width = s->first_glyph->pixel_width;
22519
22520 /* Adjust base line for subscript/superscript text. */
22521 s->ybase += s->first_glyph->voffset;
22522 }
22523
22524
22525 /* Fill glyph string S from a sequence of stretch glyphs.
22526
22527 START is the index of the first glyph to consider,
22528 END is the index of the last + 1.
22529
22530 Value is the index of the first glyph not in S. */
22531
22532 static int
22533 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22534 {
22535 struct glyph *glyph, *last;
22536 int voffset, face_id;
22537
22538 xassert (s->first_glyph->type == STRETCH_GLYPH);
22539
22540 glyph = s->row->glyphs[s->area] + start;
22541 last = s->row->glyphs[s->area] + end;
22542 face_id = glyph->face_id;
22543 s->face = FACE_FROM_ID (s->f, face_id);
22544 s->font = s->face->font;
22545 s->width = glyph->pixel_width;
22546 s->nchars = 1;
22547 voffset = glyph->voffset;
22548
22549 for (++glyph;
22550 (glyph < last
22551 && glyph->type == STRETCH_GLYPH
22552 && glyph->voffset == voffset
22553 && glyph->face_id == face_id);
22554 ++glyph)
22555 s->width += glyph->pixel_width;
22556
22557 /* Adjust base line for subscript/superscript text. */
22558 s->ybase += voffset;
22559
22560 /* The case that face->gc == 0 is handled when drawing the glyph
22561 string by calling PREPARE_FACE_FOR_DISPLAY. */
22562 xassert (s->face);
22563 return glyph - s->row->glyphs[s->area];
22564 }
22565
22566 static struct font_metrics *
22567 get_per_char_metric (struct font *font, XChar2b *char2b)
22568 {
22569 static struct font_metrics metrics;
22570 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22571
22572 if (! font || code == FONT_INVALID_CODE)
22573 return NULL;
22574 font->driver->text_extents (font, &code, 1, &metrics);
22575 return &metrics;
22576 }
22577
22578 /* EXPORT for RIF:
22579 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22580 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22581 assumed to be zero. */
22582
22583 void
22584 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22585 {
22586 *left = *right = 0;
22587
22588 if (glyph->type == CHAR_GLYPH)
22589 {
22590 struct face *face;
22591 XChar2b char2b;
22592 struct font_metrics *pcm;
22593
22594 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22595 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22596 {
22597 if (pcm->rbearing > pcm->width)
22598 *right = pcm->rbearing - pcm->width;
22599 if (pcm->lbearing < 0)
22600 *left = -pcm->lbearing;
22601 }
22602 }
22603 else if (glyph->type == COMPOSITE_GLYPH)
22604 {
22605 if (! glyph->u.cmp.automatic)
22606 {
22607 struct composition *cmp = composition_table[glyph->u.cmp.id];
22608
22609 if (cmp->rbearing > cmp->pixel_width)
22610 *right = cmp->rbearing - cmp->pixel_width;
22611 if (cmp->lbearing < 0)
22612 *left = - cmp->lbearing;
22613 }
22614 else
22615 {
22616 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22617 struct font_metrics metrics;
22618
22619 composition_gstring_width (gstring, glyph->slice.cmp.from,
22620 glyph->slice.cmp.to + 1, &metrics);
22621 if (metrics.rbearing > metrics.width)
22622 *right = metrics.rbearing - metrics.width;
22623 if (metrics.lbearing < 0)
22624 *left = - metrics.lbearing;
22625 }
22626 }
22627 }
22628
22629
22630 /* Return the index of the first glyph preceding glyph string S that
22631 is overwritten by S because of S's left overhang. Value is -1
22632 if no glyphs are overwritten. */
22633
22634 static int
22635 left_overwritten (struct glyph_string *s)
22636 {
22637 int k;
22638
22639 if (s->left_overhang)
22640 {
22641 int x = 0, i;
22642 struct glyph *glyphs = s->row->glyphs[s->area];
22643 int first = s->first_glyph - glyphs;
22644
22645 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22646 x -= glyphs[i].pixel_width;
22647
22648 k = i + 1;
22649 }
22650 else
22651 k = -1;
22652
22653 return k;
22654 }
22655
22656
22657 /* Return the index of the first glyph preceding glyph string S that
22658 is overwriting S because of its right overhang. Value is -1 if no
22659 glyph in front of S overwrites S. */
22660
22661 static int
22662 left_overwriting (struct glyph_string *s)
22663 {
22664 int i, k, x;
22665 struct glyph *glyphs = s->row->glyphs[s->area];
22666 int first = s->first_glyph - glyphs;
22667
22668 k = -1;
22669 x = 0;
22670 for (i = first - 1; i >= 0; --i)
22671 {
22672 int left, right;
22673 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22674 if (x + right > 0)
22675 k = i;
22676 x -= glyphs[i].pixel_width;
22677 }
22678
22679 return k;
22680 }
22681
22682
22683 /* Return the index of the last glyph following glyph string S that is
22684 overwritten by S because of S's right overhang. Value is -1 if
22685 no such glyph is found. */
22686
22687 static int
22688 right_overwritten (struct glyph_string *s)
22689 {
22690 int k = -1;
22691
22692 if (s->right_overhang)
22693 {
22694 int x = 0, i;
22695 struct glyph *glyphs = s->row->glyphs[s->area];
22696 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22697 int end = s->row->used[s->area];
22698
22699 for (i = first; i < end && s->right_overhang > x; ++i)
22700 x += glyphs[i].pixel_width;
22701
22702 k = i;
22703 }
22704
22705 return k;
22706 }
22707
22708
22709 /* Return the index of the last glyph following glyph string S that
22710 overwrites S because of its left overhang. Value is negative
22711 if no such glyph is found. */
22712
22713 static int
22714 right_overwriting (struct glyph_string *s)
22715 {
22716 int i, k, x;
22717 int end = s->row->used[s->area];
22718 struct glyph *glyphs = s->row->glyphs[s->area];
22719 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22720
22721 k = -1;
22722 x = 0;
22723 for (i = first; i < end; ++i)
22724 {
22725 int left, right;
22726 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22727 if (x - left < 0)
22728 k = i;
22729 x += glyphs[i].pixel_width;
22730 }
22731
22732 return k;
22733 }
22734
22735
22736 /* Set background width of glyph string S. START is the index of the
22737 first glyph following S. LAST_X is the right-most x-position + 1
22738 in the drawing area. */
22739
22740 static inline void
22741 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22742 {
22743 /* If the face of this glyph string has to be drawn to the end of
22744 the drawing area, set S->extends_to_end_of_line_p. */
22745
22746 if (start == s->row->used[s->area]
22747 && s->area == TEXT_AREA
22748 && ((s->row->fill_line_p
22749 && (s->hl == DRAW_NORMAL_TEXT
22750 || s->hl == DRAW_IMAGE_RAISED
22751 || s->hl == DRAW_IMAGE_SUNKEN))
22752 || s->hl == DRAW_MOUSE_FACE))
22753 s->extends_to_end_of_line_p = 1;
22754
22755 /* If S extends its face to the end of the line, set its
22756 background_width to the distance to the right edge of the drawing
22757 area. */
22758 if (s->extends_to_end_of_line_p)
22759 s->background_width = last_x - s->x + 1;
22760 else
22761 s->background_width = s->width;
22762 }
22763
22764
22765 /* Compute overhangs and x-positions for glyph string S and its
22766 predecessors, or successors. X is the starting x-position for S.
22767 BACKWARD_P non-zero means process predecessors. */
22768
22769 static void
22770 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22771 {
22772 if (backward_p)
22773 {
22774 while (s)
22775 {
22776 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22777 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22778 x -= s->width;
22779 s->x = x;
22780 s = s->prev;
22781 }
22782 }
22783 else
22784 {
22785 while (s)
22786 {
22787 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22788 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22789 s->x = x;
22790 x += s->width;
22791 s = s->next;
22792 }
22793 }
22794 }
22795
22796
22797
22798 /* The following macros are only called from draw_glyphs below.
22799 They reference the following parameters of that function directly:
22800 `w', `row', `area', and `overlap_p'
22801 as well as the following local variables:
22802 `s', `f', and `hdc' (in W32) */
22803
22804 #ifdef HAVE_NTGUI
22805 /* On W32, silently add local `hdc' variable to argument list of
22806 init_glyph_string. */
22807 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22808 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22809 #else
22810 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22811 init_glyph_string (s, char2b, w, row, area, start, hl)
22812 #endif
22813
22814 /* Add a glyph string for a stretch glyph to the list of strings
22815 between HEAD and TAIL. START is the index of the stretch glyph in
22816 row area AREA of glyph row ROW. END is the index of the last glyph
22817 in that glyph row area. X is the current output position assigned
22818 to the new glyph string constructed. HL overrides that face of the
22819 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22820 is the right-most x-position of the drawing area. */
22821
22822 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22823 and below -- keep them on one line. */
22824 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22825 do \
22826 { \
22827 s = (struct glyph_string *) alloca (sizeof *s); \
22828 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22829 START = fill_stretch_glyph_string (s, START, END); \
22830 append_glyph_string (&HEAD, &TAIL, s); \
22831 s->x = (X); \
22832 } \
22833 while (0)
22834
22835
22836 /* Add a glyph string for an image glyph to the list of strings
22837 between HEAD and TAIL. START is the index of the image glyph in
22838 row area AREA of glyph row ROW. END is the index of the last glyph
22839 in that glyph row area. X is the current output position assigned
22840 to the new glyph string constructed. HL overrides that face of the
22841 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22842 is the right-most x-position of the drawing area. */
22843
22844 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22845 do \
22846 { \
22847 s = (struct glyph_string *) alloca (sizeof *s); \
22848 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22849 fill_image_glyph_string (s); \
22850 append_glyph_string (&HEAD, &TAIL, s); \
22851 ++START; \
22852 s->x = (X); \
22853 } \
22854 while (0)
22855
22856
22857 /* Add a glyph string for a sequence of character glyphs to the list
22858 of strings between HEAD and TAIL. START is the index of the first
22859 glyph in row area AREA of glyph row ROW that is part of the new
22860 glyph string. END is the index of the last glyph in that glyph row
22861 area. X is the current output position assigned to the new glyph
22862 string constructed. HL overrides that face of the glyph; e.g. it
22863 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22864 right-most x-position of the drawing area. */
22865
22866 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22867 do \
22868 { \
22869 int face_id; \
22870 XChar2b *char2b; \
22871 \
22872 face_id = (row)->glyphs[area][START].face_id; \
22873 \
22874 s = (struct glyph_string *) alloca (sizeof *s); \
22875 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22876 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22877 append_glyph_string (&HEAD, &TAIL, s); \
22878 s->x = (X); \
22879 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22880 } \
22881 while (0)
22882
22883
22884 /* Add a glyph string for a composite sequence to the list of strings
22885 between HEAD and TAIL. START is the index of the first glyph in
22886 row area AREA of glyph row ROW that is part of the new glyph
22887 string. END is the index of the last glyph in that glyph row area.
22888 X is the current output position assigned to the new glyph string
22889 constructed. HL overrides that face of the glyph; e.g. it is
22890 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22891 x-position of the drawing area. */
22892
22893 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22894 do { \
22895 int face_id = (row)->glyphs[area][START].face_id; \
22896 struct face *base_face = FACE_FROM_ID (f, face_id); \
22897 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22898 struct composition *cmp = composition_table[cmp_id]; \
22899 XChar2b *char2b; \
22900 struct glyph_string *first_s = NULL; \
22901 int n; \
22902 \
22903 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22904 \
22905 /* Make glyph_strings for each glyph sequence that is drawable by \
22906 the same face, and append them to HEAD/TAIL. */ \
22907 for (n = 0; n < cmp->glyph_len;) \
22908 { \
22909 s = (struct glyph_string *) alloca (sizeof *s); \
22910 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22911 append_glyph_string (&(HEAD), &(TAIL), s); \
22912 s->cmp = cmp; \
22913 s->cmp_from = n; \
22914 s->x = (X); \
22915 if (n == 0) \
22916 first_s = s; \
22917 n = fill_composite_glyph_string (s, base_face, overlaps); \
22918 } \
22919 \
22920 ++START; \
22921 s = first_s; \
22922 } while (0)
22923
22924
22925 /* Add a glyph string for a glyph-string sequence to the list of strings
22926 between HEAD and TAIL. */
22927
22928 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22929 do { \
22930 int face_id; \
22931 XChar2b *char2b; \
22932 Lisp_Object gstring; \
22933 \
22934 face_id = (row)->glyphs[area][START].face_id; \
22935 gstring = (composition_gstring_from_id \
22936 ((row)->glyphs[area][START].u.cmp.id)); \
22937 s = (struct glyph_string *) alloca (sizeof *s); \
22938 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22939 * LGSTRING_GLYPH_LEN (gstring)); \
22940 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22941 append_glyph_string (&(HEAD), &(TAIL), s); \
22942 s->x = (X); \
22943 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22944 } while (0)
22945
22946
22947 /* Add a glyph string for a sequence of glyphless character's glyphs
22948 to the list of strings between HEAD and TAIL. The meanings of
22949 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22950
22951 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22952 do \
22953 { \
22954 int face_id; \
22955 \
22956 face_id = (row)->glyphs[area][START].face_id; \
22957 \
22958 s = (struct glyph_string *) alloca (sizeof *s); \
22959 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22960 append_glyph_string (&HEAD, &TAIL, s); \
22961 s->x = (X); \
22962 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22963 overlaps); \
22964 } \
22965 while (0)
22966
22967
22968 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22969 of AREA of glyph row ROW on window W between indices START and END.
22970 HL overrides the face for drawing glyph strings, e.g. it is
22971 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22972 x-positions of the drawing area.
22973
22974 This is an ugly monster macro construct because we must use alloca
22975 to allocate glyph strings (because draw_glyphs can be called
22976 asynchronously). */
22977
22978 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22979 do \
22980 { \
22981 HEAD = TAIL = NULL; \
22982 while (START < END) \
22983 { \
22984 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22985 switch (first_glyph->type) \
22986 { \
22987 case CHAR_GLYPH: \
22988 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22989 HL, X, LAST_X); \
22990 break; \
22991 \
22992 case COMPOSITE_GLYPH: \
22993 if (first_glyph->u.cmp.automatic) \
22994 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22995 HL, X, LAST_X); \
22996 else \
22997 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22998 HL, X, LAST_X); \
22999 break; \
23000 \
23001 case STRETCH_GLYPH: \
23002 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23003 HL, X, LAST_X); \
23004 break; \
23005 \
23006 case IMAGE_GLYPH: \
23007 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23008 HL, X, LAST_X); \
23009 break; \
23010 \
23011 case GLYPHLESS_GLYPH: \
23012 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23013 HL, X, LAST_X); \
23014 break; \
23015 \
23016 default: \
23017 abort (); \
23018 } \
23019 \
23020 if (s) \
23021 { \
23022 set_glyph_string_background_width (s, START, LAST_X); \
23023 (X) += s->width; \
23024 } \
23025 } \
23026 } while (0)
23027
23028
23029 /* Draw glyphs between START and END in AREA of ROW on window W,
23030 starting at x-position X. X is relative to AREA in W. HL is a
23031 face-override with the following meaning:
23032
23033 DRAW_NORMAL_TEXT draw normally
23034 DRAW_CURSOR draw in cursor face
23035 DRAW_MOUSE_FACE draw in mouse face.
23036 DRAW_INVERSE_VIDEO draw in mode line face
23037 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23038 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23039
23040 If OVERLAPS is non-zero, draw only the foreground of characters and
23041 clip to the physical height of ROW. Non-zero value also defines
23042 the overlapping part to be drawn:
23043
23044 OVERLAPS_PRED overlap with preceding rows
23045 OVERLAPS_SUCC overlap with succeeding rows
23046 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23047 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23048
23049 Value is the x-position reached, relative to AREA of W. */
23050
23051 static int
23052 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23053 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
23054 enum draw_glyphs_face hl, int overlaps)
23055 {
23056 struct glyph_string *head, *tail;
23057 struct glyph_string *s;
23058 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23059 int i, j, x_reached, last_x, area_left = 0;
23060 struct frame *f = XFRAME (WINDOW_FRAME (w));
23061 DECLARE_HDC (hdc);
23062
23063 ALLOCATE_HDC (hdc, f);
23064
23065 /* Let's rather be paranoid than getting a SEGV. */
23066 end = min (end, row->used[area]);
23067 start = max (0, start);
23068 start = min (end, start);
23069
23070 /* Translate X to frame coordinates. Set last_x to the right
23071 end of the drawing area. */
23072 if (row->full_width_p)
23073 {
23074 /* X is relative to the left edge of W, without scroll bars
23075 or fringes. */
23076 area_left = WINDOW_LEFT_EDGE_X (w);
23077 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23078 }
23079 else
23080 {
23081 area_left = window_box_left (w, area);
23082 last_x = area_left + window_box_width (w, area);
23083 }
23084 x += area_left;
23085
23086 /* Build a doubly-linked list of glyph_string structures between
23087 head and tail from what we have to draw. Note that the macro
23088 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23089 the reason we use a separate variable `i'. */
23090 i = start;
23091 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23092 if (tail)
23093 x_reached = tail->x + tail->background_width;
23094 else
23095 x_reached = x;
23096
23097 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23098 the row, redraw some glyphs in front or following the glyph
23099 strings built above. */
23100 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23101 {
23102 struct glyph_string *h, *t;
23103 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23104 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23105 int check_mouse_face = 0;
23106 int dummy_x = 0;
23107
23108 /* If mouse highlighting is on, we may need to draw adjacent
23109 glyphs using mouse-face highlighting. */
23110 if (area == TEXT_AREA && row->mouse_face_p)
23111 {
23112 struct glyph_row *mouse_beg_row, *mouse_end_row;
23113
23114 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23115 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23116
23117 if (row >= mouse_beg_row && row <= mouse_end_row)
23118 {
23119 check_mouse_face = 1;
23120 mouse_beg_col = (row == mouse_beg_row)
23121 ? hlinfo->mouse_face_beg_col : 0;
23122 mouse_end_col = (row == mouse_end_row)
23123 ? hlinfo->mouse_face_end_col
23124 : row->used[TEXT_AREA];
23125 }
23126 }
23127
23128 /* Compute overhangs for all glyph strings. */
23129 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23130 for (s = head; s; s = s->next)
23131 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23132
23133 /* Prepend glyph strings for glyphs in front of the first glyph
23134 string that are overwritten because of the first glyph
23135 string's left overhang. The background of all strings
23136 prepended must be drawn because the first glyph string
23137 draws over it. */
23138 i = left_overwritten (head);
23139 if (i >= 0)
23140 {
23141 enum draw_glyphs_face overlap_hl;
23142
23143 /* If this row contains mouse highlighting, attempt to draw
23144 the overlapped glyphs with the correct highlight. This
23145 code fails if the overlap encompasses more than one glyph
23146 and mouse-highlight spans only some of these glyphs.
23147 However, making it work perfectly involves a lot more
23148 code, and I don't know if the pathological case occurs in
23149 practice, so we'll stick to this for now. --- cyd */
23150 if (check_mouse_face
23151 && mouse_beg_col < start && mouse_end_col > i)
23152 overlap_hl = DRAW_MOUSE_FACE;
23153 else
23154 overlap_hl = DRAW_NORMAL_TEXT;
23155
23156 j = i;
23157 BUILD_GLYPH_STRINGS (j, start, h, t,
23158 overlap_hl, dummy_x, last_x);
23159 start = i;
23160 compute_overhangs_and_x (t, head->x, 1);
23161 prepend_glyph_string_lists (&head, &tail, h, t);
23162 clip_head = head;
23163 }
23164
23165 /* Prepend glyph strings for glyphs in front of the first glyph
23166 string that overwrite that glyph string because of their
23167 right overhang. For these strings, only the foreground must
23168 be drawn, because it draws over the glyph string at `head'.
23169 The background must not be drawn because this would overwrite
23170 right overhangs of preceding glyphs for which no glyph
23171 strings exist. */
23172 i = left_overwriting (head);
23173 if (i >= 0)
23174 {
23175 enum draw_glyphs_face overlap_hl;
23176
23177 if (check_mouse_face
23178 && mouse_beg_col < start && mouse_end_col > i)
23179 overlap_hl = DRAW_MOUSE_FACE;
23180 else
23181 overlap_hl = DRAW_NORMAL_TEXT;
23182
23183 clip_head = head;
23184 BUILD_GLYPH_STRINGS (i, start, h, t,
23185 overlap_hl, dummy_x, last_x);
23186 for (s = h; s; s = s->next)
23187 s->background_filled_p = 1;
23188 compute_overhangs_and_x (t, head->x, 1);
23189 prepend_glyph_string_lists (&head, &tail, h, t);
23190 }
23191
23192 /* Append glyphs strings for glyphs following the last glyph
23193 string tail that are overwritten by tail. The background of
23194 these strings has to be drawn because tail's foreground draws
23195 over it. */
23196 i = right_overwritten (tail);
23197 if (i >= 0)
23198 {
23199 enum draw_glyphs_face overlap_hl;
23200
23201 if (check_mouse_face
23202 && mouse_beg_col < i && mouse_end_col > end)
23203 overlap_hl = DRAW_MOUSE_FACE;
23204 else
23205 overlap_hl = DRAW_NORMAL_TEXT;
23206
23207 BUILD_GLYPH_STRINGS (end, i, h, t,
23208 overlap_hl, x, last_x);
23209 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23210 we don't have `end = i;' here. */
23211 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23212 append_glyph_string_lists (&head, &tail, h, t);
23213 clip_tail = tail;
23214 }
23215
23216 /* Append glyph strings for glyphs following the last glyph
23217 string tail that overwrite tail. The foreground of such
23218 glyphs has to be drawn because it writes into the background
23219 of tail. The background must not be drawn because it could
23220 paint over the foreground of following glyphs. */
23221 i = right_overwriting (tail);
23222 if (i >= 0)
23223 {
23224 enum draw_glyphs_face overlap_hl;
23225 if (check_mouse_face
23226 && mouse_beg_col < i && mouse_end_col > end)
23227 overlap_hl = DRAW_MOUSE_FACE;
23228 else
23229 overlap_hl = DRAW_NORMAL_TEXT;
23230
23231 clip_tail = tail;
23232 i++; /* We must include the Ith glyph. */
23233 BUILD_GLYPH_STRINGS (end, i, h, t,
23234 overlap_hl, x, last_x);
23235 for (s = h; s; s = s->next)
23236 s->background_filled_p = 1;
23237 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23238 append_glyph_string_lists (&head, &tail, h, t);
23239 }
23240 if (clip_head || clip_tail)
23241 for (s = head; s; s = s->next)
23242 {
23243 s->clip_head = clip_head;
23244 s->clip_tail = clip_tail;
23245 }
23246 }
23247
23248 /* Draw all strings. */
23249 for (s = head; s; s = s->next)
23250 FRAME_RIF (f)->draw_glyph_string (s);
23251
23252 #ifndef HAVE_NS
23253 /* When focus a sole frame and move horizontally, this sets on_p to 0
23254 causing a failure to erase prev cursor position. */
23255 if (area == TEXT_AREA
23256 && !row->full_width_p
23257 /* When drawing overlapping rows, only the glyph strings'
23258 foreground is drawn, which doesn't erase a cursor
23259 completely. */
23260 && !overlaps)
23261 {
23262 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23263 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23264 : (tail ? tail->x + tail->background_width : x));
23265 x0 -= area_left;
23266 x1 -= area_left;
23267
23268 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23269 row->y, MATRIX_ROW_BOTTOM_Y (row));
23270 }
23271 #endif
23272
23273 /* Value is the x-position up to which drawn, relative to AREA of W.
23274 This doesn't include parts drawn because of overhangs. */
23275 if (row->full_width_p)
23276 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23277 else
23278 x_reached -= area_left;
23279
23280 RELEASE_HDC (hdc, f);
23281
23282 return x_reached;
23283 }
23284
23285 /* Expand row matrix if too narrow. Don't expand if area
23286 is not present. */
23287
23288 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23289 { \
23290 if (!fonts_changed_p \
23291 && (it->glyph_row->glyphs[area] \
23292 < it->glyph_row->glyphs[area + 1])) \
23293 { \
23294 it->w->ncols_scale_factor++; \
23295 fonts_changed_p = 1; \
23296 } \
23297 }
23298
23299 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23300 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23301
23302 static inline void
23303 append_glyph (struct it *it)
23304 {
23305 struct glyph *glyph;
23306 enum glyph_row_area area = it->area;
23307
23308 xassert (it->glyph_row);
23309 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23310
23311 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23312 if (glyph < it->glyph_row->glyphs[area + 1])
23313 {
23314 /* If the glyph row is reversed, we need to prepend the glyph
23315 rather than append it. */
23316 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23317 {
23318 struct glyph *g;
23319
23320 /* Make room for the additional glyph. */
23321 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23322 g[1] = *g;
23323 glyph = it->glyph_row->glyphs[area];
23324 }
23325 glyph->charpos = CHARPOS (it->position);
23326 glyph->object = it->object;
23327 if (it->pixel_width > 0)
23328 {
23329 glyph->pixel_width = it->pixel_width;
23330 glyph->padding_p = 0;
23331 }
23332 else
23333 {
23334 /* Assure at least 1-pixel width. Otherwise, cursor can't
23335 be displayed correctly. */
23336 glyph->pixel_width = 1;
23337 glyph->padding_p = 1;
23338 }
23339 glyph->ascent = it->ascent;
23340 glyph->descent = it->descent;
23341 glyph->voffset = it->voffset;
23342 glyph->type = CHAR_GLYPH;
23343 glyph->avoid_cursor_p = it->avoid_cursor_p;
23344 glyph->multibyte_p = it->multibyte_p;
23345 glyph->left_box_line_p = it->start_of_box_run_p;
23346 glyph->right_box_line_p = it->end_of_box_run_p;
23347 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23348 || it->phys_descent > it->descent);
23349 glyph->glyph_not_available_p = it->glyph_not_available_p;
23350 glyph->face_id = it->face_id;
23351 glyph->u.ch = it->char_to_display;
23352 glyph->slice.img = null_glyph_slice;
23353 glyph->font_type = FONT_TYPE_UNKNOWN;
23354 if (it->bidi_p)
23355 {
23356 glyph->resolved_level = it->bidi_it.resolved_level;
23357 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23358 abort ();
23359 glyph->bidi_type = it->bidi_it.type;
23360 }
23361 else
23362 {
23363 glyph->resolved_level = 0;
23364 glyph->bidi_type = UNKNOWN_BT;
23365 }
23366 ++it->glyph_row->used[area];
23367 }
23368 else
23369 IT_EXPAND_MATRIX_WIDTH (it, area);
23370 }
23371
23372 /* Store one glyph for the composition IT->cmp_it.id in
23373 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23374 non-null. */
23375
23376 static inline void
23377 append_composite_glyph (struct it *it)
23378 {
23379 struct glyph *glyph;
23380 enum glyph_row_area area = it->area;
23381
23382 xassert (it->glyph_row);
23383
23384 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23385 if (glyph < it->glyph_row->glyphs[area + 1])
23386 {
23387 /* If the glyph row is reversed, we need to prepend the glyph
23388 rather than append it. */
23389 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23390 {
23391 struct glyph *g;
23392
23393 /* Make room for the new glyph. */
23394 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23395 g[1] = *g;
23396 glyph = it->glyph_row->glyphs[it->area];
23397 }
23398 glyph->charpos = it->cmp_it.charpos;
23399 glyph->object = it->object;
23400 glyph->pixel_width = it->pixel_width;
23401 glyph->ascent = it->ascent;
23402 glyph->descent = it->descent;
23403 glyph->voffset = it->voffset;
23404 glyph->type = COMPOSITE_GLYPH;
23405 if (it->cmp_it.ch < 0)
23406 {
23407 glyph->u.cmp.automatic = 0;
23408 glyph->u.cmp.id = it->cmp_it.id;
23409 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23410 }
23411 else
23412 {
23413 glyph->u.cmp.automatic = 1;
23414 glyph->u.cmp.id = it->cmp_it.id;
23415 glyph->slice.cmp.from = it->cmp_it.from;
23416 glyph->slice.cmp.to = it->cmp_it.to - 1;
23417 }
23418 glyph->avoid_cursor_p = it->avoid_cursor_p;
23419 glyph->multibyte_p = it->multibyte_p;
23420 glyph->left_box_line_p = it->start_of_box_run_p;
23421 glyph->right_box_line_p = it->end_of_box_run_p;
23422 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23423 || it->phys_descent > it->descent);
23424 glyph->padding_p = 0;
23425 glyph->glyph_not_available_p = 0;
23426 glyph->face_id = it->face_id;
23427 glyph->font_type = FONT_TYPE_UNKNOWN;
23428 if (it->bidi_p)
23429 {
23430 glyph->resolved_level = it->bidi_it.resolved_level;
23431 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23432 abort ();
23433 glyph->bidi_type = it->bidi_it.type;
23434 }
23435 ++it->glyph_row->used[area];
23436 }
23437 else
23438 IT_EXPAND_MATRIX_WIDTH (it, area);
23439 }
23440
23441
23442 /* Change IT->ascent and IT->height according to the setting of
23443 IT->voffset. */
23444
23445 static inline void
23446 take_vertical_position_into_account (struct it *it)
23447 {
23448 if (it->voffset)
23449 {
23450 if (it->voffset < 0)
23451 /* Increase the ascent so that we can display the text higher
23452 in the line. */
23453 it->ascent -= it->voffset;
23454 else
23455 /* Increase the descent so that we can display the text lower
23456 in the line. */
23457 it->descent += it->voffset;
23458 }
23459 }
23460
23461
23462 /* Produce glyphs/get display metrics for the image IT is loaded with.
23463 See the description of struct display_iterator in dispextern.h for
23464 an overview of struct display_iterator. */
23465
23466 static void
23467 produce_image_glyph (struct it *it)
23468 {
23469 struct image *img;
23470 struct face *face;
23471 int glyph_ascent, crop;
23472 struct glyph_slice slice;
23473
23474 xassert (it->what == IT_IMAGE);
23475
23476 face = FACE_FROM_ID (it->f, it->face_id);
23477 xassert (face);
23478 /* Make sure X resources of the face is loaded. */
23479 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23480
23481 if (it->image_id < 0)
23482 {
23483 /* Fringe bitmap. */
23484 it->ascent = it->phys_ascent = 0;
23485 it->descent = it->phys_descent = 0;
23486 it->pixel_width = 0;
23487 it->nglyphs = 0;
23488 return;
23489 }
23490
23491 img = IMAGE_FROM_ID (it->f, it->image_id);
23492 xassert (img);
23493 /* Make sure X resources of the image is loaded. */
23494 prepare_image_for_display (it->f, img);
23495
23496 slice.x = slice.y = 0;
23497 slice.width = img->width;
23498 slice.height = img->height;
23499
23500 if (INTEGERP (it->slice.x))
23501 slice.x = XINT (it->slice.x);
23502 else if (FLOATP (it->slice.x))
23503 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23504
23505 if (INTEGERP (it->slice.y))
23506 slice.y = XINT (it->slice.y);
23507 else if (FLOATP (it->slice.y))
23508 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23509
23510 if (INTEGERP (it->slice.width))
23511 slice.width = XINT (it->slice.width);
23512 else if (FLOATP (it->slice.width))
23513 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23514
23515 if (INTEGERP (it->slice.height))
23516 slice.height = XINT (it->slice.height);
23517 else if (FLOATP (it->slice.height))
23518 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23519
23520 if (slice.x >= img->width)
23521 slice.x = img->width;
23522 if (slice.y >= img->height)
23523 slice.y = img->height;
23524 if (slice.x + slice.width >= img->width)
23525 slice.width = img->width - slice.x;
23526 if (slice.y + slice.height > img->height)
23527 slice.height = img->height - slice.y;
23528
23529 if (slice.width == 0 || slice.height == 0)
23530 return;
23531
23532 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23533
23534 it->descent = slice.height - glyph_ascent;
23535 if (slice.y == 0)
23536 it->descent += img->vmargin;
23537 if (slice.y + slice.height == img->height)
23538 it->descent += img->vmargin;
23539 it->phys_descent = it->descent;
23540
23541 it->pixel_width = slice.width;
23542 if (slice.x == 0)
23543 it->pixel_width += img->hmargin;
23544 if (slice.x + slice.width == img->width)
23545 it->pixel_width += img->hmargin;
23546
23547 /* It's quite possible for images to have an ascent greater than
23548 their height, so don't get confused in that case. */
23549 if (it->descent < 0)
23550 it->descent = 0;
23551
23552 it->nglyphs = 1;
23553
23554 if (face->box != FACE_NO_BOX)
23555 {
23556 if (face->box_line_width > 0)
23557 {
23558 if (slice.y == 0)
23559 it->ascent += face->box_line_width;
23560 if (slice.y + slice.height == img->height)
23561 it->descent += face->box_line_width;
23562 }
23563
23564 if (it->start_of_box_run_p && slice.x == 0)
23565 it->pixel_width += eabs (face->box_line_width);
23566 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23567 it->pixel_width += eabs (face->box_line_width);
23568 }
23569
23570 take_vertical_position_into_account (it);
23571
23572 /* Automatically crop wide image glyphs at right edge so we can
23573 draw the cursor on same display row. */
23574 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23575 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23576 {
23577 it->pixel_width -= crop;
23578 slice.width -= crop;
23579 }
23580
23581 if (it->glyph_row)
23582 {
23583 struct glyph *glyph;
23584 enum glyph_row_area area = it->area;
23585
23586 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23587 if (glyph < it->glyph_row->glyphs[area + 1])
23588 {
23589 glyph->charpos = CHARPOS (it->position);
23590 glyph->object = it->object;
23591 glyph->pixel_width = it->pixel_width;
23592 glyph->ascent = glyph_ascent;
23593 glyph->descent = it->descent;
23594 glyph->voffset = it->voffset;
23595 glyph->type = IMAGE_GLYPH;
23596 glyph->avoid_cursor_p = it->avoid_cursor_p;
23597 glyph->multibyte_p = it->multibyte_p;
23598 glyph->left_box_line_p = it->start_of_box_run_p;
23599 glyph->right_box_line_p = it->end_of_box_run_p;
23600 glyph->overlaps_vertically_p = 0;
23601 glyph->padding_p = 0;
23602 glyph->glyph_not_available_p = 0;
23603 glyph->face_id = it->face_id;
23604 glyph->u.img_id = img->id;
23605 glyph->slice.img = slice;
23606 glyph->font_type = FONT_TYPE_UNKNOWN;
23607 if (it->bidi_p)
23608 {
23609 glyph->resolved_level = it->bidi_it.resolved_level;
23610 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23611 abort ();
23612 glyph->bidi_type = it->bidi_it.type;
23613 }
23614 ++it->glyph_row->used[area];
23615 }
23616 else
23617 IT_EXPAND_MATRIX_WIDTH (it, area);
23618 }
23619 }
23620
23621
23622 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23623 of the glyph, WIDTH and HEIGHT are the width and height of the
23624 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23625
23626 static void
23627 append_stretch_glyph (struct it *it, Lisp_Object object,
23628 int width, int height, int ascent)
23629 {
23630 struct glyph *glyph;
23631 enum glyph_row_area area = it->area;
23632
23633 xassert (ascent >= 0 && ascent <= height);
23634
23635 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23636 if (glyph < it->glyph_row->glyphs[area + 1])
23637 {
23638 /* If the glyph row is reversed, we need to prepend the glyph
23639 rather than append it. */
23640 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23641 {
23642 struct glyph *g;
23643
23644 /* Make room for the additional glyph. */
23645 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23646 g[1] = *g;
23647 glyph = it->glyph_row->glyphs[area];
23648 }
23649 glyph->charpos = CHARPOS (it->position);
23650 glyph->object = object;
23651 glyph->pixel_width = width;
23652 glyph->ascent = ascent;
23653 glyph->descent = height - ascent;
23654 glyph->voffset = it->voffset;
23655 glyph->type = STRETCH_GLYPH;
23656 glyph->avoid_cursor_p = it->avoid_cursor_p;
23657 glyph->multibyte_p = it->multibyte_p;
23658 glyph->left_box_line_p = it->start_of_box_run_p;
23659 glyph->right_box_line_p = it->end_of_box_run_p;
23660 glyph->overlaps_vertically_p = 0;
23661 glyph->padding_p = 0;
23662 glyph->glyph_not_available_p = 0;
23663 glyph->face_id = it->face_id;
23664 glyph->u.stretch.ascent = ascent;
23665 glyph->u.stretch.height = height;
23666 glyph->slice.img = null_glyph_slice;
23667 glyph->font_type = FONT_TYPE_UNKNOWN;
23668 if (it->bidi_p)
23669 {
23670 glyph->resolved_level = it->bidi_it.resolved_level;
23671 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23672 abort ();
23673 glyph->bidi_type = it->bidi_it.type;
23674 }
23675 else
23676 {
23677 glyph->resolved_level = 0;
23678 glyph->bidi_type = UNKNOWN_BT;
23679 }
23680 ++it->glyph_row->used[area];
23681 }
23682 else
23683 IT_EXPAND_MATRIX_WIDTH (it, area);
23684 }
23685
23686 #endif /* HAVE_WINDOW_SYSTEM */
23687
23688 /* Produce a stretch glyph for iterator IT. IT->object is the value
23689 of the glyph property displayed. The value must be a list
23690 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23691 being recognized:
23692
23693 1. `:width WIDTH' specifies that the space should be WIDTH *
23694 canonical char width wide. WIDTH may be an integer or floating
23695 point number.
23696
23697 2. `:relative-width FACTOR' specifies that the width of the stretch
23698 should be computed from the width of the first character having the
23699 `glyph' property, and should be FACTOR times that width.
23700
23701 3. `:align-to HPOS' specifies that the space should be wide enough
23702 to reach HPOS, a value in canonical character units.
23703
23704 Exactly one of the above pairs must be present.
23705
23706 4. `:height HEIGHT' specifies that the height of the stretch produced
23707 should be HEIGHT, measured in canonical character units.
23708
23709 5. `:relative-height FACTOR' specifies that the height of the
23710 stretch should be FACTOR times the height of the characters having
23711 the glyph property.
23712
23713 Either none or exactly one of 4 or 5 must be present.
23714
23715 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23716 of the stretch should be used for the ascent of the stretch.
23717 ASCENT must be in the range 0 <= ASCENT <= 100. */
23718
23719 void
23720 produce_stretch_glyph (struct it *it)
23721 {
23722 /* (space :width WIDTH :height HEIGHT ...) */
23723 Lisp_Object prop, plist;
23724 int width = 0, height = 0, align_to = -1;
23725 int zero_width_ok_p = 0;
23726 int ascent = 0;
23727 double tem;
23728 struct face *face = NULL;
23729 struct font *font = NULL;
23730
23731 #ifdef HAVE_WINDOW_SYSTEM
23732 int zero_height_ok_p = 0;
23733
23734 if (FRAME_WINDOW_P (it->f))
23735 {
23736 face = FACE_FROM_ID (it->f, it->face_id);
23737 font = face->font ? face->font : FRAME_FONT (it->f);
23738 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23739 }
23740 #endif
23741
23742 /* List should start with `space'. */
23743 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23744 plist = XCDR (it->object);
23745
23746 /* Compute the width of the stretch. */
23747 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23748 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23749 {
23750 /* Absolute width `:width WIDTH' specified and valid. */
23751 zero_width_ok_p = 1;
23752 width = (int)tem;
23753 }
23754 #ifdef HAVE_WINDOW_SYSTEM
23755 else if (FRAME_WINDOW_P (it->f)
23756 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23757 {
23758 /* Relative width `:relative-width FACTOR' specified and valid.
23759 Compute the width of the characters having the `glyph'
23760 property. */
23761 struct it it2;
23762 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23763
23764 it2 = *it;
23765 if (it->multibyte_p)
23766 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23767 else
23768 {
23769 it2.c = it2.char_to_display = *p, it2.len = 1;
23770 if (! ASCII_CHAR_P (it2.c))
23771 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23772 }
23773
23774 it2.glyph_row = NULL;
23775 it2.what = IT_CHARACTER;
23776 x_produce_glyphs (&it2);
23777 width = NUMVAL (prop) * it2.pixel_width;
23778 }
23779 #endif /* HAVE_WINDOW_SYSTEM */
23780 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23781 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23782 {
23783 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23784 align_to = (align_to < 0
23785 ? 0
23786 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23787 else if (align_to < 0)
23788 align_to = window_box_left_offset (it->w, TEXT_AREA);
23789 width = max (0, (int)tem + align_to - it->current_x);
23790 zero_width_ok_p = 1;
23791 }
23792 else
23793 /* Nothing specified -> width defaults to canonical char width. */
23794 width = FRAME_COLUMN_WIDTH (it->f);
23795
23796 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23797 width = 1;
23798
23799 #ifdef HAVE_WINDOW_SYSTEM
23800 /* Compute height. */
23801 if (FRAME_WINDOW_P (it->f))
23802 {
23803 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23804 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23805 {
23806 height = (int)tem;
23807 zero_height_ok_p = 1;
23808 }
23809 else if (prop = Fplist_get (plist, QCrelative_height),
23810 NUMVAL (prop) > 0)
23811 height = FONT_HEIGHT (font) * NUMVAL (prop);
23812 else
23813 height = FONT_HEIGHT (font);
23814
23815 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23816 height = 1;
23817
23818 /* Compute percentage of height used for ascent. If
23819 `:ascent ASCENT' is present and valid, use that. Otherwise,
23820 derive the ascent from the font in use. */
23821 if (prop = Fplist_get (plist, QCascent),
23822 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23823 ascent = height * NUMVAL (prop) / 100.0;
23824 else if (!NILP (prop)
23825 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23826 ascent = min (max (0, (int)tem), height);
23827 else
23828 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23829 }
23830 else
23831 #endif /* HAVE_WINDOW_SYSTEM */
23832 height = 1;
23833
23834 if (width > 0 && it->line_wrap != TRUNCATE
23835 && it->current_x + width > it->last_visible_x)
23836 {
23837 width = it->last_visible_x - it->current_x;
23838 #ifdef HAVE_WINDOW_SYSTEM
23839 /* Subtract one more pixel from the stretch width, but only on
23840 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23841 width -= FRAME_WINDOW_P (it->f);
23842 #endif
23843 }
23844
23845 if (width > 0 && height > 0 && it->glyph_row)
23846 {
23847 Lisp_Object o_object = it->object;
23848 Lisp_Object object = it->stack[it->sp - 1].string;
23849 int n = width;
23850
23851 if (!STRINGP (object))
23852 object = it->w->buffer;
23853 #ifdef HAVE_WINDOW_SYSTEM
23854 if (FRAME_WINDOW_P (it->f))
23855 append_stretch_glyph (it, object, width, height, ascent);
23856 else
23857 #endif
23858 {
23859 it->object = object;
23860 it->char_to_display = ' ';
23861 it->pixel_width = it->len = 1;
23862 while (n--)
23863 tty_append_glyph (it);
23864 it->object = o_object;
23865 }
23866 }
23867
23868 it->pixel_width = width;
23869 #ifdef HAVE_WINDOW_SYSTEM
23870 if (FRAME_WINDOW_P (it->f))
23871 {
23872 it->ascent = it->phys_ascent = ascent;
23873 it->descent = it->phys_descent = height - it->ascent;
23874 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23875 take_vertical_position_into_account (it);
23876 }
23877 else
23878 #endif
23879 it->nglyphs = width;
23880 }
23881
23882 #ifdef HAVE_WINDOW_SYSTEM
23883
23884 /* Calculate line-height and line-spacing properties.
23885 An integer value specifies explicit pixel value.
23886 A float value specifies relative value to current face height.
23887 A cons (float . face-name) specifies relative value to
23888 height of specified face font.
23889
23890 Returns height in pixels, or nil. */
23891
23892
23893 static Lisp_Object
23894 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23895 int boff, int override)
23896 {
23897 Lisp_Object face_name = Qnil;
23898 int ascent, descent, height;
23899
23900 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23901 return val;
23902
23903 if (CONSP (val))
23904 {
23905 face_name = XCAR (val);
23906 val = XCDR (val);
23907 if (!NUMBERP (val))
23908 val = make_number (1);
23909 if (NILP (face_name))
23910 {
23911 height = it->ascent + it->descent;
23912 goto scale;
23913 }
23914 }
23915
23916 if (NILP (face_name))
23917 {
23918 font = FRAME_FONT (it->f);
23919 boff = FRAME_BASELINE_OFFSET (it->f);
23920 }
23921 else if (EQ (face_name, Qt))
23922 {
23923 override = 0;
23924 }
23925 else
23926 {
23927 int face_id;
23928 struct face *face;
23929
23930 face_id = lookup_named_face (it->f, face_name, 0);
23931 if (face_id < 0)
23932 return make_number (-1);
23933
23934 face = FACE_FROM_ID (it->f, face_id);
23935 font = face->font;
23936 if (font == NULL)
23937 return make_number (-1);
23938 boff = font->baseline_offset;
23939 if (font->vertical_centering)
23940 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23941 }
23942
23943 ascent = FONT_BASE (font) + boff;
23944 descent = FONT_DESCENT (font) - boff;
23945
23946 if (override)
23947 {
23948 it->override_ascent = ascent;
23949 it->override_descent = descent;
23950 it->override_boff = boff;
23951 }
23952
23953 height = ascent + descent;
23954
23955 scale:
23956 if (FLOATP (val))
23957 height = (int)(XFLOAT_DATA (val) * height);
23958 else if (INTEGERP (val))
23959 height *= XINT (val);
23960
23961 return make_number (height);
23962 }
23963
23964
23965 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23966 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23967 and only if this is for a character for which no font was found.
23968
23969 If the display method (it->glyphless_method) is
23970 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23971 length of the acronym or the hexadecimal string, UPPER_XOFF and
23972 UPPER_YOFF are pixel offsets for the upper part of the string,
23973 LOWER_XOFF and LOWER_YOFF are for the lower part.
23974
23975 For the other display methods, LEN through LOWER_YOFF are zero. */
23976
23977 static void
23978 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23979 short upper_xoff, short upper_yoff,
23980 short lower_xoff, short lower_yoff)
23981 {
23982 struct glyph *glyph;
23983 enum glyph_row_area area = it->area;
23984
23985 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23986 if (glyph < it->glyph_row->glyphs[area + 1])
23987 {
23988 /* If the glyph row is reversed, we need to prepend the glyph
23989 rather than append it. */
23990 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23991 {
23992 struct glyph *g;
23993
23994 /* Make room for the additional glyph. */
23995 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23996 g[1] = *g;
23997 glyph = it->glyph_row->glyphs[area];
23998 }
23999 glyph->charpos = CHARPOS (it->position);
24000 glyph->object = it->object;
24001 glyph->pixel_width = it->pixel_width;
24002 glyph->ascent = it->ascent;
24003 glyph->descent = it->descent;
24004 glyph->voffset = it->voffset;
24005 glyph->type = GLYPHLESS_GLYPH;
24006 glyph->u.glyphless.method = it->glyphless_method;
24007 glyph->u.glyphless.for_no_font = for_no_font;
24008 glyph->u.glyphless.len = len;
24009 glyph->u.glyphless.ch = it->c;
24010 glyph->slice.glyphless.upper_xoff = upper_xoff;
24011 glyph->slice.glyphless.upper_yoff = upper_yoff;
24012 glyph->slice.glyphless.lower_xoff = lower_xoff;
24013 glyph->slice.glyphless.lower_yoff = lower_yoff;
24014 glyph->avoid_cursor_p = it->avoid_cursor_p;
24015 glyph->multibyte_p = it->multibyte_p;
24016 glyph->left_box_line_p = it->start_of_box_run_p;
24017 glyph->right_box_line_p = it->end_of_box_run_p;
24018 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24019 || it->phys_descent > it->descent);
24020 glyph->padding_p = 0;
24021 glyph->glyph_not_available_p = 0;
24022 glyph->face_id = face_id;
24023 glyph->font_type = FONT_TYPE_UNKNOWN;
24024 if (it->bidi_p)
24025 {
24026 glyph->resolved_level = it->bidi_it.resolved_level;
24027 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24028 abort ();
24029 glyph->bidi_type = it->bidi_it.type;
24030 }
24031 ++it->glyph_row->used[area];
24032 }
24033 else
24034 IT_EXPAND_MATRIX_WIDTH (it, area);
24035 }
24036
24037
24038 /* Produce a glyph for a glyphless character for iterator IT.
24039 IT->glyphless_method specifies which method to use for displaying
24040 the character. See the description of enum
24041 glyphless_display_method in dispextern.h for the detail.
24042
24043 FOR_NO_FONT is nonzero if and only if this is for a character for
24044 which no font was found. ACRONYM, if non-nil, is an acronym string
24045 for the character. */
24046
24047 static void
24048 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24049 {
24050 int face_id;
24051 struct face *face;
24052 struct font *font;
24053 int base_width, base_height, width, height;
24054 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24055 int len;
24056
24057 /* Get the metrics of the base font. We always refer to the current
24058 ASCII face. */
24059 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24060 font = face->font ? face->font : FRAME_FONT (it->f);
24061 it->ascent = FONT_BASE (font) + font->baseline_offset;
24062 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24063 base_height = it->ascent + it->descent;
24064 base_width = font->average_width;
24065
24066 /* Get a face ID for the glyph by utilizing a cache (the same way as
24067 done for `escape-glyph' in get_next_display_element). */
24068 if (it->f == last_glyphless_glyph_frame
24069 && it->face_id == last_glyphless_glyph_face_id)
24070 {
24071 face_id = last_glyphless_glyph_merged_face_id;
24072 }
24073 else
24074 {
24075 /* Merge the `glyphless-char' face into the current face. */
24076 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24077 last_glyphless_glyph_frame = it->f;
24078 last_glyphless_glyph_face_id = it->face_id;
24079 last_glyphless_glyph_merged_face_id = face_id;
24080 }
24081
24082 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24083 {
24084 it->pixel_width = THIN_SPACE_WIDTH;
24085 len = 0;
24086 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24087 }
24088 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24089 {
24090 width = CHAR_WIDTH (it->c);
24091 if (width == 0)
24092 width = 1;
24093 else if (width > 4)
24094 width = 4;
24095 it->pixel_width = base_width * width;
24096 len = 0;
24097 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24098 }
24099 else
24100 {
24101 char buf[7];
24102 const char *str;
24103 unsigned int code[6];
24104 int upper_len;
24105 int ascent, descent;
24106 struct font_metrics metrics_upper, metrics_lower;
24107
24108 face = FACE_FROM_ID (it->f, face_id);
24109 font = face->font ? face->font : FRAME_FONT (it->f);
24110 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24111
24112 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24113 {
24114 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24115 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24116 if (CONSP (acronym))
24117 acronym = XCAR (acronym);
24118 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24119 }
24120 else
24121 {
24122 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24123 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24124 str = buf;
24125 }
24126 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24127 code[len] = font->driver->encode_char (font, str[len]);
24128 upper_len = (len + 1) / 2;
24129 font->driver->text_extents (font, code, upper_len,
24130 &metrics_upper);
24131 font->driver->text_extents (font, code + upper_len, len - upper_len,
24132 &metrics_lower);
24133
24134
24135
24136 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24137 width = max (metrics_upper.width, metrics_lower.width) + 4;
24138 upper_xoff = upper_yoff = 2; /* the typical case */
24139 if (base_width >= width)
24140 {
24141 /* Align the upper to the left, the lower to the right. */
24142 it->pixel_width = base_width;
24143 lower_xoff = base_width - 2 - metrics_lower.width;
24144 }
24145 else
24146 {
24147 /* Center the shorter one. */
24148 it->pixel_width = width;
24149 if (metrics_upper.width >= metrics_lower.width)
24150 lower_xoff = (width - metrics_lower.width) / 2;
24151 else
24152 {
24153 /* FIXME: This code doesn't look right. It formerly was
24154 missing the "lower_xoff = 0;", which couldn't have
24155 been right since it left lower_xoff uninitialized. */
24156 lower_xoff = 0;
24157 upper_xoff = (width - metrics_upper.width) / 2;
24158 }
24159 }
24160
24161 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24162 top, bottom, and between upper and lower strings. */
24163 height = (metrics_upper.ascent + metrics_upper.descent
24164 + metrics_lower.ascent + metrics_lower.descent) + 5;
24165 /* Center vertically.
24166 H:base_height, D:base_descent
24167 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24168
24169 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24170 descent = D - H/2 + h/2;
24171 lower_yoff = descent - 2 - ld;
24172 upper_yoff = lower_yoff - la - 1 - ud; */
24173 ascent = - (it->descent - (base_height + height + 1) / 2);
24174 descent = it->descent - (base_height - height) / 2;
24175 lower_yoff = descent - 2 - metrics_lower.descent;
24176 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24177 - metrics_upper.descent);
24178 /* Don't make the height shorter than the base height. */
24179 if (height > base_height)
24180 {
24181 it->ascent = ascent;
24182 it->descent = descent;
24183 }
24184 }
24185
24186 it->phys_ascent = it->ascent;
24187 it->phys_descent = it->descent;
24188 if (it->glyph_row)
24189 append_glyphless_glyph (it, face_id, for_no_font, len,
24190 upper_xoff, upper_yoff,
24191 lower_xoff, lower_yoff);
24192 it->nglyphs = 1;
24193 take_vertical_position_into_account (it);
24194 }
24195
24196
24197 /* RIF:
24198 Produce glyphs/get display metrics for the display element IT is
24199 loaded with. See the description of struct it in dispextern.h
24200 for an overview of struct it. */
24201
24202 void
24203 x_produce_glyphs (struct it *it)
24204 {
24205 int extra_line_spacing = it->extra_line_spacing;
24206
24207 it->glyph_not_available_p = 0;
24208
24209 if (it->what == IT_CHARACTER)
24210 {
24211 XChar2b char2b;
24212 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24213 struct font *font = face->font;
24214 struct font_metrics *pcm = NULL;
24215 int boff; /* baseline offset */
24216
24217 if (font == NULL)
24218 {
24219 /* When no suitable font is found, display this character by
24220 the method specified in the first extra slot of
24221 Vglyphless_char_display. */
24222 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24223
24224 xassert (it->what == IT_GLYPHLESS);
24225 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24226 goto done;
24227 }
24228
24229 boff = font->baseline_offset;
24230 if (font->vertical_centering)
24231 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24232
24233 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24234 {
24235 int stretched_p;
24236
24237 it->nglyphs = 1;
24238
24239 if (it->override_ascent >= 0)
24240 {
24241 it->ascent = it->override_ascent;
24242 it->descent = it->override_descent;
24243 boff = it->override_boff;
24244 }
24245 else
24246 {
24247 it->ascent = FONT_BASE (font) + boff;
24248 it->descent = FONT_DESCENT (font) - boff;
24249 }
24250
24251 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24252 {
24253 pcm = get_per_char_metric (font, &char2b);
24254 if (pcm->width == 0
24255 && pcm->rbearing == 0 && pcm->lbearing == 0)
24256 pcm = NULL;
24257 }
24258
24259 if (pcm)
24260 {
24261 it->phys_ascent = pcm->ascent + boff;
24262 it->phys_descent = pcm->descent - boff;
24263 it->pixel_width = pcm->width;
24264 }
24265 else
24266 {
24267 it->glyph_not_available_p = 1;
24268 it->phys_ascent = it->ascent;
24269 it->phys_descent = it->descent;
24270 it->pixel_width = font->space_width;
24271 }
24272
24273 if (it->constrain_row_ascent_descent_p)
24274 {
24275 if (it->descent > it->max_descent)
24276 {
24277 it->ascent += it->descent - it->max_descent;
24278 it->descent = it->max_descent;
24279 }
24280 if (it->ascent > it->max_ascent)
24281 {
24282 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24283 it->ascent = it->max_ascent;
24284 }
24285 it->phys_ascent = min (it->phys_ascent, it->ascent);
24286 it->phys_descent = min (it->phys_descent, it->descent);
24287 extra_line_spacing = 0;
24288 }
24289
24290 /* If this is a space inside a region of text with
24291 `space-width' property, change its width. */
24292 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24293 if (stretched_p)
24294 it->pixel_width *= XFLOATINT (it->space_width);
24295
24296 /* If face has a box, add the box thickness to the character
24297 height. If character has a box line to the left and/or
24298 right, add the box line width to the character's width. */
24299 if (face->box != FACE_NO_BOX)
24300 {
24301 int thick = face->box_line_width;
24302
24303 if (thick > 0)
24304 {
24305 it->ascent += thick;
24306 it->descent += thick;
24307 }
24308 else
24309 thick = -thick;
24310
24311 if (it->start_of_box_run_p)
24312 it->pixel_width += thick;
24313 if (it->end_of_box_run_p)
24314 it->pixel_width += thick;
24315 }
24316
24317 /* If face has an overline, add the height of the overline
24318 (1 pixel) and a 1 pixel margin to the character height. */
24319 if (face->overline_p)
24320 it->ascent += overline_margin;
24321
24322 if (it->constrain_row_ascent_descent_p)
24323 {
24324 if (it->ascent > it->max_ascent)
24325 it->ascent = it->max_ascent;
24326 if (it->descent > it->max_descent)
24327 it->descent = it->max_descent;
24328 }
24329
24330 take_vertical_position_into_account (it);
24331
24332 /* If we have to actually produce glyphs, do it. */
24333 if (it->glyph_row)
24334 {
24335 if (stretched_p)
24336 {
24337 /* Translate a space with a `space-width' property
24338 into a stretch glyph. */
24339 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24340 / FONT_HEIGHT (font));
24341 append_stretch_glyph (it, it->object, it->pixel_width,
24342 it->ascent + it->descent, ascent);
24343 }
24344 else
24345 append_glyph (it);
24346
24347 /* If characters with lbearing or rbearing are displayed
24348 in this line, record that fact in a flag of the
24349 glyph row. This is used to optimize X output code. */
24350 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24351 it->glyph_row->contains_overlapping_glyphs_p = 1;
24352 }
24353 if (! stretched_p && it->pixel_width == 0)
24354 /* We assure that all visible glyphs have at least 1-pixel
24355 width. */
24356 it->pixel_width = 1;
24357 }
24358 else if (it->char_to_display == '\n')
24359 {
24360 /* A newline has no width, but we need the height of the
24361 line. But if previous part of the line sets a height,
24362 don't increase that height */
24363
24364 Lisp_Object height;
24365 Lisp_Object total_height = Qnil;
24366
24367 it->override_ascent = -1;
24368 it->pixel_width = 0;
24369 it->nglyphs = 0;
24370
24371 height = get_it_property (it, Qline_height);
24372 /* Split (line-height total-height) list */
24373 if (CONSP (height)
24374 && CONSP (XCDR (height))
24375 && NILP (XCDR (XCDR (height))))
24376 {
24377 total_height = XCAR (XCDR (height));
24378 height = XCAR (height);
24379 }
24380 height = calc_line_height_property (it, height, font, boff, 1);
24381
24382 if (it->override_ascent >= 0)
24383 {
24384 it->ascent = it->override_ascent;
24385 it->descent = it->override_descent;
24386 boff = it->override_boff;
24387 }
24388 else
24389 {
24390 it->ascent = FONT_BASE (font) + boff;
24391 it->descent = FONT_DESCENT (font) - boff;
24392 }
24393
24394 if (EQ (height, Qt))
24395 {
24396 if (it->descent > it->max_descent)
24397 {
24398 it->ascent += it->descent - it->max_descent;
24399 it->descent = it->max_descent;
24400 }
24401 if (it->ascent > it->max_ascent)
24402 {
24403 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24404 it->ascent = it->max_ascent;
24405 }
24406 it->phys_ascent = min (it->phys_ascent, it->ascent);
24407 it->phys_descent = min (it->phys_descent, it->descent);
24408 it->constrain_row_ascent_descent_p = 1;
24409 extra_line_spacing = 0;
24410 }
24411 else
24412 {
24413 Lisp_Object spacing;
24414
24415 it->phys_ascent = it->ascent;
24416 it->phys_descent = it->descent;
24417
24418 if ((it->max_ascent > 0 || it->max_descent > 0)
24419 && face->box != FACE_NO_BOX
24420 && face->box_line_width > 0)
24421 {
24422 it->ascent += face->box_line_width;
24423 it->descent += face->box_line_width;
24424 }
24425 if (!NILP (height)
24426 && XINT (height) > it->ascent + it->descent)
24427 it->ascent = XINT (height) - it->descent;
24428
24429 if (!NILP (total_height))
24430 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24431 else
24432 {
24433 spacing = get_it_property (it, Qline_spacing);
24434 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24435 }
24436 if (INTEGERP (spacing))
24437 {
24438 extra_line_spacing = XINT (spacing);
24439 if (!NILP (total_height))
24440 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24441 }
24442 }
24443 }
24444 else /* i.e. (it->char_to_display == '\t') */
24445 {
24446 if (font->space_width > 0)
24447 {
24448 int tab_width = it->tab_width * font->space_width;
24449 int x = it->current_x + it->continuation_lines_width;
24450 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24451
24452 /* If the distance from the current position to the next tab
24453 stop is less than a space character width, use the
24454 tab stop after that. */
24455 if (next_tab_x - x < font->space_width)
24456 next_tab_x += tab_width;
24457
24458 it->pixel_width = next_tab_x - x;
24459 it->nglyphs = 1;
24460 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24461 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24462
24463 if (it->glyph_row)
24464 {
24465 append_stretch_glyph (it, it->object, it->pixel_width,
24466 it->ascent + it->descent, it->ascent);
24467 }
24468 }
24469 else
24470 {
24471 it->pixel_width = 0;
24472 it->nglyphs = 1;
24473 }
24474 }
24475 }
24476 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24477 {
24478 /* A static composition.
24479
24480 Note: A composition is represented as one glyph in the
24481 glyph matrix. There are no padding glyphs.
24482
24483 Important note: pixel_width, ascent, and descent are the
24484 values of what is drawn by draw_glyphs (i.e. the values of
24485 the overall glyphs composed). */
24486 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24487 int boff; /* baseline offset */
24488 struct composition *cmp = composition_table[it->cmp_it.id];
24489 int glyph_len = cmp->glyph_len;
24490 struct font *font = face->font;
24491
24492 it->nglyphs = 1;
24493
24494 /* If we have not yet calculated pixel size data of glyphs of
24495 the composition for the current face font, calculate them
24496 now. Theoretically, we have to check all fonts for the
24497 glyphs, but that requires much time and memory space. So,
24498 here we check only the font of the first glyph. This may
24499 lead to incorrect display, but it's very rare, and C-l
24500 (recenter-top-bottom) can correct the display anyway. */
24501 if (! cmp->font || cmp->font != font)
24502 {
24503 /* Ascent and descent of the font of the first character
24504 of this composition (adjusted by baseline offset).
24505 Ascent and descent of overall glyphs should not be less
24506 than these, respectively. */
24507 int font_ascent, font_descent, font_height;
24508 /* Bounding box of the overall glyphs. */
24509 int leftmost, rightmost, lowest, highest;
24510 int lbearing, rbearing;
24511 int i, width, ascent, descent;
24512 int left_padded = 0, right_padded = 0;
24513 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24514 XChar2b char2b;
24515 struct font_metrics *pcm;
24516 int font_not_found_p;
24517 EMACS_INT pos;
24518
24519 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24520 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24521 break;
24522 if (glyph_len < cmp->glyph_len)
24523 right_padded = 1;
24524 for (i = 0; i < glyph_len; i++)
24525 {
24526 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24527 break;
24528 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24529 }
24530 if (i > 0)
24531 left_padded = 1;
24532
24533 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24534 : IT_CHARPOS (*it));
24535 /* If no suitable font is found, use the default font. */
24536 font_not_found_p = font == NULL;
24537 if (font_not_found_p)
24538 {
24539 face = face->ascii_face;
24540 font = face->font;
24541 }
24542 boff = font->baseline_offset;
24543 if (font->vertical_centering)
24544 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24545 font_ascent = FONT_BASE (font) + boff;
24546 font_descent = FONT_DESCENT (font) - boff;
24547 font_height = FONT_HEIGHT (font);
24548
24549 cmp->font = (void *) font;
24550
24551 pcm = NULL;
24552 if (! font_not_found_p)
24553 {
24554 get_char_face_and_encoding (it->f, c, it->face_id,
24555 &char2b, 0);
24556 pcm = get_per_char_metric (font, &char2b);
24557 }
24558
24559 /* Initialize the bounding box. */
24560 if (pcm)
24561 {
24562 width = cmp->glyph_len > 0 ? pcm->width : 0;
24563 ascent = pcm->ascent;
24564 descent = pcm->descent;
24565 lbearing = pcm->lbearing;
24566 rbearing = pcm->rbearing;
24567 }
24568 else
24569 {
24570 width = cmp->glyph_len > 0 ? font->space_width : 0;
24571 ascent = FONT_BASE (font);
24572 descent = FONT_DESCENT (font);
24573 lbearing = 0;
24574 rbearing = width;
24575 }
24576
24577 rightmost = width;
24578 leftmost = 0;
24579 lowest = - descent + boff;
24580 highest = ascent + boff;
24581
24582 if (! font_not_found_p
24583 && font->default_ascent
24584 && CHAR_TABLE_P (Vuse_default_ascent)
24585 && !NILP (Faref (Vuse_default_ascent,
24586 make_number (it->char_to_display))))
24587 highest = font->default_ascent + boff;
24588
24589 /* Draw the first glyph at the normal position. It may be
24590 shifted to right later if some other glyphs are drawn
24591 at the left. */
24592 cmp->offsets[i * 2] = 0;
24593 cmp->offsets[i * 2 + 1] = boff;
24594 cmp->lbearing = lbearing;
24595 cmp->rbearing = rbearing;
24596
24597 /* Set cmp->offsets for the remaining glyphs. */
24598 for (i++; i < glyph_len; i++)
24599 {
24600 int left, right, btm, top;
24601 int ch = COMPOSITION_GLYPH (cmp, i);
24602 int face_id;
24603 struct face *this_face;
24604
24605 if (ch == '\t')
24606 ch = ' ';
24607 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24608 this_face = FACE_FROM_ID (it->f, face_id);
24609 font = this_face->font;
24610
24611 if (font == NULL)
24612 pcm = NULL;
24613 else
24614 {
24615 get_char_face_and_encoding (it->f, ch, face_id,
24616 &char2b, 0);
24617 pcm = get_per_char_metric (font, &char2b);
24618 }
24619 if (! pcm)
24620 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24621 else
24622 {
24623 width = pcm->width;
24624 ascent = pcm->ascent;
24625 descent = pcm->descent;
24626 lbearing = pcm->lbearing;
24627 rbearing = pcm->rbearing;
24628 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24629 {
24630 /* Relative composition with or without
24631 alternate chars. */
24632 left = (leftmost + rightmost - width) / 2;
24633 btm = - descent + boff;
24634 if (font->relative_compose
24635 && (! CHAR_TABLE_P (Vignore_relative_composition)
24636 || NILP (Faref (Vignore_relative_composition,
24637 make_number (ch)))))
24638 {
24639
24640 if (- descent >= font->relative_compose)
24641 /* One extra pixel between two glyphs. */
24642 btm = highest + 1;
24643 else if (ascent <= 0)
24644 /* One extra pixel between two glyphs. */
24645 btm = lowest - 1 - ascent - descent;
24646 }
24647 }
24648 else
24649 {
24650 /* A composition rule is specified by an integer
24651 value that encodes global and new reference
24652 points (GREF and NREF). GREF and NREF are
24653 specified by numbers as below:
24654
24655 0---1---2 -- ascent
24656 | |
24657 | |
24658 | |
24659 9--10--11 -- center
24660 | |
24661 ---3---4---5--- baseline
24662 | |
24663 6---7---8 -- descent
24664 */
24665 int rule = COMPOSITION_RULE (cmp, i);
24666 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24667
24668 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24669 grefx = gref % 3, nrefx = nref % 3;
24670 grefy = gref / 3, nrefy = nref / 3;
24671 if (xoff)
24672 xoff = font_height * (xoff - 128) / 256;
24673 if (yoff)
24674 yoff = font_height * (yoff - 128) / 256;
24675
24676 left = (leftmost
24677 + grefx * (rightmost - leftmost) / 2
24678 - nrefx * width / 2
24679 + xoff);
24680
24681 btm = ((grefy == 0 ? highest
24682 : grefy == 1 ? 0
24683 : grefy == 2 ? lowest
24684 : (highest + lowest) / 2)
24685 - (nrefy == 0 ? ascent + descent
24686 : nrefy == 1 ? descent - boff
24687 : nrefy == 2 ? 0
24688 : (ascent + descent) / 2)
24689 + yoff);
24690 }
24691
24692 cmp->offsets[i * 2] = left;
24693 cmp->offsets[i * 2 + 1] = btm + descent;
24694
24695 /* Update the bounding box of the overall glyphs. */
24696 if (width > 0)
24697 {
24698 right = left + width;
24699 if (left < leftmost)
24700 leftmost = left;
24701 if (right > rightmost)
24702 rightmost = right;
24703 }
24704 top = btm + descent + ascent;
24705 if (top > highest)
24706 highest = top;
24707 if (btm < lowest)
24708 lowest = btm;
24709
24710 if (cmp->lbearing > left + lbearing)
24711 cmp->lbearing = left + lbearing;
24712 if (cmp->rbearing < left + rbearing)
24713 cmp->rbearing = left + rbearing;
24714 }
24715 }
24716
24717 /* If there are glyphs whose x-offsets are negative,
24718 shift all glyphs to the right and make all x-offsets
24719 non-negative. */
24720 if (leftmost < 0)
24721 {
24722 for (i = 0; i < cmp->glyph_len; i++)
24723 cmp->offsets[i * 2] -= leftmost;
24724 rightmost -= leftmost;
24725 cmp->lbearing -= leftmost;
24726 cmp->rbearing -= leftmost;
24727 }
24728
24729 if (left_padded && cmp->lbearing < 0)
24730 {
24731 for (i = 0; i < cmp->glyph_len; i++)
24732 cmp->offsets[i * 2] -= cmp->lbearing;
24733 rightmost -= cmp->lbearing;
24734 cmp->rbearing -= cmp->lbearing;
24735 cmp->lbearing = 0;
24736 }
24737 if (right_padded && rightmost < cmp->rbearing)
24738 {
24739 rightmost = cmp->rbearing;
24740 }
24741
24742 cmp->pixel_width = rightmost;
24743 cmp->ascent = highest;
24744 cmp->descent = - lowest;
24745 if (cmp->ascent < font_ascent)
24746 cmp->ascent = font_ascent;
24747 if (cmp->descent < font_descent)
24748 cmp->descent = font_descent;
24749 }
24750
24751 if (it->glyph_row
24752 && (cmp->lbearing < 0
24753 || cmp->rbearing > cmp->pixel_width))
24754 it->glyph_row->contains_overlapping_glyphs_p = 1;
24755
24756 it->pixel_width = cmp->pixel_width;
24757 it->ascent = it->phys_ascent = cmp->ascent;
24758 it->descent = it->phys_descent = cmp->descent;
24759 if (face->box != FACE_NO_BOX)
24760 {
24761 int thick = face->box_line_width;
24762
24763 if (thick > 0)
24764 {
24765 it->ascent += thick;
24766 it->descent += thick;
24767 }
24768 else
24769 thick = - thick;
24770
24771 if (it->start_of_box_run_p)
24772 it->pixel_width += thick;
24773 if (it->end_of_box_run_p)
24774 it->pixel_width += thick;
24775 }
24776
24777 /* If face has an overline, add the height of the overline
24778 (1 pixel) and a 1 pixel margin to the character height. */
24779 if (face->overline_p)
24780 it->ascent += overline_margin;
24781
24782 take_vertical_position_into_account (it);
24783 if (it->ascent < 0)
24784 it->ascent = 0;
24785 if (it->descent < 0)
24786 it->descent = 0;
24787
24788 if (it->glyph_row && cmp->glyph_len > 0)
24789 append_composite_glyph (it);
24790 }
24791 else if (it->what == IT_COMPOSITION)
24792 {
24793 /* A dynamic (automatic) composition. */
24794 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24795 Lisp_Object gstring;
24796 struct font_metrics metrics;
24797
24798 it->nglyphs = 1;
24799
24800 gstring = composition_gstring_from_id (it->cmp_it.id);
24801 it->pixel_width
24802 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24803 &metrics);
24804 if (it->glyph_row
24805 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24806 it->glyph_row->contains_overlapping_glyphs_p = 1;
24807 it->ascent = it->phys_ascent = metrics.ascent;
24808 it->descent = it->phys_descent = metrics.descent;
24809 if (face->box != FACE_NO_BOX)
24810 {
24811 int thick = face->box_line_width;
24812
24813 if (thick > 0)
24814 {
24815 it->ascent += thick;
24816 it->descent += thick;
24817 }
24818 else
24819 thick = - thick;
24820
24821 if (it->start_of_box_run_p)
24822 it->pixel_width += thick;
24823 if (it->end_of_box_run_p)
24824 it->pixel_width += thick;
24825 }
24826 /* If face has an overline, add the height of the overline
24827 (1 pixel) and a 1 pixel margin to the character height. */
24828 if (face->overline_p)
24829 it->ascent += overline_margin;
24830 take_vertical_position_into_account (it);
24831 if (it->ascent < 0)
24832 it->ascent = 0;
24833 if (it->descent < 0)
24834 it->descent = 0;
24835
24836 if (it->glyph_row)
24837 append_composite_glyph (it);
24838 }
24839 else if (it->what == IT_GLYPHLESS)
24840 produce_glyphless_glyph (it, 0, Qnil);
24841 else if (it->what == IT_IMAGE)
24842 produce_image_glyph (it);
24843 else if (it->what == IT_STRETCH)
24844 produce_stretch_glyph (it);
24845
24846 done:
24847 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24848 because this isn't true for images with `:ascent 100'. */
24849 xassert (it->ascent >= 0 && it->descent >= 0);
24850 if (it->area == TEXT_AREA)
24851 it->current_x += it->pixel_width;
24852
24853 if (extra_line_spacing > 0)
24854 {
24855 it->descent += extra_line_spacing;
24856 if (extra_line_spacing > it->max_extra_line_spacing)
24857 it->max_extra_line_spacing = extra_line_spacing;
24858 }
24859
24860 it->max_ascent = max (it->max_ascent, it->ascent);
24861 it->max_descent = max (it->max_descent, it->descent);
24862 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24863 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24864 }
24865
24866 /* EXPORT for RIF:
24867 Output LEN glyphs starting at START at the nominal cursor position.
24868 Advance the nominal cursor over the text. The global variable
24869 updated_window contains the window being updated, updated_row is
24870 the glyph row being updated, and updated_area is the area of that
24871 row being updated. */
24872
24873 void
24874 x_write_glyphs (struct glyph *start, int len)
24875 {
24876 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24877
24878 xassert (updated_window && updated_row);
24879 /* When the window is hscrolled, cursor hpos can legitimately be out
24880 of bounds, but we draw the cursor at the corresponding window
24881 margin in that case. */
24882 if (!updated_row->reversed_p && chpos < 0)
24883 chpos = 0;
24884 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24885 chpos = updated_row->used[TEXT_AREA] - 1;
24886
24887 BLOCK_INPUT;
24888
24889 /* Write glyphs. */
24890
24891 hpos = start - updated_row->glyphs[updated_area];
24892 x = draw_glyphs (updated_window, output_cursor.x,
24893 updated_row, updated_area,
24894 hpos, hpos + len,
24895 DRAW_NORMAL_TEXT, 0);
24896
24897 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24898 if (updated_area == TEXT_AREA
24899 && updated_window->phys_cursor_on_p
24900 && updated_window->phys_cursor.vpos == output_cursor.vpos
24901 && chpos >= hpos
24902 && chpos < hpos + len)
24903 updated_window->phys_cursor_on_p = 0;
24904
24905 UNBLOCK_INPUT;
24906
24907 /* Advance the output cursor. */
24908 output_cursor.hpos += len;
24909 output_cursor.x = x;
24910 }
24911
24912
24913 /* EXPORT for RIF:
24914 Insert LEN glyphs from START at the nominal cursor position. */
24915
24916 void
24917 x_insert_glyphs (struct glyph *start, int len)
24918 {
24919 struct frame *f;
24920 struct window *w;
24921 int line_height, shift_by_width, shifted_region_width;
24922 struct glyph_row *row;
24923 struct glyph *glyph;
24924 int frame_x, frame_y;
24925 EMACS_INT hpos;
24926
24927 xassert (updated_window && updated_row);
24928 BLOCK_INPUT;
24929 w = updated_window;
24930 f = XFRAME (WINDOW_FRAME (w));
24931
24932 /* Get the height of the line we are in. */
24933 row = updated_row;
24934 line_height = row->height;
24935
24936 /* Get the width of the glyphs to insert. */
24937 shift_by_width = 0;
24938 for (glyph = start; glyph < start + len; ++glyph)
24939 shift_by_width += glyph->pixel_width;
24940
24941 /* Get the width of the region to shift right. */
24942 shifted_region_width = (window_box_width (w, updated_area)
24943 - output_cursor.x
24944 - shift_by_width);
24945
24946 /* Shift right. */
24947 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24948 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24949
24950 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24951 line_height, shift_by_width);
24952
24953 /* Write the glyphs. */
24954 hpos = start - row->glyphs[updated_area];
24955 draw_glyphs (w, output_cursor.x, row, updated_area,
24956 hpos, hpos + len,
24957 DRAW_NORMAL_TEXT, 0);
24958
24959 /* Advance the output cursor. */
24960 output_cursor.hpos += len;
24961 output_cursor.x += shift_by_width;
24962 UNBLOCK_INPUT;
24963 }
24964
24965
24966 /* EXPORT for RIF:
24967 Erase the current text line from the nominal cursor position
24968 (inclusive) to pixel column TO_X (exclusive). The idea is that
24969 everything from TO_X onward is already erased.
24970
24971 TO_X is a pixel position relative to updated_area of
24972 updated_window. TO_X == -1 means clear to the end of this area. */
24973
24974 void
24975 x_clear_end_of_line (int to_x)
24976 {
24977 struct frame *f;
24978 struct window *w = updated_window;
24979 int max_x, min_y, max_y;
24980 int from_x, from_y, to_y;
24981
24982 xassert (updated_window && updated_row);
24983 f = XFRAME (w->frame);
24984
24985 if (updated_row->full_width_p)
24986 max_x = WINDOW_TOTAL_WIDTH (w);
24987 else
24988 max_x = window_box_width (w, updated_area);
24989 max_y = window_text_bottom_y (w);
24990
24991 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24992 of window. For TO_X > 0, truncate to end of drawing area. */
24993 if (to_x == 0)
24994 return;
24995 else if (to_x < 0)
24996 to_x = max_x;
24997 else
24998 to_x = min (to_x, max_x);
24999
25000 to_y = min (max_y, output_cursor.y + updated_row->height);
25001
25002 /* Notice if the cursor will be cleared by this operation. */
25003 if (!updated_row->full_width_p)
25004 notice_overwritten_cursor (w, updated_area,
25005 output_cursor.x, -1,
25006 updated_row->y,
25007 MATRIX_ROW_BOTTOM_Y (updated_row));
25008
25009 from_x = output_cursor.x;
25010
25011 /* Translate to frame coordinates. */
25012 if (updated_row->full_width_p)
25013 {
25014 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25015 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25016 }
25017 else
25018 {
25019 int area_left = window_box_left (w, updated_area);
25020 from_x += area_left;
25021 to_x += area_left;
25022 }
25023
25024 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25025 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25026 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25027
25028 /* Prevent inadvertently clearing to end of the X window. */
25029 if (to_x > from_x && to_y > from_y)
25030 {
25031 BLOCK_INPUT;
25032 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25033 to_x - from_x, to_y - from_y);
25034 UNBLOCK_INPUT;
25035 }
25036 }
25037
25038 #endif /* HAVE_WINDOW_SYSTEM */
25039
25040
25041 \f
25042 /***********************************************************************
25043 Cursor types
25044 ***********************************************************************/
25045
25046 /* Value is the internal representation of the specified cursor type
25047 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25048 of the bar cursor. */
25049
25050 static enum text_cursor_kinds
25051 get_specified_cursor_type (Lisp_Object arg, int *width)
25052 {
25053 enum text_cursor_kinds type;
25054
25055 if (NILP (arg))
25056 return NO_CURSOR;
25057
25058 if (EQ (arg, Qbox))
25059 return FILLED_BOX_CURSOR;
25060
25061 if (EQ (arg, Qhollow))
25062 return HOLLOW_BOX_CURSOR;
25063
25064 if (EQ (arg, Qbar))
25065 {
25066 *width = 2;
25067 return BAR_CURSOR;
25068 }
25069
25070 if (CONSP (arg)
25071 && EQ (XCAR (arg), Qbar)
25072 && INTEGERP (XCDR (arg))
25073 && XINT (XCDR (arg)) >= 0)
25074 {
25075 *width = XINT (XCDR (arg));
25076 return BAR_CURSOR;
25077 }
25078
25079 if (EQ (arg, Qhbar))
25080 {
25081 *width = 2;
25082 return HBAR_CURSOR;
25083 }
25084
25085 if (CONSP (arg)
25086 && EQ (XCAR (arg), Qhbar)
25087 && INTEGERP (XCDR (arg))
25088 && XINT (XCDR (arg)) >= 0)
25089 {
25090 *width = XINT (XCDR (arg));
25091 return HBAR_CURSOR;
25092 }
25093
25094 /* Treat anything unknown as "hollow box cursor".
25095 It was bad to signal an error; people have trouble fixing
25096 .Xdefaults with Emacs, when it has something bad in it. */
25097 type = HOLLOW_BOX_CURSOR;
25098
25099 return type;
25100 }
25101
25102 /* Set the default cursor types for specified frame. */
25103 void
25104 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25105 {
25106 int width = 1;
25107 Lisp_Object tem;
25108
25109 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25110 FRAME_CURSOR_WIDTH (f) = width;
25111
25112 /* By default, set up the blink-off state depending on the on-state. */
25113
25114 tem = Fassoc (arg, Vblink_cursor_alist);
25115 if (!NILP (tem))
25116 {
25117 FRAME_BLINK_OFF_CURSOR (f)
25118 = get_specified_cursor_type (XCDR (tem), &width);
25119 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25120 }
25121 else
25122 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25123 }
25124
25125
25126 #ifdef HAVE_WINDOW_SYSTEM
25127
25128 /* Return the cursor we want to be displayed in window W. Return
25129 width of bar/hbar cursor through WIDTH arg. Return with
25130 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25131 (i.e. if the `system caret' should track this cursor).
25132
25133 In a mini-buffer window, we want the cursor only to appear if we
25134 are reading input from this window. For the selected window, we
25135 want the cursor type given by the frame parameter or buffer local
25136 setting of cursor-type. If explicitly marked off, draw no cursor.
25137 In all other cases, we want a hollow box cursor. */
25138
25139 static enum text_cursor_kinds
25140 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25141 int *active_cursor)
25142 {
25143 struct frame *f = XFRAME (w->frame);
25144 struct buffer *b = XBUFFER (w->buffer);
25145 int cursor_type = DEFAULT_CURSOR;
25146 Lisp_Object alt_cursor;
25147 int non_selected = 0;
25148
25149 *active_cursor = 1;
25150
25151 /* Echo area */
25152 if (cursor_in_echo_area
25153 && FRAME_HAS_MINIBUF_P (f)
25154 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25155 {
25156 if (w == XWINDOW (echo_area_window))
25157 {
25158 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25159 {
25160 *width = FRAME_CURSOR_WIDTH (f);
25161 return FRAME_DESIRED_CURSOR (f);
25162 }
25163 else
25164 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25165 }
25166
25167 *active_cursor = 0;
25168 non_selected = 1;
25169 }
25170
25171 /* Detect a nonselected window or nonselected frame. */
25172 else if (w != XWINDOW (f->selected_window)
25173 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25174 {
25175 *active_cursor = 0;
25176
25177 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25178 return NO_CURSOR;
25179
25180 non_selected = 1;
25181 }
25182
25183 /* Never display a cursor in a window in which cursor-type is nil. */
25184 if (NILP (BVAR (b, cursor_type)))
25185 return NO_CURSOR;
25186
25187 /* Get the normal cursor type for this window. */
25188 if (EQ (BVAR (b, cursor_type), Qt))
25189 {
25190 cursor_type = FRAME_DESIRED_CURSOR (f);
25191 *width = FRAME_CURSOR_WIDTH (f);
25192 }
25193 else
25194 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25195
25196 /* Use cursor-in-non-selected-windows instead
25197 for non-selected window or frame. */
25198 if (non_selected)
25199 {
25200 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25201 if (!EQ (Qt, alt_cursor))
25202 return get_specified_cursor_type (alt_cursor, width);
25203 /* t means modify the normal cursor type. */
25204 if (cursor_type == FILLED_BOX_CURSOR)
25205 cursor_type = HOLLOW_BOX_CURSOR;
25206 else if (cursor_type == BAR_CURSOR && *width > 1)
25207 --*width;
25208 return cursor_type;
25209 }
25210
25211 /* Use normal cursor if not blinked off. */
25212 if (!w->cursor_off_p)
25213 {
25214 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25215 {
25216 if (cursor_type == FILLED_BOX_CURSOR)
25217 {
25218 /* Using a block cursor on large images can be very annoying.
25219 So use a hollow cursor for "large" images.
25220 If image is not transparent (no mask), also use hollow cursor. */
25221 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25222 if (img != NULL && IMAGEP (img->spec))
25223 {
25224 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25225 where N = size of default frame font size.
25226 This should cover most of the "tiny" icons people may use. */
25227 if (!img->mask
25228 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25229 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25230 cursor_type = HOLLOW_BOX_CURSOR;
25231 }
25232 }
25233 else if (cursor_type != NO_CURSOR)
25234 {
25235 /* Display current only supports BOX and HOLLOW cursors for images.
25236 So for now, unconditionally use a HOLLOW cursor when cursor is
25237 not a solid box cursor. */
25238 cursor_type = HOLLOW_BOX_CURSOR;
25239 }
25240 }
25241 return cursor_type;
25242 }
25243
25244 /* Cursor is blinked off, so determine how to "toggle" it. */
25245
25246 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25247 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25248 return get_specified_cursor_type (XCDR (alt_cursor), width);
25249
25250 /* Then see if frame has specified a specific blink off cursor type. */
25251 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25252 {
25253 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25254 return FRAME_BLINK_OFF_CURSOR (f);
25255 }
25256
25257 #if 0
25258 /* Some people liked having a permanently visible blinking cursor,
25259 while others had very strong opinions against it. So it was
25260 decided to remove it. KFS 2003-09-03 */
25261
25262 /* Finally perform built-in cursor blinking:
25263 filled box <-> hollow box
25264 wide [h]bar <-> narrow [h]bar
25265 narrow [h]bar <-> no cursor
25266 other type <-> no cursor */
25267
25268 if (cursor_type == FILLED_BOX_CURSOR)
25269 return HOLLOW_BOX_CURSOR;
25270
25271 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25272 {
25273 *width = 1;
25274 return cursor_type;
25275 }
25276 #endif
25277
25278 return NO_CURSOR;
25279 }
25280
25281
25282 /* Notice when the text cursor of window W has been completely
25283 overwritten by a drawing operation that outputs glyphs in AREA
25284 starting at X0 and ending at X1 in the line starting at Y0 and
25285 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25286 the rest of the line after X0 has been written. Y coordinates
25287 are window-relative. */
25288
25289 static void
25290 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25291 int x0, int x1, int y0, int y1)
25292 {
25293 int cx0, cx1, cy0, cy1;
25294 struct glyph_row *row;
25295
25296 if (!w->phys_cursor_on_p)
25297 return;
25298 if (area != TEXT_AREA)
25299 return;
25300
25301 if (w->phys_cursor.vpos < 0
25302 || w->phys_cursor.vpos >= w->current_matrix->nrows
25303 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25304 !(row->enabled_p && row->displays_text_p)))
25305 return;
25306
25307 if (row->cursor_in_fringe_p)
25308 {
25309 row->cursor_in_fringe_p = 0;
25310 draw_fringe_bitmap (w, row, row->reversed_p);
25311 w->phys_cursor_on_p = 0;
25312 return;
25313 }
25314
25315 cx0 = w->phys_cursor.x;
25316 cx1 = cx0 + w->phys_cursor_width;
25317 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25318 return;
25319
25320 /* The cursor image will be completely removed from the
25321 screen if the output area intersects the cursor area in
25322 y-direction. When we draw in [y0 y1[, and some part of
25323 the cursor is at y < y0, that part must have been drawn
25324 before. When scrolling, the cursor is erased before
25325 actually scrolling, so we don't come here. When not
25326 scrolling, the rows above the old cursor row must have
25327 changed, and in this case these rows must have written
25328 over the cursor image.
25329
25330 Likewise if part of the cursor is below y1, with the
25331 exception of the cursor being in the first blank row at
25332 the buffer and window end because update_text_area
25333 doesn't draw that row. (Except when it does, but
25334 that's handled in update_text_area.) */
25335
25336 cy0 = w->phys_cursor.y;
25337 cy1 = cy0 + w->phys_cursor_height;
25338 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25339 return;
25340
25341 w->phys_cursor_on_p = 0;
25342 }
25343
25344 #endif /* HAVE_WINDOW_SYSTEM */
25345
25346 \f
25347 /************************************************************************
25348 Mouse Face
25349 ************************************************************************/
25350
25351 #ifdef HAVE_WINDOW_SYSTEM
25352
25353 /* EXPORT for RIF:
25354 Fix the display of area AREA of overlapping row ROW in window W
25355 with respect to the overlapping part OVERLAPS. */
25356
25357 void
25358 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25359 enum glyph_row_area area, int overlaps)
25360 {
25361 int i, x;
25362
25363 BLOCK_INPUT;
25364
25365 x = 0;
25366 for (i = 0; i < row->used[area];)
25367 {
25368 if (row->glyphs[area][i].overlaps_vertically_p)
25369 {
25370 int start = i, start_x = x;
25371
25372 do
25373 {
25374 x += row->glyphs[area][i].pixel_width;
25375 ++i;
25376 }
25377 while (i < row->used[area]
25378 && row->glyphs[area][i].overlaps_vertically_p);
25379
25380 draw_glyphs (w, start_x, row, area,
25381 start, i,
25382 DRAW_NORMAL_TEXT, overlaps);
25383 }
25384 else
25385 {
25386 x += row->glyphs[area][i].pixel_width;
25387 ++i;
25388 }
25389 }
25390
25391 UNBLOCK_INPUT;
25392 }
25393
25394
25395 /* EXPORT:
25396 Draw the cursor glyph of window W in glyph row ROW. See the
25397 comment of draw_glyphs for the meaning of HL. */
25398
25399 void
25400 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25401 enum draw_glyphs_face hl)
25402 {
25403 /* If cursor hpos is out of bounds, don't draw garbage. This can
25404 happen in mini-buffer windows when switching between echo area
25405 glyphs and mini-buffer. */
25406 if ((row->reversed_p
25407 ? (w->phys_cursor.hpos >= 0)
25408 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25409 {
25410 int on_p = w->phys_cursor_on_p;
25411 int x1;
25412 int hpos = w->phys_cursor.hpos;
25413
25414 /* When the window is hscrolled, cursor hpos can legitimately be
25415 out of bounds, but we draw the cursor at the corresponding
25416 window margin in that case. */
25417 if (!row->reversed_p && hpos < 0)
25418 hpos = 0;
25419 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25420 hpos = row->used[TEXT_AREA] - 1;
25421
25422 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25423 hl, 0);
25424 w->phys_cursor_on_p = on_p;
25425
25426 if (hl == DRAW_CURSOR)
25427 w->phys_cursor_width = x1 - w->phys_cursor.x;
25428 /* When we erase the cursor, and ROW is overlapped by other
25429 rows, make sure that these overlapping parts of other rows
25430 are redrawn. */
25431 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25432 {
25433 w->phys_cursor_width = x1 - w->phys_cursor.x;
25434
25435 if (row > w->current_matrix->rows
25436 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25437 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25438 OVERLAPS_ERASED_CURSOR);
25439
25440 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25441 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25442 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25443 OVERLAPS_ERASED_CURSOR);
25444 }
25445 }
25446 }
25447
25448
25449 /* EXPORT:
25450 Erase the image of a cursor of window W from the screen. */
25451
25452 void
25453 erase_phys_cursor (struct window *w)
25454 {
25455 struct frame *f = XFRAME (w->frame);
25456 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25457 int hpos = w->phys_cursor.hpos;
25458 int vpos = w->phys_cursor.vpos;
25459 int mouse_face_here_p = 0;
25460 struct glyph_matrix *active_glyphs = w->current_matrix;
25461 struct glyph_row *cursor_row;
25462 struct glyph *cursor_glyph;
25463 enum draw_glyphs_face hl;
25464
25465 /* No cursor displayed or row invalidated => nothing to do on the
25466 screen. */
25467 if (w->phys_cursor_type == NO_CURSOR)
25468 goto mark_cursor_off;
25469
25470 /* VPOS >= active_glyphs->nrows means that window has been resized.
25471 Don't bother to erase the cursor. */
25472 if (vpos >= active_glyphs->nrows)
25473 goto mark_cursor_off;
25474
25475 /* If row containing cursor is marked invalid, there is nothing we
25476 can do. */
25477 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25478 if (!cursor_row->enabled_p)
25479 goto mark_cursor_off;
25480
25481 /* If line spacing is > 0, old cursor may only be partially visible in
25482 window after split-window. So adjust visible height. */
25483 cursor_row->visible_height = min (cursor_row->visible_height,
25484 window_text_bottom_y (w) - cursor_row->y);
25485
25486 /* If row is completely invisible, don't attempt to delete a cursor which
25487 isn't there. This can happen if cursor is at top of a window, and
25488 we switch to a buffer with a header line in that window. */
25489 if (cursor_row->visible_height <= 0)
25490 goto mark_cursor_off;
25491
25492 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25493 if (cursor_row->cursor_in_fringe_p)
25494 {
25495 cursor_row->cursor_in_fringe_p = 0;
25496 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25497 goto mark_cursor_off;
25498 }
25499
25500 /* This can happen when the new row is shorter than the old one.
25501 In this case, either draw_glyphs or clear_end_of_line
25502 should have cleared the cursor. Note that we wouldn't be
25503 able to erase the cursor in this case because we don't have a
25504 cursor glyph at hand. */
25505 if ((cursor_row->reversed_p
25506 ? (w->phys_cursor.hpos < 0)
25507 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25508 goto mark_cursor_off;
25509
25510 /* When the window is hscrolled, cursor hpos can legitimately be out
25511 of bounds, but we draw the cursor at the corresponding window
25512 margin in that case. */
25513 if (!cursor_row->reversed_p && hpos < 0)
25514 hpos = 0;
25515 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25516 hpos = cursor_row->used[TEXT_AREA] - 1;
25517
25518 /* If the cursor is in the mouse face area, redisplay that when
25519 we clear the cursor. */
25520 if (! NILP (hlinfo->mouse_face_window)
25521 && coords_in_mouse_face_p (w, hpos, vpos)
25522 /* Don't redraw the cursor's spot in mouse face if it is at the
25523 end of a line (on a newline). The cursor appears there, but
25524 mouse highlighting does not. */
25525 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25526 mouse_face_here_p = 1;
25527
25528 /* Maybe clear the display under the cursor. */
25529 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25530 {
25531 int x, y, left_x;
25532 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25533 int width;
25534
25535 cursor_glyph = get_phys_cursor_glyph (w);
25536 if (cursor_glyph == NULL)
25537 goto mark_cursor_off;
25538
25539 width = cursor_glyph->pixel_width;
25540 left_x = window_box_left_offset (w, TEXT_AREA);
25541 x = w->phys_cursor.x;
25542 if (x < left_x)
25543 width -= left_x - x;
25544 width = min (width, window_box_width (w, TEXT_AREA) - x);
25545 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25546 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25547
25548 if (width > 0)
25549 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25550 }
25551
25552 /* Erase the cursor by redrawing the character underneath it. */
25553 if (mouse_face_here_p)
25554 hl = DRAW_MOUSE_FACE;
25555 else
25556 hl = DRAW_NORMAL_TEXT;
25557 draw_phys_cursor_glyph (w, cursor_row, hl);
25558
25559 mark_cursor_off:
25560 w->phys_cursor_on_p = 0;
25561 w->phys_cursor_type = NO_CURSOR;
25562 }
25563
25564
25565 /* EXPORT:
25566 Display or clear cursor of window W. If ON is zero, clear the
25567 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25568 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25569
25570 void
25571 display_and_set_cursor (struct window *w, int on,
25572 int hpos, int vpos, int x, int y)
25573 {
25574 struct frame *f = XFRAME (w->frame);
25575 int new_cursor_type;
25576 int new_cursor_width;
25577 int active_cursor;
25578 struct glyph_row *glyph_row;
25579 struct glyph *glyph;
25580
25581 /* This is pointless on invisible frames, and dangerous on garbaged
25582 windows and frames; in the latter case, the frame or window may
25583 be in the midst of changing its size, and x and y may be off the
25584 window. */
25585 if (! FRAME_VISIBLE_P (f)
25586 || FRAME_GARBAGED_P (f)
25587 || vpos >= w->current_matrix->nrows
25588 || hpos >= w->current_matrix->matrix_w)
25589 return;
25590
25591 /* If cursor is off and we want it off, return quickly. */
25592 if (!on && !w->phys_cursor_on_p)
25593 return;
25594
25595 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25596 /* If cursor row is not enabled, we don't really know where to
25597 display the cursor. */
25598 if (!glyph_row->enabled_p)
25599 {
25600 w->phys_cursor_on_p = 0;
25601 return;
25602 }
25603
25604 glyph = NULL;
25605 if (!glyph_row->exact_window_width_line_p
25606 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25607 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25608
25609 xassert (interrupt_input_blocked);
25610
25611 /* Set new_cursor_type to the cursor we want to be displayed. */
25612 new_cursor_type = get_window_cursor_type (w, glyph,
25613 &new_cursor_width, &active_cursor);
25614
25615 /* If cursor is currently being shown and we don't want it to be or
25616 it is in the wrong place, or the cursor type is not what we want,
25617 erase it. */
25618 if (w->phys_cursor_on_p
25619 && (!on
25620 || w->phys_cursor.x != x
25621 || w->phys_cursor.y != y
25622 || new_cursor_type != w->phys_cursor_type
25623 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25624 && new_cursor_width != w->phys_cursor_width)))
25625 erase_phys_cursor (w);
25626
25627 /* Don't check phys_cursor_on_p here because that flag is only set
25628 to zero in some cases where we know that the cursor has been
25629 completely erased, to avoid the extra work of erasing the cursor
25630 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25631 still not be visible, or it has only been partly erased. */
25632 if (on)
25633 {
25634 w->phys_cursor_ascent = glyph_row->ascent;
25635 w->phys_cursor_height = glyph_row->height;
25636
25637 /* Set phys_cursor_.* before x_draw_.* is called because some
25638 of them may need the information. */
25639 w->phys_cursor.x = x;
25640 w->phys_cursor.y = glyph_row->y;
25641 w->phys_cursor.hpos = hpos;
25642 w->phys_cursor.vpos = vpos;
25643 }
25644
25645 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25646 new_cursor_type, new_cursor_width,
25647 on, active_cursor);
25648 }
25649
25650
25651 /* Switch the display of W's cursor on or off, according to the value
25652 of ON. */
25653
25654 static void
25655 update_window_cursor (struct window *w, int on)
25656 {
25657 /* Don't update cursor in windows whose frame is in the process
25658 of being deleted. */
25659 if (w->current_matrix)
25660 {
25661 int hpos = w->phys_cursor.hpos;
25662 int vpos = w->phys_cursor.vpos;
25663 struct glyph_row *row;
25664
25665 if (vpos >= w->current_matrix->nrows
25666 || hpos >= w->current_matrix->matrix_w)
25667 return;
25668
25669 row = MATRIX_ROW (w->current_matrix, vpos);
25670
25671 /* When the window is hscrolled, cursor hpos can legitimately be
25672 out of bounds, but we draw the cursor at the corresponding
25673 window margin in that case. */
25674 if (!row->reversed_p && hpos < 0)
25675 hpos = 0;
25676 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25677 hpos = row->used[TEXT_AREA] - 1;
25678
25679 BLOCK_INPUT;
25680 display_and_set_cursor (w, on, hpos, vpos,
25681 w->phys_cursor.x, w->phys_cursor.y);
25682 UNBLOCK_INPUT;
25683 }
25684 }
25685
25686
25687 /* Call update_window_cursor with parameter ON_P on all leaf windows
25688 in the window tree rooted at W. */
25689
25690 static void
25691 update_cursor_in_window_tree (struct window *w, int on_p)
25692 {
25693 while (w)
25694 {
25695 if (!NILP (w->hchild))
25696 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25697 else if (!NILP (w->vchild))
25698 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25699 else
25700 update_window_cursor (w, on_p);
25701
25702 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25703 }
25704 }
25705
25706
25707 /* EXPORT:
25708 Display the cursor on window W, or clear it, according to ON_P.
25709 Don't change the cursor's position. */
25710
25711 void
25712 x_update_cursor (struct frame *f, int on_p)
25713 {
25714 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25715 }
25716
25717
25718 /* EXPORT:
25719 Clear the cursor of window W to background color, and mark the
25720 cursor as not shown. This is used when the text where the cursor
25721 is about to be rewritten. */
25722
25723 void
25724 x_clear_cursor (struct window *w)
25725 {
25726 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25727 update_window_cursor (w, 0);
25728 }
25729
25730 #endif /* HAVE_WINDOW_SYSTEM */
25731
25732 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25733 and MSDOS. */
25734 static void
25735 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25736 int start_hpos, int end_hpos,
25737 enum draw_glyphs_face draw)
25738 {
25739 #ifdef HAVE_WINDOW_SYSTEM
25740 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25741 {
25742 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25743 return;
25744 }
25745 #endif
25746 #if defined (HAVE_GPM) || defined (MSDOS)
25747 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25748 #endif
25749 }
25750
25751 /* Display the active region described by mouse_face_* according to DRAW. */
25752
25753 static void
25754 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25755 {
25756 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25757 struct frame *f = XFRAME (WINDOW_FRAME (w));
25758
25759 if (/* If window is in the process of being destroyed, don't bother
25760 to do anything. */
25761 w->current_matrix != NULL
25762 /* Don't update mouse highlight if hidden */
25763 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25764 /* Recognize when we are called to operate on rows that don't exist
25765 anymore. This can happen when a window is split. */
25766 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25767 {
25768 int phys_cursor_on_p = w->phys_cursor_on_p;
25769 struct glyph_row *row, *first, *last;
25770
25771 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25772 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25773
25774 for (row = first; row <= last && row->enabled_p; ++row)
25775 {
25776 int start_hpos, end_hpos, start_x;
25777
25778 /* For all but the first row, the highlight starts at column 0. */
25779 if (row == first)
25780 {
25781 /* R2L rows have BEG and END in reversed order, but the
25782 screen drawing geometry is always left to right. So
25783 we need to mirror the beginning and end of the
25784 highlighted area in R2L rows. */
25785 if (!row->reversed_p)
25786 {
25787 start_hpos = hlinfo->mouse_face_beg_col;
25788 start_x = hlinfo->mouse_face_beg_x;
25789 }
25790 else if (row == last)
25791 {
25792 start_hpos = hlinfo->mouse_face_end_col;
25793 start_x = hlinfo->mouse_face_end_x;
25794 }
25795 else
25796 {
25797 start_hpos = 0;
25798 start_x = 0;
25799 }
25800 }
25801 else if (row->reversed_p && row == last)
25802 {
25803 start_hpos = hlinfo->mouse_face_end_col;
25804 start_x = hlinfo->mouse_face_end_x;
25805 }
25806 else
25807 {
25808 start_hpos = 0;
25809 start_x = 0;
25810 }
25811
25812 if (row == last)
25813 {
25814 if (!row->reversed_p)
25815 end_hpos = hlinfo->mouse_face_end_col;
25816 else if (row == first)
25817 end_hpos = hlinfo->mouse_face_beg_col;
25818 else
25819 {
25820 end_hpos = row->used[TEXT_AREA];
25821 if (draw == DRAW_NORMAL_TEXT)
25822 row->fill_line_p = 1; /* Clear to end of line */
25823 }
25824 }
25825 else if (row->reversed_p && row == first)
25826 end_hpos = hlinfo->mouse_face_beg_col;
25827 else
25828 {
25829 end_hpos = row->used[TEXT_AREA];
25830 if (draw == DRAW_NORMAL_TEXT)
25831 row->fill_line_p = 1; /* Clear to end of line */
25832 }
25833
25834 if (end_hpos > start_hpos)
25835 {
25836 draw_row_with_mouse_face (w, start_x, row,
25837 start_hpos, end_hpos, draw);
25838
25839 row->mouse_face_p
25840 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25841 }
25842 }
25843
25844 #ifdef HAVE_WINDOW_SYSTEM
25845 /* When we've written over the cursor, arrange for it to
25846 be displayed again. */
25847 if (FRAME_WINDOW_P (f)
25848 && phys_cursor_on_p && !w->phys_cursor_on_p)
25849 {
25850 int hpos = w->phys_cursor.hpos;
25851
25852 /* When the window is hscrolled, cursor hpos can legitimately be
25853 out of bounds, but we draw the cursor at the corresponding
25854 window margin in that case. */
25855 if (!row->reversed_p && hpos < 0)
25856 hpos = 0;
25857 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25858 hpos = row->used[TEXT_AREA] - 1;
25859
25860 BLOCK_INPUT;
25861 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25862 w->phys_cursor.x, w->phys_cursor.y);
25863 UNBLOCK_INPUT;
25864 }
25865 #endif /* HAVE_WINDOW_SYSTEM */
25866 }
25867
25868 #ifdef HAVE_WINDOW_SYSTEM
25869 /* Change the mouse cursor. */
25870 if (FRAME_WINDOW_P (f))
25871 {
25872 if (draw == DRAW_NORMAL_TEXT
25873 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25874 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25875 else if (draw == DRAW_MOUSE_FACE)
25876 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25877 else
25878 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25879 }
25880 #endif /* HAVE_WINDOW_SYSTEM */
25881 }
25882
25883 /* EXPORT:
25884 Clear out the mouse-highlighted active region.
25885 Redraw it un-highlighted first. Value is non-zero if mouse
25886 face was actually drawn unhighlighted. */
25887
25888 int
25889 clear_mouse_face (Mouse_HLInfo *hlinfo)
25890 {
25891 int cleared = 0;
25892
25893 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25894 {
25895 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25896 cleared = 1;
25897 }
25898
25899 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25900 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25901 hlinfo->mouse_face_window = Qnil;
25902 hlinfo->mouse_face_overlay = Qnil;
25903 return cleared;
25904 }
25905
25906 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25907 within the mouse face on that window. */
25908 static int
25909 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25910 {
25911 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25912
25913 /* Quickly resolve the easy cases. */
25914 if (!(WINDOWP (hlinfo->mouse_face_window)
25915 && XWINDOW (hlinfo->mouse_face_window) == w))
25916 return 0;
25917 if (vpos < hlinfo->mouse_face_beg_row
25918 || vpos > hlinfo->mouse_face_end_row)
25919 return 0;
25920 if (vpos > hlinfo->mouse_face_beg_row
25921 && vpos < hlinfo->mouse_face_end_row)
25922 return 1;
25923
25924 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25925 {
25926 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25927 {
25928 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25929 return 1;
25930 }
25931 else if ((vpos == hlinfo->mouse_face_beg_row
25932 && hpos >= hlinfo->mouse_face_beg_col)
25933 || (vpos == hlinfo->mouse_face_end_row
25934 && hpos < hlinfo->mouse_face_end_col))
25935 return 1;
25936 }
25937 else
25938 {
25939 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25940 {
25941 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25942 return 1;
25943 }
25944 else if ((vpos == hlinfo->mouse_face_beg_row
25945 && hpos <= hlinfo->mouse_face_beg_col)
25946 || (vpos == hlinfo->mouse_face_end_row
25947 && hpos > hlinfo->mouse_face_end_col))
25948 return 1;
25949 }
25950 return 0;
25951 }
25952
25953
25954 /* EXPORT:
25955 Non-zero if physical cursor of window W is within mouse face. */
25956
25957 int
25958 cursor_in_mouse_face_p (struct window *w)
25959 {
25960 int hpos = w->phys_cursor.hpos;
25961 int vpos = w->phys_cursor.vpos;
25962 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25963
25964 /* When the window is hscrolled, cursor hpos can legitimately be out
25965 of bounds, but we draw the cursor at the corresponding window
25966 margin in that case. */
25967 if (!row->reversed_p && hpos < 0)
25968 hpos = 0;
25969 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25970 hpos = row->used[TEXT_AREA] - 1;
25971
25972 return coords_in_mouse_face_p (w, hpos, vpos);
25973 }
25974
25975
25976 \f
25977 /* Find the glyph rows START_ROW and END_ROW of window W that display
25978 characters between buffer positions START_CHARPOS and END_CHARPOS
25979 (excluding END_CHARPOS). DISP_STRING is a display string that
25980 covers these buffer positions. This is similar to
25981 row_containing_pos, but is more accurate when bidi reordering makes
25982 buffer positions change non-linearly with glyph rows. */
25983 static void
25984 rows_from_pos_range (struct window *w,
25985 EMACS_INT start_charpos, EMACS_INT end_charpos,
25986 Lisp_Object disp_string,
25987 struct glyph_row **start, struct glyph_row **end)
25988 {
25989 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25990 int last_y = window_text_bottom_y (w);
25991 struct glyph_row *row;
25992
25993 *start = NULL;
25994 *end = NULL;
25995
25996 while (!first->enabled_p
25997 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25998 first++;
25999
26000 /* Find the START row. */
26001 for (row = first;
26002 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26003 row++)
26004 {
26005 /* A row can potentially be the START row if the range of the
26006 characters it displays intersects the range
26007 [START_CHARPOS..END_CHARPOS). */
26008 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26009 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26010 /* See the commentary in row_containing_pos, for the
26011 explanation of the complicated way to check whether
26012 some position is beyond the end of the characters
26013 displayed by a row. */
26014 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26015 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26016 && !row->ends_at_zv_p
26017 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26018 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26019 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26020 && !row->ends_at_zv_p
26021 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26022 {
26023 /* Found a candidate row. Now make sure at least one of the
26024 glyphs it displays has a charpos from the range
26025 [START_CHARPOS..END_CHARPOS).
26026
26027 This is not obvious because bidi reordering could make
26028 buffer positions of a row be 1,2,3,102,101,100, and if we
26029 want to highlight characters in [50..60), we don't want
26030 this row, even though [50..60) does intersect [1..103),
26031 the range of character positions given by the row's start
26032 and end positions. */
26033 struct glyph *g = row->glyphs[TEXT_AREA];
26034 struct glyph *e = g + row->used[TEXT_AREA];
26035
26036 while (g < e)
26037 {
26038 if (((BUFFERP (g->object) || INTEGERP (g->object))
26039 && start_charpos <= g->charpos && g->charpos < end_charpos)
26040 /* A glyph that comes from DISP_STRING is by
26041 definition to be highlighted. */
26042 || EQ (g->object, disp_string))
26043 *start = row;
26044 g++;
26045 }
26046 if (*start)
26047 break;
26048 }
26049 }
26050
26051 /* Find the END row. */
26052 if (!*start
26053 /* If the last row is partially visible, start looking for END
26054 from that row, instead of starting from FIRST. */
26055 && !(row->enabled_p
26056 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26057 row = first;
26058 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26059 {
26060 struct glyph_row *next = row + 1;
26061 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
26062
26063 if (!next->enabled_p
26064 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26065 /* The first row >= START whose range of displayed characters
26066 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26067 is the row END + 1. */
26068 || (start_charpos < next_start
26069 && end_charpos < next_start)
26070 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26071 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26072 && !next->ends_at_zv_p
26073 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26074 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26075 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26076 && !next->ends_at_zv_p
26077 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26078 {
26079 *end = row;
26080 break;
26081 }
26082 else
26083 {
26084 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26085 but none of the characters it displays are in the range, it is
26086 also END + 1. */
26087 struct glyph *g = next->glyphs[TEXT_AREA];
26088 struct glyph *s = g;
26089 struct glyph *e = g + next->used[TEXT_AREA];
26090
26091 while (g < e)
26092 {
26093 if (((BUFFERP (g->object) || INTEGERP (g->object))
26094 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26095 /* If the buffer position of the first glyph in
26096 the row is equal to END_CHARPOS, it means
26097 the last character to be highlighted is the
26098 newline of ROW, and we must consider NEXT as
26099 END, not END+1. */
26100 || (((!next->reversed_p && g == s)
26101 || (next->reversed_p && g == e - 1))
26102 && (g->charpos == end_charpos
26103 /* Special case for when NEXT is an
26104 empty line at ZV. */
26105 || (g->charpos == -1
26106 && !row->ends_at_zv_p
26107 && next_start == end_charpos)))))
26108 /* A glyph that comes from DISP_STRING is by
26109 definition to be highlighted. */
26110 || EQ (g->object, disp_string))
26111 break;
26112 g++;
26113 }
26114 if (g == e)
26115 {
26116 *end = row;
26117 break;
26118 }
26119 /* The first row that ends at ZV must be the last to be
26120 highlighted. */
26121 else if (next->ends_at_zv_p)
26122 {
26123 *end = next;
26124 break;
26125 }
26126 }
26127 }
26128 }
26129
26130 /* This function sets the mouse_face_* elements of HLINFO, assuming
26131 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26132 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26133 for the overlay or run of text properties specifying the mouse
26134 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26135 before-string and after-string that must also be highlighted.
26136 DISP_STRING, if non-nil, is a display string that may cover some
26137 or all of the highlighted text. */
26138
26139 static void
26140 mouse_face_from_buffer_pos (Lisp_Object window,
26141 Mouse_HLInfo *hlinfo,
26142 EMACS_INT mouse_charpos,
26143 EMACS_INT start_charpos,
26144 EMACS_INT end_charpos,
26145 Lisp_Object before_string,
26146 Lisp_Object after_string,
26147 Lisp_Object disp_string)
26148 {
26149 struct window *w = XWINDOW (window);
26150 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26151 struct glyph_row *r1, *r2;
26152 struct glyph *glyph, *end;
26153 EMACS_INT ignore, pos;
26154 int x;
26155
26156 xassert (NILP (disp_string) || STRINGP (disp_string));
26157 xassert (NILP (before_string) || STRINGP (before_string));
26158 xassert (NILP (after_string) || STRINGP (after_string));
26159
26160 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26161 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26162 if (r1 == NULL)
26163 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26164 /* If the before-string or display-string contains newlines,
26165 rows_from_pos_range skips to its last row. Move back. */
26166 if (!NILP (before_string) || !NILP (disp_string))
26167 {
26168 struct glyph_row *prev;
26169 while ((prev = r1 - 1, prev >= first)
26170 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26171 && prev->used[TEXT_AREA] > 0)
26172 {
26173 struct glyph *beg = prev->glyphs[TEXT_AREA];
26174 glyph = beg + prev->used[TEXT_AREA];
26175 while (--glyph >= beg && INTEGERP (glyph->object));
26176 if (glyph < beg
26177 || !(EQ (glyph->object, before_string)
26178 || EQ (glyph->object, disp_string)))
26179 break;
26180 r1 = prev;
26181 }
26182 }
26183 if (r2 == NULL)
26184 {
26185 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26186 hlinfo->mouse_face_past_end = 1;
26187 }
26188 else if (!NILP (after_string))
26189 {
26190 /* If the after-string has newlines, advance to its last row. */
26191 struct glyph_row *next;
26192 struct glyph_row *last
26193 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26194
26195 for (next = r2 + 1;
26196 next <= last
26197 && next->used[TEXT_AREA] > 0
26198 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26199 ++next)
26200 r2 = next;
26201 }
26202 /* The rest of the display engine assumes that mouse_face_beg_row is
26203 either above mouse_face_end_row or identical to it. But with
26204 bidi-reordered continued lines, the row for START_CHARPOS could
26205 be below the row for END_CHARPOS. If so, swap the rows and store
26206 them in correct order. */
26207 if (r1->y > r2->y)
26208 {
26209 struct glyph_row *tem = r2;
26210
26211 r2 = r1;
26212 r1 = tem;
26213 }
26214
26215 hlinfo->mouse_face_beg_y = r1->y;
26216 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26217 hlinfo->mouse_face_end_y = r2->y;
26218 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26219
26220 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26221 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26222 could be anywhere in the row and in any order. The strategy
26223 below is to find the leftmost and the rightmost glyph that
26224 belongs to either of these 3 strings, or whose position is
26225 between START_CHARPOS and END_CHARPOS, and highlight all the
26226 glyphs between those two. This may cover more than just the text
26227 between START_CHARPOS and END_CHARPOS if the range of characters
26228 strides the bidi level boundary, e.g. if the beginning is in R2L
26229 text while the end is in L2R text or vice versa. */
26230 if (!r1->reversed_p)
26231 {
26232 /* This row is in a left to right paragraph. Scan it left to
26233 right. */
26234 glyph = r1->glyphs[TEXT_AREA];
26235 end = glyph + r1->used[TEXT_AREA];
26236 x = r1->x;
26237
26238 /* Skip truncation glyphs at the start of the glyph row. */
26239 if (r1->displays_text_p)
26240 for (; glyph < end
26241 && INTEGERP (glyph->object)
26242 && glyph->charpos < 0;
26243 ++glyph)
26244 x += glyph->pixel_width;
26245
26246 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26247 or DISP_STRING, and the first glyph from buffer whose
26248 position is between START_CHARPOS and END_CHARPOS. */
26249 for (; glyph < end
26250 && !INTEGERP (glyph->object)
26251 && !EQ (glyph->object, disp_string)
26252 && !(BUFFERP (glyph->object)
26253 && (glyph->charpos >= start_charpos
26254 && glyph->charpos < end_charpos));
26255 ++glyph)
26256 {
26257 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26258 are present at buffer positions between START_CHARPOS and
26259 END_CHARPOS, or if they come from an overlay. */
26260 if (EQ (glyph->object, before_string))
26261 {
26262 pos = string_buffer_position (before_string,
26263 start_charpos);
26264 /* If pos == 0, it means before_string came from an
26265 overlay, not from a buffer position. */
26266 if (!pos || (pos >= start_charpos && pos < end_charpos))
26267 break;
26268 }
26269 else if (EQ (glyph->object, after_string))
26270 {
26271 pos = string_buffer_position (after_string, end_charpos);
26272 if (!pos || (pos >= start_charpos && pos < end_charpos))
26273 break;
26274 }
26275 x += glyph->pixel_width;
26276 }
26277 hlinfo->mouse_face_beg_x = x;
26278 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26279 }
26280 else
26281 {
26282 /* This row is in a right to left paragraph. Scan it right to
26283 left. */
26284 struct glyph *g;
26285
26286 end = r1->glyphs[TEXT_AREA] - 1;
26287 glyph = end + r1->used[TEXT_AREA];
26288
26289 /* Skip truncation glyphs at the start of the glyph row. */
26290 if (r1->displays_text_p)
26291 for (; glyph > end
26292 && INTEGERP (glyph->object)
26293 && glyph->charpos < 0;
26294 --glyph)
26295 ;
26296
26297 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26298 or DISP_STRING, and the first glyph from buffer whose
26299 position is between START_CHARPOS and END_CHARPOS. */
26300 for (; glyph > end
26301 && !INTEGERP (glyph->object)
26302 && !EQ (glyph->object, disp_string)
26303 && !(BUFFERP (glyph->object)
26304 && (glyph->charpos >= start_charpos
26305 && glyph->charpos < end_charpos));
26306 --glyph)
26307 {
26308 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26309 are present at buffer positions between START_CHARPOS and
26310 END_CHARPOS, or if they come from an overlay. */
26311 if (EQ (glyph->object, before_string))
26312 {
26313 pos = string_buffer_position (before_string, start_charpos);
26314 /* If pos == 0, it means before_string came from an
26315 overlay, not from a buffer position. */
26316 if (!pos || (pos >= start_charpos && pos < end_charpos))
26317 break;
26318 }
26319 else if (EQ (glyph->object, after_string))
26320 {
26321 pos = string_buffer_position (after_string, end_charpos);
26322 if (!pos || (pos >= start_charpos && pos < end_charpos))
26323 break;
26324 }
26325 }
26326
26327 glyph++; /* first glyph to the right of the highlighted area */
26328 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26329 x += g->pixel_width;
26330 hlinfo->mouse_face_beg_x = x;
26331 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26332 }
26333
26334 /* If the highlight ends in a different row, compute GLYPH and END
26335 for the end row. Otherwise, reuse the values computed above for
26336 the row where the highlight begins. */
26337 if (r2 != r1)
26338 {
26339 if (!r2->reversed_p)
26340 {
26341 glyph = r2->glyphs[TEXT_AREA];
26342 end = glyph + r2->used[TEXT_AREA];
26343 x = r2->x;
26344 }
26345 else
26346 {
26347 end = r2->glyphs[TEXT_AREA] - 1;
26348 glyph = end + r2->used[TEXT_AREA];
26349 }
26350 }
26351
26352 if (!r2->reversed_p)
26353 {
26354 /* Skip truncation and continuation glyphs near the end of the
26355 row, and also blanks and stretch glyphs inserted by
26356 extend_face_to_end_of_line. */
26357 while (end > glyph
26358 && INTEGERP ((end - 1)->object))
26359 --end;
26360 /* Scan the rest of the glyph row from the end, looking for the
26361 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26362 DISP_STRING, or whose position is between START_CHARPOS
26363 and END_CHARPOS */
26364 for (--end;
26365 end > glyph
26366 && !INTEGERP (end->object)
26367 && !EQ (end->object, disp_string)
26368 && !(BUFFERP (end->object)
26369 && (end->charpos >= start_charpos
26370 && end->charpos < end_charpos));
26371 --end)
26372 {
26373 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26374 are present at buffer positions between START_CHARPOS and
26375 END_CHARPOS, or if they come from an overlay. */
26376 if (EQ (end->object, before_string))
26377 {
26378 pos = string_buffer_position (before_string, start_charpos);
26379 if (!pos || (pos >= start_charpos && pos < end_charpos))
26380 break;
26381 }
26382 else if (EQ (end->object, after_string))
26383 {
26384 pos = string_buffer_position (after_string, end_charpos);
26385 if (!pos || (pos >= start_charpos && pos < end_charpos))
26386 break;
26387 }
26388 }
26389 /* Find the X coordinate of the last glyph to be highlighted. */
26390 for (; glyph <= end; ++glyph)
26391 x += glyph->pixel_width;
26392
26393 hlinfo->mouse_face_end_x = x;
26394 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26395 }
26396 else
26397 {
26398 /* Skip truncation and continuation glyphs near the end of the
26399 row, and also blanks and stretch glyphs inserted by
26400 extend_face_to_end_of_line. */
26401 x = r2->x;
26402 end++;
26403 while (end < glyph
26404 && INTEGERP (end->object))
26405 {
26406 x += end->pixel_width;
26407 ++end;
26408 }
26409 /* Scan the rest of the glyph row from the end, looking for the
26410 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26411 DISP_STRING, or whose position is between START_CHARPOS
26412 and END_CHARPOS */
26413 for ( ;
26414 end < glyph
26415 && !INTEGERP (end->object)
26416 && !EQ (end->object, disp_string)
26417 && !(BUFFERP (end->object)
26418 && (end->charpos >= start_charpos
26419 && end->charpos < end_charpos));
26420 ++end)
26421 {
26422 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26423 are present at buffer positions between START_CHARPOS and
26424 END_CHARPOS, or if they come from an overlay. */
26425 if (EQ (end->object, before_string))
26426 {
26427 pos = string_buffer_position (before_string, start_charpos);
26428 if (!pos || (pos >= start_charpos && pos < end_charpos))
26429 break;
26430 }
26431 else if (EQ (end->object, after_string))
26432 {
26433 pos = string_buffer_position (after_string, end_charpos);
26434 if (!pos || (pos >= start_charpos && pos < end_charpos))
26435 break;
26436 }
26437 x += end->pixel_width;
26438 }
26439 /* If we exited the above loop because we arrived at the last
26440 glyph of the row, and its buffer position is still not in
26441 range, it means the last character in range is the preceding
26442 newline. Bump the end column and x values to get past the
26443 last glyph. */
26444 if (end == glyph
26445 && BUFFERP (end->object)
26446 && (end->charpos < start_charpos
26447 || end->charpos >= end_charpos))
26448 {
26449 x += end->pixel_width;
26450 ++end;
26451 }
26452 hlinfo->mouse_face_end_x = x;
26453 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26454 }
26455
26456 hlinfo->mouse_face_window = window;
26457 hlinfo->mouse_face_face_id
26458 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26459 mouse_charpos + 1,
26460 !hlinfo->mouse_face_hidden, -1);
26461 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26462 }
26463
26464 /* The following function is not used anymore (replaced with
26465 mouse_face_from_string_pos), but I leave it here for the time
26466 being, in case someone would. */
26467
26468 #if 0 /* not used */
26469
26470 /* Find the position of the glyph for position POS in OBJECT in
26471 window W's current matrix, and return in *X, *Y the pixel
26472 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26473
26474 RIGHT_P non-zero means return the position of the right edge of the
26475 glyph, RIGHT_P zero means return the left edge position.
26476
26477 If no glyph for POS exists in the matrix, return the position of
26478 the glyph with the next smaller position that is in the matrix, if
26479 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26480 exists in the matrix, return the position of the glyph with the
26481 next larger position in OBJECT.
26482
26483 Value is non-zero if a glyph was found. */
26484
26485 static int
26486 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26487 int *hpos, int *vpos, int *x, int *y, int right_p)
26488 {
26489 int yb = window_text_bottom_y (w);
26490 struct glyph_row *r;
26491 struct glyph *best_glyph = NULL;
26492 struct glyph_row *best_row = NULL;
26493 int best_x = 0;
26494
26495 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26496 r->enabled_p && r->y < yb;
26497 ++r)
26498 {
26499 struct glyph *g = r->glyphs[TEXT_AREA];
26500 struct glyph *e = g + r->used[TEXT_AREA];
26501 int gx;
26502
26503 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26504 if (EQ (g->object, object))
26505 {
26506 if (g->charpos == pos)
26507 {
26508 best_glyph = g;
26509 best_x = gx;
26510 best_row = r;
26511 goto found;
26512 }
26513 else if (best_glyph == NULL
26514 || ((eabs (g->charpos - pos)
26515 < eabs (best_glyph->charpos - pos))
26516 && (right_p
26517 ? g->charpos < pos
26518 : g->charpos > pos)))
26519 {
26520 best_glyph = g;
26521 best_x = gx;
26522 best_row = r;
26523 }
26524 }
26525 }
26526
26527 found:
26528
26529 if (best_glyph)
26530 {
26531 *x = best_x;
26532 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26533
26534 if (right_p)
26535 {
26536 *x += best_glyph->pixel_width;
26537 ++*hpos;
26538 }
26539
26540 *y = best_row->y;
26541 *vpos = best_row - w->current_matrix->rows;
26542 }
26543
26544 return best_glyph != NULL;
26545 }
26546 #endif /* not used */
26547
26548 /* Find the positions of the first and the last glyphs in window W's
26549 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26550 (assumed to be a string), and return in HLINFO's mouse_face_*
26551 members the pixel and column/row coordinates of those glyphs. */
26552
26553 static void
26554 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26555 Lisp_Object object,
26556 EMACS_INT startpos, EMACS_INT endpos)
26557 {
26558 int yb = window_text_bottom_y (w);
26559 struct glyph_row *r;
26560 struct glyph *g, *e;
26561 int gx;
26562 int found = 0;
26563
26564 /* Find the glyph row with at least one position in the range
26565 [STARTPOS..ENDPOS], and the first glyph in that row whose
26566 position belongs to that range. */
26567 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26568 r->enabled_p && r->y < yb;
26569 ++r)
26570 {
26571 if (!r->reversed_p)
26572 {
26573 g = r->glyphs[TEXT_AREA];
26574 e = g + r->used[TEXT_AREA];
26575 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26576 if (EQ (g->object, object)
26577 && startpos <= g->charpos && g->charpos <= endpos)
26578 {
26579 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26580 hlinfo->mouse_face_beg_y = r->y;
26581 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26582 hlinfo->mouse_face_beg_x = gx;
26583 found = 1;
26584 break;
26585 }
26586 }
26587 else
26588 {
26589 struct glyph *g1;
26590
26591 e = r->glyphs[TEXT_AREA];
26592 g = e + r->used[TEXT_AREA];
26593 for ( ; g > e; --g)
26594 if (EQ ((g-1)->object, object)
26595 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26596 {
26597 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26598 hlinfo->mouse_face_beg_y = r->y;
26599 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26600 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26601 gx += g1->pixel_width;
26602 hlinfo->mouse_face_beg_x = gx;
26603 found = 1;
26604 break;
26605 }
26606 }
26607 if (found)
26608 break;
26609 }
26610
26611 if (!found)
26612 return;
26613
26614 /* Starting with the next row, look for the first row which does NOT
26615 include any glyphs whose positions are in the range. */
26616 for (++r; r->enabled_p && r->y < yb; ++r)
26617 {
26618 g = r->glyphs[TEXT_AREA];
26619 e = g + r->used[TEXT_AREA];
26620 found = 0;
26621 for ( ; g < e; ++g)
26622 if (EQ (g->object, object)
26623 && startpos <= g->charpos && g->charpos <= endpos)
26624 {
26625 found = 1;
26626 break;
26627 }
26628 if (!found)
26629 break;
26630 }
26631
26632 /* The highlighted region ends on the previous row. */
26633 r--;
26634
26635 /* Set the end row and its vertical pixel coordinate. */
26636 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26637 hlinfo->mouse_face_end_y = r->y;
26638
26639 /* Compute and set the end column and the end column's horizontal
26640 pixel coordinate. */
26641 if (!r->reversed_p)
26642 {
26643 g = r->glyphs[TEXT_AREA];
26644 e = g + r->used[TEXT_AREA];
26645 for ( ; e > g; --e)
26646 if (EQ ((e-1)->object, object)
26647 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26648 break;
26649 hlinfo->mouse_face_end_col = e - g;
26650
26651 for (gx = r->x; g < e; ++g)
26652 gx += g->pixel_width;
26653 hlinfo->mouse_face_end_x = gx;
26654 }
26655 else
26656 {
26657 e = r->glyphs[TEXT_AREA];
26658 g = e + r->used[TEXT_AREA];
26659 for (gx = r->x ; e < g; ++e)
26660 {
26661 if (EQ (e->object, object)
26662 && startpos <= e->charpos && e->charpos <= endpos)
26663 break;
26664 gx += e->pixel_width;
26665 }
26666 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26667 hlinfo->mouse_face_end_x = gx;
26668 }
26669 }
26670
26671 #ifdef HAVE_WINDOW_SYSTEM
26672
26673 /* See if position X, Y is within a hot-spot of an image. */
26674
26675 static int
26676 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26677 {
26678 if (!CONSP (hot_spot))
26679 return 0;
26680
26681 if (EQ (XCAR (hot_spot), Qrect))
26682 {
26683 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26684 Lisp_Object rect = XCDR (hot_spot);
26685 Lisp_Object tem;
26686 if (!CONSP (rect))
26687 return 0;
26688 if (!CONSP (XCAR (rect)))
26689 return 0;
26690 if (!CONSP (XCDR (rect)))
26691 return 0;
26692 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26693 return 0;
26694 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26695 return 0;
26696 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26697 return 0;
26698 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26699 return 0;
26700 return 1;
26701 }
26702 else if (EQ (XCAR (hot_spot), Qcircle))
26703 {
26704 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26705 Lisp_Object circ = XCDR (hot_spot);
26706 Lisp_Object lr, lx0, ly0;
26707 if (CONSP (circ)
26708 && CONSP (XCAR (circ))
26709 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26710 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26711 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26712 {
26713 double r = XFLOATINT (lr);
26714 double dx = XINT (lx0) - x;
26715 double dy = XINT (ly0) - y;
26716 return (dx * dx + dy * dy <= r * r);
26717 }
26718 }
26719 else if (EQ (XCAR (hot_spot), Qpoly))
26720 {
26721 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26722 if (VECTORP (XCDR (hot_spot)))
26723 {
26724 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26725 Lisp_Object *poly = v->contents;
26726 int n = v->header.size;
26727 int i;
26728 int inside = 0;
26729 Lisp_Object lx, ly;
26730 int x0, y0;
26731
26732 /* Need an even number of coordinates, and at least 3 edges. */
26733 if (n < 6 || n & 1)
26734 return 0;
26735
26736 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26737 If count is odd, we are inside polygon. Pixels on edges
26738 may or may not be included depending on actual geometry of the
26739 polygon. */
26740 if ((lx = poly[n-2], !INTEGERP (lx))
26741 || (ly = poly[n-1], !INTEGERP (lx)))
26742 return 0;
26743 x0 = XINT (lx), y0 = XINT (ly);
26744 for (i = 0; i < n; i += 2)
26745 {
26746 int x1 = x0, y1 = y0;
26747 if ((lx = poly[i], !INTEGERP (lx))
26748 || (ly = poly[i+1], !INTEGERP (ly)))
26749 return 0;
26750 x0 = XINT (lx), y0 = XINT (ly);
26751
26752 /* Does this segment cross the X line? */
26753 if (x0 >= x)
26754 {
26755 if (x1 >= x)
26756 continue;
26757 }
26758 else if (x1 < x)
26759 continue;
26760 if (y > y0 && y > y1)
26761 continue;
26762 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26763 inside = !inside;
26764 }
26765 return inside;
26766 }
26767 }
26768 return 0;
26769 }
26770
26771 Lisp_Object
26772 find_hot_spot (Lisp_Object map, int x, int y)
26773 {
26774 while (CONSP (map))
26775 {
26776 if (CONSP (XCAR (map))
26777 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26778 return XCAR (map);
26779 map = XCDR (map);
26780 }
26781
26782 return Qnil;
26783 }
26784
26785 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26786 3, 3, 0,
26787 doc: /* Lookup in image map MAP coordinates X and Y.
26788 An image map is an alist where each element has the format (AREA ID PLIST).
26789 An AREA is specified as either a rectangle, a circle, or a polygon:
26790 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26791 pixel coordinates of the upper left and bottom right corners.
26792 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26793 and the radius of the circle; r may be a float or integer.
26794 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26795 vector describes one corner in the polygon.
26796 Returns the alist element for the first matching AREA in MAP. */)
26797 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26798 {
26799 if (NILP (map))
26800 return Qnil;
26801
26802 CHECK_NUMBER (x);
26803 CHECK_NUMBER (y);
26804
26805 return find_hot_spot (map, XINT (x), XINT (y));
26806 }
26807
26808
26809 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26810 static void
26811 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26812 {
26813 /* Do not change cursor shape while dragging mouse. */
26814 if (!NILP (do_mouse_tracking))
26815 return;
26816
26817 if (!NILP (pointer))
26818 {
26819 if (EQ (pointer, Qarrow))
26820 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26821 else if (EQ (pointer, Qhand))
26822 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26823 else if (EQ (pointer, Qtext))
26824 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26825 else if (EQ (pointer, intern ("hdrag")))
26826 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26827 #ifdef HAVE_X_WINDOWS
26828 else if (EQ (pointer, intern ("vdrag")))
26829 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26830 #endif
26831 else if (EQ (pointer, intern ("hourglass")))
26832 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26833 else if (EQ (pointer, Qmodeline))
26834 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26835 else
26836 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26837 }
26838
26839 if (cursor != No_Cursor)
26840 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26841 }
26842
26843 #endif /* HAVE_WINDOW_SYSTEM */
26844
26845 /* Take proper action when mouse has moved to the mode or header line
26846 or marginal area AREA of window W, x-position X and y-position Y.
26847 X is relative to the start of the text display area of W, so the
26848 width of bitmap areas and scroll bars must be subtracted to get a
26849 position relative to the start of the mode line. */
26850
26851 static void
26852 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26853 enum window_part area)
26854 {
26855 struct window *w = XWINDOW (window);
26856 struct frame *f = XFRAME (w->frame);
26857 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26858 #ifdef HAVE_WINDOW_SYSTEM
26859 Display_Info *dpyinfo;
26860 #endif
26861 Cursor cursor = No_Cursor;
26862 Lisp_Object pointer = Qnil;
26863 int dx, dy, width, height;
26864 EMACS_INT charpos;
26865 Lisp_Object string, object = Qnil;
26866 Lisp_Object pos, help;
26867
26868 Lisp_Object mouse_face;
26869 int original_x_pixel = x;
26870 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26871 struct glyph_row *row;
26872
26873 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26874 {
26875 int x0;
26876 struct glyph *end;
26877
26878 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26879 returns them in row/column units! */
26880 string = mode_line_string (w, area, &x, &y, &charpos,
26881 &object, &dx, &dy, &width, &height);
26882
26883 row = (area == ON_MODE_LINE
26884 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26885 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26886
26887 /* Find the glyph under the mouse pointer. */
26888 if (row->mode_line_p && row->enabled_p)
26889 {
26890 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26891 end = glyph + row->used[TEXT_AREA];
26892
26893 for (x0 = original_x_pixel;
26894 glyph < end && x0 >= glyph->pixel_width;
26895 ++glyph)
26896 x0 -= glyph->pixel_width;
26897
26898 if (glyph >= end)
26899 glyph = NULL;
26900 }
26901 }
26902 else
26903 {
26904 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26905 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26906 returns them in row/column units! */
26907 string = marginal_area_string (w, area, &x, &y, &charpos,
26908 &object, &dx, &dy, &width, &height);
26909 }
26910
26911 help = Qnil;
26912
26913 #ifdef HAVE_WINDOW_SYSTEM
26914 if (IMAGEP (object))
26915 {
26916 Lisp_Object image_map, hotspot;
26917 if ((image_map = Fplist_get (XCDR (object), QCmap),
26918 !NILP (image_map))
26919 && (hotspot = find_hot_spot (image_map, dx, dy),
26920 CONSP (hotspot))
26921 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26922 {
26923 Lisp_Object plist;
26924
26925 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26926 If so, we could look for mouse-enter, mouse-leave
26927 properties in PLIST (and do something...). */
26928 hotspot = XCDR (hotspot);
26929 if (CONSP (hotspot)
26930 && (plist = XCAR (hotspot), CONSP (plist)))
26931 {
26932 pointer = Fplist_get (plist, Qpointer);
26933 if (NILP (pointer))
26934 pointer = Qhand;
26935 help = Fplist_get (plist, Qhelp_echo);
26936 if (!NILP (help))
26937 {
26938 help_echo_string = help;
26939 /* Is this correct? ++kfs */
26940 XSETWINDOW (help_echo_window, w);
26941 help_echo_object = w->buffer;
26942 help_echo_pos = charpos;
26943 }
26944 }
26945 }
26946 if (NILP (pointer))
26947 pointer = Fplist_get (XCDR (object), QCpointer);
26948 }
26949 #endif /* HAVE_WINDOW_SYSTEM */
26950
26951 if (STRINGP (string))
26952 {
26953 pos = make_number (charpos);
26954 /* If we're on a string with `help-echo' text property, arrange
26955 for the help to be displayed. This is done by setting the
26956 global variable help_echo_string to the help string. */
26957 if (NILP (help))
26958 {
26959 help = Fget_text_property (pos, Qhelp_echo, string);
26960 if (!NILP (help))
26961 {
26962 help_echo_string = help;
26963 XSETWINDOW (help_echo_window, w);
26964 help_echo_object = string;
26965 help_echo_pos = charpos;
26966 }
26967 }
26968
26969 #ifdef HAVE_WINDOW_SYSTEM
26970 if (FRAME_WINDOW_P (f))
26971 {
26972 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26973 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26974 if (NILP (pointer))
26975 pointer = Fget_text_property (pos, Qpointer, string);
26976
26977 /* Change the mouse pointer according to what is under X/Y. */
26978 if (NILP (pointer)
26979 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26980 {
26981 Lisp_Object map;
26982 map = Fget_text_property (pos, Qlocal_map, string);
26983 if (!KEYMAPP (map))
26984 map = Fget_text_property (pos, Qkeymap, string);
26985 if (!KEYMAPP (map))
26986 cursor = dpyinfo->vertical_scroll_bar_cursor;
26987 }
26988 }
26989 #endif
26990
26991 /* Change the mouse face according to what is under X/Y. */
26992 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26993 if (!NILP (mouse_face)
26994 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26995 && glyph)
26996 {
26997 Lisp_Object b, e;
26998
26999 struct glyph * tmp_glyph;
27000
27001 int gpos;
27002 int gseq_length;
27003 int total_pixel_width;
27004 EMACS_INT begpos, endpos, ignore;
27005
27006 int vpos, hpos;
27007
27008 b = Fprevious_single_property_change (make_number (charpos + 1),
27009 Qmouse_face, string, Qnil);
27010 if (NILP (b))
27011 begpos = 0;
27012 else
27013 begpos = XINT (b);
27014
27015 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27016 if (NILP (e))
27017 endpos = SCHARS (string);
27018 else
27019 endpos = XINT (e);
27020
27021 /* Calculate the glyph position GPOS of GLYPH in the
27022 displayed string, relative to the beginning of the
27023 highlighted part of the string.
27024
27025 Note: GPOS is different from CHARPOS. CHARPOS is the
27026 position of GLYPH in the internal string object. A mode
27027 line string format has structures which are converted to
27028 a flattened string by the Emacs Lisp interpreter. The
27029 internal string is an element of those structures. The
27030 displayed string is the flattened string. */
27031 tmp_glyph = row_start_glyph;
27032 while (tmp_glyph < glyph
27033 && (!(EQ (tmp_glyph->object, glyph->object)
27034 && begpos <= tmp_glyph->charpos
27035 && tmp_glyph->charpos < endpos)))
27036 tmp_glyph++;
27037 gpos = glyph - tmp_glyph;
27038
27039 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27040 the highlighted part of the displayed string to which
27041 GLYPH belongs. Note: GSEQ_LENGTH is different from
27042 SCHARS (STRING), because the latter returns the length of
27043 the internal string. */
27044 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27045 tmp_glyph > glyph
27046 && (!(EQ (tmp_glyph->object, glyph->object)
27047 && begpos <= tmp_glyph->charpos
27048 && tmp_glyph->charpos < endpos));
27049 tmp_glyph--)
27050 ;
27051 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27052
27053 /* Calculate the total pixel width of all the glyphs between
27054 the beginning of the highlighted area and GLYPH. */
27055 total_pixel_width = 0;
27056 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27057 total_pixel_width += tmp_glyph->pixel_width;
27058
27059 /* Pre calculation of re-rendering position. Note: X is in
27060 column units here, after the call to mode_line_string or
27061 marginal_area_string. */
27062 hpos = x - gpos;
27063 vpos = (area == ON_MODE_LINE
27064 ? (w->current_matrix)->nrows - 1
27065 : 0);
27066
27067 /* If GLYPH's position is included in the region that is
27068 already drawn in mouse face, we have nothing to do. */
27069 if ( EQ (window, hlinfo->mouse_face_window)
27070 && (!row->reversed_p
27071 ? (hlinfo->mouse_face_beg_col <= hpos
27072 && hpos < hlinfo->mouse_face_end_col)
27073 /* In R2L rows we swap BEG and END, see below. */
27074 : (hlinfo->mouse_face_end_col <= hpos
27075 && hpos < hlinfo->mouse_face_beg_col))
27076 && hlinfo->mouse_face_beg_row == vpos )
27077 return;
27078
27079 if (clear_mouse_face (hlinfo))
27080 cursor = No_Cursor;
27081
27082 if (!row->reversed_p)
27083 {
27084 hlinfo->mouse_face_beg_col = hpos;
27085 hlinfo->mouse_face_beg_x = original_x_pixel
27086 - (total_pixel_width + dx);
27087 hlinfo->mouse_face_end_col = hpos + gseq_length;
27088 hlinfo->mouse_face_end_x = 0;
27089 }
27090 else
27091 {
27092 /* In R2L rows, show_mouse_face expects BEG and END
27093 coordinates to be swapped. */
27094 hlinfo->mouse_face_end_col = hpos;
27095 hlinfo->mouse_face_end_x = original_x_pixel
27096 - (total_pixel_width + dx);
27097 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27098 hlinfo->mouse_face_beg_x = 0;
27099 }
27100
27101 hlinfo->mouse_face_beg_row = vpos;
27102 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27103 hlinfo->mouse_face_beg_y = 0;
27104 hlinfo->mouse_face_end_y = 0;
27105 hlinfo->mouse_face_past_end = 0;
27106 hlinfo->mouse_face_window = window;
27107
27108 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27109 charpos,
27110 0, 0, 0,
27111 &ignore,
27112 glyph->face_id,
27113 1);
27114 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27115
27116 if (NILP (pointer))
27117 pointer = Qhand;
27118 }
27119 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27120 clear_mouse_face (hlinfo);
27121 }
27122 #ifdef HAVE_WINDOW_SYSTEM
27123 if (FRAME_WINDOW_P (f))
27124 define_frame_cursor1 (f, cursor, pointer);
27125 #endif
27126 }
27127
27128
27129 /* EXPORT:
27130 Take proper action when the mouse has moved to position X, Y on
27131 frame F as regards highlighting characters that have mouse-face
27132 properties. Also de-highlighting chars where the mouse was before.
27133 X and Y can be negative or out of range. */
27134
27135 void
27136 note_mouse_highlight (struct frame *f, int x, int y)
27137 {
27138 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27139 enum window_part part = ON_NOTHING;
27140 Lisp_Object window;
27141 struct window *w;
27142 Cursor cursor = No_Cursor;
27143 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27144 struct buffer *b;
27145
27146 /* When a menu is active, don't highlight because this looks odd. */
27147 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27148 if (popup_activated ())
27149 return;
27150 #endif
27151
27152 if (NILP (Vmouse_highlight)
27153 || !f->glyphs_initialized_p
27154 || f->pointer_invisible)
27155 return;
27156
27157 hlinfo->mouse_face_mouse_x = x;
27158 hlinfo->mouse_face_mouse_y = y;
27159 hlinfo->mouse_face_mouse_frame = f;
27160
27161 if (hlinfo->mouse_face_defer)
27162 return;
27163
27164 if (gc_in_progress)
27165 {
27166 hlinfo->mouse_face_deferred_gc = 1;
27167 return;
27168 }
27169
27170 /* Which window is that in? */
27171 window = window_from_coordinates (f, x, y, &part, 1);
27172
27173 /* If displaying active text in another window, clear that. */
27174 if (! EQ (window, hlinfo->mouse_face_window)
27175 /* Also clear if we move out of text area in same window. */
27176 || (!NILP (hlinfo->mouse_face_window)
27177 && !NILP (window)
27178 && part != ON_TEXT
27179 && part != ON_MODE_LINE
27180 && part != ON_HEADER_LINE))
27181 clear_mouse_face (hlinfo);
27182
27183 /* Not on a window -> return. */
27184 if (!WINDOWP (window))
27185 return;
27186
27187 /* Reset help_echo_string. It will get recomputed below. */
27188 help_echo_string = Qnil;
27189
27190 /* Convert to window-relative pixel coordinates. */
27191 w = XWINDOW (window);
27192 frame_to_window_pixel_xy (w, &x, &y);
27193
27194 #ifdef HAVE_WINDOW_SYSTEM
27195 /* Handle tool-bar window differently since it doesn't display a
27196 buffer. */
27197 if (EQ (window, f->tool_bar_window))
27198 {
27199 note_tool_bar_highlight (f, x, y);
27200 return;
27201 }
27202 #endif
27203
27204 /* Mouse is on the mode, header line or margin? */
27205 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27206 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27207 {
27208 note_mode_line_or_margin_highlight (window, x, y, part);
27209 return;
27210 }
27211
27212 #ifdef HAVE_WINDOW_SYSTEM
27213 if (part == ON_VERTICAL_BORDER)
27214 {
27215 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27216 help_echo_string = build_string ("drag-mouse-1: resize");
27217 }
27218 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27219 || part == ON_SCROLL_BAR)
27220 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27221 else
27222 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27223 #endif
27224
27225 /* Are we in a window whose display is up to date?
27226 And verify the buffer's text has not changed. */
27227 b = XBUFFER (w->buffer);
27228 if (part == ON_TEXT
27229 && EQ (w->window_end_valid, w->buffer)
27230 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27231 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27232 {
27233 int hpos, vpos, dx, dy, area = LAST_AREA;
27234 EMACS_INT pos;
27235 struct glyph *glyph;
27236 Lisp_Object object;
27237 Lisp_Object mouse_face = Qnil, position;
27238 Lisp_Object *overlay_vec = NULL;
27239 ptrdiff_t i, noverlays;
27240 struct buffer *obuf;
27241 EMACS_INT obegv, ozv;
27242 int same_region;
27243
27244 /* Find the glyph under X/Y. */
27245 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27246
27247 #ifdef HAVE_WINDOW_SYSTEM
27248 /* Look for :pointer property on image. */
27249 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27250 {
27251 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27252 if (img != NULL && IMAGEP (img->spec))
27253 {
27254 Lisp_Object image_map, hotspot;
27255 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27256 !NILP (image_map))
27257 && (hotspot = find_hot_spot (image_map,
27258 glyph->slice.img.x + dx,
27259 glyph->slice.img.y + dy),
27260 CONSP (hotspot))
27261 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27262 {
27263 Lisp_Object plist;
27264
27265 /* Could check XCAR (hotspot) to see if we enter/leave
27266 this hot-spot.
27267 If so, we could look for mouse-enter, mouse-leave
27268 properties in PLIST (and do something...). */
27269 hotspot = XCDR (hotspot);
27270 if (CONSP (hotspot)
27271 && (plist = XCAR (hotspot), CONSP (plist)))
27272 {
27273 pointer = Fplist_get (plist, Qpointer);
27274 if (NILP (pointer))
27275 pointer = Qhand;
27276 help_echo_string = Fplist_get (plist, Qhelp_echo);
27277 if (!NILP (help_echo_string))
27278 {
27279 help_echo_window = window;
27280 help_echo_object = glyph->object;
27281 help_echo_pos = glyph->charpos;
27282 }
27283 }
27284 }
27285 if (NILP (pointer))
27286 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27287 }
27288 }
27289 #endif /* HAVE_WINDOW_SYSTEM */
27290
27291 /* Clear mouse face if X/Y not over text. */
27292 if (glyph == NULL
27293 || area != TEXT_AREA
27294 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27295 /* Glyph's OBJECT is an integer for glyphs inserted by the
27296 display engine for its internal purposes, like truncation
27297 and continuation glyphs and blanks beyond the end of
27298 line's text on text terminals. If we are over such a
27299 glyph, we are not over any text. */
27300 || INTEGERP (glyph->object)
27301 /* R2L rows have a stretch glyph at their front, which
27302 stands for no text, whereas L2R rows have no glyphs at
27303 all beyond the end of text. Treat such stretch glyphs
27304 like we do with NULL glyphs in L2R rows. */
27305 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27306 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27307 && glyph->type == STRETCH_GLYPH
27308 && glyph->avoid_cursor_p))
27309 {
27310 if (clear_mouse_face (hlinfo))
27311 cursor = No_Cursor;
27312 #ifdef HAVE_WINDOW_SYSTEM
27313 if (FRAME_WINDOW_P (f) && NILP (pointer))
27314 {
27315 if (area != TEXT_AREA)
27316 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27317 else
27318 pointer = Vvoid_text_area_pointer;
27319 }
27320 #endif
27321 goto set_cursor;
27322 }
27323
27324 pos = glyph->charpos;
27325 object = glyph->object;
27326 if (!STRINGP (object) && !BUFFERP (object))
27327 goto set_cursor;
27328
27329 /* If we get an out-of-range value, return now; avoid an error. */
27330 if (BUFFERP (object) && pos > BUF_Z (b))
27331 goto set_cursor;
27332
27333 /* Make the window's buffer temporarily current for
27334 overlays_at and compute_char_face. */
27335 obuf = current_buffer;
27336 current_buffer = b;
27337 obegv = BEGV;
27338 ozv = ZV;
27339 BEGV = BEG;
27340 ZV = Z;
27341
27342 /* Is this char mouse-active or does it have help-echo? */
27343 position = make_number (pos);
27344
27345 if (BUFFERP (object))
27346 {
27347 /* Put all the overlays we want in a vector in overlay_vec. */
27348 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27349 /* Sort overlays into increasing priority order. */
27350 noverlays = sort_overlays (overlay_vec, noverlays, w);
27351 }
27352 else
27353 noverlays = 0;
27354
27355 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27356
27357 if (same_region)
27358 cursor = No_Cursor;
27359
27360 /* Check mouse-face highlighting. */
27361 if (! same_region
27362 /* If there exists an overlay with mouse-face overlapping
27363 the one we are currently highlighting, we have to
27364 check if we enter the overlapping overlay, and then
27365 highlight only that. */
27366 || (OVERLAYP (hlinfo->mouse_face_overlay)
27367 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27368 {
27369 /* Find the highest priority overlay with a mouse-face. */
27370 Lisp_Object overlay = Qnil;
27371 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27372 {
27373 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27374 if (!NILP (mouse_face))
27375 overlay = overlay_vec[i];
27376 }
27377
27378 /* If we're highlighting the same overlay as before, there's
27379 no need to do that again. */
27380 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27381 goto check_help_echo;
27382 hlinfo->mouse_face_overlay = overlay;
27383
27384 /* Clear the display of the old active region, if any. */
27385 if (clear_mouse_face (hlinfo))
27386 cursor = No_Cursor;
27387
27388 /* If no overlay applies, get a text property. */
27389 if (NILP (overlay))
27390 mouse_face = Fget_text_property (position, Qmouse_face, object);
27391
27392 /* Next, compute the bounds of the mouse highlighting and
27393 display it. */
27394 if (!NILP (mouse_face) && STRINGP (object))
27395 {
27396 /* The mouse-highlighting comes from a display string
27397 with a mouse-face. */
27398 Lisp_Object s, e;
27399 EMACS_INT ignore;
27400
27401 s = Fprevious_single_property_change
27402 (make_number (pos + 1), Qmouse_face, object, Qnil);
27403 e = Fnext_single_property_change
27404 (position, Qmouse_face, object, Qnil);
27405 if (NILP (s))
27406 s = make_number (0);
27407 if (NILP (e))
27408 e = make_number (SCHARS (object) - 1);
27409 mouse_face_from_string_pos (w, hlinfo, object,
27410 XINT (s), XINT (e));
27411 hlinfo->mouse_face_past_end = 0;
27412 hlinfo->mouse_face_window = window;
27413 hlinfo->mouse_face_face_id
27414 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27415 glyph->face_id, 1);
27416 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27417 cursor = No_Cursor;
27418 }
27419 else
27420 {
27421 /* The mouse-highlighting, if any, comes from an overlay
27422 or text property in the buffer. */
27423 Lisp_Object buffer IF_LINT (= Qnil);
27424 Lisp_Object disp_string IF_LINT (= Qnil);
27425
27426 if (STRINGP (object))
27427 {
27428 /* If we are on a display string with no mouse-face,
27429 check if the text under it has one. */
27430 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27431 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27432 pos = string_buffer_position (object, start);
27433 if (pos > 0)
27434 {
27435 mouse_face = get_char_property_and_overlay
27436 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27437 buffer = w->buffer;
27438 disp_string = object;
27439 }
27440 }
27441 else
27442 {
27443 buffer = object;
27444 disp_string = Qnil;
27445 }
27446
27447 if (!NILP (mouse_face))
27448 {
27449 Lisp_Object before, after;
27450 Lisp_Object before_string, after_string;
27451 /* To correctly find the limits of mouse highlight
27452 in a bidi-reordered buffer, we must not use the
27453 optimization of limiting the search in
27454 previous-single-property-change and
27455 next-single-property-change, because
27456 rows_from_pos_range needs the real start and end
27457 positions to DTRT in this case. That's because
27458 the first row visible in a window does not
27459 necessarily display the character whose position
27460 is the smallest. */
27461 Lisp_Object lim1 =
27462 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27463 ? Fmarker_position (w->start)
27464 : Qnil;
27465 Lisp_Object lim2 =
27466 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27467 ? make_number (BUF_Z (XBUFFER (buffer))
27468 - XFASTINT (w->window_end_pos))
27469 : Qnil;
27470
27471 if (NILP (overlay))
27472 {
27473 /* Handle the text property case. */
27474 before = Fprevious_single_property_change
27475 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27476 after = Fnext_single_property_change
27477 (make_number (pos), Qmouse_face, buffer, lim2);
27478 before_string = after_string = Qnil;
27479 }
27480 else
27481 {
27482 /* Handle the overlay case. */
27483 before = Foverlay_start (overlay);
27484 after = Foverlay_end (overlay);
27485 before_string = Foverlay_get (overlay, Qbefore_string);
27486 after_string = Foverlay_get (overlay, Qafter_string);
27487
27488 if (!STRINGP (before_string)) before_string = Qnil;
27489 if (!STRINGP (after_string)) after_string = Qnil;
27490 }
27491
27492 mouse_face_from_buffer_pos (window, hlinfo, pos,
27493 NILP (before)
27494 ? 1
27495 : XFASTINT (before),
27496 NILP (after)
27497 ? BUF_Z (XBUFFER (buffer))
27498 : XFASTINT (after),
27499 before_string, after_string,
27500 disp_string);
27501 cursor = No_Cursor;
27502 }
27503 }
27504 }
27505
27506 check_help_echo:
27507
27508 /* Look for a `help-echo' property. */
27509 if (NILP (help_echo_string)) {
27510 Lisp_Object help, overlay;
27511
27512 /* Check overlays first. */
27513 help = overlay = Qnil;
27514 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27515 {
27516 overlay = overlay_vec[i];
27517 help = Foverlay_get (overlay, Qhelp_echo);
27518 }
27519
27520 if (!NILP (help))
27521 {
27522 help_echo_string = help;
27523 help_echo_window = window;
27524 help_echo_object = overlay;
27525 help_echo_pos = pos;
27526 }
27527 else
27528 {
27529 Lisp_Object obj = glyph->object;
27530 EMACS_INT charpos = glyph->charpos;
27531
27532 /* Try text properties. */
27533 if (STRINGP (obj)
27534 && charpos >= 0
27535 && charpos < SCHARS (obj))
27536 {
27537 help = Fget_text_property (make_number (charpos),
27538 Qhelp_echo, obj);
27539 if (NILP (help))
27540 {
27541 /* If the string itself doesn't specify a help-echo,
27542 see if the buffer text ``under'' it does. */
27543 struct glyph_row *r
27544 = MATRIX_ROW (w->current_matrix, vpos);
27545 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27546 EMACS_INT p = string_buffer_position (obj, start);
27547 if (p > 0)
27548 {
27549 help = Fget_char_property (make_number (p),
27550 Qhelp_echo, w->buffer);
27551 if (!NILP (help))
27552 {
27553 charpos = p;
27554 obj = w->buffer;
27555 }
27556 }
27557 }
27558 }
27559 else if (BUFFERP (obj)
27560 && charpos >= BEGV
27561 && charpos < ZV)
27562 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27563 obj);
27564
27565 if (!NILP (help))
27566 {
27567 help_echo_string = help;
27568 help_echo_window = window;
27569 help_echo_object = obj;
27570 help_echo_pos = charpos;
27571 }
27572 }
27573 }
27574
27575 #ifdef HAVE_WINDOW_SYSTEM
27576 /* Look for a `pointer' property. */
27577 if (FRAME_WINDOW_P (f) && NILP (pointer))
27578 {
27579 /* Check overlays first. */
27580 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27581 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27582
27583 if (NILP (pointer))
27584 {
27585 Lisp_Object obj = glyph->object;
27586 EMACS_INT charpos = glyph->charpos;
27587
27588 /* Try text properties. */
27589 if (STRINGP (obj)
27590 && charpos >= 0
27591 && charpos < SCHARS (obj))
27592 {
27593 pointer = Fget_text_property (make_number (charpos),
27594 Qpointer, obj);
27595 if (NILP (pointer))
27596 {
27597 /* If the string itself doesn't specify a pointer,
27598 see if the buffer text ``under'' it does. */
27599 struct glyph_row *r
27600 = MATRIX_ROW (w->current_matrix, vpos);
27601 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27602 EMACS_INT p = string_buffer_position (obj, start);
27603 if (p > 0)
27604 pointer = Fget_char_property (make_number (p),
27605 Qpointer, w->buffer);
27606 }
27607 }
27608 else if (BUFFERP (obj)
27609 && charpos >= BEGV
27610 && charpos < ZV)
27611 pointer = Fget_text_property (make_number (charpos),
27612 Qpointer, obj);
27613 }
27614 }
27615 #endif /* HAVE_WINDOW_SYSTEM */
27616
27617 BEGV = obegv;
27618 ZV = ozv;
27619 current_buffer = obuf;
27620 }
27621
27622 set_cursor:
27623
27624 #ifdef HAVE_WINDOW_SYSTEM
27625 if (FRAME_WINDOW_P (f))
27626 define_frame_cursor1 (f, cursor, pointer);
27627 #else
27628 /* This is here to prevent a compiler error, about "label at end of
27629 compound statement". */
27630 return;
27631 #endif
27632 }
27633
27634
27635 /* EXPORT for RIF:
27636 Clear any mouse-face on window W. This function is part of the
27637 redisplay interface, and is called from try_window_id and similar
27638 functions to ensure the mouse-highlight is off. */
27639
27640 void
27641 x_clear_window_mouse_face (struct window *w)
27642 {
27643 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27644 Lisp_Object window;
27645
27646 BLOCK_INPUT;
27647 XSETWINDOW (window, w);
27648 if (EQ (window, hlinfo->mouse_face_window))
27649 clear_mouse_face (hlinfo);
27650 UNBLOCK_INPUT;
27651 }
27652
27653
27654 /* EXPORT:
27655 Just discard the mouse face information for frame F, if any.
27656 This is used when the size of F is changed. */
27657
27658 void
27659 cancel_mouse_face (struct frame *f)
27660 {
27661 Lisp_Object window;
27662 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27663
27664 window = hlinfo->mouse_face_window;
27665 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27666 {
27667 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27668 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27669 hlinfo->mouse_face_window = Qnil;
27670 }
27671 }
27672
27673
27674 \f
27675 /***********************************************************************
27676 Exposure Events
27677 ***********************************************************************/
27678
27679 #ifdef HAVE_WINDOW_SYSTEM
27680
27681 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27682 which intersects rectangle R. R is in window-relative coordinates. */
27683
27684 static void
27685 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27686 enum glyph_row_area area)
27687 {
27688 struct glyph *first = row->glyphs[area];
27689 struct glyph *end = row->glyphs[area] + row->used[area];
27690 struct glyph *last;
27691 int first_x, start_x, x;
27692
27693 if (area == TEXT_AREA && row->fill_line_p)
27694 /* If row extends face to end of line write the whole line. */
27695 draw_glyphs (w, 0, row, area,
27696 0, row->used[area],
27697 DRAW_NORMAL_TEXT, 0);
27698 else
27699 {
27700 /* Set START_X to the window-relative start position for drawing glyphs of
27701 AREA. The first glyph of the text area can be partially visible.
27702 The first glyphs of other areas cannot. */
27703 start_x = window_box_left_offset (w, area);
27704 x = start_x;
27705 if (area == TEXT_AREA)
27706 x += row->x;
27707
27708 /* Find the first glyph that must be redrawn. */
27709 while (first < end
27710 && x + first->pixel_width < r->x)
27711 {
27712 x += first->pixel_width;
27713 ++first;
27714 }
27715
27716 /* Find the last one. */
27717 last = first;
27718 first_x = x;
27719 while (last < end
27720 && x < r->x + r->width)
27721 {
27722 x += last->pixel_width;
27723 ++last;
27724 }
27725
27726 /* Repaint. */
27727 if (last > first)
27728 draw_glyphs (w, first_x - start_x, row, area,
27729 first - row->glyphs[area], last - row->glyphs[area],
27730 DRAW_NORMAL_TEXT, 0);
27731 }
27732 }
27733
27734
27735 /* Redraw the parts of the glyph row ROW on window W intersecting
27736 rectangle R. R is in window-relative coordinates. Value is
27737 non-zero if mouse-face was overwritten. */
27738
27739 static int
27740 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27741 {
27742 xassert (row->enabled_p);
27743
27744 if (row->mode_line_p || w->pseudo_window_p)
27745 draw_glyphs (w, 0, row, TEXT_AREA,
27746 0, row->used[TEXT_AREA],
27747 DRAW_NORMAL_TEXT, 0);
27748 else
27749 {
27750 if (row->used[LEFT_MARGIN_AREA])
27751 expose_area (w, row, r, LEFT_MARGIN_AREA);
27752 if (row->used[TEXT_AREA])
27753 expose_area (w, row, r, TEXT_AREA);
27754 if (row->used[RIGHT_MARGIN_AREA])
27755 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27756 draw_row_fringe_bitmaps (w, row);
27757 }
27758
27759 return row->mouse_face_p;
27760 }
27761
27762
27763 /* Redraw those parts of glyphs rows during expose event handling that
27764 overlap other rows. Redrawing of an exposed line writes over parts
27765 of lines overlapping that exposed line; this function fixes that.
27766
27767 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27768 row in W's current matrix that is exposed and overlaps other rows.
27769 LAST_OVERLAPPING_ROW is the last such row. */
27770
27771 static void
27772 expose_overlaps (struct window *w,
27773 struct glyph_row *first_overlapping_row,
27774 struct glyph_row *last_overlapping_row,
27775 XRectangle *r)
27776 {
27777 struct glyph_row *row;
27778
27779 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27780 if (row->overlapping_p)
27781 {
27782 xassert (row->enabled_p && !row->mode_line_p);
27783
27784 row->clip = r;
27785 if (row->used[LEFT_MARGIN_AREA])
27786 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27787
27788 if (row->used[TEXT_AREA])
27789 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27790
27791 if (row->used[RIGHT_MARGIN_AREA])
27792 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27793 row->clip = NULL;
27794 }
27795 }
27796
27797
27798 /* Return non-zero if W's cursor intersects rectangle R. */
27799
27800 static int
27801 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27802 {
27803 XRectangle cr, result;
27804 struct glyph *cursor_glyph;
27805 struct glyph_row *row;
27806
27807 if (w->phys_cursor.vpos >= 0
27808 && w->phys_cursor.vpos < w->current_matrix->nrows
27809 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27810 row->enabled_p)
27811 && row->cursor_in_fringe_p)
27812 {
27813 /* Cursor is in the fringe. */
27814 cr.x = window_box_right_offset (w,
27815 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27816 ? RIGHT_MARGIN_AREA
27817 : TEXT_AREA));
27818 cr.y = row->y;
27819 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27820 cr.height = row->height;
27821 return x_intersect_rectangles (&cr, r, &result);
27822 }
27823
27824 cursor_glyph = get_phys_cursor_glyph (w);
27825 if (cursor_glyph)
27826 {
27827 /* r is relative to W's box, but w->phys_cursor.x is relative
27828 to left edge of W's TEXT area. Adjust it. */
27829 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27830 cr.y = w->phys_cursor.y;
27831 cr.width = cursor_glyph->pixel_width;
27832 cr.height = w->phys_cursor_height;
27833 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27834 I assume the effect is the same -- and this is portable. */
27835 return x_intersect_rectangles (&cr, r, &result);
27836 }
27837 /* If we don't understand the format, pretend we're not in the hot-spot. */
27838 return 0;
27839 }
27840
27841
27842 /* EXPORT:
27843 Draw a vertical window border to the right of window W if W doesn't
27844 have vertical scroll bars. */
27845
27846 void
27847 x_draw_vertical_border (struct window *w)
27848 {
27849 struct frame *f = XFRAME (WINDOW_FRAME (w));
27850
27851 /* We could do better, if we knew what type of scroll-bar the adjacent
27852 windows (on either side) have... But we don't :-(
27853 However, I think this works ok. ++KFS 2003-04-25 */
27854
27855 /* Redraw borders between horizontally adjacent windows. Don't
27856 do it for frames with vertical scroll bars because either the
27857 right scroll bar of a window, or the left scroll bar of its
27858 neighbor will suffice as a border. */
27859 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27860 return;
27861
27862 if (!WINDOW_RIGHTMOST_P (w)
27863 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27864 {
27865 int x0, x1, y0, y1;
27866
27867 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27868 y1 -= 1;
27869
27870 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27871 x1 -= 1;
27872
27873 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27874 }
27875 else if (!WINDOW_LEFTMOST_P (w)
27876 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27877 {
27878 int x0, x1, y0, y1;
27879
27880 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27881 y1 -= 1;
27882
27883 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27884 x0 -= 1;
27885
27886 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27887 }
27888 }
27889
27890
27891 /* Redraw the part of window W intersection rectangle FR. Pixel
27892 coordinates in FR are frame-relative. Call this function with
27893 input blocked. Value is non-zero if the exposure overwrites
27894 mouse-face. */
27895
27896 static int
27897 expose_window (struct window *w, XRectangle *fr)
27898 {
27899 struct frame *f = XFRAME (w->frame);
27900 XRectangle wr, r;
27901 int mouse_face_overwritten_p = 0;
27902
27903 /* If window is not yet fully initialized, do nothing. This can
27904 happen when toolkit scroll bars are used and a window is split.
27905 Reconfiguring the scroll bar will generate an expose for a newly
27906 created window. */
27907 if (w->current_matrix == NULL)
27908 return 0;
27909
27910 /* When we're currently updating the window, display and current
27911 matrix usually don't agree. Arrange for a thorough display
27912 later. */
27913 if (w == updated_window)
27914 {
27915 SET_FRAME_GARBAGED (f);
27916 return 0;
27917 }
27918
27919 /* Frame-relative pixel rectangle of W. */
27920 wr.x = WINDOW_LEFT_EDGE_X (w);
27921 wr.y = WINDOW_TOP_EDGE_Y (w);
27922 wr.width = WINDOW_TOTAL_WIDTH (w);
27923 wr.height = WINDOW_TOTAL_HEIGHT (w);
27924
27925 if (x_intersect_rectangles (fr, &wr, &r))
27926 {
27927 int yb = window_text_bottom_y (w);
27928 struct glyph_row *row;
27929 int cursor_cleared_p, phys_cursor_on_p;
27930 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27931
27932 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27933 r.x, r.y, r.width, r.height));
27934
27935 /* Convert to window coordinates. */
27936 r.x -= WINDOW_LEFT_EDGE_X (w);
27937 r.y -= WINDOW_TOP_EDGE_Y (w);
27938
27939 /* Turn off the cursor. */
27940 if (!w->pseudo_window_p
27941 && phys_cursor_in_rect_p (w, &r))
27942 {
27943 x_clear_cursor (w);
27944 cursor_cleared_p = 1;
27945 }
27946 else
27947 cursor_cleared_p = 0;
27948
27949 /* If the row containing the cursor extends face to end of line,
27950 then expose_area might overwrite the cursor outside the
27951 rectangle and thus notice_overwritten_cursor might clear
27952 w->phys_cursor_on_p. We remember the original value and
27953 check later if it is changed. */
27954 phys_cursor_on_p = w->phys_cursor_on_p;
27955
27956 /* Update lines intersecting rectangle R. */
27957 first_overlapping_row = last_overlapping_row = NULL;
27958 for (row = w->current_matrix->rows;
27959 row->enabled_p;
27960 ++row)
27961 {
27962 int y0 = row->y;
27963 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27964
27965 if ((y0 >= r.y && y0 < r.y + r.height)
27966 || (y1 > r.y && y1 < r.y + r.height)
27967 || (r.y >= y0 && r.y < y1)
27968 || (r.y + r.height > y0 && r.y + r.height < y1))
27969 {
27970 /* A header line may be overlapping, but there is no need
27971 to fix overlapping areas for them. KFS 2005-02-12 */
27972 if (row->overlapping_p && !row->mode_line_p)
27973 {
27974 if (first_overlapping_row == NULL)
27975 first_overlapping_row = row;
27976 last_overlapping_row = row;
27977 }
27978
27979 row->clip = fr;
27980 if (expose_line (w, row, &r))
27981 mouse_face_overwritten_p = 1;
27982 row->clip = NULL;
27983 }
27984 else if (row->overlapping_p)
27985 {
27986 /* We must redraw a row overlapping the exposed area. */
27987 if (y0 < r.y
27988 ? y0 + row->phys_height > r.y
27989 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27990 {
27991 if (first_overlapping_row == NULL)
27992 first_overlapping_row = row;
27993 last_overlapping_row = row;
27994 }
27995 }
27996
27997 if (y1 >= yb)
27998 break;
27999 }
28000
28001 /* Display the mode line if there is one. */
28002 if (WINDOW_WANTS_MODELINE_P (w)
28003 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28004 row->enabled_p)
28005 && row->y < r.y + r.height)
28006 {
28007 if (expose_line (w, row, &r))
28008 mouse_face_overwritten_p = 1;
28009 }
28010
28011 if (!w->pseudo_window_p)
28012 {
28013 /* Fix the display of overlapping rows. */
28014 if (first_overlapping_row)
28015 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28016 fr);
28017
28018 /* Draw border between windows. */
28019 x_draw_vertical_border (w);
28020
28021 /* Turn the cursor on again. */
28022 if (cursor_cleared_p
28023 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28024 update_window_cursor (w, 1);
28025 }
28026 }
28027
28028 return mouse_face_overwritten_p;
28029 }
28030
28031
28032
28033 /* Redraw (parts) of all windows in the window tree rooted at W that
28034 intersect R. R contains frame pixel coordinates. Value is
28035 non-zero if the exposure overwrites mouse-face. */
28036
28037 static int
28038 expose_window_tree (struct window *w, XRectangle *r)
28039 {
28040 struct frame *f = XFRAME (w->frame);
28041 int mouse_face_overwritten_p = 0;
28042
28043 while (w && !FRAME_GARBAGED_P (f))
28044 {
28045 if (!NILP (w->hchild))
28046 mouse_face_overwritten_p
28047 |= expose_window_tree (XWINDOW (w->hchild), r);
28048 else if (!NILP (w->vchild))
28049 mouse_face_overwritten_p
28050 |= expose_window_tree (XWINDOW (w->vchild), r);
28051 else
28052 mouse_face_overwritten_p |= expose_window (w, r);
28053
28054 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28055 }
28056
28057 return mouse_face_overwritten_p;
28058 }
28059
28060
28061 /* EXPORT:
28062 Redisplay an exposed area of frame F. X and Y are the upper-left
28063 corner of the exposed rectangle. W and H are width and height of
28064 the exposed area. All are pixel values. W or H zero means redraw
28065 the entire frame. */
28066
28067 void
28068 expose_frame (struct frame *f, int x, int y, int w, int h)
28069 {
28070 XRectangle r;
28071 int mouse_face_overwritten_p = 0;
28072
28073 TRACE ((stderr, "expose_frame "));
28074
28075 /* No need to redraw if frame will be redrawn soon. */
28076 if (FRAME_GARBAGED_P (f))
28077 {
28078 TRACE ((stderr, " garbaged\n"));
28079 return;
28080 }
28081
28082 /* If basic faces haven't been realized yet, there is no point in
28083 trying to redraw anything. This can happen when we get an expose
28084 event while Emacs is starting, e.g. by moving another window. */
28085 if (FRAME_FACE_CACHE (f) == NULL
28086 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28087 {
28088 TRACE ((stderr, " no faces\n"));
28089 return;
28090 }
28091
28092 if (w == 0 || h == 0)
28093 {
28094 r.x = r.y = 0;
28095 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28096 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28097 }
28098 else
28099 {
28100 r.x = x;
28101 r.y = y;
28102 r.width = w;
28103 r.height = h;
28104 }
28105
28106 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28107 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28108
28109 if (WINDOWP (f->tool_bar_window))
28110 mouse_face_overwritten_p
28111 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28112
28113 #ifdef HAVE_X_WINDOWS
28114 #ifndef MSDOS
28115 #ifndef USE_X_TOOLKIT
28116 if (WINDOWP (f->menu_bar_window))
28117 mouse_face_overwritten_p
28118 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28119 #endif /* not USE_X_TOOLKIT */
28120 #endif
28121 #endif
28122
28123 /* Some window managers support a focus-follows-mouse style with
28124 delayed raising of frames. Imagine a partially obscured frame,
28125 and moving the mouse into partially obscured mouse-face on that
28126 frame. The visible part of the mouse-face will be highlighted,
28127 then the WM raises the obscured frame. With at least one WM, KDE
28128 2.1, Emacs is not getting any event for the raising of the frame
28129 (even tried with SubstructureRedirectMask), only Expose events.
28130 These expose events will draw text normally, i.e. not
28131 highlighted. Which means we must redo the highlight here.
28132 Subsume it under ``we love X''. --gerd 2001-08-15 */
28133 /* Included in Windows version because Windows most likely does not
28134 do the right thing if any third party tool offers
28135 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28136 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28137 {
28138 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28139 if (f == hlinfo->mouse_face_mouse_frame)
28140 {
28141 int mouse_x = hlinfo->mouse_face_mouse_x;
28142 int mouse_y = hlinfo->mouse_face_mouse_y;
28143 clear_mouse_face (hlinfo);
28144 note_mouse_highlight (f, mouse_x, mouse_y);
28145 }
28146 }
28147 }
28148
28149
28150 /* EXPORT:
28151 Determine the intersection of two rectangles R1 and R2. Return
28152 the intersection in *RESULT. Value is non-zero if RESULT is not
28153 empty. */
28154
28155 int
28156 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28157 {
28158 XRectangle *left, *right;
28159 XRectangle *upper, *lower;
28160 int intersection_p = 0;
28161
28162 /* Rearrange so that R1 is the left-most rectangle. */
28163 if (r1->x < r2->x)
28164 left = r1, right = r2;
28165 else
28166 left = r2, right = r1;
28167
28168 /* X0 of the intersection is right.x0, if this is inside R1,
28169 otherwise there is no intersection. */
28170 if (right->x <= left->x + left->width)
28171 {
28172 result->x = right->x;
28173
28174 /* The right end of the intersection is the minimum of
28175 the right ends of left and right. */
28176 result->width = (min (left->x + left->width, right->x + right->width)
28177 - result->x);
28178
28179 /* Same game for Y. */
28180 if (r1->y < r2->y)
28181 upper = r1, lower = r2;
28182 else
28183 upper = r2, lower = r1;
28184
28185 /* The upper end of the intersection is lower.y0, if this is inside
28186 of upper. Otherwise, there is no intersection. */
28187 if (lower->y <= upper->y + upper->height)
28188 {
28189 result->y = lower->y;
28190
28191 /* The lower end of the intersection is the minimum of the lower
28192 ends of upper and lower. */
28193 result->height = (min (lower->y + lower->height,
28194 upper->y + upper->height)
28195 - result->y);
28196 intersection_p = 1;
28197 }
28198 }
28199
28200 return intersection_p;
28201 }
28202
28203 #endif /* HAVE_WINDOW_SYSTEM */
28204
28205 \f
28206 /***********************************************************************
28207 Initialization
28208 ***********************************************************************/
28209
28210 void
28211 syms_of_xdisp (void)
28212 {
28213 Vwith_echo_area_save_vector = Qnil;
28214 staticpro (&Vwith_echo_area_save_vector);
28215
28216 Vmessage_stack = Qnil;
28217 staticpro (&Vmessage_stack);
28218
28219 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28220
28221 message_dolog_marker1 = Fmake_marker ();
28222 staticpro (&message_dolog_marker1);
28223 message_dolog_marker2 = Fmake_marker ();
28224 staticpro (&message_dolog_marker2);
28225 message_dolog_marker3 = Fmake_marker ();
28226 staticpro (&message_dolog_marker3);
28227
28228 #if GLYPH_DEBUG
28229 defsubr (&Sdump_frame_glyph_matrix);
28230 defsubr (&Sdump_glyph_matrix);
28231 defsubr (&Sdump_glyph_row);
28232 defsubr (&Sdump_tool_bar_row);
28233 defsubr (&Strace_redisplay);
28234 defsubr (&Strace_to_stderr);
28235 #endif
28236 #ifdef HAVE_WINDOW_SYSTEM
28237 defsubr (&Stool_bar_lines_needed);
28238 defsubr (&Slookup_image_map);
28239 #endif
28240 defsubr (&Sformat_mode_line);
28241 defsubr (&Sinvisible_p);
28242 defsubr (&Scurrent_bidi_paragraph_direction);
28243
28244 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28245 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28246 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28247 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28248 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28249 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28250 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28251 DEFSYM (Qeval, "eval");
28252 DEFSYM (QCdata, ":data");
28253 DEFSYM (Qdisplay, "display");
28254 DEFSYM (Qspace_width, "space-width");
28255 DEFSYM (Qraise, "raise");
28256 DEFSYM (Qslice, "slice");
28257 DEFSYM (Qspace, "space");
28258 DEFSYM (Qmargin, "margin");
28259 DEFSYM (Qpointer, "pointer");
28260 DEFSYM (Qleft_margin, "left-margin");
28261 DEFSYM (Qright_margin, "right-margin");
28262 DEFSYM (Qcenter, "center");
28263 DEFSYM (Qline_height, "line-height");
28264 DEFSYM (QCalign_to, ":align-to");
28265 DEFSYM (QCrelative_width, ":relative-width");
28266 DEFSYM (QCrelative_height, ":relative-height");
28267 DEFSYM (QCeval, ":eval");
28268 DEFSYM (QCpropertize, ":propertize");
28269 DEFSYM (QCfile, ":file");
28270 DEFSYM (Qfontified, "fontified");
28271 DEFSYM (Qfontification_functions, "fontification-functions");
28272 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28273 DEFSYM (Qescape_glyph, "escape-glyph");
28274 DEFSYM (Qnobreak_space, "nobreak-space");
28275 DEFSYM (Qimage, "image");
28276 DEFSYM (Qtext, "text");
28277 DEFSYM (Qboth, "both");
28278 DEFSYM (Qboth_horiz, "both-horiz");
28279 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28280 DEFSYM (QCmap, ":map");
28281 DEFSYM (QCpointer, ":pointer");
28282 DEFSYM (Qrect, "rect");
28283 DEFSYM (Qcircle, "circle");
28284 DEFSYM (Qpoly, "poly");
28285 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28286 DEFSYM (Qgrow_only, "grow-only");
28287 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28288 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28289 DEFSYM (Qposition, "position");
28290 DEFSYM (Qbuffer_position, "buffer-position");
28291 DEFSYM (Qobject, "object");
28292 DEFSYM (Qbar, "bar");
28293 DEFSYM (Qhbar, "hbar");
28294 DEFSYM (Qbox, "box");
28295 DEFSYM (Qhollow, "hollow");
28296 DEFSYM (Qhand, "hand");
28297 DEFSYM (Qarrow, "arrow");
28298 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28299
28300 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28301 Fcons (intern_c_string ("void-variable"), Qnil)),
28302 Qnil);
28303 staticpro (&list_of_error);
28304
28305 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28306 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28307 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28308 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28309
28310 echo_buffer[0] = echo_buffer[1] = Qnil;
28311 staticpro (&echo_buffer[0]);
28312 staticpro (&echo_buffer[1]);
28313
28314 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28315 staticpro (&echo_area_buffer[0]);
28316 staticpro (&echo_area_buffer[1]);
28317
28318 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28319 staticpro (&Vmessages_buffer_name);
28320
28321 mode_line_proptrans_alist = Qnil;
28322 staticpro (&mode_line_proptrans_alist);
28323 mode_line_string_list = Qnil;
28324 staticpro (&mode_line_string_list);
28325 mode_line_string_face = Qnil;
28326 staticpro (&mode_line_string_face);
28327 mode_line_string_face_prop = Qnil;
28328 staticpro (&mode_line_string_face_prop);
28329 Vmode_line_unwind_vector = Qnil;
28330 staticpro (&Vmode_line_unwind_vector);
28331
28332 help_echo_string = Qnil;
28333 staticpro (&help_echo_string);
28334 help_echo_object = Qnil;
28335 staticpro (&help_echo_object);
28336 help_echo_window = Qnil;
28337 staticpro (&help_echo_window);
28338 previous_help_echo_string = Qnil;
28339 staticpro (&previous_help_echo_string);
28340 help_echo_pos = -1;
28341
28342 DEFSYM (Qright_to_left, "right-to-left");
28343 DEFSYM (Qleft_to_right, "left-to-right");
28344
28345 #ifdef HAVE_WINDOW_SYSTEM
28346 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28347 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28348 For example, if a block cursor is over a tab, it will be drawn as
28349 wide as that tab on the display. */);
28350 x_stretch_cursor_p = 0;
28351 #endif
28352
28353 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28354 doc: /* Non-nil means highlight trailing whitespace.
28355 The face used for trailing whitespace is `trailing-whitespace'. */);
28356 Vshow_trailing_whitespace = Qnil;
28357
28358 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28359 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28360 If the value is t, Emacs highlights non-ASCII chars which have the
28361 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28362 or `escape-glyph' face respectively.
28363
28364 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28365 U+2011 (non-breaking hyphen) are affected.
28366
28367 Any other non-nil value means to display these characters as a escape
28368 glyph followed by an ordinary space or hyphen.
28369
28370 A value of nil means no special handling of these characters. */);
28371 Vnobreak_char_display = Qt;
28372
28373 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28374 doc: /* The pointer shape to show in void text areas.
28375 A value of nil means to show the text pointer. Other options are `arrow',
28376 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28377 Vvoid_text_area_pointer = Qarrow;
28378
28379 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28380 doc: /* Non-nil means don't actually do any redisplay.
28381 This is used for internal purposes. */);
28382 Vinhibit_redisplay = Qnil;
28383
28384 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28385 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28386 Vglobal_mode_string = Qnil;
28387
28388 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28389 doc: /* Marker for where to display an arrow on top of the buffer text.
28390 This must be the beginning of a line in order to work.
28391 See also `overlay-arrow-string'. */);
28392 Voverlay_arrow_position = Qnil;
28393
28394 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28395 doc: /* String to display as an arrow in non-window frames.
28396 See also `overlay-arrow-position'. */);
28397 Voverlay_arrow_string = make_pure_c_string ("=>");
28398
28399 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28400 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28401 The symbols on this list are examined during redisplay to determine
28402 where to display overlay arrows. */);
28403 Voverlay_arrow_variable_list
28404 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28405
28406 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28407 doc: /* The number of lines to try scrolling a window by when point moves out.
28408 If that fails to bring point back on frame, point is centered instead.
28409 If this is zero, point is always centered after it moves off frame.
28410 If you want scrolling to always be a line at a time, you should set
28411 `scroll-conservatively' to a large value rather than set this to 1. */);
28412
28413 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28414 doc: /* Scroll up to this many lines, to bring point back on screen.
28415 If point moves off-screen, redisplay will scroll by up to
28416 `scroll-conservatively' lines in order to bring point just barely
28417 onto the screen again. If that cannot be done, then redisplay
28418 recenters point as usual.
28419
28420 If the value is greater than 100, redisplay will never recenter point,
28421 but will always scroll just enough text to bring point into view, even
28422 if you move far away.
28423
28424 A value of zero means always recenter point if it moves off screen. */);
28425 scroll_conservatively = 0;
28426
28427 DEFVAR_INT ("scroll-margin", scroll_margin,
28428 doc: /* Number of lines of margin at the top and bottom of a window.
28429 Recenter the window whenever point gets within this many lines
28430 of the top or bottom of the window. */);
28431 scroll_margin = 0;
28432
28433 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28434 doc: /* Pixels per inch value for non-window system displays.
28435 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28436 Vdisplay_pixels_per_inch = make_float (72.0);
28437
28438 #if GLYPH_DEBUG
28439 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28440 #endif
28441
28442 DEFVAR_LISP ("truncate-partial-width-windows",
28443 Vtruncate_partial_width_windows,
28444 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28445 For an integer value, truncate lines in each window narrower than the
28446 full frame width, provided the window width is less than that integer;
28447 otherwise, respect the value of `truncate-lines'.
28448
28449 For any other non-nil value, truncate lines in all windows that do
28450 not span the full frame width.
28451
28452 A value of nil means to respect the value of `truncate-lines'.
28453
28454 If `word-wrap' is enabled, you might want to reduce this. */);
28455 Vtruncate_partial_width_windows = make_number (50);
28456
28457 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28458 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28459 Any other value means to use the appropriate face, `mode-line',
28460 `header-line', or `menu' respectively. */);
28461 mode_line_inverse_video = 1;
28462
28463 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28464 doc: /* Maximum buffer size for which line number should be displayed.
28465 If the buffer is bigger than this, the line number does not appear
28466 in the mode line. A value of nil means no limit. */);
28467 Vline_number_display_limit = Qnil;
28468
28469 DEFVAR_INT ("line-number-display-limit-width",
28470 line_number_display_limit_width,
28471 doc: /* Maximum line width (in characters) for line number display.
28472 If the average length of the lines near point is bigger than this, then the
28473 line number may be omitted from the mode line. */);
28474 line_number_display_limit_width = 200;
28475
28476 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28477 doc: /* Non-nil means highlight region even in nonselected windows. */);
28478 highlight_nonselected_windows = 0;
28479
28480 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28481 doc: /* Non-nil if more than one frame is visible on this display.
28482 Minibuffer-only frames don't count, but iconified frames do.
28483 This variable is not guaranteed to be accurate except while processing
28484 `frame-title-format' and `icon-title-format'. */);
28485
28486 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28487 doc: /* Template for displaying the title bar of visible frames.
28488 \(Assuming the window manager supports this feature.)
28489
28490 This variable has the same structure as `mode-line-format', except that
28491 the %c and %l constructs are ignored. It is used only on frames for
28492 which no explicit name has been set \(see `modify-frame-parameters'). */);
28493
28494 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28495 doc: /* Template for displaying the title bar of an iconified frame.
28496 \(Assuming the window manager supports this feature.)
28497 This variable has the same structure as `mode-line-format' (which see),
28498 and is used only on frames for which no explicit name has been set
28499 \(see `modify-frame-parameters'). */);
28500 Vicon_title_format
28501 = Vframe_title_format
28502 = pure_cons (intern_c_string ("multiple-frames"),
28503 pure_cons (make_pure_c_string ("%b"),
28504 pure_cons (pure_cons (empty_unibyte_string,
28505 pure_cons (intern_c_string ("invocation-name"),
28506 pure_cons (make_pure_c_string ("@"),
28507 pure_cons (intern_c_string ("system-name"),
28508 Qnil)))),
28509 Qnil)));
28510
28511 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28512 doc: /* Maximum number of lines to keep in the message log buffer.
28513 If nil, disable message logging. If t, log messages but don't truncate
28514 the buffer when it becomes large. */);
28515 Vmessage_log_max = make_number (100);
28516
28517 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28518 doc: /* Functions called before redisplay, if window sizes have changed.
28519 The value should be a list of functions that take one argument.
28520 Just before redisplay, for each frame, if any of its windows have changed
28521 size since the last redisplay, or have been split or deleted,
28522 all the functions in the list are called, with the frame as argument. */);
28523 Vwindow_size_change_functions = Qnil;
28524
28525 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28526 doc: /* List of functions to call before redisplaying a window with scrolling.
28527 Each function is called with two arguments, the window and its new
28528 display-start position. Note that these functions are also called by
28529 `set-window-buffer'. Also note that the value of `window-end' is not
28530 valid when these functions are called.
28531
28532 Warning: Do not use this feature to alter the way the window
28533 is scrolled. It is not designed for that, and such use probably won't
28534 work. */);
28535 Vwindow_scroll_functions = Qnil;
28536
28537 DEFVAR_LISP ("window-text-change-functions",
28538 Vwindow_text_change_functions,
28539 doc: /* Functions to call in redisplay when text in the window might change. */);
28540 Vwindow_text_change_functions = Qnil;
28541
28542 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28543 doc: /* Functions called when redisplay of a window reaches the end trigger.
28544 Each function is called with two arguments, the window and the end trigger value.
28545 See `set-window-redisplay-end-trigger'. */);
28546 Vredisplay_end_trigger_functions = Qnil;
28547
28548 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28549 doc: /* Non-nil means autoselect window with mouse pointer.
28550 If nil, do not autoselect windows.
28551 A positive number means delay autoselection by that many seconds: a
28552 window is autoselected only after the mouse has remained in that
28553 window for the duration of the delay.
28554 A negative number has a similar effect, but causes windows to be
28555 autoselected only after the mouse has stopped moving. \(Because of
28556 the way Emacs compares mouse events, you will occasionally wait twice
28557 that time before the window gets selected.\)
28558 Any other value means to autoselect window instantaneously when the
28559 mouse pointer enters it.
28560
28561 Autoselection selects the minibuffer only if it is active, and never
28562 unselects the minibuffer if it is active.
28563
28564 When customizing this variable make sure that the actual value of
28565 `focus-follows-mouse' matches the behavior of your window manager. */);
28566 Vmouse_autoselect_window = Qnil;
28567
28568 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28569 doc: /* Non-nil means automatically resize tool-bars.
28570 This dynamically changes the tool-bar's height to the minimum height
28571 that is needed to make all tool-bar items visible.
28572 If value is `grow-only', the tool-bar's height is only increased
28573 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28574 Vauto_resize_tool_bars = Qt;
28575
28576 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28577 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28578 auto_raise_tool_bar_buttons_p = 1;
28579
28580 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28581 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28582 make_cursor_line_fully_visible_p = 1;
28583
28584 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28585 doc: /* Border below tool-bar in pixels.
28586 If an integer, use it as the height of the border.
28587 If it is one of `internal-border-width' or `border-width', use the
28588 value of the corresponding frame parameter.
28589 Otherwise, no border is added below the tool-bar. */);
28590 Vtool_bar_border = Qinternal_border_width;
28591
28592 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28593 doc: /* Margin around tool-bar buttons in pixels.
28594 If an integer, use that for both horizontal and vertical margins.
28595 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28596 HORZ specifying the horizontal margin, and VERT specifying the
28597 vertical margin. */);
28598 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28599
28600 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28601 doc: /* Relief thickness of tool-bar buttons. */);
28602 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28603
28604 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28605 doc: /* Tool bar style to use.
28606 It can be one of
28607 image - show images only
28608 text - show text only
28609 both - show both, text below image
28610 both-horiz - show text to the right of the image
28611 text-image-horiz - show text to the left of the image
28612 any other - use system default or image if no system default. */);
28613 Vtool_bar_style = Qnil;
28614
28615 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28616 doc: /* Maximum number of characters a label can have to be shown.
28617 The tool bar style must also show labels for this to have any effect, see
28618 `tool-bar-style'. */);
28619 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28620
28621 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28622 doc: /* List of functions to call to fontify regions of text.
28623 Each function is called with one argument POS. Functions must
28624 fontify a region starting at POS in the current buffer, and give
28625 fontified regions the property `fontified'. */);
28626 Vfontification_functions = Qnil;
28627 Fmake_variable_buffer_local (Qfontification_functions);
28628
28629 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28630 unibyte_display_via_language_environment,
28631 doc: /* Non-nil means display unibyte text according to language environment.
28632 Specifically, this means that raw bytes in the range 160-255 decimal
28633 are displayed by converting them to the equivalent multibyte characters
28634 according to the current language environment. As a result, they are
28635 displayed according to the current fontset.
28636
28637 Note that this variable affects only how these bytes are displayed,
28638 but does not change the fact they are interpreted as raw bytes. */);
28639 unibyte_display_via_language_environment = 0;
28640
28641 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28642 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28643 If a float, it specifies a fraction of the mini-window frame's height.
28644 If an integer, it specifies a number of lines. */);
28645 Vmax_mini_window_height = make_float (0.25);
28646
28647 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28648 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28649 A value of nil means don't automatically resize mini-windows.
28650 A value of t means resize them to fit the text displayed in them.
28651 A value of `grow-only', the default, means let mini-windows grow only;
28652 they return to their normal size when the minibuffer is closed, or the
28653 echo area becomes empty. */);
28654 Vresize_mini_windows = Qgrow_only;
28655
28656 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28657 doc: /* Alist specifying how to blink the cursor off.
28658 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28659 `cursor-type' frame-parameter or variable equals ON-STATE,
28660 comparing using `equal', Emacs uses OFF-STATE to specify
28661 how to blink it off. ON-STATE and OFF-STATE are values for
28662 the `cursor-type' frame parameter.
28663
28664 If a frame's ON-STATE has no entry in this list,
28665 the frame's other specifications determine how to blink the cursor off. */);
28666 Vblink_cursor_alist = Qnil;
28667
28668 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28669 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28670 If non-nil, windows are automatically scrolled horizontally to make
28671 point visible. */);
28672 automatic_hscrolling_p = 1;
28673 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28674
28675 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28676 doc: /* How many columns away from the window edge point is allowed to get
28677 before automatic hscrolling will horizontally scroll the window. */);
28678 hscroll_margin = 5;
28679
28680 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28681 doc: /* How many columns to scroll the window when point gets too close to the edge.
28682 When point is less than `hscroll-margin' columns from the window
28683 edge, automatic hscrolling will scroll the window by the amount of columns
28684 determined by this variable. If its value is a positive integer, scroll that
28685 many columns. If it's a positive floating-point number, it specifies the
28686 fraction of the window's width to scroll. If it's nil or zero, point will be
28687 centered horizontally after the scroll. Any other value, including negative
28688 numbers, are treated as if the value were zero.
28689
28690 Automatic hscrolling always moves point outside the scroll margin, so if
28691 point was more than scroll step columns inside the margin, the window will
28692 scroll more than the value given by the scroll step.
28693
28694 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28695 and `scroll-right' overrides this variable's effect. */);
28696 Vhscroll_step = make_number (0);
28697
28698 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28699 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28700 Bind this around calls to `message' to let it take effect. */);
28701 message_truncate_lines = 0;
28702
28703 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28704 doc: /* Normal hook run to update the menu bar definitions.
28705 Redisplay runs this hook before it redisplays the menu bar.
28706 This is used to update submenus such as Buffers,
28707 whose contents depend on various data. */);
28708 Vmenu_bar_update_hook = Qnil;
28709
28710 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28711 doc: /* Frame for which we are updating a menu.
28712 The enable predicate for a menu binding should check this variable. */);
28713 Vmenu_updating_frame = Qnil;
28714
28715 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28716 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28717 inhibit_menubar_update = 0;
28718
28719 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28720 doc: /* Prefix prepended to all continuation lines at display time.
28721 The value may be a string, an image, or a stretch-glyph; it is
28722 interpreted in the same way as the value of a `display' text property.
28723
28724 This variable is overridden by any `wrap-prefix' text or overlay
28725 property.
28726
28727 To add a prefix to non-continuation lines, use `line-prefix'. */);
28728 Vwrap_prefix = Qnil;
28729 DEFSYM (Qwrap_prefix, "wrap-prefix");
28730 Fmake_variable_buffer_local (Qwrap_prefix);
28731
28732 DEFVAR_LISP ("line-prefix", Vline_prefix,
28733 doc: /* Prefix prepended to all non-continuation lines at display time.
28734 The value may be a string, an image, or a stretch-glyph; it is
28735 interpreted in the same way as the value of a `display' text property.
28736
28737 This variable is overridden by any `line-prefix' text or overlay
28738 property.
28739
28740 To add a prefix to continuation lines, use `wrap-prefix'. */);
28741 Vline_prefix = Qnil;
28742 DEFSYM (Qline_prefix, "line-prefix");
28743 Fmake_variable_buffer_local (Qline_prefix);
28744
28745 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28746 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28747 inhibit_eval_during_redisplay = 0;
28748
28749 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28750 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28751 inhibit_free_realized_faces = 0;
28752
28753 #if GLYPH_DEBUG
28754 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28755 doc: /* Inhibit try_window_id display optimization. */);
28756 inhibit_try_window_id = 0;
28757
28758 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28759 doc: /* Inhibit try_window_reusing display optimization. */);
28760 inhibit_try_window_reusing = 0;
28761
28762 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28763 doc: /* Inhibit try_cursor_movement display optimization. */);
28764 inhibit_try_cursor_movement = 0;
28765 #endif /* GLYPH_DEBUG */
28766
28767 DEFVAR_INT ("overline-margin", overline_margin,
28768 doc: /* Space between overline and text, in pixels.
28769 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28770 margin to the character height. */);
28771 overline_margin = 2;
28772
28773 DEFVAR_INT ("underline-minimum-offset",
28774 underline_minimum_offset,
28775 doc: /* Minimum distance between baseline and underline.
28776 This can improve legibility of underlined text at small font sizes,
28777 particularly when using variable `x-use-underline-position-properties'
28778 with fonts that specify an UNDERLINE_POSITION relatively close to the
28779 baseline. The default value is 1. */);
28780 underline_minimum_offset = 1;
28781
28782 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28783 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28784 This feature only works when on a window system that can change
28785 cursor shapes. */);
28786 display_hourglass_p = 1;
28787
28788 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28789 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28790 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28791
28792 hourglass_atimer = NULL;
28793 hourglass_shown_p = 0;
28794
28795 DEFSYM (Qglyphless_char, "glyphless-char");
28796 DEFSYM (Qhex_code, "hex-code");
28797 DEFSYM (Qempty_box, "empty-box");
28798 DEFSYM (Qthin_space, "thin-space");
28799 DEFSYM (Qzero_width, "zero-width");
28800
28801 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28802 /* Intern this now in case it isn't already done.
28803 Setting this variable twice is harmless.
28804 But don't staticpro it here--that is done in alloc.c. */
28805 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28806 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28807
28808 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28809 doc: /* Char-table defining glyphless characters.
28810 Each element, if non-nil, should be one of the following:
28811 an ASCII acronym string: display this string in a box
28812 `hex-code': display the hexadecimal code of a character in a box
28813 `empty-box': display as an empty box
28814 `thin-space': display as 1-pixel width space
28815 `zero-width': don't display
28816 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28817 display method for graphical terminals and text terminals respectively.
28818 GRAPHICAL and TEXT should each have one of the values listed above.
28819
28820 The char-table has one extra slot to control the display of a character for
28821 which no font is found. This slot only takes effect on graphical terminals.
28822 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28823 `thin-space'. The default is `empty-box'. */);
28824 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28825 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28826 Qempty_box);
28827 }
28828
28829
28830 /* Initialize this module when Emacs starts. */
28831
28832 void
28833 init_xdisp (void)
28834 {
28835 current_header_line_height = current_mode_line_height = -1;
28836
28837 CHARPOS (this_line_start_pos) = 0;
28838
28839 if (!noninteractive)
28840 {
28841 struct window *m = XWINDOW (minibuf_window);
28842 Lisp_Object frame = m->frame;
28843 struct frame *f = XFRAME (frame);
28844 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28845 struct window *r = XWINDOW (root);
28846 int i;
28847
28848 echo_area_window = minibuf_window;
28849
28850 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28851 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28852 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28853 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28854 XSETFASTINT (m->total_lines, 1);
28855 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28856
28857 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28858 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28859 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28860
28861 /* The default ellipsis glyphs `...'. */
28862 for (i = 0; i < 3; ++i)
28863 default_invis_vector[i] = make_number ('.');
28864 }
28865
28866 {
28867 /* Allocate the buffer for frame titles.
28868 Also used for `format-mode-line'. */
28869 int size = 100;
28870 mode_line_noprop_buf = (char *) xmalloc (size);
28871 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28872 mode_line_noprop_ptr = mode_line_noprop_buf;
28873 mode_line_target = MODE_LINE_DISPLAY;
28874 }
28875
28876 help_echo_showing_p = 0;
28877 }
28878
28879 /* Since w32 does not support atimers, it defines its own implementation of
28880 the following three functions in w32fns.c. */
28881 #ifndef WINDOWSNT
28882
28883 /* Platform-independent portion of hourglass implementation. */
28884
28885 /* Return non-zero if hourglass timer has been started or hourglass is
28886 shown. */
28887 int
28888 hourglass_started (void)
28889 {
28890 return hourglass_shown_p || hourglass_atimer != NULL;
28891 }
28892
28893 /* Cancel a currently active hourglass timer, and start a new one. */
28894 void
28895 start_hourglass (void)
28896 {
28897 #if defined (HAVE_WINDOW_SYSTEM)
28898 EMACS_TIME delay;
28899 int secs, usecs = 0;
28900
28901 cancel_hourglass ();
28902
28903 if (INTEGERP (Vhourglass_delay)
28904 && XINT (Vhourglass_delay) > 0)
28905 secs = XFASTINT (Vhourglass_delay);
28906 else if (FLOATP (Vhourglass_delay)
28907 && XFLOAT_DATA (Vhourglass_delay) > 0)
28908 {
28909 Lisp_Object tem;
28910 tem = Ftruncate (Vhourglass_delay, Qnil);
28911 secs = XFASTINT (tem);
28912 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28913 }
28914 else
28915 secs = DEFAULT_HOURGLASS_DELAY;
28916
28917 EMACS_SET_SECS_USECS (delay, secs, usecs);
28918 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28919 show_hourglass, NULL);
28920 #endif
28921 }
28922
28923
28924 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28925 shown. */
28926 void
28927 cancel_hourglass (void)
28928 {
28929 #if defined (HAVE_WINDOW_SYSTEM)
28930 if (hourglass_atimer)
28931 {
28932 cancel_atimer (hourglass_atimer);
28933 hourglass_atimer = NULL;
28934 }
28935
28936 if (hourglass_shown_p)
28937 hide_hourglass ();
28938 #endif
28939 }
28940 #endif /* ! WINDOWSNT */