Fixed a bug with displaying strings padded with blanks.
[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 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 #if GLYPH_DEBUG
598
599 /* Non-zero means print traces of redisplay if compiled with
600 GLYPH_DEBUG != 0. */
601
602 int trace_redisplay_p;
603
604 #endif /* GLYPH_DEBUG */
605
606 #ifdef DEBUG_TRACE_MOVE
607 /* Non-zero means trace with TRACE_MOVE to stderr. */
608 int trace_move;
609
610 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
611 #else
612 #define TRACE_MOVE(x) (void) 0
613 #endif
614
615 static Lisp_Object Qauto_hscroll_mode;
616
617 /* Buffer being redisplayed -- for redisplay_window_error. */
618
619 static struct buffer *displayed_buffer;
620
621 /* Value returned from text property handlers (see below). */
622
623 enum prop_handled
624 {
625 HANDLED_NORMALLY,
626 HANDLED_RECOMPUTE_PROPS,
627 HANDLED_OVERLAY_STRING_CONSUMED,
628 HANDLED_RETURN
629 };
630
631 /* A description of text properties that redisplay is interested
632 in. */
633
634 struct props
635 {
636 /* The name of the property. */
637 Lisp_Object *name;
638
639 /* A unique index for the property. */
640 enum prop_idx idx;
641
642 /* A handler function called to set up iterator IT from the property
643 at IT's current position. Value is used to steer handle_stop. */
644 enum prop_handled (*handler) (struct it *it);
645 };
646
647 static enum prop_handled handle_face_prop (struct it *);
648 static enum prop_handled handle_invisible_prop (struct it *);
649 static enum prop_handled handle_display_prop (struct it *);
650 static enum prop_handled handle_composition_prop (struct it *);
651 static enum prop_handled handle_overlay_change (struct it *);
652 static enum prop_handled handle_fontified_prop (struct it *);
653
654 /* Properties handled by iterators. */
655
656 static struct props it_props[] =
657 {
658 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
659 /* Handle `face' before `display' because some sub-properties of
660 `display' need to know the face. */
661 {&Qface, FACE_PROP_IDX, handle_face_prop},
662 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
663 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
664 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
665 {NULL, 0, NULL}
666 };
667
668 /* Value is the position described by X. If X is a marker, value is
669 the marker_position of X. Otherwise, value is X. */
670
671 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
672
673 /* Enumeration returned by some move_it_.* functions internally. */
674
675 enum move_it_result
676 {
677 /* Not used. Undefined value. */
678 MOVE_UNDEFINED,
679
680 /* Move ended at the requested buffer position or ZV. */
681 MOVE_POS_MATCH_OR_ZV,
682
683 /* Move ended at the requested X pixel position. */
684 MOVE_X_REACHED,
685
686 /* Move within a line ended at the end of a line that must be
687 continued. */
688 MOVE_LINE_CONTINUED,
689
690 /* Move within a line ended at the end of a line that would
691 be displayed truncated. */
692 MOVE_LINE_TRUNCATED,
693
694 /* Move within a line ended at a line end. */
695 MOVE_NEWLINE_OR_CR
696 };
697
698 /* This counter is used to clear the face cache every once in a while
699 in redisplay_internal. It is incremented for each redisplay.
700 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
701 cleared. */
702
703 #define CLEAR_FACE_CACHE_COUNT 500
704 static int clear_face_cache_count;
705
706 /* Similarly for the image cache. */
707
708 #ifdef HAVE_WINDOW_SYSTEM
709 #define CLEAR_IMAGE_CACHE_COUNT 101
710 static int clear_image_cache_count;
711
712 /* Null glyph slice */
713 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
714 #endif
715
716 /* Non-zero while redisplay_internal is in progress. */
717
718 int redisplaying_p;
719
720 static Lisp_Object Qinhibit_free_realized_faces;
721
722 /* If a string, XTread_socket generates an event to display that string.
723 (The display is done in read_char.) */
724
725 Lisp_Object help_echo_string;
726 Lisp_Object help_echo_window;
727 Lisp_Object help_echo_object;
728 EMACS_INT help_echo_pos;
729
730 /* Temporary variable for XTread_socket. */
731
732 Lisp_Object previous_help_echo_string;
733
734 /* Platform-independent portion of hourglass implementation. */
735
736 /* Non-zero means an hourglass cursor is currently shown. */
737 int hourglass_shown_p;
738
739 /* If non-null, an asynchronous timer that, when it expires, displays
740 an hourglass cursor on all frames. */
741 struct atimer *hourglass_atimer;
742
743 /* Name of the face used to display glyphless characters. */
744 Lisp_Object Qglyphless_char;
745
746 /* Symbol for the purpose of Vglyphless_char_display. */
747 static Lisp_Object Qglyphless_char_display;
748
749 /* Method symbols for Vglyphless_char_display. */
750 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
751
752 /* Default pixel width of `thin-space' display method. */
753 #define THIN_SPACE_WIDTH 1
754
755 /* Default number of seconds to wait before displaying an hourglass
756 cursor. */
757 #define DEFAULT_HOURGLASS_DELAY 1
758
759 \f
760 /* Function prototypes. */
761
762 static void setup_for_ellipsis (struct it *, int);
763 static void set_iterator_to_next (struct it *, int);
764 static void mark_window_display_accurate_1 (struct window *, int);
765 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
766 static int display_prop_string_p (Lisp_Object, Lisp_Object);
767 static int cursor_row_p (struct glyph_row *);
768 static int redisplay_mode_lines (Lisp_Object, int);
769 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
770
771 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
772
773 static void handle_line_prefix (struct it *);
774
775 static void pint2str (char *, int, EMACS_INT);
776 static void pint2hrstr (char *, int, EMACS_INT);
777 static struct text_pos run_window_scroll_functions (Lisp_Object,
778 struct text_pos);
779 static void reconsider_clip_changes (struct window *, struct buffer *);
780 static int text_outside_line_unchanged_p (struct window *,
781 EMACS_INT, EMACS_INT);
782 static void store_mode_line_noprop_char (char);
783 static int store_mode_line_noprop (const char *, int, int);
784 static void handle_stop (struct it *);
785 static void handle_stop_backwards (struct it *, EMACS_INT);
786 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
787 static void ensure_echo_area_buffers (void);
788 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
789 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
790 static int with_echo_area_buffer (struct window *, int,
791 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
792 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
793 static void clear_garbaged_frames (void);
794 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
795 static void pop_message (void);
796 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
797 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
798 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
799 static int display_echo_area (struct window *);
800 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
801 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
802 static Lisp_Object unwind_redisplay (Lisp_Object);
803 static int string_char_and_length (const unsigned char *, int *);
804 static struct text_pos display_prop_end (struct it *, Lisp_Object,
805 struct text_pos);
806 static int compute_window_start_on_continuation_line (struct window *);
807 static Lisp_Object safe_eval_handler (Lisp_Object);
808 static void insert_left_trunc_glyphs (struct it *);
809 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
810 Lisp_Object);
811 static void extend_face_to_end_of_line (struct it *);
812 static int append_space_for_newline (struct it *, int);
813 static int cursor_row_fully_visible_p (struct window *, int, int);
814 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
815 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
816 static int trailing_whitespace_p (EMACS_INT);
817 static unsigned long int message_log_check_duplicate (EMACS_INT, EMACS_INT);
818 static void push_it (struct it *, struct text_pos *);
819 static void pop_it (struct it *);
820 static void sync_frame_with_window_matrix_rows (struct window *);
821 static void select_frame_for_redisplay (Lisp_Object);
822 static void redisplay_internal (void);
823 static int echo_area_display (int);
824 static void redisplay_windows (Lisp_Object);
825 static void redisplay_window (Lisp_Object, int);
826 static Lisp_Object redisplay_window_error (Lisp_Object);
827 static Lisp_Object redisplay_window_0 (Lisp_Object);
828 static Lisp_Object redisplay_window_1 (Lisp_Object);
829 static int set_cursor_from_row (struct window *, struct glyph_row *,
830 struct glyph_matrix *, EMACS_INT, EMACS_INT,
831 int, int);
832 static int update_menu_bar (struct frame *, int, int);
833 static int try_window_reusing_current_matrix (struct window *);
834 static int try_window_id (struct window *);
835 static int display_line (struct it *);
836 static int display_mode_lines (struct window *);
837 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
838 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
839 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
840 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
841 static void display_menu_bar (struct window *);
842 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
843 EMACS_INT *);
844 static int display_string (const char *, Lisp_Object, Lisp_Object,
845 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
846 static void compute_line_metrics (struct it *);
847 static void run_redisplay_end_trigger_hook (struct it *);
848 static int get_overlay_strings (struct it *, EMACS_INT);
849 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
850 static void next_overlay_string (struct it *);
851 static void reseat (struct it *, struct text_pos, int);
852 static void reseat_1 (struct it *, struct text_pos, int);
853 static void back_to_previous_visible_line_start (struct it *);
854 void reseat_at_previous_visible_line_start (struct it *);
855 static void reseat_at_next_visible_line_start (struct it *, int);
856 static int next_element_from_ellipsis (struct it *);
857 static int next_element_from_display_vector (struct it *);
858 static int next_element_from_string (struct it *);
859 static int next_element_from_c_string (struct it *);
860 static int next_element_from_buffer (struct it *);
861 static int next_element_from_composition (struct it *);
862 static int next_element_from_image (struct it *);
863 static int next_element_from_stretch (struct it *);
864 static void load_overlay_strings (struct it *, EMACS_INT);
865 static int init_from_display_pos (struct it *, struct window *,
866 struct display_pos *);
867 static void reseat_to_string (struct it *, const char *,
868 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
869 static int get_next_display_element (struct it *);
870 static enum move_it_result
871 move_it_in_display_line_to (struct it *, EMACS_INT, int,
872 enum move_operation_enum);
873 void move_it_vertically_backward (struct it *, int);
874 static void init_to_row_start (struct it *, struct window *,
875 struct glyph_row *);
876 static int init_to_row_end (struct it *, struct window *,
877 struct glyph_row *);
878 static void back_to_previous_line_start (struct it *);
879 static int forward_to_next_line_start (struct it *, int *);
880 static struct text_pos string_pos_nchars_ahead (struct text_pos,
881 Lisp_Object, EMACS_INT);
882 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
883 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
884 static EMACS_INT number_of_chars (const char *, int);
885 static void compute_stop_pos (struct it *);
886 static void compute_string_pos (struct text_pos *, struct text_pos,
887 Lisp_Object);
888 static int face_before_or_after_it_pos (struct it *, int);
889 static EMACS_INT next_overlay_change (EMACS_INT);
890 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
891 Lisp_Object, struct text_pos *, EMACS_INT, int);
892 static int handle_single_display_spec (struct it *, Lisp_Object,
893 Lisp_Object, Lisp_Object,
894 struct text_pos *, EMACS_INT, int, int);
895 static int underlying_face_id (struct it *);
896 static int in_ellipses_for_invisible_text_p (struct display_pos *,
897 struct window *);
898
899 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
900 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
901
902 #ifdef HAVE_WINDOW_SYSTEM
903
904 static void x_consider_frame_title (Lisp_Object);
905 static int tool_bar_lines_needed (struct frame *, int *);
906 static void update_tool_bar (struct frame *, int);
907 static void build_desired_tool_bar_string (struct frame *f);
908 static int redisplay_tool_bar (struct frame *);
909 static void display_tool_bar_line (struct it *, int);
910 static void notice_overwritten_cursor (struct window *,
911 enum glyph_row_area,
912 int, int, int, int);
913 static void append_stretch_glyph (struct it *, Lisp_Object,
914 int, int, int);
915
916
917 #endif /* HAVE_WINDOW_SYSTEM */
918
919 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
920 static int coords_in_mouse_face_p (struct window *, int, int);
921
922
923 \f
924 /***********************************************************************
925 Window display dimensions
926 ***********************************************************************/
927
928 /* Return the bottom boundary y-position for text lines in window W.
929 This is the first y position at which a line cannot start.
930 It is relative to the top of the window.
931
932 This is the height of W minus the height of a mode line, if any. */
933
934 INLINE int
935 window_text_bottom_y (struct window *w)
936 {
937 int height = WINDOW_TOTAL_HEIGHT (w);
938
939 if (WINDOW_WANTS_MODELINE_P (w))
940 height -= CURRENT_MODE_LINE_HEIGHT (w);
941 return height;
942 }
943
944 /* Return the pixel width of display area AREA of window W. AREA < 0
945 means return the total width of W, not including fringes to
946 the left and right of the window. */
947
948 INLINE int
949 window_box_width (struct window *w, int area)
950 {
951 int cols = XFASTINT (w->total_cols);
952 int pixels = 0;
953
954 if (!w->pseudo_window_p)
955 {
956 cols -= WINDOW_SCROLL_BAR_COLS (w);
957
958 if (area == TEXT_AREA)
959 {
960 if (INTEGERP (w->left_margin_cols))
961 cols -= XFASTINT (w->left_margin_cols);
962 if (INTEGERP (w->right_margin_cols))
963 cols -= XFASTINT (w->right_margin_cols);
964 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
965 }
966 else if (area == LEFT_MARGIN_AREA)
967 {
968 cols = (INTEGERP (w->left_margin_cols)
969 ? XFASTINT (w->left_margin_cols) : 0);
970 pixels = 0;
971 }
972 else if (area == RIGHT_MARGIN_AREA)
973 {
974 cols = (INTEGERP (w->right_margin_cols)
975 ? XFASTINT (w->right_margin_cols) : 0);
976 pixels = 0;
977 }
978 }
979
980 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
981 }
982
983
984 /* Return the pixel height of the display area of window W, not
985 including mode lines of W, if any. */
986
987 INLINE int
988 window_box_height (struct window *w)
989 {
990 struct frame *f = XFRAME (w->frame);
991 int height = WINDOW_TOTAL_HEIGHT (w);
992
993 xassert (height >= 0);
994
995 /* Note: the code below that determines the mode-line/header-line
996 height is essentially the same as that contained in the macro
997 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
998 the appropriate glyph row has its `mode_line_p' flag set,
999 and if it doesn't, uses estimate_mode_line_height instead. */
1000
1001 if (WINDOW_WANTS_MODELINE_P (w))
1002 {
1003 struct glyph_row *ml_row
1004 = (w->current_matrix && w->current_matrix->rows
1005 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1006 : 0);
1007 if (ml_row && ml_row->mode_line_p)
1008 height -= ml_row->height;
1009 else
1010 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1011 }
1012
1013 if (WINDOW_WANTS_HEADER_LINE_P (w))
1014 {
1015 struct glyph_row *hl_row
1016 = (w->current_matrix && w->current_matrix->rows
1017 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1018 : 0);
1019 if (hl_row && hl_row->mode_line_p)
1020 height -= hl_row->height;
1021 else
1022 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1023 }
1024
1025 /* With a very small font and a mode-line that's taller than
1026 default, we might end up with a negative height. */
1027 return max (0, height);
1028 }
1029
1030 /* Return the window-relative coordinate of the left edge of display
1031 area AREA of window W. AREA < 0 means return the left edge of the
1032 whole window, to the right of the left fringe of W. */
1033
1034 INLINE int
1035 window_box_left_offset (struct window *w, int area)
1036 {
1037 int x;
1038
1039 if (w->pseudo_window_p)
1040 return 0;
1041
1042 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1043
1044 if (area == TEXT_AREA)
1045 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1046 + window_box_width (w, LEFT_MARGIN_AREA));
1047 else if (area == RIGHT_MARGIN_AREA)
1048 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1049 + window_box_width (w, LEFT_MARGIN_AREA)
1050 + window_box_width (w, TEXT_AREA)
1051 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1052 ? 0
1053 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1054 else if (area == LEFT_MARGIN_AREA
1055 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1056 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1057
1058 return x;
1059 }
1060
1061
1062 /* Return the window-relative coordinate of the right edge of display
1063 area AREA of window W. AREA < 0 means return the right edge of the
1064 whole window, to the left of the right fringe of W. */
1065
1066 INLINE int
1067 window_box_right_offset (struct window *w, int area)
1068 {
1069 return window_box_left_offset (w, area) + window_box_width (w, area);
1070 }
1071
1072 /* Return the frame-relative coordinate of the left edge of display
1073 area AREA of window W. AREA < 0 means return the left edge of the
1074 whole window, to the right of the left fringe of W. */
1075
1076 INLINE int
1077 window_box_left (struct window *w, int area)
1078 {
1079 struct frame *f = XFRAME (w->frame);
1080 int x;
1081
1082 if (w->pseudo_window_p)
1083 return FRAME_INTERNAL_BORDER_WIDTH (f);
1084
1085 x = (WINDOW_LEFT_EDGE_X (w)
1086 + window_box_left_offset (w, area));
1087
1088 return x;
1089 }
1090
1091
1092 /* Return the frame-relative coordinate of the right edge of display
1093 area AREA of window W. AREA < 0 means return the right edge of the
1094 whole window, to the left of the right fringe of W. */
1095
1096 INLINE int
1097 window_box_right (struct window *w, int area)
1098 {
1099 return window_box_left (w, area) + window_box_width (w, area);
1100 }
1101
1102 /* Get the bounding box of the display area AREA of window W, without
1103 mode lines, in frame-relative coordinates. AREA < 0 means the
1104 whole window, not including the left and right fringes of
1105 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1106 coordinates of the upper-left corner of the box. Return in
1107 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1108
1109 INLINE void
1110 window_box (struct window *w, int area, int *box_x, int *box_y,
1111 int *box_width, int *box_height)
1112 {
1113 if (box_width)
1114 *box_width = window_box_width (w, area);
1115 if (box_height)
1116 *box_height = window_box_height (w);
1117 if (box_x)
1118 *box_x = window_box_left (w, area);
1119 if (box_y)
1120 {
1121 *box_y = WINDOW_TOP_EDGE_Y (w);
1122 if (WINDOW_WANTS_HEADER_LINE_P (w))
1123 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1124 }
1125 }
1126
1127
1128 /* Get the bounding box of the display area AREA of window W, without
1129 mode lines. AREA < 0 means the whole window, not including the
1130 left and right fringe of the window. Return in *TOP_LEFT_X
1131 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1132 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1133 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1134 box. */
1135
1136 static INLINE void
1137 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1138 int *bottom_right_x, int *bottom_right_y)
1139 {
1140 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1141 bottom_right_y);
1142 *bottom_right_x += *top_left_x;
1143 *bottom_right_y += *top_left_y;
1144 }
1145
1146
1147 \f
1148 /***********************************************************************
1149 Utilities
1150 ***********************************************************************/
1151
1152 /* Return the bottom y-position of the line the iterator IT is in.
1153 This can modify IT's settings. */
1154
1155 int
1156 line_bottom_y (struct it *it)
1157 {
1158 int line_height = it->max_ascent + it->max_descent;
1159 int line_top_y = it->current_y;
1160
1161 if (line_height == 0)
1162 {
1163 if (last_height)
1164 line_height = last_height;
1165 else if (IT_CHARPOS (*it) < ZV)
1166 {
1167 move_it_by_lines (it, 1);
1168 line_height = (it->max_ascent || it->max_descent
1169 ? it->max_ascent + it->max_descent
1170 : last_height);
1171 }
1172 else
1173 {
1174 struct glyph_row *row = it->glyph_row;
1175
1176 /* Use the default character height. */
1177 it->glyph_row = NULL;
1178 it->what = IT_CHARACTER;
1179 it->c = ' ';
1180 it->len = 1;
1181 PRODUCE_GLYPHS (it);
1182 line_height = it->ascent + it->descent;
1183 it->glyph_row = row;
1184 }
1185 }
1186
1187 return line_top_y + line_height;
1188 }
1189
1190
1191 /* Return 1 if position CHARPOS is visible in window W.
1192 CHARPOS < 0 means return info about WINDOW_END position.
1193 If visible, set *X and *Y to pixel coordinates of top left corner.
1194 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1195 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1196
1197 int
1198 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1199 int *rtop, int *rbot, int *rowh, int *vpos)
1200 {
1201 struct it it;
1202 struct text_pos top;
1203 int visible_p = 0;
1204 struct buffer *old_buffer = NULL;
1205
1206 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1207 return visible_p;
1208
1209 if (XBUFFER (w->buffer) != current_buffer)
1210 {
1211 old_buffer = current_buffer;
1212 set_buffer_internal_1 (XBUFFER (w->buffer));
1213 }
1214
1215 SET_TEXT_POS_FROM_MARKER (top, w->start);
1216
1217 /* Compute exact mode line heights. */
1218 if (WINDOW_WANTS_MODELINE_P (w))
1219 current_mode_line_height
1220 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1221 BVAR (current_buffer, mode_line_format));
1222
1223 if (WINDOW_WANTS_HEADER_LINE_P (w))
1224 current_header_line_height
1225 = display_mode_line (w, HEADER_LINE_FACE_ID,
1226 BVAR (current_buffer, header_line_format));
1227
1228 start_display (&it, w, top);
1229 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1230 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1231
1232 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1233 {
1234 /* We have reached CHARPOS, or passed it. How the call to
1235 move_it_to can overshoot: (i) If CHARPOS is on invisible
1236 text, move_it_to stops at the end of the invisible text,
1237 after CHARPOS. (ii) If CHARPOS is in a display vector,
1238 move_it_to stops on its last glyph. */
1239 int top_x = it.current_x;
1240 int top_y = it.current_y;
1241 enum it_method it_method = it.method;
1242 /* Calling line_bottom_y may change it.method, it.position, etc. */
1243 int bottom_y = (last_height = 0, line_bottom_y (&it));
1244 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1245
1246 if (top_y < window_top_y)
1247 visible_p = bottom_y > window_top_y;
1248 else if (top_y < it.last_visible_y)
1249 visible_p = 1;
1250 if (visible_p)
1251 {
1252 if (it_method == GET_FROM_DISPLAY_VECTOR)
1253 {
1254 /* We stopped on the last glyph of a display vector.
1255 Try and recompute. Hack alert! */
1256 if (charpos < 2 || top.charpos >= charpos)
1257 top_x = it.glyph_row->x;
1258 else
1259 {
1260 struct it it2;
1261 start_display (&it2, w, top);
1262 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1263 get_next_display_element (&it2);
1264 PRODUCE_GLYPHS (&it2);
1265 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1266 || it2.current_x > it2.last_visible_x)
1267 top_x = it.glyph_row->x;
1268 else
1269 {
1270 top_x = it2.current_x;
1271 top_y = it2.current_y;
1272 }
1273 }
1274 }
1275
1276 *x = top_x;
1277 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1278 *rtop = max (0, window_top_y - top_y);
1279 *rbot = max (0, bottom_y - it.last_visible_y);
1280 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1281 - max (top_y, window_top_y)));
1282 *vpos = it.vpos;
1283 }
1284 }
1285 else
1286 {
1287 struct it it2;
1288
1289 it2 = it;
1290 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1291 move_it_by_lines (&it, 1);
1292 if (charpos < IT_CHARPOS (it)
1293 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1294 {
1295 visible_p = 1;
1296 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1297 *x = it2.current_x;
1298 *y = it2.current_y + it2.max_ascent - it2.ascent;
1299 *rtop = max (0, -it2.current_y);
1300 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1301 - it.last_visible_y));
1302 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1303 it.last_visible_y)
1304 - max (it2.current_y,
1305 WINDOW_HEADER_LINE_HEIGHT (w))));
1306 *vpos = it2.vpos;
1307 }
1308 }
1309
1310 if (old_buffer)
1311 set_buffer_internal_1 (old_buffer);
1312
1313 current_header_line_height = current_mode_line_height = -1;
1314
1315 if (visible_p && XFASTINT (w->hscroll) > 0)
1316 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1317
1318 #if 0
1319 /* Debugging code. */
1320 if (visible_p)
1321 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1322 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1323 else
1324 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1325 #endif
1326
1327 return visible_p;
1328 }
1329
1330
1331 /* Return the next character from STR. Return in *LEN the length of
1332 the character. This is like STRING_CHAR_AND_LENGTH but never
1333 returns an invalid character. If we find one, we return a `?', but
1334 with the length of the invalid character. */
1335
1336 static INLINE int
1337 string_char_and_length (const unsigned char *str, int *len)
1338 {
1339 int c;
1340
1341 c = STRING_CHAR_AND_LENGTH (str, *len);
1342 if (!CHAR_VALID_P (c, 1))
1343 /* We may not change the length here because other places in Emacs
1344 don't use this function, i.e. they silently accept invalid
1345 characters. */
1346 c = '?';
1347
1348 return c;
1349 }
1350
1351
1352
1353 /* Given a position POS containing a valid character and byte position
1354 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1355
1356 static struct text_pos
1357 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1358 {
1359 xassert (STRINGP (string) && nchars >= 0);
1360
1361 if (STRING_MULTIBYTE (string))
1362 {
1363 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1364 int len;
1365
1366 while (nchars--)
1367 {
1368 string_char_and_length (p, &len);
1369 p += len;
1370 CHARPOS (pos) += 1;
1371 BYTEPOS (pos) += len;
1372 }
1373 }
1374 else
1375 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1376
1377 return pos;
1378 }
1379
1380
1381 /* Value is the text position, i.e. character and byte position,
1382 for character position CHARPOS in STRING. */
1383
1384 static INLINE struct text_pos
1385 string_pos (EMACS_INT charpos, Lisp_Object string)
1386 {
1387 struct text_pos pos;
1388 xassert (STRINGP (string));
1389 xassert (charpos >= 0);
1390 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1391 return pos;
1392 }
1393
1394
1395 /* Value is a text position, i.e. character and byte position, for
1396 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1397 means recognize multibyte characters. */
1398
1399 static struct text_pos
1400 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1401 {
1402 struct text_pos pos;
1403
1404 xassert (s != NULL);
1405 xassert (charpos >= 0);
1406
1407 if (multibyte_p)
1408 {
1409 int len;
1410
1411 SET_TEXT_POS (pos, 0, 0);
1412 while (charpos--)
1413 {
1414 string_char_and_length ((const unsigned char *) s, &len);
1415 s += len;
1416 CHARPOS (pos) += 1;
1417 BYTEPOS (pos) += len;
1418 }
1419 }
1420 else
1421 SET_TEXT_POS (pos, charpos, charpos);
1422
1423 return pos;
1424 }
1425
1426
1427 /* Value is the number of characters in C string S. MULTIBYTE_P
1428 non-zero means recognize multibyte characters. */
1429
1430 static EMACS_INT
1431 number_of_chars (const char *s, int multibyte_p)
1432 {
1433 EMACS_INT nchars;
1434
1435 if (multibyte_p)
1436 {
1437 EMACS_INT rest = strlen (s);
1438 int len;
1439 const unsigned char *p = (const unsigned char *) s;
1440
1441 for (nchars = 0; rest > 0; ++nchars)
1442 {
1443 string_char_and_length (p, &len);
1444 rest -= len, p += len;
1445 }
1446 }
1447 else
1448 nchars = strlen (s);
1449
1450 return nchars;
1451 }
1452
1453
1454 /* Compute byte position NEWPOS->bytepos corresponding to
1455 NEWPOS->charpos. POS is a known position in string STRING.
1456 NEWPOS->charpos must be >= POS.charpos. */
1457
1458 static void
1459 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1460 {
1461 xassert (STRINGP (string));
1462 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1463
1464 if (STRING_MULTIBYTE (string))
1465 *newpos = string_pos_nchars_ahead (pos, string,
1466 CHARPOS (*newpos) - CHARPOS (pos));
1467 else
1468 BYTEPOS (*newpos) = CHARPOS (*newpos);
1469 }
1470
1471 /* EXPORT:
1472 Return an estimation of the pixel height of mode or header lines on
1473 frame F. FACE_ID specifies what line's height to estimate. */
1474
1475 int
1476 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1477 {
1478 #ifdef HAVE_WINDOW_SYSTEM
1479 if (FRAME_WINDOW_P (f))
1480 {
1481 int height = FONT_HEIGHT (FRAME_FONT (f));
1482
1483 /* This function is called so early when Emacs starts that the face
1484 cache and mode line face are not yet initialized. */
1485 if (FRAME_FACE_CACHE (f))
1486 {
1487 struct face *face = FACE_FROM_ID (f, face_id);
1488 if (face)
1489 {
1490 if (face->font)
1491 height = FONT_HEIGHT (face->font);
1492 if (face->box_line_width > 0)
1493 height += 2 * face->box_line_width;
1494 }
1495 }
1496
1497 return height;
1498 }
1499 #endif
1500
1501 return 1;
1502 }
1503
1504 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1505 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1506 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1507 not force the value into range. */
1508
1509 void
1510 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1511 int *x, int *y, NativeRectangle *bounds, int noclip)
1512 {
1513
1514 #ifdef HAVE_WINDOW_SYSTEM
1515 if (FRAME_WINDOW_P (f))
1516 {
1517 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1518 even for negative values. */
1519 if (pix_x < 0)
1520 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1521 if (pix_y < 0)
1522 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1523
1524 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1525 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1526
1527 if (bounds)
1528 STORE_NATIVE_RECT (*bounds,
1529 FRAME_COL_TO_PIXEL_X (f, pix_x),
1530 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1531 FRAME_COLUMN_WIDTH (f) - 1,
1532 FRAME_LINE_HEIGHT (f) - 1);
1533
1534 if (!noclip)
1535 {
1536 if (pix_x < 0)
1537 pix_x = 0;
1538 else if (pix_x > FRAME_TOTAL_COLS (f))
1539 pix_x = FRAME_TOTAL_COLS (f);
1540
1541 if (pix_y < 0)
1542 pix_y = 0;
1543 else if (pix_y > FRAME_LINES (f))
1544 pix_y = FRAME_LINES (f);
1545 }
1546 }
1547 #endif
1548
1549 *x = pix_x;
1550 *y = pix_y;
1551 }
1552
1553
1554 /* Find the glyph under window-relative coordinates X/Y in window W.
1555 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1556 strings. Return in *HPOS and *VPOS the row and column number of
1557 the glyph found. Return in *AREA the glyph area containing X.
1558 Value is a pointer to the glyph found or null if X/Y is not on
1559 text, or we can't tell because W's current matrix is not up to
1560 date. */
1561
1562 static
1563 struct glyph *
1564 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1565 int *dx, int *dy, int *area)
1566 {
1567 struct glyph *glyph, *end;
1568 struct glyph_row *row = NULL;
1569 int x0, i;
1570
1571 /* Find row containing Y. Give up if some row is not enabled. */
1572 for (i = 0; i < w->current_matrix->nrows; ++i)
1573 {
1574 row = MATRIX_ROW (w->current_matrix, i);
1575 if (!row->enabled_p)
1576 return NULL;
1577 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1578 break;
1579 }
1580
1581 *vpos = i;
1582 *hpos = 0;
1583
1584 /* Give up if Y is not in the window. */
1585 if (i == w->current_matrix->nrows)
1586 return NULL;
1587
1588 /* Get the glyph area containing X. */
1589 if (w->pseudo_window_p)
1590 {
1591 *area = TEXT_AREA;
1592 x0 = 0;
1593 }
1594 else
1595 {
1596 if (x < window_box_left_offset (w, TEXT_AREA))
1597 {
1598 *area = LEFT_MARGIN_AREA;
1599 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1600 }
1601 else if (x < window_box_right_offset (w, TEXT_AREA))
1602 {
1603 *area = TEXT_AREA;
1604 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1605 }
1606 else
1607 {
1608 *area = RIGHT_MARGIN_AREA;
1609 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1610 }
1611 }
1612
1613 /* Find glyph containing X. */
1614 glyph = row->glyphs[*area];
1615 end = glyph + row->used[*area];
1616 x -= x0;
1617 while (glyph < end && x >= glyph->pixel_width)
1618 {
1619 x -= glyph->pixel_width;
1620 ++glyph;
1621 }
1622
1623 if (glyph == end)
1624 return NULL;
1625
1626 if (dx)
1627 {
1628 *dx = x;
1629 *dy = y - (row->y + row->ascent - glyph->ascent);
1630 }
1631
1632 *hpos = glyph - row->glyphs[*area];
1633 return glyph;
1634 }
1635
1636 /* Convert frame-relative x/y to coordinates relative to window W.
1637 Takes pseudo-windows into account. */
1638
1639 static void
1640 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1641 {
1642 if (w->pseudo_window_p)
1643 {
1644 /* A pseudo-window is always full-width, and starts at the
1645 left edge of the frame, plus a frame border. */
1646 struct frame *f = XFRAME (w->frame);
1647 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1648 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1649 }
1650 else
1651 {
1652 *x -= WINDOW_LEFT_EDGE_X (w);
1653 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1654 }
1655 }
1656
1657 #ifdef HAVE_WINDOW_SYSTEM
1658
1659 /* EXPORT:
1660 Return in RECTS[] at most N clipping rectangles for glyph string S.
1661 Return the number of stored rectangles. */
1662
1663 int
1664 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1665 {
1666 XRectangle r;
1667
1668 if (n <= 0)
1669 return 0;
1670
1671 if (s->row->full_width_p)
1672 {
1673 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1674 r.x = WINDOW_LEFT_EDGE_X (s->w);
1675 r.width = WINDOW_TOTAL_WIDTH (s->w);
1676
1677 /* Unless displaying a mode or menu bar line, which are always
1678 fully visible, clip to the visible part of the row. */
1679 if (s->w->pseudo_window_p)
1680 r.height = s->row->visible_height;
1681 else
1682 r.height = s->height;
1683 }
1684 else
1685 {
1686 /* This is a text line that may be partially visible. */
1687 r.x = window_box_left (s->w, s->area);
1688 r.width = window_box_width (s->w, s->area);
1689 r.height = s->row->visible_height;
1690 }
1691
1692 if (s->clip_head)
1693 if (r.x < s->clip_head->x)
1694 {
1695 if (r.width >= s->clip_head->x - r.x)
1696 r.width -= s->clip_head->x - r.x;
1697 else
1698 r.width = 0;
1699 r.x = s->clip_head->x;
1700 }
1701 if (s->clip_tail)
1702 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1703 {
1704 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1705 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1706 else
1707 r.width = 0;
1708 }
1709
1710 /* If S draws overlapping rows, it's sufficient to use the top and
1711 bottom of the window for clipping because this glyph string
1712 intentionally draws over other lines. */
1713 if (s->for_overlaps)
1714 {
1715 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1716 r.height = window_text_bottom_y (s->w) - r.y;
1717
1718 /* Alas, the above simple strategy does not work for the
1719 environments with anti-aliased text: if the same text is
1720 drawn onto the same place multiple times, it gets thicker.
1721 If the overlap we are processing is for the erased cursor, we
1722 take the intersection with the rectagle of the cursor. */
1723 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1724 {
1725 XRectangle rc, r_save = r;
1726
1727 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1728 rc.y = s->w->phys_cursor.y;
1729 rc.width = s->w->phys_cursor_width;
1730 rc.height = s->w->phys_cursor_height;
1731
1732 x_intersect_rectangles (&r_save, &rc, &r);
1733 }
1734 }
1735 else
1736 {
1737 /* Don't use S->y for clipping because it doesn't take partially
1738 visible lines into account. For example, it can be negative for
1739 partially visible lines at the top of a window. */
1740 if (!s->row->full_width_p
1741 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1742 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1743 else
1744 r.y = max (0, s->row->y);
1745 }
1746
1747 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1748
1749 /* If drawing the cursor, don't let glyph draw outside its
1750 advertised boundaries. Cleartype does this under some circumstances. */
1751 if (s->hl == DRAW_CURSOR)
1752 {
1753 struct glyph *glyph = s->first_glyph;
1754 int height, max_y;
1755
1756 if (s->x > r.x)
1757 {
1758 r.width -= s->x - r.x;
1759 r.x = s->x;
1760 }
1761 r.width = min (r.width, glyph->pixel_width);
1762
1763 /* If r.y is below window bottom, ensure that we still see a cursor. */
1764 height = min (glyph->ascent + glyph->descent,
1765 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1766 max_y = window_text_bottom_y (s->w) - height;
1767 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1768 if (s->ybase - glyph->ascent > max_y)
1769 {
1770 r.y = max_y;
1771 r.height = height;
1772 }
1773 else
1774 {
1775 /* Don't draw cursor glyph taller than our actual glyph. */
1776 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1777 if (height < r.height)
1778 {
1779 max_y = r.y + r.height;
1780 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1781 r.height = min (max_y - r.y, height);
1782 }
1783 }
1784 }
1785
1786 if (s->row->clip)
1787 {
1788 XRectangle r_save = r;
1789
1790 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1791 r.width = 0;
1792 }
1793
1794 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1795 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1796 {
1797 #ifdef CONVERT_FROM_XRECT
1798 CONVERT_FROM_XRECT (r, *rects);
1799 #else
1800 *rects = r;
1801 #endif
1802 return 1;
1803 }
1804 else
1805 {
1806 /* If we are processing overlapping and allowed to return
1807 multiple clipping rectangles, we exclude the row of the glyph
1808 string from the clipping rectangle. This is to avoid drawing
1809 the same text on the environment with anti-aliasing. */
1810 #ifdef CONVERT_FROM_XRECT
1811 XRectangle rs[2];
1812 #else
1813 XRectangle *rs = rects;
1814 #endif
1815 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1816
1817 if (s->for_overlaps & OVERLAPS_PRED)
1818 {
1819 rs[i] = r;
1820 if (r.y + r.height > row_y)
1821 {
1822 if (r.y < row_y)
1823 rs[i].height = row_y - r.y;
1824 else
1825 rs[i].height = 0;
1826 }
1827 i++;
1828 }
1829 if (s->for_overlaps & OVERLAPS_SUCC)
1830 {
1831 rs[i] = r;
1832 if (r.y < row_y + s->row->visible_height)
1833 {
1834 if (r.y + r.height > row_y + s->row->visible_height)
1835 {
1836 rs[i].y = row_y + s->row->visible_height;
1837 rs[i].height = r.y + r.height - rs[i].y;
1838 }
1839 else
1840 rs[i].height = 0;
1841 }
1842 i++;
1843 }
1844
1845 n = i;
1846 #ifdef CONVERT_FROM_XRECT
1847 for (i = 0; i < n; i++)
1848 CONVERT_FROM_XRECT (rs[i], rects[i]);
1849 #endif
1850 return n;
1851 }
1852 }
1853
1854 /* EXPORT:
1855 Return in *NR the clipping rectangle for glyph string S. */
1856
1857 void
1858 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1859 {
1860 get_glyph_string_clip_rects (s, nr, 1);
1861 }
1862
1863
1864 /* EXPORT:
1865 Return the position and height of the phys cursor in window W.
1866 Set w->phys_cursor_width to width of phys cursor.
1867 */
1868
1869 void
1870 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1871 struct glyph *glyph, int *xp, int *yp, int *heightp)
1872 {
1873 struct frame *f = XFRAME (WINDOW_FRAME (w));
1874 int x, y, wd, h, h0, y0;
1875
1876 /* Compute the width of the rectangle to draw. If on a stretch
1877 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1878 rectangle as wide as the glyph, but use a canonical character
1879 width instead. */
1880 wd = glyph->pixel_width - 1;
1881 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1882 wd++; /* Why? */
1883 #endif
1884
1885 x = w->phys_cursor.x;
1886 if (x < 0)
1887 {
1888 wd += x;
1889 x = 0;
1890 }
1891
1892 if (glyph->type == STRETCH_GLYPH
1893 && !x_stretch_cursor_p)
1894 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1895 w->phys_cursor_width = wd;
1896
1897 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1898
1899 /* If y is below window bottom, ensure that we still see a cursor. */
1900 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1901
1902 h = max (h0, glyph->ascent + glyph->descent);
1903 h0 = min (h0, glyph->ascent + glyph->descent);
1904
1905 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1906 if (y < y0)
1907 {
1908 h = max (h - (y0 - y) + 1, h0);
1909 y = y0 - 1;
1910 }
1911 else
1912 {
1913 y0 = window_text_bottom_y (w) - h0;
1914 if (y > y0)
1915 {
1916 h += y - y0;
1917 y = y0;
1918 }
1919 }
1920
1921 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1922 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1923 *heightp = h;
1924 }
1925
1926 /*
1927 * Remember which glyph the mouse is over.
1928 */
1929
1930 void
1931 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1932 {
1933 Lisp_Object window;
1934 struct window *w;
1935 struct glyph_row *r, *gr, *end_row;
1936 enum window_part part;
1937 enum glyph_row_area area;
1938 int x, y, width, height;
1939
1940 /* Try to determine frame pixel position and size of the glyph under
1941 frame pixel coordinates X/Y on frame F. */
1942
1943 if (!f->glyphs_initialized_p
1944 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1945 NILP (window)))
1946 {
1947 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1948 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1949 goto virtual_glyph;
1950 }
1951
1952 w = XWINDOW (window);
1953 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1954 height = WINDOW_FRAME_LINE_HEIGHT (w);
1955
1956 x = window_relative_x_coord (w, part, gx);
1957 y = gy - WINDOW_TOP_EDGE_Y (w);
1958
1959 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1960 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1961
1962 if (w->pseudo_window_p)
1963 {
1964 area = TEXT_AREA;
1965 part = ON_MODE_LINE; /* Don't adjust margin. */
1966 goto text_glyph;
1967 }
1968
1969 switch (part)
1970 {
1971 case ON_LEFT_MARGIN:
1972 area = LEFT_MARGIN_AREA;
1973 goto text_glyph;
1974
1975 case ON_RIGHT_MARGIN:
1976 area = RIGHT_MARGIN_AREA;
1977 goto text_glyph;
1978
1979 case ON_HEADER_LINE:
1980 case ON_MODE_LINE:
1981 gr = (part == ON_HEADER_LINE
1982 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1983 : MATRIX_MODE_LINE_ROW (w->current_matrix));
1984 gy = gr->y;
1985 area = TEXT_AREA;
1986 goto text_glyph_row_found;
1987
1988 case ON_TEXT:
1989 area = TEXT_AREA;
1990
1991 text_glyph:
1992 gr = 0; gy = 0;
1993 for (; r <= end_row && r->enabled_p; ++r)
1994 if (r->y + r->height > y)
1995 {
1996 gr = r; gy = r->y;
1997 break;
1998 }
1999
2000 text_glyph_row_found:
2001 if (gr && gy <= y)
2002 {
2003 struct glyph *g = gr->glyphs[area];
2004 struct glyph *end = g + gr->used[area];
2005
2006 height = gr->height;
2007 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2008 if (gx + g->pixel_width > x)
2009 break;
2010
2011 if (g < end)
2012 {
2013 if (g->type == IMAGE_GLYPH)
2014 {
2015 /* Don't remember when mouse is over image, as
2016 image may have hot-spots. */
2017 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2018 return;
2019 }
2020 width = g->pixel_width;
2021 }
2022 else
2023 {
2024 /* Use nominal char spacing at end of line. */
2025 x -= gx;
2026 gx += (x / width) * width;
2027 }
2028
2029 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2030 gx += window_box_left_offset (w, area);
2031 }
2032 else
2033 {
2034 /* Use nominal line height at end of window. */
2035 gx = (x / width) * width;
2036 y -= gy;
2037 gy += (y / height) * height;
2038 }
2039 break;
2040
2041 case ON_LEFT_FRINGE:
2042 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2043 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2044 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2045 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2046 goto row_glyph;
2047
2048 case ON_RIGHT_FRINGE:
2049 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2050 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2051 : window_box_right_offset (w, TEXT_AREA));
2052 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2053 goto row_glyph;
2054
2055 case ON_SCROLL_BAR:
2056 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2057 ? 0
2058 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2059 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2060 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2061 : 0)));
2062 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2063
2064 row_glyph:
2065 gr = 0, gy = 0;
2066 for (; r <= end_row && r->enabled_p; ++r)
2067 if (r->y + r->height > y)
2068 {
2069 gr = r; gy = r->y;
2070 break;
2071 }
2072
2073 if (gr && gy <= y)
2074 height = gr->height;
2075 else
2076 {
2077 /* Use nominal line height at end of window. */
2078 y -= gy;
2079 gy += (y / height) * height;
2080 }
2081 break;
2082
2083 default:
2084 ;
2085 virtual_glyph:
2086 /* If there is no glyph under the mouse, then we divide the screen
2087 into a grid of the smallest glyph in the frame, and use that
2088 as our "glyph". */
2089
2090 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2091 round down even for negative values. */
2092 if (gx < 0)
2093 gx -= width - 1;
2094 if (gy < 0)
2095 gy -= height - 1;
2096
2097 gx = (gx / width) * width;
2098 gy = (gy / height) * height;
2099
2100 goto store_rect;
2101 }
2102
2103 gx += WINDOW_LEFT_EDGE_X (w);
2104 gy += WINDOW_TOP_EDGE_Y (w);
2105
2106 store_rect:
2107 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2108
2109 /* Visible feedback for debugging. */
2110 #if 0
2111 #if HAVE_X_WINDOWS
2112 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2113 f->output_data.x->normal_gc,
2114 gx, gy, width, height);
2115 #endif
2116 #endif
2117 }
2118
2119
2120 #endif /* HAVE_WINDOW_SYSTEM */
2121
2122 \f
2123 /***********************************************************************
2124 Lisp form evaluation
2125 ***********************************************************************/
2126
2127 /* Error handler for safe_eval and safe_call. */
2128
2129 static Lisp_Object
2130 safe_eval_handler (Lisp_Object arg)
2131 {
2132 add_to_log ("Error during redisplay: %S", arg, Qnil);
2133 return Qnil;
2134 }
2135
2136
2137 /* Evaluate SEXPR and return the result, or nil if something went
2138 wrong. Prevent redisplay during the evaluation. */
2139
2140 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2141 Return the result, or nil if something went wrong. Prevent
2142 redisplay during the evaluation. */
2143
2144 Lisp_Object
2145 safe_call (size_t nargs, Lisp_Object *args)
2146 {
2147 Lisp_Object val;
2148
2149 if (inhibit_eval_during_redisplay)
2150 val = Qnil;
2151 else
2152 {
2153 int count = SPECPDL_INDEX ();
2154 struct gcpro gcpro1;
2155
2156 GCPRO1 (args[0]);
2157 gcpro1.nvars = nargs;
2158 specbind (Qinhibit_redisplay, Qt);
2159 /* Use Qt to ensure debugger does not run,
2160 so there is no possibility of wanting to redisplay. */
2161 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2162 safe_eval_handler);
2163 UNGCPRO;
2164 val = unbind_to (count, val);
2165 }
2166
2167 return val;
2168 }
2169
2170
2171 /* Call function FN with one argument ARG.
2172 Return the result, or nil if something went wrong. */
2173
2174 Lisp_Object
2175 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2176 {
2177 Lisp_Object args[2];
2178 args[0] = fn;
2179 args[1] = arg;
2180 return safe_call (2, args);
2181 }
2182
2183 static Lisp_Object Qeval;
2184
2185 Lisp_Object
2186 safe_eval (Lisp_Object sexpr)
2187 {
2188 return safe_call1 (Qeval, sexpr);
2189 }
2190
2191 /* Call function FN with one argument ARG.
2192 Return the result, or nil if something went wrong. */
2193
2194 Lisp_Object
2195 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2196 {
2197 Lisp_Object args[3];
2198 args[0] = fn;
2199 args[1] = arg1;
2200 args[2] = arg2;
2201 return safe_call (3, args);
2202 }
2203
2204
2205 \f
2206 /***********************************************************************
2207 Debugging
2208 ***********************************************************************/
2209
2210 #if 0
2211
2212 /* Define CHECK_IT to perform sanity checks on iterators.
2213 This is for debugging. It is too slow to do unconditionally. */
2214
2215 static void
2216 check_it (it)
2217 struct it *it;
2218 {
2219 if (it->method == GET_FROM_STRING)
2220 {
2221 xassert (STRINGP (it->string));
2222 xassert (IT_STRING_CHARPOS (*it) >= 0);
2223 }
2224 else
2225 {
2226 xassert (IT_STRING_CHARPOS (*it) < 0);
2227 if (it->method == GET_FROM_BUFFER)
2228 {
2229 /* Check that character and byte positions agree. */
2230 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2231 }
2232 }
2233
2234 if (it->dpvec)
2235 xassert (it->current.dpvec_index >= 0);
2236 else
2237 xassert (it->current.dpvec_index < 0);
2238 }
2239
2240 #define CHECK_IT(IT) check_it ((IT))
2241
2242 #else /* not 0 */
2243
2244 #define CHECK_IT(IT) (void) 0
2245
2246 #endif /* not 0 */
2247
2248
2249 #if GLYPH_DEBUG
2250
2251 /* Check that the window end of window W is what we expect it
2252 to be---the last row in the current matrix displaying text. */
2253
2254 static void
2255 check_window_end (w)
2256 struct window *w;
2257 {
2258 if (!MINI_WINDOW_P (w)
2259 && !NILP (w->window_end_valid))
2260 {
2261 struct glyph_row *row;
2262 xassert ((row = MATRIX_ROW (w->current_matrix,
2263 XFASTINT (w->window_end_vpos)),
2264 !row->enabled_p
2265 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2266 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2267 }
2268 }
2269
2270 #define CHECK_WINDOW_END(W) check_window_end ((W))
2271
2272 #else /* not GLYPH_DEBUG */
2273
2274 #define CHECK_WINDOW_END(W) (void) 0
2275
2276 #endif /* not GLYPH_DEBUG */
2277
2278
2279 \f
2280 /***********************************************************************
2281 Iterator initialization
2282 ***********************************************************************/
2283
2284 /* Initialize IT for displaying current_buffer in window W, starting
2285 at character position CHARPOS. CHARPOS < 0 means that no buffer
2286 position is specified which is useful when the iterator is assigned
2287 a position later. BYTEPOS is the byte position corresponding to
2288 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2289
2290 If ROW is not null, calls to produce_glyphs with IT as parameter
2291 will produce glyphs in that row.
2292
2293 BASE_FACE_ID is the id of a base face to use. It must be one of
2294 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2295 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2296 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2297
2298 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2299 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2300 will be initialized to use the corresponding mode line glyph row of
2301 the desired matrix of W. */
2302
2303 void
2304 init_iterator (struct it *it, struct window *w,
2305 EMACS_INT charpos, EMACS_INT bytepos,
2306 struct glyph_row *row, enum face_id base_face_id)
2307 {
2308 int highlight_region_p;
2309 enum face_id remapped_base_face_id = base_face_id;
2310
2311 /* Some precondition checks. */
2312 xassert (w != NULL && it != NULL);
2313 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2314 && charpos <= ZV));
2315
2316 /* If face attributes have been changed since the last redisplay,
2317 free realized faces now because they depend on face definitions
2318 that might have changed. Don't free faces while there might be
2319 desired matrices pending which reference these faces. */
2320 if (face_change_count && !inhibit_free_realized_faces)
2321 {
2322 face_change_count = 0;
2323 free_all_realized_faces (Qnil);
2324 }
2325
2326 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2327 if (! NILP (Vface_remapping_alist))
2328 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2329
2330 /* Use one of the mode line rows of W's desired matrix if
2331 appropriate. */
2332 if (row == NULL)
2333 {
2334 if (base_face_id == MODE_LINE_FACE_ID
2335 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2336 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2337 else if (base_face_id == HEADER_LINE_FACE_ID)
2338 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2339 }
2340
2341 /* Clear IT. */
2342 memset (it, 0, sizeof *it);
2343 it->current.overlay_string_index = -1;
2344 it->current.dpvec_index = -1;
2345 it->base_face_id = remapped_base_face_id;
2346 it->string = Qnil;
2347 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2348 it->paragraph_embedding = L2R;
2349 it->bidi_it.string.lstring = Qnil;
2350 it->bidi_it.string.s = NULL;
2351 it->bidi_it.string.bufpos = 0;
2352
2353 /* The window in which we iterate over current_buffer: */
2354 XSETWINDOW (it->window, w);
2355 it->w = w;
2356 it->f = XFRAME (w->frame);
2357
2358 it->cmp_it.id = -1;
2359
2360 /* Extra space between lines (on window systems only). */
2361 if (base_face_id == DEFAULT_FACE_ID
2362 && FRAME_WINDOW_P (it->f))
2363 {
2364 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2365 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2366 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2367 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2368 * FRAME_LINE_HEIGHT (it->f));
2369 else if (it->f->extra_line_spacing > 0)
2370 it->extra_line_spacing = it->f->extra_line_spacing;
2371 it->max_extra_line_spacing = 0;
2372 }
2373
2374 /* If realized faces have been removed, e.g. because of face
2375 attribute changes of named faces, recompute them. When running
2376 in batch mode, the face cache of the initial frame is null. If
2377 we happen to get called, make a dummy face cache. */
2378 if (FRAME_FACE_CACHE (it->f) == NULL)
2379 init_frame_faces (it->f);
2380 if (FRAME_FACE_CACHE (it->f)->used == 0)
2381 recompute_basic_faces (it->f);
2382
2383 /* Current value of the `slice', `space-width', and 'height' properties. */
2384 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2385 it->space_width = Qnil;
2386 it->font_height = Qnil;
2387 it->override_ascent = -1;
2388
2389 /* Are control characters displayed as `^C'? */
2390 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2391
2392 /* -1 means everything between a CR and the following line end
2393 is invisible. >0 means lines indented more than this value are
2394 invisible. */
2395 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2396 ? XFASTINT (BVAR (current_buffer, selective_display))
2397 : (!NILP (BVAR (current_buffer, selective_display))
2398 ? -1 : 0));
2399 it->selective_display_ellipsis_p
2400 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2401
2402 /* Display table to use. */
2403 it->dp = window_display_table (w);
2404
2405 /* Are multibyte characters enabled in current_buffer? */
2406 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2407
2408
2409 /* Bidirectional reordering of strings is controlled by the default
2410 value of bidi-display-reordering. For buffers, we reconsider
2411 this below. */
2412 it->bidi_p =
2413 !NILP (BVAR (&buffer_defaults, bidi_display_reordering)) && it->multibyte_p;
2414
2415 /* Non-zero if we should highlight the region. */
2416 highlight_region_p
2417 = (!NILP (Vtransient_mark_mode)
2418 && !NILP (BVAR (current_buffer, mark_active))
2419 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2420
2421 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2422 start and end of a visible region in window IT->w. Set both to
2423 -1 to indicate no region. */
2424 if (highlight_region_p
2425 /* Maybe highlight only in selected window. */
2426 && (/* Either show region everywhere. */
2427 highlight_nonselected_windows
2428 /* Or show region in the selected window. */
2429 || w == XWINDOW (selected_window)
2430 /* Or show the region if we are in the mini-buffer and W is
2431 the window the mini-buffer refers to. */
2432 || (MINI_WINDOW_P (XWINDOW (selected_window))
2433 && WINDOWP (minibuf_selected_window)
2434 && w == XWINDOW (minibuf_selected_window))))
2435 {
2436 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2437 it->region_beg_charpos = min (PT, markpos);
2438 it->region_end_charpos = max (PT, markpos);
2439 }
2440 else
2441 it->region_beg_charpos = it->region_end_charpos = -1;
2442
2443 /* Get the position at which the redisplay_end_trigger hook should
2444 be run, if it is to be run at all. */
2445 if (MARKERP (w->redisplay_end_trigger)
2446 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2447 it->redisplay_end_trigger_charpos
2448 = marker_position (w->redisplay_end_trigger);
2449 else if (INTEGERP (w->redisplay_end_trigger))
2450 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2451
2452 /* Correct bogus values of tab_width. */
2453 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2454 if (it->tab_width <= 0 || it->tab_width > 1000)
2455 it->tab_width = 8;
2456
2457 /* Are lines in the display truncated? */
2458 if (base_face_id != DEFAULT_FACE_ID
2459 || XINT (it->w->hscroll)
2460 || (! WINDOW_FULL_WIDTH_P (it->w)
2461 && ((!NILP (Vtruncate_partial_width_windows)
2462 && !INTEGERP (Vtruncate_partial_width_windows))
2463 || (INTEGERP (Vtruncate_partial_width_windows)
2464 && (WINDOW_TOTAL_COLS (it->w)
2465 < XINT (Vtruncate_partial_width_windows))))))
2466 it->line_wrap = TRUNCATE;
2467 else if (NILP (BVAR (current_buffer, truncate_lines)))
2468 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2469 ? WINDOW_WRAP : WORD_WRAP;
2470 else
2471 it->line_wrap = TRUNCATE;
2472
2473 /* Get dimensions of truncation and continuation glyphs. These are
2474 displayed as fringe bitmaps under X, so we don't need them for such
2475 frames. */
2476 if (!FRAME_WINDOW_P (it->f))
2477 {
2478 if (it->line_wrap == TRUNCATE)
2479 {
2480 /* We will need the truncation glyph. */
2481 xassert (it->glyph_row == NULL);
2482 produce_special_glyphs (it, IT_TRUNCATION);
2483 it->truncation_pixel_width = it->pixel_width;
2484 }
2485 else
2486 {
2487 /* We will need the continuation glyph. */
2488 xassert (it->glyph_row == NULL);
2489 produce_special_glyphs (it, IT_CONTINUATION);
2490 it->continuation_pixel_width = it->pixel_width;
2491 }
2492
2493 /* Reset these values to zero because the produce_special_glyphs
2494 above has changed them. */
2495 it->pixel_width = it->ascent = it->descent = 0;
2496 it->phys_ascent = it->phys_descent = 0;
2497 }
2498
2499 /* Set this after getting the dimensions of truncation and
2500 continuation glyphs, so that we don't produce glyphs when calling
2501 produce_special_glyphs, above. */
2502 it->glyph_row = row;
2503 it->area = TEXT_AREA;
2504
2505 /* Forget any previous info about this row being reversed. */
2506 if (it->glyph_row)
2507 it->glyph_row->reversed_p = 0;
2508
2509 /* Get the dimensions of the display area. The display area
2510 consists of the visible window area plus a horizontally scrolled
2511 part to the left of the window. All x-values are relative to the
2512 start of this total display area. */
2513 if (base_face_id != DEFAULT_FACE_ID)
2514 {
2515 /* Mode lines, menu bar in terminal frames. */
2516 it->first_visible_x = 0;
2517 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2518 }
2519 else
2520 {
2521 it->first_visible_x
2522 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2523 it->last_visible_x = (it->first_visible_x
2524 + window_box_width (w, TEXT_AREA));
2525
2526 /* If we truncate lines, leave room for the truncator glyph(s) at
2527 the right margin. Otherwise, leave room for the continuation
2528 glyph(s). Truncation and continuation glyphs are not inserted
2529 for window-based redisplay. */
2530 if (!FRAME_WINDOW_P (it->f))
2531 {
2532 if (it->line_wrap == TRUNCATE)
2533 it->last_visible_x -= it->truncation_pixel_width;
2534 else
2535 it->last_visible_x -= it->continuation_pixel_width;
2536 }
2537
2538 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2539 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2540 }
2541
2542 /* Leave room for a border glyph. */
2543 if (!FRAME_WINDOW_P (it->f)
2544 && !WINDOW_RIGHTMOST_P (it->w))
2545 it->last_visible_x -= 1;
2546
2547 it->last_visible_y = window_text_bottom_y (w);
2548
2549 /* For mode lines and alike, arrange for the first glyph having a
2550 left box line if the face specifies a box. */
2551 if (base_face_id != DEFAULT_FACE_ID)
2552 {
2553 struct face *face;
2554
2555 it->face_id = remapped_base_face_id;
2556
2557 /* If we have a boxed mode line, make the first character appear
2558 with a left box line. */
2559 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2560 if (face->box != FACE_NO_BOX)
2561 it->start_of_box_run_p = 1;
2562 }
2563
2564 /* If a buffer position was specified, set the iterator there,
2565 getting overlays and face properties from that position. */
2566 if (charpos >= BUF_BEG (current_buffer))
2567 {
2568 it->end_charpos = ZV;
2569 it->face_id = -1;
2570 IT_CHARPOS (*it) = charpos;
2571
2572 /* Compute byte position if not specified. */
2573 if (bytepos < charpos)
2574 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2575 else
2576 IT_BYTEPOS (*it) = bytepos;
2577
2578 it->start = it->current;
2579 /* Do we need to reorder bidirectional text? Not if this is a
2580 unibyte buffer: by definition, none of the single-byte
2581 characters are strong R2L, so no reordering is needed. And
2582 bidi.c doesn't support unibyte buffers anyway. */
2583 it->bidi_p =
2584 !NILP (BVAR (current_buffer, bidi_display_reordering))
2585 && it->multibyte_p;
2586
2587 /* If we are to reorder bidirectional text, init the bidi
2588 iterator. */
2589 if (it->bidi_p)
2590 {
2591 /* Note the paragraph direction that this buffer wants to
2592 use. */
2593 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2594 Qleft_to_right))
2595 it->paragraph_embedding = L2R;
2596 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2597 Qright_to_left))
2598 it->paragraph_embedding = R2L;
2599 else
2600 it->paragraph_embedding = NEUTRAL_DIR;
2601 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2602 &it->bidi_it);
2603 }
2604
2605 /* Compute faces etc. */
2606 reseat (it, it->current.pos, 1);
2607 }
2608
2609 CHECK_IT (it);
2610 }
2611
2612
2613 /* Initialize IT for the display of window W with window start POS. */
2614
2615 void
2616 start_display (struct it *it, struct window *w, struct text_pos pos)
2617 {
2618 struct glyph_row *row;
2619 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2620
2621 row = w->desired_matrix->rows + first_vpos;
2622 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2623 it->first_vpos = first_vpos;
2624
2625 /* Don't reseat to previous visible line start if current start
2626 position is in a string or image. */
2627 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2628 {
2629 int start_at_line_beg_p;
2630 int first_y = it->current_y;
2631
2632 /* If window start is not at a line start, skip forward to POS to
2633 get the correct continuation lines width. */
2634 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2635 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2636 if (!start_at_line_beg_p)
2637 {
2638 int new_x;
2639
2640 reseat_at_previous_visible_line_start (it);
2641 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2642
2643 new_x = it->current_x + it->pixel_width;
2644
2645 /* If lines are continued, this line may end in the middle
2646 of a multi-glyph character (e.g. a control character
2647 displayed as \003, or in the middle of an overlay
2648 string). In this case move_it_to above will not have
2649 taken us to the start of the continuation line but to the
2650 end of the continued line. */
2651 if (it->current_x > 0
2652 && it->line_wrap != TRUNCATE /* Lines are continued. */
2653 && (/* And glyph doesn't fit on the line. */
2654 new_x > it->last_visible_x
2655 /* Or it fits exactly and we're on a window
2656 system frame. */
2657 || (new_x == it->last_visible_x
2658 && FRAME_WINDOW_P (it->f))))
2659 {
2660 if (it->current.dpvec_index >= 0
2661 || it->current.overlay_string_index >= 0)
2662 {
2663 set_iterator_to_next (it, 1);
2664 move_it_in_display_line_to (it, -1, -1, 0);
2665 }
2666
2667 it->continuation_lines_width += it->current_x;
2668 }
2669
2670 /* We're starting a new display line, not affected by the
2671 height of the continued line, so clear the appropriate
2672 fields in the iterator structure. */
2673 it->max_ascent = it->max_descent = 0;
2674 it->max_phys_ascent = it->max_phys_descent = 0;
2675
2676 it->current_y = first_y;
2677 it->vpos = 0;
2678 it->current_x = it->hpos = 0;
2679 }
2680 }
2681 }
2682
2683
2684 /* Return 1 if POS is a position in ellipses displayed for invisible
2685 text. W is the window we display, for text property lookup. */
2686
2687 static int
2688 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2689 {
2690 Lisp_Object prop, window;
2691 int ellipses_p = 0;
2692 EMACS_INT charpos = CHARPOS (pos->pos);
2693
2694 /* If POS specifies a position in a display vector, this might
2695 be for an ellipsis displayed for invisible text. We won't
2696 get the iterator set up for delivering that ellipsis unless
2697 we make sure that it gets aware of the invisible text. */
2698 if (pos->dpvec_index >= 0
2699 && pos->overlay_string_index < 0
2700 && CHARPOS (pos->string_pos) < 0
2701 && charpos > BEGV
2702 && (XSETWINDOW (window, w),
2703 prop = Fget_char_property (make_number (charpos),
2704 Qinvisible, window),
2705 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2706 {
2707 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2708 window);
2709 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2710 }
2711
2712 return ellipses_p;
2713 }
2714
2715
2716 /* Initialize IT for stepping through current_buffer in window W,
2717 starting at position POS that includes overlay string and display
2718 vector/ control character translation position information. Value
2719 is zero if there are overlay strings with newlines at POS. */
2720
2721 static int
2722 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2723 {
2724 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2725 int i, overlay_strings_with_newlines = 0;
2726
2727 /* If POS specifies a position in a display vector, this might
2728 be for an ellipsis displayed for invisible text. We won't
2729 get the iterator set up for delivering that ellipsis unless
2730 we make sure that it gets aware of the invisible text. */
2731 if (in_ellipses_for_invisible_text_p (pos, w))
2732 {
2733 --charpos;
2734 bytepos = 0;
2735 }
2736
2737 /* Keep in mind: the call to reseat in init_iterator skips invisible
2738 text, so we might end up at a position different from POS. This
2739 is only a problem when POS is a row start after a newline and an
2740 overlay starts there with an after-string, and the overlay has an
2741 invisible property. Since we don't skip invisible text in
2742 display_line and elsewhere immediately after consuming the
2743 newline before the row start, such a POS will not be in a string,
2744 but the call to init_iterator below will move us to the
2745 after-string. */
2746 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2747
2748 /* This only scans the current chunk -- it should scan all chunks.
2749 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2750 to 16 in 22.1 to make this a lesser problem. */
2751 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2752 {
2753 const char *s = SSDATA (it->overlay_strings[i]);
2754 const char *e = s + SBYTES (it->overlay_strings[i]);
2755
2756 while (s < e && *s != '\n')
2757 ++s;
2758
2759 if (s < e)
2760 {
2761 overlay_strings_with_newlines = 1;
2762 break;
2763 }
2764 }
2765
2766 /* If position is within an overlay string, set up IT to the right
2767 overlay string. */
2768 if (pos->overlay_string_index >= 0)
2769 {
2770 int relative_index;
2771
2772 /* If the first overlay string happens to have a `display'
2773 property for an image, the iterator will be set up for that
2774 image, and we have to undo that setup first before we can
2775 correct the overlay string index. */
2776 if (it->method == GET_FROM_IMAGE)
2777 pop_it (it);
2778
2779 /* We already have the first chunk of overlay strings in
2780 IT->overlay_strings. Load more until the one for
2781 pos->overlay_string_index is in IT->overlay_strings. */
2782 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2783 {
2784 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2785 it->current.overlay_string_index = 0;
2786 while (n--)
2787 {
2788 load_overlay_strings (it, 0);
2789 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2790 }
2791 }
2792
2793 it->current.overlay_string_index = pos->overlay_string_index;
2794 relative_index = (it->current.overlay_string_index
2795 % OVERLAY_STRING_CHUNK_SIZE);
2796 it->string = it->overlay_strings[relative_index];
2797 xassert (STRINGP (it->string));
2798 it->current.string_pos = pos->string_pos;
2799 it->method = GET_FROM_STRING;
2800 }
2801
2802 if (CHARPOS (pos->string_pos) >= 0)
2803 {
2804 /* Recorded position is not in an overlay string, but in another
2805 string. This can only be a string from a `display' property.
2806 IT should already be filled with that string. */
2807 it->current.string_pos = pos->string_pos;
2808 xassert (STRINGP (it->string));
2809 }
2810
2811 /* Restore position in display vector translations, control
2812 character translations or ellipses. */
2813 if (pos->dpvec_index >= 0)
2814 {
2815 if (it->dpvec == NULL)
2816 get_next_display_element (it);
2817 xassert (it->dpvec && it->current.dpvec_index == 0);
2818 it->current.dpvec_index = pos->dpvec_index;
2819 }
2820
2821 CHECK_IT (it);
2822 return !overlay_strings_with_newlines;
2823 }
2824
2825
2826 /* Initialize IT for stepping through current_buffer in window W
2827 starting at ROW->start. */
2828
2829 static void
2830 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2831 {
2832 init_from_display_pos (it, w, &row->start);
2833 it->start = row->start;
2834 it->continuation_lines_width = row->continuation_lines_width;
2835 CHECK_IT (it);
2836 }
2837
2838
2839 /* Initialize IT for stepping through current_buffer in window W
2840 starting in the line following ROW, i.e. starting at ROW->end.
2841 Value is zero if there are overlay strings with newlines at ROW's
2842 end position. */
2843
2844 static int
2845 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2846 {
2847 int success = 0;
2848
2849 if (init_from_display_pos (it, w, &row->end))
2850 {
2851 if (row->continued_p)
2852 it->continuation_lines_width
2853 = row->continuation_lines_width + row->pixel_width;
2854 CHECK_IT (it);
2855 success = 1;
2856 }
2857
2858 return success;
2859 }
2860
2861
2862
2863 \f
2864 /***********************************************************************
2865 Text properties
2866 ***********************************************************************/
2867
2868 /* Called when IT reaches IT->stop_charpos. Handle text property and
2869 overlay changes. Set IT->stop_charpos to the next position where
2870 to stop. */
2871
2872 static void
2873 handle_stop (struct it *it)
2874 {
2875 enum prop_handled handled;
2876 int handle_overlay_change_p;
2877 struct props *p;
2878
2879 it->dpvec = NULL;
2880 it->current.dpvec_index = -1;
2881 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2882 it->ignore_overlay_strings_at_pos_p = 0;
2883 it->ellipsis_p = 0;
2884
2885 /* Use face of preceding text for ellipsis (if invisible) */
2886 if (it->selective_display_ellipsis_p)
2887 it->saved_face_id = it->face_id;
2888
2889 do
2890 {
2891 handled = HANDLED_NORMALLY;
2892
2893 /* Call text property handlers. */
2894 for (p = it_props; p->handler; ++p)
2895 {
2896 handled = p->handler (it);
2897
2898 if (handled == HANDLED_RECOMPUTE_PROPS)
2899 break;
2900 else if (handled == HANDLED_RETURN)
2901 {
2902 /* We still want to show before and after strings from
2903 overlays even if the actual buffer text is replaced. */
2904 if (!handle_overlay_change_p
2905 || it->sp > 1
2906 || !get_overlay_strings_1 (it, 0, 0))
2907 {
2908 if (it->ellipsis_p)
2909 setup_for_ellipsis (it, 0);
2910 /* When handling a display spec, we might load an
2911 empty string. In that case, discard it here. We
2912 used to discard it in handle_single_display_spec,
2913 but that causes get_overlay_strings_1, above, to
2914 ignore overlay strings that we must check. */
2915 if (STRINGP (it->string) && !SCHARS (it->string))
2916 pop_it (it);
2917 return;
2918 }
2919 else if (STRINGP (it->string) && !SCHARS (it->string))
2920 pop_it (it);
2921 else
2922 {
2923 it->ignore_overlay_strings_at_pos_p = 1;
2924 it->string_from_display_prop_p = 0;
2925 handle_overlay_change_p = 0;
2926 }
2927 handled = HANDLED_RECOMPUTE_PROPS;
2928 break;
2929 }
2930 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2931 handle_overlay_change_p = 0;
2932 }
2933
2934 if (handled != HANDLED_RECOMPUTE_PROPS)
2935 {
2936 /* Don't check for overlay strings below when set to deliver
2937 characters from a display vector. */
2938 if (it->method == GET_FROM_DISPLAY_VECTOR)
2939 handle_overlay_change_p = 0;
2940
2941 /* Handle overlay changes.
2942 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2943 if it finds overlays. */
2944 if (handle_overlay_change_p)
2945 handled = handle_overlay_change (it);
2946 }
2947
2948 if (it->ellipsis_p)
2949 {
2950 setup_for_ellipsis (it, 0);
2951 break;
2952 }
2953 }
2954 while (handled == HANDLED_RECOMPUTE_PROPS);
2955
2956 /* Determine where to stop next. */
2957 if (handled == HANDLED_NORMALLY)
2958 compute_stop_pos (it);
2959 }
2960
2961
2962 /* Compute IT->stop_charpos from text property and overlay change
2963 information for IT's current position. */
2964
2965 static void
2966 compute_stop_pos (struct it *it)
2967 {
2968 register INTERVAL iv, next_iv;
2969 Lisp_Object object, limit, position;
2970 EMACS_INT charpos, bytepos;
2971
2972 /* If nowhere else, stop at the end. */
2973 it->stop_charpos = it->end_charpos;
2974
2975 if (STRINGP (it->string))
2976 {
2977 /* Strings are usually short, so don't limit the search for
2978 properties. */
2979 object = it->string;
2980 limit = Qnil;
2981 charpos = IT_STRING_CHARPOS (*it);
2982 bytepos = IT_STRING_BYTEPOS (*it);
2983 }
2984 else
2985 {
2986 EMACS_INT pos;
2987
2988 /* If next overlay change is in front of the current stop pos
2989 (which is IT->end_charpos), stop there. Note: value of
2990 next_overlay_change is point-max if no overlay change
2991 follows. */
2992 charpos = IT_CHARPOS (*it);
2993 bytepos = IT_BYTEPOS (*it);
2994 pos = next_overlay_change (charpos);
2995 if (pos < it->stop_charpos)
2996 it->stop_charpos = pos;
2997
2998 /* If showing the region, we have to stop at the region
2999 start or end because the face might change there. */
3000 if (it->region_beg_charpos > 0)
3001 {
3002 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3003 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3004 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3005 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3006 }
3007
3008 /* Set up variables for computing the stop position from text
3009 property changes. */
3010 XSETBUFFER (object, current_buffer);
3011 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3012 }
3013
3014 /* Get the interval containing IT's position. Value is a null
3015 interval if there isn't such an interval. */
3016 position = make_number (charpos);
3017 iv = validate_interval_range (object, &position, &position, 0);
3018 if (!NULL_INTERVAL_P (iv))
3019 {
3020 Lisp_Object values_here[LAST_PROP_IDX];
3021 struct props *p;
3022
3023 /* Get properties here. */
3024 for (p = it_props; p->handler; ++p)
3025 values_here[p->idx] = textget (iv->plist, *p->name);
3026
3027 /* Look for an interval following iv that has different
3028 properties. */
3029 for (next_iv = next_interval (iv);
3030 (!NULL_INTERVAL_P (next_iv)
3031 && (NILP (limit)
3032 || XFASTINT (limit) > next_iv->position));
3033 next_iv = next_interval (next_iv))
3034 {
3035 for (p = it_props; p->handler; ++p)
3036 {
3037 Lisp_Object new_value;
3038
3039 new_value = textget (next_iv->plist, *p->name);
3040 if (!EQ (values_here[p->idx], new_value))
3041 break;
3042 }
3043
3044 if (p->handler)
3045 break;
3046 }
3047
3048 if (!NULL_INTERVAL_P (next_iv))
3049 {
3050 if (INTEGERP (limit)
3051 && next_iv->position >= XFASTINT (limit))
3052 /* No text property change up to limit. */
3053 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3054 else
3055 /* Text properties change in next_iv. */
3056 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3057 }
3058 }
3059
3060 if (it->cmp_it.id < 0)
3061 {
3062 EMACS_INT stoppos = it->end_charpos;
3063
3064 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3065 stoppos = -1;
3066 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3067 stoppos, it->string);
3068 }
3069
3070 xassert (STRINGP (it->string)
3071 || (it->stop_charpos >= BEGV
3072 && it->stop_charpos >= IT_CHARPOS (*it)));
3073 }
3074
3075
3076 /* Return the position of the next overlay change after POS in
3077 current_buffer. Value is point-max if no overlay change
3078 follows. This is like `next-overlay-change' but doesn't use
3079 xmalloc. */
3080
3081 static EMACS_INT
3082 next_overlay_change (EMACS_INT pos)
3083 {
3084 int noverlays;
3085 EMACS_INT endpos;
3086 Lisp_Object *overlays;
3087 int i;
3088
3089 /* Get all overlays at the given position. */
3090 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3091
3092 /* If any of these overlays ends before endpos,
3093 use its ending point instead. */
3094 for (i = 0; i < noverlays; ++i)
3095 {
3096 Lisp_Object oend;
3097 EMACS_INT oendpos;
3098
3099 oend = OVERLAY_END (overlays[i]);
3100 oendpos = OVERLAY_POSITION (oend);
3101 endpos = min (endpos, oendpos);
3102 }
3103
3104 return endpos;
3105 }
3106
3107 /* Return the character position of a display string at or after
3108 position specified by POSITION. If no display string exists at or
3109 after POSITION, return ZV. A display string is either an overlay
3110 with `display' property whose value is a string, or a `display'
3111 text property whose value is a string. STRING is data about the
3112 string to iterate; if STRING->lstring is nil, we are iterating a
3113 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3114 on a GUI frame. */
3115 EMACS_INT
3116 compute_display_string_pos (struct text_pos *position,
3117 struct bidi_string_data *string, int frame_window_p)
3118 {
3119 /* OBJECT = nil means current buffer. */
3120 Lisp_Object object = (string && string->s) ? string->lstring : Qnil;
3121 Lisp_Object pos, spec;
3122 EMACS_INT eob = STRINGP (object) ? string->schars : ZV;
3123 EMACS_INT begb = STRINGP (object) ? 0 : BEGV;
3124 EMACS_INT bufpos, charpos = CHARPOS (*position);
3125 struct text_pos tpos;
3126
3127 if (charpos >= eob
3128 /* We don't support display properties whose values are strings
3129 that have display string properties. */
3130 || string->from_disp_str
3131 /* C strings cannot have display properties. */
3132 || (string->s && !STRINGP (object)))
3133 return eob;
3134
3135 /* If the character at CHARPOS is where the display string begins,
3136 return CHARPOS. */
3137 pos = make_number (charpos);
3138 if (STRINGP (object))
3139 bufpos = string->bufpos;
3140 else
3141 bufpos = charpos;
3142 tpos = *position;
3143 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3144 && (charpos <= begb
3145 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3146 object),
3147 spec))
3148 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3149 frame_window_p))
3150 return charpos;
3151
3152 /* Look forward for the first character with a `display' property
3153 that will replace the underlying text when displayed. */
3154 do {
3155 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3156 CHARPOS (tpos) = XFASTINT (pos);
3157 if (STRINGP (object))
3158 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3159 else
3160 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3161 if (CHARPOS (tpos) >= eob)
3162 break;
3163 spec = Fget_char_property (pos, Qdisplay, object);
3164 if (!STRINGP (object))
3165 bufpos = CHARPOS (tpos);
3166 } while (NILP (spec)
3167 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3168 frame_window_p));
3169
3170 return CHARPOS (tpos);
3171 }
3172
3173 /* Return the character position of the end of the display string that
3174 started at CHARPOS. A display string is either an overlay with
3175 `display' property whose value is a string or a `display' text
3176 property whose value is a string. */
3177 EMACS_INT
3178 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3179 {
3180 /* OBJECT = nil means current buffer. */
3181 Lisp_Object object = (string && string->s) ? string->lstring : Qnil;
3182 Lisp_Object pos = make_number (charpos);
3183 EMACS_INT eob = STRINGP (object) ? string->schars : ZV;
3184
3185 if (charpos >= eob)
3186 return eob;
3187
3188 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3189 abort ();
3190
3191 /* Look forward for the first character where the `display' property
3192 changes. */
3193 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3194
3195 return XFASTINT (pos);
3196 }
3197
3198
3199 \f
3200 /***********************************************************************
3201 Fontification
3202 ***********************************************************************/
3203
3204 /* Handle changes in the `fontified' property of the current buffer by
3205 calling hook functions from Qfontification_functions to fontify
3206 regions of text. */
3207
3208 static enum prop_handled
3209 handle_fontified_prop (struct it *it)
3210 {
3211 Lisp_Object prop, pos;
3212 enum prop_handled handled = HANDLED_NORMALLY;
3213
3214 if (!NILP (Vmemory_full))
3215 return handled;
3216
3217 /* Get the value of the `fontified' property at IT's current buffer
3218 position. (The `fontified' property doesn't have a special
3219 meaning in strings.) If the value is nil, call functions from
3220 Qfontification_functions. */
3221 if (!STRINGP (it->string)
3222 && it->s == NULL
3223 && !NILP (Vfontification_functions)
3224 && !NILP (Vrun_hooks)
3225 && (pos = make_number (IT_CHARPOS (*it)),
3226 prop = Fget_char_property (pos, Qfontified, Qnil),
3227 /* Ignore the special cased nil value always present at EOB since
3228 no amount of fontifying will be able to change it. */
3229 NILP (prop) && IT_CHARPOS (*it) < Z))
3230 {
3231 int count = SPECPDL_INDEX ();
3232 Lisp_Object val;
3233 struct buffer *obuf = current_buffer;
3234 int begv = BEGV, zv = ZV;
3235 int old_clip_changed = current_buffer->clip_changed;
3236
3237 val = Vfontification_functions;
3238 specbind (Qfontification_functions, Qnil);
3239
3240 xassert (it->end_charpos == ZV);
3241
3242 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3243 safe_call1 (val, pos);
3244 else
3245 {
3246 Lisp_Object fns, fn;
3247 struct gcpro gcpro1, gcpro2;
3248
3249 fns = Qnil;
3250 GCPRO2 (val, fns);
3251
3252 for (; CONSP (val); val = XCDR (val))
3253 {
3254 fn = XCAR (val);
3255
3256 if (EQ (fn, Qt))
3257 {
3258 /* A value of t indicates this hook has a local
3259 binding; it means to run the global binding too.
3260 In a global value, t should not occur. If it
3261 does, we must ignore it to avoid an endless
3262 loop. */
3263 for (fns = Fdefault_value (Qfontification_functions);
3264 CONSP (fns);
3265 fns = XCDR (fns))
3266 {
3267 fn = XCAR (fns);
3268 if (!EQ (fn, Qt))
3269 safe_call1 (fn, pos);
3270 }
3271 }
3272 else
3273 safe_call1 (fn, pos);
3274 }
3275
3276 UNGCPRO;
3277 }
3278
3279 unbind_to (count, Qnil);
3280
3281 /* Fontification functions routinely call `save-restriction'.
3282 Normally, this tags clip_changed, which can confuse redisplay
3283 (see discussion in Bug#6671). Since we don't perform any
3284 special handling of fontification changes in the case where
3285 `save-restriction' isn't called, there's no point doing so in
3286 this case either. So, if the buffer's restrictions are
3287 actually left unchanged, reset clip_changed. */
3288 if (obuf == current_buffer)
3289 {
3290 if (begv == BEGV && zv == ZV)
3291 current_buffer->clip_changed = old_clip_changed;
3292 }
3293 /* There isn't much we can reasonably do to protect against
3294 misbehaving fontification, but here's a fig leaf. */
3295 else if (!NILP (BVAR (obuf, name)))
3296 set_buffer_internal_1 (obuf);
3297
3298 /* The fontification code may have added/removed text.
3299 It could do even a lot worse, but let's at least protect against
3300 the most obvious case where only the text past `pos' gets changed',
3301 as is/was done in grep.el where some escapes sequences are turned
3302 into face properties (bug#7876). */
3303 it->end_charpos = ZV;
3304
3305 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3306 something. This avoids an endless loop if they failed to
3307 fontify the text for which reason ever. */
3308 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3309 handled = HANDLED_RECOMPUTE_PROPS;
3310 }
3311
3312 return handled;
3313 }
3314
3315
3316 \f
3317 /***********************************************************************
3318 Faces
3319 ***********************************************************************/
3320
3321 /* Set up iterator IT from face properties at its current position.
3322 Called from handle_stop. */
3323
3324 static enum prop_handled
3325 handle_face_prop (struct it *it)
3326 {
3327 int new_face_id;
3328 EMACS_INT next_stop;
3329
3330 if (!STRINGP (it->string))
3331 {
3332 new_face_id
3333 = face_at_buffer_position (it->w,
3334 IT_CHARPOS (*it),
3335 it->region_beg_charpos,
3336 it->region_end_charpos,
3337 &next_stop,
3338 (IT_CHARPOS (*it)
3339 + TEXT_PROP_DISTANCE_LIMIT),
3340 0, it->base_face_id);
3341
3342 /* Is this a start of a run of characters with box face?
3343 Caveat: this can be called for a freshly initialized
3344 iterator; face_id is -1 in this case. We know that the new
3345 face will not change until limit, i.e. if the new face has a
3346 box, all characters up to limit will have one. But, as
3347 usual, we don't know whether limit is really the end. */
3348 if (new_face_id != it->face_id)
3349 {
3350 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3351
3352 /* If new face has a box but old face has not, this is
3353 the start of a run of characters with box, i.e. it has
3354 a shadow on the left side. The value of face_id of the
3355 iterator will be -1 if this is the initial call that gets
3356 the face. In this case, we have to look in front of IT's
3357 position and see whether there is a face != new_face_id. */
3358 it->start_of_box_run_p
3359 = (new_face->box != FACE_NO_BOX
3360 && (it->face_id >= 0
3361 || IT_CHARPOS (*it) == BEG
3362 || new_face_id != face_before_it_pos (it)));
3363 it->face_box_p = new_face->box != FACE_NO_BOX;
3364 }
3365 }
3366 else
3367 {
3368 int base_face_id;
3369 EMACS_INT bufpos;
3370 int i;
3371 Lisp_Object from_overlay
3372 = (it->current.overlay_string_index >= 0
3373 ? it->string_overlays[it->current.overlay_string_index]
3374 : Qnil);
3375
3376 /* See if we got to this string directly or indirectly from
3377 an overlay property. That includes the before-string or
3378 after-string of an overlay, strings in display properties
3379 provided by an overlay, their text properties, etc.
3380
3381 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3382 if (! NILP (from_overlay))
3383 for (i = it->sp - 1; i >= 0; i--)
3384 {
3385 if (it->stack[i].current.overlay_string_index >= 0)
3386 from_overlay
3387 = it->string_overlays[it->stack[i].current.overlay_string_index];
3388 else if (! NILP (it->stack[i].from_overlay))
3389 from_overlay = it->stack[i].from_overlay;
3390
3391 if (!NILP (from_overlay))
3392 break;
3393 }
3394
3395 if (! NILP (from_overlay))
3396 {
3397 bufpos = IT_CHARPOS (*it);
3398 /* For a string from an overlay, the base face depends
3399 only on text properties and ignores overlays. */
3400 base_face_id
3401 = face_for_overlay_string (it->w,
3402 IT_CHARPOS (*it),
3403 it->region_beg_charpos,
3404 it->region_end_charpos,
3405 &next_stop,
3406 (IT_CHARPOS (*it)
3407 + TEXT_PROP_DISTANCE_LIMIT),
3408 0,
3409 from_overlay);
3410 }
3411 else
3412 {
3413 bufpos = 0;
3414
3415 /* For strings from a `display' property, use the face at
3416 IT's current buffer position as the base face to merge
3417 with, so that overlay strings appear in the same face as
3418 surrounding text, unless they specify their own
3419 faces. */
3420 base_face_id = underlying_face_id (it);
3421 }
3422
3423 new_face_id = face_at_string_position (it->w,
3424 it->string,
3425 IT_STRING_CHARPOS (*it),
3426 bufpos,
3427 it->region_beg_charpos,
3428 it->region_end_charpos,
3429 &next_stop,
3430 base_face_id, 0);
3431
3432 /* Is this a start of a run of characters with box? Caveat:
3433 this can be called for a freshly allocated iterator; face_id
3434 is -1 is this case. We know that the new face will not
3435 change until the next check pos, i.e. if the new face has a
3436 box, all characters up to that position will have a
3437 box. But, as usual, we don't know whether that position
3438 is really the end. */
3439 if (new_face_id != it->face_id)
3440 {
3441 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3442 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3443
3444 /* If new face has a box but old face hasn't, this is the
3445 start of a run of characters with box, i.e. it has a
3446 shadow on the left side. */
3447 it->start_of_box_run_p
3448 = new_face->box && (old_face == NULL || !old_face->box);
3449 it->face_box_p = new_face->box != FACE_NO_BOX;
3450 }
3451 }
3452
3453 it->face_id = new_face_id;
3454 return HANDLED_NORMALLY;
3455 }
3456
3457
3458 /* Return the ID of the face ``underlying'' IT's current position,
3459 which is in a string. If the iterator is associated with a
3460 buffer, return the face at IT's current buffer position.
3461 Otherwise, use the iterator's base_face_id. */
3462
3463 static int
3464 underlying_face_id (struct it *it)
3465 {
3466 int face_id = it->base_face_id, i;
3467
3468 xassert (STRINGP (it->string));
3469
3470 for (i = it->sp - 1; i >= 0; --i)
3471 if (NILP (it->stack[i].string))
3472 face_id = it->stack[i].face_id;
3473
3474 return face_id;
3475 }
3476
3477
3478 /* Compute the face one character before or after the current position
3479 of IT. BEFORE_P non-zero means get the face in front of IT's
3480 position. Value is the id of the face. */
3481
3482 static int
3483 face_before_or_after_it_pos (struct it *it, int before_p)
3484 {
3485 int face_id, limit;
3486 EMACS_INT next_check_charpos;
3487 struct text_pos pos;
3488
3489 xassert (it->s == NULL);
3490
3491 if (STRINGP (it->string))
3492 {
3493 EMACS_INT bufpos;
3494 int base_face_id;
3495
3496 /* No face change past the end of the string (for the case
3497 we are padding with spaces). No face change before the
3498 string start. */
3499 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3500 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3501 return it->face_id;
3502
3503 /* Set pos to the position before or after IT's current position. */
3504 if (before_p)
3505 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3506 else
3507 /* For composition, we must check the character after the
3508 composition. */
3509 pos = (it->what == IT_COMPOSITION
3510 ? string_pos (IT_STRING_CHARPOS (*it)
3511 + it->cmp_it.nchars, it->string)
3512 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3513
3514 if (it->current.overlay_string_index >= 0)
3515 bufpos = IT_CHARPOS (*it);
3516 else
3517 bufpos = 0;
3518
3519 base_face_id = underlying_face_id (it);
3520
3521 /* Get the face for ASCII, or unibyte. */
3522 face_id = face_at_string_position (it->w,
3523 it->string,
3524 CHARPOS (pos),
3525 bufpos,
3526 it->region_beg_charpos,
3527 it->region_end_charpos,
3528 &next_check_charpos,
3529 base_face_id, 0);
3530
3531 /* Correct the face for charsets different from ASCII. Do it
3532 for the multibyte case only. The face returned above is
3533 suitable for unibyte text if IT->string is unibyte. */
3534 if (STRING_MULTIBYTE (it->string))
3535 {
3536 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3537 int c, len;
3538 struct face *face = FACE_FROM_ID (it->f, face_id);
3539
3540 c = string_char_and_length (p, &len);
3541 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3542 }
3543 }
3544 else
3545 {
3546 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3547 || (IT_CHARPOS (*it) <= BEGV && before_p))
3548 return it->face_id;
3549
3550 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3551 pos = it->current.pos;
3552
3553 if (before_p)
3554 DEC_TEXT_POS (pos, it->multibyte_p);
3555 else
3556 {
3557 if (it->what == IT_COMPOSITION)
3558 /* For composition, we must check the position after the
3559 composition. */
3560 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3561 else
3562 INC_TEXT_POS (pos, it->multibyte_p);
3563 }
3564
3565 /* Determine face for CHARSET_ASCII, or unibyte. */
3566 face_id = face_at_buffer_position (it->w,
3567 CHARPOS (pos),
3568 it->region_beg_charpos,
3569 it->region_end_charpos,
3570 &next_check_charpos,
3571 limit, 0, -1);
3572
3573 /* Correct the face for charsets different from ASCII. Do it
3574 for the multibyte case only. The face returned above is
3575 suitable for unibyte text if current_buffer is unibyte. */
3576 if (it->multibyte_p)
3577 {
3578 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3579 struct face *face = FACE_FROM_ID (it->f, face_id);
3580 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3581 }
3582 }
3583
3584 return face_id;
3585 }
3586
3587
3588 \f
3589 /***********************************************************************
3590 Invisible text
3591 ***********************************************************************/
3592
3593 /* Set up iterator IT from invisible properties at its current
3594 position. Called from handle_stop. */
3595
3596 static enum prop_handled
3597 handle_invisible_prop (struct it *it)
3598 {
3599 enum prop_handled handled = HANDLED_NORMALLY;
3600
3601 if (STRINGP (it->string))
3602 {
3603 Lisp_Object prop, end_charpos, limit, charpos;
3604
3605 /* Get the value of the invisible text property at the
3606 current position. Value will be nil if there is no such
3607 property. */
3608 charpos = make_number (IT_STRING_CHARPOS (*it));
3609 prop = Fget_text_property (charpos, Qinvisible, it->string);
3610
3611 if (!NILP (prop)
3612 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3613 {
3614 EMACS_INT endpos;
3615
3616 handled = HANDLED_RECOMPUTE_PROPS;
3617
3618 /* Get the position at which the next change of the
3619 invisible text property can be found in IT->string.
3620 Value will be nil if the property value is the same for
3621 all the rest of IT->string. */
3622 XSETINT (limit, SCHARS (it->string));
3623 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3624 it->string, limit);
3625
3626 /* Text at current position is invisible. The next
3627 change in the property is at position end_charpos.
3628 Move IT's current position to that position. */
3629 if (INTEGERP (end_charpos)
3630 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3631 {
3632 struct text_pos old;
3633 EMACS_INT oldpos;
3634
3635 old = it->current.string_pos;
3636 oldpos = CHARPOS (old);
3637 if (it->bidi_p)
3638 {
3639 if (it->bidi_it.first_elt
3640 && it->bidi_it.charpos < SCHARS (it->string))
3641 bidi_paragraph_init (it->paragraph_embedding,
3642 &it->bidi_it, 1);
3643 /* Bidi-iterate out of the invisible text. */
3644 do
3645 {
3646 bidi_move_to_visually_next (&it->bidi_it);
3647 }
3648 while (oldpos <= it->bidi_it.charpos
3649 && it->bidi_it.charpos < endpos);
3650
3651 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3652 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3653 if (IT_CHARPOS (*it) >= endpos)
3654 it->prev_stop = endpos;
3655 }
3656 else
3657 {
3658 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3659 compute_string_pos (&it->current.string_pos, old, it->string);
3660 }
3661 }
3662 else
3663 {
3664 /* The rest of the string is invisible. If this is an
3665 overlay string, proceed with the next overlay string
3666 or whatever comes and return a character from there. */
3667 if (it->current.overlay_string_index >= 0)
3668 {
3669 next_overlay_string (it);
3670 /* Don't check for overlay strings when we just
3671 finished processing them. */
3672 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3673 }
3674 else
3675 {
3676 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3677 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3678 }
3679 }
3680 }
3681 }
3682 else
3683 {
3684 int invis_p;
3685 EMACS_INT newpos, next_stop, start_charpos, tem;
3686 Lisp_Object pos, prop, overlay;
3687
3688 /* First of all, is there invisible text at this position? */
3689 tem = start_charpos = IT_CHARPOS (*it);
3690 pos = make_number (tem);
3691 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3692 &overlay);
3693 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3694
3695 /* If we are on invisible text, skip over it. */
3696 if (invis_p && start_charpos < it->end_charpos)
3697 {
3698 /* Record whether we have to display an ellipsis for the
3699 invisible text. */
3700 int display_ellipsis_p = invis_p == 2;
3701
3702 handled = HANDLED_RECOMPUTE_PROPS;
3703
3704 /* Loop skipping over invisible text. The loop is left at
3705 ZV or with IT on the first char being visible again. */
3706 do
3707 {
3708 /* Try to skip some invisible text. Return value is the
3709 position reached which can be equal to where we start
3710 if there is nothing invisible there. This skips both
3711 over invisible text properties and overlays with
3712 invisible property. */
3713 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3714
3715 /* If we skipped nothing at all we weren't at invisible
3716 text in the first place. If everything to the end of
3717 the buffer was skipped, end the loop. */
3718 if (newpos == tem || newpos >= ZV)
3719 invis_p = 0;
3720 else
3721 {
3722 /* We skipped some characters but not necessarily
3723 all there are. Check if we ended up on visible
3724 text. Fget_char_property returns the property of
3725 the char before the given position, i.e. if we
3726 get invis_p = 0, this means that the char at
3727 newpos is visible. */
3728 pos = make_number (newpos);
3729 prop = Fget_char_property (pos, Qinvisible, it->window);
3730 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3731 }
3732
3733 /* If we ended up on invisible text, proceed to
3734 skip starting with next_stop. */
3735 if (invis_p)
3736 tem = next_stop;
3737
3738 /* If there are adjacent invisible texts, don't lose the
3739 second one's ellipsis. */
3740 if (invis_p == 2)
3741 display_ellipsis_p = 1;
3742 }
3743 while (invis_p);
3744
3745 /* The position newpos is now either ZV or on visible text. */
3746 if (it->bidi_p && newpos < ZV)
3747 {
3748 /* With bidi iteration, the region of invisible text
3749 could start and/or end in the middle of a non-base
3750 embedding level. Therefore, we need to skip
3751 invisible text using the bidi iterator, starting at
3752 IT's current position, until we find ourselves
3753 outside the invisible text. Skipping invisible text
3754 _after_ bidi iteration avoids affecting the visual
3755 order of the displayed text when invisible properties
3756 are added or removed. */
3757 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3758 {
3759 /* If we were `reseat'ed to a new paragraph,
3760 determine the paragraph base direction. We need
3761 to do it now because next_element_from_buffer may
3762 not have a chance to do it, if we are going to
3763 skip any text at the beginning, which resets the
3764 FIRST_ELT flag. */
3765 bidi_paragraph_init (it->paragraph_embedding,
3766 &it->bidi_it, 1);
3767 }
3768 do
3769 {
3770 bidi_move_to_visually_next (&it->bidi_it);
3771 }
3772 while (it->stop_charpos <= it->bidi_it.charpos
3773 && it->bidi_it.charpos < newpos);
3774 IT_CHARPOS (*it) = it->bidi_it.charpos;
3775 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3776 /* If we overstepped NEWPOS, record its position in the
3777 iterator, so that we skip invisible text if later the
3778 bidi iteration lands us in the invisible region
3779 again. */
3780 if (IT_CHARPOS (*it) >= newpos)
3781 it->prev_stop = newpos;
3782 }
3783 else
3784 {
3785 IT_CHARPOS (*it) = newpos;
3786 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3787 }
3788
3789 /* If there are before-strings at the start of invisible
3790 text, and the text is invisible because of a text
3791 property, arrange to show before-strings because 20.x did
3792 it that way. (If the text is invisible because of an
3793 overlay property instead of a text property, this is
3794 already handled in the overlay code.) */
3795 if (NILP (overlay)
3796 && get_overlay_strings (it, it->stop_charpos))
3797 {
3798 handled = HANDLED_RECOMPUTE_PROPS;
3799 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3800 }
3801 else if (display_ellipsis_p)
3802 {
3803 /* Make sure that the glyphs of the ellipsis will get
3804 correct `charpos' values. If we would not update
3805 it->position here, the glyphs would belong to the
3806 last visible character _before_ the invisible
3807 text, which confuses `set_cursor_from_row'.
3808
3809 We use the last invisible position instead of the
3810 first because this way the cursor is always drawn on
3811 the first "." of the ellipsis, whenever PT is inside
3812 the invisible text. Otherwise the cursor would be
3813 placed _after_ the ellipsis when the point is after the
3814 first invisible character. */
3815 if (!STRINGP (it->object))
3816 {
3817 it->position.charpos = newpos - 1;
3818 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3819 }
3820 it->ellipsis_p = 1;
3821 /* Let the ellipsis display before
3822 considering any properties of the following char.
3823 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3824 handled = HANDLED_RETURN;
3825 }
3826 }
3827 }
3828
3829 return handled;
3830 }
3831
3832
3833 /* Make iterator IT return `...' next.
3834 Replaces LEN characters from buffer. */
3835
3836 static void
3837 setup_for_ellipsis (struct it *it, int len)
3838 {
3839 /* Use the display table definition for `...'. Invalid glyphs
3840 will be handled by the method returning elements from dpvec. */
3841 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3842 {
3843 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3844 it->dpvec = v->contents;
3845 it->dpend = v->contents + v->header.size;
3846 }
3847 else
3848 {
3849 /* Default `...'. */
3850 it->dpvec = default_invis_vector;
3851 it->dpend = default_invis_vector + 3;
3852 }
3853
3854 it->dpvec_char_len = len;
3855 it->current.dpvec_index = 0;
3856 it->dpvec_face_id = -1;
3857
3858 /* Remember the current face id in case glyphs specify faces.
3859 IT's face is restored in set_iterator_to_next.
3860 saved_face_id was set to preceding char's face in handle_stop. */
3861 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3862 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3863
3864 it->method = GET_FROM_DISPLAY_VECTOR;
3865 it->ellipsis_p = 1;
3866 }
3867
3868
3869 \f
3870 /***********************************************************************
3871 'display' property
3872 ***********************************************************************/
3873
3874 /* Set up iterator IT from `display' property at its current position.
3875 Called from handle_stop.
3876 We return HANDLED_RETURN if some part of the display property
3877 overrides the display of the buffer text itself.
3878 Otherwise we return HANDLED_NORMALLY. */
3879
3880 static enum prop_handled
3881 handle_display_prop (struct it *it)
3882 {
3883 Lisp_Object propval, object, overlay;
3884 struct text_pos *position;
3885 EMACS_INT bufpos;
3886 /* Nonzero if some property replaces the display of the text itself. */
3887 int display_replaced_p = 0;
3888
3889 if (STRINGP (it->string))
3890 {
3891 object = it->string;
3892 position = &it->current.string_pos;
3893 bufpos = CHARPOS (it->current.pos);
3894 }
3895 else
3896 {
3897 XSETWINDOW (object, it->w);
3898 position = &it->current.pos;
3899 bufpos = CHARPOS (*position);
3900 }
3901
3902 /* Reset those iterator values set from display property values. */
3903 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3904 it->space_width = Qnil;
3905 it->font_height = Qnil;
3906 it->voffset = 0;
3907
3908 /* We don't support recursive `display' properties, i.e. string
3909 values that have a string `display' property, that have a string
3910 `display' property etc. */
3911 if (!it->string_from_display_prop_p)
3912 it->area = TEXT_AREA;
3913
3914 propval = get_char_property_and_overlay (make_number (position->charpos),
3915 Qdisplay, object, &overlay);
3916 if (NILP (propval))
3917 return HANDLED_NORMALLY;
3918 /* Now OVERLAY is the overlay that gave us this property, or nil
3919 if it was a text property. */
3920
3921 if (!STRINGP (it->string))
3922 object = it->w->buffer;
3923
3924 display_replaced_p = handle_display_spec (it, propval, object, overlay,
3925 position, bufpos,
3926 FRAME_WINDOW_P (it->f));
3927
3928 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3929 }
3930
3931 /* Subroutine of handle_display_prop. Returns non-zero if the display
3932 specification in SPEC is a replacing specification, i.e. it would
3933 replace the text covered by `display' property with something else,
3934 such as an image or a display string.
3935
3936 See handle_single_display_spec for documentation of arguments.
3937 frame_window_p is non-zero if the window being redisplayed is on a
3938 GUI frame; this argument is used only if IT is NULL, see below.
3939
3940 IT can be NULL, if this is called by the bidi reordering code
3941 through compute_display_string_pos, which see. In that case, this
3942 function only examines SPEC, but does not otherwise "handle" it, in
3943 the sense that it doesn't set up members of IT from the display
3944 spec. */
3945 static int
3946 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3947 Lisp_Object overlay, struct text_pos *position,
3948 EMACS_INT bufpos, int frame_window_p)
3949 {
3950 int replacing_p = 0;
3951
3952 if (CONSP (spec)
3953 /* Simple specerties. */
3954 && !EQ (XCAR (spec), Qimage)
3955 && !EQ (XCAR (spec), Qspace)
3956 && !EQ (XCAR (spec), Qwhen)
3957 && !EQ (XCAR (spec), Qslice)
3958 && !EQ (XCAR (spec), Qspace_width)
3959 && !EQ (XCAR (spec), Qheight)
3960 && !EQ (XCAR (spec), Qraise)
3961 /* Marginal area specifications. */
3962 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
3963 && !EQ (XCAR (spec), Qleft_fringe)
3964 && !EQ (XCAR (spec), Qright_fringe)
3965 && !NILP (XCAR (spec)))
3966 {
3967 for (; CONSP (spec); spec = XCDR (spec))
3968 {
3969 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
3970 position, bufpos, replacing_p,
3971 frame_window_p))
3972 {
3973 replacing_p = 1;
3974 /* If some text in a string is replaced, `position' no
3975 longer points to the position of `object'. */
3976 if (!it || STRINGP (object))
3977 break;
3978 }
3979 }
3980 }
3981 else if (VECTORP (spec))
3982 {
3983 int i;
3984 for (i = 0; i < ASIZE (spec); ++i)
3985 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
3986 position, bufpos, replacing_p,
3987 frame_window_p))
3988 {
3989 replacing_p = 1;
3990 /* If some text in a string is replaced, `position' no
3991 longer points to the position of `object'. */
3992 if (!it || STRINGP (object))
3993 break;
3994 }
3995 }
3996 else
3997 {
3998 if (handle_single_display_spec (it, spec, object, overlay,
3999 position, bufpos, 0, frame_window_p))
4000 replacing_p = 1;
4001 }
4002
4003 return replacing_p;
4004 }
4005
4006 /* Value is the position of the end of the `display' property starting
4007 at START_POS in OBJECT. */
4008
4009 static struct text_pos
4010 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4011 {
4012 Lisp_Object end;
4013 struct text_pos end_pos;
4014
4015 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4016 Qdisplay, object, Qnil);
4017 CHARPOS (end_pos) = XFASTINT (end);
4018 if (STRINGP (object))
4019 compute_string_pos (&end_pos, start_pos, it->string);
4020 else
4021 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4022
4023 return end_pos;
4024 }
4025
4026
4027 /* Set up IT from a single `display' property specification SPEC. OBJECT
4028 is the object in which the `display' property was found. *POSITION
4029 is the position in OBJECT at which the `display' property was found.
4030 BUFPOS is the buffer position of OBJECT (different from POSITION if
4031 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4032 previously saw a display specification which already replaced text
4033 display with something else, for example an image; we ignore such
4034 properties after the first one has been processed.
4035
4036 OVERLAY is the overlay this `display' property came from,
4037 or nil if it was a text property.
4038
4039 If SPEC is a `space' or `image' specification, and in some other
4040 cases too, set *POSITION to the position where the `display'
4041 property ends.
4042
4043 If IT is NULL, only examine the property specification in SPEC, but
4044 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4045 is intended to be displayed in a window on a GUI frame.
4046
4047 Value is non-zero if something was found which replaces the display
4048 of buffer or string text. */
4049
4050 static int
4051 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4052 Lisp_Object overlay, struct text_pos *position,
4053 EMACS_INT bufpos, int display_replaced_p,
4054 int frame_window_p)
4055 {
4056 Lisp_Object form;
4057 Lisp_Object location, value;
4058 struct text_pos start_pos = *position;
4059 int valid_p;
4060
4061 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4062 If the result is non-nil, use VALUE instead of SPEC. */
4063 form = Qt;
4064 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4065 {
4066 spec = XCDR (spec);
4067 if (!CONSP (spec))
4068 return 0;
4069 form = XCAR (spec);
4070 spec = XCDR (spec);
4071 }
4072
4073 if (!NILP (form) && !EQ (form, Qt))
4074 {
4075 int count = SPECPDL_INDEX ();
4076 struct gcpro gcpro1;
4077
4078 /* Bind `object' to the object having the `display' property, a
4079 buffer or string. Bind `position' to the position in the
4080 object where the property was found, and `buffer-position'
4081 to the current position in the buffer. */
4082
4083 if (NILP (object))
4084 XSETBUFFER (object, current_buffer);
4085 specbind (Qobject, object);
4086 specbind (Qposition, make_number (CHARPOS (*position)));
4087 specbind (Qbuffer_position, make_number (bufpos));
4088 GCPRO1 (form);
4089 form = safe_eval (form);
4090 UNGCPRO;
4091 unbind_to (count, Qnil);
4092 }
4093
4094 if (NILP (form))
4095 return 0;
4096
4097 /* Handle `(height HEIGHT)' specifications. */
4098 if (CONSP (spec)
4099 && EQ (XCAR (spec), Qheight)
4100 && CONSP (XCDR (spec)))
4101 {
4102 if (it)
4103 {
4104 if (!FRAME_WINDOW_P (it->f))
4105 return 0;
4106
4107 it->font_height = XCAR (XCDR (spec));
4108 if (!NILP (it->font_height))
4109 {
4110 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4111 int new_height = -1;
4112
4113 if (CONSP (it->font_height)
4114 && (EQ (XCAR (it->font_height), Qplus)
4115 || EQ (XCAR (it->font_height), Qminus))
4116 && CONSP (XCDR (it->font_height))
4117 && INTEGERP (XCAR (XCDR (it->font_height))))
4118 {
4119 /* `(+ N)' or `(- N)' where N is an integer. */
4120 int steps = XINT (XCAR (XCDR (it->font_height)));
4121 if (EQ (XCAR (it->font_height), Qplus))
4122 steps = - steps;
4123 it->face_id = smaller_face (it->f, it->face_id, steps);
4124 }
4125 else if (FUNCTIONP (it->font_height))
4126 {
4127 /* Call function with current height as argument.
4128 Value is the new height. */
4129 Lisp_Object height;
4130 height = safe_call1 (it->font_height,
4131 face->lface[LFACE_HEIGHT_INDEX]);
4132 if (NUMBERP (height))
4133 new_height = XFLOATINT (height);
4134 }
4135 else if (NUMBERP (it->font_height))
4136 {
4137 /* Value is a multiple of the canonical char height. */
4138 struct face *f;
4139
4140 f = FACE_FROM_ID (it->f,
4141 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4142 new_height = (XFLOATINT (it->font_height)
4143 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4144 }
4145 else
4146 {
4147 /* Evaluate IT->font_height with `height' bound to the
4148 current specified height to get the new height. */
4149 int count = SPECPDL_INDEX ();
4150
4151 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4152 value = safe_eval (it->font_height);
4153 unbind_to (count, Qnil);
4154
4155 if (NUMBERP (value))
4156 new_height = XFLOATINT (value);
4157 }
4158
4159 if (new_height > 0)
4160 it->face_id = face_with_height (it->f, it->face_id, new_height);
4161 }
4162 }
4163
4164 return 0;
4165 }
4166
4167 /* Handle `(space-width WIDTH)'. */
4168 if (CONSP (spec)
4169 && EQ (XCAR (spec), Qspace_width)
4170 && CONSP (XCDR (spec)))
4171 {
4172 if (it)
4173 {
4174 if (!FRAME_WINDOW_P (it->f))
4175 return 0;
4176
4177 value = XCAR (XCDR (spec));
4178 if (NUMBERP (value) && XFLOATINT (value) > 0)
4179 it->space_width = value;
4180 }
4181
4182 return 0;
4183 }
4184
4185 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4186 if (CONSP (spec)
4187 && EQ (XCAR (spec), Qslice))
4188 {
4189 Lisp_Object tem;
4190
4191 if (it)
4192 {
4193 if (!FRAME_WINDOW_P (it->f))
4194 return 0;
4195
4196 if (tem = XCDR (spec), CONSP (tem))
4197 {
4198 it->slice.x = XCAR (tem);
4199 if (tem = XCDR (tem), CONSP (tem))
4200 {
4201 it->slice.y = XCAR (tem);
4202 if (tem = XCDR (tem), CONSP (tem))
4203 {
4204 it->slice.width = XCAR (tem);
4205 if (tem = XCDR (tem), CONSP (tem))
4206 it->slice.height = XCAR (tem);
4207 }
4208 }
4209 }
4210 }
4211
4212 return 0;
4213 }
4214
4215 /* Handle `(raise FACTOR)'. */
4216 if (CONSP (spec)
4217 && EQ (XCAR (spec), Qraise)
4218 && CONSP (XCDR (spec)))
4219 {
4220 if (it)
4221 {
4222 if (!FRAME_WINDOW_P (it->f))
4223 return 0;
4224
4225 #ifdef HAVE_WINDOW_SYSTEM
4226 value = XCAR (XCDR (spec));
4227 if (NUMBERP (value))
4228 {
4229 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4230 it->voffset = - (XFLOATINT (value)
4231 * (FONT_HEIGHT (face->font)));
4232 }
4233 #endif /* HAVE_WINDOW_SYSTEM */
4234 }
4235
4236 return 0;
4237 }
4238
4239 /* Don't handle the other kinds of display specifications
4240 inside a string that we got from a `display' property. */
4241 if (it && it->string_from_display_prop_p)
4242 return 0;
4243
4244 /* Characters having this form of property are not displayed, so
4245 we have to find the end of the property. */
4246 if (it)
4247 {
4248 start_pos = *position;
4249 *position = display_prop_end (it, object, start_pos);
4250 }
4251 value = Qnil;
4252
4253 /* Stop the scan at that end position--we assume that all
4254 text properties change there. */
4255 if (it)
4256 it->stop_charpos = position->charpos;
4257
4258 /* Handle `(left-fringe BITMAP [FACE])'
4259 and `(right-fringe BITMAP [FACE])'. */
4260 if (CONSP (spec)
4261 && (EQ (XCAR (spec), Qleft_fringe)
4262 || EQ (XCAR (spec), Qright_fringe))
4263 && CONSP (XCDR (spec)))
4264 {
4265 int fringe_bitmap;
4266
4267 if (it)
4268 {
4269 if (!FRAME_WINDOW_P (it->f))
4270 /* If we return here, POSITION has been advanced
4271 across the text with this property. */
4272 return 0;
4273 }
4274 else if (!frame_window_p)
4275 return 0;
4276
4277 #ifdef HAVE_WINDOW_SYSTEM
4278 value = XCAR (XCDR (spec));
4279 if (!SYMBOLP (value)
4280 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4281 /* If we return here, POSITION has been advanced
4282 across the text with this property. */
4283 return 0;
4284
4285 if (it)
4286 {
4287 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4288
4289 if (CONSP (XCDR (XCDR (spec))))
4290 {
4291 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4292 int face_id2 = lookup_derived_face (it->f, face_name,
4293 FRINGE_FACE_ID, 0);
4294 if (face_id2 >= 0)
4295 face_id = face_id2;
4296 }
4297
4298 /* Save current settings of IT so that we can restore them
4299 when we are finished with the glyph property value. */
4300 push_it (it, position);
4301
4302 it->area = TEXT_AREA;
4303 it->what = IT_IMAGE;
4304 it->image_id = -1; /* no image */
4305 it->position = start_pos;
4306 it->object = NILP (object) ? it->w->buffer : object;
4307 it->method = GET_FROM_IMAGE;
4308 it->from_overlay = Qnil;
4309 it->face_id = face_id;
4310
4311 /* Say that we haven't consumed the characters with
4312 `display' property yet. The call to pop_it in
4313 set_iterator_to_next will clean this up. */
4314 *position = start_pos;
4315
4316 if (EQ (XCAR (spec), Qleft_fringe))
4317 {
4318 it->left_user_fringe_bitmap = fringe_bitmap;
4319 it->left_user_fringe_face_id = face_id;
4320 }
4321 else
4322 {
4323 it->right_user_fringe_bitmap = fringe_bitmap;
4324 it->right_user_fringe_face_id = face_id;
4325 }
4326 }
4327 #endif /* HAVE_WINDOW_SYSTEM */
4328 return 1;
4329 }
4330
4331 /* Prepare to handle `((margin left-margin) ...)',
4332 `((margin right-margin) ...)' and `((margin nil) ...)'
4333 prefixes for display specifications. */
4334 location = Qunbound;
4335 if (CONSP (spec) && CONSP (XCAR (spec)))
4336 {
4337 Lisp_Object tem;
4338
4339 value = XCDR (spec);
4340 if (CONSP (value))
4341 value = XCAR (value);
4342
4343 tem = XCAR (spec);
4344 if (EQ (XCAR (tem), Qmargin)
4345 && (tem = XCDR (tem),
4346 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4347 (NILP (tem)
4348 || EQ (tem, Qleft_margin)
4349 || EQ (tem, Qright_margin))))
4350 location = tem;
4351 }
4352
4353 if (EQ (location, Qunbound))
4354 {
4355 location = Qnil;
4356 value = spec;
4357 }
4358
4359 /* After this point, VALUE is the property after any
4360 margin prefix has been stripped. It must be a string,
4361 an image specification, or `(space ...)'.
4362
4363 LOCATION specifies where to display: `left-margin',
4364 `right-margin' or nil. */
4365
4366 valid_p = (STRINGP (value)
4367 #ifdef HAVE_WINDOW_SYSTEM
4368 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4369 && valid_image_p (value))
4370 #endif /* not HAVE_WINDOW_SYSTEM */
4371 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4372
4373 if (valid_p && !display_replaced_p)
4374 {
4375 if (!it)
4376 return 1;
4377
4378 /* Save current settings of IT so that we can restore them
4379 when we are finished with the glyph property value. */
4380 push_it (it, position);
4381 it->from_overlay = overlay;
4382
4383 if (NILP (location))
4384 it->area = TEXT_AREA;
4385 else if (EQ (location, Qleft_margin))
4386 it->area = LEFT_MARGIN_AREA;
4387 else
4388 it->area = RIGHT_MARGIN_AREA;
4389
4390 if (STRINGP (value))
4391 {
4392 it->string = value;
4393 it->multibyte_p = STRING_MULTIBYTE (it->string);
4394 it->current.overlay_string_index = -1;
4395 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4396 it->end_charpos = it->string_nchars = SCHARS (it->string);
4397 it->method = GET_FROM_STRING;
4398 it->stop_charpos = 0;
4399 it->string_from_display_prop_p = 1;
4400 /* Say that we haven't consumed the characters with
4401 `display' property yet. The call to pop_it in
4402 set_iterator_to_next will clean this up. */
4403 if (BUFFERP (object))
4404 *position = start_pos;
4405 }
4406 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4407 {
4408 it->method = GET_FROM_STRETCH;
4409 it->object = value;
4410 *position = it->position = start_pos;
4411 }
4412 #ifdef HAVE_WINDOW_SYSTEM
4413 else
4414 {
4415 it->what = IT_IMAGE;
4416 it->image_id = lookup_image (it->f, value);
4417 it->position = start_pos;
4418 it->object = NILP (object) ? it->w->buffer : object;
4419 it->method = GET_FROM_IMAGE;
4420
4421 /* Say that we haven't consumed the characters with
4422 `display' property yet. The call to pop_it in
4423 set_iterator_to_next will clean this up. */
4424 *position = start_pos;
4425 }
4426 #endif /* HAVE_WINDOW_SYSTEM */
4427
4428 return 1;
4429 }
4430
4431 /* Invalid property or property not supported. Restore
4432 POSITION to what it was before. */
4433 *position = start_pos;
4434 return 0;
4435 }
4436
4437 /* Check if PROP is a display property value whose text should be
4438 treated as intangible. OVERLAY is the overlay from which PROP
4439 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4440 specify the buffer position covered by PROP. */
4441
4442 int
4443 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4444 EMACS_INT charpos, EMACS_INT bytepos)
4445 {
4446 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4447 struct text_pos position;
4448
4449 SET_TEXT_POS (position, charpos, bytepos);
4450 return handle_display_spec (NULL, prop, Qnil, overlay,
4451 &position, charpos, frame_window_p);
4452 }
4453
4454
4455 /* Return 1 if PROP is a display sub-property value containing STRING.
4456
4457 Implementation note: this and the following function are really
4458 special cases of handle_display_spec and
4459 handle_single_display_spec, and should ideally use the same code.
4460 Until they do, these two pairs must be consistent and must be
4461 modified in sync. */
4462
4463 static int
4464 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4465 {
4466 if (EQ (string, prop))
4467 return 1;
4468
4469 /* Skip over `when FORM'. */
4470 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4471 {
4472 prop = XCDR (prop);
4473 if (!CONSP (prop))
4474 return 0;
4475 /* Actually, the condition following `when' should be eval'ed,
4476 like handle_single_display_spec does, and we should return
4477 zero if it evaluates to nil. However, this function is
4478 called only when the buffer was already displayed and some
4479 glyph in the glyph matrix was found to come from a display
4480 string. Therefore, the condition was already evaluated, and
4481 the result was non-nil, otherwise the display string wouldn't
4482 have been displayed and we would have never been called for
4483 this property. Thus, we can skip the evaluation and assume
4484 its result is non-nil. */
4485 prop = XCDR (prop);
4486 }
4487
4488 if (CONSP (prop))
4489 /* Skip over `margin LOCATION'. */
4490 if (EQ (XCAR (prop), Qmargin))
4491 {
4492 prop = XCDR (prop);
4493 if (!CONSP (prop))
4494 return 0;
4495
4496 prop = XCDR (prop);
4497 if (!CONSP (prop))
4498 return 0;
4499 }
4500
4501 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4502 }
4503
4504
4505 /* Return 1 if STRING appears in the `display' property PROP. */
4506
4507 static int
4508 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4509 {
4510 if (CONSP (prop)
4511 && !EQ (XCAR (prop), Qwhen)
4512 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4513 {
4514 /* A list of sub-properties. */
4515 while (CONSP (prop))
4516 {
4517 if (single_display_spec_string_p (XCAR (prop), string))
4518 return 1;
4519 prop = XCDR (prop);
4520 }
4521 }
4522 else if (VECTORP (prop))
4523 {
4524 /* A vector of sub-properties. */
4525 int i;
4526 for (i = 0; i < ASIZE (prop); ++i)
4527 if (single_display_spec_string_p (AREF (prop, i), string))
4528 return 1;
4529 }
4530 else
4531 return single_display_spec_string_p (prop, string);
4532
4533 return 0;
4534 }
4535
4536 /* Look for STRING in overlays and text properties in the current
4537 buffer, between character positions FROM and TO (excluding TO).
4538 BACK_P non-zero means look back (in this case, TO is supposed to be
4539 less than FROM).
4540 Value is the first character position where STRING was found, or
4541 zero if it wasn't found before hitting TO.
4542
4543 This function may only use code that doesn't eval because it is
4544 called asynchronously from note_mouse_highlight. */
4545
4546 static EMACS_INT
4547 string_buffer_position_lim (Lisp_Object string,
4548 EMACS_INT from, EMACS_INT to, int back_p)
4549 {
4550 Lisp_Object limit, prop, pos;
4551 int found = 0;
4552
4553 pos = make_number (from);
4554
4555 if (!back_p) /* looking forward */
4556 {
4557 limit = make_number (min (to, ZV));
4558 while (!found && !EQ (pos, limit))
4559 {
4560 prop = Fget_char_property (pos, Qdisplay, Qnil);
4561 if (!NILP (prop) && display_prop_string_p (prop, string))
4562 found = 1;
4563 else
4564 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4565 limit);
4566 }
4567 }
4568 else /* looking back */
4569 {
4570 limit = make_number (max (to, BEGV));
4571 while (!found && !EQ (pos, limit))
4572 {
4573 prop = Fget_char_property (pos, Qdisplay, Qnil);
4574 if (!NILP (prop) && display_prop_string_p (prop, string))
4575 found = 1;
4576 else
4577 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4578 limit);
4579 }
4580 }
4581
4582 return found ? XINT (pos) : 0;
4583 }
4584
4585 /* Determine which buffer position in current buffer STRING comes from.
4586 AROUND_CHARPOS is an approximate position where it could come from.
4587 Value is the buffer position or 0 if it couldn't be determined.
4588
4589 This function is necessary because we don't record buffer positions
4590 in glyphs generated from strings (to keep struct glyph small).
4591 This function may only use code that doesn't eval because it is
4592 called asynchronously from note_mouse_highlight. */
4593
4594 static EMACS_INT
4595 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4596 {
4597 const int MAX_DISTANCE = 1000;
4598 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4599 around_charpos + MAX_DISTANCE,
4600 0);
4601
4602 if (!found)
4603 found = string_buffer_position_lim (string, around_charpos,
4604 around_charpos - MAX_DISTANCE, 1);
4605 return found;
4606 }
4607
4608
4609 \f
4610 /***********************************************************************
4611 `composition' property
4612 ***********************************************************************/
4613
4614 /* Set up iterator IT from `composition' property at its current
4615 position. Called from handle_stop. */
4616
4617 static enum prop_handled
4618 handle_composition_prop (struct it *it)
4619 {
4620 Lisp_Object prop, string;
4621 EMACS_INT pos, pos_byte, start, end;
4622
4623 if (STRINGP (it->string))
4624 {
4625 unsigned char *s;
4626
4627 pos = IT_STRING_CHARPOS (*it);
4628 pos_byte = IT_STRING_BYTEPOS (*it);
4629 string = it->string;
4630 s = SDATA (string) + pos_byte;
4631 it->c = STRING_CHAR (s);
4632 }
4633 else
4634 {
4635 pos = IT_CHARPOS (*it);
4636 pos_byte = IT_BYTEPOS (*it);
4637 string = Qnil;
4638 it->c = FETCH_CHAR (pos_byte);
4639 }
4640
4641 /* If there's a valid composition and point is not inside of the
4642 composition (in the case that the composition is from the current
4643 buffer), draw a glyph composed from the composition components. */
4644 if (find_composition (pos, -1, &start, &end, &prop, string)
4645 && COMPOSITION_VALID_P (start, end, prop)
4646 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4647 {
4648 if (start != pos)
4649 {
4650 if (STRINGP (it->string))
4651 pos_byte = string_char_to_byte (it->string, start);
4652 else
4653 pos_byte = CHAR_TO_BYTE (start);
4654 }
4655 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4656 prop, string);
4657
4658 if (it->cmp_it.id >= 0)
4659 {
4660 it->cmp_it.ch = -1;
4661 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4662 it->cmp_it.nglyphs = -1;
4663 }
4664 }
4665
4666 return HANDLED_NORMALLY;
4667 }
4668
4669
4670 \f
4671 /***********************************************************************
4672 Overlay strings
4673 ***********************************************************************/
4674
4675 /* The following structure is used to record overlay strings for
4676 later sorting in load_overlay_strings. */
4677
4678 struct overlay_entry
4679 {
4680 Lisp_Object overlay;
4681 Lisp_Object string;
4682 int priority;
4683 int after_string_p;
4684 };
4685
4686
4687 /* Set up iterator IT from overlay strings at its current position.
4688 Called from handle_stop. */
4689
4690 static enum prop_handled
4691 handle_overlay_change (struct it *it)
4692 {
4693 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4694 return HANDLED_RECOMPUTE_PROPS;
4695 else
4696 return HANDLED_NORMALLY;
4697 }
4698
4699
4700 /* Set up the next overlay string for delivery by IT, if there is an
4701 overlay string to deliver. Called by set_iterator_to_next when the
4702 end of the current overlay string is reached. If there are more
4703 overlay strings to display, IT->string and
4704 IT->current.overlay_string_index are set appropriately here.
4705 Otherwise IT->string is set to nil. */
4706
4707 static void
4708 next_overlay_string (struct it *it)
4709 {
4710 ++it->current.overlay_string_index;
4711 if (it->current.overlay_string_index == it->n_overlay_strings)
4712 {
4713 /* No more overlay strings. Restore IT's settings to what
4714 they were before overlay strings were processed, and
4715 continue to deliver from current_buffer. */
4716
4717 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4718 pop_it (it);
4719 xassert (it->sp > 0
4720 || (NILP (it->string)
4721 && it->method == GET_FROM_BUFFER
4722 && it->stop_charpos >= BEGV
4723 && it->stop_charpos <= it->end_charpos));
4724 it->current.overlay_string_index = -1;
4725 it->n_overlay_strings = 0;
4726 it->overlay_strings_charpos = -1;
4727
4728 /* If we're at the end of the buffer, record that we have
4729 processed the overlay strings there already, so that
4730 next_element_from_buffer doesn't try it again. */
4731 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4732 it->overlay_strings_at_end_processed_p = 1;
4733 }
4734 else
4735 {
4736 /* There are more overlay strings to process. If
4737 IT->current.overlay_string_index has advanced to a position
4738 where we must load IT->overlay_strings with more strings, do
4739 it. We must load at the IT->overlay_strings_charpos where
4740 IT->n_overlay_strings was originally computed; when invisible
4741 text is present, this might not be IT_CHARPOS (Bug#7016). */
4742 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4743
4744 if (it->current.overlay_string_index && i == 0)
4745 load_overlay_strings (it, it->overlay_strings_charpos);
4746
4747 /* Initialize IT to deliver display elements from the overlay
4748 string. */
4749 it->string = it->overlay_strings[i];
4750 it->multibyte_p = STRING_MULTIBYTE (it->string);
4751 SET_TEXT_POS (it->current.string_pos, 0, 0);
4752 it->method = GET_FROM_STRING;
4753 it->stop_charpos = 0;
4754 if (it->cmp_it.stop_pos >= 0)
4755 it->cmp_it.stop_pos = 0;
4756 }
4757
4758 CHECK_IT (it);
4759 }
4760
4761
4762 /* Compare two overlay_entry structures E1 and E2. Used as a
4763 comparison function for qsort in load_overlay_strings. Overlay
4764 strings for the same position are sorted so that
4765
4766 1. All after-strings come in front of before-strings, except
4767 when they come from the same overlay.
4768
4769 2. Within after-strings, strings are sorted so that overlay strings
4770 from overlays with higher priorities come first.
4771
4772 2. Within before-strings, strings are sorted so that overlay
4773 strings from overlays with higher priorities come last.
4774
4775 Value is analogous to strcmp. */
4776
4777
4778 static int
4779 compare_overlay_entries (const void *e1, const void *e2)
4780 {
4781 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4782 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4783 int result;
4784
4785 if (entry1->after_string_p != entry2->after_string_p)
4786 {
4787 /* Let after-strings appear in front of before-strings if
4788 they come from different overlays. */
4789 if (EQ (entry1->overlay, entry2->overlay))
4790 result = entry1->after_string_p ? 1 : -1;
4791 else
4792 result = entry1->after_string_p ? -1 : 1;
4793 }
4794 else if (entry1->after_string_p)
4795 /* After-strings sorted in order of decreasing priority. */
4796 result = entry2->priority - entry1->priority;
4797 else
4798 /* Before-strings sorted in order of increasing priority. */
4799 result = entry1->priority - entry2->priority;
4800
4801 return result;
4802 }
4803
4804
4805 /* Load the vector IT->overlay_strings with overlay strings from IT's
4806 current buffer position, or from CHARPOS if that is > 0. Set
4807 IT->n_overlays to the total number of overlay strings found.
4808
4809 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4810 a time. On entry into load_overlay_strings,
4811 IT->current.overlay_string_index gives the number of overlay
4812 strings that have already been loaded by previous calls to this
4813 function.
4814
4815 IT->add_overlay_start contains an additional overlay start
4816 position to consider for taking overlay strings from, if non-zero.
4817 This position comes into play when the overlay has an `invisible'
4818 property, and both before and after-strings. When we've skipped to
4819 the end of the overlay, because of its `invisible' property, we
4820 nevertheless want its before-string to appear.
4821 IT->add_overlay_start will contain the overlay start position
4822 in this case.
4823
4824 Overlay strings are sorted so that after-string strings come in
4825 front of before-string strings. Within before and after-strings,
4826 strings are sorted by overlay priority. See also function
4827 compare_overlay_entries. */
4828
4829 static void
4830 load_overlay_strings (struct it *it, EMACS_INT charpos)
4831 {
4832 Lisp_Object overlay, window, str, invisible;
4833 struct Lisp_Overlay *ov;
4834 EMACS_INT start, end;
4835 int size = 20;
4836 int n = 0, i, j, invis_p;
4837 struct overlay_entry *entries
4838 = (struct overlay_entry *) alloca (size * sizeof *entries);
4839
4840 if (charpos <= 0)
4841 charpos = IT_CHARPOS (*it);
4842
4843 /* Append the overlay string STRING of overlay OVERLAY to vector
4844 `entries' which has size `size' and currently contains `n'
4845 elements. AFTER_P non-zero means STRING is an after-string of
4846 OVERLAY. */
4847 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4848 do \
4849 { \
4850 Lisp_Object priority; \
4851 \
4852 if (n == size) \
4853 { \
4854 int new_size = 2 * size; \
4855 struct overlay_entry *old = entries; \
4856 entries = \
4857 (struct overlay_entry *) alloca (new_size \
4858 * sizeof *entries); \
4859 memcpy (entries, old, size * sizeof *entries); \
4860 size = new_size; \
4861 } \
4862 \
4863 entries[n].string = (STRING); \
4864 entries[n].overlay = (OVERLAY); \
4865 priority = Foverlay_get ((OVERLAY), Qpriority); \
4866 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4867 entries[n].after_string_p = (AFTER_P); \
4868 ++n; \
4869 } \
4870 while (0)
4871
4872 /* Process overlay before the overlay center. */
4873 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4874 {
4875 XSETMISC (overlay, ov);
4876 xassert (OVERLAYP (overlay));
4877 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4878 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4879
4880 if (end < charpos)
4881 break;
4882
4883 /* Skip this overlay if it doesn't start or end at IT's current
4884 position. */
4885 if (end != charpos && start != charpos)
4886 continue;
4887
4888 /* Skip this overlay if it doesn't apply to IT->w. */
4889 window = Foverlay_get (overlay, Qwindow);
4890 if (WINDOWP (window) && XWINDOW (window) != it->w)
4891 continue;
4892
4893 /* If the text ``under'' the overlay is invisible, both before-
4894 and after-strings from this overlay are visible; start and
4895 end position are indistinguishable. */
4896 invisible = Foverlay_get (overlay, Qinvisible);
4897 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4898
4899 /* If overlay has a non-empty before-string, record it. */
4900 if ((start == charpos || (end == charpos && invis_p))
4901 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4902 && SCHARS (str))
4903 RECORD_OVERLAY_STRING (overlay, str, 0);
4904
4905 /* If overlay has a non-empty after-string, record it. */
4906 if ((end == charpos || (start == charpos && invis_p))
4907 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4908 && SCHARS (str))
4909 RECORD_OVERLAY_STRING (overlay, str, 1);
4910 }
4911
4912 /* Process overlays after the overlay center. */
4913 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4914 {
4915 XSETMISC (overlay, ov);
4916 xassert (OVERLAYP (overlay));
4917 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4918 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4919
4920 if (start > charpos)
4921 break;
4922
4923 /* Skip this overlay if it doesn't start or end at IT's current
4924 position. */
4925 if (end != charpos && start != charpos)
4926 continue;
4927
4928 /* Skip this overlay if it doesn't apply to IT->w. */
4929 window = Foverlay_get (overlay, Qwindow);
4930 if (WINDOWP (window) && XWINDOW (window) != it->w)
4931 continue;
4932
4933 /* If the text ``under'' the overlay is invisible, it has a zero
4934 dimension, and both before- and after-strings apply. */
4935 invisible = Foverlay_get (overlay, Qinvisible);
4936 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4937
4938 /* If overlay has a non-empty before-string, record it. */
4939 if ((start == charpos || (end == charpos && invis_p))
4940 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4941 && SCHARS (str))
4942 RECORD_OVERLAY_STRING (overlay, str, 0);
4943
4944 /* If overlay has a non-empty after-string, record it. */
4945 if ((end == charpos || (start == charpos && invis_p))
4946 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4947 && SCHARS (str))
4948 RECORD_OVERLAY_STRING (overlay, str, 1);
4949 }
4950
4951 #undef RECORD_OVERLAY_STRING
4952
4953 /* Sort entries. */
4954 if (n > 1)
4955 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4956
4957 /* Record number of overlay strings, and where we computed it. */
4958 it->n_overlay_strings = n;
4959 it->overlay_strings_charpos = charpos;
4960
4961 /* IT->current.overlay_string_index is the number of overlay strings
4962 that have already been consumed by IT. Copy some of the
4963 remaining overlay strings to IT->overlay_strings. */
4964 i = 0;
4965 j = it->current.overlay_string_index;
4966 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4967 {
4968 it->overlay_strings[i] = entries[j].string;
4969 it->string_overlays[i++] = entries[j++].overlay;
4970 }
4971
4972 CHECK_IT (it);
4973 }
4974
4975
4976 /* Get the first chunk of overlay strings at IT's current buffer
4977 position, or at CHARPOS if that is > 0. Value is non-zero if at
4978 least one overlay string was found. */
4979
4980 static int
4981 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
4982 {
4983 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4984 process. This fills IT->overlay_strings with strings, and sets
4985 IT->n_overlay_strings to the total number of strings to process.
4986 IT->pos.overlay_string_index has to be set temporarily to zero
4987 because load_overlay_strings needs this; it must be set to -1
4988 when no overlay strings are found because a zero value would
4989 indicate a position in the first overlay string. */
4990 it->current.overlay_string_index = 0;
4991 load_overlay_strings (it, charpos);
4992
4993 /* If we found overlay strings, set up IT to deliver display
4994 elements from the first one. Otherwise set up IT to deliver
4995 from current_buffer. */
4996 if (it->n_overlay_strings)
4997 {
4998 /* Make sure we know settings in current_buffer, so that we can
4999 restore meaningful values when we're done with the overlay
5000 strings. */
5001 if (compute_stop_p)
5002 compute_stop_pos (it);
5003 xassert (it->face_id >= 0);
5004
5005 /* Save IT's settings. They are restored after all overlay
5006 strings have been processed. */
5007 xassert (!compute_stop_p || it->sp == 0);
5008
5009 /* When called from handle_stop, there might be an empty display
5010 string loaded. In that case, don't bother saving it. */
5011 if (!STRINGP (it->string) || SCHARS (it->string))
5012 push_it (it, NULL);
5013
5014 /* Set up IT to deliver display elements from the first overlay
5015 string. */
5016 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5017 it->string = it->overlay_strings[0];
5018 it->from_overlay = Qnil;
5019 it->stop_charpos = 0;
5020 xassert (STRINGP (it->string));
5021 it->end_charpos = SCHARS (it->string);
5022 it->multibyte_p = STRING_MULTIBYTE (it->string);
5023 it->method = GET_FROM_STRING;
5024 return 1;
5025 }
5026
5027 it->current.overlay_string_index = -1;
5028 return 0;
5029 }
5030
5031 static int
5032 get_overlay_strings (struct it *it, EMACS_INT charpos)
5033 {
5034 it->string = Qnil;
5035 it->method = GET_FROM_BUFFER;
5036
5037 (void) get_overlay_strings_1 (it, charpos, 1);
5038
5039 CHECK_IT (it);
5040
5041 /* Value is non-zero if we found at least one overlay string. */
5042 return STRINGP (it->string);
5043 }
5044
5045
5046 \f
5047 /***********************************************************************
5048 Saving and restoring state
5049 ***********************************************************************/
5050
5051 /* Save current settings of IT on IT->stack. Called, for example,
5052 before setting up IT for an overlay string, to be able to restore
5053 IT's settings to what they were after the overlay string has been
5054 processed. If POSITION is non-NULL, it is the position to save on
5055 the stack instead of IT->position. */
5056
5057 static void
5058 push_it (struct it *it, struct text_pos *position)
5059 {
5060 struct iterator_stack_entry *p;
5061
5062 xassert (it->sp < IT_STACK_SIZE);
5063 p = it->stack + it->sp;
5064
5065 p->stop_charpos = it->stop_charpos;
5066 p->prev_stop = it->prev_stop;
5067 p->base_level_stop = it->base_level_stop;
5068 p->cmp_it = it->cmp_it;
5069 xassert (it->face_id >= 0);
5070 p->face_id = it->face_id;
5071 p->string = it->string;
5072 p->method = it->method;
5073 p->from_overlay = it->from_overlay;
5074 switch (p->method)
5075 {
5076 case GET_FROM_IMAGE:
5077 p->u.image.object = it->object;
5078 p->u.image.image_id = it->image_id;
5079 p->u.image.slice = it->slice;
5080 break;
5081 case GET_FROM_STRETCH:
5082 p->u.stretch.object = it->object;
5083 break;
5084 }
5085 p->position = position ? *position : it->position;
5086 p->current = it->current;
5087 p->end_charpos = it->end_charpos;
5088 p->string_nchars = it->string_nchars;
5089 p->area = it->area;
5090 p->multibyte_p = it->multibyte_p;
5091 p->avoid_cursor_p = it->avoid_cursor_p;
5092 p->space_width = it->space_width;
5093 p->font_height = it->font_height;
5094 p->voffset = it->voffset;
5095 p->string_from_display_prop_p = it->string_from_display_prop_p;
5096 p->display_ellipsis_p = 0;
5097 p->line_wrap = it->line_wrap;
5098 ++it->sp;
5099 }
5100
5101 static void
5102 iterate_out_of_display_property (struct it *it)
5103 {
5104 /* Maybe initialize paragraph direction. If we are at the beginning
5105 of a new paragraph, next_element_from_buffer may not have a
5106 chance to do that. */
5107 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5108 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5109 /* prev_stop can be zero, so check against BEGV as well. */
5110 while (it->bidi_it.charpos >= BEGV
5111 && it->prev_stop <= it->bidi_it.charpos
5112 && it->bidi_it.charpos < CHARPOS (it->position))
5113 bidi_move_to_visually_next (&it->bidi_it);
5114 /* Record the stop_pos we just crossed, for when we cross it
5115 back, maybe. */
5116 if (it->bidi_it.charpos > CHARPOS (it->position))
5117 it->prev_stop = CHARPOS (it->position);
5118 /* If we ended up not where pop_it put us, resync IT's
5119 positional members with the bidi iterator. */
5120 if (it->bidi_it.charpos != CHARPOS (it->position))
5121 {
5122 SET_TEXT_POS (it->position,
5123 it->bidi_it.charpos, it->bidi_it.bytepos);
5124 it->current.pos = it->position;
5125 }
5126 }
5127
5128 /* Restore IT's settings from IT->stack. Called, for example, when no
5129 more overlay strings must be processed, and we return to delivering
5130 display elements from a buffer, or when the end of a string from a
5131 `display' property is reached and we return to delivering display
5132 elements from an overlay string, or from a buffer. */
5133
5134 static void
5135 pop_it (struct it *it)
5136 {
5137 struct iterator_stack_entry *p;
5138
5139 xassert (it->sp > 0);
5140 --it->sp;
5141 p = it->stack + it->sp;
5142 it->stop_charpos = p->stop_charpos;
5143 it->prev_stop = p->prev_stop;
5144 it->base_level_stop = p->base_level_stop;
5145 it->cmp_it = p->cmp_it;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 it->from_overlay = p->from_overlay;
5151 if (NILP (it->string))
5152 SET_TEXT_POS (it->current.string_pos, -1, -1);
5153 it->method = p->method;
5154 switch (it->method)
5155 {
5156 case GET_FROM_IMAGE:
5157 it->image_id = p->u.image.image_id;
5158 it->object = p->u.image.object;
5159 it->slice = p->u.image.slice;
5160 break;
5161 case GET_FROM_STRETCH:
5162 it->object = p->u.comp.object;
5163 break;
5164 case GET_FROM_BUFFER:
5165 it->object = it->w->buffer;
5166 if (it->bidi_p)
5167 {
5168 /* Bidi-iterate until we get out of the portion of text, if
5169 any, covered by a `display' text property or an overlay
5170 with `display' property. (We cannot just jump there,
5171 because the internal coherency of the bidi iterator state
5172 can not be preserved across such jumps.) We also must
5173 determine the paragraph base direction if the overlay we
5174 just processed is at the beginning of a new
5175 paragraph. */
5176 iterate_out_of_display_property (it);
5177 }
5178 break;
5179 case GET_FROM_STRING:
5180 it->object = it->string;
5181 break;
5182 case GET_FROM_DISPLAY_VECTOR:
5183 if (it->s)
5184 it->method = GET_FROM_C_STRING;
5185 else if (STRINGP (it->string))
5186 it->method = GET_FROM_STRING;
5187 else
5188 {
5189 it->method = GET_FROM_BUFFER;
5190 it->object = it->w->buffer;
5191 }
5192 }
5193 it->end_charpos = p->end_charpos;
5194 it->string_nchars = p->string_nchars;
5195 it->area = p->area;
5196 it->multibyte_p = p->multibyte_p;
5197 it->avoid_cursor_p = p->avoid_cursor_p;
5198 it->space_width = p->space_width;
5199 it->font_height = p->font_height;
5200 it->voffset = p->voffset;
5201 it->string_from_display_prop_p = p->string_from_display_prop_p;
5202 it->line_wrap = p->line_wrap;
5203 }
5204
5205
5206 \f
5207 /***********************************************************************
5208 Moving over lines
5209 ***********************************************************************/
5210
5211 /* Set IT's current position to the previous line start. */
5212
5213 static void
5214 back_to_previous_line_start (struct it *it)
5215 {
5216 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5217 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5218 }
5219
5220
5221 /* Move IT to the next line start.
5222
5223 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5224 we skipped over part of the text (as opposed to moving the iterator
5225 continuously over the text). Otherwise, don't change the value
5226 of *SKIPPED_P.
5227
5228 Newlines may come from buffer text, overlay strings, or strings
5229 displayed via the `display' property. That's the reason we can't
5230 simply use find_next_newline_no_quit.
5231
5232 Note that this function may not skip over invisible text that is so
5233 because of text properties and immediately follows a newline. If
5234 it would, function reseat_at_next_visible_line_start, when called
5235 from set_iterator_to_next, would effectively make invisible
5236 characters following a newline part of the wrong glyph row, which
5237 leads to wrong cursor motion. */
5238
5239 static int
5240 forward_to_next_line_start (struct it *it, int *skipped_p)
5241 {
5242 int old_selective, newline_found_p, n;
5243 const int MAX_NEWLINE_DISTANCE = 500;
5244
5245 /* If already on a newline, just consume it to avoid unintended
5246 skipping over invisible text below. */
5247 if (it->what == IT_CHARACTER
5248 && it->c == '\n'
5249 && CHARPOS (it->position) == IT_CHARPOS (*it))
5250 {
5251 set_iterator_to_next (it, 0);
5252 it->c = 0;
5253 return 1;
5254 }
5255
5256 /* Don't handle selective display in the following. It's (a)
5257 unnecessary because it's done by the caller, and (b) leads to an
5258 infinite recursion because next_element_from_ellipsis indirectly
5259 calls this function. */
5260 old_selective = it->selective;
5261 it->selective = 0;
5262
5263 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5264 from buffer text. */
5265 for (n = newline_found_p = 0;
5266 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5267 n += STRINGP (it->string) ? 0 : 1)
5268 {
5269 if (!get_next_display_element (it))
5270 return 0;
5271 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5272 set_iterator_to_next (it, 0);
5273 }
5274
5275 /* If we didn't find a newline near enough, see if we can use a
5276 short-cut. */
5277 if (!newline_found_p)
5278 {
5279 EMACS_INT start = IT_CHARPOS (*it);
5280 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5281 Lisp_Object pos;
5282
5283 xassert (!STRINGP (it->string));
5284
5285 /* If there isn't any `display' property in sight, and no
5286 overlays, we can just use the position of the newline in
5287 buffer text. */
5288 if (it->stop_charpos >= limit
5289 || ((pos = Fnext_single_property_change (make_number (start),
5290 Qdisplay,
5291 Qnil, make_number (limit)),
5292 NILP (pos))
5293 && next_overlay_change (start) == ZV))
5294 {
5295 IT_CHARPOS (*it) = limit;
5296 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5297 *skipped_p = newline_found_p = 1;
5298 }
5299 else
5300 {
5301 while (get_next_display_element (it)
5302 && !newline_found_p)
5303 {
5304 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5305 set_iterator_to_next (it, 0);
5306 }
5307 }
5308 }
5309
5310 it->selective = old_selective;
5311 return newline_found_p;
5312 }
5313
5314
5315 /* Set IT's current position to the previous visible line start. Skip
5316 invisible text that is so either due to text properties or due to
5317 selective display. Caution: this does not change IT->current_x and
5318 IT->hpos. */
5319
5320 static void
5321 back_to_previous_visible_line_start (struct it *it)
5322 {
5323 while (IT_CHARPOS (*it) > BEGV)
5324 {
5325 back_to_previous_line_start (it);
5326
5327 if (IT_CHARPOS (*it) <= BEGV)
5328 break;
5329
5330 /* If selective > 0, then lines indented more than its value are
5331 invisible. */
5332 if (it->selective > 0
5333 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5334 (double) it->selective)) /* iftc */
5335 continue;
5336
5337 /* Check the newline before point for invisibility. */
5338 {
5339 Lisp_Object prop;
5340 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5341 Qinvisible, it->window);
5342 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5343 continue;
5344 }
5345
5346 if (IT_CHARPOS (*it) <= BEGV)
5347 break;
5348
5349 {
5350 struct it it2;
5351 EMACS_INT pos;
5352 EMACS_INT beg, end;
5353 Lisp_Object val, overlay;
5354
5355 /* If newline is part of a composition, continue from start of composition */
5356 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5357 && beg < IT_CHARPOS (*it))
5358 goto replaced;
5359
5360 /* If newline is replaced by a display property, find start of overlay
5361 or interval and continue search from that point. */
5362 it2 = *it;
5363 pos = --IT_CHARPOS (it2);
5364 --IT_BYTEPOS (it2);
5365 it2.sp = 0;
5366 it2.string_from_display_prop_p = 0;
5367 if (handle_display_prop (&it2) == HANDLED_RETURN
5368 && !NILP (val = get_char_property_and_overlay
5369 (make_number (pos), Qdisplay, Qnil, &overlay))
5370 && (OVERLAYP (overlay)
5371 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5372 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5373 goto replaced;
5374
5375 /* Newline is not replaced by anything -- so we are done. */
5376 break;
5377
5378 replaced:
5379 if (beg < BEGV)
5380 beg = BEGV;
5381 IT_CHARPOS (*it) = beg;
5382 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5383 }
5384 }
5385
5386 it->continuation_lines_width = 0;
5387
5388 xassert (IT_CHARPOS (*it) >= BEGV);
5389 xassert (IT_CHARPOS (*it) == BEGV
5390 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5391 CHECK_IT (it);
5392 }
5393
5394
5395 /* Reseat iterator IT at the previous visible line start. Skip
5396 invisible text that is so either due to text properties or due to
5397 selective display. At the end, update IT's overlay information,
5398 face information etc. */
5399
5400 void
5401 reseat_at_previous_visible_line_start (struct it *it)
5402 {
5403 back_to_previous_visible_line_start (it);
5404 reseat (it, it->current.pos, 1);
5405 CHECK_IT (it);
5406 }
5407
5408
5409 /* Reseat iterator IT on the next visible line start in the current
5410 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5411 preceding the line start. Skip over invisible text that is so
5412 because of selective display. Compute faces, overlays etc at the
5413 new position. Note that this function does not skip over text that
5414 is invisible because of text properties. */
5415
5416 static void
5417 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5418 {
5419 int newline_found_p, skipped_p = 0;
5420
5421 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5422
5423 /* Skip over lines that are invisible because they are indented
5424 more than the value of IT->selective. */
5425 if (it->selective > 0)
5426 while (IT_CHARPOS (*it) < ZV
5427 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5428 (double) it->selective)) /* iftc */
5429 {
5430 xassert (IT_BYTEPOS (*it) == BEGV
5431 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5432 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5433 }
5434
5435 /* Position on the newline if that's what's requested. */
5436 if (on_newline_p && newline_found_p)
5437 {
5438 if (STRINGP (it->string))
5439 {
5440 if (IT_STRING_CHARPOS (*it) > 0)
5441 {
5442 --IT_STRING_CHARPOS (*it);
5443 --IT_STRING_BYTEPOS (*it);
5444 }
5445 }
5446 else if (IT_CHARPOS (*it) > BEGV)
5447 {
5448 --IT_CHARPOS (*it);
5449 --IT_BYTEPOS (*it);
5450 reseat (it, it->current.pos, 0);
5451 }
5452 }
5453 else if (skipped_p)
5454 reseat (it, it->current.pos, 0);
5455
5456 CHECK_IT (it);
5457 }
5458
5459
5460 \f
5461 /***********************************************************************
5462 Changing an iterator's position
5463 ***********************************************************************/
5464
5465 /* Change IT's current position to POS in current_buffer. If FORCE_P
5466 is non-zero, always check for text properties at the new position.
5467 Otherwise, text properties are only looked up if POS >=
5468 IT->check_charpos of a property. */
5469
5470 static void
5471 reseat (struct it *it, struct text_pos pos, int force_p)
5472 {
5473 EMACS_INT original_pos = IT_CHARPOS (*it);
5474
5475 reseat_1 (it, pos, 0);
5476
5477 /* Determine where to check text properties. Avoid doing it
5478 where possible because text property lookup is very expensive. */
5479 if (force_p
5480 || CHARPOS (pos) > it->stop_charpos
5481 || CHARPOS (pos) < original_pos)
5482 {
5483 if (it->bidi_p)
5484 {
5485 /* For bidi iteration, we need to prime prev_stop and
5486 base_level_stop with our best estimations. */
5487 if (CHARPOS (pos) < it->prev_stop)
5488 {
5489 handle_stop_backwards (it, BEGV);
5490 if (CHARPOS (pos) < it->base_level_stop)
5491 it->base_level_stop = 0;
5492 }
5493 else if (CHARPOS (pos) > it->stop_charpos
5494 && it->stop_charpos >= BEGV)
5495 handle_stop_backwards (it, it->stop_charpos);
5496 else /* force_p */
5497 handle_stop (it);
5498 }
5499 else
5500 {
5501 handle_stop (it);
5502 it->prev_stop = it->base_level_stop = 0;
5503 }
5504
5505 }
5506
5507 CHECK_IT (it);
5508 }
5509
5510
5511 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5512 IT->stop_pos to POS, also. */
5513
5514 static void
5515 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5516 {
5517 /* Don't call this function when scanning a C string. */
5518 xassert (it->s == NULL);
5519
5520 /* POS must be a reasonable value. */
5521 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5522
5523 it->current.pos = it->position = pos;
5524 it->end_charpos = ZV;
5525 it->dpvec = NULL;
5526 it->current.dpvec_index = -1;
5527 it->current.overlay_string_index = -1;
5528 IT_STRING_CHARPOS (*it) = -1;
5529 IT_STRING_BYTEPOS (*it) = -1;
5530 it->string = Qnil;
5531 it->string_from_display_prop_p = 0;
5532 it->method = GET_FROM_BUFFER;
5533 it->object = it->w->buffer;
5534 it->area = TEXT_AREA;
5535 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5536 it->sp = 0;
5537 it->string_from_display_prop_p = 0;
5538 it->face_before_selective_p = 0;
5539 if (it->bidi_p)
5540 {
5541 it->bidi_it.first_elt = 1;
5542 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5543 it->bidi_it.disp_pos = -1;
5544 it->bidi_it.string.s = NULL;
5545 it->bidi_it.string.lstring = Qnil;
5546 it->bidi_it.string.bufpos = 0;
5547 }
5548
5549 if (set_stop_p)
5550 {
5551 it->stop_charpos = CHARPOS (pos);
5552 it->base_level_stop = CHARPOS (pos);
5553 }
5554 }
5555
5556
5557 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5558 If S is non-null, it is a C string to iterate over. Otherwise,
5559 STRING gives a Lisp string to iterate over.
5560
5561 If PRECISION > 0, don't return more then PRECISION number of
5562 characters from the string.
5563
5564 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5565 characters have been returned. FIELD_WIDTH < 0 means an infinite
5566 field width.
5567
5568 MULTIBYTE = 0 means disable processing of multibyte characters,
5569 MULTIBYTE > 0 means enable it,
5570 MULTIBYTE < 0 means use IT->multibyte_p.
5571
5572 IT must be initialized via a prior call to init_iterator before
5573 calling this function. */
5574
5575 static void
5576 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5577 EMACS_INT charpos, EMACS_INT precision, int field_width,
5578 int multibyte)
5579 {
5580 /* No region in strings. */
5581 it->region_beg_charpos = it->region_end_charpos = -1;
5582
5583 /* No text property checks performed by default, but see below. */
5584 it->stop_charpos = -1;
5585
5586 /* Set iterator position and end position. */
5587 memset (&it->current, 0, sizeof it->current);
5588 it->current.overlay_string_index = -1;
5589 it->current.dpvec_index = -1;
5590 xassert (charpos >= 0);
5591
5592 /* If STRING is specified, use its multibyteness, otherwise use the
5593 setting of MULTIBYTE, if specified. */
5594 if (multibyte >= 0)
5595 it->multibyte_p = multibyte > 0;
5596
5597 if (s == NULL)
5598 {
5599 xassert (STRINGP (string));
5600 it->string = string;
5601 it->s = NULL;
5602 it->end_charpos = it->string_nchars = SCHARS (string);
5603 it->method = GET_FROM_STRING;
5604 it->current.string_pos = string_pos (charpos, string);
5605
5606 if (it->bidi_p)
5607 {
5608 it->paragraph_embedding = NEUTRAL_DIR;
5609 it->bidi_it.string.lstring = string;
5610 it->bidi_it.string.s = SDATA (string);
5611 it->bidi_it.string.schars = it->end_charpos;
5612 it->bidi_it.string.bufpos = 0;
5613 it->bidi_it.string.from_disp_str = 0;
5614 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5615 FRAME_WINDOW_P (it->f), &it->bidi_it);
5616 }
5617 }
5618 else
5619 {
5620 it->s = (const unsigned char *) s;
5621 it->string = Qnil;
5622
5623 /* Note that we use IT->current.pos, not it->current.string_pos,
5624 for displaying C strings. */
5625 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5626 if (it->multibyte_p)
5627 {
5628 it->current.pos = c_string_pos (charpos, s, 1);
5629 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5630
5631 if (it->bidi_p)
5632 {
5633 it->paragraph_embedding = NEUTRAL_DIR;
5634 it->bidi_it.string.lstring = Qnil;
5635 it->bidi_it.string.s = s;
5636 it->bidi_it.string.schars = it->end_charpos;
5637 it->bidi_it.string.bufpos = 0;
5638 it->bidi_it.string.from_disp_str = 0;
5639 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5640 &it->bidi_it);
5641 }
5642 }
5643 else
5644 {
5645 /* Unibyte (a.k.a. ASCII) C strings are never bidi-reordered. */
5646 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5647 it->end_charpos = it->string_nchars = strlen (s);
5648 }
5649
5650 it->method = GET_FROM_C_STRING;
5651 }
5652
5653 /* PRECISION > 0 means don't return more than PRECISION characters
5654 from the string. */
5655 if (precision > 0 && it->end_charpos - charpos > precision)
5656 {
5657 it->end_charpos = it->string_nchars = charpos + precision;
5658 if (it->bidi_p)
5659 it->bidi_it.string.schars = it->end_charpos;
5660 }
5661
5662 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5663 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5664 FIELD_WIDTH < 0 means infinite field width. This is useful for
5665 padding with `-' at the end of a mode line. */
5666 if (field_width < 0)
5667 field_width = INFINITY;
5668 /* Implementation note: We deliberately don't enlarge
5669 it->bidi_it.string.schars here to fit it->end_charpos, because
5670 the bidi iterator cannot produce characters out of thin air. */
5671 if (field_width > it->end_charpos - charpos)
5672 it->end_charpos = charpos + field_width;
5673
5674 /* Use the standard display table for displaying strings. */
5675 if (DISP_TABLE_P (Vstandard_display_table))
5676 it->dp = XCHAR_TABLE (Vstandard_display_table);
5677
5678 it->stop_charpos = charpos;
5679 it->prev_stop = charpos;
5680 it->base_level_stop = 0;
5681 if (it->bidi_p)
5682 {
5683 it->bidi_it.first_elt = 1;
5684 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5685 it->bidi_it.disp_pos = -1;
5686 }
5687 if (s == NULL && it->multibyte_p)
5688 {
5689 EMACS_INT endpos = SCHARS (it->string);
5690 if (endpos > it->end_charpos)
5691 endpos = it->end_charpos;
5692 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5693 it->string);
5694 }
5695 CHECK_IT (it);
5696 }
5697
5698
5699 \f
5700 /***********************************************************************
5701 Iteration
5702 ***********************************************************************/
5703
5704 /* Map enum it_method value to corresponding next_element_from_* function. */
5705
5706 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5707 {
5708 next_element_from_buffer,
5709 next_element_from_display_vector,
5710 next_element_from_string,
5711 next_element_from_c_string,
5712 next_element_from_image,
5713 next_element_from_stretch
5714 };
5715
5716 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5717
5718
5719 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5720 (possibly with the following characters). */
5721
5722 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5723 ((IT)->cmp_it.id >= 0 \
5724 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5725 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5726 END_CHARPOS, (IT)->w, \
5727 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5728 (IT)->string)))
5729
5730
5731 /* Lookup the char-table Vglyphless_char_display for character C (-1
5732 if we want information for no-font case), and return the display
5733 method symbol. By side-effect, update it->what and
5734 it->glyphless_method. This function is called from
5735 get_next_display_element for each character element, and from
5736 x_produce_glyphs when no suitable font was found. */
5737
5738 Lisp_Object
5739 lookup_glyphless_char_display (int c, struct it *it)
5740 {
5741 Lisp_Object glyphless_method = Qnil;
5742
5743 if (CHAR_TABLE_P (Vglyphless_char_display)
5744 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5745 {
5746 if (c >= 0)
5747 {
5748 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5749 if (CONSP (glyphless_method))
5750 glyphless_method = FRAME_WINDOW_P (it->f)
5751 ? XCAR (glyphless_method)
5752 : XCDR (glyphless_method);
5753 }
5754 else
5755 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
5756 }
5757
5758 retry:
5759 if (NILP (glyphless_method))
5760 {
5761 if (c >= 0)
5762 /* The default is to display the character by a proper font. */
5763 return Qnil;
5764 /* The default for the no-font case is to display an empty box. */
5765 glyphless_method = Qempty_box;
5766 }
5767 if (EQ (glyphless_method, Qzero_width))
5768 {
5769 if (c >= 0)
5770 return glyphless_method;
5771 /* This method can't be used for the no-font case. */
5772 glyphless_method = Qempty_box;
5773 }
5774 if (EQ (glyphless_method, Qthin_space))
5775 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5776 else if (EQ (glyphless_method, Qempty_box))
5777 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5778 else if (EQ (glyphless_method, Qhex_code))
5779 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5780 else if (STRINGP (glyphless_method))
5781 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5782 else
5783 {
5784 /* Invalid value. We use the default method. */
5785 glyphless_method = Qnil;
5786 goto retry;
5787 }
5788 it->what = IT_GLYPHLESS;
5789 return glyphless_method;
5790 }
5791
5792 /* Load IT's display element fields with information about the next
5793 display element from the current position of IT. Value is zero if
5794 end of buffer (or C string) is reached. */
5795
5796 static struct frame *last_escape_glyph_frame = NULL;
5797 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5798 static int last_escape_glyph_merged_face_id = 0;
5799
5800 struct frame *last_glyphless_glyph_frame = NULL;
5801 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5802 int last_glyphless_glyph_merged_face_id = 0;
5803
5804 static int
5805 get_next_display_element (struct it *it)
5806 {
5807 /* Non-zero means that we found a display element. Zero means that
5808 we hit the end of what we iterate over. Performance note: the
5809 function pointer `method' used here turns out to be faster than
5810 using a sequence of if-statements. */
5811 int success_p;
5812
5813 get_next:
5814 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5815
5816 if (it->what == IT_CHARACTER)
5817 {
5818 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5819 and only if (a) the resolved directionality of that character
5820 is R..." */
5821 /* FIXME: Do we need an exception for characters from display
5822 tables? */
5823 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5824 it->c = bidi_mirror_char (it->c);
5825 /* Map via display table or translate control characters.
5826 IT->c, IT->len etc. have been set to the next character by
5827 the function call above. If we have a display table, and it
5828 contains an entry for IT->c, translate it. Don't do this if
5829 IT->c itself comes from a display table, otherwise we could
5830 end up in an infinite recursion. (An alternative could be to
5831 count the recursion depth of this function and signal an
5832 error when a certain maximum depth is reached.) Is it worth
5833 it? */
5834 if (success_p && it->dpvec == NULL)
5835 {
5836 Lisp_Object dv;
5837 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5838 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5839 nbsp_or_shy = char_is_other;
5840 int c = it->c; /* This is the character to display. */
5841
5842 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5843 {
5844 xassert (SINGLE_BYTE_CHAR_P (c));
5845 if (unibyte_display_via_language_environment)
5846 {
5847 c = DECODE_CHAR (unibyte, c);
5848 if (c < 0)
5849 c = BYTE8_TO_CHAR (it->c);
5850 }
5851 else
5852 c = BYTE8_TO_CHAR (it->c);
5853 }
5854
5855 if (it->dp
5856 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5857 VECTORP (dv)))
5858 {
5859 struct Lisp_Vector *v = XVECTOR (dv);
5860
5861 /* Return the first character from the display table
5862 entry, if not empty. If empty, don't display the
5863 current character. */
5864 if (v->header.size)
5865 {
5866 it->dpvec_char_len = it->len;
5867 it->dpvec = v->contents;
5868 it->dpend = v->contents + v->header.size;
5869 it->current.dpvec_index = 0;
5870 it->dpvec_face_id = -1;
5871 it->saved_face_id = it->face_id;
5872 it->method = GET_FROM_DISPLAY_VECTOR;
5873 it->ellipsis_p = 0;
5874 }
5875 else
5876 {
5877 set_iterator_to_next (it, 0);
5878 }
5879 goto get_next;
5880 }
5881
5882 if (! NILP (lookup_glyphless_char_display (c, it)))
5883 {
5884 if (it->what == IT_GLYPHLESS)
5885 goto done;
5886 /* Don't display this character. */
5887 set_iterator_to_next (it, 0);
5888 goto get_next;
5889 }
5890
5891 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5892 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5893 : c == 0xAD ? char_is_soft_hyphen
5894 : char_is_other);
5895
5896 /* Translate control characters into `\003' or `^C' form.
5897 Control characters coming from a display table entry are
5898 currently not translated because we use IT->dpvec to hold
5899 the translation. This could easily be changed but I
5900 don't believe that it is worth doing.
5901
5902 NBSP and SOFT-HYPEN are property translated too.
5903
5904 Non-printable characters and raw-byte characters are also
5905 translated to octal form. */
5906 if (((c < ' ' || c == 127) /* ASCII control chars */
5907 ? (it->area != TEXT_AREA
5908 /* In mode line, treat \n, \t like other crl chars. */
5909 || (c != '\t'
5910 && it->glyph_row
5911 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5912 || (c != '\n' && c != '\t'))
5913 : (nbsp_or_shy
5914 || CHAR_BYTE8_P (c)
5915 || ! CHAR_PRINTABLE_P (c))))
5916 {
5917 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5918 or a non-printable character which must be displayed
5919 either as '\003' or as `^C' where the '\\' and '^'
5920 can be defined in the display table. Fill
5921 IT->ctl_chars with glyphs for what we have to
5922 display. Then, set IT->dpvec to these glyphs. */
5923 Lisp_Object gc;
5924 int ctl_len;
5925 int face_id, lface_id = 0 ;
5926 int escape_glyph;
5927
5928 /* Handle control characters with ^. */
5929
5930 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5931 {
5932 int g;
5933
5934 g = '^'; /* default glyph for Control */
5935 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5936 if (it->dp
5937 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5938 && GLYPH_CODE_CHAR_VALID_P (gc))
5939 {
5940 g = GLYPH_CODE_CHAR (gc);
5941 lface_id = GLYPH_CODE_FACE (gc);
5942 }
5943 if (lface_id)
5944 {
5945 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5946 }
5947 else if (it->f == last_escape_glyph_frame
5948 && it->face_id == last_escape_glyph_face_id)
5949 {
5950 face_id = last_escape_glyph_merged_face_id;
5951 }
5952 else
5953 {
5954 /* Merge the escape-glyph face into the current face. */
5955 face_id = merge_faces (it->f, Qescape_glyph, 0,
5956 it->face_id);
5957 last_escape_glyph_frame = it->f;
5958 last_escape_glyph_face_id = it->face_id;
5959 last_escape_glyph_merged_face_id = face_id;
5960 }
5961
5962 XSETINT (it->ctl_chars[0], g);
5963 XSETINT (it->ctl_chars[1], c ^ 0100);
5964 ctl_len = 2;
5965 goto display_control;
5966 }
5967
5968 /* Handle non-break space in the mode where it only gets
5969 highlighting. */
5970
5971 if (EQ (Vnobreak_char_display, Qt)
5972 && nbsp_or_shy == char_is_nbsp)
5973 {
5974 /* Merge the no-break-space face into the current face. */
5975 face_id = merge_faces (it->f, Qnobreak_space, 0,
5976 it->face_id);
5977
5978 c = ' ';
5979 XSETINT (it->ctl_chars[0], ' ');
5980 ctl_len = 1;
5981 goto display_control;
5982 }
5983
5984 /* Handle sequences that start with the "escape glyph". */
5985
5986 /* the default escape glyph is \. */
5987 escape_glyph = '\\';
5988
5989 if (it->dp
5990 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5991 && GLYPH_CODE_CHAR_VALID_P (gc))
5992 {
5993 escape_glyph = GLYPH_CODE_CHAR (gc);
5994 lface_id = GLYPH_CODE_FACE (gc);
5995 }
5996 if (lface_id)
5997 {
5998 /* The display table specified a face.
5999 Merge it into face_id and also into escape_glyph. */
6000 face_id = merge_faces (it->f, Qt, lface_id,
6001 it->face_id);
6002 }
6003 else if (it->f == last_escape_glyph_frame
6004 && it->face_id == last_escape_glyph_face_id)
6005 {
6006 face_id = last_escape_glyph_merged_face_id;
6007 }
6008 else
6009 {
6010 /* Merge the escape-glyph face into the current face. */
6011 face_id = merge_faces (it->f, Qescape_glyph, 0,
6012 it->face_id);
6013 last_escape_glyph_frame = it->f;
6014 last_escape_glyph_face_id = it->face_id;
6015 last_escape_glyph_merged_face_id = face_id;
6016 }
6017
6018 /* Handle soft hyphens in the mode where they only get
6019 highlighting. */
6020
6021 if (EQ (Vnobreak_char_display, Qt)
6022 && nbsp_or_shy == char_is_soft_hyphen)
6023 {
6024 XSETINT (it->ctl_chars[0], '-');
6025 ctl_len = 1;
6026 goto display_control;
6027 }
6028
6029 /* Handle non-break space and soft hyphen
6030 with the escape glyph. */
6031
6032 if (nbsp_or_shy)
6033 {
6034 XSETINT (it->ctl_chars[0], escape_glyph);
6035 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6036 XSETINT (it->ctl_chars[1], c);
6037 ctl_len = 2;
6038 goto display_control;
6039 }
6040
6041 {
6042 char str[10];
6043 int len, i;
6044
6045 if (CHAR_BYTE8_P (c))
6046 /* Display \200 instead of \17777600. */
6047 c = CHAR_TO_BYTE8 (c);
6048 len = sprintf (str, "%03o", c);
6049
6050 XSETINT (it->ctl_chars[0], escape_glyph);
6051 for (i = 0; i < len; i++)
6052 XSETINT (it->ctl_chars[i + 1], str[i]);
6053 ctl_len = len + 1;
6054 }
6055
6056 display_control:
6057 /* Set up IT->dpvec and return first character from it. */
6058 it->dpvec_char_len = it->len;
6059 it->dpvec = it->ctl_chars;
6060 it->dpend = it->dpvec + ctl_len;
6061 it->current.dpvec_index = 0;
6062 it->dpvec_face_id = face_id;
6063 it->saved_face_id = it->face_id;
6064 it->method = GET_FROM_DISPLAY_VECTOR;
6065 it->ellipsis_p = 0;
6066 goto get_next;
6067 }
6068 it->char_to_display = c;
6069 }
6070 else if (success_p)
6071 {
6072 it->char_to_display = it->c;
6073 }
6074 }
6075
6076 /* Adjust face id for a multibyte character. There are no multibyte
6077 character in unibyte text. */
6078 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6079 && it->multibyte_p
6080 && success_p
6081 && FRAME_WINDOW_P (it->f))
6082 {
6083 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6084
6085 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6086 {
6087 /* Automatic composition with glyph-string. */
6088 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6089
6090 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6091 }
6092 else
6093 {
6094 EMACS_INT pos = (it->s ? -1
6095 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6096 : IT_CHARPOS (*it));
6097
6098 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6099 it->string);
6100 }
6101 }
6102
6103 done:
6104 /* Is this character the last one of a run of characters with
6105 box? If yes, set IT->end_of_box_run_p to 1. */
6106 if (it->face_box_p
6107 && it->s == NULL)
6108 {
6109 if (it->method == GET_FROM_STRING && it->sp)
6110 {
6111 int face_id = underlying_face_id (it);
6112 struct face *face = FACE_FROM_ID (it->f, face_id);
6113
6114 if (face)
6115 {
6116 if (face->box == FACE_NO_BOX)
6117 {
6118 /* If the box comes from face properties in a
6119 display string, check faces in that string. */
6120 int string_face_id = face_after_it_pos (it);
6121 it->end_of_box_run_p
6122 = (FACE_FROM_ID (it->f, string_face_id)->box
6123 == FACE_NO_BOX);
6124 }
6125 /* Otherwise, the box comes from the underlying face.
6126 If this is the last string character displayed, check
6127 the next buffer location. */
6128 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6129 && (it->current.overlay_string_index
6130 == it->n_overlay_strings - 1))
6131 {
6132 EMACS_INT ignore;
6133 int next_face_id;
6134 struct text_pos pos = it->current.pos;
6135 INC_TEXT_POS (pos, it->multibyte_p);
6136
6137 next_face_id = face_at_buffer_position
6138 (it->w, CHARPOS (pos), it->region_beg_charpos,
6139 it->region_end_charpos, &ignore,
6140 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6141 -1);
6142 it->end_of_box_run_p
6143 = (FACE_FROM_ID (it->f, next_face_id)->box
6144 == FACE_NO_BOX);
6145 }
6146 }
6147 }
6148 else
6149 {
6150 int face_id = face_after_it_pos (it);
6151 it->end_of_box_run_p
6152 = (face_id != it->face_id
6153 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6154 }
6155 }
6156
6157 /* Value is 0 if end of buffer or string reached. */
6158 return success_p;
6159 }
6160
6161
6162 /* Move IT to the next display element.
6163
6164 RESEAT_P non-zero means if called on a newline in buffer text,
6165 skip to the next visible line start.
6166
6167 Functions get_next_display_element and set_iterator_to_next are
6168 separate because I find this arrangement easier to handle than a
6169 get_next_display_element function that also increments IT's
6170 position. The way it is we can first look at an iterator's current
6171 display element, decide whether it fits on a line, and if it does,
6172 increment the iterator position. The other way around we probably
6173 would either need a flag indicating whether the iterator has to be
6174 incremented the next time, or we would have to implement a
6175 decrement position function which would not be easy to write. */
6176
6177 void
6178 set_iterator_to_next (struct it *it, int reseat_p)
6179 {
6180 /* Reset flags indicating start and end of a sequence of characters
6181 with box. Reset them at the start of this function because
6182 moving the iterator to a new position might set them. */
6183 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6184
6185 switch (it->method)
6186 {
6187 case GET_FROM_BUFFER:
6188 /* The current display element of IT is a character from
6189 current_buffer. Advance in the buffer, and maybe skip over
6190 invisible lines that are so because of selective display. */
6191 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6192 reseat_at_next_visible_line_start (it, 0);
6193 else if (it->cmp_it.id >= 0)
6194 {
6195 /* We are currently getting glyphs from a composition. */
6196 int i;
6197
6198 if (! it->bidi_p)
6199 {
6200 IT_CHARPOS (*it) += it->cmp_it.nchars;
6201 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6202 if (it->cmp_it.to < it->cmp_it.nglyphs)
6203 {
6204 it->cmp_it.from = it->cmp_it.to;
6205 }
6206 else
6207 {
6208 it->cmp_it.id = -1;
6209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6210 IT_BYTEPOS (*it),
6211 it->end_charpos, Qnil);
6212 }
6213 }
6214 else if (! it->cmp_it.reversed_p)
6215 {
6216 /* Composition created while scanning forward. */
6217 /* Update IT's char/byte positions to point to the first
6218 character of the next grapheme cluster, or to the
6219 character visually after the current composition. */
6220 for (i = 0; i < it->cmp_it.nchars; i++)
6221 bidi_move_to_visually_next (&it->bidi_it);
6222 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6223 IT_CHARPOS (*it) = it->bidi_it.charpos;
6224
6225 if (it->cmp_it.to < it->cmp_it.nglyphs)
6226 {
6227 /* Proceed to the next grapheme cluster. */
6228 it->cmp_it.from = it->cmp_it.to;
6229 }
6230 else
6231 {
6232 /* No more grapheme clusters in this composition.
6233 Find the next stop position. */
6234 EMACS_INT stop = it->end_charpos;
6235 if (it->bidi_it.scan_dir < 0)
6236 /* Now we are scanning backward and don't know
6237 where to stop. */
6238 stop = -1;
6239 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6240 IT_BYTEPOS (*it), stop, Qnil);
6241 }
6242 }
6243 else
6244 {
6245 /* Composition created while scanning backward. */
6246 /* Update IT's char/byte positions to point to the last
6247 character of the previous grapheme cluster, or the
6248 character visually after the current composition. */
6249 for (i = 0; i < it->cmp_it.nchars; i++)
6250 bidi_move_to_visually_next (&it->bidi_it);
6251 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6252 IT_CHARPOS (*it) = it->bidi_it.charpos;
6253 if (it->cmp_it.from > 0)
6254 {
6255 /* Proceed to the previous grapheme cluster. */
6256 it->cmp_it.to = it->cmp_it.from;
6257 }
6258 else
6259 {
6260 /* No more grapheme clusters in this composition.
6261 Find the next stop position. */
6262 EMACS_INT stop = it->end_charpos;
6263 if (it->bidi_it.scan_dir < 0)
6264 /* Now we are scanning backward and don't know
6265 where to stop. */
6266 stop = -1;
6267 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6268 IT_BYTEPOS (*it), stop, Qnil);
6269 }
6270 }
6271 }
6272 else
6273 {
6274 xassert (it->len != 0);
6275
6276 if (!it->bidi_p)
6277 {
6278 IT_BYTEPOS (*it) += it->len;
6279 IT_CHARPOS (*it) += 1;
6280 }
6281 else
6282 {
6283 int prev_scan_dir = it->bidi_it.scan_dir;
6284 /* If this is a new paragraph, determine its base
6285 direction (a.k.a. its base embedding level). */
6286 if (it->bidi_it.new_paragraph)
6287 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6288 bidi_move_to_visually_next (&it->bidi_it);
6289 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6290 IT_CHARPOS (*it) = it->bidi_it.charpos;
6291 if (prev_scan_dir != it->bidi_it.scan_dir)
6292 {
6293 /* As the scan direction was changed, we must
6294 re-compute the stop position for composition. */
6295 EMACS_INT stop = it->end_charpos;
6296 if (it->bidi_it.scan_dir < 0)
6297 stop = -1;
6298 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6299 IT_BYTEPOS (*it), stop, Qnil);
6300 }
6301 }
6302 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6303 }
6304 break;
6305
6306 case GET_FROM_C_STRING:
6307 /* Current display element of IT is from a C string. */
6308 if (!it->bidi_p
6309 /* If the string position is beyond string_nchars, it means
6310 next_element_from_c_string is padding the string with
6311 blanks, in which case we bypass the bidi iterator,
6312 because it cannot deal with such virtual characters. */
6313 || IT_CHARPOS (*it) >= it->string_nchars)
6314 {
6315 IT_BYTEPOS (*it) += it->len;
6316 IT_CHARPOS (*it) += 1;
6317 }
6318 else
6319 {
6320 bidi_move_to_visually_next (&it->bidi_it);
6321 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6322 IT_CHARPOS (*it) = it->bidi_it.charpos;
6323 }
6324 break;
6325
6326 case GET_FROM_DISPLAY_VECTOR:
6327 /* Current display element of IT is from a display table entry.
6328 Advance in the display table definition. Reset it to null if
6329 end reached, and continue with characters from buffers/
6330 strings. */
6331 ++it->current.dpvec_index;
6332
6333 /* Restore face of the iterator to what they were before the
6334 display vector entry (these entries may contain faces). */
6335 it->face_id = it->saved_face_id;
6336
6337 if (it->dpvec + it->current.dpvec_index == it->dpend)
6338 {
6339 int recheck_faces = it->ellipsis_p;
6340
6341 if (it->s)
6342 it->method = GET_FROM_C_STRING;
6343 else if (STRINGP (it->string))
6344 it->method = GET_FROM_STRING;
6345 else
6346 {
6347 it->method = GET_FROM_BUFFER;
6348 it->object = it->w->buffer;
6349 }
6350
6351 it->dpvec = NULL;
6352 it->current.dpvec_index = -1;
6353
6354 /* Skip over characters which were displayed via IT->dpvec. */
6355 if (it->dpvec_char_len < 0)
6356 reseat_at_next_visible_line_start (it, 1);
6357 else if (it->dpvec_char_len > 0)
6358 {
6359 if (it->method == GET_FROM_STRING
6360 && it->n_overlay_strings > 0)
6361 it->ignore_overlay_strings_at_pos_p = 1;
6362 it->len = it->dpvec_char_len;
6363 set_iterator_to_next (it, reseat_p);
6364 }
6365
6366 /* Maybe recheck faces after display vector */
6367 if (recheck_faces)
6368 it->stop_charpos = IT_CHARPOS (*it);
6369 }
6370 break;
6371
6372 case GET_FROM_STRING:
6373 /* Current display element is a character from a Lisp string. */
6374 xassert (it->s == NULL && STRINGP (it->string));
6375 if (it->cmp_it.id >= 0)
6376 {
6377 int i;
6378
6379 if (! it->bidi_p)
6380 {
6381 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6382 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6383 if (it->cmp_it.to < it->cmp_it.nglyphs)
6384 it->cmp_it.from = it->cmp_it.to;
6385 else
6386 {
6387 it->cmp_it.id = -1;
6388 composition_compute_stop_pos (&it->cmp_it,
6389 IT_STRING_CHARPOS (*it),
6390 IT_STRING_BYTEPOS (*it),
6391 it->end_charpos, it->string);
6392 }
6393 }
6394 else if (! it->cmp_it.reversed_p)
6395 {
6396 for (i = 0; i < it->cmp_it.nchars; i++)
6397 bidi_move_to_visually_next (&it->bidi_it);
6398 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6399 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6400
6401 if (it->cmp_it.to < it->cmp_it.nglyphs)
6402 it->cmp_it.from = it->cmp_it.to;
6403 else
6404 {
6405 EMACS_INT stop = it->end_charpos;
6406 if (it->bidi_it.scan_dir < 0)
6407 stop = -1;
6408 composition_compute_stop_pos (&it->cmp_it,
6409 IT_STRING_CHARPOS (*it),
6410 IT_STRING_BYTEPOS (*it), stop,
6411 it->string);
6412 }
6413 }
6414 else
6415 {
6416 for (i = 0; i < it->cmp_it.nchars; i++)
6417 bidi_move_to_visually_next (&it->bidi_it);
6418 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6419 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6420 if (it->cmp_it.from > 0)
6421 it->cmp_it.to = it->cmp_it.from;
6422 else
6423 {
6424 EMACS_INT stop = it->end_charpos;
6425 if (it->bidi_it.scan_dir < 0)
6426 stop = -1;
6427 composition_compute_stop_pos (&it->cmp_it,
6428 IT_STRING_CHARPOS (*it),
6429 IT_STRING_BYTEPOS (*it), stop,
6430 it->string);
6431 }
6432 }
6433 }
6434 else
6435 {
6436 if (!it->bidi_p
6437 /* If the string position is beyond string_nchars, it
6438 means next_element_from_string is padding the string
6439 with blanks, in which case we bypass the bidi
6440 iterator, because it cannot deal with such virtual
6441 characters. */
6442 || IT_STRING_CHARPOS (*it) >= it->string_nchars)
6443 {
6444 IT_STRING_BYTEPOS (*it) += it->len;
6445 IT_STRING_CHARPOS (*it) += 1;
6446 }
6447 else
6448 {
6449 int prev_scan_dir = it->bidi_it.scan_dir;
6450
6451 bidi_move_to_visually_next (&it->bidi_it);
6452 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6453 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6454 if (prev_scan_dir != it->bidi_it.scan_dir)
6455 {
6456 EMACS_INT stop = it->end_charpos;
6457
6458 if (it->bidi_it.scan_dir < 0)
6459 stop = -1;
6460 composition_compute_stop_pos (&it->cmp_it,
6461 IT_STRING_CHARPOS (*it),
6462 IT_STRING_BYTEPOS (*it), stop,
6463 it->string);
6464 }
6465 }
6466 }
6467
6468 consider_string_end:
6469
6470 if (it->current.overlay_string_index >= 0)
6471 {
6472 /* IT->string is an overlay string. Advance to the
6473 next, if there is one. */
6474 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6475 {
6476 it->ellipsis_p = 0;
6477 next_overlay_string (it);
6478 if (it->ellipsis_p)
6479 setup_for_ellipsis (it, 0);
6480 }
6481 }
6482 else
6483 {
6484 /* IT->string is not an overlay string. If we reached
6485 its end, and there is something on IT->stack, proceed
6486 with what is on the stack. This can be either another
6487 string, this time an overlay string, or a buffer. */
6488 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6489 && it->sp > 0)
6490 {
6491 pop_it (it);
6492 if (it->method == GET_FROM_STRING)
6493 goto consider_string_end;
6494 }
6495 }
6496 break;
6497
6498 case GET_FROM_IMAGE:
6499 case GET_FROM_STRETCH:
6500 /* The position etc with which we have to proceed are on
6501 the stack. The position may be at the end of a string,
6502 if the `display' property takes up the whole string. */
6503 xassert (it->sp > 0);
6504 pop_it (it);
6505 if (it->method == GET_FROM_STRING)
6506 goto consider_string_end;
6507 break;
6508
6509 default:
6510 /* There are no other methods defined, so this should be a bug. */
6511 abort ();
6512 }
6513
6514 xassert (it->method != GET_FROM_STRING
6515 || (STRINGP (it->string)
6516 && IT_STRING_CHARPOS (*it) >= 0));
6517 }
6518
6519 /* Load IT's display element fields with information about the next
6520 display element which comes from a display table entry or from the
6521 result of translating a control character to one of the forms `^C'
6522 or `\003'.
6523
6524 IT->dpvec holds the glyphs to return as characters.
6525 IT->saved_face_id holds the face id before the display vector--it
6526 is restored into IT->face_id in set_iterator_to_next. */
6527
6528 static int
6529 next_element_from_display_vector (struct it *it)
6530 {
6531 Lisp_Object gc;
6532
6533 /* Precondition. */
6534 xassert (it->dpvec && it->current.dpvec_index >= 0);
6535
6536 it->face_id = it->saved_face_id;
6537
6538 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6539 That seemed totally bogus - so I changed it... */
6540 gc = it->dpvec[it->current.dpvec_index];
6541
6542 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6543 {
6544 it->c = GLYPH_CODE_CHAR (gc);
6545 it->len = CHAR_BYTES (it->c);
6546
6547 /* The entry may contain a face id to use. Such a face id is
6548 the id of a Lisp face, not a realized face. A face id of
6549 zero means no face is specified. */
6550 if (it->dpvec_face_id >= 0)
6551 it->face_id = it->dpvec_face_id;
6552 else
6553 {
6554 int lface_id = GLYPH_CODE_FACE (gc);
6555 if (lface_id > 0)
6556 it->face_id = merge_faces (it->f, Qt, lface_id,
6557 it->saved_face_id);
6558 }
6559 }
6560 else
6561 /* Display table entry is invalid. Return a space. */
6562 it->c = ' ', it->len = 1;
6563
6564 /* Don't change position and object of the iterator here. They are
6565 still the values of the character that had this display table
6566 entry or was translated, and that's what we want. */
6567 it->what = IT_CHARACTER;
6568 return 1;
6569 }
6570
6571
6572 /* Load IT with the next display element from Lisp string IT->string.
6573 IT->current.string_pos is the current position within the string.
6574 If IT->current.overlay_string_index >= 0, the Lisp string is an
6575 overlay string. */
6576
6577 static int
6578 next_element_from_string (struct it *it)
6579 {
6580 struct text_pos position;
6581
6582 xassert (STRINGP (it->string));
6583 xassert (IT_STRING_CHARPOS (*it) >= 0);
6584 position = it->current.string_pos;
6585
6586 /* With bidi reordering, the character to display might not be the
6587 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
6588 that we were reseat()ed to a new string, whose paragraph
6589 direction is not known. */
6590 if (it->bidi_p && it->bidi_it.first_elt)
6591 {
6592 it->bidi_it.charpos = CHARPOS (position);
6593 it->bidi_it.bytepos = BYTEPOS (position);
6594 if (it->bidi_it.charpos >= it->string_nchars)
6595 {
6596 /* Nothing to do, but reset the FIRST_ELT flag, like
6597 bidi_paragraph_init does, because we are not going to
6598 call it. */
6599 it->bidi_it.first_elt = 0;
6600 }
6601 else if (it->bidi_it.charpos <= 0)
6602 {
6603 /* If we are at the beginning of the string, we can produce
6604 the next element right away. */
6605 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6606 bidi_move_to_visually_next (&it->bidi_it);
6607 }
6608 else
6609 {
6610 EMACS_INT orig_bytepos = BYTEPOS (position);
6611
6612 /* We need to prime the bidi iterator starting at the string
6613 beginning, before we will be able to produce the next
6614 element. */
6615 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6616 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6617 do
6618 {
6619 /* Now return to buffer position where we were asked to
6620 get the next display element, and produce that. */
6621 bidi_move_to_visually_next (&it->bidi_it);
6622 }
6623 while (it->bidi_it.bytepos != orig_bytepos
6624 && it->bidi_it.charpos < it->string_nchars);
6625 }
6626
6627 /* Adjust IT's position information to where we ended up. */
6628 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6629 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6630 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
6631 {
6632 EMACS_INT stop = SCHARS (it->string);
6633
6634 if (it->bidi_it.scan_dir < 0)
6635 stop = -1;
6636 else if (stop > it->end_charpos)
6637 stop = it->end_charpos;
6638 composition_compute_stop_pos (&it->cmp_it, IT_STRING_CHARPOS (*it),
6639 IT_STRING_BYTEPOS (*it), stop,
6640 it->string);
6641 }
6642 }
6643
6644 /* Time to check for invisible text? */
6645 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
6646 {
6647 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
6648 {
6649 if (!(!it->bidi_p
6650 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6651 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
6652 {
6653 /* With bidi non-linear iteration, we could find
6654 ourselves far beyond the last computed stop_charpos,
6655 with several other stop positions in between that we
6656 missed. Scan them all now, in buffer's logical
6657 order, until we find and handle the last stop_charpos
6658 that precedes our current position. */
6659 handle_stop_backwards (it, it->stop_charpos);
6660 return GET_NEXT_DISPLAY_ELEMENT (it);
6661 }
6662 else
6663 {
6664 if (it->bidi_p)
6665 {
6666 /* Take note of the stop position we just moved
6667 across, for when we will move back across it. */
6668 it->prev_stop = it->stop_charpos;
6669 /* If we are at base paragraph embedding level, take
6670 note of the last stop position seen at this
6671 level. */
6672 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6673 it->base_level_stop = it->stop_charpos;
6674 }
6675 handle_stop (it);
6676
6677 /* Since a handler may have changed IT->method, we must
6678 recurse here. */
6679 return GET_NEXT_DISPLAY_ELEMENT (it);
6680 }
6681 }
6682 else if (it->bidi_p
6683 /* If we are before prev_stop, we may have overstepped
6684 on our way backwards a stop_pos, and if so, we need
6685 to handle that stop_pos. */
6686 && IT_STRING_CHARPOS (*it) < it->prev_stop
6687 /* We can sometimes back up for reasons that have nothing
6688 to do with bidi reordering. E.g., compositions. The
6689 code below is only needed when we are above the base
6690 embedding level, so test for that explicitly. */
6691 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
6692 {
6693 /* If we lost track of base_level_stop, we have no better place
6694 for handle_stop_backwards to start from than BEGV. This
6695 happens, e.g., when we were reseated to the previous
6696 screenful of text by vertical-motion. */
6697 if (it->base_level_stop <= 0
6698 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
6699 it->base_level_stop = 0;
6700 handle_stop_backwards (it, it->base_level_stop);
6701 return GET_NEXT_DISPLAY_ELEMENT (it);
6702 }
6703 }
6704
6705 if (it->current.overlay_string_index >= 0)
6706 {
6707 /* Get the next character from an overlay string. In overlay
6708 strings, There is no field width or padding with spaces to
6709 do. */
6710 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6711 {
6712 it->what = IT_EOB;
6713 return 0;
6714 }
6715 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6716 IT_STRING_BYTEPOS (*it),
6717 it->bidi_it.scan_dir < 0
6718 ? -1
6719 : SCHARS (it->string))
6720 && next_element_from_composition (it))
6721 {
6722 return 1;
6723 }
6724 else if (STRING_MULTIBYTE (it->string))
6725 {
6726 const unsigned char *s = (SDATA (it->string)
6727 + IT_STRING_BYTEPOS (*it));
6728 it->c = string_char_and_length (s, &it->len);
6729 }
6730 else
6731 {
6732 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6733 it->len = 1;
6734 }
6735 }
6736 else
6737 {
6738 /* Get the next character from a Lisp string that is not an
6739 overlay string. Such strings come from the mode line, for
6740 example. We may have to pad with spaces, or truncate the
6741 string. See also next_element_from_c_string. */
6742 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6743 {
6744 it->what = IT_EOB;
6745 return 0;
6746 }
6747 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6748 {
6749 /* Pad with spaces. */
6750 it->c = ' ', it->len = 1;
6751 CHARPOS (position) = BYTEPOS (position) = -1;
6752 }
6753 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6754 IT_STRING_BYTEPOS (*it),
6755 it->bidi_it.scan_dir < 0
6756 ? -1
6757 : it->string_nchars)
6758 && next_element_from_composition (it))
6759 {
6760 return 1;
6761 }
6762 else if (STRING_MULTIBYTE (it->string))
6763 {
6764 const unsigned char *s = (SDATA (it->string)
6765 + IT_STRING_BYTEPOS (*it));
6766 it->c = string_char_and_length (s, &it->len);
6767 }
6768 else
6769 {
6770 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6771 it->len = 1;
6772 }
6773 }
6774
6775 /* Record what we have and where it came from. */
6776 it->what = IT_CHARACTER;
6777 it->object = it->string;
6778 it->position = position;
6779 return 1;
6780 }
6781
6782
6783 /* Load IT with next display element from C string IT->s.
6784 IT->string_nchars is the maximum number of characters to return
6785 from the string. IT->end_charpos may be greater than
6786 IT->string_nchars when this function is called, in which case we
6787 may have to return padding spaces. Value is zero if end of string
6788 reached, including padding spaces. */
6789
6790 static int
6791 next_element_from_c_string (struct it *it)
6792 {
6793 int success_p = 1;
6794
6795 xassert (it->s);
6796 it->what = IT_CHARACTER;
6797 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6798 it->object = Qnil;
6799
6800 /* IT's position can be greater IT->string_nchars in case a field
6801 width or precision has been specified when the iterator was
6802 initialized. */
6803 if (IT_CHARPOS (*it) >= it->end_charpos)
6804 {
6805 /* End of the game. */
6806 it->what = IT_EOB;
6807 success_p = 0;
6808 }
6809 else if (IT_CHARPOS (*it) >= it->string_nchars)
6810 {
6811 /* Pad with spaces. */
6812 it->c = ' ', it->len = 1;
6813 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6814 }
6815 else if (it->multibyte_p)
6816 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6817 else
6818 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6819
6820 return success_p;
6821 }
6822
6823
6824 /* Set up IT to return characters from an ellipsis, if appropriate.
6825 The definition of the ellipsis glyphs may come from a display table
6826 entry. This function fills IT with the first glyph from the
6827 ellipsis if an ellipsis is to be displayed. */
6828
6829 static int
6830 next_element_from_ellipsis (struct it *it)
6831 {
6832 if (it->selective_display_ellipsis_p)
6833 setup_for_ellipsis (it, it->len);
6834 else
6835 {
6836 /* The face at the current position may be different from the
6837 face we find after the invisible text. Remember what it
6838 was in IT->saved_face_id, and signal that it's there by
6839 setting face_before_selective_p. */
6840 it->saved_face_id = it->face_id;
6841 it->method = GET_FROM_BUFFER;
6842 it->object = it->w->buffer;
6843 reseat_at_next_visible_line_start (it, 1);
6844 it->face_before_selective_p = 1;
6845 }
6846
6847 return GET_NEXT_DISPLAY_ELEMENT (it);
6848 }
6849
6850
6851 /* Deliver an image display element. The iterator IT is already
6852 filled with image information (done in handle_display_prop). Value
6853 is always 1. */
6854
6855
6856 static int
6857 next_element_from_image (struct it *it)
6858 {
6859 it->what = IT_IMAGE;
6860 it->ignore_overlay_strings_at_pos_p = 0;
6861 return 1;
6862 }
6863
6864
6865 /* Fill iterator IT with next display element from a stretch glyph
6866 property. IT->object is the value of the text property. Value is
6867 always 1. */
6868
6869 static int
6870 next_element_from_stretch (struct it *it)
6871 {
6872 it->what = IT_STRETCH;
6873 return 1;
6874 }
6875
6876 /* Scan forward from CHARPOS in the current buffer/string, until we
6877 find a stop position > current IT's position. Then handle the stop
6878 position before that. This is called when we bump into a stop
6879 position while reordering bidirectional text. CHARPOS should be
6880 the last previously processed stop_pos (or BEGV/0, if none were
6881 processed yet) whose position is less that IT's current
6882 position. */
6883
6884 static void
6885 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6886 {
6887 int bufp = !STRINGP (it->string);
6888 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
6889 struct display_pos save_current = it->current;
6890 struct text_pos save_position = it->position;
6891 struct text_pos pos1;
6892 EMACS_INT next_stop;
6893
6894 /* Scan in strict logical order. */
6895 it->bidi_p = 0;
6896 do
6897 {
6898 it->prev_stop = charpos;
6899 if (bufp)
6900 {
6901 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6902 reseat_1 (it, pos1, 0);
6903 }
6904 else
6905 it->current.string_pos = string_pos (charpos, it->string);
6906 compute_stop_pos (it);
6907 /* We must advance forward, right? */
6908 if (it->stop_charpos <= it->prev_stop)
6909 abort ();
6910 charpos = it->stop_charpos;
6911 }
6912 while (charpos <= where_we_are);
6913
6914 next_stop = it->stop_charpos;
6915 it->stop_charpos = it->prev_stop;
6916 it->bidi_p = 1;
6917 it->current = save_current;
6918 it->position = save_position;
6919 handle_stop (it);
6920 it->stop_charpos = next_stop;
6921 }
6922
6923 /* Load IT with the next display element from current_buffer. Value
6924 is zero if end of buffer reached. IT->stop_charpos is the next
6925 position at which to stop and check for text properties or buffer
6926 end. */
6927
6928 static int
6929 next_element_from_buffer (struct it *it)
6930 {
6931 int success_p = 1;
6932
6933 xassert (IT_CHARPOS (*it) >= BEGV);
6934
6935 /* With bidi reordering, the character to display might not be the
6936 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6937 we were reseat()ed to a new buffer position, which is potentially
6938 a different paragraph. */
6939 if (it->bidi_p && it->bidi_it.first_elt)
6940 {
6941 it->bidi_it.charpos = IT_CHARPOS (*it);
6942 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6943 if (it->bidi_it.bytepos == ZV_BYTE)
6944 {
6945 /* Nothing to do, but reset the FIRST_ELT flag, like
6946 bidi_paragraph_init does, because we are not going to
6947 call it. */
6948 it->bidi_it.first_elt = 0;
6949 }
6950 else if (it->bidi_it.bytepos == BEGV_BYTE
6951 /* FIXME: Should support all Unicode line separators. */
6952 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6953 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6954 {
6955 /* If we are at the beginning of a line, we can produce the
6956 next element right away. */
6957 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6958 bidi_move_to_visually_next (&it->bidi_it);
6959 }
6960 else
6961 {
6962 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6963
6964 /* We need to prime the bidi iterator starting at the line's
6965 beginning, before we will be able to produce the next
6966 element. */
6967 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6968 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6969 it->bidi_it.charpos = IT_CHARPOS (*it);
6970 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6971 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6972 do
6973 {
6974 /* Now return to buffer position where we were asked to
6975 get the next display element, and produce that. */
6976 bidi_move_to_visually_next (&it->bidi_it);
6977 }
6978 while (it->bidi_it.bytepos != orig_bytepos
6979 && it->bidi_it.bytepos < ZV_BYTE);
6980 }
6981
6982 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6983 /* Adjust IT's position information to where we ended up. */
6984 IT_CHARPOS (*it) = it->bidi_it.charpos;
6985 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6986 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6987 {
6988 EMACS_INT stop = it->end_charpos;
6989 if (it->bidi_it.scan_dir < 0)
6990 stop = -1;
6991 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6992 IT_BYTEPOS (*it), stop, Qnil);
6993 }
6994 }
6995
6996 if (IT_CHARPOS (*it) >= it->stop_charpos)
6997 {
6998 if (IT_CHARPOS (*it) >= it->end_charpos)
6999 {
7000 int overlay_strings_follow_p;
7001
7002 /* End of the game, except when overlay strings follow that
7003 haven't been returned yet. */
7004 if (it->overlay_strings_at_end_processed_p)
7005 overlay_strings_follow_p = 0;
7006 else
7007 {
7008 it->overlay_strings_at_end_processed_p = 1;
7009 overlay_strings_follow_p = get_overlay_strings (it, 0);
7010 }
7011
7012 if (overlay_strings_follow_p)
7013 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7014 else
7015 {
7016 it->what = IT_EOB;
7017 it->position = it->current.pos;
7018 success_p = 0;
7019 }
7020 }
7021 else if (!(!it->bidi_p
7022 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7023 || IT_CHARPOS (*it) == it->stop_charpos))
7024 {
7025 /* With bidi non-linear iteration, we could find ourselves
7026 far beyond the last computed stop_charpos, with several
7027 other stop positions in between that we missed. Scan
7028 them all now, in buffer's logical order, until we find
7029 and handle the last stop_charpos that precedes our
7030 current position. */
7031 handle_stop_backwards (it, it->stop_charpos);
7032 return GET_NEXT_DISPLAY_ELEMENT (it);
7033 }
7034 else
7035 {
7036 if (it->bidi_p)
7037 {
7038 /* Take note of the stop position we just moved across,
7039 for when we will move back across it. */
7040 it->prev_stop = it->stop_charpos;
7041 /* If we are at base paragraph embedding level, take
7042 note of the last stop position seen at this
7043 level. */
7044 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7045 it->base_level_stop = it->stop_charpos;
7046 }
7047 handle_stop (it);
7048 return GET_NEXT_DISPLAY_ELEMENT (it);
7049 }
7050 }
7051 else if (it->bidi_p
7052 /* If we are before prev_stop, we may have overstepped on
7053 our way backwards a stop_pos, and if so, we need to
7054 handle that stop_pos. */
7055 && IT_CHARPOS (*it) < it->prev_stop
7056 /* We can sometimes back up for reasons that have nothing
7057 to do with bidi reordering. E.g., compositions. The
7058 code below is only needed when we are above the base
7059 embedding level, so test for that explicitly. */
7060 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7061 {
7062 /* If we lost track of base_level_stop, we have no better place
7063 for handle_stop_backwards to start from than BEGV. This
7064 happens, e.g., when we were reseated to the previous
7065 screenful of text by vertical-motion. */
7066 if (it->base_level_stop <= 0
7067 || IT_CHARPOS (*it) < it->base_level_stop)
7068 it->base_level_stop = BEGV;
7069 handle_stop_backwards (it, it->base_level_stop);
7070 return GET_NEXT_DISPLAY_ELEMENT (it);
7071 }
7072 else
7073 {
7074 /* No face changes, overlays etc. in sight, so just return a
7075 character from current_buffer. */
7076 unsigned char *p;
7077 EMACS_INT stop;
7078
7079 /* Maybe run the redisplay end trigger hook. Performance note:
7080 This doesn't seem to cost measurable time. */
7081 if (it->redisplay_end_trigger_charpos
7082 && it->glyph_row
7083 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7084 run_redisplay_end_trigger_hook (it);
7085
7086 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7087 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7088 stop)
7089 && next_element_from_composition (it))
7090 {
7091 return 1;
7092 }
7093
7094 /* Get the next character, maybe multibyte. */
7095 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7096 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7097 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7098 else
7099 it->c = *p, it->len = 1;
7100
7101 /* Record what we have and where it came from. */
7102 it->what = IT_CHARACTER;
7103 it->object = it->w->buffer;
7104 it->position = it->current.pos;
7105
7106 /* Normally we return the character found above, except when we
7107 really want to return an ellipsis for selective display. */
7108 if (it->selective)
7109 {
7110 if (it->c == '\n')
7111 {
7112 /* A value of selective > 0 means hide lines indented more
7113 than that number of columns. */
7114 if (it->selective > 0
7115 && IT_CHARPOS (*it) + 1 < ZV
7116 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7117 IT_BYTEPOS (*it) + 1,
7118 (double) it->selective)) /* iftc */
7119 {
7120 success_p = next_element_from_ellipsis (it);
7121 it->dpvec_char_len = -1;
7122 }
7123 }
7124 else if (it->c == '\r' && it->selective == -1)
7125 {
7126 /* A value of selective == -1 means that everything from the
7127 CR to the end of the line is invisible, with maybe an
7128 ellipsis displayed for it. */
7129 success_p = next_element_from_ellipsis (it);
7130 it->dpvec_char_len = -1;
7131 }
7132 }
7133 }
7134
7135 /* Value is zero if end of buffer reached. */
7136 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7137 return success_p;
7138 }
7139
7140
7141 /* Run the redisplay end trigger hook for IT. */
7142
7143 static void
7144 run_redisplay_end_trigger_hook (struct it *it)
7145 {
7146 Lisp_Object args[3];
7147
7148 /* IT->glyph_row should be non-null, i.e. we should be actually
7149 displaying something, or otherwise we should not run the hook. */
7150 xassert (it->glyph_row);
7151
7152 /* Set up hook arguments. */
7153 args[0] = Qredisplay_end_trigger_functions;
7154 args[1] = it->window;
7155 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7156 it->redisplay_end_trigger_charpos = 0;
7157
7158 /* Since we are *trying* to run these functions, don't try to run
7159 them again, even if they get an error. */
7160 it->w->redisplay_end_trigger = Qnil;
7161 Frun_hook_with_args (3, args);
7162
7163 /* Notice if it changed the face of the character we are on. */
7164 handle_face_prop (it);
7165 }
7166
7167
7168 /* Deliver a composition display element. Unlike the other
7169 next_element_from_XXX, this function is not registered in the array
7170 get_next_element[]. It is called from next_element_from_buffer and
7171 next_element_from_string when necessary. */
7172
7173 static int
7174 next_element_from_composition (struct it *it)
7175 {
7176 it->what = IT_COMPOSITION;
7177 it->len = it->cmp_it.nbytes;
7178 if (STRINGP (it->string))
7179 {
7180 if (it->c < 0)
7181 {
7182 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7183 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7184 return 0;
7185 }
7186 it->position = it->current.string_pos;
7187 it->object = it->string;
7188 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7189 IT_STRING_BYTEPOS (*it), it->string);
7190 }
7191 else
7192 {
7193 if (it->c < 0)
7194 {
7195 IT_CHARPOS (*it) += it->cmp_it.nchars;
7196 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7197 if (it->bidi_p)
7198 {
7199 if (it->bidi_it.new_paragraph)
7200 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7201 /* Resync the bidi iterator with IT's new position.
7202 FIXME: this doesn't support bidirectional text. */
7203 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7204 bidi_move_to_visually_next (&it->bidi_it);
7205 }
7206 return 0;
7207 }
7208 it->position = it->current.pos;
7209 it->object = it->w->buffer;
7210 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7211 IT_BYTEPOS (*it), Qnil);
7212 }
7213 return 1;
7214 }
7215
7216
7217 \f
7218 /***********************************************************************
7219 Moving an iterator without producing glyphs
7220 ***********************************************************************/
7221
7222 /* Check if iterator is at a position corresponding to a valid buffer
7223 position after some move_it_ call. */
7224
7225 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7226 ((it)->method == GET_FROM_STRING \
7227 ? IT_STRING_CHARPOS (*it) == 0 \
7228 : 1)
7229
7230
7231 /* Move iterator IT to a specified buffer or X position within one
7232 line on the display without producing glyphs.
7233
7234 OP should be a bit mask including some or all of these bits:
7235 MOVE_TO_X: Stop upon reaching x-position TO_X.
7236 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7237 Regardless of OP's value, stop upon reaching the end of the display line.
7238
7239 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7240 This means, in particular, that TO_X includes window's horizontal
7241 scroll amount.
7242
7243 The return value has several possible values that
7244 say what condition caused the scan to stop:
7245
7246 MOVE_POS_MATCH_OR_ZV
7247 - when TO_POS or ZV was reached.
7248
7249 MOVE_X_REACHED
7250 -when TO_X was reached before TO_POS or ZV were reached.
7251
7252 MOVE_LINE_CONTINUED
7253 - when we reached the end of the display area and the line must
7254 be continued.
7255
7256 MOVE_LINE_TRUNCATED
7257 - when we reached the end of the display area and the line is
7258 truncated.
7259
7260 MOVE_NEWLINE_OR_CR
7261 - when we stopped at a line end, i.e. a newline or a CR and selective
7262 display is on. */
7263
7264 static enum move_it_result
7265 move_it_in_display_line_to (struct it *it,
7266 EMACS_INT to_charpos, int to_x,
7267 enum move_operation_enum op)
7268 {
7269 enum move_it_result result = MOVE_UNDEFINED;
7270 struct glyph_row *saved_glyph_row;
7271 struct it wrap_it, atpos_it, atx_it;
7272 int may_wrap = 0;
7273 enum it_method prev_method = it->method;
7274 EMACS_INT prev_pos = IT_CHARPOS (*it);
7275
7276 /* Don't produce glyphs in produce_glyphs. */
7277 saved_glyph_row = it->glyph_row;
7278 it->glyph_row = NULL;
7279
7280 /* Use wrap_it to save a copy of IT wherever a word wrap could
7281 occur. Use atpos_it to save a copy of IT at the desired buffer
7282 position, if found, so that we can scan ahead and check if the
7283 word later overshoots the window edge. Use atx_it similarly, for
7284 pixel positions. */
7285 wrap_it.sp = -1;
7286 atpos_it.sp = -1;
7287 atx_it.sp = -1;
7288
7289 #define BUFFER_POS_REACHED_P() \
7290 ((op & MOVE_TO_POS) != 0 \
7291 && BUFFERP (it->object) \
7292 && (IT_CHARPOS (*it) == to_charpos \
7293 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7294 && (it->method == GET_FROM_BUFFER \
7295 || (it->method == GET_FROM_DISPLAY_VECTOR \
7296 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7297
7298 /* If there's a line-/wrap-prefix, handle it. */
7299 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7300 && it->current_y < it->last_visible_y)
7301 handle_line_prefix (it);
7302
7303 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7304 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7305
7306 while (1)
7307 {
7308 int x, i, ascent = 0, descent = 0;
7309
7310 /* Utility macro to reset an iterator with x, ascent, and descent. */
7311 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7312 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7313 (IT)->max_descent = descent)
7314
7315 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7316 glyph). */
7317 if ((op & MOVE_TO_POS) != 0
7318 && BUFFERP (it->object)
7319 && it->method == GET_FROM_BUFFER
7320 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7321 || (it->bidi_p
7322 && (prev_method == GET_FROM_IMAGE
7323 || prev_method == GET_FROM_STRETCH)
7324 /* Passed TO_CHARPOS from left to right. */
7325 && ((prev_pos < to_charpos
7326 && IT_CHARPOS (*it) > to_charpos)
7327 /* Passed TO_CHARPOS from right to left. */
7328 || (prev_pos > to_charpos
7329 && IT_CHARPOS (*it) < to_charpos)))))
7330 {
7331 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7332 {
7333 result = MOVE_POS_MATCH_OR_ZV;
7334 break;
7335 }
7336 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7337 /* If wrap_it is valid, the current position might be in a
7338 word that is wrapped. So, save the iterator in
7339 atpos_it and continue to see if wrapping happens. */
7340 atpos_it = *it;
7341 }
7342
7343 prev_method = it->method;
7344 if (it->method == GET_FROM_BUFFER)
7345 prev_pos = IT_CHARPOS (*it);
7346 /* Stop when ZV reached.
7347 We used to stop here when TO_CHARPOS reached as well, but that is
7348 too soon if this glyph does not fit on this line. So we handle it
7349 explicitly below. */
7350 if (!get_next_display_element (it))
7351 {
7352 result = MOVE_POS_MATCH_OR_ZV;
7353 break;
7354 }
7355
7356 if (it->line_wrap == TRUNCATE)
7357 {
7358 if (BUFFER_POS_REACHED_P ())
7359 {
7360 result = MOVE_POS_MATCH_OR_ZV;
7361 break;
7362 }
7363 }
7364 else
7365 {
7366 if (it->line_wrap == WORD_WRAP)
7367 {
7368 if (IT_DISPLAYING_WHITESPACE (it))
7369 may_wrap = 1;
7370 else if (may_wrap)
7371 {
7372 /* We have reached a glyph that follows one or more
7373 whitespace characters. If the position is
7374 already found, we are done. */
7375 if (atpos_it.sp >= 0)
7376 {
7377 *it = atpos_it;
7378 result = MOVE_POS_MATCH_OR_ZV;
7379 goto done;
7380 }
7381 if (atx_it.sp >= 0)
7382 {
7383 *it = atx_it;
7384 result = MOVE_X_REACHED;
7385 goto done;
7386 }
7387 /* Otherwise, we can wrap here. */
7388 wrap_it = *it;
7389 may_wrap = 0;
7390 }
7391 }
7392 }
7393
7394 /* Remember the line height for the current line, in case
7395 the next element doesn't fit on the line. */
7396 ascent = it->max_ascent;
7397 descent = it->max_descent;
7398
7399 /* The call to produce_glyphs will get the metrics of the
7400 display element IT is loaded with. Record the x-position
7401 before this display element, in case it doesn't fit on the
7402 line. */
7403 x = it->current_x;
7404
7405 PRODUCE_GLYPHS (it);
7406
7407 if (it->area != TEXT_AREA)
7408 {
7409 set_iterator_to_next (it, 1);
7410 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7411 SET_TEXT_POS (this_line_min_pos,
7412 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7413 continue;
7414 }
7415
7416 /* The number of glyphs we get back in IT->nglyphs will normally
7417 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7418 character on a terminal frame, or (iii) a line end. For the
7419 second case, IT->nglyphs - 1 padding glyphs will be present.
7420 (On X frames, there is only one glyph produced for a
7421 composite character.)
7422
7423 The behavior implemented below means, for continuation lines,
7424 that as many spaces of a TAB as fit on the current line are
7425 displayed there. For terminal frames, as many glyphs of a
7426 multi-glyph character are displayed in the current line, too.
7427 This is what the old redisplay code did, and we keep it that
7428 way. Under X, the whole shape of a complex character must
7429 fit on the line or it will be completely displayed in the
7430 next line.
7431
7432 Note that both for tabs and padding glyphs, all glyphs have
7433 the same width. */
7434 if (it->nglyphs)
7435 {
7436 /* More than one glyph or glyph doesn't fit on line. All
7437 glyphs have the same width. */
7438 int single_glyph_width = it->pixel_width / it->nglyphs;
7439 int new_x;
7440 int x_before_this_char = x;
7441 int hpos_before_this_char = it->hpos;
7442
7443 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7444 {
7445 new_x = x + single_glyph_width;
7446
7447 /* We want to leave anything reaching TO_X to the caller. */
7448 if ((op & MOVE_TO_X) && new_x > to_x)
7449 {
7450 if (BUFFER_POS_REACHED_P ())
7451 {
7452 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7453 goto buffer_pos_reached;
7454 if (atpos_it.sp < 0)
7455 {
7456 atpos_it = *it;
7457 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7458 }
7459 }
7460 else
7461 {
7462 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7463 {
7464 it->current_x = x;
7465 result = MOVE_X_REACHED;
7466 break;
7467 }
7468 if (atx_it.sp < 0)
7469 {
7470 atx_it = *it;
7471 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7472 }
7473 }
7474 }
7475
7476 if (/* Lines are continued. */
7477 it->line_wrap != TRUNCATE
7478 && (/* And glyph doesn't fit on the line. */
7479 new_x > it->last_visible_x
7480 /* Or it fits exactly and we're on a window
7481 system frame. */
7482 || (new_x == it->last_visible_x
7483 && FRAME_WINDOW_P (it->f))))
7484 {
7485 if (/* IT->hpos == 0 means the very first glyph
7486 doesn't fit on the line, e.g. a wide image. */
7487 it->hpos == 0
7488 || (new_x == it->last_visible_x
7489 && FRAME_WINDOW_P (it->f)))
7490 {
7491 ++it->hpos;
7492 it->current_x = new_x;
7493
7494 /* The character's last glyph just barely fits
7495 in this row. */
7496 if (i == it->nglyphs - 1)
7497 {
7498 /* If this is the destination position,
7499 return a position *before* it in this row,
7500 now that we know it fits in this row. */
7501 if (BUFFER_POS_REACHED_P ())
7502 {
7503 if (it->line_wrap != WORD_WRAP
7504 || wrap_it.sp < 0)
7505 {
7506 it->hpos = hpos_before_this_char;
7507 it->current_x = x_before_this_char;
7508 result = MOVE_POS_MATCH_OR_ZV;
7509 break;
7510 }
7511 if (it->line_wrap == WORD_WRAP
7512 && atpos_it.sp < 0)
7513 {
7514 atpos_it = *it;
7515 atpos_it.current_x = x_before_this_char;
7516 atpos_it.hpos = hpos_before_this_char;
7517 }
7518 }
7519
7520 set_iterator_to_next (it, 1);
7521 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7522 SET_TEXT_POS (this_line_min_pos,
7523 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7524 /* On graphical terminals, newlines may
7525 "overflow" into the fringe if
7526 overflow-newline-into-fringe is non-nil.
7527 On text-only terminals, newlines may
7528 overflow into the last glyph on the
7529 display line.*/
7530 if (!FRAME_WINDOW_P (it->f)
7531 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7532 {
7533 if (!get_next_display_element (it))
7534 {
7535 result = MOVE_POS_MATCH_OR_ZV;
7536 break;
7537 }
7538 if (BUFFER_POS_REACHED_P ())
7539 {
7540 if (ITERATOR_AT_END_OF_LINE_P (it))
7541 result = MOVE_POS_MATCH_OR_ZV;
7542 else
7543 result = MOVE_LINE_CONTINUED;
7544 break;
7545 }
7546 if (ITERATOR_AT_END_OF_LINE_P (it))
7547 {
7548 result = MOVE_NEWLINE_OR_CR;
7549 break;
7550 }
7551 }
7552 }
7553 }
7554 else
7555 IT_RESET_X_ASCENT_DESCENT (it);
7556
7557 if (wrap_it.sp >= 0)
7558 {
7559 *it = wrap_it;
7560 atpos_it.sp = -1;
7561 atx_it.sp = -1;
7562 }
7563
7564 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7565 IT_CHARPOS (*it)));
7566 result = MOVE_LINE_CONTINUED;
7567 break;
7568 }
7569
7570 if (BUFFER_POS_REACHED_P ())
7571 {
7572 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7573 goto buffer_pos_reached;
7574 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7575 {
7576 atpos_it = *it;
7577 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7578 }
7579 }
7580
7581 if (new_x > it->first_visible_x)
7582 {
7583 /* Glyph is visible. Increment number of glyphs that
7584 would be displayed. */
7585 ++it->hpos;
7586 }
7587 }
7588
7589 if (result != MOVE_UNDEFINED)
7590 break;
7591 }
7592 else if (BUFFER_POS_REACHED_P ())
7593 {
7594 buffer_pos_reached:
7595 IT_RESET_X_ASCENT_DESCENT (it);
7596 result = MOVE_POS_MATCH_OR_ZV;
7597 break;
7598 }
7599 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7600 {
7601 /* Stop when TO_X specified and reached. This check is
7602 necessary here because of lines consisting of a line end,
7603 only. The line end will not produce any glyphs and we
7604 would never get MOVE_X_REACHED. */
7605 xassert (it->nglyphs == 0);
7606 result = MOVE_X_REACHED;
7607 break;
7608 }
7609
7610 /* Is this a line end? If yes, we're done. */
7611 if (ITERATOR_AT_END_OF_LINE_P (it))
7612 {
7613 result = MOVE_NEWLINE_OR_CR;
7614 break;
7615 }
7616
7617 if (it->method == GET_FROM_BUFFER)
7618 prev_pos = IT_CHARPOS (*it);
7619 /* The current display element has been consumed. Advance
7620 to the next. */
7621 set_iterator_to_next (it, 1);
7622 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7623 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7624
7625 /* Stop if lines are truncated and IT's current x-position is
7626 past the right edge of the window now. */
7627 if (it->line_wrap == TRUNCATE
7628 && it->current_x >= it->last_visible_x)
7629 {
7630 if (!FRAME_WINDOW_P (it->f)
7631 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7632 {
7633 if (!get_next_display_element (it)
7634 || BUFFER_POS_REACHED_P ())
7635 {
7636 result = MOVE_POS_MATCH_OR_ZV;
7637 break;
7638 }
7639 if (ITERATOR_AT_END_OF_LINE_P (it))
7640 {
7641 result = MOVE_NEWLINE_OR_CR;
7642 break;
7643 }
7644 }
7645 result = MOVE_LINE_TRUNCATED;
7646 break;
7647 }
7648 #undef IT_RESET_X_ASCENT_DESCENT
7649 }
7650
7651 #undef BUFFER_POS_REACHED_P
7652
7653 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7654 restore the saved iterator. */
7655 if (atpos_it.sp >= 0)
7656 *it = atpos_it;
7657 else if (atx_it.sp >= 0)
7658 *it = atx_it;
7659
7660 done:
7661
7662 /* Restore the iterator settings altered at the beginning of this
7663 function. */
7664 it->glyph_row = saved_glyph_row;
7665 return result;
7666 }
7667
7668 /* For external use. */
7669 void
7670 move_it_in_display_line (struct it *it,
7671 EMACS_INT to_charpos, int to_x,
7672 enum move_operation_enum op)
7673 {
7674 if (it->line_wrap == WORD_WRAP
7675 && (op & MOVE_TO_X))
7676 {
7677 struct it save_it = *it;
7678 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7679 /* When word-wrap is on, TO_X may lie past the end
7680 of a wrapped line. Then it->current is the
7681 character on the next line, so backtrack to the
7682 space before the wrap point. */
7683 if (skip == MOVE_LINE_CONTINUED)
7684 {
7685 int prev_x = max (it->current_x - 1, 0);
7686 *it = save_it;
7687 move_it_in_display_line_to
7688 (it, -1, prev_x, MOVE_TO_X);
7689 }
7690 }
7691 else
7692 move_it_in_display_line_to (it, to_charpos, to_x, op);
7693 }
7694
7695
7696 /* Move IT forward until it satisfies one or more of the criteria in
7697 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7698
7699 OP is a bit-mask that specifies where to stop, and in particular,
7700 which of those four position arguments makes a difference. See the
7701 description of enum move_operation_enum.
7702
7703 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7704 screen line, this function will set IT to the next position >
7705 TO_CHARPOS. */
7706
7707 void
7708 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7709 {
7710 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7711 int line_height, line_start_x = 0, reached = 0;
7712
7713 for (;;)
7714 {
7715 if (op & MOVE_TO_VPOS)
7716 {
7717 /* If no TO_CHARPOS and no TO_X specified, stop at the
7718 start of the line TO_VPOS. */
7719 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7720 {
7721 if (it->vpos == to_vpos)
7722 {
7723 reached = 1;
7724 break;
7725 }
7726 else
7727 skip = move_it_in_display_line_to (it, -1, -1, 0);
7728 }
7729 else
7730 {
7731 /* TO_VPOS >= 0 means stop at TO_X in the line at
7732 TO_VPOS, or at TO_POS, whichever comes first. */
7733 if (it->vpos == to_vpos)
7734 {
7735 reached = 2;
7736 break;
7737 }
7738
7739 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7740
7741 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7742 {
7743 reached = 3;
7744 break;
7745 }
7746 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7747 {
7748 /* We have reached TO_X but not in the line we want. */
7749 skip = move_it_in_display_line_to (it, to_charpos,
7750 -1, MOVE_TO_POS);
7751 if (skip == MOVE_POS_MATCH_OR_ZV)
7752 {
7753 reached = 4;
7754 break;
7755 }
7756 }
7757 }
7758 }
7759 else if (op & MOVE_TO_Y)
7760 {
7761 struct it it_backup;
7762
7763 if (it->line_wrap == WORD_WRAP)
7764 it_backup = *it;
7765
7766 /* TO_Y specified means stop at TO_X in the line containing
7767 TO_Y---or at TO_CHARPOS if this is reached first. The
7768 problem is that we can't really tell whether the line
7769 contains TO_Y before we have completely scanned it, and
7770 this may skip past TO_X. What we do is to first scan to
7771 TO_X.
7772
7773 If TO_X is not specified, use a TO_X of zero. The reason
7774 is to make the outcome of this function more predictable.
7775 If we didn't use TO_X == 0, we would stop at the end of
7776 the line which is probably not what a caller would expect
7777 to happen. */
7778 skip = move_it_in_display_line_to
7779 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7780 (MOVE_TO_X | (op & MOVE_TO_POS)));
7781
7782 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7783 if (skip == MOVE_POS_MATCH_OR_ZV)
7784 reached = 5;
7785 else if (skip == MOVE_X_REACHED)
7786 {
7787 /* If TO_X was reached, we want to know whether TO_Y is
7788 in the line. We know this is the case if the already
7789 scanned glyphs make the line tall enough. Otherwise,
7790 we must check by scanning the rest of the line. */
7791 line_height = it->max_ascent + it->max_descent;
7792 if (to_y >= it->current_y
7793 && to_y < it->current_y + line_height)
7794 {
7795 reached = 6;
7796 break;
7797 }
7798 it_backup = *it;
7799 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7800 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7801 op & MOVE_TO_POS);
7802 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7803 line_height = it->max_ascent + it->max_descent;
7804 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7805
7806 if (to_y >= it->current_y
7807 && to_y < it->current_y + line_height)
7808 {
7809 /* If TO_Y is in this line and TO_X was reached
7810 above, we scanned too far. We have to restore
7811 IT's settings to the ones before skipping. */
7812 *it = it_backup;
7813 reached = 6;
7814 }
7815 else
7816 {
7817 skip = skip2;
7818 if (skip == MOVE_POS_MATCH_OR_ZV)
7819 reached = 7;
7820 }
7821 }
7822 else
7823 {
7824 /* Check whether TO_Y is in this line. */
7825 line_height = it->max_ascent + it->max_descent;
7826 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7827
7828 if (to_y >= it->current_y
7829 && to_y < it->current_y + line_height)
7830 {
7831 /* When word-wrap is on, TO_X may lie past the end
7832 of a wrapped line. Then it->current is the
7833 character on the next line, so backtrack to the
7834 space before the wrap point. */
7835 if (skip == MOVE_LINE_CONTINUED
7836 && it->line_wrap == WORD_WRAP)
7837 {
7838 int prev_x = max (it->current_x - 1, 0);
7839 *it = it_backup;
7840 skip = move_it_in_display_line_to
7841 (it, -1, prev_x, MOVE_TO_X);
7842 }
7843 reached = 6;
7844 }
7845 }
7846
7847 if (reached)
7848 break;
7849 }
7850 else if (BUFFERP (it->object)
7851 && (it->method == GET_FROM_BUFFER
7852 || it->method == GET_FROM_STRETCH)
7853 && IT_CHARPOS (*it) >= to_charpos)
7854 skip = MOVE_POS_MATCH_OR_ZV;
7855 else
7856 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7857
7858 switch (skip)
7859 {
7860 case MOVE_POS_MATCH_OR_ZV:
7861 reached = 8;
7862 goto out;
7863
7864 case MOVE_NEWLINE_OR_CR:
7865 set_iterator_to_next (it, 1);
7866 it->continuation_lines_width = 0;
7867 break;
7868
7869 case MOVE_LINE_TRUNCATED:
7870 it->continuation_lines_width = 0;
7871 reseat_at_next_visible_line_start (it, 0);
7872 if ((op & MOVE_TO_POS) != 0
7873 && IT_CHARPOS (*it) > to_charpos)
7874 {
7875 reached = 9;
7876 goto out;
7877 }
7878 break;
7879
7880 case MOVE_LINE_CONTINUED:
7881 /* For continued lines ending in a tab, some of the glyphs
7882 associated with the tab are displayed on the current
7883 line. Since it->current_x does not include these glyphs,
7884 we use it->last_visible_x instead. */
7885 if (it->c == '\t')
7886 {
7887 it->continuation_lines_width += it->last_visible_x;
7888 /* When moving by vpos, ensure that the iterator really
7889 advances to the next line (bug#847, bug#969). Fixme:
7890 do we need to do this in other circumstances? */
7891 if (it->current_x != it->last_visible_x
7892 && (op & MOVE_TO_VPOS)
7893 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7894 {
7895 line_start_x = it->current_x + it->pixel_width
7896 - it->last_visible_x;
7897 set_iterator_to_next (it, 0);
7898 }
7899 }
7900 else
7901 it->continuation_lines_width += it->current_x;
7902 break;
7903
7904 default:
7905 abort ();
7906 }
7907
7908 /* Reset/increment for the next run. */
7909 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7910 it->current_x = line_start_x;
7911 line_start_x = 0;
7912 it->hpos = 0;
7913 it->current_y += it->max_ascent + it->max_descent;
7914 ++it->vpos;
7915 last_height = it->max_ascent + it->max_descent;
7916 last_max_ascent = it->max_ascent;
7917 it->max_ascent = it->max_descent = 0;
7918 }
7919
7920 out:
7921
7922 /* On text terminals, we may stop at the end of a line in the middle
7923 of a multi-character glyph. If the glyph itself is continued,
7924 i.e. it is actually displayed on the next line, don't treat this
7925 stopping point as valid; move to the next line instead (unless
7926 that brings us offscreen). */
7927 if (!FRAME_WINDOW_P (it->f)
7928 && op & MOVE_TO_POS
7929 && IT_CHARPOS (*it) == to_charpos
7930 && it->what == IT_CHARACTER
7931 && it->nglyphs > 1
7932 && it->line_wrap == WINDOW_WRAP
7933 && it->current_x == it->last_visible_x - 1
7934 && it->c != '\n'
7935 && it->c != '\t'
7936 && it->vpos < XFASTINT (it->w->window_end_vpos))
7937 {
7938 it->continuation_lines_width += it->current_x;
7939 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7940 it->current_y += it->max_ascent + it->max_descent;
7941 ++it->vpos;
7942 last_height = it->max_ascent + it->max_descent;
7943 last_max_ascent = it->max_ascent;
7944 }
7945
7946 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7947 }
7948
7949
7950 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7951
7952 If DY > 0, move IT backward at least that many pixels. DY = 0
7953 means move IT backward to the preceding line start or BEGV. This
7954 function may move over more than DY pixels if IT->current_y - DY
7955 ends up in the middle of a line; in this case IT->current_y will be
7956 set to the top of the line moved to. */
7957
7958 void
7959 move_it_vertically_backward (struct it *it, int dy)
7960 {
7961 int nlines, h;
7962 struct it it2, it3;
7963 EMACS_INT start_pos;
7964
7965 move_further_back:
7966 xassert (dy >= 0);
7967
7968 start_pos = IT_CHARPOS (*it);
7969
7970 /* Estimate how many newlines we must move back. */
7971 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7972
7973 /* Set the iterator's position that many lines back. */
7974 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7975 back_to_previous_visible_line_start (it);
7976
7977 /* Reseat the iterator here. When moving backward, we don't want
7978 reseat to skip forward over invisible text, set up the iterator
7979 to deliver from overlay strings at the new position etc. So,
7980 use reseat_1 here. */
7981 reseat_1 (it, it->current.pos, 1);
7982
7983 /* We are now surely at a line start. */
7984 it->current_x = it->hpos = 0;
7985 it->continuation_lines_width = 0;
7986
7987 /* Move forward and see what y-distance we moved. First move to the
7988 start of the next line so that we get its height. We need this
7989 height to be able to tell whether we reached the specified
7990 y-distance. */
7991 it2 = *it;
7992 it2.max_ascent = it2.max_descent = 0;
7993 do
7994 {
7995 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7996 MOVE_TO_POS | MOVE_TO_VPOS);
7997 }
7998 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7999 xassert (IT_CHARPOS (*it) >= BEGV);
8000 it3 = it2;
8001
8002 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8003 xassert (IT_CHARPOS (*it) >= BEGV);
8004 /* H is the actual vertical distance from the position in *IT
8005 and the starting position. */
8006 h = it2.current_y - it->current_y;
8007 /* NLINES is the distance in number of lines. */
8008 nlines = it2.vpos - it->vpos;
8009
8010 /* Correct IT's y and vpos position
8011 so that they are relative to the starting point. */
8012 it->vpos -= nlines;
8013 it->current_y -= h;
8014
8015 if (dy == 0)
8016 {
8017 /* DY == 0 means move to the start of the screen line. The
8018 value of nlines is > 0 if continuation lines were involved. */
8019 if (nlines > 0)
8020 move_it_by_lines (it, nlines);
8021 }
8022 else
8023 {
8024 /* The y-position we try to reach, relative to *IT.
8025 Note that H has been subtracted in front of the if-statement. */
8026 int target_y = it->current_y + h - dy;
8027 int y0 = it3.current_y;
8028 int y1 = line_bottom_y (&it3);
8029 int line_height = y1 - y0;
8030
8031 /* If we did not reach target_y, try to move further backward if
8032 we can. If we moved too far backward, try to move forward. */
8033 if (target_y < it->current_y
8034 /* This is heuristic. In a window that's 3 lines high, with
8035 a line height of 13 pixels each, recentering with point
8036 on the bottom line will try to move -39/2 = 19 pixels
8037 backward. Try to avoid moving into the first line. */
8038 && (it->current_y - target_y
8039 > min (window_box_height (it->w), line_height * 2 / 3))
8040 && IT_CHARPOS (*it) > BEGV)
8041 {
8042 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8043 target_y - it->current_y));
8044 dy = it->current_y - target_y;
8045 goto move_further_back;
8046 }
8047 else if (target_y >= it->current_y + line_height
8048 && IT_CHARPOS (*it) < ZV)
8049 {
8050 /* Should move forward by at least one line, maybe more.
8051
8052 Note: Calling move_it_by_lines can be expensive on
8053 terminal frames, where compute_motion is used (via
8054 vmotion) to do the job, when there are very long lines
8055 and truncate-lines is nil. That's the reason for
8056 treating terminal frames specially here. */
8057
8058 if (!FRAME_WINDOW_P (it->f))
8059 move_it_vertically (it, target_y - (it->current_y + line_height));
8060 else
8061 {
8062 do
8063 {
8064 move_it_by_lines (it, 1);
8065 }
8066 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8067 }
8068 }
8069 }
8070 }
8071
8072
8073 /* Move IT by a specified amount of pixel lines DY. DY negative means
8074 move backwards. DY = 0 means move to start of screen line. At the
8075 end, IT will be on the start of a screen line. */
8076
8077 void
8078 move_it_vertically (struct it *it, int dy)
8079 {
8080 if (dy <= 0)
8081 move_it_vertically_backward (it, -dy);
8082 else
8083 {
8084 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8085 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8086 MOVE_TO_POS | MOVE_TO_Y);
8087 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8088
8089 /* If buffer ends in ZV without a newline, move to the start of
8090 the line to satisfy the post-condition. */
8091 if (IT_CHARPOS (*it) == ZV
8092 && ZV > BEGV
8093 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8094 move_it_by_lines (it, 0);
8095 }
8096 }
8097
8098
8099 /* Move iterator IT past the end of the text line it is in. */
8100
8101 void
8102 move_it_past_eol (struct it *it)
8103 {
8104 enum move_it_result rc;
8105
8106 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8107 if (rc == MOVE_NEWLINE_OR_CR)
8108 set_iterator_to_next (it, 0);
8109 }
8110
8111
8112 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8113 negative means move up. DVPOS == 0 means move to the start of the
8114 screen line.
8115
8116 Optimization idea: If we would know that IT->f doesn't use
8117 a face with proportional font, we could be faster for
8118 truncate-lines nil. */
8119
8120 void
8121 move_it_by_lines (struct it *it, int dvpos)
8122 {
8123
8124 /* The commented-out optimization uses vmotion on terminals. This
8125 gives bad results, because elements like it->what, on which
8126 callers such as pos_visible_p rely, aren't updated. */
8127 /* struct position pos;
8128 if (!FRAME_WINDOW_P (it->f))
8129 {
8130 struct text_pos textpos;
8131
8132 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8133 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8134 reseat (it, textpos, 1);
8135 it->vpos += pos.vpos;
8136 it->current_y += pos.vpos;
8137 }
8138 else */
8139
8140 if (dvpos == 0)
8141 {
8142 /* DVPOS == 0 means move to the start of the screen line. */
8143 move_it_vertically_backward (it, 0);
8144 xassert (it->current_x == 0 && it->hpos == 0);
8145 /* Let next call to line_bottom_y calculate real line height */
8146 last_height = 0;
8147 }
8148 else if (dvpos > 0)
8149 {
8150 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8151 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8152 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8153 }
8154 else
8155 {
8156 struct it it2;
8157 EMACS_INT start_charpos, i;
8158
8159 /* Start at the beginning of the screen line containing IT's
8160 position. This may actually move vertically backwards,
8161 in case of overlays, so adjust dvpos accordingly. */
8162 dvpos += it->vpos;
8163 move_it_vertically_backward (it, 0);
8164 dvpos -= it->vpos;
8165
8166 /* Go back -DVPOS visible lines and reseat the iterator there. */
8167 start_charpos = IT_CHARPOS (*it);
8168 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8169 back_to_previous_visible_line_start (it);
8170 reseat (it, it->current.pos, 1);
8171
8172 /* Move further back if we end up in a string or an image. */
8173 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8174 {
8175 /* First try to move to start of display line. */
8176 dvpos += it->vpos;
8177 move_it_vertically_backward (it, 0);
8178 dvpos -= it->vpos;
8179 if (IT_POS_VALID_AFTER_MOVE_P (it))
8180 break;
8181 /* If start of line is still in string or image,
8182 move further back. */
8183 back_to_previous_visible_line_start (it);
8184 reseat (it, it->current.pos, 1);
8185 dvpos--;
8186 }
8187
8188 it->current_x = it->hpos = 0;
8189
8190 /* Above call may have moved too far if continuation lines
8191 are involved. Scan forward and see if it did. */
8192 it2 = *it;
8193 it2.vpos = it2.current_y = 0;
8194 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8195 it->vpos -= it2.vpos;
8196 it->current_y -= it2.current_y;
8197 it->current_x = it->hpos = 0;
8198
8199 /* If we moved too far back, move IT some lines forward. */
8200 if (it2.vpos > -dvpos)
8201 {
8202 int delta = it2.vpos + dvpos;
8203 it2 = *it;
8204 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8205 /* Move back again if we got too far ahead. */
8206 if (IT_CHARPOS (*it) >= start_charpos)
8207 *it = it2;
8208 }
8209 }
8210 }
8211
8212 /* Return 1 if IT points into the middle of a display vector. */
8213
8214 int
8215 in_display_vector_p (struct it *it)
8216 {
8217 return (it->method == GET_FROM_DISPLAY_VECTOR
8218 && it->current.dpvec_index > 0
8219 && it->dpvec + it->current.dpvec_index != it->dpend);
8220 }
8221
8222 \f
8223 /***********************************************************************
8224 Messages
8225 ***********************************************************************/
8226
8227
8228 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8229 to *Messages*. */
8230
8231 void
8232 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8233 {
8234 Lisp_Object args[3];
8235 Lisp_Object msg, fmt;
8236 char *buffer;
8237 EMACS_INT len;
8238 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8239 USE_SAFE_ALLOCA;
8240
8241 /* Do nothing if called asynchronously. Inserting text into
8242 a buffer may call after-change-functions and alike and
8243 that would means running Lisp asynchronously. */
8244 if (handling_signal)
8245 return;
8246
8247 fmt = msg = Qnil;
8248 GCPRO4 (fmt, msg, arg1, arg2);
8249
8250 args[0] = fmt = build_string (format);
8251 args[1] = arg1;
8252 args[2] = arg2;
8253 msg = Fformat (3, args);
8254
8255 len = SBYTES (msg) + 1;
8256 SAFE_ALLOCA (buffer, char *, len);
8257 memcpy (buffer, SDATA (msg), len);
8258
8259 message_dolog (buffer, len - 1, 1, 0);
8260 SAFE_FREE ();
8261
8262 UNGCPRO;
8263 }
8264
8265
8266 /* Output a newline in the *Messages* buffer if "needs" one. */
8267
8268 void
8269 message_log_maybe_newline (void)
8270 {
8271 if (message_log_need_newline)
8272 message_dolog ("", 0, 1, 0);
8273 }
8274
8275
8276 /* Add a string M of length NBYTES to the message log, optionally
8277 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8278 nonzero, means interpret the contents of M as multibyte. This
8279 function calls low-level routines in order to bypass text property
8280 hooks, etc. which might not be safe to run.
8281
8282 This may GC (insert may run before/after change hooks),
8283 so the buffer M must NOT point to a Lisp string. */
8284
8285 void
8286 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8287 {
8288 const unsigned char *msg = (const unsigned char *) m;
8289
8290 if (!NILP (Vmemory_full))
8291 return;
8292
8293 if (!NILP (Vmessage_log_max))
8294 {
8295 struct buffer *oldbuf;
8296 Lisp_Object oldpoint, oldbegv, oldzv;
8297 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8298 EMACS_INT point_at_end = 0;
8299 EMACS_INT zv_at_end = 0;
8300 Lisp_Object old_deactivate_mark, tem;
8301 struct gcpro gcpro1;
8302
8303 old_deactivate_mark = Vdeactivate_mark;
8304 oldbuf = current_buffer;
8305 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8306 BVAR (current_buffer, undo_list) = Qt;
8307
8308 oldpoint = message_dolog_marker1;
8309 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8310 oldbegv = message_dolog_marker2;
8311 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8312 oldzv = message_dolog_marker3;
8313 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8314 GCPRO1 (old_deactivate_mark);
8315
8316 if (PT == Z)
8317 point_at_end = 1;
8318 if (ZV == Z)
8319 zv_at_end = 1;
8320
8321 BEGV = BEG;
8322 BEGV_BYTE = BEG_BYTE;
8323 ZV = Z;
8324 ZV_BYTE = Z_BYTE;
8325 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8326
8327 /* Insert the string--maybe converting multibyte to single byte
8328 or vice versa, so that all the text fits the buffer. */
8329 if (multibyte
8330 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8331 {
8332 EMACS_INT i;
8333 int c, char_bytes;
8334 char work[1];
8335
8336 /* Convert a multibyte string to single-byte
8337 for the *Message* buffer. */
8338 for (i = 0; i < nbytes; i += char_bytes)
8339 {
8340 c = string_char_and_length (msg + i, &char_bytes);
8341 work[0] = (ASCII_CHAR_P (c)
8342 ? c
8343 : multibyte_char_to_unibyte (c));
8344 insert_1_both (work, 1, 1, 1, 0, 0);
8345 }
8346 }
8347 else if (! multibyte
8348 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8349 {
8350 EMACS_INT i;
8351 int c, char_bytes;
8352 unsigned char str[MAX_MULTIBYTE_LENGTH];
8353 /* Convert a single-byte string to multibyte
8354 for the *Message* buffer. */
8355 for (i = 0; i < nbytes; i++)
8356 {
8357 c = msg[i];
8358 MAKE_CHAR_MULTIBYTE (c);
8359 char_bytes = CHAR_STRING (c, str);
8360 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8361 }
8362 }
8363 else if (nbytes)
8364 insert_1 (m, nbytes, 1, 0, 0);
8365
8366 if (nlflag)
8367 {
8368 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8369 unsigned long int dups;
8370 insert_1 ("\n", 1, 1, 0, 0);
8371
8372 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8373 this_bol = PT;
8374 this_bol_byte = PT_BYTE;
8375
8376 /* See if this line duplicates the previous one.
8377 If so, combine duplicates. */
8378 if (this_bol > BEG)
8379 {
8380 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8381 prev_bol = PT;
8382 prev_bol_byte = PT_BYTE;
8383
8384 dups = message_log_check_duplicate (prev_bol_byte,
8385 this_bol_byte);
8386 if (dups)
8387 {
8388 del_range_both (prev_bol, prev_bol_byte,
8389 this_bol, this_bol_byte, 0);
8390 if (dups > 1)
8391 {
8392 char dupstr[40];
8393 int duplen;
8394
8395 /* If you change this format, don't forget to also
8396 change message_log_check_duplicate. */
8397 sprintf (dupstr, " [%lu times]", dups);
8398 duplen = strlen (dupstr);
8399 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8400 insert_1 (dupstr, duplen, 1, 0, 1);
8401 }
8402 }
8403 }
8404
8405 /* If we have more than the desired maximum number of lines
8406 in the *Messages* buffer now, delete the oldest ones.
8407 This is safe because we don't have undo in this buffer. */
8408
8409 if (NATNUMP (Vmessage_log_max))
8410 {
8411 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8412 -XFASTINT (Vmessage_log_max) - 1, 0);
8413 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8414 }
8415 }
8416 BEGV = XMARKER (oldbegv)->charpos;
8417 BEGV_BYTE = marker_byte_position (oldbegv);
8418
8419 if (zv_at_end)
8420 {
8421 ZV = Z;
8422 ZV_BYTE = Z_BYTE;
8423 }
8424 else
8425 {
8426 ZV = XMARKER (oldzv)->charpos;
8427 ZV_BYTE = marker_byte_position (oldzv);
8428 }
8429
8430 if (point_at_end)
8431 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8432 else
8433 /* We can't do Fgoto_char (oldpoint) because it will run some
8434 Lisp code. */
8435 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8436 XMARKER (oldpoint)->bytepos);
8437
8438 UNGCPRO;
8439 unchain_marker (XMARKER (oldpoint));
8440 unchain_marker (XMARKER (oldbegv));
8441 unchain_marker (XMARKER (oldzv));
8442
8443 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8444 set_buffer_internal (oldbuf);
8445 if (NILP (tem))
8446 windows_or_buffers_changed = old_windows_or_buffers_changed;
8447 message_log_need_newline = !nlflag;
8448 Vdeactivate_mark = old_deactivate_mark;
8449 }
8450 }
8451
8452
8453 /* We are at the end of the buffer after just having inserted a newline.
8454 (Note: We depend on the fact we won't be crossing the gap.)
8455 Check to see if the most recent message looks a lot like the previous one.
8456 Return 0 if different, 1 if the new one should just replace it, or a
8457 value N > 1 if we should also append " [N times]". */
8458
8459 static unsigned long int
8460 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8461 {
8462 EMACS_INT i;
8463 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8464 int seen_dots = 0;
8465 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8466 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8467
8468 for (i = 0; i < len; i++)
8469 {
8470 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8471 seen_dots = 1;
8472 if (p1[i] != p2[i])
8473 return seen_dots;
8474 }
8475 p1 += len;
8476 if (*p1 == '\n')
8477 return 2;
8478 if (*p1++ == ' ' && *p1++ == '[')
8479 {
8480 char *pend;
8481 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8482 if (strncmp (pend, " times]\n", 8) == 0)
8483 return n+1;
8484 }
8485 return 0;
8486 }
8487 \f
8488
8489 /* Display an echo area message M with a specified length of NBYTES
8490 bytes. The string may include null characters. If M is 0, clear
8491 out any existing message, and let the mini-buffer text show
8492 through.
8493
8494 This may GC, so the buffer M must NOT point to a Lisp string. */
8495
8496 void
8497 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8498 {
8499 /* First flush out any partial line written with print. */
8500 message_log_maybe_newline ();
8501 if (m)
8502 message_dolog (m, nbytes, 1, multibyte);
8503 message2_nolog (m, nbytes, multibyte);
8504 }
8505
8506
8507 /* The non-logging counterpart of message2. */
8508
8509 void
8510 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8511 {
8512 struct frame *sf = SELECTED_FRAME ();
8513 message_enable_multibyte = multibyte;
8514
8515 if (FRAME_INITIAL_P (sf))
8516 {
8517 if (noninteractive_need_newline)
8518 putc ('\n', stderr);
8519 noninteractive_need_newline = 0;
8520 if (m)
8521 fwrite (m, nbytes, 1, stderr);
8522 if (cursor_in_echo_area == 0)
8523 fprintf (stderr, "\n");
8524 fflush (stderr);
8525 }
8526 /* A null message buffer means that the frame hasn't really been
8527 initialized yet. Error messages get reported properly by
8528 cmd_error, so this must be just an informative message; toss it. */
8529 else if (INTERACTIVE
8530 && sf->glyphs_initialized_p
8531 && FRAME_MESSAGE_BUF (sf))
8532 {
8533 Lisp_Object mini_window;
8534 struct frame *f;
8535
8536 /* Get the frame containing the mini-buffer
8537 that the selected frame is using. */
8538 mini_window = FRAME_MINIBUF_WINDOW (sf);
8539 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8540
8541 FRAME_SAMPLE_VISIBILITY (f);
8542 if (FRAME_VISIBLE_P (sf)
8543 && ! FRAME_VISIBLE_P (f))
8544 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8545
8546 if (m)
8547 {
8548 set_message (m, Qnil, nbytes, multibyte);
8549 if (minibuffer_auto_raise)
8550 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8551 }
8552 else
8553 clear_message (1, 1);
8554
8555 do_pending_window_change (0);
8556 echo_area_display (1);
8557 do_pending_window_change (0);
8558 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8559 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8560 }
8561 }
8562
8563
8564 /* Display an echo area message M with a specified length of NBYTES
8565 bytes. The string may include null characters. If M is not a
8566 string, clear out any existing message, and let the mini-buffer
8567 text show through.
8568
8569 This function cancels echoing. */
8570
8571 void
8572 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8573 {
8574 struct gcpro gcpro1;
8575
8576 GCPRO1 (m);
8577 clear_message (1,1);
8578 cancel_echoing ();
8579
8580 /* First flush out any partial line written with print. */
8581 message_log_maybe_newline ();
8582 if (STRINGP (m))
8583 {
8584 char *buffer;
8585 USE_SAFE_ALLOCA;
8586
8587 SAFE_ALLOCA (buffer, char *, nbytes);
8588 memcpy (buffer, SDATA (m), nbytes);
8589 message_dolog (buffer, nbytes, 1, multibyte);
8590 SAFE_FREE ();
8591 }
8592 message3_nolog (m, nbytes, multibyte);
8593
8594 UNGCPRO;
8595 }
8596
8597
8598 /* The non-logging version of message3.
8599 This does not cancel echoing, because it is used for echoing.
8600 Perhaps we need to make a separate function for echoing
8601 and make this cancel echoing. */
8602
8603 void
8604 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8605 {
8606 struct frame *sf = SELECTED_FRAME ();
8607 message_enable_multibyte = multibyte;
8608
8609 if (FRAME_INITIAL_P (sf))
8610 {
8611 if (noninteractive_need_newline)
8612 putc ('\n', stderr);
8613 noninteractive_need_newline = 0;
8614 if (STRINGP (m))
8615 fwrite (SDATA (m), nbytes, 1, stderr);
8616 if (cursor_in_echo_area == 0)
8617 fprintf (stderr, "\n");
8618 fflush (stderr);
8619 }
8620 /* A null message buffer means that the frame hasn't really been
8621 initialized yet. Error messages get reported properly by
8622 cmd_error, so this must be just an informative message; toss it. */
8623 else if (INTERACTIVE
8624 && sf->glyphs_initialized_p
8625 && FRAME_MESSAGE_BUF (sf))
8626 {
8627 Lisp_Object mini_window;
8628 Lisp_Object frame;
8629 struct frame *f;
8630
8631 /* Get the frame containing the mini-buffer
8632 that the selected frame is using. */
8633 mini_window = FRAME_MINIBUF_WINDOW (sf);
8634 frame = XWINDOW (mini_window)->frame;
8635 f = XFRAME (frame);
8636
8637 FRAME_SAMPLE_VISIBILITY (f);
8638 if (FRAME_VISIBLE_P (sf)
8639 && !FRAME_VISIBLE_P (f))
8640 Fmake_frame_visible (frame);
8641
8642 if (STRINGP (m) && SCHARS (m) > 0)
8643 {
8644 set_message (NULL, m, nbytes, multibyte);
8645 if (minibuffer_auto_raise)
8646 Fraise_frame (frame);
8647 /* Assume we are not echoing.
8648 (If we are, echo_now will override this.) */
8649 echo_message_buffer = Qnil;
8650 }
8651 else
8652 clear_message (1, 1);
8653
8654 do_pending_window_change (0);
8655 echo_area_display (1);
8656 do_pending_window_change (0);
8657 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8658 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8659 }
8660 }
8661
8662
8663 /* Display a null-terminated echo area message M. If M is 0, clear
8664 out any existing message, and let the mini-buffer text show through.
8665
8666 The buffer M must continue to exist until after the echo area gets
8667 cleared or some other message gets displayed there. Do not pass
8668 text that is stored in a Lisp string. Do not pass text in a buffer
8669 that was alloca'd. */
8670
8671 void
8672 message1 (const char *m)
8673 {
8674 message2 (m, (m ? strlen (m) : 0), 0);
8675 }
8676
8677
8678 /* The non-logging counterpart of message1. */
8679
8680 void
8681 message1_nolog (const char *m)
8682 {
8683 message2_nolog (m, (m ? strlen (m) : 0), 0);
8684 }
8685
8686 /* Display a message M which contains a single %s
8687 which gets replaced with STRING. */
8688
8689 void
8690 message_with_string (const char *m, Lisp_Object string, int log)
8691 {
8692 CHECK_STRING (string);
8693
8694 if (noninteractive)
8695 {
8696 if (m)
8697 {
8698 if (noninteractive_need_newline)
8699 putc ('\n', stderr);
8700 noninteractive_need_newline = 0;
8701 fprintf (stderr, m, SDATA (string));
8702 if (!cursor_in_echo_area)
8703 fprintf (stderr, "\n");
8704 fflush (stderr);
8705 }
8706 }
8707 else if (INTERACTIVE)
8708 {
8709 /* The frame whose minibuffer we're going to display the message on.
8710 It may be larger than the selected frame, so we need
8711 to use its buffer, not the selected frame's buffer. */
8712 Lisp_Object mini_window;
8713 struct frame *f, *sf = SELECTED_FRAME ();
8714
8715 /* Get the frame containing the minibuffer
8716 that the selected frame is using. */
8717 mini_window = FRAME_MINIBUF_WINDOW (sf);
8718 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8719
8720 /* A null message buffer means that the frame hasn't really been
8721 initialized yet. Error messages get reported properly by
8722 cmd_error, so this must be just an informative message; toss it. */
8723 if (FRAME_MESSAGE_BUF (f))
8724 {
8725 Lisp_Object args[2], msg;
8726 struct gcpro gcpro1, gcpro2;
8727
8728 args[0] = build_string (m);
8729 args[1] = msg = string;
8730 GCPRO2 (args[0], msg);
8731 gcpro1.nvars = 2;
8732
8733 msg = Fformat (2, args);
8734
8735 if (log)
8736 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8737 else
8738 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8739
8740 UNGCPRO;
8741
8742 /* Print should start at the beginning of the message
8743 buffer next time. */
8744 message_buf_print = 0;
8745 }
8746 }
8747 }
8748
8749
8750 /* Dump an informative message to the minibuf. If M is 0, clear out
8751 any existing message, and let the mini-buffer text show through. */
8752
8753 static void
8754 vmessage (const char *m, va_list ap)
8755 {
8756 if (noninteractive)
8757 {
8758 if (m)
8759 {
8760 if (noninteractive_need_newline)
8761 putc ('\n', stderr);
8762 noninteractive_need_newline = 0;
8763 vfprintf (stderr, m, ap);
8764 if (cursor_in_echo_area == 0)
8765 fprintf (stderr, "\n");
8766 fflush (stderr);
8767 }
8768 }
8769 else if (INTERACTIVE)
8770 {
8771 /* The frame whose mini-buffer we're going to display the message
8772 on. It may be larger than the selected frame, so we need to
8773 use its buffer, not the selected frame's buffer. */
8774 Lisp_Object mini_window;
8775 struct frame *f, *sf = SELECTED_FRAME ();
8776
8777 /* Get the frame containing the mini-buffer
8778 that the selected frame is using. */
8779 mini_window = FRAME_MINIBUF_WINDOW (sf);
8780 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8781
8782 /* A null message buffer means that the frame hasn't really been
8783 initialized yet. Error messages get reported properly by
8784 cmd_error, so this must be just an informative message; toss
8785 it. */
8786 if (FRAME_MESSAGE_BUF (f))
8787 {
8788 if (m)
8789 {
8790 size_t len;
8791
8792 len = doprnt (FRAME_MESSAGE_BUF (f),
8793 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8794
8795 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8796 }
8797 else
8798 message1 (0);
8799
8800 /* Print should start at the beginning of the message
8801 buffer next time. */
8802 message_buf_print = 0;
8803 }
8804 }
8805 }
8806
8807 void
8808 message (const char *m, ...)
8809 {
8810 va_list ap;
8811 va_start (ap, m);
8812 vmessage (m, ap);
8813 va_end (ap);
8814 }
8815
8816
8817 #if 0
8818 /* The non-logging version of message. */
8819
8820 void
8821 message_nolog (const char *m, ...)
8822 {
8823 Lisp_Object old_log_max;
8824 va_list ap;
8825 va_start (ap, m);
8826 old_log_max = Vmessage_log_max;
8827 Vmessage_log_max = Qnil;
8828 vmessage (m, ap);
8829 Vmessage_log_max = old_log_max;
8830 va_end (ap);
8831 }
8832 #endif
8833
8834
8835 /* Display the current message in the current mini-buffer. This is
8836 only called from error handlers in process.c, and is not time
8837 critical. */
8838
8839 void
8840 update_echo_area (void)
8841 {
8842 if (!NILP (echo_area_buffer[0]))
8843 {
8844 Lisp_Object string;
8845 string = Fcurrent_message ();
8846 message3 (string, SBYTES (string),
8847 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
8848 }
8849 }
8850
8851
8852 /* Make sure echo area buffers in `echo_buffers' are live.
8853 If they aren't, make new ones. */
8854
8855 static void
8856 ensure_echo_area_buffers (void)
8857 {
8858 int i;
8859
8860 for (i = 0; i < 2; ++i)
8861 if (!BUFFERP (echo_buffer[i])
8862 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
8863 {
8864 char name[30];
8865 Lisp_Object old_buffer;
8866 int j;
8867
8868 old_buffer = echo_buffer[i];
8869 sprintf (name, " *Echo Area %d*", i);
8870 echo_buffer[i] = Fget_buffer_create (build_string (name));
8871 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
8872 /* to force word wrap in echo area -
8873 it was decided to postpone this*/
8874 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8875
8876 for (j = 0; j < 2; ++j)
8877 if (EQ (old_buffer, echo_area_buffer[j]))
8878 echo_area_buffer[j] = echo_buffer[i];
8879 }
8880 }
8881
8882
8883 /* Call FN with args A1..A4 with either the current or last displayed
8884 echo_area_buffer as current buffer.
8885
8886 WHICH zero means use the current message buffer
8887 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8888 from echo_buffer[] and clear it.
8889
8890 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8891 suitable buffer from echo_buffer[] and clear it.
8892
8893 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8894 that the current message becomes the last displayed one, make
8895 choose a suitable buffer for echo_area_buffer[0], and clear it.
8896
8897 Value is what FN returns. */
8898
8899 static int
8900 with_echo_area_buffer (struct window *w, int which,
8901 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8902 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8903 {
8904 Lisp_Object buffer;
8905 int this_one, the_other, clear_buffer_p, rc;
8906 int count = SPECPDL_INDEX ();
8907
8908 /* If buffers aren't live, make new ones. */
8909 ensure_echo_area_buffers ();
8910
8911 clear_buffer_p = 0;
8912
8913 if (which == 0)
8914 this_one = 0, the_other = 1;
8915 else if (which > 0)
8916 this_one = 1, the_other = 0;
8917 else
8918 {
8919 this_one = 0, the_other = 1;
8920 clear_buffer_p = 1;
8921
8922 /* We need a fresh one in case the current echo buffer equals
8923 the one containing the last displayed echo area message. */
8924 if (!NILP (echo_area_buffer[this_one])
8925 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8926 echo_area_buffer[this_one] = Qnil;
8927 }
8928
8929 /* Choose a suitable buffer from echo_buffer[] is we don't
8930 have one. */
8931 if (NILP (echo_area_buffer[this_one]))
8932 {
8933 echo_area_buffer[this_one]
8934 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8935 ? echo_buffer[the_other]
8936 : echo_buffer[this_one]);
8937 clear_buffer_p = 1;
8938 }
8939
8940 buffer = echo_area_buffer[this_one];
8941
8942 /* Don't get confused by reusing the buffer used for echoing
8943 for a different purpose. */
8944 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8945 cancel_echoing ();
8946
8947 record_unwind_protect (unwind_with_echo_area_buffer,
8948 with_echo_area_buffer_unwind_data (w));
8949
8950 /* Make the echo area buffer current. Note that for display
8951 purposes, it is not necessary that the displayed window's buffer
8952 == current_buffer, except for text property lookup. So, let's
8953 only set that buffer temporarily here without doing a full
8954 Fset_window_buffer. We must also change w->pointm, though,
8955 because otherwise an assertions in unshow_buffer fails, and Emacs
8956 aborts. */
8957 set_buffer_internal_1 (XBUFFER (buffer));
8958 if (w)
8959 {
8960 w->buffer = buffer;
8961 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8962 }
8963
8964 BVAR (current_buffer, undo_list) = Qt;
8965 BVAR (current_buffer, read_only) = Qnil;
8966 specbind (Qinhibit_read_only, Qt);
8967 specbind (Qinhibit_modification_hooks, Qt);
8968
8969 if (clear_buffer_p && Z > BEG)
8970 del_range (BEG, Z);
8971
8972 xassert (BEGV >= BEG);
8973 xassert (ZV <= Z && ZV >= BEGV);
8974
8975 rc = fn (a1, a2, a3, a4);
8976
8977 xassert (BEGV >= BEG);
8978 xassert (ZV <= Z && ZV >= BEGV);
8979
8980 unbind_to (count, Qnil);
8981 return rc;
8982 }
8983
8984
8985 /* Save state that should be preserved around the call to the function
8986 FN called in with_echo_area_buffer. */
8987
8988 static Lisp_Object
8989 with_echo_area_buffer_unwind_data (struct window *w)
8990 {
8991 int i = 0;
8992 Lisp_Object vector, tmp;
8993
8994 /* Reduce consing by keeping one vector in
8995 Vwith_echo_area_save_vector. */
8996 vector = Vwith_echo_area_save_vector;
8997 Vwith_echo_area_save_vector = Qnil;
8998
8999 if (NILP (vector))
9000 vector = Fmake_vector (make_number (7), Qnil);
9001
9002 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9003 ASET (vector, i, Vdeactivate_mark); ++i;
9004 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9005
9006 if (w)
9007 {
9008 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9009 ASET (vector, i, w->buffer); ++i;
9010 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9011 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9012 }
9013 else
9014 {
9015 int end = i + 4;
9016 for (; i < end; ++i)
9017 ASET (vector, i, Qnil);
9018 }
9019
9020 xassert (i == ASIZE (vector));
9021 return vector;
9022 }
9023
9024
9025 /* Restore global state from VECTOR which was created by
9026 with_echo_area_buffer_unwind_data. */
9027
9028 static Lisp_Object
9029 unwind_with_echo_area_buffer (Lisp_Object vector)
9030 {
9031 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9032 Vdeactivate_mark = AREF (vector, 1);
9033 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9034
9035 if (WINDOWP (AREF (vector, 3)))
9036 {
9037 struct window *w;
9038 Lisp_Object buffer, charpos, bytepos;
9039
9040 w = XWINDOW (AREF (vector, 3));
9041 buffer = AREF (vector, 4);
9042 charpos = AREF (vector, 5);
9043 bytepos = AREF (vector, 6);
9044
9045 w->buffer = buffer;
9046 set_marker_both (w->pointm, buffer,
9047 XFASTINT (charpos), XFASTINT (bytepos));
9048 }
9049
9050 Vwith_echo_area_save_vector = vector;
9051 return Qnil;
9052 }
9053
9054
9055 /* Set up the echo area for use by print functions. MULTIBYTE_P
9056 non-zero means we will print multibyte. */
9057
9058 void
9059 setup_echo_area_for_printing (int multibyte_p)
9060 {
9061 /* If we can't find an echo area any more, exit. */
9062 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9063 Fkill_emacs (Qnil);
9064
9065 ensure_echo_area_buffers ();
9066
9067 if (!message_buf_print)
9068 {
9069 /* A message has been output since the last time we printed.
9070 Choose a fresh echo area buffer. */
9071 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9072 echo_area_buffer[0] = echo_buffer[1];
9073 else
9074 echo_area_buffer[0] = echo_buffer[0];
9075
9076 /* Switch to that buffer and clear it. */
9077 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9078 BVAR (current_buffer, truncate_lines) = Qnil;
9079
9080 if (Z > BEG)
9081 {
9082 int count = SPECPDL_INDEX ();
9083 specbind (Qinhibit_read_only, Qt);
9084 /* Note that undo recording is always disabled. */
9085 del_range (BEG, Z);
9086 unbind_to (count, Qnil);
9087 }
9088 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9089
9090 /* Set up the buffer for the multibyteness we need. */
9091 if (multibyte_p
9092 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9093 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9094
9095 /* Raise the frame containing the echo area. */
9096 if (minibuffer_auto_raise)
9097 {
9098 struct frame *sf = SELECTED_FRAME ();
9099 Lisp_Object mini_window;
9100 mini_window = FRAME_MINIBUF_WINDOW (sf);
9101 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9102 }
9103
9104 message_log_maybe_newline ();
9105 message_buf_print = 1;
9106 }
9107 else
9108 {
9109 if (NILP (echo_area_buffer[0]))
9110 {
9111 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9112 echo_area_buffer[0] = echo_buffer[1];
9113 else
9114 echo_area_buffer[0] = echo_buffer[0];
9115 }
9116
9117 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9118 {
9119 /* Someone switched buffers between print requests. */
9120 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9121 BVAR (current_buffer, truncate_lines) = Qnil;
9122 }
9123 }
9124 }
9125
9126
9127 /* Display an echo area message in window W. Value is non-zero if W's
9128 height is changed. If display_last_displayed_message_p is
9129 non-zero, display the message that was last displayed, otherwise
9130 display the current message. */
9131
9132 static int
9133 display_echo_area (struct window *w)
9134 {
9135 int i, no_message_p, window_height_changed_p, count;
9136
9137 /* Temporarily disable garbage collections while displaying the echo
9138 area. This is done because a GC can print a message itself.
9139 That message would modify the echo area buffer's contents while a
9140 redisplay of the buffer is going on, and seriously confuse
9141 redisplay. */
9142 count = inhibit_garbage_collection ();
9143
9144 /* If there is no message, we must call display_echo_area_1
9145 nevertheless because it resizes the window. But we will have to
9146 reset the echo_area_buffer in question to nil at the end because
9147 with_echo_area_buffer will sets it to an empty buffer. */
9148 i = display_last_displayed_message_p ? 1 : 0;
9149 no_message_p = NILP (echo_area_buffer[i]);
9150
9151 window_height_changed_p
9152 = with_echo_area_buffer (w, display_last_displayed_message_p,
9153 display_echo_area_1,
9154 (intptr_t) w, Qnil, 0, 0);
9155
9156 if (no_message_p)
9157 echo_area_buffer[i] = Qnil;
9158
9159 unbind_to (count, Qnil);
9160 return window_height_changed_p;
9161 }
9162
9163
9164 /* Helper for display_echo_area. Display the current buffer which
9165 contains the current echo area message in window W, a mini-window,
9166 a pointer to which is passed in A1. A2..A4 are currently not used.
9167 Change the height of W so that all of the message is displayed.
9168 Value is non-zero if height of W was changed. */
9169
9170 static int
9171 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9172 {
9173 intptr_t i1 = a1;
9174 struct window *w = (struct window *) i1;
9175 Lisp_Object window;
9176 struct text_pos start;
9177 int window_height_changed_p = 0;
9178
9179 /* Do this before displaying, so that we have a large enough glyph
9180 matrix for the display. If we can't get enough space for the
9181 whole text, display the last N lines. That works by setting w->start. */
9182 window_height_changed_p = resize_mini_window (w, 0);
9183
9184 /* Use the starting position chosen by resize_mini_window. */
9185 SET_TEXT_POS_FROM_MARKER (start, w->start);
9186
9187 /* Display. */
9188 clear_glyph_matrix (w->desired_matrix);
9189 XSETWINDOW (window, w);
9190 try_window (window, start, 0);
9191
9192 return window_height_changed_p;
9193 }
9194
9195
9196 /* Resize the echo area window to exactly the size needed for the
9197 currently displayed message, if there is one. If a mini-buffer
9198 is active, don't shrink it. */
9199
9200 void
9201 resize_echo_area_exactly (void)
9202 {
9203 if (BUFFERP (echo_area_buffer[0])
9204 && WINDOWP (echo_area_window))
9205 {
9206 struct window *w = XWINDOW (echo_area_window);
9207 int resized_p;
9208 Lisp_Object resize_exactly;
9209
9210 if (minibuf_level == 0)
9211 resize_exactly = Qt;
9212 else
9213 resize_exactly = Qnil;
9214
9215 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9216 (intptr_t) w, resize_exactly,
9217 0, 0);
9218 if (resized_p)
9219 {
9220 ++windows_or_buffers_changed;
9221 ++update_mode_lines;
9222 redisplay_internal ();
9223 }
9224 }
9225 }
9226
9227
9228 /* Callback function for with_echo_area_buffer, when used from
9229 resize_echo_area_exactly. A1 contains a pointer to the window to
9230 resize, EXACTLY non-nil means resize the mini-window exactly to the
9231 size of the text displayed. A3 and A4 are not used. Value is what
9232 resize_mini_window returns. */
9233
9234 static int
9235 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9236 {
9237 intptr_t i1 = a1;
9238 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9239 }
9240
9241
9242 /* Resize mini-window W to fit the size of its contents. EXACT_P
9243 means size the window exactly to the size needed. Otherwise, it's
9244 only enlarged until W's buffer is empty.
9245
9246 Set W->start to the right place to begin display. If the whole
9247 contents fit, start at the beginning. Otherwise, start so as
9248 to make the end of the contents appear. This is particularly
9249 important for y-or-n-p, but seems desirable generally.
9250
9251 Value is non-zero if the window height has been changed. */
9252
9253 int
9254 resize_mini_window (struct window *w, int exact_p)
9255 {
9256 struct frame *f = XFRAME (w->frame);
9257 int window_height_changed_p = 0;
9258
9259 xassert (MINI_WINDOW_P (w));
9260
9261 /* By default, start display at the beginning. */
9262 set_marker_both (w->start, w->buffer,
9263 BUF_BEGV (XBUFFER (w->buffer)),
9264 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9265
9266 /* Don't resize windows while redisplaying a window; it would
9267 confuse redisplay functions when the size of the window they are
9268 displaying changes from under them. Such a resizing can happen,
9269 for instance, when which-func prints a long message while
9270 we are running fontification-functions. We're running these
9271 functions with safe_call which binds inhibit-redisplay to t. */
9272 if (!NILP (Vinhibit_redisplay))
9273 return 0;
9274
9275 /* Nil means don't try to resize. */
9276 if (NILP (Vresize_mini_windows)
9277 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9278 return 0;
9279
9280 if (!FRAME_MINIBUF_ONLY_P (f))
9281 {
9282 struct it it;
9283 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9284 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9285 int height, max_height;
9286 int unit = FRAME_LINE_HEIGHT (f);
9287 struct text_pos start;
9288 struct buffer *old_current_buffer = NULL;
9289
9290 if (current_buffer != XBUFFER (w->buffer))
9291 {
9292 old_current_buffer = current_buffer;
9293 set_buffer_internal (XBUFFER (w->buffer));
9294 }
9295
9296 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9297
9298 /* Compute the max. number of lines specified by the user. */
9299 if (FLOATP (Vmax_mini_window_height))
9300 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9301 else if (INTEGERP (Vmax_mini_window_height))
9302 max_height = XINT (Vmax_mini_window_height);
9303 else
9304 max_height = total_height / 4;
9305
9306 /* Correct that max. height if it's bogus. */
9307 max_height = max (1, max_height);
9308 max_height = min (total_height, max_height);
9309
9310 /* Find out the height of the text in the window. */
9311 if (it.line_wrap == TRUNCATE)
9312 height = 1;
9313 else
9314 {
9315 last_height = 0;
9316 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9317 if (it.max_ascent == 0 && it.max_descent == 0)
9318 height = it.current_y + last_height;
9319 else
9320 height = it.current_y + it.max_ascent + it.max_descent;
9321 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9322 height = (height + unit - 1) / unit;
9323 }
9324
9325 /* Compute a suitable window start. */
9326 if (height > max_height)
9327 {
9328 height = max_height;
9329 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9330 move_it_vertically_backward (&it, (height - 1) * unit);
9331 start = it.current.pos;
9332 }
9333 else
9334 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9335 SET_MARKER_FROM_TEXT_POS (w->start, start);
9336
9337 if (EQ (Vresize_mini_windows, Qgrow_only))
9338 {
9339 /* Let it grow only, until we display an empty message, in which
9340 case the window shrinks again. */
9341 if (height > WINDOW_TOTAL_LINES (w))
9342 {
9343 int old_height = WINDOW_TOTAL_LINES (w);
9344 freeze_window_starts (f, 1);
9345 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9346 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9347 }
9348 else if (height < WINDOW_TOTAL_LINES (w)
9349 && (exact_p || BEGV == ZV))
9350 {
9351 int old_height = WINDOW_TOTAL_LINES (w);
9352 freeze_window_starts (f, 0);
9353 shrink_mini_window (w);
9354 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9355 }
9356 }
9357 else
9358 {
9359 /* Always resize to exact size needed. */
9360 if (height > WINDOW_TOTAL_LINES (w))
9361 {
9362 int old_height = WINDOW_TOTAL_LINES (w);
9363 freeze_window_starts (f, 1);
9364 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9365 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9366 }
9367 else if (height < WINDOW_TOTAL_LINES (w))
9368 {
9369 int old_height = WINDOW_TOTAL_LINES (w);
9370 freeze_window_starts (f, 0);
9371 shrink_mini_window (w);
9372
9373 if (height)
9374 {
9375 freeze_window_starts (f, 1);
9376 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9377 }
9378
9379 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9380 }
9381 }
9382
9383 if (old_current_buffer)
9384 set_buffer_internal (old_current_buffer);
9385 }
9386
9387 return window_height_changed_p;
9388 }
9389
9390
9391 /* Value is the current message, a string, or nil if there is no
9392 current message. */
9393
9394 Lisp_Object
9395 current_message (void)
9396 {
9397 Lisp_Object msg;
9398
9399 if (!BUFFERP (echo_area_buffer[0]))
9400 msg = Qnil;
9401 else
9402 {
9403 with_echo_area_buffer (0, 0, current_message_1,
9404 (intptr_t) &msg, Qnil, 0, 0);
9405 if (NILP (msg))
9406 echo_area_buffer[0] = Qnil;
9407 }
9408
9409 return msg;
9410 }
9411
9412
9413 static int
9414 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9415 {
9416 intptr_t i1 = a1;
9417 Lisp_Object *msg = (Lisp_Object *) i1;
9418
9419 if (Z > BEG)
9420 *msg = make_buffer_string (BEG, Z, 1);
9421 else
9422 *msg = Qnil;
9423 return 0;
9424 }
9425
9426
9427 /* Push the current message on Vmessage_stack for later restauration
9428 by restore_message. Value is non-zero if the current message isn't
9429 empty. This is a relatively infrequent operation, so it's not
9430 worth optimizing. */
9431
9432 int
9433 push_message (void)
9434 {
9435 Lisp_Object msg;
9436 msg = current_message ();
9437 Vmessage_stack = Fcons (msg, Vmessage_stack);
9438 return STRINGP (msg);
9439 }
9440
9441
9442 /* Restore message display from the top of Vmessage_stack. */
9443
9444 void
9445 restore_message (void)
9446 {
9447 Lisp_Object msg;
9448
9449 xassert (CONSP (Vmessage_stack));
9450 msg = XCAR (Vmessage_stack);
9451 if (STRINGP (msg))
9452 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9453 else
9454 message3_nolog (msg, 0, 0);
9455 }
9456
9457
9458 /* Handler for record_unwind_protect calling pop_message. */
9459
9460 Lisp_Object
9461 pop_message_unwind (Lisp_Object dummy)
9462 {
9463 pop_message ();
9464 return Qnil;
9465 }
9466
9467 /* Pop the top-most entry off Vmessage_stack. */
9468
9469 static void
9470 pop_message (void)
9471 {
9472 xassert (CONSP (Vmessage_stack));
9473 Vmessage_stack = XCDR (Vmessage_stack);
9474 }
9475
9476
9477 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9478 exits. If the stack is not empty, we have a missing pop_message
9479 somewhere. */
9480
9481 void
9482 check_message_stack (void)
9483 {
9484 if (!NILP (Vmessage_stack))
9485 abort ();
9486 }
9487
9488
9489 /* Truncate to NCHARS what will be displayed in the echo area the next
9490 time we display it---but don't redisplay it now. */
9491
9492 void
9493 truncate_echo_area (EMACS_INT nchars)
9494 {
9495 if (nchars == 0)
9496 echo_area_buffer[0] = Qnil;
9497 /* A null message buffer means that the frame hasn't really been
9498 initialized yet. Error messages get reported properly by
9499 cmd_error, so this must be just an informative message; toss it. */
9500 else if (!noninteractive
9501 && INTERACTIVE
9502 && !NILP (echo_area_buffer[0]))
9503 {
9504 struct frame *sf = SELECTED_FRAME ();
9505 if (FRAME_MESSAGE_BUF (sf))
9506 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9507 }
9508 }
9509
9510
9511 /* Helper function for truncate_echo_area. Truncate the current
9512 message to at most NCHARS characters. */
9513
9514 static int
9515 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9516 {
9517 if (BEG + nchars < Z)
9518 del_range (BEG + nchars, Z);
9519 if (Z == BEG)
9520 echo_area_buffer[0] = Qnil;
9521 return 0;
9522 }
9523
9524
9525 /* Set the current message to a substring of S or STRING.
9526
9527 If STRING is a Lisp string, set the message to the first NBYTES
9528 bytes from STRING. NBYTES zero means use the whole string. If
9529 STRING is multibyte, the message will be displayed multibyte.
9530
9531 If S is not null, set the message to the first LEN bytes of S. LEN
9532 zero means use the whole string. MULTIBYTE_P non-zero means S is
9533 multibyte. Display the message multibyte in that case.
9534
9535 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9536 to t before calling set_message_1 (which calls insert).
9537 */
9538
9539 static void
9540 set_message (const char *s, Lisp_Object string,
9541 EMACS_INT nbytes, int multibyte_p)
9542 {
9543 message_enable_multibyte
9544 = ((s && multibyte_p)
9545 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9546
9547 with_echo_area_buffer (0, -1, set_message_1,
9548 (intptr_t) s, string, nbytes, multibyte_p);
9549 message_buf_print = 0;
9550 help_echo_showing_p = 0;
9551 }
9552
9553
9554 /* Helper function for set_message. Arguments have the same meaning
9555 as there, with A1 corresponding to S and A2 corresponding to STRING
9556 This function is called with the echo area buffer being
9557 current. */
9558
9559 static int
9560 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9561 {
9562 intptr_t i1 = a1;
9563 const char *s = (const char *) i1;
9564 const unsigned char *msg = (const unsigned char *) s;
9565 Lisp_Object string = a2;
9566
9567 /* Change multibyteness of the echo buffer appropriately. */
9568 if (message_enable_multibyte
9569 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9570 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9571
9572 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9573 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9574 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9575
9576 /* Insert new message at BEG. */
9577 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9578
9579 if (STRINGP (string))
9580 {
9581 EMACS_INT nchars;
9582
9583 if (nbytes == 0)
9584 nbytes = SBYTES (string);
9585 nchars = string_byte_to_char (string, nbytes);
9586
9587 /* This function takes care of single/multibyte conversion. We
9588 just have to ensure that the echo area buffer has the right
9589 setting of enable_multibyte_characters. */
9590 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9591 }
9592 else if (s)
9593 {
9594 if (nbytes == 0)
9595 nbytes = strlen (s);
9596
9597 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9598 {
9599 /* Convert from multi-byte to single-byte. */
9600 EMACS_INT i;
9601 int c, n;
9602 char work[1];
9603
9604 /* Convert a multibyte string to single-byte. */
9605 for (i = 0; i < nbytes; i += n)
9606 {
9607 c = string_char_and_length (msg + i, &n);
9608 work[0] = (ASCII_CHAR_P (c)
9609 ? c
9610 : multibyte_char_to_unibyte (c));
9611 insert_1_both (work, 1, 1, 1, 0, 0);
9612 }
9613 }
9614 else if (!multibyte_p
9615 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9616 {
9617 /* Convert from single-byte to multi-byte. */
9618 EMACS_INT i;
9619 int c, n;
9620 unsigned char str[MAX_MULTIBYTE_LENGTH];
9621
9622 /* Convert a single-byte string to multibyte. */
9623 for (i = 0; i < nbytes; i++)
9624 {
9625 c = msg[i];
9626 MAKE_CHAR_MULTIBYTE (c);
9627 n = CHAR_STRING (c, str);
9628 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9629 }
9630 }
9631 else
9632 insert_1 (s, nbytes, 1, 0, 0);
9633 }
9634
9635 return 0;
9636 }
9637
9638
9639 /* Clear messages. CURRENT_P non-zero means clear the current
9640 message. LAST_DISPLAYED_P non-zero means clear the message
9641 last displayed. */
9642
9643 void
9644 clear_message (int current_p, int last_displayed_p)
9645 {
9646 if (current_p)
9647 {
9648 echo_area_buffer[0] = Qnil;
9649 message_cleared_p = 1;
9650 }
9651
9652 if (last_displayed_p)
9653 echo_area_buffer[1] = Qnil;
9654
9655 message_buf_print = 0;
9656 }
9657
9658 /* Clear garbaged frames.
9659
9660 This function is used where the old redisplay called
9661 redraw_garbaged_frames which in turn called redraw_frame which in
9662 turn called clear_frame. The call to clear_frame was a source of
9663 flickering. I believe a clear_frame is not necessary. It should
9664 suffice in the new redisplay to invalidate all current matrices,
9665 and ensure a complete redisplay of all windows. */
9666
9667 static void
9668 clear_garbaged_frames (void)
9669 {
9670 if (frame_garbaged)
9671 {
9672 Lisp_Object tail, frame;
9673 int changed_count = 0;
9674
9675 FOR_EACH_FRAME (tail, frame)
9676 {
9677 struct frame *f = XFRAME (frame);
9678
9679 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9680 {
9681 if (f->resized_p)
9682 {
9683 Fredraw_frame (frame);
9684 f->force_flush_display_p = 1;
9685 }
9686 clear_current_matrices (f);
9687 changed_count++;
9688 f->garbaged = 0;
9689 f->resized_p = 0;
9690 }
9691 }
9692
9693 frame_garbaged = 0;
9694 if (changed_count)
9695 ++windows_or_buffers_changed;
9696 }
9697 }
9698
9699
9700 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9701 is non-zero update selected_frame. Value is non-zero if the
9702 mini-windows height has been changed. */
9703
9704 static int
9705 echo_area_display (int update_frame_p)
9706 {
9707 Lisp_Object mini_window;
9708 struct window *w;
9709 struct frame *f;
9710 int window_height_changed_p = 0;
9711 struct frame *sf = SELECTED_FRAME ();
9712
9713 mini_window = FRAME_MINIBUF_WINDOW (sf);
9714 w = XWINDOW (mini_window);
9715 f = XFRAME (WINDOW_FRAME (w));
9716
9717 /* Don't display if frame is invisible or not yet initialized. */
9718 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9719 return 0;
9720
9721 #ifdef HAVE_WINDOW_SYSTEM
9722 /* When Emacs starts, selected_frame may be the initial terminal
9723 frame. If we let this through, a message would be displayed on
9724 the terminal. */
9725 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9726 return 0;
9727 #endif /* HAVE_WINDOW_SYSTEM */
9728
9729 /* Redraw garbaged frames. */
9730 if (frame_garbaged)
9731 clear_garbaged_frames ();
9732
9733 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9734 {
9735 echo_area_window = mini_window;
9736 window_height_changed_p = display_echo_area (w);
9737 w->must_be_updated_p = 1;
9738
9739 /* Update the display, unless called from redisplay_internal.
9740 Also don't update the screen during redisplay itself. The
9741 update will happen at the end of redisplay, and an update
9742 here could cause confusion. */
9743 if (update_frame_p && !redisplaying_p)
9744 {
9745 int n = 0;
9746
9747 /* If the display update has been interrupted by pending
9748 input, update mode lines in the frame. Due to the
9749 pending input, it might have been that redisplay hasn't
9750 been called, so that mode lines above the echo area are
9751 garbaged. This looks odd, so we prevent it here. */
9752 if (!display_completed)
9753 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9754
9755 if (window_height_changed_p
9756 /* Don't do this if Emacs is shutting down. Redisplay
9757 needs to run hooks. */
9758 && !NILP (Vrun_hooks))
9759 {
9760 /* Must update other windows. Likewise as in other
9761 cases, don't let this update be interrupted by
9762 pending input. */
9763 int count = SPECPDL_INDEX ();
9764 specbind (Qredisplay_dont_pause, Qt);
9765 windows_or_buffers_changed = 1;
9766 redisplay_internal ();
9767 unbind_to (count, Qnil);
9768 }
9769 else if (FRAME_WINDOW_P (f) && n == 0)
9770 {
9771 /* Window configuration is the same as before.
9772 Can do with a display update of the echo area,
9773 unless we displayed some mode lines. */
9774 update_single_window (w, 1);
9775 FRAME_RIF (f)->flush_display (f);
9776 }
9777 else
9778 update_frame (f, 1, 1);
9779
9780 /* If cursor is in the echo area, make sure that the next
9781 redisplay displays the minibuffer, so that the cursor will
9782 be replaced with what the minibuffer wants. */
9783 if (cursor_in_echo_area)
9784 ++windows_or_buffers_changed;
9785 }
9786 }
9787 else if (!EQ (mini_window, selected_window))
9788 windows_or_buffers_changed++;
9789
9790 /* Last displayed message is now the current message. */
9791 echo_area_buffer[1] = echo_area_buffer[0];
9792 /* Inform read_char that we're not echoing. */
9793 echo_message_buffer = Qnil;
9794
9795 /* Prevent redisplay optimization in redisplay_internal by resetting
9796 this_line_start_pos. This is done because the mini-buffer now
9797 displays the message instead of its buffer text. */
9798 if (EQ (mini_window, selected_window))
9799 CHARPOS (this_line_start_pos) = 0;
9800
9801 return window_height_changed_p;
9802 }
9803
9804
9805 \f
9806 /***********************************************************************
9807 Mode Lines and Frame Titles
9808 ***********************************************************************/
9809
9810 /* A buffer for constructing non-propertized mode-line strings and
9811 frame titles in it; allocated from the heap in init_xdisp and
9812 resized as needed in store_mode_line_noprop_char. */
9813
9814 static char *mode_line_noprop_buf;
9815
9816 /* The buffer's end, and a current output position in it. */
9817
9818 static char *mode_line_noprop_buf_end;
9819 static char *mode_line_noprop_ptr;
9820
9821 #define MODE_LINE_NOPROP_LEN(start) \
9822 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9823
9824 static enum {
9825 MODE_LINE_DISPLAY = 0,
9826 MODE_LINE_TITLE,
9827 MODE_LINE_NOPROP,
9828 MODE_LINE_STRING
9829 } mode_line_target;
9830
9831 /* Alist that caches the results of :propertize.
9832 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9833 static Lisp_Object mode_line_proptrans_alist;
9834
9835 /* List of strings making up the mode-line. */
9836 static Lisp_Object mode_line_string_list;
9837
9838 /* Base face property when building propertized mode line string. */
9839 static Lisp_Object mode_line_string_face;
9840 static Lisp_Object mode_line_string_face_prop;
9841
9842
9843 /* Unwind data for mode line strings */
9844
9845 static Lisp_Object Vmode_line_unwind_vector;
9846
9847 static Lisp_Object
9848 format_mode_line_unwind_data (struct buffer *obuf,
9849 Lisp_Object owin,
9850 int save_proptrans)
9851 {
9852 Lisp_Object vector, tmp;
9853
9854 /* Reduce consing by keeping one vector in
9855 Vwith_echo_area_save_vector. */
9856 vector = Vmode_line_unwind_vector;
9857 Vmode_line_unwind_vector = Qnil;
9858
9859 if (NILP (vector))
9860 vector = Fmake_vector (make_number (8), Qnil);
9861
9862 ASET (vector, 0, make_number (mode_line_target));
9863 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9864 ASET (vector, 2, mode_line_string_list);
9865 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9866 ASET (vector, 4, mode_line_string_face);
9867 ASET (vector, 5, mode_line_string_face_prop);
9868
9869 if (obuf)
9870 XSETBUFFER (tmp, obuf);
9871 else
9872 tmp = Qnil;
9873 ASET (vector, 6, tmp);
9874 ASET (vector, 7, owin);
9875
9876 return vector;
9877 }
9878
9879 static Lisp_Object
9880 unwind_format_mode_line (Lisp_Object vector)
9881 {
9882 mode_line_target = XINT (AREF (vector, 0));
9883 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9884 mode_line_string_list = AREF (vector, 2);
9885 if (! EQ (AREF (vector, 3), Qt))
9886 mode_line_proptrans_alist = AREF (vector, 3);
9887 mode_line_string_face = AREF (vector, 4);
9888 mode_line_string_face_prop = AREF (vector, 5);
9889
9890 if (!NILP (AREF (vector, 7)))
9891 /* Select window before buffer, since it may change the buffer. */
9892 Fselect_window (AREF (vector, 7), Qt);
9893
9894 if (!NILP (AREF (vector, 6)))
9895 {
9896 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9897 ASET (vector, 6, Qnil);
9898 }
9899
9900 Vmode_line_unwind_vector = vector;
9901 return Qnil;
9902 }
9903
9904
9905 /* Store a single character C for the frame title in mode_line_noprop_buf.
9906 Re-allocate mode_line_noprop_buf if necessary. */
9907
9908 static void
9909 store_mode_line_noprop_char (char c)
9910 {
9911 /* If output position has reached the end of the allocated buffer,
9912 double the buffer's size. */
9913 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9914 {
9915 int len = MODE_LINE_NOPROP_LEN (0);
9916 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9917 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9918 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9919 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9920 }
9921
9922 *mode_line_noprop_ptr++ = c;
9923 }
9924
9925
9926 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9927 mode_line_noprop_ptr. STRING is the string to store. Do not copy
9928 characters that yield more columns than PRECISION; PRECISION <= 0
9929 means copy the whole string. Pad with spaces until FIELD_WIDTH
9930 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9931 pad. Called from display_mode_element when it is used to build a
9932 frame title. */
9933
9934 static int
9935 store_mode_line_noprop (const char *string, int field_width, int precision)
9936 {
9937 const unsigned char *str = (const unsigned char *) string;
9938 int n = 0;
9939 EMACS_INT dummy, nbytes;
9940
9941 /* Copy at most PRECISION chars from STR. */
9942 nbytes = strlen (string);
9943 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9944 while (nbytes--)
9945 store_mode_line_noprop_char (*str++);
9946
9947 /* Fill up with spaces until FIELD_WIDTH reached. */
9948 while (field_width > 0
9949 && n < field_width)
9950 {
9951 store_mode_line_noprop_char (' ');
9952 ++n;
9953 }
9954
9955 return n;
9956 }
9957
9958 /***********************************************************************
9959 Frame Titles
9960 ***********************************************************************/
9961
9962 #ifdef HAVE_WINDOW_SYSTEM
9963
9964 /* Set the title of FRAME, if it has changed. The title format is
9965 Vicon_title_format if FRAME is iconified, otherwise it is
9966 frame_title_format. */
9967
9968 static void
9969 x_consider_frame_title (Lisp_Object frame)
9970 {
9971 struct frame *f = XFRAME (frame);
9972
9973 if (FRAME_WINDOW_P (f)
9974 || FRAME_MINIBUF_ONLY_P (f)
9975 || f->explicit_name)
9976 {
9977 /* Do we have more than one visible frame on this X display? */
9978 Lisp_Object tail;
9979 Lisp_Object fmt;
9980 int title_start;
9981 char *title;
9982 int len;
9983 struct it it;
9984 int count = SPECPDL_INDEX ();
9985
9986 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9987 {
9988 Lisp_Object other_frame = XCAR (tail);
9989 struct frame *tf = XFRAME (other_frame);
9990
9991 if (tf != f
9992 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9993 && !FRAME_MINIBUF_ONLY_P (tf)
9994 && !EQ (other_frame, tip_frame)
9995 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9996 break;
9997 }
9998
9999 /* Set global variable indicating that multiple frames exist. */
10000 multiple_frames = CONSP (tail);
10001
10002 /* Switch to the buffer of selected window of the frame. Set up
10003 mode_line_target so that display_mode_element will output into
10004 mode_line_noprop_buf; then display the title. */
10005 record_unwind_protect (unwind_format_mode_line,
10006 format_mode_line_unwind_data
10007 (current_buffer, selected_window, 0));
10008
10009 Fselect_window (f->selected_window, Qt);
10010 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10011 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10012
10013 mode_line_target = MODE_LINE_TITLE;
10014 title_start = MODE_LINE_NOPROP_LEN (0);
10015 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10016 NULL, DEFAULT_FACE_ID);
10017 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10018 len = MODE_LINE_NOPROP_LEN (title_start);
10019 title = mode_line_noprop_buf + title_start;
10020 unbind_to (count, Qnil);
10021
10022 /* Set the title only if it's changed. This avoids consing in
10023 the common case where it hasn't. (If it turns out that we've
10024 already wasted too much time by walking through the list with
10025 display_mode_element, then we might need to optimize at a
10026 higher level than this.) */
10027 if (! STRINGP (f->name)
10028 || SBYTES (f->name) != len
10029 || memcmp (title, SDATA (f->name), len) != 0)
10030 x_implicitly_set_name (f, make_string (title, len), Qnil);
10031 }
10032 }
10033
10034 #endif /* not HAVE_WINDOW_SYSTEM */
10035
10036
10037
10038 \f
10039 /***********************************************************************
10040 Menu Bars
10041 ***********************************************************************/
10042
10043
10044 /* Prepare for redisplay by updating menu-bar item lists when
10045 appropriate. This can call eval. */
10046
10047 void
10048 prepare_menu_bars (void)
10049 {
10050 int all_windows;
10051 struct gcpro gcpro1, gcpro2;
10052 struct frame *f;
10053 Lisp_Object tooltip_frame;
10054
10055 #ifdef HAVE_WINDOW_SYSTEM
10056 tooltip_frame = tip_frame;
10057 #else
10058 tooltip_frame = Qnil;
10059 #endif
10060
10061 /* Update all frame titles based on their buffer names, etc. We do
10062 this before the menu bars so that the buffer-menu will show the
10063 up-to-date frame titles. */
10064 #ifdef HAVE_WINDOW_SYSTEM
10065 if (windows_or_buffers_changed || update_mode_lines)
10066 {
10067 Lisp_Object tail, frame;
10068
10069 FOR_EACH_FRAME (tail, frame)
10070 {
10071 f = XFRAME (frame);
10072 if (!EQ (frame, tooltip_frame)
10073 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10074 x_consider_frame_title (frame);
10075 }
10076 }
10077 #endif /* HAVE_WINDOW_SYSTEM */
10078
10079 /* Update the menu bar item lists, if appropriate. This has to be
10080 done before any actual redisplay or generation of display lines. */
10081 all_windows = (update_mode_lines
10082 || buffer_shared > 1
10083 || windows_or_buffers_changed);
10084 if (all_windows)
10085 {
10086 Lisp_Object tail, frame;
10087 int count = SPECPDL_INDEX ();
10088 /* 1 means that update_menu_bar has run its hooks
10089 so any further calls to update_menu_bar shouldn't do so again. */
10090 int menu_bar_hooks_run = 0;
10091
10092 record_unwind_save_match_data ();
10093
10094 FOR_EACH_FRAME (tail, frame)
10095 {
10096 f = XFRAME (frame);
10097
10098 /* Ignore tooltip frame. */
10099 if (EQ (frame, tooltip_frame))
10100 continue;
10101
10102 /* If a window on this frame changed size, report that to
10103 the user and clear the size-change flag. */
10104 if (FRAME_WINDOW_SIZES_CHANGED (f))
10105 {
10106 Lisp_Object functions;
10107
10108 /* Clear flag first in case we get an error below. */
10109 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10110 functions = Vwindow_size_change_functions;
10111 GCPRO2 (tail, functions);
10112
10113 while (CONSP (functions))
10114 {
10115 if (!EQ (XCAR (functions), Qt))
10116 call1 (XCAR (functions), frame);
10117 functions = XCDR (functions);
10118 }
10119 UNGCPRO;
10120 }
10121
10122 GCPRO1 (tail);
10123 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10124 #ifdef HAVE_WINDOW_SYSTEM
10125 update_tool_bar (f, 0);
10126 #endif
10127 #ifdef HAVE_NS
10128 if (windows_or_buffers_changed
10129 && FRAME_NS_P (f))
10130 ns_set_doc_edited (f, Fbuffer_modified_p
10131 (XWINDOW (f->selected_window)->buffer));
10132 #endif
10133 UNGCPRO;
10134 }
10135
10136 unbind_to (count, Qnil);
10137 }
10138 else
10139 {
10140 struct frame *sf = SELECTED_FRAME ();
10141 update_menu_bar (sf, 1, 0);
10142 #ifdef HAVE_WINDOW_SYSTEM
10143 update_tool_bar (sf, 1);
10144 #endif
10145 }
10146 }
10147
10148
10149 /* Update the menu bar item list for frame F. This has to be done
10150 before we start to fill in any display lines, because it can call
10151 eval.
10152
10153 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10154
10155 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10156 already ran the menu bar hooks for this redisplay, so there
10157 is no need to run them again. The return value is the
10158 updated value of this flag, to pass to the next call. */
10159
10160 static int
10161 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10162 {
10163 Lisp_Object window;
10164 register struct window *w;
10165
10166 /* If called recursively during a menu update, do nothing. This can
10167 happen when, for instance, an activate-menubar-hook causes a
10168 redisplay. */
10169 if (inhibit_menubar_update)
10170 return hooks_run;
10171
10172 window = FRAME_SELECTED_WINDOW (f);
10173 w = XWINDOW (window);
10174
10175 if (FRAME_WINDOW_P (f)
10176 ?
10177 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10178 || defined (HAVE_NS) || defined (USE_GTK)
10179 FRAME_EXTERNAL_MENU_BAR (f)
10180 #else
10181 FRAME_MENU_BAR_LINES (f) > 0
10182 #endif
10183 : FRAME_MENU_BAR_LINES (f) > 0)
10184 {
10185 /* If the user has switched buffers or windows, we need to
10186 recompute to reflect the new bindings. But we'll
10187 recompute when update_mode_lines is set too; that means
10188 that people can use force-mode-line-update to request
10189 that the menu bar be recomputed. The adverse effect on
10190 the rest of the redisplay algorithm is about the same as
10191 windows_or_buffers_changed anyway. */
10192 if (windows_or_buffers_changed
10193 /* This used to test w->update_mode_line, but we believe
10194 there is no need to recompute the menu in that case. */
10195 || update_mode_lines
10196 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10197 < BUF_MODIFF (XBUFFER (w->buffer)))
10198 != !NILP (w->last_had_star))
10199 || ((!NILP (Vtransient_mark_mode)
10200 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10201 != !NILP (w->region_showing)))
10202 {
10203 struct buffer *prev = current_buffer;
10204 int count = SPECPDL_INDEX ();
10205
10206 specbind (Qinhibit_menubar_update, Qt);
10207
10208 set_buffer_internal_1 (XBUFFER (w->buffer));
10209 if (save_match_data)
10210 record_unwind_save_match_data ();
10211 if (NILP (Voverriding_local_map_menu_flag))
10212 {
10213 specbind (Qoverriding_terminal_local_map, Qnil);
10214 specbind (Qoverriding_local_map, Qnil);
10215 }
10216
10217 if (!hooks_run)
10218 {
10219 /* Run the Lucid hook. */
10220 safe_run_hooks (Qactivate_menubar_hook);
10221
10222 /* If it has changed current-menubar from previous value,
10223 really recompute the menu-bar from the value. */
10224 if (! NILP (Vlucid_menu_bar_dirty_flag))
10225 call0 (Qrecompute_lucid_menubar);
10226
10227 safe_run_hooks (Qmenu_bar_update_hook);
10228
10229 hooks_run = 1;
10230 }
10231
10232 XSETFRAME (Vmenu_updating_frame, f);
10233 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10234
10235 /* Redisplay the menu bar in case we changed it. */
10236 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10237 || defined (HAVE_NS) || defined (USE_GTK)
10238 if (FRAME_WINDOW_P (f))
10239 {
10240 #if defined (HAVE_NS)
10241 /* All frames on Mac OS share the same menubar. So only
10242 the selected frame should be allowed to set it. */
10243 if (f == SELECTED_FRAME ())
10244 #endif
10245 set_frame_menubar (f, 0, 0);
10246 }
10247 else
10248 /* On a terminal screen, the menu bar is an ordinary screen
10249 line, and this makes it get updated. */
10250 w->update_mode_line = Qt;
10251 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10252 /* In the non-toolkit version, the menu bar is an ordinary screen
10253 line, and this makes it get updated. */
10254 w->update_mode_line = Qt;
10255 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10256
10257 unbind_to (count, Qnil);
10258 set_buffer_internal_1 (prev);
10259 }
10260 }
10261
10262 return hooks_run;
10263 }
10264
10265
10266 \f
10267 /***********************************************************************
10268 Output Cursor
10269 ***********************************************************************/
10270
10271 #ifdef HAVE_WINDOW_SYSTEM
10272
10273 /* EXPORT:
10274 Nominal cursor position -- where to draw output.
10275 HPOS and VPOS are window relative glyph matrix coordinates.
10276 X and Y are window relative pixel coordinates. */
10277
10278 struct cursor_pos output_cursor;
10279
10280
10281 /* EXPORT:
10282 Set the global variable output_cursor to CURSOR. All cursor
10283 positions are relative to updated_window. */
10284
10285 void
10286 set_output_cursor (struct cursor_pos *cursor)
10287 {
10288 output_cursor.hpos = cursor->hpos;
10289 output_cursor.vpos = cursor->vpos;
10290 output_cursor.x = cursor->x;
10291 output_cursor.y = cursor->y;
10292 }
10293
10294
10295 /* EXPORT for RIF:
10296 Set a nominal cursor position.
10297
10298 HPOS and VPOS are column/row positions in a window glyph matrix. X
10299 and Y are window text area relative pixel positions.
10300
10301 If this is done during an update, updated_window will contain the
10302 window that is being updated and the position is the future output
10303 cursor position for that window. If updated_window is null, use
10304 selected_window and display the cursor at the given position. */
10305
10306 void
10307 x_cursor_to (int vpos, int hpos, int y, int x)
10308 {
10309 struct window *w;
10310
10311 /* If updated_window is not set, work on selected_window. */
10312 if (updated_window)
10313 w = updated_window;
10314 else
10315 w = XWINDOW (selected_window);
10316
10317 /* Set the output cursor. */
10318 output_cursor.hpos = hpos;
10319 output_cursor.vpos = vpos;
10320 output_cursor.x = x;
10321 output_cursor.y = y;
10322
10323 /* If not called as part of an update, really display the cursor.
10324 This will also set the cursor position of W. */
10325 if (updated_window == NULL)
10326 {
10327 BLOCK_INPUT;
10328 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10329 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10330 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10331 UNBLOCK_INPUT;
10332 }
10333 }
10334
10335 #endif /* HAVE_WINDOW_SYSTEM */
10336
10337 \f
10338 /***********************************************************************
10339 Tool-bars
10340 ***********************************************************************/
10341
10342 #ifdef HAVE_WINDOW_SYSTEM
10343
10344 /* Where the mouse was last time we reported a mouse event. */
10345
10346 FRAME_PTR last_mouse_frame;
10347
10348 /* Tool-bar item index of the item on which a mouse button was pressed
10349 or -1. */
10350
10351 int last_tool_bar_item;
10352
10353
10354 static Lisp_Object
10355 update_tool_bar_unwind (Lisp_Object frame)
10356 {
10357 selected_frame = frame;
10358 return Qnil;
10359 }
10360
10361 /* Update the tool-bar item list for frame F. This has to be done
10362 before we start to fill in any display lines. Called from
10363 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10364 and restore it here. */
10365
10366 static void
10367 update_tool_bar (struct frame *f, int save_match_data)
10368 {
10369 #if defined (USE_GTK) || defined (HAVE_NS)
10370 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10371 #else
10372 int do_update = WINDOWP (f->tool_bar_window)
10373 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10374 #endif
10375
10376 if (do_update)
10377 {
10378 Lisp_Object window;
10379 struct window *w;
10380
10381 window = FRAME_SELECTED_WINDOW (f);
10382 w = XWINDOW (window);
10383
10384 /* If the user has switched buffers or windows, we need to
10385 recompute to reflect the new bindings. But we'll
10386 recompute when update_mode_lines is set too; that means
10387 that people can use force-mode-line-update to request
10388 that the menu bar be recomputed. The adverse effect on
10389 the rest of the redisplay algorithm is about the same as
10390 windows_or_buffers_changed anyway. */
10391 if (windows_or_buffers_changed
10392 || !NILP (w->update_mode_line)
10393 || update_mode_lines
10394 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10395 < BUF_MODIFF (XBUFFER (w->buffer)))
10396 != !NILP (w->last_had_star))
10397 || ((!NILP (Vtransient_mark_mode)
10398 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10399 != !NILP (w->region_showing)))
10400 {
10401 struct buffer *prev = current_buffer;
10402 int count = SPECPDL_INDEX ();
10403 Lisp_Object frame, new_tool_bar;
10404 int new_n_tool_bar;
10405 struct gcpro gcpro1;
10406
10407 /* Set current_buffer to the buffer of the selected
10408 window of the frame, so that we get the right local
10409 keymaps. */
10410 set_buffer_internal_1 (XBUFFER (w->buffer));
10411
10412 /* Save match data, if we must. */
10413 if (save_match_data)
10414 record_unwind_save_match_data ();
10415
10416 /* Make sure that we don't accidentally use bogus keymaps. */
10417 if (NILP (Voverriding_local_map_menu_flag))
10418 {
10419 specbind (Qoverriding_terminal_local_map, Qnil);
10420 specbind (Qoverriding_local_map, Qnil);
10421 }
10422
10423 GCPRO1 (new_tool_bar);
10424
10425 /* We must temporarily set the selected frame to this frame
10426 before calling tool_bar_items, because the calculation of
10427 the tool-bar keymap uses the selected frame (see
10428 `tool-bar-make-keymap' in tool-bar.el). */
10429 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10430 XSETFRAME (frame, f);
10431 selected_frame = frame;
10432
10433 /* Build desired tool-bar items from keymaps. */
10434 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10435 &new_n_tool_bar);
10436
10437 /* Redisplay the tool-bar if we changed it. */
10438 if (new_n_tool_bar != f->n_tool_bar_items
10439 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10440 {
10441 /* Redisplay that happens asynchronously due to an expose event
10442 may access f->tool_bar_items. Make sure we update both
10443 variables within BLOCK_INPUT so no such event interrupts. */
10444 BLOCK_INPUT;
10445 f->tool_bar_items = new_tool_bar;
10446 f->n_tool_bar_items = new_n_tool_bar;
10447 w->update_mode_line = Qt;
10448 UNBLOCK_INPUT;
10449 }
10450
10451 UNGCPRO;
10452
10453 unbind_to (count, Qnil);
10454 set_buffer_internal_1 (prev);
10455 }
10456 }
10457 }
10458
10459
10460 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10461 F's desired tool-bar contents. F->tool_bar_items must have
10462 been set up previously by calling prepare_menu_bars. */
10463
10464 static void
10465 build_desired_tool_bar_string (struct frame *f)
10466 {
10467 int i, size, size_needed;
10468 struct gcpro gcpro1, gcpro2, gcpro3;
10469 Lisp_Object image, plist, props;
10470
10471 image = plist = props = Qnil;
10472 GCPRO3 (image, plist, props);
10473
10474 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10475 Otherwise, make a new string. */
10476
10477 /* The size of the string we might be able to reuse. */
10478 size = (STRINGP (f->desired_tool_bar_string)
10479 ? SCHARS (f->desired_tool_bar_string)
10480 : 0);
10481
10482 /* We need one space in the string for each image. */
10483 size_needed = f->n_tool_bar_items;
10484
10485 /* Reuse f->desired_tool_bar_string, if possible. */
10486 if (size < size_needed || NILP (f->desired_tool_bar_string))
10487 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10488 make_number (' '));
10489 else
10490 {
10491 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10492 Fremove_text_properties (make_number (0), make_number (size),
10493 props, f->desired_tool_bar_string);
10494 }
10495
10496 /* Put a `display' property on the string for the images to display,
10497 put a `menu_item' property on tool-bar items with a value that
10498 is the index of the item in F's tool-bar item vector. */
10499 for (i = 0; i < f->n_tool_bar_items; ++i)
10500 {
10501 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10502
10503 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10504 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10505 int hmargin, vmargin, relief, idx, end;
10506
10507 /* If image is a vector, choose the image according to the
10508 button state. */
10509 image = PROP (TOOL_BAR_ITEM_IMAGES);
10510 if (VECTORP (image))
10511 {
10512 if (enabled_p)
10513 idx = (selected_p
10514 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10515 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10516 else
10517 idx = (selected_p
10518 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10519 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10520
10521 xassert (ASIZE (image) >= idx);
10522 image = AREF (image, idx);
10523 }
10524 else
10525 idx = -1;
10526
10527 /* Ignore invalid image specifications. */
10528 if (!valid_image_p (image))
10529 continue;
10530
10531 /* Display the tool-bar button pressed, or depressed. */
10532 plist = Fcopy_sequence (XCDR (image));
10533
10534 /* Compute margin and relief to draw. */
10535 relief = (tool_bar_button_relief >= 0
10536 ? tool_bar_button_relief
10537 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10538 hmargin = vmargin = relief;
10539
10540 if (INTEGERP (Vtool_bar_button_margin)
10541 && XINT (Vtool_bar_button_margin) > 0)
10542 {
10543 hmargin += XFASTINT (Vtool_bar_button_margin);
10544 vmargin += XFASTINT (Vtool_bar_button_margin);
10545 }
10546 else if (CONSP (Vtool_bar_button_margin))
10547 {
10548 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10549 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10550 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10551
10552 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10553 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10554 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10555 }
10556
10557 if (auto_raise_tool_bar_buttons_p)
10558 {
10559 /* Add a `:relief' property to the image spec if the item is
10560 selected. */
10561 if (selected_p)
10562 {
10563 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10564 hmargin -= relief;
10565 vmargin -= relief;
10566 }
10567 }
10568 else
10569 {
10570 /* If image is selected, display it pressed, i.e. with a
10571 negative relief. If it's not selected, display it with a
10572 raised relief. */
10573 plist = Fplist_put (plist, QCrelief,
10574 (selected_p
10575 ? make_number (-relief)
10576 : make_number (relief)));
10577 hmargin -= relief;
10578 vmargin -= relief;
10579 }
10580
10581 /* Put a margin around the image. */
10582 if (hmargin || vmargin)
10583 {
10584 if (hmargin == vmargin)
10585 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10586 else
10587 plist = Fplist_put (plist, QCmargin,
10588 Fcons (make_number (hmargin),
10589 make_number (vmargin)));
10590 }
10591
10592 /* If button is not enabled, and we don't have special images
10593 for the disabled state, make the image appear disabled by
10594 applying an appropriate algorithm to it. */
10595 if (!enabled_p && idx < 0)
10596 plist = Fplist_put (plist, QCconversion, Qdisabled);
10597
10598 /* Put a `display' text property on the string for the image to
10599 display. Put a `menu-item' property on the string that gives
10600 the start of this item's properties in the tool-bar items
10601 vector. */
10602 image = Fcons (Qimage, plist);
10603 props = list4 (Qdisplay, image,
10604 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10605
10606 /* Let the last image hide all remaining spaces in the tool bar
10607 string. The string can be longer than needed when we reuse a
10608 previous string. */
10609 if (i + 1 == f->n_tool_bar_items)
10610 end = SCHARS (f->desired_tool_bar_string);
10611 else
10612 end = i + 1;
10613 Fadd_text_properties (make_number (i), make_number (end),
10614 props, f->desired_tool_bar_string);
10615 #undef PROP
10616 }
10617
10618 UNGCPRO;
10619 }
10620
10621
10622 /* Display one line of the tool-bar of frame IT->f.
10623
10624 HEIGHT specifies the desired height of the tool-bar line.
10625 If the actual height of the glyph row is less than HEIGHT, the
10626 row's height is increased to HEIGHT, and the icons are centered
10627 vertically in the new height.
10628
10629 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10630 count a final empty row in case the tool-bar width exactly matches
10631 the window width.
10632 */
10633
10634 static void
10635 display_tool_bar_line (struct it *it, int height)
10636 {
10637 struct glyph_row *row = it->glyph_row;
10638 int max_x = it->last_visible_x;
10639 struct glyph *last;
10640
10641 prepare_desired_row (row);
10642 row->y = it->current_y;
10643
10644 /* Note that this isn't made use of if the face hasn't a box,
10645 so there's no need to check the face here. */
10646 it->start_of_box_run_p = 1;
10647
10648 while (it->current_x < max_x)
10649 {
10650 int x, n_glyphs_before, i, nglyphs;
10651 struct it it_before;
10652
10653 /* Get the next display element. */
10654 if (!get_next_display_element (it))
10655 {
10656 /* Don't count empty row if we are counting needed tool-bar lines. */
10657 if (height < 0 && !it->hpos)
10658 return;
10659 break;
10660 }
10661
10662 /* Produce glyphs. */
10663 n_glyphs_before = row->used[TEXT_AREA];
10664 it_before = *it;
10665
10666 PRODUCE_GLYPHS (it);
10667
10668 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10669 i = 0;
10670 x = it_before.current_x;
10671 while (i < nglyphs)
10672 {
10673 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10674
10675 if (x + glyph->pixel_width > max_x)
10676 {
10677 /* Glyph doesn't fit on line. Backtrack. */
10678 row->used[TEXT_AREA] = n_glyphs_before;
10679 *it = it_before;
10680 /* If this is the only glyph on this line, it will never fit on the
10681 tool-bar, so skip it. But ensure there is at least one glyph,
10682 so we don't accidentally disable the tool-bar. */
10683 if (n_glyphs_before == 0
10684 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10685 break;
10686 goto out;
10687 }
10688
10689 ++it->hpos;
10690 x += glyph->pixel_width;
10691 ++i;
10692 }
10693
10694 /* Stop at line ends. */
10695 if (ITERATOR_AT_END_OF_LINE_P (it))
10696 break;
10697
10698 set_iterator_to_next (it, 1);
10699 }
10700
10701 out:;
10702
10703 row->displays_text_p = row->used[TEXT_AREA] != 0;
10704
10705 /* Use default face for the border below the tool bar.
10706
10707 FIXME: When auto-resize-tool-bars is grow-only, there is
10708 no additional border below the possibly empty tool-bar lines.
10709 So to make the extra empty lines look "normal", we have to
10710 use the tool-bar face for the border too. */
10711 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10712 it->face_id = DEFAULT_FACE_ID;
10713
10714 extend_face_to_end_of_line (it);
10715 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10716 last->right_box_line_p = 1;
10717 if (last == row->glyphs[TEXT_AREA])
10718 last->left_box_line_p = 1;
10719
10720 /* Make line the desired height and center it vertically. */
10721 if ((height -= it->max_ascent + it->max_descent) > 0)
10722 {
10723 /* Don't add more than one line height. */
10724 height %= FRAME_LINE_HEIGHT (it->f);
10725 it->max_ascent += height / 2;
10726 it->max_descent += (height + 1) / 2;
10727 }
10728
10729 compute_line_metrics (it);
10730
10731 /* If line is empty, make it occupy the rest of the tool-bar. */
10732 if (!row->displays_text_p)
10733 {
10734 row->height = row->phys_height = it->last_visible_y - row->y;
10735 row->visible_height = row->height;
10736 row->ascent = row->phys_ascent = 0;
10737 row->extra_line_spacing = 0;
10738 }
10739
10740 row->full_width_p = 1;
10741 row->continued_p = 0;
10742 row->truncated_on_left_p = 0;
10743 row->truncated_on_right_p = 0;
10744
10745 it->current_x = it->hpos = 0;
10746 it->current_y += row->height;
10747 ++it->vpos;
10748 ++it->glyph_row;
10749 }
10750
10751
10752 /* Max tool-bar height. */
10753
10754 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10755 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10756
10757 /* Value is the number of screen lines needed to make all tool-bar
10758 items of frame F visible. The number of actual rows needed is
10759 returned in *N_ROWS if non-NULL. */
10760
10761 static int
10762 tool_bar_lines_needed (struct frame *f, int *n_rows)
10763 {
10764 struct window *w = XWINDOW (f->tool_bar_window);
10765 struct it it;
10766 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10767 the desired matrix, so use (unused) mode-line row as temporary row to
10768 avoid destroying the first tool-bar row. */
10769 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10770
10771 /* Initialize an iterator for iteration over
10772 F->desired_tool_bar_string in the tool-bar window of frame F. */
10773 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10774 it.first_visible_x = 0;
10775 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10776 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10777
10778 while (!ITERATOR_AT_END_P (&it))
10779 {
10780 clear_glyph_row (temp_row);
10781 it.glyph_row = temp_row;
10782 display_tool_bar_line (&it, -1);
10783 }
10784 clear_glyph_row (temp_row);
10785
10786 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10787 if (n_rows)
10788 *n_rows = it.vpos > 0 ? it.vpos : -1;
10789
10790 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10791 }
10792
10793
10794 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10795 0, 1, 0,
10796 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10797 (Lisp_Object frame)
10798 {
10799 struct frame *f;
10800 struct window *w;
10801 int nlines = 0;
10802
10803 if (NILP (frame))
10804 frame = selected_frame;
10805 else
10806 CHECK_FRAME (frame);
10807 f = XFRAME (frame);
10808
10809 if (WINDOWP (f->tool_bar_window)
10810 || (w = XWINDOW (f->tool_bar_window),
10811 WINDOW_TOTAL_LINES (w) > 0))
10812 {
10813 update_tool_bar (f, 1);
10814 if (f->n_tool_bar_items)
10815 {
10816 build_desired_tool_bar_string (f);
10817 nlines = tool_bar_lines_needed (f, NULL);
10818 }
10819 }
10820
10821 return make_number (nlines);
10822 }
10823
10824
10825 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10826 height should be changed. */
10827
10828 static int
10829 redisplay_tool_bar (struct frame *f)
10830 {
10831 struct window *w;
10832 struct it it;
10833 struct glyph_row *row;
10834
10835 #if defined (USE_GTK) || defined (HAVE_NS)
10836 if (FRAME_EXTERNAL_TOOL_BAR (f))
10837 update_frame_tool_bar (f);
10838 return 0;
10839 #endif
10840
10841 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10842 do anything. This means you must start with tool-bar-lines
10843 non-zero to get the auto-sizing effect. Or in other words, you
10844 can turn off tool-bars by specifying tool-bar-lines zero. */
10845 if (!WINDOWP (f->tool_bar_window)
10846 || (w = XWINDOW (f->tool_bar_window),
10847 WINDOW_TOTAL_LINES (w) == 0))
10848 return 0;
10849
10850 /* Set up an iterator for the tool-bar window. */
10851 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10852 it.first_visible_x = 0;
10853 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10854 row = it.glyph_row;
10855
10856 /* Build a string that represents the contents of the tool-bar. */
10857 build_desired_tool_bar_string (f);
10858 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10859
10860 if (f->n_tool_bar_rows == 0)
10861 {
10862 int nlines;
10863
10864 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10865 nlines != WINDOW_TOTAL_LINES (w)))
10866 {
10867 Lisp_Object frame;
10868 int old_height = WINDOW_TOTAL_LINES (w);
10869
10870 XSETFRAME (frame, f);
10871 Fmodify_frame_parameters (frame,
10872 Fcons (Fcons (Qtool_bar_lines,
10873 make_number (nlines)),
10874 Qnil));
10875 if (WINDOW_TOTAL_LINES (w) != old_height)
10876 {
10877 clear_glyph_matrix (w->desired_matrix);
10878 fonts_changed_p = 1;
10879 return 1;
10880 }
10881 }
10882 }
10883
10884 /* Display as many lines as needed to display all tool-bar items. */
10885
10886 if (f->n_tool_bar_rows > 0)
10887 {
10888 int border, rows, height, extra;
10889
10890 if (INTEGERP (Vtool_bar_border))
10891 border = XINT (Vtool_bar_border);
10892 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10893 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10894 else if (EQ (Vtool_bar_border, Qborder_width))
10895 border = f->border_width;
10896 else
10897 border = 0;
10898 if (border < 0)
10899 border = 0;
10900
10901 rows = f->n_tool_bar_rows;
10902 height = max (1, (it.last_visible_y - border) / rows);
10903 extra = it.last_visible_y - border - height * rows;
10904
10905 while (it.current_y < it.last_visible_y)
10906 {
10907 int h = 0;
10908 if (extra > 0 && rows-- > 0)
10909 {
10910 h = (extra + rows - 1) / rows;
10911 extra -= h;
10912 }
10913 display_tool_bar_line (&it, height + h);
10914 }
10915 }
10916 else
10917 {
10918 while (it.current_y < it.last_visible_y)
10919 display_tool_bar_line (&it, 0);
10920 }
10921
10922 /* It doesn't make much sense to try scrolling in the tool-bar
10923 window, so don't do it. */
10924 w->desired_matrix->no_scrolling_p = 1;
10925 w->must_be_updated_p = 1;
10926
10927 if (!NILP (Vauto_resize_tool_bars))
10928 {
10929 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10930 int change_height_p = 0;
10931
10932 /* If we couldn't display everything, change the tool-bar's
10933 height if there is room for more. */
10934 if (IT_STRING_CHARPOS (it) < it.end_charpos
10935 && it.current_y < max_tool_bar_height)
10936 change_height_p = 1;
10937
10938 row = it.glyph_row - 1;
10939
10940 /* If there are blank lines at the end, except for a partially
10941 visible blank line at the end that is smaller than
10942 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10943 if (!row->displays_text_p
10944 && row->height >= FRAME_LINE_HEIGHT (f))
10945 change_height_p = 1;
10946
10947 /* If row displays tool-bar items, but is partially visible,
10948 change the tool-bar's height. */
10949 if (row->displays_text_p
10950 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10951 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10952 change_height_p = 1;
10953
10954 /* Resize windows as needed by changing the `tool-bar-lines'
10955 frame parameter. */
10956 if (change_height_p)
10957 {
10958 Lisp_Object frame;
10959 int old_height = WINDOW_TOTAL_LINES (w);
10960 int nrows;
10961 int nlines = tool_bar_lines_needed (f, &nrows);
10962
10963 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10964 && !f->minimize_tool_bar_window_p)
10965 ? (nlines > old_height)
10966 : (nlines != old_height));
10967 f->minimize_tool_bar_window_p = 0;
10968
10969 if (change_height_p)
10970 {
10971 XSETFRAME (frame, f);
10972 Fmodify_frame_parameters (frame,
10973 Fcons (Fcons (Qtool_bar_lines,
10974 make_number (nlines)),
10975 Qnil));
10976 if (WINDOW_TOTAL_LINES (w) != old_height)
10977 {
10978 clear_glyph_matrix (w->desired_matrix);
10979 f->n_tool_bar_rows = nrows;
10980 fonts_changed_p = 1;
10981 return 1;
10982 }
10983 }
10984 }
10985 }
10986
10987 f->minimize_tool_bar_window_p = 0;
10988 return 0;
10989 }
10990
10991
10992 /* Get information about the tool-bar item which is displayed in GLYPH
10993 on frame F. Return in *PROP_IDX the index where tool-bar item
10994 properties start in F->tool_bar_items. Value is zero if
10995 GLYPH doesn't display a tool-bar item. */
10996
10997 static int
10998 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10999 {
11000 Lisp_Object prop;
11001 int success_p;
11002 int charpos;
11003
11004 /* This function can be called asynchronously, which means we must
11005 exclude any possibility that Fget_text_property signals an
11006 error. */
11007 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11008 charpos = max (0, charpos);
11009
11010 /* Get the text property `menu-item' at pos. The value of that
11011 property is the start index of this item's properties in
11012 F->tool_bar_items. */
11013 prop = Fget_text_property (make_number (charpos),
11014 Qmenu_item, f->current_tool_bar_string);
11015 if (INTEGERP (prop))
11016 {
11017 *prop_idx = XINT (prop);
11018 success_p = 1;
11019 }
11020 else
11021 success_p = 0;
11022
11023 return success_p;
11024 }
11025
11026 \f
11027 /* Get information about the tool-bar item at position X/Y on frame F.
11028 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11029 the current matrix of the tool-bar window of F, or NULL if not
11030 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11031 item in F->tool_bar_items. Value is
11032
11033 -1 if X/Y is not on a tool-bar item
11034 0 if X/Y is on the same item that was highlighted before.
11035 1 otherwise. */
11036
11037 static int
11038 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11039 int *hpos, int *vpos, int *prop_idx)
11040 {
11041 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11042 struct window *w = XWINDOW (f->tool_bar_window);
11043 int area;
11044
11045 /* Find the glyph under X/Y. */
11046 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11047 if (*glyph == NULL)
11048 return -1;
11049
11050 /* Get the start of this tool-bar item's properties in
11051 f->tool_bar_items. */
11052 if (!tool_bar_item_info (f, *glyph, prop_idx))
11053 return -1;
11054
11055 /* Is mouse on the highlighted item? */
11056 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11057 && *vpos >= hlinfo->mouse_face_beg_row
11058 && *vpos <= hlinfo->mouse_face_end_row
11059 && (*vpos > hlinfo->mouse_face_beg_row
11060 || *hpos >= hlinfo->mouse_face_beg_col)
11061 && (*vpos < hlinfo->mouse_face_end_row
11062 || *hpos < hlinfo->mouse_face_end_col
11063 || hlinfo->mouse_face_past_end))
11064 return 0;
11065
11066 return 1;
11067 }
11068
11069
11070 /* EXPORT:
11071 Handle mouse button event on the tool-bar of frame F, at
11072 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11073 0 for button release. MODIFIERS is event modifiers for button
11074 release. */
11075
11076 void
11077 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11078 unsigned int modifiers)
11079 {
11080 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11081 struct window *w = XWINDOW (f->tool_bar_window);
11082 int hpos, vpos, prop_idx;
11083 struct glyph *glyph;
11084 Lisp_Object enabled_p;
11085
11086 /* If not on the highlighted tool-bar item, return. */
11087 frame_to_window_pixel_xy (w, &x, &y);
11088 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11089 return;
11090
11091 /* If item is disabled, do nothing. */
11092 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11093 if (NILP (enabled_p))
11094 return;
11095
11096 if (down_p)
11097 {
11098 /* Show item in pressed state. */
11099 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11100 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11101 last_tool_bar_item = prop_idx;
11102 }
11103 else
11104 {
11105 Lisp_Object key, frame;
11106 struct input_event event;
11107 EVENT_INIT (event);
11108
11109 /* Show item in released state. */
11110 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11111 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11112
11113 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11114
11115 XSETFRAME (frame, f);
11116 event.kind = TOOL_BAR_EVENT;
11117 event.frame_or_window = frame;
11118 event.arg = frame;
11119 kbd_buffer_store_event (&event);
11120
11121 event.kind = TOOL_BAR_EVENT;
11122 event.frame_or_window = frame;
11123 event.arg = key;
11124 event.modifiers = modifiers;
11125 kbd_buffer_store_event (&event);
11126 last_tool_bar_item = -1;
11127 }
11128 }
11129
11130
11131 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11132 tool-bar window-relative coordinates X/Y. Called from
11133 note_mouse_highlight. */
11134
11135 static void
11136 note_tool_bar_highlight (struct frame *f, int x, int y)
11137 {
11138 Lisp_Object window = f->tool_bar_window;
11139 struct window *w = XWINDOW (window);
11140 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11141 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11142 int hpos, vpos;
11143 struct glyph *glyph;
11144 struct glyph_row *row;
11145 int i;
11146 Lisp_Object enabled_p;
11147 int prop_idx;
11148 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11149 int mouse_down_p, rc;
11150
11151 /* Function note_mouse_highlight is called with negative X/Y
11152 values when mouse moves outside of the frame. */
11153 if (x <= 0 || y <= 0)
11154 {
11155 clear_mouse_face (hlinfo);
11156 return;
11157 }
11158
11159 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11160 if (rc < 0)
11161 {
11162 /* Not on tool-bar item. */
11163 clear_mouse_face (hlinfo);
11164 return;
11165 }
11166 else if (rc == 0)
11167 /* On same tool-bar item as before. */
11168 goto set_help_echo;
11169
11170 clear_mouse_face (hlinfo);
11171
11172 /* Mouse is down, but on different tool-bar item? */
11173 mouse_down_p = (dpyinfo->grabbed
11174 && f == last_mouse_frame
11175 && FRAME_LIVE_P (f));
11176 if (mouse_down_p
11177 && last_tool_bar_item != prop_idx)
11178 return;
11179
11180 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11181 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11182
11183 /* If tool-bar item is not enabled, don't highlight it. */
11184 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11185 if (!NILP (enabled_p))
11186 {
11187 /* Compute the x-position of the glyph. In front and past the
11188 image is a space. We include this in the highlighted area. */
11189 row = MATRIX_ROW (w->current_matrix, vpos);
11190 for (i = x = 0; i < hpos; ++i)
11191 x += row->glyphs[TEXT_AREA][i].pixel_width;
11192
11193 /* Record this as the current active region. */
11194 hlinfo->mouse_face_beg_col = hpos;
11195 hlinfo->mouse_face_beg_row = vpos;
11196 hlinfo->mouse_face_beg_x = x;
11197 hlinfo->mouse_face_beg_y = row->y;
11198 hlinfo->mouse_face_past_end = 0;
11199
11200 hlinfo->mouse_face_end_col = hpos + 1;
11201 hlinfo->mouse_face_end_row = vpos;
11202 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11203 hlinfo->mouse_face_end_y = row->y;
11204 hlinfo->mouse_face_window = window;
11205 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11206
11207 /* Display it as active. */
11208 show_mouse_face (hlinfo, draw);
11209 hlinfo->mouse_face_image_state = draw;
11210 }
11211
11212 set_help_echo:
11213
11214 /* Set help_echo_string to a help string to display for this tool-bar item.
11215 XTread_socket does the rest. */
11216 help_echo_object = help_echo_window = Qnil;
11217 help_echo_pos = -1;
11218 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11219 if (NILP (help_echo_string))
11220 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11221 }
11222
11223 #endif /* HAVE_WINDOW_SYSTEM */
11224
11225
11226 \f
11227 /************************************************************************
11228 Horizontal scrolling
11229 ************************************************************************/
11230
11231 static int hscroll_window_tree (Lisp_Object);
11232 static int hscroll_windows (Lisp_Object);
11233
11234 /* For all leaf windows in the window tree rooted at WINDOW, set their
11235 hscroll value so that PT is (i) visible in the window, and (ii) so
11236 that it is not within a certain margin at the window's left and
11237 right border. Value is non-zero if any window's hscroll has been
11238 changed. */
11239
11240 static int
11241 hscroll_window_tree (Lisp_Object window)
11242 {
11243 int hscrolled_p = 0;
11244 int hscroll_relative_p = FLOATP (Vhscroll_step);
11245 int hscroll_step_abs = 0;
11246 double hscroll_step_rel = 0;
11247
11248 if (hscroll_relative_p)
11249 {
11250 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11251 if (hscroll_step_rel < 0)
11252 {
11253 hscroll_relative_p = 0;
11254 hscroll_step_abs = 0;
11255 }
11256 }
11257 else if (INTEGERP (Vhscroll_step))
11258 {
11259 hscroll_step_abs = XINT (Vhscroll_step);
11260 if (hscroll_step_abs < 0)
11261 hscroll_step_abs = 0;
11262 }
11263 else
11264 hscroll_step_abs = 0;
11265
11266 while (WINDOWP (window))
11267 {
11268 struct window *w = XWINDOW (window);
11269
11270 if (WINDOWP (w->hchild))
11271 hscrolled_p |= hscroll_window_tree (w->hchild);
11272 else if (WINDOWP (w->vchild))
11273 hscrolled_p |= hscroll_window_tree (w->vchild);
11274 else if (w->cursor.vpos >= 0)
11275 {
11276 int h_margin;
11277 int text_area_width;
11278 struct glyph_row *current_cursor_row
11279 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11280 struct glyph_row *desired_cursor_row
11281 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11282 struct glyph_row *cursor_row
11283 = (desired_cursor_row->enabled_p
11284 ? desired_cursor_row
11285 : current_cursor_row);
11286
11287 text_area_width = window_box_width (w, TEXT_AREA);
11288
11289 /* Scroll when cursor is inside this scroll margin. */
11290 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11291
11292 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11293 && ((XFASTINT (w->hscroll)
11294 && w->cursor.x <= h_margin)
11295 || (cursor_row->enabled_p
11296 && cursor_row->truncated_on_right_p
11297 && (w->cursor.x >= text_area_width - h_margin))))
11298 {
11299 struct it it;
11300 int hscroll;
11301 struct buffer *saved_current_buffer;
11302 EMACS_INT pt;
11303 int wanted_x;
11304
11305 /* Find point in a display of infinite width. */
11306 saved_current_buffer = current_buffer;
11307 current_buffer = XBUFFER (w->buffer);
11308
11309 if (w == XWINDOW (selected_window))
11310 pt = PT;
11311 else
11312 {
11313 pt = marker_position (w->pointm);
11314 pt = max (BEGV, pt);
11315 pt = min (ZV, pt);
11316 }
11317
11318 /* Move iterator to pt starting at cursor_row->start in
11319 a line with infinite width. */
11320 init_to_row_start (&it, w, cursor_row);
11321 it.last_visible_x = INFINITY;
11322 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11323 current_buffer = saved_current_buffer;
11324
11325 /* Position cursor in window. */
11326 if (!hscroll_relative_p && hscroll_step_abs == 0)
11327 hscroll = max (0, (it.current_x
11328 - (ITERATOR_AT_END_OF_LINE_P (&it)
11329 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11330 : (text_area_width / 2))))
11331 / FRAME_COLUMN_WIDTH (it.f);
11332 else if (w->cursor.x >= text_area_width - h_margin)
11333 {
11334 if (hscroll_relative_p)
11335 wanted_x = text_area_width * (1 - hscroll_step_rel)
11336 - h_margin;
11337 else
11338 wanted_x = text_area_width
11339 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11340 - h_margin;
11341 hscroll
11342 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11343 }
11344 else
11345 {
11346 if (hscroll_relative_p)
11347 wanted_x = text_area_width * hscroll_step_rel
11348 + h_margin;
11349 else
11350 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11351 + h_margin;
11352 hscroll
11353 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11354 }
11355 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11356
11357 /* Don't call Fset_window_hscroll if value hasn't
11358 changed because it will prevent redisplay
11359 optimizations. */
11360 if (XFASTINT (w->hscroll) != hscroll)
11361 {
11362 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11363 w->hscroll = make_number (hscroll);
11364 hscrolled_p = 1;
11365 }
11366 }
11367 }
11368
11369 window = w->next;
11370 }
11371
11372 /* Value is non-zero if hscroll of any leaf window has been changed. */
11373 return hscrolled_p;
11374 }
11375
11376
11377 /* Set hscroll so that cursor is visible and not inside horizontal
11378 scroll margins for all windows in the tree rooted at WINDOW. See
11379 also hscroll_window_tree above. Value is non-zero if any window's
11380 hscroll has been changed. If it has, desired matrices on the frame
11381 of WINDOW are cleared. */
11382
11383 static int
11384 hscroll_windows (Lisp_Object window)
11385 {
11386 int hscrolled_p = hscroll_window_tree (window);
11387 if (hscrolled_p)
11388 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11389 return hscrolled_p;
11390 }
11391
11392
11393 \f
11394 /************************************************************************
11395 Redisplay
11396 ************************************************************************/
11397
11398 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11399 to a non-zero value. This is sometimes handy to have in a debugger
11400 session. */
11401
11402 #if GLYPH_DEBUG
11403
11404 /* First and last unchanged row for try_window_id. */
11405
11406 int debug_first_unchanged_at_end_vpos;
11407 int debug_last_unchanged_at_beg_vpos;
11408
11409 /* Delta vpos and y. */
11410
11411 int debug_dvpos, debug_dy;
11412
11413 /* Delta in characters and bytes for try_window_id. */
11414
11415 EMACS_INT debug_delta, debug_delta_bytes;
11416
11417 /* Values of window_end_pos and window_end_vpos at the end of
11418 try_window_id. */
11419
11420 EMACS_INT debug_end_vpos;
11421
11422 /* Append a string to W->desired_matrix->method. FMT is a printf
11423 format string. A1...A9 are a supplement for a variable-length
11424 argument list. If trace_redisplay_p is non-zero also printf the
11425 resulting string to stderr. */
11426
11427 static void
11428 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11429 struct window *w;
11430 char *fmt;
11431 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11432 {
11433 char buffer[512];
11434 char *method = w->desired_matrix->method;
11435 int len = strlen (method);
11436 int size = sizeof w->desired_matrix->method;
11437 int remaining = size - len - 1;
11438
11439 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11440 if (len && remaining)
11441 {
11442 method[len] = '|';
11443 --remaining, ++len;
11444 }
11445
11446 strncpy (method + len, buffer, remaining);
11447
11448 if (trace_redisplay_p)
11449 fprintf (stderr, "%p (%s): %s\n",
11450 w,
11451 ((BUFFERP (w->buffer)
11452 && STRINGP (XBUFFER (w->buffer)->name))
11453 ? SSDATA (XBUFFER (w->buffer)->name)
11454 : "no buffer"),
11455 buffer);
11456 }
11457
11458 #endif /* GLYPH_DEBUG */
11459
11460
11461 /* Value is non-zero if all changes in window W, which displays
11462 current_buffer, are in the text between START and END. START is a
11463 buffer position, END is given as a distance from Z. Used in
11464 redisplay_internal for display optimization. */
11465
11466 static INLINE int
11467 text_outside_line_unchanged_p (struct window *w,
11468 EMACS_INT start, EMACS_INT end)
11469 {
11470 int unchanged_p = 1;
11471
11472 /* If text or overlays have changed, see where. */
11473 if (XFASTINT (w->last_modified) < MODIFF
11474 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11475 {
11476 /* Gap in the line? */
11477 if (GPT < start || Z - GPT < end)
11478 unchanged_p = 0;
11479
11480 /* Changes start in front of the line, or end after it? */
11481 if (unchanged_p
11482 && (BEG_UNCHANGED < start - 1
11483 || END_UNCHANGED < end))
11484 unchanged_p = 0;
11485
11486 /* If selective display, can't optimize if changes start at the
11487 beginning of the line. */
11488 if (unchanged_p
11489 && INTEGERP (BVAR (current_buffer, selective_display))
11490 && XINT (BVAR (current_buffer, selective_display)) > 0
11491 && (BEG_UNCHANGED < start || GPT <= start))
11492 unchanged_p = 0;
11493
11494 /* If there are overlays at the start or end of the line, these
11495 may have overlay strings with newlines in them. A change at
11496 START, for instance, may actually concern the display of such
11497 overlay strings as well, and they are displayed on different
11498 lines. So, quickly rule out this case. (For the future, it
11499 might be desirable to implement something more telling than
11500 just BEG/END_UNCHANGED.) */
11501 if (unchanged_p)
11502 {
11503 if (BEG + BEG_UNCHANGED == start
11504 && overlay_touches_p (start))
11505 unchanged_p = 0;
11506 if (END_UNCHANGED == end
11507 && overlay_touches_p (Z - end))
11508 unchanged_p = 0;
11509 }
11510
11511 /* Under bidi reordering, adding or deleting a character in the
11512 beginning of a paragraph, before the first strong directional
11513 character, can change the base direction of the paragraph (unless
11514 the buffer specifies a fixed paragraph direction), which will
11515 require to redisplay the whole paragraph. It might be worthwhile
11516 to find the paragraph limits and widen the range of redisplayed
11517 lines to that, but for now just give up this optimization. */
11518 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11519 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11520 unchanged_p = 0;
11521 }
11522
11523 return unchanged_p;
11524 }
11525
11526
11527 /* Do a frame update, taking possible shortcuts into account. This is
11528 the main external entry point for redisplay.
11529
11530 If the last redisplay displayed an echo area message and that message
11531 is no longer requested, we clear the echo area or bring back the
11532 mini-buffer if that is in use. */
11533
11534 void
11535 redisplay (void)
11536 {
11537 redisplay_internal ();
11538 }
11539
11540
11541 static Lisp_Object
11542 overlay_arrow_string_or_property (Lisp_Object var)
11543 {
11544 Lisp_Object val;
11545
11546 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11547 return val;
11548
11549 return Voverlay_arrow_string;
11550 }
11551
11552 /* Return 1 if there are any overlay-arrows in current_buffer. */
11553 static int
11554 overlay_arrow_in_current_buffer_p (void)
11555 {
11556 Lisp_Object vlist;
11557
11558 for (vlist = Voverlay_arrow_variable_list;
11559 CONSP (vlist);
11560 vlist = XCDR (vlist))
11561 {
11562 Lisp_Object var = XCAR (vlist);
11563 Lisp_Object val;
11564
11565 if (!SYMBOLP (var))
11566 continue;
11567 val = find_symbol_value (var);
11568 if (MARKERP (val)
11569 && current_buffer == XMARKER (val)->buffer)
11570 return 1;
11571 }
11572 return 0;
11573 }
11574
11575
11576 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11577 has changed. */
11578
11579 static int
11580 overlay_arrows_changed_p (void)
11581 {
11582 Lisp_Object vlist;
11583
11584 for (vlist = Voverlay_arrow_variable_list;
11585 CONSP (vlist);
11586 vlist = XCDR (vlist))
11587 {
11588 Lisp_Object var = XCAR (vlist);
11589 Lisp_Object val, pstr;
11590
11591 if (!SYMBOLP (var))
11592 continue;
11593 val = find_symbol_value (var);
11594 if (!MARKERP (val))
11595 continue;
11596 if (! EQ (COERCE_MARKER (val),
11597 Fget (var, Qlast_arrow_position))
11598 || ! (pstr = overlay_arrow_string_or_property (var),
11599 EQ (pstr, Fget (var, Qlast_arrow_string))))
11600 return 1;
11601 }
11602 return 0;
11603 }
11604
11605 /* Mark overlay arrows to be updated on next redisplay. */
11606
11607 static void
11608 update_overlay_arrows (int up_to_date)
11609 {
11610 Lisp_Object vlist;
11611
11612 for (vlist = Voverlay_arrow_variable_list;
11613 CONSP (vlist);
11614 vlist = XCDR (vlist))
11615 {
11616 Lisp_Object var = XCAR (vlist);
11617
11618 if (!SYMBOLP (var))
11619 continue;
11620
11621 if (up_to_date > 0)
11622 {
11623 Lisp_Object val = find_symbol_value (var);
11624 Fput (var, Qlast_arrow_position,
11625 COERCE_MARKER (val));
11626 Fput (var, Qlast_arrow_string,
11627 overlay_arrow_string_or_property (var));
11628 }
11629 else if (up_to_date < 0
11630 || !NILP (Fget (var, Qlast_arrow_position)))
11631 {
11632 Fput (var, Qlast_arrow_position, Qt);
11633 Fput (var, Qlast_arrow_string, Qt);
11634 }
11635 }
11636 }
11637
11638
11639 /* Return overlay arrow string to display at row.
11640 Return integer (bitmap number) for arrow bitmap in left fringe.
11641 Return nil if no overlay arrow. */
11642
11643 static Lisp_Object
11644 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11645 {
11646 Lisp_Object vlist;
11647
11648 for (vlist = Voverlay_arrow_variable_list;
11649 CONSP (vlist);
11650 vlist = XCDR (vlist))
11651 {
11652 Lisp_Object var = XCAR (vlist);
11653 Lisp_Object val;
11654
11655 if (!SYMBOLP (var))
11656 continue;
11657
11658 val = find_symbol_value (var);
11659
11660 if (MARKERP (val)
11661 && current_buffer == XMARKER (val)->buffer
11662 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11663 {
11664 if (FRAME_WINDOW_P (it->f)
11665 /* FIXME: if ROW->reversed_p is set, this should test
11666 the right fringe, not the left one. */
11667 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11668 {
11669 #ifdef HAVE_WINDOW_SYSTEM
11670 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11671 {
11672 int fringe_bitmap;
11673 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11674 return make_number (fringe_bitmap);
11675 }
11676 #endif
11677 return make_number (-1); /* Use default arrow bitmap */
11678 }
11679 return overlay_arrow_string_or_property (var);
11680 }
11681 }
11682
11683 return Qnil;
11684 }
11685
11686 /* Return 1 if point moved out of or into a composition. Otherwise
11687 return 0. PREV_BUF and PREV_PT are the last point buffer and
11688 position. BUF and PT are the current point buffer and position. */
11689
11690 static int
11691 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11692 struct buffer *buf, EMACS_INT pt)
11693 {
11694 EMACS_INT start, end;
11695 Lisp_Object prop;
11696 Lisp_Object buffer;
11697
11698 XSETBUFFER (buffer, buf);
11699 /* Check a composition at the last point if point moved within the
11700 same buffer. */
11701 if (prev_buf == buf)
11702 {
11703 if (prev_pt == pt)
11704 /* Point didn't move. */
11705 return 0;
11706
11707 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11708 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11709 && COMPOSITION_VALID_P (start, end, prop)
11710 && start < prev_pt && end > prev_pt)
11711 /* The last point was within the composition. Return 1 iff
11712 point moved out of the composition. */
11713 return (pt <= start || pt >= end);
11714 }
11715
11716 /* Check a composition at the current point. */
11717 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11718 && find_composition (pt, -1, &start, &end, &prop, buffer)
11719 && COMPOSITION_VALID_P (start, end, prop)
11720 && start < pt && end > pt);
11721 }
11722
11723
11724 /* Reconsider the setting of B->clip_changed which is displayed
11725 in window W. */
11726
11727 static INLINE void
11728 reconsider_clip_changes (struct window *w, struct buffer *b)
11729 {
11730 if (b->clip_changed
11731 && !NILP (w->window_end_valid)
11732 && w->current_matrix->buffer == b
11733 && w->current_matrix->zv == BUF_ZV (b)
11734 && w->current_matrix->begv == BUF_BEGV (b))
11735 b->clip_changed = 0;
11736
11737 /* If display wasn't paused, and W is not a tool bar window, see if
11738 point has been moved into or out of a composition. In that case,
11739 we set b->clip_changed to 1 to force updating the screen. If
11740 b->clip_changed has already been set to 1, we can skip this
11741 check. */
11742 if (!b->clip_changed
11743 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11744 {
11745 EMACS_INT pt;
11746
11747 if (w == XWINDOW (selected_window))
11748 pt = PT;
11749 else
11750 pt = marker_position (w->pointm);
11751
11752 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11753 || pt != XINT (w->last_point))
11754 && check_point_in_composition (w->current_matrix->buffer,
11755 XINT (w->last_point),
11756 XBUFFER (w->buffer), pt))
11757 b->clip_changed = 1;
11758 }
11759 }
11760 \f
11761
11762 /* Select FRAME to forward the values of frame-local variables into C
11763 variables so that the redisplay routines can access those values
11764 directly. */
11765
11766 static void
11767 select_frame_for_redisplay (Lisp_Object frame)
11768 {
11769 Lisp_Object tail, tem;
11770 Lisp_Object old = selected_frame;
11771 struct Lisp_Symbol *sym;
11772
11773 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11774
11775 selected_frame = frame;
11776
11777 do {
11778 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11779 if (CONSP (XCAR (tail))
11780 && (tem = XCAR (XCAR (tail)),
11781 SYMBOLP (tem))
11782 && (sym = indirect_variable (XSYMBOL (tem)),
11783 sym->redirect == SYMBOL_LOCALIZED)
11784 && sym->val.blv->frame_local)
11785 /* Use find_symbol_value rather than Fsymbol_value
11786 to avoid an error if it is void. */
11787 find_symbol_value (tem);
11788 } while (!EQ (frame, old) && (frame = old, 1));
11789 }
11790
11791
11792 #define STOP_POLLING \
11793 do { if (! polling_stopped_here) stop_polling (); \
11794 polling_stopped_here = 1; } while (0)
11795
11796 #define RESUME_POLLING \
11797 do { if (polling_stopped_here) start_polling (); \
11798 polling_stopped_here = 0; } while (0)
11799
11800
11801 /* Perhaps in the future avoid recentering windows if it
11802 is not necessary; currently that causes some problems. */
11803
11804 static void
11805 redisplay_internal (void)
11806 {
11807 struct window *w = XWINDOW (selected_window);
11808 struct window *sw;
11809 struct frame *fr;
11810 int pending;
11811 int must_finish = 0;
11812 struct text_pos tlbufpos, tlendpos;
11813 int number_of_visible_frames;
11814 int count, count1;
11815 struct frame *sf;
11816 int polling_stopped_here = 0;
11817 Lisp_Object old_frame = selected_frame;
11818
11819 /* Non-zero means redisplay has to consider all windows on all
11820 frames. Zero means, only selected_window is considered. */
11821 int consider_all_windows_p;
11822
11823 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11824
11825 /* No redisplay if running in batch mode or frame is not yet fully
11826 initialized, or redisplay is explicitly turned off by setting
11827 Vinhibit_redisplay. */
11828 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11829 || !NILP (Vinhibit_redisplay))
11830 return;
11831
11832 /* Don't examine these until after testing Vinhibit_redisplay.
11833 When Emacs is shutting down, perhaps because its connection to
11834 X has dropped, we should not look at them at all. */
11835 fr = XFRAME (w->frame);
11836 sf = SELECTED_FRAME ();
11837
11838 if (!fr->glyphs_initialized_p)
11839 return;
11840
11841 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11842 if (popup_activated ())
11843 return;
11844 #endif
11845
11846 /* I don't think this happens but let's be paranoid. */
11847 if (redisplaying_p)
11848 return;
11849
11850 /* Record a function that resets redisplaying_p to its old value
11851 when we leave this function. */
11852 count = SPECPDL_INDEX ();
11853 record_unwind_protect (unwind_redisplay,
11854 Fcons (make_number (redisplaying_p), selected_frame));
11855 ++redisplaying_p;
11856 specbind (Qinhibit_free_realized_faces, Qnil);
11857
11858 {
11859 Lisp_Object tail, frame;
11860
11861 FOR_EACH_FRAME (tail, frame)
11862 {
11863 struct frame *f = XFRAME (frame);
11864 f->already_hscrolled_p = 0;
11865 }
11866 }
11867
11868 retry:
11869 /* Remember the currently selected window. */
11870 sw = w;
11871
11872 if (!EQ (old_frame, selected_frame)
11873 && FRAME_LIVE_P (XFRAME (old_frame)))
11874 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11875 selected_frame and selected_window to be temporarily out-of-sync so
11876 when we come back here via `goto retry', we need to resync because we
11877 may need to run Elisp code (via prepare_menu_bars). */
11878 select_frame_for_redisplay (old_frame);
11879
11880 pending = 0;
11881 reconsider_clip_changes (w, current_buffer);
11882 last_escape_glyph_frame = NULL;
11883 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11884 last_glyphless_glyph_frame = NULL;
11885 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11886
11887 /* If new fonts have been loaded that make a glyph matrix adjustment
11888 necessary, do it. */
11889 if (fonts_changed_p)
11890 {
11891 adjust_glyphs (NULL);
11892 ++windows_or_buffers_changed;
11893 fonts_changed_p = 0;
11894 }
11895
11896 /* If face_change_count is non-zero, init_iterator will free all
11897 realized faces, which includes the faces referenced from current
11898 matrices. So, we can't reuse current matrices in this case. */
11899 if (face_change_count)
11900 ++windows_or_buffers_changed;
11901
11902 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11903 && FRAME_TTY (sf)->previous_frame != sf)
11904 {
11905 /* Since frames on a single ASCII terminal share the same
11906 display area, displaying a different frame means redisplay
11907 the whole thing. */
11908 windows_or_buffers_changed++;
11909 SET_FRAME_GARBAGED (sf);
11910 #ifndef DOS_NT
11911 set_tty_color_mode (FRAME_TTY (sf), sf);
11912 #endif
11913 FRAME_TTY (sf)->previous_frame = sf;
11914 }
11915
11916 /* Set the visible flags for all frames. Do this before checking
11917 for resized or garbaged frames; they want to know if their frames
11918 are visible. See the comment in frame.h for
11919 FRAME_SAMPLE_VISIBILITY. */
11920 {
11921 Lisp_Object tail, frame;
11922
11923 number_of_visible_frames = 0;
11924
11925 FOR_EACH_FRAME (tail, frame)
11926 {
11927 struct frame *f = XFRAME (frame);
11928
11929 FRAME_SAMPLE_VISIBILITY (f);
11930 if (FRAME_VISIBLE_P (f))
11931 ++number_of_visible_frames;
11932 clear_desired_matrices (f);
11933 }
11934 }
11935
11936 /* Notice any pending interrupt request to change frame size. */
11937 do_pending_window_change (1);
11938
11939 /* do_pending_window_change could change the selected_window due to
11940 frame resizing which makes the selected window too small. */
11941 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
11942 {
11943 sw = w;
11944 reconsider_clip_changes (w, current_buffer);
11945 }
11946
11947 /* Clear frames marked as garbaged. */
11948 if (frame_garbaged)
11949 clear_garbaged_frames ();
11950
11951 /* Build menubar and tool-bar items. */
11952 if (NILP (Vmemory_full))
11953 prepare_menu_bars ();
11954
11955 if (windows_or_buffers_changed)
11956 update_mode_lines++;
11957
11958 /* Detect case that we need to write or remove a star in the mode line. */
11959 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11960 {
11961 w->update_mode_line = Qt;
11962 if (buffer_shared > 1)
11963 update_mode_lines++;
11964 }
11965
11966 /* Avoid invocation of point motion hooks by `current_column' below. */
11967 count1 = SPECPDL_INDEX ();
11968 specbind (Qinhibit_point_motion_hooks, Qt);
11969
11970 /* If %c is in the mode line, update it if needed. */
11971 if (!NILP (w->column_number_displayed)
11972 /* This alternative quickly identifies a common case
11973 where no change is needed. */
11974 && !(PT == XFASTINT (w->last_point)
11975 && XFASTINT (w->last_modified) >= MODIFF
11976 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11977 && (XFASTINT (w->column_number_displayed) != current_column ()))
11978 w->update_mode_line = Qt;
11979
11980 unbind_to (count1, Qnil);
11981
11982 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11983
11984 /* The variable buffer_shared is set in redisplay_window and
11985 indicates that we redisplay a buffer in different windows. See
11986 there. */
11987 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11988 || cursor_type_changed);
11989
11990 /* If specs for an arrow have changed, do thorough redisplay
11991 to ensure we remove any arrow that should no longer exist. */
11992 if (overlay_arrows_changed_p ())
11993 consider_all_windows_p = windows_or_buffers_changed = 1;
11994
11995 /* Normally the message* functions will have already displayed and
11996 updated the echo area, but the frame may have been trashed, or
11997 the update may have been preempted, so display the echo area
11998 again here. Checking message_cleared_p captures the case that
11999 the echo area should be cleared. */
12000 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12001 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12002 || (message_cleared_p
12003 && minibuf_level == 0
12004 /* If the mini-window is currently selected, this means the
12005 echo-area doesn't show through. */
12006 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12007 {
12008 int window_height_changed_p = echo_area_display (0);
12009 must_finish = 1;
12010
12011 /* If we don't display the current message, don't clear the
12012 message_cleared_p flag, because, if we did, we wouldn't clear
12013 the echo area in the next redisplay which doesn't preserve
12014 the echo area. */
12015 if (!display_last_displayed_message_p)
12016 message_cleared_p = 0;
12017
12018 if (fonts_changed_p)
12019 goto retry;
12020 else if (window_height_changed_p)
12021 {
12022 consider_all_windows_p = 1;
12023 ++update_mode_lines;
12024 ++windows_or_buffers_changed;
12025
12026 /* If window configuration was changed, frames may have been
12027 marked garbaged. Clear them or we will experience
12028 surprises wrt scrolling. */
12029 if (frame_garbaged)
12030 clear_garbaged_frames ();
12031 }
12032 }
12033 else if (EQ (selected_window, minibuf_window)
12034 && (current_buffer->clip_changed
12035 || XFASTINT (w->last_modified) < MODIFF
12036 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12037 && resize_mini_window (w, 0))
12038 {
12039 /* Resized active mini-window to fit the size of what it is
12040 showing if its contents might have changed. */
12041 must_finish = 1;
12042 /* FIXME: this causes all frames to be updated, which seems unnecessary
12043 since only the current frame needs to be considered. This function needs
12044 to be rewritten with two variables, consider_all_windows and
12045 consider_all_frames. */
12046 consider_all_windows_p = 1;
12047 ++windows_or_buffers_changed;
12048 ++update_mode_lines;
12049
12050 /* If window configuration was changed, frames may have been
12051 marked garbaged. Clear them or we will experience
12052 surprises wrt scrolling. */
12053 if (frame_garbaged)
12054 clear_garbaged_frames ();
12055 }
12056
12057
12058 /* If showing the region, and mark has changed, we must redisplay
12059 the whole window. The assignment to this_line_start_pos prevents
12060 the optimization directly below this if-statement. */
12061 if (((!NILP (Vtransient_mark_mode)
12062 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12063 != !NILP (w->region_showing))
12064 || (!NILP (w->region_showing)
12065 && !EQ (w->region_showing,
12066 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12067 CHARPOS (this_line_start_pos) = 0;
12068
12069 /* Optimize the case that only the line containing the cursor in the
12070 selected window has changed. Variables starting with this_ are
12071 set in display_line and record information about the line
12072 containing the cursor. */
12073 tlbufpos = this_line_start_pos;
12074 tlendpos = this_line_end_pos;
12075 if (!consider_all_windows_p
12076 && CHARPOS (tlbufpos) > 0
12077 && NILP (w->update_mode_line)
12078 && !current_buffer->clip_changed
12079 && !current_buffer->prevent_redisplay_optimizations_p
12080 && FRAME_VISIBLE_P (XFRAME (w->frame))
12081 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12082 /* Make sure recorded data applies to current buffer, etc. */
12083 && this_line_buffer == current_buffer
12084 && current_buffer == XBUFFER (w->buffer)
12085 && NILP (w->force_start)
12086 && NILP (w->optional_new_start)
12087 /* Point must be on the line that we have info recorded about. */
12088 && PT >= CHARPOS (tlbufpos)
12089 && PT <= Z - CHARPOS (tlendpos)
12090 /* All text outside that line, including its final newline,
12091 must be unchanged. */
12092 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12093 CHARPOS (tlendpos)))
12094 {
12095 if (CHARPOS (tlbufpos) > BEGV
12096 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12097 && (CHARPOS (tlbufpos) == ZV
12098 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12099 /* Former continuation line has disappeared by becoming empty. */
12100 goto cancel;
12101 else if (XFASTINT (w->last_modified) < MODIFF
12102 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12103 || MINI_WINDOW_P (w))
12104 {
12105 /* We have to handle the case of continuation around a
12106 wide-column character (see the comment in indent.c around
12107 line 1340).
12108
12109 For instance, in the following case:
12110
12111 -------- Insert --------
12112 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12113 J_I_ ==> J_I_ `^^' are cursors.
12114 ^^ ^^
12115 -------- --------
12116
12117 As we have to redraw the line above, we cannot use this
12118 optimization. */
12119
12120 struct it it;
12121 int line_height_before = this_line_pixel_height;
12122
12123 /* Note that start_display will handle the case that the
12124 line starting at tlbufpos is a continuation line. */
12125 start_display (&it, w, tlbufpos);
12126
12127 /* Implementation note: It this still necessary? */
12128 if (it.current_x != this_line_start_x)
12129 goto cancel;
12130
12131 TRACE ((stderr, "trying display optimization 1\n"));
12132 w->cursor.vpos = -1;
12133 overlay_arrow_seen = 0;
12134 it.vpos = this_line_vpos;
12135 it.current_y = this_line_y;
12136 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12137 display_line (&it);
12138
12139 /* If line contains point, is not continued,
12140 and ends at same distance from eob as before, we win. */
12141 if (w->cursor.vpos >= 0
12142 /* Line is not continued, otherwise this_line_start_pos
12143 would have been set to 0 in display_line. */
12144 && CHARPOS (this_line_start_pos)
12145 /* Line ends as before. */
12146 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12147 /* Line has same height as before. Otherwise other lines
12148 would have to be shifted up or down. */
12149 && this_line_pixel_height == line_height_before)
12150 {
12151 /* If this is not the window's last line, we must adjust
12152 the charstarts of the lines below. */
12153 if (it.current_y < it.last_visible_y)
12154 {
12155 struct glyph_row *row
12156 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12157 EMACS_INT delta, delta_bytes;
12158
12159 /* We used to distinguish between two cases here,
12160 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12161 when the line ends in a newline or the end of the
12162 buffer's accessible portion. But both cases did
12163 the same, so they were collapsed. */
12164 delta = (Z
12165 - CHARPOS (tlendpos)
12166 - MATRIX_ROW_START_CHARPOS (row));
12167 delta_bytes = (Z_BYTE
12168 - BYTEPOS (tlendpos)
12169 - MATRIX_ROW_START_BYTEPOS (row));
12170
12171 increment_matrix_positions (w->current_matrix,
12172 this_line_vpos + 1,
12173 w->current_matrix->nrows,
12174 delta, delta_bytes);
12175 }
12176
12177 /* If this row displays text now but previously didn't,
12178 or vice versa, w->window_end_vpos may have to be
12179 adjusted. */
12180 if ((it.glyph_row - 1)->displays_text_p)
12181 {
12182 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12183 XSETINT (w->window_end_vpos, this_line_vpos);
12184 }
12185 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12186 && this_line_vpos > 0)
12187 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12188 w->window_end_valid = Qnil;
12189
12190 /* Update hint: No need to try to scroll in update_window. */
12191 w->desired_matrix->no_scrolling_p = 1;
12192
12193 #if GLYPH_DEBUG
12194 *w->desired_matrix->method = 0;
12195 debug_method_add (w, "optimization 1");
12196 #endif
12197 #ifdef HAVE_WINDOW_SYSTEM
12198 update_window_fringes (w, 0);
12199 #endif
12200 goto update;
12201 }
12202 else
12203 goto cancel;
12204 }
12205 else if (/* Cursor position hasn't changed. */
12206 PT == XFASTINT (w->last_point)
12207 /* Make sure the cursor was last displayed
12208 in this window. Otherwise we have to reposition it. */
12209 && 0 <= w->cursor.vpos
12210 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12211 {
12212 if (!must_finish)
12213 {
12214 do_pending_window_change (1);
12215 /* If selected_window changed, redisplay again. */
12216 if (WINDOWP (selected_window)
12217 && (w = XWINDOW (selected_window)) != sw)
12218 goto retry;
12219
12220 /* We used to always goto end_of_redisplay here, but this
12221 isn't enough if we have a blinking cursor. */
12222 if (w->cursor_off_p == w->last_cursor_off_p)
12223 goto end_of_redisplay;
12224 }
12225 goto update;
12226 }
12227 /* If highlighting the region, or if the cursor is in the echo area,
12228 then we can't just move the cursor. */
12229 else if (! (!NILP (Vtransient_mark_mode)
12230 && !NILP (BVAR (current_buffer, mark_active)))
12231 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12232 || highlight_nonselected_windows)
12233 && NILP (w->region_showing)
12234 && NILP (Vshow_trailing_whitespace)
12235 && !cursor_in_echo_area)
12236 {
12237 struct it it;
12238 struct glyph_row *row;
12239
12240 /* Skip from tlbufpos to PT and see where it is. Note that
12241 PT may be in invisible text. If so, we will end at the
12242 next visible position. */
12243 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12244 NULL, DEFAULT_FACE_ID);
12245 it.current_x = this_line_start_x;
12246 it.current_y = this_line_y;
12247 it.vpos = this_line_vpos;
12248
12249 /* The call to move_it_to stops in front of PT, but
12250 moves over before-strings. */
12251 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12252
12253 if (it.vpos == this_line_vpos
12254 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12255 row->enabled_p))
12256 {
12257 xassert (this_line_vpos == it.vpos);
12258 xassert (this_line_y == it.current_y);
12259 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12260 #if GLYPH_DEBUG
12261 *w->desired_matrix->method = 0;
12262 debug_method_add (w, "optimization 3");
12263 #endif
12264 goto update;
12265 }
12266 else
12267 goto cancel;
12268 }
12269
12270 cancel:
12271 /* Text changed drastically or point moved off of line. */
12272 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12273 }
12274
12275 CHARPOS (this_line_start_pos) = 0;
12276 consider_all_windows_p |= buffer_shared > 1;
12277 ++clear_face_cache_count;
12278 #ifdef HAVE_WINDOW_SYSTEM
12279 ++clear_image_cache_count;
12280 #endif
12281
12282 /* Build desired matrices, and update the display. If
12283 consider_all_windows_p is non-zero, do it for all windows on all
12284 frames. Otherwise do it for selected_window, only. */
12285
12286 if (consider_all_windows_p)
12287 {
12288 Lisp_Object tail, frame;
12289
12290 FOR_EACH_FRAME (tail, frame)
12291 XFRAME (frame)->updated_p = 0;
12292
12293 /* Recompute # windows showing selected buffer. This will be
12294 incremented each time such a window is displayed. */
12295 buffer_shared = 0;
12296
12297 FOR_EACH_FRAME (tail, frame)
12298 {
12299 struct frame *f = XFRAME (frame);
12300
12301 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12302 {
12303 if (! EQ (frame, selected_frame))
12304 /* Select the frame, for the sake of frame-local
12305 variables. */
12306 select_frame_for_redisplay (frame);
12307
12308 /* Mark all the scroll bars to be removed; we'll redeem
12309 the ones we want when we redisplay their windows. */
12310 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12311 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12312
12313 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12314 redisplay_windows (FRAME_ROOT_WINDOW (f));
12315
12316 /* The X error handler may have deleted that frame. */
12317 if (!FRAME_LIVE_P (f))
12318 continue;
12319
12320 /* Any scroll bars which redisplay_windows should have
12321 nuked should now go away. */
12322 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12323 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12324
12325 /* If fonts changed, display again. */
12326 /* ??? rms: I suspect it is a mistake to jump all the way
12327 back to retry here. It should just retry this frame. */
12328 if (fonts_changed_p)
12329 goto retry;
12330
12331 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12332 {
12333 /* See if we have to hscroll. */
12334 if (!f->already_hscrolled_p)
12335 {
12336 f->already_hscrolled_p = 1;
12337 if (hscroll_windows (f->root_window))
12338 goto retry;
12339 }
12340
12341 /* Prevent various kinds of signals during display
12342 update. stdio is not robust about handling
12343 signals, which can cause an apparent I/O
12344 error. */
12345 if (interrupt_input)
12346 unrequest_sigio ();
12347 STOP_POLLING;
12348
12349 /* Update the display. */
12350 set_window_update_flags (XWINDOW (f->root_window), 1);
12351 pending |= update_frame (f, 0, 0);
12352 f->updated_p = 1;
12353 }
12354 }
12355 }
12356
12357 if (!EQ (old_frame, selected_frame)
12358 && FRAME_LIVE_P (XFRAME (old_frame)))
12359 /* We played a bit fast-and-loose above and allowed selected_frame
12360 and selected_window to be temporarily out-of-sync but let's make
12361 sure this stays contained. */
12362 select_frame_for_redisplay (old_frame);
12363 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12364
12365 if (!pending)
12366 {
12367 /* Do the mark_window_display_accurate after all windows have
12368 been redisplayed because this call resets flags in buffers
12369 which are needed for proper redisplay. */
12370 FOR_EACH_FRAME (tail, frame)
12371 {
12372 struct frame *f = XFRAME (frame);
12373 if (f->updated_p)
12374 {
12375 mark_window_display_accurate (f->root_window, 1);
12376 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12377 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12378 }
12379 }
12380 }
12381 }
12382 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12383 {
12384 Lisp_Object mini_window;
12385 struct frame *mini_frame;
12386
12387 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12388 /* Use list_of_error, not Qerror, so that
12389 we catch only errors and don't run the debugger. */
12390 internal_condition_case_1 (redisplay_window_1, selected_window,
12391 list_of_error,
12392 redisplay_window_error);
12393
12394 /* Compare desired and current matrices, perform output. */
12395
12396 update:
12397 /* If fonts changed, display again. */
12398 if (fonts_changed_p)
12399 goto retry;
12400
12401 /* Prevent various kinds of signals during display update.
12402 stdio is not robust about handling signals,
12403 which can cause an apparent I/O error. */
12404 if (interrupt_input)
12405 unrequest_sigio ();
12406 STOP_POLLING;
12407
12408 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12409 {
12410 if (hscroll_windows (selected_window))
12411 goto retry;
12412
12413 XWINDOW (selected_window)->must_be_updated_p = 1;
12414 pending = update_frame (sf, 0, 0);
12415 }
12416
12417 /* We may have called echo_area_display at the top of this
12418 function. If the echo area is on another frame, that may
12419 have put text on a frame other than the selected one, so the
12420 above call to update_frame would not have caught it. Catch
12421 it here. */
12422 mini_window = FRAME_MINIBUF_WINDOW (sf);
12423 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12424
12425 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12426 {
12427 XWINDOW (mini_window)->must_be_updated_p = 1;
12428 pending |= update_frame (mini_frame, 0, 0);
12429 if (!pending && hscroll_windows (mini_window))
12430 goto retry;
12431 }
12432 }
12433
12434 /* If display was paused because of pending input, make sure we do a
12435 thorough update the next time. */
12436 if (pending)
12437 {
12438 /* Prevent the optimization at the beginning of
12439 redisplay_internal that tries a single-line update of the
12440 line containing the cursor in the selected window. */
12441 CHARPOS (this_line_start_pos) = 0;
12442
12443 /* Let the overlay arrow be updated the next time. */
12444 update_overlay_arrows (0);
12445
12446 /* If we pause after scrolling, some rows in the current
12447 matrices of some windows are not valid. */
12448 if (!WINDOW_FULL_WIDTH_P (w)
12449 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12450 update_mode_lines = 1;
12451 }
12452 else
12453 {
12454 if (!consider_all_windows_p)
12455 {
12456 /* This has already been done above if
12457 consider_all_windows_p is set. */
12458 mark_window_display_accurate_1 (w, 1);
12459
12460 /* Say overlay arrows are up to date. */
12461 update_overlay_arrows (1);
12462
12463 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12464 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12465 }
12466
12467 update_mode_lines = 0;
12468 windows_or_buffers_changed = 0;
12469 cursor_type_changed = 0;
12470 }
12471
12472 /* Start SIGIO interrupts coming again. Having them off during the
12473 code above makes it less likely one will discard output, but not
12474 impossible, since there might be stuff in the system buffer here.
12475 But it is much hairier to try to do anything about that. */
12476 if (interrupt_input)
12477 request_sigio ();
12478 RESUME_POLLING;
12479
12480 /* If a frame has become visible which was not before, redisplay
12481 again, so that we display it. Expose events for such a frame
12482 (which it gets when becoming visible) don't call the parts of
12483 redisplay constructing glyphs, so simply exposing a frame won't
12484 display anything in this case. So, we have to display these
12485 frames here explicitly. */
12486 if (!pending)
12487 {
12488 Lisp_Object tail, frame;
12489 int new_count = 0;
12490
12491 FOR_EACH_FRAME (tail, frame)
12492 {
12493 int this_is_visible = 0;
12494
12495 if (XFRAME (frame)->visible)
12496 this_is_visible = 1;
12497 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12498 if (XFRAME (frame)->visible)
12499 this_is_visible = 1;
12500
12501 if (this_is_visible)
12502 new_count++;
12503 }
12504
12505 if (new_count != number_of_visible_frames)
12506 windows_or_buffers_changed++;
12507 }
12508
12509 /* Change frame size now if a change is pending. */
12510 do_pending_window_change (1);
12511
12512 /* If we just did a pending size change, or have additional
12513 visible frames, or selected_window changed, redisplay again. */
12514 if ((windows_or_buffers_changed && !pending)
12515 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12516 goto retry;
12517
12518 /* Clear the face and image caches.
12519
12520 We used to do this only if consider_all_windows_p. But the cache
12521 needs to be cleared if a timer creates images in the current
12522 buffer (e.g. the test case in Bug#6230). */
12523
12524 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12525 {
12526 clear_face_cache (0);
12527 clear_face_cache_count = 0;
12528 }
12529
12530 #ifdef HAVE_WINDOW_SYSTEM
12531 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12532 {
12533 clear_image_caches (Qnil);
12534 clear_image_cache_count = 0;
12535 }
12536 #endif /* HAVE_WINDOW_SYSTEM */
12537
12538 end_of_redisplay:
12539 unbind_to (count, Qnil);
12540 RESUME_POLLING;
12541 }
12542
12543
12544 /* Redisplay, but leave alone any recent echo area message unless
12545 another message has been requested in its place.
12546
12547 This is useful in situations where you need to redisplay but no
12548 user action has occurred, making it inappropriate for the message
12549 area to be cleared. See tracking_off and
12550 wait_reading_process_output for examples of these situations.
12551
12552 FROM_WHERE is an integer saying from where this function was
12553 called. This is useful for debugging. */
12554
12555 void
12556 redisplay_preserve_echo_area (int from_where)
12557 {
12558 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12559
12560 if (!NILP (echo_area_buffer[1]))
12561 {
12562 /* We have a previously displayed message, but no current
12563 message. Redisplay the previous message. */
12564 display_last_displayed_message_p = 1;
12565 redisplay_internal ();
12566 display_last_displayed_message_p = 0;
12567 }
12568 else
12569 redisplay_internal ();
12570
12571 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12572 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12573 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12574 }
12575
12576
12577 /* Function registered with record_unwind_protect in
12578 redisplay_internal. Reset redisplaying_p to the value it had
12579 before redisplay_internal was called, and clear
12580 prevent_freeing_realized_faces_p. It also selects the previously
12581 selected frame, unless it has been deleted (by an X connection
12582 failure during redisplay, for example). */
12583
12584 static Lisp_Object
12585 unwind_redisplay (Lisp_Object val)
12586 {
12587 Lisp_Object old_redisplaying_p, old_frame;
12588
12589 old_redisplaying_p = XCAR (val);
12590 redisplaying_p = XFASTINT (old_redisplaying_p);
12591 old_frame = XCDR (val);
12592 if (! EQ (old_frame, selected_frame)
12593 && FRAME_LIVE_P (XFRAME (old_frame)))
12594 select_frame_for_redisplay (old_frame);
12595 return Qnil;
12596 }
12597
12598
12599 /* Mark the display of window W as accurate or inaccurate. If
12600 ACCURATE_P is non-zero mark display of W as accurate. If
12601 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12602 redisplay_internal is called. */
12603
12604 static void
12605 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12606 {
12607 if (BUFFERP (w->buffer))
12608 {
12609 struct buffer *b = XBUFFER (w->buffer);
12610
12611 w->last_modified
12612 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12613 w->last_overlay_modified
12614 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12615 w->last_had_star
12616 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12617
12618 if (accurate_p)
12619 {
12620 b->clip_changed = 0;
12621 b->prevent_redisplay_optimizations_p = 0;
12622
12623 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12624 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12625 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12626 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12627
12628 w->current_matrix->buffer = b;
12629 w->current_matrix->begv = BUF_BEGV (b);
12630 w->current_matrix->zv = BUF_ZV (b);
12631
12632 w->last_cursor = w->cursor;
12633 w->last_cursor_off_p = w->cursor_off_p;
12634
12635 if (w == XWINDOW (selected_window))
12636 w->last_point = make_number (BUF_PT (b));
12637 else
12638 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12639 }
12640 }
12641
12642 if (accurate_p)
12643 {
12644 w->window_end_valid = w->buffer;
12645 w->update_mode_line = Qnil;
12646 }
12647 }
12648
12649
12650 /* Mark the display of windows in the window tree rooted at WINDOW as
12651 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12652 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12653 be redisplayed the next time redisplay_internal is called. */
12654
12655 void
12656 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12657 {
12658 struct window *w;
12659
12660 for (; !NILP (window); window = w->next)
12661 {
12662 w = XWINDOW (window);
12663 mark_window_display_accurate_1 (w, accurate_p);
12664
12665 if (!NILP (w->vchild))
12666 mark_window_display_accurate (w->vchild, accurate_p);
12667 if (!NILP (w->hchild))
12668 mark_window_display_accurate (w->hchild, accurate_p);
12669 }
12670
12671 if (accurate_p)
12672 {
12673 update_overlay_arrows (1);
12674 }
12675 else
12676 {
12677 /* Force a thorough redisplay the next time by setting
12678 last_arrow_position and last_arrow_string to t, which is
12679 unequal to any useful value of Voverlay_arrow_... */
12680 update_overlay_arrows (-1);
12681 }
12682 }
12683
12684
12685 /* Return value in display table DP (Lisp_Char_Table *) for character
12686 C. Since a display table doesn't have any parent, we don't have to
12687 follow parent. Do not call this function directly but use the
12688 macro DISP_CHAR_VECTOR. */
12689
12690 Lisp_Object
12691 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12692 {
12693 Lisp_Object val;
12694
12695 if (ASCII_CHAR_P (c))
12696 {
12697 val = dp->ascii;
12698 if (SUB_CHAR_TABLE_P (val))
12699 val = XSUB_CHAR_TABLE (val)->contents[c];
12700 }
12701 else
12702 {
12703 Lisp_Object table;
12704
12705 XSETCHAR_TABLE (table, dp);
12706 val = char_table_ref (table, c);
12707 }
12708 if (NILP (val))
12709 val = dp->defalt;
12710 return val;
12711 }
12712
12713
12714 \f
12715 /***********************************************************************
12716 Window Redisplay
12717 ***********************************************************************/
12718
12719 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12720
12721 static void
12722 redisplay_windows (Lisp_Object window)
12723 {
12724 while (!NILP (window))
12725 {
12726 struct window *w = XWINDOW (window);
12727
12728 if (!NILP (w->hchild))
12729 redisplay_windows (w->hchild);
12730 else if (!NILP (w->vchild))
12731 redisplay_windows (w->vchild);
12732 else if (!NILP (w->buffer))
12733 {
12734 displayed_buffer = XBUFFER (w->buffer);
12735 /* Use list_of_error, not Qerror, so that
12736 we catch only errors and don't run the debugger. */
12737 internal_condition_case_1 (redisplay_window_0, window,
12738 list_of_error,
12739 redisplay_window_error);
12740 }
12741
12742 window = w->next;
12743 }
12744 }
12745
12746 static Lisp_Object
12747 redisplay_window_error (Lisp_Object ignore)
12748 {
12749 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12750 return Qnil;
12751 }
12752
12753 static Lisp_Object
12754 redisplay_window_0 (Lisp_Object window)
12755 {
12756 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12757 redisplay_window (window, 0);
12758 return Qnil;
12759 }
12760
12761 static Lisp_Object
12762 redisplay_window_1 (Lisp_Object window)
12763 {
12764 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12765 redisplay_window (window, 1);
12766 return Qnil;
12767 }
12768 \f
12769
12770 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12771 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12772 which positions recorded in ROW differ from current buffer
12773 positions.
12774
12775 Return 0 if cursor is not on this row, 1 otherwise. */
12776
12777 static int
12778 set_cursor_from_row (struct window *w, struct glyph_row *row,
12779 struct glyph_matrix *matrix,
12780 EMACS_INT delta, EMACS_INT delta_bytes,
12781 int dy, int dvpos)
12782 {
12783 struct glyph *glyph = row->glyphs[TEXT_AREA];
12784 struct glyph *end = glyph + row->used[TEXT_AREA];
12785 struct glyph *cursor = NULL;
12786 /* The last known character position in row. */
12787 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12788 int x = row->x;
12789 EMACS_INT pt_old = PT - delta;
12790 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12791 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12792 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12793 /* A glyph beyond the edge of TEXT_AREA which we should never
12794 touch. */
12795 struct glyph *glyphs_end = end;
12796 /* Non-zero means we've found a match for cursor position, but that
12797 glyph has the avoid_cursor_p flag set. */
12798 int match_with_avoid_cursor = 0;
12799 /* Non-zero means we've seen at least one glyph that came from a
12800 display string. */
12801 int string_seen = 0;
12802 /* Largest and smalles buffer positions seen so far during scan of
12803 glyph row. */
12804 EMACS_INT bpos_max = pos_before;
12805 EMACS_INT bpos_min = pos_after;
12806 /* Last buffer position covered by an overlay string with an integer
12807 `cursor' property. */
12808 EMACS_INT bpos_covered = 0;
12809
12810 /* Skip over glyphs not having an object at the start and the end of
12811 the row. These are special glyphs like truncation marks on
12812 terminal frames. */
12813 if (row->displays_text_p)
12814 {
12815 if (!row->reversed_p)
12816 {
12817 while (glyph < end
12818 && INTEGERP (glyph->object)
12819 && glyph->charpos < 0)
12820 {
12821 x += glyph->pixel_width;
12822 ++glyph;
12823 }
12824 while (end > glyph
12825 && INTEGERP ((end - 1)->object)
12826 /* CHARPOS is zero for blanks and stretch glyphs
12827 inserted by extend_face_to_end_of_line. */
12828 && (end - 1)->charpos <= 0)
12829 --end;
12830 glyph_before = glyph - 1;
12831 glyph_after = end;
12832 }
12833 else
12834 {
12835 struct glyph *g;
12836
12837 /* If the glyph row is reversed, we need to process it from back
12838 to front, so swap the edge pointers. */
12839 glyphs_end = end = glyph - 1;
12840 glyph += row->used[TEXT_AREA] - 1;
12841
12842 while (glyph > end + 1
12843 && INTEGERP (glyph->object)
12844 && glyph->charpos < 0)
12845 {
12846 --glyph;
12847 x -= glyph->pixel_width;
12848 }
12849 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12850 --glyph;
12851 /* By default, in reversed rows we put the cursor on the
12852 rightmost (first in the reading order) glyph. */
12853 for (g = end + 1; g < glyph; g++)
12854 x += g->pixel_width;
12855 while (end < glyph
12856 && INTEGERP ((end + 1)->object)
12857 && (end + 1)->charpos <= 0)
12858 ++end;
12859 glyph_before = glyph + 1;
12860 glyph_after = end;
12861 }
12862 }
12863 else if (row->reversed_p)
12864 {
12865 /* In R2L rows that don't display text, put the cursor on the
12866 rightmost glyph. Case in point: an empty last line that is
12867 part of an R2L paragraph. */
12868 cursor = end - 1;
12869 /* Avoid placing the cursor on the last glyph of the row, where
12870 on terminal frames we hold the vertical border between
12871 adjacent windows. */
12872 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12873 && !WINDOW_RIGHTMOST_P (w)
12874 && cursor == row->glyphs[LAST_AREA] - 1)
12875 cursor--;
12876 x = -1; /* will be computed below, at label compute_x */
12877 }
12878
12879 /* Step 1: Try to find the glyph whose character position
12880 corresponds to point. If that's not possible, find 2 glyphs
12881 whose character positions are the closest to point, one before
12882 point, the other after it. */
12883 if (!row->reversed_p)
12884 while (/* not marched to end of glyph row */
12885 glyph < end
12886 /* glyph was not inserted by redisplay for internal purposes */
12887 && !INTEGERP (glyph->object))
12888 {
12889 if (BUFFERP (glyph->object))
12890 {
12891 EMACS_INT dpos = glyph->charpos - pt_old;
12892
12893 if (glyph->charpos > bpos_max)
12894 bpos_max = glyph->charpos;
12895 if (glyph->charpos < bpos_min)
12896 bpos_min = glyph->charpos;
12897 if (!glyph->avoid_cursor_p)
12898 {
12899 /* If we hit point, we've found the glyph on which to
12900 display the cursor. */
12901 if (dpos == 0)
12902 {
12903 match_with_avoid_cursor = 0;
12904 break;
12905 }
12906 /* See if we've found a better approximation to
12907 POS_BEFORE or to POS_AFTER. Note that we want the
12908 first (leftmost) glyph of all those that are the
12909 closest from below, and the last (rightmost) of all
12910 those from above. */
12911 if (0 > dpos && dpos > pos_before - pt_old)
12912 {
12913 pos_before = glyph->charpos;
12914 glyph_before = glyph;
12915 }
12916 else if (0 < dpos && dpos <= pos_after - pt_old)
12917 {
12918 pos_after = glyph->charpos;
12919 glyph_after = glyph;
12920 }
12921 }
12922 else if (dpos == 0)
12923 match_with_avoid_cursor = 1;
12924 }
12925 else if (STRINGP (glyph->object))
12926 {
12927 Lisp_Object chprop;
12928 EMACS_INT glyph_pos = glyph->charpos;
12929
12930 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12931 glyph->object);
12932 if (INTEGERP (chprop))
12933 {
12934 bpos_covered = bpos_max + XINT (chprop);
12935 /* If the `cursor' property covers buffer positions up
12936 to and including point, we should display cursor on
12937 this glyph. Note that overlays and text properties
12938 with string values stop bidi reordering, so every
12939 buffer position to the left of the string is always
12940 smaller than any position to the right of the
12941 string. Therefore, if a `cursor' property on one
12942 of the string's characters has an integer value, we
12943 will break out of the loop below _before_ we get to
12944 the position match above. IOW, integer values of
12945 the `cursor' property override the "exact match for
12946 point" strategy of positioning the cursor. */
12947 /* Implementation note: bpos_max == pt_old when, e.g.,
12948 we are in an empty line, where bpos_max is set to
12949 MATRIX_ROW_START_CHARPOS, see above. */
12950 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12951 {
12952 cursor = glyph;
12953 break;
12954 }
12955 }
12956
12957 string_seen = 1;
12958 }
12959 x += glyph->pixel_width;
12960 ++glyph;
12961 }
12962 else if (glyph > end) /* row is reversed */
12963 while (!INTEGERP (glyph->object))
12964 {
12965 if (BUFFERP (glyph->object))
12966 {
12967 EMACS_INT dpos = glyph->charpos - pt_old;
12968
12969 if (glyph->charpos > bpos_max)
12970 bpos_max = glyph->charpos;
12971 if (glyph->charpos < bpos_min)
12972 bpos_min = glyph->charpos;
12973 if (!glyph->avoid_cursor_p)
12974 {
12975 if (dpos == 0)
12976 {
12977 match_with_avoid_cursor = 0;
12978 break;
12979 }
12980 if (0 > dpos && dpos > pos_before - pt_old)
12981 {
12982 pos_before = glyph->charpos;
12983 glyph_before = glyph;
12984 }
12985 else if (0 < dpos && dpos <= pos_after - pt_old)
12986 {
12987 pos_after = glyph->charpos;
12988 glyph_after = glyph;
12989 }
12990 }
12991 else if (dpos == 0)
12992 match_with_avoid_cursor = 1;
12993 }
12994 else if (STRINGP (glyph->object))
12995 {
12996 Lisp_Object chprop;
12997 EMACS_INT glyph_pos = glyph->charpos;
12998
12999 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13000 glyph->object);
13001 if (INTEGERP (chprop))
13002 {
13003 bpos_covered = bpos_max + XINT (chprop);
13004 /* If the `cursor' property covers buffer positions up
13005 to and including point, we should display cursor on
13006 this glyph. */
13007 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13008 {
13009 cursor = glyph;
13010 break;
13011 }
13012 }
13013 string_seen = 1;
13014 }
13015 --glyph;
13016 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13017 {
13018 x--; /* can't use any pixel_width */
13019 break;
13020 }
13021 x -= glyph->pixel_width;
13022 }
13023
13024 /* Step 2: If we didn't find an exact match for point, we need to
13025 look for a proper place to put the cursor among glyphs between
13026 GLYPH_BEFORE and GLYPH_AFTER. */
13027 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13028 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13029 && bpos_covered < pt_old)
13030 {
13031 /* An empty line has a single glyph whose OBJECT is zero and
13032 whose CHARPOS is the position of a newline on that line.
13033 Note that on a TTY, there are more glyphs after that, which
13034 were produced by extend_face_to_end_of_line, but their
13035 CHARPOS is zero or negative. */
13036 int empty_line_p =
13037 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13038 && INTEGERP (glyph->object) && glyph->charpos > 0;
13039
13040 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13041 {
13042 EMACS_INT ellipsis_pos;
13043
13044 /* Scan back over the ellipsis glyphs. */
13045 if (!row->reversed_p)
13046 {
13047 ellipsis_pos = (glyph - 1)->charpos;
13048 while (glyph > row->glyphs[TEXT_AREA]
13049 && (glyph - 1)->charpos == ellipsis_pos)
13050 glyph--, x -= glyph->pixel_width;
13051 /* That loop always goes one position too far, including
13052 the glyph before the ellipsis. So scan forward over
13053 that one. */
13054 x += glyph->pixel_width;
13055 glyph++;
13056 }
13057 else /* row is reversed */
13058 {
13059 ellipsis_pos = (glyph + 1)->charpos;
13060 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13061 && (glyph + 1)->charpos == ellipsis_pos)
13062 glyph++, x += glyph->pixel_width;
13063 x -= glyph->pixel_width;
13064 glyph--;
13065 }
13066 }
13067 else if (match_with_avoid_cursor
13068 /* A truncated row may not include PT among its
13069 character positions. Setting the cursor inside the
13070 scroll margin will trigger recalculation of hscroll
13071 in hscroll_window_tree. */
13072 || (row->truncated_on_left_p && pt_old < bpos_min)
13073 || (row->truncated_on_right_p && pt_old > bpos_max)
13074 /* Zero-width characters produce no glyphs. */
13075 || (!string_seen
13076 && !empty_line_p
13077 && (row->reversed_p
13078 ? glyph_after > glyphs_end
13079 : glyph_after < glyphs_end)))
13080 {
13081 cursor = glyph_after;
13082 x = -1;
13083 }
13084 else if (string_seen)
13085 {
13086 int incr = row->reversed_p ? -1 : +1;
13087
13088 /* Need to find the glyph that came out of a string which is
13089 present at point. That glyph is somewhere between
13090 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13091 positioned between POS_BEFORE and POS_AFTER in the
13092 buffer. */
13093 struct glyph *start, *stop;
13094 EMACS_INT pos = pos_before;
13095
13096 x = -1;
13097
13098 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13099 correspond to POS_BEFORE and POS_AFTER, respectively. We
13100 need START and STOP in the order that corresponds to the
13101 row's direction as given by its reversed_p flag. If the
13102 directionality of characters between POS_BEFORE and
13103 POS_AFTER is the opposite of the row's base direction,
13104 these characters will have been reordered for display,
13105 and we need to reverse START and STOP. */
13106 if (!row->reversed_p)
13107 {
13108 start = min (glyph_before, glyph_after);
13109 stop = max (glyph_before, glyph_after);
13110 }
13111 else
13112 {
13113 start = max (glyph_before, glyph_after);
13114 stop = min (glyph_before, glyph_after);
13115 }
13116 for (glyph = start + incr;
13117 row->reversed_p ? glyph > stop : glyph < stop; )
13118 {
13119
13120 /* Any glyphs that come from the buffer are here because
13121 of bidi reordering. Skip them, and only pay
13122 attention to glyphs that came from some string. */
13123 if (STRINGP (glyph->object))
13124 {
13125 Lisp_Object str;
13126 EMACS_INT tem;
13127
13128 str = glyph->object;
13129 tem = string_buffer_position_lim (str, pos, pos_after, 0);
13130 if (tem == 0 /* from overlay */
13131 || pos <= tem)
13132 {
13133 /* If the string from which this glyph came is
13134 found in the buffer at point, then we've
13135 found the glyph we've been looking for. If
13136 it comes from an overlay (tem == 0), and it
13137 has the `cursor' property on one of its
13138 glyphs, record that glyph as a candidate for
13139 displaying the cursor. (As in the
13140 unidirectional version, we will display the
13141 cursor on the last candidate we find.) */
13142 if (tem == 0 || tem == pt_old)
13143 {
13144 /* The glyphs from this string could have
13145 been reordered. Find the one with the
13146 smallest string position. Or there could
13147 be a character in the string with the
13148 `cursor' property, which means display
13149 cursor on that character's glyph. */
13150 EMACS_INT strpos = glyph->charpos;
13151
13152 if (tem)
13153 cursor = glyph;
13154 for ( ;
13155 (row->reversed_p ? glyph > stop : glyph < stop)
13156 && EQ (glyph->object, str);
13157 glyph += incr)
13158 {
13159 Lisp_Object cprop;
13160 EMACS_INT gpos = glyph->charpos;
13161
13162 cprop = Fget_char_property (make_number (gpos),
13163 Qcursor,
13164 glyph->object);
13165 if (!NILP (cprop))
13166 {
13167 cursor = glyph;
13168 break;
13169 }
13170 if (tem && glyph->charpos < strpos)
13171 {
13172 strpos = glyph->charpos;
13173 cursor = glyph;
13174 }
13175 }
13176
13177 if (tem == pt_old)
13178 goto compute_x;
13179 }
13180 if (tem)
13181 pos = tem + 1; /* don't find previous instances */
13182 }
13183 /* This string is not what we want; skip all of the
13184 glyphs that came from it. */
13185 while ((row->reversed_p ? glyph > stop : glyph < stop)
13186 && EQ (glyph->object, str))
13187 glyph += incr;
13188 }
13189 else
13190 glyph += incr;
13191 }
13192
13193 /* If we reached the end of the line, and END was from a string,
13194 the cursor is not on this line. */
13195 if (cursor == NULL
13196 && (row->reversed_p ? glyph <= end : glyph >= end)
13197 && STRINGP (end->object)
13198 && row->continued_p)
13199 return 0;
13200 }
13201 }
13202
13203 compute_x:
13204 if (cursor != NULL)
13205 glyph = cursor;
13206 if (x < 0)
13207 {
13208 struct glyph *g;
13209
13210 /* Need to compute x that corresponds to GLYPH. */
13211 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13212 {
13213 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13214 abort ();
13215 x += g->pixel_width;
13216 }
13217 }
13218
13219 /* ROW could be part of a continued line, which, under bidi
13220 reordering, might have other rows whose start and end charpos
13221 occlude point. Only set w->cursor if we found a better
13222 approximation to the cursor position than we have from previously
13223 examined candidate rows belonging to the same continued line. */
13224 if (/* we already have a candidate row */
13225 w->cursor.vpos >= 0
13226 /* that candidate is not the row we are processing */
13227 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13228 /* the row we are processing is part of a continued line */
13229 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13230 /* Make sure cursor.vpos specifies a row whose start and end
13231 charpos occlude point. This is because some callers of this
13232 function leave cursor.vpos at the row where the cursor was
13233 displayed during the last redisplay cycle. */
13234 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13235 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13236 {
13237 struct glyph *g1 =
13238 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13239
13240 /* Don't consider glyphs that are outside TEXT_AREA. */
13241 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13242 return 0;
13243 /* Keep the candidate whose buffer position is the closest to
13244 point. */
13245 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13246 w->cursor.hpos >= 0
13247 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13248 && BUFFERP (g1->object)
13249 && (g1->charpos == pt_old /* an exact match always wins */
13250 || (BUFFERP (glyph->object)
13251 && eabs (g1->charpos - pt_old)
13252 < eabs (glyph->charpos - pt_old))))
13253 return 0;
13254 /* If this candidate gives an exact match, use that. */
13255 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13256 /* Otherwise, keep the candidate that comes from a row
13257 spanning less buffer positions. This may win when one or
13258 both candidate positions are on glyphs that came from
13259 display strings, for which we cannot compare buffer
13260 positions. */
13261 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13262 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13263 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13264 return 0;
13265 }
13266 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13267 w->cursor.x = x;
13268 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13269 w->cursor.y = row->y + dy;
13270
13271 if (w == XWINDOW (selected_window))
13272 {
13273 if (!row->continued_p
13274 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13275 && row->x == 0)
13276 {
13277 this_line_buffer = XBUFFER (w->buffer);
13278
13279 CHARPOS (this_line_start_pos)
13280 = MATRIX_ROW_START_CHARPOS (row) + delta;
13281 BYTEPOS (this_line_start_pos)
13282 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13283
13284 CHARPOS (this_line_end_pos)
13285 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13286 BYTEPOS (this_line_end_pos)
13287 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13288
13289 this_line_y = w->cursor.y;
13290 this_line_pixel_height = row->height;
13291 this_line_vpos = w->cursor.vpos;
13292 this_line_start_x = row->x;
13293 }
13294 else
13295 CHARPOS (this_line_start_pos) = 0;
13296 }
13297
13298 return 1;
13299 }
13300
13301
13302 /* Run window scroll functions, if any, for WINDOW with new window
13303 start STARTP. Sets the window start of WINDOW to that position.
13304
13305 We assume that the window's buffer is really current. */
13306
13307 static INLINE struct text_pos
13308 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13309 {
13310 struct window *w = XWINDOW (window);
13311 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13312
13313 if (current_buffer != XBUFFER (w->buffer))
13314 abort ();
13315
13316 if (!NILP (Vwindow_scroll_functions))
13317 {
13318 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13319 make_number (CHARPOS (startp)));
13320 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13321 /* In case the hook functions switch buffers. */
13322 if (current_buffer != XBUFFER (w->buffer))
13323 set_buffer_internal_1 (XBUFFER (w->buffer));
13324 }
13325
13326 return startp;
13327 }
13328
13329
13330 /* Make sure the line containing the cursor is fully visible.
13331 A value of 1 means there is nothing to be done.
13332 (Either the line is fully visible, or it cannot be made so,
13333 or we cannot tell.)
13334
13335 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13336 is higher than window.
13337
13338 A value of 0 means the caller should do scrolling
13339 as if point had gone off the screen. */
13340
13341 static int
13342 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13343 {
13344 struct glyph_matrix *matrix;
13345 struct glyph_row *row;
13346 int window_height;
13347
13348 if (!make_cursor_line_fully_visible_p)
13349 return 1;
13350
13351 /* It's not always possible to find the cursor, e.g, when a window
13352 is full of overlay strings. Don't do anything in that case. */
13353 if (w->cursor.vpos < 0)
13354 return 1;
13355
13356 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13357 row = MATRIX_ROW (matrix, w->cursor.vpos);
13358
13359 /* If the cursor row is not partially visible, there's nothing to do. */
13360 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13361 return 1;
13362
13363 /* If the row the cursor is in is taller than the window's height,
13364 it's not clear what to do, so do nothing. */
13365 window_height = window_box_height (w);
13366 if (row->height >= window_height)
13367 {
13368 if (!force_p || MINI_WINDOW_P (w)
13369 || w->vscroll || w->cursor.vpos == 0)
13370 return 1;
13371 }
13372 return 0;
13373 }
13374
13375
13376 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13377 non-zero means only WINDOW is redisplayed in redisplay_internal.
13378 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13379 in redisplay_window to bring a partially visible line into view in
13380 the case that only the cursor has moved.
13381
13382 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13383 last screen line's vertical height extends past the end of the screen.
13384
13385 Value is
13386
13387 1 if scrolling succeeded
13388
13389 0 if scrolling didn't find point.
13390
13391 -1 if new fonts have been loaded so that we must interrupt
13392 redisplay, adjust glyph matrices, and try again. */
13393
13394 enum
13395 {
13396 SCROLLING_SUCCESS,
13397 SCROLLING_FAILED,
13398 SCROLLING_NEED_LARGER_MATRICES
13399 };
13400
13401 /* If scroll-conservatively is more than this, never recenter.
13402
13403 If you change this, don't forget to update the doc string of
13404 `scroll-conservatively' and the Emacs manual. */
13405 #define SCROLL_LIMIT 100
13406
13407 static int
13408 try_scrolling (Lisp_Object window, int just_this_one_p,
13409 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13410 int temp_scroll_step, int last_line_misfit)
13411 {
13412 struct window *w = XWINDOW (window);
13413 struct frame *f = XFRAME (w->frame);
13414 struct text_pos pos, startp;
13415 struct it it;
13416 int this_scroll_margin, scroll_max, rc, height;
13417 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13418 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13419 Lisp_Object aggressive;
13420 /* We will never try scrolling more than this number of lines. */
13421 int scroll_limit = SCROLL_LIMIT;
13422
13423 #if GLYPH_DEBUG
13424 debug_method_add (w, "try_scrolling");
13425 #endif
13426
13427 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13428
13429 /* Compute scroll margin height in pixels. We scroll when point is
13430 within this distance from the top or bottom of the window. */
13431 if (scroll_margin > 0)
13432 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13433 * FRAME_LINE_HEIGHT (f);
13434 else
13435 this_scroll_margin = 0;
13436
13437 /* Force arg_scroll_conservatively to have a reasonable value, to
13438 avoid scrolling too far away with slow move_it_* functions. Note
13439 that the user can supply scroll-conservatively equal to
13440 `most-positive-fixnum', which can be larger than INT_MAX. */
13441 if (arg_scroll_conservatively > scroll_limit)
13442 {
13443 arg_scroll_conservatively = scroll_limit + 1;
13444 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13445 }
13446 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13447 /* Compute how much we should try to scroll maximally to bring
13448 point into view. */
13449 scroll_max = (max (scroll_step,
13450 max (arg_scroll_conservatively, temp_scroll_step))
13451 * FRAME_LINE_HEIGHT (f));
13452 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13453 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13454 /* We're trying to scroll because of aggressive scrolling but no
13455 scroll_step is set. Choose an arbitrary one. */
13456 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13457 else
13458 scroll_max = 0;
13459
13460 too_near_end:
13461
13462 /* Decide whether to scroll down. */
13463 if (PT > CHARPOS (startp))
13464 {
13465 int scroll_margin_y;
13466
13467 /* Compute the pixel ypos of the scroll margin, then move it to
13468 either that ypos or PT, whichever comes first. */
13469 start_display (&it, w, startp);
13470 scroll_margin_y = it.last_visible_y - this_scroll_margin
13471 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13472 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13473 (MOVE_TO_POS | MOVE_TO_Y));
13474
13475 if (PT > CHARPOS (it.current.pos))
13476 {
13477 int y0 = line_bottom_y (&it);
13478 /* Compute how many pixels below window bottom to stop searching
13479 for PT. This avoids costly search for PT that is far away if
13480 the user limited scrolling by a small number of lines, but
13481 always finds PT if scroll_conservatively is set to a large
13482 number, such as most-positive-fixnum. */
13483 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13484 int y_to_move = it.last_visible_y + slack;
13485
13486 /* Compute the distance from the scroll margin to PT or to
13487 the scroll limit, whichever comes first. This should
13488 include the height of the cursor line, to make that line
13489 fully visible. */
13490 move_it_to (&it, PT, -1, y_to_move,
13491 -1, MOVE_TO_POS | MOVE_TO_Y);
13492 dy = line_bottom_y (&it) - y0;
13493
13494 if (dy > scroll_max)
13495 return SCROLLING_FAILED;
13496
13497 scroll_down_p = 1;
13498 }
13499 }
13500
13501 if (scroll_down_p)
13502 {
13503 /* Point is in or below the bottom scroll margin, so move the
13504 window start down. If scrolling conservatively, move it just
13505 enough down to make point visible. If scroll_step is set,
13506 move it down by scroll_step. */
13507 if (arg_scroll_conservatively)
13508 amount_to_scroll
13509 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13510 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13511 else if (scroll_step || temp_scroll_step)
13512 amount_to_scroll = scroll_max;
13513 else
13514 {
13515 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13516 height = WINDOW_BOX_TEXT_HEIGHT (w);
13517 if (NUMBERP (aggressive))
13518 {
13519 double float_amount = XFLOATINT (aggressive) * height;
13520 amount_to_scroll = float_amount;
13521 if (amount_to_scroll == 0 && float_amount > 0)
13522 amount_to_scroll = 1;
13523 /* Don't let point enter the scroll margin near top of
13524 the window. */
13525 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13526 amount_to_scroll = height - 2*this_scroll_margin + dy;
13527 }
13528 }
13529
13530 if (amount_to_scroll <= 0)
13531 return SCROLLING_FAILED;
13532
13533 start_display (&it, w, startp);
13534 if (arg_scroll_conservatively <= scroll_limit)
13535 move_it_vertically (&it, amount_to_scroll);
13536 else
13537 {
13538 /* Extra precision for users who set scroll-conservatively
13539 to a large number: make sure the amount we scroll
13540 the window start is never less than amount_to_scroll,
13541 which was computed as distance from window bottom to
13542 point. This matters when lines at window top and lines
13543 below window bottom have different height. */
13544 struct it it1 = it;
13545 /* We use a temporary it1 because line_bottom_y can modify
13546 its argument, if it moves one line down; see there. */
13547 int start_y = line_bottom_y (&it1);
13548
13549 do {
13550 move_it_by_lines (&it, 1);
13551 it1 = it;
13552 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13553 }
13554
13555 /* If STARTP is unchanged, move it down another screen line. */
13556 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13557 move_it_by_lines (&it, 1);
13558 startp = it.current.pos;
13559 }
13560 else
13561 {
13562 struct text_pos scroll_margin_pos = startp;
13563
13564 /* See if point is inside the scroll margin at the top of the
13565 window. */
13566 if (this_scroll_margin)
13567 {
13568 start_display (&it, w, startp);
13569 move_it_vertically (&it, this_scroll_margin);
13570 scroll_margin_pos = it.current.pos;
13571 }
13572
13573 if (PT < CHARPOS (scroll_margin_pos))
13574 {
13575 /* Point is in the scroll margin at the top of the window or
13576 above what is displayed in the window. */
13577 int y0, y_to_move;
13578
13579 /* Compute the vertical distance from PT to the scroll
13580 margin position. Move as far as scroll_max allows, or
13581 one screenful, or 10 screen lines, whichever is largest.
13582 Give up if distance is greater than scroll_max. */
13583 SET_TEXT_POS (pos, PT, PT_BYTE);
13584 start_display (&it, w, pos);
13585 y0 = it.current_y;
13586 y_to_move = max (it.last_visible_y,
13587 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13588 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13589 y_to_move, -1,
13590 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13591 dy = it.current_y - y0;
13592 if (dy > scroll_max)
13593 return SCROLLING_FAILED;
13594
13595 /* Compute new window start. */
13596 start_display (&it, w, startp);
13597
13598 if (arg_scroll_conservatively)
13599 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13600 max (scroll_step, temp_scroll_step));
13601 else if (scroll_step || temp_scroll_step)
13602 amount_to_scroll = scroll_max;
13603 else
13604 {
13605 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13606 height = WINDOW_BOX_TEXT_HEIGHT (w);
13607 if (NUMBERP (aggressive))
13608 {
13609 double float_amount = XFLOATINT (aggressive) * height;
13610 amount_to_scroll = float_amount;
13611 if (amount_to_scroll == 0 && float_amount > 0)
13612 amount_to_scroll = 1;
13613 amount_to_scroll -=
13614 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13615 /* Don't let point enter the scroll margin near
13616 bottom of the window. */
13617 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13618 amount_to_scroll = height - 2*this_scroll_margin + dy;
13619 }
13620 }
13621
13622 if (amount_to_scroll <= 0)
13623 return SCROLLING_FAILED;
13624
13625 move_it_vertically_backward (&it, amount_to_scroll);
13626 startp = it.current.pos;
13627 }
13628 }
13629
13630 /* Run window scroll functions. */
13631 startp = run_window_scroll_functions (window, startp);
13632
13633 /* Display the window. Give up if new fonts are loaded, or if point
13634 doesn't appear. */
13635 if (!try_window (window, startp, 0))
13636 rc = SCROLLING_NEED_LARGER_MATRICES;
13637 else if (w->cursor.vpos < 0)
13638 {
13639 clear_glyph_matrix (w->desired_matrix);
13640 rc = SCROLLING_FAILED;
13641 }
13642 else
13643 {
13644 /* Maybe forget recorded base line for line number display. */
13645 if (!just_this_one_p
13646 || current_buffer->clip_changed
13647 || BEG_UNCHANGED < CHARPOS (startp))
13648 w->base_line_number = Qnil;
13649
13650 /* If cursor ends up on a partially visible line,
13651 treat that as being off the bottom of the screen. */
13652 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13653 /* It's possible that the cursor is on the first line of the
13654 buffer, which is partially obscured due to a vscroll
13655 (Bug#7537). In that case, avoid looping forever . */
13656 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13657 {
13658 clear_glyph_matrix (w->desired_matrix);
13659 ++extra_scroll_margin_lines;
13660 goto too_near_end;
13661 }
13662 rc = SCROLLING_SUCCESS;
13663 }
13664
13665 return rc;
13666 }
13667
13668
13669 /* Compute a suitable window start for window W if display of W starts
13670 on a continuation line. Value is non-zero if a new window start
13671 was computed.
13672
13673 The new window start will be computed, based on W's width, starting
13674 from the start of the continued line. It is the start of the
13675 screen line with the minimum distance from the old start W->start. */
13676
13677 static int
13678 compute_window_start_on_continuation_line (struct window *w)
13679 {
13680 struct text_pos pos, start_pos;
13681 int window_start_changed_p = 0;
13682
13683 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13684
13685 /* If window start is on a continuation line... Window start may be
13686 < BEGV in case there's invisible text at the start of the
13687 buffer (M-x rmail, for example). */
13688 if (CHARPOS (start_pos) > BEGV
13689 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13690 {
13691 struct it it;
13692 struct glyph_row *row;
13693
13694 /* Handle the case that the window start is out of range. */
13695 if (CHARPOS (start_pos) < BEGV)
13696 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13697 else if (CHARPOS (start_pos) > ZV)
13698 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13699
13700 /* Find the start of the continued line. This should be fast
13701 because scan_buffer is fast (newline cache). */
13702 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13703 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13704 row, DEFAULT_FACE_ID);
13705 reseat_at_previous_visible_line_start (&it);
13706
13707 /* If the line start is "too far" away from the window start,
13708 say it takes too much time to compute a new window start. */
13709 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13710 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13711 {
13712 int min_distance, distance;
13713
13714 /* Move forward by display lines to find the new window
13715 start. If window width was enlarged, the new start can
13716 be expected to be > the old start. If window width was
13717 decreased, the new window start will be < the old start.
13718 So, we're looking for the display line start with the
13719 minimum distance from the old window start. */
13720 pos = it.current.pos;
13721 min_distance = INFINITY;
13722 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13723 distance < min_distance)
13724 {
13725 min_distance = distance;
13726 pos = it.current.pos;
13727 move_it_by_lines (&it, 1);
13728 }
13729
13730 /* Set the window start there. */
13731 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13732 window_start_changed_p = 1;
13733 }
13734 }
13735
13736 return window_start_changed_p;
13737 }
13738
13739
13740 /* Try cursor movement in case text has not changed in window WINDOW,
13741 with window start STARTP. Value is
13742
13743 CURSOR_MOVEMENT_SUCCESS if successful
13744
13745 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13746
13747 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13748 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13749 we want to scroll as if scroll-step were set to 1. See the code.
13750
13751 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13752 which case we have to abort this redisplay, and adjust matrices
13753 first. */
13754
13755 enum
13756 {
13757 CURSOR_MOVEMENT_SUCCESS,
13758 CURSOR_MOVEMENT_CANNOT_BE_USED,
13759 CURSOR_MOVEMENT_MUST_SCROLL,
13760 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13761 };
13762
13763 static int
13764 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13765 {
13766 struct window *w = XWINDOW (window);
13767 struct frame *f = XFRAME (w->frame);
13768 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13769
13770 #if GLYPH_DEBUG
13771 if (inhibit_try_cursor_movement)
13772 return rc;
13773 #endif
13774
13775 /* Handle case where text has not changed, only point, and it has
13776 not moved off the frame. */
13777 if (/* Point may be in this window. */
13778 PT >= CHARPOS (startp)
13779 /* Selective display hasn't changed. */
13780 && !current_buffer->clip_changed
13781 /* Function force-mode-line-update is used to force a thorough
13782 redisplay. It sets either windows_or_buffers_changed or
13783 update_mode_lines. So don't take a shortcut here for these
13784 cases. */
13785 && !update_mode_lines
13786 && !windows_or_buffers_changed
13787 && !cursor_type_changed
13788 /* Can't use this case if highlighting a region. When a
13789 region exists, cursor movement has to do more than just
13790 set the cursor. */
13791 && !(!NILP (Vtransient_mark_mode)
13792 && !NILP (BVAR (current_buffer, mark_active)))
13793 && NILP (w->region_showing)
13794 && NILP (Vshow_trailing_whitespace)
13795 /* Right after splitting windows, last_point may be nil. */
13796 && INTEGERP (w->last_point)
13797 /* This code is not used for mini-buffer for the sake of the case
13798 of redisplaying to replace an echo area message; since in
13799 that case the mini-buffer contents per se are usually
13800 unchanged. This code is of no real use in the mini-buffer
13801 since the handling of this_line_start_pos, etc., in redisplay
13802 handles the same cases. */
13803 && !EQ (window, minibuf_window)
13804 /* When splitting windows or for new windows, it happens that
13805 redisplay is called with a nil window_end_vpos or one being
13806 larger than the window. This should really be fixed in
13807 window.c. I don't have this on my list, now, so we do
13808 approximately the same as the old redisplay code. --gerd. */
13809 && INTEGERP (w->window_end_vpos)
13810 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13811 && (FRAME_WINDOW_P (f)
13812 || !overlay_arrow_in_current_buffer_p ()))
13813 {
13814 int this_scroll_margin, top_scroll_margin;
13815 struct glyph_row *row = NULL;
13816
13817 #if GLYPH_DEBUG
13818 debug_method_add (w, "cursor movement");
13819 #endif
13820
13821 /* Scroll if point within this distance from the top or bottom
13822 of the window. This is a pixel value. */
13823 if (scroll_margin > 0)
13824 {
13825 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13826 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13827 }
13828 else
13829 this_scroll_margin = 0;
13830
13831 top_scroll_margin = this_scroll_margin;
13832 if (WINDOW_WANTS_HEADER_LINE_P (w))
13833 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13834
13835 /* Start with the row the cursor was displayed during the last
13836 not paused redisplay. Give up if that row is not valid. */
13837 if (w->last_cursor.vpos < 0
13838 || w->last_cursor.vpos >= w->current_matrix->nrows)
13839 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13840 else
13841 {
13842 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13843 if (row->mode_line_p)
13844 ++row;
13845 if (!row->enabled_p)
13846 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13847 }
13848
13849 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13850 {
13851 int scroll_p = 0, must_scroll = 0;
13852 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13853
13854 if (PT > XFASTINT (w->last_point))
13855 {
13856 /* Point has moved forward. */
13857 while (MATRIX_ROW_END_CHARPOS (row) < PT
13858 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13859 {
13860 xassert (row->enabled_p);
13861 ++row;
13862 }
13863
13864 /* If the end position of a row equals the start
13865 position of the next row, and PT is at that position,
13866 we would rather display cursor in the next line. */
13867 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13868 && MATRIX_ROW_END_CHARPOS (row) == PT
13869 && row < w->current_matrix->rows
13870 + w->current_matrix->nrows - 1
13871 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13872 && !cursor_row_p (row))
13873 ++row;
13874
13875 /* If within the scroll margin, scroll. Note that
13876 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13877 the next line would be drawn, and that
13878 this_scroll_margin can be zero. */
13879 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13880 || PT > MATRIX_ROW_END_CHARPOS (row)
13881 /* Line is completely visible last line in window
13882 and PT is to be set in the next line. */
13883 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13884 && PT == MATRIX_ROW_END_CHARPOS (row)
13885 && !row->ends_at_zv_p
13886 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13887 scroll_p = 1;
13888 }
13889 else if (PT < XFASTINT (w->last_point))
13890 {
13891 /* Cursor has to be moved backward. Note that PT >=
13892 CHARPOS (startp) because of the outer if-statement. */
13893 while (!row->mode_line_p
13894 && (MATRIX_ROW_START_CHARPOS (row) > PT
13895 || (MATRIX_ROW_START_CHARPOS (row) == PT
13896 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13897 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13898 row > w->current_matrix->rows
13899 && (row-1)->ends_in_newline_from_string_p))))
13900 && (row->y > top_scroll_margin
13901 || CHARPOS (startp) == BEGV))
13902 {
13903 xassert (row->enabled_p);
13904 --row;
13905 }
13906
13907 /* Consider the following case: Window starts at BEGV,
13908 there is invisible, intangible text at BEGV, so that
13909 display starts at some point START > BEGV. It can
13910 happen that we are called with PT somewhere between
13911 BEGV and START. Try to handle that case. */
13912 if (row < w->current_matrix->rows
13913 || row->mode_line_p)
13914 {
13915 row = w->current_matrix->rows;
13916 if (row->mode_line_p)
13917 ++row;
13918 }
13919
13920 /* Due to newlines in overlay strings, we may have to
13921 skip forward over overlay strings. */
13922 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13923 && MATRIX_ROW_END_CHARPOS (row) == PT
13924 && !cursor_row_p (row))
13925 ++row;
13926
13927 /* If within the scroll margin, scroll. */
13928 if (row->y < top_scroll_margin
13929 && CHARPOS (startp) != BEGV)
13930 scroll_p = 1;
13931 }
13932 else
13933 {
13934 /* Cursor did not move. So don't scroll even if cursor line
13935 is partially visible, as it was so before. */
13936 rc = CURSOR_MOVEMENT_SUCCESS;
13937 }
13938
13939 if (PT < MATRIX_ROW_START_CHARPOS (row)
13940 || PT > MATRIX_ROW_END_CHARPOS (row))
13941 {
13942 /* if PT is not in the glyph row, give up. */
13943 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13944 must_scroll = 1;
13945 }
13946 else if (rc != CURSOR_MOVEMENT_SUCCESS
13947 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
13948 {
13949 /* If rows are bidi-reordered and point moved, back up
13950 until we find a row that does not belong to a
13951 continuation line. This is because we must consider
13952 all rows of a continued line as candidates for the
13953 new cursor positioning, since row start and end
13954 positions change non-linearly with vertical position
13955 in such rows. */
13956 /* FIXME: Revisit this when glyph ``spilling'' in
13957 continuation lines' rows is implemented for
13958 bidi-reordered rows. */
13959 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13960 {
13961 xassert (row->enabled_p);
13962 --row;
13963 /* If we hit the beginning of the displayed portion
13964 without finding the first row of a continued
13965 line, give up. */
13966 if (row <= w->current_matrix->rows)
13967 {
13968 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13969 break;
13970 }
13971
13972 }
13973 }
13974 if (must_scroll)
13975 ;
13976 else if (rc != CURSOR_MOVEMENT_SUCCESS
13977 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13978 && make_cursor_line_fully_visible_p)
13979 {
13980 if (PT == MATRIX_ROW_END_CHARPOS (row)
13981 && !row->ends_at_zv_p
13982 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13983 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13984 else if (row->height > window_box_height (w))
13985 {
13986 /* If we end up in a partially visible line, let's
13987 make it fully visible, except when it's taller
13988 than the window, in which case we can't do much
13989 about it. */
13990 *scroll_step = 1;
13991 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13992 }
13993 else
13994 {
13995 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13996 if (!cursor_row_fully_visible_p (w, 0, 1))
13997 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13998 else
13999 rc = CURSOR_MOVEMENT_SUCCESS;
14000 }
14001 }
14002 else if (scroll_p)
14003 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14004 else if (rc != CURSOR_MOVEMENT_SUCCESS
14005 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14006 {
14007 /* With bidi-reordered rows, there could be more than
14008 one candidate row whose start and end positions
14009 occlude point. We need to let set_cursor_from_row
14010 find the best candidate. */
14011 /* FIXME: Revisit this when glyph ``spilling'' in
14012 continuation lines' rows is implemented for
14013 bidi-reordered rows. */
14014 int rv = 0;
14015
14016 do
14017 {
14018 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14019 && PT <= MATRIX_ROW_END_CHARPOS (row)
14020 && cursor_row_p (row))
14021 rv |= set_cursor_from_row (w, row, w->current_matrix,
14022 0, 0, 0, 0);
14023 /* As soon as we've found the first suitable row
14024 whose ends_at_zv_p flag is set, we are done. */
14025 if (rv
14026 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14027 {
14028 rc = CURSOR_MOVEMENT_SUCCESS;
14029 break;
14030 }
14031 ++row;
14032 }
14033 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14034 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14035 || (MATRIX_ROW_START_CHARPOS (row) == PT
14036 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14037 /* If we didn't find any candidate rows, or exited the
14038 loop before all the candidates were examined, signal
14039 to the caller that this method failed. */
14040 if (rc != CURSOR_MOVEMENT_SUCCESS
14041 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14042 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14043 else if (rv)
14044 rc = CURSOR_MOVEMENT_SUCCESS;
14045 }
14046 else
14047 {
14048 do
14049 {
14050 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14051 {
14052 rc = CURSOR_MOVEMENT_SUCCESS;
14053 break;
14054 }
14055 ++row;
14056 }
14057 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14058 && MATRIX_ROW_START_CHARPOS (row) == PT
14059 && cursor_row_p (row));
14060 }
14061 }
14062 }
14063
14064 return rc;
14065 }
14066
14067 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14068 static
14069 #endif
14070 void
14071 set_vertical_scroll_bar (struct window *w)
14072 {
14073 EMACS_INT start, end, whole;
14074
14075 /* Calculate the start and end positions for the current window.
14076 At some point, it would be nice to choose between scrollbars
14077 which reflect the whole buffer size, with special markers
14078 indicating narrowing, and scrollbars which reflect only the
14079 visible region.
14080
14081 Note that mini-buffers sometimes aren't displaying any text. */
14082 if (!MINI_WINDOW_P (w)
14083 || (w == XWINDOW (minibuf_window)
14084 && NILP (echo_area_buffer[0])))
14085 {
14086 struct buffer *buf = XBUFFER (w->buffer);
14087 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14088 start = marker_position (w->start) - BUF_BEGV (buf);
14089 /* I don't think this is guaranteed to be right. For the
14090 moment, we'll pretend it is. */
14091 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14092
14093 if (end < start)
14094 end = start;
14095 if (whole < (end - start))
14096 whole = end - start;
14097 }
14098 else
14099 start = end = whole = 0;
14100
14101 /* Indicate what this scroll bar ought to be displaying now. */
14102 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14103 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14104 (w, end - start, whole, start);
14105 }
14106
14107
14108 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14109 selected_window is redisplayed.
14110
14111 We can return without actually redisplaying the window if
14112 fonts_changed_p is nonzero. In that case, redisplay_internal will
14113 retry. */
14114
14115 static void
14116 redisplay_window (Lisp_Object window, int just_this_one_p)
14117 {
14118 struct window *w = XWINDOW (window);
14119 struct frame *f = XFRAME (w->frame);
14120 struct buffer *buffer = XBUFFER (w->buffer);
14121 struct buffer *old = current_buffer;
14122 struct text_pos lpoint, opoint, startp;
14123 int update_mode_line;
14124 int tem;
14125 struct it it;
14126 /* Record it now because it's overwritten. */
14127 int current_matrix_up_to_date_p = 0;
14128 int used_current_matrix_p = 0;
14129 /* This is less strict than current_matrix_up_to_date_p.
14130 It indictes that the buffer contents and narrowing are unchanged. */
14131 int buffer_unchanged_p = 0;
14132 int temp_scroll_step = 0;
14133 int count = SPECPDL_INDEX ();
14134 int rc;
14135 int centering_position = -1;
14136 int last_line_misfit = 0;
14137 EMACS_INT beg_unchanged, end_unchanged;
14138
14139 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14140 opoint = lpoint;
14141
14142 /* W must be a leaf window here. */
14143 xassert (!NILP (w->buffer));
14144 #if GLYPH_DEBUG
14145 *w->desired_matrix->method = 0;
14146 #endif
14147
14148 restart:
14149 reconsider_clip_changes (w, buffer);
14150
14151 /* Has the mode line to be updated? */
14152 update_mode_line = (!NILP (w->update_mode_line)
14153 || update_mode_lines
14154 || buffer->clip_changed
14155 || buffer->prevent_redisplay_optimizations_p);
14156
14157 if (MINI_WINDOW_P (w))
14158 {
14159 if (w == XWINDOW (echo_area_window)
14160 && !NILP (echo_area_buffer[0]))
14161 {
14162 if (update_mode_line)
14163 /* We may have to update a tty frame's menu bar or a
14164 tool-bar. Example `M-x C-h C-h C-g'. */
14165 goto finish_menu_bars;
14166 else
14167 /* We've already displayed the echo area glyphs in this window. */
14168 goto finish_scroll_bars;
14169 }
14170 else if ((w != XWINDOW (minibuf_window)
14171 || minibuf_level == 0)
14172 /* When buffer is nonempty, redisplay window normally. */
14173 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14174 /* Quail displays non-mini buffers in minibuffer window.
14175 In that case, redisplay the window normally. */
14176 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14177 {
14178 /* W is a mini-buffer window, but it's not active, so clear
14179 it. */
14180 int yb = window_text_bottom_y (w);
14181 struct glyph_row *row;
14182 int y;
14183
14184 for (y = 0, row = w->desired_matrix->rows;
14185 y < yb;
14186 y += row->height, ++row)
14187 blank_row (w, row, y);
14188 goto finish_scroll_bars;
14189 }
14190
14191 clear_glyph_matrix (w->desired_matrix);
14192 }
14193
14194 /* Otherwise set up data on this window; select its buffer and point
14195 value. */
14196 /* Really select the buffer, for the sake of buffer-local
14197 variables. */
14198 set_buffer_internal_1 (XBUFFER (w->buffer));
14199
14200 current_matrix_up_to_date_p
14201 = (!NILP (w->window_end_valid)
14202 && !current_buffer->clip_changed
14203 && !current_buffer->prevent_redisplay_optimizations_p
14204 && XFASTINT (w->last_modified) >= MODIFF
14205 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14206
14207 /* Run the window-bottom-change-functions
14208 if it is possible that the text on the screen has changed
14209 (either due to modification of the text, or any other reason). */
14210 if (!current_matrix_up_to_date_p
14211 && !NILP (Vwindow_text_change_functions))
14212 {
14213 safe_run_hooks (Qwindow_text_change_functions);
14214 goto restart;
14215 }
14216
14217 beg_unchanged = BEG_UNCHANGED;
14218 end_unchanged = END_UNCHANGED;
14219
14220 SET_TEXT_POS (opoint, PT, PT_BYTE);
14221
14222 specbind (Qinhibit_point_motion_hooks, Qt);
14223
14224 buffer_unchanged_p
14225 = (!NILP (w->window_end_valid)
14226 && !current_buffer->clip_changed
14227 && XFASTINT (w->last_modified) >= MODIFF
14228 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14229
14230 /* When windows_or_buffers_changed is non-zero, we can't rely on
14231 the window end being valid, so set it to nil there. */
14232 if (windows_or_buffers_changed)
14233 {
14234 /* If window starts on a continuation line, maybe adjust the
14235 window start in case the window's width changed. */
14236 if (XMARKER (w->start)->buffer == current_buffer)
14237 compute_window_start_on_continuation_line (w);
14238
14239 w->window_end_valid = Qnil;
14240 }
14241
14242 /* Some sanity checks. */
14243 CHECK_WINDOW_END (w);
14244 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14245 abort ();
14246 if (BYTEPOS (opoint) < CHARPOS (opoint))
14247 abort ();
14248
14249 /* If %c is in mode line, update it if needed. */
14250 if (!NILP (w->column_number_displayed)
14251 /* This alternative quickly identifies a common case
14252 where no change is needed. */
14253 && !(PT == XFASTINT (w->last_point)
14254 && XFASTINT (w->last_modified) >= MODIFF
14255 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14256 && (XFASTINT (w->column_number_displayed) != current_column ()))
14257 update_mode_line = 1;
14258
14259 /* Count number of windows showing the selected buffer. An indirect
14260 buffer counts as its base buffer. */
14261 if (!just_this_one_p)
14262 {
14263 struct buffer *current_base, *window_base;
14264 current_base = current_buffer;
14265 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14266 if (current_base->base_buffer)
14267 current_base = current_base->base_buffer;
14268 if (window_base->base_buffer)
14269 window_base = window_base->base_buffer;
14270 if (current_base == window_base)
14271 buffer_shared++;
14272 }
14273
14274 /* Point refers normally to the selected window. For any other
14275 window, set up appropriate value. */
14276 if (!EQ (window, selected_window))
14277 {
14278 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14279 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14280 if (new_pt < BEGV)
14281 {
14282 new_pt = BEGV;
14283 new_pt_byte = BEGV_BYTE;
14284 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14285 }
14286 else if (new_pt > (ZV - 1))
14287 {
14288 new_pt = ZV;
14289 new_pt_byte = ZV_BYTE;
14290 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14291 }
14292
14293 /* We don't use SET_PT so that the point-motion hooks don't run. */
14294 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14295 }
14296
14297 /* If any of the character widths specified in the display table
14298 have changed, invalidate the width run cache. It's true that
14299 this may be a bit late to catch such changes, but the rest of
14300 redisplay goes (non-fatally) haywire when the display table is
14301 changed, so why should we worry about doing any better? */
14302 if (current_buffer->width_run_cache)
14303 {
14304 struct Lisp_Char_Table *disptab = buffer_display_table ();
14305
14306 if (! disptab_matches_widthtab (disptab,
14307 XVECTOR (BVAR (current_buffer, width_table))))
14308 {
14309 invalidate_region_cache (current_buffer,
14310 current_buffer->width_run_cache,
14311 BEG, Z);
14312 recompute_width_table (current_buffer, disptab);
14313 }
14314 }
14315
14316 /* If window-start is screwed up, choose a new one. */
14317 if (XMARKER (w->start)->buffer != current_buffer)
14318 goto recenter;
14319
14320 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14321
14322 /* If someone specified a new starting point but did not insist,
14323 check whether it can be used. */
14324 if (!NILP (w->optional_new_start)
14325 && CHARPOS (startp) >= BEGV
14326 && CHARPOS (startp) <= ZV)
14327 {
14328 w->optional_new_start = Qnil;
14329 start_display (&it, w, startp);
14330 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14331 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14332 if (IT_CHARPOS (it) == PT)
14333 w->force_start = Qt;
14334 /* IT may overshoot PT if text at PT is invisible. */
14335 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14336 w->force_start = Qt;
14337 }
14338
14339 force_start:
14340
14341 /* Handle case where place to start displaying has been specified,
14342 unless the specified location is outside the accessible range. */
14343 if (!NILP (w->force_start)
14344 || w->frozen_window_start_p)
14345 {
14346 /* We set this later on if we have to adjust point. */
14347 int new_vpos = -1;
14348
14349 w->force_start = Qnil;
14350 w->vscroll = 0;
14351 w->window_end_valid = Qnil;
14352
14353 /* Forget any recorded base line for line number display. */
14354 if (!buffer_unchanged_p)
14355 w->base_line_number = Qnil;
14356
14357 /* Redisplay the mode line. Select the buffer properly for that.
14358 Also, run the hook window-scroll-functions
14359 because we have scrolled. */
14360 /* Note, we do this after clearing force_start because
14361 if there's an error, it is better to forget about force_start
14362 than to get into an infinite loop calling the hook functions
14363 and having them get more errors. */
14364 if (!update_mode_line
14365 || ! NILP (Vwindow_scroll_functions))
14366 {
14367 update_mode_line = 1;
14368 w->update_mode_line = Qt;
14369 startp = run_window_scroll_functions (window, startp);
14370 }
14371
14372 w->last_modified = make_number (0);
14373 w->last_overlay_modified = make_number (0);
14374 if (CHARPOS (startp) < BEGV)
14375 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14376 else if (CHARPOS (startp) > ZV)
14377 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14378
14379 /* Redisplay, then check if cursor has been set during the
14380 redisplay. Give up if new fonts were loaded. */
14381 /* We used to issue a CHECK_MARGINS argument to try_window here,
14382 but this causes scrolling to fail when point begins inside
14383 the scroll margin (bug#148) -- cyd */
14384 if (!try_window (window, startp, 0))
14385 {
14386 w->force_start = Qt;
14387 clear_glyph_matrix (w->desired_matrix);
14388 goto need_larger_matrices;
14389 }
14390
14391 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14392 {
14393 /* If point does not appear, try to move point so it does
14394 appear. The desired matrix has been built above, so we
14395 can use it here. */
14396 new_vpos = window_box_height (w) / 2;
14397 }
14398
14399 if (!cursor_row_fully_visible_p (w, 0, 0))
14400 {
14401 /* Point does appear, but on a line partly visible at end of window.
14402 Move it back to a fully-visible line. */
14403 new_vpos = window_box_height (w);
14404 }
14405
14406 /* If we need to move point for either of the above reasons,
14407 now actually do it. */
14408 if (new_vpos >= 0)
14409 {
14410 struct glyph_row *row;
14411
14412 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14413 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14414 ++row;
14415
14416 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14417 MATRIX_ROW_START_BYTEPOS (row));
14418
14419 if (w != XWINDOW (selected_window))
14420 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14421 else if (current_buffer == old)
14422 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14423
14424 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14425
14426 /* If we are highlighting the region, then we just changed
14427 the region, so redisplay to show it. */
14428 if (!NILP (Vtransient_mark_mode)
14429 && !NILP (BVAR (current_buffer, mark_active)))
14430 {
14431 clear_glyph_matrix (w->desired_matrix);
14432 if (!try_window (window, startp, 0))
14433 goto need_larger_matrices;
14434 }
14435 }
14436
14437 #if GLYPH_DEBUG
14438 debug_method_add (w, "forced window start");
14439 #endif
14440 goto done;
14441 }
14442
14443 /* Handle case where text has not changed, only point, and it has
14444 not moved off the frame, and we are not retrying after hscroll.
14445 (current_matrix_up_to_date_p is nonzero when retrying.) */
14446 if (current_matrix_up_to_date_p
14447 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14448 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14449 {
14450 switch (rc)
14451 {
14452 case CURSOR_MOVEMENT_SUCCESS:
14453 used_current_matrix_p = 1;
14454 goto done;
14455
14456 case CURSOR_MOVEMENT_MUST_SCROLL:
14457 goto try_to_scroll;
14458
14459 default:
14460 abort ();
14461 }
14462 }
14463 /* If current starting point was originally the beginning of a line
14464 but no longer is, find a new starting point. */
14465 else if (!NILP (w->start_at_line_beg)
14466 && !(CHARPOS (startp) <= BEGV
14467 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14468 {
14469 #if GLYPH_DEBUG
14470 debug_method_add (w, "recenter 1");
14471 #endif
14472 goto recenter;
14473 }
14474
14475 /* Try scrolling with try_window_id. Value is > 0 if update has
14476 been done, it is -1 if we know that the same window start will
14477 not work. It is 0 if unsuccessful for some other reason. */
14478 else if ((tem = try_window_id (w)) != 0)
14479 {
14480 #if GLYPH_DEBUG
14481 debug_method_add (w, "try_window_id %d", tem);
14482 #endif
14483
14484 if (fonts_changed_p)
14485 goto need_larger_matrices;
14486 if (tem > 0)
14487 goto done;
14488
14489 /* Otherwise try_window_id has returned -1 which means that we
14490 don't want the alternative below this comment to execute. */
14491 }
14492 else if (CHARPOS (startp) >= BEGV
14493 && CHARPOS (startp) <= ZV
14494 && PT >= CHARPOS (startp)
14495 && (CHARPOS (startp) < ZV
14496 /* Avoid starting at end of buffer. */
14497 || CHARPOS (startp) == BEGV
14498 || (XFASTINT (w->last_modified) >= MODIFF
14499 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14500 {
14501
14502 /* If first window line is a continuation line, and window start
14503 is inside the modified region, but the first change is before
14504 current window start, we must select a new window start.
14505
14506 However, if this is the result of a down-mouse event (e.g. by
14507 extending the mouse-drag-overlay), we don't want to select a
14508 new window start, since that would change the position under
14509 the mouse, resulting in an unwanted mouse-movement rather
14510 than a simple mouse-click. */
14511 if (NILP (w->start_at_line_beg)
14512 && NILP (do_mouse_tracking)
14513 && CHARPOS (startp) > BEGV
14514 && CHARPOS (startp) > BEG + beg_unchanged
14515 && CHARPOS (startp) <= Z - end_unchanged
14516 /* Even if w->start_at_line_beg is nil, a new window may
14517 start at a line_beg, since that's how set_buffer_window
14518 sets it. So, we need to check the return value of
14519 compute_window_start_on_continuation_line. (See also
14520 bug#197). */
14521 && XMARKER (w->start)->buffer == current_buffer
14522 && compute_window_start_on_continuation_line (w))
14523 {
14524 w->force_start = Qt;
14525 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14526 goto force_start;
14527 }
14528
14529 #if GLYPH_DEBUG
14530 debug_method_add (w, "same window start");
14531 #endif
14532
14533 /* Try to redisplay starting at same place as before.
14534 If point has not moved off frame, accept the results. */
14535 if (!current_matrix_up_to_date_p
14536 /* Don't use try_window_reusing_current_matrix in this case
14537 because a window scroll function can have changed the
14538 buffer. */
14539 || !NILP (Vwindow_scroll_functions)
14540 || MINI_WINDOW_P (w)
14541 || !(used_current_matrix_p
14542 = try_window_reusing_current_matrix (w)))
14543 {
14544 IF_DEBUG (debug_method_add (w, "1"));
14545 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14546 /* -1 means we need to scroll.
14547 0 means we need new matrices, but fonts_changed_p
14548 is set in that case, so we will detect it below. */
14549 goto try_to_scroll;
14550 }
14551
14552 if (fonts_changed_p)
14553 goto need_larger_matrices;
14554
14555 if (w->cursor.vpos >= 0)
14556 {
14557 if (!just_this_one_p
14558 || current_buffer->clip_changed
14559 || BEG_UNCHANGED < CHARPOS (startp))
14560 /* Forget any recorded base line for line number display. */
14561 w->base_line_number = Qnil;
14562
14563 if (!cursor_row_fully_visible_p (w, 1, 0))
14564 {
14565 clear_glyph_matrix (w->desired_matrix);
14566 last_line_misfit = 1;
14567 }
14568 /* Drop through and scroll. */
14569 else
14570 goto done;
14571 }
14572 else
14573 clear_glyph_matrix (w->desired_matrix);
14574 }
14575
14576 try_to_scroll:
14577
14578 w->last_modified = make_number (0);
14579 w->last_overlay_modified = make_number (0);
14580
14581 /* Redisplay the mode line. Select the buffer properly for that. */
14582 if (!update_mode_line)
14583 {
14584 update_mode_line = 1;
14585 w->update_mode_line = Qt;
14586 }
14587
14588 /* Try to scroll by specified few lines. */
14589 if ((scroll_conservatively
14590 || emacs_scroll_step
14591 || temp_scroll_step
14592 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14593 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14594 && CHARPOS (startp) >= BEGV
14595 && CHARPOS (startp) <= ZV)
14596 {
14597 /* The function returns -1 if new fonts were loaded, 1 if
14598 successful, 0 if not successful. */
14599 int ss = try_scrolling (window, just_this_one_p,
14600 scroll_conservatively,
14601 emacs_scroll_step,
14602 temp_scroll_step, last_line_misfit);
14603 switch (ss)
14604 {
14605 case SCROLLING_SUCCESS:
14606 goto done;
14607
14608 case SCROLLING_NEED_LARGER_MATRICES:
14609 goto need_larger_matrices;
14610
14611 case SCROLLING_FAILED:
14612 break;
14613
14614 default:
14615 abort ();
14616 }
14617 }
14618
14619 /* Finally, just choose a place to start which positions point
14620 according to user preferences. */
14621
14622 recenter:
14623
14624 #if GLYPH_DEBUG
14625 debug_method_add (w, "recenter");
14626 #endif
14627
14628 /* w->vscroll = 0; */
14629
14630 /* Forget any previously recorded base line for line number display. */
14631 if (!buffer_unchanged_p)
14632 w->base_line_number = Qnil;
14633
14634 /* Determine the window start relative to point. */
14635 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14636 it.current_y = it.last_visible_y;
14637 if (centering_position < 0)
14638 {
14639 int margin =
14640 scroll_margin > 0
14641 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14642 : 0;
14643 EMACS_INT margin_pos = CHARPOS (startp);
14644 int scrolling_up;
14645 Lisp_Object aggressive;
14646
14647 /* If there is a scroll margin at the top of the window, find
14648 its character position. */
14649 if (margin
14650 /* Cannot call start_display if startp is not in the
14651 accessible region of the buffer. This can happen when we
14652 have just switched to a different buffer and/or changed
14653 its restriction. In that case, startp is initialized to
14654 the character position 1 (BEG) because we did not yet
14655 have chance to display the buffer even once. */
14656 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14657 {
14658 struct it it1;
14659
14660 start_display (&it1, w, startp);
14661 move_it_vertically (&it1, margin);
14662 margin_pos = IT_CHARPOS (it1);
14663 }
14664 scrolling_up = PT > margin_pos;
14665 aggressive =
14666 scrolling_up
14667 ? BVAR (current_buffer, scroll_up_aggressively)
14668 : BVAR (current_buffer, scroll_down_aggressively);
14669
14670 if (!MINI_WINDOW_P (w)
14671 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14672 {
14673 int pt_offset = 0;
14674
14675 /* Setting scroll-conservatively overrides
14676 scroll-*-aggressively. */
14677 if (!scroll_conservatively && NUMBERP (aggressive))
14678 {
14679 double float_amount = XFLOATINT (aggressive);
14680
14681 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14682 if (pt_offset == 0 && float_amount > 0)
14683 pt_offset = 1;
14684 if (pt_offset)
14685 margin -= 1;
14686 }
14687 /* Compute how much to move the window start backward from
14688 point so that point will be displayed where the user
14689 wants it. */
14690 if (scrolling_up)
14691 {
14692 centering_position = it.last_visible_y;
14693 if (pt_offset)
14694 centering_position -= pt_offset;
14695 centering_position -=
14696 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14697 /* Don't let point enter the scroll margin near top of
14698 the window. */
14699 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14700 centering_position = margin * FRAME_LINE_HEIGHT (f);
14701 }
14702 else
14703 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14704 }
14705 else
14706 /* Set the window start half the height of the window backward
14707 from point. */
14708 centering_position = window_box_height (w) / 2;
14709 }
14710 move_it_vertically_backward (&it, centering_position);
14711
14712 xassert (IT_CHARPOS (it) >= BEGV);
14713
14714 /* The function move_it_vertically_backward may move over more
14715 than the specified y-distance. If it->w is small, e.g. a
14716 mini-buffer window, we may end up in front of the window's
14717 display area. Start displaying at the start of the line
14718 containing PT in this case. */
14719 if (it.current_y <= 0)
14720 {
14721 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14722 move_it_vertically_backward (&it, 0);
14723 it.current_y = 0;
14724 }
14725
14726 it.current_x = it.hpos = 0;
14727
14728 /* Set the window start position here explicitly, to avoid an
14729 infinite loop in case the functions in window-scroll-functions
14730 get errors. */
14731 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14732
14733 /* Run scroll hooks. */
14734 startp = run_window_scroll_functions (window, it.current.pos);
14735
14736 /* Redisplay the window. */
14737 if (!current_matrix_up_to_date_p
14738 || windows_or_buffers_changed
14739 || cursor_type_changed
14740 /* Don't use try_window_reusing_current_matrix in this case
14741 because it can have changed the buffer. */
14742 || !NILP (Vwindow_scroll_functions)
14743 || !just_this_one_p
14744 || MINI_WINDOW_P (w)
14745 || !(used_current_matrix_p
14746 = try_window_reusing_current_matrix (w)))
14747 try_window (window, startp, 0);
14748
14749 /* If new fonts have been loaded (due to fontsets), give up. We
14750 have to start a new redisplay since we need to re-adjust glyph
14751 matrices. */
14752 if (fonts_changed_p)
14753 goto need_larger_matrices;
14754
14755 /* If cursor did not appear assume that the middle of the window is
14756 in the first line of the window. Do it again with the next line.
14757 (Imagine a window of height 100, displaying two lines of height
14758 60. Moving back 50 from it->last_visible_y will end in the first
14759 line.) */
14760 if (w->cursor.vpos < 0)
14761 {
14762 if (!NILP (w->window_end_valid)
14763 && PT >= Z - XFASTINT (w->window_end_pos))
14764 {
14765 clear_glyph_matrix (w->desired_matrix);
14766 move_it_by_lines (&it, 1);
14767 try_window (window, it.current.pos, 0);
14768 }
14769 else if (PT < IT_CHARPOS (it))
14770 {
14771 clear_glyph_matrix (w->desired_matrix);
14772 move_it_by_lines (&it, -1);
14773 try_window (window, it.current.pos, 0);
14774 }
14775 else
14776 {
14777 /* Not much we can do about it. */
14778 }
14779 }
14780
14781 /* Consider the following case: Window starts at BEGV, there is
14782 invisible, intangible text at BEGV, so that display starts at
14783 some point START > BEGV. It can happen that we are called with
14784 PT somewhere between BEGV and START. Try to handle that case. */
14785 if (w->cursor.vpos < 0)
14786 {
14787 struct glyph_row *row = w->current_matrix->rows;
14788 if (row->mode_line_p)
14789 ++row;
14790 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14791 }
14792
14793 if (!cursor_row_fully_visible_p (w, 0, 0))
14794 {
14795 /* If vscroll is enabled, disable it and try again. */
14796 if (w->vscroll)
14797 {
14798 w->vscroll = 0;
14799 clear_glyph_matrix (w->desired_matrix);
14800 goto recenter;
14801 }
14802
14803 /* If centering point failed to make the whole line visible,
14804 put point at the top instead. That has to make the whole line
14805 visible, if it can be done. */
14806 if (centering_position == 0)
14807 goto done;
14808
14809 clear_glyph_matrix (w->desired_matrix);
14810 centering_position = 0;
14811 goto recenter;
14812 }
14813
14814 done:
14815
14816 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14817 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14818 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14819 ? Qt : Qnil);
14820
14821 /* Display the mode line, if we must. */
14822 if ((update_mode_line
14823 /* If window not full width, must redo its mode line
14824 if (a) the window to its side is being redone and
14825 (b) we do a frame-based redisplay. This is a consequence
14826 of how inverted lines are drawn in frame-based redisplay. */
14827 || (!just_this_one_p
14828 && !FRAME_WINDOW_P (f)
14829 && !WINDOW_FULL_WIDTH_P (w))
14830 /* Line number to display. */
14831 || INTEGERP (w->base_line_pos)
14832 /* Column number is displayed and different from the one displayed. */
14833 || (!NILP (w->column_number_displayed)
14834 && (XFASTINT (w->column_number_displayed) != current_column ())))
14835 /* This means that the window has a mode line. */
14836 && (WINDOW_WANTS_MODELINE_P (w)
14837 || WINDOW_WANTS_HEADER_LINE_P (w)))
14838 {
14839 display_mode_lines (w);
14840
14841 /* If mode line height has changed, arrange for a thorough
14842 immediate redisplay using the correct mode line height. */
14843 if (WINDOW_WANTS_MODELINE_P (w)
14844 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14845 {
14846 fonts_changed_p = 1;
14847 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14848 = DESIRED_MODE_LINE_HEIGHT (w);
14849 }
14850
14851 /* If header line height has changed, arrange for a thorough
14852 immediate redisplay using the correct header line height. */
14853 if (WINDOW_WANTS_HEADER_LINE_P (w)
14854 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14855 {
14856 fonts_changed_p = 1;
14857 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14858 = DESIRED_HEADER_LINE_HEIGHT (w);
14859 }
14860
14861 if (fonts_changed_p)
14862 goto need_larger_matrices;
14863 }
14864
14865 if (!line_number_displayed
14866 && !BUFFERP (w->base_line_pos))
14867 {
14868 w->base_line_pos = Qnil;
14869 w->base_line_number = Qnil;
14870 }
14871
14872 finish_menu_bars:
14873
14874 /* When we reach a frame's selected window, redo the frame's menu bar. */
14875 if (update_mode_line
14876 && EQ (FRAME_SELECTED_WINDOW (f), window))
14877 {
14878 int redisplay_menu_p = 0;
14879
14880 if (FRAME_WINDOW_P (f))
14881 {
14882 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14883 || defined (HAVE_NS) || defined (USE_GTK)
14884 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14885 #else
14886 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14887 #endif
14888 }
14889 else
14890 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14891
14892 if (redisplay_menu_p)
14893 display_menu_bar (w);
14894
14895 #ifdef HAVE_WINDOW_SYSTEM
14896 if (FRAME_WINDOW_P (f))
14897 {
14898 #if defined (USE_GTK) || defined (HAVE_NS)
14899 if (FRAME_EXTERNAL_TOOL_BAR (f))
14900 redisplay_tool_bar (f);
14901 #else
14902 if (WINDOWP (f->tool_bar_window)
14903 && (FRAME_TOOL_BAR_LINES (f) > 0
14904 || !NILP (Vauto_resize_tool_bars))
14905 && redisplay_tool_bar (f))
14906 ignore_mouse_drag_p = 1;
14907 #endif
14908 }
14909 #endif
14910 }
14911
14912 #ifdef HAVE_WINDOW_SYSTEM
14913 if (FRAME_WINDOW_P (f)
14914 && update_window_fringes (w, (just_this_one_p
14915 || (!used_current_matrix_p && !overlay_arrow_seen)
14916 || w->pseudo_window_p)))
14917 {
14918 update_begin (f);
14919 BLOCK_INPUT;
14920 if (draw_window_fringes (w, 1))
14921 x_draw_vertical_border (w);
14922 UNBLOCK_INPUT;
14923 update_end (f);
14924 }
14925 #endif /* HAVE_WINDOW_SYSTEM */
14926
14927 /* We go to this label, with fonts_changed_p nonzero,
14928 if it is necessary to try again using larger glyph matrices.
14929 We have to redeem the scroll bar even in this case,
14930 because the loop in redisplay_internal expects that. */
14931 need_larger_matrices:
14932 ;
14933 finish_scroll_bars:
14934
14935 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14936 {
14937 /* Set the thumb's position and size. */
14938 set_vertical_scroll_bar (w);
14939
14940 /* Note that we actually used the scroll bar attached to this
14941 window, so it shouldn't be deleted at the end of redisplay. */
14942 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14943 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14944 }
14945
14946 /* Restore current_buffer and value of point in it. The window
14947 update may have changed the buffer, so first make sure `opoint'
14948 is still valid (Bug#6177). */
14949 if (CHARPOS (opoint) < BEGV)
14950 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14951 else if (CHARPOS (opoint) > ZV)
14952 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14953 else
14954 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14955
14956 set_buffer_internal_1 (old);
14957 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14958 shorter. This can be caused by log truncation in *Messages*. */
14959 if (CHARPOS (lpoint) <= ZV)
14960 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14961
14962 unbind_to (count, Qnil);
14963 }
14964
14965
14966 /* Build the complete desired matrix of WINDOW with a window start
14967 buffer position POS.
14968
14969 Value is 1 if successful. It is zero if fonts were loaded during
14970 redisplay which makes re-adjusting glyph matrices necessary, and -1
14971 if point would appear in the scroll margins.
14972 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14973 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14974 set in FLAGS.) */
14975
14976 int
14977 try_window (Lisp_Object window, struct text_pos pos, int flags)
14978 {
14979 struct window *w = XWINDOW (window);
14980 struct it it;
14981 struct glyph_row *last_text_row = NULL;
14982 struct frame *f = XFRAME (w->frame);
14983
14984 /* Make POS the new window start. */
14985 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14986
14987 /* Mark cursor position as unknown. No overlay arrow seen. */
14988 w->cursor.vpos = -1;
14989 overlay_arrow_seen = 0;
14990
14991 /* Initialize iterator and info to start at POS. */
14992 start_display (&it, w, pos);
14993
14994 /* Display all lines of W. */
14995 while (it.current_y < it.last_visible_y)
14996 {
14997 if (display_line (&it))
14998 last_text_row = it.glyph_row - 1;
14999 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15000 return 0;
15001 }
15002
15003 /* Don't let the cursor end in the scroll margins. */
15004 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15005 && !MINI_WINDOW_P (w))
15006 {
15007 int this_scroll_margin;
15008
15009 if (scroll_margin > 0)
15010 {
15011 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15012 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15013 }
15014 else
15015 this_scroll_margin = 0;
15016
15017 if ((w->cursor.y >= 0 /* not vscrolled */
15018 && w->cursor.y < this_scroll_margin
15019 && CHARPOS (pos) > BEGV
15020 && IT_CHARPOS (it) < ZV)
15021 /* rms: considering make_cursor_line_fully_visible_p here
15022 seems to give wrong results. We don't want to recenter
15023 when the last line is partly visible, we want to allow
15024 that case to be handled in the usual way. */
15025 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15026 {
15027 w->cursor.vpos = -1;
15028 clear_glyph_matrix (w->desired_matrix);
15029 return -1;
15030 }
15031 }
15032
15033 /* If bottom moved off end of frame, change mode line percentage. */
15034 if (XFASTINT (w->window_end_pos) <= 0
15035 && Z != IT_CHARPOS (it))
15036 w->update_mode_line = Qt;
15037
15038 /* Set window_end_pos to the offset of the last character displayed
15039 on the window from the end of current_buffer. Set
15040 window_end_vpos to its row number. */
15041 if (last_text_row)
15042 {
15043 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15044 w->window_end_bytepos
15045 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15046 w->window_end_pos
15047 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15048 w->window_end_vpos
15049 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15050 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15051 ->displays_text_p);
15052 }
15053 else
15054 {
15055 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15056 w->window_end_pos = make_number (Z - ZV);
15057 w->window_end_vpos = make_number (0);
15058 }
15059
15060 /* But that is not valid info until redisplay finishes. */
15061 w->window_end_valid = Qnil;
15062 return 1;
15063 }
15064
15065
15066 \f
15067 /************************************************************************
15068 Window redisplay reusing current matrix when buffer has not changed
15069 ************************************************************************/
15070
15071 /* Try redisplay of window W showing an unchanged buffer with a
15072 different window start than the last time it was displayed by
15073 reusing its current matrix. Value is non-zero if successful.
15074 W->start is the new window start. */
15075
15076 static int
15077 try_window_reusing_current_matrix (struct window *w)
15078 {
15079 struct frame *f = XFRAME (w->frame);
15080 struct glyph_row *bottom_row;
15081 struct it it;
15082 struct run run;
15083 struct text_pos start, new_start;
15084 int nrows_scrolled, i;
15085 struct glyph_row *last_text_row;
15086 struct glyph_row *last_reused_text_row;
15087 struct glyph_row *start_row;
15088 int start_vpos, min_y, max_y;
15089
15090 #if GLYPH_DEBUG
15091 if (inhibit_try_window_reusing)
15092 return 0;
15093 #endif
15094
15095 if (/* This function doesn't handle terminal frames. */
15096 !FRAME_WINDOW_P (f)
15097 /* Don't try to reuse the display if windows have been split
15098 or such. */
15099 || windows_or_buffers_changed
15100 || cursor_type_changed)
15101 return 0;
15102
15103 /* Can't do this if region may have changed. */
15104 if ((!NILP (Vtransient_mark_mode)
15105 && !NILP (BVAR (current_buffer, mark_active)))
15106 || !NILP (w->region_showing)
15107 || !NILP (Vshow_trailing_whitespace))
15108 return 0;
15109
15110 /* If top-line visibility has changed, give up. */
15111 if (WINDOW_WANTS_HEADER_LINE_P (w)
15112 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15113 return 0;
15114
15115 /* Give up if old or new display is scrolled vertically. We could
15116 make this function handle this, but right now it doesn't. */
15117 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15118 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15119 return 0;
15120
15121 /* The variable new_start now holds the new window start. The old
15122 start `start' can be determined from the current matrix. */
15123 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15124 start = start_row->minpos;
15125 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15126
15127 /* Clear the desired matrix for the display below. */
15128 clear_glyph_matrix (w->desired_matrix);
15129
15130 if (CHARPOS (new_start) <= CHARPOS (start))
15131 {
15132 /* Don't use this method if the display starts with an ellipsis
15133 displayed for invisible text. It's not easy to handle that case
15134 below, and it's certainly not worth the effort since this is
15135 not a frequent case. */
15136 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15137 return 0;
15138
15139 IF_DEBUG (debug_method_add (w, "twu1"));
15140
15141 /* Display up to a row that can be reused. The variable
15142 last_text_row is set to the last row displayed that displays
15143 text. Note that it.vpos == 0 if or if not there is a
15144 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15145 start_display (&it, w, new_start);
15146 w->cursor.vpos = -1;
15147 last_text_row = last_reused_text_row = NULL;
15148
15149 while (it.current_y < it.last_visible_y
15150 && !fonts_changed_p)
15151 {
15152 /* If we have reached into the characters in the START row,
15153 that means the line boundaries have changed. So we
15154 can't start copying with the row START. Maybe it will
15155 work to start copying with the following row. */
15156 while (IT_CHARPOS (it) > CHARPOS (start))
15157 {
15158 /* Advance to the next row as the "start". */
15159 start_row++;
15160 start = start_row->minpos;
15161 /* If there are no more rows to try, or just one, give up. */
15162 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15163 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15164 || CHARPOS (start) == ZV)
15165 {
15166 clear_glyph_matrix (w->desired_matrix);
15167 return 0;
15168 }
15169
15170 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15171 }
15172 /* If we have reached alignment,
15173 we can copy the rest of the rows. */
15174 if (IT_CHARPOS (it) == CHARPOS (start))
15175 break;
15176
15177 if (display_line (&it))
15178 last_text_row = it.glyph_row - 1;
15179 }
15180
15181 /* A value of current_y < last_visible_y means that we stopped
15182 at the previous window start, which in turn means that we
15183 have at least one reusable row. */
15184 if (it.current_y < it.last_visible_y)
15185 {
15186 struct glyph_row *row;
15187
15188 /* IT.vpos always starts from 0; it counts text lines. */
15189 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15190
15191 /* Find PT if not already found in the lines displayed. */
15192 if (w->cursor.vpos < 0)
15193 {
15194 int dy = it.current_y - start_row->y;
15195
15196 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15197 row = row_containing_pos (w, PT, row, NULL, dy);
15198 if (row)
15199 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15200 dy, nrows_scrolled);
15201 else
15202 {
15203 clear_glyph_matrix (w->desired_matrix);
15204 return 0;
15205 }
15206 }
15207
15208 /* Scroll the display. Do it before the current matrix is
15209 changed. The problem here is that update has not yet
15210 run, i.e. part of the current matrix is not up to date.
15211 scroll_run_hook will clear the cursor, and use the
15212 current matrix to get the height of the row the cursor is
15213 in. */
15214 run.current_y = start_row->y;
15215 run.desired_y = it.current_y;
15216 run.height = it.last_visible_y - it.current_y;
15217
15218 if (run.height > 0 && run.current_y != run.desired_y)
15219 {
15220 update_begin (f);
15221 FRAME_RIF (f)->update_window_begin_hook (w);
15222 FRAME_RIF (f)->clear_window_mouse_face (w);
15223 FRAME_RIF (f)->scroll_run_hook (w, &run);
15224 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15225 update_end (f);
15226 }
15227
15228 /* Shift current matrix down by nrows_scrolled lines. */
15229 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15230 rotate_matrix (w->current_matrix,
15231 start_vpos,
15232 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15233 nrows_scrolled);
15234
15235 /* Disable lines that must be updated. */
15236 for (i = 0; i < nrows_scrolled; ++i)
15237 (start_row + i)->enabled_p = 0;
15238
15239 /* Re-compute Y positions. */
15240 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15241 max_y = it.last_visible_y;
15242 for (row = start_row + nrows_scrolled;
15243 row < bottom_row;
15244 ++row)
15245 {
15246 row->y = it.current_y;
15247 row->visible_height = row->height;
15248
15249 if (row->y < min_y)
15250 row->visible_height -= min_y - row->y;
15251 if (row->y + row->height > max_y)
15252 row->visible_height -= row->y + row->height - max_y;
15253 row->redraw_fringe_bitmaps_p = 1;
15254
15255 it.current_y += row->height;
15256
15257 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15258 last_reused_text_row = row;
15259 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15260 break;
15261 }
15262
15263 /* Disable lines in the current matrix which are now
15264 below the window. */
15265 for (++row; row < bottom_row; ++row)
15266 row->enabled_p = row->mode_line_p = 0;
15267 }
15268
15269 /* Update window_end_pos etc.; last_reused_text_row is the last
15270 reused row from the current matrix containing text, if any.
15271 The value of last_text_row is the last displayed line
15272 containing text. */
15273 if (last_reused_text_row)
15274 {
15275 w->window_end_bytepos
15276 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15277 w->window_end_pos
15278 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15279 w->window_end_vpos
15280 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15281 w->current_matrix));
15282 }
15283 else if (last_text_row)
15284 {
15285 w->window_end_bytepos
15286 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15287 w->window_end_pos
15288 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15289 w->window_end_vpos
15290 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15291 }
15292 else
15293 {
15294 /* This window must be completely empty. */
15295 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15296 w->window_end_pos = make_number (Z - ZV);
15297 w->window_end_vpos = make_number (0);
15298 }
15299 w->window_end_valid = Qnil;
15300
15301 /* Update hint: don't try scrolling again in update_window. */
15302 w->desired_matrix->no_scrolling_p = 1;
15303
15304 #if GLYPH_DEBUG
15305 debug_method_add (w, "try_window_reusing_current_matrix 1");
15306 #endif
15307 return 1;
15308 }
15309 else if (CHARPOS (new_start) > CHARPOS (start))
15310 {
15311 struct glyph_row *pt_row, *row;
15312 struct glyph_row *first_reusable_row;
15313 struct glyph_row *first_row_to_display;
15314 int dy;
15315 int yb = window_text_bottom_y (w);
15316
15317 /* Find the row starting at new_start, if there is one. Don't
15318 reuse a partially visible line at the end. */
15319 first_reusable_row = start_row;
15320 while (first_reusable_row->enabled_p
15321 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15322 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15323 < CHARPOS (new_start)))
15324 ++first_reusable_row;
15325
15326 /* Give up if there is no row to reuse. */
15327 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15328 || !first_reusable_row->enabled_p
15329 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15330 != CHARPOS (new_start)))
15331 return 0;
15332
15333 /* We can reuse fully visible rows beginning with
15334 first_reusable_row to the end of the window. Set
15335 first_row_to_display to the first row that cannot be reused.
15336 Set pt_row to the row containing point, if there is any. */
15337 pt_row = NULL;
15338 for (first_row_to_display = first_reusable_row;
15339 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15340 ++first_row_to_display)
15341 {
15342 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15343 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15344 pt_row = first_row_to_display;
15345 }
15346
15347 /* Start displaying at the start of first_row_to_display. */
15348 xassert (first_row_to_display->y < yb);
15349 init_to_row_start (&it, w, first_row_to_display);
15350
15351 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15352 - start_vpos);
15353 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15354 - nrows_scrolled);
15355 it.current_y = (first_row_to_display->y - first_reusable_row->y
15356 + WINDOW_HEADER_LINE_HEIGHT (w));
15357
15358 /* Display lines beginning with first_row_to_display in the
15359 desired matrix. Set last_text_row to the last row displayed
15360 that displays text. */
15361 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15362 if (pt_row == NULL)
15363 w->cursor.vpos = -1;
15364 last_text_row = NULL;
15365 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15366 if (display_line (&it))
15367 last_text_row = it.glyph_row - 1;
15368
15369 /* If point is in a reused row, adjust y and vpos of the cursor
15370 position. */
15371 if (pt_row)
15372 {
15373 w->cursor.vpos -= nrows_scrolled;
15374 w->cursor.y -= first_reusable_row->y - start_row->y;
15375 }
15376
15377 /* Give up if point isn't in a row displayed or reused. (This
15378 also handles the case where w->cursor.vpos < nrows_scrolled
15379 after the calls to display_line, which can happen with scroll
15380 margins. See bug#1295.) */
15381 if (w->cursor.vpos < 0)
15382 {
15383 clear_glyph_matrix (w->desired_matrix);
15384 return 0;
15385 }
15386
15387 /* Scroll the display. */
15388 run.current_y = first_reusable_row->y;
15389 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15390 run.height = it.last_visible_y - run.current_y;
15391 dy = run.current_y - run.desired_y;
15392
15393 if (run.height)
15394 {
15395 update_begin (f);
15396 FRAME_RIF (f)->update_window_begin_hook (w);
15397 FRAME_RIF (f)->clear_window_mouse_face (w);
15398 FRAME_RIF (f)->scroll_run_hook (w, &run);
15399 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15400 update_end (f);
15401 }
15402
15403 /* Adjust Y positions of reused rows. */
15404 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15405 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15406 max_y = it.last_visible_y;
15407 for (row = first_reusable_row; row < first_row_to_display; ++row)
15408 {
15409 row->y -= dy;
15410 row->visible_height = row->height;
15411 if (row->y < min_y)
15412 row->visible_height -= min_y - row->y;
15413 if (row->y + row->height > max_y)
15414 row->visible_height -= row->y + row->height - max_y;
15415 row->redraw_fringe_bitmaps_p = 1;
15416 }
15417
15418 /* Scroll the current matrix. */
15419 xassert (nrows_scrolled > 0);
15420 rotate_matrix (w->current_matrix,
15421 start_vpos,
15422 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15423 -nrows_scrolled);
15424
15425 /* Disable rows not reused. */
15426 for (row -= nrows_scrolled; row < bottom_row; ++row)
15427 row->enabled_p = 0;
15428
15429 /* Point may have moved to a different line, so we cannot assume that
15430 the previous cursor position is valid; locate the correct row. */
15431 if (pt_row)
15432 {
15433 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15434 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15435 row++)
15436 {
15437 w->cursor.vpos++;
15438 w->cursor.y = row->y;
15439 }
15440 if (row < bottom_row)
15441 {
15442 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15443 struct glyph *end = glyph + row->used[TEXT_AREA];
15444
15445 /* Can't use this optimization with bidi-reordered glyph
15446 rows, unless cursor is already at point. */
15447 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15448 {
15449 if (!(w->cursor.hpos >= 0
15450 && w->cursor.hpos < row->used[TEXT_AREA]
15451 && BUFFERP (glyph->object)
15452 && glyph->charpos == PT))
15453 return 0;
15454 }
15455 else
15456 for (; glyph < end
15457 && (!BUFFERP (glyph->object)
15458 || glyph->charpos < PT);
15459 glyph++)
15460 {
15461 w->cursor.hpos++;
15462 w->cursor.x += glyph->pixel_width;
15463 }
15464 }
15465 }
15466
15467 /* Adjust window end. A null value of last_text_row means that
15468 the window end is in reused rows which in turn means that
15469 only its vpos can have changed. */
15470 if (last_text_row)
15471 {
15472 w->window_end_bytepos
15473 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15474 w->window_end_pos
15475 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15476 w->window_end_vpos
15477 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15478 }
15479 else
15480 {
15481 w->window_end_vpos
15482 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15483 }
15484
15485 w->window_end_valid = Qnil;
15486 w->desired_matrix->no_scrolling_p = 1;
15487
15488 #if GLYPH_DEBUG
15489 debug_method_add (w, "try_window_reusing_current_matrix 2");
15490 #endif
15491 return 1;
15492 }
15493
15494 return 0;
15495 }
15496
15497
15498 \f
15499 /************************************************************************
15500 Window redisplay reusing current matrix when buffer has changed
15501 ************************************************************************/
15502
15503 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15504 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15505 EMACS_INT *, EMACS_INT *);
15506 static struct glyph_row *
15507 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15508 struct glyph_row *);
15509
15510
15511 /* Return the last row in MATRIX displaying text. If row START is
15512 non-null, start searching with that row. IT gives the dimensions
15513 of the display. Value is null if matrix is empty; otherwise it is
15514 a pointer to the row found. */
15515
15516 static struct glyph_row *
15517 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15518 struct glyph_row *start)
15519 {
15520 struct glyph_row *row, *row_found;
15521
15522 /* Set row_found to the last row in IT->w's current matrix
15523 displaying text. The loop looks funny but think of partially
15524 visible lines. */
15525 row_found = NULL;
15526 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15527 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15528 {
15529 xassert (row->enabled_p);
15530 row_found = row;
15531 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15532 break;
15533 ++row;
15534 }
15535
15536 return row_found;
15537 }
15538
15539
15540 /* Return the last row in the current matrix of W that is not affected
15541 by changes at the start of current_buffer that occurred since W's
15542 current matrix was built. Value is null if no such row exists.
15543
15544 BEG_UNCHANGED us the number of characters unchanged at the start of
15545 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15546 first changed character in current_buffer. Characters at positions <
15547 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15548 when the current matrix was built. */
15549
15550 static struct glyph_row *
15551 find_last_unchanged_at_beg_row (struct window *w)
15552 {
15553 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15554 struct glyph_row *row;
15555 struct glyph_row *row_found = NULL;
15556 int yb = window_text_bottom_y (w);
15557
15558 /* Find the last row displaying unchanged text. */
15559 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15560 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15561 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15562 ++row)
15563 {
15564 if (/* If row ends before first_changed_pos, it is unchanged,
15565 except in some case. */
15566 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15567 /* When row ends in ZV and we write at ZV it is not
15568 unchanged. */
15569 && !row->ends_at_zv_p
15570 /* When first_changed_pos is the end of a continued line,
15571 row is not unchanged because it may be no longer
15572 continued. */
15573 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15574 && (row->continued_p
15575 || row->exact_window_width_line_p)))
15576 row_found = row;
15577
15578 /* Stop if last visible row. */
15579 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15580 break;
15581 }
15582
15583 return row_found;
15584 }
15585
15586
15587 /* Find the first glyph row in the current matrix of W that is not
15588 affected by changes at the end of current_buffer since the
15589 time W's current matrix was built.
15590
15591 Return in *DELTA the number of chars by which buffer positions in
15592 unchanged text at the end of current_buffer must be adjusted.
15593
15594 Return in *DELTA_BYTES the corresponding number of bytes.
15595
15596 Value is null if no such row exists, i.e. all rows are affected by
15597 changes. */
15598
15599 static struct glyph_row *
15600 find_first_unchanged_at_end_row (struct window *w,
15601 EMACS_INT *delta, EMACS_INT *delta_bytes)
15602 {
15603 struct glyph_row *row;
15604 struct glyph_row *row_found = NULL;
15605
15606 *delta = *delta_bytes = 0;
15607
15608 /* Display must not have been paused, otherwise the current matrix
15609 is not up to date. */
15610 eassert (!NILP (w->window_end_valid));
15611
15612 /* A value of window_end_pos >= END_UNCHANGED means that the window
15613 end is in the range of changed text. If so, there is no
15614 unchanged row at the end of W's current matrix. */
15615 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15616 return NULL;
15617
15618 /* Set row to the last row in W's current matrix displaying text. */
15619 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15620
15621 /* If matrix is entirely empty, no unchanged row exists. */
15622 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15623 {
15624 /* The value of row is the last glyph row in the matrix having a
15625 meaningful buffer position in it. The end position of row
15626 corresponds to window_end_pos. This allows us to translate
15627 buffer positions in the current matrix to current buffer
15628 positions for characters not in changed text. */
15629 EMACS_INT Z_old =
15630 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15631 EMACS_INT Z_BYTE_old =
15632 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15633 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15634 struct glyph_row *first_text_row
15635 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15636
15637 *delta = Z - Z_old;
15638 *delta_bytes = Z_BYTE - Z_BYTE_old;
15639
15640 /* Set last_unchanged_pos to the buffer position of the last
15641 character in the buffer that has not been changed. Z is the
15642 index + 1 of the last character in current_buffer, i.e. by
15643 subtracting END_UNCHANGED we get the index of the last
15644 unchanged character, and we have to add BEG to get its buffer
15645 position. */
15646 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15647 last_unchanged_pos_old = last_unchanged_pos - *delta;
15648
15649 /* Search backward from ROW for a row displaying a line that
15650 starts at a minimum position >= last_unchanged_pos_old. */
15651 for (; row > first_text_row; --row)
15652 {
15653 /* This used to abort, but it can happen.
15654 It is ok to just stop the search instead here. KFS. */
15655 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15656 break;
15657
15658 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15659 row_found = row;
15660 }
15661 }
15662
15663 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15664
15665 return row_found;
15666 }
15667
15668
15669 /* Make sure that glyph rows in the current matrix of window W
15670 reference the same glyph memory as corresponding rows in the
15671 frame's frame matrix. This function is called after scrolling W's
15672 current matrix on a terminal frame in try_window_id and
15673 try_window_reusing_current_matrix. */
15674
15675 static void
15676 sync_frame_with_window_matrix_rows (struct window *w)
15677 {
15678 struct frame *f = XFRAME (w->frame);
15679 struct glyph_row *window_row, *window_row_end, *frame_row;
15680
15681 /* Preconditions: W must be a leaf window and full-width. Its frame
15682 must have a frame matrix. */
15683 xassert (NILP (w->hchild) && NILP (w->vchild));
15684 xassert (WINDOW_FULL_WIDTH_P (w));
15685 xassert (!FRAME_WINDOW_P (f));
15686
15687 /* If W is a full-width window, glyph pointers in W's current matrix
15688 have, by definition, to be the same as glyph pointers in the
15689 corresponding frame matrix. Note that frame matrices have no
15690 marginal areas (see build_frame_matrix). */
15691 window_row = w->current_matrix->rows;
15692 window_row_end = window_row + w->current_matrix->nrows;
15693 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15694 while (window_row < window_row_end)
15695 {
15696 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15697 struct glyph *end = window_row->glyphs[LAST_AREA];
15698
15699 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15700 frame_row->glyphs[TEXT_AREA] = start;
15701 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15702 frame_row->glyphs[LAST_AREA] = end;
15703
15704 /* Disable frame rows whose corresponding window rows have
15705 been disabled in try_window_id. */
15706 if (!window_row->enabled_p)
15707 frame_row->enabled_p = 0;
15708
15709 ++window_row, ++frame_row;
15710 }
15711 }
15712
15713
15714 /* Find the glyph row in window W containing CHARPOS. Consider all
15715 rows between START and END (not inclusive). END null means search
15716 all rows to the end of the display area of W. Value is the row
15717 containing CHARPOS or null. */
15718
15719 struct glyph_row *
15720 row_containing_pos (struct window *w, EMACS_INT charpos,
15721 struct glyph_row *start, struct glyph_row *end, int dy)
15722 {
15723 struct glyph_row *row = start;
15724 struct glyph_row *best_row = NULL;
15725 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15726 int last_y;
15727
15728 /* If we happen to start on a header-line, skip that. */
15729 if (row->mode_line_p)
15730 ++row;
15731
15732 if ((end && row >= end) || !row->enabled_p)
15733 return NULL;
15734
15735 last_y = window_text_bottom_y (w) - dy;
15736
15737 while (1)
15738 {
15739 /* Give up if we have gone too far. */
15740 if (end && row >= end)
15741 return NULL;
15742 /* This formerly returned if they were equal.
15743 I think that both quantities are of a "last plus one" type;
15744 if so, when they are equal, the row is within the screen. -- rms. */
15745 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15746 return NULL;
15747
15748 /* If it is in this row, return this row. */
15749 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15750 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15751 /* The end position of a row equals the start
15752 position of the next row. If CHARPOS is there, we
15753 would rather display it in the next line, except
15754 when this line ends in ZV. */
15755 && !row->ends_at_zv_p
15756 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15757 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15758 {
15759 struct glyph *g;
15760
15761 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15762 || (!best_row && !row->continued_p))
15763 return row;
15764 /* In bidi-reordered rows, there could be several rows
15765 occluding point, all of them belonging to the same
15766 continued line. We need to find the row which fits
15767 CHARPOS the best. */
15768 for (g = row->glyphs[TEXT_AREA];
15769 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15770 g++)
15771 {
15772 if (!STRINGP (g->object))
15773 {
15774 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15775 {
15776 mindif = eabs (g->charpos - charpos);
15777 best_row = row;
15778 /* Exact match always wins. */
15779 if (mindif == 0)
15780 return best_row;
15781 }
15782 }
15783 }
15784 }
15785 else if (best_row && !row->continued_p)
15786 return best_row;
15787 ++row;
15788 }
15789 }
15790
15791
15792 /* Try to redisplay window W by reusing its existing display. W's
15793 current matrix must be up to date when this function is called,
15794 i.e. window_end_valid must not be nil.
15795
15796 Value is
15797
15798 1 if display has been updated
15799 0 if otherwise unsuccessful
15800 -1 if redisplay with same window start is known not to succeed
15801
15802 The following steps are performed:
15803
15804 1. Find the last row in the current matrix of W that is not
15805 affected by changes at the start of current_buffer. If no such row
15806 is found, give up.
15807
15808 2. Find the first row in W's current matrix that is not affected by
15809 changes at the end of current_buffer. Maybe there is no such row.
15810
15811 3. Display lines beginning with the row + 1 found in step 1 to the
15812 row found in step 2 or, if step 2 didn't find a row, to the end of
15813 the window.
15814
15815 4. If cursor is not known to appear on the window, give up.
15816
15817 5. If display stopped at the row found in step 2, scroll the
15818 display and current matrix as needed.
15819
15820 6. Maybe display some lines at the end of W, if we must. This can
15821 happen under various circumstances, like a partially visible line
15822 becoming fully visible, or because newly displayed lines are displayed
15823 in smaller font sizes.
15824
15825 7. Update W's window end information. */
15826
15827 static int
15828 try_window_id (struct window *w)
15829 {
15830 struct frame *f = XFRAME (w->frame);
15831 struct glyph_matrix *current_matrix = w->current_matrix;
15832 struct glyph_matrix *desired_matrix = w->desired_matrix;
15833 struct glyph_row *last_unchanged_at_beg_row;
15834 struct glyph_row *first_unchanged_at_end_row;
15835 struct glyph_row *row;
15836 struct glyph_row *bottom_row;
15837 int bottom_vpos;
15838 struct it it;
15839 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15840 int dvpos, dy;
15841 struct text_pos start_pos;
15842 struct run run;
15843 int first_unchanged_at_end_vpos = 0;
15844 struct glyph_row *last_text_row, *last_text_row_at_end;
15845 struct text_pos start;
15846 EMACS_INT first_changed_charpos, last_changed_charpos;
15847
15848 #if GLYPH_DEBUG
15849 if (inhibit_try_window_id)
15850 return 0;
15851 #endif
15852
15853 /* This is handy for debugging. */
15854 #if 0
15855 #define GIVE_UP(X) \
15856 do { \
15857 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15858 return 0; \
15859 } while (0)
15860 #else
15861 #define GIVE_UP(X) return 0
15862 #endif
15863
15864 SET_TEXT_POS_FROM_MARKER (start, w->start);
15865
15866 /* Don't use this for mini-windows because these can show
15867 messages and mini-buffers, and we don't handle that here. */
15868 if (MINI_WINDOW_P (w))
15869 GIVE_UP (1);
15870
15871 /* This flag is used to prevent redisplay optimizations. */
15872 if (windows_or_buffers_changed || cursor_type_changed)
15873 GIVE_UP (2);
15874
15875 /* Verify that narrowing has not changed.
15876 Also verify that we were not told to prevent redisplay optimizations.
15877 It would be nice to further
15878 reduce the number of cases where this prevents try_window_id. */
15879 if (current_buffer->clip_changed
15880 || current_buffer->prevent_redisplay_optimizations_p)
15881 GIVE_UP (3);
15882
15883 /* Window must either use window-based redisplay or be full width. */
15884 if (!FRAME_WINDOW_P (f)
15885 && (!FRAME_LINE_INS_DEL_OK (f)
15886 || !WINDOW_FULL_WIDTH_P (w)))
15887 GIVE_UP (4);
15888
15889 /* Give up if point is known NOT to appear in W. */
15890 if (PT < CHARPOS (start))
15891 GIVE_UP (5);
15892
15893 /* Another way to prevent redisplay optimizations. */
15894 if (XFASTINT (w->last_modified) == 0)
15895 GIVE_UP (6);
15896
15897 /* Verify that window is not hscrolled. */
15898 if (XFASTINT (w->hscroll) != 0)
15899 GIVE_UP (7);
15900
15901 /* Verify that display wasn't paused. */
15902 if (NILP (w->window_end_valid))
15903 GIVE_UP (8);
15904
15905 /* Can't use this if highlighting a region because a cursor movement
15906 will do more than just set the cursor. */
15907 if (!NILP (Vtransient_mark_mode)
15908 && !NILP (BVAR (current_buffer, mark_active)))
15909 GIVE_UP (9);
15910
15911 /* Likewise if highlighting trailing whitespace. */
15912 if (!NILP (Vshow_trailing_whitespace))
15913 GIVE_UP (11);
15914
15915 /* Likewise if showing a region. */
15916 if (!NILP (w->region_showing))
15917 GIVE_UP (10);
15918
15919 /* Can't use this if overlay arrow position and/or string have
15920 changed. */
15921 if (overlay_arrows_changed_p ())
15922 GIVE_UP (12);
15923
15924 /* When word-wrap is on, adding a space to the first word of a
15925 wrapped line can change the wrap position, altering the line
15926 above it. It might be worthwhile to handle this more
15927 intelligently, but for now just redisplay from scratch. */
15928 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
15929 GIVE_UP (21);
15930
15931 /* Under bidi reordering, adding or deleting a character in the
15932 beginning of a paragraph, before the first strong directional
15933 character, can change the base direction of the paragraph (unless
15934 the buffer specifies a fixed paragraph direction), which will
15935 require to redisplay the whole paragraph. It might be worthwhile
15936 to find the paragraph limits and widen the range of redisplayed
15937 lines to that, but for now just give up this optimization and
15938 redisplay from scratch. */
15939 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15940 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
15941 GIVE_UP (22);
15942
15943 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15944 only if buffer has really changed. The reason is that the gap is
15945 initially at Z for freshly visited files. The code below would
15946 set end_unchanged to 0 in that case. */
15947 if (MODIFF > SAVE_MODIFF
15948 /* This seems to happen sometimes after saving a buffer. */
15949 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15950 {
15951 if (GPT - BEG < BEG_UNCHANGED)
15952 BEG_UNCHANGED = GPT - BEG;
15953 if (Z - GPT < END_UNCHANGED)
15954 END_UNCHANGED = Z - GPT;
15955 }
15956
15957 /* The position of the first and last character that has been changed. */
15958 first_changed_charpos = BEG + BEG_UNCHANGED;
15959 last_changed_charpos = Z - END_UNCHANGED;
15960
15961 /* If window starts after a line end, and the last change is in
15962 front of that newline, then changes don't affect the display.
15963 This case happens with stealth-fontification. Note that although
15964 the display is unchanged, glyph positions in the matrix have to
15965 be adjusted, of course. */
15966 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15967 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15968 && ((last_changed_charpos < CHARPOS (start)
15969 && CHARPOS (start) == BEGV)
15970 || (last_changed_charpos < CHARPOS (start) - 1
15971 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15972 {
15973 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
15974 struct glyph_row *r0;
15975
15976 /* Compute how many chars/bytes have been added to or removed
15977 from the buffer. */
15978 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15979 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15980 Z_delta = Z - Z_old;
15981 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
15982
15983 /* Give up if PT is not in the window. Note that it already has
15984 been checked at the start of try_window_id that PT is not in
15985 front of the window start. */
15986 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
15987 GIVE_UP (13);
15988
15989 /* If window start is unchanged, we can reuse the whole matrix
15990 as is, after adjusting glyph positions. No need to compute
15991 the window end again, since its offset from Z hasn't changed. */
15992 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15993 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
15994 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
15995 /* PT must not be in a partially visible line. */
15996 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
15997 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15998 {
15999 /* Adjust positions in the glyph matrix. */
16000 if (Z_delta || Z_delta_bytes)
16001 {
16002 struct glyph_row *r1
16003 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16004 increment_matrix_positions (w->current_matrix,
16005 MATRIX_ROW_VPOS (r0, current_matrix),
16006 MATRIX_ROW_VPOS (r1, current_matrix),
16007 Z_delta, Z_delta_bytes);
16008 }
16009
16010 /* Set the cursor. */
16011 row = row_containing_pos (w, PT, r0, NULL, 0);
16012 if (row)
16013 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16014 else
16015 abort ();
16016 return 1;
16017 }
16018 }
16019
16020 /* Handle the case that changes are all below what is displayed in
16021 the window, and that PT is in the window. This shortcut cannot
16022 be taken if ZV is visible in the window, and text has been added
16023 there that is visible in the window. */
16024 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16025 /* ZV is not visible in the window, or there are no
16026 changes at ZV, actually. */
16027 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16028 || first_changed_charpos == last_changed_charpos))
16029 {
16030 struct glyph_row *r0;
16031
16032 /* Give up if PT is not in the window. Note that it already has
16033 been checked at the start of try_window_id that PT is not in
16034 front of the window start. */
16035 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16036 GIVE_UP (14);
16037
16038 /* If window start is unchanged, we can reuse the whole matrix
16039 as is, without changing glyph positions since no text has
16040 been added/removed in front of the window end. */
16041 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16042 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16043 /* PT must not be in a partially visible line. */
16044 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16045 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16046 {
16047 /* We have to compute the window end anew since text
16048 could have been added/removed after it. */
16049 w->window_end_pos
16050 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16051 w->window_end_bytepos
16052 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16053
16054 /* Set the cursor. */
16055 row = row_containing_pos (w, PT, r0, NULL, 0);
16056 if (row)
16057 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16058 else
16059 abort ();
16060 return 2;
16061 }
16062 }
16063
16064 /* Give up if window start is in the changed area.
16065
16066 The condition used to read
16067
16068 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16069
16070 but why that was tested escapes me at the moment. */
16071 if (CHARPOS (start) >= first_changed_charpos
16072 && CHARPOS (start) <= last_changed_charpos)
16073 GIVE_UP (15);
16074
16075 /* Check that window start agrees with the start of the first glyph
16076 row in its current matrix. Check this after we know the window
16077 start is not in changed text, otherwise positions would not be
16078 comparable. */
16079 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16080 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16081 GIVE_UP (16);
16082
16083 /* Give up if the window ends in strings. Overlay strings
16084 at the end are difficult to handle, so don't try. */
16085 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16086 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16087 GIVE_UP (20);
16088
16089 /* Compute the position at which we have to start displaying new
16090 lines. Some of the lines at the top of the window might be
16091 reusable because they are not displaying changed text. Find the
16092 last row in W's current matrix not affected by changes at the
16093 start of current_buffer. Value is null if changes start in the
16094 first line of window. */
16095 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16096 if (last_unchanged_at_beg_row)
16097 {
16098 /* Avoid starting to display in the moddle of a character, a TAB
16099 for instance. This is easier than to set up the iterator
16100 exactly, and it's not a frequent case, so the additional
16101 effort wouldn't really pay off. */
16102 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16103 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16104 && last_unchanged_at_beg_row > w->current_matrix->rows)
16105 --last_unchanged_at_beg_row;
16106
16107 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16108 GIVE_UP (17);
16109
16110 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16111 GIVE_UP (18);
16112 start_pos = it.current.pos;
16113
16114 /* Start displaying new lines in the desired matrix at the same
16115 vpos we would use in the current matrix, i.e. below
16116 last_unchanged_at_beg_row. */
16117 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16118 current_matrix);
16119 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16120 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16121
16122 xassert (it.hpos == 0 && it.current_x == 0);
16123 }
16124 else
16125 {
16126 /* There are no reusable lines at the start of the window.
16127 Start displaying in the first text line. */
16128 start_display (&it, w, start);
16129 it.vpos = it.first_vpos;
16130 start_pos = it.current.pos;
16131 }
16132
16133 /* Find the first row that is not affected by changes at the end of
16134 the buffer. Value will be null if there is no unchanged row, in
16135 which case we must redisplay to the end of the window. delta
16136 will be set to the value by which buffer positions beginning with
16137 first_unchanged_at_end_row have to be adjusted due to text
16138 changes. */
16139 first_unchanged_at_end_row
16140 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16141 IF_DEBUG (debug_delta = delta);
16142 IF_DEBUG (debug_delta_bytes = delta_bytes);
16143
16144 /* Set stop_pos to the buffer position up to which we will have to
16145 display new lines. If first_unchanged_at_end_row != NULL, this
16146 is the buffer position of the start of the line displayed in that
16147 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16148 that we don't stop at a buffer position. */
16149 stop_pos = 0;
16150 if (first_unchanged_at_end_row)
16151 {
16152 xassert (last_unchanged_at_beg_row == NULL
16153 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16154
16155 /* If this is a continuation line, move forward to the next one
16156 that isn't. Changes in lines above affect this line.
16157 Caution: this may move first_unchanged_at_end_row to a row
16158 not displaying text. */
16159 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16160 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16161 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16162 < it.last_visible_y))
16163 ++first_unchanged_at_end_row;
16164
16165 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16166 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16167 >= it.last_visible_y))
16168 first_unchanged_at_end_row = NULL;
16169 else
16170 {
16171 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16172 + delta);
16173 first_unchanged_at_end_vpos
16174 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16175 xassert (stop_pos >= Z - END_UNCHANGED);
16176 }
16177 }
16178 else if (last_unchanged_at_beg_row == NULL)
16179 GIVE_UP (19);
16180
16181
16182 #if GLYPH_DEBUG
16183
16184 /* Either there is no unchanged row at the end, or the one we have
16185 now displays text. This is a necessary condition for the window
16186 end pos calculation at the end of this function. */
16187 xassert (first_unchanged_at_end_row == NULL
16188 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16189
16190 debug_last_unchanged_at_beg_vpos
16191 = (last_unchanged_at_beg_row
16192 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16193 : -1);
16194 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16195
16196 #endif /* GLYPH_DEBUG != 0 */
16197
16198
16199 /* Display new lines. Set last_text_row to the last new line
16200 displayed which has text on it, i.e. might end up as being the
16201 line where the window_end_vpos is. */
16202 w->cursor.vpos = -1;
16203 last_text_row = NULL;
16204 overlay_arrow_seen = 0;
16205 while (it.current_y < it.last_visible_y
16206 && !fonts_changed_p
16207 && (first_unchanged_at_end_row == NULL
16208 || IT_CHARPOS (it) < stop_pos))
16209 {
16210 if (display_line (&it))
16211 last_text_row = it.glyph_row - 1;
16212 }
16213
16214 if (fonts_changed_p)
16215 return -1;
16216
16217
16218 /* Compute differences in buffer positions, y-positions etc. for
16219 lines reused at the bottom of the window. Compute what we can
16220 scroll. */
16221 if (first_unchanged_at_end_row
16222 /* No lines reused because we displayed everything up to the
16223 bottom of the window. */
16224 && it.current_y < it.last_visible_y)
16225 {
16226 dvpos = (it.vpos
16227 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16228 current_matrix));
16229 dy = it.current_y - first_unchanged_at_end_row->y;
16230 run.current_y = first_unchanged_at_end_row->y;
16231 run.desired_y = run.current_y + dy;
16232 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16233 }
16234 else
16235 {
16236 delta = delta_bytes = dvpos = dy
16237 = run.current_y = run.desired_y = run.height = 0;
16238 first_unchanged_at_end_row = NULL;
16239 }
16240 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16241
16242
16243 /* Find the cursor if not already found. We have to decide whether
16244 PT will appear on this window (it sometimes doesn't, but this is
16245 not a very frequent case.) This decision has to be made before
16246 the current matrix is altered. A value of cursor.vpos < 0 means
16247 that PT is either in one of the lines beginning at
16248 first_unchanged_at_end_row or below the window. Don't care for
16249 lines that might be displayed later at the window end; as
16250 mentioned, this is not a frequent case. */
16251 if (w->cursor.vpos < 0)
16252 {
16253 /* Cursor in unchanged rows at the top? */
16254 if (PT < CHARPOS (start_pos)
16255 && last_unchanged_at_beg_row)
16256 {
16257 row = row_containing_pos (w, PT,
16258 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16259 last_unchanged_at_beg_row + 1, 0);
16260 if (row)
16261 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16262 }
16263
16264 /* Start from first_unchanged_at_end_row looking for PT. */
16265 else if (first_unchanged_at_end_row)
16266 {
16267 row = row_containing_pos (w, PT - delta,
16268 first_unchanged_at_end_row, NULL, 0);
16269 if (row)
16270 set_cursor_from_row (w, row, w->current_matrix, delta,
16271 delta_bytes, dy, dvpos);
16272 }
16273
16274 /* Give up if cursor was not found. */
16275 if (w->cursor.vpos < 0)
16276 {
16277 clear_glyph_matrix (w->desired_matrix);
16278 return -1;
16279 }
16280 }
16281
16282 /* Don't let the cursor end in the scroll margins. */
16283 {
16284 int this_scroll_margin, cursor_height;
16285
16286 this_scroll_margin = max (0, scroll_margin);
16287 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16288 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16289 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16290
16291 if ((w->cursor.y < this_scroll_margin
16292 && CHARPOS (start) > BEGV)
16293 /* Old redisplay didn't take scroll margin into account at the bottom,
16294 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16295 || (w->cursor.y + (make_cursor_line_fully_visible_p
16296 ? cursor_height + this_scroll_margin
16297 : 1)) > it.last_visible_y)
16298 {
16299 w->cursor.vpos = -1;
16300 clear_glyph_matrix (w->desired_matrix);
16301 return -1;
16302 }
16303 }
16304
16305 /* Scroll the display. Do it before changing the current matrix so
16306 that xterm.c doesn't get confused about where the cursor glyph is
16307 found. */
16308 if (dy && run.height)
16309 {
16310 update_begin (f);
16311
16312 if (FRAME_WINDOW_P (f))
16313 {
16314 FRAME_RIF (f)->update_window_begin_hook (w);
16315 FRAME_RIF (f)->clear_window_mouse_face (w);
16316 FRAME_RIF (f)->scroll_run_hook (w, &run);
16317 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16318 }
16319 else
16320 {
16321 /* Terminal frame. In this case, dvpos gives the number of
16322 lines to scroll by; dvpos < 0 means scroll up. */
16323 int from_vpos
16324 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16325 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16326 int end = (WINDOW_TOP_EDGE_LINE (w)
16327 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16328 + window_internal_height (w));
16329
16330 #if defined (HAVE_GPM) || defined (MSDOS)
16331 x_clear_window_mouse_face (w);
16332 #endif
16333 /* Perform the operation on the screen. */
16334 if (dvpos > 0)
16335 {
16336 /* Scroll last_unchanged_at_beg_row to the end of the
16337 window down dvpos lines. */
16338 set_terminal_window (f, end);
16339
16340 /* On dumb terminals delete dvpos lines at the end
16341 before inserting dvpos empty lines. */
16342 if (!FRAME_SCROLL_REGION_OK (f))
16343 ins_del_lines (f, end - dvpos, -dvpos);
16344
16345 /* Insert dvpos empty lines in front of
16346 last_unchanged_at_beg_row. */
16347 ins_del_lines (f, from, dvpos);
16348 }
16349 else if (dvpos < 0)
16350 {
16351 /* Scroll up last_unchanged_at_beg_vpos to the end of
16352 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16353 set_terminal_window (f, end);
16354
16355 /* Delete dvpos lines in front of
16356 last_unchanged_at_beg_vpos. ins_del_lines will set
16357 the cursor to the given vpos and emit |dvpos| delete
16358 line sequences. */
16359 ins_del_lines (f, from + dvpos, dvpos);
16360
16361 /* On a dumb terminal insert dvpos empty lines at the
16362 end. */
16363 if (!FRAME_SCROLL_REGION_OK (f))
16364 ins_del_lines (f, end + dvpos, -dvpos);
16365 }
16366
16367 set_terminal_window (f, 0);
16368 }
16369
16370 update_end (f);
16371 }
16372
16373 /* Shift reused rows of the current matrix to the right position.
16374 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16375 text. */
16376 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16377 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16378 if (dvpos < 0)
16379 {
16380 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16381 bottom_vpos, dvpos);
16382 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16383 bottom_vpos, 0);
16384 }
16385 else if (dvpos > 0)
16386 {
16387 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16388 bottom_vpos, dvpos);
16389 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16390 first_unchanged_at_end_vpos + dvpos, 0);
16391 }
16392
16393 /* For frame-based redisplay, make sure that current frame and window
16394 matrix are in sync with respect to glyph memory. */
16395 if (!FRAME_WINDOW_P (f))
16396 sync_frame_with_window_matrix_rows (w);
16397
16398 /* Adjust buffer positions in reused rows. */
16399 if (delta || delta_bytes)
16400 increment_matrix_positions (current_matrix,
16401 first_unchanged_at_end_vpos + dvpos,
16402 bottom_vpos, delta, delta_bytes);
16403
16404 /* Adjust Y positions. */
16405 if (dy)
16406 shift_glyph_matrix (w, current_matrix,
16407 first_unchanged_at_end_vpos + dvpos,
16408 bottom_vpos, dy);
16409
16410 if (first_unchanged_at_end_row)
16411 {
16412 first_unchanged_at_end_row += dvpos;
16413 if (first_unchanged_at_end_row->y >= it.last_visible_y
16414 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16415 first_unchanged_at_end_row = NULL;
16416 }
16417
16418 /* If scrolling up, there may be some lines to display at the end of
16419 the window. */
16420 last_text_row_at_end = NULL;
16421 if (dy < 0)
16422 {
16423 /* Scrolling up can leave for example a partially visible line
16424 at the end of the window to be redisplayed. */
16425 /* Set last_row to the glyph row in the current matrix where the
16426 window end line is found. It has been moved up or down in
16427 the matrix by dvpos. */
16428 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16429 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16430
16431 /* If last_row is the window end line, it should display text. */
16432 xassert (last_row->displays_text_p);
16433
16434 /* If window end line was partially visible before, begin
16435 displaying at that line. Otherwise begin displaying with the
16436 line following it. */
16437 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16438 {
16439 init_to_row_start (&it, w, last_row);
16440 it.vpos = last_vpos;
16441 it.current_y = last_row->y;
16442 }
16443 else
16444 {
16445 init_to_row_end (&it, w, last_row);
16446 it.vpos = 1 + last_vpos;
16447 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16448 ++last_row;
16449 }
16450
16451 /* We may start in a continuation line. If so, we have to
16452 get the right continuation_lines_width and current_x. */
16453 it.continuation_lines_width = last_row->continuation_lines_width;
16454 it.hpos = it.current_x = 0;
16455
16456 /* Display the rest of the lines at the window end. */
16457 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16458 while (it.current_y < it.last_visible_y
16459 && !fonts_changed_p)
16460 {
16461 /* Is it always sure that the display agrees with lines in
16462 the current matrix? I don't think so, so we mark rows
16463 displayed invalid in the current matrix by setting their
16464 enabled_p flag to zero. */
16465 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16466 if (display_line (&it))
16467 last_text_row_at_end = it.glyph_row - 1;
16468 }
16469 }
16470
16471 /* Update window_end_pos and window_end_vpos. */
16472 if (first_unchanged_at_end_row
16473 && !last_text_row_at_end)
16474 {
16475 /* Window end line if one of the preserved rows from the current
16476 matrix. Set row to the last row displaying text in current
16477 matrix starting at first_unchanged_at_end_row, after
16478 scrolling. */
16479 xassert (first_unchanged_at_end_row->displays_text_p);
16480 row = find_last_row_displaying_text (w->current_matrix, &it,
16481 first_unchanged_at_end_row);
16482 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16483
16484 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16485 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16486 w->window_end_vpos
16487 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16488 xassert (w->window_end_bytepos >= 0);
16489 IF_DEBUG (debug_method_add (w, "A"));
16490 }
16491 else if (last_text_row_at_end)
16492 {
16493 w->window_end_pos
16494 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16495 w->window_end_bytepos
16496 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16497 w->window_end_vpos
16498 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16499 xassert (w->window_end_bytepos >= 0);
16500 IF_DEBUG (debug_method_add (w, "B"));
16501 }
16502 else if (last_text_row)
16503 {
16504 /* We have displayed either to the end of the window or at the
16505 end of the window, i.e. the last row with text is to be found
16506 in the desired matrix. */
16507 w->window_end_pos
16508 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16509 w->window_end_bytepos
16510 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16511 w->window_end_vpos
16512 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16513 xassert (w->window_end_bytepos >= 0);
16514 }
16515 else if (first_unchanged_at_end_row == NULL
16516 && last_text_row == NULL
16517 && last_text_row_at_end == NULL)
16518 {
16519 /* Displayed to end of window, but no line containing text was
16520 displayed. Lines were deleted at the end of the window. */
16521 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16522 int vpos = XFASTINT (w->window_end_vpos);
16523 struct glyph_row *current_row = current_matrix->rows + vpos;
16524 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16525
16526 for (row = NULL;
16527 row == NULL && vpos >= first_vpos;
16528 --vpos, --current_row, --desired_row)
16529 {
16530 if (desired_row->enabled_p)
16531 {
16532 if (desired_row->displays_text_p)
16533 row = desired_row;
16534 }
16535 else if (current_row->displays_text_p)
16536 row = current_row;
16537 }
16538
16539 xassert (row != NULL);
16540 w->window_end_vpos = make_number (vpos + 1);
16541 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16542 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16543 xassert (w->window_end_bytepos >= 0);
16544 IF_DEBUG (debug_method_add (w, "C"));
16545 }
16546 else
16547 abort ();
16548
16549 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16550 debug_end_vpos = XFASTINT (w->window_end_vpos));
16551
16552 /* Record that display has not been completed. */
16553 w->window_end_valid = Qnil;
16554 w->desired_matrix->no_scrolling_p = 1;
16555 return 3;
16556
16557 #undef GIVE_UP
16558 }
16559
16560
16561 \f
16562 /***********************************************************************
16563 More debugging support
16564 ***********************************************************************/
16565
16566 #if GLYPH_DEBUG
16567
16568 void dump_glyph_row (struct glyph_row *, int, int);
16569 void dump_glyph_matrix (struct glyph_matrix *, int);
16570 void dump_glyph (struct glyph_row *, struct glyph *, int);
16571
16572
16573 /* Dump the contents of glyph matrix MATRIX on stderr.
16574
16575 GLYPHS 0 means don't show glyph contents.
16576 GLYPHS 1 means show glyphs in short form
16577 GLYPHS > 1 means show glyphs in long form. */
16578
16579 void
16580 dump_glyph_matrix (matrix, glyphs)
16581 struct glyph_matrix *matrix;
16582 int glyphs;
16583 {
16584 int i;
16585 for (i = 0; i < matrix->nrows; ++i)
16586 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16587 }
16588
16589
16590 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16591 the glyph row and area where the glyph comes from. */
16592
16593 void
16594 dump_glyph (row, glyph, area)
16595 struct glyph_row *row;
16596 struct glyph *glyph;
16597 int area;
16598 {
16599 if (glyph->type == CHAR_GLYPH)
16600 {
16601 fprintf (stderr,
16602 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16603 glyph - row->glyphs[TEXT_AREA],
16604 'C',
16605 glyph->charpos,
16606 (BUFFERP (glyph->object)
16607 ? 'B'
16608 : (STRINGP (glyph->object)
16609 ? 'S'
16610 : '-')),
16611 glyph->pixel_width,
16612 glyph->u.ch,
16613 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16614 ? glyph->u.ch
16615 : '.'),
16616 glyph->face_id,
16617 glyph->left_box_line_p,
16618 glyph->right_box_line_p);
16619 }
16620 else if (glyph->type == STRETCH_GLYPH)
16621 {
16622 fprintf (stderr,
16623 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16624 glyph - row->glyphs[TEXT_AREA],
16625 'S',
16626 glyph->charpos,
16627 (BUFFERP (glyph->object)
16628 ? 'B'
16629 : (STRINGP (glyph->object)
16630 ? 'S'
16631 : '-')),
16632 glyph->pixel_width,
16633 0,
16634 '.',
16635 glyph->face_id,
16636 glyph->left_box_line_p,
16637 glyph->right_box_line_p);
16638 }
16639 else if (glyph->type == IMAGE_GLYPH)
16640 {
16641 fprintf (stderr,
16642 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16643 glyph - row->glyphs[TEXT_AREA],
16644 'I',
16645 glyph->charpos,
16646 (BUFFERP (glyph->object)
16647 ? 'B'
16648 : (STRINGP (glyph->object)
16649 ? 'S'
16650 : '-')),
16651 glyph->pixel_width,
16652 glyph->u.img_id,
16653 '.',
16654 glyph->face_id,
16655 glyph->left_box_line_p,
16656 glyph->right_box_line_p);
16657 }
16658 else if (glyph->type == COMPOSITE_GLYPH)
16659 {
16660 fprintf (stderr,
16661 " %5d %4c %6d %c %3d 0x%05x",
16662 glyph - row->glyphs[TEXT_AREA],
16663 '+',
16664 glyph->charpos,
16665 (BUFFERP (glyph->object)
16666 ? 'B'
16667 : (STRINGP (glyph->object)
16668 ? 'S'
16669 : '-')),
16670 glyph->pixel_width,
16671 glyph->u.cmp.id);
16672 if (glyph->u.cmp.automatic)
16673 fprintf (stderr,
16674 "[%d-%d]",
16675 glyph->slice.cmp.from, glyph->slice.cmp.to);
16676 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16677 glyph->face_id,
16678 glyph->left_box_line_p,
16679 glyph->right_box_line_p);
16680 }
16681 }
16682
16683
16684 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16685 GLYPHS 0 means don't show glyph contents.
16686 GLYPHS 1 means show glyphs in short form
16687 GLYPHS > 1 means show glyphs in long form. */
16688
16689 void
16690 dump_glyph_row (row, vpos, glyphs)
16691 struct glyph_row *row;
16692 int vpos, glyphs;
16693 {
16694 if (glyphs != 1)
16695 {
16696 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16697 fprintf (stderr, "======================================================================\n");
16698
16699 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16700 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16701 vpos,
16702 MATRIX_ROW_START_CHARPOS (row),
16703 MATRIX_ROW_END_CHARPOS (row),
16704 row->used[TEXT_AREA],
16705 row->contains_overlapping_glyphs_p,
16706 row->enabled_p,
16707 row->truncated_on_left_p,
16708 row->truncated_on_right_p,
16709 row->continued_p,
16710 MATRIX_ROW_CONTINUATION_LINE_P (row),
16711 row->displays_text_p,
16712 row->ends_at_zv_p,
16713 row->fill_line_p,
16714 row->ends_in_middle_of_char_p,
16715 row->starts_in_middle_of_char_p,
16716 row->mouse_face_p,
16717 row->x,
16718 row->y,
16719 row->pixel_width,
16720 row->height,
16721 row->visible_height,
16722 row->ascent,
16723 row->phys_ascent);
16724 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16725 row->end.overlay_string_index,
16726 row->continuation_lines_width);
16727 fprintf (stderr, "%9d %5d\n",
16728 CHARPOS (row->start.string_pos),
16729 CHARPOS (row->end.string_pos));
16730 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16731 row->end.dpvec_index);
16732 }
16733
16734 if (glyphs > 1)
16735 {
16736 int area;
16737
16738 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16739 {
16740 struct glyph *glyph = row->glyphs[area];
16741 struct glyph *glyph_end = glyph + row->used[area];
16742
16743 /* Glyph for a line end in text. */
16744 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16745 ++glyph_end;
16746
16747 if (glyph < glyph_end)
16748 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16749
16750 for (; glyph < glyph_end; ++glyph)
16751 dump_glyph (row, glyph, area);
16752 }
16753 }
16754 else if (glyphs == 1)
16755 {
16756 int area;
16757
16758 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16759 {
16760 char *s = (char *) alloca (row->used[area] + 1);
16761 int i;
16762
16763 for (i = 0; i < row->used[area]; ++i)
16764 {
16765 struct glyph *glyph = row->glyphs[area] + i;
16766 if (glyph->type == CHAR_GLYPH
16767 && glyph->u.ch < 0x80
16768 && glyph->u.ch >= ' ')
16769 s[i] = glyph->u.ch;
16770 else
16771 s[i] = '.';
16772 }
16773
16774 s[i] = '\0';
16775 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16776 }
16777 }
16778 }
16779
16780
16781 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16782 Sdump_glyph_matrix, 0, 1, "p",
16783 doc: /* Dump the current matrix of the selected window to stderr.
16784 Shows contents of glyph row structures. With non-nil
16785 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16786 glyphs in short form, otherwise show glyphs in long form. */)
16787 (Lisp_Object glyphs)
16788 {
16789 struct window *w = XWINDOW (selected_window);
16790 struct buffer *buffer = XBUFFER (w->buffer);
16791
16792 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16793 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16794 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16795 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16796 fprintf (stderr, "=============================================\n");
16797 dump_glyph_matrix (w->current_matrix,
16798 NILP (glyphs) ? 0 : XINT (glyphs));
16799 return Qnil;
16800 }
16801
16802
16803 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16804 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16805 (void)
16806 {
16807 struct frame *f = XFRAME (selected_frame);
16808 dump_glyph_matrix (f->current_matrix, 1);
16809 return Qnil;
16810 }
16811
16812
16813 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16814 doc: /* Dump glyph row ROW to stderr.
16815 GLYPH 0 means don't dump glyphs.
16816 GLYPH 1 means dump glyphs in short form.
16817 GLYPH > 1 or omitted means dump glyphs in long form. */)
16818 (Lisp_Object row, Lisp_Object glyphs)
16819 {
16820 struct glyph_matrix *matrix;
16821 int vpos;
16822
16823 CHECK_NUMBER (row);
16824 matrix = XWINDOW (selected_window)->current_matrix;
16825 vpos = XINT (row);
16826 if (vpos >= 0 && vpos < matrix->nrows)
16827 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16828 vpos,
16829 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16830 return Qnil;
16831 }
16832
16833
16834 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16835 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16836 GLYPH 0 means don't dump glyphs.
16837 GLYPH 1 means dump glyphs in short form.
16838 GLYPH > 1 or omitted means dump glyphs in long form. */)
16839 (Lisp_Object row, Lisp_Object glyphs)
16840 {
16841 struct frame *sf = SELECTED_FRAME ();
16842 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16843 int vpos;
16844
16845 CHECK_NUMBER (row);
16846 vpos = XINT (row);
16847 if (vpos >= 0 && vpos < m->nrows)
16848 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16849 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16850 return Qnil;
16851 }
16852
16853
16854 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16855 doc: /* Toggle tracing of redisplay.
16856 With ARG, turn tracing on if and only if ARG is positive. */)
16857 (Lisp_Object arg)
16858 {
16859 if (NILP (arg))
16860 trace_redisplay_p = !trace_redisplay_p;
16861 else
16862 {
16863 arg = Fprefix_numeric_value (arg);
16864 trace_redisplay_p = XINT (arg) > 0;
16865 }
16866
16867 return Qnil;
16868 }
16869
16870
16871 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16872 doc: /* Like `format', but print result to stderr.
16873 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16874 (size_t nargs, Lisp_Object *args)
16875 {
16876 Lisp_Object s = Fformat (nargs, args);
16877 fprintf (stderr, "%s", SDATA (s));
16878 return Qnil;
16879 }
16880
16881 #endif /* GLYPH_DEBUG */
16882
16883
16884 \f
16885 /***********************************************************************
16886 Building Desired Matrix Rows
16887 ***********************************************************************/
16888
16889 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16890 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16891
16892 static struct glyph_row *
16893 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16894 {
16895 struct frame *f = XFRAME (WINDOW_FRAME (w));
16896 struct buffer *buffer = XBUFFER (w->buffer);
16897 struct buffer *old = current_buffer;
16898 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16899 int arrow_len = SCHARS (overlay_arrow_string);
16900 const unsigned char *arrow_end = arrow_string + arrow_len;
16901 const unsigned char *p;
16902 struct it it;
16903 int multibyte_p;
16904 int n_glyphs_before;
16905
16906 set_buffer_temp (buffer);
16907 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16908 it.glyph_row->used[TEXT_AREA] = 0;
16909 SET_TEXT_POS (it.position, 0, 0);
16910
16911 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
16912 p = arrow_string;
16913 while (p < arrow_end)
16914 {
16915 Lisp_Object face, ilisp;
16916
16917 /* Get the next character. */
16918 if (multibyte_p)
16919 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16920 else
16921 {
16922 it.c = it.char_to_display = *p, it.len = 1;
16923 if (! ASCII_CHAR_P (it.c))
16924 it.char_to_display = BYTE8_TO_CHAR (it.c);
16925 }
16926 p += it.len;
16927
16928 /* Get its face. */
16929 ilisp = make_number (p - arrow_string);
16930 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16931 it.face_id = compute_char_face (f, it.char_to_display, face);
16932
16933 /* Compute its width, get its glyphs. */
16934 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16935 SET_TEXT_POS (it.position, -1, -1);
16936 PRODUCE_GLYPHS (&it);
16937
16938 /* If this character doesn't fit any more in the line, we have
16939 to remove some glyphs. */
16940 if (it.current_x > it.last_visible_x)
16941 {
16942 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16943 break;
16944 }
16945 }
16946
16947 set_buffer_temp (old);
16948 return it.glyph_row;
16949 }
16950
16951
16952 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16953 glyphs are only inserted for terminal frames since we can't really
16954 win with truncation glyphs when partially visible glyphs are
16955 involved. Which glyphs to insert is determined by
16956 produce_special_glyphs. */
16957
16958 static void
16959 insert_left_trunc_glyphs (struct it *it)
16960 {
16961 struct it truncate_it;
16962 struct glyph *from, *end, *to, *toend;
16963
16964 xassert (!FRAME_WINDOW_P (it->f));
16965
16966 /* Get the truncation glyphs. */
16967 truncate_it = *it;
16968 truncate_it.current_x = 0;
16969 truncate_it.face_id = DEFAULT_FACE_ID;
16970 truncate_it.glyph_row = &scratch_glyph_row;
16971 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16972 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16973 truncate_it.object = make_number (0);
16974 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16975
16976 /* Overwrite glyphs from IT with truncation glyphs. */
16977 if (!it->glyph_row->reversed_p)
16978 {
16979 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16980 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16981 to = it->glyph_row->glyphs[TEXT_AREA];
16982 toend = to + it->glyph_row->used[TEXT_AREA];
16983
16984 while (from < end)
16985 *to++ = *from++;
16986
16987 /* There may be padding glyphs left over. Overwrite them too. */
16988 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16989 {
16990 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16991 while (from < end)
16992 *to++ = *from++;
16993 }
16994
16995 if (to > toend)
16996 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16997 }
16998 else
16999 {
17000 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17001 that back to front. */
17002 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17003 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17004 toend = it->glyph_row->glyphs[TEXT_AREA];
17005 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17006
17007 while (from >= end && to >= toend)
17008 *to-- = *from--;
17009 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17010 {
17011 from =
17012 truncate_it.glyph_row->glyphs[TEXT_AREA]
17013 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17014 while (from >= end && to >= toend)
17015 *to-- = *from--;
17016 }
17017 if (from >= end)
17018 {
17019 /* Need to free some room before prepending additional
17020 glyphs. */
17021 int move_by = from - end + 1;
17022 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17023 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17024
17025 for ( ; g >= g0; g--)
17026 g[move_by] = *g;
17027 while (from >= end)
17028 *to-- = *from--;
17029 it->glyph_row->used[TEXT_AREA] += move_by;
17030 }
17031 }
17032 }
17033
17034
17035 /* Compute the pixel height and width of IT->glyph_row.
17036
17037 Most of the time, ascent and height of a display line will be equal
17038 to the max_ascent and max_height values of the display iterator
17039 structure. This is not the case if
17040
17041 1. We hit ZV without displaying anything. In this case, max_ascent
17042 and max_height will be zero.
17043
17044 2. We have some glyphs that don't contribute to the line height.
17045 (The glyph row flag contributes_to_line_height_p is for future
17046 pixmap extensions).
17047
17048 The first case is easily covered by using default values because in
17049 these cases, the line height does not really matter, except that it
17050 must not be zero. */
17051
17052 static void
17053 compute_line_metrics (struct it *it)
17054 {
17055 struct glyph_row *row = it->glyph_row;
17056
17057 if (FRAME_WINDOW_P (it->f))
17058 {
17059 int i, min_y, max_y;
17060
17061 /* The line may consist of one space only, that was added to
17062 place the cursor on it. If so, the row's height hasn't been
17063 computed yet. */
17064 if (row->height == 0)
17065 {
17066 if (it->max_ascent + it->max_descent == 0)
17067 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17068 row->ascent = it->max_ascent;
17069 row->height = it->max_ascent + it->max_descent;
17070 row->phys_ascent = it->max_phys_ascent;
17071 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17072 row->extra_line_spacing = it->max_extra_line_spacing;
17073 }
17074
17075 /* Compute the width of this line. */
17076 row->pixel_width = row->x;
17077 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17078 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17079
17080 xassert (row->pixel_width >= 0);
17081 xassert (row->ascent >= 0 && row->height > 0);
17082
17083 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17084 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17085
17086 /* If first line's physical ascent is larger than its logical
17087 ascent, use the physical ascent, and make the row taller.
17088 This makes accented characters fully visible. */
17089 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17090 && row->phys_ascent > row->ascent)
17091 {
17092 row->height += row->phys_ascent - row->ascent;
17093 row->ascent = row->phys_ascent;
17094 }
17095
17096 /* Compute how much of the line is visible. */
17097 row->visible_height = row->height;
17098
17099 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17100 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17101
17102 if (row->y < min_y)
17103 row->visible_height -= min_y - row->y;
17104 if (row->y + row->height > max_y)
17105 row->visible_height -= row->y + row->height - max_y;
17106 }
17107 else
17108 {
17109 row->pixel_width = row->used[TEXT_AREA];
17110 if (row->continued_p)
17111 row->pixel_width -= it->continuation_pixel_width;
17112 else if (row->truncated_on_right_p)
17113 row->pixel_width -= it->truncation_pixel_width;
17114 row->ascent = row->phys_ascent = 0;
17115 row->height = row->phys_height = row->visible_height = 1;
17116 row->extra_line_spacing = 0;
17117 }
17118
17119 /* Compute a hash code for this row. */
17120 {
17121 int area, i;
17122 row->hash = 0;
17123 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17124 for (i = 0; i < row->used[area]; ++i)
17125 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17126 + row->glyphs[area][i].u.val
17127 + row->glyphs[area][i].face_id
17128 + row->glyphs[area][i].padding_p
17129 + (row->glyphs[area][i].type << 2));
17130 }
17131
17132 it->max_ascent = it->max_descent = 0;
17133 it->max_phys_ascent = it->max_phys_descent = 0;
17134 }
17135
17136
17137 /* Append one space to the glyph row of iterator IT if doing a
17138 window-based redisplay. The space has the same face as
17139 IT->face_id. Value is non-zero if a space was added.
17140
17141 This function is called to make sure that there is always one glyph
17142 at the end of a glyph row that the cursor can be set on under
17143 window-systems. (If there weren't such a glyph we would not know
17144 how wide and tall a box cursor should be displayed).
17145
17146 At the same time this space let's a nicely handle clearing to the
17147 end of the line if the row ends in italic text. */
17148
17149 static int
17150 append_space_for_newline (struct it *it, int default_face_p)
17151 {
17152 if (FRAME_WINDOW_P (it->f))
17153 {
17154 int n = it->glyph_row->used[TEXT_AREA];
17155
17156 if (it->glyph_row->glyphs[TEXT_AREA] + n
17157 < it->glyph_row->glyphs[1 + TEXT_AREA])
17158 {
17159 /* Save some values that must not be changed.
17160 Must save IT->c and IT->len because otherwise
17161 ITERATOR_AT_END_P wouldn't work anymore after
17162 append_space_for_newline has been called. */
17163 enum display_element_type saved_what = it->what;
17164 int saved_c = it->c, saved_len = it->len;
17165 int saved_char_to_display = it->char_to_display;
17166 int saved_x = it->current_x;
17167 int saved_face_id = it->face_id;
17168 struct text_pos saved_pos;
17169 Lisp_Object saved_object;
17170 struct face *face;
17171
17172 saved_object = it->object;
17173 saved_pos = it->position;
17174
17175 it->what = IT_CHARACTER;
17176 memset (&it->position, 0, sizeof it->position);
17177 it->object = make_number (0);
17178 it->c = it->char_to_display = ' ';
17179 it->len = 1;
17180
17181 if (default_face_p)
17182 it->face_id = DEFAULT_FACE_ID;
17183 else if (it->face_before_selective_p)
17184 it->face_id = it->saved_face_id;
17185 face = FACE_FROM_ID (it->f, it->face_id);
17186 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17187
17188 PRODUCE_GLYPHS (it);
17189
17190 it->override_ascent = -1;
17191 it->constrain_row_ascent_descent_p = 0;
17192 it->current_x = saved_x;
17193 it->object = saved_object;
17194 it->position = saved_pos;
17195 it->what = saved_what;
17196 it->face_id = saved_face_id;
17197 it->len = saved_len;
17198 it->c = saved_c;
17199 it->char_to_display = saved_char_to_display;
17200 return 1;
17201 }
17202 }
17203
17204 return 0;
17205 }
17206
17207
17208 /* Extend the face of the last glyph in the text area of IT->glyph_row
17209 to the end of the display line. Called from display_line. If the
17210 glyph row is empty, add a space glyph to it so that we know the
17211 face to draw. Set the glyph row flag fill_line_p. If the glyph
17212 row is R2L, prepend a stretch glyph to cover the empty space to the
17213 left of the leftmost glyph. */
17214
17215 static void
17216 extend_face_to_end_of_line (struct it *it)
17217 {
17218 struct face *face;
17219 struct frame *f = it->f;
17220
17221 /* If line is already filled, do nothing. Non window-system frames
17222 get a grace of one more ``pixel'' because their characters are
17223 1-``pixel'' wide, so they hit the equality too early. This grace
17224 is needed only for R2L rows that are not continued, to produce
17225 one extra blank where we could display the cursor. */
17226 if (it->current_x >= it->last_visible_x
17227 + (!FRAME_WINDOW_P (f)
17228 && it->glyph_row->reversed_p
17229 && !it->glyph_row->continued_p))
17230 return;
17231
17232 /* Face extension extends the background and box of IT->face_id
17233 to the end of the line. If the background equals the background
17234 of the frame, we don't have to do anything. */
17235 if (it->face_before_selective_p)
17236 face = FACE_FROM_ID (f, it->saved_face_id);
17237 else
17238 face = FACE_FROM_ID (f, it->face_id);
17239
17240 if (FRAME_WINDOW_P (f)
17241 && it->glyph_row->displays_text_p
17242 && face->box == FACE_NO_BOX
17243 && face->background == FRAME_BACKGROUND_PIXEL (f)
17244 && !face->stipple
17245 && !it->glyph_row->reversed_p)
17246 return;
17247
17248 /* Set the glyph row flag indicating that the face of the last glyph
17249 in the text area has to be drawn to the end of the text area. */
17250 it->glyph_row->fill_line_p = 1;
17251
17252 /* If current character of IT is not ASCII, make sure we have the
17253 ASCII face. This will be automatically undone the next time
17254 get_next_display_element returns a multibyte character. Note
17255 that the character will always be single byte in unibyte
17256 text. */
17257 if (!ASCII_CHAR_P (it->c))
17258 {
17259 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17260 }
17261
17262 if (FRAME_WINDOW_P (f))
17263 {
17264 /* If the row is empty, add a space with the current face of IT,
17265 so that we know which face to draw. */
17266 if (it->glyph_row->used[TEXT_AREA] == 0)
17267 {
17268 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17269 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17270 it->glyph_row->used[TEXT_AREA] = 1;
17271 }
17272 #ifdef HAVE_WINDOW_SYSTEM
17273 if (it->glyph_row->reversed_p)
17274 {
17275 /* Prepend a stretch glyph to the row, such that the
17276 rightmost glyph will be drawn flushed all the way to the
17277 right margin of the window. The stretch glyph that will
17278 occupy the empty space, if any, to the left of the
17279 glyphs. */
17280 struct font *font = face->font ? face->font : FRAME_FONT (f);
17281 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17282 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17283 struct glyph *g;
17284 int row_width, stretch_ascent, stretch_width;
17285 struct text_pos saved_pos;
17286 int saved_face_id, saved_avoid_cursor;
17287
17288 for (row_width = 0, g = row_start; g < row_end; g++)
17289 row_width += g->pixel_width;
17290 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17291 if (stretch_width > 0)
17292 {
17293 stretch_ascent =
17294 (((it->ascent + it->descent)
17295 * FONT_BASE (font)) / FONT_HEIGHT (font));
17296 saved_pos = it->position;
17297 memset (&it->position, 0, sizeof it->position);
17298 saved_avoid_cursor = it->avoid_cursor_p;
17299 it->avoid_cursor_p = 1;
17300 saved_face_id = it->face_id;
17301 /* The last row's stretch glyph should get the default
17302 face, to avoid painting the rest of the window with
17303 the region face, if the region ends at ZV. */
17304 if (it->glyph_row->ends_at_zv_p)
17305 it->face_id = DEFAULT_FACE_ID;
17306 else
17307 it->face_id = face->id;
17308 append_stretch_glyph (it, make_number (0), stretch_width,
17309 it->ascent + it->descent, stretch_ascent);
17310 it->position = saved_pos;
17311 it->avoid_cursor_p = saved_avoid_cursor;
17312 it->face_id = saved_face_id;
17313 }
17314 }
17315 #endif /* HAVE_WINDOW_SYSTEM */
17316 }
17317 else
17318 {
17319 /* Save some values that must not be changed. */
17320 int saved_x = it->current_x;
17321 struct text_pos saved_pos;
17322 Lisp_Object saved_object;
17323 enum display_element_type saved_what = it->what;
17324 int saved_face_id = it->face_id;
17325
17326 saved_object = it->object;
17327 saved_pos = it->position;
17328
17329 it->what = IT_CHARACTER;
17330 memset (&it->position, 0, sizeof it->position);
17331 it->object = make_number (0);
17332 it->c = it->char_to_display = ' ';
17333 it->len = 1;
17334 /* The last row's blank glyphs should get the default face, to
17335 avoid painting the rest of the window with the region face,
17336 if the region ends at ZV. */
17337 if (it->glyph_row->ends_at_zv_p)
17338 it->face_id = DEFAULT_FACE_ID;
17339 else
17340 it->face_id = face->id;
17341
17342 PRODUCE_GLYPHS (it);
17343
17344 while (it->current_x <= it->last_visible_x)
17345 PRODUCE_GLYPHS (it);
17346
17347 /* Don't count these blanks really. It would let us insert a left
17348 truncation glyph below and make us set the cursor on them, maybe. */
17349 it->current_x = saved_x;
17350 it->object = saved_object;
17351 it->position = saved_pos;
17352 it->what = saved_what;
17353 it->face_id = saved_face_id;
17354 }
17355 }
17356
17357
17358 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17359 trailing whitespace. */
17360
17361 static int
17362 trailing_whitespace_p (EMACS_INT charpos)
17363 {
17364 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17365 int c = 0;
17366
17367 while (bytepos < ZV_BYTE
17368 && (c = FETCH_CHAR (bytepos),
17369 c == ' ' || c == '\t'))
17370 ++bytepos;
17371
17372 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17373 {
17374 if (bytepos != PT_BYTE)
17375 return 1;
17376 }
17377 return 0;
17378 }
17379
17380
17381 /* Highlight trailing whitespace, if any, in ROW. */
17382
17383 static void
17384 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17385 {
17386 int used = row->used[TEXT_AREA];
17387
17388 if (used)
17389 {
17390 struct glyph *start = row->glyphs[TEXT_AREA];
17391 struct glyph *glyph = start + used - 1;
17392
17393 if (row->reversed_p)
17394 {
17395 /* Right-to-left rows need to be processed in the opposite
17396 direction, so swap the edge pointers. */
17397 glyph = start;
17398 start = row->glyphs[TEXT_AREA] + used - 1;
17399 }
17400
17401 /* Skip over glyphs inserted to display the cursor at the
17402 end of a line, for extending the face of the last glyph
17403 to the end of the line on terminals, and for truncation
17404 and continuation glyphs. */
17405 if (!row->reversed_p)
17406 {
17407 while (glyph >= start
17408 && glyph->type == CHAR_GLYPH
17409 && INTEGERP (glyph->object))
17410 --glyph;
17411 }
17412 else
17413 {
17414 while (glyph <= start
17415 && glyph->type == CHAR_GLYPH
17416 && INTEGERP (glyph->object))
17417 ++glyph;
17418 }
17419
17420 /* If last glyph is a space or stretch, and it's trailing
17421 whitespace, set the face of all trailing whitespace glyphs in
17422 IT->glyph_row to `trailing-whitespace'. */
17423 if ((row->reversed_p ? glyph <= start : glyph >= start)
17424 && BUFFERP (glyph->object)
17425 && (glyph->type == STRETCH_GLYPH
17426 || (glyph->type == CHAR_GLYPH
17427 && glyph->u.ch == ' '))
17428 && trailing_whitespace_p (glyph->charpos))
17429 {
17430 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17431 if (face_id < 0)
17432 return;
17433
17434 if (!row->reversed_p)
17435 {
17436 while (glyph >= start
17437 && BUFFERP (glyph->object)
17438 && (glyph->type == STRETCH_GLYPH
17439 || (glyph->type == CHAR_GLYPH
17440 && glyph->u.ch == ' ')))
17441 (glyph--)->face_id = face_id;
17442 }
17443 else
17444 {
17445 while (glyph <= start
17446 && BUFFERP (glyph->object)
17447 && (glyph->type == STRETCH_GLYPH
17448 || (glyph->type == CHAR_GLYPH
17449 && glyph->u.ch == ' ')))
17450 (glyph++)->face_id = face_id;
17451 }
17452 }
17453 }
17454 }
17455
17456
17457 /* Value is non-zero if glyph row ROW should be
17458 used to hold the cursor. */
17459
17460 static int
17461 cursor_row_p (struct glyph_row *row)
17462 {
17463 int result = 1;
17464
17465 if (PT == CHARPOS (row->end.pos))
17466 {
17467 /* Suppose the row ends on a string.
17468 Unless the row is continued, that means it ends on a newline
17469 in the string. If it's anything other than a display string
17470 (e.g. a before-string from an overlay), we don't want the
17471 cursor there. (This heuristic seems to give the optimal
17472 behavior for the various types of multi-line strings.) */
17473 if (CHARPOS (row->end.string_pos) >= 0)
17474 {
17475 if (row->continued_p)
17476 result = 1;
17477 else
17478 {
17479 /* Check for `display' property. */
17480 struct glyph *beg = row->glyphs[TEXT_AREA];
17481 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17482 struct glyph *glyph;
17483
17484 result = 0;
17485 for (glyph = end; glyph >= beg; --glyph)
17486 if (STRINGP (glyph->object))
17487 {
17488 Lisp_Object prop
17489 = Fget_char_property (make_number (PT),
17490 Qdisplay, Qnil);
17491 result =
17492 (!NILP (prop)
17493 && display_prop_string_p (prop, glyph->object));
17494 break;
17495 }
17496 }
17497 }
17498 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17499 {
17500 /* If the row ends in middle of a real character,
17501 and the line is continued, we want the cursor here.
17502 That's because CHARPOS (ROW->end.pos) would equal
17503 PT if PT is before the character. */
17504 if (!row->ends_in_ellipsis_p)
17505 result = row->continued_p;
17506 else
17507 /* If the row ends in an ellipsis, then
17508 CHARPOS (ROW->end.pos) will equal point after the
17509 invisible text. We want that position to be displayed
17510 after the ellipsis. */
17511 result = 0;
17512 }
17513 /* If the row ends at ZV, display the cursor at the end of that
17514 row instead of at the start of the row below. */
17515 else if (row->ends_at_zv_p)
17516 result = 1;
17517 else
17518 result = 0;
17519 }
17520
17521 return result;
17522 }
17523
17524 \f
17525
17526 /* Push the display property PROP so that it will be rendered at the
17527 current position in IT. Return 1 if PROP was successfully pushed,
17528 0 otherwise. */
17529
17530 static int
17531 push_display_prop (struct it *it, Lisp_Object prop)
17532 {
17533 push_it (it, NULL);
17534
17535 if (STRINGP (prop))
17536 {
17537 if (SCHARS (prop) == 0)
17538 {
17539 pop_it (it);
17540 return 0;
17541 }
17542
17543 it->string = prop;
17544 it->multibyte_p = STRING_MULTIBYTE (it->string);
17545 it->current.overlay_string_index = -1;
17546 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17547 it->end_charpos = it->string_nchars = SCHARS (it->string);
17548 it->method = GET_FROM_STRING;
17549 it->stop_charpos = 0;
17550 }
17551 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17552 {
17553 it->method = GET_FROM_STRETCH;
17554 it->object = prop;
17555 }
17556 #ifdef HAVE_WINDOW_SYSTEM
17557 else if (IMAGEP (prop))
17558 {
17559 it->what = IT_IMAGE;
17560 it->image_id = lookup_image (it->f, prop);
17561 it->method = GET_FROM_IMAGE;
17562 }
17563 #endif /* HAVE_WINDOW_SYSTEM */
17564 else
17565 {
17566 pop_it (it); /* bogus display property, give up */
17567 return 0;
17568 }
17569
17570 return 1;
17571 }
17572
17573 /* Return the character-property PROP at the current position in IT. */
17574
17575 static Lisp_Object
17576 get_it_property (struct it *it, Lisp_Object prop)
17577 {
17578 Lisp_Object position;
17579
17580 if (STRINGP (it->object))
17581 position = make_number (IT_STRING_CHARPOS (*it));
17582 else if (BUFFERP (it->object))
17583 position = make_number (IT_CHARPOS (*it));
17584 else
17585 return Qnil;
17586
17587 return Fget_char_property (position, prop, it->object);
17588 }
17589
17590 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17591
17592 static void
17593 handle_line_prefix (struct it *it)
17594 {
17595 Lisp_Object prefix;
17596 if (it->continuation_lines_width > 0)
17597 {
17598 prefix = get_it_property (it, Qwrap_prefix);
17599 if (NILP (prefix))
17600 prefix = Vwrap_prefix;
17601 }
17602 else
17603 {
17604 prefix = get_it_property (it, Qline_prefix);
17605 if (NILP (prefix))
17606 prefix = Vline_prefix;
17607 }
17608 if (! NILP (prefix) && push_display_prop (it, prefix))
17609 {
17610 /* If the prefix is wider than the window, and we try to wrap
17611 it, it would acquire its own wrap prefix, and so on till the
17612 iterator stack overflows. So, don't wrap the prefix. */
17613 it->line_wrap = TRUNCATE;
17614 it->avoid_cursor_p = 1;
17615 }
17616 }
17617
17618 \f
17619
17620 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17621 only for R2L lines from display_line and display_string, when they
17622 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
17623 the line/string needs to be continued on the next glyph row. */
17624 static void
17625 unproduce_glyphs (struct it *it, int n)
17626 {
17627 struct glyph *glyph, *end;
17628
17629 xassert (it->glyph_row);
17630 xassert (it->glyph_row->reversed_p);
17631 xassert (it->area == TEXT_AREA);
17632 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17633
17634 if (n > it->glyph_row->used[TEXT_AREA])
17635 n = it->glyph_row->used[TEXT_AREA];
17636 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17637 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17638 for ( ; glyph < end; glyph++)
17639 glyph[-n] = *glyph;
17640 }
17641
17642 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17643 and ROW->maxpos. */
17644 static void
17645 find_row_edges (struct it *it, struct glyph_row *row,
17646 EMACS_INT min_pos, EMACS_INT min_bpos,
17647 EMACS_INT max_pos, EMACS_INT max_bpos)
17648 {
17649 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17650 lines' rows is implemented for bidi-reordered rows. */
17651
17652 /* ROW->minpos is the value of min_pos, the minimal buffer position
17653 we have in ROW. */
17654 if (min_pos <= ZV)
17655 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17656 else
17657 /* We didn't find _any_ valid buffer positions in any of the
17658 glyphs, so we must trust the iterator's computed positions. */
17659 row->minpos = row->start.pos;
17660 if (max_pos <= 0)
17661 {
17662 max_pos = CHARPOS (it->current.pos);
17663 max_bpos = BYTEPOS (it->current.pos);
17664 }
17665
17666 /* Here are the various use-cases for ending the row, and the
17667 corresponding values for ROW->maxpos:
17668
17669 Line ends in a newline from buffer eol_pos + 1
17670 Line is continued from buffer max_pos + 1
17671 Line is truncated on right it->current.pos
17672 Line ends in a newline from string max_pos
17673 Line is continued from string max_pos
17674 Line is continued from display vector max_pos
17675 Line is entirely from a string min_pos == max_pos
17676 Line is entirely from a display vector min_pos == max_pos
17677 Line that ends at ZV ZV
17678
17679 If you discover other use-cases, please add them here as
17680 appropriate. */
17681 if (row->ends_at_zv_p)
17682 row->maxpos = it->current.pos;
17683 else if (row->used[TEXT_AREA])
17684 {
17685 if (row->ends_in_newline_from_string_p)
17686 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17687 else if (CHARPOS (it->eol_pos) > 0)
17688 SET_TEXT_POS (row->maxpos,
17689 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17690 else if (row->continued_p)
17691 {
17692 /* If max_pos is different from IT's current position, it
17693 means IT->method does not belong to the display element
17694 at max_pos. However, it also means that the display
17695 element at max_pos was displayed in its entirety on this
17696 line, which is equivalent to saying that the next line
17697 starts at the next buffer position. */
17698 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17699 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17700 else
17701 {
17702 INC_BOTH (max_pos, max_bpos);
17703 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17704 }
17705 }
17706 else if (row->truncated_on_right_p)
17707 /* display_line already called reseat_at_next_visible_line_start,
17708 which puts the iterator at the beginning of the next line, in
17709 the logical order. */
17710 row->maxpos = it->current.pos;
17711 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17712 /* A line that is entirely from a string/image/stretch... */
17713 row->maxpos = row->minpos;
17714 else
17715 abort ();
17716 }
17717 else
17718 row->maxpos = it->current.pos;
17719 }
17720
17721 /* Construct the glyph row IT->glyph_row in the desired matrix of
17722 IT->w from text at the current position of IT. See dispextern.h
17723 for an overview of struct it. Value is non-zero if
17724 IT->glyph_row displays text, as opposed to a line displaying ZV
17725 only. */
17726
17727 static int
17728 display_line (struct it *it)
17729 {
17730 struct glyph_row *row = it->glyph_row;
17731 Lisp_Object overlay_arrow_string;
17732 struct it wrap_it;
17733 int may_wrap = 0, wrap_x IF_LINT (= 0);
17734 int wrap_row_used = -1;
17735 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17736 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17737 int wrap_row_extra_line_spacing IF_LINT (= 0);
17738 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17739 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17740 int cvpos;
17741 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17742 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
17743
17744 /* We always start displaying at hpos zero even if hscrolled. */
17745 xassert (it->hpos == 0 && it->current_x == 0);
17746
17747 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17748 >= it->w->desired_matrix->nrows)
17749 {
17750 it->w->nrows_scale_factor++;
17751 fonts_changed_p = 1;
17752 return 0;
17753 }
17754
17755 /* Is IT->w showing the region? */
17756 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17757
17758 /* Clear the result glyph row and enable it. */
17759 prepare_desired_row (row);
17760
17761 row->y = it->current_y;
17762 row->start = it->start;
17763 row->continuation_lines_width = it->continuation_lines_width;
17764 row->displays_text_p = 1;
17765 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17766 it->starts_in_middle_of_char_p = 0;
17767
17768 /* Arrange the overlays nicely for our purposes. Usually, we call
17769 display_line on only one line at a time, in which case this
17770 can't really hurt too much, or we call it on lines which appear
17771 one after another in the buffer, in which case all calls to
17772 recenter_overlay_lists but the first will be pretty cheap. */
17773 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17774
17775 /* Move over display elements that are not visible because we are
17776 hscrolled. This may stop at an x-position < IT->first_visible_x
17777 if the first glyph is partially visible or if we hit a line end. */
17778 if (it->current_x < it->first_visible_x)
17779 {
17780 this_line_min_pos = row->start.pos;
17781 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17782 MOVE_TO_POS | MOVE_TO_X);
17783 /* Record the smallest positions seen while we moved over
17784 display elements that are not visible. This is needed by
17785 redisplay_internal for optimizing the case where the cursor
17786 stays inside the same line. The rest of this function only
17787 considers positions that are actually displayed, so
17788 RECORD_MAX_MIN_POS will not otherwise record positions that
17789 are hscrolled to the left of the left edge of the window. */
17790 min_pos = CHARPOS (this_line_min_pos);
17791 min_bpos = BYTEPOS (this_line_min_pos);
17792 }
17793 else
17794 {
17795 /* We only do this when not calling `move_it_in_display_line_to'
17796 above, because move_it_in_display_line_to calls
17797 handle_line_prefix itself. */
17798 handle_line_prefix (it);
17799 }
17800
17801 /* Get the initial row height. This is either the height of the
17802 text hscrolled, if there is any, or zero. */
17803 row->ascent = it->max_ascent;
17804 row->height = it->max_ascent + it->max_descent;
17805 row->phys_ascent = it->max_phys_ascent;
17806 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17807 row->extra_line_spacing = it->max_extra_line_spacing;
17808
17809 /* Utility macro to record max and min buffer positions seen until now. */
17810 #define RECORD_MAX_MIN_POS(IT) \
17811 do \
17812 { \
17813 if (IT_CHARPOS (*(IT)) < min_pos) \
17814 { \
17815 min_pos = IT_CHARPOS (*(IT)); \
17816 min_bpos = IT_BYTEPOS (*(IT)); \
17817 } \
17818 if (IT_CHARPOS (*(IT)) > max_pos) \
17819 { \
17820 max_pos = IT_CHARPOS (*(IT)); \
17821 max_bpos = IT_BYTEPOS (*(IT)); \
17822 } \
17823 } \
17824 while (0)
17825
17826 /* Loop generating characters. The loop is left with IT on the next
17827 character to display. */
17828 while (1)
17829 {
17830 int n_glyphs_before, hpos_before, x_before;
17831 int x, nglyphs;
17832 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17833
17834 /* Retrieve the next thing to display. Value is zero if end of
17835 buffer reached. */
17836 if (!get_next_display_element (it))
17837 {
17838 /* Maybe add a space at the end of this line that is used to
17839 display the cursor there under X. Set the charpos of the
17840 first glyph of blank lines not corresponding to any text
17841 to -1. */
17842 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17843 row->exact_window_width_line_p = 1;
17844 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17845 || row->used[TEXT_AREA] == 0)
17846 {
17847 row->glyphs[TEXT_AREA]->charpos = -1;
17848 row->displays_text_p = 0;
17849
17850 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
17851 && (!MINI_WINDOW_P (it->w)
17852 || (minibuf_level && EQ (it->window, minibuf_window))))
17853 row->indicate_empty_line_p = 1;
17854 }
17855
17856 it->continuation_lines_width = 0;
17857 row->ends_at_zv_p = 1;
17858 /* A row that displays right-to-left text must always have
17859 its last face extended all the way to the end of line,
17860 even if this row ends in ZV, because we still write to
17861 the screen left to right. */
17862 if (row->reversed_p)
17863 extend_face_to_end_of_line (it);
17864 break;
17865 }
17866
17867 /* Now, get the metrics of what we want to display. This also
17868 generates glyphs in `row' (which is IT->glyph_row). */
17869 n_glyphs_before = row->used[TEXT_AREA];
17870 x = it->current_x;
17871
17872 /* Remember the line height so far in case the next element doesn't
17873 fit on the line. */
17874 if (it->line_wrap != TRUNCATE)
17875 {
17876 ascent = it->max_ascent;
17877 descent = it->max_descent;
17878 phys_ascent = it->max_phys_ascent;
17879 phys_descent = it->max_phys_descent;
17880
17881 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17882 {
17883 if (IT_DISPLAYING_WHITESPACE (it))
17884 may_wrap = 1;
17885 else if (may_wrap)
17886 {
17887 wrap_it = *it;
17888 wrap_x = x;
17889 wrap_row_used = row->used[TEXT_AREA];
17890 wrap_row_ascent = row->ascent;
17891 wrap_row_height = row->height;
17892 wrap_row_phys_ascent = row->phys_ascent;
17893 wrap_row_phys_height = row->phys_height;
17894 wrap_row_extra_line_spacing = row->extra_line_spacing;
17895 wrap_row_min_pos = min_pos;
17896 wrap_row_min_bpos = min_bpos;
17897 wrap_row_max_pos = max_pos;
17898 wrap_row_max_bpos = max_bpos;
17899 may_wrap = 0;
17900 }
17901 }
17902 }
17903
17904 PRODUCE_GLYPHS (it);
17905
17906 /* If this display element was in marginal areas, continue with
17907 the next one. */
17908 if (it->area != TEXT_AREA)
17909 {
17910 row->ascent = max (row->ascent, it->max_ascent);
17911 row->height = max (row->height, it->max_ascent + it->max_descent);
17912 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17913 row->phys_height = max (row->phys_height,
17914 it->max_phys_ascent + it->max_phys_descent);
17915 row->extra_line_spacing = max (row->extra_line_spacing,
17916 it->max_extra_line_spacing);
17917 set_iterator_to_next (it, 1);
17918 continue;
17919 }
17920
17921 /* Does the display element fit on the line? If we truncate
17922 lines, we should draw past the right edge of the window. If
17923 we don't truncate, we want to stop so that we can display the
17924 continuation glyph before the right margin. If lines are
17925 continued, there are two possible strategies for characters
17926 resulting in more than 1 glyph (e.g. tabs): Display as many
17927 glyphs as possible in this line and leave the rest for the
17928 continuation line, or display the whole element in the next
17929 line. Original redisplay did the former, so we do it also. */
17930 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17931 hpos_before = it->hpos;
17932 x_before = x;
17933
17934 if (/* Not a newline. */
17935 nglyphs > 0
17936 /* Glyphs produced fit entirely in the line. */
17937 && it->current_x < it->last_visible_x)
17938 {
17939 it->hpos += nglyphs;
17940 row->ascent = max (row->ascent, it->max_ascent);
17941 row->height = max (row->height, it->max_ascent + it->max_descent);
17942 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17943 row->phys_height = max (row->phys_height,
17944 it->max_phys_ascent + it->max_phys_descent);
17945 row->extra_line_spacing = max (row->extra_line_spacing,
17946 it->max_extra_line_spacing);
17947 if (it->current_x - it->pixel_width < it->first_visible_x)
17948 row->x = x - it->first_visible_x;
17949 /* Record the maximum and minimum buffer positions seen so
17950 far in glyphs that will be displayed by this row. */
17951 if (it->bidi_p)
17952 RECORD_MAX_MIN_POS (it);
17953 }
17954 else
17955 {
17956 int i, new_x;
17957 struct glyph *glyph;
17958
17959 for (i = 0; i < nglyphs; ++i, x = new_x)
17960 {
17961 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17962 new_x = x + glyph->pixel_width;
17963
17964 if (/* Lines are continued. */
17965 it->line_wrap != TRUNCATE
17966 && (/* Glyph doesn't fit on the line. */
17967 new_x > it->last_visible_x
17968 /* Or it fits exactly on a window system frame. */
17969 || (new_x == it->last_visible_x
17970 && FRAME_WINDOW_P (it->f))))
17971 {
17972 /* End of a continued line. */
17973
17974 if (it->hpos == 0
17975 || (new_x == it->last_visible_x
17976 && FRAME_WINDOW_P (it->f)))
17977 {
17978 /* Current glyph is the only one on the line or
17979 fits exactly on the line. We must continue
17980 the line because we can't draw the cursor
17981 after the glyph. */
17982 row->continued_p = 1;
17983 it->current_x = new_x;
17984 it->continuation_lines_width += new_x;
17985 ++it->hpos;
17986 /* Record the maximum and minimum buffer
17987 positions seen so far in glyphs that will be
17988 displayed by this row. */
17989 if (it->bidi_p)
17990 RECORD_MAX_MIN_POS (it);
17991 if (i == nglyphs - 1)
17992 {
17993 /* If line-wrap is on, check if a previous
17994 wrap point was found. */
17995 if (wrap_row_used > 0
17996 /* Even if there is a previous wrap
17997 point, continue the line here as
17998 usual, if (i) the previous character
17999 was a space or tab AND (ii) the
18000 current character is not. */
18001 && (!may_wrap
18002 || IT_DISPLAYING_WHITESPACE (it)))
18003 goto back_to_wrap;
18004
18005 set_iterator_to_next (it, 1);
18006 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18007 {
18008 if (!get_next_display_element (it))
18009 {
18010 row->exact_window_width_line_p = 1;
18011 it->continuation_lines_width = 0;
18012 row->continued_p = 0;
18013 row->ends_at_zv_p = 1;
18014 }
18015 else if (ITERATOR_AT_END_OF_LINE_P (it))
18016 {
18017 row->continued_p = 0;
18018 row->exact_window_width_line_p = 1;
18019 }
18020 }
18021 }
18022 }
18023 else if (CHAR_GLYPH_PADDING_P (*glyph)
18024 && !FRAME_WINDOW_P (it->f))
18025 {
18026 /* A padding glyph that doesn't fit on this line.
18027 This means the whole character doesn't fit
18028 on the line. */
18029 if (row->reversed_p)
18030 unproduce_glyphs (it, row->used[TEXT_AREA]
18031 - n_glyphs_before);
18032 row->used[TEXT_AREA] = n_glyphs_before;
18033
18034 /* Fill the rest of the row with continuation
18035 glyphs like in 20.x. */
18036 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18037 < row->glyphs[1 + TEXT_AREA])
18038 produce_special_glyphs (it, IT_CONTINUATION);
18039
18040 row->continued_p = 1;
18041 it->current_x = x_before;
18042 it->continuation_lines_width += x_before;
18043
18044 /* Restore the height to what it was before the
18045 element not fitting on the line. */
18046 it->max_ascent = ascent;
18047 it->max_descent = descent;
18048 it->max_phys_ascent = phys_ascent;
18049 it->max_phys_descent = phys_descent;
18050 }
18051 else if (wrap_row_used > 0)
18052 {
18053 back_to_wrap:
18054 if (row->reversed_p)
18055 unproduce_glyphs (it,
18056 row->used[TEXT_AREA] - wrap_row_used);
18057 *it = wrap_it;
18058 it->continuation_lines_width += wrap_x;
18059 row->used[TEXT_AREA] = wrap_row_used;
18060 row->ascent = wrap_row_ascent;
18061 row->height = wrap_row_height;
18062 row->phys_ascent = wrap_row_phys_ascent;
18063 row->phys_height = wrap_row_phys_height;
18064 row->extra_line_spacing = wrap_row_extra_line_spacing;
18065 min_pos = wrap_row_min_pos;
18066 min_bpos = wrap_row_min_bpos;
18067 max_pos = wrap_row_max_pos;
18068 max_bpos = wrap_row_max_bpos;
18069 row->continued_p = 1;
18070 row->ends_at_zv_p = 0;
18071 row->exact_window_width_line_p = 0;
18072 it->continuation_lines_width += x;
18073
18074 /* Make sure that a non-default face is extended
18075 up to the right margin of the window. */
18076 extend_face_to_end_of_line (it);
18077 }
18078 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18079 {
18080 /* A TAB that extends past the right edge of the
18081 window. This produces a single glyph on
18082 window system frames. We leave the glyph in
18083 this row and let it fill the row, but don't
18084 consume the TAB. */
18085 it->continuation_lines_width += it->last_visible_x;
18086 row->ends_in_middle_of_char_p = 1;
18087 row->continued_p = 1;
18088 glyph->pixel_width = it->last_visible_x - x;
18089 it->starts_in_middle_of_char_p = 1;
18090 }
18091 else
18092 {
18093 /* Something other than a TAB that draws past
18094 the right edge of the window. Restore
18095 positions to values before the element. */
18096 if (row->reversed_p)
18097 unproduce_glyphs (it, row->used[TEXT_AREA]
18098 - (n_glyphs_before + i));
18099 row->used[TEXT_AREA] = n_glyphs_before + i;
18100
18101 /* Display continuation glyphs. */
18102 if (!FRAME_WINDOW_P (it->f))
18103 produce_special_glyphs (it, IT_CONTINUATION);
18104 row->continued_p = 1;
18105
18106 it->current_x = x_before;
18107 it->continuation_lines_width += x;
18108 extend_face_to_end_of_line (it);
18109
18110 if (nglyphs > 1 && i > 0)
18111 {
18112 row->ends_in_middle_of_char_p = 1;
18113 it->starts_in_middle_of_char_p = 1;
18114 }
18115
18116 /* Restore the height to what it was before the
18117 element not fitting on the line. */
18118 it->max_ascent = ascent;
18119 it->max_descent = descent;
18120 it->max_phys_ascent = phys_ascent;
18121 it->max_phys_descent = phys_descent;
18122 }
18123
18124 break;
18125 }
18126 else if (new_x > it->first_visible_x)
18127 {
18128 /* Increment number of glyphs actually displayed. */
18129 ++it->hpos;
18130
18131 /* Record the maximum and minimum buffer positions
18132 seen so far in glyphs that will be displayed by
18133 this row. */
18134 if (it->bidi_p)
18135 RECORD_MAX_MIN_POS (it);
18136
18137 if (x < it->first_visible_x)
18138 /* Glyph is partially visible, i.e. row starts at
18139 negative X position. */
18140 row->x = x - it->first_visible_x;
18141 }
18142 else
18143 {
18144 /* Glyph is completely off the left margin of the
18145 window. This should not happen because of the
18146 move_it_in_display_line at the start of this
18147 function, unless the text display area of the
18148 window is empty. */
18149 xassert (it->first_visible_x <= it->last_visible_x);
18150 }
18151 }
18152
18153 row->ascent = max (row->ascent, it->max_ascent);
18154 row->height = max (row->height, it->max_ascent + it->max_descent);
18155 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18156 row->phys_height = max (row->phys_height,
18157 it->max_phys_ascent + it->max_phys_descent);
18158 row->extra_line_spacing = max (row->extra_line_spacing,
18159 it->max_extra_line_spacing);
18160
18161 /* End of this display line if row is continued. */
18162 if (row->continued_p || row->ends_at_zv_p)
18163 break;
18164 }
18165
18166 at_end_of_line:
18167 /* Is this a line end? If yes, we're also done, after making
18168 sure that a non-default face is extended up to the right
18169 margin of the window. */
18170 if (ITERATOR_AT_END_OF_LINE_P (it))
18171 {
18172 int used_before = row->used[TEXT_AREA];
18173
18174 row->ends_in_newline_from_string_p = STRINGP (it->object);
18175
18176 /* Add a space at the end of the line that is used to
18177 display the cursor there. */
18178 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18179 append_space_for_newline (it, 0);
18180
18181 /* Extend the face to the end of the line. */
18182 extend_face_to_end_of_line (it);
18183
18184 /* Make sure we have the position. */
18185 if (used_before == 0)
18186 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18187
18188 /* Record the position of the newline, for use in
18189 find_row_edges. */
18190 it->eol_pos = it->current.pos;
18191
18192 /* Consume the line end. This skips over invisible lines. */
18193 set_iterator_to_next (it, 1);
18194 it->continuation_lines_width = 0;
18195 break;
18196 }
18197
18198 /* Proceed with next display element. Note that this skips
18199 over lines invisible because of selective display. */
18200 set_iterator_to_next (it, 1);
18201
18202 /* If we truncate lines, we are done when the last displayed
18203 glyphs reach past the right margin of the window. */
18204 if (it->line_wrap == TRUNCATE
18205 && (FRAME_WINDOW_P (it->f)
18206 ? (it->current_x >= it->last_visible_x)
18207 : (it->current_x > it->last_visible_x)))
18208 {
18209 /* Maybe add truncation glyphs. */
18210 if (!FRAME_WINDOW_P (it->f))
18211 {
18212 int i, n;
18213
18214 if (!row->reversed_p)
18215 {
18216 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18217 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18218 break;
18219 }
18220 else
18221 {
18222 for (i = 0; i < row->used[TEXT_AREA]; i++)
18223 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18224 break;
18225 /* Remove any padding glyphs at the front of ROW, to
18226 make room for the truncation glyphs we will be
18227 adding below. The loop below always inserts at
18228 least one truncation glyph, so also remove the
18229 last glyph added to ROW. */
18230 unproduce_glyphs (it, i + 1);
18231 /* Adjust i for the loop below. */
18232 i = row->used[TEXT_AREA] - (i + 1);
18233 }
18234
18235 for (n = row->used[TEXT_AREA]; i < n; ++i)
18236 {
18237 row->used[TEXT_AREA] = i;
18238 produce_special_glyphs (it, IT_TRUNCATION);
18239 }
18240 }
18241 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18242 {
18243 /* Don't truncate if we can overflow newline into fringe. */
18244 if (!get_next_display_element (it))
18245 {
18246 it->continuation_lines_width = 0;
18247 row->ends_at_zv_p = 1;
18248 row->exact_window_width_line_p = 1;
18249 break;
18250 }
18251 if (ITERATOR_AT_END_OF_LINE_P (it))
18252 {
18253 row->exact_window_width_line_p = 1;
18254 goto at_end_of_line;
18255 }
18256 }
18257
18258 row->truncated_on_right_p = 1;
18259 it->continuation_lines_width = 0;
18260 reseat_at_next_visible_line_start (it, 0);
18261 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18262 it->hpos = hpos_before;
18263 it->current_x = x_before;
18264 break;
18265 }
18266 }
18267
18268 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18269 at the left window margin. */
18270 if (it->first_visible_x
18271 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18272 {
18273 if (!FRAME_WINDOW_P (it->f))
18274 insert_left_trunc_glyphs (it);
18275 row->truncated_on_left_p = 1;
18276 }
18277
18278 /* Remember the position at which this line ends.
18279
18280 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18281 cannot be before the call to find_row_edges below, since that is
18282 where these positions are determined. */
18283 row->end = it->current;
18284 if (!it->bidi_p)
18285 {
18286 row->minpos = row->start.pos;
18287 row->maxpos = row->end.pos;
18288 }
18289 else
18290 {
18291 /* ROW->minpos and ROW->maxpos must be the smallest and
18292 `1 + the largest' buffer positions in ROW. But if ROW was
18293 bidi-reordered, these two positions can be anywhere in the
18294 row, so we must determine them now. */
18295 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18296 }
18297
18298 /* If the start of this line is the overlay arrow-position, then
18299 mark this glyph row as the one containing the overlay arrow.
18300 This is clearly a mess with variable size fonts. It would be
18301 better to let it be displayed like cursors under X. */
18302 if ((row->displays_text_p || !overlay_arrow_seen)
18303 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18304 !NILP (overlay_arrow_string)))
18305 {
18306 /* Overlay arrow in window redisplay is a fringe bitmap. */
18307 if (STRINGP (overlay_arrow_string))
18308 {
18309 struct glyph_row *arrow_row
18310 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18311 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18312 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18313 struct glyph *p = row->glyphs[TEXT_AREA];
18314 struct glyph *p2, *end;
18315
18316 /* Copy the arrow glyphs. */
18317 while (glyph < arrow_end)
18318 *p++ = *glyph++;
18319
18320 /* Throw away padding glyphs. */
18321 p2 = p;
18322 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18323 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18324 ++p2;
18325 if (p2 > p)
18326 {
18327 while (p2 < end)
18328 *p++ = *p2++;
18329 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18330 }
18331 }
18332 else
18333 {
18334 xassert (INTEGERP (overlay_arrow_string));
18335 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18336 }
18337 overlay_arrow_seen = 1;
18338 }
18339
18340 /* Compute pixel dimensions of this line. */
18341 compute_line_metrics (it);
18342
18343 /* Record whether this row ends inside an ellipsis. */
18344 row->ends_in_ellipsis_p
18345 = (it->method == GET_FROM_DISPLAY_VECTOR
18346 && it->ellipsis_p);
18347
18348 /* Save fringe bitmaps in this row. */
18349 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18350 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18351 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18352 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18353
18354 it->left_user_fringe_bitmap = 0;
18355 it->left_user_fringe_face_id = 0;
18356 it->right_user_fringe_bitmap = 0;
18357 it->right_user_fringe_face_id = 0;
18358
18359 /* Maybe set the cursor. */
18360 cvpos = it->w->cursor.vpos;
18361 if ((cvpos < 0
18362 /* In bidi-reordered rows, keep checking for proper cursor
18363 position even if one has been found already, because buffer
18364 positions in such rows change non-linearly with ROW->VPOS,
18365 when a line is continued. One exception: when we are at ZV,
18366 display cursor on the first suitable glyph row, since all
18367 the empty rows after that also have their position set to ZV. */
18368 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18369 lines' rows is implemented for bidi-reordered rows. */
18370 || (it->bidi_p
18371 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18372 && PT >= MATRIX_ROW_START_CHARPOS (row)
18373 && PT <= MATRIX_ROW_END_CHARPOS (row)
18374 && cursor_row_p (row))
18375 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18376
18377 /* Highlight trailing whitespace. */
18378 if (!NILP (Vshow_trailing_whitespace))
18379 highlight_trailing_whitespace (it->f, it->glyph_row);
18380
18381 /* Prepare for the next line. This line starts horizontally at (X
18382 HPOS) = (0 0). Vertical positions are incremented. As a
18383 convenience for the caller, IT->glyph_row is set to the next
18384 row to be used. */
18385 it->current_x = it->hpos = 0;
18386 it->current_y += row->height;
18387 SET_TEXT_POS (it->eol_pos, 0, 0);
18388 ++it->vpos;
18389 ++it->glyph_row;
18390 /* The next row should by default use the same value of the
18391 reversed_p flag as this one. set_iterator_to_next decides when
18392 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18393 the flag accordingly. */
18394 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18395 it->glyph_row->reversed_p = row->reversed_p;
18396 it->start = row->end;
18397 return row->displays_text_p;
18398
18399 #undef RECORD_MAX_MIN_POS
18400 }
18401
18402 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18403 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18404 doc: /* Return paragraph direction at point in BUFFER.
18405 Value is either `left-to-right' or `right-to-left'.
18406 If BUFFER is omitted or nil, it defaults to the current buffer.
18407
18408 Paragraph direction determines how the text in the paragraph is displayed.
18409 In left-to-right paragraphs, text begins at the left margin of the window
18410 and the reading direction is generally left to right. In right-to-left
18411 paragraphs, text begins at the right margin and is read from right to left.
18412
18413 See also `bidi-paragraph-direction'. */)
18414 (Lisp_Object buffer)
18415 {
18416 struct buffer *buf = current_buffer;
18417 struct buffer *old = buf;
18418
18419 if (! NILP (buffer))
18420 {
18421 CHECK_BUFFER (buffer);
18422 buf = XBUFFER (buffer);
18423 }
18424
18425 if (NILP (BVAR (buf, bidi_display_reordering)))
18426 return Qleft_to_right;
18427 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18428 return BVAR (buf, bidi_paragraph_direction);
18429 else
18430 {
18431 /* Determine the direction from buffer text. We could try to
18432 use current_matrix if it is up to date, but this seems fast
18433 enough as it is. */
18434 struct bidi_it itb;
18435 EMACS_INT pos = BUF_PT (buf);
18436 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18437 int c;
18438
18439 set_buffer_temp (buf);
18440 /* bidi_paragraph_init finds the base direction of the paragraph
18441 by searching forward from paragraph start. We need the base
18442 direction of the current or _previous_ paragraph, so we need
18443 to make sure we are within that paragraph. To that end, find
18444 the previous non-empty line. */
18445 if (pos >= ZV && pos > BEGV)
18446 {
18447 pos--;
18448 bytepos = CHAR_TO_BYTE (pos);
18449 }
18450 while ((c = FETCH_BYTE (bytepos)) == '\n'
18451 || c == ' ' || c == '\t' || c == '\f')
18452 {
18453 if (bytepos <= BEGV_BYTE)
18454 break;
18455 bytepos--;
18456 pos--;
18457 }
18458 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18459 bytepos--;
18460 itb.charpos = pos;
18461 itb.bytepos = bytepos;
18462 itb.nchars = -1;
18463 itb.string.s = NULL;
18464 itb.string.lstring = Qnil;
18465 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18466 itb.first_elt = 1;
18467 itb.separator_limit = -1;
18468 itb.paragraph_dir = NEUTRAL_DIR;
18469
18470 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18471 set_buffer_temp (old);
18472 switch (itb.paragraph_dir)
18473 {
18474 case L2R:
18475 return Qleft_to_right;
18476 break;
18477 case R2L:
18478 return Qright_to_left;
18479 break;
18480 default:
18481 abort ();
18482 }
18483 }
18484 }
18485
18486
18487 \f
18488 /***********************************************************************
18489 Menu Bar
18490 ***********************************************************************/
18491
18492 /* Redisplay the menu bar in the frame for window W.
18493
18494 The menu bar of X frames that don't have X toolkit support is
18495 displayed in a special window W->frame->menu_bar_window.
18496
18497 The menu bar of terminal frames is treated specially as far as
18498 glyph matrices are concerned. Menu bar lines are not part of
18499 windows, so the update is done directly on the frame matrix rows
18500 for the menu bar. */
18501
18502 static void
18503 display_menu_bar (struct window *w)
18504 {
18505 struct frame *f = XFRAME (WINDOW_FRAME (w));
18506 struct it it;
18507 Lisp_Object items;
18508 int i;
18509
18510 /* Don't do all this for graphical frames. */
18511 #ifdef HAVE_NTGUI
18512 if (FRAME_W32_P (f))
18513 return;
18514 #endif
18515 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18516 if (FRAME_X_P (f))
18517 return;
18518 #endif
18519
18520 #ifdef HAVE_NS
18521 if (FRAME_NS_P (f))
18522 return;
18523 #endif /* HAVE_NS */
18524
18525 #ifdef USE_X_TOOLKIT
18526 xassert (!FRAME_WINDOW_P (f));
18527 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18528 it.first_visible_x = 0;
18529 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18530 #else /* not USE_X_TOOLKIT */
18531 if (FRAME_WINDOW_P (f))
18532 {
18533 /* Menu bar lines are displayed in the desired matrix of the
18534 dummy window menu_bar_window. */
18535 struct window *menu_w;
18536 xassert (WINDOWP (f->menu_bar_window));
18537 menu_w = XWINDOW (f->menu_bar_window);
18538 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18539 MENU_FACE_ID);
18540 it.first_visible_x = 0;
18541 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18542 }
18543 else
18544 {
18545 /* This is a TTY frame, i.e. character hpos/vpos are used as
18546 pixel x/y. */
18547 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18548 MENU_FACE_ID);
18549 it.first_visible_x = 0;
18550 it.last_visible_x = FRAME_COLS (f);
18551 }
18552 #endif /* not USE_X_TOOLKIT */
18553
18554 if (! mode_line_inverse_video)
18555 /* Force the menu-bar to be displayed in the default face. */
18556 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18557
18558 /* Clear all rows of the menu bar. */
18559 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18560 {
18561 struct glyph_row *row = it.glyph_row + i;
18562 clear_glyph_row (row);
18563 row->enabled_p = 1;
18564 row->full_width_p = 1;
18565 }
18566
18567 /* Display all items of the menu bar. */
18568 items = FRAME_MENU_BAR_ITEMS (it.f);
18569 for (i = 0; i < ASIZE (items); i += 4)
18570 {
18571 Lisp_Object string;
18572
18573 /* Stop at nil string. */
18574 string = AREF (items, i + 1);
18575 if (NILP (string))
18576 break;
18577
18578 /* Remember where item was displayed. */
18579 ASET (items, i + 3, make_number (it.hpos));
18580
18581 /* Display the item, pad with one space. */
18582 if (it.current_x < it.last_visible_x)
18583 display_string (NULL, string, Qnil, 0, 0, &it,
18584 SCHARS (string) + 1, 0, 0, -1);
18585 }
18586
18587 /* Fill out the line with spaces. */
18588 if (it.current_x < it.last_visible_x)
18589 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18590
18591 /* Compute the total height of the lines. */
18592 compute_line_metrics (&it);
18593 }
18594
18595
18596 \f
18597 /***********************************************************************
18598 Mode Line
18599 ***********************************************************************/
18600
18601 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18602 FORCE is non-zero, redisplay mode lines unconditionally.
18603 Otherwise, redisplay only mode lines that are garbaged. Value is
18604 the number of windows whose mode lines were redisplayed. */
18605
18606 static int
18607 redisplay_mode_lines (Lisp_Object window, int force)
18608 {
18609 int nwindows = 0;
18610
18611 while (!NILP (window))
18612 {
18613 struct window *w = XWINDOW (window);
18614
18615 if (WINDOWP (w->hchild))
18616 nwindows += redisplay_mode_lines (w->hchild, force);
18617 else if (WINDOWP (w->vchild))
18618 nwindows += redisplay_mode_lines (w->vchild, force);
18619 else if (force
18620 || FRAME_GARBAGED_P (XFRAME (w->frame))
18621 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18622 {
18623 struct text_pos lpoint;
18624 struct buffer *old = current_buffer;
18625
18626 /* Set the window's buffer for the mode line display. */
18627 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18628 set_buffer_internal_1 (XBUFFER (w->buffer));
18629
18630 /* Point refers normally to the selected window. For any
18631 other window, set up appropriate value. */
18632 if (!EQ (window, selected_window))
18633 {
18634 struct text_pos pt;
18635
18636 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18637 if (CHARPOS (pt) < BEGV)
18638 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18639 else if (CHARPOS (pt) > (ZV - 1))
18640 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18641 else
18642 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18643 }
18644
18645 /* Display mode lines. */
18646 clear_glyph_matrix (w->desired_matrix);
18647 if (display_mode_lines (w))
18648 {
18649 ++nwindows;
18650 w->must_be_updated_p = 1;
18651 }
18652
18653 /* Restore old settings. */
18654 set_buffer_internal_1 (old);
18655 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18656 }
18657
18658 window = w->next;
18659 }
18660
18661 return nwindows;
18662 }
18663
18664
18665 /* Display the mode and/or header line of window W. Value is the
18666 sum number of mode lines and header lines displayed. */
18667
18668 static int
18669 display_mode_lines (struct window *w)
18670 {
18671 Lisp_Object old_selected_window, old_selected_frame;
18672 int n = 0;
18673
18674 old_selected_frame = selected_frame;
18675 selected_frame = w->frame;
18676 old_selected_window = selected_window;
18677 XSETWINDOW (selected_window, w);
18678
18679 /* These will be set while the mode line specs are processed. */
18680 line_number_displayed = 0;
18681 w->column_number_displayed = Qnil;
18682
18683 if (WINDOW_WANTS_MODELINE_P (w))
18684 {
18685 struct window *sel_w = XWINDOW (old_selected_window);
18686
18687 /* Select mode line face based on the real selected window. */
18688 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18689 BVAR (current_buffer, mode_line_format));
18690 ++n;
18691 }
18692
18693 if (WINDOW_WANTS_HEADER_LINE_P (w))
18694 {
18695 display_mode_line (w, HEADER_LINE_FACE_ID,
18696 BVAR (current_buffer, header_line_format));
18697 ++n;
18698 }
18699
18700 selected_frame = old_selected_frame;
18701 selected_window = old_selected_window;
18702 return n;
18703 }
18704
18705
18706 /* Display mode or header line of window W. FACE_ID specifies which
18707 line to display; it is either MODE_LINE_FACE_ID or
18708 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18709 display. Value is the pixel height of the mode/header line
18710 displayed. */
18711
18712 static int
18713 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18714 {
18715 struct it it;
18716 struct face *face;
18717 int count = SPECPDL_INDEX ();
18718
18719 init_iterator (&it, w, -1, -1, NULL, face_id);
18720 /* Don't extend on a previously drawn mode-line.
18721 This may happen if called from pos_visible_p. */
18722 it.glyph_row->enabled_p = 0;
18723 prepare_desired_row (it.glyph_row);
18724
18725 it.glyph_row->mode_line_p = 1;
18726
18727 if (! mode_line_inverse_video)
18728 /* Force the mode-line to be displayed in the default face. */
18729 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18730
18731 /* FIXME: This should take its value from a user option. */
18732 it.paragraph_embedding = L2R;
18733
18734 record_unwind_protect (unwind_format_mode_line,
18735 format_mode_line_unwind_data (NULL, Qnil, 0));
18736
18737 mode_line_target = MODE_LINE_DISPLAY;
18738
18739 /* Temporarily make frame's keyboard the current kboard so that
18740 kboard-local variables in the mode_line_format will get the right
18741 values. */
18742 push_kboard (FRAME_KBOARD (it.f));
18743 record_unwind_save_match_data ();
18744 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18745 pop_kboard ();
18746
18747 unbind_to (count, Qnil);
18748
18749 /* Fill up with spaces. */
18750 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18751
18752 compute_line_metrics (&it);
18753 it.glyph_row->full_width_p = 1;
18754 it.glyph_row->continued_p = 0;
18755 it.glyph_row->truncated_on_left_p = 0;
18756 it.glyph_row->truncated_on_right_p = 0;
18757
18758 /* Make a 3D mode-line have a shadow at its right end. */
18759 face = FACE_FROM_ID (it.f, face_id);
18760 extend_face_to_end_of_line (&it);
18761 if (face->box != FACE_NO_BOX)
18762 {
18763 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18764 + it.glyph_row->used[TEXT_AREA] - 1);
18765 last->right_box_line_p = 1;
18766 }
18767
18768 return it.glyph_row->height;
18769 }
18770
18771 /* Move element ELT in LIST to the front of LIST.
18772 Return the updated list. */
18773
18774 static Lisp_Object
18775 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18776 {
18777 register Lisp_Object tail, prev;
18778 register Lisp_Object tem;
18779
18780 tail = list;
18781 prev = Qnil;
18782 while (CONSP (tail))
18783 {
18784 tem = XCAR (tail);
18785
18786 if (EQ (elt, tem))
18787 {
18788 /* Splice out the link TAIL. */
18789 if (NILP (prev))
18790 list = XCDR (tail);
18791 else
18792 Fsetcdr (prev, XCDR (tail));
18793
18794 /* Now make it the first. */
18795 Fsetcdr (tail, list);
18796 return tail;
18797 }
18798 else
18799 prev = tail;
18800 tail = XCDR (tail);
18801 QUIT;
18802 }
18803
18804 /* Not found--return unchanged LIST. */
18805 return list;
18806 }
18807
18808 /* Contribute ELT to the mode line for window IT->w. How it
18809 translates into text depends on its data type.
18810
18811 IT describes the display environment in which we display, as usual.
18812
18813 DEPTH is the depth in recursion. It is used to prevent
18814 infinite recursion here.
18815
18816 FIELD_WIDTH is the number of characters the display of ELT should
18817 occupy in the mode line, and PRECISION is the maximum number of
18818 characters to display from ELT's representation. See
18819 display_string for details.
18820
18821 Returns the hpos of the end of the text generated by ELT.
18822
18823 PROPS is a property list to add to any string we encounter.
18824
18825 If RISKY is nonzero, remove (disregard) any properties in any string
18826 we encounter, and ignore :eval and :propertize.
18827
18828 The global variable `mode_line_target' determines whether the
18829 output is passed to `store_mode_line_noprop',
18830 `store_mode_line_string', or `display_string'. */
18831
18832 static int
18833 display_mode_element (struct it *it, int depth, int field_width, int precision,
18834 Lisp_Object elt, Lisp_Object props, int risky)
18835 {
18836 int n = 0, field, prec;
18837 int literal = 0;
18838
18839 it->paragraph_embedding = L2R;
18840
18841 tail_recurse:
18842 if (depth > 100)
18843 elt = build_string ("*too-deep*");
18844
18845 depth++;
18846
18847 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18848 {
18849 case Lisp_String:
18850 {
18851 /* A string: output it and check for %-constructs within it. */
18852 unsigned char c;
18853 EMACS_INT offset = 0;
18854
18855 if (SCHARS (elt) > 0
18856 && (!NILP (props) || risky))
18857 {
18858 Lisp_Object oprops, aelt;
18859 oprops = Ftext_properties_at (make_number (0), elt);
18860
18861 /* If the starting string's properties are not what
18862 we want, translate the string. Also, if the string
18863 is risky, do that anyway. */
18864
18865 if (NILP (Fequal (props, oprops)) || risky)
18866 {
18867 /* If the starting string has properties,
18868 merge the specified ones onto the existing ones. */
18869 if (! NILP (oprops) && !risky)
18870 {
18871 Lisp_Object tem;
18872
18873 oprops = Fcopy_sequence (oprops);
18874 tem = props;
18875 while (CONSP (tem))
18876 {
18877 oprops = Fplist_put (oprops, XCAR (tem),
18878 XCAR (XCDR (tem)));
18879 tem = XCDR (XCDR (tem));
18880 }
18881 props = oprops;
18882 }
18883
18884 aelt = Fassoc (elt, mode_line_proptrans_alist);
18885 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18886 {
18887 /* AELT is what we want. Move it to the front
18888 without consing. */
18889 elt = XCAR (aelt);
18890 mode_line_proptrans_alist
18891 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18892 }
18893 else
18894 {
18895 Lisp_Object tem;
18896
18897 /* If AELT has the wrong props, it is useless.
18898 so get rid of it. */
18899 if (! NILP (aelt))
18900 mode_line_proptrans_alist
18901 = Fdelq (aelt, mode_line_proptrans_alist);
18902
18903 elt = Fcopy_sequence (elt);
18904 Fset_text_properties (make_number (0), Flength (elt),
18905 props, elt);
18906 /* Add this item to mode_line_proptrans_alist. */
18907 mode_line_proptrans_alist
18908 = Fcons (Fcons (elt, props),
18909 mode_line_proptrans_alist);
18910 /* Truncate mode_line_proptrans_alist
18911 to at most 50 elements. */
18912 tem = Fnthcdr (make_number (50),
18913 mode_line_proptrans_alist);
18914 if (! NILP (tem))
18915 XSETCDR (tem, Qnil);
18916 }
18917 }
18918 }
18919
18920 offset = 0;
18921
18922 if (literal)
18923 {
18924 prec = precision - n;
18925 switch (mode_line_target)
18926 {
18927 case MODE_LINE_NOPROP:
18928 case MODE_LINE_TITLE:
18929 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
18930 break;
18931 case MODE_LINE_STRING:
18932 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18933 break;
18934 case MODE_LINE_DISPLAY:
18935 n += display_string (NULL, elt, Qnil, 0, 0, it,
18936 0, prec, 0, STRING_MULTIBYTE (elt));
18937 break;
18938 }
18939
18940 break;
18941 }
18942
18943 /* Handle the non-literal case. */
18944
18945 while ((precision <= 0 || n < precision)
18946 && SREF (elt, offset) != 0
18947 && (mode_line_target != MODE_LINE_DISPLAY
18948 || it->current_x < it->last_visible_x))
18949 {
18950 EMACS_INT last_offset = offset;
18951
18952 /* Advance to end of string or next format specifier. */
18953 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18954 ;
18955
18956 if (offset - 1 != last_offset)
18957 {
18958 EMACS_INT nchars, nbytes;
18959
18960 /* Output to end of string or up to '%'. Field width
18961 is length of string. Don't output more than
18962 PRECISION allows us. */
18963 offset--;
18964
18965 prec = c_string_width (SDATA (elt) + last_offset,
18966 offset - last_offset, precision - n,
18967 &nchars, &nbytes);
18968
18969 switch (mode_line_target)
18970 {
18971 case MODE_LINE_NOPROP:
18972 case MODE_LINE_TITLE:
18973 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
18974 break;
18975 case MODE_LINE_STRING:
18976 {
18977 EMACS_INT bytepos = last_offset;
18978 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18979 EMACS_INT endpos = (precision <= 0
18980 ? string_byte_to_char (elt, offset)
18981 : charpos + nchars);
18982
18983 n += store_mode_line_string (NULL,
18984 Fsubstring (elt, make_number (charpos),
18985 make_number (endpos)),
18986 0, 0, 0, Qnil);
18987 }
18988 break;
18989 case MODE_LINE_DISPLAY:
18990 {
18991 EMACS_INT bytepos = last_offset;
18992 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18993
18994 if (precision <= 0)
18995 nchars = string_byte_to_char (elt, offset) - charpos;
18996 n += display_string (NULL, elt, Qnil, 0, charpos,
18997 it, 0, nchars, 0,
18998 STRING_MULTIBYTE (elt));
18999 }
19000 break;
19001 }
19002 }
19003 else /* c == '%' */
19004 {
19005 EMACS_INT percent_position = offset;
19006
19007 /* Get the specified minimum width. Zero means
19008 don't pad. */
19009 field = 0;
19010 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19011 field = field * 10 + c - '0';
19012
19013 /* Don't pad beyond the total padding allowed. */
19014 if (field_width - n > 0 && field > field_width - n)
19015 field = field_width - n;
19016
19017 /* Note that either PRECISION <= 0 or N < PRECISION. */
19018 prec = precision - n;
19019
19020 if (c == 'M')
19021 n += display_mode_element (it, depth, field, prec,
19022 Vglobal_mode_string, props,
19023 risky);
19024 else if (c != 0)
19025 {
19026 int multibyte;
19027 EMACS_INT bytepos, charpos;
19028 const char *spec;
19029 Lisp_Object string;
19030
19031 bytepos = percent_position;
19032 charpos = (STRING_MULTIBYTE (elt)
19033 ? string_byte_to_char (elt, bytepos)
19034 : bytepos);
19035 spec = decode_mode_spec (it->w, c, field, &string);
19036 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19037
19038 switch (mode_line_target)
19039 {
19040 case MODE_LINE_NOPROP:
19041 case MODE_LINE_TITLE:
19042 n += store_mode_line_noprop (spec, field, prec);
19043 break;
19044 case MODE_LINE_STRING:
19045 {
19046 int len = strlen (spec);
19047 Lisp_Object tem = make_string (spec, len);
19048 props = Ftext_properties_at (make_number (charpos), elt);
19049 /* Should only keep face property in props */
19050 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19051 }
19052 break;
19053 case MODE_LINE_DISPLAY:
19054 {
19055 int nglyphs_before, nwritten;
19056
19057 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19058 nwritten = display_string (spec, string, elt,
19059 charpos, 0, it,
19060 field, prec, 0,
19061 multibyte);
19062
19063 /* Assign to the glyphs written above the
19064 string where the `%x' came from, position
19065 of the `%'. */
19066 if (nwritten > 0)
19067 {
19068 struct glyph *glyph
19069 = (it->glyph_row->glyphs[TEXT_AREA]
19070 + nglyphs_before);
19071 int i;
19072
19073 for (i = 0; i < nwritten; ++i)
19074 {
19075 glyph[i].object = elt;
19076 glyph[i].charpos = charpos;
19077 }
19078
19079 n += nwritten;
19080 }
19081 }
19082 break;
19083 }
19084 }
19085 else /* c == 0 */
19086 break;
19087 }
19088 }
19089 }
19090 break;
19091
19092 case Lisp_Symbol:
19093 /* A symbol: process the value of the symbol recursively
19094 as if it appeared here directly. Avoid error if symbol void.
19095 Special case: if value of symbol is a string, output the string
19096 literally. */
19097 {
19098 register Lisp_Object tem;
19099
19100 /* If the variable is not marked as risky to set
19101 then its contents are risky to use. */
19102 if (NILP (Fget (elt, Qrisky_local_variable)))
19103 risky = 1;
19104
19105 tem = Fboundp (elt);
19106 if (!NILP (tem))
19107 {
19108 tem = Fsymbol_value (elt);
19109 /* If value is a string, output that string literally:
19110 don't check for % within it. */
19111 if (STRINGP (tem))
19112 literal = 1;
19113
19114 if (!EQ (tem, elt))
19115 {
19116 /* Give up right away for nil or t. */
19117 elt = tem;
19118 goto tail_recurse;
19119 }
19120 }
19121 }
19122 break;
19123
19124 case Lisp_Cons:
19125 {
19126 register Lisp_Object car, tem;
19127
19128 /* A cons cell: five distinct cases.
19129 If first element is :eval or :propertize, do something special.
19130 If first element is a string or a cons, process all the elements
19131 and effectively concatenate them.
19132 If first element is a negative number, truncate displaying cdr to
19133 at most that many characters. If positive, pad (with spaces)
19134 to at least that many characters.
19135 If first element is a symbol, process the cadr or caddr recursively
19136 according to whether the symbol's value is non-nil or nil. */
19137 car = XCAR (elt);
19138 if (EQ (car, QCeval))
19139 {
19140 /* An element of the form (:eval FORM) means evaluate FORM
19141 and use the result as mode line elements. */
19142
19143 if (risky)
19144 break;
19145
19146 if (CONSP (XCDR (elt)))
19147 {
19148 Lisp_Object spec;
19149 spec = safe_eval (XCAR (XCDR (elt)));
19150 n += display_mode_element (it, depth, field_width - n,
19151 precision - n, spec, props,
19152 risky);
19153 }
19154 }
19155 else if (EQ (car, QCpropertize))
19156 {
19157 /* An element of the form (:propertize ELT PROPS...)
19158 means display ELT but applying properties PROPS. */
19159
19160 if (risky)
19161 break;
19162
19163 if (CONSP (XCDR (elt)))
19164 n += display_mode_element (it, depth, field_width - n,
19165 precision - n, XCAR (XCDR (elt)),
19166 XCDR (XCDR (elt)), risky);
19167 }
19168 else if (SYMBOLP (car))
19169 {
19170 tem = Fboundp (car);
19171 elt = XCDR (elt);
19172 if (!CONSP (elt))
19173 goto invalid;
19174 /* elt is now the cdr, and we know it is a cons cell.
19175 Use its car if CAR has a non-nil value. */
19176 if (!NILP (tem))
19177 {
19178 tem = Fsymbol_value (car);
19179 if (!NILP (tem))
19180 {
19181 elt = XCAR (elt);
19182 goto tail_recurse;
19183 }
19184 }
19185 /* Symbol's value is nil (or symbol is unbound)
19186 Get the cddr of the original list
19187 and if possible find the caddr and use that. */
19188 elt = XCDR (elt);
19189 if (NILP (elt))
19190 break;
19191 else if (!CONSP (elt))
19192 goto invalid;
19193 elt = XCAR (elt);
19194 goto tail_recurse;
19195 }
19196 else if (INTEGERP (car))
19197 {
19198 register int lim = XINT (car);
19199 elt = XCDR (elt);
19200 if (lim < 0)
19201 {
19202 /* Negative int means reduce maximum width. */
19203 if (precision <= 0)
19204 precision = -lim;
19205 else
19206 precision = min (precision, -lim);
19207 }
19208 else if (lim > 0)
19209 {
19210 /* Padding specified. Don't let it be more than
19211 current maximum. */
19212 if (precision > 0)
19213 lim = min (precision, lim);
19214
19215 /* If that's more padding than already wanted, queue it.
19216 But don't reduce padding already specified even if
19217 that is beyond the current truncation point. */
19218 field_width = max (lim, field_width);
19219 }
19220 goto tail_recurse;
19221 }
19222 else if (STRINGP (car) || CONSP (car))
19223 {
19224 Lisp_Object halftail = elt;
19225 int len = 0;
19226
19227 while (CONSP (elt)
19228 && (precision <= 0 || n < precision))
19229 {
19230 n += display_mode_element (it, depth,
19231 /* Do padding only after the last
19232 element in the list. */
19233 (! CONSP (XCDR (elt))
19234 ? field_width - n
19235 : 0),
19236 precision - n, XCAR (elt),
19237 props, risky);
19238 elt = XCDR (elt);
19239 len++;
19240 if ((len & 1) == 0)
19241 halftail = XCDR (halftail);
19242 /* Check for cycle. */
19243 if (EQ (halftail, elt))
19244 break;
19245 }
19246 }
19247 }
19248 break;
19249
19250 default:
19251 invalid:
19252 elt = build_string ("*invalid*");
19253 goto tail_recurse;
19254 }
19255
19256 /* Pad to FIELD_WIDTH. */
19257 if (field_width > 0 && n < field_width)
19258 {
19259 switch (mode_line_target)
19260 {
19261 case MODE_LINE_NOPROP:
19262 case MODE_LINE_TITLE:
19263 n += store_mode_line_noprop ("", field_width - n, 0);
19264 break;
19265 case MODE_LINE_STRING:
19266 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19267 break;
19268 case MODE_LINE_DISPLAY:
19269 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19270 0, 0, 0);
19271 break;
19272 }
19273 }
19274
19275 return n;
19276 }
19277
19278 /* Store a mode-line string element in mode_line_string_list.
19279
19280 If STRING is non-null, display that C string. Otherwise, the Lisp
19281 string LISP_STRING is displayed.
19282
19283 FIELD_WIDTH is the minimum number of output glyphs to produce.
19284 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19285 with spaces. FIELD_WIDTH <= 0 means don't pad.
19286
19287 PRECISION is the maximum number of characters to output from
19288 STRING. PRECISION <= 0 means don't truncate the string.
19289
19290 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19291 properties to the string.
19292
19293 PROPS are the properties to add to the string.
19294 The mode_line_string_face face property is always added to the string.
19295 */
19296
19297 static int
19298 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19299 int field_width, int precision, Lisp_Object props)
19300 {
19301 EMACS_INT len;
19302 int n = 0;
19303
19304 if (string != NULL)
19305 {
19306 len = strlen (string);
19307 if (precision > 0 && len > precision)
19308 len = precision;
19309 lisp_string = make_string (string, len);
19310 if (NILP (props))
19311 props = mode_line_string_face_prop;
19312 else if (!NILP (mode_line_string_face))
19313 {
19314 Lisp_Object face = Fplist_get (props, Qface);
19315 props = Fcopy_sequence (props);
19316 if (NILP (face))
19317 face = mode_line_string_face;
19318 else
19319 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19320 props = Fplist_put (props, Qface, face);
19321 }
19322 Fadd_text_properties (make_number (0), make_number (len),
19323 props, lisp_string);
19324 }
19325 else
19326 {
19327 len = XFASTINT (Flength (lisp_string));
19328 if (precision > 0 && len > precision)
19329 {
19330 len = precision;
19331 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19332 precision = -1;
19333 }
19334 if (!NILP (mode_line_string_face))
19335 {
19336 Lisp_Object face;
19337 if (NILP (props))
19338 props = Ftext_properties_at (make_number (0), lisp_string);
19339 face = Fplist_get (props, Qface);
19340 if (NILP (face))
19341 face = mode_line_string_face;
19342 else
19343 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19344 props = Fcons (Qface, Fcons (face, Qnil));
19345 if (copy_string)
19346 lisp_string = Fcopy_sequence (lisp_string);
19347 }
19348 if (!NILP (props))
19349 Fadd_text_properties (make_number (0), make_number (len),
19350 props, lisp_string);
19351 }
19352
19353 if (len > 0)
19354 {
19355 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19356 n += len;
19357 }
19358
19359 if (field_width > len)
19360 {
19361 field_width -= len;
19362 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19363 if (!NILP (props))
19364 Fadd_text_properties (make_number (0), make_number (field_width),
19365 props, lisp_string);
19366 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19367 n += field_width;
19368 }
19369
19370 return n;
19371 }
19372
19373
19374 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19375 1, 4, 0,
19376 doc: /* Format a string out of a mode line format specification.
19377 First arg FORMAT specifies the mode line format (see `mode-line-format'
19378 for details) to use.
19379
19380 By default, the format is evaluated for the currently selected window.
19381
19382 Optional second arg FACE specifies the face property to put on all
19383 characters for which no face is specified. The value nil means the
19384 default face. The value t means whatever face the window's mode line
19385 currently uses (either `mode-line' or `mode-line-inactive',
19386 depending on whether the window is the selected window or not).
19387 An integer value means the value string has no text
19388 properties.
19389
19390 Optional third and fourth args WINDOW and BUFFER specify the window
19391 and buffer to use as the context for the formatting (defaults
19392 are the selected window and the WINDOW's buffer). */)
19393 (Lisp_Object format, Lisp_Object face,
19394 Lisp_Object window, Lisp_Object buffer)
19395 {
19396 struct it it;
19397 int len;
19398 struct window *w;
19399 struct buffer *old_buffer = NULL;
19400 int face_id;
19401 int no_props = INTEGERP (face);
19402 int count = SPECPDL_INDEX ();
19403 Lisp_Object str;
19404 int string_start = 0;
19405
19406 if (NILP (window))
19407 window = selected_window;
19408 CHECK_WINDOW (window);
19409 w = XWINDOW (window);
19410
19411 if (NILP (buffer))
19412 buffer = w->buffer;
19413 CHECK_BUFFER (buffer);
19414
19415 /* Make formatting the modeline a non-op when noninteractive, otherwise
19416 there will be problems later caused by a partially initialized frame. */
19417 if (NILP (format) || noninteractive)
19418 return empty_unibyte_string;
19419
19420 if (no_props)
19421 face = Qnil;
19422
19423 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19424 : EQ (face, Qt) ? (EQ (window, selected_window)
19425 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19426 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19427 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19428 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19429 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19430 : DEFAULT_FACE_ID;
19431
19432 if (XBUFFER (buffer) != current_buffer)
19433 old_buffer = current_buffer;
19434
19435 /* Save things including mode_line_proptrans_alist,
19436 and set that to nil so that we don't alter the outer value. */
19437 record_unwind_protect (unwind_format_mode_line,
19438 format_mode_line_unwind_data
19439 (old_buffer, selected_window, 1));
19440 mode_line_proptrans_alist = Qnil;
19441
19442 Fselect_window (window, Qt);
19443 if (old_buffer)
19444 set_buffer_internal_1 (XBUFFER (buffer));
19445
19446 init_iterator (&it, w, -1, -1, NULL, face_id);
19447
19448 if (no_props)
19449 {
19450 mode_line_target = MODE_LINE_NOPROP;
19451 mode_line_string_face_prop = Qnil;
19452 mode_line_string_list = Qnil;
19453 string_start = MODE_LINE_NOPROP_LEN (0);
19454 }
19455 else
19456 {
19457 mode_line_target = MODE_LINE_STRING;
19458 mode_line_string_list = Qnil;
19459 mode_line_string_face = face;
19460 mode_line_string_face_prop
19461 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19462 }
19463
19464 push_kboard (FRAME_KBOARD (it.f));
19465 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19466 pop_kboard ();
19467
19468 if (no_props)
19469 {
19470 len = MODE_LINE_NOPROP_LEN (string_start);
19471 str = make_string (mode_line_noprop_buf + string_start, len);
19472 }
19473 else
19474 {
19475 mode_line_string_list = Fnreverse (mode_line_string_list);
19476 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19477 empty_unibyte_string);
19478 }
19479
19480 unbind_to (count, Qnil);
19481 return str;
19482 }
19483
19484 /* Write a null-terminated, right justified decimal representation of
19485 the positive integer D to BUF using a minimal field width WIDTH. */
19486
19487 static void
19488 pint2str (register char *buf, register int width, register EMACS_INT d)
19489 {
19490 register char *p = buf;
19491
19492 if (d <= 0)
19493 *p++ = '0';
19494 else
19495 {
19496 while (d > 0)
19497 {
19498 *p++ = d % 10 + '0';
19499 d /= 10;
19500 }
19501 }
19502
19503 for (width -= (int) (p - buf); width > 0; --width)
19504 *p++ = ' ';
19505 *p-- = '\0';
19506 while (p > buf)
19507 {
19508 d = *buf;
19509 *buf++ = *p;
19510 *p-- = d;
19511 }
19512 }
19513
19514 /* Write a null-terminated, right justified decimal and "human
19515 readable" representation of the nonnegative integer D to BUF using
19516 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19517
19518 static const char power_letter[] =
19519 {
19520 0, /* no letter */
19521 'k', /* kilo */
19522 'M', /* mega */
19523 'G', /* giga */
19524 'T', /* tera */
19525 'P', /* peta */
19526 'E', /* exa */
19527 'Z', /* zetta */
19528 'Y' /* yotta */
19529 };
19530
19531 static void
19532 pint2hrstr (char *buf, int width, EMACS_INT d)
19533 {
19534 /* We aim to represent the nonnegative integer D as
19535 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19536 EMACS_INT quotient = d;
19537 int remainder = 0;
19538 /* -1 means: do not use TENTHS. */
19539 int tenths = -1;
19540 int exponent = 0;
19541
19542 /* Length of QUOTIENT.TENTHS as a string. */
19543 int length;
19544
19545 char * psuffix;
19546 char * p;
19547
19548 if (1000 <= quotient)
19549 {
19550 /* Scale to the appropriate EXPONENT. */
19551 do
19552 {
19553 remainder = quotient % 1000;
19554 quotient /= 1000;
19555 exponent++;
19556 }
19557 while (1000 <= quotient);
19558
19559 /* Round to nearest and decide whether to use TENTHS or not. */
19560 if (quotient <= 9)
19561 {
19562 tenths = remainder / 100;
19563 if (50 <= remainder % 100)
19564 {
19565 if (tenths < 9)
19566 tenths++;
19567 else
19568 {
19569 quotient++;
19570 if (quotient == 10)
19571 tenths = -1;
19572 else
19573 tenths = 0;
19574 }
19575 }
19576 }
19577 else
19578 if (500 <= remainder)
19579 {
19580 if (quotient < 999)
19581 quotient++;
19582 else
19583 {
19584 quotient = 1;
19585 exponent++;
19586 tenths = 0;
19587 }
19588 }
19589 }
19590
19591 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19592 if (tenths == -1 && quotient <= 99)
19593 if (quotient <= 9)
19594 length = 1;
19595 else
19596 length = 2;
19597 else
19598 length = 3;
19599 p = psuffix = buf + max (width, length);
19600
19601 /* Print EXPONENT. */
19602 *psuffix++ = power_letter[exponent];
19603 *psuffix = '\0';
19604
19605 /* Print TENTHS. */
19606 if (tenths >= 0)
19607 {
19608 *--p = '0' + tenths;
19609 *--p = '.';
19610 }
19611
19612 /* Print QUOTIENT. */
19613 do
19614 {
19615 int digit = quotient % 10;
19616 *--p = '0' + digit;
19617 }
19618 while ((quotient /= 10) != 0);
19619
19620 /* Print leading spaces. */
19621 while (buf < p)
19622 *--p = ' ';
19623 }
19624
19625 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19626 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19627 type of CODING_SYSTEM. Return updated pointer into BUF. */
19628
19629 static unsigned char invalid_eol_type[] = "(*invalid*)";
19630
19631 static char *
19632 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19633 {
19634 Lisp_Object val;
19635 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19636 const unsigned char *eol_str;
19637 int eol_str_len;
19638 /* The EOL conversion we are using. */
19639 Lisp_Object eoltype;
19640
19641 val = CODING_SYSTEM_SPEC (coding_system);
19642 eoltype = Qnil;
19643
19644 if (!VECTORP (val)) /* Not yet decided. */
19645 {
19646 if (multibyte)
19647 *buf++ = '-';
19648 if (eol_flag)
19649 eoltype = eol_mnemonic_undecided;
19650 /* Don't mention EOL conversion if it isn't decided. */
19651 }
19652 else
19653 {
19654 Lisp_Object attrs;
19655 Lisp_Object eolvalue;
19656
19657 attrs = AREF (val, 0);
19658 eolvalue = AREF (val, 2);
19659
19660 if (multibyte)
19661 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19662
19663 if (eol_flag)
19664 {
19665 /* The EOL conversion that is normal on this system. */
19666
19667 if (NILP (eolvalue)) /* Not yet decided. */
19668 eoltype = eol_mnemonic_undecided;
19669 else if (VECTORP (eolvalue)) /* Not yet decided. */
19670 eoltype = eol_mnemonic_undecided;
19671 else /* eolvalue is Qunix, Qdos, or Qmac. */
19672 eoltype = (EQ (eolvalue, Qunix)
19673 ? eol_mnemonic_unix
19674 : (EQ (eolvalue, Qdos) == 1
19675 ? eol_mnemonic_dos : eol_mnemonic_mac));
19676 }
19677 }
19678
19679 if (eol_flag)
19680 {
19681 /* Mention the EOL conversion if it is not the usual one. */
19682 if (STRINGP (eoltype))
19683 {
19684 eol_str = SDATA (eoltype);
19685 eol_str_len = SBYTES (eoltype);
19686 }
19687 else if (CHARACTERP (eoltype))
19688 {
19689 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19690 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19691 eol_str = tmp;
19692 }
19693 else
19694 {
19695 eol_str = invalid_eol_type;
19696 eol_str_len = sizeof (invalid_eol_type) - 1;
19697 }
19698 memcpy (buf, eol_str, eol_str_len);
19699 buf += eol_str_len;
19700 }
19701
19702 return buf;
19703 }
19704
19705 /* Return a string for the output of a mode line %-spec for window W,
19706 generated by character C. FIELD_WIDTH > 0 means pad the string
19707 returned with spaces to that value. Return a Lisp string in
19708 *STRING if the resulting string is taken from that Lisp string.
19709
19710 Note we operate on the current buffer for most purposes,
19711 the exception being w->base_line_pos. */
19712
19713 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19714
19715 static const char *
19716 decode_mode_spec (struct window *w, register int c, int field_width,
19717 Lisp_Object *string)
19718 {
19719 Lisp_Object obj;
19720 struct frame *f = XFRAME (WINDOW_FRAME (w));
19721 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19722 struct buffer *b = current_buffer;
19723
19724 obj = Qnil;
19725 *string = Qnil;
19726
19727 switch (c)
19728 {
19729 case '*':
19730 if (!NILP (BVAR (b, read_only)))
19731 return "%";
19732 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19733 return "*";
19734 return "-";
19735
19736 case '+':
19737 /* This differs from %* only for a modified read-only buffer. */
19738 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19739 return "*";
19740 if (!NILP (BVAR (b, read_only)))
19741 return "%";
19742 return "-";
19743
19744 case '&':
19745 /* This differs from %* in ignoring read-only-ness. */
19746 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19747 return "*";
19748 return "-";
19749
19750 case '%':
19751 return "%";
19752
19753 case '[':
19754 {
19755 int i;
19756 char *p;
19757
19758 if (command_loop_level > 5)
19759 return "[[[... ";
19760 p = decode_mode_spec_buf;
19761 for (i = 0; i < command_loop_level; i++)
19762 *p++ = '[';
19763 *p = 0;
19764 return decode_mode_spec_buf;
19765 }
19766
19767 case ']':
19768 {
19769 int i;
19770 char *p;
19771
19772 if (command_loop_level > 5)
19773 return " ...]]]";
19774 p = decode_mode_spec_buf;
19775 for (i = 0; i < command_loop_level; i++)
19776 *p++ = ']';
19777 *p = 0;
19778 return decode_mode_spec_buf;
19779 }
19780
19781 case '-':
19782 {
19783 register int i;
19784
19785 /* Let lots_of_dashes be a string of infinite length. */
19786 if (mode_line_target == MODE_LINE_NOPROP ||
19787 mode_line_target == MODE_LINE_STRING)
19788 return "--";
19789 if (field_width <= 0
19790 || field_width > sizeof (lots_of_dashes))
19791 {
19792 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19793 decode_mode_spec_buf[i] = '-';
19794 decode_mode_spec_buf[i] = '\0';
19795 return decode_mode_spec_buf;
19796 }
19797 else
19798 return lots_of_dashes;
19799 }
19800
19801 case 'b':
19802 obj = BVAR (b, name);
19803 break;
19804
19805 case 'c':
19806 /* %c and %l are ignored in `frame-title-format'.
19807 (In redisplay_internal, the frame title is drawn _before_ the
19808 windows are updated, so the stuff which depends on actual
19809 window contents (such as %l) may fail to render properly, or
19810 even crash emacs.) */
19811 if (mode_line_target == MODE_LINE_TITLE)
19812 return "";
19813 else
19814 {
19815 EMACS_INT col = current_column ();
19816 w->column_number_displayed = make_number (col);
19817 pint2str (decode_mode_spec_buf, field_width, col);
19818 return decode_mode_spec_buf;
19819 }
19820
19821 case 'e':
19822 #ifndef SYSTEM_MALLOC
19823 {
19824 if (NILP (Vmemory_full))
19825 return "";
19826 else
19827 return "!MEM FULL! ";
19828 }
19829 #else
19830 return "";
19831 #endif
19832
19833 case 'F':
19834 /* %F displays the frame name. */
19835 if (!NILP (f->title))
19836 return SSDATA (f->title);
19837 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19838 return SSDATA (f->name);
19839 return "Emacs";
19840
19841 case 'f':
19842 obj = BVAR (b, filename);
19843 break;
19844
19845 case 'i':
19846 {
19847 EMACS_INT size = ZV - BEGV;
19848 pint2str (decode_mode_spec_buf, field_width, size);
19849 return decode_mode_spec_buf;
19850 }
19851
19852 case 'I':
19853 {
19854 EMACS_INT size = ZV - BEGV;
19855 pint2hrstr (decode_mode_spec_buf, field_width, size);
19856 return decode_mode_spec_buf;
19857 }
19858
19859 case 'l':
19860 {
19861 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19862 EMACS_INT topline, nlines, height;
19863 EMACS_INT junk;
19864
19865 /* %c and %l are ignored in `frame-title-format'. */
19866 if (mode_line_target == MODE_LINE_TITLE)
19867 return "";
19868
19869 startpos = XMARKER (w->start)->charpos;
19870 startpos_byte = marker_byte_position (w->start);
19871 height = WINDOW_TOTAL_LINES (w);
19872
19873 /* If we decided that this buffer isn't suitable for line numbers,
19874 don't forget that too fast. */
19875 if (EQ (w->base_line_pos, w->buffer))
19876 goto no_value;
19877 /* But do forget it, if the window shows a different buffer now. */
19878 else if (BUFFERP (w->base_line_pos))
19879 w->base_line_pos = Qnil;
19880
19881 /* If the buffer is very big, don't waste time. */
19882 if (INTEGERP (Vline_number_display_limit)
19883 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19884 {
19885 w->base_line_pos = Qnil;
19886 w->base_line_number = Qnil;
19887 goto no_value;
19888 }
19889
19890 if (INTEGERP (w->base_line_number)
19891 && INTEGERP (w->base_line_pos)
19892 && XFASTINT (w->base_line_pos) <= startpos)
19893 {
19894 line = XFASTINT (w->base_line_number);
19895 linepos = XFASTINT (w->base_line_pos);
19896 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19897 }
19898 else
19899 {
19900 line = 1;
19901 linepos = BUF_BEGV (b);
19902 linepos_byte = BUF_BEGV_BYTE (b);
19903 }
19904
19905 /* Count lines from base line to window start position. */
19906 nlines = display_count_lines (linepos_byte,
19907 startpos_byte,
19908 startpos, &junk);
19909
19910 topline = nlines + line;
19911
19912 /* Determine a new base line, if the old one is too close
19913 or too far away, or if we did not have one.
19914 "Too close" means it's plausible a scroll-down would
19915 go back past it. */
19916 if (startpos == BUF_BEGV (b))
19917 {
19918 w->base_line_number = make_number (topline);
19919 w->base_line_pos = make_number (BUF_BEGV (b));
19920 }
19921 else if (nlines < height + 25 || nlines > height * 3 + 50
19922 || linepos == BUF_BEGV (b))
19923 {
19924 EMACS_INT limit = BUF_BEGV (b);
19925 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19926 EMACS_INT position;
19927 EMACS_INT distance =
19928 (height * 2 + 30) * line_number_display_limit_width;
19929
19930 if (startpos - distance > limit)
19931 {
19932 limit = startpos - distance;
19933 limit_byte = CHAR_TO_BYTE (limit);
19934 }
19935
19936 nlines = display_count_lines (startpos_byte,
19937 limit_byte,
19938 - (height * 2 + 30),
19939 &position);
19940 /* If we couldn't find the lines we wanted within
19941 line_number_display_limit_width chars per line,
19942 give up on line numbers for this window. */
19943 if (position == limit_byte && limit == startpos - distance)
19944 {
19945 w->base_line_pos = w->buffer;
19946 w->base_line_number = Qnil;
19947 goto no_value;
19948 }
19949
19950 w->base_line_number = make_number (topline - nlines);
19951 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19952 }
19953
19954 /* Now count lines from the start pos to point. */
19955 nlines = display_count_lines (startpos_byte,
19956 PT_BYTE, PT, &junk);
19957
19958 /* Record that we did display the line number. */
19959 line_number_displayed = 1;
19960
19961 /* Make the string to show. */
19962 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19963 return decode_mode_spec_buf;
19964 no_value:
19965 {
19966 char* p = decode_mode_spec_buf;
19967 int pad = field_width - 2;
19968 while (pad-- > 0)
19969 *p++ = ' ';
19970 *p++ = '?';
19971 *p++ = '?';
19972 *p = '\0';
19973 return decode_mode_spec_buf;
19974 }
19975 }
19976 break;
19977
19978 case 'm':
19979 obj = BVAR (b, mode_name);
19980 break;
19981
19982 case 'n':
19983 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19984 return " Narrow";
19985 break;
19986
19987 case 'p':
19988 {
19989 EMACS_INT pos = marker_position (w->start);
19990 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19991
19992 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19993 {
19994 if (pos <= BUF_BEGV (b))
19995 return "All";
19996 else
19997 return "Bottom";
19998 }
19999 else if (pos <= BUF_BEGV (b))
20000 return "Top";
20001 else
20002 {
20003 if (total > 1000000)
20004 /* Do it differently for a large value, to avoid overflow. */
20005 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20006 else
20007 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20008 /* We can't normally display a 3-digit number,
20009 so get us a 2-digit number that is close. */
20010 if (total == 100)
20011 total = 99;
20012 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20013 return decode_mode_spec_buf;
20014 }
20015 }
20016
20017 /* Display percentage of size above the bottom of the screen. */
20018 case 'P':
20019 {
20020 EMACS_INT toppos = marker_position (w->start);
20021 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20022 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20023
20024 if (botpos >= BUF_ZV (b))
20025 {
20026 if (toppos <= BUF_BEGV (b))
20027 return "All";
20028 else
20029 return "Bottom";
20030 }
20031 else
20032 {
20033 if (total > 1000000)
20034 /* Do it differently for a large value, to avoid overflow. */
20035 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20036 else
20037 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20038 /* We can't normally display a 3-digit number,
20039 so get us a 2-digit number that is close. */
20040 if (total == 100)
20041 total = 99;
20042 if (toppos <= BUF_BEGV (b))
20043 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20044 else
20045 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20046 return decode_mode_spec_buf;
20047 }
20048 }
20049
20050 case 's':
20051 /* status of process */
20052 obj = Fget_buffer_process (Fcurrent_buffer ());
20053 if (NILP (obj))
20054 return "no process";
20055 #ifndef MSDOS
20056 obj = Fsymbol_name (Fprocess_status (obj));
20057 #endif
20058 break;
20059
20060 case '@':
20061 {
20062 int count = inhibit_garbage_collection ();
20063 Lisp_Object val = call1 (intern ("file-remote-p"),
20064 BVAR (current_buffer, directory));
20065 unbind_to (count, Qnil);
20066
20067 if (NILP (val))
20068 return "-";
20069 else
20070 return "@";
20071 }
20072
20073 case 't': /* indicate TEXT or BINARY */
20074 return "T";
20075
20076 case 'z':
20077 /* coding-system (not including end-of-line format) */
20078 case 'Z':
20079 /* coding-system (including end-of-line type) */
20080 {
20081 int eol_flag = (c == 'Z');
20082 char *p = decode_mode_spec_buf;
20083
20084 if (! FRAME_WINDOW_P (f))
20085 {
20086 /* No need to mention EOL here--the terminal never needs
20087 to do EOL conversion. */
20088 p = decode_mode_spec_coding (CODING_ID_NAME
20089 (FRAME_KEYBOARD_CODING (f)->id),
20090 p, 0);
20091 p = decode_mode_spec_coding (CODING_ID_NAME
20092 (FRAME_TERMINAL_CODING (f)->id),
20093 p, 0);
20094 }
20095 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20096 p, eol_flag);
20097
20098 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20099 #ifdef subprocesses
20100 obj = Fget_buffer_process (Fcurrent_buffer ());
20101 if (PROCESSP (obj))
20102 {
20103 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20104 p, eol_flag);
20105 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20106 p, eol_flag);
20107 }
20108 #endif /* subprocesses */
20109 #endif /* 0 */
20110 *p = 0;
20111 return decode_mode_spec_buf;
20112 }
20113 }
20114
20115 if (STRINGP (obj))
20116 {
20117 *string = obj;
20118 return SSDATA (obj);
20119 }
20120 else
20121 return "";
20122 }
20123
20124
20125 /* Count up to COUNT lines starting from START_BYTE.
20126 But don't go beyond LIMIT_BYTE.
20127 Return the number of lines thus found (always nonnegative).
20128
20129 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20130
20131 static EMACS_INT
20132 display_count_lines (EMACS_INT start_byte,
20133 EMACS_INT limit_byte, EMACS_INT count,
20134 EMACS_INT *byte_pos_ptr)
20135 {
20136 register unsigned char *cursor;
20137 unsigned char *base;
20138
20139 register EMACS_INT ceiling;
20140 register unsigned char *ceiling_addr;
20141 EMACS_INT orig_count = count;
20142
20143 /* If we are not in selective display mode,
20144 check only for newlines. */
20145 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20146 && !INTEGERP (BVAR (current_buffer, selective_display)));
20147
20148 if (count > 0)
20149 {
20150 while (start_byte < limit_byte)
20151 {
20152 ceiling = BUFFER_CEILING_OF (start_byte);
20153 ceiling = min (limit_byte - 1, ceiling);
20154 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20155 base = (cursor = BYTE_POS_ADDR (start_byte));
20156 while (1)
20157 {
20158 if (selective_display)
20159 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20160 ;
20161 else
20162 while (*cursor != '\n' && ++cursor != ceiling_addr)
20163 ;
20164
20165 if (cursor != ceiling_addr)
20166 {
20167 if (--count == 0)
20168 {
20169 start_byte += cursor - base + 1;
20170 *byte_pos_ptr = start_byte;
20171 return orig_count;
20172 }
20173 else
20174 if (++cursor == ceiling_addr)
20175 break;
20176 }
20177 else
20178 break;
20179 }
20180 start_byte += cursor - base;
20181 }
20182 }
20183 else
20184 {
20185 while (start_byte > limit_byte)
20186 {
20187 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20188 ceiling = max (limit_byte, ceiling);
20189 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20190 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20191 while (1)
20192 {
20193 if (selective_display)
20194 while (--cursor != ceiling_addr
20195 && *cursor != '\n' && *cursor != 015)
20196 ;
20197 else
20198 while (--cursor != ceiling_addr && *cursor != '\n')
20199 ;
20200
20201 if (cursor != ceiling_addr)
20202 {
20203 if (++count == 0)
20204 {
20205 start_byte += cursor - base + 1;
20206 *byte_pos_ptr = start_byte;
20207 /* When scanning backwards, we should
20208 not count the newline posterior to which we stop. */
20209 return - orig_count - 1;
20210 }
20211 }
20212 else
20213 break;
20214 }
20215 /* Here we add 1 to compensate for the last decrement
20216 of CURSOR, which took it past the valid range. */
20217 start_byte += cursor - base + 1;
20218 }
20219 }
20220
20221 *byte_pos_ptr = limit_byte;
20222
20223 if (count < 0)
20224 return - orig_count + count;
20225 return orig_count - count;
20226
20227 }
20228
20229
20230 \f
20231 /***********************************************************************
20232 Displaying strings
20233 ***********************************************************************/
20234
20235 /* Display a NUL-terminated string, starting with index START.
20236
20237 If STRING is non-null, display that C string. Otherwise, the Lisp
20238 string LISP_STRING is displayed. There's a case that STRING is
20239 non-null and LISP_STRING is not nil. It means STRING is a string
20240 data of LISP_STRING. In that case, we display LISP_STRING while
20241 ignoring its text properties.
20242
20243 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20244 FACE_STRING. Display STRING or LISP_STRING with the face at
20245 FACE_STRING_POS in FACE_STRING:
20246
20247 Display the string in the environment given by IT, but use the
20248 standard display table, temporarily.
20249
20250 FIELD_WIDTH is the minimum number of output glyphs to produce.
20251 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20252 with spaces. If STRING has more characters, more than FIELD_WIDTH
20253 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20254
20255 PRECISION is the maximum number of characters to output from
20256 STRING. PRECISION < 0 means don't truncate the string.
20257
20258 This is roughly equivalent to printf format specifiers:
20259
20260 FIELD_WIDTH PRECISION PRINTF
20261 ----------------------------------------
20262 -1 -1 %s
20263 -1 10 %.10s
20264 10 -1 %10s
20265 20 10 %20.10s
20266
20267 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20268 display them, and < 0 means obey the current buffer's value of
20269 enable_multibyte_characters.
20270
20271 Value is the number of columns displayed. */
20272
20273 static int
20274 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20275 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20276 int field_width, int precision, int max_x, int multibyte)
20277 {
20278 int hpos_at_start = it->hpos;
20279 int saved_face_id = it->face_id;
20280 struct glyph_row *row = it->glyph_row;
20281 EMACS_INT it_charpos;
20282
20283 /* Initialize the iterator IT for iteration over STRING beginning
20284 with index START. */
20285 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20286 precision, field_width, multibyte);
20287 if (string && STRINGP (lisp_string))
20288 /* LISP_STRING is the one returned by decode_mode_spec. We should
20289 ignore its text properties. */
20290 it->stop_charpos = it->end_charpos;
20291
20292 /* If displaying STRING, set up the face of the iterator from
20293 FACE_STRING, if that's given. */
20294 if (STRINGP (face_string))
20295 {
20296 EMACS_INT endptr;
20297 struct face *face;
20298
20299 it->face_id
20300 = face_at_string_position (it->w, face_string, face_string_pos,
20301 0, it->region_beg_charpos,
20302 it->region_end_charpos,
20303 &endptr, it->base_face_id, 0);
20304 face = FACE_FROM_ID (it->f, it->face_id);
20305 it->face_box_p = face->box != FACE_NO_BOX;
20306 }
20307
20308 /* Set max_x to the maximum allowed X position. Don't let it go
20309 beyond the right edge of the window. */
20310 if (max_x <= 0)
20311 max_x = it->last_visible_x;
20312 else
20313 max_x = min (max_x, it->last_visible_x);
20314
20315 /* Skip over display elements that are not visible. because IT->w is
20316 hscrolled. */
20317 if (it->current_x < it->first_visible_x)
20318 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20319 MOVE_TO_POS | MOVE_TO_X);
20320
20321 row->ascent = it->max_ascent;
20322 row->height = it->max_ascent + it->max_descent;
20323 row->phys_ascent = it->max_phys_ascent;
20324 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20325 row->extra_line_spacing = it->max_extra_line_spacing;
20326
20327 if (STRINGP (it->string))
20328 it_charpos = IT_STRING_CHARPOS (*it);
20329 else
20330 it_charpos = IT_CHARPOS (*it);
20331
20332 /* This condition is for the case that we are called with current_x
20333 past last_visible_x. */
20334 while (it->current_x < max_x)
20335 {
20336 int x_before, x, n_glyphs_before, i, nglyphs;
20337
20338 /* Get the next display element. */
20339 if (!get_next_display_element (it))
20340 break;
20341
20342 /* Produce glyphs. */
20343 x_before = it->current_x;
20344 n_glyphs_before = row->used[TEXT_AREA];
20345 PRODUCE_GLYPHS (it);
20346
20347 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20348 i = 0;
20349 x = x_before;
20350 while (i < nglyphs)
20351 {
20352 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20353
20354 if (it->line_wrap != TRUNCATE
20355 && x + glyph->pixel_width > max_x)
20356 {
20357 /* End of continued line or max_x reached. */
20358 if (CHAR_GLYPH_PADDING_P (*glyph))
20359 {
20360 /* A wide character is unbreakable. */
20361 if (row->reversed_p)
20362 unproduce_glyphs (it, row->used[TEXT_AREA]
20363 - n_glyphs_before);
20364 row->used[TEXT_AREA] = n_glyphs_before;
20365 it->current_x = x_before;
20366 }
20367 else
20368 {
20369 if (row->reversed_p)
20370 unproduce_glyphs (it, row->used[TEXT_AREA]
20371 - (n_glyphs_before + i));
20372 row->used[TEXT_AREA] = n_glyphs_before + i;
20373 it->current_x = x;
20374 }
20375 break;
20376 }
20377 else if (x + glyph->pixel_width >= it->first_visible_x)
20378 {
20379 /* Glyph is at least partially visible. */
20380 ++it->hpos;
20381 if (x < it->first_visible_x)
20382 row->x = x - it->first_visible_x;
20383 }
20384 else
20385 {
20386 /* Glyph is off the left margin of the display area.
20387 Should not happen. */
20388 abort ();
20389 }
20390
20391 row->ascent = max (row->ascent, it->max_ascent);
20392 row->height = max (row->height, it->max_ascent + it->max_descent);
20393 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20394 row->phys_height = max (row->phys_height,
20395 it->max_phys_ascent + it->max_phys_descent);
20396 row->extra_line_spacing = max (row->extra_line_spacing,
20397 it->max_extra_line_spacing);
20398 x += glyph->pixel_width;
20399 ++i;
20400 }
20401
20402 /* Stop if max_x reached. */
20403 if (i < nglyphs)
20404 break;
20405
20406 /* Stop at line ends. */
20407 if (ITERATOR_AT_END_OF_LINE_P (it))
20408 {
20409 it->continuation_lines_width = 0;
20410 break;
20411 }
20412
20413 set_iterator_to_next (it, 1);
20414 if (STRINGP (it->string))
20415 it_charpos = IT_STRING_CHARPOS (*it);
20416 else
20417 it_charpos = IT_CHARPOS (*it);
20418
20419 /* Stop if truncating at the right edge. */
20420 if (it->line_wrap == TRUNCATE
20421 && it->current_x >= it->last_visible_x)
20422 {
20423 /* Add truncation mark, but don't do it if the line is
20424 truncated at a padding space. */
20425 if (it_charpos < it->string_nchars)
20426 {
20427 if (!FRAME_WINDOW_P (it->f))
20428 {
20429 int ii, n;
20430
20431 if (it->current_x > it->last_visible_x)
20432 {
20433 if (!row->reversed_p)
20434 {
20435 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20436 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20437 break;
20438 }
20439 else
20440 {
20441 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
20442 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20443 break;
20444 unproduce_glyphs (it, ii + 1);
20445 ii = row->used[TEXT_AREA] - (ii + 1);
20446 }
20447 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20448 {
20449 row->used[TEXT_AREA] = ii;
20450 produce_special_glyphs (it, IT_TRUNCATION);
20451 }
20452 }
20453 produce_special_glyphs (it, IT_TRUNCATION);
20454 }
20455 row->truncated_on_right_p = 1;
20456 }
20457 break;
20458 }
20459 }
20460
20461 /* Maybe insert a truncation at the left. */
20462 if (it->first_visible_x
20463 && it_charpos > 0)
20464 {
20465 if (!FRAME_WINDOW_P (it->f))
20466 insert_left_trunc_glyphs (it);
20467 row->truncated_on_left_p = 1;
20468 }
20469
20470 it->face_id = saved_face_id;
20471
20472 /* Value is number of columns displayed. */
20473 return it->hpos - hpos_at_start;
20474 }
20475
20476
20477 \f
20478 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20479 appears as an element of LIST or as the car of an element of LIST.
20480 If PROPVAL is a list, compare each element against LIST in that
20481 way, and return 1/2 if any element of PROPVAL is found in LIST.
20482 Otherwise return 0. This function cannot quit.
20483 The return value is 2 if the text is invisible but with an ellipsis
20484 and 1 if it's invisible and without an ellipsis. */
20485
20486 int
20487 invisible_p (register Lisp_Object propval, Lisp_Object list)
20488 {
20489 register Lisp_Object tail, proptail;
20490
20491 for (tail = list; CONSP (tail); tail = XCDR (tail))
20492 {
20493 register Lisp_Object tem;
20494 tem = XCAR (tail);
20495 if (EQ (propval, tem))
20496 return 1;
20497 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20498 return NILP (XCDR (tem)) ? 1 : 2;
20499 }
20500
20501 if (CONSP (propval))
20502 {
20503 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20504 {
20505 Lisp_Object propelt;
20506 propelt = XCAR (proptail);
20507 for (tail = list; CONSP (tail); tail = XCDR (tail))
20508 {
20509 register Lisp_Object tem;
20510 tem = XCAR (tail);
20511 if (EQ (propelt, tem))
20512 return 1;
20513 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20514 return NILP (XCDR (tem)) ? 1 : 2;
20515 }
20516 }
20517 }
20518
20519 return 0;
20520 }
20521
20522 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20523 doc: /* Non-nil if the property makes the text invisible.
20524 POS-OR-PROP can be a marker or number, in which case it is taken to be
20525 a position in the current buffer and the value of the `invisible' property
20526 is checked; or it can be some other value, which is then presumed to be the
20527 value of the `invisible' property of the text of interest.
20528 The non-nil value returned can be t for truly invisible text or something
20529 else if the text is replaced by an ellipsis. */)
20530 (Lisp_Object pos_or_prop)
20531 {
20532 Lisp_Object prop
20533 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20534 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20535 : pos_or_prop);
20536 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20537 return (invis == 0 ? Qnil
20538 : invis == 1 ? Qt
20539 : make_number (invis));
20540 }
20541
20542 /* Calculate a width or height in pixels from a specification using
20543 the following elements:
20544
20545 SPEC ::=
20546 NUM - a (fractional) multiple of the default font width/height
20547 (NUM) - specifies exactly NUM pixels
20548 UNIT - a fixed number of pixels, see below.
20549 ELEMENT - size of a display element in pixels, see below.
20550 (NUM . SPEC) - equals NUM * SPEC
20551 (+ SPEC SPEC ...) - add pixel values
20552 (- SPEC SPEC ...) - subtract pixel values
20553 (- SPEC) - negate pixel value
20554
20555 NUM ::=
20556 INT or FLOAT - a number constant
20557 SYMBOL - use symbol's (buffer local) variable binding.
20558
20559 UNIT ::=
20560 in - pixels per inch *)
20561 mm - pixels per 1/1000 meter *)
20562 cm - pixels per 1/100 meter *)
20563 width - width of current font in pixels.
20564 height - height of current font in pixels.
20565
20566 *) using the ratio(s) defined in display-pixels-per-inch.
20567
20568 ELEMENT ::=
20569
20570 left-fringe - left fringe width in pixels
20571 right-fringe - right fringe width in pixels
20572
20573 left-margin - left margin width in pixels
20574 right-margin - right margin width in pixels
20575
20576 scroll-bar - scroll-bar area width in pixels
20577
20578 Examples:
20579
20580 Pixels corresponding to 5 inches:
20581 (5 . in)
20582
20583 Total width of non-text areas on left side of window (if scroll-bar is on left):
20584 '(space :width (+ left-fringe left-margin scroll-bar))
20585
20586 Align to first text column (in header line):
20587 '(space :align-to 0)
20588
20589 Align to middle of text area minus half the width of variable `my-image'
20590 containing a loaded image:
20591 '(space :align-to (0.5 . (- text my-image)))
20592
20593 Width of left margin minus width of 1 character in the default font:
20594 '(space :width (- left-margin 1))
20595
20596 Width of left margin minus width of 2 characters in the current font:
20597 '(space :width (- left-margin (2 . width)))
20598
20599 Center 1 character over left-margin (in header line):
20600 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20601
20602 Different ways to express width of left fringe plus left margin minus one pixel:
20603 '(space :width (- (+ left-fringe left-margin) (1)))
20604 '(space :width (+ left-fringe left-margin (- (1))))
20605 '(space :width (+ left-fringe left-margin (-1)))
20606
20607 */
20608
20609 #define NUMVAL(X) \
20610 ((INTEGERP (X) || FLOATP (X)) \
20611 ? XFLOATINT (X) \
20612 : - 1)
20613
20614 int
20615 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20616 struct font *font, int width_p, int *align_to)
20617 {
20618 double pixels;
20619
20620 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20621 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20622
20623 if (NILP (prop))
20624 return OK_PIXELS (0);
20625
20626 xassert (FRAME_LIVE_P (it->f));
20627
20628 if (SYMBOLP (prop))
20629 {
20630 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20631 {
20632 char *unit = SSDATA (SYMBOL_NAME (prop));
20633
20634 if (unit[0] == 'i' && unit[1] == 'n')
20635 pixels = 1.0;
20636 else if (unit[0] == 'm' && unit[1] == 'm')
20637 pixels = 25.4;
20638 else if (unit[0] == 'c' && unit[1] == 'm')
20639 pixels = 2.54;
20640 else
20641 pixels = 0;
20642 if (pixels > 0)
20643 {
20644 double ppi;
20645 #ifdef HAVE_WINDOW_SYSTEM
20646 if (FRAME_WINDOW_P (it->f)
20647 && (ppi = (width_p
20648 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20649 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20650 ppi > 0))
20651 return OK_PIXELS (ppi / pixels);
20652 #endif
20653
20654 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20655 || (CONSP (Vdisplay_pixels_per_inch)
20656 && (ppi = (width_p
20657 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20658 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20659 ppi > 0)))
20660 return OK_PIXELS (ppi / pixels);
20661
20662 return 0;
20663 }
20664 }
20665
20666 #ifdef HAVE_WINDOW_SYSTEM
20667 if (EQ (prop, Qheight))
20668 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20669 if (EQ (prop, Qwidth))
20670 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20671 #else
20672 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20673 return OK_PIXELS (1);
20674 #endif
20675
20676 if (EQ (prop, Qtext))
20677 return OK_PIXELS (width_p
20678 ? window_box_width (it->w, TEXT_AREA)
20679 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20680
20681 if (align_to && *align_to < 0)
20682 {
20683 *res = 0;
20684 if (EQ (prop, Qleft))
20685 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20686 if (EQ (prop, Qright))
20687 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20688 if (EQ (prop, Qcenter))
20689 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20690 + window_box_width (it->w, TEXT_AREA) / 2);
20691 if (EQ (prop, Qleft_fringe))
20692 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20693 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20694 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20695 if (EQ (prop, Qright_fringe))
20696 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20697 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20698 : window_box_right_offset (it->w, TEXT_AREA));
20699 if (EQ (prop, Qleft_margin))
20700 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20701 if (EQ (prop, Qright_margin))
20702 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20703 if (EQ (prop, Qscroll_bar))
20704 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20705 ? 0
20706 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20707 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20708 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20709 : 0)));
20710 }
20711 else
20712 {
20713 if (EQ (prop, Qleft_fringe))
20714 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20715 if (EQ (prop, Qright_fringe))
20716 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20717 if (EQ (prop, Qleft_margin))
20718 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20719 if (EQ (prop, Qright_margin))
20720 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20721 if (EQ (prop, Qscroll_bar))
20722 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20723 }
20724
20725 prop = Fbuffer_local_value (prop, it->w->buffer);
20726 }
20727
20728 if (INTEGERP (prop) || FLOATP (prop))
20729 {
20730 int base_unit = (width_p
20731 ? FRAME_COLUMN_WIDTH (it->f)
20732 : FRAME_LINE_HEIGHT (it->f));
20733 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20734 }
20735
20736 if (CONSP (prop))
20737 {
20738 Lisp_Object car = XCAR (prop);
20739 Lisp_Object cdr = XCDR (prop);
20740
20741 if (SYMBOLP (car))
20742 {
20743 #ifdef HAVE_WINDOW_SYSTEM
20744 if (FRAME_WINDOW_P (it->f)
20745 && valid_image_p (prop))
20746 {
20747 int id = lookup_image (it->f, prop);
20748 struct image *img = IMAGE_FROM_ID (it->f, id);
20749
20750 return OK_PIXELS (width_p ? img->width : img->height);
20751 }
20752 #endif
20753 if (EQ (car, Qplus) || EQ (car, Qminus))
20754 {
20755 int first = 1;
20756 double px;
20757
20758 pixels = 0;
20759 while (CONSP (cdr))
20760 {
20761 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20762 font, width_p, align_to))
20763 return 0;
20764 if (first)
20765 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20766 else
20767 pixels += px;
20768 cdr = XCDR (cdr);
20769 }
20770 if (EQ (car, Qminus))
20771 pixels = -pixels;
20772 return OK_PIXELS (pixels);
20773 }
20774
20775 car = Fbuffer_local_value (car, it->w->buffer);
20776 }
20777
20778 if (INTEGERP (car) || FLOATP (car))
20779 {
20780 double fact;
20781 pixels = XFLOATINT (car);
20782 if (NILP (cdr))
20783 return OK_PIXELS (pixels);
20784 if (calc_pixel_width_or_height (&fact, it, cdr,
20785 font, width_p, align_to))
20786 return OK_PIXELS (pixels * fact);
20787 return 0;
20788 }
20789
20790 return 0;
20791 }
20792
20793 return 0;
20794 }
20795
20796 \f
20797 /***********************************************************************
20798 Glyph Display
20799 ***********************************************************************/
20800
20801 #ifdef HAVE_WINDOW_SYSTEM
20802
20803 #if GLYPH_DEBUG
20804
20805 void
20806 dump_glyph_string (s)
20807 struct glyph_string *s;
20808 {
20809 fprintf (stderr, "glyph string\n");
20810 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20811 s->x, s->y, s->width, s->height);
20812 fprintf (stderr, " ybase = %d\n", s->ybase);
20813 fprintf (stderr, " hl = %d\n", s->hl);
20814 fprintf (stderr, " left overhang = %d, right = %d\n",
20815 s->left_overhang, s->right_overhang);
20816 fprintf (stderr, " nchars = %d\n", s->nchars);
20817 fprintf (stderr, " extends to end of line = %d\n",
20818 s->extends_to_end_of_line_p);
20819 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20820 fprintf (stderr, " bg width = %d\n", s->background_width);
20821 }
20822
20823 #endif /* GLYPH_DEBUG */
20824
20825 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20826 of XChar2b structures for S; it can't be allocated in
20827 init_glyph_string because it must be allocated via `alloca'. W
20828 is the window on which S is drawn. ROW and AREA are the glyph row
20829 and area within the row from which S is constructed. START is the
20830 index of the first glyph structure covered by S. HL is a
20831 face-override for drawing S. */
20832
20833 #ifdef HAVE_NTGUI
20834 #define OPTIONAL_HDC(hdc) HDC hdc,
20835 #define DECLARE_HDC(hdc) HDC hdc;
20836 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20837 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20838 #endif
20839
20840 #ifndef OPTIONAL_HDC
20841 #define OPTIONAL_HDC(hdc)
20842 #define DECLARE_HDC(hdc)
20843 #define ALLOCATE_HDC(hdc, f)
20844 #define RELEASE_HDC(hdc, f)
20845 #endif
20846
20847 static void
20848 init_glyph_string (struct glyph_string *s,
20849 OPTIONAL_HDC (hdc)
20850 XChar2b *char2b, struct window *w, struct glyph_row *row,
20851 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20852 {
20853 memset (s, 0, sizeof *s);
20854 s->w = w;
20855 s->f = XFRAME (w->frame);
20856 #ifdef HAVE_NTGUI
20857 s->hdc = hdc;
20858 #endif
20859 s->display = FRAME_X_DISPLAY (s->f);
20860 s->window = FRAME_X_WINDOW (s->f);
20861 s->char2b = char2b;
20862 s->hl = hl;
20863 s->row = row;
20864 s->area = area;
20865 s->first_glyph = row->glyphs[area] + start;
20866 s->height = row->height;
20867 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20868 s->ybase = s->y + row->ascent;
20869 }
20870
20871
20872 /* Append the list of glyph strings with head H and tail T to the list
20873 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20874
20875 static INLINE void
20876 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20877 struct glyph_string *h, struct glyph_string *t)
20878 {
20879 if (h)
20880 {
20881 if (*head)
20882 (*tail)->next = h;
20883 else
20884 *head = h;
20885 h->prev = *tail;
20886 *tail = t;
20887 }
20888 }
20889
20890
20891 /* Prepend the list of glyph strings with head H and tail T to the
20892 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20893 result. */
20894
20895 static INLINE void
20896 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20897 struct glyph_string *h, struct glyph_string *t)
20898 {
20899 if (h)
20900 {
20901 if (*head)
20902 (*head)->prev = t;
20903 else
20904 *tail = t;
20905 t->next = *head;
20906 *head = h;
20907 }
20908 }
20909
20910
20911 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20912 Set *HEAD and *TAIL to the resulting list. */
20913
20914 static INLINE void
20915 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20916 struct glyph_string *s)
20917 {
20918 s->next = s->prev = NULL;
20919 append_glyph_string_lists (head, tail, s, s);
20920 }
20921
20922
20923 /* Get face and two-byte form of character C in face FACE_ID on frame F.
20924 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
20925 make sure that X resources for the face returned are allocated.
20926 Value is a pointer to a realized face that is ready for display if
20927 DISPLAY_P is non-zero. */
20928
20929 static INLINE struct face *
20930 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20931 XChar2b *char2b, int display_p)
20932 {
20933 struct face *face = FACE_FROM_ID (f, face_id);
20934
20935 if (face->font)
20936 {
20937 unsigned code = face->font->driver->encode_char (face->font, c);
20938
20939 if (code != FONT_INVALID_CODE)
20940 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20941 else
20942 STORE_XCHAR2B (char2b, 0, 0);
20943 }
20944
20945 /* Make sure X resources of the face are allocated. */
20946 #ifdef HAVE_X_WINDOWS
20947 if (display_p)
20948 #endif
20949 {
20950 xassert (face != NULL);
20951 PREPARE_FACE_FOR_DISPLAY (f, face);
20952 }
20953
20954 return face;
20955 }
20956
20957
20958 /* Get face and two-byte form of character glyph GLYPH on frame F.
20959 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20960 a pointer to a realized face that is ready for display. */
20961
20962 static INLINE struct face *
20963 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20964 XChar2b *char2b, int *two_byte_p)
20965 {
20966 struct face *face;
20967
20968 xassert (glyph->type == CHAR_GLYPH);
20969 face = FACE_FROM_ID (f, glyph->face_id);
20970
20971 if (two_byte_p)
20972 *two_byte_p = 0;
20973
20974 if (face->font)
20975 {
20976 unsigned code;
20977
20978 if (CHAR_BYTE8_P (glyph->u.ch))
20979 code = CHAR_TO_BYTE8 (glyph->u.ch);
20980 else
20981 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20982
20983 if (code != FONT_INVALID_CODE)
20984 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20985 else
20986 STORE_XCHAR2B (char2b, 0, 0);
20987 }
20988
20989 /* Make sure X resources of the face are allocated. */
20990 xassert (face != NULL);
20991 PREPARE_FACE_FOR_DISPLAY (f, face);
20992 return face;
20993 }
20994
20995
20996 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20997 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20998
20999 static INLINE int
21000 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21001 {
21002 unsigned code;
21003
21004 if (CHAR_BYTE8_P (c))
21005 code = CHAR_TO_BYTE8 (c);
21006 else
21007 code = font->driver->encode_char (font, c);
21008
21009 if (code == FONT_INVALID_CODE)
21010 return 0;
21011 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21012 return 1;
21013 }
21014
21015
21016 /* Fill glyph string S with composition components specified by S->cmp.
21017
21018 BASE_FACE is the base face of the composition.
21019 S->cmp_from is the index of the first component for S.
21020
21021 OVERLAPS non-zero means S should draw the foreground only, and use
21022 its physical height for clipping. See also draw_glyphs.
21023
21024 Value is the index of a component not in S. */
21025
21026 static int
21027 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21028 int overlaps)
21029 {
21030 int i;
21031 /* For all glyphs of this composition, starting at the offset
21032 S->cmp_from, until we reach the end of the definition or encounter a
21033 glyph that requires the different face, add it to S. */
21034 struct face *face;
21035
21036 xassert (s);
21037
21038 s->for_overlaps = overlaps;
21039 s->face = NULL;
21040 s->font = NULL;
21041 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21042 {
21043 int c = COMPOSITION_GLYPH (s->cmp, i);
21044
21045 if (c != '\t')
21046 {
21047 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21048 -1, Qnil);
21049
21050 face = get_char_face_and_encoding (s->f, c, face_id,
21051 s->char2b + i, 1);
21052 if (face)
21053 {
21054 if (! s->face)
21055 {
21056 s->face = face;
21057 s->font = s->face->font;
21058 }
21059 else if (s->face != face)
21060 break;
21061 }
21062 }
21063 ++s->nchars;
21064 }
21065 s->cmp_to = i;
21066
21067 /* All glyph strings for the same composition has the same width,
21068 i.e. the width set for the first component of the composition. */
21069 s->width = s->first_glyph->pixel_width;
21070
21071 /* If the specified font could not be loaded, use the frame's
21072 default font, but record the fact that we couldn't load it in
21073 the glyph string so that we can draw rectangles for the
21074 characters of the glyph string. */
21075 if (s->font == NULL)
21076 {
21077 s->font_not_found_p = 1;
21078 s->font = FRAME_FONT (s->f);
21079 }
21080
21081 /* Adjust base line for subscript/superscript text. */
21082 s->ybase += s->first_glyph->voffset;
21083
21084 /* This glyph string must always be drawn with 16-bit functions. */
21085 s->two_byte_p = 1;
21086
21087 return s->cmp_to;
21088 }
21089
21090 static int
21091 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21092 int start, int end, int overlaps)
21093 {
21094 struct glyph *glyph, *last;
21095 Lisp_Object lgstring;
21096 int i;
21097
21098 s->for_overlaps = overlaps;
21099 glyph = s->row->glyphs[s->area] + start;
21100 last = s->row->glyphs[s->area] + end;
21101 s->cmp_id = glyph->u.cmp.id;
21102 s->cmp_from = glyph->slice.cmp.from;
21103 s->cmp_to = glyph->slice.cmp.to + 1;
21104 s->face = FACE_FROM_ID (s->f, face_id);
21105 lgstring = composition_gstring_from_id (s->cmp_id);
21106 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21107 glyph++;
21108 while (glyph < last
21109 && glyph->u.cmp.automatic
21110 && glyph->u.cmp.id == s->cmp_id
21111 && s->cmp_to == glyph->slice.cmp.from)
21112 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21113
21114 for (i = s->cmp_from; i < s->cmp_to; i++)
21115 {
21116 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21117 unsigned code = LGLYPH_CODE (lglyph);
21118
21119 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21120 }
21121 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21122 return glyph - s->row->glyphs[s->area];
21123 }
21124
21125
21126 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21127 See the comment of fill_glyph_string for arguments.
21128 Value is the index of the first glyph not in S. */
21129
21130
21131 static int
21132 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21133 int start, int end, int overlaps)
21134 {
21135 struct glyph *glyph, *last;
21136 int voffset;
21137
21138 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21139 s->for_overlaps = overlaps;
21140 glyph = s->row->glyphs[s->area] + start;
21141 last = s->row->glyphs[s->area] + end;
21142 voffset = glyph->voffset;
21143 s->face = FACE_FROM_ID (s->f, face_id);
21144 s->font = s->face->font;
21145 s->nchars = 1;
21146 s->width = glyph->pixel_width;
21147 glyph++;
21148 while (glyph < last
21149 && glyph->type == GLYPHLESS_GLYPH
21150 && glyph->voffset == voffset
21151 && glyph->face_id == face_id)
21152 {
21153 s->nchars++;
21154 s->width += glyph->pixel_width;
21155 glyph++;
21156 }
21157 s->ybase += voffset;
21158 return glyph - s->row->glyphs[s->area];
21159 }
21160
21161
21162 /* Fill glyph string S from a sequence of character glyphs.
21163
21164 FACE_ID is the face id of the string. START is the index of the
21165 first glyph to consider, END is the index of the last + 1.
21166 OVERLAPS non-zero means S should draw the foreground only, and use
21167 its physical height for clipping. See also draw_glyphs.
21168
21169 Value is the index of the first glyph not in S. */
21170
21171 static int
21172 fill_glyph_string (struct glyph_string *s, int face_id,
21173 int start, int end, int overlaps)
21174 {
21175 struct glyph *glyph, *last;
21176 int voffset;
21177 int glyph_not_available_p;
21178
21179 xassert (s->f == XFRAME (s->w->frame));
21180 xassert (s->nchars == 0);
21181 xassert (start >= 0 && end > start);
21182
21183 s->for_overlaps = overlaps;
21184 glyph = s->row->glyphs[s->area] + start;
21185 last = s->row->glyphs[s->area] + end;
21186 voffset = glyph->voffset;
21187 s->padding_p = glyph->padding_p;
21188 glyph_not_available_p = glyph->glyph_not_available_p;
21189
21190 while (glyph < last
21191 && glyph->type == CHAR_GLYPH
21192 && glyph->voffset == voffset
21193 /* Same face id implies same font, nowadays. */
21194 && glyph->face_id == face_id
21195 && glyph->glyph_not_available_p == glyph_not_available_p)
21196 {
21197 int two_byte_p;
21198
21199 s->face = get_glyph_face_and_encoding (s->f, glyph,
21200 s->char2b + s->nchars,
21201 &two_byte_p);
21202 s->two_byte_p = two_byte_p;
21203 ++s->nchars;
21204 xassert (s->nchars <= end - start);
21205 s->width += glyph->pixel_width;
21206 if (glyph++->padding_p != s->padding_p)
21207 break;
21208 }
21209
21210 s->font = s->face->font;
21211
21212 /* If the specified font could not be loaded, use the frame's font,
21213 but record the fact that we couldn't load it in
21214 S->font_not_found_p so that we can draw rectangles for the
21215 characters of the glyph string. */
21216 if (s->font == NULL || glyph_not_available_p)
21217 {
21218 s->font_not_found_p = 1;
21219 s->font = FRAME_FONT (s->f);
21220 }
21221
21222 /* Adjust base line for subscript/superscript text. */
21223 s->ybase += voffset;
21224
21225 xassert (s->face && s->face->gc);
21226 return glyph - s->row->glyphs[s->area];
21227 }
21228
21229
21230 /* Fill glyph string S from image glyph S->first_glyph. */
21231
21232 static void
21233 fill_image_glyph_string (struct glyph_string *s)
21234 {
21235 xassert (s->first_glyph->type == IMAGE_GLYPH);
21236 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21237 xassert (s->img);
21238 s->slice = s->first_glyph->slice.img;
21239 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21240 s->font = s->face->font;
21241 s->width = s->first_glyph->pixel_width;
21242
21243 /* Adjust base line for subscript/superscript text. */
21244 s->ybase += s->first_glyph->voffset;
21245 }
21246
21247
21248 /* Fill glyph string S from a sequence of stretch glyphs.
21249
21250 START is the index of the first glyph to consider,
21251 END is the index of the last + 1.
21252
21253 Value is the index of the first glyph not in S. */
21254
21255 static int
21256 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21257 {
21258 struct glyph *glyph, *last;
21259 int voffset, face_id;
21260
21261 xassert (s->first_glyph->type == STRETCH_GLYPH);
21262
21263 glyph = s->row->glyphs[s->area] + start;
21264 last = s->row->glyphs[s->area] + end;
21265 face_id = glyph->face_id;
21266 s->face = FACE_FROM_ID (s->f, face_id);
21267 s->font = s->face->font;
21268 s->width = glyph->pixel_width;
21269 s->nchars = 1;
21270 voffset = glyph->voffset;
21271
21272 for (++glyph;
21273 (glyph < last
21274 && glyph->type == STRETCH_GLYPH
21275 && glyph->voffset == voffset
21276 && glyph->face_id == face_id);
21277 ++glyph)
21278 s->width += glyph->pixel_width;
21279
21280 /* Adjust base line for subscript/superscript text. */
21281 s->ybase += voffset;
21282
21283 /* The case that face->gc == 0 is handled when drawing the glyph
21284 string by calling PREPARE_FACE_FOR_DISPLAY. */
21285 xassert (s->face);
21286 return glyph - s->row->glyphs[s->area];
21287 }
21288
21289 static struct font_metrics *
21290 get_per_char_metric (struct font *font, XChar2b *char2b)
21291 {
21292 static struct font_metrics metrics;
21293 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21294
21295 if (! font || code == FONT_INVALID_CODE)
21296 return NULL;
21297 font->driver->text_extents (font, &code, 1, &metrics);
21298 return &metrics;
21299 }
21300
21301 /* EXPORT for RIF:
21302 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21303 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21304 assumed to be zero. */
21305
21306 void
21307 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21308 {
21309 *left = *right = 0;
21310
21311 if (glyph->type == CHAR_GLYPH)
21312 {
21313 struct face *face;
21314 XChar2b char2b;
21315 struct font_metrics *pcm;
21316
21317 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21318 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21319 {
21320 if (pcm->rbearing > pcm->width)
21321 *right = pcm->rbearing - pcm->width;
21322 if (pcm->lbearing < 0)
21323 *left = -pcm->lbearing;
21324 }
21325 }
21326 else if (glyph->type == COMPOSITE_GLYPH)
21327 {
21328 if (! glyph->u.cmp.automatic)
21329 {
21330 struct composition *cmp = composition_table[glyph->u.cmp.id];
21331
21332 if (cmp->rbearing > cmp->pixel_width)
21333 *right = cmp->rbearing - cmp->pixel_width;
21334 if (cmp->lbearing < 0)
21335 *left = - cmp->lbearing;
21336 }
21337 else
21338 {
21339 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21340 struct font_metrics metrics;
21341
21342 composition_gstring_width (gstring, glyph->slice.cmp.from,
21343 glyph->slice.cmp.to + 1, &metrics);
21344 if (metrics.rbearing > metrics.width)
21345 *right = metrics.rbearing - metrics.width;
21346 if (metrics.lbearing < 0)
21347 *left = - metrics.lbearing;
21348 }
21349 }
21350 }
21351
21352
21353 /* Return the index of the first glyph preceding glyph string S that
21354 is overwritten by S because of S's left overhang. Value is -1
21355 if no glyphs are overwritten. */
21356
21357 static int
21358 left_overwritten (struct glyph_string *s)
21359 {
21360 int k;
21361
21362 if (s->left_overhang)
21363 {
21364 int x = 0, i;
21365 struct glyph *glyphs = s->row->glyphs[s->area];
21366 int first = s->first_glyph - glyphs;
21367
21368 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21369 x -= glyphs[i].pixel_width;
21370
21371 k = i + 1;
21372 }
21373 else
21374 k = -1;
21375
21376 return k;
21377 }
21378
21379
21380 /* Return the index of the first glyph preceding glyph string S that
21381 is overwriting S because of its right overhang. Value is -1 if no
21382 glyph in front of S overwrites S. */
21383
21384 static int
21385 left_overwriting (struct glyph_string *s)
21386 {
21387 int i, k, x;
21388 struct glyph *glyphs = s->row->glyphs[s->area];
21389 int first = s->first_glyph - glyphs;
21390
21391 k = -1;
21392 x = 0;
21393 for (i = first - 1; i >= 0; --i)
21394 {
21395 int left, right;
21396 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21397 if (x + right > 0)
21398 k = i;
21399 x -= glyphs[i].pixel_width;
21400 }
21401
21402 return k;
21403 }
21404
21405
21406 /* Return the index of the last glyph following glyph string S that is
21407 overwritten by S because of S's right overhang. Value is -1 if
21408 no such glyph is found. */
21409
21410 static int
21411 right_overwritten (struct glyph_string *s)
21412 {
21413 int k = -1;
21414
21415 if (s->right_overhang)
21416 {
21417 int x = 0, i;
21418 struct glyph *glyphs = s->row->glyphs[s->area];
21419 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21420 int end = s->row->used[s->area];
21421
21422 for (i = first; i < end && s->right_overhang > x; ++i)
21423 x += glyphs[i].pixel_width;
21424
21425 k = i;
21426 }
21427
21428 return k;
21429 }
21430
21431
21432 /* Return the index of the last glyph following glyph string S that
21433 overwrites S because of its left overhang. Value is negative
21434 if no such glyph is found. */
21435
21436 static int
21437 right_overwriting (struct glyph_string *s)
21438 {
21439 int i, k, x;
21440 int end = s->row->used[s->area];
21441 struct glyph *glyphs = s->row->glyphs[s->area];
21442 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21443
21444 k = -1;
21445 x = 0;
21446 for (i = first; i < end; ++i)
21447 {
21448 int left, right;
21449 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21450 if (x - left < 0)
21451 k = i;
21452 x += glyphs[i].pixel_width;
21453 }
21454
21455 return k;
21456 }
21457
21458
21459 /* Set background width of glyph string S. START is the index of the
21460 first glyph following S. LAST_X is the right-most x-position + 1
21461 in the drawing area. */
21462
21463 static INLINE void
21464 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21465 {
21466 /* If the face of this glyph string has to be drawn to the end of
21467 the drawing area, set S->extends_to_end_of_line_p. */
21468
21469 if (start == s->row->used[s->area]
21470 && s->area == TEXT_AREA
21471 && ((s->row->fill_line_p
21472 && (s->hl == DRAW_NORMAL_TEXT
21473 || s->hl == DRAW_IMAGE_RAISED
21474 || s->hl == DRAW_IMAGE_SUNKEN))
21475 || s->hl == DRAW_MOUSE_FACE))
21476 s->extends_to_end_of_line_p = 1;
21477
21478 /* If S extends its face to the end of the line, set its
21479 background_width to the distance to the right edge of the drawing
21480 area. */
21481 if (s->extends_to_end_of_line_p)
21482 s->background_width = last_x - s->x + 1;
21483 else
21484 s->background_width = s->width;
21485 }
21486
21487
21488 /* Compute overhangs and x-positions for glyph string S and its
21489 predecessors, or successors. X is the starting x-position for S.
21490 BACKWARD_P non-zero means process predecessors. */
21491
21492 static void
21493 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21494 {
21495 if (backward_p)
21496 {
21497 while (s)
21498 {
21499 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21500 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21501 x -= s->width;
21502 s->x = x;
21503 s = s->prev;
21504 }
21505 }
21506 else
21507 {
21508 while (s)
21509 {
21510 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21511 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21512 s->x = x;
21513 x += s->width;
21514 s = s->next;
21515 }
21516 }
21517 }
21518
21519
21520
21521 /* The following macros are only called from draw_glyphs below.
21522 They reference the following parameters of that function directly:
21523 `w', `row', `area', and `overlap_p'
21524 as well as the following local variables:
21525 `s', `f', and `hdc' (in W32) */
21526
21527 #ifdef HAVE_NTGUI
21528 /* On W32, silently add local `hdc' variable to argument list of
21529 init_glyph_string. */
21530 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21531 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21532 #else
21533 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21534 init_glyph_string (s, char2b, w, row, area, start, hl)
21535 #endif
21536
21537 /* Add a glyph string for a stretch glyph to the list of strings
21538 between HEAD and TAIL. START is the index of the stretch glyph in
21539 row area AREA of glyph row ROW. END is the index of the last glyph
21540 in that glyph row area. X is the current output position assigned
21541 to the new glyph string constructed. HL overrides that face of the
21542 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21543 is the right-most x-position of the drawing area. */
21544
21545 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21546 and below -- keep them on one line. */
21547 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21548 do \
21549 { \
21550 s = (struct glyph_string *) alloca (sizeof *s); \
21551 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21552 START = fill_stretch_glyph_string (s, START, END); \
21553 append_glyph_string (&HEAD, &TAIL, s); \
21554 s->x = (X); \
21555 } \
21556 while (0)
21557
21558
21559 /* Add a glyph string for an image glyph to the list of strings
21560 between HEAD and TAIL. START is the index of the image glyph in
21561 row area AREA of glyph row ROW. END is the index of the last glyph
21562 in that glyph row area. X is the current output position assigned
21563 to the new glyph string constructed. HL overrides that face of the
21564 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21565 is the right-most x-position of the drawing area. */
21566
21567 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21568 do \
21569 { \
21570 s = (struct glyph_string *) alloca (sizeof *s); \
21571 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21572 fill_image_glyph_string (s); \
21573 append_glyph_string (&HEAD, &TAIL, s); \
21574 ++START; \
21575 s->x = (X); \
21576 } \
21577 while (0)
21578
21579
21580 /* Add a glyph string for a sequence of character glyphs to the list
21581 of strings between HEAD and TAIL. START is the index of the first
21582 glyph in row area AREA of glyph row ROW that is part of the new
21583 glyph string. END is the index of the last glyph in that glyph row
21584 area. X is the current output position assigned to the new glyph
21585 string constructed. HL overrides that face of the glyph; e.g. it
21586 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21587 right-most x-position of the drawing area. */
21588
21589 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21590 do \
21591 { \
21592 int face_id; \
21593 XChar2b *char2b; \
21594 \
21595 face_id = (row)->glyphs[area][START].face_id; \
21596 \
21597 s = (struct glyph_string *) alloca (sizeof *s); \
21598 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21599 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21600 append_glyph_string (&HEAD, &TAIL, s); \
21601 s->x = (X); \
21602 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21603 } \
21604 while (0)
21605
21606
21607 /* Add a glyph string for a composite sequence to the list of strings
21608 between HEAD and TAIL. START is the index of the first glyph in
21609 row area AREA of glyph row ROW that is part of the new glyph
21610 string. END is the index of the last glyph in that glyph row area.
21611 X is the current output position assigned to the new glyph string
21612 constructed. HL overrides that face of the glyph; e.g. it is
21613 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21614 x-position of the drawing area. */
21615
21616 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21617 do { \
21618 int face_id = (row)->glyphs[area][START].face_id; \
21619 struct face *base_face = FACE_FROM_ID (f, face_id); \
21620 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21621 struct composition *cmp = composition_table[cmp_id]; \
21622 XChar2b *char2b; \
21623 struct glyph_string *first_s IF_LINT (= NULL); \
21624 int n; \
21625 \
21626 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21627 \
21628 /* Make glyph_strings for each glyph sequence that is drawable by \
21629 the same face, and append them to HEAD/TAIL. */ \
21630 for (n = 0; n < cmp->glyph_len;) \
21631 { \
21632 s = (struct glyph_string *) alloca (sizeof *s); \
21633 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21634 append_glyph_string (&(HEAD), &(TAIL), s); \
21635 s->cmp = cmp; \
21636 s->cmp_from = n; \
21637 s->x = (X); \
21638 if (n == 0) \
21639 first_s = s; \
21640 n = fill_composite_glyph_string (s, base_face, overlaps); \
21641 } \
21642 \
21643 ++START; \
21644 s = first_s; \
21645 } while (0)
21646
21647
21648 /* Add a glyph string for a glyph-string sequence to the list of strings
21649 between HEAD and TAIL. */
21650
21651 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21652 do { \
21653 int face_id; \
21654 XChar2b *char2b; \
21655 Lisp_Object gstring; \
21656 \
21657 face_id = (row)->glyphs[area][START].face_id; \
21658 gstring = (composition_gstring_from_id \
21659 ((row)->glyphs[area][START].u.cmp.id)); \
21660 s = (struct glyph_string *) alloca (sizeof *s); \
21661 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21662 * LGSTRING_GLYPH_LEN (gstring)); \
21663 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21664 append_glyph_string (&(HEAD), &(TAIL), s); \
21665 s->x = (X); \
21666 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21667 } while (0)
21668
21669
21670 /* Add a glyph string for a sequence of glyphless character's glyphs
21671 to the list of strings between HEAD and TAIL. The meanings of
21672 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21673
21674 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21675 do \
21676 { \
21677 int face_id; \
21678 \
21679 face_id = (row)->glyphs[area][START].face_id; \
21680 \
21681 s = (struct glyph_string *) alloca (sizeof *s); \
21682 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21683 append_glyph_string (&HEAD, &TAIL, s); \
21684 s->x = (X); \
21685 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21686 overlaps); \
21687 } \
21688 while (0)
21689
21690
21691 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21692 of AREA of glyph row ROW on window W between indices START and END.
21693 HL overrides the face for drawing glyph strings, e.g. it is
21694 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21695 x-positions of the drawing area.
21696
21697 This is an ugly monster macro construct because we must use alloca
21698 to allocate glyph strings (because draw_glyphs can be called
21699 asynchronously). */
21700
21701 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21702 do \
21703 { \
21704 HEAD = TAIL = NULL; \
21705 while (START < END) \
21706 { \
21707 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21708 switch (first_glyph->type) \
21709 { \
21710 case CHAR_GLYPH: \
21711 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21712 HL, X, LAST_X); \
21713 break; \
21714 \
21715 case COMPOSITE_GLYPH: \
21716 if (first_glyph->u.cmp.automatic) \
21717 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21718 HL, X, LAST_X); \
21719 else \
21720 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21721 HL, X, LAST_X); \
21722 break; \
21723 \
21724 case STRETCH_GLYPH: \
21725 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21726 HL, X, LAST_X); \
21727 break; \
21728 \
21729 case IMAGE_GLYPH: \
21730 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21731 HL, X, LAST_X); \
21732 break; \
21733 \
21734 case GLYPHLESS_GLYPH: \
21735 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21736 HL, X, LAST_X); \
21737 break; \
21738 \
21739 default: \
21740 abort (); \
21741 } \
21742 \
21743 if (s) \
21744 { \
21745 set_glyph_string_background_width (s, START, LAST_X); \
21746 (X) += s->width; \
21747 } \
21748 } \
21749 } while (0)
21750
21751
21752 /* Draw glyphs between START and END in AREA of ROW on window W,
21753 starting at x-position X. X is relative to AREA in W. HL is a
21754 face-override with the following meaning:
21755
21756 DRAW_NORMAL_TEXT draw normally
21757 DRAW_CURSOR draw in cursor face
21758 DRAW_MOUSE_FACE draw in mouse face.
21759 DRAW_INVERSE_VIDEO draw in mode line face
21760 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21761 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21762
21763 If OVERLAPS is non-zero, draw only the foreground of characters and
21764 clip to the physical height of ROW. Non-zero value also defines
21765 the overlapping part to be drawn:
21766
21767 OVERLAPS_PRED overlap with preceding rows
21768 OVERLAPS_SUCC overlap with succeeding rows
21769 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21770 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21771
21772 Value is the x-position reached, relative to AREA of W. */
21773
21774 static int
21775 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21776 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21777 enum draw_glyphs_face hl, int overlaps)
21778 {
21779 struct glyph_string *head, *tail;
21780 struct glyph_string *s;
21781 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21782 int i, j, x_reached, last_x, area_left = 0;
21783 struct frame *f = XFRAME (WINDOW_FRAME (w));
21784 DECLARE_HDC (hdc);
21785
21786 ALLOCATE_HDC (hdc, f);
21787
21788 /* Let's rather be paranoid than getting a SEGV. */
21789 end = min (end, row->used[area]);
21790 start = max (0, start);
21791 start = min (end, start);
21792
21793 /* Translate X to frame coordinates. Set last_x to the right
21794 end of the drawing area. */
21795 if (row->full_width_p)
21796 {
21797 /* X is relative to the left edge of W, without scroll bars
21798 or fringes. */
21799 area_left = WINDOW_LEFT_EDGE_X (w);
21800 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21801 }
21802 else
21803 {
21804 area_left = window_box_left (w, area);
21805 last_x = area_left + window_box_width (w, area);
21806 }
21807 x += area_left;
21808
21809 /* Build a doubly-linked list of glyph_string structures between
21810 head and tail from what we have to draw. Note that the macro
21811 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21812 the reason we use a separate variable `i'. */
21813 i = start;
21814 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21815 if (tail)
21816 x_reached = tail->x + tail->background_width;
21817 else
21818 x_reached = x;
21819
21820 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21821 the row, redraw some glyphs in front or following the glyph
21822 strings built above. */
21823 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21824 {
21825 struct glyph_string *h, *t;
21826 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21827 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
21828 int check_mouse_face = 0;
21829 int dummy_x = 0;
21830
21831 /* If mouse highlighting is on, we may need to draw adjacent
21832 glyphs using mouse-face highlighting. */
21833 if (area == TEXT_AREA && row->mouse_face_p)
21834 {
21835 struct glyph_row *mouse_beg_row, *mouse_end_row;
21836
21837 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21838 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21839
21840 if (row >= mouse_beg_row && row <= mouse_end_row)
21841 {
21842 check_mouse_face = 1;
21843 mouse_beg_col = (row == mouse_beg_row)
21844 ? hlinfo->mouse_face_beg_col : 0;
21845 mouse_end_col = (row == mouse_end_row)
21846 ? hlinfo->mouse_face_end_col
21847 : row->used[TEXT_AREA];
21848 }
21849 }
21850
21851 /* Compute overhangs for all glyph strings. */
21852 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21853 for (s = head; s; s = s->next)
21854 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21855
21856 /* Prepend glyph strings for glyphs in front of the first glyph
21857 string that are overwritten because of the first glyph
21858 string's left overhang. The background of all strings
21859 prepended must be drawn because the first glyph string
21860 draws over it. */
21861 i = left_overwritten (head);
21862 if (i >= 0)
21863 {
21864 enum draw_glyphs_face overlap_hl;
21865
21866 /* If this row contains mouse highlighting, attempt to draw
21867 the overlapped glyphs with the correct highlight. This
21868 code fails if the overlap encompasses more than one glyph
21869 and mouse-highlight spans only some of these glyphs.
21870 However, making it work perfectly involves a lot more
21871 code, and I don't know if the pathological case occurs in
21872 practice, so we'll stick to this for now. --- cyd */
21873 if (check_mouse_face
21874 && mouse_beg_col < start && mouse_end_col > i)
21875 overlap_hl = DRAW_MOUSE_FACE;
21876 else
21877 overlap_hl = DRAW_NORMAL_TEXT;
21878
21879 j = i;
21880 BUILD_GLYPH_STRINGS (j, start, h, t,
21881 overlap_hl, dummy_x, last_x);
21882 start = i;
21883 compute_overhangs_and_x (t, head->x, 1);
21884 prepend_glyph_string_lists (&head, &tail, h, t);
21885 clip_head = head;
21886 }
21887
21888 /* Prepend glyph strings for glyphs in front of the first glyph
21889 string that overwrite that glyph string because of their
21890 right overhang. For these strings, only the foreground must
21891 be drawn, because it draws over the glyph string at `head'.
21892 The background must not be drawn because this would overwrite
21893 right overhangs of preceding glyphs for which no glyph
21894 strings exist. */
21895 i = left_overwriting (head);
21896 if (i >= 0)
21897 {
21898 enum draw_glyphs_face overlap_hl;
21899
21900 if (check_mouse_face
21901 && mouse_beg_col < start && mouse_end_col > i)
21902 overlap_hl = DRAW_MOUSE_FACE;
21903 else
21904 overlap_hl = DRAW_NORMAL_TEXT;
21905
21906 clip_head = head;
21907 BUILD_GLYPH_STRINGS (i, start, h, t,
21908 overlap_hl, dummy_x, last_x);
21909 for (s = h; s; s = s->next)
21910 s->background_filled_p = 1;
21911 compute_overhangs_and_x (t, head->x, 1);
21912 prepend_glyph_string_lists (&head, &tail, h, t);
21913 }
21914
21915 /* Append glyphs strings for glyphs following the last glyph
21916 string tail that are overwritten by tail. The background of
21917 these strings has to be drawn because tail's foreground draws
21918 over it. */
21919 i = right_overwritten (tail);
21920 if (i >= 0)
21921 {
21922 enum draw_glyphs_face overlap_hl;
21923
21924 if (check_mouse_face
21925 && mouse_beg_col < i && mouse_end_col > end)
21926 overlap_hl = DRAW_MOUSE_FACE;
21927 else
21928 overlap_hl = DRAW_NORMAL_TEXT;
21929
21930 BUILD_GLYPH_STRINGS (end, i, h, t,
21931 overlap_hl, x, last_x);
21932 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21933 we don't have `end = i;' here. */
21934 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21935 append_glyph_string_lists (&head, &tail, h, t);
21936 clip_tail = tail;
21937 }
21938
21939 /* Append glyph strings for glyphs following the last glyph
21940 string tail that overwrite tail. The foreground of such
21941 glyphs has to be drawn because it writes into the background
21942 of tail. The background must not be drawn because it could
21943 paint over the foreground of following glyphs. */
21944 i = right_overwriting (tail);
21945 if (i >= 0)
21946 {
21947 enum draw_glyphs_face overlap_hl;
21948 if (check_mouse_face
21949 && mouse_beg_col < i && mouse_end_col > end)
21950 overlap_hl = DRAW_MOUSE_FACE;
21951 else
21952 overlap_hl = DRAW_NORMAL_TEXT;
21953
21954 clip_tail = tail;
21955 i++; /* We must include the Ith glyph. */
21956 BUILD_GLYPH_STRINGS (end, i, h, t,
21957 overlap_hl, x, last_x);
21958 for (s = h; s; s = s->next)
21959 s->background_filled_p = 1;
21960 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21961 append_glyph_string_lists (&head, &tail, h, t);
21962 }
21963 if (clip_head || clip_tail)
21964 for (s = head; s; s = s->next)
21965 {
21966 s->clip_head = clip_head;
21967 s->clip_tail = clip_tail;
21968 }
21969 }
21970
21971 /* Draw all strings. */
21972 for (s = head; s; s = s->next)
21973 FRAME_RIF (f)->draw_glyph_string (s);
21974
21975 #ifndef HAVE_NS
21976 /* When focus a sole frame and move horizontally, this sets on_p to 0
21977 causing a failure to erase prev cursor position. */
21978 if (area == TEXT_AREA
21979 && !row->full_width_p
21980 /* When drawing overlapping rows, only the glyph strings'
21981 foreground is drawn, which doesn't erase a cursor
21982 completely. */
21983 && !overlaps)
21984 {
21985 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21986 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21987 : (tail ? tail->x + tail->background_width : x));
21988 x0 -= area_left;
21989 x1 -= area_left;
21990
21991 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21992 row->y, MATRIX_ROW_BOTTOM_Y (row));
21993 }
21994 #endif
21995
21996 /* Value is the x-position up to which drawn, relative to AREA of W.
21997 This doesn't include parts drawn because of overhangs. */
21998 if (row->full_width_p)
21999 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22000 else
22001 x_reached -= area_left;
22002
22003 RELEASE_HDC (hdc, f);
22004
22005 return x_reached;
22006 }
22007
22008 /* Expand row matrix if too narrow. Don't expand if area
22009 is not present. */
22010
22011 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22012 { \
22013 if (!fonts_changed_p \
22014 && (it->glyph_row->glyphs[area] \
22015 < it->glyph_row->glyphs[area + 1])) \
22016 { \
22017 it->w->ncols_scale_factor++; \
22018 fonts_changed_p = 1; \
22019 } \
22020 }
22021
22022 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22023 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22024
22025 static INLINE void
22026 append_glyph (struct it *it)
22027 {
22028 struct glyph *glyph;
22029 enum glyph_row_area area = it->area;
22030
22031 xassert (it->glyph_row);
22032 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22033
22034 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22035 if (glyph < it->glyph_row->glyphs[area + 1])
22036 {
22037 /* If the glyph row is reversed, we need to prepend the glyph
22038 rather than append it. */
22039 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22040 {
22041 struct glyph *g;
22042
22043 /* Make room for the additional glyph. */
22044 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22045 g[1] = *g;
22046 glyph = it->glyph_row->glyphs[area];
22047 }
22048 glyph->charpos = CHARPOS (it->position);
22049 glyph->object = it->object;
22050 if (it->pixel_width > 0)
22051 {
22052 glyph->pixel_width = it->pixel_width;
22053 glyph->padding_p = 0;
22054 }
22055 else
22056 {
22057 /* Assure at least 1-pixel width. Otherwise, cursor can't
22058 be displayed correctly. */
22059 glyph->pixel_width = 1;
22060 glyph->padding_p = 1;
22061 }
22062 glyph->ascent = it->ascent;
22063 glyph->descent = it->descent;
22064 glyph->voffset = it->voffset;
22065 glyph->type = CHAR_GLYPH;
22066 glyph->avoid_cursor_p = it->avoid_cursor_p;
22067 glyph->multibyte_p = it->multibyte_p;
22068 glyph->left_box_line_p = it->start_of_box_run_p;
22069 glyph->right_box_line_p = it->end_of_box_run_p;
22070 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22071 || it->phys_descent > it->descent);
22072 glyph->glyph_not_available_p = it->glyph_not_available_p;
22073 glyph->face_id = it->face_id;
22074 glyph->u.ch = it->char_to_display;
22075 glyph->slice.img = null_glyph_slice;
22076 glyph->font_type = FONT_TYPE_UNKNOWN;
22077 if (it->bidi_p)
22078 {
22079 glyph->resolved_level = it->bidi_it.resolved_level;
22080 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22081 abort ();
22082 glyph->bidi_type = it->bidi_it.type;
22083 }
22084 else
22085 {
22086 glyph->resolved_level = 0;
22087 glyph->bidi_type = UNKNOWN_BT;
22088 }
22089 ++it->glyph_row->used[area];
22090 }
22091 else
22092 IT_EXPAND_MATRIX_WIDTH (it, area);
22093 }
22094
22095 /* Store one glyph for the composition IT->cmp_it.id in
22096 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22097 non-null. */
22098
22099 static INLINE void
22100 append_composite_glyph (struct it *it)
22101 {
22102 struct glyph *glyph;
22103 enum glyph_row_area area = it->area;
22104
22105 xassert (it->glyph_row);
22106
22107 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22108 if (glyph < it->glyph_row->glyphs[area + 1])
22109 {
22110 /* If the glyph row is reversed, we need to prepend the glyph
22111 rather than append it. */
22112 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22113 {
22114 struct glyph *g;
22115
22116 /* Make room for the new glyph. */
22117 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22118 g[1] = *g;
22119 glyph = it->glyph_row->glyphs[it->area];
22120 }
22121 glyph->charpos = it->cmp_it.charpos;
22122 glyph->object = it->object;
22123 glyph->pixel_width = it->pixel_width;
22124 glyph->ascent = it->ascent;
22125 glyph->descent = it->descent;
22126 glyph->voffset = it->voffset;
22127 glyph->type = COMPOSITE_GLYPH;
22128 if (it->cmp_it.ch < 0)
22129 {
22130 glyph->u.cmp.automatic = 0;
22131 glyph->u.cmp.id = it->cmp_it.id;
22132 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22133 }
22134 else
22135 {
22136 glyph->u.cmp.automatic = 1;
22137 glyph->u.cmp.id = it->cmp_it.id;
22138 glyph->slice.cmp.from = it->cmp_it.from;
22139 glyph->slice.cmp.to = it->cmp_it.to - 1;
22140 }
22141 glyph->avoid_cursor_p = it->avoid_cursor_p;
22142 glyph->multibyte_p = it->multibyte_p;
22143 glyph->left_box_line_p = it->start_of_box_run_p;
22144 glyph->right_box_line_p = it->end_of_box_run_p;
22145 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22146 || it->phys_descent > it->descent);
22147 glyph->padding_p = 0;
22148 glyph->glyph_not_available_p = 0;
22149 glyph->face_id = it->face_id;
22150 glyph->font_type = FONT_TYPE_UNKNOWN;
22151 if (it->bidi_p)
22152 {
22153 glyph->resolved_level = it->bidi_it.resolved_level;
22154 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22155 abort ();
22156 glyph->bidi_type = it->bidi_it.type;
22157 }
22158 ++it->glyph_row->used[area];
22159 }
22160 else
22161 IT_EXPAND_MATRIX_WIDTH (it, area);
22162 }
22163
22164
22165 /* Change IT->ascent and IT->height according to the setting of
22166 IT->voffset. */
22167
22168 static INLINE void
22169 take_vertical_position_into_account (struct it *it)
22170 {
22171 if (it->voffset)
22172 {
22173 if (it->voffset < 0)
22174 /* Increase the ascent so that we can display the text higher
22175 in the line. */
22176 it->ascent -= it->voffset;
22177 else
22178 /* Increase the descent so that we can display the text lower
22179 in the line. */
22180 it->descent += it->voffset;
22181 }
22182 }
22183
22184
22185 /* Produce glyphs/get display metrics for the image IT is loaded with.
22186 See the description of struct display_iterator in dispextern.h for
22187 an overview of struct display_iterator. */
22188
22189 static void
22190 produce_image_glyph (struct it *it)
22191 {
22192 struct image *img;
22193 struct face *face;
22194 int glyph_ascent, crop;
22195 struct glyph_slice slice;
22196
22197 xassert (it->what == IT_IMAGE);
22198
22199 face = FACE_FROM_ID (it->f, it->face_id);
22200 xassert (face);
22201 /* Make sure X resources of the face is loaded. */
22202 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22203
22204 if (it->image_id < 0)
22205 {
22206 /* Fringe bitmap. */
22207 it->ascent = it->phys_ascent = 0;
22208 it->descent = it->phys_descent = 0;
22209 it->pixel_width = 0;
22210 it->nglyphs = 0;
22211 return;
22212 }
22213
22214 img = IMAGE_FROM_ID (it->f, it->image_id);
22215 xassert (img);
22216 /* Make sure X resources of the image is loaded. */
22217 prepare_image_for_display (it->f, img);
22218
22219 slice.x = slice.y = 0;
22220 slice.width = img->width;
22221 slice.height = img->height;
22222
22223 if (INTEGERP (it->slice.x))
22224 slice.x = XINT (it->slice.x);
22225 else if (FLOATP (it->slice.x))
22226 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22227
22228 if (INTEGERP (it->slice.y))
22229 slice.y = XINT (it->slice.y);
22230 else if (FLOATP (it->slice.y))
22231 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22232
22233 if (INTEGERP (it->slice.width))
22234 slice.width = XINT (it->slice.width);
22235 else if (FLOATP (it->slice.width))
22236 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22237
22238 if (INTEGERP (it->slice.height))
22239 slice.height = XINT (it->slice.height);
22240 else if (FLOATP (it->slice.height))
22241 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22242
22243 if (slice.x >= img->width)
22244 slice.x = img->width;
22245 if (slice.y >= img->height)
22246 slice.y = img->height;
22247 if (slice.x + slice.width >= img->width)
22248 slice.width = img->width - slice.x;
22249 if (slice.y + slice.height > img->height)
22250 slice.height = img->height - slice.y;
22251
22252 if (slice.width == 0 || slice.height == 0)
22253 return;
22254
22255 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22256
22257 it->descent = slice.height - glyph_ascent;
22258 if (slice.y == 0)
22259 it->descent += img->vmargin;
22260 if (slice.y + slice.height == img->height)
22261 it->descent += img->vmargin;
22262 it->phys_descent = it->descent;
22263
22264 it->pixel_width = slice.width;
22265 if (slice.x == 0)
22266 it->pixel_width += img->hmargin;
22267 if (slice.x + slice.width == img->width)
22268 it->pixel_width += img->hmargin;
22269
22270 /* It's quite possible for images to have an ascent greater than
22271 their height, so don't get confused in that case. */
22272 if (it->descent < 0)
22273 it->descent = 0;
22274
22275 it->nglyphs = 1;
22276
22277 if (face->box != FACE_NO_BOX)
22278 {
22279 if (face->box_line_width > 0)
22280 {
22281 if (slice.y == 0)
22282 it->ascent += face->box_line_width;
22283 if (slice.y + slice.height == img->height)
22284 it->descent += face->box_line_width;
22285 }
22286
22287 if (it->start_of_box_run_p && slice.x == 0)
22288 it->pixel_width += eabs (face->box_line_width);
22289 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22290 it->pixel_width += eabs (face->box_line_width);
22291 }
22292
22293 take_vertical_position_into_account (it);
22294
22295 /* Automatically crop wide image glyphs at right edge so we can
22296 draw the cursor on same display row. */
22297 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22298 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22299 {
22300 it->pixel_width -= crop;
22301 slice.width -= crop;
22302 }
22303
22304 if (it->glyph_row)
22305 {
22306 struct glyph *glyph;
22307 enum glyph_row_area area = it->area;
22308
22309 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22310 if (glyph < it->glyph_row->glyphs[area + 1])
22311 {
22312 glyph->charpos = CHARPOS (it->position);
22313 glyph->object = it->object;
22314 glyph->pixel_width = it->pixel_width;
22315 glyph->ascent = glyph_ascent;
22316 glyph->descent = it->descent;
22317 glyph->voffset = it->voffset;
22318 glyph->type = IMAGE_GLYPH;
22319 glyph->avoid_cursor_p = it->avoid_cursor_p;
22320 glyph->multibyte_p = it->multibyte_p;
22321 glyph->left_box_line_p = it->start_of_box_run_p;
22322 glyph->right_box_line_p = it->end_of_box_run_p;
22323 glyph->overlaps_vertically_p = 0;
22324 glyph->padding_p = 0;
22325 glyph->glyph_not_available_p = 0;
22326 glyph->face_id = it->face_id;
22327 glyph->u.img_id = img->id;
22328 glyph->slice.img = slice;
22329 glyph->font_type = FONT_TYPE_UNKNOWN;
22330 if (it->bidi_p)
22331 {
22332 glyph->resolved_level = it->bidi_it.resolved_level;
22333 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22334 abort ();
22335 glyph->bidi_type = it->bidi_it.type;
22336 }
22337 ++it->glyph_row->used[area];
22338 }
22339 else
22340 IT_EXPAND_MATRIX_WIDTH (it, area);
22341 }
22342 }
22343
22344
22345 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22346 of the glyph, WIDTH and HEIGHT are the width and height of the
22347 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22348
22349 static void
22350 append_stretch_glyph (struct it *it, Lisp_Object object,
22351 int width, int height, int ascent)
22352 {
22353 struct glyph *glyph;
22354 enum glyph_row_area area = it->area;
22355
22356 xassert (ascent >= 0 && ascent <= height);
22357
22358 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22359 if (glyph < it->glyph_row->glyphs[area + 1])
22360 {
22361 /* If the glyph row is reversed, we need to prepend the glyph
22362 rather than append it. */
22363 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22364 {
22365 struct glyph *g;
22366
22367 /* Make room for the additional glyph. */
22368 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22369 g[1] = *g;
22370 glyph = it->glyph_row->glyphs[area];
22371 }
22372 glyph->charpos = CHARPOS (it->position);
22373 glyph->object = object;
22374 glyph->pixel_width = width;
22375 glyph->ascent = ascent;
22376 glyph->descent = height - ascent;
22377 glyph->voffset = it->voffset;
22378 glyph->type = STRETCH_GLYPH;
22379 glyph->avoid_cursor_p = it->avoid_cursor_p;
22380 glyph->multibyte_p = it->multibyte_p;
22381 glyph->left_box_line_p = it->start_of_box_run_p;
22382 glyph->right_box_line_p = it->end_of_box_run_p;
22383 glyph->overlaps_vertically_p = 0;
22384 glyph->padding_p = 0;
22385 glyph->glyph_not_available_p = 0;
22386 glyph->face_id = it->face_id;
22387 glyph->u.stretch.ascent = ascent;
22388 glyph->u.stretch.height = height;
22389 glyph->slice.img = null_glyph_slice;
22390 glyph->font_type = FONT_TYPE_UNKNOWN;
22391 if (it->bidi_p)
22392 {
22393 glyph->resolved_level = it->bidi_it.resolved_level;
22394 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22395 abort ();
22396 glyph->bidi_type = it->bidi_it.type;
22397 }
22398 else
22399 {
22400 glyph->resolved_level = 0;
22401 glyph->bidi_type = UNKNOWN_BT;
22402 }
22403 ++it->glyph_row->used[area];
22404 }
22405 else
22406 IT_EXPAND_MATRIX_WIDTH (it, area);
22407 }
22408
22409
22410 /* Produce a stretch glyph for iterator IT. IT->object is the value
22411 of the glyph property displayed. The value must be a list
22412 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22413 being recognized:
22414
22415 1. `:width WIDTH' specifies that the space should be WIDTH *
22416 canonical char width wide. WIDTH may be an integer or floating
22417 point number.
22418
22419 2. `:relative-width FACTOR' specifies that the width of the stretch
22420 should be computed from the width of the first character having the
22421 `glyph' property, and should be FACTOR times that width.
22422
22423 3. `:align-to HPOS' specifies that the space should be wide enough
22424 to reach HPOS, a value in canonical character units.
22425
22426 Exactly one of the above pairs must be present.
22427
22428 4. `:height HEIGHT' specifies that the height of the stretch produced
22429 should be HEIGHT, measured in canonical character units.
22430
22431 5. `:relative-height FACTOR' specifies that the height of the
22432 stretch should be FACTOR times the height of the characters having
22433 the glyph property.
22434
22435 Either none or exactly one of 4 or 5 must be present.
22436
22437 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22438 of the stretch should be used for the ascent of the stretch.
22439 ASCENT must be in the range 0 <= ASCENT <= 100. */
22440
22441 static void
22442 produce_stretch_glyph (struct it *it)
22443 {
22444 /* (space :width WIDTH :height HEIGHT ...) */
22445 Lisp_Object prop, plist;
22446 int width = 0, height = 0, align_to = -1;
22447 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22448 int ascent = 0;
22449 double tem;
22450 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22451 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22452
22453 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22454
22455 /* List should start with `space'. */
22456 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22457 plist = XCDR (it->object);
22458
22459 /* Compute the width of the stretch. */
22460 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22461 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22462 {
22463 /* Absolute width `:width WIDTH' specified and valid. */
22464 zero_width_ok_p = 1;
22465 width = (int)tem;
22466 }
22467 else if (prop = Fplist_get (plist, QCrelative_width),
22468 NUMVAL (prop) > 0)
22469 {
22470 /* Relative width `:relative-width FACTOR' specified and valid.
22471 Compute the width of the characters having the `glyph'
22472 property. */
22473 struct it it2;
22474 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22475
22476 it2 = *it;
22477 if (it->multibyte_p)
22478 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22479 else
22480 {
22481 it2.c = it2.char_to_display = *p, it2.len = 1;
22482 if (! ASCII_CHAR_P (it2.c))
22483 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22484 }
22485
22486 it2.glyph_row = NULL;
22487 it2.what = IT_CHARACTER;
22488 x_produce_glyphs (&it2);
22489 width = NUMVAL (prop) * it2.pixel_width;
22490 }
22491 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22492 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22493 {
22494 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22495 align_to = (align_to < 0
22496 ? 0
22497 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22498 else if (align_to < 0)
22499 align_to = window_box_left_offset (it->w, TEXT_AREA);
22500 width = max (0, (int)tem + align_to - it->current_x);
22501 zero_width_ok_p = 1;
22502 }
22503 else
22504 /* Nothing specified -> width defaults to canonical char width. */
22505 width = FRAME_COLUMN_WIDTH (it->f);
22506
22507 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22508 width = 1;
22509
22510 /* Compute height. */
22511 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22512 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22513 {
22514 height = (int)tem;
22515 zero_height_ok_p = 1;
22516 }
22517 else if (prop = Fplist_get (plist, QCrelative_height),
22518 NUMVAL (prop) > 0)
22519 height = FONT_HEIGHT (font) * NUMVAL (prop);
22520 else
22521 height = FONT_HEIGHT (font);
22522
22523 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22524 height = 1;
22525
22526 /* Compute percentage of height used for ascent. If
22527 `:ascent ASCENT' is present and valid, use that. Otherwise,
22528 derive the ascent from the font in use. */
22529 if (prop = Fplist_get (plist, QCascent),
22530 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22531 ascent = height * NUMVAL (prop) / 100.0;
22532 else if (!NILP (prop)
22533 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22534 ascent = min (max (0, (int)tem), height);
22535 else
22536 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22537
22538 if (width > 0 && it->line_wrap != TRUNCATE
22539 && it->current_x + width > it->last_visible_x)
22540 width = it->last_visible_x - it->current_x - 1;
22541
22542 if (width > 0 && height > 0 && it->glyph_row)
22543 {
22544 Lisp_Object object = it->stack[it->sp - 1].string;
22545 if (!STRINGP (object))
22546 object = it->w->buffer;
22547 append_stretch_glyph (it, object, width, height, ascent);
22548 }
22549
22550 it->pixel_width = width;
22551 it->ascent = it->phys_ascent = ascent;
22552 it->descent = it->phys_descent = height - it->ascent;
22553 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22554
22555 take_vertical_position_into_account (it);
22556 }
22557
22558 /* Calculate line-height and line-spacing properties.
22559 An integer value specifies explicit pixel value.
22560 A float value specifies relative value to current face height.
22561 A cons (float . face-name) specifies relative value to
22562 height of specified face font.
22563
22564 Returns height in pixels, or nil. */
22565
22566
22567 static Lisp_Object
22568 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22569 int boff, int override)
22570 {
22571 Lisp_Object face_name = Qnil;
22572 int ascent, descent, height;
22573
22574 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22575 return val;
22576
22577 if (CONSP (val))
22578 {
22579 face_name = XCAR (val);
22580 val = XCDR (val);
22581 if (!NUMBERP (val))
22582 val = make_number (1);
22583 if (NILP (face_name))
22584 {
22585 height = it->ascent + it->descent;
22586 goto scale;
22587 }
22588 }
22589
22590 if (NILP (face_name))
22591 {
22592 font = FRAME_FONT (it->f);
22593 boff = FRAME_BASELINE_OFFSET (it->f);
22594 }
22595 else if (EQ (face_name, Qt))
22596 {
22597 override = 0;
22598 }
22599 else
22600 {
22601 int face_id;
22602 struct face *face;
22603
22604 face_id = lookup_named_face (it->f, face_name, 0);
22605 if (face_id < 0)
22606 return make_number (-1);
22607
22608 face = FACE_FROM_ID (it->f, face_id);
22609 font = face->font;
22610 if (font == NULL)
22611 return make_number (-1);
22612 boff = font->baseline_offset;
22613 if (font->vertical_centering)
22614 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22615 }
22616
22617 ascent = FONT_BASE (font) + boff;
22618 descent = FONT_DESCENT (font) - boff;
22619
22620 if (override)
22621 {
22622 it->override_ascent = ascent;
22623 it->override_descent = descent;
22624 it->override_boff = boff;
22625 }
22626
22627 height = ascent + descent;
22628
22629 scale:
22630 if (FLOATP (val))
22631 height = (int)(XFLOAT_DATA (val) * height);
22632 else if (INTEGERP (val))
22633 height *= XINT (val);
22634
22635 return make_number (height);
22636 }
22637
22638
22639 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22640 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22641 and only if this is for a character for which no font was found.
22642
22643 If the display method (it->glyphless_method) is
22644 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22645 length of the acronym or the hexadecimal string, UPPER_XOFF and
22646 UPPER_YOFF are pixel offsets for the upper part of the string,
22647 LOWER_XOFF and LOWER_YOFF are for the lower part.
22648
22649 For the other display methods, LEN through LOWER_YOFF are zero. */
22650
22651 static void
22652 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22653 short upper_xoff, short upper_yoff,
22654 short lower_xoff, short lower_yoff)
22655 {
22656 struct glyph *glyph;
22657 enum glyph_row_area area = it->area;
22658
22659 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22660 if (glyph < it->glyph_row->glyphs[area + 1])
22661 {
22662 /* If the glyph row is reversed, we need to prepend the glyph
22663 rather than append it. */
22664 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22665 {
22666 struct glyph *g;
22667
22668 /* Make room for the additional glyph. */
22669 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22670 g[1] = *g;
22671 glyph = it->glyph_row->glyphs[area];
22672 }
22673 glyph->charpos = CHARPOS (it->position);
22674 glyph->object = it->object;
22675 glyph->pixel_width = it->pixel_width;
22676 glyph->ascent = it->ascent;
22677 glyph->descent = it->descent;
22678 glyph->voffset = it->voffset;
22679 glyph->type = GLYPHLESS_GLYPH;
22680 glyph->u.glyphless.method = it->glyphless_method;
22681 glyph->u.glyphless.for_no_font = for_no_font;
22682 glyph->u.glyphless.len = len;
22683 glyph->u.glyphless.ch = it->c;
22684 glyph->slice.glyphless.upper_xoff = upper_xoff;
22685 glyph->slice.glyphless.upper_yoff = upper_yoff;
22686 glyph->slice.glyphless.lower_xoff = lower_xoff;
22687 glyph->slice.glyphless.lower_yoff = lower_yoff;
22688 glyph->avoid_cursor_p = it->avoid_cursor_p;
22689 glyph->multibyte_p = it->multibyte_p;
22690 glyph->left_box_line_p = it->start_of_box_run_p;
22691 glyph->right_box_line_p = it->end_of_box_run_p;
22692 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22693 || it->phys_descent > it->descent);
22694 glyph->padding_p = 0;
22695 glyph->glyph_not_available_p = 0;
22696 glyph->face_id = face_id;
22697 glyph->font_type = FONT_TYPE_UNKNOWN;
22698 if (it->bidi_p)
22699 {
22700 glyph->resolved_level = it->bidi_it.resolved_level;
22701 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22702 abort ();
22703 glyph->bidi_type = it->bidi_it.type;
22704 }
22705 ++it->glyph_row->used[area];
22706 }
22707 else
22708 IT_EXPAND_MATRIX_WIDTH (it, area);
22709 }
22710
22711
22712 /* Produce a glyph for a glyphless character for iterator IT.
22713 IT->glyphless_method specifies which method to use for displaying
22714 the character. See the description of enum
22715 glyphless_display_method in dispextern.h for the detail.
22716
22717 FOR_NO_FONT is nonzero if and only if this is for a character for
22718 which no font was found. ACRONYM, if non-nil, is an acronym string
22719 for the character. */
22720
22721 static void
22722 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22723 {
22724 int face_id;
22725 struct face *face;
22726 struct font *font;
22727 int base_width, base_height, width, height;
22728 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22729 int len;
22730
22731 /* Get the metrics of the base font. We always refer to the current
22732 ASCII face. */
22733 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22734 font = face->font ? face->font : FRAME_FONT (it->f);
22735 it->ascent = FONT_BASE (font) + font->baseline_offset;
22736 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22737 base_height = it->ascent + it->descent;
22738 base_width = font->average_width;
22739
22740 /* Get a face ID for the glyph by utilizing a cache (the same way as
22741 doen for `escape-glyph' in get_next_display_element). */
22742 if (it->f == last_glyphless_glyph_frame
22743 && it->face_id == last_glyphless_glyph_face_id)
22744 {
22745 face_id = last_glyphless_glyph_merged_face_id;
22746 }
22747 else
22748 {
22749 /* Merge the `glyphless-char' face into the current face. */
22750 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22751 last_glyphless_glyph_frame = it->f;
22752 last_glyphless_glyph_face_id = it->face_id;
22753 last_glyphless_glyph_merged_face_id = face_id;
22754 }
22755
22756 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22757 {
22758 it->pixel_width = THIN_SPACE_WIDTH;
22759 len = 0;
22760 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22761 }
22762 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22763 {
22764 width = CHAR_WIDTH (it->c);
22765 if (width == 0)
22766 width = 1;
22767 else if (width > 4)
22768 width = 4;
22769 it->pixel_width = base_width * width;
22770 len = 0;
22771 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22772 }
22773 else
22774 {
22775 char buf[7];
22776 const char *str;
22777 unsigned int code[6];
22778 int upper_len;
22779 int ascent, descent;
22780 struct font_metrics metrics_upper, metrics_lower;
22781
22782 face = FACE_FROM_ID (it->f, face_id);
22783 font = face->font ? face->font : FRAME_FONT (it->f);
22784 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22785
22786 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22787 {
22788 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22789 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22790 if (CONSP (acronym))
22791 acronym = XCAR (acronym);
22792 str = STRINGP (acronym) ? SSDATA (acronym) : "";
22793 }
22794 else
22795 {
22796 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22797 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22798 str = buf;
22799 }
22800 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22801 code[len] = font->driver->encode_char (font, str[len]);
22802 upper_len = (len + 1) / 2;
22803 font->driver->text_extents (font, code, upper_len,
22804 &metrics_upper);
22805 font->driver->text_extents (font, code + upper_len, len - upper_len,
22806 &metrics_lower);
22807
22808
22809
22810 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22811 width = max (metrics_upper.width, metrics_lower.width) + 4;
22812 upper_xoff = upper_yoff = 2; /* the typical case */
22813 if (base_width >= width)
22814 {
22815 /* Align the upper to the left, the lower to the right. */
22816 it->pixel_width = base_width;
22817 lower_xoff = base_width - 2 - metrics_lower.width;
22818 }
22819 else
22820 {
22821 /* Center the shorter one. */
22822 it->pixel_width = width;
22823 if (metrics_upper.width >= metrics_lower.width)
22824 lower_xoff = (width - metrics_lower.width) / 2;
22825 else
22826 {
22827 /* FIXME: This code doesn't look right. It formerly was
22828 missing the "lower_xoff = 0;", which couldn't have
22829 been right since it left lower_xoff uninitialized. */
22830 lower_xoff = 0;
22831 upper_xoff = (width - metrics_upper.width) / 2;
22832 }
22833 }
22834
22835 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22836 top, bottom, and between upper and lower strings. */
22837 height = (metrics_upper.ascent + metrics_upper.descent
22838 + metrics_lower.ascent + metrics_lower.descent) + 5;
22839 /* Center vertically.
22840 H:base_height, D:base_descent
22841 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22842
22843 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22844 descent = D - H/2 + h/2;
22845 lower_yoff = descent - 2 - ld;
22846 upper_yoff = lower_yoff - la - 1 - ud; */
22847 ascent = - (it->descent - (base_height + height + 1) / 2);
22848 descent = it->descent - (base_height - height) / 2;
22849 lower_yoff = descent - 2 - metrics_lower.descent;
22850 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22851 - metrics_upper.descent);
22852 /* Don't make the height shorter than the base height. */
22853 if (height > base_height)
22854 {
22855 it->ascent = ascent;
22856 it->descent = descent;
22857 }
22858 }
22859
22860 it->phys_ascent = it->ascent;
22861 it->phys_descent = it->descent;
22862 if (it->glyph_row)
22863 append_glyphless_glyph (it, face_id, for_no_font, len,
22864 upper_xoff, upper_yoff,
22865 lower_xoff, lower_yoff);
22866 it->nglyphs = 1;
22867 take_vertical_position_into_account (it);
22868 }
22869
22870
22871 /* RIF:
22872 Produce glyphs/get display metrics for the display element IT is
22873 loaded with. See the description of struct it in dispextern.h
22874 for an overview of struct it. */
22875
22876 void
22877 x_produce_glyphs (struct it *it)
22878 {
22879 int extra_line_spacing = it->extra_line_spacing;
22880
22881 it->glyph_not_available_p = 0;
22882
22883 if (it->what == IT_CHARACTER)
22884 {
22885 XChar2b char2b;
22886 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22887 struct font *font = face->font;
22888 struct font_metrics *pcm = NULL;
22889 int boff; /* baseline offset */
22890
22891 if (font == NULL)
22892 {
22893 /* When no suitable font is found, display this character by
22894 the method specified in the first extra slot of
22895 Vglyphless_char_display. */
22896 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22897
22898 xassert (it->what == IT_GLYPHLESS);
22899 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22900 goto done;
22901 }
22902
22903 boff = font->baseline_offset;
22904 if (font->vertical_centering)
22905 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22906
22907 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22908 {
22909 int stretched_p;
22910
22911 it->nglyphs = 1;
22912
22913 if (it->override_ascent >= 0)
22914 {
22915 it->ascent = it->override_ascent;
22916 it->descent = it->override_descent;
22917 boff = it->override_boff;
22918 }
22919 else
22920 {
22921 it->ascent = FONT_BASE (font) + boff;
22922 it->descent = FONT_DESCENT (font) - boff;
22923 }
22924
22925 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22926 {
22927 pcm = get_per_char_metric (font, &char2b);
22928 if (pcm->width == 0
22929 && pcm->rbearing == 0 && pcm->lbearing == 0)
22930 pcm = NULL;
22931 }
22932
22933 if (pcm)
22934 {
22935 it->phys_ascent = pcm->ascent + boff;
22936 it->phys_descent = pcm->descent - boff;
22937 it->pixel_width = pcm->width;
22938 }
22939 else
22940 {
22941 it->glyph_not_available_p = 1;
22942 it->phys_ascent = it->ascent;
22943 it->phys_descent = it->descent;
22944 it->pixel_width = font->space_width;
22945 }
22946
22947 if (it->constrain_row_ascent_descent_p)
22948 {
22949 if (it->descent > it->max_descent)
22950 {
22951 it->ascent += it->descent - it->max_descent;
22952 it->descent = it->max_descent;
22953 }
22954 if (it->ascent > it->max_ascent)
22955 {
22956 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22957 it->ascent = it->max_ascent;
22958 }
22959 it->phys_ascent = min (it->phys_ascent, it->ascent);
22960 it->phys_descent = min (it->phys_descent, it->descent);
22961 extra_line_spacing = 0;
22962 }
22963
22964 /* If this is a space inside a region of text with
22965 `space-width' property, change its width. */
22966 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22967 if (stretched_p)
22968 it->pixel_width *= XFLOATINT (it->space_width);
22969
22970 /* If face has a box, add the box thickness to the character
22971 height. If character has a box line to the left and/or
22972 right, add the box line width to the character's width. */
22973 if (face->box != FACE_NO_BOX)
22974 {
22975 int thick = face->box_line_width;
22976
22977 if (thick > 0)
22978 {
22979 it->ascent += thick;
22980 it->descent += thick;
22981 }
22982 else
22983 thick = -thick;
22984
22985 if (it->start_of_box_run_p)
22986 it->pixel_width += thick;
22987 if (it->end_of_box_run_p)
22988 it->pixel_width += thick;
22989 }
22990
22991 /* If face has an overline, add the height of the overline
22992 (1 pixel) and a 1 pixel margin to the character height. */
22993 if (face->overline_p)
22994 it->ascent += overline_margin;
22995
22996 if (it->constrain_row_ascent_descent_p)
22997 {
22998 if (it->ascent > it->max_ascent)
22999 it->ascent = it->max_ascent;
23000 if (it->descent > it->max_descent)
23001 it->descent = it->max_descent;
23002 }
23003
23004 take_vertical_position_into_account (it);
23005
23006 /* If we have to actually produce glyphs, do it. */
23007 if (it->glyph_row)
23008 {
23009 if (stretched_p)
23010 {
23011 /* Translate a space with a `space-width' property
23012 into a stretch glyph. */
23013 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23014 / FONT_HEIGHT (font));
23015 append_stretch_glyph (it, it->object, it->pixel_width,
23016 it->ascent + it->descent, ascent);
23017 }
23018 else
23019 append_glyph (it);
23020
23021 /* If characters with lbearing or rbearing are displayed
23022 in this line, record that fact in a flag of the
23023 glyph row. This is used to optimize X output code. */
23024 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23025 it->glyph_row->contains_overlapping_glyphs_p = 1;
23026 }
23027 if (! stretched_p && it->pixel_width == 0)
23028 /* We assure that all visible glyphs have at least 1-pixel
23029 width. */
23030 it->pixel_width = 1;
23031 }
23032 else if (it->char_to_display == '\n')
23033 {
23034 /* A newline has no width, but we need the height of the
23035 line. But if previous part of the line sets a height,
23036 don't increase that height */
23037
23038 Lisp_Object height;
23039 Lisp_Object total_height = Qnil;
23040
23041 it->override_ascent = -1;
23042 it->pixel_width = 0;
23043 it->nglyphs = 0;
23044
23045 height = get_it_property (it, Qline_height);
23046 /* Split (line-height total-height) list */
23047 if (CONSP (height)
23048 && CONSP (XCDR (height))
23049 && NILP (XCDR (XCDR (height))))
23050 {
23051 total_height = XCAR (XCDR (height));
23052 height = XCAR (height);
23053 }
23054 height = calc_line_height_property (it, height, font, boff, 1);
23055
23056 if (it->override_ascent >= 0)
23057 {
23058 it->ascent = it->override_ascent;
23059 it->descent = it->override_descent;
23060 boff = it->override_boff;
23061 }
23062 else
23063 {
23064 it->ascent = FONT_BASE (font) + boff;
23065 it->descent = FONT_DESCENT (font) - boff;
23066 }
23067
23068 if (EQ (height, Qt))
23069 {
23070 if (it->descent > it->max_descent)
23071 {
23072 it->ascent += it->descent - it->max_descent;
23073 it->descent = it->max_descent;
23074 }
23075 if (it->ascent > it->max_ascent)
23076 {
23077 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23078 it->ascent = it->max_ascent;
23079 }
23080 it->phys_ascent = min (it->phys_ascent, it->ascent);
23081 it->phys_descent = min (it->phys_descent, it->descent);
23082 it->constrain_row_ascent_descent_p = 1;
23083 extra_line_spacing = 0;
23084 }
23085 else
23086 {
23087 Lisp_Object spacing;
23088
23089 it->phys_ascent = it->ascent;
23090 it->phys_descent = it->descent;
23091
23092 if ((it->max_ascent > 0 || it->max_descent > 0)
23093 && face->box != FACE_NO_BOX
23094 && face->box_line_width > 0)
23095 {
23096 it->ascent += face->box_line_width;
23097 it->descent += face->box_line_width;
23098 }
23099 if (!NILP (height)
23100 && XINT (height) > it->ascent + it->descent)
23101 it->ascent = XINT (height) - it->descent;
23102
23103 if (!NILP (total_height))
23104 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23105 else
23106 {
23107 spacing = get_it_property (it, Qline_spacing);
23108 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23109 }
23110 if (INTEGERP (spacing))
23111 {
23112 extra_line_spacing = XINT (spacing);
23113 if (!NILP (total_height))
23114 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23115 }
23116 }
23117 }
23118 else /* i.e. (it->char_to_display == '\t') */
23119 {
23120 if (font->space_width > 0)
23121 {
23122 int tab_width = it->tab_width * font->space_width;
23123 int x = it->current_x + it->continuation_lines_width;
23124 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23125
23126 /* If the distance from the current position to the next tab
23127 stop is less than a space character width, use the
23128 tab stop after that. */
23129 if (next_tab_x - x < font->space_width)
23130 next_tab_x += tab_width;
23131
23132 it->pixel_width = next_tab_x - x;
23133 it->nglyphs = 1;
23134 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23135 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23136
23137 if (it->glyph_row)
23138 {
23139 append_stretch_glyph (it, it->object, it->pixel_width,
23140 it->ascent + it->descent, it->ascent);
23141 }
23142 }
23143 else
23144 {
23145 it->pixel_width = 0;
23146 it->nglyphs = 1;
23147 }
23148 }
23149 }
23150 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23151 {
23152 /* A static composition.
23153
23154 Note: A composition is represented as one glyph in the
23155 glyph matrix. There are no padding glyphs.
23156
23157 Important note: pixel_width, ascent, and descent are the
23158 values of what is drawn by draw_glyphs (i.e. the values of
23159 the overall glyphs composed). */
23160 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23161 int boff; /* baseline offset */
23162 struct composition *cmp = composition_table[it->cmp_it.id];
23163 int glyph_len = cmp->glyph_len;
23164 struct font *font = face->font;
23165
23166 it->nglyphs = 1;
23167
23168 /* If we have not yet calculated pixel size data of glyphs of
23169 the composition for the current face font, calculate them
23170 now. Theoretically, we have to check all fonts for the
23171 glyphs, but that requires much time and memory space. So,
23172 here we check only the font of the first glyph. This may
23173 lead to incorrect display, but it's very rare, and C-l
23174 (recenter-top-bottom) can correct the display anyway. */
23175 if (! cmp->font || cmp->font != font)
23176 {
23177 /* Ascent and descent of the font of the first character
23178 of this composition (adjusted by baseline offset).
23179 Ascent and descent of overall glyphs should not be less
23180 than these, respectively. */
23181 int font_ascent, font_descent, font_height;
23182 /* Bounding box of the overall glyphs. */
23183 int leftmost, rightmost, lowest, highest;
23184 int lbearing, rbearing;
23185 int i, width, ascent, descent;
23186 int left_padded = 0, right_padded = 0;
23187 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23188 XChar2b char2b;
23189 struct font_metrics *pcm;
23190 int font_not_found_p;
23191 EMACS_INT pos;
23192
23193 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23194 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23195 break;
23196 if (glyph_len < cmp->glyph_len)
23197 right_padded = 1;
23198 for (i = 0; i < glyph_len; i++)
23199 {
23200 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23201 break;
23202 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23203 }
23204 if (i > 0)
23205 left_padded = 1;
23206
23207 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23208 : IT_CHARPOS (*it));
23209 /* If no suitable font is found, use the default font. */
23210 font_not_found_p = font == NULL;
23211 if (font_not_found_p)
23212 {
23213 face = face->ascii_face;
23214 font = face->font;
23215 }
23216 boff = font->baseline_offset;
23217 if (font->vertical_centering)
23218 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23219 font_ascent = FONT_BASE (font) + boff;
23220 font_descent = FONT_DESCENT (font) - boff;
23221 font_height = FONT_HEIGHT (font);
23222
23223 cmp->font = (void *) font;
23224
23225 pcm = NULL;
23226 if (! font_not_found_p)
23227 {
23228 get_char_face_and_encoding (it->f, c, it->face_id,
23229 &char2b, 0);
23230 pcm = get_per_char_metric (font, &char2b);
23231 }
23232
23233 /* Initialize the bounding box. */
23234 if (pcm)
23235 {
23236 width = pcm->width;
23237 ascent = pcm->ascent;
23238 descent = pcm->descent;
23239 lbearing = pcm->lbearing;
23240 rbearing = pcm->rbearing;
23241 }
23242 else
23243 {
23244 width = font->space_width;
23245 ascent = FONT_BASE (font);
23246 descent = FONT_DESCENT (font);
23247 lbearing = 0;
23248 rbearing = width;
23249 }
23250
23251 rightmost = width;
23252 leftmost = 0;
23253 lowest = - descent + boff;
23254 highest = ascent + boff;
23255
23256 if (! font_not_found_p
23257 && font->default_ascent
23258 && CHAR_TABLE_P (Vuse_default_ascent)
23259 && !NILP (Faref (Vuse_default_ascent,
23260 make_number (it->char_to_display))))
23261 highest = font->default_ascent + boff;
23262
23263 /* Draw the first glyph at the normal position. It may be
23264 shifted to right later if some other glyphs are drawn
23265 at the left. */
23266 cmp->offsets[i * 2] = 0;
23267 cmp->offsets[i * 2 + 1] = boff;
23268 cmp->lbearing = lbearing;
23269 cmp->rbearing = rbearing;
23270
23271 /* Set cmp->offsets for the remaining glyphs. */
23272 for (i++; i < glyph_len; i++)
23273 {
23274 int left, right, btm, top;
23275 int ch = COMPOSITION_GLYPH (cmp, i);
23276 int face_id;
23277 struct face *this_face;
23278
23279 if (ch == '\t')
23280 ch = ' ';
23281 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23282 this_face = FACE_FROM_ID (it->f, face_id);
23283 font = this_face->font;
23284
23285 if (font == NULL)
23286 pcm = NULL;
23287 else
23288 {
23289 get_char_face_and_encoding (it->f, ch, face_id,
23290 &char2b, 0);
23291 pcm = get_per_char_metric (font, &char2b);
23292 }
23293 if (! pcm)
23294 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23295 else
23296 {
23297 width = pcm->width;
23298 ascent = pcm->ascent;
23299 descent = pcm->descent;
23300 lbearing = pcm->lbearing;
23301 rbearing = pcm->rbearing;
23302 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23303 {
23304 /* Relative composition with or without
23305 alternate chars. */
23306 left = (leftmost + rightmost - width) / 2;
23307 btm = - descent + boff;
23308 if (font->relative_compose
23309 && (! CHAR_TABLE_P (Vignore_relative_composition)
23310 || NILP (Faref (Vignore_relative_composition,
23311 make_number (ch)))))
23312 {
23313
23314 if (- descent >= font->relative_compose)
23315 /* One extra pixel between two glyphs. */
23316 btm = highest + 1;
23317 else if (ascent <= 0)
23318 /* One extra pixel between two glyphs. */
23319 btm = lowest - 1 - ascent - descent;
23320 }
23321 }
23322 else
23323 {
23324 /* A composition rule is specified by an integer
23325 value that encodes global and new reference
23326 points (GREF and NREF). GREF and NREF are
23327 specified by numbers as below:
23328
23329 0---1---2 -- ascent
23330 | |
23331 | |
23332 | |
23333 9--10--11 -- center
23334 | |
23335 ---3---4---5--- baseline
23336 | |
23337 6---7---8 -- descent
23338 */
23339 int rule = COMPOSITION_RULE (cmp, i);
23340 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23341
23342 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23343 grefx = gref % 3, nrefx = nref % 3;
23344 grefy = gref / 3, nrefy = nref / 3;
23345 if (xoff)
23346 xoff = font_height * (xoff - 128) / 256;
23347 if (yoff)
23348 yoff = font_height * (yoff - 128) / 256;
23349
23350 left = (leftmost
23351 + grefx * (rightmost - leftmost) / 2
23352 - nrefx * width / 2
23353 + xoff);
23354
23355 btm = ((grefy == 0 ? highest
23356 : grefy == 1 ? 0
23357 : grefy == 2 ? lowest
23358 : (highest + lowest) / 2)
23359 - (nrefy == 0 ? ascent + descent
23360 : nrefy == 1 ? descent - boff
23361 : nrefy == 2 ? 0
23362 : (ascent + descent) / 2)
23363 + yoff);
23364 }
23365
23366 cmp->offsets[i * 2] = left;
23367 cmp->offsets[i * 2 + 1] = btm + descent;
23368
23369 /* Update the bounding box of the overall glyphs. */
23370 if (width > 0)
23371 {
23372 right = left + width;
23373 if (left < leftmost)
23374 leftmost = left;
23375 if (right > rightmost)
23376 rightmost = right;
23377 }
23378 top = btm + descent + ascent;
23379 if (top > highest)
23380 highest = top;
23381 if (btm < lowest)
23382 lowest = btm;
23383
23384 if (cmp->lbearing > left + lbearing)
23385 cmp->lbearing = left + lbearing;
23386 if (cmp->rbearing < left + rbearing)
23387 cmp->rbearing = left + rbearing;
23388 }
23389 }
23390
23391 /* If there are glyphs whose x-offsets are negative,
23392 shift all glyphs to the right and make all x-offsets
23393 non-negative. */
23394 if (leftmost < 0)
23395 {
23396 for (i = 0; i < cmp->glyph_len; i++)
23397 cmp->offsets[i * 2] -= leftmost;
23398 rightmost -= leftmost;
23399 cmp->lbearing -= leftmost;
23400 cmp->rbearing -= leftmost;
23401 }
23402
23403 if (left_padded && cmp->lbearing < 0)
23404 {
23405 for (i = 0; i < cmp->glyph_len; i++)
23406 cmp->offsets[i * 2] -= cmp->lbearing;
23407 rightmost -= cmp->lbearing;
23408 cmp->rbearing -= cmp->lbearing;
23409 cmp->lbearing = 0;
23410 }
23411 if (right_padded && rightmost < cmp->rbearing)
23412 {
23413 rightmost = cmp->rbearing;
23414 }
23415
23416 cmp->pixel_width = rightmost;
23417 cmp->ascent = highest;
23418 cmp->descent = - lowest;
23419 if (cmp->ascent < font_ascent)
23420 cmp->ascent = font_ascent;
23421 if (cmp->descent < font_descent)
23422 cmp->descent = font_descent;
23423 }
23424
23425 if (it->glyph_row
23426 && (cmp->lbearing < 0
23427 || cmp->rbearing > cmp->pixel_width))
23428 it->glyph_row->contains_overlapping_glyphs_p = 1;
23429
23430 it->pixel_width = cmp->pixel_width;
23431 it->ascent = it->phys_ascent = cmp->ascent;
23432 it->descent = it->phys_descent = cmp->descent;
23433 if (face->box != FACE_NO_BOX)
23434 {
23435 int thick = face->box_line_width;
23436
23437 if (thick > 0)
23438 {
23439 it->ascent += thick;
23440 it->descent += thick;
23441 }
23442 else
23443 thick = - thick;
23444
23445 if (it->start_of_box_run_p)
23446 it->pixel_width += thick;
23447 if (it->end_of_box_run_p)
23448 it->pixel_width += thick;
23449 }
23450
23451 /* If face has an overline, add the height of the overline
23452 (1 pixel) and a 1 pixel margin to the character height. */
23453 if (face->overline_p)
23454 it->ascent += overline_margin;
23455
23456 take_vertical_position_into_account (it);
23457 if (it->ascent < 0)
23458 it->ascent = 0;
23459 if (it->descent < 0)
23460 it->descent = 0;
23461
23462 if (it->glyph_row)
23463 append_composite_glyph (it);
23464 }
23465 else if (it->what == IT_COMPOSITION)
23466 {
23467 /* A dynamic (automatic) composition. */
23468 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23469 Lisp_Object gstring;
23470 struct font_metrics metrics;
23471
23472 gstring = composition_gstring_from_id (it->cmp_it.id);
23473 it->pixel_width
23474 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23475 &metrics);
23476 if (it->glyph_row
23477 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23478 it->glyph_row->contains_overlapping_glyphs_p = 1;
23479 it->ascent = it->phys_ascent = metrics.ascent;
23480 it->descent = it->phys_descent = metrics.descent;
23481 if (face->box != FACE_NO_BOX)
23482 {
23483 int thick = face->box_line_width;
23484
23485 if (thick > 0)
23486 {
23487 it->ascent += thick;
23488 it->descent += thick;
23489 }
23490 else
23491 thick = - thick;
23492
23493 if (it->start_of_box_run_p)
23494 it->pixel_width += thick;
23495 if (it->end_of_box_run_p)
23496 it->pixel_width += thick;
23497 }
23498 /* If face has an overline, add the height of the overline
23499 (1 pixel) and a 1 pixel margin to the character height. */
23500 if (face->overline_p)
23501 it->ascent += overline_margin;
23502 take_vertical_position_into_account (it);
23503 if (it->ascent < 0)
23504 it->ascent = 0;
23505 if (it->descent < 0)
23506 it->descent = 0;
23507
23508 if (it->glyph_row)
23509 append_composite_glyph (it);
23510 }
23511 else if (it->what == IT_GLYPHLESS)
23512 produce_glyphless_glyph (it, 0, Qnil);
23513 else if (it->what == IT_IMAGE)
23514 produce_image_glyph (it);
23515 else if (it->what == IT_STRETCH)
23516 produce_stretch_glyph (it);
23517
23518 done:
23519 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23520 because this isn't true for images with `:ascent 100'. */
23521 xassert (it->ascent >= 0 && it->descent >= 0);
23522 if (it->area == TEXT_AREA)
23523 it->current_x += it->pixel_width;
23524
23525 if (extra_line_spacing > 0)
23526 {
23527 it->descent += extra_line_spacing;
23528 if (extra_line_spacing > it->max_extra_line_spacing)
23529 it->max_extra_line_spacing = extra_line_spacing;
23530 }
23531
23532 it->max_ascent = max (it->max_ascent, it->ascent);
23533 it->max_descent = max (it->max_descent, it->descent);
23534 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23535 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23536 }
23537
23538 /* EXPORT for RIF:
23539 Output LEN glyphs starting at START at the nominal cursor position.
23540 Advance the nominal cursor over the text. The global variable
23541 updated_window contains the window being updated, updated_row is
23542 the glyph row being updated, and updated_area is the area of that
23543 row being updated. */
23544
23545 void
23546 x_write_glyphs (struct glyph *start, int len)
23547 {
23548 int x, hpos;
23549
23550 xassert (updated_window && updated_row);
23551 BLOCK_INPUT;
23552
23553 /* Write glyphs. */
23554
23555 hpos = start - updated_row->glyphs[updated_area];
23556 x = draw_glyphs (updated_window, output_cursor.x,
23557 updated_row, updated_area,
23558 hpos, hpos + len,
23559 DRAW_NORMAL_TEXT, 0);
23560
23561 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23562 if (updated_area == TEXT_AREA
23563 && updated_window->phys_cursor_on_p
23564 && updated_window->phys_cursor.vpos == output_cursor.vpos
23565 && updated_window->phys_cursor.hpos >= hpos
23566 && updated_window->phys_cursor.hpos < hpos + len)
23567 updated_window->phys_cursor_on_p = 0;
23568
23569 UNBLOCK_INPUT;
23570
23571 /* Advance the output cursor. */
23572 output_cursor.hpos += len;
23573 output_cursor.x = x;
23574 }
23575
23576
23577 /* EXPORT for RIF:
23578 Insert LEN glyphs from START at the nominal cursor position. */
23579
23580 void
23581 x_insert_glyphs (struct glyph *start, int len)
23582 {
23583 struct frame *f;
23584 struct window *w;
23585 int line_height, shift_by_width, shifted_region_width;
23586 struct glyph_row *row;
23587 struct glyph *glyph;
23588 int frame_x, frame_y;
23589 EMACS_INT hpos;
23590
23591 xassert (updated_window && updated_row);
23592 BLOCK_INPUT;
23593 w = updated_window;
23594 f = XFRAME (WINDOW_FRAME (w));
23595
23596 /* Get the height of the line we are in. */
23597 row = updated_row;
23598 line_height = row->height;
23599
23600 /* Get the width of the glyphs to insert. */
23601 shift_by_width = 0;
23602 for (glyph = start; glyph < start + len; ++glyph)
23603 shift_by_width += glyph->pixel_width;
23604
23605 /* Get the width of the region to shift right. */
23606 shifted_region_width = (window_box_width (w, updated_area)
23607 - output_cursor.x
23608 - shift_by_width);
23609
23610 /* Shift right. */
23611 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23612 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23613
23614 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23615 line_height, shift_by_width);
23616
23617 /* Write the glyphs. */
23618 hpos = start - row->glyphs[updated_area];
23619 draw_glyphs (w, output_cursor.x, row, updated_area,
23620 hpos, hpos + len,
23621 DRAW_NORMAL_TEXT, 0);
23622
23623 /* Advance the output cursor. */
23624 output_cursor.hpos += len;
23625 output_cursor.x += shift_by_width;
23626 UNBLOCK_INPUT;
23627 }
23628
23629
23630 /* EXPORT for RIF:
23631 Erase the current text line from the nominal cursor position
23632 (inclusive) to pixel column TO_X (exclusive). The idea is that
23633 everything from TO_X onward is already erased.
23634
23635 TO_X is a pixel position relative to updated_area of
23636 updated_window. TO_X == -1 means clear to the end of this area. */
23637
23638 void
23639 x_clear_end_of_line (int to_x)
23640 {
23641 struct frame *f;
23642 struct window *w = updated_window;
23643 int max_x, min_y, max_y;
23644 int from_x, from_y, to_y;
23645
23646 xassert (updated_window && updated_row);
23647 f = XFRAME (w->frame);
23648
23649 if (updated_row->full_width_p)
23650 max_x = WINDOW_TOTAL_WIDTH (w);
23651 else
23652 max_x = window_box_width (w, updated_area);
23653 max_y = window_text_bottom_y (w);
23654
23655 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23656 of window. For TO_X > 0, truncate to end of drawing area. */
23657 if (to_x == 0)
23658 return;
23659 else if (to_x < 0)
23660 to_x = max_x;
23661 else
23662 to_x = min (to_x, max_x);
23663
23664 to_y = min (max_y, output_cursor.y + updated_row->height);
23665
23666 /* Notice if the cursor will be cleared by this operation. */
23667 if (!updated_row->full_width_p)
23668 notice_overwritten_cursor (w, updated_area,
23669 output_cursor.x, -1,
23670 updated_row->y,
23671 MATRIX_ROW_BOTTOM_Y (updated_row));
23672
23673 from_x = output_cursor.x;
23674
23675 /* Translate to frame coordinates. */
23676 if (updated_row->full_width_p)
23677 {
23678 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23679 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23680 }
23681 else
23682 {
23683 int area_left = window_box_left (w, updated_area);
23684 from_x += area_left;
23685 to_x += area_left;
23686 }
23687
23688 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23689 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23690 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23691
23692 /* Prevent inadvertently clearing to end of the X window. */
23693 if (to_x > from_x && to_y > from_y)
23694 {
23695 BLOCK_INPUT;
23696 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23697 to_x - from_x, to_y - from_y);
23698 UNBLOCK_INPUT;
23699 }
23700 }
23701
23702 #endif /* HAVE_WINDOW_SYSTEM */
23703
23704
23705 \f
23706 /***********************************************************************
23707 Cursor types
23708 ***********************************************************************/
23709
23710 /* Value is the internal representation of the specified cursor type
23711 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23712 of the bar cursor. */
23713
23714 static enum text_cursor_kinds
23715 get_specified_cursor_type (Lisp_Object arg, int *width)
23716 {
23717 enum text_cursor_kinds type;
23718
23719 if (NILP (arg))
23720 return NO_CURSOR;
23721
23722 if (EQ (arg, Qbox))
23723 return FILLED_BOX_CURSOR;
23724
23725 if (EQ (arg, Qhollow))
23726 return HOLLOW_BOX_CURSOR;
23727
23728 if (EQ (arg, Qbar))
23729 {
23730 *width = 2;
23731 return BAR_CURSOR;
23732 }
23733
23734 if (CONSP (arg)
23735 && EQ (XCAR (arg), Qbar)
23736 && INTEGERP (XCDR (arg))
23737 && XINT (XCDR (arg)) >= 0)
23738 {
23739 *width = XINT (XCDR (arg));
23740 return BAR_CURSOR;
23741 }
23742
23743 if (EQ (arg, Qhbar))
23744 {
23745 *width = 2;
23746 return HBAR_CURSOR;
23747 }
23748
23749 if (CONSP (arg)
23750 && EQ (XCAR (arg), Qhbar)
23751 && INTEGERP (XCDR (arg))
23752 && XINT (XCDR (arg)) >= 0)
23753 {
23754 *width = XINT (XCDR (arg));
23755 return HBAR_CURSOR;
23756 }
23757
23758 /* Treat anything unknown as "hollow box cursor".
23759 It was bad to signal an error; people have trouble fixing
23760 .Xdefaults with Emacs, when it has something bad in it. */
23761 type = HOLLOW_BOX_CURSOR;
23762
23763 return type;
23764 }
23765
23766 /* Set the default cursor types for specified frame. */
23767 void
23768 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23769 {
23770 int width = 1;
23771 Lisp_Object tem;
23772
23773 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23774 FRAME_CURSOR_WIDTH (f) = width;
23775
23776 /* By default, set up the blink-off state depending on the on-state. */
23777
23778 tem = Fassoc (arg, Vblink_cursor_alist);
23779 if (!NILP (tem))
23780 {
23781 FRAME_BLINK_OFF_CURSOR (f)
23782 = get_specified_cursor_type (XCDR (tem), &width);
23783 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23784 }
23785 else
23786 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23787 }
23788
23789
23790 #ifdef HAVE_WINDOW_SYSTEM
23791
23792 /* Return the cursor we want to be displayed in window W. Return
23793 width of bar/hbar cursor through WIDTH arg. Return with
23794 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23795 (i.e. if the `system caret' should track this cursor).
23796
23797 In a mini-buffer window, we want the cursor only to appear if we
23798 are reading input from this window. For the selected window, we
23799 want the cursor type given by the frame parameter or buffer local
23800 setting of cursor-type. If explicitly marked off, draw no cursor.
23801 In all other cases, we want a hollow box cursor. */
23802
23803 static enum text_cursor_kinds
23804 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23805 int *active_cursor)
23806 {
23807 struct frame *f = XFRAME (w->frame);
23808 struct buffer *b = XBUFFER (w->buffer);
23809 int cursor_type = DEFAULT_CURSOR;
23810 Lisp_Object alt_cursor;
23811 int non_selected = 0;
23812
23813 *active_cursor = 1;
23814
23815 /* Echo area */
23816 if (cursor_in_echo_area
23817 && FRAME_HAS_MINIBUF_P (f)
23818 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23819 {
23820 if (w == XWINDOW (echo_area_window))
23821 {
23822 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
23823 {
23824 *width = FRAME_CURSOR_WIDTH (f);
23825 return FRAME_DESIRED_CURSOR (f);
23826 }
23827 else
23828 return get_specified_cursor_type (BVAR (b, cursor_type), width);
23829 }
23830
23831 *active_cursor = 0;
23832 non_selected = 1;
23833 }
23834
23835 /* Detect a nonselected window or nonselected frame. */
23836 else if (w != XWINDOW (f->selected_window)
23837 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23838 {
23839 *active_cursor = 0;
23840
23841 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23842 return NO_CURSOR;
23843
23844 non_selected = 1;
23845 }
23846
23847 /* Never display a cursor in a window in which cursor-type is nil. */
23848 if (NILP (BVAR (b, cursor_type)))
23849 return NO_CURSOR;
23850
23851 /* Get the normal cursor type for this window. */
23852 if (EQ (BVAR (b, cursor_type), Qt))
23853 {
23854 cursor_type = FRAME_DESIRED_CURSOR (f);
23855 *width = FRAME_CURSOR_WIDTH (f);
23856 }
23857 else
23858 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
23859
23860 /* Use cursor-in-non-selected-windows instead
23861 for non-selected window or frame. */
23862 if (non_selected)
23863 {
23864 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
23865 if (!EQ (Qt, alt_cursor))
23866 return get_specified_cursor_type (alt_cursor, width);
23867 /* t means modify the normal cursor type. */
23868 if (cursor_type == FILLED_BOX_CURSOR)
23869 cursor_type = HOLLOW_BOX_CURSOR;
23870 else if (cursor_type == BAR_CURSOR && *width > 1)
23871 --*width;
23872 return cursor_type;
23873 }
23874
23875 /* Use normal cursor if not blinked off. */
23876 if (!w->cursor_off_p)
23877 {
23878 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23879 {
23880 if (cursor_type == FILLED_BOX_CURSOR)
23881 {
23882 /* Using a block cursor on large images can be very annoying.
23883 So use a hollow cursor for "large" images.
23884 If image is not transparent (no mask), also use hollow cursor. */
23885 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23886 if (img != NULL && IMAGEP (img->spec))
23887 {
23888 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23889 where N = size of default frame font size.
23890 This should cover most of the "tiny" icons people may use. */
23891 if (!img->mask
23892 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23893 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23894 cursor_type = HOLLOW_BOX_CURSOR;
23895 }
23896 }
23897 else if (cursor_type != NO_CURSOR)
23898 {
23899 /* Display current only supports BOX and HOLLOW cursors for images.
23900 So for now, unconditionally use a HOLLOW cursor when cursor is
23901 not a solid box cursor. */
23902 cursor_type = HOLLOW_BOX_CURSOR;
23903 }
23904 }
23905 return cursor_type;
23906 }
23907
23908 /* Cursor is blinked off, so determine how to "toggle" it. */
23909
23910 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23911 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
23912 return get_specified_cursor_type (XCDR (alt_cursor), width);
23913
23914 /* Then see if frame has specified a specific blink off cursor type. */
23915 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23916 {
23917 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23918 return FRAME_BLINK_OFF_CURSOR (f);
23919 }
23920
23921 #if 0
23922 /* Some people liked having a permanently visible blinking cursor,
23923 while others had very strong opinions against it. So it was
23924 decided to remove it. KFS 2003-09-03 */
23925
23926 /* Finally perform built-in cursor blinking:
23927 filled box <-> hollow box
23928 wide [h]bar <-> narrow [h]bar
23929 narrow [h]bar <-> no cursor
23930 other type <-> no cursor */
23931
23932 if (cursor_type == FILLED_BOX_CURSOR)
23933 return HOLLOW_BOX_CURSOR;
23934
23935 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23936 {
23937 *width = 1;
23938 return cursor_type;
23939 }
23940 #endif
23941
23942 return NO_CURSOR;
23943 }
23944
23945
23946 /* Notice when the text cursor of window W has been completely
23947 overwritten by a drawing operation that outputs glyphs in AREA
23948 starting at X0 and ending at X1 in the line starting at Y0 and
23949 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23950 the rest of the line after X0 has been written. Y coordinates
23951 are window-relative. */
23952
23953 static void
23954 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23955 int x0, int x1, int y0, int y1)
23956 {
23957 int cx0, cx1, cy0, cy1;
23958 struct glyph_row *row;
23959
23960 if (!w->phys_cursor_on_p)
23961 return;
23962 if (area != TEXT_AREA)
23963 return;
23964
23965 if (w->phys_cursor.vpos < 0
23966 || w->phys_cursor.vpos >= w->current_matrix->nrows
23967 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23968 !(row->enabled_p && row->displays_text_p)))
23969 return;
23970
23971 if (row->cursor_in_fringe_p)
23972 {
23973 row->cursor_in_fringe_p = 0;
23974 draw_fringe_bitmap (w, row, row->reversed_p);
23975 w->phys_cursor_on_p = 0;
23976 return;
23977 }
23978
23979 cx0 = w->phys_cursor.x;
23980 cx1 = cx0 + w->phys_cursor_width;
23981 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23982 return;
23983
23984 /* The cursor image will be completely removed from the
23985 screen if the output area intersects the cursor area in
23986 y-direction. When we draw in [y0 y1[, and some part of
23987 the cursor is at y < y0, that part must have been drawn
23988 before. When scrolling, the cursor is erased before
23989 actually scrolling, so we don't come here. When not
23990 scrolling, the rows above the old cursor row must have
23991 changed, and in this case these rows must have written
23992 over the cursor image.
23993
23994 Likewise if part of the cursor is below y1, with the
23995 exception of the cursor being in the first blank row at
23996 the buffer and window end because update_text_area
23997 doesn't draw that row. (Except when it does, but
23998 that's handled in update_text_area.) */
23999
24000 cy0 = w->phys_cursor.y;
24001 cy1 = cy0 + w->phys_cursor_height;
24002 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24003 return;
24004
24005 w->phys_cursor_on_p = 0;
24006 }
24007
24008 #endif /* HAVE_WINDOW_SYSTEM */
24009
24010 \f
24011 /************************************************************************
24012 Mouse Face
24013 ************************************************************************/
24014
24015 #ifdef HAVE_WINDOW_SYSTEM
24016
24017 /* EXPORT for RIF:
24018 Fix the display of area AREA of overlapping row ROW in window W
24019 with respect to the overlapping part OVERLAPS. */
24020
24021 void
24022 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24023 enum glyph_row_area area, int overlaps)
24024 {
24025 int i, x;
24026
24027 BLOCK_INPUT;
24028
24029 x = 0;
24030 for (i = 0; i < row->used[area];)
24031 {
24032 if (row->glyphs[area][i].overlaps_vertically_p)
24033 {
24034 int start = i, start_x = x;
24035
24036 do
24037 {
24038 x += row->glyphs[area][i].pixel_width;
24039 ++i;
24040 }
24041 while (i < row->used[area]
24042 && row->glyphs[area][i].overlaps_vertically_p);
24043
24044 draw_glyphs (w, start_x, row, area,
24045 start, i,
24046 DRAW_NORMAL_TEXT, overlaps);
24047 }
24048 else
24049 {
24050 x += row->glyphs[area][i].pixel_width;
24051 ++i;
24052 }
24053 }
24054
24055 UNBLOCK_INPUT;
24056 }
24057
24058
24059 /* EXPORT:
24060 Draw the cursor glyph of window W in glyph row ROW. See the
24061 comment of draw_glyphs for the meaning of HL. */
24062
24063 void
24064 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24065 enum draw_glyphs_face hl)
24066 {
24067 /* If cursor hpos is out of bounds, don't draw garbage. This can
24068 happen in mini-buffer windows when switching between echo area
24069 glyphs and mini-buffer. */
24070 if ((row->reversed_p
24071 ? (w->phys_cursor.hpos >= 0)
24072 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24073 {
24074 int on_p = w->phys_cursor_on_p;
24075 int x1;
24076 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24077 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24078 hl, 0);
24079 w->phys_cursor_on_p = on_p;
24080
24081 if (hl == DRAW_CURSOR)
24082 w->phys_cursor_width = x1 - w->phys_cursor.x;
24083 /* When we erase the cursor, and ROW is overlapped by other
24084 rows, make sure that these overlapping parts of other rows
24085 are redrawn. */
24086 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24087 {
24088 w->phys_cursor_width = x1 - w->phys_cursor.x;
24089
24090 if (row > w->current_matrix->rows
24091 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24092 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24093 OVERLAPS_ERASED_CURSOR);
24094
24095 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24096 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24097 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24098 OVERLAPS_ERASED_CURSOR);
24099 }
24100 }
24101 }
24102
24103
24104 /* EXPORT:
24105 Erase the image of a cursor of window W from the screen. */
24106
24107 void
24108 erase_phys_cursor (struct window *w)
24109 {
24110 struct frame *f = XFRAME (w->frame);
24111 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24112 int hpos = w->phys_cursor.hpos;
24113 int vpos = w->phys_cursor.vpos;
24114 int mouse_face_here_p = 0;
24115 struct glyph_matrix *active_glyphs = w->current_matrix;
24116 struct glyph_row *cursor_row;
24117 struct glyph *cursor_glyph;
24118 enum draw_glyphs_face hl;
24119
24120 /* No cursor displayed or row invalidated => nothing to do on the
24121 screen. */
24122 if (w->phys_cursor_type == NO_CURSOR)
24123 goto mark_cursor_off;
24124
24125 /* VPOS >= active_glyphs->nrows means that window has been resized.
24126 Don't bother to erase the cursor. */
24127 if (vpos >= active_glyphs->nrows)
24128 goto mark_cursor_off;
24129
24130 /* If row containing cursor is marked invalid, there is nothing we
24131 can do. */
24132 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24133 if (!cursor_row->enabled_p)
24134 goto mark_cursor_off;
24135
24136 /* If line spacing is > 0, old cursor may only be partially visible in
24137 window after split-window. So adjust visible height. */
24138 cursor_row->visible_height = min (cursor_row->visible_height,
24139 window_text_bottom_y (w) - cursor_row->y);
24140
24141 /* If row is completely invisible, don't attempt to delete a cursor which
24142 isn't there. This can happen if cursor is at top of a window, and
24143 we switch to a buffer with a header line in that window. */
24144 if (cursor_row->visible_height <= 0)
24145 goto mark_cursor_off;
24146
24147 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24148 if (cursor_row->cursor_in_fringe_p)
24149 {
24150 cursor_row->cursor_in_fringe_p = 0;
24151 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24152 goto mark_cursor_off;
24153 }
24154
24155 /* This can happen when the new row is shorter than the old one.
24156 In this case, either draw_glyphs or clear_end_of_line
24157 should have cleared the cursor. Note that we wouldn't be
24158 able to erase the cursor in this case because we don't have a
24159 cursor glyph at hand. */
24160 if ((cursor_row->reversed_p
24161 ? (w->phys_cursor.hpos < 0)
24162 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24163 goto mark_cursor_off;
24164
24165 /* If the cursor is in the mouse face area, redisplay that when
24166 we clear the cursor. */
24167 if (! NILP (hlinfo->mouse_face_window)
24168 && coords_in_mouse_face_p (w, hpos, vpos)
24169 /* Don't redraw the cursor's spot in mouse face if it is at the
24170 end of a line (on a newline). The cursor appears there, but
24171 mouse highlighting does not. */
24172 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24173 mouse_face_here_p = 1;
24174
24175 /* Maybe clear the display under the cursor. */
24176 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24177 {
24178 int x, y, left_x;
24179 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24180 int width;
24181
24182 cursor_glyph = get_phys_cursor_glyph (w);
24183 if (cursor_glyph == NULL)
24184 goto mark_cursor_off;
24185
24186 width = cursor_glyph->pixel_width;
24187 left_x = window_box_left_offset (w, TEXT_AREA);
24188 x = w->phys_cursor.x;
24189 if (x < left_x)
24190 width -= left_x - x;
24191 width = min (width, window_box_width (w, TEXT_AREA) - x);
24192 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24193 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24194
24195 if (width > 0)
24196 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24197 }
24198
24199 /* Erase the cursor by redrawing the character underneath it. */
24200 if (mouse_face_here_p)
24201 hl = DRAW_MOUSE_FACE;
24202 else
24203 hl = DRAW_NORMAL_TEXT;
24204 draw_phys_cursor_glyph (w, cursor_row, hl);
24205
24206 mark_cursor_off:
24207 w->phys_cursor_on_p = 0;
24208 w->phys_cursor_type = NO_CURSOR;
24209 }
24210
24211
24212 /* EXPORT:
24213 Display or clear cursor of window W. If ON is zero, clear the
24214 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24215 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24216
24217 void
24218 display_and_set_cursor (struct window *w, int on,
24219 int hpos, int vpos, int x, int y)
24220 {
24221 struct frame *f = XFRAME (w->frame);
24222 int new_cursor_type;
24223 int new_cursor_width;
24224 int active_cursor;
24225 struct glyph_row *glyph_row;
24226 struct glyph *glyph;
24227
24228 /* This is pointless on invisible frames, and dangerous on garbaged
24229 windows and frames; in the latter case, the frame or window may
24230 be in the midst of changing its size, and x and y may be off the
24231 window. */
24232 if (! FRAME_VISIBLE_P (f)
24233 || FRAME_GARBAGED_P (f)
24234 || vpos >= w->current_matrix->nrows
24235 || hpos >= w->current_matrix->matrix_w)
24236 return;
24237
24238 /* If cursor is off and we want it off, return quickly. */
24239 if (!on && !w->phys_cursor_on_p)
24240 return;
24241
24242 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24243 /* If cursor row is not enabled, we don't really know where to
24244 display the cursor. */
24245 if (!glyph_row->enabled_p)
24246 {
24247 w->phys_cursor_on_p = 0;
24248 return;
24249 }
24250
24251 glyph = NULL;
24252 if (!glyph_row->exact_window_width_line_p
24253 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24254 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24255
24256 xassert (interrupt_input_blocked);
24257
24258 /* Set new_cursor_type to the cursor we want to be displayed. */
24259 new_cursor_type = get_window_cursor_type (w, glyph,
24260 &new_cursor_width, &active_cursor);
24261
24262 /* If cursor is currently being shown and we don't want it to be or
24263 it is in the wrong place, or the cursor type is not what we want,
24264 erase it. */
24265 if (w->phys_cursor_on_p
24266 && (!on
24267 || w->phys_cursor.x != x
24268 || w->phys_cursor.y != y
24269 || new_cursor_type != w->phys_cursor_type
24270 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24271 && new_cursor_width != w->phys_cursor_width)))
24272 erase_phys_cursor (w);
24273
24274 /* Don't check phys_cursor_on_p here because that flag is only set
24275 to zero in some cases where we know that the cursor has been
24276 completely erased, to avoid the extra work of erasing the cursor
24277 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24278 still not be visible, or it has only been partly erased. */
24279 if (on)
24280 {
24281 w->phys_cursor_ascent = glyph_row->ascent;
24282 w->phys_cursor_height = glyph_row->height;
24283
24284 /* Set phys_cursor_.* before x_draw_.* is called because some
24285 of them may need the information. */
24286 w->phys_cursor.x = x;
24287 w->phys_cursor.y = glyph_row->y;
24288 w->phys_cursor.hpos = hpos;
24289 w->phys_cursor.vpos = vpos;
24290 }
24291
24292 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24293 new_cursor_type, new_cursor_width,
24294 on, active_cursor);
24295 }
24296
24297
24298 /* Switch the display of W's cursor on or off, according to the value
24299 of ON. */
24300
24301 static void
24302 update_window_cursor (struct window *w, int on)
24303 {
24304 /* Don't update cursor in windows whose frame is in the process
24305 of being deleted. */
24306 if (w->current_matrix)
24307 {
24308 BLOCK_INPUT;
24309 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24310 w->phys_cursor.x, w->phys_cursor.y);
24311 UNBLOCK_INPUT;
24312 }
24313 }
24314
24315
24316 /* Call update_window_cursor with parameter ON_P on all leaf windows
24317 in the window tree rooted at W. */
24318
24319 static void
24320 update_cursor_in_window_tree (struct window *w, int on_p)
24321 {
24322 while (w)
24323 {
24324 if (!NILP (w->hchild))
24325 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24326 else if (!NILP (w->vchild))
24327 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24328 else
24329 update_window_cursor (w, on_p);
24330
24331 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24332 }
24333 }
24334
24335
24336 /* EXPORT:
24337 Display the cursor on window W, or clear it, according to ON_P.
24338 Don't change the cursor's position. */
24339
24340 void
24341 x_update_cursor (struct frame *f, int on_p)
24342 {
24343 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24344 }
24345
24346
24347 /* EXPORT:
24348 Clear the cursor of window W to background color, and mark the
24349 cursor as not shown. This is used when the text where the cursor
24350 is about to be rewritten. */
24351
24352 void
24353 x_clear_cursor (struct window *w)
24354 {
24355 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24356 update_window_cursor (w, 0);
24357 }
24358
24359 #endif /* HAVE_WINDOW_SYSTEM */
24360
24361 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24362 and MSDOS. */
24363 static void
24364 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24365 int start_hpos, int end_hpos,
24366 enum draw_glyphs_face draw)
24367 {
24368 #ifdef HAVE_WINDOW_SYSTEM
24369 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24370 {
24371 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24372 return;
24373 }
24374 #endif
24375 #if defined (HAVE_GPM) || defined (MSDOS)
24376 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24377 #endif
24378 }
24379
24380 /* Display the active region described by mouse_face_* according to DRAW. */
24381
24382 static void
24383 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24384 {
24385 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24386 struct frame *f = XFRAME (WINDOW_FRAME (w));
24387
24388 if (/* If window is in the process of being destroyed, don't bother
24389 to do anything. */
24390 w->current_matrix != NULL
24391 /* Don't update mouse highlight if hidden */
24392 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24393 /* Recognize when we are called to operate on rows that don't exist
24394 anymore. This can happen when a window is split. */
24395 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24396 {
24397 int phys_cursor_on_p = w->phys_cursor_on_p;
24398 struct glyph_row *row, *first, *last;
24399
24400 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24401 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24402
24403 for (row = first; row <= last && row->enabled_p; ++row)
24404 {
24405 int start_hpos, end_hpos, start_x;
24406
24407 /* For all but the first row, the highlight starts at column 0. */
24408 if (row == first)
24409 {
24410 /* R2L rows have BEG and END in reversed order, but the
24411 screen drawing geometry is always left to right. So
24412 we need to mirror the beginning and end of the
24413 highlighted area in R2L rows. */
24414 if (!row->reversed_p)
24415 {
24416 start_hpos = hlinfo->mouse_face_beg_col;
24417 start_x = hlinfo->mouse_face_beg_x;
24418 }
24419 else if (row == last)
24420 {
24421 start_hpos = hlinfo->mouse_face_end_col;
24422 start_x = hlinfo->mouse_face_end_x;
24423 }
24424 else
24425 {
24426 start_hpos = 0;
24427 start_x = 0;
24428 }
24429 }
24430 else if (row->reversed_p && row == last)
24431 {
24432 start_hpos = hlinfo->mouse_face_end_col;
24433 start_x = hlinfo->mouse_face_end_x;
24434 }
24435 else
24436 {
24437 start_hpos = 0;
24438 start_x = 0;
24439 }
24440
24441 if (row == last)
24442 {
24443 if (!row->reversed_p)
24444 end_hpos = hlinfo->mouse_face_end_col;
24445 else if (row == first)
24446 end_hpos = hlinfo->mouse_face_beg_col;
24447 else
24448 {
24449 end_hpos = row->used[TEXT_AREA];
24450 if (draw == DRAW_NORMAL_TEXT)
24451 row->fill_line_p = 1; /* Clear to end of line */
24452 }
24453 }
24454 else if (row->reversed_p && row == first)
24455 end_hpos = hlinfo->mouse_face_beg_col;
24456 else
24457 {
24458 end_hpos = row->used[TEXT_AREA];
24459 if (draw == DRAW_NORMAL_TEXT)
24460 row->fill_line_p = 1; /* Clear to end of line */
24461 }
24462
24463 if (end_hpos > start_hpos)
24464 {
24465 draw_row_with_mouse_face (w, start_x, row,
24466 start_hpos, end_hpos, draw);
24467
24468 row->mouse_face_p
24469 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24470 }
24471 }
24472
24473 #ifdef HAVE_WINDOW_SYSTEM
24474 /* When we've written over the cursor, arrange for it to
24475 be displayed again. */
24476 if (FRAME_WINDOW_P (f)
24477 && phys_cursor_on_p && !w->phys_cursor_on_p)
24478 {
24479 BLOCK_INPUT;
24480 display_and_set_cursor (w, 1,
24481 w->phys_cursor.hpos, w->phys_cursor.vpos,
24482 w->phys_cursor.x, w->phys_cursor.y);
24483 UNBLOCK_INPUT;
24484 }
24485 #endif /* HAVE_WINDOW_SYSTEM */
24486 }
24487
24488 #ifdef HAVE_WINDOW_SYSTEM
24489 /* Change the mouse cursor. */
24490 if (FRAME_WINDOW_P (f))
24491 {
24492 if (draw == DRAW_NORMAL_TEXT
24493 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24494 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24495 else if (draw == DRAW_MOUSE_FACE)
24496 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24497 else
24498 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24499 }
24500 #endif /* HAVE_WINDOW_SYSTEM */
24501 }
24502
24503 /* EXPORT:
24504 Clear out the mouse-highlighted active region.
24505 Redraw it un-highlighted first. Value is non-zero if mouse
24506 face was actually drawn unhighlighted. */
24507
24508 int
24509 clear_mouse_face (Mouse_HLInfo *hlinfo)
24510 {
24511 int cleared = 0;
24512
24513 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24514 {
24515 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24516 cleared = 1;
24517 }
24518
24519 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24520 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24521 hlinfo->mouse_face_window = Qnil;
24522 hlinfo->mouse_face_overlay = Qnil;
24523 return cleared;
24524 }
24525
24526 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24527 within the mouse face on that window. */
24528 static int
24529 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24530 {
24531 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24532
24533 /* Quickly resolve the easy cases. */
24534 if (!(WINDOWP (hlinfo->mouse_face_window)
24535 && XWINDOW (hlinfo->mouse_face_window) == w))
24536 return 0;
24537 if (vpos < hlinfo->mouse_face_beg_row
24538 || vpos > hlinfo->mouse_face_end_row)
24539 return 0;
24540 if (vpos > hlinfo->mouse_face_beg_row
24541 && vpos < hlinfo->mouse_face_end_row)
24542 return 1;
24543
24544 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24545 {
24546 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24547 {
24548 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24549 return 1;
24550 }
24551 else if ((vpos == hlinfo->mouse_face_beg_row
24552 && hpos >= hlinfo->mouse_face_beg_col)
24553 || (vpos == hlinfo->mouse_face_end_row
24554 && hpos < hlinfo->mouse_face_end_col))
24555 return 1;
24556 }
24557 else
24558 {
24559 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24560 {
24561 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24562 return 1;
24563 }
24564 else if ((vpos == hlinfo->mouse_face_beg_row
24565 && hpos <= hlinfo->mouse_face_beg_col)
24566 || (vpos == hlinfo->mouse_face_end_row
24567 && hpos > hlinfo->mouse_face_end_col))
24568 return 1;
24569 }
24570 return 0;
24571 }
24572
24573
24574 /* EXPORT:
24575 Non-zero if physical cursor of window W is within mouse face. */
24576
24577 int
24578 cursor_in_mouse_face_p (struct window *w)
24579 {
24580 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24581 }
24582
24583
24584 \f
24585 /* Find the glyph rows START_ROW and END_ROW of window W that display
24586 characters between buffer positions START_CHARPOS and END_CHARPOS
24587 (excluding END_CHARPOS). This is similar to row_containing_pos,
24588 but is more accurate when bidi reordering makes buffer positions
24589 change non-linearly with glyph rows. */
24590 static void
24591 rows_from_pos_range (struct window *w,
24592 EMACS_INT start_charpos, EMACS_INT end_charpos,
24593 struct glyph_row **start, struct glyph_row **end)
24594 {
24595 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24596 int last_y = window_text_bottom_y (w);
24597 struct glyph_row *row;
24598
24599 *start = NULL;
24600 *end = NULL;
24601
24602 while (!first->enabled_p
24603 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24604 first++;
24605
24606 /* Find the START row. */
24607 for (row = first;
24608 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24609 row++)
24610 {
24611 /* A row can potentially be the START row if the range of the
24612 characters it displays intersects the range
24613 [START_CHARPOS..END_CHARPOS). */
24614 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24615 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24616 /* See the commentary in row_containing_pos, for the
24617 explanation of the complicated way to check whether
24618 some position is beyond the end of the characters
24619 displayed by a row. */
24620 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24621 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24622 && !row->ends_at_zv_p
24623 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24624 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24625 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24626 && !row->ends_at_zv_p
24627 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24628 {
24629 /* Found a candidate row. Now make sure at least one of the
24630 glyphs it displays has a charpos from the range
24631 [START_CHARPOS..END_CHARPOS).
24632
24633 This is not obvious because bidi reordering could make
24634 buffer positions of a row be 1,2,3,102,101,100, and if we
24635 want to highlight characters in [50..60), we don't want
24636 this row, even though [50..60) does intersect [1..103),
24637 the range of character positions given by the row's start
24638 and end positions. */
24639 struct glyph *g = row->glyphs[TEXT_AREA];
24640 struct glyph *e = g + row->used[TEXT_AREA];
24641
24642 while (g < e)
24643 {
24644 if (BUFFERP (g->object)
24645 && start_charpos <= g->charpos && g->charpos < end_charpos)
24646 *start = row;
24647 g++;
24648 }
24649 if (*start)
24650 break;
24651 }
24652 }
24653
24654 /* Find the END row. */
24655 if (!*start
24656 /* If the last row is partially visible, start looking for END
24657 from that row, instead of starting from FIRST. */
24658 && !(row->enabled_p
24659 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24660 row = first;
24661 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24662 {
24663 struct glyph_row *next = row + 1;
24664
24665 if (!next->enabled_p
24666 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24667 /* The first row >= START whose range of displayed characters
24668 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24669 is the row END + 1. */
24670 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24671 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24672 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24673 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24674 && !next->ends_at_zv_p
24675 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24676 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24677 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24678 && !next->ends_at_zv_p
24679 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24680 {
24681 *end = row;
24682 break;
24683 }
24684 else
24685 {
24686 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24687 but none of the characters it displays are in the range, it is
24688 also END + 1. */
24689 struct glyph *g = next->glyphs[TEXT_AREA];
24690 struct glyph *e = g + next->used[TEXT_AREA];
24691
24692 while (g < e)
24693 {
24694 if (BUFFERP (g->object)
24695 && start_charpos <= g->charpos && g->charpos < end_charpos)
24696 break;
24697 g++;
24698 }
24699 if (g == e)
24700 {
24701 *end = row;
24702 break;
24703 }
24704 }
24705 }
24706 }
24707
24708 /* This function sets the mouse_face_* elements of HLINFO, assuming
24709 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24710 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24711 for the overlay or run of text properties specifying the mouse
24712 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24713 before-string and after-string that must also be highlighted.
24714 COVER_STRING, if non-nil, is a display string that may cover some
24715 or all of the highlighted text. */
24716
24717 static void
24718 mouse_face_from_buffer_pos (Lisp_Object window,
24719 Mouse_HLInfo *hlinfo,
24720 EMACS_INT mouse_charpos,
24721 EMACS_INT start_charpos,
24722 EMACS_INT end_charpos,
24723 Lisp_Object before_string,
24724 Lisp_Object after_string,
24725 Lisp_Object cover_string)
24726 {
24727 struct window *w = XWINDOW (window);
24728 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24729 struct glyph_row *r1, *r2;
24730 struct glyph *glyph, *end;
24731 EMACS_INT ignore, pos;
24732 int x;
24733
24734 xassert (NILP (cover_string) || STRINGP (cover_string));
24735 xassert (NILP (before_string) || STRINGP (before_string));
24736 xassert (NILP (after_string) || STRINGP (after_string));
24737
24738 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24739 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24740 if (r1 == NULL)
24741 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24742 /* If the before-string or display-string contains newlines,
24743 rows_from_pos_range skips to its last row. Move back. */
24744 if (!NILP (before_string) || !NILP (cover_string))
24745 {
24746 struct glyph_row *prev;
24747 while ((prev = r1 - 1, prev >= first)
24748 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24749 && prev->used[TEXT_AREA] > 0)
24750 {
24751 struct glyph *beg = prev->glyphs[TEXT_AREA];
24752 glyph = beg + prev->used[TEXT_AREA];
24753 while (--glyph >= beg && INTEGERP (glyph->object));
24754 if (glyph < beg
24755 || !(EQ (glyph->object, before_string)
24756 || EQ (glyph->object, cover_string)))
24757 break;
24758 r1 = prev;
24759 }
24760 }
24761 if (r2 == NULL)
24762 {
24763 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24764 hlinfo->mouse_face_past_end = 1;
24765 }
24766 else if (!NILP (after_string))
24767 {
24768 /* If the after-string has newlines, advance to its last row. */
24769 struct glyph_row *next;
24770 struct glyph_row *last
24771 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24772
24773 for (next = r2 + 1;
24774 next <= last
24775 && next->used[TEXT_AREA] > 0
24776 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24777 ++next)
24778 r2 = next;
24779 }
24780 /* The rest of the display engine assumes that mouse_face_beg_row is
24781 either above below mouse_face_end_row or identical to it. But
24782 with bidi-reordered continued lines, the row for START_CHARPOS
24783 could be below the row for END_CHARPOS. If so, swap the rows and
24784 store them in correct order. */
24785 if (r1->y > r2->y)
24786 {
24787 struct glyph_row *tem = r2;
24788
24789 r2 = r1;
24790 r1 = tem;
24791 }
24792
24793 hlinfo->mouse_face_beg_y = r1->y;
24794 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24795 hlinfo->mouse_face_end_y = r2->y;
24796 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24797
24798 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24799 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
24800 could be anywhere in the row and in any order. The strategy
24801 below is to find the leftmost and the rightmost glyph that
24802 belongs to either of these 3 strings, or whose position is
24803 between START_CHARPOS and END_CHARPOS, and highlight all the
24804 glyphs between those two. This may cover more than just the text
24805 between START_CHARPOS and END_CHARPOS if the range of characters
24806 strides the bidi level boundary, e.g. if the beginning is in R2L
24807 text while the end is in L2R text or vice versa. */
24808 if (!r1->reversed_p)
24809 {
24810 /* This row is in a left to right paragraph. Scan it left to
24811 right. */
24812 glyph = r1->glyphs[TEXT_AREA];
24813 end = glyph + r1->used[TEXT_AREA];
24814 x = r1->x;
24815
24816 /* Skip truncation glyphs at the start of the glyph row. */
24817 if (r1->displays_text_p)
24818 for (; glyph < end
24819 && INTEGERP (glyph->object)
24820 && glyph->charpos < 0;
24821 ++glyph)
24822 x += glyph->pixel_width;
24823
24824 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24825 or COVER_STRING, and the first glyph from buffer whose
24826 position is between START_CHARPOS and END_CHARPOS. */
24827 for (; glyph < end
24828 && !INTEGERP (glyph->object)
24829 && !EQ (glyph->object, cover_string)
24830 && !(BUFFERP (glyph->object)
24831 && (glyph->charpos >= start_charpos
24832 && glyph->charpos < end_charpos));
24833 ++glyph)
24834 {
24835 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24836 are present at buffer positions between START_CHARPOS and
24837 END_CHARPOS, or if they come from an overlay. */
24838 if (EQ (glyph->object, before_string))
24839 {
24840 pos = string_buffer_position (before_string,
24841 start_charpos);
24842 /* If pos == 0, it means before_string came from an
24843 overlay, not from a buffer position. */
24844 if (!pos || (pos >= start_charpos && pos < end_charpos))
24845 break;
24846 }
24847 else if (EQ (glyph->object, after_string))
24848 {
24849 pos = string_buffer_position (after_string, end_charpos);
24850 if (!pos || (pos >= start_charpos && pos < end_charpos))
24851 break;
24852 }
24853 x += glyph->pixel_width;
24854 }
24855 hlinfo->mouse_face_beg_x = x;
24856 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24857 }
24858 else
24859 {
24860 /* This row is in a right to left paragraph. Scan it right to
24861 left. */
24862 struct glyph *g;
24863
24864 end = r1->glyphs[TEXT_AREA] - 1;
24865 glyph = end + r1->used[TEXT_AREA];
24866
24867 /* Skip truncation glyphs at the start of the glyph row. */
24868 if (r1->displays_text_p)
24869 for (; glyph > end
24870 && INTEGERP (glyph->object)
24871 && glyph->charpos < 0;
24872 --glyph)
24873 ;
24874
24875 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24876 or COVER_STRING, and the first glyph from buffer whose
24877 position is between START_CHARPOS and END_CHARPOS. */
24878 for (; glyph > end
24879 && !INTEGERP (glyph->object)
24880 && !EQ (glyph->object, cover_string)
24881 && !(BUFFERP (glyph->object)
24882 && (glyph->charpos >= start_charpos
24883 && glyph->charpos < end_charpos));
24884 --glyph)
24885 {
24886 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24887 are present at buffer positions between START_CHARPOS and
24888 END_CHARPOS, or if they come from an overlay. */
24889 if (EQ (glyph->object, before_string))
24890 {
24891 pos = string_buffer_position (before_string, start_charpos);
24892 /* If pos == 0, it means before_string came from an
24893 overlay, not from a buffer position. */
24894 if (!pos || (pos >= start_charpos && pos < end_charpos))
24895 break;
24896 }
24897 else if (EQ (glyph->object, after_string))
24898 {
24899 pos = string_buffer_position (after_string, end_charpos);
24900 if (!pos || (pos >= start_charpos && pos < end_charpos))
24901 break;
24902 }
24903 }
24904
24905 glyph++; /* first glyph to the right of the highlighted area */
24906 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24907 x += g->pixel_width;
24908 hlinfo->mouse_face_beg_x = x;
24909 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24910 }
24911
24912 /* If the highlight ends in a different row, compute GLYPH and END
24913 for the end row. Otherwise, reuse the values computed above for
24914 the row where the highlight begins. */
24915 if (r2 != r1)
24916 {
24917 if (!r2->reversed_p)
24918 {
24919 glyph = r2->glyphs[TEXT_AREA];
24920 end = glyph + r2->used[TEXT_AREA];
24921 x = r2->x;
24922 }
24923 else
24924 {
24925 end = r2->glyphs[TEXT_AREA] - 1;
24926 glyph = end + r2->used[TEXT_AREA];
24927 }
24928 }
24929
24930 if (!r2->reversed_p)
24931 {
24932 /* Skip truncation and continuation glyphs near the end of the
24933 row, and also blanks and stretch glyphs inserted by
24934 extend_face_to_end_of_line. */
24935 while (end > glyph
24936 && INTEGERP ((end - 1)->object)
24937 && (end - 1)->charpos <= 0)
24938 --end;
24939 /* Scan the rest of the glyph row from the end, looking for the
24940 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24941 COVER_STRING, or whose position is between START_CHARPOS
24942 and END_CHARPOS */
24943 for (--end;
24944 end > glyph
24945 && !INTEGERP (end->object)
24946 && !EQ (end->object, cover_string)
24947 && !(BUFFERP (end->object)
24948 && (end->charpos >= start_charpos
24949 && end->charpos < end_charpos));
24950 --end)
24951 {
24952 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24953 are present at buffer positions between START_CHARPOS and
24954 END_CHARPOS, or if they come from an overlay. */
24955 if (EQ (end->object, before_string))
24956 {
24957 pos = string_buffer_position (before_string, start_charpos);
24958 if (!pos || (pos >= start_charpos && pos < end_charpos))
24959 break;
24960 }
24961 else if (EQ (end->object, after_string))
24962 {
24963 pos = string_buffer_position (after_string, end_charpos);
24964 if (!pos || (pos >= start_charpos && pos < end_charpos))
24965 break;
24966 }
24967 }
24968 /* Find the X coordinate of the last glyph to be highlighted. */
24969 for (; glyph <= end; ++glyph)
24970 x += glyph->pixel_width;
24971
24972 hlinfo->mouse_face_end_x = x;
24973 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24974 }
24975 else
24976 {
24977 /* Skip truncation and continuation glyphs near the end of the
24978 row, and also blanks and stretch glyphs inserted by
24979 extend_face_to_end_of_line. */
24980 x = r2->x;
24981 end++;
24982 while (end < glyph
24983 && INTEGERP (end->object)
24984 && end->charpos <= 0)
24985 {
24986 x += end->pixel_width;
24987 ++end;
24988 }
24989 /* Scan the rest of the glyph row from the end, looking for the
24990 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24991 COVER_STRING, or whose position is between START_CHARPOS
24992 and END_CHARPOS */
24993 for ( ;
24994 end < glyph
24995 && !INTEGERP (end->object)
24996 && !EQ (end->object, cover_string)
24997 && !(BUFFERP (end->object)
24998 && (end->charpos >= start_charpos
24999 && end->charpos < end_charpos));
25000 ++end)
25001 {
25002 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25003 are present at buffer positions between START_CHARPOS and
25004 END_CHARPOS, or if they come from an overlay. */
25005 if (EQ (end->object, before_string))
25006 {
25007 pos = string_buffer_position (before_string, start_charpos);
25008 if (!pos || (pos >= start_charpos && pos < end_charpos))
25009 break;
25010 }
25011 else if (EQ (end->object, after_string))
25012 {
25013 pos = string_buffer_position (after_string, end_charpos);
25014 if (!pos || (pos >= start_charpos && pos < end_charpos))
25015 break;
25016 }
25017 x += end->pixel_width;
25018 }
25019 hlinfo->mouse_face_end_x = x;
25020 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25021 }
25022
25023 hlinfo->mouse_face_window = window;
25024 hlinfo->mouse_face_face_id
25025 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25026 mouse_charpos + 1,
25027 !hlinfo->mouse_face_hidden, -1);
25028 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25029 }
25030
25031 /* The following function is not used anymore (replaced with
25032 mouse_face_from_string_pos), but I leave it here for the time
25033 being, in case someone would. */
25034
25035 #if 0 /* not used */
25036
25037 /* Find the position of the glyph for position POS in OBJECT in
25038 window W's current matrix, and return in *X, *Y the pixel
25039 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25040
25041 RIGHT_P non-zero means return the position of the right edge of the
25042 glyph, RIGHT_P zero means return the left edge position.
25043
25044 If no glyph for POS exists in the matrix, return the position of
25045 the glyph with the next smaller position that is in the matrix, if
25046 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25047 exists in the matrix, return the position of the glyph with the
25048 next larger position in OBJECT.
25049
25050 Value is non-zero if a glyph was found. */
25051
25052 static int
25053 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25054 int *hpos, int *vpos, int *x, int *y, int right_p)
25055 {
25056 int yb = window_text_bottom_y (w);
25057 struct glyph_row *r;
25058 struct glyph *best_glyph = NULL;
25059 struct glyph_row *best_row = NULL;
25060 int best_x = 0;
25061
25062 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25063 r->enabled_p && r->y < yb;
25064 ++r)
25065 {
25066 struct glyph *g = r->glyphs[TEXT_AREA];
25067 struct glyph *e = g + r->used[TEXT_AREA];
25068 int gx;
25069
25070 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25071 if (EQ (g->object, object))
25072 {
25073 if (g->charpos == pos)
25074 {
25075 best_glyph = g;
25076 best_x = gx;
25077 best_row = r;
25078 goto found;
25079 }
25080 else if (best_glyph == NULL
25081 || ((eabs (g->charpos - pos)
25082 < eabs (best_glyph->charpos - pos))
25083 && (right_p
25084 ? g->charpos < pos
25085 : g->charpos > pos)))
25086 {
25087 best_glyph = g;
25088 best_x = gx;
25089 best_row = r;
25090 }
25091 }
25092 }
25093
25094 found:
25095
25096 if (best_glyph)
25097 {
25098 *x = best_x;
25099 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25100
25101 if (right_p)
25102 {
25103 *x += best_glyph->pixel_width;
25104 ++*hpos;
25105 }
25106
25107 *y = best_row->y;
25108 *vpos = best_row - w->current_matrix->rows;
25109 }
25110
25111 return best_glyph != NULL;
25112 }
25113 #endif /* not used */
25114
25115 /* Find the positions of the first and the last glyphs in window W's
25116 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25117 (assumed to be a string), and return in HLINFO's mouse_face_*
25118 members the pixel and column/row coordinates of those glyphs. */
25119
25120 static void
25121 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25122 Lisp_Object object,
25123 EMACS_INT startpos, EMACS_INT endpos)
25124 {
25125 int yb = window_text_bottom_y (w);
25126 struct glyph_row *r;
25127 struct glyph *g, *e;
25128 int gx;
25129 int found = 0;
25130
25131 /* Find the glyph row with at least one position in the range
25132 [STARTPOS..ENDPOS], and the first glyph in that row whose
25133 position belongs to that range. */
25134 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25135 r->enabled_p && r->y < yb;
25136 ++r)
25137 {
25138 if (!r->reversed_p)
25139 {
25140 g = r->glyphs[TEXT_AREA];
25141 e = g + r->used[TEXT_AREA];
25142 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25143 if (EQ (g->object, object)
25144 && startpos <= g->charpos && g->charpos <= endpos)
25145 {
25146 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25147 hlinfo->mouse_face_beg_y = r->y;
25148 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25149 hlinfo->mouse_face_beg_x = gx;
25150 found = 1;
25151 break;
25152 }
25153 }
25154 else
25155 {
25156 struct glyph *g1;
25157
25158 e = r->glyphs[TEXT_AREA];
25159 g = e + r->used[TEXT_AREA];
25160 for ( ; g > e; --g)
25161 if (EQ ((g-1)->object, object)
25162 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25163 {
25164 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25165 hlinfo->mouse_face_beg_y = r->y;
25166 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25167 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25168 gx += g1->pixel_width;
25169 hlinfo->mouse_face_beg_x = gx;
25170 found = 1;
25171 break;
25172 }
25173 }
25174 if (found)
25175 break;
25176 }
25177
25178 if (!found)
25179 return;
25180
25181 /* Starting with the next row, look for the first row which does NOT
25182 include any glyphs whose positions are in the range. */
25183 for (++r; r->enabled_p && r->y < yb; ++r)
25184 {
25185 g = r->glyphs[TEXT_AREA];
25186 e = g + r->used[TEXT_AREA];
25187 found = 0;
25188 for ( ; g < e; ++g)
25189 if (EQ (g->object, object)
25190 && startpos <= g->charpos && g->charpos <= endpos)
25191 {
25192 found = 1;
25193 break;
25194 }
25195 if (!found)
25196 break;
25197 }
25198
25199 /* The highlighted region ends on the previous row. */
25200 r--;
25201
25202 /* Set the end row and its vertical pixel coordinate. */
25203 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25204 hlinfo->mouse_face_end_y = r->y;
25205
25206 /* Compute and set the end column and the end column's horizontal
25207 pixel coordinate. */
25208 if (!r->reversed_p)
25209 {
25210 g = r->glyphs[TEXT_AREA];
25211 e = g + r->used[TEXT_AREA];
25212 for ( ; e > g; --e)
25213 if (EQ ((e-1)->object, object)
25214 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25215 break;
25216 hlinfo->mouse_face_end_col = e - g;
25217
25218 for (gx = r->x; g < e; ++g)
25219 gx += g->pixel_width;
25220 hlinfo->mouse_face_end_x = gx;
25221 }
25222 else
25223 {
25224 e = r->glyphs[TEXT_AREA];
25225 g = e + r->used[TEXT_AREA];
25226 for (gx = r->x ; e < g; ++e)
25227 {
25228 if (EQ (e->object, object)
25229 && startpos <= e->charpos && e->charpos <= endpos)
25230 break;
25231 gx += e->pixel_width;
25232 }
25233 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25234 hlinfo->mouse_face_end_x = gx;
25235 }
25236 }
25237
25238 #ifdef HAVE_WINDOW_SYSTEM
25239
25240 /* See if position X, Y is within a hot-spot of an image. */
25241
25242 static int
25243 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25244 {
25245 if (!CONSP (hot_spot))
25246 return 0;
25247
25248 if (EQ (XCAR (hot_spot), Qrect))
25249 {
25250 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25251 Lisp_Object rect = XCDR (hot_spot);
25252 Lisp_Object tem;
25253 if (!CONSP (rect))
25254 return 0;
25255 if (!CONSP (XCAR (rect)))
25256 return 0;
25257 if (!CONSP (XCDR (rect)))
25258 return 0;
25259 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25260 return 0;
25261 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25262 return 0;
25263 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25264 return 0;
25265 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25266 return 0;
25267 return 1;
25268 }
25269 else if (EQ (XCAR (hot_spot), Qcircle))
25270 {
25271 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25272 Lisp_Object circ = XCDR (hot_spot);
25273 Lisp_Object lr, lx0, ly0;
25274 if (CONSP (circ)
25275 && CONSP (XCAR (circ))
25276 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25277 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25278 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25279 {
25280 double r = XFLOATINT (lr);
25281 double dx = XINT (lx0) - x;
25282 double dy = XINT (ly0) - y;
25283 return (dx * dx + dy * dy <= r * r);
25284 }
25285 }
25286 else if (EQ (XCAR (hot_spot), Qpoly))
25287 {
25288 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25289 if (VECTORP (XCDR (hot_spot)))
25290 {
25291 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25292 Lisp_Object *poly = v->contents;
25293 int n = v->header.size;
25294 int i;
25295 int inside = 0;
25296 Lisp_Object lx, ly;
25297 int x0, y0;
25298
25299 /* Need an even number of coordinates, and at least 3 edges. */
25300 if (n < 6 || n & 1)
25301 return 0;
25302
25303 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25304 If count is odd, we are inside polygon. Pixels on edges
25305 may or may not be included depending on actual geometry of the
25306 polygon. */
25307 if ((lx = poly[n-2], !INTEGERP (lx))
25308 || (ly = poly[n-1], !INTEGERP (lx)))
25309 return 0;
25310 x0 = XINT (lx), y0 = XINT (ly);
25311 for (i = 0; i < n; i += 2)
25312 {
25313 int x1 = x0, y1 = y0;
25314 if ((lx = poly[i], !INTEGERP (lx))
25315 || (ly = poly[i+1], !INTEGERP (ly)))
25316 return 0;
25317 x0 = XINT (lx), y0 = XINT (ly);
25318
25319 /* Does this segment cross the X line? */
25320 if (x0 >= x)
25321 {
25322 if (x1 >= x)
25323 continue;
25324 }
25325 else if (x1 < x)
25326 continue;
25327 if (y > y0 && y > y1)
25328 continue;
25329 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25330 inside = !inside;
25331 }
25332 return inside;
25333 }
25334 }
25335 return 0;
25336 }
25337
25338 Lisp_Object
25339 find_hot_spot (Lisp_Object map, int x, int y)
25340 {
25341 while (CONSP (map))
25342 {
25343 if (CONSP (XCAR (map))
25344 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25345 return XCAR (map);
25346 map = XCDR (map);
25347 }
25348
25349 return Qnil;
25350 }
25351
25352 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25353 3, 3, 0,
25354 doc: /* Lookup in image map MAP coordinates X and Y.
25355 An image map is an alist where each element has the format (AREA ID PLIST).
25356 An AREA is specified as either a rectangle, a circle, or a polygon:
25357 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25358 pixel coordinates of the upper left and bottom right corners.
25359 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25360 and the radius of the circle; r may be a float or integer.
25361 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25362 vector describes one corner in the polygon.
25363 Returns the alist element for the first matching AREA in MAP. */)
25364 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25365 {
25366 if (NILP (map))
25367 return Qnil;
25368
25369 CHECK_NUMBER (x);
25370 CHECK_NUMBER (y);
25371
25372 return find_hot_spot (map, XINT (x), XINT (y));
25373 }
25374
25375
25376 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25377 static void
25378 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25379 {
25380 /* Do not change cursor shape while dragging mouse. */
25381 if (!NILP (do_mouse_tracking))
25382 return;
25383
25384 if (!NILP (pointer))
25385 {
25386 if (EQ (pointer, Qarrow))
25387 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25388 else if (EQ (pointer, Qhand))
25389 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25390 else if (EQ (pointer, Qtext))
25391 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25392 else if (EQ (pointer, intern ("hdrag")))
25393 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25394 #ifdef HAVE_X_WINDOWS
25395 else if (EQ (pointer, intern ("vdrag")))
25396 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25397 #endif
25398 else if (EQ (pointer, intern ("hourglass")))
25399 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25400 else if (EQ (pointer, Qmodeline))
25401 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25402 else
25403 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25404 }
25405
25406 if (cursor != No_Cursor)
25407 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25408 }
25409
25410 #endif /* HAVE_WINDOW_SYSTEM */
25411
25412 /* Take proper action when mouse has moved to the mode or header line
25413 or marginal area AREA of window W, x-position X and y-position Y.
25414 X is relative to the start of the text display area of W, so the
25415 width of bitmap areas and scroll bars must be subtracted to get a
25416 position relative to the start of the mode line. */
25417
25418 static void
25419 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25420 enum window_part area)
25421 {
25422 struct window *w = XWINDOW (window);
25423 struct frame *f = XFRAME (w->frame);
25424 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25425 #ifdef HAVE_WINDOW_SYSTEM
25426 Display_Info *dpyinfo;
25427 #endif
25428 Cursor cursor = No_Cursor;
25429 Lisp_Object pointer = Qnil;
25430 int dx, dy, width, height;
25431 EMACS_INT charpos;
25432 Lisp_Object string, object = Qnil;
25433 Lisp_Object pos, help;
25434
25435 Lisp_Object mouse_face;
25436 int original_x_pixel = x;
25437 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25438 struct glyph_row *row;
25439
25440 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25441 {
25442 int x0;
25443 struct glyph *end;
25444
25445 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25446 returns them in row/column units! */
25447 string = mode_line_string (w, area, &x, &y, &charpos,
25448 &object, &dx, &dy, &width, &height);
25449
25450 row = (area == ON_MODE_LINE
25451 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25452 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25453
25454 /* Find the glyph under the mouse pointer. */
25455 if (row->mode_line_p && row->enabled_p)
25456 {
25457 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25458 end = glyph + row->used[TEXT_AREA];
25459
25460 for (x0 = original_x_pixel;
25461 glyph < end && x0 >= glyph->pixel_width;
25462 ++glyph)
25463 x0 -= glyph->pixel_width;
25464
25465 if (glyph >= end)
25466 glyph = NULL;
25467 }
25468 }
25469 else
25470 {
25471 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25472 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25473 returns them in row/column units! */
25474 string = marginal_area_string (w, area, &x, &y, &charpos,
25475 &object, &dx, &dy, &width, &height);
25476 }
25477
25478 help = Qnil;
25479
25480 #ifdef HAVE_WINDOW_SYSTEM
25481 if (IMAGEP (object))
25482 {
25483 Lisp_Object image_map, hotspot;
25484 if ((image_map = Fplist_get (XCDR (object), QCmap),
25485 !NILP (image_map))
25486 && (hotspot = find_hot_spot (image_map, dx, dy),
25487 CONSP (hotspot))
25488 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25489 {
25490 Lisp_Object plist;
25491
25492 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25493 If so, we could look for mouse-enter, mouse-leave
25494 properties in PLIST (and do something...). */
25495 hotspot = XCDR (hotspot);
25496 if (CONSP (hotspot)
25497 && (plist = XCAR (hotspot), CONSP (plist)))
25498 {
25499 pointer = Fplist_get (plist, Qpointer);
25500 if (NILP (pointer))
25501 pointer = Qhand;
25502 help = Fplist_get (plist, Qhelp_echo);
25503 if (!NILP (help))
25504 {
25505 help_echo_string = help;
25506 /* Is this correct? ++kfs */
25507 XSETWINDOW (help_echo_window, w);
25508 help_echo_object = w->buffer;
25509 help_echo_pos = charpos;
25510 }
25511 }
25512 }
25513 if (NILP (pointer))
25514 pointer = Fplist_get (XCDR (object), QCpointer);
25515 }
25516 #endif /* HAVE_WINDOW_SYSTEM */
25517
25518 if (STRINGP (string))
25519 {
25520 pos = make_number (charpos);
25521 /* If we're on a string with `help-echo' text property, arrange
25522 for the help to be displayed. This is done by setting the
25523 global variable help_echo_string to the help string. */
25524 if (NILP (help))
25525 {
25526 help = Fget_text_property (pos, Qhelp_echo, string);
25527 if (!NILP (help))
25528 {
25529 help_echo_string = help;
25530 XSETWINDOW (help_echo_window, w);
25531 help_echo_object = string;
25532 help_echo_pos = charpos;
25533 }
25534 }
25535
25536 #ifdef HAVE_WINDOW_SYSTEM
25537 if (FRAME_WINDOW_P (f))
25538 {
25539 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25540 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25541 if (NILP (pointer))
25542 pointer = Fget_text_property (pos, Qpointer, string);
25543
25544 /* Change the mouse pointer according to what is under X/Y. */
25545 if (NILP (pointer)
25546 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25547 {
25548 Lisp_Object map;
25549 map = Fget_text_property (pos, Qlocal_map, string);
25550 if (!KEYMAPP (map))
25551 map = Fget_text_property (pos, Qkeymap, string);
25552 if (!KEYMAPP (map))
25553 cursor = dpyinfo->vertical_scroll_bar_cursor;
25554 }
25555 }
25556 #endif
25557
25558 /* Change the mouse face according to what is under X/Y. */
25559 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25560 if (!NILP (mouse_face)
25561 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25562 && glyph)
25563 {
25564 Lisp_Object b, e;
25565
25566 struct glyph * tmp_glyph;
25567
25568 int gpos;
25569 int gseq_length;
25570 int total_pixel_width;
25571 EMACS_INT begpos, endpos, ignore;
25572
25573 int vpos, hpos;
25574
25575 b = Fprevious_single_property_change (make_number (charpos + 1),
25576 Qmouse_face, string, Qnil);
25577 if (NILP (b))
25578 begpos = 0;
25579 else
25580 begpos = XINT (b);
25581
25582 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25583 if (NILP (e))
25584 endpos = SCHARS (string);
25585 else
25586 endpos = XINT (e);
25587
25588 /* Calculate the glyph position GPOS of GLYPH in the
25589 displayed string, relative to the beginning of the
25590 highlighted part of the string.
25591
25592 Note: GPOS is different from CHARPOS. CHARPOS is the
25593 position of GLYPH in the internal string object. A mode
25594 line string format has structures which are converted to
25595 a flattened string by the Emacs Lisp interpreter. The
25596 internal string is an element of those structures. The
25597 displayed string is the flattened string. */
25598 tmp_glyph = row_start_glyph;
25599 while (tmp_glyph < glyph
25600 && (!(EQ (tmp_glyph->object, glyph->object)
25601 && begpos <= tmp_glyph->charpos
25602 && tmp_glyph->charpos < endpos)))
25603 tmp_glyph++;
25604 gpos = glyph - tmp_glyph;
25605
25606 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25607 the highlighted part of the displayed string to which
25608 GLYPH belongs. Note: GSEQ_LENGTH is different from
25609 SCHARS (STRING), because the latter returns the length of
25610 the internal string. */
25611 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25612 tmp_glyph > glyph
25613 && (!(EQ (tmp_glyph->object, glyph->object)
25614 && begpos <= tmp_glyph->charpos
25615 && tmp_glyph->charpos < endpos));
25616 tmp_glyph--)
25617 ;
25618 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25619
25620 /* Calculate the total pixel width of all the glyphs between
25621 the beginning of the highlighted area and GLYPH. */
25622 total_pixel_width = 0;
25623 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25624 total_pixel_width += tmp_glyph->pixel_width;
25625
25626 /* Pre calculation of re-rendering position. Note: X is in
25627 column units here, after the call to mode_line_string or
25628 marginal_area_string. */
25629 hpos = x - gpos;
25630 vpos = (area == ON_MODE_LINE
25631 ? (w->current_matrix)->nrows - 1
25632 : 0);
25633
25634 /* If GLYPH's position is included in the region that is
25635 already drawn in mouse face, we have nothing to do. */
25636 if ( EQ (window, hlinfo->mouse_face_window)
25637 && (!row->reversed_p
25638 ? (hlinfo->mouse_face_beg_col <= hpos
25639 && hpos < hlinfo->mouse_face_end_col)
25640 /* In R2L rows we swap BEG and END, see below. */
25641 : (hlinfo->mouse_face_end_col <= hpos
25642 && hpos < hlinfo->mouse_face_beg_col))
25643 && hlinfo->mouse_face_beg_row == vpos )
25644 return;
25645
25646 if (clear_mouse_face (hlinfo))
25647 cursor = No_Cursor;
25648
25649 if (!row->reversed_p)
25650 {
25651 hlinfo->mouse_face_beg_col = hpos;
25652 hlinfo->mouse_face_beg_x = original_x_pixel
25653 - (total_pixel_width + dx);
25654 hlinfo->mouse_face_end_col = hpos + gseq_length;
25655 hlinfo->mouse_face_end_x = 0;
25656 }
25657 else
25658 {
25659 /* In R2L rows, show_mouse_face expects BEG and END
25660 coordinates to be swapped. */
25661 hlinfo->mouse_face_end_col = hpos;
25662 hlinfo->mouse_face_end_x = original_x_pixel
25663 - (total_pixel_width + dx);
25664 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25665 hlinfo->mouse_face_beg_x = 0;
25666 }
25667
25668 hlinfo->mouse_face_beg_row = vpos;
25669 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25670 hlinfo->mouse_face_beg_y = 0;
25671 hlinfo->mouse_face_end_y = 0;
25672 hlinfo->mouse_face_past_end = 0;
25673 hlinfo->mouse_face_window = window;
25674
25675 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25676 charpos,
25677 0, 0, 0,
25678 &ignore,
25679 glyph->face_id,
25680 1);
25681 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25682
25683 if (NILP (pointer))
25684 pointer = Qhand;
25685 }
25686 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25687 clear_mouse_face (hlinfo);
25688 }
25689 #ifdef HAVE_WINDOW_SYSTEM
25690 if (FRAME_WINDOW_P (f))
25691 define_frame_cursor1 (f, cursor, pointer);
25692 #endif
25693 }
25694
25695
25696 /* EXPORT:
25697 Take proper action when the mouse has moved to position X, Y on
25698 frame F as regards highlighting characters that have mouse-face
25699 properties. Also de-highlighting chars where the mouse was before.
25700 X and Y can be negative or out of range. */
25701
25702 void
25703 note_mouse_highlight (struct frame *f, int x, int y)
25704 {
25705 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25706 enum window_part part;
25707 Lisp_Object window;
25708 struct window *w;
25709 Cursor cursor = No_Cursor;
25710 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25711 struct buffer *b;
25712
25713 /* When a menu is active, don't highlight because this looks odd. */
25714 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25715 if (popup_activated ())
25716 return;
25717 #endif
25718
25719 if (NILP (Vmouse_highlight)
25720 || !f->glyphs_initialized_p
25721 || f->pointer_invisible)
25722 return;
25723
25724 hlinfo->mouse_face_mouse_x = x;
25725 hlinfo->mouse_face_mouse_y = y;
25726 hlinfo->mouse_face_mouse_frame = f;
25727
25728 if (hlinfo->mouse_face_defer)
25729 return;
25730
25731 if (gc_in_progress)
25732 {
25733 hlinfo->mouse_face_deferred_gc = 1;
25734 return;
25735 }
25736
25737 /* Which window is that in? */
25738 window = window_from_coordinates (f, x, y, &part, 1);
25739
25740 /* If we were displaying active text in another window, clear that.
25741 Also clear if we move out of text area in same window. */
25742 if (! EQ (window, hlinfo->mouse_face_window)
25743 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25744 && !NILP (hlinfo->mouse_face_window)))
25745 clear_mouse_face (hlinfo);
25746
25747 /* Not on a window -> return. */
25748 if (!WINDOWP (window))
25749 return;
25750
25751 /* Reset help_echo_string. It will get recomputed below. */
25752 help_echo_string = Qnil;
25753
25754 /* Convert to window-relative pixel coordinates. */
25755 w = XWINDOW (window);
25756 frame_to_window_pixel_xy (w, &x, &y);
25757
25758 #ifdef HAVE_WINDOW_SYSTEM
25759 /* Handle tool-bar window differently since it doesn't display a
25760 buffer. */
25761 if (EQ (window, f->tool_bar_window))
25762 {
25763 note_tool_bar_highlight (f, x, y);
25764 return;
25765 }
25766 #endif
25767
25768 /* Mouse is on the mode, header line or margin? */
25769 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25770 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25771 {
25772 note_mode_line_or_margin_highlight (window, x, y, part);
25773 return;
25774 }
25775
25776 #ifdef HAVE_WINDOW_SYSTEM
25777 if (part == ON_VERTICAL_BORDER)
25778 {
25779 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25780 help_echo_string = build_string ("drag-mouse-1: resize");
25781 }
25782 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25783 || part == ON_SCROLL_BAR)
25784 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25785 else
25786 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25787 #endif
25788
25789 /* Are we in a window whose display is up to date?
25790 And verify the buffer's text has not changed. */
25791 b = XBUFFER (w->buffer);
25792 if (part == ON_TEXT
25793 && EQ (w->window_end_valid, w->buffer)
25794 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25795 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25796 {
25797 int hpos, vpos, i, dx, dy, area;
25798 EMACS_INT pos;
25799 struct glyph *glyph;
25800 Lisp_Object object;
25801 Lisp_Object mouse_face = Qnil, position;
25802 Lisp_Object *overlay_vec = NULL;
25803 int noverlays;
25804 struct buffer *obuf;
25805 EMACS_INT obegv, ozv;
25806 int same_region;
25807
25808 /* Find the glyph under X/Y. */
25809 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25810
25811 #ifdef HAVE_WINDOW_SYSTEM
25812 /* Look for :pointer property on image. */
25813 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25814 {
25815 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25816 if (img != NULL && IMAGEP (img->spec))
25817 {
25818 Lisp_Object image_map, hotspot;
25819 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25820 !NILP (image_map))
25821 && (hotspot = find_hot_spot (image_map,
25822 glyph->slice.img.x + dx,
25823 glyph->slice.img.y + dy),
25824 CONSP (hotspot))
25825 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25826 {
25827 Lisp_Object plist;
25828
25829 /* Could check XCAR (hotspot) to see if we enter/leave
25830 this hot-spot.
25831 If so, we could look for mouse-enter, mouse-leave
25832 properties in PLIST (and do something...). */
25833 hotspot = XCDR (hotspot);
25834 if (CONSP (hotspot)
25835 && (plist = XCAR (hotspot), CONSP (plist)))
25836 {
25837 pointer = Fplist_get (plist, Qpointer);
25838 if (NILP (pointer))
25839 pointer = Qhand;
25840 help_echo_string = Fplist_get (plist, Qhelp_echo);
25841 if (!NILP (help_echo_string))
25842 {
25843 help_echo_window = window;
25844 help_echo_object = glyph->object;
25845 help_echo_pos = glyph->charpos;
25846 }
25847 }
25848 }
25849 if (NILP (pointer))
25850 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25851 }
25852 }
25853 #endif /* HAVE_WINDOW_SYSTEM */
25854
25855 /* Clear mouse face if X/Y not over text. */
25856 if (glyph == NULL
25857 || area != TEXT_AREA
25858 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25859 /* Glyph's OBJECT is an integer for glyphs inserted by the
25860 display engine for its internal purposes, like truncation
25861 and continuation glyphs and blanks beyond the end of
25862 line's text on text terminals. If we are over such a
25863 glyph, we are not over any text. */
25864 || INTEGERP (glyph->object)
25865 /* R2L rows have a stretch glyph at their front, which
25866 stands for no text, whereas L2R rows have no glyphs at
25867 all beyond the end of text. Treat such stretch glyphs
25868 like we do with NULL glyphs in L2R rows. */
25869 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25870 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25871 && glyph->type == STRETCH_GLYPH
25872 && glyph->avoid_cursor_p))
25873 {
25874 if (clear_mouse_face (hlinfo))
25875 cursor = No_Cursor;
25876 #ifdef HAVE_WINDOW_SYSTEM
25877 if (FRAME_WINDOW_P (f) && NILP (pointer))
25878 {
25879 if (area != TEXT_AREA)
25880 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25881 else
25882 pointer = Vvoid_text_area_pointer;
25883 }
25884 #endif
25885 goto set_cursor;
25886 }
25887
25888 pos = glyph->charpos;
25889 object = glyph->object;
25890 if (!STRINGP (object) && !BUFFERP (object))
25891 goto set_cursor;
25892
25893 /* If we get an out-of-range value, return now; avoid an error. */
25894 if (BUFFERP (object) && pos > BUF_Z (b))
25895 goto set_cursor;
25896
25897 /* Make the window's buffer temporarily current for
25898 overlays_at and compute_char_face. */
25899 obuf = current_buffer;
25900 current_buffer = b;
25901 obegv = BEGV;
25902 ozv = ZV;
25903 BEGV = BEG;
25904 ZV = Z;
25905
25906 /* Is this char mouse-active or does it have help-echo? */
25907 position = make_number (pos);
25908
25909 if (BUFFERP (object))
25910 {
25911 /* Put all the overlays we want in a vector in overlay_vec. */
25912 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25913 /* Sort overlays into increasing priority order. */
25914 noverlays = sort_overlays (overlay_vec, noverlays, w);
25915 }
25916 else
25917 noverlays = 0;
25918
25919 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25920
25921 if (same_region)
25922 cursor = No_Cursor;
25923
25924 /* Check mouse-face highlighting. */
25925 if (! same_region
25926 /* If there exists an overlay with mouse-face overlapping
25927 the one we are currently highlighting, we have to
25928 check if we enter the overlapping overlay, and then
25929 highlight only that. */
25930 || (OVERLAYP (hlinfo->mouse_face_overlay)
25931 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25932 {
25933 /* Find the highest priority overlay with a mouse-face. */
25934 Lisp_Object overlay = Qnil;
25935 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25936 {
25937 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25938 if (!NILP (mouse_face))
25939 overlay = overlay_vec[i];
25940 }
25941
25942 /* If we're highlighting the same overlay as before, there's
25943 no need to do that again. */
25944 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25945 goto check_help_echo;
25946 hlinfo->mouse_face_overlay = overlay;
25947
25948 /* Clear the display of the old active region, if any. */
25949 if (clear_mouse_face (hlinfo))
25950 cursor = No_Cursor;
25951
25952 /* If no overlay applies, get a text property. */
25953 if (NILP (overlay))
25954 mouse_face = Fget_text_property (position, Qmouse_face, object);
25955
25956 /* Next, compute the bounds of the mouse highlighting and
25957 display it. */
25958 if (!NILP (mouse_face) && STRINGP (object))
25959 {
25960 /* The mouse-highlighting comes from a display string
25961 with a mouse-face. */
25962 Lisp_Object s, e;
25963 EMACS_INT ignore;
25964
25965 s = Fprevious_single_property_change
25966 (make_number (pos + 1), Qmouse_face, object, Qnil);
25967 e = Fnext_single_property_change
25968 (position, Qmouse_face, object, Qnil);
25969 if (NILP (s))
25970 s = make_number (0);
25971 if (NILP (e))
25972 e = make_number (SCHARS (object) - 1);
25973 mouse_face_from_string_pos (w, hlinfo, object,
25974 XINT (s), XINT (e));
25975 hlinfo->mouse_face_past_end = 0;
25976 hlinfo->mouse_face_window = window;
25977 hlinfo->mouse_face_face_id
25978 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25979 glyph->face_id, 1);
25980 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25981 cursor = No_Cursor;
25982 }
25983 else
25984 {
25985 /* The mouse-highlighting, if any, comes from an overlay
25986 or text property in the buffer. */
25987 Lisp_Object buffer IF_LINT (= Qnil);
25988 Lisp_Object cover_string IF_LINT (= Qnil);
25989
25990 if (STRINGP (object))
25991 {
25992 /* If we are on a display string with no mouse-face,
25993 check if the text under it has one. */
25994 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25995 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25996 pos = string_buffer_position (object, start);
25997 if (pos > 0)
25998 {
25999 mouse_face = get_char_property_and_overlay
26000 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26001 buffer = w->buffer;
26002 cover_string = object;
26003 }
26004 }
26005 else
26006 {
26007 buffer = object;
26008 cover_string = Qnil;
26009 }
26010
26011 if (!NILP (mouse_face))
26012 {
26013 Lisp_Object before, after;
26014 Lisp_Object before_string, after_string;
26015 /* To correctly find the limits of mouse highlight
26016 in a bidi-reordered buffer, we must not use the
26017 optimization of limiting the search in
26018 previous-single-property-change and
26019 next-single-property-change, because
26020 rows_from_pos_range needs the real start and end
26021 positions to DTRT in this case. That's because
26022 the first row visible in a window does not
26023 necessarily display the character whose position
26024 is the smallest. */
26025 Lisp_Object lim1 =
26026 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26027 ? Fmarker_position (w->start)
26028 : Qnil;
26029 Lisp_Object lim2 =
26030 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26031 ? make_number (BUF_Z (XBUFFER (buffer))
26032 - XFASTINT (w->window_end_pos))
26033 : Qnil;
26034
26035 if (NILP (overlay))
26036 {
26037 /* Handle the text property case. */
26038 before = Fprevious_single_property_change
26039 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26040 after = Fnext_single_property_change
26041 (make_number (pos), Qmouse_face, buffer, lim2);
26042 before_string = after_string = Qnil;
26043 }
26044 else
26045 {
26046 /* Handle the overlay case. */
26047 before = Foverlay_start (overlay);
26048 after = Foverlay_end (overlay);
26049 before_string = Foverlay_get (overlay, Qbefore_string);
26050 after_string = Foverlay_get (overlay, Qafter_string);
26051
26052 if (!STRINGP (before_string)) before_string = Qnil;
26053 if (!STRINGP (after_string)) after_string = Qnil;
26054 }
26055
26056 mouse_face_from_buffer_pos (window, hlinfo, pos,
26057 XFASTINT (before),
26058 XFASTINT (after),
26059 before_string, after_string,
26060 cover_string);
26061 cursor = No_Cursor;
26062 }
26063 }
26064 }
26065
26066 check_help_echo:
26067
26068 /* Look for a `help-echo' property. */
26069 if (NILP (help_echo_string)) {
26070 Lisp_Object help, overlay;
26071
26072 /* Check overlays first. */
26073 help = overlay = Qnil;
26074 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26075 {
26076 overlay = overlay_vec[i];
26077 help = Foverlay_get (overlay, Qhelp_echo);
26078 }
26079
26080 if (!NILP (help))
26081 {
26082 help_echo_string = help;
26083 help_echo_window = window;
26084 help_echo_object = overlay;
26085 help_echo_pos = pos;
26086 }
26087 else
26088 {
26089 Lisp_Object obj = glyph->object;
26090 EMACS_INT charpos = glyph->charpos;
26091
26092 /* Try text properties. */
26093 if (STRINGP (obj)
26094 && charpos >= 0
26095 && charpos < SCHARS (obj))
26096 {
26097 help = Fget_text_property (make_number (charpos),
26098 Qhelp_echo, obj);
26099 if (NILP (help))
26100 {
26101 /* If the string itself doesn't specify a help-echo,
26102 see if the buffer text ``under'' it does. */
26103 struct glyph_row *r
26104 = MATRIX_ROW (w->current_matrix, vpos);
26105 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26106 EMACS_INT p = string_buffer_position (obj, start);
26107 if (p > 0)
26108 {
26109 help = Fget_char_property (make_number (p),
26110 Qhelp_echo, w->buffer);
26111 if (!NILP (help))
26112 {
26113 charpos = p;
26114 obj = w->buffer;
26115 }
26116 }
26117 }
26118 }
26119 else if (BUFFERP (obj)
26120 && charpos >= BEGV
26121 && charpos < ZV)
26122 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26123 obj);
26124
26125 if (!NILP (help))
26126 {
26127 help_echo_string = help;
26128 help_echo_window = window;
26129 help_echo_object = obj;
26130 help_echo_pos = charpos;
26131 }
26132 }
26133 }
26134
26135 #ifdef HAVE_WINDOW_SYSTEM
26136 /* Look for a `pointer' property. */
26137 if (FRAME_WINDOW_P (f) && NILP (pointer))
26138 {
26139 /* Check overlays first. */
26140 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26141 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26142
26143 if (NILP (pointer))
26144 {
26145 Lisp_Object obj = glyph->object;
26146 EMACS_INT charpos = glyph->charpos;
26147
26148 /* Try text properties. */
26149 if (STRINGP (obj)
26150 && charpos >= 0
26151 && charpos < SCHARS (obj))
26152 {
26153 pointer = Fget_text_property (make_number (charpos),
26154 Qpointer, obj);
26155 if (NILP (pointer))
26156 {
26157 /* If the string itself doesn't specify a pointer,
26158 see if the buffer text ``under'' it does. */
26159 struct glyph_row *r
26160 = MATRIX_ROW (w->current_matrix, vpos);
26161 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26162 EMACS_INT p = string_buffer_position (obj, start);
26163 if (p > 0)
26164 pointer = Fget_char_property (make_number (p),
26165 Qpointer, w->buffer);
26166 }
26167 }
26168 else if (BUFFERP (obj)
26169 && charpos >= BEGV
26170 && charpos < ZV)
26171 pointer = Fget_text_property (make_number (charpos),
26172 Qpointer, obj);
26173 }
26174 }
26175 #endif /* HAVE_WINDOW_SYSTEM */
26176
26177 BEGV = obegv;
26178 ZV = ozv;
26179 current_buffer = obuf;
26180 }
26181
26182 set_cursor:
26183
26184 #ifdef HAVE_WINDOW_SYSTEM
26185 if (FRAME_WINDOW_P (f))
26186 define_frame_cursor1 (f, cursor, pointer);
26187 #else
26188 /* This is here to prevent a compiler error, about "label at end of
26189 compound statement". */
26190 return;
26191 #endif
26192 }
26193
26194
26195 /* EXPORT for RIF:
26196 Clear any mouse-face on window W. This function is part of the
26197 redisplay interface, and is called from try_window_id and similar
26198 functions to ensure the mouse-highlight is off. */
26199
26200 void
26201 x_clear_window_mouse_face (struct window *w)
26202 {
26203 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26204 Lisp_Object window;
26205
26206 BLOCK_INPUT;
26207 XSETWINDOW (window, w);
26208 if (EQ (window, hlinfo->mouse_face_window))
26209 clear_mouse_face (hlinfo);
26210 UNBLOCK_INPUT;
26211 }
26212
26213
26214 /* EXPORT:
26215 Just discard the mouse face information for frame F, if any.
26216 This is used when the size of F is changed. */
26217
26218 void
26219 cancel_mouse_face (struct frame *f)
26220 {
26221 Lisp_Object window;
26222 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26223
26224 window = hlinfo->mouse_face_window;
26225 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26226 {
26227 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26228 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26229 hlinfo->mouse_face_window = Qnil;
26230 }
26231 }
26232
26233
26234 \f
26235 /***********************************************************************
26236 Exposure Events
26237 ***********************************************************************/
26238
26239 #ifdef HAVE_WINDOW_SYSTEM
26240
26241 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26242 which intersects rectangle R. R is in window-relative coordinates. */
26243
26244 static void
26245 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26246 enum glyph_row_area area)
26247 {
26248 struct glyph *first = row->glyphs[area];
26249 struct glyph *end = row->glyphs[area] + row->used[area];
26250 struct glyph *last;
26251 int first_x, start_x, x;
26252
26253 if (area == TEXT_AREA && row->fill_line_p)
26254 /* If row extends face to end of line write the whole line. */
26255 draw_glyphs (w, 0, row, area,
26256 0, row->used[area],
26257 DRAW_NORMAL_TEXT, 0);
26258 else
26259 {
26260 /* Set START_X to the window-relative start position for drawing glyphs of
26261 AREA. The first glyph of the text area can be partially visible.
26262 The first glyphs of other areas cannot. */
26263 start_x = window_box_left_offset (w, area);
26264 x = start_x;
26265 if (area == TEXT_AREA)
26266 x += row->x;
26267
26268 /* Find the first glyph that must be redrawn. */
26269 while (first < end
26270 && x + first->pixel_width < r->x)
26271 {
26272 x += first->pixel_width;
26273 ++first;
26274 }
26275
26276 /* Find the last one. */
26277 last = first;
26278 first_x = x;
26279 while (last < end
26280 && x < r->x + r->width)
26281 {
26282 x += last->pixel_width;
26283 ++last;
26284 }
26285
26286 /* Repaint. */
26287 if (last > first)
26288 draw_glyphs (w, first_x - start_x, row, area,
26289 first - row->glyphs[area], last - row->glyphs[area],
26290 DRAW_NORMAL_TEXT, 0);
26291 }
26292 }
26293
26294
26295 /* Redraw the parts of the glyph row ROW on window W intersecting
26296 rectangle R. R is in window-relative coordinates. Value is
26297 non-zero if mouse-face was overwritten. */
26298
26299 static int
26300 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26301 {
26302 xassert (row->enabled_p);
26303
26304 if (row->mode_line_p || w->pseudo_window_p)
26305 draw_glyphs (w, 0, row, TEXT_AREA,
26306 0, row->used[TEXT_AREA],
26307 DRAW_NORMAL_TEXT, 0);
26308 else
26309 {
26310 if (row->used[LEFT_MARGIN_AREA])
26311 expose_area (w, row, r, LEFT_MARGIN_AREA);
26312 if (row->used[TEXT_AREA])
26313 expose_area (w, row, r, TEXT_AREA);
26314 if (row->used[RIGHT_MARGIN_AREA])
26315 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26316 draw_row_fringe_bitmaps (w, row);
26317 }
26318
26319 return row->mouse_face_p;
26320 }
26321
26322
26323 /* Redraw those parts of glyphs rows during expose event handling that
26324 overlap other rows. Redrawing of an exposed line writes over parts
26325 of lines overlapping that exposed line; this function fixes that.
26326
26327 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26328 row in W's current matrix that is exposed and overlaps other rows.
26329 LAST_OVERLAPPING_ROW is the last such row. */
26330
26331 static void
26332 expose_overlaps (struct window *w,
26333 struct glyph_row *first_overlapping_row,
26334 struct glyph_row *last_overlapping_row,
26335 XRectangle *r)
26336 {
26337 struct glyph_row *row;
26338
26339 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26340 if (row->overlapping_p)
26341 {
26342 xassert (row->enabled_p && !row->mode_line_p);
26343
26344 row->clip = r;
26345 if (row->used[LEFT_MARGIN_AREA])
26346 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26347
26348 if (row->used[TEXT_AREA])
26349 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26350
26351 if (row->used[RIGHT_MARGIN_AREA])
26352 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26353 row->clip = NULL;
26354 }
26355 }
26356
26357
26358 /* Return non-zero if W's cursor intersects rectangle R. */
26359
26360 static int
26361 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26362 {
26363 XRectangle cr, result;
26364 struct glyph *cursor_glyph;
26365 struct glyph_row *row;
26366
26367 if (w->phys_cursor.vpos >= 0
26368 && w->phys_cursor.vpos < w->current_matrix->nrows
26369 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26370 row->enabled_p)
26371 && row->cursor_in_fringe_p)
26372 {
26373 /* Cursor is in the fringe. */
26374 cr.x = window_box_right_offset (w,
26375 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26376 ? RIGHT_MARGIN_AREA
26377 : TEXT_AREA));
26378 cr.y = row->y;
26379 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26380 cr.height = row->height;
26381 return x_intersect_rectangles (&cr, r, &result);
26382 }
26383
26384 cursor_glyph = get_phys_cursor_glyph (w);
26385 if (cursor_glyph)
26386 {
26387 /* r is relative to W's box, but w->phys_cursor.x is relative
26388 to left edge of W's TEXT area. Adjust it. */
26389 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26390 cr.y = w->phys_cursor.y;
26391 cr.width = cursor_glyph->pixel_width;
26392 cr.height = w->phys_cursor_height;
26393 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26394 I assume the effect is the same -- and this is portable. */
26395 return x_intersect_rectangles (&cr, r, &result);
26396 }
26397 /* If we don't understand the format, pretend we're not in the hot-spot. */
26398 return 0;
26399 }
26400
26401
26402 /* EXPORT:
26403 Draw a vertical window border to the right of window W if W doesn't
26404 have vertical scroll bars. */
26405
26406 void
26407 x_draw_vertical_border (struct window *w)
26408 {
26409 struct frame *f = XFRAME (WINDOW_FRAME (w));
26410
26411 /* We could do better, if we knew what type of scroll-bar the adjacent
26412 windows (on either side) have... But we don't :-(
26413 However, I think this works ok. ++KFS 2003-04-25 */
26414
26415 /* Redraw borders between horizontally adjacent windows. Don't
26416 do it for frames with vertical scroll bars because either the
26417 right scroll bar of a window, or the left scroll bar of its
26418 neighbor will suffice as a border. */
26419 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26420 return;
26421
26422 if (!WINDOW_RIGHTMOST_P (w)
26423 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26424 {
26425 int x0, x1, y0, y1;
26426
26427 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26428 y1 -= 1;
26429
26430 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26431 x1 -= 1;
26432
26433 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26434 }
26435 else if (!WINDOW_LEFTMOST_P (w)
26436 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26437 {
26438 int x0, x1, y0, y1;
26439
26440 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26441 y1 -= 1;
26442
26443 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26444 x0 -= 1;
26445
26446 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26447 }
26448 }
26449
26450
26451 /* Redraw the part of window W intersection rectangle FR. Pixel
26452 coordinates in FR are frame-relative. Call this function with
26453 input blocked. Value is non-zero if the exposure overwrites
26454 mouse-face. */
26455
26456 static int
26457 expose_window (struct window *w, XRectangle *fr)
26458 {
26459 struct frame *f = XFRAME (w->frame);
26460 XRectangle wr, r;
26461 int mouse_face_overwritten_p = 0;
26462
26463 /* If window is not yet fully initialized, do nothing. This can
26464 happen when toolkit scroll bars are used and a window is split.
26465 Reconfiguring the scroll bar will generate an expose for a newly
26466 created window. */
26467 if (w->current_matrix == NULL)
26468 return 0;
26469
26470 /* When we're currently updating the window, display and current
26471 matrix usually don't agree. Arrange for a thorough display
26472 later. */
26473 if (w == updated_window)
26474 {
26475 SET_FRAME_GARBAGED (f);
26476 return 0;
26477 }
26478
26479 /* Frame-relative pixel rectangle of W. */
26480 wr.x = WINDOW_LEFT_EDGE_X (w);
26481 wr.y = WINDOW_TOP_EDGE_Y (w);
26482 wr.width = WINDOW_TOTAL_WIDTH (w);
26483 wr.height = WINDOW_TOTAL_HEIGHT (w);
26484
26485 if (x_intersect_rectangles (fr, &wr, &r))
26486 {
26487 int yb = window_text_bottom_y (w);
26488 struct glyph_row *row;
26489 int cursor_cleared_p;
26490 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26491
26492 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26493 r.x, r.y, r.width, r.height));
26494
26495 /* Convert to window coordinates. */
26496 r.x -= WINDOW_LEFT_EDGE_X (w);
26497 r.y -= WINDOW_TOP_EDGE_Y (w);
26498
26499 /* Turn off the cursor. */
26500 if (!w->pseudo_window_p
26501 && phys_cursor_in_rect_p (w, &r))
26502 {
26503 x_clear_cursor (w);
26504 cursor_cleared_p = 1;
26505 }
26506 else
26507 cursor_cleared_p = 0;
26508
26509 /* Update lines intersecting rectangle R. */
26510 first_overlapping_row = last_overlapping_row = NULL;
26511 for (row = w->current_matrix->rows;
26512 row->enabled_p;
26513 ++row)
26514 {
26515 int y0 = row->y;
26516 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26517
26518 if ((y0 >= r.y && y0 < r.y + r.height)
26519 || (y1 > r.y && y1 < r.y + r.height)
26520 || (r.y >= y0 && r.y < y1)
26521 || (r.y + r.height > y0 && r.y + r.height < y1))
26522 {
26523 /* A header line may be overlapping, but there is no need
26524 to fix overlapping areas for them. KFS 2005-02-12 */
26525 if (row->overlapping_p && !row->mode_line_p)
26526 {
26527 if (first_overlapping_row == NULL)
26528 first_overlapping_row = row;
26529 last_overlapping_row = row;
26530 }
26531
26532 row->clip = fr;
26533 if (expose_line (w, row, &r))
26534 mouse_face_overwritten_p = 1;
26535 row->clip = NULL;
26536 }
26537 else if (row->overlapping_p)
26538 {
26539 /* We must redraw a row overlapping the exposed area. */
26540 if (y0 < r.y
26541 ? y0 + row->phys_height > r.y
26542 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26543 {
26544 if (first_overlapping_row == NULL)
26545 first_overlapping_row = row;
26546 last_overlapping_row = row;
26547 }
26548 }
26549
26550 if (y1 >= yb)
26551 break;
26552 }
26553
26554 /* Display the mode line if there is one. */
26555 if (WINDOW_WANTS_MODELINE_P (w)
26556 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26557 row->enabled_p)
26558 && row->y < r.y + r.height)
26559 {
26560 if (expose_line (w, row, &r))
26561 mouse_face_overwritten_p = 1;
26562 }
26563
26564 if (!w->pseudo_window_p)
26565 {
26566 /* Fix the display of overlapping rows. */
26567 if (first_overlapping_row)
26568 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26569 fr);
26570
26571 /* Draw border between windows. */
26572 x_draw_vertical_border (w);
26573
26574 /* Turn the cursor on again. */
26575 if (cursor_cleared_p)
26576 update_window_cursor (w, 1);
26577 }
26578 }
26579
26580 return mouse_face_overwritten_p;
26581 }
26582
26583
26584
26585 /* Redraw (parts) of all windows in the window tree rooted at W that
26586 intersect R. R contains frame pixel coordinates. Value is
26587 non-zero if the exposure overwrites mouse-face. */
26588
26589 static int
26590 expose_window_tree (struct window *w, XRectangle *r)
26591 {
26592 struct frame *f = XFRAME (w->frame);
26593 int mouse_face_overwritten_p = 0;
26594
26595 while (w && !FRAME_GARBAGED_P (f))
26596 {
26597 if (!NILP (w->hchild))
26598 mouse_face_overwritten_p
26599 |= expose_window_tree (XWINDOW (w->hchild), r);
26600 else if (!NILP (w->vchild))
26601 mouse_face_overwritten_p
26602 |= expose_window_tree (XWINDOW (w->vchild), r);
26603 else
26604 mouse_face_overwritten_p |= expose_window (w, r);
26605
26606 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26607 }
26608
26609 return mouse_face_overwritten_p;
26610 }
26611
26612
26613 /* EXPORT:
26614 Redisplay an exposed area of frame F. X and Y are the upper-left
26615 corner of the exposed rectangle. W and H are width and height of
26616 the exposed area. All are pixel values. W or H zero means redraw
26617 the entire frame. */
26618
26619 void
26620 expose_frame (struct frame *f, int x, int y, int w, int h)
26621 {
26622 XRectangle r;
26623 int mouse_face_overwritten_p = 0;
26624
26625 TRACE ((stderr, "expose_frame "));
26626
26627 /* No need to redraw if frame will be redrawn soon. */
26628 if (FRAME_GARBAGED_P (f))
26629 {
26630 TRACE ((stderr, " garbaged\n"));
26631 return;
26632 }
26633
26634 /* If basic faces haven't been realized yet, there is no point in
26635 trying to redraw anything. This can happen when we get an expose
26636 event while Emacs is starting, e.g. by moving another window. */
26637 if (FRAME_FACE_CACHE (f) == NULL
26638 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26639 {
26640 TRACE ((stderr, " no faces\n"));
26641 return;
26642 }
26643
26644 if (w == 0 || h == 0)
26645 {
26646 r.x = r.y = 0;
26647 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26648 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26649 }
26650 else
26651 {
26652 r.x = x;
26653 r.y = y;
26654 r.width = w;
26655 r.height = h;
26656 }
26657
26658 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26659 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26660
26661 if (WINDOWP (f->tool_bar_window))
26662 mouse_face_overwritten_p
26663 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26664
26665 #ifdef HAVE_X_WINDOWS
26666 #ifndef MSDOS
26667 #ifndef USE_X_TOOLKIT
26668 if (WINDOWP (f->menu_bar_window))
26669 mouse_face_overwritten_p
26670 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26671 #endif /* not USE_X_TOOLKIT */
26672 #endif
26673 #endif
26674
26675 /* Some window managers support a focus-follows-mouse style with
26676 delayed raising of frames. Imagine a partially obscured frame,
26677 and moving the mouse into partially obscured mouse-face on that
26678 frame. The visible part of the mouse-face will be highlighted,
26679 then the WM raises the obscured frame. With at least one WM, KDE
26680 2.1, Emacs is not getting any event for the raising of the frame
26681 (even tried with SubstructureRedirectMask), only Expose events.
26682 These expose events will draw text normally, i.e. not
26683 highlighted. Which means we must redo the highlight here.
26684 Subsume it under ``we love X''. --gerd 2001-08-15 */
26685 /* Included in Windows version because Windows most likely does not
26686 do the right thing if any third party tool offers
26687 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26688 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26689 {
26690 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26691 if (f == hlinfo->mouse_face_mouse_frame)
26692 {
26693 int mouse_x = hlinfo->mouse_face_mouse_x;
26694 int mouse_y = hlinfo->mouse_face_mouse_y;
26695 clear_mouse_face (hlinfo);
26696 note_mouse_highlight (f, mouse_x, mouse_y);
26697 }
26698 }
26699 }
26700
26701
26702 /* EXPORT:
26703 Determine the intersection of two rectangles R1 and R2. Return
26704 the intersection in *RESULT. Value is non-zero if RESULT is not
26705 empty. */
26706
26707 int
26708 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26709 {
26710 XRectangle *left, *right;
26711 XRectangle *upper, *lower;
26712 int intersection_p = 0;
26713
26714 /* Rearrange so that R1 is the left-most rectangle. */
26715 if (r1->x < r2->x)
26716 left = r1, right = r2;
26717 else
26718 left = r2, right = r1;
26719
26720 /* X0 of the intersection is right.x0, if this is inside R1,
26721 otherwise there is no intersection. */
26722 if (right->x <= left->x + left->width)
26723 {
26724 result->x = right->x;
26725
26726 /* The right end of the intersection is the minimum of the
26727 the right ends of left and right. */
26728 result->width = (min (left->x + left->width, right->x + right->width)
26729 - result->x);
26730
26731 /* Same game for Y. */
26732 if (r1->y < r2->y)
26733 upper = r1, lower = r2;
26734 else
26735 upper = r2, lower = r1;
26736
26737 /* The upper end of the intersection is lower.y0, if this is inside
26738 of upper. Otherwise, there is no intersection. */
26739 if (lower->y <= upper->y + upper->height)
26740 {
26741 result->y = lower->y;
26742
26743 /* The lower end of the intersection is the minimum of the lower
26744 ends of upper and lower. */
26745 result->height = (min (lower->y + lower->height,
26746 upper->y + upper->height)
26747 - result->y);
26748 intersection_p = 1;
26749 }
26750 }
26751
26752 return intersection_p;
26753 }
26754
26755 #endif /* HAVE_WINDOW_SYSTEM */
26756
26757 \f
26758 /***********************************************************************
26759 Initialization
26760 ***********************************************************************/
26761
26762 void
26763 syms_of_xdisp (void)
26764 {
26765 Vwith_echo_area_save_vector = Qnil;
26766 staticpro (&Vwith_echo_area_save_vector);
26767
26768 Vmessage_stack = Qnil;
26769 staticpro (&Vmessage_stack);
26770
26771 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26772 staticpro (&Qinhibit_redisplay);
26773
26774 message_dolog_marker1 = Fmake_marker ();
26775 staticpro (&message_dolog_marker1);
26776 message_dolog_marker2 = Fmake_marker ();
26777 staticpro (&message_dolog_marker2);
26778 message_dolog_marker3 = Fmake_marker ();
26779 staticpro (&message_dolog_marker3);
26780
26781 #if GLYPH_DEBUG
26782 defsubr (&Sdump_frame_glyph_matrix);
26783 defsubr (&Sdump_glyph_matrix);
26784 defsubr (&Sdump_glyph_row);
26785 defsubr (&Sdump_tool_bar_row);
26786 defsubr (&Strace_redisplay);
26787 defsubr (&Strace_to_stderr);
26788 #endif
26789 #ifdef HAVE_WINDOW_SYSTEM
26790 defsubr (&Stool_bar_lines_needed);
26791 defsubr (&Slookup_image_map);
26792 #endif
26793 defsubr (&Sformat_mode_line);
26794 defsubr (&Sinvisible_p);
26795 defsubr (&Scurrent_bidi_paragraph_direction);
26796
26797 staticpro (&Qmenu_bar_update_hook);
26798 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26799
26800 staticpro (&Qoverriding_terminal_local_map);
26801 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26802
26803 staticpro (&Qoverriding_local_map);
26804 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26805
26806 staticpro (&Qwindow_scroll_functions);
26807 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26808
26809 staticpro (&Qwindow_text_change_functions);
26810 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26811
26812 staticpro (&Qredisplay_end_trigger_functions);
26813 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26814
26815 staticpro (&Qinhibit_point_motion_hooks);
26816 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26817
26818 Qeval = intern_c_string ("eval");
26819 staticpro (&Qeval);
26820
26821 QCdata = intern_c_string (":data");
26822 staticpro (&QCdata);
26823 Qdisplay = intern_c_string ("display");
26824 staticpro (&Qdisplay);
26825 Qspace_width = intern_c_string ("space-width");
26826 staticpro (&Qspace_width);
26827 Qraise = intern_c_string ("raise");
26828 staticpro (&Qraise);
26829 Qslice = intern_c_string ("slice");
26830 staticpro (&Qslice);
26831 Qspace = intern_c_string ("space");
26832 staticpro (&Qspace);
26833 Qmargin = intern_c_string ("margin");
26834 staticpro (&Qmargin);
26835 Qpointer = intern_c_string ("pointer");
26836 staticpro (&Qpointer);
26837 Qleft_margin = intern_c_string ("left-margin");
26838 staticpro (&Qleft_margin);
26839 Qright_margin = intern_c_string ("right-margin");
26840 staticpro (&Qright_margin);
26841 Qcenter = intern_c_string ("center");
26842 staticpro (&Qcenter);
26843 Qline_height = intern_c_string ("line-height");
26844 staticpro (&Qline_height);
26845 QCalign_to = intern_c_string (":align-to");
26846 staticpro (&QCalign_to);
26847 QCrelative_width = intern_c_string (":relative-width");
26848 staticpro (&QCrelative_width);
26849 QCrelative_height = intern_c_string (":relative-height");
26850 staticpro (&QCrelative_height);
26851 QCeval = intern_c_string (":eval");
26852 staticpro (&QCeval);
26853 QCpropertize = intern_c_string (":propertize");
26854 staticpro (&QCpropertize);
26855 QCfile = intern_c_string (":file");
26856 staticpro (&QCfile);
26857 Qfontified = intern_c_string ("fontified");
26858 staticpro (&Qfontified);
26859 Qfontification_functions = intern_c_string ("fontification-functions");
26860 staticpro (&Qfontification_functions);
26861 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26862 staticpro (&Qtrailing_whitespace);
26863 Qescape_glyph = intern_c_string ("escape-glyph");
26864 staticpro (&Qescape_glyph);
26865 Qnobreak_space = intern_c_string ("nobreak-space");
26866 staticpro (&Qnobreak_space);
26867 Qimage = intern_c_string ("image");
26868 staticpro (&Qimage);
26869 Qtext = intern_c_string ("text");
26870 staticpro (&Qtext);
26871 Qboth = intern_c_string ("both");
26872 staticpro (&Qboth);
26873 Qboth_horiz = intern_c_string ("both-horiz");
26874 staticpro (&Qboth_horiz);
26875 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26876 staticpro (&Qtext_image_horiz);
26877 QCmap = intern_c_string (":map");
26878 staticpro (&QCmap);
26879 QCpointer = intern_c_string (":pointer");
26880 staticpro (&QCpointer);
26881 Qrect = intern_c_string ("rect");
26882 staticpro (&Qrect);
26883 Qcircle = intern_c_string ("circle");
26884 staticpro (&Qcircle);
26885 Qpoly = intern_c_string ("poly");
26886 staticpro (&Qpoly);
26887 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26888 staticpro (&Qmessage_truncate_lines);
26889 Qgrow_only = intern_c_string ("grow-only");
26890 staticpro (&Qgrow_only);
26891 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26892 staticpro (&Qinhibit_menubar_update);
26893 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26894 staticpro (&Qinhibit_eval_during_redisplay);
26895 Qposition = intern_c_string ("position");
26896 staticpro (&Qposition);
26897 Qbuffer_position = intern_c_string ("buffer-position");
26898 staticpro (&Qbuffer_position);
26899 Qobject = intern_c_string ("object");
26900 staticpro (&Qobject);
26901 Qbar = intern_c_string ("bar");
26902 staticpro (&Qbar);
26903 Qhbar = intern_c_string ("hbar");
26904 staticpro (&Qhbar);
26905 Qbox = intern_c_string ("box");
26906 staticpro (&Qbox);
26907 Qhollow = intern_c_string ("hollow");
26908 staticpro (&Qhollow);
26909 Qhand = intern_c_string ("hand");
26910 staticpro (&Qhand);
26911 Qarrow = intern_c_string ("arrow");
26912 staticpro (&Qarrow);
26913 Qtext = intern_c_string ("text");
26914 staticpro (&Qtext);
26915 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26916 staticpro (&Qinhibit_free_realized_faces);
26917
26918 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26919 Fcons (intern_c_string ("void-variable"), Qnil)),
26920 Qnil);
26921 staticpro (&list_of_error);
26922
26923 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26924 staticpro (&Qlast_arrow_position);
26925 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26926 staticpro (&Qlast_arrow_string);
26927
26928 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26929 staticpro (&Qoverlay_arrow_string);
26930 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26931 staticpro (&Qoverlay_arrow_bitmap);
26932
26933 echo_buffer[0] = echo_buffer[1] = Qnil;
26934 staticpro (&echo_buffer[0]);
26935 staticpro (&echo_buffer[1]);
26936
26937 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26938 staticpro (&echo_area_buffer[0]);
26939 staticpro (&echo_area_buffer[1]);
26940
26941 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26942 staticpro (&Vmessages_buffer_name);
26943
26944 mode_line_proptrans_alist = Qnil;
26945 staticpro (&mode_line_proptrans_alist);
26946 mode_line_string_list = Qnil;
26947 staticpro (&mode_line_string_list);
26948 mode_line_string_face = Qnil;
26949 staticpro (&mode_line_string_face);
26950 mode_line_string_face_prop = Qnil;
26951 staticpro (&mode_line_string_face_prop);
26952 Vmode_line_unwind_vector = Qnil;
26953 staticpro (&Vmode_line_unwind_vector);
26954
26955 help_echo_string = Qnil;
26956 staticpro (&help_echo_string);
26957 help_echo_object = Qnil;
26958 staticpro (&help_echo_object);
26959 help_echo_window = Qnil;
26960 staticpro (&help_echo_window);
26961 previous_help_echo_string = Qnil;
26962 staticpro (&previous_help_echo_string);
26963 help_echo_pos = -1;
26964
26965 Qright_to_left = intern_c_string ("right-to-left");
26966 staticpro (&Qright_to_left);
26967 Qleft_to_right = intern_c_string ("left-to-right");
26968 staticpro (&Qleft_to_right);
26969
26970 #ifdef HAVE_WINDOW_SYSTEM
26971 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
26972 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26973 For example, if a block cursor is over a tab, it will be drawn as
26974 wide as that tab on the display. */);
26975 x_stretch_cursor_p = 0;
26976 #endif
26977
26978 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
26979 doc: /* *Non-nil means highlight trailing whitespace.
26980 The face used for trailing whitespace is `trailing-whitespace'. */);
26981 Vshow_trailing_whitespace = Qnil;
26982
26983 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
26984 doc: /* *Control highlighting of nobreak space and soft hyphen.
26985 A value of t means highlight the character itself (for nobreak space,
26986 use face `nobreak-space').
26987 A value of nil means no highlighting.
26988 Other values mean display the escape glyph followed by an ordinary
26989 space or ordinary hyphen. */);
26990 Vnobreak_char_display = Qt;
26991
26992 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
26993 doc: /* *The pointer shape to show in void text areas.
26994 A value of nil means to show the text pointer. Other options are `arrow',
26995 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26996 Vvoid_text_area_pointer = Qarrow;
26997
26998 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
26999 doc: /* Non-nil means don't actually do any redisplay.
27000 This is used for internal purposes. */);
27001 Vinhibit_redisplay = Qnil;
27002
27003 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27004 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27005 Vglobal_mode_string = Qnil;
27006
27007 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27008 doc: /* Marker for where to display an arrow on top of the buffer text.
27009 This must be the beginning of a line in order to work.
27010 See also `overlay-arrow-string'. */);
27011 Voverlay_arrow_position = Qnil;
27012
27013 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27014 doc: /* String to display as an arrow in non-window frames.
27015 See also `overlay-arrow-position'. */);
27016 Voverlay_arrow_string = make_pure_c_string ("=>");
27017
27018 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27019 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27020 The symbols on this list are examined during redisplay to determine
27021 where to display overlay arrows. */);
27022 Voverlay_arrow_variable_list
27023 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27024
27025 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27026 doc: /* *The number of lines to try scrolling a window by when point moves out.
27027 If that fails to bring point back on frame, point is centered instead.
27028 If this is zero, point is always centered after it moves off frame.
27029 If you want scrolling to always be a line at a time, you should set
27030 `scroll-conservatively' to a large value rather than set this to 1. */);
27031
27032 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27033 doc: /* *Scroll up to this many lines, to bring point back on screen.
27034 If point moves off-screen, redisplay will scroll by up to
27035 `scroll-conservatively' lines in order to bring point just barely
27036 onto the screen again. If that cannot be done, then redisplay
27037 recenters point as usual.
27038
27039 If the value is greater than 100, redisplay will never recenter point,
27040 but will always scroll just enough text to bring point into view, even
27041 if you move far away.
27042
27043 A value of zero means always recenter point if it moves off screen. */);
27044 scroll_conservatively = 0;
27045
27046 DEFVAR_INT ("scroll-margin", scroll_margin,
27047 doc: /* *Number of lines of margin at the top and bottom of a window.
27048 Recenter the window whenever point gets within this many lines
27049 of the top or bottom of the window. */);
27050 scroll_margin = 0;
27051
27052 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27053 doc: /* Pixels per inch value for non-window system displays.
27054 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27055 Vdisplay_pixels_per_inch = make_float (72.0);
27056
27057 #if GLYPH_DEBUG
27058 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27059 #endif
27060
27061 DEFVAR_LISP ("truncate-partial-width-windows",
27062 Vtruncate_partial_width_windows,
27063 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27064 For an integer value, truncate lines in each window narrower than the
27065 full frame width, provided the window width is less than that integer;
27066 otherwise, respect the value of `truncate-lines'.
27067
27068 For any other non-nil value, truncate lines in all windows that do
27069 not span the full frame width.
27070
27071 A value of nil means to respect the value of `truncate-lines'.
27072
27073 If `word-wrap' is enabled, you might want to reduce this. */);
27074 Vtruncate_partial_width_windows = make_number (50);
27075
27076 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27077 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27078 Any other value means to use the appropriate face, `mode-line',
27079 `header-line', or `menu' respectively. */);
27080 mode_line_inverse_video = 1;
27081
27082 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27083 doc: /* *Maximum buffer size for which line number should be displayed.
27084 If the buffer is bigger than this, the line number does not appear
27085 in the mode line. A value of nil means no limit. */);
27086 Vline_number_display_limit = Qnil;
27087
27088 DEFVAR_INT ("line-number-display-limit-width",
27089 line_number_display_limit_width,
27090 doc: /* *Maximum line width (in characters) for line number display.
27091 If the average length of the lines near point is bigger than this, then the
27092 line number may be omitted from the mode line. */);
27093 line_number_display_limit_width = 200;
27094
27095 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27096 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27097 highlight_nonselected_windows = 0;
27098
27099 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27100 doc: /* Non-nil if more than one frame is visible on this display.
27101 Minibuffer-only frames don't count, but iconified frames do.
27102 This variable is not guaranteed to be accurate except while processing
27103 `frame-title-format' and `icon-title-format'. */);
27104
27105 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27106 doc: /* Template for displaying the title bar of visible frames.
27107 \(Assuming the window manager supports this feature.)
27108
27109 This variable has the same structure as `mode-line-format', except that
27110 the %c and %l constructs are ignored. It is used only on frames for
27111 which no explicit name has been set \(see `modify-frame-parameters'). */);
27112
27113 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27114 doc: /* Template for displaying the title bar of an iconified frame.
27115 \(Assuming the window manager supports this feature.)
27116 This variable has the same structure as `mode-line-format' (which see),
27117 and is used only on frames for which no explicit name has been set
27118 \(see `modify-frame-parameters'). */);
27119 Vicon_title_format
27120 = Vframe_title_format
27121 = pure_cons (intern_c_string ("multiple-frames"),
27122 pure_cons (make_pure_c_string ("%b"),
27123 pure_cons (pure_cons (empty_unibyte_string,
27124 pure_cons (intern_c_string ("invocation-name"),
27125 pure_cons (make_pure_c_string ("@"),
27126 pure_cons (intern_c_string ("system-name"),
27127 Qnil)))),
27128 Qnil)));
27129
27130 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27131 doc: /* Maximum number of lines to keep in the message log buffer.
27132 If nil, disable message logging. If t, log messages but don't truncate
27133 the buffer when it becomes large. */);
27134 Vmessage_log_max = make_number (100);
27135
27136 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27137 doc: /* Functions called before redisplay, if window sizes have changed.
27138 The value should be a list of functions that take one argument.
27139 Just before redisplay, for each frame, if any of its windows have changed
27140 size since the last redisplay, or have been split or deleted,
27141 all the functions in the list are called, with the frame as argument. */);
27142 Vwindow_size_change_functions = Qnil;
27143
27144 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27145 doc: /* List of functions to call before redisplaying a window with scrolling.
27146 Each function is called with two arguments, the window and its new
27147 display-start position. Note that these functions are also called by
27148 `set-window-buffer'. Also note that the value of `window-end' is not
27149 valid when these functions are called. */);
27150 Vwindow_scroll_functions = Qnil;
27151
27152 DEFVAR_LISP ("window-text-change-functions",
27153 Vwindow_text_change_functions,
27154 doc: /* Functions to call in redisplay when text in the window might change. */);
27155 Vwindow_text_change_functions = Qnil;
27156
27157 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27158 doc: /* Functions called when redisplay of a window reaches the end trigger.
27159 Each function is called with two arguments, the window and the end trigger value.
27160 See `set-window-redisplay-end-trigger'. */);
27161 Vredisplay_end_trigger_functions = Qnil;
27162
27163 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27164 doc: /* *Non-nil means autoselect window with mouse pointer.
27165 If nil, do not autoselect windows.
27166 A positive number means delay autoselection by that many seconds: a
27167 window is autoselected only after the mouse has remained in that
27168 window for the duration of the delay.
27169 A negative number has a similar effect, but causes windows to be
27170 autoselected only after the mouse has stopped moving. \(Because of
27171 the way Emacs compares mouse events, you will occasionally wait twice
27172 that time before the window gets selected.\)
27173 Any other value means to autoselect window instantaneously when the
27174 mouse pointer enters it.
27175
27176 Autoselection selects the minibuffer only if it is active, and never
27177 unselects the minibuffer if it is active.
27178
27179 When customizing this variable make sure that the actual value of
27180 `focus-follows-mouse' matches the behavior of your window manager. */);
27181 Vmouse_autoselect_window = Qnil;
27182
27183 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27184 doc: /* *Non-nil means automatically resize tool-bars.
27185 This dynamically changes the tool-bar's height to the minimum height
27186 that is needed to make all tool-bar items visible.
27187 If value is `grow-only', the tool-bar's height is only increased
27188 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27189 Vauto_resize_tool_bars = Qt;
27190
27191 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27192 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27193 auto_raise_tool_bar_buttons_p = 1;
27194
27195 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27196 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27197 make_cursor_line_fully_visible_p = 1;
27198
27199 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27200 doc: /* *Border below tool-bar in pixels.
27201 If an integer, use it as the height of the border.
27202 If it is one of `internal-border-width' or `border-width', use the
27203 value of the corresponding frame parameter.
27204 Otherwise, no border is added below the tool-bar. */);
27205 Vtool_bar_border = Qinternal_border_width;
27206
27207 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27208 doc: /* *Margin around tool-bar buttons in pixels.
27209 If an integer, use that for both horizontal and vertical margins.
27210 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27211 HORZ specifying the horizontal margin, and VERT specifying the
27212 vertical margin. */);
27213 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27214
27215 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27216 doc: /* *Relief thickness of tool-bar buttons. */);
27217 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27218
27219 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27220 doc: /* Tool bar style to use.
27221 It can be one of
27222 image - show images only
27223 text - show text only
27224 both - show both, text below image
27225 both-horiz - show text to the right of the image
27226 text-image-horiz - show text to the left of the image
27227 any other - use system default or image if no system default. */);
27228 Vtool_bar_style = Qnil;
27229
27230 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27231 doc: /* *Maximum number of characters a label can have to be shown.
27232 The tool bar style must also show labels for this to have any effect, see
27233 `tool-bar-style'. */);
27234 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27235
27236 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27237 doc: /* List of functions to call to fontify regions of text.
27238 Each function is called with one argument POS. Functions must
27239 fontify a region starting at POS in the current buffer, and give
27240 fontified regions the property `fontified'. */);
27241 Vfontification_functions = Qnil;
27242 Fmake_variable_buffer_local (Qfontification_functions);
27243
27244 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27245 unibyte_display_via_language_environment,
27246 doc: /* *Non-nil means display unibyte text according to language environment.
27247 Specifically, this means that raw bytes in the range 160-255 decimal
27248 are displayed by converting them to the equivalent multibyte characters
27249 according to the current language environment. As a result, they are
27250 displayed according to the current fontset.
27251
27252 Note that this variable affects only how these bytes are displayed,
27253 but does not change the fact they are interpreted as raw bytes. */);
27254 unibyte_display_via_language_environment = 0;
27255
27256 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27257 doc: /* *Maximum height for resizing mini-windows.
27258 If a float, it specifies a fraction of the mini-window frame's height.
27259 If an integer, it specifies a number of lines. */);
27260 Vmax_mini_window_height = make_float (0.25);
27261
27262 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27263 doc: /* *How to resize mini-windows.
27264 A value of nil means don't automatically resize mini-windows.
27265 A value of t means resize them to fit the text displayed in them.
27266 A value of `grow-only', the default, means let mini-windows grow
27267 only, until their display becomes empty, at which point the windows
27268 go back to their normal size. */);
27269 Vresize_mini_windows = Qgrow_only;
27270
27271 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27272 doc: /* Alist specifying how to blink the cursor off.
27273 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27274 `cursor-type' frame-parameter or variable equals ON-STATE,
27275 comparing using `equal', Emacs uses OFF-STATE to specify
27276 how to blink it off. ON-STATE and OFF-STATE are values for
27277 the `cursor-type' frame parameter.
27278
27279 If a frame's ON-STATE has no entry in this list,
27280 the frame's other specifications determine how to blink the cursor off. */);
27281 Vblink_cursor_alist = Qnil;
27282
27283 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27284 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27285 If non-nil, windows are automatically scrolled horizontally to make
27286 point visible. */);
27287 automatic_hscrolling_p = 1;
27288 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
27289 staticpro (&Qauto_hscroll_mode);
27290
27291 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27292 doc: /* *How many columns away from the window edge point is allowed to get
27293 before automatic hscrolling will horizontally scroll the window. */);
27294 hscroll_margin = 5;
27295
27296 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27297 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27298 When point is less than `hscroll-margin' columns from the window
27299 edge, automatic hscrolling will scroll the window by the amount of columns
27300 determined by this variable. If its value is a positive integer, scroll that
27301 many columns. If it's a positive floating-point number, it specifies the
27302 fraction of the window's width to scroll. If it's nil or zero, point will be
27303 centered horizontally after the scroll. Any other value, including negative
27304 numbers, are treated as if the value were zero.
27305
27306 Automatic hscrolling always moves point outside the scroll margin, so if
27307 point was more than scroll step columns inside the margin, the window will
27308 scroll more than the value given by the scroll step.
27309
27310 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27311 and `scroll-right' overrides this variable's effect. */);
27312 Vhscroll_step = make_number (0);
27313
27314 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27315 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27316 Bind this around calls to `message' to let it take effect. */);
27317 message_truncate_lines = 0;
27318
27319 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27320 doc: /* Normal hook run to update the menu bar definitions.
27321 Redisplay runs this hook before it redisplays the menu bar.
27322 This is used to update submenus such as Buffers,
27323 whose contents depend on various data. */);
27324 Vmenu_bar_update_hook = Qnil;
27325
27326 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27327 doc: /* Frame for which we are updating a menu.
27328 The enable predicate for a menu binding should check this variable. */);
27329 Vmenu_updating_frame = Qnil;
27330
27331 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27332 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27333 inhibit_menubar_update = 0;
27334
27335 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27336 doc: /* Prefix prepended to all continuation lines at display time.
27337 The value may be a string, an image, or a stretch-glyph; it is
27338 interpreted in the same way as the value of a `display' text property.
27339
27340 This variable is overridden by any `wrap-prefix' text or overlay
27341 property.
27342
27343 To add a prefix to non-continuation lines, use `line-prefix'. */);
27344 Vwrap_prefix = Qnil;
27345 staticpro (&Qwrap_prefix);
27346 Qwrap_prefix = intern_c_string ("wrap-prefix");
27347 Fmake_variable_buffer_local (Qwrap_prefix);
27348
27349 DEFVAR_LISP ("line-prefix", Vline_prefix,
27350 doc: /* Prefix prepended to all non-continuation lines at display time.
27351 The value may be a string, an image, or a stretch-glyph; it is
27352 interpreted in the same way as the value of a `display' text property.
27353
27354 This variable is overridden by any `line-prefix' text or overlay
27355 property.
27356
27357 To add a prefix to continuation lines, use `wrap-prefix'. */);
27358 Vline_prefix = Qnil;
27359 staticpro (&Qline_prefix);
27360 Qline_prefix = intern_c_string ("line-prefix");
27361 Fmake_variable_buffer_local (Qline_prefix);
27362
27363 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27364 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27365 inhibit_eval_during_redisplay = 0;
27366
27367 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27368 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27369 inhibit_free_realized_faces = 0;
27370
27371 #if GLYPH_DEBUG
27372 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27373 doc: /* Inhibit try_window_id display optimization. */);
27374 inhibit_try_window_id = 0;
27375
27376 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27377 doc: /* Inhibit try_window_reusing display optimization. */);
27378 inhibit_try_window_reusing = 0;
27379
27380 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27381 doc: /* Inhibit try_cursor_movement display optimization. */);
27382 inhibit_try_cursor_movement = 0;
27383 #endif /* GLYPH_DEBUG */
27384
27385 DEFVAR_INT ("overline-margin", overline_margin,
27386 doc: /* *Space between overline and text, in pixels.
27387 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27388 margin to the caracter height. */);
27389 overline_margin = 2;
27390
27391 DEFVAR_INT ("underline-minimum-offset",
27392 underline_minimum_offset,
27393 doc: /* Minimum distance between baseline and underline.
27394 This can improve legibility of underlined text at small font sizes,
27395 particularly when using variable `x-use-underline-position-properties'
27396 with fonts that specify an UNDERLINE_POSITION relatively close to the
27397 baseline. The default value is 1. */);
27398 underline_minimum_offset = 1;
27399
27400 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27401 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27402 This feature only works when on a window system that can change
27403 cursor shapes. */);
27404 display_hourglass_p = 1;
27405
27406 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27407 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27408 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27409
27410 hourglass_atimer = NULL;
27411 hourglass_shown_p = 0;
27412
27413 DEFSYM (Qglyphless_char, "glyphless-char");
27414 DEFSYM (Qhex_code, "hex-code");
27415 DEFSYM (Qempty_box, "empty-box");
27416 DEFSYM (Qthin_space, "thin-space");
27417 DEFSYM (Qzero_width, "zero-width");
27418
27419 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27420 /* Intern this now in case it isn't already done.
27421 Setting this variable twice is harmless.
27422 But don't staticpro it here--that is done in alloc.c. */
27423 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27424 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27425
27426 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27427 doc: /* Char-table defining glyphless characters.
27428 Each element, if non-nil, should be one of the following:
27429 an ASCII acronym string: display this string in a box
27430 `hex-code': display the hexadecimal code of a character in a box
27431 `empty-box': display as an empty box
27432 `thin-space': display as 1-pixel width space
27433 `zero-width': don't display
27434 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27435 display method for graphical terminals and text terminals respectively.
27436 GRAPHICAL and TEXT should each have one of the values listed above.
27437
27438 The char-table has one extra slot to control the display of a character for
27439 which no font is found. This slot only takes effect on graphical terminals.
27440 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27441 `thin-space'. The default is `empty-box'. */);
27442 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27443 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27444 Qempty_box);
27445 }
27446
27447
27448 /* Initialize this module when Emacs starts. */
27449
27450 void
27451 init_xdisp (void)
27452 {
27453 Lisp_Object root_window;
27454 struct window *mini_w;
27455
27456 current_header_line_height = current_mode_line_height = -1;
27457
27458 CHARPOS (this_line_start_pos) = 0;
27459
27460 mini_w = XWINDOW (minibuf_window);
27461 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27462 echo_area_window = minibuf_window;
27463
27464 if (!noninteractive)
27465 {
27466 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27467 int i;
27468
27469 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27470 set_window_height (root_window,
27471 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27472 0);
27473 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27474 set_window_height (minibuf_window, 1, 0);
27475
27476 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27477 mini_w->total_cols = make_number (FRAME_COLS (f));
27478
27479 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27480 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27481 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27482
27483 /* The default ellipsis glyphs `...'. */
27484 for (i = 0; i < 3; ++i)
27485 default_invis_vector[i] = make_number ('.');
27486 }
27487
27488 {
27489 /* Allocate the buffer for frame titles.
27490 Also used for `format-mode-line'. */
27491 int size = 100;
27492 mode_line_noprop_buf = (char *) xmalloc (size);
27493 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27494 mode_line_noprop_ptr = mode_line_noprop_buf;
27495 mode_line_target = MODE_LINE_DISPLAY;
27496 }
27497
27498 help_echo_showing_p = 0;
27499 }
27500
27501 /* Since w32 does not support atimers, it defines its own implementation of
27502 the following three functions in w32fns.c. */
27503 #ifndef WINDOWSNT
27504
27505 /* Platform-independent portion of hourglass implementation. */
27506
27507 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27508 int
27509 hourglass_started (void)
27510 {
27511 return hourglass_shown_p || hourglass_atimer != NULL;
27512 }
27513
27514 /* Cancel a currently active hourglass timer, and start a new one. */
27515 void
27516 start_hourglass (void)
27517 {
27518 #if defined (HAVE_WINDOW_SYSTEM)
27519 EMACS_TIME delay;
27520 int secs, usecs = 0;
27521
27522 cancel_hourglass ();
27523
27524 if (INTEGERP (Vhourglass_delay)
27525 && XINT (Vhourglass_delay) > 0)
27526 secs = XFASTINT (Vhourglass_delay);
27527 else if (FLOATP (Vhourglass_delay)
27528 && XFLOAT_DATA (Vhourglass_delay) > 0)
27529 {
27530 Lisp_Object tem;
27531 tem = Ftruncate (Vhourglass_delay, Qnil);
27532 secs = XFASTINT (tem);
27533 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27534 }
27535 else
27536 secs = DEFAULT_HOURGLASS_DELAY;
27537
27538 EMACS_SET_SECS_USECS (delay, secs, usecs);
27539 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27540 show_hourglass, NULL);
27541 #endif
27542 }
27543
27544
27545 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27546 shown. */
27547 void
27548 cancel_hourglass (void)
27549 {
27550 #if defined (HAVE_WINDOW_SYSTEM)
27551 if (hourglass_atimer)
27552 {
27553 cancel_atimer (hourglass_atimer);
27554 hourglass_atimer = NULL;
27555 }
27556
27557 if (hourglass_shown_p)
27558 hide_hourglass ();
27559 #endif
27560 }
27561 #endif /* ! WINDOWSNT */