merge trunk
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 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 is a space or tab
387 character. This is used to determine word wrapping. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
391
392 /* Name of the face used to highlight trailing whitespace. */
393
394 static Lisp_Object Qtrailing_whitespace;
395
396 /* Name and number of the face used to highlight escape glyphs. */
397
398 static Lisp_Object Qescape_glyph;
399
400 /* Name and number of the face used to highlight non-breaking spaces. */
401
402 static Lisp_Object Qnobreak_space;
403
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
406
407 Lisp_Object Qimage;
408
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
413
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
419
420 int noninteractive_need_newline;
421
422 /* Non-zero means print newline to message log before next message. */
423
424 static int message_log_need_newline;
425
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
432 \f
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
437
438 static struct text_pos this_line_start_pos;
439
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
442
443 static struct text_pos this_line_end_pos;
444
445 /* The vertical positions and the height of this line. */
446
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
450
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
453
454 static int this_line_start_x;
455
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
459
460 static struct text_pos this_line_min_pos;
461
462 /* Buffer that this_line_.* variables are referring to. */
463
464 static struct buffer *this_line_buffer;
465
466
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
471
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
476
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478
479 Lisp_Object Qmenu_bar_update_hook;
480
481 /* Nonzero if an overlay arrow has been displayed in this window. */
482
483 static int overlay_arrow_seen;
484
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
488
489 int buffer_shared;
490
491 /* Vector containing glyphs for an ellipsis `...'. */
492
493 static Lisp_Object default_invis_vector[3];
494
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
498
499 Lisp_Object echo_area_window;
500
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
505
506 static Lisp_Object Vmessage_stack;
507
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
510
511 static int message_enable_multibyte;
512
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
514
515 int update_mode_lines;
516
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero means a frame's cursor type has been changed. */
523
524 int cursor_type_changed;
525
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
528
529 static int line_number_displayed;
530
531 /* The name of the *Messages* buffer, a string. */
532
533 static Lisp_Object Vmessages_buffer_name;
534
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
537
538 Lisp_Object echo_area_buffer[2];
539
540 /* The buffers referenced from echo_area_buffer. */
541
542 static Lisp_Object echo_buffer[2];
543
544 /* A vector saved used in with_area_buffer to reduce consing. */
545
546 static Lisp_Object Vwith_echo_area_save_vector;
547
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
550
551 static int display_last_displayed_message_p;
552
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
555
556 static int message_buf_print;
557
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
559
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
562
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
565
566 static int message_cleared_p;
567
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
570
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
574
575 /* Ascent and height of the last line processed by move_it_to. */
576
577 static int last_max_ascent, last_height;
578
579 /* Non-zero if there's a help-echo in the echo area. */
580
581 int help_echo_showing_p;
582
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
586
587 int current_mode_line_height, current_header_line_height;
588
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
594
595 #define TEXT_PROP_DISTANCE_LIMIT 100
596
597 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 bidi_unshelve_cache (CACHE, 1); \
608 ITCOPY = ITORIG; \
609 CACHE = bidi_shelve_cache(); \
610 } while (0)
611
612 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
613 do { \
614 if (pITORIG != pITCOPY) \
615 *(pITORIG) = *(pITCOPY); \
616 bidi_unshelve_cache (CACHE, 0); \
617 CACHE = NULL; \
618 } while (0)
619
620 #if GLYPH_DEBUG
621
622 /* Non-zero means print traces of redisplay if compiled with
623 GLYPH_DEBUG != 0. */
624
625 int trace_redisplay_p;
626
627 #endif /* GLYPH_DEBUG */
628
629 #ifdef DEBUG_TRACE_MOVE
630 /* Non-zero means trace with TRACE_MOVE to stderr. */
631 int trace_move;
632
633 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
634 #else
635 #define TRACE_MOVE(x) (void) 0
636 #endif
637
638 static Lisp_Object Qauto_hscroll_mode;
639
640 /* Buffer being redisplayed -- for redisplay_window_error. */
641
642 static struct buffer *displayed_buffer;
643
644 /* Value returned from text property handlers (see below). */
645
646 enum prop_handled
647 {
648 HANDLED_NORMALLY,
649 HANDLED_RECOMPUTE_PROPS,
650 HANDLED_OVERLAY_STRING_CONSUMED,
651 HANDLED_RETURN
652 };
653
654 /* A description of text properties that redisplay is interested
655 in. */
656
657 struct props
658 {
659 /* The name of the property. */
660 Lisp_Object *name;
661
662 /* A unique index for the property. */
663 enum prop_idx idx;
664
665 /* A handler function called to set up iterator IT from the property
666 at IT's current position. Value is used to steer handle_stop. */
667 enum prop_handled (*handler) (struct it *it);
668 };
669
670 static enum prop_handled handle_face_prop (struct it *);
671 static enum prop_handled handle_invisible_prop (struct it *);
672 static enum prop_handled handle_display_prop (struct it *);
673 static enum prop_handled handle_composition_prop (struct it *);
674 static enum prop_handled handle_overlay_change (struct it *);
675 static enum prop_handled handle_fontified_prop (struct it *);
676
677 /* Properties handled by iterators. */
678
679 static struct props it_props[] =
680 {
681 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
682 /* Handle `face' before `display' because some sub-properties of
683 `display' need to know the face. */
684 {&Qface, FACE_PROP_IDX, handle_face_prop},
685 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
686 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
687 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
688 {NULL, 0, NULL}
689 };
690
691 /* Value is the position described by X. If X is a marker, value is
692 the marker_position of X. Otherwise, value is X. */
693
694 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
695
696 /* Enumeration returned by some move_it_.* functions internally. */
697
698 enum move_it_result
699 {
700 /* Not used. Undefined value. */
701 MOVE_UNDEFINED,
702
703 /* Move ended at the requested buffer position or ZV. */
704 MOVE_POS_MATCH_OR_ZV,
705
706 /* Move ended at the requested X pixel position. */
707 MOVE_X_REACHED,
708
709 /* Move within a line ended at the end of a line that must be
710 continued. */
711 MOVE_LINE_CONTINUED,
712
713 /* Move within a line ended at the end of a line that would
714 be displayed truncated. */
715 MOVE_LINE_TRUNCATED,
716
717 /* Move within a line ended at a line end. */
718 MOVE_NEWLINE_OR_CR
719 };
720
721 /* This counter is used to clear the face cache every once in a while
722 in redisplay_internal. It is incremented for each redisplay.
723 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
724 cleared. */
725
726 #define CLEAR_FACE_CACHE_COUNT 500
727 static int clear_face_cache_count;
728
729 /* Similarly for the image cache. */
730
731 #ifdef HAVE_WINDOW_SYSTEM
732 #define CLEAR_IMAGE_CACHE_COUNT 101
733 static int clear_image_cache_count;
734
735 /* Null glyph slice */
736 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
737 #endif
738
739 /* Non-zero while redisplay_internal is in progress. */
740
741 int redisplaying_p;
742
743 static Lisp_Object Qinhibit_free_realized_faces;
744
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
747
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 EMACS_INT help_echo_pos;
752
753 /* Temporary variable for XTread_socket. */
754
755 Lisp_Object previous_help_echo_string;
756
757 /* Platform-independent portion of hourglass implementation. */
758
759 /* Non-zero means an hourglass cursor is currently shown. */
760 int hourglass_shown_p;
761
762 /* If non-null, an asynchronous timer that, when it expires, displays
763 an hourglass cursor on all frames. */
764 struct atimer *hourglass_atimer;
765
766 /* Name of the face used to display glyphless characters. */
767 Lisp_Object Qglyphless_char;
768
769 /* Symbol for the purpose of Vglyphless_char_display. */
770 static Lisp_Object Qglyphless_char_display;
771
772 /* Method symbols for Vglyphless_char_display. */
773 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
774
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
777
778 /* Default number of seconds to wait before displaying an hourglass
779 cursor. */
780 #define DEFAULT_HOURGLASS_DELAY 1
781
782 \f
783 /* Function prototypes. */
784
785 static void setup_for_ellipsis (struct it *, int);
786 static void set_iterator_to_next (struct it *, int);
787 static void mark_window_display_accurate_1 (struct window *, int);
788 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
789 static int display_prop_string_p (Lisp_Object, Lisp_Object);
790 static int cursor_row_p (struct glyph_row *);
791 static int redisplay_mode_lines (Lisp_Object, int);
792 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
793
794 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
795
796 static void handle_line_prefix (struct it *);
797
798 static void pint2str (char *, int, EMACS_INT);
799 static void pint2hrstr (char *, int, EMACS_INT);
800 static struct text_pos run_window_scroll_functions (Lisp_Object,
801 struct text_pos);
802 static void reconsider_clip_changes (struct window *, struct buffer *);
803 static int text_outside_line_unchanged_p (struct window *,
804 EMACS_INT, EMACS_INT);
805 static void store_mode_line_noprop_char (char);
806 static int store_mode_line_noprop (const char *, int, int);
807 static void handle_stop (struct it *);
808 static void handle_stop_backwards (struct it *, EMACS_INT);
809 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
810 static void ensure_echo_area_buffers (void);
811 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
812 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
813 static int with_echo_area_buffer (struct window *, int,
814 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
815 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
818 static void pop_message (void);
819 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
820 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
821 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
824 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
825 static Lisp_Object unwind_redisplay (Lisp_Object);
826 static int string_char_and_length (const unsigned char *, int *);
827 static struct text_pos display_prop_end (struct it *, Lisp_Object,
828 struct text_pos);
829 static int compute_window_start_on_continuation_line (struct window *);
830 static Lisp_Object safe_eval_handler (Lisp_Object);
831 static void insert_left_trunc_glyphs (struct it *);
832 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
833 Lisp_Object);
834 static void extend_face_to_end_of_line (struct it *);
835 static int append_space_for_newline (struct it *, int);
836 static int cursor_row_fully_visible_p (struct window *, int, int);
837 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (EMACS_INT);
840 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
841 static void push_it (struct it *, struct text_pos *);
842 static void pop_it (struct it *);
843 static void sync_frame_with_window_matrix_rows (struct window *);
844 static void select_frame_for_redisplay (Lisp_Object);
845 static void redisplay_internal (void);
846 static int echo_area_display (int);
847 static void redisplay_windows (Lisp_Object);
848 static void redisplay_window (Lisp_Object, int);
849 static Lisp_Object redisplay_window_error (Lisp_Object);
850 static Lisp_Object redisplay_window_0 (Lisp_Object);
851 static Lisp_Object redisplay_window_1 (Lisp_Object);
852 static int set_cursor_from_row (struct window *, struct glyph_row *,
853 struct glyph_matrix *, EMACS_INT, EMACS_INT,
854 int, int);
855 static int update_menu_bar (struct frame *, int, int);
856 static int try_window_reusing_current_matrix (struct window *);
857 static int try_window_id (struct window *);
858 static int display_line (struct it *);
859 static int display_mode_lines (struct window *);
860 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
861 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
862 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
863 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
864 static void display_menu_bar (struct window *);
865 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
866 EMACS_INT *);
867 static int display_string (const char *, Lisp_Object, Lisp_Object,
868 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
869 static void compute_line_metrics (struct it *);
870 static void run_redisplay_end_trigger_hook (struct it *);
871 static int get_overlay_strings (struct it *, EMACS_INT);
872 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
873 static void next_overlay_string (struct it *);
874 static void reseat (struct it *, struct text_pos, int);
875 static void reseat_1 (struct it *, struct text_pos, int);
876 static void back_to_previous_visible_line_start (struct it *);
877 void reseat_at_previous_visible_line_start (struct it *);
878 static void reseat_at_next_visible_line_start (struct it *, int);
879 static int next_element_from_ellipsis (struct it *);
880 static int next_element_from_display_vector (struct it *);
881 static int next_element_from_string (struct it *);
882 static int next_element_from_c_string (struct it *);
883 static int next_element_from_buffer (struct it *);
884 static int next_element_from_composition (struct it *);
885 static int next_element_from_image (struct it *);
886 static int next_element_from_stretch (struct it *);
887 static void load_overlay_strings (struct it *, EMACS_INT);
888 static int init_from_display_pos (struct it *, struct window *,
889 struct display_pos *);
890 static void reseat_to_string (struct it *, const char *,
891 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
892 static int get_next_display_element (struct it *);
893 static enum move_it_result
894 move_it_in_display_line_to (struct it *, EMACS_INT, int,
895 enum move_operation_enum);
896 void move_it_vertically_backward (struct it *, int);
897 static void init_to_row_start (struct it *, struct window *,
898 struct glyph_row *);
899 static int init_to_row_end (struct it *, struct window *,
900 struct glyph_row *);
901 static void back_to_previous_line_start (struct it *);
902 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
903 static struct text_pos string_pos_nchars_ahead (struct text_pos,
904 Lisp_Object, EMACS_INT);
905 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
906 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
907 static EMACS_INT number_of_chars (const char *, int);
908 static void compute_stop_pos (struct it *);
909 static void compute_string_pos (struct text_pos *, struct text_pos,
910 Lisp_Object);
911 static int face_before_or_after_it_pos (struct it *, int);
912 static EMACS_INT next_overlay_change (EMACS_INT);
913 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
914 Lisp_Object, struct text_pos *, EMACS_INT, int);
915 static int handle_single_display_spec (struct it *, Lisp_Object,
916 Lisp_Object, Lisp_Object,
917 struct text_pos *, EMACS_INT, int, int);
918 static int underlying_face_id (struct it *);
919 static int in_ellipses_for_invisible_text_p (struct display_pos *,
920 struct window *);
921
922 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
923 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
924
925 #ifdef HAVE_WINDOW_SYSTEM
926
927 static void x_consider_frame_title (Lisp_Object);
928 static int tool_bar_lines_needed (struct frame *, int *);
929 static void update_tool_bar (struct frame *, int);
930 static void build_desired_tool_bar_string (struct frame *f);
931 static int redisplay_tool_bar (struct frame *);
932 static void display_tool_bar_line (struct it *, int);
933 static void notice_overwritten_cursor (struct window *,
934 enum glyph_row_area,
935 int, int, int, int);
936 static void append_stretch_glyph (struct it *, Lisp_Object,
937 int, int, int);
938
939
940 #endif /* HAVE_WINDOW_SYSTEM */
941
942 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
943 static int coords_in_mouse_face_p (struct window *, int, int);
944
945
946 \f
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
950
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
954
955 This is the height of W minus the height of a mode line, if any. */
956
957 inline int
958 window_text_bottom_y (struct window *w)
959 {
960 int height = WINDOW_TOTAL_HEIGHT (w);
961
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
965 }
966
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
970
971 inline int
972 window_box_width (struct window *w, int area)
973 {
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
976
977 if (!w->pseudo_window_p)
978 {
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
980
981 if (area == TEXT_AREA)
982 {
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
988 }
989 else if (area == LEFT_MARGIN_AREA)
990 {
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
994 }
995 else if (area == RIGHT_MARGIN_AREA)
996 {
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1000 }
1001 }
1002
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1004 }
1005
1006
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1009
1010 inline int
1011 window_box_height (struct window *w)
1012 {
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1015
1016 xassert (height >= 0);
1017
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 {
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1034 }
1035
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1037 {
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1046 }
1047
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1051 }
1052
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 inline int
1058 window_box_left_offset (struct window *w, int area)
1059 {
1060 int x;
1061
1062 if (w->pseudo_window_p)
1063 return 0;
1064
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1066
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1075 ? 0
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1080
1081 return x;
1082 }
1083
1084
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1088
1089 inline int
1090 window_box_right_offset (struct window *w, int area)
1091 {
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1093 }
1094
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1098
1099 inline int
1100 window_box_left (struct window *w, int area)
1101 {
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1104
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1107
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 inline int
1120 window_box_right (struct window *w, int area)
1121 {
1122 return window_box_left (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1131
1132 inline void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1135 {
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1143 {
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1147 }
1148 }
1149
1150
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1158
1159 static inline void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1162 {
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1167 }
1168
1169
1170 \f
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1174
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1177
1178 int
1179 line_bottom_y (struct it *it)
1180 {
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1183
1184 if (line_height == 0)
1185 {
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1189 {
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1194 }
1195 else
1196 {
1197 struct glyph_row *row = it->glyph_row;
1198
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1207 }
1208 }
1209
1210 return line_top_y + line_height;
1211 }
1212
1213
1214 /* Return 1 if position CHARPOS is visible in window W.
1215 CHARPOS < 0 means return info about WINDOW_END position.
1216 If visible, set *X and *Y to pixel coordinates of top left corner.
1217 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1218 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1219
1220 int
1221 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1222 int *rtop, int *rbot, int *rowh, int *vpos)
1223 {
1224 struct it it;
1225 void *itdata = bidi_shelve_cache ();
1226 struct text_pos top;
1227 int visible_p = 0;
1228 struct buffer *old_buffer = NULL;
1229
1230 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1231 return visible_p;
1232
1233 if (XBUFFER (w->buffer) != current_buffer)
1234 {
1235 old_buffer = current_buffer;
1236 set_buffer_internal_1 (XBUFFER (w->buffer));
1237 }
1238
1239 SET_TEXT_POS_FROM_MARKER (top, w->start);
1240
1241 /* Compute exact mode line heights. */
1242 if (WINDOW_WANTS_MODELINE_P (w))
1243 current_mode_line_height
1244 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1245 BVAR (current_buffer, mode_line_format));
1246
1247 if (WINDOW_WANTS_HEADER_LINE_P (w))
1248 current_header_line_height
1249 = display_mode_line (w, HEADER_LINE_FACE_ID,
1250 BVAR (current_buffer, header_line_format));
1251
1252 start_display (&it, w, top);
1253 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1254 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1255
1256 if (charpos >= 0
1257 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1258 && IT_CHARPOS (it) >= charpos)
1259 /* When scanning backwards under bidi iteration, move_it_to
1260 stops at or _before_ CHARPOS, because it stops at or to
1261 the _right_ of the character at CHARPOS. */
1262 || (it.bidi_p && it.bidi_it.scan_dir == -1
1263 && IT_CHARPOS (it) <= charpos)))
1264 {
1265 /* We have reached CHARPOS, or passed it. How the call to
1266 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1267 or covered by a display property, move_it_to stops at the end
1268 of the invisible text, to the right of CHARPOS. (ii) If
1269 CHARPOS is in a display vector, move_it_to stops on its last
1270 glyph. */
1271 int top_x = it.current_x;
1272 int top_y = it.current_y;
1273 enum it_method it_method = it.method;
1274 /* Calling line_bottom_y may change it.method, it.position, etc. */
1275 int bottom_y = (last_height = 0, line_bottom_y (&it));
1276 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1277
1278 if (top_y < window_top_y)
1279 visible_p = bottom_y > window_top_y;
1280 else if (top_y < it.last_visible_y)
1281 visible_p = 1;
1282 if (visible_p)
1283 {
1284 if (it_method == GET_FROM_DISPLAY_VECTOR)
1285 {
1286 /* We stopped on the last glyph of a display vector.
1287 Try and recompute. Hack alert! */
1288 if (charpos < 2 || top.charpos >= charpos)
1289 top_x = it.glyph_row->x;
1290 else
1291 {
1292 struct it it2;
1293 start_display (&it2, w, top);
1294 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1295 get_next_display_element (&it2);
1296 PRODUCE_GLYPHS (&it2);
1297 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1298 || it2.current_x > it2.last_visible_x)
1299 top_x = it.glyph_row->x;
1300 else
1301 {
1302 top_x = it2.current_x;
1303 top_y = it2.current_y;
1304 }
1305 }
1306 }
1307
1308 *x = top_x;
1309 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1310 *rtop = max (0, window_top_y - top_y);
1311 *rbot = max (0, bottom_y - it.last_visible_y);
1312 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1313 - max (top_y, window_top_y)));
1314 *vpos = it.vpos;
1315 }
1316 }
1317 else
1318 {
1319 /* We were asked to provide info about WINDOW_END. */
1320 struct it it2;
1321 void *it2data = NULL;
1322
1323 SAVE_IT (it2, it, it2data);
1324 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1325 move_it_by_lines (&it, 1);
1326 if (charpos < IT_CHARPOS (it)
1327 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1328 {
1329 visible_p = 1;
1330 RESTORE_IT (&it2, &it2, it2data);
1331 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1332 *x = it2.current_x;
1333 *y = it2.current_y + it2.max_ascent - it2.ascent;
1334 *rtop = max (0, -it2.current_y);
1335 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1336 - it.last_visible_y));
1337 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1338 it.last_visible_y)
1339 - max (it2.current_y,
1340 WINDOW_HEADER_LINE_HEIGHT (w))));
1341 *vpos = it2.vpos;
1342 }
1343 else
1344 bidi_unshelve_cache (it2data, 1);
1345 }
1346 bidi_unshelve_cache (itdata, 0);
1347
1348 if (old_buffer)
1349 set_buffer_internal_1 (old_buffer);
1350
1351 current_header_line_height = current_mode_line_height = -1;
1352
1353 if (visible_p && XFASTINT (w->hscroll) > 0)
1354 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1355
1356 #if 0
1357 /* Debugging code. */
1358 if (visible_p)
1359 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1360 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1361 else
1362 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1363 #endif
1364
1365 return visible_p;
1366 }
1367
1368
1369 /* Return the next character from STR. Return in *LEN the length of
1370 the character. This is like STRING_CHAR_AND_LENGTH but never
1371 returns an invalid character. If we find one, we return a `?', but
1372 with the length of the invalid character. */
1373
1374 static inline int
1375 string_char_and_length (const unsigned char *str, int *len)
1376 {
1377 int c;
1378
1379 c = STRING_CHAR_AND_LENGTH (str, *len);
1380 if (!CHAR_VALID_P (c))
1381 /* We may not change the length here because other places in Emacs
1382 don't use this function, i.e. they silently accept invalid
1383 characters. */
1384 c = '?';
1385
1386 return c;
1387 }
1388
1389
1390
1391 /* Given a position POS containing a valid character and byte position
1392 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1393
1394 static struct text_pos
1395 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1396 {
1397 xassert (STRINGP (string) && nchars >= 0);
1398
1399 if (STRING_MULTIBYTE (string))
1400 {
1401 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1402 int len;
1403
1404 while (nchars--)
1405 {
1406 string_char_and_length (p, &len);
1407 p += len;
1408 CHARPOS (pos) += 1;
1409 BYTEPOS (pos) += len;
1410 }
1411 }
1412 else
1413 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1414
1415 return pos;
1416 }
1417
1418
1419 /* Value is the text position, i.e. character and byte position,
1420 for character position CHARPOS in STRING. */
1421
1422 static inline struct text_pos
1423 string_pos (EMACS_INT charpos, Lisp_Object string)
1424 {
1425 struct text_pos pos;
1426 xassert (STRINGP (string));
1427 xassert (charpos >= 0);
1428 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1429 return pos;
1430 }
1431
1432
1433 /* Value is a text position, i.e. character and byte position, for
1434 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1435 means recognize multibyte characters. */
1436
1437 static struct text_pos
1438 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1439 {
1440 struct text_pos pos;
1441
1442 xassert (s != NULL);
1443 xassert (charpos >= 0);
1444
1445 if (multibyte_p)
1446 {
1447 int len;
1448
1449 SET_TEXT_POS (pos, 0, 0);
1450 while (charpos--)
1451 {
1452 string_char_and_length ((const unsigned char *) s, &len);
1453 s += len;
1454 CHARPOS (pos) += 1;
1455 BYTEPOS (pos) += len;
1456 }
1457 }
1458 else
1459 SET_TEXT_POS (pos, charpos, charpos);
1460
1461 return pos;
1462 }
1463
1464
1465 /* Value is the number of characters in C string S. MULTIBYTE_P
1466 non-zero means recognize multibyte characters. */
1467
1468 static EMACS_INT
1469 number_of_chars (const char *s, int multibyte_p)
1470 {
1471 EMACS_INT nchars;
1472
1473 if (multibyte_p)
1474 {
1475 EMACS_INT rest = strlen (s);
1476 int len;
1477 const unsigned char *p = (const unsigned char *) s;
1478
1479 for (nchars = 0; rest > 0; ++nchars)
1480 {
1481 string_char_and_length (p, &len);
1482 rest -= len, p += len;
1483 }
1484 }
1485 else
1486 nchars = strlen (s);
1487
1488 return nchars;
1489 }
1490
1491
1492 /* Compute byte position NEWPOS->bytepos corresponding to
1493 NEWPOS->charpos. POS is a known position in string STRING.
1494 NEWPOS->charpos must be >= POS.charpos. */
1495
1496 static void
1497 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1498 {
1499 xassert (STRINGP (string));
1500 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1501
1502 if (STRING_MULTIBYTE (string))
1503 *newpos = string_pos_nchars_ahead (pos, string,
1504 CHARPOS (*newpos) - CHARPOS (pos));
1505 else
1506 BYTEPOS (*newpos) = CHARPOS (*newpos);
1507 }
1508
1509 /* EXPORT:
1510 Return an estimation of the pixel height of mode or header lines on
1511 frame F. FACE_ID specifies what line's height to estimate. */
1512
1513 int
1514 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1515 {
1516 #ifdef HAVE_WINDOW_SYSTEM
1517 if (FRAME_WINDOW_P (f))
1518 {
1519 int height = FONT_HEIGHT (FRAME_FONT (f));
1520
1521 /* This function is called so early when Emacs starts that the face
1522 cache and mode line face are not yet initialized. */
1523 if (FRAME_FACE_CACHE (f))
1524 {
1525 struct face *face = FACE_FROM_ID (f, face_id);
1526 if (face)
1527 {
1528 if (face->font)
1529 height = FONT_HEIGHT (face->font);
1530 if (face->box_line_width > 0)
1531 height += 2 * face->box_line_width;
1532 }
1533 }
1534
1535 return height;
1536 }
1537 #endif
1538
1539 return 1;
1540 }
1541
1542 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1543 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1544 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1545 not force the value into range. */
1546
1547 void
1548 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1549 int *x, int *y, NativeRectangle *bounds, int noclip)
1550 {
1551
1552 #ifdef HAVE_WINDOW_SYSTEM
1553 if (FRAME_WINDOW_P (f))
1554 {
1555 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1556 even for negative values. */
1557 if (pix_x < 0)
1558 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1559 if (pix_y < 0)
1560 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1561
1562 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1563 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1564
1565 if (bounds)
1566 STORE_NATIVE_RECT (*bounds,
1567 FRAME_COL_TO_PIXEL_X (f, pix_x),
1568 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1569 FRAME_COLUMN_WIDTH (f) - 1,
1570 FRAME_LINE_HEIGHT (f) - 1);
1571
1572 if (!noclip)
1573 {
1574 if (pix_x < 0)
1575 pix_x = 0;
1576 else if (pix_x > FRAME_TOTAL_COLS (f))
1577 pix_x = FRAME_TOTAL_COLS (f);
1578
1579 if (pix_y < 0)
1580 pix_y = 0;
1581 else if (pix_y > FRAME_LINES (f))
1582 pix_y = FRAME_LINES (f);
1583 }
1584 }
1585 #endif
1586
1587 *x = pix_x;
1588 *y = pix_y;
1589 }
1590
1591
1592 /* Find the glyph under window-relative coordinates X/Y in window W.
1593 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1594 strings. Return in *HPOS and *VPOS the row and column number of
1595 the glyph found. Return in *AREA the glyph area containing X.
1596 Value is a pointer to the glyph found or null if X/Y is not on
1597 text, or we can't tell because W's current matrix is not up to
1598 date. */
1599
1600 static
1601 struct glyph *
1602 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1603 int *dx, int *dy, int *area)
1604 {
1605 struct glyph *glyph, *end;
1606 struct glyph_row *row = NULL;
1607 int x0, i;
1608
1609 /* Find row containing Y. Give up if some row is not enabled. */
1610 for (i = 0; i < w->current_matrix->nrows; ++i)
1611 {
1612 row = MATRIX_ROW (w->current_matrix, i);
1613 if (!row->enabled_p)
1614 return NULL;
1615 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1616 break;
1617 }
1618
1619 *vpos = i;
1620 *hpos = 0;
1621
1622 /* Give up if Y is not in the window. */
1623 if (i == w->current_matrix->nrows)
1624 return NULL;
1625
1626 /* Get the glyph area containing X. */
1627 if (w->pseudo_window_p)
1628 {
1629 *area = TEXT_AREA;
1630 x0 = 0;
1631 }
1632 else
1633 {
1634 if (x < window_box_left_offset (w, TEXT_AREA))
1635 {
1636 *area = LEFT_MARGIN_AREA;
1637 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1638 }
1639 else if (x < window_box_right_offset (w, TEXT_AREA))
1640 {
1641 *area = TEXT_AREA;
1642 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1643 }
1644 else
1645 {
1646 *area = RIGHT_MARGIN_AREA;
1647 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1648 }
1649 }
1650
1651 /* Find glyph containing X. */
1652 glyph = row->glyphs[*area];
1653 end = glyph + row->used[*area];
1654 x -= x0;
1655 while (glyph < end && x >= glyph->pixel_width)
1656 {
1657 x -= glyph->pixel_width;
1658 ++glyph;
1659 }
1660
1661 if (glyph == end)
1662 return NULL;
1663
1664 if (dx)
1665 {
1666 *dx = x;
1667 *dy = y - (row->y + row->ascent - glyph->ascent);
1668 }
1669
1670 *hpos = glyph - row->glyphs[*area];
1671 return glyph;
1672 }
1673
1674 /* Convert frame-relative x/y to coordinates relative to window W.
1675 Takes pseudo-windows into account. */
1676
1677 static void
1678 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1679 {
1680 if (w->pseudo_window_p)
1681 {
1682 /* A pseudo-window is always full-width, and starts at the
1683 left edge of the frame, plus a frame border. */
1684 struct frame *f = XFRAME (w->frame);
1685 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1686 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1687 }
1688 else
1689 {
1690 *x -= WINDOW_LEFT_EDGE_X (w);
1691 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1692 }
1693 }
1694
1695 #ifdef HAVE_WINDOW_SYSTEM
1696
1697 /* EXPORT:
1698 Return in RECTS[] at most N clipping rectangles for glyph string S.
1699 Return the number of stored rectangles. */
1700
1701 int
1702 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1703 {
1704 XRectangle r;
1705
1706 if (n <= 0)
1707 return 0;
1708
1709 if (s->row->full_width_p)
1710 {
1711 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1712 r.x = WINDOW_LEFT_EDGE_X (s->w);
1713 r.width = WINDOW_TOTAL_WIDTH (s->w);
1714
1715 /* Unless displaying a mode or menu bar line, which are always
1716 fully visible, clip to the visible part of the row. */
1717 if (s->w->pseudo_window_p)
1718 r.height = s->row->visible_height;
1719 else
1720 r.height = s->height;
1721 }
1722 else
1723 {
1724 /* This is a text line that may be partially visible. */
1725 r.x = window_box_left (s->w, s->area);
1726 r.width = window_box_width (s->w, s->area);
1727 r.height = s->row->visible_height;
1728 }
1729
1730 if (s->clip_head)
1731 if (r.x < s->clip_head->x)
1732 {
1733 if (r.width >= s->clip_head->x - r.x)
1734 r.width -= s->clip_head->x - r.x;
1735 else
1736 r.width = 0;
1737 r.x = s->clip_head->x;
1738 }
1739 if (s->clip_tail)
1740 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1741 {
1742 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1743 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1744 else
1745 r.width = 0;
1746 }
1747
1748 /* If S draws overlapping rows, it's sufficient to use the top and
1749 bottom of the window for clipping because this glyph string
1750 intentionally draws over other lines. */
1751 if (s->for_overlaps)
1752 {
1753 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1754 r.height = window_text_bottom_y (s->w) - r.y;
1755
1756 /* Alas, the above simple strategy does not work for the
1757 environments with anti-aliased text: if the same text is
1758 drawn onto the same place multiple times, it gets thicker.
1759 If the overlap we are processing is for the erased cursor, we
1760 take the intersection with the rectagle of the cursor. */
1761 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1762 {
1763 XRectangle rc, r_save = r;
1764
1765 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1766 rc.y = s->w->phys_cursor.y;
1767 rc.width = s->w->phys_cursor_width;
1768 rc.height = s->w->phys_cursor_height;
1769
1770 x_intersect_rectangles (&r_save, &rc, &r);
1771 }
1772 }
1773 else
1774 {
1775 /* Don't use S->y for clipping because it doesn't take partially
1776 visible lines into account. For example, it can be negative for
1777 partially visible lines at the top of a window. */
1778 if (!s->row->full_width_p
1779 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1780 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1781 else
1782 r.y = max (0, s->row->y);
1783 }
1784
1785 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1786
1787 /* If drawing the cursor, don't let glyph draw outside its
1788 advertised boundaries. Cleartype does this under some circumstances. */
1789 if (s->hl == DRAW_CURSOR)
1790 {
1791 struct glyph *glyph = s->first_glyph;
1792 int height, max_y;
1793
1794 if (s->x > r.x)
1795 {
1796 r.width -= s->x - r.x;
1797 r.x = s->x;
1798 }
1799 r.width = min (r.width, glyph->pixel_width);
1800
1801 /* If r.y is below window bottom, ensure that we still see a cursor. */
1802 height = min (glyph->ascent + glyph->descent,
1803 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1804 max_y = window_text_bottom_y (s->w) - height;
1805 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1806 if (s->ybase - glyph->ascent > max_y)
1807 {
1808 r.y = max_y;
1809 r.height = height;
1810 }
1811 else
1812 {
1813 /* Don't draw cursor glyph taller than our actual glyph. */
1814 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1815 if (height < r.height)
1816 {
1817 max_y = r.y + r.height;
1818 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1819 r.height = min (max_y - r.y, height);
1820 }
1821 }
1822 }
1823
1824 if (s->row->clip)
1825 {
1826 XRectangle r_save = r;
1827
1828 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1829 r.width = 0;
1830 }
1831
1832 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1833 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1834 {
1835 #ifdef CONVERT_FROM_XRECT
1836 CONVERT_FROM_XRECT (r, *rects);
1837 #else
1838 *rects = r;
1839 #endif
1840 return 1;
1841 }
1842 else
1843 {
1844 /* If we are processing overlapping and allowed to return
1845 multiple clipping rectangles, we exclude the row of the glyph
1846 string from the clipping rectangle. This is to avoid drawing
1847 the same text on the environment with anti-aliasing. */
1848 #ifdef CONVERT_FROM_XRECT
1849 XRectangle rs[2];
1850 #else
1851 XRectangle *rs = rects;
1852 #endif
1853 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1854
1855 if (s->for_overlaps & OVERLAPS_PRED)
1856 {
1857 rs[i] = r;
1858 if (r.y + r.height > row_y)
1859 {
1860 if (r.y < row_y)
1861 rs[i].height = row_y - r.y;
1862 else
1863 rs[i].height = 0;
1864 }
1865 i++;
1866 }
1867 if (s->for_overlaps & OVERLAPS_SUCC)
1868 {
1869 rs[i] = r;
1870 if (r.y < row_y + s->row->visible_height)
1871 {
1872 if (r.y + r.height > row_y + s->row->visible_height)
1873 {
1874 rs[i].y = row_y + s->row->visible_height;
1875 rs[i].height = r.y + r.height - rs[i].y;
1876 }
1877 else
1878 rs[i].height = 0;
1879 }
1880 i++;
1881 }
1882
1883 n = i;
1884 #ifdef CONVERT_FROM_XRECT
1885 for (i = 0; i < n; i++)
1886 CONVERT_FROM_XRECT (rs[i], rects[i]);
1887 #endif
1888 return n;
1889 }
1890 }
1891
1892 /* EXPORT:
1893 Return in *NR the clipping rectangle for glyph string S. */
1894
1895 void
1896 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1897 {
1898 get_glyph_string_clip_rects (s, nr, 1);
1899 }
1900
1901
1902 /* EXPORT:
1903 Return the position and height of the phys cursor in window W.
1904 Set w->phys_cursor_width to width of phys cursor.
1905 */
1906
1907 void
1908 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1909 struct glyph *glyph, int *xp, int *yp, int *heightp)
1910 {
1911 struct frame *f = XFRAME (WINDOW_FRAME (w));
1912 int x, y, wd, h, h0, y0;
1913
1914 /* Compute the width of the rectangle to draw. If on a stretch
1915 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1916 rectangle as wide as the glyph, but use a canonical character
1917 width instead. */
1918 wd = glyph->pixel_width - 1;
1919 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1920 wd++; /* Why? */
1921 #endif
1922
1923 x = w->phys_cursor.x;
1924 if (x < 0)
1925 {
1926 wd += x;
1927 x = 0;
1928 }
1929
1930 if (glyph->type == STRETCH_GLYPH
1931 && !x_stretch_cursor_p)
1932 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1933 w->phys_cursor_width = wd;
1934
1935 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1936
1937 /* If y is below window bottom, ensure that we still see a cursor. */
1938 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1939
1940 h = max (h0, glyph->ascent + glyph->descent);
1941 h0 = min (h0, glyph->ascent + glyph->descent);
1942
1943 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1944 if (y < y0)
1945 {
1946 h = max (h - (y0 - y) + 1, h0);
1947 y = y0 - 1;
1948 }
1949 else
1950 {
1951 y0 = window_text_bottom_y (w) - h0;
1952 if (y > y0)
1953 {
1954 h += y - y0;
1955 y = y0;
1956 }
1957 }
1958
1959 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1960 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1961 *heightp = h;
1962 }
1963
1964 /*
1965 * Remember which glyph the mouse is over.
1966 */
1967
1968 void
1969 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1970 {
1971 Lisp_Object window;
1972 struct window *w;
1973 struct glyph_row *r, *gr, *end_row;
1974 enum window_part part;
1975 enum glyph_row_area area;
1976 int x, y, width, height;
1977
1978 /* Try to determine frame pixel position and size of the glyph under
1979 frame pixel coordinates X/Y on frame F. */
1980
1981 if (!f->glyphs_initialized_p
1982 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1983 NILP (window)))
1984 {
1985 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1986 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1987 goto virtual_glyph;
1988 }
1989
1990 w = XWINDOW (window);
1991 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1992 height = WINDOW_FRAME_LINE_HEIGHT (w);
1993
1994 x = window_relative_x_coord (w, part, gx);
1995 y = gy - WINDOW_TOP_EDGE_Y (w);
1996
1997 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1998 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1999
2000 if (w->pseudo_window_p)
2001 {
2002 area = TEXT_AREA;
2003 part = ON_MODE_LINE; /* Don't adjust margin. */
2004 goto text_glyph;
2005 }
2006
2007 switch (part)
2008 {
2009 case ON_LEFT_MARGIN:
2010 area = LEFT_MARGIN_AREA;
2011 goto text_glyph;
2012
2013 case ON_RIGHT_MARGIN:
2014 area = RIGHT_MARGIN_AREA;
2015 goto text_glyph;
2016
2017 case ON_HEADER_LINE:
2018 case ON_MODE_LINE:
2019 gr = (part == ON_HEADER_LINE
2020 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2021 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2022 gy = gr->y;
2023 area = TEXT_AREA;
2024 goto text_glyph_row_found;
2025
2026 case ON_TEXT:
2027 area = TEXT_AREA;
2028
2029 text_glyph:
2030 gr = 0; gy = 0;
2031 for (; r <= end_row && r->enabled_p; ++r)
2032 if (r->y + r->height > y)
2033 {
2034 gr = r; gy = r->y;
2035 break;
2036 }
2037
2038 text_glyph_row_found:
2039 if (gr && gy <= y)
2040 {
2041 struct glyph *g = gr->glyphs[area];
2042 struct glyph *end = g + gr->used[area];
2043
2044 height = gr->height;
2045 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2046 if (gx + g->pixel_width > x)
2047 break;
2048
2049 if (g < end)
2050 {
2051 if (g->type == IMAGE_GLYPH)
2052 {
2053 /* Don't remember when mouse is over image, as
2054 image may have hot-spots. */
2055 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2056 return;
2057 }
2058 width = g->pixel_width;
2059 }
2060 else
2061 {
2062 /* Use nominal char spacing at end of line. */
2063 x -= gx;
2064 gx += (x / width) * width;
2065 }
2066
2067 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2068 gx += window_box_left_offset (w, area);
2069 }
2070 else
2071 {
2072 /* Use nominal line height at end of window. */
2073 gx = (x / width) * width;
2074 y -= gy;
2075 gy += (y / height) * height;
2076 }
2077 break;
2078
2079 case ON_LEFT_FRINGE:
2080 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2081 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2082 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2083 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2084 goto row_glyph;
2085
2086 case ON_RIGHT_FRINGE:
2087 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2088 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2089 : window_box_right_offset (w, TEXT_AREA));
2090 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2091 goto row_glyph;
2092
2093 case ON_SCROLL_BAR:
2094 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2095 ? 0
2096 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2097 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2098 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2099 : 0)));
2100 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2101
2102 row_glyph:
2103 gr = 0, gy = 0;
2104 for (; r <= end_row && r->enabled_p; ++r)
2105 if (r->y + r->height > y)
2106 {
2107 gr = r; gy = r->y;
2108 break;
2109 }
2110
2111 if (gr && gy <= y)
2112 height = gr->height;
2113 else
2114 {
2115 /* Use nominal line height at end of window. */
2116 y -= gy;
2117 gy += (y / height) * height;
2118 }
2119 break;
2120
2121 default:
2122 ;
2123 virtual_glyph:
2124 /* If there is no glyph under the mouse, then we divide the screen
2125 into a grid of the smallest glyph in the frame, and use that
2126 as our "glyph". */
2127
2128 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2129 round down even for negative values. */
2130 if (gx < 0)
2131 gx -= width - 1;
2132 if (gy < 0)
2133 gy -= height - 1;
2134
2135 gx = (gx / width) * width;
2136 gy = (gy / height) * height;
2137
2138 goto store_rect;
2139 }
2140
2141 gx += WINDOW_LEFT_EDGE_X (w);
2142 gy += WINDOW_TOP_EDGE_Y (w);
2143
2144 store_rect:
2145 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2146
2147 /* Visible feedback for debugging. */
2148 #if 0
2149 #if HAVE_X_WINDOWS
2150 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2151 f->output_data.x->normal_gc,
2152 gx, gy, width, height);
2153 #endif
2154 #endif
2155 }
2156
2157
2158 #endif /* HAVE_WINDOW_SYSTEM */
2159
2160 \f
2161 /***********************************************************************
2162 Lisp form evaluation
2163 ***********************************************************************/
2164
2165 /* Error handler for safe_eval and safe_call. */
2166
2167 static Lisp_Object
2168 safe_eval_handler (Lisp_Object arg)
2169 {
2170 add_to_log ("Error during redisplay: %S", arg, Qnil);
2171 return Qnil;
2172 }
2173
2174
2175 /* Evaluate SEXPR and return the result, or nil if something went
2176 wrong. Prevent redisplay during the evaluation. */
2177
2178 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2179 Return the result, or nil if something went wrong. Prevent
2180 redisplay during the evaluation. */
2181
2182 Lisp_Object
2183 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2184 {
2185 Lisp_Object val;
2186
2187 if (inhibit_eval_during_redisplay)
2188 val = Qnil;
2189 else
2190 {
2191 int count = SPECPDL_INDEX ();
2192 struct gcpro gcpro1;
2193
2194 GCPRO1 (args[0]);
2195 gcpro1.nvars = nargs;
2196 specbind (Qinhibit_redisplay, Qt);
2197 /* Use Qt to ensure debugger does not run,
2198 so there is no possibility of wanting to redisplay. */
2199 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2200 safe_eval_handler);
2201 UNGCPRO;
2202 val = unbind_to (count, val);
2203 }
2204
2205 return val;
2206 }
2207
2208
2209 /* Call function FN with one argument ARG.
2210 Return the result, or nil if something went wrong. */
2211
2212 Lisp_Object
2213 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2214 {
2215 Lisp_Object args[2];
2216 args[0] = fn;
2217 args[1] = arg;
2218 return safe_call (2, args);
2219 }
2220
2221 static Lisp_Object Qeval;
2222
2223 Lisp_Object
2224 safe_eval (Lisp_Object sexpr)
2225 {
2226 return safe_call1 (Qeval, sexpr);
2227 }
2228
2229 /* Call function FN with one argument ARG.
2230 Return the result, or nil if something went wrong. */
2231
2232 Lisp_Object
2233 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2234 {
2235 Lisp_Object args[3];
2236 args[0] = fn;
2237 args[1] = arg1;
2238 args[2] = arg2;
2239 return safe_call (3, args);
2240 }
2241
2242
2243 \f
2244 /***********************************************************************
2245 Debugging
2246 ***********************************************************************/
2247
2248 #if 0
2249
2250 /* Define CHECK_IT to perform sanity checks on iterators.
2251 This is for debugging. It is too slow to do unconditionally. */
2252
2253 static void
2254 check_it (struct it *it)
2255 {
2256 if (it->method == GET_FROM_STRING)
2257 {
2258 xassert (STRINGP (it->string));
2259 xassert (IT_STRING_CHARPOS (*it) >= 0);
2260 }
2261 else
2262 {
2263 xassert (IT_STRING_CHARPOS (*it) < 0);
2264 if (it->method == GET_FROM_BUFFER)
2265 {
2266 /* Check that character and byte positions agree. */
2267 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2268 }
2269 }
2270
2271 if (it->dpvec)
2272 xassert (it->current.dpvec_index >= 0);
2273 else
2274 xassert (it->current.dpvec_index < 0);
2275 }
2276
2277 #define CHECK_IT(IT) check_it ((IT))
2278
2279 #else /* not 0 */
2280
2281 #define CHECK_IT(IT) (void) 0
2282
2283 #endif /* not 0 */
2284
2285
2286 #if GLYPH_DEBUG && XASSERTS
2287
2288 /* Check that the window end of window W is what we expect it
2289 to be---the last row in the current matrix displaying text. */
2290
2291 static void
2292 check_window_end (struct window *w)
2293 {
2294 if (!MINI_WINDOW_P (w)
2295 && !NILP (w->window_end_valid))
2296 {
2297 struct glyph_row *row;
2298 xassert ((row = MATRIX_ROW (w->current_matrix,
2299 XFASTINT (w->window_end_vpos)),
2300 !row->enabled_p
2301 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2302 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2303 }
2304 }
2305
2306 #define CHECK_WINDOW_END(W) check_window_end ((W))
2307
2308 #else
2309
2310 #define CHECK_WINDOW_END(W) (void) 0
2311
2312 #endif
2313
2314
2315 \f
2316 /***********************************************************************
2317 Iterator initialization
2318 ***********************************************************************/
2319
2320 /* Initialize IT for displaying current_buffer in window W, starting
2321 at character position CHARPOS. CHARPOS < 0 means that no buffer
2322 position is specified which is useful when the iterator is assigned
2323 a position later. BYTEPOS is the byte position corresponding to
2324 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2325
2326 If ROW is not null, calls to produce_glyphs with IT as parameter
2327 will produce glyphs in that row.
2328
2329 BASE_FACE_ID is the id of a base face to use. It must be one of
2330 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2331 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2332 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2333
2334 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2335 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2336 will be initialized to use the corresponding mode line glyph row of
2337 the desired matrix of W. */
2338
2339 void
2340 init_iterator (struct it *it, struct window *w,
2341 EMACS_INT charpos, EMACS_INT bytepos,
2342 struct glyph_row *row, enum face_id base_face_id)
2343 {
2344 int highlight_region_p;
2345 enum face_id remapped_base_face_id = base_face_id;
2346
2347 /* Some precondition checks. */
2348 xassert (w != NULL && it != NULL);
2349 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2350 && charpos <= ZV));
2351
2352 /* If face attributes have been changed since the last redisplay,
2353 free realized faces now because they depend on face definitions
2354 that might have changed. Don't free faces while there might be
2355 desired matrices pending which reference these faces. */
2356 if (face_change_count && !inhibit_free_realized_faces)
2357 {
2358 face_change_count = 0;
2359 free_all_realized_faces (Qnil);
2360 }
2361
2362 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2363 if (! NILP (Vface_remapping_alist))
2364 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2365
2366 /* Use one of the mode line rows of W's desired matrix if
2367 appropriate. */
2368 if (row == NULL)
2369 {
2370 if (base_face_id == MODE_LINE_FACE_ID
2371 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2372 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2373 else if (base_face_id == HEADER_LINE_FACE_ID)
2374 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2375 }
2376
2377 /* Clear IT. */
2378 memset (it, 0, sizeof *it);
2379 it->current.overlay_string_index = -1;
2380 it->current.dpvec_index = -1;
2381 it->base_face_id = remapped_base_face_id;
2382 it->string = Qnil;
2383 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2384 it->paragraph_embedding = L2R;
2385 it->bidi_it.string.lstring = Qnil;
2386 it->bidi_it.string.s = NULL;
2387 it->bidi_it.string.bufpos = 0;
2388
2389 /* The window in which we iterate over current_buffer: */
2390 XSETWINDOW (it->window, w);
2391 it->w = w;
2392 it->f = XFRAME (w->frame);
2393
2394 it->cmp_it.id = -1;
2395
2396 /* Extra space between lines (on window systems only). */
2397 if (base_face_id == DEFAULT_FACE_ID
2398 && FRAME_WINDOW_P (it->f))
2399 {
2400 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2401 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2402 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2403 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2404 * FRAME_LINE_HEIGHT (it->f));
2405 else if (it->f->extra_line_spacing > 0)
2406 it->extra_line_spacing = it->f->extra_line_spacing;
2407 it->max_extra_line_spacing = 0;
2408 }
2409
2410 /* If realized faces have been removed, e.g. because of face
2411 attribute changes of named faces, recompute them. When running
2412 in batch mode, the face cache of the initial frame is null. If
2413 we happen to get called, make a dummy face cache. */
2414 if (FRAME_FACE_CACHE (it->f) == NULL)
2415 init_frame_faces (it->f);
2416 if (FRAME_FACE_CACHE (it->f)->used == 0)
2417 recompute_basic_faces (it->f);
2418
2419 /* Current value of the `slice', `space-width', and 'height' properties. */
2420 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2421 it->space_width = Qnil;
2422 it->font_height = Qnil;
2423 it->override_ascent = -1;
2424
2425 /* Are control characters displayed as `^C'? */
2426 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2427
2428 /* -1 means everything between a CR and the following line end
2429 is invisible. >0 means lines indented more than this value are
2430 invisible. */
2431 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2432 ? XINT (BVAR (current_buffer, selective_display))
2433 : (!NILP (BVAR (current_buffer, selective_display))
2434 ? -1 : 0));
2435 it->selective_display_ellipsis_p
2436 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2437
2438 /* Display table to use. */
2439 it->dp = window_display_table (w);
2440
2441 /* Are multibyte characters enabled in current_buffer? */
2442 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2443
2444 /* Non-zero if we should highlight the region. */
2445 highlight_region_p
2446 = (!NILP (Vtransient_mark_mode)
2447 && !NILP (BVAR (current_buffer, mark_active))
2448 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2449
2450 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2451 start and end of a visible region in window IT->w. Set both to
2452 -1 to indicate no region. */
2453 if (highlight_region_p
2454 /* Maybe highlight only in selected window. */
2455 && (/* Either show region everywhere. */
2456 highlight_nonselected_windows
2457 /* Or show region in the selected window. */
2458 || w == XWINDOW (selected_window)
2459 /* Or show the region if we are in the mini-buffer and W is
2460 the window the mini-buffer refers to. */
2461 || (MINI_WINDOW_P (XWINDOW (selected_window))
2462 && WINDOWP (minibuf_selected_window)
2463 && w == XWINDOW (minibuf_selected_window))))
2464 {
2465 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2466 it->region_beg_charpos = min (PT, markpos);
2467 it->region_end_charpos = max (PT, markpos);
2468 }
2469 else
2470 it->region_beg_charpos = it->region_end_charpos = -1;
2471
2472 /* Get the position at which the redisplay_end_trigger hook should
2473 be run, if it is to be run at all. */
2474 if (MARKERP (w->redisplay_end_trigger)
2475 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2476 it->redisplay_end_trigger_charpos
2477 = marker_position (w->redisplay_end_trigger);
2478 else if (INTEGERP (w->redisplay_end_trigger))
2479 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2480
2481 /* Correct bogus values of tab_width. */
2482 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2483 if (it->tab_width <= 0 || it->tab_width > 1000)
2484 it->tab_width = 8;
2485
2486 /* Are lines in the display truncated? */
2487 if (base_face_id != DEFAULT_FACE_ID
2488 || XINT (it->w->hscroll)
2489 || (! WINDOW_FULL_WIDTH_P (it->w)
2490 && ((!NILP (Vtruncate_partial_width_windows)
2491 && !INTEGERP (Vtruncate_partial_width_windows))
2492 || (INTEGERP (Vtruncate_partial_width_windows)
2493 && (WINDOW_TOTAL_COLS (it->w)
2494 < XINT (Vtruncate_partial_width_windows))))))
2495 it->line_wrap = TRUNCATE;
2496 else if (NILP (BVAR (current_buffer, truncate_lines)))
2497 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2498 ? WINDOW_WRAP : WORD_WRAP;
2499 else
2500 it->line_wrap = TRUNCATE;
2501
2502 /* Get dimensions of truncation and continuation glyphs. These are
2503 displayed as fringe bitmaps under X, so we don't need them for such
2504 frames. */
2505 if (!FRAME_WINDOW_P (it->f))
2506 {
2507 if (it->line_wrap == TRUNCATE)
2508 {
2509 /* We will need the truncation glyph. */
2510 xassert (it->glyph_row == NULL);
2511 produce_special_glyphs (it, IT_TRUNCATION);
2512 it->truncation_pixel_width = it->pixel_width;
2513 }
2514 else
2515 {
2516 /* We will need the continuation glyph. */
2517 xassert (it->glyph_row == NULL);
2518 produce_special_glyphs (it, IT_CONTINUATION);
2519 it->continuation_pixel_width = it->pixel_width;
2520 }
2521
2522 /* Reset these values to zero because the produce_special_glyphs
2523 above has changed them. */
2524 it->pixel_width = it->ascent = it->descent = 0;
2525 it->phys_ascent = it->phys_descent = 0;
2526 }
2527
2528 /* Set this after getting the dimensions of truncation and
2529 continuation glyphs, so that we don't produce glyphs when calling
2530 produce_special_glyphs, above. */
2531 it->glyph_row = row;
2532 it->area = TEXT_AREA;
2533
2534 /* Forget any previous info about this row being reversed. */
2535 if (it->glyph_row)
2536 it->glyph_row->reversed_p = 0;
2537
2538 /* Get the dimensions of the display area. The display area
2539 consists of the visible window area plus a horizontally scrolled
2540 part to the left of the window. All x-values are relative to the
2541 start of this total display area. */
2542 if (base_face_id != DEFAULT_FACE_ID)
2543 {
2544 /* Mode lines, menu bar in terminal frames. */
2545 it->first_visible_x = 0;
2546 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2547 }
2548 else
2549 {
2550 it->first_visible_x
2551 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2552 it->last_visible_x = (it->first_visible_x
2553 + window_box_width (w, TEXT_AREA));
2554
2555 /* If we truncate lines, leave room for the truncator glyph(s) at
2556 the right margin. Otherwise, leave room for the continuation
2557 glyph(s). Truncation and continuation glyphs are not inserted
2558 for window-based redisplay. */
2559 if (!FRAME_WINDOW_P (it->f))
2560 {
2561 if (it->line_wrap == TRUNCATE)
2562 it->last_visible_x -= it->truncation_pixel_width;
2563 else
2564 it->last_visible_x -= it->continuation_pixel_width;
2565 }
2566
2567 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2568 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2569 }
2570
2571 /* Leave room for a border glyph. */
2572 if (!FRAME_WINDOW_P (it->f)
2573 && !WINDOW_RIGHTMOST_P (it->w))
2574 it->last_visible_x -= 1;
2575
2576 it->last_visible_y = window_text_bottom_y (w);
2577
2578 /* For mode lines and alike, arrange for the first glyph having a
2579 left box line if the face specifies a box. */
2580 if (base_face_id != DEFAULT_FACE_ID)
2581 {
2582 struct face *face;
2583
2584 it->face_id = remapped_base_face_id;
2585
2586 /* If we have a boxed mode line, make the first character appear
2587 with a left box line. */
2588 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2589 if (face->box != FACE_NO_BOX)
2590 it->start_of_box_run_p = 1;
2591 }
2592
2593 /* If a buffer position was specified, set the iterator there,
2594 getting overlays and face properties from that position. */
2595 if (charpos >= BUF_BEG (current_buffer))
2596 {
2597 it->end_charpos = ZV;
2598 it->face_id = -1;
2599 IT_CHARPOS (*it) = charpos;
2600
2601 /* Compute byte position if not specified. */
2602 if (bytepos < charpos)
2603 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2604 else
2605 IT_BYTEPOS (*it) = bytepos;
2606
2607 it->start = it->current;
2608 /* Do we need to reorder bidirectional text? Not if this is a
2609 unibyte buffer: by definition, none of the single-byte
2610 characters are strong R2L, so no reordering is needed. And
2611 bidi.c doesn't support unibyte buffers anyway. */
2612 it->bidi_p =
2613 !NILP (BVAR (current_buffer, bidi_display_reordering))
2614 && it->multibyte_p;
2615
2616 /* If we are to reorder bidirectional text, init the bidi
2617 iterator. */
2618 if (it->bidi_p)
2619 {
2620 /* Note the paragraph direction that this buffer wants to
2621 use. */
2622 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2623 Qleft_to_right))
2624 it->paragraph_embedding = L2R;
2625 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2626 Qright_to_left))
2627 it->paragraph_embedding = R2L;
2628 else
2629 it->paragraph_embedding = NEUTRAL_DIR;
2630 bidi_unshelve_cache (NULL, 0);
2631 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2632 &it->bidi_it);
2633 }
2634
2635 /* Compute faces etc. */
2636 reseat (it, it->current.pos, 1);
2637 }
2638
2639 CHECK_IT (it);
2640 }
2641
2642
2643 /* Initialize IT for the display of window W with window start POS. */
2644
2645 void
2646 start_display (struct it *it, struct window *w, struct text_pos pos)
2647 {
2648 struct glyph_row *row;
2649 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2650
2651 row = w->desired_matrix->rows + first_vpos;
2652 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2653 it->first_vpos = first_vpos;
2654
2655 /* Don't reseat to previous visible line start if current start
2656 position is in a string or image. */
2657 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2658 {
2659 int start_at_line_beg_p;
2660 int first_y = it->current_y;
2661
2662 /* If window start is not at a line start, skip forward to POS to
2663 get the correct continuation lines width. */
2664 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2665 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2666 if (!start_at_line_beg_p)
2667 {
2668 int new_x;
2669
2670 reseat_at_previous_visible_line_start (it);
2671 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2672
2673 new_x = it->current_x + it->pixel_width;
2674
2675 /* If lines are continued, this line may end in the middle
2676 of a multi-glyph character (e.g. a control character
2677 displayed as \003, or in the middle of an overlay
2678 string). In this case move_it_to above will not have
2679 taken us to the start of the continuation line but to the
2680 end of the continued line. */
2681 if (it->current_x > 0
2682 && it->line_wrap != TRUNCATE /* Lines are continued. */
2683 && (/* And glyph doesn't fit on the line. */
2684 new_x > it->last_visible_x
2685 /* Or it fits exactly and we're on a window
2686 system frame. */
2687 || (new_x == it->last_visible_x
2688 && FRAME_WINDOW_P (it->f))))
2689 {
2690 if (it->current.dpvec_index >= 0
2691 || it->current.overlay_string_index >= 0)
2692 {
2693 set_iterator_to_next (it, 1);
2694 move_it_in_display_line_to (it, -1, -1, 0);
2695 }
2696
2697 it->continuation_lines_width += it->current_x;
2698 }
2699
2700 /* We're starting a new display line, not affected by the
2701 height of the continued line, so clear the appropriate
2702 fields in the iterator structure. */
2703 it->max_ascent = it->max_descent = 0;
2704 it->max_phys_ascent = it->max_phys_descent = 0;
2705
2706 it->current_y = first_y;
2707 it->vpos = 0;
2708 it->current_x = it->hpos = 0;
2709 }
2710 }
2711 }
2712
2713
2714 /* Return 1 if POS is a position in ellipses displayed for invisible
2715 text. W is the window we display, for text property lookup. */
2716
2717 static int
2718 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2719 {
2720 Lisp_Object prop, window;
2721 int ellipses_p = 0;
2722 EMACS_INT charpos = CHARPOS (pos->pos);
2723
2724 /* If POS specifies a position in a display vector, this might
2725 be for an ellipsis displayed for invisible text. We won't
2726 get the iterator set up for delivering that ellipsis unless
2727 we make sure that it gets aware of the invisible text. */
2728 if (pos->dpvec_index >= 0
2729 && pos->overlay_string_index < 0
2730 && CHARPOS (pos->string_pos) < 0
2731 && charpos > BEGV
2732 && (XSETWINDOW (window, w),
2733 prop = Fget_char_property (make_number (charpos),
2734 Qinvisible, window),
2735 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2736 {
2737 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2738 window);
2739 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2740 }
2741
2742 return ellipses_p;
2743 }
2744
2745
2746 /* Initialize IT for stepping through current_buffer in window W,
2747 starting at position POS that includes overlay string and display
2748 vector/ control character translation position information. Value
2749 is zero if there are overlay strings with newlines at POS. */
2750
2751 static int
2752 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2753 {
2754 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2755 int i, overlay_strings_with_newlines = 0;
2756
2757 /* If POS specifies a position in a display vector, this might
2758 be for an ellipsis displayed for invisible text. We won't
2759 get the iterator set up for delivering that ellipsis unless
2760 we make sure that it gets aware of the invisible text. */
2761 if (in_ellipses_for_invisible_text_p (pos, w))
2762 {
2763 --charpos;
2764 bytepos = 0;
2765 }
2766
2767 /* Keep in mind: the call to reseat in init_iterator skips invisible
2768 text, so we might end up at a position different from POS. This
2769 is only a problem when POS is a row start after a newline and an
2770 overlay starts there with an after-string, and the overlay has an
2771 invisible property. Since we don't skip invisible text in
2772 display_line and elsewhere immediately after consuming the
2773 newline before the row start, such a POS will not be in a string,
2774 but the call to init_iterator below will move us to the
2775 after-string. */
2776 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2777
2778 /* This only scans the current chunk -- it should scan all chunks.
2779 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2780 to 16 in 22.1 to make this a lesser problem. */
2781 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2782 {
2783 const char *s = SSDATA (it->overlay_strings[i]);
2784 const char *e = s + SBYTES (it->overlay_strings[i]);
2785
2786 while (s < e && *s != '\n')
2787 ++s;
2788
2789 if (s < e)
2790 {
2791 overlay_strings_with_newlines = 1;
2792 break;
2793 }
2794 }
2795
2796 /* If position is within an overlay string, set up IT to the right
2797 overlay string. */
2798 if (pos->overlay_string_index >= 0)
2799 {
2800 int relative_index;
2801
2802 /* If the first overlay string happens to have a `display'
2803 property for an image, the iterator will be set up for that
2804 image, and we have to undo that setup first before we can
2805 correct the overlay string index. */
2806 if (it->method == GET_FROM_IMAGE)
2807 pop_it (it);
2808
2809 /* We already have the first chunk of overlay strings in
2810 IT->overlay_strings. Load more until the one for
2811 pos->overlay_string_index is in IT->overlay_strings. */
2812 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2813 {
2814 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2815 it->current.overlay_string_index = 0;
2816 while (n--)
2817 {
2818 load_overlay_strings (it, 0);
2819 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2820 }
2821 }
2822
2823 it->current.overlay_string_index = pos->overlay_string_index;
2824 relative_index = (it->current.overlay_string_index
2825 % OVERLAY_STRING_CHUNK_SIZE);
2826 it->string = it->overlay_strings[relative_index];
2827 xassert (STRINGP (it->string));
2828 it->current.string_pos = pos->string_pos;
2829 it->method = GET_FROM_STRING;
2830 }
2831
2832 if (CHARPOS (pos->string_pos) >= 0)
2833 {
2834 /* Recorded position is not in an overlay string, but in another
2835 string. This can only be a string from a `display' property.
2836 IT should already be filled with that string. */
2837 it->current.string_pos = pos->string_pos;
2838 xassert (STRINGP (it->string));
2839 }
2840
2841 /* Restore position in display vector translations, control
2842 character translations or ellipses. */
2843 if (pos->dpvec_index >= 0)
2844 {
2845 if (it->dpvec == NULL)
2846 get_next_display_element (it);
2847 xassert (it->dpvec && it->current.dpvec_index == 0);
2848 it->current.dpvec_index = pos->dpvec_index;
2849 }
2850
2851 CHECK_IT (it);
2852 return !overlay_strings_with_newlines;
2853 }
2854
2855
2856 /* Initialize IT for stepping through current_buffer in window W
2857 starting at ROW->start. */
2858
2859 static void
2860 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2861 {
2862 init_from_display_pos (it, w, &row->start);
2863 it->start = row->start;
2864 it->continuation_lines_width = row->continuation_lines_width;
2865 CHECK_IT (it);
2866 }
2867
2868
2869 /* Initialize IT for stepping through current_buffer in window W
2870 starting in the line following ROW, i.e. starting at ROW->end.
2871 Value is zero if there are overlay strings with newlines at ROW's
2872 end position. */
2873
2874 static int
2875 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2876 {
2877 int success = 0;
2878
2879 if (init_from_display_pos (it, w, &row->end))
2880 {
2881 if (row->continued_p)
2882 it->continuation_lines_width
2883 = row->continuation_lines_width + row->pixel_width;
2884 CHECK_IT (it);
2885 success = 1;
2886 }
2887
2888 return success;
2889 }
2890
2891
2892
2893 \f
2894 /***********************************************************************
2895 Text properties
2896 ***********************************************************************/
2897
2898 /* Called when IT reaches IT->stop_charpos. Handle text property and
2899 overlay changes. Set IT->stop_charpos to the next position where
2900 to stop. */
2901
2902 static void
2903 handle_stop (struct it *it)
2904 {
2905 enum prop_handled handled;
2906 int handle_overlay_change_p;
2907 struct props *p;
2908
2909 it->dpvec = NULL;
2910 it->current.dpvec_index = -1;
2911 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2912 it->ignore_overlay_strings_at_pos_p = 0;
2913 it->ellipsis_p = 0;
2914
2915 /* Use face of preceding text for ellipsis (if invisible) */
2916 if (it->selective_display_ellipsis_p)
2917 it->saved_face_id = it->face_id;
2918
2919 do
2920 {
2921 handled = HANDLED_NORMALLY;
2922
2923 /* Call text property handlers. */
2924 for (p = it_props; p->handler; ++p)
2925 {
2926 handled = p->handler (it);
2927
2928 if (handled == HANDLED_RECOMPUTE_PROPS)
2929 break;
2930 else if (handled == HANDLED_RETURN)
2931 {
2932 /* We still want to show before and after strings from
2933 overlays even if the actual buffer text is replaced. */
2934 if (!handle_overlay_change_p
2935 || it->sp > 1
2936 || !get_overlay_strings_1 (it, 0, 0))
2937 {
2938 if (it->ellipsis_p)
2939 setup_for_ellipsis (it, 0);
2940 /* When handling a display spec, we might load an
2941 empty string. In that case, discard it here. We
2942 used to discard it in handle_single_display_spec,
2943 but that causes get_overlay_strings_1, above, to
2944 ignore overlay strings that we must check. */
2945 if (STRINGP (it->string) && !SCHARS (it->string))
2946 pop_it (it);
2947 return;
2948 }
2949 else if (STRINGP (it->string) && !SCHARS (it->string))
2950 pop_it (it);
2951 else
2952 {
2953 it->ignore_overlay_strings_at_pos_p = 1;
2954 it->string_from_display_prop_p = 0;
2955 it->from_disp_prop_p = 0;
2956 handle_overlay_change_p = 0;
2957 }
2958 handled = HANDLED_RECOMPUTE_PROPS;
2959 break;
2960 }
2961 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2962 handle_overlay_change_p = 0;
2963 }
2964
2965 if (handled != HANDLED_RECOMPUTE_PROPS)
2966 {
2967 /* Don't check for overlay strings below when set to deliver
2968 characters from a display vector. */
2969 if (it->method == GET_FROM_DISPLAY_VECTOR)
2970 handle_overlay_change_p = 0;
2971
2972 /* Handle overlay changes.
2973 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2974 if it finds overlays. */
2975 if (handle_overlay_change_p)
2976 handled = handle_overlay_change (it);
2977 }
2978
2979 if (it->ellipsis_p)
2980 {
2981 setup_for_ellipsis (it, 0);
2982 break;
2983 }
2984 }
2985 while (handled == HANDLED_RECOMPUTE_PROPS);
2986
2987 /* Determine where to stop next. */
2988 if (handled == HANDLED_NORMALLY)
2989 compute_stop_pos (it);
2990 }
2991
2992
2993 /* Compute IT->stop_charpos from text property and overlay change
2994 information for IT's current position. */
2995
2996 static void
2997 compute_stop_pos (struct it *it)
2998 {
2999 register INTERVAL iv, next_iv;
3000 Lisp_Object object, limit, position;
3001 EMACS_INT charpos, bytepos;
3002
3003 /* If nowhere else, stop at the end. */
3004 it->stop_charpos = it->end_charpos;
3005
3006 if (STRINGP (it->string))
3007 {
3008 /* Strings are usually short, so don't limit the search for
3009 properties. */
3010 object = it->string;
3011 limit = Qnil;
3012 charpos = IT_STRING_CHARPOS (*it);
3013 bytepos = IT_STRING_BYTEPOS (*it);
3014 }
3015 else
3016 {
3017 EMACS_INT pos;
3018
3019 /* If next overlay change is in front of the current stop pos
3020 (which is IT->end_charpos), stop there. Note: value of
3021 next_overlay_change is point-max if no overlay change
3022 follows. */
3023 charpos = IT_CHARPOS (*it);
3024 bytepos = IT_BYTEPOS (*it);
3025 pos = next_overlay_change (charpos);
3026 if (pos < it->stop_charpos)
3027 it->stop_charpos = pos;
3028
3029 /* If showing the region, we have to stop at the region
3030 start or end because the face might change there. */
3031 if (it->region_beg_charpos > 0)
3032 {
3033 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3034 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3035 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3036 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3037 }
3038
3039 /* Set up variables for computing the stop position from text
3040 property changes. */
3041 XSETBUFFER (object, current_buffer);
3042 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3043 }
3044
3045 /* Get the interval containing IT's position. Value is a null
3046 interval if there isn't such an interval. */
3047 position = make_number (charpos);
3048 iv = validate_interval_range (object, &position, &position, 0);
3049 if (!NULL_INTERVAL_P (iv))
3050 {
3051 Lisp_Object values_here[LAST_PROP_IDX];
3052 struct props *p;
3053
3054 /* Get properties here. */
3055 for (p = it_props; p->handler; ++p)
3056 values_here[p->idx] = textget (iv->plist, *p->name);
3057
3058 /* Look for an interval following iv that has different
3059 properties. */
3060 for (next_iv = next_interval (iv);
3061 (!NULL_INTERVAL_P (next_iv)
3062 && (NILP (limit)
3063 || XFASTINT (limit) > next_iv->position));
3064 next_iv = next_interval (next_iv))
3065 {
3066 for (p = it_props; p->handler; ++p)
3067 {
3068 Lisp_Object new_value;
3069
3070 new_value = textget (next_iv->plist, *p->name);
3071 if (!EQ (values_here[p->idx], new_value))
3072 break;
3073 }
3074
3075 if (p->handler)
3076 break;
3077 }
3078
3079 if (!NULL_INTERVAL_P (next_iv))
3080 {
3081 if (INTEGERP (limit)
3082 && next_iv->position >= XFASTINT (limit))
3083 /* No text property change up to limit. */
3084 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3085 else
3086 /* Text properties change in next_iv. */
3087 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3088 }
3089 }
3090
3091 if (it->cmp_it.id < 0)
3092 {
3093 EMACS_INT stoppos = it->end_charpos;
3094
3095 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3096 stoppos = -1;
3097 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3098 stoppos, it->string);
3099 }
3100
3101 xassert (STRINGP (it->string)
3102 || (it->stop_charpos >= BEGV
3103 && it->stop_charpos >= IT_CHARPOS (*it)));
3104 }
3105
3106
3107 /* Return the position of the next overlay change after POS in
3108 current_buffer. Value is point-max if no overlay change
3109 follows. This is like `next-overlay-change' but doesn't use
3110 xmalloc. */
3111
3112 static EMACS_INT
3113 next_overlay_change (EMACS_INT pos)
3114 {
3115 ptrdiff_t i, noverlays;
3116 EMACS_INT endpos;
3117 Lisp_Object *overlays;
3118
3119 /* Get all overlays at the given position. */
3120 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3121
3122 /* If any of these overlays ends before endpos,
3123 use its ending point instead. */
3124 for (i = 0; i < noverlays; ++i)
3125 {
3126 Lisp_Object oend;
3127 EMACS_INT oendpos;
3128
3129 oend = OVERLAY_END (overlays[i]);
3130 oendpos = OVERLAY_POSITION (oend);
3131 endpos = min (endpos, oendpos);
3132 }
3133
3134 return endpos;
3135 }
3136
3137 /* How many characters forward to search for a display property or
3138 display string. Enough for a screenful of 100 lines x 50
3139 characters in a line. */
3140 #define MAX_DISP_SCAN 5000
3141
3142 /* Return the character position of a display string at or after
3143 position specified by POSITION. If no display string exists at or
3144 after POSITION, return ZV. A display string is either an overlay
3145 with `display' property whose value is a string, or a `display'
3146 text property whose value is a string. STRING is data about the
3147 string to iterate; if STRING->lstring is nil, we are iterating a
3148 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3149 on a GUI frame. */
3150 EMACS_INT
3151 compute_display_string_pos (struct text_pos *position,
3152 struct bidi_string_data *string,
3153 int frame_window_p, int *disp_prop_p)
3154 {
3155 /* OBJECT = nil means current buffer. */
3156 Lisp_Object object =
3157 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3158 Lisp_Object pos, spec, limpos;
3159 int string_p = (string && (STRINGP (string->lstring) || string->s));
3160 EMACS_INT eob = string_p ? string->schars : ZV;
3161 EMACS_INT begb = string_p ? 0 : BEGV;
3162 EMACS_INT bufpos, charpos = CHARPOS (*position);
3163 EMACS_INT lim =
3164 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3165 struct text_pos tpos;
3166
3167 *disp_prop_p = 1;
3168
3169 if (charpos >= eob
3170 /* We don't support display properties whose values are strings
3171 that have display string properties. */
3172 || string->from_disp_str
3173 /* C strings cannot have display properties. */
3174 || (string->s && !STRINGP (object)))
3175 {
3176 *disp_prop_p = 0;
3177 return eob;
3178 }
3179
3180 /* If the character at CHARPOS is where the display string begins,
3181 return CHARPOS. */
3182 pos = make_number (charpos);
3183 if (STRINGP (object))
3184 bufpos = string->bufpos;
3185 else
3186 bufpos = charpos;
3187 tpos = *position;
3188 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3189 && (charpos <= begb
3190 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3191 object),
3192 spec))
3193 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3194 frame_window_p))
3195 {
3196 return charpos;
3197 }
3198
3199 /* Look forward for the first character with a `display' property
3200 that will replace the underlying text when displayed. */
3201 limpos = make_number (lim);
3202 do {
3203 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3204 CHARPOS (tpos) = XFASTINT (pos);
3205 if (CHARPOS (tpos) >= lim)
3206 {
3207 *disp_prop_p = 0;
3208 break;
3209 }
3210 if (STRINGP (object))
3211 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3212 else
3213 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3214 spec = Fget_char_property (pos, Qdisplay, object);
3215 if (!STRINGP (object))
3216 bufpos = CHARPOS (tpos);
3217 } while (NILP (spec)
3218 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3219 frame_window_p));
3220
3221 return CHARPOS (tpos);
3222 }
3223
3224 /* Return the character position of the end of the display string that
3225 started at CHARPOS. A display string is either an overlay with
3226 `display' property whose value is a string or a `display' text
3227 property whose value is a string. */
3228 EMACS_INT
3229 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3230 {
3231 /* OBJECT = nil means current buffer. */
3232 Lisp_Object object =
3233 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3234 Lisp_Object pos = make_number (charpos);
3235 EMACS_INT eob =
3236 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3237
3238 if (charpos >= eob || (string->s && !STRINGP (object)))
3239 return eob;
3240
3241 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3242 abort ();
3243
3244 /* Look forward for the first character where the `display' property
3245 changes. */
3246 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3247
3248 return XFASTINT (pos);
3249 }
3250
3251
3252 \f
3253 /***********************************************************************
3254 Fontification
3255 ***********************************************************************/
3256
3257 /* Handle changes in the `fontified' property of the current buffer by
3258 calling hook functions from Qfontification_functions to fontify
3259 regions of text. */
3260
3261 static enum prop_handled
3262 handle_fontified_prop (struct it *it)
3263 {
3264 Lisp_Object prop, pos;
3265 enum prop_handled handled = HANDLED_NORMALLY;
3266
3267 if (!NILP (Vmemory_full))
3268 return handled;
3269
3270 /* Get the value of the `fontified' property at IT's current buffer
3271 position. (The `fontified' property doesn't have a special
3272 meaning in strings.) If the value is nil, call functions from
3273 Qfontification_functions. */
3274 if (!STRINGP (it->string)
3275 && it->s == NULL
3276 && !NILP (Vfontification_functions)
3277 && !NILP (Vrun_hooks)
3278 && (pos = make_number (IT_CHARPOS (*it)),
3279 prop = Fget_char_property (pos, Qfontified, Qnil),
3280 /* Ignore the special cased nil value always present at EOB since
3281 no amount of fontifying will be able to change it. */
3282 NILP (prop) && IT_CHARPOS (*it) < Z))
3283 {
3284 int count = SPECPDL_INDEX ();
3285 Lisp_Object val;
3286 struct buffer *obuf = current_buffer;
3287 int begv = BEGV, zv = ZV;
3288 int old_clip_changed = current_buffer->clip_changed;
3289
3290 val = Vfontification_functions;
3291 specbind (Qfontification_functions, Qnil);
3292
3293 xassert (it->end_charpos == ZV);
3294
3295 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3296 safe_call1 (val, pos);
3297 else
3298 {
3299 Lisp_Object fns, fn;
3300 struct gcpro gcpro1, gcpro2;
3301
3302 fns = Qnil;
3303 GCPRO2 (val, fns);
3304
3305 for (; CONSP (val); val = XCDR (val))
3306 {
3307 fn = XCAR (val);
3308
3309 if (EQ (fn, Qt))
3310 {
3311 /* A value of t indicates this hook has a local
3312 binding; it means to run the global binding too.
3313 In a global value, t should not occur. If it
3314 does, we must ignore it to avoid an endless
3315 loop. */
3316 for (fns = Fdefault_value (Qfontification_functions);
3317 CONSP (fns);
3318 fns = XCDR (fns))
3319 {
3320 fn = XCAR (fns);
3321 if (!EQ (fn, Qt))
3322 safe_call1 (fn, pos);
3323 }
3324 }
3325 else
3326 safe_call1 (fn, pos);
3327 }
3328
3329 UNGCPRO;
3330 }
3331
3332 unbind_to (count, Qnil);
3333
3334 /* Fontification functions routinely call `save-restriction'.
3335 Normally, this tags clip_changed, which can confuse redisplay
3336 (see discussion in Bug#6671). Since we don't perform any
3337 special handling of fontification changes in the case where
3338 `save-restriction' isn't called, there's no point doing so in
3339 this case either. So, if the buffer's restrictions are
3340 actually left unchanged, reset clip_changed. */
3341 if (obuf == current_buffer)
3342 {
3343 if (begv == BEGV && zv == ZV)
3344 current_buffer->clip_changed = old_clip_changed;
3345 }
3346 /* There isn't much we can reasonably do to protect against
3347 misbehaving fontification, but here's a fig leaf. */
3348 else if (!NILP (BVAR (obuf, name)))
3349 set_buffer_internal_1 (obuf);
3350
3351 /* The fontification code may have added/removed text.
3352 It could do even a lot worse, but let's at least protect against
3353 the most obvious case where only the text past `pos' gets changed',
3354 as is/was done in grep.el where some escapes sequences are turned
3355 into face properties (bug#7876). */
3356 it->end_charpos = ZV;
3357
3358 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3359 something. This avoids an endless loop if they failed to
3360 fontify the text for which reason ever. */
3361 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3362 handled = HANDLED_RECOMPUTE_PROPS;
3363 }
3364
3365 return handled;
3366 }
3367
3368
3369 \f
3370 /***********************************************************************
3371 Faces
3372 ***********************************************************************/
3373
3374 /* Set up iterator IT from face properties at its current position.
3375 Called from handle_stop. */
3376
3377 static enum prop_handled
3378 handle_face_prop (struct it *it)
3379 {
3380 int new_face_id;
3381 EMACS_INT next_stop;
3382
3383 if (!STRINGP (it->string))
3384 {
3385 new_face_id
3386 = face_at_buffer_position (it->w,
3387 IT_CHARPOS (*it),
3388 it->region_beg_charpos,
3389 it->region_end_charpos,
3390 &next_stop,
3391 (IT_CHARPOS (*it)
3392 + TEXT_PROP_DISTANCE_LIMIT),
3393 0, it->base_face_id);
3394
3395 /* Is this a start of a run of characters with box face?
3396 Caveat: this can be called for a freshly initialized
3397 iterator; face_id is -1 in this case. We know that the new
3398 face will not change until limit, i.e. if the new face has a
3399 box, all characters up to limit will have one. But, as
3400 usual, we don't know whether limit is really the end. */
3401 if (new_face_id != it->face_id)
3402 {
3403 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3404
3405 /* If new face has a box but old face has not, this is
3406 the start of a run of characters with box, i.e. it has
3407 a shadow on the left side. The value of face_id of the
3408 iterator will be -1 if this is the initial call that gets
3409 the face. In this case, we have to look in front of IT's
3410 position and see whether there is a face != new_face_id. */
3411 it->start_of_box_run_p
3412 = (new_face->box != FACE_NO_BOX
3413 && (it->face_id >= 0
3414 || IT_CHARPOS (*it) == BEG
3415 || new_face_id != face_before_it_pos (it)));
3416 it->face_box_p = new_face->box != FACE_NO_BOX;
3417 }
3418 }
3419 else
3420 {
3421 int base_face_id;
3422 EMACS_INT bufpos;
3423 int i;
3424 Lisp_Object from_overlay
3425 = (it->current.overlay_string_index >= 0
3426 ? it->string_overlays[it->current.overlay_string_index]
3427 : Qnil);
3428
3429 /* See if we got to this string directly or indirectly from
3430 an overlay property. That includes the before-string or
3431 after-string of an overlay, strings in display properties
3432 provided by an overlay, their text properties, etc.
3433
3434 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3435 if (! NILP (from_overlay))
3436 for (i = it->sp - 1; i >= 0; i--)
3437 {
3438 if (it->stack[i].current.overlay_string_index >= 0)
3439 from_overlay
3440 = it->string_overlays[it->stack[i].current.overlay_string_index];
3441 else if (! NILP (it->stack[i].from_overlay))
3442 from_overlay = it->stack[i].from_overlay;
3443
3444 if (!NILP (from_overlay))
3445 break;
3446 }
3447
3448 if (! NILP (from_overlay))
3449 {
3450 bufpos = IT_CHARPOS (*it);
3451 /* For a string from an overlay, the base face depends
3452 only on text properties and ignores overlays. */
3453 base_face_id
3454 = face_for_overlay_string (it->w,
3455 IT_CHARPOS (*it),
3456 it->region_beg_charpos,
3457 it->region_end_charpos,
3458 &next_stop,
3459 (IT_CHARPOS (*it)
3460 + TEXT_PROP_DISTANCE_LIMIT),
3461 0,
3462 from_overlay);
3463 }
3464 else
3465 {
3466 bufpos = 0;
3467
3468 /* For strings from a `display' property, use the face at
3469 IT's current buffer position as the base face to merge
3470 with, so that overlay strings appear in the same face as
3471 surrounding text, unless they specify their own
3472 faces. */
3473 base_face_id = underlying_face_id (it);
3474 }
3475
3476 new_face_id = face_at_string_position (it->w,
3477 it->string,
3478 IT_STRING_CHARPOS (*it),
3479 bufpos,
3480 it->region_beg_charpos,
3481 it->region_end_charpos,
3482 &next_stop,
3483 base_face_id, 0);
3484
3485 /* Is this a start of a run of characters with box? Caveat:
3486 this can be called for a freshly allocated iterator; face_id
3487 is -1 is this case. We know that the new face will not
3488 change until the next check pos, i.e. if the new face has a
3489 box, all characters up to that position will have a
3490 box. But, as usual, we don't know whether that position
3491 is really the end. */
3492 if (new_face_id != it->face_id)
3493 {
3494 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3495 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3496
3497 /* If new face has a box but old face hasn't, this is the
3498 start of a run of characters with box, i.e. it has a
3499 shadow on the left side. */
3500 it->start_of_box_run_p
3501 = new_face->box && (old_face == NULL || !old_face->box);
3502 it->face_box_p = new_face->box != FACE_NO_BOX;
3503 }
3504 }
3505
3506 it->face_id = new_face_id;
3507 return HANDLED_NORMALLY;
3508 }
3509
3510
3511 /* Return the ID of the face ``underlying'' IT's current position,
3512 which is in a string. If the iterator is associated with a
3513 buffer, return the face at IT's current buffer position.
3514 Otherwise, use the iterator's base_face_id. */
3515
3516 static int
3517 underlying_face_id (struct it *it)
3518 {
3519 int face_id = it->base_face_id, i;
3520
3521 xassert (STRINGP (it->string));
3522
3523 for (i = it->sp - 1; i >= 0; --i)
3524 if (NILP (it->stack[i].string))
3525 face_id = it->stack[i].face_id;
3526
3527 return face_id;
3528 }
3529
3530
3531 /* Compute the face one character before or after the current position
3532 of IT, in the visual order. BEFORE_P non-zero means get the face
3533 in front (to the left in L2R paragraphs, to the right in R2L
3534 paragraphs) of IT's screen position. Value is the ID of the face. */
3535
3536 static int
3537 face_before_or_after_it_pos (struct it *it, int before_p)
3538 {
3539 int face_id, limit;
3540 EMACS_INT next_check_charpos;
3541 struct it it_copy;
3542 void *it_copy_data = NULL;
3543
3544 xassert (it->s == NULL);
3545
3546 if (STRINGP (it->string))
3547 {
3548 EMACS_INT bufpos, charpos;
3549 int base_face_id;
3550
3551 /* No face change past the end of the string (for the case
3552 we are padding with spaces). No face change before the
3553 string start. */
3554 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3555 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3556 return it->face_id;
3557
3558 if (!it->bidi_p)
3559 {
3560 /* Set charpos to the position before or after IT's current
3561 position, in the logical order, which in the non-bidi
3562 case is the same as the visual order. */
3563 if (before_p)
3564 charpos = IT_STRING_CHARPOS (*it) - 1;
3565 else if (it->what == IT_COMPOSITION)
3566 /* For composition, we must check the character after the
3567 composition. */
3568 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3569 else
3570 charpos = IT_STRING_CHARPOS (*it) + 1;
3571 }
3572 else
3573 {
3574 if (before_p)
3575 {
3576 /* With bidi iteration, the character before the current
3577 in the visual order cannot be found by simple
3578 iteration, because "reverse" reordering is not
3579 supported. Instead, we need to use the move_it_*
3580 family of functions. */
3581 /* Ignore face changes before the first visible
3582 character on this display line. */
3583 if (it->current_x <= it->first_visible_x)
3584 return it->face_id;
3585 SAVE_IT (it_copy, *it, it_copy_data);
3586 /* Implementation note: Since move_it_in_display_line
3587 works in the iterator geometry, and thinks the first
3588 character is always the leftmost, even in R2L lines,
3589 we don't need to distinguish between the R2L and L2R
3590 cases here. */
3591 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3592 it_copy.current_x - 1, MOVE_TO_X);
3593 charpos = IT_STRING_CHARPOS (it_copy);
3594 RESTORE_IT (it, it, it_copy_data);
3595 }
3596 else
3597 {
3598 /* Set charpos to the string position of the character
3599 that comes after IT's current position in the visual
3600 order. */
3601 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3602
3603 it_copy = *it;
3604 while (n--)
3605 bidi_move_to_visually_next (&it_copy.bidi_it);
3606
3607 charpos = it_copy.bidi_it.charpos;
3608 }
3609 }
3610 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3611
3612 if (it->current.overlay_string_index >= 0)
3613 bufpos = IT_CHARPOS (*it);
3614 else
3615 bufpos = 0;
3616
3617 base_face_id = underlying_face_id (it);
3618
3619 /* Get the face for ASCII, or unibyte. */
3620 face_id = face_at_string_position (it->w,
3621 it->string,
3622 charpos,
3623 bufpos,
3624 it->region_beg_charpos,
3625 it->region_end_charpos,
3626 &next_check_charpos,
3627 base_face_id, 0);
3628
3629 /* Correct the face for charsets different from ASCII. Do it
3630 for the multibyte case only. The face returned above is
3631 suitable for unibyte text if IT->string is unibyte. */
3632 if (STRING_MULTIBYTE (it->string))
3633 {
3634 struct text_pos pos1 = string_pos (charpos, it->string);
3635 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3636 int c, len;
3637 struct face *face = FACE_FROM_ID (it->f, face_id);
3638
3639 c = string_char_and_length (p, &len);
3640 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3641 }
3642 }
3643 else
3644 {
3645 struct text_pos pos;
3646
3647 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3648 || (IT_CHARPOS (*it) <= BEGV && before_p))
3649 return it->face_id;
3650
3651 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3652 pos = it->current.pos;
3653
3654 if (!it->bidi_p)
3655 {
3656 if (before_p)
3657 DEC_TEXT_POS (pos, it->multibyte_p);
3658 else
3659 {
3660 if (it->what == IT_COMPOSITION)
3661 {
3662 /* For composition, we must check the position after
3663 the composition. */
3664 pos.charpos += it->cmp_it.nchars;
3665 pos.bytepos += it->len;
3666 }
3667 else
3668 INC_TEXT_POS (pos, it->multibyte_p);
3669 }
3670 }
3671 else
3672 {
3673 if (before_p)
3674 {
3675 /* With bidi iteration, the character before the current
3676 in the visual order cannot be found by simple
3677 iteration, because "reverse" reordering is not
3678 supported. Instead, we need to use the move_it_*
3679 family of functions. */
3680 /* Ignore face changes before the first visible
3681 character on this display line. */
3682 if (it->current_x <= it->first_visible_x)
3683 return it->face_id;
3684 SAVE_IT (it_copy, *it, it_copy_data);
3685 /* Implementation note: Since move_it_in_display_line
3686 works in the iterator geometry, and thinks the first
3687 character is always the leftmost, even in R2L lines,
3688 we don't need to distinguish between the R2L and L2R
3689 cases here. */
3690 move_it_in_display_line (&it_copy, ZV,
3691 it_copy.current_x - 1, MOVE_TO_X);
3692 pos = it_copy.current.pos;
3693 RESTORE_IT (it, it, it_copy_data);
3694 }
3695 else
3696 {
3697 /* Set charpos to the buffer position of the character
3698 that comes after IT's current position in the visual
3699 order. */
3700 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3701
3702 it_copy = *it;
3703 while (n--)
3704 bidi_move_to_visually_next (&it_copy.bidi_it);
3705
3706 SET_TEXT_POS (pos,
3707 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3708 }
3709 }
3710 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3711
3712 /* Determine face for CHARSET_ASCII, or unibyte. */
3713 face_id = face_at_buffer_position (it->w,
3714 CHARPOS (pos),
3715 it->region_beg_charpos,
3716 it->region_end_charpos,
3717 &next_check_charpos,
3718 limit, 0, -1);
3719
3720 /* Correct the face for charsets different from ASCII. Do it
3721 for the multibyte case only. The face returned above is
3722 suitable for unibyte text if current_buffer is unibyte. */
3723 if (it->multibyte_p)
3724 {
3725 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3726 struct face *face = FACE_FROM_ID (it->f, face_id);
3727 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3728 }
3729 }
3730
3731 return face_id;
3732 }
3733
3734
3735 \f
3736 /***********************************************************************
3737 Invisible text
3738 ***********************************************************************/
3739
3740 /* Set up iterator IT from invisible properties at its current
3741 position. Called from handle_stop. */
3742
3743 static enum prop_handled
3744 handle_invisible_prop (struct it *it)
3745 {
3746 enum prop_handled handled = HANDLED_NORMALLY;
3747
3748 if (STRINGP (it->string))
3749 {
3750 Lisp_Object prop, end_charpos, limit, charpos;
3751
3752 /* Get the value of the invisible text property at the
3753 current position. Value will be nil if there is no such
3754 property. */
3755 charpos = make_number (IT_STRING_CHARPOS (*it));
3756 prop = Fget_text_property (charpos, Qinvisible, it->string);
3757
3758 if (!NILP (prop)
3759 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3760 {
3761 EMACS_INT endpos;
3762
3763 handled = HANDLED_RECOMPUTE_PROPS;
3764
3765 /* Get the position at which the next change of the
3766 invisible text property can be found in IT->string.
3767 Value will be nil if the property value is the same for
3768 all the rest of IT->string. */
3769 XSETINT (limit, SCHARS (it->string));
3770 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3771 it->string, limit);
3772
3773 /* Text at current position is invisible. The next
3774 change in the property is at position end_charpos.
3775 Move IT's current position to that position. */
3776 if (INTEGERP (end_charpos)
3777 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3778 {
3779 struct text_pos old;
3780 EMACS_INT oldpos;
3781
3782 old = it->current.string_pos;
3783 oldpos = CHARPOS (old);
3784 if (it->bidi_p)
3785 {
3786 if (it->bidi_it.first_elt
3787 && it->bidi_it.charpos < SCHARS (it->string))
3788 bidi_paragraph_init (it->paragraph_embedding,
3789 &it->bidi_it, 1);
3790 /* Bidi-iterate out of the invisible text. */
3791 do
3792 {
3793 bidi_move_to_visually_next (&it->bidi_it);
3794 }
3795 while (oldpos <= it->bidi_it.charpos
3796 && it->bidi_it.charpos < endpos);
3797
3798 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3799 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3800 if (IT_CHARPOS (*it) >= endpos)
3801 it->prev_stop = endpos;
3802 }
3803 else
3804 {
3805 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3806 compute_string_pos (&it->current.string_pos, old, it->string);
3807 }
3808 }
3809 else
3810 {
3811 /* The rest of the string is invisible. If this is an
3812 overlay string, proceed with the next overlay string
3813 or whatever comes and return a character from there. */
3814 if (it->current.overlay_string_index >= 0)
3815 {
3816 next_overlay_string (it);
3817 /* Don't check for overlay strings when we just
3818 finished processing them. */
3819 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3820 }
3821 else
3822 {
3823 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3824 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3825 }
3826 }
3827 }
3828 }
3829 else
3830 {
3831 int invis_p;
3832 EMACS_INT newpos, next_stop, start_charpos, tem;
3833 Lisp_Object pos, prop, overlay;
3834
3835 /* First of all, is there invisible text at this position? */
3836 tem = start_charpos = IT_CHARPOS (*it);
3837 pos = make_number (tem);
3838 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3839 &overlay);
3840 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3841
3842 /* If we are on invisible text, skip over it. */
3843 if (invis_p && start_charpos < it->end_charpos)
3844 {
3845 /* Record whether we have to display an ellipsis for the
3846 invisible text. */
3847 int display_ellipsis_p = invis_p == 2;
3848
3849 handled = HANDLED_RECOMPUTE_PROPS;
3850
3851 /* Loop skipping over invisible text. The loop is left at
3852 ZV or with IT on the first char being visible again. */
3853 do
3854 {
3855 /* Try to skip some invisible text. Return value is the
3856 position reached which can be equal to where we start
3857 if there is nothing invisible there. This skips both
3858 over invisible text properties and overlays with
3859 invisible property. */
3860 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3861
3862 /* If we skipped nothing at all we weren't at invisible
3863 text in the first place. If everything to the end of
3864 the buffer was skipped, end the loop. */
3865 if (newpos == tem || newpos >= ZV)
3866 invis_p = 0;
3867 else
3868 {
3869 /* We skipped some characters but not necessarily
3870 all there are. Check if we ended up on visible
3871 text. Fget_char_property returns the property of
3872 the char before the given position, i.e. if we
3873 get invis_p = 0, this means that the char at
3874 newpos is visible. */
3875 pos = make_number (newpos);
3876 prop = Fget_char_property (pos, Qinvisible, it->window);
3877 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3878 }
3879
3880 /* If we ended up on invisible text, proceed to
3881 skip starting with next_stop. */
3882 if (invis_p)
3883 tem = next_stop;
3884
3885 /* If there are adjacent invisible texts, don't lose the
3886 second one's ellipsis. */
3887 if (invis_p == 2)
3888 display_ellipsis_p = 1;
3889 }
3890 while (invis_p);
3891
3892 /* The position newpos is now either ZV or on visible text. */
3893 if (it->bidi_p && newpos < ZV)
3894 {
3895 /* With bidi iteration, the region of invisible text
3896 could start and/or end in the middle of a non-base
3897 embedding level. Therefore, we need to skip
3898 invisible text using the bidi iterator, starting at
3899 IT's current position, until we find ourselves
3900 outside the invisible text. Skipping invisible text
3901 _after_ bidi iteration avoids affecting the visual
3902 order of the displayed text when invisible properties
3903 are added or removed. */
3904 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3905 {
3906 /* If we were `reseat'ed to a new paragraph,
3907 determine the paragraph base direction. We need
3908 to do it now because next_element_from_buffer may
3909 not have a chance to do it, if we are going to
3910 skip any text at the beginning, which resets the
3911 FIRST_ELT flag. */
3912 bidi_paragraph_init (it->paragraph_embedding,
3913 &it->bidi_it, 1);
3914 }
3915 do
3916 {
3917 bidi_move_to_visually_next (&it->bidi_it);
3918 }
3919 while (it->stop_charpos <= it->bidi_it.charpos
3920 && it->bidi_it.charpos < newpos);
3921 IT_CHARPOS (*it) = it->bidi_it.charpos;
3922 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3923 /* If we overstepped NEWPOS, record its position in the
3924 iterator, so that we skip invisible text if later the
3925 bidi iteration lands us in the invisible region
3926 again. */
3927 if (IT_CHARPOS (*it) >= newpos)
3928 it->prev_stop = newpos;
3929 }
3930 else
3931 {
3932 IT_CHARPOS (*it) = newpos;
3933 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3934 }
3935
3936 /* If there are before-strings at the start of invisible
3937 text, and the text is invisible because of a text
3938 property, arrange to show before-strings because 20.x did
3939 it that way. (If the text is invisible because of an
3940 overlay property instead of a text property, this is
3941 already handled in the overlay code.) */
3942 if (NILP (overlay)
3943 && get_overlay_strings (it, it->stop_charpos))
3944 {
3945 handled = HANDLED_RECOMPUTE_PROPS;
3946 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3947 }
3948 else if (display_ellipsis_p)
3949 {
3950 /* Make sure that the glyphs of the ellipsis will get
3951 correct `charpos' values. If we would not update
3952 it->position here, the glyphs would belong to the
3953 last visible character _before_ the invisible
3954 text, which confuses `set_cursor_from_row'.
3955
3956 We use the last invisible position instead of the
3957 first because this way the cursor is always drawn on
3958 the first "." of the ellipsis, whenever PT is inside
3959 the invisible text. Otherwise the cursor would be
3960 placed _after_ the ellipsis when the point is after the
3961 first invisible character. */
3962 if (!STRINGP (it->object))
3963 {
3964 it->position.charpos = newpos - 1;
3965 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3966 }
3967 it->ellipsis_p = 1;
3968 /* Let the ellipsis display before
3969 considering any properties of the following char.
3970 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3971 handled = HANDLED_RETURN;
3972 }
3973 }
3974 }
3975
3976 return handled;
3977 }
3978
3979
3980 /* Make iterator IT return `...' next.
3981 Replaces LEN characters from buffer. */
3982
3983 static void
3984 setup_for_ellipsis (struct it *it, int len)
3985 {
3986 /* Use the display table definition for `...'. Invalid glyphs
3987 will be handled by the method returning elements from dpvec. */
3988 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3989 {
3990 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3991 it->dpvec = v->contents;
3992 it->dpend = v->contents + v->header.size;
3993 }
3994 else
3995 {
3996 /* Default `...'. */
3997 it->dpvec = default_invis_vector;
3998 it->dpend = default_invis_vector + 3;
3999 }
4000
4001 it->dpvec_char_len = len;
4002 it->current.dpvec_index = 0;
4003 it->dpvec_face_id = -1;
4004
4005 /* Remember the current face id in case glyphs specify faces.
4006 IT's face is restored in set_iterator_to_next.
4007 saved_face_id was set to preceding char's face in handle_stop. */
4008 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4009 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4010
4011 it->method = GET_FROM_DISPLAY_VECTOR;
4012 it->ellipsis_p = 1;
4013 }
4014
4015
4016 \f
4017 /***********************************************************************
4018 'display' property
4019 ***********************************************************************/
4020
4021 /* Set up iterator IT from `display' property at its current position.
4022 Called from handle_stop.
4023 We return HANDLED_RETURN if some part of the display property
4024 overrides the display of the buffer text itself.
4025 Otherwise we return HANDLED_NORMALLY. */
4026
4027 static enum prop_handled
4028 handle_display_prop (struct it *it)
4029 {
4030 Lisp_Object propval, object, overlay;
4031 struct text_pos *position;
4032 EMACS_INT bufpos;
4033 /* Nonzero if some property replaces the display of the text itself. */
4034 int display_replaced_p = 0;
4035
4036 if (STRINGP (it->string))
4037 {
4038 object = it->string;
4039 position = &it->current.string_pos;
4040 bufpos = CHARPOS (it->current.pos);
4041 }
4042 else
4043 {
4044 XSETWINDOW (object, it->w);
4045 position = &it->current.pos;
4046 bufpos = CHARPOS (*position);
4047 }
4048
4049 /* Reset those iterator values set from display property values. */
4050 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4051 it->space_width = Qnil;
4052 it->font_height = Qnil;
4053 it->voffset = 0;
4054
4055 /* We don't support recursive `display' properties, i.e. string
4056 values that have a string `display' property, that have a string
4057 `display' property etc. */
4058 if (!it->string_from_display_prop_p)
4059 it->area = TEXT_AREA;
4060
4061 propval = get_char_property_and_overlay (make_number (position->charpos),
4062 Qdisplay, object, &overlay);
4063 if (NILP (propval))
4064 return HANDLED_NORMALLY;
4065 /* Now OVERLAY is the overlay that gave us this property, or nil
4066 if it was a text property. */
4067
4068 if (!STRINGP (it->string))
4069 object = it->w->buffer;
4070
4071 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4072 position, bufpos,
4073 FRAME_WINDOW_P (it->f));
4074
4075 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4076 }
4077
4078 /* Subroutine of handle_display_prop. Returns non-zero if the display
4079 specification in SPEC is a replacing specification, i.e. it would
4080 replace the text covered by `display' property with something else,
4081 such as an image or a display string.
4082
4083 See handle_single_display_spec for documentation of arguments.
4084 frame_window_p is non-zero if the window being redisplayed is on a
4085 GUI frame; this argument is used only if IT is NULL, see below.
4086
4087 IT can be NULL, if this is called by the bidi reordering code
4088 through compute_display_string_pos, which see. In that case, this
4089 function only examines SPEC, but does not otherwise "handle" it, in
4090 the sense that it doesn't set up members of IT from the display
4091 spec. */
4092 static int
4093 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4094 Lisp_Object overlay, struct text_pos *position,
4095 EMACS_INT bufpos, int frame_window_p)
4096 {
4097 int replacing_p = 0;
4098
4099 if (CONSP (spec)
4100 /* Simple specerties. */
4101 && !EQ (XCAR (spec), Qimage)
4102 && !EQ (XCAR (spec), Qspace)
4103 && !EQ (XCAR (spec), Qwhen)
4104 && !EQ (XCAR (spec), Qslice)
4105 && !EQ (XCAR (spec), Qspace_width)
4106 && !EQ (XCAR (spec), Qheight)
4107 && !EQ (XCAR (spec), Qraise)
4108 /* Marginal area specifications. */
4109 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4110 && !EQ (XCAR (spec), Qleft_fringe)
4111 && !EQ (XCAR (spec), Qright_fringe)
4112 && !NILP (XCAR (spec)))
4113 {
4114 for (; CONSP (spec); spec = XCDR (spec))
4115 {
4116 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4117 position, bufpos, replacing_p,
4118 frame_window_p))
4119 {
4120 replacing_p = 1;
4121 /* If some text in a string is replaced, `position' no
4122 longer points to the position of `object'. */
4123 if (!it || STRINGP (object))
4124 break;
4125 }
4126 }
4127 }
4128 else if (VECTORP (spec))
4129 {
4130 int i;
4131 for (i = 0; i < ASIZE (spec); ++i)
4132 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4133 position, bufpos, replacing_p,
4134 frame_window_p))
4135 {
4136 replacing_p = 1;
4137 /* If some text in a string is replaced, `position' no
4138 longer points to the position of `object'. */
4139 if (!it || STRINGP (object))
4140 break;
4141 }
4142 }
4143 else
4144 {
4145 if (handle_single_display_spec (it, spec, object, overlay,
4146 position, bufpos, 0, frame_window_p))
4147 replacing_p = 1;
4148 }
4149
4150 return replacing_p;
4151 }
4152
4153 /* Value is the position of the end of the `display' property starting
4154 at START_POS in OBJECT. */
4155
4156 static struct text_pos
4157 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4158 {
4159 Lisp_Object end;
4160 struct text_pos end_pos;
4161
4162 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4163 Qdisplay, object, Qnil);
4164 CHARPOS (end_pos) = XFASTINT (end);
4165 if (STRINGP (object))
4166 compute_string_pos (&end_pos, start_pos, it->string);
4167 else
4168 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4169
4170 return end_pos;
4171 }
4172
4173
4174 /* Set up IT from a single `display' property specification SPEC. OBJECT
4175 is the object in which the `display' property was found. *POSITION
4176 is the position in OBJECT at which the `display' property was found.
4177 BUFPOS is the buffer position of OBJECT (different from POSITION if
4178 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4179 previously saw a display specification which already replaced text
4180 display with something else, for example an image; we ignore such
4181 properties after the first one has been processed.
4182
4183 OVERLAY is the overlay this `display' property came from,
4184 or nil if it was a text property.
4185
4186 If SPEC is a `space' or `image' specification, and in some other
4187 cases too, set *POSITION to the position where the `display'
4188 property ends.
4189
4190 If IT is NULL, only examine the property specification in SPEC, but
4191 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4192 is intended to be displayed in a window on a GUI frame.
4193
4194 Value is non-zero if something was found which replaces the display
4195 of buffer or string text. */
4196
4197 static int
4198 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4199 Lisp_Object overlay, struct text_pos *position,
4200 EMACS_INT bufpos, int display_replaced_p,
4201 int frame_window_p)
4202 {
4203 Lisp_Object form;
4204 Lisp_Object location, value;
4205 struct text_pos start_pos = *position;
4206 int valid_p;
4207
4208 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4209 If the result is non-nil, use VALUE instead of SPEC. */
4210 form = Qt;
4211 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4212 {
4213 spec = XCDR (spec);
4214 if (!CONSP (spec))
4215 return 0;
4216 form = XCAR (spec);
4217 spec = XCDR (spec);
4218 }
4219
4220 if (!NILP (form) && !EQ (form, Qt))
4221 {
4222 int count = SPECPDL_INDEX ();
4223 struct gcpro gcpro1;
4224
4225 /* Bind `object' to the object having the `display' property, a
4226 buffer or string. Bind `position' to the position in the
4227 object where the property was found, and `buffer-position'
4228 to the current position in the buffer. */
4229
4230 if (NILP (object))
4231 XSETBUFFER (object, current_buffer);
4232 specbind (Qobject, object);
4233 specbind (Qposition, make_number (CHARPOS (*position)));
4234 specbind (Qbuffer_position, make_number (bufpos));
4235 GCPRO1 (form);
4236 form = safe_eval (form);
4237 UNGCPRO;
4238 unbind_to (count, Qnil);
4239 }
4240
4241 if (NILP (form))
4242 return 0;
4243
4244 /* Handle `(height HEIGHT)' specifications. */
4245 if (CONSP (spec)
4246 && EQ (XCAR (spec), Qheight)
4247 && CONSP (XCDR (spec)))
4248 {
4249 if (it)
4250 {
4251 if (!FRAME_WINDOW_P (it->f))
4252 return 0;
4253
4254 it->font_height = XCAR (XCDR (spec));
4255 if (!NILP (it->font_height))
4256 {
4257 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4258 int new_height = -1;
4259
4260 if (CONSP (it->font_height)
4261 && (EQ (XCAR (it->font_height), Qplus)
4262 || EQ (XCAR (it->font_height), Qminus))
4263 && CONSP (XCDR (it->font_height))
4264 && INTEGERP (XCAR (XCDR (it->font_height))))
4265 {
4266 /* `(+ N)' or `(- N)' where N is an integer. */
4267 int steps = XINT (XCAR (XCDR (it->font_height)));
4268 if (EQ (XCAR (it->font_height), Qplus))
4269 steps = - steps;
4270 it->face_id = smaller_face (it->f, it->face_id, steps);
4271 }
4272 else if (FUNCTIONP (it->font_height))
4273 {
4274 /* Call function with current height as argument.
4275 Value is the new height. */
4276 Lisp_Object height;
4277 height = safe_call1 (it->font_height,
4278 face->lface[LFACE_HEIGHT_INDEX]);
4279 if (NUMBERP (height))
4280 new_height = XFLOATINT (height);
4281 }
4282 else if (NUMBERP (it->font_height))
4283 {
4284 /* Value is a multiple of the canonical char height. */
4285 struct face *f;
4286
4287 f = FACE_FROM_ID (it->f,
4288 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4289 new_height = (XFLOATINT (it->font_height)
4290 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4291 }
4292 else
4293 {
4294 /* Evaluate IT->font_height with `height' bound to the
4295 current specified height to get the new height. */
4296 int count = SPECPDL_INDEX ();
4297
4298 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4299 value = safe_eval (it->font_height);
4300 unbind_to (count, Qnil);
4301
4302 if (NUMBERP (value))
4303 new_height = XFLOATINT (value);
4304 }
4305
4306 if (new_height > 0)
4307 it->face_id = face_with_height (it->f, it->face_id, new_height);
4308 }
4309 }
4310
4311 return 0;
4312 }
4313
4314 /* Handle `(space-width WIDTH)'. */
4315 if (CONSP (spec)
4316 && EQ (XCAR (spec), Qspace_width)
4317 && CONSP (XCDR (spec)))
4318 {
4319 if (it)
4320 {
4321 if (!FRAME_WINDOW_P (it->f))
4322 return 0;
4323
4324 value = XCAR (XCDR (spec));
4325 if (NUMBERP (value) && XFLOATINT (value) > 0)
4326 it->space_width = value;
4327 }
4328
4329 return 0;
4330 }
4331
4332 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4333 if (CONSP (spec)
4334 && EQ (XCAR (spec), Qslice))
4335 {
4336 Lisp_Object tem;
4337
4338 if (it)
4339 {
4340 if (!FRAME_WINDOW_P (it->f))
4341 return 0;
4342
4343 if (tem = XCDR (spec), CONSP (tem))
4344 {
4345 it->slice.x = XCAR (tem);
4346 if (tem = XCDR (tem), CONSP (tem))
4347 {
4348 it->slice.y = XCAR (tem);
4349 if (tem = XCDR (tem), CONSP (tem))
4350 {
4351 it->slice.width = XCAR (tem);
4352 if (tem = XCDR (tem), CONSP (tem))
4353 it->slice.height = XCAR (tem);
4354 }
4355 }
4356 }
4357 }
4358
4359 return 0;
4360 }
4361
4362 /* Handle `(raise FACTOR)'. */
4363 if (CONSP (spec)
4364 && EQ (XCAR (spec), Qraise)
4365 && CONSP (XCDR (spec)))
4366 {
4367 if (it)
4368 {
4369 if (!FRAME_WINDOW_P (it->f))
4370 return 0;
4371
4372 #ifdef HAVE_WINDOW_SYSTEM
4373 value = XCAR (XCDR (spec));
4374 if (NUMBERP (value))
4375 {
4376 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4377 it->voffset = - (XFLOATINT (value)
4378 * (FONT_HEIGHT (face->font)));
4379 }
4380 #endif /* HAVE_WINDOW_SYSTEM */
4381 }
4382
4383 return 0;
4384 }
4385
4386 /* Don't handle the other kinds of display specifications
4387 inside a string that we got from a `display' property. */
4388 if (it && it->string_from_display_prop_p)
4389 return 0;
4390
4391 /* Characters having this form of property are not displayed, so
4392 we have to find the end of the property. */
4393 if (it)
4394 {
4395 start_pos = *position;
4396 *position = display_prop_end (it, object, start_pos);
4397 }
4398 value = Qnil;
4399
4400 /* Stop the scan at that end position--we assume that all
4401 text properties change there. */
4402 if (it)
4403 it->stop_charpos = position->charpos;
4404
4405 /* Handle `(left-fringe BITMAP [FACE])'
4406 and `(right-fringe BITMAP [FACE])'. */
4407 if (CONSP (spec)
4408 && (EQ (XCAR (spec), Qleft_fringe)
4409 || EQ (XCAR (spec), Qright_fringe))
4410 && CONSP (XCDR (spec)))
4411 {
4412 int fringe_bitmap;
4413
4414 if (it)
4415 {
4416 if (!FRAME_WINDOW_P (it->f))
4417 /* If we return here, POSITION has been advanced
4418 across the text with this property. */
4419 return 0;
4420 }
4421 else if (!frame_window_p)
4422 return 0;
4423
4424 #ifdef HAVE_WINDOW_SYSTEM
4425 value = XCAR (XCDR (spec));
4426 if (!SYMBOLP (value)
4427 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4428 /* If we return here, POSITION has been advanced
4429 across the text with this property. */
4430 return 0;
4431
4432 if (it)
4433 {
4434 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4435
4436 if (CONSP (XCDR (XCDR (spec))))
4437 {
4438 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4439 int face_id2 = lookup_derived_face (it->f, face_name,
4440 FRINGE_FACE_ID, 0);
4441 if (face_id2 >= 0)
4442 face_id = face_id2;
4443 }
4444
4445 /* Save current settings of IT so that we can restore them
4446 when we are finished with the glyph property value. */
4447 push_it (it, position);
4448
4449 it->area = TEXT_AREA;
4450 it->what = IT_IMAGE;
4451 it->image_id = -1; /* no image */
4452 it->position = start_pos;
4453 it->object = NILP (object) ? it->w->buffer : object;
4454 it->method = GET_FROM_IMAGE;
4455 it->from_overlay = Qnil;
4456 it->face_id = face_id;
4457 it->from_disp_prop_p = 1;
4458
4459 /* Say that we haven't consumed the characters with
4460 `display' property yet. The call to pop_it in
4461 set_iterator_to_next will clean this up. */
4462 *position = start_pos;
4463
4464 if (EQ (XCAR (spec), Qleft_fringe))
4465 {
4466 it->left_user_fringe_bitmap = fringe_bitmap;
4467 it->left_user_fringe_face_id = face_id;
4468 }
4469 else
4470 {
4471 it->right_user_fringe_bitmap = fringe_bitmap;
4472 it->right_user_fringe_face_id = face_id;
4473 }
4474 }
4475 #endif /* HAVE_WINDOW_SYSTEM */
4476 return 1;
4477 }
4478
4479 /* Prepare to handle `((margin left-margin) ...)',
4480 `((margin right-margin) ...)' and `((margin nil) ...)'
4481 prefixes for display specifications. */
4482 location = Qunbound;
4483 if (CONSP (spec) && CONSP (XCAR (spec)))
4484 {
4485 Lisp_Object tem;
4486
4487 value = XCDR (spec);
4488 if (CONSP (value))
4489 value = XCAR (value);
4490
4491 tem = XCAR (spec);
4492 if (EQ (XCAR (tem), Qmargin)
4493 && (tem = XCDR (tem),
4494 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4495 (NILP (tem)
4496 || EQ (tem, Qleft_margin)
4497 || EQ (tem, Qright_margin))))
4498 location = tem;
4499 }
4500
4501 if (EQ (location, Qunbound))
4502 {
4503 location = Qnil;
4504 value = spec;
4505 }
4506
4507 /* After this point, VALUE is the property after any
4508 margin prefix has been stripped. It must be a string,
4509 an image specification, or `(space ...)'.
4510
4511 LOCATION specifies where to display: `left-margin',
4512 `right-margin' or nil. */
4513
4514 valid_p = (STRINGP (value)
4515 #ifdef HAVE_WINDOW_SYSTEM
4516 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4517 && valid_image_p (value))
4518 #endif /* not HAVE_WINDOW_SYSTEM */
4519 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4520
4521 if (valid_p && !display_replaced_p)
4522 {
4523 if (!it)
4524 return 1;
4525
4526 /* Save current settings of IT so that we can restore them
4527 when we are finished with the glyph property value. */
4528 push_it (it, position);
4529 it->from_overlay = overlay;
4530 it->from_disp_prop_p = 1;
4531
4532 if (NILP (location))
4533 it->area = TEXT_AREA;
4534 else if (EQ (location, Qleft_margin))
4535 it->area = LEFT_MARGIN_AREA;
4536 else
4537 it->area = RIGHT_MARGIN_AREA;
4538
4539 if (STRINGP (value))
4540 {
4541 it->string = value;
4542 it->multibyte_p = STRING_MULTIBYTE (it->string);
4543 it->current.overlay_string_index = -1;
4544 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4545 it->end_charpos = it->string_nchars = SCHARS (it->string);
4546 it->method = GET_FROM_STRING;
4547 it->stop_charpos = 0;
4548 it->prev_stop = 0;
4549 it->base_level_stop = 0;
4550 it->string_from_display_prop_p = 1;
4551 /* Say that we haven't consumed the characters with
4552 `display' property yet. The call to pop_it in
4553 set_iterator_to_next will clean this up. */
4554 if (BUFFERP (object))
4555 *position = start_pos;
4556
4557 /* Force paragraph direction to be that of the parent
4558 object. If the parent object's paragraph direction is
4559 not yet determined, default to L2R. */
4560 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4561 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4562 else
4563 it->paragraph_embedding = L2R;
4564
4565 /* Set up the bidi iterator for this display string. */
4566 if (it->bidi_p)
4567 {
4568 it->bidi_it.string.lstring = it->string;
4569 it->bidi_it.string.s = NULL;
4570 it->bidi_it.string.schars = it->end_charpos;
4571 it->bidi_it.string.bufpos = bufpos;
4572 it->bidi_it.string.from_disp_str = 1;
4573 it->bidi_it.string.unibyte = !it->multibyte_p;
4574 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4575 }
4576 }
4577 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4578 {
4579 it->method = GET_FROM_STRETCH;
4580 it->object = value;
4581 *position = it->position = start_pos;
4582 }
4583 #ifdef HAVE_WINDOW_SYSTEM
4584 else
4585 {
4586 it->what = IT_IMAGE;
4587 it->image_id = lookup_image (it->f, value);
4588 it->position = start_pos;
4589 it->object = NILP (object) ? it->w->buffer : object;
4590 it->method = GET_FROM_IMAGE;
4591
4592 /* Say that we haven't consumed the characters with
4593 `display' property yet. The call to pop_it in
4594 set_iterator_to_next will clean this up. */
4595 *position = start_pos;
4596 }
4597 #endif /* HAVE_WINDOW_SYSTEM */
4598
4599 return 1;
4600 }
4601
4602 /* Invalid property or property not supported. Restore
4603 POSITION to what it was before. */
4604 *position = start_pos;
4605 return 0;
4606 }
4607
4608 /* Check if PROP is a display property value whose text should be
4609 treated as intangible. OVERLAY is the overlay from which PROP
4610 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4611 specify the buffer position covered by PROP. */
4612
4613 int
4614 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4615 EMACS_INT charpos, EMACS_INT bytepos)
4616 {
4617 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4618 struct text_pos position;
4619
4620 SET_TEXT_POS (position, charpos, bytepos);
4621 return handle_display_spec (NULL, prop, Qnil, overlay,
4622 &position, charpos, frame_window_p);
4623 }
4624
4625
4626 /* Return 1 if PROP is a display sub-property value containing STRING.
4627
4628 Implementation note: this and the following function are really
4629 special cases of handle_display_spec and
4630 handle_single_display_spec, and should ideally use the same code.
4631 Until they do, these two pairs must be consistent and must be
4632 modified in sync. */
4633
4634 static int
4635 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4636 {
4637 if (EQ (string, prop))
4638 return 1;
4639
4640 /* Skip over `when FORM'. */
4641 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4642 {
4643 prop = XCDR (prop);
4644 if (!CONSP (prop))
4645 return 0;
4646 /* Actually, the condition following `when' should be eval'ed,
4647 like handle_single_display_spec does, and we should return
4648 zero if it evaluates to nil. However, this function is
4649 called only when the buffer was already displayed and some
4650 glyph in the glyph matrix was found to come from a display
4651 string. Therefore, the condition was already evaluated, and
4652 the result was non-nil, otherwise the display string wouldn't
4653 have been displayed and we would have never been called for
4654 this property. Thus, we can skip the evaluation and assume
4655 its result is non-nil. */
4656 prop = XCDR (prop);
4657 }
4658
4659 if (CONSP (prop))
4660 /* Skip over `margin LOCATION'. */
4661 if (EQ (XCAR (prop), Qmargin))
4662 {
4663 prop = XCDR (prop);
4664 if (!CONSP (prop))
4665 return 0;
4666
4667 prop = XCDR (prop);
4668 if (!CONSP (prop))
4669 return 0;
4670 }
4671
4672 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4673 }
4674
4675
4676 /* Return 1 if STRING appears in the `display' property PROP. */
4677
4678 static int
4679 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4680 {
4681 if (CONSP (prop)
4682 && !EQ (XCAR (prop), Qwhen)
4683 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4684 {
4685 /* A list of sub-properties. */
4686 while (CONSP (prop))
4687 {
4688 if (single_display_spec_string_p (XCAR (prop), string))
4689 return 1;
4690 prop = XCDR (prop);
4691 }
4692 }
4693 else if (VECTORP (prop))
4694 {
4695 /* A vector of sub-properties. */
4696 int i;
4697 for (i = 0; i < ASIZE (prop); ++i)
4698 if (single_display_spec_string_p (AREF (prop, i), string))
4699 return 1;
4700 }
4701 else
4702 return single_display_spec_string_p (prop, string);
4703
4704 return 0;
4705 }
4706
4707 /* Look for STRING in overlays and text properties in the current
4708 buffer, between character positions FROM and TO (excluding TO).
4709 BACK_P non-zero means look back (in this case, TO is supposed to be
4710 less than FROM).
4711 Value is the first character position where STRING was found, or
4712 zero if it wasn't found before hitting TO.
4713
4714 This function may only use code that doesn't eval because it is
4715 called asynchronously from note_mouse_highlight. */
4716
4717 static EMACS_INT
4718 string_buffer_position_lim (Lisp_Object string,
4719 EMACS_INT from, EMACS_INT to, int back_p)
4720 {
4721 Lisp_Object limit, prop, pos;
4722 int found = 0;
4723
4724 pos = make_number (from);
4725
4726 if (!back_p) /* looking forward */
4727 {
4728 limit = make_number (min (to, ZV));
4729 while (!found && !EQ (pos, limit))
4730 {
4731 prop = Fget_char_property (pos, Qdisplay, Qnil);
4732 if (!NILP (prop) && display_prop_string_p (prop, string))
4733 found = 1;
4734 else
4735 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4736 limit);
4737 }
4738 }
4739 else /* looking back */
4740 {
4741 limit = make_number (max (to, BEGV));
4742 while (!found && !EQ (pos, limit))
4743 {
4744 prop = Fget_char_property (pos, Qdisplay, Qnil);
4745 if (!NILP (prop) && display_prop_string_p (prop, string))
4746 found = 1;
4747 else
4748 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4749 limit);
4750 }
4751 }
4752
4753 return found ? XINT (pos) : 0;
4754 }
4755
4756 /* Determine which buffer position in current buffer STRING comes from.
4757 AROUND_CHARPOS is an approximate position where it could come from.
4758 Value is the buffer position or 0 if it couldn't be determined.
4759
4760 This function is necessary because we don't record buffer positions
4761 in glyphs generated from strings (to keep struct glyph small).
4762 This function may only use code that doesn't eval because it is
4763 called asynchronously from note_mouse_highlight. */
4764
4765 static EMACS_INT
4766 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4767 {
4768 const int MAX_DISTANCE = 1000;
4769 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4770 around_charpos + MAX_DISTANCE,
4771 0);
4772
4773 if (!found)
4774 found = string_buffer_position_lim (string, around_charpos,
4775 around_charpos - MAX_DISTANCE, 1);
4776 return found;
4777 }
4778
4779
4780 \f
4781 /***********************************************************************
4782 `composition' property
4783 ***********************************************************************/
4784
4785 /* Set up iterator IT from `composition' property at its current
4786 position. Called from handle_stop. */
4787
4788 static enum prop_handled
4789 handle_composition_prop (struct it *it)
4790 {
4791 Lisp_Object prop, string;
4792 EMACS_INT pos, pos_byte, start, end;
4793
4794 if (STRINGP (it->string))
4795 {
4796 unsigned char *s;
4797
4798 pos = IT_STRING_CHARPOS (*it);
4799 pos_byte = IT_STRING_BYTEPOS (*it);
4800 string = it->string;
4801 s = SDATA (string) + pos_byte;
4802 it->c = STRING_CHAR (s);
4803 }
4804 else
4805 {
4806 pos = IT_CHARPOS (*it);
4807 pos_byte = IT_BYTEPOS (*it);
4808 string = Qnil;
4809 it->c = FETCH_CHAR (pos_byte);
4810 }
4811
4812 /* If there's a valid composition and point is not inside of the
4813 composition (in the case that the composition is from the current
4814 buffer), draw a glyph composed from the composition components. */
4815 if (find_composition (pos, -1, &start, &end, &prop, string)
4816 && COMPOSITION_VALID_P (start, end, prop)
4817 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4818 {
4819 if (start < pos)
4820 /* As we can't handle this situation (perhaps font-lock added
4821 a new composition), we just return here hoping that next
4822 redisplay will detect this composition much earlier. */
4823 return HANDLED_NORMALLY;
4824 if (start != pos)
4825 {
4826 if (STRINGP (it->string))
4827 pos_byte = string_char_to_byte (it->string, start);
4828 else
4829 pos_byte = CHAR_TO_BYTE (start);
4830 }
4831 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4832 prop, string);
4833
4834 if (it->cmp_it.id >= 0)
4835 {
4836 it->cmp_it.ch = -1;
4837 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4838 it->cmp_it.nglyphs = -1;
4839 }
4840 }
4841
4842 return HANDLED_NORMALLY;
4843 }
4844
4845
4846 \f
4847 /***********************************************************************
4848 Overlay strings
4849 ***********************************************************************/
4850
4851 /* The following structure is used to record overlay strings for
4852 later sorting in load_overlay_strings. */
4853
4854 struct overlay_entry
4855 {
4856 Lisp_Object overlay;
4857 Lisp_Object string;
4858 int priority;
4859 int after_string_p;
4860 };
4861
4862
4863 /* Set up iterator IT from overlay strings at its current position.
4864 Called from handle_stop. */
4865
4866 static enum prop_handled
4867 handle_overlay_change (struct it *it)
4868 {
4869 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4870 return HANDLED_RECOMPUTE_PROPS;
4871 else
4872 return HANDLED_NORMALLY;
4873 }
4874
4875
4876 /* Set up the next overlay string for delivery by IT, if there is an
4877 overlay string to deliver. Called by set_iterator_to_next when the
4878 end of the current overlay string is reached. If there are more
4879 overlay strings to display, IT->string and
4880 IT->current.overlay_string_index are set appropriately here.
4881 Otherwise IT->string is set to nil. */
4882
4883 static void
4884 next_overlay_string (struct it *it)
4885 {
4886 ++it->current.overlay_string_index;
4887 if (it->current.overlay_string_index == it->n_overlay_strings)
4888 {
4889 /* No more overlay strings. Restore IT's settings to what
4890 they were before overlay strings were processed, and
4891 continue to deliver from current_buffer. */
4892
4893 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4894 pop_it (it);
4895 xassert (it->sp > 0
4896 || (NILP (it->string)
4897 && it->method == GET_FROM_BUFFER
4898 && it->stop_charpos >= BEGV
4899 && it->stop_charpos <= it->end_charpos));
4900 it->current.overlay_string_index = -1;
4901 it->n_overlay_strings = 0;
4902 it->overlay_strings_charpos = -1;
4903
4904 /* If we're at the end of the buffer, record that we have
4905 processed the overlay strings there already, so that
4906 next_element_from_buffer doesn't try it again. */
4907 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4908 it->overlay_strings_at_end_processed_p = 1;
4909 }
4910 else
4911 {
4912 /* There are more overlay strings to process. If
4913 IT->current.overlay_string_index has advanced to a position
4914 where we must load IT->overlay_strings with more strings, do
4915 it. We must load at the IT->overlay_strings_charpos where
4916 IT->n_overlay_strings was originally computed; when invisible
4917 text is present, this might not be IT_CHARPOS (Bug#7016). */
4918 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4919
4920 if (it->current.overlay_string_index && i == 0)
4921 load_overlay_strings (it, it->overlay_strings_charpos);
4922
4923 /* Initialize IT to deliver display elements from the overlay
4924 string. */
4925 it->string = it->overlay_strings[i];
4926 it->multibyte_p = STRING_MULTIBYTE (it->string);
4927 SET_TEXT_POS (it->current.string_pos, 0, 0);
4928 it->method = GET_FROM_STRING;
4929 it->stop_charpos = 0;
4930 if (it->cmp_it.stop_pos >= 0)
4931 it->cmp_it.stop_pos = 0;
4932 it->prev_stop = 0;
4933 it->base_level_stop = 0;
4934
4935 /* Set up the bidi iterator for this overlay string. */
4936 if (it->bidi_p)
4937 {
4938 it->bidi_it.string.lstring = it->string;
4939 it->bidi_it.string.s = NULL;
4940 it->bidi_it.string.schars = SCHARS (it->string);
4941 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4942 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4943 it->bidi_it.string.unibyte = !it->multibyte_p;
4944 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4945 }
4946 }
4947
4948 CHECK_IT (it);
4949 }
4950
4951
4952 /* Compare two overlay_entry structures E1 and E2. Used as a
4953 comparison function for qsort in load_overlay_strings. Overlay
4954 strings for the same position are sorted so that
4955
4956 1. All after-strings come in front of before-strings, except
4957 when they come from the same overlay.
4958
4959 2. Within after-strings, strings are sorted so that overlay strings
4960 from overlays with higher priorities come first.
4961
4962 2. Within before-strings, strings are sorted so that overlay
4963 strings from overlays with higher priorities come last.
4964
4965 Value is analogous to strcmp. */
4966
4967
4968 static int
4969 compare_overlay_entries (const void *e1, const void *e2)
4970 {
4971 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4972 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4973 int result;
4974
4975 if (entry1->after_string_p != entry2->after_string_p)
4976 {
4977 /* Let after-strings appear in front of before-strings if
4978 they come from different overlays. */
4979 if (EQ (entry1->overlay, entry2->overlay))
4980 result = entry1->after_string_p ? 1 : -1;
4981 else
4982 result = entry1->after_string_p ? -1 : 1;
4983 }
4984 else if (entry1->after_string_p)
4985 /* After-strings sorted in order of decreasing priority. */
4986 result = entry2->priority - entry1->priority;
4987 else
4988 /* Before-strings sorted in order of increasing priority. */
4989 result = entry1->priority - entry2->priority;
4990
4991 return result;
4992 }
4993
4994
4995 /* Load the vector IT->overlay_strings with overlay strings from IT's
4996 current buffer position, or from CHARPOS if that is > 0. Set
4997 IT->n_overlays to the total number of overlay strings found.
4998
4999 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5000 a time. On entry into load_overlay_strings,
5001 IT->current.overlay_string_index gives the number of overlay
5002 strings that have already been loaded by previous calls to this
5003 function.
5004
5005 IT->add_overlay_start contains an additional overlay start
5006 position to consider for taking overlay strings from, if non-zero.
5007 This position comes into play when the overlay has an `invisible'
5008 property, and both before and after-strings. When we've skipped to
5009 the end of the overlay, because of its `invisible' property, we
5010 nevertheless want its before-string to appear.
5011 IT->add_overlay_start will contain the overlay start position
5012 in this case.
5013
5014 Overlay strings are sorted so that after-string strings come in
5015 front of before-string strings. Within before and after-strings,
5016 strings are sorted by overlay priority. See also function
5017 compare_overlay_entries. */
5018
5019 static void
5020 load_overlay_strings (struct it *it, EMACS_INT charpos)
5021 {
5022 Lisp_Object overlay, window, str, invisible;
5023 struct Lisp_Overlay *ov;
5024 EMACS_INT start, end;
5025 int size = 20;
5026 int n = 0, i, j, invis_p;
5027 struct overlay_entry *entries
5028 = (struct overlay_entry *) alloca (size * sizeof *entries);
5029
5030 if (charpos <= 0)
5031 charpos = IT_CHARPOS (*it);
5032
5033 /* Append the overlay string STRING of overlay OVERLAY to vector
5034 `entries' which has size `size' and currently contains `n'
5035 elements. AFTER_P non-zero means STRING is an after-string of
5036 OVERLAY. */
5037 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5038 do \
5039 { \
5040 Lisp_Object priority; \
5041 \
5042 if (n == size) \
5043 { \
5044 int new_size = 2 * size; \
5045 struct overlay_entry *old = entries; \
5046 entries = \
5047 (struct overlay_entry *) alloca (new_size \
5048 * sizeof *entries); \
5049 memcpy (entries, old, size * sizeof *entries); \
5050 size = new_size; \
5051 } \
5052 \
5053 entries[n].string = (STRING); \
5054 entries[n].overlay = (OVERLAY); \
5055 priority = Foverlay_get ((OVERLAY), Qpriority); \
5056 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5057 entries[n].after_string_p = (AFTER_P); \
5058 ++n; \
5059 } \
5060 while (0)
5061
5062 /* Process overlay before the overlay center. */
5063 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5064 {
5065 XSETMISC (overlay, ov);
5066 xassert (OVERLAYP (overlay));
5067 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5068 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5069
5070 if (end < charpos)
5071 break;
5072
5073 /* Skip this overlay if it doesn't start or end at IT's current
5074 position. */
5075 if (end != charpos && start != charpos)
5076 continue;
5077
5078 /* Skip this overlay if it doesn't apply to IT->w. */
5079 window = Foverlay_get (overlay, Qwindow);
5080 if (WINDOWP (window) && XWINDOW (window) != it->w)
5081 continue;
5082
5083 /* If the text ``under'' the overlay is invisible, both before-
5084 and after-strings from this overlay are visible; start and
5085 end position are indistinguishable. */
5086 invisible = Foverlay_get (overlay, Qinvisible);
5087 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5088
5089 /* If overlay has a non-empty before-string, record it. */
5090 if ((start == charpos || (end == charpos && invis_p))
5091 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5092 && SCHARS (str))
5093 RECORD_OVERLAY_STRING (overlay, str, 0);
5094
5095 /* If overlay has a non-empty after-string, record it. */
5096 if ((end == charpos || (start == charpos && invis_p))
5097 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5098 && SCHARS (str))
5099 RECORD_OVERLAY_STRING (overlay, str, 1);
5100 }
5101
5102 /* Process overlays after the overlay center. */
5103 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5104 {
5105 XSETMISC (overlay, ov);
5106 xassert (OVERLAYP (overlay));
5107 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5108 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5109
5110 if (start > charpos)
5111 break;
5112
5113 /* Skip this overlay if it doesn't start or end at IT's current
5114 position. */
5115 if (end != charpos && start != charpos)
5116 continue;
5117
5118 /* Skip this overlay if it doesn't apply to IT->w. */
5119 window = Foverlay_get (overlay, Qwindow);
5120 if (WINDOWP (window) && XWINDOW (window) != it->w)
5121 continue;
5122
5123 /* If the text ``under'' the overlay is invisible, it has a zero
5124 dimension, and both before- and after-strings apply. */
5125 invisible = Foverlay_get (overlay, Qinvisible);
5126 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5127
5128 /* If overlay has a non-empty before-string, record it. */
5129 if ((start == charpos || (end == charpos && invis_p))
5130 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5131 && SCHARS (str))
5132 RECORD_OVERLAY_STRING (overlay, str, 0);
5133
5134 /* If overlay has a non-empty after-string, record it. */
5135 if ((end == charpos || (start == charpos && invis_p))
5136 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5137 && SCHARS (str))
5138 RECORD_OVERLAY_STRING (overlay, str, 1);
5139 }
5140
5141 #undef RECORD_OVERLAY_STRING
5142
5143 /* Sort entries. */
5144 if (n > 1)
5145 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5146
5147 /* Record number of overlay strings, and where we computed it. */
5148 it->n_overlay_strings = n;
5149 it->overlay_strings_charpos = charpos;
5150
5151 /* IT->current.overlay_string_index is the number of overlay strings
5152 that have already been consumed by IT. Copy some of the
5153 remaining overlay strings to IT->overlay_strings. */
5154 i = 0;
5155 j = it->current.overlay_string_index;
5156 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5157 {
5158 it->overlay_strings[i] = entries[j].string;
5159 it->string_overlays[i++] = entries[j++].overlay;
5160 }
5161
5162 CHECK_IT (it);
5163 }
5164
5165
5166 /* Get the first chunk of overlay strings at IT's current buffer
5167 position, or at CHARPOS if that is > 0. Value is non-zero if at
5168 least one overlay string was found. */
5169
5170 static int
5171 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5172 {
5173 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5174 process. This fills IT->overlay_strings with strings, and sets
5175 IT->n_overlay_strings to the total number of strings to process.
5176 IT->pos.overlay_string_index has to be set temporarily to zero
5177 because load_overlay_strings needs this; it must be set to -1
5178 when no overlay strings are found because a zero value would
5179 indicate a position in the first overlay string. */
5180 it->current.overlay_string_index = 0;
5181 load_overlay_strings (it, charpos);
5182
5183 /* If we found overlay strings, set up IT to deliver display
5184 elements from the first one. Otherwise set up IT to deliver
5185 from current_buffer. */
5186 if (it->n_overlay_strings)
5187 {
5188 /* Make sure we know settings in current_buffer, so that we can
5189 restore meaningful values when we're done with the overlay
5190 strings. */
5191 if (compute_stop_p)
5192 compute_stop_pos (it);
5193 xassert (it->face_id >= 0);
5194
5195 /* Save IT's settings. They are restored after all overlay
5196 strings have been processed. */
5197 xassert (!compute_stop_p || it->sp == 0);
5198
5199 /* When called from handle_stop, there might be an empty display
5200 string loaded. In that case, don't bother saving it. */
5201 if (!STRINGP (it->string) || SCHARS (it->string))
5202 push_it (it, NULL);
5203
5204 /* Set up IT to deliver display elements from the first overlay
5205 string. */
5206 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5207 it->string = it->overlay_strings[0];
5208 it->from_overlay = Qnil;
5209 it->stop_charpos = 0;
5210 xassert (STRINGP (it->string));
5211 it->end_charpos = SCHARS (it->string);
5212 it->prev_stop = 0;
5213 it->base_level_stop = 0;
5214 it->multibyte_p = STRING_MULTIBYTE (it->string);
5215 it->method = GET_FROM_STRING;
5216 it->from_disp_prop_p = 0;
5217
5218 /* Force paragraph direction to be that of the parent
5219 buffer. */
5220 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5221 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5222 else
5223 it->paragraph_embedding = L2R;
5224
5225 /* Set up the bidi iterator for this overlay string. */
5226 if (it->bidi_p)
5227 {
5228 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5229
5230 it->bidi_it.string.lstring = it->string;
5231 it->bidi_it.string.s = NULL;
5232 it->bidi_it.string.schars = SCHARS (it->string);
5233 it->bidi_it.string.bufpos = pos;
5234 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5235 it->bidi_it.string.unibyte = !it->multibyte_p;
5236 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5237 }
5238 return 1;
5239 }
5240
5241 it->current.overlay_string_index = -1;
5242 return 0;
5243 }
5244
5245 static int
5246 get_overlay_strings (struct it *it, EMACS_INT charpos)
5247 {
5248 it->string = Qnil;
5249 it->method = GET_FROM_BUFFER;
5250
5251 (void) get_overlay_strings_1 (it, charpos, 1);
5252
5253 CHECK_IT (it);
5254
5255 /* Value is non-zero if we found at least one overlay string. */
5256 return STRINGP (it->string);
5257 }
5258
5259
5260 \f
5261 /***********************************************************************
5262 Saving and restoring state
5263 ***********************************************************************/
5264
5265 /* Save current settings of IT on IT->stack. Called, for example,
5266 before setting up IT for an overlay string, to be able to restore
5267 IT's settings to what they were after the overlay string has been
5268 processed. If POSITION is non-NULL, it is the position to save on
5269 the stack instead of IT->position. */
5270
5271 static void
5272 push_it (struct it *it, struct text_pos *position)
5273 {
5274 struct iterator_stack_entry *p;
5275
5276 xassert (it->sp < IT_STACK_SIZE);
5277 p = it->stack + it->sp;
5278
5279 p->stop_charpos = it->stop_charpos;
5280 p->prev_stop = it->prev_stop;
5281 p->base_level_stop = it->base_level_stop;
5282 p->cmp_it = it->cmp_it;
5283 xassert (it->face_id >= 0);
5284 p->face_id = it->face_id;
5285 p->string = it->string;
5286 p->method = it->method;
5287 p->from_overlay = it->from_overlay;
5288 switch (p->method)
5289 {
5290 case GET_FROM_IMAGE:
5291 p->u.image.object = it->object;
5292 p->u.image.image_id = it->image_id;
5293 p->u.image.slice = it->slice;
5294 break;
5295 case GET_FROM_STRETCH:
5296 p->u.stretch.object = it->object;
5297 break;
5298 }
5299 p->position = position ? *position : it->position;
5300 p->current = it->current;
5301 p->end_charpos = it->end_charpos;
5302 p->string_nchars = it->string_nchars;
5303 p->area = it->area;
5304 p->multibyte_p = it->multibyte_p;
5305 p->avoid_cursor_p = it->avoid_cursor_p;
5306 p->space_width = it->space_width;
5307 p->font_height = it->font_height;
5308 p->voffset = it->voffset;
5309 p->string_from_display_prop_p = it->string_from_display_prop_p;
5310 p->display_ellipsis_p = 0;
5311 p->line_wrap = it->line_wrap;
5312 p->bidi_p = it->bidi_p;
5313 p->paragraph_embedding = it->paragraph_embedding;
5314 p->from_disp_prop_p = it->from_disp_prop_p;
5315 ++it->sp;
5316
5317 /* Save the state of the bidi iterator as well. */
5318 if (it->bidi_p)
5319 bidi_push_it (&it->bidi_it);
5320 }
5321
5322 static void
5323 iterate_out_of_display_property (struct it *it)
5324 {
5325 int buffer_p = BUFFERP (it->object);
5326 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5327 EMACS_INT bob = (buffer_p ? BEGV : 0);
5328
5329 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5330
5331 /* Maybe initialize paragraph direction. If we are at the beginning
5332 of a new paragraph, next_element_from_buffer may not have a
5333 chance to do that. */
5334 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5335 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5336 /* prev_stop can be zero, so check against BEGV as well. */
5337 while (it->bidi_it.charpos >= bob
5338 && it->prev_stop <= it->bidi_it.charpos
5339 && it->bidi_it.charpos < CHARPOS (it->position)
5340 && it->bidi_it.charpos < eob)
5341 bidi_move_to_visually_next (&it->bidi_it);
5342 /* Record the stop_pos we just crossed, for when we cross it
5343 back, maybe. */
5344 if (it->bidi_it.charpos > CHARPOS (it->position))
5345 it->prev_stop = CHARPOS (it->position);
5346 /* If we ended up not where pop_it put us, resync IT's
5347 positional members with the bidi iterator. */
5348 if (it->bidi_it.charpos != CHARPOS (it->position))
5349 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5350 if (buffer_p)
5351 it->current.pos = it->position;
5352 else
5353 it->current.string_pos = it->position;
5354 }
5355
5356 /* Restore IT's settings from IT->stack. Called, for example, when no
5357 more overlay strings must be processed, and we return to delivering
5358 display elements from a buffer, or when the end of a string from a
5359 `display' property is reached and we return to delivering display
5360 elements from an overlay string, or from a buffer. */
5361
5362 static void
5363 pop_it (struct it *it)
5364 {
5365 struct iterator_stack_entry *p;
5366 int from_display_prop = it->from_disp_prop_p;
5367
5368 xassert (it->sp > 0);
5369 --it->sp;
5370 p = it->stack + it->sp;
5371 it->stop_charpos = p->stop_charpos;
5372 it->prev_stop = p->prev_stop;
5373 it->base_level_stop = p->base_level_stop;
5374 it->cmp_it = p->cmp_it;
5375 it->face_id = p->face_id;
5376 it->current = p->current;
5377 it->position = p->position;
5378 it->string = p->string;
5379 it->from_overlay = p->from_overlay;
5380 if (NILP (it->string))
5381 SET_TEXT_POS (it->current.string_pos, -1, -1);
5382 it->method = p->method;
5383 switch (it->method)
5384 {
5385 case GET_FROM_IMAGE:
5386 it->image_id = p->u.image.image_id;
5387 it->object = p->u.image.object;
5388 it->slice = p->u.image.slice;
5389 break;
5390 case GET_FROM_STRETCH:
5391 it->object = p->u.stretch.object;
5392 break;
5393 case GET_FROM_BUFFER:
5394 it->object = it->w->buffer;
5395 break;
5396 case GET_FROM_STRING:
5397 it->object = it->string;
5398 break;
5399 case GET_FROM_DISPLAY_VECTOR:
5400 if (it->s)
5401 it->method = GET_FROM_C_STRING;
5402 else if (STRINGP (it->string))
5403 it->method = GET_FROM_STRING;
5404 else
5405 {
5406 it->method = GET_FROM_BUFFER;
5407 it->object = it->w->buffer;
5408 }
5409 }
5410 it->end_charpos = p->end_charpos;
5411 it->string_nchars = p->string_nchars;
5412 it->area = p->area;
5413 it->multibyte_p = p->multibyte_p;
5414 it->avoid_cursor_p = p->avoid_cursor_p;
5415 it->space_width = p->space_width;
5416 it->font_height = p->font_height;
5417 it->voffset = p->voffset;
5418 it->string_from_display_prop_p = p->string_from_display_prop_p;
5419 it->line_wrap = p->line_wrap;
5420 it->bidi_p = p->bidi_p;
5421 it->paragraph_embedding = p->paragraph_embedding;
5422 it->from_disp_prop_p = p->from_disp_prop_p;
5423 if (it->bidi_p)
5424 {
5425 bidi_pop_it (&it->bidi_it);
5426 /* Bidi-iterate until we get out of the portion of text, if any,
5427 covered by a `display' text property or by an overlay with
5428 `display' property. (We cannot just jump there, because the
5429 internal coherency of the bidi iterator state can not be
5430 preserved across such jumps.) We also must determine the
5431 paragraph base direction if the overlay we just processed is
5432 at the beginning of a new paragraph. */
5433 if (from_display_prop
5434 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5435 iterate_out_of_display_property (it);
5436
5437 xassert ((BUFFERP (it->object)
5438 && IT_CHARPOS (*it) == it->bidi_it.charpos
5439 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5440 || (STRINGP (it->object)
5441 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5442 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos));
5443 }
5444 }
5445
5446
5447 \f
5448 /***********************************************************************
5449 Moving over lines
5450 ***********************************************************************/
5451
5452 /* Set IT's current position to the previous line start. */
5453
5454 static void
5455 back_to_previous_line_start (struct it *it)
5456 {
5457 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5458 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5459 }
5460
5461
5462 /* Move IT to the next line start.
5463
5464 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5465 we skipped over part of the text (as opposed to moving the iterator
5466 continuously over the text). Otherwise, don't change the value
5467 of *SKIPPED_P.
5468
5469 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5470 iterator on the newline, if it was found.
5471
5472 Newlines may come from buffer text, overlay strings, or strings
5473 displayed via the `display' property. That's the reason we can't
5474 simply use find_next_newline_no_quit.
5475
5476 Note that this function may not skip over invisible text that is so
5477 because of text properties and immediately follows a newline. If
5478 it would, function reseat_at_next_visible_line_start, when called
5479 from set_iterator_to_next, would effectively make invisible
5480 characters following a newline part of the wrong glyph row, which
5481 leads to wrong cursor motion. */
5482
5483 static int
5484 forward_to_next_line_start (struct it *it, int *skipped_p,
5485 struct bidi_it *bidi_it_prev)
5486 {
5487 EMACS_INT old_selective;
5488 int newline_found_p, n;
5489 const int MAX_NEWLINE_DISTANCE = 500;
5490
5491 /* If already on a newline, just consume it to avoid unintended
5492 skipping over invisible text below. */
5493 if (it->what == IT_CHARACTER
5494 && it->c == '\n'
5495 && CHARPOS (it->position) == IT_CHARPOS (*it))
5496 {
5497 if (it->bidi_p && bidi_it_prev)
5498 *bidi_it_prev = it->bidi_it;
5499 set_iterator_to_next (it, 0);
5500 it->c = 0;
5501 return 1;
5502 }
5503
5504 /* Don't handle selective display in the following. It's (a)
5505 unnecessary because it's done by the caller, and (b) leads to an
5506 infinite recursion because next_element_from_ellipsis indirectly
5507 calls this function. */
5508 old_selective = it->selective;
5509 it->selective = 0;
5510
5511 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5512 from buffer text. */
5513 for (n = newline_found_p = 0;
5514 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5515 n += STRINGP (it->string) ? 0 : 1)
5516 {
5517 if (!get_next_display_element (it))
5518 return 0;
5519 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5520 if (newline_found_p && it->bidi_p && bidi_it_prev)
5521 *bidi_it_prev = it->bidi_it;
5522 set_iterator_to_next (it, 0);
5523 }
5524
5525 /* If we didn't find a newline near enough, see if we can use a
5526 short-cut. */
5527 if (!newline_found_p)
5528 {
5529 EMACS_INT start = IT_CHARPOS (*it);
5530 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5531 Lisp_Object pos;
5532
5533 xassert (!STRINGP (it->string));
5534
5535 /* If there isn't any `display' property in sight, and no
5536 overlays, we can just use the position of the newline in
5537 buffer text. */
5538 if (it->stop_charpos >= limit
5539 || ((pos = Fnext_single_property_change (make_number (start),
5540 Qdisplay, Qnil,
5541 make_number (limit)),
5542 NILP (pos))
5543 && next_overlay_change (start) == ZV))
5544 {
5545 if (!it->bidi_p)
5546 {
5547 IT_CHARPOS (*it) = limit;
5548 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5549 }
5550 else
5551 {
5552 struct bidi_it bprev;
5553
5554 /* Help bidi.c avoid expensive searches for display
5555 properties and overlays, by telling it that there are
5556 none up to `limit'. */
5557 if (it->bidi_it.disp_pos < limit)
5558 {
5559 it->bidi_it.disp_pos = limit;
5560 it->bidi_it.disp_prop_p = 0;
5561 }
5562 do {
5563 bprev = it->bidi_it;
5564 bidi_move_to_visually_next (&it->bidi_it);
5565 } while (it->bidi_it.charpos != limit);
5566 IT_CHARPOS (*it) = limit;
5567 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5568 if (bidi_it_prev)
5569 *bidi_it_prev = bprev;
5570 }
5571 *skipped_p = newline_found_p = 1;
5572 }
5573 else
5574 {
5575 while (get_next_display_element (it)
5576 && !newline_found_p)
5577 {
5578 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5579 if (newline_found_p && it->bidi_p && bidi_it_prev)
5580 *bidi_it_prev = it->bidi_it;
5581 set_iterator_to_next (it, 0);
5582 }
5583 }
5584 }
5585
5586 it->selective = old_selective;
5587 return newline_found_p;
5588 }
5589
5590
5591 /* Set IT's current position to the previous visible line start. Skip
5592 invisible text that is so either due to text properties or due to
5593 selective display. Caution: this does not change IT->current_x and
5594 IT->hpos. */
5595
5596 static void
5597 back_to_previous_visible_line_start (struct it *it)
5598 {
5599 while (IT_CHARPOS (*it) > BEGV)
5600 {
5601 back_to_previous_line_start (it);
5602
5603 if (IT_CHARPOS (*it) <= BEGV)
5604 break;
5605
5606 /* If selective > 0, then lines indented more than its value are
5607 invisible. */
5608 if (it->selective > 0
5609 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5610 it->selective))
5611 continue;
5612
5613 /* Check the newline before point for invisibility. */
5614 {
5615 Lisp_Object prop;
5616 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5617 Qinvisible, it->window);
5618 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5619 continue;
5620 }
5621
5622 if (IT_CHARPOS (*it) <= BEGV)
5623 break;
5624
5625 {
5626 struct it it2;
5627 void *it2data = NULL;
5628 EMACS_INT pos;
5629 EMACS_INT beg, end;
5630 Lisp_Object val, overlay;
5631
5632 SAVE_IT (it2, *it, it2data);
5633
5634 /* If newline is part of a composition, continue from start of composition */
5635 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5636 && beg < IT_CHARPOS (*it))
5637 goto replaced;
5638
5639 /* If newline is replaced by a display property, find start of overlay
5640 or interval and continue search from that point. */
5641 pos = --IT_CHARPOS (it2);
5642 --IT_BYTEPOS (it2);
5643 it2.sp = 0;
5644 bidi_unshelve_cache (NULL, 0);
5645 it2.string_from_display_prop_p = 0;
5646 it2.from_disp_prop_p = 0;
5647 if (handle_display_prop (&it2) == HANDLED_RETURN
5648 && !NILP (val = get_char_property_and_overlay
5649 (make_number (pos), Qdisplay, Qnil, &overlay))
5650 && (OVERLAYP (overlay)
5651 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5652 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5653 {
5654 RESTORE_IT (it, it, it2data);
5655 goto replaced;
5656 }
5657
5658 /* Newline is not replaced by anything -- so we are done. */
5659 RESTORE_IT (it, it, it2data);
5660 break;
5661
5662 replaced:
5663 if (beg < BEGV)
5664 beg = BEGV;
5665 IT_CHARPOS (*it) = beg;
5666 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5667 }
5668 }
5669
5670 it->continuation_lines_width = 0;
5671
5672 xassert (IT_CHARPOS (*it) >= BEGV);
5673 xassert (IT_CHARPOS (*it) == BEGV
5674 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5675 CHECK_IT (it);
5676 }
5677
5678
5679 /* Reseat iterator IT at the previous visible line start. Skip
5680 invisible text that is so either due to text properties or due to
5681 selective display. At the end, update IT's overlay information,
5682 face information etc. */
5683
5684 void
5685 reseat_at_previous_visible_line_start (struct it *it)
5686 {
5687 back_to_previous_visible_line_start (it);
5688 reseat (it, it->current.pos, 1);
5689 CHECK_IT (it);
5690 }
5691
5692
5693 /* Reseat iterator IT on the next visible line start in the current
5694 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5695 preceding the line start. Skip over invisible text that is so
5696 because of selective display. Compute faces, overlays etc at the
5697 new position. Note that this function does not skip over text that
5698 is invisible because of text properties. */
5699
5700 static void
5701 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5702 {
5703 int newline_found_p, skipped_p = 0;
5704 struct bidi_it bidi_it_prev;
5705
5706 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5707
5708 /* Skip over lines that are invisible because they are indented
5709 more than the value of IT->selective. */
5710 if (it->selective > 0)
5711 while (IT_CHARPOS (*it) < ZV
5712 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5713 it->selective))
5714 {
5715 xassert (IT_BYTEPOS (*it) == BEGV
5716 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5717 newline_found_p =
5718 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5719 }
5720
5721 /* Position on the newline if that's what's requested. */
5722 if (on_newline_p && newline_found_p)
5723 {
5724 if (STRINGP (it->string))
5725 {
5726 if (IT_STRING_CHARPOS (*it) > 0)
5727 {
5728 if (!it->bidi_p)
5729 {
5730 --IT_STRING_CHARPOS (*it);
5731 --IT_STRING_BYTEPOS (*it);
5732 }
5733 else
5734 {
5735 /* We need to restore the bidi iterator to the state
5736 it had on the newline, and resync the IT's
5737 position with that. */
5738 it->bidi_it = bidi_it_prev;
5739 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
5740 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
5741 }
5742 }
5743 }
5744 else if (IT_CHARPOS (*it) > BEGV)
5745 {
5746 if (!it->bidi_p)
5747 {
5748 --IT_CHARPOS (*it);
5749 --IT_BYTEPOS (*it);
5750 }
5751 else
5752 {
5753 /* We need to restore the bidi iterator to the state it
5754 had on the newline and resync IT with that. */
5755 it->bidi_it = bidi_it_prev;
5756 IT_CHARPOS (*it) = it->bidi_it.charpos;
5757 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5758 }
5759 reseat (it, it->current.pos, 0);
5760 }
5761 }
5762 else if (skipped_p)
5763 reseat (it, it->current.pos, 0);
5764
5765 CHECK_IT (it);
5766 }
5767
5768
5769 \f
5770 /***********************************************************************
5771 Changing an iterator's position
5772 ***********************************************************************/
5773
5774 /* Change IT's current position to POS in current_buffer. If FORCE_P
5775 is non-zero, always check for text properties at the new position.
5776 Otherwise, text properties are only looked up if POS >=
5777 IT->check_charpos of a property. */
5778
5779 static void
5780 reseat (struct it *it, struct text_pos pos, int force_p)
5781 {
5782 EMACS_INT original_pos = IT_CHARPOS (*it);
5783
5784 reseat_1 (it, pos, 0);
5785
5786 /* Determine where to check text properties. Avoid doing it
5787 where possible because text property lookup is very expensive. */
5788 if (force_p
5789 || CHARPOS (pos) > it->stop_charpos
5790 || CHARPOS (pos) < original_pos)
5791 {
5792 if (it->bidi_p)
5793 {
5794 /* For bidi iteration, we need to prime prev_stop and
5795 base_level_stop with our best estimations. */
5796 /* Implementation note: Of course, POS is not necessarily a
5797 stop position, so assigning prev_pos to it is a lie; we
5798 should have called compute_stop_backwards. However, if
5799 the current buffer does not include any R2L characters,
5800 that call would be a waste of cycles, because the
5801 iterator will never move back, and thus never cross this
5802 "fake" stop position. So we delay that backward search
5803 until the time we really need it, in next_element_from_buffer. */
5804 if (CHARPOS (pos) != it->prev_stop)
5805 it->prev_stop = CHARPOS (pos);
5806 if (CHARPOS (pos) < it->base_level_stop)
5807 it->base_level_stop = 0; /* meaning it's unknown */
5808 handle_stop (it);
5809 }
5810 else
5811 {
5812 handle_stop (it);
5813 it->prev_stop = it->base_level_stop = 0;
5814 }
5815
5816 }
5817
5818 CHECK_IT (it);
5819 }
5820
5821
5822 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5823 IT->stop_pos to POS, also. */
5824
5825 static void
5826 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5827 {
5828 /* Don't call this function when scanning a C string. */
5829 xassert (it->s == NULL);
5830
5831 /* POS must be a reasonable value. */
5832 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5833
5834 it->current.pos = it->position = pos;
5835 it->end_charpos = ZV;
5836 it->dpvec = NULL;
5837 it->current.dpvec_index = -1;
5838 it->current.overlay_string_index = -1;
5839 IT_STRING_CHARPOS (*it) = -1;
5840 IT_STRING_BYTEPOS (*it) = -1;
5841 it->string = Qnil;
5842 it->method = GET_FROM_BUFFER;
5843 it->object = it->w->buffer;
5844 it->area = TEXT_AREA;
5845 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5846 it->sp = 0;
5847 it->string_from_display_prop_p = 0;
5848 it->from_disp_prop_p = 0;
5849 it->face_before_selective_p = 0;
5850 if (it->bidi_p)
5851 {
5852 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5853 &it->bidi_it);
5854 bidi_unshelve_cache (NULL, 0);
5855 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5856 it->bidi_it.string.s = NULL;
5857 it->bidi_it.string.lstring = Qnil;
5858 it->bidi_it.string.bufpos = 0;
5859 it->bidi_it.string.unibyte = 0;
5860 }
5861
5862 if (set_stop_p)
5863 {
5864 it->stop_charpos = CHARPOS (pos);
5865 it->base_level_stop = CHARPOS (pos);
5866 }
5867 }
5868
5869
5870 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5871 If S is non-null, it is a C string to iterate over. Otherwise,
5872 STRING gives a Lisp string to iterate over.
5873
5874 If PRECISION > 0, don't return more then PRECISION number of
5875 characters from the string.
5876
5877 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5878 characters have been returned. FIELD_WIDTH < 0 means an infinite
5879 field width.
5880
5881 MULTIBYTE = 0 means disable processing of multibyte characters,
5882 MULTIBYTE > 0 means enable it,
5883 MULTIBYTE < 0 means use IT->multibyte_p.
5884
5885 IT must be initialized via a prior call to init_iterator before
5886 calling this function. */
5887
5888 static void
5889 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5890 EMACS_INT charpos, EMACS_INT precision, int field_width,
5891 int multibyte)
5892 {
5893 /* No region in strings. */
5894 it->region_beg_charpos = it->region_end_charpos = -1;
5895
5896 /* No text property checks performed by default, but see below. */
5897 it->stop_charpos = -1;
5898
5899 /* Set iterator position and end position. */
5900 memset (&it->current, 0, sizeof it->current);
5901 it->current.overlay_string_index = -1;
5902 it->current.dpvec_index = -1;
5903 xassert (charpos >= 0);
5904
5905 /* If STRING is specified, use its multibyteness, otherwise use the
5906 setting of MULTIBYTE, if specified. */
5907 if (multibyte >= 0)
5908 it->multibyte_p = multibyte > 0;
5909
5910 /* Bidirectional reordering of strings is controlled by the default
5911 value of bidi-display-reordering. */
5912 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
5913
5914 if (s == NULL)
5915 {
5916 xassert (STRINGP (string));
5917 it->string = string;
5918 it->s = NULL;
5919 it->end_charpos = it->string_nchars = SCHARS (string);
5920 it->method = GET_FROM_STRING;
5921 it->current.string_pos = string_pos (charpos, string);
5922
5923 if (it->bidi_p)
5924 {
5925 it->bidi_it.string.lstring = string;
5926 it->bidi_it.string.s = NULL;
5927 it->bidi_it.string.schars = it->end_charpos;
5928 it->bidi_it.string.bufpos = 0;
5929 it->bidi_it.string.from_disp_str = 0;
5930 it->bidi_it.string.unibyte = !it->multibyte_p;
5931 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5932 FRAME_WINDOW_P (it->f), &it->bidi_it);
5933 }
5934 }
5935 else
5936 {
5937 it->s = (const unsigned char *) s;
5938 it->string = Qnil;
5939
5940 /* Note that we use IT->current.pos, not it->current.string_pos,
5941 for displaying C strings. */
5942 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5943 if (it->multibyte_p)
5944 {
5945 it->current.pos = c_string_pos (charpos, s, 1);
5946 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5947 }
5948 else
5949 {
5950 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5951 it->end_charpos = it->string_nchars = strlen (s);
5952 }
5953
5954 if (it->bidi_p)
5955 {
5956 it->bidi_it.string.lstring = Qnil;
5957 it->bidi_it.string.s = (const unsigned char *) s;
5958 it->bidi_it.string.schars = it->end_charpos;
5959 it->bidi_it.string.bufpos = 0;
5960 it->bidi_it.string.from_disp_str = 0;
5961 it->bidi_it.string.unibyte = !it->multibyte_p;
5962 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5963 &it->bidi_it);
5964 }
5965 it->method = GET_FROM_C_STRING;
5966 }
5967
5968 /* PRECISION > 0 means don't return more than PRECISION characters
5969 from the string. */
5970 if (precision > 0 && it->end_charpos - charpos > precision)
5971 {
5972 it->end_charpos = it->string_nchars = charpos + precision;
5973 if (it->bidi_p)
5974 it->bidi_it.string.schars = it->end_charpos;
5975 }
5976
5977 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5978 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5979 FIELD_WIDTH < 0 means infinite field width. This is useful for
5980 padding with `-' at the end of a mode line. */
5981 if (field_width < 0)
5982 field_width = INFINITY;
5983 /* Implementation note: We deliberately don't enlarge
5984 it->bidi_it.string.schars here to fit it->end_charpos, because
5985 the bidi iterator cannot produce characters out of thin air. */
5986 if (field_width > it->end_charpos - charpos)
5987 it->end_charpos = charpos + field_width;
5988
5989 /* Use the standard display table for displaying strings. */
5990 if (DISP_TABLE_P (Vstandard_display_table))
5991 it->dp = XCHAR_TABLE (Vstandard_display_table);
5992
5993 it->stop_charpos = charpos;
5994 it->prev_stop = charpos;
5995 it->base_level_stop = 0;
5996 if (it->bidi_p)
5997 {
5998 it->bidi_it.first_elt = 1;
5999 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6000 it->bidi_it.disp_pos = -1;
6001 }
6002 if (s == NULL && it->multibyte_p)
6003 {
6004 EMACS_INT endpos = SCHARS (it->string);
6005 if (endpos > it->end_charpos)
6006 endpos = it->end_charpos;
6007 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6008 it->string);
6009 }
6010 CHECK_IT (it);
6011 }
6012
6013
6014 \f
6015 /***********************************************************************
6016 Iteration
6017 ***********************************************************************/
6018
6019 /* Map enum it_method value to corresponding next_element_from_* function. */
6020
6021 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6022 {
6023 next_element_from_buffer,
6024 next_element_from_display_vector,
6025 next_element_from_string,
6026 next_element_from_c_string,
6027 next_element_from_image,
6028 next_element_from_stretch
6029 };
6030
6031 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6032
6033
6034 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6035 (possibly with the following characters). */
6036
6037 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6038 ((IT)->cmp_it.id >= 0 \
6039 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6040 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6041 END_CHARPOS, (IT)->w, \
6042 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6043 (IT)->string)))
6044
6045
6046 /* Lookup the char-table Vglyphless_char_display for character C (-1
6047 if we want information for no-font case), and return the display
6048 method symbol. By side-effect, update it->what and
6049 it->glyphless_method. This function is called from
6050 get_next_display_element for each character element, and from
6051 x_produce_glyphs when no suitable font was found. */
6052
6053 Lisp_Object
6054 lookup_glyphless_char_display (int c, struct it *it)
6055 {
6056 Lisp_Object glyphless_method = Qnil;
6057
6058 if (CHAR_TABLE_P (Vglyphless_char_display)
6059 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6060 {
6061 if (c >= 0)
6062 {
6063 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6064 if (CONSP (glyphless_method))
6065 glyphless_method = FRAME_WINDOW_P (it->f)
6066 ? XCAR (glyphless_method)
6067 : XCDR (glyphless_method);
6068 }
6069 else
6070 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6071 }
6072
6073 retry:
6074 if (NILP (glyphless_method))
6075 {
6076 if (c >= 0)
6077 /* The default is to display the character by a proper font. */
6078 return Qnil;
6079 /* The default for the no-font case is to display an empty box. */
6080 glyphless_method = Qempty_box;
6081 }
6082 if (EQ (glyphless_method, Qzero_width))
6083 {
6084 if (c >= 0)
6085 return glyphless_method;
6086 /* This method can't be used for the no-font case. */
6087 glyphless_method = Qempty_box;
6088 }
6089 if (EQ (glyphless_method, Qthin_space))
6090 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6091 else if (EQ (glyphless_method, Qempty_box))
6092 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6093 else if (EQ (glyphless_method, Qhex_code))
6094 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6095 else if (STRINGP (glyphless_method))
6096 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6097 else
6098 {
6099 /* Invalid value. We use the default method. */
6100 glyphless_method = Qnil;
6101 goto retry;
6102 }
6103 it->what = IT_GLYPHLESS;
6104 return glyphless_method;
6105 }
6106
6107 /* Load IT's display element fields with information about the next
6108 display element from the current position of IT. Value is zero if
6109 end of buffer (or C string) is reached. */
6110
6111 static struct frame *last_escape_glyph_frame = NULL;
6112 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6113 static int last_escape_glyph_merged_face_id = 0;
6114
6115 struct frame *last_glyphless_glyph_frame = NULL;
6116 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6117 int last_glyphless_glyph_merged_face_id = 0;
6118
6119 static int
6120 get_next_display_element (struct it *it)
6121 {
6122 /* Non-zero means that we found a display element. Zero means that
6123 we hit the end of what we iterate over. Performance note: the
6124 function pointer `method' used here turns out to be faster than
6125 using a sequence of if-statements. */
6126 int success_p;
6127
6128 get_next:
6129 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6130
6131 if (it->what == IT_CHARACTER)
6132 {
6133 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6134 and only if (a) the resolved directionality of that character
6135 is R..." */
6136 /* FIXME: Do we need an exception for characters from display
6137 tables? */
6138 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6139 it->c = bidi_mirror_char (it->c);
6140 /* Map via display table or translate control characters.
6141 IT->c, IT->len etc. have been set to the next character by
6142 the function call above. If we have a display table, and it
6143 contains an entry for IT->c, translate it. Don't do this if
6144 IT->c itself comes from a display table, otherwise we could
6145 end up in an infinite recursion. (An alternative could be to
6146 count the recursion depth of this function and signal an
6147 error when a certain maximum depth is reached.) Is it worth
6148 it? */
6149 if (success_p && it->dpvec == NULL)
6150 {
6151 Lisp_Object dv;
6152 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6153 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6154 nbsp_or_shy = char_is_other;
6155 int c = it->c; /* This is the character to display. */
6156
6157 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6158 {
6159 xassert (SINGLE_BYTE_CHAR_P (c));
6160 if (unibyte_display_via_language_environment)
6161 {
6162 c = DECODE_CHAR (unibyte, c);
6163 if (c < 0)
6164 c = BYTE8_TO_CHAR (it->c);
6165 }
6166 else
6167 c = BYTE8_TO_CHAR (it->c);
6168 }
6169
6170 if (it->dp
6171 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6172 VECTORP (dv)))
6173 {
6174 struct Lisp_Vector *v = XVECTOR (dv);
6175
6176 /* Return the first character from the display table
6177 entry, if not empty. If empty, don't display the
6178 current character. */
6179 if (v->header.size)
6180 {
6181 it->dpvec_char_len = it->len;
6182 it->dpvec = v->contents;
6183 it->dpend = v->contents + v->header.size;
6184 it->current.dpvec_index = 0;
6185 it->dpvec_face_id = -1;
6186 it->saved_face_id = it->face_id;
6187 it->method = GET_FROM_DISPLAY_VECTOR;
6188 it->ellipsis_p = 0;
6189 }
6190 else
6191 {
6192 set_iterator_to_next (it, 0);
6193 }
6194 goto get_next;
6195 }
6196
6197 if (! NILP (lookup_glyphless_char_display (c, it)))
6198 {
6199 if (it->what == IT_GLYPHLESS)
6200 goto done;
6201 /* Don't display this character. */
6202 set_iterator_to_next (it, 0);
6203 goto get_next;
6204 }
6205
6206 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6207 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6208 : c == 0xAD ? char_is_soft_hyphen
6209 : char_is_other);
6210
6211 /* Translate control characters into `\003' or `^C' form.
6212 Control characters coming from a display table entry are
6213 currently not translated because we use IT->dpvec to hold
6214 the translation. This could easily be changed but I
6215 don't believe that it is worth doing.
6216
6217 NBSP and SOFT-HYPEN are property translated too.
6218
6219 Non-printable characters and raw-byte characters are also
6220 translated to octal form. */
6221 if (((c < ' ' || c == 127) /* ASCII control chars */
6222 ? (it->area != TEXT_AREA
6223 /* In mode line, treat \n, \t like other crl chars. */
6224 || (c != '\t'
6225 && it->glyph_row
6226 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6227 || (c != '\n' && c != '\t'))
6228 : (nbsp_or_shy
6229 || CHAR_BYTE8_P (c)
6230 || ! CHAR_PRINTABLE_P (c))))
6231 {
6232 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6233 or a non-printable character which must be displayed
6234 either as '\003' or as `^C' where the '\\' and '^'
6235 can be defined in the display table. Fill
6236 IT->ctl_chars with glyphs for what we have to
6237 display. Then, set IT->dpvec to these glyphs. */
6238 Lisp_Object gc;
6239 int ctl_len;
6240 int face_id;
6241 EMACS_INT lface_id = 0;
6242 int escape_glyph;
6243
6244 /* Handle control characters with ^. */
6245
6246 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6247 {
6248 int g;
6249
6250 g = '^'; /* default glyph for Control */
6251 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6252 if (it->dp
6253 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6254 && GLYPH_CODE_CHAR_VALID_P (gc))
6255 {
6256 g = GLYPH_CODE_CHAR (gc);
6257 lface_id = GLYPH_CODE_FACE (gc);
6258 }
6259 if (lface_id)
6260 {
6261 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6262 }
6263 else if (it->f == last_escape_glyph_frame
6264 && it->face_id == last_escape_glyph_face_id)
6265 {
6266 face_id = last_escape_glyph_merged_face_id;
6267 }
6268 else
6269 {
6270 /* Merge the escape-glyph face into the current face. */
6271 face_id = merge_faces (it->f, Qescape_glyph, 0,
6272 it->face_id);
6273 last_escape_glyph_frame = it->f;
6274 last_escape_glyph_face_id = it->face_id;
6275 last_escape_glyph_merged_face_id = face_id;
6276 }
6277
6278 XSETINT (it->ctl_chars[0], g);
6279 XSETINT (it->ctl_chars[1], c ^ 0100);
6280 ctl_len = 2;
6281 goto display_control;
6282 }
6283
6284 /* Handle non-break space in the mode where it only gets
6285 highlighting. */
6286
6287 if (EQ (Vnobreak_char_display, Qt)
6288 && nbsp_or_shy == char_is_nbsp)
6289 {
6290 /* Merge the no-break-space face into the current face. */
6291 face_id = merge_faces (it->f, Qnobreak_space, 0,
6292 it->face_id);
6293
6294 c = ' ';
6295 XSETINT (it->ctl_chars[0], ' ');
6296 ctl_len = 1;
6297 goto display_control;
6298 }
6299
6300 /* Handle sequences that start with the "escape glyph". */
6301
6302 /* the default escape glyph is \. */
6303 escape_glyph = '\\';
6304
6305 if (it->dp
6306 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6307 && GLYPH_CODE_CHAR_VALID_P (gc))
6308 {
6309 escape_glyph = GLYPH_CODE_CHAR (gc);
6310 lface_id = GLYPH_CODE_FACE (gc);
6311 }
6312 if (lface_id)
6313 {
6314 /* The display table specified a face.
6315 Merge it into face_id and also into escape_glyph. */
6316 face_id = merge_faces (it->f, Qt, lface_id,
6317 it->face_id);
6318 }
6319 else if (it->f == last_escape_glyph_frame
6320 && it->face_id == last_escape_glyph_face_id)
6321 {
6322 face_id = last_escape_glyph_merged_face_id;
6323 }
6324 else
6325 {
6326 /* Merge the escape-glyph face into the current face. */
6327 face_id = merge_faces (it->f, Qescape_glyph, 0,
6328 it->face_id);
6329 last_escape_glyph_frame = it->f;
6330 last_escape_glyph_face_id = it->face_id;
6331 last_escape_glyph_merged_face_id = face_id;
6332 }
6333
6334 /* Handle soft hyphens in the mode where they only get
6335 highlighting. */
6336
6337 if (EQ (Vnobreak_char_display, Qt)
6338 && nbsp_or_shy == char_is_soft_hyphen)
6339 {
6340 XSETINT (it->ctl_chars[0], '-');
6341 ctl_len = 1;
6342 goto display_control;
6343 }
6344
6345 /* Handle non-break space and soft hyphen
6346 with the escape glyph. */
6347
6348 if (nbsp_or_shy)
6349 {
6350 XSETINT (it->ctl_chars[0], escape_glyph);
6351 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6352 XSETINT (it->ctl_chars[1], c);
6353 ctl_len = 2;
6354 goto display_control;
6355 }
6356
6357 {
6358 char str[10];
6359 int len, i;
6360
6361 if (CHAR_BYTE8_P (c))
6362 /* Display \200 instead of \17777600. */
6363 c = CHAR_TO_BYTE8 (c);
6364 len = sprintf (str, "%03o", c);
6365
6366 XSETINT (it->ctl_chars[0], escape_glyph);
6367 for (i = 0; i < len; i++)
6368 XSETINT (it->ctl_chars[i + 1], str[i]);
6369 ctl_len = len + 1;
6370 }
6371
6372 display_control:
6373 /* Set up IT->dpvec and return first character from it. */
6374 it->dpvec_char_len = it->len;
6375 it->dpvec = it->ctl_chars;
6376 it->dpend = it->dpvec + ctl_len;
6377 it->current.dpvec_index = 0;
6378 it->dpvec_face_id = face_id;
6379 it->saved_face_id = it->face_id;
6380 it->method = GET_FROM_DISPLAY_VECTOR;
6381 it->ellipsis_p = 0;
6382 goto get_next;
6383 }
6384 it->char_to_display = c;
6385 }
6386 else if (success_p)
6387 {
6388 it->char_to_display = it->c;
6389 }
6390 }
6391
6392 /* Adjust face id for a multibyte character. There are no multibyte
6393 character in unibyte text. */
6394 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6395 && it->multibyte_p
6396 && success_p
6397 && FRAME_WINDOW_P (it->f))
6398 {
6399 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6400
6401 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6402 {
6403 /* Automatic composition with glyph-string. */
6404 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6405
6406 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6407 }
6408 else
6409 {
6410 EMACS_INT pos = (it->s ? -1
6411 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6412 : IT_CHARPOS (*it));
6413 int c;
6414
6415 if (it->what == IT_CHARACTER)
6416 c = it->char_to_display;
6417 else
6418 {
6419 struct composition *cmp = composition_table[it->cmp_it.id];
6420 int i;
6421
6422 c = ' ';
6423 for (i = 0; i < cmp->glyph_len; i++)
6424 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6425 break;
6426 }
6427 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6428 }
6429 }
6430
6431 done:
6432 /* Is this character the last one of a run of characters with
6433 box? If yes, set IT->end_of_box_run_p to 1. */
6434 if (it->face_box_p
6435 && it->s == NULL)
6436 {
6437 if (it->method == GET_FROM_STRING && it->sp)
6438 {
6439 int face_id = underlying_face_id (it);
6440 struct face *face = FACE_FROM_ID (it->f, face_id);
6441
6442 if (face)
6443 {
6444 if (face->box == FACE_NO_BOX)
6445 {
6446 /* If the box comes from face properties in a
6447 display string, check faces in that string. */
6448 int string_face_id = face_after_it_pos (it);
6449 it->end_of_box_run_p
6450 = (FACE_FROM_ID (it->f, string_face_id)->box
6451 == FACE_NO_BOX);
6452 }
6453 /* Otherwise, the box comes from the underlying face.
6454 If this is the last string character displayed, check
6455 the next buffer location. */
6456 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6457 && (it->current.overlay_string_index
6458 == it->n_overlay_strings - 1))
6459 {
6460 EMACS_INT ignore;
6461 int next_face_id;
6462 struct text_pos pos = it->current.pos;
6463 INC_TEXT_POS (pos, it->multibyte_p);
6464
6465 next_face_id = face_at_buffer_position
6466 (it->w, CHARPOS (pos), it->region_beg_charpos,
6467 it->region_end_charpos, &ignore,
6468 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6469 -1);
6470 it->end_of_box_run_p
6471 = (FACE_FROM_ID (it->f, next_face_id)->box
6472 == FACE_NO_BOX);
6473 }
6474 }
6475 }
6476 else
6477 {
6478 int face_id = face_after_it_pos (it);
6479 it->end_of_box_run_p
6480 = (face_id != it->face_id
6481 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6482 }
6483 }
6484
6485 /* Value is 0 if end of buffer or string reached. */
6486 return success_p;
6487 }
6488
6489
6490 /* Move IT to the next display element.
6491
6492 RESEAT_P non-zero means if called on a newline in buffer text,
6493 skip to the next visible line start.
6494
6495 Functions get_next_display_element and set_iterator_to_next are
6496 separate because I find this arrangement easier to handle than a
6497 get_next_display_element function that also increments IT's
6498 position. The way it is we can first look at an iterator's current
6499 display element, decide whether it fits on a line, and if it does,
6500 increment the iterator position. The other way around we probably
6501 would either need a flag indicating whether the iterator has to be
6502 incremented the next time, or we would have to implement a
6503 decrement position function which would not be easy to write. */
6504
6505 void
6506 set_iterator_to_next (struct it *it, int reseat_p)
6507 {
6508 /* Reset flags indicating start and end of a sequence of characters
6509 with box. Reset them at the start of this function because
6510 moving the iterator to a new position might set them. */
6511 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6512
6513 switch (it->method)
6514 {
6515 case GET_FROM_BUFFER:
6516 /* The current display element of IT is a character from
6517 current_buffer. Advance in the buffer, and maybe skip over
6518 invisible lines that are so because of selective display. */
6519 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6520 reseat_at_next_visible_line_start (it, 0);
6521 else if (it->cmp_it.id >= 0)
6522 {
6523 /* We are currently getting glyphs from a composition. */
6524 int i;
6525
6526 if (! it->bidi_p)
6527 {
6528 IT_CHARPOS (*it) += it->cmp_it.nchars;
6529 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6530 if (it->cmp_it.to < it->cmp_it.nglyphs)
6531 {
6532 it->cmp_it.from = it->cmp_it.to;
6533 }
6534 else
6535 {
6536 it->cmp_it.id = -1;
6537 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6538 IT_BYTEPOS (*it),
6539 it->end_charpos, Qnil);
6540 }
6541 }
6542 else if (! it->cmp_it.reversed_p)
6543 {
6544 /* Composition created while scanning forward. */
6545 /* Update IT's char/byte positions to point to the first
6546 character of the next grapheme cluster, or to the
6547 character visually after the current composition. */
6548 for (i = 0; i < it->cmp_it.nchars; i++)
6549 bidi_move_to_visually_next (&it->bidi_it);
6550 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6551 IT_CHARPOS (*it) = it->bidi_it.charpos;
6552
6553 if (it->cmp_it.to < it->cmp_it.nglyphs)
6554 {
6555 /* Proceed to the next grapheme cluster. */
6556 it->cmp_it.from = it->cmp_it.to;
6557 }
6558 else
6559 {
6560 /* No more grapheme clusters in this composition.
6561 Find the next stop position. */
6562 EMACS_INT stop = it->end_charpos;
6563 if (it->bidi_it.scan_dir < 0)
6564 /* Now we are scanning backward and don't know
6565 where to stop. */
6566 stop = -1;
6567 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6568 IT_BYTEPOS (*it), stop, Qnil);
6569 }
6570 }
6571 else
6572 {
6573 /* Composition created while scanning backward. */
6574 /* Update IT's char/byte positions to point to the last
6575 character of the previous grapheme cluster, or the
6576 character visually after the current composition. */
6577 for (i = 0; i < it->cmp_it.nchars; i++)
6578 bidi_move_to_visually_next (&it->bidi_it);
6579 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6580 IT_CHARPOS (*it) = it->bidi_it.charpos;
6581 if (it->cmp_it.from > 0)
6582 {
6583 /* Proceed to the previous grapheme cluster. */
6584 it->cmp_it.to = it->cmp_it.from;
6585 }
6586 else
6587 {
6588 /* No more grapheme clusters in this composition.
6589 Find the next stop position. */
6590 EMACS_INT stop = it->end_charpos;
6591 if (it->bidi_it.scan_dir < 0)
6592 /* Now we are scanning backward and don't know
6593 where to stop. */
6594 stop = -1;
6595 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6596 IT_BYTEPOS (*it), stop, Qnil);
6597 }
6598 }
6599 }
6600 else
6601 {
6602 xassert (it->len != 0);
6603
6604 if (!it->bidi_p)
6605 {
6606 IT_BYTEPOS (*it) += it->len;
6607 IT_CHARPOS (*it) += 1;
6608 }
6609 else
6610 {
6611 int prev_scan_dir = it->bidi_it.scan_dir;
6612 /* If this is a new paragraph, determine its base
6613 direction (a.k.a. its base embedding level). */
6614 if (it->bidi_it.new_paragraph)
6615 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6616 bidi_move_to_visually_next (&it->bidi_it);
6617 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6618 IT_CHARPOS (*it) = it->bidi_it.charpos;
6619 if (prev_scan_dir != it->bidi_it.scan_dir)
6620 {
6621 /* As the scan direction was changed, we must
6622 re-compute the stop position for composition. */
6623 EMACS_INT stop = it->end_charpos;
6624 if (it->bidi_it.scan_dir < 0)
6625 stop = -1;
6626 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6627 IT_BYTEPOS (*it), stop, Qnil);
6628 }
6629 }
6630 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6631 }
6632 break;
6633
6634 case GET_FROM_C_STRING:
6635 /* Current display element of IT is from a C string. */
6636 if (!it->bidi_p
6637 /* If the string position is beyond string's end, it means
6638 next_element_from_c_string is padding the string with
6639 blanks, in which case we bypass the bidi iterator,
6640 because it cannot deal with such virtual characters. */
6641 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6642 {
6643 IT_BYTEPOS (*it) += it->len;
6644 IT_CHARPOS (*it) += 1;
6645 }
6646 else
6647 {
6648 bidi_move_to_visually_next (&it->bidi_it);
6649 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6650 IT_CHARPOS (*it) = it->bidi_it.charpos;
6651 }
6652 break;
6653
6654 case GET_FROM_DISPLAY_VECTOR:
6655 /* Current display element of IT is from a display table entry.
6656 Advance in the display table definition. Reset it to null if
6657 end reached, and continue with characters from buffers/
6658 strings. */
6659 ++it->current.dpvec_index;
6660
6661 /* Restore face of the iterator to what they were before the
6662 display vector entry (these entries may contain faces). */
6663 it->face_id = it->saved_face_id;
6664
6665 if (it->dpvec + it->current.dpvec_index == it->dpend)
6666 {
6667 int recheck_faces = it->ellipsis_p;
6668
6669 if (it->s)
6670 it->method = GET_FROM_C_STRING;
6671 else if (STRINGP (it->string))
6672 it->method = GET_FROM_STRING;
6673 else
6674 {
6675 it->method = GET_FROM_BUFFER;
6676 it->object = it->w->buffer;
6677 }
6678
6679 it->dpvec = NULL;
6680 it->current.dpvec_index = -1;
6681
6682 /* Skip over characters which were displayed via IT->dpvec. */
6683 if (it->dpvec_char_len < 0)
6684 reseat_at_next_visible_line_start (it, 1);
6685 else if (it->dpvec_char_len > 0)
6686 {
6687 if (it->method == GET_FROM_STRING
6688 && it->n_overlay_strings > 0)
6689 it->ignore_overlay_strings_at_pos_p = 1;
6690 it->len = it->dpvec_char_len;
6691 set_iterator_to_next (it, reseat_p);
6692 }
6693
6694 /* Maybe recheck faces after display vector */
6695 if (recheck_faces)
6696 it->stop_charpos = IT_CHARPOS (*it);
6697 }
6698 break;
6699
6700 case GET_FROM_STRING:
6701 /* Current display element is a character from a Lisp string. */
6702 xassert (it->s == NULL && STRINGP (it->string));
6703 if (it->cmp_it.id >= 0)
6704 {
6705 int i;
6706
6707 if (! it->bidi_p)
6708 {
6709 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6710 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6711 if (it->cmp_it.to < it->cmp_it.nglyphs)
6712 it->cmp_it.from = it->cmp_it.to;
6713 else
6714 {
6715 it->cmp_it.id = -1;
6716 composition_compute_stop_pos (&it->cmp_it,
6717 IT_STRING_CHARPOS (*it),
6718 IT_STRING_BYTEPOS (*it),
6719 it->end_charpos, it->string);
6720 }
6721 }
6722 else if (! it->cmp_it.reversed_p)
6723 {
6724 for (i = 0; i < it->cmp_it.nchars; i++)
6725 bidi_move_to_visually_next (&it->bidi_it);
6726 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6727 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6728
6729 if (it->cmp_it.to < it->cmp_it.nglyphs)
6730 it->cmp_it.from = it->cmp_it.to;
6731 else
6732 {
6733 EMACS_INT stop = it->end_charpos;
6734 if (it->bidi_it.scan_dir < 0)
6735 stop = -1;
6736 composition_compute_stop_pos (&it->cmp_it,
6737 IT_STRING_CHARPOS (*it),
6738 IT_STRING_BYTEPOS (*it), stop,
6739 it->string);
6740 }
6741 }
6742 else
6743 {
6744 for (i = 0; i < it->cmp_it.nchars; i++)
6745 bidi_move_to_visually_next (&it->bidi_it);
6746 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6747 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6748 if (it->cmp_it.from > 0)
6749 it->cmp_it.to = it->cmp_it.from;
6750 else
6751 {
6752 EMACS_INT stop = it->end_charpos;
6753 if (it->bidi_it.scan_dir < 0)
6754 stop = -1;
6755 composition_compute_stop_pos (&it->cmp_it,
6756 IT_STRING_CHARPOS (*it),
6757 IT_STRING_BYTEPOS (*it), stop,
6758 it->string);
6759 }
6760 }
6761 }
6762 else
6763 {
6764 if (!it->bidi_p
6765 /* If the string position is beyond string's end, it
6766 means next_element_from_string is padding the string
6767 with blanks, in which case we bypass the bidi
6768 iterator, because it cannot deal with such virtual
6769 characters. */
6770 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6771 {
6772 IT_STRING_BYTEPOS (*it) += it->len;
6773 IT_STRING_CHARPOS (*it) += 1;
6774 }
6775 else
6776 {
6777 int prev_scan_dir = it->bidi_it.scan_dir;
6778
6779 bidi_move_to_visually_next (&it->bidi_it);
6780 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6781 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6782 if (prev_scan_dir != it->bidi_it.scan_dir)
6783 {
6784 EMACS_INT stop = it->end_charpos;
6785
6786 if (it->bidi_it.scan_dir < 0)
6787 stop = -1;
6788 composition_compute_stop_pos (&it->cmp_it,
6789 IT_STRING_CHARPOS (*it),
6790 IT_STRING_BYTEPOS (*it), stop,
6791 it->string);
6792 }
6793 }
6794 }
6795
6796 consider_string_end:
6797
6798 if (it->current.overlay_string_index >= 0)
6799 {
6800 /* IT->string is an overlay string. Advance to the
6801 next, if there is one. */
6802 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6803 {
6804 it->ellipsis_p = 0;
6805 next_overlay_string (it);
6806 if (it->ellipsis_p)
6807 setup_for_ellipsis (it, 0);
6808 }
6809 }
6810 else
6811 {
6812 /* IT->string is not an overlay string. If we reached
6813 its end, and there is something on IT->stack, proceed
6814 with what is on the stack. This can be either another
6815 string, this time an overlay string, or a buffer. */
6816 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6817 && it->sp > 0)
6818 {
6819 pop_it (it);
6820 if (it->method == GET_FROM_STRING)
6821 goto consider_string_end;
6822 }
6823 }
6824 break;
6825
6826 case GET_FROM_IMAGE:
6827 case GET_FROM_STRETCH:
6828 /* The position etc with which we have to proceed are on
6829 the stack. The position may be at the end of a string,
6830 if the `display' property takes up the whole string. */
6831 xassert (it->sp > 0);
6832 pop_it (it);
6833 if (it->method == GET_FROM_STRING)
6834 goto consider_string_end;
6835 break;
6836
6837 default:
6838 /* There are no other methods defined, so this should be a bug. */
6839 abort ();
6840 }
6841
6842 xassert (it->method != GET_FROM_STRING
6843 || (STRINGP (it->string)
6844 && IT_STRING_CHARPOS (*it) >= 0));
6845 }
6846
6847 /* Load IT's display element fields with information about the next
6848 display element which comes from a display table entry or from the
6849 result of translating a control character to one of the forms `^C'
6850 or `\003'.
6851
6852 IT->dpvec holds the glyphs to return as characters.
6853 IT->saved_face_id holds the face id before the display vector--it
6854 is restored into IT->face_id in set_iterator_to_next. */
6855
6856 static int
6857 next_element_from_display_vector (struct it *it)
6858 {
6859 Lisp_Object gc;
6860
6861 /* Precondition. */
6862 xassert (it->dpvec && it->current.dpvec_index >= 0);
6863
6864 it->face_id = it->saved_face_id;
6865
6866 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6867 That seemed totally bogus - so I changed it... */
6868 gc = it->dpvec[it->current.dpvec_index];
6869
6870 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6871 {
6872 it->c = GLYPH_CODE_CHAR (gc);
6873 it->len = CHAR_BYTES (it->c);
6874
6875 /* The entry may contain a face id to use. Such a face id is
6876 the id of a Lisp face, not a realized face. A face id of
6877 zero means no face is specified. */
6878 if (it->dpvec_face_id >= 0)
6879 it->face_id = it->dpvec_face_id;
6880 else
6881 {
6882 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
6883 if (lface_id > 0)
6884 it->face_id = merge_faces (it->f, Qt, lface_id,
6885 it->saved_face_id);
6886 }
6887 }
6888 else
6889 /* Display table entry is invalid. Return a space. */
6890 it->c = ' ', it->len = 1;
6891
6892 /* Don't change position and object of the iterator here. They are
6893 still the values of the character that had this display table
6894 entry or was translated, and that's what we want. */
6895 it->what = IT_CHARACTER;
6896 return 1;
6897 }
6898
6899 /* Get the first element of string/buffer in the visual order, after
6900 being reseated to a new position in a string or a buffer. */
6901 static void
6902 get_visually_first_element (struct it *it)
6903 {
6904 int string_p = STRINGP (it->string) || it->s;
6905 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6906 EMACS_INT bob = (string_p ? 0 : BEGV);
6907
6908 if (STRINGP (it->string))
6909 {
6910 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6911 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6912 }
6913 else
6914 {
6915 it->bidi_it.charpos = IT_CHARPOS (*it);
6916 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6917 }
6918
6919 if (it->bidi_it.charpos == eob)
6920 {
6921 /* Nothing to do, but reset the FIRST_ELT flag, like
6922 bidi_paragraph_init does, because we are not going to
6923 call it. */
6924 it->bidi_it.first_elt = 0;
6925 }
6926 else if (it->bidi_it.charpos == bob
6927 || (!string_p
6928 /* FIXME: Should support all Unicode line separators. */
6929 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6930 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6931 {
6932 /* If we are at the beginning of a line/string, we can produce
6933 the next element right away. */
6934 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6935 bidi_move_to_visually_next (&it->bidi_it);
6936 }
6937 else
6938 {
6939 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6940
6941 /* We need to prime the bidi iterator starting at the line's or
6942 string's beginning, before we will be able to produce the
6943 next element. */
6944 if (string_p)
6945 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6946 else
6947 {
6948 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6949 -1);
6950 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6951 }
6952 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6953 do
6954 {
6955 /* Now return to buffer/string position where we were asked
6956 to get the next display element, and produce that. */
6957 bidi_move_to_visually_next (&it->bidi_it);
6958 }
6959 while (it->bidi_it.bytepos != orig_bytepos
6960 && it->bidi_it.charpos < eob);
6961 }
6962
6963 /* Adjust IT's position information to where we ended up. */
6964 if (STRINGP (it->string))
6965 {
6966 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6967 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6968 }
6969 else
6970 {
6971 IT_CHARPOS (*it) = it->bidi_it.charpos;
6972 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6973 }
6974
6975 if (STRINGP (it->string) || !it->s)
6976 {
6977 EMACS_INT stop, charpos, bytepos;
6978
6979 if (STRINGP (it->string))
6980 {
6981 xassert (!it->s);
6982 stop = SCHARS (it->string);
6983 if (stop > it->end_charpos)
6984 stop = it->end_charpos;
6985 charpos = IT_STRING_CHARPOS (*it);
6986 bytepos = IT_STRING_BYTEPOS (*it);
6987 }
6988 else
6989 {
6990 stop = it->end_charpos;
6991 charpos = IT_CHARPOS (*it);
6992 bytepos = IT_BYTEPOS (*it);
6993 }
6994 if (it->bidi_it.scan_dir < 0)
6995 stop = -1;
6996 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6997 it->string);
6998 }
6999 }
7000
7001 /* Load IT with the next display element from Lisp string IT->string.
7002 IT->current.string_pos is the current position within the string.
7003 If IT->current.overlay_string_index >= 0, the Lisp string is an
7004 overlay string. */
7005
7006 static int
7007 next_element_from_string (struct it *it)
7008 {
7009 struct text_pos position;
7010
7011 xassert (STRINGP (it->string));
7012 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7013 xassert (IT_STRING_CHARPOS (*it) >= 0);
7014 position = it->current.string_pos;
7015
7016 /* With bidi reordering, the character to display might not be the
7017 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7018 that we were reseat()ed to a new string, whose paragraph
7019 direction is not known. */
7020 if (it->bidi_p && it->bidi_it.first_elt)
7021 {
7022 get_visually_first_element (it);
7023 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7024 }
7025
7026 /* Time to check for invisible text? */
7027 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7028 {
7029 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7030 {
7031 if (!(!it->bidi_p
7032 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7033 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7034 {
7035 /* With bidi non-linear iteration, we could find
7036 ourselves far beyond the last computed stop_charpos,
7037 with several other stop positions in between that we
7038 missed. Scan them all now, in buffer's logical
7039 order, until we find and handle the last stop_charpos
7040 that precedes our current position. */
7041 handle_stop_backwards (it, it->stop_charpos);
7042 return GET_NEXT_DISPLAY_ELEMENT (it);
7043 }
7044 else
7045 {
7046 if (it->bidi_p)
7047 {
7048 /* Take note of the stop position we just moved
7049 across, for when we will move back across it. */
7050 it->prev_stop = it->stop_charpos;
7051 /* If we are at base paragraph embedding level, take
7052 note of the last stop position seen at this
7053 level. */
7054 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7055 it->base_level_stop = it->stop_charpos;
7056 }
7057 handle_stop (it);
7058
7059 /* Since a handler may have changed IT->method, we must
7060 recurse here. */
7061 return GET_NEXT_DISPLAY_ELEMENT (it);
7062 }
7063 }
7064 else if (it->bidi_p
7065 /* If we are before prev_stop, we may have overstepped
7066 on our way backwards a stop_pos, and if so, we need
7067 to handle that stop_pos. */
7068 && IT_STRING_CHARPOS (*it) < it->prev_stop
7069 /* We can sometimes back up for reasons that have nothing
7070 to do with bidi reordering. E.g., compositions. The
7071 code below is only needed when we are above the base
7072 embedding level, so test for that explicitly. */
7073 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7074 {
7075 /* If we lost track of base_level_stop, we have no better
7076 place for handle_stop_backwards to start from than string
7077 beginning. This happens, e.g., when we were reseated to
7078 the previous screenful of text by vertical-motion. */
7079 if (it->base_level_stop <= 0
7080 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7081 it->base_level_stop = 0;
7082 handle_stop_backwards (it, it->base_level_stop);
7083 return GET_NEXT_DISPLAY_ELEMENT (it);
7084 }
7085 }
7086
7087 if (it->current.overlay_string_index >= 0)
7088 {
7089 /* Get the next character from an overlay string. In overlay
7090 strings, There is no field width or padding with spaces to
7091 do. */
7092 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7093 {
7094 it->what = IT_EOB;
7095 return 0;
7096 }
7097 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7098 IT_STRING_BYTEPOS (*it),
7099 it->bidi_it.scan_dir < 0
7100 ? -1
7101 : SCHARS (it->string))
7102 && next_element_from_composition (it))
7103 {
7104 return 1;
7105 }
7106 else if (STRING_MULTIBYTE (it->string))
7107 {
7108 const unsigned char *s = (SDATA (it->string)
7109 + IT_STRING_BYTEPOS (*it));
7110 it->c = string_char_and_length (s, &it->len);
7111 }
7112 else
7113 {
7114 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7115 it->len = 1;
7116 }
7117 }
7118 else
7119 {
7120 /* Get the next character from a Lisp string that is not an
7121 overlay string. Such strings come from the mode line, for
7122 example. We may have to pad with spaces, or truncate the
7123 string. See also next_element_from_c_string. */
7124 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7125 {
7126 it->what = IT_EOB;
7127 return 0;
7128 }
7129 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7130 {
7131 /* Pad with spaces. */
7132 it->c = ' ', it->len = 1;
7133 CHARPOS (position) = BYTEPOS (position) = -1;
7134 }
7135 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7136 IT_STRING_BYTEPOS (*it),
7137 it->bidi_it.scan_dir < 0
7138 ? -1
7139 : it->string_nchars)
7140 && next_element_from_composition (it))
7141 {
7142 return 1;
7143 }
7144 else if (STRING_MULTIBYTE (it->string))
7145 {
7146 const unsigned char *s = (SDATA (it->string)
7147 + IT_STRING_BYTEPOS (*it));
7148 it->c = string_char_and_length (s, &it->len);
7149 }
7150 else
7151 {
7152 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7153 it->len = 1;
7154 }
7155 }
7156
7157 /* Record what we have and where it came from. */
7158 it->what = IT_CHARACTER;
7159 it->object = it->string;
7160 it->position = position;
7161 return 1;
7162 }
7163
7164
7165 /* Load IT with next display element from C string IT->s.
7166 IT->string_nchars is the maximum number of characters to return
7167 from the string. IT->end_charpos may be greater than
7168 IT->string_nchars when this function is called, in which case we
7169 may have to return padding spaces. Value is zero if end of string
7170 reached, including padding spaces. */
7171
7172 static int
7173 next_element_from_c_string (struct it *it)
7174 {
7175 int success_p = 1;
7176
7177 xassert (it->s);
7178 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7179 it->what = IT_CHARACTER;
7180 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7181 it->object = Qnil;
7182
7183 /* With bidi reordering, the character to display might not be the
7184 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7185 we were reseated to a new string, whose paragraph direction is
7186 not known. */
7187 if (it->bidi_p && it->bidi_it.first_elt)
7188 get_visually_first_element (it);
7189
7190 /* IT's position can be greater than IT->string_nchars in case a
7191 field width or precision has been specified when the iterator was
7192 initialized. */
7193 if (IT_CHARPOS (*it) >= it->end_charpos)
7194 {
7195 /* End of the game. */
7196 it->what = IT_EOB;
7197 success_p = 0;
7198 }
7199 else if (IT_CHARPOS (*it) >= it->string_nchars)
7200 {
7201 /* Pad with spaces. */
7202 it->c = ' ', it->len = 1;
7203 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7204 }
7205 else if (it->multibyte_p)
7206 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7207 else
7208 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7209
7210 return success_p;
7211 }
7212
7213
7214 /* Set up IT to return characters from an ellipsis, if appropriate.
7215 The definition of the ellipsis glyphs may come from a display table
7216 entry. This function fills IT with the first glyph from the
7217 ellipsis if an ellipsis is to be displayed. */
7218
7219 static int
7220 next_element_from_ellipsis (struct it *it)
7221 {
7222 if (it->selective_display_ellipsis_p)
7223 setup_for_ellipsis (it, it->len);
7224 else
7225 {
7226 /* The face at the current position may be different from the
7227 face we find after the invisible text. Remember what it
7228 was in IT->saved_face_id, and signal that it's there by
7229 setting face_before_selective_p. */
7230 it->saved_face_id = it->face_id;
7231 it->method = GET_FROM_BUFFER;
7232 it->object = it->w->buffer;
7233 reseat_at_next_visible_line_start (it, 1);
7234 it->face_before_selective_p = 1;
7235 }
7236
7237 return GET_NEXT_DISPLAY_ELEMENT (it);
7238 }
7239
7240
7241 /* Deliver an image display element. The iterator IT is already
7242 filled with image information (done in handle_display_prop). Value
7243 is always 1. */
7244
7245
7246 static int
7247 next_element_from_image (struct it *it)
7248 {
7249 it->what = IT_IMAGE;
7250 it->ignore_overlay_strings_at_pos_p = 0;
7251 return 1;
7252 }
7253
7254
7255 /* Fill iterator IT with next display element from a stretch glyph
7256 property. IT->object is the value of the text property. Value is
7257 always 1. */
7258
7259 static int
7260 next_element_from_stretch (struct it *it)
7261 {
7262 it->what = IT_STRETCH;
7263 return 1;
7264 }
7265
7266 /* Scan backwards from IT's current position until we find a stop
7267 position, or until BEGV. This is called when we find ourself
7268 before both the last known prev_stop and base_level_stop while
7269 reordering bidirectional text. */
7270
7271 static void
7272 compute_stop_pos_backwards (struct it *it)
7273 {
7274 const int SCAN_BACK_LIMIT = 1000;
7275 struct text_pos pos;
7276 struct display_pos save_current = it->current;
7277 struct text_pos save_position = it->position;
7278 EMACS_INT charpos = IT_CHARPOS (*it);
7279 EMACS_INT where_we_are = charpos;
7280 EMACS_INT save_stop_pos = it->stop_charpos;
7281 EMACS_INT save_end_pos = it->end_charpos;
7282
7283 xassert (NILP (it->string) && !it->s);
7284 xassert (it->bidi_p);
7285 it->bidi_p = 0;
7286 do
7287 {
7288 it->end_charpos = min (charpos + 1, ZV);
7289 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7290 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7291 reseat_1 (it, pos, 0);
7292 compute_stop_pos (it);
7293 /* We must advance forward, right? */
7294 if (it->stop_charpos <= charpos)
7295 abort ();
7296 }
7297 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7298
7299 if (it->stop_charpos <= where_we_are)
7300 it->prev_stop = it->stop_charpos;
7301 else
7302 it->prev_stop = BEGV;
7303 it->bidi_p = 1;
7304 it->current = save_current;
7305 it->position = save_position;
7306 it->stop_charpos = save_stop_pos;
7307 it->end_charpos = save_end_pos;
7308 }
7309
7310 /* Scan forward from CHARPOS in the current buffer/string, until we
7311 find a stop position > current IT's position. Then handle the stop
7312 position before that. This is called when we bump into a stop
7313 position while reordering bidirectional text. CHARPOS should be
7314 the last previously processed stop_pos (or BEGV/0, if none were
7315 processed yet) whose position is less that IT's current
7316 position. */
7317
7318 static void
7319 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7320 {
7321 int bufp = !STRINGP (it->string);
7322 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7323 struct display_pos save_current = it->current;
7324 struct text_pos save_position = it->position;
7325 struct text_pos pos1;
7326 EMACS_INT next_stop;
7327
7328 /* Scan in strict logical order. */
7329 xassert (it->bidi_p);
7330 it->bidi_p = 0;
7331 do
7332 {
7333 it->prev_stop = charpos;
7334 if (bufp)
7335 {
7336 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7337 reseat_1 (it, pos1, 0);
7338 }
7339 else
7340 it->current.string_pos = string_pos (charpos, it->string);
7341 compute_stop_pos (it);
7342 /* We must advance forward, right? */
7343 if (it->stop_charpos <= it->prev_stop)
7344 abort ();
7345 charpos = it->stop_charpos;
7346 }
7347 while (charpos <= where_we_are);
7348
7349 it->bidi_p = 1;
7350 it->current = save_current;
7351 it->position = save_position;
7352 next_stop = it->stop_charpos;
7353 it->stop_charpos = it->prev_stop;
7354 handle_stop (it);
7355 it->stop_charpos = next_stop;
7356 }
7357
7358 /* Load IT with the next display element from current_buffer. Value
7359 is zero if end of buffer reached. IT->stop_charpos is the next
7360 position at which to stop and check for text properties or buffer
7361 end. */
7362
7363 static int
7364 next_element_from_buffer (struct it *it)
7365 {
7366 int success_p = 1;
7367
7368 xassert (IT_CHARPOS (*it) >= BEGV);
7369 xassert (NILP (it->string) && !it->s);
7370 xassert (!it->bidi_p
7371 || (EQ (it->bidi_it.string.lstring, Qnil)
7372 && it->bidi_it.string.s == NULL));
7373
7374 /* With bidi reordering, the character to display might not be the
7375 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7376 we were reseat()ed to a new buffer position, which is potentially
7377 a different paragraph. */
7378 if (it->bidi_p && it->bidi_it.first_elt)
7379 {
7380 get_visually_first_element (it);
7381 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7382 }
7383
7384 if (IT_CHARPOS (*it) >= it->stop_charpos)
7385 {
7386 if (IT_CHARPOS (*it) >= it->end_charpos)
7387 {
7388 int overlay_strings_follow_p;
7389
7390 /* End of the game, except when overlay strings follow that
7391 haven't been returned yet. */
7392 if (it->overlay_strings_at_end_processed_p)
7393 overlay_strings_follow_p = 0;
7394 else
7395 {
7396 it->overlay_strings_at_end_processed_p = 1;
7397 overlay_strings_follow_p = get_overlay_strings (it, 0);
7398 }
7399
7400 if (overlay_strings_follow_p)
7401 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7402 else
7403 {
7404 it->what = IT_EOB;
7405 it->position = it->current.pos;
7406 success_p = 0;
7407 }
7408 }
7409 else if (!(!it->bidi_p
7410 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7411 || IT_CHARPOS (*it) == it->stop_charpos))
7412 {
7413 /* With bidi non-linear iteration, we could find ourselves
7414 far beyond the last computed stop_charpos, with several
7415 other stop positions in between that we missed. Scan
7416 them all now, in buffer's logical order, until we find
7417 and handle the last stop_charpos that precedes our
7418 current position. */
7419 handle_stop_backwards (it, it->stop_charpos);
7420 return GET_NEXT_DISPLAY_ELEMENT (it);
7421 }
7422 else
7423 {
7424 if (it->bidi_p)
7425 {
7426 /* Take note of the stop position we just moved across,
7427 for when we will move back across it. */
7428 it->prev_stop = it->stop_charpos;
7429 /* If we are at base paragraph embedding level, take
7430 note of the last stop position seen at this
7431 level. */
7432 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7433 it->base_level_stop = it->stop_charpos;
7434 }
7435 handle_stop (it);
7436 return GET_NEXT_DISPLAY_ELEMENT (it);
7437 }
7438 }
7439 else if (it->bidi_p
7440 /* If we are before prev_stop, we may have overstepped on
7441 our way backwards a stop_pos, and if so, we need to
7442 handle that stop_pos. */
7443 && IT_CHARPOS (*it) < it->prev_stop
7444 /* We can sometimes back up for reasons that have nothing
7445 to do with bidi reordering. E.g., compositions. The
7446 code below is only needed when we are above the base
7447 embedding level, so test for that explicitly. */
7448 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7449 {
7450 if (it->base_level_stop <= 0
7451 || IT_CHARPOS (*it) < it->base_level_stop)
7452 {
7453 /* If we lost track of base_level_stop, we need to find
7454 prev_stop by looking backwards. This happens, e.g., when
7455 we were reseated to the previous screenful of text by
7456 vertical-motion. */
7457 it->base_level_stop = BEGV;
7458 compute_stop_pos_backwards (it);
7459 handle_stop_backwards (it, it->prev_stop);
7460 }
7461 else
7462 handle_stop_backwards (it, it->base_level_stop);
7463 return GET_NEXT_DISPLAY_ELEMENT (it);
7464 }
7465 else
7466 {
7467 /* No face changes, overlays etc. in sight, so just return a
7468 character from current_buffer. */
7469 unsigned char *p;
7470 EMACS_INT stop;
7471
7472 /* Maybe run the redisplay end trigger hook. Performance note:
7473 This doesn't seem to cost measurable time. */
7474 if (it->redisplay_end_trigger_charpos
7475 && it->glyph_row
7476 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7477 run_redisplay_end_trigger_hook (it);
7478
7479 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7480 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7481 stop)
7482 && next_element_from_composition (it))
7483 {
7484 return 1;
7485 }
7486
7487 /* Get the next character, maybe multibyte. */
7488 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7489 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7490 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7491 else
7492 it->c = *p, it->len = 1;
7493
7494 /* Record what we have and where it came from. */
7495 it->what = IT_CHARACTER;
7496 it->object = it->w->buffer;
7497 it->position = it->current.pos;
7498
7499 /* Normally we return the character found above, except when we
7500 really want to return an ellipsis for selective display. */
7501 if (it->selective)
7502 {
7503 if (it->c == '\n')
7504 {
7505 /* A value of selective > 0 means hide lines indented more
7506 than that number of columns. */
7507 if (it->selective > 0
7508 && IT_CHARPOS (*it) + 1 < ZV
7509 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7510 IT_BYTEPOS (*it) + 1,
7511 it->selective))
7512 {
7513 success_p = next_element_from_ellipsis (it);
7514 it->dpvec_char_len = -1;
7515 }
7516 }
7517 else if (it->c == '\r' && it->selective == -1)
7518 {
7519 /* A value of selective == -1 means that everything from the
7520 CR to the end of the line is invisible, with maybe an
7521 ellipsis displayed for it. */
7522 success_p = next_element_from_ellipsis (it);
7523 it->dpvec_char_len = -1;
7524 }
7525 }
7526 }
7527
7528 /* Value is zero if end of buffer reached. */
7529 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7530 return success_p;
7531 }
7532
7533
7534 /* Run the redisplay end trigger hook for IT. */
7535
7536 static void
7537 run_redisplay_end_trigger_hook (struct it *it)
7538 {
7539 Lisp_Object args[3];
7540
7541 /* IT->glyph_row should be non-null, i.e. we should be actually
7542 displaying something, or otherwise we should not run the hook. */
7543 xassert (it->glyph_row);
7544
7545 /* Set up hook arguments. */
7546 args[0] = Qredisplay_end_trigger_functions;
7547 args[1] = it->window;
7548 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7549 it->redisplay_end_trigger_charpos = 0;
7550
7551 /* Since we are *trying* to run these functions, don't try to run
7552 them again, even if they get an error. */
7553 it->w->redisplay_end_trigger = Qnil;
7554 Frun_hook_with_args (3, args);
7555
7556 /* Notice if it changed the face of the character we are on. */
7557 handle_face_prop (it);
7558 }
7559
7560
7561 /* Deliver a composition display element. Unlike the other
7562 next_element_from_XXX, this function is not registered in the array
7563 get_next_element[]. It is called from next_element_from_buffer and
7564 next_element_from_string when necessary. */
7565
7566 static int
7567 next_element_from_composition (struct it *it)
7568 {
7569 it->what = IT_COMPOSITION;
7570 it->len = it->cmp_it.nbytes;
7571 if (STRINGP (it->string))
7572 {
7573 if (it->c < 0)
7574 {
7575 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7576 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7577 return 0;
7578 }
7579 it->position = it->current.string_pos;
7580 it->object = it->string;
7581 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7582 IT_STRING_BYTEPOS (*it), it->string);
7583 }
7584 else
7585 {
7586 if (it->c < 0)
7587 {
7588 IT_CHARPOS (*it) += it->cmp_it.nchars;
7589 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7590 if (it->bidi_p)
7591 {
7592 if (it->bidi_it.new_paragraph)
7593 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7594 /* Resync the bidi iterator with IT's new position.
7595 FIXME: this doesn't support bidirectional text. */
7596 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7597 bidi_move_to_visually_next (&it->bidi_it);
7598 }
7599 return 0;
7600 }
7601 it->position = it->current.pos;
7602 it->object = it->w->buffer;
7603 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7604 IT_BYTEPOS (*it), Qnil);
7605 }
7606 return 1;
7607 }
7608
7609
7610 \f
7611 /***********************************************************************
7612 Moving an iterator without producing glyphs
7613 ***********************************************************************/
7614
7615 /* Check if iterator is at a position corresponding to a valid buffer
7616 position after some move_it_ call. */
7617
7618 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7619 ((it)->method == GET_FROM_STRING \
7620 ? IT_STRING_CHARPOS (*it) == 0 \
7621 : 1)
7622
7623
7624 /* Move iterator IT to a specified buffer or X position within one
7625 line on the display without producing glyphs.
7626
7627 OP should be a bit mask including some or all of these bits:
7628 MOVE_TO_X: Stop upon reaching x-position TO_X.
7629 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7630 Regardless of OP's value, stop upon reaching the end of the display line.
7631
7632 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7633 This means, in particular, that TO_X includes window's horizontal
7634 scroll amount.
7635
7636 The return value has several possible values that
7637 say what condition caused the scan to stop:
7638
7639 MOVE_POS_MATCH_OR_ZV
7640 - when TO_POS or ZV was reached.
7641
7642 MOVE_X_REACHED
7643 -when TO_X was reached before TO_POS or ZV were reached.
7644
7645 MOVE_LINE_CONTINUED
7646 - when we reached the end of the display area and the line must
7647 be continued.
7648
7649 MOVE_LINE_TRUNCATED
7650 - when we reached the end of the display area and the line is
7651 truncated.
7652
7653 MOVE_NEWLINE_OR_CR
7654 - when we stopped at a line end, i.e. a newline or a CR and selective
7655 display is on. */
7656
7657 static enum move_it_result
7658 move_it_in_display_line_to (struct it *it,
7659 EMACS_INT to_charpos, int to_x,
7660 enum move_operation_enum op)
7661 {
7662 enum move_it_result result = MOVE_UNDEFINED;
7663 struct glyph_row *saved_glyph_row;
7664 struct it wrap_it, atpos_it, atx_it, ppos_it;
7665 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7666 void *ppos_data = NULL;
7667 int may_wrap = 0;
7668 enum it_method prev_method = it->method;
7669 EMACS_INT prev_pos = IT_CHARPOS (*it);
7670 int saw_smaller_pos = prev_pos < to_charpos;
7671
7672 /* Don't produce glyphs in produce_glyphs. */
7673 saved_glyph_row = it->glyph_row;
7674 it->glyph_row = NULL;
7675
7676 /* Use wrap_it to save a copy of IT wherever a word wrap could
7677 occur. Use atpos_it to save a copy of IT at the desired buffer
7678 position, if found, so that we can scan ahead and check if the
7679 word later overshoots the window edge. Use atx_it similarly, for
7680 pixel positions. */
7681 wrap_it.sp = -1;
7682 atpos_it.sp = -1;
7683 atx_it.sp = -1;
7684
7685 /* Use ppos_it under bidi reordering to save a copy of IT for the
7686 position > CHARPOS that is the closest to CHARPOS. We restore
7687 that position in IT when we have scanned the entire display line
7688 without finding a match for CHARPOS and all the character
7689 positions are greater than CHARPOS. */
7690 if (it->bidi_p)
7691 {
7692 SAVE_IT (ppos_it, *it, ppos_data);
7693 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7694 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7695 SAVE_IT (ppos_it, *it, ppos_data);
7696 }
7697
7698 #define BUFFER_POS_REACHED_P() \
7699 ((op & MOVE_TO_POS) != 0 \
7700 && BUFFERP (it->object) \
7701 && (IT_CHARPOS (*it) == to_charpos \
7702 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos) \
7703 || (it->what == IT_COMPOSITION \
7704 && ((IT_CHARPOS (*it) > to_charpos \
7705 && to_charpos >= it->cmp_it.charpos) \
7706 || (IT_CHARPOS (*it) < to_charpos \
7707 && to_charpos <= it->cmp_it.charpos)))) \
7708 && (it->method == GET_FROM_BUFFER \
7709 || (it->method == GET_FROM_DISPLAY_VECTOR \
7710 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7711
7712 /* If there's a line-/wrap-prefix, handle it. */
7713 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7714 && it->current_y < it->last_visible_y)
7715 handle_line_prefix (it);
7716
7717 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7718 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7719
7720 while (1)
7721 {
7722 int x, i, ascent = 0, descent = 0;
7723
7724 /* Utility macro to reset an iterator with x, ascent, and descent. */
7725 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7726 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7727 (IT)->max_descent = descent)
7728
7729 /* Stop if we move beyond TO_CHARPOS (after an image or a
7730 display string or stretch glyph). */
7731 if ((op & MOVE_TO_POS) != 0
7732 && BUFFERP (it->object)
7733 && it->method == GET_FROM_BUFFER
7734 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7735 || (it->bidi_p
7736 && (prev_method == GET_FROM_IMAGE
7737 || prev_method == GET_FROM_STRETCH
7738 || prev_method == GET_FROM_STRING)
7739 /* Passed TO_CHARPOS from left to right. */
7740 && ((prev_pos < to_charpos
7741 && IT_CHARPOS (*it) > to_charpos)
7742 /* Passed TO_CHARPOS from right to left. */
7743 || (prev_pos > to_charpos
7744 && IT_CHARPOS (*it) < to_charpos)))))
7745 {
7746 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7747 {
7748 result = MOVE_POS_MATCH_OR_ZV;
7749 break;
7750 }
7751 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7752 /* If wrap_it is valid, the current position might be in a
7753 word that is wrapped. So, save the iterator in
7754 atpos_it and continue to see if wrapping happens. */
7755 SAVE_IT (atpos_it, *it, atpos_data);
7756 }
7757
7758 /* Stop when ZV reached.
7759 We used to stop here when TO_CHARPOS reached as well, but that is
7760 too soon if this glyph does not fit on this line. So we handle it
7761 explicitly below. */
7762 if (!get_next_display_element (it))
7763 {
7764 result = MOVE_POS_MATCH_OR_ZV;
7765 break;
7766 }
7767
7768 if (it->line_wrap == TRUNCATE)
7769 {
7770 if (BUFFER_POS_REACHED_P ())
7771 {
7772 result = MOVE_POS_MATCH_OR_ZV;
7773 break;
7774 }
7775 }
7776 else
7777 {
7778 if (it->line_wrap == WORD_WRAP)
7779 {
7780 if (IT_DISPLAYING_WHITESPACE (it))
7781 may_wrap = 1;
7782 else if (may_wrap)
7783 {
7784 /* We have reached a glyph that follows one or more
7785 whitespace characters. If the position is
7786 already found, we are done. */
7787 if (atpos_it.sp >= 0)
7788 {
7789 RESTORE_IT (it, &atpos_it, atpos_data);
7790 result = MOVE_POS_MATCH_OR_ZV;
7791 goto done;
7792 }
7793 if (atx_it.sp >= 0)
7794 {
7795 RESTORE_IT (it, &atx_it, atx_data);
7796 result = MOVE_X_REACHED;
7797 goto done;
7798 }
7799 /* Otherwise, we can wrap here. */
7800 SAVE_IT (wrap_it, *it, wrap_data);
7801 may_wrap = 0;
7802 }
7803 }
7804 }
7805
7806 /* Remember the line height for the current line, in case
7807 the next element doesn't fit on the line. */
7808 ascent = it->max_ascent;
7809 descent = it->max_descent;
7810
7811 /* The call to produce_glyphs will get the metrics of the
7812 display element IT is loaded with. Record the x-position
7813 before this display element, in case it doesn't fit on the
7814 line. */
7815 x = it->current_x;
7816
7817 PRODUCE_GLYPHS (it);
7818
7819 if (it->area != TEXT_AREA)
7820 {
7821 prev_method = it->method;
7822 if (it->method == GET_FROM_BUFFER)
7823 prev_pos = IT_CHARPOS (*it);
7824 set_iterator_to_next (it, 1);
7825 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7826 SET_TEXT_POS (this_line_min_pos,
7827 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7828 if (it->bidi_p
7829 && (op & MOVE_TO_POS)
7830 && IT_CHARPOS (*it) > to_charpos
7831 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
7832 SAVE_IT (ppos_it, *it, ppos_data);
7833 continue;
7834 }
7835
7836 /* The number of glyphs we get back in IT->nglyphs will normally
7837 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7838 character on a terminal frame, or (iii) a line end. For the
7839 second case, IT->nglyphs - 1 padding glyphs will be present.
7840 (On X frames, there is only one glyph produced for a
7841 composite character.)
7842
7843 The behavior implemented below means, for continuation lines,
7844 that as many spaces of a TAB as fit on the current line are
7845 displayed there. For terminal frames, as many glyphs of a
7846 multi-glyph character are displayed in the current line, too.
7847 This is what the old redisplay code did, and we keep it that
7848 way. Under X, the whole shape of a complex character must
7849 fit on the line or it will be completely displayed in the
7850 next line.
7851
7852 Note that both for tabs and padding glyphs, all glyphs have
7853 the same width. */
7854 if (it->nglyphs)
7855 {
7856 /* More than one glyph or glyph doesn't fit on line. All
7857 glyphs have the same width. */
7858 int single_glyph_width = it->pixel_width / it->nglyphs;
7859 int new_x;
7860 int x_before_this_char = x;
7861 int hpos_before_this_char = it->hpos;
7862
7863 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7864 {
7865 new_x = x + single_glyph_width;
7866
7867 /* We want to leave anything reaching TO_X to the caller. */
7868 if ((op & MOVE_TO_X) && new_x > to_x)
7869 {
7870 if (BUFFER_POS_REACHED_P ())
7871 {
7872 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7873 goto buffer_pos_reached;
7874 if (atpos_it.sp < 0)
7875 {
7876 SAVE_IT (atpos_it, *it, atpos_data);
7877 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7878 }
7879 }
7880 else
7881 {
7882 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7883 {
7884 it->current_x = x;
7885 result = MOVE_X_REACHED;
7886 break;
7887 }
7888 if (atx_it.sp < 0)
7889 {
7890 SAVE_IT (atx_it, *it, atx_data);
7891 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7892 }
7893 }
7894 }
7895
7896 if (/* Lines are continued. */
7897 it->line_wrap != TRUNCATE
7898 && (/* And glyph doesn't fit on the line. */
7899 new_x > it->last_visible_x
7900 /* Or it fits exactly and we're on a window
7901 system frame. */
7902 || (new_x == it->last_visible_x
7903 && FRAME_WINDOW_P (it->f))))
7904 {
7905 if (/* IT->hpos == 0 means the very first glyph
7906 doesn't fit on the line, e.g. a wide image. */
7907 it->hpos == 0
7908 || (new_x == it->last_visible_x
7909 && FRAME_WINDOW_P (it->f)))
7910 {
7911 ++it->hpos;
7912 it->current_x = new_x;
7913
7914 /* The character's last glyph just barely fits
7915 in this row. */
7916 if (i == it->nglyphs - 1)
7917 {
7918 /* If this is the destination position,
7919 return a position *before* it in this row,
7920 now that we know it fits in this row. */
7921 if (BUFFER_POS_REACHED_P ())
7922 {
7923 if (it->line_wrap != WORD_WRAP
7924 || wrap_it.sp < 0)
7925 {
7926 it->hpos = hpos_before_this_char;
7927 it->current_x = x_before_this_char;
7928 result = MOVE_POS_MATCH_OR_ZV;
7929 break;
7930 }
7931 if (it->line_wrap == WORD_WRAP
7932 && atpos_it.sp < 0)
7933 {
7934 SAVE_IT (atpos_it, *it, atpos_data);
7935 atpos_it.current_x = x_before_this_char;
7936 atpos_it.hpos = hpos_before_this_char;
7937 }
7938 }
7939
7940 prev_method = it->method;
7941 if (it->method == GET_FROM_BUFFER)
7942 prev_pos = IT_CHARPOS (*it);
7943 set_iterator_to_next (it, 1);
7944 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7945 SET_TEXT_POS (this_line_min_pos,
7946 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7947 /* On graphical terminals, newlines may
7948 "overflow" into the fringe if
7949 overflow-newline-into-fringe is non-nil.
7950 On text-only terminals, newlines may
7951 overflow into the last glyph on the
7952 display line.*/
7953 if (!FRAME_WINDOW_P (it->f)
7954 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7955 {
7956 if (!get_next_display_element (it))
7957 {
7958 result = MOVE_POS_MATCH_OR_ZV;
7959 break;
7960 }
7961 if (BUFFER_POS_REACHED_P ())
7962 {
7963 if (ITERATOR_AT_END_OF_LINE_P (it))
7964 result = MOVE_POS_MATCH_OR_ZV;
7965 else
7966 result = MOVE_LINE_CONTINUED;
7967 break;
7968 }
7969 if (ITERATOR_AT_END_OF_LINE_P (it))
7970 {
7971 result = MOVE_NEWLINE_OR_CR;
7972 break;
7973 }
7974 }
7975 }
7976 }
7977 else
7978 IT_RESET_X_ASCENT_DESCENT (it);
7979
7980 if (wrap_it.sp >= 0)
7981 {
7982 RESTORE_IT (it, &wrap_it, wrap_data);
7983 atpos_it.sp = -1;
7984 atx_it.sp = -1;
7985 }
7986
7987 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7988 IT_CHARPOS (*it)));
7989 result = MOVE_LINE_CONTINUED;
7990 break;
7991 }
7992
7993 if (BUFFER_POS_REACHED_P ())
7994 {
7995 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7996 goto buffer_pos_reached;
7997 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7998 {
7999 SAVE_IT (atpos_it, *it, atpos_data);
8000 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8001 }
8002 }
8003
8004 if (new_x > it->first_visible_x)
8005 {
8006 /* Glyph is visible. Increment number of glyphs that
8007 would be displayed. */
8008 ++it->hpos;
8009 }
8010 }
8011
8012 if (result != MOVE_UNDEFINED)
8013 break;
8014 }
8015 else if (BUFFER_POS_REACHED_P ())
8016 {
8017 buffer_pos_reached:
8018 IT_RESET_X_ASCENT_DESCENT (it);
8019 result = MOVE_POS_MATCH_OR_ZV;
8020 break;
8021 }
8022 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8023 {
8024 /* Stop when TO_X specified and reached. This check is
8025 necessary here because of lines consisting of a line end,
8026 only. The line end will not produce any glyphs and we
8027 would never get MOVE_X_REACHED. */
8028 xassert (it->nglyphs == 0);
8029 result = MOVE_X_REACHED;
8030 break;
8031 }
8032
8033 /* Is this a line end? If yes, we're done. */
8034 if (ITERATOR_AT_END_OF_LINE_P (it))
8035 {
8036 /* If we are past TO_CHARPOS, but never saw any character
8037 positions smaller than TO_CHARPOS, return
8038 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8039 did. */
8040 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8041 {
8042 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8043 {
8044 if (IT_CHARPOS (ppos_it) < ZV)
8045 {
8046 RESTORE_IT (it, &ppos_it, ppos_data);
8047 result = MOVE_POS_MATCH_OR_ZV;
8048 }
8049 else
8050 goto buffer_pos_reached;
8051 }
8052 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8053 && IT_CHARPOS (*it) > to_charpos)
8054 goto buffer_pos_reached;
8055 else
8056 result = MOVE_NEWLINE_OR_CR;
8057 }
8058 else
8059 result = MOVE_NEWLINE_OR_CR;
8060 break;
8061 }
8062
8063 prev_method = it->method;
8064 if (it->method == GET_FROM_BUFFER)
8065 prev_pos = IT_CHARPOS (*it);
8066 /* The current display element has been consumed. Advance
8067 to the next. */
8068 set_iterator_to_next (it, 1);
8069 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8070 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8071 if (IT_CHARPOS (*it) < to_charpos)
8072 saw_smaller_pos = 1;
8073 if (it->bidi_p
8074 && (op & MOVE_TO_POS)
8075 && IT_CHARPOS (*it) >= to_charpos
8076 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8077 SAVE_IT (ppos_it, *it, ppos_data);
8078
8079 /* Stop if lines are truncated and IT's current x-position is
8080 past the right edge of the window now. */
8081 if (it->line_wrap == TRUNCATE
8082 && it->current_x >= it->last_visible_x)
8083 {
8084 if (!FRAME_WINDOW_P (it->f)
8085 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8086 {
8087 int at_eob_p = 0;
8088
8089 if ((at_eob_p = !get_next_display_element (it))
8090 || BUFFER_POS_REACHED_P ()
8091 /* If we are past TO_CHARPOS, but never saw any
8092 character positions smaller than TO_CHARPOS,
8093 return MOVE_POS_MATCH_OR_ZV, like the
8094 unidirectional display did. */
8095 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8096 && !saw_smaller_pos
8097 && IT_CHARPOS (*it) > to_charpos))
8098 {
8099 if (!at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8100 RESTORE_IT (it, &ppos_it, ppos_data);
8101 result = MOVE_POS_MATCH_OR_ZV;
8102 break;
8103 }
8104 if (ITERATOR_AT_END_OF_LINE_P (it))
8105 {
8106 result = MOVE_NEWLINE_OR_CR;
8107 break;
8108 }
8109 }
8110 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8111 && !saw_smaller_pos
8112 && IT_CHARPOS (*it) > to_charpos)
8113 {
8114 if (IT_CHARPOS (ppos_it) < ZV)
8115 RESTORE_IT (it, &ppos_it, ppos_data);
8116 result = MOVE_POS_MATCH_OR_ZV;
8117 break;
8118 }
8119 result = MOVE_LINE_TRUNCATED;
8120 break;
8121 }
8122 #undef IT_RESET_X_ASCENT_DESCENT
8123 }
8124
8125 #undef BUFFER_POS_REACHED_P
8126
8127 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8128 restore the saved iterator. */
8129 if (atpos_it.sp >= 0)
8130 RESTORE_IT (it, &atpos_it, atpos_data);
8131 else if (atx_it.sp >= 0)
8132 RESTORE_IT (it, &atx_it, atx_data);
8133
8134 done:
8135
8136 if (atpos_data)
8137 bidi_unshelve_cache (atpos_data, 1);
8138 if (atx_data)
8139 bidi_unshelve_cache (atx_data, 1);
8140 if (wrap_data)
8141 bidi_unshelve_cache (wrap_data, 1);
8142 if (ppos_data)
8143 bidi_unshelve_cache (ppos_data, 1);
8144
8145 /* Restore the iterator settings altered at the beginning of this
8146 function. */
8147 it->glyph_row = saved_glyph_row;
8148 return result;
8149 }
8150
8151 /* For external use. */
8152 void
8153 move_it_in_display_line (struct it *it,
8154 EMACS_INT to_charpos, int to_x,
8155 enum move_operation_enum op)
8156 {
8157 if (it->line_wrap == WORD_WRAP
8158 && (op & MOVE_TO_X))
8159 {
8160 struct it save_it;
8161 void *save_data = NULL;
8162 int skip;
8163
8164 SAVE_IT (save_it, *it, save_data);
8165 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8166 /* When word-wrap is on, TO_X may lie past the end
8167 of a wrapped line. Then it->current is the
8168 character on the next line, so backtrack to the
8169 space before the wrap point. */
8170 if (skip == MOVE_LINE_CONTINUED)
8171 {
8172 int prev_x = max (it->current_x - 1, 0);
8173 RESTORE_IT (it, &save_it, save_data);
8174 move_it_in_display_line_to
8175 (it, -1, prev_x, MOVE_TO_X);
8176 }
8177 else
8178 bidi_unshelve_cache (save_data, 1);
8179 }
8180 else
8181 move_it_in_display_line_to (it, to_charpos, to_x, op);
8182 }
8183
8184
8185 /* Move IT forward until it satisfies one or more of the criteria in
8186 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8187
8188 OP is a bit-mask that specifies where to stop, and in particular,
8189 which of those four position arguments makes a difference. See the
8190 description of enum move_operation_enum.
8191
8192 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8193 screen line, this function will set IT to the next position that is
8194 displayed to the right of TO_CHARPOS on the screen. */
8195
8196 void
8197 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8198 {
8199 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8200 int line_height, line_start_x = 0, reached = 0;
8201 void *backup_data = NULL;
8202
8203 for (;;)
8204 {
8205 if (op & MOVE_TO_VPOS)
8206 {
8207 /* If no TO_CHARPOS and no TO_X specified, stop at the
8208 start of the line TO_VPOS. */
8209 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8210 {
8211 if (it->vpos == to_vpos)
8212 {
8213 reached = 1;
8214 break;
8215 }
8216 else
8217 skip = move_it_in_display_line_to (it, -1, -1, 0);
8218 }
8219 else
8220 {
8221 /* TO_VPOS >= 0 means stop at TO_X in the line at
8222 TO_VPOS, or at TO_POS, whichever comes first. */
8223 if (it->vpos == to_vpos)
8224 {
8225 reached = 2;
8226 break;
8227 }
8228
8229 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8230
8231 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8232 {
8233 reached = 3;
8234 break;
8235 }
8236 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8237 {
8238 /* We have reached TO_X but not in the line we want. */
8239 skip = move_it_in_display_line_to (it, to_charpos,
8240 -1, MOVE_TO_POS);
8241 if (skip == MOVE_POS_MATCH_OR_ZV)
8242 {
8243 reached = 4;
8244 break;
8245 }
8246 }
8247 }
8248 }
8249 else if (op & MOVE_TO_Y)
8250 {
8251 struct it it_backup;
8252
8253 if (it->line_wrap == WORD_WRAP)
8254 SAVE_IT (it_backup, *it, backup_data);
8255
8256 /* TO_Y specified means stop at TO_X in the line containing
8257 TO_Y---or at TO_CHARPOS if this is reached first. The
8258 problem is that we can't really tell whether the line
8259 contains TO_Y before we have completely scanned it, and
8260 this may skip past TO_X. What we do is to first scan to
8261 TO_X.
8262
8263 If TO_X is not specified, use a TO_X of zero. The reason
8264 is to make the outcome of this function more predictable.
8265 If we didn't use TO_X == 0, we would stop at the end of
8266 the line which is probably not what a caller would expect
8267 to happen. */
8268 skip = move_it_in_display_line_to
8269 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8270 (MOVE_TO_X | (op & MOVE_TO_POS)));
8271
8272 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8273 if (skip == MOVE_POS_MATCH_OR_ZV)
8274 reached = 5;
8275 else if (skip == MOVE_X_REACHED)
8276 {
8277 /* If TO_X was reached, we want to know whether TO_Y is
8278 in the line. We know this is the case if the already
8279 scanned glyphs make the line tall enough. Otherwise,
8280 we must check by scanning the rest of the line. */
8281 line_height = it->max_ascent + it->max_descent;
8282 if (to_y >= it->current_y
8283 && to_y < it->current_y + line_height)
8284 {
8285 reached = 6;
8286 break;
8287 }
8288 SAVE_IT (it_backup, *it, backup_data);
8289 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8290 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8291 op & MOVE_TO_POS);
8292 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8293 line_height = it->max_ascent + it->max_descent;
8294 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8295
8296 if (to_y >= it->current_y
8297 && to_y < it->current_y + line_height)
8298 {
8299 /* If TO_Y is in this line and TO_X was reached
8300 above, we scanned too far. We have to restore
8301 IT's settings to the ones before skipping. */
8302 RESTORE_IT (it, &it_backup, backup_data);
8303 reached = 6;
8304 }
8305 else
8306 {
8307 skip = skip2;
8308 if (skip == MOVE_POS_MATCH_OR_ZV)
8309 reached = 7;
8310 }
8311 }
8312 else
8313 {
8314 /* Check whether TO_Y is in this line. */
8315 line_height = it->max_ascent + it->max_descent;
8316 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8317
8318 if (to_y >= it->current_y
8319 && to_y < it->current_y + line_height)
8320 {
8321 /* When word-wrap is on, TO_X may lie past the end
8322 of a wrapped line. Then it->current is the
8323 character on the next line, so backtrack to the
8324 space before the wrap point. */
8325 if (skip == MOVE_LINE_CONTINUED
8326 && it->line_wrap == WORD_WRAP)
8327 {
8328 int prev_x = max (it->current_x - 1, 0);
8329 RESTORE_IT (it, &it_backup, backup_data);
8330 skip = move_it_in_display_line_to
8331 (it, -1, prev_x, MOVE_TO_X);
8332 }
8333 reached = 6;
8334 }
8335 }
8336
8337 if (reached)
8338 break;
8339 }
8340 else if (BUFFERP (it->object)
8341 && (it->method == GET_FROM_BUFFER
8342 || it->method == GET_FROM_STRETCH)
8343 && IT_CHARPOS (*it) >= to_charpos)
8344 skip = MOVE_POS_MATCH_OR_ZV;
8345 else
8346 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8347
8348 switch (skip)
8349 {
8350 case MOVE_POS_MATCH_OR_ZV:
8351 reached = 8;
8352 goto out;
8353
8354 case MOVE_NEWLINE_OR_CR:
8355 set_iterator_to_next (it, 1);
8356 it->continuation_lines_width = 0;
8357 break;
8358
8359 case MOVE_LINE_TRUNCATED:
8360 it->continuation_lines_width = 0;
8361 reseat_at_next_visible_line_start (it, 0);
8362 if ((op & MOVE_TO_POS) != 0
8363 && IT_CHARPOS (*it) > to_charpos)
8364 {
8365 reached = 9;
8366 goto out;
8367 }
8368 break;
8369
8370 case MOVE_LINE_CONTINUED:
8371 /* For continued lines ending in a tab, some of the glyphs
8372 associated with the tab are displayed on the current
8373 line. Since it->current_x does not include these glyphs,
8374 we use it->last_visible_x instead. */
8375 if (it->c == '\t')
8376 {
8377 it->continuation_lines_width += it->last_visible_x;
8378 /* When moving by vpos, ensure that the iterator really
8379 advances to the next line (bug#847, bug#969). Fixme:
8380 do we need to do this in other circumstances? */
8381 if (it->current_x != it->last_visible_x
8382 && (op & MOVE_TO_VPOS)
8383 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8384 {
8385 line_start_x = it->current_x + it->pixel_width
8386 - it->last_visible_x;
8387 set_iterator_to_next (it, 0);
8388 }
8389 }
8390 else
8391 it->continuation_lines_width += it->current_x;
8392 break;
8393
8394 default:
8395 abort ();
8396 }
8397
8398 /* Reset/increment for the next run. */
8399 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8400 it->current_x = line_start_x;
8401 line_start_x = 0;
8402 it->hpos = 0;
8403 it->current_y += it->max_ascent + it->max_descent;
8404 ++it->vpos;
8405 last_height = it->max_ascent + it->max_descent;
8406 last_max_ascent = it->max_ascent;
8407 it->max_ascent = it->max_descent = 0;
8408 }
8409
8410 out:
8411
8412 /* On text terminals, we may stop at the end of a line in the middle
8413 of a multi-character glyph. If the glyph itself is continued,
8414 i.e. it is actually displayed on the next line, don't treat this
8415 stopping point as valid; move to the next line instead (unless
8416 that brings us offscreen). */
8417 if (!FRAME_WINDOW_P (it->f)
8418 && op & MOVE_TO_POS
8419 && IT_CHARPOS (*it) == to_charpos
8420 && it->what == IT_CHARACTER
8421 && it->nglyphs > 1
8422 && it->line_wrap == WINDOW_WRAP
8423 && it->current_x == it->last_visible_x - 1
8424 && it->c != '\n'
8425 && it->c != '\t'
8426 && it->vpos < XFASTINT (it->w->window_end_vpos))
8427 {
8428 it->continuation_lines_width += it->current_x;
8429 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8430 it->current_y += it->max_ascent + it->max_descent;
8431 ++it->vpos;
8432 last_height = it->max_ascent + it->max_descent;
8433 last_max_ascent = it->max_ascent;
8434 }
8435
8436 if (backup_data)
8437 bidi_unshelve_cache (backup_data, 1);
8438
8439 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8440 }
8441
8442
8443 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8444
8445 If DY > 0, move IT backward at least that many pixels. DY = 0
8446 means move IT backward to the preceding line start or BEGV. This
8447 function may move over more than DY pixels if IT->current_y - DY
8448 ends up in the middle of a line; in this case IT->current_y will be
8449 set to the top of the line moved to. */
8450
8451 void
8452 move_it_vertically_backward (struct it *it, int dy)
8453 {
8454 int nlines, h;
8455 struct it it2, it3;
8456 void *it2data = NULL, *it3data = NULL;
8457 EMACS_INT start_pos;
8458
8459 move_further_back:
8460 xassert (dy >= 0);
8461
8462 start_pos = IT_CHARPOS (*it);
8463
8464 /* Estimate how many newlines we must move back. */
8465 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8466
8467 /* Set the iterator's position that many lines back. */
8468 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8469 back_to_previous_visible_line_start (it);
8470
8471 /* Reseat the iterator here. When moving backward, we don't want
8472 reseat to skip forward over invisible text, set up the iterator
8473 to deliver from overlay strings at the new position etc. So,
8474 use reseat_1 here. */
8475 reseat_1 (it, it->current.pos, 1);
8476
8477 /* We are now surely at a line start. */
8478 it->current_x = it->hpos = 0;
8479 it->continuation_lines_width = 0;
8480
8481 /* Move forward and see what y-distance we moved. First move to the
8482 start of the next line so that we get its height. We need this
8483 height to be able to tell whether we reached the specified
8484 y-distance. */
8485 SAVE_IT (it2, *it, it2data);
8486 it2.max_ascent = it2.max_descent = 0;
8487 do
8488 {
8489 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8490 MOVE_TO_POS | MOVE_TO_VPOS);
8491 }
8492 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8493 xassert (IT_CHARPOS (*it) >= BEGV);
8494 SAVE_IT (it3, it2, it3data);
8495
8496 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8497 xassert (IT_CHARPOS (*it) >= BEGV);
8498 /* H is the actual vertical distance from the position in *IT
8499 and the starting position. */
8500 h = it2.current_y - it->current_y;
8501 /* NLINES is the distance in number of lines. */
8502 nlines = it2.vpos - it->vpos;
8503
8504 /* Correct IT's y and vpos position
8505 so that they are relative to the starting point. */
8506 it->vpos -= nlines;
8507 it->current_y -= h;
8508
8509 if (dy == 0)
8510 {
8511 /* DY == 0 means move to the start of the screen line. The
8512 value of nlines is > 0 if continuation lines were involved. */
8513 RESTORE_IT (it, it, it2data);
8514 if (nlines > 0)
8515 move_it_by_lines (it, nlines);
8516 bidi_unshelve_cache (it3data, 1);
8517 }
8518 else
8519 {
8520 /* The y-position we try to reach, relative to *IT.
8521 Note that H has been subtracted in front of the if-statement. */
8522 int target_y = it->current_y + h - dy;
8523 int y0 = it3.current_y;
8524 int y1;
8525 int line_height;
8526
8527 RESTORE_IT (&it3, &it3, it3data);
8528 y1 = line_bottom_y (&it3);
8529 line_height = y1 - y0;
8530 RESTORE_IT (it, it, it2data);
8531 /* If we did not reach target_y, try to move further backward if
8532 we can. If we moved too far backward, try to move forward. */
8533 if (target_y < it->current_y
8534 /* This is heuristic. In a window that's 3 lines high, with
8535 a line height of 13 pixels each, recentering with point
8536 on the bottom line will try to move -39/2 = 19 pixels
8537 backward. Try to avoid moving into the first line. */
8538 && (it->current_y - target_y
8539 > min (window_box_height (it->w), line_height * 2 / 3))
8540 && IT_CHARPOS (*it) > BEGV)
8541 {
8542 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8543 target_y - it->current_y));
8544 dy = it->current_y - target_y;
8545 goto move_further_back;
8546 }
8547 else if (target_y >= it->current_y + line_height
8548 && IT_CHARPOS (*it) < ZV)
8549 {
8550 /* Should move forward by at least one line, maybe more.
8551
8552 Note: Calling move_it_by_lines can be expensive on
8553 terminal frames, where compute_motion is used (via
8554 vmotion) to do the job, when there are very long lines
8555 and truncate-lines is nil. That's the reason for
8556 treating terminal frames specially here. */
8557
8558 if (!FRAME_WINDOW_P (it->f))
8559 move_it_vertically (it, target_y - (it->current_y + line_height));
8560 else
8561 {
8562 do
8563 {
8564 move_it_by_lines (it, 1);
8565 }
8566 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8567 }
8568 }
8569 }
8570 }
8571
8572
8573 /* Move IT by a specified amount of pixel lines DY. DY negative means
8574 move backwards. DY = 0 means move to start of screen line. At the
8575 end, IT will be on the start of a screen line. */
8576
8577 void
8578 move_it_vertically (struct it *it, int dy)
8579 {
8580 if (dy <= 0)
8581 move_it_vertically_backward (it, -dy);
8582 else
8583 {
8584 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8585 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8586 MOVE_TO_POS | MOVE_TO_Y);
8587 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8588
8589 /* If buffer ends in ZV without a newline, move to the start of
8590 the line to satisfy the post-condition. */
8591 if (IT_CHARPOS (*it) == ZV
8592 && ZV > BEGV
8593 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8594 move_it_by_lines (it, 0);
8595 }
8596 }
8597
8598
8599 /* Move iterator IT past the end of the text line it is in. */
8600
8601 void
8602 move_it_past_eol (struct it *it)
8603 {
8604 enum move_it_result rc;
8605
8606 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8607 if (rc == MOVE_NEWLINE_OR_CR)
8608 set_iterator_to_next (it, 0);
8609 }
8610
8611
8612 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8613 negative means move up. DVPOS == 0 means move to the start of the
8614 screen line.
8615
8616 Optimization idea: If we would know that IT->f doesn't use
8617 a face with proportional font, we could be faster for
8618 truncate-lines nil. */
8619
8620 void
8621 move_it_by_lines (struct it *it, int dvpos)
8622 {
8623
8624 /* The commented-out optimization uses vmotion on terminals. This
8625 gives bad results, because elements like it->what, on which
8626 callers such as pos_visible_p rely, aren't updated. */
8627 /* struct position pos;
8628 if (!FRAME_WINDOW_P (it->f))
8629 {
8630 struct text_pos textpos;
8631
8632 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8633 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8634 reseat (it, textpos, 1);
8635 it->vpos += pos.vpos;
8636 it->current_y += pos.vpos;
8637 }
8638 else */
8639
8640 if (dvpos == 0)
8641 {
8642 /* DVPOS == 0 means move to the start of the screen line. */
8643 move_it_vertically_backward (it, 0);
8644 xassert (it->current_x == 0 && it->hpos == 0);
8645 /* Let next call to line_bottom_y calculate real line height */
8646 last_height = 0;
8647 }
8648 else if (dvpos > 0)
8649 {
8650 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8651 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8652 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8653 }
8654 else
8655 {
8656 struct it it2;
8657 void *it2data = NULL;
8658 EMACS_INT start_charpos, i;
8659
8660 /* Start at the beginning of the screen line containing IT's
8661 position. This may actually move vertically backwards,
8662 in case of overlays, so adjust dvpos accordingly. */
8663 dvpos += it->vpos;
8664 move_it_vertically_backward (it, 0);
8665 dvpos -= it->vpos;
8666
8667 /* Go back -DVPOS visible lines and reseat the iterator there. */
8668 start_charpos = IT_CHARPOS (*it);
8669 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8670 back_to_previous_visible_line_start (it);
8671 reseat (it, it->current.pos, 1);
8672
8673 /* Move further back if we end up in a string or an image. */
8674 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8675 {
8676 /* First try to move to start of display line. */
8677 dvpos += it->vpos;
8678 move_it_vertically_backward (it, 0);
8679 dvpos -= it->vpos;
8680 if (IT_POS_VALID_AFTER_MOVE_P (it))
8681 break;
8682 /* If start of line is still in string or image,
8683 move further back. */
8684 back_to_previous_visible_line_start (it);
8685 reseat (it, it->current.pos, 1);
8686 dvpos--;
8687 }
8688
8689 it->current_x = it->hpos = 0;
8690
8691 /* Above call may have moved too far if continuation lines
8692 are involved. Scan forward and see if it did. */
8693 SAVE_IT (it2, *it, it2data);
8694 it2.vpos = it2.current_y = 0;
8695 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8696 it->vpos -= it2.vpos;
8697 it->current_y -= it2.current_y;
8698 it->current_x = it->hpos = 0;
8699
8700 /* If we moved too far back, move IT some lines forward. */
8701 if (it2.vpos > -dvpos)
8702 {
8703 int delta = it2.vpos + dvpos;
8704
8705 RESTORE_IT (&it2, &it2, it2data);
8706 SAVE_IT (it2, *it, it2data);
8707 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8708 /* Move back again if we got too far ahead. */
8709 if (IT_CHARPOS (*it) >= start_charpos)
8710 RESTORE_IT (it, &it2, it2data);
8711 else
8712 bidi_unshelve_cache (it2data, 1);
8713 }
8714 else
8715 RESTORE_IT (it, it, it2data);
8716 }
8717 }
8718
8719 /* Return 1 if IT points into the middle of a display vector. */
8720
8721 int
8722 in_display_vector_p (struct it *it)
8723 {
8724 return (it->method == GET_FROM_DISPLAY_VECTOR
8725 && it->current.dpvec_index > 0
8726 && it->dpvec + it->current.dpvec_index != it->dpend);
8727 }
8728
8729 \f
8730 /***********************************************************************
8731 Messages
8732 ***********************************************************************/
8733
8734
8735 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8736 to *Messages*. */
8737
8738 void
8739 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8740 {
8741 Lisp_Object args[3];
8742 Lisp_Object msg, fmt;
8743 char *buffer;
8744 EMACS_INT len;
8745 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8746 USE_SAFE_ALLOCA;
8747
8748 /* Do nothing if called asynchronously. Inserting text into
8749 a buffer may call after-change-functions and alike and
8750 that would means running Lisp asynchronously. */
8751 if (handling_signal)
8752 return;
8753
8754 fmt = msg = Qnil;
8755 GCPRO4 (fmt, msg, arg1, arg2);
8756
8757 args[0] = fmt = build_string (format);
8758 args[1] = arg1;
8759 args[2] = arg2;
8760 msg = Fformat (3, args);
8761
8762 len = SBYTES (msg) + 1;
8763 SAFE_ALLOCA (buffer, char *, len);
8764 memcpy (buffer, SDATA (msg), len);
8765
8766 message_dolog (buffer, len - 1, 1, 0);
8767 SAFE_FREE ();
8768
8769 UNGCPRO;
8770 }
8771
8772
8773 /* Output a newline in the *Messages* buffer if "needs" one. */
8774
8775 void
8776 message_log_maybe_newline (void)
8777 {
8778 if (message_log_need_newline)
8779 message_dolog ("", 0, 1, 0);
8780 }
8781
8782
8783 /* Add a string M of length NBYTES to the message log, optionally
8784 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8785 nonzero, means interpret the contents of M as multibyte. This
8786 function calls low-level routines in order to bypass text property
8787 hooks, etc. which might not be safe to run.
8788
8789 This may GC (insert may run before/after change hooks),
8790 so the buffer M must NOT point to a Lisp string. */
8791
8792 void
8793 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8794 {
8795 const unsigned char *msg = (const unsigned char *) m;
8796
8797 if (!NILP (Vmemory_full))
8798 return;
8799
8800 if (!NILP (Vmessage_log_max))
8801 {
8802 struct buffer *oldbuf;
8803 Lisp_Object oldpoint, oldbegv, oldzv;
8804 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8805 EMACS_INT point_at_end = 0;
8806 EMACS_INT zv_at_end = 0;
8807 Lisp_Object old_deactivate_mark, tem;
8808 struct gcpro gcpro1;
8809
8810 old_deactivate_mark = Vdeactivate_mark;
8811 oldbuf = current_buffer;
8812 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8813 BVAR (current_buffer, undo_list) = Qt;
8814
8815 oldpoint = message_dolog_marker1;
8816 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8817 oldbegv = message_dolog_marker2;
8818 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8819 oldzv = message_dolog_marker3;
8820 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8821 GCPRO1 (old_deactivate_mark);
8822
8823 if (PT == Z)
8824 point_at_end = 1;
8825 if (ZV == Z)
8826 zv_at_end = 1;
8827
8828 BEGV = BEG;
8829 BEGV_BYTE = BEG_BYTE;
8830 ZV = Z;
8831 ZV_BYTE = Z_BYTE;
8832 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8833
8834 /* Insert the string--maybe converting multibyte to single byte
8835 or vice versa, so that all the text fits the buffer. */
8836 if (multibyte
8837 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8838 {
8839 EMACS_INT i;
8840 int c, char_bytes;
8841 char work[1];
8842
8843 /* Convert a multibyte string to single-byte
8844 for the *Message* buffer. */
8845 for (i = 0; i < nbytes; i += char_bytes)
8846 {
8847 c = string_char_and_length (msg + i, &char_bytes);
8848 work[0] = (ASCII_CHAR_P (c)
8849 ? c
8850 : multibyte_char_to_unibyte (c));
8851 insert_1_both (work, 1, 1, 1, 0, 0);
8852 }
8853 }
8854 else if (! multibyte
8855 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8856 {
8857 EMACS_INT i;
8858 int c, char_bytes;
8859 unsigned char str[MAX_MULTIBYTE_LENGTH];
8860 /* Convert a single-byte string to multibyte
8861 for the *Message* buffer. */
8862 for (i = 0; i < nbytes; i++)
8863 {
8864 c = msg[i];
8865 MAKE_CHAR_MULTIBYTE (c);
8866 char_bytes = CHAR_STRING (c, str);
8867 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8868 }
8869 }
8870 else if (nbytes)
8871 insert_1 (m, nbytes, 1, 0, 0);
8872
8873 if (nlflag)
8874 {
8875 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8876 printmax_t dups;
8877 insert_1 ("\n", 1, 1, 0, 0);
8878
8879 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8880 this_bol = PT;
8881 this_bol_byte = PT_BYTE;
8882
8883 /* See if this line duplicates the previous one.
8884 If so, combine duplicates. */
8885 if (this_bol > BEG)
8886 {
8887 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8888 prev_bol = PT;
8889 prev_bol_byte = PT_BYTE;
8890
8891 dups = message_log_check_duplicate (prev_bol_byte,
8892 this_bol_byte);
8893 if (dups)
8894 {
8895 del_range_both (prev_bol, prev_bol_byte,
8896 this_bol, this_bol_byte, 0);
8897 if (dups > 1)
8898 {
8899 char dupstr[sizeof " [ times]"
8900 + INT_STRLEN_BOUND (printmax_t)];
8901 int duplen;
8902
8903 /* If you change this format, don't forget to also
8904 change message_log_check_duplicate. */
8905 sprintf (dupstr, " [%"pMd" times]", dups);
8906 duplen = strlen (dupstr);
8907 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8908 insert_1 (dupstr, duplen, 1, 0, 1);
8909 }
8910 }
8911 }
8912
8913 /* If we have more than the desired maximum number of lines
8914 in the *Messages* buffer now, delete the oldest ones.
8915 This is safe because we don't have undo in this buffer. */
8916
8917 if (NATNUMP (Vmessage_log_max))
8918 {
8919 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8920 -XFASTINT (Vmessage_log_max) - 1, 0);
8921 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8922 }
8923 }
8924 BEGV = XMARKER (oldbegv)->charpos;
8925 BEGV_BYTE = marker_byte_position (oldbegv);
8926
8927 if (zv_at_end)
8928 {
8929 ZV = Z;
8930 ZV_BYTE = Z_BYTE;
8931 }
8932 else
8933 {
8934 ZV = XMARKER (oldzv)->charpos;
8935 ZV_BYTE = marker_byte_position (oldzv);
8936 }
8937
8938 if (point_at_end)
8939 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8940 else
8941 /* We can't do Fgoto_char (oldpoint) because it will run some
8942 Lisp code. */
8943 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8944 XMARKER (oldpoint)->bytepos);
8945
8946 UNGCPRO;
8947 unchain_marker (XMARKER (oldpoint));
8948 unchain_marker (XMARKER (oldbegv));
8949 unchain_marker (XMARKER (oldzv));
8950
8951 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8952 set_buffer_internal (oldbuf);
8953 if (NILP (tem))
8954 windows_or_buffers_changed = old_windows_or_buffers_changed;
8955 message_log_need_newline = !nlflag;
8956 Vdeactivate_mark = old_deactivate_mark;
8957 }
8958 }
8959
8960
8961 /* We are at the end of the buffer after just having inserted a newline.
8962 (Note: We depend on the fact we won't be crossing the gap.)
8963 Check to see if the most recent message looks a lot like the previous one.
8964 Return 0 if different, 1 if the new one should just replace it, or a
8965 value N > 1 if we should also append " [N times]". */
8966
8967 static intmax_t
8968 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8969 {
8970 EMACS_INT i;
8971 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8972 int seen_dots = 0;
8973 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8974 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8975
8976 for (i = 0; i < len; i++)
8977 {
8978 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8979 seen_dots = 1;
8980 if (p1[i] != p2[i])
8981 return seen_dots;
8982 }
8983 p1 += len;
8984 if (*p1 == '\n')
8985 return 2;
8986 if (*p1++ == ' ' && *p1++ == '[')
8987 {
8988 char *pend;
8989 intmax_t n = strtoimax ((char *) p1, &pend, 10);
8990 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
8991 return n+1;
8992 }
8993 return 0;
8994 }
8995 \f
8996
8997 /* Display an echo area message M with a specified length of NBYTES
8998 bytes. The string may include null characters. If M is 0, clear
8999 out any existing message, and let the mini-buffer text show
9000 through.
9001
9002 This may GC, so the buffer M must NOT point to a Lisp string. */
9003
9004 void
9005 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9006 {
9007 /* First flush out any partial line written with print. */
9008 message_log_maybe_newline ();
9009 if (m)
9010 message_dolog (m, nbytes, 1, multibyte);
9011 message2_nolog (m, nbytes, multibyte);
9012 }
9013
9014
9015 /* The non-logging counterpart of message2. */
9016
9017 void
9018 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9019 {
9020 struct frame *sf = SELECTED_FRAME ();
9021 message_enable_multibyte = multibyte;
9022
9023 if (FRAME_INITIAL_P (sf))
9024 {
9025 if (noninteractive_need_newline)
9026 putc ('\n', stderr);
9027 noninteractive_need_newline = 0;
9028 if (m)
9029 fwrite (m, nbytes, 1, stderr);
9030 if (cursor_in_echo_area == 0)
9031 fprintf (stderr, "\n");
9032 fflush (stderr);
9033 }
9034 /* A null message buffer means that the frame hasn't really been
9035 initialized yet. Error messages get reported properly by
9036 cmd_error, so this must be just an informative message; toss it. */
9037 else if (INTERACTIVE
9038 && sf->glyphs_initialized_p
9039 && FRAME_MESSAGE_BUF (sf))
9040 {
9041 Lisp_Object mini_window;
9042 struct frame *f;
9043
9044 /* Get the frame containing the mini-buffer
9045 that the selected frame is using. */
9046 mini_window = FRAME_MINIBUF_WINDOW (sf);
9047 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9048
9049 FRAME_SAMPLE_VISIBILITY (f);
9050 if (FRAME_VISIBLE_P (sf)
9051 && ! FRAME_VISIBLE_P (f))
9052 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9053
9054 if (m)
9055 {
9056 set_message (m, Qnil, nbytes, multibyte);
9057 if (minibuffer_auto_raise)
9058 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9059 }
9060 else
9061 clear_message (1, 1);
9062
9063 do_pending_window_change (0);
9064 echo_area_display (1);
9065 do_pending_window_change (0);
9066 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9067 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9068 }
9069 }
9070
9071
9072 /* Display an echo area message M with a specified length of NBYTES
9073 bytes. The string may include null characters. If M is not a
9074 string, clear out any existing message, and let the mini-buffer
9075 text show through.
9076
9077 This function cancels echoing. */
9078
9079 void
9080 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9081 {
9082 struct gcpro gcpro1;
9083
9084 GCPRO1 (m);
9085 clear_message (1,1);
9086 cancel_echoing ();
9087
9088 /* First flush out any partial line written with print. */
9089 message_log_maybe_newline ();
9090 if (STRINGP (m))
9091 {
9092 char *buffer;
9093 USE_SAFE_ALLOCA;
9094
9095 SAFE_ALLOCA (buffer, char *, nbytes);
9096 memcpy (buffer, SDATA (m), nbytes);
9097 message_dolog (buffer, nbytes, 1, multibyte);
9098 SAFE_FREE ();
9099 }
9100 message3_nolog (m, nbytes, multibyte);
9101
9102 UNGCPRO;
9103 }
9104
9105
9106 /* The non-logging version of message3.
9107 This does not cancel echoing, because it is used for echoing.
9108 Perhaps we need to make a separate function for echoing
9109 and make this cancel echoing. */
9110
9111 void
9112 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9113 {
9114 struct frame *sf = SELECTED_FRAME ();
9115 message_enable_multibyte = multibyte;
9116
9117 if (FRAME_INITIAL_P (sf))
9118 {
9119 if (noninteractive_need_newline)
9120 putc ('\n', stderr);
9121 noninteractive_need_newline = 0;
9122 if (STRINGP (m))
9123 fwrite (SDATA (m), nbytes, 1, stderr);
9124 if (cursor_in_echo_area == 0)
9125 fprintf (stderr, "\n");
9126 fflush (stderr);
9127 }
9128 /* A null message buffer means that the frame hasn't really been
9129 initialized yet. Error messages get reported properly by
9130 cmd_error, so this must be just an informative message; toss it. */
9131 else if (INTERACTIVE
9132 && sf->glyphs_initialized_p
9133 && FRAME_MESSAGE_BUF (sf))
9134 {
9135 Lisp_Object mini_window;
9136 Lisp_Object frame;
9137 struct frame *f;
9138
9139 /* Get the frame containing the mini-buffer
9140 that the selected frame is using. */
9141 mini_window = FRAME_MINIBUF_WINDOW (sf);
9142 frame = XWINDOW (mini_window)->frame;
9143 f = XFRAME (frame);
9144
9145 FRAME_SAMPLE_VISIBILITY (f);
9146 if (FRAME_VISIBLE_P (sf)
9147 && !FRAME_VISIBLE_P (f))
9148 Fmake_frame_visible (frame);
9149
9150 if (STRINGP (m) && SCHARS (m) > 0)
9151 {
9152 set_message (NULL, m, nbytes, multibyte);
9153 if (minibuffer_auto_raise)
9154 Fraise_frame (frame);
9155 /* Assume we are not echoing.
9156 (If we are, echo_now will override this.) */
9157 echo_message_buffer = Qnil;
9158 }
9159 else
9160 clear_message (1, 1);
9161
9162 do_pending_window_change (0);
9163 echo_area_display (1);
9164 do_pending_window_change (0);
9165 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9166 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9167 }
9168 }
9169
9170
9171 /* Display a null-terminated echo area message M. If M is 0, clear
9172 out any existing message, and let the mini-buffer text show through.
9173
9174 The buffer M must continue to exist until after the echo area gets
9175 cleared or some other message gets displayed there. Do not pass
9176 text that is stored in a Lisp string. Do not pass text in a buffer
9177 that was alloca'd. */
9178
9179 void
9180 message1 (const char *m)
9181 {
9182 message2 (m, (m ? strlen (m) : 0), 0);
9183 }
9184
9185
9186 /* The non-logging counterpart of message1. */
9187
9188 void
9189 message1_nolog (const char *m)
9190 {
9191 message2_nolog (m, (m ? strlen (m) : 0), 0);
9192 }
9193
9194 /* Display a message M which contains a single %s
9195 which gets replaced with STRING. */
9196
9197 void
9198 message_with_string (const char *m, Lisp_Object string, int log)
9199 {
9200 CHECK_STRING (string);
9201
9202 if (noninteractive)
9203 {
9204 if (m)
9205 {
9206 if (noninteractive_need_newline)
9207 putc ('\n', stderr);
9208 noninteractive_need_newline = 0;
9209 fprintf (stderr, m, SDATA (string));
9210 if (!cursor_in_echo_area)
9211 fprintf (stderr, "\n");
9212 fflush (stderr);
9213 }
9214 }
9215 else if (INTERACTIVE)
9216 {
9217 /* The frame whose minibuffer we're going to display the message on.
9218 It may be larger than the selected frame, so we need
9219 to use its buffer, not the selected frame's buffer. */
9220 Lisp_Object mini_window;
9221 struct frame *f, *sf = SELECTED_FRAME ();
9222
9223 /* Get the frame containing the minibuffer
9224 that the selected frame is using. */
9225 mini_window = FRAME_MINIBUF_WINDOW (sf);
9226 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9227
9228 /* A null message buffer means that the frame hasn't really been
9229 initialized yet. Error messages get reported properly by
9230 cmd_error, so this must be just an informative message; toss it. */
9231 if (FRAME_MESSAGE_BUF (f))
9232 {
9233 Lisp_Object args[2], msg;
9234 struct gcpro gcpro1, gcpro2;
9235
9236 args[0] = build_string (m);
9237 args[1] = msg = string;
9238 GCPRO2 (args[0], msg);
9239 gcpro1.nvars = 2;
9240
9241 msg = Fformat (2, args);
9242
9243 if (log)
9244 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9245 else
9246 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9247
9248 UNGCPRO;
9249
9250 /* Print should start at the beginning of the message
9251 buffer next time. */
9252 message_buf_print = 0;
9253 }
9254 }
9255 }
9256
9257
9258 /* Dump an informative message to the minibuf. If M is 0, clear out
9259 any existing message, and let the mini-buffer text show through. */
9260
9261 static void
9262 vmessage (const char *m, va_list ap)
9263 {
9264 if (noninteractive)
9265 {
9266 if (m)
9267 {
9268 if (noninteractive_need_newline)
9269 putc ('\n', stderr);
9270 noninteractive_need_newline = 0;
9271 vfprintf (stderr, m, ap);
9272 if (cursor_in_echo_area == 0)
9273 fprintf (stderr, "\n");
9274 fflush (stderr);
9275 }
9276 }
9277 else if (INTERACTIVE)
9278 {
9279 /* The frame whose mini-buffer we're going to display the message
9280 on. It may be larger than the selected frame, so we need to
9281 use its buffer, not the selected frame's buffer. */
9282 Lisp_Object mini_window;
9283 struct frame *f, *sf = SELECTED_FRAME ();
9284
9285 /* Get the frame containing the mini-buffer
9286 that the selected frame is using. */
9287 mini_window = FRAME_MINIBUF_WINDOW (sf);
9288 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9289
9290 /* A null message buffer means that the frame hasn't really been
9291 initialized yet. Error messages get reported properly by
9292 cmd_error, so this must be just an informative message; toss
9293 it. */
9294 if (FRAME_MESSAGE_BUF (f))
9295 {
9296 if (m)
9297 {
9298 ptrdiff_t len;
9299
9300 len = doprnt (FRAME_MESSAGE_BUF (f),
9301 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9302
9303 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9304 }
9305 else
9306 message1 (0);
9307
9308 /* Print should start at the beginning of the message
9309 buffer next time. */
9310 message_buf_print = 0;
9311 }
9312 }
9313 }
9314
9315 void
9316 message (const char *m, ...)
9317 {
9318 va_list ap;
9319 va_start (ap, m);
9320 vmessage (m, ap);
9321 va_end (ap);
9322 }
9323
9324
9325 #if 0
9326 /* The non-logging version of message. */
9327
9328 void
9329 message_nolog (const char *m, ...)
9330 {
9331 Lisp_Object old_log_max;
9332 va_list ap;
9333 va_start (ap, m);
9334 old_log_max = Vmessage_log_max;
9335 Vmessage_log_max = Qnil;
9336 vmessage (m, ap);
9337 Vmessage_log_max = old_log_max;
9338 va_end (ap);
9339 }
9340 #endif
9341
9342
9343 /* Display the current message in the current mini-buffer. This is
9344 only called from error handlers in process.c, and is not time
9345 critical. */
9346
9347 void
9348 update_echo_area (void)
9349 {
9350 if (!NILP (echo_area_buffer[0]))
9351 {
9352 Lisp_Object string;
9353 string = Fcurrent_message ();
9354 message3 (string, SBYTES (string),
9355 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9356 }
9357 }
9358
9359
9360 /* Make sure echo area buffers in `echo_buffers' are live.
9361 If they aren't, make new ones. */
9362
9363 static void
9364 ensure_echo_area_buffers (void)
9365 {
9366 int i;
9367
9368 for (i = 0; i < 2; ++i)
9369 if (!BUFFERP (echo_buffer[i])
9370 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9371 {
9372 char name[30];
9373 Lisp_Object old_buffer;
9374 int j;
9375
9376 old_buffer = echo_buffer[i];
9377 sprintf (name, " *Echo Area %d*", i);
9378 echo_buffer[i] = Fget_buffer_create (build_string (name));
9379 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9380 /* to force word wrap in echo area -
9381 it was decided to postpone this*/
9382 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9383
9384 for (j = 0; j < 2; ++j)
9385 if (EQ (old_buffer, echo_area_buffer[j]))
9386 echo_area_buffer[j] = echo_buffer[i];
9387 }
9388 }
9389
9390
9391 /* Call FN with args A1..A4 with either the current or last displayed
9392 echo_area_buffer as current buffer.
9393
9394 WHICH zero means use the current message buffer
9395 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9396 from echo_buffer[] and clear it.
9397
9398 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9399 suitable buffer from echo_buffer[] and clear it.
9400
9401 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9402 that the current message becomes the last displayed one, make
9403 choose a suitable buffer for echo_area_buffer[0], and clear it.
9404
9405 Value is what FN returns. */
9406
9407 static int
9408 with_echo_area_buffer (struct window *w, int which,
9409 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9410 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9411 {
9412 Lisp_Object buffer;
9413 int this_one, the_other, clear_buffer_p, rc;
9414 int count = SPECPDL_INDEX ();
9415
9416 /* If buffers aren't live, make new ones. */
9417 ensure_echo_area_buffers ();
9418
9419 clear_buffer_p = 0;
9420
9421 if (which == 0)
9422 this_one = 0, the_other = 1;
9423 else if (which > 0)
9424 this_one = 1, the_other = 0;
9425 else
9426 {
9427 this_one = 0, the_other = 1;
9428 clear_buffer_p = 1;
9429
9430 /* We need a fresh one in case the current echo buffer equals
9431 the one containing the last displayed echo area message. */
9432 if (!NILP (echo_area_buffer[this_one])
9433 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9434 echo_area_buffer[this_one] = Qnil;
9435 }
9436
9437 /* Choose a suitable buffer from echo_buffer[] is we don't
9438 have one. */
9439 if (NILP (echo_area_buffer[this_one]))
9440 {
9441 echo_area_buffer[this_one]
9442 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9443 ? echo_buffer[the_other]
9444 : echo_buffer[this_one]);
9445 clear_buffer_p = 1;
9446 }
9447
9448 buffer = echo_area_buffer[this_one];
9449
9450 /* Don't get confused by reusing the buffer used for echoing
9451 for a different purpose. */
9452 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9453 cancel_echoing ();
9454
9455 record_unwind_protect (unwind_with_echo_area_buffer,
9456 with_echo_area_buffer_unwind_data (w));
9457
9458 /* Make the echo area buffer current. Note that for display
9459 purposes, it is not necessary that the displayed window's buffer
9460 == current_buffer, except for text property lookup. So, let's
9461 only set that buffer temporarily here without doing a full
9462 Fset_window_buffer. We must also change w->pointm, though,
9463 because otherwise an assertions in unshow_buffer fails, and Emacs
9464 aborts. */
9465 set_buffer_internal_1 (XBUFFER (buffer));
9466 if (w)
9467 {
9468 w->buffer = buffer;
9469 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9470 }
9471
9472 BVAR (current_buffer, undo_list) = Qt;
9473 BVAR (current_buffer, read_only) = Qnil;
9474 specbind (Qinhibit_read_only, Qt);
9475 specbind (Qinhibit_modification_hooks, Qt);
9476
9477 if (clear_buffer_p && Z > BEG)
9478 del_range (BEG, Z);
9479
9480 xassert (BEGV >= BEG);
9481 xassert (ZV <= Z && ZV >= BEGV);
9482
9483 rc = fn (a1, a2, a3, a4);
9484
9485 xassert (BEGV >= BEG);
9486 xassert (ZV <= Z && ZV >= BEGV);
9487
9488 unbind_to (count, Qnil);
9489 return rc;
9490 }
9491
9492
9493 /* Save state that should be preserved around the call to the function
9494 FN called in with_echo_area_buffer. */
9495
9496 static Lisp_Object
9497 with_echo_area_buffer_unwind_data (struct window *w)
9498 {
9499 int i = 0;
9500 Lisp_Object vector, tmp;
9501
9502 /* Reduce consing by keeping one vector in
9503 Vwith_echo_area_save_vector. */
9504 vector = Vwith_echo_area_save_vector;
9505 Vwith_echo_area_save_vector = Qnil;
9506
9507 if (NILP (vector))
9508 vector = Fmake_vector (make_number (7), Qnil);
9509
9510 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9511 ASET (vector, i, Vdeactivate_mark); ++i;
9512 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9513
9514 if (w)
9515 {
9516 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9517 ASET (vector, i, w->buffer); ++i;
9518 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9519 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9520 }
9521 else
9522 {
9523 int end = i + 4;
9524 for (; i < end; ++i)
9525 ASET (vector, i, Qnil);
9526 }
9527
9528 xassert (i == ASIZE (vector));
9529 return vector;
9530 }
9531
9532
9533 /* Restore global state from VECTOR which was created by
9534 with_echo_area_buffer_unwind_data. */
9535
9536 static Lisp_Object
9537 unwind_with_echo_area_buffer (Lisp_Object vector)
9538 {
9539 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9540 Vdeactivate_mark = AREF (vector, 1);
9541 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9542
9543 if (WINDOWP (AREF (vector, 3)))
9544 {
9545 struct window *w;
9546 Lisp_Object buffer, charpos, bytepos;
9547
9548 w = XWINDOW (AREF (vector, 3));
9549 buffer = AREF (vector, 4);
9550 charpos = AREF (vector, 5);
9551 bytepos = AREF (vector, 6);
9552
9553 w->buffer = buffer;
9554 set_marker_both (w->pointm, buffer,
9555 XFASTINT (charpos), XFASTINT (bytepos));
9556 }
9557
9558 Vwith_echo_area_save_vector = vector;
9559 return Qnil;
9560 }
9561
9562
9563 /* Set up the echo area for use by print functions. MULTIBYTE_P
9564 non-zero means we will print multibyte. */
9565
9566 void
9567 setup_echo_area_for_printing (int multibyte_p)
9568 {
9569 /* If we can't find an echo area any more, exit. */
9570 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9571 Fkill_emacs (Qnil);
9572
9573 ensure_echo_area_buffers ();
9574
9575 if (!message_buf_print)
9576 {
9577 /* A message has been output since the last time we printed.
9578 Choose a fresh echo area buffer. */
9579 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9580 echo_area_buffer[0] = echo_buffer[1];
9581 else
9582 echo_area_buffer[0] = echo_buffer[0];
9583
9584 /* Switch to that buffer and clear it. */
9585 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9586 BVAR (current_buffer, truncate_lines) = Qnil;
9587
9588 if (Z > BEG)
9589 {
9590 int count = SPECPDL_INDEX ();
9591 specbind (Qinhibit_read_only, Qt);
9592 /* Note that undo recording is always disabled. */
9593 del_range (BEG, Z);
9594 unbind_to (count, Qnil);
9595 }
9596 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9597
9598 /* Set up the buffer for the multibyteness we need. */
9599 if (multibyte_p
9600 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9601 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9602
9603 /* Raise the frame containing the echo area. */
9604 if (minibuffer_auto_raise)
9605 {
9606 struct frame *sf = SELECTED_FRAME ();
9607 Lisp_Object mini_window;
9608 mini_window = FRAME_MINIBUF_WINDOW (sf);
9609 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9610 }
9611
9612 message_log_maybe_newline ();
9613 message_buf_print = 1;
9614 }
9615 else
9616 {
9617 if (NILP (echo_area_buffer[0]))
9618 {
9619 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9620 echo_area_buffer[0] = echo_buffer[1];
9621 else
9622 echo_area_buffer[0] = echo_buffer[0];
9623 }
9624
9625 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9626 {
9627 /* Someone switched buffers between print requests. */
9628 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9629 BVAR (current_buffer, truncate_lines) = Qnil;
9630 }
9631 }
9632 }
9633
9634
9635 /* Display an echo area message in window W. Value is non-zero if W's
9636 height is changed. If display_last_displayed_message_p is
9637 non-zero, display the message that was last displayed, otherwise
9638 display the current message. */
9639
9640 static int
9641 display_echo_area (struct window *w)
9642 {
9643 int i, no_message_p, window_height_changed_p, count;
9644
9645 /* Temporarily disable garbage collections while displaying the echo
9646 area. This is done because a GC can print a message itself.
9647 That message would modify the echo area buffer's contents while a
9648 redisplay of the buffer is going on, and seriously confuse
9649 redisplay. */
9650 count = inhibit_garbage_collection ();
9651
9652 /* If there is no message, we must call display_echo_area_1
9653 nevertheless because it resizes the window. But we will have to
9654 reset the echo_area_buffer in question to nil at the end because
9655 with_echo_area_buffer will sets it to an empty buffer. */
9656 i = display_last_displayed_message_p ? 1 : 0;
9657 no_message_p = NILP (echo_area_buffer[i]);
9658
9659 window_height_changed_p
9660 = with_echo_area_buffer (w, display_last_displayed_message_p,
9661 display_echo_area_1,
9662 (intptr_t) w, Qnil, 0, 0);
9663
9664 if (no_message_p)
9665 echo_area_buffer[i] = Qnil;
9666
9667 unbind_to (count, Qnil);
9668 return window_height_changed_p;
9669 }
9670
9671
9672 /* Helper for display_echo_area. Display the current buffer which
9673 contains the current echo area message in window W, a mini-window,
9674 a pointer to which is passed in A1. A2..A4 are currently not used.
9675 Change the height of W so that all of the message is displayed.
9676 Value is non-zero if height of W was changed. */
9677
9678 static int
9679 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9680 {
9681 intptr_t i1 = a1;
9682 struct window *w = (struct window *) i1;
9683 Lisp_Object window;
9684 struct text_pos start;
9685 int window_height_changed_p = 0;
9686
9687 /* Do this before displaying, so that we have a large enough glyph
9688 matrix for the display. If we can't get enough space for the
9689 whole text, display the last N lines. That works by setting w->start. */
9690 window_height_changed_p = resize_mini_window (w, 0);
9691
9692 /* Use the starting position chosen by resize_mini_window. */
9693 SET_TEXT_POS_FROM_MARKER (start, w->start);
9694
9695 /* Display. */
9696 clear_glyph_matrix (w->desired_matrix);
9697 XSETWINDOW (window, w);
9698 try_window (window, start, 0);
9699
9700 return window_height_changed_p;
9701 }
9702
9703
9704 /* Resize the echo area window to exactly the size needed for the
9705 currently displayed message, if there is one. If a mini-buffer
9706 is active, don't shrink it. */
9707
9708 void
9709 resize_echo_area_exactly (void)
9710 {
9711 if (BUFFERP (echo_area_buffer[0])
9712 && WINDOWP (echo_area_window))
9713 {
9714 struct window *w = XWINDOW (echo_area_window);
9715 int resized_p;
9716 Lisp_Object resize_exactly;
9717
9718 if (minibuf_level == 0)
9719 resize_exactly = Qt;
9720 else
9721 resize_exactly = Qnil;
9722
9723 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9724 (intptr_t) w, resize_exactly,
9725 0, 0);
9726 if (resized_p)
9727 {
9728 ++windows_or_buffers_changed;
9729 ++update_mode_lines;
9730 redisplay_internal ();
9731 }
9732 }
9733 }
9734
9735
9736 /* Callback function for with_echo_area_buffer, when used from
9737 resize_echo_area_exactly. A1 contains a pointer to the window to
9738 resize, EXACTLY non-nil means resize the mini-window exactly to the
9739 size of the text displayed. A3 and A4 are not used. Value is what
9740 resize_mini_window returns. */
9741
9742 static int
9743 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9744 {
9745 intptr_t i1 = a1;
9746 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9747 }
9748
9749
9750 /* Resize mini-window W to fit the size of its contents. EXACT_P
9751 means size the window exactly to the size needed. Otherwise, it's
9752 only enlarged until W's buffer is empty.
9753
9754 Set W->start to the right place to begin display. If the whole
9755 contents fit, start at the beginning. Otherwise, start so as
9756 to make the end of the contents appear. This is particularly
9757 important for y-or-n-p, but seems desirable generally.
9758
9759 Value is non-zero if the window height has been changed. */
9760
9761 int
9762 resize_mini_window (struct window *w, int exact_p)
9763 {
9764 struct frame *f = XFRAME (w->frame);
9765 int window_height_changed_p = 0;
9766
9767 xassert (MINI_WINDOW_P (w));
9768
9769 /* By default, start display at the beginning. */
9770 set_marker_both (w->start, w->buffer,
9771 BUF_BEGV (XBUFFER (w->buffer)),
9772 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9773
9774 /* Don't resize windows while redisplaying a window; it would
9775 confuse redisplay functions when the size of the window they are
9776 displaying changes from under them. Such a resizing can happen,
9777 for instance, when which-func prints a long message while
9778 we are running fontification-functions. We're running these
9779 functions with safe_call which binds inhibit-redisplay to t. */
9780 if (!NILP (Vinhibit_redisplay))
9781 return 0;
9782
9783 /* Nil means don't try to resize. */
9784 if (NILP (Vresize_mini_windows)
9785 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9786 return 0;
9787
9788 if (!FRAME_MINIBUF_ONLY_P (f))
9789 {
9790 struct it it;
9791 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9792 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9793 int height, max_height;
9794 int unit = FRAME_LINE_HEIGHT (f);
9795 struct text_pos start;
9796 struct buffer *old_current_buffer = NULL;
9797
9798 if (current_buffer != XBUFFER (w->buffer))
9799 {
9800 old_current_buffer = current_buffer;
9801 set_buffer_internal (XBUFFER (w->buffer));
9802 }
9803
9804 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9805
9806 /* Compute the max. number of lines specified by the user. */
9807 if (FLOATP (Vmax_mini_window_height))
9808 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9809 else if (INTEGERP (Vmax_mini_window_height))
9810 max_height = XINT (Vmax_mini_window_height);
9811 else
9812 max_height = total_height / 4;
9813
9814 /* Correct that max. height if it's bogus. */
9815 max_height = max (1, max_height);
9816 max_height = min (total_height, max_height);
9817
9818 /* Find out the height of the text in the window. */
9819 if (it.line_wrap == TRUNCATE)
9820 height = 1;
9821 else
9822 {
9823 last_height = 0;
9824 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9825 if (it.max_ascent == 0 && it.max_descent == 0)
9826 height = it.current_y + last_height;
9827 else
9828 height = it.current_y + it.max_ascent + it.max_descent;
9829 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9830 height = (height + unit - 1) / unit;
9831 }
9832
9833 /* Compute a suitable window start. */
9834 if (height > max_height)
9835 {
9836 height = max_height;
9837 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9838 move_it_vertically_backward (&it, (height - 1) * unit);
9839 start = it.current.pos;
9840 }
9841 else
9842 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9843 SET_MARKER_FROM_TEXT_POS (w->start, start);
9844
9845 if (EQ (Vresize_mini_windows, Qgrow_only))
9846 {
9847 /* Let it grow only, until we display an empty message, in which
9848 case the window shrinks again. */
9849 if (height > WINDOW_TOTAL_LINES (w))
9850 {
9851 int old_height = WINDOW_TOTAL_LINES (w);
9852 freeze_window_starts (f, 1);
9853 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9854 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9855 }
9856 else if (height < WINDOW_TOTAL_LINES (w)
9857 && (exact_p || BEGV == ZV))
9858 {
9859 int old_height = WINDOW_TOTAL_LINES (w);
9860 freeze_window_starts (f, 0);
9861 shrink_mini_window (w);
9862 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9863 }
9864 }
9865 else
9866 {
9867 /* Always resize to exact size needed. */
9868 if (height > WINDOW_TOTAL_LINES (w))
9869 {
9870 int old_height = WINDOW_TOTAL_LINES (w);
9871 freeze_window_starts (f, 1);
9872 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9873 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9874 }
9875 else if (height < WINDOW_TOTAL_LINES (w))
9876 {
9877 int old_height = WINDOW_TOTAL_LINES (w);
9878 freeze_window_starts (f, 0);
9879 shrink_mini_window (w);
9880
9881 if (height)
9882 {
9883 freeze_window_starts (f, 1);
9884 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9885 }
9886
9887 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9888 }
9889 }
9890
9891 if (old_current_buffer)
9892 set_buffer_internal (old_current_buffer);
9893 }
9894
9895 return window_height_changed_p;
9896 }
9897
9898
9899 /* Value is the current message, a string, or nil if there is no
9900 current message. */
9901
9902 Lisp_Object
9903 current_message (void)
9904 {
9905 Lisp_Object msg;
9906
9907 if (!BUFFERP (echo_area_buffer[0]))
9908 msg = Qnil;
9909 else
9910 {
9911 with_echo_area_buffer (0, 0, current_message_1,
9912 (intptr_t) &msg, Qnil, 0, 0);
9913 if (NILP (msg))
9914 echo_area_buffer[0] = Qnil;
9915 }
9916
9917 return msg;
9918 }
9919
9920
9921 static int
9922 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9923 {
9924 intptr_t i1 = a1;
9925 Lisp_Object *msg = (Lisp_Object *) i1;
9926
9927 if (Z > BEG)
9928 *msg = make_buffer_string (BEG, Z, 1);
9929 else
9930 *msg = Qnil;
9931 return 0;
9932 }
9933
9934
9935 /* Push the current message on Vmessage_stack for later restauration
9936 by restore_message. Value is non-zero if the current message isn't
9937 empty. This is a relatively infrequent operation, so it's not
9938 worth optimizing. */
9939
9940 int
9941 push_message (void)
9942 {
9943 Lisp_Object msg;
9944 msg = current_message ();
9945 Vmessage_stack = Fcons (msg, Vmessage_stack);
9946 return STRINGP (msg);
9947 }
9948
9949
9950 /* Restore message display from the top of Vmessage_stack. */
9951
9952 void
9953 restore_message (void)
9954 {
9955 Lisp_Object msg;
9956
9957 xassert (CONSP (Vmessage_stack));
9958 msg = XCAR (Vmessage_stack);
9959 if (STRINGP (msg))
9960 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9961 else
9962 message3_nolog (msg, 0, 0);
9963 }
9964
9965
9966 /* Handler for record_unwind_protect calling pop_message. */
9967
9968 Lisp_Object
9969 pop_message_unwind (Lisp_Object dummy)
9970 {
9971 pop_message ();
9972 return Qnil;
9973 }
9974
9975 /* Pop the top-most entry off Vmessage_stack. */
9976
9977 static void
9978 pop_message (void)
9979 {
9980 xassert (CONSP (Vmessage_stack));
9981 Vmessage_stack = XCDR (Vmessage_stack);
9982 }
9983
9984
9985 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9986 exits. If the stack is not empty, we have a missing pop_message
9987 somewhere. */
9988
9989 void
9990 check_message_stack (void)
9991 {
9992 if (!NILP (Vmessage_stack))
9993 abort ();
9994 }
9995
9996
9997 /* Truncate to NCHARS what will be displayed in the echo area the next
9998 time we display it---but don't redisplay it now. */
9999
10000 void
10001 truncate_echo_area (EMACS_INT nchars)
10002 {
10003 if (nchars == 0)
10004 echo_area_buffer[0] = Qnil;
10005 /* A null message buffer means that the frame hasn't really been
10006 initialized yet. Error messages get reported properly by
10007 cmd_error, so this must be just an informative message; toss it. */
10008 else if (!noninteractive
10009 && INTERACTIVE
10010 && !NILP (echo_area_buffer[0]))
10011 {
10012 struct frame *sf = SELECTED_FRAME ();
10013 if (FRAME_MESSAGE_BUF (sf))
10014 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10015 }
10016 }
10017
10018
10019 /* Helper function for truncate_echo_area. Truncate the current
10020 message to at most NCHARS characters. */
10021
10022 static int
10023 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10024 {
10025 if (BEG + nchars < Z)
10026 del_range (BEG + nchars, Z);
10027 if (Z == BEG)
10028 echo_area_buffer[0] = Qnil;
10029 return 0;
10030 }
10031
10032
10033 /* Set the current message to a substring of S or STRING.
10034
10035 If STRING is a Lisp string, set the message to the first NBYTES
10036 bytes from STRING. NBYTES zero means use the whole string. If
10037 STRING is multibyte, the message will be displayed multibyte.
10038
10039 If S is not null, set the message to the first LEN bytes of S. LEN
10040 zero means use the whole string. MULTIBYTE_P non-zero means S is
10041 multibyte. Display the message multibyte in that case.
10042
10043 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10044 to t before calling set_message_1 (which calls insert).
10045 */
10046
10047 static void
10048 set_message (const char *s, Lisp_Object string,
10049 EMACS_INT nbytes, int multibyte_p)
10050 {
10051 message_enable_multibyte
10052 = ((s && multibyte_p)
10053 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10054
10055 with_echo_area_buffer (0, -1, set_message_1,
10056 (intptr_t) s, string, nbytes, multibyte_p);
10057 message_buf_print = 0;
10058 help_echo_showing_p = 0;
10059 }
10060
10061
10062 /* Helper function for set_message. Arguments have the same meaning
10063 as there, with A1 corresponding to S and A2 corresponding to STRING
10064 This function is called with the echo area buffer being
10065 current. */
10066
10067 static int
10068 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10069 {
10070 intptr_t i1 = a1;
10071 const char *s = (const char *) i1;
10072 const unsigned char *msg = (const unsigned char *) s;
10073 Lisp_Object string = a2;
10074
10075 /* Change multibyteness of the echo buffer appropriately. */
10076 if (message_enable_multibyte
10077 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10078 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10079
10080 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10081 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10082 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10083
10084 /* Insert new message at BEG. */
10085 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10086
10087 if (STRINGP (string))
10088 {
10089 EMACS_INT nchars;
10090
10091 if (nbytes == 0)
10092 nbytes = SBYTES (string);
10093 nchars = string_byte_to_char (string, nbytes);
10094
10095 /* This function takes care of single/multibyte conversion. We
10096 just have to ensure that the echo area buffer has the right
10097 setting of enable_multibyte_characters. */
10098 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10099 }
10100 else if (s)
10101 {
10102 if (nbytes == 0)
10103 nbytes = strlen (s);
10104
10105 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10106 {
10107 /* Convert from multi-byte to single-byte. */
10108 EMACS_INT i;
10109 int c, n;
10110 char work[1];
10111
10112 /* Convert a multibyte string to single-byte. */
10113 for (i = 0; i < nbytes; i += n)
10114 {
10115 c = string_char_and_length (msg + i, &n);
10116 work[0] = (ASCII_CHAR_P (c)
10117 ? c
10118 : multibyte_char_to_unibyte (c));
10119 insert_1_both (work, 1, 1, 1, 0, 0);
10120 }
10121 }
10122 else if (!multibyte_p
10123 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10124 {
10125 /* Convert from single-byte to multi-byte. */
10126 EMACS_INT i;
10127 int c, n;
10128 unsigned char str[MAX_MULTIBYTE_LENGTH];
10129
10130 /* Convert a single-byte string to multibyte. */
10131 for (i = 0; i < nbytes; i++)
10132 {
10133 c = msg[i];
10134 MAKE_CHAR_MULTIBYTE (c);
10135 n = CHAR_STRING (c, str);
10136 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10137 }
10138 }
10139 else
10140 insert_1 (s, nbytes, 1, 0, 0);
10141 }
10142
10143 return 0;
10144 }
10145
10146
10147 /* Clear messages. CURRENT_P non-zero means clear the current
10148 message. LAST_DISPLAYED_P non-zero means clear the message
10149 last displayed. */
10150
10151 void
10152 clear_message (int current_p, int last_displayed_p)
10153 {
10154 if (current_p)
10155 {
10156 echo_area_buffer[0] = Qnil;
10157 message_cleared_p = 1;
10158 }
10159
10160 if (last_displayed_p)
10161 echo_area_buffer[1] = Qnil;
10162
10163 message_buf_print = 0;
10164 }
10165
10166 /* Clear garbaged frames.
10167
10168 This function is used where the old redisplay called
10169 redraw_garbaged_frames which in turn called redraw_frame which in
10170 turn called clear_frame. The call to clear_frame was a source of
10171 flickering. I believe a clear_frame is not necessary. It should
10172 suffice in the new redisplay to invalidate all current matrices,
10173 and ensure a complete redisplay of all windows. */
10174
10175 static void
10176 clear_garbaged_frames (void)
10177 {
10178 if (frame_garbaged)
10179 {
10180 Lisp_Object tail, frame;
10181 int changed_count = 0;
10182
10183 FOR_EACH_FRAME (tail, frame)
10184 {
10185 struct frame *f = XFRAME (frame);
10186
10187 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10188 {
10189 if (f->resized_p)
10190 {
10191 Fredraw_frame (frame);
10192 f->force_flush_display_p = 1;
10193 }
10194 clear_current_matrices (f);
10195 changed_count++;
10196 f->garbaged = 0;
10197 f->resized_p = 0;
10198 }
10199 }
10200
10201 frame_garbaged = 0;
10202 if (changed_count)
10203 ++windows_or_buffers_changed;
10204 }
10205 }
10206
10207
10208 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10209 is non-zero update selected_frame. Value is non-zero if the
10210 mini-windows height has been changed. */
10211
10212 static int
10213 echo_area_display (int update_frame_p)
10214 {
10215 Lisp_Object mini_window;
10216 struct window *w;
10217 struct frame *f;
10218 int window_height_changed_p = 0;
10219 struct frame *sf = SELECTED_FRAME ();
10220
10221 mini_window = FRAME_MINIBUF_WINDOW (sf);
10222 w = XWINDOW (mini_window);
10223 f = XFRAME (WINDOW_FRAME (w));
10224
10225 /* Don't display if frame is invisible or not yet initialized. */
10226 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10227 return 0;
10228
10229 #ifdef HAVE_WINDOW_SYSTEM
10230 /* When Emacs starts, selected_frame may be the initial terminal
10231 frame. If we let this through, a message would be displayed on
10232 the terminal. */
10233 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10234 return 0;
10235 #endif /* HAVE_WINDOW_SYSTEM */
10236
10237 /* Redraw garbaged frames. */
10238 if (frame_garbaged)
10239 clear_garbaged_frames ();
10240
10241 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10242 {
10243 echo_area_window = mini_window;
10244 window_height_changed_p = display_echo_area (w);
10245 w->must_be_updated_p = 1;
10246
10247 /* Update the display, unless called from redisplay_internal.
10248 Also don't update the screen during redisplay itself. The
10249 update will happen at the end of redisplay, and an update
10250 here could cause confusion. */
10251 if (update_frame_p && !redisplaying_p)
10252 {
10253 int n = 0;
10254
10255 /* If the display update has been interrupted by pending
10256 input, update mode lines in the frame. Due to the
10257 pending input, it might have been that redisplay hasn't
10258 been called, so that mode lines above the echo area are
10259 garbaged. This looks odd, so we prevent it here. */
10260 if (!display_completed)
10261 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10262
10263 if (window_height_changed_p
10264 /* Don't do this if Emacs is shutting down. Redisplay
10265 needs to run hooks. */
10266 && !NILP (Vrun_hooks))
10267 {
10268 /* Must update other windows. Likewise as in other
10269 cases, don't let this update be interrupted by
10270 pending input. */
10271 int count = SPECPDL_INDEX ();
10272 specbind (Qredisplay_dont_pause, Qt);
10273 windows_or_buffers_changed = 1;
10274 redisplay_internal ();
10275 unbind_to (count, Qnil);
10276 }
10277 else if (FRAME_WINDOW_P (f) && n == 0)
10278 {
10279 /* Window configuration is the same as before.
10280 Can do with a display update of the echo area,
10281 unless we displayed some mode lines. */
10282 update_single_window (w, 1);
10283 FRAME_RIF (f)->flush_display (f);
10284 }
10285 else
10286 update_frame (f, 1, 1);
10287
10288 /* If cursor is in the echo area, make sure that the next
10289 redisplay displays the minibuffer, so that the cursor will
10290 be replaced with what the minibuffer wants. */
10291 if (cursor_in_echo_area)
10292 ++windows_or_buffers_changed;
10293 }
10294 }
10295 else if (!EQ (mini_window, selected_window))
10296 windows_or_buffers_changed++;
10297
10298 /* Last displayed message is now the current message. */
10299 echo_area_buffer[1] = echo_area_buffer[0];
10300 /* Inform read_char that we're not echoing. */
10301 echo_message_buffer = Qnil;
10302
10303 /* Prevent redisplay optimization in redisplay_internal by resetting
10304 this_line_start_pos. This is done because the mini-buffer now
10305 displays the message instead of its buffer text. */
10306 if (EQ (mini_window, selected_window))
10307 CHARPOS (this_line_start_pos) = 0;
10308
10309 return window_height_changed_p;
10310 }
10311
10312
10313 \f
10314 /***********************************************************************
10315 Mode Lines and Frame Titles
10316 ***********************************************************************/
10317
10318 /* A buffer for constructing non-propertized mode-line strings and
10319 frame titles in it; allocated from the heap in init_xdisp and
10320 resized as needed in store_mode_line_noprop_char. */
10321
10322 static char *mode_line_noprop_buf;
10323
10324 /* The buffer's end, and a current output position in it. */
10325
10326 static char *mode_line_noprop_buf_end;
10327 static char *mode_line_noprop_ptr;
10328
10329 #define MODE_LINE_NOPROP_LEN(start) \
10330 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10331
10332 static enum {
10333 MODE_LINE_DISPLAY = 0,
10334 MODE_LINE_TITLE,
10335 MODE_LINE_NOPROP,
10336 MODE_LINE_STRING
10337 } mode_line_target;
10338
10339 /* Alist that caches the results of :propertize.
10340 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10341 static Lisp_Object mode_line_proptrans_alist;
10342
10343 /* List of strings making up the mode-line. */
10344 static Lisp_Object mode_line_string_list;
10345
10346 /* Base face property when building propertized mode line string. */
10347 static Lisp_Object mode_line_string_face;
10348 static Lisp_Object mode_line_string_face_prop;
10349
10350
10351 /* Unwind data for mode line strings */
10352
10353 static Lisp_Object Vmode_line_unwind_vector;
10354
10355 static Lisp_Object
10356 format_mode_line_unwind_data (struct buffer *obuf,
10357 Lisp_Object owin,
10358 int save_proptrans)
10359 {
10360 Lisp_Object vector, tmp;
10361
10362 /* Reduce consing by keeping one vector in
10363 Vwith_echo_area_save_vector. */
10364 vector = Vmode_line_unwind_vector;
10365 Vmode_line_unwind_vector = Qnil;
10366
10367 if (NILP (vector))
10368 vector = Fmake_vector (make_number (8), Qnil);
10369
10370 ASET (vector, 0, make_number (mode_line_target));
10371 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10372 ASET (vector, 2, mode_line_string_list);
10373 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10374 ASET (vector, 4, mode_line_string_face);
10375 ASET (vector, 5, mode_line_string_face_prop);
10376
10377 if (obuf)
10378 XSETBUFFER (tmp, obuf);
10379 else
10380 tmp = Qnil;
10381 ASET (vector, 6, tmp);
10382 ASET (vector, 7, owin);
10383
10384 return vector;
10385 }
10386
10387 static Lisp_Object
10388 unwind_format_mode_line (Lisp_Object vector)
10389 {
10390 mode_line_target = XINT (AREF (vector, 0));
10391 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10392 mode_line_string_list = AREF (vector, 2);
10393 if (! EQ (AREF (vector, 3), Qt))
10394 mode_line_proptrans_alist = AREF (vector, 3);
10395 mode_line_string_face = AREF (vector, 4);
10396 mode_line_string_face_prop = AREF (vector, 5);
10397
10398 if (!NILP (AREF (vector, 7)))
10399 /* Select window before buffer, since it may change the buffer. */
10400 Fselect_window (AREF (vector, 7), Qt);
10401
10402 if (!NILP (AREF (vector, 6)))
10403 {
10404 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10405 ASET (vector, 6, Qnil);
10406 }
10407
10408 Vmode_line_unwind_vector = vector;
10409 return Qnil;
10410 }
10411
10412
10413 /* Store a single character C for the frame title in mode_line_noprop_buf.
10414 Re-allocate mode_line_noprop_buf if necessary. */
10415
10416 static void
10417 store_mode_line_noprop_char (char c)
10418 {
10419 /* If output position has reached the end of the allocated buffer,
10420 double the buffer's size. */
10421 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10422 {
10423 int len = MODE_LINE_NOPROP_LEN (0);
10424 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10425 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10426 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10427 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10428 }
10429
10430 *mode_line_noprop_ptr++ = c;
10431 }
10432
10433
10434 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10435 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10436 characters that yield more columns than PRECISION; PRECISION <= 0
10437 means copy the whole string. Pad with spaces until FIELD_WIDTH
10438 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10439 pad. Called from display_mode_element when it is used to build a
10440 frame title. */
10441
10442 static int
10443 store_mode_line_noprop (const char *string, int field_width, int precision)
10444 {
10445 const unsigned char *str = (const unsigned char *) string;
10446 int n = 0;
10447 EMACS_INT dummy, nbytes;
10448
10449 /* Copy at most PRECISION chars from STR. */
10450 nbytes = strlen (string);
10451 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10452 while (nbytes--)
10453 store_mode_line_noprop_char (*str++);
10454
10455 /* Fill up with spaces until FIELD_WIDTH reached. */
10456 while (field_width > 0
10457 && n < field_width)
10458 {
10459 store_mode_line_noprop_char (' ');
10460 ++n;
10461 }
10462
10463 return n;
10464 }
10465
10466 /***********************************************************************
10467 Frame Titles
10468 ***********************************************************************/
10469
10470 #ifdef HAVE_WINDOW_SYSTEM
10471
10472 /* Set the title of FRAME, if it has changed. The title format is
10473 Vicon_title_format if FRAME is iconified, otherwise it is
10474 frame_title_format. */
10475
10476 static void
10477 x_consider_frame_title (Lisp_Object frame)
10478 {
10479 struct frame *f = XFRAME (frame);
10480
10481 if (FRAME_WINDOW_P (f)
10482 || FRAME_MINIBUF_ONLY_P (f)
10483 || f->explicit_name)
10484 {
10485 /* Do we have more than one visible frame on this X display? */
10486 Lisp_Object tail;
10487 Lisp_Object fmt;
10488 int title_start;
10489 char *title;
10490 int len;
10491 struct it it;
10492 int count = SPECPDL_INDEX ();
10493
10494 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10495 {
10496 Lisp_Object other_frame = XCAR (tail);
10497 struct frame *tf = XFRAME (other_frame);
10498
10499 if (tf != f
10500 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10501 && !FRAME_MINIBUF_ONLY_P (tf)
10502 && !EQ (other_frame, tip_frame)
10503 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10504 break;
10505 }
10506
10507 /* Set global variable indicating that multiple frames exist. */
10508 multiple_frames = CONSP (tail);
10509
10510 /* Switch to the buffer of selected window of the frame. Set up
10511 mode_line_target so that display_mode_element will output into
10512 mode_line_noprop_buf; then display the title. */
10513 record_unwind_protect (unwind_format_mode_line,
10514 format_mode_line_unwind_data
10515 (current_buffer, selected_window, 0));
10516
10517 Fselect_window (f->selected_window, Qt);
10518 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10519 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10520
10521 mode_line_target = MODE_LINE_TITLE;
10522 title_start = MODE_LINE_NOPROP_LEN (0);
10523 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10524 NULL, DEFAULT_FACE_ID);
10525 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10526 len = MODE_LINE_NOPROP_LEN (title_start);
10527 title = mode_line_noprop_buf + title_start;
10528 unbind_to (count, Qnil);
10529
10530 /* Set the title only if it's changed. This avoids consing in
10531 the common case where it hasn't. (If it turns out that we've
10532 already wasted too much time by walking through the list with
10533 display_mode_element, then we might need to optimize at a
10534 higher level than this.) */
10535 if (! STRINGP (f->name)
10536 || SBYTES (f->name) != len
10537 || memcmp (title, SDATA (f->name), len) != 0)
10538 x_implicitly_set_name (f, make_string (title, len), Qnil);
10539 }
10540 }
10541
10542 #endif /* not HAVE_WINDOW_SYSTEM */
10543
10544
10545
10546 \f
10547 /***********************************************************************
10548 Menu Bars
10549 ***********************************************************************/
10550
10551
10552 /* Prepare for redisplay by updating menu-bar item lists when
10553 appropriate. This can call eval. */
10554
10555 void
10556 prepare_menu_bars (void)
10557 {
10558 int all_windows;
10559 struct gcpro gcpro1, gcpro2;
10560 struct frame *f;
10561 Lisp_Object tooltip_frame;
10562
10563 #ifdef HAVE_WINDOW_SYSTEM
10564 tooltip_frame = tip_frame;
10565 #else
10566 tooltip_frame = Qnil;
10567 #endif
10568
10569 /* Update all frame titles based on their buffer names, etc. We do
10570 this before the menu bars so that the buffer-menu will show the
10571 up-to-date frame titles. */
10572 #ifdef HAVE_WINDOW_SYSTEM
10573 if (windows_or_buffers_changed || update_mode_lines)
10574 {
10575 Lisp_Object tail, frame;
10576
10577 FOR_EACH_FRAME (tail, frame)
10578 {
10579 f = XFRAME (frame);
10580 if (!EQ (frame, tooltip_frame)
10581 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10582 x_consider_frame_title (frame);
10583 }
10584 }
10585 #endif /* HAVE_WINDOW_SYSTEM */
10586
10587 /* Update the menu bar item lists, if appropriate. This has to be
10588 done before any actual redisplay or generation of display lines. */
10589 all_windows = (update_mode_lines
10590 || buffer_shared > 1
10591 || windows_or_buffers_changed);
10592 if (all_windows)
10593 {
10594 Lisp_Object tail, frame;
10595 int count = SPECPDL_INDEX ();
10596 /* 1 means that update_menu_bar has run its hooks
10597 so any further calls to update_menu_bar shouldn't do so again. */
10598 int menu_bar_hooks_run = 0;
10599
10600 record_unwind_save_match_data ();
10601
10602 FOR_EACH_FRAME (tail, frame)
10603 {
10604 f = XFRAME (frame);
10605
10606 /* Ignore tooltip frame. */
10607 if (EQ (frame, tooltip_frame))
10608 continue;
10609
10610 /* If a window on this frame changed size, report that to
10611 the user and clear the size-change flag. */
10612 if (FRAME_WINDOW_SIZES_CHANGED (f))
10613 {
10614 Lisp_Object functions;
10615
10616 /* Clear flag first in case we get an error below. */
10617 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10618 functions = Vwindow_size_change_functions;
10619 GCPRO2 (tail, functions);
10620
10621 while (CONSP (functions))
10622 {
10623 if (!EQ (XCAR (functions), Qt))
10624 call1 (XCAR (functions), frame);
10625 functions = XCDR (functions);
10626 }
10627 UNGCPRO;
10628 }
10629
10630 GCPRO1 (tail);
10631 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10632 #ifdef HAVE_WINDOW_SYSTEM
10633 update_tool_bar (f, 0);
10634 #endif
10635 #ifdef HAVE_NS
10636 if (windows_or_buffers_changed
10637 && FRAME_NS_P (f))
10638 ns_set_doc_edited (f, Fbuffer_modified_p
10639 (XWINDOW (f->selected_window)->buffer));
10640 #endif
10641 UNGCPRO;
10642 }
10643
10644 unbind_to (count, Qnil);
10645 }
10646 else
10647 {
10648 struct frame *sf = SELECTED_FRAME ();
10649 update_menu_bar (sf, 1, 0);
10650 #ifdef HAVE_WINDOW_SYSTEM
10651 update_tool_bar (sf, 1);
10652 #endif
10653 }
10654 }
10655
10656
10657 /* Update the menu bar item list for frame F. This has to be done
10658 before we start to fill in any display lines, because it can call
10659 eval.
10660
10661 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10662
10663 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10664 already ran the menu bar hooks for this redisplay, so there
10665 is no need to run them again. The return value is the
10666 updated value of this flag, to pass to the next call. */
10667
10668 static int
10669 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10670 {
10671 Lisp_Object window;
10672 register struct window *w;
10673
10674 /* If called recursively during a menu update, do nothing. This can
10675 happen when, for instance, an activate-menubar-hook causes a
10676 redisplay. */
10677 if (inhibit_menubar_update)
10678 return hooks_run;
10679
10680 window = FRAME_SELECTED_WINDOW (f);
10681 w = XWINDOW (window);
10682
10683 if (FRAME_WINDOW_P (f)
10684 ?
10685 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10686 || defined (HAVE_NS) || defined (USE_GTK)
10687 FRAME_EXTERNAL_MENU_BAR (f)
10688 #else
10689 FRAME_MENU_BAR_LINES (f) > 0
10690 #endif
10691 : FRAME_MENU_BAR_LINES (f) > 0)
10692 {
10693 /* If the user has switched buffers or windows, we need to
10694 recompute to reflect the new bindings. But we'll
10695 recompute when update_mode_lines is set too; that means
10696 that people can use force-mode-line-update to request
10697 that the menu bar be recomputed. The adverse effect on
10698 the rest of the redisplay algorithm is about the same as
10699 windows_or_buffers_changed anyway. */
10700 if (windows_or_buffers_changed
10701 /* This used to test w->update_mode_line, but we believe
10702 there is no need to recompute the menu in that case. */
10703 || update_mode_lines
10704 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10705 < BUF_MODIFF (XBUFFER (w->buffer)))
10706 != !NILP (w->last_had_star))
10707 || ((!NILP (Vtransient_mark_mode)
10708 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10709 != !NILP (w->region_showing)))
10710 {
10711 struct buffer *prev = current_buffer;
10712 int count = SPECPDL_INDEX ();
10713
10714 specbind (Qinhibit_menubar_update, Qt);
10715
10716 set_buffer_internal_1 (XBUFFER (w->buffer));
10717 if (save_match_data)
10718 record_unwind_save_match_data ();
10719 if (NILP (Voverriding_local_map_menu_flag))
10720 {
10721 specbind (Qoverriding_terminal_local_map, Qnil);
10722 specbind (Qoverriding_local_map, Qnil);
10723 }
10724
10725 if (!hooks_run)
10726 {
10727 /* Run the Lucid hook. */
10728 safe_run_hooks (Qactivate_menubar_hook);
10729
10730 /* If it has changed current-menubar from previous value,
10731 really recompute the menu-bar from the value. */
10732 if (! NILP (Vlucid_menu_bar_dirty_flag))
10733 call0 (Qrecompute_lucid_menubar);
10734
10735 safe_run_hooks (Qmenu_bar_update_hook);
10736
10737 hooks_run = 1;
10738 }
10739
10740 XSETFRAME (Vmenu_updating_frame, f);
10741 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10742
10743 /* Redisplay the menu bar in case we changed it. */
10744 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10745 || defined (HAVE_NS) || defined (USE_GTK)
10746 if (FRAME_WINDOW_P (f))
10747 {
10748 #if defined (HAVE_NS)
10749 /* All frames on Mac OS share the same menubar. So only
10750 the selected frame should be allowed to set it. */
10751 if (f == SELECTED_FRAME ())
10752 #endif
10753 set_frame_menubar (f, 0, 0);
10754 }
10755 else
10756 /* On a terminal screen, the menu bar is an ordinary screen
10757 line, and this makes it get updated. */
10758 w->update_mode_line = Qt;
10759 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10760 /* In the non-toolkit version, the menu bar is an ordinary screen
10761 line, and this makes it get updated. */
10762 w->update_mode_line = Qt;
10763 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10764
10765 unbind_to (count, Qnil);
10766 set_buffer_internal_1 (prev);
10767 }
10768 }
10769
10770 return hooks_run;
10771 }
10772
10773
10774 \f
10775 /***********************************************************************
10776 Output Cursor
10777 ***********************************************************************/
10778
10779 #ifdef HAVE_WINDOW_SYSTEM
10780
10781 /* EXPORT:
10782 Nominal cursor position -- where to draw output.
10783 HPOS and VPOS are window relative glyph matrix coordinates.
10784 X and Y are window relative pixel coordinates. */
10785
10786 struct cursor_pos output_cursor;
10787
10788
10789 /* EXPORT:
10790 Set the global variable output_cursor to CURSOR. All cursor
10791 positions are relative to updated_window. */
10792
10793 void
10794 set_output_cursor (struct cursor_pos *cursor)
10795 {
10796 output_cursor.hpos = cursor->hpos;
10797 output_cursor.vpos = cursor->vpos;
10798 output_cursor.x = cursor->x;
10799 output_cursor.y = cursor->y;
10800 }
10801
10802
10803 /* EXPORT for RIF:
10804 Set a nominal cursor position.
10805
10806 HPOS and VPOS are column/row positions in a window glyph matrix. X
10807 and Y are window text area relative pixel positions.
10808
10809 If this is done during an update, updated_window will contain the
10810 window that is being updated and the position is the future output
10811 cursor position for that window. If updated_window is null, use
10812 selected_window and display the cursor at the given position. */
10813
10814 void
10815 x_cursor_to (int vpos, int hpos, int y, int x)
10816 {
10817 struct window *w;
10818
10819 /* If updated_window is not set, work on selected_window. */
10820 if (updated_window)
10821 w = updated_window;
10822 else
10823 w = XWINDOW (selected_window);
10824
10825 /* Set the output cursor. */
10826 output_cursor.hpos = hpos;
10827 output_cursor.vpos = vpos;
10828 output_cursor.x = x;
10829 output_cursor.y = y;
10830
10831 /* If not called as part of an update, really display the cursor.
10832 This will also set the cursor position of W. */
10833 if (updated_window == NULL)
10834 {
10835 BLOCK_INPUT;
10836 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10837 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10838 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10839 UNBLOCK_INPUT;
10840 }
10841 }
10842
10843 #endif /* HAVE_WINDOW_SYSTEM */
10844
10845 \f
10846 /***********************************************************************
10847 Tool-bars
10848 ***********************************************************************/
10849
10850 #ifdef HAVE_WINDOW_SYSTEM
10851
10852 /* Where the mouse was last time we reported a mouse event. */
10853
10854 FRAME_PTR last_mouse_frame;
10855
10856 /* Tool-bar item index of the item on which a mouse button was pressed
10857 or -1. */
10858
10859 int last_tool_bar_item;
10860
10861
10862 static Lisp_Object
10863 update_tool_bar_unwind (Lisp_Object frame)
10864 {
10865 selected_frame = frame;
10866 return Qnil;
10867 }
10868
10869 /* Update the tool-bar item list for frame F. This has to be done
10870 before we start to fill in any display lines. Called from
10871 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10872 and restore it here. */
10873
10874 static void
10875 update_tool_bar (struct frame *f, int save_match_data)
10876 {
10877 #if defined (USE_GTK) || defined (HAVE_NS)
10878 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10879 #else
10880 int do_update = WINDOWP (f->tool_bar_window)
10881 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10882 #endif
10883
10884 if (do_update)
10885 {
10886 Lisp_Object window;
10887 struct window *w;
10888
10889 window = FRAME_SELECTED_WINDOW (f);
10890 w = XWINDOW (window);
10891
10892 /* If the user has switched buffers or windows, we need to
10893 recompute to reflect the new bindings. But we'll
10894 recompute when update_mode_lines is set too; that means
10895 that people can use force-mode-line-update to request
10896 that the menu bar be recomputed. The adverse effect on
10897 the rest of the redisplay algorithm is about the same as
10898 windows_or_buffers_changed anyway. */
10899 if (windows_or_buffers_changed
10900 || !NILP (w->update_mode_line)
10901 || update_mode_lines
10902 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10903 < BUF_MODIFF (XBUFFER (w->buffer)))
10904 != !NILP (w->last_had_star))
10905 || ((!NILP (Vtransient_mark_mode)
10906 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10907 != !NILP (w->region_showing)))
10908 {
10909 struct buffer *prev = current_buffer;
10910 int count = SPECPDL_INDEX ();
10911 Lisp_Object frame, new_tool_bar;
10912 int new_n_tool_bar;
10913 struct gcpro gcpro1;
10914
10915 /* Set current_buffer to the buffer of the selected
10916 window of the frame, so that we get the right local
10917 keymaps. */
10918 set_buffer_internal_1 (XBUFFER (w->buffer));
10919
10920 /* Save match data, if we must. */
10921 if (save_match_data)
10922 record_unwind_save_match_data ();
10923
10924 /* Make sure that we don't accidentally use bogus keymaps. */
10925 if (NILP (Voverriding_local_map_menu_flag))
10926 {
10927 specbind (Qoverriding_terminal_local_map, Qnil);
10928 specbind (Qoverriding_local_map, Qnil);
10929 }
10930
10931 GCPRO1 (new_tool_bar);
10932
10933 /* We must temporarily set the selected frame to this frame
10934 before calling tool_bar_items, because the calculation of
10935 the tool-bar keymap uses the selected frame (see
10936 `tool-bar-make-keymap' in tool-bar.el). */
10937 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10938 XSETFRAME (frame, f);
10939 selected_frame = frame;
10940
10941 /* Build desired tool-bar items from keymaps. */
10942 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10943 &new_n_tool_bar);
10944
10945 /* Redisplay the tool-bar if we changed it. */
10946 if (new_n_tool_bar != f->n_tool_bar_items
10947 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10948 {
10949 /* Redisplay that happens asynchronously due to an expose event
10950 may access f->tool_bar_items. Make sure we update both
10951 variables within BLOCK_INPUT so no such event interrupts. */
10952 BLOCK_INPUT;
10953 f->tool_bar_items = new_tool_bar;
10954 f->n_tool_bar_items = new_n_tool_bar;
10955 w->update_mode_line = Qt;
10956 UNBLOCK_INPUT;
10957 }
10958
10959 UNGCPRO;
10960
10961 unbind_to (count, Qnil);
10962 set_buffer_internal_1 (prev);
10963 }
10964 }
10965 }
10966
10967
10968 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10969 F's desired tool-bar contents. F->tool_bar_items must have
10970 been set up previously by calling prepare_menu_bars. */
10971
10972 static void
10973 build_desired_tool_bar_string (struct frame *f)
10974 {
10975 int i, size, size_needed;
10976 struct gcpro gcpro1, gcpro2, gcpro3;
10977 Lisp_Object image, plist, props;
10978
10979 image = plist = props = Qnil;
10980 GCPRO3 (image, plist, props);
10981
10982 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10983 Otherwise, make a new string. */
10984
10985 /* The size of the string we might be able to reuse. */
10986 size = (STRINGP (f->desired_tool_bar_string)
10987 ? SCHARS (f->desired_tool_bar_string)
10988 : 0);
10989
10990 /* We need one space in the string for each image. */
10991 size_needed = f->n_tool_bar_items;
10992
10993 /* Reuse f->desired_tool_bar_string, if possible. */
10994 if (size < size_needed || NILP (f->desired_tool_bar_string))
10995 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10996 make_number (' '));
10997 else
10998 {
10999 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11000 Fremove_text_properties (make_number (0), make_number (size),
11001 props, f->desired_tool_bar_string);
11002 }
11003
11004 /* Put a `display' property on the string for the images to display,
11005 put a `menu_item' property on tool-bar items with a value that
11006 is the index of the item in F's tool-bar item vector. */
11007 for (i = 0; i < f->n_tool_bar_items; ++i)
11008 {
11009 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11010
11011 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11012 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11013 int hmargin, vmargin, relief, idx, end;
11014
11015 /* If image is a vector, choose the image according to the
11016 button state. */
11017 image = PROP (TOOL_BAR_ITEM_IMAGES);
11018 if (VECTORP (image))
11019 {
11020 if (enabled_p)
11021 idx = (selected_p
11022 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11023 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11024 else
11025 idx = (selected_p
11026 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11027 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11028
11029 xassert (ASIZE (image) >= idx);
11030 image = AREF (image, idx);
11031 }
11032 else
11033 idx = -1;
11034
11035 /* Ignore invalid image specifications. */
11036 if (!valid_image_p (image))
11037 continue;
11038
11039 /* Display the tool-bar button pressed, or depressed. */
11040 plist = Fcopy_sequence (XCDR (image));
11041
11042 /* Compute margin and relief to draw. */
11043 relief = (tool_bar_button_relief >= 0
11044 ? tool_bar_button_relief
11045 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11046 hmargin = vmargin = relief;
11047
11048 if (INTEGERP (Vtool_bar_button_margin)
11049 && XINT (Vtool_bar_button_margin) > 0)
11050 {
11051 hmargin += XFASTINT (Vtool_bar_button_margin);
11052 vmargin += XFASTINT (Vtool_bar_button_margin);
11053 }
11054 else if (CONSP (Vtool_bar_button_margin))
11055 {
11056 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11057 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11058 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11059
11060 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11061 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11062 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11063 }
11064
11065 if (auto_raise_tool_bar_buttons_p)
11066 {
11067 /* Add a `:relief' property to the image spec if the item is
11068 selected. */
11069 if (selected_p)
11070 {
11071 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11072 hmargin -= relief;
11073 vmargin -= relief;
11074 }
11075 }
11076 else
11077 {
11078 /* If image is selected, display it pressed, i.e. with a
11079 negative relief. If it's not selected, display it with a
11080 raised relief. */
11081 plist = Fplist_put (plist, QCrelief,
11082 (selected_p
11083 ? make_number (-relief)
11084 : make_number (relief)));
11085 hmargin -= relief;
11086 vmargin -= relief;
11087 }
11088
11089 /* Put a margin around the image. */
11090 if (hmargin || vmargin)
11091 {
11092 if (hmargin == vmargin)
11093 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11094 else
11095 plist = Fplist_put (plist, QCmargin,
11096 Fcons (make_number (hmargin),
11097 make_number (vmargin)));
11098 }
11099
11100 /* If button is not enabled, and we don't have special images
11101 for the disabled state, make the image appear disabled by
11102 applying an appropriate algorithm to it. */
11103 if (!enabled_p && idx < 0)
11104 plist = Fplist_put (plist, QCconversion, Qdisabled);
11105
11106 /* Put a `display' text property on the string for the image to
11107 display. Put a `menu-item' property on the string that gives
11108 the start of this item's properties in the tool-bar items
11109 vector. */
11110 image = Fcons (Qimage, plist);
11111 props = list4 (Qdisplay, image,
11112 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11113
11114 /* Let the last image hide all remaining spaces in the tool bar
11115 string. The string can be longer than needed when we reuse a
11116 previous string. */
11117 if (i + 1 == f->n_tool_bar_items)
11118 end = SCHARS (f->desired_tool_bar_string);
11119 else
11120 end = i + 1;
11121 Fadd_text_properties (make_number (i), make_number (end),
11122 props, f->desired_tool_bar_string);
11123 #undef PROP
11124 }
11125
11126 UNGCPRO;
11127 }
11128
11129
11130 /* Display one line of the tool-bar of frame IT->f.
11131
11132 HEIGHT specifies the desired height of the tool-bar line.
11133 If the actual height of the glyph row is less than HEIGHT, the
11134 row's height is increased to HEIGHT, and the icons are centered
11135 vertically in the new height.
11136
11137 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11138 count a final empty row in case the tool-bar width exactly matches
11139 the window width.
11140 */
11141
11142 static void
11143 display_tool_bar_line (struct it *it, int height)
11144 {
11145 struct glyph_row *row = it->glyph_row;
11146 int max_x = it->last_visible_x;
11147 struct glyph *last;
11148
11149 prepare_desired_row (row);
11150 row->y = it->current_y;
11151
11152 /* Note that this isn't made use of if the face hasn't a box,
11153 so there's no need to check the face here. */
11154 it->start_of_box_run_p = 1;
11155
11156 while (it->current_x < max_x)
11157 {
11158 int x, n_glyphs_before, i, nglyphs;
11159 struct it it_before;
11160
11161 /* Get the next display element. */
11162 if (!get_next_display_element (it))
11163 {
11164 /* Don't count empty row if we are counting needed tool-bar lines. */
11165 if (height < 0 && !it->hpos)
11166 return;
11167 break;
11168 }
11169
11170 /* Produce glyphs. */
11171 n_glyphs_before = row->used[TEXT_AREA];
11172 it_before = *it;
11173
11174 PRODUCE_GLYPHS (it);
11175
11176 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11177 i = 0;
11178 x = it_before.current_x;
11179 while (i < nglyphs)
11180 {
11181 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11182
11183 if (x + glyph->pixel_width > max_x)
11184 {
11185 /* Glyph doesn't fit on line. Backtrack. */
11186 row->used[TEXT_AREA] = n_glyphs_before;
11187 *it = it_before;
11188 /* If this is the only glyph on this line, it will never fit on the
11189 tool-bar, so skip it. But ensure there is at least one glyph,
11190 so we don't accidentally disable the tool-bar. */
11191 if (n_glyphs_before == 0
11192 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11193 break;
11194 goto out;
11195 }
11196
11197 ++it->hpos;
11198 x += glyph->pixel_width;
11199 ++i;
11200 }
11201
11202 /* Stop at line end. */
11203 if (ITERATOR_AT_END_OF_LINE_P (it))
11204 break;
11205
11206 set_iterator_to_next (it, 1);
11207 }
11208
11209 out:;
11210
11211 row->displays_text_p = row->used[TEXT_AREA] != 0;
11212
11213 /* Use default face for the border below the tool bar.
11214
11215 FIXME: When auto-resize-tool-bars is grow-only, there is
11216 no additional border below the possibly empty tool-bar lines.
11217 So to make the extra empty lines look "normal", we have to
11218 use the tool-bar face for the border too. */
11219 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11220 it->face_id = DEFAULT_FACE_ID;
11221
11222 extend_face_to_end_of_line (it);
11223 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11224 last->right_box_line_p = 1;
11225 if (last == row->glyphs[TEXT_AREA])
11226 last->left_box_line_p = 1;
11227
11228 /* Make line the desired height and center it vertically. */
11229 if ((height -= it->max_ascent + it->max_descent) > 0)
11230 {
11231 /* Don't add more than one line height. */
11232 height %= FRAME_LINE_HEIGHT (it->f);
11233 it->max_ascent += height / 2;
11234 it->max_descent += (height + 1) / 2;
11235 }
11236
11237 compute_line_metrics (it);
11238
11239 /* If line is empty, make it occupy the rest of the tool-bar. */
11240 if (!row->displays_text_p)
11241 {
11242 row->height = row->phys_height = it->last_visible_y - row->y;
11243 row->visible_height = row->height;
11244 row->ascent = row->phys_ascent = 0;
11245 row->extra_line_spacing = 0;
11246 }
11247
11248 row->full_width_p = 1;
11249 row->continued_p = 0;
11250 row->truncated_on_left_p = 0;
11251 row->truncated_on_right_p = 0;
11252
11253 it->current_x = it->hpos = 0;
11254 it->current_y += row->height;
11255 ++it->vpos;
11256 ++it->glyph_row;
11257 }
11258
11259
11260 /* Max tool-bar height. */
11261
11262 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11263 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11264
11265 /* Value is the number of screen lines needed to make all tool-bar
11266 items of frame F visible. The number of actual rows needed is
11267 returned in *N_ROWS if non-NULL. */
11268
11269 static int
11270 tool_bar_lines_needed (struct frame *f, int *n_rows)
11271 {
11272 struct window *w = XWINDOW (f->tool_bar_window);
11273 struct it it;
11274 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11275 the desired matrix, so use (unused) mode-line row as temporary row to
11276 avoid destroying the first tool-bar row. */
11277 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11278
11279 /* Initialize an iterator for iteration over
11280 F->desired_tool_bar_string in the tool-bar window of frame F. */
11281 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11282 it.first_visible_x = 0;
11283 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11284 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11285 it.paragraph_embedding = L2R;
11286
11287 while (!ITERATOR_AT_END_P (&it))
11288 {
11289 clear_glyph_row (temp_row);
11290 it.glyph_row = temp_row;
11291 display_tool_bar_line (&it, -1);
11292 }
11293 clear_glyph_row (temp_row);
11294
11295 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11296 if (n_rows)
11297 *n_rows = it.vpos > 0 ? it.vpos : -1;
11298
11299 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11300 }
11301
11302
11303 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11304 0, 1, 0,
11305 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11306 (Lisp_Object frame)
11307 {
11308 struct frame *f;
11309 struct window *w;
11310 int nlines = 0;
11311
11312 if (NILP (frame))
11313 frame = selected_frame;
11314 else
11315 CHECK_FRAME (frame);
11316 f = XFRAME (frame);
11317
11318 if (WINDOWP (f->tool_bar_window)
11319 && (w = XWINDOW (f->tool_bar_window),
11320 WINDOW_TOTAL_LINES (w) > 0))
11321 {
11322 update_tool_bar (f, 1);
11323 if (f->n_tool_bar_items)
11324 {
11325 build_desired_tool_bar_string (f);
11326 nlines = tool_bar_lines_needed (f, NULL);
11327 }
11328 }
11329
11330 return make_number (nlines);
11331 }
11332
11333
11334 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11335 height should be changed. */
11336
11337 static int
11338 redisplay_tool_bar (struct frame *f)
11339 {
11340 struct window *w;
11341 struct it it;
11342 struct glyph_row *row;
11343
11344 #if defined (USE_GTK) || defined (HAVE_NS)
11345 if (FRAME_EXTERNAL_TOOL_BAR (f))
11346 update_frame_tool_bar (f);
11347 return 0;
11348 #endif
11349
11350 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11351 do anything. This means you must start with tool-bar-lines
11352 non-zero to get the auto-sizing effect. Or in other words, you
11353 can turn off tool-bars by specifying tool-bar-lines zero. */
11354 if (!WINDOWP (f->tool_bar_window)
11355 || (w = XWINDOW (f->tool_bar_window),
11356 WINDOW_TOTAL_LINES (w) == 0))
11357 return 0;
11358
11359 /* Set up an iterator for the tool-bar window. */
11360 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11361 it.first_visible_x = 0;
11362 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11363 row = it.glyph_row;
11364
11365 /* Build a string that represents the contents of the tool-bar. */
11366 build_desired_tool_bar_string (f);
11367 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11368 /* FIXME: This should be controlled by a user option. But it
11369 doesn't make sense to have an R2L tool bar if the menu bar cannot
11370 be drawn also R2L, and making the menu bar R2L is tricky due
11371 toolkit-specific code that implements it. If an R2L tool bar is
11372 ever supported, display_tool_bar_line should also be augmented to
11373 call unproduce_glyphs like display_line and display_string
11374 do. */
11375 it.paragraph_embedding = L2R;
11376
11377 if (f->n_tool_bar_rows == 0)
11378 {
11379 int nlines;
11380
11381 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11382 nlines != WINDOW_TOTAL_LINES (w)))
11383 {
11384 Lisp_Object frame;
11385 int old_height = WINDOW_TOTAL_LINES (w);
11386
11387 XSETFRAME (frame, f);
11388 Fmodify_frame_parameters (frame,
11389 Fcons (Fcons (Qtool_bar_lines,
11390 make_number (nlines)),
11391 Qnil));
11392 if (WINDOW_TOTAL_LINES (w) != old_height)
11393 {
11394 clear_glyph_matrix (w->desired_matrix);
11395 fonts_changed_p = 1;
11396 return 1;
11397 }
11398 }
11399 }
11400
11401 /* Display as many lines as needed to display all tool-bar items. */
11402
11403 if (f->n_tool_bar_rows > 0)
11404 {
11405 int border, rows, height, extra;
11406
11407 if (INTEGERP (Vtool_bar_border))
11408 border = XINT (Vtool_bar_border);
11409 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11410 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11411 else if (EQ (Vtool_bar_border, Qborder_width))
11412 border = f->border_width;
11413 else
11414 border = 0;
11415 if (border < 0)
11416 border = 0;
11417
11418 rows = f->n_tool_bar_rows;
11419 height = max (1, (it.last_visible_y - border) / rows);
11420 extra = it.last_visible_y - border - height * rows;
11421
11422 while (it.current_y < it.last_visible_y)
11423 {
11424 int h = 0;
11425 if (extra > 0 && rows-- > 0)
11426 {
11427 h = (extra + rows - 1) / rows;
11428 extra -= h;
11429 }
11430 display_tool_bar_line (&it, height + h);
11431 }
11432 }
11433 else
11434 {
11435 while (it.current_y < it.last_visible_y)
11436 display_tool_bar_line (&it, 0);
11437 }
11438
11439 /* It doesn't make much sense to try scrolling in the tool-bar
11440 window, so don't do it. */
11441 w->desired_matrix->no_scrolling_p = 1;
11442 w->must_be_updated_p = 1;
11443
11444 if (!NILP (Vauto_resize_tool_bars))
11445 {
11446 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11447 int change_height_p = 0;
11448
11449 /* If we couldn't display everything, change the tool-bar's
11450 height if there is room for more. */
11451 if (IT_STRING_CHARPOS (it) < it.end_charpos
11452 && it.current_y < max_tool_bar_height)
11453 change_height_p = 1;
11454
11455 row = it.glyph_row - 1;
11456
11457 /* If there are blank lines at the end, except for a partially
11458 visible blank line at the end that is smaller than
11459 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11460 if (!row->displays_text_p
11461 && row->height >= FRAME_LINE_HEIGHT (f))
11462 change_height_p = 1;
11463
11464 /* If row displays tool-bar items, but is partially visible,
11465 change the tool-bar's height. */
11466 if (row->displays_text_p
11467 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11468 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11469 change_height_p = 1;
11470
11471 /* Resize windows as needed by changing the `tool-bar-lines'
11472 frame parameter. */
11473 if (change_height_p)
11474 {
11475 Lisp_Object frame;
11476 int old_height = WINDOW_TOTAL_LINES (w);
11477 int nrows;
11478 int nlines = tool_bar_lines_needed (f, &nrows);
11479
11480 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11481 && !f->minimize_tool_bar_window_p)
11482 ? (nlines > old_height)
11483 : (nlines != old_height));
11484 f->minimize_tool_bar_window_p = 0;
11485
11486 if (change_height_p)
11487 {
11488 XSETFRAME (frame, f);
11489 Fmodify_frame_parameters (frame,
11490 Fcons (Fcons (Qtool_bar_lines,
11491 make_number (nlines)),
11492 Qnil));
11493 if (WINDOW_TOTAL_LINES (w) != old_height)
11494 {
11495 clear_glyph_matrix (w->desired_matrix);
11496 f->n_tool_bar_rows = nrows;
11497 fonts_changed_p = 1;
11498 return 1;
11499 }
11500 }
11501 }
11502 }
11503
11504 f->minimize_tool_bar_window_p = 0;
11505 return 0;
11506 }
11507
11508
11509 /* Get information about the tool-bar item which is displayed in GLYPH
11510 on frame F. Return in *PROP_IDX the index where tool-bar item
11511 properties start in F->tool_bar_items. Value is zero if
11512 GLYPH doesn't display a tool-bar item. */
11513
11514 static int
11515 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11516 {
11517 Lisp_Object prop;
11518 int success_p;
11519 int charpos;
11520
11521 /* This function can be called asynchronously, which means we must
11522 exclude any possibility that Fget_text_property signals an
11523 error. */
11524 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11525 charpos = max (0, charpos);
11526
11527 /* Get the text property `menu-item' at pos. The value of that
11528 property is the start index of this item's properties in
11529 F->tool_bar_items. */
11530 prop = Fget_text_property (make_number (charpos),
11531 Qmenu_item, f->current_tool_bar_string);
11532 if (INTEGERP (prop))
11533 {
11534 *prop_idx = XINT (prop);
11535 success_p = 1;
11536 }
11537 else
11538 success_p = 0;
11539
11540 return success_p;
11541 }
11542
11543 \f
11544 /* Get information about the tool-bar item at position X/Y on frame F.
11545 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11546 the current matrix of the tool-bar window of F, or NULL if not
11547 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11548 item in F->tool_bar_items. Value is
11549
11550 -1 if X/Y is not on a tool-bar item
11551 0 if X/Y is on the same item that was highlighted before.
11552 1 otherwise. */
11553
11554 static int
11555 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11556 int *hpos, int *vpos, int *prop_idx)
11557 {
11558 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11559 struct window *w = XWINDOW (f->tool_bar_window);
11560 int area;
11561
11562 /* Find the glyph under X/Y. */
11563 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11564 if (*glyph == NULL)
11565 return -1;
11566
11567 /* Get the start of this tool-bar item's properties in
11568 f->tool_bar_items. */
11569 if (!tool_bar_item_info (f, *glyph, prop_idx))
11570 return -1;
11571
11572 /* Is mouse on the highlighted item? */
11573 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11574 && *vpos >= hlinfo->mouse_face_beg_row
11575 && *vpos <= hlinfo->mouse_face_end_row
11576 && (*vpos > hlinfo->mouse_face_beg_row
11577 || *hpos >= hlinfo->mouse_face_beg_col)
11578 && (*vpos < hlinfo->mouse_face_end_row
11579 || *hpos < hlinfo->mouse_face_end_col
11580 || hlinfo->mouse_face_past_end))
11581 return 0;
11582
11583 return 1;
11584 }
11585
11586
11587 /* EXPORT:
11588 Handle mouse button event on the tool-bar of frame F, at
11589 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11590 0 for button release. MODIFIERS is event modifiers for button
11591 release. */
11592
11593 void
11594 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11595 unsigned int modifiers)
11596 {
11597 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11598 struct window *w = XWINDOW (f->tool_bar_window);
11599 int hpos, vpos, prop_idx;
11600 struct glyph *glyph;
11601 Lisp_Object enabled_p;
11602
11603 /* If not on the highlighted tool-bar item, return. */
11604 frame_to_window_pixel_xy (w, &x, &y);
11605 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11606 return;
11607
11608 /* If item is disabled, do nothing. */
11609 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11610 if (NILP (enabled_p))
11611 return;
11612
11613 if (down_p)
11614 {
11615 /* Show item in pressed state. */
11616 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11617 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11618 last_tool_bar_item = prop_idx;
11619 }
11620 else
11621 {
11622 Lisp_Object key, frame;
11623 struct input_event event;
11624 EVENT_INIT (event);
11625
11626 /* Show item in released state. */
11627 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11628 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11629
11630 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11631
11632 XSETFRAME (frame, f);
11633 event.kind = TOOL_BAR_EVENT;
11634 event.frame_or_window = frame;
11635 event.arg = frame;
11636 kbd_buffer_store_event (&event);
11637
11638 event.kind = TOOL_BAR_EVENT;
11639 event.frame_or_window = frame;
11640 event.arg = key;
11641 event.modifiers = modifiers;
11642 kbd_buffer_store_event (&event);
11643 last_tool_bar_item = -1;
11644 }
11645 }
11646
11647
11648 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11649 tool-bar window-relative coordinates X/Y. Called from
11650 note_mouse_highlight. */
11651
11652 static void
11653 note_tool_bar_highlight (struct frame *f, int x, int y)
11654 {
11655 Lisp_Object window = f->tool_bar_window;
11656 struct window *w = XWINDOW (window);
11657 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11658 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11659 int hpos, vpos;
11660 struct glyph *glyph;
11661 struct glyph_row *row;
11662 int i;
11663 Lisp_Object enabled_p;
11664 int prop_idx;
11665 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11666 int mouse_down_p, rc;
11667
11668 /* Function note_mouse_highlight is called with negative X/Y
11669 values when mouse moves outside of the frame. */
11670 if (x <= 0 || y <= 0)
11671 {
11672 clear_mouse_face (hlinfo);
11673 return;
11674 }
11675
11676 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11677 if (rc < 0)
11678 {
11679 /* Not on tool-bar item. */
11680 clear_mouse_face (hlinfo);
11681 return;
11682 }
11683 else if (rc == 0)
11684 /* On same tool-bar item as before. */
11685 goto set_help_echo;
11686
11687 clear_mouse_face (hlinfo);
11688
11689 /* Mouse is down, but on different tool-bar item? */
11690 mouse_down_p = (dpyinfo->grabbed
11691 && f == last_mouse_frame
11692 && FRAME_LIVE_P (f));
11693 if (mouse_down_p
11694 && last_tool_bar_item != prop_idx)
11695 return;
11696
11697 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11698 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11699
11700 /* If tool-bar item is not enabled, don't highlight it. */
11701 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11702 if (!NILP (enabled_p))
11703 {
11704 /* Compute the x-position of the glyph. In front and past the
11705 image is a space. We include this in the highlighted area. */
11706 row = MATRIX_ROW (w->current_matrix, vpos);
11707 for (i = x = 0; i < hpos; ++i)
11708 x += row->glyphs[TEXT_AREA][i].pixel_width;
11709
11710 /* Record this as the current active region. */
11711 hlinfo->mouse_face_beg_col = hpos;
11712 hlinfo->mouse_face_beg_row = vpos;
11713 hlinfo->mouse_face_beg_x = x;
11714 hlinfo->mouse_face_beg_y = row->y;
11715 hlinfo->mouse_face_past_end = 0;
11716
11717 hlinfo->mouse_face_end_col = hpos + 1;
11718 hlinfo->mouse_face_end_row = vpos;
11719 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11720 hlinfo->mouse_face_end_y = row->y;
11721 hlinfo->mouse_face_window = window;
11722 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11723
11724 /* Display it as active. */
11725 show_mouse_face (hlinfo, draw);
11726 hlinfo->mouse_face_image_state = draw;
11727 }
11728
11729 set_help_echo:
11730
11731 /* Set help_echo_string to a help string to display for this tool-bar item.
11732 XTread_socket does the rest. */
11733 help_echo_object = help_echo_window = Qnil;
11734 help_echo_pos = -1;
11735 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11736 if (NILP (help_echo_string))
11737 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11738 }
11739
11740 #endif /* HAVE_WINDOW_SYSTEM */
11741
11742
11743 \f
11744 /************************************************************************
11745 Horizontal scrolling
11746 ************************************************************************/
11747
11748 static int hscroll_window_tree (Lisp_Object);
11749 static int hscroll_windows (Lisp_Object);
11750
11751 /* For all leaf windows in the window tree rooted at WINDOW, set their
11752 hscroll value so that PT is (i) visible in the window, and (ii) so
11753 that it is not within a certain margin at the window's left and
11754 right border. Value is non-zero if any window's hscroll has been
11755 changed. */
11756
11757 static int
11758 hscroll_window_tree (Lisp_Object window)
11759 {
11760 int hscrolled_p = 0;
11761 int hscroll_relative_p = FLOATP (Vhscroll_step);
11762 int hscroll_step_abs = 0;
11763 double hscroll_step_rel = 0;
11764
11765 if (hscroll_relative_p)
11766 {
11767 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11768 if (hscroll_step_rel < 0)
11769 {
11770 hscroll_relative_p = 0;
11771 hscroll_step_abs = 0;
11772 }
11773 }
11774 else if (INTEGERP (Vhscroll_step))
11775 {
11776 hscroll_step_abs = XINT (Vhscroll_step);
11777 if (hscroll_step_abs < 0)
11778 hscroll_step_abs = 0;
11779 }
11780 else
11781 hscroll_step_abs = 0;
11782
11783 while (WINDOWP (window))
11784 {
11785 struct window *w = XWINDOW (window);
11786
11787 if (WINDOWP (w->hchild))
11788 hscrolled_p |= hscroll_window_tree (w->hchild);
11789 else if (WINDOWP (w->vchild))
11790 hscrolled_p |= hscroll_window_tree (w->vchild);
11791 else if (w->cursor.vpos >= 0)
11792 {
11793 int h_margin;
11794 int text_area_width;
11795 struct glyph_row *current_cursor_row
11796 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11797 struct glyph_row *desired_cursor_row
11798 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11799 struct glyph_row *cursor_row
11800 = (desired_cursor_row->enabled_p
11801 ? desired_cursor_row
11802 : current_cursor_row);
11803
11804 text_area_width = window_box_width (w, TEXT_AREA);
11805
11806 /* Scroll when cursor is inside this scroll margin. */
11807 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11808
11809 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11810 && ((XFASTINT (w->hscroll)
11811 && w->cursor.x <= h_margin)
11812 || (cursor_row->enabled_p
11813 && cursor_row->truncated_on_right_p
11814 && (w->cursor.x >= text_area_width - h_margin))))
11815 {
11816 struct it it;
11817 int hscroll;
11818 struct buffer *saved_current_buffer;
11819 EMACS_INT pt;
11820 int wanted_x;
11821
11822 /* Find point in a display of infinite width. */
11823 saved_current_buffer = current_buffer;
11824 current_buffer = XBUFFER (w->buffer);
11825
11826 if (w == XWINDOW (selected_window))
11827 pt = PT;
11828 else
11829 {
11830 pt = marker_position (w->pointm);
11831 pt = max (BEGV, pt);
11832 pt = min (ZV, pt);
11833 }
11834
11835 /* Move iterator to pt starting at cursor_row->start in
11836 a line with infinite width. */
11837 init_to_row_start (&it, w, cursor_row);
11838 it.last_visible_x = INFINITY;
11839 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11840 current_buffer = saved_current_buffer;
11841
11842 /* Position cursor in window. */
11843 if (!hscroll_relative_p && hscroll_step_abs == 0)
11844 hscroll = max (0, (it.current_x
11845 - (ITERATOR_AT_END_OF_LINE_P (&it)
11846 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11847 : (text_area_width / 2))))
11848 / FRAME_COLUMN_WIDTH (it.f);
11849 else if (w->cursor.x >= text_area_width - h_margin)
11850 {
11851 if (hscroll_relative_p)
11852 wanted_x = text_area_width * (1 - hscroll_step_rel)
11853 - h_margin;
11854 else
11855 wanted_x = text_area_width
11856 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11857 - h_margin;
11858 hscroll
11859 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11860 }
11861 else
11862 {
11863 if (hscroll_relative_p)
11864 wanted_x = text_area_width * hscroll_step_rel
11865 + h_margin;
11866 else
11867 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11868 + h_margin;
11869 hscroll
11870 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11871 }
11872 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11873
11874 /* Don't call Fset_window_hscroll if value hasn't
11875 changed because it will prevent redisplay
11876 optimizations. */
11877 if (XFASTINT (w->hscroll) != hscroll)
11878 {
11879 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11880 w->hscroll = make_number (hscroll);
11881 hscrolled_p = 1;
11882 }
11883 }
11884 }
11885
11886 window = w->next;
11887 }
11888
11889 /* Value is non-zero if hscroll of any leaf window has been changed. */
11890 return hscrolled_p;
11891 }
11892
11893
11894 /* Set hscroll so that cursor is visible and not inside horizontal
11895 scroll margins for all windows in the tree rooted at WINDOW. See
11896 also hscroll_window_tree above. Value is non-zero if any window's
11897 hscroll has been changed. If it has, desired matrices on the frame
11898 of WINDOW are cleared. */
11899
11900 static int
11901 hscroll_windows (Lisp_Object window)
11902 {
11903 int hscrolled_p = hscroll_window_tree (window);
11904 if (hscrolled_p)
11905 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11906 return hscrolled_p;
11907 }
11908
11909
11910 \f
11911 /************************************************************************
11912 Redisplay
11913 ************************************************************************/
11914
11915 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11916 to a non-zero value. This is sometimes handy to have in a debugger
11917 session. */
11918
11919 #if GLYPH_DEBUG
11920
11921 /* First and last unchanged row for try_window_id. */
11922
11923 static int debug_first_unchanged_at_end_vpos;
11924 static int debug_last_unchanged_at_beg_vpos;
11925
11926 /* Delta vpos and y. */
11927
11928 static int debug_dvpos, debug_dy;
11929
11930 /* Delta in characters and bytes for try_window_id. */
11931
11932 static EMACS_INT debug_delta, debug_delta_bytes;
11933
11934 /* Values of window_end_pos and window_end_vpos at the end of
11935 try_window_id. */
11936
11937 static EMACS_INT debug_end_vpos;
11938
11939 /* Append a string to W->desired_matrix->method. FMT is a printf
11940 format string. If trace_redisplay_p is non-zero also printf the
11941 resulting string to stderr. */
11942
11943 static void debug_method_add (struct window *, char const *, ...)
11944 ATTRIBUTE_FORMAT_PRINTF (2, 3);
11945
11946 static void
11947 debug_method_add (struct window *w, char const *fmt, ...)
11948 {
11949 char buffer[512];
11950 char *method = w->desired_matrix->method;
11951 int len = strlen (method);
11952 int size = sizeof w->desired_matrix->method;
11953 int remaining = size - len - 1;
11954 va_list ap;
11955
11956 va_start (ap, fmt);
11957 vsprintf (buffer, fmt, ap);
11958 va_end (ap);
11959 if (len && remaining)
11960 {
11961 method[len] = '|';
11962 --remaining, ++len;
11963 }
11964
11965 strncpy (method + len, buffer, remaining);
11966
11967 if (trace_redisplay_p)
11968 fprintf (stderr, "%p (%s): %s\n",
11969 w,
11970 ((BUFFERP (w->buffer)
11971 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
11972 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
11973 : "no buffer"),
11974 buffer);
11975 }
11976
11977 #endif /* GLYPH_DEBUG */
11978
11979
11980 /* Value is non-zero if all changes in window W, which displays
11981 current_buffer, are in the text between START and END. START is a
11982 buffer position, END is given as a distance from Z. Used in
11983 redisplay_internal for display optimization. */
11984
11985 static inline int
11986 text_outside_line_unchanged_p (struct window *w,
11987 EMACS_INT start, EMACS_INT end)
11988 {
11989 int unchanged_p = 1;
11990
11991 /* If text or overlays have changed, see where. */
11992 if (XFASTINT (w->last_modified) < MODIFF
11993 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11994 {
11995 /* Gap in the line? */
11996 if (GPT < start || Z - GPT < end)
11997 unchanged_p = 0;
11998
11999 /* Changes start in front of the line, or end after it? */
12000 if (unchanged_p
12001 && (BEG_UNCHANGED < start - 1
12002 || END_UNCHANGED < end))
12003 unchanged_p = 0;
12004
12005 /* If selective display, can't optimize if changes start at the
12006 beginning of the line. */
12007 if (unchanged_p
12008 && INTEGERP (BVAR (current_buffer, selective_display))
12009 && XINT (BVAR (current_buffer, selective_display)) > 0
12010 && (BEG_UNCHANGED < start || GPT <= start))
12011 unchanged_p = 0;
12012
12013 /* If there are overlays at the start or end of the line, these
12014 may have overlay strings with newlines in them. A change at
12015 START, for instance, may actually concern the display of such
12016 overlay strings as well, and they are displayed on different
12017 lines. So, quickly rule out this case. (For the future, it
12018 might be desirable to implement something more telling than
12019 just BEG/END_UNCHANGED.) */
12020 if (unchanged_p)
12021 {
12022 if (BEG + BEG_UNCHANGED == start
12023 && overlay_touches_p (start))
12024 unchanged_p = 0;
12025 if (END_UNCHANGED == end
12026 && overlay_touches_p (Z - end))
12027 unchanged_p = 0;
12028 }
12029
12030 /* Under bidi reordering, adding or deleting a character in the
12031 beginning of a paragraph, before the first strong directional
12032 character, can change the base direction of the paragraph (unless
12033 the buffer specifies a fixed paragraph direction), which will
12034 require to redisplay the whole paragraph. It might be worthwhile
12035 to find the paragraph limits and widen the range of redisplayed
12036 lines to that, but for now just give up this optimization. */
12037 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12038 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12039 unchanged_p = 0;
12040 }
12041
12042 return unchanged_p;
12043 }
12044
12045
12046 /* Do a frame update, taking possible shortcuts into account. This is
12047 the main external entry point for redisplay.
12048
12049 If the last redisplay displayed an echo area message and that message
12050 is no longer requested, we clear the echo area or bring back the
12051 mini-buffer if that is in use. */
12052
12053 void
12054 redisplay (void)
12055 {
12056 redisplay_internal ();
12057 }
12058
12059
12060 static Lisp_Object
12061 overlay_arrow_string_or_property (Lisp_Object var)
12062 {
12063 Lisp_Object val;
12064
12065 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12066 return val;
12067
12068 return Voverlay_arrow_string;
12069 }
12070
12071 /* Return 1 if there are any overlay-arrows in current_buffer. */
12072 static int
12073 overlay_arrow_in_current_buffer_p (void)
12074 {
12075 Lisp_Object vlist;
12076
12077 for (vlist = Voverlay_arrow_variable_list;
12078 CONSP (vlist);
12079 vlist = XCDR (vlist))
12080 {
12081 Lisp_Object var = XCAR (vlist);
12082 Lisp_Object val;
12083
12084 if (!SYMBOLP (var))
12085 continue;
12086 val = find_symbol_value (var);
12087 if (MARKERP (val)
12088 && current_buffer == XMARKER (val)->buffer)
12089 return 1;
12090 }
12091 return 0;
12092 }
12093
12094
12095 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12096 has changed. */
12097
12098 static int
12099 overlay_arrows_changed_p (void)
12100 {
12101 Lisp_Object vlist;
12102
12103 for (vlist = Voverlay_arrow_variable_list;
12104 CONSP (vlist);
12105 vlist = XCDR (vlist))
12106 {
12107 Lisp_Object var = XCAR (vlist);
12108 Lisp_Object val, pstr;
12109
12110 if (!SYMBOLP (var))
12111 continue;
12112 val = find_symbol_value (var);
12113 if (!MARKERP (val))
12114 continue;
12115 if (! EQ (COERCE_MARKER (val),
12116 Fget (var, Qlast_arrow_position))
12117 || ! (pstr = overlay_arrow_string_or_property (var),
12118 EQ (pstr, Fget (var, Qlast_arrow_string))))
12119 return 1;
12120 }
12121 return 0;
12122 }
12123
12124 /* Mark overlay arrows to be updated on next redisplay. */
12125
12126 static void
12127 update_overlay_arrows (int up_to_date)
12128 {
12129 Lisp_Object vlist;
12130
12131 for (vlist = Voverlay_arrow_variable_list;
12132 CONSP (vlist);
12133 vlist = XCDR (vlist))
12134 {
12135 Lisp_Object var = XCAR (vlist);
12136
12137 if (!SYMBOLP (var))
12138 continue;
12139
12140 if (up_to_date > 0)
12141 {
12142 Lisp_Object val = find_symbol_value (var);
12143 Fput (var, Qlast_arrow_position,
12144 COERCE_MARKER (val));
12145 Fput (var, Qlast_arrow_string,
12146 overlay_arrow_string_or_property (var));
12147 }
12148 else if (up_to_date < 0
12149 || !NILP (Fget (var, Qlast_arrow_position)))
12150 {
12151 Fput (var, Qlast_arrow_position, Qt);
12152 Fput (var, Qlast_arrow_string, Qt);
12153 }
12154 }
12155 }
12156
12157
12158 /* Return overlay arrow string to display at row.
12159 Return integer (bitmap number) for arrow bitmap in left fringe.
12160 Return nil if no overlay arrow. */
12161
12162 static Lisp_Object
12163 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12164 {
12165 Lisp_Object vlist;
12166
12167 for (vlist = Voverlay_arrow_variable_list;
12168 CONSP (vlist);
12169 vlist = XCDR (vlist))
12170 {
12171 Lisp_Object var = XCAR (vlist);
12172 Lisp_Object val;
12173
12174 if (!SYMBOLP (var))
12175 continue;
12176
12177 val = find_symbol_value (var);
12178
12179 if (MARKERP (val)
12180 && current_buffer == XMARKER (val)->buffer
12181 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12182 {
12183 if (FRAME_WINDOW_P (it->f)
12184 /* FIXME: if ROW->reversed_p is set, this should test
12185 the right fringe, not the left one. */
12186 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12187 {
12188 #ifdef HAVE_WINDOW_SYSTEM
12189 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12190 {
12191 int fringe_bitmap;
12192 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12193 return make_number (fringe_bitmap);
12194 }
12195 #endif
12196 return make_number (-1); /* Use default arrow bitmap */
12197 }
12198 return overlay_arrow_string_or_property (var);
12199 }
12200 }
12201
12202 return Qnil;
12203 }
12204
12205 /* Return 1 if point moved out of or into a composition. Otherwise
12206 return 0. PREV_BUF and PREV_PT are the last point buffer and
12207 position. BUF and PT are the current point buffer and position. */
12208
12209 static int
12210 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12211 struct buffer *buf, EMACS_INT pt)
12212 {
12213 EMACS_INT start, end;
12214 Lisp_Object prop;
12215 Lisp_Object buffer;
12216
12217 XSETBUFFER (buffer, buf);
12218 /* Check a composition at the last point if point moved within the
12219 same buffer. */
12220 if (prev_buf == buf)
12221 {
12222 if (prev_pt == pt)
12223 /* Point didn't move. */
12224 return 0;
12225
12226 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12227 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12228 && COMPOSITION_VALID_P (start, end, prop)
12229 && start < prev_pt && end > prev_pt)
12230 /* The last point was within the composition. Return 1 iff
12231 point moved out of the composition. */
12232 return (pt <= start || pt >= end);
12233 }
12234
12235 /* Check a composition at the current point. */
12236 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12237 && find_composition (pt, -1, &start, &end, &prop, buffer)
12238 && COMPOSITION_VALID_P (start, end, prop)
12239 && start < pt && end > pt);
12240 }
12241
12242
12243 /* Reconsider the setting of B->clip_changed which is displayed
12244 in window W. */
12245
12246 static inline void
12247 reconsider_clip_changes (struct window *w, struct buffer *b)
12248 {
12249 if (b->clip_changed
12250 && !NILP (w->window_end_valid)
12251 && w->current_matrix->buffer == b
12252 && w->current_matrix->zv == BUF_ZV (b)
12253 && w->current_matrix->begv == BUF_BEGV (b))
12254 b->clip_changed = 0;
12255
12256 /* If display wasn't paused, and W is not a tool bar window, see if
12257 point has been moved into or out of a composition. In that case,
12258 we set b->clip_changed to 1 to force updating the screen. If
12259 b->clip_changed has already been set to 1, we can skip this
12260 check. */
12261 if (!b->clip_changed
12262 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12263 {
12264 EMACS_INT pt;
12265
12266 if (w == XWINDOW (selected_window))
12267 pt = PT;
12268 else
12269 pt = marker_position (w->pointm);
12270
12271 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12272 || pt != XINT (w->last_point))
12273 && check_point_in_composition (w->current_matrix->buffer,
12274 XINT (w->last_point),
12275 XBUFFER (w->buffer), pt))
12276 b->clip_changed = 1;
12277 }
12278 }
12279 \f
12280
12281 /* Select FRAME to forward the values of frame-local variables into C
12282 variables so that the redisplay routines can access those values
12283 directly. */
12284
12285 static void
12286 select_frame_for_redisplay (Lisp_Object frame)
12287 {
12288 Lisp_Object tail, tem;
12289 Lisp_Object old = selected_frame;
12290 struct Lisp_Symbol *sym;
12291
12292 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12293
12294 selected_frame = frame;
12295
12296 do {
12297 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12298 if (CONSP (XCAR (tail))
12299 && (tem = XCAR (XCAR (tail)),
12300 SYMBOLP (tem))
12301 && (sym = indirect_variable (XSYMBOL (tem)),
12302 sym->redirect == SYMBOL_LOCALIZED)
12303 && sym->val.blv->frame_local)
12304 /* Use find_symbol_value rather than Fsymbol_value
12305 to avoid an error if it is void. */
12306 find_symbol_value (tem);
12307 } while (!EQ (frame, old) && (frame = old, 1));
12308 }
12309
12310
12311 #define STOP_POLLING \
12312 do { if (! polling_stopped_here) stop_polling (); \
12313 polling_stopped_here = 1; } while (0)
12314
12315 #define RESUME_POLLING \
12316 do { if (polling_stopped_here) start_polling (); \
12317 polling_stopped_here = 0; } while (0)
12318
12319
12320 /* Perhaps in the future avoid recentering windows if it
12321 is not necessary; currently that causes some problems. */
12322
12323 static void
12324 redisplay_internal (void)
12325 {
12326 struct window *w = XWINDOW (selected_window);
12327 struct window *sw;
12328 struct frame *fr;
12329 int pending;
12330 int must_finish = 0;
12331 struct text_pos tlbufpos, tlendpos;
12332 int number_of_visible_frames;
12333 int count, count1;
12334 struct frame *sf;
12335 int polling_stopped_here = 0;
12336 Lisp_Object old_frame = selected_frame;
12337
12338 /* Non-zero means redisplay has to consider all windows on all
12339 frames. Zero means, only selected_window is considered. */
12340 int consider_all_windows_p;
12341
12342 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12343
12344 /* No redisplay if running in batch mode or frame is not yet fully
12345 initialized, or redisplay is explicitly turned off by setting
12346 Vinhibit_redisplay. */
12347 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12348 || !NILP (Vinhibit_redisplay))
12349 return;
12350
12351 /* Don't examine these until after testing Vinhibit_redisplay.
12352 When Emacs is shutting down, perhaps because its connection to
12353 X has dropped, we should not look at them at all. */
12354 fr = XFRAME (w->frame);
12355 sf = SELECTED_FRAME ();
12356
12357 if (!fr->glyphs_initialized_p)
12358 return;
12359
12360 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12361 if (popup_activated ())
12362 return;
12363 #endif
12364
12365 /* I don't think this happens but let's be paranoid. */
12366 if (redisplaying_p)
12367 return;
12368
12369 /* Record a function that resets redisplaying_p to its old value
12370 when we leave this function. */
12371 count = SPECPDL_INDEX ();
12372 record_unwind_protect (unwind_redisplay,
12373 Fcons (make_number (redisplaying_p), selected_frame));
12374 ++redisplaying_p;
12375 specbind (Qinhibit_free_realized_faces, Qnil);
12376
12377 {
12378 Lisp_Object tail, frame;
12379
12380 FOR_EACH_FRAME (tail, frame)
12381 {
12382 struct frame *f = XFRAME (frame);
12383 f->already_hscrolled_p = 0;
12384 }
12385 }
12386
12387 retry:
12388 /* Remember the currently selected window. */
12389 sw = w;
12390
12391 if (!EQ (old_frame, selected_frame)
12392 && FRAME_LIVE_P (XFRAME (old_frame)))
12393 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12394 selected_frame and selected_window to be temporarily out-of-sync so
12395 when we come back here via `goto retry', we need to resync because we
12396 may need to run Elisp code (via prepare_menu_bars). */
12397 select_frame_for_redisplay (old_frame);
12398
12399 pending = 0;
12400 reconsider_clip_changes (w, current_buffer);
12401 last_escape_glyph_frame = NULL;
12402 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12403 last_glyphless_glyph_frame = NULL;
12404 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12405
12406 /* If new fonts have been loaded that make a glyph matrix adjustment
12407 necessary, do it. */
12408 if (fonts_changed_p)
12409 {
12410 adjust_glyphs (NULL);
12411 ++windows_or_buffers_changed;
12412 fonts_changed_p = 0;
12413 }
12414
12415 /* If face_change_count is non-zero, init_iterator will free all
12416 realized faces, which includes the faces referenced from current
12417 matrices. So, we can't reuse current matrices in this case. */
12418 if (face_change_count)
12419 ++windows_or_buffers_changed;
12420
12421 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12422 && FRAME_TTY (sf)->previous_frame != sf)
12423 {
12424 /* Since frames on a single ASCII terminal share the same
12425 display area, displaying a different frame means redisplay
12426 the whole thing. */
12427 windows_or_buffers_changed++;
12428 SET_FRAME_GARBAGED (sf);
12429 #ifndef DOS_NT
12430 set_tty_color_mode (FRAME_TTY (sf), sf);
12431 #endif
12432 FRAME_TTY (sf)->previous_frame = sf;
12433 }
12434
12435 /* Set the visible flags for all frames. Do this before checking
12436 for resized or garbaged frames; they want to know if their frames
12437 are visible. See the comment in frame.h for
12438 FRAME_SAMPLE_VISIBILITY. */
12439 {
12440 Lisp_Object tail, frame;
12441
12442 number_of_visible_frames = 0;
12443
12444 FOR_EACH_FRAME (tail, frame)
12445 {
12446 struct frame *f = XFRAME (frame);
12447
12448 FRAME_SAMPLE_VISIBILITY (f);
12449 if (FRAME_VISIBLE_P (f))
12450 ++number_of_visible_frames;
12451 clear_desired_matrices (f);
12452 }
12453 }
12454
12455 /* Notice any pending interrupt request to change frame size. */
12456 do_pending_window_change (1);
12457
12458 /* do_pending_window_change could change the selected_window due to
12459 frame resizing which makes the selected window too small. */
12460 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12461 {
12462 sw = w;
12463 reconsider_clip_changes (w, current_buffer);
12464 }
12465
12466 /* Clear frames marked as garbaged. */
12467 if (frame_garbaged)
12468 clear_garbaged_frames ();
12469
12470 /* Build menubar and tool-bar items. */
12471 if (NILP (Vmemory_full))
12472 prepare_menu_bars ();
12473
12474 if (windows_or_buffers_changed)
12475 update_mode_lines++;
12476
12477 /* Detect case that we need to write or remove a star in the mode line. */
12478 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12479 {
12480 w->update_mode_line = Qt;
12481 if (buffer_shared > 1)
12482 update_mode_lines++;
12483 }
12484
12485 /* Avoid invocation of point motion hooks by `current_column' below. */
12486 count1 = SPECPDL_INDEX ();
12487 specbind (Qinhibit_point_motion_hooks, Qt);
12488
12489 /* If %c is in the mode line, update it if needed. */
12490 if (!NILP (w->column_number_displayed)
12491 /* This alternative quickly identifies a common case
12492 where no change is needed. */
12493 && !(PT == XFASTINT (w->last_point)
12494 && XFASTINT (w->last_modified) >= MODIFF
12495 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12496 && (XFASTINT (w->column_number_displayed) != current_column ()))
12497 w->update_mode_line = Qt;
12498
12499 unbind_to (count1, Qnil);
12500
12501 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12502
12503 /* The variable buffer_shared is set in redisplay_window and
12504 indicates that we redisplay a buffer in different windows. See
12505 there. */
12506 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12507 || cursor_type_changed);
12508
12509 /* If specs for an arrow have changed, do thorough redisplay
12510 to ensure we remove any arrow that should no longer exist. */
12511 if (overlay_arrows_changed_p ())
12512 consider_all_windows_p = windows_or_buffers_changed = 1;
12513
12514 /* Normally the message* functions will have already displayed and
12515 updated the echo area, but the frame may have been trashed, or
12516 the update may have been preempted, so display the echo area
12517 again here. Checking message_cleared_p captures the case that
12518 the echo area should be cleared. */
12519 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12520 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12521 || (message_cleared_p
12522 && minibuf_level == 0
12523 /* If the mini-window is currently selected, this means the
12524 echo-area doesn't show through. */
12525 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12526 {
12527 int window_height_changed_p = echo_area_display (0);
12528 must_finish = 1;
12529
12530 /* If we don't display the current message, don't clear the
12531 message_cleared_p flag, because, if we did, we wouldn't clear
12532 the echo area in the next redisplay which doesn't preserve
12533 the echo area. */
12534 if (!display_last_displayed_message_p)
12535 message_cleared_p = 0;
12536
12537 if (fonts_changed_p)
12538 goto retry;
12539 else if (window_height_changed_p)
12540 {
12541 consider_all_windows_p = 1;
12542 ++update_mode_lines;
12543 ++windows_or_buffers_changed;
12544
12545 /* If window configuration was changed, frames may have been
12546 marked garbaged. Clear them or we will experience
12547 surprises wrt scrolling. */
12548 if (frame_garbaged)
12549 clear_garbaged_frames ();
12550 }
12551 }
12552 else if (EQ (selected_window, minibuf_window)
12553 && (current_buffer->clip_changed
12554 || XFASTINT (w->last_modified) < MODIFF
12555 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12556 && resize_mini_window (w, 0))
12557 {
12558 /* Resized active mini-window to fit the size of what it is
12559 showing if its contents might have changed. */
12560 must_finish = 1;
12561 /* FIXME: this causes all frames to be updated, which seems unnecessary
12562 since only the current frame needs to be considered. This function needs
12563 to be rewritten with two variables, consider_all_windows and
12564 consider_all_frames. */
12565 consider_all_windows_p = 1;
12566 ++windows_or_buffers_changed;
12567 ++update_mode_lines;
12568
12569 /* If window configuration was changed, frames may have been
12570 marked garbaged. Clear them or we will experience
12571 surprises wrt scrolling. */
12572 if (frame_garbaged)
12573 clear_garbaged_frames ();
12574 }
12575
12576
12577 /* If showing the region, and mark has changed, we must redisplay
12578 the whole window. The assignment to this_line_start_pos prevents
12579 the optimization directly below this if-statement. */
12580 if (((!NILP (Vtransient_mark_mode)
12581 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12582 != !NILP (w->region_showing))
12583 || (!NILP (w->region_showing)
12584 && !EQ (w->region_showing,
12585 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12586 CHARPOS (this_line_start_pos) = 0;
12587
12588 /* Optimize the case that only the line containing the cursor in the
12589 selected window has changed. Variables starting with this_ are
12590 set in display_line and record information about the line
12591 containing the cursor. */
12592 tlbufpos = this_line_start_pos;
12593 tlendpos = this_line_end_pos;
12594 if (!consider_all_windows_p
12595 && CHARPOS (tlbufpos) > 0
12596 && NILP (w->update_mode_line)
12597 && !current_buffer->clip_changed
12598 && !current_buffer->prevent_redisplay_optimizations_p
12599 && FRAME_VISIBLE_P (XFRAME (w->frame))
12600 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12601 /* Make sure recorded data applies to current buffer, etc. */
12602 && this_line_buffer == current_buffer
12603 && current_buffer == XBUFFER (w->buffer)
12604 && NILP (w->force_start)
12605 && NILP (w->optional_new_start)
12606 /* Point must be on the line that we have info recorded about. */
12607 && PT >= CHARPOS (tlbufpos)
12608 && PT <= Z - CHARPOS (tlendpos)
12609 /* All text outside that line, including its final newline,
12610 must be unchanged. */
12611 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12612 CHARPOS (tlendpos)))
12613 {
12614 if (CHARPOS (tlbufpos) > BEGV
12615 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12616 && (CHARPOS (tlbufpos) == ZV
12617 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12618 /* Former continuation line has disappeared by becoming empty. */
12619 goto cancel;
12620 else if (XFASTINT (w->last_modified) < MODIFF
12621 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12622 || MINI_WINDOW_P (w))
12623 {
12624 /* We have to handle the case of continuation around a
12625 wide-column character (see the comment in indent.c around
12626 line 1340).
12627
12628 For instance, in the following case:
12629
12630 -------- Insert --------
12631 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12632 J_I_ ==> J_I_ `^^' are cursors.
12633 ^^ ^^
12634 -------- --------
12635
12636 As we have to redraw the line above, we cannot use this
12637 optimization. */
12638
12639 struct it it;
12640 int line_height_before = this_line_pixel_height;
12641
12642 /* Note that start_display will handle the case that the
12643 line starting at tlbufpos is a continuation line. */
12644 start_display (&it, w, tlbufpos);
12645
12646 /* Implementation note: It this still necessary? */
12647 if (it.current_x != this_line_start_x)
12648 goto cancel;
12649
12650 TRACE ((stderr, "trying display optimization 1\n"));
12651 w->cursor.vpos = -1;
12652 overlay_arrow_seen = 0;
12653 it.vpos = this_line_vpos;
12654 it.current_y = this_line_y;
12655 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12656 display_line (&it);
12657
12658 /* If line contains point, is not continued,
12659 and ends at same distance from eob as before, we win. */
12660 if (w->cursor.vpos >= 0
12661 /* Line is not continued, otherwise this_line_start_pos
12662 would have been set to 0 in display_line. */
12663 && CHARPOS (this_line_start_pos)
12664 /* Line ends as before. */
12665 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12666 /* Line has same height as before. Otherwise other lines
12667 would have to be shifted up or down. */
12668 && this_line_pixel_height == line_height_before)
12669 {
12670 /* If this is not the window's last line, we must adjust
12671 the charstarts of the lines below. */
12672 if (it.current_y < it.last_visible_y)
12673 {
12674 struct glyph_row *row
12675 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12676 EMACS_INT delta, delta_bytes;
12677
12678 /* We used to distinguish between two cases here,
12679 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12680 when the line ends in a newline or the end of the
12681 buffer's accessible portion. But both cases did
12682 the same, so they were collapsed. */
12683 delta = (Z
12684 - CHARPOS (tlendpos)
12685 - MATRIX_ROW_START_CHARPOS (row));
12686 delta_bytes = (Z_BYTE
12687 - BYTEPOS (tlendpos)
12688 - MATRIX_ROW_START_BYTEPOS (row));
12689
12690 increment_matrix_positions (w->current_matrix,
12691 this_line_vpos + 1,
12692 w->current_matrix->nrows,
12693 delta, delta_bytes);
12694 }
12695
12696 /* If this row displays text now but previously didn't,
12697 or vice versa, w->window_end_vpos may have to be
12698 adjusted. */
12699 if ((it.glyph_row - 1)->displays_text_p)
12700 {
12701 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12702 XSETINT (w->window_end_vpos, this_line_vpos);
12703 }
12704 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12705 && this_line_vpos > 0)
12706 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12707 w->window_end_valid = Qnil;
12708
12709 /* Update hint: No need to try to scroll in update_window. */
12710 w->desired_matrix->no_scrolling_p = 1;
12711
12712 #if GLYPH_DEBUG
12713 *w->desired_matrix->method = 0;
12714 debug_method_add (w, "optimization 1");
12715 #endif
12716 #ifdef HAVE_WINDOW_SYSTEM
12717 update_window_fringes (w, 0);
12718 #endif
12719 goto update;
12720 }
12721 else
12722 goto cancel;
12723 }
12724 else if (/* Cursor position hasn't changed. */
12725 PT == XFASTINT (w->last_point)
12726 /* Make sure the cursor was last displayed
12727 in this window. Otherwise we have to reposition it. */
12728 && 0 <= w->cursor.vpos
12729 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12730 {
12731 if (!must_finish)
12732 {
12733 do_pending_window_change (1);
12734 /* If selected_window changed, redisplay again. */
12735 if (WINDOWP (selected_window)
12736 && (w = XWINDOW (selected_window)) != sw)
12737 goto retry;
12738
12739 /* We used to always goto end_of_redisplay here, but this
12740 isn't enough if we have a blinking cursor. */
12741 if (w->cursor_off_p == w->last_cursor_off_p)
12742 goto end_of_redisplay;
12743 }
12744 goto update;
12745 }
12746 /* If highlighting the region, or if the cursor is in the echo area,
12747 then we can't just move the cursor. */
12748 else if (! (!NILP (Vtransient_mark_mode)
12749 && !NILP (BVAR (current_buffer, mark_active)))
12750 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12751 || highlight_nonselected_windows)
12752 && NILP (w->region_showing)
12753 && NILP (Vshow_trailing_whitespace)
12754 && !cursor_in_echo_area)
12755 {
12756 struct it it;
12757 struct glyph_row *row;
12758
12759 /* Skip from tlbufpos to PT and see where it is. Note that
12760 PT may be in invisible text. If so, we will end at the
12761 next visible position. */
12762 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12763 NULL, DEFAULT_FACE_ID);
12764 it.current_x = this_line_start_x;
12765 it.current_y = this_line_y;
12766 it.vpos = this_line_vpos;
12767
12768 /* The call to move_it_to stops in front of PT, but
12769 moves over before-strings. */
12770 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12771
12772 if (it.vpos == this_line_vpos
12773 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12774 row->enabled_p))
12775 {
12776 xassert (this_line_vpos == it.vpos);
12777 xassert (this_line_y == it.current_y);
12778 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12779 #if GLYPH_DEBUG
12780 *w->desired_matrix->method = 0;
12781 debug_method_add (w, "optimization 3");
12782 #endif
12783 goto update;
12784 }
12785 else
12786 goto cancel;
12787 }
12788
12789 cancel:
12790 /* Text changed drastically or point moved off of line. */
12791 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12792 }
12793
12794 CHARPOS (this_line_start_pos) = 0;
12795 consider_all_windows_p |= buffer_shared > 1;
12796 ++clear_face_cache_count;
12797 #ifdef HAVE_WINDOW_SYSTEM
12798 ++clear_image_cache_count;
12799 #endif
12800
12801 /* Build desired matrices, and update the display. If
12802 consider_all_windows_p is non-zero, do it for all windows on all
12803 frames. Otherwise do it for selected_window, only. */
12804
12805 if (consider_all_windows_p)
12806 {
12807 Lisp_Object tail, frame;
12808
12809 FOR_EACH_FRAME (tail, frame)
12810 XFRAME (frame)->updated_p = 0;
12811
12812 /* Recompute # windows showing selected buffer. This will be
12813 incremented each time such a window is displayed. */
12814 buffer_shared = 0;
12815
12816 FOR_EACH_FRAME (tail, frame)
12817 {
12818 struct frame *f = XFRAME (frame);
12819
12820 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12821 {
12822 if (! EQ (frame, selected_frame))
12823 /* Select the frame, for the sake of frame-local
12824 variables. */
12825 select_frame_for_redisplay (frame);
12826
12827 /* Mark all the scroll bars to be removed; we'll redeem
12828 the ones we want when we redisplay their windows. */
12829 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12830 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12831
12832 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12833 redisplay_windows (FRAME_ROOT_WINDOW (f));
12834
12835 /* The X error handler may have deleted that frame. */
12836 if (!FRAME_LIVE_P (f))
12837 continue;
12838
12839 /* Any scroll bars which redisplay_windows should have
12840 nuked should now go away. */
12841 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12842 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12843
12844 /* If fonts changed, display again. */
12845 /* ??? rms: I suspect it is a mistake to jump all the way
12846 back to retry here. It should just retry this frame. */
12847 if (fonts_changed_p)
12848 goto retry;
12849
12850 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12851 {
12852 /* See if we have to hscroll. */
12853 if (!f->already_hscrolled_p)
12854 {
12855 f->already_hscrolled_p = 1;
12856 if (hscroll_windows (f->root_window))
12857 goto retry;
12858 }
12859
12860 /* Prevent various kinds of signals during display
12861 update. stdio is not robust about handling
12862 signals, which can cause an apparent I/O
12863 error. */
12864 if (interrupt_input)
12865 unrequest_sigio ();
12866 STOP_POLLING;
12867
12868 /* Update the display. */
12869 set_window_update_flags (XWINDOW (f->root_window), 1);
12870 pending |= update_frame (f, 0, 0);
12871 f->updated_p = 1;
12872 }
12873 }
12874 }
12875
12876 if (!EQ (old_frame, selected_frame)
12877 && FRAME_LIVE_P (XFRAME (old_frame)))
12878 /* We played a bit fast-and-loose above and allowed selected_frame
12879 and selected_window to be temporarily out-of-sync but let's make
12880 sure this stays contained. */
12881 select_frame_for_redisplay (old_frame);
12882 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12883
12884 if (!pending)
12885 {
12886 /* Do the mark_window_display_accurate after all windows have
12887 been redisplayed because this call resets flags in buffers
12888 which are needed for proper redisplay. */
12889 FOR_EACH_FRAME (tail, frame)
12890 {
12891 struct frame *f = XFRAME (frame);
12892 if (f->updated_p)
12893 {
12894 mark_window_display_accurate (f->root_window, 1);
12895 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12896 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12897 }
12898 }
12899 }
12900 }
12901 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12902 {
12903 Lisp_Object mini_window;
12904 struct frame *mini_frame;
12905
12906 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12907 /* Use list_of_error, not Qerror, so that
12908 we catch only errors and don't run the debugger. */
12909 internal_condition_case_1 (redisplay_window_1, selected_window,
12910 list_of_error,
12911 redisplay_window_error);
12912
12913 /* Compare desired and current matrices, perform output. */
12914
12915 update:
12916 /* If fonts changed, display again. */
12917 if (fonts_changed_p)
12918 goto retry;
12919
12920 /* Prevent various kinds of signals during display update.
12921 stdio is not robust about handling signals,
12922 which can cause an apparent I/O error. */
12923 if (interrupt_input)
12924 unrequest_sigio ();
12925 STOP_POLLING;
12926
12927 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12928 {
12929 if (hscroll_windows (selected_window))
12930 goto retry;
12931
12932 XWINDOW (selected_window)->must_be_updated_p = 1;
12933 pending = update_frame (sf, 0, 0);
12934 }
12935
12936 /* We may have called echo_area_display at the top of this
12937 function. If the echo area is on another frame, that may
12938 have put text on a frame other than the selected one, so the
12939 above call to update_frame would not have caught it. Catch
12940 it here. */
12941 mini_window = FRAME_MINIBUF_WINDOW (sf);
12942 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12943
12944 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12945 {
12946 XWINDOW (mini_window)->must_be_updated_p = 1;
12947 pending |= update_frame (mini_frame, 0, 0);
12948 if (!pending && hscroll_windows (mini_window))
12949 goto retry;
12950 }
12951 }
12952
12953 /* If display was paused because of pending input, make sure we do a
12954 thorough update the next time. */
12955 if (pending)
12956 {
12957 /* Prevent the optimization at the beginning of
12958 redisplay_internal that tries a single-line update of the
12959 line containing the cursor in the selected window. */
12960 CHARPOS (this_line_start_pos) = 0;
12961
12962 /* Let the overlay arrow be updated the next time. */
12963 update_overlay_arrows (0);
12964
12965 /* If we pause after scrolling, some rows in the current
12966 matrices of some windows are not valid. */
12967 if (!WINDOW_FULL_WIDTH_P (w)
12968 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12969 update_mode_lines = 1;
12970 }
12971 else
12972 {
12973 if (!consider_all_windows_p)
12974 {
12975 /* This has already been done above if
12976 consider_all_windows_p is set. */
12977 mark_window_display_accurate_1 (w, 1);
12978
12979 /* Say overlay arrows are up to date. */
12980 update_overlay_arrows (1);
12981
12982 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12983 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12984 }
12985
12986 update_mode_lines = 0;
12987 windows_or_buffers_changed = 0;
12988 cursor_type_changed = 0;
12989 }
12990
12991 /* Start SIGIO interrupts coming again. Having them off during the
12992 code above makes it less likely one will discard output, but not
12993 impossible, since there might be stuff in the system buffer here.
12994 But it is much hairier to try to do anything about that. */
12995 if (interrupt_input)
12996 request_sigio ();
12997 RESUME_POLLING;
12998
12999 /* If a frame has become visible which was not before, redisplay
13000 again, so that we display it. Expose events for such a frame
13001 (which it gets when becoming visible) don't call the parts of
13002 redisplay constructing glyphs, so simply exposing a frame won't
13003 display anything in this case. So, we have to display these
13004 frames here explicitly. */
13005 if (!pending)
13006 {
13007 Lisp_Object tail, frame;
13008 int new_count = 0;
13009
13010 FOR_EACH_FRAME (tail, frame)
13011 {
13012 int this_is_visible = 0;
13013
13014 if (XFRAME (frame)->visible)
13015 this_is_visible = 1;
13016 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13017 if (XFRAME (frame)->visible)
13018 this_is_visible = 1;
13019
13020 if (this_is_visible)
13021 new_count++;
13022 }
13023
13024 if (new_count != number_of_visible_frames)
13025 windows_or_buffers_changed++;
13026 }
13027
13028 /* Change frame size now if a change is pending. */
13029 do_pending_window_change (1);
13030
13031 /* If we just did a pending size change, or have additional
13032 visible frames, or selected_window changed, redisplay again. */
13033 if ((windows_or_buffers_changed && !pending)
13034 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13035 goto retry;
13036
13037 /* Clear the face and image caches.
13038
13039 We used to do this only if consider_all_windows_p. But the cache
13040 needs to be cleared if a timer creates images in the current
13041 buffer (e.g. the test case in Bug#6230). */
13042
13043 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13044 {
13045 clear_face_cache (0);
13046 clear_face_cache_count = 0;
13047 }
13048
13049 #ifdef HAVE_WINDOW_SYSTEM
13050 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13051 {
13052 clear_image_caches (Qnil);
13053 clear_image_cache_count = 0;
13054 }
13055 #endif /* HAVE_WINDOW_SYSTEM */
13056
13057 end_of_redisplay:
13058 unbind_to (count, Qnil);
13059 RESUME_POLLING;
13060 }
13061
13062
13063 /* Redisplay, but leave alone any recent echo area message unless
13064 another message has been requested in its place.
13065
13066 This is useful in situations where you need to redisplay but no
13067 user action has occurred, making it inappropriate for the message
13068 area to be cleared. See tracking_off and
13069 wait_reading_process_output for examples of these situations.
13070
13071 FROM_WHERE is an integer saying from where this function was
13072 called. This is useful for debugging. */
13073
13074 void
13075 redisplay_preserve_echo_area (int from_where)
13076 {
13077 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13078
13079 if (!NILP (echo_area_buffer[1]))
13080 {
13081 /* We have a previously displayed message, but no current
13082 message. Redisplay the previous message. */
13083 display_last_displayed_message_p = 1;
13084 redisplay_internal ();
13085 display_last_displayed_message_p = 0;
13086 }
13087 else
13088 redisplay_internal ();
13089
13090 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13091 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13092 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13093 }
13094
13095
13096 /* Function registered with record_unwind_protect in
13097 redisplay_internal. Reset redisplaying_p to the value it had
13098 before redisplay_internal was called, and clear
13099 prevent_freeing_realized_faces_p. It also selects the previously
13100 selected frame, unless it has been deleted (by an X connection
13101 failure during redisplay, for example). */
13102
13103 static Lisp_Object
13104 unwind_redisplay (Lisp_Object val)
13105 {
13106 Lisp_Object old_redisplaying_p, old_frame;
13107
13108 old_redisplaying_p = XCAR (val);
13109 redisplaying_p = XFASTINT (old_redisplaying_p);
13110 old_frame = XCDR (val);
13111 if (! EQ (old_frame, selected_frame)
13112 && FRAME_LIVE_P (XFRAME (old_frame)))
13113 select_frame_for_redisplay (old_frame);
13114 return Qnil;
13115 }
13116
13117
13118 /* Mark the display of window W as accurate or inaccurate. If
13119 ACCURATE_P is non-zero mark display of W as accurate. If
13120 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13121 redisplay_internal is called. */
13122
13123 static void
13124 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13125 {
13126 if (BUFFERP (w->buffer))
13127 {
13128 struct buffer *b = XBUFFER (w->buffer);
13129
13130 w->last_modified
13131 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13132 w->last_overlay_modified
13133 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13134 w->last_had_star
13135 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13136
13137 if (accurate_p)
13138 {
13139 b->clip_changed = 0;
13140 b->prevent_redisplay_optimizations_p = 0;
13141
13142 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13143 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13144 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13145 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13146
13147 w->current_matrix->buffer = b;
13148 w->current_matrix->begv = BUF_BEGV (b);
13149 w->current_matrix->zv = BUF_ZV (b);
13150
13151 w->last_cursor = w->cursor;
13152 w->last_cursor_off_p = w->cursor_off_p;
13153
13154 if (w == XWINDOW (selected_window))
13155 w->last_point = make_number (BUF_PT (b));
13156 else
13157 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13158 }
13159 }
13160
13161 if (accurate_p)
13162 {
13163 w->window_end_valid = w->buffer;
13164 w->update_mode_line = Qnil;
13165 }
13166 }
13167
13168
13169 /* Mark the display of windows in the window tree rooted at WINDOW as
13170 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13171 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13172 be redisplayed the next time redisplay_internal is called. */
13173
13174 void
13175 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13176 {
13177 struct window *w;
13178
13179 for (; !NILP (window); window = w->next)
13180 {
13181 w = XWINDOW (window);
13182 mark_window_display_accurate_1 (w, accurate_p);
13183
13184 if (!NILP (w->vchild))
13185 mark_window_display_accurate (w->vchild, accurate_p);
13186 if (!NILP (w->hchild))
13187 mark_window_display_accurate (w->hchild, accurate_p);
13188 }
13189
13190 if (accurate_p)
13191 {
13192 update_overlay_arrows (1);
13193 }
13194 else
13195 {
13196 /* Force a thorough redisplay the next time by setting
13197 last_arrow_position and last_arrow_string to t, which is
13198 unequal to any useful value of Voverlay_arrow_... */
13199 update_overlay_arrows (-1);
13200 }
13201 }
13202
13203
13204 /* Return value in display table DP (Lisp_Char_Table *) for character
13205 C. Since a display table doesn't have any parent, we don't have to
13206 follow parent. Do not call this function directly but use the
13207 macro DISP_CHAR_VECTOR. */
13208
13209 Lisp_Object
13210 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13211 {
13212 Lisp_Object val;
13213
13214 if (ASCII_CHAR_P (c))
13215 {
13216 val = dp->ascii;
13217 if (SUB_CHAR_TABLE_P (val))
13218 val = XSUB_CHAR_TABLE (val)->contents[c];
13219 }
13220 else
13221 {
13222 Lisp_Object table;
13223
13224 XSETCHAR_TABLE (table, dp);
13225 val = char_table_ref (table, c);
13226 }
13227 if (NILP (val))
13228 val = dp->defalt;
13229 return val;
13230 }
13231
13232
13233 \f
13234 /***********************************************************************
13235 Window Redisplay
13236 ***********************************************************************/
13237
13238 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13239
13240 static void
13241 redisplay_windows (Lisp_Object window)
13242 {
13243 while (!NILP (window))
13244 {
13245 struct window *w = XWINDOW (window);
13246
13247 if (!NILP (w->hchild))
13248 redisplay_windows (w->hchild);
13249 else if (!NILP (w->vchild))
13250 redisplay_windows (w->vchild);
13251 else if (!NILP (w->buffer))
13252 {
13253 displayed_buffer = XBUFFER (w->buffer);
13254 /* Use list_of_error, not Qerror, so that
13255 we catch only errors and don't run the debugger. */
13256 internal_condition_case_1 (redisplay_window_0, window,
13257 list_of_error,
13258 redisplay_window_error);
13259 }
13260
13261 window = w->next;
13262 }
13263 }
13264
13265 static Lisp_Object
13266 redisplay_window_error (Lisp_Object ignore)
13267 {
13268 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13269 return Qnil;
13270 }
13271
13272 static Lisp_Object
13273 redisplay_window_0 (Lisp_Object window)
13274 {
13275 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13276 redisplay_window (window, 0);
13277 return Qnil;
13278 }
13279
13280 static Lisp_Object
13281 redisplay_window_1 (Lisp_Object window)
13282 {
13283 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13284 redisplay_window (window, 1);
13285 return Qnil;
13286 }
13287 \f
13288
13289 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13290 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13291 which positions recorded in ROW differ from current buffer
13292 positions.
13293
13294 Return 0 if cursor is not on this row, 1 otherwise. */
13295
13296 static int
13297 set_cursor_from_row (struct window *w, struct glyph_row *row,
13298 struct glyph_matrix *matrix,
13299 EMACS_INT delta, EMACS_INT delta_bytes,
13300 int dy, int dvpos)
13301 {
13302 struct glyph *glyph = row->glyphs[TEXT_AREA];
13303 struct glyph *end = glyph + row->used[TEXT_AREA];
13304 struct glyph *cursor = NULL;
13305 /* The last known character position in row. */
13306 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13307 int x = row->x;
13308 EMACS_INT pt_old = PT - delta;
13309 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13310 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13311 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13312 /* A glyph beyond the edge of TEXT_AREA which we should never
13313 touch. */
13314 struct glyph *glyphs_end = end;
13315 /* Non-zero means we've found a match for cursor position, but that
13316 glyph has the avoid_cursor_p flag set. */
13317 int match_with_avoid_cursor = 0;
13318 /* Non-zero means we've seen at least one glyph that came from a
13319 display string. */
13320 int string_seen = 0;
13321 /* Largest and smalles buffer positions seen so far during scan of
13322 glyph row. */
13323 EMACS_INT bpos_max = pos_before;
13324 EMACS_INT bpos_min = pos_after;
13325 /* Last buffer position covered by an overlay string with an integer
13326 `cursor' property. */
13327 EMACS_INT bpos_covered = 0;
13328 /* Non-zero means the display string on which to display the cursor
13329 comes from a text property, not from an overlay. */
13330 int string_from_text_prop = 0;
13331
13332 /* Skip over glyphs not having an object at the start and the end of
13333 the row. These are special glyphs like truncation marks on
13334 terminal frames. */
13335 if (row->displays_text_p)
13336 {
13337 if (!row->reversed_p)
13338 {
13339 while (glyph < end
13340 && INTEGERP (glyph->object)
13341 && glyph->charpos < 0)
13342 {
13343 x += glyph->pixel_width;
13344 ++glyph;
13345 }
13346 while (end > glyph
13347 && INTEGERP ((end - 1)->object)
13348 /* CHARPOS is zero for blanks and stretch glyphs
13349 inserted by extend_face_to_end_of_line. */
13350 && (end - 1)->charpos <= 0)
13351 --end;
13352 glyph_before = glyph - 1;
13353 glyph_after = end;
13354 }
13355 else
13356 {
13357 struct glyph *g;
13358
13359 /* If the glyph row is reversed, we need to process it from back
13360 to front, so swap the edge pointers. */
13361 glyphs_end = end = glyph - 1;
13362 glyph += row->used[TEXT_AREA] - 1;
13363
13364 while (glyph > end + 1
13365 && INTEGERP (glyph->object)
13366 && glyph->charpos < 0)
13367 {
13368 --glyph;
13369 x -= glyph->pixel_width;
13370 }
13371 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13372 --glyph;
13373 /* By default, in reversed rows we put the cursor on the
13374 rightmost (first in the reading order) glyph. */
13375 for (g = end + 1; g < glyph; g++)
13376 x += g->pixel_width;
13377 while (end < glyph
13378 && INTEGERP ((end + 1)->object)
13379 && (end + 1)->charpos <= 0)
13380 ++end;
13381 glyph_before = glyph + 1;
13382 glyph_after = end;
13383 }
13384 }
13385 else if (row->reversed_p)
13386 {
13387 /* In R2L rows that don't display text, put the cursor on the
13388 rightmost glyph. Case in point: an empty last line that is
13389 part of an R2L paragraph. */
13390 cursor = end - 1;
13391 /* Avoid placing the cursor on the last glyph of the row, where
13392 on terminal frames we hold the vertical border between
13393 adjacent windows. */
13394 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13395 && !WINDOW_RIGHTMOST_P (w)
13396 && cursor == row->glyphs[LAST_AREA] - 1)
13397 cursor--;
13398 x = -1; /* will be computed below, at label compute_x */
13399 }
13400
13401 /* Step 1: Try to find the glyph whose character position
13402 corresponds to point. If that's not possible, find 2 glyphs
13403 whose character positions are the closest to point, one before
13404 point, the other after it. */
13405 if (!row->reversed_p)
13406 while (/* not marched to end of glyph row */
13407 glyph < end
13408 /* glyph was not inserted by redisplay for internal purposes */
13409 && !INTEGERP (glyph->object))
13410 {
13411 if (BUFFERP (glyph->object))
13412 {
13413 EMACS_INT dpos = glyph->charpos - pt_old;
13414
13415 if (glyph->charpos > bpos_max)
13416 bpos_max = glyph->charpos;
13417 if (glyph->charpos < bpos_min)
13418 bpos_min = glyph->charpos;
13419 if (!glyph->avoid_cursor_p)
13420 {
13421 /* If we hit point, we've found the glyph on which to
13422 display the cursor. */
13423 if (dpos == 0)
13424 {
13425 match_with_avoid_cursor = 0;
13426 break;
13427 }
13428 /* See if we've found a better approximation to
13429 POS_BEFORE or to POS_AFTER. Note that we want the
13430 first (leftmost) glyph of all those that are the
13431 closest from below, and the last (rightmost) of all
13432 those from above. */
13433 if (0 > dpos && dpos > pos_before - pt_old)
13434 {
13435 pos_before = glyph->charpos;
13436 glyph_before = glyph;
13437 }
13438 else if (0 < dpos && dpos <= pos_after - pt_old)
13439 {
13440 pos_after = glyph->charpos;
13441 glyph_after = glyph;
13442 }
13443 }
13444 else if (dpos == 0)
13445 match_with_avoid_cursor = 1;
13446 }
13447 else if (STRINGP (glyph->object))
13448 {
13449 Lisp_Object chprop;
13450 EMACS_INT glyph_pos = glyph->charpos;
13451
13452 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13453 glyph->object);
13454 if (INTEGERP (chprop))
13455 {
13456 bpos_covered = bpos_max + XINT (chprop);
13457 /* If the `cursor' property covers buffer positions up
13458 to and including point, we should display cursor on
13459 this glyph. Note that overlays and text properties
13460 with string values stop bidi reordering, so every
13461 buffer position to the left of the string is always
13462 smaller than any position to the right of the
13463 string. Therefore, if a `cursor' property on one
13464 of the string's characters has an integer value, we
13465 will break out of the loop below _before_ we get to
13466 the position match above. IOW, integer values of
13467 the `cursor' property override the "exact match for
13468 point" strategy of positioning the cursor. */
13469 /* Implementation note: bpos_max == pt_old when, e.g.,
13470 we are in an empty line, where bpos_max is set to
13471 MATRIX_ROW_START_CHARPOS, see above. */
13472 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13473 {
13474 cursor = glyph;
13475 break;
13476 }
13477 }
13478
13479 string_seen = 1;
13480 }
13481 x += glyph->pixel_width;
13482 ++glyph;
13483 }
13484 else if (glyph > end) /* row is reversed */
13485 while (!INTEGERP (glyph->object))
13486 {
13487 if (BUFFERP (glyph->object))
13488 {
13489 EMACS_INT dpos = glyph->charpos - pt_old;
13490
13491 if (glyph->charpos > bpos_max)
13492 bpos_max = glyph->charpos;
13493 if (glyph->charpos < bpos_min)
13494 bpos_min = glyph->charpos;
13495 if (!glyph->avoid_cursor_p)
13496 {
13497 if (dpos == 0)
13498 {
13499 match_with_avoid_cursor = 0;
13500 break;
13501 }
13502 if (0 > dpos && dpos > pos_before - pt_old)
13503 {
13504 pos_before = glyph->charpos;
13505 glyph_before = glyph;
13506 }
13507 else if (0 < dpos && dpos <= pos_after - pt_old)
13508 {
13509 pos_after = glyph->charpos;
13510 glyph_after = glyph;
13511 }
13512 }
13513 else if (dpos == 0)
13514 match_with_avoid_cursor = 1;
13515 }
13516 else if (STRINGP (glyph->object))
13517 {
13518 Lisp_Object chprop;
13519 EMACS_INT glyph_pos = glyph->charpos;
13520
13521 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13522 glyph->object);
13523 if (INTEGERP (chprop))
13524 {
13525 bpos_covered = bpos_max + XINT (chprop);
13526 /* If the `cursor' property covers buffer positions up
13527 to and including point, we should display cursor on
13528 this glyph. */
13529 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13530 {
13531 cursor = glyph;
13532 break;
13533 }
13534 }
13535 string_seen = 1;
13536 }
13537 --glyph;
13538 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13539 {
13540 x--; /* can't use any pixel_width */
13541 break;
13542 }
13543 x -= glyph->pixel_width;
13544 }
13545
13546 /* Step 2: If we didn't find an exact match for point, we need to
13547 look for a proper place to put the cursor among glyphs between
13548 GLYPH_BEFORE and GLYPH_AFTER. */
13549 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13550 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13551 && bpos_covered < pt_old)
13552 {
13553 /* An empty line has a single glyph whose OBJECT is zero and
13554 whose CHARPOS is the position of a newline on that line.
13555 Note that on a TTY, there are more glyphs after that, which
13556 were produced by extend_face_to_end_of_line, but their
13557 CHARPOS is zero or negative. */
13558 int empty_line_p =
13559 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13560 && INTEGERP (glyph->object) && glyph->charpos > 0;
13561
13562 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13563 {
13564 EMACS_INT ellipsis_pos;
13565
13566 /* Scan back over the ellipsis glyphs. */
13567 if (!row->reversed_p)
13568 {
13569 ellipsis_pos = (glyph - 1)->charpos;
13570 while (glyph > row->glyphs[TEXT_AREA]
13571 && (glyph - 1)->charpos == ellipsis_pos)
13572 glyph--, x -= glyph->pixel_width;
13573 /* That loop always goes one position too far, including
13574 the glyph before the ellipsis. So scan forward over
13575 that one. */
13576 x += glyph->pixel_width;
13577 glyph++;
13578 }
13579 else /* row is reversed */
13580 {
13581 ellipsis_pos = (glyph + 1)->charpos;
13582 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13583 && (glyph + 1)->charpos == ellipsis_pos)
13584 glyph++, x += glyph->pixel_width;
13585 x -= glyph->pixel_width;
13586 glyph--;
13587 }
13588 }
13589 else if (match_with_avoid_cursor
13590 /* A truncated row may not include PT among its
13591 character positions. Setting the cursor inside the
13592 scroll margin will trigger recalculation of hscroll
13593 in hscroll_window_tree. */
13594 || (row->truncated_on_left_p && pt_old < bpos_min)
13595 || (row->truncated_on_right_p && pt_old > bpos_max)
13596 /* Zero-width characters produce no glyphs. */
13597 || (!string_seen
13598 && !empty_line_p
13599 && (row->reversed_p
13600 ? glyph_after > glyphs_end
13601 : glyph_after < glyphs_end)))
13602 {
13603 cursor = glyph_after;
13604 x = -1;
13605 }
13606 else if (string_seen)
13607 {
13608 int incr = row->reversed_p ? -1 : +1;
13609
13610 /* Need to find the glyph that came out of a string which is
13611 present at point. That glyph is somewhere between
13612 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13613 positioned between POS_BEFORE and POS_AFTER in the
13614 buffer. */
13615 struct glyph *start, *stop;
13616 EMACS_INT pos = pos_before;
13617
13618 x = -1;
13619
13620 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13621 correspond to POS_BEFORE and POS_AFTER, respectively. We
13622 need START and STOP in the order that corresponds to the
13623 row's direction as given by its reversed_p flag. If the
13624 directionality of characters between POS_BEFORE and
13625 POS_AFTER is the opposite of the row's base direction,
13626 these characters will have been reordered for display,
13627 and we need to reverse START and STOP. */
13628 if (!row->reversed_p)
13629 {
13630 start = min (glyph_before, glyph_after);
13631 stop = max (glyph_before, glyph_after);
13632 }
13633 else
13634 {
13635 start = max (glyph_before, glyph_after);
13636 stop = min (glyph_before, glyph_after);
13637 }
13638 for (glyph = start + incr;
13639 row->reversed_p ? glyph > stop : glyph < stop; )
13640 {
13641
13642 /* Any glyphs that come from the buffer are here because
13643 of bidi reordering. Skip them, and only pay
13644 attention to glyphs that came from some string. */
13645 if (STRINGP (glyph->object))
13646 {
13647 Lisp_Object str;
13648 EMACS_INT tem;
13649 /* If the display property covers the newline, we
13650 need to search for it one position farther. */
13651 EMACS_INT lim = pos_after
13652 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13653
13654 string_from_text_prop = 0;
13655 str = glyph->object;
13656 tem = string_buffer_position_lim (str, pos, lim, 0);
13657 if (tem == 0 /* from overlay */
13658 || pos <= tem)
13659 {
13660 /* If the string from which this glyph came is
13661 found in the buffer at point, then we've
13662 found the glyph we've been looking for. If
13663 it comes from an overlay (tem == 0), and it
13664 has the `cursor' property on one of its
13665 glyphs, record that glyph as a candidate for
13666 displaying the cursor. (As in the
13667 unidirectional version, we will display the
13668 cursor on the last candidate we find.) */
13669 if (tem == 0 || tem == pt_old)
13670 {
13671 /* The glyphs from this string could have
13672 been reordered. Find the one with the
13673 smallest string position. Or there could
13674 be a character in the string with the
13675 `cursor' property, which means display
13676 cursor on that character's glyph. */
13677 EMACS_INT strpos = glyph->charpos;
13678
13679 if (tem)
13680 {
13681 cursor = glyph;
13682 string_from_text_prop = 1;
13683 }
13684 for ( ;
13685 (row->reversed_p ? glyph > stop : glyph < stop)
13686 && EQ (glyph->object, str);
13687 glyph += incr)
13688 {
13689 Lisp_Object cprop;
13690 EMACS_INT gpos = glyph->charpos;
13691
13692 cprop = Fget_char_property (make_number (gpos),
13693 Qcursor,
13694 glyph->object);
13695 if (!NILP (cprop))
13696 {
13697 cursor = glyph;
13698 break;
13699 }
13700 if (tem && glyph->charpos < strpos)
13701 {
13702 strpos = glyph->charpos;
13703 cursor = glyph;
13704 }
13705 }
13706
13707 if (tem == pt_old)
13708 goto compute_x;
13709 }
13710 if (tem)
13711 pos = tem + 1; /* don't find previous instances */
13712 }
13713 /* This string is not what we want; skip all of the
13714 glyphs that came from it. */
13715 while ((row->reversed_p ? glyph > stop : glyph < stop)
13716 && EQ (glyph->object, str))
13717 glyph += incr;
13718 }
13719 else
13720 glyph += incr;
13721 }
13722
13723 /* If we reached the end of the line, and END was from a string,
13724 the cursor is not on this line. */
13725 if (cursor == NULL
13726 && (row->reversed_p ? glyph <= end : glyph >= end)
13727 && STRINGP (end->object)
13728 && row->continued_p)
13729 return 0;
13730 }
13731 }
13732
13733 compute_x:
13734 if (cursor != NULL)
13735 glyph = cursor;
13736 if (x < 0)
13737 {
13738 struct glyph *g;
13739
13740 /* Need to compute x that corresponds to GLYPH. */
13741 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13742 {
13743 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13744 abort ();
13745 x += g->pixel_width;
13746 }
13747 }
13748
13749 /* ROW could be part of a continued line, which, under bidi
13750 reordering, might have other rows whose start and end charpos
13751 occlude point. Only set w->cursor if we found a better
13752 approximation to the cursor position than we have from previously
13753 examined candidate rows belonging to the same continued line. */
13754 if (/* we already have a candidate row */
13755 w->cursor.vpos >= 0
13756 /* that candidate is not the row we are processing */
13757 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13758 /* Make sure cursor.vpos specifies a row whose start and end
13759 charpos occlude point, and it is valid candidate for being a
13760 cursor-row. This is because some callers of this function
13761 leave cursor.vpos at the row where the cursor was displayed
13762 during the last redisplay cycle. */
13763 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13764 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13765 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
13766 {
13767 struct glyph *g1 =
13768 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13769
13770 /* Don't consider glyphs that are outside TEXT_AREA. */
13771 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13772 return 0;
13773 /* Keep the candidate whose buffer position is the closest to
13774 point or has the `cursor' property. */
13775 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13776 w->cursor.hpos >= 0
13777 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13778 && ((BUFFERP (g1->object)
13779 && (g1->charpos == pt_old /* an exact match always wins */
13780 || (BUFFERP (glyph->object)
13781 && eabs (g1->charpos - pt_old)
13782 < eabs (glyph->charpos - pt_old))))
13783 /* previous candidate is a glyph from a string that has
13784 a non-nil `cursor' property */
13785 || (STRINGP (g1->object)
13786 && (!NILP (Fget_char_property (make_number (g1->charpos),
13787 Qcursor, g1->object))
13788 /* pevious candidate is from the same display
13789 string as this one, and the display string
13790 came from a text property */
13791 || (EQ (g1->object, glyph->object)
13792 && string_from_text_prop)
13793 /* this candidate is from newline and its
13794 position is not an exact match */
13795 || (INTEGERP (glyph->object)
13796 && glyph->charpos != pt_old)))))
13797 return 0;
13798 /* If this candidate gives an exact match, use that. */
13799 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13800 /* Otherwise, keep the candidate that comes from a row
13801 spanning less buffer positions. This may win when one or
13802 both candidate positions are on glyphs that came from
13803 display strings, for which we cannot compare buffer
13804 positions. */
13805 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13806 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13807 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13808 return 0;
13809 }
13810 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13811 w->cursor.x = x;
13812 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13813 w->cursor.y = row->y + dy;
13814
13815 if (w == XWINDOW (selected_window))
13816 {
13817 if (!row->continued_p
13818 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13819 && row->x == 0)
13820 {
13821 this_line_buffer = XBUFFER (w->buffer);
13822
13823 CHARPOS (this_line_start_pos)
13824 = MATRIX_ROW_START_CHARPOS (row) + delta;
13825 BYTEPOS (this_line_start_pos)
13826 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13827
13828 CHARPOS (this_line_end_pos)
13829 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13830 BYTEPOS (this_line_end_pos)
13831 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13832
13833 this_line_y = w->cursor.y;
13834 this_line_pixel_height = row->height;
13835 this_line_vpos = w->cursor.vpos;
13836 this_line_start_x = row->x;
13837 }
13838 else
13839 CHARPOS (this_line_start_pos) = 0;
13840 }
13841
13842 return 1;
13843 }
13844
13845
13846 /* Run window scroll functions, if any, for WINDOW with new window
13847 start STARTP. Sets the window start of WINDOW to that position.
13848
13849 We assume that the window's buffer is really current. */
13850
13851 static inline struct text_pos
13852 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13853 {
13854 struct window *w = XWINDOW (window);
13855 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13856
13857 if (current_buffer != XBUFFER (w->buffer))
13858 abort ();
13859
13860 if (!NILP (Vwindow_scroll_functions))
13861 {
13862 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13863 make_number (CHARPOS (startp)));
13864 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13865 /* In case the hook functions switch buffers. */
13866 if (current_buffer != XBUFFER (w->buffer))
13867 set_buffer_internal_1 (XBUFFER (w->buffer));
13868 }
13869
13870 return startp;
13871 }
13872
13873
13874 /* Make sure the line containing the cursor is fully visible.
13875 A value of 1 means there is nothing to be done.
13876 (Either the line is fully visible, or it cannot be made so,
13877 or we cannot tell.)
13878
13879 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13880 is higher than window.
13881
13882 A value of 0 means the caller should do scrolling
13883 as if point had gone off the screen. */
13884
13885 static int
13886 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13887 {
13888 struct glyph_matrix *matrix;
13889 struct glyph_row *row;
13890 int window_height;
13891
13892 if (!make_cursor_line_fully_visible_p)
13893 return 1;
13894
13895 /* It's not always possible to find the cursor, e.g, when a window
13896 is full of overlay strings. Don't do anything in that case. */
13897 if (w->cursor.vpos < 0)
13898 return 1;
13899
13900 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13901 row = MATRIX_ROW (matrix, w->cursor.vpos);
13902
13903 /* If the cursor row is not partially visible, there's nothing to do. */
13904 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13905 return 1;
13906
13907 /* If the row the cursor is in is taller than the window's height,
13908 it's not clear what to do, so do nothing. */
13909 window_height = window_box_height (w);
13910 if (row->height >= window_height)
13911 {
13912 if (!force_p || MINI_WINDOW_P (w)
13913 || w->vscroll || w->cursor.vpos == 0)
13914 return 1;
13915 }
13916 return 0;
13917 }
13918
13919
13920 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13921 non-zero means only WINDOW is redisplayed in redisplay_internal.
13922 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13923 in redisplay_window to bring a partially visible line into view in
13924 the case that only the cursor has moved.
13925
13926 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13927 last screen line's vertical height extends past the end of the screen.
13928
13929 Value is
13930
13931 1 if scrolling succeeded
13932
13933 0 if scrolling didn't find point.
13934
13935 -1 if new fonts have been loaded so that we must interrupt
13936 redisplay, adjust glyph matrices, and try again. */
13937
13938 enum
13939 {
13940 SCROLLING_SUCCESS,
13941 SCROLLING_FAILED,
13942 SCROLLING_NEED_LARGER_MATRICES
13943 };
13944
13945 /* If scroll-conservatively is more than this, never recenter.
13946
13947 If you change this, don't forget to update the doc string of
13948 `scroll-conservatively' and the Emacs manual. */
13949 #define SCROLL_LIMIT 100
13950
13951 static int
13952 try_scrolling (Lisp_Object window, int just_this_one_p,
13953 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13954 int temp_scroll_step, int last_line_misfit)
13955 {
13956 struct window *w = XWINDOW (window);
13957 struct frame *f = XFRAME (w->frame);
13958 struct text_pos pos, startp;
13959 struct it it;
13960 int this_scroll_margin, scroll_max, rc, height;
13961 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13962 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13963 Lisp_Object aggressive;
13964 /* We will never try scrolling more than this number of lines. */
13965 int scroll_limit = SCROLL_LIMIT;
13966
13967 #if GLYPH_DEBUG
13968 debug_method_add (w, "try_scrolling");
13969 #endif
13970
13971 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13972
13973 /* Compute scroll margin height in pixels. We scroll when point is
13974 within this distance from the top or bottom of the window. */
13975 if (scroll_margin > 0)
13976 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13977 * FRAME_LINE_HEIGHT (f);
13978 else
13979 this_scroll_margin = 0;
13980
13981 /* Force arg_scroll_conservatively to have a reasonable value, to
13982 avoid scrolling too far away with slow move_it_* functions. Note
13983 that the user can supply scroll-conservatively equal to
13984 `most-positive-fixnum', which can be larger than INT_MAX. */
13985 if (arg_scroll_conservatively > scroll_limit)
13986 {
13987 arg_scroll_conservatively = scroll_limit + 1;
13988 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13989 }
13990 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13991 /* Compute how much we should try to scroll maximally to bring
13992 point into view. */
13993 scroll_max = (max (scroll_step,
13994 max (arg_scroll_conservatively, temp_scroll_step))
13995 * FRAME_LINE_HEIGHT (f));
13996 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13997 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13998 /* We're trying to scroll because of aggressive scrolling but no
13999 scroll_step is set. Choose an arbitrary one. */
14000 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14001 else
14002 scroll_max = 0;
14003
14004 too_near_end:
14005
14006 /* Decide whether to scroll down. */
14007 if (PT > CHARPOS (startp))
14008 {
14009 int scroll_margin_y;
14010
14011 /* Compute the pixel ypos of the scroll margin, then move it to
14012 either that ypos or PT, whichever comes first. */
14013 start_display (&it, w, startp);
14014 scroll_margin_y = it.last_visible_y - this_scroll_margin
14015 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14016 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14017 (MOVE_TO_POS | MOVE_TO_Y));
14018
14019 if (PT > CHARPOS (it.current.pos))
14020 {
14021 int y0 = line_bottom_y (&it);
14022 /* Compute how many pixels below window bottom to stop searching
14023 for PT. This avoids costly search for PT that is far away if
14024 the user limited scrolling by a small number of lines, but
14025 always finds PT if scroll_conservatively is set to a large
14026 number, such as most-positive-fixnum. */
14027 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14028 int y_to_move = it.last_visible_y + slack;
14029
14030 /* Compute the distance from the scroll margin to PT or to
14031 the scroll limit, whichever comes first. This should
14032 include the height of the cursor line, to make that line
14033 fully visible. */
14034 move_it_to (&it, PT, -1, y_to_move,
14035 -1, MOVE_TO_POS | MOVE_TO_Y);
14036 dy = line_bottom_y (&it) - y0;
14037
14038 if (dy > scroll_max)
14039 return SCROLLING_FAILED;
14040
14041 scroll_down_p = 1;
14042 }
14043 }
14044
14045 if (scroll_down_p)
14046 {
14047 /* Point is in or below the bottom scroll margin, so move the
14048 window start down. If scrolling conservatively, move it just
14049 enough down to make point visible. If scroll_step is set,
14050 move it down by scroll_step. */
14051 if (arg_scroll_conservatively)
14052 amount_to_scroll
14053 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14054 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14055 else if (scroll_step || temp_scroll_step)
14056 amount_to_scroll = scroll_max;
14057 else
14058 {
14059 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14060 height = WINDOW_BOX_TEXT_HEIGHT (w);
14061 if (NUMBERP (aggressive))
14062 {
14063 double float_amount = XFLOATINT (aggressive) * height;
14064 amount_to_scroll = float_amount;
14065 if (amount_to_scroll == 0 && float_amount > 0)
14066 amount_to_scroll = 1;
14067 /* Don't let point enter the scroll margin near top of
14068 the window. */
14069 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14070 amount_to_scroll = height - 2*this_scroll_margin + dy;
14071 }
14072 }
14073
14074 if (amount_to_scroll <= 0)
14075 return SCROLLING_FAILED;
14076
14077 start_display (&it, w, startp);
14078 if (arg_scroll_conservatively <= scroll_limit)
14079 move_it_vertically (&it, amount_to_scroll);
14080 else
14081 {
14082 /* Extra precision for users who set scroll-conservatively
14083 to a large number: make sure the amount we scroll
14084 the window start is never less than amount_to_scroll,
14085 which was computed as distance from window bottom to
14086 point. This matters when lines at window top and lines
14087 below window bottom have different height. */
14088 struct it it1;
14089 void *it1data = NULL;
14090 /* We use a temporary it1 because line_bottom_y can modify
14091 its argument, if it moves one line down; see there. */
14092 int start_y;
14093
14094 SAVE_IT (it1, it, it1data);
14095 start_y = line_bottom_y (&it1);
14096 do {
14097 RESTORE_IT (&it, &it, it1data);
14098 move_it_by_lines (&it, 1);
14099 SAVE_IT (it1, it, it1data);
14100 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14101 }
14102
14103 /* If STARTP is unchanged, move it down another screen line. */
14104 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14105 move_it_by_lines (&it, 1);
14106 startp = it.current.pos;
14107 }
14108 else
14109 {
14110 struct text_pos scroll_margin_pos = startp;
14111
14112 /* See if point is inside the scroll margin at the top of the
14113 window. */
14114 if (this_scroll_margin)
14115 {
14116 start_display (&it, w, startp);
14117 move_it_vertically (&it, this_scroll_margin);
14118 scroll_margin_pos = it.current.pos;
14119 }
14120
14121 if (PT < CHARPOS (scroll_margin_pos))
14122 {
14123 /* Point is in the scroll margin at the top of the window or
14124 above what is displayed in the window. */
14125 int y0, y_to_move;
14126
14127 /* Compute the vertical distance from PT to the scroll
14128 margin position. Move as far as scroll_max allows, or
14129 one screenful, or 10 screen lines, whichever is largest.
14130 Give up if distance is greater than scroll_max. */
14131 SET_TEXT_POS (pos, PT, PT_BYTE);
14132 start_display (&it, w, pos);
14133 y0 = it.current_y;
14134 y_to_move = max (it.last_visible_y,
14135 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14136 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14137 y_to_move, -1,
14138 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14139 dy = it.current_y - y0;
14140 if (dy > scroll_max)
14141 return SCROLLING_FAILED;
14142
14143 /* Compute new window start. */
14144 start_display (&it, w, startp);
14145
14146 if (arg_scroll_conservatively)
14147 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14148 max (scroll_step, temp_scroll_step));
14149 else if (scroll_step || temp_scroll_step)
14150 amount_to_scroll = scroll_max;
14151 else
14152 {
14153 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14154 height = WINDOW_BOX_TEXT_HEIGHT (w);
14155 if (NUMBERP (aggressive))
14156 {
14157 double float_amount = XFLOATINT (aggressive) * height;
14158 amount_to_scroll = float_amount;
14159 if (amount_to_scroll == 0 && float_amount > 0)
14160 amount_to_scroll = 1;
14161 amount_to_scroll -=
14162 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14163 /* Don't let point enter the scroll margin near
14164 bottom of the window. */
14165 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14166 amount_to_scroll = height - 2*this_scroll_margin + dy;
14167 }
14168 }
14169
14170 if (amount_to_scroll <= 0)
14171 return SCROLLING_FAILED;
14172
14173 move_it_vertically_backward (&it, amount_to_scroll);
14174 startp = it.current.pos;
14175 }
14176 }
14177
14178 /* Run window scroll functions. */
14179 startp = run_window_scroll_functions (window, startp);
14180
14181 /* Display the window. Give up if new fonts are loaded, or if point
14182 doesn't appear. */
14183 if (!try_window (window, startp, 0))
14184 rc = SCROLLING_NEED_LARGER_MATRICES;
14185 else if (w->cursor.vpos < 0)
14186 {
14187 clear_glyph_matrix (w->desired_matrix);
14188 rc = SCROLLING_FAILED;
14189 }
14190 else
14191 {
14192 /* Maybe forget recorded base line for line number display. */
14193 if (!just_this_one_p
14194 || current_buffer->clip_changed
14195 || BEG_UNCHANGED < CHARPOS (startp))
14196 w->base_line_number = Qnil;
14197
14198 /* If cursor ends up on a partially visible line,
14199 treat that as being off the bottom of the screen. */
14200 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14201 /* It's possible that the cursor is on the first line of the
14202 buffer, which is partially obscured due to a vscroll
14203 (Bug#7537). In that case, avoid looping forever . */
14204 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14205 {
14206 clear_glyph_matrix (w->desired_matrix);
14207 ++extra_scroll_margin_lines;
14208 goto too_near_end;
14209 }
14210 rc = SCROLLING_SUCCESS;
14211 }
14212
14213 return rc;
14214 }
14215
14216
14217 /* Compute a suitable window start for window W if display of W starts
14218 on a continuation line. Value is non-zero if a new window start
14219 was computed.
14220
14221 The new window start will be computed, based on W's width, starting
14222 from the start of the continued line. It is the start of the
14223 screen line with the minimum distance from the old start W->start. */
14224
14225 static int
14226 compute_window_start_on_continuation_line (struct window *w)
14227 {
14228 struct text_pos pos, start_pos;
14229 int window_start_changed_p = 0;
14230
14231 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14232
14233 /* If window start is on a continuation line... Window start may be
14234 < BEGV in case there's invisible text at the start of the
14235 buffer (M-x rmail, for example). */
14236 if (CHARPOS (start_pos) > BEGV
14237 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14238 {
14239 struct it it;
14240 struct glyph_row *row;
14241
14242 /* Handle the case that the window start is out of range. */
14243 if (CHARPOS (start_pos) < BEGV)
14244 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14245 else if (CHARPOS (start_pos) > ZV)
14246 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14247
14248 /* Find the start of the continued line. This should be fast
14249 because scan_buffer is fast (newline cache). */
14250 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14251 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14252 row, DEFAULT_FACE_ID);
14253 reseat_at_previous_visible_line_start (&it);
14254
14255 /* If the line start is "too far" away from the window start,
14256 say it takes too much time to compute a new window start. */
14257 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14258 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14259 {
14260 int min_distance, distance;
14261
14262 /* Move forward by display lines to find the new window
14263 start. If window width was enlarged, the new start can
14264 be expected to be > the old start. If window width was
14265 decreased, the new window start will be < the old start.
14266 So, we're looking for the display line start with the
14267 minimum distance from the old window start. */
14268 pos = it.current.pos;
14269 min_distance = INFINITY;
14270 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14271 distance < min_distance)
14272 {
14273 min_distance = distance;
14274 pos = it.current.pos;
14275 move_it_by_lines (&it, 1);
14276 }
14277
14278 /* Set the window start there. */
14279 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14280 window_start_changed_p = 1;
14281 }
14282 }
14283
14284 return window_start_changed_p;
14285 }
14286
14287
14288 /* Try cursor movement in case text has not changed in window WINDOW,
14289 with window start STARTP. Value is
14290
14291 CURSOR_MOVEMENT_SUCCESS if successful
14292
14293 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14294
14295 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14296 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14297 we want to scroll as if scroll-step were set to 1. See the code.
14298
14299 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14300 which case we have to abort this redisplay, and adjust matrices
14301 first. */
14302
14303 enum
14304 {
14305 CURSOR_MOVEMENT_SUCCESS,
14306 CURSOR_MOVEMENT_CANNOT_BE_USED,
14307 CURSOR_MOVEMENT_MUST_SCROLL,
14308 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14309 };
14310
14311 static int
14312 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14313 {
14314 struct window *w = XWINDOW (window);
14315 struct frame *f = XFRAME (w->frame);
14316 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14317
14318 #if GLYPH_DEBUG
14319 if (inhibit_try_cursor_movement)
14320 return rc;
14321 #endif
14322
14323 /* Handle case where text has not changed, only point, and it has
14324 not moved off the frame. */
14325 if (/* Point may be in this window. */
14326 PT >= CHARPOS (startp)
14327 /* Selective display hasn't changed. */
14328 && !current_buffer->clip_changed
14329 /* Function force-mode-line-update is used to force a thorough
14330 redisplay. It sets either windows_or_buffers_changed or
14331 update_mode_lines. So don't take a shortcut here for these
14332 cases. */
14333 && !update_mode_lines
14334 && !windows_or_buffers_changed
14335 && !cursor_type_changed
14336 /* Can't use this case if highlighting a region. When a
14337 region exists, cursor movement has to do more than just
14338 set the cursor. */
14339 && !(!NILP (Vtransient_mark_mode)
14340 && !NILP (BVAR (current_buffer, mark_active)))
14341 && NILP (w->region_showing)
14342 && NILP (Vshow_trailing_whitespace)
14343 /* Right after splitting windows, last_point may be nil. */
14344 && INTEGERP (w->last_point)
14345 /* This code is not used for mini-buffer for the sake of the case
14346 of redisplaying to replace an echo area message; since in
14347 that case the mini-buffer contents per se are usually
14348 unchanged. This code is of no real use in the mini-buffer
14349 since the handling of this_line_start_pos, etc., in redisplay
14350 handles the same cases. */
14351 && !EQ (window, minibuf_window)
14352 /* When splitting windows or for new windows, it happens that
14353 redisplay is called with a nil window_end_vpos or one being
14354 larger than the window. This should really be fixed in
14355 window.c. I don't have this on my list, now, so we do
14356 approximately the same as the old redisplay code. --gerd. */
14357 && INTEGERP (w->window_end_vpos)
14358 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14359 && (FRAME_WINDOW_P (f)
14360 || !overlay_arrow_in_current_buffer_p ()))
14361 {
14362 int this_scroll_margin, top_scroll_margin;
14363 struct glyph_row *row = NULL;
14364
14365 #if GLYPH_DEBUG
14366 debug_method_add (w, "cursor movement");
14367 #endif
14368
14369 /* Scroll if point within this distance from the top or bottom
14370 of the window. This is a pixel value. */
14371 if (scroll_margin > 0)
14372 {
14373 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14374 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14375 }
14376 else
14377 this_scroll_margin = 0;
14378
14379 top_scroll_margin = this_scroll_margin;
14380 if (WINDOW_WANTS_HEADER_LINE_P (w))
14381 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14382
14383 /* Start with the row the cursor was displayed during the last
14384 not paused redisplay. Give up if that row is not valid. */
14385 if (w->last_cursor.vpos < 0
14386 || w->last_cursor.vpos >= w->current_matrix->nrows)
14387 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14388 else
14389 {
14390 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14391 if (row->mode_line_p)
14392 ++row;
14393 if (!row->enabled_p)
14394 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14395 }
14396
14397 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14398 {
14399 int scroll_p = 0, must_scroll = 0;
14400 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14401
14402 if (PT > XFASTINT (w->last_point))
14403 {
14404 /* Point has moved forward. */
14405 while (MATRIX_ROW_END_CHARPOS (row) < PT
14406 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14407 {
14408 xassert (row->enabled_p);
14409 ++row;
14410 }
14411
14412 /* If the end position of a row equals the start
14413 position of the next row, and PT is at that position,
14414 we would rather display cursor in the next line. */
14415 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14416 && MATRIX_ROW_END_CHARPOS (row) == PT
14417 && row < w->current_matrix->rows
14418 + w->current_matrix->nrows - 1
14419 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14420 && !cursor_row_p (row))
14421 ++row;
14422
14423 /* If within the scroll margin, scroll. Note that
14424 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14425 the next line would be drawn, and that
14426 this_scroll_margin can be zero. */
14427 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14428 || PT > MATRIX_ROW_END_CHARPOS (row)
14429 /* Line is completely visible last line in window
14430 and PT is to be set in the next line. */
14431 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14432 && PT == MATRIX_ROW_END_CHARPOS (row)
14433 && !row->ends_at_zv_p
14434 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14435 scroll_p = 1;
14436 }
14437 else if (PT < XFASTINT (w->last_point))
14438 {
14439 /* Cursor has to be moved backward. Note that PT >=
14440 CHARPOS (startp) because of the outer if-statement. */
14441 while (!row->mode_line_p
14442 && (MATRIX_ROW_START_CHARPOS (row) > PT
14443 || (MATRIX_ROW_START_CHARPOS (row) == PT
14444 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14445 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14446 row > w->current_matrix->rows
14447 && (row-1)->ends_in_newline_from_string_p))))
14448 && (row->y > top_scroll_margin
14449 || CHARPOS (startp) == BEGV))
14450 {
14451 xassert (row->enabled_p);
14452 --row;
14453 }
14454
14455 /* Consider the following case: Window starts at BEGV,
14456 there is invisible, intangible text at BEGV, so that
14457 display starts at some point START > BEGV. It can
14458 happen that we are called with PT somewhere between
14459 BEGV and START. Try to handle that case. */
14460 if (row < w->current_matrix->rows
14461 || row->mode_line_p)
14462 {
14463 row = w->current_matrix->rows;
14464 if (row->mode_line_p)
14465 ++row;
14466 }
14467
14468 /* Due to newlines in overlay strings, we may have to
14469 skip forward over overlay strings. */
14470 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14471 && MATRIX_ROW_END_CHARPOS (row) == PT
14472 && !cursor_row_p (row))
14473 ++row;
14474
14475 /* If within the scroll margin, scroll. */
14476 if (row->y < top_scroll_margin
14477 && CHARPOS (startp) != BEGV)
14478 scroll_p = 1;
14479 }
14480 else
14481 {
14482 /* Cursor did not move. So don't scroll even if cursor line
14483 is partially visible, as it was so before. */
14484 rc = CURSOR_MOVEMENT_SUCCESS;
14485 }
14486
14487 if (PT < MATRIX_ROW_START_CHARPOS (row)
14488 || PT > MATRIX_ROW_END_CHARPOS (row))
14489 {
14490 /* if PT is not in the glyph row, give up. */
14491 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14492 must_scroll = 1;
14493 }
14494 else if (rc != CURSOR_MOVEMENT_SUCCESS
14495 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14496 {
14497 /* If rows are bidi-reordered and point moved, back up
14498 until we find a row that does not belong to a
14499 continuation line. This is because we must consider
14500 all rows of a continued line as candidates for the
14501 new cursor positioning, since row start and end
14502 positions change non-linearly with vertical position
14503 in such rows. */
14504 /* FIXME: Revisit this when glyph ``spilling'' in
14505 continuation lines' rows is implemented for
14506 bidi-reordered rows. */
14507 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14508 {
14509 xassert (row->enabled_p);
14510 --row;
14511 /* If we hit the beginning of the displayed portion
14512 without finding the first row of a continued
14513 line, give up. */
14514 if (row <= w->current_matrix->rows)
14515 {
14516 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14517 break;
14518 }
14519
14520 }
14521 }
14522 if (must_scroll)
14523 ;
14524 else if (rc != CURSOR_MOVEMENT_SUCCESS
14525 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14526 && make_cursor_line_fully_visible_p)
14527 {
14528 if (PT == MATRIX_ROW_END_CHARPOS (row)
14529 && !row->ends_at_zv_p
14530 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14531 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14532 else if (row->height > window_box_height (w))
14533 {
14534 /* If we end up in a partially visible line, let's
14535 make it fully visible, except when it's taller
14536 than the window, in which case we can't do much
14537 about it. */
14538 *scroll_step = 1;
14539 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14540 }
14541 else
14542 {
14543 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14544 if (!cursor_row_fully_visible_p (w, 0, 1))
14545 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14546 else
14547 rc = CURSOR_MOVEMENT_SUCCESS;
14548 }
14549 }
14550 else if (scroll_p)
14551 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14552 else if (rc != CURSOR_MOVEMENT_SUCCESS
14553 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14554 {
14555 /* With bidi-reordered rows, there could be more than
14556 one candidate row whose start and end positions
14557 occlude point. We need to let set_cursor_from_row
14558 find the best candidate. */
14559 /* FIXME: Revisit this when glyph ``spilling'' in
14560 continuation lines' rows is implemented for
14561 bidi-reordered rows. */
14562 int rv = 0;
14563
14564 do
14565 {
14566 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14567 && PT <= MATRIX_ROW_END_CHARPOS (row)
14568 && cursor_row_p (row))
14569 rv |= set_cursor_from_row (w, row, w->current_matrix,
14570 0, 0, 0, 0);
14571 /* As soon as we've found the first suitable row
14572 whose ends_at_zv_p flag is set, we are done. */
14573 if (rv
14574 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14575 {
14576 rc = CURSOR_MOVEMENT_SUCCESS;
14577 break;
14578 }
14579 ++row;
14580 }
14581 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14582 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14583 || (MATRIX_ROW_START_CHARPOS (row) == PT
14584 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14585 /* If we didn't find any candidate rows, or exited the
14586 loop before all the candidates were examined, signal
14587 to the caller that this method failed. */
14588 if (rc != CURSOR_MOVEMENT_SUCCESS
14589 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14590 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14591 else if (rv)
14592 rc = CURSOR_MOVEMENT_SUCCESS;
14593 }
14594 else
14595 {
14596 do
14597 {
14598 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14599 {
14600 rc = CURSOR_MOVEMENT_SUCCESS;
14601 break;
14602 }
14603 ++row;
14604 }
14605 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14606 && MATRIX_ROW_START_CHARPOS (row) == PT
14607 && cursor_row_p (row));
14608 }
14609 }
14610 }
14611
14612 return rc;
14613 }
14614
14615 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14616 static
14617 #endif
14618 void
14619 set_vertical_scroll_bar (struct window *w)
14620 {
14621 EMACS_INT start, end, whole;
14622
14623 /* Calculate the start and end positions for the current window.
14624 At some point, it would be nice to choose between scrollbars
14625 which reflect the whole buffer size, with special markers
14626 indicating narrowing, and scrollbars which reflect only the
14627 visible region.
14628
14629 Note that mini-buffers sometimes aren't displaying any text. */
14630 if (!MINI_WINDOW_P (w)
14631 || (w == XWINDOW (minibuf_window)
14632 && NILP (echo_area_buffer[0])))
14633 {
14634 struct buffer *buf = XBUFFER (w->buffer);
14635 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14636 start = marker_position (w->start) - BUF_BEGV (buf);
14637 /* I don't think this is guaranteed to be right. For the
14638 moment, we'll pretend it is. */
14639 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14640
14641 if (end < start)
14642 end = start;
14643 if (whole < (end - start))
14644 whole = end - start;
14645 }
14646 else
14647 start = end = whole = 0;
14648
14649 /* Indicate what this scroll bar ought to be displaying now. */
14650 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14651 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14652 (w, end - start, whole, start);
14653 }
14654
14655
14656 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14657 selected_window is redisplayed.
14658
14659 We can return without actually redisplaying the window if
14660 fonts_changed_p is nonzero. In that case, redisplay_internal will
14661 retry. */
14662
14663 static void
14664 redisplay_window (Lisp_Object window, int just_this_one_p)
14665 {
14666 struct window *w = XWINDOW (window);
14667 struct frame *f = XFRAME (w->frame);
14668 struct buffer *buffer = XBUFFER (w->buffer);
14669 struct buffer *old = current_buffer;
14670 struct text_pos lpoint, opoint, startp;
14671 int update_mode_line;
14672 int tem;
14673 struct it it;
14674 /* Record it now because it's overwritten. */
14675 int current_matrix_up_to_date_p = 0;
14676 int used_current_matrix_p = 0;
14677 /* This is less strict than current_matrix_up_to_date_p.
14678 It indictes that the buffer contents and narrowing are unchanged. */
14679 int buffer_unchanged_p = 0;
14680 int temp_scroll_step = 0;
14681 int count = SPECPDL_INDEX ();
14682 int rc;
14683 int centering_position = -1;
14684 int last_line_misfit = 0;
14685 EMACS_INT beg_unchanged, end_unchanged;
14686
14687 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14688 opoint = lpoint;
14689
14690 /* W must be a leaf window here. */
14691 xassert (!NILP (w->buffer));
14692 #if GLYPH_DEBUG
14693 *w->desired_matrix->method = 0;
14694 #endif
14695
14696 restart:
14697 reconsider_clip_changes (w, buffer);
14698
14699 /* Has the mode line to be updated? */
14700 update_mode_line = (!NILP (w->update_mode_line)
14701 || update_mode_lines
14702 || buffer->clip_changed
14703 || buffer->prevent_redisplay_optimizations_p);
14704
14705 if (MINI_WINDOW_P (w))
14706 {
14707 if (w == XWINDOW (echo_area_window)
14708 && !NILP (echo_area_buffer[0]))
14709 {
14710 if (update_mode_line)
14711 /* We may have to update a tty frame's menu bar or a
14712 tool-bar. Example `M-x C-h C-h C-g'. */
14713 goto finish_menu_bars;
14714 else
14715 /* We've already displayed the echo area glyphs in this window. */
14716 goto finish_scroll_bars;
14717 }
14718 else if ((w != XWINDOW (minibuf_window)
14719 || minibuf_level == 0)
14720 /* When buffer is nonempty, redisplay window normally. */
14721 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14722 /* Quail displays non-mini buffers in minibuffer window.
14723 In that case, redisplay the window normally. */
14724 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14725 {
14726 /* W is a mini-buffer window, but it's not active, so clear
14727 it. */
14728 int yb = window_text_bottom_y (w);
14729 struct glyph_row *row;
14730 int y;
14731
14732 for (y = 0, row = w->desired_matrix->rows;
14733 y < yb;
14734 y += row->height, ++row)
14735 blank_row (w, row, y);
14736 goto finish_scroll_bars;
14737 }
14738
14739 clear_glyph_matrix (w->desired_matrix);
14740 }
14741
14742 /* Otherwise set up data on this window; select its buffer and point
14743 value. */
14744 /* Really select the buffer, for the sake of buffer-local
14745 variables. */
14746 set_buffer_internal_1 (XBUFFER (w->buffer));
14747
14748 current_matrix_up_to_date_p
14749 = (!NILP (w->window_end_valid)
14750 && !current_buffer->clip_changed
14751 && !current_buffer->prevent_redisplay_optimizations_p
14752 && XFASTINT (w->last_modified) >= MODIFF
14753 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14754
14755 /* Run the window-bottom-change-functions
14756 if it is possible that the text on the screen has changed
14757 (either due to modification of the text, or any other reason). */
14758 if (!current_matrix_up_to_date_p
14759 && !NILP (Vwindow_text_change_functions))
14760 {
14761 safe_run_hooks (Qwindow_text_change_functions);
14762 goto restart;
14763 }
14764
14765 beg_unchanged = BEG_UNCHANGED;
14766 end_unchanged = END_UNCHANGED;
14767
14768 SET_TEXT_POS (opoint, PT, PT_BYTE);
14769
14770 specbind (Qinhibit_point_motion_hooks, Qt);
14771
14772 buffer_unchanged_p
14773 = (!NILP (w->window_end_valid)
14774 && !current_buffer->clip_changed
14775 && XFASTINT (w->last_modified) >= MODIFF
14776 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14777
14778 /* When windows_or_buffers_changed is non-zero, we can't rely on
14779 the window end being valid, so set it to nil there. */
14780 if (windows_or_buffers_changed)
14781 {
14782 /* If window starts on a continuation line, maybe adjust the
14783 window start in case the window's width changed. */
14784 if (XMARKER (w->start)->buffer == current_buffer)
14785 compute_window_start_on_continuation_line (w);
14786
14787 w->window_end_valid = Qnil;
14788 }
14789
14790 /* Some sanity checks. */
14791 CHECK_WINDOW_END (w);
14792 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14793 abort ();
14794 if (BYTEPOS (opoint) < CHARPOS (opoint))
14795 abort ();
14796
14797 /* If %c is in mode line, update it if needed. */
14798 if (!NILP (w->column_number_displayed)
14799 /* This alternative quickly identifies a common case
14800 where no change is needed. */
14801 && !(PT == XFASTINT (w->last_point)
14802 && XFASTINT (w->last_modified) >= MODIFF
14803 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14804 && (XFASTINT (w->column_number_displayed) != current_column ()))
14805 update_mode_line = 1;
14806
14807 /* Count number of windows showing the selected buffer. An indirect
14808 buffer counts as its base buffer. */
14809 if (!just_this_one_p)
14810 {
14811 struct buffer *current_base, *window_base;
14812 current_base = current_buffer;
14813 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14814 if (current_base->base_buffer)
14815 current_base = current_base->base_buffer;
14816 if (window_base->base_buffer)
14817 window_base = window_base->base_buffer;
14818 if (current_base == window_base)
14819 buffer_shared++;
14820 }
14821
14822 /* Point refers normally to the selected window. For any other
14823 window, set up appropriate value. */
14824 if (!EQ (window, selected_window))
14825 {
14826 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14827 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14828 if (new_pt < BEGV)
14829 {
14830 new_pt = BEGV;
14831 new_pt_byte = BEGV_BYTE;
14832 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14833 }
14834 else if (new_pt > (ZV - 1))
14835 {
14836 new_pt = ZV;
14837 new_pt_byte = ZV_BYTE;
14838 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14839 }
14840
14841 /* We don't use SET_PT so that the point-motion hooks don't run. */
14842 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14843 }
14844
14845 /* If any of the character widths specified in the display table
14846 have changed, invalidate the width run cache. It's true that
14847 this may be a bit late to catch such changes, but the rest of
14848 redisplay goes (non-fatally) haywire when the display table is
14849 changed, so why should we worry about doing any better? */
14850 if (current_buffer->width_run_cache)
14851 {
14852 struct Lisp_Char_Table *disptab = buffer_display_table ();
14853
14854 if (! disptab_matches_widthtab (disptab,
14855 XVECTOR (BVAR (current_buffer, width_table))))
14856 {
14857 invalidate_region_cache (current_buffer,
14858 current_buffer->width_run_cache,
14859 BEG, Z);
14860 recompute_width_table (current_buffer, disptab);
14861 }
14862 }
14863
14864 /* If window-start is screwed up, choose a new one. */
14865 if (XMARKER (w->start)->buffer != current_buffer)
14866 goto recenter;
14867
14868 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14869
14870 /* If someone specified a new starting point but did not insist,
14871 check whether it can be used. */
14872 if (!NILP (w->optional_new_start)
14873 && CHARPOS (startp) >= BEGV
14874 && CHARPOS (startp) <= ZV)
14875 {
14876 w->optional_new_start = Qnil;
14877 start_display (&it, w, startp);
14878 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14879 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14880 if (IT_CHARPOS (it) == PT)
14881 w->force_start = Qt;
14882 /* IT may overshoot PT if text at PT is invisible. */
14883 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14884 w->force_start = Qt;
14885 }
14886
14887 force_start:
14888
14889 /* Handle case where place to start displaying has been specified,
14890 unless the specified location is outside the accessible range. */
14891 if (!NILP (w->force_start)
14892 || w->frozen_window_start_p)
14893 {
14894 /* We set this later on if we have to adjust point. */
14895 int new_vpos = -1;
14896
14897 w->force_start = Qnil;
14898 w->vscroll = 0;
14899 w->window_end_valid = Qnil;
14900
14901 /* Forget any recorded base line for line number display. */
14902 if (!buffer_unchanged_p)
14903 w->base_line_number = Qnil;
14904
14905 /* Redisplay the mode line. Select the buffer properly for that.
14906 Also, run the hook window-scroll-functions
14907 because we have scrolled. */
14908 /* Note, we do this after clearing force_start because
14909 if there's an error, it is better to forget about force_start
14910 than to get into an infinite loop calling the hook functions
14911 and having them get more errors. */
14912 if (!update_mode_line
14913 || ! NILP (Vwindow_scroll_functions))
14914 {
14915 update_mode_line = 1;
14916 w->update_mode_line = Qt;
14917 startp = run_window_scroll_functions (window, startp);
14918 }
14919
14920 w->last_modified = make_number (0);
14921 w->last_overlay_modified = make_number (0);
14922 if (CHARPOS (startp) < BEGV)
14923 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14924 else if (CHARPOS (startp) > ZV)
14925 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14926
14927 /* Redisplay, then check if cursor has been set during the
14928 redisplay. Give up if new fonts were loaded. */
14929 /* We used to issue a CHECK_MARGINS argument to try_window here,
14930 but this causes scrolling to fail when point begins inside
14931 the scroll margin (bug#148) -- cyd */
14932 if (!try_window (window, startp, 0))
14933 {
14934 w->force_start = Qt;
14935 clear_glyph_matrix (w->desired_matrix);
14936 goto need_larger_matrices;
14937 }
14938
14939 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14940 {
14941 /* If point does not appear, try to move point so it does
14942 appear. The desired matrix has been built above, so we
14943 can use it here. */
14944 new_vpos = window_box_height (w) / 2;
14945 }
14946
14947 if (!cursor_row_fully_visible_p (w, 0, 0))
14948 {
14949 /* Point does appear, but on a line partly visible at end of window.
14950 Move it back to a fully-visible line. */
14951 new_vpos = window_box_height (w);
14952 }
14953
14954 /* If we need to move point for either of the above reasons,
14955 now actually do it. */
14956 if (new_vpos >= 0)
14957 {
14958 struct glyph_row *row;
14959
14960 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14961 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14962 ++row;
14963
14964 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14965 MATRIX_ROW_START_BYTEPOS (row));
14966
14967 if (w != XWINDOW (selected_window))
14968 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14969 else if (current_buffer == old)
14970 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14971
14972 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14973
14974 /* If we are highlighting the region, then we just changed
14975 the region, so redisplay to show it. */
14976 if (!NILP (Vtransient_mark_mode)
14977 && !NILP (BVAR (current_buffer, mark_active)))
14978 {
14979 clear_glyph_matrix (w->desired_matrix);
14980 if (!try_window (window, startp, 0))
14981 goto need_larger_matrices;
14982 }
14983 }
14984
14985 #if GLYPH_DEBUG
14986 debug_method_add (w, "forced window start");
14987 #endif
14988 goto done;
14989 }
14990
14991 /* Handle case where text has not changed, only point, and it has
14992 not moved off the frame, and we are not retrying after hscroll.
14993 (current_matrix_up_to_date_p is nonzero when retrying.) */
14994 if (current_matrix_up_to_date_p
14995 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14996 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14997 {
14998 switch (rc)
14999 {
15000 case CURSOR_MOVEMENT_SUCCESS:
15001 used_current_matrix_p = 1;
15002 goto done;
15003
15004 case CURSOR_MOVEMENT_MUST_SCROLL:
15005 goto try_to_scroll;
15006
15007 default:
15008 abort ();
15009 }
15010 }
15011 /* If current starting point was originally the beginning of a line
15012 but no longer is, find a new starting point. */
15013 else if (!NILP (w->start_at_line_beg)
15014 && !(CHARPOS (startp) <= BEGV
15015 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15016 {
15017 #if GLYPH_DEBUG
15018 debug_method_add (w, "recenter 1");
15019 #endif
15020 goto recenter;
15021 }
15022
15023 /* Try scrolling with try_window_id. Value is > 0 if update has
15024 been done, it is -1 if we know that the same window start will
15025 not work. It is 0 if unsuccessful for some other reason. */
15026 else if ((tem = try_window_id (w)) != 0)
15027 {
15028 #if GLYPH_DEBUG
15029 debug_method_add (w, "try_window_id %d", tem);
15030 #endif
15031
15032 if (fonts_changed_p)
15033 goto need_larger_matrices;
15034 if (tem > 0)
15035 goto done;
15036
15037 /* Otherwise try_window_id has returned -1 which means that we
15038 don't want the alternative below this comment to execute. */
15039 }
15040 else if (CHARPOS (startp) >= BEGV
15041 && CHARPOS (startp) <= ZV
15042 && PT >= CHARPOS (startp)
15043 && (CHARPOS (startp) < ZV
15044 /* Avoid starting at end of buffer. */
15045 || CHARPOS (startp) == BEGV
15046 || (XFASTINT (w->last_modified) >= MODIFF
15047 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15048 {
15049
15050 /* If first window line is a continuation line, and window start
15051 is inside the modified region, but the first change is before
15052 current window start, we must select a new window start.
15053
15054 However, if this is the result of a down-mouse event (e.g. by
15055 extending the mouse-drag-overlay), we don't want to select a
15056 new window start, since that would change the position under
15057 the mouse, resulting in an unwanted mouse-movement rather
15058 than a simple mouse-click. */
15059 if (NILP (w->start_at_line_beg)
15060 && NILP (do_mouse_tracking)
15061 && CHARPOS (startp) > BEGV
15062 && CHARPOS (startp) > BEG + beg_unchanged
15063 && CHARPOS (startp) <= Z - end_unchanged
15064 /* Even if w->start_at_line_beg is nil, a new window may
15065 start at a line_beg, since that's how set_buffer_window
15066 sets it. So, we need to check the return value of
15067 compute_window_start_on_continuation_line. (See also
15068 bug#197). */
15069 && XMARKER (w->start)->buffer == current_buffer
15070 && compute_window_start_on_continuation_line (w))
15071 {
15072 w->force_start = Qt;
15073 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15074 goto force_start;
15075 }
15076
15077 #if GLYPH_DEBUG
15078 debug_method_add (w, "same window start");
15079 #endif
15080
15081 /* Try to redisplay starting at same place as before.
15082 If point has not moved off frame, accept the results. */
15083 if (!current_matrix_up_to_date_p
15084 /* Don't use try_window_reusing_current_matrix in this case
15085 because a window scroll function can have changed the
15086 buffer. */
15087 || !NILP (Vwindow_scroll_functions)
15088 || MINI_WINDOW_P (w)
15089 || !(used_current_matrix_p
15090 = try_window_reusing_current_matrix (w)))
15091 {
15092 IF_DEBUG (debug_method_add (w, "1"));
15093 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15094 /* -1 means we need to scroll.
15095 0 means we need new matrices, but fonts_changed_p
15096 is set in that case, so we will detect it below. */
15097 goto try_to_scroll;
15098 }
15099
15100 if (fonts_changed_p)
15101 goto need_larger_matrices;
15102
15103 if (w->cursor.vpos >= 0)
15104 {
15105 if (!just_this_one_p
15106 || current_buffer->clip_changed
15107 || BEG_UNCHANGED < CHARPOS (startp))
15108 /* Forget any recorded base line for line number display. */
15109 w->base_line_number = Qnil;
15110
15111 if (!cursor_row_fully_visible_p (w, 1, 0))
15112 {
15113 clear_glyph_matrix (w->desired_matrix);
15114 last_line_misfit = 1;
15115 }
15116 /* Drop through and scroll. */
15117 else
15118 goto done;
15119 }
15120 else
15121 clear_glyph_matrix (w->desired_matrix);
15122 }
15123
15124 try_to_scroll:
15125
15126 w->last_modified = make_number (0);
15127 w->last_overlay_modified = make_number (0);
15128
15129 /* Redisplay the mode line. Select the buffer properly for that. */
15130 if (!update_mode_line)
15131 {
15132 update_mode_line = 1;
15133 w->update_mode_line = Qt;
15134 }
15135
15136 /* Try to scroll by specified few lines. */
15137 if ((scroll_conservatively
15138 || emacs_scroll_step
15139 || temp_scroll_step
15140 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15141 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15142 && CHARPOS (startp) >= BEGV
15143 && CHARPOS (startp) <= ZV)
15144 {
15145 /* The function returns -1 if new fonts were loaded, 1 if
15146 successful, 0 if not successful. */
15147 int ss = try_scrolling (window, just_this_one_p,
15148 scroll_conservatively,
15149 emacs_scroll_step,
15150 temp_scroll_step, last_line_misfit);
15151 switch (ss)
15152 {
15153 case SCROLLING_SUCCESS:
15154 goto done;
15155
15156 case SCROLLING_NEED_LARGER_MATRICES:
15157 goto need_larger_matrices;
15158
15159 case SCROLLING_FAILED:
15160 break;
15161
15162 default:
15163 abort ();
15164 }
15165 }
15166
15167 /* Finally, just choose a place to start which positions point
15168 according to user preferences. */
15169
15170 recenter:
15171
15172 #if GLYPH_DEBUG
15173 debug_method_add (w, "recenter");
15174 #endif
15175
15176 /* w->vscroll = 0; */
15177
15178 /* Forget any previously recorded base line for line number display. */
15179 if (!buffer_unchanged_p)
15180 w->base_line_number = Qnil;
15181
15182 /* Determine the window start relative to point. */
15183 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15184 it.current_y = it.last_visible_y;
15185 if (centering_position < 0)
15186 {
15187 int margin =
15188 scroll_margin > 0
15189 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15190 : 0;
15191 EMACS_INT margin_pos = CHARPOS (startp);
15192 int scrolling_up;
15193 Lisp_Object aggressive;
15194
15195 /* If there is a scroll margin at the top of the window, find
15196 its character position. */
15197 if (margin
15198 /* Cannot call start_display if startp is not in the
15199 accessible region of the buffer. This can happen when we
15200 have just switched to a different buffer and/or changed
15201 its restriction. In that case, startp is initialized to
15202 the character position 1 (BEG) because we did not yet
15203 have chance to display the buffer even once. */
15204 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15205 {
15206 struct it it1;
15207 void *it1data = NULL;
15208
15209 SAVE_IT (it1, it, it1data);
15210 start_display (&it1, w, startp);
15211 move_it_vertically (&it1, margin);
15212 margin_pos = IT_CHARPOS (it1);
15213 RESTORE_IT (&it, &it, it1data);
15214 }
15215 scrolling_up = PT > margin_pos;
15216 aggressive =
15217 scrolling_up
15218 ? BVAR (current_buffer, scroll_up_aggressively)
15219 : BVAR (current_buffer, scroll_down_aggressively);
15220
15221 if (!MINI_WINDOW_P (w)
15222 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15223 {
15224 int pt_offset = 0;
15225
15226 /* Setting scroll-conservatively overrides
15227 scroll-*-aggressively. */
15228 if (!scroll_conservatively && NUMBERP (aggressive))
15229 {
15230 double float_amount = XFLOATINT (aggressive);
15231
15232 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15233 if (pt_offset == 0 && float_amount > 0)
15234 pt_offset = 1;
15235 if (pt_offset)
15236 margin -= 1;
15237 }
15238 /* Compute how much to move the window start backward from
15239 point so that point will be displayed where the user
15240 wants it. */
15241 if (scrolling_up)
15242 {
15243 centering_position = it.last_visible_y;
15244 if (pt_offset)
15245 centering_position -= pt_offset;
15246 centering_position -=
15247 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15248 + WINDOW_HEADER_LINE_HEIGHT (w);
15249 /* Don't let point enter the scroll margin near top of
15250 the window. */
15251 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15252 centering_position = margin * FRAME_LINE_HEIGHT (f);
15253 }
15254 else
15255 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15256 }
15257 else
15258 /* Set the window start half the height of the window backward
15259 from point. */
15260 centering_position = window_box_height (w) / 2;
15261 }
15262 move_it_vertically_backward (&it, centering_position);
15263
15264 xassert (IT_CHARPOS (it) >= BEGV);
15265
15266 /* The function move_it_vertically_backward may move over more
15267 than the specified y-distance. If it->w is small, e.g. a
15268 mini-buffer window, we may end up in front of the window's
15269 display area. Start displaying at the start of the line
15270 containing PT in this case. */
15271 if (it.current_y <= 0)
15272 {
15273 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15274 move_it_vertically_backward (&it, 0);
15275 it.current_y = 0;
15276 }
15277
15278 it.current_x = it.hpos = 0;
15279
15280 /* Set the window start position here explicitly, to avoid an
15281 infinite loop in case the functions in window-scroll-functions
15282 get errors. */
15283 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15284
15285 /* Run scroll hooks. */
15286 startp = run_window_scroll_functions (window, it.current.pos);
15287
15288 /* Redisplay the window. */
15289 if (!current_matrix_up_to_date_p
15290 || windows_or_buffers_changed
15291 || cursor_type_changed
15292 /* Don't use try_window_reusing_current_matrix in this case
15293 because it can have changed the buffer. */
15294 || !NILP (Vwindow_scroll_functions)
15295 || !just_this_one_p
15296 || MINI_WINDOW_P (w)
15297 || !(used_current_matrix_p
15298 = try_window_reusing_current_matrix (w)))
15299 try_window (window, startp, 0);
15300
15301 /* If new fonts have been loaded (due to fontsets), give up. We
15302 have to start a new redisplay since we need to re-adjust glyph
15303 matrices. */
15304 if (fonts_changed_p)
15305 goto need_larger_matrices;
15306
15307 /* If cursor did not appear assume that the middle of the window is
15308 in the first line of the window. Do it again with the next line.
15309 (Imagine a window of height 100, displaying two lines of height
15310 60. Moving back 50 from it->last_visible_y will end in the first
15311 line.) */
15312 if (w->cursor.vpos < 0)
15313 {
15314 if (!NILP (w->window_end_valid)
15315 && PT >= Z - XFASTINT (w->window_end_pos))
15316 {
15317 clear_glyph_matrix (w->desired_matrix);
15318 move_it_by_lines (&it, 1);
15319 try_window (window, it.current.pos, 0);
15320 }
15321 else if (PT < IT_CHARPOS (it))
15322 {
15323 clear_glyph_matrix (w->desired_matrix);
15324 move_it_by_lines (&it, -1);
15325 try_window (window, it.current.pos, 0);
15326 }
15327 else
15328 {
15329 /* Not much we can do about it. */
15330 }
15331 }
15332
15333 /* Consider the following case: Window starts at BEGV, there is
15334 invisible, intangible text at BEGV, so that display starts at
15335 some point START > BEGV. It can happen that we are called with
15336 PT somewhere between BEGV and START. Try to handle that case. */
15337 if (w->cursor.vpos < 0)
15338 {
15339 struct glyph_row *row = w->current_matrix->rows;
15340 if (row->mode_line_p)
15341 ++row;
15342 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15343 }
15344
15345 if (!cursor_row_fully_visible_p (w, 0, 0))
15346 {
15347 /* If vscroll is enabled, disable it and try again. */
15348 if (w->vscroll)
15349 {
15350 w->vscroll = 0;
15351 clear_glyph_matrix (w->desired_matrix);
15352 goto recenter;
15353 }
15354
15355 /* If centering point failed to make the whole line visible,
15356 put point at the top instead. That has to make the whole line
15357 visible, if it can be done. */
15358 if (centering_position == 0)
15359 goto done;
15360
15361 clear_glyph_matrix (w->desired_matrix);
15362 centering_position = 0;
15363 goto recenter;
15364 }
15365
15366 done:
15367
15368 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15369 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15370 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15371 ? Qt : Qnil);
15372
15373 /* Display the mode line, if we must. */
15374 if ((update_mode_line
15375 /* If window not full width, must redo its mode line
15376 if (a) the window to its side is being redone and
15377 (b) we do a frame-based redisplay. This is a consequence
15378 of how inverted lines are drawn in frame-based redisplay. */
15379 || (!just_this_one_p
15380 && !FRAME_WINDOW_P (f)
15381 && !WINDOW_FULL_WIDTH_P (w))
15382 /* Line number to display. */
15383 || INTEGERP (w->base_line_pos)
15384 /* Column number is displayed and different from the one displayed. */
15385 || (!NILP (w->column_number_displayed)
15386 && (XFASTINT (w->column_number_displayed) != current_column ())))
15387 /* This means that the window has a mode line. */
15388 && (WINDOW_WANTS_MODELINE_P (w)
15389 || WINDOW_WANTS_HEADER_LINE_P (w)))
15390 {
15391 display_mode_lines (w);
15392
15393 /* If mode line height has changed, arrange for a thorough
15394 immediate redisplay using the correct mode line height. */
15395 if (WINDOW_WANTS_MODELINE_P (w)
15396 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15397 {
15398 fonts_changed_p = 1;
15399 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15400 = DESIRED_MODE_LINE_HEIGHT (w);
15401 }
15402
15403 /* If header line height has changed, arrange for a thorough
15404 immediate redisplay using the correct header line height. */
15405 if (WINDOW_WANTS_HEADER_LINE_P (w)
15406 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15407 {
15408 fonts_changed_p = 1;
15409 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15410 = DESIRED_HEADER_LINE_HEIGHT (w);
15411 }
15412
15413 if (fonts_changed_p)
15414 goto need_larger_matrices;
15415 }
15416
15417 if (!line_number_displayed
15418 && !BUFFERP (w->base_line_pos))
15419 {
15420 w->base_line_pos = Qnil;
15421 w->base_line_number = Qnil;
15422 }
15423
15424 finish_menu_bars:
15425
15426 /* When we reach a frame's selected window, redo the frame's menu bar. */
15427 if (update_mode_line
15428 && EQ (FRAME_SELECTED_WINDOW (f), window))
15429 {
15430 int redisplay_menu_p = 0;
15431
15432 if (FRAME_WINDOW_P (f))
15433 {
15434 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15435 || defined (HAVE_NS) || defined (USE_GTK)
15436 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15437 #else
15438 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15439 #endif
15440 }
15441 else
15442 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15443
15444 if (redisplay_menu_p)
15445 display_menu_bar (w);
15446
15447 #ifdef HAVE_WINDOW_SYSTEM
15448 if (FRAME_WINDOW_P (f))
15449 {
15450 #if defined (USE_GTK) || defined (HAVE_NS)
15451 if (FRAME_EXTERNAL_TOOL_BAR (f))
15452 redisplay_tool_bar (f);
15453 #else
15454 if (WINDOWP (f->tool_bar_window)
15455 && (FRAME_TOOL_BAR_LINES (f) > 0
15456 || !NILP (Vauto_resize_tool_bars))
15457 && redisplay_tool_bar (f))
15458 ignore_mouse_drag_p = 1;
15459 #endif
15460 }
15461 #endif
15462 }
15463
15464 #ifdef HAVE_WINDOW_SYSTEM
15465 if (FRAME_WINDOW_P (f)
15466 && update_window_fringes (w, (just_this_one_p
15467 || (!used_current_matrix_p && !overlay_arrow_seen)
15468 || w->pseudo_window_p)))
15469 {
15470 update_begin (f);
15471 BLOCK_INPUT;
15472 if (draw_window_fringes (w, 1))
15473 x_draw_vertical_border (w);
15474 UNBLOCK_INPUT;
15475 update_end (f);
15476 }
15477 #endif /* HAVE_WINDOW_SYSTEM */
15478
15479 /* We go to this label, with fonts_changed_p nonzero,
15480 if it is necessary to try again using larger glyph matrices.
15481 We have to redeem the scroll bar even in this case,
15482 because the loop in redisplay_internal expects that. */
15483 need_larger_matrices:
15484 ;
15485 finish_scroll_bars:
15486
15487 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15488 {
15489 /* Set the thumb's position and size. */
15490 set_vertical_scroll_bar (w);
15491
15492 /* Note that we actually used the scroll bar attached to this
15493 window, so it shouldn't be deleted at the end of redisplay. */
15494 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15495 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15496 }
15497
15498 /* Restore current_buffer and value of point in it. The window
15499 update may have changed the buffer, so first make sure `opoint'
15500 is still valid (Bug#6177). */
15501 if (CHARPOS (opoint) < BEGV)
15502 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15503 else if (CHARPOS (opoint) > ZV)
15504 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15505 else
15506 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15507
15508 set_buffer_internal_1 (old);
15509 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15510 shorter. This can be caused by log truncation in *Messages*. */
15511 if (CHARPOS (lpoint) <= ZV)
15512 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15513
15514 unbind_to (count, Qnil);
15515 }
15516
15517
15518 /* Build the complete desired matrix of WINDOW with a window start
15519 buffer position POS.
15520
15521 Value is 1 if successful. It is zero if fonts were loaded during
15522 redisplay which makes re-adjusting glyph matrices necessary, and -1
15523 if point would appear in the scroll margins.
15524 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15525 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15526 set in FLAGS.) */
15527
15528 int
15529 try_window (Lisp_Object window, struct text_pos pos, int flags)
15530 {
15531 struct window *w = XWINDOW (window);
15532 struct it it;
15533 struct glyph_row *last_text_row = NULL;
15534 struct frame *f = XFRAME (w->frame);
15535
15536 /* Make POS the new window start. */
15537 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15538
15539 /* Mark cursor position as unknown. No overlay arrow seen. */
15540 w->cursor.vpos = -1;
15541 overlay_arrow_seen = 0;
15542
15543 /* Initialize iterator and info to start at POS. */
15544 start_display (&it, w, pos);
15545
15546 /* Display all lines of W. */
15547 while (it.current_y < it.last_visible_y)
15548 {
15549 if (display_line (&it))
15550 last_text_row = it.glyph_row - 1;
15551 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15552 return 0;
15553 }
15554
15555 /* Don't let the cursor end in the scroll margins. */
15556 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15557 && !MINI_WINDOW_P (w))
15558 {
15559 int this_scroll_margin;
15560
15561 if (scroll_margin > 0)
15562 {
15563 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15564 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15565 }
15566 else
15567 this_scroll_margin = 0;
15568
15569 if ((w->cursor.y >= 0 /* not vscrolled */
15570 && w->cursor.y < this_scroll_margin
15571 && CHARPOS (pos) > BEGV
15572 && IT_CHARPOS (it) < ZV)
15573 /* rms: considering make_cursor_line_fully_visible_p here
15574 seems to give wrong results. We don't want to recenter
15575 when the last line is partly visible, we want to allow
15576 that case to be handled in the usual way. */
15577 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15578 {
15579 w->cursor.vpos = -1;
15580 clear_glyph_matrix (w->desired_matrix);
15581 return -1;
15582 }
15583 }
15584
15585 /* If bottom moved off end of frame, change mode line percentage. */
15586 if (XFASTINT (w->window_end_pos) <= 0
15587 && Z != IT_CHARPOS (it))
15588 w->update_mode_line = Qt;
15589
15590 /* Set window_end_pos to the offset of the last character displayed
15591 on the window from the end of current_buffer. Set
15592 window_end_vpos to its row number. */
15593 if (last_text_row)
15594 {
15595 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15596 w->window_end_bytepos
15597 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15598 w->window_end_pos
15599 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15600 w->window_end_vpos
15601 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15602 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15603 ->displays_text_p);
15604 }
15605 else
15606 {
15607 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15608 w->window_end_pos = make_number (Z - ZV);
15609 w->window_end_vpos = make_number (0);
15610 }
15611
15612 /* But that is not valid info until redisplay finishes. */
15613 w->window_end_valid = Qnil;
15614 return 1;
15615 }
15616
15617
15618 \f
15619 /************************************************************************
15620 Window redisplay reusing current matrix when buffer has not changed
15621 ************************************************************************/
15622
15623 /* Try redisplay of window W showing an unchanged buffer with a
15624 different window start than the last time it was displayed by
15625 reusing its current matrix. Value is non-zero if successful.
15626 W->start is the new window start. */
15627
15628 static int
15629 try_window_reusing_current_matrix (struct window *w)
15630 {
15631 struct frame *f = XFRAME (w->frame);
15632 struct glyph_row *bottom_row;
15633 struct it it;
15634 struct run run;
15635 struct text_pos start, new_start;
15636 int nrows_scrolled, i;
15637 struct glyph_row *last_text_row;
15638 struct glyph_row *last_reused_text_row;
15639 struct glyph_row *start_row;
15640 int start_vpos, min_y, max_y;
15641
15642 #if GLYPH_DEBUG
15643 if (inhibit_try_window_reusing)
15644 return 0;
15645 #endif
15646
15647 if (/* This function doesn't handle terminal frames. */
15648 !FRAME_WINDOW_P (f)
15649 /* Don't try to reuse the display if windows have been split
15650 or such. */
15651 || windows_or_buffers_changed
15652 || cursor_type_changed)
15653 return 0;
15654
15655 /* Can't do this if region may have changed. */
15656 if ((!NILP (Vtransient_mark_mode)
15657 && !NILP (BVAR (current_buffer, mark_active)))
15658 || !NILP (w->region_showing)
15659 || !NILP (Vshow_trailing_whitespace))
15660 return 0;
15661
15662 /* If top-line visibility has changed, give up. */
15663 if (WINDOW_WANTS_HEADER_LINE_P (w)
15664 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15665 return 0;
15666
15667 /* Give up if old or new display is scrolled vertically. We could
15668 make this function handle this, but right now it doesn't. */
15669 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15670 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15671 return 0;
15672
15673 /* The variable new_start now holds the new window start. The old
15674 start `start' can be determined from the current matrix. */
15675 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15676 start = start_row->minpos;
15677 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15678
15679 /* Clear the desired matrix for the display below. */
15680 clear_glyph_matrix (w->desired_matrix);
15681
15682 if (CHARPOS (new_start) <= CHARPOS (start))
15683 {
15684 /* Don't use this method if the display starts with an ellipsis
15685 displayed for invisible text. It's not easy to handle that case
15686 below, and it's certainly not worth the effort since this is
15687 not a frequent case. */
15688 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15689 return 0;
15690
15691 IF_DEBUG (debug_method_add (w, "twu1"));
15692
15693 /* Display up to a row that can be reused. The variable
15694 last_text_row is set to the last row displayed that displays
15695 text. Note that it.vpos == 0 if or if not there is a
15696 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15697 start_display (&it, w, new_start);
15698 w->cursor.vpos = -1;
15699 last_text_row = last_reused_text_row = NULL;
15700
15701 while (it.current_y < it.last_visible_y
15702 && !fonts_changed_p)
15703 {
15704 /* If we have reached into the characters in the START row,
15705 that means the line boundaries have changed. So we
15706 can't start copying with the row START. Maybe it will
15707 work to start copying with the following row. */
15708 while (IT_CHARPOS (it) > CHARPOS (start))
15709 {
15710 /* Advance to the next row as the "start". */
15711 start_row++;
15712 start = start_row->minpos;
15713 /* If there are no more rows to try, or just one, give up. */
15714 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15715 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15716 || CHARPOS (start) == ZV)
15717 {
15718 clear_glyph_matrix (w->desired_matrix);
15719 return 0;
15720 }
15721
15722 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15723 }
15724 /* If we have reached alignment,
15725 we can copy the rest of the rows. */
15726 if (IT_CHARPOS (it) == CHARPOS (start))
15727 break;
15728
15729 if (display_line (&it))
15730 last_text_row = it.glyph_row - 1;
15731 }
15732
15733 /* A value of current_y < last_visible_y means that we stopped
15734 at the previous window start, which in turn means that we
15735 have at least one reusable row. */
15736 if (it.current_y < it.last_visible_y)
15737 {
15738 struct glyph_row *row;
15739
15740 /* IT.vpos always starts from 0; it counts text lines. */
15741 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15742
15743 /* Find PT if not already found in the lines displayed. */
15744 if (w->cursor.vpos < 0)
15745 {
15746 int dy = it.current_y - start_row->y;
15747
15748 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15749 row = row_containing_pos (w, PT, row, NULL, dy);
15750 if (row)
15751 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15752 dy, nrows_scrolled);
15753 else
15754 {
15755 clear_glyph_matrix (w->desired_matrix);
15756 return 0;
15757 }
15758 }
15759
15760 /* Scroll the display. Do it before the current matrix is
15761 changed. The problem here is that update has not yet
15762 run, i.e. part of the current matrix is not up to date.
15763 scroll_run_hook will clear the cursor, and use the
15764 current matrix to get the height of the row the cursor is
15765 in. */
15766 run.current_y = start_row->y;
15767 run.desired_y = it.current_y;
15768 run.height = it.last_visible_y - it.current_y;
15769
15770 if (run.height > 0 && run.current_y != run.desired_y)
15771 {
15772 update_begin (f);
15773 FRAME_RIF (f)->update_window_begin_hook (w);
15774 FRAME_RIF (f)->clear_window_mouse_face (w);
15775 FRAME_RIF (f)->scroll_run_hook (w, &run);
15776 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15777 update_end (f);
15778 }
15779
15780 /* Shift current matrix down by nrows_scrolled lines. */
15781 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15782 rotate_matrix (w->current_matrix,
15783 start_vpos,
15784 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15785 nrows_scrolled);
15786
15787 /* Disable lines that must be updated. */
15788 for (i = 0; i < nrows_scrolled; ++i)
15789 (start_row + i)->enabled_p = 0;
15790
15791 /* Re-compute Y positions. */
15792 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15793 max_y = it.last_visible_y;
15794 for (row = start_row + nrows_scrolled;
15795 row < bottom_row;
15796 ++row)
15797 {
15798 row->y = it.current_y;
15799 row->visible_height = row->height;
15800
15801 if (row->y < min_y)
15802 row->visible_height -= min_y - row->y;
15803 if (row->y + row->height > max_y)
15804 row->visible_height -= row->y + row->height - max_y;
15805 if (row->fringe_bitmap_periodic_p)
15806 row->redraw_fringe_bitmaps_p = 1;
15807
15808 it.current_y += row->height;
15809
15810 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15811 last_reused_text_row = row;
15812 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15813 break;
15814 }
15815
15816 /* Disable lines in the current matrix which are now
15817 below the window. */
15818 for (++row; row < bottom_row; ++row)
15819 row->enabled_p = row->mode_line_p = 0;
15820 }
15821
15822 /* Update window_end_pos etc.; last_reused_text_row is the last
15823 reused row from the current matrix containing text, if any.
15824 The value of last_text_row is the last displayed line
15825 containing text. */
15826 if (last_reused_text_row)
15827 {
15828 w->window_end_bytepos
15829 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15830 w->window_end_pos
15831 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15832 w->window_end_vpos
15833 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15834 w->current_matrix));
15835 }
15836 else if (last_text_row)
15837 {
15838 w->window_end_bytepos
15839 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15840 w->window_end_pos
15841 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15842 w->window_end_vpos
15843 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15844 }
15845 else
15846 {
15847 /* This window must be completely empty. */
15848 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15849 w->window_end_pos = make_number (Z - ZV);
15850 w->window_end_vpos = make_number (0);
15851 }
15852 w->window_end_valid = Qnil;
15853
15854 /* Update hint: don't try scrolling again in update_window. */
15855 w->desired_matrix->no_scrolling_p = 1;
15856
15857 #if GLYPH_DEBUG
15858 debug_method_add (w, "try_window_reusing_current_matrix 1");
15859 #endif
15860 return 1;
15861 }
15862 else if (CHARPOS (new_start) > CHARPOS (start))
15863 {
15864 struct glyph_row *pt_row, *row;
15865 struct glyph_row *first_reusable_row;
15866 struct glyph_row *first_row_to_display;
15867 int dy;
15868 int yb = window_text_bottom_y (w);
15869
15870 /* Find the row starting at new_start, if there is one. Don't
15871 reuse a partially visible line at the end. */
15872 first_reusable_row = start_row;
15873 while (first_reusable_row->enabled_p
15874 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15875 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15876 < CHARPOS (new_start)))
15877 ++first_reusable_row;
15878
15879 /* Give up if there is no row to reuse. */
15880 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15881 || !first_reusable_row->enabled_p
15882 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15883 != CHARPOS (new_start)))
15884 return 0;
15885
15886 /* We can reuse fully visible rows beginning with
15887 first_reusable_row to the end of the window. Set
15888 first_row_to_display to the first row that cannot be reused.
15889 Set pt_row to the row containing point, if there is any. */
15890 pt_row = NULL;
15891 for (first_row_to_display = first_reusable_row;
15892 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15893 ++first_row_to_display)
15894 {
15895 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15896 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15897 pt_row = first_row_to_display;
15898 }
15899
15900 /* Start displaying at the start of first_row_to_display. */
15901 xassert (first_row_to_display->y < yb);
15902 init_to_row_start (&it, w, first_row_to_display);
15903
15904 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15905 - start_vpos);
15906 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15907 - nrows_scrolled);
15908 it.current_y = (first_row_to_display->y - first_reusable_row->y
15909 + WINDOW_HEADER_LINE_HEIGHT (w));
15910
15911 /* Display lines beginning with first_row_to_display in the
15912 desired matrix. Set last_text_row to the last row displayed
15913 that displays text. */
15914 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15915 if (pt_row == NULL)
15916 w->cursor.vpos = -1;
15917 last_text_row = NULL;
15918 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15919 if (display_line (&it))
15920 last_text_row = it.glyph_row - 1;
15921
15922 /* If point is in a reused row, adjust y and vpos of the cursor
15923 position. */
15924 if (pt_row)
15925 {
15926 w->cursor.vpos -= nrows_scrolled;
15927 w->cursor.y -= first_reusable_row->y - start_row->y;
15928 }
15929
15930 /* Give up if point isn't in a row displayed or reused. (This
15931 also handles the case where w->cursor.vpos < nrows_scrolled
15932 after the calls to display_line, which can happen with scroll
15933 margins. See bug#1295.) */
15934 if (w->cursor.vpos < 0)
15935 {
15936 clear_glyph_matrix (w->desired_matrix);
15937 return 0;
15938 }
15939
15940 /* Scroll the display. */
15941 run.current_y = first_reusable_row->y;
15942 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15943 run.height = it.last_visible_y - run.current_y;
15944 dy = run.current_y - run.desired_y;
15945
15946 if (run.height)
15947 {
15948 update_begin (f);
15949 FRAME_RIF (f)->update_window_begin_hook (w);
15950 FRAME_RIF (f)->clear_window_mouse_face (w);
15951 FRAME_RIF (f)->scroll_run_hook (w, &run);
15952 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15953 update_end (f);
15954 }
15955
15956 /* Adjust Y positions of reused rows. */
15957 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15958 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15959 max_y = it.last_visible_y;
15960 for (row = first_reusable_row; row < first_row_to_display; ++row)
15961 {
15962 row->y -= dy;
15963 row->visible_height = row->height;
15964 if (row->y < min_y)
15965 row->visible_height -= min_y - row->y;
15966 if (row->y + row->height > max_y)
15967 row->visible_height -= row->y + row->height - max_y;
15968 if (row->fringe_bitmap_periodic_p)
15969 row->redraw_fringe_bitmaps_p = 1;
15970 }
15971
15972 /* Scroll the current matrix. */
15973 xassert (nrows_scrolled > 0);
15974 rotate_matrix (w->current_matrix,
15975 start_vpos,
15976 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15977 -nrows_scrolled);
15978
15979 /* Disable rows not reused. */
15980 for (row -= nrows_scrolled; row < bottom_row; ++row)
15981 row->enabled_p = 0;
15982
15983 /* Point may have moved to a different line, so we cannot assume that
15984 the previous cursor position is valid; locate the correct row. */
15985 if (pt_row)
15986 {
15987 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15988 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15989 row++)
15990 {
15991 w->cursor.vpos++;
15992 w->cursor.y = row->y;
15993 }
15994 if (row < bottom_row)
15995 {
15996 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15997 struct glyph *end = glyph + row->used[TEXT_AREA];
15998
15999 /* Can't use this optimization with bidi-reordered glyph
16000 rows, unless cursor is already at point. */
16001 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16002 {
16003 if (!(w->cursor.hpos >= 0
16004 && w->cursor.hpos < row->used[TEXT_AREA]
16005 && BUFFERP (glyph->object)
16006 && glyph->charpos == PT))
16007 return 0;
16008 }
16009 else
16010 for (; glyph < end
16011 && (!BUFFERP (glyph->object)
16012 || glyph->charpos < PT);
16013 glyph++)
16014 {
16015 w->cursor.hpos++;
16016 w->cursor.x += glyph->pixel_width;
16017 }
16018 }
16019 }
16020
16021 /* Adjust window end. A null value of last_text_row means that
16022 the window end is in reused rows which in turn means that
16023 only its vpos can have changed. */
16024 if (last_text_row)
16025 {
16026 w->window_end_bytepos
16027 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16028 w->window_end_pos
16029 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16030 w->window_end_vpos
16031 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16032 }
16033 else
16034 {
16035 w->window_end_vpos
16036 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16037 }
16038
16039 w->window_end_valid = Qnil;
16040 w->desired_matrix->no_scrolling_p = 1;
16041
16042 #if GLYPH_DEBUG
16043 debug_method_add (w, "try_window_reusing_current_matrix 2");
16044 #endif
16045 return 1;
16046 }
16047
16048 return 0;
16049 }
16050
16051
16052 \f
16053 /************************************************************************
16054 Window redisplay reusing current matrix when buffer has changed
16055 ************************************************************************/
16056
16057 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16058 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16059 EMACS_INT *, EMACS_INT *);
16060 static struct glyph_row *
16061 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16062 struct glyph_row *);
16063
16064
16065 /* Return the last row in MATRIX displaying text. If row START is
16066 non-null, start searching with that row. IT gives the dimensions
16067 of the display. Value is null if matrix is empty; otherwise it is
16068 a pointer to the row found. */
16069
16070 static struct glyph_row *
16071 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16072 struct glyph_row *start)
16073 {
16074 struct glyph_row *row, *row_found;
16075
16076 /* Set row_found to the last row in IT->w's current matrix
16077 displaying text. The loop looks funny but think of partially
16078 visible lines. */
16079 row_found = NULL;
16080 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16081 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16082 {
16083 xassert (row->enabled_p);
16084 row_found = row;
16085 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16086 break;
16087 ++row;
16088 }
16089
16090 return row_found;
16091 }
16092
16093
16094 /* Return the last row in the current matrix of W that is not affected
16095 by changes at the start of current_buffer that occurred since W's
16096 current matrix was built. Value is null if no such row exists.
16097
16098 BEG_UNCHANGED us the number of characters unchanged at the start of
16099 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16100 first changed character in current_buffer. Characters at positions <
16101 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16102 when the current matrix was built. */
16103
16104 static struct glyph_row *
16105 find_last_unchanged_at_beg_row (struct window *w)
16106 {
16107 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16108 struct glyph_row *row;
16109 struct glyph_row *row_found = NULL;
16110 int yb = window_text_bottom_y (w);
16111
16112 /* Find the last row displaying unchanged text. */
16113 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16114 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16115 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16116 ++row)
16117 {
16118 if (/* If row ends before first_changed_pos, it is unchanged,
16119 except in some case. */
16120 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16121 /* When row ends in ZV and we write at ZV it is not
16122 unchanged. */
16123 && !row->ends_at_zv_p
16124 /* When first_changed_pos is the end of a continued line,
16125 row is not unchanged because it may be no longer
16126 continued. */
16127 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16128 && (row->continued_p
16129 || row->exact_window_width_line_p)))
16130 row_found = row;
16131
16132 /* Stop if last visible row. */
16133 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16134 break;
16135 }
16136
16137 return row_found;
16138 }
16139
16140
16141 /* Find the first glyph row in the current matrix of W that is not
16142 affected by changes at the end of current_buffer since the
16143 time W's current matrix was built.
16144
16145 Return in *DELTA the number of chars by which buffer positions in
16146 unchanged text at the end of current_buffer must be adjusted.
16147
16148 Return in *DELTA_BYTES the corresponding number of bytes.
16149
16150 Value is null if no such row exists, i.e. all rows are affected by
16151 changes. */
16152
16153 static struct glyph_row *
16154 find_first_unchanged_at_end_row (struct window *w,
16155 EMACS_INT *delta, EMACS_INT *delta_bytes)
16156 {
16157 struct glyph_row *row;
16158 struct glyph_row *row_found = NULL;
16159
16160 *delta = *delta_bytes = 0;
16161
16162 /* Display must not have been paused, otherwise the current matrix
16163 is not up to date. */
16164 eassert (!NILP (w->window_end_valid));
16165
16166 /* A value of window_end_pos >= END_UNCHANGED means that the window
16167 end is in the range of changed text. If so, there is no
16168 unchanged row at the end of W's current matrix. */
16169 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16170 return NULL;
16171
16172 /* Set row to the last row in W's current matrix displaying text. */
16173 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16174
16175 /* If matrix is entirely empty, no unchanged row exists. */
16176 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16177 {
16178 /* The value of row is the last glyph row in the matrix having a
16179 meaningful buffer position in it. The end position of row
16180 corresponds to window_end_pos. This allows us to translate
16181 buffer positions in the current matrix to current buffer
16182 positions for characters not in changed text. */
16183 EMACS_INT Z_old =
16184 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16185 EMACS_INT Z_BYTE_old =
16186 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16187 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16188 struct glyph_row *first_text_row
16189 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16190
16191 *delta = Z - Z_old;
16192 *delta_bytes = Z_BYTE - Z_BYTE_old;
16193
16194 /* Set last_unchanged_pos to the buffer position of the last
16195 character in the buffer that has not been changed. Z is the
16196 index + 1 of the last character in current_buffer, i.e. by
16197 subtracting END_UNCHANGED we get the index of the last
16198 unchanged character, and we have to add BEG to get its buffer
16199 position. */
16200 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16201 last_unchanged_pos_old = last_unchanged_pos - *delta;
16202
16203 /* Search backward from ROW for a row displaying a line that
16204 starts at a minimum position >= last_unchanged_pos_old. */
16205 for (; row > first_text_row; --row)
16206 {
16207 /* This used to abort, but it can happen.
16208 It is ok to just stop the search instead here. KFS. */
16209 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16210 break;
16211
16212 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16213 row_found = row;
16214 }
16215 }
16216
16217 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16218
16219 return row_found;
16220 }
16221
16222
16223 /* Make sure that glyph rows in the current matrix of window W
16224 reference the same glyph memory as corresponding rows in the
16225 frame's frame matrix. This function is called after scrolling W's
16226 current matrix on a terminal frame in try_window_id and
16227 try_window_reusing_current_matrix. */
16228
16229 static void
16230 sync_frame_with_window_matrix_rows (struct window *w)
16231 {
16232 struct frame *f = XFRAME (w->frame);
16233 struct glyph_row *window_row, *window_row_end, *frame_row;
16234
16235 /* Preconditions: W must be a leaf window and full-width. Its frame
16236 must have a frame matrix. */
16237 xassert (NILP (w->hchild) && NILP (w->vchild));
16238 xassert (WINDOW_FULL_WIDTH_P (w));
16239 xassert (!FRAME_WINDOW_P (f));
16240
16241 /* If W is a full-width window, glyph pointers in W's current matrix
16242 have, by definition, to be the same as glyph pointers in the
16243 corresponding frame matrix. Note that frame matrices have no
16244 marginal areas (see build_frame_matrix). */
16245 window_row = w->current_matrix->rows;
16246 window_row_end = window_row + w->current_matrix->nrows;
16247 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16248 while (window_row < window_row_end)
16249 {
16250 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16251 struct glyph *end = window_row->glyphs[LAST_AREA];
16252
16253 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16254 frame_row->glyphs[TEXT_AREA] = start;
16255 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16256 frame_row->glyphs[LAST_AREA] = end;
16257
16258 /* Disable frame rows whose corresponding window rows have
16259 been disabled in try_window_id. */
16260 if (!window_row->enabled_p)
16261 frame_row->enabled_p = 0;
16262
16263 ++window_row, ++frame_row;
16264 }
16265 }
16266
16267
16268 /* Find the glyph row in window W containing CHARPOS. Consider all
16269 rows between START and END (not inclusive). END null means search
16270 all rows to the end of the display area of W. Value is the row
16271 containing CHARPOS or null. */
16272
16273 struct glyph_row *
16274 row_containing_pos (struct window *w, EMACS_INT charpos,
16275 struct glyph_row *start, struct glyph_row *end, int dy)
16276 {
16277 struct glyph_row *row = start;
16278 struct glyph_row *best_row = NULL;
16279 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16280 int last_y;
16281
16282 /* If we happen to start on a header-line, skip that. */
16283 if (row->mode_line_p)
16284 ++row;
16285
16286 if ((end && row >= end) || !row->enabled_p)
16287 return NULL;
16288
16289 last_y = window_text_bottom_y (w) - dy;
16290
16291 while (1)
16292 {
16293 /* Give up if we have gone too far. */
16294 if (end && row >= end)
16295 return NULL;
16296 /* This formerly returned if they were equal.
16297 I think that both quantities are of a "last plus one" type;
16298 if so, when they are equal, the row is within the screen. -- rms. */
16299 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16300 return NULL;
16301
16302 /* If it is in this row, return this row. */
16303 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16304 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16305 /* The end position of a row equals the start
16306 position of the next row. If CHARPOS is there, we
16307 would rather display it in the next line, except
16308 when this line ends in ZV. */
16309 && !row->ends_at_zv_p
16310 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16311 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16312 {
16313 struct glyph *g;
16314
16315 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16316 || (!best_row && !row->continued_p))
16317 return row;
16318 /* In bidi-reordered rows, there could be several rows
16319 occluding point, all of them belonging to the same
16320 continued line. We need to find the row which fits
16321 CHARPOS the best. */
16322 for (g = row->glyphs[TEXT_AREA];
16323 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16324 g++)
16325 {
16326 if (!STRINGP (g->object))
16327 {
16328 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16329 {
16330 mindif = eabs (g->charpos - charpos);
16331 best_row = row;
16332 /* Exact match always wins. */
16333 if (mindif == 0)
16334 return best_row;
16335 }
16336 }
16337 }
16338 }
16339 else if (best_row && !row->continued_p)
16340 return best_row;
16341 ++row;
16342 }
16343 }
16344
16345
16346 /* Try to redisplay window W by reusing its existing display. W's
16347 current matrix must be up to date when this function is called,
16348 i.e. window_end_valid must not be nil.
16349
16350 Value is
16351
16352 1 if display has been updated
16353 0 if otherwise unsuccessful
16354 -1 if redisplay with same window start is known not to succeed
16355
16356 The following steps are performed:
16357
16358 1. Find the last row in the current matrix of W that is not
16359 affected by changes at the start of current_buffer. If no such row
16360 is found, give up.
16361
16362 2. Find the first row in W's current matrix that is not affected by
16363 changes at the end of current_buffer. Maybe there is no such row.
16364
16365 3. Display lines beginning with the row + 1 found in step 1 to the
16366 row found in step 2 or, if step 2 didn't find a row, to the end of
16367 the window.
16368
16369 4. If cursor is not known to appear on the window, give up.
16370
16371 5. If display stopped at the row found in step 2, scroll the
16372 display and current matrix as needed.
16373
16374 6. Maybe display some lines at the end of W, if we must. This can
16375 happen under various circumstances, like a partially visible line
16376 becoming fully visible, or because newly displayed lines are displayed
16377 in smaller font sizes.
16378
16379 7. Update W's window end information. */
16380
16381 static int
16382 try_window_id (struct window *w)
16383 {
16384 struct frame *f = XFRAME (w->frame);
16385 struct glyph_matrix *current_matrix = w->current_matrix;
16386 struct glyph_matrix *desired_matrix = w->desired_matrix;
16387 struct glyph_row *last_unchanged_at_beg_row;
16388 struct glyph_row *first_unchanged_at_end_row;
16389 struct glyph_row *row;
16390 struct glyph_row *bottom_row;
16391 int bottom_vpos;
16392 struct it it;
16393 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16394 int dvpos, dy;
16395 struct text_pos start_pos;
16396 struct run run;
16397 int first_unchanged_at_end_vpos = 0;
16398 struct glyph_row *last_text_row, *last_text_row_at_end;
16399 struct text_pos start;
16400 EMACS_INT first_changed_charpos, last_changed_charpos;
16401
16402 #if GLYPH_DEBUG
16403 if (inhibit_try_window_id)
16404 return 0;
16405 #endif
16406
16407 /* This is handy for debugging. */
16408 #if 0
16409 #define GIVE_UP(X) \
16410 do { \
16411 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16412 return 0; \
16413 } while (0)
16414 #else
16415 #define GIVE_UP(X) return 0
16416 #endif
16417
16418 SET_TEXT_POS_FROM_MARKER (start, w->start);
16419
16420 /* Don't use this for mini-windows because these can show
16421 messages and mini-buffers, and we don't handle that here. */
16422 if (MINI_WINDOW_P (w))
16423 GIVE_UP (1);
16424
16425 /* This flag is used to prevent redisplay optimizations. */
16426 if (windows_or_buffers_changed || cursor_type_changed)
16427 GIVE_UP (2);
16428
16429 /* Verify that narrowing has not changed.
16430 Also verify that we were not told to prevent redisplay optimizations.
16431 It would be nice to further
16432 reduce the number of cases where this prevents try_window_id. */
16433 if (current_buffer->clip_changed
16434 || current_buffer->prevent_redisplay_optimizations_p)
16435 GIVE_UP (3);
16436
16437 /* Window must either use window-based redisplay or be full width. */
16438 if (!FRAME_WINDOW_P (f)
16439 && (!FRAME_LINE_INS_DEL_OK (f)
16440 || !WINDOW_FULL_WIDTH_P (w)))
16441 GIVE_UP (4);
16442
16443 /* Give up if point is known NOT to appear in W. */
16444 if (PT < CHARPOS (start))
16445 GIVE_UP (5);
16446
16447 /* Another way to prevent redisplay optimizations. */
16448 if (XFASTINT (w->last_modified) == 0)
16449 GIVE_UP (6);
16450
16451 /* Verify that window is not hscrolled. */
16452 if (XFASTINT (w->hscroll) != 0)
16453 GIVE_UP (7);
16454
16455 /* Verify that display wasn't paused. */
16456 if (NILP (w->window_end_valid))
16457 GIVE_UP (8);
16458
16459 /* Can't use this if highlighting a region because a cursor movement
16460 will do more than just set the cursor. */
16461 if (!NILP (Vtransient_mark_mode)
16462 && !NILP (BVAR (current_buffer, mark_active)))
16463 GIVE_UP (9);
16464
16465 /* Likewise if highlighting trailing whitespace. */
16466 if (!NILP (Vshow_trailing_whitespace))
16467 GIVE_UP (11);
16468
16469 /* Likewise if showing a region. */
16470 if (!NILP (w->region_showing))
16471 GIVE_UP (10);
16472
16473 /* Can't use this if overlay arrow position and/or string have
16474 changed. */
16475 if (overlay_arrows_changed_p ())
16476 GIVE_UP (12);
16477
16478 /* When word-wrap is on, adding a space to the first word of a
16479 wrapped line can change the wrap position, altering the line
16480 above it. It might be worthwhile to handle this more
16481 intelligently, but for now just redisplay from scratch. */
16482 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16483 GIVE_UP (21);
16484
16485 /* Under bidi reordering, adding or deleting a character in the
16486 beginning of a paragraph, before the first strong directional
16487 character, can change the base direction of the paragraph (unless
16488 the buffer specifies a fixed paragraph direction), which will
16489 require to redisplay the whole paragraph. It might be worthwhile
16490 to find the paragraph limits and widen the range of redisplayed
16491 lines to that, but for now just give up this optimization and
16492 redisplay from scratch. */
16493 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16494 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16495 GIVE_UP (22);
16496
16497 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16498 only if buffer has really changed. The reason is that the gap is
16499 initially at Z for freshly visited files. The code below would
16500 set end_unchanged to 0 in that case. */
16501 if (MODIFF > SAVE_MODIFF
16502 /* This seems to happen sometimes after saving a buffer. */
16503 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16504 {
16505 if (GPT - BEG < BEG_UNCHANGED)
16506 BEG_UNCHANGED = GPT - BEG;
16507 if (Z - GPT < END_UNCHANGED)
16508 END_UNCHANGED = Z - GPT;
16509 }
16510
16511 /* The position of the first and last character that has been changed. */
16512 first_changed_charpos = BEG + BEG_UNCHANGED;
16513 last_changed_charpos = Z - END_UNCHANGED;
16514
16515 /* If window starts after a line end, and the last change is in
16516 front of that newline, then changes don't affect the display.
16517 This case happens with stealth-fontification. Note that although
16518 the display is unchanged, glyph positions in the matrix have to
16519 be adjusted, of course. */
16520 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16521 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16522 && ((last_changed_charpos < CHARPOS (start)
16523 && CHARPOS (start) == BEGV)
16524 || (last_changed_charpos < CHARPOS (start) - 1
16525 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16526 {
16527 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16528 struct glyph_row *r0;
16529
16530 /* Compute how many chars/bytes have been added to or removed
16531 from the buffer. */
16532 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16533 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16534 Z_delta = Z - Z_old;
16535 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16536
16537 /* Give up if PT is not in the window. Note that it already has
16538 been checked at the start of try_window_id that PT is not in
16539 front of the window start. */
16540 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16541 GIVE_UP (13);
16542
16543 /* If window start is unchanged, we can reuse the whole matrix
16544 as is, after adjusting glyph positions. No need to compute
16545 the window end again, since its offset from Z hasn't changed. */
16546 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16547 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16548 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16549 /* PT must not be in a partially visible line. */
16550 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16551 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16552 {
16553 /* Adjust positions in the glyph matrix. */
16554 if (Z_delta || Z_delta_bytes)
16555 {
16556 struct glyph_row *r1
16557 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16558 increment_matrix_positions (w->current_matrix,
16559 MATRIX_ROW_VPOS (r0, current_matrix),
16560 MATRIX_ROW_VPOS (r1, current_matrix),
16561 Z_delta, Z_delta_bytes);
16562 }
16563
16564 /* Set the cursor. */
16565 row = row_containing_pos (w, PT, r0, NULL, 0);
16566 if (row)
16567 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16568 else
16569 abort ();
16570 return 1;
16571 }
16572 }
16573
16574 /* Handle the case that changes are all below what is displayed in
16575 the window, and that PT is in the window. This shortcut cannot
16576 be taken if ZV is visible in the window, and text has been added
16577 there that is visible in the window. */
16578 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16579 /* ZV is not visible in the window, or there are no
16580 changes at ZV, actually. */
16581 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16582 || first_changed_charpos == last_changed_charpos))
16583 {
16584 struct glyph_row *r0;
16585
16586 /* Give up if PT is not in the window. Note that it already has
16587 been checked at the start of try_window_id that PT is not in
16588 front of the window start. */
16589 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16590 GIVE_UP (14);
16591
16592 /* If window start is unchanged, we can reuse the whole matrix
16593 as is, without changing glyph positions since no text has
16594 been added/removed in front of the window end. */
16595 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16596 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16597 /* PT must not be in a partially visible line. */
16598 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16599 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16600 {
16601 /* We have to compute the window end anew since text
16602 could have been added/removed after it. */
16603 w->window_end_pos
16604 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16605 w->window_end_bytepos
16606 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16607
16608 /* Set the cursor. */
16609 row = row_containing_pos (w, PT, r0, NULL, 0);
16610 if (row)
16611 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16612 else
16613 abort ();
16614 return 2;
16615 }
16616 }
16617
16618 /* Give up if window start is in the changed area.
16619
16620 The condition used to read
16621
16622 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16623
16624 but why that was tested escapes me at the moment. */
16625 if (CHARPOS (start) >= first_changed_charpos
16626 && CHARPOS (start) <= last_changed_charpos)
16627 GIVE_UP (15);
16628
16629 /* Check that window start agrees with the start of the first glyph
16630 row in its current matrix. Check this after we know the window
16631 start is not in changed text, otherwise positions would not be
16632 comparable. */
16633 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16634 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16635 GIVE_UP (16);
16636
16637 /* Give up if the window ends in strings. Overlay strings
16638 at the end are difficult to handle, so don't try. */
16639 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16640 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16641 GIVE_UP (20);
16642
16643 /* Compute the position at which we have to start displaying new
16644 lines. Some of the lines at the top of the window might be
16645 reusable because they are not displaying changed text. Find the
16646 last row in W's current matrix not affected by changes at the
16647 start of current_buffer. Value is null if changes start in the
16648 first line of window. */
16649 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16650 if (last_unchanged_at_beg_row)
16651 {
16652 /* Avoid starting to display in the moddle of a character, a TAB
16653 for instance. This is easier than to set up the iterator
16654 exactly, and it's not a frequent case, so the additional
16655 effort wouldn't really pay off. */
16656 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16657 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16658 && last_unchanged_at_beg_row > w->current_matrix->rows)
16659 --last_unchanged_at_beg_row;
16660
16661 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16662 GIVE_UP (17);
16663
16664 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16665 GIVE_UP (18);
16666 start_pos = it.current.pos;
16667
16668 /* Start displaying new lines in the desired matrix at the same
16669 vpos we would use in the current matrix, i.e. below
16670 last_unchanged_at_beg_row. */
16671 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16672 current_matrix);
16673 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16674 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16675
16676 xassert (it.hpos == 0 && it.current_x == 0);
16677 }
16678 else
16679 {
16680 /* There are no reusable lines at the start of the window.
16681 Start displaying in the first text line. */
16682 start_display (&it, w, start);
16683 it.vpos = it.first_vpos;
16684 start_pos = it.current.pos;
16685 }
16686
16687 /* Find the first row that is not affected by changes at the end of
16688 the buffer. Value will be null if there is no unchanged row, in
16689 which case we must redisplay to the end of the window. delta
16690 will be set to the value by which buffer positions beginning with
16691 first_unchanged_at_end_row have to be adjusted due to text
16692 changes. */
16693 first_unchanged_at_end_row
16694 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16695 IF_DEBUG (debug_delta = delta);
16696 IF_DEBUG (debug_delta_bytes = delta_bytes);
16697
16698 /* Set stop_pos to the buffer position up to which we will have to
16699 display new lines. If first_unchanged_at_end_row != NULL, this
16700 is the buffer position of the start of the line displayed in that
16701 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16702 that we don't stop at a buffer position. */
16703 stop_pos = 0;
16704 if (first_unchanged_at_end_row)
16705 {
16706 xassert (last_unchanged_at_beg_row == NULL
16707 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16708
16709 /* If this is a continuation line, move forward to the next one
16710 that isn't. Changes in lines above affect this line.
16711 Caution: this may move first_unchanged_at_end_row to a row
16712 not displaying text. */
16713 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16714 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16715 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16716 < it.last_visible_y))
16717 ++first_unchanged_at_end_row;
16718
16719 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16720 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16721 >= it.last_visible_y))
16722 first_unchanged_at_end_row = NULL;
16723 else
16724 {
16725 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16726 + delta);
16727 first_unchanged_at_end_vpos
16728 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16729 xassert (stop_pos >= Z - END_UNCHANGED);
16730 }
16731 }
16732 else if (last_unchanged_at_beg_row == NULL)
16733 GIVE_UP (19);
16734
16735
16736 #if GLYPH_DEBUG
16737
16738 /* Either there is no unchanged row at the end, or the one we have
16739 now displays text. This is a necessary condition for the window
16740 end pos calculation at the end of this function. */
16741 xassert (first_unchanged_at_end_row == NULL
16742 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16743
16744 debug_last_unchanged_at_beg_vpos
16745 = (last_unchanged_at_beg_row
16746 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16747 : -1);
16748 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16749
16750 #endif /* GLYPH_DEBUG != 0 */
16751
16752
16753 /* Display new lines. Set last_text_row to the last new line
16754 displayed which has text on it, i.e. might end up as being the
16755 line where the window_end_vpos is. */
16756 w->cursor.vpos = -1;
16757 last_text_row = NULL;
16758 overlay_arrow_seen = 0;
16759 while (it.current_y < it.last_visible_y
16760 && !fonts_changed_p
16761 && (first_unchanged_at_end_row == NULL
16762 || IT_CHARPOS (it) < stop_pos))
16763 {
16764 if (display_line (&it))
16765 last_text_row = it.glyph_row - 1;
16766 }
16767
16768 if (fonts_changed_p)
16769 return -1;
16770
16771
16772 /* Compute differences in buffer positions, y-positions etc. for
16773 lines reused at the bottom of the window. Compute what we can
16774 scroll. */
16775 if (first_unchanged_at_end_row
16776 /* No lines reused because we displayed everything up to the
16777 bottom of the window. */
16778 && it.current_y < it.last_visible_y)
16779 {
16780 dvpos = (it.vpos
16781 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16782 current_matrix));
16783 dy = it.current_y - first_unchanged_at_end_row->y;
16784 run.current_y = first_unchanged_at_end_row->y;
16785 run.desired_y = run.current_y + dy;
16786 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16787 }
16788 else
16789 {
16790 delta = delta_bytes = dvpos = dy
16791 = run.current_y = run.desired_y = run.height = 0;
16792 first_unchanged_at_end_row = NULL;
16793 }
16794 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16795
16796
16797 /* Find the cursor if not already found. We have to decide whether
16798 PT will appear on this window (it sometimes doesn't, but this is
16799 not a very frequent case.) This decision has to be made before
16800 the current matrix is altered. A value of cursor.vpos < 0 means
16801 that PT is either in one of the lines beginning at
16802 first_unchanged_at_end_row or below the window. Don't care for
16803 lines that might be displayed later at the window end; as
16804 mentioned, this is not a frequent case. */
16805 if (w->cursor.vpos < 0)
16806 {
16807 /* Cursor in unchanged rows at the top? */
16808 if (PT < CHARPOS (start_pos)
16809 && last_unchanged_at_beg_row)
16810 {
16811 row = row_containing_pos (w, PT,
16812 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16813 last_unchanged_at_beg_row + 1, 0);
16814 if (row)
16815 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16816 }
16817
16818 /* Start from first_unchanged_at_end_row looking for PT. */
16819 else if (first_unchanged_at_end_row)
16820 {
16821 row = row_containing_pos (w, PT - delta,
16822 first_unchanged_at_end_row, NULL, 0);
16823 if (row)
16824 set_cursor_from_row (w, row, w->current_matrix, delta,
16825 delta_bytes, dy, dvpos);
16826 }
16827
16828 /* Give up if cursor was not found. */
16829 if (w->cursor.vpos < 0)
16830 {
16831 clear_glyph_matrix (w->desired_matrix);
16832 return -1;
16833 }
16834 }
16835
16836 /* Don't let the cursor end in the scroll margins. */
16837 {
16838 int this_scroll_margin, cursor_height;
16839
16840 this_scroll_margin = max (0, scroll_margin);
16841 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16842 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16843 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16844
16845 if ((w->cursor.y < this_scroll_margin
16846 && CHARPOS (start) > BEGV)
16847 /* Old redisplay didn't take scroll margin into account at the bottom,
16848 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16849 || (w->cursor.y + (make_cursor_line_fully_visible_p
16850 ? cursor_height + this_scroll_margin
16851 : 1)) > it.last_visible_y)
16852 {
16853 w->cursor.vpos = -1;
16854 clear_glyph_matrix (w->desired_matrix);
16855 return -1;
16856 }
16857 }
16858
16859 /* Scroll the display. Do it before changing the current matrix so
16860 that xterm.c doesn't get confused about where the cursor glyph is
16861 found. */
16862 if (dy && run.height)
16863 {
16864 update_begin (f);
16865
16866 if (FRAME_WINDOW_P (f))
16867 {
16868 FRAME_RIF (f)->update_window_begin_hook (w);
16869 FRAME_RIF (f)->clear_window_mouse_face (w);
16870 FRAME_RIF (f)->scroll_run_hook (w, &run);
16871 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16872 }
16873 else
16874 {
16875 /* Terminal frame. In this case, dvpos gives the number of
16876 lines to scroll by; dvpos < 0 means scroll up. */
16877 int from_vpos
16878 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16879 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16880 int end = (WINDOW_TOP_EDGE_LINE (w)
16881 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16882 + window_internal_height (w));
16883
16884 #if defined (HAVE_GPM) || defined (MSDOS)
16885 x_clear_window_mouse_face (w);
16886 #endif
16887 /* Perform the operation on the screen. */
16888 if (dvpos > 0)
16889 {
16890 /* Scroll last_unchanged_at_beg_row to the end of the
16891 window down dvpos lines. */
16892 set_terminal_window (f, end);
16893
16894 /* On dumb terminals delete dvpos lines at the end
16895 before inserting dvpos empty lines. */
16896 if (!FRAME_SCROLL_REGION_OK (f))
16897 ins_del_lines (f, end - dvpos, -dvpos);
16898
16899 /* Insert dvpos empty lines in front of
16900 last_unchanged_at_beg_row. */
16901 ins_del_lines (f, from, dvpos);
16902 }
16903 else if (dvpos < 0)
16904 {
16905 /* Scroll up last_unchanged_at_beg_vpos to the end of
16906 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16907 set_terminal_window (f, end);
16908
16909 /* Delete dvpos lines in front of
16910 last_unchanged_at_beg_vpos. ins_del_lines will set
16911 the cursor to the given vpos and emit |dvpos| delete
16912 line sequences. */
16913 ins_del_lines (f, from + dvpos, dvpos);
16914
16915 /* On a dumb terminal insert dvpos empty lines at the
16916 end. */
16917 if (!FRAME_SCROLL_REGION_OK (f))
16918 ins_del_lines (f, end + dvpos, -dvpos);
16919 }
16920
16921 set_terminal_window (f, 0);
16922 }
16923
16924 update_end (f);
16925 }
16926
16927 /* Shift reused rows of the current matrix to the right position.
16928 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16929 text. */
16930 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16931 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16932 if (dvpos < 0)
16933 {
16934 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16935 bottom_vpos, dvpos);
16936 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16937 bottom_vpos, 0);
16938 }
16939 else if (dvpos > 0)
16940 {
16941 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16942 bottom_vpos, dvpos);
16943 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16944 first_unchanged_at_end_vpos + dvpos, 0);
16945 }
16946
16947 /* For frame-based redisplay, make sure that current frame and window
16948 matrix are in sync with respect to glyph memory. */
16949 if (!FRAME_WINDOW_P (f))
16950 sync_frame_with_window_matrix_rows (w);
16951
16952 /* Adjust buffer positions in reused rows. */
16953 if (delta || delta_bytes)
16954 increment_matrix_positions (current_matrix,
16955 first_unchanged_at_end_vpos + dvpos,
16956 bottom_vpos, delta, delta_bytes);
16957
16958 /* Adjust Y positions. */
16959 if (dy)
16960 shift_glyph_matrix (w, current_matrix,
16961 first_unchanged_at_end_vpos + dvpos,
16962 bottom_vpos, dy);
16963
16964 if (first_unchanged_at_end_row)
16965 {
16966 first_unchanged_at_end_row += dvpos;
16967 if (first_unchanged_at_end_row->y >= it.last_visible_y
16968 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16969 first_unchanged_at_end_row = NULL;
16970 }
16971
16972 /* If scrolling up, there may be some lines to display at the end of
16973 the window. */
16974 last_text_row_at_end = NULL;
16975 if (dy < 0)
16976 {
16977 /* Scrolling up can leave for example a partially visible line
16978 at the end of the window to be redisplayed. */
16979 /* Set last_row to the glyph row in the current matrix where the
16980 window end line is found. It has been moved up or down in
16981 the matrix by dvpos. */
16982 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16983 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16984
16985 /* If last_row is the window end line, it should display text. */
16986 xassert (last_row->displays_text_p);
16987
16988 /* If window end line was partially visible before, begin
16989 displaying at that line. Otherwise begin displaying with the
16990 line following it. */
16991 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16992 {
16993 init_to_row_start (&it, w, last_row);
16994 it.vpos = last_vpos;
16995 it.current_y = last_row->y;
16996 }
16997 else
16998 {
16999 init_to_row_end (&it, w, last_row);
17000 it.vpos = 1 + last_vpos;
17001 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17002 ++last_row;
17003 }
17004
17005 /* We may start in a continuation line. If so, we have to
17006 get the right continuation_lines_width and current_x. */
17007 it.continuation_lines_width = last_row->continuation_lines_width;
17008 it.hpos = it.current_x = 0;
17009
17010 /* Display the rest of the lines at the window end. */
17011 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17012 while (it.current_y < it.last_visible_y
17013 && !fonts_changed_p)
17014 {
17015 /* Is it always sure that the display agrees with lines in
17016 the current matrix? I don't think so, so we mark rows
17017 displayed invalid in the current matrix by setting their
17018 enabled_p flag to zero. */
17019 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17020 if (display_line (&it))
17021 last_text_row_at_end = it.glyph_row - 1;
17022 }
17023 }
17024
17025 /* Update window_end_pos and window_end_vpos. */
17026 if (first_unchanged_at_end_row
17027 && !last_text_row_at_end)
17028 {
17029 /* Window end line if one of the preserved rows from the current
17030 matrix. Set row to the last row displaying text in current
17031 matrix starting at first_unchanged_at_end_row, after
17032 scrolling. */
17033 xassert (first_unchanged_at_end_row->displays_text_p);
17034 row = find_last_row_displaying_text (w->current_matrix, &it,
17035 first_unchanged_at_end_row);
17036 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17037
17038 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17039 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17040 w->window_end_vpos
17041 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17042 xassert (w->window_end_bytepos >= 0);
17043 IF_DEBUG (debug_method_add (w, "A"));
17044 }
17045 else if (last_text_row_at_end)
17046 {
17047 w->window_end_pos
17048 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17049 w->window_end_bytepos
17050 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17051 w->window_end_vpos
17052 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17053 xassert (w->window_end_bytepos >= 0);
17054 IF_DEBUG (debug_method_add (w, "B"));
17055 }
17056 else if (last_text_row)
17057 {
17058 /* We have displayed either to the end of the window or at the
17059 end of the window, i.e. the last row with text is to be found
17060 in the desired matrix. */
17061 w->window_end_pos
17062 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17063 w->window_end_bytepos
17064 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17065 w->window_end_vpos
17066 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17067 xassert (w->window_end_bytepos >= 0);
17068 }
17069 else if (first_unchanged_at_end_row == NULL
17070 && last_text_row == NULL
17071 && last_text_row_at_end == NULL)
17072 {
17073 /* Displayed to end of window, but no line containing text was
17074 displayed. Lines were deleted at the end of the window. */
17075 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17076 int vpos = XFASTINT (w->window_end_vpos);
17077 struct glyph_row *current_row = current_matrix->rows + vpos;
17078 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17079
17080 for (row = NULL;
17081 row == NULL && vpos >= first_vpos;
17082 --vpos, --current_row, --desired_row)
17083 {
17084 if (desired_row->enabled_p)
17085 {
17086 if (desired_row->displays_text_p)
17087 row = desired_row;
17088 }
17089 else if (current_row->displays_text_p)
17090 row = current_row;
17091 }
17092
17093 xassert (row != NULL);
17094 w->window_end_vpos = make_number (vpos + 1);
17095 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17096 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17097 xassert (w->window_end_bytepos >= 0);
17098 IF_DEBUG (debug_method_add (w, "C"));
17099 }
17100 else
17101 abort ();
17102
17103 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17104 debug_end_vpos = XFASTINT (w->window_end_vpos));
17105
17106 /* Record that display has not been completed. */
17107 w->window_end_valid = Qnil;
17108 w->desired_matrix->no_scrolling_p = 1;
17109 return 3;
17110
17111 #undef GIVE_UP
17112 }
17113
17114
17115 \f
17116 /***********************************************************************
17117 More debugging support
17118 ***********************************************************************/
17119
17120 #if GLYPH_DEBUG
17121
17122 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17123 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17124 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17125
17126
17127 /* Dump the contents of glyph matrix MATRIX on stderr.
17128
17129 GLYPHS 0 means don't show glyph contents.
17130 GLYPHS 1 means show glyphs in short form
17131 GLYPHS > 1 means show glyphs in long form. */
17132
17133 void
17134 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17135 {
17136 int i;
17137 for (i = 0; i < matrix->nrows; ++i)
17138 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17139 }
17140
17141
17142 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17143 the glyph row and area where the glyph comes from. */
17144
17145 void
17146 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17147 {
17148 if (glyph->type == CHAR_GLYPH)
17149 {
17150 fprintf (stderr,
17151 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17152 glyph - row->glyphs[TEXT_AREA],
17153 'C',
17154 glyph->charpos,
17155 (BUFFERP (glyph->object)
17156 ? 'B'
17157 : (STRINGP (glyph->object)
17158 ? 'S'
17159 : '-')),
17160 glyph->pixel_width,
17161 glyph->u.ch,
17162 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17163 ? glyph->u.ch
17164 : '.'),
17165 glyph->face_id,
17166 glyph->left_box_line_p,
17167 glyph->right_box_line_p);
17168 }
17169 else if (glyph->type == STRETCH_GLYPH)
17170 {
17171 fprintf (stderr,
17172 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17173 glyph - row->glyphs[TEXT_AREA],
17174 'S',
17175 glyph->charpos,
17176 (BUFFERP (glyph->object)
17177 ? 'B'
17178 : (STRINGP (glyph->object)
17179 ? 'S'
17180 : '-')),
17181 glyph->pixel_width,
17182 0,
17183 '.',
17184 glyph->face_id,
17185 glyph->left_box_line_p,
17186 glyph->right_box_line_p);
17187 }
17188 else if (glyph->type == IMAGE_GLYPH)
17189 {
17190 fprintf (stderr,
17191 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17192 glyph - row->glyphs[TEXT_AREA],
17193 'I',
17194 glyph->charpos,
17195 (BUFFERP (glyph->object)
17196 ? 'B'
17197 : (STRINGP (glyph->object)
17198 ? 'S'
17199 : '-')),
17200 glyph->pixel_width,
17201 glyph->u.img_id,
17202 '.',
17203 glyph->face_id,
17204 glyph->left_box_line_p,
17205 glyph->right_box_line_p);
17206 }
17207 else if (glyph->type == COMPOSITE_GLYPH)
17208 {
17209 fprintf (stderr,
17210 " %5td %4c %6"pI"d %c %3d 0x%05x",
17211 glyph - row->glyphs[TEXT_AREA],
17212 '+',
17213 glyph->charpos,
17214 (BUFFERP (glyph->object)
17215 ? 'B'
17216 : (STRINGP (glyph->object)
17217 ? 'S'
17218 : '-')),
17219 glyph->pixel_width,
17220 glyph->u.cmp.id);
17221 if (glyph->u.cmp.automatic)
17222 fprintf (stderr,
17223 "[%d-%d]",
17224 glyph->slice.cmp.from, glyph->slice.cmp.to);
17225 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17226 glyph->face_id,
17227 glyph->left_box_line_p,
17228 glyph->right_box_line_p);
17229 }
17230 }
17231
17232
17233 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17234 GLYPHS 0 means don't show glyph contents.
17235 GLYPHS 1 means show glyphs in short form
17236 GLYPHS > 1 means show glyphs in long form. */
17237
17238 void
17239 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17240 {
17241 if (glyphs != 1)
17242 {
17243 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17244 fprintf (stderr, "======================================================================\n");
17245
17246 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17247 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17248 vpos,
17249 MATRIX_ROW_START_CHARPOS (row),
17250 MATRIX_ROW_END_CHARPOS (row),
17251 row->used[TEXT_AREA],
17252 row->contains_overlapping_glyphs_p,
17253 row->enabled_p,
17254 row->truncated_on_left_p,
17255 row->truncated_on_right_p,
17256 row->continued_p,
17257 MATRIX_ROW_CONTINUATION_LINE_P (row),
17258 row->displays_text_p,
17259 row->ends_at_zv_p,
17260 row->fill_line_p,
17261 row->ends_in_middle_of_char_p,
17262 row->starts_in_middle_of_char_p,
17263 row->mouse_face_p,
17264 row->x,
17265 row->y,
17266 row->pixel_width,
17267 row->height,
17268 row->visible_height,
17269 row->ascent,
17270 row->phys_ascent);
17271 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17272 row->end.overlay_string_index,
17273 row->continuation_lines_width);
17274 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17275 CHARPOS (row->start.string_pos),
17276 CHARPOS (row->end.string_pos));
17277 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17278 row->end.dpvec_index);
17279 }
17280
17281 if (glyphs > 1)
17282 {
17283 int area;
17284
17285 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17286 {
17287 struct glyph *glyph = row->glyphs[area];
17288 struct glyph *glyph_end = glyph + row->used[area];
17289
17290 /* Glyph for a line end in text. */
17291 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17292 ++glyph_end;
17293
17294 if (glyph < glyph_end)
17295 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17296
17297 for (; glyph < glyph_end; ++glyph)
17298 dump_glyph (row, glyph, area);
17299 }
17300 }
17301 else if (glyphs == 1)
17302 {
17303 int area;
17304
17305 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17306 {
17307 char *s = (char *) alloca (row->used[area] + 1);
17308 int i;
17309
17310 for (i = 0; i < row->used[area]; ++i)
17311 {
17312 struct glyph *glyph = row->glyphs[area] + i;
17313 if (glyph->type == CHAR_GLYPH
17314 && glyph->u.ch < 0x80
17315 && glyph->u.ch >= ' ')
17316 s[i] = glyph->u.ch;
17317 else
17318 s[i] = '.';
17319 }
17320
17321 s[i] = '\0';
17322 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17323 }
17324 }
17325 }
17326
17327
17328 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17329 Sdump_glyph_matrix, 0, 1, "p",
17330 doc: /* Dump the current matrix of the selected window to stderr.
17331 Shows contents of glyph row structures. With non-nil
17332 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17333 glyphs in short form, otherwise show glyphs in long form. */)
17334 (Lisp_Object glyphs)
17335 {
17336 struct window *w = XWINDOW (selected_window);
17337 struct buffer *buffer = XBUFFER (w->buffer);
17338
17339 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17340 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17341 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17342 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17343 fprintf (stderr, "=============================================\n");
17344 dump_glyph_matrix (w->current_matrix,
17345 NILP (glyphs) ? 0 : XINT (glyphs));
17346 return Qnil;
17347 }
17348
17349
17350 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17351 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17352 (void)
17353 {
17354 struct frame *f = XFRAME (selected_frame);
17355 dump_glyph_matrix (f->current_matrix, 1);
17356 return Qnil;
17357 }
17358
17359
17360 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17361 doc: /* Dump glyph row ROW to stderr.
17362 GLYPH 0 means don't dump glyphs.
17363 GLYPH 1 means dump glyphs in short form.
17364 GLYPH > 1 or omitted means dump glyphs in long form. */)
17365 (Lisp_Object row, Lisp_Object glyphs)
17366 {
17367 struct glyph_matrix *matrix;
17368 int vpos;
17369
17370 CHECK_NUMBER (row);
17371 matrix = XWINDOW (selected_window)->current_matrix;
17372 vpos = XINT (row);
17373 if (vpos >= 0 && vpos < matrix->nrows)
17374 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17375 vpos,
17376 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17377 return Qnil;
17378 }
17379
17380
17381 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17382 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17383 GLYPH 0 means don't dump glyphs.
17384 GLYPH 1 means dump glyphs in short form.
17385 GLYPH > 1 or omitted means dump glyphs in long form. */)
17386 (Lisp_Object row, Lisp_Object glyphs)
17387 {
17388 struct frame *sf = SELECTED_FRAME ();
17389 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17390 int vpos;
17391
17392 CHECK_NUMBER (row);
17393 vpos = XINT (row);
17394 if (vpos >= 0 && vpos < m->nrows)
17395 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17396 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17397 return Qnil;
17398 }
17399
17400
17401 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17402 doc: /* Toggle tracing of redisplay.
17403 With ARG, turn tracing on if and only if ARG is positive. */)
17404 (Lisp_Object arg)
17405 {
17406 if (NILP (arg))
17407 trace_redisplay_p = !trace_redisplay_p;
17408 else
17409 {
17410 arg = Fprefix_numeric_value (arg);
17411 trace_redisplay_p = XINT (arg) > 0;
17412 }
17413
17414 return Qnil;
17415 }
17416
17417
17418 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17419 doc: /* Like `format', but print result to stderr.
17420 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17421 (ptrdiff_t nargs, Lisp_Object *args)
17422 {
17423 Lisp_Object s = Fformat (nargs, args);
17424 fprintf (stderr, "%s", SDATA (s));
17425 return Qnil;
17426 }
17427
17428 #endif /* GLYPH_DEBUG */
17429
17430
17431 \f
17432 /***********************************************************************
17433 Building Desired Matrix Rows
17434 ***********************************************************************/
17435
17436 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17437 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17438
17439 static struct glyph_row *
17440 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17441 {
17442 struct frame *f = XFRAME (WINDOW_FRAME (w));
17443 struct buffer *buffer = XBUFFER (w->buffer);
17444 struct buffer *old = current_buffer;
17445 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17446 int arrow_len = SCHARS (overlay_arrow_string);
17447 const unsigned char *arrow_end = arrow_string + arrow_len;
17448 const unsigned char *p;
17449 struct it it;
17450 int multibyte_p;
17451 int n_glyphs_before;
17452
17453 set_buffer_temp (buffer);
17454 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17455 it.glyph_row->used[TEXT_AREA] = 0;
17456 SET_TEXT_POS (it.position, 0, 0);
17457
17458 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17459 p = arrow_string;
17460 while (p < arrow_end)
17461 {
17462 Lisp_Object face, ilisp;
17463
17464 /* Get the next character. */
17465 if (multibyte_p)
17466 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17467 else
17468 {
17469 it.c = it.char_to_display = *p, it.len = 1;
17470 if (! ASCII_CHAR_P (it.c))
17471 it.char_to_display = BYTE8_TO_CHAR (it.c);
17472 }
17473 p += it.len;
17474
17475 /* Get its face. */
17476 ilisp = make_number (p - arrow_string);
17477 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17478 it.face_id = compute_char_face (f, it.char_to_display, face);
17479
17480 /* Compute its width, get its glyphs. */
17481 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17482 SET_TEXT_POS (it.position, -1, -1);
17483 PRODUCE_GLYPHS (&it);
17484
17485 /* If this character doesn't fit any more in the line, we have
17486 to remove some glyphs. */
17487 if (it.current_x > it.last_visible_x)
17488 {
17489 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17490 break;
17491 }
17492 }
17493
17494 set_buffer_temp (old);
17495 return it.glyph_row;
17496 }
17497
17498
17499 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17500 glyphs are only inserted for terminal frames since we can't really
17501 win with truncation glyphs when partially visible glyphs are
17502 involved. Which glyphs to insert is determined by
17503 produce_special_glyphs. */
17504
17505 static void
17506 insert_left_trunc_glyphs (struct it *it)
17507 {
17508 struct it truncate_it;
17509 struct glyph *from, *end, *to, *toend;
17510
17511 xassert (!FRAME_WINDOW_P (it->f));
17512
17513 /* Get the truncation glyphs. */
17514 truncate_it = *it;
17515 truncate_it.current_x = 0;
17516 truncate_it.face_id = DEFAULT_FACE_ID;
17517 truncate_it.glyph_row = &scratch_glyph_row;
17518 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17519 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17520 truncate_it.object = make_number (0);
17521 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17522
17523 /* Overwrite glyphs from IT with truncation glyphs. */
17524 if (!it->glyph_row->reversed_p)
17525 {
17526 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17527 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17528 to = it->glyph_row->glyphs[TEXT_AREA];
17529 toend = to + it->glyph_row->used[TEXT_AREA];
17530
17531 while (from < end)
17532 *to++ = *from++;
17533
17534 /* There may be padding glyphs left over. Overwrite them too. */
17535 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17536 {
17537 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17538 while (from < end)
17539 *to++ = *from++;
17540 }
17541
17542 if (to > toend)
17543 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17544 }
17545 else
17546 {
17547 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17548 that back to front. */
17549 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17550 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17551 toend = it->glyph_row->glyphs[TEXT_AREA];
17552 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17553
17554 while (from >= end && to >= toend)
17555 *to-- = *from--;
17556 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17557 {
17558 from =
17559 truncate_it.glyph_row->glyphs[TEXT_AREA]
17560 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17561 while (from >= end && to >= toend)
17562 *to-- = *from--;
17563 }
17564 if (from >= end)
17565 {
17566 /* Need to free some room before prepending additional
17567 glyphs. */
17568 int move_by = from - end + 1;
17569 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17570 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17571
17572 for ( ; g >= g0; g--)
17573 g[move_by] = *g;
17574 while (from >= end)
17575 *to-- = *from--;
17576 it->glyph_row->used[TEXT_AREA] += move_by;
17577 }
17578 }
17579 }
17580
17581
17582 /* Compute the pixel height and width of IT->glyph_row.
17583
17584 Most of the time, ascent and height of a display line will be equal
17585 to the max_ascent and max_height values of the display iterator
17586 structure. This is not the case if
17587
17588 1. We hit ZV without displaying anything. In this case, max_ascent
17589 and max_height will be zero.
17590
17591 2. We have some glyphs that don't contribute to the line height.
17592 (The glyph row flag contributes_to_line_height_p is for future
17593 pixmap extensions).
17594
17595 The first case is easily covered by using default values because in
17596 these cases, the line height does not really matter, except that it
17597 must not be zero. */
17598
17599 static void
17600 compute_line_metrics (struct it *it)
17601 {
17602 struct glyph_row *row = it->glyph_row;
17603
17604 if (FRAME_WINDOW_P (it->f))
17605 {
17606 int i, min_y, max_y;
17607
17608 /* The line may consist of one space only, that was added to
17609 place the cursor on it. If so, the row's height hasn't been
17610 computed yet. */
17611 if (row->height == 0)
17612 {
17613 if (it->max_ascent + it->max_descent == 0)
17614 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17615 row->ascent = it->max_ascent;
17616 row->height = it->max_ascent + it->max_descent;
17617 row->phys_ascent = it->max_phys_ascent;
17618 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17619 row->extra_line_spacing = it->max_extra_line_spacing;
17620 }
17621
17622 /* Compute the width of this line. */
17623 row->pixel_width = row->x;
17624 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17625 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17626
17627 xassert (row->pixel_width >= 0);
17628 xassert (row->ascent >= 0 && row->height > 0);
17629
17630 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17631 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17632
17633 /* If first line's physical ascent is larger than its logical
17634 ascent, use the physical ascent, and make the row taller.
17635 This makes accented characters fully visible. */
17636 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17637 && row->phys_ascent > row->ascent)
17638 {
17639 row->height += row->phys_ascent - row->ascent;
17640 row->ascent = row->phys_ascent;
17641 }
17642
17643 /* Compute how much of the line is visible. */
17644 row->visible_height = row->height;
17645
17646 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17647 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17648
17649 if (row->y < min_y)
17650 row->visible_height -= min_y - row->y;
17651 if (row->y + row->height > max_y)
17652 row->visible_height -= row->y + row->height - max_y;
17653 }
17654 else
17655 {
17656 row->pixel_width = row->used[TEXT_AREA];
17657 if (row->continued_p)
17658 row->pixel_width -= it->continuation_pixel_width;
17659 else if (row->truncated_on_right_p)
17660 row->pixel_width -= it->truncation_pixel_width;
17661 row->ascent = row->phys_ascent = 0;
17662 row->height = row->phys_height = row->visible_height = 1;
17663 row->extra_line_spacing = 0;
17664 }
17665
17666 /* Compute a hash code for this row. */
17667 {
17668 int area, i;
17669 row->hash = 0;
17670 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17671 for (i = 0; i < row->used[area]; ++i)
17672 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17673 + row->glyphs[area][i].u.val
17674 + row->glyphs[area][i].face_id
17675 + row->glyphs[area][i].padding_p
17676 + (row->glyphs[area][i].type << 2));
17677 }
17678
17679 it->max_ascent = it->max_descent = 0;
17680 it->max_phys_ascent = it->max_phys_descent = 0;
17681 }
17682
17683
17684 /* Append one space to the glyph row of iterator IT if doing a
17685 window-based redisplay. The space has the same face as
17686 IT->face_id. Value is non-zero if a space was added.
17687
17688 This function is called to make sure that there is always one glyph
17689 at the end of a glyph row that the cursor can be set on under
17690 window-systems. (If there weren't such a glyph we would not know
17691 how wide and tall a box cursor should be displayed).
17692
17693 At the same time this space let's a nicely handle clearing to the
17694 end of the line if the row ends in italic text. */
17695
17696 static int
17697 append_space_for_newline (struct it *it, int default_face_p)
17698 {
17699 if (FRAME_WINDOW_P (it->f))
17700 {
17701 int n = it->glyph_row->used[TEXT_AREA];
17702
17703 if (it->glyph_row->glyphs[TEXT_AREA] + n
17704 < it->glyph_row->glyphs[1 + TEXT_AREA])
17705 {
17706 /* Save some values that must not be changed.
17707 Must save IT->c and IT->len because otherwise
17708 ITERATOR_AT_END_P wouldn't work anymore after
17709 append_space_for_newline has been called. */
17710 enum display_element_type saved_what = it->what;
17711 int saved_c = it->c, saved_len = it->len;
17712 int saved_char_to_display = it->char_to_display;
17713 int saved_x = it->current_x;
17714 int saved_face_id = it->face_id;
17715 struct text_pos saved_pos;
17716 Lisp_Object saved_object;
17717 struct face *face;
17718
17719 saved_object = it->object;
17720 saved_pos = it->position;
17721
17722 it->what = IT_CHARACTER;
17723 memset (&it->position, 0, sizeof it->position);
17724 it->object = make_number (0);
17725 it->c = it->char_to_display = ' ';
17726 it->len = 1;
17727
17728 if (default_face_p)
17729 it->face_id = DEFAULT_FACE_ID;
17730 else if (it->face_before_selective_p)
17731 it->face_id = it->saved_face_id;
17732 face = FACE_FROM_ID (it->f, it->face_id);
17733 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17734
17735 PRODUCE_GLYPHS (it);
17736
17737 it->override_ascent = -1;
17738 it->constrain_row_ascent_descent_p = 0;
17739 it->current_x = saved_x;
17740 it->object = saved_object;
17741 it->position = saved_pos;
17742 it->what = saved_what;
17743 it->face_id = saved_face_id;
17744 it->len = saved_len;
17745 it->c = saved_c;
17746 it->char_to_display = saved_char_to_display;
17747 return 1;
17748 }
17749 }
17750
17751 return 0;
17752 }
17753
17754
17755 /* Extend the face of the last glyph in the text area of IT->glyph_row
17756 to the end of the display line. Called from display_line. If the
17757 glyph row is empty, add a space glyph to it so that we know the
17758 face to draw. Set the glyph row flag fill_line_p. If the glyph
17759 row is R2L, prepend a stretch glyph to cover the empty space to the
17760 left of the leftmost glyph. */
17761
17762 static void
17763 extend_face_to_end_of_line (struct it *it)
17764 {
17765 struct face *face;
17766 struct frame *f = it->f;
17767
17768 /* If line is already filled, do nothing. Non window-system frames
17769 get a grace of one more ``pixel'' because their characters are
17770 1-``pixel'' wide, so they hit the equality too early. This grace
17771 is needed only for R2L rows that are not continued, to produce
17772 one extra blank where we could display the cursor. */
17773 if (it->current_x >= it->last_visible_x
17774 + (!FRAME_WINDOW_P (f)
17775 && it->glyph_row->reversed_p
17776 && !it->glyph_row->continued_p))
17777 return;
17778
17779 /* Face extension extends the background and box of IT->face_id
17780 to the end of the line. If the background equals the background
17781 of the frame, we don't have to do anything. */
17782 if (it->face_before_selective_p)
17783 face = FACE_FROM_ID (f, it->saved_face_id);
17784 else
17785 face = FACE_FROM_ID (f, it->face_id);
17786
17787 if (FRAME_WINDOW_P (f)
17788 && it->glyph_row->displays_text_p
17789 && face->box == FACE_NO_BOX
17790 && face->background == FRAME_BACKGROUND_PIXEL (f)
17791 && !face->stipple
17792 && !it->glyph_row->reversed_p)
17793 return;
17794
17795 /* Set the glyph row flag indicating that the face of the last glyph
17796 in the text area has to be drawn to the end of the text area. */
17797 it->glyph_row->fill_line_p = 1;
17798
17799 /* If current character of IT is not ASCII, make sure we have the
17800 ASCII face. This will be automatically undone the next time
17801 get_next_display_element returns a multibyte character. Note
17802 that the character will always be single byte in unibyte
17803 text. */
17804 if (!ASCII_CHAR_P (it->c))
17805 {
17806 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17807 }
17808
17809 if (FRAME_WINDOW_P (f))
17810 {
17811 /* If the row is empty, add a space with the current face of IT,
17812 so that we know which face to draw. */
17813 if (it->glyph_row->used[TEXT_AREA] == 0)
17814 {
17815 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17816 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17817 it->glyph_row->used[TEXT_AREA] = 1;
17818 }
17819 #ifdef HAVE_WINDOW_SYSTEM
17820 if (it->glyph_row->reversed_p)
17821 {
17822 /* Prepend a stretch glyph to the row, such that the
17823 rightmost glyph will be drawn flushed all the way to the
17824 right margin of the window. The stretch glyph that will
17825 occupy the empty space, if any, to the left of the
17826 glyphs. */
17827 struct font *font = face->font ? face->font : FRAME_FONT (f);
17828 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17829 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17830 struct glyph *g;
17831 int row_width, stretch_ascent, stretch_width;
17832 struct text_pos saved_pos;
17833 int saved_face_id, saved_avoid_cursor;
17834
17835 for (row_width = 0, g = row_start; g < row_end; g++)
17836 row_width += g->pixel_width;
17837 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17838 if (stretch_width > 0)
17839 {
17840 stretch_ascent =
17841 (((it->ascent + it->descent)
17842 * FONT_BASE (font)) / FONT_HEIGHT (font));
17843 saved_pos = it->position;
17844 memset (&it->position, 0, sizeof it->position);
17845 saved_avoid_cursor = it->avoid_cursor_p;
17846 it->avoid_cursor_p = 1;
17847 saved_face_id = it->face_id;
17848 /* The last row's stretch glyph should get the default
17849 face, to avoid painting the rest of the window with
17850 the region face, if the region ends at ZV. */
17851 if (it->glyph_row->ends_at_zv_p)
17852 it->face_id = DEFAULT_FACE_ID;
17853 else
17854 it->face_id = face->id;
17855 append_stretch_glyph (it, make_number (0), stretch_width,
17856 it->ascent + it->descent, stretch_ascent);
17857 it->position = saved_pos;
17858 it->avoid_cursor_p = saved_avoid_cursor;
17859 it->face_id = saved_face_id;
17860 }
17861 }
17862 #endif /* HAVE_WINDOW_SYSTEM */
17863 }
17864 else
17865 {
17866 /* Save some values that must not be changed. */
17867 int saved_x = it->current_x;
17868 struct text_pos saved_pos;
17869 Lisp_Object saved_object;
17870 enum display_element_type saved_what = it->what;
17871 int saved_face_id = it->face_id;
17872
17873 saved_object = it->object;
17874 saved_pos = it->position;
17875
17876 it->what = IT_CHARACTER;
17877 memset (&it->position, 0, sizeof it->position);
17878 it->object = make_number (0);
17879 it->c = it->char_to_display = ' ';
17880 it->len = 1;
17881 /* The last row's blank glyphs should get the default face, to
17882 avoid painting the rest of the window with the region face,
17883 if the region ends at ZV. */
17884 if (it->glyph_row->ends_at_zv_p)
17885 it->face_id = DEFAULT_FACE_ID;
17886 else
17887 it->face_id = face->id;
17888
17889 PRODUCE_GLYPHS (it);
17890
17891 while (it->current_x <= it->last_visible_x)
17892 PRODUCE_GLYPHS (it);
17893
17894 /* Don't count these blanks really. It would let us insert a left
17895 truncation glyph below and make us set the cursor on them, maybe. */
17896 it->current_x = saved_x;
17897 it->object = saved_object;
17898 it->position = saved_pos;
17899 it->what = saved_what;
17900 it->face_id = saved_face_id;
17901 }
17902 }
17903
17904
17905 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17906 trailing whitespace. */
17907
17908 static int
17909 trailing_whitespace_p (EMACS_INT charpos)
17910 {
17911 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17912 int c = 0;
17913
17914 while (bytepos < ZV_BYTE
17915 && (c = FETCH_CHAR (bytepos),
17916 c == ' ' || c == '\t'))
17917 ++bytepos;
17918
17919 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17920 {
17921 if (bytepos != PT_BYTE)
17922 return 1;
17923 }
17924 return 0;
17925 }
17926
17927
17928 /* Highlight trailing whitespace, if any, in ROW. */
17929
17930 static void
17931 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17932 {
17933 int used = row->used[TEXT_AREA];
17934
17935 if (used)
17936 {
17937 struct glyph *start = row->glyphs[TEXT_AREA];
17938 struct glyph *glyph = start + used - 1;
17939
17940 if (row->reversed_p)
17941 {
17942 /* Right-to-left rows need to be processed in the opposite
17943 direction, so swap the edge pointers. */
17944 glyph = start;
17945 start = row->glyphs[TEXT_AREA] + used - 1;
17946 }
17947
17948 /* Skip over glyphs inserted to display the cursor at the
17949 end of a line, for extending the face of the last glyph
17950 to the end of the line on terminals, and for truncation
17951 and continuation glyphs. */
17952 if (!row->reversed_p)
17953 {
17954 while (glyph >= start
17955 && glyph->type == CHAR_GLYPH
17956 && INTEGERP (glyph->object))
17957 --glyph;
17958 }
17959 else
17960 {
17961 while (glyph <= start
17962 && glyph->type == CHAR_GLYPH
17963 && INTEGERP (glyph->object))
17964 ++glyph;
17965 }
17966
17967 /* If last glyph is a space or stretch, and it's trailing
17968 whitespace, set the face of all trailing whitespace glyphs in
17969 IT->glyph_row to `trailing-whitespace'. */
17970 if ((row->reversed_p ? glyph <= start : glyph >= start)
17971 && BUFFERP (glyph->object)
17972 && (glyph->type == STRETCH_GLYPH
17973 || (glyph->type == CHAR_GLYPH
17974 && glyph->u.ch == ' '))
17975 && trailing_whitespace_p (glyph->charpos))
17976 {
17977 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17978 if (face_id < 0)
17979 return;
17980
17981 if (!row->reversed_p)
17982 {
17983 while (glyph >= start
17984 && BUFFERP (glyph->object)
17985 && (glyph->type == STRETCH_GLYPH
17986 || (glyph->type == CHAR_GLYPH
17987 && glyph->u.ch == ' ')))
17988 (glyph--)->face_id = face_id;
17989 }
17990 else
17991 {
17992 while (glyph <= start
17993 && BUFFERP (glyph->object)
17994 && (glyph->type == STRETCH_GLYPH
17995 || (glyph->type == CHAR_GLYPH
17996 && glyph->u.ch == ' ')))
17997 (glyph++)->face_id = face_id;
17998 }
17999 }
18000 }
18001 }
18002
18003
18004 /* Value is non-zero if glyph row ROW should be
18005 used to hold the cursor. */
18006
18007 static int
18008 cursor_row_p (struct glyph_row *row)
18009 {
18010 int result = 1;
18011
18012 if (PT == CHARPOS (row->end.pos))
18013 {
18014 /* Suppose the row ends on a string.
18015 Unless the row is continued, that means it ends on a newline
18016 in the string. If it's anything other than a display string
18017 (e.g. a before-string from an overlay), we don't want the
18018 cursor there. (This heuristic seems to give the optimal
18019 behavior for the various types of multi-line strings.) */
18020 if (CHARPOS (row->end.string_pos) >= 0)
18021 {
18022 if (row->continued_p)
18023 result = 1;
18024 else
18025 {
18026 /* Check for `display' property. */
18027 struct glyph *beg = row->glyphs[TEXT_AREA];
18028 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18029 struct glyph *glyph;
18030
18031 result = 0;
18032 for (glyph = end; glyph >= beg; --glyph)
18033 if (STRINGP (glyph->object))
18034 {
18035 Lisp_Object prop
18036 = Fget_char_property (make_number (PT),
18037 Qdisplay, Qnil);
18038 result =
18039 (!NILP (prop)
18040 && display_prop_string_p (prop, glyph->object));
18041 break;
18042 }
18043 }
18044 }
18045 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18046 {
18047 /* If the row ends in middle of a real character,
18048 and the line is continued, we want the cursor here.
18049 That's because CHARPOS (ROW->end.pos) would equal
18050 PT if PT is before the character. */
18051 if (!row->ends_in_ellipsis_p)
18052 result = row->continued_p;
18053 else
18054 /* If the row ends in an ellipsis, then
18055 CHARPOS (ROW->end.pos) will equal point after the
18056 invisible text. We want that position to be displayed
18057 after the ellipsis. */
18058 result = 0;
18059 }
18060 /* If the row ends at ZV, display the cursor at the end of that
18061 row instead of at the start of the row below. */
18062 else if (row->ends_at_zv_p)
18063 result = 1;
18064 else
18065 result = 0;
18066 }
18067
18068 return result;
18069 }
18070
18071 \f
18072
18073 /* Push the property PROP so that it will be rendered at the current
18074 position in IT. Return 1 if PROP was successfully pushed, 0
18075 otherwise. Called from handle_line_prefix to handle the
18076 `line-prefix' and `wrap-prefix' properties. */
18077
18078 static int
18079 push_display_prop (struct it *it, Lisp_Object prop)
18080 {
18081 struct text_pos pos =
18082 (it->method == GET_FROM_STRING) ? it->current.string_pos : it->current.pos;
18083
18084 xassert (it->method == GET_FROM_BUFFER
18085 || it->method == GET_FROM_STRING);
18086
18087 /* We need to save the current buffer/string position, so it will be
18088 restored by pop_it, because iterate_out_of_display_property
18089 depends on that being set correctly, but some situations leave
18090 it->position not yet set when this function is called. */
18091 push_it (it, &pos);
18092
18093 if (STRINGP (prop))
18094 {
18095 if (SCHARS (prop) == 0)
18096 {
18097 pop_it (it);
18098 return 0;
18099 }
18100
18101 it->string = prop;
18102 it->multibyte_p = STRING_MULTIBYTE (it->string);
18103 it->current.overlay_string_index = -1;
18104 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18105 it->end_charpos = it->string_nchars = SCHARS (it->string);
18106 it->method = GET_FROM_STRING;
18107 it->stop_charpos = 0;
18108 it->prev_stop = 0;
18109 it->base_level_stop = 0;
18110
18111 /* Force paragraph direction to be that of the parent
18112 buffer/string. */
18113 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18114 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18115 else
18116 it->paragraph_embedding = L2R;
18117
18118 /* Set up the bidi iterator for this display string. */
18119 if (it->bidi_p)
18120 {
18121 it->bidi_it.string.lstring = it->string;
18122 it->bidi_it.string.s = NULL;
18123 it->bidi_it.string.schars = it->end_charpos;
18124 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18125 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18126 it->bidi_it.string.unibyte = !it->multibyte_p;
18127 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18128 }
18129 }
18130 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18131 {
18132 it->method = GET_FROM_STRETCH;
18133 it->object = prop;
18134 }
18135 #ifdef HAVE_WINDOW_SYSTEM
18136 else if (IMAGEP (prop))
18137 {
18138 it->what = IT_IMAGE;
18139 it->image_id = lookup_image (it->f, prop);
18140 it->method = GET_FROM_IMAGE;
18141 }
18142 #endif /* HAVE_WINDOW_SYSTEM */
18143 else
18144 {
18145 pop_it (it); /* bogus display property, give up */
18146 return 0;
18147 }
18148
18149 return 1;
18150 }
18151
18152 /* Return the character-property PROP at the current position in IT. */
18153
18154 static Lisp_Object
18155 get_it_property (struct it *it, Lisp_Object prop)
18156 {
18157 Lisp_Object position;
18158
18159 if (STRINGP (it->object))
18160 position = make_number (IT_STRING_CHARPOS (*it));
18161 else if (BUFFERP (it->object))
18162 position = make_number (IT_CHARPOS (*it));
18163 else
18164 return Qnil;
18165
18166 return Fget_char_property (position, prop, it->object);
18167 }
18168
18169 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18170
18171 static void
18172 handle_line_prefix (struct it *it)
18173 {
18174 Lisp_Object prefix;
18175
18176 if (it->continuation_lines_width > 0)
18177 {
18178 prefix = get_it_property (it, Qwrap_prefix);
18179 if (NILP (prefix))
18180 prefix = Vwrap_prefix;
18181 }
18182 else
18183 {
18184 prefix = get_it_property (it, Qline_prefix);
18185 if (NILP (prefix))
18186 prefix = Vline_prefix;
18187 }
18188 if (! NILP (prefix) && push_display_prop (it, prefix))
18189 {
18190 /* If the prefix is wider than the window, and we try to wrap
18191 it, it would acquire its own wrap prefix, and so on till the
18192 iterator stack overflows. So, don't wrap the prefix. */
18193 it->line_wrap = TRUNCATE;
18194 it->avoid_cursor_p = 1;
18195 }
18196 }
18197
18198 \f
18199
18200 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18201 only for R2L lines from display_line and display_string, when they
18202 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18203 the line/string needs to be continued on the next glyph row. */
18204 static void
18205 unproduce_glyphs (struct it *it, int n)
18206 {
18207 struct glyph *glyph, *end;
18208
18209 xassert (it->glyph_row);
18210 xassert (it->glyph_row->reversed_p);
18211 xassert (it->area == TEXT_AREA);
18212 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18213
18214 if (n > it->glyph_row->used[TEXT_AREA])
18215 n = it->glyph_row->used[TEXT_AREA];
18216 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18217 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18218 for ( ; glyph < end; glyph++)
18219 glyph[-n] = *glyph;
18220 }
18221
18222 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18223 and ROW->maxpos. */
18224 static void
18225 find_row_edges (struct it *it, struct glyph_row *row,
18226 EMACS_INT min_pos, EMACS_INT min_bpos,
18227 EMACS_INT max_pos, EMACS_INT max_bpos)
18228 {
18229 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18230 lines' rows is implemented for bidi-reordered rows. */
18231
18232 /* ROW->minpos is the value of min_pos, the minimal buffer position
18233 we have in ROW, or ROW->start.pos if that is smaller. */
18234 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18235 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18236 else
18237 /* We didn't find buffer positions smaller than ROW->start, or
18238 didn't find _any_ valid buffer positions in any of the glyphs,
18239 so we must trust the iterator's computed positions. */
18240 row->minpos = row->start.pos;
18241 if (max_pos <= 0)
18242 {
18243 max_pos = CHARPOS (it->current.pos);
18244 max_bpos = BYTEPOS (it->current.pos);
18245 }
18246
18247 /* Here are the various use-cases for ending the row, and the
18248 corresponding values for ROW->maxpos:
18249
18250 Line ends in a newline from buffer eol_pos + 1
18251 Line is continued from buffer max_pos + 1
18252 Line is truncated on right it->current.pos
18253 Line ends in a newline from string max_pos
18254 Line is continued from string max_pos
18255 Line is continued from display vector max_pos
18256 Line is entirely from a string min_pos == max_pos
18257 Line is entirely from a display vector min_pos == max_pos
18258 Line that ends at ZV ZV
18259
18260 If you discover other use-cases, please add them here as
18261 appropriate. */
18262 if (row->ends_at_zv_p)
18263 row->maxpos = it->current.pos;
18264 else if (row->used[TEXT_AREA])
18265 {
18266 if (row->ends_in_newline_from_string_p)
18267 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18268 else if (CHARPOS (it->eol_pos) > 0)
18269 SET_TEXT_POS (row->maxpos,
18270 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18271 else if (row->continued_p)
18272 {
18273 /* If max_pos is different from IT's current position, it
18274 means IT->method does not belong to the display element
18275 at max_pos. However, it also means that the display
18276 element at max_pos was displayed in its entirety on this
18277 line, which is equivalent to saying that the next line
18278 starts at the next buffer position. */
18279 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18280 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18281 else
18282 {
18283 INC_BOTH (max_pos, max_bpos);
18284 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18285 }
18286 }
18287 else if (row->truncated_on_right_p)
18288 /* display_line already called reseat_at_next_visible_line_start,
18289 which puts the iterator at the beginning of the next line, in
18290 the logical order. */
18291 row->maxpos = it->current.pos;
18292 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18293 /* A line that is entirely from a string/image/stretch... */
18294 row->maxpos = row->minpos;
18295 else
18296 abort ();
18297 }
18298 else
18299 row->maxpos = it->current.pos;
18300 }
18301
18302 /* Construct the glyph row IT->glyph_row in the desired matrix of
18303 IT->w from text at the current position of IT. See dispextern.h
18304 for an overview of struct it. Value is non-zero if
18305 IT->glyph_row displays text, as opposed to a line displaying ZV
18306 only. */
18307
18308 static int
18309 display_line (struct it *it)
18310 {
18311 struct glyph_row *row = it->glyph_row;
18312 Lisp_Object overlay_arrow_string;
18313 struct it wrap_it;
18314 void *wrap_data = NULL;
18315 int may_wrap = 0, wrap_x IF_LINT (= 0);
18316 int wrap_row_used = -1;
18317 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18318 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18319 int wrap_row_extra_line_spacing IF_LINT (= 0);
18320 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18321 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18322 int cvpos;
18323 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18324 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18325
18326 /* We always start displaying at hpos zero even if hscrolled. */
18327 xassert (it->hpos == 0 && it->current_x == 0);
18328
18329 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18330 >= it->w->desired_matrix->nrows)
18331 {
18332 it->w->nrows_scale_factor++;
18333 fonts_changed_p = 1;
18334 return 0;
18335 }
18336
18337 /* Is IT->w showing the region? */
18338 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18339
18340 /* Clear the result glyph row and enable it. */
18341 prepare_desired_row (row);
18342
18343 row->y = it->current_y;
18344 row->start = it->start;
18345 row->continuation_lines_width = it->continuation_lines_width;
18346 row->displays_text_p = 1;
18347 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18348 it->starts_in_middle_of_char_p = 0;
18349
18350 /* Arrange the overlays nicely for our purposes. Usually, we call
18351 display_line on only one line at a time, in which case this
18352 can't really hurt too much, or we call it on lines which appear
18353 one after another in the buffer, in which case all calls to
18354 recenter_overlay_lists but the first will be pretty cheap. */
18355 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18356
18357 /* Move over display elements that are not visible because we are
18358 hscrolled. This may stop at an x-position < IT->first_visible_x
18359 if the first glyph is partially visible or if we hit a line end. */
18360 if (it->current_x < it->first_visible_x)
18361 {
18362 this_line_min_pos = row->start.pos;
18363 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18364 MOVE_TO_POS | MOVE_TO_X);
18365 /* Record the smallest positions seen while we moved over
18366 display elements that are not visible. This is needed by
18367 redisplay_internal for optimizing the case where the cursor
18368 stays inside the same line. The rest of this function only
18369 considers positions that are actually displayed, so
18370 RECORD_MAX_MIN_POS will not otherwise record positions that
18371 are hscrolled to the left of the left edge of the window. */
18372 min_pos = CHARPOS (this_line_min_pos);
18373 min_bpos = BYTEPOS (this_line_min_pos);
18374 }
18375 else
18376 {
18377 /* We only do this when not calling `move_it_in_display_line_to'
18378 above, because move_it_in_display_line_to calls
18379 handle_line_prefix itself. */
18380 handle_line_prefix (it);
18381 }
18382
18383 /* Get the initial row height. This is either the height of the
18384 text hscrolled, if there is any, or zero. */
18385 row->ascent = it->max_ascent;
18386 row->height = it->max_ascent + it->max_descent;
18387 row->phys_ascent = it->max_phys_ascent;
18388 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18389 row->extra_line_spacing = it->max_extra_line_spacing;
18390
18391 /* Utility macro to record max and min buffer positions seen until now. */
18392 #define RECORD_MAX_MIN_POS(IT) \
18393 do \
18394 { \
18395 int composition_p = (IT)->what == IT_COMPOSITION; \
18396 EMACS_INT current_pos = \
18397 composition_p ? (IT)->cmp_it.charpos \
18398 : IT_CHARPOS (*(IT)); \
18399 EMACS_INT current_bpos = \
18400 composition_p ? CHAR_TO_BYTE (current_pos) \
18401 : IT_BYTEPOS (*(IT)); \
18402 if (current_pos < min_pos) \
18403 { \
18404 min_pos = current_pos; \
18405 min_bpos = current_bpos; \
18406 } \
18407 if (current_pos > max_pos) \
18408 { \
18409 max_pos = current_pos; \
18410 max_bpos = current_bpos; \
18411 } \
18412 } \
18413 while (0)
18414
18415 /* Loop generating characters. The loop is left with IT on the next
18416 character to display. */
18417 while (1)
18418 {
18419 int n_glyphs_before, hpos_before, x_before;
18420 int x, nglyphs;
18421 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18422
18423 /* Retrieve the next thing to display. Value is zero if end of
18424 buffer reached. */
18425 if (!get_next_display_element (it))
18426 {
18427 /* Maybe add a space at the end of this line that is used to
18428 display the cursor there under X. Set the charpos of the
18429 first glyph of blank lines not corresponding to any text
18430 to -1. */
18431 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18432 row->exact_window_width_line_p = 1;
18433 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18434 || row->used[TEXT_AREA] == 0)
18435 {
18436 row->glyphs[TEXT_AREA]->charpos = -1;
18437 row->displays_text_p = 0;
18438
18439 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18440 && (!MINI_WINDOW_P (it->w)
18441 || (minibuf_level && EQ (it->window, minibuf_window))))
18442 row->indicate_empty_line_p = 1;
18443 }
18444
18445 it->continuation_lines_width = 0;
18446 row->ends_at_zv_p = 1;
18447 /* A row that displays right-to-left text must always have
18448 its last face extended all the way to the end of line,
18449 even if this row ends in ZV, because we still write to
18450 the screen left to right. */
18451 if (row->reversed_p)
18452 extend_face_to_end_of_line (it);
18453 break;
18454 }
18455
18456 /* Now, get the metrics of what we want to display. This also
18457 generates glyphs in `row' (which is IT->glyph_row). */
18458 n_glyphs_before = row->used[TEXT_AREA];
18459 x = it->current_x;
18460
18461 /* Remember the line height so far in case the next element doesn't
18462 fit on the line. */
18463 if (it->line_wrap != TRUNCATE)
18464 {
18465 ascent = it->max_ascent;
18466 descent = it->max_descent;
18467 phys_ascent = it->max_phys_ascent;
18468 phys_descent = it->max_phys_descent;
18469
18470 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18471 {
18472 if (IT_DISPLAYING_WHITESPACE (it))
18473 may_wrap = 1;
18474 else if (may_wrap)
18475 {
18476 SAVE_IT (wrap_it, *it, wrap_data);
18477 wrap_x = x;
18478 wrap_row_used = row->used[TEXT_AREA];
18479 wrap_row_ascent = row->ascent;
18480 wrap_row_height = row->height;
18481 wrap_row_phys_ascent = row->phys_ascent;
18482 wrap_row_phys_height = row->phys_height;
18483 wrap_row_extra_line_spacing = row->extra_line_spacing;
18484 wrap_row_min_pos = min_pos;
18485 wrap_row_min_bpos = min_bpos;
18486 wrap_row_max_pos = max_pos;
18487 wrap_row_max_bpos = max_bpos;
18488 may_wrap = 0;
18489 }
18490 }
18491 }
18492
18493 PRODUCE_GLYPHS (it);
18494
18495 /* If this display element was in marginal areas, continue with
18496 the next one. */
18497 if (it->area != TEXT_AREA)
18498 {
18499 row->ascent = max (row->ascent, it->max_ascent);
18500 row->height = max (row->height, it->max_ascent + it->max_descent);
18501 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18502 row->phys_height = max (row->phys_height,
18503 it->max_phys_ascent + it->max_phys_descent);
18504 row->extra_line_spacing = max (row->extra_line_spacing,
18505 it->max_extra_line_spacing);
18506 set_iterator_to_next (it, 1);
18507 continue;
18508 }
18509
18510 /* Does the display element fit on the line? If we truncate
18511 lines, we should draw past the right edge of the window. If
18512 we don't truncate, we want to stop so that we can display the
18513 continuation glyph before the right margin. If lines are
18514 continued, there are two possible strategies for characters
18515 resulting in more than 1 glyph (e.g. tabs): Display as many
18516 glyphs as possible in this line and leave the rest for the
18517 continuation line, or display the whole element in the next
18518 line. Original redisplay did the former, so we do it also. */
18519 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18520 hpos_before = it->hpos;
18521 x_before = x;
18522
18523 if (/* Not a newline. */
18524 nglyphs > 0
18525 /* Glyphs produced fit entirely in the line. */
18526 && it->current_x < it->last_visible_x)
18527 {
18528 it->hpos += nglyphs;
18529 row->ascent = max (row->ascent, it->max_ascent);
18530 row->height = max (row->height, it->max_ascent + it->max_descent);
18531 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18532 row->phys_height = max (row->phys_height,
18533 it->max_phys_ascent + it->max_phys_descent);
18534 row->extra_line_spacing = max (row->extra_line_spacing,
18535 it->max_extra_line_spacing);
18536 if (it->current_x - it->pixel_width < it->first_visible_x)
18537 row->x = x - it->first_visible_x;
18538 /* Record the maximum and minimum buffer positions seen so
18539 far in glyphs that will be displayed by this row. */
18540 if (it->bidi_p)
18541 RECORD_MAX_MIN_POS (it);
18542 }
18543 else
18544 {
18545 int i, new_x;
18546 struct glyph *glyph;
18547
18548 for (i = 0; i < nglyphs; ++i, x = new_x)
18549 {
18550 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18551 new_x = x + glyph->pixel_width;
18552
18553 if (/* Lines are continued. */
18554 it->line_wrap != TRUNCATE
18555 && (/* Glyph doesn't fit on the line. */
18556 new_x > it->last_visible_x
18557 /* Or it fits exactly on a window system frame. */
18558 || (new_x == it->last_visible_x
18559 && FRAME_WINDOW_P (it->f))))
18560 {
18561 /* End of a continued line. */
18562
18563 if (it->hpos == 0
18564 || (new_x == it->last_visible_x
18565 && FRAME_WINDOW_P (it->f)))
18566 {
18567 /* Current glyph is the only one on the line or
18568 fits exactly on the line. We must continue
18569 the line because we can't draw the cursor
18570 after the glyph. */
18571 row->continued_p = 1;
18572 it->current_x = new_x;
18573 it->continuation_lines_width += new_x;
18574 ++it->hpos;
18575 /* Record the maximum and minimum buffer
18576 positions seen so far in glyphs that will be
18577 displayed by this row. */
18578 if (it->bidi_p)
18579 RECORD_MAX_MIN_POS (it);
18580 if (i == nglyphs - 1)
18581 {
18582 /* If line-wrap is on, check if a previous
18583 wrap point was found. */
18584 if (wrap_row_used > 0
18585 /* Even if there is a previous wrap
18586 point, continue the line here as
18587 usual, if (i) the previous character
18588 was a space or tab AND (ii) the
18589 current character is not. */
18590 && (!may_wrap
18591 || IT_DISPLAYING_WHITESPACE (it)))
18592 goto back_to_wrap;
18593
18594 set_iterator_to_next (it, 1);
18595 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18596 {
18597 if (!get_next_display_element (it))
18598 {
18599 row->exact_window_width_line_p = 1;
18600 it->continuation_lines_width = 0;
18601 row->continued_p = 0;
18602 row->ends_at_zv_p = 1;
18603 }
18604 else if (ITERATOR_AT_END_OF_LINE_P (it))
18605 {
18606 row->continued_p = 0;
18607 row->exact_window_width_line_p = 1;
18608 }
18609 }
18610 }
18611 }
18612 else if (CHAR_GLYPH_PADDING_P (*glyph)
18613 && !FRAME_WINDOW_P (it->f))
18614 {
18615 /* A padding glyph that doesn't fit on this line.
18616 This means the whole character doesn't fit
18617 on the line. */
18618 if (row->reversed_p)
18619 unproduce_glyphs (it, row->used[TEXT_AREA]
18620 - n_glyphs_before);
18621 row->used[TEXT_AREA] = n_glyphs_before;
18622
18623 /* Fill the rest of the row with continuation
18624 glyphs like in 20.x. */
18625 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18626 < row->glyphs[1 + TEXT_AREA])
18627 produce_special_glyphs (it, IT_CONTINUATION);
18628
18629 row->continued_p = 1;
18630 it->current_x = x_before;
18631 it->continuation_lines_width += x_before;
18632
18633 /* Restore the height to what it was before the
18634 element not fitting on the line. */
18635 it->max_ascent = ascent;
18636 it->max_descent = descent;
18637 it->max_phys_ascent = phys_ascent;
18638 it->max_phys_descent = phys_descent;
18639 }
18640 else if (wrap_row_used > 0)
18641 {
18642 back_to_wrap:
18643 if (row->reversed_p)
18644 unproduce_glyphs (it,
18645 row->used[TEXT_AREA] - wrap_row_used);
18646 RESTORE_IT (it, &wrap_it, wrap_data);
18647 it->continuation_lines_width += wrap_x;
18648 row->used[TEXT_AREA] = wrap_row_used;
18649 row->ascent = wrap_row_ascent;
18650 row->height = wrap_row_height;
18651 row->phys_ascent = wrap_row_phys_ascent;
18652 row->phys_height = wrap_row_phys_height;
18653 row->extra_line_spacing = wrap_row_extra_line_spacing;
18654 min_pos = wrap_row_min_pos;
18655 min_bpos = wrap_row_min_bpos;
18656 max_pos = wrap_row_max_pos;
18657 max_bpos = wrap_row_max_bpos;
18658 row->continued_p = 1;
18659 row->ends_at_zv_p = 0;
18660 row->exact_window_width_line_p = 0;
18661 it->continuation_lines_width += x;
18662
18663 /* Make sure that a non-default face is extended
18664 up to the right margin of the window. */
18665 extend_face_to_end_of_line (it);
18666 }
18667 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18668 {
18669 /* A TAB that extends past the right edge of the
18670 window. This produces a single glyph on
18671 window system frames. We leave the glyph in
18672 this row and let it fill the row, but don't
18673 consume the TAB. */
18674 it->continuation_lines_width += it->last_visible_x;
18675 row->ends_in_middle_of_char_p = 1;
18676 row->continued_p = 1;
18677 glyph->pixel_width = it->last_visible_x - x;
18678 it->starts_in_middle_of_char_p = 1;
18679 }
18680 else
18681 {
18682 /* Something other than a TAB that draws past
18683 the right edge of the window. Restore
18684 positions to values before the element. */
18685 if (row->reversed_p)
18686 unproduce_glyphs (it, row->used[TEXT_AREA]
18687 - (n_glyphs_before + i));
18688 row->used[TEXT_AREA] = n_glyphs_before + i;
18689
18690 /* Display continuation glyphs. */
18691 if (!FRAME_WINDOW_P (it->f))
18692 produce_special_glyphs (it, IT_CONTINUATION);
18693 row->continued_p = 1;
18694
18695 it->current_x = x_before;
18696 it->continuation_lines_width += x;
18697 extend_face_to_end_of_line (it);
18698
18699 if (nglyphs > 1 && i > 0)
18700 {
18701 row->ends_in_middle_of_char_p = 1;
18702 it->starts_in_middle_of_char_p = 1;
18703 }
18704
18705 /* Restore the height to what it was before the
18706 element not fitting on the line. */
18707 it->max_ascent = ascent;
18708 it->max_descent = descent;
18709 it->max_phys_ascent = phys_ascent;
18710 it->max_phys_descent = phys_descent;
18711 }
18712
18713 break;
18714 }
18715 else if (new_x > it->first_visible_x)
18716 {
18717 /* Increment number of glyphs actually displayed. */
18718 ++it->hpos;
18719
18720 /* Record the maximum and minimum buffer positions
18721 seen so far in glyphs that will be displayed by
18722 this row. */
18723 if (it->bidi_p)
18724 RECORD_MAX_MIN_POS (it);
18725
18726 if (x < it->first_visible_x)
18727 /* Glyph is partially visible, i.e. row starts at
18728 negative X position. */
18729 row->x = x - it->first_visible_x;
18730 }
18731 else
18732 {
18733 /* Glyph is completely off the left margin of the
18734 window. This should not happen because of the
18735 move_it_in_display_line at the start of this
18736 function, unless the text display area of the
18737 window is empty. */
18738 xassert (it->first_visible_x <= it->last_visible_x);
18739 }
18740 }
18741
18742 row->ascent = max (row->ascent, it->max_ascent);
18743 row->height = max (row->height, it->max_ascent + it->max_descent);
18744 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18745 row->phys_height = max (row->phys_height,
18746 it->max_phys_ascent + it->max_phys_descent);
18747 row->extra_line_spacing = max (row->extra_line_spacing,
18748 it->max_extra_line_spacing);
18749
18750 /* End of this display line if row is continued. */
18751 if (row->continued_p || row->ends_at_zv_p)
18752 break;
18753 }
18754
18755 at_end_of_line:
18756 /* Is this a line end? If yes, we're also done, after making
18757 sure that a non-default face is extended up to the right
18758 margin of the window. */
18759 if (ITERATOR_AT_END_OF_LINE_P (it))
18760 {
18761 int used_before = row->used[TEXT_AREA];
18762
18763 row->ends_in_newline_from_string_p = STRINGP (it->object);
18764
18765 /* Add a space at the end of the line that is used to
18766 display the cursor there. */
18767 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18768 append_space_for_newline (it, 0);
18769
18770 /* Extend the face to the end of the line. */
18771 extend_face_to_end_of_line (it);
18772
18773 /* Make sure we have the position. */
18774 if (used_before == 0)
18775 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18776
18777 /* Record the position of the newline, for use in
18778 find_row_edges. */
18779 it->eol_pos = it->current.pos;
18780
18781 /* Consume the line end. This skips over invisible lines. */
18782 set_iterator_to_next (it, 1);
18783 it->continuation_lines_width = 0;
18784 break;
18785 }
18786
18787 /* Proceed with next display element. Note that this skips
18788 over lines invisible because of selective display. */
18789 set_iterator_to_next (it, 1);
18790
18791 /* If we truncate lines, we are done when the last displayed
18792 glyphs reach past the right margin of the window. */
18793 if (it->line_wrap == TRUNCATE
18794 && (FRAME_WINDOW_P (it->f)
18795 ? (it->current_x >= it->last_visible_x)
18796 : (it->current_x > it->last_visible_x)))
18797 {
18798 /* Maybe add truncation glyphs. */
18799 if (!FRAME_WINDOW_P (it->f))
18800 {
18801 int i, n;
18802
18803 if (!row->reversed_p)
18804 {
18805 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18806 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18807 break;
18808 }
18809 else
18810 {
18811 for (i = 0; i < row->used[TEXT_AREA]; i++)
18812 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18813 break;
18814 /* Remove any padding glyphs at the front of ROW, to
18815 make room for the truncation glyphs we will be
18816 adding below. The loop below always inserts at
18817 least one truncation glyph, so also remove the
18818 last glyph added to ROW. */
18819 unproduce_glyphs (it, i + 1);
18820 /* Adjust i for the loop below. */
18821 i = row->used[TEXT_AREA] - (i + 1);
18822 }
18823
18824 for (n = row->used[TEXT_AREA]; i < n; ++i)
18825 {
18826 row->used[TEXT_AREA] = i;
18827 produce_special_glyphs (it, IT_TRUNCATION);
18828 }
18829 }
18830 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18831 {
18832 /* Don't truncate if we can overflow newline into fringe. */
18833 if (!get_next_display_element (it))
18834 {
18835 it->continuation_lines_width = 0;
18836 row->ends_at_zv_p = 1;
18837 row->exact_window_width_line_p = 1;
18838 break;
18839 }
18840 if (ITERATOR_AT_END_OF_LINE_P (it))
18841 {
18842 row->exact_window_width_line_p = 1;
18843 goto at_end_of_line;
18844 }
18845 }
18846
18847 row->truncated_on_right_p = 1;
18848 it->continuation_lines_width = 0;
18849 reseat_at_next_visible_line_start (it, 0);
18850 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18851 it->hpos = hpos_before;
18852 it->current_x = x_before;
18853 break;
18854 }
18855 }
18856
18857 if (wrap_data)
18858 bidi_unshelve_cache (wrap_data, 1);
18859
18860 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18861 at the left window margin. */
18862 if (it->first_visible_x
18863 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18864 {
18865 if (!FRAME_WINDOW_P (it->f))
18866 insert_left_trunc_glyphs (it);
18867 row->truncated_on_left_p = 1;
18868 }
18869
18870 /* Remember the position at which this line ends.
18871
18872 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18873 cannot be before the call to find_row_edges below, since that is
18874 where these positions are determined. */
18875 row->end = it->current;
18876 if (!it->bidi_p)
18877 {
18878 row->minpos = row->start.pos;
18879 row->maxpos = row->end.pos;
18880 }
18881 else
18882 {
18883 /* ROW->minpos and ROW->maxpos must be the smallest and
18884 `1 + the largest' buffer positions in ROW. But if ROW was
18885 bidi-reordered, these two positions can be anywhere in the
18886 row, so we must determine them now. */
18887 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18888 }
18889
18890 /* If the start of this line is the overlay arrow-position, then
18891 mark this glyph row as the one containing the overlay arrow.
18892 This is clearly a mess with variable size fonts. It would be
18893 better to let it be displayed like cursors under X. */
18894 if ((row->displays_text_p || !overlay_arrow_seen)
18895 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18896 !NILP (overlay_arrow_string)))
18897 {
18898 /* Overlay arrow in window redisplay is a fringe bitmap. */
18899 if (STRINGP (overlay_arrow_string))
18900 {
18901 struct glyph_row *arrow_row
18902 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18903 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18904 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18905 struct glyph *p = row->glyphs[TEXT_AREA];
18906 struct glyph *p2, *end;
18907
18908 /* Copy the arrow glyphs. */
18909 while (glyph < arrow_end)
18910 *p++ = *glyph++;
18911
18912 /* Throw away padding glyphs. */
18913 p2 = p;
18914 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18915 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18916 ++p2;
18917 if (p2 > p)
18918 {
18919 while (p2 < end)
18920 *p++ = *p2++;
18921 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18922 }
18923 }
18924 else
18925 {
18926 xassert (INTEGERP (overlay_arrow_string));
18927 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18928 }
18929 overlay_arrow_seen = 1;
18930 }
18931
18932 /* Compute pixel dimensions of this line. */
18933 compute_line_metrics (it);
18934
18935 /* Record whether this row ends inside an ellipsis. */
18936 row->ends_in_ellipsis_p
18937 = (it->method == GET_FROM_DISPLAY_VECTOR
18938 && it->ellipsis_p);
18939
18940 /* Save fringe bitmaps in this row. */
18941 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18942 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18943 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18944 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18945
18946 it->left_user_fringe_bitmap = 0;
18947 it->left_user_fringe_face_id = 0;
18948 it->right_user_fringe_bitmap = 0;
18949 it->right_user_fringe_face_id = 0;
18950
18951 /* Maybe set the cursor. */
18952 cvpos = it->w->cursor.vpos;
18953 if ((cvpos < 0
18954 /* In bidi-reordered rows, keep checking for proper cursor
18955 position even if one has been found already, because buffer
18956 positions in such rows change non-linearly with ROW->VPOS,
18957 when a line is continued. One exception: when we are at ZV,
18958 display cursor on the first suitable glyph row, since all
18959 the empty rows after that also have their position set to ZV. */
18960 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18961 lines' rows is implemented for bidi-reordered rows. */
18962 || (it->bidi_p
18963 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18964 && PT >= MATRIX_ROW_START_CHARPOS (row)
18965 && PT <= MATRIX_ROW_END_CHARPOS (row)
18966 && cursor_row_p (row))
18967 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18968
18969 /* Highlight trailing whitespace. */
18970 if (!NILP (Vshow_trailing_whitespace))
18971 highlight_trailing_whitespace (it->f, it->glyph_row);
18972
18973 /* Prepare for the next line. This line starts horizontally at (X
18974 HPOS) = (0 0). Vertical positions are incremented. As a
18975 convenience for the caller, IT->glyph_row is set to the next
18976 row to be used. */
18977 it->current_x = it->hpos = 0;
18978 it->current_y += row->height;
18979 SET_TEXT_POS (it->eol_pos, 0, 0);
18980 ++it->vpos;
18981 ++it->glyph_row;
18982 /* The next row should by default use the same value of the
18983 reversed_p flag as this one. set_iterator_to_next decides when
18984 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18985 the flag accordingly. */
18986 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18987 it->glyph_row->reversed_p = row->reversed_p;
18988 it->start = row->end;
18989 return row->displays_text_p;
18990
18991 #undef RECORD_MAX_MIN_POS
18992 }
18993
18994 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18995 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18996 doc: /* Return paragraph direction at point in BUFFER.
18997 Value is either `left-to-right' or `right-to-left'.
18998 If BUFFER is omitted or nil, it defaults to the current buffer.
18999
19000 Paragraph direction determines how the text in the paragraph is displayed.
19001 In left-to-right paragraphs, text begins at the left margin of the window
19002 and the reading direction is generally left to right. In right-to-left
19003 paragraphs, text begins at the right margin and is read from right to left.
19004
19005 See also `bidi-paragraph-direction'. */)
19006 (Lisp_Object buffer)
19007 {
19008 struct buffer *buf = current_buffer;
19009 struct buffer *old = buf;
19010
19011 if (! NILP (buffer))
19012 {
19013 CHECK_BUFFER (buffer);
19014 buf = XBUFFER (buffer);
19015 }
19016
19017 if (NILP (BVAR (buf, bidi_display_reordering)))
19018 return Qleft_to_right;
19019 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19020 return BVAR (buf, bidi_paragraph_direction);
19021 else
19022 {
19023 /* Determine the direction from buffer text. We could try to
19024 use current_matrix if it is up to date, but this seems fast
19025 enough as it is. */
19026 struct bidi_it itb;
19027 EMACS_INT pos = BUF_PT (buf);
19028 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19029 int c;
19030
19031 set_buffer_temp (buf);
19032 /* bidi_paragraph_init finds the base direction of the paragraph
19033 by searching forward from paragraph start. We need the base
19034 direction of the current or _previous_ paragraph, so we need
19035 to make sure we are within that paragraph. To that end, find
19036 the previous non-empty line. */
19037 if (pos >= ZV && pos > BEGV)
19038 {
19039 pos--;
19040 bytepos = CHAR_TO_BYTE (pos);
19041 }
19042 while ((c = FETCH_BYTE (bytepos)) == '\n'
19043 || c == ' ' || c == '\t' || c == '\f')
19044 {
19045 if (bytepos <= BEGV_BYTE)
19046 break;
19047 bytepos--;
19048 pos--;
19049 }
19050 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19051 bytepos--;
19052 itb.charpos = pos;
19053 itb.bytepos = bytepos;
19054 itb.nchars = -1;
19055 itb.string.s = NULL;
19056 itb.string.lstring = Qnil;
19057 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
19058 itb.first_elt = 1;
19059 itb.separator_limit = -1;
19060 itb.paragraph_dir = NEUTRAL_DIR;
19061
19062 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19063 set_buffer_temp (old);
19064 switch (itb.paragraph_dir)
19065 {
19066 case L2R:
19067 return Qleft_to_right;
19068 break;
19069 case R2L:
19070 return Qright_to_left;
19071 break;
19072 default:
19073 abort ();
19074 }
19075 }
19076 }
19077
19078
19079 \f
19080 /***********************************************************************
19081 Menu Bar
19082 ***********************************************************************/
19083
19084 /* Redisplay the menu bar in the frame for window W.
19085
19086 The menu bar of X frames that don't have X toolkit support is
19087 displayed in a special window W->frame->menu_bar_window.
19088
19089 The menu bar of terminal frames is treated specially as far as
19090 glyph matrices are concerned. Menu bar lines are not part of
19091 windows, so the update is done directly on the frame matrix rows
19092 for the menu bar. */
19093
19094 static void
19095 display_menu_bar (struct window *w)
19096 {
19097 struct frame *f = XFRAME (WINDOW_FRAME (w));
19098 struct it it;
19099 Lisp_Object items;
19100 int i;
19101
19102 /* Don't do all this for graphical frames. */
19103 #ifdef HAVE_NTGUI
19104 if (FRAME_W32_P (f))
19105 return;
19106 #endif
19107 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19108 if (FRAME_X_P (f))
19109 return;
19110 #endif
19111
19112 #ifdef HAVE_NS
19113 if (FRAME_NS_P (f))
19114 return;
19115 #endif /* HAVE_NS */
19116
19117 #ifdef USE_X_TOOLKIT
19118 xassert (!FRAME_WINDOW_P (f));
19119 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19120 it.first_visible_x = 0;
19121 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19122 #else /* not USE_X_TOOLKIT */
19123 if (FRAME_WINDOW_P (f))
19124 {
19125 /* Menu bar lines are displayed in the desired matrix of the
19126 dummy window menu_bar_window. */
19127 struct window *menu_w;
19128 xassert (WINDOWP (f->menu_bar_window));
19129 menu_w = XWINDOW (f->menu_bar_window);
19130 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19131 MENU_FACE_ID);
19132 it.first_visible_x = 0;
19133 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19134 }
19135 else
19136 {
19137 /* This is a TTY frame, i.e. character hpos/vpos are used as
19138 pixel x/y. */
19139 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19140 MENU_FACE_ID);
19141 it.first_visible_x = 0;
19142 it.last_visible_x = FRAME_COLS (f);
19143 }
19144 #endif /* not USE_X_TOOLKIT */
19145
19146 /* FIXME: This should be controlled by a user option. See the
19147 comments in redisplay_tool_bar and display_mode_line about
19148 this. */
19149 it.paragraph_embedding = L2R;
19150
19151 if (! mode_line_inverse_video)
19152 /* Force the menu-bar to be displayed in the default face. */
19153 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19154
19155 /* Clear all rows of the menu bar. */
19156 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19157 {
19158 struct glyph_row *row = it.glyph_row + i;
19159 clear_glyph_row (row);
19160 row->enabled_p = 1;
19161 row->full_width_p = 1;
19162 }
19163
19164 /* Display all items of the menu bar. */
19165 items = FRAME_MENU_BAR_ITEMS (it.f);
19166 for (i = 0; i < ASIZE (items); i += 4)
19167 {
19168 Lisp_Object string;
19169
19170 /* Stop at nil string. */
19171 string = AREF (items, i + 1);
19172 if (NILP (string))
19173 break;
19174
19175 /* Remember where item was displayed. */
19176 ASET (items, i + 3, make_number (it.hpos));
19177
19178 /* Display the item, pad with one space. */
19179 if (it.current_x < it.last_visible_x)
19180 display_string (NULL, string, Qnil, 0, 0, &it,
19181 SCHARS (string) + 1, 0, 0, -1);
19182 }
19183
19184 /* Fill out the line with spaces. */
19185 if (it.current_x < it.last_visible_x)
19186 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19187
19188 /* Compute the total height of the lines. */
19189 compute_line_metrics (&it);
19190 }
19191
19192
19193 \f
19194 /***********************************************************************
19195 Mode Line
19196 ***********************************************************************/
19197
19198 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19199 FORCE is non-zero, redisplay mode lines unconditionally.
19200 Otherwise, redisplay only mode lines that are garbaged. Value is
19201 the number of windows whose mode lines were redisplayed. */
19202
19203 static int
19204 redisplay_mode_lines (Lisp_Object window, int force)
19205 {
19206 int nwindows = 0;
19207
19208 while (!NILP (window))
19209 {
19210 struct window *w = XWINDOW (window);
19211
19212 if (WINDOWP (w->hchild))
19213 nwindows += redisplay_mode_lines (w->hchild, force);
19214 else if (WINDOWP (w->vchild))
19215 nwindows += redisplay_mode_lines (w->vchild, force);
19216 else if (force
19217 || FRAME_GARBAGED_P (XFRAME (w->frame))
19218 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19219 {
19220 struct text_pos lpoint;
19221 struct buffer *old = current_buffer;
19222
19223 /* Set the window's buffer for the mode line display. */
19224 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19225 set_buffer_internal_1 (XBUFFER (w->buffer));
19226
19227 /* Point refers normally to the selected window. For any
19228 other window, set up appropriate value. */
19229 if (!EQ (window, selected_window))
19230 {
19231 struct text_pos pt;
19232
19233 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19234 if (CHARPOS (pt) < BEGV)
19235 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19236 else if (CHARPOS (pt) > (ZV - 1))
19237 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19238 else
19239 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19240 }
19241
19242 /* Display mode lines. */
19243 clear_glyph_matrix (w->desired_matrix);
19244 if (display_mode_lines (w))
19245 {
19246 ++nwindows;
19247 w->must_be_updated_p = 1;
19248 }
19249
19250 /* Restore old settings. */
19251 set_buffer_internal_1 (old);
19252 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19253 }
19254
19255 window = w->next;
19256 }
19257
19258 return nwindows;
19259 }
19260
19261
19262 /* Display the mode and/or header line of window W. Value is the
19263 sum number of mode lines and header lines displayed. */
19264
19265 static int
19266 display_mode_lines (struct window *w)
19267 {
19268 Lisp_Object old_selected_window, old_selected_frame;
19269 int n = 0;
19270
19271 old_selected_frame = selected_frame;
19272 selected_frame = w->frame;
19273 old_selected_window = selected_window;
19274 XSETWINDOW (selected_window, w);
19275
19276 /* These will be set while the mode line specs are processed. */
19277 line_number_displayed = 0;
19278 w->column_number_displayed = Qnil;
19279
19280 if (WINDOW_WANTS_MODELINE_P (w))
19281 {
19282 struct window *sel_w = XWINDOW (old_selected_window);
19283
19284 /* Select mode line face based on the real selected window. */
19285 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19286 BVAR (current_buffer, mode_line_format));
19287 ++n;
19288 }
19289
19290 if (WINDOW_WANTS_HEADER_LINE_P (w))
19291 {
19292 display_mode_line (w, HEADER_LINE_FACE_ID,
19293 BVAR (current_buffer, header_line_format));
19294 ++n;
19295 }
19296
19297 selected_frame = old_selected_frame;
19298 selected_window = old_selected_window;
19299 return n;
19300 }
19301
19302
19303 /* Display mode or header line of window W. FACE_ID specifies which
19304 line to display; it is either MODE_LINE_FACE_ID or
19305 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19306 display. Value is the pixel height of the mode/header line
19307 displayed. */
19308
19309 static int
19310 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19311 {
19312 struct it it;
19313 struct face *face;
19314 int count = SPECPDL_INDEX ();
19315
19316 init_iterator (&it, w, -1, -1, NULL, face_id);
19317 /* Don't extend on a previously drawn mode-line.
19318 This may happen if called from pos_visible_p. */
19319 it.glyph_row->enabled_p = 0;
19320 prepare_desired_row (it.glyph_row);
19321
19322 it.glyph_row->mode_line_p = 1;
19323
19324 if (! mode_line_inverse_video)
19325 /* Force the mode-line to be displayed in the default face. */
19326 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19327
19328 /* FIXME: This should be controlled by a user option. But
19329 supporting such an option is not trivial, since the mode line is
19330 made up of many separate strings. */
19331 it.paragraph_embedding = L2R;
19332
19333 record_unwind_protect (unwind_format_mode_line,
19334 format_mode_line_unwind_data (NULL, Qnil, 0));
19335
19336 mode_line_target = MODE_LINE_DISPLAY;
19337
19338 /* Temporarily make frame's keyboard the current kboard so that
19339 kboard-local variables in the mode_line_format will get the right
19340 values. */
19341 push_kboard (FRAME_KBOARD (it.f));
19342 record_unwind_save_match_data ();
19343 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19344 pop_kboard ();
19345
19346 unbind_to (count, Qnil);
19347
19348 /* Fill up with spaces. */
19349 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19350
19351 compute_line_metrics (&it);
19352 it.glyph_row->full_width_p = 1;
19353 it.glyph_row->continued_p = 0;
19354 it.glyph_row->truncated_on_left_p = 0;
19355 it.glyph_row->truncated_on_right_p = 0;
19356
19357 /* Make a 3D mode-line have a shadow at its right end. */
19358 face = FACE_FROM_ID (it.f, face_id);
19359 extend_face_to_end_of_line (&it);
19360 if (face->box != FACE_NO_BOX)
19361 {
19362 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19363 + it.glyph_row->used[TEXT_AREA] - 1);
19364 last->right_box_line_p = 1;
19365 }
19366
19367 return it.glyph_row->height;
19368 }
19369
19370 /* Move element ELT in LIST to the front of LIST.
19371 Return the updated list. */
19372
19373 static Lisp_Object
19374 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19375 {
19376 register Lisp_Object tail, prev;
19377 register Lisp_Object tem;
19378
19379 tail = list;
19380 prev = Qnil;
19381 while (CONSP (tail))
19382 {
19383 tem = XCAR (tail);
19384
19385 if (EQ (elt, tem))
19386 {
19387 /* Splice out the link TAIL. */
19388 if (NILP (prev))
19389 list = XCDR (tail);
19390 else
19391 Fsetcdr (prev, XCDR (tail));
19392
19393 /* Now make it the first. */
19394 Fsetcdr (tail, list);
19395 return tail;
19396 }
19397 else
19398 prev = tail;
19399 tail = XCDR (tail);
19400 QUIT;
19401 }
19402
19403 /* Not found--return unchanged LIST. */
19404 return list;
19405 }
19406
19407 /* Contribute ELT to the mode line for window IT->w. How it
19408 translates into text depends on its data type.
19409
19410 IT describes the display environment in which we display, as usual.
19411
19412 DEPTH is the depth in recursion. It is used to prevent
19413 infinite recursion here.
19414
19415 FIELD_WIDTH is the number of characters the display of ELT should
19416 occupy in the mode line, and PRECISION is the maximum number of
19417 characters to display from ELT's representation. See
19418 display_string for details.
19419
19420 Returns the hpos of the end of the text generated by ELT.
19421
19422 PROPS is a property list to add to any string we encounter.
19423
19424 If RISKY is nonzero, remove (disregard) any properties in any string
19425 we encounter, and ignore :eval and :propertize.
19426
19427 The global variable `mode_line_target' determines whether the
19428 output is passed to `store_mode_line_noprop',
19429 `store_mode_line_string', or `display_string'. */
19430
19431 static int
19432 display_mode_element (struct it *it, int depth, int field_width, int precision,
19433 Lisp_Object elt, Lisp_Object props, int risky)
19434 {
19435 int n = 0, field, prec;
19436 int literal = 0;
19437
19438 tail_recurse:
19439 if (depth > 100)
19440 elt = build_string ("*too-deep*");
19441
19442 depth++;
19443
19444 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19445 {
19446 case Lisp_String:
19447 {
19448 /* A string: output it and check for %-constructs within it. */
19449 unsigned char c;
19450 EMACS_INT offset = 0;
19451
19452 if (SCHARS (elt) > 0
19453 && (!NILP (props) || risky))
19454 {
19455 Lisp_Object oprops, aelt;
19456 oprops = Ftext_properties_at (make_number (0), elt);
19457
19458 /* If the starting string's properties are not what
19459 we want, translate the string. Also, if the string
19460 is risky, do that anyway. */
19461
19462 if (NILP (Fequal (props, oprops)) || risky)
19463 {
19464 /* If the starting string has properties,
19465 merge the specified ones onto the existing ones. */
19466 if (! NILP (oprops) && !risky)
19467 {
19468 Lisp_Object tem;
19469
19470 oprops = Fcopy_sequence (oprops);
19471 tem = props;
19472 while (CONSP (tem))
19473 {
19474 oprops = Fplist_put (oprops, XCAR (tem),
19475 XCAR (XCDR (tem)));
19476 tem = XCDR (XCDR (tem));
19477 }
19478 props = oprops;
19479 }
19480
19481 aelt = Fassoc (elt, mode_line_proptrans_alist);
19482 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19483 {
19484 /* AELT is what we want. Move it to the front
19485 without consing. */
19486 elt = XCAR (aelt);
19487 mode_line_proptrans_alist
19488 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19489 }
19490 else
19491 {
19492 Lisp_Object tem;
19493
19494 /* If AELT has the wrong props, it is useless.
19495 so get rid of it. */
19496 if (! NILP (aelt))
19497 mode_line_proptrans_alist
19498 = Fdelq (aelt, mode_line_proptrans_alist);
19499
19500 elt = Fcopy_sequence (elt);
19501 Fset_text_properties (make_number (0), Flength (elt),
19502 props, elt);
19503 /* Add this item to mode_line_proptrans_alist. */
19504 mode_line_proptrans_alist
19505 = Fcons (Fcons (elt, props),
19506 mode_line_proptrans_alist);
19507 /* Truncate mode_line_proptrans_alist
19508 to at most 50 elements. */
19509 tem = Fnthcdr (make_number (50),
19510 mode_line_proptrans_alist);
19511 if (! NILP (tem))
19512 XSETCDR (tem, Qnil);
19513 }
19514 }
19515 }
19516
19517 offset = 0;
19518
19519 if (literal)
19520 {
19521 prec = precision - n;
19522 switch (mode_line_target)
19523 {
19524 case MODE_LINE_NOPROP:
19525 case MODE_LINE_TITLE:
19526 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19527 break;
19528 case MODE_LINE_STRING:
19529 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19530 break;
19531 case MODE_LINE_DISPLAY:
19532 n += display_string (NULL, elt, Qnil, 0, 0, it,
19533 0, prec, 0, STRING_MULTIBYTE (elt));
19534 break;
19535 }
19536
19537 break;
19538 }
19539
19540 /* Handle the non-literal case. */
19541
19542 while ((precision <= 0 || n < precision)
19543 && SREF (elt, offset) != 0
19544 && (mode_line_target != MODE_LINE_DISPLAY
19545 || it->current_x < it->last_visible_x))
19546 {
19547 EMACS_INT last_offset = offset;
19548
19549 /* Advance to end of string or next format specifier. */
19550 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19551 ;
19552
19553 if (offset - 1 != last_offset)
19554 {
19555 EMACS_INT nchars, nbytes;
19556
19557 /* Output to end of string or up to '%'. Field width
19558 is length of string. Don't output more than
19559 PRECISION allows us. */
19560 offset--;
19561
19562 prec = c_string_width (SDATA (elt) + last_offset,
19563 offset - last_offset, precision - n,
19564 &nchars, &nbytes);
19565
19566 switch (mode_line_target)
19567 {
19568 case MODE_LINE_NOPROP:
19569 case MODE_LINE_TITLE:
19570 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19571 break;
19572 case MODE_LINE_STRING:
19573 {
19574 EMACS_INT bytepos = last_offset;
19575 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19576 EMACS_INT endpos = (precision <= 0
19577 ? string_byte_to_char (elt, offset)
19578 : charpos + nchars);
19579
19580 n += store_mode_line_string (NULL,
19581 Fsubstring (elt, make_number (charpos),
19582 make_number (endpos)),
19583 0, 0, 0, Qnil);
19584 }
19585 break;
19586 case MODE_LINE_DISPLAY:
19587 {
19588 EMACS_INT bytepos = last_offset;
19589 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19590
19591 if (precision <= 0)
19592 nchars = string_byte_to_char (elt, offset) - charpos;
19593 n += display_string (NULL, elt, Qnil, 0, charpos,
19594 it, 0, nchars, 0,
19595 STRING_MULTIBYTE (elt));
19596 }
19597 break;
19598 }
19599 }
19600 else /* c == '%' */
19601 {
19602 EMACS_INT percent_position = offset;
19603
19604 /* Get the specified minimum width. Zero means
19605 don't pad. */
19606 field = 0;
19607 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19608 field = field * 10 + c - '0';
19609
19610 /* Don't pad beyond the total padding allowed. */
19611 if (field_width - n > 0 && field > field_width - n)
19612 field = field_width - n;
19613
19614 /* Note that either PRECISION <= 0 or N < PRECISION. */
19615 prec = precision - n;
19616
19617 if (c == 'M')
19618 n += display_mode_element (it, depth, field, prec,
19619 Vglobal_mode_string, props,
19620 risky);
19621 else if (c != 0)
19622 {
19623 int multibyte;
19624 EMACS_INT bytepos, charpos;
19625 const char *spec;
19626 Lisp_Object string;
19627
19628 bytepos = percent_position;
19629 charpos = (STRING_MULTIBYTE (elt)
19630 ? string_byte_to_char (elt, bytepos)
19631 : bytepos);
19632 spec = decode_mode_spec (it->w, c, field, &string);
19633 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19634
19635 switch (mode_line_target)
19636 {
19637 case MODE_LINE_NOPROP:
19638 case MODE_LINE_TITLE:
19639 n += store_mode_line_noprop (spec, field, prec);
19640 break;
19641 case MODE_LINE_STRING:
19642 {
19643 Lisp_Object tem = build_string (spec);
19644 props = Ftext_properties_at (make_number (charpos), elt);
19645 /* Should only keep face property in props */
19646 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19647 }
19648 break;
19649 case MODE_LINE_DISPLAY:
19650 {
19651 int nglyphs_before, nwritten;
19652
19653 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19654 nwritten = display_string (spec, string, elt,
19655 charpos, 0, it,
19656 field, prec, 0,
19657 multibyte);
19658
19659 /* Assign to the glyphs written above the
19660 string where the `%x' came from, position
19661 of the `%'. */
19662 if (nwritten > 0)
19663 {
19664 struct glyph *glyph
19665 = (it->glyph_row->glyphs[TEXT_AREA]
19666 + nglyphs_before);
19667 int i;
19668
19669 for (i = 0; i < nwritten; ++i)
19670 {
19671 glyph[i].object = elt;
19672 glyph[i].charpos = charpos;
19673 }
19674
19675 n += nwritten;
19676 }
19677 }
19678 break;
19679 }
19680 }
19681 else /* c == 0 */
19682 break;
19683 }
19684 }
19685 }
19686 break;
19687
19688 case Lisp_Symbol:
19689 /* A symbol: process the value of the symbol recursively
19690 as if it appeared here directly. Avoid error if symbol void.
19691 Special case: if value of symbol is a string, output the string
19692 literally. */
19693 {
19694 register Lisp_Object tem;
19695
19696 /* If the variable is not marked as risky to set
19697 then its contents are risky to use. */
19698 if (NILP (Fget (elt, Qrisky_local_variable)))
19699 risky = 1;
19700
19701 tem = Fboundp (elt);
19702 if (!NILP (tem))
19703 {
19704 tem = Fsymbol_value (elt);
19705 /* If value is a string, output that string literally:
19706 don't check for % within it. */
19707 if (STRINGP (tem))
19708 literal = 1;
19709
19710 if (!EQ (tem, elt))
19711 {
19712 /* Give up right away for nil or t. */
19713 elt = tem;
19714 goto tail_recurse;
19715 }
19716 }
19717 }
19718 break;
19719
19720 case Lisp_Cons:
19721 {
19722 register Lisp_Object car, tem;
19723
19724 /* A cons cell: five distinct cases.
19725 If first element is :eval or :propertize, do something special.
19726 If first element is a string or a cons, process all the elements
19727 and effectively concatenate them.
19728 If first element is a negative number, truncate displaying cdr to
19729 at most that many characters. If positive, pad (with spaces)
19730 to at least that many characters.
19731 If first element is a symbol, process the cadr or caddr recursively
19732 according to whether the symbol's value is non-nil or nil. */
19733 car = XCAR (elt);
19734 if (EQ (car, QCeval))
19735 {
19736 /* An element of the form (:eval FORM) means evaluate FORM
19737 and use the result as mode line elements. */
19738
19739 if (risky)
19740 break;
19741
19742 if (CONSP (XCDR (elt)))
19743 {
19744 Lisp_Object spec;
19745 spec = safe_eval (XCAR (XCDR (elt)));
19746 n += display_mode_element (it, depth, field_width - n,
19747 precision - n, spec, props,
19748 risky);
19749 }
19750 }
19751 else if (EQ (car, QCpropertize))
19752 {
19753 /* An element of the form (:propertize ELT PROPS...)
19754 means display ELT but applying properties PROPS. */
19755
19756 if (risky)
19757 break;
19758
19759 if (CONSP (XCDR (elt)))
19760 n += display_mode_element (it, depth, field_width - n,
19761 precision - n, XCAR (XCDR (elt)),
19762 XCDR (XCDR (elt)), risky);
19763 }
19764 else if (SYMBOLP (car))
19765 {
19766 tem = Fboundp (car);
19767 elt = XCDR (elt);
19768 if (!CONSP (elt))
19769 goto invalid;
19770 /* elt is now the cdr, and we know it is a cons cell.
19771 Use its car if CAR has a non-nil value. */
19772 if (!NILP (tem))
19773 {
19774 tem = Fsymbol_value (car);
19775 if (!NILP (tem))
19776 {
19777 elt = XCAR (elt);
19778 goto tail_recurse;
19779 }
19780 }
19781 /* Symbol's value is nil (or symbol is unbound)
19782 Get the cddr of the original list
19783 and if possible find the caddr and use that. */
19784 elt = XCDR (elt);
19785 if (NILP (elt))
19786 break;
19787 else if (!CONSP (elt))
19788 goto invalid;
19789 elt = XCAR (elt);
19790 goto tail_recurse;
19791 }
19792 else if (INTEGERP (car))
19793 {
19794 register int lim = XINT (car);
19795 elt = XCDR (elt);
19796 if (lim < 0)
19797 {
19798 /* Negative int means reduce maximum width. */
19799 if (precision <= 0)
19800 precision = -lim;
19801 else
19802 precision = min (precision, -lim);
19803 }
19804 else if (lim > 0)
19805 {
19806 /* Padding specified. Don't let it be more than
19807 current maximum. */
19808 if (precision > 0)
19809 lim = min (precision, lim);
19810
19811 /* If that's more padding than already wanted, queue it.
19812 But don't reduce padding already specified even if
19813 that is beyond the current truncation point. */
19814 field_width = max (lim, field_width);
19815 }
19816 goto tail_recurse;
19817 }
19818 else if (STRINGP (car) || CONSP (car))
19819 {
19820 Lisp_Object halftail = elt;
19821 int len = 0;
19822
19823 while (CONSP (elt)
19824 && (precision <= 0 || n < precision))
19825 {
19826 n += display_mode_element (it, depth,
19827 /* Do padding only after the last
19828 element in the list. */
19829 (! CONSP (XCDR (elt))
19830 ? field_width - n
19831 : 0),
19832 precision - n, XCAR (elt),
19833 props, risky);
19834 elt = XCDR (elt);
19835 len++;
19836 if ((len & 1) == 0)
19837 halftail = XCDR (halftail);
19838 /* Check for cycle. */
19839 if (EQ (halftail, elt))
19840 break;
19841 }
19842 }
19843 }
19844 break;
19845
19846 default:
19847 invalid:
19848 elt = build_string ("*invalid*");
19849 goto tail_recurse;
19850 }
19851
19852 /* Pad to FIELD_WIDTH. */
19853 if (field_width > 0 && n < field_width)
19854 {
19855 switch (mode_line_target)
19856 {
19857 case MODE_LINE_NOPROP:
19858 case MODE_LINE_TITLE:
19859 n += store_mode_line_noprop ("", field_width - n, 0);
19860 break;
19861 case MODE_LINE_STRING:
19862 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19863 break;
19864 case MODE_LINE_DISPLAY:
19865 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19866 0, 0, 0);
19867 break;
19868 }
19869 }
19870
19871 return n;
19872 }
19873
19874 /* Store a mode-line string element in mode_line_string_list.
19875
19876 If STRING is non-null, display that C string. Otherwise, the Lisp
19877 string LISP_STRING is displayed.
19878
19879 FIELD_WIDTH is the minimum number of output glyphs to produce.
19880 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19881 with spaces. FIELD_WIDTH <= 0 means don't pad.
19882
19883 PRECISION is the maximum number of characters to output from
19884 STRING. PRECISION <= 0 means don't truncate the string.
19885
19886 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19887 properties to the string.
19888
19889 PROPS are the properties to add to the string.
19890 The mode_line_string_face face property is always added to the string.
19891 */
19892
19893 static int
19894 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19895 int field_width, int precision, Lisp_Object props)
19896 {
19897 EMACS_INT len;
19898 int n = 0;
19899
19900 if (string != NULL)
19901 {
19902 len = strlen (string);
19903 if (precision > 0 && len > precision)
19904 len = precision;
19905 lisp_string = make_string (string, len);
19906 if (NILP (props))
19907 props = mode_line_string_face_prop;
19908 else if (!NILP (mode_line_string_face))
19909 {
19910 Lisp_Object face = Fplist_get (props, Qface);
19911 props = Fcopy_sequence (props);
19912 if (NILP (face))
19913 face = mode_line_string_face;
19914 else
19915 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19916 props = Fplist_put (props, Qface, face);
19917 }
19918 Fadd_text_properties (make_number (0), make_number (len),
19919 props, lisp_string);
19920 }
19921 else
19922 {
19923 len = XFASTINT (Flength (lisp_string));
19924 if (precision > 0 && len > precision)
19925 {
19926 len = precision;
19927 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19928 precision = -1;
19929 }
19930 if (!NILP (mode_line_string_face))
19931 {
19932 Lisp_Object face;
19933 if (NILP (props))
19934 props = Ftext_properties_at (make_number (0), lisp_string);
19935 face = Fplist_get (props, Qface);
19936 if (NILP (face))
19937 face = mode_line_string_face;
19938 else
19939 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19940 props = Fcons (Qface, Fcons (face, Qnil));
19941 if (copy_string)
19942 lisp_string = Fcopy_sequence (lisp_string);
19943 }
19944 if (!NILP (props))
19945 Fadd_text_properties (make_number (0), make_number (len),
19946 props, lisp_string);
19947 }
19948
19949 if (len > 0)
19950 {
19951 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19952 n += len;
19953 }
19954
19955 if (field_width > len)
19956 {
19957 field_width -= len;
19958 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19959 if (!NILP (props))
19960 Fadd_text_properties (make_number (0), make_number (field_width),
19961 props, lisp_string);
19962 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19963 n += field_width;
19964 }
19965
19966 return n;
19967 }
19968
19969
19970 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19971 1, 4, 0,
19972 doc: /* Format a string out of a mode line format specification.
19973 First arg FORMAT specifies the mode line format (see `mode-line-format'
19974 for details) to use.
19975
19976 By default, the format is evaluated for the currently selected window.
19977
19978 Optional second arg FACE specifies the face property to put on all
19979 characters for which no face is specified. The value nil means the
19980 default face. The value t means whatever face the window's mode line
19981 currently uses (either `mode-line' or `mode-line-inactive',
19982 depending on whether the window is the selected window or not).
19983 An integer value means the value string has no text
19984 properties.
19985
19986 Optional third and fourth args WINDOW and BUFFER specify the window
19987 and buffer to use as the context for the formatting (defaults
19988 are the selected window and the WINDOW's buffer). */)
19989 (Lisp_Object format, Lisp_Object face,
19990 Lisp_Object window, Lisp_Object buffer)
19991 {
19992 struct it it;
19993 int len;
19994 struct window *w;
19995 struct buffer *old_buffer = NULL;
19996 int face_id;
19997 int no_props = INTEGERP (face);
19998 int count = SPECPDL_INDEX ();
19999 Lisp_Object str;
20000 int string_start = 0;
20001
20002 if (NILP (window))
20003 window = selected_window;
20004 CHECK_WINDOW (window);
20005 w = XWINDOW (window);
20006
20007 if (NILP (buffer))
20008 buffer = w->buffer;
20009 CHECK_BUFFER (buffer);
20010
20011 /* Make formatting the modeline a non-op when noninteractive, otherwise
20012 there will be problems later caused by a partially initialized frame. */
20013 if (NILP (format) || noninteractive)
20014 return empty_unibyte_string;
20015
20016 if (no_props)
20017 face = Qnil;
20018
20019 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20020 : EQ (face, Qt) ? (EQ (window, selected_window)
20021 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20022 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20023 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20024 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20025 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20026 : DEFAULT_FACE_ID;
20027
20028 if (XBUFFER (buffer) != current_buffer)
20029 old_buffer = current_buffer;
20030
20031 /* Save things including mode_line_proptrans_alist,
20032 and set that to nil so that we don't alter the outer value. */
20033 record_unwind_protect (unwind_format_mode_line,
20034 format_mode_line_unwind_data
20035 (old_buffer, selected_window, 1));
20036 mode_line_proptrans_alist = Qnil;
20037
20038 Fselect_window (window, Qt);
20039 if (old_buffer)
20040 set_buffer_internal_1 (XBUFFER (buffer));
20041
20042 init_iterator (&it, w, -1, -1, NULL, face_id);
20043
20044 if (no_props)
20045 {
20046 mode_line_target = MODE_LINE_NOPROP;
20047 mode_line_string_face_prop = Qnil;
20048 mode_line_string_list = Qnil;
20049 string_start = MODE_LINE_NOPROP_LEN (0);
20050 }
20051 else
20052 {
20053 mode_line_target = MODE_LINE_STRING;
20054 mode_line_string_list = Qnil;
20055 mode_line_string_face = face;
20056 mode_line_string_face_prop
20057 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20058 }
20059
20060 push_kboard (FRAME_KBOARD (it.f));
20061 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20062 pop_kboard ();
20063
20064 if (no_props)
20065 {
20066 len = MODE_LINE_NOPROP_LEN (string_start);
20067 str = make_string (mode_line_noprop_buf + string_start, len);
20068 }
20069 else
20070 {
20071 mode_line_string_list = Fnreverse (mode_line_string_list);
20072 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20073 empty_unibyte_string);
20074 }
20075
20076 unbind_to (count, Qnil);
20077 return str;
20078 }
20079
20080 /* Write a null-terminated, right justified decimal representation of
20081 the positive integer D to BUF using a minimal field width WIDTH. */
20082
20083 static void
20084 pint2str (register char *buf, register int width, register EMACS_INT d)
20085 {
20086 register char *p = buf;
20087
20088 if (d <= 0)
20089 *p++ = '0';
20090 else
20091 {
20092 while (d > 0)
20093 {
20094 *p++ = d % 10 + '0';
20095 d /= 10;
20096 }
20097 }
20098
20099 for (width -= (int) (p - buf); width > 0; --width)
20100 *p++ = ' ';
20101 *p-- = '\0';
20102 while (p > buf)
20103 {
20104 d = *buf;
20105 *buf++ = *p;
20106 *p-- = d;
20107 }
20108 }
20109
20110 /* Write a null-terminated, right justified decimal and "human
20111 readable" representation of the nonnegative integer D to BUF using
20112 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20113
20114 static const char power_letter[] =
20115 {
20116 0, /* no letter */
20117 'k', /* kilo */
20118 'M', /* mega */
20119 'G', /* giga */
20120 'T', /* tera */
20121 'P', /* peta */
20122 'E', /* exa */
20123 'Z', /* zetta */
20124 'Y' /* yotta */
20125 };
20126
20127 static void
20128 pint2hrstr (char *buf, int width, EMACS_INT d)
20129 {
20130 /* We aim to represent the nonnegative integer D as
20131 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20132 EMACS_INT quotient = d;
20133 int remainder = 0;
20134 /* -1 means: do not use TENTHS. */
20135 int tenths = -1;
20136 int exponent = 0;
20137
20138 /* Length of QUOTIENT.TENTHS as a string. */
20139 int length;
20140
20141 char * psuffix;
20142 char * p;
20143
20144 if (1000 <= quotient)
20145 {
20146 /* Scale to the appropriate EXPONENT. */
20147 do
20148 {
20149 remainder = quotient % 1000;
20150 quotient /= 1000;
20151 exponent++;
20152 }
20153 while (1000 <= quotient);
20154
20155 /* Round to nearest and decide whether to use TENTHS or not. */
20156 if (quotient <= 9)
20157 {
20158 tenths = remainder / 100;
20159 if (50 <= remainder % 100)
20160 {
20161 if (tenths < 9)
20162 tenths++;
20163 else
20164 {
20165 quotient++;
20166 if (quotient == 10)
20167 tenths = -1;
20168 else
20169 tenths = 0;
20170 }
20171 }
20172 }
20173 else
20174 if (500 <= remainder)
20175 {
20176 if (quotient < 999)
20177 quotient++;
20178 else
20179 {
20180 quotient = 1;
20181 exponent++;
20182 tenths = 0;
20183 }
20184 }
20185 }
20186
20187 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20188 if (tenths == -1 && quotient <= 99)
20189 if (quotient <= 9)
20190 length = 1;
20191 else
20192 length = 2;
20193 else
20194 length = 3;
20195 p = psuffix = buf + max (width, length);
20196
20197 /* Print EXPONENT. */
20198 *psuffix++ = power_letter[exponent];
20199 *psuffix = '\0';
20200
20201 /* Print TENTHS. */
20202 if (tenths >= 0)
20203 {
20204 *--p = '0' + tenths;
20205 *--p = '.';
20206 }
20207
20208 /* Print QUOTIENT. */
20209 do
20210 {
20211 int digit = quotient % 10;
20212 *--p = '0' + digit;
20213 }
20214 while ((quotient /= 10) != 0);
20215
20216 /* Print leading spaces. */
20217 while (buf < p)
20218 *--p = ' ';
20219 }
20220
20221 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20222 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20223 type of CODING_SYSTEM. Return updated pointer into BUF. */
20224
20225 static unsigned char invalid_eol_type[] = "(*invalid*)";
20226
20227 static char *
20228 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20229 {
20230 Lisp_Object val;
20231 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20232 const unsigned char *eol_str;
20233 int eol_str_len;
20234 /* The EOL conversion we are using. */
20235 Lisp_Object eoltype;
20236
20237 val = CODING_SYSTEM_SPEC (coding_system);
20238 eoltype = Qnil;
20239
20240 if (!VECTORP (val)) /* Not yet decided. */
20241 {
20242 if (multibyte)
20243 *buf++ = '-';
20244 if (eol_flag)
20245 eoltype = eol_mnemonic_undecided;
20246 /* Don't mention EOL conversion if it isn't decided. */
20247 }
20248 else
20249 {
20250 Lisp_Object attrs;
20251 Lisp_Object eolvalue;
20252
20253 attrs = AREF (val, 0);
20254 eolvalue = AREF (val, 2);
20255
20256 if (multibyte)
20257 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20258
20259 if (eol_flag)
20260 {
20261 /* The EOL conversion that is normal on this system. */
20262
20263 if (NILP (eolvalue)) /* Not yet decided. */
20264 eoltype = eol_mnemonic_undecided;
20265 else if (VECTORP (eolvalue)) /* Not yet decided. */
20266 eoltype = eol_mnemonic_undecided;
20267 else /* eolvalue is Qunix, Qdos, or Qmac. */
20268 eoltype = (EQ (eolvalue, Qunix)
20269 ? eol_mnemonic_unix
20270 : (EQ (eolvalue, Qdos) == 1
20271 ? eol_mnemonic_dos : eol_mnemonic_mac));
20272 }
20273 }
20274
20275 if (eol_flag)
20276 {
20277 /* Mention the EOL conversion if it is not the usual one. */
20278 if (STRINGP (eoltype))
20279 {
20280 eol_str = SDATA (eoltype);
20281 eol_str_len = SBYTES (eoltype);
20282 }
20283 else if (CHARACTERP (eoltype))
20284 {
20285 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20286 int c = XFASTINT (eoltype);
20287 eol_str_len = CHAR_STRING (c, tmp);
20288 eol_str = tmp;
20289 }
20290 else
20291 {
20292 eol_str = invalid_eol_type;
20293 eol_str_len = sizeof (invalid_eol_type) - 1;
20294 }
20295 memcpy (buf, eol_str, eol_str_len);
20296 buf += eol_str_len;
20297 }
20298
20299 return buf;
20300 }
20301
20302 /* Return a string for the output of a mode line %-spec for window W,
20303 generated by character C. FIELD_WIDTH > 0 means pad the string
20304 returned with spaces to that value. Return a Lisp string in
20305 *STRING if the resulting string is taken from that Lisp string.
20306
20307 Note we operate on the current buffer for most purposes,
20308 the exception being w->base_line_pos. */
20309
20310 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20311
20312 static const char *
20313 decode_mode_spec (struct window *w, register int c, int field_width,
20314 Lisp_Object *string)
20315 {
20316 Lisp_Object obj;
20317 struct frame *f = XFRAME (WINDOW_FRAME (w));
20318 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20319 struct buffer *b = current_buffer;
20320
20321 obj = Qnil;
20322 *string = Qnil;
20323
20324 switch (c)
20325 {
20326 case '*':
20327 if (!NILP (BVAR (b, read_only)))
20328 return "%";
20329 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20330 return "*";
20331 return "-";
20332
20333 case '+':
20334 /* This differs from %* only for a modified read-only buffer. */
20335 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20336 return "*";
20337 if (!NILP (BVAR (b, read_only)))
20338 return "%";
20339 return "-";
20340
20341 case '&':
20342 /* This differs from %* in ignoring read-only-ness. */
20343 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20344 return "*";
20345 return "-";
20346
20347 case '%':
20348 return "%";
20349
20350 case '[':
20351 {
20352 int i;
20353 char *p;
20354
20355 if (command_loop_level > 5)
20356 return "[[[... ";
20357 p = decode_mode_spec_buf;
20358 for (i = 0; i < command_loop_level; i++)
20359 *p++ = '[';
20360 *p = 0;
20361 return decode_mode_spec_buf;
20362 }
20363
20364 case ']':
20365 {
20366 int i;
20367 char *p;
20368
20369 if (command_loop_level > 5)
20370 return " ...]]]";
20371 p = decode_mode_spec_buf;
20372 for (i = 0; i < command_loop_level; i++)
20373 *p++ = ']';
20374 *p = 0;
20375 return decode_mode_spec_buf;
20376 }
20377
20378 case '-':
20379 {
20380 register int i;
20381
20382 /* Let lots_of_dashes be a string of infinite length. */
20383 if (mode_line_target == MODE_LINE_NOPROP ||
20384 mode_line_target == MODE_LINE_STRING)
20385 return "--";
20386 if (field_width <= 0
20387 || field_width > sizeof (lots_of_dashes))
20388 {
20389 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20390 decode_mode_spec_buf[i] = '-';
20391 decode_mode_spec_buf[i] = '\0';
20392 return decode_mode_spec_buf;
20393 }
20394 else
20395 return lots_of_dashes;
20396 }
20397
20398 case 'b':
20399 obj = BVAR (b, name);
20400 break;
20401
20402 case 'c':
20403 /* %c and %l are ignored in `frame-title-format'.
20404 (In redisplay_internal, the frame title is drawn _before_ the
20405 windows are updated, so the stuff which depends on actual
20406 window contents (such as %l) may fail to render properly, or
20407 even crash emacs.) */
20408 if (mode_line_target == MODE_LINE_TITLE)
20409 return "";
20410 else
20411 {
20412 EMACS_INT col = current_column ();
20413 w->column_number_displayed = make_number (col);
20414 pint2str (decode_mode_spec_buf, field_width, col);
20415 return decode_mode_spec_buf;
20416 }
20417
20418 case 'e':
20419 #ifndef SYSTEM_MALLOC
20420 {
20421 if (NILP (Vmemory_full))
20422 return "";
20423 else
20424 return "!MEM FULL! ";
20425 }
20426 #else
20427 return "";
20428 #endif
20429
20430 case 'F':
20431 /* %F displays the frame name. */
20432 if (!NILP (f->title))
20433 return SSDATA (f->title);
20434 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20435 return SSDATA (f->name);
20436 return "Emacs";
20437
20438 case 'f':
20439 obj = BVAR (b, filename);
20440 break;
20441
20442 case 'i':
20443 {
20444 EMACS_INT size = ZV - BEGV;
20445 pint2str (decode_mode_spec_buf, field_width, size);
20446 return decode_mode_spec_buf;
20447 }
20448
20449 case 'I':
20450 {
20451 EMACS_INT size = ZV - BEGV;
20452 pint2hrstr (decode_mode_spec_buf, field_width, size);
20453 return decode_mode_spec_buf;
20454 }
20455
20456 case 'l':
20457 {
20458 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20459 EMACS_INT topline, nlines, height;
20460 EMACS_INT junk;
20461
20462 /* %c and %l are ignored in `frame-title-format'. */
20463 if (mode_line_target == MODE_LINE_TITLE)
20464 return "";
20465
20466 startpos = XMARKER (w->start)->charpos;
20467 startpos_byte = marker_byte_position (w->start);
20468 height = WINDOW_TOTAL_LINES (w);
20469
20470 /* If we decided that this buffer isn't suitable for line numbers,
20471 don't forget that too fast. */
20472 if (EQ (w->base_line_pos, w->buffer))
20473 goto no_value;
20474 /* But do forget it, if the window shows a different buffer now. */
20475 else if (BUFFERP (w->base_line_pos))
20476 w->base_line_pos = Qnil;
20477
20478 /* If the buffer is very big, don't waste time. */
20479 if (INTEGERP (Vline_number_display_limit)
20480 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20481 {
20482 w->base_line_pos = Qnil;
20483 w->base_line_number = Qnil;
20484 goto no_value;
20485 }
20486
20487 if (INTEGERP (w->base_line_number)
20488 && INTEGERP (w->base_line_pos)
20489 && XFASTINT (w->base_line_pos) <= startpos)
20490 {
20491 line = XFASTINT (w->base_line_number);
20492 linepos = XFASTINT (w->base_line_pos);
20493 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20494 }
20495 else
20496 {
20497 line = 1;
20498 linepos = BUF_BEGV (b);
20499 linepos_byte = BUF_BEGV_BYTE (b);
20500 }
20501
20502 /* Count lines from base line to window start position. */
20503 nlines = display_count_lines (linepos_byte,
20504 startpos_byte,
20505 startpos, &junk);
20506
20507 topline = nlines + line;
20508
20509 /* Determine a new base line, if the old one is too close
20510 or too far away, or if we did not have one.
20511 "Too close" means it's plausible a scroll-down would
20512 go back past it. */
20513 if (startpos == BUF_BEGV (b))
20514 {
20515 w->base_line_number = make_number (topline);
20516 w->base_line_pos = make_number (BUF_BEGV (b));
20517 }
20518 else if (nlines < height + 25 || nlines > height * 3 + 50
20519 || linepos == BUF_BEGV (b))
20520 {
20521 EMACS_INT limit = BUF_BEGV (b);
20522 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20523 EMACS_INT position;
20524 EMACS_INT distance =
20525 (height * 2 + 30) * line_number_display_limit_width;
20526
20527 if (startpos - distance > limit)
20528 {
20529 limit = startpos - distance;
20530 limit_byte = CHAR_TO_BYTE (limit);
20531 }
20532
20533 nlines = display_count_lines (startpos_byte,
20534 limit_byte,
20535 - (height * 2 + 30),
20536 &position);
20537 /* If we couldn't find the lines we wanted within
20538 line_number_display_limit_width chars per line,
20539 give up on line numbers for this window. */
20540 if (position == limit_byte && limit == startpos - distance)
20541 {
20542 w->base_line_pos = w->buffer;
20543 w->base_line_number = Qnil;
20544 goto no_value;
20545 }
20546
20547 w->base_line_number = make_number (topline - nlines);
20548 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20549 }
20550
20551 /* Now count lines from the start pos to point. */
20552 nlines = display_count_lines (startpos_byte,
20553 PT_BYTE, PT, &junk);
20554
20555 /* Record that we did display the line number. */
20556 line_number_displayed = 1;
20557
20558 /* Make the string to show. */
20559 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20560 return decode_mode_spec_buf;
20561 no_value:
20562 {
20563 char* p = decode_mode_spec_buf;
20564 int pad = field_width - 2;
20565 while (pad-- > 0)
20566 *p++ = ' ';
20567 *p++ = '?';
20568 *p++ = '?';
20569 *p = '\0';
20570 return decode_mode_spec_buf;
20571 }
20572 }
20573 break;
20574
20575 case 'm':
20576 obj = BVAR (b, mode_name);
20577 break;
20578
20579 case 'n':
20580 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20581 return " Narrow";
20582 break;
20583
20584 case 'p':
20585 {
20586 EMACS_INT pos = marker_position (w->start);
20587 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20588
20589 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20590 {
20591 if (pos <= BUF_BEGV (b))
20592 return "All";
20593 else
20594 return "Bottom";
20595 }
20596 else if (pos <= BUF_BEGV (b))
20597 return "Top";
20598 else
20599 {
20600 if (total > 1000000)
20601 /* Do it differently for a large value, to avoid overflow. */
20602 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20603 else
20604 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20605 /* We can't normally display a 3-digit number,
20606 so get us a 2-digit number that is close. */
20607 if (total == 100)
20608 total = 99;
20609 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20610 return decode_mode_spec_buf;
20611 }
20612 }
20613
20614 /* Display percentage of size above the bottom of the screen. */
20615 case 'P':
20616 {
20617 EMACS_INT toppos = marker_position (w->start);
20618 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20619 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20620
20621 if (botpos >= BUF_ZV (b))
20622 {
20623 if (toppos <= BUF_BEGV (b))
20624 return "All";
20625 else
20626 return "Bottom";
20627 }
20628 else
20629 {
20630 if (total > 1000000)
20631 /* Do it differently for a large value, to avoid overflow. */
20632 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20633 else
20634 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20635 /* We can't normally display a 3-digit number,
20636 so get us a 2-digit number that is close. */
20637 if (total == 100)
20638 total = 99;
20639 if (toppos <= BUF_BEGV (b))
20640 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20641 else
20642 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20643 return decode_mode_spec_buf;
20644 }
20645 }
20646
20647 case 's':
20648 /* status of process */
20649 obj = Fget_buffer_process (Fcurrent_buffer ());
20650 if (NILP (obj))
20651 return "no process";
20652 #ifndef MSDOS
20653 obj = Fsymbol_name (Fprocess_status (obj));
20654 #endif
20655 break;
20656
20657 case '@':
20658 {
20659 int count = inhibit_garbage_collection ();
20660 Lisp_Object val = call1 (intern ("file-remote-p"),
20661 BVAR (current_buffer, directory));
20662 unbind_to (count, Qnil);
20663
20664 if (NILP (val))
20665 return "-";
20666 else
20667 return "@";
20668 }
20669
20670 case 't': /* indicate TEXT or BINARY */
20671 return "T";
20672
20673 case 'z':
20674 /* coding-system (not including end-of-line format) */
20675 case 'Z':
20676 /* coding-system (including end-of-line type) */
20677 {
20678 int eol_flag = (c == 'Z');
20679 char *p = decode_mode_spec_buf;
20680
20681 if (! FRAME_WINDOW_P (f))
20682 {
20683 /* No need to mention EOL here--the terminal never needs
20684 to do EOL conversion. */
20685 p = decode_mode_spec_coding (CODING_ID_NAME
20686 (FRAME_KEYBOARD_CODING (f)->id),
20687 p, 0);
20688 p = decode_mode_spec_coding (CODING_ID_NAME
20689 (FRAME_TERMINAL_CODING (f)->id),
20690 p, 0);
20691 }
20692 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20693 p, eol_flag);
20694
20695 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20696 #ifdef subprocesses
20697 obj = Fget_buffer_process (Fcurrent_buffer ());
20698 if (PROCESSP (obj))
20699 {
20700 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20701 p, eol_flag);
20702 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20703 p, eol_flag);
20704 }
20705 #endif /* subprocesses */
20706 #endif /* 0 */
20707 *p = 0;
20708 return decode_mode_spec_buf;
20709 }
20710 }
20711
20712 if (STRINGP (obj))
20713 {
20714 *string = obj;
20715 return SSDATA (obj);
20716 }
20717 else
20718 return "";
20719 }
20720
20721
20722 /* Count up to COUNT lines starting from START_BYTE.
20723 But don't go beyond LIMIT_BYTE.
20724 Return the number of lines thus found (always nonnegative).
20725
20726 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20727
20728 static EMACS_INT
20729 display_count_lines (EMACS_INT start_byte,
20730 EMACS_INT limit_byte, EMACS_INT count,
20731 EMACS_INT *byte_pos_ptr)
20732 {
20733 register unsigned char *cursor;
20734 unsigned char *base;
20735
20736 register EMACS_INT ceiling;
20737 register unsigned char *ceiling_addr;
20738 EMACS_INT orig_count = count;
20739
20740 /* If we are not in selective display mode,
20741 check only for newlines. */
20742 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20743 && !INTEGERP (BVAR (current_buffer, selective_display)));
20744
20745 if (count > 0)
20746 {
20747 while (start_byte < limit_byte)
20748 {
20749 ceiling = BUFFER_CEILING_OF (start_byte);
20750 ceiling = min (limit_byte - 1, ceiling);
20751 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20752 base = (cursor = BYTE_POS_ADDR (start_byte));
20753 while (1)
20754 {
20755 if (selective_display)
20756 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20757 ;
20758 else
20759 while (*cursor != '\n' && ++cursor != ceiling_addr)
20760 ;
20761
20762 if (cursor != ceiling_addr)
20763 {
20764 if (--count == 0)
20765 {
20766 start_byte += cursor - base + 1;
20767 *byte_pos_ptr = start_byte;
20768 return orig_count;
20769 }
20770 else
20771 if (++cursor == ceiling_addr)
20772 break;
20773 }
20774 else
20775 break;
20776 }
20777 start_byte += cursor - base;
20778 }
20779 }
20780 else
20781 {
20782 while (start_byte > limit_byte)
20783 {
20784 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20785 ceiling = max (limit_byte, ceiling);
20786 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20787 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20788 while (1)
20789 {
20790 if (selective_display)
20791 while (--cursor != ceiling_addr
20792 && *cursor != '\n' && *cursor != 015)
20793 ;
20794 else
20795 while (--cursor != ceiling_addr && *cursor != '\n')
20796 ;
20797
20798 if (cursor != ceiling_addr)
20799 {
20800 if (++count == 0)
20801 {
20802 start_byte += cursor - base + 1;
20803 *byte_pos_ptr = start_byte;
20804 /* When scanning backwards, we should
20805 not count the newline posterior to which we stop. */
20806 return - orig_count - 1;
20807 }
20808 }
20809 else
20810 break;
20811 }
20812 /* Here we add 1 to compensate for the last decrement
20813 of CURSOR, which took it past the valid range. */
20814 start_byte += cursor - base + 1;
20815 }
20816 }
20817
20818 *byte_pos_ptr = limit_byte;
20819
20820 if (count < 0)
20821 return - orig_count + count;
20822 return orig_count - count;
20823
20824 }
20825
20826
20827 \f
20828 /***********************************************************************
20829 Displaying strings
20830 ***********************************************************************/
20831
20832 /* Display a NUL-terminated string, starting with index START.
20833
20834 If STRING is non-null, display that C string. Otherwise, the Lisp
20835 string LISP_STRING is displayed. There's a case that STRING is
20836 non-null and LISP_STRING is not nil. It means STRING is a string
20837 data of LISP_STRING. In that case, we display LISP_STRING while
20838 ignoring its text properties.
20839
20840 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20841 FACE_STRING. Display STRING or LISP_STRING with the face at
20842 FACE_STRING_POS in FACE_STRING:
20843
20844 Display the string in the environment given by IT, but use the
20845 standard display table, temporarily.
20846
20847 FIELD_WIDTH is the minimum number of output glyphs to produce.
20848 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20849 with spaces. If STRING has more characters, more than FIELD_WIDTH
20850 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20851
20852 PRECISION is the maximum number of characters to output from
20853 STRING. PRECISION < 0 means don't truncate the string.
20854
20855 This is roughly equivalent to printf format specifiers:
20856
20857 FIELD_WIDTH PRECISION PRINTF
20858 ----------------------------------------
20859 -1 -1 %s
20860 -1 10 %.10s
20861 10 -1 %10s
20862 20 10 %20.10s
20863
20864 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20865 display them, and < 0 means obey the current buffer's value of
20866 enable_multibyte_characters.
20867
20868 Value is the number of columns displayed. */
20869
20870 static int
20871 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20872 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20873 int field_width, int precision, int max_x, int multibyte)
20874 {
20875 int hpos_at_start = it->hpos;
20876 int saved_face_id = it->face_id;
20877 struct glyph_row *row = it->glyph_row;
20878 EMACS_INT it_charpos;
20879
20880 /* Initialize the iterator IT for iteration over STRING beginning
20881 with index START. */
20882 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20883 precision, field_width, multibyte);
20884 if (string && STRINGP (lisp_string))
20885 /* LISP_STRING is the one returned by decode_mode_spec. We should
20886 ignore its text properties. */
20887 it->stop_charpos = it->end_charpos;
20888
20889 /* If displaying STRING, set up the face of the iterator from
20890 FACE_STRING, if that's given. */
20891 if (STRINGP (face_string))
20892 {
20893 EMACS_INT endptr;
20894 struct face *face;
20895
20896 it->face_id
20897 = face_at_string_position (it->w, face_string, face_string_pos,
20898 0, it->region_beg_charpos,
20899 it->region_end_charpos,
20900 &endptr, it->base_face_id, 0);
20901 face = FACE_FROM_ID (it->f, it->face_id);
20902 it->face_box_p = face->box != FACE_NO_BOX;
20903 }
20904
20905 /* Set max_x to the maximum allowed X position. Don't let it go
20906 beyond the right edge of the window. */
20907 if (max_x <= 0)
20908 max_x = it->last_visible_x;
20909 else
20910 max_x = min (max_x, it->last_visible_x);
20911
20912 /* Skip over display elements that are not visible. because IT->w is
20913 hscrolled. */
20914 if (it->current_x < it->first_visible_x)
20915 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20916 MOVE_TO_POS | MOVE_TO_X);
20917
20918 row->ascent = it->max_ascent;
20919 row->height = it->max_ascent + it->max_descent;
20920 row->phys_ascent = it->max_phys_ascent;
20921 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20922 row->extra_line_spacing = it->max_extra_line_spacing;
20923
20924 if (STRINGP (it->string))
20925 it_charpos = IT_STRING_CHARPOS (*it);
20926 else
20927 it_charpos = IT_CHARPOS (*it);
20928
20929 /* This condition is for the case that we are called with current_x
20930 past last_visible_x. */
20931 while (it->current_x < max_x)
20932 {
20933 int x_before, x, n_glyphs_before, i, nglyphs;
20934
20935 /* Get the next display element. */
20936 if (!get_next_display_element (it))
20937 break;
20938
20939 /* Produce glyphs. */
20940 x_before = it->current_x;
20941 n_glyphs_before = row->used[TEXT_AREA];
20942 PRODUCE_GLYPHS (it);
20943
20944 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20945 i = 0;
20946 x = x_before;
20947 while (i < nglyphs)
20948 {
20949 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20950
20951 if (it->line_wrap != TRUNCATE
20952 && x + glyph->pixel_width > max_x)
20953 {
20954 /* End of continued line or max_x reached. */
20955 if (CHAR_GLYPH_PADDING_P (*glyph))
20956 {
20957 /* A wide character is unbreakable. */
20958 if (row->reversed_p)
20959 unproduce_glyphs (it, row->used[TEXT_AREA]
20960 - n_glyphs_before);
20961 row->used[TEXT_AREA] = n_glyphs_before;
20962 it->current_x = x_before;
20963 }
20964 else
20965 {
20966 if (row->reversed_p)
20967 unproduce_glyphs (it, row->used[TEXT_AREA]
20968 - (n_glyphs_before + i));
20969 row->used[TEXT_AREA] = n_glyphs_before + i;
20970 it->current_x = x;
20971 }
20972 break;
20973 }
20974 else if (x + glyph->pixel_width >= it->first_visible_x)
20975 {
20976 /* Glyph is at least partially visible. */
20977 ++it->hpos;
20978 if (x < it->first_visible_x)
20979 row->x = x - it->first_visible_x;
20980 }
20981 else
20982 {
20983 /* Glyph is off the left margin of the display area.
20984 Should not happen. */
20985 abort ();
20986 }
20987
20988 row->ascent = max (row->ascent, it->max_ascent);
20989 row->height = max (row->height, it->max_ascent + it->max_descent);
20990 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20991 row->phys_height = max (row->phys_height,
20992 it->max_phys_ascent + it->max_phys_descent);
20993 row->extra_line_spacing = max (row->extra_line_spacing,
20994 it->max_extra_line_spacing);
20995 x += glyph->pixel_width;
20996 ++i;
20997 }
20998
20999 /* Stop if max_x reached. */
21000 if (i < nglyphs)
21001 break;
21002
21003 /* Stop at line ends. */
21004 if (ITERATOR_AT_END_OF_LINE_P (it))
21005 {
21006 it->continuation_lines_width = 0;
21007 break;
21008 }
21009
21010 set_iterator_to_next (it, 1);
21011 if (STRINGP (it->string))
21012 it_charpos = IT_STRING_CHARPOS (*it);
21013 else
21014 it_charpos = IT_CHARPOS (*it);
21015
21016 /* Stop if truncating at the right edge. */
21017 if (it->line_wrap == TRUNCATE
21018 && it->current_x >= it->last_visible_x)
21019 {
21020 /* Add truncation mark, but don't do it if the line is
21021 truncated at a padding space. */
21022 if (it_charpos < it->string_nchars)
21023 {
21024 if (!FRAME_WINDOW_P (it->f))
21025 {
21026 int ii, n;
21027
21028 if (it->current_x > it->last_visible_x)
21029 {
21030 if (!row->reversed_p)
21031 {
21032 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21033 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21034 break;
21035 }
21036 else
21037 {
21038 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21039 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21040 break;
21041 unproduce_glyphs (it, ii + 1);
21042 ii = row->used[TEXT_AREA] - (ii + 1);
21043 }
21044 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21045 {
21046 row->used[TEXT_AREA] = ii;
21047 produce_special_glyphs (it, IT_TRUNCATION);
21048 }
21049 }
21050 produce_special_glyphs (it, IT_TRUNCATION);
21051 }
21052 row->truncated_on_right_p = 1;
21053 }
21054 break;
21055 }
21056 }
21057
21058 /* Maybe insert a truncation at the left. */
21059 if (it->first_visible_x
21060 && it_charpos > 0)
21061 {
21062 if (!FRAME_WINDOW_P (it->f))
21063 insert_left_trunc_glyphs (it);
21064 row->truncated_on_left_p = 1;
21065 }
21066
21067 it->face_id = saved_face_id;
21068
21069 /* Value is number of columns displayed. */
21070 return it->hpos - hpos_at_start;
21071 }
21072
21073
21074 \f
21075 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21076 appears as an element of LIST or as the car of an element of LIST.
21077 If PROPVAL is a list, compare each element against LIST in that
21078 way, and return 1/2 if any element of PROPVAL is found in LIST.
21079 Otherwise return 0. This function cannot quit.
21080 The return value is 2 if the text is invisible but with an ellipsis
21081 and 1 if it's invisible and without an ellipsis. */
21082
21083 int
21084 invisible_p (register Lisp_Object propval, Lisp_Object list)
21085 {
21086 register Lisp_Object tail, proptail;
21087
21088 for (tail = list; CONSP (tail); tail = XCDR (tail))
21089 {
21090 register Lisp_Object tem;
21091 tem = XCAR (tail);
21092 if (EQ (propval, tem))
21093 return 1;
21094 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21095 return NILP (XCDR (tem)) ? 1 : 2;
21096 }
21097
21098 if (CONSP (propval))
21099 {
21100 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21101 {
21102 Lisp_Object propelt;
21103 propelt = XCAR (proptail);
21104 for (tail = list; CONSP (tail); tail = XCDR (tail))
21105 {
21106 register Lisp_Object tem;
21107 tem = XCAR (tail);
21108 if (EQ (propelt, tem))
21109 return 1;
21110 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21111 return NILP (XCDR (tem)) ? 1 : 2;
21112 }
21113 }
21114 }
21115
21116 return 0;
21117 }
21118
21119 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21120 doc: /* Non-nil if the property makes the text invisible.
21121 POS-OR-PROP can be a marker or number, in which case it is taken to be
21122 a position in the current buffer and the value of the `invisible' property
21123 is checked; or it can be some other value, which is then presumed to be the
21124 value of the `invisible' property of the text of interest.
21125 The non-nil value returned can be t for truly invisible text or something
21126 else if the text is replaced by an ellipsis. */)
21127 (Lisp_Object pos_or_prop)
21128 {
21129 Lisp_Object prop
21130 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21131 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21132 : pos_or_prop);
21133 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21134 return (invis == 0 ? Qnil
21135 : invis == 1 ? Qt
21136 : make_number (invis));
21137 }
21138
21139 /* Calculate a width or height in pixels from a specification using
21140 the following elements:
21141
21142 SPEC ::=
21143 NUM - a (fractional) multiple of the default font width/height
21144 (NUM) - specifies exactly NUM pixels
21145 UNIT - a fixed number of pixels, see below.
21146 ELEMENT - size of a display element in pixels, see below.
21147 (NUM . SPEC) - equals NUM * SPEC
21148 (+ SPEC SPEC ...) - add pixel values
21149 (- SPEC SPEC ...) - subtract pixel values
21150 (- SPEC) - negate pixel value
21151
21152 NUM ::=
21153 INT or FLOAT - a number constant
21154 SYMBOL - use symbol's (buffer local) variable binding.
21155
21156 UNIT ::=
21157 in - pixels per inch *)
21158 mm - pixels per 1/1000 meter *)
21159 cm - pixels per 1/100 meter *)
21160 width - width of current font in pixels.
21161 height - height of current font in pixels.
21162
21163 *) using the ratio(s) defined in display-pixels-per-inch.
21164
21165 ELEMENT ::=
21166
21167 left-fringe - left fringe width in pixels
21168 right-fringe - right fringe width in pixels
21169
21170 left-margin - left margin width in pixels
21171 right-margin - right margin width in pixels
21172
21173 scroll-bar - scroll-bar area width in pixels
21174
21175 Examples:
21176
21177 Pixels corresponding to 5 inches:
21178 (5 . in)
21179
21180 Total width of non-text areas on left side of window (if scroll-bar is on left):
21181 '(space :width (+ left-fringe left-margin scroll-bar))
21182
21183 Align to first text column (in header line):
21184 '(space :align-to 0)
21185
21186 Align to middle of text area minus half the width of variable `my-image'
21187 containing a loaded image:
21188 '(space :align-to (0.5 . (- text my-image)))
21189
21190 Width of left margin minus width of 1 character in the default font:
21191 '(space :width (- left-margin 1))
21192
21193 Width of left margin minus width of 2 characters in the current font:
21194 '(space :width (- left-margin (2 . width)))
21195
21196 Center 1 character over left-margin (in header line):
21197 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21198
21199 Different ways to express width of left fringe plus left margin minus one pixel:
21200 '(space :width (- (+ left-fringe left-margin) (1)))
21201 '(space :width (+ left-fringe left-margin (- (1))))
21202 '(space :width (+ left-fringe left-margin (-1)))
21203
21204 */
21205
21206 #define NUMVAL(X) \
21207 ((INTEGERP (X) || FLOATP (X)) \
21208 ? XFLOATINT (X) \
21209 : - 1)
21210
21211 int
21212 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21213 struct font *font, int width_p, int *align_to)
21214 {
21215 double pixels;
21216
21217 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21218 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21219
21220 if (NILP (prop))
21221 return OK_PIXELS (0);
21222
21223 xassert (FRAME_LIVE_P (it->f));
21224
21225 if (SYMBOLP (prop))
21226 {
21227 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21228 {
21229 char *unit = SSDATA (SYMBOL_NAME (prop));
21230
21231 if (unit[0] == 'i' && unit[1] == 'n')
21232 pixels = 1.0;
21233 else if (unit[0] == 'm' && unit[1] == 'm')
21234 pixels = 25.4;
21235 else if (unit[0] == 'c' && unit[1] == 'm')
21236 pixels = 2.54;
21237 else
21238 pixels = 0;
21239 if (pixels > 0)
21240 {
21241 double ppi;
21242 #ifdef HAVE_WINDOW_SYSTEM
21243 if (FRAME_WINDOW_P (it->f)
21244 && (ppi = (width_p
21245 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21246 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21247 ppi > 0))
21248 return OK_PIXELS (ppi / pixels);
21249 #endif
21250
21251 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21252 || (CONSP (Vdisplay_pixels_per_inch)
21253 && (ppi = (width_p
21254 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21255 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21256 ppi > 0)))
21257 return OK_PIXELS (ppi / pixels);
21258
21259 return 0;
21260 }
21261 }
21262
21263 #ifdef HAVE_WINDOW_SYSTEM
21264 if (EQ (prop, Qheight))
21265 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21266 if (EQ (prop, Qwidth))
21267 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21268 #else
21269 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21270 return OK_PIXELS (1);
21271 #endif
21272
21273 if (EQ (prop, Qtext))
21274 return OK_PIXELS (width_p
21275 ? window_box_width (it->w, TEXT_AREA)
21276 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21277
21278 if (align_to && *align_to < 0)
21279 {
21280 *res = 0;
21281 if (EQ (prop, Qleft))
21282 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21283 if (EQ (prop, Qright))
21284 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21285 if (EQ (prop, Qcenter))
21286 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21287 + window_box_width (it->w, TEXT_AREA) / 2);
21288 if (EQ (prop, Qleft_fringe))
21289 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21290 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21291 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21292 if (EQ (prop, Qright_fringe))
21293 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21294 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21295 : window_box_right_offset (it->w, TEXT_AREA));
21296 if (EQ (prop, Qleft_margin))
21297 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21298 if (EQ (prop, Qright_margin))
21299 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21300 if (EQ (prop, Qscroll_bar))
21301 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21302 ? 0
21303 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21304 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21305 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21306 : 0)));
21307 }
21308 else
21309 {
21310 if (EQ (prop, Qleft_fringe))
21311 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21312 if (EQ (prop, Qright_fringe))
21313 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21314 if (EQ (prop, Qleft_margin))
21315 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21316 if (EQ (prop, Qright_margin))
21317 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21318 if (EQ (prop, Qscroll_bar))
21319 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21320 }
21321
21322 prop = Fbuffer_local_value (prop, it->w->buffer);
21323 }
21324
21325 if (INTEGERP (prop) || FLOATP (prop))
21326 {
21327 int base_unit = (width_p
21328 ? FRAME_COLUMN_WIDTH (it->f)
21329 : FRAME_LINE_HEIGHT (it->f));
21330 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21331 }
21332
21333 if (CONSP (prop))
21334 {
21335 Lisp_Object car = XCAR (prop);
21336 Lisp_Object cdr = XCDR (prop);
21337
21338 if (SYMBOLP (car))
21339 {
21340 #ifdef HAVE_WINDOW_SYSTEM
21341 if (FRAME_WINDOW_P (it->f)
21342 && valid_image_p (prop))
21343 {
21344 int id = lookup_image (it->f, prop);
21345 struct image *img = IMAGE_FROM_ID (it->f, id);
21346
21347 return OK_PIXELS (width_p ? img->width : img->height);
21348 }
21349 #endif
21350 if (EQ (car, Qplus) || EQ (car, Qminus))
21351 {
21352 int first = 1;
21353 double px;
21354
21355 pixels = 0;
21356 while (CONSP (cdr))
21357 {
21358 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21359 font, width_p, align_to))
21360 return 0;
21361 if (first)
21362 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21363 else
21364 pixels += px;
21365 cdr = XCDR (cdr);
21366 }
21367 if (EQ (car, Qminus))
21368 pixels = -pixels;
21369 return OK_PIXELS (pixels);
21370 }
21371
21372 car = Fbuffer_local_value (car, it->w->buffer);
21373 }
21374
21375 if (INTEGERP (car) || FLOATP (car))
21376 {
21377 double fact;
21378 pixels = XFLOATINT (car);
21379 if (NILP (cdr))
21380 return OK_PIXELS (pixels);
21381 if (calc_pixel_width_or_height (&fact, it, cdr,
21382 font, width_p, align_to))
21383 return OK_PIXELS (pixels * fact);
21384 return 0;
21385 }
21386
21387 return 0;
21388 }
21389
21390 return 0;
21391 }
21392
21393 \f
21394 /***********************************************************************
21395 Glyph Display
21396 ***********************************************************************/
21397
21398 #ifdef HAVE_WINDOW_SYSTEM
21399
21400 #if GLYPH_DEBUG
21401
21402 void
21403 dump_glyph_string (struct glyph_string *s)
21404 {
21405 fprintf (stderr, "glyph string\n");
21406 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21407 s->x, s->y, s->width, s->height);
21408 fprintf (stderr, " ybase = %d\n", s->ybase);
21409 fprintf (stderr, " hl = %d\n", s->hl);
21410 fprintf (stderr, " left overhang = %d, right = %d\n",
21411 s->left_overhang, s->right_overhang);
21412 fprintf (stderr, " nchars = %d\n", s->nchars);
21413 fprintf (stderr, " extends to end of line = %d\n",
21414 s->extends_to_end_of_line_p);
21415 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21416 fprintf (stderr, " bg width = %d\n", s->background_width);
21417 }
21418
21419 #endif /* GLYPH_DEBUG */
21420
21421 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21422 of XChar2b structures for S; it can't be allocated in
21423 init_glyph_string because it must be allocated via `alloca'. W
21424 is the window on which S is drawn. ROW and AREA are the glyph row
21425 and area within the row from which S is constructed. START is the
21426 index of the first glyph structure covered by S. HL is a
21427 face-override for drawing S. */
21428
21429 #ifdef HAVE_NTGUI
21430 #define OPTIONAL_HDC(hdc) HDC hdc,
21431 #define DECLARE_HDC(hdc) HDC hdc;
21432 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21433 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21434 #endif
21435
21436 #ifndef OPTIONAL_HDC
21437 #define OPTIONAL_HDC(hdc)
21438 #define DECLARE_HDC(hdc)
21439 #define ALLOCATE_HDC(hdc, f)
21440 #define RELEASE_HDC(hdc, f)
21441 #endif
21442
21443 static void
21444 init_glyph_string (struct glyph_string *s,
21445 OPTIONAL_HDC (hdc)
21446 XChar2b *char2b, struct window *w, struct glyph_row *row,
21447 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21448 {
21449 memset (s, 0, sizeof *s);
21450 s->w = w;
21451 s->f = XFRAME (w->frame);
21452 #ifdef HAVE_NTGUI
21453 s->hdc = hdc;
21454 #endif
21455 s->display = FRAME_X_DISPLAY (s->f);
21456 s->window = FRAME_X_WINDOW (s->f);
21457 s->char2b = char2b;
21458 s->hl = hl;
21459 s->row = row;
21460 s->area = area;
21461 s->first_glyph = row->glyphs[area] + start;
21462 s->height = row->height;
21463 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21464 s->ybase = s->y + row->ascent;
21465 }
21466
21467
21468 /* Append the list of glyph strings with head H and tail T to the list
21469 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21470
21471 static inline void
21472 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21473 struct glyph_string *h, struct glyph_string *t)
21474 {
21475 if (h)
21476 {
21477 if (*head)
21478 (*tail)->next = h;
21479 else
21480 *head = h;
21481 h->prev = *tail;
21482 *tail = t;
21483 }
21484 }
21485
21486
21487 /* Prepend the list of glyph strings with head H and tail T to the
21488 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21489 result. */
21490
21491 static inline void
21492 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21493 struct glyph_string *h, struct glyph_string *t)
21494 {
21495 if (h)
21496 {
21497 if (*head)
21498 (*head)->prev = t;
21499 else
21500 *tail = t;
21501 t->next = *head;
21502 *head = h;
21503 }
21504 }
21505
21506
21507 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21508 Set *HEAD and *TAIL to the resulting list. */
21509
21510 static inline void
21511 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21512 struct glyph_string *s)
21513 {
21514 s->next = s->prev = NULL;
21515 append_glyph_string_lists (head, tail, s, s);
21516 }
21517
21518
21519 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21520 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21521 make sure that X resources for the face returned are allocated.
21522 Value is a pointer to a realized face that is ready for display if
21523 DISPLAY_P is non-zero. */
21524
21525 static inline struct face *
21526 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21527 XChar2b *char2b, int display_p)
21528 {
21529 struct face *face = FACE_FROM_ID (f, face_id);
21530
21531 if (face->font)
21532 {
21533 unsigned code = face->font->driver->encode_char (face->font, c);
21534
21535 if (code != FONT_INVALID_CODE)
21536 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21537 else
21538 STORE_XCHAR2B (char2b, 0, 0);
21539 }
21540
21541 /* Make sure X resources of the face are allocated. */
21542 #ifdef HAVE_X_WINDOWS
21543 if (display_p)
21544 #endif
21545 {
21546 xassert (face != NULL);
21547 PREPARE_FACE_FOR_DISPLAY (f, face);
21548 }
21549
21550 return face;
21551 }
21552
21553
21554 /* Get face and two-byte form of character glyph GLYPH on frame F.
21555 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21556 a pointer to a realized face that is ready for display. */
21557
21558 static inline struct face *
21559 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21560 XChar2b *char2b, int *two_byte_p)
21561 {
21562 struct face *face;
21563
21564 xassert (glyph->type == CHAR_GLYPH);
21565 face = FACE_FROM_ID (f, glyph->face_id);
21566
21567 if (two_byte_p)
21568 *two_byte_p = 0;
21569
21570 if (face->font)
21571 {
21572 unsigned code;
21573
21574 if (CHAR_BYTE8_P (glyph->u.ch))
21575 code = CHAR_TO_BYTE8 (glyph->u.ch);
21576 else
21577 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21578
21579 if (code != FONT_INVALID_CODE)
21580 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21581 else
21582 STORE_XCHAR2B (char2b, 0, 0);
21583 }
21584
21585 /* Make sure X resources of the face are allocated. */
21586 xassert (face != NULL);
21587 PREPARE_FACE_FOR_DISPLAY (f, face);
21588 return face;
21589 }
21590
21591
21592 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21593 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21594
21595 static inline int
21596 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21597 {
21598 unsigned code;
21599
21600 if (CHAR_BYTE8_P (c))
21601 code = CHAR_TO_BYTE8 (c);
21602 else
21603 code = font->driver->encode_char (font, c);
21604
21605 if (code == FONT_INVALID_CODE)
21606 return 0;
21607 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21608 return 1;
21609 }
21610
21611
21612 /* Fill glyph string S with composition components specified by S->cmp.
21613
21614 BASE_FACE is the base face of the composition.
21615 S->cmp_from is the index of the first component for S.
21616
21617 OVERLAPS non-zero means S should draw the foreground only, and use
21618 its physical height for clipping. See also draw_glyphs.
21619
21620 Value is the index of a component not in S. */
21621
21622 static int
21623 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21624 int overlaps)
21625 {
21626 int i;
21627 /* For all glyphs of this composition, starting at the offset
21628 S->cmp_from, until we reach the end of the definition or encounter a
21629 glyph that requires the different face, add it to S. */
21630 struct face *face;
21631
21632 xassert (s);
21633
21634 s->for_overlaps = overlaps;
21635 s->face = NULL;
21636 s->font = NULL;
21637 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21638 {
21639 int c = COMPOSITION_GLYPH (s->cmp, i);
21640
21641 if (c != '\t')
21642 {
21643 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21644 -1, Qnil);
21645
21646 face = get_char_face_and_encoding (s->f, c, face_id,
21647 s->char2b + i, 1);
21648 if (face)
21649 {
21650 if (! s->face)
21651 {
21652 s->face = face;
21653 s->font = s->face->font;
21654 }
21655 else if (s->face != face)
21656 break;
21657 }
21658 }
21659 ++s->nchars;
21660 }
21661 s->cmp_to = i;
21662
21663 /* All glyph strings for the same composition has the same width,
21664 i.e. the width set for the first component of the composition. */
21665 s->width = s->first_glyph->pixel_width;
21666
21667 /* If the specified font could not be loaded, use the frame's
21668 default font, but record the fact that we couldn't load it in
21669 the glyph string so that we can draw rectangles for the
21670 characters of the glyph string. */
21671 if (s->font == NULL)
21672 {
21673 s->font_not_found_p = 1;
21674 s->font = FRAME_FONT (s->f);
21675 }
21676
21677 /* Adjust base line for subscript/superscript text. */
21678 s->ybase += s->first_glyph->voffset;
21679
21680 /* This glyph string must always be drawn with 16-bit functions. */
21681 s->two_byte_p = 1;
21682
21683 return s->cmp_to;
21684 }
21685
21686 static int
21687 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21688 int start, int end, int overlaps)
21689 {
21690 struct glyph *glyph, *last;
21691 Lisp_Object lgstring;
21692 int i;
21693
21694 s->for_overlaps = overlaps;
21695 glyph = s->row->glyphs[s->area] + start;
21696 last = s->row->glyphs[s->area] + end;
21697 s->cmp_id = glyph->u.cmp.id;
21698 s->cmp_from = glyph->slice.cmp.from;
21699 s->cmp_to = glyph->slice.cmp.to + 1;
21700 s->face = FACE_FROM_ID (s->f, face_id);
21701 lgstring = composition_gstring_from_id (s->cmp_id);
21702 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21703 glyph++;
21704 while (glyph < last
21705 && glyph->u.cmp.automatic
21706 && glyph->u.cmp.id == s->cmp_id
21707 && s->cmp_to == glyph->slice.cmp.from)
21708 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21709
21710 for (i = s->cmp_from; i < s->cmp_to; i++)
21711 {
21712 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21713 unsigned code = LGLYPH_CODE (lglyph);
21714
21715 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21716 }
21717 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21718 return glyph - s->row->glyphs[s->area];
21719 }
21720
21721
21722 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21723 See the comment of fill_glyph_string for arguments.
21724 Value is the index of the first glyph not in S. */
21725
21726
21727 static int
21728 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21729 int start, int end, int overlaps)
21730 {
21731 struct glyph *glyph, *last;
21732 int voffset;
21733
21734 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21735 s->for_overlaps = overlaps;
21736 glyph = s->row->glyphs[s->area] + start;
21737 last = s->row->glyphs[s->area] + end;
21738 voffset = glyph->voffset;
21739 s->face = FACE_FROM_ID (s->f, face_id);
21740 s->font = s->face->font;
21741 s->nchars = 1;
21742 s->width = glyph->pixel_width;
21743 glyph++;
21744 while (glyph < last
21745 && glyph->type == GLYPHLESS_GLYPH
21746 && glyph->voffset == voffset
21747 && glyph->face_id == face_id)
21748 {
21749 s->nchars++;
21750 s->width += glyph->pixel_width;
21751 glyph++;
21752 }
21753 s->ybase += voffset;
21754 return glyph - s->row->glyphs[s->area];
21755 }
21756
21757
21758 /* Fill glyph string S from a sequence of character glyphs.
21759
21760 FACE_ID is the face id of the string. START is the index of the
21761 first glyph to consider, END is the index of the last + 1.
21762 OVERLAPS non-zero means S should draw the foreground only, and use
21763 its physical height for clipping. See also draw_glyphs.
21764
21765 Value is the index of the first glyph not in S. */
21766
21767 static int
21768 fill_glyph_string (struct glyph_string *s, int face_id,
21769 int start, int end, int overlaps)
21770 {
21771 struct glyph *glyph, *last;
21772 int voffset;
21773 int glyph_not_available_p;
21774
21775 xassert (s->f == XFRAME (s->w->frame));
21776 xassert (s->nchars == 0);
21777 xassert (start >= 0 && end > start);
21778
21779 s->for_overlaps = overlaps;
21780 glyph = s->row->glyphs[s->area] + start;
21781 last = s->row->glyphs[s->area] + end;
21782 voffset = glyph->voffset;
21783 s->padding_p = glyph->padding_p;
21784 glyph_not_available_p = glyph->glyph_not_available_p;
21785
21786 while (glyph < last
21787 && glyph->type == CHAR_GLYPH
21788 && glyph->voffset == voffset
21789 /* Same face id implies same font, nowadays. */
21790 && glyph->face_id == face_id
21791 && glyph->glyph_not_available_p == glyph_not_available_p)
21792 {
21793 int two_byte_p;
21794
21795 s->face = get_glyph_face_and_encoding (s->f, glyph,
21796 s->char2b + s->nchars,
21797 &two_byte_p);
21798 s->two_byte_p = two_byte_p;
21799 ++s->nchars;
21800 xassert (s->nchars <= end - start);
21801 s->width += glyph->pixel_width;
21802 if (glyph++->padding_p != s->padding_p)
21803 break;
21804 }
21805
21806 s->font = s->face->font;
21807
21808 /* If the specified font could not be loaded, use the frame's font,
21809 but record the fact that we couldn't load it in
21810 S->font_not_found_p so that we can draw rectangles for the
21811 characters of the glyph string. */
21812 if (s->font == NULL || glyph_not_available_p)
21813 {
21814 s->font_not_found_p = 1;
21815 s->font = FRAME_FONT (s->f);
21816 }
21817
21818 /* Adjust base line for subscript/superscript text. */
21819 s->ybase += voffset;
21820
21821 xassert (s->face && s->face->gc);
21822 return glyph - s->row->glyphs[s->area];
21823 }
21824
21825
21826 /* Fill glyph string S from image glyph S->first_glyph. */
21827
21828 static void
21829 fill_image_glyph_string (struct glyph_string *s)
21830 {
21831 xassert (s->first_glyph->type == IMAGE_GLYPH);
21832 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21833 xassert (s->img);
21834 s->slice = s->first_glyph->slice.img;
21835 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21836 s->font = s->face->font;
21837 s->width = s->first_glyph->pixel_width;
21838
21839 /* Adjust base line for subscript/superscript text. */
21840 s->ybase += s->first_glyph->voffset;
21841 }
21842
21843
21844 /* Fill glyph string S from a sequence of stretch glyphs.
21845
21846 START is the index of the first glyph to consider,
21847 END is the index of the last + 1.
21848
21849 Value is the index of the first glyph not in S. */
21850
21851 static int
21852 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21853 {
21854 struct glyph *glyph, *last;
21855 int voffset, face_id;
21856
21857 xassert (s->first_glyph->type == STRETCH_GLYPH);
21858
21859 glyph = s->row->glyphs[s->area] + start;
21860 last = s->row->glyphs[s->area] + end;
21861 face_id = glyph->face_id;
21862 s->face = FACE_FROM_ID (s->f, face_id);
21863 s->font = s->face->font;
21864 s->width = glyph->pixel_width;
21865 s->nchars = 1;
21866 voffset = glyph->voffset;
21867
21868 for (++glyph;
21869 (glyph < last
21870 && glyph->type == STRETCH_GLYPH
21871 && glyph->voffset == voffset
21872 && glyph->face_id == face_id);
21873 ++glyph)
21874 s->width += glyph->pixel_width;
21875
21876 /* Adjust base line for subscript/superscript text. */
21877 s->ybase += voffset;
21878
21879 /* The case that face->gc == 0 is handled when drawing the glyph
21880 string by calling PREPARE_FACE_FOR_DISPLAY. */
21881 xassert (s->face);
21882 return glyph - s->row->glyphs[s->area];
21883 }
21884
21885 static struct font_metrics *
21886 get_per_char_metric (struct font *font, XChar2b *char2b)
21887 {
21888 static struct font_metrics metrics;
21889 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21890
21891 if (! font || code == FONT_INVALID_CODE)
21892 return NULL;
21893 font->driver->text_extents (font, &code, 1, &metrics);
21894 return &metrics;
21895 }
21896
21897 /* EXPORT for RIF:
21898 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21899 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21900 assumed to be zero. */
21901
21902 void
21903 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21904 {
21905 *left = *right = 0;
21906
21907 if (glyph->type == CHAR_GLYPH)
21908 {
21909 struct face *face;
21910 XChar2b char2b;
21911 struct font_metrics *pcm;
21912
21913 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21914 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21915 {
21916 if (pcm->rbearing > pcm->width)
21917 *right = pcm->rbearing - pcm->width;
21918 if (pcm->lbearing < 0)
21919 *left = -pcm->lbearing;
21920 }
21921 }
21922 else if (glyph->type == COMPOSITE_GLYPH)
21923 {
21924 if (! glyph->u.cmp.automatic)
21925 {
21926 struct composition *cmp = composition_table[glyph->u.cmp.id];
21927
21928 if (cmp->rbearing > cmp->pixel_width)
21929 *right = cmp->rbearing - cmp->pixel_width;
21930 if (cmp->lbearing < 0)
21931 *left = - cmp->lbearing;
21932 }
21933 else
21934 {
21935 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21936 struct font_metrics metrics;
21937
21938 composition_gstring_width (gstring, glyph->slice.cmp.from,
21939 glyph->slice.cmp.to + 1, &metrics);
21940 if (metrics.rbearing > metrics.width)
21941 *right = metrics.rbearing - metrics.width;
21942 if (metrics.lbearing < 0)
21943 *left = - metrics.lbearing;
21944 }
21945 }
21946 }
21947
21948
21949 /* Return the index of the first glyph preceding glyph string S that
21950 is overwritten by S because of S's left overhang. Value is -1
21951 if no glyphs are overwritten. */
21952
21953 static int
21954 left_overwritten (struct glyph_string *s)
21955 {
21956 int k;
21957
21958 if (s->left_overhang)
21959 {
21960 int x = 0, i;
21961 struct glyph *glyphs = s->row->glyphs[s->area];
21962 int first = s->first_glyph - glyphs;
21963
21964 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21965 x -= glyphs[i].pixel_width;
21966
21967 k = i + 1;
21968 }
21969 else
21970 k = -1;
21971
21972 return k;
21973 }
21974
21975
21976 /* Return the index of the first glyph preceding glyph string S that
21977 is overwriting S because of its right overhang. Value is -1 if no
21978 glyph in front of S overwrites S. */
21979
21980 static int
21981 left_overwriting (struct glyph_string *s)
21982 {
21983 int i, k, x;
21984 struct glyph *glyphs = s->row->glyphs[s->area];
21985 int first = s->first_glyph - glyphs;
21986
21987 k = -1;
21988 x = 0;
21989 for (i = first - 1; i >= 0; --i)
21990 {
21991 int left, right;
21992 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21993 if (x + right > 0)
21994 k = i;
21995 x -= glyphs[i].pixel_width;
21996 }
21997
21998 return k;
21999 }
22000
22001
22002 /* Return the index of the last glyph following glyph string S that is
22003 overwritten by S because of S's right overhang. Value is -1 if
22004 no such glyph is found. */
22005
22006 static int
22007 right_overwritten (struct glyph_string *s)
22008 {
22009 int k = -1;
22010
22011 if (s->right_overhang)
22012 {
22013 int x = 0, i;
22014 struct glyph *glyphs = s->row->glyphs[s->area];
22015 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22016 int end = s->row->used[s->area];
22017
22018 for (i = first; i < end && s->right_overhang > x; ++i)
22019 x += glyphs[i].pixel_width;
22020
22021 k = i;
22022 }
22023
22024 return k;
22025 }
22026
22027
22028 /* Return the index of the last glyph following glyph string S that
22029 overwrites S because of its left overhang. Value is negative
22030 if no such glyph is found. */
22031
22032 static int
22033 right_overwriting (struct glyph_string *s)
22034 {
22035 int i, k, x;
22036 int end = s->row->used[s->area];
22037 struct glyph *glyphs = s->row->glyphs[s->area];
22038 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22039
22040 k = -1;
22041 x = 0;
22042 for (i = first; i < end; ++i)
22043 {
22044 int left, right;
22045 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22046 if (x - left < 0)
22047 k = i;
22048 x += glyphs[i].pixel_width;
22049 }
22050
22051 return k;
22052 }
22053
22054
22055 /* Set background width of glyph string S. START is the index of the
22056 first glyph following S. LAST_X is the right-most x-position + 1
22057 in the drawing area. */
22058
22059 static inline void
22060 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22061 {
22062 /* If the face of this glyph string has to be drawn to the end of
22063 the drawing area, set S->extends_to_end_of_line_p. */
22064
22065 if (start == s->row->used[s->area]
22066 && s->area == TEXT_AREA
22067 && ((s->row->fill_line_p
22068 && (s->hl == DRAW_NORMAL_TEXT
22069 || s->hl == DRAW_IMAGE_RAISED
22070 || s->hl == DRAW_IMAGE_SUNKEN))
22071 || s->hl == DRAW_MOUSE_FACE))
22072 s->extends_to_end_of_line_p = 1;
22073
22074 /* If S extends its face to the end of the line, set its
22075 background_width to the distance to the right edge of the drawing
22076 area. */
22077 if (s->extends_to_end_of_line_p)
22078 s->background_width = last_x - s->x + 1;
22079 else
22080 s->background_width = s->width;
22081 }
22082
22083
22084 /* Compute overhangs and x-positions for glyph string S and its
22085 predecessors, or successors. X is the starting x-position for S.
22086 BACKWARD_P non-zero means process predecessors. */
22087
22088 static void
22089 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22090 {
22091 if (backward_p)
22092 {
22093 while (s)
22094 {
22095 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22096 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22097 x -= s->width;
22098 s->x = x;
22099 s = s->prev;
22100 }
22101 }
22102 else
22103 {
22104 while (s)
22105 {
22106 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22107 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22108 s->x = x;
22109 x += s->width;
22110 s = s->next;
22111 }
22112 }
22113 }
22114
22115
22116
22117 /* The following macros are only called from draw_glyphs below.
22118 They reference the following parameters of that function directly:
22119 `w', `row', `area', and `overlap_p'
22120 as well as the following local variables:
22121 `s', `f', and `hdc' (in W32) */
22122
22123 #ifdef HAVE_NTGUI
22124 /* On W32, silently add local `hdc' variable to argument list of
22125 init_glyph_string. */
22126 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22127 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22128 #else
22129 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22130 init_glyph_string (s, char2b, w, row, area, start, hl)
22131 #endif
22132
22133 /* Add a glyph string for a stretch glyph to the list of strings
22134 between HEAD and TAIL. START is the index of the stretch glyph in
22135 row area AREA of glyph row ROW. END is the index of the last glyph
22136 in that glyph row area. X is the current output position assigned
22137 to the new glyph string constructed. HL overrides that face of the
22138 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22139 is the right-most x-position of the drawing area. */
22140
22141 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22142 and below -- keep them on one line. */
22143 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22144 do \
22145 { \
22146 s = (struct glyph_string *) alloca (sizeof *s); \
22147 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22148 START = fill_stretch_glyph_string (s, START, END); \
22149 append_glyph_string (&HEAD, &TAIL, s); \
22150 s->x = (X); \
22151 } \
22152 while (0)
22153
22154
22155 /* Add a glyph string for an image glyph to the list of strings
22156 between HEAD and TAIL. START is the index of the image glyph in
22157 row area AREA of glyph row ROW. END is the index of the last glyph
22158 in that glyph row area. X is the current output position assigned
22159 to the new glyph string constructed. HL overrides that face of the
22160 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22161 is the right-most x-position of the drawing area. */
22162
22163 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22164 do \
22165 { \
22166 s = (struct glyph_string *) alloca (sizeof *s); \
22167 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22168 fill_image_glyph_string (s); \
22169 append_glyph_string (&HEAD, &TAIL, s); \
22170 ++START; \
22171 s->x = (X); \
22172 } \
22173 while (0)
22174
22175
22176 /* Add a glyph string for a sequence of character glyphs to the list
22177 of strings between HEAD and TAIL. START is the index of the first
22178 glyph in row area AREA of glyph row ROW that is part of the new
22179 glyph string. END is the index of the last glyph in that glyph row
22180 area. X is the current output position assigned to the new glyph
22181 string constructed. HL overrides that face of the glyph; e.g. it
22182 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22183 right-most x-position of the drawing area. */
22184
22185 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22186 do \
22187 { \
22188 int face_id; \
22189 XChar2b *char2b; \
22190 \
22191 face_id = (row)->glyphs[area][START].face_id; \
22192 \
22193 s = (struct glyph_string *) alloca (sizeof *s); \
22194 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22195 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22196 append_glyph_string (&HEAD, &TAIL, s); \
22197 s->x = (X); \
22198 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22199 } \
22200 while (0)
22201
22202
22203 /* Add a glyph string for a composite sequence to the list of strings
22204 between HEAD and TAIL. START is the index of the first glyph in
22205 row area AREA of glyph row ROW that is part of the new glyph
22206 string. END is the index of the last glyph in that glyph row area.
22207 X is the current output position assigned to the new glyph string
22208 constructed. HL overrides that face of the glyph; e.g. it is
22209 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22210 x-position of the drawing area. */
22211
22212 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22213 do { \
22214 int face_id = (row)->glyphs[area][START].face_id; \
22215 struct face *base_face = FACE_FROM_ID (f, face_id); \
22216 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22217 struct composition *cmp = composition_table[cmp_id]; \
22218 XChar2b *char2b; \
22219 struct glyph_string *first_s IF_LINT (= NULL); \
22220 int n; \
22221 \
22222 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22223 \
22224 /* Make glyph_strings for each glyph sequence that is drawable by \
22225 the same face, and append them to HEAD/TAIL. */ \
22226 for (n = 0; n < cmp->glyph_len;) \
22227 { \
22228 s = (struct glyph_string *) alloca (sizeof *s); \
22229 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22230 append_glyph_string (&(HEAD), &(TAIL), s); \
22231 s->cmp = cmp; \
22232 s->cmp_from = n; \
22233 s->x = (X); \
22234 if (n == 0) \
22235 first_s = s; \
22236 n = fill_composite_glyph_string (s, base_face, overlaps); \
22237 } \
22238 \
22239 ++START; \
22240 s = first_s; \
22241 } while (0)
22242
22243
22244 /* Add a glyph string for a glyph-string sequence to the list of strings
22245 between HEAD and TAIL. */
22246
22247 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22248 do { \
22249 int face_id; \
22250 XChar2b *char2b; \
22251 Lisp_Object gstring; \
22252 \
22253 face_id = (row)->glyphs[area][START].face_id; \
22254 gstring = (composition_gstring_from_id \
22255 ((row)->glyphs[area][START].u.cmp.id)); \
22256 s = (struct glyph_string *) alloca (sizeof *s); \
22257 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22258 * LGSTRING_GLYPH_LEN (gstring)); \
22259 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22260 append_glyph_string (&(HEAD), &(TAIL), s); \
22261 s->x = (X); \
22262 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22263 } while (0)
22264
22265
22266 /* Add a glyph string for a sequence of glyphless character's glyphs
22267 to the list of strings between HEAD and TAIL. The meanings of
22268 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22269
22270 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22271 do \
22272 { \
22273 int face_id; \
22274 \
22275 face_id = (row)->glyphs[area][START].face_id; \
22276 \
22277 s = (struct glyph_string *) alloca (sizeof *s); \
22278 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22279 append_glyph_string (&HEAD, &TAIL, s); \
22280 s->x = (X); \
22281 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22282 overlaps); \
22283 } \
22284 while (0)
22285
22286
22287 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22288 of AREA of glyph row ROW on window W between indices START and END.
22289 HL overrides the face for drawing glyph strings, e.g. it is
22290 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22291 x-positions of the drawing area.
22292
22293 This is an ugly monster macro construct because we must use alloca
22294 to allocate glyph strings (because draw_glyphs can be called
22295 asynchronously). */
22296
22297 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22298 do \
22299 { \
22300 HEAD = TAIL = NULL; \
22301 while (START < END) \
22302 { \
22303 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22304 switch (first_glyph->type) \
22305 { \
22306 case CHAR_GLYPH: \
22307 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22308 HL, X, LAST_X); \
22309 break; \
22310 \
22311 case COMPOSITE_GLYPH: \
22312 if (first_glyph->u.cmp.automatic) \
22313 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22314 HL, X, LAST_X); \
22315 else \
22316 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22317 HL, X, LAST_X); \
22318 break; \
22319 \
22320 case STRETCH_GLYPH: \
22321 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22322 HL, X, LAST_X); \
22323 break; \
22324 \
22325 case IMAGE_GLYPH: \
22326 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22327 HL, X, LAST_X); \
22328 break; \
22329 \
22330 case GLYPHLESS_GLYPH: \
22331 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22332 HL, X, LAST_X); \
22333 break; \
22334 \
22335 default: \
22336 abort (); \
22337 } \
22338 \
22339 if (s) \
22340 { \
22341 set_glyph_string_background_width (s, START, LAST_X); \
22342 (X) += s->width; \
22343 } \
22344 } \
22345 } while (0)
22346
22347
22348 /* Draw glyphs between START and END in AREA of ROW on window W,
22349 starting at x-position X. X is relative to AREA in W. HL is a
22350 face-override with the following meaning:
22351
22352 DRAW_NORMAL_TEXT draw normally
22353 DRAW_CURSOR draw in cursor face
22354 DRAW_MOUSE_FACE draw in mouse face.
22355 DRAW_INVERSE_VIDEO draw in mode line face
22356 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22357 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22358
22359 If OVERLAPS is non-zero, draw only the foreground of characters and
22360 clip to the physical height of ROW. Non-zero value also defines
22361 the overlapping part to be drawn:
22362
22363 OVERLAPS_PRED overlap with preceding rows
22364 OVERLAPS_SUCC overlap with succeeding rows
22365 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22366 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22367
22368 Value is the x-position reached, relative to AREA of W. */
22369
22370 static int
22371 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22372 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22373 enum draw_glyphs_face hl, int overlaps)
22374 {
22375 struct glyph_string *head, *tail;
22376 struct glyph_string *s;
22377 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22378 int i, j, x_reached, last_x, area_left = 0;
22379 struct frame *f = XFRAME (WINDOW_FRAME (w));
22380 DECLARE_HDC (hdc);
22381
22382 ALLOCATE_HDC (hdc, f);
22383
22384 /* Let's rather be paranoid than getting a SEGV. */
22385 end = min (end, row->used[area]);
22386 start = max (0, start);
22387 start = min (end, start);
22388
22389 /* Translate X to frame coordinates. Set last_x to the right
22390 end of the drawing area. */
22391 if (row->full_width_p)
22392 {
22393 /* X is relative to the left edge of W, without scroll bars
22394 or fringes. */
22395 area_left = WINDOW_LEFT_EDGE_X (w);
22396 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22397 }
22398 else
22399 {
22400 area_left = window_box_left (w, area);
22401 last_x = area_left + window_box_width (w, area);
22402 }
22403 x += area_left;
22404
22405 /* Build a doubly-linked list of glyph_string structures between
22406 head and tail from what we have to draw. Note that the macro
22407 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22408 the reason we use a separate variable `i'. */
22409 i = start;
22410 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22411 if (tail)
22412 x_reached = tail->x + tail->background_width;
22413 else
22414 x_reached = x;
22415
22416 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22417 the row, redraw some glyphs in front or following the glyph
22418 strings built above. */
22419 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22420 {
22421 struct glyph_string *h, *t;
22422 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22423 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22424 int check_mouse_face = 0;
22425 int dummy_x = 0;
22426
22427 /* If mouse highlighting is on, we may need to draw adjacent
22428 glyphs using mouse-face highlighting. */
22429 if (area == TEXT_AREA && row->mouse_face_p)
22430 {
22431 struct glyph_row *mouse_beg_row, *mouse_end_row;
22432
22433 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22434 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22435
22436 if (row >= mouse_beg_row && row <= mouse_end_row)
22437 {
22438 check_mouse_face = 1;
22439 mouse_beg_col = (row == mouse_beg_row)
22440 ? hlinfo->mouse_face_beg_col : 0;
22441 mouse_end_col = (row == mouse_end_row)
22442 ? hlinfo->mouse_face_end_col
22443 : row->used[TEXT_AREA];
22444 }
22445 }
22446
22447 /* Compute overhangs for all glyph strings. */
22448 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22449 for (s = head; s; s = s->next)
22450 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22451
22452 /* Prepend glyph strings for glyphs in front of the first glyph
22453 string that are overwritten because of the first glyph
22454 string's left overhang. The background of all strings
22455 prepended must be drawn because the first glyph string
22456 draws over it. */
22457 i = left_overwritten (head);
22458 if (i >= 0)
22459 {
22460 enum draw_glyphs_face overlap_hl;
22461
22462 /* If this row contains mouse highlighting, attempt to draw
22463 the overlapped glyphs with the correct highlight. This
22464 code fails if the overlap encompasses more than one glyph
22465 and mouse-highlight spans only some of these glyphs.
22466 However, making it work perfectly involves a lot more
22467 code, and I don't know if the pathological case occurs in
22468 practice, so we'll stick to this for now. --- cyd */
22469 if (check_mouse_face
22470 && mouse_beg_col < start && mouse_end_col > i)
22471 overlap_hl = DRAW_MOUSE_FACE;
22472 else
22473 overlap_hl = DRAW_NORMAL_TEXT;
22474
22475 j = i;
22476 BUILD_GLYPH_STRINGS (j, start, h, t,
22477 overlap_hl, dummy_x, last_x);
22478 start = i;
22479 compute_overhangs_and_x (t, head->x, 1);
22480 prepend_glyph_string_lists (&head, &tail, h, t);
22481 clip_head = head;
22482 }
22483
22484 /* Prepend glyph strings for glyphs in front of the first glyph
22485 string that overwrite that glyph string because of their
22486 right overhang. For these strings, only the foreground must
22487 be drawn, because it draws over the glyph string at `head'.
22488 The background must not be drawn because this would overwrite
22489 right overhangs of preceding glyphs for which no glyph
22490 strings exist. */
22491 i = left_overwriting (head);
22492 if (i >= 0)
22493 {
22494 enum draw_glyphs_face overlap_hl;
22495
22496 if (check_mouse_face
22497 && mouse_beg_col < start && mouse_end_col > i)
22498 overlap_hl = DRAW_MOUSE_FACE;
22499 else
22500 overlap_hl = DRAW_NORMAL_TEXT;
22501
22502 clip_head = head;
22503 BUILD_GLYPH_STRINGS (i, start, h, t,
22504 overlap_hl, dummy_x, last_x);
22505 for (s = h; s; s = s->next)
22506 s->background_filled_p = 1;
22507 compute_overhangs_and_x (t, head->x, 1);
22508 prepend_glyph_string_lists (&head, &tail, h, t);
22509 }
22510
22511 /* Append glyphs strings for glyphs following the last glyph
22512 string tail that are overwritten by tail. The background of
22513 these strings has to be drawn because tail's foreground draws
22514 over it. */
22515 i = right_overwritten (tail);
22516 if (i >= 0)
22517 {
22518 enum draw_glyphs_face overlap_hl;
22519
22520 if (check_mouse_face
22521 && mouse_beg_col < i && mouse_end_col > end)
22522 overlap_hl = DRAW_MOUSE_FACE;
22523 else
22524 overlap_hl = DRAW_NORMAL_TEXT;
22525
22526 BUILD_GLYPH_STRINGS (end, i, h, t,
22527 overlap_hl, x, last_x);
22528 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22529 we don't have `end = i;' here. */
22530 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22531 append_glyph_string_lists (&head, &tail, h, t);
22532 clip_tail = tail;
22533 }
22534
22535 /* Append glyph strings for glyphs following the last glyph
22536 string tail that overwrite tail. The foreground of such
22537 glyphs has to be drawn because it writes into the background
22538 of tail. The background must not be drawn because it could
22539 paint over the foreground of following glyphs. */
22540 i = right_overwriting (tail);
22541 if (i >= 0)
22542 {
22543 enum draw_glyphs_face overlap_hl;
22544 if (check_mouse_face
22545 && mouse_beg_col < i && mouse_end_col > end)
22546 overlap_hl = DRAW_MOUSE_FACE;
22547 else
22548 overlap_hl = DRAW_NORMAL_TEXT;
22549
22550 clip_tail = tail;
22551 i++; /* We must include the Ith glyph. */
22552 BUILD_GLYPH_STRINGS (end, i, h, t,
22553 overlap_hl, x, last_x);
22554 for (s = h; s; s = s->next)
22555 s->background_filled_p = 1;
22556 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22557 append_glyph_string_lists (&head, &tail, h, t);
22558 }
22559 if (clip_head || clip_tail)
22560 for (s = head; s; s = s->next)
22561 {
22562 s->clip_head = clip_head;
22563 s->clip_tail = clip_tail;
22564 }
22565 }
22566
22567 /* Draw all strings. */
22568 for (s = head; s; s = s->next)
22569 FRAME_RIF (f)->draw_glyph_string (s);
22570
22571 #ifndef HAVE_NS
22572 /* When focus a sole frame and move horizontally, this sets on_p to 0
22573 causing a failure to erase prev cursor position. */
22574 if (area == TEXT_AREA
22575 && !row->full_width_p
22576 /* When drawing overlapping rows, only the glyph strings'
22577 foreground is drawn, which doesn't erase a cursor
22578 completely. */
22579 && !overlaps)
22580 {
22581 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22582 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22583 : (tail ? tail->x + tail->background_width : x));
22584 x0 -= area_left;
22585 x1 -= area_left;
22586
22587 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22588 row->y, MATRIX_ROW_BOTTOM_Y (row));
22589 }
22590 #endif
22591
22592 /* Value is the x-position up to which drawn, relative to AREA of W.
22593 This doesn't include parts drawn because of overhangs. */
22594 if (row->full_width_p)
22595 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22596 else
22597 x_reached -= area_left;
22598
22599 RELEASE_HDC (hdc, f);
22600
22601 return x_reached;
22602 }
22603
22604 /* Expand row matrix if too narrow. Don't expand if area
22605 is not present. */
22606
22607 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22608 { \
22609 if (!fonts_changed_p \
22610 && (it->glyph_row->glyphs[area] \
22611 < it->glyph_row->glyphs[area + 1])) \
22612 { \
22613 it->w->ncols_scale_factor++; \
22614 fonts_changed_p = 1; \
22615 } \
22616 }
22617
22618 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22619 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22620
22621 static inline void
22622 append_glyph (struct it *it)
22623 {
22624 struct glyph *glyph;
22625 enum glyph_row_area area = it->area;
22626
22627 xassert (it->glyph_row);
22628 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22629
22630 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22631 if (glyph < it->glyph_row->glyphs[area + 1])
22632 {
22633 /* If the glyph row is reversed, we need to prepend the glyph
22634 rather than append it. */
22635 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22636 {
22637 struct glyph *g;
22638
22639 /* Make room for the additional glyph. */
22640 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22641 g[1] = *g;
22642 glyph = it->glyph_row->glyphs[area];
22643 }
22644 glyph->charpos = CHARPOS (it->position);
22645 glyph->object = it->object;
22646 if (it->pixel_width > 0)
22647 {
22648 glyph->pixel_width = it->pixel_width;
22649 glyph->padding_p = 0;
22650 }
22651 else
22652 {
22653 /* Assure at least 1-pixel width. Otherwise, cursor can't
22654 be displayed correctly. */
22655 glyph->pixel_width = 1;
22656 glyph->padding_p = 1;
22657 }
22658 glyph->ascent = it->ascent;
22659 glyph->descent = it->descent;
22660 glyph->voffset = it->voffset;
22661 glyph->type = CHAR_GLYPH;
22662 glyph->avoid_cursor_p = it->avoid_cursor_p;
22663 glyph->multibyte_p = it->multibyte_p;
22664 glyph->left_box_line_p = it->start_of_box_run_p;
22665 glyph->right_box_line_p = it->end_of_box_run_p;
22666 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22667 || it->phys_descent > it->descent);
22668 glyph->glyph_not_available_p = it->glyph_not_available_p;
22669 glyph->face_id = it->face_id;
22670 glyph->u.ch = it->char_to_display;
22671 glyph->slice.img = null_glyph_slice;
22672 glyph->font_type = FONT_TYPE_UNKNOWN;
22673 if (it->bidi_p)
22674 {
22675 glyph->resolved_level = it->bidi_it.resolved_level;
22676 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22677 abort ();
22678 glyph->bidi_type = it->bidi_it.type;
22679 }
22680 else
22681 {
22682 glyph->resolved_level = 0;
22683 glyph->bidi_type = UNKNOWN_BT;
22684 }
22685 ++it->glyph_row->used[area];
22686 }
22687 else
22688 IT_EXPAND_MATRIX_WIDTH (it, area);
22689 }
22690
22691 /* Store one glyph for the composition IT->cmp_it.id in
22692 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22693 non-null. */
22694
22695 static inline void
22696 append_composite_glyph (struct it *it)
22697 {
22698 struct glyph *glyph;
22699 enum glyph_row_area area = it->area;
22700
22701 xassert (it->glyph_row);
22702
22703 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22704 if (glyph < it->glyph_row->glyphs[area + 1])
22705 {
22706 /* If the glyph row is reversed, we need to prepend the glyph
22707 rather than append it. */
22708 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22709 {
22710 struct glyph *g;
22711
22712 /* Make room for the new glyph. */
22713 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22714 g[1] = *g;
22715 glyph = it->glyph_row->glyphs[it->area];
22716 }
22717 glyph->charpos = it->cmp_it.charpos;
22718 glyph->object = it->object;
22719 glyph->pixel_width = it->pixel_width;
22720 glyph->ascent = it->ascent;
22721 glyph->descent = it->descent;
22722 glyph->voffset = it->voffset;
22723 glyph->type = COMPOSITE_GLYPH;
22724 if (it->cmp_it.ch < 0)
22725 {
22726 glyph->u.cmp.automatic = 0;
22727 glyph->u.cmp.id = it->cmp_it.id;
22728 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22729 }
22730 else
22731 {
22732 glyph->u.cmp.automatic = 1;
22733 glyph->u.cmp.id = it->cmp_it.id;
22734 glyph->slice.cmp.from = it->cmp_it.from;
22735 glyph->slice.cmp.to = it->cmp_it.to - 1;
22736 }
22737 glyph->avoid_cursor_p = it->avoid_cursor_p;
22738 glyph->multibyte_p = it->multibyte_p;
22739 glyph->left_box_line_p = it->start_of_box_run_p;
22740 glyph->right_box_line_p = it->end_of_box_run_p;
22741 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22742 || it->phys_descent > it->descent);
22743 glyph->padding_p = 0;
22744 glyph->glyph_not_available_p = 0;
22745 glyph->face_id = it->face_id;
22746 glyph->font_type = FONT_TYPE_UNKNOWN;
22747 if (it->bidi_p)
22748 {
22749 glyph->resolved_level = it->bidi_it.resolved_level;
22750 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22751 abort ();
22752 glyph->bidi_type = it->bidi_it.type;
22753 }
22754 ++it->glyph_row->used[area];
22755 }
22756 else
22757 IT_EXPAND_MATRIX_WIDTH (it, area);
22758 }
22759
22760
22761 /* Change IT->ascent and IT->height according to the setting of
22762 IT->voffset. */
22763
22764 static inline void
22765 take_vertical_position_into_account (struct it *it)
22766 {
22767 if (it->voffset)
22768 {
22769 if (it->voffset < 0)
22770 /* Increase the ascent so that we can display the text higher
22771 in the line. */
22772 it->ascent -= it->voffset;
22773 else
22774 /* Increase the descent so that we can display the text lower
22775 in the line. */
22776 it->descent += it->voffset;
22777 }
22778 }
22779
22780
22781 /* Produce glyphs/get display metrics for the image IT is loaded with.
22782 See the description of struct display_iterator in dispextern.h for
22783 an overview of struct display_iterator. */
22784
22785 static void
22786 produce_image_glyph (struct it *it)
22787 {
22788 struct image *img;
22789 struct face *face;
22790 int glyph_ascent, crop;
22791 struct glyph_slice slice;
22792
22793 xassert (it->what == IT_IMAGE);
22794
22795 face = FACE_FROM_ID (it->f, it->face_id);
22796 xassert (face);
22797 /* Make sure X resources of the face is loaded. */
22798 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22799
22800 if (it->image_id < 0)
22801 {
22802 /* Fringe bitmap. */
22803 it->ascent = it->phys_ascent = 0;
22804 it->descent = it->phys_descent = 0;
22805 it->pixel_width = 0;
22806 it->nglyphs = 0;
22807 return;
22808 }
22809
22810 img = IMAGE_FROM_ID (it->f, it->image_id);
22811 xassert (img);
22812 /* Make sure X resources of the image is loaded. */
22813 prepare_image_for_display (it->f, img);
22814
22815 slice.x = slice.y = 0;
22816 slice.width = img->width;
22817 slice.height = img->height;
22818
22819 if (INTEGERP (it->slice.x))
22820 slice.x = XINT (it->slice.x);
22821 else if (FLOATP (it->slice.x))
22822 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22823
22824 if (INTEGERP (it->slice.y))
22825 slice.y = XINT (it->slice.y);
22826 else if (FLOATP (it->slice.y))
22827 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22828
22829 if (INTEGERP (it->slice.width))
22830 slice.width = XINT (it->slice.width);
22831 else if (FLOATP (it->slice.width))
22832 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22833
22834 if (INTEGERP (it->slice.height))
22835 slice.height = XINT (it->slice.height);
22836 else if (FLOATP (it->slice.height))
22837 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22838
22839 if (slice.x >= img->width)
22840 slice.x = img->width;
22841 if (slice.y >= img->height)
22842 slice.y = img->height;
22843 if (slice.x + slice.width >= img->width)
22844 slice.width = img->width - slice.x;
22845 if (slice.y + slice.height > img->height)
22846 slice.height = img->height - slice.y;
22847
22848 if (slice.width == 0 || slice.height == 0)
22849 return;
22850
22851 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22852
22853 it->descent = slice.height - glyph_ascent;
22854 if (slice.y == 0)
22855 it->descent += img->vmargin;
22856 if (slice.y + slice.height == img->height)
22857 it->descent += img->vmargin;
22858 it->phys_descent = it->descent;
22859
22860 it->pixel_width = slice.width;
22861 if (slice.x == 0)
22862 it->pixel_width += img->hmargin;
22863 if (slice.x + slice.width == img->width)
22864 it->pixel_width += img->hmargin;
22865
22866 /* It's quite possible for images to have an ascent greater than
22867 their height, so don't get confused in that case. */
22868 if (it->descent < 0)
22869 it->descent = 0;
22870
22871 it->nglyphs = 1;
22872
22873 if (face->box != FACE_NO_BOX)
22874 {
22875 if (face->box_line_width > 0)
22876 {
22877 if (slice.y == 0)
22878 it->ascent += face->box_line_width;
22879 if (slice.y + slice.height == img->height)
22880 it->descent += face->box_line_width;
22881 }
22882
22883 if (it->start_of_box_run_p && slice.x == 0)
22884 it->pixel_width += eabs (face->box_line_width);
22885 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22886 it->pixel_width += eabs (face->box_line_width);
22887 }
22888
22889 take_vertical_position_into_account (it);
22890
22891 /* Automatically crop wide image glyphs at right edge so we can
22892 draw the cursor on same display row. */
22893 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22894 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22895 {
22896 it->pixel_width -= crop;
22897 slice.width -= crop;
22898 }
22899
22900 if (it->glyph_row)
22901 {
22902 struct glyph *glyph;
22903 enum glyph_row_area area = it->area;
22904
22905 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22906 if (glyph < it->glyph_row->glyphs[area + 1])
22907 {
22908 glyph->charpos = CHARPOS (it->position);
22909 glyph->object = it->object;
22910 glyph->pixel_width = it->pixel_width;
22911 glyph->ascent = glyph_ascent;
22912 glyph->descent = it->descent;
22913 glyph->voffset = it->voffset;
22914 glyph->type = IMAGE_GLYPH;
22915 glyph->avoid_cursor_p = it->avoid_cursor_p;
22916 glyph->multibyte_p = it->multibyte_p;
22917 glyph->left_box_line_p = it->start_of_box_run_p;
22918 glyph->right_box_line_p = it->end_of_box_run_p;
22919 glyph->overlaps_vertically_p = 0;
22920 glyph->padding_p = 0;
22921 glyph->glyph_not_available_p = 0;
22922 glyph->face_id = it->face_id;
22923 glyph->u.img_id = img->id;
22924 glyph->slice.img = slice;
22925 glyph->font_type = FONT_TYPE_UNKNOWN;
22926 if (it->bidi_p)
22927 {
22928 glyph->resolved_level = it->bidi_it.resolved_level;
22929 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22930 abort ();
22931 glyph->bidi_type = it->bidi_it.type;
22932 }
22933 ++it->glyph_row->used[area];
22934 }
22935 else
22936 IT_EXPAND_MATRIX_WIDTH (it, area);
22937 }
22938 }
22939
22940
22941 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22942 of the glyph, WIDTH and HEIGHT are the width and height of the
22943 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22944
22945 static void
22946 append_stretch_glyph (struct it *it, Lisp_Object object,
22947 int width, int height, int ascent)
22948 {
22949 struct glyph *glyph;
22950 enum glyph_row_area area = it->area;
22951
22952 xassert (ascent >= 0 && ascent <= height);
22953
22954 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22955 if (glyph < it->glyph_row->glyphs[area + 1])
22956 {
22957 /* If the glyph row is reversed, we need to prepend the glyph
22958 rather than append it. */
22959 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22960 {
22961 struct glyph *g;
22962
22963 /* Make room for the additional glyph. */
22964 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22965 g[1] = *g;
22966 glyph = it->glyph_row->glyphs[area];
22967 }
22968 glyph->charpos = CHARPOS (it->position);
22969 glyph->object = object;
22970 glyph->pixel_width = width;
22971 glyph->ascent = ascent;
22972 glyph->descent = height - ascent;
22973 glyph->voffset = it->voffset;
22974 glyph->type = STRETCH_GLYPH;
22975 glyph->avoid_cursor_p = it->avoid_cursor_p;
22976 glyph->multibyte_p = it->multibyte_p;
22977 glyph->left_box_line_p = it->start_of_box_run_p;
22978 glyph->right_box_line_p = it->end_of_box_run_p;
22979 glyph->overlaps_vertically_p = 0;
22980 glyph->padding_p = 0;
22981 glyph->glyph_not_available_p = 0;
22982 glyph->face_id = it->face_id;
22983 glyph->u.stretch.ascent = ascent;
22984 glyph->u.stretch.height = height;
22985 glyph->slice.img = null_glyph_slice;
22986 glyph->font_type = FONT_TYPE_UNKNOWN;
22987 if (it->bidi_p)
22988 {
22989 glyph->resolved_level = it->bidi_it.resolved_level;
22990 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22991 abort ();
22992 glyph->bidi_type = it->bidi_it.type;
22993 }
22994 else
22995 {
22996 glyph->resolved_level = 0;
22997 glyph->bidi_type = UNKNOWN_BT;
22998 }
22999 ++it->glyph_row->used[area];
23000 }
23001 else
23002 IT_EXPAND_MATRIX_WIDTH (it, area);
23003 }
23004
23005
23006 /* Produce a stretch glyph for iterator IT. IT->object is the value
23007 of the glyph property displayed. The value must be a list
23008 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23009 being recognized:
23010
23011 1. `:width WIDTH' specifies that the space should be WIDTH *
23012 canonical char width wide. WIDTH may be an integer or floating
23013 point number.
23014
23015 2. `:relative-width FACTOR' specifies that the width of the stretch
23016 should be computed from the width of the first character having the
23017 `glyph' property, and should be FACTOR times that width.
23018
23019 3. `:align-to HPOS' specifies that the space should be wide enough
23020 to reach HPOS, a value in canonical character units.
23021
23022 Exactly one of the above pairs must be present.
23023
23024 4. `:height HEIGHT' specifies that the height of the stretch produced
23025 should be HEIGHT, measured in canonical character units.
23026
23027 5. `:relative-height FACTOR' specifies that the height of the
23028 stretch should be FACTOR times the height of the characters having
23029 the glyph property.
23030
23031 Either none or exactly one of 4 or 5 must be present.
23032
23033 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23034 of the stretch should be used for the ascent of the stretch.
23035 ASCENT must be in the range 0 <= ASCENT <= 100. */
23036
23037 static void
23038 produce_stretch_glyph (struct it *it)
23039 {
23040 /* (space :width WIDTH :height HEIGHT ...) */
23041 Lisp_Object prop, plist;
23042 int width = 0, height = 0, align_to = -1;
23043 int zero_width_ok_p = 0, zero_height_ok_p = 0;
23044 int ascent = 0;
23045 double tem;
23046 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23047 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
23048
23049 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23050
23051 /* List should start with `space'. */
23052 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23053 plist = XCDR (it->object);
23054
23055 /* Compute the width of the stretch. */
23056 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23057 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23058 {
23059 /* Absolute width `:width WIDTH' specified and valid. */
23060 zero_width_ok_p = 1;
23061 width = (int)tem;
23062 }
23063 else if (prop = Fplist_get (plist, QCrelative_width),
23064 NUMVAL (prop) > 0)
23065 {
23066 /* Relative width `:relative-width FACTOR' specified and valid.
23067 Compute the width of the characters having the `glyph'
23068 property. */
23069 struct it it2;
23070 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23071
23072 it2 = *it;
23073 if (it->multibyte_p)
23074 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23075 else
23076 {
23077 it2.c = it2.char_to_display = *p, it2.len = 1;
23078 if (! ASCII_CHAR_P (it2.c))
23079 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23080 }
23081
23082 it2.glyph_row = NULL;
23083 it2.what = IT_CHARACTER;
23084 x_produce_glyphs (&it2);
23085 width = NUMVAL (prop) * it2.pixel_width;
23086 }
23087 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23088 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23089 {
23090 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23091 align_to = (align_to < 0
23092 ? 0
23093 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23094 else if (align_to < 0)
23095 align_to = window_box_left_offset (it->w, TEXT_AREA);
23096 width = max (0, (int)tem + align_to - it->current_x);
23097 zero_width_ok_p = 1;
23098 }
23099 else
23100 /* Nothing specified -> width defaults to canonical char width. */
23101 width = FRAME_COLUMN_WIDTH (it->f);
23102
23103 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23104 width = 1;
23105
23106 /* Compute height. */
23107 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23108 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23109 {
23110 height = (int)tem;
23111 zero_height_ok_p = 1;
23112 }
23113 else if (prop = Fplist_get (plist, QCrelative_height),
23114 NUMVAL (prop) > 0)
23115 height = FONT_HEIGHT (font) * NUMVAL (prop);
23116 else
23117 height = FONT_HEIGHT (font);
23118
23119 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23120 height = 1;
23121
23122 /* Compute percentage of height used for ascent. If
23123 `:ascent ASCENT' is present and valid, use that. Otherwise,
23124 derive the ascent from the font in use. */
23125 if (prop = Fplist_get (plist, QCascent),
23126 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23127 ascent = height * NUMVAL (prop) / 100.0;
23128 else if (!NILP (prop)
23129 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23130 ascent = min (max (0, (int)tem), height);
23131 else
23132 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23133
23134 if (width > 0 && it->line_wrap != TRUNCATE
23135 && it->current_x + width > it->last_visible_x)
23136 width = it->last_visible_x - it->current_x - 1;
23137
23138 if (width > 0 && height > 0 && it->glyph_row)
23139 {
23140 Lisp_Object object = it->stack[it->sp - 1].string;
23141 if (!STRINGP (object))
23142 object = it->w->buffer;
23143 append_stretch_glyph (it, object, width, height, ascent);
23144 }
23145
23146 it->pixel_width = width;
23147 it->ascent = it->phys_ascent = ascent;
23148 it->descent = it->phys_descent = height - it->ascent;
23149 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23150
23151 take_vertical_position_into_account (it);
23152 }
23153
23154 /* Calculate line-height and line-spacing properties.
23155 An integer value specifies explicit pixel value.
23156 A float value specifies relative value to current face height.
23157 A cons (float . face-name) specifies relative value to
23158 height of specified face font.
23159
23160 Returns height in pixels, or nil. */
23161
23162
23163 static Lisp_Object
23164 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23165 int boff, int override)
23166 {
23167 Lisp_Object face_name = Qnil;
23168 int ascent, descent, height;
23169
23170 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23171 return val;
23172
23173 if (CONSP (val))
23174 {
23175 face_name = XCAR (val);
23176 val = XCDR (val);
23177 if (!NUMBERP (val))
23178 val = make_number (1);
23179 if (NILP (face_name))
23180 {
23181 height = it->ascent + it->descent;
23182 goto scale;
23183 }
23184 }
23185
23186 if (NILP (face_name))
23187 {
23188 font = FRAME_FONT (it->f);
23189 boff = FRAME_BASELINE_OFFSET (it->f);
23190 }
23191 else if (EQ (face_name, Qt))
23192 {
23193 override = 0;
23194 }
23195 else
23196 {
23197 int face_id;
23198 struct face *face;
23199
23200 face_id = lookup_named_face (it->f, face_name, 0);
23201 if (face_id < 0)
23202 return make_number (-1);
23203
23204 face = FACE_FROM_ID (it->f, face_id);
23205 font = face->font;
23206 if (font == NULL)
23207 return make_number (-1);
23208 boff = font->baseline_offset;
23209 if (font->vertical_centering)
23210 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23211 }
23212
23213 ascent = FONT_BASE (font) + boff;
23214 descent = FONT_DESCENT (font) - boff;
23215
23216 if (override)
23217 {
23218 it->override_ascent = ascent;
23219 it->override_descent = descent;
23220 it->override_boff = boff;
23221 }
23222
23223 height = ascent + descent;
23224
23225 scale:
23226 if (FLOATP (val))
23227 height = (int)(XFLOAT_DATA (val) * height);
23228 else if (INTEGERP (val))
23229 height *= XINT (val);
23230
23231 return make_number (height);
23232 }
23233
23234
23235 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23236 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23237 and only if this is for a character for which no font was found.
23238
23239 If the display method (it->glyphless_method) is
23240 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23241 length of the acronym or the hexadecimal string, UPPER_XOFF and
23242 UPPER_YOFF are pixel offsets for the upper part of the string,
23243 LOWER_XOFF and LOWER_YOFF are for the lower part.
23244
23245 For the other display methods, LEN through LOWER_YOFF are zero. */
23246
23247 static void
23248 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23249 short upper_xoff, short upper_yoff,
23250 short lower_xoff, short lower_yoff)
23251 {
23252 struct glyph *glyph;
23253 enum glyph_row_area area = it->area;
23254
23255 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23256 if (glyph < it->glyph_row->glyphs[area + 1])
23257 {
23258 /* If the glyph row is reversed, we need to prepend the glyph
23259 rather than append it. */
23260 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23261 {
23262 struct glyph *g;
23263
23264 /* Make room for the additional glyph. */
23265 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23266 g[1] = *g;
23267 glyph = it->glyph_row->glyphs[area];
23268 }
23269 glyph->charpos = CHARPOS (it->position);
23270 glyph->object = it->object;
23271 glyph->pixel_width = it->pixel_width;
23272 glyph->ascent = it->ascent;
23273 glyph->descent = it->descent;
23274 glyph->voffset = it->voffset;
23275 glyph->type = GLYPHLESS_GLYPH;
23276 glyph->u.glyphless.method = it->glyphless_method;
23277 glyph->u.glyphless.for_no_font = for_no_font;
23278 glyph->u.glyphless.len = len;
23279 glyph->u.glyphless.ch = it->c;
23280 glyph->slice.glyphless.upper_xoff = upper_xoff;
23281 glyph->slice.glyphless.upper_yoff = upper_yoff;
23282 glyph->slice.glyphless.lower_xoff = lower_xoff;
23283 glyph->slice.glyphless.lower_yoff = lower_yoff;
23284 glyph->avoid_cursor_p = it->avoid_cursor_p;
23285 glyph->multibyte_p = it->multibyte_p;
23286 glyph->left_box_line_p = it->start_of_box_run_p;
23287 glyph->right_box_line_p = it->end_of_box_run_p;
23288 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23289 || it->phys_descent > it->descent);
23290 glyph->padding_p = 0;
23291 glyph->glyph_not_available_p = 0;
23292 glyph->face_id = face_id;
23293 glyph->font_type = FONT_TYPE_UNKNOWN;
23294 if (it->bidi_p)
23295 {
23296 glyph->resolved_level = it->bidi_it.resolved_level;
23297 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23298 abort ();
23299 glyph->bidi_type = it->bidi_it.type;
23300 }
23301 ++it->glyph_row->used[area];
23302 }
23303 else
23304 IT_EXPAND_MATRIX_WIDTH (it, area);
23305 }
23306
23307
23308 /* Produce a glyph for a glyphless character for iterator IT.
23309 IT->glyphless_method specifies which method to use for displaying
23310 the character. See the description of enum
23311 glyphless_display_method in dispextern.h for the detail.
23312
23313 FOR_NO_FONT is nonzero if and only if this is for a character for
23314 which no font was found. ACRONYM, if non-nil, is an acronym string
23315 for the character. */
23316
23317 static void
23318 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23319 {
23320 int face_id;
23321 struct face *face;
23322 struct font *font;
23323 int base_width, base_height, width, height;
23324 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23325 int len;
23326
23327 /* Get the metrics of the base font. We always refer to the current
23328 ASCII face. */
23329 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23330 font = face->font ? face->font : FRAME_FONT (it->f);
23331 it->ascent = FONT_BASE (font) + font->baseline_offset;
23332 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23333 base_height = it->ascent + it->descent;
23334 base_width = font->average_width;
23335
23336 /* Get a face ID for the glyph by utilizing a cache (the same way as
23337 done for `escape-glyph' in get_next_display_element). */
23338 if (it->f == last_glyphless_glyph_frame
23339 && it->face_id == last_glyphless_glyph_face_id)
23340 {
23341 face_id = last_glyphless_glyph_merged_face_id;
23342 }
23343 else
23344 {
23345 /* Merge the `glyphless-char' face into the current face. */
23346 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23347 last_glyphless_glyph_frame = it->f;
23348 last_glyphless_glyph_face_id = it->face_id;
23349 last_glyphless_glyph_merged_face_id = face_id;
23350 }
23351
23352 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23353 {
23354 it->pixel_width = THIN_SPACE_WIDTH;
23355 len = 0;
23356 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23357 }
23358 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23359 {
23360 width = CHAR_WIDTH (it->c);
23361 if (width == 0)
23362 width = 1;
23363 else if (width > 4)
23364 width = 4;
23365 it->pixel_width = base_width * width;
23366 len = 0;
23367 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23368 }
23369 else
23370 {
23371 char buf[7];
23372 const char *str;
23373 unsigned int code[6];
23374 int upper_len;
23375 int ascent, descent;
23376 struct font_metrics metrics_upper, metrics_lower;
23377
23378 face = FACE_FROM_ID (it->f, face_id);
23379 font = face->font ? face->font : FRAME_FONT (it->f);
23380 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23381
23382 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23383 {
23384 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23385 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23386 if (CONSP (acronym))
23387 acronym = XCAR (acronym);
23388 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23389 }
23390 else
23391 {
23392 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23393 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23394 str = buf;
23395 }
23396 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23397 code[len] = font->driver->encode_char (font, str[len]);
23398 upper_len = (len + 1) / 2;
23399 font->driver->text_extents (font, code, upper_len,
23400 &metrics_upper);
23401 font->driver->text_extents (font, code + upper_len, len - upper_len,
23402 &metrics_lower);
23403
23404
23405
23406 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23407 width = max (metrics_upper.width, metrics_lower.width) + 4;
23408 upper_xoff = upper_yoff = 2; /* the typical case */
23409 if (base_width >= width)
23410 {
23411 /* Align the upper to the left, the lower to the right. */
23412 it->pixel_width = base_width;
23413 lower_xoff = base_width - 2 - metrics_lower.width;
23414 }
23415 else
23416 {
23417 /* Center the shorter one. */
23418 it->pixel_width = width;
23419 if (metrics_upper.width >= metrics_lower.width)
23420 lower_xoff = (width - metrics_lower.width) / 2;
23421 else
23422 {
23423 /* FIXME: This code doesn't look right. It formerly was
23424 missing the "lower_xoff = 0;", which couldn't have
23425 been right since it left lower_xoff uninitialized. */
23426 lower_xoff = 0;
23427 upper_xoff = (width - metrics_upper.width) / 2;
23428 }
23429 }
23430
23431 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23432 top, bottom, and between upper and lower strings. */
23433 height = (metrics_upper.ascent + metrics_upper.descent
23434 + metrics_lower.ascent + metrics_lower.descent) + 5;
23435 /* Center vertically.
23436 H:base_height, D:base_descent
23437 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23438
23439 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23440 descent = D - H/2 + h/2;
23441 lower_yoff = descent - 2 - ld;
23442 upper_yoff = lower_yoff - la - 1 - ud; */
23443 ascent = - (it->descent - (base_height + height + 1) / 2);
23444 descent = it->descent - (base_height - height) / 2;
23445 lower_yoff = descent - 2 - metrics_lower.descent;
23446 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23447 - metrics_upper.descent);
23448 /* Don't make the height shorter than the base height. */
23449 if (height > base_height)
23450 {
23451 it->ascent = ascent;
23452 it->descent = descent;
23453 }
23454 }
23455
23456 it->phys_ascent = it->ascent;
23457 it->phys_descent = it->descent;
23458 if (it->glyph_row)
23459 append_glyphless_glyph (it, face_id, for_no_font, len,
23460 upper_xoff, upper_yoff,
23461 lower_xoff, lower_yoff);
23462 it->nglyphs = 1;
23463 take_vertical_position_into_account (it);
23464 }
23465
23466
23467 /* RIF:
23468 Produce glyphs/get display metrics for the display element IT is
23469 loaded with. See the description of struct it in dispextern.h
23470 for an overview of struct it. */
23471
23472 void
23473 x_produce_glyphs (struct it *it)
23474 {
23475 int extra_line_spacing = it->extra_line_spacing;
23476
23477 it->glyph_not_available_p = 0;
23478
23479 if (it->what == IT_CHARACTER)
23480 {
23481 XChar2b char2b;
23482 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23483 struct font *font = face->font;
23484 struct font_metrics *pcm = NULL;
23485 int boff; /* baseline offset */
23486
23487 if (font == NULL)
23488 {
23489 /* When no suitable font is found, display this character by
23490 the method specified in the first extra slot of
23491 Vglyphless_char_display. */
23492 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23493
23494 xassert (it->what == IT_GLYPHLESS);
23495 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23496 goto done;
23497 }
23498
23499 boff = font->baseline_offset;
23500 if (font->vertical_centering)
23501 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23502
23503 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23504 {
23505 int stretched_p;
23506
23507 it->nglyphs = 1;
23508
23509 if (it->override_ascent >= 0)
23510 {
23511 it->ascent = it->override_ascent;
23512 it->descent = it->override_descent;
23513 boff = it->override_boff;
23514 }
23515 else
23516 {
23517 it->ascent = FONT_BASE (font) + boff;
23518 it->descent = FONT_DESCENT (font) - boff;
23519 }
23520
23521 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23522 {
23523 pcm = get_per_char_metric (font, &char2b);
23524 if (pcm->width == 0
23525 && pcm->rbearing == 0 && pcm->lbearing == 0)
23526 pcm = NULL;
23527 }
23528
23529 if (pcm)
23530 {
23531 it->phys_ascent = pcm->ascent + boff;
23532 it->phys_descent = pcm->descent - boff;
23533 it->pixel_width = pcm->width;
23534 }
23535 else
23536 {
23537 it->glyph_not_available_p = 1;
23538 it->phys_ascent = it->ascent;
23539 it->phys_descent = it->descent;
23540 it->pixel_width = font->space_width;
23541 }
23542
23543 if (it->constrain_row_ascent_descent_p)
23544 {
23545 if (it->descent > it->max_descent)
23546 {
23547 it->ascent += it->descent - it->max_descent;
23548 it->descent = it->max_descent;
23549 }
23550 if (it->ascent > it->max_ascent)
23551 {
23552 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23553 it->ascent = it->max_ascent;
23554 }
23555 it->phys_ascent = min (it->phys_ascent, it->ascent);
23556 it->phys_descent = min (it->phys_descent, it->descent);
23557 extra_line_spacing = 0;
23558 }
23559
23560 /* If this is a space inside a region of text with
23561 `space-width' property, change its width. */
23562 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23563 if (stretched_p)
23564 it->pixel_width *= XFLOATINT (it->space_width);
23565
23566 /* If face has a box, add the box thickness to the character
23567 height. If character has a box line to the left and/or
23568 right, add the box line width to the character's width. */
23569 if (face->box != FACE_NO_BOX)
23570 {
23571 int thick = face->box_line_width;
23572
23573 if (thick > 0)
23574 {
23575 it->ascent += thick;
23576 it->descent += thick;
23577 }
23578 else
23579 thick = -thick;
23580
23581 if (it->start_of_box_run_p)
23582 it->pixel_width += thick;
23583 if (it->end_of_box_run_p)
23584 it->pixel_width += thick;
23585 }
23586
23587 /* If face has an overline, add the height of the overline
23588 (1 pixel) and a 1 pixel margin to the character height. */
23589 if (face->overline_p)
23590 it->ascent += overline_margin;
23591
23592 if (it->constrain_row_ascent_descent_p)
23593 {
23594 if (it->ascent > it->max_ascent)
23595 it->ascent = it->max_ascent;
23596 if (it->descent > it->max_descent)
23597 it->descent = it->max_descent;
23598 }
23599
23600 take_vertical_position_into_account (it);
23601
23602 /* If we have to actually produce glyphs, do it. */
23603 if (it->glyph_row)
23604 {
23605 if (stretched_p)
23606 {
23607 /* Translate a space with a `space-width' property
23608 into a stretch glyph. */
23609 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23610 / FONT_HEIGHT (font));
23611 append_stretch_glyph (it, it->object, it->pixel_width,
23612 it->ascent + it->descent, ascent);
23613 }
23614 else
23615 append_glyph (it);
23616
23617 /* If characters with lbearing or rbearing are displayed
23618 in this line, record that fact in a flag of the
23619 glyph row. This is used to optimize X output code. */
23620 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23621 it->glyph_row->contains_overlapping_glyphs_p = 1;
23622 }
23623 if (! stretched_p && it->pixel_width == 0)
23624 /* We assure that all visible glyphs have at least 1-pixel
23625 width. */
23626 it->pixel_width = 1;
23627 }
23628 else if (it->char_to_display == '\n')
23629 {
23630 /* A newline has no width, but we need the height of the
23631 line. But if previous part of the line sets a height,
23632 don't increase that height */
23633
23634 Lisp_Object height;
23635 Lisp_Object total_height = Qnil;
23636
23637 it->override_ascent = -1;
23638 it->pixel_width = 0;
23639 it->nglyphs = 0;
23640
23641 height = get_it_property (it, Qline_height);
23642 /* Split (line-height total-height) list */
23643 if (CONSP (height)
23644 && CONSP (XCDR (height))
23645 && NILP (XCDR (XCDR (height))))
23646 {
23647 total_height = XCAR (XCDR (height));
23648 height = XCAR (height);
23649 }
23650 height = calc_line_height_property (it, height, font, boff, 1);
23651
23652 if (it->override_ascent >= 0)
23653 {
23654 it->ascent = it->override_ascent;
23655 it->descent = it->override_descent;
23656 boff = it->override_boff;
23657 }
23658 else
23659 {
23660 it->ascent = FONT_BASE (font) + boff;
23661 it->descent = FONT_DESCENT (font) - boff;
23662 }
23663
23664 if (EQ (height, Qt))
23665 {
23666 if (it->descent > it->max_descent)
23667 {
23668 it->ascent += it->descent - it->max_descent;
23669 it->descent = it->max_descent;
23670 }
23671 if (it->ascent > it->max_ascent)
23672 {
23673 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23674 it->ascent = it->max_ascent;
23675 }
23676 it->phys_ascent = min (it->phys_ascent, it->ascent);
23677 it->phys_descent = min (it->phys_descent, it->descent);
23678 it->constrain_row_ascent_descent_p = 1;
23679 extra_line_spacing = 0;
23680 }
23681 else
23682 {
23683 Lisp_Object spacing;
23684
23685 it->phys_ascent = it->ascent;
23686 it->phys_descent = it->descent;
23687
23688 if ((it->max_ascent > 0 || it->max_descent > 0)
23689 && face->box != FACE_NO_BOX
23690 && face->box_line_width > 0)
23691 {
23692 it->ascent += face->box_line_width;
23693 it->descent += face->box_line_width;
23694 }
23695 if (!NILP (height)
23696 && XINT (height) > it->ascent + it->descent)
23697 it->ascent = XINT (height) - it->descent;
23698
23699 if (!NILP (total_height))
23700 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23701 else
23702 {
23703 spacing = get_it_property (it, Qline_spacing);
23704 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23705 }
23706 if (INTEGERP (spacing))
23707 {
23708 extra_line_spacing = XINT (spacing);
23709 if (!NILP (total_height))
23710 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23711 }
23712 }
23713 }
23714 else /* i.e. (it->char_to_display == '\t') */
23715 {
23716 if (font->space_width > 0)
23717 {
23718 int tab_width = it->tab_width * font->space_width;
23719 int x = it->current_x + it->continuation_lines_width;
23720 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23721
23722 /* If the distance from the current position to the next tab
23723 stop is less than a space character width, use the
23724 tab stop after that. */
23725 if (next_tab_x - x < font->space_width)
23726 next_tab_x += tab_width;
23727
23728 it->pixel_width = next_tab_x - x;
23729 it->nglyphs = 1;
23730 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23731 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23732
23733 if (it->glyph_row)
23734 {
23735 append_stretch_glyph (it, it->object, it->pixel_width,
23736 it->ascent + it->descent, it->ascent);
23737 }
23738 }
23739 else
23740 {
23741 it->pixel_width = 0;
23742 it->nglyphs = 1;
23743 }
23744 }
23745 }
23746 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23747 {
23748 /* A static composition.
23749
23750 Note: A composition is represented as one glyph in the
23751 glyph matrix. There are no padding glyphs.
23752
23753 Important note: pixel_width, ascent, and descent are the
23754 values of what is drawn by draw_glyphs (i.e. the values of
23755 the overall glyphs composed). */
23756 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23757 int boff; /* baseline offset */
23758 struct composition *cmp = composition_table[it->cmp_it.id];
23759 int glyph_len = cmp->glyph_len;
23760 struct font *font = face->font;
23761
23762 it->nglyphs = 1;
23763
23764 /* If we have not yet calculated pixel size data of glyphs of
23765 the composition for the current face font, calculate them
23766 now. Theoretically, we have to check all fonts for the
23767 glyphs, but that requires much time and memory space. So,
23768 here we check only the font of the first glyph. This may
23769 lead to incorrect display, but it's very rare, and C-l
23770 (recenter-top-bottom) can correct the display anyway. */
23771 if (! cmp->font || cmp->font != font)
23772 {
23773 /* Ascent and descent of the font of the first character
23774 of this composition (adjusted by baseline offset).
23775 Ascent and descent of overall glyphs should not be less
23776 than these, respectively. */
23777 int font_ascent, font_descent, font_height;
23778 /* Bounding box of the overall glyphs. */
23779 int leftmost, rightmost, lowest, highest;
23780 int lbearing, rbearing;
23781 int i, width, ascent, descent;
23782 int left_padded = 0, right_padded = 0;
23783 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23784 XChar2b char2b;
23785 struct font_metrics *pcm;
23786 int font_not_found_p;
23787 EMACS_INT pos;
23788
23789 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23790 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23791 break;
23792 if (glyph_len < cmp->glyph_len)
23793 right_padded = 1;
23794 for (i = 0; i < glyph_len; i++)
23795 {
23796 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23797 break;
23798 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23799 }
23800 if (i > 0)
23801 left_padded = 1;
23802
23803 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23804 : IT_CHARPOS (*it));
23805 /* If no suitable font is found, use the default font. */
23806 font_not_found_p = font == NULL;
23807 if (font_not_found_p)
23808 {
23809 face = face->ascii_face;
23810 font = face->font;
23811 }
23812 boff = font->baseline_offset;
23813 if (font->vertical_centering)
23814 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23815 font_ascent = FONT_BASE (font) + boff;
23816 font_descent = FONT_DESCENT (font) - boff;
23817 font_height = FONT_HEIGHT (font);
23818
23819 cmp->font = (void *) font;
23820
23821 pcm = NULL;
23822 if (! font_not_found_p)
23823 {
23824 get_char_face_and_encoding (it->f, c, it->face_id,
23825 &char2b, 0);
23826 pcm = get_per_char_metric (font, &char2b);
23827 }
23828
23829 /* Initialize the bounding box. */
23830 if (pcm)
23831 {
23832 width = pcm->width;
23833 ascent = pcm->ascent;
23834 descent = pcm->descent;
23835 lbearing = pcm->lbearing;
23836 rbearing = pcm->rbearing;
23837 }
23838 else
23839 {
23840 width = font->space_width;
23841 ascent = FONT_BASE (font);
23842 descent = FONT_DESCENT (font);
23843 lbearing = 0;
23844 rbearing = width;
23845 }
23846
23847 rightmost = width;
23848 leftmost = 0;
23849 lowest = - descent + boff;
23850 highest = ascent + boff;
23851
23852 if (! font_not_found_p
23853 && font->default_ascent
23854 && CHAR_TABLE_P (Vuse_default_ascent)
23855 && !NILP (Faref (Vuse_default_ascent,
23856 make_number (it->char_to_display))))
23857 highest = font->default_ascent + boff;
23858
23859 /* Draw the first glyph at the normal position. It may be
23860 shifted to right later if some other glyphs are drawn
23861 at the left. */
23862 cmp->offsets[i * 2] = 0;
23863 cmp->offsets[i * 2 + 1] = boff;
23864 cmp->lbearing = lbearing;
23865 cmp->rbearing = rbearing;
23866
23867 /* Set cmp->offsets for the remaining glyphs. */
23868 for (i++; i < glyph_len; i++)
23869 {
23870 int left, right, btm, top;
23871 int ch = COMPOSITION_GLYPH (cmp, i);
23872 int face_id;
23873 struct face *this_face;
23874
23875 if (ch == '\t')
23876 ch = ' ';
23877 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23878 this_face = FACE_FROM_ID (it->f, face_id);
23879 font = this_face->font;
23880
23881 if (font == NULL)
23882 pcm = NULL;
23883 else
23884 {
23885 get_char_face_and_encoding (it->f, ch, face_id,
23886 &char2b, 0);
23887 pcm = get_per_char_metric (font, &char2b);
23888 }
23889 if (! pcm)
23890 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23891 else
23892 {
23893 width = pcm->width;
23894 ascent = pcm->ascent;
23895 descent = pcm->descent;
23896 lbearing = pcm->lbearing;
23897 rbearing = pcm->rbearing;
23898 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23899 {
23900 /* Relative composition with or without
23901 alternate chars. */
23902 left = (leftmost + rightmost - width) / 2;
23903 btm = - descent + boff;
23904 if (font->relative_compose
23905 && (! CHAR_TABLE_P (Vignore_relative_composition)
23906 || NILP (Faref (Vignore_relative_composition,
23907 make_number (ch)))))
23908 {
23909
23910 if (- descent >= font->relative_compose)
23911 /* One extra pixel between two glyphs. */
23912 btm = highest + 1;
23913 else if (ascent <= 0)
23914 /* One extra pixel between two glyphs. */
23915 btm = lowest - 1 - ascent - descent;
23916 }
23917 }
23918 else
23919 {
23920 /* A composition rule is specified by an integer
23921 value that encodes global and new reference
23922 points (GREF and NREF). GREF and NREF are
23923 specified by numbers as below:
23924
23925 0---1---2 -- ascent
23926 | |
23927 | |
23928 | |
23929 9--10--11 -- center
23930 | |
23931 ---3---4---5--- baseline
23932 | |
23933 6---7---8 -- descent
23934 */
23935 int rule = COMPOSITION_RULE (cmp, i);
23936 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23937
23938 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23939 grefx = gref % 3, nrefx = nref % 3;
23940 grefy = gref / 3, nrefy = nref / 3;
23941 if (xoff)
23942 xoff = font_height * (xoff - 128) / 256;
23943 if (yoff)
23944 yoff = font_height * (yoff - 128) / 256;
23945
23946 left = (leftmost
23947 + grefx * (rightmost - leftmost) / 2
23948 - nrefx * width / 2
23949 + xoff);
23950
23951 btm = ((grefy == 0 ? highest
23952 : grefy == 1 ? 0
23953 : grefy == 2 ? lowest
23954 : (highest + lowest) / 2)
23955 - (nrefy == 0 ? ascent + descent
23956 : nrefy == 1 ? descent - boff
23957 : nrefy == 2 ? 0
23958 : (ascent + descent) / 2)
23959 + yoff);
23960 }
23961
23962 cmp->offsets[i * 2] = left;
23963 cmp->offsets[i * 2 + 1] = btm + descent;
23964
23965 /* Update the bounding box of the overall glyphs. */
23966 if (width > 0)
23967 {
23968 right = left + width;
23969 if (left < leftmost)
23970 leftmost = left;
23971 if (right > rightmost)
23972 rightmost = right;
23973 }
23974 top = btm + descent + ascent;
23975 if (top > highest)
23976 highest = top;
23977 if (btm < lowest)
23978 lowest = btm;
23979
23980 if (cmp->lbearing > left + lbearing)
23981 cmp->lbearing = left + lbearing;
23982 if (cmp->rbearing < left + rbearing)
23983 cmp->rbearing = left + rbearing;
23984 }
23985 }
23986
23987 /* If there are glyphs whose x-offsets are negative,
23988 shift all glyphs to the right and make all x-offsets
23989 non-negative. */
23990 if (leftmost < 0)
23991 {
23992 for (i = 0; i < cmp->glyph_len; i++)
23993 cmp->offsets[i * 2] -= leftmost;
23994 rightmost -= leftmost;
23995 cmp->lbearing -= leftmost;
23996 cmp->rbearing -= leftmost;
23997 }
23998
23999 if (left_padded && cmp->lbearing < 0)
24000 {
24001 for (i = 0; i < cmp->glyph_len; i++)
24002 cmp->offsets[i * 2] -= cmp->lbearing;
24003 rightmost -= cmp->lbearing;
24004 cmp->rbearing -= cmp->lbearing;
24005 cmp->lbearing = 0;
24006 }
24007 if (right_padded && rightmost < cmp->rbearing)
24008 {
24009 rightmost = cmp->rbearing;
24010 }
24011
24012 cmp->pixel_width = rightmost;
24013 cmp->ascent = highest;
24014 cmp->descent = - lowest;
24015 if (cmp->ascent < font_ascent)
24016 cmp->ascent = font_ascent;
24017 if (cmp->descent < font_descent)
24018 cmp->descent = font_descent;
24019 }
24020
24021 if (it->glyph_row
24022 && (cmp->lbearing < 0
24023 || cmp->rbearing > cmp->pixel_width))
24024 it->glyph_row->contains_overlapping_glyphs_p = 1;
24025
24026 it->pixel_width = cmp->pixel_width;
24027 it->ascent = it->phys_ascent = cmp->ascent;
24028 it->descent = it->phys_descent = cmp->descent;
24029 if (face->box != FACE_NO_BOX)
24030 {
24031 int thick = face->box_line_width;
24032
24033 if (thick > 0)
24034 {
24035 it->ascent += thick;
24036 it->descent += thick;
24037 }
24038 else
24039 thick = - thick;
24040
24041 if (it->start_of_box_run_p)
24042 it->pixel_width += thick;
24043 if (it->end_of_box_run_p)
24044 it->pixel_width += thick;
24045 }
24046
24047 /* If face has an overline, add the height of the overline
24048 (1 pixel) and a 1 pixel margin to the character height. */
24049 if (face->overline_p)
24050 it->ascent += overline_margin;
24051
24052 take_vertical_position_into_account (it);
24053 if (it->ascent < 0)
24054 it->ascent = 0;
24055 if (it->descent < 0)
24056 it->descent = 0;
24057
24058 if (it->glyph_row)
24059 append_composite_glyph (it);
24060 }
24061 else if (it->what == IT_COMPOSITION)
24062 {
24063 /* A dynamic (automatic) composition. */
24064 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24065 Lisp_Object gstring;
24066 struct font_metrics metrics;
24067
24068 it->nglyphs = 1;
24069
24070 gstring = composition_gstring_from_id (it->cmp_it.id);
24071 it->pixel_width
24072 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24073 &metrics);
24074 if (it->glyph_row
24075 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24076 it->glyph_row->contains_overlapping_glyphs_p = 1;
24077 it->ascent = it->phys_ascent = metrics.ascent;
24078 it->descent = it->phys_descent = metrics.descent;
24079 if (face->box != FACE_NO_BOX)
24080 {
24081 int thick = face->box_line_width;
24082
24083 if (thick > 0)
24084 {
24085 it->ascent += thick;
24086 it->descent += thick;
24087 }
24088 else
24089 thick = - thick;
24090
24091 if (it->start_of_box_run_p)
24092 it->pixel_width += thick;
24093 if (it->end_of_box_run_p)
24094 it->pixel_width += thick;
24095 }
24096 /* If face has an overline, add the height of the overline
24097 (1 pixel) and a 1 pixel margin to the character height. */
24098 if (face->overline_p)
24099 it->ascent += overline_margin;
24100 take_vertical_position_into_account (it);
24101 if (it->ascent < 0)
24102 it->ascent = 0;
24103 if (it->descent < 0)
24104 it->descent = 0;
24105
24106 if (it->glyph_row)
24107 append_composite_glyph (it);
24108 }
24109 else if (it->what == IT_GLYPHLESS)
24110 produce_glyphless_glyph (it, 0, Qnil);
24111 else if (it->what == IT_IMAGE)
24112 produce_image_glyph (it);
24113 else if (it->what == IT_STRETCH)
24114 produce_stretch_glyph (it);
24115
24116 done:
24117 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24118 because this isn't true for images with `:ascent 100'. */
24119 xassert (it->ascent >= 0 && it->descent >= 0);
24120 if (it->area == TEXT_AREA)
24121 it->current_x += it->pixel_width;
24122
24123 if (extra_line_spacing > 0)
24124 {
24125 it->descent += extra_line_spacing;
24126 if (extra_line_spacing > it->max_extra_line_spacing)
24127 it->max_extra_line_spacing = extra_line_spacing;
24128 }
24129
24130 it->max_ascent = max (it->max_ascent, it->ascent);
24131 it->max_descent = max (it->max_descent, it->descent);
24132 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24133 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24134 }
24135
24136 /* EXPORT for RIF:
24137 Output LEN glyphs starting at START at the nominal cursor position.
24138 Advance the nominal cursor over the text. The global variable
24139 updated_window contains the window being updated, updated_row is
24140 the glyph row being updated, and updated_area is the area of that
24141 row being updated. */
24142
24143 void
24144 x_write_glyphs (struct glyph *start, int len)
24145 {
24146 int x, hpos;
24147
24148 xassert (updated_window && updated_row);
24149 BLOCK_INPUT;
24150
24151 /* Write glyphs. */
24152
24153 hpos = start - updated_row->glyphs[updated_area];
24154 x = draw_glyphs (updated_window, output_cursor.x,
24155 updated_row, updated_area,
24156 hpos, hpos + len,
24157 DRAW_NORMAL_TEXT, 0);
24158
24159 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24160 if (updated_area == TEXT_AREA
24161 && updated_window->phys_cursor_on_p
24162 && updated_window->phys_cursor.vpos == output_cursor.vpos
24163 && updated_window->phys_cursor.hpos >= hpos
24164 && updated_window->phys_cursor.hpos < hpos + len)
24165 updated_window->phys_cursor_on_p = 0;
24166
24167 UNBLOCK_INPUT;
24168
24169 /* Advance the output cursor. */
24170 output_cursor.hpos += len;
24171 output_cursor.x = x;
24172 }
24173
24174
24175 /* EXPORT for RIF:
24176 Insert LEN glyphs from START at the nominal cursor position. */
24177
24178 void
24179 x_insert_glyphs (struct glyph *start, int len)
24180 {
24181 struct frame *f;
24182 struct window *w;
24183 int line_height, shift_by_width, shifted_region_width;
24184 struct glyph_row *row;
24185 struct glyph *glyph;
24186 int frame_x, frame_y;
24187 EMACS_INT hpos;
24188
24189 xassert (updated_window && updated_row);
24190 BLOCK_INPUT;
24191 w = updated_window;
24192 f = XFRAME (WINDOW_FRAME (w));
24193
24194 /* Get the height of the line we are in. */
24195 row = updated_row;
24196 line_height = row->height;
24197
24198 /* Get the width of the glyphs to insert. */
24199 shift_by_width = 0;
24200 for (glyph = start; glyph < start + len; ++glyph)
24201 shift_by_width += glyph->pixel_width;
24202
24203 /* Get the width of the region to shift right. */
24204 shifted_region_width = (window_box_width (w, updated_area)
24205 - output_cursor.x
24206 - shift_by_width);
24207
24208 /* Shift right. */
24209 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24210 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24211
24212 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24213 line_height, shift_by_width);
24214
24215 /* Write the glyphs. */
24216 hpos = start - row->glyphs[updated_area];
24217 draw_glyphs (w, output_cursor.x, row, updated_area,
24218 hpos, hpos + len,
24219 DRAW_NORMAL_TEXT, 0);
24220
24221 /* Advance the output cursor. */
24222 output_cursor.hpos += len;
24223 output_cursor.x += shift_by_width;
24224 UNBLOCK_INPUT;
24225 }
24226
24227
24228 /* EXPORT for RIF:
24229 Erase the current text line from the nominal cursor position
24230 (inclusive) to pixel column TO_X (exclusive). The idea is that
24231 everything from TO_X onward is already erased.
24232
24233 TO_X is a pixel position relative to updated_area of
24234 updated_window. TO_X == -1 means clear to the end of this area. */
24235
24236 void
24237 x_clear_end_of_line (int to_x)
24238 {
24239 struct frame *f;
24240 struct window *w = updated_window;
24241 int max_x, min_y, max_y;
24242 int from_x, from_y, to_y;
24243
24244 xassert (updated_window && updated_row);
24245 f = XFRAME (w->frame);
24246
24247 if (updated_row->full_width_p)
24248 max_x = WINDOW_TOTAL_WIDTH (w);
24249 else
24250 max_x = window_box_width (w, updated_area);
24251 max_y = window_text_bottom_y (w);
24252
24253 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24254 of window. For TO_X > 0, truncate to end of drawing area. */
24255 if (to_x == 0)
24256 return;
24257 else if (to_x < 0)
24258 to_x = max_x;
24259 else
24260 to_x = min (to_x, max_x);
24261
24262 to_y = min (max_y, output_cursor.y + updated_row->height);
24263
24264 /* Notice if the cursor will be cleared by this operation. */
24265 if (!updated_row->full_width_p)
24266 notice_overwritten_cursor (w, updated_area,
24267 output_cursor.x, -1,
24268 updated_row->y,
24269 MATRIX_ROW_BOTTOM_Y (updated_row));
24270
24271 from_x = output_cursor.x;
24272
24273 /* Translate to frame coordinates. */
24274 if (updated_row->full_width_p)
24275 {
24276 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24277 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24278 }
24279 else
24280 {
24281 int area_left = window_box_left (w, updated_area);
24282 from_x += area_left;
24283 to_x += area_left;
24284 }
24285
24286 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24287 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24288 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24289
24290 /* Prevent inadvertently clearing to end of the X window. */
24291 if (to_x > from_x && to_y > from_y)
24292 {
24293 BLOCK_INPUT;
24294 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24295 to_x - from_x, to_y - from_y);
24296 UNBLOCK_INPUT;
24297 }
24298 }
24299
24300 #endif /* HAVE_WINDOW_SYSTEM */
24301
24302
24303 \f
24304 /***********************************************************************
24305 Cursor types
24306 ***********************************************************************/
24307
24308 /* Value is the internal representation of the specified cursor type
24309 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24310 of the bar cursor. */
24311
24312 static enum text_cursor_kinds
24313 get_specified_cursor_type (Lisp_Object arg, int *width)
24314 {
24315 enum text_cursor_kinds type;
24316
24317 if (NILP (arg))
24318 return NO_CURSOR;
24319
24320 if (EQ (arg, Qbox))
24321 return FILLED_BOX_CURSOR;
24322
24323 if (EQ (arg, Qhollow))
24324 return HOLLOW_BOX_CURSOR;
24325
24326 if (EQ (arg, Qbar))
24327 {
24328 *width = 2;
24329 return BAR_CURSOR;
24330 }
24331
24332 if (CONSP (arg)
24333 && EQ (XCAR (arg), Qbar)
24334 && INTEGERP (XCDR (arg))
24335 && XINT (XCDR (arg)) >= 0)
24336 {
24337 *width = XINT (XCDR (arg));
24338 return BAR_CURSOR;
24339 }
24340
24341 if (EQ (arg, Qhbar))
24342 {
24343 *width = 2;
24344 return HBAR_CURSOR;
24345 }
24346
24347 if (CONSP (arg)
24348 && EQ (XCAR (arg), Qhbar)
24349 && INTEGERP (XCDR (arg))
24350 && XINT (XCDR (arg)) >= 0)
24351 {
24352 *width = XINT (XCDR (arg));
24353 return HBAR_CURSOR;
24354 }
24355
24356 /* Treat anything unknown as "hollow box cursor".
24357 It was bad to signal an error; people have trouble fixing
24358 .Xdefaults with Emacs, when it has something bad in it. */
24359 type = HOLLOW_BOX_CURSOR;
24360
24361 return type;
24362 }
24363
24364 /* Set the default cursor types for specified frame. */
24365 void
24366 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24367 {
24368 int width = 1;
24369 Lisp_Object tem;
24370
24371 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24372 FRAME_CURSOR_WIDTH (f) = width;
24373
24374 /* By default, set up the blink-off state depending on the on-state. */
24375
24376 tem = Fassoc (arg, Vblink_cursor_alist);
24377 if (!NILP (tem))
24378 {
24379 FRAME_BLINK_OFF_CURSOR (f)
24380 = get_specified_cursor_type (XCDR (tem), &width);
24381 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24382 }
24383 else
24384 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24385 }
24386
24387
24388 #ifdef HAVE_WINDOW_SYSTEM
24389
24390 /* Return the cursor we want to be displayed in window W. Return
24391 width of bar/hbar cursor through WIDTH arg. Return with
24392 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24393 (i.e. if the `system caret' should track this cursor).
24394
24395 In a mini-buffer window, we want the cursor only to appear if we
24396 are reading input from this window. For the selected window, we
24397 want the cursor type given by the frame parameter or buffer local
24398 setting of cursor-type. If explicitly marked off, draw no cursor.
24399 In all other cases, we want a hollow box cursor. */
24400
24401 static enum text_cursor_kinds
24402 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24403 int *active_cursor)
24404 {
24405 struct frame *f = XFRAME (w->frame);
24406 struct buffer *b = XBUFFER (w->buffer);
24407 int cursor_type = DEFAULT_CURSOR;
24408 Lisp_Object alt_cursor;
24409 int non_selected = 0;
24410
24411 *active_cursor = 1;
24412
24413 /* Echo area */
24414 if (cursor_in_echo_area
24415 && FRAME_HAS_MINIBUF_P (f)
24416 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24417 {
24418 if (w == XWINDOW (echo_area_window))
24419 {
24420 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24421 {
24422 *width = FRAME_CURSOR_WIDTH (f);
24423 return FRAME_DESIRED_CURSOR (f);
24424 }
24425 else
24426 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24427 }
24428
24429 *active_cursor = 0;
24430 non_selected = 1;
24431 }
24432
24433 /* Detect a nonselected window or nonselected frame. */
24434 else if (w != XWINDOW (f->selected_window)
24435 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24436 {
24437 *active_cursor = 0;
24438
24439 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24440 return NO_CURSOR;
24441
24442 non_selected = 1;
24443 }
24444
24445 /* Never display a cursor in a window in which cursor-type is nil. */
24446 if (NILP (BVAR (b, cursor_type)))
24447 return NO_CURSOR;
24448
24449 /* Get the normal cursor type for this window. */
24450 if (EQ (BVAR (b, cursor_type), Qt))
24451 {
24452 cursor_type = FRAME_DESIRED_CURSOR (f);
24453 *width = FRAME_CURSOR_WIDTH (f);
24454 }
24455 else
24456 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24457
24458 /* Use cursor-in-non-selected-windows instead
24459 for non-selected window or frame. */
24460 if (non_selected)
24461 {
24462 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24463 if (!EQ (Qt, alt_cursor))
24464 return get_specified_cursor_type (alt_cursor, width);
24465 /* t means modify the normal cursor type. */
24466 if (cursor_type == FILLED_BOX_CURSOR)
24467 cursor_type = HOLLOW_BOX_CURSOR;
24468 else if (cursor_type == BAR_CURSOR && *width > 1)
24469 --*width;
24470 return cursor_type;
24471 }
24472
24473 /* Use normal cursor if not blinked off. */
24474 if (!w->cursor_off_p)
24475 {
24476 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24477 {
24478 if (cursor_type == FILLED_BOX_CURSOR)
24479 {
24480 /* Using a block cursor on large images can be very annoying.
24481 So use a hollow cursor for "large" images.
24482 If image is not transparent (no mask), also use hollow cursor. */
24483 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24484 if (img != NULL && IMAGEP (img->spec))
24485 {
24486 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24487 where N = size of default frame font size.
24488 This should cover most of the "tiny" icons people may use. */
24489 if (!img->mask
24490 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24491 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24492 cursor_type = HOLLOW_BOX_CURSOR;
24493 }
24494 }
24495 else if (cursor_type != NO_CURSOR)
24496 {
24497 /* Display current only supports BOX and HOLLOW cursors for images.
24498 So for now, unconditionally use a HOLLOW cursor when cursor is
24499 not a solid box cursor. */
24500 cursor_type = HOLLOW_BOX_CURSOR;
24501 }
24502 }
24503 return cursor_type;
24504 }
24505
24506 /* Cursor is blinked off, so determine how to "toggle" it. */
24507
24508 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24509 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24510 return get_specified_cursor_type (XCDR (alt_cursor), width);
24511
24512 /* Then see if frame has specified a specific blink off cursor type. */
24513 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24514 {
24515 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24516 return FRAME_BLINK_OFF_CURSOR (f);
24517 }
24518
24519 #if 0
24520 /* Some people liked having a permanently visible blinking cursor,
24521 while others had very strong opinions against it. So it was
24522 decided to remove it. KFS 2003-09-03 */
24523
24524 /* Finally perform built-in cursor blinking:
24525 filled box <-> hollow box
24526 wide [h]bar <-> narrow [h]bar
24527 narrow [h]bar <-> no cursor
24528 other type <-> no cursor */
24529
24530 if (cursor_type == FILLED_BOX_CURSOR)
24531 return HOLLOW_BOX_CURSOR;
24532
24533 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24534 {
24535 *width = 1;
24536 return cursor_type;
24537 }
24538 #endif
24539
24540 return NO_CURSOR;
24541 }
24542
24543
24544 /* Notice when the text cursor of window W has been completely
24545 overwritten by a drawing operation that outputs glyphs in AREA
24546 starting at X0 and ending at X1 in the line starting at Y0 and
24547 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24548 the rest of the line after X0 has been written. Y coordinates
24549 are window-relative. */
24550
24551 static void
24552 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24553 int x0, int x1, int y0, int y1)
24554 {
24555 int cx0, cx1, cy0, cy1;
24556 struct glyph_row *row;
24557
24558 if (!w->phys_cursor_on_p)
24559 return;
24560 if (area != TEXT_AREA)
24561 return;
24562
24563 if (w->phys_cursor.vpos < 0
24564 || w->phys_cursor.vpos >= w->current_matrix->nrows
24565 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24566 !(row->enabled_p && row->displays_text_p)))
24567 return;
24568
24569 if (row->cursor_in_fringe_p)
24570 {
24571 row->cursor_in_fringe_p = 0;
24572 draw_fringe_bitmap (w, row, row->reversed_p);
24573 w->phys_cursor_on_p = 0;
24574 return;
24575 }
24576
24577 cx0 = w->phys_cursor.x;
24578 cx1 = cx0 + w->phys_cursor_width;
24579 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24580 return;
24581
24582 /* The cursor image will be completely removed from the
24583 screen if the output area intersects the cursor area in
24584 y-direction. When we draw in [y0 y1[, and some part of
24585 the cursor is at y < y0, that part must have been drawn
24586 before. When scrolling, the cursor is erased before
24587 actually scrolling, so we don't come here. When not
24588 scrolling, the rows above the old cursor row must have
24589 changed, and in this case these rows must have written
24590 over the cursor image.
24591
24592 Likewise if part of the cursor is below y1, with the
24593 exception of the cursor being in the first blank row at
24594 the buffer and window end because update_text_area
24595 doesn't draw that row. (Except when it does, but
24596 that's handled in update_text_area.) */
24597
24598 cy0 = w->phys_cursor.y;
24599 cy1 = cy0 + w->phys_cursor_height;
24600 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24601 return;
24602
24603 w->phys_cursor_on_p = 0;
24604 }
24605
24606 #endif /* HAVE_WINDOW_SYSTEM */
24607
24608 \f
24609 /************************************************************************
24610 Mouse Face
24611 ************************************************************************/
24612
24613 #ifdef HAVE_WINDOW_SYSTEM
24614
24615 /* EXPORT for RIF:
24616 Fix the display of area AREA of overlapping row ROW in window W
24617 with respect to the overlapping part OVERLAPS. */
24618
24619 void
24620 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24621 enum glyph_row_area area, int overlaps)
24622 {
24623 int i, x;
24624
24625 BLOCK_INPUT;
24626
24627 x = 0;
24628 for (i = 0; i < row->used[area];)
24629 {
24630 if (row->glyphs[area][i].overlaps_vertically_p)
24631 {
24632 int start = i, start_x = x;
24633
24634 do
24635 {
24636 x += row->glyphs[area][i].pixel_width;
24637 ++i;
24638 }
24639 while (i < row->used[area]
24640 && row->glyphs[area][i].overlaps_vertically_p);
24641
24642 draw_glyphs (w, start_x, row, area,
24643 start, i,
24644 DRAW_NORMAL_TEXT, overlaps);
24645 }
24646 else
24647 {
24648 x += row->glyphs[area][i].pixel_width;
24649 ++i;
24650 }
24651 }
24652
24653 UNBLOCK_INPUT;
24654 }
24655
24656
24657 /* EXPORT:
24658 Draw the cursor glyph of window W in glyph row ROW. See the
24659 comment of draw_glyphs for the meaning of HL. */
24660
24661 void
24662 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24663 enum draw_glyphs_face hl)
24664 {
24665 /* If cursor hpos is out of bounds, don't draw garbage. This can
24666 happen in mini-buffer windows when switching between echo area
24667 glyphs and mini-buffer. */
24668 if ((row->reversed_p
24669 ? (w->phys_cursor.hpos >= 0)
24670 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24671 {
24672 int on_p = w->phys_cursor_on_p;
24673 int x1;
24674 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24675 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24676 hl, 0);
24677 w->phys_cursor_on_p = on_p;
24678
24679 if (hl == DRAW_CURSOR)
24680 w->phys_cursor_width = x1 - w->phys_cursor.x;
24681 /* When we erase the cursor, and ROW is overlapped by other
24682 rows, make sure that these overlapping parts of other rows
24683 are redrawn. */
24684 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24685 {
24686 w->phys_cursor_width = x1 - w->phys_cursor.x;
24687
24688 if (row > w->current_matrix->rows
24689 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24690 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24691 OVERLAPS_ERASED_CURSOR);
24692
24693 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24694 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24695 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24696 OVERLAPS_ERASED_CURSOR);
24697 }
24698 }
24699 }
24700
24701
24702 /* EXPORT:
24703 Erase the image of a cursor of window W from the screen. */
24704
24705 void
24706 erase_phys_cursor (struct window *w)
24707 {
24708 struct frame *f = XFRAME (w->frame);
24709 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24710 int hpos = w->phys_cursor.hpos;
24711 int vpos = w->phys_cursor.vpos;
24712 int mouse_face_here_p = 0;
24713 struct glyph_matrix *active_glyphs = w->current_matrix;
24714 struct glyph_row *cursor_row;
24715 struct glyph *cursor_glyph;
24716 enum draw_glyphs_face hl;
24717
24718 /* No cursor displayed or row invalidated => nothing to do on the
24719 screen. */
24720 if (w->phys_cursor_type == NO_CURSOR)
24721 goto mark_cursor_off;
24722
24723 /* VPOS >= active_glyphs->nrows means that window has been resized.
24724 Don't bother to erase the cursor. */
24725 if (vpos >= active_glyphs->nrows)
24726 goto mark_cursor_off;
24727
24728 /* If row containing cursor is marked invalid, there is nothing we
24729 can do. */
24730 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24731 if (!cursor_row->enabled_p)
24732 goto mark_cursor_off;
24733
24734 /* If line spacing is > 0, old cursor may only be partially visible in
24735 window after split-window. So adjust visible height. */
24736 cursor_row->visible_height = min (cursor_row->visible_height,
24737 window_text_bottom_y (w) - cursor_row->y);
24738
24739 /* If row is completely invisible, don't attempt to delete a cursor which
24740 isn't there. This can happen if cursor is at top of a window, and
24741 we switch to a buffer with a header line in that window. */
24742 if (cursor_row->visible_height <= 0)
24743 goto mark_cursor_off;
24744
24745 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24746 if (cursor_row->cursor_in_fringe_p)
24747 {
24748 cursor_row->cursor_in_fringe_p = 0;
24749 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24750 goto mark_cursor_off;
24751 }
24752
24753 /* This can happen when the new row is shorter than the old one.
24754 In this case, either draw_glyphs or clear_end_of_line
24755 should have cleared the cursor. Note that we wouldn't be
24756 able to erase the cursor in this case because we don't have a
24757 cursor glyph at hand. */
24758 if ((cursor_row->reversed_p
24759 ? (w->phys_cursor.hpos < 0)
24760 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24761 goto mark_cursor_off;
24762
24763 /* If the cursor is in the mouse face area, redisplay that when
24764 we clear the cursor. */
24765 if (! NILP (hlinfo->mouse_face_window)
24766 && coords_in_mouse_face_p (w, hpos, vpos)
24767 /* Don't redraw the cursor's spot in mouse face if it is at the
24768 end of a line (on a newline). The cursor appears there, but
24769 mouse highlighting does not. */
24770 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24771 mouse_face_here_p = 1;
24772
24773 /* Maybe clear the display under the cursor. */
24774 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24775 {
24776 int x, y, left_x;
24777 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24778 int width;
24779
24780 cursor_glyph = get_phys_cursor_glyph (w);
24781 if (cursor_glyph == NULL)
24782 goto mark_cursor_off;
24783
24784 width = cursor_glyph->pixel_width;
24785 left_x = window_box_left_offset (w, TEXT_AREA);
24786 x = w->phys_cursor.x;
24787 if (x < left_x)
24788 width -= left_x - x;
24789 width = min (width, window_box_width (w, TEXT_AREA) - x);
24790 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24791 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24792
24793 if (width > 0)
24794 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24795 }
24796
24797 /* Erase the cursor by redrawing the character underneath it. */
24798 if (mouse_face_here_p)
24799 hl = DRAW_MOUSE_FACE;
24800 else
24801 hl = DRAW_NORMAL_TEXT;
24802 draw_phys_cursor_glyph (w, cursor_row, hl);
24803
24804 mark_cursor_off:
24805 w->phys_cursor_on_p = 0;
24806 w->phys_cursor_type = NO_CURSOR;
24807 }
24808
24809
24810 /* EXPORT:
24811 Display or clear cursor of window W. If ON is zero, clear the
24812 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24813 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24814
24815 void
24816 display_and_set_cursor (struct window *w, int on,
24817 int hpos, int vpos, int x, int y)
24818 {
24819 struct frame *f = XFRAME (w->frame);
24820 int new_cursor_type;
24821 int new_cursor_width;
24822 int active_cursor;
24823 struct glyph_row *glyph_row;
24824 struct glyph *glyph;
24825
24826 /* This is pointless on invisible frames, and dangerous on garbaged
24827 windows and frames; in the latter case, the frame or window may
24828 be in the midst of changing its size, and x and y may be off the
24829 window. */
24830 if (! FRAME_VISIBLE_P (f)
24831 || FRAME_GARBAGED_P (f)
24832 || vpos >= w->current_matrix->nrows
24833 || hpos >= w->current_matrix->matrix_w)
24834 return;
24835
24836 /* If cursor is off and we want it off, return quickly. */
24837 if (!on && !w->phys_cursor_on_p)
24838 return;
24839
24840 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24841 /* If cursor row is not enabled, we don't really know where to
24842 display the cursor. */
24843 if (!glyph_row->enabled_p)
24844 {
24845 w->phys_cursor_on_p = 0;
24846 return;
24847 }
24848
24849 glyph = NULL;
24850 if (!glyph_row->exact_window_width_line_p
24851 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24852 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24853
24854 xassert (interrupt_input_blocked);
24855
24856 /* Set new_cursor_type to the cursor we want to be displayed. */
24857 new_cursor_type = get_window_cursor_type (w, glyph,
24858 &new_cursor_width, &active_cursor);
24859
24860 /* If cursor is currently being shown and we don't want it to be or
24861 it is in the wrong place, or the cursor type is not what we want,
24862 erase it. */
24863 if (w->phys_cursor_on_p
24864 && (!on
24865 || w->phys_cursor.x != x
24866 || w->phys_cursor.y != y
24867 || new_cursor_type != w->phys_cursor_type
24868 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24869 && new_cursor_width != w->phys_cursor_width)))
24870 erase_phys_cursor (w);
24871
24872 /* Don't check phys_cursor_on_p here because that flag is only set
24873 to zero in some cases where we know that the cursor has been
24874 completely erased, to avoid the extra work of erasing the cursor
24875 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24876 still not be visible, or it has only been partly erased. */
24877 if (on)
24878 {
24879 w->phys_cursor_ascent = glyph_row->ascent;
24880 w->phys_cursor_height = glyph_row->height;
24881
24882 /* Set phys_cursor_.* before x_draw_.* is called because some
24883 of them may need the information. */
24884 w->phys_cursor.x = x;
24885 w->phys_cursor.y = glyph_row->y;
24886 w->phys_cursor.hpos = hpos;
24887 w->phys_cursor.vpos = vpos;
24888 }
24889
24890 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24891 new_cursor_type, new_cursor_width,
24892 on, active_cursor);
24893 }
24894
24895
24896 /* Switch the display of W's cursor on or off, according to the value
24897 of ON. */
24898
24899 static void
24900 update_window_cursor (struct window *w, int on)
24901 {
24902 /* Don't update cursor in windows whose frame is in the process
24903 of being deleted. */
24904 if (w->current_matrix)
24905 {
24906 BLOCK_INPUT;
24907 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24908 w->phys_cursor.x, w->phys_cursor.y);
24909 UNBLOCK_INPUT;
24910 }
24911 }
24912
24913
24914 /* Call update_window_cursor with parameter ON_P on all leaf windows
24915 in the window tree rooted at W. */
24916
24917 static void
24918 update_cursor_in_window_tree (struct window *w, int on_p)
24919 {
24920 while (w)
24921 {
24922 if (!NILP (w->hchild))
24923 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24924 else if (!NILP (w->vchild))
24925 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24926 else
24927 update_window_cursor (w, on_p);
24928
24929 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24930 }
24931 }
24932
24933
24934 /* EXPORT:
24935 Display the cursor on window W, or clear it, according to ON_P.
24936 Don't change the cursor's position. */
24937
24938 void
24939 x_update_cursor (struct frame *f, int on_p)
24940 {
24941 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24942 }
24943
24944
24945 /* EXPORT:
24946 Clear the cursor of window W to background color, and mark the
24947 cursor as not shown. This is used when the text where the cursor
24948 is about to be rewritten. */
24949
24950 void
24951 x_clear_cursor (struct window *w)
24952 {
24953 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24954 update_window_cursor (w, 0);
24955 }
24956
24957 #endif /* HAVE_WINDOW_SYSTEM */
24958
24959 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24960 and MSDOS. */
24961 static void
24962 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24963 int start_hpos, int end_hpos,
24964 enum draw_glyphs_face draw)
24965 {
24966 #ifdef HAVE_WINDOW_SYSTEM
24967 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24968 {
24969 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24970 return;
24971 }
24972 #endif
24973 #if defined (HAVE_GPM) || defined (MSDOS)
24974 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24975 #endif
24976 }
24977
24978 /* Display the active region described by mouse_face_* according to DRAW. */
24979
24980 static void
24981 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24982 {
24983 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24984 struct frame *f = XFRAME (WINDOW_FRAME (w));
24985
24986 if (/* If window is in the process of being destroyed, don't bother
24987 to do anything. */
24988 w->current_matrix != NULL
24989 /* Don't update mouse highlight if hidden */
24990 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24991 /* Recognize when we are called to operate on rows that don't exist
24992 anymore. This can happen when a window is split. */
24993 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24994 {
24995 int phys_cursor_on_p = w->phys_cursor_on_p;
24996 struct glyph_row *row, *first, *last;
24997
24998 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24999 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25000
25001 for (row = first; row <= last && row->enabled_p; ++row)
25002 {
25003 int start_hpos, end_hpos, start_x;
25004
25005 /* For all but the first row, the highlight starts at column 0. */
25006 if (row == first)
25007 {
25008 /* R2L rows have BEG and END in reversed order, but the
25009 screen drawing geometry is always left to right. So
25010 we need to mirror the beginning and end of the
25011 highlighted area in R2L rows. */
25012 if (!row->reversed_p)
25013 {
25014 start_hpos = hlinfo->mouse_face_beg_col;
25015 start_x = hlinfo->mouse_face_beg_x;
25016 }
25017 else if (row == last)
25018 {
25019 start_hpos = hlinfo->mouse_face_end_col;
25020 start_x = hlinfo->mouse_face_end_x;
25021 }
25022 else
25023 {
25024 start_hpos = 0;
25025 start_x = 0;
25026 }
25027 }
25028 else if (row->reversed_p && row == last)
25029 {
25030 start_hpos = hlinfo->mouse_face_end_col;
25031 start_x = hlinfo->mouse_face_end_x;
25032 }
25033 else
25034 {
25035 start_hpos = 0;
25036 start_x = 0;
25037 }
25038
25039 if (row == last)
25040 {
25041 if (!row->reversed_p)
25042 end_hpos = hlinfo->mouse_face_end_col;
25043 else if (row == first)
25044 end_hpos = hlinfo->mouse_face_beg_col;
25045 else
25046 {
25047 end_hpos = row->used[TEXT_AREA];
25048 if (draw == DRAW_NORMAL_TEXT)
25049 row->fill_line_p = 1; /* Clear to end of line */
25050 }
25051 }
25052 else if (row->reversed_p && row == first)
25053 end_hpos = hlinfo->mouse_face_beg_col;
25054 else
25055 {
25056 end_hpos = row->used[TEXT_AREA];
25057 if (draw == DRAW_NORMAL_TEXT)
25058 row->fill_line_p = 1; /* Clear to end of line */
25059 }
25060
25061 if (end_hpos > start_hpos)
25062 {
25063 draw_row_with_mouse_face (w, start_x, row,
25064 start_hpos, end_hpos, draw);
25065
25066 row->mouse_face_p
25067 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25068 }
25069 }
25070
25071 #ifdef HAVE_WINDOW_SYSTEM
25072 /* When we've written over the cursor, arrange for it to
25073 be displayed again. */
25074 if (FRAME_WINDOW_P (f)
25075 && phys_cursor_on_p && !w->phys_cursor_on_p)
25076 {
25077 BLOCK_INPUT;
25078 display_and_set_cursor (w, 1,
25079 w->phys_cursor.hpos, w->phys_cursor.vpos,
25080 w->phys_cursor.x, w->phys_cursor.y);
25081 UNBLOCK_INPUT;
25082 }
25083 #endif /* HAVE_WINDOW_SYSTEM */
25084 }
25085
25086 #ifdef HAVE_WINDOW_SYSTEM
25087 /* Change the mouse cursor. */
25088 if (FRAME_WINDOW_P (f))
25089 {
25090 if (draw == DRAW_NORMAL_TEXT
25091 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25092 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25093 else if (draw == DRAW_MOUSE_FACE)
25094 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25095 else
25096 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25097 }
25098 #endif /* HAVE_WINDOW_SYSTEM */
25099 }
25100
25101 /* EXPORT:
25102 Clear out the mouse-highlighted active region.
25103 Redraw it un-highlighted first. Value is non-zero if mouse
25104 face was actually drawn unhighlighted. */
25105
25106 int
25107 clear_mouse_face (Mouse_HLInfo *hlinfo)
25108 {
25109 int cleared = 0;
25110
25111 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25112 {
25113 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25114 cleared = 1;
25115 }
25116
25117 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25118 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25119 hlinfo->mouse_face_window = Qnil;
25120 hlinfo->mouse_face_overlay = Qnil;
25121 return cleared;
25122 }
25123
25124 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25125 within the mouse face on that window. */
25126 static int
25127 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25128 {
25129 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25130
25131 /* Quickly resolve the easy cases. */
25132 if (!(WINDOWP (hlinfo->mouse_face_window)
25133 && XWINDOW (hlinfo->mouse_face_window) == w))
25134 return 0;
25135 if (vpos < hlinfo->mouse_face_beg_row
25136 || vpos > hlinfo->mouse_face_end_row)
25137 return 0;
25138 if (vpos > hlinfo->mouse_face_beg_row
25139 && vpos < hlinfo->mouse_face_end_row)
25140 return 1;
25141
25142 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25143 {
25144 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25145 {
25146 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25147 return 1;
25148 }
25149 else if ((vpos == hlinfo->mouse_face_beg_row
25150 && hpos >= hlinfo->mouse_face_beg_col)
25151 || (vpos == hlinfo->mouse_face_end_row
25152 && hpos < hlinfo->mouse_face_end_col))
25153 return 1;
25154 }
25155 else
25156 {
25157 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25158 {
25159 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25160 return 1;
25161 }
25162 else if ((vpos == hlinfo->mouse_face_beg_row
25163 && hpos <= hlinfo->mouse_face_beg_col)
25164 || (vpos == hlinfo->mouse_face_end_row
25165 && hpos > hlinfo->mouse_face_end_col))
25166 return 1;
25167 }
25168 return 0;
25169 }
25170
25171
25172 /* EXPORT:
25173 Non-zero if physical cursor of window W is within mouse face. */
25174
25175 int
25176 cursor_in_mouse_face_p (struct window *w)
25177 {
25178 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
25179 }
25180
25181
25182 \f
25183 /* Find the glyph rows START_ROW and END_ROW of window W that display
25184 characters between buffer positions START_CHARPOS and END_CHARPOS
25185 (excluding END_CHARPOS). This is similar to row_containing_pos,
25186 but is more accurate when bidi reordering makes buffer positions
25187 change non-linearly with glyph rows. */
25188 static void
25189 rows_from_pos_range (struct window *w,
25190 EMACS_INT start_charpos, EMACS_INT end_charpos,
25191 struct glyph_row **start, struct glyph_row **end)
25192 {
25193 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25194 int last_y = window_text_bottom_y (w);
25195 struct glyph_row *row;
25196
25197 *start = NULL;
25198 *end = NULL;
25199
25200 while (!first->enabled_p
25201 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25202 first++;
25203
25204 /* Find the START row. */
25205 for (row = first;
25206 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25207 row++)
25208 {
25209 /* A row can potentially be the START row if the range of the
25210 characters it displays intersects the range
25211 [START_CHARPOS..END_CHARPOS). */
25212 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25213 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25214 /* See the commentary in row_containing_pos, for the
25215 explanation of the complicated way to check whether
25216 some position is beyond the end of the characters
25217 displayed by a row. */
25218 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25219 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25220 && !row->ends_at_zv_p
25221 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25222 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25223 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25224 && !row->ends_at_zv_p
25225 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25226 {
25227 /* Found a candidate row. Now make sure at least one of the
25228 glyphs it displays has a charpos from the range
25229 [START_CHARPOS..END_CHARPOS).
25230
25231 This is not obvious because bidi reordering could make
25232 buffer positions of a row be 1,2,3,102,101,100, and if we
25233 want to highlight characters in [50..60), we don't want
25234 this row, even though [50..60) does intersect [1..103),
25235 the range of character positions given by the row's start
25236 and end positions. */
25237 struct glyph *g = row->glyphs[TEXT_AREA];
25238 struct glyph *e = g + row->used[TEXT_AREA];
25239
25240 while (g < e)
25241 {
25242 if ((BUFFERP (g->object) || INTEGERP (g->object))
25243 && start_charpos <= g->charpos && g->charpos < end_charpos)
25244 *start = row;
25245 g++;
25246 }
25247 if (*start)
25248 break;
25249 }
25250 }
25251
25252 /* Find the END row. */
25253 if (!*start
25254 /* If the last row is partially visible, start looking for END
25255 from that row, instead of starting from FIRST. */
25256 && !(row->enabled_p
25257 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25258 row = first;
25259 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25260 {
25261 struct glyph_row *next = row + 1;
25262
25263 if (!next->enabled_p
25264 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25265 /* The first row >= START whose range of displayed characters
25266 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25267 is the row END + 1. */
25268 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
25269 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
25270 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25271 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25272 && !next->ends_at_zv_p
25273 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25274 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25275 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25276 && !next->ends_at_zv_p
25277 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25278 {
25279 *end = row;
25280 break;
25281 }
25282 else
25283 {
25284 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25285 but none of the characters it displays are in the range, it is
25286 also END + 1. */
25287 struct glyph *g = next->glyphs[TEXT_AREA];
25288 struct glyph *e = g + next->used[TEXT_AREA];
25289
25290 while (g < e)
25291 {
25292 if ((BUFFERP (g->object) || INTEGERP (g->object))
25293 && start_charpos <= g->charpos && g->charpos < end_charpos)
25294 break;
25295 g++;
25296 }
25297 if (g == e)
25298 {
25299 *end = row;
25300 break;
25301 }
25302 }
25303 }
25304 }
25305
25306 /* This function sets the mouse_face_* elements of HLINFO, assuming
25307 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25308 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25309 for the overlay or run of text properties specifying the mouse
25310 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25311 before-string and after-string that must also be highlighted.
25312 COVER_STRING, if non-nil, is a display string that may cover some
25313 or all of the highlighted text. */
25314
25315 static void
25316 mouse_face_from_buffer_pos (Lisp_Object window,
25317 Mouse_HLInfo *hlinfo,
25318 EMACS_INT mouse_charpos,
25319 EMACS_INT start_charpos,
25320 EMACS_INT end_charpos,
25321 Lisp_Object before_string,
25322 Lisp_Object after_string,
25323 Lisp_Object cover_string)
25324 {
25325 struct window *w = XWINDOW (window);
25326 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25327 struct glyph_row *r1, *r2;
25328 struct glyph *glyph, *end;
25329 EMACS_INT ignore, pos;
25330 int x;
25331
25332 xassert (NILP (cover_string) || STRINGP (cover_string));
25333 xassert (NILP (before_string) || STRINGP (before_string));
25334 xassert (NILP (after_string) || STRINGP (after_string));
25335
25336 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25337 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25338 if (r1 == NULL)
25339 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25340 /* If the before-string or display-string contains newlines,
25341 rows_from_pos_range skips to its last row. Move back. */
25342 if (!NILP (before_string) || !NILP (cover_string))
25343 {
25344 struct glyph_row *prev;
25345 while ((prev = r1 - 1, prev >= first)
25346 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25347 && prev->used[TEXT_AREA] > 0)
25348 {
25349 struct glyph *beg = prev->glyphs[TEXT_AREA];
25350 glyph = beg + prev->used[TEXT_AREA];
25351 while (--glyph >= beg && INTEGERP (glyph->object));
25352 if (glyph < beg
25353 || !(EQ (glyph->object, before_string)
25354 || EQ (glyph->object, cover_string)))
25355 break;
25356 r1 = prev;
25357 }
25358 }
25359 if (r2 == NULL)
25360 {
25361 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25362 hlinfo->mouse_face_past_end = 1;
25363 }
25364 else if (!NILP (after_string))
25365 {
25366 /* If the after-string has newlines, advance to its last row. */
25367 struct glyph_row *next;
25368 struct glyph_row *last
25369 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25370
25371 for (next = r2 + 1;
25372 next <= last
25373 && next->used[TEXT_AREA] > 0
25374 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25375 ++next)
25376 r2 = next;
25377 }
25378 /* The rest of the display engine assumes that mouse_face_beg_row is
25379 either above below mouse_face_end_row or identical to it. But
25380 with bidi-reordered continued lines, the row for START_CHARPOS
25381 could be below the row for END_CHARPOS. If so, swap the rows and
25382 store them in correct order. */
25383 if (r1->y > r2->y)
25384 {
25385 struct glyph_row *tem = r2;
25386
25387 r2 = r1;
25388 r1 = tem;
25389 }
25390
25391 hlinfo->mouse_face_beg_y = r1->y;
25392 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25393 hlinfo->mouse_face_end_y = r2->y;
25394 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25395
25396 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25397 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25398 could be anywhere in the row and in any order. The strategy
25399 below is to find the leftmost and the rightmost glyph that
25400 belongs to either of these 3 strings, or whose position is
25401 between START_CHARPOS and END_CHARPOS, and highlight all the
25402 glyphs between those two. This may cover more than just the text
25403 between START_CHARPOS and END_CHARPOS if the range of characters
25404 strides the bidi level boundary, e.g. if the beginning is in R2L
25405 text while the end is in L2R text or vice versa. */
25406 if (!r1->reversed_p)
25407 {
25408 /* This row is in a left to right paragraph. Scan it left to
25409 right. */
25410 glyph = r1->glyphs[TEXT_AREA];
25411 end = glyph + r1->used[TEXT_AREA];
25412 x = r1->x;
25413
25414 /* Skip truncation glyphs at the start of the glyph row. */
25415 if (r1->displays_text_p)
25416 for (; glyph < end
25417 && INTEGERP (glyph->object)
25418 && glyph->charpos < 0;
25419 ++glyph)
25420 x += glyph->pixel_width;
25421
25422 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25423 or COVER_STRING, and the first glyph from buffer whose
25424 position is between START_CHARPOS and END_CHARPOS. */
25425 for (; glyph < end
25426 && !INTEGERP (glyph->object)
25427 && !EQ (glyph->object, cover_string)
25428 && !(BUFFERP (glyph->object)
25429 && (glyph->charpos >= start_charpos
25430 && glyph->charpos < end_charpos));
25431 ++glyph)
25432 {
25433 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25434 are present at buffer positions between START_CHARPOS and
25435 END_CHARPOS, or if they come from an overlay. */
25436 if (EQ (glyph->object, before_string))
25437 {
25438 pos = string_buffer_position (before_string,
25439 start_charpos);
25440 /* If pos == 0, it means before_string came from an
25441 overlay, not from a buffer position. */
25442 if (!pos || (pos >= start_charpos && pos < end_charpos))
25443 break;
25444 }
25445 else if (EQ (glyph->object, after_string))
25446 {
25447 pos = string_buffer_position (after_string, end_charpos);
25448 if (!pos || (pos >= start_charpos && pos < end_charpos))
25449 break;
25450 }
25451 x += glyph->pixel_width;
25452 }
25453 hlinfo->mouse_face_beg_x = x;
25454 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25455 }
25456 else
25457 {
25458 /* This row is in a right to left paragraph. Scan it right to
25459 left. */
25460 struct glyph *g;
25461
25462 end = r1->glyphs[TEXT_AREA] - 1;
25463 glyph = end + r1->used[TEXT_AREA];
25464
25465 /* Skip truncation glyphs at the start of the glyph row. */
25466 if (r1->displays_text_p)
25467 for (; glyph > end
25468 && INTEGERP (glyph->object)
25469 && glyph->charpos < 0;
25470 --glyph)
25471 ;
25472
25473 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25474 or COVER_STRING, and the first glyph from buffer whose
25475 position is between START_CHARPOS and END_CHARPOS. */
25476 for (; glyph > end
25477 && !INTEGERP (glyph->object)
25478 && !EQ (glyph->object, cover_string)
25479 && !(BUFFERP (glyph->object)
25480 && (glyph->charpos >= start_charpos
25481 && glyph->charpos < end_charpos));
25482 --glyph)
25483 {
25484 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25485 are present at buffer positions between START_CHARPOS and
25486 END_CHARPOS, or if they come from an overlay. */
25487 if (EQ (glyph->object, before_string))
25488 {
25489 pos = string_buffer_position (before_string, start_charpos);
25490 /* If pos == 0, it means before_string came from an
25491 overlay, not from a buffer position. */
25492 if (!pos || (pos >= start_charpos && pos < end_charpos))
25493 break;
25494 }
25495 else if (EQ (glyph->object, after_string))
25496 {
25497 pos = string_buffer_position (after_string, end_charpos);
25498 if (!pos || (pos >= start_charpos && pos < end_charpos))
25499 break;
25500 }
25501 }
25502
25503 glyph++; /* first glyph to the right of the highlighted area */
25504 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25505 x += g->pixel_width;
25506 hlinfo->mouse_face_beg_x = x;
25507 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25508 }
25509
25510 /* If the highlight ends in a different row, compute GLYPH and END
25511 for the end row. Otherwise, reuse the values computed above for
25512 the row where the highlight begins. */
25513 if (r2 != r1)
25514 {
25515 if (!r2->reversed_p)
25516 {
25517 glyph = r2->glyphs[TEXT_AREA];
25518 end = glyph + r2->used[TEXT_AREA];
25519 x = r2->x;
25520 }
25521 else
25522 {
25523 end = r2->glyphs[TEXT_AREA] - 1;
25524 glyph = end + r2->used[TEXT_AREA];
25525 }
25526 }
25527
25528 if (!r2->reversed_p)
25529 {
25530 /* Skip truncation and continuation glyphs near the end of the
25531 row, and also blanks and stretch glyphs inserted by
25532 extend_face_to_end_of_line. */
25533 while (end > glyph
25534 && INTEGERP ((end - 1)->object)
25535 && (end - 1)->charpos <= 0)
25536 --end;
25537 /* Scan the rest of the glyph row from the end, looking for the
25538 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25539 COVER_STRING, or whose position is between START_CHARPOS
25540 and END_CHARPOS */
25541 for (--end;
25542 end > glyph
25543 && !INTEGERP (end->object)
25544 && !EQ (end->object, cover_string)
25545 && !(BUFFERP (end->object)
25546 && (end->charpos >= start_charpos
25547 && end->charpos < end_charpos));
25548 --end)
25549 {
25550 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25551 are present at buffer positions between START_CHARPOS and
25552 END_CHARPOS, or if they come from an overlay. */
25553 if (EQ (end->object, before_string))
25554 {
25555 pos = string_buffer_position (before_string, start_charpos);
25556 if (!pos || (pos >= start_charpos && pos < end_charpos))
25557 break;
25558 }
25559 else if (EQ (end->object, after_string))
25560 {
25561 pos = string_buffer_position (after_string, end_charpos);
25562 if (!pos || (pos >= start_charpos && pos < end_charpos))
25563 break;
25564 }
25565 }
25566 /* Find the X coordinate of the last glyph to be highlighted. */
25567 for (; glyph <= end; ++glyph)
25568 x += glyph->pixel_width;
25569
25570 hlinfo->mouse_face_end_x = x;
25571 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25572 }
25573 else
25574 {
25575 /* Skip truncation and continuation glyphs near the end of the
25576 row, and also blanks and stretch glyphs inserted by
25577 extend_face_to_end_of_line. */
25578 x = r2->x;
25579 end++;
25580 while (end < glyph
25581 && INTEGERP (end->object)
25582 && end->charpos <= 0)
25583 {
25584 x += end->pixel_width;
25585 ++end;
25586 }
25587 /* Scan the rest of the glyph row from the end, looking for the
25588 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25589 COVER_STRING, or whose position is between START_CHARPOS
25590 and END_CHARPOS */
25591 for ( ;
25592 end < glyph
25593 && !INTEGERP (end->object)
25594 && !EQ (end->object, cover_string)
25595 && !(BUFFERP (end->object)
25596 && (end->charpos >= start_charpos
25597 && end->charpos < end_charpos));
25598 ++end)
25599 {
25600 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25601 are present at buffer positions between START_CHARPOS and
25602 END_CHARPOS, or if they come from an overlay. */
25603 if (EQ (end->object, before_string))
25604 {
25605 pos = string_buffer_position (before_string, start_charpos);
25606 if (!pos || (pos >= start_charpos && pos < end_charpos))
25607 break;
25608 }
25609 else if (EQ (end->object, after_string))
25610 {
25611 pos = string_buffer_position (after_string, end_charpos);
25612 if (!pos || (pos >= start_charpos && pos < end_charpos))
25613 break;
25614 }
25615 x += end->pixel_width;
25616 }
25617 hlinfo->mouse_face_end_x = x;
25618 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25619 }
25620
25621 hlinfo->mouse_face_window = window;
25622 hlinfo->mouse_face_face_id
25623 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25624 mouse_charpos + 1,
25625 !hlinfo->mouse_face_hidden, -1);
25626 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25627 }
25628
25629 /* The following function is not used anymore (replaced with
25630 mouse_face_from_string_pos), but I leave it here for the time
25631 being, in case someone would. */
25632
25633 #if 0 /* not used */
25634
25635 /* Find the position of the glyph for position POS in OBJECT in
25636 window W's current matrix, and return in *X, *Y the pixel
25637 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25638
25639 RIGHT_P non-zero means return the position of the right edge of the
25640 glyph, RIGHT_P zero means return the left edge position.
25641
25642 If no glyph for POS exists in the matrix, return the position of
25643 the glyph with the next smaller position that is in the matrix, if
25644 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25645 exists in the matrix, return the position of the glyph with the
25646 next larger position in OBJECT.
25647
25648 Value is non-zero if a glyph was found. */
25649
25650 static int
25651 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25652 int *hpos, int *vpos, int *x, int *y, int right_p)
25653 {
25654 int yb = window_text_bottom_y (w);
25655 struct glyph_row *r;
25656 struct glyph *best_glyph = NULL;
25657 struct glyph_row *best_row = NULL;
25658 int best_x = 0;
25659
25660 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25661 r->enabled_p && r->y < yb;
25662 ++r)
25663 {
25664 struct glyph *g = r->glyphs[TEXT_AREA];
25665 struct glyph *e = g + r->used[TEXT_AREA];
25666 int gx;
25667
25668 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25669 if (EQ (g->object, object))
25670 {
25671 if (g->charpos == pos)
25672 {
25673 best_glyph = g;
25674 best_x = gx;
25675 best_row = r;
25676 goto found;
25677 }
25678 else if (best_glyph == NULL
25679 || ((eabs (g->charpos - pos)
25680 < eabs (best_glyph->charpos - pos))
25681 && (right_p
25682 ? g->charpos < pos
25683 : g->charpos > pos)))
25684 {
25685 best_glyph = g;
25686 best_x = gx;
25687 best_row = r;
25688 }
25689 }
25690 }
25691
25692 found:
25693
25694 if (best_glyph)
25695 {
25696 *x = best_x;
25697 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25698
25699 if (right_p)
25700 {
25701 *x += best_glyph->pixel_width;
25702 ++*hpos;
25703 }
25704
25705 *y = best_row->y;
25706 *vpos = best_row - w->current_matrix->rows;
25707 }
25708
25709 return best_glyph != NULL;
25710 }
25711 #endif /* not used */
25712
25713 /* Find the positions of the first and the last glyphs in window W's
25714 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25715 (assumed to be a string), and return in HLINFO's mouse_face_*
25716 members the pixel and column/row coordinates of those glyphs. */
25717
25718 static void
25719 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25720 Lisp_Object object,
25721 EMACS_INT startpos, EMACS_INT endpos)
25722 {
25723 int yb = window_text_bottom_y (w);
25724 struct glyph_row *r;
25725 struct glyph *g, *e;
25726 int gx;
25727 int found = 0;
25728
25729 /* Find the glyph row with at least one position in the range
25730 [STARTPOS..ENDPOS], and the first glyph in that row whose
25731 position belongs to that range. */
25732 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25733 r->enabled_p && r->y < yb;
25734 ++r)
25735 {
25736 if (!r->reversed_p)
25737 {
25738 g = r->glyphs[TEXT_AREA];
25739 e = g + r->used[TEXT_AREA];
25740 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25741 if (EQ (g->object, object)
25742 && startpos <= g->charpos && g->charpos <= endpos)
25743 {
25744 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25745 hlinfo->mouse_face_beg_y = r->y;
25746 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25747 hlinfo->mouse_face_beg_x = gx;
25748 found = 1;
25749 break;
25750 }
25751 }
25752 else
25753 {
25754 struct glyph *g1;
25755
25756 e = r->glyphs[TEXT_AREA];
25757 g = e + r->used[TEXT_AREA];
25758 for ( ; g > e; --g)
25759 if (EQ ((g-1)->object, object)
25760 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25761 {
25762 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25763 hlinfo->mouse_face_beg_y = r->y;
25764 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25765 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25766 gx += g1->pixel_width;
25767 hlinfo->mouse_face_beg_x = gx;
25768 found = 1;
25769 break;
25770 }
25771 }
25772 if (found)
25773 break;
25774 }
25775
25776 if (!found)
25777 return;
25778
25779 /* Starting with the next row, look for the first row which does NOT
25780 include any glyphs whose positions are in the range. */
25781 for (++r; r->enabled_p && r->y < yb; ++r)
25782 {
25783 g = r->glyphs[TEXT_AREA];
25784 e = g + r->used[TEXT_AREA];
25785 found = 0;
25786 for ( ; g < e; ++g)
25787 if (EQ (g->object, object)
25788 && startpos <= g->charpos && g->charpos <= endpos)
25789 {
25790 found = 1;
25791 break;
25792 }
25793 if (!found)
25794 break;
25795 }
25796
25797 /* The highlighted region ends on the previous row. */
25798 r--;
25799
25800 /* Set the end row and its vertical pixel coordinate. */
25801 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25802 hlinfo->mouse_face_end_y = r->y;
25803
25804 /* Compute and set the end column and the end column's horizontal
25805 pixel coordinate. */
25806 if (!r->reversed_p)
25807 {
25808 g = r->glyphs[TEXT_AREA];
25809 e = g + r->used[TEXT_AREA];
25810 for ( ; e > g; --e)
25811 if (EQ ((e-1)->object, object)
25812 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25813 break;
25814 hlinfo->mouse_face_end_col = e - g;
25815
25816 for (gx = r->x; g < e; ++g)
25817 gx += g->pixel_width;
25818 hlinfo->mouse_face_end_x = gx;
25819 }
25820 else
25821 {
25822 e = r->glyphs[TEXT_AREA];
25823 g = e + r->used[TEXT_AREA];
25824 for (gx = r->x ; e < g; ++e)
25825 {
25826 if (EQ (e->object, object)
25827 && startpos <= e->charpos && e->charpos <= endpos)
25828 break;
25829 gx += e->pixel_width;
25830 }
25831 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25832 hlinfo->mouse_face_end_x = gx;
25833 }
25834 }
25835
25836 #ifdef HAVE_WINDOW_SYSTEM
25837
25838 /* See if position X, Y is within a hot-spot of an image. */
25839
25840 static int
25841 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25842 {
25843 if (!CONSP (hot_spot))
25844 return 0;
25845
25846 if (EQ (XCAR (hot_spot), Qrect))
25847 {
25848 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25849 Lisp_Object rect = XCDR (hot_spot);
25850 Lisp_Object tem;
25851 if (!CONSP (rect))
25852 return 0;
25853 if (!CONSP (XCAR (rect)))
25854 return 0;
25855 if (!CONSP (XCDR (rect)))
25856 return 0;
25857 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25858 return 0;
25859 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25860 return 0;
25861 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25862 return 0;
25863 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25864 return 0;
25865 return 1;
25866 }
25867 else if (EQ (XCAR (hot_spot), Qcircle))
25868 {
25869 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25870 Lisp_Object circ = XCDR (hot_spot);
25871 Lisp_Object lr, lx0, ly0;
25872 if (CONSP (circ)
25873 && CONSP (XCAR (circ))
25874 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25875 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25876 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25877 {
25878 double r = XFLOATINT (lr);
25879 double dx = XINT (lx0) - x;
25880 double dy = XINT (ly0) - y;
25881 return (dx * dx + dy * dy <= r * r);
25882 }
25883 }
25884 else if (EQ (XCAR (hot_spot), Qpoly))
25885 {
25886 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25887 if (VECTORP (XCDR (hot_spot)))
25888 {
25889 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25890 Lisp_Object *poly = v->contents;
25891 int n = v->header.size;
25892 int i;
25893 int inside = 0;
25894 Lisp_Object lx, ly;
25895 int x0, y0;
25896
25897 /* Need an even number of coordinates, and at least 3 edges. */
25898 if (n < 6 || n & 1)
25899 return 0;
25900
25901 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25902 If count is odd, we are inside polygon. Pixels on edges
25903 may or may not be included depending on actual geometry of the
25904 polygon. */
25905 if ((lx = poly[n-2], !INTEGERP (lx))
25906 || (ly = poly[n-1], !INTEGERP (lx)))
25907 return 0;
25908 x0 = XINT (lx), y0 = XINT (ly);
25909 for (i = 0; i < n; i += 2)
25910 {
25911 int x1 = x0, y1 = y0;
25912 if ((lx = poly[i], !INTEGERP (lx))
25913 || (ly = poly[i+1], !INTEGERP (ly)))
25914 return 0;
25915 x0 = XINT (lx), y0 = XINT (ly);
25916
25917 /* Does this segment cross the X line? */
25918 if (x0 >= x)
25919 {
25920 if (x1 >= x)
25921 continue;
25922 }
25923 else if (x1 < x)
25924 continue;
25925 if (y > y0 && y > y1)
25926 continue;
25927 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25928 inside = !inside;
25929 }
25930 return inside;
25931 }
25932 }
25933 return 0;
25934 }
25935
25936 Lisp_Object
25937 find_hot_spot (Lisp_Object map, int x, int y)
25938 {
25939 while (CONSP (map))
25940 {
25941 if (CONSP (XCAR (map))
25942 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25943 return XCAR (map);
25944 map = XCDR (map);
25945 }
25946
25947 return Qnil;
25948 }
25949
25950 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25951 3, 3, 0,
25952 doc: /* Lookup in image map MAP coordinates X and Y.
25953 An image map is an alist where each element has the format (AREA ID PLIST).
25954 An AREA is specified as either a rectangle, a circle, or a polygon:
25955 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25956 pixel coordinates of the upper left and bottom right corners.
25957 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25958 and the radius of the circle; r may be a float or integer.
25959 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25960 vector describes one corner in the polygon.
25961 Returns the alist element for the first matching AREA in MAP. */)
25962 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25963 {
25964 if (NILP (map))
25965 return Qnil;
25966
25967 CHECK_NUMBER (x);
25968 CHECK_NUMBER (y);
25969
25970 return find_hot_spot (map, XINT (x), XINT (y));
25971 }
25972
25973
25974 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25975 static void
25976 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25977 {
25978 /* Do not change cursor shape while dragging mouse. */
25979 if (!NILP (do_mouse_tracking))
25980 return;
25981
25982 if (!NILP (pointer))
25983 {
25984 if (EQ (pointer, Qarrow))
25985 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25986 else if (EQ (pointer, Qhand))
25987 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25988 else if (EQ (pointer, Qtext))
25989 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25990 else if (EQ (pointer, intern ("hdrag")))
25991 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25992 #ifdef HAVE_X_WINDOWS
25993 else if (EQ (pointer, intern ("vdrag")))
25994 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25995 #endif
25996 else if (EQ (pointer, intern ("hourglass")))
25997 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25998 else if (EQ (pointer, Qmodeline))
25999 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26000 else
26001 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26002 }
26003
26004 if (cursor != No_Cursor)
26005 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26006 }
26007
26008 #endif /* HAVE_WINDOW_SYSTEM */
26009
26010 /* Take proper action when mouse has moved to the mode or header line
26011 or marginal area AREA of window W, x-position X and y-position Y.
26012 X is relative to the start of the text display area of W, so the
26013 width of bitmap areas and scroll bars must be subtracted to get a
26014 position relative to the start of the mode line. */
26015
26016 static void
26017 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26018 enum window_part area)
26019 {
26020 struct window *w = XWINDOW (window);
26021 struct frame *f = XFRAME (w->frame);
26022 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26023 #ifdef HAVE_WINDOW_SYSTEM
26024 Display_Info *dpyinfo;
26025 #endif
26026 Cursor cursor = No_Cursor;
26027 Lisp_Object pointer = Qnil;
26028 int dx, dy, width, height;
26029 EMACS_INT charpos;
26030 Lisp_Object string, object = Qnil;
26031 Lisp_Object pos, help;
26032
26033 Lisp_Object mouse_face;
26034 int original_x_pixel = x;
26035 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26036 struct glyph_row *row;
26037
26038 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26039 {
26040 int x0;
26041 struct glyph *end;
26042
26043 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26044 returns them in row/column units! */
26045 string = mode_line_string (w, area, &x, &y, &charpos,
26046 &object, &dx, &dy, &width, &height);
26047
26048 row = (area == ON_MODE_LINE
26049 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26050 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26051
26052 /* Find the glyph under the mouse pointer. */
26053 if (row->mode_line_p && row->enabled_p)
26054 {
26055 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26056 end = glyph + row->used[TEXT_AREA];
26057
26058 for (x0 = original_x_pixel;
26059 glyph < end && x0 >= glyph->pixel_width;
26060 ++glyph)
26061 x0 -= glyph->pixel_width;
26062
26063 if (glyph >= end)
26064 glyph = NULL;
26065 }
26066 }
26067 else
26068 {
26069 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26070 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26071 returns them in row/column units! */
26072 string = marginal_area_string (w, area, &x, &y, &charpos,
26073 &object, &dx, &dy, &width, &height);
26074 }
26075
26076 help = Qnil;
26077
26078 #ifdef HAVE_WINDOW_SYSTEM
26079 if (IMAGEP (object))
26080 {
26081 Lisp_Object image_map, hotspot;
26082 if ((image_map = Fplist_get (XCDR (object), QCmap),
26083 !NILP (image_map))
26084 && (hotspot = find_hot_spot (image_map, dx, dy),
26085 CONSP (hotspot))
26086 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26087 {
26088 Lisp_Object plist;
26089
26090 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26091 If so, we could look for mouse-enter, mouse-leave
26092 properties in PLIST (and do something...). */
26093 hotspot = XCDR (hotspot);
26094 if (CONSP (hotspot)
26095 && (plist = XCAR (hotspot), CONSP (plist)))
26096 {
26097 pointer = Fplist_get (plist, Qpointer);
26098 if (NILP (pointer))
26099 pointer = Qhand;
26100 help = Fplist_get (plist, Qhelp_echo);
26101 if (!NILP (help))
26102 {
26103 help_echo_string = help;
26104 /* Is this correct? ++kfs */
26105 XSETWINDOW (help_echo_window, w);
26106 help_echo_object = w->buffer;
26107 help_echo_pos = charpos;
26108 }
26109 }
26110 }
26111 if (NILP (pointer))
26112 pointer = Fplist_get (XCDR (object), QCpointer);
26113 }
26114 #endif /* HAVE_WINDOW_SYSTEM */
26115
26116 if (STRINGP (string))
26117 {
26118 pos = make_number (charpos);
26119 /* If we're on a string with `help-echo' text property, arrange
26120 for the help to be displayed. This is done by setting the
26121 global variable help_echo_string to the help string. */
26122 if (NILP (help))
26123 {
26124 help = Fget_text_property (pos, Qhelp_echo, string);
26125 if (!NILP (help))
26126 {
26127 help_echo_string = help;
26128 XSETWINDOW (help_echo_window, w);
26129 help_echo_object = string;
26130 help_echo_pos = charpos;
26131 }
26132 }
26133
26134 #ifdef HAVE_WINDOW_SYSTEM
26135 if (FRAME_WINDOW_P (f))
26136 {
26137 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26138 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26139 if (NILP (pointer))
26140 pointer = Fget_text_property (pos, Qpointer, string);
26141
26142 /* Change the mouse pointer according to what is under X/Y. */
26143 if (NILP (pointer)
26144 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26145 {
26146 Lisp_Object map;
26147 map = Fget_text_property (pos, Qlocal_map, string);
26148 if (!KEYMAPP (map))
26149 map = Fget_text_property (pos, Qkeymap, string);
26150 if (!KEYMAPP (map))
26151 cursor = dpyinfo->vertical_scroll_bar_cursor;
26152 }
26153 }
26154 #endif
26155
26156 /* Change the mouse face according to what is under X/Y. */
26157 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26158 if (!NILP (mouse_face)
26159 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26160 && glyph)
26161 {
26162 Lisp_Object b, e;
26163
26164 struct glyph * tmp_glyph;
26165
26166 int gpos;
26167 int gseq_length;
26168 int total_pixel_width;
26169 EMACS_INT begpos, endpos, ignore;
26170
26171 int vpos, hpos;
26172
26173 b = Fprevious_single_property_change (make_number (charpos + 1),
26174 Qmouse_face, string, Qnil);
26175 if (NILP (b))
26176 begpos = 0;
26177 else
26178 begpos = XINT (b);
26179
26180 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26181 if (NILP (e))
26182 endpos = SCHARS (string);
26183 else
26184 endpos = XINT (e);
26185
26186 /* Calculate the glyph position GPOS of GLYPH in the
26187 displayed string, relative to the beginning of the
26188 highlighted part of the string.
26189
26190 Note: GPOS is different from CHARPOS. CHARPOS is the
26191 position of GLYPH in the internal string object. A mode
26192 line string format has structures which are converted to
26193 a flattened string by the Emacs Lisp interpreter. The
26194 internal string is an element of those structures. The
26195 displayed string is the flattened string. */
26196 tmp_glyph = row_start_glyph;
26197 while (tmp_glyph < glyph
26198 && (!(EQ (tmp_glyph->object, glyph->object)
26199 && begpos <= tmp_glyph->charpos
26200 && tmp_glyph->charpos < endpos)))
26201 tmp_glyph++;
26202 gpos = glyph - tmp_glyph;
26203
26204 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26205 the highlighted part of the displayed string to which
26206 GLYPH belongs. Note: GSEQ_LENGTH is different from
26207 SCHARS (STRING), because the latter returns the length of
26208 the internal string. */
26209 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26210 tmp_glyph > glyph
26211 && (!(EQ (tmp_glyph->object, glyph->object)
26212 && begpos <= tmp_glyph->charpos
26213 && tmp_glyph->charpos < endpos));
26214 tmp_glyph--)
26215 ;
26216 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26217
26218 /* Calculate the total pixel width of all the glyphs between
26219 the beginning of the highlighted area and GLYPH. */
26220 total_pixel_width = 0;
26221 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26222 total_pixel_width += tmp_glyph->pixel_width;
26223
26224 /* Pre calculation of re-rendering position. Note: X is in
26225 column units here, after the call to mode_line_string or
26226 marginal_area_string. */
26227 hpos = x - gpos;
26228 vpos = (area == ON_MODE_LINE
26229 ? (w->current_matrix)->nrows - 1
26230 : 0);
26231
26232 /* If GLYPH's position is included in the region that is
26233 already drawn in mouse face, we have nothing to do. */
26234 if ( EQ (window, hlinfo->mouse_face_window)
26235 && (!row->reversed_p
26236 ? (hlinfo->mouse_face_beg_col <= hpos
26237 && hpos < hlinfo->mouse_face_end_col)
26238 /* In R2L rows we swap BEG and END, see below. */
26239 : (hlinfo->mouse_face_end_col <= hpos
26240 && hpos < hlinfo->mouse_face_beg_col))
26241 && hlinfo->mouse_face_beg_row == vpos )
26242 return;
26243
26244 if (clear_mouse_face (hlinfo))
26245 cursor = No_Cursor;
26246
26247 if (!row->reversed_p)
26248 {
26249 hlinfo->mouse_face_beg_col = hpos;
26250 hlinfo->mouse_face_beg_x = original_x_pixel
26251 - (total_pixel_width + dx);
26252 hlinfo->mouse_face_end_col = hpos + gseq_length;
26253 hlinfo->mouse_face_end_x = 0;
26254 }
26255 else
26256 {
26257 /* In R2L rows, show_mouse_face expects BEG and END
26258 coordinates to be swapped. */
26259 hlinfo->mouse_face_end_col = hpos;
26260 hlinfo->mouse_face_end_x = original_x_pixel
26261 - (total_pixel_width + dx);
26262 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26263 hlinfo->mouse_face_beg_x = 0;
26264 }
26265
26266 hlinfo->mouse_face_beg_row = vpos;
26267 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26268 hlinfo->mouse_face_beg_y = 0;
26269 hlinfo->mouse_face_end_y = 0;
26270 hlinfo->mouse_face_past_end = 0;
26271 hlinfo->mouse_face_window = window;
26272
26273 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26274 charpos,
26275 0, 0, 0,
26276 &ignore,
26277 glyph->face_id,
26278 1);
26279 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26280
26281 if (NILP (pointer))
26282 pointer = Qhand;
26283 }
26284 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26285 clear_mouse_face (hlinfo);
26286 }
26287 #ifdef HAVE_WINDOW_SYSTEM
26288 if (FRAME_WINDOW_P (f))
26289 define_frame_cursor1 (f, cursor, pointer);
26290 #endif
26291 }
26292
26293
26294 /* EXPORT:
26295 Take proper action when the mouse has moved to position X, Y on
26296 frame F as regards highlighting characters that have mouse-face
26297 properties. Also de-highlighting chars where the mouse was before.
26298 X and Y can be negative or out of range. */
26299
26300 void
26301 note_mouse_highlight (struct frame *f, int x, int y)
26302 {
26303 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26304 enum window_part part;
26305 Lisp_Object window;
26306 struct window *w;
26307 Cursor cursor = No_Cursor;
26308 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26309 struct buffer *b;
26310
26311 /* When a menu is active, don't highlight because this looks odd. */
26312 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26313 if (popup_activated ())
26314 return;
26315 #endif
26316
26317 if (NILP (Vmouse_highlight)
26318 || !f->glyphs_initialized_p
26319 || f->pointer_invisible)
26320 return;
26321
26322 hlinfo->mouse_face_mouse_x = x;
26323 hlinfo->mouse_face_mouse_y = y;
26324 hlinfo->mouse_face_mouse_frame = f;
26325
26326 if (hlinfo->mouse_face_defer)
26327 return;
26328
26329 if (gc_in_progress)
26330 {
26331 hlinfo->mouse_face_deferred_gc = 1;
26332 return;
26333 }
26334
26335 /* Which window is that in? */
26336 window = window_from_coordinates (f, x, y, &part, 1);
26337
26338 /* If we were displaying active text in another window, clear that.
26339 Also clear if we move out of text area in same window. */
26340 if (! EQ (window, hlinfo->mouse_face_window)
26341 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26342 && !NILP (hlinfo->mouse_face_window)))
26343 clear_mouse_face (hlinfo);
26344
26345 /* Not on a window -> return. */
26346 if (!WINDOWP (window))
26347 return;
26348
26349 /* Reset help_echo_string. It will get recomputed below. */
26350 help_echo_string = Qnil;
26351
26352 /* Convert to window-relative pixel coordinates. */
26353 w = XWINDOW (window);
26354 frame_to_window_pixel_xy (w, &x, &y);
26355
26356 #ifdef HAVE_WINDOW_SYSTEM
26357 /* Handle tool-bar window differently since it doesn't display a
26358 buffer. */
26359 if (EQ (window, f->tool_bar_window))
26360 {
26361 note_tool_bar_highlight (f, x, y);
26362 return;
26363 }
26364 #endif
26365
26366 /* Mouse is on the mode, header line or margin? */
26367 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26368 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26369 {
26370 note_mode_line_or_margin_highlight (window, x, y, part);
26371 return;
26372 }
26373
26374 #ifdef HAVE_WINDOW_SYSTEM
26375 if (part == ON_VERTICAL_BORDER)
26376 {
26377 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26378 help_echo_string = build_string ("drag-mouse-1: resize");
26379 }
26380 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26381 || part == ON_SCROLL_BAR)
26382 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26383 else
26384 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26385 #endif
26386
26387 /* Are we in a window whose display is up to date?
26388 And verify the buffer's text has not changed. */
26389 b = XBUFFER (w->buffer);
26390 if (part == ON_TEXT
26391 && EQ (w->window_end_valid, w->buffer)
26392 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26393 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26394 {
26395 int hpos, vpos, dx, dy, area;
26396 EMACS_INT pos;
26397 struct glyph *glyph;
26398 Lisp_Object object;
26399 Lisp_Object mouse_face = Qnil, position;
26400 Lisp_Object *overlay_vec = NULL;
26401 ptrdiff_t i, noverlays;
26402 struct buffer *obuf;
26403 EMACS_INT obegv, ozv;
26404 int same_region;
26405
26406 /* Find the glyph under X/Y. */
26407 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26408
26409 #ifdef HAVE_WINDOW_SYSTEM
26410 /* Look for :pointer property on image. */
26411 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26412 {
26413 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26414 if (img != NULL && IMAGEP (img->spec))
26415 {
26416 Lisp_Object image_map, hotspot;
26417 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26418 !NILP (image_map))
26419 && (hotspot = find_hot_spot (image_map,
26420 glyph->slice.img.x + dx,
26421 glyph->slice.img.y + dy),
26422 CONSP (hotspot))
26423 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26424 {
26425 Lisp_Object plist;
26426
26427 /* Could check XCAR (hotspot) to see if we enter/leave
26428 this hot-spot.
26429 If so, we could look for mouse-enter, mouse-leave
26430 properties in PLIST (and do something...). */
26431 hotspot = XCDR (hotspot);
26432 if (CONSP (hotspot)
26433 && (plist = XCAR (hotspot), CONSP (plist)))
26434 {
26435 pointer = Fplist_get (plist, Qpointer);
26436 if (NILP (pointer))
26437 pointer = Qhand;
26438 help_echo_string = Fplist_get (plist, Qhelp_echo);
26439 if (!NILP (help_echo_string))
26440 {
26441 help_echo_window = window;
26442 help_echo_object = glyph->object;
26443 help_echo_pos = glyph->charpos;
26444 }
26445 }
26446 }
26447 if (NILP (pointer))
26448 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26449 }
26450 }
26451 #endif /* HAVE_WINDOW_SYSTEM */
26452
26453 /* Clear mouse face if X/Y not over text. */
26454 if (glyph == NULL
26455 || area != TEXT_AREA
26456 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26457 /* Glyph's OBJECT is an integer for glyphs inserted by the
26458 display engine for its internal purposes, like truncation
26459 and continuation glyphs and blanks beyond the end of
26460 line's text on text terminals. If we are over such a
26461 glyph, we are not over any text. */
26462 || INTEGERP (glyph->object)
26463 /* R2L rows have a stretch glyph at their front, which
26464 stands for no text, whereas L2R rows have no glyphs at
26465 all beyond the end of text. Treat such stretch glyphs
26466 like we do with NULL glyphs in L2R rows. */
26467 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26468 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26469 && glyph->type == STRETCH_GLYPH
26470 && glyph->avoid_cursor_p))
26471 {
26472 if (clear_mouse_face (hlinfo))
26473 cursor = No_Cursor;
26474 #ifdef HAVE_WINDOW_SYSTEM
26475 if (FRAME_WINDOW_P (f) && NILP (pointer))
26476 {
26477 if (area != TEXT_AREA)
26478 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26479 else
26480 pointer = Vvoid_text_area_pointer;
26481 }
26482 #endif
26483 goto set_cursor;
26484 }
26485
26486 pos = glyph->charpos;
26487 object = glyph->object;
26488 if (!STRINGP (object) && !BUFFERP (object))
26489 goto set_cursor;
26490
26491 /* If we get an out-of-range value, return now; avoid an error. */
26492 if (BUFFERP (object) && pos > BUF_Z (b))
26493 goto set_cursor;
26494
26495 /* Make the window's buffer temporarily current for
26496 overlays_at and compute_char_face. */
26497 obuf = current_buffer;
26498 current_buffer = b;
26499 obegv = BEGV;
26500 ozv = ZV;
26501 BEGV = BEG;
26502 ZV = Z;
26503
26504 /* Is this char mouse-active or does it have help-echo? */
26505 position = make_number (pos);
26506
26507 if (BUFFERP (object))
26508 {
26509 /* Put all the overlays we want in a vector in overlay_vec. */
26510 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26511 /* Sort overlays into increasing priority order. */
26512 noverlays = sort_overlays (overlay_vec, noverlays, w);
26513 }
26514 else
26515 noverlays = 0;
26516
26517 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26518
26519 if (same_region)
26520 cursor = No_Cursor;
26521
26522 /* Check mouse-face highlighting. */
26523 if (! same_region
26524 /* If there exists an overlay with mouse-face overlapping
26525 the one we are currently highlighting, we have to
26526 check if we enter the overlapping overlay, and then
26527 highlight only that. */
26528 || (OVERLAYP (hlinfo->mouse_face_overlay)
26529 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26530 {
26531 /* Find the highest priority overlay with a mouse-face. */
26532 Lisp_Object overlay = Qnil;
26533 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26534 {
26535 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26536 if (!NILP (mouse_face))
26537 overlay = overlay_vec[i];
26538 }
26539
26540 /* If we're highlighting the same overlay as before, there's
26541 no need to do that again. */
26542 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26543 goto check_help_echo;
26544 hlinfo->mouse_face_overlay = overlay;
26545
26546 /* Clear the display of the old active region, if any. */
26547 if (clear_mouse_face (hlinfo))
26548 cursor = No_Cursor;
26549
26550 /* If no overlay applies, get a text property. */
26551 if (NILP (overlay))
26552 mouse_face = Fget_text_property (position, Qmouse_face, object);
26553
26554 /* Next, compute the bounds of the mouse highlighting and
26555 display it. */
26556 if (!NILP (mouse_face) && STRINGP (object))
26557 {
26558 /* The mouse-highlighting comes from a display string
26559 with a mouse-face. */
26560 Lisp_Object s, e;
26561 EMACS_INT ignore;
26562
26563 s = Fprevious_single_property_change
26564 (make_number (pos + 1), Qmouse_face, object, Qnil);
26565 e = Fnext_single_property_change
26566 (position, Qmouse_face, object, Qnil);
26567 if (NILP (s))
26568 s = make_number (0);
26569 if (NILP (e))
26570 e = make_number (SCHARS (object) - 1);
26571 mouse_face_from_string_pos (w, hlinfo, object,
26572 XINT (s), XINT (e));
26573 hlinfo->mouse_face_past_end = 0;
26574 hlinfo->mouse_face_window = window;
26575 hlinfo->mouse_face_face_id
26576 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26577 glyph->face_id, 1);
26578 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26579 cursor = No_Cursor;
26580 }
26581 else
26582 {
26583 /* The mouse-highlighting, if any, comes from an overlay
26584 or text property in the buffer. */
26585 Lisp_Object buffer IF_LINT (= Qnil);
26586 Lisp_Object cover_string IF_LINT (= Qnil);
26587
26588 if (STRINGP (object))
26589 {
26590 /* If we are on a display string with no mouse-face,
26591 check if the text under it has one. */
26592 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26593 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26594 pos = string_buffer_position (object, start);
26595 if (pos > 0)
26596 {
26597 mouse_face = get_char_property_and_overlay
26598 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26599 buffer = w->buffer;
26600 cover_string = object;
26601 }
26602 }
26603 else
26604 {
26605 buffer = object;
26606 cover_string = Qnil;
26607 }
26608
26609 if (!NILP (mouse_face))
26610 {
26611 Lisp_Object before, after;
26612 Lisp_Object before_string, after_string;
26613 /* To correctly find the limits of mouse highlight
26614 in a bidi-reordered buffer, we must not use the
26615 optimization of limiting the search in
26616 previous-single-property-change and
26617 next-single-property-change, because
26618 rows_from_pos_range needs the real start and end
26619 positions to DTRT in this case. That's because
26620 the first row visible in a window does not
26621 necessarily display the character whose position
26622 is the smallest. */
26623 Lisp_Object lim1 =
26624 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26625 ? Fmarker_position (w->start)
26626 : Qnil;
26627 Lisp_Object lim2 =
26628 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26629 ? make_number (BUF_Z (XBUFFER (buffer))
26630 - XFASTINT (w->window_end_pos))
26631 : Qnil;
26632
26633 if (NILP (overlay))
26634 {
26635 /* Handle the text property case. */
26636 before = Fprevious_single_property_change
26637 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26638 after = Fnext_single_property_change
26639 (make_number (pos), Qmouse_face, buffer, lim2);
26640 before_string = after_string = Qnil;
26641 }
26642 else
26643 {
26644 /* Handle the overlay case. */
26645 before = Foverlay_start (overlay);
26646 after = Foverlay_end (overlay);
26647 before_string = Foverlay_get (overlay, Qbefore_string);
26648 after_string = Foverlay_get (overlay, Qafter_string);
26649
26650 if (!STRINGP (before_string)) before_string = Qnil;
26651 if (!STRINGP (after_string)) after_string = Qnil;
26652 }
26653
26654 mouse_face_from_buffer_pos (window, hlinfo, pos,
26655 XFASTINT (before),
26656 XFASTINT (after),
26657 before_string, after_string,
26658 cover_string);
26659 cursor = No_Cursor;
26660 }
26661 }
26662 }
26663
26664 check_help_echo:
26665
26666 /* Look for a `help-echo' property. */
26667 if (NILP (help_echo_string)) {
26668 Lisp_Object help, overlay;
26669
26670 /* Check overlays first. */
26671 help = overlay = Qnil;
26672 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26673 {
26674 overlay = overlay_vec[i];
26675 help = Foverlay_get (overlay, Qhelp_echo);
26676 }
26677
26678 if (!NILP (help))
26679 {
26680 help_echo_string = help;
26681 help_echo_window = window;
26682 help_echo_object = overlay;
26683 help_echo_pos = pos;
26684 }
26685 else
26686 {
26687 Lisp_Object obj = glyph->object;
26688 EMACS_INT charpos = glyph->charpos;
26689
26690 /* Try text properties. */
26691 if (STRINGP (obj)
26692 && charpos >= 0
26693 && charpos < SCHARS (obj))
26694 {
26695 help = Fget_text_property (make_number (charpos),
26696 Qhelp_echo, obj);
26697 if (NILP (help))
26698 {
26699 /* If the string itself doesn't specify a help-echo,
26700 see if the buffer text ``under'' it does. */
26701 struct glyph_row *r
26702 = MATRIX_ROW (w->current_matrix, vpos);
26703 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26704 EMACS_INT p = string_buffer_position (obj, start);
26705 if (p > 0)
26706 {
26707 help = Fget_char_property (make_number (p),
26708 Qhelp_echo, w->buffer);
26709 if (!NILP (help))
26710 {
26711 charpos = p;
26712 obj = w->buffer;
26713 }
26714 }
26715 }
26716 }
26717 else if (BUFFERP (obj)
26718 && charpos >= BEGV
26719 && charpos < ZV)
26720 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26721 obj);
26722
26723 if (!NILP (help))
26724 {
26725 help_echo_string = help;
26726 help_echo_window = window;
26727 help_echo_object = obj;
26728 help_echo_pos = charpos;
26729 }
26730 }
26731 }
26732
26733 #ifdef HAVE_WINDOW_SYSTEM
26734 /* Look for a `pointer' property. */
26735 if (FRAME_WINDOW_P (f) && NILP (pointer))
26736 {
26737 /* Check overlays first. */
26738 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26739 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26740
26741 if (NILP (pointer))
26742 {
26743 Lisp_Object obj = glyph->object;
26744 EMACS_INT charpos = glyph->charpos;
26745
26746 /* Try text properties. */
26747 if (STRINGP (obj)
26748 && charpos >= 0
26749 && charpos < SCHARS (obj))
26750 {
26751 pointer = Fget_text_property (make_number (charpos),
26752 Qpointer, obj);
26753 if (NILP (pointer))
26754 {
26755 /* If the string itself doesn't specify a pointer,
26756 see if the buffer text ``under'' it does. */
26757 struct glyph_row *r
26758 = MATRIX_ROW (w->current_matrix, vpos);
26759 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26760 EMACS_INT p = string_buffer_position (obj, start);
26761 if (p > 0)
26762 pointer = Fget_char_property (make_number (p),
26763 Qpointer, w->buffer);
26764 }
26765 }
26766 else if (BUFFERP (obj)
26767 && charpos >= BEGV
26768 && charpos < ZV)
26769 pointer = Fget_text_property (make_number (charpos),
26770 Qpointer, obj);
26771 }
26772 }
26773 #endif /* HAVE_WINDOW_SYSTEM */
26774
26775 BEGV = obegv;
26776 ZV = ozv;
26777 current_buffer = obuf;
26778 }
26779
26780 set_cursor:
26781
26782 #ifdef HAVE_WINDOW_SYSTEM
26783 if (FRAME_WINDOW_P (f))
26784 define_frame_cursor1 (f, cursor, pointer);
26785 #else
26786 /* This is here to prevent a compiler error, about "label at end of
26787 compound statement". */
26788 return;
26789 #endif
26790 }
26791
26792
26793 /* EXPORT for RIF:
26794 Clear any mouse-face on window W. This function is part of the
26795 redisplay interface, and is called from try_window_id and similar
26796 functions to ensure the mouse-highlight is off. */
26797
26798 void
26799 x_clear_window_mouse_face (struct window *w)
26800 {
26801 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26802 Lisp_Object window;
26803
26804 BLOCK_INPUT;
26805 XSETWINDOW (window, w);
26806 if (EQ (window, hlinfo->mouse_face_window))
26807 clear_mouse_face (hlinfo);
26808 UNBLOCK_INPUT;
26809 }
26810
26811
26812 /* EXPORT:
26813 Just discard the mouse face information for frame F, if any.
26814 This is used when the size of F is changed. */
26815
26816 void
26817 cancel_mouse_face (struct frame *f)
26818 {
26819 Lisp_Object window;
26820 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26821
26822 window = hlinfo->mouse_face_window;
26823 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26824 {
26825 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26826 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26827 hlinfo->mouse_face_window = Qnil;
26828 }
26829 }
26830
26831
26832 \f
26833 /***********************************************************************
26834 Exposure Events
26835 ***********************************************************************/
26836
26837 #ifdef HAVE_WINDOW_SYSTEM
26838
26839 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26840 which intersects rectangle R. R is in window-relative coordinates. */
26841
26842 static void
26843 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26844 enum glyph_row_area area)
26845 {
26846 struct glyph *first = row->glyphs[area];
26847 struct glyph *end = row->glyphs[area] + row->used[area];
26848 struct glyph *last;
26849 int first_x, start_x, x;
26850
26851 if (area == TEXT_AREA && row->fill_line_p)
26852 /* If row extends face to end of line write the whole line. */
26853 draw_glyphs (w, 0, row, area,
26854 0, row->used[area],
26855 DRAW_NORMAL_TEXT, 0);
26856 else
26857 {
26858 /* Set START_X to the window-relative start position for drawing glyphs of
26859 AREA. The first glyph of the text area can be partially visible.
26860 The first glyphs of other areas cannot. */
26861 start_x = window_box_left_offset (w, area);
26862 x = start_x;
26863 if (area == TEXT_AREA)
26864 x += row->x;
26865
26866 /* Find the first glyph that must be redrawn. */
26867 while (first < end
26868 && x + first->pixel_width < r->x)
26869 {
26870 x += first->pixel_width;
26871 ++first;
26872 }
26873
26874 /* Find the last one. */
26875 last = first;
26876 first_x = x;
26877 while (last < end
26878 && x < r->x + r->width)
26879 {
26880 x += last->pixel_width;
26881 ++last;
26882 }
26883
26884 /* Repaint. */
26885 if (last > first)
26886 draw_glyphs (w, first_x - start_x, row, area,
26887 first - row->glyphs[area], last - row->glyphs[area],
26888 DRAW_NORMAL_TEXT, 0);
26889 }
26890 }
26891
26892
26893 /* Redraw the parts of the glyph row ROW on window W intersecting
26894 rectangle R. R is in window-relative coordinates. Value is
26895 non-zero if mouse-face was overwritten. */
26896
26897 static int
26898 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26899 {
26900 xassert (row->enabled_p);
26901
26902 if (row->mode_line_p || w->pseudo_window_p)
26903 draw_glyphs (w, 0, row, TEXT_AREA,
26904 0, row->used[TEXT_AREA],
26905 DRAW_NORMAL_TEXT, 0);
26906 else
26907 {
26908 if (row->used[LEFT_MARGIN_AREA])
26909 expose_area (w, row, r, LEFT_MARGIN_AREA);
26910 if (row->used[TEXT_AREA])
26911 expose_area (w, row, r, TEXT_AREA);
26912 if (row->used[RIGHT_MARGIN_AREA])
26913 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26914 draw_row_fringe_bitmaps (w, row);
26915 }
26916
26917 return row->mouse_face_p;
26918 }
26919
26920
26921 /* Redraw those parts of glyphs rows during expose event handling that
26922 overlap other rows. Redrawing of an exposed line writes over parts
26923 of lines overlapping that exposed line; this function fixes that.
26924
26925 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26926 row in W's current matrix that is exposed and overlaps other rows.
26927 LAST_OVERLAPPING_ROW is the last such row. */
26928
26929 static void
26930 expose_overlaps (struct window *w,
26931 struct glyph_row *first_overlapping_row,
26932 struct glyph_row *last_overlapping_row,
26933 XRectangle *r)
26934 {
26935 struct glyph_row *row;
26936
26937 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26938 if (row->overlapping_p)
26939 {
26940 xassert (row->enabled_p && !row->mode_line_p);
26941
26942 row->clip = r;
26943 if (row->used[LEFT_MARGIN_AREA])
26944 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26945
26946 if (row->used[TEXT_AREA])
26947 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26948
26949 if (row->used[RIGHT_MARGIN_AREA])
26950 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26951 row->clip = NULL;
26952 }
26953 }
26954
26955
26956 /* Return non-zero if W's cursor intersects rectangle R. */
26957
26958 static int
26959 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26960 {
26961 XRectangle cr, result;
26962 struct glyph *cursor_glyph;
26963 struct glyph_row *row;
26964
26965 if (w->phys_cursor.vpos >= 0
26966 && w->phys_cursor.vpos < w->current_matrix->nrows
26967 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26968 row->enabled_p)
26969 && row->cursor_in_fringe_p)
26970 {
26971 /* Cursor is in the fringe. */
26972 cr.x = window_box_right_offset (w,
26973 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26974 ? RIGHT_MARGIN_AREA
26975 : TEXT_AREA));
26976 cr.y = row->y;
26977 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26978 cr.height = row->height;
26979 return x_intersect_rectangles (&cr, r, &result);
26980 }
26981
26982 cursor_glyph = get_phys_cursor_glyph (w);
26983 if (cursor_glyph)
26984 {
26985 /* r is relative to W's box, but w->phys_cursor.x is relative
26986 to left edge of W's TEXT area. Adjust it. */
26987 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26988 cr.y = w->phys_cursor.y;
26989 cr.width = cursor_glyph->pixel_width;
26990 cr.height = w->phys_cursor_height;
26991 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26992 I assume the effect is the same -- and this is portable. */
26993 return x_intersect_rectangles (&cr, r, &result);
26994 }
26995 /* If we don't understand the format, pretend we're not in the hot-spot. */
26996 return 0;
26997 }
26998
26999
27000 /* EXPORT:
27001 Draw a vertical window border to the right of window W if W doesn't
27002 have vertical scroll bars. */
27003
27004 void
27005 x_draw_vertical_border (struct window *w)
27006 {
27007 struct frame *f = XFRAME (WINDOW_FRAME (w));
27008
27009 /* We could do better, if we knew what type of scroll-bar the adjacent
27010 windows (on either side) have... But we don't :-(
27011 However, I think this works ok. ++KFS 2003-04-25 */
27012
27013 /* Redraw borders between horizontally adjacent windows. Don't
27014 do it for frames with vertical scroll bars because either the
27015 right scroll bar of a window, or the left scroll bar of its
27016 neighbor will suffice as a border. */
27017 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27018 return;
27019
27020 if (!WINDOW_RIGHTMOST_P (w)
27021 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27022 {
27023 int x0, x1, y0, y1;
27024
27025 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27026 y1 -= 1;
27027
27028 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27029 x1 -= 1;
27030
27031 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27032 }
27033 else if (!WINDOW_LEFTMOST_P (w)
27034 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27035 {
27036 int x0, x1, y0, y1;
27037
27038 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27039 y1 -= 1;
27040
27041 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27042 x0 -= 1;
27043
27044 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27045 }
27046 }
27047
27048
27049 /* Redraw the part of window W intersection rectangle FR. Pixel
27050 coordinates in FR are frame-relative. Call this function with
27051 input blocked. Value is non-zero if the exposure overwrites
27052 mouse-face. */
27053
27054 static int
27055 expose_window (struct window *w, XRectangle *fr)
27056 {
27057 struct frame *f = XFRAME (w->frame);
27058 XRectangle wr, r;
27059 int mouse_face_overwritten_p = 0;
27060
27061 /* If window is not yet fully initialized, do nothing. This can
27062 happen when toolkit scroll bars are used and a window is split.
27063 Reconfiguring the scroll bar will generate an expose for a newly
27064 created window. */
27065 if (w->current_matrix == NULL)
27066 return 0;
27067
27068 /* When we're currently updating the window, display and current
27069 matrix usually don't agree. Arrange for a thorough display
27070 later. */
27071 if (w == updated_window)
27072 {
27073 SET_FRAME_GARBAGED (f);
27074 return 0;
27075 }
27076
27077 /* Frame-relative pixel rectangle of W. */
27078 wr.x = WINDOW_LEFT_EDGE_X (w);
27079 wr.y = WINDOW_TOP_EDGE_Y (w);
27080 wr.width = WINDOW_TOTAL_WIDTH (w);
27081 wr.height = WINDOW_TOTAL_HEIGHT (w);
27082
27083 if (x_intersect_rectangles (fr, &wr, &r))
27084 {
27085 int yb = window_text_bottom_y (w);
27086 struct glyph_row *row;
27087 int cursor_cleared_p;
27088 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27089
27090 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27091 r.x, r.y, r.width, r.height));
27092
27093 /* Convert to window coordinates. */
27094 r.x -= WINDOW_LEFT_EDGE_X (w);
27095 r.y -= WINDOW_TOP_EDGE_Y (w);
27096
27097 /* Turn off the cursor. */
27098 if (!w->pseudo_window_p
27099 && phys_cursor_in_rect_p (w, &r))
27100 {
27101 x_clear_cursor (w);
27102 cursor_cleared_p = 1;
27103 }
27104 else
27105 cursor_cleared_p = 0;
27106
27107 /* Update lines intersecting rectangle R. */
27108 first_overlapping_row = last_overlapping_row = NULL;
27109 for (row = w->current_matrix->rows;
27110 row->enabled_p;
27111 ++row)
27112 {
27113 int y0 = row->y;
27114 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27115
27116 if ((y0 >= r.y && y0 < r.y + r.height)
27117 || (y1 > r.y && y1 < r.y + r.height)
27118 || (r.y >= y0 && r.y < y1)
27119 || (r.y + r.height > y0 && r.y + r.height < y1))
27120 {
27121 /* A header line may be overlapping, but there is no need
27122 to fix overlapping areas for them. KFS 2005-02-12 */
27123 if (row->overlapping_p && !row->mode_line_p)
27124 {
27125 if (first_overlapping_row == NULL)
27126 first_overlapping_row = row;
27127 last_overlapping_row = row;
27128 }
27129
27130 row->clip = fr;
27131 if (expose_line (w, row, &r))
27132 mouse_face_overwritten_p = 1;
27133 row->clip = NULL;
27134 }
27135 else if (row->overlapping_p)
27136 {
27137 /* We must redraw a row overlapping the exposed area. */
27138 if (y0 < r.y
27139 ? y0 + row->phys_height > r.y
27140 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27141 {
27142 if (first_overlapping_row == NULL)
27143 first_overlapping_row = row;
27144 last_overlapping_row = row;
27145 }
27146 }
27147
27148 if (y1 >= yb)
27149 break;
27150 }
27151
27152 /* Display the mode line if there is one. */
27153 if (WINDOW_WANTS_MODELINE_P (w)
27154 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27155 row->enabled_p)
27156 && row->y < r.y + r.height)
27157 {
27158 if (expose_line (w, row, &r))
27159 mouse_face_overwritten_p = 1;
27160 }
27161
27162 if (!w->pseudo_window_p)
27163 {
27164 /* Fix the display of overlapping rows. */
27165 if (first_overlapping_row)
27166 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27167 fr);
27168
27169 /* Draw border between windows. */
27170 x_draw_vertical_border (w);
27171
27172 /* Turn the cursor on again. */
27173 if (cursor_cleared_p)
27174 update_window_cursor (w, 1);
27175 }
27176 }
27177
27178 return mouse_face_overwritten_p;
27179 }
27180
27181
27182
27183 /* Redraw (parts) of all windows in the window tree rooted at W that
27184 intersect R. R contains frame pixel coordinates. Value is
27185 non-zero if the exposure overwrites mouse-face. */
27186
27187 static int
27188 expose_window_tree (struct window *w, XRectangle *r)
27189 {
27190 struct frame *f = XFRAME (w->frame);
27191 int mouse_face_overwritten_p = 0;
27192
27193 while (w && !FRAME_GARBAGED_P (f))
27194 {
27195 if (!NILP (w->hchild))
27196 mouse_face_overwritten_p
27197 |= expose_window_tree (XWINDOW (w->hchild), r);
27198 else if (!NILP (w->vchild))
27199 mouse_face_overwritten_p
27200 |= expose_window_tree (XWINDOW (w->vchild), r);
27201 else
27202 mouse_face_overwritten_p |= expose_window (w, r);
27203
27204 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27205 }
27206
27207 return mouse_face_overwritten_p;
27208 }
27209
27210
27211 /* EXPORT:
27212 Redisplay an exposed area of frame F. X and Y are the upper-left
27213 corner of the exposed rectangle. W and H are width and height of
27214 the exposed area. All are pixel values. W or H zero means redraw
27215 the entire frame. */
27216
27217 void
27218 expose_frame (struct frame *f, int x, int y, int w, int h)
27219 {
27220 XRectangle r;
27221 int mouse_face_overwritten_p = 0;
27222
27223 TRACE ((stderr, "expose_frame "));
27224
27225 /* No need to redraw if frame will be redrawn soon. */
27226 if (FRAME_GARBAGED_P (f))
27227 {
27228 TRACE ((stderr, " garbaged\n"));
27229 return;
27230 }
27231
27232 /* If basic faces haven't been realized yet, there is no point in
27233 trying to redraw anything. This can happen when we get an expose
27234 event while Emacs is starting, e.g. by moving another window. */
27235 if (FRAME_FACE_CACHE (f) == NULL
27236 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27237 {
27238 TRACE ((stderr, " no faces\n"));
27239 return;
27240 }
27241
27242 if (w == 0 || h == 0)
27243 {
27244 r.x = r.y = 0;
27245 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27246 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27247 }
27248 else
27249 {
27250 r.x = x;
27251 r.y = y;
27252 r.width = w;
27253 r.height = h;
27254 }
27255
27256 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27257 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27258
27259 if (WINDOWP (f->tool_bar_window))
27260 mouse_face_overwritten_p
27261 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27262
27263 #ifdef HAVE_X_WINDOWS
27264 #ifndef MSDOS
27265 #ifndef USE_X_TOOLKIT
27266 if (WINDOWP (f->menu_bar_window))
27267 mouse_face_overwritten_p
27268 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27269 #endif /* not USE_X_TOOLKIT */
27270 #endif
27271 #endif
27272
27273 /* Some window managers support a focus-follows-mouse style with
27274 delayed raising of frames. Imagine a partially obscured frame,
27275 and moving the mouse into partially obscured mouse-face on that
27276 frame. The visible part of the mouse-face will be highlighted,
27277 then the WM raises the obscured frame. With at least one WM, KDE
27278 2.1, Emacs is not getting any event for the raising of the frame
27279 (even tried with SubstructureRedirectMask), only Expose events.
27280 These expose events will draw text normally, i.e. not
27281 highlighted. Which means we must redo the highlight here.
27282 Subsume it under ``we love X''. --gerd 2001-08-15 */
27283 /* Included in Windows version because Windows most likely does not
27284 do the right thing if any third party tool offers
27285 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27286 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27287 {
27288 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27289 if (f == hlinfo->mouse_face_mouse_frame)
27290 {
27291 int mouse_x = hlinfo->mouse_face_mouse_x;
27292 int mouse_y = hlinfo->mouse_face_mouse_y;
27293 clear_mouse_face (hlinfo);
27294 note_mouse_highlight (f, mouse_x, mouse_y);
27295 }
27296 }
27297 }
27298
27299
27300 /* EXPORT:
27301 Determine the intersection of two rectangles R1 and R2. Return
27302 the intersection in *RESULT. Value is non-zero if RESULT is not
27303 empty. */
27304
27305 int
27306 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
27307 {
27308 XRectangle *left, *right;
27309 XRectangle *upper, *lower;
27310 int intersection_p = 0;
27311
27312 /* Rearrange so that R1 is the left-most rectangle. */
27313 if (r1->x < r2->x)
27314 left = r1, right = r2;
27315 else
27316 left = r2, right = r1;
27317
27318 /* X0 of the intersection is right.x0, if this is inside R1,
27319 otherwise there is no intersection. */
27320 if (right->x <= left->x + left->width)
27321 {
27322 result->x = right->x;
27323
27324 /* The right end of the intersection is the minimum of
27325 the right ends of left and right. */
27326 result->width = (min (left->x + left->width, right->x + right->width)
27327 - result->x);
27328
27329 /* Same game for Y. */
27330 if (r1->y < r2->y)
27331 upper = r1, lower = r2;
27332 else
27333 upper = r2, lower = r1;
27334
27335 /* The upper end of the intersection is lower.y0, if this is inside
27336 of upper. Otherwise, there is no intersection. */
27337 if (lower->y <= upper->y + upper->height)
27338 {
27339 result->y = lower->y;
27340
27341 /* The lower end of the intersection is the minimum of the lower
27342 ends of upper and lower. */
27343 result->height = (min (lower->y + lower->height,
27344 upper->y + upper->height)
27345 - result->y);
27346 intersection_p = 1;
27347 }
27348 }
27349
27350 return intersection_p;
27351 }
27352
27353 #endif /* HAVE_WINDOW_SYSTEM */
27354
27355 \f
27356 /***********************************************************************
27357 Initialization
27358 ***********************************************************************/
27359
27360 void
27361 syms_of_xdisp (void)
27362 {
27363 Vwith_echo_area_save_vector = Qnil;
27364 staticpro (&Vwith_echo_area_save_vector);
27365
27366 Vmessage_stack = Qnil;
27367 staticpro (&Vmessage_stack);
27368
27369 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
27370
27371 message_dolog_marker1 = Fmake_marker ();
27372 staticpro (&message_dolog_marker1);
27373 message_dolog_marker2 = Fmake_marker ();
27374 staticpro (&message_dolog_marker2);
27375 message_dolog_marker3 = Fmake_marker ();
27376 staticpro (&message_dolog_marker3);
27377
27378 #if GLYPH_DEBUG
27379 defsubr (&Sdump_frame_glyph_matrix);
27380 defsubr (&Sdump_glyph_matrix);
27381 defsubr (&Sdump_glyph_row);
27382 defsubr (&Sdump_tool_bar_row);
27383 defsubr (&Strace_redisplay);
27384 defsubr (&Strace_to_stderr);
27385 #endif
27386 #ifdef HAVE_WINDOW_SYSTEM
27387 defsubr (&Stool_bar_lines_needed);
27388 defsubr (&Slookup_image_map);
27389 #endif
27390 defsubr (&Sformat_mode_line);
27391 defsubr (&Sinvisible_p);
27392 defsubr (&Scurrent_bidi_paragraph_direction);
27393
27394 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
27395 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
27396 DEFSYM (Qoverriding_local_map, "overriding-local-map");
27397 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
27398 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
27399 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
27400 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
27401 DEFSYM (Qeval, "eval");
27402 DEFSYM (QCdata, ":data");
27403 DEFSYM (Qdisplay, "display");
27404 DEFSYM (Qspace_width, "space-width");
27405 DEFSYM (Qraise, "raise");
27406 DEFSYM (Qslice, "slice");
27407 DEFSYM (Qspace, "space");
27408 DEFSYM (Qmargin, "margin");
27409 DEFSYM (Qpointer, "pointer");
27410 DEFSYM (Qleft_margin, "left-margin");
27411 DEFSYM (Qright_margin, "right-margin");
27412 DEFSYM (Qcenter, "center");
27413 DEFSYM (Qline_height, "line-height");
27414 DEFSYM (QCalign_to, ":align-to");
27415 DEFSYM (QCrelative_width, ":relative-width");
27416 DEFSYM (QCrelative_height, ":relative-height");
27417 DEFSYM (QCeval, ":eval");
27418 DEFSYM (QCpropertize, ":propertize");
27419 DEFSYM (QCfile, ":file");
27420 DEFSYM (Qfontified, "fontified");
27421 DEFSYM (Qfontification_functions, "fontification-functions");
27422 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
27423 DEFSYM (Qescape_glyph, "escape-glyph");
27424 DEFSYM (Qnobreak_space, "nobreak-space");
27425 DEFSYM (Qimage, "image");
27426 DEFSYM (Qtext, "text");
27427 DEFSYM (Qboth, "both");
27428 DEFSYM (Qboth_horiz, "both-horiz");
27429 DEFSYM (Qtext_image_horiz, "text-image-horiz");
27430 DEFSYM (QCmap, ":map");
27431 DEFSYM (QCpointer, ":pointer");
27432 DEFSYM (Qrect, "rect");
27433 DEFSYM (Qcircle, "circle");
27434 DEFSYM (Qpoly, "poly");
27435 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
27436 DEFSYM (Qgrow_only, "grow-only");
27437 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
27438 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
27439 DEFSYM (Qposition, "position");
27440 DEFSYM (Qbuffer_position, "buffer-position");
27441 DEFSYM (Qobject, "object");
27442 DEFSYM (Qbar, "bar");
27443 DEFSYM (Qhbar, "hbar");
27444 DEFSYM (Qbox, "box");
27445 DEFSYM (Qhollow, "hollow");
27446 DEFSYM (Qhand, "hand");
27447 DEFSYM (Qarrow, "arrow");
27448 DEFSYM (Qtext, "text");
27449 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
27450
27451 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27452 Fcons (intern_c_string ("void-variable"), Qnil)),
27453 Qnil);
27454 staticpro (&list_of_error);
27455
27456 DEFSYM (Qlast_arrow_position, "last-arrow-position");
27457 DEFSYM (Qlast_arrow_string, "last-arrow-string");
27458 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
27459 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
27460
27461 echo_buffer[0] = echo_buffer[1] = Qnil;
27462 staticpro (&echo_buffer[0]);
27463 staticpro (&echo_buffer[1]);
27464
27465 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27466 staticpro (&echo_area_buffer[0]);
27467 staticpro (&echo_area_buffer[1]);
27468
27469 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27470 staticpro (&Vmessages_buffer_name);
27471
27472 mode_line_proptrans_alist = Qnil;
27473 staticpro (&mode_line_proptrans_alist);
27474 mode_line_string_list = Qnil;
27475 staticpro (&mode_line_string_list);
27476 mode_line_string_face = Qnil;
27477 staticpro (&mode_line_string_face);
27478 mode_line_string_face_prop = Qnil;
27479 staticpro (&mode_line_string_face_prop);
27480 Vmode_line_unwind_vector = Qnil;
27481 staticpro (&Vmode_line_unwind_vector);
27482
27483 help_echo_string = Qnil;
27484 staticpro (&help_echo_string);
27485 help_echo_object = Qnil;
27486 staticpro (&help_echo_object);
27487 help_echo_window = Qnil;
27488 staticpro (&help_echo_window);
27489 previous_help_echo_string = Qnil;
27490 staticpro (&previous_help_echo_string);
27491 help_echo_pos = -1;
27492
27493 DEFSYM (Qright_to_left, "right-to-left");
27494 DEFSYM (Qleft_to_right, "left-to-right");
27495
27496 #ifdef HAVE_WINDOW_SYSTEM
27497 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27498 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27499 For example, if a block cursor is over a tab, it will be drawn as
27500 wide as that tab on the display. */);
27501 x_stretch_cursor_p = 0;
27502 #endif
27503
27504 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27505 doc: /* *Non-nil means highlight trailing whitespace.
27506 The face used for trailing whitespace is `trailing-whitespace'. */);
27507 Vshow_trailing_whitespace = Qnil;
27508
27509 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27510 doc: /* *Control highlighting of nobreak space and soft hyphen.
27511 A value of t means highlight the character itself (for nobreak space,
27512 use face `nobreak-space').
27513 A value of nil means no highlighting.
27514 Other values mean display the escape glyph followed by an ordinary
27515 space or ordinary hyphen. */);
27516 Vnobreak_char_display = Qt;
27517
27518 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27519 doc: /* *The pointer shape to show in void text areas.
27520 A value of nil means to show the text pointer. Other options are `arrow',
27521 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27522 Vvoid_text_area_pointer = Qarrow;
27523
27524 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27525 doc: /* Non-nil means don't actually do any redisplay.
27526 This is used for internal purposes. */);
27527 Vinhibit_redisplay = Qnil;
27528
27529 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27530 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27531 Vglobal_mode_string = Qnil;
27532
27533 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27534 doc: /* Marker for where to display an arrow on top of the buffer text.
27535 This must be the beginning of a line in order to work.
27536 See also `overlay-arrow-string'. */);
27537 Voverlay_arrow_position = Qnil;
27538
27539 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27540 doc: /* String to display as an arrow in non-window frames.
27541 See also `overlay-arrow-position'. */);
27542 Voverlay_arrow_string = make_pure_c_string ("=>");
27543
27544 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27545 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27546 The symbols on this list are examined during redisplay to determine
27547 where to display overlay arrows. */);
27548 Voverlay_arrow_variable_list
27549 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27550
27551 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27552 doc: /* *The number of lines to try scrolling a window by when point moves out.
27553 If that fails to bring point back on frame, point is centered instead.
27554 If this is zero, point is always centered after it moves off frame.
27555 If you want scrolling to always be a line at a time, you should set
27556 `scroll-conservatively' to a large value rather than set this to 1. */);
27557
27558 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27559 doc: /* *Scroll up to this many lines, to bring point back on screen.
27560 If point moves off-screen, redisplay will scroll by up to
27561 `scroll-conservatively' lines in order to bring point just barely
27562 onto the screen again. If that cannot be done, then redisplay
27563 recenters point as usual.
27564
27565 If the value is greater than 100, redisplay will never recenter point,
27566 but will always scroll just enough text to bring point into view, even
27567 if you move far away.
27568
27569 A value of zero means always recenter point if it moves off screen. */);
27570 scroll_conservatively = 0;
27571
27572 DEFVAR_INT ("scroll-margin", scroll_margin,
27573 doc: /* *Number of lines of margin at the top and bottom of a window.
27574 Recenter the window whenever point gets within this many lines
27575 of the top or bottom of the window. */);
27576 scroll_margin = 0;
27577
27578 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27579 doc: /* Pixels per inch value for non-window system displays.
27580 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27581 Vdisplay_pixels_per_inch = make_float (72.0);
27582
27583 #if GLYPH_DEBUG
27584 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27585 #endif
27586
27587 DEFVAR_LISP ("truncate-partial-width-windows",
27588 Vtruncate_partial_width_windows,
27589 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27590 For an integer value, truncate lines in each window narrower than the
27591 full frame width, provided the window width is less than that integer;
27592 otherwise, respect the value of `truncate-lines'.
27593
27594 For any other non-nil value, truncate lines in all windows that do
27595 not span the full frame width.
27596
27597 A value of nil means to respect the value of `truncate-lines'.
27598
27599 If `word-wrap' is enabled, you might want to reduce this. */);
27600 Vtruncate_partial_width_windows = make_number (50);
27601
27602 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27603 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27604 Any other value means to use the appropriate face, `mode-line',
27605 `header-line', or `menu' respectively. */);
27606 mode_line_inverse_video = 1;
27607
27608 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27609 doc: /* *Maximum buffer size for which line number should be displayed.
27610 If the buffer is bigger than this, the line number does not appear
27611 in the mode line. A value of nil means no limit. */);
27612 Vline_number_display_limit = Qnil;
27613
27614 DEFVAR_INT ("line-number-display-limit-width",
27615 line_number_display_limit_width,
27616 doc: /* *Maximum line width (in characters) for line number display.
27617 If the average length of the lines near point is bigger than this, then the
27618 line number may be omitted from the mode line. */);
27619 line_number_display_limit_width = 200;
27620
27621 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27622 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27623 highlight_nonselected_windows = 0;
27624
27625 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27626 doc: /* Non-nil if more than one frame is visible on this display.
27627 Minibuffer-only frames don't count, but iconified frames do.
27628 This variable is not guaranteed to be accurate except while processing
27629 `frame-title-format' and `icon-title-format'. */);
27630
27631 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27632 doc: /* Template for displaying the title bar of visible frames.
27633 \(Assuming the window manager supports this feature.)
27634
27635 This variable has the same structure as `mode-line-format', except that
27636 the %c and %l constructs are ignored. It is used only on frames for
27637 which no explicit name has been set \(see `modify-frame-parameters'). */);
27638
27639 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27640 doc: /* Template for displaying the title bar of an iconified frame.
27641 \(Assuming the window manager supports this feature.)
27642 This variable has the same structure as `mode-line-format' (which see),
27643 and is used only on frames for which no explicit name has been set
27644 \(see `modify-frame-parameters'). */);
27645 Vicon_title_format
27646 = Vframe_title_format
27647 = pure_cons (intern_c_string ("multiple-frames"),
27648 pure_cons (make_pure_c_string ("%b"),
27649 pure_cons (pure_cons (empty_unibyte_string,
27650 pure_cons (intern_c_string ("invocation-name"),
27651 pure_cons (make_pure_c_string ("@"),
27652 pure_cons (intern_c_string ("system-name"),
27653 Qnil)))),
27654 Qnil)));
27655
27656 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27657 doc: /* Maximum number of lines to keep in the message log buffer.
27658 If nil, disable message logging. If t, log messages but don't truncate
27659 the buffer when it becomes large. */);
27660 Vmessage_log_max = make_number (100);
27661
27662 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27663 doc: /* Functions called before redisplay, if window sizes have changed.
27664 The value should be a list of functions that take one argument.
27665 Just before redisplay, for each frame, if any of its windows have changed
27666 size since the last redisplay, or have been split or deleted,
27667 all the functions in the list are called, with the frame as argument. */);
27668 Vwindow_size_change_functions = Qnil;
27669
27670 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27671 doc: /* List of functions to call before redisplaying a window with scrolling.
27672 Each function is called with two arguments, the window and its new
27673 display-start position. Note that these functions are also called by
27674 `set-window-buffer'. Also note that the value of `window-end' is not
27675 valid when these functions are called. */);
27676 Vwindow_scroll_functions = Qnil;
27677
27678 DEFVAR_LISP ("window-text-change-functions",
27679 Vwindow_text_change_functions,
27680 doc: /* Functions to call in redisplay when text in the window might change. */);
27681 Vwindow_text_change_functions = Qnil;
27682
27683 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27684 doc: /* Functions called when redisplay of a window reaches the end trigger.
27685 Each function is called with two arguments, the window and the end trigger value.
27686 See `set-window-redisplay-end-trigger'. */);
27687 Vredisplay_end_trigger_functions = Qnil;
27688
27689 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27690 doc: /* *Non-nil means autoselect window with mouse pointer.
27691 If nil, do not autoselect windows.
27692 A positive number means delay autoselection by that many seconds: a
27693 window is autoselected only after the mouse has remained in that
27694 window for the duration of the delay.
27695 A negative number has a similar effect, but causes windows to be
27696 autoselected only after the mouse has stopped moving. \(Because of
27697 the way Emacs compares mouse events, you will occasionally wait twice
27698 that time before the window gets selected.\)
27699 Any other value means to autoselect window instantaneously when the
27700 mouse pointer enters it.
27701
27702 Autoselection selects the minibuffer only if it is active, and never
27703 unselects the minibuffer if it is active.
27704
27705 When customizing this variable make sure that the actual value of
27706 `focus-follows-mouse' matches the behavior of your window manager. */);
27707 Vmouse_autoselect_window = Qnil;
27708
27709 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27710 doc: /* *Non-nil means automatically resize tool-bars.
27711 This dynamically changes the tool-bar's height to the minimum height
27712 that is needed to make all tool-bar items visible.
27713 If value is `grow-only', the tool-bar's height is only increased
27714 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27715 Vauto_resize_tool_bars = Qt;
27716
27717 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27718 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27719 auto_raise_tool_bar_buttons_p = 1;
27720
27721 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27722 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27723 make_cursor_line_fully_visible_p = 1;
27724
27725 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27726 doc: /* *Border below tool-bar in pixels.
27727 If an integer, use it as the height of the border.
27728 If it is one of `internal-border-width' or `border-width', use the
27729 value of the corresponding frame parameter.
27730 Otherwise, no border is added below the tool-bar. */);
27731 Vtool_bar_border = Qinternal_border_width;
27732
27733 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27734 doc: /* *Margin around tool-bar buttons in pixels.
27735 If an integer, use that for both horizontal and vertical margins.
27736 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27737 HORZ specifying the horizontal margin, and VERT specifying the
27738 vertical margin. */);
27739 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27740
27741 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27742 doc: /* *Relief thickness of tool-bar buttons. */);
27743 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27744
27745 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27746 doc: /* Tool bar style to use.
27747 It can be one of
27748 image - show images only
27749 text - show text only
27750 both - show both, text below image
27751 both-horiz - show text to the right of the image
27752 text-image-horiz - show text to the left of the image
27753 any other - use system default or image if no system default. */);
27754 Vtool_bar_style = Qnil;
27755
27756 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27757 doc: /* *Maximum number of characters a label can have to be shown.
27758 The tool bar style must also show labels for this to have any effect, see
27759 `tool-bar-style'. */);
27760 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27761
27762 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27763 doc: /* List of functions to call to fontify regions of text.
27764 Each function is called with one argument POS. Functions must
27765 fontify a region starting at POS in the current buffer, and give
27766 fontified regions the property `fontified'. */);
27767 Vfontification_functions = Qnil;
27768 Fmake_variable_buffer_local (Qfontification_functions);
27769
27770 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27771 unibyte_display_via_language_environment,
27772 doc: /* *Non-nil means display unibyte text according to language environment.
27773 Specifically, this means that raw bytes in the range 160-255 decimal
27774 are displayed by converting them to the equivalent multibyte characters
27775 according to the current language environment. As a result, they are
27776 displayed according to the current fontset.
27777
27778 Note that this variable affects only how these bytes are displayed,
27779 but does not change the fact they are interpreted as raw bytes. */);
27780 unibyte_display_via_language_environment = 0;
27781
27782 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27783 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
27784 If a float, it specifies a fraction of the mini-window frame's height.
27785 If an integer, it specifies a number of lines. */);
27786 Vmax_mini_window_height = make_float (0.25);
27787
27788 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27789 doc: /* How to resize mini-windows (the minibuffer and the echo area).
27790 A value of nil means don't automatically resize mini-windows.
27791 A value of t means resize them to fit the text displayed in them.
27792 A value of `grow-only', the default, means let mini-windows grow only;
27793 they return to their normal size when the minibuffer is closed, or the
27794 echo area becomes empty. */);
27795 Vresize_mini_windows = Qgrow_only;
27796
27797 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27798 doc: /* Alist specifying how to blink the cursor off.
27799 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27800 `cursor-type' frame-parameter or variable equals ON-STATE,
27801 comparing using `equal', Emacs uses OFF-STATE to specify
27802 how to blink it off. ON-STATE and OFF-STATE are values for
27803 the `cursor-type' frame parameter.
27804
27805 If a frame's ON-STATE has no entry in this list,
27806 the frame's other specifications determine how to blink the cursor off. */);
27807 Vblink_cursor_alist = Qnil;
27808
27809 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27810 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27811 If non-nil, windows are automatically scrolled horizontally to make
27812 point visible. */);
27813 automatic_hscrolling_p = 1;
27814 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
27815
27816 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27817 doc: /* *How many columns away from the window edge point is allowed to get
27818 before automatic hscrolling will horizontally scroll the window. */);
27819 hscroll_margin = 5;
27820
27821 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27822 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27823 When point is less than `hscroll-margin' columns from the window
27824 edge, automatic hscrolling will scroll the window by the amount of columns
27825 determined by this variable. If its value is a positive integer, scroll that
27826 many columns. If it's a positive floating-point number, it specifies the
27827 fraction of the window's width to scroll. If it's nil or zero, point will be
27828 centered horizontally after the scroll. Any other value, including negative
27829 numbers, are treated as if the value were zero.
27830
27831 Automatic hscrolling always moves point outside the scroll margin, so if
27832 point was more than scroll step columns inside the margin, the window will
27833 scroll more than the value given by the scroll step.
27834
27835 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27836 and `scroll-right' overrides this variable's effect. */);
27837 Vhscroll_step = make_number (0);
27838
27839 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27840 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27841 Bind this around calls to `message' to let it take effect. */);
27842 message_truncate_lines = 0;
27843
27844 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27845 doc: /* Normal hook run to update the menu bar definitions.
27846 Redisplay runs this hook before it redisplays the menu bar.
27847 This is used to update submenus such as Buffers,
27848 whose contents depend on various data. */);
27849 Vmenu_bar_update_hook = Qnil;
27850
27851 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27852 doc: /* Frame for which we are updating a menu.
27853 The enable predicate for a menu binding should check this variable. */);
27854 Vmenu_updating_frame = Qnil;
27855
27856 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27857 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27858 inhibit_menubar_update = 0;
27859
27860 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27861 doc: /* Prefix prepended to all continuation lines at display time.
27862 The value may be a string, an image, or a stretch-glyph; it is
27863 interpreted in the same way as the value of a `display' text property.
27864
27865 This variable is overridden by any `wrap-prefix' text or overlay
27866 property.
27867
27868 To add a prefix to non-continuation lines, use `line-prefix'. */);
27869 Vwrap_prefix = Qnil;
27870 DEFSYM (Qwrap_prefix, "wrap-prefix");
27871 Fmake_variable_buffer_local (Qwrap_prefix);
27872
27873 DEFVAR_LISP ("line-prefix", Vline_prefix,
27874 doc: /* Prefix prepended to all non-continuation lines at display time.
27875 The value may be a string, an image, or a stretch-glyph; it is
27876 interpreted in the same way as the value of a `display' text property.
27877
27878 This variable is overridden by any `line-prefix' text or overlay
27879 property.
27880
27881 To add a prefix to continuation lines, use `wrap-prefix'. */);
27882 Vline_prefix = Qnil;
27883 DEFSYM (Qline_prefix, "line-prefix");
27884 Fmake_variable_buffer_local (Qline_prefix);
27885
27886 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27887 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27888 inhibit_eval_during_redisplay = 0;
27889
27890 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27891 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27892 inhibit_free_realized_faces = 0;
27893
27894 #if GLYPH_DEBUG
27895 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27896 doc: /* Inhibit try_window_id display optimization. */);
27897 inhibit_try_window_id = 0;
27898
27899 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27900 doc: /* Inhibit try_window_reusing display optimization. */);
27901 inhibit_try_window_reusing = 0;
27902
27903 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27904 doc: /* Inhibit try_cursor_movement display optimization. */);
27905 inhibit_try_cursor_movement = 0;
27906 #endif /* GLYPH_DEBUG */
27907
27908 DEFVAR_INT ("overline-margin", overline_margin,
27909 doc: /* *Space between overline and text, in pixels.
27910 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27911 margin to the caracter height. */);
27912 overline_margin = 2;
27913
27914 DEFVAR_INT ("underline-minimum-offset",
27915 underline_minimum_offset,
27916 doc: /* Minimum distance between baseline and underline.
27917 This can improve legibility of underlined text at small font sizes,
27918 particularly when using variable `x-use-underline-position-properties'
27919 with fonts that specify an UNDERLINE_POSITION relatively close to the
27920 baseline. The default value is 1. */);
27921 underline_minimum_offset = 1;
27922
27923 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27924 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27925 This feature only works when on a window system that can change
27926 cursor shapes. */);
27927 display_hourglass_p = 1;
27928
27929 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27930 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27931 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27932
27933 hourglass_atimer = NULL;
27934 hourglass_shown_p = 0;
27935
27936 DEFSYM (Qglyphless_char, "glyphless-char");
27937 DEFSYM (Qhex_code, "hex-code");
27938 DEFSYM (Qempty_box, "empty-box");
27939 DEFSYM (Qthin_space, "thin-space");
27940 DEFSYM (Qzero_width, "zero-width");
27941
27942 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27943 /* Intern this now in case it isn't already done.
27944 Setting this variable twice is harmless.
27945 But don't staticpro it here--that is done in alloc.c. */
27946 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27947 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27948
27949 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27950 doc: /* Char-table defining glyphless characters.
27951 Each element, if non-nil, should be one of the following:
27952 an ASCII acronym string: display this string in a box
27953 `hex-code': display the hexadecimal code of a character in a box
27954 `empty-box': display as an empty box
27955 `thin-space': display as 1-pixel width space
27956 `zero-width': don't display
27957 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27958 display method for graphical terminals and text terminals respectively.
27959 GRAPHICAL and TEXT should each have one of the values listed above.
27960
27961 The char-table has one extra slot to control the display of a character for
27962 which no font is found. This slot only takes effect on graphical terminals.
27963 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27964 `thin-space'. The default is `empty-box'. */);
27965 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27966 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27967 Qempty_box);
27968 }
27969
27970
27971 /* Initialize this module when Emacs starts. */
27972
27973 void
27974 init_xdisp (void)
27975 {
27976 current_header_line_height = current_mode_line_height = -1;
27977
27978 CHARPOS (this_line_start_pos) = 0;
27979
27980 if (!noninteractive)
27981 {
27982 struct window *m = XWINDOW (minibuf_window);
27983 Lisp_Object frame = m->frame;
27984 struct frame *f = XFRAME (frame);
27985 Lisp_Object root = FRAME_ROOT_WINDOW (f);
27986 struct window *r = XWINDOW (root);
27987 int i;
27988
27989 echo_area_window = minibuf_window;
27990
27991 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
27992 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
27993 XSETFASTINT (r->total_cols, FRAME_COLS (f));
27994 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
27995 XSETFASTINT (m->total_lines, 1);
27996 XSETFASTINT (m->total_cols, FRAME_COLS (f));
27997
27998 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27999 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28000 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28001
28002 /* The default ellipsis glyphs `...'. */
28003 for (i = 0; i < 3; ++i)
28004 default_invis_vector[i] = make_number ('.');
28005 }
28006
28007 {
28008 /* Allocate the buffer for frame titles.
28009 Also used for `format-mode-line'. */
28010 int size = 100;
28011 mode_line_noprop_buf = (char *) xmalloc (size);
28012 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28013 mode_line_noprop_ptr = mode_line_noprop_buf;
28014 mode_line_target = MODE_LINE_DISPLAY;
28015 }
28016
28017 help_echo_showing_p = 0;
28018 }
28019
28020 /* Since w32 does not support atimers, it defines its own implementation of
28021 the following three functions in w32fns.c. */
28022 #ifndef WINDOWSNT
28023
28024 /* Platform-independent portion of hourglass implementation. */
28025
28026 /* Return non-zero if houglass timer has been started or hourglass is shown. */
28027 int
28028 hourglass_started (void)
28029 {
28030 return hourglass_shown_p || hourglass_atimer != NULL;
28031 }
28032
28033 /* Cancel a currently active hourglass timer, and start a new one. */
28034 void
28035 start_hourglass (void)
28036 {
28037 #if defined (HAVE_WINDOW_SYSTEM)
28038 EMACS_TIME delay;
28039 int secs, usecs = 0;
28040
28041 cancel_hourglass ();
28042
28043 if (INTEGERP (Vhourglass_delay)
28044 && XINT (Vhourglass_delay) > 0)
28045 secs = XFASTINT (Vhourglass_delay);
28046 else if (FLOATP (Vhourglass_delay)
28047 && XFLOAT_DATA (Vhourglass_delay) > 0)
28048 {
28049 Lisp_Object tem;
28050 tem = Ftruncate (Vhourglass_delay, Qnil);
28051 secs = XFASTINT (tem);
28052 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28053 }
28054 else
28055 secs = DEFAULT_HOURGLASS_DELAY;
28056
28057 EMACS_SET_SECS_USECS (delay, secs, usecs);
28058 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28059 show_hourglass, NULL);
28060 #endif
28061 }
28062
28063
28064 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28065 shown. */
28066 void
28067 cancel_hourglass (void)
28068 {
28069 #if defined (HAVE_WINDOW_SYSTEM)
28070 if (hourglass_atimer)
28071 {
28072 cancel_atimer (hourglass_atimer);
28073 hourglass_atimer = NULL;
28074 }
28075
28076 if (hourglass_shown_p)
28077 hide_hourglass ();
28078 #endif
28079 }
28080 #endif /* ! WINDOWSNT */